Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 16 to 318

Compare

+124 −36 in 9 places
Lines 1–18 at 318 (were 1–14 at 16)
11// The folder's settings (.avcs/config.json), finding the folder that holds
22// them, and the token.
33//
4−// The token is never in the folder: it comes from the AVCS_TOKEN environment
5−// variable, or else from ~/.avcs/token, outside every repo.
4+// The token is never in the folder: it comes from the ABSO_TOKEN environment
5+// variable, or else from ~/.avcs/token, outside every repo. A pipeline run's
6+// key comes from the file ABSO_KEY_FILE names, and is only ever held in memory.
67
78import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
89import { homedir } from "node:os";
9−import { dirname, join, resolve } from "node:path";
10+import { basename, dirname, join, resolve } from "node:path";
1011import { UsageError } from "./args.ts";
1112import { type MachineServers, machineServers, TOKEN_SERVERS, tokenGoesTo, trustedList } from "./core.ts";
13+import { fromEnv } from "./env-names.ts";
14+import { isLocal, storeDir } from "./local-store.ts";
15+import { homeFile, stateDir } from "./state-dir.ts";
1216
1317/** What .avcs/config.json holds. */
1418export interface Settings {
Lines 23–48 at 318 (were 19–37 at 16)
1923 readonly follow?: string;
2024 /** A reader folder (abso join --reader, #37): it reads everything and records nothing; the server refuses its records too. */
2125 readonly reader?: boolean;
26+ /**
27+ * A visitor folder (abso join <project> --reader with no sign-in, for a public project): it reads through the
28+ * routes anyone can read and sends no credential. It has no agent; its config holds server, project and this.
29+ */
30+ readonly visitor?: boolean;
31+ /** A local project's store (abso init --local --store), when it isn't the machine's own: ABSO_STORE or the OS data folder. */
32+ readonly store?: string;
2233}
2334
2435export interface Config extends Settings {
2536 readonly token?: string;
37+ /** For a local project (server "local"): the store folder its history is in, or undefined when there's none to use. */
38+ readonly localStore?: string;
2639}
2740
28−export const AVCS_DIR = ".avcs";
29−
3041export function configPath(root: string): string {
31− return join(root, AVCS_DIR, "config.json");
42+ return join(stateDir(root), "config.json");
3243}
3344
34−/** The nearest folder at or above `start` that has .avcs/config.json, or undefined. */
45+/** The nearest folder at or above `start` that has .abso/config.json (or .avcs/config.json, from before the rename), or undefined. */
3546export function findRoot(start: string): string | undefined {
3647 let dir = resolve(start);
3748 for (;;) {
Lines 53–98 at 318 (were 42–63 at 16)
4253 }
4354}
4455
45−export function readConfig(root: string, env: Readonly<Record<string, string | undefined>> = process.env): Config {
56+export function readConfig(
57+ root: string,
58+ env: Readonly<Record<string, string | undefined>> = process.env,
59+ tokenFileOk = false,
60+): Config {
4661 const raw = JSON.parse(readFileSync(configPath(root), "utf8")) as Partial<Settings>;
62+ if (raw.visitor === true) {
63+ for (const key of ["server", "project"] as const) {
64+ if (typeof raw[key] !== "string" || raw[key] === "") throw new Error(`${configPath(root)} has no "${key}"`);
65+ }
66+ // No token and no key, whatever the environment or the folder holds: a visitor sends no credential.
67+ return { server: raw.server!, project: raw.project!, agent: "", visitor: true };
68+ }
4769 for (const key of ["server", "project", "agent"] as const) {
4870 if (typeof raw[key] !== "string" || raw[key] === "") throw new Error(`${configPath(root)} has no "${key}"`);
4971 }
50− const token = tokenFor(raw.server!, env, root);
72+ // A local project's calls never leave this process, so no credential goes with them.
73+ const local = isLocal(raw.server);
74+ const token = local ? undefined : tokenFor(raw.server!, env, root, tokenFileOk);
5175 return {
5276 server: raw.server!,
5377 project: raw.project!,
5478 agent: raw.agent!,
79+ ...(typeof raw.store === "string" && raw.store !== "" ? { store: raw.store } : {}),
80+ ...(local ? { localStore: storeDir(env, raw.store, root) } : {}),
5581 ...(typeof raw.follow === "string" && raw.follow !== "" ? { follow: raw.follow } : {}),
5682 ...(raw.reader === true ? { reader: true } : {}),
5783 ...(token ? { token } : {}),
5884 };
5985}
6086
87+/** Whether the folder is a visitor's: it reads signed out and sends no credential. Read straight from its config. */
88+export function isVisitor(root: string): boolean {
89+ try {
90+ return (JSON.parse(readFileSync(configPath(root), "utf8")) as { visitor?: unknown }).visitor === true;
91+ } catch {
92+ return false;
93+ }
94+}
6196/** Whether the folder is a reader (#37): records nothing. Read straight from its config, for the paths that only need this. */
6297export function isReader(root: string): boolean {
6398 try {
Lines 111–126 at 318 (were 76–88 at 16)
76111 root: string,
77112 server: string,
78113 env: Readonly<Record<string, string | undefined>> = process.env,
79−): { kind: "agent key" | "shared token" | "none"; id: string | null } {
114+ tokenFileOk = false,
115+): { kind: "agent key" | "shared token" | "pipeline run key" | "none"; id: string | null } {
80116 const key = readKeyFile(root);
81117 if (key) {
82118 if (originOf(key.server) !== originOf(server)) return { kind: "none", id: null };
83119 return { kind: "agent key", id: key.id };
84120 }
85− return { kind: readToken(env) ? "shared token" : "none", id: null };
121+ if (fromEnv(env, "TOKEN")) return { kind: "shared token", id: null };
122+ if (fromEnv(env, "KEY_FILE")) return { kind: "pipeline run key", id: null };
123+ return { kind: tokenFileOk && tokenFile() ? "shared token" : "none", id: null };
86124}
87125
88126interface StoredKey {
Lines 130–136 at 318 (were 92–98 at 16)
92130}
93131
94132function keyPath(root: string): string {
95− return join(root, AVCS_DIR, "key");
133+ return join(stateDir(root), "key");
96134}
97135
98136function originOf(server: string): string {
Lines 164–170 at 318 (were 126–132 at 16)
126164 if (configured && serverFlag && originOf(configured) !== originOf(serverFlag))
127165 throw new UsageError(`this folder's server is ${configured}, not ${serverFlag}`);
128166 const server = configured ?? serverFlag;
129− if (!server) throw new UsageError("this folder has no .avcs/config.json yet: give --server <url>");
167+ if (!server) throw new UsageError("this folder has no .abso/config.json yet: give --server <url>");
130168 let parsed: { key?: unknown; id?: unknown };
131169 try {
132170 parsed = JSON.parse(stdin) as { key?: unknown; id?: unknown };
Lines 178–184 at 318 (were 140–146 at 16)
140178 !/^k_[0-9a-f]{8}$/.test(parsed.id)
141179 )
142180 throw new UsageError("stdin needs {key, id} from the staging issue route");
143− mkdirSync(join(root, AVCS_DIR), { recursive: true });
181+ mkdirSync(stateDir(root), { recursive: true });
144182 writeFileSync(keyPath(root), `${JSON.stringify({ server, key: parsed.key, id: parsed.id })}\n`, { mode: 0o600 });
145183 try {
146184 chmodSync(keyPath(root), 0o600);
Lines 192–237 at 318 (were 154–182 at 16)
154192 server: string,
155193 env: Readonly<Record<string, string | undefined>> = process.env,
156194 root?: string,
195+ tokenFileOk = false,
157196): string | undefined {
158197 if (root) {
159198 const key = readKeyFile(root);
199+ // A folder with a key signs in with that key and nothing else, even when the server refuses it.
200+ if (!key && existsSync(keyPath(root)))
201+ throw new UsageError(
202+ `this folder's key file (${keyPath(root)}) can't be read, so abso sends nothing; abso key install puts a key back`,
203+ );
160204 if (key) {
161205 if (originOf(key.server) !== originOf(server))
162206 throw new UsageError("this folder's key was issued for a different server, so abso will not send it");
163207 return key.key;
164208 }
165209 }
166− const token = readToken(env);
167− const trusted = trustedServers(env);
168− if (token === undefined || tokenGoesTo(server, trusted)) return token;
210+ return trustedToken(
211+ server,
212+ env,
213+ fromEnv(env, "TOKEN")?.trim() || runKey(env) || (tokenFileOk ? readToken(env) : undefined),
214+ );
215+}
217+/** The token as given, if abso may send it to this server; refused, in words, if not. */
218+export function trustedToken(
219+ server: string,
220+ env: Readonly<Record<string, string | undefined>>,
221+ token: string | undefined,
222+): string | undefined {
223+ if (token === undefined || tokenGoesTo(server, trustedServers(env))) return token;
169224 throw new UsageError(
170− `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. ` +
171− `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.`,
225+ `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. ` +
226+ `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.`,
172227 );
173228}
174229
175230let machine: MachineServers | undefined;
176−/** This machine's servers (core.ts machineServers), from ~/.avcs/servers.json, read once. AVCS_SERVERS_FILE names another file (tests). */
231+/** 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). */
177232export function readMachineServers(env: Readonly<Record<string, string | undefined>> = process.env): MachineServers {
178− if (env.AVCS_SERVERS_FILE !== undefined) return machineServers(readOr(env.AVCS_SERVERS_FILE));
179− machine ??= machineServers(readOr(join(homedir(), AVCS_DIR, "servers.json")));
233+ if (fromEnv(env, "SERVERS_FILE") !== undefined) return machineServers(readOr(fromEnv(env, "SERVERS_FILE") as string));
234+ machine ??= machineServers(readOr(homeFile(homedir(), "servers.json")));
180235 return machine;
181236}
182237const readOr = (path: string): string | undefined => {
Lines 242–312 at 318 (were 187–224 at 16)
187242 }
188243};
189244
190−/** The servers the token may go to beyond abso's own: AVCS_TRUSTED_SERVERS, then this machine's servers.json. */
245+/** The servers the token may go to beyond abso's own: ABSO_TRUSTED_SERVERS, then this machine's servers.json. */
191246export function trustedServers(env: Readonly<Record<string, string | undefined>> = process.env): string {
192− return trustedList(env.AVCS_TRUSTED_SERVERS, readMachineServers(env));
247+ return trustedList(fromEnv(env, "TRUSTED_SERVERS"), readMachineServers(env));
193248}
194249
195−/** AVCS_TOKEN if set, else ~/.avcs/token, else none. */
250+/**
251+ * A pipeline run's key, from the file ABSO_KEY_FILE names (the runner writes it, mode 600, holding the bare key).
252+ * Read each time and kept in memory only; nothing abso writes or prints holds it, errors included.
253+ */
254+export function runKey(env: Readonly<Record<string, string | undefined>> = process.env): string | undefined {
255+ const path = fromEnv(env, "KEY_FILE");
256+ if (!path) return undefined;
257+ let text: string;
258+ try {
259+ text = readFileSync(path, "utf8");
260+ } catch {
261+ throw new UsageError(`ABSO_KEY_FILE names ${path}, which abso can't read`);
262+ }
263+ const key = text.trim();
264+ if (!/^abso_sk_[A-Za-z0-9_-]+$/.test(key))
265+ throw new UsageError(`ABSO_KEY_FILE names ${path}, which doesn't hold an abso key`);
266+ return key;
267+}
269+/** This machine's token file (~/.abso/token, else ~/.avcs/token), or none. Read only for a command given --token-file. */
270+export function tokenFile(): string | undefined {
271+ return readToken({});
272+}
274+/** ABSO_TOKEN if set, else ~/.abso/token, else ~/.avcs/token, else none. */
196275export function readToken(env: Readonly<Record<string, string | undefined>> = process.env): string | undefined {
197− if (env.AVCS_TOKEN) return env.AVCS_TOKEN.trim();
276+ const fromVar = fromEnv(env, "TOKEN");
277+ if (fromVar) return fromVar.trim();
198278 try {
199− return readFileSync(join(homedir(), AVCS_DIR, "token"), "utf8").trim() || undefined;
279+ return readFileSync(homeFile(homedir(), "token"), "utf8").trim() || undefined;
200280 } catch {
201281 return undefined;
202282 }
203283}
204284
205285export function writeConfig(root: string, settings: Settings): void {
206− mkdirSync(join(root, AVCS_DIR), { recursive: true });
207− const { server, project, agent, follow, reader } = settings;
208− writeFileSync(
209− configPath(root),
210− `${JSON.stringify({ server, project, agent, ...(follow ? { follow } : {}), ...(reader ? { reader: true } : {}) }, null, 2)}\n`,
211− );
286+ mkdirSync(stateDir(root), { recursive: true });
287+ const { server, project, agent, follow, reader, visitor, store } = settings;
288+ const written = visitor
289+ ? { server, project, visitor: true }
290+ : {
291+ server,
292+ project,
293+ agent,
294+ ...(follow ? { follow } : {}),
295+ ...(reader ? { reader: true } : {}),
296+ ...(store ? { store } : {}),
297+ };
298+ writeFileSync(configPath(root), `${JSON.stringify(written, null, 2)}\n`);
212299 excludeFromGit(root);
213300}
214301
215−/** If the folder is a git repo, keep .avcs/ out of it (in .git/info/exclude, so .gitignore isn't changed). */
302+/** 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). */
216303function excludeFromGit(root: string): void {
217304 const git = join(root, ".git");
218305 if (!existsSync(join(git, "HEAD"))) return;
219306 const exclude = join(git, "info", "exclude");
220307 const text = existsSync(exclude) ? readFileSync(exclude, "utf8") : "";
221− if (text.split(/\r?\n/).some((line) => line.trim() === ".avcs/" || line.trim() === "/.avcs/")) return;
308+ const name = `${basename(stateDir(root))}/`;
309+ if (text.split(/\r?\n/).some((line) => line.trim() === name || line.trim() === `/${name}`)) return;
222310 mkdirSync(dirname(exclude), { recursive: true });
223− writeFileSync(exclude, `${text}${text === "" || text.endsWith("\n") ? "" : "\n"}/.avcs/\n`);
311+ writeFileSync(exclude, `${text}${text === "" || text.endsWith("\n") ? "" : "\n"}/${name}\n`);
224312}
MCP
compare on this site's /mcp
CLI
abso diff 16 318 client/src/config.ts --json
JSON
/christopher/abso-core/compare/16..318?file=client%2Fsrc%2Fconfig.ts&format=json