@cortexkit/opencode-magic-context 0.49.92 → 0.49.95

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 (151) hide show
  1. package/dist/auto-search-worker.js +4 -4
  2. package/dist/chunk-embedding-write-worker.js +3 -3
  3. package/dist/{compartment-chunk-embedding-rp4asvk0.js → compartment-chunk-embedding-4nn3hsw2.js} +2 -2
  4. package/dist/{context-e8qd2v00.js → context-h9x9m2p3.js} +192 -68
  5. package/dist/{embedding-backfill-w0yq71q2.js → embedding-backfill-wdrqkw09.js} +6 -6
  6. package/dist/embedding-worker.js +3 -3
  7. package/dist/features/magic-context/compartment-storage.d.ts.map +1 -1
  8. package/dist/features/magic-context/dreamer/docs-proposals.d.ts +5 -2
  9. package/dist/features/magic-context/dreamer/docs-proposals.d.ts.map +1 -1
  10. package/dist/features/magic-context/history-gap-recovery.d.ts +42 -0
  11. package/dist/features/magic-context/history-gap-recovery.d.ts.map +1 -0
  12. package/dist/features/magic-context/memory/embedding-local.d.ts.map +1 -1
  13. package/dist/features/magic-context/memory/embedding-synapse.d.ts +18 -5
  14. package/dist/features/magic-context/memory/embedding-synapse.d.ts.map +1 -1
  15. package/dist/features/magic-context/memory/embedding-worker-client.d.ts +3 -0
  16. package/dist/features/magic-context/memory/embedding-worker-client.d.ts.map +1 -1
  17. package/dist/features/magic-context/message-visibility.d.ts +10 -0
  18. package/dist/features/magic-context/message-visibility.d.ts.map +1 -0
  19. package/dist/features/magic-context/search.d.ts +4 -0
  20. package/dist/features/magic-context/search.d.ts.map +1 -1
  21. package/dist/features/magic-context/session-decision-calibration.d.ts.map +1 -1
  22. package/dist/features/magic-context/session-project-backfill.d.ts.map +1 -1
  23. package/dist/features/magic-context/store-generation-rebase.d.ts.map +1 -1
  24. package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
  25. package/dist/hooks/magic-context/compartment-runner-recomp.d.ts.map +1 -1
  26. package/dist/hooks/magic-context/compartment-trigger.d.ts +1 -1
  27. package/dist/hooks/magic-context/compartment-trigger.d.ts.map +1 -1
  28. package/dist/hooks/magic-context/history-boundary-repair.d.ts +2 -2
  29. package/dist/hooks/magic-context/history-boundary-repair.d.ts.map +1 -1
  30. package/dist/hooks/magic-context/lkg-measured-request.d.ts +1 -0
  31. package/dist/hooks/magic-context/lkg-measured-request.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/lkg-persist.d.ts +3 -1
  33. package/dist/hooks/magic-context/lkg-persist.d.ts.map +1 -1
  34. package/dist/hooks/magic-context/lkg-recovery-fence.d.ts +1 -0
  35. package/dist/hooks/magic-context/lkg-recovery-fence.d.ts.map +1 -1
  36. package/dist/hooks/magic-context/lkg-replay.d.ts +4 -0
  37. package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -1
  38. package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts +1 -0
  39. package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts.map +1 -1
  40. package/dist/hooks/magic-context/tail-hygiene-output-count.d.ts +4 -0
  41. package/dist/hooks/magic-context/tail-hygiene-output-count.d.ts.map +1 -1
  42. package/dist/hooks/magic-context/tail-hygiene-walk.d.ts +2 -0
  43. package/dist/hooks/magic-context/tail-hygiene-walk.d.ts.map +1 -1
  44. package/dist/hooks/magic-context/transform-stage-logger.d.ts +6 -0
  45. package/dist/hooks/magic-context/transform-stage-logger.d.ts.map +1 -1
  46. package/dist/{index-3f3d548g.js → index-2gewejcz.js} +18 -9
  47. package/dist/{index-n71006wq.js → index-2v9qpmb3.js} +5 -5
  48. package/dist/{index-zzhcshdk.js → index-3tmbvn0z.js} +9 -9
  49. package/dist/{index-f25ha337.js → index-3ywyr4zx.js} +1 -1
  50. package/dist/{index-hbn5g7rt.js → index-47hst5me.js} +1 -1
  51. package/dist/{index-xg1zjht5.js → index-7dzaamqg.js} +6 -6
  52. package/dist/{index-5y9w2ax7.js → index-7r11k4xr.js} +2 -2
  53. package/dist/{index-5pmf4jpj.js → index-7skgrbsg.js} +100 -52
  54. package/dist/{index-6rk44cer.js → index-c81tggen.js} +1 -1
  55. package/dist/{index-0twg41mh.js → index-ejgp0z9p.js} +1 -1
  56. package/dist/{index-hre29c1a.js → index-esacb9d0.js} +1 -1
  57. package/dist/{index-5a9kwapd.js → index-ewta5h87.js} +4 -4
  58. package/dist/{index-6r99v53h.js → index-j7n64sg4.js} +162 -60
  59. package/dist/{index-r3tdqqtq.js → index-jm62g83a.js} +180 -64
  60. package/dist/{index-anwy2mfb.js → index-pc49a7zt.js} +162 -31
  61. package/dist/{index-ydhymf0b.js → index-qmm8xpbc.js} +4 -4
  62. package/dist/{index-3n376ann.js → index-tedck05r.js} +4 -4
  63. package/dist/{index-j2w79s9r.js → index-tm799pq7.js} +1 -1
  64. package/dist/{index-01h7tqe3.js → index-tr4ep2ej.js} +21 -6
  65. package/dist/{index-wvbg9m0h.js → index-wqwjawer.js} +27 -11
  66. package/dist/index-xe34pwb6.js +119 -0
  67. package/dist/{index-xn9efn18.js → index-xjaf9g90.js} +13 -6
  68. package/dist/{index-6nqz8hsz.js → index-y915t3ey.js} +1 -1
  69. package/dist/{index-41bma6qs.js → index-yzzxb5e1.js} +2 -2
  70. package/dist/{index-901exj0f.js → index-zk6gtf43.js} +45 -9
  71. package/dist/index.js +27 -27
  72. package/dist/{message-index-bounded-f6fc00ze.js → message-index-bounded-d3x8rhpz.js} +4 -4
  73. package/dist/migration-worker.js +1 -1
  74. package/dist/{project-embedding-registry-8je0xg18.js → project-embedding-registry-az68q792.js} +5 -5
  75. package/dist/raw-ordinal-warmup-worker.js +5 -5
  76. package/dist/{read-session-chunk-y2ew3630.js → read-session-chunk-70r1snq3.js} +3 -3
  77. package/dist/{recomp-orchestrator-ggtcvb9g.js → recomp-orchestrator-cn4r2gz6.js} +15 -15
  78. package/dist/{render-q8c6wrq9.js → render-g45k3gts.js} +10 -10
  79. package/dist/{search-archive-pz1jf655.js → search-archive-s9t3exc1.js} +9 -7
  80. package/dist/{search-gjyrwapc.js → search-b0cb342r.js} +13 -13
  81. package/dist/shared/storage-file-mutex.d.ts.map +1 -1
  82. package/dist/{storage-tags-7raphcw6.js → storage-tags-xvbje7jm.js} +2 -2
  83. package/dist/{store-yr443ye8.js → store-91kawq9a.js} +2 -2
  84. package/dist/{store-reader-qbq6q4v0.js → store-reader-dgh3ssxx.js} +1 -1
  85. package/dist/tools/ctx-expand/search-archive.d.ts +3 -2
  86. package/dist/tools/ctx-expand/search-archive.d.ts.map +1 -1
  87. package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
  88. package/dist/tools/ctx-expand/v2-archive-reader.d.ts +2 -0
  89. package/dist/tools/ctx-expand/v2-archive-reader.d.ts.map +1 -1
  90. package/dist/tools/ctx-search/tools.d.ts.map +1 -1
  91. package/dist/{transform-postprocess-phase-pmzdd3s2.js → transform-postprocess-phase-r1xhsjrh.js} +16 -16
  92. package/dist/v2/auto-search-worker.js +4 -4
  93. package/dist/v2/chunk-embedding-write-worker.js +3 -3
  94. package/dist/v2/{compartment-chunk-embedding-w7ht0t9v.js → compartment-chunk-embedding-a7ddvz7v.js} +2 -2
  95. package/dist/v2/{context-qqbpzk1q.js → context-2hssw26q.js} +351 -111
  96. package/dist/v2/{embedding-backfill-95m72gxa.js → embedding-backfill-gveter8p.js} +6 -6
  97. package/dist/v2/embedding-worker.js +3 -3
  98. package/dist/v2/fold/memory-cache.d.ts +2 -0
  99. package/dist/v2/fold/memory-cache.d.ts.map +1 -1
  100. package/dist/v2/fold/native-replay.d.ts +22 -5
  101. package/dist/v2/fold/native-replay.d.ts.map +1 -1
  102. package/dist/v2/hooks/context.d.ts.map +1 -1
  103. package/dist/v2/hooks/revert-message-state.d.ts.map +1 -1
  104. package/dist/v2/hooks/session-project-backfill.d.ts +2 -1
  105. package/dist/v2/hooks/session-project-backfill.d.ts.map +1 -1
  106. package/dist/v2/{message-index-bounded-70g28hyg.js → message-index-bounded-tbwrff24.js} +4 -4
  107. package/dist/v2/migration-worker.js +1 -1
  108. package/dist/v2/{project-embedding-registry-fcxf4qht.js → project-embedding-registry-2zeycxek.js} +5 -5
  109. package/dist/v2/raw-ordinal-warmup-worker.js +5 -5
  110. package/dist/v2/{read-session-chunk-9rf0n9e6.js → read-session-chunk-cvgsfsgr.js} +3 -3
  111. package/dist/v2/{recomp-orchestrator-cwghhjc9.js → recomp-orchestrator-k4cj62f2.js} +15 -15
  112. package/dist/v2/{render-e852zmxz.js → render-ys7a3zks.js} +10 -10
  113. package/dist/v2/{search-archive-tmn6b5w8.js → search-archive-k2yh0jj3.js} +9 -7
  114. package/dist/v2/{search-jd3vczhn.js → search-x1cac8t6.js} +12 -12
  115. package/dist/v2/{server-vsqmkvxc.js → server-00adcfjd.js} +1 -1
  116. package/dist/v2/{server-rkj44mez.js → server-476anw1w.js} +1 -1
  117. package/dist/v2/{server-barj0mjz.js → server-5016ccyn.js} +1 -1
  118. package/dist/v2/{server-72k2r3kt.js → server-6aj72z52.js} +4 -4
  119. package/dist/v2/{server-7x5a46hf.js → server-6nh36020.js} +70 -61
  120. package/dist/v2/{server-fj3pekz2.js → server-88pjnecb.js} +1 -1
  121. package/dist/v2/{server-js2v309w.js → server-bfct1yvb.js} +99 -51
  122. package/dist/v2/{server-6t3asmx8.js → server-cd93xyyx.js} +162 -31
  123. package/dist/v2/{server-ysjjcatk.js → server-cvm1qrqc.js} +2 -2
  124. package/dist/v2/{server-rz6g5xne.js → server-e6xneh68.js} +1 -1
  125. package/dist/v2/{server-d4trxrj3.js → server-gj259dqy.js} +45 -9
  126. package/dist/v2/{server-8ps3717t.js → server-prrx17q3.js} +4 -4
  127. package/dist/v2/{server-7e89a2kf.js → server-qy58c11s.js} +13 -6
  128. package/dist/v2/{server-9011eteb.js → server-s81b3nct.js} +2 -2
  129. package/dist/v2/{server-p4g021y5.js → server-s8mk882p.js} +27 -11
  130. package/dist/v2/{server-mgxqfprb.js → server-srtq37nh.js} +1 -1
  131. package/dist/v2/{server-bvr9xs8h.js → server-vbr1rdkg.js} +6 -6
  132. package/dist/v2/{server-vtgeyhq6.js → server-vtkbxzw1.js} +21 -6
  133. package/dist/v2/{server-ps6zrzwj.js → server-xepfjgv5.js} +162 -60
  134. package/dist/v2/server-y0pd600x.js +119 -0
  135. package/dist/v2/{server-b4efn08m.js → server-zc8s06gx.js} +1 -1
  136. package/dist/v2/server.js +4 -4
  137. package/dist/v2/{storage-tags-wgsemtw1.js → storage-tags-y20j3jdp.js} +2 -2
  138. package/dist/v2/{store-enef5jcn.js → store-b95snz31.js} +2 -2
  139. package/dist/v2/{store-reader-jbaqv1yp.js → store-reader-qt2msa8z.js} +1 -1
  140. package/dist/v2/store-reader.d.ts +1 -1
  141. package/dist/v2/store-reader.d.ts.map +1 -1
  142. package/dist/v2/{v2-archive-reader-dm1aeqk2.js → v2-archive-reader-q92kwkr5.js} +1 -1
  143. package/dist/v2/{wrapup-orchestrator-cke368np.js → wrapup-orchestrator-ydqkxkhj.js} +15 -15
  144. package/dist/{v2-archive-reader-39ye0d3b.js → v2-archive-reader-x62hsr4c.js} +1 -1
  145. package/dist/{wrapup-orchestrator-ggnk0r4r.js → wrapup-orchestrator-kt81d6et.js} +16 -16
  146. package/package.json +1 -1
  147. package/src/config/schema/magic-context.ts +1 -1
  148. package/src/shared/storage-file-mutex-review.test.ts +110 -0
  149. package/src/shared/storage-file-mutex.ts +12 -0
  150. package/dist/index-52x68mj4.js +0 -58
  151. package/dist/v2/server-52x68mj4.js +0 -58
@@ -48,7 +48,7 @@ import {
48
48
  clearNoteNudgeTriggerOnly,
49
49
  nonNarrativeStoredGapRanges,
50
50
  getActiveCompartmentRun
51
- } from "./index-3f3d548g.js";
51
+ } from "./index-2gewejcz.js";
52
52
  import"./index-sd3egckz.js";
53
53
  import {
54
54
  getHarness2
@@ -82,7 +82,7 @@ import {
82
82
  formatUnsupportedDreamTasks,
83
83
  isCanonicalDreamTask
84
84
  } from "./index-fqth7850.js";
85
- import"./index-6nqz8hsz.js";
85
+ import"./index-y915t3ey.js";
86
86
  import {
87
87
  logConfigDependencyNotesOnce,
88
88
  runSessionProjectBackfill,
@@ -143,7 +143,7 @@ import {
143
143
  applyJsonSchemaParameterDescriptions,
144
144
  MagicContextRpcServer,
145
145
  subagentTemperature
146
- } from "./index-r3tdqqtq.js";
146
+ } from "./index-jm62g83a.js";
147
147
  import {
148
148
  invalidateAutoEmbedSession,
149
149
  clearEmbedSessionState
@@ -153,7 +153,7 @@ import {
153
153
  preloadTokenizer,
154
154
  estimateTokens
155
155
  } from "./index-s7460efs.js";
156
- import"./index-6r99v53h.js";
156
+ import"./index-j7n64sg4.js";
157
157
  import {
158
158
  inRootAsyncContext2,
159
159
  withSqliteTransformPass2,
@@ -168,13 +168,15 @@ import"./index-q7q6qp1f.js";
168
168
  import"./index-s32f8y67.js";
169
169
  import {
170
170
  deleteChunkEmbedBackoffForSession2
171
- } from "./index-0twg41mh.js";
171
+ } from "./index-ejgp0z9p.js";
172
172
  import {
173
173
  advanceSessionActivity,
174
174
  backfillSessionActivity,
175
175
  acquireCompartmentLease,
176
176
  releaseCompartmentLeaseBestEffort,
177
177
  isCompartmentLeaseHeld,
178
+ readSummaryCoverage,
179
+ markHistoryGapAfterCoverageChange,
178
180
  queueM0Mutation,
179
181
  getLastCompartmentEndMessageId,
180
182
  clearRecompStaging,
@@ -206,8 +208,8 @@ import {
206
208
  getSlot,
207
209
  dropSlot,
208
210
  copySessionStateForClone
209
- } from "./index-anwy2mfb.js";
210
- import"./index-hre29c1a.js";
211
+ } from "./index-pc49a7zt.js";
212
+ import"./index-esacb9d0.js";
211
213
  import {
212
214
  stripPersistedAssistantText,
213
215
  currentReclaimableToolOutputCount,
@@ -250,7 +252,7 @@ import {
250
252
  estimateFinalWireInputTokens,
251
253
  getMaxTagNumberBySession2,
252
254
  getTagsBySession2
253
- } from "./index-901exj0f.js";
255
+ } from "./index-zk6gtf43.js";
254
256
  import {
255
257
  BoundedSessionMap,
256
258
  getTableColumnNames,
@@ -293,10 +295,10 @@ import {
293
295
  cleanUserText2,
294
296
  readRawSessionMessageIdOrdinalsForRange2,
295
297
  readRawSessionMessageOrdinalById2
296
- } from "./index-5y9w2ax7.js";
298
+ } from "./index-7r11k4xr.js";
297
299
  import {
298
300
  scheduleClearAndReindex
299
- } from "./index-hbn5g7rt.js";
301
+ } from "./index-47hst5me.js";
300
302
  import {
301
303
  startBootDeadline,
302
304
  getSchemaFenceRejection,
@@ -306,7 +308,7 @@ import {
306
308
  openDatabaseAsync,
307
309
  isDatabasePersisted,
308
310
  getDatabasePersistenceError
309
- } from "./index-6rk44cer.js";
311
+ } from "./index-c81tggen.js";
310
312
  import"./index-fr0wbhdn.js";
311
313
  import"./index-jba6ny4m.js";
312
314
  import {
@@ -314,7 +316,7 @@ import {
314
316
  RAW_MESSAGE_TYPES2,
315
317
  V2StoreReaderPool2,
316
318
  V2StoreReader2
317
- } from "./index-01h7tqe3.js";
319
+ } from "./index-tr4ep2ej.js";
318
320
  import"./index-fb3zxn06.js";
319
321
  import"./index-mjy2k9jr.js";
320
322
  import {
@@ -335,20 +337,20 @@ import {
335
337
  servedHeadChanged,
336
338
  noteHostCheckpointCommit,
337
339
  isSyntheticHeadMessage2
338
- } from "./index-zzhcshdk.js";
340
+ } from "./index-3tmbvn0z.js";
339
341
  import"./index-832w615x.js";
340
342
  import"./index-qt1qqasc.js";
341
343
  import {
342
344
  retainProjectEmbedding2
343
- } from "./index-3n376ann.js";
345
+ } from "./index-tedck05r.js";
344
346
  import"./index-r4y0dczp.js";
345
347
  import"./index-qjwpqvnf.js";
346
- import"./index-xn9efn18.js";
348
+ import"./index-xjaf9g90.js";
347
349
  import {
348
350
  renderUserFacingFailure,
349
351
  userFacingFailureCode
350
- } from "./index-ydhymf0b.js";
351
- import"./index-f25ha337.js";
352
+ } from "./index-qmm8xpbc.js";
353
+ import"./index-3ywyr4zx.js";
352
354
  import"./index-9emdjcb5.js";
353
355
  import {
354
356
  pushLiveNotification2,
@@ -357,8 +359,8 @@ import {
357
359
  } from "./index-y14pawe1.js";
358
360
  import"./index-e6ncz7a6.js";
359
361
  import"./index-9ffntwyr.js";
360
- import"./index-5pmf4jpj.js";
361
- import"./index-5a9kwapd.js";
362
+ import"./index-7skgrbsg.js";
363
+ import"./index-ewta5h87.js";
362
364
  import"./index-cr483vx6.js";
363
365
  import {
364
366
  detectConflicts
@@ -388,12 +390,12 @@ import {
388
390
  readV2RawMessagePagesForConversion2,
389
391
  createV2RawMessageReader2,
390
392
  createV2RawMessageProvider2
391
- } from "./index-j2w79s9r.js";
393
+ } from "./index-tm799pq7.js";
392
394
  import"./index-fmsr7v69.js";
393
- import"./index-41bma6qs.js";
394
- import"./index-52x68mj4.js";
395
- import"./index-xg1zjht5.js";
396
- import"./index-wvbg9m0h.js";
395
+ import"./index-yzzxb5e1.js";
396
+ import"./index-xe34pwb6.js";
397
+ import"./index-7dzaamqg.js";
398
+ import"./index-wqwjawer.js";
397
399
  import {
398
400
  markHostInstructionRow,
399
401
  hostInstructionRow,
@@ -474,11 +476,14 @@ function dropCompartmentsAfter(db, sessionId, anchor, holderId, activeBranch = f
474
476
  return null;
475
477
  }
476
478
  const dropped = db.prepare("SELECT id, sequence FROM compartments WHERE session_id = ? AND sequence > ? ORDER BY sequence").all(sessionId, anchor?.sequence ?? -1);
477
- onRetire?.();
479
+ const previousCoverage = readSummaryCoverage(db, sessionId);
480
+ const hostQueuedRecovery = onRetire?.() === true;
478
481
  const deleteEmbeddings = db.prepare("DELETE FROM compartment_chunk_embeddings WHERE compartment_id = ?");
479
482
  for (const row of dropped)
480
483
  deleteEmbeddings.run(row.id);
481
484
  db.prepare("DELETE FROM compartments WHERE session_id = ? AND sequence > ?").run(sessionId, anchor?.sequence ?? -1);
485
+ if (!hostQueuedRecovery)
486
+ markHistoryGapAfterCoverageChange(db, sessionId, previousCoverage);
482
487
  db.prepare("DELETE FROM compression_depth WHERE session_id = ? AND message_ordinal > ?").run(sessionId, anchor?.endMessage ?? 0);
483
488
  if (activeBranch) {
484
489
  db.prepare("UPDATE session_meta SET prior_boundary_ordinal = ? WHERE session_id = ?").run((anchor?.endMessage ?? 0) + 1, sessionId);
@@ -1463,6 +1468,9 @@ class NativeFoldCache {
1463
1468
  const kept = this.nativeRows.get(sessionID);
1464
1469
  return ids.every((id) => kept?.has(id));
1465
1470
  }
1471
+ holdsState(sessionID) {
1472
+ return this.tails.has(sessionID) || (this.nativeRows.get(sessionID)?.size ?? 0) > 0;
1473
+ }
1466
1474
  rows(sessionID, after, through) {
1467
1475
  if (this.retention.has(sessionID))
1468
1476
  this.touch(sessionID);
@@ -2363,8 +2371,11 @@ class NativeFoldReplay {
2363
2371
  stampMRU;
2364
2372
  loaded = new Map;
2365
2373
  sources = new Map;
2374
+ memorySources = new Map;
2366
2375
  staleHeads = new Set;
2367
2376
  unprovenControls = new Set;
2377
+ reportedCaptureReplacements = new Set;
2378
+ reportedForeignRefreshes = new Set;
2368
2379
  editedAdmissions = new Map;
2369
2380
  legacyUpgrades = new Map;
2370
2381
  constructor(storage, openReader, admissions, options = {}) {
@@ -2388,6 +2399,7 @@ class NativeFoldReplay {
2388
2399
  await this.forget(sessionID);
2389
2400
  this.sources.set(sessionID, source);
2390
2401
  this.account(sessionID);
2402
+ return source;
2391
2403
  }
2392
2404
  onEvent(event) {
2393
2405
  const sessionID = event.data?.sessionID;
@@ -2460,6 +2472,18 @@ class NativeFoldReplay {
2460
2472
  this.options.log?.(sessionID, `v3 host fold: ${reason}`);
2461
2473
  } catch {}
2462
2474
  }
2475
+ reportOnce(sessionID, reported, message) {
2476
+ if (reported.has(sessionID))
2477
+ return;
2478
+ reported.add(sessionID);
2479
+ const sessionLog = this.options.log;
2480
+ try {
2481
+ sessionLog?.(sessionID, message);
2482
+ } catch {}
2483
+ }
2484
+ reportCaptureReplacement(sessionID) {
2485
+ this.reportOnce(sessionID, this.reportedCaptureReplacements, "v3 host fold: capture skipped: host database file changed between readers");
2486
+ }
2463
2487
  historyCoverage(sessionID, reader) {
2464
2488
  if (!this.options.rendersHistory?.(sessionID))
2465
2489
  return;
@@ -2476,13 +2500,15 @@ class NativeFoldReplay {
2476
2500
  return false;
2477
2501
  }
2478
2502
  async floorState(sessionID, reader, boundary, served, preferCapture = false) {
2503
+ const source = reader.hostIdentity();
2504
+ await this.fenceMemory(sessionID, source);
2479
2505
  const coverage = this.historyCoverage(sessionID, reader);
2480
2506
  const retained = this.tail(sessionID)?.replay;
2481
2507
  const controlsKnown = (replay) => replay.controls.every((control) => !control.after || reader.sequenceForId(sessionID, control.after) !== undefined);
2482
2508
  const memory = retained && validReplayState(retained) && controlsKnown(retained) && validFloor(reader, sessionID, retained.floor) && this.floorCovered(sessionID, retained.floor, coverage, retained) ? retained : undefined;
2483
2509
  const cut = reader.latestCompaction(sessionID);
2484
2510
  const saved = await this.readV3(sessionID);
2485
- const durable = saved && saved.proof.sourceDigest === foldDigest(reader.hostIdentity()) && (!cut || saved.proof.admissionID === cut.id) && controlsKnown(saved.replay) && validFloor(reader, sessionID, saved.replay.floor) && this.floorCovered(sessionID, saved.replay.floor, coverage, saved.replay) ? saved.replay : undefined;
2511
+ const durable = saved && saved.proof.sourceDigest === foldDigest(source) && (!cut || saved.proof.admissionID === cut.id) && controlsKnown(saved.replay) && validFloor(reader, sessionID, saved.replay.floor) && this.floorCovered(sessionID, saved.replay.floor, coverage, saved.replay) ? saved.replay : undefined;
2486
2512
  const marker = importedFloor(reader, sessionID, cut?.seq);
2487
2513
  const imported = marker && this.floorCovered(sessionID, marker, coverage) ? marker : undefined;
2488
2514
  const capturedRecord = await this.readCapturedReplay(sessionID, reader);
@@ -2542,11 +2568,11 @@ class NativeFoldReplay {
2542
2568
  return;
2543
2569
  }
2544
2570
  }
2545
- async saveCapturedReplay(sessionID, replay, reader) {
2571
+ async saveCapturedReplay(sessionID, replay, reader, sourceDigest) {
2546
2572
  const value = {
2547
2573
  version: 3,
2548
2574
  sessionID,
2549
- sourceDigest: foldDigest(reader.hostIdentity()),
2575
+ sourceDigest,
2550
2576
  through: reader.latestSequence(sessionID),
2551
2577
  replay
2552
2578
  };
@@ -2576,11 +2602,33 @@ class NativeFoldReplay {
2576
2602
  ${cached}
2577
2603
  </summary>`)))) ? cached : undefined;
2578
2604
  }
2579
- async retainReplay(sessionID, replay, model, sourceSeq) {
2605
+ async retainReplay(sessionID, replay, model, sourceSeq, source) {
2606
+ await this.fenceMemory(sessionID, source);
2580
2607
  const tail = this.tail(sessionID) ?? { model, sourceSeq };
2581
2608
  await this.storage.saveTail(sessionID, JSON.stringify({ ...tail, replay }), []);
2582
2609
  if (!this.storage.tail(sessionID))
2583
2610
  await this.storage.saveTail(sessionID, JSON.stringify({ model, sourceSeq, replay }), []);
2611
+ this.memorySources.set(sessionID, source);
2612
+ }
2613
+ async fenceMemory(sessionID, source) {
2614
+ const recorded = this.memorySources.get(sessionID);
2615
+ if (recorded === source)
2616
+ return;
2617
+ if (recorded === undefined && !this.storage.holdsState(sessionID) && !this.metadata.has(sessionID))
2618
+ return;
2619
+ await this.discardCapture(sessionID);
2620
+ this.reportCaptureReplacement(sessionID);
2621
+ }
2622
+ async discardCapture(sessionID) {
2623
+ if (this.stampMRU?.sessionID === sessionID)
2624
+ this.stampMRU = undefined;
2625
+ const source = this.sources.get(sessionID);
2626
+ const forgotten = this.storage.forget(sessionID);
2627
+ if (source !== undefined) {
2628
+ this.sources.set(sessionID, source);
2629
+ this.account(sessionID);
2630
+ }
2631
+ await forgotten;
2584
2632
  }
2585
2633
  async canFold(sessionID, reader) {
2586
2634
  if (this.options.protocol !== 3) {
@@ -2681,7 +2729,7 @@ ${cached}
2681
2729
  }
2682
2730
  };
2683
2731
  this.legacyUpgrades.set(draft.sessionID, { value, rows, started });
2684
- await this.retainReplay(draft.sessionID, replay, `${draft.model.providerID}/${draft.model.id}`, cut.seq - 1);
2732
+ await this.retainReplay(draft.sessionID, replay, `${draft.model.providerID}/${draft.model.id}`, cut.seq - 1, reader.hostIdentity());
2685
2733
  return true;
2686
2734
  }
2687
2735
  legacyUpgradeInput(sessionID, model) {
@@ -2689,8 +2737,16 @@ ${cached}
2689
2737
  if (!pending)
2690
2738
  return;
2691
2739
  let reader;
2740
+ const open = () => {
2741
+ const opened = this.openReader(sessionID);
2742
+ if (foldDigest(opened.hostIdentity()) !== pending.value.proof.sourceDigest) {
2743
+ opened.close();
2744
+ throw new NativeReplayUnavailable("Host database file changed during upgrade");
2745
+ }
2746
+ return opened;
2747
+ };
2692
2748
  const blobs = {
2693
- mediaBlob: (hash) => (reader ??= this.openReader(sessionID)).mediaBlob(hash)
2749
+ mediaBlob: (hash) => (reader ??= open()).mediaBlob(hash)
2694
2750
  };
2695
2751
  try {
2696
2752
  return pending.rows.filter((row) => row.seq > pending.value.replay.floor.after).flatMap((row) => {
@@ -2710,6 +2766,11 @@ ${cached}
2710
2766
  const admissions = this.admissions;
2711
2767
  if (!pending || !admissions)
2712
2768
  return false;
2769
+ const recorded = this.memorySources.get(sessionID);
2770
+ if (recorded === undefined ? this.storage.holdsState(sessionID) || this.metadata.has(sessionID) : foldDigest(recorded) !== pending.value.proof.sourceDigest) {
2771
+ await this.discardCapture(sessionID);
2772
+ this.reportCaptureReplacement(sessionID);
2773
+ }
2713
2774
  const replay = this.tail(sessionID)?.replay ?? pending.value.replay;
2714
2775
  if (replay.blocked)
2715
2776
  return false;
@@ -2747,13 +2808,16 @@ ${cached}
2747
2808
  stripCheckpointRecentContext(rendered, row.data.summary ?? "");
2748
2809
  return { row, rendered, submitted: row.data.summary ?? "" };
2749
2810
  }
2750
- async checkpointCovered(sessionID, checkpointID) {
2811
+ async checkpointCovered(sessionID, checkpointID, sourceIdentity) {
2751
2812
  const admissions = this.admissions;
2752
2813
  if (!admissions)
2753
2814
  return;
2815
+ await this.fenceMemory(sessionID, sourceIdentity);
2754
2816
  const pending = this.legacyUpgrades.get(sessionID);
2755
- const value = await this.readV3(sessionID) ?? pending?.value;
2756
- if (value?.replay.floor.kind !== "host-checkpoint" || value.replay.floor.id !== checkpointID || value.replay.withoutRecentContext)
2817
+ const sourceDigest = foldDigest(sourceIdentity);
2818
+ const saved = await this.readV3(sessionID);
2819
+ const value = saved?.proof.sourceDigest === sourceDigest ? saved : pending?.value;
2820
+ if (value?.proof.sourceDigest !== sourceDigest || value.replay.floor.kind !== "host-checkpoint" || value.replay.floor.id !== checkpointID || value.replay.withoutRecentContext)
2757
2821
  return;
2758
2822
  value.replay.withoutRecentContext = true;
2759
2823
  const payload = encodeAdmissionV3(value);
@@ -2764,7 +2828,7 @@ ${cached}
2764
2828
  });
2765
2829
  const tail = this.tail(sessionID);
2766
2830
  if (tail)
2767
- await this.retainReplay(sessionID, value.replay, tail.model, tail.sourceSeq);
2831
+ await this.retainReplay(sessionID, value.replay, tail.model, tail.sourceSeq, sourceIdentity);
2768
2832
  }
2769
2833
  async removeAdmission(key) {
2770
2834
  if (this.admissions?.remove)
@@ -2782,7 +2846,8 @@ ${cached}
2782
2846
  if (!saved || !isLocalCheckpoint(cut) || cut.session_id !== sessionID)
2783
2847
  return false;
2784
2848
  const proof = saved.proof;
2785
- if (proof.sourceDigest !== foldDigest(reader.hostIdentity()) || proof.admissionID !== cut.id || proof.summaryDigest !== foldDigest(cut.data.summary ?? "")) {
2849
+ const source = reader.hostIdentity();
2850
+ if (proof.sourceDigest !== foldDigest(source) || proof.admissionID !== cut.id || proof.summaryDigest !== foldDigest(cut.data.summary ?? "")) {
2786
2851
  this.report(sessionID, "replay refused: invalid checkpoint floor or provenance");
2787
2852
  return false;
2788
2853
  }
@@ -2798,17 +2863,17 @@ ${cached}
2798
2863
  this.editedAdmissions.set(sessionID, { original: saved, replay });
2799
2864
  this.staleHeads.add(sessionID);
2800
2865
  this.invalidate(sessionID);
2801
- await this.retainReplay(sessionID, replay, this.tail(sessionID)?.model ?? "", cut.seq - 1);
2866
+ await this.retainReplay(sessionID, replay, this.tail(sessionID)?.model ?? "", cut.seq - 1, source);
2802
2867
  return false;
2803
2868
  }
2804
2869
  if (!this.floorCovered(sessionID, replay.floor, this.historyCoverage(sessionID, reader), replay))
2805
2870
  return false;
2806
- await this.retainReplay(sessionID, saved.replay, this.tail(sessionID)?.model ?? "", cut.seq - 1);
2871
+ await this.retainReplay(sessionID, saved.replay, this.tail(sessionID)?.model ?? "", cut.seq - 1, source);
2807
2872
  if (proof.historyDigest !== historyDigest(reader, sessionID, cut.seq - 1, saved.replay.floor.after)) {
2808
2873
  this.staleHeads.add(sessionID);
2809
2874
  this.invalidate(sessionID);
2810
2875
  this.editedAdmissions.set(sessionID, { original: saved, replay });
2811
- await this.retainReplay(sessionID, saved.replay, this.tail(sessionID)?.model ?? "", cut.seq - 1);
2876
+ await this.retainReplay(sessionID, saved.replay, this.tail(sessionID)?.model ?? "", cut.seq - 1, source);
2812
2877
  return false;
2813
2878
  }
2814
2879
  await this.storage.supply(sessionID, cut.id, JSON.stringify({
@@ -2860,6 +2925,7 @@ ${cached}
2860
2925
  return false;
2861
2926
  const reader = this.openReader(sessionID);
2862
2927
  try {
2928
+ await this.fenceMemory(sessionID, reader.hostIdentity());
2863
2929
  const cut = reader.latestCompaction(sessionID);
2864
2930
  const proof = pending.original.proof;
2865
2931
  const replay = this.tail(sessionID)?.replay ?? pending.replay;
@@ -3064,14 +3130,23 @@ ${cached}
3064
3130
  }
3065
3131
  return [...grouped].map(([row, messages]) => this.record(row, messages));
3066
3132
  }
3067
- async capture(draft, native, boundary, restoredFirstRow) {
3133
+ async capture(draft, native, draftSourceIdentity, boundary, restoredFirstRow) {
3068
3134
  const release = this.storage.hold(draft.sessionID);
3069
3135
  let replay;
3136
+ let capturedIdentity;
3070
3137
  let sourceSeq = -1;
3071
3138
  try {
3072
3139
  rememberHostMedia(native);
3073
3140
  const reader = this.openReader(draft.sessionID);
3074
3141
  try {
3142
+ capturedIdentity = reader.hostIdentity();
3143
+ if (capturedIdentity !== draftSourceIdentity) {
3144
+ await this.discardCapture(draft.sessionID);
3145
+ this.reportCaptureReplacement(draft.sessionID);
3146
+ return;
3147
+ }
3148
+ await this.fenceMemory(draft.sessionID, capturedIdentity);
3149
+ this.memorySources.set(draft.sessionID, capturedIdentity);
3075
3150
  if (this.options.protocol === 3) {
3076
3151
  replay = await this.floorState(draft.sessionID, reader, boundary, draft);
3077
3152
  if (!replay && restoredFirstRow) {
@@ -3121,6 +3196,7 @@ ${cached}
3121
3196
  };
3122
3197
  await this.storage.saveTail(draft.sessionID, JSON.stringify(tail), rows);
3123
3198
  this.storage.truncate(draft.sessionID, through);
3199
+ this.memorySources.set(draft.sessionID, capturedIdentity);
3124
3200
  this.loaded.set(draft.sessionID, through);
3125
3201
  this.unavailable.delete(draft.sessionID);
3126
3202
  this.changed.delete(draft.sessionID);
@@ -3136,17 +3212,31 @@ ${cached}
3136
3212
  if (this.options.protocol === 3)
3137
3213
  this.report(draft.sessionID, `capture unavailable: ${String(error)}`);
3138
3214
  } finally {
3139
- this.account(draft.sessionID);
3140
- release();
3215
+ try {
3216
+ this.account(draft.sessionID);
3217
+ } finally {
3218
+ release();
3219
+ }
3141
3220
  }
3142
3221
  if (replay) {
3143
- await this.retainReplay(draft.sessionID, replay, `${draft.model.providerID}/${draft.model.id}`, sourceSeq);
3222
+ await this.retainReplay(draft.sessionID, replay, `${draft.model.providerID}/${draft.model.id}`, sourceSeq, draftSourceIdentity);
3144
3223
  const reader = this.openReader(draft.sessionID);
3145
3224
  try {
3146
- await this.saveCapturedReplay(draft.sessionID, replay, reader);
3225
+ const identity = reader.hostIdentity();
3226
+ if (identity !== capturedIdentity) {
3227
+ await this.discardCapture(draft.sessionID);
3228
+ this.reportCaptureReplacement(draft.sessionID);
3229
+ return;
3230
+ }
3231
+ const sourceDigest = foldDigest(identity);
3232
+ await this.saveCapturedReplay(draft.sessionID, replay, reader, sourceDigest);
3147
3233
  const saved = await this.readV3(draft.sessionID);
3148
3234
  const cut = reader.latestCompaction(draft.sessionID);
3149
3235
  if (saved && !this.editedAdmissions.has(draft.sessionID) && cut?.id === saved.proof.admissionID && (replay.floor.after > saved.replay.floor.after || !this.floorCovered(draft.sessionID, saved.replay.floor, this.historyCoverage(draft.sessionID, reader), saved.replay)) && (boundary && this.servedBaseline(draft) === boundary.summary || replay.floor.kind === "start" && restoredFirstRow === replay.floor.id)) {
3236
+ if (saved.proof.sourceDigest !== sourceDigest) {
3237
+ this.reportOnce(draft.sessionID, this.reportedForeignRefreshes, "v3 host fold: admission refresh skipped: admission belongs to a different host database file");
3238
+ return;
3239
+ }
3150
3240
  const value = {
3151
3241
  ...saved,
3152
3242
  replay: {
@@ -3179,13 +3269,18 @@ ${cached}
3179
3269
  try {
3180
3270
  return await this.supplyHeld(args);
3181
3271
  } finally {
3182
- this.account(args.draft.sessionID);
3183
- release();
3272
+ try {
3273
+ this.account(args.draft.sessionID);
3274
+ } finally {
3275
+ release();
3276
+ }
3184
3277
  }
3185
3278
  }
3186
3279
  async supplyHeld(args) {
3187
3280
  const { draft, reader, summary } = args;
3188
3281
  const stored = args.checkpointText ?? summary;
3282
+ const source = reader.hostIdentity();
3283
+ await this.fenceMemory(draft.sessionID, source);
3189
3284
  if (this.options.protocol === 3) {
3190
3285
  if (this.unprovenControls.has(draft.sessionID))
3191
3286
  return false;
@@ -3210,7 +3305,7 @@ ${cached}
3210
3305
  if (!args.manual && this.tail(draft.sessionID)?.model !== model)
3211
3306
  this.storage.replaceRows(draft.sessionID, reader.latestSequence(draft.sessionID), []);
3212
3307
  if (!args.manual)
3213
- await this.retainReplay(draft.sessionID, replay, model, reader.latestSequence(draft.sessionID));
3308
+ await this.retainReplay(draft.sessionID, replay, model, reader.latestSequence(draft.sessionID), source);
3214
3309
  if (!args.manual)
3215
3310
  await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
3216
3311
  source: this.storage.sourceID,
@@ -3272,7 +3367,7 @@ ${cached}
3272
3367
  this.invalidate(draft.sessionID);
3273
3368
  this.cancelLegacyUpgrade(draft.sessionID);
3274
3369
  this.cancelEditedAdmission(draft.sessionID);
3275
- await this.retainReplay(draft.sessionID, replay, model, running.seq - 1);
3370
+ await this.retainReplay(draft.sessionID, replay, model, running.seq - 1, source);
3276
3371
  await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
3277
3372
  source: this.storage.sourceID,
3278
3373
  sessionID: draft.sessionID,
@@ -3305,6 +3400,7 @@ ${cached}
3305
3400
  baseline: tail?.baseline ?? summary,
3306
3401
  sourceSeq: Math.max(tail?.sourceSeq ?? through, ...fresh.map((row) => row.seq))
3307
3402
  }), records);
3403
+ this.memorySources.set(draft.sessionID, source);
3308
3404
  this.dirty.delete(draft.sessionID);
3309
3405
  this.metadata.delete(draft.sessionID);
3310
3406
  this.metadataThrough.delete(draft.sessionID);
@@ -3333,13 +3429,16 @@ ${cached}
3333
3429
  }
3334
3430
  return true;
3335
3431
  }
3336
- async restore(sessionID, cut, model, options = {}) {
3432
+ async restore(sessionID, cut, model, passSource, options = {}) {
3337
3433
  let replay;
3338
3434
  let controls = [];
3339
3435
  let after;
3436
+ await this.fenceMemory(sessionID, passSource);
3340
3437
  if (this.options.protocol === 3) {
3341
3438
  const reader = this.openReader(sessionID);
3342
3439
  try {
3440
+ if (reader.hostIdentity() !== passSource)
3441
+ throw new NativeReplayUnavailable("Host database file changed during restore");
3343
3442
  replay = await this.floorState(sessionID, reader);
3344
3443
  if (replay) {
3345
3444
  controls = controlsThrough(replay.controls, reader, sessionID, cut.seq - 1);
@@ -3363,20 +3462,32 @@ ${cached}
3363
3462
  const release = this.storage.hold(sessionID);
3364
3463
  let result;
3365
3464
  try {
3366
- result = await this.restoreHeld(sessionID, cut, model, options);
3465
+ result = await this.restoreHeld(sessionID, cut, model, options, passSource);
3367
3466
  } finally {
3368
- this.account(sessionID);
3369
- release();
3467
+ try {
3468
+ this.account(sessionID);
3469
+ } finally {
3470
+ release();
3471
+ }
3370
3472
  }
3371
3473
  if (replay) {
3372
- await this.retainReplay(sessionID, replay, model, cut.seq - 1);
3474
+ await this.retainReplay(sessionID, replay, model, cut.seq - 1, passSource);
3373
3475
  return result && insertControls(result, controls, nativeRowID);
3374
3476
  }
3375
3477
  return result;
3376
3478
  }
3377
- async restoreHeld(sessionID, cut, model, options) {
3479
+ async restoreHeld(sessionID, cut, model, options, expected) {
3378
3480
  if (!isLocalCheckpoint(cut))
3379
3481
  return;
3482
+ const open = () => {
3483
+ const opened = this.openReader(sessionID);
3484
+ if (opened.hostIdentity() !== expected) {
3485
+ opened.close();
3486
+ throw new NativeReplayUnavailable("Host database file changed during restore");
3487
+ }
3488
+ this.memorySources.set(sessionID, expected);
3489
+ return opened;
3490
+ };
3380
3491
  const resolvedModel = {
3381
3492
  providerID: model.slice(0, model.indexOf("/")),
3382
3493
  id: model.slice(model.indexOf("/") + 1)
@@ -3392,13 +3503,13 @@ ${cached}
3392
3503
  const max = this.storage.db.prepare("SELECT COALESCE(MAX(id),0) AS id FROM m0_mutation_log WHERE session_id=?").get(sessionID);
3393
3504
  const boundary = this.storage.db.prepare("SELECT cached_m0_last_baseline_end_message_id AS id FROM session_meta WHERE session_id=?").get(sessionID)?.id;
3394
3505
  if (max.id <= (state.cachedM0MaxMutationId ?? 0) && boundary) {
3395
- after = this.storage.sequenceForID(sessionID, boundary) ?? (reader = this.openReader(sessionID)).sequenceForId(sessionID, boundary) ?? -1;
3506
+ after = this.storage.sequenceForID(sessionID, boundary) ?? (reader = open()).sequenceForId(sessionID, boundary) ?? -1;
3396
3507
  }
3397
3508
  }
3398
3509
  const through = cut.seq - 1;
3399
3510
  let metadata = this.metadata.get(sessionID);
3400
3511
  if (!metadata || (this.metadataThrough.get(sessionID) ?? -1) < through || this.options.protocol === 3 && this.options.rendersHistory?.(sessionID) && (this.metadataAfter.get(sessionID) ?? -1) > after) {
3401
- reader ??= this.openReader(sessionID);
3512
+ reader ??= open();
3402
3513
  metadata = reader.rowStampsThrough(sessionID, through, this.options.protocol === 3 ? after : undefined).filter(conversational);
3403
3514
  this.metadata.set(sessionID, metadata);
3404
3515
  this.metadataThrough.set(sessionID, through);
@@ -3406,7 +3517,7 @@ ${cached}
3406
3517
  }
3407
3518
  const required = metadata.filter((row) => row.seq > after && row.seq <= through);
3408
3519
  if (this.dirty.has(sessionID) || !tail || (this.loaded.get(sessionID) ?? -1) < through && !this.storage.admission(sessionID, cut.id) || !this.storage.containsRows(sessionID, required.map((row) => row.id))) {
3409
- const source = reader ??= this.openReader(sessionID);
3520
+ const source = reader ??= open();
3410
3521
  const fresh = source.range(sessionID, after, through).filter(conversational);
3411
3522
  const records = fresh.map((row) => this.record(row, this.rendered(row, resolvedModel, source)));
3412
3523
  const restored = new Set(records.map((row) => row.id));
@@ -3436,9 +3547,11 @@ ${cached}
3436
3547
  }
3437
3548
  return messages;
3438
3549
  }
3439
- async restoreRead(sessionID, cut, model) {
3550
+ async restoreRead(sessionID, cut, model, passSource) {
3440
3551
  const reader = this.openReader(sessionID);
3441
3552
  try {
3553
+ if (reader.hostIdentity() !== passSource)
3554
+ throw new NativeReplayUnavailable("Host checkpoint replay boundary is unavailable");
3442
3555
  const replay = this.options.protocol === 3 ? await this.floorState(sessionID, reader) : undefined;
3443
3556
  if (this.options.protocol === 3 && !replay) {
3444
3557
  this.report(sessionID, "replay refused: missing usable checkpoint boundary");
@@ -3465,6 +3578,7 @@ ${cached}
3465
3578
  }
3466
3579
  clear(sessionID) {
3467
3580
  this.sources.delete(sessionID);
3581
+ this.memorySources.delete(sessionID);
3468
3582
  this.metadata.delete(sessionID);
3469
3583
  this.metadataThrough.delete(sessionID);
3470
3584
  this.metadataAfter.delete(sessionID);
@@ -3479,6 +3593,8 @@ ${cached}
3479
3593
  if (this.stampMRU?.sessionID === sessionID)
3480
3594
  this.stampMRU = undefined;
3481
3595
  this.unprovenControls.delete(sessionID);
3596
+ this.reportedCaptureReplacements.delete(sessionID);
3597
+ this.reportedForeignRefreshes.delete(sessionID);
3482
3598
  this.cancelLegacyUpgrade(sessionID);
3483
3599
  this.cancelEditedAdmission(sessionID);
3484
3600
  if (this.admissions)
@@ -6653,7 +6769,10 @@ class V2RevertMessageCleanup {
6653
6769
  sessionId: sessionID,
6654
6770
  isInHostStore: (id) => source.sequenceForId(sessionID, id) !== undefined,
6655
6771
  removedByCommittedRevert: removed,
6656
- onRetire: () => clearRecompStaging(db, sessionID)
6772
+ onRetire: () => {
6773
+ clearRecompStaging(db, sessionID);
6774
+ return;
6775
+ }
6657
6776
  });
6658
6777
  if (result.kind !== "unresolved") {
6659
6778
  this.pendingHistory = this.pendingHistory.filter((proof) => proof.sessionID !== sessionID);
@@ -7336,7 +7455,10 @@ function checkHistoryBoundary(db, reader, sessionID) {
7336
7455
  db,
7337
7456
  sessionId: sessionID,
7338
7457
  isInHostStore: (messageId) => storeHasSession ? reader.sequenceForId(sessionID, messageId) !== undefined : null,
7339
- onRetire: () => clearRecompStaging(db, sessionID)
7458
+ onRetire: () => {
7459
+ clearRecompStaging(db, sessionID);
7460
+ return;
7461
+ }
7340
7462
  });
7341
7463
  } catch (error) {
7342
7464
  sessionLog2(sessionID, "history boundary check failed; continuing without repair", error);
@@ -9070,11 +9192,12 @@ async function setupContext(context, config, nativeRemove, compactionOff, cleanu
9070
9192
  let recordedHead;
9071
9193
  let upgradeCheckpointSequence;
9072
9194
  let restoredFirstRow;
9195
+ let draftSourceIdentity;
9073
9196
  try {
9074
9197
  syntheticIDs = await readSyntheticProvenance(reader, draft.messages);
9075
9198
  const cut = reader.latestCompaction(draft.sessionID);
9076
9199
  if (nativeFoldReplay)
9077
- await nativeFoldReplay.observeSource(draft.sessionID, reader);
9200
+ draftSourceIdentity = await nativeFoldReplay.observeSource(draft.sessionID, reader);
9078
9201
  const boundaryRepair = !rustModeModuleClient && db && !compactionOff ? revertMessageCleanup.retireProvenHistory(db, draft.sessionID, reader) ?? checkHistoryBoundary(db, reader, draft.sessionID) : undefined;
9079
9202
  if (boundaryRepair?.kind === "repaired" || boundaryRepair?.kind === "baseline-reset") {
9080
9203
  historyRefreshSessions.add(draft.sessionID);
@@ -9114,13 +9237,14 @@ async function setupContext(context, config, nativeRemove, compactionOff, cleanu
9114
9237
  }
9115
9238
  }
9116
9239
  let nativeRestored = localCut ? nativeFoldReplay?.legacyUpgradeInput(draft.sessionID, draft.model) : undefined;
9117
- if (nativeRestored === undefined && cut && nativeFoldReplay && await nativeFoldReplay.admits(draft.sessionID, cut, reader)) {
9240
+ if (nativeRestored === undefined && cut && nativeFoldReplay && draftSourceIdentity !== undefined && await nativeFoldReplay.admits(draft.sessionID, cut, reader)) {
9241
+ const passSource = draftSourceIdentity;
9118
9242
  try {
9119
- nativeRestored = await nativeFoldReplay.restore(draft.sessionID, cut, `${draft.model.providerID}/${draft.model.id}`, { bounded: !rustModeModuleClient });
9243
+ nativeRestored = await nativeFoldReplay.restore(draft.sessionID, cut, `${draft.model.providerID}/${draft.model.id}`, passSource, { bounded: !rustModeModuleClient });
9120
9244
  } catch (error) {
9121
9245
  nativeFoldReplay.invalidate(draft.sessionID, true);
9122
9246
  sessionLog2(draft.sessionID, "v2 host fold: cache unavailable; restoring host rows", error);
9123
- nativeRestored = await nativeFoldReplay.restoreRead(draft.sessionID, cut, draft.model);
9247
+ nativeRestored = await nativeFoldReplay.restoreRead(draft.sessionID, cut, draft.model, passSource);
9124
9248
  }
9125
9249
  if (nativeRestored)
9126
9250
  pass.nativeReplayCheckpointID = cut.id;
@@ -9269,8 +9393,8 @@ async function setupContext(context, config, nativeRemove, compactionOff, cleanu
9269
9393
  const stripped = await folds.stripRecentContext(draft.sessionID, checkpointID);
9270
9394
  if (stripped?.rendered) {
9271
9395
  checkpoint = structuredClone(stripped.rendered);
9272
- if (upgradeCheckpointSequence !== undefined)
9273
- await nativeFoldReplay?.checkpointCovered(draft.sessionID, checkpointID);
9396
+ if (upgradeCheckpointSequence !== undefined && draftSourceIdentity !== undefined)
9397
+ await nativeFoldReplay?.checkpointCovered(draft.sessionID, checkpointID, draftSourceIdentity);
9274
9398
  }
9275
9399
  }
9276
9400
  }
@@ -9288,11 +9412,11 @@ async function setupContext(context, config, nativeRemove, compactionOff, cleanu
9288
9412
  } : null;
9289
9413
  const servedHead = head && structuredClone(head);
9290
9414
  const headApplied = head ? applyCheckpointHead(draft, head) : false;
9291
- if (nativeMessages && (!checkpoint || upgradeCheckpointSequence !== undefined)) {
9415
+ if (nativeMessages && draftSourceIdentity !== undefined && (!checkpoint || upgradeCheckpointSequence !== undefined)) {
9292
9416
  try {
9293
9417
  const state = db && getOrCreateSessionMeta(db, draft.sessionID);
9294
9418
  const boundary = db?.prepare("SELECT cached_m0_last_baseline_end_message_id AS id FROM session_meta WHERE session_id=?").get(draft.sessionID);
9295
- await nativeFoldReplay?.capture(draft, nativeMessages, boundary?.id && state?.cachedM0Bytes ? { id: boundary.id, summary: state.cachedM0Bytes.toString("utf8") } : undefined, restoredFirstRow);
9419
+ await nativeFoldReplay?.capture(draft, nativeMessages, draftSourceIdentity, boundary?.id && state?.cachedM0Bytes ? { id: boundary.id, summary: state.cachedM0Bytes.toString("utf8") } : undefined, restoredFirstRow);
9296
9420
  } catch (error) {
9297
9421
  nativeFoldReplay?.invalidate(draft.sessionID, true);
9298
9422
  sessionLog2(draft.sessionID, "v2 host fold: optional capture unavailable", error);