@indigoai-us/hq-cloud 6.16.39 → 6.16.41

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 (80) hide show
  1. package/dist/bin/sync-runner-heap-telemetry.d.ts +145 -9
  2. package/dist/bin/sync-runner-heap-telemetry.d.ts.map +1 -1
  3. package/dist/bin/sync-runner-heap-telemetry.js +178 -11
  4. package/dist/bin/sync-runner-heap-telemetry.js.map +1 -1
  5. package/dist/bin/sync-runner-heap-telemetry.test.js +511 -2
  6. package/dist/bin/sync-runner-heap-telemetry.test.js.map +1 -1
  7. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  8. package/dist/bin/sync-runner-watch-loop.js +20 -10
  9. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  10. package/dist/bin/sync-runner-watch-loop.test.js +21 -0
  11. package/dist/bin/sync-runner-watch-loop.test.js.map +1 -1
  12. package/dist/bin/sync-runner.d.ts +16 -5
  13. package/dist/bin/sync-runner.d.ts.map +1 -1
  14. package/dist/bin/sync-runner.js.map +1 -1
  15. package/dist/cli/share.d.ts.map +1 -1
  16. package/dist/cli/share.js +27 -28
  17. package/dist/cli/share.js.map +1 -1
  18. package/dist/cli/share.test.js +0 -49
  19. package/dist/cli/share.test.js.map +1 -1
  20. package/dist/cli/sync.d.ts.map +1 -1
  21. package/dist/cli/sync.js +8 -8
  22. package/dist/cli/sync.js.map +1 -1
  23. package/dist/journal-area-ledger-read-boundary.test.js +12 -46
  24. package/dist/journal-area-ledger-read-boundary.test.js.map +1 -1
  25. package/dist/journal-record-proxy.d.ts +2 -12
  26. package/dist/journal-record-proxy.d.ts.map +1 -1
  27. package/dist/journal-record-proxy.js +1 -8
  28. package/dist/journal-record-proxy.js.map +1 -1
  29. package/dist/journal-row-store.d.ts +0 -64
  30. package/dist/journal-row-store.d.ts.map +1 -1
  31. package/dist/journal-row-store.js +3 -129
  32. package/dist/journal-row-store.js.map +1 -1
  33. package/dist/journal-row-store.test.js +1 -3
  34. package/dist/journal-row-store.test.js.map +1 -1
  35. package/dist/journal-snapshot-rows.d.ts +1 -17
  36. package/dist/journal-snapshot-rows.d.ts.map +1 -1
  37. package/dist/journal-snapshot-rows.js +0 -18
  38. package/dist/journal-snapshot-rows.js.map +1 -1
  39. package/dist/journal-snapshot-v4.d.ts +2 -3
  40. package/dist/journal-snapshot-v4.d.ts.map +1 -1
  41. package/dist/journal-snapshot-v4.js +3 -9
  42. package/dist/journal-snapshot-v4.js.map +1 -1
  43. package/dist/journal-snapshot-v4.test.js +0 -1
  44. package/dist/journal-snapshot-v4.test.js.map +1 -1
  45. package/dist/journal.d.ts +5 -23
  46. package/dist/journal.d.ts.map +1 -1
  47. package/dist/journal.js +189 -309
  48. package/dist/journal.js.map +1 -1
  49. package/dist/journal.test.js +1 -58
  50. package/dist/journal.test.js.map +1 -1
  51. package/dist/personal-vault.d.ts.map +1 -1
  52. package/dist/personal-vault.js +4 -6
  53. package/dist/personal-vault.js.map +1 -1
  54. package/dist/remote-pull.d.ts.map +1 -1
  55. package/dist/remote-pull.js +8 -9
  56. package/dist/remote-pull.js.map +1 -1
  57. package/dist/scope-shrink.js +2 -2
  58. package/dist/scope-shrink.js.map +1 -1
  59. package/dist/session-log-retention.d.ts.map +1 -1
  60. package/dist/session-log-retention.js +2 -2
  61. package/dist/session-log-retention.js.map +1 -1
  62. package/dist/sync/area-ledger-migration.d.ts +6 -13
  63. package/dist/sync/area-ledger-migration.d.ts.map +1 -1
  64. package/dist/sync/area-ledger-migration.js +19 -45
  65. package/dist/sync/area-ledger-migration.js.map +1 -1
  66. package/dist/sync/area-ledger-migration.test.js +0 -23
  67. package/dist/sync/area-ledger-migration.test.js.map +1 -1
  68. package/dist/sync/area-ledger.d.ts +1 -31
  69. package/dist/sync/area-ledger.d.ts.map +1 -1
  70. package/dist/sync/area-ledger.js +88 -385
  71. package/dist/sync/area-ledger.js.map +1 -1
  72. package/dist/sync/area-ledger.test.js +10 -123
  73. package/dist/sync/area-ledger.test.js.map +1 -1
  74. package/dist/types.d.ts +0 -2
  75. package/dist/types.d.ts.map +1 -1
  76. package/package.json +1 -1
  77. package/dist/packed-journal-phase2.test.d.ts +0 -2
  78. package/dist/packed-journal-phase2.test.d.ts.map +0 -1
  79. package/dist/packed-journal-phase2.test.js +0 -273
  80. package/dist/packed-journal-phase2.test.js.map +0 -1
package/dist/journal.js CHANGED
@@ -16,10 +16,9 @@ import * as fs from "fs";
16
16
  import * as os from "os";
17
17
  import * as path from "path";
18
18
  import * as crypto from "crypto";
19
- import { JournalRowOverlay, JournalRowStore, journalEntryFingerprint, } from "./journal-row-store.js";
19
+ import { JournalRowStore, journalEntryFingerprint } from "./journal-row-store.js";
20
20
  import { createJournalRecordProxy } from "./journal-record-proxy.js";
21
21
  import { decodeJournalSnapshot, decodedJournalRows } from "./journal-snapshot-decoder.js";
22
- import { forgetSharedAreaJournalRows } from "./journal-snapshot-rows.js";
23
22
  import { encodeV4JournalSnapshotChunks } from "./journal-snapshot-v4.js";
24
23
  import { toPosixKey } from "./posix-key.js";
25
24
  import { StateStore, StateStoreCorruptionError, SNAPSHOT_V4_MAGIC, } from "./sync/state-store.js";
@@ -52,73 +51,11 @@ const journalBaselines = new WeakMap();
52
51
  // the public journal object while letting writeJournal select its O(changes)
53
52
  // dirty-log path only for journals returned by readJournal.
54
53
  const storeBackedJournals = new WeakMap();
55
- /** Full-pass journals must not retain the post-cutover v3 rollback mirror between checkpoints. */
56
- const fullPassAreaJournals = new WeakSet();
57
- /**
58
- * Record-shaped journals are a public compatibility boundary. Internal callers
59
- * use this adapter so packed rows stay lazy through reads, scans and writes.
60
- * Plain records only reach it from legacy/public inputs, where the adapter
61
- * yields one row at a time rather than materialising a whole entry array.
62
- */
63
- class RecordJournalRows {
64
- files;
65
- constructor(files) {
66
- this.files = files;
67
- }
68
- get size() {
69
- let size = 0;
70
- for (const key in this.files) {
71
- if (Object.hasOwn(this.files, key))
72
- size += 1;
73
- }
74
- return size;
75
- }
76
- get(path) {
77
- return this.files[path];
78
- }
79
- has(path) {
80
- return Object.hasOwn(this.files, path);
81
- }
82
- set(path, entry) {
83
- this.files[path] = entry;
84
- }
85
- delete(path) {
86
- if (!Object.hasOwn(this.files, path))
87
- return false;
88
- delete this.files[path];
89
- return true;
90
- }
91
- *keys() {
92
- for (const key in this.files) {
93
- if (Object.hasOwn(this.files, key))
94
- yield key;
95
- }
96
- }
97
- *entries() {
98
- for (const key of this.keys()) {
99
- const entry = this.files[key];
100
- if (entry !== undefined)
101
- yield [key, entry];
102
- }
103
- }
104
- *values() {
105
- for (const [, entry] of this.entries())
106
- yield entry;
107
- }
108
- toRecord() {
109
- return this.files;
110
- }
111
- }
112
- /** Return the lazy internal row view for any journal without exposing its record proxy. */
113
- export function journalRows(journal) {
114
- const backing = storeBackedJournals.get(journal);
115
- if (backing)
116
- return backing.store;
117
- const decoded = decodedJournalRows(journal);
118
- if (decoded)
119
- return decoded.rows;
120
- return new RecordJournalRows(journal.files);
121
- }
54
+ // Full pulls need a plain, shallow files map so they can share frozen ledger
55
+ // rows. This separate brand gives that map the same O(changes) checkpoint
56
+ // behavior as the packed record proxy without projecting every row into a
57
+ // second JournalRowStore.
58
+ const mutationTrackedJournals = new WeakMap();
122
59
  // Area-authoritative reads still refresh every participating area so a cached
123
60
  // bridge observes committed writers in this and other processes. The expensive
124
61
  // part we can share is the bridge construction: it owns the lazy AreaLedger
@@ -132,11 +69,10 @@ function areaMigrationCacheKey(stateDir, slug) {
132
69
  function closeCachedAreaMigration(stateDir, slug) {
133
70
  const cacheKey = areaMigrationCacheKey(stateDir, slug);
134
71
  const cached = cachedAreaMigrations.get(cacheKey);
135
- if (cached) {
136
- cached.migration.dispose();
137
- cachedAreaMigrations.delete(cacheKey);
138
- }
139
- forgetSharedAreaJournalRows(cacheKey);
72
+ if (!cached)
73
+ return;
74
+ cached.migration.dispose();
75
+ cachedAreaMigrations.delete(cacheKey);
140
76
  }
141
77
  /**
142
78
  * Release this process's cached area bridge for a journal. The durable stores
@@ -154,7 +90,6 @@ export function releaseCachedAreaJournalMigrations() {
154
90
  for (const [cacheKey, cached] of cachedAreaMigrations) {
155
91
  cached.migration.dispose();
156
92
  cachedAreaMigrations.delete(cacheKey);
157
- forgetSharedAreaJournalRows(cacheKey);
158
93
  }
159
94
  }
160
95
  /** Path-free count exported solely for the runner heap census. */
@@ -167,8 +102,9 @@ export function journalCacheCensusSizes() {
167
102
  */
168
103
  export function buildJournalBaseline(journal) {
169
104
  const rows = new Map();
170
- for (const [key, entry] of journalRows(journal).entries())
171
- rows.set(key, entryFingerprint(entry));
105
+ const files = journal.files;
106
+ for (const key of Object.keys(files))
107
+ rows.set(key, entryFingerprint(files[key]));
172
108
  return {
173
109
  version: journal.version,
174
110
  lastSync: journal.lastSync,
@@ -180,7 +116,7 @@ function rememberJournalBaseline(journal) {
180
116
  journalBaselines.set(journal, buildJournalBaseline(journal));
181
117
  return journal;
182
118
  }
183
- function readStoreBackedJournal(journal, options = {}) {
119
+ function readStoreBackedJournal(journal) {
184
120
  const existing = storeBackedJournals.get(journal);
185
121
  if (existing) {
186
122
  // Snapshot decode starts the store's change log before WAL replay. The
@@ -193,32 +129,17 @@ function readStoreBackedJournal(journal, options = {}) {
193
129
  }
194
130
  const decoded = decodedJournalRows(journal);
195
131
  if (decoded) {
196
- if (options.privateOverlay) {
197
- const store = new JournalRowOverlay(decoded);
198
- const result = { ...journal, files: createJournalRecordProxy(store) };
199
- if (Object.hasOwn(journal, "pulls"))
200
- result.pulls = structuredClone(journal.pulls);
201
- storeBackedJournals.set(result, {
202
- store,
203
- version: result.version,
204
- lastSync: result.lastSync,
205
- pullsJson: JSON.stringify(result.pulls ?? []),
206
- keysNormalized: false,
207
- });
208
- return result;
209
- }
210
132
  decoded.enableChangeTracking();
211
133
  storeBackedJournals.set(journal, {
212
134
  store: decoded,
213
135
  version: journal.version,
214
136
  lastSync: journal.lastSync,
215
137
  pullsJson: JSON.stringify(journal.pulls ?? []),
216
- keysNormalized: false,
217
138
  });
218
139
  return journal;
219
140
  }
220
141
  const store = new JournalRowStore();
221
- for (const [key, entry] of journalRows(journal).entries())
142
+ for (const [key, entry] of Object.entries(journal.files))
222
143
  store.set(key, entry);
223
144
  // Loading establishes the read baseline; only caller mutations belong in the
224
145
  // write delta. The store remains change-tracked after this checkpoint.
@@ -233,10 +154,77 @@ function readStoreBackedJournal(journal, options = {}) {
233
154
  version: result.version,
234
155
  lastSync: result.lastSync,
235
156
  pullsJson: JSON.stringify(result.pulls ?? []),
236
- keysNormalized: false,
237
157
  });
238
158
  return result;
239
159
  }
160
+ function recordMutation(backing, path, hadPrevious, previous) {
161
+ if (backing.dirtyPaths.has(path))
162
+ return;
163
+ backing.dirtyPaths.add(path);
164
+ backing.dirtyLog.push({
165
+ path,
166
+ hadPrevious,
167
+ previousFingerprint: previous === undefined ? undefined : journalEntryFingerprint(previous),
168
+ });
169
+ }
170
+ function mutationTrackedFiles(backing) {
171
+ return new Proxy(backing.files, {
172
+ set(target, key, value) {
173
+ if (typeof key !== "string")
174
+ return Reflect.set(target, key, value);
175
+ const hadPrevious = Object.hasOwn(target, key);
176
+ const previous = target[key];
177
+ const written = Reflect.set(target, key, value);
178
+ if (written) {
179
+ recordMutation(backing, key, hadPrevious, previous);
180
+ if (toPosixKey(key) !== key)
181
+ backing.needsNormalization = true;
182
+ }
183
+ return written;
184
+ },
185
+ deleteProperty(target, key) {
186
+ if (typeof key !== "string")
187
+ return Reflect.deleteProperty(target, key);
188
+ const hadPrevious = Object.hasOwn(target, key);
189
+ const previous = target[key];
190
+ const deleted = Reflect.deleteProperty(target, key);
191
+ if (deleted && hadPrevious)
192
+ recordMutation(backing, key, true, previous);
193
+ return deleted;
194
+ },
195
+ defineProperty(target, key, descriptor) {
196
+ if (typeof key !== "string")
197
+ return Reflect.defineProperty(target, key, descriptor);
198
+ const hadPrevious = Object.hasOwn(target, key);
199
+ const previous = target[key];
200
+ const defined = Reflect.defineProperty(target, key, descriptor);
201
+ if (defined) {
202
+ recordMutation(backing, key, hadPrevious, previous);
203
+ if (toPosixKey(key) !== key)
204
+ backing.needsNormalization = true;
205
+ }
206
+ return defined;
207
+ },
208
+ });
209
+ }
210
+ function readMutationTrackedJournal(journal) {
211
+ const backing = {
212
+ files: journal.files,
213
+ dirtyLog: [],
214
+ dirtyPaths: new Set(),
215
+ needsNormalization: false,
216
+ version: journal.version,
217
+ lastSync: journal.lastSync,
218
+ pullsJson: JSON.stringify(journal.pulls ?? []),
219
+ };
220
+ journal.files = mutationTrackedFiles(backing);
221
+ mutationTrackedJournals.set(journal, backing);
222
+ // Area authority may originate from an old on-disk journal. Normalize its
223
+ // shallow pass map once at read time; any repaired keys are in this dirty log
224
+ // and will be durably emitted by the first checkpoint, not silently hidden.
225
+ migrateToV2(journal);
226
+ return journal;
227
+ }
240
228
  function journalDeltaFromDirtyLog(dirtyLog, getCurrent, lastSync, pullsJson, next) {
241
229
  const previousByPath = new Map();
242
230
  for (const change of dirtyLog) {
@@ -277,20 +265,29 @@ function journalDeltaFromDirtyLog(dirtyLog, getCurrent, lastSync, pullsJson, nex
277
265
  function journalDeltaFromStoreDirtyLog(backing, next) {
278
266
  return journalDeltaFromDirtyLog(backing.store.dirtyLogSnapshot(), (key) => backing.store.get(key), backing.lastSync, backing.pullsJson, next);
279
267
  }
268
+ function journalDeltaFromMutationLog(backing, next) {
269
+ return journalDeltaFromDirtyLog(backing.dirtyLog, (key) => backing.files[key], backing.lastSync, backing.pullsJson, next);
270
+ }
280
271
  function checkpointStoreBackedJournal(backing, journal) {
281
272
  backing.store.drainDirtyLog();
282
- backing.store.clearCommittedChanges?.();
283
273
  backing.version = journal.version;
284
274
  backing.lastSync = journal.lastSync;
285
275
  backing.pullsJson = JSON.stringify(journal.pulls ?? []);
286
276
  }
287
- function storeBackedJournalNeedsMigration(backing, journal) {
288
- return journal.version !== JOURNAL_VERSION_CURRENT ||
289
- !Array.isArray(journal.pulls) ||
290
- typeof journal.lastSync !== "string";
277
+ function checkpointMutationTrackedJournal(backing, journal) {
278
+ // The pass's append-only mutation log is drained here, after durable write,
279
+ // so a later download checkpoint only publishes rows it changed since this
280
+ // checkpoint rather than retaining a pass-long list.
281
+ backing.dirtyLog = [];
282
+ backing.dirtyPaths.clear();
283
+ backing.version = journal.version;
284
+ backing.lastSync = journal.lastSync;
285
+ backing.pullsJson = JSON.stringify(journal.pulls ?? []);
291
286
  }
292
287
  function journalDeltaPath(journal) {
293
- return storeBackedJournals.has(journal) ? "dirty-log" : "fingerprint";
288
+ return storeBackedJournals.has(journal) || mutationTrackedJournals.has(journal)
289
+ ? "dirty-log"
290
+ : "fingerprint";
294
291
  }
295
292
  /** Exposed only to pin write-path selection at the public boundary. */
296
293
  export function journalDeltaPathForTest(journal) {
@@ -389,6 +386,9 @@ export function journalDeltaForTest(journal) {
389
386
  const backing = storeBackedJournals.get(journal);
390
387
  if (backing)
391
388
  return journalDeltaFromStoreDirtyLog(backing, journal);
389
+ const mutationTracking = mutationTrackedJournals.get(journal);
390
+ if (mutationTracking)
391
+ return journalDeltaFromMutationLog(mutationTracking, journal);
392
392
  const baseline = journalBaselines.get(journal);
393
393
  if (!baseline)
394
394
  throw new Error("journalDeltaForTest: journal has no remembered baseline");
@@ -505,10 +505,10 @@ export function migratePersonalVaultJournal() {
505
505
  export function readJournal(slug) {
506
506
  const areaMigration = openAreaMigrationIfAuthoritative(slug);
507
507
  if (areaMigration) {
508
- // Area rows stay in their shared packed store. This record-compatible
509
- // overlay owns only the caller's writes, so the public boundary remains
510
- // mutable without deep-cloning the aggregate before every pass.
511
- return readStoreBackedJournal(areaMigration.readAreaJournalCached().journal, { privateOverlay: true });
508
+ // `readJournal` is the legacy mutable snapshot boundary. Direct area-ledger
509
+ // reads retain their frozen rows; only a caller that requested this mutable
510
+ // aggregate pays to detach it for a later writeJournal() diff.
511
+ return readStoreBackedJournal(structuredClone(areaMigration.readAreaJournalCached().journal));
512
512
  }
513
513
  return readStoreBackedJournal(readJournalAggregateWith(slug, areaMigration));
514
514
  }
@@ -516,10 +516,11 @@ export function readJournal(slug) {
516
516
  * Read the mutable outer shell required by a full pull without detaching every
517
517
  * authoritative-ledger row from its immutable source.
518
518
  *
519
- * A full pull only replaces or removes rows. It never edits an existing row,
520
- * so it can safely borrow the ledger's frozen packed rows and place only its
521
- * own changes in a private overlay. `writeJournal()` drains that overlay's
522
- * dirty log, avoiding a full-ledger fingerprint at every download checkpoint.
519
+ * A full pull only replaces or removes `files` keys. It never edits fields on
520
+ * an existing row, so it can safely share the ledger's deeply frozen entries
521
+ * while owning the map that it mutates. That map records only pass mutations,
522
+ * so `writeJournal()` emits an incremental delta without fingerprinting the
523
+ * whole ledger at each download checkpoint.
523
524
  *
524
525
  * Legacy journals deliberately keep `readJournal()`'s packed-record boundary.
525
526
  * That path has different ownership and dirty-log semantics, and does not make
@@ -531,11 +532,13 @@ export function readJournalForPass(slug) {
531
532
  const areaMigration = openAreaMigrationIfAuthoritative(slug);
532
533
  if (!areaMigration)
533
534
  return readJournal(slug);
534
- // The overlay owns only keys this pass changes; it borrows all untouched
535
- // packed rows from the cached aggregate and keeps the public record facade.
536
- const journal = readStoreBackedJournal(areaMigration.readAreaJournalCached().journal, { privateOverlay: true });
537
- fullPassAreaJournals.add(journal);
538
- return journal;
535
+ const cached = areaMigration.readAreaJournalCached().journal;
536
+ return readMutationTrackedJournal({
537
+ version: cached.version,
538
+ lastSync: cached.lastSync,
539
+ files: { ...cached.files },
540
+ pulls: structuredClone(cached.pulls ?? []),
541
+ });
539
542
  }
540
543
  /**
541
544
  * The live aggregate for `slug` without recording a delta baseline.
@@ -585,23 +588,21 @@ export function readJournalScoped(slug, keys, deleteScopeRoots = []) {
585
588
  return rememberJournalBaseline(areaMigration.readAreaJournalScoped(keys, deleteScopeRoots));
586
589
  }
587
590
  const full = readJournalAggregate(slug);
588
- const fullRows = journalRows(full);
589
591
  const files = {};
590
592
  const roots = new Set(deleteScopeRoots);
591
593
  if (roots.has("")) {
592
- for (const [key, entry] of fullRows.entries()) {
594
+ for (const [key, entry] of Object.entries(full.files)) {
593
595
  files[key] = structuredClone(entry);
594
596
  }
595
597
  }
596
598
  else {
597
599
  for (const key of new Set(keys)) {
598
- const entry = fullRows.get(key);
599
- if (entry === undefined)
600
+ if (!Object.hasOwn(full.files, key))
600
601
  continue;
601
- files[key] = structuredClone(entry);
602
+ files[key] = structuredClone(full.files[key]);
602
603
  }
603
604
  if (roots.size > 0) {
604
- for (const [key, entry] of fullRows.entries()) {
605
+ for (const [key, entry] of Object.entries(full.files)) {
605
606
  if (Object.hasOwn(files, key))
606
607
  continue;
607
608
  if (isUnderAnyRoot(key, roots))
@@ -811,14 +812,6 @@ export class JournalStore {
811
812
  store;
812
813
  areaMigration;
813
814
  files = new JournalRowStore({ trackChanges: false });
814
- usesSharedRows = false;
815
- /**
816
- * Pass-local rows that have not reached the shared projection yet. A missing
817
- * value represents a staged delete; every other value is a frozen copy of
818
- * the caller's entry, so mutating that entry after `set()` cannot alter what
819
- * this session commits.
820
- */
821
- overlay = new Map();
822
815
  version = JOURNAL_VERSION_CURRENT;
823
816
  lastSync = "";
824
817
  pulls = [];
@@ -839,87 +832,14 @@ export class JournalStore {
839
832
  }
840
833
  get(key) {
841
834
  this.assertOpen();
842
- if (this.overlay.has(key))
843
- return this.overlay.get(key);
844
835
  return this.files.get(key);
845
836
  }
846
837
  scanPrefix(prefix) {
847
838
  this.assertOpen();
848
- const rows = new Map(this.files.entriesForPrefix(prefix));
849
- const normalizedPrefix = prefix.replace(/\/+$/, "");
850
- for (const [key, entry] of this.overlay) {
851
- if (!isJournalStorePrefixMatch(key, normalizedPrefix))
852
- continue;
853
- if (entry === undefined)
854
- rows.delete(key);
855
- else
856
- rows.set(key, entry);
857
- }
858
- return Array.from(rows.entries());
839
+ return Array.from(this.files.entriesForPrefix(prefix));
859
840
  }
860
841
  rowCount() {
861
- if (this.disposed)
862
- return 0;
863
- let count = this.files.size;
864
- for (const [key, entry] of this.overlay) {
865
- const existsInSharedStore = this.files.has(key);
866
- if (entry === undefined) {
867
- if (existsInSharedStore)
868
- count -= 1;
869
- }
870
- else if (!existsInSharedStore) {
871
- count += 1;
872
- }
873
- }
874
- return count;
875
- }
876
- /** Stage an upsert in this session without exposing it to other sessions. */
877
- set(key, entry) {
878
- this.assertOpen();
879
- const ownedEntry = immutableCopy(entry);
880
- const sharedEntry = this.files.get(key);
881
- if (sharedEntry !== undefined && sameEntry(sharedEntry, ownedEntry)) {
882
- this.overlay.delete(key);
883
- return;
884
- }
885
- this.overlay.set(key, ownedEntry);
886
- }
887
- /** Stage a delete in this session. Returns false when no session-visible row exists. */
888
- delete(key) {
889
- this.assertOpen();
890
- if (this.overlay.has(key)) {
891
- if (this.overlay.get(key) === undefined)
892
- return false;
893
- if (!this.files.has(key)) {
894
- this.overlay.delete(key);
895
- return true;
896
- }
897
- }
898
- else if (!this.files.has(key)) {
899
- return false;
900
- }
901
- this.overlay.set(key, undefined);
902
- return true;
903
- }
904
- /** Persist this session's staged rows, then release its private overlay. */
905
- commit() {
906
- this.assertOpen();
907
- if (this.overlay.size === 0)
908
- return;
909
- const upserts = {};
910
- const deletes = [];
911
- for (const [key, entry] of this.overlay) {
912
- if (entry === undefined)
913
- deletes.push(key);
914
- else
915
- upserts[key] = entry;
916
- }
917
- this.applyDelta({ upserts, deletes });
918
- this.overlay.clear();
919
- }
920
- /** Test-only visibility into the bounded, caller-owned session state. */
921
- overlaySizeForTest() {
922
- return this.overlay.size;
842
+ return this.files.size;
923
843
  }
924
844
  pullHistory() {
925
845
  this.assertOpen();
@@ -1042,19 +962,15 @@ export class JournalStore {
1042
962
  replaceFromJournal(journal) {
1043
963
  const decoded = decodedJournalRows(journal);
1044
964
  if (decoded) {
1045
- if (!this.usesSharedRows && this.files !== decoded)
1046
- this.files.clear();
1047
965
  this.files = decoded;
1048
- this.usesSharedRows = true;
1049
966
  this.version = journal.version;
1050
967
  this.lastSync = journal.lastSync;
1051
968
  this.pulls = structuredClone(journal.pulls ?? []);
1052
969
  return;
1053
970
  }
1054
- if (this.usesSharedRows)
1055
- this.files = new JournalRowStore({ trackChanges: false });
1056
- this.files.replaceFromRecord(journal.files);
1057
- this.usesSharedRows = false;
971
+ this.files.clear();
972
+ for (const [key, entry] of Object.entries(journal.files))
973
+ this.files.set(key, entry);
1058
974
  this.version = journal.version;
1059
975
  this.lastSync = journal.lastSync;
1060
976
  this.pulls = structuredClone(journal.pulls ?? []);
@@ -1080,9 +996,7 @@ export class JournalStore {
1080
996
  if (this.disposed)
1081
997
  return;
1082
998
  this.disposed = true;
1083
- this.overlay.clear();
1084
- if (!this.usesSharedRows)
1085
- this.files.clear();
999
+ this.files.clear();
1086
1000
  this.store = undefined;
1087
1001
  this.areaMigration = undefined;
1088
1002
  this.pulls = [];
@@ -1092,9 +1006,6 @@ export class JournalStore {
1092
1006
  throw new Error(`journal: session for ${this.slug} is disposed`);
1093
1007
  }
1094
1008
  }
1095
- function isJournalStorePrefixMatch(key, normalizedPrefix) {
1096
- return normalizedPrefix === "" || key === normalizedPrefix || key.startsWith(`${normalizedPrefix}/`);
1097
- }
1098
1009
  /** Return a detached, deeply frozen JSON-shaped metadata value. */
1099
1010
  function immutableCopy(value) {
1100
1011
  const copy = structuredClone(value);
@@ -1500,70 +1411,22 @@ export function listJournals() {
1500
1411
  export function normalizeJournalKeys(journal) {
1501
1412
  if (!journal.files)
1502
1413
  return 0;
1503
- const rows = journalRows(journal);
1504
- const rewrites = [];
1505
1414
  let rewritten = 0;
1506
- // Preserve only malformed keys while scanning. This avoids an all-key array
1507
- // on a normal journal, where the common result is no rewrite at all.
1508
- for (const key of rows.keys()) {
1415
+ // Snapshot keys up front — we mutate journal.files during the walk.
1416
+ for (const key of Object.keys(journal.files)) {
1509
1417
  const posix = toPosixKey(key);
1510
1418
  if (posix === key)
1511
1419
  continue; // already canonical (no backslash)
1512
- const entry = rows.get(key);
1513
- if (entry !== undefined)
1514
- rewrites.push({ key, posix, entry });
1515
- }
1516
- for (const { key, posix, entry } of rewrites) {
1517
- rows.delete(key);
1420
+ const entry = journal.files[key];
1421
+ delete journal.files[key];
1518
1422
  rewritten++;
1519
1423
  // POSIX twin already present → it wins; drop the malformed duplicate.
1520
- if (!rows.has(posix))
1521
- rows.set(posix, entry);
1424
+ if (!(posix in journal.files)) {
1425
+ journal.files[posix] = entry;
1426
+ }
1522
1427
  }
1523
1428
  return rewritten;
1524
1429
  }
1525
- /**
1526
- * Normalize a store-backed journal without repeatedly walking its immutable
1527
- * packed base. The first checkpoint verifies every inherited key because old
1528
- * state may already contain a Windows-shaped path. Once that pass succeeds,
1529
- * only keys named by the dirty log can introduce a new malformed spelling.
1530
- */
1531
- function normalizeStoreBackedJournalKeys(backing, journal) {
1532
- if (!backing.keysNormalized) {
1533
- const rewritten = normalizeJournalKeys(journal);
1534
- backing.keysNormalized = true;
1535
- return rewritten;
1536
- }
1537
- let rewritten = 0;
1538
- const rows = backing.store;
1539
- const dirtyPaths = new Set(backing.store.dirtyLogSnapshot().map((change) => change.path));
1540
- for (const key of dirtyPaths) {
1541
- const posix = toPosixKey(key);
1542
- if (posix === key)
1543
- continue;
1544
- const entry = rows.get(key);
1545
- // A delete of a malformed key already leaves no non-canonical row. A
1546
- // canonical twin wins when both were changed in one checkpoint.
1547
- if (entry === undefined)
1548
- continue;
1549
- rows.delete(key);
1550
- rewritten++;
1551
- if (!rows.has(posix))
1552
- rows.set(posix, entry);
1553
- }
1554
- return rewritten;
1555
- }
1556
- function migrateJournalShapeToV2(journal) {
1557
- if (journal.version === "2" && Array.isArray(journal.pulls) && typeof journal.lastSync === "string") {
1558
- return journal;
1559
- }
1560
- journal.version = JOURNAL_VERSION_CURRENT;
1561
- if (typeof journal.lastSync !== "string")
1562
- journal.lastSync = "";
1563
- if (!Array.isArray(journal.pulls))
1564
- journal.pulls = [];
1565
- return journal;
1566
- }
1567
1430
  /**
1568
1431
  * Coerce any-version journal into a v2 shape. Idempotent for v2 inputs.
1569
1432
  * Mutates the input and returns it for chainable use. Call this immediately
@@ -1579,7 +1442,15 @@ export function migrateToV2(journal) {
1579
1442
  // already carry version "2" (the landmine predates the schema bump), so this
1580
1443
  // must precede the v2 early-return or already-v2 journals stay poisoned.
1581
1444
  normalizeJournalKeys(journal);
1582
- return migrateJournalShapeToV2(journal);
1445
+ if (journal.version === "2" && Array.isArray(journal.pulls) && typeof journal.lastSync === "string") {
1446
+ return journal;
1447
+ }
1448
+ journal.version = JOURNAL_VERSION_CURRENT;
1449
+ if (typeof journal.lastSync !== "string")
1450
+ journal.lastSync = "";
1451
+ if (!Array.isArray(journal.pulls))
1452
+ journal.pulls = [];
1453
+ return journal;
1583
1454
  }
1584
1455
  /**
1585
1456
  * Write a journal to disk, migrating to the current schema version in-place.
@@ -1588,31 +1459,36 @@ export function migrateToV2(journal) {
1588
1459
  */
1589
1460
  export function writeJournal(slug, journal) {
1590
1461
  const storeBacking = storeBackedJournals.get(journal);
1591
- if (storeBacking) {
1592
- // The first checkpoint repairs inherited poison. Afterwards the dirty log
1593
- // is the complete set of newly introduced keys, so stay O(changes).
1594
- normalizeStoreBackedJournalKeys(storeBacking, journal);
1595
- if (storeBackedJournalNeedsMigration(storeBacking, journal))
1596
- migrateJournalShapeToV2(journal);
1597
- }
1598
- else {
1462
+ const mutationTracking = mutationTrackedJournals.get(journal);
1463
+ // A full-pull journal was normalized when its shallow tracked map was
1464
+ // opened. Re-running normalizeJournalKeys here would enumerate every row at
1465
+ // each 10k download checkpoint. Preserve writeJournal's general migration
1466
+ // contract when metadata changes shape or a pass introduces a non-POSIX key.
1467
+ const mutationTrackingNeedsMigration = mutationTracking !== undefined && (mutationTracking.needsNormalization ||
1468
+ journal.version !== JOURNAL_VERSION_CURRENT ||
1469
+ !Array.isArray(journal.pulls) ||
1470
+ typeof journal.lastSync !== "string");
1471
+ if (!mutationTracking || mutationTrackingNeedsMigration) {
1599
1472
  migrateToV2(journal);
1473
+ if (mutationTracking)
1474
+ mutationTracking.needsNormalization = false;
1600
1475
  }
1601
1476
  const areaMigration = openAreaMigrationIfAuthoritative(slug);
1602
1477
  if (areaMigration) {
1603
1478
  const baseline = journalBaselines.get(journal);
1604
1479
  const delta = storeBacking
1605
1480
  ? journalDeltaFromStoreDirtyLog(storeBacking, journal)
1606
- : baseline
1607
- ? journalDeltaFromBaseline(baseline, journal)
1608
- : journalDelta(areaMigration.readAreaJournal(), journal);
1609
- if (hasJournalDelta(delta)) {
1610
- areaMigration.reverseShadow(toAreaLedgerDelta(delta), {
1611
- releaseLegacyStoreImmediately: fullPassAreaJournals.has(journal),
1612
- });
1613
- }
1481
+ : mutationTracking
1482
+ ? journalDeltaFromMutationLog(mutationTracking, journal)
1483
+ : baseline
1484
+ ? journalDeltaFromBaseline(baseline, journal)
1485
+ : journalDelta(areaMigration.readAreaJournal(), journal);
1486
+ if (hasJournalDelta(delta))
1487
+ areaMigration.reverseShadow(toAreaLedgerDelta(delta));
1614
1488
  if (storeBacking)
1615
1489
  checkpointStoreBackedJournal(storeBacking, journal);
1490
+ else if (mutationTracking)
1491
+ checkpointMutationTrackedJournal(mutationTracking, journal);
1616
1492
  else
1617
1493
  rememberJournalBaseline(journal);
1618
1494
  ensureJournalLocator(slug);
@@ -1624,6 +1500,8 @@ export function writeJournal(slug, journal) {
1624
1500
  // and there is no delta to record. Only the locator is missing.
1625
1501
  if (storeBacking)
1626
1502
  checkpointStoreBackedJournal(storeBacking, journal);
1503
+ else if (mutationTracking)
1504
+ checkpointMutationTrackedJournal(mutationTracking, journal);
1627
1505
  writeJournalLocator(slug);
1628
1506
  return;
1629
1507
  }
@@ -1644,14 +1522,18 @@ export function writeJournal(slug, journal) {
1644
1522
  const baseline = journalBaselines.get(journal);
1645
1523
  const delta = storeBacking
1646
1524
  ? journalDeltaFromStoreDirtyLog(storeBacking, journal)
1647
- : baseline
1648
- ? journalDeltaFromBaseline(baseline, journal)
1649
- : journalDelta(store.getState().journal, journal);
1525
+ : mutationTracking
1526
+ ? journalDeltaFromMutationLog(mutationTracking, journal)
1527
+ : baseline
1528
+ ? journalDeltaFromBaseline(baseline, journal)
1529
+ : journalDelta(store.getState().journal, journal);
1650
1530
  store.append(JOURNAL_DELTA_RECORD_TYPE, delta);
1651
1531
  // A caller can checkpoint more than once during one pass. Advancing only its
1652
1532
  // own tracking state means the next checkpoint again publishes only new mutations.
1653
1533
  if (storeBacking)
1654
1534
  checkpointStoreBackedJournal(storeBacking, journal);
1535
+ else if (mutationTracking)
1536
+ checkpointMutationTrackedJournal(mutationTracking, journal);
1655
1537
  else
1656
1538
  rememberJournalBaseline(journal);
1657
1539
  // The locator is immutable content, so it only needs writing if it is absent
@@ -1667,10 +1549,8 @@ export function writeJournal(slug, journal) {
1667
1549
  */
1668
1550
  function journalDelta(previous, next) {
1669
1551
  const upserts = {};
1670
- const previousRows = journalRows(previous);
1671
- const nextRows = journalRows(next);
1672
- for (const [key, entry] of nextRows.entries()) {
1673
- const before = previousRows.get(key);
1552
+ for (const [key, entry] of Object.entries(next.files)) {
1553
+ const before = previous.files[key];
1674
1554
  // Value comparison, not identity: `previous` was just deserialized from the
1675
1555
  // snapshot on disk, so nothing in it shares a reference with the caller's
1676
1556
  // journal and an identity check would mark every file changed.
@@ -1679,8 +1559,8 @@ function journalDelta(previous, next) {
1679
1559
  upserts[key] = entry;
1680
1560
  }
1681
1561
  const deletes = [];
1682
- for (const key of previousRows.keys()) {
1683
- if (!nextRows.has(key))
1562
+ for (const key of Object.keys(previous.files)) {
1563
+ if (!(key in next.files))
1684
1564
  deletes.push(key);
1685
1565
  }
1686
1566
  const delta = {
@@ -2029,7 +1909,7 @@ export function updateEntry(journal, relativePath, hash, size, direction, verify
2029
1909
  if (skillMetadataPending === true) {
2030
1910
  entry.skillMetadataPending = true;
2031
1911
  }
2032
- journalRows(journal).set(relativePath, entry);
1912
+ journal.files[relativePath] = entry;
2033
1913
  journal.lastSync = new Date().toISOString();
2034
1914
  }
2035
1915
  /**
@@ -2043,10 +1923,10 @@ export function normalizeEtag(etag) {
2043
1923
  return etag.replace(/^"|"$/g, "");
2044
1924
  }
2045
1925
  export function getEntry(journal, relativePath) {
2046
- return journalRows(journal).get(relativePath);
1926
+ return journal.files[relativePath];
2047
1927
  }
2048
1928
  export function removeEntry(journal, relativePath) {
2049
- journalRows(journal).delete(relativePath);
1929
+ delete journal.files[relativePath];
2050
1930
  }
2051
1931
  // ─── Journal v2 (US-005): pulls, tombstones, GC ─────────────────────────────
2052
1932
  /** Crockford base32 alphabet (ULID-compatible). */
@@ -2147,10 +2027,10 @@ export function appendPullRecord(journal, record) {
2147
2027
  * They are GC'd after `TOMBSTONE_TTL_MS` via `gcTombstones`.
2148
2028
  */
2149
2029
  export function tombstoneEntry(journal, relativePath, reason, now = new Date().toISOString()) {
2150
- const entry = journalRows(journal).get(relativePath);
2030
+ const entry = journal.files[relativePath];
2151
2031
  if (!entry)
2152
2032
  return;
2153
- journalRows(journal).set(relativePath, { ...entry, removedAt: now, removedReason: reason });
2033
+ journal.files[relativePath] = { ...entry, removedAt: now, removedReason: reason };
2154
2034
  }
2155
2035
  /**
2156
2036
  * Mark a journal entry for a user-authorized local delete. Callers must
@@ -2158,14 +2038,14 @@ export function tombstoneEntry(journal, relativePath, reason, now = new Date().t
2158
2038
  * alone is never deletion intent.
2159
2039
  */
2160
2040
  export function markLocalDeleteIntent(journal, relativePath, localHash, localKind, expectedRemoteEtag) {
2161
- const entry = journalRows(journal).get(relativePath);
2041
+ const entry = journal.files[relativePath];
2162
2042
  if (!entry?.remoteEtag ||
2163
2043
  entry.remoteEtag !== normalizeEtag(expectedRemoteEtag) ||
2164
2044
  entry.hash !== localHash ||
2165
2045
  entry.kind !== localKind) {
2166
2046
  return false;
2167
2047
  }
2168
- journalRows(journal).set(relativePath, {
2048
+ journal.files[relativePath] = {
2169
2049
  ...entry,
2170
2050
  localDeleteIntent: {
2171
2051
  version: 1,
@@ -2173,16 +2053,16 @@ export function markLocalDeleteIntent(journal, relativePath, localHash, localKin
2173
2053
  localHash,
2174
2054
  localKind,
2175
2055
  },
2176
- });
2056
+ };
2177
2057
  return true;
2178
2058
  }
2179
2059
  /** Invalidate a prior delete authorization after a watcher-observed recreate. */
2180
2060
  export function clearLocalDeleteIntent(journal, relativePath) {
2181
- const entry = journalRows(journal).get(relativePath);
2061
+ const entry = journal.files[relativePath];
2182
2062
  if (!entry?.localDeleteIntent)
2183
2063
  return false;
2184
2064
  const { localDeleteIntent: _localDeleteIntent, ...cleared } = entry;
2185
- journalRows(journal).set(relativePath, cleared);
2065
+ journal.files[relativePath] = cleared;
2186
2066
  return true;
2187
2067
  }
2188
2068
  /** True if the entry is a tombstone (set by `tombstoneEntry`). */
@@ -2197,14 +2077,14 @@ export function isTombstone(entry) {
2197
2077
  */
2198
2078
  export function gcTombstones(journal, now = Date.now()) {
2199
2079
  let removed = 0;
2200
- for (const [path, entry] of journalRows(journal).entries()) {
2080
+ for (const [path, entry] of Object.entries(journal.files)) {
2201
2081
  if (!entry.removedAt)
2202
2082
  continue;
2203
2083
  const removedTime = Date.parse(entry.removedAt);
2204
2084
  if (Number.isNaN(removedTime))
2205
2085
  continue;
2206
2086
  if (now - removedTime > TOMBSTONE_TTL_MS) {
2207
- journalRows(journal).delete(path);
2087
+ delete journal.files[path];
2208
2088
  removed++;
2209
2089
  }
2210
2090
  }