@retaw106/mmc-oc 2.4.0 → 2.6.0

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/{context-c72brxe5.js → context-cfbjrr0n.js} +1636 -513
  2. package/dist/features/magic-context/compaction-marker.d.ts +34 -6
  3. package/dist/features/magic-context/compaction-marker.d.ts.map +1 -1
  4. package/dist/features/magic-context/message-index-async.d.ts +6 -2
  5. package/dist/features/magic-context/message-index-async.d.ts.map +1 -1
  6. package/dist/features/magic-context/message-index.d.ts +0 -26
  7. package/dist/features/magic-context/message-index.d.ts.map +1 -1
  8. package/dist/features/magic-context/search.d.ts +5 -1
  9. package/dist/features/magic-context/search.d.ts.map +1 -1
  10. package/dist/features/magic-context/session-decision-calibration.d.ts.map +1 -1
  11. package/dist/features/magic-context/session-retention.fixture.d.ts +4 -0
  12. package/dist/features/magic-context/session-retention.fixture.d.ts.map +1 -0
  13. package/dist/features/magic-context/storage-db.d.ts +2 -0
  14. package/dist/features/magic-context/storage-db.d.ts.map +1 -1
  15. package/dist/features/magic-context/storage-host-instruction-retention.d.ts +4 -0
  16. package/dist/features/magic-context/storage-host-instruction-retention.d.ts.map +1 -0
  17. package/dist/features/magic-context/storage-meta-persisted.d.ts +21 -1
  18. package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
  19. package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
  20. package/dist/features/magic-context/storage-meta-shared.d.ts +2 -2
  21. package/dist/features/magic-context/storage-meta-shared.d.ts.map +1 -1
  22. package/dist/features/magic-context/storage-native-replay.d.ts +2 -0
  23. package/dist/features/magic-context/storage-native-replay.d.ts.map +1 -1
  24. package/dist/features/magic-context/storage-session-tables.d.ts +5 -4
  25. package/dist/features/magic-context/storage-session-tables.d.ts.map +1 -1
  26. package/dist/features/magic-context/tool-definition-tokens.d.ts +113 -58
  27. package/dist/features/magic-context/tool-definition-tokens.d.ts.map +1 -1
  28. package/dist/features/magic-context/work-metrics.d.ts +5 -3
  29. package/dist/features/magic-context/work-metrics.d.ts.map +1 -1
  30. package/dist/hooks/magic-context/apply-operations.d.ts +32 -0
  31. package/dist/hooks/magic-context/apply-operations.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/ask-answer-notice.d.ts +25 -0
  33. package/dist/hooks/magic-context/ask-answer-notice.d.ts.map +1 -0
  34. package/dist/hooks/magic-context/auto-search-hint.d.ts +3 -0
  35. package/dist/hooks/magic-context/auto-search-hint.d.ts.map +1 -1
  36. package/dist/hooks/magic-context/auto-search-runner.d.ts.map +1 -1
  37. package/dist/hooks/magic-context/channel2-delivery.d.ts.map +1 -1
  38. package/dist/hooks/magic-context/compaction-marker-manager.d.ts +5 -5
  39. package/dist/hooks/magic-context/compaction-marker-manager.d.ts.map +1 -1
  40. package/dist/hooks/magic-context/compaction-marker-publication.d.ts +14 -0
  41. package/dist/hooks/magic-context/compaction-marker-publication.d.ts.map +1 -0
  42. package/dist/hooks/magic-context/compaction-off-transition.d.ts.map +1 -1
  43. package/dist/hooks/magic-context/compartment-runner-historian.d.ts +2 -2
  44. package/dist/hooks/magic-context/compartment-runner-incremental.d.ts +11 -0
  45. package/dist/hooks/magic-context/compartment-runner-incremental.d.ts.map +1 -1
  46. package/dist/hooks/magic-context/compartment-runner-validation.d.ts.map +1 -1
  47. package/dist/hooks/magic-context/compartment-trigger.d.ts.map +1 -1
  48. package/dist/hooks/magic-context/diagnostic-log-state.d.ts +4 -0
  49. package/dist/hooks/magic-context/diagnostic-log-state.d.ts.map +1 -0
  50. package/dist/hooks/magic-context/emergency-drop.d.ts.map +1 -1
  51. package/dist/hooks/magic-context/file-token-coverage.d.ts +11 -0
  52. package/dist/hooks/magic-context/file-token-coverage.d.ts.map +1 -0
  53. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +11 -3
  54. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
  55. package/dist/hooks/magic-context/historian-history-wait.d.ts +16 -0
  56. package/dist/hooks/magic-context/historian-history-wait.d.ts.map +1 -0
  57. package/dist/hooks/magic-context/history-boundary-repair.d.ts +1 -1
  58. package/dist/hooks/magic-context/history-boundary-repair.d.ts.map +1 -1
  59. package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
  60. package/dist/hooks/magic-context/host-served-rows.d.ts +10 -0
  61. package/dist/hooks/magic-context/host-served-rows.d.ts.map +1 -1
  62. package/dist/hooks/magic-context/inject-compartments.d.ts +1 -1
  63. package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
  64. package/dist/hooks/magic-context/lkg-measured-request.d.ts.map +1 -1
  65. package/dist/hooks/magic-context/lkg-recovery-fence.d.ts.map +1 -1
  66. package/dist/hooks/magic-context/lkg-recovery-prefix.d.ts +3 -1
  67. package/dist/hooks/magic-context/lkg-recovery-prefix.d.ts.map +1 -1
  68. package/dist/hooks/magic-context/lkg-replay-fit.d.ts.map +1 -1
  69. package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -1
  70. package/dist/hooks/magic-context/lkg-slot.d.ts +3 -1
  71. package/dist/hooks/magic-context/lkg-slot.d.ts.map +1 -1
  72. package/dist/hooks/magic-context/nudge-evaluation-log.d.ts +3 -0
  73. package/dist/hooks/magic-context/nudge-evaluation-log.d.ts.map +1 -0
  74. package/dist/hooks/magic-context/pass-outcome.d.ts +2 -0
  75. package/dist/hooks/magic-context/pass-outcome.d.ts.map +1 -1
  76. package/dist/hooks/magic-context/pending-drop-eligibility.d.ts.map +1 -1
  77. package/dist/hooks/magic-context/protected-tail-boundary.d.ts +2 -0
  78. package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
  79. package/dist/hooks/magic-context/read-session-chunk.d.ts.map +1 -1
  80. package/dist/hooks/magic-context/read-session-db.d.ts +8 -2
  81. package/dist/hooks/magic-context/read-session-db.d.ts.map +1 -1
  82. package/dist/hooks/magic-context/read-session-raw.d.ts +20 -0
  83. package/dist/hooks/magic-context/read-session-raw.d.ts.map +1 -1
  84. package/dist/hooks/magic-context/recomp-orchestrator.d.ts +3 -3
  85. package/dist/hooks/magic-context/recomp-orchestrator.d.ts.map +1 -1
  86. package/dist/hooks/magic-context/signed-thinking-fit-snapshot.d.ts +42 -0
  87. package/dist/hooks/magic-context/signed-thinking-fit-snapshot.d.ts.map +1 -0
  88. package/dist/hooks/magic-context/system-date-refresh.d.ts +3 -2
  89. package/dist/hooks/magic-context/system-date-refresh.d.ts.map +1 -1
  90. package/dist/hooks/magic-context/system-prompt-hash.d.ts.map +1 -1
  91. package/dist/hooks/magic-context/system-prompt-sticky-date.d.ts +4 -0
  92. package/dist/hooks/magic-context/system-prompt-sticky-date.d.ts.map +1 -0
  93. package/dist/hooks/magic-context/tag-content-primitives.d.ts +1 -1
  94. package/dist/hooks/magic-context/tag-content-primitives.d.ts.map +1 -1
  95. package/dist/hooks/magic-context/tag-messages.d.ts +17 -2
  96. package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
  97. package/dist/hooks/magic-context/text-complete.d.ts +1 -1
  98. package/dist/hooks/magic-context/text-complete.d.ts.map +1 -1
  99. package/dist/hooks/magic-context/tool-drop-target.d.ts +3 -2
  100. package/dist/hooks/magic-context/tool-drop-target.d.ts.map +1 -1
  101. package/dist/hooks/magic-context/transform-compartment-phase.d.ts +1 -0
  102. package/dist/hooks/magic-context/transform-compartment-phase.d.ts.map +1 -1
  103. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +6 -0
  104. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  105. package/dist/hooks/magic-context/transform-stage-logger.d.ts +3 -0
  106. package/dist/hooks/magic-context/transform-stage-logger.d.ts.map +1 -1
  107. package/dist/hooks/magic-context/transform.d.ts +4 -2
  108. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  109. package/dist/{index-27ddyx39.js → index-1np16gxq.js} +1 -1
  110. package/dist/{index-ykq24x1n.js → index-8rmt6s4f.js} +78 -32
  111. package/dist/{index-dwh99z66.js → index-pte3cfs9.js} +194 -5
  112. package/dist/{index-r7s7d350.js → index-s3ebcy63.js} +1 -1
  113. package/dist/{index-48e9znrc.js → index-t0zx3tgz.js} +481 -109
  114. package/dist/{index-kahkxcqw.js → index-xk1dz6q9.js} +870 -315
  115. package/dist/index.js +2 -2
  116. package/dist/{message-time-backfill-j96x1sab.js → message-time-backfill-g9py24yt.js} +1 -1
  117. package/dist/plugin/rpc-handlers.d.ts.map +1 -1
  118. package/dist/plugin/subagent-tool-policy.d.ts +4 -0
  119. package/dist/plugin/subagent-tool-policy.d.ts.map +1 -1
  120. package/dist/{read-session-chunk-t8bg6wkz.js → read-session-chunk-2s8x6xps.js} +3 -3
  121. package/dist/{recomp-orchestrator-k760qy1n.js → recomp-orchestrator-51xyaexp.js} +6 -6
  122. package/dist/{send-session-notification-njjf3b4s.js → send-session-notification-vdppp5x6.js} +3 -3
  123. package/dist/shared/model-suggestion-retry.d.ts +2 -0
  124. package/dist/shared/model-suggestion-retry.d.ts.map +1 -1
  125. package/dist/shared/rpc-discovery-cleanup.d.ts +3 -0
  126. package/dist/shared/rpc-discovery-cleanup.d.ts.map +1 -0
  127. package/dist/shared/rpc-server.d.ts +2 -0
  128. package/dist/shared/rpc-server.d.ts.map +1 -1
  129. package/dist/shared/search-text.d.ts +8 -0
  130. package/dist/shared/search-text.d.ts.map +1 -0
  131. package/dist/shared/sqlite.d.ts +6 -1
  132. package/dist/shared/sqlite.d.ts.map +1 -1
  133. package/dist/shared/storage-file-mutex.d.ts.map +1 -1
  134. package/dist/shared/system-directive.d.ts +2 -0
  135. package/dist/shared/system-directive.d.ts.map +1 -1
  136. package/dist/shared/tag-transcript.d.ts +2 -0
  137. package/dist/shared/tag-transcript.d.ts.map +1 -1
  138. package/dist/shared/transcript.d.ts +3 -1
  139. package/dist/shared/transcript.d.ts.map +1 -1
  140. package/dist/tools/ctx-memory/tools.d.ts.map +1 -1
  141. package/dist/tools/ctx-note/tools.d.ts.map +1 -1
  142. package/dist/tools/parameter-descriptions.d.ts +6 -6
  143. package/dist/tools/parameter-descriptions.d.ts.map +1 -1
  144. package/dist/v2/fold/native-replay.d.ts +11 -2
  145. package/dist/v2/fold/native-replay.d.ts.map +1 -1
  146. package/dist/v2/fold/policy.d.ts.map +1 -1
  147. package/dist/v2/fold/restore.d.ts.map +1 -1
  148. package/dist/v2/hooks/context.d.ts +4 -0
  149. package/dist/v2/hooks/context.d.ts.map +1 -1
  150. package/dist/v2/hooks/generate.d.ts +36 -1
  151. package/dist/v2/hooks/generate.d.ts.map +1 -1
  152. package/dist/v2/hooks/lkg-system.d.ts +35 -4
  153. package/dist/v2/hooks/lkg-system.d.ts.map +1 -1
  154. package/dist/v2/hooks/payload.d.ts +10 -1
  155. package/dist/v2/hooks/payload.d.ts.map +1 -1
  156. package/dist/v2/hooks/store.d.ts +2 -0
  157. package/dist/v2/hooks/store.d.ts.map +1 -1
  158. package/dist/v2/hooks/types.d.ts +12 -0
  159. package/dist/v2/hooks/types.d.ts.map +1 -1
  160. package/dist/v2/message-times.d.ts +17 -0
  161. package/dist/v2/message-times.d.ts.map +1 -0
  162. package/dist/v2/server.js +4692 -2394
  163. package/dist/v2/store-reader.d.ts +21 -0
  164. package/dist/v2/store-reader.d.ts.map +1 -1
  165. package/dist/web/main.js +307 -12
  166. package/dist/{wrapup-orchestrator-b2gswatj.js → wrapup-orchestrator-g0mww1h4.js} +6 -6
  167. package/package.json +1 -1
  168. package/src/config/latch-permanence-guard.test.ts +4 -5
  169. package/src/shared/model-suggestion-retry.test.ts +52 -0
  170. package/src/shared/model-suggestion-retry.ts +8 -3
  171. package/src/shared/prompt-surface-a1-golden.md +6 -7
  172. package/src/shared/rpc-discovery-cleanup.test.ts +125 -0
  173. package/src/shared/rpc-discovery-cleanup.ts +57 -0
  174. package/src/shared/rpc-server.ts +36 -13
  175. package/src/shared/search-text.test.ts +30 -0
  176. package/src/shared/search-text.ts +62 -0
  177. package/src/shared/sqlite-transaction-mode-guard.test.ts +2 -0
  178. package/src/shared/sqlite-turn-deadline.test.ts +286 -0
  179. package/src/shared/sqlite.ts +237 -9
  180. package/src/shared/storage-file-mutex.test.ts +51 -2
  181. package/src/shared/storage-file-mutex.ts +11 -1
  182. package/src/shared/system-directive.test.ts +37 -0
  183. package/src/shared/system-directive.ts +19 -1
  184. package/src/shared/tag-transcript.ts +63 -8
  185. package/src/shared/transcript.ts +1 -1
  186. package/src/shared/write-transaction-attribution.test.ts +1 -6
  187. package/dist/features/magic-context/message-index-sweep.d.ts +0 -11
  188. package/dist/features/magic-context/message-index-sweep.d.ts.map +0 -1
@@ -20,6 +20,7 @@ import {
20
20
  peelLeadingMcTagNotation,
21
21
  isRecord,
22
22
  removeSystemReminders,
23
+ removeNestedBlocks,
23
24
  hasMeaningfulUserText2,
24
25
  extractTexts2,
25
26
  estimateTokens,
@@ -42,12 +43,16 @@ import {
42
43
  retreatPastHostUnservedRows,
43
44
  snapTerminalCompartmentToServedRow,
44
45
  isHostRenderedSystemMessage,
46
+ markCoveredInstructionRow,
47
+ isCoveredInstructionRow,
45
48
  completedToolArcCrossesBoundary,
46
49
  buildToolArcs,
47
50
  fenceBoundaryForCompletedToolArcs,
48
51
  fenceBoundaryForToolArcs,
49
52
  buildTrueRawTokenIndex,
50
53
  computeRawRangeFingerprint,
54
+ isFilePart,
55
+ BoundedSessionMap,
51
56
  setDatabase,
52
57
  loadToolDefinitionMeasurements,
53
58
  getMeasuredToolDefinitionTokens,
@@ -67,20 +72,22 @@ import {
67
72
  getRawSessionTagKeysThrough2,
68
73
  getLegacyProtectedTailStartOrdinal2,
69
74
  readSessionChunk2
70
- } from "./index-48e9znrc.js";
75
+ } from "./index-t0zx3tgz.js";
71
76
  import {
72
77
  inRootAsyncContext,
73
78
  registerSlowWriteReporter,
74
79
  registerSqliteDiagnosticSink,
75
80
  detectSqliteRuntime,
76
81
  Database,
82
+ createSqliteTurnDatabase,
83
+ hasSqliteTurnDeadlineRouting,
77
84
  prepareCachedStatement,
78
85
  withSqliteBackgroundWriter,
79
86
  withoutSqliteTransformPass,
80
87
  isTransientSqliteError,
81
88
  beginSqliteWriterAsync,
82
89
  withPrivilegedWriter
83
- } from "./index-dwh99z66.js";
90
+ } from "./index-pte3cfs9.js";
84
91
  import {
85
92
  piModelRefToCanonical,
86
93
  modelRefLookupOrder,
@@ -98,9 +105,11 @@ import {
98
105
  openCodeDbPathExists,
99
106
  claimOpenCodeDbDiagnosticOnce,
100
107
  isStrictGapHealingMessage,
108
+ dropRawSessionOrdinalCheckpoints,
109
+ openCodeDbExists,
101
110
  withReadOnlySessionDb,
102
111
  getMessageTimesFromOpenCodeDb
103
- } from "./index-ykq24x1n.js";
112
+ } from "./index-8rmt6s4f.js";
104
113
  import {
105
114
  shouldEnforcePrivateStoragePermissions,
106
115
  ensureStorageDirectorySync,
@@ -125,7 +134,7 @@ import {
125
134
  getErrorMessage,
126
135
  describeError,
127
136
  sendStatusNotification2
128
- } from "./index-r7s7d350.js";
137
+ } from "./index-s3ebcy63.js";
129
138
  import {
130
139
  logSlowWriteTransaction,
131
140
  WriteTransactionSteps
@@ -1454,51 +1463,6 @@ function scheduleAfterBootQuiet(task, additionalDelayMs = 0) {
1454
1463
  return timer;
1455
1464
  }
1456
1465
 
1457
- // src/shared/bounded-session-map.ts
1458
- class BoundedSessionMap {
1459
- maxEntries;
1460
- store = new Map;
1461
- constructor(maxEntries) {
1462
- if (!Number.isFinite(maxEntries) || maxEntries < 1) {
1463
- throw new Error(`BoundedSessionMap: maxEntries must be >= 1, got ${maxEntries}`);
1464
- }
1465
- this.maxEntries = maxEntries;
1466
- }
1467
- get(sessionId) {
1468
- const value = this.store.get(sessionId);
1469
- if (value === undefined)
1470
- return;
1471
- this.store.delete(sessionId);
1472
- this.store.set(sessionId, value);
1473
- return value;
1474
- }
1475
- peek(sessionId) {
1476
- return this.store.get(sessionId);
1477
- }
1478
- has(sessionId) {
1479
- return this.store.has(sessionId);
1480
- }
1481
- set(sessionId, value) {
1482
- if (this.store.has(sessionId)) {
1483
- this.store.delete(sessionId);
1484
- } else if (this.store.size >= this.maxEntries) {
1485
- const oldest = this.store.keys().next().value;
1486
- if (oldest !== undefined)
1487
- this.store.delete(oldest);
1488
- }
1489
- this.store.set(sessionId, value);
1490
- }
1491
- delete(sessionId) {
1492
- return this.store.delete(sessionId);
1493
- }
1494
- clear() {
1495
- this.store.clear();
1496
- }
1497
- get size() {
1498
- return this.store.size;
1499
- }
1500
- }
1501
-
1502
1466
  // src/shared/rpc-utils.ts
1503
1467
  import { execFile, execFileSync as execFileSync2 } from "node:child_process";
1504
1468
  import { createHash as createHash4 } from "node:crypto";
@@ -2285,76 +2249,6 @@ function isCompartmentLeaseHeld(db, sessionId, holderId) {
2285
2249
  return row != null;
2286
2250
  }
2287
2251
 
2288
- // src/features/magic-context/compression-depth-storage.ts
2289
- var incrementDepthStatements = new WeakMap;
2290
- var totalDepthStatements = new WeakMap;
2291
- var maxDepthStatements = new WeakMap;
2292
- var clearDepthStatements = new WeakMap;
2293
- function getClearDepthStatement(db) {
2294
- let stmt = clearDepthStatements.get(db);
2295
- if (!stmt) {
2296
- stmt = db.prepare("DELETE FROM compression_depth WHERE session_id = ?");
2297
- clearDepthStatements.set(db, stmt);
2298
- }
2299
- return stmt;
2300
- }
2301
- function clearCompressionDepth(db, sessionId) {
2302
- getClearDepthStatement(db).run(sessionId);
2303
- }
2304
- function clearCompressionDepthRange(db, sessionId, startOrdinal, endOrdinal) {
2305
- if (endOrdinal < startOrdinal) {
2306
- return;
2307
- }
2308
- db.prepare("DELETE FROM compression_depth WHERE session_id = ? AND message_ordinal BETWEEN ? AND ?").run(sessionId, startOrdinal, endOrdinal);
2309
- }
2310
-
2311
- // src/features/magic-context/no-content-compartment.ts
2312
- function isNoContentCompartment(row) {
2313
- return [row.title, row.content, row.p1, row.p2, row.p3, row.p4].every((value) => !value);
2314
- }
2315
-
2316
- // src/features/magic-context/storage-m0-mutation-log.ts
2317
- var M0_MUTATION_TYPES = new Set([
2318
- "compartment_delete",
2319
- "compartment_merge",
2320
- "recomp_boundary_change",
2321
- "compartment_upgrade"
2322
- ]);
2323
- function assertMutationType(mutationType) {
2324
- if (!M0_MUTATION_TYPES.has(mutationType)) {
2325
- throw new Error(`Invalid m0 mutation type: ${mutationType}`);
2326
- }
2327
- }
2328
- function toM0Mutation(row) {
2329
- return {
2330
- id: row.id,
2331
- sessionId: row.session_id,
2332
- mutationType: row.mutation_type,
2333
- targetId: row.target_id,
2334
- queuedAt: row.queued_at
2335
- };
2336
- }
2337
- function queueM0Mutation(db, input) {
2338
- assertMutationType(input.mutationType);
2339
- const result = db.prepare(`INSERT INTO m0_mutation_log (session_id, mutation_type, target_id, queued_at)
2340
- VALUES (?, ?, ?, ?)`).run(input.sessionId, input.mutationType, input.targetId ?? null, input.queuedAt ?? Date.now());
2341
- const row = getM0Mutation(db, Number(result.lastInsertRowid));
2342
- if (!row) {
2343
- throw new Error("Failed to load queued m0 mutation");
2344
- }
2345
- return row;
2346
- }
2347
- function getM0Mutation(db, id) {
2348
- const row = db.prepare(`SELECT id, session_id, mutation_type, target_id, queued_at
2349
- FROM m0_mutation_log
2350
- WHERE id = ?`).get(id);
2351
- return row ? toM0Mutation(row) : null;
2352
- }
2353
- function getMaxM0MutationId(db, sessionId) {
2354
- const row = db.prepare("SELECT MAX(id) AS max_id FROM m0_mutation_log WHERE session_id = ?").get(sessionId);
2355
- return row?.max_id ?? null;
2356
- }
2357
-
2358
2252
  // src/features/magic-context/storage-meta-shared.ts
2359
2253
  import { Buffer as Buffer2 } from "node:buffer";
2360
2254
  var SESSION_META_SELECT_COLUMNS = [
@@ -2513,7 +2407,7 @@ function isSessionMetaRow(row) {
2513
2407
  if (row === null || typeof row !== "object")
2514
2408
  return false;
2515
2409
  const r = row;
2516
- 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) && typeof r.is_subagent === "number" && 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);
2410
+ 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);
2517
2411
  }
2518
2412
  function getDefaultSessionMeta(sessionId) {
2519
2413
  return {
@@ -2573,7 +2467,7 @@ function getDefaultSessionMeta(sessionId) {
2573
2467
  };
2574
2468
  }
2575
2469
  var sessionMetaExistsStatements = new WeakMap;
2576
- function ensureSessionMetaRow(db, sessionId, initialIsSubagent = false) {
2470
+ function ensureSessionMetaRow(db, sessionId, initialIsSubagent = getHarness() === "opencode2" ? null : false) {
2577
2471
  let exists = sessionMetaExistsStatements.get(db);
2578
2472
  if (!exists) {
2579
2473
  exists = prepareCachedStatement(db, "SELECT 1 FROM session_meta WHERE session_id = ?");
@@ -2582,7 +2476,7 @@ function ensureSessionMetaRow(db, sessionId, initialIsSubagent = false) {
2582
2476
  if (exists.get(sessionId))
2583
2477
  return;
2584
2478
  const defaults = getDefaultSessionMeta(sessionId);
2585
- 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, getHarness(), defaults.lastResponseTime, defaults.cacheTtl, defaults.counter, defaults.lastNudgeTokens, defaults.lastNudgeBand ?? "", defaults.lastTransformError ?? "", initialIsSubagent ? 1 : 0, defaults.lastContextPercentage, defaults.lastInputTokens, defaults.observedSafeInputTokens, defaults.cacheAlertSent ? 1 : 0, defaults.timesExecuteThresholdReached, defaults.compartmentInProgress ? 1 : 0, defaults.systemPromptHash ?? "", defaults.clearedReasoningThroughTag);
2479
+ 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, getHarness(), 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);
2586
2480
  }
2587
2481
  function toSessionMeta(row) {
2588
2482
  const nudgeBandRaw = typeof row.last_nudge_band === "string" ? row.last_nudge_band : "";
@@ -2713,6 +2607,79 @@ function clearCachedM0M1(db, sessionId) {
2713
2607
  return;
2714
2608
  db.prepare(`UPDATE session_meta SET ${setClauses.join(", ")} WHERE session_id = ?`).run(...values, sessionId);
2715
2609
  }
2610
+ function clearCachedM0(db, sessionId) {
2611
+ clearCachedM0M1(db, sessionId);
2612
+ }
2613
+
2614
+ // src/features/magic-context/compression-depth-storage.ts
2615
+ var incrementDepthStatements = new WeakMap;
2616
+ var totalDepthStatements = new WeakMap;
2617
+ var maxDepthStatements = new WeakMap;
2618
+ var clearDepthStatements = new WeakMap;
2619
+ function getClearDepthStatement(db) {
2620
+ let stmt = clearDepthStatements.get(db);
2621
+ if (!stmt) {
2622
+ stmt = db.prepare("DELETE FROM compression_depth WHERE session_id = ?");
2623
+ clearDepthStatements.set(db, stmt);
2624
+ }
2625
+ return stmt;
2626
+ }
2627
+ function clearCompressionDepth(db, sessionId) {
2628
+ getClearDepthStatement(db).run(sessionId);
2629
+ }
2630
+ function clearCompressionDepthRange(db, sessionId, startOrdinal, endOrdinal) {
2631
+ if (endOrdinal < startOrdinal) {
2632
+ return;
2633
+ }
2634
+ db.prepare("DELETE FROM compression_depth WHERE session_id = ? AND message_ordinal BETWEEN ? AND ?").run(sessionId, startOrdinal, endOrdinal);
2635
+ }
2636
+
2637
+ // src/features/magic-context/no-content-compartment.ts
2638
+ function isNoContentCompartment(row) {
2639
+ return [row.title, row.content, row.p1, row.p2, row.p3, row.p4].every((value) => !value);
2640
+ }
2641
+
2642
+ // src/features/magic-context/storage-m0-mutation-log.ts
2643
+ var M0_MUTATION_TYPES = new Set([
2644
+ "compartment_delete",
2645
+ "compartment_merge",
2646
+ "recomp_boundary_change",
2647
+ "compartment_upgrade"
2648
+ ]);
2649
+ function assertMutationType(mutationType) {
2650
+ if (!M0_MUTATION_TYPES.has(mutationType)) {
2651
+ throw new Error(`Invalid m0 mutation type: ${mutationType}`);
2652
+ }
2653
+ }
2654
+ function toM0Mutation(row) {
2655
+ return {
2656
+ id: row.id,
2657
+ sessionId: row.session_id,
2658
+ mutationType: row.mutation_type,
2659
+ targetId: row.target_id,
2660
+ queuedAt: row.queued_at
2661
+ };
2662
+ }
2663
+ function queueM0Mutation(db, input) {
2664
+ assertMutationType(input.mutationType);
2665
+ const result = db.prepare(`INSERT INTO m0_mutation_log (session_id, mutation_type, target_id, queued_at)
2666
+ VALUES (?, ?, ?, ?)`).run(input.sessionId, input.mutationType, input.targetId ?? null, input.queuedAt ?? Date.now());
2667
+ const row = getM0Mutation(db, Number(result.lastInsertRowid));
2668
+ if (!row) {
2669
+ throw new Error("Failed to load queued m0 mutation");
2670
+ }
2671
+ return row;
2672
+ }
2673
+ function getM0Mutation(db, id) {
2674
+ const row = db.prepare(`SELECT id, session_id, mutation_type, target_id, queued_at
2675
+ FROM m0_mutation_log
2676
+ WHERE id = ?`).get(id);
2677
+ return row ? toM0Mutation(row) : null;
2678
+ }
2679
+ function getMaxM0MutationId(db, sessionId) {
2680
+ const row = db.prepare("SELECT MAX(id) AS max_id FROM m0_mutation_log WHERE session_id = ?").get(sessionId);
2681
+ return row?.max_id ?? null;
2682
+ }
2716
2683
 
2717
2684
  // src/features/magic-context/storage-schema-helpers.ts
2718
2685
  var columnCaches = new WeakMap;
@@ -8174,7 +8141,7 @@ function parseFrozenCalibration(value) {
8174
8141
  if (typeof value !== "object" || value === null || Array.isArray(value))
8175
8142
  return null;
8176
8143
  const candidate = value;
8177
- if (typeof candidate.revision !== "string" || candidate.revision.length === 0 || typeof candidate.providerId !== "string" || typeof candidate.modelId !== "string" || !finitePositive(candidate.systemRatio) || !finitePositive(candidate.toolsRatio) || !finitePositive(candidate.proseRatio) || candidate.source !== "seed" && candidate.source !== "family-fallback") {
8144
+ if (typeof candidate.revision !== "string" || candidate.revision.length === 0 || typeof candidate.providerId !== "string" || typeof candidate.modelId !== "string" || !finitePositive(candidate.systemRatio) || !finitePositive(candidate.toolsRatio) || !finitePositive(candidate.proseRatio) || candidate.source !== "seed" && candidate.source !== "family-fallback" && candidate.source !== "model-id") {
8178
8145
  return null;
8179
8146
  }
8180
8147
  return {
@@ -9809,7 +9776,11 @@ function parsePersistedCompactionMarkerState(raw, storedTargetEndMessageId) {
9809
9776
  if (parsed && typeof parsed === "object" && typeof parsed.boundaryMessageId === "string" && typeof parsed.summaryMessageId === "string" && typeof parsed.compactionPartId === "string" && typeof parsed.summaryPartId === "string" && typeof parsed.boundaryOrdinal === "number") {
9810
9777
  const targetEndMessageId = typeof storedTargetEndMessageId === "string" && storedTargetEndMessageId.length > 0 ? storedTargetEndMessageId : typeof parsed.targetEndMessageId === "string" && parsed.targetEndMessageId.length > 0 ? parsed.targetEndMessageId : null;
9811
9778
  return {
9812
- ...parsed,
9779
+ boundaryMessageId: parsed.boundaryMessageId,
9780
+ summaryMessageId: parsed.summaryMessageId,
9781
+ compactionPartId: parsed.compactionPartId,
9782
+ summaryPartId: parsed.summaryPartId,
9783
+ boundaryOrdinal: parsed.boundaryOrdinal,
9813
9784
  targetEndMessageId
9814
9785
  };
9815
9786
  }
@@ -9820,11 +9791,57 @@ function getPersistedCompactionMarkerState(db, sessionId) {
9820
9791
  const row = db.prepare("SELECT compaction_marker_state, compaction_marker_target_end_message_id FROM session_meta WHERE session_id = ?").get(sessionId);
9821
9792
  return parsePersistedCompactionMarkerState(row?.compaction_marker_state, row?.compaction_marker_target_end_message_id);
9822
9793
  }
9823
- function setPersistedCompactionMarkerState(db, sessionId, state) {
9794
+ function getCompactionMarkerPublicationState(db, sessionId) {
9795
+ const row = db.prepare("SELECT compaction_marker_state FROM session_meta WHERE session_id = ?").get(sessionId);
9796
+ const raw = row?.compaction_marker_state ?? "";
9797
+ let pending = null;
9798
+ try {
9799
+ pending = parsePersistedCompactionMarkerState(JSON.stringify(JSON.parse(raw).pending));
9800
+ } catch {}
9801
+ return { raw, pending };
9802
+ }
9803
+ function compareAndSetCompactionMarkerPublicationState(db, sessionId, expectedRaw, state, pending) {
9804
+ return db.transaction(() => {
9805
+ ensureSessionMetaRow(db, sessionId);
9806
+ let envelope = {};
9807
+ if (pending) {
9808
+ try {
9809
+ const parsed = JSON.parse(expectedRaw);
9810
+ if (parsed && typeof parsed === "object")
9811
+ envelope = parsed;
9812
+ } catch {}
9813
+ }
9814
+ const raw = JSON.stringify(pending ? { ...envelope, pending } : state);
9815
+ const result = db.prepare(`UPDATE session_meta SET compaction_marker_state = ?, compaction_marker_target_end_message_id = ?
9816
+ WHERE session_id = ? AND COALESCE(compaction_marker_state, '') = ?`).run(raw, state?.targetEndMessageId ?? null, sessionId, expectedRaw);
9817
+ if (result.changes !== 1)
9818
+ throw new Error("marker publication state changed concurrently");
9819
+ return raw;
9820
+ }).immediate();
9821
+ }
9822
+ function clearCompactionMarkerPublicationIntent(db, sessionId) {
9823
+ const { raw, pending } = getCompactionMarkerPublicationState(db, sessionId);
9824
+ if (!pending)
9825
+ return;
9826
+ let envelope = {};
9827
+ try {
9828
+ const parsed = JSON.parse(raw);
9829
+ if (parsed && typeof parsed === "object")
9830
+ envelope = parsed;
9831
+ } catch {}
9832
+ delete envelope.pending;
9833
+ const json = Object.keys(envelope).length > 0 ? JSON.stringify(envelope) : "";
9834
+ db.prepare("UPDATE session_meta SET compaction_marker_state = ? WHERE session_id = ? AND compaction_marker_state = ?").run(json, sessionId, raw);
9835
+ }
9836
+ function setPersistedCompactionMarkerState(db, sessionId, state, options = {}) {
9824
9837
  db.transaction(() => {
9825
9838
  ensureSessionMetaRow(db, sessionId);
9826
9839
  const previous = state === null ? getPersistedCompactionMarkerState(db, sessionId) ?? getDeferredClearedCompactionMarkerState(db, sessionId) : null;
9827
- const json = state ? JSON.stringify(state) : previous ? JSON.stringify({ deferredClear: previous }) : "";
9840
+ const pending = state === null && options.preservePending ? getCompactionMarkerPublicationState(db, sessionId).pending : null;
9841
+ const json = state ? JSON.stringify(state) : previous || pending ? JSON.stringify({
9842
+ ...previous ? { deferredClear: previous } : {},
9843
+ ...pending ? { pending } : {}
9844
+ }) : "";
9828
9845
  db.prepare("UPDATE session_meta SET compaction_marker_state = ?, compaction_marker_target_end_message_id = ? WHERE session_id = ?").run(json, state?.targetEndMessageId ?? null, sessionId);
9829
9846
  }).immediate();
9830
9847
  }
@@ -10580,6 +10597,12 @@ class V2StoreReaderPool {
10580
10597
  this.idleWaiters.clear();
10581
10598
  }
10582
10599
  }
10600
+ function messageTimesQuery(count, withVersions = false) {
10601
+ return `SELECT id, COALESCE(time_created, json_extract(data, '$.time.created')) AS created,
10602
+ json_extract(data, '$.time.completed') AS completed
10603
+ ${withVersions ? ", type, seq, time_created AS version_created, time_updated AS version_updated" : ""}
10604
+ FROM session_message WHERE +session_id = ? AND id IN (${Array.from({ length: count }, () => "?").join(",")})`;
10605
+ }
10583
10606
 
10584
10607
  class V2StoreReader {
10585
10608
  path;
@@ -10676,6 +10699,12 @@ class V2StoreReader {
10676
10699
  return typeof row?.count === "number" ? row.count : 0;
10677
10700
  });
10678
10701
  }
10702
+ messageCountAfterSequence(sessionID, after, through) {
10703
+ const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
10704
+ const row = this.prepare(`SELECT COUNT(*) AS count FROM session_message
10705
+ WHERE session_id = ? AND seq > ? AND seq <= ? AND type IN (${rawTypes})`).get(sessionID, after, through, ...RAW_MESSAGE_TYPES);
10706
+ return typeof row?.count === "number" ? row.count : 0;
10707
+ }
10679
10708
  sessionDirectoryPage(afterSessionID, limit) {
10680
10709
  const rows = this.prepare(`SELECT id, COALESCE(directory, '') AS directory FROM session_v2
10681
10710
  WHERE id > ? ORDER BY id ASC LIMIT ?`).all(afterSessionID ?? "", limit);
@@ -10709,6 +10738,49 @@ class V2StoreReader {
10709
10738
  return row ? decode2(row) : null;
10710
10739
  });
10711
10740
  }
10741
+ messageTimeVersions(sessionID, ids, span) {
10742
+ const versions = new Map;
10743
+ if (ids.length > 0 && span && span.last - span.first < ids.length * 2) {
10744
+ const wanted = new Set(ids);
10745
+ const rows = this.prepare(`SELECT id, type, seq, time_created AS created, time_updated AS updated
10746
+ FROM session_message WHERE session_id = ? AND seq >= ? AND seq <= ?`).all(sessionID, span.first, span.last);
10747
+ for (const row of rows)
10748
+ if (wanted.has(row.id))
10749
+ versions.set(row.id, row);
10750
+ for (const [id, version] of this.messageTimeVersions(sessionID, ids.filter((id) => !versions.has(id))))
10751
+ versions.set(id, version);
10752
+ return versions;
10753
+ }
10754
+ for (let offset = 0;offset < ids.length; offset += 500) {
10755
+ const batch = ids.slice(offset, offset + 500);
10756
+ const rows = this.prepare(`SELECT id, type, seq, time_created AS created, time_updated AS updated
10757
+ FROM session_message WHERE +session_id = ? AND id IN (${batch.map(() => "?").join(",")})`).all(sessionID, ...batch);
10758
+ for (const row of rows)
10759
+ versions.set(row.id, row);
10760
+ }
10761
+ return versions;
10762
+ }
10763
+ messageTimes(sessionID, ids, versions) {
10764
+ const times = new Map;
10765
+ for (let offset = 0;offset < ids.length; offset += 500) {
10766
+ const batch = ids.slice(offset, offset + 500);
10767
+ const rows = this.prepare(messageTimesQuery(batch.length, versions !== undefined)).all(sessionID, ...batch);
10768
+ for (const row of rows) {
10769
+ times.set(row.id, {
10770
+ created: typeof row.created === "number" ? row.created : undefined,
10771
+ completed: typeof row.completed === "number" ? row.completed : undefined
10772
+ });
10773
+ versions?.set(row.id, {
10774
+ id: row.id,
10775
+ type: row.type,
10776
+ seq: row.seq,
10777
+ created: row.version_created,
10778
+ updated: row.version_updated
10779
+ });
10780
+ }
10781
+ }
10782
+ return times;
10783
+ }
10712
10784
  messageExistsById(sessionID, id) {
10713
10785
  return trackDecodeOperation("messageExistsById", () => {
10714
10786
  const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
@@ -10794,6 +10866,14 @@ class V2StoreReader {
10794
10866
  ORDER BY seq DESC LIMIT ?`).all(sessionID, ...RAW_MESSAGE_TYPES, throughSeq, limit).map(decode2);
10795
10867
  });
10796
10868
  }
10869
+ instructionRowsThrough(sessionID, throughSeq) {
10870
+ return trackDecodeOperation("instructionRowsThrough", () => {
10871
+ if (!Number.isSafeInteger(throughSeq))
10872
+ throw new Error("Invalid seq upper bound");
10873
+ return this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
10874
+ WHERE session_id = ? AND type = 'system' AND seq <= ? ORDER BY seq ASC`).all(sessionID, throughSeq).map(decode2);
10875
+ });
10876
+ }
10797
10877
  sequenceForId(sessionID, id) {
10798
10878
  if (!id)
10799
10879
  return;
@@ -10913,87 +10993,9 @@ class V2StoreReader {
10913
10993
  // src/features/magic-context/storage-meta-session.ts
10914
10994
  import { Buffer as Buffer3 } from "node:buffer";
10915
10995
 
10916
- // src/shared/auto-search-lifecycle.ts
10917
- var pending = new Map;
10918
- var unpublishedNoHints = new Map;
10919
- function rememberUnpublishedAutoSearchNoHint(sessionId, messageId, reason) {
10920
- let messages = unpublishedNoHints.get(sessionId);
10921
- if (!messages) {
10922
- messages = new Map;
10923
- unpublishedNoHints.set(sessionId, messages);
10924
- }
10925
- if (!messages.has(messageId))
10926
- messages.set(messageId, reason);
10927
- }
10928
- function getUnpublishedAutoSearchNoHint(sessionId, messageId) {
10929
- return unpublishedNoHints.get(sessionId)?.get(messageId);
10930
- }
10931
- function forgetUnpublishedAutoSearchNoHint(sessionId, messageId) {
10932
- const messages = unpublishedNoHints.get(sessionId);
10933
- messages?.delete(messageId);
10934
- if (messages?.size === 0)
10935
- unpublishedNoHints.delete(sessionId);
10936
- }
10937
- function clearUnpublishedAutoSearchNoHints(sessionId) {
10938
- if (sessionId === undefined)
10939
- unpublishedNoHints.clear();
10940
- else
10941
- unpublishedNoHints.delete(sessionId);
10942
- }
10943
- function registerAutoSearchWork(sessionId) {
10944
- const controller = new AbortController;
10945
- const work = pending.get(sessionId) ?? new Set;
10946
- pending.set(sessionId, work);
10947
- work.add(controller);
10948
- return {
10949
- signal: controller.signal,
10950
- done: () => {
10951
- work.delete(controller);
10952
- if (pending.get(sessionId) === work && work.size === 0)
10953
- pending.delete(sessionId);
10954
- }
10955
- };
10956
- }
10957
- function cancelAutoSearchWork(sessionId) {
10958
- for (const [id, work] of pending) {
10959
- if (sessionId !== undefined && id !== sessionId)
10960
- continue;
10961
- pending.delete(id);
10962
- for (const controller of work)
10963
- controller.abort();
10964
- }
10965
- }
10966
-
10967
- // src/features/magic-context/resolve-subagent-fallback.ts
10968
- function resolveIsSubagentFromOpenCodeDb(sessionId) {
10969
- try {
10970
- return withReadOnlySessionDb((openCodeDb) => {
10971
- const row = openCodeDb.prepare("SELECT parent_id FROM session WHERE id = ?").get(sessionId);
10972
- if (!row)
10973
- return null;
10974
- return typeof row.parent_id === "string" && row.parent_id.length > 0;
10975
- });
10976
- } catch (error) {
10977
- log(`[magic-context] resolveIsSubagentFromOpenCodeDb failed for ${sessionId}:`, error);
10978
- return null;
10979
- }
10980
- }
10981
- function resolveIsSubagentFromOpenCode2Db(sessionId) {
10982
- let reader;
10983
- try {
10984
- reader = new V2StoreReader(gaDatabasePath(getDataDir(), process.env.OPENCODE_CHANNEL ?? "latest"));
10985
- return reader.isSubagent(sessionId);
10986
- } catch (error) {
10987
- log(`[magic-context] resolveIsSubagentFromOpenCode2Db failed for ${sessionId}:`, error);
10988
- return null;
10989
- } finally {
10990
- reader?.close();
10991
- }
10992
- }
10993
-
10994
10996
  // src/hooks/magic-context/lkg-recovery-fence.ts
10995
10997
  import { createHash as createHash7, randomUUID as randomUUID2 } from "node:crypto";
10996
- import { lstatSync, readdirSync as readdirSync2, readFileSync as readFileSync4, rmSync as rmSync2 } from "node:fs";
10998
+ import { lstatSync, readdirSync as readdirSync2, readFileSync as readFileSync4, rmdirSync, rmSync as rmSync2 } from "node:fs";
10997
10999
  import { basename, dirname, join as join3 } from "node:path";
10998
11000
 
10999
11001
  // src/shared/storage-file-mutex.ts
@@ -11038,7 +11040,16 @@ function withStorageFileMutex(directory, work, timeoutMs = 1000) {
11038
11040
  const birth = processBirth(process.pid);
11039
11041
  const ticket = { choosing: true, number: 0, birth, checksum: "" };
11040
11042
  const encodedTicket = () => JSON.stringify({ ...ticket, checksum: ticketChecksum(name, ticket) });
11041
- writeStorageFileSync(ownPath, encodedTicket(), { flag: "wx", forcePrivate: true });
11043
+ for (let attempt = 0;; attempt++) {
11044
+ try {
11045
+ writeStorageFileSync(ownPath, encodedTicket(), { flag: "wx", forcePrivate: true });
11046
+ break;
11047
+ } catch (error) {
11048
+ if (error.code !== "ENOENT" || attempt >= 2)
11049
+ throw error;
11050
+ ensurePrivateStorageDirectorySync(directory);
11051
+ }
11052
+ }
11042
11053
  const deadline = performance.now() + timeoutMs;
11043
11054
  const peers = () => {
11044
11055
  const found = [];
@@ -11315,15 +11326,112 @@ function commitLkgWithRecoveryFence(args) {
11315
11326
  function clearLkgRecoveryFence(sessionId, remove, shouldClear) {
11316
11327
  if (!durableFencesSupported())
11317
11328
  return remove?.();
11318
- return locked(sessionId, () => {
11319
- const result = remove?.();
11320
- if (shouldClear && !shouldClear(result))
11321
- return result;
11322
- rmSync2(lkgRecoveryFencePath(sessionId), { force: true });
11323
- syncStorageDirectorySync(dirname(lkgRecoveryFencePath(sessionId)));
11329
+ const path = lkgRecoveryFencePath(sessionId);
11330
+ const missing = (file) => {
11331
+ try {
11332
+ lstatSync(file);
11333
+ return false;
11334
+ } catch (error) {
11335
+ return error.code === "ENOENT";
11336
+ }
11337
+ };
11338
+ if (!remove && !shouldClear && missing(path) && missing(`${path}.locks`)) {
11324
11339
  clearLkgFenceProcessState(sessionId);
11325
- return result;
11326
- });
11340
+ return;
11341
+ }
11342
+ try {
11343
+ return locked(sessionId, () => {
11344
+ const result = remove?.();
11345
+ if (shouldClear && !shouldClear(result))
11346
+ return result;
11347
+ rmSync2(lkgRecoveryFencePath(sessionId), { force: true });
11348
+ syncStorageDirectorySync(dirname(lkgRecoveryFencePath(sessionId)));
11349
+ clearLkgFenceProcessState(sessionId);
11350
+ return result;
11351
+ });
11352
+ } finally {
11353
+ try {
11354
+ rmdirSync(`${path}.locks`);
11355
+ } catch {}
11356
+ }
11357
+ }
11358
+
11359
+ // src/shared/auto-search-lifecycle.ts
11360
+ var pending = new Map;
11361
+ var unpublishedNoHints = new Map;
11362
+ function rememberUnpublishedAutoSearchNoHint(sessionId, messageId, reason) {
11363
+ let messages = unpublishedNoHints.get(sessionId);
11364
+ if (!messages) {
11365
+ messages = new Map;
11366
+ unpublishedNoHints.set(sessionId, messages);
11367
+ }
11368
+ if (!messages.has(messageId))
11369
+ messages.set(messageId, reason);
11370
+ }
11371
+ function getUnpublishedAutoSearchNoHint(sessionId, messageId) {
11372
+ return unpublishedNoHints.get(sessionId)?.get(messageId);
11373
+ }
11374
+ function forgetUnpublishedAutoSearchNoHint(sessionId, messageId) {
11375
+ const messages = unpublishedNoHints.get(sessionId);
11376
+ messages?.delete(messageId);
11377
+ if (messages?.size === 0)
11378
+ unpublishedNoHints.delete(sessionId);
11379
+ }
11380
+ function clearUnpublishedAutoSearchNoHints(sessionId) {
11381
+ if (sessionId === undefined)
11382
+ unpublishedNoHints.clear();
11383
+ else
11384
+ unpublishedNoHints.delete(sessionId);
11385
+ }
11386
+ function registerAutoSearchWork(sessionId) {
11387
+ const controller = new AbortController;
11388
+ const work = pending.get(sessionId) ?? new Set;
11389
+ pending.set(sessionId, work);
11390
+ work.add(controller);
11391
+ return {
11392
+ signal: controller.signal,
11393
+ done: () => {
11394
+ work.delete(controller);
11395
+ if (pending.get(sessionId) === work && work.size === 0)
11396
+ pending.delete(sessionId);
11397
+ }
11398
+ };
11399
+ }
11400
+ function cancelAutoSearchWork(sessionId) {
11401
+ for (const [id, work] of pending) {
11402
+ if (sessionId !== undefined && id !== sessionId)
11403
+ continue;
11404
+ pending.delete(id);
11405
+ for (const controller of work)
11406
+ controller.abort();
11407
+ }
11408
+ }
11409
+
11410
+ // src/features/magic-context/resolve-subagent-fallback.ts
11411
+ function resolveIsSubagentFromOpenCodeDb(sessionId) {
11412
+ try {
11413
+ return withReadOnlySessionDb((openCodeDb) => {
11414
+ const row = openCodeDb.prepare("SELECT parent_id FROM session WHERE id = ?").get(sessionId);
11415
+ if (!row)
11416
+ return null;
11417
+ return typeof row.parent_id === "string" && row.parent_id.length > 0;
11418
+ });
11419
+ } catch (error) {
11420
+ log(`[magic-context] resolveIsSubagentFromOpenCodeDb failed for ${sessionId}:`, error);
11421
+ return null;
11422
+ }
11423
+ }
11424
+ function resolveIsSubagentFromOpenCode2Db(sessionId) {
11425
+ let reader;
11426
+ try {
11427
+ reader = new V2StoreReader(gaDatabasePath(getDataDir(), process.env.OPENCODE_CHANNEL ?? "latest"));
11428
+ return reader.isSubagent(sessionId);
11429
+ } catch (error) {
11430
+ log(`[magic-context] resolveIsSubagentFromOpenCode2Db failed for ${sessionId}:`, error);
11431
+ return null;
11432
+ } finally {
11433
+ reader?.close();
11434
+ }
11327
11435
  }
11328
11436
 
11329
11437
  // src/features/magic-context/message-fts-rowid-map.ts
@@ -11572,8 +11680,9 @@ function deleteSessionScopedRows(db, sessionIds, harness, options = {}) {
11572
11680
  retire.run(tagIdentityRevisionKey(id), tagRevisionKey(id), ...harnessTables.map(() => id), ...tagTombstoneOwnerRange(id));
11573
11681
  }
11574
11682
  }
11575
- for (const sessionId of deletableSessionIds)
11576
- clearLkgRecoveryFence(sessionId);
11683
+ if (!options.deferRecoveryFenceCleanup)
11684
+ for (const sessionId of deletableSessionIds)
11685
+ clearLkgRecoveryFence(sessionId);
11577
11686
  return deletableSessionIds.length;
11578
11687
  }
11579
11688
 
@@ -11626,17 +11735,48 @@ function findSessionMeta(db, sessionId) {
11626
11735
  const result = db.prepare(`SELECT ${getSessionMetaSelectColumns(db)} FROM session_meta WHERE session_id = ?`).get(sessionId);
11627
11736
  return isSessionMetaRow(result) ? toSessionMeta(result) : undefined;
11628
11737
  }
11738
+ var parentReadRetries = new WeakMap;
11739
+ var PARENT_READ_ATTEMPTS = 5;
11740
+ var PARENT_READ_COOLDOWN_MS = 30000;
11629
11741
  function getOrCreateSessionMeta(db, sessionId) {
11630
11742
  const existing = findSessionMeta(db, sessionId);
11631
- if (existing)
11632
- return existing;
11633
11743
  const harness = getHarness();
11634
- const defaults = getDefaultSessionMeta(sessionId);
11744
+ if (existing) {
11745
+ if (harness !== "opencode2")
11746
+ return existing;
11747
+ const row = db.prepare("SELECT is_subagent FROM session_meta WHERE session_id = ?").get(sessionId);
11748
+ if (row.is_subagent !== null)
11749
+ return existing;
11750
+ }
11751
+ const defaults = existing ?? getDefaultSessionMeta(sessionId);
11752
+ let retries = parentReadRetries.get(db);
11753
+ if (harness === "opencode2") {
11754
+ if (!retries) {
11755
+ retries = new BoundedSessionMap(1000);
11756
+ parentReadRetries.set(db, retries);
11757
+ }
11758
+ const retry = retries.get(sessionId);
11759
+ if (retry && retry.attempts >= PARENT_READ_ATTEMPTS && Date.now() - retry.at < PARENT_READ_COOLDOWN_MS)
11760
+ return defaults;
11761
+ }
11635
11762
  const fallbackSubagent = harness === "opencode" ? resolveIsSubagentFromOpenCodeDb(sessionId) : harness === "opencode2" ? resolveIsSubagentFromOpenCode2Db(sessionId) : null;
11636
11763
  if (fallbackSubagent === true) {
11637
11764
  defaults.isSubagent = true;
11638
11765
  }
11639
- ensureSessionMetaRow(db, sessionId, harness === "opencode2" && fallbackSubagent === true);
11766
+ if (harness === "opencode2") {
11767
+ if (fallbackSubagent === null) {
11768
+ const previous = retries?.get(sessionId);
11769
+ const attempts = previous && Date.now() - previous.at < PARENT_READ_COOLDOWN_MS ? previous.attempts + 1 : 1;
11770
+ retries?.set(sessionId, { attempts, at: Date.now() });
11771
+ ensureSessionMetaRow(db, sessionId, null);
11772
+ return defaults;
11773
+ }
11774
+ retries?.delete(sessionId);
11775
+ ensureSessionMetaRow(db, sessionId, fallbackSubagent);
11776
+ db.prepare("UPDATE session_meta SET is_subagent = ? WHERE session_id = ? AND is_subagent IS NULL").run(fallbackSubagent ? 1 : 0, sessionId);
11777
+ return defaults;
11778
+ }
11779
+ ensureSessionMetaRow(db, sessionId);
11640
11780
  if (harness === "opencode" && fallbackSubagent === true) {
11641
11781
  db.prepare("UPDATE session_meta SET is_subagent = 1 WHERE session_id = ?").run(sessionId);
11642
11782
  }
@@ -11689,12 +11829,16 @@ function clearSession(db, sessionId, _rustModuleCleanupAcknowledged = false, per
11689
11829
  cancelAutoSearchWork(sessionId);
11690
11830
  const transactionStartedAt = performance.now();
11691
11831
  let clearedMarker = null;
11832
+ let eligibleSessions = 0;
11692
11833
  db.transaction(() => {
11693
11834
  clearedMarker = getPendingPiCompactionMarkerState(db, sessionId);
11694
- deleteSessionScopedRows(db, [sessionId], undefined, {
11695
- preserveTagRevisionTombstones: !permanentlyDeleted
11835
+ eligibleSessions = deleteSessionScopedRows(db, [sessionId], undefined, {
11836
+ preserveTagRevisionTombstones: !permanentlyDeleted,
11837
+ deferRecoveryFenceCleanup: true
11696
11838
  });
11697
11839
  }).immediate();
11840
+ if (eligibleSessions > 0)
11841
+ clearLkgRecoveryFence(sessionId);
11698
11842
  if (clearedMarker)
11699
11843
  logPiCompactionMarkerClear(sessionId, clearedMarker, "session-reset");
11700
11844
  clearUnpublishedAutoSearchNoHints(sessionId);
@@ -11881,13 +12025,13 @@ function identity(slot) {
11881
12025
  slot.inputContentDigests
11882
12026
  ])).update(slot.jsonPrefix).digest("hex");
11883
12027
  }
11884
- function envelope(modelKey, systemPromptTokens, agentName) {
12028
+ function envelope(modelKey, systemPromptTokens, agentName, sessionId) {
11885
12029
  const slash = modelKey.indexOf("/");
11886
12030
  return JSON.stringify([
11887
12031
  modelKey,
11888
12032
  systemPromptTokens,
11889
12033
  agentName,
11890
- getMeasuredToolDefinitionTokens(modelKey.slice(0, slash), modelKey.slice(slash + 1), agentName)
12034
+ getMeasuredToolDefinitionTokens(modelKey.slice(0, slash), modelKey.slice(slash + 1), agentName, sessionId)
11891
12035
  ]);
11892
12036
  }
11893
12037
  function beginV2LkgRequest(sessionId, modelKey, previousResponseId, responseAfterSequence) {
@@ -11919,7 +12063,7 @@ function noteCapturedLkgRequest(args) {
11919
12063
  request,
11920
12064
  slot: identity(args.slot),
11921
12065
  prefixCount: JSON.parse(args.slot.jsonPrefix).length,
11922
- envelope: envelope(request.modelKey, args.systemPromptTokens, args.agentName)
12066
+ envelope: envelope(request.modelKey, args.systemPromptTokens, args.agentName, args.sessionId)
11923
12067
  });
11924
12068
  }
11925
12069
  function noteLkgProviderResponse(args) {
@@ -12317,6 +12461,12 @@ function noteLkgCapturedPrefix(sessionId) {
12317
12461
  sessions.set(sessionId, {});
12318
12462
  }
12319
12463
  function readLkgRecoveryPrefix(sessionId, inputs, messages, db) {
12464
+ return readRecoveryIdentityPrefix(sessionId, inputs.map((input, index) => ({
12465
+ id: lkgInputIdentity(input.id, input.role, index),
12466
+ contentDigest: input.contentDigest
12467
+ })), messages, db);
12468
+ }
12469
+ function readRecoveryIdentityPrefix(sessionId, inputs, messages, db) {
12320
12470
  const state = sessions.get(sessionId);
12321
12471
  const slot = db && (!state || state.prefix || state.uncertifiedNormal) ? loadPersistedLkgSlot(db, sessionId, true) ?? getSlot(sessionId) : getSlot(sessionId);
12322
12472
  const prefix = state?.prefix;
@@ -12335,8 +12485,8 @@ function noteLkgHealthyPrefix(sessionId, inputs, messages, release) {
12335
12485
  fenceRevision: sessions.get(sessionId)?.fenceRevision,
12336
12486
  prefix: prefix && !release ? {
12337
12487
  ...prefix,
12338
- inputs: inputs.map((input) => ({
12339
- id: input.id ?? "",
12488
+ inputs: inputs.map((input, index) => ({
12489
+ id: lkgInputIdentity(input.id, input.role, index) ?? "",
12340
12490
  digest: input.contentDigest?.() ?? ""
12341
12491
  })),
12342
12492
  json: JSON.stringify(messages)
@@ -12345,6 +12495,12 @@ function noteLkgHealthyPrefix(sessionId, inputs, messages, release) {
12345
12495
  }
12346
12496
 
12347
12497
  // src/hooks/magic-context/lkg-slot.ts
12498
+ var POSITIONAL_SYSTEM_KEY = "\x00mc-system:";
12499
+ function lkgInputIdentity(id, role, index) {
12500
+ if (typeof id === "string" && id.length > 0)
12501
+ return id.startsWith(POSITIONAL_SYSTEM_KEY) ? null : id;
12502
+ return role === "system" ? `${POSITIONAL_SYSTEM_KEY}${index}` : null;
12503
+ }
12348
12504
  var LKG_SINGLE_SLOT_BYTES = 72 * 1024 * 1024;
12349
12505
  var LKG_TOTAL_BYTES = 160 * 1024 * 1024;
12350
12506
  var LKG_METADATA_BYTES = 256;
@@ -12708,14 +12864,12 @@ function dropSlot(sessionId, _reason) {
12708
12864
  if (pass !== undefined)
12709
12865
  hydrationAttemptBySession.set(sessionId, pass);
12710
12866
  }
12711
- function noteEntry(sessionId, messages) {
12867
+ function noteEntry(sessionId, input) {
12712
12868
  const slot = getSlot(sessionId);
12713
12869
  if (!slot)
12714
12870
  return null;
12715
- const entryInputIds = messages.map((message) => {
12716
- const id = message.info?.id;
12717
- return typeof id === "string" ? id : "";
12718
- });
12871
+ const messages = input;
12872
+ const entryInputIds = messages.map((message, index) => lkgInputIdentity(message.info?.id, message.info?.role, index) ?? "");
12719
12873
  const anchorIndex = entryInputIds.indexOf(slot.lastInputMessageId);
12720
12874
  if (anchorIndex < 0)
12721
12875
  return null;
@@ -13129,6 +13283,8 @@ function projectEntryWithDigests(messages, digests) {
13129
13283
  value: () => contentDigest,
13130
13284
  enumerable: false
13131
13285
  });
13286
+ if (isCoveredInstructionRow(message.info))
13287
+ markCoveredInstructionRow(projection);
13132
13288
  return projection;
13133
13289
  });
13134
13290
  }
@@ -13205,7 +13361,7 @@ function asEntryProjection(input) {
13205
13361
  }
13206
13362
  return projectLkgEntry(input);
13207
13363
  }
13208
- function outputMessageIsPostAnchor(message, inputIndexById, anchorIndex) {
13364
+ function outputMessageIsPostAnchor(message, inputIndexById, anchorIndex, systemIndices) {
13209
13365
  const id = messageId(message);
13210
13366
  if (id !== null) {
13211
13367
  const inputIndex = inputIndexById.get(id);
@@ -13222,6 +13378,12 @@ function outputMessageIsPostAnchor(message, inputIndexById, anchorIndex) {
13222
13378
  }
13223
13379
  return false;
13224
13380
  }
13381
+ if (isCoveredInstructionRow(message.info))
13382
+ return false;
13383
+ if (messageRole(message) === "system") {
13384
+ const index = systemIndices.get(message);
13385
+ return index === undefined ? null : index > anchorIndex;
13386
+ }
13225
13387
  if (!isSyntheticOutput(message))
13226
13388
  return null;
13227
13389
  const linked = ["sourceMessageId", "ownerMessageId", "anchorMessageId"].map((key) => recordValue(message.info, key)).find((value) => typeof value === "string");
@@ -13235,7 +13397,7 @@ function buildLkgPrefix(input, output, completeOutput = false) {
13235
13397
  const anchorIndex = completeOutput && projected.length > 0 ? projected.length - 1 : findLkgAnchor(projected);
13236
13398
  if (anchorIndex === null)
13237
13399
  return null;
13238
- const ids = projected.map((message) => message.id);
13400
+ const ids = projected.map((message, index) => lkgInputIdentity(message.id, message.role, index));
13239
13401
  if (ids.some((id) => id === null))
13240
13402
  return null;
13241
13403
  const validIds = ids;
@@ -13246,9 +13408,27 @@ function buildLkgPrefix(input, output, completeOutput = false) {
13246
13408
  if (inputContentDigests.some((digest) => digest === null))
13247
13409
  return null;
13248
13410
  const inputIndexById = new Map(validIds.map((id, index) => [id, index]));
13411
+ const systemIndices = new Map;
13412
+ if (!completeOutput) {
13413
+ const inputSystems = projected.flatMap((message, index) => message.id === null && message.role === "system" && !isCoveredInstructionRow(message) ? [{ message, index }] : []);
13414
+ const outputSystems = output.filter((message) => messageId(message) === null && messageRole(message) === "system");
13415
+ const restoredCount = outputSystems.length - inputSystems.length;
13416
+ if (restoredCount < 0 || outputSystems.slice(0, restoredCount).some((message) => !isCoveredInstructionRow(message.info)))
13417
+ return null;
13418
+ for (let index = 0;index < inputSystems.length; index += 1) {
13419
+ const source = inputSystems[index];
13420
+ const message = outputSystems[index + restoredCount];
13421
+ const digest = source.message.contentDigest?.();
13422
+ if (!digest || lkgContentDigest(message) !== digest)
13423
+ return null;
13424
+ if (isCoveredInstructionRow(message.info) && source.index > anchorIndex)
13425
+ return null;
13426
+ systemIndices.set(message, source.index);
13427
+ }
13428
+ }
13249
13429
  const prefix = [];
13250
13430
  for (const message of output) {
13251
- const postAnchor = completeOutput ? false : outputMessageIsPostAnchor(message, inputIndexById, anchorIndex);
13431
+ const postAnchor = completeOutput ? false : outputMessageIsPostAnchor(message, inputIndexById, anchorIndex, systemIndices);
13252
13432
  if (postAnchor === null)
13253
13433
  return null;
13254
13434
  if (!postAnchor)
@@ -14757,8 +14937,8 @@ function finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion)
14757
14937
  startMessageFtsRowidMapBackfill(db).then(async () => {
14758
14938
  if (!readsOpenCodeStore)
14759
14939
  return;
14760
- const { readRawSessionMessagePage, readRawSessionMessages } = await import("./read-session-chunk-t8bg6wkz.js");
14761
- const { startMessageTimeBackfill } = await import("./message-time-backfill-j96x1sab.js");
14940
+ const { readRawSessionMessagePage, readRawSessionMessages } = await import("./read-session-chunk-2s8x6xps.js");
14941
+ const { startMessageTimeBackfill } = await import("./message-time-backfill-g9py24yt.js");
14762
14942
  await startMessageTimeBackfill(db, Object.assign(readRawSessionMessages, {
14763
14943
  readPage: readRawSessionMessagePage
14764
14944
  }));
@@ -14766,7 +14946,7 @@ function finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion)
14766
14946
  if (isTransientSqliteError(error)) {
14767
14947
  withoutSqliteTransformPass(() => {
14768
14948
  const timer = setTimeout(runBackfills, busyRetryMs);
14769
- timer.unref();
14949
+ timer.unref?.();
14770
14950
  });
14771
14951
  busyRetryMs = Math.min(30000, busyRetryMs * 2);
14772
14952
  return;
@@ -16249,6 +16429,10 @@ function openDatabase(dbPathOrOptions) {
16249
16429
  lastMigrationOnOpenRefusal = null;
16250
16430
  const existing = databases.get(dbPath);
16251
16431
  if (existing) {
16432
+ if (options?.nativeTurnDeadlines && !hasSqliteTurnDeadlineRouting(existing)) {
16433
+ log("[magic-context] storage unavailable: native turn deadlines must be selected on the first open; restart the host");
16434
+ return null;
16435
+ }
16252
16436
  if (!enforceSchemaFence(existing, dbPath, latestSupportedVersion)) {
16253
16437
  return null;
16254
16438
  }
@@ -16273,7 +16457,7 @@ function openDatabase(dbPathOrOptions) {
16273
16457
  throw error;
16274
16458
  }
16275
16459
  }
16276
- db = new Database(dbPath);
16460
+ db = options?.nativeTurnDeadlines ? createSqliteTurnDatabase(dbPath) : new Database(dbPath);
16277
16461
  installBootBusyTimeout(db, dbPath, busyTimeoutMs, options?.onBootBusyTimeout);
16278
16462
  if (!enforceSchemaFence(db, dbPath, latestSupportedVersion)) {
16279
16463
  closeQuietly(db);
@@ -16320,6 +16504,10 @@ async function openDatabaseAsync(dbPathOrOptions) {
16320
16504
  lastMigrationOnOpenRefusal = null;
16321
16505
  const existing = databases.get(dbPath);
16322
16506
  if (existing) {
16507
+ if (options?.nativeTurnDeadlines && !hasSqliteTurnDeadlineRouting(existing)) {
16508
+ log("[magic-context] storage unavailable: native turn deadlines must be selected on the first open; restart the host");
16509
+ return null;
16510
+ }
16323
16511
  const startedAt = performance.now();
16324
16512
  const accepted = enforceSchemaFence(existing, dbPath, latestSupportedVersion);
16325
16513
  if (accepted) {
@@ -16335,8 +16523,12 @@ async function openDatabaseAsync(dbPathOrOptions) {
16335
16523
  return accepted ? existing : null;
16336
16524
  }
16337
16525
  const pending = pendingAsyncOpens.get(dbPath);
16338
- if (pending)
16339
- return pending;
16526
+ if (pending) {
16527
+ if (!options?.nativeTurnDeadlines)
16528
+ return pending;
16529
+ const opened = await pending;
16530
+ return opened && hasSqliteTurnDeadlineRouting(opened) ? opened : null;
16531
+ }
16340
16532
  const opening = (async () => {
16341
16533
  let db;
16342
16534
  const openStartedAt = performance.now();
@@ -16358,7 +16550,7 @@ async function openDatabaseAsync(dbPathOrOptions) {
16358
16550
  throw error;
16359
16551
  }
16360
16552
  }
16361
- db = new Database(dbPath);
16553
+ db = options?.nativeTurnDeadlines ? createSqliteTurnDatabase(dbPath) : new Database(dbPath);
16362
16554
  installBootBusyTimeout(db, dbPath, busyTimeoutMs, options?.onBootBusyTimeout);
16363
16555
  openMs = performance.now() - openStartedAt;
16364
16556
  guardStartedAt = performance.now();
@@ -16420,9 +16612,53 @@ function getDatabasePersistenceError(db) {
16420
16612
  }
16421
16613
  // src/features/magic-context/message-index.ts
16422
16614
  import { createHash as createHash13 } from "node:crypto";
16423
- var MESSAGE_HISTORY_ORPHAN_SAFETY_AGE_MS = 24 * 60 * 60 * 1000;
16424
- var MESSAGE_HISTORY_ORPHAN_SWEEP_COOLDOWN_MS = 10 * 60 * 1000;
16425
- var MESSAGE_HISTORY_ORPHAN_UNAVAILABLE_REPROBE_MS = 24 * 60 * 60 * 1000;
16615
+
16616
+ // src/shared/search-text.ts
16617
+ function removeTaskNotifications(text) {
16618
+ return removeNestedBlocks(text, ["task", "task_cohort"]);
16619
+ }
16620
+ function cleanSearchText(text) {
16621
+ return removeNestedBlocks(text, ["system-reminder", "task", "task_cohort"]).replace(/\s+/g, " ").trim();
16622
+ }
16623
+ var QUERY_STOP_WORDS = new Set("a an and are as at be been by can could did do does for from how i in is it me my of on or our please should that the their them there these they this to us was we were what when where which who why will with would you your".split(" "));
16624
+ function terms(text) {
16625
+ return [
16626
+ ...new Set((text.toLowerCase().match(/[\p{L}\p{N}_]+/gu) ?? []).filter((term) => !QUERY_STOP_WORDS.has(term)))
16627
+ ];
16628
+ }
16629
+ function keywordRelevance(query, content) {
16630
+ const needles = terms(query);
16631
+ if (needles.length === 0)
16632
+ return 0;
16633
+ const words = new Set(terms(cleanSearchText(content)));
16634
+ return 0.8 * needles.filter((term) => words.has(term)).length / needles.length;
16635
+ }
16636
+ function matchedSearchSnippet(content, query, cap = 220) {
16637
+ const text = cleanSearchText(content);
16638
+ const needles = new Set(terms(query));
16639
+ const matches = [...text.matchAll(/[\p{L}\p{N}_]+/gu)].filter((match) => needles.has(match[0].toLowerCase()));
16640
+ if (matches.length === 0)
16641
+ return "";
16642
+ let bestStart = matches[0].index;
16643
+ let bestCoverage = -1;
16644
+ for (const match of matches) {
16645
+ const start = Math.max(0, match.index - Math.floor(cap / 5));
16646
+ const coverage = keywordRelevance(query, text.slice(start, start + cap));
16647
+ if (coverage > bestCoverage) {
16648
+ bestStart = start;
16649
+ bestCoverage = coverage;
16650
+ }
16651
+ }
16652
+ if (bestStart > 0) {
16653
+ const boundary = text.indexOf(" ", bestStart);
16654
+ if (boundary >= 0 && boundary < matches[matches.length - 1].index)
16655
+ bestStart = boundary + 1;
16656
+ }
16657
+ const passage = text.slice(bestStart, bestStart + cap).trim();
16658
+ return `${bestStart > 0 ? "…" : ""}${passage}${bestStart + cap < text.length ? "…" : ""}`;
16659
+ }
16660
+
16661
+ // src/features/magic-context/message-index.ts
16426
16662
  var lastIndexedStatements = new WeakMap;
16427
16663
  var insertMessageStatements = new WeakMap;
16428
16664
  var upsertProgressStatements = new WeakMap;
@@ -16684,10 +16920,10 @@ function getIndexableContent(role, parts) {
16684
16920
  if (!hasMeaningfulUserText2(parts)) {
16685
16921
  return "";
16686
16922
  }
16687
- return extractTexts2(parts).map(cleanUserText2).map(normalizeIndexText).filter((text) => text.length > 0).join(" / ");
16923
+ return extractTexts2(parts).map(removeTaskNotifications).map(cleanUserText2).map(normalizeIndexText).filter((text) => text.length > 0).join(" / ");
16688
16924
  }
16689
16925
  if (role === "assistant") {
16690
- return extractTexts2(parts).map(removeSystemReminders).map(normalizeIndexText).filter((text) => text.length > 0).join(" / ");
16926
+ return extractTexts2(parts).map(removeTaskNotifications).map(removeSystemReminders).map(normalizeIndexText).filter((text) => text.length > 0).join(" / ");
16691
16927
  }
16692
16928
  return "";
16693
16929
  }
@@ -16869,6 +17105,7 @@ function decodePiContentDecision(value) {
16869
17105
  // src/features/magic-context/storage-native-replay.ts
16870
17106
  var NATIVE_TOOL_INPUTS = "native tool inputs";
16871
17107
  var NATIVE_REASONING_IDS = "native reasoning ids";
17108
+ var nativeWriteBarriers = new WeakMap;
16872
17109
  function invalidPersistedReplayState(lane, sessionId) {
16873
17110
  return new Error(`invalid persisted ${lane} state for session ${sessionId}`);
16874
17111
  }
@@ -19964,6 +20201,93 @@ function getOpenCodeMessageById(sessionId, messageId) {
19964
20201
  const row = db.prepare(`SELECT id FROM message WHERE session_id = ? AND id = ? LIMIT 1`).get(sessionId, messageId);
19965
20202
  return row ?? null;
19966
20203
  }
20204
+ function planCompactionMarker(args, boundary) {
20205
+ const identity = `${args.sessionId}\x00${args.endMessageId}`;
20206
+ return {
20207
+ boundaryMessageId: boundary.id,
20208
+ summaryMessageId: generateMessageId(boundary.timeCreated + 1, 1n, `${identity}\x00summary-message`),
20209
+ compactionPartId: generatePartId(boundary.timeCreated, 1n, `${identity}\x00compaction-part`),
20210
+ summaryPartId: generatePartId(boundary.timeCreated + 1, 2n, `${identity}\x00summary-part`)
20211
+ };
20212
+ }
20213
+ function withCompactionMarkerPublicationLock(operation) {
20214
+ return runMarkerWriteTransaction(getWritableOpenCodeDb(), "compaction-marker-replace", operation);
20215
+ }
20216
+ var MARKER_PAIR_FROM = `FROM message m
20217
+ JOIN part p ON +p.session_id = m.session_id
20218
+ AND p.message_id = json_extract(m.data, '$.parentID')
20219
+ WHERE m.session_id = ?
20220
+ AND CASE WHEN json_valid(m.data)
20221
+ THEN COALESCE(json_extract(m.data, '$.summary'), 0) ELSE 0 END = 1
20222
+ AND COALESCE(json_extract(m.data, '$.finish'), '') <> ''
20223
+ AND COALESCE(json_extract(p.data, '$.type'), '') = 'compaction'`;
20224
+ var MC_MARKER_IDENTITY = `(COALESCE(json_extract(m.data, '$.providerID'), '') = 'magic-context'
20225
+ OR COALESCE(json_extract(m.data, '$.modelID'), '') = 'magic-context'
20226
+ OR EXISTS (SELECT 1 FROM part t
20227
+ WHERE t.message_id = m.id AND +t.session_id = m.session_id
20228
+ AND COALESCE(json_extract(t.data, '$.type'), '') = 'text'
20229
+ AND COALESCE(json_extract(t.data, '$.text'), '') = ?))`;
20230
+ function inspectCompactionMarkerPublication(sessionId, recognized, summaryText) {
20231
+ const db = getWritableOpenCodeDb();
20232
+ const ids = [...new Set(recognized.map((marker) => marker.summaryMessageId))];
20233
+ const placeholders = ids.map(() => "?").join(", ");
20234
+ const rogue = db.prepare(`SELECT 1 ${MARKER_PAIR_FROM}
20235
+ AND ${MC_MARKER_IDENTITY}
20236
+ AND m.id NOT IN (${placeholders})
20237
+ LIMIT 1`).get(sessionId, summaryText, ...ids);
20238
+ if (rogue)
20239
+ return { kind: "ambiguous" };
20240
+ const pairs = db.prepare(`SELECT m.id AS summaryId, p.message_id AS boundaryId, p.id AS partId
20241
+ ${MARKER_PAIR_FROM}
20242
+ AND m.id IN (${placeholders})
20243
+ LIMIT 2`).all(sessionId, ...ids);
20244
+ if (pairs.length > 1)
20245
+ return { kind: "ambiguous" };
20246
+ const rowById = db.prepare(`SELECT 1 FROM message WHERE id = ?
20247
+ UNION ALL SELECT 1 FROM part WHERE id IN (?, ?) LIMIT 1`);
20248
+ if (pairs.length === 0) {
20249
+ for (const marker of recognized) {
20250
+ if (rowById.get(marker.summaryMessageId, marker.compactionPartId, marker.summaryPartId))
20251
+ return { kind: "ambiguous" };
20252
+ }
20253
+ return { kind: "verified", marker: null };
20254
+ }
20255
+ const pair = pairs[0];
20256
+ const marker = recognized.find((item) => item.summaryMessageId === pair.summaryId && item.boundaryMessageId === pair.boundaryId && item.compactionPartId === pair.partId);
20257
+ if (!marker)
20258
+ return { kind: "ambiguous" };
20259
+ for (const other of recognized) {
20260
+ if (other.summaryMessageId === marker.summaryMessageId && other.compactionPartId === marker.compactionPartId && other.summaryPartId === marker.summaryPartId)
20261
+ continue;
20262
+ if (rowById.get(other.summaryMessageId, other.compactionPartId, other.summaryPartId))
20263
+ return { kind: "ambiguous" };
20264
+ }
20265
+ const row = db.prepare(`SELECT c.data AS compactionData FROM message m
20266
+ JOIN message b ON b.id = ? AND b.session_id = m.session_id
20267
+ JOIN part c ON c.id = ? AND c.message_id = b.id AND c.session_id = m.session_id
20268
+ JOIN part t ON t.id = ? AND t.message_id = m.id AND t.session_id = m.session_id
20269
+ WHERE m.id = ? AND m.session_id = ?
20270
+ AND json_extract(b.data, '$.role') = 'user'
20271
+ AND json_extract(m.data, '$.role') = 'assistant'
20272
+ AND json_extract(m.data, '$.parentID') = b.id
20273
+ AND json_extract(m.data, '$.summary') = 1
20274
+ AND json_extract(m.data, '$.finish') = 'stop'
20275
+ AND json_extract(m.data, '$.error') IS NULL
20276
+ AND json_extract(m.data, '$.providerID') = 'magic-context'
20277
+ AND json_extract(m.data, '$.modelID') = 'magic-context'
20278
+ AND json_extract(t.data, '$.type') = 'text' AND json_extract(t.data, '$.text') = ?
20279
+ AND (SELECT COUNT(*) FROM part p WHERE p.message_id = m.id AND +p.session_id = m.session_id) = 1`).get(marker.boundaryMessageId, marker.compactionPartId, marker.summaryPartId, marker.summaryMessageId, sessionId, summaryText);
20280
+ if (!row || !isMcCanonicalCompactionPartData(JSON.parse(row.compactionData)) || findBoundaryUserMessage(sessionId, marker.boundaryMessageId)?.id !== marker.boundaryMessageId)
20281
+ return { kind: "ambiguous" };
20282
+ return { kind: "verified", marker };
20283
+ }
20284
+ function isCompactionMarkerIntentAbsent(marker, targetEndMessageId) {
20285
+ if (!targetEndMessageId)
20286
+ return false;
20287
+ const db = getWritableOpenCodeDb();
20288
+ return !db.prepare(`SELECT 1 FROM message WHERE id IN (?, ?)
20289
+ UNION ALL SELECT 1 FROM part WHERE id IN (?, ?) LIMIT 1`).get(targetEndMessageId, marker.summaryMessageId, marker.compactionPartId, marker.summaryPartId);
20290
+ }
19967
20291
  function removeLegacyMarkerLineageRows(db, args) {
19968
20292
  const legacySummaries = db.prepare(`SELECT m.id
19969
20293
  FROM message m
@@ -20007,10 +20331,11 @@ function injectCompactionMarker(args) {
20007
20331
  return null;
20008
20332
  }
20009
20333
  const boundaryTime = boundary.timeCreated;
20010
- const markerIdentity = `${args.sessionId}\x00${args.endMessageId}`;
20011
- const summaryMsgId = generateMessageId(boundaryTime + 1, 1n, `${markerIdentity}\x00summary-message`);
20012
- const compactionPartId = generatePartId(boundaryTime, 1n, `${markerIdentity}\x00compaction-part`);
20013
- const summaryPartId = generatePartId(boundaryTime + 1, 2n, `${markerIdentity}\x00summary-part`);
20334
+ const {
20335
+ summaryMessageId: summaryMsgId,
20336
+ compactionPartId,
20337
+ summaryPartId
20338
+ } = planCompactionMarker(args, boundary);
20014
20339
  const summaryMsgData = JSON.stringify({
20015
20340
  role: "assistant",
20016
20341
  parentID: boundary.id,
@@ -20148,10 +20473,12 @@ function listSessionCompactionMarkers(sessionId) {
20148
20473
  markers.sort((left, right) => left.timeCreated - right.timeCreated || left.compactionPartId.localeCompare(right.compactionPartId));
20149
20474
  return markers.map(({ timeCreated: _, ...marker }) => marker);
20150
20475
  }
20151
- function removeForeignCompactionMarker(sessionId, marker, protectedSummaryMessageId) {
20476
+ function removeForeignCompactionMarker(sessionId, marker, protectedSummaryMessageId, shouldRemove) {
20152
20477
  try {
20153
20478
  const db = getWritableOpenCodeDb("removal");
20154
20479
  runMarkerRemovalTransaction(db, () => {
20480
+ if (shouldRemove && !shouldRemove())
20481
+ return;
20155
20482
  const deletePartsOfMessage = db.prepare("DELETE FROM part WHERE +session_id = ? AND message_id = ?");
20156
20483
  const deleteMessage = db.prepare("DELETE FROM message WHERE session_id = ? AND id = ?");
20157
20484
  for (const summaryMessageId of marker.summaryMessageIds) {
@@ -20332,24 +20659,114 @@ function removeMcOwnedCompactionMarkers(sessionId, summaryText) {
20332
20659
  };
20333
20660
  }
20334
20661
 
20335
- // src/hooks/magic-context/compaction-marker-manager.ts
20336
- var MARKER_SUMMARY_TEXT = "[Compacted by magic-context — session history is managed by the plugin]";
20337
- function dropMarkerSummaryTag(db, sessionId, summaryMessageId) {
20338
- const tagNumber = getTagNumberByMessageId(db, sessionId, `${summaryMessageId}:p0`);
20339
- if (tagNumber !== null)
20340
- updateTagStatus(db, sessionId, tagNumber, "dropped");
20341
- }
20342
- function persistMarkerStateAndDropReplacedTag(db, sessionId, state, replacedSummaryMessageId) {
20343
- let transactionStartedAt = 0;
20344
- db.transaction(() => {
20345
- transactionStartedAt = performance.now();
20346
- setPersistedCompactionMarkerState(db, sessionId, state);
20347
- if (replacedSummaryMessageId !== null) {
20348
- dropMarkerSummaryTag(db, sessionId, replacedSummaryMessageId);
20662
+ // src/hooks/magic-context/compaction-marker-publication.ts
20663
+ function samePair(left, right) {
20664
+ return left.boundaryMessageId === right.boundaryMessageId && left.summaryMessageId === right.summaryMessageId && left.compactionPartId === right.compactionPartId && left.summaryPartId === right.summaryPartId;
20665
+ }
20666
+ function publishRecoverableCompactionMarker(db, args, targetIsCurrent) {
20667
+ const { sessionId } = args;
20668
+ let raw = "";
20669
+ let previous = null;
20670
+ let oldSummary = null;
20671
+ let intended;
20672
+ let reason = "publication-failed";
20673
+ let recovered;
20674
+ let cut = "uncertain";
20675
+ let writingState = false;
20676
+ const persist = (state) => {
20677
+ writingState = true;
20678
+ db.transaction(() => {
20679
+ raw = compareAndSetCompactionMarkerPublicationState(db, sessionId, raw, state, null);
20680
+ if (previous && previous.summaryMessageId !== state.summaryMessageId) {
20681
+ const tag = getTagNumberByMessageId(db, sessionId, `${previous.summaryMessageId}:p0`);
20682
+ if (tag !== null)
20683
+ updateTagStatus(db, sessionId, tag, "dropped");
20684
+ }
20685
+ }).immediate();
20686
+ writingState = false;
20687
+ previous = state;
20688
+ };
20689
+ try {
20690
+ cut = getCompactionMarkerPublicationState(db, sessionId).pending ? "uncertain" : "definitely-no-cut";
20691
+ const result = withCompactionMarkerPublicationLock(() => {
20692
+ const snapshot = getCompactionMarkerPublicationState(db, sessionId);
20693
+ if (snapshot.pending)
20694
+ cut = "uncertain";
20695
+ raw = snapshot.raw;
20696
+ previous = getPersistedCompactionMarkerState(db, sessionId) ?? getDeferredClearedCompactionMarkerState(db, sessionId);
20697
+ oldSummary = previous?.summaryMessageId ?? null;
20698
+ if (snapshot.pending && (snapshot.pending.boundaryOrdinal > args.endOrdinal || snapshot.pending.boundaryOrdinal === args.endOrdinal && snapshot.pending.targetEndMessageId !== args.endMessageId) && !isCompactionMarkerIntentAbsent(snapshot.pending, snapshot.pending.targetEndMessageId))
20699
+ throw new Error("a newer marker publication intent is pending");
20700
+ const boundary = findBoundaryUserMessage(sessionId, args.endMessageId);
20701
+ if (!targetIsCurrent() || !boundary || boundary.id !== args.resolvedBoundary?.id || boundary.timeCreated !== args.resolvedBoundary.timeCreated)
20702
+ throw new Error("marker publication frontier changed before publication");
20703
+ intended = {
20704
+ ...planCompactionMarker(args, boundary),
20705
+ boundaryOrdinal: args.endOrdinal,
20706
+ targetEndMessageId: args.endMessageId
20707
+ };
20708
+ let pending = snapshot.pending;
20709
+ if (pending) {
20710
+ const pendingTarget = pending.targetEndMessageId;
20711
+ const pendingBoundary = pendingTarget ? findBoundaryUserMessage(sessionId, pendingTarget) : null;
20712
+ const compartments = pendingTarget ? getCompartmentsByEndMessageId(db, sessionId, pendingTarget) : [];
20713
+ const validTarget = pending.targetEndMessageId === args.endMessageId && pending.boundaryOrdinal === args.endOrdinal || compartments.length === 1 && compartments[0].endMessage === pending.boundaryOrdinal;
20714
+ if (!validTarget || !pendingBoundary || !pendingTarget || !samePair(pending, planCompactionMarker({ ...args, endMessageId: pendingTarget }, pendingBoundary)))
20715
+ pending = null;
20716
+ }
20717
+ const inspection = inspectCompactionMarkerPublication(sessionId, [intended, ...previous ? [previous] : [], ...pending ? [pending] : []], args.summaryText);
20718
+ if (inspection.kind === "ambiguous") {
20719
+ reason = "ambiguous-left-alone";
20720
+ return {
20721
+ kind: cut,
20722
+ error: new Error("unverified or ambiguous host marker pairs; left alone")
20723
+ };
20724
+ }
20725
+ cut = "definitely-no-cut";
20726
+ const host = inspection.marker;
20727
+ if (host && (pending && samePair(host, pending) || samePair(host, intended))) {
20728
+ const adopted = pending && samePair(host, pending) ? pending : intended;
20729
+ recovered = adopted === pending ? "pending-intent-recovered" : "adopted-existing-pair";
20730
+ cut = "uncertain";
20731
+ if (samePair(host, intended))
20732
+ return { kind: "committed", marker: host };
20733
+ persist(adopted);
20734
+ }
20735
+ writingState = true;
20736
+ raw = compareAndSetCompactionMarkerPublicationState(db, sessionId, raw, previous, intended);
20737
+ writingState = false;
20738
+ cut = "uncertain";
20739
+ const replacement = replaceCompactionMarker(host, {
20740
+ ...args,
20741
+ resolvedBoundary: boundary
20742
+ });
20743
+ if (recovered && replacement.kind !== "committed")
20744
+ return { kind: "uncertain", error: replacement.error };
20745
+ if (replacement.kind !== "committed")
20746
+ cut = replacement.kind === "uncertain" ? "uncertain" : "definitely-no-cut";
20747
+ return replacement;
20748
+ });
20749
+ if (result.kind === "committed") {
20750
+ if (!intended)
20751
+ throw new Error("committed marker has no publication intent");
20752
+ persist(intended);
20753
+ reason = recovered ?? "published";
20349
20754
  }
20350
- }).immediate();
20351
- logSlowWriteTransaction("marker-drain", transactionStartedAt);
20755
+ return result;
20756
+ } catch (error) {
20757
+ if (writingState)
20758
+ reason = "state-write-failed";
20759
+ return {
20760
+ kind: cut,
20761
+ error: error instanceof Error ? error : new Error(String(error))
20762
+ };
20763
+ } finally {
20764
+ sessionLog(sessionId, `compaction-marker publication reason=${reason} oldSummary=${oldSummary ?? "none"} newSummary=${intended?.summaryMessageId ?? "none"} target=${args.endMessageId} at=${Date.now()}`);
20765
+ }
20352
20766
  }
20767
+
20768
+ // src/hooks/magic-context/compaction-marker-manager.ts
20769
+ var MARKER_SUMMARY_TEXT = "[Compacted by magic-context — session history is managed by the plugin]";
20353
20770
  function validatePendingTarget(db, sessionId, pending) {
20354
20771
  const ocMessage = getOpenCodeMessageById(sessionId, pending.endMessageId);
20355
20772
  if (!ocMessage) {
@@ -20415,35 +20832,47 @@ function applyDeferredCompactionMarker(db, sessionId, pending, directory, truste
20415
20832
  stepStartedAt = now;
20416
20833
  };
20417
20834
  try {
20835
+ const unresolvedIntentFailure = () => {
20836
+ if (getCompactionMarkerPublicationState(db, sessionId).pending) {
20837
+ return {
20838
+ kind: "retryable-failure",
20839
+ error: new Error("unresolved marker publication intent; retaining retry state")
20840
+ };
20841
+ }
20842
+ return null;
20843
+ };
20418
20844
  const responseFencesTarget = trustedBoundary !== undefined && Number.isSafeInteger(trustedBoundary.rowVersion) && trustedBoundary.rowVersion > 0 && trustedBoundary.ordinal === pending.ordinal && trustedBoundary.endMessageId === pending.endMessageId;
20419
20845
  const validation = responseFencesTarget ? "ok" : validatePendingTarget(db, sessionId, pending);
20420
20846
  endStep("validate");
20421
20847
  if (validation !== "ok") {
20848
+ const unresolved = unresolvedIntentFailure();
20849
+ if (unresolved)
20850
+ return unresolved;
20422
20851
  sessionLog(sessionId, `compaction-marker drain: stale-skip (${validation}) for ordinal ${pending.ordinal} endMessageId=${pending.endMessageId}`);
20423
20852
  return { kind: "stale-skip", reason: validation };
20424
20853
  }
20425
20854
  const existing = getPersistedCompactionMarkerState(db, sessionId);
20426
20855
  if (existing && existingMarkerAlreadyCoversTarget(sessionId, existing, pending.ordinal, pending.endMessageId)) {
20427
- return { kind: "already-current" };
20856
+ if (!unresolvedIntentFailure())
20857
+ return { kind: "already-current" };
20428
20858
  }
20429
20859
  const boundary = findBoundaryUserMessage(sessionId, pending.endMessageId);
20430
20860
  endStep("boundary");
20431
20861
  if (!boundary) {
20432
- return {
20862
+ return unresolvedIntentFailure() ?? {
20433
20863
  kind: "retryable-failure",
20434
20864
  error: new Error(`no user boundary found at or before endMessageId ${pending.endMessageId} (ordinal ${pending.ordinal}); preserving existing marker`)
20435
20865
  };
20436
20866
  }
20437
20867
  endStep("discardCheck");
20438
- const removedSummaryMessageId = existing?.summaryMessageId ?? null;
20439
- const replacement = replaceCompactionMarker(existing, {
20868
+ const replacement = publishRecoverableCompactionMarker(db, {
20440
20869
  sessionId,
20441
20870
  endOrdinal: pending.ordinal,
20442
20871
  endMessageId: pending.endMessageId,
20443
20872
  summaryText: MARKER_SUMMARY_TEXT,
20444
20873
  directory: directory ?? process.cwd(),
20445
20874
  resolvedBoundary: boundary
20446
- });
20875
+ }, () => JSON.stringify(getPersistedCompactionMarkerState(db, sessionId)) === JSON.stringify(existing) && (responseFencesTarget || validatePendingTarget(db, sessionId, pending) === "ok"));
20447
20876
  endStep("replace");
20448
20877
  if (replacement.kind !== "committed") {
20449
20878
  return {
@@ -20451,14 +20880,8 @@ function applyDeferredCompactionMarker(db, sessionId, pending, directory, truste
20451
20880
  error: new Error(`atomic marker replacement failed for ordinal ${pending.ordinal}; will retry`)
20452
20881
  };
20453
20882
  }
20454
- const result = replacement.marker;
20455
- persistMarkerStateAndDropReplacedTag(db, sessionId, {
20456
- ...result,
20457
- boundaryOrdinal: pending.ordinal,
20458
- targetEndMessageId: pending.endMessageId
20459
- }, removedSummaryMessageId);
20460
20883
  endStep("persist");
20461
- sessionLog(sessionId, `compaction-marker drain: applied at ordinal ${pending.ordinal}, boundary user msg ${result.boundaryMessageId} ${stepMs.join(" ")}`);
20884
+ sessionLog(sessionId, `compaction-marker drain timings: ${stepMs.join(" ")}`);
20462
20885
  return {
20463
20886
  kind: "applied",
20464
20887
  markerOrdinal: pending.ordinal
@@ -20479,8 +20902,7 @@ function updateCompactionMarkerAfterPublication(db, sessionId, lastCompartmentEn
20479
20902
  return false;
20480
20903
  }
20481
20904
  const existing = getPersistedCompactionMarkerState(db, sessionId);
20482
- const removedSummaryMessageId = existing?.summaryMessageId ?? null;
20483
- if (existing) {
20905
+ if (existing && !getCompactionMarkerPublicationState(db, sessionId).pending) {
20484
20906
  if (existingMarkerAlreadyCoversTarget(sessionId, existing, lastCompartmentEnd, targetEndMessageId)) {
20485
20907
  return true;
20486
20908
  }
@@ -20490,33 +20912,26 @@ function updateCompactionMarkerAfterPublication(db, sessionId, lastCompartmentEn
20490
20912
  sessionLog(sessionId, `compaction-marker: no user boundary found at or before endMessageId ${targetEndMessageId} (ordinal ${lastCompartmentEnd}); preserving existing marker`);
20491
20913
  return false;
20492
20914
  }
20493
- const replacement = replaceCompactionMarker(existing, {
20915
+ const replacement = publishRecoverableCompactionMarker(db, {
20494
20916
  sessionId,
20495
20917
  endOrdinal: lastCompartmentEnd,
20496
20918
  endMessageId: targetEndMessageId,
20497
20919
  summaryText: MARKER_SUMMARY_TEXT,
20498
20920
  directory: directory ?? process.cwd(),
20499
20921
  resolvedBoundary: boundary
20500
- });
20922
+ }, () => JSON.stringify(getPersistedCompactionMarkerState(db, sessionId)) === JSON.stringify(existing) && getCompartmentEndMessageIdForOrdinal(db, sessionId, lastCompartmentEnd) === targetEndMessageId);
20501
20923
  if (replacement.kind === "committed") {
20502
- const result = replacement.marker;
20503
- persistMarkerStateAndDropReplacedTag(db, sessionId, {
20504
- ...result,
20505
- boundaryOrdinal: lastCompartmentEnd,
20506
- targetEndMessageId
20507
- }, removedSummaryMessageId);
20508
- sessionLog(sessionId, `compaction-marker: injected at ordinal ${lastCompartmentEnd}, boundary user msg ${result.boundaryMessageId}`);
20509
20924
  return true;
20510
20925
  }
20511
20926
  return false;
20512
20927
  }
20513
- function reconcileForkOrphanedCompactionMarkers(db, sessionId) {
20514
- if (getHarness() !== "opencode") {
20928
+ function reconcileForkOrphanedCompactionMarkers(db, sessionId, isCacheBusting) {
20929
+ if (!isCacheBusting || getHarness() !== "opencode") {
20515
20930
  return { removed: 0, failed: false };
20516
20931
  }
20517
20932
  try {
20518
20933
  const owned = getPersistedCompactionMarkerState(db, sessionId);
20519
- if (!owned) {
20934
+ if (!owned || getCompactionMarkerPublicationState(db, sessionId).pending) {
20520
20935
  return { removed: 0, failed: false };
20521
20936
  }
20522
20937
  const markers = listSessionCompactionMarkers(sessionId);
@@ -20537,7 +20952,15 @@ function reconcileForkOrphanedCompactionMarkers(db, sessionId) {
20537
20952
  if (!currentOwned || marker.compactionPartId === currentOwned.compactionPartId) {
20538
20953
  continue;
20539
20954
  }
20540
- const ok = removeForeignCompactionMarker(sessionId, marker, currentOwned.summaryMessageId);
20955
+ let protectedByState = false;
20956
+ const ok = removeForeignCompactionMarker(sessionId, marker, currentOwned.summaryMessageId, () => db.transaction(() => {
20957
+ const publication = getCompactionMarkerPublicationState(db, sessionId);
20958
+ const latestOwned = getPersistedCompactionMarkerState(db, sessionId);
20959
+ protectedByState = Boolean(publication.pending || !latestOwned || marker.compactionPartId === latestOwned.compactionPartId || marker.summaryMessageIds.includes(latestOwned.summaryMessageId) || compareOpenCodeMessagesByCanonicalOrder(sessionId, marker.boundaryMessageId, latestOwned.boundaryMessageId) !== 1);
20960
+ return !protectedByState;
20961
+ }).deferred());
20962
+ if (protectedByState)
20963
+ continue;
20541
20964
  if (ok) {
20542
20965
  removed += 1;
20543
20966
  sessionLog(sessionId, `compaction-marker hygiene: removed fork-orphaned marker part=${marker.compactionPartId} boundary=${marker.boundaryMessageId} (outranked owned boundary ${currentOwned.boundaryMessageId})`);
@@ -21151,7 +21574,18 @@ function selectHiddenMessagesAtCompactionSeam(messagesBeforeTrim, skippedVisible
21151
21574
  }
21152
21575
  return markerBoundaryIndex < 0 ? [] : trimmed.slice(markerBoundaryIndex + 1);
21153
21576
  }
21154
- function prepareCompartmentInjection(db, sessionId, messages, isCacheBusting, projectPath, injectionBudgetTokens, temporalAwareness) {
21577
+ function cutCoveredPrefix(messages, count, coveredInstructions) {
21578
+ const removed = messages.splice(0, count);
21579
+ if (!coveredInstructions)
21580
+ return;
21581
+ for (const message of removed) {
21582
+ if (!isHostRenderedSystemMessage(message))
21583
+ continue;
21584
+ markCoveredInstructionRow(message.info);
21585
+ coveredInstructions.push(message);
21586
+ }
21587
+ }
21588
+ function prepareCompartmentInjection(db, sessionId, messages, isCacheBusting, projectPath, injectionBudgetTokens, temporalAwareness, coveredInstructions) {
21155
21589
  const cached = injectionCache.get(sessionId);
21156
21590
  if (cached && cached.db !== db) {
21157
21591
  clearInjectionCache(sessionId);
@@ -21168,8 +21602,7 @@ function prepareCompartmentInjection(db, sessionId, messages, isCacheBusting, pr
21168
21602
  if (prepared.compartmentEndMessageId.length > 0) {
21169
21603
  const cutoffIndex = findBoundaryIndex(sessionId, messages, prepared.compartmentEndMessageId);
21170
21604
  if (cutoffIndex >= 0) {
21171
- const remaining = messages.slice(cutoffIndex + 1);
21172
- messages.splice(0, messages.length, ...remaining);
21605
+ cutCoveredPrefix(messages, cutoffIndex + 1, coveredInstructions);
21173
21606
  } else {
21174
21607
  sessionLog(sessionId, `compartment injection: cached boundary ${prepared.compartmentEndMessageId} not in messages (already trimmed), reusing cache`);
21175
21608
  }
@@ -21291,13 +21724,12 @@ function prepareCompartmentInjection(db, sessionId, messages, isCacheBusting, pr
21291
21724
  if (cutoffIndex >= 0) {
21292
21725
  clearDegradedRebuild(sessionId);
21293
21726
  skippedVisibleMessages = cutoffIndex + 1;
21294
- const remaining = messages.slice(cutoffIndex + 1);
21295
- messages.splice(0, messages.length, ...remaining);
21727
+ cutCoveredPrefix(messages, cutoffIndex + 1, coveredInstructions);
21296
21728
  resultEndMessageId = trimEndMessageId;
21297
21729
  } else {
21298
21730
  const degradedCount = noteDegradedRebuild(sessionId);
21299
- if (degradedCount === 1) {
21300
- reconcileForkOrphanedCompactionMarkers(db, sessionId);
21731
+ if (isCacheBusting) {
21732
+ reconcileForkOrphanedCompactionMarkers(db, sessionId, isCacheBusting);
21301
21733
  }
21302
21734
  let reAnchored = false;
21303
21735
  if (degradedCount >= REANCHOR_MIN_DEGRADED_PASSES && isCacheBusting) {
@@ -21312,8 +21744,7 @@ function prepareCompartmentInjection(db, sessionId, messages, isCacheBusting, pr
21312
21744
  const reAnchorCutoff = findBoundaryIndex(sessionId, messages, reAnchorCompartment.endMessageId);
21313
21745
  if (reAnchorCutoff >= 0) {
21314
21746
  skippedVisibleMessages = reAnchorCutoff + 1;
21315
- const remaining = messages.slice(reAnchorCutoff + 1);
21316
- messages.splice(0, messages.length, ...remaining);
21747
+ cutCoveredPrefix(messages, reAnchorCutoff + 1, coveredInstructions);
21317
21748
  resultEndMessage = reAnchorCompartment.endMessage;
21318
21749
  resultEndMessageId = reAnchorCompartment.endMessageId;
21319
21750
  reAnchored = true;
@@ -22915,7 +23346,7 @@ function trimToPreparedPrefix(options, prepared) {
22915
23346
  for (const [index, message] of options.messages.entries()) {
22916
23347
  const id = message.info.id;
22917
23348
  if (isHostRenderedSystemMessage(message)) {
22918
- if (lastSourcePosition >= boundaryPosition)
23349
+ if (!sawPersistedRow || lastSourcePosition >= boundaryPosition)
22919
23350
  retained.push(message);
22920
23351
  continue;
22921
23352
  }
@@ -23185,6 +23616,31 @@ function injectM0M1(options) {
23185
23616
  };
23186
23617
  }
23187
23618
 
23619
+ // src/hooks/magic-context/file-token-coverage.ts
23620
+ function omitIncompleteFileTokenTotals(db, sessionId, messages, totals) {
23621
+ const filesByMessage = new Map;
23622
+ for (const message of messages) {
23623
+ const count = message.parts.filter(isFilePart).length;
23624
+ if (count > 0 && totals.has(message.id))
23625
+ filesByMessage.set(message.id, count);
23626
+ }
23627
+ if (filesByMessage.size === 0)
23628
+ return totals;
23629
+ const rows = db.prepare(`
23630
+ SELECT o.value AS owner, COUNT(t.id) AS count
23631
+ FROM json_each(?) o LEFT JOIN tags t
23632
+ ON t.session_id = ? AND t.type = 'file'
23633
+ AND t.message_id >= o.value || ':file'
23634
+ AND t.message_id < o.value || ':file;'
23635
+ GROUP BY o.value
23636
+ `).all(JSON.stringify([...filesByMessage.keys()]), sessionId);
23637
+ for (const row of rows) {
23638
+ if (row.count < (filesByMessage.get(row.owner) ?? 0))
23639
+ totals.delete(row.owner);
23640
+ }
23641
+ return totals;
23642
+ }
23643
+
23188
23644
  // src/hooks/magic-context/protected-tail-boundary.ts
23189
23645
  function describeBoundaryDiagnostics(snapshot) {
23190
23646
  return `boundaryDiagnostics=${JSON.stringify(snapshot.diagnostics ?? { unavailable: true, boundaryReason: snapshot.boundaryReason })}`;
@@ -23410,7 +23866,7 @@ function resolveProtectedTailBoundary(ctx) {
23410
23866
  const offset = Math.max(1, ctx.lastCompartmentEndOrdinal + 1);
23411
23867
  const absoluteMessageCount = getCachedAbsoluteMessageCount2(ctx.sessionId) ?? getRawSessionMessageOrdinalCount2(ctx.sessionId);
23412
23868
  const messages = readRawSessionMessageRange2(ctx.sessionId, Math.max(1, offset - 1), absoluteMessageCount);
23413
- const storedTotals = ctx.storedTokenTotals;
23869
+ const storedTotals = ctx.storedTokenTotalsForMessages?.(messages.map((m) => m.id), messages) ?? ctx.storedTokenTotals;
23414
23870
  const index = buildTrueRawTokenIndex(ctx.sessionId, messages, {
23415
23871
  providerShapeVersion: ctx.providerShapeVersion,
23416
23872
  cacheNamespace: ctx.cacheNamespace,
@@ -23676,6 +24132,7 @@ function resolveBoundaryContext(args) {
23676
24132
  providerShapeVersion: args.providerShapeVersion ?? "opencode-v1",
23677
24133
  cacheNamespace: args.cacheNamespace ?? `opencode:${args.sessionId}`,
23678
24134
  storedTokenTotals,
24135
+ storedTokenTotalsForMessages: (_ids, messages = []) => storedTokenTotals && omitIncompleteFileTokenTotals(args.db, args.sessionId, messages, new Map(storedTokenTotals)),
23679
24136
  calibration
23680
24137
  };
23681
24138
  }
@@ -23688,13 +24145,14 @@ function resolveWrapupProtectedTailBoundary(args) {
23688
24145
  const offset = Math.max(1, ctx.lastCompartmentEndOrdinal + 1);
23689
24146
  const absoluteMessageCount = getCachedAbsoluteMessageCount2(ctx.sessionId) ?? getRawSessionMessageOrdinalCount2(ctx.sessionId);
23690
24147
  const messages = readRawSessionMessageRange2(ctx.sessionId, Math.max(1, offset - 1), absoluteMessageCount);
24148
+ const storedTotals = ctx.storedTokenTotalsForMessages?.(messages.map((m) => m.id), messages) ?? ctx.storedTokenTotals;
23691
24149
  const index = buildTrueRawTokenIndex(ctx.sessionId, messages, {
23692
24150
  providerShapeVersion: ctx.providerShapeVersion,
23693
24151
  cacheNamespace: ctx.cacheNamespace,
23694
24152
  calibration: ctx.calibration,
23695
24153
  absoluteMessageCount,
23696
- storedTotalForMessage: ctx.storedTokenTotals ? (m) => {
23697
- const value = ctx.storedTokenTotals?.get(m.id);
24154
+ storedTotalForMessage: storedTotals ? (m) => {
24155
+ const value = storedTotals.get(m.id);
23698
24156
  return value === undefined ? null : value;
23699
24157
  } : undefined
23700
24158
  });
@@ -24374,14 +24832,14 @@ function shortErr(error) {
24374
24832
  }
24375
24833
  return extractMessage(error);
24376
24834
  }
24377
- async function attemptOnce(client, args, timeoutMs, signal, callContext, label, transport) {
24835
+ async function attemptOnce(client, args, timeoutMs, signal, callContext, label, transport, allowModelSuggestions = true) {
24378
24836
  const originalBody = { ...args.body };
24379
24837
  const attemptArgs = copyPromptArgs(args, originalBody);
24380
24838
  try {
24381
24839
  await promptWithTimeout(client, attemptArgs, timeoutMs, signal, transport);
24382
24840
  return;
24383
24841
  } catch (error) {
24384
- if (isNonRetryable(error, signal))
24842
+ if (isNonRetryable(error, signal) || !allowModelSuggestions)
24385
24843
  throw error;
24386
24844
  const suggestion = parseModelSuggestion(error);
24387
24845
  if (!suggestion || !originalBody.model) {
@@ -24409,7 +24867,7 @@ async function promptSyncWithModelSuggestionRetry(client, args, options = {}) {
24409
24867
  const explicitPrimaryLabel = baseBody.model?.providerID && baseBody.model.modelID ? `${baseBody.model.providerID}/${baseBody.model.modelID}` : "primary";
24410
24868
  let lastError = null;
24411
24869
  try {
24412
- await attemptOnce(client, baseArgs, timeoutMs, options.signal, callContext, explicitPrimaryLabel, options.transport);
24870
+ await attemptOnce(client, baseArgs, timeoutMs, options.signal, callContext, explicitPrimaryLabel, options.transport, options.allowModelSuggestions);
24413
24871
  return;
24414
24872
  } catch (error) {
24415
24873
  lastError = error;
@@ -24435,7 +24893,7 @@ async function promptSyncWithModelSuggestionRetry(client, args, options = {}) {
24435
24893
  ...fallback?.qualifier ? { variant: fallback.qualifier } : {}
24436
24894
  });
24437
24895
  try {
24438
- await attemptOnce(client, attemptArgs, timeoutMs, options.signal, callContext, label, options.transport);
24896
+ await attemptOnce(client, attemptArgs, timeoutMs, options.signal, callContext, label, options.transport, options.allowModelSuggestions);
24439
24897
  log(`[${callContext}] fallback succeeded with ${label} (attempt ${i + 2}/${fallbacks.length + 1})`);
24440
24898
  return;
24441
24899
  } catch (error) {
@@ -26052,6 +26510,7 @@ function buildHistorianRepairPrompt(originalPrompt, previousOutput, validationEr
26052
26510
  "Return a corrected full XML response for the same existing state and new messages.",
26053
26511
  "Do not skip any displayed raw ordinal or displayed raw range, even if the message looks trivial.",
26054
26512
  "Every displayed message range must belong to exactly one compartment unless it is intentionally left in one trailing suffix marked by <unprocessed_from>.",
26513
+ "If every message in the chunk is covered by a compartment, nothing is unprocessed: set `<unprocessed_from>` to the chunk's last message index + 1.",
26055
26514
  "",
26056
26515
  "Previous invalid XML:",
26057
26516
  previousOutput
@@ -26555,6 +27014,7 @@ async function runHistorianPrompt(args) {
26555
27014
  return executor.attempt(opened, request);
26556
27015
  }, { childSessionId: opened.childSessionId }),
26557
27016
  timeoutMs: timeoutMs ?? DEFAULT_HISTORIAN_TIMEOUT_MS,
27017
+ allowModelSuggestions: false,
26558
27018
  fallbackModels: modelOverride ? undefined : fallbackModels,
26559
27019
  callContext: agentId === HISTORIAN_EDITOR_AGENT ? "historian:editor" : "historian"
26560
27020
  });
@@ -26631,7 +27091,7 @@ async function runFallbackHistorianPass(args) {
26631
27091
  const primary = toModelEntry(args.model);
26632
27092
  for (const candidateInput of [
26633
27093
  ...args.fallbackModels ?? [],
26634
- ...args.fallbackModelId ? [{ model: args.fallbackModelId }] : []
27094
+ ...!primary && args.fallbackModelId ? [{ model: args.fallbackModelId }] : []
26635
27095
  ]) {
26636
27096
  const candidate = toModelEntry(candidateInput);
26637
27097
  if (!candidate)
@@ -26771,6 +27231,56 @@ function sanitizeDumpName(value) {
26771
27231
  return value.replace(/[^a-zA-Z0-9._-]/g, "-");
26772
27232
  }
26773
27233
 
27234
+ // src/hooks/magic-context/historian-history-wait.ts
27235
+ var historyWaits = new Map;
27236
+ function newestMessageId(snapshot) {
27237
+ try {
27238
+ const count = getRawSessionMessageOrdinalCount2(snapshot.sessionId);
27239
+ const ids = readRawSessionMessageIdOrdinalsForRange2(snapshot.sessionId, count, count);
27240
+ const newest = ids.keys().next().value;
27241
+ if (newest)
27242
+ return newest;
27243
+ } catch {}
27244
+ return snapshot.rawLastMessageIdAtTrigger;
27245
+ }
27246
+ function recoveryWindow(snapshot, percentage) {
27247
+ return percentage >= escalationBands(snapshot.executeThresholdPercentage).forceMaterializationPercentage || snapshot.emergencyTailScale !== undefined;
27248
+ }
27249
+ function clearHistorianHistoryWait(sessionId) {
27250
+ historyWaits.delete(sessionId);
27251
+ }
27252
+ function recordHistorianNoProgress(snapshot, emergency = false) {
27253
+ const newest = newestMessageId(snapshot);
27254
+ if (!newest)
27255
+ return;
27256
+ const previous = historyWaits.get(snapshot.sessionId);
27257
+ const sameHistory = previous?.newestMessageId === newest;
27258
+ historyWaits.set(snapshot.sessionId, {
27259
+ newestMessageId: newest,
27260
+ recoveryAttempted: sameHistory && previous.recoveryAttempted || emergency || recoveryWindow(snapshot, snapshot.usagePercentage)
27261
+ });
27262
+ }
27263
+ function shouldWaitForNewHistorianHistory(snapshot, options = {}) {
27264
+ const wait = historyWaits.get(snapshot.sessionId);
27265
+ if (!wait)
27266
+ return false;
27267
+ const newest = newestMessageId(snapshot);
27268
+ if (newest && newest !== wait.newestMessageId) {
27269
+ try {
27270
+ const order = compareRawSessionMessageOrder2(snapshot.sessionId, newest, wait.newestMessageId);
27271
+ if (order === null || order > 0)
27272
+ return false;
27273
+ } catch {}
27274
+ }
27275
+ const recovery = options.emergency || recoveryWindow(snapshot, options.usagePercentage ?? snapshot.usagePercentage);
27276
+ if (recovery && !wait.recoveryAttempted) {
27277
+ if (options.claim)
27278
+ wait.recoveryAttempted = true;
27279
+ return false;
27280
+ }
27281
+ return true;
27282
+ }
27283
+
26774
27284
  // src/hooks/magic-context/reference-seeds.generated.ts
26775
27285
  var REFERENCE_SEEDS = [
26776
27286
  {
@@ -28843,6 +29353,24 @@ function findDanglingPublicationBoundary(sessionId, compartments, messageExists
28843
29353
  }
28844
29354
  return null;
28845
29355
  }
29356
+ function isServingOrdinalDriftSignal(detail) {
29357
+ if (!detail)
29358
+ return false;
29359
+ if (detail === "raw message count shrank")
29360
+ return true;
29361
+ return /(?:offset|protectedTailStart|eligibleEnd) ordinal \d+ id changed$/.test(detail);
29362
+ }
29363
+ function shouldRefuseStaleSnapshotForDrift(sessionId, detail) {
29364
+ if (hasRawMessageProvider2(sessionId) || !openCodeDbExists())
29365
+ return false;
29366
+ return isServingOrdinalDriftSignal(detail);
29367
+ }
29368
+ function dropDriftedServingOrdinalRanks(sessionId, detail) {
29369
+ if (hasRawMessageProvider2(sessionId) || !openCodeDbExists())
29370
+ return;
29371
+ withReadOnlySessionDb((reader) => dropRawSessionOrdinalCheckpoints(reader, sessionId));
29372
+ sessionLog(sessionId, `historian: dropped drifted ordinal checkpoints after exact-read disagreement (${detail}); the next run rebuilds them from a full scan`);
29373
+ }
28846
29374
  async function runCompartmentAgent(deps) {
28847
29375
  if (shouldSkipHomeProjectMemory(deps.directory ?? process.cwd()))
28848
29376
  return;
@@ -28986,6 +29514,14 @@ async function runCompartmentAgent(deps) {
28986
29514
  snapshot: boundarySnapshot,
28987
29515
  currentContextLimit: deps.currentContextLimit ?? boundarySnapshot.contextLimit
28988
29516
  }) : { ok: true };
29517
+ if (!validation.ok && validation.reason === "stale_snapshot" && shouldRefuseStaleSnapshotForDrift(sessionId, validation.detail)) {
29518
+ dropDriftedServingOrdinalRanks(sessionId, validation.detail ?? validation.reason ?? "stale_snapshot");
29519
+ sessionLog(sessionId, `historian: refusing to publish over a stale protected-tail snapshot (was: ${validation.detail ?? "stale"}); ending the run without publishing`);
29520
+ telemetry.status = "noop";
29521
+ telemetry.failureReason = "stale_snapshot";
29522
+ rollbackDrainReservation();
29523
+ return;
29524
+ }
28989
29525
  if (!validation.ok && validation.reason === "stale_snapshot") {
28990
29526
  const refreshed = deps.refreshBoundarySnapshot ? deps.refreshBoundarySnapshot(boundarySnapshot, validation) : resolveOpenCodeProtectedTailBoundary({
28991
29527
  db,
@@ -29066,6 +29602,14 @@ async function runCompartmentAgent(deps) {
29066
29602
  sessionLog(sessionId, `historian prompt fit: sized to ${producerModel ?? "unknown"} (${describeHistorianPromptTrim(promptFit)}; requestedChunkTokens=${historianChunkTokens})`);
29067
29603
  }
29068
29604
  const perRunCap = selectPerRunCap(boundarySnapshot);
29605
+ if (!deps.forceDrainQuota && shouldWaitForNewHistorianHistory(boundarySnapshot, {
29606
+ emergency: getOverflowState(db, sessionId).needsEmergencyRecovery
29607
+ })) {
29608
+ telemetry.status = "noop";
29609
+ telemetry.failureReason = "waiting for new history after no forward progress";
29610
+ sessionLog(sessionId, "historian no-op: waiting for new history after no forward progress");
29611
+ return;
29612
+ }
29069
29613
  const usable = Math.max(1, Math.round(boundarySnapshot.contextLimit * boundarySnapshot.executeThresholdPercentage / 100));
29070
29614
  const reserve = deps.forceDrainQuota ? { ok: true, reservation: null } : reserveProtectedTailDrainTokens({
29071
29615
  db,
@@ -29170,6 +29714,12 @@ ${chunkText}`,
29170
29714
  const maxExistingSequence = priorCompartments.reduce((max, c) => c.sequence > max ? c.sequence : max, -1);
29171
29715
  const sequenceOffset = priorCompartments.length === 0 ? 0 : maxExistingSequence + 1;
29172
29716
  retainDrainReservationForRetryThrottle = true;
29717
+ if (!deps.forceDrainQuota) {
29718
+ shouldWaitForNewHistorianHistory(boundarySnapshot, {
29719
+ emergency: getOverflowState(db, sessionId).needsEmergencyRecovery,
29720
+ claim: true
29721
+ });
29722
+ }
29173
29723
  const validatedPass = await runValidatedHistorianPass({
29174
29724
  client,
29175
29725
  hiddenCompletionExecutor: deps.hiddenCompletionExecutor,
@@ -29221,7 +29771,11 @@ ${chunkText}`,
29221
29771
  sessionLog(sessionId, `historian failure: source=no-progress reason="historian returned compartments that did not advance past raw message ${offset - 1}" newCompartmentCount=${newCompartments.length} lastNewEnd=${lastNewEnd} priorEnd=${offset - 1}`);
29222
29772
  sessionLog(sessionId, "historian output discarded: reason=no_forward_progress");
29223
29773
  const failCount = incrementHistorianFailure(db, sessionId, `no forward progress beyond raw message ${offset - 1}`);
29224
- await notifyHistorianIssue(buildHistorianFailureNotice(failCount, `historian made no forward progress beyond raw message ${offset - 1}`));
29774
+ try {
29775
+ await notifyHistorianIssue(buildHistorianFailureNotice(failCount, `historian made no forward progress beyond raw message ${offset - 1}`));
29776
+ } finally {
29777
+ recordHistorianNoProgress(boundarySnapshot, inEmergency);
29778
+ }
29225
29779
  return;
29226
29780
  }
29227
29781
  retainDrainReservationForRetryThrottle = false;
@@ -29329,6 +29883,7 @@ ${chunkText}`,
29329
29883
  db.exec("COMMIT");
29330
29884
  published = true;
29331
29885
  publishCommitted = true;
29886
+ clearHistorianHistoryWait(sessionId);
29332
29887
  finishHistorianPublishStage(sessionId, "publish-txn", transactionStartedAt, "completed", `compartments=${persistedCompartments.length}`);
29333
29888
  logSlowWriteTransaction("historian-publish", lockAcquiredAt);
29334
29889
  } catch (error) {
@@ -30361,4 +30916,4 @@ async function executeContextRecomp(deps, options = {}) {
30361
30916
  return (await executeContextRecompWithResult(deps, options)).message;
30362
30917
  }
30363
30918
 
30364
- export { MEMORY_MAINTAINER_A_AGENT, MEMORY_MAINTAINER_B_AGENT, MEMORY_MAINTAINER_A_STEP_CAP, resolveMaintenanceStepCap, MEMORY_MAINTAINER_A_SYSTEM_PROMPT, MEMORY_MAINTAINER_B_SYSTEM_PROMPT, MEMORY_MAINTAINER_FINALIZE_INSTRUCTION, buildMemoryMaintainerStageAPrompt, buildMemoryMaintainerStageBPrompt, getOpenCodeGlobalConfigDir, getOpenCodeConfigDirs, opencodeUserConfigBasePath, mmcProjectConfigBasePath, buildPrimaryLanguageDirective, resolveModelConfigValue, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, PROTECTED_TOKENS_MIN, HISTORIAN_MODEL_RESOLUTION_FIELDS, LEGACY_HARNESS_BLOCK_KEYS, ConfigProfilesSchema, MagicContextConfigSchema, LIVE_RELOAD_CONFIG_PATHS, formatFailClosedBlockingMessage, formatFailClosedBlockingSummary, createFailClosedBlockingError, isFailClosedBlockingError, setHomeProjectPermission, shouldSkipHomeProjectMemory, ProjectIdentityError, takeDubiousOwnershipProjectIdentityWarning, isUserHomeDirectory, describeUnresolvedProjectIdentity, resolveProjectIdentityForSession, normalizeStoredProjectPath, storedPathBelongsToIdentity, parseProviderModel, modelBodyField, toModelEntry, HiddenCompletionRefusal, HIDDEN_HISTORIAN_AGENT, resolveHistorianStepCap, HIDDEN_RUN_DISPOSED_REASON, noteChildOfDisposedInstance, hiddenChildPermissions, registerHiddenChildAgents, HiddenAgentStepLimit, HiddenChildHook, isNonRetryable, V2_MEMORY_CATEGORIES, CATEGORY_PRIORITY, computeNormalizedHash, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, ModuleMemoryAuthorityError, insertMemory, insertMemoryIdempotent, getMemoryByHash, getMemoriesByProject, getMemoriesByProjects, getMemoryById, getMemoriesByIds, updateMemorySeenCount, updateMemoryRetrievalCount, supersededMemory, mergeMemoryStats, archiveMemory, relaxedFtsQuery, sanitizeFtsQuery, searchMemoriesFTS, searchMemoriesFTSUnion, clearMemoryVerifications, queueMemoryMutation, scheduleAfterBootQuiet, BoundedSessionMap, registerOwnRpcServerInstance, rpcPortDir, rpcPortFilePath, parseRpcPortFile, inspectProcessesAsync, dirtyLkgRecoveryRevision, ownsLkgDirtyFence, invalidateCleanLkgRecoveryFence, getLastIndexedOrdinal, getIndexedMessageIdAtOrdinal, getIndexedMessageCorpusSize, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, clearIndexedMessages, indexMessagesAfterOrdinal, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLeaseBestEffort, isCompartmentLeaseHeld, queueM0Mutation, getMaxM0MutationId, ensureSessionMetaRow, clearCachedM0M1, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getAuthorityManagedMarker, getTemporalDecisions, observeTemporalDecisions, freezeTemporalDecisions, MAX_EXECUTE_THRESHOLD, escalationBands, MERGED_REASONING_PARTS_PREFIX, decodeMergedReasoningParts, readFrozenMergedReasoningParts, HYGIENE_PROVIDER_UNITS_VERSION, sessionDecisionCalibration, transitionSessionHygieneUnits, readEpochFloorSnapshot, getProtectionWindowForSession, ReplayDocumentError, readReplayEnvelope, updateReplayDocument, isEmergencyRecoveryArmed, loadPersistedUsage, loadProtectedTailMeta, resetProtectedTailNoEligibleHead, getWrapupInProgressState, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, getProtectedTailDrainBudgetSkip, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getAutoSearchHintDecisions, appendAutoSearchHintDecision, flushAutoSearchHintDecisionRetirement, getPersistedTodoPermissionDenied, setPersistedTodoPermissionDenied, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getHistorianFailureState, clearHistorianFailureState, getOverflowState, loadTransformPassStateSnapshot, recordOverflowDetected, recordDetectedContextLimit, clearEmergencyRecovery, clearDetectedContextLimit, getPersistedCompactionMarkerState, setPersistedCompactionMarkerState, getDeferredClearedCompactionMarkerState, retireDeferredClearedCompactionMarkerState, applyStrippedPlaceholderDelta, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, thinkingBindingRecoveryFrozenId, getThinkingBindingRecoveryTarget, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getTrailingBlankDecisions, addTrailingBlankDecisions, demoteTrailingBlankKeepDecisions, addStaleReduceStrippedIds, getProcessedImageMarkers, addProcessedImageStrippedIds, getPendingCompactionMarkerState, getCompactionMarkerHealth, loadPostprocessReplaySnapshot, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, logPiCompactionMarkerClear, setSessionWorkMetrics, getObservedEpochFloor, resolveEpochFloorForPass, providerResponseFailed, isSuccessfulProviderCompletion, rememberUnpublishedAutoSearchNoHint, getUnpublishedAutoSearchNoHint, forgetUnpublishedAutoSearchNoHint, registerAutoSearchWork, gaDatabasePath, RAW_MESSAGE_TYPES, V2StoreReaderPool, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, markSessionCleanupPending, clearSession, requestPendingMaterialization, pendingMaterializationOrigin, hasPendingMaterializationOrigin, clearPendingMaterialization, recordSystemPromptForRequest, recordHeldHeadForRequest, bindSystemPromptResponse, isHeldHeadForRequest, deferDeliveredSystemRefresh, retirePendingSystemPromptDelivery, lkgProviderInputTotal, beginV2LkgRequest, currentLkgRequestIdentity, noteLkgProviderResponse, clearLkgMeasuredRequest, saveLkgSlotToDb, loadPersistedLkgSlot, pruneStaleLkgSlots, resolveLkgModelKeys, createLkgEntryProjector, captureLkgSlot, replayLkg, noteUncertifiedNormalLkg, lkgDirtyRevision, readLkgRecoveryPrefix, noteLkgHealthyPrefix, beginLkgPass, getInMemorySlot, getSlot, dropSlot, getLkgSlotHeapStats, TOOL_SWEEP_SCOPED_MARKER, useScopedToolSweep, createPreAdoptionToolSweepResolver, copySessionStateForClone, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getPersistedSchemaVersion, openCurrentDatabase, openDatabaseAsync, isDatabasePersisted, getDatabasePersistenceError, getNoteByIdInScope, getNotes, addNote, getSessionNotes, updateNote, dismissNotes, dismissNote, getActivePrimers, saveSourceContent, replaceSourceContent, getSourceContents, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, resolveStoredPathWorkspaceIdentity, storedPathBelongsToWorkspace, sourceNameForMemory, rebaseSessionCoordinatesAsync, renderUserFacingFailure, userFacingFailureCode, renderCapabilityRefusal, ACTIVE_TOOL_IDS, systemPromptFingerprint, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, getOpenCodeDbPath, compareOpenCodeMessagesByCanonicalOrder, removeMcOwnedCompactionMarkers, MARKER_SUMMARY_TEXT, applyDeferredCompactionMarker, HISTORIAN_AGENT, HISTORIAN_RECOMP_AGENT, HISTORIAN_EDITOR_AGENT, runTokenLog, extractM0Block, TEMPORAL_MARKER_REPLAY_PATTERN, collectTemporalCandidates, injectTemporalMarkers, clearInjectionCache, getVisibleMemoryIds, selectHiddenMessagesAtCompactionSeam, prepareCompartmentInjection, renderCompartmentInjection, MaterializeContentionError, mustMaterialize, trimMemoriesToBudgetV2, renderMemoryBlockV2, materializeM0, resetPrefixTrimFallbackState, findHostCompactionWindow, capturePrefixTrimSourceOrder, prepareCachedM0M1Replay, hasCompleteCachedM0M1, injectM0M1, describeBoundaryDiagnostics, RECOVERY_NO_HEAD_LIMIT, deriveProtectedTailTokenTarget, selectPerRunCap, resolveOpenCodeProtectedTailBoundary, resolveWrapupProtectedTailBoundary, getRawHistoryEligibility, hasRunnableCompartmentWindow, recordHighPressureNoEligibleHead, runCompartmentAgent, getActiveCompartmentRun, markActiveCompartmentRunPublished, registerActiveCompartmentRun, startCompartmentAgent, executeContextRecomp };
30919
+ export { MEMORY_MAINTAINER_A_AGENT, MEMORY_MAINTAINER_B_AGENT, MEMORY_MAINTAINER_A_STEP_CAP, resolveMaintenanceStepCap, MEMORY_MAINTAINER_A_SYSTEM_PROMPT, MEMORY_MAINTAINER_B_SYSTEM_PROMPT, MEMORY_MAINTAINER_FINALIZE_INSTRUCTION, buildMemoryMaintainerStageAPrompt, buildMemoryMaintainerStageBPrompt, getOpenCodeGlobalConfigDir, getOpenCodeConfigDirs, opencodeUserConfigBasePath, mmcProjectConfigBasePath, buildPrimaryLanguageDirective, resolveModelConfigValue, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, PROTECTED_TOKENS_MIN, HISTORIAN_MODEL_RESOLUTION_FIELDS, LEGACY_HARNESS_BLOCK_KEYS, ConfigProfilesSchema, MagicContextConfigSchema, LIVE_RELOAD_CONFIG_PATHS, formatFailClosedBlockingMessage, formatFailClosedBlockingSummary, createFailClosedBlockingError, isFailClosedBlockingError, setHomeProjectPermission, shouldSkipHomeProjectMemory, ProjectIdentityError, takeDubiousOwnershipProjectIdentityWarning, isUserHomeDirectory, describeUnresolvedProjectIdentity, resolveProjectIdentityForSession, normalizeStoredProjectPath, storedPathBelongsToIdentity, parseProviderModel, modelBodyField, toModelEntry, HiddenCompletionRefusal, HIDDEN_HISTORIAN_AGENT, resolveHistorianStepCap, HIDDEN_RUN_DISPOSED_REASON, noteChildOfDisposedInstance, hiddenChildPermissions, registerHiddenChildAgents, HiddenAgentStepLimit, HiddenChildHook, isNonRetryable, V2_MEMORY_CATEGORIES, CATEGORY_PRIORITY, computeNormalizedHash, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, ModuleMemoryAuthorityError, insertMemory, insertMemoryIdempotent, getMemoryByHash, getMemoriesByProject, getMemoriesByProjects, getMemoryById, getMemoriesByIds, updateMemorySeenCount, updateMemoryRetrievalCount, supersededMemory, mergeMemoryStats, archiveMemory, relaxedFtsQuery, sanitizeFtsQuery, searchMemoriesFTS, searchMemoriesFTSUnion, clearMemoryVerifications, queueMemoryMutation, scheduleAfterBootQuiet, cleanSearchText, keywordRelevance, matchedSearchSnippet, getLastIndexedOrdinal, getIndexedMessageIdAtOrdinal, getIndexedMessageCorpusSize, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, clearIndexedMessages, indexMessagesAfterOrdinal, registerOwnRpcServerInstance, rpcPortDir, rpcPortFilePath, parseRpcPortFile, inspectProcessesAsync, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLeaseBestEffort, isCompartmentLeaseHeld, queueM0Mutation, getMaxM0MutationId, ensureSessionMetaRow, clearCachedM0M1, clearCachedM0, getTableColumnNames, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getAuthorityManagedMarker, getTemporalDecisions, observeTemporalDecisions, freezeTemporalDecisions, MAX_EXECUTE_THRESHOLD, escalationBands, MERGED_REASONING_PARTS_PREFIX, decodeMergedReasoningParts, readFrozenMergedReasoningParts, HYGIENE_PROVIDER_UNITS_VERSION, sessionDecisionCalibration, transitionSessionHygieneUnits, readEpochFloorSnapshot, getProtectionWindowForSession, ReplayDocumentError, readReplayEnvelope, updateReplayDocument, isEmergencyRecoveryArmed, loadPersistedUsage, loadProtectedTailMeta, resetProtectedTailNoEligibleHead, getWrapupInProgressState, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, getProtectedTailDrainBudgetSkip, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getAutoSearchHintDecisions, appendAutoSearchHintDecision, flushAutoSearchHintDecisionRetirement, getPersistedTodoPermissionDenied, setPersistedTodoPermissionDenied, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getHistorianFailureState, clearHistorianFailureState, getOverflowState, loadTransformPassStateSnapshot, recordOverflowDetected, recordDetectedContextLimit, clearEmergencyRecovery, clearDetectedContextLimit, getPersistedCompactionMarkerState, clearCompactionMarkerPublicationIntent, setPersistedCompactionMarkerState, getDeferredClearedCompactionMarkerState, retireDeferredClearedCompactionMarkerState, applyStrippedPlaceholderDelta, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, thinkingBindingRecoveryFrozenId, getThinkingBindingRecoveryTarget, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getTrailingBlankDecisions, addTrailingBlankDecisions, demoteTrailingBlankKeepDecisions, addStaleReduceStrippedIds, getProcessedImageMarkers, addProcessedImageStrippedIds, getPendingCompactionMarkerState, getCompactionMarkerHealth, loadPostprocessReplaySnapshot, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, logPiCompactionMarkerClear, setSessionWorkMetrics, getObservedEpochFloor, resolveEpochFloorForPass, providerResponseFailed, isSuccessfulProviderCompletion, dirtyLkgRecoveryRevision, ownsLkgDirtyFence, invalidateCleanLkgRecoveryFence, rememberUnpublishedAutoSearchNoHint, getUnpublishedAutoSearchNoHint, forgetUnpublishedAutoSearchNoHint, registerAutoSearchWork, gaDatabasePath, RAW_MESSAGE_TYPES, V2StoreReaderPool, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, markSessionCleanupPending, clearSession, requestPendingMaterialization, pendingMaterializationOrigin, hasPendingMaterializationOrigin, clearPendingMaterialization, recordSystemPromptForRequest, recordHeldHeadForRequest, bindSystemPromptResponse, isHeldHeadForRequest, deferDeliveredSystemRefresh, retirePendingSystemPromptDelivery, lkgProviderInputTotal, beginV2LkgRequest, currentLkgRequestIdentity, noteLkgProviderResponse, clearLkgMeasuredRequest, saveLkgSlotToDb, loadPersistedLkgSlot, pruneStaleLkgSlots, resolveLkgModelKeys, createLkgEntryProjector, captureLkgSlot, replayLkg, noteUncertifiedNormalLkg, lkgDirtyRevision, readLkgRecoveryPrefix, noteLkgHealthyPrefix, lkgInputIdentity, beginLkgPass, getInMemorySlot, getSlot, dropSlot, getLkgSlotHeapStats, TOOL_SWEEP_SCOPED_MARKER, useScopedToolSweep, createPreAdoptionToolSweepResolver, copySessionStateForClone, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getPersistedSchemaVersion, openCurrentDatabase, openDatabaseAsync, isDatabasePersisted, getDatabasePersistenceError, getNoteByIdInScope, getNotes, addNote, getSessionNotes, updateNote, dismissNotes, dismissNote, getActivePrimers, saveSourceContent, replaceSourceContent, getSourceContents, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, resolveStoredPathWorkspaceIdentity, storedPathBelongsToWorkspace, sourceNameForMemory, rebaseSessionCoordinatesAsync, renderUserFacingFailure, userFacingFailureCode, renderCapabilityRefusal, ACTIVE_TOOL_IDS, systemPromptFingerprint, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, getOpenCodeDbPath, compareOpenCodeMessagesByCanonicalOrder, removeMcOwnedCompactionMarkers, MARKER_SUMMARY_TEXT, applyDeferredCompactionMarker, HISTORIAN_AGENT, HISTORIAN_RECOMP_AGENT, HISTORIAN_EDITOR_AGENT, runTokenLog, shouldWaitForNewHistorianHistory, extractM0Block, TEMPORAL_MARKER_REPLAY_PATTERN, collectTemporalCandidates, injectTemporalMarkers, clearInjectionCache, getVisibleMemoryIds, selectHiddenMessagesAtCompactionSeam, prepareCompartmentInjection, renderCompartmentInjection, MaterializeContentionError, mustMaterialize, trimMemoriesToBudgetV2, renderMemoryBlockV2, materializeM0, resetPrefixTrimFallbackState, findHostCompactionWindow, capturePrefixTrimSourceOrder, prepareCachedM0M1Replay, hasCompleteCachedM0M1, injectM0M1, omitIncompleteFileTokenTotals, describeBoundaryDiagnostics, RECOVERY_NO_HEAD_LIMIT, deriveProtectedTailTokenTarget, selectPerRunCap, resolveOpenCodeProtectedTailBoundary, resolveWrapupProtectedTailBoundary, getRawHistoryEligibility, hasRunnableCompartmentWindow, recordHighPressureNoEligibleHead, runCompartmentAgent, getActiveCompartmentRun, markActiveCompartmentRunPublished, registerActiveCompartmentRun, startCompartmentAgent, executeContextRecomp };