In a sandbox of the mind, git's helper asks the mind for the repo's token
Jonas Templestein, 2026-11-26 12:53 UTC · a57e25d93ab897b7ab4f7f8d3177106761a2d0d7 · parent 1b626b0
4 files changed
- M core/lib/src/cli/README.md
- M core/lib/src/cli/cli.ts
- M core/lib/src/cli/git.test.ts
- M core/lib/src/cli/git.ts
M core/lib/src/cli/README.md
@@ -82,6 +82,11 @@ and git stops without asking for a password. Run under `npx`, the setup names the helper as `npx --yes iterate git-credential`; otherwise by the Node and the file that ran it. +In a sandbox of the mind, `iterate git setup <host>` signs nobody in. The helper there reaches +the mind as the code of the sandbox's own context, mints a write token for the one repo that git +names (`itx.cfArtifacts`, five minutes, kept nowhere) and hands it to git, and the host passes it +on. A sandbox whose requests the mind filters has no such session, and so no git. + Not tried on macOS or Windows. ## Deploy a mind
M core/lib/src/cli/cli.ts
@@ -23,7 +23,12 @@ openForSignIn, signInToMind, } from "../node.ts"; -import { mindApiUrlOf, mindMcpUrlOf, mindProvideConnectResourceOf } from "../mind-api.ts"; +import { + mindApiUrlOf, + mindMcpUrlOf, + mindProvideConnectResourceOf, + ownSandboxOf, +} from "../mind-api.ts"; import { MCP_CLIENTS, type McpClient, mcpAddArgvOf } from "./mcp-clients.ts"; import { connectChatgpt } from "./chatgpt.ts"; import { isCodingAgent } from "./coding-agent.ts"; @@ -232,6 +237,14 @@ const mind = await discoveredMind(address); return { issuer: mind.issuer, tokenEndpoint: mind.endpoints.token, clientId: "", token }; }, + // in a sandbox of this mind: the mind's own token for the repo, by the sandbox's own session + ...(ownSandboxOf(process.env, address) && { + async repoToken(repo) { + using connection = await connectMind({ address }); + const token = await connection.itx.cfArtifacts.get(repo).createToken("write", 300); + return token.plaintext; + }, + }), }); /** THE COMMAND THAT GIT RUNS AS ITS HELPER for a host of the mind at `address`: this same CLI, @@ -621,6 +634,14 @@ }) .handler(async ({ input }) => { const host = gitHostOf(input.host); + // in a sandbox of the mind there is nobody to sign in: the helper asks the mind itself + const told = process.env.ITERATE_MIND?.trim().toLowerCase() || ""; + if (!input.mind && ownSandboxOf(process.env, told)) { + writeGitConfig({ host, helper: gitHelperCommandOf(told), namesRepo: true }); + return { + message: `Git is set up for https://${host} in this sandbox, with no sign-in: it asks ${told} for each repo's token.`, + }; + } const mind = await discoveredMind(addressOf(input.mind || mindOfGitHost(host))); const signedIn = readSession(mindApiUrlOf(mind.address)) || process.env.ITERATE_MIND_TOKEN?.trim()
M core/lib/src/cli/git.test.ts
@@ -17,6 +17,7 @@ HOST_TOKEN_LEAST_LEFT_MS, mindOfGitHost, personIsThere, + repoOfGitPath, } = await import("./git.ts"); /** `test`, with the folder removed after the file's last test. */ const test = base.extend<{ folderRemoved: void }>({ @@ -45,11 +46,57 @@ [ ["--replace-all", "credential.https://git.example.com.helper", ""], ["--add", "credential.https://git.example.com.helper", "!iterate git-credential --mind m"], + ["--replace-all", "credential.https://git.example.com.useHttpPath", "false"], ["--replace-all", "http.https://git.example.com/.extraHeader", "X-Iterate-Git: 1"], ], ); + // in a sandbox git names the repo to the helper + expect(gitConfigOf({ host: "git.example.com", helper: "h", namesRepo: true })[2]).toEqual([ + "--replace-all", + "credential.https://git.example.com.useHttpPath", + "true", + ]); }); +test("in a sandbox of the mind, get answers the mind's own token for the repo that git names, and keeps nothing", async () => { + await run(issuerOf(), "erase"); + const issuer = issuerOf(); + const minted: string[] = []; + const io = { + ...issuer.io, + repoToken: async (repo: string) => { + minted.push(repo); + if (repo === "/repos/away") throw new Error("no such repo"); + return `token-of-${repo}`; + }, + }; + const get = (path: string) => + gitCredential({ + operation: "get", + asked: `protocol=https\nhost=git.example.com\n${path}\n`, + address: ADDRESS, + interactive: false, + io, + }); + expect(await get("path=repos/config.git")).toBe( + "username=iterate\npassword=token-of-/repos/config\n", + ); + expect(await get("path=repos/away")).toBe("quit=1\n"); + // git named no repo: the setup of a sandbox has it name one + expect(await get("")).toBe("quit=1\n"); + expect(minted).toEqual(["/repos/config", "/repos/away"]); + expect(issuer.said).toEqual([ + "iterate: this sandbox got no token for /repos/away from iterate.example.com: no such repo", + "iterate: git named no repo of git.example.com to its helper. Run this, then the same git command again:\n npx iterate git setup git.example.com", + ]); + // no session was asked for, no exchange was made, and nothing is kept + expect([issuer.sessions, issuer.asked, readSession("https://git.example.com")]).toEqual([ + [], + [], + null, + ]); +}); + test("a git host is a hostname, typed bare or as a remote's address, and its mind is the host without its first label", () => { expect([ gitHostOf("git.example.com"), @@ -58,6 +105,14 @@ ]).toEqual(["git.example.com", "git.example.com", "example.com"]); for (const typed of ["localhost", "http://git.example.com", "git.example.com:8443"]) expect(() => gitHostOf(typed)).toThrow("no https host"); +}); + +test("the path that git names is a repo of the mind, or none", () => { + expect( + ["repos/config", "/repos/config.git/", "repos/github.com/iterate/core", "config", "repos/a b", undefined].map( + repoOfGitPath, + ), + ).toEqual(["/repos/config", "/repos/config", "/repos/github.com/iterate/core", null, null, null]); }); test("what git writes is read line by line up to the empty line", () => {
M core/lib/src/cli/git.ts
@@ -20,6 +20,13 @@ // // WITH NO SIGN-IN the helper signs in while git waits, when a person is at the terminal. With // nobody there it prints the setup command and tells git to stop, so git asks for no password. +// +// IN A SANDBOX OF THE MIND there is no sign-in and none is needed: the CLI reaches the mind as the +// code of the sandbox's own context (../node.ts `connectMind`), and code there may mint a token +// for a repo as the mind's other code does (`itx.cfArtifacts`). So the helper answers git a write +// token for the one repo that git names, which lasts five minutes and is kept nowhere, and the +// host passes it on. `setup` there has git name the repo to the helper, and signs nobody in. A +// sandbox whose requests the mind filters has no such session, and so no git. import { spawnSync } from "node:child_process"; import { z } from "zod"; import { isCodingAgent } from "./coding-agent.ts"; @@ -58,19 +65,25 @@ /** WHAT `setup` WRITES INTO GIT'S GLOBAL CONFIG for `host`, as arguments of `git config --global`, * in order. Running them twice leaves the same config. `helper` is the shell command that runs * this CLI's `git-credential` for the host's mind. */ -export function gitConfigOf(input: { host: string; helper: string }): string[][] { - const credential = `credential.https://${input.host}.helper`; +export function gitConfigOf(input: { + host: string; + helper: string; + /** In a sandbox: git tells the helper which repo it asks for. */ + namesRepo?: boolean; +}): string[][] { + const credential = `credential.https://${input.host}`; return [ // an empty value clears the helpers that git inherited (the system's keychain, a manager's // window), for this host alone - ["--replace-all", credential, ""], - ["--add", credential, `!${input.helper}`], + ["--replace-all", `${credential}.helper`, ""], + ["--add", `${credential}.helper`, `!${input.helper}`], + ["--replace-all", `${credential}.useHttpPath`, input.namesRepo ? "true" : "false"], ["--replace-all", `http.https://${input.host}/.extraHeader`, GIT_MARKER_HEADER], ]; } /** Writes `gitConfigOf` with the `git` on the path, or throws with what git said. */ -export function writeGitConfig(input: { host: string; helper: string }) { +export function writeGitConfig(input: Parameters<typeof gitConfigOf>[0]) { for (const args of gitConfigOf(input)) { const run = spawnSync("git", ["config", "--global", ...args], { encoding: "utf8" }); if (run.error) throw new Error(`git could not be run: ${run.error.message}`); @@ -139,8 +152,18 @@ fetcher?: typeof fetch; /** A line for the person: stderr, which git passes to the screen. */ say(line: string): void; + /** IN A SANDBOX OF THE MIND ALONE: a write token of the mind for the one repo at `repo` + * (`/repos/<name>`), through the sandbox's own session. Absent on any other computer. */ + repoToken?(repo: string): Promise<string>; }; +/** The repo that git names to the helper as `path` (`repos/config.git`), as the mind names it + * (`/repos/config`), or null for what is no repo's path. */ +export function repoOfGitPath(path: string | undefined): string | null { + const repo = `/${(path || "").replace(/^\/+|\/+$/g, "").replace(/\.git$/, "")}`; + return /^\/repos\/[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)*$/.test(repo) ? repo : null; +} + /** Whether a person is there to sign in while git waits: a terminal on stderr, prompts not turned * off, and no coding agent. */ export const personIsThere = (env: Record<string, string | undefined>, stderrIsTty: boolean) => @@ -167,15 +190,27 @@ return ""; } if (input.operation !== "get") return ""; - const kept = readSession(resource); - if (kept?.expiresAt && Date.parse(kept.expiresAt) > Date.now() + HOST_TOKEN_LEAST_LEFT_MS) - return credentialOf(kept.token); const stop = (why: string) => { input.io.say( `iterate: ${why} Run this, then the same git command again:\n ${setupCommandOf(fields.host!)}`, ); return "quit=1\n"; }; + if (input.io.repoToken) { + const repo = repoOfGitPath(fields.path); + if (!repo) return stop(`git named no repo of ${fields.host} to its helper.`); + try { + return credentialOf(await input.io.repoToken(repo)); + } catch (error) { + input.io.say( + `iterate: this sandbox got no token for ${repo} from ${input.address}: ${error instanceof Error ? error.message : String(error)}`, + ); + return "quit=1\n"; + } + } + const kept = readSession(resource); + if (kept?.expiresAt && Date.parse(kept.expiresAt) > Date.now() + HOST_TOKEN_LEAST_LEFT_MS) + return credentialOf(kept.token); let session: StoredSession; try { // twice the least, so the host's token starts with more than the least left