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

status: skip reading files found unchanged before. A record in the folder's state dir (clean.json) keeps, per file, the size, time and inode of the file and its known copy, taken before reading; only status's unrecorded and line-ending checks use it, record always reads every file. Files changed too recently to trust are read; an unknown format or client version reads everything. The state dir is found once per walk. The mod-commit test no longer depends on how long it takes.

of shared·speed··Not checked

status: skip reading files found unchanged before. A record in the folder's state dir (clean.json) keeps, per file, the size, time and inode of the file and its known copy, taken before reading; only status's unrecorded and line-ending checks use it, record always reads every file.

Files changed too recently to trust are read; an unknown format or client version reads everything. The state dir is found once per walk. The mod-commit test no longer depends on how long it takes.

Version 43 · Files at version 44

5 files, 251 lines added, 11 removed.

--- /dev/null
+++ b/client/src/clean-files.ts
@@ -0,0 +1,134 @@
1+// Which files `abso status` found the same as their known copy last time, so the
2+// next status reads only the files that may have changed, as git's index does.
3+//
4+// An entry is kept per file: the size, modified time and inode of the file on
5+// disk and of its known copy, each taken before either was read, and whether
6+// the file held a CR. It's trusted only while both still match, and only when
7+// both were changed clearly before the entry was made: a file changed in the
8+// same moment can't be told apart from one changed just after, so it's read.
9+// Anything abso writes into the folder or the known copy changes one side's
10+// size, time or inode, so no writer has to remember to drop entries.
11+//
12+// Only status and the checks it prints use this. record reads every file: a
13+// line status misses is a nuisance, a change record misses is lost work.
15+import { lstatSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
16+import { join } from "node:path";
17+import { clientVersion } from "./client-version.ts";
18+import { stateDir } from "./state-dir.ts";
20+const FILE = "clean.json";
21+const FORMAT = 1;
22+/** How much older than an entry a file's time must be to trust it. */
23+const RACY_MS = 2_000;
25+/** A file's place in the record now, taken before it is read. */
26+export interface CleanKey {
27+ readonly id: string;
28+ /** The newer of the two modified times. */
29+ readonly newest: number;
30+ /** The file on disk's mode bits. */
31+ readonly mode: number;
32+}
34+interface Entry {
35+ readonly id: string;
36+ readonly at: number;
37+ readonly cr: boolean;
38+}
40+export class CleanFiles {
41+ readonly #root: string;
42+ readonly #entries: Map<string, Entry>;
43+ /** When this run started looking; entries made in it carry this time. */
44+ readonly #at = Date.now();
45+ readonly #keys = new Map<string, CleanKey | undefined>();
46+ #changed = false;
48+ private constructor(root: string, entries: Map<string, Entry>) {
49+ this.#root = root;
50+ this.#entries = entries;
51+ }
53+ /** The folder's record, or an empty one when it's missing, unreadable, or from another format or client. */
54+ static load(root: string): CleanFiles {
55+ const entries = new Map<string, Entry>();
56+ try {
57+ const file = JSON.parse(readFileSync(join(stateDir(root), FILE), "utf8")) as {
58+ format?: unknown;
59+ client?: unknown;
60+ files?: Record<string, [string, number, boolean]>;
61+ };
62+ if (file.format === FORMAT && file.client === clientVersion() && typeof file.files === "object" && file.files)
63+ for (const [rel, [id, at, cr]] of Object.entries(file.files))
64+ if (typeof id === "string" && typeof at === "number" && typeof cr === "boolean")
65+ entries.set(rel, { id, at, cr });
66+ } catch {}
67+ return new CleanFiles(root, entries);
68+ }
70+ /**
71+ * The key of a plain file on disk and its known copy; undefined for a link, or when either is missing.
72+ * Taken once per run: a key taken before a later read is still safe to mark with, since any change
73+ * after it was taken makes the next run's key differ.
74+ */
75+ key(diskPath: string, knownPath: string): CleanKey | undefined {
76+ if (this.#keys.has(diskPath)) return this.#keys.get(diskPath);
77+ let key: CleanKey | undefined;
78+ try {
79+ const disk = lstatSync(diskPath);
80+ if (disk.isFile()) {
81+ const known = statSync(knownPath);
82+ key = {
83+ id: `${disk.size}:${disk.mtimeMs}:${disk.ino}|${known.size}:${known.mtimeMs}:${known.ino}`,
84+ newest: Math.max(disk.mtimeMs, known.mtimeMs),
85+ mode: disk.mode,
86+ };
87+ }
88+ } catch {}
89+ this.#keys.set(diskPath, key);
90+ return key;
91+ }
93+ /** Whether the file was the same as its known copy at this key, and if so whether it held a CR; undefined: read it. */
94+ check(rel: string, key: CleanKey): { cr: boolean } | undefined {
95+ const entry = this.#entries.get(rel);
96+ if (entry === undefined || entry.id !== key.id || key.newest >= entry.at - RACY_MS) return undefined;
97+ return { cr: entry.cr };
98+ }
100+ /** The file, read after `key` was taken, is the same as its known copy. */
101+ mark(rel: string, key: CleanKey, cr: boolean): void {
102+ const entry = this.#entries.get(rel);
103+ if (key.newest >= this.#at - RACY_MS) {
104+ // Too new to trust next time either; an old entry for it is no use now.
105+ if (entry !== undefined) {
106+ this.#entries.delete(rel);
107+ this.#changed = true;
108+ }
109+ return;
110+ }
111+ if (entry?.id === key.id && entry.cr === cr) return;
112+ this.#entries.set(rel, { id: key.id, at: this.#at, cr });
113+ this.#changed = true;
114+ }
116+ /** The file differs from its known copy, or is gone. */
117+ drop(rel: string): void {
118+ if (this.#entries.delete(rel)) this.#changed = true;
119+ }
121+ /** Write the record, if anything changed. Best effort: a run that can't write it just reads more next time. */
122+ save(): void {
123+ if (!this.#changed) return;
124+ try {
125+ const dir = stateDir(this.#root);
126+ mkdirSync(dir, { recursive: true });
127+ const files = Object.fromEntries([...this.#entries].map(([rel, e]) => [rel, [e.id, e.at, e.cr]]));
128+ const next = join(dir, `${FILE}.${process.pid}.tmp`);
129+ writeFileSync(next, JSON.stringify({ format: FORMAT, client: clientVersion(), files }));
130+ renameSync(next, join(dir, FILE));
131+ this.#changed = false;
132+ } catch {}
133+ }
134+}
--- a/client/src/folder.ts
+++ b/client/src/folder.ts
@@ -26,8 +26,9 @@
2626import { homedir } from "node:os";
2727import { dirname, join, relative, sep } from "node:path";
2828import { blobOf, keptWholeAs, readKnownBlobs } from "./blobs.ts";
29+import type { CleanFiles } from "./clean-files.ts";
2930import { type GitHome, gitIgnoreSetup, type Ignore, isLocalOnly, makeIgnore, normalize, readAllSync } from "./core.ts";
30−import { type Mode, modeChange, onDisk, PLAIN, readKnownModes } from "./modes.ts";
31+import { FILE_MODE, type Mode, modeChange, onDisk, PLAIN, readKnownModes } from "./modes.ts";
3132import { stateDir } from "./state-dir.ts";
3233
3334export { type Ignore, isLocalOnly, normalize, withEndingsOf } from "./core.ts";
@@ -279,12 +280,13 @@
279280
280281// --- the known copy ---
281282
282−function knownPath(root: string, rel: string): string {
283− return join(stateDir(root), KNOWN, ...insideParts(rel));
283+/** `state` is the folder's stateDir, for a caller that looks up many files: finding it checks the disk each time. */
284+export function knownPath(root: string, rel: string, state: string = stateDir(root)): string {
285+ return join(state, KNOWN, ...insideParts(rel));
284286}
285287
286−export function readKnown(root: string, rel: string): string | undefined {
287− return readText(knownPath(root, rel));
288+export function readKnown(root: string, rel: string, state?: string): string | undefined {
289+ return readText(knownPath(root, rel, state));
288290}
289291
290292export function writeKnown(root: string, rel: string, text: string): void {
@@ -321,11 +323,15 @@
321323 * server's form), known files that are gone (null), and the modes that differ
322324 * from the known modes. An empty file is a file like any other (""); a link's
323325 * text is "".
326+ *
327+ * `clean` lets status skip reading the files it found unchanged last time
328+ * (clean-files.ts). record never passes it, so it always reads every file.
324329 */
325330export function unrecorded(
326331 root: string,
327332 all = false,
328333 meantBinary: ReadonlySet<string> = new Set(),
334+ clean?: CleanFiles,
329335): {
330336 files: Map<string, string | null>;
331337 blobs: Map<string, BlobToSend>;
@@ -342,10 +348,23 @@
342348 const turnedBinary: TurnedBinary[] = [];
343349 // Exact names: on a case-insensitive disk, known/a.ts would also answer for A.ts, hiding a rename that changes only case.
344350 const knownPaths = new Set(listKnown(root));
351+ const state = stateDir(root);
345352 const knownBlobs = readKnownBlobs(root);
346353 const knownModes = readKnownModes(root);
354+ const trusted = all ? undefined : clean;
347355 for (const rel of walk(root, ignore)) {
348356 const was = knownModes.get(rel) ?? PLAIN;
357+ // Taken before the file is read, so an edit during the read changes the key.
358+ const key =
359+ trusted && knownPaths.has(rel) && was.link === null
360+ ? trusted.key(absolute(root, rel), knownPath(root, rel, state))
361+ : undefined;
362+ if (key && trusted!.check(rel, key)) {
363+ present.add(rel);
364+ const change = modeChange(was, { exec: FILE_MODE ? (key.mode & 0o111) !== 0 : was.exec, link: null });
365+ if (change) modes[rel] = change;
366+ continue;
367+ }
349368 const d = onDisk(absolute(root, rel), was);
350369 if (d === undefined) continue;
351370 if (d.text === undefined) {
@@ -367,11 +386,13 @@
367386 if (all || knownBlobs.get(rel) !== b.sha) blobs.set(rel, { sha: b.sha, bytes: b.bytes });
368387 } else {
369388 const text = normalize(d.text);
370− const known = knownPaths.has(rel) ? readKnown(root, rel) : undefined;
389+ const known = knownPaths.has(rel) ? readKnown(root, rel, state) : undefined;
371390 present.add(rel);
372391 // A file the server keeps whole has no known copy, only its hash (#46).
373392 if (all || (known !== text && !(known === undefined && keptWholeAs(text, knownBlobs.get(rel)))))
374393 out.set(rel, text);
394+ if (key && d.link === null && known === text) trusted!.mark(rel, key, d.text.includes("\r"));
395+ else trusted?.drop(rel);
375396 }
376397 // --all sends every file's whole mode, as it sends every file's text.
377398 const change = all ? { exec: d.exec, link: d.link } : modeChange(was, d);
--- a/client/src/status-commands.ts
+++ b/client/src/status-commands.ts
@@ -8,6 +8,7 @@
88import { bool, type Parsed, UsageError } from "./args.ts";
99import { readKnownBlobs, writeKnownBlobs } from "./blobs.ts";
1010import { ciText } from "./ci.ts";
11+import { CleanFiles } from "./clean-files.ts";
1112import { newerCommitNote } from "./client-version.ts";
1213import { type Env, heard, type Output, open, plural } from "./command-env.ts";
1314import {
@@ -26,6 +27,7 @@
2627 absolute,
2728 forgetKnown,
2829 isLocalOnly,
30+ knownPath,
2931 listKnown,
3032 newFileEndings,
3133 normalize,
@@ -68,13 +70,24 @@
6870 * line endings (#37: Python's text mode on Windows writes CRLF).
6971 * abso records them, and abso run sends them, with LF; a tool run here sees
7072 * CRLF. Files git would check out with CRLF are left out of the list.
73+ * With `clean` (status), a file found unchanged before is answered without reading it.
7174 */
72−export function crlfOnly(root: string): string[] {
75+export function crlfOnly(root: string, clean?: CleanFiles): string[] {
7376 const out: string[] = [];
77+ const state = stateDir(root);
7478 for (const rel of listKnown(root)) {
79+ const key = clean?.key(absolute(root, rel), knownPath(root, rel, state));
80+ const was = key && clean!.check(rel, key);
81+ if (was) {
82+ if (was.cr) out.push(rel);
83+ continue;
84+ }
7585 const disk = readText(absolute(root, rel));
7686 if (disk === undefined || !disk.includes("\r")) continue;
77− if (normalize(disk) === readKnown(root, rel)) out.push(rel);
87+ if (normalize(disk) === readKnown(root, rel, state)) {
88+ out.push(rel);
89+ if (key) clean!.mark(rel, key, true);
90+ }
7891 }
7992 const git = newFileEndings(root, out);
8093 return out.filter((rel) => git.get(rel) !== "crlf").sort();
@@ -269,9 +282,11 @@
269282 const { root, config, api } = open(env);
270283 const short = bool(args, "short");
271284 const { files: view, ...status } = short ? { ...(await api.status()), files: undefined } : await api.statusAndFiles();
272− const pending = unrecorded(root);
285+ const clean = CleanFiles.load(root);
286+ const pending = unrecorded(root, false, new Set(), clean);
273287 const local = new Set([...pending.files.keys(), ...pending.blobs.keys(), ...Object.keys(pending.modes)]);
274288 if (short) {
289+ clean.save();
275290 // One word to stamp a build with (#37): 214, coast@3, 214+unpublished.
276291 const me = status.agents.find((a) => a.agent === config.agent);
277292 const unpublished = local.size > 0 || (me?.draftChanges ?? 0) > 0;
@@ -281,7 +296,8 @@
281296 }
282297 const missing = view === undefined ? [] : missingHere(root, view.files, local);
283298 const unwritten = readUnwritten(root);
284− const crlf = crlfOnly(root);
299+ const crlf = crlfOnly(root, clean);
300+ clean.save();
285301 // Files an earlier record added to the draft and that are gone again (#37: scratch files a run
286302 // recorded, deleted after): nothing reached shared, and the next record takes them out of the draft.
287303 const deleted = [...pending.files].filter(([, text]) => text === null).map(([rel]) => rel);
--- a/client/test/client.test.ts
+++ b/client/test/client.test.ts
@@ -46,7 +46,7 @@
4646 uncomposedText,
4747} from "../src/core.ts";
4848import { unifiedDiff } from "../src/diff.ts";
49−import { absolute, writeFileAtomic } from "../src/folder.ts";
49+import { absolute, knownPath, unrecorded, writeFileAtomic } from "../src/folder.ts";
5050import { runHook } from "../src/hook.ts";
5151import { tryFolderLock } from "../src/lock.ts";
5252import { boxServer, commandTable, main } from "../src/main.ts";
@@ -3495,6 +3495,72 @@
34953495 assert.deepEqual(after.json.unrecorded, []);
34963496 });
34973497
3498+ it("status skips reading files it found unchanged before, never trusts one changed too recently, and record still reads every file", async () => {
3499+ const { folders } = await project({ "a.txt": "a\n", "b.txt": "b\n", "c.txt": "c\r\n" }, "alice", "bob");
3500+ const root = folders.alice!;
3501+ const record = join(stateDir(root), "clean.json");
3502+ const old = new Date(Date.now() - 60_000);
3503+ // The file and its known copy a minute old, as after a quiet spell.
3504+ const age = (...rels: string[]) => {
3505+ for (const rel of rels) {
3506+ utimesSync(join(root, rel), old, old);
3507+ utimesSync(knownPath(root, rel), old, old);
3508+ }
3509+ };
3510+ // Changes a file without changing its size or time: only reading it can tell.
3511+ const swap = (rel: string, text: string) => {
3512+ const was = statSync(join(root, rel));
3513+ writeFileSync(join(root, rel), text);
3514+ utimesSync(join(root, rel), was.atime, was.mtime);
3515+ };
3516+ writeFileSync(join(root, "c.txt"), "c\r\n");
3518+ // Files too new to trust are read, and not kept: a swap is still caught. A time a minute ahead stays
3519+ // too new however long the run takes.
3520+ const soon = new Date(Date.now() + 60_000);
3521+ for (const rel of ["a.txt", "b.txt", "c.txt"]) utimesSync(join(root, rel), soon, soon);
3522+ assert.deepEqual((await avcs(root, "status")).json.unrecorded, []);
3523+ swap("a.txt", "A\n");
3524+ assert.deepEqual((await avcs(root, "status")).json.unrecorded, ["a.txt"]);
3525+ swap("a.txt", "a\n");
3527+ // Old files are kept after one status, and the next answers from the record without reading them.
3528+ age("a.txt", "b.txt", "c.txt");
3529+ const first = await avcs(root, "status");
3530+ assert.deepEqual(first.json.unrecorded, []);
3531+ assert.deepEqual(first.json.crlf, ["c.txt"]);
3532+ assert.deepEqual(Object.keys(JSON.parse(readFileSync(record, "utf8")).files).sort(), ["a.txt", "b.txt", "c.txt"]);
3533+ swap("b.txt", "B\n");
3534+ const second = await avcs(root, "status");
3535+ assert.deepEqual(second.json.unrecorded, [], "b.txt was answered from the record");
3536+ assert.deepEqual(second.json.crlf, ["c.txt"]);
3537+ // What record sends is read from every file, whatever the record says.
3538+ assert.deepEqual([...unrecorded(root).files.keys()], ["b.txt"]);
3540+ // A record of an unknown format, or one that doesn't parse, is not used.
3541+ const kept = JSON.parse(readFileSync(record, "utf8"));
3542+ writeFileSync(record, JSON.stringify({ ...kept, format: 2 }));
3543+ assert.deepEqual((await avcs(root, "status")).json.unrecorded, ["b.txt"]);
3544+ writeFileSync(record, "not json");
3545+ assert.deepEqual((await avcs(root, "status")).json.unrecorded, ["b.txt"]);
3547+ // An edit that changes the size or time is read at once.
3548+ swap("b.txt", "b\n");
3549+ age("b.txt");
3550+ assert.deepEqual((await avcs(root, "status")).json.unrecorded, []);
3551+ writeFileSync(join(root, "b.txt"), "bb\n");
3552+ assert.deepEqual((await avcs(root, "status")).json.unrecorded, ["b.txt"]);
3554+ // A sync writes the file and its known copy, so what was kept for them no longer holds.
3555+ writeFileSync(join(root, "b.txt"), "b\n");
3556+ write(folders.bob!, { "a.txt": "a2\n" });
3557+ assert.equal((await avcs(folders.bob!, "publish", "-m", "a2")).exit, 0);
3558+ assert.equal((await avcs(root, "sync")).exit, 0);
3559+ assert.equal(readFileSync(join(root, "a.txt"), "utf8"), "a2\n");
3560+ swap("a.txt", "a3\n");
3561+ assert.deepEqual((await avcs(root, "status")).json.unrecorded, ["a.txt"]);
3562+ });
34983564 it("several files marked against one version get one settle --theirs hint", () => {
34993565 const c = (file: string, version: number) => ({
35003566 kind: "lines" as const,
--- a/client/test/install.test.ts
+++ b/client/test/install.test.ts
@@ -337,7 +337,10 @@
337337 assert.equal(keptModCommit(spec, home), "fffffff", "a kept answer is used while nothing moved");
338338
339339 // A file changed in the same moment the answer was kept is never trusted, even when its size and time match.
340+ // A time a minute ahead stays too new however long the run takes.
340341 write(spec.from, "src/core.ts", "export const core = 2\n");
342+ const soon = new Date(Date.now() + 60_000);
343+ utimesSync(at(spec.from, "src/core.ts"), soon, soon);
341344 assert.equal(keptModCommit(spec, home), `${mod}.dirty`);
342345 plant();
343346 assert.equal(keptModCommit(spec, home), `${mod}.dirty`, "too new to trust: git runs again");