9 files, 218 lines added, 31 removed.
--- a/client/src/command-env.ts
+++ b/client/src/command-env.ts
@@ -19,6 +19,8 @@
1919 readonly env: Readonly<Record<string, string | undefined>>; 2020 /** Where `run` streams the box's output as it arrives. */ 2121 readonly write: (text: string) => void; 22+ /** --token-file: this machine's ~/.abso/token may sign in, where nothing else does. Never read otherwise. */ 23+ readonly tokenFile?: boolean; 2224 /** Where `run` streams the box's stderr; `write` when not given. */ 2325 readonly writeErr?: (text: string) => void; 2426 /** What a box run is for, when a command runs one for its own ends (shot, bisect, publish --check). */ @@ -42,7 +44,7 @@
4244 throw new UsageError( 4345 `You're not in an abso folder: ${env.cwd} has no .abso/config.json, nor does any folder above it. abso join <project> makes one, or abso init a new project.`, 45− const config = readConfig(root, env.env);
47+ const config = readConfig(root, env.env, env.tokenFile === true); 4648 return { root, config, api: new Api(config) }; --- a/client/src/config.ts
+++ b/client/src/config.ts
@@ -51,7 +51,7 @@
5151export function readConfig( 5353 env: Readonly<Record<string, string | undefined>> = process.env, 54− tokenFileOk = true,
5656 const raw = JSON.parse(readFileSync(configPath(root), "utf8")) as Partial<Settings>; 5757 if (raw.visitor === true) { @@ -102,6 +102,7 @@
104104 env: Readonly<Record<string, string | undefined>> = process.env, 105+ tokenFileOk = false, 105106): { kind: "agent key" | "shared token" | "pipeline run key" | "none"; id: string | null } { 106107 const key = readKeyFile(root); @@ -110,7 +111,7 @@
111112 if (fromEnv(env, "TOKEN")) return { kind: "shared token", id: null }; 112113 if (fromEnv(env, "KEY_FILE")) return { kind: "pipeline run key", id: null }; 113− return { kind: readToken(env) ? "shared token" : "none", id: null };
114+ return { kind: tokenFileOk && tokenFile() ? "shared token" : "none", id: null }; 116117interface StoredKey { @@ -182,10 +183,15 @@
183184 env: Readonly<Record<string, string | undefined>> = process.env, 185− tokenFileOk = true,
186+ tokenFileOk = false, 186187): string | undefined { 188189 const key = readKeyFile(root); 190+ // A folder with a key signs in with that key and nothing else, even when the server refuses it. 191+ if (!key && existsSync(keyPath(root))) 192+ throw new UsageError( 193+ `this folder's key file (${keyPath(root)}) can't be read, so abso sends nothing; abso key install puts a key back`, 190196 if (originOf(key.server) !== originOf(server)) 191197 throw new UsageError("this folder's key was issued for a different server, so abso will not send it"); @@ -251,7 +257,7 @@
254−/** This machine's token file (~/.abso/token, else ~/.avcs/token), or none. abso init and join read it only with --token-file. */
260+/** This machine's token file (~/.abso/token, else ~/.avcs/token), or none. Read only for a command given --token-file. */ 255261export function tokenFile(): string | undefined { 256262 return readToken({}); --- a/client/src/doctor.ts
+++ b/client/src/doctor.ts
@@ -5,7 +5,7 @@
55import { ApiError } from "./api-error.ts"; 66import { type Answer, type BoxRecord, boxGet, boxText, originOf, readBox, signedInServers } from "./box.ts"; 77import { builtCopy } from "./client-version.ts"; 8−import { type Config, credentialLabel, findRoot, readConfig } from "./config.ts";
8+import { type Config, credentialLabel, findRoot, readConfig, tokenFile } from "./config.ts"; 99import { sameServer } from "./server-choice.ts"; @@ -84,7 +84,9 @@
8484 ? "the shared token" 8585 : c.kind === "pipeline run key" 8686 ? "a pipeline run's key" 87− : "no key";
87+ : tokenFile() !== undefined 88+ ? "no key (~/.abso/token is here, and is used only with --token-file)" 9092export async function whoami( @@ -327,12 +329,12 @@
327329 * The commonest failures in plain words, with the next step: no credential, a credential the server refuses, and a 328330 * server that can't be reached. Anything else is left as it was. `signedIn` is whether the call carried a key or token. 330−export function plainError(err: unknown, signedIn: boolean): string | undefined {
332+export function plainError(err: unknown, signedIn: boolean, tokenFileUnasked = false): string | undefined { 331333 if (!(err instanceof ApiError)) return undefined; 332334 if (err.status === 401) 334336 ? `The server didn't accept this folder's key: it may be revoked or expired. abso doctor checks it. (${err.message})` 335− : "You're not signed in. Run abso login, then abso join <project>.";
337+ : `You're not signed in. Run abso login, then abso join <project>.${tokenFileUnasked ? " ~/.abso/token is used only with --token-file." : ""}`; 336338 if (err.status === undefined && err.message.startsWith("could not reach ")) { 337339 const said = err.message.replace(/^could not reach /, "Can't reach "); 338340 // A message that already gives its next step (publish's "Run the same command again.") keeps it as the last word. --- a/client/src/flag-commands.ts
+++ b/client/src/flag-commands.ts
@@ -6,7 +6,7 @@
66import { type Env, type Output, open } from "./commands.ts"; 77import type { Config } from "./config.ts"; 88import { fromEnv } from "./env-names.ts"; 9−import { adminGoesTo, adminToken } from "./key-admin.ts";
9+import { adminGoesTo, adminToken, adminTokenWords } from "./key-admin.ts"; 1010import { shortWhen } from "./times.ts"; 1212/** One flag as GET /admin/flags answers it: what it takes and gates, and where it's set. */ @@ -117,6 +117,7 @@
117117 const [sub = "list", flag, value, ...extra] = args.positionals; 118118 const { config } = open(env); 119119 const admin = adminFor(config.server, env.env); 120+ if (admin) (env.writeErr ?? env.write)(`${adminTokenWords(env.env)}\n`); 120121 if (sub === "list" && flag === undefined) { 121122 const body = (await call(config, admin, "/admin/flags")) as { --- a/client/src/history.ts
+++ b/client/src/history.ts
@@ -15,7 +15,7 @@
1515 const root = findRoot(env.cwd); 1616 if (root === undefined) 1717 throw new UsageError(`${env.cwd} is not in an abso folder (no .abso/config.json here or above)`); 18− const config = readConfig(root, env.env);
18+ const config = readConfig(root, env.env, env.tokenFile === true); 1919 return { root, api: new Api(config), agent: config.agent }; --- a/client/src/key-admin.ts
+++ b/client/src/key-admin.ts
@@ -38,6 +38,11 @@
3838 throw new UsageError("abso key needs the admin token in ABSO_ADMIN_TOKEN or ~/.abso/admin-token"); 41+/** What an admin command says each time it uses the admin token, and where it came from. Never the token. */ 42+export function adminTokenWords(env: Readonly<Record<string, string | undefined>> = process.env): string { 43+ return `Using the admin token, from ${fromEnv(env, "ADMIN_TOKEN")?.trim() ? "ABSO_ADMIN_TOKEN" : "~/.abso/admin-token"}.`; 4146function scrub(text: string, secrets: readonly string[]): string { 4247 let out = text.replace(/abso_sk_[A-Za-z0-9_-]+/g, "[redacted]"); 4348 for (const secret of secrets) if (secret.length > 0) out = out.split(secret).join("[redacted]"); --- a/client/src/main.ts
+++ b/client/src/main.ts
@@ -38,10 +38,11 @@
4040} from "./commands.ts"; 41−import { configPath, findRoot, installKey, isVisitor, readConfig, readToken } from "./config.ts";
41+import { configPath, findRoot, installKey, isVisitor, readConfig, tokenFile } from "./config.ts"; 4242import { newsBlockLines, newsShown } from "./core.ts"; 4343import { doctor, doctorText, plainError, realProbe, whoami } from "./doctor.ts"; 4444import { runDraftSync, runLand, runSwitch } from "./drafts.ts"; 45+import { fromEnv } from "./env-names.ts"; 4546import { runExport } from "./export.ts"; 4647import { runFlag } from "./flag-commands.ts"; 4748import { runGroup, runGroups } from "./group-commands.ts"; @@ -57,7 +58,7 @@
5960} from "./issues.ts"; 60−import { adminToken, issuedPublic, keyList, keyListText, keyNew, keyRevoke } from "./key-admin.ts";
61+import { adminToken, adminTokenWords, issuedPublic, keyList, keyListText, keyNew, keyRevoke } from "./key-admin.ts"; 6162import { browserSkip, openInBrowser } from "./open-browser.ts"; 6263import { runOrg, runOrgs } from "./orgs.ts"; 6364import { runUnqueue } from "./queue.ts"; @@ -148,17 +149,18 @@
148149 if (!root) throw new UsageError("abso key runs inside an agent's folder, or takes --server <url>"); 149150 let config: ReturnType<typeof readConfig>; 151− config = readConfig(root, env.env);
152+ config = readConfig(root, env.env, env.tokenFile === true); 154155 server: serverFlag ?? "", 155156 project: str(args, "project") ?? "", 157− token: readToken(env.env),
158+ token: fromEnv(env.env, "TOKEN")?.trim() || (env.tokenFile ? tokenFile() : undefined), 160161 if (serverFlag) config = { ...config, server: serverFlag }; 161162 const admin = adminToken(env.env); 163+ (env.writeErr ?? env.write)(`${adminTokenWords(env.env)}\n`); 162164 if (sub === "new" && args.positionals.length === 2) { 163165 const server = str(args, "server") ?? config.server; 164166 const project = str(args, "project") ?? config.project; @@ -876,14 +878,14 @@
877879const HELP_INTRO = "abso: version control for agents. This is its command-line client."; 879− "Every command takes --json. Settings live in .abso/config.json (.avcs/config.json in a folder from before the rename). The token comes from ABSO_TOKEN, or else ~/.abso/token (or ~/.avcs/token from before). " +
881+ "Every command takes --json. Settings live in .abso/config.json (.avcs/config.json in a folder from before the rename). A folder with a key signs in with that key and nothing else. Without one, the token comes from ABSO_TOKEN; this machine's ~/.abso/token (or ~/.avcs/token from before) is used only by a command given --token-file. " + 880882 "Every command that talks to a server takes --server <url>; one that works only on this machine (skill, check-ignore, pause, resume, fix-eol) doesn't. In a joined folder --server must be the folder's own server: another is refused, never swapped in. An explicit --server is used as given, and no setting redirects it. The server is " + 882884 ": which servers this box is signed in to plays no part."; 884−/** A command's flags: its own, and --server for every command that talks to a server (server-choice.ts). */
886+/** A command's flags: its own, and --server and --token-file for every command that talks to a server (server-choice.ts). */ 885887function flagsOf(name: string, def: CommandDef): FlagSpec { 886− return serverUse(name) === "local" ? def.flags : { ...def.flags, server: "string" };
888+ return serverUse(name) === "local" ? def.flags : { ...def.flags, server: "string", "token-file": "boolean" }; 889891/** Every command as help shows it, in order: the table's, then the three it doesn't hold. */ @@ -960,10 +962,12 @@
962964/** Whether this folder's calls carry a key or token, for how a refusal is worded. False when that can't be told. */ 963−function carriesCredential(env: Env): boolean {
965+function carriesCredential(env: Env, tokenFileAsked: boolean): boolean { 965967 const root = findRoot(env.cwd); 966− return root ? readConfig(root, env.env).token !== undefined : readToken(env.env) !== undefined;
969+ ? readConfig(root, env.env, tokenFileAsked).token !== undefined 970+ : (fromEnv(env.env, "TOKEN") ?? (tokenFileAsked ? tokenFile() : undefined)) !== undefined; @@ -984,6 +988,7 @@
984988export async function main(argv: readonly string[], env: Env): Promise<MainResult> { 985989 const command = argv[0]; 986990 const json = argv.includes("--json") && command !== "run" && command !== "logs"; 991+ const asked = argv.includes("--token-file"); 987992 if (command === undefined || command === "help" || command === "--help" || command === "-h") { 989994 const data = helpJson(); @@ -1047,7 +1052,12 @@
10471052 if (root !== undefined && isVisitor(root)) checkVisitor(command, parsed, readConfig(root, env.env).project); 10481053 const args = readBodyFiles(command, parsed, env.cwd, env.readStdin); 10491054 takeNews(); // what an earlier call in this process left (tests run many) 1050− const out = await def.run(args, named === undefined ? env : { ...env, server: named });
1055+ const runEnv: Env = { 1057+ ...(named === undefined ? {} : { server: named }), 1058+ ...(bool(parsed, "token-file") ? { tokenFile: true } : {}), 1060+ const out = await def.run(args, runEnv); 10511061 const exit = out.exit ?? EXIT.ok; 10521062 // Every command that took news while recording prints it (#37, News Report). 10531063 const news = takeNews(); @@ -1069,7 +1079,7 @@
10691079 const e = err as Error; 10701080 const code = err instanceof UsageError ? "usage" : err instanceof ApiError ? "server" : "error"; 1072− plainError(err, carriesCredential(env)) ??
1082+ plainError(err, carriesCredential(env, asked), !asked && tokenFile() !== undefined) ?? 10731083 (err instanceof UsageError || err instanceof ApiError 10751085 : `${e.message}${e.stack ? `\n${e.stack}` : ""}`); --- a/client/src/mcp.ts
+++ b/client/src/mcp.ts
@@ -78,15 +78,19 @@
7878 return MCP_COMMANDS.map((command) => { 7979 const def = table[command]!; 8080 const flags = Object.fromEntries( 81− Object.entries(def.flags).map(([flag, kind]) => [
82− flag,
83− kind === "boolean"
84− ? { type: "boolean", description: `--${flag}` }
85− : {
86− type: "string",
87− description: kind === "optional" ? `--${flag} "<value>"; "" gives --${flag} alone` : `--${flag} <value>`,
88− },
89− ]),
81+ // No --token-file: a tool signs in with its folder's key, never this machine's token file. 82+ Object.entries(def.flags) 83+ .filter(([flag]) => flag !== "token-file") 84+ .map(([flag, kind]) => [ 87+ ? { type: "boolean", description: `--${flag}` } 91+ kind === "optional" ? `--${flag} "<value>"; "" gives --${flag} alone` : `--${flag} <value>`, 9296 name: toolName(command), --- /dev/null
+++ b/client/test/token-file.test.ts
@@ -0,0 +1,157 @@
1+// A credential is never picked up from a file nobody asked for. A folder with a key signs in with that key only, and a 2+// key file that can't be read is an error, not a reason to try another credential. With no key, this machine's 3+// ~/.abso/token signs in only for a command given --token-file. The admin token is read by admin commands alone, and 4+// each says when it uses it. 6+import assert from "node:assert/strict"; 7+import { execFile } from "node:child_process"; 8+import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; 9+import { tmpdir } from "node:os"; 10+import { join, resolve } from "node:path"; 11+import { after, before, describe, it } from "node:test"; 12+import { tokenFor } from "../src/config.ts"; 13+import { adminTokenWords } from "../src/key-admin.ts"; 14+import { type Fake, startFake } from "./fake-server.ts"; 16+const CLIENT = resolve(import.meta.dirname, "..", "abso.ts"); 17+const TOKEN = "file-token-not-secret"; 18+const KEY = "abso_sk_folder-key-not-secret"; 20+const dirs: string[] = []; 22+ const dir = mkdtempSync(join(tmpdir(), "abso-test-token-file-")); 28+ fake = await startFake(0, TOKEN); 32+ for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); 35+function homeWithToken(): string { 37+ mkdirSync(join(dir, ".abso"), { recursive: true }); 38+ writeFileSync(join(dir, ".abso", "token"), `${TOKEN}\n`); 42+/** A joined folder on `server`, with the given key file text, or none. */ 43+function folder(server: string, key?: string): string { 45+ mkdirSync(join(dir, ".abso")); 46+ writeFileSync(join(dir, ".abso", "config.json"), JSON.stringify({ server, project: "p", agent: "me" })); 47+ if (key !== undefined) writeFileSync(join(dir, ".abso", "key"), key); 51+/** Runs `fn` with this home as the user's, so the token file is the test's own. */ 52+function asHome<T>(home: string, fn: () => T): T { 53+ const saved = { HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE }; 54+ process.env.HOME = home; 55+ process.env.USERPROFILE = home; 59+ for (const [k, v] of Object.entries(saved)) { 60+ if (v === undefined) delete process.env[k]; 61+ else process.env[k] = v; 70+): Promise<{ code: number; stdout: string; stderr: string }> { 71+ const env: Record<string, string> = { 72+ PATH: process.env.PATH ?? "", 75+ AVCS_SERVERS_FILE: "no-servers.json", 76+ ABSO_NO_BROWSER: "1", 78+ if (process.env.CLAUDE_CONFIG_DIR) env.CLAUDE_CONFIG_DIR = process.env.CLAUDE_CONFIG_DIR; 79+ if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot; 80+ return new Promise((done) => { 81+ execFile(process.execPath, [CLIENT, ...args], { cwd, env }, (error, stdout, stderr) => 82+ done({ code: typeof error?.code === "number" ? error.code : 0, stdout, stderr }), 87+describe("credentials only where asked", () => { 88+ it("a folder's key is the only credential it sends; an unreadable key file is an error", () => { 89+ const server = "http://127.0.0.1:1"; 90+ asHome(homeWithToken(), () => { 91+ const keyed = folder(server, JSON.stringify({ server, key: KEY, id: "k_0123abcd" })); 92+ assert.equal(tokenFor(server, {}, keyed), KEY); 93+ assert.equal(tokenFor(server, {}, keyed, true), KEY, "even with --token-file"); 94+ assert.equal(tokenFor(server, { ABSO_TOKEN: "env-token" }, keyed), KEY, "even with ABSO_TOKEN"); 95+ assert.throws(() => tokenFor(server, {}, folder(server, "not a key")), /key file .* can't be read/); 96+ const keyless = folder(server); 97+ assert.equal(tokenFor(server, {}, keyless), undefined, "the token file is never read by itself"); 98+ assert.equal(tokenFor(server, {}, keyless, true), TOKEN, "with --token-file it is"); 99+ assert.equal(tokenFor(server, { ABSO_TOKEN: "env-token" }, keyless), "env-token"); 103+ it("a keyless folder: refused as signed out, naming --token-file; with --token-file it signs in", async () => { 104+ const home = homeWithToken(); 105+ const made = await client( 109+ writeFileSync(join(d, "a.txt"), "hi\n"); 121+ assert.equal(made.code, 0, made.stderr); 122+ const keyless = folder(fake.url); 123+ const unasked = await client(home, keyless, "status"); 124+ assert.notEqual(unasked.code, 0); 127+ /You're not signed in\. Run abso login, then abso join <project>\. ~\/\.abso\/token is used only with --token-file\./, 129+ const asked = await client(home, keyless, "status", "--token-file"); 130+ assert.equal(asked.code, 0, asked.stderr); 131+ assert.match(asked.stdout, /Project p/); 134+ it("the admin token is read by admin commands alone, and they say when they use it", () => { 135+ assert.equal(adminTokenWords({ ABSO_ADMIN_TOKEN: "x" }), "Using the admin token, from ABSO_ADMIN_TOKEN."); 136+ assert.equal(adminTokenWords({}), "Using the admin token, from ~/.abso/admin-token."); 137+ const src = resolve(import.meta.dirname, "..", "src"); 138+ const readers: string[] = []; 139+ const users: string[] = []; 140+ for (const name of readdirSync(src).filter((n) => n.endsWith(".ts"))) { 141+ const text = readFileSync(join(src, name), "utf8"); 142+ if (/"admin-token"/.test(text)) readers.push(name); 143+ if (/\badminToken\(/.test(text.replace(/export function adminToken\(/, ""))) users.push(name); 145+ assert.deepEqual(readers, ["key-admin.ts"], "only key-admin.ts reads ~/.abso/admin-token"); 146+ assert.deepEqual(users.sort(), ["flag-commands.ts", "main.ts"], "abso flag and abso key, and nothing else"); 147+ const main = readFileSync(join(src, "main.ts"), "utf8"); 150+ /const admin = adminToken\(env\.env\);\s+\(env\.writeErr \?\? env\.write\)\(`\$\{adminTokenWords/, 153+ readFileSync(join(src, "flag-commands.ts"), "utf8"), 154+ /if \(admin\)\s+\(env\.writeErr \?\? env\.write\)\(`\$\{adminTokenWords/,