itx.cfArtifacts lists a repo's tokens and ends one by its id (#6)
jonas, 2026-11-26 17:24 UTC · 6e19dc2a3f36aff7ffd1533810a7b5716b00710e · parent 68f727b
4 files changed
- M core/lib/src/api.ts
- A core/mind-worker/src/context/cf-artifacts.test.ts
- M core/mind-worker/src/context/cf-artifacts.ts
- M core/mind-worker/src/itx-host.ts
M core/lib/src/api.ts
@@ -1069,16 +1069,35 @@ text(content: string, options: TextOptions): CfImageTransformer; }; -/** A token for an Artifacts repo's git remote. */ +/** A token for an Artifacts repo's git remote. `id` names it to `revokeToken` and in + * `listTokens`, and opens nothing. */ export interface ArtifactToken { + id?: string; plaintext: string; expiresAt?: string; } -/** `itx.cfArtifacts.get(path)`: the repo's handle — a token for its remote, and the remote's - * git-over-HTTPS URL. A repo that does not exist fails at these, with the binding's own error. */ +/** What Artifacts says of a token after the mint: never its text. */ +export interface ArtifactTokenInfo { + id: string; + scope: "read" | "write"; + state: "active" | "expired" | "revoked"; + createdAt: string; + expiresAt: string; +} + +/** `itx.cfArtifacts.get(path)`: the repo's handle — a token for its remote, the tokens that + * Artifacts has for it, and the remote's git-over-HTTPS URL. A repo that does not exist fails at + * these, with the binding's own error. */ export interface CfArtifactRepoApi { createToken(scope: "read" | "write", ttlSeconds: number): Promise<ArtifactToken>; + /** The tokens of this repo that still open it, with no token's text. One that ended, by its + * date or by `revokeToken`, is not listed. */ + listTokens(): Promise<{ tokens: ArtifactTokenInfo[]; total: number }>; + /** Ends one token of this repo, by its id. A token's text is refused: pass none, a script's + * source is on its caller's log. True also for a token that has ended already; false when the + * repo never had a token of that id. */ + revokeToken(id: string): Promise<boolean>; remote(): Promise<string>; }
A core/mind-worker/src/context/cf-artifacts.test.ts
@@ -0,0 +1,125 @@ +// A repo's tokens through `itx.cfArtifacts.get(path)` (./cf-artifacts.ts), against a stand-in for +// the Artifacts binding that answers as Artifacts did on a dev mind (26 Nov 2026): each repo's +// tokens apart, an ended token not listed, and true again for a second revocation. +import { expect, test } from "vitest"; +import { type ArtifactsNamespace, mindScopedArtifacts } from "./cf-artifacts.ts"; + +test("a repo's tokens are listed with no token's text, and one is ended by its id", async () => { + const store = storeOf(["repos--config", "repos--git"]); + const artifacts = mindScopedArtifacts({ namespace: store.namespace }); + const config = await artifacts.get("/repos/config"); + const minted = await config.createToken("write", 300); + await config.createToken("read", 60); + + const listed = await config.listTokens(); + expect(listed).toEqual({ + tokens: [ + { id: "tok_1", scope: "write", state: "active", createdAt: CREATED, expiresAt: EXPIRES }, + { id: "tok_2", scope: "read", state: "active", createdAt: CREATED, expiresAt: EXPIRES }, + ], + total: 2, + }); + // not the token, and no field that the stand-in adds beside the known ones + expect(JSON.stringify(listed)).not.toContain("art_"); + expect(JSON.stringify(listed)).not.toContain("note"); + + expect(await config.revokeToken(minted.id!)).toBe(true); + // an ended token is not listed + expect((await config.listTokens()).tokens.map(({ id }) => id)).toEqual(["tok_2"]); + // an id that the repo never had: not found + expect(await config.revokeToken("tok_9")).toBe(false); +}); + +test("a repo's handle sees and ends that repo's tokens alone", async () => { + const store = storeOf(["repos--config", "repos--git"]); + const artifacts = mindScopedArtifacts({ namespace: store.namespace }); + const config = await artifacts.get("/repos/config"); + const git = await artifacts.get("/repos/git"); + const ofConfig = await config.createToken("write", 300); + + expect(await git.listTokens()).toEqual({ tokens: [], total: 0 }); + expect(await git.revokeToken(ofConfig.id!)).toBe(false); + expect((await config.listTokens()).tokens[0]!.state).toBe("active"); +}); + +test("a revocation whose answer was lost is asked again, and is true for the token that the first attempt ended", async () => { + const store = storeOf(["repos--config"]); + const config = await mindScopedArtifacts({ namespace: store.namespace }).get("/repos/config"); + const minted = await config.createToken("write", 300); + + // Artifacts ends the token, and its answer is the internal error that the proxy tries again on + store.loseNextRevokeAnswer(); + expect(await config.revokeToken(minted.id!)).toBe(true); + expect(store.revokeAsked).toEqual([minted.id, minted.id]); + store.loseNextRevokeAnswer(); + expect(await config.revokeToken("tok_9")).toBe(false); +}); + +test("a token's own text is refused before the binding is asked, so no caller learns to pass one", async () => { + const store = storeOf(["repos--config"]); + const config = await mindScopedArtifacts({ namespace: store.namespace }).get("/repos/config"); + const minted = await config.createToken("write", 300); + + for (const text of [minted.plaintext, "art_v1_abc", "", "an id with spaces", "x".repeat(129)]) + await expect(config.revokeToken(text)).rejects.toThrow("takes a token's id"); + expect(store.revokeAsked).toEqual([]); + expect((await config.listTokens()).tokens[0]!.state).toBe("active"); +}); + +test("a repo that is not there fails with the binding's own words", async () => { + const store = storeOf([]); + const away = await mindScopedArtifacts({ namespace: store.namespace }).get("/repos/away"); + await expect(away.listTokens()).rejects.toThrow("not found"); + await expect(away.revokeToken("tok_1")).rejects.toThrow("not found"); +}); + +const CREATED = "2026-11-26T15:00:00.000Z"; +const EXPIRES = "2026-11-26T15:05:00.000Z"; + +/** The binding, as much as the proxy calls: repos by name, each with its own tokens. A token's + * row holds a field that the proxy does not know (`note`). */ +function storeOf(names: string[]) { + type Row = { id: string; scope: "read" | "write"; state: "active" | "revoked"; text: string }; + const repos = new Map<string, Row[]>(names.map((name) => [name, []])); + const revokeAsked: string[] = []; + let made = 0; + let answerIsLost = false; + const namespace = { + async get(name: string) { + const rows = repos.get(name); + if (!rows) throw new Error(`Repository not found: ${name}`); + const live = () => rows.filter((row) => row.state === "active"); + return { + info: async () => ({ remote: `https://artifacts.test/${name}.git` }), + async createToken(scope: "read" | "write") { + const row: Row = { id: `tok_${++made}`, scope, state: "active", text: `art_v1_${made}?expires=1` }; + rows.push(row); + return { id: row.id, plaintext: row.text, scope, expiresAt: EXPIRES }; + }, + listTokens: async () => ({ + tokens: live().map(({ id, scope, state }) => ({ + id, + scope, + state, + createdAt: CREATED, + expiresAt: EXPIRES, + note: "a field of a later version", + })), + total: live().length, + }), + async revokeToken(tokenOrId: string) { + revokeAsked.push(tokenOrId); + const row = rows.find((row) => row.id === tokenOrId); + if (row) row.state = "revoked"; + if (answerIsLost) { + answerIsLost = false; + throw new Error("An internal error occurred."); + } + return Boolean(row); + }, + }; + }, + // the cast: these tests call `get` alone + } as unknown as ArtifactsNamespace; + return { namespace, revokeAsked, loseNextRevokeAnswer: () => void (answerIsLost = true) }; +}
M core/mind-worker/src/context/cf-artifacts.ts
@@ -1,7 +1,12 @@ // A proxy to the binding, by the repo's path: git itself lives in the repo facet (../repo/). import { RpcTarget } from "capnweb"; import { failureKind, retryPlatformFailures, UPSTREAM_ONCE } from "iterate/platform-retry"; -import type { ArtifactToken, CfArtifactRepoApi, CfArtifactsApi } from "iterate/api"; +import type { + ArtifactToken, + ArtifactTokenInfo, + CfArtifactRepoApi, + CfArtifactsApi, +} from "iterate/api"; import { errorCode } from "iterate/lib"; import { unavailableError } from "core-itx"; @@ -19,6 +24,8 @@ // a method: the handle is a Workers-RPC stub, whose data fields do not cross the wire info(): Promise<{ remote: string }>; createToken(scope: "read" | "write", ttlSeconds: number): Promise<ArtifactToken>; + listTokens(): Promise<{ tokens: ArtifactTokenInfo[]; total: number }>; + revokeToken(tokenOrId: string): Promise<boolean>; } interface ArtifactListResult { repos: { name: string }[]; @@ -43,6 +50,44 @@ ), ); } + // The tokens of this repo that still open it, and never a token's text: Artifacts has none after + // the mint. A token that ended, by its date or by `revokeToken`, is not in the list (measured on + // a dev mind, 26 Nov 2026). Each row is copied field by field, so a field that Artifacts adds + // later does not leave the mind unread. + async listTokens() { + const { tokens, total } = await retryingOnePlatformFailure("listTokens", this.#name, () => + withArtifactRepoHandle(this.#namespace, this.#name, "listTokens", (handle) => + handle.listTokens(), + ), + ); + return { + tokens: tokens.map(({ id, scope, state, createdAt, expiresAt }) => ({ + id, + scope, + state, + createdAt, + expiresAt, + })), + total, + }; + } + // BY ITS ID ALONE. The binding takes a token's text too, and this refuses one, so that no caller + // learns to pass a token here: a script's source is on the caller's log before this runs, and + // nothing here can take a token out of it. The handle is this repo's, so an id of another + // repo's token is not found here: false. SAFE TO RUN TWICE: Artifacts answers true again for a + // token that has ended (measured on a dev mind, 26 Nov 2026), so a second attempt after a lost + // answer says the same as the first would have. + async revokeToken(id: string) { + if (typeof id !== "string" || !TOKEN_ID.test(id) || id.startsWith("art_")) + throw refusal( + "revokeToken takes a token's id, as listTokens() and createToken() answer it, and never the token itself", + ); + return await retryingOnePlatformFailure("revokeToken", this.#name, () => + withArtifactRepoHandle(this.#namespace, this.#name, "revokeToken", (handle) => + handle.revokeToken(id), + ), + ); + } async remote() { const { remote } = await retryingOnePlatformFailure("remote", this.#name, () => withArtifactRepoHandle(this.#namespace, this.#name, "info", (handle) => handle.info()), @@ -51,11 +96,15 @@ } } +/** A token's id as a caller may write it: short, and with no character of a token's text that an + * id has not (`?`, `=`). What an id of Artifacts looks like is Artifacts' to say. */ +const TOKEN_ID = /^[A-Za-z0-9_-]{1,128}$/; + // The handle and the answer object each keep this actor's session to Artifacts open until disposed. async function withArtifactRepoHandle<T>( namespace: ArtifactsNamespace, name: string, - method: "createToken" | "info", + method: "createToken" | "info" | "listTokens" | "revokeToken", verb: (handle: ArtifactRepoHandle) => Promise<T>, ): Promise<T> { const handle = await answeredInTime(`get(${name})`, namespace.get(name));
M core/mind-worker/src/itx-host.ts
@@ -137,7 +137,8 @@ new ItxAi(env.AI, { mindSlug: config().controlPlaneHostname, path: scope.path }), }, cfArtifacts: { - description: "the Artifacts binding, mind-scoped (`repos` is the friendlier surface)", + description: + "the Artifacts binding, mind-scoped (`repos` is the friendlier surface); a repo's tokens: `(await cfArtifacts.get(path)).createToken(scope, ttlSeconds)` · `listTokens()` → { tokens: [{ id, scope, state, createdAt, expiresAt }], total }, the live ones, never a token's text · `revokeToken(id)`", build: () => mindScopedArtifacts({ namespace: env.ARTIFACTS }), }, r2: {