@cortexkit/opencode-magic-context 0.49.20 → 0.49.21

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 (188) hide show
  1. package/dist/auto-search-worker.js +6 -6
  2. package/dist/chunk-embedding-write-worker.js +9 -9
  3. package/dist/{compaction-marker-candidates-b2q30cft.js → compaction-marker-candidates-31h86ph0.js} +2 -2
  4. package/dist/compaction-marker-candidates-worker.js +2 -2
  5. package/dist/{compartment-chunk-embedding-va090as0.js → compartment-chunk-embedding-he60aaa2.js} +5 -5
  6. package/dist/config/project-security.d.ts.map +1 -1
  7. package/dist/config/schema/magic-context.d.ts +37 -3
  8. package/dist/config/schema/magic-context.d.ts.map +1 -1
  9. package/dist/{conflict-warning-hook-frp3z2wk.js → conflict-warning-hook-2r32hyx6.js} +5 -5
  10. package/dist/{context-mkece1db.js → context-j7hp43a0.js} +153 -83
  11. package/dist/{embedding-backfill-gy3p78py.js → embedding-backfill-dtke6tck.js} +11 -11
  12. package/dist/embedding-worker-process.js +1 -1
  13. package/dist/embedding-worker.js +4 -4
  14. package/dist/features/magic-context/dreamer/docs-proposals.d.ts +1 -0
  15. package/dist/features/magic-context/dreamer/docs-proposals.d.ts.map +1 -1
  16. package/dist/features/magic-context/dreamer/task-executor.d.ts.map +1 -1
  17. package/dist/features/magic-context/memory/embedding-synapse.d.ts.map +1 -1
  18. package/dist/features/magic-context/memory/embedding-worker-client.d.ts +7 -1
  19. package/dist/features/magic-context/memory/embedding-worker-client.d.ts.map +1 -1
  20. package/dist/features/magic-context/message-index-async.d.ts +7 -2
  21. package/dist/features/magic-context/message-index-async.d.ts.map +1 -1
  22. package/dist/features/magic-context/project-embedding-registry.d.ts +2 -0
  23. package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
  24. package/dist/features/magic-context/search-rerank.d.ts +9 -2
  25. package/dist/features/magic-context/search-rerank.d.ts.map +1 -1
  26. package/dist/features/magic-context/search.d.ts +2 -0
  27. package/dist/features/magic-context/search.d.ts.map +1 -1
  28. package/dist/features/magic-context/storage-db.d.ts +2 -0
  29. package/dist/features/magic-context/storage-db.d.ts.map +1 -1
  30. package/dist/features/magic-context/storage-meta-persisted.d.ts +1 -0
  31. package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/auto-search-worker-client.d.ts.map +1 -1
  33. package/dist/hooks/magic-context/cache-busting-signals.d.ts +1 -0
  34. package/dist/hooks/magic-context/cache-busting-signals.d.ts.map +1 -1
  35. package/dist/hooks/magic-context/hook.d.ts.map +1 -1
  36. package/dist/hooks/magic-context/served-shape-repair.d.ts +19 -0
  37. package/dist/hooks/magic-context/served-shape-repair.d.ts.map +1 -0
  38. package/dist/hooks/magic-context/strip-content.d.ts +4 -2
  39. package/dist/hooks/magic-context/strip-content.d.ts.map +1 -1
  40. package/dist/hooks/magic-context/tag-content-primitives.d.ts +1 -1
  41. package/dist/hooks/magic-context/tag-content-primitives.d.ts.map +1 -1
  42. package/dist/hooks/magic-context/tag-messages.d.ts +2 -1
  43. package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
  44. package/dist/hooks/magic-context/text-complete.d.ts +1 -1
  45. package/dist/hooks/magic-context/text-complete.d.ts.map +1 -1
  46. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  47. package/dist/hooks/magic-context/transform.d.ts +2 -2
  48. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  49. package/dist/{index-t7hx3qcb.js → index-11gzxvf4.js} +101 -46
  50. package/dist/{index-neb2jaed.js → index-1gtfne2c.js} +1 -1
  51. package/dist/{index-67nyj0gy.js → index-1kbznkft.js} +48 -24
  52. package/dist/{index-1pefp8ar.js → index-3svaejkh.js} +4 -4
  53. package/dist/{index-0wthn7fp.js → index-51rdvtkz.js} +35 -4
  54. package/dist/{index-kc9pn495.js → index-66eby7hx.js} +1 -1
  55. package/dist/{index-pnjwa6qa.js → index-9q1g3c5q.js} +2 -2
  56. package/dist/{index-rmd0069m.js → index-a9zvn5fe.js} +24 -10
  57. package/dist/{index-jfh3cktr.js → index-ffzhwfzy.js} +3 -3
  58. package/dist/{v2/server-1zx2pa5d.js → index-gzy5w8ya.js} +193 -4
  59. package/dist/{index-d6rp4dxh.js → index-j32jj2t8.js} +4 -4
  60. package/dist/{index-35qc6606.js → index-jfem3dgz.js} +1 -1
  61. package/dist/{index-y4a3nhtk.js → index-jjvbk92s.js} +1 -1
  62. package/dist/{index-4n7w9rad.js → index-jmq8kfe7.js} +5 -5
  63. package/dist/{index-c4b76d4t.js → index-kfyb3k6n.js} +1 -1
  64. package/dist/{index-vkc1ca9p.js → index-kq5w68dp.js} +1 -1
  65. package/dist/{index-11zf2wwc.js → index-ksq50rg0.js} +2 -2
  66. package/dist/{index-153r2q6z.js → index-m32esgmj.js} +2 -2
  67. package/dist/{index-x4k3f4mf.js → index-mnhcdfxg.js} +148 -8
  68. package/dist/{index-48stjd56.js → index-mnnkgq0q.js} +10 -4
  69. package/dist/{index-wgypt3cs.js → index-nxrsns5x.js} +2466 -1290
  70. package/dist/{index-616rsjrv.js → index-qgh1qwjj.js} +1 -1
  71. package/dist/{index-jr5na10t.js → index-rxqr723x.js} +4 -4
  72. package/dist/{index-dc8hy95c.js → index-syaazvy2.js} +123 -40
  73. package/dist/{index-adk2z5d8.js → index-wfybj1dw.js} +2 -4
  74. package/dist/{index-305y133n.js → index-whcgcfrv.js} +6 -3
  75. package/dist/{index-jmycsey7.js → index-x6cbrv9h.js} +2 -2
  76. package/dist/{index-v7r4td4c.js → index-y76rhsnr.js} +28 -17
  77. package/dist/{index-2yhtnfcf.js → index-zy67whjr.js} +26 -8
  78. package/dist/index.js +43 -42
  79. package/dist/{message-fts-rowid-map-22tevnnj.js → message-fts-rowid-map-e79b7b6x.js} +3 -3
  80. package/dist/{message-index-5qxcz6h7.js → message-index-4q8ek2fc.js} +10 -10
  81. package/dist/{message-index-bounded-n0m0t7tv.js → message-index-bounded-rydsg9r5.js} +10 -10
  82. package/dist/{message-time-backfill-0myx7fz3.js → message-time-backfill-4qva0b4f.js} +1 -1
  83. package/dist/migration-worker.js +6 -6
  84. package/dist/plugin/embedding-bootstrap.d.ts +1 -0
  85. package/dist/plugin/embedding-bootstrap.d.ts.map +1 -1
  86. package/dist/plugin/hooks/create-session-hooks.d.ts +1 -0
  87. package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
  88. package/dist/plugin/tool-registry.d.ts.map +1 -1
  89. package/dist/{project-embedding-registry-s2pqgec9.js → project-embedding-registry-hn529988.js} +12 -10
  90. package/dist/raw-ordinal-warmup-worker.js +8 -8
  91. package/dist/{read-session-chunk-0a8vdyqp.js → read-session-chunk-t039f0m5.js} +6 -6
  92. package/dist/{read-session-raw-y5cszf0b.js → read-session-raw-aw6jgvwg.js} +2 -2
  93. package/dist/{recomp-orchestrator-61vyha9j.js → recomp-orchestrator-hbm53jj5.js} +23 -23
  94. package/dist/{search-archive-8q9s49p5.js → search-archive-weh164mh.js} +7 -7
  95. package/dist/{search-v1y9ejws.js → search-hea079d7.js} +19 -19
  96. package/dist/{send-session-notification-0k6qzydk.js → send-session-notification-gqzkz7da.js} +4 -4
  97. package/dist/shared/sqlite.d.ts +5 -0
  98. package/dist/shared/sqlite.d.ts.map +1 -1
  99. package/dist/shared/tag-transcript.d.ts +2 -0
  100. package/dist/shared/tag-transcript.d.ts.map +1 -1
  101. package/dist/{sqlite-1rpvw2rj.js → sqlite-zdgm52sv.js} +5 -1
  102. package/dist/{storage-memory-nm7v82mj.js → storage-memory-e6a7mb0k.js} +2 -2
  103. package/dist/{storage-tags-r6nk29n4.js → storage-tags-a77q4v77.js} +2 -2
  104. package/dist/tools/ctx-search/tools.d.ts.map +1 -1
  105. package/dist/tools/ctx-search/types.d.ts +1 -0
  106. package/dist/tools/ctx-search/types.d.ts.map +1 -1
  107. package/dist/{transform-postprocess-phase-vtrgb7m6.js → transform-postprocess-phase-0b04tm1p.js} +23 -23
  108. package/dist/v2/auto-search-worker.js +6 -6
  109. package/dist/v2/chunk-embedding-write-worker.js +11 -11
  110. package/dist/v2/{compaction-marker-candidates-cfab70cs.js → compaction-marker-candidates-m4vmye3w.js} +2 -2
  111. package/dist/v2/compaction-marker-candidates-worker.js +2 -2
  112. package/dist/v2/{compartment-chunk-embedding-nbdqx1td.js → compartment-chunk-embedding-8jjmcgeg.js} +5 -5
  113. package/dist/v2/{context-cc33tv2z.js → context-ea3x7smm.js} +652 -468
  114. package/dist/v2/{embedding-backfill-yxv2h5mc.js → embedding-backfill-5e74ae0m.js} +11 -11
  115. package/dist/v2/embedding-worker-process.js +1 -1
  116. package/dist/v2/embedding-worker.js +4 -4
  117. package/dist/v2/hooks/context.d.ts.map +1 -1
  118. package/dist/v2/hooks/copied-tag-notation.d.ts +1 -1
  119. package/dist/v2/hooks/copied-tag-notation.d.ts.map +1 -1
  120. package/dist/v2/hooks/dream-timer.d.ts +2 -0
  121. package/dist/v2/hooks/dream-timer.d.ts.map +1 -1
  122. package/dist/v2/hooks/embedding-lifecycle.d.ts +7 -0
  123. package/dist/v2/hooks/embedding-lifecycle.d.ts.map +1 -0
  124. package/dist/v2/hooks/payload.d.ts.map +1 -1
  125. package/dist/v2/hooks/store.d.ts +2 -0
  126. package/dist/v2/hooks/store.d.ts.map +1 -1
  127. package/dist/v2/hooks/tools.d.ts +2 -1
  128. package/dist/v2/hooks/tools.d.ts.map +1 -1
  129. package/dist/v2/hooks/transform-features.d.ts +2 -2
  130. package/dist/v2/hooks/transform-features.d.ts.map +1 -1
  131. package/dist/v2/{message-fts-rowid-map-dg12wymk.js → message-fts-rowid-map-n8fdg2ja.js} +3 -3
  132. package/dist/v2/{message-index-bounded-f4e01xk0.js → message-index-bounded-8rcd7qq9.js} +12 -12
  133. package/dist/v2/{message-index-ydzrp6y6.js → message-index-qz9wdp82.js} +12 -12
  134. package/dist/v2/{message-time-backfill-ddh8n9gy.js → message-time-backfill-zvthw6xb.js} +1 -1
  135. package/dist/v2/migration-worker.js +8 -8
  136. package/dist/v2/{project-embedding-registry-deza9ksw.js → project-embedding-registry-v38q4xd5.js} +12 -10
  137. package/dist/v2/raw-ordinal-warmup-worker.js +8 -8
  138. package/dist/v2/{read-session-chunk-8cxfagky.js → read-session-chunk-wazfyxcg.js} +8 -8
  139. package/dist/v2/{read-session-raw-1nrrecvx.js → read-session-raw-b70cpwzj.js} +2 -2
  140. package/dist/v2/{recomp-orchestrator-47m4hv5g.js → recomp-orchestrator-b12zaptj.js} +25 -25
  141. package/dist/v2/{search-archive-e80cvc09.js → search-archive-x64rtz1z.js} +9 -9
  142. package/dist/v2/{search-fjz746zr.js → search-hza9x72a.js} +21 -21
  143. package/dist/v2/{send-session-notification-nwvrv34d.js → send-session-notification-8eqe259v.js} +4 -4
  144. package/dist/v2/{server-4sxjs6hn.js → server-132h8twc.js} +4 -4
  145. package/dist/v2/{server-271qxsf5.js → server-24g8r2d0.js} +2466 -1290
  146. package/dist/v2/{server-m91f9m5s.js → server-33nqbqkw.js} +123 -40
  147. package/dist/v2/{server-brj8j2gk.js → server-3vhxy2k5.js} +1 -1
  148. package/dist/v2/{server-55rgxfrw.js → server-4kjmrjn4.js} +1 -1
  149. package/dist/v2/{server-wyawsa88.js → server-62g58aaz.js} +2 -2
  150. package/dist/v2/{server-230fnbws.js → server-6v3cdzsx.js} +1 -1
  151. package/dist/v2/{server-ygtykank.js → server-806rzhs6.js} +1 -1
  152. package/dist/v2/{server-5arr9ecn.js → server-94wk1ey3.js} +12 -1
  153. package/dist/v2/{server-af2pght7.js → server-ab1g0wn9.js} +2 -4
  154. package/dist/v2/{server-0epqaxhd.js → server-ad18djzs.js} +26 -8
  155. package/dist/v2/{server-4nw4fqhr.js → server-bsgd4dk1.js} +1 -1
  156. package/dist/v2/{server-dj7tz4ph.js → server-d1dzgvwd.js} +1 -1
  157. package/dist/v2/{server-vn2nwcv6.js → server-d48x3m4x.js} +4 -4
  158. package/dist/v2/{server-br1x0tev.js → server-dtp8hcjh.js} +2 -2
  159. package/dist/v2/{server-a3dwxfgd.js → server-fa7vtp40.js} +7 -4
  160. package/dist/{index-1zx2pa5d.js → v2/server-gzy5w8ya.js} +193 -4
  161. package/dist/v2/{server-hqzyq8n4.js → server-j8h7bsca.js} +26 -12
  162. package/dist/v2/{server-y4a3nhtk.js → server-jjvbk92s.js} +1 -1
  163. package/dist/v2/{server-ej96a39v.js → server-m4bfaevz.js} +1186 -1046
  164. package/dist/v2/{server-63fqdghs.js → server-ppcd1qxh.js} +92 -86
  165. package/dist/v2/{server-ne8arh8t.js → server-qjvk4a9t.js} +1 -1
  166. package/dist/v2/{server-xsryvyf6.js → server-qteg2kt9.js} +4 -4
  167. package/dist/v2/{server-168pb207.js → server-rqebawx1.js} +1 -1
  168. package/dist/v2/{server-j4jnp9b5.js → server-vr4szghm.js} +35 -4
  169. package/dist/v2/{server-9x0kmta4.js → server-wwaw5npa.js} +3 -3
  170. package/dist/v2/{server-c7dxxsj7.js → server-zg5y109h.js} +33 -33
  171. package/dist/v2/server.js +6 -6
  172. package/dist/v2/{sqlite-1rpvw2rj.js → sqlite-zdgm52sv.js} +5 -1
  173. package/dist/v2/{storage-memory-nm7v82mj.js → storage-memory-e6a7mb0k.js} +2 -2
  174. package/dist/v2/{storage-tags-ss08kj5r.js → storage-tags-q71rnr9f.js} +3 -3
  175. package/dist/v2/store-reader.d.ts +2 -0
  176. package/dist/v2/store-reader.d.ts.map +1 -1
  177. package/dist/v2/{wrapup-orchestrator-639whfhq.js → wrapup-orchestrator-3ssd5yy1.js} +25 -25
  178. package/dist/wrapup-orchestrator-5vpcd1zx.js +49 -0
  179. package/package.json +2 -2
  180. package/src/config/index.test.ts +44 -0
  181. package/src/config/project-security.ts +15 -0
  182. package/src/config/rerank.test.ts +60 -0
  183. package/src/config/schema/magic-context.ts +57 -0
  184. package/src/shared/model-suggestion-retry.ts +1 -1
  185. package/src/shared/sqlite-turn-deadline.test.ts +270 -0
  186. package/src/shared/sqlite.ts +235 -7
  187. package/src/shared/tag-transcript.ts +9 -1
  188. package/dist/wrapup-orchestrator-mnvt2g8m.js +0 -49
@@ -1,9 +1,3 @@
1
- import {
2
- getHarness2
3
- } from "./server-pavzsdc6.js";
4
- import {
5
- prepareCachedStatement2
6
- } from "./server-1zx2pa5d.js";
7
1
  import {
8
2
  UNKNOWN_FIT_RATIO,
9
3
  resolveDecisionCalibration,
@@ -13,6 +7,7 @@ import {
13
7
  } from "./server-1zx87sdm.js";
14
8
  import {
15
9
  stripWellFormedLeadingTagPrefix,
10
+ stripPersistedAssistantText,
16
11
  stripTagPrefix,
17
12
  isRecord,
18
13
  estimateImageTokensFromDataUrl,
@@ -23,7 +18,7 @@ import {
23
18
  TAG_SELECT_COLUMNS2,
24
19
  getCandidateToolOwners2,
25
20
  pickNearestPriorOwner2
26
- } from "./server-a3dwxfgd.js";
21
+ } from "./server-fa7vtp40.js";
27
22
  import {
28
23
  OMO_INTERNAL_INITIATOR_MARKER,
29
24
  isSystemDirective,
@@ -42,17 +37,23 @@ import {
42
37
  compactTextForSummary,
43
38
  mergeCommitHashes
44
39
  } from "./server-3mz2qh5a.js";
40
+ import {
41
+ getHarness2
42
+ } from "./server-pavzsdc6.js";
45
43
  import {
46
44
  sessionLog2
47
45
  } from "./server-6495nmn9.js";
46
+ import {
47
+ prepareCachedStatement2
48
+ } from "./server-gzy5w8ya.js";
48
49
  import {
49
50
  stableStringify
50
- } from "./server-168pb207.js";
51
+ } from "./server-rqebawx1.js";
51
52
  import {
52
53
  renderToolTemplate,
53
54
  resolveModelConfigValue,
54
55
  deriveDefaultProtectedTokens
55
- } from "./server-271qxsf5.js";
56
+ } from "./server-24g8r2d0.js";
56
57
  import {
57
58
  readRawSessionMessagesFromDb2,
58
59
  readRawSessionMessagePageFromDb2,
@@ -69,18 +70,18 @@ import {
69
70
  readRawSeedTailFromDb2,
70
71
  readRawSessionBoundaryEndpointsFromDb2,
71
72
  prewarmRawSessionOrdinalsForDb
72
- } from "./server-55rgxfrw.js";
73
+ } from "./server-4kjmrjn4.js";
73
74
  import {
74
75
  openCodeDbExists,
75
76
  withReadOnlySessionDb,
76
77
  getRawSessionMessageCountFromDb,
77
78
  getMessageTimesFromOpenCodeDb
78
- } from "./server-brj8j2gk.js";
79
+ } from "./server-3vhxy2k5.js";
79
80
  import {
80
81
  getToolDefinitionMeasurementSignature,
81
82
  getLargestMeasuredToolDefinitionTokens,
82
83
  getMeasuredToolDefinitionTokens
83
- } from "./server-dj7tz4ph.js";
84
+ } from "./server-d1dzgvwd.js";
84
85
  import {
85
86
  canonicalModelIdentity,
86
87
  piModelRefToCanonical
@@ -88,1092 +89,537 @@ import {
88
89
  import {
89
90
  logSlowWriteTransaction
90
91
  } from "./server-gyy4k6mf.js";
92
+ // src/shared/historian-tool-defaults.json
93
+ var historian_tool_defaults_default = {
94
+ ctx_note: "Note ${input.action} ${input.note_ids}: ${input.content} → ${output.truncate(160)}",
95
+ ctx_memory: "Memory ${input.action} ${input.category} ${input.ids}: ${input.content} → ${output.truncate(160)}",
96
+ todowrite: 'Todos (${input.todos.count}): ${input.todos.each("${status}: ${content}")}',
97
+ question: 'Asked user: ${input.questions[*].question.join(" / ")} → ${output}',
98
+ task: "Task ${input.subagent_type}: ${input.prompt.truncate(600)} → ${output.truncate(200)}",
99
+ ask: 'Asked: ${input.question} [options: ${input.options.join(" / ")}] ${input.text}${input.resolution} → ${output.truncate(300)}',
100
+ peer_send: "PM ${input.recipient}: ${input.message}",
101
+ board: 'Board ${input.verb}${input.board_lane}: ${input.ops.each("${op} ${lane_label} ${item}${props.text}${lane.items} ${state}${props.state}${lane.status}${id}${title}")}',
102
+ room: "Room ${input.action} ${input.room_label}: ${input.room_detail}",
103
+ work: "Work ${input.action} ${input.title}${input.id}: ${input.prompt.truncate(600)}${input.notes.truncate(600)} → ${output.truncate(160)}",
104
+ knowhow: "Looked up how-to: ${input.query}${input.id}"
105
+ };
106
+ // src/shared/historian-tool-preview-defaults.json
107
+ var historian_tool_preview_defaults_default = {
108
+ ctx_note: "Note ${input.action} ${input.note_ids}: ${input.content} → ${output.truncate(160)}",
109
+ ctx_memory: "Memory ${input.action} ${input.category} ${input.ids}: ${input.content} → ${output.truncate(160)}",
110
+ todowrite: 'Todos (${input.todos.count}): ${input.todos.each("${status}: ${content}")}',
111
+ question: 'Asked user: ${input.questions[*].question.join(" / ")} → ${output}',
112
+ task: "Task ${input.subagent_type}: ${input.prompt} → ${output.truncate(200)}",
113
+ ask: 'Asked: ${input.question.truncate(250)} [options: ${input.options.join(" / ")}] ${input.resolution} → ${output.truncate(300)}',
114
+ peer_send: "PM to ${input.agent}${input.agent_id}: ${input.message.truncate(400)}",
115
+ board: 'Board ${input.verb}: ${input.ops.each("${op} ${item}${lane.title} ${state}${lane.status}${props.text} ${lane.items[*].text}")}',
116
+ room: "Room ${input.action} ${input.room_id}: ${input.text.truncate(400)}",
117
+ work: "Work ${input.action} ${input.id}: ${input.prompt_file}${input.prompt.truncate(200)}${input.notes.truncate(200)} → ${output.truncate(160)}",
118
+ knowhow: "Looked up how-to: ${input.query}${input.id}"
119
+ };
91
120
 
92
- // src/features/magic-context/overflow-detection.ts
93
- var OVERFLOW_PATTERNS = [
94
- /prompt is too long/i,
95
- /input is too long for requested model/i,
96
- /exceeds the context window/i,
97
- /input token count.*exceeds the maximum/i,
98
- /maximum prompt length is \d+/i,
99
- /reduce the length of the messages/i,
100
- /maximum context length is \d+ tokens/i,
101
- /maximum model length is \d+/i,
102
- /exceeds the limit of \d+/i,
103
- /exceeds the available context size/i,
104
- /greater than the context length/i,
105
- /context window exceeds limit/i,
106
- /exceeded model token limit/i,
107
- /context[_ ]length[_ ]exceeded/i,
108
- /request entity too large/i,
109
- /context length is only \d+ tokens/i,
110
- /input length.*exceeds.*context length/i,
111
- /prompt too long; exceeded (?:max )?context length/i,
112
- /too large for model with \d+ maximum context length/i,
113
- /model_context_window_exceeded/i,
114
- /context size has been exceeded/i,
115
- /prepared prompt exceeds engine max_context/i,
116
- /prompt exceeds (?:the )?.{0,32}\bmax_context\b/i
117
- ];
118
- var LIMIT_EXTRACTION_PATTERNS = [
119
- { pattern: /maximum prompt length is (\d+)/i, provenance: "prompt_only" },
120
- {
121
- pattern: /maximum context length is (\d+) tokens?/i,
122
- provenance: "combined"
123
- },
124
- { pattern: /maximum model length is (\d+)/i, provenance: "combined" },
125
- { pattern: /context length is only (\d+) tokens?/i, provenance: "combined" },
126
- { pattern: /exceeds the limit of (\d+)/i, provenance: "unknown" },
127
- {
128
- pattern: /too large for model with (\d+) maximum context length/i,
129
- provenance: "combined"
130
- },
131
- { pattern: /context size[^0-9]{0,40}(\d{4,})\s*tokens?/i, provenance: "combined" },
132
- { pattern: /exceeds? the context length of (\d+)/i, provenance: "combined" },
133
- {
134
- pattern: />\s*(\d+)\s*(?:tokens?\s*)?(?:maximum|max|limit)\b/i,
135
- provenance: "prompt_only"
136
- },
137
- {
138
- pattern: /maximum number of tokens allowed\s*\(?\s*(\d+)/i,
139
- provenance: "prompt_only"
140
- },
141
- { pattern: /prepared prompt exceeds engine max_context\s+(\d+)/i, provenance: "unknown" },
142
- { pattern: /prompt exceeds (?:the )?.{0,32}\bmax_context\s+(\d+)/i, provenance: "unknown" },
143
- { pattern: /max(?:imum)?.*context.*?(\d+)/i, provenance: "unknown" }
144
- ];
145
- var MIN_PLAUSIBLE_LIMIT = 1024;
146
- var MAX_PLAUSIBLE_LIMIT = 1e7;
147
- function extractErrorMessage(error) {
148
- if (!error)
149
- return "";
150
- if (typeof error === "string")
151
- return error;
152
- if (typeof error === "object") {
153
- const obj = error;
154
- const nested = obj.error;
155
- if (nested && typeof nested.message === "string" && nested.message.length > 0) {
156
- return nested.message;
157
- }
121
+ // src/shared/historian-tool-expansions.ts
122
+ function builtinInput(tool, input) {
123
+ if (!input || typeof input !== "object")
124
+ return input;
125
+ const i = input;
126
+ if (tool === "peer_send") {
127
+ const external = i.external;
128
+ const recipient = i.reply_to_pmid ? `reply to ${i.reply_to_pmid}` : i.agent || i.agent_id ? `to ${i.agent || i.agent_id}` : external?.session_id || i.session_id ? `to session ${external?.session_id || i.session_id}` : "to unknown recipient";
129
+ return { ...i, recipient };
158
130
  }
159
- if (error instanceof Error)
160
- return error.message;
161
- if (typeof error === "object") {
162
- const obj = error;
163
- if (typeof obj.message === "string")
164
- return obj.message;
165
- if (typeof obj.responseBody === "string")
166
- return obj.responseBody;
167
- try {
168
- return JSON.stringify(error);
169
- } catch {
170
- return String(error);
171
- }
131
+ if (tool === "room") {
132
+ return {
133
+ ...i,
134
+ room_label: i.room_id || i.title,
135
+ room_detail: i.action === "create" || i.action === "invite" ? `invitees: ${renderToolTemplate("${input.invitees}", i)}` : i.text
136
+ };
172
137
  }
173
- return String(error);
174
- }
175
- function extractExplicitHttpStatus(error) {
176
- if (!error || typeof error !== "object")
177
- return;
178
- const seen = new Set;
179
- const queue = [error];
180
- while (queue.length > 0) {
181
- const current = queue.shift();
182
- if (!current || seen.has(current))
183
- continue;
184
- seen.add(current);
185
- const record = current;
186
- for (const key of ["status", "statusCode", "status_code"]) {
187
- const value = record[key];
188
- if (typeof value === "number")
189
- return value;
190
- }
191
- for (const value of Object.values(record)) {
192
- if (value && typeof value === "object")
193
- queue.push(value);
194
- }
138
+ if (tool === "board") {
139
+ return {
140
+ ...i,
141
+ board_lane: i.lane ? ` ${i.lane}` : "",
142
+ ops: Array.isArray(i.ops) ? i.ops.map((op) => op && typeof op === "object" ? {
143
+ ...op,
144
+ lane_label: typeof op.lane === "string" ? op.lane : op.lane?.title || op.lane?.id
145
+ } : op) : i.ops
146
+ };
195
147
  }
196
- return;
148
+ return input;
197
149
  }
198
- function detectLatestTurnThinkingMismatch(error) {
199
- const status = extractExplicitHttpStatus(error);
200
- return (status === undefined || status === 400) && /(?:thinking|redacted_thinking)[\s\S]*latest assistant (?:message|turn)[\s\S]*cannot be modified/i.test(extractErrorMessage(error));
150
+ function expandToolPart(part, overrides, legacyPreview = false) {
151
+ if (!part || typeof part !== "object")
152
+ return null;
153
+ const p = part;
154
+ if (p.type !== "tool" || typeof p.tool !== "string")
155
+ return null;
156
+ const builtinDefaults = legacyPreview ? historian_tool_preview_defaults_default : historian_tool_defaults_default;
157
+ const overridden = overrides && Object.hasOwn(overrides, p.tool);
158
+ const template = overridden ? overrides[p.tool] : Object.hasOwn(builtinDefaults, p.tool) ? builtinDefaults[p.tool] : undefined;
159
+ if (typeof template !== "string")
160
+ return null;
161
+ const state = p.state;
162
+ const input = overridden || legacyPreview ? state?.input : builtinInput(p.tool, state?.input);
163
+ return renderToolTemplate(template, input, state?.output ?? state?.error, legacyPreview);
201
164
  }
202
- var PREFIX_BOUND_THINKING_MODELS = [
203
- ["fable", 5, 1],
204
- ["opus", 5, 5],
205
- ["sonnet", 5, 5],
206
- ["haiku", 5, 5]
207
- ];
208
- var PREFIX_BOUND_THINKING_PATTERN = new RegExp(`(?:^|[-_.:/])(?:${PREFIX_BOUND_THINKING_MODELS.map(([family, major, minor]) => `${family}[-_.]?${major}[-_.]${minor}`).join("|")})(?:$|[-_.:/@])`, "i");
209
- function isPrefixBoundThinkingModel(_providerID, modelID) {
210
- if (!modelID)
211
- return false;
212
- return PREFIX_BOUND_THINKING_PATTERN.test(modelID);
165
+
166
+ // src/hooks/magic-context/host-served-rows.ts
167
+ var HOST_UNSERVED_ROW = "hostUnservedRow";
168
+ function markHostUnservedRow(target) {
169
+ Object.defineProperty(target, HOST_UNSERVED_ROW, {
170
+ value: true,
171
+ enumerable: false,
172
+ configurable: true
173
+ });
174
+ return target;
213
175
  }
214
- function isFable51ThinkingBindingModel(providerID, modelID) {
215
- if (providerID?.toLowerCase() !== "anthropic" || !modelID)
216
- return false;
217
- return /(?:^|[-_.])fable[-_.]?5[-_.]1(?:$|[-_.])/i.test(modelID);
176
+ function isHostUnservedRow(value) {
177
+ return typeof value === "object" && value !== null && value[HOST_UNSERVED_ROW] === true;
218
178
  }
219
- function detectOverflow(error) {
220
- const message = extractErrorMessage(error);
221
- if (!message) {
222
- return { isOverflow: false };
223
- }
224
- const hasStatus413 = /\b413\b/.test(message) && /(entity|payload|context|prompt)/i.test(message);
225
- let matched;
226
- for (const pattern of OVERFLOW_PATTERNS) {
227
- if (pattern.test(message)) {
228
- matched = pattern;
179
+ function retreatPastHostUnservedRows(messages, exclusiveEnd, floor) {
180
+ if (!messages.some(isHostUnservedRow))
181
+ return exclusiveEnd;
182
+ const byOrdinal = new Map(messages.map((message) => [message.ordinal, message]));
183
+ let end = exclusiveEnd;
184
+ while (end - 1 >= floor && isHostUnservedRow(byOrdinal.get(end - 1)))
185
+ end -= 1;
186
+ return end;
187
+ }
188
+ function snapTerminalCompartmentToServedRow(compartments, lines) {
189
+ if (!lines.some(isHostUnservedRow))
190
+ return { compartments: [...compartments], snapped: false };
191
+ const byOrdinal = new Map(lines.map((line) => [line.ordinal, line]));
192
+ const result = [...compartments];
193
+ let snapped = false;
194
+ while (result.length > 0) {
195
+ const last = result[result.length - 1];
196
+ if (!isHostUnservedRow(byOrdinal.get(last.endMessage)))
229
197
  break;
198
+ snapped = true;
199
+ let served;
200
+ for (let ordinal = last.endMessage - 1;ordinal >= last.startMessage; ordinal -= 1) {
201
+ const line = byOrdinal.get(ordinal);
202
+ if (line && !isHostUnservedRow(line)) {
203
+ served = line;
204
+ break;
205
+ }
206
+ }
207
+ if (!served) {
208
+ result.pop();
209
+ continue;
230
210
  }
211
+ result[result.length - 1] = {
212
+ ...last,
213
+ endMessage: served.ordinal,
214
+ endMessageId: served.messageId
215
+ };
216
+ break;
231
217
  }
232
- if (!matched && !hasStatus413) {
233
- return { isOverflow: false };
234
- }
235
- const reportedLimit = parseReportedLimit(message);
236
- const reportedInputTokens = parseReportedInputTokens(message);
237
- return {
238
- isOverflow: true,
239
- reportedInputTokens,
240
- reportedLimit: reportedLimit?.value,
241
- reportedLimitProvenance: reportedLimit?.provenance,
242
- matchedPattern: matched?.source
243
- };
218
+ return { compartments: result, snapped };
244
219
  }
245
- function parseReportedInputTokens(message) {
246
- if (!message)
247
- return;
248
- const patterns = [
249
- /prompt is too long:\s*(\d+)/i,
250
- /input token count\s*\(?\s*(\d+)/i,
251
- /input length\s*(\d+)/i,
252
- /prompt was\s*(\d+)/i,
253
- /messages resulted in\s*(\d+)\s*tokens?/i
254
- ];
255
- for (const pattern of patterns) {
256
- const raw = message.match(pattern)?.[1];
257
- if (!raw)
258
- continue;
259
- const value = Number.parseInt(raw, 10);
260
- if (Number.isFinite(value) && value > 0)
261
- return value;
262
- }
263
- return;
220
+ function isHostRenderedSystemMessage(message) {
221
+ const id = message.info.id;
222
+ return (typeof id !== "string" || id.length === 0) && message.info.role === "system";
264
223
  }
265
- function parseReportedLimit(message) {
266
- if (!message)
267
- return;
268
- for (const { pattern, provenance } of LIMIT_EXTRACTION_PATTERNS) {
269
- const match = message.match(pattern);
270
- if (!match)
271
- continue;
272
- const raw = match[1];
273
- if (!raw)
274
- continue;
275
- const value = Number.parseInt(raw, 10);
276
- if (!Number.isFinite(value))
277
- continue;
278
- if (value < MIN_PLAUSIBLE_LIMIT || value > MAX_PLAUSIBLE_LIMIT)
279
- continue;
280
- return { value, provenance };
224
+ var COVERED_INSTRUCTION_ROW = "coveredInstructionRow";
225
+ function markCoveredInstructionRow(target) {
226
+ Object.defineProperty(target, COVERED_INSTRUCTION_ROW, {
227
+ value: true,
228
+ enumerable: false,
229
+ configurable: true
230
+ });
231
+ return target;
232
+ }
233
+ function isCoveredInstructionRow(value) {
234
+ return typeof value === "object" && value !== null && value[COVERED_INSTRUCTION_ROW] === true;
235
+ }
236
+ function takeCoveredInstructionRows(messages) {
237
+ const taken = messages.filter((message) => isCoveredInstructionRow(message.info));
238
+ if (taken.length > 0) {
239
+ const kept = messages.filter((message) => !isCoveredInstructionRow(message.info));
240
+ messages.splice(0, messages.length, ...kept);
281
241
  }
282
- return;
242
+ return taken;
243
+ }
244
+ function cloneKeepingCoveredInstructionMarks(messages) {
245
+ const copy = structuredClone(messages);
246
+ messages.forEach((message, index) => {
247
+ const copied = copy[index];
248
+ if (copied && isCoveredInstructionRow(message.info))
249
+ markCoveredInstructionRow(copied.info);
250
+ });
251
+ return copy;
283
252
  }
284
253
 
285
- // src/features/magic-context/storage-meta-shared.ts
286
- import { Buffer as Buffer2 } from "node:buffer";
287
- var SESSION_META_SELECT_COLUMNS = [
288
- "session_id",
289
- "last_response_time",
290
- "cache_ttl",
291
- "counter",
292
- "last_nudge_tokens",
293
- "last_nudge_band",
294
- "last_transform_error",
295
- "is_subagent",
296
- "last_context_percentage",
297
- "last_input_tokens",
298
- "observed_safe_input_tokens",
299
- "cache_alert_sent",
300
- "times_execute_threshold_reached",
301
- "compartment_in_progress",
302
- "system_prompt_hash",
303
- "system_prompt_tokens",
304
- "conversation_tokens",
305
- "tool_call_tokens",
306
- "cleared_reasoning_through_tag",
307
- "tool_reclaim_watermark",
308
- "last_todo_state",
309
- "cached_m0_bytes",
310
- "cached_m0_mural_data_url",
311
- "cached_m0_mural_hash",
312
- "cached_m1_bytes",
313
- "cached_m0_project_memory_epoch",
314
- "cached_m0_workspace_fingerprint",
315
- "cached_m0_project_user_profile_version",
316
- "cached_m0_max_compartment_seq",
317
- "cached_m0_max_memory_id",
318
- "cached_m0_max_mutation_id",
319
- "cached_m0_max_memory_mutation_id",
320
- "cached_m0_project_docs_hash",
321
- "cached_m0_materialized_at",
322
- "cached_m0_session_facts_version",
323
- "cached_m0_upgrade_state",
324
- "cached_m0_system_hash",
325
- "cached_m0_tool_set_hash",
326
- "cached_m0_model_key",
327
- "cached_m0_project_identity",
328
- "last_observed_model_key",
329
- "last_usage_context_limit",
330
- "prior_boundary_ordinal",
331
- "protected_tail_policy_version",
332
- "protected_tail_drain_window_started_at",
333
- "protected_tail_drain_tokens",
334
- "recovery_no_eligible_head_count",
335
- "force_emergency_bypass_window_start",
336
- "force_emergency_bypass_used",
337
- "emergency_drain_active",
338
- "historian_drain_failure_at",
339
- "upgrade_reminded_at",
340
- "upgrade_reminder_last_sent_at",
341
- "upgrade_reminder_count",
342
- "pi_stable_id_scheme"
343
- ];
344
- var META_COLUMNS = {
345
- lastResponseTime: "last_response_time",
346
- cacheTtl: "cache_ttl",
347
- counter: "counter",
348
- lastNudgeTokens: "last_nudge_tokens",
349
- lastNudgeBand: "last_nudge_band",
350
- lastTransformError: "last_transform_error",
351
- isSubagent: "is_subagent",
352
- lastContextPercentage: "last_context_percentage",
353
- lastInputTokens: "last_input_tokens",
354
- observedSafeInputTokens: "observed_safe_input_tokens",
355
- cacheAlertSent: "cache_alert_sent",
356
- timesExecuteThresholdReached: "times_execute_threshold_reached",
357
- compartmentInProgress: "compartment_in_progress",
358
- systemPromptHash: "system_prompt_hash",
359
- systemPromptTokens: "system_prompt_tokens",
360
- conversationTokens: "conversation_tokens",
361
- toolCallTokens: "tool_call_tokens",
362
- clearedReasoningThroughTag: "cleared_reasoning_through_tag",
363
- toolReclaimWatermark: "tool_reclaim_watermark",
364
- lastTodoState: "last_todo_state",
365
- cachedM0Bytes: "cached_m0_bytes",
366
- cachedM0MuralDataUrl: "cached_m0_mural_data_url",
367
- cachedM0MuralHash: "cached_m0_mural_hash",
368
- cachedM1Bytes: "cached_m1_bytes",
369
- cachedM0ProjectMemoryEpoch: "cached_m0_project_memory_epoch",
370
- cachedM0WorkspaceFingerprint: "cached_m0_workspace_fingerprint",
371
- cachedM0ProjectUserProfileVersion: "cached_m0_project_user_profile_version",
372
- cachedM0MaxCompartmentSeq: "cached_m0_max_compartment_seq",
373
- cachedM0MaxMemoryId: "cached_m0_max_memory_id",
374
- cachedM0MaxMutationId: "cached_m0_max_mutation_id",
375
- cachedM0MaxMemoryMutationId: "cached_m0_max_memory_mutation_id",
376
- cachedM0ProjectDocsHash: "cached_m0_project_docs_hash",
377
- cachedM0MaterializedAt: "cached_m0_materialized_at",
378
- cachedM0SessionFactsVersion: "cached_m0_session_facts_version",
379
- cachedM0UpgradeState: "cached_m0_upgrade_state",
380
- cachedM0SystemHash: "cached_m0_system_hash",
381
- cachedM0ToolSetHash: "cached_m0_tool_set_hash",
382
- cachedM0ModelKey: "cached_m0_model_key",
383
- cachedM0ProjectIdentity: "cached_m0_project_identity",
384
- lastObservedModelKey: "last_observed_model_key",
385
- lastUsageContextLimit: "last_usage_context_limit",
386
- priorBoundaryOrdinal: "prior_boundary_ordinal",
387
- protectedTailPolicyVersion: "protected_tail_policy_version",
388
- protectedTailDrainWindowStartedAt: "protected_tail_drain_window_started_at",
389
- protectedTailDrainTokens: "protected_tail_drain_tokens",
390
- recoveryNoEligibleHeadCount: "recovery_no_eligible_head_count",
391
- forceEmergencyBypassWindowStart: "force_emergency_bypass_window_start",
392
- forceEmergencyBypassUsed: "force_emergency_bypass_used",
393
- emergencyDrainActive: "emergency_drain_active",
394
- historianDrainFailureAt: "historian_drain_failure_at",
395
- upgradeRemindedAt: "upgrade_reminded_at",
396
- upgradeReminderLastSentAt: "upgrade_reminder_last_sent_at",
397
- upgradeReminderCount: "upgrade_reminder_count",
398
- piStableIdScheme: "pi_stable_id_scheme"
399
- };
400
- var BOOLEAN_META_KEYS = new Set(["isSubagent", "compartmentInProgress", "cacheAlertSent"]);
401
- var NULL_BIND_META_KEYS = new Set([
402
- "cachedM0Bytes",
403
- "cachedM0MuralDataUrl",
404
- "cachedM0MuralHash",
405
- "cachedM1Bytes",
406
- "cachedM0ProjectMemoryEpoch",
407
- "cachedM0WorkspaceFingerprint",
408
- "cachedM0ProjectUserProfileVersion",
409
- "cachedM0MaxCompartmentSeq",
410
- "cachedM0MaxMemoryId",
411
- "cachedM0MaxMutationId",
412
- "cachedM0MaxMemoryMutationId",
413
- "cachedM0ProjectDocsHash",
414
- "cachedM0MaterializedAt",
415
- "cachedM0SessionFactsVersion",
416
- "cachedM0UpgradeState",
417
- "cachedM0ProjectIdentity",
418
- "lastObservedModelKey",
419
- "upgradeRemindedAt",
420
- "upgradeReminderLastSentAt",
421
- "piStableIdScheme"
422
- ]);
423
- function isStringOrNull(value) {
424
- return value === null || typeof value === "string";
254
+ // src/hooks/magic-context/read-session-true-raw-tokens.ts
255
+ function completedToolArcCrossesBoundary(invOrdinal, resOrdinal, boundary) {
256
+ return invOrdinal < boundary && boundary <= resOrdinal;
425
257
  }
426
- function isNumberOrNull(value) {
427
- return value === null || typeof value === "number";
258
+ var MAX_MESSAGE_CACHE_ENTRIES = 1e5;
259
+ var MAX_MESSAGE_CACHE_KEY_BYTES = 64 * 1024 * 1024;
260
+ var FNV1A_32_OFFSET = 2166136261;
261
+ var FNV1A_32_PRIME = 16777619;
262
+ var messageEstimateCache = new Map;
263
+ var messageEstimateKeysByField = new Map;
264
+ var messageEstimateCacheBytes = 0;
265
+ var EMPTY_BREAKDOWN = {
266
+ text: 0,
267
+ reasoning: 0,
268
+ toolInput: 0,
269
+ toolOutput: 0,
270
+ image: 0,
271
+ other: 0,
272
+ total: 0
273
+ };
274
+ function addBreakdown(target, kind, value) {
275
+ if (kind === "total")
276
+ return;
277
+ const safeValue = Number.isFinite(value) && value > 0 ? Math.round(value) : 0;
278
+ target[kind] += safeValue;
279
+ target.total += safeValue;
428
280
  }
429
- function isBlobOrNull(value) {
430
- return value === null || Buffer2.isBuffer(value) || value instanceof Uint8Array;
281
+ function estimateStructured(value) {
282
+ if (typeof value === "string")
283
+ return estimateTokens(value);
284
+ if (value === undefined || value === null)
285
+ return 0;
286
+ return estimateTokens(stableStringify(value));
431
287
  }
432
- function toBufferOrNull(value) {
433
- if (value === null)
434
- return null;
435
- if (Buffer2.isBuffer(value))
288
+ function firstStringField(record, fields) {
289
+ for (const field of fields) {
290
+ const value = record[field];
291
+ if (typeof value === "string" && value.length > 0)
292
+ return value;
293
+ }
294
+ return null;
295
+ }
296
+ function stringValue(value) {
297
+ if (typeof value === "string")
436
298
  return value;
437
- return Buffer2.from(value.buffer, value.byteOffset, value.byteLength);
299
+ if (value === undefined || value === null)
300
+ return "";
301
+ return stableStringify(value);
438
302
  }
439
- function isSessionMetaRow(row) {
440
- if (row === null || typeof row !== "object")
441
- return false;
442
- const r = row;
443
- return typeof r.session_id === "string" && isNumberOrNull(r.last_response_time) && isStringOrNull(r.cache_ttl) && typeof r.counter === "number" && typeof r.last_nudge_tokens === "number" && isStringOrNull(r.last_nudge_band) && isStringOrNull(r.last_transform_error) && isNumberOrNull(r.is_subagent) && typeof r.last_context_percentage === "number" && typeof r.last_input_tokens === "number" && isNumberOrNull(r.observed_safe_input_tokens) && isNumberOrNull(r.cache_alert_sent) && isNumberOrNull(r.times_execute_threshold_reached) && isNumberOrNull(r.compartment_in_progress) && (r.system_prompt_hash === null || typeof r.system_prompt_hash === "string" || typeof r.system_prompt_hash === "number") && isNumberOrNull(r.system_prompt_tokens) && isNumberOrNull(r.conversation_tokens) && isNumberOrNull(r.tool_call_tokens) && isNumberOrNull(r.cleared_reasoning_through_tag) && isStringOrNull(r.last_todo_state) && isBlobOrNull(r.cached_m0_bytes) && isStringOrNull(r.cached_m0_mural_data_url) && isStringOrNull(r.cached_m0_mural_hash) && isBlobOrNull(r.cached_m1_bytes) && isNumberOrNull(r.cached_m0_project_memory_epoch) && isStringOrNull(r.cached_m0_workspace_fingerprint) && isNumberOrNull(r.cached_m0_project_user_profile_version) && isNumberOrNull(r.cached_m0_max_compartment_seq) && isNumberOrNull(r.cached_m0_max_memory_id) && isNumberOrNull(r.cached_m0_max_mutation_id) && isNumberOrNull(r.cached_m0_max_memory_mutation_id) && isStringOrNull(r.cached_m0_project_docs_hash) && isNumberOrNull(r.cached_m0_materialized_at) && isNumberOrNull(r.cached_m0_session_facts_version) && isStringOrNull(r.cached_m0_upgrade_state) && isStringOrNull(r.cached_m0_system_hash) && isStringOrNull(r.cached_m0_tool_set_hash) && isStringOrNull(r.cached_m0_model_key) && isStringOrNull(r.cached_m0_project_identity) && isStringOrNull(r.last_observed_model_key) && isNumberOrNull(r.last_usage_context_limit) && isNumberOrNull(r.prior_boundary_ordinal) && isNumberOrNull(r.protected_tail_policy_version) && isNumberOrNull(r.protected_tail_drain_window_started_at) && isNumberOrNull(r.protected_tail_drain_tokens) && isNumberOrNull(r.recovery_no_eligible_head_count) && isNumberOrNull(r.force_emergency_bypass_window_start) && isNumberOrNull(r.force_emergency_bypass_used) && isNumberOrNull(r.upgrade_reminded_at) && isNumberOrNull(r.upgrade_reminder_last_sent_at) && isNumberOrNull(r.upgrade_reminder_count) && isNumberOrNull(r.pi_stable_id_scheme) && isNumberOrNull(r.tool_reclaim_watermark);
303
+ function textFromToolResultContent(content) {
304
+ if (typeof content === "string")
305
+ return content;
306
+ if (Array.isArray(content)) {
307
+ const pieces = [];
308
+ for (const entry of content) {
309
+ if (typeof entry === "string") {
310
+ pieces.push(entry);
311
+ } else if (isRecord(entry)) {
312
+ const text = firstStringField(entry, ["text", "content", "value"]);
313
+ pieces.push(text ?? stableStringify(entry));
314
+ } else if (entry !== null && entry !== undefined) {
315
+ pieces.push(String(entry));
316
+ }
317
+ }
318
+ return pieces.join(`
319
+ `);
320
+ }
321
+ return stringValue(content);
444
322
  }
445
- function getDefaultSessionMeta(sessionId) {
446
- return {
447
- sessionId,
448
- lastResponseTime: 0,
449
- cacheTtl: "5m",
450
- counter: 0,
451
- lastNudgeTokens: 0,
452
- lastNudgeBand: null,
453
- lastTransformError: null,
454
- isSubagent: false,
455
- lastContextPercentage: 0,
456
- lastInputTokens: 0,
457
- observedSafeInputTokens: 0,
458
- cacheAlertSent: false,
459
- timesExecuteThresholdReached: 0,
460
- compartmentInProgress: false,
461
- systemPromptHash: "",
462
- systemPromptTokens: 0,
463
- conversationTokens: 0,
464
- toolCallTokens: 0,
465
- clearedReasoningThroughTag: 0,
466
- toolReclaimWatermark: 0,
467
- lastTodoState: "",
468
- cachedM0Bytes: null,
469
- cachedM0MuralDataUrl: null,
470
- cachedM0MuralHash: null,
471
- cachedM1Bytes: null,
472
- cachedM0ProjectMemoryEpoch: null,
473
- cachedM0WorkspaceFingerprint: null,
474
- cachedM0ProjectUserProfileVersion: null,
475
- cachedM0MaxCompartmentSeq: null,
476
- cachedM0MaxMemoryId: null,
477
- cachedM0MaxMutationId: null,
478
- cachedM0MaxMemoryMutationId: null,
479
- cachedM0ProjectDocsHash: null,
480
- cachedM0MaterializedAt: null,
481
- cachedM0SessionFactsVersion: null,
482
- cachedM0UpgradeState: null,
483
- cachedM0SystemHash: null,
484
- cachedM0ToolSetHash: null,
485
- cachedM0ModelKey: null,
486
- cachedM0ProjectIdentity: null,
487
- lastObservedModelKey: null,
488
- lastUsageContextLimit: 0,
489
- priorBoundaryOrdinal: 1,
490
- protectedTailPolicyVersion: 0,
491
- protectedTailDrainWindowStartedAt: 0,
492
- protectedTailDrainTokens: 0,
493
- recoveryNoEligibleHeadCount: 0,
494
- forceEmergencyBypassWindowStart: 0,
495
- forceEmergencyBypassUsed: 0,
496
- upgradeRemindedAt: null,
497
- upgradeReminderLastSentAt: null,
498
- upgradeReminderCount: 0,
499
- piStableIdScheme: null
500
- };
323
+ function looksImageLike(part) {
324
+ const type = typeof part.type === "string" ? part.type.toLowerCase() : "";
325
+ const mime = typeof part.mime === "string" ? part.mime.toLowerCase() : "";
326
+ const mediaType = typeof part.mediaType === "string" ? part.mediaType.toLowerCase() : "";
327
+ return type.includes("image") || mime.startsWith("image/") || mediaType.startsWith("image/") || part.image_url !== undefined || part.imageUrl !== undefined || part.image !== undefined;
501
328
  }
502
- var sessionMetaExistsStatements = new WeakMap;
503
- function ensureSessionMetaRow(db, sessionId, initialIsSubagent = getHarness2() === "opencode2" ? null : false) {
504
- let exists = sessionMetaExistsStatements.get(db);
505
- if (!exists) {
506
- exists = prepareCachedStatement2(db, "SELECT 1 FROM session_meta WHERE session_id = ?");
507
- sessionMetaExistsStatements.set(db, exists);
329
+ function defaultImageTokenHeuristic(part) {
330
+ if (isRecord(part)) {
331
+ const width = part.width;
332
+ const height = part.height;
333
+ if (typeof width === "number" && typeof height === "number" && width > 0 && height > 0) {
334
+ return Math.max(256, Math.min(4096, Math.ceil(width * height / 750)));
335
+ }
508
336
  }
509
- if (exists.get(sessionId))
510
- return;
511
- const defaults = getDefaultSessionMeta(sessionId);
512
- db.prepare("INSERT OR IGNORE INTO session_meta (session_id, harness, last_response_time, cache_ttl, counter, last_nudge_tokens, last_nudge_band, last_transform_error, is_subagent, last_context_percentage, last_input_tokens, observed_safe_input_tokens, cache_alert_sent, times_execute_threshold_reached, compartment_in_progress, system_prompt_hash, cleared_reasoning_through_tag) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").run(sessionId, getHarness2(), defaults.lastResponseTime, defaults.cacheTtl, defaults.counter, defaults.lastNudgeTokens, defaults.lastNudgeBand ?? "", defaults.lastTransformError ?? "", initialIsSubagent === null ? null : initialIsSubagent ? 1 : 0, defaults.lastContextPercentage, defaults.lastInputTokens, defaults.observedSafeInputTokens, defaults.cacheAlertSent ? 1 : 0, defaults.timesExecuteThresholdReached, defaults.compartmentInProgress ? 1 : 0, defaults.systemPromptHash ?? "", defaults.clearedReasoningThroughTag);
337
+ return 1024;
513
338
  }
514
- function toSessionMeta(row) {
515
- const nudgeBandRaw = typeof row.last_nudge_band === "string" ? row.last_nudge_band : "";
516
- const transformErrorRaw = typeof row.last_transform_error === "string" ? row.last_transform_error : "";
517
- const cacheTtlRaw = typeof row.cache_ttl === "string" && row.cache_ttl.length > 0 ? row.cache_ttl : "5m";
518
- const systemPromptHashRaw = row.system_prompt_hash == null ? "" : row.system_prompt_hash;
519
- const lastTodoStateRaw = typeof row.last_todo_state === "string" ? row.last_todo_state : "";
520
- const numOrZero = (value) => typeof value === "number" ? value : 0;
521
- const numOrNull = (value) => typeof value === "number" ? value : null;
522
- const stringOrNull = (value) => typeof value === "string" ? value : null;
523
- return {
524
- sessionId: row.session_id,
525
- lastResponseTime: numOrZero(row.last_response_time),
526
- cacheTtl: cacheTtlRaw,
527
- counter: row.counter,
528
- lastNudgeTokens: row.last_nudge_tokens,
529
- lastNudgeBand: nudgeBandRaw.length > 0 ? nudgeBandRaw : null,
530
- lastTransformError: transformErrorRaw.length > 0 ? transformErrorRaw : null,
531
- isSubagent: row.is_subagent === 1,
532
- lastContextPercentage: row.last_context_percentage,
533
- lastInputTokens: row.last_input_tokens,
534
- observedSafeInputTokens: numOrZero(row.observed_safe_input_tokens),
535
- cacheAlertSent: numOrZero(row.cache_alert_sent) === 1,
536
- timesExecuteThresholdReached: numOrZero(row.times_execute_threshold_reached),
537
- compartmentInProgress: row.compartment_in_progress === 1,
538
- systemPromptHash: String(systemPromptHashRaw),
539
- systemPromptTokens: numOrZero(row.system_prompt_tokens),
540
- conversationTokens: numOrZero(row.conversation_tokens),
541
- toolCallTokens: numOrZero(row.tool_call_tokens),
542
- clearedReasoningThroughTag: numOrZero(row.cleared_reasoning_through_tag),
543
- toolReclaimWatermark: numOrZero(row.tool_reclaim_watermark),
544
- lastTodoState: lastTodoStateRaw,
545
- cachedM0Bytes: toBufferOrNull(row.cached_m0_bytes),
546
- cachedM0MuralDataUrl: stringOrNull(row.cached_m0_mural_data_url),
547
- cachedM0MuralHash: stringOrNull(row.cached_m0_mural_hash),
548
- cachedM1Bytes: toBufferOrNull(row.cached_m1_bytes),
549
- cachedM0ProjectMemoryEpoch: numOrNull(row.cached_m0_project_memory_epoch),
550
- cachedM0WorkspaceFingerprint: stringOrNull(row.cached_m0_workspace_fingerprint),
551
- cachedM0ProjectUserProfileVersion: numOrNull(row.cached_m0_project_user_profile_version),
552
- cachedM0MaxCompartmentSeq: numOrNull(row.cached_m0_max_compartment_seq),
553
- cachedM0MaxMemoryId: numOrNull(row.cached_m0_max_memory_id),
554
- cachedM0MaxMutationId: numOrNull(row.cached_m0_max_mutation_id),
555
- cachedM0MaxMemoryMutationId: numOrNull(row.cached_m0_max_memory_mutation_id),
556
- cachedM0ProjectDocsHash: stringOrNull(row.cached_m0_project_docs_hash),
557
- cachedM0MaterializedAt: numOrNull(row.cached_m0_materialized_at),
558
- cachedM0SessionFactsVersion: numOrNull(row.cached_m0_session_facts_version),
559
- cachedM0UpgradeState: stringOrNull(row.cached_m0_upgrade_state),
560
- cachedM0SystemHash: stringOrNull(row.cached_m0_system_hash),
561
- cachedM0ToolSetHash: stringOrNull(row.cached_m0_tool_set_hash),
562
- cachedM0ModelKey: stringOrNull(row.cached_m0_model_key),
563
- cachedM0ProjectIdentity: stringOrNull(row.cached_m0_project_identity),
564
- lastObservedModelKey: stringOrNull(row.last_observed_model_key),
565
- lastUsageContextLimit: numOrZero(row.last_usage_context_limit),
566
- priorBoundaryOrdinal: Math.max(1, numOrZero(row.prior_boundary_ordinal) || 1),
567
- protectedTailPolicyVersion: numOrZero(row.protected_tail_policy_version),
568
- protectedTailDrainWindowStartedAt: numOrZero(row.protected_tail_drain_window_started_at),
569
- protectedTailDrainTokens: numOrZero(row.protected_tail_drain_tokens),
570
- recoveryNoEligibleHeadCount: numOrZero(row.recovery_no_eligible_head_count),
571
- forceEmergencyBypassWindowStart: numOrZero(row.force_emergency_bypass_window_start),
572
- forceEmergencyBypassUsed: numOrZero(row.force_emergency_bypass_used),
573
- upgradeRemindedAt: numOrNull(row.upgrade_reminded_at),
574
- upgradeReminderLastSentAt: numOrNull(row.upgrade_reminder_last_sent_at),
575
- upgradeReminderCount: numOrZero(row.upgrade_reminder_count),
576
- piStableIdScheme: numOrNull(row.pi_stable_id_scheme)
577
- };
339
+ function partType(part) {
340
+ return typeof part.type === "string" ? part.type : "";
578
341
  }
579
- function persistCachedM0(db, sessionId, payload) {
580
- ensureSessionMetaRow(db, sessionId);
581
- db.prepare(`UPDATE session_meta SET
582
- cached_m0_bytes = ?,
583
- cached_m0_mural_data_url = ?,
584
- cached_m0_mural_hash = ?,
585
- cached_m0_project_memory_epoch = ?,
586
- cached_m0_workspace_fingerprint = ?,
587
- cached_m0_project_user_profile_version = ?,
588
- cached_m0_max_compartment_seq = ?,
589
- cached_m0_max_memory_id = ?,
590
- cached_m0_max_mutation_id = ?,
591
- cached_m0_max_memory_mutation_id = ?,
592
- cached_m1_bytes = ?,
593
- cached_m0_project_docs_hash = ?,
594
- cached_m0_materialized_at = ?,
595
- cached_m0_session_facts_version = ?,
596
- cached_m0_upgrade_state = ?,
597
- cached_m0_system_hash = ?,
598
- cached_m0_tool_set_hash = ?,
599
- cached_m0_model_key = ?,
600
- cached_m0_project_identity = ?
601
- WHERE session_id = ?`).run(Buffer2.from(payload.m0Bytes), payload.muralDataUrl ?? null, payload.muralHash ?? null, payload.projectMemoryEpoch, payload.workspaceFingerprint ?? null, payload.projectUserProfileVersion, payload.maxCompartmentSeq, payload.maxMemoryId, payload.maxMutationId, payload.maxMemoryMutationId ?? null, payload.m1Bytes ? Buffer2.from(payload.m1Bytes) : null, payload.projectDocsHash, payload.materializedAt, payload.sessionFactsVersion, payload.upgradeState, payload.systemHash ?? "", payload.toolSetHash ?? "", payload.modelKey ? piModelRefToCanonical(payload.modelKey) : "", payload.projectIdentity ?? null, sessionId);
342
+ function hasOwn(record, key) {
343
+ return Object.hasOwn(record, key);
602
344
  }
603
- function clearCachedM0M1(db, sessionId) {
604
- ensureSessionMetaRow(db, sessionId);
605
- const existingColumns = new Set(db.prepare("PRAGMA table_info(session_meta)").all().map((column) => column.name));
606
- const clears = [
607
- ["cached_m0_bytes", null],
608
- ["cached_m0_mural_data_url", null],
609
- ["cached_m0_mural_hash", null],
610
- ["cached_m1_bytes", null],
611
- ["cached_m0_project_memory_epoch", null],
612
- ["cached_m0_workspace_fingerprint", null],
613
- ["cached_m0_project_user_profile_version", null],
614
- ["cached_m0_max_compartment_seq", null],
615
- ["cached_m0_max_memory_id", null],
616
- ["cached_m0_max_mutation_id", null],
617
- ["cached_m0_max_memory_mutation_id", null],
618
- ["cached_m0_project_docs_hash", null],
619
- ["cached_m0_materialized_at", null],
620
- ["cached_m0_session_facts_version", null],
621
- ["cached_m0_upgrade_state", null],
622
- ["cached_m0_system_hash", null],
623
- ["cached_m0_tool_set_hash", null],
624
- ["cached_m0_model_key", null],
625
- ["cached_m0_project_identity", null],
626
- ["cached_m0_last_baseline_end_message_id", null],
627
- ["memory_block_cache", ""],
628
- ["memory_block_count", 0],
629
- ["memory_block_ids", ""]
630
- ];
631
- const setClauses = [];
632
- const values = [];
633
- for (const [column, value] of clears) {
634
- if (!existingColumns.has(column))
635
- continue;
636
- setClauses.push(`${column} = ?`);
637
- values.push(value);
345
+ function recursiveByteLength(value) {
346
+ if (value === null || value === undefined)
347
+ return 0;
348
+ if (typeof value === "string")
349
+ return value.length;
350
+ if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
351
+ return String(value).length;
638
352
  }
639
- if (setClauses.length === 0)
640
- return;
641
- db.prepare(`UPDATE session_meta SET ${setClauses.join(", ")} WHERE session_id = ?`).run(...values, sessionId);
642
- }
643
- function clearCachedM0(db, sessionId) {
644
- clearCachedM0M1(db, sessionId);
353
+ if (Array.isArray(value)) {
354
+ return value.reduce((sum, item) => sum + recursiveByteLength(item), value.length);
355
+ }
356
+ if (isRecord(value)) {
357
+ let total = Object.keys(value).length;
358
+ for (const [key, child] of Object.entries(value)) {
359
+ total += key.length + recursiveByteLength(child);
360
+ }
361
+ return total;
362
+ }
363
+ return String(value).length;
645
364
  }
646
- // src/shared/historian-tool-defaults.json
647
- var historian_tool_defaults_default = {
648
- ctx_note: "Note ${input.action} ${input.note_ids}: ${input.content} → ${output.truncate(160)}",
649
- ctx_memory: "Memory ${input.action} ${input.category} ${input.ids}: ${input.content} → ${output.truncate(160)}",
650
- todowrite: 'Todos (${input.todos.count}): ${input.todos.each("${status}: ${content}")}',
651
- question: 'Asked user: ${input.questions[*].question.join(" / ")} → ${output}',
652
- task: "Task ${input.subagent_type}: ${input.prompt.truncate(600)} → ${output.truncate(200)}",
653
- ask: 'Asked: ${input.question} [options: ${input.options.join(" / ")}] ${input.text}${input.resolution} → ${output.truncate(300)}',
654
- peer_send: "PM ${input.recipient}: ${input.message}",
655
- board: 'Board ${input.verb}${input.board_lane}: ${input.ops.each("${op} ${lane_label} ${item}${props.text}${lane.items} ${state}${props.state}${lane.status}${id}${title}")}',
656
- room: "Room ${input.action} ${input.room_label}: ${input.room_detail}",
657
- work: "Work ${input.action} ${input.title}${input.id}: ${input.prompt.truncate(600)}${input.notes.truncate(600)} → ${output.truncate(160)}",
658
- knowhow: "Looked up how-to: ${input.query}${input.id}"
659
- };
660
- // src/shared/historian-tool-preview-defaults.json
661
- var historian_tool_preview_defaults_default = {
662
- ctx_note: "Note ${input.action} ${input.note_ids}: ${input.content} → ${output.truncate(160)}",
663
- ctx_memory: "Memory ${input.action} ${input.category} ${input.ids}: ${input.content} → ${output.truncate(160)}",
664
- todowrite: 'Todos (${input.todos.count}): ${input.todos.each("${status}: ${content}")}',
665
- question: 'Asked user: ${input.questions[*].question.join(" / ")} → ${output}',
666
- task: "Task ${input.subagent_type}: ${input.prompt} → ${output.truncate(200)}",
667
- ask: 'Asked: ${input.question.truncate(250)} [options: ${input.options.join(" / ")}] ${input.resolution} → ${output.truncate(300)}',
668
- peer_send: "PM to ${input.agent}${input.agent_id}: ${input.message.truncate(400)}",
669
- board: 'Board ${input.verb}: ${input.ops.each("${op} ${item}${lane.title} ${state}${lane.status}${props.text} ${lane.items[*].text}")}',
670
- room: "Room ${input.action} ${input.room_id}: ${input.text.truncate(400)}",
671
- work: "Work ${input.action} ${input.id}: ${input.prompt_file}${input.prompt.truncate(200)}${input.notes.truncate(200)} → ${output.truncate(160)}",
672
- knowhow: "Looked up how-to: ${input.query}${input.id}"
673
- };
674
-
675
- // src/shared/historian-tool-expansions.ts
676
- function builtinInput(tool, input) {
677
- if (!input || typeof input !== "object")
678
- return input;
679
- const i = input;
680
- if (tool === "peer_send") {
681
- const external = i.external;
682
- const recipient = i.reply_to_pmid ? `reply to ${i.reply_to_pmid}` : i.agent || i.agent_id ? `to ${i.agent || i.agent_id}` : external?.session_id || i.session_id ? `to session ${external?.session_id || i.session_id}` : "to unknown recipient";
683
- return { ...i, recipient };
365
+ function updateFnv1a32(hash, text) {
366
+ let next = hash;
367
+ for (let index = 0;index < text.length; index += 1) {
368
+ next ^= text.charCodeAt(index);
369
+ next = Math.imul(next, FNV1A_32_PRIME) >>> 0;
684
370
  }
685
- if (tool === "room") {
371
+ return next;
372
+ }
373
+ function contentStringsHash(fields) {
374
+ let hash = FNV1A_32_OFFSET;
375
+ for (const field of fields) {
376
+ hash = updateFnv1a32(hash, `${field.length}:`);
377
+ hash = updateFnv1a32(hash, field);
378
+ hash = updateFnv1a32(hash, "\x00");
379
+ }
380
+ return hash.toString(16).padStart(8, "0");
381
+ }
382
+ function rawPartVersion(part) {
383
+ return part.__magicContextPartUpdatedAt ?? part.updated_at ?? part.updatedAt ?? part.version ?? part.revision ?? "";
384
+ }
385
+ function callIdFromPart(part) {
386
+ const direct = firstStringField(part, ["callID", "callId", "toolCallId", "tool_call_id", "id"]);
387
+ if (direct)
388
+ return direct;
389
+ const state = isRecord(part.state) ? part.state : null;
390
+ return state ? firstStringField(state, ["callID", "callId", "toolCallId", "tool_call_id", "id"]) ?? "" : "";
391
+ }
392
+ function toolSignalFromPart(part, withText = true) {
393
+ if (!isRecord(part))
394
+ return null;
395
+ const type = partType(part);
396
+ const state = isRecord(part.state) ? part.state : null;
397
+ const callId = callIdFromPart(part);
398
+ if (!callId && type !== "tool")
399
+ return null;
400
+ if (type === "tool") {
401
+ const hasInput = state !== null && hasOwn(state, "input");
402
+ const outputKey = state ? hasOwn(state, "output") ? "output" : hasOwn(state, "error") ? "error" : hasOwn(state, "result") ? "result" : null : null;
403
+ const hasOutput = outputKey !== null;
404
+ const outputValue = outputKey && state ? state[outputKey] : undefined;
405
+ const providerExecuted = part.providerExecuted === true;
406
+ const openInvocation = !providerExecuted && !hasOutput;
686
407
  return {
687
- ...i,
688
- room_label: i.room_id || i.title,
689
- room_detail: i.action === "create" || i.action === "invite" ? `invitees: ${renderToolTemplate("${input.invitees}", i)}` : i.text
408
+ callId,
409
+ hasInput: hasInput || openInvocation,
410
+ hasOutput,
411
+ inputText: withText && hasInput && state ? stringValue(state.input) : "",
412
+ outputText: withText && hasOutput ? stringValue(outputValue) : ""
690
413
  };
691
414
  }
692
- if (tool === "board") {
415
+ if (type === "tool-invocation") {
416
+ const args = part.args ?? part.input;
693
417
  return {
694
- ...i,
695
- board_lane: i.lane ? ` ${i.lane}` : "",
696
- ops: Array.isArray(i.ops) ? i.ops.map((op) => op && typeof op === "object" ? {
697
- ...op,
698
- lane_label: typeof op.lane === "string" ? op.lane : op.lane?.title || op.lane?.id
699
- } : op) : i.ops
418
+ callId,
419
+ hasInput: args !== undefined,
420
+ hasOutput: false,
421
+ inputText: withText && args !== undefined ? stringValue(args) : "",
422
+ outputText: ""
700
423
  };
701
424
  }
702
- return input;
703
- }
704
- function expandToolPart(part, overrides, legacyPreview = false) {
705
- if (!part || typeof part !== "object")
706
- return null;
707
- const p = part;
708
- if (p.type !== "tool" || typeof p.tool !== "string")
709
- return null;
710
- const builtinDefaults = legacyPreview ? historian_tool_preview_defaults_default : historian_tool_defaults_default;
711
- const overridden = overrides && Object.hasOwn(overrides, p.tool);
712
- const template = overridden ? overrides[p.tool] : Object.hasOwn(builtinDefaults, p.tool) ? builtinDefaults[p.tool] : undefined;
713
- if (typeof template !== "string")
714
- return null;
715
- const state = p.state;
716
- const input = overridden || legacyPreview ? state?.input : builtinInput(p.tool, state?.input);
717
- return renderToolTemplate(template, input, state?.output ?? state?.error, legacyPreview);
718
- }
719
-
720
- // src/hooks/magic-context/host-served-rows.ts
721
- var HOST_UNSERVED_ROW = "hostUnservedRow";
722
- function markHostUnservedRow(target) {
723
- Object.defineProperty(target, HOST_UNSERVED_ROW, {
724
- value: true,
725
- enumerable: false,
726
- configurable: true
727
- });
728
- return target;
425
+ if (type === "tool_use") {
426
+ const input = part.input;
427
+ return {
428
+ callId,
429
+ hasInput: input !== undefined,
430
+ hasOutput: false,
431
+ inputText: withText && input !== undefined ? stringValue(input) : "",
432
+ outputText: ""
433
+ };
434
+ }
435
+ if (type === "tool_result") {
436
+ const content = part.content ?? part.output ?? part.result;
437
+ return {
438
+ callId,
439
+ hasInput: false,
440
+ hasOutput: content !== undefined,
441
+ inputText: "",
442
+ outputText: withText && content !== undefined ? textFromToolResultContent(content) : ""
443
+ };
444
+ }
445
+ return null;
729
446
  }
730
- function isHostUnservedRow(value) {
731
- return typeof value === "object" && value !== null && value[HOST_UNSERVED_ROW] === true;
447
+ function partCheapFingerprint(part) {
448
+ if (!isRecord(part))
449
+ return `${typeof part}:${recursiveByteLength(part)}`;
450
+ const version = rawPartVersion(part);
451
+ const type = typeof part.type === "string" ? part.type : "";
452
+ return `${type}:${String(version)}:${recursiveByteLength(providerVisiblePart(part))}`;
732
453
  }
733
- function retreatPastHostUnservedRows(messages, exclusiveEnd, floor) {
734
- if (!messages.some(isHostUnservedRow))
735
- return exclusiveEnd;
736
- const byOrdinal = new Map(messages.map((message) => [message.ordinal, message]));
737
- let end = exclusiveEnd;
738
- while (end - 1 >= floor && isHostUnservedRow(byOrdinal.get(end - 1)))
739
- end -= 1;
740
- return end;
454
+ function messageCacheKey(message, options) {
455
+ const namespace = "cacheNamespace" in options ? options.cacheNamespace : "estimate";
456
+ const cheapFingerprint = message.parts.map(partCheapFingerprint).join("|");
457
+ return [
458
+ namespace,
459
+ options.providerShapeVersion,
460
+ message.id || `ordinal:${message.ordinal}`,
461
+ message.role,
462
+ message.parts.length,
463
+ cheapFingerprint
464
+ ].join("\x00");
741
465
  }
742
- function snapTerminalCompartmentToServedRow(compartments, lines) {
743
- if (!lines.some(isHostUnservedRow))
744
- return { compartments: [...compartments], snapped: false };
745
- const byOrdinal = new Map(lines.map((line) => [line.ordinal, line]));
746
- const result = [...compartments];
747
- let snapped = false;
748
- while (result.length > 0) {
749
- const last = result[result.length - 1];
750
- if (!isHostUnservedRow(byOrdinal.get(last.endMessage)))
751
- break;
752
- snapped = true;
753
- let served;
754
- for (let ordinal = last.endMessage - 1;ordinal >= last.startMessage; ordinal -= 1) {
755
- const line = byOrdinal.get(ordinal);
756
- if (line && !isHostUnservedRow(line)) {
757
- served = line;
758
- break;
759
- }
760
- }
761
- if (!served) {
762
- result.pop();
763
- continue;
466
+ function setCachedEstimate(key, breakdown) {
467
+ const keyEstimateBytes = key.length * 2 + 64;
468
+ const existing = messageEstimateCache.get(key);
469
+ if (existing)
470
+ messageEstimateCacheBytes -= existing.keyEstimateBytes;
471
+ else {
472
+ for (const field of new Set(key.split("\x00").slice(1, -1))) {
473
+ const keys = messageEstimateKeysByField.get(field) ?? new Set;
474
+ keys.add(key);
475
+ messageEstimateKeysByField.set(field, keys);
764
476
  }
765
- result[result.length - 1] = {
766
- ...last,
767
- endMessage: served.ordinal,
768
- endMessageId: served.messageId
769
- };
770
- break;
771
477
  }
772
- return { compartments: result, snapped };
478
+ messageEstimateCache.set(key, { breakdown, keyEstimateBytes });
479
+ messageEstimateCacheBytes += keyEstimateBytes;
480
+ while (messageEstimateCache.size > MAX_MESSAGE_CACHE_ENTRIES || messageEstimateCacheBytes > MAX_MESSAGE_CACHE_KEY_BYTES) {
481
+ const first = messageEstimateCache.keys().next().value;
482
+ if (typeof first !== "string")
483
+ break;
484
+ deleteCachedEstimate(first);
485
+ }
773
486
  }
774
- function isHostRenderedSystemMessage(message) {
775
- const id = message.info.id;
776
- return (typeof id !== "string" || id.length === 0) && message.info.role === "system";
487
+ function deleteCachedEstimate(key) {
488
+ const removed = messageEstimateCache.get(key);
489
+ if (!removed)
490
+ return;
491
+ messageEstimateCacheBytes -= removed.keyEstimateBytes;
492
+ messageEstimateCache.delete(key);
493
+ for (const field of new Set(key.split("\x00").slice(1, -1))) {
494
+ const keys = messageEstimateKeysByField.get(field);
495
+ keys?.delete(key);
496
+ if (keys?.size === 0)
497
+ messageEstimateKeysByField.delete(field);
498
+ }
777
499
  }
778
- var COVERED_INSTRUCTION_ROW = "coveredInstructionRow";
779
- function markCoveredInstructionRow(target) {
780
- Object.defineProperty(target, COVERED_INSTRUCTION_ROW, {
781
- value: true,
782
- enumerable: false,
783
- configurable: true
784
- });
785
- return target;
786
- }
787
- function isCoveredInstructionRow(value) {
788
- return typeof value === "object" && value !== null && value[COVERED_INSTRUCTION_ROW] === true;
500
+ function cloneBreakdown(value) {
501
+ return { ...value };
789
502
  }
790
- function takeCoveredInstructionRows(messages) {
791
- const taken = messages.filter((message) => isCoveredInstructionRow(message.info));
792
- if (taken.length > 0) {
793
- const kept = messages.filter((message) => !isCoveredInstructionRow(message.info));
794
- messages.splice(0, messages.length, ...kept);
503
+ function estimateNonToolPart(part, options, breakdown) {
504
+ if (!isRecord(part)) {
505
+ if (part !== null && part !== undefined)
506
+ addBreakdown(breakdown, "other", estimateStructured(part));
507
+ return true;
795
508
  }
796
- return taken;
797
- }
798
- function cloneKeepingCoveredInstructionMarks(messages) {
799
- const copy = structuredClone(messages);
800
- messages.forEach((message, index) => {
801
- const copied = copy[index];
802
- if (copied && isCoveredInstructionRow(message.info))
803
- markCoveredInstructionRow(copied.info);
804
- });
805
- return copy;
806
- }
807
-
808
- // src/hooks/magic-context/read-session-true-raw-tokens.ts
809
- function completedToolArcCrossesBoundary(invOrdinal, resOrdinal, boundary) {
810
- return invOrdinal < boundary && boundary <= resOrdinal;
811
- }
812
- var MAX_MESSAGE_CACHE_ENTRIES = 1e5;
813
- var MAX_MESSAGE_CACHE_KEY_BYTES = 64 * 1024 * 1024;
814
- var FNV1A_32_OFFSET = 2166136261;
815
- var FNV1A_32_PRIME = 16777619;
816
- var messageEstimateCache = new Map;
817
- var messageEstimateKeysByField = new Map;
818
- var messageEstimateCacheBytes = 0;
819
- var EMPTY_BREAKDOWN = {
820
- text: 0,
821
- reasoning: 0,
822
- toolInput: 0,
823
- toolOutput: 0,
824
- image: 0,
825
- other: 0,
826
- total: 0
827
- };
828
- function addBreakdown(target, kind, value) {
829
- if (kind === "total")
830
- return;
831
- const safeValue = Number.isFinite(value) && value > 0 ? Math.round(value) : 0;
832
- target[kind] += safeValue;
833
- target.total += safeValue;
834
- }
835
- function estimateStructured(value) {
836
- if (typeof value === "string")
837
- return estimateTokens(value);
838
- if (value === undefined || value === null)
839
- return 0;
840
- return estimateTokens(stableStringify(value));
841
- }
842
- function firstStringField(record, fields) {
843
- for (const field of fields) {
844
- const value = record[field];
845
- if (typeof value === "string" && value.length > 0)
846
- return value;
509
+ const type = partType(part);
510
+ if (type === "step-start" || type === "step-finish" || type === "meta" && Object.keys(part).length <= 1) {
511
+ return true;
847
512
  }
848
- return null;
849
- }
850
- function stringValue(value) {
851
- if (typeof value === "string")
852
- return value;
853
- if (value === undefined || value === null)
854
- return "";
855
- return stableStringify(value);
856
- }
857
- function textFromToolResultContent(content) {
858
- if (typeof content === "string")
859
- return content;
860
- if (Array.isArray(content)) {
861
- const pieces = [];
862
- for (const entry of content) {
863
- if (typeof entry === "string") {
864
- pieces.push(entry);
865
- } else if (isRecord(entry)) {
866
- const text = firstStringField(entry, ["text", "content", "value"]);
867
- pieces.push(text ?? stableStringify(entry));
868
- } else if (entry !== null && entry !== undefined) {
869
- pieces.push(String(entry));
870
- }
871
- }
872
- return pieces.join(`
873
- `);
513
+ if (type === "text") {
514
+ const text = firstStringField(part, ["text", "content"]);
515
+ if (text)
516
+ addBreakdown(breakdown, "text", estimateTokens(text));
517
+ return true;
874
518
  }
875
- return stringValue(content);
876
- }
877
- function looksImageLike(part) {
878
- const type = typeof part.type === "string" ? part.type.toLowerCase() : "";
879
- const mime = typeof part.mime === "string" ? part.mime.toLowerCase() : "";
880
- const mediaType = typeof part.mediaType === "string" ? part.mediaType.toLowerCase() : "";
881
- return type.includes("image") || mime.startsWith("image/") || mediaType.startsWith("image/") || part.image_url !== undefined || part.imageUrl !== undefined || part.image !== undefined;
882
- }
883
- function defaultImageTokenHeuristic(part) {
884
- if (isRecord(part)) {
885
- const width = part.width;
886
- const height = part.height;
887
- if (typeof width === "number" && typeof height === "number" && width > 0 && height > 0) {
888
- return Math.max(256, Math.min(4096, Math.ceil(width * height / 750)));
519
+ if (type === "reasoning" || type === "thinking" || type === "redacted_thinking") {
520
+ const text = firstStringField(part, ["thinking", "text", "content", "reasoning"]);
521
+ if (text) {
522
+ addBreakdown(breakdown, "reasoning", estimateTokens(text));
523
+ } else {
524
+ addBreakdown(breakdown, "other", estimateStructured(part));
889
525
  }
526
+ return true;
890
527
  }
891
- return 1024;
892
- }
893
- function partType(part) {
894
- return typeof part.type === "string" ? part.type : "";
895
- }
896
- function hasOwn(record, key) {
897
- return Object.hasOwn(record, key);
898
- }
899
- function recursiveByteLength(value) {
900
- if (value === null || value === undefined)
901
- return 0;
902
- if (typeof value === "string")
903
- return value.length;
904
- if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
905
- return String(value).length;
528
+ const reasoningText = firstStringField(part, ["thinking", "reasoning"]);
529
+ if (reasoningText && type.length === 0) {
530
+ addBreakdown(breakdown, "reasoning", estimateTokens(reasoningText));
531
+ return true;
906
532
  }
907
- if (Array.isArray(value)) {
908
- return value.reduce((sum, item) => sum + recursiveByteLength(item), value.length);
533
+ if (looksImageLike(part)) {
534
+ addBreakdown(breakdown, "image", options.imageTokenHeuristic?.(part) ?? defaultImageTokenHeuristic(part));
535
+ const altText = firstStringField(part, ["alt", "text", "description"]);
536
+ if (altText)
537
+ addBreakdown(breakdown, "text", estimateTokens(altText));
538
+ return true;
909
539
  }
910
- if (isRecord(value)) {
911
- let total = Object.keys(value).length;
912
- for (const [key, child] of Object.entries(value)) {
913
- total += key.length + recursiveByteLength(child);
914
- }
915
- return total;
540
+ if (type.includes("file") || type === "source") {
541
+ const content = firstStringField(part, ["content", "text", "source"]);
542
+ if (content)
543
+ addBreakdown(breakdown, "text", estimateTokens(content));
544
+ else
545
+ addBreakdown(breakdown, "other", estimateStructured(part));
546
+ return true;
916
547
  }
917
- return String(value).length;
548
+ return false;
918
549
  }
919
- function updateFnv1a32(hash, text) {
920
- let next = hash;
921
- for (let index = 0;index < text.length; index += 1) {
922
- next ^= text.charCodeAt(index);
923
- next = Math.imul(next, FNV1A_32_PRIME) >>> 0;
550
+ function estimateTrueRawMessageTokens(message, options) {
551
+ const breakdown = cloneBreakdown(EMPTY_BREAKDOWN);
552
+ const countedInput = new Set;
553
+ const countedOutput = new Set;
554
+ let ordinalToolIndex = 0;
555
+ for (const part of message.parts) {
556
+ const signal = toolSignalFromPart(part);
557
+ if (signal) {
558
+ const localKey = `${signal.callId || "tool"}:${message.ordinal}:${ordinalToolIndex}`;
559
+ ordinalToolIndex += 1;
560
+ if (signal.hasInput) {
561
+ const key = `${signal.callId}:input:${message.ordinal}`;
562
+ if (!countedInput.has(key)) {
563
+ countedInput.add(key);
564
+ addBreakdown(breakdown, "toolInput", estimateTokens(signal.inputText));
565
+ }
566
+ }
567
+ if (signal.hasOutput) {
568
+ const key = `${signal.callId}:output:${message.ordinal}:${localKey}`;
569
+ if (!countedOutput.has(key)) {
570
+ countedOutput.add(key);
571
+ addBreakdown(breakdown, "toolOutput", estimateTokens(signal.outputText));
572
+ }
573
+ }
574
+ continue;
575
+ }
576
+ if (!estimateNonToolPart(part, options, breakdown)) {
577
+ addBreakdown(breakdown, "other", estimateStructured(part));
578
+ }
924
579
  }
925
- return next;
580
+ return breakdown;
926
581
  }
927
- function contentStringsHash(fields) {
928
- let hash = FNV1A_32_OFFSET;
929
- for (const field of fields) {
930
- hash = updateFnv1a32(hash, `${field.length}:`);
931
- hash = updateFnv1a32(hash, field);
932
- hash = updateFnv1a32(hash, "\x00");
582
+ function buildToolArcs(messages) {
583
+ const openQueues = new Map;
584
+ const arcs = [];
585
+ for (const message of messages) {
586
+ for (const part of message.parts) {
587
+ const signal = toolSignalFromPart(part, false);
588
+ if (!signal || signal.callId.length === 0)
589
+ continue;
590
+ if (signal.hasInput && signal.hasOutput) {
591
+ arcs.push({
592
+ callId: signal.callId,
593
+ invOrdinal: message.ordinal,
594
+ resOrdinal: message.ordinal
595
+ });
596
+ continue;
597
+ }
598
+ if (signal.hasInput) {
599
+ const queue = openQueues.get(signal.callId) ?? [];
600
+ queue.push(message.ordinal);
601
+ openQueues.set(signal.callId, queue);
602
+ continue;
603
+ }
604
+ if (signal.hasOutput) {
605
+ const queue = openQueues.get(signal.callId) ?? [];
606
+ const invOrdinal = queue.shift();
607
+ if (queue.length === 0)
608
+ openQueues.delete(signal.callId);
609
+ else
610
+ openQueues.set(signal.callId, queue);
611
+ if (invOrdinal !== undefined) {
612
+ arcs.push({ callId: signal.callId, invOrdinal, resOrdinal: message.ordinal });
613
+ }
614
+ }
615
+ }
933
616
  }
934
- return hash.toString(16).padStart(8, "0");
935
- }
936
- function rawPartVersion(part) {
937
- return part.__magicContextPartUpdatedAt ?? part.updated_at ?? part.updatedAt ?? part.version ?? part.revision ?? "";
938
- }
939
- function callIdFromPart(part) {
940
- const direct = firstStringField(part, ["callID", "callId", "toolCallId", "tool_call_id", "id"]);
941
- if (direct)
942
- return direct;
943
- const state = isRecord(part.state) ? part.state : null;
944
- return state ? firstStringField(state, ["callID", "callId", "toolCallId", "tool_call_id", "id"]) ?? "" : "";
945
- }
946
- function toolSignalFromPart(part, withText = true) {
947
- if (!isRecord(part))
948
- return null;
949
- const type = partType(part);
950
- const state = isRecord(part.state) ? part.state : null;
951
- const callId = callIdFromPart(part);
952
- if (!callId && type !== "tool")
953
- return null;
954
- if (type === "tool") {
955
- const hasInput = state !== null && hasOwn(state, "input");
956
- const outputKey = state ? hasOwn(state, "output") ? "output" : hasOwn(state, "error") ? "error" : hasOwn(state, "result") ? "result" : null : null;
957
- const hasOutput = outputKey !== null;
958
- const outputValue = outputKey && state ? state[outputKey] : undefined;
959
- const providerExecuted = part.providerExecuted === true;
960
- const openInvocation = !providerExecuted && !hasOutput;
961
- return {
962
- callId,
963
- hasInput: hasInput || openInvocation,
964
- hasOutput,
965
- inputText: withText && hasInput && state ? stringValue(state.input) : "",
966
- outputText: withText && hasOutput ? stringValue(outputValue) : ""
967
- };
968
- }
969
- if (type === "tool-invocation") {
970
- const args = part.args ?? part.input;
971
- return {
972
- callId,
973
- hasInput: args !== undefined,
974
- hasOutput: false,
975
- inputText: withText && args !== undefined ? stringValue(args) : "",
976
- outputText: ""
977
- };
978
- }
979
- if (type === "tool_use") {
980
- const input = part.input;
981
- return {
982
- callId,
983
- hasInput: input !== undefined,
984
- hasOutput: false,
985
- inputText: withText && input !== undefined ? stringValue(input) : "",
986
- outputText: ""
987
- };
988
- }
989
- if (type === "tool_result") {
990
- const content = part.content ?? part.output ?? part.result;
991
- return {
992
- callId,
993
- hasInput: false,
994
- hasOutput: content !== undefined,
995
- inputText: "",
996
- outputText: withText && content !== undefined ? textFromToolResultContent(content) : ""
997
- };
998
- }
999
- return null;
1000
- }
1001
- function partCheapFingerprint(part) {
1002
- if (!isRecord(part))
1003
- return `${typeof part}:${recursiveByteLength(part)}`;
1004
- const version = rawPartVersion(part);
1005
- const type = typeof part.type === "string" ? part.type : "";
1006
- return `${type}:${String(version)}:${recursiveByteLength(providerVisiblePart(part))}`;
1007
- }
1008
- function messageCacheKey(message, options) {
1009
- const namespace = "cacheNamespace" in options ? options.cacheNamespace : "estimate";
1010
- const cheapFingerprint = message.parts.map(partCheapFingerprint).join("|");
1011
- return [
1012
- namespace,
1013
- options.providerShapeVersion,
1014
- message.id || `ordinal:${message.ordinal}`,
1015
- message.role,
1016
- message.parts.length,
1017
- cheapFingerprint
1018
- ].join("\x00");
1019
- }
1020
- function setCachedEstimate(key, breakdown) {
1021
- const keyEstimateBytes = key.length * 2 + 64;
1022
- const existing = messageEstimateCache.get(key);
1023
- if (existing)
1024
- messageEstimateCacheBytes -= existing.keyEstimateBytes;
1025
- else {
1026
- for (const field of new Set(key.split("\x00").slice(1, -1))) {
1027
- const keys = messageEstimateKeysByField.get(field) ?? new Set;
1028
- keys.add(key);
1029
- messageEstimateKeysByField.set(field, keys);
1030
- }
1031
- }
1032
- messageEstimateCache.set(key, { breakdown, keyEstimateBytes });
1033
- messageEstimateCacheBytes += keyEstimateBytes;
1034
- while (messageEstimateCache.size > MAX_MESSAGE_CACHE_ENTRIES || messageEstimateCacheBytes > MAX_MESSAGE_CACHE_KEY_BYTES) {
1035
- const first = messageEstimateCache.keys().next().value;
1036
- if (typeof first !== "string")
1037
- break;
1038
- deleteCachedEstimate(first);
1039
- }
1040
- }
1041
- function deleteCachedEstimate(key) {
1042
- const removed = messageEstimateCache.get(key);
1043
- if (!removed)
1044
- return;
1045
- messageEstimateCacheBytes -= removed.keyEstimateBytes;
1046
- messageEstimateCache.delete(key);
1047
- for (const field of new Set(key.split("\x00").slice(1, -1))) {
1048
- const keys = messageEstimateKeysByField.get(field);
1049
- keys?.delete(key);
1050
- if (keys?.size === 0)
1051
- messageEstimateKeysByField.delete(field);
1052
- }
1053
- }
1054
- function cloneBreakdown(value) {
1055
- return { ...value };
1056
- }
1057
- function estimateNonToolPart(part, options, breakdown) {
1058
- if (!isRecord(part)) {
1059
- if (part !== null && part !== undefined)
1060
- addBreakdown(breakdown, "other", estimateStructured(part));
1061
- return true;
1062
- }
1063
- const type = partType(part);
1064
- if (type === "step-start" || type === "step-finish" || type === "meta" && Object.keys(part).length <= 1) {
1065
- return true;
1066
- }
1067
- if (type === "text") {
1068
- const text = firstStringField(part, ["text", "content"]);
1069
- if (text)
1070
- addBreakdown(breakdown, "text", estimateTokens(text));
1071
- return true;
1072
- }
1073
- if (type === "reasoning" || type === "thinking" || type === "redacted_thinking") {
1074
- const text = firstStringField(part, ["thinking", "text", "content", "reasoning"]);
1075
- if (text) {
1076
- addBreakdown(breakdown, "reasoning", estimateTokens(text));
1077
- } else {
1078
- addBreakdown(breakdown, "other", estimateStructured(part));
1079
- }
1080
- return true;
1081
- }
1082
- const reasoningText = firstStringField(part, ["thinking", "reasoning"]);
1083
- if (reasoningText && type.length === 0) {
1084
- addBreakdown(breakdown, "reasoning", estimateTokens(reasoningText));
1085
- return true;
1086
- }
1087
- if (looksImageLike(part)) {
1088
- addBreakdown(breakdown, "image", options.imageTokenHeuristic?.(part) ?? defaultImageTokenHeuristic(part));
1089
- const altText = firstStringField(part, ["alt", "text", "description"]);
1090
- if (altText)
1091
- addBreakdown(breakdown, "text", estimateTokens(altText));
1092
- return true;
1093
- }
1094
- if (type.includes("file") || type === "source") {
1095
- const content = firstStringField(part, ["content", "text", "source"]);
1096
- if (content)
1097
- addBreakdown(breakdown, "text", estimateTokens(content));
1098
- else
1099
- addBreakdown(breakdown, "other", estimateStructured(part));
1100
- return true;
1101
- }
1102
- return false;
1103
- }
1104
- function estimateTrueRawMessageTokens(message, options) {
1105
- const breakdown = cloneBreakdown(EMPTY_BREAKDOWN);
1106
- const countedInput = new Set;
1107
- const countedOutput = new Set;
1108
- let ordinalToolIndex = 0;
1109
- for (const part of message.parts) {
1110
- const signal = toolSignalFromPart(part);
1111
- if (signal) {
1112
- const localKey = `${signal.callId || "tool"}:${message.ordinal}:${ordinalToolIndex}`;
1113
- ordinalToolIndex += 1;
1114
- if (signal.hasInput) {
1115
- const key = `${signal.callId}:input:${message.ordinal}`;
1116
- if (!countedInput.has(key)) {
1117
- countedInput.add(key);
1118
- addBreakdown(breakdown, "toolInput", estimateTokens(signal.inputText));
1119
- }
1120
- }
1121
- if (signal.hasOutput) {
1122
- const key = `${signal.callId}:output:${message.ordinal}:${localKey}`;
1123
- if (!countedOutput.has(key)) {
1124
- countedOutput.add(key);
1125
- addBreakdown(breakdown, "toolOutput", estimateTokens(signal.outputText));
1126
- }
1127
- }
1128
- continue;
1129
- }
1130
- if (!estimateNonToolPart(part, options, breakdown)) {
1131
- addBreakdown(breakdown, "other", estimateStructured(part));
1132
- }
1133
- }
1134
- return breakdown;
1135
- }
1136
- function buildToolArcs(messages) {
1137
- const openQueues = new Map;
1138
- const arcs = [];
1139
- for (const message of messages) {
1140
- for (const part of message.parts) {
1141
- const signal = toolSignalFromPart(part, false);
1142
- if (!signal || signal.callId.length === 0)
1143
- continue;
1144
- if (signal.hasInput && signal.hasOutput) {
1145
- arcs.push({
1146
- callId: signal.callId,
1147
- invOrdinal: message.ordinal,
1148
- resOrdinal: message.ordinal
1149
- });
1150
- continue;
1151
- }
1152
- if (signal.hasInput) {
1153
- const queue = openQueues.get(signal.callId) ?? [];
1154
- queue.push(message.ordinal);
1155
- openQueues.set(signal.callId, queue);
1156
- continue;
1157
- }
1158
- if (signal.hasOutput) {
1159
- const queue = openQueues.get(signal.callId) ?? [];
1160
- const invOrdinal = queue.shift();
1161
- if (queue.length === 0)
1162
- openQueues.delete(signal.callId);
1163
- else
1164
- openQueues.set(signal.callId, queue);
1165
- if (invOrdinal !== undefined) {
1166
- arcs.push({ callId: signal.callId, invOrdinal, resOrdinal: message.ordinal });
1167
- }
1168
- }
1169
- }
1170
- }
1171
- for (const [callId, queue] of openQueues) {
1172
- for (const invOrdinal of queue) {
1173
- arcs.push({ callId, invOrdinal, resOrdinal: null });
1174
- }
1175
- }
1176
- return arcs.sort((a, b) => a.invOrdinal - b.invOrdinal || (a.resOrdinal ?? Number.MAX_SAFE_INTEGER) - (b.resOrdinal ?? Number.MAX_SAFE_INTEGER));
617
+ for (const [callId, queue] of openQueues) {
618
+ for (const invOrdinal of queue) {
619
+ arcs.push({ callId, invOrdinal, resOrdinal: null });
620
+ }
621
+ }
622
+ return arcs.sort((a, b) => a.invOrdinal - b.invOrdinal || (a.resOrdinal ?? Number.MAX_SAFE_INTEGER) - (b.resOrdinal ?? Number.MAX_SAFE_INTEGER));
1177
623
  }
1178
624
  function fenceBoundaryForCompletedToolArcs(candidate, arcs, publicationFloorOrdinal, observeComponent) {
1179
625
  const completed = arcs.filter((arc) => arc.resOrdinal !== null).map((arc) => ({ invocation: arc.invOrdinal, result: arc.resOrdinal }));
@@ -2180,7 +1626,369 @@ function getProtectionWindowForSession(db, sessionId, floor) {
2180
1626
  const last = page[page.length - 1];
2181
1627
  page = db.prepare(`${projection} AND (tag_number, id) < (?, ?) ${order}`).all(sessionId, getRowTagNumber(last), getRowIdentity(last));
2182
1628
  }
2183
- return computeProtectionWindow(rows, effectiveFloor, toolsRatio);
1629
+ return computeProtectionWindow(rows, effectiveFloor, toolsRatio);
1630
+ }
1631
+
1632
+ // src/features/magic-context/storage-meta-shared.ts
1633
+ import { Buffer as Buffer2 } from "node:buffer";
1634
+ var SESSION_META_SELECT_COLUMNS = [
1635
+ "session_id",
1636
+ "last_response_time",
1637
+ "cache_ttl",
1638
+ "counter",
1639
+ "last_nudge_tokens",
1640
+ "last_nudge_band",
1641
+ "last_transform_error",
1642
+ "is_subagent",
1643
+ "last_context_percentage",
1644
+ "last_input_tokens",
1645
+ "observed_safe_input_tokens",
1646
+ "cache_alert_sent",
1647
+ "times_execute_threshold_reached",
1648
+ "compartment_in_progress",
1649
+ "system_prompt_hash",
1650
+ "system_prompt_tokens",
1651
+ "conversation_tokens",
1652
+ "tool_call_tokens",
1653
+ "cleared_reasoning_through_tag",
1654
+ "tool_reclaim_watermark",
1655
+ "last_todo_state",
1656
+ "cached_m0_bytes",
1657
+ "cached_m0_mural_data_url",
1658
+ "cached_m0_mural_hash",
1659
+ "cached_m1_bytes",
1660
+ "cached_m0_project_memory_epoch",
1661
+ "cached_m0_workspace_fingerprint",
1662
+ "cached_m0_project_user_profile_version",
1663
+ "cached_m0_max_compartment_seq",
1664
+ "cached_m0_max_memory_id",
1665
+ "cached_m0_max_mutation_id",
1666
+ "cached_m0_max_memory_mutation_id",
1667
+ "cached_m0_project_docs_hash",
1668
+ "cached_m0_materialized_at",
1669
+ "cached_m0_session_facts_version",
1670
+ "cached_m0_upgrade_state",
1671
+ "cached_m0_system_hash",
1672
+ "cached_m0_tool_set_hash",
1673
+ "cached_m0_model_key",
1674
+ "cached_m0_project_identity",
1675
+ "last_observed_model_key",
1676
+ "last_usage_context_limit",
1677
+ "prior_boundary_ordinal",
1678
+ "protected_tail_policy_version",
1679
+ "protected_tail_drain_window_started_at",
1680
+ "protected_tail_drain_tokens",
1681
+ "recovery_no_eligible_head_count",
1682
+ "force_emergency_bypass_window_start",
1683
+ "force_emergency_bypass_used",
1684
+ "emergency_drain_active",
1685
+ "historian_drain_failure_at",
1686
+ "upgrade_reminded_at",
1687
+ "upgrade_reminder_last_sent_at",
1688
+ "upgrade_reminder_count",
1689
+ "pi_stable_id_scheme"
1690
+ ];
1691
+ var META_COLUMNS = {
1692
+ lastResponseTime: "last_response_time",
1693
+ cacheTtl: "cache_ttl",
1694
+ counter: "counter",
1695
+ lastNudgeTokens: "last_nudge_tokens",
1696
+ lastNudgeBand: "last_nudge_band",
1697
+ lastTransformError: "last_transform_error",
1698
+ isSubagent: "is_subagent",
1699
+ lastContextPercentage: "last_context_percentage",
1700
+ lastInputTokens: "last_input_tokens",
1701
+ observedSafeInputTokens: "observed_safe_input_tokens",
1702
+ cacheAlertSent: "cache_alert_sent",
1703
+ timesExecuteThresholdReached: "times_execute_threshold_reached",
1704
+ compartmentInProgress: "compartment_in_progress",
1705
+ systemPromptHash: "system_prompt_hash",
1706
+ systemPromptTokens: "system_prompt_tokens",
1707
+ conversationTokens: "conversation_tokens",
1708
+ toolCallTokens: "tool_call_tokens",
1709
+ clearedReasoningThroughTag: "cleared_reasoning_through_tag",
1710
+ toolReclaimWatermark: "tool_reclaim_watermark",
1711
+ lastTodoState: "last_todo_state",
1712
+ cachedM0Bytes: "cached_m0_bytes",
1713
+ cachedM0MuralDataUrl: "cached_m0_mural_data_url",
1714
+ cachedM0MuralHash: "cached_m0_mural_hash",
1715
+ cachedM1Bytes: "cached_m1_bytes",
1716
+ cachedM0ProjectMemoryEpoch: "cached_m0_project_memory_epoch",
1717
+ cachedM0WorkspaceFingerprint: "cached_m0_workspace_fingerprint",
1718
+ cachedM0ProjectUserProfileVersion: "cached_m0_project_user_profile_version",
1719
+ cachedM0MaxCompartmentSeq: "cached_m0_max_compartment_seq",
1720
+ cachedM0MaxMemoryId: "cached_m0_max_memory_id",
1721
+ cachedM0MaxMutationId: "cached_m0_max_mutation_id",
1722
+ cachedM0MaxMemoryMutationId: "cached_m0_max_memory_mutation_id",
1723
+ cachedM0ProjectDocsHash: "cached_m0_project_docs_hash",
1724
+ cachedM0MaterializedAt: "cached_m0_materialized_at",
1725
+ cachedM0SessionFactsVersion: "cached_m0_session_facts_version",
1726
+ cachedM0UpgradeState: "cached_m0_upgrade_state",
1727
+ cachedM0SystemHash: "cached_m0_system_hash",
1728
+ cachedM0ToolSetHash: "cached_m0_tool_set_hash",
1729
+ cachedM0ModelKey: "cached_m0_model_key",
1730
+ cachedM0ProjectIdentity: "cached_m0_project_identity",
1731
+ lastObservedModelKey: "last_observed_model_key",
1732
+ lastUsageContextLimit: "last_usage_context_limit",
1733
+ priorBoundaryOrdinal: "prior_boundary_ordinal",
1734
+ protectedTailPolicyVersion: "protected_tail_policy_version",
1735
+ protectedTailDrainWindowStartedAt: "protected_tail_drain_window_started_at",
1736
+ protectedTailDrainTokens: "protected_tail_drain_tokens",
1737
+ recoveryNoEligibleHeadCount: "recovery_no_eligible_head_count",
1738
+ forceEmergencyBypassWindowStart: "force_emergency_bypass_window_start",
1739
+ forceEmergencyBypassUsed: "force_emergency_bypass_used",
1740
+ emergencyDrainActive: "emergency_drain_active",
1741
+ historianDrainFailureAt: "historian_drain_failure_at",
1742
+ upgradeRemindedAt: "upgrade_reminded_at",
1743
+ upgradeReminderLastSentAt: "upgrade_reminder_last_sent_at",
1744
+ upgradeReminderCount: "upgrade_reminder_count",
1745
+ piStableIdScheme: "pi_stable_id_scheme"
1746
+ };
1747
+ var BOOLEAN_META_KEYS = new Set(["isSubagent", "compartmentInProgress", "cacheAlertSent"]);
1748
+ var NULL_BIND_META_KEYS = new Set([
1749
+ "cachedM0Bytes",
1750
+ "cachedM0MuralDataUrl",
1751
+ "cachedM0MuralHash",
1752
+ "cachedM1Bytes",
1753
+ "cachedM0ProjectMemoryEpoch",
1754
+ "cachedM0WorkspaceFingerprint",
1755
+ "cachedM0ProjectUserProfileVersion",
1756
+ "cachedM0MaxCompartmentSeq",
1757
+ "cachedM0MaxMemoryId",
1758
+ "cachedM0MaxMutationId",
1759
+ "cachedM0MaxMemoryMutationId",
1760
+ "cachedM0ProjectDocsHash",
1761
+ "cachedM0MaterializedAt",
1762
+ "cachedM0SessionFactsVersion",
1763
+ "cachedM0UpgradeState",
1764
+ "cachedM0ProjectIdentity",
1765
+ "lastObservedModelKey",
1766
+ "upgradeRemindedAt",
1767
+ "upgradeReminderLastSentAt",
1768
+ "piStableIdScheme"
1769
+ ]);
1770
+ function isStringOrNull(value) {
1771
+ return value === null || typeof value === "string";
1772
+ }
1773
+ function isNumberOrNull(value) {
1774
+ return value === null || typeof value === "number";
1775
+ }
1776
+ function isBlobOrNull(value) {
1777
+ return value === null || Buffer2.isBuffer(value) || value instanceof Uint8Array;
1778
+ }
1779
+ function toBufferOrNull(value) {
1780
+ if (value === null)
1781
+ return null;
1782
+ if (Buffer2.isBuffer(value))
1783
+ return value;
1784
+ return Buffer2.from(value.buffer, value.byteOffset, value.byteLength);
1785
+ }
1786
+ function isSessionMetaRow(row) {
1787
+ if (row === null || typeof row !== "object")
1788
+ return false;
1789
+ const r = row;
1790
+ return typeof r.session_id === "string" && isNumberOrNull(r.last_response_time) && isStringOrNull(r.cache_ttl) && typeof r.counter === "number" && typeof r.last_nudge_tokens === "number" && isStringOrNull(r.last_nudge_band) && isStringOrNull(r.last_transform_error) && isNumberOrNull(r.is_subagent) && typeof r.last_context_percentage === "number" && typeof r.last_input_tokens === "number" && isNumberOrNull(r.observed_safe_input_tokens) && isNumberOrNull(r.cache_alert_sent) && isNumberOrNull(r.times_execute_threshold_reached) && isNumberOrNull(r.compartment_in_progress) && (r.system_prompt_hash === null || typeof r.system_prompt_hash === "string" || typeof r.system_prompt_hash === "number") && isNumberOrNull(r.system_prompt_tokens) && isNumberOrNull(r.conversation_tokens) && isNumberOrNull(r.tool_call_tokens) && isNumberOrNull(r.cleared_reasoning_through_tag) && isStringOrNull(r.last_todo_state) && isBlobOrNull(r.cached_m0_bytes) && isStringOrNull(r.cached_m0_mural_data_url) && isStringOrNull(r.cached_m0_mural_hash) && isBlobOrNull(r.cached_m1_bytes) && isNumberOrNull(r.cached_m0_project_memory_epoch) && isStringOrNull(r.cached_m0_workspace_fingerprint) && isNumberOrNull(r.cached_m0_project_user_profile_version) && isNumberOrNull(r.cached_m0_max_compartment_seq) && isNumberOrNull(r.cached_m0_max_memory_id) && isNumberOrNull(r.cached_m0_max_mutation_id) && isNumberOrNull(r.cached_m0_max_memory_mutation_id) && isStringOrNull(r.cached_m0_project_docs_hash) && isNumberOrNull(r.cached_m0_materialized_at) && isNumberOrNull(r.cached_m0_session_facts_version) && isStringOrNull(r.cached_m0_upgrade_state) && isStringOrNull(r.cached_m0_system_hash) && isStringOrNull(r.cached_m0_tool_set_hash) && isStringOrNull(r.cached_m0_model_key) && isStringOrNull(r.cached_m0_project_identity) && isStringOrNull(r.last_observed_model_key) && isNumberOrNull(r.last_usage_context_limit) && isNumberOrNull(r.prior_boundary_ordinal) && isNumberOrNull(r.protected_tail_policy_version) && isNumberOrNull(r.protected_tail_drain_window_started_at) && isNumberOrNull(r.protected_tail_drain_tokens) && isNumberOrNull(r.recovery_no_eligible_head_count) && isNumberOrNull(r.force_emergency_bypass_window_start) && isNumberOrNull(r.force_emergency_bypass_used) && isNumberOrNull(r.upgrade_reminded_at) && isNumberOrNull(r.upgrade_reminder_last_sent_at) && isNumberOrNull(r.upgrade_reminder_count) && isNumberOrNull(r.pi_stable_id_scheme) && isNumberOrNull(r.tool_reclaim_watermark);
1791
+ }
1792
+ function getDefaultSessionMeta(sessionId) {
1793
+ return {
1794
+ sessionId,
1795
+ lastResponseTime: 0,
1796
+ cacheTtl: "5m",
1797
+ counter: 0,
1798
+ lastNudgeTokens: 0,
1799
+ lastNudgeBand: null,
1800
+ lastTransformError: null,
1801
+ isSubagent: false,
1802
+ lastContextPercentage: 0,
1803
+ lastInputTokens: 0,
1804
+ observedSafeInputTokens: 0,
1805
+ cacheAlertSent: false,
1806
+ timesExecuteThresholdReached: 0,
1807
+ compartmentInProgress: false,
1808
+ systemPromptHash: "",
1809
+ systemPromptTokens: 0,
1810
+ conversationTokens: 0,
1811
+ toolCallTokens: 0,
1812
+ clearedReasoningThroughTag: 0,
1813
+ toolReclaimWatermark: 0,
1814
+ lastTodoState: "",
1815
+ cachedM0Bytes: null,
1816
+ cachedM0MuralDataUrl: null,
1817
+ cachedM0MuralHash: null,
1818
+ cachedM1Bytes: null,
1819
+ cachedM0ProjectMemoryEpoch: null,
1820
+ cachedM0WorkspaceFingerprint: null,
1821
+ cachedM0ProjectUserProfileVersion: null,
1822
+ cachedM0MaxCompartmentSeq: null,
1823
+ cachedM0MaxMemoryId: null,
1824
+ cachedM0MaxMutationId: null,
1825
+ cachedM0MaxMemoryMutationId: null,
1826
+ cachedM0ProjectDocsHash: null,
1827
+ cachedM0MaterializedAt: null,
1828
+ cachedM0SessionFactsVersion: null,
1829
+ cachedM0UpgradeState: null,
1830
+ cachedM0SystemHash: null,
1831
+ cachedM0ToolSetHash: null,
1832
+ cachedM0ModelKey: null,
1833
+ cachedM0ProjectIdentity: null,
1834
+ lastObservedModelKey: null,
1835
+ lastUsageContextLimit: 0,
1836
+ priorBoundaryOrdinal: 1,
1837
+ protectedTailPolicyVersion: 0,
1838
+ protectedTailDrainWindowStartedAt: 0,
1839
+ protectedTailDrainTokens: 0,
1840
+ recoveryNoEligibleHeadCount: 0,
1841
+ forceEmergencyBypassWindowStart: 0,
1842
+ forceEmergencyBypassUsed: 0,
1843
+ upgradeRemindedAt: null,
1844
+ upgradeReminderLastSentAt: null,
1845
+ upgradeReminderCount: 0,
1846
+ piStableIdScheme: null
1847
+ };
1848
+ }
1849
+ var sessionMetaExistsStatements = new WeakMap;
1850
+ function ensureSessionMetaRow(db, sessionId, initialIsSubagent = getHarness2() === "opencode2" ? null : false) {
1851
+ let exists = sessionMetaExistsStatements.get(db);
1852
+ if (!exists) {
1853
+ exists = prepareCachedStatement2(db, "SELECT 1 FROM session_meta WHERE session_id = ?");
1854
+ sessionMetaExistsStatements.set(db, exists);
1855
+ }
1856
+ if (exists.get(sessionId))
1857
+ return;
1858
+ const defaults = getDefaultSessionMeta(sessionId);
1859
+ db.prepare("INSERT OR IGNORE INTO session_meta (session_id, harness, last_response_time, cache_ttl, counter, last_nudge_tokens, last_nudge_band, last_transform_error, is_subagent, last_context_percentage, last_input_tokens, observed_safe_input_tokens, cache_alert_sent, times_execute_threshold_reached, compartment_in_progress, system_prompt_hash, cleared_reasoning_through_tag) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").run(sessionId, getHarness2(), defaults.lastResponseTime, defaults.cacheTtl, defaults.counter, defaults.lastNudgeTokens, defaults.lastNudgeBand ?? "", defaults.lastTransformError ?? "", initialIsSubagent === null ? null : initialIsSubagent ? 1 : 0, defaults.lastContextPercentage, defaults.lastInputTokens, defaults.observedSafeInputTokens, defaults.cacheAlertSent ? 1 : 0, defaults.timesExecuteThresholdReached, defaults.compartmentInProgress ? 1 : 0, defaults.systemPromptHash ?? "", defaults.clearedReasoningThroughTag);
1860
+ }
1861
+ function toSessionMeta(row) {
1862
+ const nudgeBandRaw = typeof row.last_nudge_band === "string" ? row.last_nudge_band : "";
1863
+ const transformErrorRaw = typeof row.last_transform_error === "string" ? row.last_transform_error : "";
1864
+ const cacheTtlRaw = typeof row.cache_ttl === "string" && row.cache_ttl.length > 0 ? row.cache_ttl : "5m";
1865
+ const systemPromptHashRaw = row.system_prompt_hash == null ? "" : row.system_prompt_hash;
1866
+ const lastTodoStateRaw = typeof row.last_todo_state === "string" ? row.last_todo_state : "";
1867
+ const numOrZero = (value) => typeof value === "number" ? value : 0;
1868
+ const numOrNull = (value) => typeof value === "number" ? value : null;
1869
+ const stringOrNull = (value) => typeof value === "string" ? value : null;
1870
+ return {
1871
+ sessionId: row.session_id,
1872
+ lastResponseTime: numOrZero(row.last_response_time),
1873
+ cacheTtl: cacheTtlRaw,
1874
+ counter: row.counter,
1875
+ lastNudgeTokens: row.last_nudge_tokens,
1876
+ lastNudgeBand: nudgeBandRaw.length > 0 ? nudgeBandRaw : null,
1877
+ lastTransformError: transformErrorRaw.length > 0 ? transformErrorRaw : null,
1878
+ isSubagent: row.is_subagent === 1,
1879
+ lastContextPercentage: row.last_context_percentage,
1880
+ lastInputTokens: row.last_input_tokens,
1881
+ observedSafeInputTokens: numOrZero(row.observed_safe_input_tokens),
1882
+ cacheAlertSent: numOrZero(row.cache_alert_sent) === 1,
1883
+ timesExecuteThresholdReached: numOrZero(row.times_execute_threshold_reached),
1884
+ compartmentInProgress: row.compartment_in_progress === 1,
1885
+ systemPromptHash: String(systemPromptHashRaw),
1886
+ systemPromptTokens: numOrZero(row.system_prompt_tokens),
1887
+ conversationTokens: numOrZero(row.conversation_tokens),
1888
+ toolCallTokens: numOrZero(row.tool_call_tokens),
1889
+ clearedReasoningThroughTag: numOrZero(row.cleared_reasoning_through_tag),
1890
+ toolReclaimWatermark: numOrZero(row.tool_reclaim_watermark),
1891
+ lastTodoState: lastTodoStateRaw,
1892
+ cachedM0Bytes: toBufferOrNull(row.cached_m0_bytes),
1893
+ cachedM0MuralDataUrl: stringOrNull(row.cached_m0_mural_data_url),
1894
+ cachedM0MuralHash: stringOrNull(row.cached_m0_mural_hash),
1895
+ cachedM1Bytes: toBufferOrNull(row.cached_m1_bytes),
1896
+ cachedM0ProjectMemoryEpoch: numOrNull(row.cached_m0_project_memory_epoch),
1897
+ cachedM0WorkspaceFingerprint: stringOrNull(row.cached_m0_workspace_fingerprint),
1898
+ cachedM0ProjectUserProfileVersion: numOrNull(row.cached_m0_project_user_profile_version),
1899
+ cachedM0MaxCompartmentSeq: numOrNull(row.cached_m0_max_compartment_seq),
1900
+ cachedM0MaxMemoryId: numOrNull(row.cached_m0_max_memory_id),
1901
+ cachedM0MaxMutationId: numOrNull(row.cached_m0_max_mutation_id),
1902
+ cachedM0MaxMemoryMutationId: numOrNull(row.cached_m0_max_memory_mutation_id),
1903
+ cachedM0ProjectDocsHash: stringOrNull(row.cached_m0_project_docs_hash),
1904
+ cachedM0MaterializedAt: numOrNull(row.cached_m0_materialized_at),
1905
+ cachedM0SessionFactsVersion: numOrNull(row.cached_m0_session_facts_version),
1906
+ cachedM0UpgradeState: stringOrNull(row.cached_m0_upgrade_state),
1907
+ cachedM0SystemHash: stringOrNull(row.cached_m0_system_hash),
1908
+ cachedM0ToolSetHash: stringOrNull(row.cached_m0_tool_set_hash),
1909
+ cachedM0ModelKey: stringOrNull(row.cached_m0_model_key),
1910
+ cachedM0ProjectIdentity: stringOrNull(row.cached_m0_project_identity),
1911
+ lastObservedModelKey: stringOrNull(row.last_observed_model_key),
1912
+ lastUsageContextLimit: numOrZero(row.last_usage_context_limit),
1913
+ priorBoundaryOrdinal: Math.max(1, numOrZero(row.prior_boundary_ordinal) || 1),
1914
+ protectedTailPolicyVersion: numOrZero(row.protected_tail_policy_version),
1915
+ protectedTailDrainWindowStartedAt: numOrZero(row.protected_tail_drain_window_started_at),
1916
+ protectedTailDrainTokens: numOrZero(row.protected_tail_drain_tokens),
1917
+ recoveryNoEligibleHeadCount: numOrZero(row.recovery_no_eligible_head_count),
1918
+ forceEmergencyBypassWindowStart: numOrZero(row.force_emergency_bypass_window_start),
1919
+ forceEmergencyBypassUsed: numOrZero(row.force_emergency_bypass_used),
1920
+ upgradeRemindedAt: numOrNull(row.upgrade_reminded_at),
1921
+ upgradeReminderLastSentAt: numOrNull(row.upgrade_reminder_last_sent_at),
1922
+ upgradeReminderCount: numOrZero(row.upgrade_reminder_count),
1923
+ piStableIdScheme: numOrNull(row.pi_stable_id_scheme)
1924
+ };
1925
+ }
1926
+ function persistCachedM0(db, sessionId, payload) {
1927
+ ensureSessionMetaRow(db, sessionId);
1928
+ db.prepare(`UPDATE session_meta SET
1929
+ cached_m0_bytes = ?,
1930
+ cached_m0_mural_data_url = ?,
1931
+ cached_m0_mural_hash = ?,
1932
+ cached_m0_project_memory_epoch = ?,
1933
+ cached_m0_workspace_fingerprint = ?,
1934
+ cached_m0_project_user_profile_version = ?,
1935
+ cached_m0_max_compartment_seq = ?,
1936
+ cached_m0_max_memory_id = ?,
1937
+ cached_m0_max_mutation_id = ?,
1938
+ cached_m0_max_memory_mutation_id = ?,
1939
+ cached_m1_bytes = ?,
1940
+ cached_m0_project_docs_hash = ?,
1941
+ cached_m0_materialized_at = ?,
1942
+ cached_m0_session_facts_version = ?,
1943
+ cached_m0_upgrade_state = ?,
1944
+ cached_m0_system_hash = ?,
1945
+ cached_m0_tool_set_hash = ?,
1946
+ cached_m0_model_key = ?,
1947
+ cached_m0_project_identity = ?
1948
+ WHERE session_id = ?`).run(Buffer2.from(payload.m0Bytes), payload.muralDataUrl ?? null, payload.muralHash ?? null, payload.projectMemoryEpoch, payload.workspaceFingerprint ?? null, payload.projectUserProfileVersion, payload.maxCompartmentSeq, payload.maxMemoryId, payload.maxMutationId, payload.maxMemoryMutationId ?? null, payload.m1Bytes ? Buffer2.from(payload.m1Bytes) : null, payload.projectDocsHash, payload.materializedAt, payload.sessionFactsVersion, payload.upgradeState, payload.systemHash ?? "", payload.toolSetHash ?? "", payload.modelKey ? piModelRefToCanonical(payload.modelKey) : "", payload.projectIdentity ?? null, sessionId);
1949
+ }
1950
+ function clearCachedM0M1(db, sessionId) {
1951
+ ensureSessionMetaRow(db, sessionId);
1952
+ const existingColumns = new Set(db.prepare("PRAGMA table_info(session_meta)").all().map((column) => column.name));
1953
+ const clears = [
1954
+ ["cached_m0_bytes", null],
1955
+ ["cached_m0_mural_data_url", null],
1956
+ ["cached_m0_mural_hash", null],
1957
+ ["cached_m1_bytes", null],
1958
+ ["cached_m0_project_memory_epoch", null],
1959
+ ["cached_m0_workspace_fingerprint", null],
1960
+ ["cached_m0_project_user_profile_version", null],
1961
+ ["cached_m0_max_compartment_seq", null],
1962
+ ["cached_m0_max_memory_id", null],
1963
+ ["cached_m0_max_mutation_id", null],
1964
+ ["cached_m0_max_memory_mutation_id", null],
1965
+ ["cached_m0_project_docs_hash", null],
1966
+ ["cached_m0_materialized_at", null],
1967
+ ["cached_m0_session_facts_version", null],
1968
+ ["cached_m0_upgrade_state", null],
1969
+ ["cached_m0_system_hash", null],
1970
+ ["cached_m0_tool_set_hash", null],
1971
+ ["cached_m0_model_key", null],
1972
+ ["cached_m0_project_identity", null],
1973
+ ["cached_m0_last_baseline_end_message_id", null],
1974
+ ["memory_block_cache", ""],
1975
+ ["memory_block_count", 0],
1976
+ ["memory_block_ids", ""]
1977
+ ];
1978
+ const setClauses = [];
1979
+ const values = [];
1980
+ for (const [column, value] of clears) {
1981
+ if (!existingColumns.has(column))
1982
+ continue;
1983
+ setClauses.push(`${column} = ?`);
1984
+ values.push(value);
1985
+ }
1986
+ if (setClauses.length === 0)
1987
+ return;
1988
+ db.prepare(`UPDATE session_meta SET ${setClauses.join(", ")} WHERE session_id = ?`).run(...values, sessionId);
1989
+ }
1990
+ function clearCachedM0(db, sessionId) {
1991
+ clearCachedM0M1(db, sessionId);
2184
1992
  }
2185
1993
 
2186
1994
  // src/features/magic-context/storage-replay-document.ts
@@ -4048,6 +3856,13 @@ function loadPostprocessReplaySnapshot(db, sessionId) {
4048
3856
  const todoPermissionDenied = row?.todo_permission_denied === 1 ? true : row?.todo_permission_denied === 0 ? false : null;
4049
3857
  const thinkingBindingRecoveryTarget = row?.thinking_binding_recovery_target;
4050
3858
  return {
3859
+ servedShapeRepair: (() => {
3860
+ try {
3861
+ return parseReplayDocument(row?.trailing_blank_decisions).servedShapeRepair;
3862
+ } catch {
3863
+ return;
3864
+ }
3865
+ })(),
4051
3866
  executeReclaim: executeReclaimFromReplay(row?.trailing_blank_decisions),
4052
3867
  staleReduceStrippedIds: new Set(parseStrippedBlob(row?.stale_reduce_stripped_ids)),
4053
3868
  processedImageStrippedIds: new Set(parseProcessedImageMarkers(row?.processed_image_stripped_ids).keys()),
@@ -4309,6 +4124,199 @@ function markDropLeavesReasoning(db, sessionId) {
4309
4124
  });
4310
4125
  }
4311
4126
 
4127
+ // src/features/magic-context/overflow-detection.ts
4128
+ var OVERFLOW_PATTERNS = [
4129
+ /prompt is too long/i,
4130
+ /input is too long for requested model/i,
4131
+ /exceeds the context window/i,
4132
+ /input token count.*exceeds the maximum/i,
4133
+ /maximum prompt length is \d+/i,
4134
+ /reduce the length of the messages/i,
4135
+ /maximum context length is \d+ tokens/i,
4136
+ /maximum model length is \d+/i,
4137
+ /exceeds the limit of \d+/i,
4138
+ /exceeds the available context size/i,
4139
+ /greater than the context length/i,
4140
+ /context window exceeds limit/i,
4141
+ /exceeded model token limit/i,
4142
+ /context[_ ]length[_ ]exceeded/i,
4143
+ /request entity too large/i,
4144
+ /context length is only \d+ tokens/i,
4145
+ /input length.*exceeds.*context length/i,
4146
+ /prompt too long; exceeded (?:max )?context length/i,
4147
+ /too large for model with \d+ maximum context length/i,
4148
+ /model_context_window_exceeded/i,
4149
+ /context size has been exceeded/i,
4150
+ /prepared prompt exceeds engine max_context/i,
4151
+ /prompt exceeds (?:the )?.{0,32}\bmax_context\b/i
4152
+ ];
4153
+ var LIMIT_EXTRACTION_PATTERNS = [
4154
+ { pattern: /maximum prompt length is (\d+)/i, provenance: "prompt_only" },
4155
+ {
4156
+ pattern: /maximum context length is (\d+) tokens?/i,
4157
+ provenance: "combined"
4158
+ },
4159
+ { pattern: /maximum model length is (\d+)/i, provenance: "combined" },
4160
+ { pattern: /context length is only (\d+) tokens?/i, provenance: "combined" },
4161
+ { pattern: /exceeds the limit of (\d+)/i, provenance: "unknown" },
4162
+ {
4163
+ pattern: /too large for model with (\d+) maximum context length/i,
4164
+ provenance: "combined"
4165
+ },
4166
+ { pattern: /context size[^0-9]{0,40}(\d{4,})\s*tokens?/i, provenance: "combined" },
4167
+ { pattern: /exceeds? the context length of (\d+)/i, provenance: "combined" },
4168
+ {
4169
+ pattern: />\s*(\d+)\s*(?:tokens?\s*)?(?:maximum|max|limit)\b/i,
4170
+ provenance: "prompt_only"
4171
+ },
4172
+ {
4173
+ pattern: /maximum number of tokens allowed\s*\(?\s*(\d+)/i,
4174
+ provenance: "prompt_only"
4175
+ },
4176
+ { pattern: /prepared prompt exceeds engine max_context\s+(\d+)/i, provenance: "unknown" },
4177
+ { pattern: /prompt exceeds (?:the )?.{0,32}\bmax_context\s+(\d+)/i, provenance: "unknown" },
4178
+ { pattern: /max(?:imum)?.*context.*?(\d+)/i, provenance: "unknown" }
4179
+ ];
4180
+ var MIN_PLAUSIBLE_LIMIT = 1024;
4181
+ var MAX_PLAUSIBLE_LIMIT = 1e7;
4182
+ function extractErrorMessage(error) {
4183
+ if (!error)
4184
+ return "";
4185
+ if (typeof error === "string")
4186
+ return error;
4187
+ if (typeof error === "object") {
4188
+ const obj = error;
4189
+ const nested = obj.error;
4190
+ if (nested && typeof nested.message === "string" && nested.message.length > 0) {
4191
+ return nested.message;
4192
+ }
4193
+ }
4194
+ if (error instanceof Error)
4195
+ return error.message;
4196
+ if (typeof error === "object") {
4197
+ const obj = error;
4198
+ if (typeof obj.message === "string")
4199
+ return obj.message;
4200
+ if (typeof obj.responseBody === "string")
4201
+ return obj.responseBody;
4202
+ try {
4203
+ return JSON.stringify(error);
4204
+ } catch {
4205
+ return String(error);
4206
+ }
4207
+ }
4208
+ return String(error);
4209
+ }
4210
+ function extractExplicitHttpStatus(error) {
4211
+ if (!error || typeof error !== "object")
4212
+ return;
4213
+ const seen = new Set;
4214
+ const queue = [error];
4215
+ while (queue.length > 0) {
4216
+ const current = queue.shift();
4217
+ if (!current || seen.has(current))
4218
+ continue;
4219
+ seen.add(current);
4220
+ const record = current;
4221
+ for (const key of ["status", "statusCode", "status_code"]) {
4222
+ const value = record[key];
4223
+ if (typeof value === "number")
4224
+ return value;
4225
+ }
4226
+ for (const value of Object.values(record)) {
4227
+ if (value && typeof value === "object")
4228
+ queue.push(value);
4229
+ }
4230
+ }
4231
+ return;
4232
+ }
4233
+ function detectLatestTurnThinkingMismatch(error) {
4234
+ const status = extractExplicitHttpStatus(error);
4235
+ return (status === undefined || status === 400) && /(?:thinking|redacted_thinking)[\s\S]*latest assistant (?:message|turn)[\s\S]*cannot be modified/i.test(extractErrorMessage(error));
4236
+ }
4237
+ var PREFIX_BOUND_THINKING_MODELS = [
4238
+ ["fable", 5, 1],
4239
+ ["opus", 5, 5],
4240
+ ["sonnet", 5, 5],
4241
+ ["haiku", 5, 5]
4242
+ ];
4243
+ var PREFIX_BOUND_THINKING_PATTERN = new RegExp(`(?:^|[-_.:/])(?:${PREFIX_BOUND_THINKING_MODELS.map(([family, major, minor]) => `${family}[-_.]?${major}[-_.]${minor}`).join("|")})(?:$|[-_.:/@])`, "i");
4244
+ function isPrefixBoundThinkingModel(_providerID, modelID) {
4245
+ if (!modelID)
4246
+ return false;
4247
+ return PREFIX_BOUND_THINKING_PATTERN.test(modelID);
4248
+ }
4249
+ function isFable51ThinkingBindingModel(providerID, modelID) {
4250
+ if (providerID?.toLowerCase() !== "anthropic" || !modelID)
4251
+ return false;
4252
+ return /(?:^|[-_.])fable[-_.]?5[-_.]1(?:$|[-_.])/i.test(modelID);
4253
+ }
4254
+ function detectOverflow(error) {
4255
+ const message = extractErrorMessage(error);
4256
+ if (!message) {
4257
+ return { isOverflow: false };
4258
+ }
4259
+ const hasStatus413 = /\b413\b/.test(message) && /(entity|payload|context|prompt)/i.test(message);
4260
+ let matched;
4261
+ for (const pattern of OVERFLOW_PATTERNS) {
4262
+ if (pattern.test(message)) {
4263
+ matched = pattern;
4264
+ break;
4265
+ }
4266
+ }
4267
+ if (!matched && !hasStatus413) {
4268
+ return { isOverflow: false };
4269
+ }
4270
+ const reportedLimit = parseReportedLimit(message);
4271
+ const reportedInputTokens = parseReportedInputTokens(message);
4272
+ return {
4273
+ isOverflow: true,
4274
+ reportedInputTokens,
4275
+ reportedLimit: reportedLimit?.value,
4276
+ reportedLimitProvenance: reportedLimit?.provenance,
4277
+ matchedPattern: matched?.source
4278
+ };
4279
+ }
4280
+ function parseReportedInputTokens(message) {
4281
+ if (!message)
4282
+ return;
4283
+ const patterns = [
4284
+ /prompt is too long:\s*(\d+)/i,
4285
+ /input token count\s*\(?\s*(\d+)/i,
4286
+ /input length\s*(\d+)/i,
4287
+ /prompt was\s*(\d+)/i,
4288
+ /messages resulted in\s*(\d+)\s*tokens?/i
4289
+ ];
4290
+ for (const pattern of patterns) {
4291
+ const raw = message.match(pattern)?.[1];
4292
+ if (!raw)
4293
+ continue;
4294
+ const value = Number.parseInt(raw, 10);
4295
+ if (Number.isFinite(value) && value > 0)
4296
+ return value;
4297
+ }
4298
+ return;
4299
+ }
4300
+ function parseReportedLimit(message) {
4301
+ if (!message)
4302
+ return;
4303
+ for (const { pattern, provenance } of LIMIT_EXTRACTION_PATTERNS) {
4304
+ const match = message.match(pattern);
4305
+ if (!match)
4306
+ continue;
4307
+ const raw = match[1];
4308
+ if (!raw)
4309
+ continue;
4310
+ const value = Number.parseInt(raw, 10);
4311
+ if (!Number.isFinite(value))
4312
+ continue;
4313
+ if (value < MIN_PLAUSIBLE_LIMIT || value > MAX_PLAUSIBLE_LIMIT)
4314
+ continue;
4315
+ return { value, provenance };
4316
+ }
4317
+ return;
4318
+ }
4319
+
4312
4320
  // src/hooks/magic-context/sentinel.ts
4313
4321
  var WHOLE_MESSAGE_PLACEHOLDER_TEXT = "[dropped]";
4314
4322
  function modelAcceptsEmptyContent(providerID) {
@@ -4519,6 +4527,120 @@ function createProcessedImageEligibility(messages, messageTagNumbers, options) {
4519
4527
  };
4520
4528
  }
4521
4529
 
4530
+ // src/hooks/magic-context/served-shape-repair.ts
4531
+ var sources = new WeakMap;
4532
+ function markServedShapeRepair(info, parts, requiresPermission = false) {
4533
+ const merged = new Map((sources.get(info) ?? []).map((part) => [part.index, part]));
4534
+ for (const part of parts)
4535
+ merged.set(part.index, { ...part, requiresPermission });
4536
+ if (merged.size)
4537
+ sources.set(info, [...merged.values()]);
4538
+ }
4539
+ function parseDecisions(value) {
4540
+ if (value === undefined)
4541
+ return {};
4542
+ if (!value || typeof value !== "object" || Array.isArray(value))
4543
+ throw new Error("Invalid served-shape replay decisions");
4544
+ for (const entries of Object.values(value)) {
4545
+ if (!Array.isArray(entries) || entries.some((entry) => !entry || !Number.isSafeInteger(entry.index) || entry.index < 0 || typeof entry.text !== "string" || !entry.text.trim()))
4546
+ throw new Error("Invalid served-shape text decision");
4547
+ }
4548
+ return value;
4549
+ }
4550
+ function readDecisions(db, sessionID) {
4551
+ return parseDecisions(readReplayEnvelope(db, sessionID).servedShapeRepair);
4552
+ }
4553
+ function prepareServedShapeRepair(db, sessionID, messages, readPreviouslyServed, snapshot) {
4554
+ let decisions = snapshot ? parseDecisions(snapshot.servedShapeRepair) : readDecisions(db, sessionID);
4555
+ const collectPending = () => {
4556
+ const pending = {};
4557
+ for (const message of messages) {
4558
+ const id = message.info.id;
4559
+ if (typeof id !== "string")
4560
+ continue;
4561
+ const candidates = (sources.get(message.info) ?? []).filter(({ index }) => !decisions[id]?.some((part) => part.index === index));
4562
+ if (candidates.length)
4563
+ pending[id] = candidates;
4564
+ }
4565
+ return pending;
4566
+ };
4567
+ const pending = collectPending();
4568
+ let previouslyServed = null;
4569
+ try {
4570
+ const evidence = Object.keys(pending).length ? readPreviouslyServed?.() : null;
4571
+ if (Array.isArray(evidence))
4572
+ previouslyServed = evidence;
4573
+ } catch {}
4574
+ const provenUnserved = new Set;
4575
+ let needsRebuild = false;
4576
+ for (const id of Object.keys(pending)) {
4577
+ if (!previouslyServed)
4578
+ continue;
4579
+ const matches = previouslyServed.filter((value) => {
4580
+ if (!value || typeof value !== "object")
4581
+ return false;
4582
+ const message = value;
4583
+ return message.info?.id === id;
4584
+ });
4585
+ if (!matches.length)
4586
+ provenUnserved.add(id);
4587
+ const previous = matches.length === 1 ? matches[0] : undefined;
4588
+ needsRebuild ||= previous !== undefined && Array.isArray(previous.parts) && pending[id].some(({ index }) => {
4589
+ const part = previous.parts[index];
4590
+ return part?.type === "text" && part.text === "";
4591
+ });
4592
+ }
4593
+ return {
4594
+ pending: Object.keys(pending).length > 0,
4595
+ needsRebuild,
4596
+ get messageIDs() {
4597
+ return new Set(Object.keys(decisions));
4598
+ },
4599
+ apply(allowed) {
4600
+ const eligible = {};
4601
+ for (const [id, candidates] of Object.entries(collectPending())) {
4602
+ const parts = candidates.filter(({ requiresPermission }) => allowed || !requiresPermission && !needsRebuild && provenUnserved.has(id)).map(({ index, text }) => ({ index, text }));
4603
+ if (parts.length)
4604
+ eligible[id] = parts;
4605
+ }
4606
+ if (Object.keys(eligible).length) {
4607
+ if (!updateReplayDocument(db, sessionID, (doc) => {
4608
+ doc.version = 2;
4609
+ const current = parseDecisions(doc.servedShapeRepair);
4610
+ for (const [id, parts] of Object.entries(eligible)) {
4611
+ current[id] = [
4612
+ ...current[id] ?? [],
4613
+ ...parts.filter(({ index }) => !current[id]?.some((part) => part.index === index))
4614
+ ];
4615
+ }
4616
+ doc.servedShapeRepair = current;
4617
+ return true;
4618
+ }))
4619
+ throw new Error("Could not persist served-shape repair");
4620
+ decisions = readDecisions(db, sessionID);
4621
+ }
4622
+ let changed = 0;
4623
+ for (const message of messages) {
4624
+ const id = message.info.id;
4625
+ if (typeof id !== "string")
4626
+ continue;
4627
+ for (const decision of decisions[id] ?? []) {
4628
+ const part = message.parts[decision.index];
4629
+ if (part?.type !== "text")
4630
+ continue;
4631
+ if (typeof part.text !== "string" || stripPersistedAssistantText(part.text).trim())
4632
+ continue;
4633
+ if (part.text !== decision.text) {
4634
+ part.text = decision.text;
4635
+ changed++;
4636
+ }
4637
+ }
4638
+ }
4639
+ return changed;
4640
+ }
4641
+ };
4642
+ }
4643
+
4522
4644
  // src/hooks/magic-context/strip-content.ts
4523
4645
  var MARKER_ONLY_PATTERN = /^(?:(?:§\d+§|\[dropped(?: §\d+§)?\]|\[cleared\])\s*)+$/;
4524
4646
  function isMarkerOnlyText(text) {
@@ -4673,9 +4795,11 @@ function replayClearedReasoning(messages, reasoningByMessage, messageTagNumbers,
4673
4795
  }
4674
4796
  return cleared;
4675
4797
  }
4676
- function replayStrippedInlineThinking(messages, messageTagNumbers, persistedWatermark) {
4798
+ function replayStrippedInlineThinking(messages, messageTagNumbers, persistedWatermark, captureRepair = false) {
4677
4799
  if (persistedWatermark <= 0)
4678
4800
  return 0;
4801
+ if (captureRepair)
4802
+ captureInlineThinkingRepairs(messages, messageTagNumbers, persistedWatermark);
4679
4803
  let stripped = 0;
4680
4804
  for (const message of messages) {
4681
4805
  if (message.info.role !== "assistant")
@@ -4749,8 +4873,24 @@ function stripClearedReasoning(messages) {
4749
4873
  return stripped;
4750
4874
  }
4751
4875
  var INLINE_THINKING_PATTERN = /<(?:thinking|think)>[\s\S]*?<\/(?:thinking|think)>\s*/g;
4752
- function stripInlineThinking(messages, messageTagNumbers, cutoff) {
4876
+ function captureInlineThinkingRepairs(messages, messageTagNumbers, watermark) {
4877
+ for (const message of messages) {
4878
+ const tag = messageTagNumbers.get(message) ?? 0;
4879
+ if (message.info.role !== "assistant" || tag <= 0 || tag > watermark)
4880
+ continue;
4881
+ for (const [index, part] of message.parts.entries()) {
4882
+ if (!isRecord(part) || part.type !== "text" || typeof part.text !== "string")
4883
+ continue;
4884
+ const cleaned = part.text.replace(INLINE_THINKING_PATTERN, "");
4885
+ if (cleaned !== part.text && !cleaned.trim())
4886
+ markServedShapeRepair(message.info, [{ index, text: "[cleared]" }], true);
4887
+ }
4888
+ }
4889
+ }
4890
+ function stripInlineThinking(messages, messageTagNumbers, cutoff, captureRepair = false) {
4753
4891
  const ageCutoff = cutoff;
4892
+ if (captureRepair)
4893
+ captureInlineThinkingRepairs(messages, messageTagNumbers, cutoff);
4754
4894
  let stripped = 0;
4755
4895
  for (const message of messages) {
4756
4896
  if (message.info.role !== "assistant")
@@ -7220,4 +7360,4 @@ function readRawSessionSeedTail2(sessionId, boundaryId, onQuery) {
7220
7360
  });
7221
7361
  }
7222
7362
 
7223
- export { detectLatestTurnThinkingMismatch, isPrefixBoundThinkingModel, detectOverflow, SESSION_META_SELECT_COLUMNS, META_COLUMNS, BOOLEAN_META_KEYS, NULL_BIND_META_KEYS, isSessionMetaRow, getDefaultSessionMeta, ensureSessionMetaRow, toSessionMeta, persistCachedM0, clearCachedM0M1, clearCachedM0, expandToolPart, markHostUnservedRow, retreatPastHostUnservedRows, snapTerminalCompartmentToServedRow, isHostRenderedSystemMessage, markCoveredInstructionRow, isCoveredInstructionRow, takeCoveredInstructionRows, cloneKeepingCoveredInstructionMarks, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, stripSystemInjection, isTextPart, isToolPartWithOutput, isFilePart, toolPartHasUserAnswer, DreamTokenBudgetExceeded, TOKEN_BUDGET_FINALIZE_MESSAGE, registerBudgetFinalizeChild, releaseBudgetFinalizeChild, createDreamTokenBudget, TOOL_USE_DISABLED, needsBlockedRead, withBlockedReadInstruction, rememberV1HiddenRun, recordToolParameters, createDroppedInputGuard, assertExecutableToolInput, isEditTool, MERGED_REASONING_PARTS_PREFIX, decodeMergedReasoningParts, MAX_EXECUTE_THRESHOLD, escalationBands, HYGIENE_PROVIDER_UNITS_VERSION, sessionDecisionCalibration, transitionSessionHygieneUnits, readEpochFloorSnapshot, getProtectionWindowForSession, withReplayEnvelopeSnapshot, invalidateReplayEnvelopeSnapshot, isRustMarkerAdmissionFenced, setRustMarkerAdmissionFence, ReplayDocumentError, readReplayEnvelope, readReplayDocument, updateReplayDocument, writeReplayDocument, isEmergencyRecoveryArmed, isProviderOverflowReconfirmed, isProviderOverflowFailClosedProven, getEmergencyRecoveryArmedAt, describeProtectedTailDrainBudgetSkip, loadPersistedUsage, loadProtectedTailMeta, markProtectedTailPolicyV3Seeded, recordProtectedTailPublicationFloor, recordProtectedTailNoEligibleHead, resetProtectedTailNoEligibleHead, getWrapupInProgressState, isWrapupInProgress, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, getProtectedTailDrainBudgetSkip, reserveProtectedTailDrainTokens, clearEmergencyDrainLatch, recordHistorianDrainFailure, clearHistorianDrainFailure, rollbackProtectedTailDrainReservation, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, setPersistedNoteNudgeTrigger, setPersistedNoteNudgeTriggerMessageId, getNoteNudgeAnchors, getAutoSearchHintDecisions, deliverNoteNudgeAtomic, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, queueAutoSearchHintDecisionRetirement, flushAutoSearchHintDecisionRetirement, getPersistedTodoPermissionDenied, setPersistedTodoPermissionDenied, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getNoteLastReadAt, setNoteLastReadAt, getHistorianFailureState, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, loadTransformPassStateSnapshot, recordOverflowDetected, recordDetectedContextLimit, clearEmergencyRecovery, clearDetectedContextLimit, getPersistedCompactionMarkerState, setPersistedCompactionMarkerState, getDeferredClearedCompactionMarkerState, retireDeferredClearedCompactionMarkerState, parseStrippedPlaceholderState, serializeStrippedPlaceholderState, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, THINKING_BINDING_STRIP_ORDER_END_MARKER, thinkingBindingRecoveryFrozenId, COMPACTION_OFF_THINKING_FROZEN_PREFIX, compactionOffThinkingFrozenId, compactionOffFrozenThinkingIds, getThinkingBindingRecoveryTarget, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getTrailingBlankDecisions, addTrailingBlankDecisions, demoteTrailingBlankKeepDecisions, getStaleReduceStrippedIds, addStaleReduceStrippedIds, getProcessedImageStrippedIds, parseProcessedImageMarkers, serializeProcessedImageMarkers, getProcessedImageMarkers, addProcessedImageStrippedIds, getPendingCompactionMarkerState, getCompactionMarkerHealth, loadPostprocessReplaySnapshot, setPendingCompactionMarkerState, replacePendingCompactionMarkerStateIf, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, logPiCompactionMarkerClear, setSessionWorkMetrics, getObservedEpochFloor, resolveEpochFloorForPass, getReasoningRemovalState, addRemovedReasoningIds, markDropLeavesReasoning, modelAcceptsEmptyContent, isAnthropicFamilyRoute, neutralizeDroppedReasoningPart, restoreNeutralizedReasoningPart, isNeutralizedReasoningPart, variantChangeBustsProviderCache, makeSentinel, makeWholeMessageSentinel, isSentinel, replaySentinelByMessageIds, isInActiveAnthropicTurn, activeAnthropicTurnStart, hasAnthropicReasoning, stripSystemInjectedMessages, stripDroppedPlaceholderMessages, replayClearedReasoning, replayStrippedInlineThinking, clearOldReasoning, stripClearedReasoning, stripInlineThinking, findReasoningBearingAssistantIds, findLatestAssistantReasoningMutationExemptMessage, snapshotTrailingBlankSourceDecisions, findTrailingBlankDecisionCandidates, applyFrozenTrailingBlankDecisions, stripReasoningFromAssistantIds, findMergedReasoningStripDecisions, stripReasoningFromMergedAssistants, getEmptyImageParts, stripProcessedImages, projectOpencodeReasoningBudgetCutoff, DEFAULT_KEEP_REASONING_TOKENS, resolveKeepReasoningTokens, reasoningStepCost, reasoningBudgetCutoff, reasoningTextAndOpaque, opencodeReasoningBudgetCutoff, describeFinalWireTail, estimatePartImageTokens, estimateMessageTokens, wireContentBytes, createFinalWireUsageTracker, estimateFinalWireInputTokens, estimateAppendedWireTokens, SKELETON_REAL_INPUT_MAX_BYTES, partHasCompletedResult, extractToolCallObservation, ToolMutationBatch, createToolDropTarget, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, setBoundedRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, RAW_MESSAGE_VISIT_PAGE_SIZE2, visitRawSessionMessages2, visitRawSessionMessagesAsync2, readRawSessionMessageRange2, prepareRawSessionOrdinals2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageOrdinalPage2, getRawSessionStoredMessageCount2, readRawSessionMessageIdOrdinalsForRange2, readRawSessionMessageIdOrdinals2, readRawSessionMessagePartsById2, readRawSessionBoundaryEndpoints2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, readRawSessionMessageById2, getRawSessionMessageCount2, RAW_SESSION_TAG_KEY_PAGE_SIZE2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, getProtectedTailStartOrdinal2, readSessionChunk2, getRawSessionMessageIdsThrough2, readRawSessionSeedTail2 };
7363
+ export { expandToolPart, markHostUnservedRow, retreatPastHostUnservedRows, snapTerminalCompartmentToServedRow, isHostRenderedSystemMessage, markCoveredInstructionRow, isCoveredInstructionRow, takeCoveredInstructionRows, cloneKeepingCoveredInstructionMarks, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, stripSystemInjection, isTextPart, isToolPartWithOutput, isFilePart, toolPartHasUserAnswer, DreamTokenBudgetExceeded, TOKEN_BUDGET_FINALIZE_MESSAGE, registerBudgetFinalizeChild, releaseBudgetFinalizeChild, createDreamTokenBudget, TOOL_USE_DISABLED, needsBlockedRead, withBlockedReadInstruction, rememberV1HiddenRun, recordToolParameters, createDroppedInputGuard, assertExecutableToolInput, isEditTool, MERGED_REASONING_PARTS_PREFIX, decodeMergedReasoningParts, MAX_EXECUTE_THRESHOLD, escalationBands, HYGIENE_PROVIDER_UNITS_VERSION, sessionDecisionCalibration, transitionSessionHygieneUnits, readEpochFloorSnapshot, getProtectionWindowForSession, SESSION_META_SELECT_COLUMNS, META_COLUMNS, BOOLEAN_META_KEYS, NULL_BIND_META_KEYS, isSessionMetaRow, getDefaultSessionMeta, ensureSessionMetaRow, toSessionMeta, persistCachedM0, clearCachedM0M1, clearCachedM0, withReplayEnvelopeSnapshot, invalidateReplayEnvelopeSnapshot, isRustMarkerAdmissionFenced, setRustMarkerAdmissionFence, ReplayDocumentError, readReplayEnvelope, readReplayDocument, updateReplayDocument, writeReplayDocument, isEmergencyRecoveryArmed, isProviderOverflowReconfirmed, isProviderOverflowFailClosedProven, getEmergencyRecoveryArmedAt, describeProtectedTailDrainBudgetSkip, loadPersistedUsage, loadProtectedTailMeta, markProtectedTailPolicyV3Seeded, recordProtectedTailPublicationFloor, recordProtectedTailNoEligibleHead, resetProtectedTailNoEligibleHead, getWrapupInProgressState, isWrapupInProgress, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, getProtectedTailDrainBudgetSkip, reserveProtectedTailDrainTokens, clearEmergencyDrainLatch, recordHistorianDrainFailure, clearHistorianDrainFailure, rollbackProtectedTailDrainReservation, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, setPersistedNoteNudgeTrigger, setPersistedNoteNudgeTriggerMessageId, getNoteNudgeAnchors, getAutoSearchHintDecisions, deliverNoteNudgeAtomic, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, queueAutoSearchHintDecisionRetirement, flushAutoSearchHintDecisionRetirement, getPersistedTodoPermissionDenied, setPersistedTodoPermissionDenied, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getNoteLastReadAt, setNoteLastReadAt, getHistorianFailureState, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, loadTransformPassStateSnapshot, recordOverflowDetected, recordDetectedContextLimit, clearEmergencyRecovery, clearDetectedContextLimit, getPersistedCompactionMarkerState, setPersistedCompactionMarkerState, getDeferredClearedCompactionMarkerState, retireDeferredClearedCompactionMarkerState, parseStrippedPlaceholderState, serializeStrippedPlaceholderState, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, THINKING_BINDING_STRIP_ORDER_END_MARKER, thinkingBindingRecoveryFrozenId, COMPACTION_OFF_THINKING_FROZEN_PREFIX, compactionOffThinkingFrozenId, compactionOffFrozenThinkingIds, getThinkingBindingRecoveryTarget, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getTrailingBlankDecisions, addTrailingBlankDecisions, demoteTrailingBlankKeepDecisions, getStaleReduceStrippedIds, addStaleReduceStrippedIds, getProcessedImageStrippedIds, parseProcessedImageMarkers, serializeProcessedImageMarkers, getProcessedImageMarkers, addProcessedImageStrippedIds, getPendingCompactionMarkerState, getCompactionMarkerHealth, loadPostprocessReplaySnapshot, setPendingCompactionMarkerState, replacePendingCompactionMarkerStateIf, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, logPiCompactionMarkerClear, setSessionWorkMetrics, getObservedEpochFloor, resolveEpochFloorForPass, getReasoningRemovalState, addRemovedReasoningIds, markDropLeavesReasoning, detectLatestTurnThinkingMismatch, isPrefixBoundThinkingModel, detectOverflow, modelAcceptsEmptyContent, isAnthropicFamilyRoute, neutralizeDroppedReasoningPart, restoreNeutralizedReasoningPart, isNeutralizedReasoningPart, variantChangeBustsProviderCache, makeSentinel, makeWholeMessageSentinel, isSentinel, replaySentinelByMessageIds, isInActiveAnthropicTurn, activeAnthropicTurnStart, hasAnthropicReasoning, markServedShapeRepair, prepareServedShapeRepair, stripSystemInjectedMessages, stripDroppedPlaceholderMessages, replayClearedReasoning, replayStrippedInlineThinking, clearOldReasoning, stripClearedReasoning, captureInlineThinkingRepairs, stripInlineThinking, findReasoningBearingAssistantIds, findLatestAssistantReasoningMutationExemptMessage, snapshotTrailingBlankSourceDecisions, findTrailingBlankDecisionCandidates, applyFrozenTrailingBlankDecisions, stripReasoningFromAssistantIds, findMergedReasoningStripDecisions, stripReasoningFromMergedAssistants, getEmptyImageParts, stripProcessedImages, projectOpencodeReasoningBudgetCutoff, DEFAULT_KEEP_REASONING_TOKENS, resolveKeepReasoningTokens, reasoningStepCost, reasoningBudgetCutoff, reasoningTextAndOpaque, opencodeReasoningBudgetCutoff, describeFinalWireTail, estimatePartImageTokens, estimateMessageTokens, wireContentBytes, createFinalWireUsageTracker, estimateFinalWireInputTokens, estimateAppendedWireTokens, SKELETON_REAL_INPUT_MAX_BYTES, partHasCompletedResult, extractToolCallObservation, ToolMutationBatch, createToolDropTarget, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, setBoundedRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, RAW_MESSAGE_VISIT_PAGE_SIZE2, visitRawSessionMessages2, visitRawSessionMessagesAsync2, readRawSessionMessageRange2, prepareRawSessionOrdinals2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageOrdinalPage2, getRawSessionStoredMessageCount2, readRawSessionMessageIdOrdinalsForRange2, readRawSessionMessageIdOrdinals2, readRawSessionMessagePartsById2, readRawSessionBoundaryEndpoints2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, readRawSessionMessageById2, getRawSessionMessageCount2, RAW_SESSION_TAG_KEY_PAGE_SIZE2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, getProtectedTailStartOrdinal2, readSessionChunk2, getRawSessionMessageIdsThrough2, readRawSessionSeedTail2 };