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.

Not checked
MCP
show on this site's /mcp
CLI
abso show 63 --json
JSON
/christopher/abso-core/versions/63?format=json

abso join <project> works with no --agent and no --dir (abso issue 351), so the project page can show npx abso-cli join <project>.

of shared·trials··Not checked

  • Agent: the box's name (the hostname abso login sends), lower-cased; if taken, <name>-2, -3, up to 20 tries. Names this box already holds in the project (GET /box) are skipped without minting, since minting one again replaces that agent's key and would cut off its folder. A 409 for a taken name moves on; the agent-limit 409 says the limit and where to revoke. No box sign-in: abso login first, or --agent.
  • Folder: --dir, else here when empty, else ./<project>, which must be new or empty; it prints cd <folder> when it isn't here.
  • --agent, --dir and --server still win. Project names are checked before use as a folder.

Tests: box.test.ts (mintFreeName: held name skipped, 409 moves on, agent limit), client.test.ts (empty folder, ./<project>, refusing a non-empty one); the git line-endings test now joins with --dir . since its folder has files.

Version 62 · Files at version 63

5 files, 148 lines added, 14 removed.

--- a/client/src/box.ts
+++ b/client/src/box.ts
@@ -277,6 +277,50 @@
277277 return { key, id };
278278}
279279
280+/** The agent name abso join takes when none is given (abso issue 351): the box's name, lower-cased. */
281+export function defaultAgentName(): string {
282+ return boxName().toLowerCase();
283+}
285+/**
286+ * abso join with no --agent (abso issue 351): mints a key under `base`, else `base`-2, -3 and so on. Names this
287+ * box already holds in the project are skipped without asking, since minting one again would replace that agent's
288+ * key and cut off its folder. A name the server says is taken (409) moves on to the next; any other refusal, the
289+ * box's agent limit among them, is said as it is.
290+ */
291+export async function mintFreeName(
292+ record: BoxRecord,
293+ project: string,
294+ base: string,
295+ fetcher: typeof fetch = fetch,
296+ tries = 20,
297+): Promise<{ key: string; id: string; agent: string }> {
298+ const box = await boxGet(record, "/box", fetcher);
299+ if (box.status !== 200)
300+ throw new UsageError(
301+ `${originOf(record.server)} refused (${box.status}): ${String(box.body.error ?? "no reason given")}`,
302+ );
303+ const agents = Array.isArray(box.body.agents) ? (box.body.agents as { project?: unknown; agent?: unknown }[]) : [];
304+ const held = new Set(agents.filter((a) => a.project === project).map((a) => String(a.agent)));
305+ for (let n = 1; n <= tries; n++) {
306+ const agent = n === 1 ? base : `${base}-${n}`;
307+ if (held.has(agent)) continue;
308+ try {
309+ return { ...(await mintKey(record, project, agent, fetcher)), agent };
310+ } catch (err) {
311+ if (err instanceof UsageError && /refused \(409\): that agent name/.test(err.message)) continue;
312+ if (err instanceof UsageError && /refused \(409\): this box is at its agent limit/.test(err.message))
313+ throw new UsageError(
314+ `this box is at its agent limit (${record.grant.max_agents}): abso box shows its grant, and an agent can be revoked on the website's Boxes page`,
315+ );
316+ throw err;
317+ }
318+ }
319+ throw new UsageError(
320+ `${base} through ${base}-${tries} are all taken in ${project}: give this agent a name with --agent <name>`,
321+ );
322+}
280324/** Ends the box on the server, then forgets it here. Forgets it even when the server can't be reached. */
281325export async function logout(record: BoxRecord, fetcher: typeof fetch = fetch, home = homedir()): Promise<boolean> {
282326 let ended = true;
--- a/client/src/join-commands.ts
+++ b/client/src/join-commands.ts
@@ -1,11 +1,11 @@
11// abso init and abso join: start a project from a folder, or join one into a folder.
22
3−import { existsSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
4−import { join, resolve } from "node:path";
3+import { existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
4+import { join, relative, resolve } from "node:path";
55import { Api, ApiError } from "./api.ts";
66import { bool, type Parsed, str, UsageError } from "./args.ts";
77import { writeKnownBlobs } from "./blobs.ts";
8−import { mintKey, readBox, signedInServers } from "./box.ts";
8+import { defaultAgentName, mintFreeName, mintKey, readBox, signedInServers } from "./box.ts";
99import { STARTING_ABSOIGNORE } from "./check-ignore.ts";
1010import { CLIENT_DIR } from "./client-version.ts";
1111import { type Env, type Output, plural } from "./command-env.ts";
@@ -33,10 +33,12 @@
3333 return signedIn[0] ?? TOKEN_SERVERS[0]!;
3434}
3535
36+const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
3638function agentFrom(args: Parsed): string {
3739 const agent = str(args, "agent");
3840 if (!agent) throw new UsageError("give this folder's agent name with --agent <name>");
39− if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(agent))
41+ if (!NAME.test(agent))
4042 throw new UsageError(`agent names are letters, digits, dot, dash and underscore (got ${JSON.stringify(agent)})`);
4143 return agent;
4244}
@@ -103,18 +105,49 @@
103105 };
104106}
105107
108+/**
109+ * Where abso join writes (abso issue 351): --dir, else here when this folder is empty, else ./<project>, which has
110+ * to be new or empty so nobody's files are written over.
111+ */
112+function joinFolder(args: Parsed, env: Env, project: string): string {
113+ const dir = str(args, "dir");
114+ if (dir !== undefined) return resolve(env.cwd, dir);
115+ const empty = (path: string) => readdirSync(path).length === 0;
116+ if (empty(env.cwd)) return resolve(env.cwd);
117+ const sub = resolve(env.cwd, project);
118+ if (existsSync(sub) && (!statSync(sub).isDirectory() || !empty(sub)))
119+ throw new UsageError(`${sub} already has files in it: say where to join with --dir <folder>`);
120+ return sub;
121+}
106123export async function runJoin(args: Parsed, env: Env): Promise<Output> {
107− if (args.positionals.length !== 1)
108− throw new UsageError("join takes the project name: abso join <project> --agent <name>");
124+ if (args.positionals.length !== 1) throw new UsageError("join takes the project name: abso join <project>");
109125 const project = args.positionals[0]!;
110− const root = resolve(env.cwd, str(args, "dir") ?? ".");
126+ if (!NAME.test(project))
127+ throw new UsageError(
128+ `project names are letters, digits, dot, dash and underscore (got ${JSON.stringify(project)})`,
129+ );
130+ const root = joinFolder(args, env, project);
111131 mkdirSync(root, { recursive: true });
112132 if (findRoot(root) === root) throw new UsageError(`${root} already has .abso/config.json`);
113133 const reader = bool(args, "reader");
114− const config = { ...makeSettings(args, env, project), ...(reader ? { reader: true } : {}) };
115− const box = readBox(config.server);
116− const minted =
117− box && !existsSync(join(stateDir(root), "key")) ? await mintKey(box, project, config.agent) : undefined;
134+ const server = serverFrom(args, env);
135+ const box = readBox(server);
136+ const hasKey = existsSync(join(stateDir(root), "key"));
137+ let agent: string;
138+ let minted: { key: string; id: string } | undefined;
139+ if (str(args, "agent") !== undefined) {
140+ agent = agentFrom(args);
141+ minted = box && !hasKey ? await mintKey(box, project, agent) : undefined;
142+ } else {
143+ // abso issue 351: no --agent takes the box's name, or the next free -2, -3.
144+ if (!box) throw new UsageError("abso login signs this box in first (or give --agent <name>)");
145+ if (hasKey) throw new UsageError(`${root} already has a key: give its agent's name with --agent <name>`);
146+ const free = await mintFreeName(box, project, defaultAgentName());
147+ agent = free.agent;
148+ minted = { key: free.key, id: free.id };
149+ }
150+ const config = { server, project, agent, ...(reader ? { reader: true } : {}) };
118151 writeConfig(root, config);
119152 clearKnown(root);
120153 if (minted) installKey(root, JSON.stringify(minted));
@@ -142,6 +175,7 @@
142175 : []),
143176 ...setupHint(root),
144177 hookHint(env.env),
178+ ...(root === resolve(env.cwd) ? [] : [`cd ${relative(env.cwd, root) || root}`]),
145179 ].join("\n"),
146180 json: {
147181 project,
--- a/client/src/main.ts
+++ b/client/src/main.ts
@@ -262,9 +262,9 @@
262262 },
263263 join: {
264264 flags: { server: "string", agent: "string", dir: "string", reader: "boolean" },
265− usage: "abso join <project> --agent <name> [--server <url>] [--dir <folder>] [--reader]",
265+ usage: "abso join <project> [--agent <name>] [--dir <folder>] [--server <url>] [--reader]",
266266 about:
267− "Join a project as a new agent and write its shared files into the folder (default: here). The server is --server, else ABSO_SERVER, else the one this box signed in to with abso login, else https://api.abso.dev. --reader joins as a reader: it reads everything and records nothing, and the server refuses its edits, records and publishes.",
267+ "Join a project as a new agent and write its shared files into a folder: --dir, else here when this folder is empty, else ./<project> (which has to be new or empty). Without --agent the agent is named after this box (the name abso login gave it, lower-cased), or the next free <name>-2, <name>-3; that needs the box signed in with abso login. The server is --server, else ABSO_SERVER, else the one this box signed in to with abso login, else https://api.abso.dev. --reader joins as a reader: it reads everything and records nothing, and the server refuses its edits, records and publishes.",
268268 run: runJoin,
269269 },
270270 record: {
--- a/client/test/box.test.ts
+++ b/client/test/box.test.ts
@@ -13,6 +13,7 @@
1313 forgetBox,
1414 login,
1515 logout,
16+ mintFreeName,
1617 mintKey,
1718 readBox,
1819 signedInServers,
@@ -305,3 +306,35 @@
305306 );
306307 assert.equal(serverFrom(args(SERVER), none, h), SERVER, "--server settles it");
307308});
310+test("join with no --agent takes the box's name, skips names this box holds, and moves past a taken one (abso issue 351)", async () => {
311+ const seen: Seen = [];
312+ const fetcher = server(
313+ [
314+ [200, { box_id: record.id, agents: [{ project: "abso", agent: "snow", key_id: "k_0" }] }],
315+ [409, { error: "that agent name is already in use in this project; pick a new one" }],
316+ [200, { key: "abso_sk_new", id: "k_3" }],
317+ ],
318+ seen,
319+ );
320+ const got = await mintFreeName(record, "abso", "snow", fetcher);
321+ assert.deepEqual(got, { key: "abso_sk_new", id: "k_3", agent: "snow-3" });
322+ assert.deepEqual(
323+ seen.map((s) => [new URL(s.url).pathname, (s.body as { agent?: string } | null)?.agent ?? null]),
324+ [
325+ ["/box", null],
326+ ["/box/agent-key", "snow-2"],
327+ ["/box/agent-key", "snow-3"],
328+ ],
329+ "snow is this box's already: never minted again, which would replace its key",
330+ );
332+ const limit = server(
333+ [
334+ [200, { box_id: record.id, agents: [] }],
335+ [409, { error: "this box is at its agent limit" }],
336+ ],
337+ [],
338+ );
339+ await assert.rejects(mintFreeName(record, "abso", "snow", limit), /this box is at its agent limit \(5\)/);
340+});
--- a/client/test/client.test.ts
+++ b/client/test/client.test.ts
@@ -525,7 +525,7 @@
525525 git("init", "-q");
526526 git("config", "core.autocrlf", "false");
527527 write(bob, { ".gitattributes": "*.txt eol=crlf\n*.bin -text\n" });
528− assert.equal((await avcs(bob, "join", name, "--agent", "bob", "--server", fake.url)).exit, 0);
528+ assert.equal((await avcs(bob, "join", name, "--agent", "bob", "--server", fake.url, "--dir", ".")).exit, 0);
529529 write(folders.alice!, { "a.txt": "one\ntwo\n", "b.md": "one\ntwo\n", "c.bin": "x\ny\n" });
530530 assert.equal((await avcs(folders.alice!, "publish", "-m", "three files")).exit, 0);
531531 assert.equal((await avcs(bob, "sync")).exit, 0);
@@ -2542,6 +2542,29 @@
25422542 assert.doesNotMatch(without.stdout, /Nothing is installed/);
25432543 });
25442544
2545+ it("join writes here when this folder is empty, else into ./<project>, never over someone's files (abso issue 351)", async () => {
2546+ const { name } = await project({ "a.ts": "one\n" }, "alice");
2547+ const run = (cwd: string, agent: string) =>
2548+ main(["join", name, "--agent", agent, "--server", fake.url], { cwd, env: ENV, write: () => {} });
2549+ const empty = temp();
2550+ const here = await run(empty, "bob");
2551+ assert.equal(here.exit, 0, here.stderr);
2552+ assert.equal(read(empty, "a.ts"), "one\n", "an empty folder: here");
2553+ assert.doesNotMatch(here.stdout, /^cd /m);
2554+ const busy = temp();
2555+ write(busy, { "notes.md": "mine\n" });
2556+ const sub = await run(busy, "carol");
2557+ assert.equal(sub.exit, 0, sub.stderr);
2558+ assert.equal(read(join(busy, name), "a.ts"), "one\n", "a folder with files in it: ./<project>");
2559+ assert.equal(read(busy, "notes.md"), "mine\n");
2560+ assert.match(sub.stdout, new RegExp(`^cd ${name}$`, "m"));
2561+ const taken = temp();
2562+ write(taken, { "notes.md": "mine\n", [`${name}/other.md`]: "theirs\n" });
2563+ const refused = await run(taken, "dave");
2564+ assert.notEqual(refused.exit, 0);
2565+ assert.match(refused.stderr, /already has files in it: say where to join with --dir <folder>/);
2566+ });
25452568 it("gives a call more time by what it sends: 1 s per 100 KB over the base (abso issue 34)", () => {
25462569 assert.equal(timeoutFor(30_000, undefined), 30_000);
25472570 assert.equal(timeoutFor(30_000, "{}"), 30_000);