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
show on this site's /mcp
CLI
abso show 43 --json
JSON
/christopher/abso-core/versions/43?format=json

status: keep each mod's commit between runs (~/.abso/mod-commits.json), keyed on the checkout's commit read from .git and each mod file's size, time and inode, so status runs no git while nothing moved; files changed too recently to trust are asked again; outside a git checkout git isn't run

of shared·speed··Not checked

Version 42 · Files at version 43

2 files, 126 lines added, 2 removed.

--- a/client/src/install.ts
+++ b/client/src/install.ts
@@ -45,7 +45,7 @@
4545} from "node:fs";
4646import { homedir, tmpdir } from "node:os";
4747import { dirname, join, resolve } from "node:path";
48−import { CLIENT_DIR, clientPlace, clientVersion } from "./client-version.ts";
48+import { CLIENT_DIR, clientPlace, clientVersion, headCommit } from "./client-version.ts";
4949
5050export interface ModSpec {
5151 /** The mod's name, as its plugin.json has it. */
@@ -167,6 +167,65 @@
167167 return `${last.stdout.slice(0, 7)}${changed ? ".dirty" : ""}`;
168168}
169169
170+/** A file changed this close to when an answer was kept can't be told apart from one changed just after it. */
171+const RACY_MS = 2_000;
173+interface KeptModCommit {
174+ readonly head: string;
175+ readonly files: readonly string[];
176+ readonly at: number;
177+ readonly commit: string | null;
178+}
180+/**
181+ * modCommit for the version report, which `abso status` asks on every run: it runs git twice per mod, so
182+ * the answer is kept in ~/.abso/mod-commits.json. A kept answer holds while the checkout's commit (read
183+ * from .git, without git) and each of the mod's files (size, modified time, inode) are as they were, and
184+ * every file was changed clearly before the answer was kept; anything else asks git again. Outside a git
185+ * checkout there is no commit, and git isn't run at all.
186+ */
187+export function keptModCommit(spec: ModSpec, home: string = homedir()): string | null {
188+ const head = headCommit(spec.from);
189+ if (head === undefined) return null;
190+ const at = Date.now();
191+ const files = spec.files.map((rel) => {
192+ try {
193+ const s = statSync(join(spec.from, ...rel.split("/")));
194+ return `${s.size}:${s.mtimeMs}:${s.ino}`;
195+ } catch {
196+ return "-";
197+ }
198+ });
199+ const newest = Math.max(...files.map((f) => (f === "-" ? 0 : Number(f.split(":")[1]))));
200+ const path = join(home, ".abso", "mod-commits.json");
201+ const key = `${resolve(spec.from)}|${spec.files.join("|")}`;
202+ let kept: Record<string, KeptModCommit> = {};
203+ try {
204+ const parsed = JSON.parse(readFileSync(path, "utf8")) as { format?: unknown; mods?: unknown };
205+ if (parsed.format === 1 && typeof parsed.mods === "object" && parsed.mods !== null)
206+ kept = parsed.mods as Record<string, KeptModCommit>;
207+ } catch {}
208+ const was = kept[key];
209+ if (
210+ was !== undefined &&
211+ was.head === head &&
212+ Array.isArray(was.files) &&
213+ was.files.join() === files.join() &&
214+ newest < was.at - RACY_MS
215+ )
216+ return was.commit;
217+ const commit = modCommit(spec);
218+ try {
219+ mkdirSync(dirname(path), { recursive: true });
220+ const next = `${path}.${process.pid}.tmp`;
221+ writeFileSync(next, JSON.stringify({ format: 1, mods: { ...kept, [key]: { head, files, at, commit } } }));
222+ renameSync(next, path);
223+ } catch {
224+ // Best effort: the next run asks git again.
225+ }
226+ return commit;
227+}
170229/** The files to install, stamped with `version`, in install order. */
171230function filesFor(spec: ModSpec, version: string): Map<string, Buffer> {
172231 if (!/^[\w.+-]+$/.test(version)) throw new Error(`can't stamp the version ${JSON.stringify(version)}`);
@@ -486,7 +545,7 @@
486545 at: field(RECORD, "at"),
487546 current: (() => {
488547 try {
489− return versionFor(spec);
548+ return versionFor(spec, keptModCommit(spec));
490549 } catch {
491550 return "not in this copy";
492551 }
--- a/client/test/install.test.ts
+++ b/client/test/install.test.ts
@@ -27,6 +27,7 @@
2727import {
2828 AVCS_MOD,
2929 installMod,
30+ keptModCommit,
3031 type ModSpec,
3132 modsBehindText,
3233 retireOldMod,
@@ -301,6 +302,70 @@
301302 // Outside a git checkout there is no commit to add.
302303 assert.equal(versionFor(spec, null), "0.1.0");
303304 });
306+ it("keeps the mod's commit between runs, and asks git again when the checkout or a file moves, or a file is too new to trust", {
307+ skip: !hasGit,
308+ }, () => {
309+ const spec = source();
310+ const home = temp();
311+ const kept = join(home, ".abso", "mod-commits.json");
312+ const git = (...args: string[]) =>
313+ spawnSync("git", ["-c", "user.name=t", "-c", "user.email=t@t", "-C", spec.from, ...args], {
314+ encoding: "utf8",
315+ }).stdout.trim();
316+ // Every mod file a minute old, so a kept answer can be trusted.
317+ const age = () => {
318+ const old = new Date(Date.now() - 60_000);
319+ for (const rel of FILES) utimesSync(at(spec.from, rel), old, old);
320+ };
321+ // Swaps every kept answer for a made-up one: what comes back says whether git ran.
322+ const plant = () => {
323+ const file = JSON.parse(readFileSync(kept, "utf8"));
324+ for (const k of Object.keys(file.mods)) file.mods[k].commit = "fffffff";
325+ writeFileSync(kept, JSON.stringify(file));
326+ };
327+ // Not a git checkout yet: no commit, and nothing kept.
328+ assert.equal(keptModCommit(spec, home), null);
329+ assert.equal(existsSync(kept), false);
330+ git("init", "-q");
331+ git("add", ".");
332+ git("commit", "-q", "-m", "mod");
333+ const mod = git("rev-parse", "--short=7", "HEAD");
334+ age();
335+ assert.equal(keptModCommit(spec, home), mod);
336+ plant();
337+ assert.equal(keptModCommit(spec, home), "fffffff", "a kept answer is used while nothing moved");
339+ // A file changed in the same moment the answer was kept is never trusted, even when its size and time match.
340+ write(spec.from, "src/core.ts", "export const core = 2\n");
341+ assert.equal(keptModCommit(spec, home), `${mod}.dirty`);
342+ plant();
343+ assert.equal(keptModCommit(spec, home), `${mod}.dirty`, "too new to trust: git runs again");
345+ // An edit that keeps the size, with its time set back a minute, still isn't the time that was kept.
346+ age();
347+ assert.equal(keptModCommit(spec, home), `${mod}.dirty`);
348+ plant();
349+ write(spec.from, "src/core.ts", "export const core = 3\n");
350+ age();
351+ assert.equal(keptModCommit(spec, home), `${mod}.dirty`);
353+ // A commit moves the checkout.
354+ git("commit", "-q", "-am", "core");
355+ age();
356+ const next = git("rev-parse", "--short=7", "HEAD");
357+ assert.equal(keptModCommit(spec, home), next);
358+ plant();
359+ git("commit", "-q", "--allow-empty", "-m", "empty");
360+ assert.equal(keptModCommit(spec, home), next, "a new commit asks git again");
362+ // A file it can't read, or doesn't know the format of, is the same as none.
363+ plant();
364+ writeFileSync(kept, JSON.stringify({ format: 2, mods: JSON.parse(readFileSync(kept, "utf8")).mods }));
365+ assert.equal(keptModCommit(spec, home), next);
366+ writeFileSync(kept, "not json");
367+ assert.equal(keptModCommit(spec, home), next);
368+ });
304369});
305370
306371describe("which client this is", () => {