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
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/join-commands.ts?version=2&format=json
161 linesFirst import 100%Last change First import in version 0, 1 version changed it

You're reading version 2. The newest is 318, and 22 versions since then changed this file. Go to the newestCompare with 318

1// abso init and abso join: start a project from a folder, or join one into a folder.
2
3import { existsSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
4import { join, resolve } from "node:path";
5import { Api, ApiError } from "./api.ts";
6import { bool, type Parsed, str, UsageError } from "./args.ts";
7import { writeKnownBlobs } from "./blobs.ts";
8import { mintKey, readBox } from "./box.ts";
9import { STARTING_AVCSIGNORE } from "./check-ignore.ts";
10import { CLIENT_DIR } from "./client-version.ts";
11import { type Env, type Output, plural } from "./command-env.ts";
12import { leftOutText, syncFolder, uploadBlobs } from "./commands.ts";
13import { AVCS_DIR, findRoot, installKey, readConfig, type Settings, tokenFor, writeConfig } from "./config.ts";
14import { clearKnown, loadIgnore, readFolder, writeKnown } from "./folder.ts";
15import { modInstalled } from "./install.ts";
16import { type FullMode, foldModes, writeKnownModes } from "./modes.ts";
17import { PROJECT_FILE, readProjectFile, startingProjectFileText, writeStartingProjectFile } from "./project-file.ts";
18import { refusedText } from "./safety.ts";
20function serverFrom(args: Parsed, env: Env): string {
21 const server = str(args, "server") ?? env.env.AVCS_SERVER;
22 if (!server) throw new UsageError("give the server with --server <url> (or AVCS_SERVER)");
23 return server;
26function agentFrom(args: Parsed): string {
27 const agent = str(args, "agent");
28 if (!agent) throw new UsageError("give this folder's agent name with --agent <name>");
29 if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(agent))
30 throw new UsageError(`agent names are letters, digits, dot, dash and underscore (got ${JSON.stringify(agent)})`);
31 return agent;
34function makeSettings(args: Parsed, env: Env, project: string): Settings {
35 return { server: serverFrom(args, env), project, agent: agentFrom(args) };
38export async function runInit(args: Parsed, env: Env): Promise<Output> {
39 if (args.positionals.length > 1) throw new UsageError("init takes one folder");
40 const root = resolve(env.cwd, args.positionals[0] ?? ".");
41 if (!existsSync(root) || !statSync(root).isDirectory()) throw new UsageError(`${root} is not a folder`);
42 if (findRoot(root) === root) throw new UsageError(`${root} already has .avcs/config.json`);
43 const project = str(args, "project");
44 if (!project) throw new UsageError("give the project a name with --project <name>");
45 const config = makeSettings(args, env, project);
46 const asIs = bool(args, "as-is");
47 const avcsignore = join(root, ".avcsignore");
48 const wroteIgnore = !asIs && !existsSync(avcsignore);
49 if (wroteIgnore) writeFileSync(avcsignore, STARTING_AVCSIGNORE);
50 // What the server runs on each new version (#39), guessed from package.json; recorded with the rest.
51 const wroteProject = asIs ? "exists" : writeStartingProjectFile(root);
52 const { files, blobs, modes, leftOut } = readFolder(root, loadIgnore(root));
53 const token = tokenFor(config.server, env.env);
54 const api = new Api({ ...config, ...(token ? { token } : {}) });
55 await uploadBlobs(api, blobs);
56 const answer = await api.init(
57 Object.fromEntries(files),
58 modes,
59 Object.fromEntries([...blobs].map(([rel, b]) => [rel, b.sha])),
60 );
61 writeConfig(root, config);
62 clearKnown(root);
63 for (const [rel, text] of files) writeKnown(root, rel, text);
64 writeKnownBlobs(root, new Map([...blobs].map(([rel, b]) => [rel, b.sha])));
65 const knownModes = new Map<string, FullMode>();
66 foldModes(knownModes, modes);
67 writeKnownModes(root, knownModes);
68 const synced = await syncFolder(root, api);
69 return {
70 text: [
71 `Created project ${project} at version ${answer.version} from ${plural(files.size, "text file")} in ${root}.`,
72 ...leftOutText(leftOut),
73 ...(wroteIgnore
74 ? ["Wrote a starting .avcsignore (Claude Code state and operating system files); edit it like .gitignore."]
75 : []),
76 ...startingProjectFileText(wroteProject),
77 ...(answer.skipped && answer.skipped.length > 0 ? [`The server skipped: ${answer.skipped.join(", ")}.`] : []),
78 `This folder is agent ${config.agent}. Settings are in .avcs/config.json.`,
79 ...refusedText(synced.refused),
80 ...(synced.kept.length > 0 ? [`Kept your copy of ${synced.kept.join(", ")}.`] : []),
81 hookHint(env.env),
82 ].join("\n"),
83 json: {
84 project,
85 version: answer.version,
86 files: files.size,
87 root,
88 agent: config.agent,
89 wroteAvcsignore: wroteIgnore,
90 wroteProjectFile: typeof wroteProject === "string" ? null : wroteProject,
91 leftOut,
92 },
93 };
96export async function runJoin(args: Parsed, env: Env): Promise<Output> {
97 if (args.positionals.length !== 1)
98 throw new UsageError("join takes the project name: abso join <project> --agent <name>");
99 const project = args.positionals[0]!;
100 const root = resolve(env.cwd, str(args, "dir") ?? ".");
101 mkdirSync(root, { recursive: true });
102 if (findRoot(root) === root) throw new UsageError(`${root} already has .avcs/config.json`);
103 const reader = bool(args, "reader");
104 const config = { ...makeSettings(args, env, project), ...(reader ? { reader: true } : {}) };
105 const box = readBox(config.server);
106 const minted =
107 box && !existsSync(join(root, AVCS_DIR, "key")) ? await mintKey(box, project, config.agent) : undefined;
108 writeConfig(root, config);
109 clearKnown(root);
110 if (minted) installKey(root, JSON.stringify(minted));
111 const settings = readConfig(root, env.env);
112 const api = new Api(settings);
113 let synced: Awaited<ReturnType<typeof syncFolder>>;
114 try {
115 // Before the sync, so the server never takes a record from this folder.
116 if (reader) await api.reader(true);
117 // Every file, even when the server has seen this agent sync before: its plain sync answer would hold only what changed since (#37).
118 synced = await syncFolder(root, api, true);
119 } catch (error) {
120 if (!(error instanceof ApiError && error.status === 401 && !box && !settings.token)) throw error;
121 rmSync(join(root, AVCS_DIR), { recursive: true, force: true });
122 throw new UsageError(`This box isn't signed in to ${config.server}. abso login signs it in.`);
123 }
124 return {
125 text: [
126 `Joined ${project} as ${config.agent} at version ${synced.version}: wrote ${plural(synced.written.length, "file")} into ${root}.${reader ? " It is a reader: status, sync, log, show, files and export work, and nothing it does is recorded or published." : ""}${minted ? ` It signs in with its own key, ${minted.id}, made from this box's sign-in.` : ""}`,
127 ...refusedText(synced.refused),
128 ...(synced.kept.length > 0
129 ? [
130 `These files were already here and differ from shared, so they were left alone: ${synced.kept.join(", ")}. abso record sends them as your changes.`,
131 ]
132 : []),
133 ...setupHint(root),
134 hookHint(env.env),
135 ].join("\n"),
136 json: {
137 project,
138 agent: config.agent,
139 version: synced.version,
140 written: synced.written,
141 kept: synced.kept,
142 refused: synced.refused,
143 root,
144 },
145 };
148/** What to run before building or testing here (#43, Engine): a joined folder has the project's files, not what avcs.json's setup installs. */
149function setupHint(root: string): string[] {
150 const read = readProjectFile(root);
151 const setup = read !== null && "file" in read ? read.file.setup : "";
152 return setup === ""
153 ? []
154 : [`Nothing is installed yet. Before building or testing here, run ${PROJECT_FILE}'s setup: ${setup}`];
157function hookHint(env: Readonly<Record<string, string | undefined>>): string {
158 return modInstalled(env)
159 ? "Edits made by Claude Code here are recorded by the avcs mod on this machine, signed in as this folder. Anything else: abso record."
160 : `Edits made by Claude Code reach the server through the avcs mod, which isn't on this machine yet: node ${join(CLIENT_DIR, "install-mod.ts")} installs it, and new chats load it. Anything else: abso record.`;