{"draft":null,"from":4,"to":318,"files":[{"path":"client/src/commands.ts","added":222,"removed":55,"diff":"--- a/client/src/commands.ts\n+++ b/client/src/commands.ts\n@@ -7,6 +7,7 @@\n // 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.\n import {\n \ttype Api,\n+\tApiError,\n \ttype Collision,\n \ttype FileChange,\n \ttype MergedLine,\n@@ -17,7 +18,7 @@\n import { bool, type Parsed, str, UsageError } from \"./args.ts\";\n import { blobOf, keptWholeAs, MAX_BLOB_BYTES, readKnownBlobs, writeKnownBlobs } from \"./blobs.ts\";\n import { type Env, EXIT, heard, type Output, open } from \"./command-env.ts\";\n-import { AVCS_DIR, findRoot, isReader } from \"./config.ts\";\n+import { findRoot, isReader } from \"./config.ts\";\n import { spellingText, uncomposedText } from \"./core.ts\";\n import { pairRenames } from \"./diff.ts\";\n import type { BlobToSend, TurnedBinary } from \"./folder.ts\";\n@@ -28,7 +29,6 @@\n \tisPaused,\n \ttype LeftOut,\n \tlistKnown,\n-\tMAX_TEXT_BYTES,\n \tnewFileEndings,\n \tnormalize,\n \tpausedPath,\n@@ -42,6 +42,7 @@\n import { withFolderLock } from \"./lock.ts\";\n import type { OnDisk } from \"./modes.ts\";\n import { applyMode, type FullMode, foldModes, onDisk, PLAIN, readKnownModes, writeKnownModes } from \"./modes.ts\";\n+import { batchRecordItems, sendRecordBatches } from \"./record-batches.ts\";\n import { type Refused, screenAnswer, unsafePath } from \"./safety.ts\";\n \n export {\n@@ -75,6 +76,9 @@\n \tstaleModText,\n } from \"./status-commands.ts\";\n \n+import { gitMarkerLine } from \"./settle.ts\";\n+import { stateDir } from \"./state-dir.ts\";\n+\n // --- record ---\n \n interface Recorded {\n@@ -83,7 +87,7 @@\n \treadonly paths: readonly string[];\n \t/** Those of them sent as deleted (gone from the folder). */\n \treadonly deleted: readonly string[];\n-\t/** Those of them new to avcs: no copy recorded before (#37, Builder 7: a file a script made is easy to miss). */\n+\t/** Those of them new to avcs: no copy recorded before (#37: a file a script made is easy to miss). */\n \treadonly added?: readonly string[];\n \t/** Nothing was sent because the folder is paused (abso pause). */\n \treadonly paused?: boolean;\n@@ -91,6 +95,10 @@\n \treadonly news: News;\n \treadonly leftOut: readonly LeftOut[];\n \treadonly turnedBinary: readonly TurnedBinary[];\n+\t/** Text files sent that hold what looks like a git conflict (a merge git left unfinished, or markers typed by hand). */\n+\treadonly gitMarkers?: readonly { path: string; line: number }[];\n+\t/** Sent files whose path is long enough to fail on Windows, here or in another agent's folder. */\n+\treadonly longPaths?: readonly { path: string; length: number }[];\n }\n \n /** The warning for text files that turned binary: what, where, and what to do. Empty when there are none. */\n@@ -120,6 +128,21 @@\n \t];\n }\n \n+/**\n+ * A command that left files out for size did not do everything it was asked: say so in the exit code too, so\n+ * a script, CI or an agent cannot read \"ok\" while a file is missing. Other exits (refused, queued) stay as they are.\n+ */\n+export function withLeftOut(out: Output, leftOut: readonly LeftOut[]): Output {\n+\tif (leftOut.length === 0) return out;\n+\tconst text = out.text.includes(\"Left out,\") ? out.text : [out.text, ...leftOutText(leftOut)].join(\"\\n\");\n+\treturn {\n+\t\t...out,\n+\t\ttext: `${text}\\nExit ${EXIT.error}: ${leftOut.length} file${leftOut.length === 1 ? \" was\" : \"s were\"} not sent.`,\n+\t\tjson: { ...out.json, leftOut },\n+\t\texit: out.exit === undefined || out.exit === EXIT.ok ? EXIT.error : out.exit,\n+\t};\n+}\n+\n /** Upload binary files' bytes before the record or init that names them, each hash once. */\n export async function uploadBlobs(api: Api, blobs: ReadonlyMap<string, BlobToSend>): Promise<void> {\n \tconst seen = new Set<string>();\n@@ -192,56 +215,144 @@\n \t\t.map(([rel]) => rel)\n \t\t.sort();\n \tif (sent === 0) return { sent: 0, paths, deleted, changed: [], news: undefined, leftOut, turnedBinary };\n-\tawait uploadBlobs(api, blobs);\n-\tconst answer = await api.record(\n-\t\tObject.fromEntries(files),\n-\t\t{},\n-\t\tmodes,\n-\t\tObject.fromEntries([...blobs].map(([rel, b]) => [rel, b.sha])),\n+\tconst items = paths.map((path) => ({\n+\t\tpath,\n+\t\t...(files.has(path) ? { text: files.get(path) } : {}),\n+\t\t...(modes[path] !== undefined ? { mode: modes[path] } : {}),\n+\t\t...(blobs.has(path) ? { blob: blobs.get(path)?.sha } : {}),\n+\t}));\n+\tconst batches = batchRecordItems(items);\n+\tlet batchBlobs = new Map<string, BlobToSend>();\n+\tconst uploaded = new Set<string>();\n+\tconst answers = await sendRecordBatches(\n+\t\tapi,\n+\t\tbatches,\n+\t\tasync (batch) => {\n+\t\t\tbatchBlobs = new Map(\n+\t\t\t\tbatch.paths.flatMap((path) => {\n+\t\t\t\t\tconst blob = blobs.get(path);\n+\t\t\t\t\treturn blob === undefined ? [] : [[path, blob] as const];\n+\t\t\t\t}),\n+\t\t\t);\n+\t\t\tfor (const [path, blob] of batchBlobs) {\n+\t\t\t\tif (uploaded.has(blob.sha)) continue;\n+\t\t\t\tawait uploadBlobs(api, new Map([[path, blob]]));\n+\t\t\t\tuploaded.add(blob.sha);\n+\t\t\t}\n+\t\t},\n+\t\tasync (batch, answer) => {\n+\t\t\tconst batchFiles = new Map(Object.entries(batch.files));\n+\t\t\t// Commit known state only after this batch is confirmed; earlier successful batches stay committed on later failure.\n+\t\t\tfor (const [rel, text] of batchFiles) if (text === null) forgetKnown(root, rel);\n+\t\t\tfor (const [rel, text] of batchFiles) if (text !== null) writeKnown(root, rel, text);\n+\t\t\tconst knownBlobs = readKnownBlobs(root);\n+\t\t\tif (Object.keys(batch.blobs).length > 0 || batch.paths.some((rel) => knownBlobs.has(rel))) {\n+\t\t\t\tfor (const rel of new Set([...Object.keys(batch.files), ...Object.keys(batch.blobs)])) knownBlobs.delete(rel);\n+\t\t\t\tfor (const [rel, sha] of Object.entries(batch.blobs)) {\n+\t\t\t\t\tforgetKnown(root, rel);\n+\t\t\t\t\tknownBlobs.set(rel, sha);\n+\t\t\t\t}\n+\t\t\t\twriteKnownBlobs(root, knownBlobs);\n+\t\t\t}\n+\t\t\tconst gone = Object.entries(batch.files)\n+\t\t\t\t.filter(([, text]) => text === null)\n+\t\t\t\t.map(([rel]) => rel);\n+\t\t\tconst knownModes = readKnownModes(root);\n+\t\t\tif (Object.keys(batch.modes).length > 0 || gone.some((rel) => knownModes.has(rel))) {\n+\t\t\t\tfoldModes(knownModes, batch.modes, gone);\n+\t\t\t\twriteKnownModes(root, knownModes);\n+\t\t\t}\n+\t\t\theard(root, [\n+\t\t\t\t...newsLines(answer.news),\n+\t\t\t\t...(answer.spelling ?? []).map(spellingText),\n+\t\t\t\t...(answer.uncomposed ?? []).map(uncomposedText),\n+\t\t\t]);\n+\t\t},\n \t);\n-\tif (!answer.ok) throw new Error(`the server refused the record: ${answer.reason ?? \"no reason given\"}`);\n-\t// Forget before writing: after a rename that changes only case, known/a.ts and known/A.ts are one file on Windows and macOS.\n-\tfor (const [rel, text] of files) if (text === null) forgetKnown(root, rel);\n-\tfor (const [rel, text] of files) if (text !== null) writeKnown(root, rel, text);\n-\tconst knownBlobs = readKnownBlobs(root);\n-\tif (blobs.size > 0 || [...files.keys()].some((rel) => knownBlobs.has(rel))) {\n-\t\tfor (const rel of files.keys()) knownBlobs.delete(rel);\n-\t\tfor (const [rel, b] of blobs) {\n-\t\t\tforgetKnown(root, rel);\n-\t\t\tknownBlobs.set(rel, b.sha);\n-\t\t}\n-\t\twriteKnownBlobs(root, knownBlobs);\n-\t}\n-\tconst gone = [...files].filter(([, text]) => text === null).map(([rel]) => rel);\n-\tconst knownModes = readKnownModes(root);\n-\tif (Object.keys(modes).length > 0 || gone.some((rel) => knownModes.has(rel))) {\n-\t\tfoldModes(knownModes, modes, gone);\n-\t\twriteKnownModes(root, knownModes);\n-\t}\n-\t// Another spelling of an existing file went in as that file (#49); a new name not in composed form gets a gentle word.\n-\theard(root, [\n-\t\t...newsLines(answer.news),\n-\t\t...(answer.spelling ?? []).map(spellingText),\n-\t\t...(answer.uncomposed ?? []).map(uncomposedText),\n-\t]);\n-\treturn { sent, paths, deleted, added, changed: answer.changed, news: answer.news, leftOut, turnedBinary };\n+\tconst changed = answers.flatMap((answer) => (Array.isArray(answer.changed) ? answer.changed : []));\n+\tconst changedCount = answers.some((answer) => typeof answer.changed === \"number\")\n+\t\t? answers.reduce(\n+\t\t\t\t(sum, answer) =>\n+\t\t\t\t\tsum +\n+\t\t\t\t\t(typeof answer.changed === \"number\"\n+\t\t\t\t\t\t? answer.changed\n+\t\t\t\t\t\t: Array.isArray(answer.changed)\n+\t\t\t\t\t\t\t? answer.changed.length\n+\t\t\t\t\t\t\t: 0),\n+\t\t\t\t0,\n+\t\t\t)\n+\t\t: changed;\n+\tconst news = answers.flatMap((answer) => newsLines(answer.news));\n+\tconst spelling = answers.flatMap((answer) => answer.spelling ?? []);\n+\tconst uncomposed = answers.flatMap((answer) => answer.uncomposed ?? []);\n+\tconst answer = { changed: changedCount, news, spelling, uncomposed };\n+\tconst longPaths = longPathsOf(\n+\t\troot,\n+\t\t[...files].filter(([, text]) => text !== null).map(([path]) => path),\n+\t);\n+\tconst gitMarkers = [...files]\n+\t\t.flatMap(([path, text]) => {\n+\t\t\tconst line = text === null ? undefined : gitMarkerLine(text);\n+\t\t\treturn line === undefined ? [] : [{ path, line }];\n+\t\t})\n+\t\t.sort((a, b) => (a.path < b.path ? -1 : 1));\n+\treturn {\n+\t\tsent,\n+\t\tpaths,\n+\t\tdeleted,\n+\t\tadded,\n+\t\tchanged: answer.changed,\n+\t\tnews: answer.news,\n+\t\tleftOut,\n+\t\tturnedBinary,\n+\t\tgitMarkers,\n+\t\tlongPaths,\n+\t};\n }\n \n+/** 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. */\n+export function gitMarkerText(list: readonly { path: string; line: number }[] | undefined): string[] {\n+\treturn (list ?? []).map(\n+\t\t(m) =>\n+\t\t\t`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.`,\n+\t);\n+}\n+\n+/** Windows stops at 260 characters for a whole path, and another agent's folder may sit deeper than this one. */\n+const LONG_FULL_PATH = 240;\n+const LONG_RELATIVE_PATH = 160;\n+\n+/** 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. */\n+export function longPathsOf(root: string, paths: Iterable<string>): { path: string; length: number }[] {\n+\treturn [...paths]\n+\t\t.map((path) => ({ path, length: absolute(root, path).length }))\n+\t\t.filter((p) => p.length >= LONG_FULL_PATH || p.path.length >= LONG_RELATIVE_PATH)\n+\t\t.sort((a, b) => (a.path < b.path ? -1 : 1));\n+}\n+\n+/** A note per long path: abso keeps it, but a Windows agent may not be able to write the file. */\n+export function longPathText(list: readonly { path: string; length: number }[] | undefined): string[] {\n+\treturn (list ?? []).map(\n+\t\t(p) =>\n+\t\t\t`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.`,\n+\t);\n+}\n+\n /** \"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. */\n export function recordedFirst(r: Recorded): string[] {\n \tif (r.paused) return [\"Recording is paused (abso resume): your folder's changes were not sent first.\"];\n \tif (r.sent === 0) return [];\n-\treturn [`Recorded first: ${recordedList(r)}.`];\n+\treturn [`Recorded first: ${recordedList(r)}.`, ...gitMarkerText(r.gitMarkers), ...longPathText(r.longPaths)];\n }\n \n-/** \"2 changed (a, b), 3 deleted (x, y, z)\": deletions said as such (#37, Builder 2 read its rm'd files as edits). */\n+/** \"2 changed (a, b), 3 deleted (x, y, z)\": deletions said as such (#37: deleted files were read as edits). */\n function recordedList(r: Recorded): string {\n \tconst names = (list: readonly string[]) =>\n \t\tlist.length <= LISTED ? list.join(\", \") : `${list.slice(0, LISTED).join(\", \")}, and ${list.length - LISTED} more`;\n \tconst gone = new Set(r.deleted);\n \tconst fresh = new Set(r.added ?? []);\n \tconst changed = r.paths.filter((p) => !gone.has(p) && !fresh.has(p));\n-\t// New files are named in full: they are the ones a script made and nobody remembers (#37, Builder 7).\n+\t// New files are named in full: they are the ones a script made and nobody remembers (#37).\n \treturn [\n \t\t...(fresh.size > 0 ? [`${fresh.size} new (${[...fresh].join(\", \")})`] : []),\n \t\t...(changed.length > 0 ? [`${changed.length} changed (${names(changed)})`] : []),\n@@ -250,7 +361,7 @@\n }\n \n export const READER_REFUSES =\n-\t\"this folder is a reader: it records and publishes nothing (abso reader off makes it a writer again)\";\n+\t\"this folder is a reader: it records and pushes nothing (abso reader off makes it a writer again)\";\n \n /** abso pause: send nothing from the folder (no hook, no record) until abso resume. */\n export async function runPause(_args: Parsed, env: Env): Promise<Output> {\n@@ -269,7 +380,7 @@\n \trmSync(pausedPath(root), { force: true });\n \treturn {\n \t\ttext: was\n-\t\t\t? \"Recording again. Changes made while paused go at the next abso record (or publish, sync, ...); abso status lists them.\"\n+\t\t\t? \"Recording again. Changes made while paused go at the next abso record (or push, pull, ...); abso status lists them.\"\n \t\t\t: \"Recording was not paused.\",\n \t\tjson: { paused: false },\n \t};\n@@ -290,16 +401,27 @@\n \t\tr.sent === 0\n \t\t\t? \"Nothing to record: the folder matches what the server has.\"\n \t\t\t: `Recorded ${recordedList(r)}${changed === undefined ? \"\" : `; ${changed} changed on the server`}.`;\n-\treturn {\n-\t\ttext: [...turnedBinaryText(r.turnedBinary), text, ...leftOutText(r.leftOut)].join(\"\\n\"),\n-\t\tjson: {\n-\t\t\tsent: r.sent,\n-\t\t\tchanged: r.changed ?? null,\n-\t\t\tnews: newsLines(r.news),\n-\t\t\tleftOut: r.leftOut,\n-\t\t\tturnedBinary: r.turnedBinary,\n+\treturn withLeftOut(\n+\t\t{\n+\t\t\ttext: [\n+\t\t\t\t...turnedBinaryText(r.turnedBinary),\n+\t\t\t\ttext,\n+\t\t\t\t...gitMarkerText(r.gitMarkers),\n+\t\t\t\t...longPathText(r.longPaths),\n+\t\t\t\t...leftOutText(r.leftOut),\n+\t\t\t].join(\"\\n\"),\n+\t\t\tjson: {\n+\t\t\t\tsent: r.sent,\n+\t\t\t\tchanged: r.changed ?? null,\n+\t\t\t\tnews: newsLines(r.news),\n+\t\t\t\tleftOut: r.leftOut,\n+\t\t\t\tturnedBinary: r.turnedBinary,\n+\t\t\t\tgitMarkers: r.gitMarkers ?? [],\n+\t\t\t\tlongPaths: r.longPaths ?? [],\n+\t\t\t},\n \t\t},\n-\t};\n+\t\tr.leftOut,\n+\t);\n }\n \n // --- writing answers into the folder ---\n@@ -310,9 +432,54 @@\n  * overwritten.\n  */\n export async function syncFolder(root: string, api: Api, full = false): Promise<Applied> {\n-\treturn applyAnswer(root, api, await api.sync({ full }));\n+\tconst applied = await applyAnswer(root, api, await pullAnswer(root, api, full));\n+\tpullWritten(root);\n+\treturn applied;\n }\n \n+const PULLING_FILE = \"pulling.json\";\n+\n+/**\n+ * Whether a pull here asked the server and never wrote its answer (abso-core 26): it timed out, or stopped\n+ * partway. The server moved the agent's base when it answered, and later pulls send only what changed since\n+ * that base, so the files it would have written stay behind until a pull asks for every file.\n+ */\n+export function pullUnfinished(root: string): boolean {\n+\treturn existsSync(join(stateDir(root), PULLING_FILE));\n+}\n+\n+/**\n+ * Ask the server for a pull, noting first that one is under way, so a pull whose answer never gets written\n+ * leaves the note behind. With that note, or `full`, it asks for every file in the agent's view, which the\n+ * folder's own checks then write only where the folder differs. pullWritten takes the note away.\n+ */\n+export async function pullAnswer(root: string, api: Api, full = false): Promise<SyncAnswer> {\n+\tconst unfinished = pullUnfinished(root);\n+\twriteFileAtomic(join(stateDir(root), PULLING_FILE), `${JSON.stringify({ at: new Date().toISOString() })}\\n`);\n+\tif (!full && !unfinished) return api.sync();\n+\ttry {\n+\t\treturn await api.sync({ full: true });\n+\t} catch (err) {\n+\t\t// A server from before full pulls (501): the plain pull is still better than none.\n+\t\tif (unfinished && !full && err instanceof ApiError && err.status === 501) return api.sync();\n+\t\tthrow err;\n+\t}\n+}\n+\n+/** The pull's answer is written into the folder: take away pullAnswer's note. */\n+export function pullWritten(root: string): void {\n+\trmSync(join(stateDir(root), PULLING_FILE), { force: true });\n+}\n+\n+/** Status's line, and pull's when it caught up. */\n+export function pullUnfinishedText(root: string): string[] {\n+\treturn pullUnfinished(root)\n+\t\t? [\n+\t\t\t\t\"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.\",\n+\t\t\t]\n+\t\t: [];\n+}\n+\n /** What writing a sync or switch answer into the folder did. */\n export interface Applied {\n \treadonly version: number;\n@@ -380,7 +547,7 @@\n /** Files earlier syncs couldn't write here, kept in .avcs/unwritten.json until a write of them works. */\n export function readUnwritten(root: string): Record<string, Unwritten> {\n \ttry {\n-\t\treturn JSON.parse(readFileSync(join(root, AVCS_DIR, UNWRITTEN_FILE), \"utf8\")) as Record<string, Unwritten>;\n+\t\treturn JSON.parse(readFileSync(join(stateDir(root), UNWRITTEN_FILE), \"utf8\")) as Record<string, Unwritten>;\n \t} catch {\n \t\treturn {};\n \t}\n@@ -397,21 +564,21 @@\n \tif (paths.length === 0) return [];\n \tconst list = paths.map((p) => `${p} (version ${unwritten[p]!.version}: ${unwritten[p]!.why})`).join(\", \");\n \treturn [\n-\t\t`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}`,\n+\t\t`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}`,\n \t];\n }\n \n /** Write the files a sync or switch answer lists into the folder (the same rules as sync). */\n /** Write a sync's answer into the folder, holding its lock (#51). */\n export async function applyAnswer(root: string, api: Api, unscreened: SyncAnswer): Promise<Applied> {\n-\treturn withFolderLock(root, \"sync\", () => applyAnswerLocked(root, api, unscreened));\n+\treturn withFolderLock(root, \"pull\", () => applyAnswerLocked(root, api, unscreened));\n }\n \n async function applyAnswerLocked(root: string, api: Api, unscreened: SyncAnswer): Promise<Applied> {\n \t// Other agents' paths: nothing outside this folder, in .git/.claude/.avcs, or through a link.\n \tconst { answer, refused } = screenAnswer(root, unscreened);\n \t// A write the disk refuses anyway is named and skipped, so the rest of the sync still lands with its\n-\t// known copies and modes, rather than stopping partway (AVCS - Git Lessons; git checks every path first).\n+\t// known copies and modes, rather than stopping partway (git checks every path first).\n \tconst failed = new Set<string>();\n \t// Remembered until a write works (#51): the server won't send these files again.\n \tconst unwritten = readUnwritten(root);\n@@ -545,7 +712,7 @@\n \t\t\t\t\t.map((f) => ({ from: f.from!, to: f.path }))\n \t\t\t: [];\n \tif (JSON.stringify(unwritten) !== unwrittenBefore)\n-\t\twriteFileAtomic(join(root, AVCS_DIR, UNWRITTEN_FILE), `${JSON.stringify(unwritten, null, 1)}\\n`);\n+\t\twriteFileAtomic(join(stateDir(root), UNWRITTEN_FILE), `${JSON.stringify(unwritten, null, 1)}\\n`);\n \treturn {\n \t\tversion: answer.version,\n \t\twritten,"}],"unmatched":[]}