@cortexkit/pi-magic-context 0.49.35 → 0.49.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/auto-search-worker.js +163 -160
  2. package/dist/chunk-embedding-write-worker.js +59 -57
  3. package/dist/{compaction-marker-candidates-hk2frhac.js → compaction-marker-candidates-9fzqg6y0.js} +4 -2
  4. package/dist/compaction-marker-candidates-worker.js +24 -23
  5. package/dist/{compartment-chunk-embedding-5j1d1e3v.js → compartment-chunk-embedding-jpcme6tz.js} +6 -5
  6. package/dist/{embedding-backfill-jt459ady.js → embedding-backfill-002vyexq.js} +8 -7
  7. package/dist/{index-399p688g.js → index-8sqebjzk.js} +4 -1
  8. package/dist/{index-11knwv2h.js → index-a7sj2bam.js} +6 -3
  9. package/dist/{index-mkh1cm5k.js → index-c1dqxr10.js} +34 -4
  10. package/dist/{index-rgvqh5x7.js → index-ck8wj35v.js} +1 -1
  11. package/dist/{index-fzm8fm4z.js → index-dgqjq8sg.js} +143 -64
  12. package/dist/{index-yg42q9bc.js → index-f6y7mdq9.js} +8 -7
  13. package/dist/{index-2p691ayw.js → index-f9qet7y8.js} +1 -1
  14. package/dist/{index-6y6v2wvn.js → index-gxqmh9hq.js} +6 -6
  15. package/dist/{index-vgfk2xa5.js → index-jbkwszd5.js} +1 -1
  16. package/dist/{index-8m82cez2.js → index-kj49vs41.js} +2 -2
  17. package/dist/{index-svqk0701.js → index-mx757f3g.js} +13 -5
  18. package/dist/index-nypgv96r.js +58 -0
  19. package/dist/{index-d21rhpbd.js → index-p9jfa33t.js} +5 -5
  20. package/dist/{index-yhsmjvqa.js → index-pwp6zyx3.js} +12 -1
  21. package/dist/{index-m68qfjte.js → index-ry84c8dr.js} +1 -1
  22. package/dist/index-wfqysrwq.js +204 -0
  23. package/dist/{index-0rvn6vhq.js → index-wwsfevck.js} +6 -3
  24. package/dist/{index-54wmg4rj.js → index-x2n261wp.js} +16 -14
  25. package/dist/{index-d0w84hy5.js → index-y90fxz4t.js} +9 -3
  26. package/dist/{index-a11nmw5t.js → index-z7dktknj.js} +6 -4
  27. package/dist/index.js +846 -305
  28. package/dist/{message-fts-rowid-map-aw4xa05a.js → message-fts-rowid-map-hmgyfxxa.js} +5 -4
  29. package/dist/{message-index-bounded-da34vhs4.js → message-index-bounded-82sergz7.js} +8 -7
  30. package/dist/migration-worker.js +46 -41
  31. package/dist/{project-embedding-registry-davzvsz9.js → project-embedding-registry-16pj4vyf.js} +7 -6
  32. package/dist/raw-ordinal-warmup-worker.js +114 -118
  33. package/dist/{read-session-chunk-nbcznexm.js → read-session-chunk-jxq427v6.js} +6 -5
  34. package/dist/{read-session-raw-zb8kpa2y.js → read-session-raw-a9ntjqap.js} +4 -3
  35. package/dist/{render-d931g2vv.js → render-2wan3bfh.js} +11 -10
  36. package/dist/{search-atp7g95j.js → search-3p18trsb.js} +12 -11
  37. package/dist/{search-archive-a76pp30g.js → search-archive-pgrv04gv.js} +8 -7
  38. package/dist/{storage-tags-jy8b0xsy.js → storage-tags-p9y3bnw7.js} +1 -1
  39. package/dist/{store-kabezr25.js → store-v60q7dje.js} +1 -2
  40. package/dist/subagent-entry.js +18 -17
  41. package/dist/tag-snapshot-worker-pi.js +208 -202
  42. package/dist/{v2-archive-reader-wcjcfdzq.js → v2-archive-reader-5zpj8hrg.js} +3 -3
  43. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -150,7 +150,7 @@ import {
150
150
  registerTodoStateLifecycle,
151
151
  syncCtxMemoryToolEnabled,
152
152
  registerMagicContextTools
153
- } from "./index-fzm8fm4z.js";
153
+ } from "./index-dgqjq8sg.js";
154
154
  import"./index-4knyh1kt.js";
155
155
  import {
156
156
  canonicalModelIdentity,
@@ -353,7 +353,7 @@ import {
353
353
  getRawSessionTagKeysThrough2,
354
354
  getLegacyProtectedTailStartOrdinal2,
355
355
  readSessionChunk2
356
- } from "./index-svqk0701.js";
356
+ } from "./index-mx757f3g.js";
357
357
  import {
358
358
  UNKNOWN_FIT_RATIO,
359
359
  hasMeasuredDecisionCalibration,
@@ -440,7 +440,7 @@ import {
440
440
  getToolTagNumberByOwner2,
441
441
  getNullOwnerToolTag2,
442
442
  adoptNullOwnerToolTag2
443
- } from "./index-yhsmjvqa.js";
443
+ } from "./index-pwp6zyx3.js";
444
444
  import {
445
445
  textMentionsRecentCommit,
446
446
  hasMeaningfulUserText2,
@@ -490,7 +490,7 @@ import {
490
490
  openCurrentDatabase,
491
491
  openDatabaseAsync,
492
492
  closeDatabase
493
- } from "./index-11knwv2h.js";
493
+ } from "./index-a7sj2bam.js";
494
494
  import {
495
495
  LkgRecoveryFenceError,
496
496
  clearLkgFenceProcessState,
@@ -607,7 +607,7 @@ import {
607
607
  getSourceContents,
608
608
  recordSubagentInvocation,
609
609
  getLatestHistorianInvocationId
610
- } from "./index-d21rhpbd.js";
610
+ } from "./index-p9jfa33t.js";
611
611
  import {
612
612
  resolveOpenCodeDbPath,
613
613
  assertOpenCodeStoreGeneration,
@@ -616,18 +616,21 @@ import {
616
616
  clearOpenCodeDbReadFailure,
617
617
  claimOpenCodeDbDiagnosticOnce
618
618
  } from "./index-n0zjvs3h.js";
619
- import"./index-399p688g.js";
619
+ import"./index-8sqebjzk.js";
620
+ import {
621
+ reuseWorker
622
+ } from "./index-wfqysrwq.js";
620
623
  import {
621
624
  createWorkerShutdown
622
- } from "./index-54wmg4rj.js";
625
+ } from "./index-x2n261wp.js";
623
626
  import {
624
627
  RAW_SUMMARY_TEXT_MAX_CHARS2
625
- } from "./index-mkh1cm5k.js";
628
+ } from "./index-c1dqxr10.js";
626
629
  import"./index-ea37qn4x.js";
627
630
  import {
628
631
  formatSynapseLaneDescriptor
629
632
  } from "./index-rzfr7wdv.js";
630
- import"./index-rgvqh5x7.js";
633
+ import"./index-ck8wj35v.js";
631
634
  import {
632
635
  buildCanonicalChunkTextFromFts2,
633
636
  buildCompartmentSummaryFallbackText2,
@@ -637,7 +640,7 @@ import {
637
640
  replaceCompartmentChunkEmbeddings2,
638
641
  deleteChunkEmbedBackoffForSession2,
639
642
  chunkWindowSourceKey2
640
- } from "./index-2p691ayw.js";
643
+ } from "./index-f9qet7y8.js";
641
644
  import {
642
645
  recordSessionProjectIdentity,
643
646
  GIT_SWEEP_LEASE_RENEWAL_MS,
@@ -662,7 +665,7 @@ import {
662
665
  clearCompressionDepthRange,
663
666
  cleanSearchText,
664
667
  matchedSearchSnippet
665
- } from "./index-8m82cez2.js";
668
+ } from "./index-kj49vs41.js";
666
669
  import {
667
670
  rememberV1HiddenRun
668
671
  } from "./index-6x556c2q.js";
@@ -726,6 +729,7 @@ import {
726
729
  getMemoriesByProjects2,
727
730
  getMaxMemoryIdForProjects2,
728
731
  getMemoryById2,
732
+ updateMemoryVerification2,
729
733
  setMemoryClassification2,
730
734
  archiveMemory2,
731
735
  getMemoryCountsByStatus2
@@ -756,10 +760,10 @@ import {
756
760
  embedSessionCompartmentChunks2,
757
761
  getEmbeddingCoverageStatus2,
758
762
  getAutoEmbeddingSessionCoverage2
759
- } from "./index-a11nmw5t.js";
763
+ } from "./index-z7dktknj.js";
760
764
  import {
761
765
  cosineSimilarity
762
- } from "./index-vgfk2xa5.js";
766
+ } from "./index-jbkwszd5.js";
763
767
  import {
764
768
  USER_FACING_FAILURES,
765
769
  renderUserFacingFailure,
@@ -793,8 +797,8 @@ import {
793
797
  scheduleClearAndReindex,
794
798
  clearSessionTracking,
795
799
  createAutoSearchDiagnostics
796
- } from "./index-yg42q9bc.js";
797
- import"./index-d0w84hy5.js";
800
+ } from "./index-f6y7mdq9.js";
801
+ import"./index-y90fxz4t.js";
798
802
  import {
799
803
  DREAMER_PRIMER_INVESTIGATOR_ALLOWED_TOOLS,
800
804
  buildHiddenAgentRegistrations,
@@ -821,11 +825,11 @@ import {
821
825
  drainPiTagSnapshotSession,
822
826
  assertPiTagSnapshotReady,
823
827
  ensurePiTagSnapshotReady
824
- } from "./index-0rvn6vhq.js";
828
+ } from "./index-wwsfevck.js";
825
829
  import"./index-cq71g4dk.js";
826
830
  import"./index-1cvyaw9a.js";
827
- import"./index-6y6v2wvn.js";
828
- import"./index-m68qfjte.js";
831
+ import"./index-gxqmh9hq.js";
832
+ import"./index-ry84c8dr.js";
829
833
  import {
830
834
  __toESM
831
835
  } from "./index-22jkk6wg.js";
@@ -3580,7 +3584,7 @@ function startHostStallProfilerSwitch() {
3580
3584
  // package.json
3581
3585
  var package_default = {
3582
3586
  name: "@cortexkit/pi-magic-context",
3583
- version: "0.49.35",
3587
+ version: "0.49.36",
3584
3588
  type: "module",
3585
3589
  description: "Pi and OMP coding agent extension for Magic Context — cross-session memory and context management",
3586
3590
  main: "dist/index.js",
@@ -3699,6 +3703,158 @@ var package_default = {
3699
3703
 
3700
3704
  // src/clone-inheritance.ts
3701
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
3702
3858
  var CONTENT_ID_SUFFIX = /:(?:p|file)\d+$/;
3703
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;
3704
3860
  var loggedCloneStartFailures = new Set;
@@ -3883,9 +4039,14 @@ async function inheritPiCloneSession(event, ctx, deps, isStartup) {
3883
4039
  throw new Error("Pi clone branch is unavailable");
3884
4040
  }
3885
4041
  stage = "copy-state";
4042
+ const filter = createCloneFilter(branchEntries);
3886
4043
  const result = copySessionStateForClone(deps.db, sourceSessionId, destinationSessionId, {
3887
- ...createCloneFilter(branchEntries),
3888
- requireEmptyDestinationMeta: isStartup
4044
+ ...filter,
4045
+ requireEmptyDestinationMeta: isStartup,
4046
+ selectPendingPiMarker: (raw, compartments) => {
4047
+ copyPiDropBranches(deps.db, sourceSessionId, destinationSessionId);
4048
+ return filter.selectPendingPiMarker(raw, compartments);
4049
+ }
3889
4050
  });
3890
4051
  if (result.kind === "destination-not-empty") {
3891
4052
  writeLog(`[magic-context][pi] clone-inheritance: skipped source=${sourceSessionId} dest=${destinationSessionId} stage=destination-guard reason=destination-not-empty`);
@@ -4189,12 +4350,17 @@ function countCompartmentsSince(db, projectPath, since) {
4189
4350
  return row?.cnt ?? 0;
4190
4351
  }
4191
4352
  function countProjectSessionsSince(db, projectPath, since) {
4192
- 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
4193
- LEFT JOIN schema_migrations_meta activity
4194
- ON activity.key = 'retrospective_activity:' || sp.session_id
4195
- LEFT JOIN message_history_index history ON history.session_id = sp.session_id
4196
- WHERE sp.project_path = ?
4197
- 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);
4198
4364
  return row?.cnt ?? 0;
4199
4365
  }
4200
4366
  function countMappedMemories(db, projectPath) {
@@ -4207,6 +4373,10 @@ function countMappedMemories(db, projectPath) {
4207
4373
  AND v.file_path <> ''`).get(projectPath, Date.now());
4208
4374
  return row?.cnt ?? 0;
4209
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)`;
4210
4380
  function countUnverifiedMappedMemories(db, projectPath) {
4211
4381
  const row = db.prepare(`SELECT COUNT(DISTINCT m.id) AS cnt
4212
4382
  FROM memories m
@@ -4215,7 +4385,8 @@ function countUnverifiedMappedMemories(db, projectPath) {
4215
4385
  AND m.status IN ('active','permanent')
4216
4386
  AND (m.expires_at IS NULL OR m.expires_at > ?)
4217
4387
  AND v.file_path <> ''
4218
- 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());
4219
4390
  return row?.cnt ?? 0;
4220
4391
  }
4221
4392
  function countBroadCycleCandidates(db, projectPath, cycleStartAt) {
@@ -4224,6 +4395,7 @@ function countBroadCycleCandidates(db, projectPath, cycleStartAt) {
4224
4395
  WHERE m.project_path = ?
4225
4396
  AND m.status IN ('active','permanent')
4226
4397
  AND (m.expires_at IS NULL OR m.expires_at > ?)
4398
+ AND NOT (${CURRENT_VERIFY_SKIP_SQL})
4227
4399
  AND (
4228
4400
  SELECT MAX(v.verified_at)
4229
4401
  FROM memory_verifications v
@@ -4332,7 +4504,7 @@ function getDreamTaskBacklog(db, projectPath, task, options = {}) {
4332
4504
  case "verify-broad": {
4333
4505
  const total = countMappedMemories(db, projectPath);
4334
4506
  const cycleStartAt = getTaskScheduleState(db, projectPath, "verify-broad")?.lastBroadRunAt;
4335
- const pending = cycleStartAt == null ? total : countBroadCycleCandidates(db, projectPath, cycleStartAt);
4507
+ const pending = countBroadCycleCandidates(db, projectPath, cycleStartAt ?? Number.MAX_SAFE_INTEGER);
4336
4508
  return { pending, total };
4337
4509
  }
4338
4510
  case "curate": {
@@ -4459,6 +4631,7 @@ function getDreamTaskBacklogs(db, projectPath, tasks = CANONICAL_DREAM_TASKS, op
4459
4631
  }
4460
4632
  const optionKey = (value) => value === undefined ? "omitted" : value === null ? "null" : `number:${value}`;
4461
4633
  const key = JSON.stringify([
4634
+ getHarness2(),
4462
4635
  projectPath,
4463
4636
  tasks,
4464
4637
  optionKey(options.lastRunAt),
@@ -4548,7 +4721,7 @@ function openOpenCodeDb() {
4548
4721
  }
4549
4722
 
4550
4723
  // ../plugin/src/features/magic-context/dreamer/task-executor.ts
4551
- import { createHash as createHash10 } from "node:crypto";
4724
+ import { createHash as createHash11 } from "node:crypto";
4552
4725
  import { existsSync as existsSync9, readFileSync as readFileSync8 } from "node:fs";
4553
4726
 
4554
4727
  // ../plugin/src/agents/dreamer.ts
@@ -4571,7 +4744,7 @@ var DREAMER_REVIEWER_AGENT = "dreamer-reviewer";
4571
4744
  // ../plugin/src/features/magic-context/schema-fence-probe.ts
4572
4745
  var STALE_CHILD_SPAWN_FAILURE = "stale_schema_fence";
4573
4746
  var STALE_CHILD_SPAWN_LATCH_THRESHOLD = 2;
4574
- var state = {
4747
+ var state2 = {
4575
4748
  consecutiveFailures: 0,
4576
4749
  totalFailures: 0,
4577
4750
  latched: false,
@@ -4579,23 +4752,23 @@ var state = {
4579
4752
  failure: null
4580
4753
  };
4581
4754
  function recordStaleFence(persistedVersion, supportedVersion, reason = "newer_schema") {
4582
- state.consecutiveFailures += 1;
4583
- state.totalFailures += 1;
4584
- const latched = state.consecutiveFailures >= STALE_CHILD_SPAWN_LATCH_THRESHOLD;
4585
- 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;
4586
4759
  const failure = {
4587
4760
  failureClass: STALE_CHILD_SPAWN_FAILURE,
4588
4761
  reason,
4589
4762
  persistedVersion,
4590
4763
  supportedVersion,
4591
- consecutiveFailures: state.consecutiveFailures,
4592
- totalFailures: state.totalFailures,
4593
- latched: state.latched
4764
+ consecutiveFailures: state2.consecutiveFailures,
4765
+ totalFailures: state2.totalFailures,
4766
+ latched: state2.latched
4594
4767
  };
4595
- state.failure = failure;
4596
- const shouldSurface = state.latched && !state.noticeEmitted;
4768
+ state2.failure = failure;
4769
+ const shouldSurface = state2.latched && !state2.noticeEmitted;
4597
4770
  if (shouldSurface)
4598
- state.noticeEmitted = true;
4771
+ state2.noticeEmitted = true;
4599
4772
  return { allowSpawn: false, failure, shouldSurface };
4600
4773
  }
4601
4774
  function probeChildSpawnFence(db) {
@@ -4614,9 +4787,9 @@ function probeChildSpawnFence(db) {
4614
4787
  } catch {
4615
4788
  return recordStaleFence(LATEST_SUPPORTED_VERSION, LATEST_SUPPORTED_VERSION, "read_error");
4616
4789
  }
4617
- state.consecutiveFailures = 0;
4618
- state.latched = false;
4619
- state.noticeEmitted = false;
4790
+ state2.consecutiveFailures = 0;
4791
+ state2.latched = false;
4792
+ state2.noticeEmitted = false;
4620
4793
  return { allowSpawn: true };
4621
4794
  }
4622
4795
 
@@ -5811,6 +5984,18 @@ function messageId(message) {
5811
5984
  const id = infoOf(message).id;
5812
5985
  return typeof id === "string" ? id : null;
5813
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
+ }
5814
5999
  function isSettledAssistant(message) {
5815
6000
  const info = infoOf(message);
5816
6001
  if (info.role !== "assistant")
@@ -5926,6 +6111,22 @@ async function promptAsyncAndWaitForIdle(client, request, options = {}) {
5926
6111
  let completionBaseline = new Set((await readMessages(session, sessionId, dir, signal)).map(messageId).filter((id) => id !== null));
5927
6112
  const initialBaseline = new Set(completionBaseline);
5928
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
+ };
5929
6130
  try {
5930
6131
  let sent;
5931
6132
  try {
@@ -5960,22 +6161,17 @@ async function promptAsyncAndWaitForIdle(client, request, options = {}) {
5960
6161
  await sleep(pollIntervalMs, signal);
5961
6162
  const hostError = activeSessionErrors.get(sessionId);
5962
6163
  if (hostError !== undefined) {
5963
- const error = new Error(`Host recorded session error: ${describeAssistantError(hostError)}`);
5964
- Object.assign(error, {
5965
- name: "DreamerProviderOutputFailureError",
5966
- transient: true,
5967
- [HOST_SESSION_ERROR]: true,
5968
- hostError
5969
- });
5970
- annotateSubagentInvocationError(error, {
5971
- attemptedModel: requestModelRef(request),
5972
- modelConfigKey: options.modelConfigKey,
5973
- hostError
5974
- });
5975
- throw error;
6164
+ await failForHostError(hostError);
5976
6165
  }
5977
6166
  const status = await readStatus(session, sessionId, dir, signal);
5978
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);
5979
6175
  if (!sawOutput && messages.some((message) => {
5980
6176
  const id = messageId(message);
5981
6177
  return id !== null && !initialBaseline.has(id) && showsModelOutput(message);
@@ -9580,6 +9776,7 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
9580
9776
  async attempt(handle, request) {
9581
9777
  if (!client)
9582
9778
  throw new Error("Hidden completion client is unavailable");
9779
+ hostErrors.delete(handle.id);
9583
9780
  const identity = runIdentities.get(handle.id);
9584
9781
  const requestedModel = request.body?.model;
9585
9782
  const requestedRef = requestedModel && typeof requestedModel.providerID === "string" && typeof requestedModel.modelID === "string" ? `${requestedModel.providerID}/${requestedModel.modelID}` : undefined;
@@ -9622,9 +9819,9 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
9622
9819
  });
9623
9820
  const assistantFailure = extractLatestAssistantFailure(messages);
9624
9821
  const hostError = hostErrors.get(handle.id);
9625
- if (assistantFailure) {
9626
- const finish = assistantFailure.finish ? ` (finish=${assistantFailure.finish})` : "";
9627
- 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 });
9628
9825
  annotateSubagentInvocationError(error, {
9629
9826
  attemptedModel: handle.attemptedModel,
9630
9827
  modelConfigKey: handle.modelConfigKey,
@@ -16603,7 +16800,7 @@ function insertDreamRun(db, run) {
16603
16800
  }
16604
16801
 
16605
16802
  // ../plugin/src/features/magic-context/dreamer/verify.ts
16606
- import { createHash as createHash9 } from "node:crypto";
16803
+ import { createHash as createHash10 } from "node:crypto";
16607
16804
 
16608
16805
  // ../plugin/src/features/magic-context/dreamer/verify-diff.ts
16609
16806
  import { execFile as execFile3 } from "node:child_process";
@@ -16724,7 +16921,56 @@ Read only these changed line ranges if they may affect the claim.`
16724
16921
  }
16725
16922
 
16726
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";
16727
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
16728
16974
  function minOf(values) {
16729
16975
  return values.reduce((acc, v) => v < acc ? v : acc, Number.POSITIVE_INFINITY);
16730
16976
  }
@@ -16756,7 +17002,22 @@ async function partitionVerifyScope(args) {
16756
17002
  const runStartedAt = args.now ?? Date.now();
16757
17003
  const active = getMemoriesByProject2(args.db, args.projectIdentity);
16758
17004
  const verById = getMemoryVerifications(args.db, active.map((m) => m.id));
16759
- 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
+ }
16760
17021
  const toPrompt = (m) => {
16761
17022
  const verifiedAt = verById.get(m.id)?.verifiedAt ?? 0;
16762
17023
  let verifiedCommit = null;
@@ -16783,7 +17044,7 @@ async function partitionVerifyScope(args) {
16783
17044
  leaseKey: args.leaseKey,
16784
17045
  runStartedAt
16785
17046
  });
16786
- 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) => {
16787
17048
  const verifiedAtA = verById.get(a.id)?.verifiedAt ?? 0;
16788
17049
  const verifiedAtB = verById.get(b.id)?.verifiedAt ?? 0;
16789
17050
  return verifiedAtA - verifiedAtB || a.id - b.id;
@@ -16794,7 +17055,10 @@ async function partitionVerifyScope(args) {
16794
17055
  mode: "broad",
16795
17056
  inScope: broadCandidates.map(toPrompt),
16796
17057
  inScopeIds: broadCandidates.map((m) => m.id),
16797
- 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
+ ],
16798
17062
  broadCycleStartAt,
16799
17063
  reason: `broad cycle (${broadCandidates.length} remain; started ${broadCycleStartAt})`
16800
17064
  };
@@ -16805,7 +17069,7 @@ async function partitionVerifyScope(args) {
16805
17069
  mode: "incremental",
16806
17070
  inScope: [],
16807
17071
  inScopeIds: [],
16808
- skippedIds: [],
17072
+ skippedIds: unchangedSkippedIds,
16809
17073
  reason: "no file-mapped memories in scope"
16810
17074
  };
16811
17075
  }
@@ -16814,11 +17078,11 @@ async function partitionVerifyScope(args) {
16814
17078
  mode,
16815
17079
  inScope: candidates.map(toPrompt),
16816
17080
  inScopeIds: candidates.map((m) => m.id),
16817
- skippedIds: [],
17081
+ skippedIds: unchangedSkippedIds,
16818
17082
  reason
16819
17083
  });
16820
17084
  const resolvedGitRoot = await resolveGitTopLevel(args.projectDirectory);
16821
- const gitRoot = resolvedGitRoot ?? path11.resolve(args.projectDirectory);
17085
+ const gitRoot = resolvedGitRoot ?? path12.resolve(args.projectDirectory);
16822
17086
  const verifiedTimes = candidates.map((m) => verById.get(m.id)?.verifiedAt ?? 0).filter((t) => t > 0);
16823
17087
  const sinceMs = verifiedTimes.length > 0 ? minOf(verifiedTimes) : runStartedAt;
16824
17088
  const changeTimes = await readGitFileChangeTimesSince(args.projectDirectory, sinceMs, resolvedGitRoot ?? undefined);
@@ -16828,11 +17092,11 @@ async function partitionVerifyScope(args) {
16828
17092
  const head = await readGitHead(args.projectDirectory);
16829
17093
  const uncommitted = head ? await readGitChangedFilesSince(args.projectDirectory, head, resolvedGitRoot ?? undefined) ?? new Set : new Set;
16830
17094
  const inScope = [];
16831
- const skippedIds = [];
17095
+ const skippedIds = [...unchangedSkippedIds];
16832
17096
  for (const m of candidates) {
16833
17097
  const v = verById.get(m.id);
16834
17098
  const verifiedAt = v?.verifiedAt ?? 0;
16835
- if (verifiedAt === 0) {
17099
+ if (verifiedAt === 0 || changedSkippedIds.has(m.id)) {
16836
17100
  inScope.push(toPrompt(m));
16837
17101
  continue;
16838
17102
  }
@@ -17202,6 +17466,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17202
17466
  let budgetFinalized = false;
17203
17467
  let failedCompletion;
17204
17468
  let headAtPrompt = null;
17469
+ const skipSnapshots = new Map;
17205
17470
  const onBudgetUpdate = (state) => {
17206
17471
  budgetFinalized ||= state.finalizeFired;
17207
17472
  args.onBudgetUpdate?.({
@@ -17218,6 +17483,11 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17218
17483
  });
17219
17484
  };
17220
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
+ }
17221
17491
  headAtPrompt = await raceWithSignal(readGitHead(args.sessionDirectory), batchSignal);
17222
17492
  const evidence = !args.forceBroad && batch.every((m) => m.verifiedAt) ? await raceWithSignal(buildVerifyDiffEvidence(args.sessionDirectory, batch), batchSignal) : null;
17223
17493
  const prompt = buildVerifyPrompt(args.projectIdentity, await raceWithSignal(seedVerifyCandidates(batch, args.sessionDirectory), batchSignal), evidence);
@@ -17259,7 +17529,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17259
17529
  tokens: run.completion.usage
17260
17530
  });
17261
17531
  reportUsage(run.completion.messages ?? [], run.childSessionId, run.completion.usage.input + (run.completion.usage.cacheRead ?? 0) + (run.completion.usage.cacheWrite ?? 0));
17262
- return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
17532
+ return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized, skipSnapshots);
17263
17533
  }
17264
17534
  const client = args.client;
17265
17535
  if (!client)
@@ -17319,7 +17589,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17319
17589
  promptSettled = true;
17320
17590
  recordInvocation4(args, startedAt, { status: "completed", messages: run.output });
17321
17591
  reportUsage(run.output, agentSessionId);
17322
- return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
17592
+ return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized, skipSnapshots);
17323
17593
  } catch (error) {
17324
17594
  const desc = describeError(error);
17325
17595
  const providerFailure = error instanceof DreamerProviderOutputFailureError ? error : undefined;
@@ -17339,7 +17609,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17339
17609
  if (interrupted) {
17340
17610
  const text = verifyAssistantText(messages) ?? failedCompletion?.text ?? "";
17341
17611
  const recovered = parsePartialVerifyManifest(text, new Set(batch.map((m) => m.id)));
17342
- const counts = await applyParsedVerifyManifest(args, batch, recovered, headAtPrompt, true);
17612
+ const counts = await applyParsedVerifyManifest(args, batch, recovered, headAtPrompt, true, skipSnapshots);
17343
17613
  return {
17344
17614
  ...counts,
17345
17615
  timedOut: !(error instanceof DreamTokenBudgetExceeded),
@@ -17371,7 +17641,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
17371
17641
  signal.removeEventListener("abort", abort);
17372
17642
  }
17373
17643
  }
17374
- async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budgetFinalized = false) {
17644
+ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budgetFinalized = false, skipSnapshots) {
17375
17645
  const batchIds = new Set(batch.map((m) => m.id));
17376
17646
  const batchById = new Map(batch.map((memory) => [memory.id, memory]));
17377
17647
  const valid = {
@@ -17450,6 +17720,15 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17450
17720
  }
17451
17721
  writes.push({ kind: "archive", id: a.id, reason: a.reason });
17452
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
+ }
17453
17732
  let verified = 0;
17454
17733
  let updated = 0;
17455
17734
  let archived = 0;
@@ -17479,7 +17758,7 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17479
17758
  name: "memory.set_verification",
17480
17759
  arguments: {
17481
17760
  memory_project: args.projectIdentity,
17482
- 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)}`,
17483
17762
  rows
17484
17763
  }
17485
17764
  }
@@ -17511,9 +17790,17 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17511
17790
  continue;
17512
17791
  if (w.kind === "verify") {
17513
17792
  recordMemoryVerifications(args.db, w.id, w.files, now);
17514
- if (verifiedHead)
17515
- 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);
17516
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);
17517
17804
  } else if (w.kind === "update") {
17518
17805
  const duplicate = getMemoryByHash2(args.db, memory.projectPath, memory.category, w.hash);
17519
17806
  if (duplicate && duplicate.id !== memory.id) {
@@ -17522,6 +17809,8 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17522
17809
  continue;
17523
17810
  }
17524
17811
  rewriteMemoryContent(args.db, memory, w.content, w.hash);
17812
+ updateMemoryVerification2(args.db, w.id, "unverified");
17813
+ recordVerificationVerdict(args.db, memory, "update", now);
17525
17814
  queueMemoryMutation(args.db, {
17526
17815
  projectPath: args.projectIdentity,
17527
17816
  mutationType: "update",
@@ -17532,6 +17821,8 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
17532
17821
  updated += 1;
17533
17822
  } else {
17534
17823
  archiveMemory2(args.db, w.id, w.reason);
17824
+ updateMemoryVerification2(args.db, w.id, "stale");
17825
+ recordVerificationVerdict(args.db, memory, "archive", now);
17535
17826
  queueMemoryMutation(args.db, {
17536
17827
  projectPath: args.projectIdentity,
17537
17828
  mutationType: "archive",
@@ -18325,7 +18616,7 @@ function parseFrictionGateVerdict(verdict) {
18325
18616
  }
18326
18617
  function computeRetrospectiveWindowKey(flagged) {
18327
18618
  const anchors = flagged.map((message) => `${message.sessionId}:${message.ts}`).sort().join("|");
18328
- return createHash10("sha256").update(anchors).digest("hex").slice(0, 32);
18619
+ return createHash11("sha256").update(anchors).digest("hex").slice(0, 32);
18329
18620
  }
18330
18621
  function renderFrictionWindow(messages, flaggedOrdinals, radius = 2) {
18331
18622
  const flagged = new Set(flaggedOrdinals);
@@ -20425,16 +20716,17 @@ function resolvePiInvocation(options = {}) {
20425
20716
  const platform = options.platform ?? process.platform;
20426
20717
  const env = options.env ?? process.env;
20427
20718
  const resolvePackageJson = options.resolvePackageJson ?? defaultResolvePiPackageJson;
20719
+ const scriptRuntime = isGenericRuntimeExecutable(execPath) ? execPath : "node";
20428
20720
  const running = resolvePiCliFromRunningEntry(currentScript, fs);
20429
20721
  const checkedPaths = [...running.checkedPaths];
20430
20722
  if (running.cliPath) {
20431
20723
  return {
20432
- command: execPath,
20724
+ command: scriptRuntime,
20433
20725
  prefixArgs: [running.cliPath],
20434
20726
  targetHarness: running.targetHarness ?? piHarnessKindFromExecutable(running.cliPath) ?? resolvePiHarnessKind()
20435
20727
  };
20436
20728
  }
20437
- if (!isGenericRuntimeExecutable(execPath)) {
20729
+ if (piHarnessKindFromExecutable(execPath) !== undefined || !isGenericRuntimeExecutable(execPath) && currentScript?.startsWith(BUN_VIRTUAL_SCRIPT_PREFIX)) {
20438
20730
  return {
20439
20731
  command: execPath,
20440
20732
  prefixArgs: [],
@@ -20444,7 +20736,7 @@ function resolvePiInvocation(options = {}) {
20444
20736
  const bundled = resolveBundledPiCli(currentScript, fs, resolvePackageJson, checkedPaths);
20445
20737
  if (bundled.cliPath) {
20446
20738
  return {
20447
- command: execPath,
20739
+ command: scriptRuntime,
20448
20740
  prefixArgs: [bundled.cliPath],
20449
20741
  targetHarness: bundled.targetHarness ?? piHarnessKindFromExecutable(bundled.cliPath) ?? resolvePiHarnessKind()
20450
20742
  };
@@ -25696,7 +25988,9 @@ function resolveProtectedTailBoundary(ctx) {
25696
25988
  const offset = Math.max(1, ctx.lastCompartmentEndOrdinal + 1);
25697
25989
  const absoluteMessageCount = getCachedAbsoluteMessageCount2(ctx.sessionId) ?? getRawSessionMessageOrdinalCount2(ctx.sessionId);
25698
25990
  const messages = readRawSessionMessageRange2(ctx.sessionId, Math.max(1, offset - 1), absoluteMessageCount);
25699
- 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;
25700
25994
  const index = buildTrueRawTokenIndex(ctx.sessionId, messages, {
25701
25995
  providerShapeVersion: ctx.providerShapeVersion,
25702
25996
  cacheNamespace: ctx.cacheNamespace,
@@ -25707,6 +26001,14 @@ function resolveProtectedTailBoundary(ctx) {
25707
26001
  return v === undefined ? null : v;
25708
26002
  } : undefined
25709
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
+ });
25710
26012
  const rawMessageCount = index.rawMessageCount;
25711
26013
  const usagePercentage = clampPercentage(ctx.usage?.percentage ?? 0);
25712
26014
  const usageInputTokens = Math.max(0, Math.round(ctx.usage?.inputTokens ?? 0));
@@ -25738,6 +26040,7 @@ function resolveProtectedTailBoundary(ctx) {
25738
26040
  createdAt,
25739
26041
  rawRangeFingerprint: "",
25740
26042
  trueRawEligibleTokens: 0,
26043
+ summaryRemovedEligibleTokens: 0,
25741
26044
  oversizeAtomicUnit: false,
25742
26045
  boundaryReason: "empty-session"
25743
26046
  };
@@ -25781,6 +26084,7 @@ function resolveProtectedTailBoundary(ctx) {
25781
26084
  createdAt,
25782
26085
  rawRangeFingerprint,
25783
26086
  trueRawEligibleTokens: index.rangeTokens(offset, protectedTailStart),
26087
+ summaryRemovedEligibleTokens: summaryRemovedIndex.rangeTokens(offset, protectedTailStart),
25784
26088
  oversizeAtomicUnit: false,
25785
26089
  boundaryReason: firstOpenArc ? "open-tool-arc" : "manual-full-recomp"
25786
26090
  };
@@ -25857,7 +26161,7 @@ function resolveProtectedTailBoundary(ctx) {
25857
26161
  observeDiagnostics: (value) => {
25858
26162
  headDiagnostics = value;
25859
26163
  },
25860
- index,
26164
+ index: summaryRemovedIndex,
25861
26165
  protectedTailStart,
25862
26166
  offset,
25863
26167
  arcs,
@@ -25896,6 +26200,7 @@ function resolveProtectedTailBoundary(ctx) {
25896
26200
  createdAt,
25897
26201
  rawRangeFingerprint,
25898
26202
  trueRawEligibleTokens: index.rangeTokens(offset, eligibleEndOrdinal),
26203
+ summaryRemovedEligibleTokens: summaryRemovedIndex.rangeTokens(offset, eligibleEndOrdinal),
25899
26204
  oversizeAtomicUnit: head.oversizeAtomicUnit,
25900
26205
  boundaryReason,
25901
26206
  diagnostics: boundaryDiagnostics({
@@ -25943,8 +26248,11 @@ function resolveBoundaryContext(args) {
25943
26248
  const calibration = sessionDecisionCalibration(args.db, args.sessionId);
25944
26249
  const storedTokenTotalsForMessages = (messageIds, messages = []) => {
25945
26250
  try {
25946
- const totals = getAllStatusTagTokenTotalsFlat2(args.db, args.sessionId, args.taggerFloor ?? 0, calibration, messageIds).totals;
25947
- 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
+ };
25948
26256
  } catch (error) {
25949
26257
  sessionLog2(args.sessionId, "protected-tail stored-token map unavailable (live fallback):", error);
25950
26258
  return;
@@ -25980,7 +26288,9 @@ function resolveWrapupProtectedTailBoundary(args) {
25980
26288
  const offset = Math.max(1, ctx.lastCompartmentEndOrdinal + 1);
25981
26289
  const absoluteMessageCount = getCachedAbsoluteMessageCount2(ctx.sessionId) ?? getRawSessionMessageOrdinalCount2(ctx.sessionId);
25982
26290
  const messages = readRawSessionMessageRange2(ctx.sessionId, Math.max(1, offset - 1), absoluteMessageCount);
25983
- 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;
25984
26294
  const index = buildTrueRawTokenIndex(ctx.sessionId, messages, {
25985
26295
  providerShapeVersion: ctx.providerShapeVersion,
25986
26296
  cacheNamespace: ctx.cacheNamespace,
@@ -25991,6 +26301,14 @@ function resolveWrapupProtectedTailBoundary(args) {
25991
26301
  return value === undefined ? null : value;
25992
26302
  } : undefined
25993
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
+ });
25994
26312
  const rawMessageCount = index.rawMessageCount;
25995
26313
  const anchorRawMessageCount = Math.max(0, Math.min(rawMessageCount, Math.floor(args.anchorRawMessageCount ?? rawMessageCount)));
25996
26314
  const usagePercentage = clampPercentage(ctx.usage?.percentage ?? 0);
@@ -26052,7 +26370,7 @@ function resolveWrapupProtectedTailBoundary(args) {
26052
26370
  observeDiagnostics: (value) => {
26053
26371
  headDiagnostics = value;
26054
26372
  },
26055
- index,
26373
+ index: summaryRemovedIndex,
26056
26374
  protectedTailStart: targetProtectedTailStart,
26057
26375
  offset,
26058
26376
  arcs: buildToolArcs(messages),
@@ -26089,6 +26407,7 @@ function resolveWrapupProtectedTailBoundary(args) {
26089
26407
  createdAt,
26090
26408
  rawRangeFingerprint,
26091
26409
  trueRawEligibleTokens: index.rangeTokens(offset, targetProtectedTailStart),
26410
+ summaryRemovedEligibleTokens: summaryRemovedIndex.rangeTokens(offset, eligibleEndOrdinal),
26092
26411
  oversizeAtomicUnit: head.oversizeAtomicUnit,
26093
26412
  boundaryReason,
26094
26413
  diagnostics: boundaryDiagnostics({
@@ -27513,6 +27832,52 @@ ${typeof state?.output === "string" ? state.output : JSON.stringify(state?.outpu
27513
27832
  };
27514
27833
  }
27515
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
+
27516
27881
  // ../plugin/src/hooks/magic-context/transform-stage-logger.ts
27517
27882
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
27518
27883
  var SLOW_STAGE_MS = 100;
@@ -27570,7 +27935,7 @@ function recoveryReasoningPrefixes(messages, partsKey) {
27570
27935
  }
27571
27936
 
27572
27937
  // ../plugin/src/shared/tag-transcript.ts
27573
- import { createHash as createHash11 } from "node:crypto";
27938
+ import { createHash as createHash12 } from "node:crypto";
27574
27939
  function buildPersistedDropTargets(transcript, tags, options = {}) {
27575
27940
  const targets = new Map;
27576
27941
  const tools = new Map(tags.filter((tag) => tag.type === "tool").map((tag) => [makeToolCompositeKey(tag.toolOwnerMessageId ?? "", tag.messageId), tag]));
@@ -27653,7 +28018,7 @@ function buildPersistedDropTargets(transcript, tags, options = {}) {
27653
28018
  return targets;
27654
28019
  }
27655
28020
  function textIdentityDigest(value) {
27656
- return createHash11("sha256").update(value).digest("hex");
28021
+ return createHash12("sha256").update(value).digest("hex");
27657
28022
  }
27658
28023
  function buildContentDerivedTextIds(messageId, parts) {
27659
28024
  const sources = parts.filter((part) => part.kind === "text").map((part) => stripTagPrefix(part.getText() ?? ""));
@@ -28781,7 +29146,7 @@ async function acquireSearchWorker(db, key, entry, input, signal) {
28781
29146
  }
28782
29147
  const fresh = !slot;
28783
29148
  if (!slot) {
28784
- 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 } }));
28785
29150
  const shutdown = createWorkerShutdown(worker, "auto-search", AUTO_SEARCH_WORKER_CLOSE_DEADLINE_MS);
28786
29151
  slot = {
28787
29152
  worker,
@@ -28859,6 +29224,7 @@ function queueAutoSearchBackfill(db, sessionId, projectPath, query) {
28859
29224
  pending.set(key, work);
28860
29225
  return work;
28861
29226
  }
29227
+ var nextSearchJobId = 0;
28862
29228
  async function executeAutoSearchWorker(db, sessionId, projectPath, query, options, entry, job) {
28863
29229
  if (options.signal?.aborted)
28864
29230
  return [];
@@ -28879,6 +29245,7 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
28879
29245
  const signal = deadlineSignal ? AbortSignal.any([deadlineSignal, lifecycle.signal]) : lifecycle.signal;
28880
29246
  const snapshot = getProjectEmbeddingSnapshot2(projectPath);
28881
29247
  const input = {
29248
+ jobId: ++nextSearchJobId,
28882
29249
  job,
28883
29250
  path,
28884
29251
  harness: getHarness2(),
@@ -28998,6 +29365,8 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
28998
29365
  const onMessage = async (reply) => {
28999
29366
  if (released)
29000
29367
  return;
29368
+ if (entry.href === defaultAutoSearchWorkerEntry().href && reply.jobId !== input.jobId)
29369
+ return;
29001
29370
  if (reply.kind === "diagnostics") {
29002
29371
  if (!settled && options.autoSearchDiagnostics) {
29003
29372
  const workerStarted = options.autoSearchDiagnostics.workerStarted;
@@ -29025,7 +29394,7 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
29025
29394
  }
29026
29395
  if (reply.kind === "messages" || reply.kind === "accepted")
29027
29396
  return;
29028
- const response = { id: reply.id };
29397
+ const response = { id: reply.id, jobId: input.jobId };
29029
29398
  if (!signal.aborted) {
29030
29399
  const requestedAt = performance.now();
29031
29400
  try {
@@ -29534,7 +29903,7 @@ function persistFilteredNoise(db, sessionId, chunk, eligibleEnd) {
29534
29903
  }
29535
29904
 
29536
29905
  // src/pi-history-retirement.ts
29537
- var KEY = "piHistorianRetirement";
29906
+ var KEY2 = "piHistorianRetirement";
29538
29907
  function readState(db, sessionId) {
29539
29908
  const row = db.prepare("SELECT deferred_execute_state AS state FROM session_meta WHERE session_id = ?").get(sessionId);
29540
29909
  const state = row?.state ? JSON.parse(row.state) : {};
@@ -29543,24 +29912,24 @@ function readState(db, sessionId) {
29543
29912
  return state;
29544
29913
  }
29545
29914
  function readPiHistoryRetirement(db, sessionId) {
29546
- const target = readState(db, sessionId)[KEY];
29915
+ const target = readState(db, sessionId)[KEY2];
29547
29916
  return typeof target === "string" && target.length > 0 ? target : null;
29548
29917
  }
29549
29918
  function queuePiHistoryRetirement(db, sessionId, throughMessageId) {
29550
29919
  ensureSessionMetaRow(db, sessionId);
29551
29920
  const state = readState(db, sessionId);
29552
- state[KEY] = throughMessageId;
29921
+ state[KEY2] = throughMessageId;
29553
29922
  db.prepare("UPDATE session_meta SET deferred_execute_state = ? WHERE session_id = ?").run(JSON.stringify(state), sessionId);
29554
29923
  }
29555
29924
  function acknowledgePiHistoryRetirement(db, sessionId, publishedEnd, ordinalOf) {
29556
29925
  const state = readState(db, sessionId);
29557
- const target = state[KEY];
29926
+ const target = state[KEY2];
29558
29927
  if (typeof target !== "string")
29559
29928
  return;
29560
29929
  const ordinal = ordinalOf(target);
29561
29930
  if (ordinal === undefined || ordinal > publishedEnd)
29562
29931
  return;
29563
- delete state[KEY];
29932
+ delete state[KEY2];
29564
29933
  db.prepare("UPDATE session_meta SET deferred_execute_state = ? WHERE session_id = ?").run(JSON.stringify(state), sessionId);
29565
29934
  }
29566
29935
 
@@ -30089,7 +30458,7 @@ async function runPiHistorian(deps) {
30089
30458
  db,
30090
30459
  sessionId,
30091
30460
  runId: crypto2.randomUUID(),
30092
- trueRawTokens: boundarySnapshot.trueRawEligibleTokens,
30461
+ trueRawTokens: boundarySnapshot.summaryRemovedEligibleTokens,
30093
30462
  usagePercentage: boundarySnapshot.usagePercentage,
30094
30463
  usable,
30095
30464
  perRunCap,
@@ -32134,12 +32503,28 @@ function applyPiHeuristicCleanup(sessionId, db, targets, piMessages, config, pre
32134
32503
  const priorInputSample = getEmergencyInputSample(db, sessionId);
32135
32504
  const candidateTags = tags.filter((t) => t.status === "active" && t.type === "tool" && targets.get(t.tagNumber)?.canDrop?.());
32136
32505
  const recentTags = new Set(candidateTags.slice().sort((a, b) => b.tagNumber - a.tagNumber).slice(0, 20).map((tag) => tag.tagNumber));
32137
- const calibration = sessionDecisionCalibration(db, sessionId);
32506
+ const calibration = emergency.fitCalibration ?? sessionDecisionCalibration(db, sessionId);
32138
32507
  const tPrepare = performance.now();
32139
32508
  config.prepareToolRemovalMeasurements?.(tags.flatMap((tag) => tag.status === "active" && tag.type === "tool" && tag.messageId && targets.get(tag.tagNumber)?.measureReclaim ? [tag.messageId] : []));
32140
32509
  logStep("prepareNativeRemovals", tPrepare);
32141
32510
  const tMeasure = performance.now();
32142
- 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
+ });
32143
32528
  const byTag = new Map(activeTags.map((tag) => [tag.tagNumber, tag]));
32144
32529
  const droppableTags = candidateTags.flatMap((tag) => {
32145
32530
  const measured = byTag.get(tag.tagNumber);
@@ -35441,10 +35826,10 @@ function authorizePiToolRemoval(args) {
35441
35826
  }
35442
35827
 
35443
35828
  // src/native-replay-tag-provenance-pi.ts
35444
- import { createHash as createHash12 } from "node:crypto";
35829
+ import { createHash as createHash13 } from "node:crypto";
35445
35830
  var PI_NATIVE_TAG_STRIP_PROVENANCE = "magicContextNativeTagStrips";
35446
35831
  function nativeResponseTagStripKey(id, phase, text) {
35447
- return createHash12("sha256").update(JSON.stringify([id, phase, text])).digest("hex");
35832
+ return createHash13("sha256").update(JSON.stringify([id, phase, text])).digest("hex");
35448
35833
  }
35449
35834
  function ownsNativeResponseTagStrip(decisions, id, phase, text) {
35450
35835
  if (!isRecord(decisions) || phase !== undefined && phase !== "commentary" && phase !== "final_answer")
@@ -36869,7 +37254,7 @@ function piWithoutDateReminders(payload, api, cwd, developerRole, includesPrepar
36869
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;
36870
37255
  if (piDateReminderText(text, cwd)) {
36871
37256
  const keys = google ? ["role", "parts"] : api.endsWith("responses") ? ["role", "content", "type"] : ["role", "content"];
36872
- 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";
36873
37258
  if (!firstUser || row.role !== role || Object.keys(row).some((key) => !keys.includes(key)) || row.type !== undefined && row.type !== "message")
36874
37259
  return;
36875
37260
  if (!removed) {
@@ -37126,6 +37511,14 @@ function piResponsesAssistantTexts(value, model, host, alternatives, frozenTagSt
37126
37511
  const payload = object10(message.providerPayload);
37127
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))
37128
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 [];
37129
37522
  const replay = [];
37130
37523
  let bound = 0;
37131
37524
  let output = false;
@@ -38224,13 +38617,16 @@ function countAnthropicOptionalAlignments(plan, base, wire, compare, reminders,
38224
38617
  function piPreparedStructureMismatch(payload, prepared, onImageTrim) {
38225
38618
  const plan = prepared && dateReminderPlans.get(prepared);
38226
38619
  try {
38227
- const messages = plan ? object11(payload)?.messages : undefined;
38620
+ const body = plan ? object11(payload) : undefined;
38621
+ const messages = body?.messages ?? body?.input;
38228
38622
  if (Array.isArray(messages)) {
38229
38623
  for (const value of messages) {
38230
38624
  const message = object11(value);
38231
38625
  if (message?.role !== "system" && message?.role !== "developer")
38232
38626
  break;
38233
- 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))
38234
38630
  return "comparison=role";
38235
38631
  }
38236
38632
  }
@@ -40297,9 +40693,9 @@ function createPiFallbackIndex() {
40297
40693
  }
40298
40694
 
40299
40695
  // src/pi-fit-reclaim.ts
40300
- import { createHash as createHash13, randomUUID as randomUUID4 } from "node:crypto";
40696
+ import { createHash as createHash14, randomUUID as randomUUID4 } from "node:crypto";
40301
40697
  var pendingInput = new Set;
40302
- var KEY2 = "piUpperEstimateReclaim";
40698
+ var KEY3 = "piUpperEstimateReclaim";
40303
40699
  function piFitOverflowPossible(args) {
40304
40700
  if (!args.hasUI && args.inputTokens <= 0 || args.recoveryArmed || args.forceMaterialization || args.imported || args.coldInjection || args.cacheBusting || pendingInput.has(args.sessionId))
40305
40701
  return true;
@@ -40333,7 +40729,7 @@ function piUpperReclaimCursor(args) {
40333
40729
  sessionId: args.sessionId,
40334
40730
  modelKey: args.modelKey,
40335
40731
  contextWindow: args.contextWindow ?? null,
40336
- head: head ? createHash13("sha256").update(JSON.stringify([
40732
+ head: head ? createHash14("sha256").update(JSON.stringify([
40337
40733
  Buffer.from(head.cachedM0Bytes ?? []).toString("base64"),
40338
40734
  Buffer.from(head.cachedM1Bytes ?? []).toString("base64"),
40339
40735
  head.cachedM0MuralDataUrl ?? null
@@ -40351,18 +40747,18 @@ function canReclaim(previous, cursor) {
40351
40747
  function shouldReclaimPiUpperEstimate(db, cursor) {
40352
40748
  if (!cursor)
40353
40749
  return false;
40354
- return canReclaim(claimFrom(readReplayEnvelope(db, cursor.sessionId)[KEY2], cursor.sessionId), cursor);
40750
+ return canReclaim(claimFrom(readReplayEnvelope(db, cursor.sessionId)[KEY3], cursor.sessionId), cursor);
40355
40751
  }
40356
40752
  function reservePiUpperEstimateReclaim(db, cursor) {
40357
40753
  const reservation = randomUUID4();
40358
40754
  return db.transaction(() => {
40359
40755
  let claimed = false;
40360
40756
  if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
40361
- claimed = canReclaim(claimFrom(doc[KEY2], cursor.sessionId), cursor);
40757
+ claimed = canReclaim(claimFrom(doc[KEY3], cursor.sessionId), cursor);
40362
40758
  if (!claimed)
40363
40759
  return false;
40364
40760
  doc.version = 2;
40365
- doc[KEY2] = { ...cursor, reservation };
40761
+ doc[KEY3] = { ...cursor, reservation };
40366
40762
  return true;
40367
40763
  }))
40368
40764
  throw new Error("Could not reserve upper-estimate reclaim");
@@ -40374,10 +40770,10 @@ function recordPiUpperEstimateReclaim(db, cursor, reservation) {
40374
40770
  if (!owner)
40375
40771
  return;
40376
40772
  if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
40377
- if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== owner)
40773
+ if (claimFrom(doc[KEY3], cursor.sessionId)?.reservation !== owner)
40378
40774
  return false;
40379
40775
  doc.version = 2;
40380
- doc[KEY2] = cursor;
40776
+ doc[KEY3] = cursor;
40381
40777
  return true;
40382
40778
  }))
40383
40779
  throw new Error("Could not persist upper-estimate reclaim cursor");
@@ -40386,19 +40782,19 @@ function recordPiUpperEstimateReclaim(db, cursor, reservation) {
40386
40782
  function releasePiUpperEstimateReclaim(db, cursor, reservation) {
40387
40783
  withoutSqliteTransformPass2(() => withSqliteTurnDeadline2(() => 0, () => {
40388
40784
  if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
40389
- if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== reservation)
40785
+ if (claimFrom(doc[KEY3], cursor.sessionId)?.reservation !== reservation)
40390
40786
  return false;
40391
- delete doc[KEY2];
40787
+ delete doc[KEY3];
40392
40788
  return true;
40393
40789
  }))
40394
40790
  throw new Error("Could not release upper-estimate reclaim");
40395
40791
  }));
40396
40792
  }
40397
40793
  function resetPiUpperEstimateReclaim(db, sessionId) {
40398
- if (!claimFrom(readReplayEnvelope(db, sessionId)[KEY2], sessionId))
40794
+ if (!claimFrom(readReplayEnvelope(db, sessionId)[KEY3], sessionId))
40399
40795
  return;
40400
40796
  if (!updateReplayDocument(db, sessionId, (doc) => {
40401
- delete doc[KEY2];
40797
+ delete doc[KEY3];
40402
40798
  return true;
40403
40799
  }))
40404
40800
  throw new Error("Could not clear upper-estimate reclaim cursor");
@@ -40420,7 +40816,7 @@ async function waitForPiHistorianInTurn(sessionId, remainingMs, wait) {
40420
40816
  }
40421
40817
 
40422
40818
  // src/pi-lkg-fit-envelope.ts
40423
- import { createHash as createHash14 } from "node:crypto";
40819
+ import { createHash as createHash15 } from "node:crypto";
40424
40820
  function readPiLkgFitEnvelope(ctx, pi, modelKey, frozen, wireSchema) {
40425
40821
  try {
40426
40822
  if (!modelKey || typeof pi.getAllTools !== "function")
@@ -40482,7 +40878,7 @@ function readPiLkgFitEnvelope(ctx, pi, modelKey, frozen, wireSchema) {
40482
40878
  refusalToolDefinitionTokens,
40483
40879
  toolDefinitionsMeasured: refusalToolDefinitionTokens !== undefined,
40484
40880
  envelopeBytes: Buffer.byteLength(envelopeJson),
40485
- envelopeSignature: activeNames ? createHash14("sha256").update(envelopeJson).update(JSON.stringify(activeNames)).digest("hex") : undefined,
40881
+ envelopeSignature: activeNames ? createHash15("sha256").update(envelopeJson).update(JSON.stringify(activeNames)).digest("hex") : undefined,
40486
40882
  calibration
40487
40883
  };
40488
40884
  } catch {
@@ -40491,13 +40887,13 @@ function readPiLkgFitEnvelope(ctx, pi, modelKey, frozen, wireSchema) {
40491
40887
  }
40492
40888
 
40493
40889
  // src/pi-message-identity.ts
40494
- import { createHash as createHash17 } from "node:crypto";
40890
+ import { createHash as createHash18 } from "node:crypto";
40495
40891
 
40496
40892
  // src/pi-tool-identity.ts
40497
- import { createHash as createHash16 } from "node:crypto";
40893
+ import { createHash as createHash17 } from "node:crypto";
40498
40894
 
40499
40895
  // src/served-array-ledger.ts
40500
- import { createHash as createHash15 } from "node:crypto";
40896
+ import { createHash as createHash16 } from "node:crypto";
40501
40897
  import {
40502
40898
  closeSync as closeSync2,
40503
40899
  fstatSync,
@@ -40507,7 +40903,7 @@ import {
40507
40903
  readSync as readSync2,
40508
40904
  statSync as statSync6
40509
40905
  } from "node:fs";
40510
- import * as path12 from "node:path";
40906
+ import * as path13 from "node:path";
40511
40907
 
40512
40908
  // src/pi-served-identities.ts
40513
40909
  class PiServedEnvelopeUnavailable extends Error {
@@ -40775,20 +41171,20 @@ function getPiLastServedArray(sessionId) {
40775
41171
  return previous ? `[${previous.serializedMessages.join(",")}]` : undefined;
40776
41172
  }
40777
41173
  function sha256(value) {
40778
- return createHash15("sha256").update(value).digest("hex");
41174
+ return createHash16("sha256").update(value).digest("hex");
40779
41175
  }
40780
41176
  function safeSessionFileStem(sessionId) {
40781
41177
  const readable = sessionId.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 96);
40782
41178
  return `${readable || "session"}-${sha256(sessionId).slice(0, 12)}`;
40783
41179
  }
40784
41180
  function getPiServedArrayLedgerPath(sessionId, storageDir = getMagicContextStorageDir()) {
40785
- return path12.join(storageDir, LEDGER_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
41181
+ return path13.join(storageDir, LEDGER_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
40786
41182
  }
40787
41183
  function getPiServedArrayBodyPath(sessionId, storageDir = getMagicContextStorageDir()) {
40788
- return path12.join(storageDir, BODY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
41184
+ return path13.join(storageDir, BODY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
40789
41185
  }
40790
41186
  function getPiServedTagNumbersPath(sessionId, storageDir = getMagicContextStorageDir()) {
40791
- return path12.join(storageDir, IDENTITY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
41187
+ return path13.join(storageDir, IDENTITY_DIRECTORY, `${safeSessionFileStem(sessionId)}.jsonl`);
40792
41188
  }
40793
41189
  function fullBodyCaptureEnabled() {
40794
41190
  const value = process.env[PI_SERVED_ARRAY_BODY_CAPTURE_ENV]?.trim().toLowerCase();
@@ -40837,7 +41233,7 @@ function recordWriteFailure(error) {
40837
41233
  }
40838
41234
  function appendPendingLines(filePath, lines) {
40839
41235
  try {
40840
- ensureStorageDirectorySync(path12.dirname(filePath));
41236
+ ensureStorageDirectorySync(path13.dirname(filePath));
40841
41237
  repairZeroTail(filePath);
40842
41238
  writeStorageFileSync(filePath, lines.join(""), {
40843
41239
  encoding: "utf8",
@@ -40944,7 +41340,7 @@ function capturePiServedArray(sessionId, messages, options = {}) {
40944
41340
  const identityPath = getPiServedTagNumbersPath(sessionId, storageDir);
40945
41341
  if (newNumbers.length) {
40946
41342
  identityPersistenceFailed = true;
40947
- ensureStorageDirectorySync(path12.dirname(identityPath));
41343
+ ensureStorageDirectorySync(path13.dirname(identityPath));
40948
41344
  options.assertCurrentPass?.();
40949
41345
  repairZeroTail(identityPath, !!options.db);
40950
41346
  writeStorageFileSync(identityPath, `${JSON.stringify({ version: 1, session_id: sessionId, tag_numbers: newNumbers })}
@@ -41060,7 +41456,7 @@ function readPiServedCachedArray(sessionId) {
41060
41456
  const memory = getPiLastServedArray(sessionId);
41061
41457
  const slot = memory === undefined ? getSlot(sessionId) : undefined;
41062
41458
  const jsonPrefix = memory ?? slot?.jsonPrefix;
41063
- if (jsonPrefix === undefined || createHash16("sha256").update(jsonPrefix).digest("hex") !== getPiLastServedDigest(sessionId))
41459
+ if (jsonPrefix === undefined || createHash17("sha256").update(jsonPrefix).digest("hex") !== getPiLastServedDigest(sessionId))
41064
41460
  return;
41065
41461
  let parsed;
41066
41462
  try {
@@ -41084,7 +41480,7 @@ function piMessageEntryFingerprint(message) {
41084
41480
  const m = message;
41085
41481
  if (typeof m.role !== "string")
41086
41482
  return null;
41087
- const contentHash = createHash17("sha256").update(stableStringify(m.content)).digest("hex");
41483
+ const contentHash = createHash18("sha256").update(stableStringify(m.content)).digest("hex");
41088
41484
  return JSON.stringify([
41089
41485
  typeof m.responseId === "string" ? m.responseId : null,
41090
41486
  typeof m.timestamp === "number" || typeof m.timestamp === "string" ? m.timestamp : null,
@@ -41136,6 +41532,83 @@ function piCachedMessageSurvivor(sessionId, fingerprint, ordinal, rows) {
41136
41532
  return carriers.length === 1 ? winner.tagNumber : undefined;
41137
41533
  }
41138
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
+
41139
41612
  // src/pi-pressure.ts
41140
41613
  var MAX_UNKNOWN_PI_INPUT_TOKENS = 3000000;
41141
41614
  function piPressureEvidenceLimit(geometry) {
@@ -43764,7 +44237,8 @@ function convertLocatedPiUserEntry(branchEntries, messageId, ordinalOffset = 0)
43764
44237
  const converted = convertEntriesToRawMessages(branchEntries.slice(start, index + 1)).find((message) => message.id === messageId);
43765
44238
  return converted ? { ...converted, ordinal: rawOrdinal } : null;
43766
44239
  }
43767
- pendingToolStart = -1;
44240
+ if (role === "user" || role === "assistant")
44241
+ pendingToolStart = -1;
43768
44242
  }
43769
44243
  return null;
43770
44244
  }
@@ -45132,16 +45606,18 @@ function adoptPiFallbackTags(db, sessionId, tagger, fingerprintById, options = {
45132
45606
  var EMPTY_PI_HISTORIAN_STATE_SNAPSHOT = {
45133
45607
  detectedContextLimit: 0,
45134
45608
  needsEmergencyRecovery: false,
45609
+ emergencyRecoveryOrigin: null,
45135
45610
  historianFailureCount: 0
45136
45611
  };
45137
45612
  function loadPiHistorianStateSnapshot(db, sessionId) {
45138
- 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,
45139
45614
  historian_failure_count
45140
45615
  FROM session_meta
45141
45616
  WHERE session_id = ?`).get(sessionId);
45142
45617
  return {
45143
45618
  detectedContextLimit: typeof row?.detected_context_limit === "number" && row.detected_context_limit > 0 ? row.detected_context_limit : 0,
45144
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,
45145
45621
  historianFailureCount: typeof row?.historian_failure_count === "number" && row.historian_failure_count >= 0 ? row.historian_failure_count : 0
45146
45622
  };
45147
45623
  }
@@ -45164,6 +45640,13 @@ function registerPiContextHandler(pi, baseOptions) {
45164
45640
  }
45165
45641
  return s;
45166
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
+ });
45167
45650
  pi.on("session_tree", (event, ctx) => {
45168
45651
  if (event.oldLeafId === event.newLeafId)
45169
45652
  return;
@@ -45512,6 +45995,7 @@ function registerPiContextHandler(pi, baseOptions) {
45512
45995
  commitPiCompactionModeRecord(options.db, sessionId, compactionTransition.recordToWrite);
45513
45996
  }
45514
45997
  }
45998
+ const usageResetAfterRefusal = shouldResetRefusedUsage(options.db, sessionId, sessionMetaForUsage);
45515
45999
  const usageReset = {
45516
46000
  lastContextPercentage: 0,
45517
46001
  lastInputTokens: 0,
@@ -45534,8 +46018,8 @@ function registerPiContextHandler(pi, baseOptions) {
45534
46018
  sessionMetaForUsage.lastInputTokens = 0;
45535
46019
  sessionMetaForUsage.observedSafeInputTokens = 0;
45536
46020
  sessionMetaForUsage.cacheAlertSent = false;
45537
- } else if (isFirstContextPassForSession && sessionMetaForUsage.lastContextPercentage > 0) {
45538
- 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`);
45539
46023
  updateSessionMeta(options.db, sessionId, {
45540
46024
  lastContextPercentage: 0,
45541
46025
  lastInputTokens: 0
@@ -45550,7 +46034,7 @@ function registerPiContextHandler(pi, baseOptions) {
45550
46034
  usagePercentage = sessionMetaForUsage.lastContextPercentage;
45551
46035
  usageInputTokens = sessionMetaForUsage.lastInputTokens;
45552
46036
  usedPersistedUsage = true;
45553
- } else {
46037
+ } else if (!usageResetAfterRefusal) {
45554
46038
  usagePercentage = typeof piUsage?.percent === "number" ? piUsage.percent : 0;
45555
46039
  usageInputTokens = typeof piUsage?.tokens === "number" ? piUsage.tokens : 0;
45556
46040
  }
@@ -45618,7 +46102,7 @@ function registerPiContextHandler(pi, baseOptions) {
45618
46102
  }
45619
46103
  let fallbackInputTokens;
45620
46104
  const suspectReading = Math.max(usageInputTokens, piUsage?.tokens ?? 0);
45621
- if (suspectReading > providerInputLimit || piProviderUsageWasRejected(sessionId)) {
46105
+ if (usageResetAfterRefusal || suspectReading > providerInputLimit || piProviderUsageWasRejected(sessionId)) {
45622
46106
  if (suspectReading > providerInputLimit)
45623
46107
  noteRejectedPiUsage(sessionId, suspectReading, providerInputLimit, "transform");
45624
46108
  const replay = !lkgCompactionOff && lkgPassSnapshot ? lkgCoordinator.replay(lkgPassSnapshot, (id) => ctx.sessionManager.getEntry?.(id)?.parentId) : undefined;
@@ -45635,7 +46119,7 @@ function registerPiContextHandler(pi, baseOptions) {
45635
46119
  persistedFromLive: !usedPersistedUsage,
45636
46120
  persistedPercentage: usagePercentage,
45637
46121
  persistedInputTokens: usageInputTokens,
45638
- liveInputTokens: piUsage?.tokens,
46122
+ liveInputTokens: usageResetAfterRefusal ? undefined : piUsage?.tokens,
45639
46123
  usableContextLimit: usageContextLimit
45640
46124
  }));
45641
46125
  const realUsagePercentageBeforeEmergencyBump = usagePercentage;
@@ -45765,6 +46249,8 @@ function registerPiContextHandler(pi, baseOptions) {
45765
46249
  maintainExecuteFloorMark(options.db, sessionId, promptId, usagePercentage < effectiveExecuteThresholdPercentage);
45766
46250
  }
45767
46251
  const pipelineArgs = {
46252
+ providerOverflowRecovery: emergencyRecoveryArmed && historianStateForPass.emergencyRecoveryOrigin === "provider_overflow",
46253
+ dropBranchEntries: branchEntries ?? undefined,
45768
46254
  recoveryPrefix: recovery.prefix,
45769
46255
  recoveryRebuildReason: recovery.rebuildReason,
45770
46256
  assertCurrentPass,
@@ -46140,100 +46626,102 @@ function registerPiContextHandler(pi, baseOptions) {
46140
46626
  if (result.syntheticLeadingCount < 0)
46141
46627
  result.syntheticLeadingCount = prefix.length;
46142
46628
  }
46143
- const tChannelAccounting = performance.now();
46144
- try {
46145
- const sessionMetaForCh1 = sessionMeta;
46146
- if (!options.compactionOff && !sessionMetaForCh1.isSubagent) {
46147
- const { tags, protectedTagNumbers, pendingDropTagNumbers } = result.channelBaselineSnapshot;
46148
- const stableId = (message) => message && typeof message === "object" ? result.postCommitEntryIdByRef.get(message) : undefined;
46149
- const hygieneCalibration = sessionDecisionCalibration(options.db, sessionId);
46150
- const hygieneUnitsVersion = transitionSessionHygieneUnits(options.db, sessionId, result.bustedThisPass, hygieneCalibration);
46151
- const previousBaseline = getPiChannel1Baseline(sessionId);
46152
- const baseline = refreshPiTailHygieneBaseline({
46153
- messages: outputMessages,
46154
- providerApi: ctx.model?.api,
46155
- tags,
46156
- tagCacheKey: result.channelBaselineSnapshot.tagCacheKey,
46157
- protectedTagNumbers,
46158
- protectedTools: options.protectedTools,
46159
- pendingDropTagNumbers,
46160
- stableId,
46161
- protectedToolTags: protectedToolTagNumbers(result.channelBaselineSnapshot.activeTags, options.protectedTools),
46162
- syntheticLeadingCount: result.syntheticLeadingCount,
46163
- cacheBusting: result.bustedThisPass,
46164
- previous: previousBaseline,
46165
- calibration: hygieneUnitsVersion >= HYGIENE_PROVIDER_UNITS_VERSION ? hygieneCalibration : undefined,
46166
- hygieneUnitsVersion
46167
- });
46168
- const effective = effectivePiTailHygiene(baseline);
46169
- if (baseline.lastPrefixMismatch) {
46170
- sessionLog2(sessionId, formatTailHygienePrefixMismatch(baseline.lastPrefixMismatch, baseline.baselineGeneration));
46171
- }
46172
- const durableGrace = baseline.evaluable && !baseline.generationInvalidated ? captureChannel1PostReduceGraceBaseline(options.db, sessionId, effective.u) : getChannel1NudgeState(options.db, sessionId);
46173
- baseline.channel1PostReduceGrace = durableGrace.postReduceGracePending || durableGrace.postReduceGraceBaselineU !== undefined ? {
46174
- pending: durableGrace.postReduceGracePending === true,
46175
- baselineU: durableGrace.postReduceGraceBaselineU,
46176
- preReduceLevel: durableGrace.postReduceGracePreLevel ?? durableGrace.level
46177
- } : undefined;
46178
- const oldestReclaimableToolTags = getOldestActiveUnprotectedToolTags2(options.db, sessionId, protectedTagNumbers);
46179
- const channelState = {
46180
- ...baseline,
46181
- usableWindow: usageContextLimit ?? 0,
46182
- realUserTurnCount: baseline.realUserTurnCount,
46183
- reducedSinceRefresh: false,
46184
- agentDropsAppliedThisPass: result.agentDropsAppliedThisPass,
46185
- oldestReclaimableToolTags
46186
- };
46187
- setPiChannel1Baseline(sessionId, channelState);
46188
- const channel2Evaluation = evaluateChannel2(channelState);
46189
- if (channel2Evaluation.evaluable) {
46190
- try {
46191
- rearmChannel2AfterMeasuredCollapse({
46192
- db: options.db,
46193
- sessionId,
46194
- baseline: channelState,
46195
- rebuilding: result.bustedThisPass,
46196
- previous: previousBaseline
46197
- });
46198
- } catch (error) {
46199
- 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));
46200
46658
  }
46201
- if (channel2Evaluation.shouldTrigger) {
46202
- casChannel2NudgeState(options.db, sessionId, "", "pending");
46203
- } else {
46204
- 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
+ }
46205
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);
46206
46705
  }
46207
- assertTailHygieneLastWriter = () => assertPiTailHygieneContentUnchanged({
46208
- messages: outputMessages,
46209
- tags,
46210
- protectedTagNumbers,
46211
- pendingDropTagNumbers,
46212
- stableId,
46213
- syntheticLeadingCount: result.syntheticLeadingCount,
46214
- expectedSignature: baseline.contentSignature
46215
- });
46216
- } else {
46217
- 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)}`);
46218
46713
  }
46219
- } catch (err) {
46220
- const stale = getPiChannel1Baseline(sessionId);
46221
- if (stale) {
46222
- stale.evaluable = false;
46223
- 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)}`);
46224
46722
  }
46225
- sessionLog2(sessionId, `channel1 baseline / channel2 trigger failed: ${err instanceof Error ? err.message : String(err)}`);
46226
- }
46227
- logTransformTiming(sessionId, "channelNudgeAccounting", tChannelAccounting);
46228
- const tWorkMetrics = performance.now();
46229
- budget.stage = "bookkeeping";
46230
- try {
46231
- const metrics = computePiWorkMetrics(outputMessages);
46232
- setSessionWorkMetrics(options.db, sessionId, metrics.newWorkTokens, metrics.totalInputTokens);
46233
- } catch (err) {
46234
- sessionLog2(sessionId, `work-metrics update failed: ${err instanceof Error ? err.message : String(err)}`);
46235
- }
46236
- logTransformTiming(sessionId, "workMetrics", tWorkMetrics);
46723
+ logTransformTiming(sessionId, "workMetrics", tWorkMetrics);
46724
+ };
46237
46725
  const tStableIdSchemePersist = performance.now();
46238
46726
  if (stableIdSchemeCutover && !options.compactionOff) {
46239
46727
  persistStableIdSchemeForRun(options.db, sessionId, {
@@ -46317,6 +46805,18 @@ function registerPiContextHandler(pi, baseOptions) {
46317
46805
  throw contextRefusalError("The request remains over the model window after stripping signed thinking and reclaiming context");
46318
46806
  if (getHarness2() === "pi") {
46319
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");
46320
46820
  const currentUpperCursor = () => piUpperReclaimCursor({
46321
46821
  sessionId,
46322
46822
  modelKey: resolvePiContextModelKey(ctx) ?? null,
@@ -46326,24 +46826,25 @@ function registerPiContextHandler(pi, baseOptions) {
46326
46826
  snapshot: lkgPassSnapshot,
46327
46827
  input: inputForFitReclaim ?? event.messages
46328
46828
  });
46329
- const upperCursor = fitCase === "upper-estimate-pressure" ? currentUpperCursor() : undefined;
46829
+ const upperCursor = fitCase === "upper-estimate-pressure" && !recoverUnknownUsage ? currentUpperCursor() : undefined;
46330
46830
  if (fitCase === "over-window-proof") {
46331
46831
  requestPiFitReclaimInput(sessionId);
46332
46832
  }
46333
46833
  if (fitCase === "fits-or-unknown")
46334
46834
  clearPiFitReclaimInput(sessionId);
46335
46835
  const upperPassEligible = schedulerDecision === "execute" || result.bustedThisPass || result.prefixRebuildAlreadyPriced || isCacheBusting || forceMaterialization || isEmergency;
46336
- 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));
46337
46837
  if (reclaimHeld)
46338
46838
  sessionLog2(sessionId, "outgoing fit: upper-estimate-pressure; reclaim held by signed prefix, deferred pass or used session latch");
46339
46839
  if (fitCase === "upper-estimate-pressure" && !reclaimHeld && !inputForFitReclaim)
46340
46840
  requestPiFitReclaimInput(sessionId);
46341
- if (fitCase !== "fits-or-unknown" && !reclaimHeld && !fitReclaimedThisPass && !inputForFitReclaim)
46841
+ if ((fitCase !== "fits-or-unknown" || recoverUnknownUsage) && !reclaimHeld && !fitReclaimedThisPass && !inputForFitReclaim)
46342
46842
  inputForFitReclaim = restorePiLkgInputForFit(lkgPassSnapshot, branchEntries);
46343
- if (fitCase !== "fits-or-unknown" && !reclaimHeld && !fitReclaimedThisPass && inputForFitReclaim) {
46843
+ if ((fitCase !== "fits-or-unknown" || recoverUnknownUsage) && !reclaimHeld && !fitReclaimedThisPass && inputForFitReclaim) {
46344
46844
  const reservation = upperCursor ? reservePiUpperEstimateReclaim(options.db, upperCursor) : undefined;
46345
46845
  if (!upperCursor || reservation) {
46346
46846
  let reclaimCompleted = false;
46847
+ const recoveryRetryStart = performance.now();
46347
46848
  try {
46348
46849
  sessionLog2(sessionId, `outgoing fit: ${fitCase}; attempting emergency reclaim`);
46349
46850
  if (fitCase === "over-window-proof" && !ctx.hasUI && historian) {
@@ -46386,7 +46887,7 @@ function registerPiContextHandler(pi, baseOptions) {
46386
46887
  schedulerDecision: "execute",
46387
46888
  schedulerDeferReason: null,
46388
46889
  forceMaterialization: true,
46389
- upperEstimateOnly: fitCase === "upper-estimate-pressure",
46890
+ upperEstimateOnly: !recoverUnknownUsage && fitCase === "upper-estimate-pressure",
46390
46891
  forceSignedRebuildForFit: fitCase === "over-window-proof",
46391
46892
  isCacheBusting: historyRefreshSessions.has(sessionId) || deferredHistoryRefreshSessions.has(sessionId),
46392
46893
  injectionPassSnapshot: options.injection ? createPiM0M1PassSnapshot({
@@ -46395,13 +46896,13 @@ function registerPiContextHandler(pi, baseOptions) {
46395
46896
  compactionOff: false,
46396
46897
  sessionMeta: freshMeta
46397
46898
  }) : undefined,
46398
- contextUsage: {
46399
- percentage: usagePercentage,
46400
- inputTokens: usageInputTokens
46401
- }
46899
+ contextUsage: recoverUnknownUsage ? recoveryUsage : pipelineArgs.contextUsage
46402
46900
  }));
46403
46901
  outputMessages = result.messages;
46404
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)}`);
46405
46906
  injectTodo();
46406
46907
  if (upperCursor)
46407
46908
  recordPiUpperEstimateReclaim(options.db, currentUpperCursor() ?? upperCursor, reservation);
@@ -46423,6 +46924,8 @@ function registerPiContextHandler(pi, baseOptions) {
46423
46924
  }
46424
46925
  }
46425
46926
  }
46927
+ if (recoverUnknownUsage && !unknownUsageRecovered)
46928
+ throw contextRefusalError("Provider overflow recovery has unknown-usage: a pristine request is required for emergency reclaim");
46426
46929
  }
46427
46930
  const refusal = outgoingContextRefusal(estimate, rawFallbackLimit, protectedToolTokenCount(getActiveTagsBySession2(options.db, sessionId), options.protectedTools, envelope ? calibrationForModelKey(envelope.modelKey) : undefined));
46428
46931
  if (refusal)
@@ -46430,6 +46933,7 @@ function registerPiContextHandler(pi, baseOptions) {
46430
46933
  if (getHarness2() === "pi" && classifyPiOutgoingFit(estimate, outgoingFitLimit) === "over-window-proof")
46431
46934
  throw new PiDegradedPassError("outgoing-over-window");
46432
46935
  }
46936
+ publishFinalRequestAccounting();
46433
46937
  let serializedOutput;
46434
46938
  let numbersCommitted = false;
46435
46939
  budget.stage = "LKG/served publication";
@@ -46653,6 +47157,8 @@ function registerPiContextHandler(pi, baseOptions) {
46653
47157
  const sessionId = resolveSessionId2(ctx);
46654
47158
  if (!sessionId)
46655
47159
  return;
47160
+ if (!budget.sideTurn)
47161
+ recordUsageRefusal(baseOptions.db, sessionId);
46656
47162
  withoutSqliteTransformPass2(() => setImmediate(() => {
46657
47163
  if (!isCurrent())
46658
47164
  return;
@@ -46739,6 +47245,7 @@ function ensureRunnablePiBoundaryForTests(snapshot) {
46739
47245
  protectedTailStart: endOrdinal,
46740
47246
  eligibleEndOrdinal: endOrdinal,
46741
47247
  trueRawEligibleTokens: Math.max(1, snapshot.trueRawEligibleTokens),
47248
+ summaryRemovedEligibleTokens: Math.max(1, snapshot.summaryRemovedEligibleTokens),
46742
47249
  rawRangeFingerprint: ""
46743
47250
  };
46744
47251
  }
@@ -47344,6 +47851,10 @@ async function runPipeline(args) {
47344
47851
  args.assertCurrentPass?.();
47345
47852
  if (args.compactionOff)
47346
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);
47347
47858
  const thinkingRecovery = prepareLatestThinkingRecovery({
47348
47859
  db: args.db,
47349
47860
  sessionId: args.sessionId,
@@ -47880,65 +48391,67 @@ async function runPipeline(args) {
47880
48391
  message.content = content;
47881
48392
  });
47882
48393
  }
47883
- const tLegacyToolConversion = performance.now();
47884
- if (foldBustsServedPrefixThisPass) {
47885
- const savedRemovalInputs = nativeRemovalInputs;
47886
- try {
47887
- nativeRemovalInputs = savedRemovalInputs && new Map(savedRemovalInputs);
47888
- let converted = 0;
47889
- args.db.transaction(() => removalBatch((flush) => {
47890
- converted = convertLegacyToolSkeletons(args.db, args.sessionId, targets, {
47891
- prepareRemovals: (rows) => preparePiToolRemovalChecks({
47892
- db: args.db,
47893
- rows,
47894
- targets,
47895
- messages: args.messages,
47896
- prepare: transcript.prepareToolRemovalMeasurements
47897
- }),
47898
- keepFullDrop: (callId) => nativeRemovalInputs === undefined || callId !== null && authorizePiToolRemoval({
47899
- db: args.db,
47900
- sessionId: args.sessionId,
47901
- callId,
47902
- saved: nativeRemovalInputs,
47903
- canApply: false
47904
- }) === true
47905
- }).size;
47906
- flush();
47907
- })).immediate();
47908
- if (converted > 0) {
47909
- 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)}`);
47910
48426
  }
47911
- } catch (error) {
47912
- nativeRemovalInputs = savedRemovalInputs;
47913
- sessionLog2(args.sessionId, `pi legacy dropped-tool skeleton conversion failed (kept legacy bytes): ${error instanceof Error ? error.message : String(error)}`);
47914
48427
  }
47915
- }
47916
- logTransformTiming(args.sessionId, "convertLegacyToolSkeletons", tLegacyToolConversion);
47917
- if (args.sessionMeta.isSubagent && isCacheBustingPass && !protectedSignedPrefix && !args.compactionOff) {
47918
- const savedRemovalInputs = nativeRemovalInputs;
47919
- try {
47920
- nativeRemovalInputs = savedRemovalInputs && new Map(savedRemovalInputs);
47921
- const converted = args.db.transaction(() => removalBatch((flush) => {
47922
- const conversions = prepareAgedToolSkeletonConversions(args.db, args.sessionId, newTargets, {
47923
- prepareRemovals: (rows) => preparePiToolRemovalChecks({
47924
- db: args.db,
47925
- rows,
47926
- targets: newTargets,
47927
- messages: args.messages,
47928
- prepare: transcript.prepareToolRemovalMeasurements
47929
- })
47930
- });
47931
- conversions.persist(args.db);
47932
- flush();
47933
- return conversions.converted.size;
47934
- })).immediate();
47935
- if (converted > 0)
47936
- recordFirstApplicationWireEdit(true);
47937
- } catch (error) {
47938
- nativeRemovalInputs = savedRemovalInputs;
47939
- throw error;
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
+ }
47940
48453
  }
47941
- }
48454
+ };
47942
48455
  try {
47943
48456
  if (!args.sessionMeta.isSubagent) {
47944
48457
  const hasRecentCommit = detectRecentCommit(args.messages);
@@ -47955,16 +48468,23 @@ async function runPipeline(args) {
47955
48468
  const deferredMaterializationWasPending = deferredMaterializationSessions.has(args.sessionId);
47956
48469
  const deferredHistoryRefreshWasPending = deferredHistoryWasPendingAtPassStart;
47957
48470
  const shouldReadPendingOps = !args.compactionOff && (publishedWorkDrainAllowed || args.schedulerDecision === "execute" || args.forceMaterialization || hasPendingMaterializeSignal || foldExecutedThisPass || firstRenderBust);
47958
- const pendingOps = shouldReadPendingOps ? getPendingOps(args.db, args.sessionId) : [];
47959
- const tReclaimDistance = performance.now();
47960
- const reclaimDistance = isCacheBustingPass ? toolReclaimDistanceEntries(mutationView, targets) : [];
47961
- logTransformTiming(args.sessionId, "toolReclaimDistanceEntries", tReclaimDistance);
47962
- const protectedToolTags = protectedToolTagNumbers(getActiveTagsBySession2(args.db, args.sessionId), args.protectedTools);
47963
- for (const number of identityAdoption.protectedTagNumbers)
47964
- protectedToolTags.add(number);
47965
- const protectionWindowForPass = getProtectionWindowForSession(args.db, args.sessionId, protectionFloorResolution.floor);
47966
- const protectedTagNumbersForPass = usesTokenProtection ? protectionWindowForPass.protectedTagNumbers : newestActiveTagNumbersByCount(getActiveTagsBySession2(args.db, args.sessionId), args.protectedTags);
47967
- 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();
47968
48488
  if (!args.sessionMeta.isSubagent && args.schedulerDecision === "execute") {
47969
48489
  args.evaluateHistorianBeforeReclaim?.({
47970
48490
  activeTags: getActiveTagsBySession2(args.db, args.sessionId),
@@ -47981,12 +48501,12 @@ async function runPipeline(args) {
47981
48501
  reclaimableTokens: estimateExecuteReclaimTokens({
47982
48502
  db: args.db,
47983
48503
  sessionId: args.sessionId,
47984
- pendingOps,
47985
- tags: pendingOperationTags,
48504
+ pendingOps: executeReclaimSnapshot.pendingOps,
48505
+ tags: executeReclaimSnapshot.pendingOperationTags,
47986
48506
  targets: newTargets,
47987
48507
  protectedTagIds: new Set([
47988
- ...protectedTagNumbersForPass,
47989
- ...protectedToolTags
48508
+ ...executeReclaimSnapshot.protectedTagNumbersForPass,
48509
+ ...executeReclaimSnapshot.protectedToolTags
47990
48510
  ]),
47991
48511
  probePreparation: (targets) => {
47992
48512
  const saved = nativeRemovalInputs;
@@ -48022,6 +48542,23 @@ async function runPipeline(args) {
48022
48542
  stripUnheldSignedTurnOnBust();
48023
48543
  applyTemporalOnRide();
48024
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);
48025
48562
  const deferredMaterialize = canConsumeDeferredLate && deferredMaterializationWasPending;
48026
48563
  const deferredHistoryRefresh = canConsumeDeferredLate && deferredHistoryRefreshWasPending;
48027
48564
  const shouldApplyPendingOps = publishedWorkDrainAllowed;
@@ -48162,6 +48699,8 @@ async function runPipeline(args) {
48162
48699
  if (shouldRunHeuristics && args.heuristics) {
48163
48700
  try {
48164
48701
  const tHeuristic = performance.now();
48702
+ const providerOverflow = args.providerOverflowRecovery === true;
48703
+ const recoveryCalibration = sessionDecisionCalibration(args.db, args.sessionId);
48165
48704
  const independentMutationBeforeHeuristics = pendingOpsDidMutate || systemPrefixChangedThisPass || foldBustsServedPrefixThisPass || identityRebuild || firstRenderBust || heldIdleExpiryRebuild;
48166
48705
  if (args.forceMaterialization === true && independentMutationBeforeHeuristics && getEmergencyInputSample(args.db, args.sessionId) > 0) {
48167
48706
  clearEmergencyDropSample(args.db, args.sessionId);
@@ -48179,7 +48718,9 @@ async function runPipeline(args) {
48179
48718
  currentTotalInputTokens: args.contextUsage.inputTokens,
48180
48719
  ceilingTokens: args.emergencyCeilingTokens,
48181
48720
  usagePercentage: args.contextUsage.percentage,
48182
- passAlreadyPriced: independentMutationBeforeHeuristics
48721
+ passAlreadyPriced: independentMutationBeforeHeuristics,
48722
+ serializedImageTokens: providerOverflow ? piRecoveryToolImageTokens(workingMessages) : undefined,
48723
+ fitCalibration: providerOverflow ? piRecoveryFitCalibration(recoveryCalibration) : undefined
48183
48724
  } : undefined,
48184
48725
  caveman: args.isSubagent ? undefined : args.heuristics?.caveman
48185
48726
  }, activeTags, stableIdResolver));