@cortexkit/opencode-magic-context 0.49.35 → 0.49.36

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 (200) 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-969z683s.js} +485 -91
  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-bxr039be.js} +1011 -209
  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/idle.d.ts +1 -0
  128. package/dist/v2/fold/idle.d.ts.map +1 -1
  129. package/dist/v2/fold/manual-baseline.d.ts +8 -0
  130. package/dist/v2/fold/manual-baseline.d.ts.map +1 -0
  131. package/dist/v2/fold/native-replay-v3.d.ts +7 -0
  132. package/dist/v2/fold/native-replay-v3.d.ts.map +1 -1
  133. package/dist/v2/fold/native-replay.d.ts +24 -7
  134. package/dist/v2/fold/native-replay.d.ts.map +1 -1
  135. package/dist/v2/fold/restore.d.ts.map +1 -1
  136. package/dist/v2/fold/summary.d.ts +4 -0
  137. package/dist/v2/fold/summary.d.ts.map +1 -0
  138. package/dist/v2/hidden-completion.d.ts.map +1 -1
  139. package/dist/v2/hooks/channel2.d.ts +6 -0
  140. package/dist/v2/hooks/channel2.d.ts.map +1 -1
  141. package/dist/v2/hooks/context.d.ts.map +1 -1
  142. package/dist/v2/hooks/restore-rows.d.ts +12 -2
  143. package/dist/v2/hooks/restore-rows.d.ts.map +1 -1
  144. package/dist/v2/hooks/synthetic-admission.d.ts +7 -0
  145. package/dist/v2/hooks/synthetic-admission.d.ts.map +1 -0
  146. package/dist/v2/hooks/usage-persist.d.ts.map +1 -1
  147. package/dist/v2/{message-fts-rowid-map-rmk571mb.js → message-fts-rowid-map-z86avy0n.js} +5 -4
  148. package/dist/v2/{message-index-bounded-620bppw7.js → message-index-bounded-w14496s9.js} +9 -8
  149. package/dist/v2/migration-worker.js +46 -41
  150. package/dist/v2/{project-embedding-registry-bm5fe1a8.js → project-embedding-registry-jqq2qeqb.js} +9 -8
  151. package/dist/v2/raw-ordinal-warmup-worker.js +114 -118
  152. package/dist/v2/{read-session-chunk-qfwv1k71.js → read-session-chunk-tqnh1ret.js} +7 -6
  153. package/dist/v2/{read-session-raw-q294t8xs.js → read-session-raw-qpp6x9kg.js} +4 -3
  154. package/dist/v2/{recomp-orchestrator-fcnjref7.js → recomp-orchestrator-9cjk6aej.js} +19 -18
  155. package/dist/v2/{render-98a1qv0b.js → render-5dz1knf2.js} +13 -12
  156. package/dist/v2/{search-z1zqzsmh.js → search-8yfet25y.js} +15 -14
  157. package/dist/v2/{search-archive-r0y1bafr.js → search-archive-z6z9jqwf.js} +9 -8
  158. package/dist/v2/{send-session-notification-fst6hkg7.js → send-session-notification-kxp47tsw.js} +4 -3
  159. package/dist/v2/{server-cpjk5jd2.js → server-1d042x6w.js} +16 -14
  160. package/dist/v2/{server-fqj6ddpr.js → server-5by3vdzs.js} +186 -89
  161. package/dist/v2/{server-5jp38p98.js → server-6emy0v9a.js} +1 -2
  162. package/dist/v2/{server-rktnzs89.js → server-6vzh6wej.js} +1 -1
  163. package/dist/v2/{server-qa74bj91.js → server-7094pk3w.js} +9 -8
  164. package/dist/v2/{server-bxpz2dcn.js → server-7acq7815.js} +8 -6
  165. package/dist/v2/server-7v05d9b7.js +58 -0
  166. package/dist/v2/{server-24g8r2d0.js → server-7zacmty5.js} +75 -75
  167. package/dist/v2/server-96ztjvkn.js +204 -0
  168. package/dist/v2/{server-hnknwaz8.js → server-a9w5f0yw.js} +3 -3
  169. package/dist/v2/{server-9k0mjef7.js → server-avdveykk.js} +1 -1
  170. package/dist/v2/{server-hsf0qsn5.js → server-cp1a03vc.js} +1 -1
  171. package/dist/v2/{server-r3chnxs5.js → server-dfq5vb8j.js} +2 -2
  172. package/dist/v2/{server-x4m6xcn4.js → server-e51xmr4d.js} +6 -6
  173. package/dist/v2/{server-7ccxt4nq.js → server-g3gqq4ac.js} +6 -3
  174. package/dist/v2/{server-kkxzwa6w.js → server-gkse1wyz.js} +3 -3
  175. package/dist/v2/{server-1xesqm5h.js → server-jd7vzwn4.js} +15 -13
  176. package/dist/v2/{server-ws5exe6q.js → server-nmyganc8.js} +1 -1
  177. package/dist/v2/{server-q0z2q2dx.js → server-nq3cb7be.js} +1 -1
  178. package/dist/v2/{server-55v5hpth.js → server-qjyp27vf.js} +2 -2
  179. package/dist/v2/{server-xbtw0p9t.js → server-r4430x9m.js} +9 -3
  180. package/dist/v2/{server-qf2kzsh2.js → server-t8y64xj7.js} +4 -4
  181. package/dist/v2/{server-s9xgyyj9.js → server-tnzbk64q.js} +34 -4
  182. package/dist/v2/{server-51779zs2.js → server-v0p37hm2.js} +4 -1
  183. package/dist/v2/{server-bpvsvtph.js → server-xfjjbrkt.js} +12 -1
  184. package/dist/v2/server.js +4 -4
  185. package/dist/v2/{storage-tags-19s0ebf0.js → storage-tags-cw07ky67.js} +1 -1
  186. package/dist/v2/{store-vdm8v3jd.js → store-z3qe67cg.js} +1 -1
  187. package/dist/v2/{v2-archive-reader-t2c0eypn.js → v2-archive-reader-z7wr42ry.js} +3 -3
  188. package/dist/v2/{wrapup-orchestrator-aqpqsveg.js → wrapup-orchestrator-j1v62bkj.js} +19 -18
  189. package/dist/{v2-archive-reader-tm2eayp2.js → v2-archive-reader-028z6kck.js} +2 -2
  190. package/dist/{wrapup-orchestrator-k5yp19a6.js → wrapup-orchestrator-xg9avt2p.js} +21 -20
  191. package/package.json +1 -1
  192. package/src/shared/model-suggestion-retry.ts +1 -1
  193. package/src/shared/prompt-async-transport.test.ts +64 -3
  194. package/src/shared/prompt-async-transport.ts +61 -16
  195. package/src/shared/reusable-worker-entry.ts +58 -0
  196. package/src/shared/reusable-worker-reset.test.ts +108 -0
  197. package/src/shared/reusable-worker.fixture.ts +35 -0
  198. package/src/shared/reusable-worker.test.ts +253 -0
  199. package/src/shared/reusable-worker.ts +240 -0
  200. 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,
@@ -787,6 +789,7 @@ class HostFoldIdle {
787
789
  now;
788
790
  timers;
789
791
  windows = new Map;
792
+ attempted = new Map;
790
793
  constructor(fold, now = Date.now, timers = { setTimeout, clearTimeout }) {
791
794
  this.fold = fold;
792
795
  this.now = now;
@@ -808,6 +811,9 @@ class HostFoldIdle {
808
811
  }
809
812
  if (!Number.isFinite(ttl) || lastResponseTime <= 0)
810
813
  return;
814
+ const attempted = this.attempted.get(sessionID);
815
+ if (attempted?.cacheTtl === cacheTtl && attempted.lastResponseTime === lastResponseTime)
816
+ return;
811
817
  const window = { cacheTtl, lastResponseTime };
812
818
  this.windows.set(sessionID, window);
813
819
  const eligible = () => this.windows.get(sessionID) === window && computeHardCacheExpired(window.cacheTtl, window.lastResponseTime, this.now());
@@ -816,6 +822,7 @@ class HostFoldIdle {
816
822
  return;
817
823
  if (eligible()) {
818
824
  window.timer = undefined;
825
+ this.attempted.set(sessionID, { cacheTtl, lastResponseTime });
819
826
  const release = () => {
820
827
  if (this.windows.get(sessionID) === window)
821
828
  this.windows.delete(sessionID);
@@ -832,9 +839,37 @@ class HostFoldIdle {
832
839
  dispose() {
833
840
  for (const sessionID of this.windows.keys())
834
841
  this.active(sessionID);
842
+ this.attempted.clear();
835
843
  }
836
844
  }
837
845
 
846
+ // src/v2/fold/manual-baseline.ts
847
+ function stageManualBaseline(db, sessionID, render) {
848
+ const read = () => db.prepare("SELECT * FROM session_meta WHERE session_id = ?").get(sessionID);
849
+ const before = read();
850
+ const columns = Object.keys(before).filter((key) => key.startsWith("cached_m0_") || key.startsWith("cached_m1_") || key === "memory_block_count" || key === "memory_block_ids");
851
+ const update = db.prepare(`UPDATE session_meta SET ${columns.map((key) => `${key} = ?`).join(", ")} WHERE session_id = ?`);
852
+ const restore = (row) => update.run(...columns.map((key) => row[key] ?? null), sessionID);
853
+ let summary;
854
+ let staged;
855
+ try {
856
+ summary = render();
857
+ staged = read();
858
+ } finally {
859
+ restore(before);
860
+ }
861
+ const commit = db.prepare(`UPDATE session_meta SET ${columns.map((key) => `${key} = ?`).join(", ")} WHERE session_id = ? AND ${columns.map((key) => `${key} IS ?`).join(" AND ")}`);
862
+ return {
863
+ summary,
864
+ boundaryID: staged.cached_m0_last_baseline_end_message_id,
865
+ commit() {
866
+ const result = commit.run(...columns.map((key) => staged[key] ?? null), sessionID, ...columns.map((key) => before[key] ?? null));
867
+ if (result.changes !== 1)
868
+ throw new Error("Manual compaction baseline changed during admission");
869
+ }
870
+ };
871
+ }
872
+
838
873
  // src/v2/fold/memory-cache.ts
839
874
  import { randomUUID } from "node:crypto";
840
875
 
@@ -1621,6 +1656,30 @@ function adaptPayload(draft, admittedIDs = new Set, messageTimes = new Map, hidd
1621
1656
  };
1622
1657
  }
1623
1658
 
1659
+ // src/v2/fold/code-mode-catalog.ts
1660
+ var changed = "The Code Mode tool catalog has changed.";
1661
+ var replacement = `${changed} This catalog supersedes the previous Code Mode tool catalog.`;
1662
+ function codeModeCatalogKind(data) {
1663
+ const text = data.text ?? "";
1664
+ const metadata = data.metadata;
1665
+ const keyed = typeof metadata === "object" && metadata !== null && "instructionSources" in metadata;
1666
+ if (keyed) {
1667
+ const sources = metadata.instructionSources;
1668
+ if (!Array.isArray(sources) || !sources.includes("core/codemode"))
1669
+ return;
1670
+ if (sources.length !== 1)
1671
+ return text.startsWith(replacement) ? "combined-replacement" : undefined;
1672
+ }
1673
+ if (text.startsWith(replacement))
1674
+ return "replacement";
1675
+ if (text.startsWith(changed))
1676
+ return "incremental";
1677
+ if (keyed || text.startsWith(`# Code Mode
1678
+ `) || text.startsWith("No Code Mode tools are currently available."))
1679
+ return "initial";
1680
+ return;
1681
+ }
1682
+
1624
1683
  // src/v2/fold/native-codec.ts
1625
1684
  class NativeReplayUnavailable extends Error {
1626
1685
  }
@@ -1674,11 +1733,24 @@ var keys = (value, allowed) => Object.keys(value).every((key) => allowed.include
1674
1733
  var identity = (value) => typeof value === "string" && value.length > 0 && Buffer.byteLength(value) <= 1024;
1675
1734
  var digest = (value) => typeof value === "string" && /^[a-f0-9]{64}$/.test(value);
1676
1735
  function validReplayState(value) {
1677
- if (!object(value) || !keys(value, ["floor", "controls", "blocked", "upgrade", "withoutRecentContext"]))
1736
+ if (!object(value) || !keys(value, [
1737
+ "floor",
1738
+ "controls",
1739
+ "blocked",
1740
+ "upgrade",
1741
+ "withoutRecentContext",
1742
+ "retiredInstructionThrough",
1743
+ "retiredInstructionIDs"
1744
+ ]))
1678
1745
  return false;
1679
1746
  const floor = value.floor;
1680
1747
  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));
1748
+ 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));
1749
+ }
1750
+ function instructionRetirement(replay) {
1751
+ const ids = new Set(replay?.retiredInstructionIDs);
1752
+ const through = replay?.retiredInstructionThrough ?? -1;
1753
+ return (row) => row.type === "system" && (row.seq <= through || ids.has(row.id));
1682
1754
  }
1683
1755
  function validAdmissionV3(value) {
1684
1756
  if (!object(value) || !keys(value, ["version", "proof", "replay"]) || value.version !== 3 || !object(value.proof))
@@ -1837,11 +1909,17 @@ function controlsThrough(controls, reader, sessionID, through) {
1837
1909
 
1838
1910
  // src/v2/fold/native-replay.ts
1839
1911
  var coordinates = new WeakMap;
1912
+ var sequences = new WeakMap;
1913
+ function nativeRowSequence(message) {
1914
+ return sequences.get(message);
1915
+ }
1840
1916
  function nativeRowID(message) {
1841
1917
  return coordinates.get(message) ?? message.id;
1842
1918
  }
1843
- function bindNativeRowID(message, id) {
1919
+ function bindNativeRowID(message, id, seq) {
1844
1920
  coordinates.set(message, id);
1921
+ if (seq !== undefined)
1922
+ sequences.set(message, seq);
1845
1923
  }
1846
1924
  function copyNativeInput(messages) {
1847
1925
  const copies = reviveNative(encodeNative(messages));
@@ -1850,6 +1928,9 @@ function copyNativeInput(messages) {
1850
1928
  const id = original && nativeRowID(original);
1851
1929
  if (id !== undefined)
1852
1930
  coordinates.set(copy, id);
1931
+ const seq = original && sequences.get(original);
1932
+ if (seq !== undefined)
1933
+ sequences.set(copy, seq);
1853
1934
  });
1854
1935
  return copies;
1855
1936
  }
@@ -1961,6 +2042,48 @@ class NativeFoldReplay {
1961
2042
  async baseline(sessionID) {
1962
2043
  return this.tail(sessionID)?.baseline;
1963
2044
  }
2045
+ async retiredInstructionThrough(sessionID, reader) {
2046
+ if (!reader.latestCompaction(sessionID))
2047
+ return -1;
2048
+ return (await this.floorState(sessionID, reader))?.retiredInstructionThrough ?? -1;
2049
+ }
2050
+ async instructionRetirement(sessionID, reader) {
2051
+ if (!reader.latestCompaction(sessionID))
2052
+ return instructionRetirement();
2053
+ return instructionRetirement(await this.floorState(sessionID, reader));
2054
+ }
2055
+ async prepareManualFold(sessionID, reader, boundaryID, summary) {
2056
+ const running = reader.latestRunningCompaction(sessionID);
2057
+ if (!running || this.options.protocol !== 3)
2058
+ throw new NativeReplayUnavailable("Manual compaction has no running host checkpoint");
2059
+ const previous = await this.floorState(sessionID, reader);
2060
+ if (this.unprovenControls.has(sessionID) || previous?.blocked)
2061
+ throw new NativeReplayUnavailable("Manual compaction controls have not been captured");
2062
+ const seq = boundaryID ? reader.sequenceForId(sessionID, boundaryID) : undefined;
2063
+ const row = seq === undefined ? undefined : reader.range(sessionID, seq - 1, seq)[0];
2064
+ const first = reader.page(sessionID, { limit: 1 }).rows[0];
2065
+ let floor = row ? floorFor(row, "summary") : undefined;
2066
+ floor ??= first ? floorFor(first, "start") : undefined;
2067
+ if (!floor)
2068
+ throw new NativeReplayUnavailable("Manual compaction has no history boundary");
2069
+ const instructions = reader.instructionRowsThrough(sessionID, running.seq - 1);
2070
+ const retiredInstructionIDs = [];
2071
+ let newerFullCatalog = false;
2072
+ for (const instruction of [...instructions].reverse()) {
2073
+ const text = instruction.data.text ?? "";
2074
+ const catalog = codeModeCatalogKind(instruction.data);
2075
+ if (instruction.seq > floor.after && (catalog && catalog !== "combined-replacement" && newerFullCatalog || text.length > 0 && summary.includes(text)))
2076
+ retiredInstructionIDs.push(instruction.id);
2077
+ if (catalog === "replacement" || catalog === "combined-replacement")
2078
+ newerFullCatalog = true;
2079
+ }
2080
+ return {
2081
+ floor,
2082
+ controls: previous?.controls ?? [],
2083
+ ...floor.after >= 0 ? { retiredInstructionThrough: floor.after } : {},
2084
+ retiredInstructionIDs
2085
+ };
2086
+ }
1964
2087
  admissionKey(sessionID) {
1965
2088
  return this.options.protocol === 3 ? `native-fold/v3/${encodeURIComponent(sessionID)}` : admissionKey(sessionID);
1966
2089
  }
@@ -2180,7 +2303,7 @@ ${cached}
2180
2303
  return pending.rows.filter((row) => row.seq > pending.value.replay.floor.after).flatMap((row) => {
2181
2304
  const messages = this.rendered(structuredClone(row), model);
2182
2305
  for (const message of messages)
2183
- coordinates.set(message, row.id);
2306
+ bindNativeRowID(message, row.id, row.seq);
2184
2307
  return messages;
2185
2308
  });
2186
2309
  }
@@ -2407,6 +2530,7 @@ ${cached}
2407
2530
  if (!isLocalCheckpoint(cut) || cut.session_id !== sessionID)
2408
2531
  return;
2409
2532
  let after = -1;
2533
+ const retired = instructionRetirement(replayOverride ?? this.tail(sessionID)?.replay);
2410
2534
  if (this.options.protocol === 3) {
2411
2535
  const replay = replayOverride ?? this.tail(sessionID)?.replay;
2412
2536
  if (!replay || !validFloor(reader, sessionID, replay.floor))
@@ -2421,7 +2545,7 @@ ${cached}
2421
2545
  }
2422
2546
  const through = cut.seq - 1;
2423
2547
  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);
2548
+ 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
2549
  const tail = this.tail(sessionID);
2426
2550
  let records;
2427
2551
  if (!this.dirty.has(sessionID) && tail?.model === `${model.providerID}/${model.id}` && this.storage.containsRows(sessionID, required.map((row) => row.id))) {
@@ -2432,10 +2556,10 @@ ${cached}
2432
2556
  if (required.some((row) => !restored.has(row.id)))
2433
2557
  return;
2434
2558
  }
2435
- const projected = records.flatMap((row) => {
2559
+ const projected = records.filter((row) => !retired(row)).flatMap((row) => {
2436
2560
  const messages = reviveNative(JSON.parse(row.data));
2437
2561
  for (const message of messages)
2438
- coordinates.set(message, row.id);
2562
+ bindNativeRowID(message, row.id, row.seq);
2439
2563
  return messages;
2440
2564
  });
2441
2565
  return this.options.protocol === 3 ? insertControls(projected, controlsThrough((replayOverride ?? this.tail(sessionID)?.replay)?.controls ?? [], reader, sessionID, through), nativeRowID) : projected;
@@ -2517,7 +2641,7 @@ ${cached}
2517
2641
  if (!row)
2518
2642
  throw new NativeReplayUnavailable("Native carrier has no source row coordinate");
2519
2643
  cursor = row.seq;
2520
- coordinates.set(message, row.id);
2644
+ bindNativeRowID(message, row.id, row.seq);
2521
2645
  if (message.role !== "tool")
2522
2646
  owner = row.id;
2523
2647
  const messages = grouped.get(row) ?? [];
@@ -2542,13 +2666,13 @@ ${cached}
2542
2666
  replay = { ...replay, blocked: false };
2543
2667
  const replayAfter = replay.floor.after;
2544
2668
  const replayStart = replay.floor.kind === "start";
2669
+ const baseline = this.servedBaseline(draft);
2545
2670
  captured = native.filter((message) => {
2546
2671
  if (effortPart(message))
2547
2672
  return false;
2548
2673
  const id = nativeRowID(message);
2549
2674
  if (HEAD_IDS.some((head) => head === id))
2550
2675
  return false;
2551
- const baseline = this.servedBaseline(draft);
2552
2676
  if (!id)
2553
2677
  return !(baseline && message.role === "user" && message.content.some((part) => part.type === "text" && part.text === baseline));
2554
2678
  if (replayStart)
@@ -2662,21 +2786,23 @@ ${cached}
2662
2786
  if (await this.admissions?.get(admissionKey(draft.sessionID)) && !await this.readV3(draft.sessionID))
2663
2787
  return false;
2664
2788
  const running = reader.latestRunningCompaction(draft.sessionID);
2665
- const replay = await this.floorState(draft.sessionID, reader, undefined, undefined, true);
2789
+ const replay = args.manual?.replay ?? await this.floorState(draft.sessionID, reader, undefined, undefined, true);
2666
2790
  if (!running || !replay || replay.blocked) {
2667
2791
  this.report(draft.sessionID, "fold refused: no usable host checkpoint boundary");
2668
2792
  return false;
2669
2793
  }
2670
2794
  const model = `${draft.model.providerID}/${draft.model.id}`;
2671
- if (this.tail(draft.sessionID)?.model !== model)
2795
+ if (!args.manual && this.tail(draft.sessionID)?.model !== model)
2672
2796
  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
- }));
2797
+ if (!args.manual)
2798
+ await this.retainReplay(draft.sessionID, replay, model, reader.latestSequence(draft.sessionID));
2799
+ if (!args.manual)
2800
+ await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
2801
+ source: this.storage.sourceID,
2802
+ sessionID: draft.sessionID,
2803
+ admissionID: running.id,
2804
+ summary
2805
+ }));
2680
2806
  const value = {
2681
2807
  version: 3,
2682
2808
  replay,
@@ -2689,6 +2815,57 @@ ${cached}
2689
2815
  }
2690
2816
  };
2691
2817
  const payload = encodeAdmissionV3(value);
2818
+ if (args.manual) {
2819
+ const keys = [
2820
+ capturedReplayKey(draft.sessionID),
2821
+ this.admissionKey(draft.sessionID)
2822
+ ];
2823
+ const old = await Promise.all(keys.map((key) => this.admissions?.get(key)));
2824
+ const capture = {
2825
+ version: 3,
2826
+ sessionID: draft.sessionID,
2827
+ sourceDigest: foldDigest(reader.hostIdentity()),
2828
+ through: reader.latestSequence(draft.sessionID),
2829
+ replay
2830
+ };
2831
+ const capturedPayload = JSON.stringify(capture);
2832
+ let written = 0;
2833
+ try {
2834
+ for (const [index, key] of keys.entries()) {
2835
+ const data = index === 0 ? capturedPayload : payload;
2836
+ await this.admissions?.set(key, {
2837
+ payload: data,
2838
+ digest: foldDigest(data)
2839
+ });
2840
+ written++;
2841
+ }
2842
+ args.manual.commit();
2843
+ } catch (error) {
2844
+ for (let index = written - 1;index >= 0; index--) {
2845
+ const key = keys[index];
2846
+ if (!key)
2847
+ continue;
2848
+ if (old[index] !== undefined)
2849
+ await this.admissions?.set(key, old[index]);
2850
+ else if (this.admissions?.remove)
2851
+ await this.admissions.remove(key);
2852
+ else
2853
+ await this.admissions?.set(key, null);
2854
+ }
2855
+ throw error;
2856
+ }
2857
+ this.invalidate(draft.sessionID);
2858
+ this.cancelLegacyUpgrade(draft.sessionID);
2859
+ this.cancelEditedAdmission(draft.sessionID);
2860
+ await this.retainReplay(draft.sessionID, replay, model, running.seq - 1);
2861
+ await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
2862
+ source: this.storage.sourceID,
2863
+ sessionID: draft.sessionID,
2864
+ admissionID: running.id,
2865
+ summary
2866
+ }));
2867
+ return true;
2868
+ }
2692
2869
  await this.admissions?.set(this.admissionKey(draft.sessionID), {
2693
2870
  payload,
2694
2871
  digest: foldDigest(payload)
@@ -2757,7 +2934,13 @@ ${cached}
2757
2934
  this.report(sessionID, "replay refused: missing usable checkpoint boundary");
2758
2935
  throw new NativeReplayUnavailable("Host checkpoint replay boundary is unavailable");
2759
2936
  }
2760
- options = { ...options, bounded: false, after: replay.floor.after };
2937
+ options = {
2938
+ ...options,
2939
+ bounded: false,
2940
+ after: replay.floor.after,
2941
+ retiredInstructionThrough: replay.retiredInstructionThrough,
2942
+ retiredInstructionIDs: replay.retiredInstructionIDs
2943
+ };
2761
2944
  }
2762
2945
  const release = this.storage.hold(sessionID);
2763
2946
  let result;
@@ -2823,10 +3006,13 @@ ${cached}
2823
3006
  reader?.close();
2824
3007
  }
2825
3008
  const messages = [];
3009
+ const retired = instructionRetirement(options);
2826
3010
  for (const row of this.storage.rows(sessionID, after, cut.seq - 1)) {
3011
+ if (retired(row))
3012
+ continue;
2827
3013
  const revived = reviveNative(JSON.parse(row.data));
2828
3014
  for (const message of revived)
2829
- coordinates.set(message, row.id);
3015
+ bindNativeRowID(message, row.id, row.seq);
2830
3016
  messages.push(...revived);
2831
3017
  }
2832
3018
  return messages;
@@ -2839,10 +3025,11 @@ ${cached}
2839
3025
  this.report(sessionID, "replay refused: missing usable checkpoint boundary");
2840
3026
  throw new NativeReplayUnavailable("Host checkpoint replay boundary is unavailable");
2841
3027
  }
2842
- const messages = reader.range(sessionID, replay?.floor.after ?? -1, cut.seq - 1).filter(conversational).flatMap((row) => {
3028
+ const retired = instructionRetirement(replay);
3029
+ const messages = reader.range(sessionID, replay?.floor.after ?? -1, cut.seq - 1).filter(conversational).filter((row) => !retired(row)).flatMap((row) => {
2843
3030
  const messages = this.rendered(row, model);
2844
3031
  for (const message of messages)
2845
- coordinates.set(message, row.id);
3032
+ bindNativeRowID(message, row.id, row.seq);
2846
3033
  return messages;
2847
3034
  });
2848
3035
  return replay ? insertControls(messages, controlsThrough(replay.controls, reader, sessionID, cut.seq - 1), nativeRowID) : messages;
@@ -2977,6 +3164,12 @@ class HostFoldPolicy {
2977
3164
  }
2978
3165
  }
2979
3166
 
3167
+ // src/v2/fold/summary.ts
3168
+ var RAW_HISTORY_ACCOUNT = "Magic Context retains the conversation as raw messages; no summarized history is available yet.";
3169
+ function hasFoldSummary(summary) {
3170
+ return typeof summary === "string" && summary.replace(/<[^>]*>/g, "").trim().length > 0;
3171
+ }
3172
+
2980
3173
  // src/v2/fork-inheritance.ts
2981
3174
  var RAW_TYPES = new Set(RAW_MESSAGE_TYPES2);
2982
3175
  function forkBoundarySeq(origin, sequenceForId) {
@@ -3866,6 +4059,26 @@ async function createV2HiddenCompletionExecutor(host, options) {
3866
4059
  const budgetStopped = new Promise((_resolve, reject) => {
3867
4060
  budgetReject = reject;
3868
4061
  });
4062
+ const chargeBudgetRows = (rows) => {
4063
+ if (!budget)
4064
+ return;
4065
+ for (const row of rows) {
4066
+ if (row.seq <= usageSeq)
4067
+ continue;
4068
+ if (!row.data.finish)
4069
+ break;
4070
+ usageSeq = row.seq;
4071
+ const tokens = row.data.tokens;
4072
+ 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);
4073
+ const onBudgetUpdate = run.identity.metadata?.onBudgetUpdate;
4074
+ if (typeof onBudgetUpdate === "function")
4075
+ onBudgetUpdate({ ...budget.snapshot(), sessionId: run.child.id });
4076
+ if (decision !== "continue") {
4077
+ attempt.budgetExceeded = new DreamTokenBudgetExceeded(run.child.id, budget.snapshot().spent);
4078
+ return attempt.budgetExceeded;
4079
+ }
4080
+ }
4081
+ };
3869
4082
  if (budget) {
3870
4083
  budgetPoll = inRootAsyncContext2(() => setInterval(() => {
3871
4084
  try {
@@ -3873,25 +4086,12 @@ async function createV2HiddenCompletionExecutor(host, options) {
3873
4086
  const latest = reader.latestAssistant(run.child.id);
3874
4087
  return latest ? [latest] : [];
3875
4088
  })());
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
- }
4089
+ const exceeded = chargeBudgetRows(fresh);
4090
+ if (exceeded) {
4091
+ if (budgetPoll)
4092
+ clearInterval(budgetPoll);
4093
+ interruptAndRetire(run, "token-budget").finally(() => budgetReject(exceeded));
4094
+ return;
3895
4095
  }
3896
4096
  } catch (error) {
3897
4097
  if (budgetPoll)
@@ -3988,6 +4188,11 @@ async function createV2HiddenCompletionExecutor(host, options) {
3988
4188
  const tokens = row.data.tokens;
3989
4189
  const tokenNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
3990
4190
  const steps = withReader(options.openReader, (reader) => reader.assistantSince?.(run.child.id, baseline) ?? [row]).filter((step) => step.seq > baseline && step.seq <= row.seq);
4191
+ const exceeded = chargeBudgetRows(steps);
4192
+ if (exceeded) {
4193
+ await interruptAndRetire(run, "token-budget");
4194
+ throw exceeded;
4195
+ }
3991
4196
  const reportedUsage = steps.some((step) => tokenNumber(step.data.tokens?.input) !== undefined || tokenNumber(step.data.tokens?.output) !== undefined);
3992
4197
  const usage = sumRowUsage(steps);
3993
4198
  if (request.signal?.aborted || run.retired || Date.now() >= deadline) {
@@ -4207,6 +4412,100 @@ class V2MessageTimesCache {
4207
4412
 
4208
4413
  // src/v2/hooks/channel2.ts
4209
4414
  import { randomBytes } from "node:crypto";
4415
+
4416
+ // src/v2/hooks/synthetic-admission.ts
4417
+ var memos = new WeakMap;
4418
+ var MAX_SESSIONS = 64;
4419
+ var MAX_IDS = 50000;
4420
+ function admissionSession(key) {
4421
+ if (!key.startsWith("synthetic/"))
4422
+ return;
4423
+ const end = key.indexOf("/", "synthetic/".length);
4424
+ return end < 0 ? undefined : key.slice("synthetic/".length, end);
4425
+ }
4426
+ function memoFor(storage) {
4427
+ const prior = memos.get(storage);
4428
+ if (prior)
4429
+ return prior;
4430
+ const sessions = new Map;
4431
+ const set = storage.set;
4432
+ const remove = storage.remove;
4433
+ const invalidate = (key) => {
4434
+ const sessionID = admissionSession(key);
4435
+ if (sessionID !== undefined) {
4436
+ sessions.delete(sessionID);
4437
+ const current = memos.get(storage);
4438
+ if (current) {
4439
+ current.sessions.delete(sessionID);
4440
+ current.revision++;
4441
+ }
4442
+ }
4443
+ };
4444
+ const wrappedSet = async (key, value) => {
4445
+ invalidate(key);
4446
+ try {
4447
+ await set.call(storage, key, value);
4448
+ } finally {
4449
+ invalidate(key);
4450
+ }
4451
+ };
4452
+ const wrappedRemove = remove && (async (key) => {
4453
+ invalidate(key);
4454
+ try {
4455
+ await remove.call(storage, key);
4456
+ } finally {
4457
+ invalidate(key);
4458
+ }
4459
+ });
4460
+ const memo = { sessions, revision: 0, set, remove, wrappedSet, wrappedRemove };
4461
+ storage.set = wrappedSet;
4462
+ if (wrappedRemove)
4463
+ storage.remove = wrappedRemove;
4464
+ memos.set(storage, memo);
4465
+ return memo;
4466
+ }
4467
+ function forgetSyntheticAdmission(storage, sessionID) {
4468
+ const memo = memoFor(storage);
4469
+ memo.sessions.delete(sessionID);
4470
+ memo.revision++;
4471
+ }
4472
+ function clearSyntheticAdmissions(storage) {
4473
+ const memo = memos.get(storage);
4474
+ if (!memo)
4475
+ return;
4476
+ memo.sessions.clear();
4477
+ if (storage.set === memo.wrappedSet)
4478
+ storage.set = memo.set;
4479
+ if (storage.remove === memo.wrappedRemove)
4480
+ storage.remove = memo.remove;
4481
+ memos.delete(storage);
4482
+ }
4483
+ async function admittedSynthetic(storage, sessionID, id) {
4484
+ for (;; ) {
4485
+ const memo = memoFor(storage);
4486
+ const session = memo.sessions.get(sessionID) ?? new Map;
4487
+ memo.sessions.delete(sessionID);
4488
+ memo.sessions.set(sessionID, session);
4489
+ if (memo.sessions.size > MAX_SESSIONS)
4490
+ memo.sessions.delete(memo.sessions.keys().next().value);
4491
+ const known = session.get(id);
4492
+ if (known !== undefined)
4493
+ return known;
4494
+ const revision = memo.revision;
4495
+ const record = await storage.get(`synthetic/${sessionID}/${id}`);
4496
+ if (memos.get(storage) !== memo || memo.revision !== revision)
4497
+ continue;
4498
+ const admitted = record !== null && typeof record === "object" && "id" in record && record.id === id;
4499
+ if (memo.sessions.get(sessionID) !== session)
4500
+ return admitted;
4501
+ if (session.size >= MAX_IDS)
4502
+ session.delete(session.keys().next().value);
4503
+ session.set(id, admitted);
4504
+ return admitted;
4505
+ }
4506
+ }
4507
+
4508
+ // src/v2/hooks/channel2.ts
4210
4509
  var pendingCandidates = new WeakMap;
4211
4510
  function syntheticCandidates(context, sessionID, stored) {
4212
4511
  const pending = pendingCandidates.get(context.storage);
@@ -4222,9 +4521,17 @@ function syntheticCandidates(context, sessionID, stored) {
4222
4521
  }
4223
4522
  function clearSyntheticCandidates(context) {
4224
4523
  pendingCandidates.delete(context.storage);
4524
+ clearSyntheticAdmissions(context.storage);
4525
+ }
4526
+ function observeSyntheticAdmissionEvent(context, event) {
4527
+ const sessionID = event.data?.sessionID;
4528
+ if (!sessionID || !(event.type === "session.deleted" || event.type?.startsWith("session.revert.")))
4529
+ return;
4530
+ forgetSyntheticAdmission(context.storage, sessionID);
4225
4531
  }
4226
4532
  async function deliverSynthetic(context, sessionID, text) {
4227
4533
  const id = `msg_${randomBytes(12).toString("hex")}`;
4534
+ forgetSyntheticAdmission(context.storage, sessionID);
4228
4535
  await context.storage.set(`synthetic/${sessionID}/${id}`, { id });
4229
4536
  let candidates = pendingCandidates.get(context.storage);
4230
4537
  if (!candidates) {
@@ -4236,8 +4543,7 @@ async function deliverSynthetic(context, sessionID, text) {
4236
4543
  return id;
4237
4544
  }
4238
4545
  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;
4546
+ return admittedSynthetic(context.storage, sessionID, id);
4241
4547
  }
4242
4548
  async function deliverPendingChannel2(context, db, sessionID, baseline) {
4243
4549
  if (getChannel2NudgeState(db, sessionID) !== "pending")
@@ -5066,16 +5372,31 @@ async function interruptBeforeProvider(session, sessionID, timers = systemTimers
5066
5372
  }
5067
5373
 
5068
5374
  // src/v2/hooks/restore-rows.ts
5375
+ var DEFAULT_LIMITS = {
5376
+ maxSessions: 16,
5377
+ sessionRows: 40000,
5378
+ totalRows: 80000,
5379
+ sessionBytes: 192 * 1024 * 1024,
5380
+ totalBytes: 256 * 1024 * 1024
5381
+ };
5382
+
5069
5383
  class RestoredRowCache {
5070
- capacity;
5071
5384
  sessions = new Map;
5072
- constructor(capacity = 16) {
5073
- this.capacity = capacity;
5385
+ limits;
5386
+ totalRows = 0;
5387
+ totalBytes = 0;
5388
+ constructor(limits = {}) {
5389
+ this.limits = {
5390
+ ...DEFAULT_LIMITS,
5391
+ ...typeof limits === "number" ? { maxSessions: limits } : limits
5392
+ };
5074
5393
  }
5075
5394
  rows(reader, sessionID, after, through) {
5076
- if (through <= after)
5395
+ if (through <= after) {
5396
+ this.forget(sessionID);
5077
5397
  return [];
5078
- const kept = this.sessions.get(sessionID) ?? new Map;
5398
+ }
5399
+ const kept = this.sessions.get(sessionID)?.rows ?? new Map;
5079
5400
  const stamps = reader.spanRowStamps(sessionID, after, through);
5080
5401
  const runs = [];
5081
5402
  let previous = after;
@@ -5097,28 +5418,44 @@ class RestoredRowCache {
5097
5418
  fresh.set(row.seq, row);
5098
5419
  const next = new Map;
5099
5420
  const rows = [];
5421
+ let bytes = 0;
5100
5422
  for (const [seq, stamp] of stamps) {
5101
5423
  const row = fresh.get(seq) ?? kept.get(seq)?.row;
5102
5424
  if (!row)
5103
5425
  continue;
5104
- next.set(seq, { stamp, row });
5105
5426
  rows.push(row);
5427
+ const rowBytes = Number(stamp.slice(stamp.lastIndexOf("\x00") + 1));
5428
+ 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))
5429
+ continue;
5430
+ next.set(seq, { stamp, row });
5431
+ bytes += rowBytes;
5106
5432
  }
5107
- this.sessions.delete(sessionID);
5108
- this.sessions.set(sessionID, next);
5109
- while (this.sessions.size > this.capacity) {
5433
+ this.forget(sessionID);
5434
+ if (next.size) {
5435
+ this.sessions.set(sessionID, { rows: next, bytes });
5436
+ this.totalRows += next.size;
5437
+ this.totalBytes += bytes;
5438
+ }
5439
+ while (this.sessions.size > this.limits.maxSessions || this.totalRows > this.limits.totalRows || this.totalBytes > this.limits.totalBytes) {
5110
5440
  const oldest = this.sessions.keys().next().value;
5111
5441
  if (oldest === undefined)
5112
5442
  break;
5113
- this.sessions.delete(oldest);
5443
+ this.forget(oldest);
5114
5444
  }
5115
5445
  return structuredClone(rows);
5116
5446
  }
5117
5447
  forget(sessionID) {
5448
+ const prior = this.sessions.get(sessionID);
5449
+ if (prior) {
5450
+ this.totalRows -= prior.rows.size;
5451
+ this.totalBytes -= prior.bytes;
5452
+ }
5118
5453
  this.sessions.delete(sessionID);
5119
5454
  }
5120
5455
  clear() {
5121
5456
  this.sessions.clear();
5457
+ this.totalRows = 0;
5458
+ this.totalBytes = 0;
5122
5459
  }
5123
5460
  }
5124
5461
  function readPages(reader, sessionID, after, through) {
@@ -5518,7 +5855,7 @@ function persistV2UsageReading(args) {
5518
5855
  args.contextUsageMap.set(sessionID, {
5519
5856
  usage: { inputTokens: reading.inputTokens, percentage },
5520
5857
  hasUsageTokens: true,
5521
- updatedAt: Date.now()
5858
+ updatedAt: reading.completed || Date.now()
5522
5859
  });
5523
5860
  } else {
5524
5861
  args.contextUsageMap.delete(sessionID);
@@ -5566,7 +5903,7 @@ function bindKeptInstructionCoordinates(messages, incoming, reader, sessionID) {
5566
5903
  selected.forEach((row, index) => {
5567
5904
  const message = pending[index];
5568
5905
  if (message)
5569
- bindNativeRowID(message, row.id);
5906
+ bindNativeRowID(message, row.id, row.seq);
5570
5907
  });
5571
5908
  }
5572
5909
  pending = [];
@@ -5860,10 +6197,10 @@ async function applyV2SystemPrompt(systemPrompt, draft) {
5860
6197
  text
5861
6198
  })));
5862
6199
  }
5863
- function coveredInstructionRows(reader, sessionID, throughSeq, model) {
6200
+ function coveredInstructionRows(reader, sessionID, throughSeq, model, retired = () => false) {
5864
6201
  if (throughSeq < 0)
5865
6202
  return [];
5866
- return reader.instructionRowsThrough(sessionID, throughSeq).flatMap((row) => restoreRow(row, model)).map((message) => markCoveredInstructionRow(message));
6203
+ return reader.instructionRowsThrough(sessionID, throughSeq).filter((row) => !retired(row)).flatMap((row) => restoreRow(row, model)).map((message) => markCoveredInstructionRow(message));
5867
6204
  }
5868
6205
  async function registerContext(context) {
5869
6206
  const directory = context.location.directory;
@@ -6342,7 +6679,15 @@ async function registerContext(context) {
6342
6679
  storage: nativeFoldMemory,
6343
6680
  openReader: openStoreReader,
6344
6681
  canReplay: (sessionID, reader) => nativeFoldReplay.canReplay(sessionID, reader),
6345
- canFold: (sessionID, reader) => nativeFoldReplay.canFold(sessionID, reader),
6682
+ canFold: async (sessionID, reader) => {
6683
+ const meta = getOrCreateSessionMeta(storage.require(), sessionID);
6684
+ const summary = [
6685
+ servedModuleM0Text(sessionID),
6686
+ await nativeFoldReplay.baseline(sessionID),
6687
+ meta.cachedM0Bytes?.toString("utf8")
6688
+ ].find((value) => value !== RAW_HISTORY_ACCOUNT && hasFoldSummary(value));
6689
+ return !meta.isSubagent && summary !== RAW_HISTORY_ACCOUNT && hasFoldSummary(summary) && nativeFoldReplay.canFold(sessionID, reader);
6690
+ },
6346
6691
  threshold: Number(process.env.MC_OC2_HOST_FOLD_ROWS ?? 1000),
6347
6692
  log: sessionLog2
6348
6693
  }) : undefined;
@@ -6368,6 +6713,7 @@ async function registerContext(context) {
6368
6713
  if (!event.data?.sessionID)
6369
6714
  continue;
6370
6715
  const sessionID = event.data.sessionID;
6716
+ observeSyntheticAdmissionEvent(context, event);
6371
6717
  overflowRecovery.observeTerminal(event, (id, messageID) => {
6372
6718
  const reader = openStoreReader();
6373
6719
  try {
@@ -6549,22 +6895,46 @@ async function registerContext(context) {
6549
6895
  await storage.probe();
6550
6896
  const watermark = reader.latestSequenceForIds(draft.sessionID, draft.messages.flatMap((message) => message.id ? [message.id] : []));
6551
6897
  const running = reader.latestRunningCompaction(draft.sessionID);
6898
+ const attempt = running && nativeFoldMemory?.admission(draft.sessionID, running.id);
6899
+ const manual = running?.data.reason === "manual" && !(attempt?.status === "pending" && attempt.owner_token);
6552
6900
  const moduleBaseline = rustModeModuleClient ? servedModuleM0Text(draft.sessionID) : undefined;
6553
6901
  const source = !rustModeModuleClient ? "typescript" : moduleBaseline === null ? "typescript_fallback" : "module";
6554
6902
  if (nativeFoldReplay) {
6555
6903
  try {
6556
6904
  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({
6905
+ const staged = manual && moduleBaseline == null ? stageManualBaseline(storage.require(), draft.sessionID, () => materialize(draft)) : undefined;
6906
+ const publishedHistory = Boolean(storage.require().prepare("SELECT 1 FROM compartments WHERE session_id = ? LIMIT 1").get(draft.sessionID));
6907
+ const candidate = [
6908
+ moduleBaseline,
6909
+ await nativeFoldReplay.baseline(draft.sessionID),
6910
+ state.cachedM0Bytes?.toString("utf8"),
6911
+ (await folds.read(draft.sessionID))?.submitted
6912
+ ].find((value) => value !== RAW_HISTORY_ACCOUNT && (publishedHistory && value?.includes("<session-history>") || hasFoldSummary(value)));
6913
+ const manualHistory = manual && hasFoldSummary(staged?.summary ?? moduleBaseline);
6914
+ const summary = manualHistory ? staged?.summary ?? candidate ?? RAW_HISTORY_ACCOUNT : candidate ?? RAW_HISTORY_ACCOUNT;
6915
+ 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;
6916
+ if ((hasFoldSummary(summary) || publishedHistory) && await nativeFoldReplay.supply({
6559
6917
  draft,
6560
6918
  reader,
6561
6919
  previousCut: reader.latestCompaction(draft.sessionID),
6562
- summary
6920
+ summary,
6921
+ manual: manualReplay ? { replay: manualReplay, commit: () => staged?.commit() } : undefined
6563
6922
  })) {
6923
+ if (manualHistory) {
6924
+ dropSlot(draft.sessionID, "manual host compaction");
6925
+ generateReplay.forget(draft.sessionID);
6926
+ restoredRows.forget(draft.sessionID);
6927
+ historyRefreshSessions.add(draft.sessionID);
6928
+ requestPendingMaterialization(pendingMaterializationSessions, draft.sessionID, "host_compaction");
6929
+ }
6564
6930
  draft.result = { summary, metadata: { keepID: undefined } };
6565
6931
  return;
6566
6932
  }
6933
+ if (manual)
6934
+ throw new V2ContextRefusal("Magic Context could not admit the manual fold.");
6567
6935
  } catch (error) {
6936
+ if (error instanceof V2ContextRefusal || manual)
6937
+ throw error;
6568
6938
  nativeFoldReplay.invalidate(draft.sessionID, true);
6569
6939
  sessionLog2(draft.sessionID, `v2 host fold: legacy compaction reason=${JSON.stringify(String(error))}`);
6570
6940
  }
@@ -6573,7 +6943,10 @@ async function registerContext(context) {
6573
6943
  sessionID: draft.sessionID,
6574
6944
  watermark,
6575
6945
  runningCut: running?.seq,
6576
- materialize: () => moduleBaseline ?? materialize(draft)
6946
+ materialize: () => {
6947
+ const summary = moduleBaseline ?? materialize(draft);
6948
+ return hasFoldSummary(summary) ? summary : RAW_HISTORY_ACCOUNT;
6949
+ }
6577
6950
  });
6578
6951
  sessionLog2(draft.sessionID, `v2 compaction hook: fired answered=true source=${source}`);
6579
6952
  draft.result = { summary: fold.submitted, metadata: { keepID: undefined } };
@@ -6588,6 +6961,19 @@ async function registerContext(context) {
6588
6961
  });
6589
6962
  const temporalTimes = config.temporal_awareness === true;
6590
6963
  const anchorTimesOnly = !temporalTimes && !rustModeModuleClient;
6964
+ const retireInstructionMessages = async (sessionID, reader, messages) => {
6965
+ const retired = await nativeFoldReplay?.instructionRetirement(sessionID, reader);
6966
+ if (!retired)
6967
+ return messages;
6968
+ return messages.filter((message) => {
6969
+ const id = nativeRowID(message);
6970
+ return message.role !== "system" || !id || !retired({
6971
+ type: "system",
6972
+ id,
6973
+ seq: nativeRowSequence(message) ?? reader.sequenceForId(sessionID, id) ?? Infinity
6974
+ });
6975
+ });
6976
+ };
6591
6977
  const timeIds = (messages) => {
6592
6978
  if (temporalTimes)
6593
6979
  return messages.flatMap((message) => message.id ? [message.id] : []);
@@ -6609,16 +6995,17 @@ async function registerContext(context) {
6609
6995
  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
6996
  const servedBoundary = boundaryID ? servedBoundaryRow2(reader, draft.sessionID, boundaryID) : null;
6611
6997
  const nativeFloor = await nativeFoldReplay?.restorationFloor(draft.sessionID, cut, reader);
6998
+ const retiredInstructions = await nativeFoldReplay?.instructionRetirement(draft.sessionID, reader) ?? (() => false);
6612
6999
  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
7000
  if (moduleBoundarySeq !== undefined)
6614
7001
  sessionLog2(draft.sessionID, `v2 restore: from the module boundary ${moduleBoundaryID} to the host checkpoint`);
6615
7002
  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() ? {
7003
+ 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
7004
  asset: hostMediaAsset,
6618
7005
  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
7006
  } : undefined));
6620
7007
  return [
6621
- ...rustModeModuleClient ? [] : coveredInstructionRows(reader, draft.sessionID, Math.min(boundary, cut.seq), draft.model),
7008
+ ...rustModeModuleClient ? [] : coveredInstructionRows(reader, draft.sessionID, Math.min(boundary, cut.seq), draft.model, retiredInstructions),
6622
7009
  ...restored,
6623
7010
  ...args.messages.filter((message) => message !== args.incoming)
6624
7011
  ];
@@ -6851,6 +7238,7 @@ async function registerContext(context) {
6851
7238
  }
6852
7239
  } else if (localCut)
6853
7240
  return;
7241
+ messages = await retireInstructionMessages(draft.sessionID, reader, messages);
6854
7242
  times = readTimes(reader, messages);
6855
7243
  }
6856
7244
  } finally {
@@ -7080,6 +7468,7 @@ async function registerContext(context) {
7080
7468
  boundaryRepair
7081
7469
  }));
7082
7470
  }
7471
+ draft.messages = await retireInstructionMessages(draft.sessionID, reader, draft.messages);
7083
7472
  messageTimes = readTimes(reader, draft.messages);
7084
7473
  } finally {
7085
7474
  reader.close();
@@ -7260,6 +7649,11 @@ async function registerContext(context) {
7260
7649
  if (!deletedSessions.has(draft.sessionID) && admissionRebuildPasses.has(draft.sessionID)) {
7261
7650
  await nativeFoldReplay?.commitLegacyUpgrade(draft.sessionID, true);
7262
7651
  await nativeFoldReplay?.commitEditedAdmission(draft.sessionID);
7652
+ if (db && hostFoldIdle) {
7653
+ const meta = getOrCreateSessionMeta(db, draft.sessionID);
7654
+ if (computeHardCacheExpired(meta.cacheTtl, meta.lastResponseTime, Date.now()))
7655
+ hostFoldIdle.idle(draft.sessionID, meta.cacheTtl, meta.lastResponseTime);
7656
+ }
7263
7657
  }
7264
7658
  } finally {
7265
7659
  admissionRebuildPasses.delete(draft.sessionID);