A repo lists its branches and reads history from any commit
Misha Kaletsky, 2026-11-24 22:21 UTC · 8ad14475d0ef5ab7af47f2889dc7bbc5e899247d · parent 5c8b224
5 files changed
- M core/itx/src/context-stub.ts
- M core/lib/src/api.ts
- M core/mind-worker/src/repo/durable-object.test.ts
- M core/mind-worker/src/repo/durable-object.ts
- M core/mind-worker/src/repo/git-wire.ts
M core/itx/src/context-stub.ts
@@ -218,7 +218,7 @@ ] satisfies (keyof StreamProcessorDurableObject)[] ).map((read) => `facets.get.${read}`), "repos.list", - ...["tip", "readFile", "modules", "listFiles", "log", "origin"].map( + ...["tip", "readFile", "modules", "listFiles", "log", "branches", "origin"].map( (verb) => `repos.get.${verb}`, ), "workspaces.list",
M core/lib/src/api.ts
@@ -1312,7 +1312,12 @@ parent?: string | null; }): Promise<RepoCommitResult>; writeFile(path: string, content: string): Promise<RepoCommitResult>; - log(options?: { limit?: number }): Promise<RepoLogEntry[]>; + /** The newest commits, newest first, each step along the commit's first parent: from main's + * tip, or `from` any commit (a branch's tip, which reads a proposal's own commits). */ + log(options?: { limit?: number; from?: string }): Promise<RepoLogEntry[]>; + /** Every branch with its newest commit. The mind reads and publishes `main`; a push to any + * other branch changes nothing that runs. */ + branches(): Promise<{ name: string; commitOid: string }[]>; /** Lands the tip's `repo/commit-completed` fact again, and answers the tip. On a stack whose * queue has the repo's Artifacts subscription (the config repo's, made at each deploy), a * `git push` to the repo's remote lands the fact by itself, so `/repos/config` publishes. Call
M core/mind-worker/src/repo/durable-object.test.ts
@@ -201,6 +201,27 @@ expect(git).toMatchObject({ packs: 2, mostPacksInFlight: 1 }); }); +test("a repo lists its branches, each with its newest commit, and reads the history of any of them", async () => { + const [proposal, newest, first] = await commitsOf(["a proposal", "two", "one"]); + const repo = repoWith([]); + serveGit(repo.storage, [proposal!, newest!, first!], { + artifacts: newest!.oid, + branches: { "a-proposal": proposal!.oid }, + }); + + expect(await repo.facet.branches()).toEqual([ + { name: "a-proposal", commitOid: proposal!.oid }, + { name: "main", commitOid: newest!.oid }, + ]); + // from the branch's tip: its own commit, then main's that it stands on + expect(await gitLog(repo.facet, { from: proposal!.oid, limit: 2 })).toMatchObject([ + { oid: proposal!.oid, message: "a proposal", parents: [newest!.oid] }, + { oid: newest!.oid, message: "two", parents: [first!.oid] }, + ]); + // with no `from`, main's + expect(await gitLog(repo.facet, { limit: 1 })).toMatchObject([{ oid: newest!.oid }]); +}); + test("a pull's merge fetches its two tips one after the other", async () => { const [theirs, ours] = await commitsOf(["two", "one"]); const repo = repoWith([]); @@ -330,6 +351,12 @@ }; } +/** The facet's own `log`: the facet that `repoWith` hosts answers `log` from the oids the test + * gave it, so the git log itself is read through the class. */ +function gitLog(facet: RepoFacet, options: { from?: string; limit: number }) { + return RepoFacet.prototype.log.call(facet, options); +} + type Commit = { oid: string; objects: { payload: Uint8Array; type: GitObjectType }[] }; /** Commits of one file each, newest first, each the child of the next: `notes.md` is its word. */ @@ -370,7 +397,7 @@ function serveGit( storage: ReturnType<typeof facetStorage>, history: Commit[], - tips: { artifacts: string; origin?: string }, + tips: { artifacts: string; origin?: string; branches?: Record<string, string> }, ) { const until = Date.now() + 3_600_000; void storage.put("git-remote", "https://artifacts.example/config.git"); @@ -395,7 +422,10 @@ const tip = new URL(request.url).hostname === "origin.example" ? tips.origin : tips.artifacts; if (body.includes("command=ls-refs")) { git.lsRefs += 1; - return gitAnswer([pktLine(`${tip} refs/heads/main`)]); + const others = Object.entries(tips.branches || {}).map(([name, oid]) => + pktLine(`${oid} refs/heads/${name}`), + ); + return gitAnswer([pktLine(`${tip} refs/heads/main`), ...others]); } const want = history.findIndex(({ oid }) => body.includes(`want ${oid}`)); if (body.includes("deepen 1\n")) {
M core/mind-worker/src/repo/durable-object.ts
@@ -109,6 +109,7 @@ "commitFiles", "writeFile", "log", + "branches", "origin", "setOrigin", "pull", @@ -881,13 +882,31 @@ return this.commitFiles({ message: `write ${path}`, changes: [{ path, content }] }); } - async log(options: { limit?: number } = {}): Promise<RepoLogEntry[]> { + // EVERY BRANCH, with its newest commit. The mind reads `main` and publishes it. Any other branch + // is somebody's proposal: a push to it publishes nothing (the reduce above takes main's alone). + async branches(): Promise<{ name: string; commitOid: string }[]> { await this.#created(); + const heads = "refs/heads/"; + const transport = await this.#transport("read"); + return (await transport.refsUnder(heads)) + .map(({ name, oid }) => ({ name: name.slice(heads.length), commitOid: oid })) + .sort((a, b) => (a.name < b.name ? -1 : 1)); + } + + // From main's tip, or `from` any commit: a branch's tip, which is how a proposal's own commits + // are read. Each step follows the commit's first parent. + async log(options: { limit?: number; from?: string } = {}): Promise<RepoLogEntry[]> { + await this.#created(); const limit = options.limit ?? 20; if (!Number.isInteger(limit) || limit < 1) throw new Error(`repo.log: limit must be a positive integer (got ${String(limit)})`); + const from = z + .string() + .regex(/^[a-f0-9]{40}$/) + .optional() + .parse(options.from); const transport = await this.#transport("read"); - const tip = await transport.tipOf(REF); + const tip = from || (await transport.tipOf(REF)); if (!tip) return []; const objects = new Map<string, RawGitObject>( (await transport.fetchObjects({ wants: [tip], deepen: limit })).map((o) => [o.oid, o]),
M core/mind-worker/src/repo/git-wire.ts
@@ -873,6 +873,12 @@ parseLsRefs(await post("git-upload-pack", encodeLsRefsRequest([ref]))).find( (entry) => entry.name === ref, )?.oid, + /** Every ref whose name starts with `prefix`, each with its commit: `refs/heads/` asks for the + * branches. */ + refsUnder: async (prefix: string): Promise<{ name: string; oid: string }[]> => + parseLsRefs(await post("git-upload-pack", encodeLsRefsRequest([prefix]))).filter((entry) => + entry.name.startsWith(prefix), + ), push: async (request: { newOid: string; oldOid: string;