A releases page mints nothing of its own and reads no log: the app's kept read token, and one kv entry for each release
Jonas Templestein, 2026-11-27 13:13 UTC · dc8c0ea1452eac3b5178e0adda13a0b6f7bb849a · parent c8cf2c5
5 files changed
M app.ts
@@ -187,7 +187,8 @@ if (match[2] === "commits") return await commitsPage(view, handle); if (match[2] === "commit") return await commitPage(view, handle, itx, rest); if (match[2] === "branches") return await branchesPage(view, handle, itx); - if (match[2] === "releases") return await releasesPage(view, itx); + if (match[2] === "releases") + return await releasesPage(view, itx, await readRemote(itx, repo, remotes)); if (match[2] === "pulls") return await pullsPage(view, handle, itx); if (match[2] === "pull") return await pullPage(view, handle, itx, rest); if (match[2] === "compare") return await comparePage(view, handle, itx, rest); @@ -208,6 +209,37 @@ } // ── THE REMOTE ── + +/** Where a repo's remote is, with the app's read token for it: both from this isolate's memory + * while they last, so a page that reads the remote (the releases, which are tags) mints at most + * one token in ten minutes here, whoever asks and however often. Each is kept as a value, after + * its answer came (`Remotes`). */ +async function readRemote( + itx: ReturnType<Host["getItx"]>, + repo: string, + remotes: Remotes, +): Promise<{ address: string; authorization: string }> { + const now = Date.now(); + const keptAddress = remotes.addresses.get(repo); + const keptToken = remotes.readTokens.get(repo); + const artifacts = itx.cfArtifacts.get(repo); + const [address, authorization] = await Promise.all([ + keptAddress ?? + artifacts + .then((found) => found.remote()) + .then((found) => (remotes.addresses.set(repo, found), found)), + keptToken && keptToken.until > now + 60_000 + ? keptToken.authorization + : artifacts + .then((found) => found.createToken("read", READ_TOKEN_SECONDS)) + .then((made) => { + const minted = `Basic ${btoa(`x:${made.plaintext}`)}`; + remotes.readTokens.set(repo, { authorization: minted, until: now + READ_TOKEN_SECONDS * 1000 }); + return minted; + }), + ]); + return { address, authorization }; +} async function remote( request: Request,
M pages.ts
@@ -23,7 +23,7 @@ import { z } from "zod"; import { plainDiff } from "./plain-diff.ts"; import { type LinePlace, type Pull, pullsIn, type Standing, standingOf } from "./pulls.ts"; -import { type Release, releasesIn } from "./releases.ts"; +import { type Release, type Remote, releasesIn } from "./releases.ts"; type Host = Pick<IterateConfigEntrypoint, "getItx" | "auth">; export type Itx = ReturnType<Host["getItx"]>; @@ -348,10 +348,11 @@ /** The repo's releases, newest first: each one's tag, the commit it stands on, who cut it and * when, and its notes. A release is a tag of git (releases.ts), so the page says how git gets - * one. */ -export async function releasesPage(view: View, itx: Itx) { + * one. `remote` is where the tags are read, with the app's kept read token (app.ts): anyone may + * ask for a public repo's page, and a page mints nothing of its own. */ +export async function releasesPage(view: View, itx: Itx, remote: Remote) { const { repo, url } = view; - const releases = await releasesIn(itx, repo); + const releases = await releasesIn(itx, repo, remote); const row = (release: Release) => { const commit = `<a href="${esc(repo)}/commit/${release.commitOid}"><code>${release.commitOid.slice(0, 7)}</code></a>`; const cut = release.at
M releases.ts
@@ -17,6 +17,11 @@ // (`source.principal`, `createdAt`), which the platform stamps. A tag that git pushed by hand has // no such event: the page shows it with its commit alone. // +// WHAT A PAGE READS IS SMALL, whoever asks and however often: the tags, with a read token that +// the app keeps while it lasts (app.ts), and for the newest tags one kv entry each, +// `git-app:release:<repo>:<tag>`, which the cut writes beside its event. The repo's log is never +// read for a page: it grows with every push and pull, and anyone may ask for a public repo's page. +// // TO CUT A RELEASE IS THE ONE CALL (`cutRelease`, which the mind's API offers as // `itx.config.releaseCut`). With no version named, the next one raises the patch: v1.2.7 becomes // v1.2.8, and a repo's first release is v0.1.0. When main is at the newest release already, @@ -28,7 +33,6 @@ // Cloudflare Artifacts remote, with a token that is minted for the one request (app.ts, "THE // REMOTE"). A tag's push carries one object, the tag itself. import type { IterateConfigEntrypoint } from "iterate/sdk"; -import type { StreamEvent } from "iterate/stream/processor"; import { z } from "zod"; type Itx = ReturnType<IterateConfigEntrypoint["getItx"]>; @@ -54,16 +58,12 @@ notes: string | null; }; -const Cut = z.object({ - tag: z.string().min(1), - version: z.string().min(1), - commitOid: z.string().regex(/^[0-9a-f]{40}$/), - previous: z.string().nullable(), - notes: z.string(), -}); - const TAGS = "refs/tags/"; const ZERO_OID = "0".repeat(40); +/** How many releases a list holds, newest first, and how many of them with who, when and notes: + * one kv read each. */ +const RELEASES_LISTED = 200; +const RELEASES_WITH_NOTES = 30; /** How far back main is read for the notes. */ const HISTORY_READ = 300; /** The most that is read of one answer of the remote: a list of refs, or a push's report. */ @@ -104,24 +104,52 @@ // ── THE RELEASES OF A REPO ── -/** The repo's releases, newest first: its version tags by version, then any other tag by name. - * Each has what its `release/cut` event says, when it has one. */ -export async function releasesIn(itx: Itx, repo: string): Promise<Release[]> { - const [tags, cuts] = await Promise.all([ - readTags(await remoteOf(itx, repo, "read")), - cutsIn(itx, repo), - ]); - return releasesOf(tags, cuts); +/** What the cut keeps of a release in the mind's kv, for a page: what its event says. */ +const Kept = z.object({ + commitOid: z.string(), + by: z.string().nullable(), + at: z.string(), + previous: z.string().nullable(), + notes: z.string(), +}); +type Kept = z.infer<typeof Kept>; +const keptKey = (repo: string, tag: string) => `git-app:release:${repo}:${tag}`; + +/** The repo's releases, newest first: its version tags by version, then any other tag by name, + * at most 200. The newest 30 have what their cut kept: who, when, the notes. `remote` is where + * the tags are read, with its credential; with none, a token is minted for this one read. */ +export async function releasesIn(itx: Itx, repo: string, remote?: Remote): Promise<Release[]> { + const tags = orderOf(await readTags(remote || (await remoteOf(itx, repo, "read")))).slice(0, RELEASES_LISTED); + const kept = new Map<string, Kept>(); + await Promise.all( + tags.slice(0, RELEASES_WITH_NOTES).map(async ({ name }) => { + const text = await itx.kv.get(keptKey(repo, name)); + if (!text) return; + try { + const parsed = Kept.safeParse(JSON.parse(text)); + if (parsed.success) kept.set(name, parsed.data); + } catch { + // an entry that is no JSON is no entry + } + }), + ); + return releasesOf(tags, kept); } -export function releasesOf(tags: Tag[], cuts: Map<string, Omit<Release, "tag" | "version" | "commitOid"> & { commitOid: string }>): Release[] { +/** Version tags by version, newest first, then any other tag by name. */ +function orderOf(tags: Tag[]): Tag[] { const versions = versionsIn(tags); const others = tags .filter((tag) => !versions.some(({ name }) => name === tag.name)) .sort((a, b) => (a.name < b.name ? 1 : -1)); - return [...versions, ...others].map((tag) => { - const cut = cuts.get(tag.name); - // an event that names another commit than the tag has is not this tag's + return [...versions, ...others]; +} + +/** Each tag, in the order given, with what was kept of its cut. */ +export function releasesOf(tags: Tag[], kept: Map<string, Kept>): Release[] { + return tags.map((tag) => { + const cut = kept.get(tag.name); + // an entry that names another commit than the tag has is not this tag's const said = cut && cut.commitOid === tag.commitOid ? cut : null; const version = versionOf(tag.name); return { @@ -136,31 +164,6 @@ }); } -/** What the repo's `release/cut` events say, by tag: the first event of a tag is the tag's. Only - * those few fields are kept of the log. */ -async function cutsIn(itx: Itx, repo: string) { - const cuts = new Map<string, { commitOid: string; by: string | null; at: string; previous: string | null; notes: string }>(); - let after = 0; - for (;;) { - const page = await itx.cd(repo).readEvents(after, 1000); - for (const event of page.events as StreamEvent[]) { - if (event.type !== "release/cut") continue; - const cut = Cut.safeParse(event.payload || {}); - if (!cut.success || cuts.has(cut.data.tag)) continue; - const person = event.source.principal || event.source.onBehalfOf?.principal; - cuts.set(cut.data.tag, { - commitOid: cut.data.commitOid, - by: person?.email?.split("@")[0] || person?.name || null, - at: event.createdAt, - previous: cut.data.previous, - notes: cut.data.notes, - }); - } - if (page.atHead) return cuts; - after = page.scannedThroughOffset; - } -} - // ── CUT A RELEASE ── export type CutInput = { @@ -225,7 +228,17 @@ const payload = { tag, version: version.join("."), commitOid: tip.oid, previous: latest?.name || null, notes }; // one tag is cut once: a second try of this call lands no second event - await itx.cd(repo).append({ type: "release/cut", idempotencyKey: `release/cut:${tag}`, payload }); + const [event] = await itx.cd(repo).append({ type: "release/cut", idempotencyKey: `release/cut:${tag}`, payload }); + // what a page reads of this release, so that no page reads the log + const person = event?.source?.principal || event?.source?.onBehalfOf?.principal; + const kept: Kept = { + commitOid: tip.oid, + by: person?.email?.split("@")[0] || person?.name || null, + at: event?.createdAt || new Date().toISOString(), + previous: payload.previous, + notes, + }; + await itx.kv.put(keptKey(repo, tag), JSON.stringify(kept)); return { cut: true, ...payload }; } @@ -322,6 +335,9 @@ const [oid, ref, ...attributes] = line.split(" "); if (!oid || !/^[0-9a-f]{40}$/.test(oid) || !ref?.startsWith(TAGS)) continue; const peeled = attributes.find((attribute) => attribute.startsWith("peeled:"))?.slice("peeled:".length); + // the remote's words are checked before a page puts them in an address + if (peeled !== undefined && !/^[0-9a-f]{40}$/.test(peeled)) continue; + if (!isTagName(ref.slice(TAGS.length))) continue; tags.push({ name: ref.slice(TAGS.length), oid, commitOid: peeled || oid }); } return tags;
M tests/local-mind.mjs
@@ -22,6 +22,8 @@ export function localMind({ folders, member, remote }) { const kv = {}; const asked = []; + // each token that was minted, as `<scope> <repo>` + const minted = []; const events = {}; // each event a minute after the one before, from noon on 24 Nov 2026 let minutes = 0; @@ -173,7 +175,10 @@ if (!remote) throw new Error("this local mind was given no remote"); return `${remote}${repo}`; }, - createToken: async (scope) => ({ plaintext: `art_${scope}` }), + createToken: async (scope) => { + minted.push(`${scope} ${repo}`); + return { plaintext: `art_${scope}` }; + }, }), }, kv: { @@ -220,6 +225,7 @@ return { kv, asked, + minted, events, getItx: () => own, as: (address) => itxOf(principalOf(address)),
M tests/releases.test.mjs
@@ -146,9 +146,18 @@ ); const git = gitApp({ public: [repo], ...look }); + const eventsRead = []; + const own = mind.getItx(); + const cd = own.cd; + own.cd = (path) => ({ ...cd(path), readEvents: async (...args) => (eventsRead.push(path), cd(path).readEvents(...args)) }); + mind.minted.length = 0; const answer = await git.fetch(new Request("https://git.example.com/repos/kernel/releases"), mind); const html = await answer.text(); assert.equal(answer.status, 200); + // ANYONE MAY ASK, AND OFTEN: a second page mints no second token, and no page reads the log + await (await git.fetch(new Request("https://git.example.com/repos/kernel/releases"), mind)).text(); + assert.deepEqual(mind.minted, ["read /repos/kernel"]); + assert.deepEqual(eventsRead, []); assert.ok(html.includes(`<a href="/repos/kernel/releases" aria-current="page">Releases</a>`)); const rows = html.slice(html.indexOf(`<ul class="releases">`), html.indexOf("</ul>", html.indexOf(`<ul class="releases">`))); assert.deepEqual([...rows.matchAll(/<li id="([^"]+)"><b>([^<]+)<\/b>/g)].map((row) => row[2]), ["v0.1.1", "v0.1.0", "by-hand"]);