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

Versions 43 to 44

Compare

+134 −0 in 1 place
new file, 134 lines
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+}
MCP
compare on this site's /mcp
CLI
abso diff 43 44 client/src/clean-files.ts --json
JSON
/christopher/abso-core/compare/43..44?file=client%2Fsrc%2Fclean-files.ts&format=json