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. Version 38 of shared· speed· Oct 9 (2026-10-09 12:19 UTC) · 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 @@
11 11 node client/abso.ts help 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: 20 + node /path/to/abso/client/launch.mjs help 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`. 14 30 ## Setting up a folder 16 32 Folders 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. 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. 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. 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. 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) { 44 + const head = readFileSync(join(root, ".git", "HEAD"), "utf8").trim(); 45 + if (!head.startsWith("ref:")) return head; 46 + const ref = head.slice(4).trim(); 48 + return readFileSync(join(root, ".git", ...ref.split("/")), "utf8").trim(); 50 + const line = readFileSync(join(root, ".git", "packed-refs"), "utf8") 52 + .find((l) => l.endsWith(` ${ref}`)); 53 + return line?.split(" ")[0]; 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) { 63 + if (Date.now() - statSync(lock).mtimeMs > 10 * 60 * 1000) rmSync(lock, { recursive: true, force: true }); 73 + rmSync(lock, { recursive: true, force: true }); 77 + /** One line in the launcher's log, never on the command's own output. */ 80 + mkdirSync(state, { recursive: true }); 81 + appendFileSync(join(state, "launcher.log"), `${new Date().toISOString()} abso launch: ${text}\n`); 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. 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"), () => { 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"], { 108 + build(git("rev-parse", "HEAD").trim()); 110 + note(`the bundle wasn't built (${String(e?.message).split("\n")[0]}); client/abso.ts runs until it is`); 115 + /** Builds the bundle for this commit, unless it's already current or the checkout has no esbuild. */ 116 + function build(head) { 118 + if (readFileSync(bundleCommit, "utf8").trim() === head) return; 120 + const require = createRequire(import.meta.url); 123 + esbuild = require(require.resolve("esbuild", { paths: [join(repo, "worker"), repo] })); 127 + const exclude = join(repo, ".git", "info", "exclude"); 129 + if (!readFileSync(exclude, "utf8").split(/\r?\n/).includes("client/dist/")) 130 + appendFileSync(exclude, "\nclient/dist/\n"); 132 + mkdirSync(dirname(bundle), { recursive: true }); 133 + const tmp = bundle.replace(/\.mjs$/, `.${process.pid}.tmp.mjs`); 136 + entryPoints: [join(repo, "client", "abso.ts")], 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. 146 + js: `globalThis.${STARTED} = true; import { createRequire as __absoCreateRequire } from "node:module"; const require = __absoCreateRequire(import.meta.url);`, 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`); 155 + rmSync(tmp, { force: true }); 159 + /** Runs client/abso.ts in a second node, and exits with its code. */ 161 + const child = spawn(process.execPath, [join(repo, "client", "abso.ts"), ...process.argv.slice(2)], { 164 + child.on("exit", (code, signal) => process.exit(code ?? (signal ? 1 : 0))); 167 + /** Starts the background step when it's due: every 5 minutes, or within 30 seconds of a stale bundle. */ 168 + function maybeUpdate(fresh) { 172 + last = statSync(stamp).mtimeMs; 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)], { 181 + env: { ...process.env, [UPDATE]: "1" }, 186 + /** The command: the bundle when it matches the checkout, else client/abso.ts. */ 187 + async function run() { 188 + const head = headCommit(); 191 + fresh = head !== undefined && readFileSync(bundleCommit, "utf8").trim() === head; 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)]; 199 + await import(pathToFileURL(bundle).href); 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; 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; 213 + if (process.env[UPDATE] === "1") update(); --- /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. 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")); 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', 24 + const git = (...a: string[]) => execFileSync("git", ["-C", repo, ...a], { encoding: "utf8" }); 25 + git("init", "-q", "-b", "main"); 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() }; 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`); 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], { 43 + env: { ...process.env, HOME: c.home, USERPROFILE: c.home, ...env }, 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); 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"]); 63 + `bundle ${JSON.stringify([join(c.repo, "client", "dist", "abso.mjs"), "--version"])}`, 65 + assert.equal(r.status, 0); 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"]'); 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") 85 + assert.equal(log.length, 1); 86 + assert.match(log[0]!, /the bundle didn't load \(SyntaxError/); 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); 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); 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")], { 115 + env: { ...process.env, HOME: c.home, USERPROFILE: c.home }, 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"); 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"); 126 + assert.equal(launch(c, [], { ABSO_LAUNCH_UPDATE: "1" }).status, 0); 127 + assert.equal(existsSync(lock), true); 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);