Import stdlib by its name, and tell the mind's repo after a push that moved main
Misha Kaletsky, 2026-11-25 17:42 UTC · ee1aeeab3e2be899e51360f919103018a28cd4b2 · parent d1936bf
3 files changed
M app.ts
@@ -27,18 +27,21 @@ // platform records who did it. This file writes nothing of a pull. // // THE REMOTE IS A PASS-THROUGH. Each of git's three requests goes to the repo's Cloudflare -// Artifacts remote as it came, body streamed, answer streamed back. The app adds two things. The -// address is the mind's own. And for a public repo it adds a read token of its own, minted once -// and kept while it lasts. Artifacts checks every token, not this file. +// Artifacts remote as it came, body streamed, answer streamed back. The app adds three things. +// The address is the mind's own. For a public repo it adds a read token of its own, minted once +// and kept while it lasts. And after a push that moved `main` it tells the mind's repo, which is +// how the mind comes to publish a push to a repo that was made after the deploy. Artifacts checks +// every token, not this file. // // THE READ TOKEN IS KEPT IN THE MIND'S KV, not in memory: most connections get a new isolate, so // memory alone would mint a token for almost every clone. A kv read takes 12 to 32 ms, a mint 150 // (measured on tthq.app, 24 Nov 2026). What stops a push is Artifacts' own limit of 64 MiB a // push, with the same words through this app as straight to Artifacts. +import { reportIssue } from "iterate/lib"; import { type AppDescription, type IterateConfigEntrypoint, serveFiles } from "iterate/sdk"; import { z } from "zod"; -import { openFile } from "../stdlib/open-files.ts"; -import theme from "../stdlib/theme.css"; +import { openFile } from "@repos/stdlib/open-files.ts"; +import theme from "@repos/stdlib/theme.css"; import { branchesPage, codePage, @@ -92,7 +95,7 @@ /** `public`: the repos that anyone may read, clone and fetch, by path (`/repos/<name>`). * `icon`: the answer for the mind's icon, which every app serves as its own - * (../stdlib/open-files.ts). */ + * (open-files.ts of /repos/stdlib). */ export function gitApp({ public: publicRepos, icon }: { public: string[]; icon: () => Response }) { return { /** The app's description and icon, for anyone: the mind's sign-in pages ask for both. */ @@ -220,6 +223,27 @@ const value = upstream.headers.get(name); if (value) answerHeaders.set(name, value); } + // A PUSH THAT MOVED MAIN: the repo is told, so the mind publishes what was pushed. Git's report + // of a push is small, so it is read whole and passed on as it came. + if (verb === "git-receive-pack" && upstream.ok) { + const report = await upstream.arrayBuffer(); + if (new TextDecoder().decode(report).includes("ok refs/heads/main\n")) + await tellOfNewTip(host, repo); + return new Response(report, { status: upstream.status, headers: answerHeaders }); + } return new Response(upstream.body, { status: upstream.status, headers: answerHeaders }); +} + +/** Tells a repo of the mind that its main has a new tip. Cloudflare tells the mind of a push too, + * some seconds later, but only for a repo that the deploy knew: `kernel` and `config`. For those + * the repo finds the commit told already, and does nothing. A failure is reported and the push + * stands: the commits are in the repo, and `itx.repos.get(repo).announceTip()` tells it again. */ +async function tellOfNewTip(host: Host, repo: string): Promise<void> { + try { + using itx = host.getItx(); + await itx.repos.get(repo).announceTip(); + } catch (error) { + reportIssue("git-app.push-not-announced", error, { repo }); + } }
A package.json
@@ -0,0 +1,8 @@ +{ + "name": "@repos/git", + "private": true, + "type": "module", + "dependencies": { + "@repos/stdlib": "../stdlib" + } +}
M tests/app.test.mjs
@@ -114,7 +114,7 @@ }), mind, ); - assert.equal(await pushed.text(), "pack for: the pack"); + assert.equal(await pushed.text(), "unpack ok\n"); assert.deepEqual( remote.requests.map(({ url, authorization }) => ({ url, authorization })), [ @@ -125,6 +125,50 @@ assert.deepEqual(mind.minted, []); }); +test("a push that moved main tells the mind's repo, so the mind publishes what was pushed; a push of another branch tells it nothing", async () => { + await using remote = await artifactsRemote(); + const mind = fakeMind(remote); + const git = gitApp({ public: [], ...look }); + const push = (ref) => + git.fetch( + new Request("https://git.example.com/repos/kernel/git-receive-pack", { + method: "POST", + headers: { authorization: `Basic ${btoa("x:a-write-token")}` }, + body: `the pack, for ${ref}`, + }), + mind, + ); + + const branch = await push("refs/heads/add-b"); + assert.equal(await branch.text(), "unpack ok\nok refs/heads/add-b\n"); + assert.deepEqual(mind.announced, []); + + const main = await push("refs/heads/main"); + // git gets the remote's report as it came + assert.equal(await main.text(), "unpack ok\nok refs/heads/main\n"); + assert.deepEqual(mind.announced, ["/repos/kernel"]); +}); + +test("a push stands when the mind's repo could not be told: git gets the remote's report", async () => { + await using remote = await artifactsRemote(); + const mind = fakeMind(remote); + mind.away.add("/repos/kernel"); + const git = gitApp({ public: [], ...look }); + + const pushed = await git.fetch( + new Request("https://git.example.com/repos/kernel/git-receive-pack", { + method: "POST", + headers: { authorization: `Basic ${btoa("x:a-write-token")}` }, + body: "the pack, for refs/heads/main", + }), + mind, + ); + + assert.equal(pushed.status, 200); + assert.equal(await pushed.text(), "unpack ok\nok refs/heads/main\n"); + assert.deepEqual(mind.announced, []); +}); + test("a read token that is about to end is replaced, and the repo's remote is not asked for again", async () => { await using remote = await artifactsRemote(); const mind = fakeMind(remote); @@ -186,11 +230,14 @@ const look = { icon: () => new Response("the icon") }; /** The mind as the app reaches it: a kv, and the Artifacts repos `kernel` and `config`, served - * by `remote`. It records each token it mints and each repo whose remote it was asked for. */ + * by `remote`. It records each token it mints, each repo whose remote it was asked for, and each + * repo that was told its main has a new tip. A repo in `away` throws when it is told. */ function fakeMind(remote) { const kv = {}; const minted = []; const remotesAsked = []; + const announced = []; + const away = new Set(); const repos = ["/repos/kernel", "/repos/config"]; const itx = { kv: { @@ -213,10 +260,27 @@ }, }), }, + repos: { + get: (repo) => ({ + announceTip: async () => { + if (away.has(repo)) throw new Error(`the repo ${repo} is away`); + announced.push(repo); + return "the tip"; + }, + }), + }, [Symbol.dispose]() {}, }; // what is no git request is asked whether it is a member's socket: here none is - return { kv, minted, remotesAsked, getItx: () => itx, auth: { memberPage: () => null } }; + return { + kv, + minted, + remotesAsked, + announced, + away, + getItx: () => itx, + auth: { memberPage: () => null }, + }; } /** A repo's Artifacts remote: a server on this machine that records each request and answers as @@ -246,9 +310,13 @@ } const path = request.url.split("?")[0]; response.writeHead(200, { "content-type": "application/x-git-advertisement" }); - response.end( - path.endsWith("/info/refs") ? `refs of ${path.replace("/info/refs", "")}` : `pack for: ${body}`, - ); + if (path.endsWith("/info/refs")) response.end(`refs of ${path.replace("/info/refs", "")}`); + // git's report of a push: the pack was read, and each branch that the push named was moved + else if (path.endsWith("/git-receive-pack")) + response.end( + ["unpack ok\n", ...(body.match(/refs\/heads\/\S+/g) || []).map((ref) => `ok ${ref}\n`)].join(""), + ); + else response.end(`pack for: ${body}`); }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); return {