@cortexkit/pi-magic-context 0.49.34 → 0.49.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/auto-search-worker.js +163 -155
  2. package/dist/chunk-embedding-write-worker.js +71 -66
  3. package/dist/{compaction-marker-candidates-p1kqkqhq.js → compaction-marker-candidates-9fzqg6y0.js} +6 -3
  4. package/dist/compaction-marker-candidates-worker.js +24 -23
  5. package/dist/{compartment-chunk-embedding-8c85ebce.js → compartment-chunk-embedding-jpcme6tz.js} +11 -9
  6. package/dist/embedding-backfill-002vyexq.js +27 -0
  7. package/dist/embedding-worker-process.js +2 -1
  8. package/dist/embedding-worker.js +5 -4
  9. package/dist/expand-attachments-8sxeyj26.js +21 -0
  10. package/dist/historian-calibration-extension.js +2 -1
  11. package/dist/{index-hqw0rt9k.js → index-0wtm7g7f.js} +1 -1
  12. package/dist/{index-wqpjhx1p.js → index-2qqsnh1b.js} +2 -2
  13. package/dist/index-3bb24eb6.js +229 -0
  14. package/dist/{index-48twbev2.js → index-6pk49fc3.js} +37 -51
  15. package/dist/{index-399p688g.js → index-8sqebjzk.js} +4 -1
  16. package/dist/{index-np9ryr8y.js → index-a7sj2bam.js} +24 -241
  17. package/dist/index-by1bzjcd.js +24 -0
  18. package/dist/{index-6981g1xv.js → index-c1dqxr10.js} +35 -5
  19. package/dist/index-c6wbq66z.js +168 -0
  20. package/dist/{index-bvpar0bg.js → index-ck8wj35v.js} +2 -2
  21. package/dist/{index-jn44h81p.js → index-cq71g4dk.js} +1 -1
  22. package/dist/{index-pb3ea2tq.js → index-d107f484.js} +5 -5
  23. package/dist/{index-f4fz95fy.js → index-dgqjq8sg.js} +398 -4996
  24. package/dist/index-dnp1jjar.js +222 -0
  25. package/dist/index-ea37qn4x.js +777 -0
  26. package/dist/{index-g2tfayfc.js → index-f6y7mdq9.js} +98 -552
  27. package/dist/{index-0gymvr5j.js → index-f9qet7y8.js} +19 -8
  28. package/dist/index-gxqmh9hq.js +194 -0
  29. package/dist/{index-fgs78jyd.js → index-gymgz20n.js} +5 -222
  30. package/dist/index-hgd00nas.js +113 -0
  31. package/dist/{index-9xd7qveg.js → index-jbkwszd5.js} +4 -4
  32. package/dist/{index-e61zxfts.js → index-kj49vs41.js} +4 -4
  33. package/dist/{index-4zemngt7.js → index-mp0gh04g.js} +2 -2
  34. package/dist/{index-eenxm3t3.js → index-mx757f3g.js} +51 -32
  35. package/dist/{index-23t8ryqw.js → index-n0zjvs3h.js} +1 -168
  36. package/dist/index-nypgv96r.js +58 -0
  37. package/dist/index-p9jfa33t.js +4171 -0
  38. package/dist/{index-eqw9t2ty.js → index-pwp6zyx3.js} +15 -4
  39. package/dist/{index-cj3bkd16.js → index-ry84c8dr.js} +2 -2
  40. package/dist/{index-gwxaz0z5.js → index-rzfr7wdv.js} +2 -2
  41. package/dist/index-w26zvs1q.js +528 -0
  42. package/dist/index-wfqysrwq.js +204 -0
  43. package/dist/index-wrr9rp9a.js +16 -0
  44. package/dist/{index-my0dn413.js → index-wt7vmyj4.js} +1 -1
  45. package/dist/{index-0mc0vv97.js → index-wwsfevck.js} +8 -5
  46. package/dist/{index-54wmg4rj.js → index-x2n261wp.js} +16 -14
  47. package/dist/{index-6wdwv3a6.js → index-y90fxz4t.js} +10 -4
  48. package/dist/{index-c474g0gj.js → index-yacs388s.js} +1 -1
  49. package/dist/{index-xn5yh0pn.js → index-z7dktknj.js} +34 -131
  50. package/dist/index.js +1316 -488
  51. package/dist/{logger-shetp43v.js → logger-2j70hhz4.js} +2 -1
  52. package/dist/{message-fts-rowid-map-3mbvdgn9.js → message-fts-rowid-map-hmgyfxxa.js} +8 -6
  53. package/dist/{message-index-bounded-a6na71rg.js → message-index-bounded-82sergz7.js} +15 -12
  54. package/dist/{message-time-backfill-e3r8ddh1.js → message-time-backfill-x45573rs.js} +3 -2
  55. package/dist/migration-worker.js +56 -48
  56. package/dist/{project-embedding-registry-acqb6be4.js → project-embedding-registry-16pj4vyf.js} +16 -13
  57. package/dist/raw-ordinal-warmup-worker.js +114 -113
  58. package/dist/{read-session-chunk-2xcp6f40.js → read-session-chunk-jxq427v6.js} +17 -10
  59. package/dist/{read-session-raw-g9b1zdn5.js → read-session-raw-a9ntjqap.js} +6 -4
  60. package/dist/render-2wan3bfh.js +42 -0
  61. package/dist/{safe-notification-target-zcpchea6.js → safe-notification-target-jk69jxwp.js} +3 -2
  62. package/dist/search-3p18trsb.js +50 -0
  63. package/dist/search-archive-pgrv04gv.js +25 -0
  64. package/dist/{storage-tags-6409qq18.js → storage-tags-p9y3bnw7.js} +5 -4
  65. package/dist/store-reader-2m80zs5z.js +14 -0
  66. package/dist/store-v60q7dje.js +452 -0
  67. package/dist/subagent-entry.js +34 -25
  68. package/dist/tag-snapshot-worker-pi.js +218 -209
  69. package/dist/v2-archive-reader-5zpj8hrg.js +11 -0
  70. package/package.json +1 -1
  71. package/dist/embedding-backfill-rwfmyvze.js +0 -24
  72. package/dist/search-archive-jate39w5.js +0 -21
  73. package/dist/search-b6hp64hc.js +0 -45
package/dist/index.js CHANGED
@@ -9,15 +9,17 @@ import {
9
9
  getMagicContextStorageDir,
10
10
  isMagicContextStorageOutsideEngram,
11
11
  claimStorageBackupWarningCheck,
12
- sanitizeDiagnosticText,
13
- hasShareabilitySensitiveText,
14
12
  registerStalePluginBuildHost,
15
13
  classifyStalePluginBuild,
16
14
  importPluginModule,
17
15
  log2,
18
16
  sessionLog2,
19
17
  flushLogger2
20
- } from "./index-fgs78jyd.js";
18
+ } from "./index-gymgz20n.js";
19
+ import {
20
+ sanitizeDiagnosticText,
21
+ hasShareabilitySensitiveText
22
+ } from "./index-dnp1jjar.js";
21
23
  import {
22
24
  COMPACTION_ENABLED_PATH,
23
25
  isDreamerRunnable,
@@ -47,121 +49,6 @@ import {
47
49
  cortexKitProjectConfigBasePath,
48
50
  migrateMagicContextConfigLocations,
49
51
  getProtectedTokensTierOverrides,
50
- LkgRecoveryFenceError,
51
- clearLkgFenceProcessState,
52
- lkgFenceRequiresRebuild,
53
- lkgRecoveryFenceUnclean,
54
- ownsLkgDirtyFence,
55
- invalidateCleanLkgRecoveryFence,
56
- markLkgRecoveryDirty,
57
- rememberUnpublishedAutoSearchNoHint,
58
- getUnpublishedAutoSearchNoHint,
59
- forgetUnpublishedAutoSearchNoHint,
60
- registerAutoSearchWork,
61
- cancelAutoSearchWork,
62
- readSessionActivity,
63
- observeSessionActivity,
64
- backfillSessionActivity,
65
- readSessionMeta,
66
- getOrCreateSessionMeta,
67
- updateSessionMeta,
68
- advanceToolReclaimWatermark,
69
- retryPendingSessionCleanups,
70
- retryPendingRustSessionCleanupsForProject,
71
- COMPARTMENT_LEASE_RENEWAL_MS,
72
- acquireCompartmentLease,
73
- renewCompartmentLease,
74
- releaseCompartmentLease,
75
- releaseCompartmentLeaseBestEffort,
76
- isCompartmentLeaseHeld,
77
- isNoContentCompartment,
78
- HAS_COMPARTMENT_CONTENT_SQL,
79
- queueM0Mutation,
80
- getMaxM0MutationId,
81
- getCompartments,
82
- getLastCompartmentEndMessage,
83
- getLastCompartmentEndMessageId,
84
- getCompartmentsByEndMessageId,
85
- appendCompartments,
86
- saveRecompStagingPass,
87
- getRecompStaging,
88
- clearRecompStaging,
89
- getRecompPartialRange,
90
- setRecompPartialRange,
91
- escapeXmlAttr,
92
- escapeXmlContent,
93
- getUncoveredCompartmentEndThrough,
94
- isPartialCompartmentEnd,
95
- readProjectDocsCanonical,
96
- providerResponseFailed,
97
- isSuccessfulProviderCompletion,
98
- requestPendingMaterialization,
99
- pendingMaterializationOrigin,
100
- hasPendingMaterializationOrigin,
101
- clearPendingMaterialization,
102
- recordSystemRefreshOwnership,
103
- recordSystemPromptForRequest,
104
- recordHeldHeadForRequest,
105
- bindSystemPromptResponse,
106
- acknowledgeSystemPromptDelivery,
107
- deferDeliveredSystemRefresh,
108
- clearSystemPromptDelivery,
109
- saveLkgSlotToDb,
110
- loadPersistedLkgSlot,
111
- drainStaleLkgSlots,
112
- createDbLkgPersistence,
113
- replayLkg,
114
- registerLkgPersistence,
115
- LKG_SNAPSHOT_ARRAY,
116
- LKG_SNAPSHOT_OBJECT,
117
- LKG_SNAPSHOT_KEY,
118
- LKG_SNAPSHOT_STRING,
119
- LKG_SNAPSHOT_NUMBER,
120
- LKG_SNAPSHOT_BOOLEAN,
121
- LKG_SNAPSHOT_NULL,
122
- LKG_SNAPSHOT_UNDEFINED,
123
- contentSnapshotValue,
124
- signatureForFields,
125
- lkgContentFields,
126
- lkgContentDigestFromFields,
127
- isLegacyLkgDigest,
128
- legacyLkgDigestFromFields,
129
- lkgFieldsMatchDigest,
130
- exactReusablePrefix,
131
- incrementalLkgContentDigests,
132
- lkgSlotRejection,
133
- captureSlot,
134
- getInMemorySlot,
135
- getSlot,
136
- dropSlot,
137
- TOOL_SWEEP_SCOPED_MARKER,
138
- encodePiContentDecision,
139
- decodePiContentDecision,
140
- getPiContentDecisions,
141
- freezePiContentDecision,
142
- withNativeReplayWriteBarrier,
143
- getNativeReplayState,
144
- saveNativeToolInputs,
145
- addNativeReasoningIds,
146
- copySessionStateForClone,
147
- externalHistorianFailureReason,
148
- subprocessExitHistorianFailureReason,
149
- countHistorianRuns,
150
- recordHistorianRun,
151
- summarizeImportance,
152
- tallyFactsByCategory,
153
- queueMemoryMutation,
154
- getMemoryMutationsForRender,
155
- getMemoryMutationsForRenderByProjects,
156
- getMaxMemoryMutationId,
157
- getMaxMemoryMutationIdForProjects,
158
- GLOBAL_USER_PROFILE_PROJECT_PATH,
159
- getProjectState,
160
- bumpProjectUserProfileVersion,
161
- saveSourceContent,
162
- getSourceContents,
163
- recordSubagentInvocation,
164
- getLatestHistorianInvocationId,
165
52
  noteTouchedAt,
166
53
  noteNudgePickIndex,
167
54
  formatNoteNudge,
@@ -263,7 +150,7 @@ import {
263
150
  registerTodoStateLifecycle,
264
151
  syncCtxMemoryToolEnabled,
265
152
  registerMagicContextTools
266
- } from "./index-f4fz95fy.js";
153
+ } from "./index-dgqjq8sg.js";
267
154
  import"./index-4knyh1kt.js";
268
155
  import {
269
156
  canonicalModelIdentity,
@@ -288,26 +175,9 @@ import {
288
175
  createFailClosedBlockingError,
289
176
  isFailClosedBlockingError,
290
177
  shouldBypassFailClosedBlock,
291
- createFailClosedController,
292
- piHarnessKindFromExecutable,
293
- getErrorMessage,
294
- describeError,
295
- startBootDeadline,
296
- getSchemaFenceRejection,
297
- getMigrationOnOpenRefusal,
298
- LATEST_SUPPORTED_VERSION,
299
- getDatabasePath,
300
- getPersistedSchemaVersion,
301
- setSqlitePragmaConfig,
302
- applySqliteTuningPragmas,
303
- runSqliteOptimize,
304
- openDatabase,
305
- openCurrentDatabase,
306
- openDatabaseAsync,
307
- closeDatabase
308
- } from "./index-np9ryr8y.js";
178
+ createFailClosedController
179
+ } from "./index-3bb24eb6.js";
309
180
  import {
310
- sameProjectDirectory,
311
181
  pruneMissingProjectIdentities,
312
182
  setHomeProjectPermission,
313
183
  ProjectIdentityError,
@@ -318,7 +188,7 @@ import {
318
188
  resolveProjectIdentityForSession,
319
189
  beginBootQuietPeriod,
320
190
  scheduleAfterBootQuiet
321
- } from "./index-48twbev2.js";
191
+ } from "./index-6pk49fc3.js";
322
192
  import {
323
193
  detectThinkingBindingMismatch,
324
194
  detectLatestTurnThinkingMismatch,
@@ -424,7 +294,7 @@ import {
424
294
  getSessionWorkMetrics,
425
295
  resolveEpochFloorForPass,
426
296
  askAnswerLines,
427
- retreatPastHostUnservedRows,
297
+ removeUnsupportedCommitHashes,
428
298
  completedToolArcCrossesBoundary,
429
299
  estimateTrueRawMessageTokens,
430
300
  buildToolArcs,
@@ -483,7 +353,7 @@ import {
483
353
  getRawSessionTagKeysThrough2,
484
354
  getLegacyProtectedTailStartOrdinal2,
485
355
  readSessionChunk2
486
- } from "./index-eenxm3t3.js";
356
+ } from "./index-mx757f3g.js";
487
357
  import {
488
358
  UNKNOWN_FIT_RATIO,
489
359
  hasMeasuredDecisionCalibration,
@@ -570,7 +440,7 @@ import {
570
440
  getToolTagNumberByOwner2,
571
441
  getNullOwnerToolTag2,
572
442
  adoptNullOwnerToolTag2
573
- } from "./index-eqw9t2ty.js";
443
+ } from "./index-pwp6zyx3.js";
574
444
  import {
575
445
  textMentionsRecentCommit,
576
446
  hasMeaningfulUserText2,
@@ -582,7 +452,7 @@ import {
582
452
  estimateTokens,
583
453
  estimateFixedPromptTokens,
584
454
  normalizeText
585
- } from "./index-my0dn413.js";
455
+ } from "./index-wt7vmyj4.js";
586
456
  import {
587
457
  inRootAsyncContext2,
588
458
  Database2,
@@ -602,27 +472,165 @@ import {
602
472
  observeTemporalDecisions,
603
473
  freezeTemporalDecisions,
604
474
  tagIdentityRevisionKey
605
- } from "./index-c474g0gj.js";
475
+ } from "./index-yacs388s.js";
476
+ import {
477
+ piHarnessKindFromExecutable,
478
+ getErrorMessage,
479
+ describeError,
480
+ startBootDeadline,
481
+ getSchemaFenceRejection,
482
+ getMigrationOnOpenRefusal,
483
+ LATEST_SUPPORTED_VERSION,
484
+ getDatabasePath,
485
+ getPersistedSchemaVersion,
486
+ setSqlitePragmaConfig,
487
+ applySqliteTuningPragmas,
488
+ runSqliteOptimize,
489
+ openDatabase,
490
+ openCurrentDatabase,
491
+ openDatabaseAsync,
492
+ closeDatabase
493
+ } from "./index-a7sj2bam.js";
494
+ import {
495
+ LkgRecoveryFenceError,
496
+ clearLkgFenceProcessState,
497
+ lkgFenceRequiresRebuild,
498
+ lkgRecoveryFenceUnclean,
499
+ ownsLkgDirtyFence,
500
+ invalidateCleanLkgRecoveryFence,
501
+ markLkgRecoveryDirty,
502
+ rememberUnpublishedAutoSearchNoHint,
503
+ getUnpublishedAutoSearchNoHint,
504
+ forgetUnpublishedAutoSearchNoHint,
505
+ registerAutoSearchWork,
506
+ cancelAutoSearchWork,
507
+ readSessionActivity,
508
+ observeSessionActivity,
509
+ backfillSessionActivity,
510
+ readSessionMeta,
511
+ getOrCreateSessionMeta,
512
+ updateSessionMeta,
513
+ advanceToolReclaimWatermark,
514
+ retryPendingSessionCleanups,
515
+ retryPendingRustSessionCleanupsForProject,
516
+ COMPARTMENT_LEASE_RENEWAL_MS,
517
+ acquireCompartmentLease,
518
+ renewCompartmentLease,
519
+ releaseCompartmentLease,
520
+ releaseCompartmentLeaseBestEffort,
521
+ isCompartmentLeaseHeld,
522
+ isNoContentCompartment,
523
+ HAS_COMPARTMENT_CONTENT_SQL,
524
+ queueM0Mutation,
525
+ getMaxM0MutationId,
526
+ getCompartments,
527
+ getLastCompartmentEndMessage,
528
+ getLastCompartmentEndMessageId,
529
+ getCompartmentsByEndMessageId,
530
+ appendCompartments,
531
+ saveRecompStagingPass,
532
+ getRecompStaging,
533
+ clearRecompStaging,
534
+ getRecompPartialRange,
535
+ setRecompPartialRange,
536
+ escapeXmlAttr,
537
+ escapeXmlContent,
538
+ getUncoveredCompartmentEndThrough,
539
+ isPartialCompartmentEnd,
540
+ readProjectDocsCanonical,
541
+ providerResponseFailed,
542
+ isSuccessfulProviderCompletion,
543
+ requestPendingMaterialization,
544
+ pendingMaterializationOrigin,
545
+ hasPendingMaterializationOrigin,
546
+ clearPendingMaterialization,
547
+ recordSystemRefreshOwnership,
548
+ recordSystemPromptForRequest,
549
+ recordHeldHeadForRequest,
550
+ bindSystemPromptResponse,
551
+ acknowledgeSystemPromptDelivery,
552
+ deferDeliveredSystemRefresh,
553
+ clearSystemPromptDelivery,
554
+ saveLkgSlotToDb,
555
+ loadPersistedLkgSlot,
556
+ drainStaleLkgSlots,
557
+ createDbLkgPersistence,
558
+ replayLkg,
559
+ registerLkgPersistence,
560
+ LKG_SNAPSHOT_ARRAY,
561
+ LKG_SNAPSHOT_OBJECT,
562
+ LKG_SNAPSHOT_KEY,
563
+ LKG_SNAPSHOT_STRING,
564
+ LKG_SNAPSHOT_NUMBER,
565
+ LKG_SNAPSHOT_BOOLEAN,
566
+ LKG_SNAPSHOT_NULL,
567
+ LKG_SNAPSHOT_UNDEFINED,
568
+ contentSnapshotValue,
569
+ signatureForFields,
570
+ lkgContentFields,
571
+ lkgContentDigestFromFields,
572
+ isLegacyLkgDigest,
573
+ legacyLkgDigestFromFields,
574
+ lkgFieldsMatchDigest,
575
+ exactReusablePrefix,
576
+ incrementalLkgContentDigests,
577
+ lkgSlotRejection,
578
+ captureSlot,
579
+ getInMemorySlot,
580
+ getSlot,
581
+ dropSlot,
582
+ TOOL_SWEEP_SCOPED_MARKER,
583
+ encodePiContentDecision,
584
+ decodePiContentDecision,
585
+ getPiContentDecisions,
586
+ freezePiContentDecision,
587
+ withNativeReplayWriteBarrier,
588
+ getNativeReplayState,
589
+ saveNativeToolInputs,
590
+ addNativeReasoningIds,
591
+ copySessionStateForClone,
592
+ externalHistorianFailureReason,
593
+ subprocessExitHistorianFailureReason,
594
+ countHistorianRuns,
595
+ recordHistorianRun,
596
+ summarizeImportance,
597
+ tallyFactsByCategory,
598
+ queueMemoryMutation,
599
+ getMemoryMutationsForRender,
600
+ getMemoryMutationsForRenderByProjects,
601
+ getMaxMemoryMutationId,
602
+ getMaxMemoryMutationIdForProjects,
603
+ GLOBAL_USER_PROFILE_PROJECT_PATH,
604
+ getProjectState,
605
+ bumpProjectUserProfileVersion,
606
+ saveSourceContent,
607
+ getSourceContents,
608
+ recordSubagentInvocation,
609
+ getLatestHistorianInvocationId
610
+ } from "./index-p9jfa33t.js";
606
611
  import {
607
- BoundedSessionMap,
608
612
  resolveOpenCodeDbPath,
609
613
  assertOpenCodeStoreGeneration,
610
614
  openCodeDbPathExists,
611
615
  recordOpenCodeDbReadFailure,
612
616
  clearOpenCodeDbReadFailure,
613
617
  claimOpenCodeDbDiagnosticOnce
614
- } from "./index-23t8ryqw.js";
615
- import"./index-399p688g.js";
618
+ } from "./index-n0zjvs3h.js";
619
+ import"./index-8sqebjzk.js";
620
+ import {
621
+ reuseWorker
622
+ } from "./index-wfqysrwq.js";
616
623
  import {
617
624
  createWorkerShutdown
618
- } from "./index-54wmg4rj.js";
625
+ } from "./index-x2n261wp.js";
619
626
  import {
620
627
  RAW_SUMMARY_TEXT_MAX_CHARS2
621
- } from "./index-6981g1xv.js";
628
+ } from "./index-c1dqxr10.js";
629
+ import"./index-ea37qn4x.js";
622
630
  import {
623
631
  formatSynapseLaneDescriptor
624
- } from "./index-gwxaz0z5.js";
625
- import"./index-bvpar0bg.js";
632
+ } from "./index-rzfr7wdv.js";
633
+ import"./index-ck8wj35v.js";
626
634
  import {
627
635
  buildCanonicalChunkTextFromFts2,
628
636
  buildCompartmentSummaryFallbackText2,
@@ -632,7 +640,7 @@ import {
632
640
  replaceCompartmentChunkEmbeddings2,
633
641
  deleteChunkEmbedBackoffForSession2,
634
642
  chunkWindowSourceKey2
635
- } from "./index-0gymvr5j.js";
643
+ } from "./index-f9qet7y8.js";
636
644
  import {
637
645
  recordSessionProjectIdentity,
638
646
  GIT_SWEEP_LEASE_RENEWAL_MS,
@@ -641,7 +649,7 @@ import {
641
649
  markGitSweepSuccessAndRelease,
642
650
  parkGitSweepNonIndexable,
643
651
  releaseGitSweepLease
644
- } from "./index-4zemngt7.js";
652
+ } from "./index-mp0gh04g.js";
645
653
  import {
646
654
  embedPauseBySession,
647
655
  embedRunStateBySession,
@@ -650,33 +658,20 @@ import {
650
658
  autoEmbedIdentityBySession,
651
659
  autoEmbedRetryAfterBySession,
652
660
  invalidateAutoEmbedSession,
653
- getEmbedDrainUiStatus,
654
- describeShadowBackfillWriteRefusal2,
655
- contentSha2562,
656
- drainProjectEmbeddingIdentityMaintenance2,
657
- drainStaleEmbeddingIdentitiesForProject2,
658
- enqueueShadowEmbeddingItems2,
659
- getProjectEmbeddingSnapshot2,
660
- getProjectChunkEmbeddingModelId2,
661
- getProjectEmbeddingMaxInputTokens2,
662
- embedTextForProject2,
663
- embedBatchForProject2,
664
- embedItemsForProject2,
665
- embedUnembeddedMemoriesForProject2,
666
- drainCommitBacklogForProject2,
667
- embedSessionCompartmentChunks2,
668
- getEmbeddingCoverageStatus2,
669
- getAutoEmbeddingSessionCoverage2
670
- } from "./index-xn5yh0pn.js";
661
+ getEmbedDrainUiStatus
662
+ } from "./index-hgd00nas.js";
671
663
  import {
672
664
  clearCompressionDepth,
673
665
  clearCompressionDepthRange,
674
666
  cleanSearchText,
675
667
  matchedSearchSnippet
676
- } from "./index-e61zxfts.js";
668
+ } from "./index-kj49vs41.js";
677
669
  import {
678
670
  rememberV1HiddenRun
679
671
  } from "./index-6x556c2q.js";
672
+ import {
673
+ BoundedSessionMap
674
+ } from "./index-c6wbq66z.js";
680
675
  import {
681
676
  V2_MEMORY_CATEGORIES
682
677
  } from "./index-135e8hvg.js";
@@ -688,7 +683,7 @@ import {
688
683
  expandWorkspaceIdentitySetWithAliases,
689
684
  sourceNameForMemory,
690
685
  computeWorkspaceEpochFingerprint
691
- } from "./index-pb3ea2tq.js";
686
+ } from "./index-d107f484.js";
692
687
  import {
693
688
  configureContextDatabasePragmas2
694
689
  } from "./index-fapenpch.js";
@@ -712,39 +707,8 @@ import {
712
707
  getActivePrimers,
713
708
  createPrimer,
714
709
  updatePrimerSupport,
715
- updatePrimerAnswer,
716
- USER_FACING_FAILURES,
717
- renderUserFacingFailure,
718
- userFacingFailureCode,
719
- renderCapabilityRefusal,
720
- renderDreamFailure,
721
- dreamFailureCode,
722
- renderEmbeddingFailure,
723
- renderSingleStoreMigrationRequiredRefusal,
724
- SingleStoreMigrationRequiredError,
725
- projectNeedsSingleStoreMigration,
726
- promoteSessionFactsDurable,
727
- embedPromotedFacts,
728
- recordMemoryMapping,
729
- recordMemoryVerifications,
730
- getUnmappedMemoryIds,
731
- clearMemoryVerifications,
732
- getMemoryVerifications,
733
- resolveGitTopLevel,
734
- readGitHead,
735
- readGitChangedFilesSince,
736
- readGitFileChangeTimesSince,
737
- verificationFileExists,
738
- normalizeVerificationFiles,
739
- indexCommitsForProject,
740
- embedUnembeddedCommits,
741
- getMessageIndexEpoch,
742
- isMessageIndexSearchable,
743
- scheduleReconciliation,
744
- scheduleIncrementalIndex,
745
- scheduleClearAndReindex,
746
- clearSessionTracking
747
- } from "./index-g2tfayfc.js";
710
+ updatePrimerAnswer
711
+ } from "./index-w26zvs1q.js";
748
712
  import {
749
713
  setEmbeddingSessionBusy2,
750
714
  isEmbeddingHostBusy2
@@ -765,6 +729,7 @@ import {
765
729
  getMemoriesByProjects2,
766
730
  getMaxMemoryIdForProjects2,
767
731
  getMemoryById2,
732
+ updateMemoryVerification2,
768
733
  setMemoryClassification2,
769
734
  archiveMemory2,
770
735
  getMemoryCountsByStatus2
@@ -777,11 +742,63 @@ import {
777
742
  } from "./index-q1yc6bs0.js";
778
743
  import {
779
744
  getCachedLocalEmbeddingPooling
780
- } from "./index-wqpjhx1p.js";
745
+ } from "./index-2qqsnh1b.js";
746
+ import {
747
+ describeShadowBackfillWriteRefusal2,
748
+ contentSha2562,
749
+ drainProjectEmbeddingIdentityMaintenance2,
750
+ drainStaleEmbeddingIdentitiesForProject2,
751
+ enqueueShadowEmbeddingItems2,
752
+ getProjectEmbeddingSnapshot2,
753
+ getProjectChunkEmbeddingModelId2,
754
+ getProjectEmbeddingMaxInputTokens2,
755
+ embedTextForProject2,
756
+ embedBatchForProject2,
757
+ embedItemsForProject2,
758
+ embedUnembeddedMemoriesForProject2,
759
+ drainCommitBacklogForProject2,
760
+ embedSessionCompartmentChunks2,
761
+ getEmbeddingCoverageStatus2,
762
+ getAutoEmbeddingSessionCoverage2
763
+ } from "./index-z7dktknj.js";
781
764
  import {
782
765
  cosineSimilarity
783
- } from "./index-9xd7qveg.js";
784
- import"./index-6wdwv3a6.js";
766
+ } from "./index-jbkwszd5.js";
767
+ import {
768
+ USER_FACING_FAILURES,
769
+ renderUserFacingFailure,
770
+ userFacingFailureCode,
771
+ renderCapabilityRefusal,
772
+ renderDreamFailure,
773
+ dreamFailureCode,
774
+ renderEmbeddingFailure,
775
+ renderSingleStoreMigrationRequiredRefusal,
776
+ SingleStoreMigrationRequiredError,
777
+ projectNeedsSingleStoreMigration,
778
+ promoteSessionFactsDurable,
779
+ embedPromotedFacts,
780
+ recordMemoryMapping,
781
+ recordMemoryVerifications,
782
+ getUnmappedMemoryIds,
783
+ clearMemoryVerifications,
784
+ getMemoryVerifications,
785
+ resolveGitTopLevel,
786
+ readGitHead,
787
+ readGitChangedFilesSince,
788
+ readGitFileChangeTimesSince,
789
+ verificationFileExists,
790
+ normalizeVerificationFiles,
791
+ indexCommitsForProject,
792
+ embedUnembeddedCommits,
793
+ getMessageIndexEpoch,
794
+ isMessageIndexSearchable,
795
+ scheduleReconciliation,
796
+ scheduleIncrementalIndex,
797
+ scheduleClearAndReindex,
798
+ clearSessionTracking,
799
+ createAutoSearchDiagnostics
800
+ } from "./index-f6y7mdq9.js";
801
+ import"./index-y90fxz4t.js";
785
802
  import {
786
803
  DREAMER_PRIMER_INVESTIGATOR_ALLOWED_TOOLS,
787
804
  buildHiddenAgentRegistrations,
@@ -808,19 +825,26 @@ import {
808
825
  drainPiTagSnapshotSession,
809
826
  assertPiTagSnapshotReady,
810
827
  ensurePiTagSnapshotReady
811
- } from "./index-0mc0vv97.js";
812
- import"./index-jn44h81p.js";
828
+ } from "./index-wwsfevck.js";
829
+ import"./index-cq71g4dk.js";
813
830
  import"./index-1cvyaw9a.js";
814
- import"./index-cj3bkd16.js";
831
+ import"./index-gxqmh9hq.js";
832
+ import"./index-ry84c8dr.js";
815
833
  import {
816
834
  __toESM
817
835
  } from "./index-22jkk6wg.js";
836
+ import {
837
+ retreatPastHostUnservedRows
838
+ } from "./index-by1bzjcd.js";
818
839
  import {
819
840
  computeNormalizedHash
820
841
  } from "./index-9x1f96r8.js";
821
842
  import {
822
843
  normalizeProtectedToolName
823
844
  } from "./index-674f6qs2.js";
845
+ import {
846
+ sameProjectDirectory
847
+ } from "./index-wrr9rp9a.js";
824
848
  import {
825
849
  closeQuietly,
826
850
  isKnownAutocommit
@@ -828,7 +852,7 @@ import {
828
852
  import {
829
853
  logSlowWriteTransaction,
830
854
  WriteTransactionSteps
831
- } from "./index-hqw0rt9k.js";
855
+ } from "./index-0wtm7g7f.js";
832
856
  import {
833
857
  setStoragePrivatePermissionEnforcement,
834
858
  ensureStorageDirectorySync,
@@ -3560,7 +3584,7 @@ function startHostStallProfilerSwitch() {
3560
3584
  // package.json
3561
3585
  var package_default = {
3562
3586
  name: "@cortexkit/pi-magic-context",
3563
- version: "0.49.34",
3587
+ version: "0.49.36",
3564
3588
  type: "module",
3565
3589
  description: "Pi and OMP coding agent extension for Magic Context — cross-session memory and context management",
3566
3590
  main: "dist/index.js",
@@ -3679,6 +3703,158 @@ var package_default = {
3679
3703
 
3680
3704
  // src/clone-inheritance.ts
3681
3705
  import { open, stat } from "node:fs/promises";
3706
+
3707
+ // src/drop-branches-pi.ts
3708
+ var KEY = "piDropBranches";
3709
+ var installed = new WeakSet;
3710
+ function trackPiDropLeaf(db, sessionId, leaf) {
3711
+ if (!installed.has(db)) {
3712
+ db.exec(`
3713
+ CREATE TEMP TABLE IF NOT EXISTS pi_drop_leaf (session_id TEXT PRIMARY KEY, leaf TEXT);
3714
+ CREATE TEMP TRIGGER IF NOT EXISTS pi_drop_queued AFTER INSERT ON main.pending_ops
3715
+ WHEN NEW.operation = 'drop' AND EXISTS (SELECT 1 FROM pi_drop_leaf WHERE session_id = NEW.session_id AND leaf IS NOT NULL)
3716
+ BEGIN
3717
+ UPDATE session_meta SET deferred_execute_state = json_set(
3718
+ COALESCE(NULLIF(deferred_execute_state, ''), '{}'),
3719
+ '$.${KEY}.pending."' || NEW.tag_id || '"',
3720
+ (SELECT leaf FROM pi_drop_leaf WHERE session_id = NEW.session_id))
3721
+ WHERE session_id = NEW.session_id;
3722
+ END;
3723
+ CREATE TEMP TRIGGER IF NOT EXISTS pi_drop_applied AFTER UPDATE OF status ON main.tags
3724
+ WHEN NEW.status = 'dropped' AND OLD.status != 'dropped'
3725
+ AND EXISTS (SELECT 1 FROM pi_drop_leaf WHERE session_id = NEW.session_id AND leaf IS NOT NULL)
3726
+ BEGIN
3727
+ UPDATE session_meta SET deferred_execute_state = json_remove(json_set(
3728
+ COALESCE(NULLIF(deferred_execute_state, ''), '{}'),
3729
+ '$.${KEY}.drops."' || NEW.tag_number || '"',
3730
+ COALESCE(json_extract(deferred_execute_state, '$.${KEY}.pending."' || NEW.tag_number || '"'),
3731
+ (SELECT leaf FROM pi_drop_leaf WHERE session_id = NEW.session_id))),
3732
+ '$.${KEY}.pending."' || NEW.tag_number || '"')
3733
+ WHERE session_id = NEW.session_id;
3734
+ END;
3735
+ CREATE TEMP TRIGGER IF NOT EXISTS pi_drop_retired AFTER UPDATE OF status ON main.tags
3736
+ WHEN NEW.status = 'compacted'
3737
+ BEGIN
3738
+ UPDATE session_meta SET deferred_execute_state = json_remove(
3739
+ COALESCE(NULLIF(deferred_execute_state, ''), '{}'),
3740
+ '$.${KEY}.drops."' || NEW.tag_number || '"', '$.${KEY}.pending."' || NEW.tag_number || '"')
3741
+ WHERE session_id = NEW.session_id AND json_type(deferred_execute_state, '$.${KEY}') = 'object';
3742
+ END;
3743
+ `);
3744
+ installed.add(db);
3745
+ }
3746
+ ensureSessionMetaRow(db, sessionId);
3747
+ db.prepare("INSERT INTO pi_drop_leaf VALUES (?, ?) ON CONFLICT(session_id) DO UPDATE SET leaf = excluded.leaf").run(sessionId, leaf);
3748
+ }
3749
+ function state(db, sessionId) {
3750
+ const row = db.prepare("SELECT deferred_execute_state AS state FROM session_meta WHERE session_id = ?").get(sessionId);
3751
+ const value = row?.state ? JSON.parse(row.state) : {};
3752
+ if (!value || typeof value !== "object" || Array.isArray(value))
3753
+ throw new Error("Invalid session metadata JSON");
3754
+ return value;
3755
+ }
3756
+ function journal(value) {
3757
+ const result = { drops: {}, pending: {} };
3758
+ if (!value || typeof value !== "object")
3759
+ return result;
3760
+ for (const kind of ["drops", "pending"]) {
3761
+ const entries = value[kind];
3762
+ if (!entries || typeof entries !== "object")
3763
+ continue;
3764
+ for (const [tag, leaf] of Object.entries(entries)) {
3765
+ if (/^\d+$/.test(tag) && typeof leaf === "string" && leaf)
3766
+ result[kind][tag] = leaf;
3767
+ }
3768
+ }
3769
+ return result;
3770
+ }
3771
+ function save(db, sessionId, document, value) {
3772
+ document[KEY] = value;
3773
+ db.prepare("UPDATE session_meta SET deferred_execute_state = ? WHERE session_id = ?").run(JSON.stringify(document), sessionId);
3774
+ }
3775
+ function piDropBranchIds(entries) {
3776
+ const ids = new Set;
3777
+ let parent = null;
3778
+ for (const entry of entries) {
3779
+ if (!entry || typeof entry !== "object")
3780
+ return null;
3781
+ const row = entry;
3782
+ if (typeof row.id !== "string" || !row.id || row.parentId !== parent || ids.has(row.id))
3783
+ return null;
3784
+ ids.add(row.id);
3785
+ parent = row.id;
3786
+ }
3787
+ return ids;
3788
+ }
3789
+ function cancelPiAbandonedPendingDrops(db, sessionId, ids) {
3790
+ if (!ids)
3791
+ return 0;
3792
+ const abandoned = (saved) => Object.entries(saved.pending).filter(([, leaf]) => !ids.has(leaf));
3793
+ if (!abandoned(journal(state(db, sessionId)[KEY])).length)
3794
+ return 0;
3795
+ return db.transaction(() => {
3796
+ const document = state(db, sessionId);
3797
+ const saved = journal(document[KEY]);
3798
+ const offPending = abandoned(saved);
3799
+ if (!offPending.length)
3800
+ return 0;
3801
+ for (const [tag] of offPending) {
3802
+ db.prepare("DELETE FROM pending_ops WHERE session_id = ? AND tag_id = ?").run(sessionId, Number(tag));
3803
+ delete saved.pending[tag];
3804
+ }
3805
+ save(db, sessionId, document, saved);
3806
+ return offPending.length;
3807
+ }).immediate();
3808
+ }
3809
+ function reconcilePiDropBranches(db, sessionId, ids, canRebuild) {
3810
+ if (!canRebuild || !ids)
3811
+ return 0;
3812
+ return db.transaction(() => {
3813
+ const document = state(db, sessionId);
3814
+ const saved = journal(document[KEY]);
3815
+ const offBranch = Object.entries(saved.drops).filter(([, leaf]) => !ids.has(leaf));
3816
+ if (!offBranch.length)
3817
+ return 0;
3818
+ const native = getNativeReplayState(db, sessionId);
3819
+ let restored = 0;
3820
+ for (const [tag] of offBranch) {
3821
+ const row = db.prepare("UPDATE tags SET status = 'active', drop_mode = 'full' WHERE session_id = ? AND tag_number = ? AND status = 'dropped' RETURNING message_id, type").get(sessionId, Number(tag));
3822
+ if (row) {
3823
+ restored++;
3824
+ if (row.type === "tool")
3825
+ native.toolInputs.delete(row.message_id);
3826
+ }
3827
+ delete saved.drops[tag];
3828
+ }
3829
+ if (restored && !updateReplayDocument(db, sessionId, (doc) => {
3830
+ if (doc.piNative === undefined)
3831
+ return false;
3832
+ if (!doc.piNative || typeof doc.piNative !== "object" || Array.isArray(doc.piNative))
3833
+ throw new Error("Invalid Pi native replay state");
3834
+ doc.piNative = {
3835
+ ...doc.piNative,
3836
+ toolInputs: Object.fromEntries(native.toolInputs)
3837
+ };
3838
+ return true;
3839
+ }))
3840
+ throw new Error("Cannot persist restored Pi tool inputs");
3841
+ save(db, sessionId, document, saved);
3842
+ return restored;
3843
+ }).immediate();
3844
+ }
3845
+ function copyPiDropBranches(db, source, destination) {
3846
+ const sourceJournal = journal(state(db, source)[KEY]);
3847
+ const tags = new Set(db.prepare("SELECT tag_number FROM tags WHERE session_id = ?").all(destination).map((row) => String(row.tag_number)));
3848
+ for (const kind of ["drops", "pending"]) {
3849
+ for (const tag of Object.keys(sourceJournal[kind]))
3850
+ if (!tags.has(tag))
3851
+ delete sourceJournal[kind][tag];
3852
+ }
3853
+ ensureSessionMetaRow(db, destination);
3854
+ save(db, destination, state(db, destination), sourceJournal);
3855
+ }
3856
+
3857
+ // src/clone-inheritance.ts
3682
3858
  var CONTENT_ID_SUFFIX = /:(?:p|file)\d+$/;
3683
3859
  var BARE_SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3684
3860
  var loggedCloneStartFailures = new Set;
@@ -3863,9 +4039,14 @@ async function inheritPiCloneSession(event, ctx, deps, isStartup) {
3863
4039
  throw new Error("Pi clone branch is unavailable");
3864
4040
  }
3865
4041
  stage = "copy-state";
4042
+ const filter = createCloneFilter(branchEntries);
3866
4043
  const result = copySessionStateForClone(deps.db, sourceSessionId, destinationSessionId, {
3867
- ...createCloneFilter(branchEntries),
3868
- requireEmptyDestinationMeta: isStartup
4044
+ ...filter,
4045
+ requireEmptyDestinationMeta: isStartup,
4046
+ selectPendingPiMarker: (raw, compartments) => {
4047
+ copyPiDropBranches(deps.db, sourceSessionId, destinationSessionId);
4048
+ return filter.selectPendingPiMarker(raw, compartments);
4049
+ }
3869
4050
  });
3870
4051
  if (result.kind === "destination-not-empty") {
3871
4052
  writeLog(`[magic-context][pi] clone-inheritance: skipped source=${sourceSessionId} dest=${destinationSessionId} stage=destination-guard reason=destination-not-empty`);
@@ -4169,12 +4350,17 @@ function countCompartmentsSince(db, projectPath, since) {
4169
4350
  return row?.cnt ?? 0;
4170
4351
  }
4171
4352
  function countProjectSessionsSince(db, projectPath, since) {
4172
- const row = since === null ? db.prepare("SELECT COUNT(*) AS cnt FROM session_projects WHERE project_path = ?").get(projectPath) : db.prepare(`SELECT COUNT(*) AS cnt FROM session_projects sp
4173
- LEFT JOIN schema_migrations_meta activity
4174
- ON activity.key = 'retrospective_activity:' || sp.session_id
4175
- LEFT JOIN message_history_index history ON history.session_id = sp.session_id
4176
- WHERE sp.project_path = ?
4177
- AND COALESCE(CAST(activity.value AS INTEGER), history.updated_at) > ?`).get(projectPath, since);
4353
+ const scope = `FROM session_projects sp
4354
+ LEFT JOIN session_meta m ON m.session_id = sp.session_id
4355
+ WHERE sp.project_path = ? AND sp.harness = ?
4356
+ AND COALESCE(m.is_subagent, 0) = 0`;
4357
+ const row = since === null ? db.prepare(`SELECT COUNT(*) AS cnt ${scope}`).get(projectPath, getHarness2()) : db.prepare(`SELECT COUNT(*) AS cnt ${scope}
4358
+ AND COALESCE(
4359
+ (SELECT CAST(value AS INTEGER) FROM schema_migrations_meta
4360
+ WHERE key = 'retrospective_activity:' || sp.session_id),
4361
+ (SELECT updated_at FROM message_history_index
4362
+ WHERE session_id = sp.session_id AND harness = sp.harness)
4363
+ ) > ?`).get(projectPath, getHarness2(), since);
4178
4364
  return row?.cnt ?? 0;
4179
4365
  }
4180
4366
  function countMappedMemories(db, projectPath) {
@@ -4187,6 +4373,10 @@ function countMappedMemories(db, projectPath) {
4187
4373
  AND v.file_path <> ''`).get(projectPath, Date.now());
4188
4374
  return row?.cnt ?? 0;
4189
4375
  }
4376
+ var CURRENT_VERIFY_SKIP_SQL = `COALESCE(CASE WHEN json_valid(m.metadata_json) THEN
4377
+ json_extract(m.metadata_json, '$.dreamerVerifySkip.contentHash') = m.normalized_hash
4378
+ AND json_type(m.metadata_json, '$.dreamerVerifySkip.fingerprint') = 'text'
4379
+ ELSE 0 END, 0)`;
4190
4380
  function countUnverifiedMappedMemories(db, projectPath) {
4191
4381
  const row = db.prepare(`SELECT COUNT(DISTINCT m.id) AS cnt
4192
4382
  FROM memories m
@@ -4195,7 +4385,8 @@ function countUnverifiedMappedMemories(db, projectPath) {
4195
4385
  AND m.status IN ('active','permanent')
4196
4386
  AND (m.expires_at IS NULL OR m.expires_at > ?)
4197
4387
  AND v.file_path <> ''
4198
- AND v.verified_at = 0`).get(projectPath, Date.now());
4388
+ AND v.verified_at = 0
4389
+ AND NOT (${CURRENT_VERIFY_SKIP_SQL})`).get(projectPath, Date.now());
4199
4390
  return row?.cnt ?? 0;
4200
4391
  }
4201
4392
  function countBroadCycleCandidates(db, projectPath, cycleStartAt) {
@@ -4204,6 +4395,7 @@ function countBroadCycleCandidates(db, projectPath, cycleStartAt) {
4204
4395
  WHERE m.project_path = ?
4205
4396
  AND m.status IN ('active','permanent')
4206
4397
  AND (m.expires_at IS NULL OR m.expires_at > ?)
4398
+ AND NOT (${CURRENT_VERIFY_SKIP_SQL})
4207
4399
  AND (
4208
4400
  SELECT MAX(v.verified_at)
4209
4401
  FROM memory_verifications v
@@ -4312,7 +4504,7 @@ function getDreamTaskBacklog(db, projectPath, task, options = {}) {
4312
4504
  case "verify-broad": {
4313
4505
  const total = countMappedMemories(db, projectPath);
4314
4506
  const cycleStartAt = getTaskScheduleState(db, projectPath, "verify-broad")?.lastBroadRunAt;
4315
- const pending = cycleStartAt == null ? total : countBroadCycleCandidates(db, projectPath, cycleStartAt);
4507
+ const pending = countBroadCycleCandidates(db, projectPath, cycleStartAt ?? Number.MAX_SAFE_INTEGER);
4316
4508
  return { pending, total };
4317
4509
  }
4318
4510
  case "curate": {
@@ -4439,6 +4631,7 @@ function getDreamTaskBacklogs(db, projectPath, tasks = CANONICAL_DREAM_TASKS, op
4439
4631
  }
4440
4632
  const optionKey = (value) => value === undefined ? "omitted" : value === null ? "null" : `number:${value}`;
4441
4633
  const key = JSON.stringify([
4634
+ getHarness2(),
4442
4635
  projectPath,
4443
4636
  tasks,
4444
4637
  optionKey(options.lastRunAt),
@@ -4528,7 +4721,7 @@ function openOpenCodeDb() {
4528
4721
  }
4529
4722
 
4530
4723
  // ../plugin/src/features/magic-context/dreamer/task-executor.ts
4531
- import { createHash as createHash10 } from "node:crypto";
4724
+ import { createHash as createHash11 } from "node:crypto";
4532
4725
  import { existsSync as existsSync9, readFileSync as readFileSync8 } from "node:fs";
4533
4726
 
4534
4727
  // ../plugin/src/agents/dreamer.ts
@@ -4551,7 +4744,7 @@ var DREAMER_REVIEWER_AGENT = "dreamer-reviewer";
4551
4744
  // ../plugin/src/features/magic-context/schema-fence-probe.ts
4552
4745
  var STALE_CHILD_SPAWN_FAILURE = "stale_schema_fence";
4553
4746
  var STALE_CHILD_SPAWN_LATCH_THRESHOLD = 2;
4554
- var state = {
4747
+ var state2 = {
4555
4748
  consecutiveFailures: 0,
4556
4749
  totalFailures: 0,
4557
4750
  latched: false,
@@ -4559,23 +4752,23 @@ var state = {
4559
4752
  failure: null
4560
4753
  };
4561
4754
  function recordStaleFence(persistedVersion, supportedVersion, reason = "newer_schema") {
4562
- state.consecutiveFailures += 1;
4563
- state.totalFailures += 1;
4564
- const latched = state.consecutiveFailures >= STALE_CHILD_SPAWN_LATCH_THRESHOLD;
4565
- state.latched ||= latched;
4755
+ state2.consecutiveFailures += 1;
4756
+ state2.totalFailures += 1;
4757
+ const latched = state2.consecutiveFailures >= STALE_CHILD_SPAWN_LATCH_THRESHOLD;
4758
+ state2.latched ||= latched;
4566
4759
  const failure = {
4567
4760
  failureClass: STALE_CHILD_SPAWN_FAILURE,
4568
4761
  reason,
4569
4762
  persistedVersion,
4570
4763
  supportedVersion,
4571
- consecutiveFailures: state.consecutiveFailures,
4572
- totalFailures: state.totalFailures,
4573
- latched: state.latched
4764
+ consecutiveFailures: state2.consecutiveFailures,
4765
+ totalFailures: state2.totalFailures,
4766
+ latched: state2.latched
4574
4767
  };
4575
- state.failure = failure;
4576
- const shouldSurface = state.latched && !state.noticeEmitted;
4768
+ state2.failure = failure;
4769
+ const shouldSurface = state2.latched && !state2.noticeEmitted;
4577
4770
  if (shouldSurface)
4578
- state.noticeEmitted = true;
4771
+ state2.noticeEmitted = true;
4579
4772
  return { allowSpawn: false, failure, shouldSurface };
4580
4773
  }
4581
4774
  function probeChildSpawnFence(db) {
@@ -4594,9 +4787,9 @@ function probeChildSpawnFence(db) {
4594
4787
  } catch {
4595
4788
  return recordStaleFence(LATEST_SUPPORTED_VERSION, LATEST_SUPPORTED_VERSION, "read_error");
4596
4789
  }
4597
- state.consecutiveFailures = 0;
4598
- state.latched = false;
4599
- state.noticeEmitted = false;
4790
+ state2.consecutiveFailures = 0;
4791
+ state2.latched = false;
4792
+ state2.noticeEmitted = false;
4600
4793
  return { allowSpawn: true };
4601
4794
  }
4602
4795
 
@@ -5791,6 +5984,18 @@ function messageId(message) {
5791
5984
  const id = infoOf(message).id;
5792
5985
  return typeof id === "string" ? id : null;
5793
5986
  }
5987
+ function recordedAssistantError(message) {
5988
+ const info = infoOf(message);
5989
+ if (info.error != null)
5990
+ return info.error;
5991
+ if (isSettledAssistant(message))
5992
+ return;
5993
+ const parts = message?.parts;
5994
+ if (!Array.isArray(parts))
5995
+ return;
5996
+ const retry = parts.slice().reverse().find((part) => part !== null && typeof part === "object" && part.type === "retry");
5997
+ return retry?.error ?? undefined;
5998
+ }
5794
5999
  function isSettledAssistant(message) {
5795
6000
  const info = infoOf(message);
5796
6001
  if (info.role !== "assistant")
@@ -5906,6 +6111,22 @@ async function promptAsyncAndWaitForIdle(client, request, options = {}) {
5906
6111
  let completionBaseline = new Set((await readMessages(session, sessionId, dir, signal)).map(messageId).filter((id) => id !== null));
5907
6112
  const initialBaseline = new Set(completionBaseline);
5908
6113
  activeSessionErrors.set(sessionId, undefined);
6114
+ const failForHostError = async (hostError) => {
6115
+ await abortChildRun(client, sessionId);
6116
+ const error = new Error(`Host recorded session error: ${describeAssistantError(hostError)}`);
6117
+ Object.assign(error, {
6118
+ name: "DreamerProviderOutputFailureError",
6119
+ transient: true,
6120
+ [HOST_SESSION_ERROR]: true,
6121
+ hostError
6122
+ });
6123
+ annotateSubagentInvocationError(error, {
6124
+ attemptedModel: requestModelRef(request),
6125
+ modelConfigKey: options.modelConfigKey,
6126
+ hostError
6127
+ });
6128
+ throw error;
6129
+ };
5909
6130
  try {
5910
6131
  let sent;
5911
6132
  try {
@@ -5940,22 +6161,17 @@ async function promptAsyncAndWaitForIdle(client, request, options = {}) {
5940
6161
  await sleep(pollIntervalMs, signal);
5941
6162
  const hostError = activeSessionErrors.get(sessionId);
5942
6163
  if (hostError !== undefined) {
5943
- const error = new Error(`Host recorded session error: ${describeAssistantError(hostError)}`);
5944
- Object.assign(error, {
5945
- name: "DreamerProviderOutputFailureError",
5946
- transient: true,
5947
- [HOST_SESSION_ERROR]: true,
5948
- hostError
5949
- });
5950
- annotateSubagentInvocationError(error, {
5951
- attemptedModel: requestModelRef(request),
5952
- modelConfigKey: options.modelConfigKey,
5953
- hostError
5954
- });
5955
- throw error;
6164
+ await failForHostError(hostError);
5956
6165
  }
5957
6166
  const status = await readStatus(session, sessionId, dir, signal);
5958
6167
  const messages = await readMessages(session, sessionId, dir, signal);
6168
+ const latestAssistant = messages.slice().reverse().find((message) => {
6169
+ const id = messageId(message);
6170
+ return id !== null && !completionBaseline.has(id) && infoOf(message).role === "assistant";
6171
+ });
6172
+ const assistantError = recordedAssistantError(latestAssistant);
6173
+ if (assistantError !== undefined)
6174
+ await failForHostError(assistantError);
5959
6175
  if (!sawOutput && messages.some((message) => {
5960
6176
  const id = messageId(message);
5961
6177
  return id !== null && !initialBaseline.has(id) && showsModelOutput(message);
@@ -7235,6 +7451,15 @@ function validateHistorianOutput(text, sessionId, chunk, _priorCompartments, seq
7235
7451
  error: "Historian terminal boundary splits a completed tool invocation/result arc"
7236
7452
  });
7237
7453
  }
7454
+ for (const compartment of mapped.compartments) {
7455
+ const supported = new Set(chunk.lines.filter((line) => line.ordinal >= compartment.startMessage && line.ordinal <= compartment.endMessage).flatMap((line) => line.commitHashes ?? []));
7456
+ for (const field of ["title", "content", "p1", "p2", "p3", "p4"]) {
7457
+ const value = compartment[field];
7458
+ if (typeof value === "string") {
7459
+ compartment[field] = removeUnsupportedCommitHashes(value, supported);
7460
+ }
7461
+ }
7462
+ }
7238
7463
  return finish({
7239
7464
  ok: true,
7240
7465
  compartments: mapped.compartments,
@@ -9551,6 +9776,7 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
9551
9776
  async attempt(handle, request) {
9552
9777
  if (!client)
9553
9778
  throw new Error("Hidden completion client is unavailable");
9779
+ hostErrors.delete(handle.id);
9554
9780
  const identity = runIdentities.get(handle.id);
9555
9781
  const requestedModel = request.body?.model;
9556
9782
  const requestedRef = requestedModel && typeof requestedModel.providerID === "string" && typeof requestedModel.modelID === "string" ? `${requestedModel.providerID}/${requestedModel.modelID}` : undefined;
@@ -9593,9 +9819,9 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
9593
9819
  });
9594
9820
  const assistantFailure = extractLatestAssistantFailure(messages);
9595
9821
  const hostError = hostErrors.get(handle.id);
9596
- if (assistantFailure) {
9597
- const finish = assistantFailure.finish ? ` (finish=${assistantFailure.finish})` : "";
9598
- const error = new Error(`Historian host recorded an assistant error${finish}: ${describeAssistantFailure(assistantFailure.error)}`, hostError === undefined ? undefined : { cause: hostError });
9822
+ if (assistantFailure || hostError !== undefined) {
9823
+ const finish = assistantFailure?.finish ? ` (finish=${assistantFailure.finish})` : "";
9824
+ const error = new Error(`Historian host recorded an assistant error${finish}: ${describeAssistantFailure(assistantFailure?.error ?? hostError)}`, hostError === undefined ? undefined : { cause: hostError });
9599
9825
  annotateSubagentInvocationError(error, {
9600
9826
  attemptedModel: handle.attemptedModel,
9601
9827
  modelConfigKey: handle.modelConfigKey,
@@ -16574,7 +16800,7 @@ function insertDreamRun(db, run) {
16574
16800
  }
16575
16801
 
16576
16802
  // ../plugin/src/features/magic-context/dreamer/verify.ts
16577
- import { createHash as createHash9 } from "node:crypto";
16803
+ import { createHash as createHash10 } from "node:crypto";
16578
16804
 
16579
16805
  // ../plugin/src/features/magic-context/dreamer/verify-diff.ts
16580
16806
  import { execFile as execFile3 } from "node:child_process";
@@ -16695,7 +16921,56 @@ Read only these changed line ranges if they may affect the claim.`
16695
16921
  }
16696
16922
 
16697
16923
  // ../plugin/src/features/magic-context/dreamer/verify-gate.ts
16924
+ import path12 from "node:path";
16925
+
16926
+ // ../plugin/src/features/magic-context/dreamer/verify-state.ts
16927
+ import { createHash as createHash9 } from "node:crypto";
16928
+ import { readFile, realpath as realpath2 } from "node:fs/promises";
16698
16929
  import path11 from "node:path";
16930
+ function verificationMetadata(memory) {
16931
+ try {
16932
+ const parsed = JSON.parse(memory.metadataJson ?? "{}");
16933
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
16934
+ return parsed;
16935
+ } catch {}
16936
+ return {};
16937
+ }
16938
+ async function verificationFilesFingerprint(directory, files) {
16939
+ const root = await resolveGitTopLevel(directory) ?? path11.resolve(directory);
16940
+ const hash = createHash9("sha256");
16941
+ for (const file of [...new Set(files)].sort()) {
16942
+ const absolute = path11.resolve(root, file);
16943
+ const withinRoot = (candidate) => {
16944
+ const relative = path11.relative(root, candidate);
16945
+ return relative !== ".." && !relative.startsWith(`..${path11.sep}`) && !path11.isAbsolute(relative);
16946
+ };
16947
+ if (!withinRoot(absolute))
16948
+ return null;
16949
+ hash.update(JSON.stringify(file));
16950
+ try {
16951
+ if (!withinRoot(await realpath2(absolute)))
16952
+ return null;
16953
+ hash.update(createHash9("sha256").update(await readFile(absolute)).digest("hex"));
16954
+ } catch (error) {
16955
+ if (error.code !== "ENOENT")
16956
+ return null;
16957
+ hash.update("missing");
16958
+ }
16959
+ }
16960
+ return hash.digest("hex");
16961
+ }
16962
+ function recordVerificationVerdict(db, memory, verdict, at, fingerprint = null) {
16963
+ const metadata = verificationMetadata(memory);
16964
+ delete metadata.dreamerVerifySkip;
16965
+ if (verdict === "skip" && fingerprint) {
16966
+ metadata.dreamerVerifySkip = { contentHash: memory.normalizedHash, fingerprint, at };
16967
+ }
16968
+ metadata.dreamerVerifyVerdict = verdict;
16969
+ metadata.dreamerVerifyAttemptedAt = at;
16970
+ db.prepare("UPDATE memories SET metadata_json = ? WHERE id = ?").run(JSON.stringify(metadata), memory.id);
16971
+ }
16972
+
16973
+ // ../plugin/src/features/magic-context/dreamer/verify-gate.ts
16699
16974
  function minOf(values) {
16700
16975
  return values.reduce((acc, v) => v < acc ? v : acc, Number.POSITIVE_INFINITY);
16701
16976
  }
@@ -16727,7 +17002,22 @@ async function partitionVerifyScope(args) {
16727
17002
  const runStartedAt = args.now ?? Date.now();
16728
17003
  const active = getMemoriesByProject2(args.db, args.projectIdentity);
16729
17004
  const verById = getMemoryVerifications(args.db, active.map((m) => m.id));
16730
- const candidates = active.filter((m) => (verById.get(m.id)?.files.length ?? 0) > 0);
17005
+ const candidates = [];
17006
+ const unchangedSkippedIds = [];
17007
+ const changedSkippedIds = new Set;
17008
+ for (const memory of active) {
17009
+ const files = verById.get(memory.id)?.files ?? [];
17010
+ if (!files.length)
17011
+ continue;
17012
+ const skip = verificationMetadata(memory).dreamerVerifySkip;
17013
+ if (skip && skip.contentHash === memory.normalizedHash && typeof skip.fingerprint === "string" && skip.fingerprint === await verificationFilesFingerprint(args.projectDirectory, files)) {
17014
+ unchangedSkippedIds.push(memory.id);
17015
+ } else {
17016
+ candidates.push(memory);
17017
+ if (skip)
17018
+ changedSkippedIds.add(memory.id);
17019
+ }
17020
+ }
16731
17021
  const toPrompt = (m) => {
16732
17022
  const verifiedAt = verById.get(m.id)?.verifiedAt ?? 0;
16733
17023
  let verifiedCommit = null;
@@ -16754,7 +17044,7 @@ async function partitionVerifyScope(args) {
16754
17044
  leaseKey: args.leaseKey,
16755
17045
  runStartedAt
16756
17046
  });
16757
- const broadCandidates = candidates.filter((m) => (verById.get(m.id)?.verifiedAt ?? 0) < broadCycleStartAt).sort((a, b) => {
17047
+ const broadCandidates = candidates.filter((m) => changedSkippedIds.has(m.id) || (verById.get(m.id)?.verifiedAt ?? 0) < broadCycleStartAt).sort((a, b) => {
16758
17048
  const verifiedAtA = verById.get(a.id)?.verifiedAt ?? 0;
16759
17049
  const verifiedAtB = verById.get(b.id)?.verifiedAt ?? 0;
16760
17050
  return verifiedAtA - verifiedAtB || a.id - b.id;
@@ -16765,7 +17055,10 @@ async function partitionVerifyScope(args) {
16765
17055
  mode: "broad",
16766
17056
  inScope: broadCandidates.map(toPrompt),
16767
17057
  inScopeIds: broadCandidates.map((m) => m.id),
16768
- skippedIds: candidates.filter((m) => !broadIds.has(m.id)).map((m) => m.id),
17058
+ skippedIds: [
17059
+ ...unchangedSkippedIds,
17060
+ ...candidates.filter((m) => !broadIds.has(m.id)).map((m) => m.id)
17061
+ ],
16769
17062
  broadCycleStartAt,
16770
17063
  reason: `broad cycle (${broadCandidates.length} remain; started ${broadCycleStartAt})`
16771
17064
  };
@@ -16776,7 +17069,7 @@ async function partitionVerifyScope(args) {
16776
17069
  mode: "incremental",
16777
17070
  inScope: [],
16778
17071
  inScopeIds: [],
16779
- skippedIds: [],
17072
+ skippedIds: unchangedSkippedIds,
16780
17073
  reason: "no file-mapped memories in scope"
16781
17074
  };
16782
17075
  }
@@ -16785,11 +17078,11 @@ async function partitionVerifyScope(args) {
16785
17078
  mode,
16786
17079
  inScope: candidates.map(toPrompt),
16787
17080
  inScopeIds: candidates.map((m) => m.id),
16788
- skippedIds: [],
17081
+ skippedIds: unchangedSkippedIds,
16789
17082
  reason
16790
17083
  });
16791
17084
  const resolvedGitRoot = await resolveGitTopLevel(args.projectDirectory);
16792
- const gitRoot = resolvedGitRoot ?? path11.resolve(args.projectDirectory);
17085
+ const gitRoot = resolvedGitRoot ?? path12.resolve(args.projectDirectory);
16793
17086
  const verifiedTimes = candidates.map((m) => verById.get(m.id)?.verifiedAt ?? 0).filter((t) => t > 0);
16794
17087
  const sinceMs = verifiedTimes.length > 0 ? minOf(verifiedTimes) : runStartedAt;
16795
17088
  const changeTimes = await readGitFileChangeTimesSince(args.projectDirectory, sinceMs, resolvedGitRoot ?? undefined);
@@ -16799,11 +17092,11 @@ async function partitionVerifyScope(args) {
16799
17092
  const head = await readGitHead(args.projectDirectory);
16800
17093
  const uncommitted = head ? await readGitChangedFilesSince(args.projectDirectory, head, resolvedGitRoot ?? undefined) ?? new Set : new Set;
16801
17094
  const inScope = [];
16802
- const skippedIds = [];
17095
+ const skippedIds = [...unchangedSkippedIds];
16803
17096
  for (const m of candidates) {
16804
17097
  const v = verById.get(m.id);
16805
17098
  const verifiedAt = v?.verifiedAt ?? 0;
16806
- if (verifiedAt === 0) {
17099
+ if (verifiedAt === 0 || changedSkippedIds.has(m.id)) {
16807
17100
  inScope.push(toPrompt(m));
16808
17101
  continue;
16809
17102
  }
@@ -17173,6 +17466,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17173
17466
  let budgetFinalized = false;
17174
17467
  let failedCompletion;
17175
17468
  let headAtPrompt = null;
17469
+ const skipSnapshots = new Map;
17176
17470
  const onBudgetUpdate = (state) => {
17177
17471
  budgetFinalized ||= state.finalizeFired;
17178
17472
  args.onBudgetUpdate?.({
@@ -17189,6 +17483,11 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17189
17483
  });
17190
17484
  };
17191
17485
  try {
17486
+ if (!args.moduleRoute) {
17487
+ for (const memory of batch) {
17488
+ skipSnapshots.set(memory.id, await raceWithSignal(verificationFilesFingerprint(args.sessionDirectory, memory.mappedFiles), batchSignal));
17489
+ }
17490
+ }
17192
17491
  headAtPrompt = await raceWithSignal(readGitHead(args.sessionDirectory), batchSignal);
17193
17492
  const evidence = !args.forceBroad && batch.every((m) => m.verifiedAt) ? await raceWithSignal(buildVerifyDiffEvidence(args.sessionDirectory, batch), batchSignal) : null;
17194
17493
  const prompt = buildVerifyPrompt(args.projectIdentity, await raceWithSignal(seedVerifyCandidates(batch, args.sessionDirectory), batchSignal), evidence);
@@ -17230,7 +17529,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17230
17529
  tokens: run.completion.usage
17231
17530
  });
17232
17531
  reportUsage(run.completion.messages ?? [], run.childSessionId, run.completion.usage.input + (run.completion.usage.cacheRead ?? 0) + (run.completion.usage.cacheWrite ?? 0));
17233
- return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
17532
+ return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized, skipSnapshots);
17234
17533
  }
17235
17534
  const client = args.client;
17236
17535
  if (!client)
@@ -17290,7 +17589,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17290
17589
  promptSettled = true;
17291
17590
  recordInvocation4(args, startedAt, { status: "completed", messages: run.output });
17292
17591
  reportUsage(run.output, agentSessionId);
17293
- return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
17592
+ return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized, skipSnapshots);
17294
17593
  } catch (error) {
17295
17594
  const desc = describeError(error);
17296
17595
  const providerFailure = error instanceof DreamerProviderOutputFailureError ? error : undefined;
@@ -17310,7 +17609,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17310
17609
  if (interrupted) {
17311
17610
  const text = verifyAssistantText(messages) ?? failedCompletion?.text ?? "";
17312
17611
  const recovered = parsePartialVerifyManifest(text, new Set(batch.map((m) => m.id)));
17313
- const counts = await applyParsedVerifyManifest(args, batch, recovered, headAtPrompt, true);
17612
+ const counts = await applyParsedVerifyManifest(args, batch, recovered, headAtPrompt, true, skipSnapshots);
17314
17613
  return {
17315
17614
  ...counts,
17316
17615
  timedOut: !(error instanceof DreamTokenBudgetExceeded),
@@ -17342,7 +17641,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17342
17641
  signal.removeEventListener("abort", abort);
17343
17642
  }
17344
17643
  }
17345
- async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budgetFinalized = false) {
17644
+ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budgetFinalized = false, skipSnapshots) {
17346
17645
  const batchIds = new Set(batch.map((m) => m.id));
17347
17646
  const batchById = new Map(batch.map((memory) => [memory.id, memory]));
17348
17647
  const valid = {
@@ -17421,6 +17720,15 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17421
17720
  }
17422
17721
  writes.push({ kind: "archive", id: a.id, reason: a.reason });
17423
17722
  }
17723
+ if (!args.moduleRoute) {
17724
+ for (const entry of valid.skipped) {
17725
+ const original = batchById.get(entry.id);
17726
+ if (!original)
17727
+ continue;
17728
+ const fingerprint = skipSnapshots ? skipSnapshots.get(entry.id) ?? null : await verificationFilesFingerprint(args.sessionDirectory, original.mappedFiles);
17729
+ writes.push({ kind: "skip", id: entry.id, fingerprint });
17730
+ }
17731
+ }
17424
17732
  let verified = 0;
17425
17733
  let updated = 0;
17426
17734
  let archived = 0;
@@ -17450,7 +17758,7 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17450
17758
  name: "memory.set_verification",
17451
17759
  arguments: {
17452
17760
  memory_project: args.projectIdentity,
17453
- command_id: `${args.moduleRoute.moduleCommandId}:${createHash9("sha256").update(rows.map((row) => row.memory_id).join(",")).digest("hex").slice(0, 16)}`,
17761
+ command_id: `${args.moduleRoute.moduleCommandId}:${createHash10("sha256").update(rows.map((row) => row.memory_id).join(",")).digest("hex").slice(0, 16)}`,
17454
17762
  rows
17455
17763
  }
17456
17764
  }
@@ -17482,9 +17790,17 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17482
17790
  continue;
17483
17791
  if (w.kind === "verify") {
17484
17792
  recordMemoryVerifications(args.db, w.id, w.files, now);
17485
- if (verifiedHead)
17486
- recordVerifiedCommit(args.db, memory, now, verifiedHead);
17793
+ updateMemoryVerification2(args.db, w.id, "verified");
17794
+ recordVerificationVerdict(args.db, memory, "verified", now);
17795
+ const verifiedMemory = getMemoryById2(args.db, w.id);
17796
+ if (verifiedHead && verifiedMemory)
17797
+ recordVerifiedCommit(args.db, verifiedMemory, now, verifiedHead);
17487
17798
  verified += 1;
17799
+ } else if (w.kind === "skip") {
17800
+ if (memory.content !== batchById.get(w.id)?.content)
17801
+ continue;
17802
+ updateMemoryVerification2(args.db, w.id, "unverified");
17803
+ recordVerificationVerdict(args.db, memory, "skip", now, w.fingerprint);
17488
17804
  } else if (w.kind === "update") {
17489
17805
  const duplicate = getMemoryByHash2(args.db, memory.projectPath, memory.category, w.hash);
17490
17806
  if (duplicate && duplicate.id !== memory.id) {
@@ -17493,6 +17809,8 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17493
17809
  continue;
17494
17810
  }
17495
17811
  rewriteMemoryContent(args.db, memory, w.content, w.hash);
17812
+ updateMemoryVerification2(args.db, w.id, "unverified");
17813
+ recordVerificationVerdict(args.db, memory, "update", now);
17496
17814
  queueMemoryMutation(args.db, {
17497
17815
  projectPath: args.projectIdentity,
17498
17816
  mutationType: "update",
@@ -17503,6 +17821,8 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17503
17821
  updated += 1;
17504
17822
  } else {
17505
17823
  archiveMemory2(args.db, w.id, w.reason);
17824
+ updateMemoryVerification2(args.db, w.id, "stale");
17825
+ recordVerificationVerdict(args.db, memory, "archive", now);
17506
17826
  queueMemoryMutation(args.db, {
17507
17827
  projectPath: args.projectIdentity,
17508
17828
  mutationType: "archive",
@@ -18296,7 +18616,7 @@ function parseFrictionGateVerdict(verdict) {
18296
18616
  }
18297
18617
  function computeRetrospectiveWindowKey(flagged) {
18298
18618
  const anchors = flagged.map((message) => `${message.sessionId}:${message.ts}`).sort().join("|");
18299
- return createHash10("sha256").update(anchors).digest("hex").slice(0, 32);
18619
+ return createHash11("sha256").update(anchors).digest("hex").slice(0, 32);
18300
18620
  }
18301
18621
  function renderFrictionWindow(messages, flaggedOrdinals, radius = 2) {
18302
18622
  const flagged = new Set(flaggedOrdinals);
@@ -20396,16 +20716,17 @@ function resolvePiInvocation(options = {}) {
20396
20716
  const platform = options.platform ?? process.platform;
20397
20717
  const env = options.env ?? process.env;
20398
20718
  const resolvePackageJson = options.resolvePackageJson ?? defaultResolvePiPackageJson;
20719
+ const scriptRuntime = isGenericRuntimeExecutable(execPath) ? execPath : "node";
20399
20720
  const running = resolvePiCliFromRunningEntry(currentScript, fs);
20400
20721
  const checkedPaths = [...running.checkedPaths];
20401
20722
  if (running.cliPath) {
20402
20723
  return {
20403
- command: execPath,
20724
+ command: scriptRuntime,
20404
20725
  prefixArgs: [running.cliPath],
20405
20726
  targetHarness: running.targetHarness ?? piHarnessKindFromExecutable(running.cliPath) ?? resolvePiHarnessKind()
20406
20727
  };
20407
20728
  }
20408
- if (!isGenericRuntimeExecutable(execPath)) {
20729
+ if (piHarnessKindFromExecutable(execPath) !== undefined || !isGenericRuntimeExecutable(execPath) && currentScript?.startsWith(BUN_VIRTUAL_SCRIPT_PREFIX)) {
20409
20730
  return {
20410
20731
  command: execPath,
20411
20732
  prefixArgs: [],
@@ -20415,7 +20736,7 @@ function resolvePiInvocation(options = {}) {
20415
20736
  const bundled = resolveBundledPiCli(currentScript, fs, resolvePackageJson, checkedPaths);
20416
20737
  if (bundled.cliPath) {
20417
20738
  return {
20418
- command: execPath,
20739
+ command: scriptRuntime,
20419
20740
  prefixArgs: [bundled.cliPath],
20420
20741
  targetHarness: bundled.targetHarness ?? piHarnessKindFromExecutable(bundled.cliPath) ?? resolvePiHarnessKind()
20421
20742
  };
@@ -24734,6 +25055,50 @@ function prepareLegacyToolSkeletonConversions(db, sessionId, targets, options =
24734
25055
  }
24735
25056
  };
24736
25057
  }
25058
+ function prepareAgedToolSkeletonConversions(db, sessionId, targets, options = {}) {
25059
+ const candidates = [...targets.keys()].sort((a, b) => a - b);
25060
+ const readRows = (connection) => {
25061
+ const window = getNewestToolTagNumbers2(connection, sessionId, RECENT_TOOL_SKELETON_WINDOW);
25062
+ const recent = new Set(window);
25063
+ const statement = connection.prepare(`SELECT tag_number AS tagNumber, drop_mode AS dropMode, message_id AS callId
25064
+ FROM tags WHERE session_id = ? AND tag_number = ?
25065
+ AND type = 'tool' AND status = 'dropped'
25066
+ AND drop_mode IN ('truncated', 'skeleton_real', 'skeleton_stripped')`);
25067
+ const rows = candidates.filter((id) => !recent.has(id)).flatMap((id) => {
25068
+ const row = statement.get(sessionId, id);
25069
+ return row ? [row] : [];
25070
+ });
25071
+ return { window, rows };
25072
+ };
25073
+ const snapshot = readRows(db);
25074
+ options.prepareRemovals?.(snapshot.rows);
25075
+ const converted = new Map;
25076
+ const plannedRemovals = new Set;
25077
+ for (const row of snapshot.rows) {
25078
+ const target = targets.get(row.tagNumber);
25079
+ if (target?.thinkingDropProtected || target?.thinkingRewriteProtected || target?.identityRepairProtected)
25080
+ continue;
25081
+ if (target?.canDrop?.() !== true || target.prepareDrop?.() === false)
25082
+ continue;
25083
+ if (target.requiresToolArcSkeleton) {
25084
+ const owners = target.mutationParts ? target.mutationParts.map(({ message }) => message).filter((message) => message.info.role === "assistant") : target.message ? [target.message] : [];
25085
+ if (owners.length === 0 || owners.some((message) => !options.thinkingStrippedIds?.has(message.info.id ?? "")))
25086
+ continue;
25087
+ }
25088
+ if (target.cannotRemove?.() === true || target.wouldStrandConversationEnd?.(plannedRemovals) === true)
25089
+ continue;
25090
+ converted.set(row.tagNumber, "full");
25091
+ target.notePlannedRemoval?.(plannedRemovals);
25092
+ }
25093
+ return {
25094
+ converted,
25095
+ isCurrent: (connection) => JSON.stringify(readRows(connection)) === JSON.stringify(snapshot),
25096
+ persist: (connection) => {
25097
+ for (const tagNumber of converted.keys())
25098
+ updateTagDropMode2(connection, sessionId, tagNumber, "full");
25099
+ }
25100
+ };
25101
+ }
24737
25102
  var PENDING_OPERATION_CHUNK_SIZE = 64;
24738
25103
  function applyPendingOperations(sessionId, db, targets, protectedTagIds, preloadedTags, preloadedPendingOps, syntheticPendingOps = [], editMarkerTagIds = new Set, onTagReduced, onBatchComplete, onEligibilitySnapshot) {
24739
25104
  let didMutateMessage = false;
@@ -25623,7 +25988,9 @@ function resolveProtectedTailBoundary(ctx) {
25623
25988
  const offset = Math.max(1, ctx.lastCompartmentEndOrdinal + 1);
25624
25989
  const absoluteMessageCount = getCachedAbsoluteMessageCount2(ctx.sessionId) ?? getRawSessionMessageOrdinalCount2(ctx.sessionId);
25625
25990
  const messages = readRawSessionMessageRange2(ctx.sessionId, Math.max(1, offset - 1), absoluteMessageCount);
25626
- const storedTotals = ctx.storedTokenTotalsForMessages?.(messages.map((m) => m.id), messages) ?? ctx.storedTokenTotals;
25991
+ const storedWeights = ctx.storedTokenTotalsForMessages?.(messages.map((m) => m.id), messages);
25992
+ const storedTotals = storedWeights?.totals ?? ctx.storedTokenTotals;
25993
+ const storedRemovedTotals = storedWeights?.summaryRemovedTotals ?? ctx.storedSummaryRemovedTokenTotals;
25627
25994
  const index = buildTrueRawTokenIndex(ctx.sessionId, messages, {
25628
25995
  providerShapeVersion: ctx.providerShapeVersion,
25629
25996
  cacheNamespace: ctx.cacheNamespace,
@@ -25634,6 +26001,14 @@ function resolveProtectedTailBoundary(ctx) {
25634
26001
  return v === undefined ? null : v;
25635
26002
  } : undefined
25636
26003
  });
26004
+ const summaryRemovedIndex = buildTrueRawTokenIndex(ctx.sessionId, messages, {
26005
+ providerShapeVersion: ctx.providerShapeVersion,
26006
+ cacheNamespace: ctx.cacheNamespace,
26007
+ calibration: ctx.calibration,
26008
+ absoluteMessageCount,
26009
+ measure: "summary-removed",
26010
+ storedTotalForMessage: storedRemovedTotals ? (m) => storedRemovedTotals.get(m.id) ?? null : undefined
26011
+ });
25637
26012
  const rawMessageCount = index.rawMessageCount;
25638
26013
  const usagePercentage = clampPercentage(ctx.usage?.percentage ?? 0);
25639
26014
  const usageInputTokens = Math.max(0, Math.round(ctx.usage?.inputTokens ?? 0));
@@ -25665,6 +26040,7 @@ function resolveProtectedTailBoundary(ctx) {
25665
26040
  createdAt,
25666
26041
  rawRangeFingerprint: "",
25667
26042
  trueRawEligibleTokens: 0,
26043
+ summaryRemovedEligibleTokens: 0,
25668
26044
  oversizeAtomicUnit: false,
25669
26045
  boundaryReason: "empty-session"
25670
26046
  };
@@ -25708,6 +26084,7 @@ function resolveProtectedTailBoundary(ctx) {
25708
26084
  createdAt,
25709
26085
  rawRangeFingerprint,
25710
26086
  trueRawEligibleTokens: index.rangeTokens(offset, protectedTailStart),
26087
+ summaryRemovedEligibleTokens: summaryRemovedIndex.rangeTokens(offset, protectedTailStart),
25711
26088
  oversizeAtomicUnit: false,
25712
26089
  boundaryReason: firstOpenArc ? "open-tool-arc" : "manual-full-recomp"
25713
26090
  };
@@ -25784,7 +26161,7 @@ function resolveProtectedTailBoundary(ctx) {
25784
26161
  observeDiagnostics: (value) => {
25785
26162
  headDiagnostics = value;
25786
26163
  },
25787
- index,
26164
+ index: summaryRemovedIndex,
25788
26165
  protectedTailStart,
25789
26166
  offset,
25790
26167
  arcs,
@@ -25823,6 +26200,7 @@ function resolveProtectedTailBoundary(ctx) {
25823
26200
  createdAt,
25824
26201
  rawRangeFingerprint,
25825
26202
  trueRawEligibleTokens: index.rangeTokens(offset, eligibleEndOrdinal),
26203
+ summaryRemovedEligibleTokens: summaryRemovedIndex.rangeTokens(offset, eligibleEndOrdinal),
25826
26204
  oversizeAtomicUnit: head.oversizeAtomicUnit,
25827
26205
  boundaryReason,
25828
26206
  diagnostics: boundaryDiagnostics({
@@ -25870,8 +26248,11 @@ function resolveBoundaryContext(args) {
25870
26248
  const calibration = sessionDecisionCalibration(args.db, args.sessionId);
25871
26249
  const storedTokenTotalsForMessages = (messageIds, messages = []) => {
25872
26250
  try {
25873
- const totals = getAllStatusTagTokenTotalsFlat2(args.db, args.sessionId, args.taggerFloor ?? 0, calibration, messageIds).totals;
25874
- return omitIncompleteFileTokenTotals(args.db, args.sessionId, messages, totals);
26251
+ const weights = getAllStatusTagTokenTotalsFlat2(args.db, args.sessionId, args.taggerFloor ?? 0, calibration, messageIds);
26252
+ return {
26253
+ totals: omitIncompleteFileTokenTotals(args.db, args.sessionId, messages, weights.totals),
26254
+ summaryRemovedTotals: weights.summaryRemovedTotals
26255
+ };
25875
26256
  } catch (error) {
25876
26257
  sessionLog2(args.sessionId, "protected-tail stored-token map unavailable (live fallback):", error);
25877
26258
  return;
@@ -25907,7 +26288,9 @@ function resolveWrapupProtectedTailBoundary(args) {
25907
26288
  const offset = Math.max(1, ctx.lastCompartmentEndOrdinal + 1);
25908
26289
  const absoluteMessageCount = getCachedAbsoluteMessageCount2(ctx.sessionId) ?? getRawSessionMessageOrdinalCount2(ctx.sessionId);
25909
26290
  const messages = readRawSessionMessageRange2(ctx.sessionId, Math.max(1, offset - 1), absoluteMessageCount);
25910
- const storedTotals = ctx.storedTokenTotalsForMessages?.(messages.map((m) => m.id), messages) ?? ctx.storedTokenTotals;
26291
+ const storedWeights = ctx.storedTokenTotalsForMessages?.(messages.map((m) => m.id), messages);
26292
+ const storedTotals = storedWeights?.totals ?? ctx.storedTokenTotals;
26293
+ const storedRemovedTotals = storedWeights?.summaryRemovedTotals ?? ctx.storedSummaryRemovedTokenTotals;
25911
26294
  const index = buildTrueRawTokenIndex(ctx.sessionId, messages, {
25912
26295
  providerShapeVersion: ctx.providerShapeVersion,
25913
26296
  cacheNamespace: ctx.cacheNamespace,
@@ -25918,6 +26301,14 @@ function resolveWrapupProtectedTailBoundary(args) {
25918
26301
  return value === undefined ? null : value;
25919
26302
  } : undefined
25920
26303
  });
26304
+ const summaryRemovedIndex = buildTrueRawTokenIndex(ctx.sessionId, messages, {
26305
+ providerShapeVersion: ctx.providerShapeVersion,
26306
+ cacheNamespace: ctx.cacheNamespace,
26307
+ calibration: ctx.calibration,
26308
+ absoluteMessageCount,
26309
+ measure: "summary-removed",
26310
+ storedTotalForMessage: storedRemovedTotals ? (m) => storedRemovedTotals.get(m.id) ?? null : undefined
26311
+ });
25921
26312
  const rawMessageCount = index.rawMessageCount;
25922
26313
  const anchorRawMessageCount = Math.max(0, Math.min(rawMessageCount, Math.floor(args.anchorRawMessageCount ?? rawMessageCount)));
25923
26314
  const usagePercentage = clampPercentage(ctx.usage?.percentage ?? 0);
@@ -25979,7 +26370,7 @@ function resolveWrapupProtectedTailBoundary(args) {
25979
26370
  observeDiagnostics: (value) => {
25980
26371
  headDiagnostics = value;
25981
26372
  },
25982
- index,
26373
+ index: summaryRemovedIndex,
25983
26374
  protectedTailStart: targetProtectedTailStart,
25984
26375
  offset,
25985
26376
  arcs: buildToolArcs(messages),
@@ -26016,6 +26407,7 @@ function resolveWrapupProtectedTailBoundary(args) {
26016
26407
  createdAt,
26017
26408
  rawRangeFingerprint,
26018
26409
  trueRawEligibleTokens: index.rangeTokens(offset, targetProtectedTailStart),
26410
+ summaryRemovedEligibleTokens: summaryRemovedIndex.rangeTokens(offset, eligibleEndOrdinal),
26019
26411
  oversizeAtomicUnit: head.oversizeAtomicUnit,
26020
26412
  boundaryReason,
26021
26413
  diagnostics: boundaryDiagnostics({
@@ -27440,6 +27832,52 @@ ${typeof state?.output === "string" ? state.output : JSON.stringify(state?.outpu
27440
27832
  };
27441
27833
  }
27442
27834
 
27835
+ // ../plugin/src/hooks/magic-context/transform-context-state.ts
27836
+ var usageRefusals = new WeakMap;
27837
+ function recordUsageRefusal(db, sessionId, refusedAt = Date.now()) {
27838
+ let sessions = usageRefusals.get(db);
27839
+ if (!sessions) {
27840
+ sessions = new Map;
27841
+ usageRefusals.set(db, sessions);
27842
+ }
27843
+ if (!sessions.has(sessionId))
27844
+ sessions.set(sessionId, refusedAt);
27845
+ }
27846
+ function shouldResetRefusedUsage(db, sessionId, meta, liveReading) {
27847
+ const refusals = usageRefusals.get(db);
27848
+ const refusedAt = refusals?.get(sessionId);
27849
+ if (refusedAt === undefined)
27850
+ return false;
27851
+ const readingAt = liveReading?.hasUsageTokens ? liveReading.updatedAt : meta.lastResponseTime;
27852
+ if (readingAt > refusedAt) {
27853
+ refusals?.delete(sessionId);
27854
+ return false;
27855
+ }
27856
+ const now = Date.now();
27857
+ const foldedAt = meta.cachedM0MaterializedAt ?? 0;
27858
+ if (foldedAt > refusedAt && foldedAt <= now)
27859
+ return true;
27860
+ return !!db.prepare("SELECT 1 FROM compartments WHERE session_id = ? AND harness = ? AND created_at > ? AND created_at <= ? LIMIT 1").get(sessionId, getHarness2(), refusedAt, now);
27861
+ }
27862
+ function resolveUnknownUsageFromWireEstimate(input) {
27863
+ if (input.usage.inputTokens > 0 || !input.pricedPass)
27864
+ return input.usage;
27865
+ const wireTokens = input.wireEstimateTokens;
27866
+ if (typeof wireTokens !== "number" || !Number.isFinite(wireTokens) || wireTokens <= 0) {
27867
+ return input.usage;
27868
+ }
27869
+ const providerTokens = input.providerProvenInputTokens;
27870
+ const providerLimit = input.providerProvenLimitTokens;
27871
+ const canUseProviderMass = input.wireEstimateTrusted === false && typeof providerTokens === "number" && Number.isFinite(providerTokens) && providerTokens > 0 && typeof providerLimit === "number" && Number.isFinite(providerLimit) && providerLimit > 0;
27872
+ const tokens = canUseProviderMass ? Math.max(wireTokens, providerTokens) : wireTokens;
27873
+ const limit = input.usableHardLimit;
27874
+ const estimatedPercentage = typeof limit === "number" && Number.isFinite(limit) && limit > 0 ? tokens / limit * 100 : input.usage.percentage;
27875
+ return {
27876
+ inputTokens: tokens,
27877
+ percentage: Math.max(input.usage.percentage, estimatedPercentage)
27878
+ };
27879
+ }
27880
+
27443
27881
  // ../plugin/src/hooks/magic-context/transform-stage-logger.ts
27444
27882
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
27445
27883
  var SLOW_STAGE_MS = 100;
@@ -27497,7 +27935,7 @@ function recoveryReasoningPrefixes(messages, partsKey) {
27497
27935
  }
27498
27936
 
27499
27937
  // ../plugin/src/shared/tag-transcript.ts
27500
- import { createHash as createHash11 } from "node:crypto";
27938
+ import { createHash as createHash12 } from "node:crypto";
27501
27939
  function buildPersistedDropTargets(transcript, tags, options = {}) {
27502
27940
  const targets = new Map;
27503
27941
  const tools = new Map(tags.filter((tag) => tag.type === "tool").map((tag) => [makeToolCompositeKey(tag.toolOwnerMessageId ?? "", tag.messageId), tag]));
@@ -27580,7 +28018,7 @@ function buildPersistedDropTargets(transcript, tags, options = {}) {
27580
28018
  return targets;
27581
28019
  }
27582
28020
  function textIdentityDigest(value) {
27583
- return createHash11("sha256").update(value).digest("hex");
28021
+ return createHash12("sha256").update(value).digest("hex");
27584
28022
  }
27585
28023
  function buildContentDerivedTextIds(messageId, parts) {
27586
28024
  const sources = parts.filter((part) => part.kind === "text").map((part) => stripTagPrefix(part.getText() ?? ""));
@@ -28536,6 +28974,32 @@ function selectAutoSearchResults(results, threshold) {
28536
28974
  return results.filter((result) => relevance(result) >= threshold).sort((left, right) => relevance(right) - relevance(left));
28537
28975
  }
28538
28976
 
28977
+ // ../plugin/src/hooks/magic-context/auto-search-observation.ts
28978
+ var disabledTurns = new WeakMap;
28979
+ function logDisabledAutoSearch(db, sessionId, messageId, threshold, startedAt) {
28980
+ const sessions = disabledTurns.get(db) ?? new Map;
28981
+ disabledTurns.set(db, sessions);
28982
+ if (sessions.get(sessionId) === messageId)
28983
+ return;
28984
+ sessions.set(sessionId, messageId);
28985
+ observeAutoSearch(sessionId, threshold, startedAt).finish("disabled");
28986
+ }
28987
+ function observeAutoSearch(sessionId, threshold, startedAt) {
28988
+ const diagnostics = createAutoSearchDiagnostics(threshold);
28989
+ let logged = false;
28990
+ return {
28991
+ diagnostics,
28992
+ finish(outcome, reason) {
28993
+ if (logged)
28994
+ return;
28995
+ logged = true;
28996
+ const lanes = Object.entries(diagnostics.lanes).map(([lane, value]) => `${lane}:${value.top?.toFixed(3) ?? "none"}`).join(",");
28997
+ const nearMisses = Object.values(diagnostics.lanes).reduce((sum, lane) => sum + lane.nearMisses, 0);
28998
+ sessionLog2(sessionId, `auto-search: decision outcome=${outcome}${reason ? ` reason=${reason}` : ""} lanes=${lanes || "none"} nearMisses=${nearMisses} threshold=${threshold} pool=${diagnostics.pool} rows=${diagnostics.poolRows ?? "unknown"} workerStarted=${diagnostics.workerStarted ?? "unknown"} peek=${diagnostics.peek} elapsedMs=${Math.round(performance.now() - startedAt)}`);
28999
+ }
29000
+ };
29001
+ }
29002
+
28539
29003
  // ../plugin/src/hooks/magic-context/auto-search-worker-client.ts
28540
29004
  import { Worker } from "node:worker_threads";
28541
29005
  var pendingRegistrations = new WeakMap;
@@ -28682,7 +29146,7 @@ async function acquireSearchWorker(db, key, entry, input, signal) {
28682
29146
  }
28683
29147
  const fresh = !slot;
28684
29148
  if (!slot) {
28685
- const worker = inRootAsyncContext2(() => new Worker(entry, { workerData: { ...input, persistent: true } }));
29149
+ const worker = inRootAsyncContext2(() => input.job === "backfill" && entry.href === defaultAutoSearchWorkerEntry().href ? reuseWorker(entry, { workerData: input }) : new Worker(entry, { workerData: { ...input, persistent: true } }));
28686
29150
  const shutdown = createWorkerShutdown(worker, "auto-search", AUTO_SEARCH_WORKER_CLOSE_DEADLINE_MS);
28687
29151
  slot = {
28688
29152
  worker,
@@ -28760,6 +29224,7 @@ function queueAutoSearchBackfill(db, sessionId, projectPath, query) {
28760
29224
  pending.set(key, work);
28761
29225
  return work;
28762
29226
  }
29227
+ var nextSearchJobId = 0;
28763
29228
  async function executeAutoSearchWorker(db, sessionId, projectPath, query, options, entry, job) {
28764
29229
  if (options.signal?.aborted)
28765
29230
  return [];
@@ -28773,12 +29238,14 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
28773
29238
  readMessages: _readMessages,
28774
29239
  searchMessageHistory: _searchMessageHistory,
28775
29240
  stepTimings: _stepTimings,
29241
+ onAutoSearchDiagnostics: _onAutoSearchDiagnostics,
28776
29242
  ...serializable
28777
29243
  } = options;
28778
29244
  const lifecycle = registerAutoSearchWork(sessionId);
28779
29245
  const signal = deadlineSignal ? AbortSignal.any([deadlineSignal, lifecycle.signal]) : lifecycle.signal;
28780
29246
  const snapshot = getProjectEmbeddingSnapshot2(projectPath);
28781
29247
  const input = {
29248
+ jobId: ++nextSearchJobId,
28782
29249
  job,
28783
29250
  path,
28784
29251
  harness: getHarness2(),
@@ -28814,6 +29281,11 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
28814
29281
  throw new AutoSearchBusyError;
28815
29282
  }
28816
29283
  const { slot, fresh } = lease;
29284
+ if (options.autoSearchDiagnostics) {
29285
+ options.autoSearchDiagnostics.workerStarted = fresh;
29286
+ if (fresh)
29287
+ options.autoSearchDiagnostics.pool = "cold";
29288
+ }
28817
29289
  const worker = slot.worker;
28818
29290
  const detachable = job === "search" && entry.href === defaultAutoSearchWorkerEntry().href;
28819
29291
  const startedAt = performance.now();
@@ -28893,6 +29365,17 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
28893
29365
  const onMessage = async (reply) => {
28894
29366
  if (released)
28895
29367
  return;
29368
+ if (entry.href === defaultAutoSearchWorkerEntry().href && reply.jobId !== input.jobId)
29369
+ return;
29370
+ if (reply.kind === "diagnostics") {
29371
+ if (!settled && options.autoSearchDiagnostics) {
29372
+ const workerStarted = options.autoSearchDiagnostics.workerStarted;
29373
+ Object.assign(options.autoSearchDiagnostics, reply.diagnostics, {
29374
+ workerStarted
29375
+ });
29376
+ }
29377
+ return;
29378
+ }
28896
29379
  if (reply.kind === "result" || reply.kind === "error") {
28897
29380
  if (detachedAt !== undefined) {
28898
29381
  logLateSearch(sessionId, performance.now() - detachedAt, detachedAt - startedAt, ownerVectorMs, reply.kind === "result" ? reply.timings : undefined, reply.kind === "error");
@@ -28911,7 +29394,7 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
28911
29394
  }
28912
29395
  if (reply.kind === "messages" || reply.kind === "accepted")
28913
29396
  return;
28914
- const response = { id: reply.id };
29397
+ const response = { id: reply.id, jobId: input.jobId };
28915
29398
  if (!signal.aborted) {
28916
29399
  const requestedAt = performance.now();
28917
29400
  try {
@@ -29096,8 +29579,6 @@ async function runAutoSearchHintForPi(args) {
29096
29579
  const timestamp = message.timestamp;
29097
29580
  return `test-entry-${index}:${typeof timestamp === "number" ? timestamp : "no-ts"}`;
29098
29581
  }) : args.entryIds;
29099
- if (!options.enabled)
29100
- return messages;
29101
29582
  const strictResolutionFailed = entryIds === null;
29102
29583
  const effectiveEntryIds = strictResolutionFailed ? messages.map((message) => {
29103
29584
  const id = message.id;
@@ -29107,6 +29588,11 @@ async function runAutoSearchHintForPi(args) {
29107
29588
  if (found === null)
29108
29589
  return messages;
29109
29590
  const { message: userMsg, messageId: userMsgId } = found;
29591
+ if (!options.enabled) {
29592
+ if (found.index === messages.length - 1)
29593
+ logDisabledAutoSearch(db, sessionId, userMsgId, options.scoreThreshold ?? DEFAULT_SCORE_THRESHOLD, startedAt);
29594
+ return messages;
29595
+ }
29110
29596
  await wait(coalesceAutoSearchTurn(db, sessionId, userMsgId, async (lifecycleSignal) => {
29111
29597
  args.passGuard?.assert();
29112
29598
  if (lifecycleSignal.aborted)
@@ -29133,10 +29619,12 @@ async function runAutoSearchHintForPi(args) {
29133
29619
  return messages;
29134
29620
  if (!isUsableProjectIdentity(options.projectPath))
29135
29621
  return messages;
29622
+ const observation = observeAutoSearch(sessionId, options.scoreThreshold ?? DEFAULT_SCORE_THRESHOLD, startedAt);
29136
29623
  const snapshot = getProjectEmbeddingSnapshot2(options.projectPath);
29137
29624
  if (!snapshot) {
29138
29625
  queueAutoSearchRegistration(db, options.projectPath, args.ensureProjectRegistered);
29139
29626
  persistAutoSearchSkip(db, sessionId, userMsgId, "empty");
29627
+ observation.finish("no-candidates", "unregistered");
29140
29628
  return messages;
29141
29629
  }
29142
29630
  const writeNoHintAndReconcile = async (reason) => {
@@ -29149,17 +29637,20 @@ async function runAutoSearchHintForPi(args) {
29149
29637
  args.passGuard?.assert();
29150
29638
  if (outcome === null) {
29151
29639
  persistAutoSearchSkip(db, sessionId, userMsgId);
29640
+ observation.finish("timeout");
29152
29641
  return;
29153
29642
  }
29154
- if (!outcome.ok)
29643
+ if (!outcome.ok) {
29644
+ observation.finish("error", outcome.kind);
29155
29645
  return;
29646
+ }
29156
29647
  if (outcome.kind === "already-present" && outcome.decision.decision === "hint") {
29157
29648
  appendHintToUserMessage(userMsg, outcome.decision.text);
29158
29649
  }
29650
+ observation.finish(outcome.decision.decision === "hint" ? "attached" : reason === "below-threshold" ? "below-threshold" : reason === "error" ? "error" : "no-candidates", reason);
29159
29651
  };
29160
29652
  const rawPartsText = collectUserPromptParts(userMsg);
29161
29653
  if (hasStackedAugmentation(rawPartsText)) {
29162
- sessionLog2(sessionId, "auto-search: skipping — user message already carries augmentation/hint");
29163
29654
  await writeNoHintAndReconcile("stacked");
29164
29655
  return messages;
29165
29656
  }
@@ -29179,6 +29670,7 @@ async function runAutoSearchHintForPi(args) {
29179
29670
  const embeddingEnabled = snapshot ? snapshot.historyEnabled : true;
29180
29671
  const gitCommitsEnabled = snapshot?.gitCommitEnabled ?? false;
29181
29672
  const searchOptions = {
29673
+ autoSearchDiagnostics: observation.diagnostics,
29182
29674
  limit: 10,
29183
29675
  maxMessageOrdinal: Math.max(0, getLastCompartmentEndMessage(db, sessionId)),
29184
29676
  excludeMessageId: userMsgId,
@@ -29207,11 +29699,10 @@ async function runAutoSearchHintForPi(args) {
29207
29699
  if (lifecycleSignal.aborted)
29208
29700
  return messages;
29209
29701
  if (error instanceof AutoSearchBusyError) {
29210
- sessionLog2(sessionId, "auto-search: an earlier search of this session is still running, skipping hint for this turn");
29702
+ observation.finish("timeout", "worker-busy");
29211
29703
  persistAutoSearchSkip(db, sessionId, userMsgId);
29212
29704
  return messages;
29213
29705
  }
29214
- log2(`[auto-search] unified search failed for session ${sessionId} (no-hint frozen for this turn): ${error instanceof Error ? error.message : String(error)}`);
29215
29706
  await writeNoHintAndReconcile("error");
29216
29707
  return messages;
29217
29708
  }
@@ -29220,7 +29711,7 @@ async function runAutoSearchHintForPi(args) {
29220
29711
  return messages;
29221
29712
  try {
29222
29713
  if (results === null) {
29223
- sessionLog2(sessionId, `auto-search: timed out after ${AUTO_SEARCH_TIMEOUT_MS}ms, skipping hint for this turn`);
29714
+ observation.finish("timeout");
29224
29715
  persistAutoSearchSkip(db, sessionId, userMsgId);
29225
29716
  return messages;
29226
29717
  }
@@ -29231,7 +29722,6 @@ async function runAutoSearchHintForPi(args) {
29231
29722
  const scoreThreshold = options.scoreThreshold ?? DEFAULT_SCORE_THRESHOLD;
29232
29723
  results = selectAutoSearchResults(results, scoreThreshold);
29233
29724
  if (results.length === 0) {
29234
- sessionLog2(sessionId, `auto-search: no fragment meets relevance threshold ${scoreThreshold}`);
29235
29725
  await writeNoHintAndReconcile("below-threshold");
29236
29726
  return messages;
29237
29727
  }
@@ -29255,14 +29745,17 @@ ${hintText}`;
29255
29745
  args.passGuard?.assert();
29256
29746
  if (outcome === null) {
29257
29747
  persistAutoSearchSkip(db, sessionId, userMsgId);
29748
+ observation.finish("timeout");
29258
29749
  return messages;
29259
29750
  }
29260
- if (!outcome.ok)
29751
+ if (!outcome.ok) {
29752
+ observation.finish("error", outcome.kind);
29261
29753
  return messages;
29754
+ }
29262
29755
  if (outcome.decision.decision === "hint") {
29263
29756
  appendHintToUserMessage(userMsg, outcome.decision.text);
29264
29757
  }
29265
- sessionLog2(sessionId, `auto-search: attached hint to ${userMsgId} (${results.length} fragments, top relevance ${(results[0].relevanceScore ?? results[0].score).toFixed(3)})`);
29758
+ observation.finish(outcome.decision.decision === "hint" ? "attached" : "no-candidates");
29266
29759
  return messages;
29267
29760
  } finally {
29268
29761
  if (!lifecycleSignal.aborted && results !== null) {
@@ -29410,7 +29903,7 @@ function persistFilteredNoise(db, sessionId, chunk, eligibleEnd) {
29410
29903
  }
29411
29904
 
29412
29905
  // src/pi-history-retirement.ts
29413
- var KEY = "piHistorianRetirement";
29906
+ var KEY2 = "piHistorianRetirement";
29414
29907
  function readState(db, sessionId) {
29415
29908
  const row = db.prepare("SELECT deferred_execute_state AS state FROM session_meta WHERE session_id = ?").get(sessionId);
29416
29909
  const state = row?.state ? JSON.parse(row.state) : {};
@@ -29419,24 +29912,24 @@ function readState(db, sessionId) {
29419
29912
  return state;
29420
29913
  }
29421
29914
  function readPiHistoryRetirement(db, sessionId) {
29422
- const target = readState(db, sessionId)[KEY];
29915
+ const target = readState(db, sessionId)[KEY2];
29423
29916
  return typeof target === "string" && target.length > 0 ? target : null;
29424
29917
  }
29425
29918
  function queuePiHistoryRetirement(db, sessionId, throughMessageId) {
29426
29919
  ensureSessionMetaRow(db, sessionId);
29427
29920
  const state = readState(db, sessionId);
29428
- state[KEY] = throughMessageId;
29921
+ state[KEY2] = throughMessageId;
29429
29922
  db.prepare("UPDATE session_meta SET deferred_execute_state = ? WHERE session_id = ?").run(JSON.stringify(state), sessionId);
29430
29923
  }
29431
29924
  function acknowledgePiHistoryRetirement(db, sessionId, publishedEnd, ordinalOf) {
29432
29925
  const state = readState(db, sessionId);
29433
- const target = state[KEY];
29926
+ const target = state[KEY2];
29434
29927
  if (typeof target !== "string")
29435
29928
  return;
29436
29929
  const ordinal = ordinalOf(target);
29437
29930
  if (ordinal === undefined || ordinal > publishedEnd)
29438
29931
  return;
29439
- delete state[KEY];
29932
+ delete state[KEY2];
29440
29933
  db.prepare("UPDATE session_meta SET deferred_execute_state = ? WHERE session_id = ?").run(JSON.stringify(state), sessionId);
29441
29934
  }
29442
29935
 
@@ -29965,7 +30458,7 @@ async function runPiHistorian(deps) {
29965
30458
  db,
29966
30459
  sessionId,
29967
30460
  runId: crypto2.randomUUID(),
29968
- trueRawTokens: boundarySnapshot.trueRawEligibleTokens,
30461
+ trueRawTokens: boundarySnapshot.summaryRemovedEligibleTokens,
29969
30462
  usagePercentage: boundarySnapshot.usagePercentage,
29970
30463
  usable,
29971
30464
  perRunCap,
@@ -32010,12 +32503,28 @@ function applyPiHeuristicCleanup(sessionId, db, targets, piMessages, config, pre
32010
32503
  const priorInputSample = getEmergencyInputSample(db, sessionId);
32011
32504
  const candidateTags = tags.filter((t) => t.status === "active" && t.type === "tool" && targets.get(t.tagNumber)?.canDrop?.());
32012
32505
  const recentTags = new Set(candidateTags.slice().sort((a, b) => b.tagNumber - a.tagNumber).slice(0, 20).map((tag) => tag.tagNumber));
32013
- const calibration = sessionDecisionCalibration(db, sessionId);
32506
+ const calibration = emergency.fitCalibration ?? sessionDecisionCalibration(db, sessionId);
32014
32507
  const tPrepare = performance.now();
32015
32508
  config.prepareToolRemovalMeasurements?.(tags.flatMap((tag) => tag.status === "active" && tag.type === "tool" && tag.messageId && targets.get(tag.tagNumber)?.measureReclaim ? [tag.messageId] : []));
32016
32509
  logStep("prepareNativeRemovals", tPrepare);
32017
32510
  const tMeasure = performance.now();
32018
- const activeTags = tags.filter((t) => t.status === "active").map((tag) => measureEmergencyTag(tag, targets.get(tag.tagNumber), calibration, targets.get(tag.tagNumber)?.requiresToolArcSkeleton === true || (emergency.usagePercentage ?? 0) < 95 && recentTags.has(tag.tagNumber) && hasSmallToolInput(targets.get(tag.tagNumber))));
32511
+ const activeTags = tags.filter((t) => t.status === "active").map((tag) => {
32512
+ const target = targets.get(tag.tagNumber);
32513
+ const imageTokens = emergency.serializedImageTokens?.get(tag.messageId) ?? 0;
32514
+ const observation = target?.measureReclaim;
32515
+ const measuredTarget = imageTokens && observation ? {
32516
+ ...target,
32517
+ measureReclaim(skeleton) {
32518
+ const measured = observation(skeleton);
32519
+ return measured ? {
32520
+ ...measured,
32521
+ beforeTools: measured.beforeTools + imageTokens,
32522
+ afterTools: measured.afterTools + (target.canDrop?.() ? 0 : imageTokens)
32523
+ } : measured;
32524
+ }
32525
+ } : target;
32526
+ return measureEmergencyTag(tag, measuredTarget, calibration, targets.get(tag.tagNumber)?.requiresToolArcSkeleton === true || (emergency.usagePercentage ?? 0) < 95 && recentTags.has(tag.tagNumber) && hasSmallToolInput(targets.get(tag.tagNumber)));
32527
+ });
32019
32528
  const byTag = new Map(activeTags.map((tag) => [tag.tagNumber, tag]));
32020
32529
  const droppableTags = candidateTags.flatMap((tag) => {
32021
32530
  const measured = byTag.get(tag.tagNumber);
@@ -35316,6 +35825,118 @@ function authorizePiToolRemoval(args) {
35316
35825
  }
35317
35826
  }
35318
35827
 
35828
+ // src/native-replay-tag-provenance-pi.ts
35829
+ import { createHash as createHash13 } from "node:crypto";
35830
+ var PI_NATIVE_TAG_STRIP_PROVENANCE = "magicContextNativeTagStrips";
35831
+ function nativeResponseTagStripKey(id, phase, text) {
35832
+ return createHash13("sha256").update(JSON.stringify([id, phase, text])).digest("hex");
35833
+ }
35834
+ function ownsNativeResponseTagStrip(decisions, id, phase, text) {
35835
+ if (!isRecord(decisions) || phase !== undefined && phase !== "commentary" && phase !== "final_answer")
35836
+ return false;
35837
+ return Object.entries(decisions).some(([key, original]) => typeof original === "string" && /^§\d+§ /u.test(original) && original.replace(/^§\d+§ /u, "") === text && nativeResponseTagStripKey(id, phase, original) === key);
35838
+ }
35839
+
35840
+ // src/native-replay-tags-pi.ts
35841
+ var FIELD = "piResponsesTagStrips";
35842
+ var TAG = /^§\d+§ /u;
35843
+ function readDecisions(value) {
35844
+ if (value === undefined)
35845
+ return new Set;
35846
+ if (!Array.isArray(value) || !value.every((key) => typeof key === "string" && /^[a-f0-9]{64}$/u.test(key)))
35847
+ throw new Error("Invalid persisted Responses tag strips");
35848
+ return new Set(value);
35849
+ }
35850
+ function signature(part) {
35851
+ if (!isRecord(part) || typeof part.textSignature !== "string")
35852
+ return;
35853
+ try {
35854
+ const value = JSON.parse(part.textSignature);
35855
+ if (isRecord(value) && value.v === 1 && typeof value.id === "string" && value.id.length > 0 && (value.phase === undefined || value.phase === "commentary" || value.phase === "final_answer"))
35856
+ return { id: value.id, phase: value.phase };
35857
+ } catch {}
35858
+ }
35859
+ function applyNativeResponseTagReplayPi(args) {
35860
+ let saved;
35861
+ const added = new Set;
35862
+ const pending = new Map;
35863
+ for (const [index, message] of args.messages.entries()) {
35864
+ if (!isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content) || ![
35865
+ "openai-responses",
35866
+ "azure-openai-responses",
35867
+ "openai-codex-responses"
35868
+ ].includes(String(message.api)))
35869
+ continue;
35870
+ const payload = message.providerPayload;
35871
+ if (!isRecord(payload) || payload.type !== "openaiResponsesHistory" || payload.dt !== true || !Array.isArray(payload.items))
35872
+ continue;
35873
+ if (saved === undefined) {
35874
+ try {
35875
+ saved = readDecisions(readReplayEnvelope(args.db, args.sessionId)[FIELD]);
35876
+ } catch (error) {
35877
+ sessionLog2(args.sessionId, `native tag replay unavailable; retaining native history: ${String(error)}`);
35878
+ return 0;
35879
+ }
35880
+ }
35881
+ if (!args.canApply && saved.size === 0)
35882
+ return 0;
35883
+ const frozen = saved;
35884
+ const content = message.content;
35885
+ const nativeItems = payload.items;
35886
+ const decisions = {};
35887
+ let changed = false;
35888
+ const items = nativeItems.map((item) => {
35889
+ if (!isRecord(item) || item.type !== "message" || item.role !== "assistant" || item.phase !== undefined && item.phase !== "commentary" && item.phase !== "final_answer" || !Array.isArray(item.content) || item.content.length !== 1)
35890
+ return item;
35891
+ const output = item.content[0];
35892
+ if (!isRecord(output) || output.type !== "output_text" || typeof output.text !== "string" || !TAG.test(output.text))
35893
+ return item;
35894
+ if (nativeItems.filter((other) => isRecord(other) && other.id === item.id && other.phase === item.phase).length !== 1)
35895
+ return item;
35896
+ const key = nativeResponseTagStripKey(item.id, item.phase, output.text);
35897
+ if (!frozen.has(key)) {
35898
+ if (!args.canApply)
35899
+ return item;
35900
+ const blocks = content.filter((block) => {
35901
+ const identity = signature(block);
35902
+ return identity && identity.id === item.id && identity.phase === item.phase;
35903
+ });
35904
+ const block = blocks[0];
35905
+ if (blocks.length !== 1 || !isRecord(block) || block.type !== "text" || typeof block.text !== "string" || block.text.replace(TAG, "") !== output.text.replace(TAG, ""))
35906
+ return item;
35907
+ added.add(key);
35908
+ }
35909
+ decisions[key] = output.text;
35910
+ changed = true;
35911
+ return {
35912
+ ...item,
35913
+ content: [{ ...output, text: output.text.replace(TAG, "") }]
35914
+ };
35915
+ });
35916
+ if (changed)
35917
+ pending.set(index, {
35918
+ ...message,
35919
+ [PI_NATIVE_TAG_STRIP_PROVENANCE]: decisions,
35920
+ providerPayload: { ...payload, items }
35921
+ });
35922
+ }
35923
+ if (added.size > 0) {
35924
+ const persisted = updateReplayDocument(args.db, args.sessionId, (doc) => {
35925
+ const decisions = readDecisions(doc[FIELD]);
35926
+ for (const key of added)
35927
+ decisions.add(key);
35928
+ doc.version = 2;
35929
+ doc[FIELD] = [...decisions];
35930
+ return true;
35931
+ });
35932
+ if (!persisted)
35933
+ throw new Error("Could not persist Responses tag strips");
35934
+ }
35935
+ for (const [index, message] of pending)
35936
+ args.messages[index] = message;
35937
+ return added.size;
35938
+ }
35939
+
35319
35940
  // src/note-visibility-pi.ts
35320
35941
  var NOTE_TOOL_NAME = "ctx_note";
35321
35942
  var READ_ACTION = "read";
@@ -36633,7 +37254,7 @@ function piWithoutDateReminders(payload, api, cwd, developerRole, includesPrepar
36633
37254
  const text = google ? singleText(row.parts) : api === "bedrock-converse-stream" ? singleText(row.content) : api.endsWith("responses") ? singleText(row.content, "input_text") ?? (api === "openai-codex-responses" ? row.content : undefined) : row.content;
36634
37255
  if (piDateReminderText(text, cwd)) {
36635
37256
  const keys = google ? ["role", "parts"] : api.endsWith("responses") ? ["role", "content", "type"] : ["role", "content"];
36636
- const role = api === "ollama-chat" ? "system" : api === "openai-completions" && developerRole ? "developer" : "user";
37257
+ const role = api === "ollama-chat" ? "system" : api === "openai-codex-responses" || api === "openai-completions" && developerRole ? "developer" : "user";
36637
37258
  if (!firstUser || row.role !== role || Object.keys(row).some((key) => !keys.includes(key)) || row.type !== undefined && row.type !== "message")
36638
37259
  return;
36639
37260
  if (!removed) {
@@ -36846,7 +37467,7 @@ function coldReplayable(items) {
36846
37467
  }
36847
37468
  return reasoning;
36848
37469
  }
36849
- function piResponsesAssistantTexts(value, model, host, alternatives) {
37470
+ function piResponsesAssistantTexts(value, model, host, alternatives, frozenTagStrips) {
36850
37471
  const message = object10(value);
36851
37472
  if (!message || !Array.isArray(message.content))
36852
37473
  return;
@@ -36890,6 +37511,14 @@ function piResponsesAssistantTexts(value, model, host, alternatives) {
36890
37511
  const payload = object10(message.providerPayload);
36891
37512
  if (message.api !== model?.api || typeof model?.id !== "string" || typeof model?.provider !== "string" || message.model !== model?.id || (payload?.provider ?? message.provider) !== model?.provider || payload?.type !== "openaiResponsesHistory" || payload.dt !== true || !Array.isArray(payload.items))
36892
37513
  return texts;
37514
+ if (host === "omp" && model.api === "openai-codex-responses" && texts.every((text) => !/\S/u.test(JSON.parse(text)[0])) && payload.items.every((value) => {
37515
+ const item = object10(value);
37516
+ return item?.type === "reasoning" || item?.type === "message" && item.role === "assistant" && Array.isArray(item.content) && item.content.every((value) => {
37517
+ const part = object10(value);
37518
+ return part?.type === "output_text" && typeof part.text === "string" && !/\S/u.test(part.text);
37519
+ });
37520
+ }))
37521
+ return [];
36893
37522
  const replay = [];
36894
37523
  let bound = 0;
36895
37524
  let output = false;
@@ -36900,6 +37529,7 @@ function piResponsesAssistantTexts(value, model, host, alternatives) {
36900
37529
  ].includes(model?.api ?? "");
36901
37530
  let unsigned = false;
36902
37531
  let rewrittenText = false;
37532
+ let allUnsignedFrozen = true;
36903
37533
  for (const value of payload.items) {
36904
37534
  const item = object10(value);
36905
37535
  if (item?.type === "reasoning")
@@ -36929,6 +37559,7 @@ function piResponsesAssistantTexts(value, model, host, alternatives) {
36929
37559
  if (!allowUnsigned || "textSignature" in block || stripped !== block.text.replace(/^§\d+§ /u, ""))
36930
37560
  return texts;
36931
37561
  unsigned = true;
37562
+ allUnsignedFrozen &&= part.text === block.text && ownsNativeResponseTagStrip(frozenTagStrips, item.id, item.phase, part.text);
36932
37563
  rewrittenText ||= part.text !== stripped;
36933
37564
  }
36934
37565
  bound++;
@@ -36940,7 +37571,7 @@ function piResponsesAssistantTexts(value, model, host, alternatives) {
36940
37571
  for (const text of itemTexts)
36941
37572
  replay.push(textIdentity2(wellFormed2(text), item.phase));
36942
37573
  }
36943
- if (!output || bound !== blocks.length || unsigned && !rewrittenText)
37574
+ if (!output || bound !== blocks.length || unsigned && !rewrittenText && !allUnsignedFrozen)
36944
37575
  return texts;
36945
37576
  if (unsigned && alternatives) {
36946
37577
  alternatives.fallback = texts;
@@ -37555,7 +38186,7 @@ function buildPiPreparedStructure(messages, model, prepareUser, host = "omp") {
37555
38186
  continue;
37556
38187
  }
37557
38188
  const alternatives = {};
37558
- const texts = piResponsesAssistantTexts(projectedMessages[index], model, host, alternatives);
38189
+ const texts = piResponsesAssistantTexts(projectedMessages[index], model, host, alternatives, object11(projectedMessages[index])?.[PI_NATIVE_TAG_STRIP_PROVENANCE]);
37559
38190
  if (!texts) {
37560
38191
  responsesMessages = undefined;
37561
38192
  break;
@@ -37986,13 +38617,16 @@ function countAnthropicOptionalAlignments(plan, base, wire, compare, reminders,
37986
38617
  function piPreparedStructureMismatch(payload, prepared, onImageTrim) {
37987
38618
  const plan = prepared && dateReminderPlans.get(prepared);
37988
38619
  try {
37989
- const messages = plan ? object11(payload)?.messages : undefined;
38620
+ const body = plan ? object11(payload) : undefined;
38621
+ const messages = body?.messages ?? body?.input;
37990
38622
  if (Array.isArray(messages)) {
37991
38623
  for (const value of messages) {
37992
38624
  const message = object11(value);
37993
38625
  if (message?.role !== "system" && message?.role !== "developer")
37994
38626
  break;
37995
- if (piDateReminderWireText(message.content, plan?.cwd) !== undefined)
38627
+ const content = message.content;
38628
+ const inputText = Array.isArray(content) && content.length === 1 && object11(content[0])?.type === "input_text" ? object11(content[0])?.text : undefined;
38629
+ if (piDateReminderWireText(content, plan?.cwd) !== undefined || piDateReminderText(inputText, plan?.cwd))
37996
38630
  return "comparison=role";
37997
38631
  }
37998
38632
  }
@@ -40059,9 +40693,9 @@ function createPiFallbackIndex() {
40059
40693
  }
40060
40694
 
40061
40695
  // src/pi-fit-reclaim.ts
40062
- import { createHash as createHash12, randomUUID as randomUUID4 } from "node:crypto";
40696
+ import { createHash as createHash14, randomUUID as randomUUID4 } from "node:crypto";
40063
40697
  var pendingInput = new Set;
40064
- var KEY2 = "piUpperEstimateReclaim";
40698
+ var KEY3 = "piUpperEstimateReclaim";
40065
40699
  function piFitOverflowPossible(args) {
40066
40700
  if (!args.hasUI && args.inputTokens <= 0 || args.recoveryArmed || args.forceMaterialization || args.imported || args.coldInjection || args.cacheBusting || pendingInput.has(args.sessionId))
40067
40701
  return true;
@@ -40095,7 +40729,7 @@ function piUpperReclaimCursor(args) {
40095
40729
  sessionId: args.sessionId,
40096
40730
  modelKey: args.modelKey,
40097
40731
  contextWindow: args.contextWindow ?? null,
40098
- head: head ? createHash12("sha256").update(JSON.stringify([
40732
+ head: head ? createHash14("sha256").update(JSON.stringify([
40099
40733
  Buffer.from(head.cachedM0Bytes ?? []).toString("base64"),
40100
40734
  Buffer.from(head.cachedM1Bytes ?? []).toString("base64"),
40101
40735
  head.cachedM0MuralDataUrl ?? null
@@ -40113,18 +40747,18 @@ function canReclaim(previous, cursor) {
40113
40747
  function shouldReclaimPiUpperEstimate(db, cursor) {
40114
40748
  if (!cursor)
40115
40749
  return false;
40116
- return canReclaim(claimFrom(readReplayEnvelope(db, cursor.sessionId)[KEY2], cursor.sessionId), cursor);
40750
+ return canReclaim(claimFrom(readReplayEnvelope(db, cursor.sessionId)[KEY3], cursor.sessionId), cursor);
40117
40751
  }
40118
40752
  function reservePiUpperEstimateReclaim(db, cursor) {
40119
40753
  const reservation = randomUUID4();
40120
40754
  return db.transaction(() => {
40121
40755
  let claimed = false;
40122
40756
  if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
40123
- claimed = canReclaim(claimFrom(doc[KEY2], cursor.sessionId), cursor);
40757
+ claimed = canReclaim(claimFrom(doc[KEY3], cursor.sessionId), cursor);
40124
40758
  if (!claimed)
40125
40759
  return false;
40126
40760
  doc.version = 2;
40127
- doc[KEY2] = { ...cursor, reservation };
40761
+ doc[KEY3] = { ...cursor, reservation };
40128
40762
  return true;
40129
40763
  }))
40130
40764
  throw new Error("Could not reserve upper-estimate reclaim");
@@ -40136,10 +40770,10 @@ function recordPiUpperEstimateReclaim(db, cursor, reservation) {
40136
40770
  if (!owner)
40137
40771
  return;
40138
40772
  if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
40139
- if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== owner)
40773
+ if (claimFrom(doc[KEY3], cursor.sessionId)?.reservation !== owner)
40140
40774
  return false;
40141
40775
  doc.version = 2;
40142
- doc[KEY2] = cursor;
40776
+ doc[KEY3] = cursor;
40143
40777
  return true;
40144
40778
  }))
40145
40779
  throw new Error("Could not persist upper-estimate reclaim cursor");
@@ -40148,19 +40782,19 @@ function recordPiUpperEstimateReclaim(db, cursor, reservation) {
40148
40782
  function releasePiUpperEstimateReclaim(db, cursor, reservation) {
40149
40783
  withoutSqliteTransformPass2(() => withSqliteTurnDeadline2(() => 0, () => {
40150
40784
  if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
40151
- if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== reservation)
40785
+ if (claimFrom(doc[KEY3], cursor.sessionId)?.reservation !== reservation)
40152
40786
  return false;
40153
- delete doc[KEY2];
40787
+ delete doc[KEY3];
40154
40788
  return true;
40155
40789
  }))
40156
40790
  throw new Error("Could not release upper-estimate reclaim");
40157
40791
  }));
40158
40792
  }
40159
40793
  function resetPiUpperEstimateReclaim(db, sessionId) {
40160
- if (!claimFrom(readReplayEnvelope(db, sessionId)[KEY2], sessionId))
40794
+ if (!claimFrom(readReplayEnvelope(db, sessionId)[KEY3], sessionId))
40161
40795
  return;
40162
40796
  if (!updateReplayDocument(db, sessionId, (doc) => {
40163
- delete doc[KEY2];
40797
+ delete doc[KEY3];
40164
40798
  return true;
40165
40799
  }))
40166
40800
  throw new Error("Could not clear upper-estimate reclaim cursor");
@@ -40182,7 +40816,7 @@ async function waitForPiHistorianInTurn(sessionId, remainingMs, wait) {
40182
40816
  }
40183
40817
 
40184
40818
  // src/pi-lkg-fit-envelope.ts
40185
- import { createHash as createHash13 } from "node:crypto";
40819
+ import { createHash as createHash15 } from "node:crypto";
40186
40820
  function readPiLkgFitEnvelope(ctx, pi, modelKey, frozen, wireSchema) {
40187
40821
  try {
40188
40822
  if (!modelKey || typeof pi.getAllTools !== "function")
@@ -40244,7 +40878,7 @@ function readPiLkgFitEnvelope(ctx, pi, modelKey, frozen, wireSchema) {
40244
40878
  refusalToolDefinitionTokens,
40245
40879
  toolDefinitionsMeasured: refusalToolDefinitionTokens !== undefined,
40246
40880
  envelopeBytes: Buffer.byteLength(envelopeJson),
40247
- envelopeSignature: activeNames ? createHash13("sha256").update(envelopeJson).update(JSON.stringify(activeNames)).digest("hex") : undefined,
40881
+ envelopeSignature: activeNames ? createHash15("sha256").update(envelopeJson).update(JSON.stringify(activeNames)).digest("hex") : undefined,
40248
40882
  calibration
40249
40883
  };
40250
40884
  } catch {
@@ -40253,13 +40887,13 @@ function readPiLkgFitEnvelope(ctx, pi, modelKey, frozen, wireSchema) {
40253
40887
  }
40254
40888
 
40255
40889
  // src/pi-message-identity.ts
40256
- import { createHash as createHash16 } from "node:crypto";
40890
+ import { createHash as createHash18 } from "node:crypto";
40257
40891
 
40258
40892
  // src/pi-tool-identity.ts
40259
- import { createHash as createHash15 } from "node:crypto";
40893
+ import { createHash as createHash17 } from "node:crypto";
40260
40894
 
40261
40895
  // src/served-array-ledger.ts
40262
- import { createHash as createHash14 } from "node:crypto";
40896
+ import { createHash as createHash16 } from "node:crypto";
40263
40897
  import {
40264
40898
  closeSync as closeSync2,
40265
40899
  fstatSync,
@@ -40269,7 +40903,7 @@ import {
40269
40903
  readSync as readSync2,
40270
40904
  statSync as statSync6
40271
40905
  } from "node:fs";
40272
- import * as path12 from "node:path";
40906
+ import * as path13 from "node:path";
40273
40907
 
40274
40908
  // src/pi-served-identities.ts
40275
40909
  class PiServedEnvelopeUnavailable extends Error {
@@ -40537,20 +41171,20 @@ function getPiLastServedArray(sessionId) {
40537
41171
  return previous ? `[${previous.serializedMessages.join(",")}]` : undefined;
40538
41172
  }
40539
41173
  function sha256(value) {
40540
- return createHash14("sha256").update(value).digest("hex");
41174
+ return createHash16("sha256").update(value).digest("hex");
40541
41175
  }
40542
41176
  function safeSessionFileStem(sessionId) {
40543
41177
  const readable = sessionId.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 96);
40544
41178
  return `${readable || "session"}-${sha256(sessionId).slice(0, 12)}`;
40545
41179
  }
40546
41180
  function getPiServedArrayLedgerPath(sessionId, storageDir = getMagicContextStorageDir()) {
40547
- return path12.join(storageDir, LEDGER_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
41181
+ return path13.join(storageDir, LEDGER_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
40548
41182
  }
40549
41183
  function getPiServedArrayBodyPath(sessionId, storageDir = getMagicContextStorageDir()) {
40550
- return path12.join(storageDir, BODY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
41184
+ return path13.join(storageDir, BODY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
40551
41185
  }
40552
41186
  function getPiServedTagNumbersPath(sessionId, storageDir = getMagicContextStorageDir()) {
40553
- return path12.join(storageDir, IDENTITY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
41187
+ return path13.join(storageDir, IDENTITY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
40554
41188
  }
40555
41189
  function fullBodyCaptureEnabled() {
40556
41190
  const value = process.env[PI_SERVED_ARRAY_BODY_CAPTURE_ENV]?.trim().toLowerCase();
@@ -40599,7 +41233,7 @@ function recordWriteFailure(error) {
40599
41233
  }
40600
41234
  function appendPendingLines(filePath, lines) {
40601
41235
  try {
40602
- ensureStorageDirectorySync(path12.dirname(filePath));
41236
+ ensureStorageDirectorySync(path13.dirname(filePath));
40603
41237
  repairZeroTail(filePath);
40604
41238
  writeStorageFileSync(filePath, lines.join(""), {
40605
41239
  encoding: "utf8",
@@ -40706,7 +41340,7 @@ function capturePiServedArray(sessionId, messages, options = {}) {
40706
41340
  const identityPath = getPiServedTagNumbersPath(sessionId, storageDir);
40707
41341
  if (newNumbers.length) {
40708
41342
  identityPersistenceFailed = true;
40709
- ensureStorageDirectorySync(path12.dirname(identityPath));
41343
+ ensureStorageDirectorySync(path13.dirname(identityPath));
40710
41344
  options.assertCurrentPass?.();
40711
41345
  repairZeroTail(identityPath, !!options.db);
40712
41346
  writeStorageFileSync(identityPath, `${JSON.stringify({ version: 1, session_id: sessionId, tag_numbers: newNumbers })}
@@ -40822,7 +41456,7 @@ function readPiServedCachedArray(sessionId) {
40822
41456
  const memory = getPiLastServedArray(sessionId);
40823
41457
  const slot = memory === undefined ? getSlot(sessionId) : undefined;
40824
41458
  const jsonPrefix = memory ?? slot?.jsonPrefix;
40825
- if (jsonPrefix === undefined || createHash15("sha256").update(jsonPrefix).digest("hex") !== getPiLastServedDigest(sessionId))
41459
+ if (jsonPrefix === undefined || createHash17("sha256").update(jsonPrefix).digest("hex") !== getPiLastServedDigest(sessionId))
40826
41460
  return;
40827
41461
  let parsed;
40828
41462
  try {
@@ -40846,7 +41480,7 @@ function piMessageEntryFingerprint(message) {
40846
41480
  const m = message;
40847
41481
  if (typeof m.role !== "string")
40848
41482
  return null;
40849
- const contentHash = createHash16("sha256").update(stableStringify(m.content)).digest("hex");
41483
+ const contentHash = createHash18("sha256").update(stableStringify(m.content)).digest("hex");
40850
41484
  return JSON.stringify([
40851
41485
  typeof m.responseId === "string" ? m.responseId : null,
40852
41486
  typeof m.timestamp === "number" || typeof m.timestamp === "string" ? m.timestamp : null,
@@ -40898,6 +41532,83 @@ function piCachedMessageSurvivor(sessionId, fingerprint, ordinal, rows) {
40898
41532
  return carriers.length === 1 ? winner.tagNumber : undefined;
40899
41533
  }
40900
41534
 
41535
+ // src/pi-overflow-recovery.ts
41536
+ function piRecoveryFitCalibration(calibration) {
41537
+ return calibration.seeded ? calibration : { toolsRatio: UNKNOWN_FIT_RATIO, proseRatio: UNKNOWN_FIT_RATIO };
41538
+ }
41539
+ function piRecoveryToolImageTokens(messages) {
41540
+ const counts = new Map;
41541
+ const seen = new Set;
41542
+ for (const value of messages) {
41543
+ const message = value;
41544
+ if (message?.role !== "toolResult" || !message.toolCallId || !Array.isArray(message.content))
41545
+ continue;
41546
+ if (seen.has(message.toolCallId)) {
41547
+ counts.delete(message.toolCallId);
41548
+ continue;
41549
+ }
41550
+ seen.add(message.toolCallId);
41551
+ for (const part of message.content) {
41552
+ if (part?.type === "image")
41553
+ counts.set(message.toolCallId, (counts.get(message.toolCallId) ?? 0) + estimateTokens(JSON.stringify(part)));
41554
+ }
41555
+ }
41556
+ return counts;
41557
+ }
41558
+ function estimatePiOverflowRecoveryInputTokens(messages, envelope) {
41559
+ if (!envelope || !estimatePiOutgoingInputTokens([], envelope).trusted)
41560
+ return;
41561
+ const raw = tokenizePiMessages([...messages]);
41562
+ for (const value of messages) {
41563
+ if (!value || typeof value !== "object")
41564
+ return;
41565
+ const message = value;
41566
+ if (message.role === "system") {
41567
+ if (typeof message.content === "string")
41568
+ continue;
41569
+ if (!Array.isArray(message.content) || !message.content.every((part) => part?.type === "text" && typeof part.text === "string"))
41570
+ return;
41571
+ continue;
41572
+ }
41573
+ if (!["user", "assistant", "toolResult", "custom"].includes(message.role ?? ""))
41574
+ return;
41575
+ if (typeof message.content === "string")
41576
+ continue;
41577
+ if (!Array.isArray(message.content))
41578
+ return;
41579
+ for (const value of message.content) {
41580
+ if (!value || typeof value !== "object")
41581
+ return;
41582
+ const part = value;
41583
+ if (part.type === "image") {
41584
+ if (typeof part.data !== "string")
41585
+ return;
41586
+ const delta = estimateTokens(JSON.stringify(part)) - PI_IMAGE_TOKENS;
41587
+ if (message.role === "toolResult")
41588
+ raw.toolCall += delta;
41589
+ else
41590
+ raw.conversation += delta;
41591
+ } else if (!["text", "thinking", "redacted_thinking", "toolCall"].includes(part.type ?? ""))
41592
+ return;
41593
+ }
41594
+ }
41595
+ return providerMass({
41596
+ system: envelope.systemTokens,
41597
+ tools: envelope.toolDefinitionTokens + raw.toolCall,
41598
+ prose: raw.conversation
41599
+ }, envelope.calibration ?? calibrationForModelKey(envelope.modelKey), true);
41600
+ }
41601
+ function resolvePiOverflowRecoveryUsage(args) {
41602
+ if (!args.pricedPass || !args.providerOverflow || args.usage.inputTokens > 0)
41603
+ return args.usage;
41604
+ return resolveUnknownUsageFromWireEstimate({
41605
+ usage: args.usage,
41606
+ pricedPass: args.pricedPass,
41607
+ wireEstimateTokens: estimatePiOverflowRecoveryInputTokens(args.messages, args.envelope),
41608
+ usableHardLimit: args.usableHardLimit
41609
+ });
41610
+ }
41611
+
40901
41612
  // src/pi-pressure.ts
40902
41613
  var MAX_UNKNOWN_PI_INPUT_TOKENS = 3000000;
40903
41614
  function piPressureEvidenceLimit(geometry) {
@@ -43526,7 +44237,8 @@ function convertLocatedPiUserEntry(branchEntries, messageId, ordinalOffset = 0)
43526
44237
  const converted = convertEntriesToRawMessages(branchEntries.slice(start, index + 1)).find((message) => message.id === messageId);
43527
44238
  return converted ? { ...converted, ordinal: rawOrdinal } : null;
43528
44239
  }
43529
- pendingToolStart = -1;
44240
+ if (role === "user" || role === "assistant")
44241
+ pendingToolStart = -1;
43530
44242
  }
43531
44243
  return null;
43532
44244
  }
@@ -44894,16 +45606,18 @@ function adoptPiFallbackTags(db, sessionId, tagger, fingerprintById, options = {
44894
45606
  var EMPTY_PI_HISTORIAN_STATE_SNAPSHOT = {
44895
45607
  detectedContextLimit: 0,
44896
45608
  needsEmergencyRecovery: false,
45609
+ emergencyRecoveryOrigin: null,
44897
45610
  historianFailureCount: 0
44898
45611
  };
44899
45612
  function loadPiHistorianStateSnapshot(db, sessionId) {
44900
- const row = db.prepare(`SELECT detected_context_limit, needs_emergency_recovery,
45613
+ const row = db.prepare(`SELECT detected_context_limit, needs_emergency_recovery, emergency_recovery_origin,
44901
45614
  historian_failure_count
44902
45615
  FROM session_meta
44903
45616
  WHERE session_id = ?`).get(sessionId);
44904
45617
  return {
44905
45618
  detectedContextLimit: typeof row?.detected_context_limit === "number" && row.detected_context_limit > 0 ? row.detected_context_limit : 0,
44906
45619
  needsEmergencyRecovery: typeof row?.needs_emergency_recovery === "number" && row.needs_emergency_recovery > 0,
45620
+ emergencyRecoveryOrigin: row?.emergency_recovery_origin === "provider_overflow" || row?.emergency_recovery_origin === "proactive_model_shrink" ? row.emergency_recovery_origin : null,
44907
45621
  historianFailureCount: typeof row?.historian_failure_count === "number" && row.historian_failure_count >= 0 ? row.historian_failure_count : 0
44908
45622
  };
44909
45623
  }
@@ -44926,6 +45640,13 @@ function registerPiContextHandler(pi, baseOptions) {
44926
45640
  }
44927
45641
  return s;
44928
45642
  };
45643
+ pi.on("tool_call", (event, ctx) => {
45644
+ if (event.toolName !== "ctx_reduce")
45645
+ return;
45646
+ const sessionId = resolveSessionId2(ctx);
45647
+ if (sessionId)
45648
+ trackPiDropLeaf(baseOptions.db, sessionId, ctx.sessionManager.getLeafId());
45649
+ });
44929
45650
  pi.on("session_tree", (event, ctx) => {
44930
45651
  if (event.oldLeafId === event.newLeafId)
44931
45652
  return;
@@ -45274,6 +45995,7 @@ function registerPiContextHandler(pi, baseOptions) {
45274
45995
  commitPiCompactionModeRecord(options.db, sessionId, compactionTransition.recordToWrite);
45275
45996
  }
45276
45997
  }
45998
+ const usageResetAfterRefusal = shouldResetRefusedUsage(options.db, sessionId, sessionMetaForUsage);
45277
45999
  const usageReset = {
45278
46000
  lastContextPercentage: 0,
45279
46001
  lastInputTokens: 0,
@@ -45296,8 +46018,8 @@ function registerPiContextHandler(pi, baseOptions) {
45296
46018
  sessionMetaForUsage.lastInputTokens = 0;
45297
46019
  sessionMetaForUsage.observedSafeInputTokens = 0;
45298
46020
  sessionMetaForUsage.cacheAlertSent = false;
45299
- } else if (isFirstContextPassForSession && sessionMetaForUsage.lastContextPercentage > 0) {
45300
- sessionLog2(sessionId, `transform: first pass reset — percentage=${sessionMetaForUsage.lastContextPercentage.toFixed(1)}% tokens=${sessionMetaForUsage.lastInputTokens} — clearing stale usage state`);
46021
+ } else if ((isFirstContextPassForSession || usageResetAfterRefusal) && sessionMetaForUsage.lastContextPercentage > 0) {
46022
+ sessionLog2(sessionId, `transform: ${isFirstContextPassForSession ? "first pass" : "refused turn recovery"} reset — percentage=${sessionMetaForUsage.lastContextPercentage.toFixed(1)}% tokens=${sessionMetaForUsage.lastInputTokens} — clearing stale usage state`);
45301
46023
  updateSessionMeta(options.db, sessionId, {
45302
46024
  lastContextPercentage: 0,
45303
46025
  lastInputTokens: 0
@@ -45312,7 +46034,7 @@ function registerPiContextHandler(pi, baseOptions) {
45312
46034
  usagePercentage = sessionMetaForUsage.lastContextPercentage;
45313
46035
  usageInputTokens = sessionMetaForUsage.lastInputTokens;
45314
46036
  usedPersistedUsage = true;
45315
- } else {
46037
+ } else if (!usageResetAfterRefusal) {
45316
46038
  usagePercentage = typeof piUsage?.percent === "number" ? piUsage.percent : 0;
45317
46039
  usageInputTokens = typeof piUsage?.tokens === "number" ? piUsage.tokens : 0;
45318
46040
  }
@@ -45380,7 +46102,7 @@ function registerPiContextHandler(pi, baseOptions) {
45380
46102
  }
45381
46103
  let fallbackInputTokens;
45382
46104
  const suspectReading = Math.max(usageInputTokens, piUsage?.tokens ?? 0);
45383
- if (suspectReading > providerInputLimit || piProviderUsageWasRejected(sessionId)) {
46105
+ if (usageResetAfterRefusal || suspectReading > providerInputLimit || piProviderUsageWasRejected(sessionId)) {
45384
46106
  if (suspectReading > providerInputLimit)
45385
46107
  noteRejectedPiUsage(sessionId, suspectReading, providerInputLimit, "transform");
45386
46108
  const replay = !lkgCompactionOff && lkgPassSnapshot ? lkgCoordinator.replay(lkgPassSnapshot, (id) => ctx.sessionManager.getEntry?.(id)?.parentId) : undefined;
@@ -45397,7 +46119,7 @@ function registerPiContextHandler(pi, baseOptions) {
45397
46119
  persistedFromLive: !usedPersistedUsage,
45398
46120
  persistedPercentage: usagePercentage,
45399
46121
  persistedInputTokens: usageInputTokens,
45400
- liveInputTokens: piUsage?.tokens,
46122
+ liveInputTokens: usageResetAfterRefusal ? undefined : piUsage?.tokens,
45401
46123
  usableContextLimit: usageContextLimit
45402
46124
  }));
45403
46125
  const realUsagePercentageBeforeEmergencyBump = usagePercentage;
@@ -45527,6 +46249,8 @@ function registerPiContextHandler(pi, baseOptions) {
45527
46249
  maintainExecuteFloorMark(options.db, sessionId, promptId, usagePercentage < effectiveExecuteThresholdPercentage);
45528
46250
  }
45529
46251
  const pipelineArgs = {
46252
+ providerOverflowRecovery: emergencyRecoveryArmed && historianStateForPass.emergencyRecoveryOrigin === "provider_overflow",
46253
+ dropBranchEntries: branchEntries ?? undefined,
45530
46254
  recoveryPrefix: recovery.prefix,
45531
46255
  recoveryRebuildReason: recovery.rebuildReason,
45532
46256
  assertCurrentPass,
@@ -45803,6 +46527,21 @@ function registerPiContextHandler(pi, baseOptions) {
45803
46527
  });
45804
46528
  }
45805
46529
  }
46530
+ if (options.autoSearch && !options.autoSearch.enabled && !options.compactionOff && result.prefixMutationAllowed) {
46531
+ await runAutoSearchHintForPi({
46532
+ sessionId,
46533
+ db: options.db,
46534
+ messages: outputMessages,
46535
+ entryIds: strictEntryIds,
46536
+ entryIdByRef: result.postCommitEntryIdByRef,
46537
+ options: {
46538
+ enabled: false,
46539
+ scoreThreshold: options.autoSearch.scoreThreshold,
46540
+ minPromptChars: options.autoSearch.minPromptChars,
46541
+ projectPath: projectIdentity
46542
+ }
46543
+ });
46544
+ }
45806
46545
  logTransformTiming(sessionId, "autoSearch", tAutoSearch);
45807
46546
  const tTodoCapture = performance.now();
45808
46547
  const injectTodo = () => {
@@ -45887,100 +46626,102 @@ function registerPiContextHandler(pi, baseOptions) {
45887
46626
  if (result.syntheticLeadingCount < 0)
45888
46627
  result.syntheticLeadingCount = prefix.length;
45889
46628
  }
45890
- const tChannelAccounting = performance.now();
45891
- try {
45892
- const sessionMetaForCh1 = sessionMeta;
45893
- if (!options.compactionOff && !sessionMetaForCh1.isSubagent) {
45894
- const { tags, protectedTagNumbers, pendingDropTagNumbers } = result.channelBaselineSnapshot;
45895
- const stableId = (message) => message && typeof message === "object" ? result.postCommitEntryIdByRef.get(message) : undefined;
45896
- const hygieneCalibration = sessionDecisionCalibration(options.db, sessionId);
45897
- const hygieneUnitsVersion = transitionSessionHygieneUnits(options.db, sessionId, result.bustedThisPass, hygieneCalibration);
45898
- const previousBaseline = getPiChannel1Baseline(sessionId);
45899
- const baseline = refreshPiTailHygieneBaseline({
45900
- messages: outputMessages,
45901
- providerApi: ctx.model?.api,
45902
- tags,
45903
- tagCacheKey: result.channelBaselineSnapshot.tagCacheKey,
45904
- protectedTagNumbers,
45905
- protectedTools: options.protectedTools,
45906
- pendingDropTagNumbers,
45907
- stableId,
45908
- protectedToolTags: protectedToolTagNumbers(result.channelBaselineSnapshot.activeTags, options.protectedTools),
45909
- syntheticLeadingCount: result.syntheticLeadingCount,
45910
- cacheBusting: result.bustedThisPass,
45911
- previous: previousBaseline,
45912
- calibration: hygieneUnitsVersion >= HYGIENE_PROVIDER_UNITS_VERSION ? hygieneCalibration : undefined,
45913
- hygieneUnitsVersion
45914
- });
45915
- const effective = effectivePiTailHygiene(baseline);
45916
- if (baseline.lastPrefixMismatch) {
45917
- sessionLog2(sessionId, formatTailHygienePrefixMismatch(baseline.lastPrefixMismatch, baseline.baselineGeneration));
45918
- }
45919
- const durableGrace = baseline.evaluable && !baseline.generationInvalidated ? captureChannel1PostReduceGraceBaseline(options.db, sessionId, effective.u) : getChannel1NudgeState(options.db, sessionId);
45920
- baseline.channel1PostReduceGrace = durableGrace.postReduceGracePending || durableGrace.postReduceGraceBaselineU !== undefined ? {
45921
- pending: durableGrace.postReduceGracePending === true,
45922
- baselineU: durableGrace.postReduceGraceBaselineU,
45923
- preReduceLevel: durableGrace.postReduceGracePreLevel ?? durableGrace.level
45924
- } : undefined;
45925
- const oldestReclaimableToolTags = getOldestActiveUnprotectedToolTags2(options.db, sessionId, protectedTagNumbers);
45926
- const channelState = {
45927
- ...baseline,
45928
- usableWindow: usageContextLimit ?? 0,
45929
- realUserTurnCount: baseline.realUserTurnCount,
45930
- reducedSinceRefresh: false,
45931
- agentDropsAppliedThisPass: result.agentDropsAppliedThisPass,
45932
- oldestReclaimableToolTags
45933
- };
45934
- setPiChannel1Baseline(sessionId, channelState);
45935
- const channel2Evaluation = evaluateChannel2(channelState);
45936
- if (channel2Evaluation.evaluable) {
45937
- try {
45938
- rearmChannel2AfterMeasuredCollapse({
45939
- db: options.db,
45940
- sessionId,
45941
- baseline: channelState,
45942
- rebuilding: result.bustedThisPass,
45943
- previous: previousBaseline
45944
- });
45945
- } catch (error) {
45946
- sessionLog2(sessionId, `pi channel2 U-collapse reset failed: ${error instanceof Error ? error.message : String(error)}`);
46629
+ const publishFinalRequestAccounting = () => {
46630
+ const tChannelAccounting = performance.now();
46631
+ try {
46632
+ const sessionMetaForCh1 = sessionMeta;
46633
+ if (!options.compactionOff && !sessionMetaForCh1.isSubagent) {
46634
+ const { tags, protectedTagNumbers, pendingDropTagNumbers } = result.channelBaselineSnapshot;
46635
+ const stableId = (message) => message && typeof message === "object" ? result.postCommitEntryIdByRef.get(message) : undefined;
46636
+ const hygieneCalibration = sessionDecisionCalibration(options.db, sessionId);
46637
+ const hygieneUnitsVersion = transitionSessionHygieneUnits(options.db, sessionId, result.bustedThisPass, hygieneCalibration);
46638
+ const previousBaseline = getPiChannel1Baseline(sessionId);
46639
+ const baseline = refreshPiTailHygieneBaseline({
46640
+ messages: outputMessages,
46641
+ providerApi: ctx.model?.api,
46642
+ tags,
46643
+ tagCacheKey: result.channelBaselineSnapshot.tagCacheKey,
46644
+ protectedTagNumbers,
46645
+ protectedTools: options.protectedTools,
46646
+ pendingDropTagNumbers,
46647
+ stableId,
46648
+ protectedToolTags: protectedToolTagNumbers(result.channelBaselineSnapshot.activeTags, options.protectedTools),
46649
+ syntheticLeadingCount: result.syntheticLeadingCount,
46650
+ cacheBusting: result.bustedThisPass,
46651
+ previous: previousBaseline,
46652
+ calibration: hygieneUnitsVersion >= HYGIENE_PROVIDER_UNITS_VERSION ? hygieneCalibration : undefined,
46653
+ hygieneUnitsVersion
46654
+ });
46655
+ const effective = effectivePiTailHygiene(baseline);
46656
+ if (baseline.lastPrefixMismatch) {
46657
+ sessionLog2(sessionId, formatTailHygienePrefixMismatch(baseline.lastPrefixMismatch, baseline.baselineGeneration));
45947
46658
  }
45948
- if (channel2Evaluation.shouldTrigger) {
45949
- casChannel2NudgeState(options.db, sessionId, "", "pending");
45950
- } else {
45951
- casChannel2NudgeState(options.db, sessionId, "pending", "");
46659
+ const durableGrace = baseline.evaluable && !baseline.generationInvalidated ? captureChannel1PostReduceGraceBaseline(options.db, sessionId, effective.u) : getChannel1NudgeState(options.db, sessionId);
46660
+ baseline.channel1PostReduceGrace = durableGrace.postReduceGracePending || durableGrace.postReduceGraceBaselineU !== undefined ? {
46661
+ pending: durableGrace.postReduceGracePending === true,
46662
+ baselineU: durableGrace.postReduceGraceBaselineU,
46663
+ preReduceLevel: durableGrace.postReduceGracePreLevel ?? durableGrace.level
46664
+ } : undefined;
46665
+ const oldestReclaimableToolTags = getOldestActiveUnprotectedToolTags2(options.db, sessionId, protectedTagNumbers);
46666
+ const channelState = {
46667
+ ...baseline,
46668
+ usableWindow: usageContextLimit ?? 0,
46669
+ realUserTurnCount: baseline.realUserTurnCount,
46670
+ reducedSinceRefresh: false,
46671
+ agentDropsAppliedThisPass: result.agentDropsAppliedThisPass,
46672
+ oldestReclaimableToolTags
46673
+ };
46674
+ setPiChannel1Baseline(sessionId, channelState);
46675
+ const channel2Evaluation = evaluateChannel2(channelState);
46676
+ if (channel2Evaluation.evaluable) {
46677
+ try {
46678
+ rearmChannel2AfterMeasuredCollapse({
46679
+ db: options.db,
46680
+ sessionId,
46681
+ baseline: channelState,
46682
+ rebuilding: result.bustedThisPass,
46683
+ previous: previousBaseline
46684
+ });
46685
+ } catch (error) {
46686
+ sessionLog2(sessionId, `pi channel2 U-collapse reset failed: ${error instanceof Error ? error.message : String(error)}`);
46687
+ }
46688
+ if (channel2Evaluation.shouldTrigger) {
46689
+ casChannel2NudgeState(options.db, sessionId, "", "pending");
46690
+ } else {
46691
+ casChannel2NudgeState(options.db, sessionId, "pending", "");
46692
+ }
45952
46693
  }
46694
+ assertTailHygieneLastWriter = () => assertPiTailHygieneContentUnchanged({
46695
+ messages: outputMessages,
46696
+ tags,
46697
+ protectedTagNumbers,
46698
+ pendingDropTagNumbers,
46699
+ stableId,
46700
+ syntheticLeadingCount: result.syntheticLeadingCount,
46701
+ expectedSignature: baseline.contentSignature
46702
+ });
46703
+ } else {
46704
+ clearPiChannel1State(sessionId);
45953
46705
  }
45954
- assertTailHygieneLastWriter = () => assertPiTailHygieneContentUnchanged({
45955
- messages: outputMessages,
45956
- tags,
45957
- protectedTagNumbers,
45958
- pendingDropTagNumbers,
45959
- stableId,
45960
- syntheticLeadingCount: result.syntheticLeadingCount,
45961
- expectedSignature: baseline.contentSignature
45962
- });
45963
- } else {
45964
- clearPiChannel1State(sessionId);
46706
+ } catch (err) {
46707
+ const stale = getPiChannel1Baseline(sessionId);
46708
+ if (stale) {
46709
+ stale.evaluable = false;
46710
+ stale.generationInvalidated = true;
46711
+ }
46712
+ sessionLog2(sessionId, `channel1 baseline / channel2 trigger failed: ${err instanceof Error ? err.message : String(err)}`);
45965
46713
  }
45966
- } catch (err) {
45967
- const stale = getPiChannel1Baseline(sessionId);
45968
- if (stale) {
45969
- stale.evaluable = false;
45970
- stale.generationInvalidated = true;
46714
+ logTransformTiming(sessionId, "channelNudgeAccounting", tChannelAccounting);
46715
+ const tWorkMetrics = performance.now();
46716
+ budget.stage = "bookkeeping";
46717
+ try {
46718
+ const metrics = computePiWorkMetrics(outputMessages);
46719
+ setSessionWorkMetrics(options.db, sessionId, metrics.newWorkTokens, metrics.totalInputTokens);
46720
+ } catch (err) {
46721
+ sessionLog2(sessionId, `work-metrics update failed: ${err instanceof Error ? err.message : String(err)}`);
45971
46722
  }
45972
- sessionLog2(sessionId, `channel1 baseline / channel2 trigger failed: ${err instanceof Error ? err.message : String(err)}`);
45973
- }
45974
- logTransformTiming(sessionId, "channelNudgeAccounting", tChannelAccounting);
45975
- const tWorkMetrics = performance.now();
45976
- budget.stage = "bookkeeping";
45977
- try {
45978
- const metrics = computePiWorkMetrics(outputMessages);
45979
- setSessionWorkMetrics(options.db, sessionId, metrics.newWorkTokens, metrics.totalInputTokens);
45980
- } catch (err) {
45981
- sessionLog2(sessionId, `work-metrics update failed: ${err instanceof Error ? err.message : String(err)}`);
45982
- }
45983
- logTransformTiming(sessionId, "workMetrics", tWorkMetrics);
46723
+ logTransformTiming(sessionId, "workMetrics", tWorkMetrics);
46724
+ };
45984
46725
  const tStableIdSchemePersist = performance.now();
45985
46726
  if (stableIdSchemeCutover && !options.compactionOff) {
45986
46727
  persistStableIdSchemeForRun(options.db, sessionId, {
@@ -46064,6 +46805,18 @@ function registerPiContextHandler(pi, baseOptions) {
46064
46805
  throw contextRefusalError("The request remains over the model window after stripping signed thinking and reclaiming context");
46065
46806
  if (getHarness2() === "pi") {
46066
46807
  const fitCase = classifyPiOutgoingFit(estimate, outgoingFitLimit);
46808
+ const recoverUnknownUsage = usageInputTokens <= 0 && result.prefixBustPermitted && emergencyRecoveryArmed && historianStateForPass.emergencyRecoveryOrigin === "provider_overflow";
46809
+ let unknownUsageRecovered = false;
46810
+ const recoveryUsage = resolvePiOverflowRecoveryUsage({
46811
+ usage: pipelineArgs.contextUsage,
46812
+ pricedPass: result.prefixBustPermitted,
46813
+ providerOverflow: recoverUnknownUsage,
46814
+ messages: outputMessages,
46815
+ envelope,
46816
+ usableHardLimit: outgoingFitLimit
46817
+ });
46818
+ if (recoverUnknownUsage && (!(recoveryUsage.inputTokens > 0) || !((emergencyCeilingTokens ?? 0) > 0) || !Number.isFinite(outgoingFitLimit) || !((outgoingFitLimit ?? 0) > 0)))
46819
+ throw contextRefusalError("Provider overflow recovery has unknown-usage: a complete outgoing request estimate and context limit are required before retrying");
46067
46820
  const currentUpperCursor = () => piUpperReclaimCursor({
46068
46821
  sessionId,
46069
46822
  modelKey: resolvePiContextModelKey(ctx) ?? null,
@@ -46073,24 +46826,25 @@ function registerPiContextHandler(pi, baseOptions) {
46073
46826
  snapshot: lkgPassSnapshot,
46074
46827
  input: inputForFitReclaim ?? event.messages
46075
46828
  });
46076
- const upperCursor = fitCase === "upper-estimate-pressure" ? currentUpperCursor() : undefined;
46829
+ const upperCursor = fitCase === "upper-estimate-pressure" && !recoverUnknownUsage ? currentUpperCursor() : undefined;
46077
46830
  if (fitCase === "over-window-proof") {
46078
46831
  requestPiFitReclaimInput(sessionId);
46079
46832
  }
46080
46833
  if (fitCase === "fits-or-unknown")
46081
46834
  clearPiFitReclaimInput(sessionId);
46082
46835
  const upperPassEligible = schedulerDecision === "execute" || result.bustedThisPass || result.prefixRebuildAlreadyPriced || isCacheBusting || forceMaterialization || isEmergency;
46083
- const reclaimHeld = fitCase === "upper-estimate-pressure" && (result.strictThinkingKept || !upperPassEligible || !shouldReclaimPiUpperEstimate(options.db, upperCursor));
46836
+ const reclaimHeld = !recoverUnknownUsage && fitCase === "upper-estimate-pressure" && (result.strictThinkingKept || !upperPassEligible || !shouldReclaimPiUpperEstimate(options.db, upperCursor));
46084
46837
  if (reclaimHeld)
46085
46838
  sessionLog2(sessionId, "outgoing fit: upper-estimate-pressure; reclaim held by signed prefix, deferred pass or used session latch");
46086
46839
  if (fitCase === "upper-estimate-pressure" && !reclaimHeld && !inputForFitReclaim)
46087
46840
  requestPiFitReclaimInput(sessionId);
46088
- if (fitCase !== "fits-or-unknown" && !reclaimHeld && !fitReclaimedThisPass && !inputForFitReclaim)
46841
+ if ((fitCase !== "fits-or-unknown" || recoverUnknownUsage) && !reclaimHeld && !fitReclaimedThisPass && !inputForFitReclaim)
46089
46842
  inputForFitReclaim = restorePiLkgInputForFit(lkgPassSnapshot, branchEntries);
46090
- if (fitCase !== "fits-or-unknown" && !reclaimHeld && !fitReclaimedThisPass && inputForFitReclaim) {
46843
+ if ((fitCase !== "fits-or-unknown" || recoverUnknownUsage) && !reclaimHeld && !fitReclaimedThisPass && inputForFitReclaim) {
46091
46844
  const reservation = upperCursor ? reservePiUpperEstimateReclaim(options.db, upperCursor) : undefined;
46092
46845
  if (!upperCursor || reservation) {
46093
46846
  let reclaimCompleted = false;
46847
+ const recoveryRetryStart = performance.now();
46094
46848
  try {
46095
46849
  sessionLog2(sessionId, `outgoing fit: ${fitCase}; attempting emergency reclaim`);
46096
46850
  if (fitCase === "over-window-proof" && !ctx.hasUI && historian) {
@@ -46133,7 +46887,7 @@ function registerPiContextHandler(pi, baseOptions) {
46133
46887
  schedulerDecision: "execute",
46134
46888
  schedulerDeferReason: null,
46135
46889
  forceMaterialization: true,
46136
- upperEstimateOnly: fitCase === "upper-estimate-pressure",
46890
+ upperEstimateOnly: !recoverUnknownUsage && fitCase === "upper-estimate-pressure",
46137
46891
  forceSignedRebuildForFit: fitCase === "over-window-proof",
46138
46892
  isCacheBusting: historyRefreshSessions.has(sessionId) || deferredHistoryRefreshSessions.has(sessionId),
46139
46893
  injectionPassSnapshot: options.injection ? createPiM0M1PassSnapshot({
@@ -46142,13 +46896,13 @@ function registerPiContextHandler(pi, baseOptions) {
46142
46896
  compactionOff: false,
46143
46897
  sessionMeta: freshMeta
46144
46898
  }) : undefined,
46145
- contextUsage: {
46146
- percentage: usagePercentage,
46147
- inputTokens: usageInputTokens
46148
- }
46899
+ contextUsage: recoverUnknownUsage ? recoveryUsage : pipelineArgs.contextUsage
46149
46900
  }));
46150
46901
  outputMessages = result.messages;
46151
46902
  observePipelineResultForTests(sessionId, result);
46903
+ unknownUsageRecovered = recoverUnknownUsage;
46904
+ if (recoverUnknownUsage)
46905
+ sessionLog2(sessionId, `overflow unknown-usage recovery: inputTokens=${recoveryUsage.inputTokens} retryMs=${(performance.now() - recoveryRetryStart).toFixed(1)}`);
46152
46906
  injectTodo();
46153
46907
  if (upperCursor)
46154
46908
  recordPiUpperEstimateReclaim(options.db, currentUpperCursor() ?? upperCursor, reservation);
@@ -46170,6 +46924,8 @@ function registerPiContextHandler(pi, baseOptions) {
46170
46924
  }
46171
46925
  }
46172
46926
  }
46927
+ if (recoverUnknownUsage && !unknownUsageRecovered)
46928
+ throw contextRefusalError("Provider overflow recovery has unknown-usage: a pristine request is required for emergency reclaim");
46173
46929
  }
46174
46930
  const refusal = outgoingContextRefusal(estimate, rawFallbackLimit, protectedToolTokenCount(getActiveTagsBySession2(options.db, sessionId), options.protectedTools, envelope ? calibrationForModelKey(envelope.modelKey) : undefined));
46175
46931
  if (refusal)
@@ -46177,6 +46933,7 @@ function registerPiContextHandler(pi, baseOptions) {
46177
46933
  if (getHarness2() === "pi" && classifyPiOutgoingFit(estimate, outgoingFitLimit) === "over-window-proof")
46178
46934
  throw new PiDegradedPassError("outgoing-over-window");
46179
46935
  }
46936
+ publishFinalRequestAccounting();
46180
46937
  let serializedOutput;
46181
46938
  let numbersCommitted = false;
46182
46939
  budget.stage = "LKG/served publication";
@@ -46400,6 +47157,8 @@ function registerPiContextHandler(pi, baseOptions) {
46400
47157
  const sessionId = resolveSessionId2(ctx);
46401
47158
  if (!sessionId)
46402
47159
  return;
47160
+ if (!budget.sideTurn)
47161
+ recordUsageRefusal(baseOptions.db, sessionId);
46403
47162
  withoutSqliteTransformPass2(() => setImmediate(() => {
46404
47163
  if (!isCurrent())
46405
47164
  return;
@@ -46486,6 +47245,7 @@ function ensureRunnablePiBoundaryForTests(snapshot) {
46486
47245
  protectedTailStart: endOrdinal,
46487
47246
  eligibleEndOrdinal: endOrdinal,
46488
47247
  trueRawEligibleTokens: Math.max(1, snapshot.trueRawEligibleTokens),
47248
+ summaryRemovedEligibleTokens: Math.max(1, snapshot.summaryRemovedEligibleTokens),
46489
47249
  rawRangeFingerprint: ""
46490
47250
  };
46491
47251
  }
@@ -47091,6 +47851,10 @@ async function runPipeline(args) {
47091
47851
  args.assertCurrentPass?.();
47092
47852
  if (args.compactionOff)
47093
47853
  return runCompactionOffPipeline(args);
47854
+ const dropBranchEntries = args.dropBranchEntries;
47855
+ const dropBranchIds = dropBranchEntries ? piDropBranchIds(dropBranchEntries) : null;
47856
+ trackPiDropLeaf(args.db, args.sessionId, dropBranchIds ? [...dropBranchIds].at(-1) ?? null : null);
47857
+ cancelPiAbandonedPendingDrops(args.db, args.sessionId, dropBranchIds);
47094
47858
  const thinkingRecovery = prepareLatestThinkingRecovery({
47095
47859
  db: args.db,
47096
47860
  sessionId: args.sessionId,
@@ -47521,9 +48285,11 @@ async function runPipeline(args) {
47521
48285
  afterFallbackAdoptionForTests?.(args.stableIdSchemeCutover === true);
47522
48286
  const textIdentityPlan = buildPiTextIdentityPlan(args.db, args.sessionId, args.tagger, transcript, args.reusableMessageIds);
47523
48287
  const tTag = performance.now();
48288
+ const strippedSubagentThinking = args.sessionMeta.isSubagent && (!thinkingRecovery.restore || rewritingSignedPrefix) ? frozenBindingEntryIds(args.db, args.sessionId) : new Set;
47524
48289
  const mutationView = transcript.messages.map((message) => ({
47525
48290
  ...message,
47526
- parts: message.parts
48291
+ parts: message.parts,
48292
+ requiresToolArcSkeleton: strippedSubagentThinking.has(message.info.id ?? "") ? false : message.requiresToolArcSkeleton
47527
48293
  }));
47528
48294
  const recoveryNormalized = !!args.recoveryPrefix && (isCacheBustingPass || identityRebuild);
47529
48295
  const taggingView = args.recoveryPrefix && !recoveryNormalized ? mutationView.filter((message) => !args.recoveryPrefix?.untaggedInputIds.has(message.info.id ?? "")) : mutationView;
@@ -47625,40 +48391,67 @@ async function runPipeline(args) {
47625
48391
  message.content = content;
47626
48392
  });
47627
48393
  }
47628
- const tLegacyToolConversion = performance.now();
47629
- if (foldBustsServedPrefixThisPass) {
47630
- const savedRemovalInputs = nativeRemovalInputs;
47631
- try {
47632
- nativeRemovalInputs = savedRemovalInputs && new Map(savedRemovalInputs);
47633
- let converted = 0;
47634
- args.db.transaction(() => removalBatch((flush) => {
47635
- converted = convertLegacyToolSkeletons(args.db, args.sessionId, targets, {
47636
- prepareRemovals: (rows) => preparePiToolRemovalChecks({
47637
- db: args.db,
47638
- rows,
47639
- targets,
47640
- messages: args.messages,
47641
- prepare: transcript.prepareToolRemovalMeasurements
47642
- }),
47643
- keepFullDrop: (callId) => nativeRemovalInputs === undefined || callId !== null && authorizePiToolRemoval({
47644
- db: args.db,
47645
- sessionId: args.sessionId,
47646
- callId,
47647
- saved: nativeRemovalInputs,
47648
- canApply: false
47649
- }) === true
47650
- }).size;
47651
- flush();
47652
- })).immediate();
47653
- if (converted > 0) {
47654
- sessionLog2(args.sessionId, `pi HARD fold converted ${converted} legacy dropped-tool skeleton(s) to real-or-absent`);
48394
+ const applyExistingToolConversions = () => {
48395
+ const tLegacyToolConversion = performance.now();
48396
+ if (foldBustsServedPrefixThisPass) {
48397
+ const savedRemovalInputs = nativeRemovalInputs;
48398
+ try {
48399
+ nativeRemovalInputs = savedRemovalInputs && new Map(savedRemovalInputs);
48400
+ let converted = 0;
48401
+ args.db.transaction(() => removalBatch((flush) => {
48402
+ converted = convertLegacyToolSkeletons(args.db, args.sessionId, targets, {
48403
+ prepareRemovals: (rows) => preparePiToolRemovalChecks({
48404
+ db: args.db,
48405
+ rows,
48406
+ targets,
48407
+ messages: args.messages,
48408
+ prepare: transcript.prepareToolRemovalMeasurements
48409
+ }),
48410
+ keepFullDrop: (callId) => nativeRemovalInputs === undefined || callId !== null && authorizePiToolRemoval({
48411
+ db: args.db,
48412
+ sessionId: args.sessionId,
48413
+ callId,
48414
+ saved: nativeRemovalInputs,
48415
+ canApply: false
48416
+ }) === true
48417
+ }).size;
48418
+ flush();
48419
+ })).immediate();
48420
+ if (converted > 0) {
48421
+ sessionLog2(args.sessionId, `pi HARD fold converted ${converted} legacy dropped-tool skeleton(s) to real-or-absent`);
48422
+ }
48423
+ } catch (error) {
48424
+ nativeRemovalInputs = savedRemovalInputs;
48425
+ sessionLog2(args.sessionId, `pi legacy dropped-tool skeleton conversion failed (kept legacy bytes): ${error instanceof Error ? error.message : String(error)}`);
47655
48426
  }
47656
- } catch (error) {
47657
- nativeRemovalInputs = savedRemovalInputs;
47658
- sessionLog2(args.sessionId, `pi legacy dropped-tool skeleton conversion failed (kept legacy bytes): ${error instanceof Error ? error.message : String(error)}`);
47659
48427
  }
47660
- }
47661
- logTransformTiming(args.sessionId, "convertLegacyToolSkeletons", tLegacyToolConversion);
48428
+ logTransformTiming(args.sessionId, "convertLegacyToolSkeletons", tLegacyToolConversion);
48429
+ if (args.sessionMeta.isSubagent && isCacheBustingPass && !protectedSignedPrefix && !args.compactionOff) {
48430
+ const savedRemovalInputs = nativeRemovalInputs;
48431
+ try {
48432
+ nativeRemovalInputs = savedRemovalInputs && new Map(savedRemovalInputs);
48433
+ const converted = args.db.transaction(() => removalBatch((flush) => {
48434
+ const conversions = prepareAgedToolSkeletonConversions(args.db, args.sessionId, newTargets, {
48435
+ prepareRemovals: (rows) => preparePiToolRemovalChecks({
48436
+ db: args.db,
48437
+ rows,
48438
+ targets: newTargets,
48439
+ messages: args.messages,
48440
+ prepare: transcript.prepareToolRemovalMeasurements
48441
+ })
48442
+ });
48443
+ conversions.persist(args.db);
48444
+ flush();
48445
+ return conversions.converted.size;
48446
+ })).immediate();
48447
+ if (converted > 0)
48448
+ recordFirstApplicationWireEdit(true);
48449
+ } catch (error) {
48450
+ nativeRemovalInputs = savedRemovalInputs;
48451
+ throw error;
48452
+ }
48453
+ }
48454
+ };
47662
48455
  try {
47663
48456
  if (!args.sessionMeta.isSubagent) {
47664
48457
  const hasRecentCommit = detectRecentCommit(args.messages);
@@ -47675,16 +48468,23 @@ async function runPipeline(args) {
47675
48468
  const deferredMaterializationWasPending = deferredMaterializationSessions.has(args.sessionId);
47676
48469
  const deferredHistoryRefreshWasPending = deferredHistoryWasPendingAtPassStart;
47677
48470
  const shouldReadPendingOps = !args.compactionOff && (publishedWorkDrainAllowed || args.schedulerDecision === "execute" || args.forceMaterialization || hasPendingMaterializeSignal || foldExecutedThisPass || firstRenderBust);
47678
- const pendingOps = shouldReadPendingOps ? getPendingOps(args.db, args.sessionId) : [];
47679
- const tReclaimDistance = performance.now();
47680
- const reclaimDistance = isCacheBustingPass ? toolReclaimDistanceEntries(mutationView, targets) : [];
47681
- logTransformTiming(args.sessionId, "toolReclaimDistanceEntries", tReclaimDistance);
47682
- const protectedToolTags = protectedToolTagNumbers(getActiveTagsBySession2(args.db, args.sessionId), args.protectedTools);
47683
- for (const number of identityAdoption.protectedTagNumbers)
47684
- protectedToolTags.add(number);
47685
- const protectionWindowForPass = getProtectionWindowForSession(args.db, args.sessionId, protectionFloorResolution.floor);
47686
- const protectedTagNumbersForPass = usesTokenProtection ? protectionWindowForPass.protectedTagNumbers : newestActiveTagNumbersByCount(getActiveTagsBySession2(args.db, args.sessionId), args.protectedTags);
47687
- const pendingOperationTags = pendingOps.length > 0 ? getTagsForPendingOperations2(args.db, args.sessionId, pendingOps.map((operation) => operation.tagId), usesTokenProtection ? 0 : args.protectedTags, RECENT_TOOL_SKELETON_WINDOW) : [];
48471
+ const readPendingDropState = () => {
48472
+ const pendingOps = shouldReadPendingOps ? getPendingOps(args.db, args.sessionId) : [];
48473
+ const protectedToolTags = protectedToolTagNumbers(getActiveTagsBySession2(args.db, args.sessionId), args.protectedTools);
48474
+ for (const number of identityAdoption.protectedTagNumbers)
48475
+ protectedToolTags.add(number);
48476
+ const protectionWindowForPass = getProtectionWindowForSession(args.db, args.sessionId, protectionFloorResolution.floor);
48477
+ const protectedTagNumbersForPass = usesTokenProtection ? protectionWindowForPass.protectedTagNumbers : newestActiveTagNumbersByCount(getActiveTagsBySession2(args.db, args.sessionId), args.protectedTags);
48478
+ const pendingOperationTags = pendingOps.length > 0 ? getTagsForPendingOperations2(args.db, args.sessionId, pendingOps.map((operation) => operation.tagId), usesTokenProtection ? 0 : args.protectedTags, RECENT_TOOL_SKELETON_WINDOW) : [];
48479
+ return {
48480
+ pendingOps,
48481
+ protectedToolTags,
48482
+ protectionWindowForPass,
48483
+ protectedTagNumbersForPass,
48484
+ pendingOperationTags
48485
+ };
48486
+ };
48487
+ const executeReclaimSnapshot = readPendingDropState();
47688
48488
  if (!args.sessionMeta.isSubagent && args.schedulerDecision === "execute") {
47689
48489
  args.evaluateHistorianBeforeReclaim?.({
47690
48490
  activeTags: getActiveTagsBySession2(args.db, args.sessionId),
@@ -47701,12 +48501,12 @@ async function runPipeline(args) {
47701
48501
  reclaimableTokens: estimateExecuteReclaimTokens({
47702
48502
  db: args.db,
47703
48503
  sessionId: args.sessionId,
47704
- pendingOps,
47705
- tags: pendingOperationTags,
48504
+ pendingOps: executeReclaimSnapshot.pendingOps,
48505
+ tags: executeReclaimSnapshot.pendingOperationTags,
47706
48506
  targets: newTargets,
47707
48507
  protectedTagIds: new Set([
47708
- ...protectedTagNumbersForPass,
47709
- ...protectedToolTags
48508
+ ...executeReclaimSnapshot.protectedTagNumbersForPass,
48509
+ ...executeReclaimSnapshot.protectedToolTags
47710
48510
  ]),
47711
48511
  probePreparation: (targets) => {
47712
48512
  const saved = nativeRemovalInputs;
@@ -47742,6 +48542,23 @@ async function runPipeline(args) {
47742
48542
  stripUnheldSignedTurnOnBust();
47743
48543
  applyTemporalOnRide();
47744
48544
  }
48545
+ if (reconcilePiDropBranches(args.db, args.sessionId, dropBranchIds, isCacheBustingPass && !protectedSignedPrefix)) {
48546
+ args.tagger.cleanup(args.sessionId);
48547
+ args.tagger.initFromDb(args.sessionId, args.db);
48548
+ nativeRemovalInputs = getNativeReplayState(args.db, args.sessionId).toolInputs;
48549
+ recordFirstApplicationWireEdit(true);
48550
+ }
48551
+ applyExistingToolConversions();
48552
+ const {
48553
+ pendingOps,
48554
+ protectedToolTags,
48555
+ protectionWindowForPass,
48556
+ protectedTagNumbersForPass,
48557
+ pendingOperationTags
48558
+ } = readPendingDropState();
48559
+ const tReclaimDistance = performance.now();
48560
+ const reclaimDistance = isCacheBustingPass ? toolReclaimDistanceEntries(mutationView, targets) : [];
48561
+ logTransformTiming(args.sessionId, "toolReclaimDistanceEntries", tReclaimDistance);
47745
48562
  const deferredMaterialize = canConsumeDeferredLate && deferredMaterializationWasPending;
47746
48563
  const deferredHistoryRefresh = canConsumeDeferredLate && deferredHistoryRefreshWasPending;
47747
48564
  const shouldApplyPendingOps = publishedWorkDrainAllowed;
@@ -47882,6 +48699,8 @@ async function runPipeline(args) {
47882
48699
  if (shouldRunHeuristics && args.heuristics) {
47883
48700
  try {
47884
48701
  const tHeuristic = performance.now();
48702
+ const providerOverflow = args.providerOverflowRecovery === true;
48703
+ const recoveryCalibration = sessionDecisionCalibration(args.db, args.sessionId);
47885
48704
  const independentMutationBeforeHeuristics = pendingOpsDidMutate || systemPrefixChangedThisPass || foldBustsServedPrefixThisPass || identityRebuild || firstRenderBust || heldIdleExpiryRebuild;
47886
48705
  if (args.forceMaterialization === true && independentMutationBeforeHeuristics && getEmergencyInputSample(args.db, args.sessionId) > 0) {
47887
48706
  clearEmergencyDropSample(args.db, args.sessionId);
@@ -47899,7 +48718,9 @@ async function runPipeline(args) {
47899
48718
  currentTotalInputTokens: args.contextUsage.inputTokens,
47900
48719
  ceilingTokens: args.emergencyCeilingTokens,
47901
48720
  usagePercentage: args.contextUsage.percentage,
47902
- passAlreadyPriced: independentMutationBeforeHeuristics
48721
+ passAlreadyPriced: independentMutationBeforeHeuristics,
48722
+ serializedImageTokens: providerOverflow ? piRecoveryToolImageTokens(workingMessages) : undefined,
48723
+ fitCalibration: providerOverflow ? piRecoveryFitCalibration(recoveryCalibration) : undefined
47903
48724
  } : undefined,
47904
48725
  caveman: args.isSubagent ? undefined : args.heuristics?.caveman
47905
48726
  }, activeTags, stableIdResolver));
@@ -48175,6 +48996,13 @@ async function runPipeline(args) {
48175
48996
  const tTranscriptCommit = performance.now();
48176
48997
  transcript.commit();
48177
48998
  logTransformTiming(args.sessionId, "transcriptCommit", tTranscriptCommit);
48999
+ if (applyNativeResponseTagReplayPi({
49000
+ db: args.db,
49001
+ sessionId: args.sessionId,
49002
+ messages: args.messages,
49003
+ canApply: isCacheBustingPass
49004
+ }) > 0)
49005
+ recordFirstApplicationWireEdit(true);
48178
49006
  let nativeInputs;
48179
49007
  let nativeReasoningIds;
48180
49008
  try {