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 38 --json
JSON
/christopher/abso-core/versions/38?format=json

client/launch.mjs: run the client from a git checkout and keep it current. It runs a plain-JS bundle of the client in the same process when the bundle matches the checkout's commit, and client/abso.ts otherwise. A background step, one at a time under a lock, fast-forwards main, installs when package-lock.json changed, and builds the bundle (temporary file, checked, then renamed into place). stdin, stdout, stderr, arguments and exit codes pass through; a bundle that fails to load falls back to the .ts in the same run, and a command that started is never run twice. Notices go only to ~/.abso/launcher.log. Tests run it against a throwaway checkout and home.

of shared·speed··Not checked

Version 37 · Files at version 38

3 files, 367 lines added, 0 removed.

--- a/client/README.md
+++ b/client/README.md
@@ -11,6 +11,22 @@
1111node client/abso.ts help
1212```
1313
14+### Running it from a git checkout
16+`client/launch.mjs` runs the client from a git checkout of abso and keeps the
17+checkout current. Point your `abso` command at it:
19+```bash
20+node /path/to/abso/client/launch.mjs help
21+```
23+It runs a bundle of the client when one matches the checkout's commit, which
24+starts several times faster than the TypeScript, and `client/abso.ts`
25+otherwise. Every few minutes it fast-forwards the checkout (on `main` only)
26+and builds the bundle in the background, with esbuild if the checkout has it.
27+It never changes the command's output or exit code, and writes its own notices
28+to `~/.abso/launcher.log`.
1430## Setting up a folder
1531
1632Folders and machines set up before abso had its name keep working: the older
--- /dev/null
+++ b/client/launch.mjs
@@ -0,0 +1,214 @@
1+// Runs abso from a git checkout of it and keeps that checkout current.
2+//
3+// Point a command at this file (node <checkout>/client/launch.mjs <args>) and it:
4+// - runs the client: a plain-JS bundle of it in this process when one matches the checkout's commit,
5+// otherwise client/abso.ts in a second node. Node strips types from every client file on every run,
6+// so the bundle starts several times faster;
7+// - at most every 5 minutes, and within 30 seconds of a stale bundle, starts a background step that
8+// fast-forwards the checkout (on main only), runs npm install when package-lock.json changed, and
9+// builds the bundle. That step never blocks or fails the command, and only one runs at a time.
10+//
11+// stdin, stdout, stderr, the arguments and the exit code pass through untouched: the exit codes carry
12+// meaning (2 refused, 3 still queued). The launcher's own notices go only to ~/.abso/launcher.log.
13+//
14+// The bundle is client/dist/abso.mjs, with the commit it was built from in client/dist/abso.mjs.commit.
15+// It sits one folder under client/, so the client's own folder (client-version.ts) is still client/.
16+// It is built with esbuild when the checkout has it (in worker/ or at the root): bundled, for node24,
17+// as an ES module, with no build commit stamped in, since a stamped client calls itself a downloaded
18+// copy. It is built to a temporary file and renamed into place, and marked current only after every
19+// require left in it resolves and it runs --version. If it still fails to load, the same run falls
20+// back to client/abso.ts and writes one line to the log, so edit hooks keep recording.
21+//
22+// Plain JS on purpose: this file is loaded on every command, before any bundle.
23+import { execFileSync, spawn } from "node:child_process";
24+import { appendFileSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
25+import { createRequire } from "node:module";
26+import { homedir } from "node:os";
27+import { dirname, join } from "node:path";
28+import { fileURLToPath, pathToFileURL } from "node:url";
30+const repo = dirname(dirname(fileURLToPath(import.meta.url)));
31+const state = join(homedir(), ".abso");
32+const stamp = join(state, "client-update.stamp");
33+const every = 5 * 60 * 1000;
34+const bundle = join(repo, "client", "dist", "abso.mjs");
35+const bundleCommit = `${bundle}.commit`;
36+/** Set on the background step's own node, which updates the checkout and exits. */
37+const UPDATE = "ABSO_LAUNCH_UPDATE";
38+/** Set by the bundle's first line: an error after it came from the command, so it's never run again. */
39+const STARTED = "__absoBundleStarted";
41+/** The checkout's commit, read from .git without running git; undefined if it can't be read. */
42+function headCommit(root = repo) {
43+ try {
44+ const head = readFileSync(join(root, ".git", "HEAD"), "utf8").trim();
45+ if (!head.startsWith("ref:")) return head;
46+ const ref = head.slice(4).trim();
47+ try {
48+ return readFileSync(join(root, ".git", ...ref.split("/")), "utf8").trim();
49+ } catch {
50+ const line = readFileSync(join(root, ".git", "packed-refs"), "utf8")
51+ .split("\n")
52+ .find((l) => l.endsWith(` ${ref}`));
53+ return line?.split(" ")[0];
54+ }
55+ } catch {
56+ return undefined;
57+ }
58+}
60+/** Runs fn under a lock folder; a lock older than 10 minutes is a dead run's and is cleared. */
61+function locked(lock, fn) {
62+ try {
63+ if (Date.now() - statSync(lock).mtimeMs > 10 * 60 * 1000) rmSync(lock, { recursive: true, force: true });
64+ } catch {}
65+ try {
66+ mkdirSync(lock);
67+ } catch {
68+ return;
69+ }
70+ try {
71+ fn();
72+ } finally {
73+ rmSync(lock, { recursive: true, force: true });
74+ }
75+}
77+/** One line in the launcher's log, never on the command's own output. */
78+function note(text) {
79+ try {
80+ mkdirSync(state, { recursive: true });
81+ appendFileSync(join(state, "launcher.log"), `${new Date().toISOString()} abso launch: ${text}\n`);
82+ } catch {}
83+}
85+/**
86+ * The background step, one at a time under a lock (a second one finds it taken and exits): fast-forward,
87+ * install when the lock file moved, then the bundle.
88+ */
89+function update() {
90+ const git = (...a) => execFileSync("git", ["-C", repo, ...a], { encoding: "utf8", windowsHide: true });
91+ mkdirSync(state, { recursive: true });
92+ locked(join(state, "client-update.lock"), () => {
93+ try {
94+ if (git("branch", "--show-current").trim() === "main") {
95+ const before = git("rev-parse", "HEAD").trim();
96+ git("fetch", "-q", "origin", "main");
97+ git("merge", "-q", "--ff-only", "origin/main");
98+ if (git("diff", "--name-only", before, "HEAD", "--", "package-lock.json").trim())
99+ execFileSync("npm", ["install", "--no-audit", "--no-fund"], {
100+ cwd: repo,
101+ stdio: "ignore",
102+ windowsHide: true,
103+ shell: true,
104+ });
105+ }
106+ } catch {}
107+ try {
108+ build(git("rev-parse", "HEAD").trim());
109+ } catch (e) {
110+ note(`the bundle wasn't built (${String(e?.message).split("\n")[0]}); client/abso.ts runs until it is`);
111+ }
112+ });
113+}
115+/** Builds the bundle for this commit, unless it's already current or the checkout has no esbuild. */
116+function build(head) {
117+ try {
118+ if (readFileSync(bundleCommit, "utf8").trim() === head) return;
119+ } catch {}
120+ const require = createRequire(import.meta.url);
121+ let esbuild;
122+ try {
123+ esbuild = require(require.resolve("esbuild", { paths: [join(repo, "worker"), repo] }));
124+ } catch {
125+ return;
126+ }
127+ const exclude = join(repo, ".git", "info", "exclude");
128+ try {
129+ if (!readFileSync(exclude, "utf8").split(/\r?\n/).includes("client/dist/"))
130+ appendFileSync(exclude, "\nclient/dist/\n");
131+ } catch {}
132+ mkdirSync(dirname(bundle), { recursive: true });
133+ const tmp = bundle.replace(/\.mjs$/, `.${process.pid}.tmp.mjs`);
134+ try {
135+ esbuild.buildSync({
136+ entryPoints: [join(repo, "client", "abso.ts")],
137+ bundle: true,
138+ platform: "node",
139+ format: "esm",
140+ target: "node24",
141+ outfile: tmp,
142+ logLevel: "silent",
143+ // A bundled CommonJS package (yaml) calls require("process") lazily, which an ES module
144+ // bundle can't do without a real require. The flag says the bundle started running.
145+ banner: {
146+ js: `globalThis.${STARTED} = true; import { createRequire as __absoCreateRequire } from "node:module"; const require = __absoCreateRequire(import.meta.url);`,
147+ },
148+ });
149+ const from = createRequire(bundle);
150+ for (const m of readFileSync(tmp, "utf8").matchAll(/__require\("([^"]+)"\)/g)) from.resolve(m[1]);
151+ execFileSync(process.execPath, [tmp, "--version"], { cwd: repo, stdio: "ignore", windowsHide: true });
152+ renameSync(tmp, bundle);
153+ writeFileSync(bundleCommit, `${head}\n`);
154+ } finally {
155+ rmSync(tmp, { force: true });
156+ }
157+}
159+/** Runs client/abso.ts in a second node, and exits with its code. */
160+function runTs() {
161+ const child = spawn(process.execPath, [join(repo, "client", "abso.ts"), ...process.argv.slice(2)], {
162+ stdio: "inherit",
163+ });
164+ child.on("exit", (code, signal) => process.exit(code ?? (signal ? 1 : 0)));
165+}
167+/** Starts the background step when it's due: every 5 minutes, or within 30 seconds of a stale bundle. */
168+function maybeUpdate(fresh) {
169+ try {
170+ let last = 0;
171+ try {
172+ last = statSync(stamp).mtimeMs;
173+ } catch {}
174+ if (Date.now() - last <= (fresh ? every : 30_000)) return;
175+ mkdirSync(state, { recursive: true });
176+ writeFileSync(stamp, `${new Date().toISOString()}\n`);
177+ spawn(process.execPath, [fileURLToPath(import.meta.url)], {
178+ detached: true,
179+ stdio: "ignore",
180+ windowsHide: true,
181+ env: { ...process.env, [UPDATE]: "1" },
182+ }).unref();
183+ } catch {}
184+}
186+/** The command: the bundle when it matches the checkout, else client/abso.ts. */
187+async function run() {
188+ const head = headCommit();
189+ let fresh = false;
190+ try {
191+ fresh = head !== undefined && readFileSync(bundleCommit, "utf8").trim() === head;
192+ } catch {}
193+ maybeUpdate(fresh);
194+ if (!fresh) return runTs();
195+ // In this process: the client reads its arguments from process.argv, as if run as `node client/dist/abso.mjs`.
196+ const args = process.argv;
197+ process.argv = [args[0], bundle, ...args.slice(2)];
198+ try {
199+ await import(pathToFileURL(bundle).href);
200+ } catch (e) {
201+ // Only a bundle that never started falls back: one that can't be found or parsed, or imports
202+ // something missing. Anything else came from the command itself, which the .ts would repeat.
203+ const code = e?.code;
204+ const loading =
205+ e instanceof SyntaxError || code === "ERR_MODULE_NOT_FOUND" || code === "ERR_UNSUPPORTED_ESM_URL_SCHEME";
206+ if (!loading || globalThis[STARTED] === true) throw e;
207+ note(`the bundle didn't load (${code ?? e?.name}: ${String(e?.message).split("\n")[0]}), ran client/abso.ts`);
208+ process.argv = args;
209+ runTs();
210+ }
211+}
213+if (process.env[UPDATE] === "1") update();
214+else await run();
--- /dev/null
+++ b/client/test/launch.test.ts
@@ -0,0 +1,137 @@
1+// client/launch.mjs: which client a run takes, and that a bad bundle never stops a command.
2+// Each test runs the real launch.mjs from a throwaway git checkout, with a stand-in client, and a home of its own.
3+
4+import assert from "node:assert/strict";
5+import { execFileSync, spawnSync } from "node:child_process";
6+import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
7+import { tmpdir } from "node:os";
8+import { join } from "node:path";
9+import { describe, it } from "node:test";
11+const LAUNCH = new URL("../launch.mjs", import.meta.url);
13+/** A checkout on main with one commit, launch.mjs and a stand-in client/abso.ts that names itself. */
14+function checkout(): { repo: string; home: string; head: string } {
15+ const root = mkdtempSync(join(tmpdir(), "abso-launch-"));
16+ const repo = join(root, "repo");
17+ const home = join(root, "home");
18+ mkdirSync(join(repo, "client"), { recursive: true });
19+ copyFileSync(LAUNCH, join(repo, "client", "launch.mjs"));
20+ writeFileSync(
21+ join(repo, "client", "abso.ts"),
22+ 'console.log("ts " + JSON.stringify(process.argv.slice(2)));\nprocess.exitCode = Number(process.env.FAKE_EXIT ?? 0);\n',
23+ );
24+ const git = (...a: string[]) => execFileSync("git", ["-C", repo, ...a], { encoding: "utf8" });
25+ git("init", "-q", "-b", "main");
26+ git("add", ".");
27+ git("-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "-m", "one");
28+ // A stamp from now: no background step starts during a test.
29+ mkdirSync(join(home, ".abso"), { recursive: true });
30+ writeFileSync(join(home, ".abso", "client-update.stamp"), "now\n");
31+ return { repo, home, head: git("rev-parse", "HEAD").trim() };
32+}
34+function bundle(repo: string, text: string, commit: string): void {
35+ mkdirSync(join(repo, "client", "dist"), { recursive: true });
36+ writeFileSync(join(repo, "client", "dist", "abso.mjs"), text);
37+ writeFileSync(join(repo, "client", "dist", "abso.mjs.commit"), `${commit}\n`);
38+}
40+function launch(c: { repo: string; home: string }, args: string[], env: Record<string, string> = {}) {
41+ return spawnSync(process.execPath, [join(c.repo, "client", "launch.mjs"), ...args], {
42+ encoding: "utf8",
43+ env: { ...process.env, HOME: c.home, USERPROFILE: c.home, ...env },
44+ });
45+}
47+const NAMES_ITSELF = 'console.log("bundle " + JSON.stringify(process.argv.slice(1)));\n';
49+describe("client/launch.mjs", () => {
50+ it("runs client/abso.ts when there's no bundle, with the arguments and exit code passed through", () => {
51+ const c = checkout();
52+ const r = launch(c, ["issue", "42"], { FAKE_EXIT: "3" });
53+ assert.equal(r.stdout.trim(), 'ts ["issue","42"]');
54+ assert.equal(r.status, 3);
55+ });
57+ it("runs the bundle in its own process when it was built from the checkout's commit", () => {
58+ const c = checkout();
59+ bundle(c.repo, NAMES_ITSELF, c.head);
60+ const r = launch(c, ["--version"]);
61+ assert.equal(
62+ r.stdout.trim(),
63+ `bundle ${JSON.stringify([join(c.repo, "client", "dist", "abso.mjs"), "--version"])}`,
64+ );
65+ assert.equal(r.status, 0);
66+ });
68+ it("runs client/abso.ts when the bundle is from another commit", () => {
69+ const c = checkout();
70+ bundle(c.repo, NAMES_ITSELF, "0".repeat(40));
71+ assert.equal(launch(c, ["status"]).stdout.trim(), 'ts ["status"]');
72+ });
74+ it("falls back to client/abso.ts in the same run when the bundle doesn't load, and writes one line", () => {
75+ const c = checkout();
76+ bundle(c.repo, "this is not javascript (\n", c.head);
77+ const r = launch(c, ["log"]);
78+ assert.equal(r.stdout.trim(), 'ts ["log"]');
79+ assert.equal(r.status, 0);
80+ // The launcher's own notice goes to its log only, never on the command's output.
81+ assert.equal(r.stderr, "");
82+ const log = readFileSync(join(c.home, ".abso", "launcher.log"), "utf8")
83+ .trim()
84+ .split("\n");
85+ assert.equal(log.length, 1);
86+ assert.match(log[0]!, /the bundle didn't load \(SyntaxError/);
87+ });
89+ it("doesn't run a command twice when it fails inside a bundle that loaded", () => {
90+ const c = checkout();
91+ bundle(c.repo, 'throw new Error("the command failed");\n', c.head);
92+ const r = launch(c, ["publish"]);
93+ assert.notEqual(r.status, 0);
94+ assert.doesNotMatch(r.stdout, /^ts /m);
95+ assert.equal(existsSync(join(c.home, ".abso", "launcher.log")), false);
96+ });
98+ it("doesn't run a command twice when it throws a SyntaxError after the bundle started", () => {
99+ const c = checkout();
100+ bundle(c.repo, 'globalThis.__absoBundleStarted = true;\nJSON.parse("{");\n', c.head);
101+ const r = launch(c, ["publish"]);
102+ assert.notEqual(r.status, 0);
103+ assert.doesNotMatch(r.stdout, /^ts /m);
104+ assert.equal(existsSync(join(c.home, ".abso", "launcher.log")), false);
105+ });
107+ it("passes stdin through to the client, from the bundle and from the .ts", () => {
108+ const c = checkout();
109+ const echo = 'let s = "";\nprocess.stdin.on("data", (d) => (s += d)).on("end", () => console.log("got " + s));\n';
110+ writeFileSync(join(c.repo, "client", "abso.ts"), echo);
111+ const fed = (input: string) =>
112+ spawnSync(process.execPath, [join(c.repo, "client", "launch.mjs")], {
113+ input,
114+ encoding: "utf8",
115+ env: { ...process.env, HOME: c.home, USERPROFILE: c.home },
116+ }).stdout.trim();
117+ assert.equal(fed("from the ts"), "got from the ts");
118+ bundle(c.repo, echo, c.head);
119+ assert.equal(fed("from the bundle"), "got from the bundle");
120+ });
122+ it("runs one background step at a time: a second finds the lock taken and leaves it", () => {
123+ const c = checkout();
124+ const lock = join(c.home, ".abso", "client-update.lock");
125+ mkdirSync(lock);
126+ assert.equal(launch(c, [], { ABSO_LAUNCH_UPDATE: "1" }).status, 0);
127+ assert.equal(existsSync(lock), true);
128+ });
130+ it("keeps the background step quiet when it can't fetch or build", () => {
131+ const c = checkout();
132+ const r = launch(c, [], { ABSO_LAUNCH_UPDATE: "1" });
133+ assert.equal(r.status, 0);
134+ assert.equal(r.stdout, "");
135+ assert.equal(existsSync(join(c.repo, "client", "dist", "abso.mjs.commit")), false);
136+ });
137+});