{"file":"client/src/lock.ts","version":318,"total":182,"from":1,"to":182,"lines":[{"n":1,"text":"// One writer at a time for a folder's own records (#51, as git's index.lock).","atom":"a0","author":"init","version":0},{"n":2,"text":"// Recording and applying a sync rewrite .avcs/ (known copies, modes.json,","atom":"a0","author":"init","version":0},{"n":3,"text":"// blobs.json); two at once, in different processes, could leave a known copy","atom":"a0","author":"init","version":0},{"n":4,"text":"// out of step with the folder. They take .avcs/lock. A command waits a moment","atom":"a0","author":"init","version":0},{"n":5,"text":"// for the lock (a hook holds it for about 100 ms), then fails with a","atom":"a0","author":"init","version":0},{"n":6,"text":"// sentence; the command hook never waits, and leaves the edit for the next","atom":"a0","author":"init","version":0},{"n":7,"text":"// record.","atom":"a0","author":"init","version":0},{"n":8,"text":"//","atom":"a0","author":"init","version":0},{"n":9,"text":"// The lock is never there without its contents: they're written to a file of","atom":"a0","author":"init","version":0},{"n":10,"text":"// the taker's own and linked into place, which fails if a lock exists","atom":"a0","author":"init","version":0},{"n":11,"text":"// already. Its holder renews it every RENEW_MS, so a lock not","atom":"a316","author":"infra-split","version":37},{"n":12,"text":"// renewed for LOCK_STALE_MS was left behind, whatever process now has its","atom":"a0","author":"init","version":0},{"n":13,"text":"// id; a long record or sync is never taken over while it writes. Taking one","atom":"a0","author":"init","version":0},{"n":14,"text":"// over goes through a rename aside, which only one process can win.","atom":"a0","author":"init","version":0},{"n":15,"text":"","atom":"a0","author":"init","version":0},{"n":16,"text":"import { closeSync, linkSync, openSync, readFileSync, renameSync, rmSync, statSync, writeSync } from \"node:fs\";","atom":"a0","author":"init","version":0},{"n":17,"text":"import { basename, join } from \"node:path\";","atom":"a109","author":"review","version":17},{"n":18,"text":"import { UsageError } from \"./args.ts\";","atom":"a0","author":"init","version":0},{"n":19,"text":"import { LOCK_FILE, LOCK_STALE_MS, type LockInfo } from \"./core.ts\";","atom":"a0","author":"init","version":0},{"n":20,"text":"import { fromEnv } from \"./env-names.ts\";","atom":"a211","author":"review","version":27},{"n":21,"text":"import { writeFileAtomic } from \"./folder.ts\";","atom":"a0","author":"init","version":0},{"n":22,"text":"import { stateDir } from \"./state-dir.ts\";","atom":"a109","author":"review","version":17},{"n":23,"text":"","atom":"a0","author":"init","version":0},{"n":24,"text":"/** How long a command waits for the lock before it says who holds it. */","atom":"a0","author":"init","version":0},{"n":25,"text":"const WAIT_MS = 3000;","atom":"a0","author":"init","version":0},{"n":26,"text":"/** How often a holder renews its lock. */","atom":"a0","author":"init","version":0},{"n":27,"text":"const RENEW_MS = 20_000;","atom":"a0","author":"init","version":0},{"n":28,"text":"","atom":"a0","author":"init","version":0},{"n":29,"text":"/** Locks this process holds, by folder, and how deep: a locked call inside another one doesn't wait for itself. */","atom":"a0","author":"init","version":0},{"n":30,"text":"const held = new Map<string, { depth: number; renew: ReturnType<typeof setInterval> }>();","atom":"a0","author":"init","version":0},{"n":31,"text":"","atom":"a0","author":"init","version":0},{"n":32,"text":"const lockPath = (root: string): string => join(stateDir(root), LOCK_FILE);","atom":"a109","author":"review","version":17},{"n":33,"text":"","atom":"a0","author":"init","version":0},{"n":34,"text":"function readInfo(path: string): LockInfo | undefined {","atom":"a0","author":"init","version":0},{"n":35,"text":"\ttry {","atom":"a0","author":"init","version":0},{"n":36,"text":"\t\treturn JSON.parse(readFileSync(path, \"utf8\")) as LockInfo;","atom":"a0","author":"init","version":0},{"n":37,"text":"\t} catch {","atom":"a0","author":"init","version":0},{"n":38,"text":"\t\treturn undefined;","atom":"a0","author":"init","version":0},{"n":39,"text":"\t}","atom":"a0","author":"init","version":0},{"n":40,"text":"}","atom":"a0","author":"init","version":0},{"n":41,"text":"","atom":"a0","author":"init","version":0},{"n":42,"text":"/**","atom":"a0","author":"init","version":0},{"n":43,"text":" * Whether a lock is still held: renewed lately and its process running. A","atom":"a0","author":"init","version":0},{"n":44,"text":" * lock that can't be read (written in place by an older client, caught","atom":"a0","author":"init","version":0},{"n":45,"text":" * between creating and writing) is held until its file is LOCK_STALE_MS old.","atom":"a0","author":"init","version":0},{"n":46,"text":" */","atom":"a0","author":"init","version":0},{"n":47,"text":"function live(path: string, info: LockInfo | undefined, now: number): boolean {","atom":"a0","author":"init","version":0},{"n":48,"text":"\tif (!info || typeof info.pid !== \"number\" || typeof info.time !== \"number\") {","atom":"a0","author":"init","version":0},{"n":49,"text":"\t\ttry {","atom":"a0","author":"init","version":0},{"n":50,"text":"\t\t\treturn now - statSync(path).mtimeMs <= LOCK_STALE_MS;","atom":"a0","author":"init","version":0},{"n":51,"text":"\t\t} catch {","atom":"a0","author":"init","version":0},{"n":52,"text":"\t\t\treturn false;","atom":"a0","author":"init","version":0},{"n":53,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":54,"text":"\t}","atom":"a0","author":"init","version":0},{"n":55,"text":"\tif (now - info.time > LOCK_STALE_MS) return false;","atom":"a0","author":"init","version":0},{"n":56,"text":"\ttry {","atom":"a0","author":"init","version":0},{"n":57,"text":"\t\tprocess.kill(info.pid, 0);","atom":"a0","author":"init","version":0},{"n":58,"text":"\t\treturn true;","atom":"a0","author":"init","version":0},{"n":59,"text":"\t} catch (e) {","atom":"a0","author":"init","version":0},{"n":60,"text":"\t\t// EPERM: it runs, as another user.","atom":"a0","author":"init","version":0},{"n":61,"text":"\t\treturn (e as NodeJS.ErrnoException).code === \"EPERM\";","atom":"a0","author":"init","version":0},{"n":62,"text":"\t}","atom":"a0","author":"init","version":0},{"n":63,"text":"}","atom":"a0","author":"init","version":0},{"n":64,"text":"","atom":"a0","author":"init","version":0},{"n":65,"text":"/** Put `info` at `path` only if nothing is there: a file of our own, linked into place. */","atom":"a0","author":"init","version":0},{"n":66,"text":"function create(path: string, info: LockInfo): \"taken\" | \"exists\" | \"no folder\" {","atom":"a0","author":"init","version":0},{"n":67,"text":"\tconst mine = `${path}.${process.pid}.${Math.random().toString(36).slice(2)}`;","atom":"a0","author":"init","version":0},{"n":68,"text":"\ttry {","atom":"a0","author":"init","version":0},{"n":69,"text":"\t\tconst fd = openSync(mine, \"wx\");","atom":"a0","author":"init","version":0},{"n":70,"text":"\t\twriteSync(fd, JSON.stringify(info));","atom":"a0","author":"init","version":0},{"n":71,"text":"\t\tcloseSync(fd);","atom":"a0","author":"init","version":0},{"n":72,"text":"\t} catch (e) {","atom":"a0","author":"init","version":0},{"n":73,"text":"\t\tif ((e as NodeJS.ErrnoException).code === \"ENOENT\") return \"no folder\";","atom":"a0","author":"init","version":0},{"n":74,"text":"\t\tthrow e;","atom":"a0","author":"init","version":0},{"n":75,"text":"\t}","atom":"a0","author":"init","version":0},{"n":76,"text":"\ttry {","atom":"a0","author":"init","version":0},{"n":77,"text":"\t\tlinkSync(mine, path);","atom":"a0","author":"init","version":0},{"n":78,"text":"\t\treturn \"taken\";","atom":"a0","author":"init","version":0},{"n":79,"text":"\t} catch (e) {","atom":"a0","author":"init","version":0},{"n":80,"text":"\t\tif ((e as NodeJS.ErrnoException).code === \"EEXIST\") return \"exists\";","atom":"a0","author":"init","version":0},{"n":81,"text":"\t\tthrow e;","atom":"a0","author":"init","version":0},{"n":82,"text":"\t} finally {","atom":"a0","author":"init","version":0},{"n":83,"text":"\t\trmSync(mine, { force: true });","atom":"a0","author":"init","version":0},{"n":84,"text":"\t}","atom":"a0","author":"init","version":0},{"n":85,"text":"}","atom":"a0","author":"init","version":0},{"n":86,"text":"","atom":"a0","author":"init","version":0},{"n":87,"text":"/** Take the lock if it's free (or left behind): true, false while someone holds it, or 'no folder' before init. */","atom":"a0","author":"init","version":0},{"n":88,"text":"function take(root: string, what: string): boolean | \"no folder\" {","atom":"a0","author":"init","version":0},{"n":89,"text":"\tconst path = lockPath(root);","atom":"a0","author":"init","version":0},{"n":90,"text":"\tfor (let tries = 0; tries < 3; tries++) {","atom":"a0","author":"init","version":0},{"n":91,"text":"\t\tconst made = create(path, { pid: process.pid, time: Date.now(), what });","atom":"a0","author":"init","version":0},{"n":92,"text":"\t\tif (made !== \"exists\") return made === \"taken\" ? true : \"no folder\";","atom":"a0","author":"init","version":0},{"n":93,"text":"\t\tconst there = readInfo(path);","atom":"a0","author":"init","version":0},{"n":94,"text":"\t\tif (live(path, there, Date.now())) return false;","atom":"a0","author":"init","version":0},{"n":95,"text":"\t\t// Left behind: move it aside. Only one taker's rename finds it.","atom":"a0","author":"init","version":0},{"n":96,"text":"\t\tconst aside = `${path}.left.${process.pid}`;","atom":"a0","author":"init","version":0},{"n":97,"text":"\t\ttry {","atom":"a0","author":"init","version":0},{"n":98,"text":"\t\t\trenameSync(path, aside);","atom":"a0","author":"init","version":0},{"n":99,"text":"\t\t} catch {","atom":"a0","author":"init","version":0},{"n":100,"text":"\t\t\tcontinue;","atom":"a0","author":"init","version":0},{"n":101,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":102,"text":"\t\t// A quicker taker may have put a fresh lock there between our read and our rename: give it back.","atom":"a0","author":"init","version":0},{"n":103,"text":"\t\tconst moved = readInfo(aside);","atom":"a0","author":"init","version":0},{"n":104,"text":"\t\tif (moved && (moved.pid !== there?.pid || moved.time !== there?.time) && live(aside, moved, Date.now())) {","atom":"a0","author":"init","version":0},{"n":105,"text":"\t\t\ttry {","atom":"a0","author":"init","version":0},{"n":106,"text":"\t\t\t\tlinkSync(aside, path);","atom":"a0","author":"init","version":0},{"n":107,"text":"\t\t\t} catch {","atom":"a0","author":"init","version":0},{"n":108,"text":"\t\t\t\t// Someone else took it meanwhile; either way it isn't ours.","atom":"a0","author":"init","version":0},{"n":109,"text":"\t\t\t}","atom":"a0","author":"init","version":0},{"n":110,"text":"\t\t\trmSync(aside, { force: true });","atom":"a0","author":"init","version":0},{"n":111,"text":"\t\t\treturn false;","atom":"a0","author":"init","version":0},{"n":112,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":113,"text":"\t\trmSync(aside, { force: true });","atom":"a0","author":"init","version":0},{"n":114,"text":"\t}","atom":"a0","author":"init","version":0},{"n":115,"text":"\treturn false;","atom":"a0","author":"init","version":0},{"n":116,"text":"}","atom":"a0","author":"init","version":0},{"n":117,"text":"","atom":"a0","author":"init","version":0},{"n":118,"text":"/** Hold the lock for this process: renewed every RENEW_MS until let go. */","atom":"a0","author":"init","version":0},{"n":119,"text":"function hold(root: string, what: string): void {","atom":"a0","author":"init","version":0},{"n":120,"text":"\tconst renew = setInterval(() => {","atom":"a0","author":"init","version":0},{"n":121,"text":"\t\t// Only while it's still ours; written aside and renamed in, so a reader never sees half of it.","atom":"a0","author":"init","version":0},{"n":122,"text":"\t\tif (readInfo(lockPath(root))?.pid !== process.pid) return;","atom":"a0","author":"init","version":0},{"n":123,"text":"\t\ttry {","atom":"a0","author":"init","version":0},{"n":124,"text":"\t\t\twriteFileAtomic(lockPath(root), JSON.stringify({ pid: process.pid, time: Date.now(), what } satisfies LockInfo));","atom":"a0","author":"init","version":0},{"n":125,"text":"\t\t} catch {","atom":"a0","author":"init","version":0},{"n":126,"text":"\t\t\t// The next renewal tries again.","atom":"a0","author":"init","version":0},{"n":127,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":128,"text":"\t}, RENEW_MS);","atom":"a0","author":"init","version":0},{"n":129,"text":"\trenew.unref();","atom":"a0","author":"init","version":0},{"n":130,"text":"\theld.set(root, { depth: 1, renew });","atom":"a0","author":"init","version":0},{"n":131,"text":"}","atom":"a0","author":"init","version":0},{"n":132,"text":"","atom":"a0","author":"init","version":0},{"n":133,"text":"function letGo(root: string): void {","atom":"a0","author":"init","version":0},{"n":134,"text":"\tconst h = held.get(root);","atom":"a0","author":"init","version":0},{"n":135,"text":"\tif (!h || --h.depth > 0) return;","atom":"a0","author":"init","version":0},{"n":136,"text":"\tclearInterval(h.renew);","atom":"a0","author":"init","version":0},{"n":137,"text":"\theld.delete(root);","atom":"a0","author":"init","version":0},{"n":138,"text":"\tif (readInfo(lockPath(root))?.pid === process.pid) rmSync(lockPath(root), { force: true });","atom":"a0","author":"init","version":0},{"n":139,"text":"}","atom":"a0","author":"init","version":0},{"n":140,"text":"","atom":"a0","author":"init","version":0},{"n":141,"text":"/** Run `fn` holding the folder's lock, waiting a moment for another holder; then a sentence saying who holds it. */","atom":"a0","author":"init","version":0},{"n":142,"text":"export async function withFolderLock<T>(root: string, what: string, fn: () => Promise<T>): Promise<T> {","atom":"a0","author":"init","version":0},{"n":143,"text":"\tconst mine = held.get(root);","atom":"a0","author":"init","version":0},{"n":144,"text":"\tif (mine) mine.depth++;","atom":"a0","author":"init","version":0},{"n":145,"text":"\telse {","atom":"a0","author":"init","version":0},{"n":146,"text":"\t\t// ABSO_LOCK_WAIT_MS shortens the wait in tests.","atom":"a211","author":"review","version":27},{"n":147,"text":"\t\tconst until = Date.now() + (Number(fromEnv(process.env, \"LOCK_WAIT_MS\")) || WAIT_MS);","atom":"a211","author":"review","version":27},{"n":148,"text":"\t\tfor (;;) {","atom":"a0","author":"init","version":0},{"n":149,"text":"\t\t\tconst got = take(root, what);","atom":"a0","author":"init","version":0},{"n":150,"text":"\t\t\tif (got === \"no folder\") return fn(); // no .avcs/ yet (init): nothing to protect","atom":"a0","author":"init","version":0},{"n":151,"text":"\t\t\tif (got) break;","atom":"a0","author":"init","version":0},{"n":152,"text":"\t\t\tif (Date.now() >= until) {","atom":"a0","author":"init","version":0},{"n":153,"text":"\t\t\t\tconst info = readInfo(lockPath(root));","atom":"a0","author":"init","version":0},{"n":154,"text":"\t\t\t\tconst since = info ? new Date(info.time).toISOString().slice(11, 19) : \"?\";","atom":"a0","author":"init","version":0},{"n":155,"text":"\t\t\t\tthrow new UsageError(","atom":"a0","author":"init","version":0},{"n":156,"text":"\t\t\t\t\t`another abso command (${info?.what ?? \"one\"}, process ${info?.pid ?? \"?\"}, since ${since} UTC) is changing this folder's records; run this again when it has finished. If none is running, delete ${basename(stateDir(root))}/lock.`,","atom":"a109","author":"review","version":17},{"n":157,"text":"\t\t\t\t);","atom":"a0","author":"init","version":0},{"n":158,"text":"\t\t\t}","atom":"a0","author":"init","version":0},{"n":159,"text":"\t\t\tawait new Promise((ok) => setTimeout(ok, 50));","atom":"a0","author":"init","version":0},{"n":160,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":161,"text":"\t\thold(root, what);","atom":"a0","author":"init","version":0},{"n":162,"text":"\t}","atom":"a0","author":"init","version":0},{"n":163,"text":"\ttry {","atom":"a0","author":"init","version":0},{"n":164,"text":"\t\treturn await fn();","atom":"a0","author":"init","version":0},{"n":165,"text":"\t} finally {","atom":"a0","author":"init","version":0},{"n":166,"text":"\t\tletGo(root);","atom":"a0","author":"init","version":0},{"n":167,"text":"\t}","atom":"a0","author":"init","version":0},{"n":168,"text":"}","atom":"a0","author":"init","version":0},{"n":169,"text":"","atom":"a0","author":"init","version":0},{"n":170,"text":"/** For the command hook, which never waits: the lock and a way to let it go, or undefined while someone else holds it. */","atom":"a0","author":"init","version":0},{"n":171,"text":"export function tryFolderLock(root: string, what: string): (() => void) | undefined {","atom":"a0","author":"init","version":0},{"n":172,"text":"\tconst mine = held.get(root);","atom":"a0","author":"init","version":0},{"n":173,"text":"\tif (mine) {","atom":"a0","author":"init","version":0},{"n":174,"text":"\t\tmine.depth++;","atom":"a0","author":"init","version":0},{"n":175,"text":"\t\treturn () => letGo(root);","atom":"a0","author":"init","version":0},{"n":176,"text":"\t}","atom":"a0","author":"init","version":0},{"n":177,"text":"\tconst got = take(root, what);","atom":"a0","author":"init","version":0},{"n":178,"text":"\tif (got === \"no folder\") return () => {};","atom":"a0","author":"init","version":0},{"n":179,"text":"\tif (!got) return undefined;","atom":"a0","author":"init","version":0},{"n":180,"text":"\thold(root, what);","atom":"a0","author":"init","version":0},{"n":181,"text":"\treturn () => letGo(root);","atom":"a0","author":"init","version":0},{"n":182,"text":"}","atom":"a0","author":"init","version":0}],"atoms":[{"atom":"a0","author":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z","publish":{"draft":null,"version":0,"agent":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z"}},{"atom":"a109","author":"review","message":"","time":1791522261912,"timeIso":"2026-10-09T05:04:21.912Z","publish":{"draft":null,"version":17,"agent":"review","message":"New folders keep their state in .abso/, and the machine's sign-in, servers and admin token are read from ~/.abso/ first, then ~/.avcs/. A folder made before, with only .avcs/, is untouched: it keeps working, is never half-moved, and no older client or mod loses it. stateDir(root) and homeFile/homeWritePath are in the new client/src/state-dir.ts and replace the old AVCS_DIR constant everywhere. abso login writes only to ~/.abso/ and leaves ~/.avcs/ alone; logout removes both copies. The edit mod and the view mod read either folder. .abso is skipped like .avcs (including its odd spellings), the git exclude names whichever folder is in use, and the messages name .abso. Not changed: avcs.json (the server reads that name, so abso.json needs the server to learn it first), .avcsignore, env var names and wire headers. Tests: 709 run, 708 pass (one is skipped), including new ones for the folder choice, the home-file order and box sign-in; biome ci has no errors.","time":1791522270512,"timeIso":"2026-10-09T05:04:30.512Z"}},{"atom":"a211","author":"review","message":"","time":1791533506483,"timeIso":"2026-10-09T08:11:46.483Z","publish":{"draft":null,"version":27,"agent":"review","message":"Client: ABSO_ environment names (AVCS_ still read), .absoignore (.avcsignore still read), .abso-export.json, shot files; READMEs say abso (abso 224)","time":1791533840174,"timeIso":"2026-10-09T08:17:20.174Z"}},{"atom":"a316","author":"infra-split","message":"","time":1791546813593,"timeIso":"2026-10-09T11:53:33.593Z","publish":{"draft":null,"version":37,"agent":"infra-split","message":"Comments and test names give each reason in plain words, without naming who raised it\n\nNotes like \"(#37, Builder 5)\" or \"(AVCS - Security)\" become \"(#37)\" or go, and a private commit hash after a name goes with it. Issue numbers and references such as git's CVE-2019-1353 stay. Only comments and test names change; no behavior changes.","time":1791547035956,"timeIso":"2026-10-09T11:57:15.956Z"}}]}