{"draft":null,"from":2,"to":318,"files":[{"path":"client/src/config.ts","added":124,"removed":36,"diff":"--- a/client/src/config.ts\n+++ b/client/src/config.ts\n@@ -1,14 +1,18 @@\n // The folder's settings (.avcs/config.json), finding the folder that holds\n // them, and the token.\n //\n-// The token is never in the folder: it comes from the AVCS_TOKEN environment\n-// variable, or else from ~/.avcs/token, outside every repo.\n+// The token is never in the folder: it comes from the ABSO_TOKEN environment\n+// variable, or else from ~/.avcs/token, outside every repo. A pipeline run's\n+// key comes from the file ABSO_KEY_FILE names, and is only ever held in memory.\n \n import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\n import { homedir } from \"node:os\";\n-import { dirname, join, resolve } from \"node:path\";\n+import { basename, dirname, join, resolve } from \"node:path\";\n import { UsageError } from \"./args.ts\";\n import { type MachineServers, machineServers, TOKEN_SERVERS, tokenGoesTo, trustedList } from \"./core.ts\";\n+import { fromEnv } from \"./env-names.ts\";\n+import { isLocal, storeDir } from \"./local-store.ts\";\n+import { homeFile, stateDir } from \"./state-dir.ts\";\n \n /** What .avcs/config.json holds. */\n export interface Settings {\n@@ -19,19 +23,26 @@\n \treadonly follow?: string;\n \t/** A reader folder (abso join --reader, #37): it reads everything and records nothing; the server refuses its records too. */\n \treadonly reader?: boolean;\n+\t/**\n+\t * A visitor folder (abso join <project> --reader with no sign-in, for a public project): it reads through the\n+\t * routes anyone can read and sends no credential. It has no agent; its config holds server, project and this.\n+\t */\n+\treadonly visitor?: boolean;\n+\t/** A local project's store (abso init --local --store), when it isn't the machine's own: ABSO_STORE or the OS data folder. */\n+\treadonly store?: string;\n }\n \n export interface Config extends Settings {\n \treadonly token?: string;\n+\t/** For a local project (server \"local\"): the store folder its history is in, or undefined when there's none to use. */\n+\treadonly localStore?: string;\n }\n \n-export const AVCS_DIR = \".avcs\";\n-\n export function configPath(root: string): string {\n-\treturn join(root, AVCS_DIR, \"config.json\");\n+\treturn join(stateDir(root), \"config.json\");\n }\n \n-/** The nearest folder at or above `start` that has .avcs/config.json, or undefined. */\n+/** The nearest folder at or above `start` that has .abso/config.json (or .avcs/config.json, from before the rename), or undefined. */\n export function findRoot(start: string): string | undefined {\n \tlet dir = resolve(start);\n \tfor (;;) {\n@@ -42,22 +53,46 @@\n \t}\n }\n \n-export function readConfig(root: string, env: Readonly<Record<string, string | undefined>> = process.env): Config {\n+export function readConfig(\n+\troot: string,\n+\tenv: Readonly<Record<string, string | undefined>> = process.env,\n+\ttokenFileOk = false,\n+): Config {\n \tconst raw = JSON.parse(readFileSync(configPath(root), \"utf8\")) as Partial<Settings>;\n+\tif (raw.visitor === true) {\n+\t\tfor (const key of [\"server\", \"project\"] as const) {\n+\t\t\tif (typeof raw[key] !== \"string\" || raw[key] === \"\") throw new Error(`${configPath(root)} has no \"${key}\"`);\n+\t\t}\n+\t\t// No token and no key, whatever the environment or the folder holds: a visitor sends no credential.\n+\t\treturn { server: raw.server!, project: raw.project!, agent: \"\", visitor: true };\n+\t}\n \tfor (const key of [\"server\", \"project\", \"agent\"] as const) {\n \t\tif (typeof raw[key] !== \"string\" || raw[key] === \"\") throw new Error(`${configPath(root)} has no \"${key}\"`);\n \t}\n-\tconst token = tokenFor(raw.server!, env, root);\n+\t// A local project's calls never leave this process, so no credential goes with them.\n+\tconst local = isLocal(raw.server);\n+\tconst token = local ? undefined : tokenFor(raw.server!, env, root, tokenFileOk);\n \treturn {\n \t\tserver: raw.server!,\n \t\tproject: raw.project!,\n \t\tagent: raw.agent!,\n+\t\t...(typeof raw.store === \"string\" && raw.store !== \"\" ? { store: raw.store } : {}),\n+\t\t...(local ? { localStore: storeDir(env, raw.store, root) } : {}),\n \t\t...(typeof raw.follow === \"string\" && raw.follow !== \"\" ? { follow: raw.follow } : {}),\n \t\t...(raw.reader === true ? { reader: true } : {}),\n \t\t...(token ? { token } : {}),\n \t};\n }\n \n+/** Whether the folder is a visitor's: it reads signed out and sends no credential. Read straight from its config. */\n+export function isVisitor(root: string): boolean {\n+\ttry {\n+\t\treturn (JSON.parse(readFileSync(configPath(root), \"utf8\")) as { visitor?: unknown }).visitor === true;\n+\t} catch {\n+\t\treturn false;\n+\t}\n+}\n+\n /** Whether the folder is a reader (#37): records nothing. Read straight from its config, for the paths that only need this. */\n export function isReader(root: string): boolean {\n \ttry {\n@@ -76,13 +111,16 @@\n \troot: string,\n \tserver: string,\n \tenv: Readonly<Record<string, string | undefined>> = process.env,\n-): { kind: \"agent key\" | \"shared token\" | \"none\"; id: string | null } {\n+\ttokenFileOk = false,\n+): { kind: \"agent key\" | \"shared token\" | \"pipeline run key\" | \"none\"; id: string | null } {\n \tconst key = readKeyFile(root);\n \tif (key) {\n \t\tif (originOf(key.server) !== originOf(server)) return { kind: \"none\", id: null };\n \t\treturn { kind: \"agent key\", id: key.id };\n \t}\n-\treturn { kind: readToken(env) ? \"shared token\" : \"none\", id: null };\n+\tif (fromEnv(env, \"TOKEN\")) return { kind: \"shared token\", id: null };\n+\tif (fromEnv(env, \"KEY_FILE\")) return { kind: \"pipeline run key\", id: null };\n+\treturn { kind: tokenFileOk && tokenFile() ? \"shared token\" : \"none\", id: null };\n }\n \n interface StoredKey {\n@@ -92,7 +130,7 @@\n }\n \n function keyPath(root: string): string {\n-\treturn join(root, AVCS_DIR, \"key\");\n+\treturn join(stateDir(root), \"key\");\n }\n \n function originOf(server: string): string {\n@@ -126,7 +164,7 @@\n \tif (configured && serverFlag && originOf(configured) !== originOf(serverFlag))\n \t\tthrow new UsageError(`this folder's server is ${configured}, not ${serverFlag}`);\n \tconst server = configured ?? serverFlag;\n-\tif (!server) throw new UsageError(\"this folder has no .avcs/config.json yet: give --server <url>\");\n+\tif (!server) throw new UsageError(\"this folder has no .abso/config.json yet: give --server <url>\");\n \tlet parsed: { key?: unknown; id?: unknown };\n \ttry {\n \t\tparsed = JSON.parse(stdin) as { key?: unknown; id?: unknown };\n@@ -140,7 +178,7 @@\n \t\t!/^k_[0-9a-f]{8}$/.test(parsed.id)\n \t)\n \t\tthrow new UsageError(\"stdin needs {key, id} from the staging issue route\");\n-\tmkdirSync(join(root, AVCS_DIR), { recursive: true });\n+\tmkdirSync(stateDir(root), { recursive: true });\n \twriteFileSync(keyPath(root), `${JSON.stringify({ server, key: parsed.key, id: parsed.id })}\\n`, { mode: 0o600 });\n \ttry {\n \t\tchmodSync(keyPath(root), 0o600);\n@@ -154,29 +192,46 @@\n \tserver: string,\n \tenv: Readonly<Record<string, string | undefined>> = process.env,\n \troot?: string,\n+\ttokenFileOk = false,\n ): string | undefined {\n \tif (root) {\n \t\tconst key = readKeyFile(root);\n+\t\t// A folder with a key signs in with that key and nothing else, even when the server refuses it.\n+\t\tif (!key && existsSync(keyPath(root)))\n+\t\t\tthrow new UsageError(\n+\t\t\t\t`this folder's key file (${keyPath(root)}) can't be read, so abso sends nothing; abso key install puts a key back`,\n+\t\t\t);\n \t\tif (key) {\n \t\t\tif (originOf(key.server) !== originOf(server))\n \t\t\t\tthrow new UsageError(\"this folder's key was issued for a different server, so abso will not send it\");\n \t\t\treturn key.key;\n \t\t}\n \t}\n-\tconst token = readToken(env);\n-\tconst trusted = trustedServers(env);\n-\tif (token === undefined || tokenGoesTo(server, trusted)) return token;\n+\treturn trustedToken(\n+\t\tserver,\n+\t\tenv,\n+\t\tfromEnv(env, \"TOKEN\")?.trim() || runKey(env) || (tokenFileOk ? readToken(env) : undefined),\n+\t);\n+}\n+\n+/** The token as given, if abso may send it to this server; refused, in words, if not. */\n+export function trustedToken(\n+\tserver: string,\n+\tenv: Readonly<Record<string, string | undefined>>,\n+\ttoken: string | undefined,\n+): string | undefined {\n+\tif (token === undefined || tokenGoesTo(server, trustedServers(env))) return token;\n \tthrow new UsageError(\n-\t\t`this folder's server is ${server}, and abso sends your token only to ${[...TOKEN_SERVERS, \"this machine\"].join(\", \")}, and the servers ~/.avcs/servers.json or AVCS_TRUSTED_SERVERS name. ` +\n-\t\t\t`If ${server} is yours, name it in ~/.avcs/servers.json or AVCS_TRUSTED_SERVERS (comma-separated); if you didn't set it, someone else wrote .avcs/config.json.`,\n+\t\t`this folder's server is ${server}, and abso sends your token only to ${[...TOKEN_SERVERS, \"this machine\"].join(\", \")}, and the servers ~/.abso/servers.json or ABSO_TRUSTED_SERVERS name. ` +\n+\t\t\t`If ${server} is yours, name it in ~/.abso/servers.json or ABSO_TRUSTED_SERVERS (comma-separated); if you didn't set it, someone else wrote .abso/config.json.`,\n \t);\n }\n \n let machine: MachineServers | undefined;\n-/** This machine's servers (core.ts machineServers), from ~/.avcs/servers.json, read once. AVCS_SERVERS_FILE names another file (tests). */\n+/** This machine's servers (core.ts machineServers), from ~/.abso/servers.json (or the older ~/.avcs/servers.json), read once. ABSO_SERVERS_FILE names another file (tests). */\n export function readMachineServers(env: Readonly<Record<string, string | undefined>> = process.env): MachineServers {\n-\tif (env.AVCS_SERVERS_FILE !== undefined) return machineServers(readOr(env.AVCS_SERVERS_FILE));\n-\tmachine ??= machineServers(readOr(join(homedir(), AVCS_DIR, \"servers.json\")));\n+\tif (fromEnv(env, \"SERVERS_FILE\") !== undefined) return machineServers(readOr(fromEnv(env, \"SERVERS_FILE\") as string));\n+\tmachine ??= machineServers(readOr(homeFile(homedir(), \"servers.json\")));\n \treturn machine;\n }\n const readOr = (path: string): string | undefined => {\n@@ -187,38 +242,71 @@\n \t}\n };\n \n-/** The servers the token may go to beyond abso's own: AVCS_TRUSTED_SERVERS, then this machine's servers.json. */\n+/** The servers the token may go to beyond abso's own: ABSO_TRUSTED_SERVERS, then this machine's servers.json. */\n export function trustedServers(env: Readonly<Record<string, string | undefined>> = process.env): string {\n-\treturn trustedList(env.AVCS_TRUSTED_SERVERS, readMachineServers(env));\n+\treturn trustedList(fromEnv(env, \"TRUSTED_SERVERS\"), readMachineServers(env));\n }\n \n-/** AVCS_TOKEN if set, else ~/.avcs/token, else none. */\n+/**\n+ * A pipeline run's key, from the file ABSO_KEY_FILE names (the runner writes it, mode 600, holding the bare key).\n+ * Read each time and kept in memory only; nothing abso writes or prints holds it, errors included.\n+ */\n+export function runKey(env: Readonly<Record<string, string | undefined>> = process.env): string | undefined {\n+\tconst path = fromEnv(env, \"KEY_FILE\");\n+\tif (!path) return undefined;\n+\tlet text: string;\n+\ttry {\n+\t\ttext = readFileSync(path, \"utf8\");\n+\t} catch {\n+\t\tthrow new UsageError(`ABSO_KEY_FILE names ${path}, which abso can't read`);\n+\t}\n+\tconst key = text.trim();\n+\tif (!/^abso_sk_[A-Za-z0-9_-]+$/.test(key))\n+\t\tthrow new UsageError(`ABSO_KEY_FILE names ${path}, which doesn't hold an abso key`);\n+\treturn key;\n+}\n+\n+/** This machine's token file (~/.abso/token, else ~/.avcs/token), or none. Read only for a command given --token-file. */\n+export function tokenFile(): string | undefined {\n+\treturn readToken({});\n+}\n+\n+/** ABSO_TOKEN if set, else ~/.abso/token, else ~/.avcs/token, else none. */\n export function readToken(env: Readonly<Record<string, string | undefined>> = process.env): string | undefined {\n-\tif (env.AVCS_TOKEN) return env.AVCS_TOKEN.trim();\n+\tconst fromVar = fromEnv(env, \"TOKEN\");\n+\tif (fromVar) return fromVar.trim();\n \ttry {\n-\t\treturn readFileSync(join(homedir(), AVCS_DIR, \"token\"), \"utf8\").trim() || undefined;\n+\t\treturn readFileSync(homeFile(homedir(), \"token\"), \"utf8\").trim() || undefined;\n \t} catch {\n \t\treturn undefined;\n \t}\n }\n \n export function writeConfig(root: string, settings: Settings): void {\n-\tmkdirSync(join(root, AVCS_DIR), { recursive: true });\n-\tconst { server, project, agent, follow, reader } = settings;\n-\twriteFileSync(\n-\t\tconfigPath(root),\n-\t\t`${JSON.stringify({ server, project, agent, ...(follow ? { follow } : {}), ...(reader ? { reader: true } : {}) }, null, 2)}\\n`,\n-\t);\n+\tmkdirSync(stateDir(root), { recursive: true });\n+\tconst { server, project, agent, follow, reader, visitor, store } = settings;\n+\tconst written = visitor\n+\t\t? { server, project, visitor: true }\n+\t\t: {\n+\t\t\t\tserver,\n+\t\t\t\tproject,\n+\t\t\t\tagent,\n+\t\t\t\t...(follow ? { follow } : {}),\n+\t\t\t\t...(reader ? { reader: true } : {}),\n+\t\t\t\t...(store ? { store } : {}),\n+\t\t\t};\n+\twriteFileSync(configPath(root), `${JSON.stringify(written, null, 2)}\\n`);\n \texcludeFromGit(root);\n }\n \n-/** If the folder is a git repo, keep .avcs/ out of it (in .git/info/exclude, so .gitignore isn't changed). */\n+/** If the folder is a git repo, keep its state folder (.abso/, or .avcs/ in an older folder) out of it (in .git/info/exclude, so .gitignore isn't changed). */\n function excludeFromGit(root: string): void {\n \tconst git = join(root, \".git\");\n \tif (!existsSync(join(git, \"HEAD\"))) return;\n \tconst exclude = join(git, \"info\", \"exclude\");\n \tconst text = existsSync(exclude) ? readFileSync(exclude, \"utf8\") : \"\";\n-\tif (text.split(/\\r?\\n/).some((line) => line.trim() === \".avcs/\" || line.trim() === \"/.avcs/\")) return;\n+\tconst name = `${basename(stateDir(root))}/`;\n+\tif (text.split(/\\r?\\n/).some((line) => line.trim() === name || line.trim() === `/${name}`)) return;\n \tmkdirSync(dirname(exclude), { recursive: true });\n-\twriteFileSync(exclude, `${text}${text === \"\" || text.endsWith(\"\\n\") ? \"\" : \"\\n\"}/.avcs/\\n`);\n+\twriteFileSync(exclude, `${text}${text === \"\" || text.endsWith(\"\\n\") ? \"\" : \"\\n\"}/${name}\\n`);\n }"}],"unmatched":[]}