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 11 to 318

Compare

+196 −47 in 15 places
Lines 7–13 at 318 (were 7–12 at 11)
77// The engine's own key, so both sides match spellings the same way (#49). Not in core.ts: the edit mod installs core.ts alone.
88import {
99 type Api,
10+ ApiError,
1011 type Collision,
1112 type FileChange,
1213 type MergedLine,
Lines 18–24 at 318 (were 17–23 at 11)
1718import { bool, type Parsed, str, UsageError } from "./args.ts";
1819import { blobOf, keptWholeAs, MAX_BLOB_BYTES, readKnownBlobs, writeKnownBlobs } from "./blobs.ts";
1920import { type Env, EXIT, heard, type Output, open } from "./command-env.ts";
20−import { AVCS_DIR, findRoot, isReader } from "./config.ts";
21+import { findRoot, isReader } from "./config.ts";
2122import { spellingText, uncomposedText } from "./core.ts";
2223import { pairRenames } from "./diff.ts";
2324import type { BlobToSend, TurnedBinary } from "./folder.ts";
Lines 29–34 at 318 (were 28–34 at 11)
2829 isPaused,
2930 type LeftOut,
3031 listKnown,
31− MAX_TEXT_BYTES,
3232 newFileEndings,
3333 normalize,
3434 pausedPath,
Lines 42–48 at 318 (were 42–47 at 11)
4242import { withFolderLock } from "./lock.ts";
4343import type { OnDisk } from "./modes.ts";
4444import { applyMode, type FullMode, foldModes, onDisk, PLAIN, readKnownModes, writeKnownModes } from "./modes.ts";
45+import { batchRecordItems, sendRecordBatches } from "./record-batches.ts";
4546import { type Refused, screenAnswer, unsafePath } from "./safety.ts";
4647
4748export {
Lines 76–84 at 318 (were 75–80 at 11)
7576 staleModText,
7677} from "./status-commands.ts";
7778
79+import { gitMarkerLine } from "./settle.ts";
80+import { stateDir } from "./state-dir.ts";
7882// --- record ---
7983
8084interface Recorded {
Lines 87–93 at 318 (were 83–89 at 11)
8387 readonly paths: readonly string[];
8488 /** Those of them sent as deleted (gone from the folder). */
8589 readonly deleted: readonly string[];
86− /** Those of them new to avcs: no copy recorded before (#37, Builder 7: a file a script made is easy to miss). */
90+ /** Those of them new to avcs: no copy recorded before (#37: a file a script made is easy to miss). */
8791 readonly added?: readonly string[];
8892 /** Nothing was sent because the folder is paused (abso pause). */
8993 readonly paused?: boolean;
Lines 95–104 at 318 (were 91–96 at 11)
9195 readonly news: News;
9296 readonly leftOut: readonly LeftOut[];
9397 readonly turnedBinary: readonly TurnedBinary[];
98+ /** Text files sent that hold what looks like a git conflict (a merge git left unfinished, or markers typed by hand). */
99+ readonly gitMarkers?: readonly { path: string; line: number }[];
100+ /** Sent files whose path is long enough to fail on Windows, here or in another agent's folder. */
101+ readonly longPaths?: readonly { path: string; length: number }[];
94102}
95103
96104/** The warning for text files that turned binary: what, where, and what to do. Empty when there are none. */
Lines 215–358 at 318 (were 207–262 at 11)
207215 .map(([rel]) => rel)
208216 .sort();
209217 if (sent === 0) return { sent: 0, paths, deleted, changed: [], news: undefined, leftOut, turnedBinary };
210− await uploadBlobs(api, blobs);
211− const answer = await api.record(
212− Object.fromEntries(files),
213− {},
214− modes,
215− Object.fromEntries([...blobs].map(([rel, b]) => [rel, b.sha])),
218+ const items = paths.map((path) => ({
219+ path,
220+ ...(files.has(path) ? { text: files.get(path) } : {}),
221+ ...(modes[path] !== undefined ? { mode: modes[path] } : {}),
222+ ...(blobs.has(path) ? { blob: blobs.get(path)?.sha } : {}),
223+ }));
224+ const batches = batchRecordItems(items);
225+ let batchBlobs = new Map<string, BlobToSend>();
226+ const uploaded = new Set<string>();
227+ const answers = await sendRecordBatches(
228+ api,
229+ batches,
230+ async (batch) => {
231+ batchBlobs = new Map(
232+ batch.paths.flatMap((path) => {
233+ const blob = blobs.get(path);
234+ return blob === undefined ? [] : [[path, blob] as const];
235+ }),
236+ );
237+ for (const [path, blob] of batchBlobs) {
238+ if (uploaded.has(blob.sha)) continue;
239+ await uploadBlobs(api, new Map([[path, blob]]));
240+ uploaded.add(blob.sha);
241+ }
242+ },
243+ async (batch, answer) => {
244+ const batchFiles = new Map(Object.entries(batch.files));
245+ // Commit known state only after this batch is confirmed; earlier successful batches stay committed on later failure.
246+ for (const [rel, text] of batchFiles) if (text === null) forgetKnown(root, rel);
247+ for (const [rel, text] of batchFiles) if (text !== null) writeKnown(root, rel, text);
248+ const knownBlobs = readKnownBlobs(root);
249+ if (Object.keys(batch.blobs).length > 0 || batch.paths.some((rel) => knownBlobs.has(rel))) {
250+ for (const rel of new Set([...Object.keys(batch.files), ...Object.keys(batch.blobs)])) knownBlobs.delete(rel);
251+ for (const [rel, sha] of Object.entries(batch.blobs)) {
252+ forgetKnown(root, rel);
253+ knownBlobs.set(rel, sha);
254+ }
255+ writeKnownBlobs(root, knownBlobs);
256+ }
257+ const gone = Object.entries(batch.files)
258+ .filter(([, text]) => text === null)
259+ .map(([rel]) => rel);
260+ const knownModes = readKnownModes(root);
261+ if (Object.keys(batch.modes).length > 0 || gone.some((rel) => knownModes.has(rel))) {
262+ foldModes(knownModes, batch.modes, gone);
263+ writeKnownModes(root, knownModes);
264+ }
265+ heard(root, [
266+ ...newsLines(answer.news),
267+ ...(answer.spelling ?? []).map(spellingText),
268+ ...(answer.uncomposed ?? []).map(uncomposedText),
269+ ]);
270+ },
216271 );
217− if (!answer.ok) throw new Error(`the server refused the record: ${answer.reason ?? "no reason given"}`);
218− // Forget before writing: after a rename that changes only case, known/a.ts and known/A.ts are one file on Windows and macOS.
219− for (const [rel, text] of files) if (text === null) forgetKnown(root, rel);
220− for (const [rel, text] of files) if (text !== null) writeKnown(root, rel, text);
221− const knownBlobs = readKnownBlobs(root);
222− if (blobs.size > 0 || [...files.keys()].some((rel) => knownBlobs.has(rel))) {
223− for (const rel of files.keys()) knownBlobs.delete(rel);
224− for (const [rel, b] of blobs) {
225− forgetKnown(root, rel);
226− knownBlobs.set(rel, b.sha);
227− }
228− writeKnownBlobs(root, knownBlobs);
229− }
230− const gone = [...files].filter(([, text]) => text === null).map(([rel]) => rel);
231− const knownModes = readKnownModes(root);
232− if (Object.keys(modes).length > 0 || gone.some((rel) => knownModes.has(rel))) {
233− foldModes(knownModes, modes, gone);
234− writeKnownModes(root, knownModes);
235− }
236− // Another spelling of an existing file went in as that file (#49); a new name not in composed form gets a gentle word.
237− heard(root, [
238− ...newsLines(answer.news),
239− ...(answer.spelling ?? []).map(spellingText),
240− ...(answer.uncomposed ?? []).map(uncomposedText),
241− ]);
242− return { sent, paths, deleted, added, changed: answer.changed, news: answer.news, leftOut, turnedBinary };
272+ const changed = answers.flatMap((answer) => (Array.isArray(answer.changed) ? answer.changed : []));
273+ const changedCount = answers.some((answer) => typeof answer.changed === "number")
274+ ? answers.reduce(
275+ (sum, answer) =>
276+ sum +
277+ (typeof answer.changed === "number"
278+ ? answer.changed
279+ : Array.isArray(answer.changed)
280+ ? answer.changed.length
281+ : 0),
282+ 0,
283+ )
284+ : changed;
285+ const news = answers.flatMap((answer) => newsLines(answer.news));
286+ const spelling = answers.flatMap((answer) => answer.spelling ?? []);
287+ const uncomposed = answers.flatMap((answer) => answer.uncomposed ?? []);
288+ const answer = { changed: changedCount, news, spelling, uncomposed };
289+ const longPaths = longPathsOf(
290+ root,
291+ [...files].filter(([, text]) => text !== null).map(([path]) => path),
292+ );
293+ const gitMarkers = [...files]
294+ .flatMap(([path, text]) => {
295+ const line = text === null ? undefined : gitMarkerLine(text);
296+ return line === undefined ? [] : [{ path, line }];
297+ })
298+ .sort((a, b) => (a.path < b.path ? -1 : 1));
299+ return {
300+ sent,
301+ paths,
302+ deleted,
303+ added,
304+ changed: answer.changed,
305+ news: answer.news,
306+ leftOut,
307+ turnedBinary,
308+ gitMarkers,
309+ longPaths,
310+ };
243311}
244312
313+/** A note per file that holds what looks like a git conflict: abso records it as it is, as text, and does not call it a collision. */
314+export function gitMarkerText(list: readonly { path: string; line: number }[] | undefined): string[] {
315+ return (list ?? []).map(
316+ (m) =>
317+ `Note: ${m.path} has lines that look like a git conflict (from line ${m.line}). abso records them as ordinary text and will push them as they are. If a merge was left unfinished, fix the file first.`,
318+ );
319+}
321+/** Windows stops at 260 characters for a whole path, and another agent's folder may sit deeper than this one. */
322+const LONG_FULL_PATH = 240;
323+const LONG_RELATIVE_PATH = 160;
325+/** The sent files whose path will likely fail to write on Windows: the whole path is near its limit here, or the part inside the folder is already long. */
326+export function longPathsOf(root: string, paths: Iterable<string>): { path: string; length: number }[] {
327+ return [...paths]
328+ .map((path) => ({ path, length: absolute(root, path).length }))
329+ .filter((p) => p.length >= LONG_FULL_PATH || p.path.length >= LONG_RELATIVE_PATH)
330+ .sort((a, b) => (a.path < b.path ? -1 : 1));
331+}
333+/** A note per long path: abso keeps it, but a Windows agent may not be able to write the file. */
334+export function longPathText(list: readonly { path: string; length: number }[] | undefined): string[] {
335+ return (list ?? []).map(
336+ (p) =>
337+ `Note: ${p.path} is ${p.path.length} characters inside the folder (${p.length} with this folder's location). Windows stops at 260 for a whole path, so an agent on Windows may be unable to write it. A shorter path avoids that.`,
338+ );
339+}
245341/** "Recorded 3 files first: a, b, c." (names cut short past 8), or a word that the folder is paused. Empty when nothing was sent. */
246342export function recordedFirst(r: Recorded): string[] {
247343 if (r.paused) return ["Recording is paused (abso resume): your folder's changes were not sent first."];
248344 if (r.sent === 0) return [];
249− return [`Recorded first: ${recordedList(r)}.`];
345+ return [`Recorded first: ${recordedList(r)}.`, ...gitMarkerText(r.gitMarkers), ...longPathText(r.longPaths)];
250346}
251347
252−/** "2 changed (a, b), 3 deleted (x, y, z)": deletions said as such (#37, Builder 2 read its rm'd files as edits). */
348+/** "2 changed (a, b), 3 deleted (x, y, z)": deletions said as such (#37: deleted files were read as edits). */
253349function recordedList(r: Recorded): string {
254350 const names = (list: readonly string[]) =>
255351 list.length <= LISTED ? list.join(", ") : `${list.slice(0, LISTED).join(", ")}, and ${list.length - LISTED} more`;
256352 const gone = new Set(r.deleted);
257353 const fresh = new Set(r.added ?? []);
258354 const changed = r.paths.filter((p) => !gone.has(p) && !fresh.has(p));
259− // New files are named in full: they are the ones a script made and nobody remembers (#37, Builder 7).
355+ // New files are named in full: they are the ones a script made and nobody remembers (#37).
260356 return [
261357 ...(fresh.size > 0 ? [`${fresh.size} new (${[...fresh].join(", ")})`] : []),
262358 ...(changed.length > 0 ? [`${changed.length} changed (${names(changed)})`] : []),
Lines 361–367 at 318 (were 265–271 at 11)
265361}
266362
267363export const READER_REFUSES =
268− "this folder is a reader: it records and publishes nothing (abso reader off makes it a writer again)";
364+ "this folder is a reader: it records and pushes nothing (abso reader off makes it a writer again)";
269365
270366/** abso pause: send nothing from the folder (no hook, no record) until abso resume. */
271367export async function runPause(_args: Parsed, env: Env): Promise<Output> {
Lines 380–386 at 318 (were 284–290 at 11)
284380 rmSync(pausedPath(root), { force: true });
285381 return {
286382 text: was
287− ? "Recording again. Changes made while paused go at the next abso record (or publish, sync, ...); abso status lists them."
383+ ? "Recording again. Changes made while paused go at the next abso record (or push, pull, ...); abso status lists them."
288384 : "Recording was not paused.",
289385 json: { paused: false },
290386 };
Lines 403–423 at 318 (were 307–319 at 11)
307403 : `Recorded ${recordedList(r)}${changed === undefined ? "" : `; ${changed} changed on the server`}.`;
308404 return withLeftOut(
309405 {
310− text: [...turnedBinaryText(r.turnedBinary), text, ...leftOutText(r.leftOut)].join("\n"),
406+ text: [
407+ ...turnedBinaryText(r.turnedBinary),
408+ text,
409+ ...gitMarkerText(r.gitMarkers),
410+ ...longPathText(r.longPaths),
411+ ...leftOutText(r.leftOut),
412+ ].join("\n"),
311413 json: {
312414 sent: r.sent,
313415 changed: r.changed ?? null,
314416 news: newsLines(r.news),
315417 leftOut: r.leftOut,
316418 turnedBinary: r.turnedBinary,
419+ gitMarkers: r.gitMarkers ?? [],
420+ longPaths: r.longPaths ?? [],
317421 },
318422 },
319423 r.leftOut,
Lines 432–485 at 318 (were 328–336 at 11)
328432 * overwritten.
329433 */
330434export async function syncFolder(root: string, api: Api, full = false): Promise<Applied> {
331− return applyAnswer(root, api, await api.sync({ full }));
435+ const applied = await applyAnswer(root, api, await pullAnswer(root, api, full));
436+ pullWritten(root);
437+ return applied;
332438}
333439
440+const PULLING_FILE = "pulling.json";
442+/**
443+ * Whether a pull here asked the server and never wrote its answer (abso-core 26): it timed out, or stopped
444+ * partway. The server moved the agent's base when it answered, and later pulls send only what changed since
445+ * that base, so the files it would have written stay behind until a pull asks for every file.
446+ */
447+export function pullUnfinished(root: string): boolean {
448+ return existsSync(join(stateDir(root), PULLING_FILE));
449+}
451+/**
452+ * Ask the server for a pull, noting first that one is under way, so a pull whose answer never gets written
453+ * leaves the note behind. With that note, or `full`, it asks for every file in the agent's view, which the
454+ * folder's own checks then write only where the folder differs. pullWritten takes the note away.
455+ */
456+export async function pullAnswer(root: string, api: Api, full = false): Promise<SyncAnswer> {
457+ const unfinished = pullUnfinished(root);
458+ writeFileAtomic(join(stateDir(root), PULLING_FILE), `${JSON.stringify({ at: new Date().toISOString() })}\n`);
459+ if (!full && !unfinished) return api.sync();
460+ try {
461+ return await api.sync({ full: true });
462+ } catch (err) {
463+ // A server from before full pulls (501): the plain pull is still better than none.
464+ if (unfinished && !full && err instanceof ApiError && err.status === 501) return api.sync();
465+ throw err;
466+ }
467+}
469+/** The pull's answer is written into the folder: take away pullAnswer's note. */
470+export function pullWritten(root: string): void {
471+ rmSync(join(stateDir(root), PULLING_FILE), { force: true });
472+}
474+/** Status's line, and pull's when it caught up. */
475+export function pullUnfinishedText(root: string): string[] {
476+ return pullUnfinished(root)
477+ ? [
478+ "A pull here didn't finish (its answer never arrived or wasn't written), so files may be behind this folder's base. abso pull fetches every file and writes the ones that differ.",
479+ ]
480+ : [];
481+}
334483/** What writing a sync or switch answer into the folder did. */
335484export interface Applied {
336485 readonly version: number;
Lines 547–553 at 318 (were 398–404 at 11)
398547/** Files earlier syncs couldn't write here, kept in .avcs/unwritten.json until a write of them works. */
399548export function readUnwritten(root: string): Record<string, Unwritten> {
400549 try {
401− return JSON.parse(readFileSync(join(root, AVCS_DIR, UNWRITTEN_FILE), "utf8")) as Record<string, Unwritten>;
550+ return JSON.parse(readFileSync(join(stateDir(root), UNWRITTEN_FILE), "utf8")) as Record<string, Unwritten>;
402551 } catch {
403552 return {};
404553 }
Lines 564–584 at 318 (were 415–435 at 11)
415564 if (paths.length === 0) return [];
416565 const list = paths.map((p) => `${p} (version ${unwritten[p]!.version}: ${unwritten[p]!.why})`).join(", ");
417566 return [
418− `Stale here: a sync couldn't write ${paths.length === 1 ? "this file" : "these files"}, and no later sync sends ${paths.length === 1 ? "it" : "them"} again. Close whatever holds ${paths.length === 1 ? "it" : "them"}, then abso sync --full: ${list}`,
567+ `Stale here: a pull couldn't write ${paths.length === 1 ? "this file" : "these files"}, and no later pull sends ${paths.length === 1 ? "it" : "them"} again. Close whatever holds ${paths.length === 1 ? "it" : "them"}, then abso pull --full: ${list}`,
419568 ];
420569}
421570
422571/** Write the files a sync or switch answer lists into the folder (the same rules as sync). */
423572/** Write a sync's answer into the folder, holding its lock (#51). */
424573export async function applyAnswer(root: string, api: Api, unscreened: SyncAnswer): Promise<Applied> {
425− return withFolderLock(root, "sync", () => applyAnswerLocked(root, api, unscreened));
574+ return withFolderLock(root, "pull", () => applyAnswerLocked(root, api, unscreened));
426575}
427576
428577async function applyAnswerLocked(root: string, api: Api, unscreened: SyncAnswer): Promise<Applied> {
429578 // Other agents' paths: nothing outside this folder, in .git/.claude/.avcs, or through a link.
430579 const { answer, refused } = screenAnswer(root, unscreened);
431580 // A write the disk refuses anyway is named and skipped, so the rest of the sync still lands with its
432− // known copies and modes, rather than stopping partway (AVCS - Git Lessons; git checks every path first).
581+ // known copies and modes, rather than stopping partway (git checks every path first).
433582 const failed = new Set<string>();
434583 // Remembered until a write works (#51): the server won't send these files again.
435584 const unwritten = readUnwritten(root);
Lines 712–718 at 318 (were 563–569 at 11)
563712 .map((f) => ({ from: f.from!, to: f.path }))
564713 : [];
565714 if (JSON.stringify(unwritten) !== unwrittenBefore)
566− writeFileAtomic(join(root, AVCS_DIR, UNWRITTEN_FILE), `${JSON.stringify(unwritten, null, 1)}\n`);
715+ writeFileAtomic(join(stateDir(root), UNWRITTEN_FILE), `${JSON.stringify(unwritten, null, 1)}\n`);
567716 return {
568717 version: answer.version,
569718 written,
MCP
compare on this site's /mcp
CLI
abso diff 11 318 client/src/commands.ts --json
JSON
/christopher/abso-core/compare/11..318?file=client%2Fsrc%2Fcommands.ts&format=json