@cortexkit/opencode-magic-context 0.49.35 → 0.49.37

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 (202) hide show
  1. package/README.md +1 -1
  2. package/dist/auto-search-worker.js +163 -160
  3. package/dist/chunk-embedding-write-worker.js +58 -56
  4. package/dist/{compaction-marker-candidates-g0p67ced.js → compaction-marker-candidates-5p1qkve9.js} +3 -1
  5. package/dist/compaction-marker-candidates-worker.js +24 -23
  6. package/dist/{compartment-chunk-embedding-y4136are.js → compartment-chunk-embedding-ypktm0tk.js} +5 -4
  7. package/dist/{conflict-warning-hook-hhw8z5ak.js → conflict-warning-hook-b75jn458.js} +6 -5
  8. package/dist/{context-fk8e3h9p.js → context-ngk2jz60.js} +501 -97
  9. package/dist/{embedding-backfill-q6n72sy0.js → embedding-backfill-rma0v2ct.js} +9 -8
  10. package/dist/embedding-worker.js +2 -2
  11. package/dist/features/magic-context/compaction-marker-candidates.d.ts.map +1 -1
  12. package/dist/features/magic-context/compaction-marker.d.ts +5 -3
  13. package/dist/features/magic-context/compaction-marker.d.ts.map +1 -1
  14. package/dist/features/magic-context/dreamer/task-gates.d.ts.map +1 -1
  15. package/dist/features/magic-context/dreamer/verify-gate.d.ts +2 -0
  16. package/dist/features/magic-context/dreamer/verify-gate.d.ts.map +1 -1
  17. package/dist/features/magic-context/dreamer/verify-state.d.ts +9 -0
  18. package/dist/features/magic-context/dreamer/verify-state.d.ts.map +1 -0
  19. package/dist/features/magic-context/dreamer/verify.d.ts +2 -1
  20. package/dist/features/magic-context/dreamer/verify.d.ts.map +1 -1
  21. package/dist/features/magic-context/migration-worker-client.d.ts +3 -0
  22. package/dist/features/magic-context/migration-worker-client.d.ts.map +1 -1
  23. package/dist/features/magic-context/migration-worker.d.ts +15 -0
  24. package/dist/features/magic-context/migration-worker.d.ts.map +1 -1
  25. package/dist/features/magic-context/overflow-detection.d.ts +2 -2
  26. package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
  27. package/dist/features/magic-context/search-rerank.d.ts.map +1 -1
  28. package/dist/features/magic-context/storage-meta-persisted.d.ts +1 -0
  29. package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
  30. package/dist/features/magic-context/storage-tags.d.ts +4 -0
  31. package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
  32. package/dist/features/magic-context/work-metrics-carry-storage.d.ts +10 -0
  33. package/dist/features/magic-context/work-metrics-carry-storage.d.ts.map +1 -0
  34. package/dist/hooks/magic-context/auto-search-worker-client.d.ts +5 -1
  35. package/dist/hooks/magic-context/auto-search-worker-client.d.ts.map +1 -1
  36. package/dist/hooks/magic-context/compaction-marker-publication.d.ts.map +1 -1
  37. package/dist/hooks/magic-context/compartment-runner-historian.d.ts.map +1 -1
  38. package/dist/hooks/magic-context/issue-423-fixture.d.ts +1 -1
  39. package/dist/hooks/magic-context/issue-423-fixture.d.ts.map +1 -1
  40. package/dist/hooks/magic-context/issue-424-fixture.d.ts +1 -1
  41. package/dist/hooks/magic-context/issue-424-fixture.d.ts.map +1 -1
  42. package/dist/hooks/magic-context/project-structural-sentinels.d.ts +25 -0
  43. package/dist/hooks/magic-context/project-structural-sentinels.d.ts.map +1 -0
  44. package/dist/hooks/magic-context/protected-tail-boundary.d.ts +8 -1
  45. package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
  46. package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts.map +1 -1
  47. package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts +2 -0
  48. package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
  49. package/dist/hooks/magic-context/release-recovery-prefix.d.ts +9 -0
  50. package/dist/hooks/magic-context/release-recovery-prefix.d.ts.map +1 -0
  51. package/dist/hooks/magic-context/strip-structural-noise.d.ts +6 -3
  52. package/dist/hooks/magic-context/strip-structural-noise.d.ts.map +1 -1
  53. package/dist/hooks/magic-context/transform-context-state.d.ts +3 -0
  54. package/dist/hooks/magic-context/transform-context-state.d.ts.map +1 -1
  55. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +3 -0
  56. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  57. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  58. package/dist/{index-cpjk5jd2.js → index-1d042x6w.js} +16 -14
  59. package/dist/{index-jn0ddrhp.js → index-21fgrfrf.js} +8 -6
  60. package/dist/{index-pq0dpwrc.js → index-2rs7912w.js} +2 -2
  61. package/dist/{index-ezv6dz3y.js → index-4ks89h5a.js} +4 -4
  62. package/dist/{index-vzhhr1ht.js → index-4y35msh5.js} +1 -2
  63. package/dist/{index-f297z1n8.js → index-72czy45z.js} +5 -5
  64. package/dist/index-7v05d9b7.js +58 -0
  65. package/dist/{index-f9b3vtt1.js → index-850vthn3.js} +8 -2
  66. package/dist/{index-ksv0a31x.js → index-8jjjmhab.js} +12 -1
  67. package/dist/index-96ztjvkn.js +204 -0
  68. package/dist/{index-gyaa159j.js → index-9fgmkfaf.js} +6 -3
  69. package/dist/{index-4bgk5ztz.js → index-9tdvbdv1.js} +187 -90
  70. package/dist/{index-2v6nvy3k.js → index-ae1ar4v1.js} +1 -1
  71. package/dist/{index-faxb7mck.js → index-b1ydbp95.js} +1 -1
  72. package/dist/{index-nxrsns5x.js → index-c5dzns43.js} +75 -75
  73. package/dist/{index-zhqwda72.js → index-dcjkw41m.js} +1 -1
  74. package/dist/{index-x2mrq5xv.js → index-dfdg2cgg.js} +179 -49
  75. package/dist/{index-87ctnzz9.js → index-dz48h1wx.js} +2 -2
  76. package/dist/{index-b6xz3qbe.js → index-gnmhbyyc.js} +366 -83
  77. package/dist/{index-qx8j6kds.js → index-j105mf16.js} +2 -2
  78. package/dist/{index-p56dszfp.js → index-jggt5gf2.js} +1 -1
  79. package/dist/{index-xwwsbsd3.js → index-pp8vdn01.js} +5 -5
  80. package/dist/{index-dy7w6wm4.js → index-prw8apnc.js} +5 -5
  81. package/dist/{index-qncw9hey.js → index-r2epb000.js} +33 -3
  82. package/dist/{index-brg2w6qc.js → index-tn9drm1a.js} +16 -14
  83. package/dist/{index-pswsyf1w.js → index-ty4vhe4n.js} +3 -3
  84. package/dist/{index-pztqab6k.js → index-vd2ywmfe.js} +1 -1
  85. package/dist/{index-3j9t1v4n.js → index-wcwhhtdh.js} +10 -9
  86. package/dist/{index-1rhgxqs8.js → index-xd2p95m7.js} +1 -1
  87. package/dist/{index-0c7p8mq5.js → index-xnm0wq1r.js} +1 -1
  88. package/dist/{index-3d3872c6.js → index-ys7j6679.js} +4 -4
  89. package/dist/index.js +38 -37
  90. package/dist/{message-fts-rowid-map-xg7hr0mk.js → message-fts-rowid-map-t6qsyg9s.js} +4 -3
  91. package/dist/{message-index-bounded-narw7bnz.js → message-index-bounded-wfk6aqxk.js} +9 -8
  92. package/dist/migration-worker.js +45 -40
  93. package/dist/plugin/rpc-handlers.d.ts.map +1 -1
  94. package/dist/{project-embedding-registry-tdz7985n.js → project-embedding-registry-3yyegjma.js} +8 -7
  95. package/dist/raw-ordinal-warmup-worker.js +114 -118
  96. package/dist/{read-session-chunk-vmg1zjg0.js → read-session-chunk-26yrrhyz.js} +7 -6
  97. package/dist/{read-session-raw-mncmzbm9.js → read-session-raw-j7c9w57v.js} +3 -2
  98. package/dist/{recomp-orchestrator-qyyhnc9b.js → recomp-orchestrator-wk509qkp.js} +20 -19
  99. package/dist/{render-c5p0k846.js → render-h1qarr8a.js} +13 -12
  100. package/dist/{search-f6hwvcxk.js → search-5h9fydyq.js} +16 -15
  101. package/dist/{search-archive-0ny7p3bw.js → search-archive-q79hyqxs.js} +9 -8
  102. package/dist/{send-session-notification-0gwcz3fw.js → send-session-notification-14p3yje4.js} +5 -4
  103. package/dist/shared/model-suggestion-retry.d.ts +7 -0
  104. package/dist/shared/model-suggestion-retry.d.ts.map +1 -1
  105. package/dist/shared/prompt-async-transport.d.ts.map +1 -1
  106. package/dist/shared/reusable-worker-entry.d.ts +4 -0
  107. package/dist/shared/reusable-worker-entry.d.ts.map +1 -0
  108. package/dist/shared/reusable-worker.d.ts +6 -0
  109. package/dist/shared/reusable-worker.d.ts.map +1 -0
  110. package/dist/shared/reusable-worker.fixture.d.ts +2 -0
  111. package/dist/shared/reusable-worker.fixture.d.ts.map +1 -0
  112. package/dist/shared/worker-shutdown.d.ts +1 -1
  113. package/dist/shared/worker-shutdown.d.ts.map +1 -1
  114. package/dist/{storage-tags-zx1023km.js → storage-tags-vj0yj7ed.js} +1 -1
  115. package/dist/{store-620rjzt9.js → store-s3eykdjt.js} +1 -1
  116. package/dist/{transform-postprocess-phase-807ff6t2.js → transform-postprocess-phase-4gj0mtcg.js} +21 -20
  117. package/dist/v2/auto-search-worker.js +163 -160
  118. package/dist/v2/chunk-embedding-write-worker.js +59 -57
  119. package/dist/v2/compaction-marker-candidates-worker.js +24 -23
  120. package/dist/v2/{compaction-marker-candidates-t1aq6gsj.js → compaction-marker-candidates-yeb2ky46.js} +4 -2
  121. package/dist/v2/{compartment-chunk-embedding-ncsk0r9e.js → compartment-chunk-embedding-qfk791bd.js} +6 -5
  122. package/dist/v2/{context-znqfkap4.js → context-05x7kr7f.js} +1027 -215
  123. package/dist/v2/{embedding-backfill-11j425e0.js → embedding-backfill-00tw59rx.js} +10 -9
  124. package/dist/v2/embedding-worker.js +2 -2
  125. package/dist/v2/fold/code-mode-catalog.d.ts +3 -0
  126. package/dist/v2/fold/code-mode-catalog.d.ts.map +1 -0
  127. package/dist/v2/fold/host-media.d.ts +7 -3
  128. package/dist/v2/fold/host-media.d.ts.map +1 -1
  129. package/dist/v2/fold/idle.d.ts +1 -0
  130. package/dist/v2/fold/idle.d.ts.map +1 -1
  131. package/dist/v2/fold/manual-baseline.d.ts +8 -0
  132. package/dist/v2/fold/manual-baseline.d.ts.map +1 -0
  133. package/dist/v2/fold/native-replay-v3.d.ts +7 -0
  134. package/dist/v2/fold/native-replay-v3.d.ts.map +1 -1
  135. package/dist/v2/fold/native-replay.d.ts +24 -7
  136. package/dist/v2/fold/native-replay.d.ts.map +1 -1
  137. package/dist/v2/fold/restore.d.ts.map +1 -1
  138. package/dist/v2/fold/summary.d.ts +4 -0
  139. package/dist/v2/fold/summary.d.ts.map +1 -0
  140. package/dist/v2/hidden-completion.d.ts.map +1 -1
  141. package/dist/v2/hooks/channel2.d.ts +6 -0
  142. package/dist/v2/hooks/channel2.d.ts.map +1 -1
  143. package/dist/v2/hooks/context.d.ts.map +1 -1
  144. package/dist/v2/hooks/restore-rows.d.ts +12 -2
  145. package/dist/v2/hooks/restore-rows.d.ts.map +1 -1
  146. package/dist/v2/hooks/synthetic-admission.d.ts +7 -0
  147. package/dist/v2/hooks/synthetic-admission.d.ts.map +1 -0
  148. package/dist/v2/hooks/usage-persist.d.ts.map +1 -1
  149. package/dist/v2/{message-fts-rowid-map-rmk571mb.js → message-fts-rowid-map-z86avy0n.js} +5 -4
  150. package/dist/v2/{message-index-bounded-620bppw7.js → message-index-bounded-w14496s9.js} +9 -8
  151. package/dist/v2/migration-worker.js +46 -41
  152. package/dist/v2/{project-embedding-registry-bm5fe1a8.js → project-embedding-registry-jqq2qeqb.js} +9 -8
  153. package/dist/v2/raw-ordinal-warmup-worker.js +114 -118
  154. package/dist/v2/{read-session-chunk-qfwv1k71.js → read-session-chunk-tqnh1ret.js} +7 -6
  155. package/dist/v2/{read-session-raw-q294t8xs.js → read-session-raw-qpp6x9kg.js} +4 -3
  156. package/dist/v2/{recomp-orchestrator-fcnjref7.js → recomp-orchestrator-9cjk6aej.js} +19 -18
  157. package/dist/v2/{render-98a1qv0b.js → render-5dz1knf2.js} +13 -12
  158. package/dist/v2/{search-z1zqzsmh.js → search-8yfet25y.js} +15 -14
  159. package/dist/v2/{search-archive-r0y1bafr.js → search-archive-z6z9jqwf.js} +9 -8
  160. package/dist/v2/{send-session-notification-fst6hkg7.js → send-session-notification-kxp47tsw.js} +4 -3
  161. package/dist/v2/{server-cpjk5jd2.js → server-1d042x6w.js} +16 -14
  162. package/dist/v2/{server-fqj6ddpr.js → server-5by3vdzs.js} +186 -89
  163. package/dist/v2/{server-5jp38p98.js → server-6emy0v9a.js} +1 -2
  164. package/dist/v2/{server-rktnzs89.js → server-6vzh6wej.js} +1 -1
  165. package/dist/v2/{server-qa74bj91.js → server-7094pk3w.js} +9 -8
  166. package/dist/v2/{server-bxpz2dcn.js → server-7acq7815.js} +8 -6
  167. package/dist/v2/server-7v05d9b7.js +58 -0
  168. package/dist/v2/{server-24g8r2d0.js → server-7zacmty5.js} +75 -75
  169. package/dist/v2/server-96ztjvkn.js +204 -0
  170. package/dist/v2/{server-hnknwaz8.js → server-a9w5f0yw.js} +3 -3
  171. package/dist/v2/{server-9k0mjef7.js → server-avdveykk.js} +1 -1
  172. package/dist/v2/{server-hsf0qsn5.js → server-cp1a03vc.js} +1 -1
  173. package/dist/v2/{server-r3chnxs5.js → server-dfq5vb8j.js} +2 -2
  174. package/dist/v2/{server-x4m6xcn4.js → server-e51xmr4d.js} +6 -6
  175. package/dist/v2/{server-7ccxt4nq.js → server-g3gqq4ac.js} +6 -3
  176. package/dist/v2/{server-kkxzwa6w.js → server-gkse1wyz.js} +3 -3
  177. package/dist/v2/{server-1xesqm5h.js → server-jd7vzwn4.js} +15 -13
  178. package/dist/v2/{server-ws5exe6q.js → server-nmyganc8.js} +1 -1
  179. package/dist/v2/{server-q0z2q2dx.js → server-nq3cb7be.js} +1 -1
  180. package/dist/v2/{server-55v5hpth.js → server-qjyp27vf.js} +2 -2
  181. package/dist/v2/{server-xbtw0p9t.js → server-r4430x9m.js} +9 -3
  182. package/dist/v2/{server-qf2kzsh2.js → server-t8y64xj7.js} +4 -4
  183. package/dist/v2/{server-s9xgyyj9.js → server-tnzbk64q.js} +34 -4
  184. package/dist/v2/{server-51779zs2.js → server-v0p37hm2.js} +4 -1
  185. package/dist/v2/{server-bpvsvtph.js → server-xfjjbrkt.js} +12 -1
  186. package/dist/v2/server.js +4 -4
  187. package/dist/v2/{storage-tags-19s0ebf0.js → storage-tags-cw07ky67.js} +1 -1
  188. package/dist/v2/{store-vdm8v3jd.js → store-z3qe67cg.js} +1 -1
  189. package/dist/v2/{v2-archive-reader-t2c0eypn.js → v2-archive-reader-z7wr42ry.js} +3 -3
  190. package/dist/v2/{wrapup-orchestrator-aqpqsveg.js → wrapup-orchestrator-j1v62bkj.js} +19 -18
  191. package/dist/{v2-archive-reader-tm2eayp2.js → v2-archive-reader-028z6kck.js} +2 -2
  192. package/dist/{wrapup-orchestrator-k5yp19a6.js → wrapup-orchestrator-xg9avt2p.js} +21 -20
  193. package/package.json +1 -1
  194. package/src/shared/model-suggestion-retry.ts +1 -1
  195. package/src/shared/prompt-async-transport.test.ts +64 -3
  196. package/src/shared/prompt-async-transport.ts +61 -16
  197. package/src/shared/reusable-worker-entry.ts +58 -0
  198. package/src/shared/reusable-worker-reset.test.ts +108 -0
  199. package/src/shared/reusable-worker.fixture.ts +35 -0
  200. package/src/shared/reusable-worker.test.ts +253 -0
  201. package/src/shared/reusable-worker.ts +240 -0
  202. package/src/shared/worker-shutdown.ts +21 -17
@@ -37,7 +37,7 @@ import {
37
37
  rebaseSessionCoordinatesAsync,
38
38
  resolveOpenCodeProtectedTailBoundary,
39
39
  getActiveCompartmentRun
40
- } from "./index-4bgk5ztz.js";
40
+ } from "./index-9tdvbdv1.js";
41
41
  import"./index-sd3egckz.js";
42
42
  import {
43
43
  getHarness2
@@ -61,7 +61,7 @@ import {
61
61
  resolveKnownHistorianContextLimit2,
62
62
  resolveHistorianContextLimit2
63
63
  } from "./index-dnqqqc5f.js";
64
- import"./index-nxrsns5x.js";
64
+ import"./index-c5dzns43.js";
65
65
  import {
66
66
  CANONICAL_DREAM_TASKS,
67
67
  DREAM_TASK_CAPABILITIES,
@@ -131,7 +131,7 @@ import {
131
131
  applyJsonSchemaParameterDescriptions,
132
132
  MagicContextRpcServer,
133
133
  subagentTemperature
134
- } from "./index-b6xz3qbe.js";
134
+ } from "./index-gnmhbyyc.js";
135
135
  import {
136
136
  resolveHistorianModel2
137
137
  } from "./index-5kym00ag.js";
@@ -154,8 +154,9 @@ import {
154
154
  isTransientSqliteError2,
155
155
  withAsyncPrivilegedWriter2
156
156
  } from "./index-gzy5w8ya.js";
157
- import"./index-cpjk5jd2.js";
158
- import"./index-qncw9hey.js";
157
+ import"./index-96ztjvkn.js";
158
+ import"./index-1d042x6w.js";
159
+ import"./index-r2epb000.js";
159
160
  import"./index-1zx87sdm.js";
160
161
  import {
161
162
  stripPersistedAssistantText,
@@ -164,7 +165,7 @@ import {
164
165
  isDroppedToolOutput,
165
166
  evaluateChannel2,
166
167
  buildChannel2Reminder
167
- } from "./index-ksv0a31x.js";
168
+ } from "./index-8jjjmhab.js";
168
169
  import {
169
170
  hasMeaningfulUserText2,
170
171
  preloadTokenizer,
@@ -174,7 +175,7 @@ import {
174
175
  getTableColumnNames
175
176
  } from "./index-9wnbnx8f.js";
176
177
  import"./index-58e3y90n.js";
177
- import"./index-pztqab6k.js";
178
+ import"./index-vd2ywmfe.js";
178
179
  import {
179
180
  needsBlockedRead,
180
181
  DreamTokenBudgetExceeded,
@@ -207,17 +208,17 @@ import {
207
208
  cleanUserText2,
208
209
  readRawSessionMessageIdOrdinalsForRange2,
209
210
  readRawSessionMessageOrdinalById2
210
- } from "./index-brg2w6qc.js";
211
+ } from "./index-tn9drm1a.js";
211
212
  import {
212
213
  BoundedSessionMap,
213
214
  recordToolDefinitionSet,
214
215
  getCurrentToolSetHash
215
216
  } from "./index-jkanybgq.js";
216
- import"./index-pq0dpwrc.js";
217
- import"./index-faxb7mck.js";
217
+ import"./index-2rs7912w.js";
218
+ import"./index-b1ydbp95.js";
218
219
  import {
219
220
  scheduleClearAndReindex
220
- } from "./index-87ctnzz9.js";
221
+ } from "./index-dz48h1wx.js";
221
222
  import {
222
223
  invalidateAutoEmbedSession,
223
224
  clearEmbedSessionState
@@ -225,7 +226,7 @@ import {
225
226
  import"./index-g4t7850m.js";
226
227
  import {
227
228
  deleteChunkEmbedBackoffForSession2
228
- } from "./index-zhqwda72.js";
229
+ } from "./index-dcjkw41m.js";
229
230
  import"./index-2p37txes.js";
230
231
  import {
231
232
  advanceSessionActivity,
@@ -252,8 +253,9 @@ import {
252
253
  noteLkgProviderResponse,
253
254
  clearLkgMeasuredRequest,
254
255
  getSlot,
256
+ dropSlot,
255
257
  copySessionStateForClone
256
- } from "./index-f297z1n8.js";
258
+ } from "./index-72czy45z.js";
257
259
  import {
258
260
  startBootDeadline,
259
261
  BOOT_SQLITE_BUSY_TIMEOUT_MS,
@@ -261,7 +263,7 @@ import {
261
263
  openDatabaseAsync,
262
264
  isDatabasePersisted,
263
265
  getDatabasePersistenceError
264
- } from "./index-gyaa159j.js";
266
+ } from "./index-9fgmkfaf.js";
265
267
  import"./index-fr0wbhdn.js";
266
268
  import"./index-nj360hcs.js";
267
269
  import {
@@ -280,29 +282,29 @@ import {
280
282
  EmergencyFailClosedError,
281
283
  DegradedPassRefusalError,
282
284
  isSyntheticHeadMessage2
283
- } from "./index-x2mrq5xv.js";
284
- import"./index-f9b3vtt1.js";
285
+ } from "./index-dfdg2cgg.js";
286
+ import"./index-850vthn3.js";
285
287
  import"./index-qt1qqasc.js";
286
288
  import {
287
289
  retainProjectEmbedding2
288
- } from "./index-jn0ddrhp.js";
290
+ } from "./index-21fgrfrf.js";
289
291
  import"./index-jz5bk7xd.js";
290
292
  import"./index-4dhv7mqz.js";
291
- import"./index-1rhgxqs8.js";
292
- import"./index-pswsyf1w.js";
293
+ import"./index-xd2p95m7.js";
294
+ import"./index-ty4vhe4n.js";
293
295
  import"./index-jjvbk92s.js";
294
296
  import {
295
297
  renderUserFacingFailure,
296
298
  userFacingFailureCode
297
- } from "./index-0c7p8mq5.js";
299
+ } from "./index-xnm0wq1r.js";
298
300
  import {
299
301
  pushNotification2,
300
302
  isTuiConnected2
301
303
  } from "./index-x8a7jmkh.js";
302
304
  import"./index-fw42ms3m.js";
303
- import"./index-2v6nvy3k.js";
304
- import"./index-3j9t1v4n.js";
305
- import"./index-3d3872c6.js";
305
+ import"./index-ae1ar4v1.js";
306
+ import"./index-wcwhhtdh.js";
307
+ import"./index-ys7j6679.js";
306
308
  import"./index-gp3kcpc4.js";
307
309
  import {
308
310
  detectConflicts
@@ -323,12 +325,12 @@ import {
323
325
  readV2RawMessagePagesForConversion2,
324
326
  createV2RawMessageReader2,
325
327
  createV2RawMessageProvider2
326
- } from "./index-vzhhr1ht.js";
328
+ } from "./index-4y35msh5.js";
327
329
  import"./index-fmsr7v69.js";
328
- import"./index-qx8j6kds.js";
330
+ import"./index-j105mf16.js";
329
331
  import"./index-kaqs6h31.js";
330
- import"./index-dy7w6wm4.js";
331
- import"./index-ezv6dz3y.js";
332
+ import"./index-prw8apnc.js";
333
+ import"./index-4ks89h5a.js";
332
334
  import {
333
335
  isDreamerRunnable,
334
336
  isSmartNoteSurfaceAvailable,
@@ -737,12 +739,22 @@ function decodeThroughHostSchema(encoded) {
737
739
  const assetSchema = mediaPart?.fields?.media;
738
740
  const encoding = assetSchema?.ast?.encoding;
739
741
  const decode = Array.isArray(encoding) ? encoding[0]?.transformation?.decode : undefined;
740
- const run = decode?.run;
741
- if (typeof run !== "function")
742
- return "host media schema has no decode transformation";
743
- const result = run.call(decode, { _tag: "Some", value: encoded }, {});
744
- const option = result?._tag === "Success" ? result.value : undefined;
745
- const asset = option?._tag === "Some" ? option.value : undefined;
742
+ const getter = decode;
743
+ let asset;
744
+ if (getter?._tag === "Transform") {
745
+ if (typeof getter.transform !== "function")
746
+ return "host media schema has no decode transformation";
747
+ asset = getter.transform(encoded);
748
+ } else {
749
+ if (getter?._tag !== undefined)
750
+ return `host media schema has unsupported decode transformation: ${String(getter._tag)}`;
751
+ const run = getter?.run;
752
+ if (typeof run !== "function")
753
+ return "host media schema has no decode transformation";
754
+ const result = run.call(decode, { _tag: "Some", value: encoded }, {});
755
+ const option = result?._tag === "Success" ? result.value : undefined;
756
+ asset = option?._tag === "Some" ? option.value : undefined;
757
+ }
746
758
  if (!isClassInstance(asset) || asset.source !== source)
747
759
  return "host media schema decode did not return an asset";
748
760
  return asset;
@@ -787,6 +799,7 @@ class HostFoldIdle {
787
799
  now;
788
800
  timers;
789
801
  windows = new Map;
802
+ attempted = new Map;
790
803
  constructor(fold, now = Date.now, timers = { setTimeout, clearTimeout }) {
791
804
  this.fold = fold;
792
805
  this.now = now;
@@ -808,6 +821,9 @@ class HostFoldIdle {
808
821
  }
809
822
  if (!Number.isFinite(ttl) || lastResponseTime <= 0)
810
823
  return;
824
+ const attempted = this.attempted.get(sessionID);
825
+ if (attempted?.cacheTtl === cacheTtl && attempted.lastResponseTime === lastResponseTime)
826
+ return;
811
827
  const window = { cacheTtl, lastResponseTime };
812
828
  this.windows.set(sessionID, window);
813
829
  const eligible = () => this.windows.get(sessionID) === window && computeHardCacheExpired(window.cacheTtl, window.lastResponseTime, this.now());
@@ -816,6 +832,7 @@ class HostFoldIdle {
816
832
  return;
817
833
  if (eligible()) {
818
834
  window.timer = undefined;
835
+ this.attempted.set(sessionID, { cacheTtl, lastResponseTime });
819
836
  const release = () => {
820
837
  if (this.windows.get(sessionID) === window)
821
838
  this.windows.delete(sessionID);
@@ -832,9 +849,37 @@ class HostFoldIdle {
832
849
  dispose() {
833
850
  for (const sessionID of this.windows.keys())
834
851
  this.active(sessionID);
852
+ this.attempted.clear();
835
853
  }
836
854
  }
837
855
 
856
+ // src/v2/fold/manual-baseline.ts
857
+ function stageManualBaseline(db, sessionID, render) {
858
+ const read = () => db.prepare("SELECT * FROM session_meta WHERE session_id = ?").get(sessionID);
859
+ const before = read();
860
+ const columns = Object.keys(before).filter((key) => key.startsWith("cached_m0_") || key.startsWith("cached_m1_") || key === "memory_block_count" || key === "memory_block_ids");
861
+ const update = db.prepare(`UPDATE session_meta SET ${columns.map((key) => `${key} = ?`).join(", ")} WHERE session_id = ?`);
862
+ const restore = (row) => update.run(...columns.map((key) => row[key] ?? null), sessionID);
863
+ let summary;
864
+ let staged;
865
+ try {
866
+ summary = render();
867
+ staged = read();
868
+ } finally {
869
+ restore(before);
870
+ }
871
+ const commit = db.prepare(`UPDATE session_meta SET ${columns.map((key) => `${key} = ?`).join(", ")} WHERE session_id = ? AND ${columns.map((key) => `${key} IS ?`).join(" AND ")}`);
872
+ return {
873
+ summary,
874
+ boundaryID: staged.cached_m0_last_baseline_end_message_id,
875
+ commit() {
876
+ const result = commit.run(...columns.map((key) => staged[key] ?? null), sessionID, ...columns.map((key) => before[key] ?? null));
877
+ if (result.changes !== 1)
878
+ throw new Error("Manual compaction baseline changed during admission");
879
+ }
880
+ };
881
+ }
882
+
838
883
  // src/v2/fold/memory-cache.ts
839
884
  import { randomUUID } from "node:crypto";
840
885
 
@@ -1621,6 +1666,30 @@ function adaptPayload(draft, admittedIDs = new Set, messageTimes = new Map, hidd
1621
1666
  };
1622
1667
  }
1623
1668
 
1669
+ // src/v2/fold/code-mode-catalog.ts
1670
+ var changed = "The Code Mode tool catalog has changed.";
1671
+ var replacement = `${changed} This catalog supersedes the previous Code Mode tool catalog.`;
1672
+ function codeModeCatalogKind(data) {
1673
+ const text = data.text ?? "";
1674
+ const metadata = data.metadata;
1675
+ const keyed = typeof metadata === "object" && metadata !== null && "instructionSources" in metadata;
1676
+ if (keyed) {
1677
+ const sources = metadata.instructionSources;
1678
+ if (!Array.isArray(sources) || !sources.includes("core/codemode"))
1679
+ return;
1680
+ if (sources.length !== 1)
1681
+ return text.startsWith(replacement) ? "combined-replacement" : undefined;
1682
+ }
1683
+ if (text.startsWith(replacement))
1684
+ return "replacement";
1685
+ if (text.startsWith(changed))
1686
+ return "incremental";
1687
+ if (keyed || text.startsWith(`# Code Mode
1688
+ `) || text.startsWith("No Code Mode tools are currently available."))
1689
+ return "initial";
1690
+ return;
1691
+ }
1692
+
1624
1693
  // src/v2/fold/native-codec.ts
1625
1694
  class NativeReplayUnavailable extends Error {
1626
1695
  }
@@ -1674,11 +1743,24 @@ var keys = (value, allowed) => Object.keys(value).every((key) => allowed.include
1674
1743
  var identity = (value) => typeof value === "string" && value.length > 0 && Buffer.byteLength(value) <= 1024;
1675
1744
  var digest = (value) => typeof value === "string" && /^[a-f0-9]{64}$/.test(value);
1676
1745
  function validReplayState(value) {
1677
- if (!object(value) || !keys(value, ["floor", "controls", "blocked", "upgrade", "withoutRecentContext"]))
1746
+ if (!object(value) || !keys(value, [
1747
+ "floor",
1748
+ "controls",
1749
+ "blocked",
1750
+ "upgrade",
1751
+ "withoutRecentContext",
1752
+ "retiredInstructionThrough",
1753
+ "retiredInstructionIDs"
1754
+ ]))
1678
1755
  return false;
1679
1756
  const floor = value.floor;
1680
1757
  const upgrade = value.upgrade;
1681
- return object(floor) && keys(floor, ["kind", "id", "seq", "after", "digest"]) && ["start", "summary", "checkpoint", "host-checkpoint"].includes(floor.kind) && identity(floor.id) && typeof floor.seq === "number" && typeof floor.after === "number" && Number.isSafeInteger(floor.seq) && Number.isSafeInteger(floor.after) && floor.after === (floor.kind === "start" || floor.kind === "host-checkpoint" ? floor.seq - 1 : floor.seq) && digest(floor.digest) && validControls(value.controls) && (value.blocked === undefined || typeof value.blocked === "boolean") && (value.withoutRecentContext === undefined || typeof value.withoutRecentContext === "boolean") && (upgrade === undefined || object(upgrade) && keys(upgrade, ["admissionID", "mode"]) && identity(upgrade.admissionID) && ["bounded", "host-checkpoint", "source-repair"].includes(upgrade.mode));
1758
+ return object(floor) && keys(floor, ["kind", "id", "seq", "after", "digest"]) && ["start", "summary", "checkpoint", "host-checkpoint"].includes(floor.kind) && identity(floor.id) && typeof floor.seq === "number" && typeof floor.after === "number" && Number.isSafeInteger(floor.seq) && Number.isSafeInteger(floor.after) && floor.after === (floor.kind === "start" || floor.kind === "host-checkpoint" ? floor.seq - 1 : floor.seq) && digest(floor.digest) && validControls(value.controls) && (value.blocked === undefined || typeof value.blocked === "boolean") && (value.withoutRecentContext === undefined || typeof value.withoutRecentContext === "boolean") && (value.retiredInstructionThrough === undefined || Number.isSafeInteger(value.retiredInstructionThrough) && value.retiredInstructionThrough >= 0) && (value.retiredInstructionIDs === undefined || Array.isArray(value.retiredInstructionIDs) && value.retiredInstructionIDs.every(identity)) && (upgrade === undefined || object(upgrade) && keys(upgrade, ["admissionID", "mode"]) && identity(upgrade.admissionID) && ["bounded", "host-checkpoint", "source-repair"].includes(upgrade.mode));
1759
+ }
1760
+ function instructionRetirement(replay) {
1761
+ const ids = new Set(replay?.retiredInstructionIDs);
1762
+ const through = replay?.retiredInstructionThrough ?? -1;
1763
+ return (row) => row.type === "system" && (row.seq <= through || ids.has(row.id));
1682
1764
  }
1683
1765
  function validAdmissionV3(value) {
1684
1766
  if (!object(value) || !keys(value, ["version", "proof", "replay"]) || value.version !== 3 || !object(value.proof))
@@ -1837,11 +1919,17 @@ function controlsThrough(controls, reader, sessionID, through) {
1837
1919
 
1838
1920
  // src/v2/fold/native-replay.ts
1839
1921
  var coordinates = new WeakMap;
1922
+ var sequences = new WeakMap;
1923
+ function nativeRowSequence(message) {
1924
+ return sequences.get(message);
1925
+ }
1840
1926
  function nativeRowID(message) {
1841
1927
  return coordinates.get(message) ?? message.id;
1842
1928
  }
1843
- function bindNativeRowID(message, id) {
1929
+ function bindNativeRowID(message, id, seq) {
1844
1930
  coordinates.set(message, id);
1931
+ if (seq !== undefined)
1932
+ sequences.set(message, seq);
1845
1933
  }
1846
1934
  function copyNativeInput(messages) {
1847
1935
  const copies = reviveNative(encodeNative(messages));
@@ -1850,6 +1938,9 @@ function copyNativeInput(messages) {
1850
1938
  const id = original && nativeRowID(original);
1851
1939
  if (id !== undefined)
1852
1940
  coordinates.set(copy, id);
1941
+ const seq = original && sequences.get(original);
1942
+ if (seq !== undefined)
1943
+ sequences.set(copy, seq);
1853
1944
  });
1854
1945
  return copies;
1855
1946
  }
@@ -1961,6 +2052,48 @@ class NativeFoldReplay {
1961
2052
  async baseline(sessionID) {
1962
2053
  return this.tail(sessionID)?.baseline;
1963
2054
  }
2055
+ async retiredInstructionThrough(sessionID, reader) {
2056
+ if (!reader.latestCompaction(sessionID))
2057
+ return -1;
2058
+ return (await this.floorState(sessionID, reader))?.retiredInstructionThrough ?? -1;
2059
+ }
2060
+ async instructionRetirement(sessionID, reader) {
2061
+ if (!reader.latestCompaction(sessionID))
2062
+ return instructionRetirement();
2063
+ return instructionRetirement(await this.floorState(sessionID, reader));
2064
+ }
2065
+ async prepareManualFold(sessionID, reader, boundaryID, summary) {
2066
+ const running = reader.latestRunningCompaction(sessionID);
2067
+ if (!running || this.options.protocol !== 3)
2068
+ throw new NativeReplayUnavailable("Manual compaction has no running host checkpoint");
2069
+ const previous = await this.floorState(sessionID, reader);
2070
+ if (this.unprovenControls.has(sessionID) || previous?.blocked)
2071
+ throw new NativeReplayUnavailable("Manual compaction controls have not been captured");
2072
+ const seq = boundaryID ? reader.sequenceForId(sessionID, boundaryID) : undefined;
2073
+ const row = seq === undefined ? undefined : reader.range(sessionID, seq - 1, seq)[0];
2074
+ const first = reader.page(sessionID, { limit: 1 }).rows[0];
2075
+ let floor = row ? floorFor(row, "summary") : undefined;
2076
+ floor ??= first ? floorFor(first, "start") : undefined;
2077
+ if (!floor)
2078
+ throw new NativeReplayUnavailable("Manual compaction has no history boundary");
2079
+ const instructions = reader.instructionRowsThrough(sessionID, running.seq - 1);
2080
+ const retiredInstructionIDs = [];
2081
+ let newerFullCatalog = false;
2082
+ for (const instruction of [...instructions].reverse()) {
2083
+ const text = instruction.data.text ?? "";
2084
+ const catalog = codeModeCatalogKind(instruction.data);
2085
+ if (instruction.seq > floor.after && (catalog && catalog !== "combined-replacement" && newerFullCatalog || text.length > 0 && summary.includes(text)))
2086
+ retiredInstructionIDs.push(instruction.id);
2087
+ if (catalog === "replacement" || catalog === "combined-replacement")
2088
+ newerFullCatalog = true;
2089
+ }
2090
+ return {
2091
+ floor,
2092
+ controls: previous?.controls ?? [],
2093
+ ...floor.after >= 0 ? { retiredInstructionThrough: floor.after } : {},
2094
+ retiredInstructionIDs
2095
+ };
2096
+ }
1964
2097
  admissionKey(sessionID) {
1965
2098
  return this.options.protocol === 3 ? `native-fold/v3/${encodeURIComponent(sessionID)}` : admissionKey(sessionID);
1966
2099
  }
@@ -2180,7 +2313,7 @@ ${cached}
2180
2313
  return pending.rows.filter((row) => row.seq > pending.value.replay.floor.after).flatMap((row) => {
2181
2314
  const messages = this.rendered(structuredClone(row), model);
2182
2315
  for (const message of messages)
2183
- coordinates.set(message, row.id);
2316
+ bindNativeRowID(message, row.id, row.seq);
2184
2317
  return messages;
2185
2318
  });
2186
2319
  }
@@ -2407,6 +2540,7 @@ ${cached}
2407
2540
  if (!isLocalCheckpoint(cut) || cut.session_id !== sessionID)
2408
2541
  return;
2409
2542
  let after = -1;
2543
+ const retired = instructionRetirement(replayOverride ?? this.tail(sessionID)?.replay);
2410
2544
  if (this.options.protocol === 3) {
2411
2545
  const replay = replayOverride ?? this.tail(sessionID)?.replay;
2412
2546
  if (!replay || !validFloor(reader, sessionID, replay.floor))
@@ -2421,7 +2555,7 @@ ${cached}
2421
2555
  }
2422
2556
  const through = cut.seq - 1;
2423
2557
  const metadata = (this.metadataThrough.get(sessionID) ?? -1) >= through ? this.metadata.get(sessionID) : undefined;
2424
- const required = (metadata ?? reader.rowStampsThrough(sessionID, through, this.options.protocol === 3 ? after : undefined)).filter(conversational).filter((row) => row.seq > after && row.seq <= through);
2558
+ const required = (metadata ?? reader.rowStampsThrough(sessionID, through, this.options.protocol === 3 ? after : undefined)).filter(conversational).filter((row) => !retired(row)).filter((row) => row.seq > after && row.seq <= through);
2425
2559
  const tail = this.tail(sessionID);
2426
2560
  let records;
2427
2561
  if (!this.dirty.has(sessionID) && tail?.model === `${model.providerID}/${model.id}` && this.storage.containsRows(sessionID, required.map((row) => row.id))) {
@@ -2432,10 +2566,10 @@ ${cached}
2432
2566
  if (required.some((row) => !restored.has(row.id)))
2433
2567
  return;
2434
2568
  }
2435
- const projected = records.flatMap((row) => {
2569
+ const projected = records.filter((row) => !retired(row)).flatMap((row) => {
2436
2570
  const messages = reviveNative(JSON.parse(row.data));
2437
2571
  for (const message of messages)
2438
- coordinates.set(message, row.id);
2572
+ bindNativeRowID(message, row.id, row.seq);
2439
2573
  return messages;
2440
2574
  });
2441
2575
  return this.options.protocol === 3 ? insertControls(projected, controlsThrough((replayOverride ?? this.tail(sessionID)?.replay)?.controls ?? [], reader, sessionID, through), nativeRowID) : projected;
@@ -2517,7 +2651,7 @@ ${cached}
2517
2651
  if (!row)
2518
2652
  throw new NativeReplayUnavailable("Native carrier has no source row coordinate");
2519
2653
  cursor = row.seq;
2520
- coordinates.set(message, row.id);
2654
+ bindNativeRowID(message, row.id, row.seq);
2521
2655
  if (message.role !== "tool")
2522
2656
  owner = row.id;
2523
2657
  const messages = grouped.get(row) ?? [];
@@ -2542,13 +2676,13 @@ ${cached}
2542
2676
  replay = { ...replay, blocked: false };
2543
2677
  const replayAfter = replay.floor.after;
2544
2678
  const replayStart = replay.floor.kind === "start";
2679
+ const baseline = this.servedBaseline(draft);
2545
2680
  captured = native.filter((message) => {
2546
2681
  if (effortPart(message))
2547
2682
  return false;
2548
2683
  const id = nativeRowID(message);
2549
2684
  if (HEAD_IDS.some((head) => head === id))
2550
2685
  return false;
2551
- const baseline = this.servedBaseline(draft);
2552
2686
  if (!id)
2553
2687
  return !(baseline && message.role === "user" && message.content.some((part) => part.type === "text" && part.text === baseline));
2554
2688
  if (replayStart)
@@ -2662,21 +2796,23 @@ ${cached}
2662
2796
  if (await this.admissions?.get(admissionKey(draft.sessionID)) && !await this.readV3(draft.sessionID))
2663
2797
  return false;
2664
2798
  const running = reader.latestRunningCompaction(draft.sessionID);
2665
- const replay = await this.floorState(draft.sessionID, reader, undefined, undefined, true);
2799
+ const replay = args.manual?.replay ?? await this.floorState(draft.sessionID, reader, undefined, undefined, true);
2666
2800
  if (!running || !replay || replay.blocked) {
2667
2801
  this.report(draft.sessionID, "fold refused: no usable host checkpoint boundary");
2668
2802
  return false;
2669
2803
  }
2670
2804
  const model = `${draft.model.providerID}/${draft.model.id}`;
2671
- if (this.tail(draft.sessionID)?.model !== model)
2805
+ if (!args.manual && this.tail(draft.sessionID)?.model !== model)
2672
2806
  this.storage.replaceRows(draft.sessionID, reader.latestSequence(draft.sessionID), []);
2673
- await this.retainReplay(draft.sessionID, replay, model, reader.latestSequence(draft.sessionID));
2674
- await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
2675
- source: this.storage.sourceID,
2676
- sessionID: draft.sessionID,
2677
- admissionID: running.id,
2678
- summary
2679
- }));
2807
+ if (!args.manual)
2808
+ await this.retainReplay(draft.sessionID, replay, model, reader.latestSequence(draft.sessionID));
2809
+ if (!args.manual)
2810
+ await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
2811
+ source: this.storage.sourceID,
2812
+ sessionID: draft.sessionID,
2813
+ admissionID: running.id,
2814
+ summary
2815
+ }));
2680
2816
  const value = {
2681
2817
  version: 3,
2682
2818
  replay,
@@ -2689,6 +2825,57 @@ ${cached}
2689
2825
  }
2690
2826
  };
2691
2827
  const payload = encodeAdmissionV3(value);
2828
+ if (args.manual) {
2829
+ const keys = [
2830
+ capturedReplayKey(draft.sessionID),
2831
+ this.admissionKey(draft.sessionID)
2832
+ ];
2833
+ const old = await Promise.all(keys.map((key) => this.admissions?.get(key)));
2834
+ const capture = {
2835
+ version: 3,
2836
+ sessionID: draft.sessionID,
2837
+ sourceDigest: foldDigest(reader.hostIdentity()),
2838
+ through: reader.latestSequence(draft.sessionID),
2839
+ replay
2840
+ };
2841
+ const capturedPayload = JSON.stringify(capture);
2842
+ let written = 0;
2843
+ try {
2844
+ for (const [index, key] of keys.entries()) {
2845
+ const data = index === 0 ? capturedPayload : payload;
2846
+ await this.admissions?.set(key, {
2847
+ payload: data,
2848
+ digest: foldDigest(data)
2849
+ });
2850
+ written++;
2851
+ }
2852
+ args.manual.commit();
2853
+ } catch (error) {
2854
+ for (let index = written - 1;index >= 0; index--) {
2855
+ const key = keys[index];
2856
+ if (!key)
2857
+ continue;
2858
+ if (old[index] !== undefined)
2859
+ await this.admissions?.set(key, old[index]);
2860
+ else if (this.admissions?.remove)
2861
+ await this.admissions.remove(key);
2862
+ else
2863
+ await this.admissions?.set(key, null);
2864
+ }
2865
+ throw error;
2866
+ }
2867
+ this.invalidate(draft.sessionID);
2868
+ this.cancelLegacyUpgrade(draft.sessionID);
2869
+ this.cancelEditedAdmission(draft.sessionID);
2870
+ await this.retainReplay(draft.sessionID, replay, model, running.seq - 1);
2871
+ await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
2872
+ source: this.storage.sourceID,
2873
+ sessionID: draft.sessionID,
2874
+ admissionID: running.id,
2875
+ summary
2876
+ }));
2877
+ return true;
2878
+ }
2692
2879
  await this.admissions?.set(this.admissionKey(draft.sessionID), {
2693
2880
  payload,
2694
2881
  digest: foldDigest(payload)
@@ -2757,7 +2944,13 @@ ${cached}
2757
2944
  this.report(sessionID, "replay refused: missing usable checkpoint boundary");
2758
2945
  throw new NativeReplayUnavailable("Host checkpoint replay boundary is unavailable");
2759
2946
  }
2760
- options = { ...options, bounded: false, after: replay.floor.after };
2947
+ options = {
2948
+ ...options,
2949
+ bounded: false,
2950
+ after: replay.floor.after,
2951
+ retiredInstructionThrough: replay.retiredInstructionThrough,
2952
+ retiredInstructionIDs: replay.retiredInstructionIDs
2953
+ };
2761
2954
  }
2762
2955
  const release = this.storage.hold(sessionID);
2763
2956
  let result;
@@ -2823,10 +3016,13 @@ ${cached}
2823
3016
  reader?.close();
2824
3017
  }
2825
3018
  const messages = [];
3019
+ const retired = instructionRetirement(options);
2826
3020
  for (const row of this.storage.rows(sessionID, after, cut.seq - 1)) {
3021
+ if (retired(row))
3022
+ continue;
2827
3023
  const revived = reviveNative(JSON.parse(row.data));
2828
3024
  for (const message of revived)
2829
- coordinates.set(message, row.id);
3025
+ bindNativeRowID(message, row.id, row.seq);
2830
3026
  messages.push(...revived);
2831
3027
  }
2832
3028
  return messages;
@@ -2839,10 +3035,11 @@ ${cached}
2839
3035
  this.report(sessionID, "replay refused: missing usable checkpoint boundary");
2840
3036
  throw new NativeReplayUnavailable("Host checkpoint replay boundary is unavailable");
2841
3037
  }
2842
- const messages = reader.range(sessionID, replay?.floor.after ?? -1, cut.seq - 1).filter(conversational).flatMap((row) => {
3038
+ const retired = instructionRetirement(replay);
3039
+ const messages = reader.range(sessionID, replay?.floor.after ?? -1, cut.seq - 1).filter(conversational).filter((row) => !retired(row)).flatMap((row) => {
2843
3040
  const messages = this.rendered(row, model);
2844
3041
  for (const message of messages)
2845
- coordinates.set(message, row.id);
3042
+ bindNativeRowID(message, row.id, row.seq);
2846
3043
  return messages;
2847
3044
  });
2848
3045
  return replay ? insertControls(messages, controlsThrough(replay.controls, reader, sessionID, cut.seq - 1), nativeRowID) : messages;
@@ -2977,6 +3174,12 @@ class HostFoldPolicy {
2977
3174
  }
2978
3175
  }
2979
3176
 
3177
+ // src/v2/fold/summary.ts
3178
+ var RAW_HISTORY_ACCOUNT = "Magic Context retains the conversation as raw messages; no summarized history is available yet.";
3179
+ function hasFoldSummary(summary) {
3180
+ return typeof summary === "string" && summary.replace(/<[^>]*>/g, "").trim().length > 0;
3181
+ }
3182
+
2980
3183
  // src/v2/fork-inheritance.ts
2981
3184
  var RAW_TYPES = new Set(RAW_MESSAGE_TYPES2);
2982
3185
  function forkBoundarySeq(origin, sequenceForId) {
@@ -3866,6 +4069,26 @@ async function createV2HiddenCompletionExecutor(host, options) {
3866
4069
  const budgetStopped = new Promise((_resolve, reject) => {
3867
4070
  budgetReject = reject;
3868
4071
  });
4072
+ const chargeBudgetRows = (rows) => {
4073
+ if (!budget)
4074
+ return;
4075
+ for (const row of rows) {
4076
+ if (row.seq <= usageSeq)
4077
+ continue;
4078
+ if (!row.data.finish)
4079
+ break;
4080
+ usageSeq = row.seq;
4081
+ const tokens = row.data.tokens;
4082
+ const decision = budget.charge(Math.max(0, tokens?.input ?? 0), Math.max(0, tokens?.cache?.read ?? 0), Math.max(0, tokens?.cache?.write ?? 0), row.data.finish === "stop" && !(row.data.content ?? []).some((part) => part.type === "tool-call"), false);
4083
+ const onBudgetUpdate = run.identity.metadata?.onBudgetUpdate;
4084
+ if (typeof onBudgetUpdate === "function")
4085
+ onBudgetUpdate({ ...budget.snapshot(), sessionId: run.child.id });
4086
+ if (decision !== "continue") {
4087
+ attempt.budgetExceeded = new DreamTokenBudgetExceeded(run.child.id, budget.snapshot().spent);
4088
+ return attempt.budgetExceeded;
4089
+ }
4090
+ }
4091
+ };
3869
4092
  if (budget) {
3870
4093
  budgetPoll = inRootAsyncContext2(() => setInterval(() => {
3871
4094
  try {
@@ -3873,25 +4096,12 @@ async function createV2HiddenCompletionExecutor(host, options) {
3873
4096
  const latest = reader.latestAssistant(run.child.id);
3874
4097
  return latest ? [latest] : [];
3875
4098
  })());
3876
- for (const row of fresh) {
3877
- if (row.seq <= usageSeq)
3878
- continue;
3879
- usageSeq = row.seq;
3880
- const tokens = row.data.tokens;
3881
- const decision = budget.charge(Math.max(0, tokens?.input ?? 0), Math.max(0, tokens?.cache?.read ?? 0), Math.max(0, tokens?.cache?.write ?? 0), row.data.finish === "stop" && !(row.data.content ?? []).some((part) => part.type === "tool-call"), false);
3882
- const onBudgetUpdate = run.identity.metadata?.onBudgetUpdate;
3883
- if (typeof onBudgetUpdate === "function")
3884
- onBudgetUpdate({
3885
- ...budget.snapshot(),
3886
- sessionId: run.child.id
3887
- });
3888
- if (decision !== "continue") {
3889
- if (budgetPoll)
3890
- clearInterval(budgetPoll);
3891
- attempt.budgetExceeded = new DreamTokenBudgetExceeded(run.child.id, budget.snapshot().spent);
3892
- interruptAndRetire(run, "token-budget").finally(() => budgetReject(attempt.budgetExceeded));
3893
- return;
3894
- }
4099
+ const exceeded = chargeBudgetRows(fresh);
4100
+ if (exceeded) {
4101
+ if (budgetPoll)
4102
+ clearInterval(budgetPoll);
4103
+ interruptAndRetire(run, "token-budget").finally(() => budgetReject(exceeded));
4104
+ return;
3895
4105
  }
3896
4106
  } catch (error) {
3897
4107
  if (budgetPoll)
@@ -3988,6 +4198,11 @@ async function createV2HiddenCompletionExecutor(host, options) {
3988
4198
  const tokens = row.data.tokens;
3989
4199
  const tokenNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
3990
4200
  const steps = withReader(options.openReader, (reader) => reader.assistantSince?.(run.child.id, baseline) ?? [row]).filter((step) => step.seq > baseline && step.seq <= row.seq);
4201
+ const exceeded = chargeBudgetRows(steps);
4202
+ if (exceeded) {
4203
+ await interruptAndRetire(run, "token-budget");
4204
+ throw exceeded;
4205
+ }
3991
4206
  const reportedUsage = steps.some((step) => tokenNumber(step.data.tokens?.input) !== undefined || tokenNumber(step.data.tokens?.output) !== undefined);
3992
4207
  const usage = sumRowUsage(steps);
3993
4208
  if (request.signal?.aborted || run.retired || Date.now() >= deadline) {
@@ -4207,6 +4422,100 @@ class V2MessageTimesCache {
4207
4422
 
4208
4423
  // src/v2/hooks/channel2.ts
4209
4424
  import { randomBytes } from "node:crypto";
4425
+
4426
+ // src/v2/hooks/synthetic-admission.ts
4427
+ var memos = new WeakMap;
4428
+ var MAX_SESSIONS = 64;
4429
+ var MAX_IDS = 50000;
4430
+ function admissionSession(key) {
4431
+ if (!key.startsWith("synthetic/"))
4432
+ return;
4433
+ const end = key.indexOf("/", "synthetic/".length);
4434
+ return end < 0 ? undefined : key.slice("synthetic/".length, end);
4435
+ }
4436
+ function memoFor(storage) {
4437
+ const prior = memos.get(storage);
4438
+ if (prior)
4439
+ return prior;
4440
+ const sessions = new Map;
4441
+ const set = storage.set;
4442
+ const remove = storage.remove;
4443
+ const invalidate = (key) => {
4444
+ const sessionID = admissionSession(key);
4445
+ if (sessionID !== undefined) {
4446
+ sessions.delete(sessionID);
4447
+ const current = memos.get(storage);
4448
+ if (current) {
4449
+ current.sessions.delete(sessionID);
4450
+ current.revision++;
4451
+ }
4452
+ }
4453
+ };
4454
+ const wrappedSet = async (key, value) => {
4455
+ invalidate(key);
4456
+ try {
4457
+ await set.call(storage, key, value);
4458
+ } finally {
4459
+ invalidate(key);
4460
+ }
4461
+ };
4462
+ const wrappedRemove = remove && (async (key) => {
4463
+ invalidate(key);
4464
+ try {
4465
+ await remove.call(storage, key);
4466
+ } finally {
4467
+ invalidate(key);
4468
+ }
4469
+ });
4470
+ const memo = { sessions, revision: 0, set, remove, wrappedSet, wrappedRemove };
4471
+ storage.set = wrappedSet;
4472
+ if (wrappedRemove)
4473
+ storage.remove = wrappedRemove;
4474
+ memos.set(storage, memo);
4475
+ return memo;
4476
+ }
4477
+ function forgetSyntheticAdmission(storage, sessionID) {
4478
+ const memo = memoFor(storage);
4479
+ memo.sessions.delete(sessionID);
4480
+ memo.revision++;
4481
+ }
4482
+ function clearSyntheticAdmissions(storage) {
4483
+ const memo = memos.get(storage);
4484
+ if (!memo)
4485
+ return;
4486
+ memo.sessions.clear();
4487
+ if (storage.set === memo.wrappedSet)
4488
+ storage.set = memo.set;
4489
+ if (storage.remove === memo.wrappedRemove)
4490
+ storage.remove = memo.remove;
4491
+ memos.delete(storage);
4492
+ }
4493
+ async function admittedSynthetic(storage, sessionID, id) {
4494
+ for (;; ) {
4495
+ const memo = memoFor(storage);
4496
+ const session = memo.sessions.get(sessionID) ?? new Map;
4497
+ memo.sessions.delete(sessionID);
4498
+ memo.sessions.set(sessionID, session);
4499
+ if (memo.sessions.size > MAX_SESSIONS)
4500
+ memo.sessions.delete(memo.sessions.keys().next().value);
4501
+ const known = session.get(id);
4502
+ if (known !== undefined)
4503
+ return known;
4504
+ const revision = memo.revision;
4505
+ const record = await storage.get(`synthetic/${sessionID}/${id}`);
4506
+ if (memos.get(storage) !== memo || memo.revision !== revision)
4507
+ continue;
4508
+ const admitted = record !== null && typeof record === "object" && "id" in record && record.id === id;
4509
+ if (memo.sessions.get(sessionID) !== session)
4510
+ return admitted;
4511
+ if (session.size >= MAX_IDS)
4512
+ session.delete(session.keys().next().value);
4513
+ session.set(id, admitted);
4514
+ return admitted;
4515
+ }
4516
+ }
4517
+
4518
+ // src/v2/hooks/channel2.ts
4210
4519
  var pendingCandidates = new WeakMap;
4211
4520
  function syntheticCandidates(context, sessionID, stored) {
4212
4521
  const pending = pendingCandidates.get(context.storage);
@@ -4222,9 +4531,17 @@ function syntheticCandidates(context, sessionID, stored) {
4222
4531
  }
4223
4532
  function clearSyntheticCandidates(context) {
4224
4533
  pendingCandidates.delete(context.storage);
4534
+ clearSyntheticAdmissions(context.storage);
4535
+ }
4536
+ function observeSyntheticAdmissionEvent(context, event) {
4537
+ const sessionID = event.data?.sessionID;
4538
+ if (!sessionID || !(event.type === "session.deleted" || event.type?.startsWith("session.revert.")))
4539
+ return;
4540
+ forgetSyntheticAdmission(context.storage, sessionID);
4225
4541
  }
4226
4542
  async function deliverSynthetic(context, sessionID, text) {
4227
4543
  const id = `msg_${randomBytes(12).toString("hex")}`;
4544
+ forgetSyntheticAdmission(context.storage, sessionID);
4228
4545
  await context.storage.set(`synthetic/${sessionID}/${id}`, { id });
4229
4546
  let candidates = pendingCandidates.get(context.storage);
4230
4547
  if (!candidates) {
@@ -4236,8 +4553,7 @@ async function deliverSynthetic(context, sessionID, text) {
4236
4553
  return id;
4237
4554
  }
4238
4555
  async function isAdmittedSynthetic(context, sessionID, id) {
4239
- const record = await context.storage.get(`synthetic/${sessionID}/${id}`);
4240
- return record !== null && typeof record === "object" && "id" in record && record.id === id;
4556
+ return admittedSynthetic(context.storage, sessionID, id);
4241
4557
  }
4242
4558
  async function deliverPendingChannel2(context, db, sessionID, baseline) {
4243
4559
  if (getChannel2NudgeState(db, sessionID) !== "pending")
@@ -5066,16 +5382,31 @@ async function interruptBeforeProvider(session, sessionID, timers = systemTimers
5066
5382
  }
5067
5383
 
5068
5384
  // src/v2/hooks/restore-rows.ts
5385
+ var DEFAULT_LIMITS = {
5386
+ maxSessions: 16,
5387
+ sessionRows: 40000,
5388
+ totalRows: 80000,
5389
+ sessionBytes: 192 * 1024 * 1024,
5390
+ totalBytes: 256 * 1024 * 1024
5391
+ };
5392
+
5069
5393
  class RestoredRowCache {
5070
- capacity;
5071
5394
  sessions = new Map;
5072
- constructor(capacity = 16) {
5073
- this.capacity = capacity;
5395
+ limits;
5396
+ totalRows = 0;
5397
+ totalBytes = 0;
5398
+ constructor(limits = {}) {
5399
+ this.limits = {
5400
+ ...DEFAULT_LIMITS,
5401
+ ...typeof limits === "number" ? { maxSessions: limits } : limits
5402
+ };
5074
5403
  }
5075
5404
  rows(reader, sessionID, after, through) {
5076
- if (through <= after)
5405
+ if (through <= after) {
5406
+ this.forget(sessionID);
5077
5407
  return [];
5078
- const kept = this.sessions.get(sessionID) ?? new Map;
5408
+ }
5409
+ const kept = this.sessions.get(sessionID)?.rows ?? new Map;
5079
5410
  const stamps = reader.spanRowStamps(sessionID, after, through);
5080
5411
  const runs = [];
5081
5412
  let previous = after;
@@ -5097,28 +5428,44 @@ class RestoredRowCache {
5097
5428
  fresh.set(row.seq, row);
5098
5429
  const next = new Map;
5099
5430
  const rows = [];
5431
+ let bytes = 0;
5100
5432
  for (const [seq, stamp] of stamps) {
5101
5433
  const row = fresh.get(seq) ?? kept.get(seq)?.row;
5102
5434
  if (!row)
5103
5435
  continue;
5104
- next.set(seq, { stamp, row });
5105
5436
  rows.push(row);
5437
+ const rowBytes = Number(stamp.slice(stamp.lastIndexOf("\x00") + 1));
5438
+ if (!Number.isSafeInteger(rowBytes) || rowBytes < 0 || next.size >= Math.min(this.limits.sessionRows, this.limits.totalRows) || bytes + rowBytes > Math.min(this.limits.sessionBytes, this.limits.totalBytes))
5439
+ continue;
5440
+ next.set(seq, { stamp, row });
5441
+ bytes += rowBytes;
5106
5442
  }
5107
- this.sessions.delete(sessionID);
5108
- this.sessions.set(sessionID, next);
5109
- while (this.sessions.size > this.capacity) {
5443
+ this.forget(sessionID);
5444
+ if (next.size) {
5445
+ this.sessions.set(sessionID, { rows: next, bytes });
5446
+ this.totalRows += next.size;
5447
+ this.totalBytes += bytes;
5448
+ }
5449
+ while (this.sessions.size > this.limits.maxSessions || this.totalRows > this.limits.totalRows || this.totalBytes > this.limits.totalBytes) {
5110
5450
  const oldest = this.sessions.keys().next().value;
5111
5451
  if (oldest === undefined)
5112
5452
  break;
5113
- this.sessions.delete(oldest);
5453
+ this.forget(oldest);
5114
5454
  }
5115
5455
  return structuredClone(rows);
5116
5456
  }
5117
5457
  forget(sessionID) {
5458
+ const prior = this.sessions.get(sessionID);
5459
+ if (prior) {
5460
+ this.totalRows -= prior.rows.size;
5461
+ this.totalBytes -= prior.bytes;
5462
+ }
5118
5463
  this.sessions.delete(sessionID);
5119
5464
  }
5120
5465
  clear() {
5121
5466
  this.sessions.clear();
5467
+ this.totalRows = 0;
5468
+ this.totalBytes = 0;
5122
5469
  }
5123
5470
  }
5124
5471
  function readPages(reader, sessionID, after, through) {
@@ -5518,7 +5865,7 @@ function persistV2UsageReading(args) {
5518
5865
  args.contextUsageMap.set(sessionID, {
5519
5866
  usage: { inputTokens: reading.inputTokens, percentage },
5520
5867
  hasUsageTokens: true,
5521
- updatedAt: Date.now()
5868
+ updatedAt: reading.completed || Date.now()
5522
5869
  });
5523
5870
  } else {
5524
5871
  args.contextUsageMap.delete(sessionID);
@@ -5566,7 +5913,7 @@ function bindKeptInstructionCoordinates(messages, incoming, reader, sessionID) {
5566
5913
  selected.forEach((row, index) => {
5567
5914
  const message = pending[index];
5568
5915
  if (message)
5569
- bindNativeRowID(message, row.id);
5916
+ bindNativeRowID(message, row.id, row.seq);
5570
5917
  });
5571
5918
  }
5572
5919
  pending = [];
@@ -5860,10 +6207,10 @@ async function applyV2SystemPrompt(systemPrompt, draft) {
5860
6207
  text
5861
6208
  })));
5862
6209
  }
5863
- function coveredInstructionRows(reader, sessionID, throughSeq, model) {
6210
+ function coveredInstructionRows(reader, sessionID, throughSeq, model, retired = () => false) {
5864
6211
  if (throughSeq < 0)
5865
6212
  return [];
5866
- return reader.instructionRowsThrough(sessionID, throughSeq).flatMap((row) => restoreRow(row, model)).map((message) => markCoveredInstructionRow(message));
6213
+ return reader.instructionRowsThrough(sessionID, throughSeq).filter((row) => !retired(row)).flatMap((row) => restoreRow(row, model)).map((message) => markCoveredInstructionRow(message));
5867
6214
  }
5868
6215
  async function registerContext(context) {
5869
6216
  const directory = context.location.directory;
@@ -6342,7 +6689,15 @@ async function registerContext(context) {
6342
6689
  storage: nativeFoldMemory,
6343
6690
  openReader: openStoreReader,
6344
6691
  canReplay: (sessionID, reader) => nativeFoldReplay.canReplay(sessionID, reader),
6345
- canFold: (sessionID, reader) => nativeFoldReplay.canFold(sessionID, reader),
6692
+ canFold: async (sessionID, reader) => {
6693
+ const meta = getOrCreateSessionMeta(storage.require(), sessionID);
6694
+ const summary = [
6695
+ servedModuleM0Text(sessionID),
6696
+ await nativeFoldReplay.baseline(sessionID),
6697
+ meta.cachedM0Bytes?.toString("utf8")
6698
+ ].find((value) => value !== RAW_HISTORY_ACCOUNT && hasFoldSummary(value));
6699
+ return !meta.isSubagent && summary !== RAW_HISTORY_ACCOUNT && hasFoldSummary(summary) && nativeFoldReplay.canFold(sessionID, reader);
6700
+ },
6346
6701
  threshold: Number(process.env.MC_OC2_HOST_FOLD_ROWS ?? 1000),
6347
6702
  log: sessionLog2
6348
6703
  }) : undefined;
@@ -6368,6 +6723,7 @@ async function registerContext(context) {
6368
6723
  if (!event.data?.sessionID)
6369
6724
  continue;
6370
6725
  const sessionID = event.data.sessionID;
6726
+ observeSyntheticAdmissionEvent(context, event);
6371
6727
  overflowRecovery.observeTerminal(event, (id, messageID) => {
6372
6728
  const reader = openStoreReader();
6373
6729
  try {
@@ -6549,22 +6905,46 @@ async function registerContext(context) {
6549
6905
  await storage.probe();
6550
6906
  const watermark = reader.latestSequenceForIds(draft.sessionID, draft.messages.flatMap((message) => message.id ? [message.id] : []));
6551
6907
  const running = reader.latestRunningCompaction(draft.sessionID);
6908
+ const attempt = running && nativeFoldMemory?.admission(draft.sessionID, running.id);
6909
+ const manual = running?.data.reason === "manual" && !(attempt?.status === "pending" && attempt.owner_token);
6552
6910
  const moduleBaseline = rustModeModuleClient ? servedModuleM0Text(draft.sessionID) : undefined;
6553
6911
  const source = !rustModeModuleClient ? "typescript" : moduleBaseline === null ? "typescript_fallback" : "module";
6554
6912
  if (nativeFoldReplay) {
6555
6913
  try {
6556
6914
  const state = getOrCreateSessionMeta(storage.require(), draft.sessionID);
6557
- const summary = moduleBaseline ?? await nativeFoldReplay.baseline(draft.sessionID) ?? state.cachedM0Bytes?.toString("utf8") ?? "<session-history></session-history>";
6558
- if (await nativeFoldReplay.supply({
6915
+ const staged = manual && moduleBaseline == null ? stageManualBaseline(storage.require(), draft.sessionID, () => materialize(draft)) : undefined;
6916
+ const publishedHistory = Boolean(storage.require().prepare("SELECT 1 FROM compartments WHERE session_id = ? LIMIT 1").get(draft.sessionID));
6917
+ const candidate = [
6918
+ moduleBaseline,
6919
+ await nativeFoldReplay.baseline(draft.sessionID),
6920
+ state.cachedM0Bytes?.toString("utf8"),
6921
+ (await folds.read(draft.sessionID))?.submitted
6922
+ ].find((value) => value !== RAW_HISTORY_ACCOUNT && (publishedHistory && value?.includes("<session-history>") || hasFoldSummary(value)));
6923
+ const manualHistory = manual && hasFoldSummary(staged?.summary ?? moduleBaseline);
6924
+ const summary = manualHistory ? staged?.summary ?? candidate ?? RAW_HISTORY_ACCOUNT : candidate ?? RAW_HISTORY_ACCOUNT;
6925
+ const manualReplay = manualHistory && summary ? await nativeFoldReplay.prepareManualFold(draft.sessionID, reader, staged ? staged.boundaryID : storage.require().prepare("SELECT cached_m0_last_baseline_end_message_id AS id FROM session_meta WHERE session_id = ?").get(draft.sessionID).id, summary) : undefined;
6926
+ if ((hasFoldSummary(summary) || publishedHistory) && await nativeFoldReplay.supply({
6559
6927
  draft,
6560
6928
  reader,
6561
6929
  previousCut: reader.latestCompaction(draft.sessionID),
6562
- summary
6930
+ summary,
6931
+ manual: manualReplay ? { replay: manualReplay, commit: () => staged?.commit() } : undefined
6563
6932
  })) {
6933
+ if (manualHistory) {
6934
+ dropSlot(draft.sessionID, "manual host compaction");
6935
+ generateReplay.forget(draft.sessionID);
6936
+ restoredRows.forget(draft.sessionID);
6937
+ historyRefreshSessions.add(draft.sessionID);
6938
+ requestPendingMaterialization(pendingMaterializationSessions, draft.sessionID, "host_compaction");
6939
+ }
6564
6940
  draft.result = { summary, metadata: { keepID: undefined } };
6565
6941
  return;
6566
6942
  }
6943
+ if (manual)
6944
+ throw new V2ContextRefusal("Magic Context could not admit the manual fold.");
6567
6945
  } catch (error) {
6946
+ if (error instanceof V2ContextRefusal || manual)
6947
+ throw error;
6568
6948
  nativeFoldReplay.invalidate(draft.sessionID, true);
6569
6949
  sessionLog2(draft.sessionID, `v2 host fold: legacy compaction reason=${JSON.stringify(String(error))}`);
6570
6950
  }
@@ -6573,7 +6953,10 @@ async function registerContext(context) {
6573
6953
  sessionID: draft.sessionID,
6574
6954
  watermark,
6575
6955
  runningCut: running?.seq,
6576
- materialize: () => moduleBaseline ?? materialize(draft)
6956
+ materialize: () => {
6957
+ const summary = moduleBaseline ?? materialize(draft);
6958
+ return hasFoldSummary(summary) ? summary : RAW_HISTORY_ACCOUNT;
6959
+ }
6577
6960
  });
6578
6961
  sessionLog2(draft.sessionID, `v2 compaction hook: fired answered=true source=${source}`);
6579
6962
  draft.result = { summary: fold.submitted, metadata: { keepID: undefined } };
@@ -6588,6 +6971,19 @@ async function registerContext(context) {
6588
6971
  });
6589
6972
  const temporalTimes = config.temporal_awareness === true;
6590
6973
  const anchorTimesOnly = !temporalTimes && !rustModeModuleClient;
6974
+ const retireInstructionMessages = async (sessionID, reader, messages) => {
6975
+ const retired = await nativeFoldReplay?.instructionRetirement(sessionID, reader);
6976
+ if (!retired)
6977
+ return messages;
6978
+ return messages.filter((message) => {
6979
+ const id = nativeRowID(message);
6980
+ return message.role !== "system" || !id || !retired({
6981
+ type: "system",
6982
+ id,
6983
+ seq: nativeRowSequence(message) ?? reader.sequenceForId(sessionID, id) ?? Infinity
6984
+ });
6985
+ });
6986
+ };
6591
6987
  const timeIds = (messages) => {
6592
6988
  if (temporalTimes)
6593
6989
  return messages.flatMap((message) => message.id ? [message.id] : []);
@@ -6609,16 +7005,17 @@ async function registerContext(context) {
6609
7005
  const boundaryID = db.prepare("SELECT cached_m0_last_baseline_end_message_id AS id FROM session_meta WHERE session_id = ?").get(draft.sessionID)?.id ?? (boundaryRepair?.kind === "repaired" || boundaryRepair?.kind === "baseline-reset" ? boundaryRepair.anchorEndMessageId : null);
6610
7006
  const servedBoundary = boundaryID ? servedBoundaryRow2(reader, draft.sessionID, boundaryID) : null;
6611
7007
  const nativeFloor = await nativeFoldReplay?.restorationFloor(draft.sessionID, cut, reader);
7008
+ const retiredInstructions = await nativeFoldReplay?.instructionRetirement(draft.sessionID, reader) ?? (() => false);
6612
7009
  const boundary = Math.max(nativeFloor ?? -Infinity, (nativeFoldReplay?.sourceChanged(draft.sessionID) && !rustModeModuleClient ? (reader.earliestSequence(draft.sessionID) ?? 0) - 1 : undefined) ?? (moduleBoundarySeq !== undefined ? moduleBoundarySeq - 1 : undefined) ?? servedBoundary?.seq ?? reader.sequenceForId(draft.sessionID, boundaryID) ?? (reader.earliestSequence(draft.sessionID) ?? 0) - 1);
6613
7010
  if (moduleBoundarySeq !== undefined)
6614
7011
  sessionLog2(draft.sessionID, `v2 restore: from the module boundary ${moduleBoundaryID} to the host checkpoint`);
6615
7012
  const present = new Set(args.messages.map((message) => message.id));
6616
- const restored = restoredRows.rows(reader, draft.sessionID, boundary, cut.seq).filter((row) => !present.has(row.id)).flatMap((row) => restoreRow(row, draft.model, hostUsesMediaAssets() ? {
7013
+ const restored = restoredRows.rows(reader, draft.sessionID, boundary, cut.seq).filter((row) => !retiredInstructions(row)).filter((row) => !present.has(row.id)).flatMap((row) => restoreRow(row, draft.model, hostUsesMediaAssets() ? {
6617
7014
  asset: hostMediaAsset,
6618
7015
  unavailable: (detail) => sessionLog2(draft.sessionID, `v2 restore: attachment replaced by a note row=${detail.rowID} name=${JSON.stringify(detail.name ?? null)} mediaType=${detail.mediaType} reason=${JSON.stringify(detail.reason)}`)
6619
7016
  } : undefined));
6620
7017
  return [
6621
- ...rustModeModuleClient ? [] : coveredInstructionRows(reader, draft.sessionID, Math.min(boundary, cut.seq), draft.model),
7018
+ ...rustModeModuleClient ? [] : coveredInstructionRows(reader, draft.sessionID, Math.min(boundary, cut.seq), draft.model, retiredInstructions),
6622
7019
  ...restored,
6623
7020
  ...args.messages.filter((message) => message !== args.incoming)
6624
7021
  ];
@@ -6851,6 +7248,7 @@ async function registerContext(context) {
6851
7248
  }
6852
7249
  } else if (localCut)
6853
7250
  return;
7251
+ messages = await retireInstructionMessages(draft.sessionID, reader, messages);
6854
7252
  times = readTimes(reader, messages);
6855
7253
  }
6856
7254
  } finally {
@@ -7080,6 +7478,7 @@ async function registerContext(context) {
7080
7478
  boundaryRepair
7081
7479
  }));
7082
7480
  }
7481
+ draft.messages = await retireInstructionMessages(draft.sessionID, reader, draft.messages);
7083
7482
  messageTimes = readTimes(reader, draft.messages);
7084
7483
  } finally {
7085
7484
  reader.close();
@@ -7260,6 +7659,11 @@ async function registerContext(context) {
7260
7659
  if (!deletedSessions.has(draft.sessionID) && admissionRebuildPasses.has(draft.sessionID)) {
7261
7660
  await nativeFoldReplay?.commitLegacyUpgrade(draft.sessionID, true);
7262
7661
  await nativeFoldReplay?.commitEditedAdmission(draft.sessionID);
7662
+ if (db && hostFoldIdle) {
7663
+ const meta = getOrCreateSessionMeta(db, draft.sessionID);
7664
+ if (computeHardCacheExpired(meta.cacheTtl, meta.lastResponseTime, Date.now()))
7665
+ hostFoldIdle.idle(draft.sessionID, meta.cacheTtl, meta.lastResponseTime);
7666
+ }
7263
7667
  }
7264
7668
  } finally {
7265
7669
  admissionRebuildPasses.delete(draft.sessionID);