agents-can-communicate 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +46 -44
  2. package/bin/acc-update-worker.mjs +2 -1
  3. package/bin/entrypoints/hook-output.mjs +2 -0
  4. package/docs/ARCHITECTURE.md +19 -0
  5. package/docs/CAPABILITIES.md +1 -1
  6. package/docs/CLI.md +11 -1
  7. package/docs/CONFIGURATION.md +26 -0
  8. package/docs/GETTING_STARTED.md +4 -3
  9. package/docs/SECURITY_MODEL.md +6 -0
  10. package/docs/TROUBLESHOOTING.md +13 -0
  11. package/docs/UPGRADING.md +64 -0
  12. package/docs/WHY_ACC.md +32 -9
  13. package/docs/index.md +7 -5
  14. package/node_modules/@agents-can-communicate/adapter-antigravity/package.json +1 -1
  15. package/node_modules/@agents-can-communicate/adapter-antigravity/plugin/plugin.json +1 -1
  16. package/node_modules/@agents-can-communicate/adapter-antigravity/plugin/skills/acc/SKILL.md +3 -0
  17. package/node_modules/@agents-can-communicate/adapter-claude-code/package.json +1 -1
  18. package/node_modules/@agents-can-communicate/adapter-claude-code/plugin/skills/acc/SKILL.md +3 -0
  19. package/node_modules/@agents-can-communicate/adapter-claude-code/src/adapter.mjs +3 -1
  20. package/node_modules/@agents-can-communicate/adapter-claude-code/src/hooks.mjs +12 -0
  21. package/node_modules/@agents-can-communicate/adapter-codex/package.json +1 -1
  22. package/node_modules/@agents-can-communicate/adapter-codex/plugin/skills/acc/SKILL.md +3 -0
  23. package/node_modules/@agents-can-communicate/adapter-codex/src/adapter.mjs +2 -1
  24. package/node_modules/@agents-can-communicate/adapter-codex/src/allow-rule.mjs +129 -0
  25. package/node_modules/@agents-can-communicate/adapter-codex/src/hooks.mjs +11 -0
  26. package/node_modules/@agents-can-communicate/adapter-codex/src/install.mjs +18 -4
  27. package/node_modules/@agents-can-communicate/adapter-gemini-cli/extension/gemini-extension.json +1 -1
  28. package/node_modules/@agents-can-communicate/adapter-gemini-cli/extension/skills/acc/SKILL.md +3 -0
  29. package/node_modules/@agents-can-communicate/adapter-gemini-cli/package.json +1 -1
  30. package/node_modules/@agents-can-communicate/adapter-grok/package.json +1 -1
  31. package/node_modules/@agents-can-communicate/adapter-grok/plugin/skills/acc/SKILL.md +3 -0
  32. package/node_modules/@agents-can-communicate/adapter-kimi/package.json +1 -1
  33. package/node_modules/@agents-can-communicate/adapter-kimi/plugin/skills/acc/SKILL.md +3 -0
  34. package/node_modules/@agents-can-communicate/adapter-sdk/package.json +1 -1
  35. package/node_modules/@agents-can-communicate/adapter-sdk/src/capabilities.mjs +6 -0
  36. package/node_modules/@agents-can-communicate/adapter-sdk/src/own-version.mjs +27 -1
  37. package/node_modules/@agents-can-communicate/cli/package.json +1 -1
  38. package/node_modules/@agents-can-communicate/cli/src/args.mjs +4 -1
  39. package/node_modules/@agents-can-communicate/cli/src/doctor-command.mjs +14 -0
  40. package/node_modules/@agents-can-communicate/cli/src/help.mjs +2 -0
  41. package/node_modules/@agents-can-communicate/cli/src/main.mjs +12 -3
  42. package/node_modules/@agents-can-communicate/cli/src/managed-runtime/activation.mjs +19 -6
  43. package/node_modules/@agents-can-communicate/cli/src/managed-runtime/launchers.mjs +1 -1
  44. package/node_modules/@agents-can-communicate/cli/src/managed-runtime/portable-fs.mjs +41 -20
  45. package/node_modules/@agents-can-communicate/cli/src/managed-runtime/schedule.mjs +17 -1
  46. package/node_modules/@agents-can-communicate/cli/src/managed-runtime/store-upgrade.mjs +105 -0
  47. package/node_modules/@agents-can-communicate/cli/src/managed-runtime/worker.mjs +5 -0
  48. package/node_modules/@agents-can-communicate/cli/src/store-migration-command.mjs +39 -0
  49. package/node_modules/@agents-can-communicate/core/package.json +1 -1
  50. package/node_modules/@agents-can-communicate/core/src/conversations.mjs +21 -52
  51. package/node_modules/@agents-can-communicate/core/src/decision-changes.mjs +16 -11
  52. package/node_modules/@agents-can-communicate/core/src/inbox.mjs +12 -4
  53. package/node_modules/@agents-can-communicate/core/src/message-retry.mjs +43 -0
  54. package/node_modules/@agents-can-communicate/core/src/ports.mjs +12 -1
  55. package/node_modules/@agents-can-communicate/core/src/receipts.mjs +20 -6
  56. package/node_modules/@agents-can-communicate/delivery-router/package.json +1 -1
  57. package/node_modules/@agents-can-communicate/hook-runner/package.json +1 -1
  58. package/node_modules/@agents-can-communicate/hook-runner/src/runner.mjs +23 -3
  59. package/node_modules/@agents-can-communicate/installer/package.json +1 -1
  60. package/node_modules/@agents-can-communicate/installer/src/ownership.mjs +6 -2
  61. package/node_modules/@agents-can-communicate/mcp-server/package.json +1 -1
  62. package/node_modules/@agents-can-communicate/protocol/package.json +1 -1
  63. package/node_modules/@agents-can-communicate/protocol/src/index.mjs +1 -0
  64. package/node_modules/@agents-can-communicate/protocol/src/transaction-indexes.mjs +22 -0
  65. package/node_modules/@agents-can-communicate/storage-filesystem/package.json +1 -1
  66. package/node_modules/@agents-can-communicate/storage-filesystem/src/identity.mjs +22 -10
  67. package/node_modules/@agents-can-communicate/storage-filesystem/src/index-cache.mjs +144 -0
  68. package/node_modules/@agents-can-communicate/storage-filesystem/src/index-io.mjs +65 -0
  69. package/node_modules/@agents-can-communicate/storage-filesystem/src/index-pages.mjs +85 -0
  70. package/node_modules/@agents-can-communicate/storage-filesystem/src/index-reclaim-progress.mjs +35 -0
  71. package/node_modules/@agents-can-communicate/storage-filesystem/src/index-reclaim.mjs +142 -0
  72. package/node_modules/@agents-can-communicate/storage-filesystem/src/index-tree.mjs +142 -0
  73. package/node_modules/@agents-can-communicate/storage-filesystem/src/index.mjs +1 -0
  74. package/node_modules/@agents-can-communicate/storage-filesystem/src/journal.mjs +2 -0
  75. package/node_modules/@agents-can-communicate/storage-filesystem/src/portable-fs.mjs +41 -20
  76. package/node_modules/@agents-can-communicate/storage-filesystem/src/prune-durability.mjs +29 -0
  77. package/node_modules/@agents-can-communicate/storage-filesystem/src/reclaim.mjs +13 -3
  78. package/node_modules/@agents-can-communicate/storage-filesystem/src/recovery.mjs +9 -3
  79. package/node_modules/@agents-can-communicate/storage-filesystem/src/session-read.mjs +6 -4
  80. package/node_modules/@agents-can-communicate/storage-filesystem/src/state-reads.mjs +43 -0
  81. package/node_modules/@agents-can-communicate/storage-filesystem/src/store-migration.mjs +56 -0
  82. package/node_modules/@agents-can-communicate/storage-filesystem/src/store.mjs +61 -110
  83. package/node_modules/@agents-can-communicate/storage-filesystem/src/transaction-view.mjs +131 -0
  84. package/package.json +5 -4
@@ -0,0 +1,142 @@
1
+ import path from "node:path";
2
+ import { idleJournal, readActiveJournal } from "./active-journal.mjs";
3
+ import { listDirectoryEntries, nameCommittedLater } from "./atomic-json.mjs";
4
+ import { condemn, detachDoomed, discard, expired } from "./doomed-directory.mjs";
5
+ import { createIndexIO } from "./index-io.mjs";
6
+ import { IndexCacheUnavailable } from "./index-pages.mjs";
7
+ import { readReclaimProgress, writeReclaimProgress } from "./index-reclaim-progress.mjs";
8
+ import { readStoreIdentity } from "./identity.mjs";
9
+ import { journalEntry, writeJournalEntry } from "./journal.mjs";
10
+ import { syncPruneDirectories } from "./prune-durability.mjs";
11
+ import { assertManagedDirectory } from "./safe-directory.mjs";
12
+
13
+ const children = page => page.type === "branch" ? page.children.map(([, hash]) => hash)
14
+ : page.type === "leaf" ? [...page.entries.map(entry => entry.ids), page.next].filter(Boolean)
15
+ : [page.next].filter(Boolean);
16
+
17
+ // The caller holds the writer mutex. Persist both mark and sweep progress: a
18
+ // live graph or a directory larger than one pass must eventually finish. Roots
19
+ // added by intervening writers are marked before sweeping resumes. Previously
20
+ // marked roots stay in the conservative live superset until the next cycle.
21
+ export async function reclaimIndexPages(paths, { root, roots, limit = 512, deadlineAt } = {}) {
22
+ let spent = 0, reclaimed = 0;
23
+ const result = (remaining, deferred = false) => ({ reclaimed, remaining, spent, deferred });
24
+ if (expired(deadlineAt)) return result(true, true);
25
+ const directory = path.join(root, "indexes", "v1", "pages");
26
+ try { await assertManagedDirectory(root, directory); }
27
+ catch (error) { return result(error.code !== "ENOENT", error.code !== "ENOENT"); }
28
+ if (limit < 4) return result(true);
29
+ const io = createIndexIO({ paths, root, publishOptions: {} });
30
+ if (roots === undefined) {
31
+ let manifest;
32
+ try { manifest = await io.readManifest(); }
33
+ catch (error) { if (error instanceof IndexCacheUnavailable) return result(true, true); throw error; }
34
+ if (manifest === null) return result(true, true);
35
+ // Primary authority faults stay outside cache-error handling.
36
+ const active = await readActiveJournal(paths, root), identity = await readStoreIdentity(paths);
37
+ if (active.state !== "idle" || manifest.journalGeneration !== active.generation
38
+ || manifest.workspaceId !== identity?.workspaceId) return result(true, true);
39
+ roots = manifest.roots;
40
+ }
41
+ const progress = await readReclaimProgress(root);
42
+ spent += 1;
43
+ // Reserve the checkpoint publication within the same budget as page work.
44
+ const budget = limit - 1;
45
+ const reachable = new Set(progress.visited), pending = progress.pending;
46
+ for (const hash of Object.values(roots).filter(Boolean)) {
47
+ if (!reachable.has(hash) && !pending.includes(hash)) pending.push(hash);
48
+ }
49
+ const checkpoint = async (remaining) => {
50
+ progress.visited = [...reachable];
51
+ progress.done = !remaining;
52
+ try {
53
+ await writeReclaimProgress(paths, root, progress, deadlineAt);
54
+ spent += 1;
55
+ return result(remaining);
56
+ } catch { return result(true, true); }
57
+ };
58
+ try {
59
+ while (pending.length > 0) {
60
+ const hash = pending.pop();
61
+ if (reachable.has(hash)) continue;
62
+ if (spent >= budget || expired(deadlineAt)) {
63
+ pending.push(hash);
64
+ return checkpoint(true);
65
+ }
66
+ spent += 1;
67
+ pending.push(...children(await io.readPage(hash)));
68
+ reachable.add(hash);
69
+ }
70
+ } catch (error) {
71
+ if (error instanceof IndexCacheUnavailable) {
72
+ // A former root can disappear while the cache rebuilds. Do not let an
73
+ // unreadable old stack poison every later pass; restart from authority.
74
+ pending.length = 0;
75
+ reachable.clear();
76
+ progress.cursor = null;
77
+ await checkpoint(false);
78
+ return result(true, true);
79
+ }
80
+ throw error;
81
+ }
82
+ let entries;
83
+ try { entries = await listDirectoryEntries(directory, { root }); }
84
+ catch { return result(true, true); } // Cache paths alone are optional maintenance.
85
+ let doomed, reserved = 0, remaining = false;
86
+ entries.sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0);
87
+ for (const entry of entries) {
88
+ if (!/^[a-f0-9]{64}\.json$/.test(entry.name) || (progress.cursor !== null && entry.name <= progress.cursor)) continue;
89
+ if (spent + reserved >= budget || expired(deadlineAt)) { remaining = true; break; }
90
+ spent += 1;
91
+ if (!entry.isFile() || reachable.has(entry.name.slice(0, -5))) { progress.cursor = entry.name; continue; }
92
+ if (spent + reserved + 2 > budget) { remaining = true; break; }
93
+ doomed ??= await detachDoomed(root);
94
+ await assertManagedDirectory(root, directory);
95
+ await condemn(path.join(directory, entry.name), doomed);
96
+ spent += 1; reserved += 1;
97
+ progress.cursor = entry.name;
98
+ }
99
+ if (doomed !== undefined) {
100
+ const removed = await discard(doomed, root, budget - spent, deadlineAt);
101
+ reclaimed += removed.spent; spent += removed.spent;
102
+ remaining ||= !removed.drained;
103
+ }
104
+ return checkpoint(remaining);
105
+ }
106
+
107
+ async function discardCache(root, collect) {
108
+ const directory = path.join(root, "indexes", "v1");
109
+ // Only a validated cache directory may move. Damage/unsafe cache paths stay
110
+ // untouched, and the changed durable epoch already makes their roots stale.
111
+ try {
112
+ await assertManagedDirectory(root, directory);
113
+ const doomed = await detachDoomed(root);
114
+ await condemn(directory, doomed);
115
+ collect(directory, doomed);
116
+ } catch { /* Cache loss or refusal cannot fail a completed primary prune. */ }
117
+ }
118
+
119
+ // Existing empty-publication journals fence physical retirement without
120
+ // introducing a new authoritative marker or a per-message flush.
121
+ export async function withIndexedPrune(paths, options, operation) {
122
+ const { ids, firstSequence, root, deadlineAt, ...publish } = options;
123
+ const entry = journalEntry(ids.next("transaction"), firstSequence, [], options.clock.now());
124
+ await writeJournalEntry(paths, { ...publish, root, deadlineAt }, entry);
125
+ const directories = new Set([root]);
126
+ const collect = (file, doomed) => { directories.add(path.dirname(file)); directories.add(doomed); };
127
+ try {
128
+ await options.failAt?.("after-indexed-prune-activated");
129
+ await discardCache(root, collect);
130
+ return await operation(async (file, doomed) => {
131
+ collect(file, doomed);
132
+ await options.failAt?.("after-indexed-prune-move");
133
+ });
134
+ } finally {
135
+ // A decided/applied prefix must finish its durability even after its
136
+ // caller's deadline. POSIX flushes names; the final Windows journal flush
137
+ // commits every earlier NTFS metadata rename.
138
+ await syncPruneDirectories(root, directories, options);
139
+ await options.failAt?.("before-indexed-prune-idle");
140
+ await idleJournal(paths, { ...publish, root, durability: nameCommittedLater(options.platform) }, entry.transactionId);
141
+ }
142
+ }
@@ -0,0 +1,142 @@
1
+ import { createHash } from "node:crypto";
2
+ import { assertPortableId } from "@agents-can-communicate/protocol";
3
+ import { assertPageHash, canonicalTuple, IndexCacheUnavailable } from "./index-pages.mjs";
4
+
5
+ const compare = (a, b) => a.localeCompare(b);
6
+ const keyOf = entry => canonicalTuple(entry.tuple);
7
+ const digest = tuple => createHash("sha256").update(canonicalTuple(tuple)).digest("hex");
8
+ const fault = reason => { throw new IndexCacheUnavailable(reason); };
9
+ export function createIndexTree({ readPage, writePage, digestTuple = digest }) {
10
+ async function read(hash, seen) {
11
+ assertPageHash(hash);
12
+ if (seen.has(hash)) fault("page_cycle");
13
+ seen.add(hash);
14
+ return readPage(hash);
15
+ }
16
+ async function groupLookup(root) {
17
+ const ids = [], seen = new Set();
18
+ while (root !== null) {
19
+ const page = await read(root, seen);
20
+ if (page.type !== "group") fault("invalid_group_reference");
21
+ for (const id of page.ids) {
22
+ if (ids.length && compare(ids.at(-1), id) >= 0) fault("duplicate_or_unordered_group");
23
+ ids.push(id);
24
+ }
25
+ root = page.next;
26
+ }
27
+ return ids;
28
+ }
29
+ async function groupUpdate(root, id, operation, seen = new Set()) {
30
+ if (root === null) return operation === "remove" ? null : writePage({ type: "group", ids: [id], next: null });
31
+ const page = await read(root, seen);
32
+ if (page.type !== "group") fault("invalid_group_reference");
33
+ const position = page.ids.findIndex(found => compare(found, id) >= 0);
34
+ if (position === -1 && page.next !== null) {
35
+ const next = await groupUpdate(page.next, id, operation, seen);
36
+ return next === page.next ? root : writePage({ ...page, next });
37
+ }
38
+ const ids = [...page.ids], at = position === -1 ? ids.length : position;
39
+ if (ids[at] === id) {
40
+ if (operation === "put") return root;
41
+ ids.splice(at, 1);
42
+ } else {
43
+ if (operation === "remove") return root;
44
+ ids.splice(at, 0, id);
45
+ }
46
+ if (ids.length === 0) return page.next;
47
+ if (ids.length <= 32) return writePage({ ...page, ids });
48
+ const next = await writePage({ type: "group", ids: ids.slice(16), next: page.next });
49
+ return writePage({ type: "group", ids: ids.slice(0, 16), next });
50
+ }
51
+ function checkNode(page, depth, prefix) {
52
+ if (page.type === "branch") {
53
+ if (depth >= 64) fault("radix_depth_exceeded");
54
+ } else if (page.type === "leaf") {
55
+ if (page.next !== null && depth !== 64) fault("unexpected_collision_page");
56
+ for (const entry of page.entries) {
57
+ if (!entry.digest.startsWith(prefix) || entry.digest !== digestTuple(entry.tuple)) fault("misrouted_tuple");
58
+ }
59
+ } else fault("invalid_tree_reference");
60
+ }
61
+ async function lookup(root, tuple) {
62
+ const key = canonicalTuple(tuple), hash = assertPageHash(digestTuple(tuple)), seen = new Set();
63
+ let depth = 0, prefix = "", idsRoot = null;
64
+ while (root !== null) {
65
+ const page = await read(root, seen); checkNode(page, depth, prefix);
66
+ if (page.type === "branch") {
67
+ const route = hash[depth++]; prefix += route;
68
+ root = page.children.find(([digit]) => digit === route)?.[1] ?? null;
69
+ } else {
70
+ const found = page.entries.find(entry => keyOf(entry) === key);
71
+ if (found) {
72
+ if (idsRoot !== null) fault("duplicate_collision_key");
73
+ idsRoot = found.ids;
74
+ }
75
+ root = page.next;
76
+ }
77
+ }
78
+ return idsRoot === null ? [] : groupLookup(idsRoot);
79
+ }
80
+ const leaf = (entries, next = null) => writePage({ type: "leaf",
81
+ entries: [...entries].sort((a, b) => compare(keyOf(a), keyOf(b))), next });
82
+ async function split(entries, depth) {
83
+ if (entries.length <= 32) return leaf(entries);
84
+ if (depth === 64) {
85
+ let next = null;
86
+ for (let end = entries.length; end > 0; end -= 32) {
87
+ next = await leaf(entries.slice(Math.max(0, end - 32), end), next);
88
+ }
89
+ return next;
90
+ }
91
+ const routes = new Map();
92
+ for (const entry of entries) {
93
+ const route = entry.digest[depth];
94
+ if (!routes.has(route)) routes.set(route, []);
95
+ routes.get(route).push(entry);
96
+ }
97
+ const children = [];
98
+ for (const route of [...routes.keys()].sort()) children.push([route, await split(routes.get(route), depth + 1)]);
99
+ return writePage({ type: "branch", children });
100
+ }
101
+ async function update(root, tuple, id, operation, depth = 0, prefix = "", seen = new Set()) {
102
+ const hash = assertPageHash(digestTuple(tuple)), key = canonicalTuple(tuple);
103
+ if (root === null) {
104
+ if (operation === "remove") return null;
105
+ return leaf([{ digest: hash, tuple, ids: await groupUpdate(null, id, operation) }]);
106
+ }
107
+ const page = await read(root, seen); checkNode(page, depth, prefix);
108
+ if (page.type === "branch") {
109
+ const route = hash[depth], old = page.children.find(([digit]) => digit === route)?.[1] ?? null;
110
+ const next = await update(old, tuple, id, operation, depth + 1, prefix + route, seen);
111
+ if (old === next) return root;
112
+ const children = page.children.filter(([digit]) => digit !== route);
113
+ if (next !== null) children.push([route, next]);
114
+ children.sort(([a], [b]) => compare(a, b));
115
+ return children.length === 0 ? null : writePage({ type: "branch", children });
116
+ }
117
+ const entries = [...page.entries], at = entries.findIndex(entry => keyOf(entry) === key);
118
+ if (at !== -1) {
119
+ const ids = await groupUpdate(entries[at].ids, id, operation);
120
+ if (ids === entries[at].ids) return root;
121
+ if (ids === null) entries.splice(at, 1);
122
+ else entries[at] = { ...entries[at], ids };
123
+ return entries.length === 0 ? page.next : leaf(entries, page.next);
124
+ }
125
+ if (page.next !== null) {
126
+ const next = await update(page.next, tuple, id, operation, depth, prefix, seen);
127
+ return next === page.next ? root : leaf(entries, next);
128
+ }
129
+ if (operation === "remove") return root;
130
+ entries.push({ digest: hash, tuple, ids: await groupUpdate(null, id, operation) });
131
+ return split(entries, depth);
132
+ }
133
+ async function apply(root, deltas) {
134
+ for (const { tuple, id, operation } of deltas) {
135
+ assertPortableId(id, "index record ID");
136
+ if (!["put", "remove"].includes(operation)) fault("invalid_delta");
137
+ root = await update(root, tuple, id, operation);
138
+ }
139
+ return root;
140
+ }
141
+ return Object.freeze({ lookup, apply });
142
+ }
@@ -5,3 +5,4 @@ export { readStoreIdentity, requireStoreIdentity, STORE_VERSION } from "./identi
5
5
  export { withWriterMutex } from "./writer-mutex.mjs";
6
6
  export { readSessionRecord } from "./session-read.mjs";
7
7
  export { openNoFollow } from "./portable-fs.mjs";
8
+ export { migrateFilesystemStore } from "./store-migration.mjs";
@@ -5,6 +5,7 @@ import { AccError, EXIT, assertPortableId } from "@agents-can-communicate/protoc
5
5
  import { activateJournal, idleJournal, readActiveJournal } from "./active-journal.mjs";
6
6
  import { encode, nameCommittedLater, publishAtomic, readJsonIfPresent, retainFile }
7
7
  from "./atomic-json.mjs";
8
+ import { recoverPruneDurability } from "./prune-durability.mjs";
8
9
 
9
10
  export const JOURNAL_VERSION = 2;
10
11
 
@@ -133,6 +134,7 @@ export async function readJournalCeiling(paths, root) {
133
134
  // twice changes nothing and a genuine conflict is never papered over.
134
135
  export async function rollForward(paths, options, entry) {
135
136
  const published = [];
137
+ if (entry.publications.length === 0) await recoverPruneDurability(paths, options);
136
138
  for (const publication of entry.publications) {
137
139
  const destination = publicationDestination(options.root, publication.path);
138
140
  if (publication.retainedPath !== null) {
@@ -74,8 +74,22 @@ export async function renameEntry(from, to, { platform = process.platform,
74
74
  }, error => BUSY.has(error.code), { deadlineAt, sleep });
75
75
  }
76
76
 
77
- const gone = (file, lstat) => lstat(file).then(() => false,
78
- error => error.code === "ENOENT");
77
+ // A name inside a directory another process is deleting - a waiting writer's
78
+ // rmdir of a lock its owner emptied - answers lstat with EPERM until the
79
+ // deletion is done (measured on windows-latest: lstat of writer.lock\owner.json
80
+ // while eight writers elected one, 2026-10-06, and the waiting writer failed
81
+ // instead of looking again). The name is asked again within the caller's
82
+ // deadline: what it settles to is the answer, and an EPERM that outlasts the
83
+ // deadline is kept.
84
+ async function lstatSettled(file, lstat, { deadlineAt, sleep }) {
85
+ try {
86
+ return await retrying(() => lstat(file, { bigint: true }), error => error.code === "EPERM",
87
+ { deadlineAt, sleep });
88
+ } catch (error) {
89
+ if (error.code === "ENOENT") return null;
90
+ throw error;
91
+ }
92
+ }
79
93
 
80
94
  const refusedLink = file => Object.assign(
81
95
  new Error(`ELOOP: too many symbolic links encountered, open '${file}'`),
@@ -99,32 +113,39 @@ export async function openNoFollow(file, flags, { mode, platform = process.platf
99
113
  open = fs.open, lstat = fs.lstat, deadlineAt, sleep } = {}) {
100
114
  if (!isWindows(platform)) return open(file, flags | constants.O_NOFOLLOW, mode);
101
115
  for (let attempt = 1; ; attempt += 1) {
102
- const before = await lstat(file, { bigint: true }).catch(error => {
103
- if (error.code === "ENOENT") return null;
104
- throw error;
105
- });
116
+ const before = await lstatSettled(file, lstat, { deadlineAt, sleep });
106
117
  if (before?.isSymbolicLink()) throw refusedLink(file);
107
- // A scanner that opened the file without read sharing refuses every reader
108
- // with EBUSY until it lets go.
109
- const handle = await retrying(() => open(file, flags, mode), error => error.code === "EBUSY",
110
- { deadlineAt, sleep }).catch(async error => {
118
+ let handle;
119
+ try {
120
+ // A scanner that opened the file without read sharing refuses every
121
+ // reader with EBUSY until it lets go.
122
+ handle = await retrying(() => open(file, flags, mode), error => error.code === "EBUSY",
123
+ { deadlineAt, sleep });
124
+ } catch (error) {
111
125
  // A name another process is deleting, or a lock changing hands around
112
126
  // it, refuses the open with EPERM, and the name is gone right after
113
127
  // (measured on windows-latest, for a name present before the open and
114
128
  // for one absent before it): that is ENOENT, as Linux says it. A name
115
- // that is still there keeps its EPERM, and so does a create on a name
116
- // that was free, which is a refused create.
129
+ // that names another file by then changed hands - a lock released and
130
+ // taken again while the open was refused (measured on windows-latest,
131
+ // 2026-10-06) - and is opened afresh, as a swap after the open is. A
132
+ // name still naming what it named keeps its EPERM, and so does a create
133
+ // on a name that was free, which is a refused create.
117
134
  const refusedCreate = before === null && (flags & constants.O_CREAT) !== 0;
118
- if (error.code !== "EPERM" || refusedCreate || !await gone(file, lstat)) throw error;
119
- throw Object.assign(new Error(`ENOENT: no such file or directory, open '${file}'`,
120
- { cause: error }), { code: "ENOENT", syscall: "open", path: file });
121
- });
135
+ if (error.code !== "EPERM" || refusedCreate) throw error;
136
+ const now = await lstatSettled(file, lstat, { deadlineAt, sleep }).catch(() => undefined);
137
+ if (now === null) {
138
+ throw Object.assign(new Error(`ENOENT: no such file or directory, open '${file}'`,
139
+ { cause: error }), { code: "ENOENT", syscall: "open", path: file });
140
+ }
141
+ const changedHands = now !== undefined
142
+ && (before === null || now.dev !== before.dev || now.ino !== before.ino);
143
+ if (changedHands && attempt < SWAPS) continue;
144
+ throw error;
145
+ }
122
146
  try {
123
147
  const opened = await handle.stat({ bigint: true });
124
- const named = await lstat(file, { bigint: true }).catch(error => {
125
- if (error.code === "ENOENT") return null;
126
- throw error;
127
- });
148
+ const named = await lstatSettled(file, lstat, { deadlineAt, sleep });
128
149
  if (named?.isSymbolicLink()) throw refusedLink(file);
129
150
  if (named !== null && named.dev === opened.dev && named.ino === opened.ino) return handle;
130
151
  if (attempt >= SWAPS) throw refusedLink(file);
@@ -0,0 +1,29 @@
1
+ import path from "node:path";
2
+ import { doomedDirectories } from "./doomed-directory.mjs";
3
+ import { isWindows, syncEntry } from "./portable-fs.mjs";
4
+ import { assertManagedDirectory } from "./safe-directory.mjs";
5
+
6
+ // A decided prune finishes its applied prefix even after its deadline. On
7
+ // Windows the final idle-journal file flush commits earlier NTFS renames.
8
+ export async function syncPruneDirectories(root, directories, { platform } = {}) {
9
+ if (isWindows(platform)) return;
10
+ for (const directory of directories) {
11
+ try { await assertManagedDirectory(root, directory); }
12
+ catch (error) { if (error.code === "ENOENT") continue; throw error; }
13
+ await syncEntry(directory, directory, { platform });
14
+ }
15
+ }
16
+
17
+ // Empty-publication journals are physical indexed prunes. Process death loses
18
+ // their in-memory affected-directory set; conservatively fence every possible
19
+ // primary/marker parent before idle permits a current negative cache. No new
20
+ // journal format or per-record publication is needed.
21
+ export async function recoverPruneDurability(paths, { root, platform } = {}) {
22
+ if (isWindows(platform)) return;
23
+ const directories = [
24
+ ...["message", "receipt"].flatMap(kind => [path.join(paths.state, kind),
25
+ path.join(paths.retained, "state", kind)]),
26
+ ...await doomedDirectories(root), root,
27
+ ];
28
+ await syncPruneDirectories(root, directories, { platform });
29
+ }
@@ -7,6 +7,7 @@ import { listDirectoryEntries, readJsonIfPresent } from "./atomic-json.mjs";
7
7
  import { condemn, detachDoomed, discard, discardLeftovers, expired } from "./doomed-directory.mjs";
8
8
  import { assertSequence, raiseEventFloor, readEventFloor } from "./event-floor.mjs";
9
9
  import { statePath } from "./record-id.mjs";
10
+ import { reclaimIndexPages } from "./index-reclaim.mjs";
10
11
 
11
12
  // Shared with the stage sweep that calls this, so one pass cannot spend two
12
13
  // budgets. Exported for the caller that wants to reclaim without a bound.
@@ -148,8 +149,14 @@ export async function reclaimRetired(paths,
148
149
 
149
150
  const removed = await discard(doomed, root, limit - spent, deadlineAt);
150
151
  reclaimed += removed.spent;
152
+ spent += removed.spent;
153
+ const indexes = journals.drained && markers.drained && removed.drained
154
+ ? await reclaimIndexPages(paths, { root, limit: limit - spent, deadlineAt })
155
+ : { reclaimed: 0, remaining: false };
156
+ reclaimed += indexes.reclaimed;
151
157
  return { reclaimed,
152
- remaining: !journals.drained || !markers.drained || !removed.drained };
158
+ remaining: !journals.drained || !markers.drained || !removed.drained
159
+ || (indexes.remaining && !indexes.deferred) };
153
160
  }
154
161
 
155
162
  /**
@@ -172,7 +179,7 @@ export async function reclaimRetired(paths,
172
179
  * @returns {Promise<{ reclaimed: number, skipped: number, remaining: boolean }>}
173
180
  */
174
181
  export async function reclaimStateRecords(paths, entries,
175
- { root, limit = RECLAIM_BUDGET, deadlineAt } = {}) {
182
+ { root, limit = RECLAIM_BUDGET, deadlineAt, onCondemned } = {}) {
176
183
  if (expired(deadlineAt)) return { reclaimed: 0, skipped: 0, remaining: entries.length > 0 };
177
184
  let spent = 0;
178
185
  let skipped = 0;
@@ -202,10 +209,13 @@ export async function reclaimStateRecords(paths, entries,
202
209
  continue;
203
210
  }
204
211
  await condemn(filePath, doomed);
212
+ await onCondemned?.(filePath, doomed);
205
213
  spent += 1;
206
214
  condemned += 1;
207
215
  try {
208
- await condemn(path.join(paths.retained, "state", entry.kind, entry.id), doomed);
216
+ const markers = path.join(paths.retained, "state", entry.kind, entry.id);
217
+ await condemn(markers, doomed);
218
+ await onCondemned?.(markers, doomed);
209
219
  spent += 1;
210
220
  // Counted only when it moved. Most records own no marker directory at
211
221
  // all, and counting one anyway made `remaining` compare what was
@@ -18,8 +18,11 @@ import { withWriterMutex } from "./writer-mutex.mjs";
18
18
  * trimmedThrough: string|null }} RepairReport */
19
19
 
20
20
  const report = ({ repaired = [], blocked = [], corrupt = [], swept = 0, staged = 0,
21
- partials = 0, retired = 0, trimmedThrough = null }) => ({
21
+ partials = 0, retired = 0, trimmedThrough = null, migrationRequired = false }) => ({
22
22
  healthy: blocked.length === 0 && corrupt.length === 0,
23
+ // An older contract, not an unreadable file: the identity read and named its
24
+ // version. The reader names the migration rather than calling it ambiguous.
25
+ ...(migrationRequired ? { migrationRequired: true } : {}),
23
26
  repaired: [...repaired].sort(),
24
27
  blocked: [...blocked].sort(),
25
28
  corrupt: [...corrupt].sort(),
@@ -75,7 +78,8 @@ async function inspect(root) {
75
78
  identity = await readStoreIdentity(paths);
76
79
  } catch (error) {
77
80
  blocked.push(path.join(root, "protocol.json"));
78
- return { paths, identity: null, pending, blocked, corrupt, reason: error.message };
81
+ return { paths, identity: null, pending, blocked, corrupt, reason: error.message,
82
+ migrationRequired: error?.details?.reasonCode === "store_migration_required" };
79
83
  }
80
84
  if (identity === null) blocked.push(path.join(root, "protocol.json"));
81
85
 
@@ -123,6 +127,7 @@ export async function diagnoseFilesystemStore({ root }) {
123
127
  return report({
124
128
  blocked: state.blocked,
125
129
  corrupt: state.corrupt,
130
+ migrationRequired: state.migrationRequired === true,
126
131
  repaired: state.pending.map(entry => entry.transactionId),
127
132
  ...await countStaging(state.paths, root),
128
133
  });
@@ -133,7 +138,8 @@ export async function repairFilesystemStore({ root, clock }) {
133
138
  if (state.blocked.length > 0 || state.corrupt.length > 0) {
134
139
  // Fail closed. Completing a journal on top of state we cannot even read
135
140
  // would turn an ambiguous store into a confidently wrong one.
136
- return report({ blocked: state.blocked, corrupt: state.corrupt });
141
+ return report({ blocked: state.blocked, corrupt: state.corrupt,
142
+ migrationRequired: state.migrationRequired === true });
137
143
  }
138
144
  // No early return for an empty journal any more: a store with nothing to roll
139
145
  // forward is exactly the store whose staging directory needs reclaiming.
@@ -1,16 +1,18 @@
1
1
  import path from "node:path";
2
2
  import { assertPortableId, validateRecord } from "@agents-can-communicate/protocol";
3
3
  import { readJsonIfPresent } from "./atomic-json.mjs";
4
- import { requireStoreIdentity } from "./identity.mjs";
4
+ import { readMigrationIdentity, requireStoreIdentity } from "./identity.mjs";
5
5
  import { assertStateBinding, statePath } from "./record-id.mjs";
6
6
  import { ephemeralIsDeleted, stateGenerationIsDeleted } from "./retention.mjs";
7
7
  import { storePaths } from "./store.mjs";
8
8
 
9
- /** Inspect ownership without opening, creating, recovering, or locking the store. */
10
- export async function readSessionRecord({ root, workspaceId, sessionId }) {
9
+ /** Inspect ownership without opening, creating, recovering, or locking the store.
10
+ * Legacy identity inspection is read-only metadata for runtime hold classification. */
11
+ export async function readSessionRecord({ root, workspaceId, sessionId, allowLegacyIdentity = false }) {
11
12
  assertPortableId(sessionId, "session id");
12
13
  const paths = storePaths(root);
13
- await requireStoreIdentity(paths, { workspaceId, create: false });
14
+ if (allowLegacyIdentity === true) await readMigrationIdentity(paths, workspaceId);
15
+ else await requireStoreIdentity(paths, { workspaceId, create: false });
14
16
  const file = statePath(paths, "session", sessionId);
15
17
  const durable = await readJsonIfPresent(file, root);
16
18
  if (durable !== null) {
@@ -0,0 +1,43 @@
1
+ import path from "node:path";
2
+ import { AccError, EXIT, validateRecord } from "@agents-can-communicate/protocol";
3
+ import { listDirectoryEntries, listJsonFiles, readJsonIfPresent } from "./atomic-json.mjs";
4
+ import { assertStateBinding, statePath } from "./record-id.mjs";
5
+ import { stateGenerationIsDeleted } from "./retention.mjs";
6
+ import { assertPublicationDeadline } from "./deadline.mjs";
7
+
8
+ export async function readStateEnvelope(paths, { root, workspaceId }, kind, id) {
9
+ const filePath = statePath(paths, kind, id);
10
+ const found = await readJsonIfPresent(filePath, root);
11
+ if (found === null) return null;
12
+ const envelope = assertStateBinding(found.value, kind, id, filePath);
13
+ if (await stateGenerationIsDeleted(paths, root, kind, id, envelope.generation)) return null;
14
+ validateRecord(kind, envelope.record);
15
+ if (workspaceId !== undefined && envelope.record.workspaceId !== workspaceId) {
16
+ throw new AccError(EXIT.DATA, "state record belongs to a different workspace",
17
+ { filePath, expected: workspaceId, actual: envelope.record.workspaceId });
18
+ }
19
+ return envelope;
20
+ }
21
+
22
+ export async function listState(paths, root, kind, deadlineAt) {
23
+ const envelopes = [];
24
+ for (const filePath of await listJsonFiles(path.join(paths.state, kind), { root })) {
25
+ assertPublicationDeadline(deadlineAt);
26
+ const envelope = await readStateEnvelope(paths, { root }, kind, path.basename(filePath, ".json"));
27
+ if (envelope !== null) envelopes.push(envelope);
28
+ }
29
+ return envelopes;
30
+ }
31
+
32
+ export async function loadStateEnvelopes(paths, { root, deadlineAt }, wanted = null) {
33
+ const loaded = new Map();
34
+ const kinds = (await listDirectoryEntries(paths.state, { root }))
35
+ .filter(entry => entry.isDirectory()).map(entry => entry.name)
36
+ .filter(kind => wanted === null || wanted.has(kind));
37
+ for (const kind of kinds) {
38
+ for (const envelope of await listState(paths, root, kind, deadlineAt)) {
39
+ loaded.set(`${envelope.kind}:${envelope.id}`, envelope);
40
+ }
41
+ }
42
+ return loaded;
43
+ }
@@ -0,0 +1,56 @@
1
+ import { AccError, EXIT, validateRecord } from "@agents-can-communicate/protocol";
2
+ import { encode, listJsonFiles, publishAtomic, readJsonIfPresent } from "./atomic-json.mjs";
3
+ import { initialiseActiveJournal, readActiveJournal } from "./active-journal.mjs";
4
+ import { assertPublicationDeadline } from "./deadline.mjs";
5
+ import { identityPath, readMigrationIdentity, STORE_VERSION } from "./identity.mjs";
6
+ import { createIndexCache } from "./index-cache.mjs";
7
+ import { IndexCacheUnavailable } from "./index-pages.mjs";
8
+ import { readOpenJournals, rollForward } from "./journal.mjs";
9
+ import { assertEventBinding } from "./record-id.mjs";
10
+ import { ensureManagedDirectory } from "./safe-directory.mjs";
11
+ import { loadStateEnvelopes } from "./state-reads.mjs";
12
+ import { storePaths } from "./store.mjs";
13
+ import { withWriterMutex } from "./writer-mutex.mjs";
14
+
15
+ /** Explicit library migration; the operator must quiesce unmanaged users first. */
16
+ export async function migrateFilesystemStore({ root, workspaceId, clock, ids,
17
+ allowMigration = false, platform = process.platform, deadlineAt, failAt }) {
18
+ if (allowMigration !== true) {
19
+ throw new AccError(EXIT.USAGE, "store migration requires explicit opt-in after quiescing other users");
20
+ }
21
+ assertPublicationDeadline(deadlineAt);
22
+ const paths = storePaths(root), publishOptions = { root, tmpDir: paths.tmp, clock, platform, failAt };
23
+ await readMigrationIdentity(paths, workspaceId);
24
+ return withWriterMutex(paths, { ...publishOptions, deadlineAt }, async () => {
25
+ const identity = await readMigrationIdentity(paths, workspaceId), fromVersion = identity.storeVersion;
26
+ for (const directory of Object.values(paths)) await ensureManagedDirectory(root, directory);
27
+ await initialiseActiveJournal(paths, { ...publishOptions, deadlineAt });
28
+ for (const entry of await readOpenJournals(paths, root)) {
29
+ // A decided journal finishes even if the migration's pre-switch budget lapses.
30
+ await rollForward(paths, publishOptions, entry);
31
+ }
32
+ assertPublicationDeadline(deadlineAt);
33
+ const primaries = await loadStateEnvelopes(paths, { root, deadlineAt });
34
+ for (const file of await listJsonFiles(paths.events, { root })) {
35
+ assertPublicationDeadline(deadlineAt);
36
+ const found = await readJsonIfPresent(file, root);
37
+ if (found === null) continue;
38
+ const event = validateRecord("event", assertEventBinding(found.value, file));
39
+ if (event.workspaceId !== workspaceId) throw new AccError(EXIT.DATA, "event belongs to a different workspace");
40
+ }
41
+ const cache = createIndexCache({ paths, root, workspaceId, publishOptions, loadPrimary: async () => primaries });
42
+ try { await cache.rebuild(await readActiveJournal(paths, root), deadlineAt, { required: true }); }
43
+ catch (error) {
44
+ if (!(error instanceof IndexCacheUnavailable)) throw error;
45
+ throw new AccError(EXIT.DATA, "store migration could not publish its complete index", { reason: error.reason });
46
+ }
47
+ if (fromVersion !== STORE_VERSION) {
48
+ await failAt?.("before-store-version-switch");
49
+ assertPublicationDeadline(deadlineAt);
50
+ await publishAtomic(identityPath(paths), encode({ ...identity, storeVersion: STORE_VERSION }),
51
+ { ...publishOptions, deadlineAt, replace: true, durability: "full" });
52
+ await failAt?.("after-store-version-switch");
53
+ }
54
+ return { fromVersion, toVersion: STORE_VERSION, migrated: fromVersion !== STORE_VERSION };
55
+ });
56
+ }