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/test/install.test.ts?version=41&format=json
421 linesFirst import 91% · review 9%Last change review in version 40, 3 versions changed it

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

1// Installing a mod (src/install.ts, #29): a staged copy that passed its check
2// goes in file by file with renames, plugin.json last; what it replaces is
3// kept for a rollback; a failed check or a failed rename leaves the mod as it
4// was. Every install here goes into a scratch folder. And which client this
5// is (src/version.ts): the commit, read from .git the way git reads it.
6
7import assert from "node:assert/strict";
8import { spawnSync } from "node:child_process";
9import {
10 copyFileSync,
11 existsSync,
12 mkdirSync,
13 mkdtempSync,
14 readdirSync,
15 readFileSync,
16 renameSync,
17 rmSync,
18 statSync,
19 utimesSync,
20 writeFileSync,
21} from "node:fs";
22import { tmpdir } from "node:os";
23import { dirname, join, relative } from "node:path";
24import { after, describe, it } from "node:test";
25import { Api } from "../src/api.ts";
26import { clientVersion, headCommit } from "../src/client-version.ts";
27import {
28 AVCS_MOD,
29 installMod,
30 type ModSpec,
31 modsBehindText,
32 retireOldMod,
33 rollbackMod,
34 versionFor,
35 versionReport,
36} from "../src/install.ts";
37import { main } from "../src/main.ts";
39const hasGit = spawnSync("git", ["--version"]).status === 0;
40const dirs: string[] = [];
41after(() => {
42 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
43});
45function temp(): string {
46 const d = mkdtempSync(join(tmpdir(), "avcs-install-test-"));
47 dirs.push(d);
48 return d;
51const FILES = ["src/core.ts", "hooks/stamp.ts", "hooks/register.ts", "hooks/hooks.json", ".claude-plugin/plugin.json"];
52const pass = () => undefined;
53const at = (dir: string, rel: string) => join(dir, ...rel.split("/"));
54const read = (dir: string, rel: string) => readFileSync(at(dir, rel), "utf8");
55const write = (dir: string, rel: string, text: string) => {
56 mkdirSync(dirname(at(dir, rel)), { recursive: true });
57 writeFileSync(at(dir, rel), text);
58};
60/** A mod's source folder: a small mod shaped like avcs, with these texts in place of the defaults. */
61function source(texts: Record<string, string> = {}): ModSpec {
62 const from = temp();
63 const all: Record<string, string> = {
64 "src/core.ts": "export const core = 1\n",
65 "hooks/stamp.ts": "// Which mod this is.\nexport const STAMP: string | undefined = undefined\n",
66 "hooks/register.ts": "import { core } from '../src/core.ts'\n",
67 "hooks/hooks.json": '{ "modules": ["./register.ts"] }\n',
68 ".claude-plugin/plugin.json": '{ "name": "avcs", "version": "0.1.0", "description": "a mod" }\n',
69 ...texts,
70 };
71 for (const [rel, text] of Object.entries(all)) write(from, rel, text);
72 return { name: "avcs", from, files: FILES };
75/** Every file under dir, relative and sorted. */
76function tree(dir: string): string[] {
77 return readdirSync(dir, { recursive: true, withFileTypes: true })
78 .filter((e) => e.isFile())
79 .map((e) => relative(dir, join(e.parentPath, e.name)).replace(/\\/g, "/"))
80 .sort();
83const contents = (dir: string) => Object.fromEntries(tree(dir).map((rel) => [rel, read(dir, rel)]));
84const record = (dir: string) =>
85 JSON.parse(read(dir, ".avcs-install.json")) as {
86 version: string;
87 files: string[];
88 prev?: { version: string; files: string[] };
89 moving?: string;
90 };
92describe("installing a mod", () => {
93 it("stamps the version into plugin.json and stamp.ts, and leaves nothing staged behind", () => {
94 const spec = source();
95 const target = join(temp(), "avcs");
96 const r = installMod(spec, { target, check: pass, commit: "702c616" });
97 assert.deepEqual(r, { changed: true, version: "0.1.0+702c616", previous: undefined, files: 5 });
98 assert.deepEqual(JSON.parse(read(target, ".claude-plugin/plugin.json")), {
99 name: "avcs",
100 version: "0.1.0+702c616",
101 description: "a mod",
102 });
103 assert.equal(
104 read(target, "hooks/stamp.ts"),
105 "// Which mod this is.\nexport const STAMP: string | undefined = '0.1.0+702c616'\n",
106 );
107 assert.equal(read(target, "src/core.ts"), "export const core = 1\n");
108 assert.deepEqual(tree(target), [".avcs-install.json", ...FILES].sort());
109 assert.deepEqual(
110 { ...record(target), at: undefined, from: undefined },
111 { mod: "avcs", version: "0.1.0+702c616", files: FILES, at: undefined, from: undefined },
112 );
113 });
115 it("moves the files in by rename, what the hook imports first and plugin.json last", () => {
116 const spec = source();
117 const target = join(temp(), "avcs");
118 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
119 write(spec.from, "src/core.ts", "export const core = 2\n");
120 const order: string[] = [];
121 installMod(spec, {
122 target,
123 check: pass,
124 commit: "bbbbbbb",
125 rename: (from, to) => {
126 assert.ok(from.endsWith(".next"));
127 order.push(relative(target, to).replace(/\\/g, "/"));
128 renameSync(from, to);
129 },
130 });
131 assert.deepEqual(order, FILES);
132 });
134 it("checks the staged copy, stamped, and when the check fails changes nothing", () => {
135 const spec = source();
136 const target = join(temp(), "avcs");
137 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
138 const before = contents(target);
139 write(spec.from, "src/core.ts", "export const core = 2\n");
140 let staged = "";
141 const failing = (dir: string) => {
142 staged = read(dir, "hooks/stamp.ts") + read(dir, "src/core.ts");
143 return "2 tests failed";
144 };
145 assert.throws(
146 () => installMod(spec, { target, check: failing, commit: "bbbbbbb" }),
147 /failed its check, so nothing was changed:\n2 tests failed/,
148 );
149 assert.match(staged, /'0\.1\.0\+bbbbbbb'[\s\S]*core = 2/);
150 assert.deepEqual(contents(target), before);
151 });
153 it("leaves no folder behind when a first install fails its check", () => {
154 const target = join(temp(), "avcs");
155 assert.throws(() => installMod(source(), { target, check: () => "no", commit: "aaaaaaa" }), /nothing was changed/);
156 assert.equal(existsSync(target), false);
157 });
159 it("changes nothing, and checks nothing, when the same files are installed again", () => {
160 const spec = source();
161 const target = join(temp(), "avcs");
162 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
163 const mtime = statSync(at(target, "hooks/register.ts")).mtimeMs;
164 let checked = false;
165 const again = installMod(spec, { target, check: () => ((checked = true), undefined), commit: "aaaaaaa" });
166 assert.equal(again.changed, false);
167 assert.equal(checked, false);
168 assert.equal(statSync(at(target, "hooks/register.ts")).mtimeMs, mtime);
169 });
171 it("keeps what it replaces; a rollback puts it back, and a second rollback undoes the first", () => {
172 const spec = source();
173 const target = join(temp(), "avcs");
174 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
175 write(spec.from, "src/core.ts", "export const core = 2\n");
176 const r = installMod(spec, { target, check: pass, commit: "bbbbbbb" });
177 assert.equal(r.previous, "0.1.0+aaaaaaa");
178 assert.equal(read(target, "src/core.ts.prev"), "export const core = 1\n");
180 const back = rollbackMod(spec, { target });
181 assert.deepEqual(back, { changed: true, version: "0.1.0+aaaaaaa", previous: "0.1.0+bbbbbbb", files: 5 });
182 assert.equal(read(target, "src/core.ts"), "export const core = 1\n");
183 assert.equal(JSON.parse(read(target, ".claude-plugin/plugin.json")).version, "0.1.0+aaaaaaa");
184 assert.match(read(target, "hooks/stamp.ts"), /'0\.1\.0\+aaaaaaa'/);
186 const forward = rollbackMod(spec, { target });
187 assert.equal(forward.version, "0.1.0+bbbbbbb");
188 assert.equal(read(target, "src/core.ts"), "export const core = 2\n");
189 assert.equal(record(target).prev?.version, "0.1.0+aaaaaaa");
190 });
192 it("puts back the files already moved when a rename fails midway", () => {
193 const spec = source();
194 const target = join(temp(), "avcs");
195 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
196 const before = Object.fromEntries(FILES.map((rel) => [rel, read(target, rel)]));
197 write(spec.from, "src/core.ts", "export const core = 2\n");
198 write(spec.from, "hooks/register.ts", "import { core } from '../src/core.ts'\nexport const two = core\n");
199 let n = 0;
200 const flaky = (from: string, to: string) => {
201 if (++n === 3) throw Object.assign(new Error("EPERM: operation not permitted, rename"), { code: "EPERM" });
202 renameSync(from, to);
203 };
204 assert.throws(() => installMod(spec, { target, check: pass, commit: "bbbbbbb", rename: flaky }), /EPERM/);
205 assert.deepEqual(Object.fromEntries(FILES.map((rel) => [rel, read(target, rel)])), before);
206 assert.deepEqual(
207 tree(target).filter((f) => f.endsWith(".next")),
208 [],
209 );
210 assert.equal(record(target).version, "0.1.0+aaaaaaa");
211 assert.equal(record(target).moving, undefined);
212 });
214 it("keeps a mod installed before .avcs-install.json for a rollback", () => {
215 const spec = source();
216 const target = join(temp(), "avcs");
217 // The old installer copied four files and wrote nothing else.
218 for (const rel of FILES.filter((f) => f !== "hooks/stamp.ts")) {
219 mkdirSync(dirname(at(target, rel)), { recursive: true });
220 copyFileSync(at(spec.from, rel), at(target, rel));
221 }
222 const r = installMod(spec, { target, check: pass, commit: "bbbbbbb" });
223 assert.equal(r.previous, "0.1.0");
224 const back = rollbackMod(spec, { target });
225 assert.equal(back.version, "0.1.0");
226 assert.equal(JSON.parse(read(target, ".claude-plugin/plugin.json")).version, "0.1.0");
227 assert.equal(existsSync(at(target, "hooks/stamp.ts")), false);
228 });
230 it("after an install that stopped midway, keeps the last whole install for a rollback", () => {
231 const spec = source();
232 const target = join(temp(), "avcs");
233 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
234 write(spec.from, "src/core.ts", "export const core = 2\n");
235 installMod(spec, { target, check: pass, commit: "bbbbbbb" });
236 // An install of ccccccc that died after its first rename: core.ts is new, the rest is bbbbbbb.
237 for (const rel of FILES) copyFileSync(at(target, rel), at(target, `${rel}.prev`));
238 writeFileSync(
239 at(target, ".avcs-install.json"),
240 JSON.stringify({ ...record(target), prev: { ...record(target), prev: undefined }, moving: "0.1.0+ccccccc" }),
241 );
242 write(target, "src/core.ts", "export const core = 3\n");
244 write(spec.from, "src/core.ts", "export const core = 4\n");
245 const r = installMod(spec, { target, check: pass, commit: "ddddddd" });
246 assert.equal(r.previous, "0.1.0+bbbbbbb");
247 assert.equal(read(target, "src/core.ts.prev"), "export const core = 2\n");
248 rollbackMod(spec, { target });
249 assert.equal(read(target, "src/core.ts"), "export const core = 2\n");
250 });
252 it("refuses while another install holds the lock, and takes over a lock left by one that died", () => {
253 const spec = source();
254 const target = join(temp(), "avcs");
255 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
256 write(spec.from, "src/core.ts", "export const core = 2\n");
257 const lock = at(target, ".avcs-install.lock");
258 writeFileSync(lock, "");
259 assert.throws(() => installMod(spec, { target, check: pass, commit: "bbbbbbb" }), /another install is running/);
260 assert.throws(() => rollbackMod(spec, { target }), /another install is running/);
261 const old = new Date(Date.now() - 11 * 60_000);
262 utimesSync(lock, old, old);
263 assert.equal(installMod(spec, { target, check: pass, commit: "bbbbbbb" }).changed, true);
264 assert.equal(existsSync(lock), false);
265 });
267 it("says so when there is nothing to roll back to", () => {
268 const spec = source();
269 const target = join(temp(), "avcs");
270 assert.throws(() => rollbackMod(spec, { target }), /nothing is installed/);
271 installMod(spec, { target, check: pass, commit: "aaaaaaa" });
272 assert.throws(() => rollbackMod(spec, { target }), /no earlier install is kept/);
273 });
275 it("stamps plugin.json's version plus the last commit that changed the mod's files, and .dirty when they differ from it", {
276 skip: !hasGit,
277 }, () => {
278 const spec = source();
279 const git = (...args: string[]) =>
280 spawnSync("git", ["-c", "user.name=t", "-c", "user.email=t@t", "-C", spec.from, ...args], {
281 encoding: "utf8",
282 }).stdout.trim();
283 git("init", "-q");
284 git("add", ".");
285 git("commit", "-q", "-m", "mod");
286 const mod = git("rev-parse", "--short=7", "HEAD");
287 assert.equal(versionFor(spec), `0.1.0+${mod}`);
288 // A commit that changes none of the mod's files leaves the stamp, so a reinstall changes nothing.
289 write(spec.from, "README.md", "not part of the mod\n");
290 assert.equal(versionFor(spec), `0.1.0+${mod}`);
291 git("add", ".");
292 git("commit", "-q", "-m", "docs");
293 assert.equal(versionFor(spec), `0.1.0+${mod}`);
294 const target = join(temp(), "avcs");
295 installMod(spec, { target, check: pass });
296 assert.equal(installMod(spec, { target, check: pass }).changed, false);
297 write(spec.from, "src/core.ts", "export const core = 2\n");
298 assert.equal(versionFor(spec), `0.1.0+${mod}.dirty`);
299 git("commit", "-q", "-am", "core");
300 assert.equal(versionFor(spec), `0.1.0+${git("rev-parse", "--short=7", "HEAD")}`);
301 // Outside a git checkout there is no commit to add.
302 assert.equal(versionFor(spec, null), "0.1.0");
303 });
304});
306describe("which client this is", () => {
307 const git = (cwd: string, ...args: string[]) =>
308 spawnSync(
309 "git",
310 ["-c", "user.name=t", "-c", "user.email=t@t", "-c", "init.defaultBranch=main", "-C", cwd, ...args],
311 { encoding: "utf8" },
312 ).stdout.trim();
314 it("reads the commit from .git as git does: a branch, a packed branch, a detached head, a worktree", {
315 skip: !hasGit,
316 }, () => {
317 const repo = temp();
318 git(repo, "init", "-q");
319 write(repo, "a/b/file.txt", "one\n");
320 git(repo, "add", ".");
321 git(repo, "commit", "-q", "-m", "one");
322 assert.equal(headCommit(join(repo, "a", "b")), git(repo, "rev-parse", "--short=7", "HEAD"));
323 write(repo, "a/b/file.txt", "two\n");
324 git(repo, "commit", "-q", "-am", "two");
325 git(repo, "pack-refs", "--all");
326 assert.equal(existsSync(join(repo, ".git", "refs", "heads", "main")), false);
327 assert.equal(headCommit(repo), git(repo, "rev-parse", "--short=7", "HEAD"));
328 const first = git(repo, "rev-parse", "--short=7", "HEAD~1");
329 git(repo, "checkout", "-q", "--detach", "HEAD~1");
330 assert.equal(headCommit(repo), first);
331 const wt = join(temp(), "wt");
332 git(repo, "worktree", "add", "-q", "-b", "side", wt, "main");
333 write(wt, "a/b/file.txt", "three\n");
334 git(wt, "commit", "-q", "-am", "three");
335 assert.equal(headCommit(join(wt, "a")), git(wt, "rev-parse", "--short=7", "HEAD"));
336 assert.notEqual(headCommit(wt), headCommit(repo));
337 });
339 it("has no commit outside a repository", () => {
340 assert.equal(headCommit(temp()), undefined);
341 });
343 it("avcs --version names this client's version and the mods installed, and which are from another commit", async () => {
344 const config = temp();
345 const spec = source();
346 installMod(spec, {
347 target: join(config, "skills", "abso-hooks"),
348 check: pass,
349 commit: "aaaaaaa",
350 now: new Date("2026-10-05T04:30:00Z"),
351 });
352 const out = await main(["--version"], { cwd: config, env: { CLAUDE_CONFIG_DIR: config }, write: () => {} });
353 const [client, mod, view] = out.stdout.trimEnd().split("\n");
354 assert.equal(out.exit, 0);
355 assert.equal(client, `abso ${clientVersion()} (${dirname(import.meta.dirname)})`);
356 assert.match(
357 mod!,
358 /^the abso-hooks mod: 0\.1\.0\+aaaaaaa in .*skills[\\/]abso-hooks, installed 2026-10-05 04:30 UTC; this client has 0.2.0(\+[0-9a-f]{7}(\.dirty)?)?: node client\/install-mod\.ts installs it$/,
359 );
360 assert.equal(view, "the abso-view mod: not installed (node client/install-view.ts)");
361 const json = JSON.parse(
362 (await main(["--version", "--json"], { cwd: config, env: { CLAUDE_CONFIG_DIR: config }, write: () => {} }))
363 .stdout,
364 );
365 assert.equal(json.client, clientVersion());
366 assert.equal(json.mods[0].version, "0.1.0+aaaaaaa");
367 });
369 it("moves the old avcs mod folder out of the skills folder, not deleting it, once abso-hooks is in", () => {
370 const config = temp();
371 const old = join(config, "skills", "avcs");
372 write(old, ".claude-plugin/plugin.json", '{"name":"avcs","version":"0.1.0"}');
373 const home = temp();
374 const was = process.env.USERPROFILE;
375 const was2 = process.env.HOME;
376 process.env.USERPROFILE = home;
377 process.env.HOME = home;
378 try {
379 const to = retireOldMod(AVCS_MOD, { CLAUDE_CONFIG_DIR: config });
380 assert.ok(to?.startsWith(join(home, ".abso", "retired-mods")));
381 assert.equal(existsSync(old), false);
382 assert.equal(existsSync(join(to!, ".claude-plugin", "plugin.json")), true);
383 assert.equal(retireOldMod(AVCS_MOD, { CLAUDE_CONFIG_DIR: config }), undefined);
384 } finally {
385 if (was === undefined) delete process.env.USERPROFILE;
386 else process.env.USERPROFILE = was;
387 if (was2 === undefined) delete process.env.HOME;
388 else process.env.HOME = was2;
389 }
390 });
392 it("status says when an installed mod is behind this client, where the client knows its commit (abso issue 41)", () => {
393 const config = temp();
394 const env = { CLAUDE_CONFIG_DIR: config };
395 assert.deepEqual(modsBehindText(env), [], "nothing installed, nothing to say");
396 installMod(source(), {
397 target: join(config, "skills", "abso-hooks"),
398 check: pass,
399 commit: "aaaaaaa",
400 now: new Date("2026-10-05T04:30:00Z"),
401 });
402 const current = (versionReport(env).json.mods as { current: string }[])[0]!.current;
403 if (/\+[0-9a-f]{7}$/.test(current))
404 assert.deepEqual(modsBehindText(env), [
405 `The avcs mod installed on this machine is 0.1.0+aaaaaaa, and this client has ${current}: node client/install-mod.ts installs it (new chats load it).`,
406 ]);
407 // A client outside git (an abso folder, a box) can't tell, so it says nothing.
408 else assert.deepEqual(modsBehindText(env), []);
409 });
411 it("the command-line client says which client it is with every call", () => {
412 const headers = new Api({
413 server: "https://avcs.test",
414 project: "p",
415 agent: "a",
416 token: "t",
417 } as ConstructorParameters<typeof Api>[0]).headers("application/json");
418 assert.equal(headers["x-avcs-client"], `abso-cli/${clientVersion()}`);
419 assert.match(clientVersion(), /^\d+\.\d+\.\d+(\+[0-9a-f]{7})?$/);
420 });
421});