@indigoai-us/hq-cloud 6.16.37 → 6.16.39

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 (154) hide show
  1. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  2. package/dist/bin/sync-runner-watch-loop.js +9 -9
  3. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  4. package/dist/bin/sync-runner.test.js +3 -3
  5. package/dist/bin/sync-runner.test.js.map +1 -1
  6. package/dist/cli/share.d.ts.map +1 -1
  7. package/dist/cli/share.js +28 -27
  8. package/dist/cli/share.js.map +1 -1
  9. package/dist/cli/share.test.js +53 -0
  10. package/dist/cli/share.test.js.map +1 -1
  11. package/dist/cli/sync.d.ts.map +1 -1
  12. package/dist/cli/sync.js +9 -9
  13. package/dist/cli/sync.js.map +1 -1
  14. package/dist/journal-area-ledger-read-boundary.test.d.ts +2 -0
  15. package/dist/journal-area-ledger-read-boundary.test.d.ts.map +1 -0
  16. package/dist/journal-area-ledger-read-boundary.test.js +420 -0
  17. package/dist/journal-area-ledger-read-boundary.test.js.map +1 -0
  18. package/dist/journal-record-proxy.d.ts +12 -2
  19. package/dist/journal-record-proxy.d.ts.map +1 -1
  20. package/dist/journal-record-proxy.js +8 -1
  21. package/dist/journal-record-proxy.js.map +1 -1
  22. package/dist/journal-row-store.d.ts +64 -0
  23. package/dist/journal-row-store.d.ts.map +1 -1
  24. package/dist/journal-row-store.js +144 -8
  25. package/dist/journal-row-store.js.map +1 -1
  26. package/dist/journal-row-store.test.js +150 -1
  27. package/dist/journal-row-store.test.js.map +1 -1
  28. package/dist/journal-snapshot-memory-helpers.d.ts +3 -0
  29. package/dist/journal-snapshot-memory-helpers.d.ts.map +1 -1
  30. package/dist/journal-snapshot-memory-helpers.js +3 -3
  31. package/dist/journal-snapshot-memory-helpers.js.map +1 -1
  32. package/dist/journal-snapshot-rows.d.ts +17 -1
  33. package/dist/journal-snapshot-rows.d.ts.map +1 -1
  34. package/dist/journal-snapshot-rows.js +18 -0
  35. package/dist/journal-snapshot-rows.js.map +1 -1
  36. package/dist/journal-snapshot-v4.d.ts +3 -2
  37. package/dist/journal-snapshot-v4.d.ts.map +1 -1
  38. package/dist/journal-snapshot-v4.js +9 -3
  39. package/dist/journal-snapshot-v4.js.map +1 -1
  40. package/dist/journal-snapshot-v4.test.js +1 -0
  41. package/dist/journal-snapshot-v4.test.js.map +1 -1
  42. package/dist/journal.d.ts +35 -0
  43. package/dist/journal.d.ts.map +1 -1
  44. package/dist/journal.js +342 -66
  45. package/dist/journal.js.map +1 -1
  46. package/dist/journal.test.js +58 -1
  47. package/dist/journal.test.js.map +1 -1
  48. package/dist/manifest/build-manifest-resident-parity.test.d.ts +2 -0
  49. package/dist/manifest/build-manifest-resident-parity.test.d.ts.map +1 -0
  50. package/dist/manifest/build-manifest-resident-parity.test.js +68 -0
  51. package/dist/manifest/build-manifest-resident-parity.test.js.map +1 -0
  52. package/dist/manifest/build-manifest.bench.test.js +262 -2
  53. package/dist/manifest/build-manifest.bench.test.js.map +1 -1
  54. package/dist/manifest/build-manifest.d.ts +15 -0
  55. package/dist/manifest/build-manifest.d.ts.map +1 -1
  56. package/dist/manifest/build-manifest.js +39 -8
  57. package/dist/manifest/build-manifest.js.map +1 -1
  58. package/dist/manifest/build-manifest.test.js +8 -0
  59. package/dist/manifest/build-manifest.test.js.map +1 -1
  60. package/dist/outposts/command.d.ts +1 -0
  61. package/dist/outposts/command.d.ts.map +1 -1
  62. package/dist/outposts/command.js +6 -0
  63. package/dist/outposts/command.js.map +1 -1
  64. package/dist/outposts/session-host-auth.d.ts +70 -0
  65. package/dist/outposts/session-host-auth.d.ts.map +1 -0
  66. package/dist/outposts/session-host-auth.js +166 -0
  67. package/dist/outposts/session-host-auth.js.map +1 -0
  68. package/dist/outposts/session-host-auth.test.d.ts +16 -0
  69. package/dist/outposts/session-host-auth.test.d.ts.map +1 -0
  70. package/dist/outposts/session-host-auth.test.js +284 -0
  71. package/dist/outposts/session-host-auth.test.js.map +1 -0
  72. package/dist/outposts/session-host-command.d.ts +42 -0
  73. package/dist/outposts/session-host-command.d.ts.map +1 -0
  74. package/dist/outposts/session-host-command.js +223 -0
  75. package/dist/outposts/session-host-command.js.map +1 -0
  76. package/dist/outposts/session-host-command.test.d.ts +10 -0
  77. package/dist/outposts/session-host-command.test.d.ts.map +1 -0
  78. package/dist/outposts/session-host-command.test.js +98 -0
  79. package/dist/outposts/session-host-command.test.js.map +1 -0
  80. package/dist/outposts/session-host-connection.d.ts +44 -0
  81. package/dist/outposts/session-host-connection.d.ts.map +1 -0
  82. package/dist/outposts/session-host-connection.js +227 -0
  83. package/dist/outposts/session-host-connection.js.map +1 -0
  84. package/dist/outposts/session-host-connection.test.d.ts +18 -0
  85. package/dist/outposts/session-host-connection.test.d.ts.map +1 -0
  86. package/dist/outposts/session-host-connection.test.js +392 -0
  87. package/dist/outposts/session-host-connection.test.js.map +1 -0
  88. package/dist/outposts/session-host-fakes.d.ts +61 -0
  89. package/dist/outposts/session-host-fakes.d.ts.map +1 -0
  90. package/dist/outposts/session-host-fakes.js +115 -0
  91. package/dist/outposts/session-host-fakes.js.map +1 -0
  92. package/dist/outposts/session-host-ndjson.d.ts +30 -0
  93. package/dist/outposts/session-host-ndjson.d.ts.map +1 -0
  94. package/dist/outposts/session-host-ndjson.js +64 -0
  95. package/dist/outposts/session-host-ndjson.js.map +1 -0
  96. package/dist/outposts/session-host-ndjson.test.d.ts +11 -0
  97. package/dist/outposts/session-host-ndjson.test.d.ts.map +1 -0
  98. package/dist/outposts/session-host-ndjson.test.js +73 -0
  99. package/dist/outposts/session-host-ndjson.test.js.map +1 -0
  100. package/dist/outposts/session-host-protocol.d.ts +137 -0
  101. package/dist/outposts/session-host-protocol.d.ts.map +1 -0
  102. package/dist/outposts/session-host-protocol.js +244 -0
  103. package/dist/outposts/session-host-protocol.js.map +1 -0
  104. package/dist/outposts/session-host-protocol.test.d.ts +15 -0
  105. package/dist/outposts/session-host-protocol.test.d.ts.map +1 -0
  106. package/dist/outposts/session-host-protocol.test.js +0 -0
  107. package/dist/outposts/session-host-protocol.test.js.map +1 -0
  108. package/dist/outposts/session-host-server.d.ts +25 -0
  109. package/dist/outposts/session-host-server.d.ts.map +1 -0
  110. package/dist/outposts/session-host-server.js +66 -0
  111. package/dist/outposts/session-host-server.js.map +1 -0
  112. package/dist/outposts/session-host-server.test.d.ts +10 -0
  113. package/dist/outposts/session-host-server.test.d.ts.map +1 -0
  114. package/dist/outposts/session-host-server.test.js +118 -0
  115. package/dist/outposts/session-host-server.test.js.map +1 -0
  116. package/dist/outposts/session-host-sessions.d.ts +144 -0
  117. package/dist/outposts/session-host-sessions.d.ts.map +1 -0
  118. package/dist/outposts/session-host-sessions.js +336 -0
  119. package/dist/outposts/session-host-sessions.js.map +1 -0
  120. package/dist/outposts/session-host-sessions.test.d.ts +18 -0
  121. package/dist/outposts/session-host-sessions.test.d.ts.map +1 -0
  122. package/dist/outposts/session-host-sessions.test.js +439 -0
  123. package/dist/outposts/session-host-sessions.test.js.map +1 -0
  124. package/dist/packed-journal-phase2.test.d.ts +2 -0
  125. package/dist/packed-journal-phase2.test.d.ts.map +1 -0
  126. package/dist/packed-journal-phase2.test.js +273 -0
  127. package/dist/packed-journal-phase2.test.js.map +1 -0
  128. package/dist/personal-vault.d.ts.map +1 -1
  129. package/dist/personal-vault.js +6 -4
  130. package/dist/personal-vault.js.map +1 -1
  131. package/dist/remote-pull.d.ts.map +1 -1
  132. package/dist/remote-pull.js +9 -8
  133. package/dist/remote-pull.js.map +1 -1
  134. package/dist/scope-shrink.js +2 -2
  135. package/dist/scope-shrink.js.map +1 -1
  136. package/dist/session-log-retention.d.ts.map +1 -1
  137. package/dist/session-log-retention.js +2 -2
  138. package/dist/session-log-retention.js.map +1 -1
  139. package/dist/sync/area-ledger-migration.d.ts +13 -6
  140. package/dist/sync/area-ledger-migration.d.ts.map +1 -1
  141. package/dist/sync/area-ledger-migration.js +50 -16
  142. package/dist/sync/area-ledger-migration.js.map +1 -1
  143. package/dist/sync/area-ledger-migration.test.js +23 -0
  144. package/dist/sync/area-ledger-migration.test.js.map +1 -1
  145. package/dist/sync/area-ledger.d.ts +31 -1
  146. package/dist/sync/area-ledger.d.ts.map +1 -1
  147. package/dist/sync/area-ledger.js +385 -88
  148. package/dist/sync/area-ledger.js.map +1 -1
  149. package/dist/sync/area-ledger.test.js +127 -10
  150. package/dist/sync/area-ledger.test.js.map +1 -1
  151. package/dist/types.d.ts +2 -0
  152. package/dist/types.d.ts.map +1 -1
  153. package/dist/watcher.test.js.map +1 -1
  154. package/package.json +3 -3
package/dist/journal.js CHANGED
@@ -16,9 +16,10 @@ 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 { JournalRowStore, journalEntryFingerprint } from "./journal-row-store.js";
19
+ import { JournalRowOverlay, 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";
22
23
  import { encodeV4JournalSnapshotChunks } from "./journal-snapshot-v4.js";
23
24
  import { toPosixKey } from "./posix-key.js";
24
25
  import { StateStore, StateStoreCorruptionError, SNAPSHOT_V4_MAGIC, } from "./sync/state-store.js";
@@ -51,6 +52,73 @@ const journalBaselines = new WeakMap();
51
52
  // the public journal object while letting writeJournal select its O(changes)
52
53
  // dirty-log path only for journals returned by readJournal.
53
54
  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
122
  // Area-authoritative reads still refresh every participating area so a cached
55
123
  // bridge observes committed writers in this and other processes. The expensive
56
124
  // part we can share is the bridge construction: it owns the lazy AreaLedger
@@ -64,10 +132,11 @@ function areaMigrationCacheKey(stateDir, slug) {
64
132
  function closeCachedAreaMigration(stateDir, slug) {
65
133
  const cacheKey = areaMigrationCacheKey(stateDir, slug);
66
134
  const cached = cachedAreaMigrations.get(cacheKey);
67
- if (!cached)
68
- return;
69
- cached.migration.dispose();
70
- cachedAreaMigrations.delete(cacheKey);
135
+ if (cached) {
136
+ cached.migration.dispose();
137
+ cachedAreaMigrations.delete(cacheKey);
138
+ }
139
+ forgetSharedAreaJournalRows(cacheKey);
71
140
  }
72
141
  /**
73
142
  * Release this process's cached area bridge for a journal. The durable stores
@@ -85,6 +154,7 @@ export function releaseCachedAreaJournalMigrations() {
85
154
  for (const [cacheKey, cached] of cachedAreaMigrations) {
86
155
  cached.migration.dispose();
87
156
  cachedAreaMigrations.delete(cacheKey);
157
+ forgetSharedAreaJournalRows(cacheKey);
88
158
  }
89
159
  }
90
160
  /** Path-free count exported solely for the runner heap census. */
@@ -97,9 +167,8 @@ export function journalCacheCensusSizes() {
97
167
  */
98
168
  export function buildJournalBaseline(journal) {
99
169
  const rows = new Map();
100
- const files = journal.files;
101
- for (const key of Object.keys(files))
102
- rows.set(key, entryFingerprint(files[key]));
170
+ for (const [key, entry] of journalRows(journal).entries())
171
+ rows.set(key, entryFingerprint(entry));
103
172
  return {
104
173
  version: journal.version,
105
174
  lastSync: journal.lastSync,
@@ -111,7 +180,7 @@ function rememberJournalBaseline(journal) {
111
180
  journalBaselines.set(journal, buildJournalBaseline(journal));
112
181
  return journal;
113
182
  }
114
- function readStoreBackedJournal(journal) {
183
+ function readStoreBackedJournal(journal, options = {}) {
115
184
  const existing = storeBackedJournals.get(journal);
116
185
  if (existing) {
117
186
  // Snapshot decode starts the store's change log before WAL replay. The
@@ -124,17 +193,32 @@ function readStoreBackedJournal(journal) {
124
193
  }
125
194
  const decoded = decodedJournalRows(journal);
126
195
  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
+ }
127
210
  decoded.enableChangeTracking();
128
211
  storeBackedJournals.set(journal, {
129
212
  store: decoded,
130
213
  version: journal.version,
131
214
  lastSync: journal.lastSync,
132
215
  pullsJson: JSON.stringify(journal.pulls ?? []),
216
+ keysNormalized: false,
133
217
  });
134
218
  return journal;
135
219
  }
136
220
  const store = new JournalRowStore();
137
- for (const [key, entry] of Object.entries(journal.files))
221
+ for (const [key, entry] of journalRows(journal).entries())
138
222
  store.set(key, entry);
139
223
  // Loading establishes the read baseline; only caller mutations belong in the
140
224
  // write delta. The store remains change-tracked after this checkpoint.
@@ -149,12 +233,13 @@ function readStoreBackedJournal(journal) {
149
233
  version: result.version,
150
234
  lastSync: result.lastSync,
151
235
  pullsJson: JSON.stringify(result.pulls ?? []),
236
+ keysNormalized: false,
152
237
  });
153
238
  return result;
154
239
  }
155
- function journalDeltaFromDirtyLog(backing, next) {
240
+ function journalDeltaFromDirtyLog(dirtyLog, getCurrent, lastSync, pullsJson, next) {
156
241
  const previousByPath = new Map();
157
- for (const change of backing.store.dirtyLogSnapshot()) {
242
+ for (const change of dirtyLog) {
158
243
  if (!previousByPath.has(change.path)) {
159
244
  previousByPath.set(change.path, {
160
245
  hadPrevious: change.hadPrevious,
@@ -165,7 +250,7 @@ function journalDeltaFromDirtyLog(backing, next) {
165
250
  const upserts = {};
166
251
  const deletes = [];
167
252
  for (const [key, previous] of previousByPath) {
168
- const current = backing.store.get(key);
253
+ const current = getCurrent(key);
169
254
  if (!previous.hadPrevious) {
170
255
  if (current !== undefined)
171
256
  upserts[key] = current;
@@ -183,18 +268,27 @@ function journalDeltaFromDirtyLog(backing, next) {
183
268
  upserts,
184
269
  deletes,
185
270
  };
186
- if (backing.lastSync !== next.lastSync)
271
+ if (lastSync !== next.lastSync)
187
272
  delta.lastSync = next.lastSync;
188
- if (JSON.stringify(next.pulls ?? []) !== backing.pullsJson)
273
+ if (JSON.stringify(next.pulls ?? []) !== pullsJson)
189
274
  delta.pulls = next.pulls ?? [];
190
275
  return delta;
191
276
  }
277
+ function journalDeltaFromStoreDirtyLog(backing, next) {
278
+ return journalDeltaFromDirtyLog(backing.store.dirtyLogSnapshot(), (key) => backing.store.get(key), backing.lastSync, backing.pullsJson, next);
279
+ }
192
280
  function checkpointStoreBackedJournal(backing, journal) {
193
281
  backing.store.drainDirtyLog();
282
+ backing.store.clearCommittedChanges?.();
194
283
  backing.version = journal.version;
195
284
  backing.lastSync = journal.lastSync;
196
285
  backing.pullsJson = JSON.stringify(journal.pulls ?? []);
197
286
  }
287
+ function storeBackedJournalNeedsMigration(backing, journal) {
288
+ return journal.version !== JOURNAL_VERSION_CURRENT ||
289
+ !Array.isArray(journal.pulls) ||
290
+ typeof journal.lastSync !== "string";
291
+ }
198
292
  function journalDeltaPath(journal) {
199
293
  return storeBackedJournals.has(journal) ? "dirty-log" : "fingerprint";
200
294
  }
@@ -294,7 +388,7 @@ export function journalDeltaFromBaseline(baseline, next) {
294
388
  export function journalDeltaForTest(journal) {
295
389
  const backing = storeBackedJournals.get(journal);
296
390
  if (backing)
297
- return journalDeltaFromDirtyLog(backing, journal);
391
+ return journalDeltaFromStoreDirtyLog(backing, journal);
298
392
  const baseline = journalBaselines.get(journal);
299
393
  if (!baseline)
300
394
  throw new Error("journalDeltaForTest: journal has no remembered baseline");
@@ -409,7 +503,39 @@ export function migratePersonalVaultJournal() {
409
503
  * installs never pass through v1 on disk.
410
504
  */
411
505
  export function readJournal(slug) {
412
- return readStoreBackedJournal(readJournalAggregate(slug));
506
+ const areaMigration = openAreaMigrationIfAuthoritative(slug);
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 });
512
+ }
513
+ return readStoreBackedJournal(readJournalAggregateWith(slug, areaMigration));
514
+ }
515
+ /**
516
+ * Read the mutable outer shell required by a full pull without detaching every
517
+ * authoritative-ledger row from its immutable source.
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.
523
+ *
524
+ * Legacy journals deliberately keep `readJournal()`'s packed-record boundary.
525
+ * That path has different ownership and dirty-log semantics, and does not make
526
+ * the area-authoritative structured-clone copy that this reader avoids.
527
+ *
528
+ * @internal Full-pull boundary; not a public journal record API.
529
+ */
530
+ export function readJournalForPass(slug) {
531
+ const areaMigration = openAreaMigrationIfAuthoritative(slug);
532
+ if (!areaMigration)
533
+ 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;
413
539
  }
414
540
  /**
415
541
  * The live aggregate for `slug` without recording a delta baseline.
@@ -431,7 +557,9 @@ export function readJournal(slug) {
431
557
  * when the WeakMap has no entry. It is merely a wider delta, not a wrong one.
432
558
  */
433
559
  function readJournalAggregate(slug) {
434
- const areaMigration = openAreaMigrationIfAuthoritative(slug);
560
+ return readJournalAggregateWith(slug, openAreaMigrationIfAuthoritative(slug));
561
+ }
562
+ function readJournalAggregateWith(slug, areaMigration) {
435
563
  if (areaMigration)
436
564
  return areaMigration.readAreaJournal();
437
565
  if (StateStore.exists(getStateDir(), journalScopeId(slug))) {
@@ -457,21 +585,23 @@ export function readJournalScoped(slug, keys, deleteScopeRoots = []) {
457
585
  return rememberJournalBaseline(areaMigration.readAreaJournalScoped(keys, deleteScopeRoots));
458
586
  }
459
587
  const full = readJournalAggregate(slug);
588
+ const fullRows = journalRows(full);
460
589
  const files = {};
461
590
  const roots = new Set(deleteScopeRoots);
462
591
  if (roots.has("")) {
463
- for (const [key, entry] of Object.entries(full.files)) {
592
+ for (const [key, entry] of fullRows.entries()) {
464
593
  files[key] = structuredClone(entry);
465
594
  }
466
595
  }
467
596
  else {
468
597
  for (const key of new Set(keys)) {
469
- if (!Object.hasOwn(full.files, key))
598
+ const entry = fullRows.get(key);
599
+ if (entry === undefined)
470
600
  continue;
471
- files[key] = structuredClone(full.files[key]);
601
+ files[key] = structuredClone(entry);
472
602
  }
473
603
  if (roots.size > 0) {
474
- for (const [key, entry] of Object.entries(full.files)) {
604
+ for (const [key, entry] of fullRows.entries()) {
475
605
  if (Object.hasOwn(files, key))
476
606
  continue;
477
607
  if (isUnderAnyRoot(key, roots))
@@ -681,6 +811,14 @@ export class JournalStore {
681
811
  store;
682
812
  areaMigration;
683
813
  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();
684
822
  version = JOURNAL_VERSION_CURRENT;
685
823
  lastSync = "";
686
824
  pulls = [];
@@ -701,14 +839,87 @@ export class JournalStore {
701
839
  }
702
840
  get(key) {
703
841
  this.assertOpen();
842
+ if (this.overlay.has(key))
843
+ return this.overlay.get(key);
704
844
  return this.files.get(key);
705
845
  }
706
846
  scanPrefix(prefix) {
707
847
  this.assertOpen();
708
- return Array.from(this.files.entriesForPrefix(prefix));
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());
709
859
  }
710
860
  rowCount() {
711
- return this.files.size;
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;
712
923
  }
713
924
  pullHistory() {
714
925
  this.assertOpen();
@@ -831,15 +1042,19 @@ export class JournalStore {
831
1042
  replaceFromJournal(journal) {
832
1043
  const decoded = decodedJournalRows(journal);
833
1044
  if (decoded) {
1045
+ if (!this.usesSharedRows && this.files !== decoded)
1046
+ this.files.clear();
834
1047
  this.files = decoded;
1048
+ this.usesSharedRows = true;
835
1049
  this.version = journal.version;
836
1050
  this.lastSync = journal.lastSync;
837
1051
  this.pulls = structuredClone(journal.pulls ?? []);
838
1052
  return;
839
1053
  }
840
- this.files.clear();
841
- for (const [key, entry] of Object.entries(journal.files))
842
- this.files.set(key, entry);
1054
+ if (this.usesSharedRows)
1055
+ this.files = new JournalRowStore({ trackChanges: false });
1056
+ this.files.replaceFromRecord(journal.files);
1057
+ this.usesSharedRows = false;
843
1058
  this.version = journal.version;
844
1059
  this.lastSync = journal.lastSync;
845
1060
  this.pulls = structuredClone(journal.pulls ?? []);
@@ -865,7 +1080,9 @@ export class JournalStore {
865
1080
  if (this.disposed)
866
1081
  return;
867
1082
  this.disposed = true;
868
- this.files.clear();
1083
+ this.overlay.clear();
1084
+ if (!this.usesSharedRows)
1085
+ this.files.clear();
869
1086
  this.store = undefined;
870
1087
  this.areaMigration = undefined;
871
1088
  this.pulls = [];
@@ -875,6 +1092,9 @@ export class JournalStore {
875
1092
  throw new Error(`journal: session for ${this.slug} is disposed`);
876
1093
  }
877
1094
  }
1095
+ function isJournalStorePrefixMatch(key, normalizedPrefix) {
1096
+ return normalizedPrefix === "" || key === normalizedPrefix || key.startsWith(`${normalizedPrefix}/`);
1097
+ }
878
1098
  /** Return a detached, deeply frozen JSON-shaped metadata value. */
879
1099
  function immutableCopy(value) {
880
1100
  const copy = structuredClone(value);
@@ -1280,22 +1500,70 @@ export function listJournals() {
1280
1500
  export function normalizeJournalKeys(journal) {
1281
1501
  if (!journal.files)
1282
1502
  return 0;
1503
+ const rows = journalRows(journal);
1504
+ const rewrites = [];
1283
1505
  let rewritten = 0;
1284
- // Snapshot keys up front — we mutate journal.files during the walk.
1285
- for (const key of Object.keys(journal.files)) {
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()) {
1286
1509
  const posix = toPosixKey(key);
1287
1510
  if (posix === key)
1288
1511
  continue; // already canonical (no backslash)
1289
- const entry = journal.files[key];
1290
- delete journal.files[key];
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);
1291
1518
  rewritten++;
1292
1519
  // POSIX twin already present → it wins; drop the malformed duplicate.
1293
- if (!(posix in journal.files)) {
1294
- journal.files[posix] = entry;
1295
- }
1520
+ if (!rows.has(posix))
1521
+ rows.set(posix, entry);
1522
+ }
1523
+ return rewritten;
1524
+ }
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);
1296
1553
  }
1297
1554
  return rewritten;
1298
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
+ }
1299
1567
  /**
1300
1568
  * Coerce any-version journal into a v2 shape. Idempotent for v2 inputs.
1301
1569
  * Mutates the input and returns it for chainable use. Call this immediately
@@ -1311,15 +1579,7 @@ export function migrateToV2(journal) {
1311
1579
  // already carry version "2" (the landmine predates the schema bump), so this
1312
1580
  // must precede the v2 early-return or already-v2 journals stay poisoned.
1313
1581
  normalizeJournalKeys(journal);
1314
- if (journal.version === "2" && Array.isArray(journal.pulls) && typeof journal.lastSync === "string") {
1315
- return journal;
1316
- }
1317
- journal.version = JOURNAL_VERSION_CURRENT;
1318
- if (typeof journal.lastSync !== "string")
1319
- journal.lastSync = "";
1320
- if (!Array.isArray(journal.pulls))
1321
- journal.pulls = [];
1322
- return journal;
1582
+ return migrateJournalShapeToV2(journal);
1323
1583
  }
1324
1584
  /**
1325
1585
  * Write a journal to disk, migrating to the current schema version in-place.
@@ -1327,18 +1587,30 @@ export function migrateToV2(journal) {
1327
1587
  * after the write will see the v2 shape.
1328
1588
  */
1329
1589
  export function writeJournal(slug, journal) {
1330
- migrateToV2(journal);
1331
1590
  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 {
1599
+ migrateToV2(journal);
1600
+ }
1332
1601
  const areaMigration = openAreaMigrationIfAuthoritative(slug);
1333
1602
  if (areaMigration) {
1334
1603
  const baseline = journalBaselines.get(journal);
1335
1604
  const delta = storeBacking
1336
- ? journalDeltaFromDirtyLog(storeBacking, journal)
1605
+ ? journalDeltaFromStoreDirtyLog(storeBacking, journal)
1337
1606
  : baseline
1338
1607
  ? journalDeltaFromBaseline(baseline, journal)
1339
1608
  : journalDelta(areaMigration.readAreaJournal(), journal);
1340
- if (hasJournalDelta(delta))
1341
- areaMigration.reverseShadow(toAreaLedgerDelta(delta));
1609
+ if (hasJournalDelta(delta)) {
1610
+ areaMigration.reverseShadow(toAreaLedgerDelta(delta), {
1611
+ releaseLegacyStoreImmediately: fullPassAreaJournals.has(journal),
1612
+ });
1613
+ }
1342
1614
  if (storeBacking)
1343
1615
  checkpointStoreBackedJournal(storeBacking, journal);
1344
1616
  else
@@ -1350,6 +1622,8 @@ export function writeJournal(slug, journal) {
1350
1622
  if (store.wasCreated) {
1351
1623
  // The store was just seeded from this journal, so its state already matches
1352
1624
  // and there is no delta to record. Only the locator is missing.
1625
+ if (storeBacking)
1626
+ checkpointStoreBackedJournal(storeBacking, journal);
1353
1627
  writeJournalLocator(slug);
1354
1628
  return;
1355
1629
  }
@@ -1369,13 +1643,13 @@ export function writeJournal(slug, journal) {
1369
1643
  // stays bounded, it just no longer happens on every single update.
1370
1644
  const baseline = journalBaselines.get(journal);
1371
1645
  const delta = storeBacking
1372
- ? journalDeltaFromDirtyLog(storeBacking, journal)
1646
+ ? journalDeltaFromStoreDirtyLog(storeBacking, journal)
1373
1647
  : baseline
1374
1648
  ? journalDeltaFromBaseline(baseline, journal)
1375
1649
  : journalDelta(store.getState().journal, journal);
1376
1650
  store.append(JOURNAL_DELTA_RECORD_TYPE, delta);
1377
1651
  // A caller can checkpoint more than once during one pass. Advancing only its
1378
- // own baseline means the next checkpoint again publishes only new mutations.
1652
+ // own tracking state means the next checkpoint again publishes only new mutations.
1379
1653
  if (storeBacking)
1380
1654
  checkpointStoreBackedJournal(storeBacking, journal);
1381
1655
  else
@@ -1393,8 +1667,10 @@ export function writeJournal(slug, journal) {
1393
1667
  */
1394
1668
  function journalDelta(previous, next) {
1395
1669
  const upserts = {};
1396
- for (const [key, entry] of Object.entries(next.files)) {
1397
- const before = previous.files[key];
1670
+ const previousRows = journalRows(previous);
1671
+ const nextRows = journalRows(next);
1672
+ for (const [key, entry] of nextRows.entries()) {
1673
+ const before = previousRows.get(key);
1398
1674
  // Value comparison, not identity: `previous` was just deserialized from the
1399
1675
  // snapshot on disk, so nothing in it shares a reference with the caller's
1400
1676
  // journal and an identity check would mark every file changed.
@@ -1403,8 +1679,8 @@ function journalDelta(previous, next) {
1403
1679
  upserts[key] = entry;
1404
1680
  }
1405
1681
  const deletes = [];
1406
- for (const key of Object.keys(previous.files)) {
1407
- if (!(key in next.files))
1682
+ for (const key of previousRows.keys()) {
1683
+ if (!nextRows.has(key))
1408
1684
  deletes.push(key);
1409
1685
  }
1410
1686
  const delta = {
@@ -1753,7 +2029,7 @@ export function updateEntry(journal, relativePath, hash, size, direction, verify
1753
2029
  if (skillMetadataPending === true) {
1754
2030
  entry.skillMetadataPending = true;
1755
2031
  }
1756
- journal.files[relativePath] = entry;
2032
+ journalRows(journal).set(relativePath, entry);
1757
2033
  journal.lastSync = new Date().toISOString();
1758
2034
  }
1759
2035
  /**
@@ -1767,10 +2043,10 @@ export function normalizeEtag(etag) {
1767
2043
  return etag.replace(/^"|"$/g, "");
1768
2044
  }
1769
2045
  export function getEntry(journal, relativePath) {
1770
- return journal.files[relativePath];
2046
+ return journalRows(journal).get(relativePath);
1771
2047
  }
1772
2048
  export function removeEntry(journal, relativePath) {
1773
- delete journal.files[relativePath];
2049
+ journalRows(journal).delete(relativePath);
1774
2050
  }
1775
2051
  // ─── Journal v2 (US-005): pulls, tombstones, GC ─────────────────────────────
1776
2052
  /** Crockford base32 alphabet (ULID-compatible). */
@@ -1871,10 +2147,10 @@ export function appendPullRecord(journal, record) {
1871
2147
  * They are GC'd after `TOMBSTONE_TTL_MS` via `gcTombstones`.
1872
2148
  */
1873
2149
  export function tombstoneEntry(journal, relativePath, reason, now = new Date().toISOString()) {
1874
- const entry = journal.files[relativePath];
2150
+ const entry = journalRows(journal).get(relativePath);
1875
2151
  if (!entry)
1876
2152
  return;
1877
- journal.files[relativePath] = { ...entry, removedAt: now, removedReason: reason };
2153
+ journalRows(journal).set(relativePath, { ...entry, removedAt: now, removedReason: reason });
1878
2154
  }
1879
2155
  /**
1880
2156
  * Mark a journal entry for a user-authorized local delete. Callers must
@@ -1882,14 +2158,14 @@ export function tombstoneEntry(journal, relativePath, reason, now = new Date().t
1882
2158
  * alone is never deletion intent.
1883
2159
  */
1884
2160
  export function markLocalDeleteIntent(journal, relativePath, localHash, localKind, expectedRemoteEtag) {
1885
- const entry = journal.files[relativePath];
2161
+ const entry = journalRows(journal).get(relativePath);
1886
2162
  if (!entry?.remoteEtag ||
1887
2163
  entry.remoteEtag !== normalizeEtag(expectedRemoteEtag) ||
1888
2164
  entry.hash !== localHash ||
1889
2165
  entry.kind !== localKind) {
1890
2166
  return false;
1891
2167
  }
1892
- journal.files[relativePath] = {
2168
+ journalRows(journal).set(relativePath, {
1893
2169
  ...entry,
1894
2170
  localDeleteIntent: {
1895
2171
  version: 1,
@@ -1897,16 +2173,16 @@ export function markLocalDeleteIntent(journal, relativePath, localHash, localKin
1897
2173
  localHash,
1898
2174
  localKind,
1899
2175
  },
1900
- };
2176
+ });
1901
2177
  return true;
1902
2178
  }
1903
2179
  /** Invalidate a prior delete authorization after a watcher-observed recreate. */
1904
2180
  export function clearLocalDeleteIntent(journal, relativePath) {
1905
- const entry = journal.files[relativePath];
2181
+ const entry = journalRows(journal).get(relativePath);
1906
2182
  if (!entry?.localDeleteIntent)
1907
2183
  return false;
1908
2184
  const { localDeleteIntent: _localDeleteIntent, ...cleared } = entry;
1909
- journal.files[relativePath] = cleared;
2185
+ journalRows(journal).set(relativePath, cleared);
1910
2186
  return true;
1911
2187
  }
1912
2188
  /** True if the entry is a tombstone (set by `tombstoneEntry`). */
@@ -1921,14 +2197,14 @@ export function isTombstone(entry) {
1921
2197
  */
1922
2198
  export function gcTombstones(journal, now = Date.now()) {
1923
2199
  let removed = 0;
1924
- for (const [path, entry] of Object.entries(journal.files)) {
2200
+ for (const [path, entry] of journalRows(journal).entries()) {
1925
2201
  if (!entry.removedAt)
1926
2202
  continue;
1927
2203
  const removedTime = Date.parse(entry.removedAt);
1928
2204
  if (Number.isNaN(removedTime))
1929
2205
  continue;
1930
2206
  if (now - removedTime > TOMBSTONE_TTL_MS) {
1931
- delete journal.files[path];
2207
+ journalRows(journal).delete(path);
1932
2208
  removed++;
1933
2209
  }
1934
2210
  }