@cortexkit/pi-magic-context 0.49.15 → 0.49.17

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 (54) hide show
  1. package/dist/auto-search-worker.js +76 -63
  2. package/dist/chunk-embedding-write-worker.js +11 -9
  3. package/dist/{compaction-marker-candidates-k4tqg4jb.js → compaction-marker-candidates-wjzqbspm.js} +2 -2
  4. package/dist/compaction-marker-candidates-worker.js +2 -2
  5. package/dist/embedding-backfill-vwag34t0.js +23 -0
  6. package/dist/embedding-worker-process.js +1 -1
  7. package/dist/embedding-worker.js +5 -5
  8. package/dist/historian-calibration-extension.js +4 -1
  9. package/dist/index-1cvyaw9a.js +58 -0
  10. package/dist/{index-g4jf772p.js → index-1kcfwk9c.js} +22 -2
  11. package/dist/{index-9jvktm7k.js → index-2mmy81qt.js} +3 -3
  12. package/dist/{index-ccsscaap.js → index-469ttgzv.js} +38 -17
  13. package/dist/index-4knyh1kt.js +1387 -0
  14. package/dist/{index-75ah9413.js → index-5k2mshvq.js} +10 -10
  15. package/dist/{index-a4pk5yv9.js → index-8r2t8fv2.js} +853 -84
  16. package/dist/{index-0awvkeb5.js → index-9j809q6f.js} +32 -72
  17. package/dist/{index-1k729e4f.js → index-bxmf4dhr.js} +6 -6
  18. package/dist/{index-6dz2ed1v.js → index-d8gc0112.js} +2 -2
  19. package/dist/{index-1vw4f2pe.js → index-dvbjsh98.js} +6 -8
  20. package/dist/{index-hy44m6jc.js → index-e1rza8gp.js} +1 -1
  21. package/dist/{index-j0n09wtp.js → index-ej4eak9d.js} +9 -7
  22. package/dist/{index-hshd96pt.js → index-es03azap.js} +127 -2093
  23. package/dist/{index-ajfpyk0x.js → index-fa485mqs.js} +59 -1
  24. package/dist/{index-6ex901g3.js → index-g7p75667.js} +33 -4
  25. package/dist/{index-bet4sd3e.js → index-gdjv1nnd.js} +2 -2
  26. package/dist/{index-46q4e104.js → index-qpq6w67c.js} +1 -1
  27. package/dist/{index-asrxgdt4.js → index-rrwv9y2k.js} +8 -0
  28. package/dist/{index-1mrvw9en.js → index-svgkv4g2.js} +1 -1
  29. package/dist/{index-he9s4qsj.js → index-vktahpdj.js} +23 -3
  30. package/dist/{index-nwbhe6qv.js → index-vmcgew7x.js} +11 -8
  31. package/dist/index-wrr9rp9a.js +16 -0
  32. package/dist/{index-7qc73yk5.js → index-xnch62s6.js} +14 -8
  33. package/dist/{index-djxc8w8c.js → index-yygsh681.js} +25 -26
  34. package/dist/{index-7wagp5jp.js → index-z2rkv0mb.js} +4 -4
  35. package/dist/index-z7kbck29.js +62 -0
  36. package/dist/index.js +688 -339
  37. package/dist/message-fts-rowid-map-cvpy2dmb.js +24 -0
  38. package/dist/{message-index-dw4z9efj.js → message-index-2g23b7fe.js} +13 -13
  39. package/dist/{message-index-bounded-z4d3x9ty.js → message-index-bounded-d6zj2tn5.js} +15 -15
  40. package/dist/{message-time-backfill-r2ar1qvk.js → message-time-backfill-ssshbdy8.js} +5 -5
  41. package/dist/migration-worker.js +8 -6
  42. package/dist/{project-embedding-registry-52zwmtbn.js → project-embedding-registry-vf1ncptn.js} +12 -10
  43. package/dist/raw-ordinal-warmup-worker.js +40 -10
  44. package/dist/{read-session-chunk-0fsgg2k1.js → read-session-chunk-2ky7mbqp.js} +7 -7
  45. package/dist/{read-session-raw-azkqjgzg.js → read-session-raw-9s3xn7fc.js} +2 -2
  46. package/dist/search-archive-9akqcr90.js +20 -0
  47. package/dist/{search-ddbz6v60.js → search-zjyc65g1.js} +20 -19
  48. package/dist/{sqlite-adjpy399.js → sqlite-fsp0sf3r.js} +1 -1
  49. package/dist/{storage-memory-axnsk5t1.js → storage-memory-yxxc5v4n.js} +2 -2
  50. package/dist/{storage-tags-3kb5tvgg.js → storage-tags-cx6ccsv1.js} +3 -3
  51. package/dist/subagent-entry.js +25 -22
  52. package/dist/tag-snapshot-worker-pi.js +17 -20
  53. package/package.json +1 -1
  54. package/dist/embedding-backfill-7kj16fh9.js +0 -21
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  harnessOwnsOpenCodeStore2
5
5
  } from "./index-0h2mkab2.js";
6
6
  import {
7
+ getDataDir,
7
8
  ensureCortexKitArtifactGitignore,
8
9
  getProjectMagicContextHistorianDir,
9
10
  getMagicContextStorageDir,
@@ -248,7 +249,8 @@ import {
248
249
  registerTodoStateLifecycle,
249
250
  syncCtxMemoryToolEnabled,
250
251
  registerMagicContextTools
251
- } from "./index-hshd96pt.js";
252
+ } from "./index-es03azap.js";
253
+ import"./index-4knyh1kt.js";
252
254
  import {
253
255
  canonicalModelIdentity,
254
256
  piModelRefToCanonical,
@@ -265,7 +267,7 @@ import {
265
267
  DEFAULT_HISTORIAN_TIMEOUT_MS,
266
268
  DREAM_TASK_PROMOTION_DEFAULTS,
267
269
  LIVE_RELOAD_CONFIG_PATHS
268
- } from "./index-1k729e4f.js";
270
+ } from "./index-bxmf4dhr.js";
269
271
  import {
270
272
  FAIL_CLOSED_DOCTOR_COMMAND,
271
273
  formatFailClosedBlockingMessage,
@@ -288,8 +290,9 @@ import {
288
290
  openCurrentDatabase,
289
291
  openDatabaseAsync,
290
292
  closeDatabase
291
- } from "./index-ccsscaap.js";
293
+ } from "./index-469ttgzv.js";
292
294
  import {
295
+ pruneMissingProjectIdentities,
293
296
  setHomeProjectPermission,
294
297
  ProjectIdentityError,
295
298
  resolveProjectIdentityStrict,
@@ -299,7 +302,7 @@ import {
299
302
  resolveProjectIdentityForSession,
300
303
  beginBootQuietPeriod,
301
304
  scheduleAfterBootQuiet
302
- } from "./index-6ex901g3.js";
305
+ } from "./index-g7p75667.js";
303
306
  import {
304
307
  detectThinkingBindingMismatch,
305
308
  detectLatestTurnThinkingMismatch,
@@ -458,7 +461,7 @@ import {
458
461
  getRawSessionTagKeysThrough2,
459
462
  getLegacyProtectedTailStartOrdinal2,
460
463
  readSessionChunk2
461
- } from "./index-75ah9413.js";
464
+ } from "./index-5k2mshvq.js";
462
465
  import {
463
466
  UNKNOWN_FIT_RATIO,
464
467
  hasMeasuredDecisionCalibration,
@@ -545,7 +548,7 @@ import {
545
548
  getToolTagNumberByOwner2,
546
549
  getNullOwnerToolTag2,
547
550
  adoptNullOwnerToolTag2
548
- } from "./index-j0n09wtp.js";
551
+ } from "./index-ej4eak9d.js";
549
552
  import {
550
553
  textMentionsRecentCommit,
551
554
  hasMeaningfulUserText2,
@@ -557,7 +560,7 @@ import {
557
560
  estimateTokens,
558
561
  estimateFixedPromptTokens,
559
562
  normalizeText
560
- } from "./index-g4jf772p.js";
563
+ } from "./index-1kcfwk9c.js";
561
564
  import {
562
565
  inRootAsyncContext2,
563
566
  Database2,
@@ -570,7 +573,7 @@ import {
570
573
  isTransientSqliteError2,
571
574
  withAsyncPrivilegedWriter2,
572
575
  indexCoordinationInput2
573
- } from "./index-asrxgdt4.js";
576
+ } from "./index-rrwv9y2k.js";
574
577
  import {
575
578
  stableStringify,
576
579
  closeQuietly,
@@ -579,19 +582,6 @@ import {
579
582
  freezeTemporalDecisions,
580
583
  tagIdentityRevisionKey
581
584
  } from "./index-jrap43gc.js";
582
- import {
583
- resolveOpenCodeDbPath,
584
- assertOpenCodeStoreGeneration,
585
- openCodeDbPathExists,
586
- recordOpenCodeDbReadFailure,
587
- clearOpenCodeDbReadFailure,
588
- claimOpenCodeDbDiagnosticOnce
589
- } from "./index-8976mezv.js";
590
- import"./index-399p688g.js";
591
- import {
592
- runSessionWarmupWorker,
593
- RAW_SUMMARY_TEXT_MAX_CHARS2
594
- } from "./index-he9s4qsj.js";
595
585
  import {
596
586
  piHarnessKindFromExecutable
597
587
  } from "./index-apgwdby5.js";
@@ -604,11 +594,14 @@ import {
604
594
  ownsLkgDirtyFence,
605
595
  invalidateCleanLkgRecoveryFence,
606
596
  markLkgRecoveryDirty,
597
+ gaDatabasePath,
607
598
  readSessionActivity,
608
599
  observeSessionActivity,
609
600
  backfillSessionActivity,
610
601
  clearCompressionDepth,
611
602
  clearCompressionDepthRange,
603
+ cleanSearchText,
604
+ matchedSearchSnippet,
612
605
  getMessageIndexSourceIdentity2,
613
606
  isMessageIndexSourceCurrent2,
614
607
  getLastIndexedOrdinal2,
@@ -616,14 +609,25 @@ import {
616
609
  isMessageIndexReconciledThrough2,
617
610
  indexSingleMessage2,
618
611
  indexMessagesAfterOrdinal2,
619
- sweepOrphanedOpenCodeMessageIndexes2
620
- } from "./index-a4pk5yv9.js";
612
+ sweepOrphanedOpenCodeMessageIndexesCooperatively2
613
+ } from "./index-8r2t8fv2.js";
621
614
  import {
622
- formatSynapseLaneDescriptor
623
- } from "./index-6dz2ed1v.js";
615
+ resolveOpenCodeDbPath,
616
+ assertOpenCodeStoreGeneration,
617
+ openCodeDbPathExists,
618
+ recordOpenCodeDbReadFailure,
619
+ clearOpenCodeDbReadFailure,
620
+ claimOpenCodeDbDiagnosticOnce
621
+ } from "./index-8976mezv.js";
622
+ import"./index-399p688g.js";
624
623
  import {
625
- sameProjectDirectory
626
- } from "./index-djxc8w8c.js";
624
+ runSessionWarmupWorker,
625
+ RAW_SUMMARY_TEXT_MAX_CHARS2
626
+ } from "./index-vktahpdj.js";
627
+ import {
628
+ formatSynapseLaneDescriptor
629
+ } from "./index-d8gc0112.js";
630
+ import"./index-yygsh681.js";
627
631
  import {
628
632
  buildCanonicalChunkTextFromFts,
629
633
  buildCompartmentSummaryFallbackText,
@@ -633,7 +637,7 @@ import {
633
637
  replaceCompartmentChunkEmbeddings,
634
638
  deleteChunkEmbedBackoffForSession,
635
639
  chunkWindowSourceKey
636
- } from "./index-7qc73yk5.js";
640
+ } from "./index-xnch62s6.js";
637
641
  import {
638
642
  recordSessionProjectIdentity,
639
643
  GIT_SWEEP_LEASE_RENEWAL_MS,
@@ -642,7 +646,7 @@ import {
642
646
  markGitSweepSuccessAndRelease,
643
647
  parkGitSweepNonIndexable,
644
648
  releaseGitSweepLease
645
- } from "./index-1mrvw9en.js";
649
+ } from "./index-svgkv4g2.js";
646
650
  import {
647
651
  embedPauseBySession,
648
652
  embedRunStateBySession,
@@ -668,7 +672,7 @@ import {
668
672
  embedSessionCompartmentChunks2,
669
673
  getEmbeddingCoverageStatus2,
670
674
  getAutoEmbeddingSessionCoverage2
671
- } from "./index-nwbhe6qv.js";
675
+ } from "./index-vmcgew7x.js";
672
676
  import {
673
677
  rememberV1HiddenRun
674
678
  } from "./index-6x556c2q.js";
@@ -683,7 +687,7 @@ import {
683
687
  expandWorkspaceIdentitySetWithAliases,
684
688
  sourceNameForMemory,
685
689
  computeWorkspaceEpochFingerprint
686
- } from "./index-bet4sd3e.js";
690
+ } from "./index-gdjv1nnd.js";
687
691
  import {
688
692
  configureContextDatabasePragmas2
689
693
  } from "./index-fapenpch.js";
@@ -733,7 +737,7 @@ import {
733
737
  normalizeVerificationFiles,
734
738
  indexCommitsForProject,
735
739
  embedUnembeddedCommits
736
- } from "./index-0awvkeb5.js";
740
+ } from "./index-9j809q6f.js";
737
741
  import {
738
742
  setEmbeddingSessionBusy2,
739
743
  isEmbeddingHostBusy2
@@ -757,18 +761,18 @@ import {
757
761
  setMemoryClassification2,
758
762
  archiveMemory2,
759
763
  getMemoryCountsByStatus2
760
- } from "./index-hy44m6jc.js";
764
+ } from "./index-e1rza8gp.js";
761
765
  import {
762
766
  invalidateMemory2
763
767
  } from "./index-yhzhdp3m.js";
764
768
  import {
765
769
  pushNotification2
766
770
  } from "./index-q1yc6bs0.js";
767
- import"./index-9jvktm7k.js";
771
+ import"./index-2mmy81qt.js";
768
772
  import {
769
773
  cosineSimilarity
770
- } from "./index-7wagp5jp.js";
771
- import"./index-46q4e104.js";
774
+ } from "./index-z2rkv0mb.js";
775
+ import"./index-qpq6w67c.js";
772
776
  import {
773
777
  DREAMER_PRIMER_INVESTIGATOR_ALLOWED_TOOLS,
774
778
  buildHiddenAgentRegistrations,
@@ -776,9 +780,10 @@ import {
776
780
  readHostSystemPrompt,
777
781
  subagentStepCap,
778
782
  SubagentTelemetry,
783
+ HISTORIAN_CACHE_TAIL_ENV2,
779
784
  HISTORIAN_MAX_OUTPUT_TOKENS_ENV2,
780
785
  SUBAGENT_OUTPUT_CEILING_ENV2
781
- } from "./index-ajfpyk0x.js";
786
+ } from "./index-fa485mqs.js";
782
787
  import {
783
788
  PiTagHydrationError,
784
789
  getPiTagSnapshotView,
@@ -789,8 +794,10 @@ import {
789
794
  drainPiTagSnapshotSession,
790
795
  assertPiTagSnapshotReady,
791
796
  ensurePiTagSnapshotReady
792
- } from "./index-1vw4f2pe.js";
797
+ } from "./index-dvbjsh98.js";
793
798
  import"./index-f18ec69t.js";
799
+ import"./index-1cvyaw9a.js";
800
+ import"./index-z7kbck29.js";
794
801
  import {
795
802
  __toESM
796
803
  } from "./index-22jkk6wg.js";
@@ -800,6 +807,9 @@ import {
800
807
  import {
801
808
  normalizeProtectedToolName
802
809
  } from "./index-674f6qs2.js";
810
+ import {
811
+ sameProjectDirectory
812
+ } from "./index-wrr9rp9a.js";
803
813
  import {
804
814
  logSlowWriteTransaction,
805
815
  WriteTransactionSteps
@@ -812,7 +822,7 @@ import {
812
822
  } from "./index-81nthk8j.js";
813
823
 
814
824
  // src/index.ts
815
- import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
825
+ import { AsyncLocalStorage as AsyncLocalStorage3 } from "node:async_hooks";
816
826
  import { createRequire as createRequire2 } from "node:module";
817
827
  import { join as join16, resolve as resolve3 } from "node:path";
818
828
 
@@ -3509,7 +3519,7 @@ function startHostStallProfilerSwitch() {
3509
3519
  // package.json
3510
3520
  var package_default = {
3511
3521
  name: "@cortexkit/pi-magic-context",
3512
- version: "0.49.15",
3522
+ version: "0.49.17",
3513
3523
  type: "module",
3514
3524
  description: "Pi and OMP coding agent extension for Magic Context — cross-session memory and context management",
3515
3525
  main: "dist/index.js",
@@ -5036,14 +5046,14 @@ function throwWithPromptFailure(legacyError, failedAttempts, args, timeoutMs, tr
5036
5046
  });
5037
5047
  throw error;
5038
5048
  }
5039
- async function attemptOnce(client, args, timeoutMs, signal, callContext, label, transport) {
5049
+ async function attemptOnce(client, args, timeoutMs, signal, callContext, label, transport, allowModelSuggestions = true) {
5040
5050
  const originalBody = { ...args.body };
5041
5051
  const attemptArgs = copyPromptArgs(args, originalBody);
5042
5052
  try {
5043
5053
  await promptWithTimeout(client, attemptArgs, timeoutMs, signal, transport);
5044
5054
  return;
5045
5055
  } catch (error) {
5046
- if (isNonRetryable(error, signal))
5056
+ if (isNonRetryable(error, signal) || !allowModelSuggestions)
5047
5057
  throw error;
5048
5058
  const suggestion = parseModelSuggestion(error);
5049
5059
  if (!suggestion || !originalBody.model) {
@@ -5071,7 +5081,7 @@ async function promptSyncWithModelSuggestionRetry(client, args, options = {}) {
5071
5081
  const explicitPrimaryLabel = baseBody.model?.providerID && baseBody.model.modelID ? `${baseBody.model.providerID}/${baseBody.model.modelID}` : "primary";
5072
5082
  let lastError = null;
5073
5083
  try {
5074
- await attemptOnce(client, baseArgs, timeoutMs, options.signal, callContext, explicitPrimaryLabel, options.transport);
5084
+ await attemptOnce(client, baseArgs, timeoutMs, options.signal, callContext, explicitPrimaryLabel, options.transport, options.allowModelSuggestions);
5075
5085
  return;
5076
5086
  } catch (error) {
5077
5087
  lastError = error;
@@ -5097,7 +5107,7 @@ async function promptSyncWithModelSuggestionRetry(client, args, options = {}) {
5097
5107
  ...fallback?.qualifier ? { variant: fallback.qualifier } : {}
5098
5108
  });
5099
5109
  try {
5100
- await attemptOnce(client, attemptArgs, timeoutMs, options.signal, callContext, label, options.transport);
5110
+ await attemptOnce(client, attemptArgs, timeoutMs, options.signal, callContext, label, options.transport, options.allowModelSuggestions);
5101
5111
  log2(`[${callContext}] fallback succeeded with ${label} (attempt ${i + 2}/${fallbacks.length + 1})`);
5102
5112
  return;
5103
5113
  } catch (error) {
@@ -5115,7 +5125,7 @@ async function promptSyncWithModelSuggestionRetry(client, args, options = {}) {
5115
5125
  }
5116
5126
  async function attemptAndValidate(client, args, timeoutMs, signal, callContext, attempt, options) {
5117
5127
  try {
5118
- await attemptOnce(client, args, timeoutMs, signal, callContext, attempt.label, options.transport);
5128
+ await attemptOnce(client, args, timeoutMs, signal, callContext, attempt.label, options.transport, options.allowModelSuggestions);
5119
5129
  } catch (error) {
5120
5130
  throw {
5121
5131
  error,
@@ -6876,6 +6886,7 @@ function buildHistorianRepairPrompt(originalPrompt, previousOutput, validationEr
6876
6886
  "Return a corrected full XML response for the same existing state and new messages.",
6877
6887
  "Do not skip any displayed raw ordinal or displayed raw range, even if the message looks trivial.",
6878
6888
  "Every displayed message range must belong to exactly one compartment unless it is intentionally left in one trailing suffix marked by <unprocessed_from>.",
6889
+ "If every message in the chunk is covered by a compartment, nothing is unprocessed: set `<unprocessed_from>` to the chunk's last message index + 1.",
6879
6890
  "",
6880
6891
  "Previous invalid XML:",
6881
6892
  previousOutput
@@ -9336,6 +9347,7 @@ async function runHistorianPrompt(args) {
9336
9347
  let handle = null;
9337
9348
  let completion;
9338
9349
  const executor = resolveHiddenCompletionExecutor(args.hiddenCompletionExecutor, client, db, sessionDirectory, "historian");
9350
+ const cacheTail = args.cacheTail ?? (executor.capabilities.harness === "opencode2" ? "skip" : undefined);
9339
9351
  let promptSettled = false;
9340
9352
  let hadUnsettledPrompt = false;
9341
9353
  const startedAt = Date.now();
@@ -9417,6 +9429,7 @@ async function runHistorianPrompt(args) {
9417
9429
  try {
9418
9430
  await promptSyncWithModelSuggestionRetry(client, {
9419
9431
  path: { id: agentSessionId },
9432
+ ...cacheTail !== undefined ? { cacheTail: retryIndex > 0 ? "write" : cacheTail } : {},
9420
9433
  query: { directory: sessionDirectory },
9421
9434
  body: {
9422
9435
  agent: agentId,
@@ -9437,6 +9450,7 @@ async function runHistorianPrompt(args) {
9437
9450
  return executor.attempt(opened, request);
9438
9451
  }, { childSessionId: opened.childSessionId }),
9439
9452
  timeoutMs: timeoutMs ?? DEFAULT_HISTORIAN_TIMEOUT_MS,
9453
+ allowModelSuggestions: false,
9440
9454
  fallbackModels: modelOverride ? undefined : fallbackModels,
9441
9455
  callContext: agentId === HISTORIAN_EDITOR_AGENT ? "historian:editor" : "historian"
9442
9456
  });
@@ -9521,7 +9535,7 @@ async function runFallbackHistorianPass(args) {
9521
9535
  const primary = toModelEntry(args.model);
9522
9536
  for (const candidateInput of [
9523
9537
  ...args.fallbackModels ?? [],
9524
- ...args.fallbackModelId ? [{ model: args.fallbackModelId }] : []
9538
+ ...!primary && args.fallbackModelId ? [{ model: args.fallbackModelId }] : []
9525
9539
  ]) {
9526
9540
  const candidate = toModelEntry(candidateInput);
9527
9541
  if (!candidate)
@@ -19228,9 +19242,14 @@ async function indexSingleMessageOffThread(db, sessionId, message) {
19228
19242
  return result.inserted;
19229
19243
  }
19230
19244
  async function sweepOrphanedOpenCodeMessageIndexesOffThread(db, openSource, options = {}) {
19245
+ if (getHarness2() === "opencode2")
19246
+ openSource = () => new Database2(gaDatabasePath(getDataDir(), process.env.OPENCODE_CHANNEL ?? "latest"), {
19247
+ readonly: true,
19248
+ fileMustExist: true
19249
+ });
19231
19250
  const path = getSqliteDatabasePath2(db);
19232
- if (!path || getHarness2() !== "opencode")
19233
- return sweepOrphanedOpenCodeMessageIndexes2(db, openSource, options);
19251
+ if (!path || getHarness2() !== "opencode" && getHarness2() !== "opencode2")
19252
+ return sweepOrphanedOpenCodeMessageIndexesCooperatively2(db, openSource, options);
19234
19253
  let source = null;
19235
19254
  let sourcePath = null;
19236
19255
  try {
@@ -19242,7 +19261,7 @@ async function sweepOrphanedOpenCodeMessageIndexesOffThread(db, openSource, opti
19242
19261
  source?.close();
19243
19262
  }
19244
19263
  if (!sourcePath)
19245
- return sweepOrphanedOpenCodeMessageIndexes2(db, () => source, options);
19264
+ return sweepOrphanedOpenCodeMessageIndexesCooperatively2(db, () => source, options);
19246
19265
  const reply = await runSessionWarmupWorker({
19247
19266
  path,
19248
19267
  sessionId: "",
@@ -19398,7 +19417,7 @@ function runTick(origin) {
19398
19417
  if (tickInFlight)
19399
19418
  return;
19400
19419
  tickInFlight = true;
19401
- log2(`[dreamer] timer tick (${origin}) — projects=${registeredProjects.size}`);
19420
+ log2(`[magic-context] maintenance tick (${origin}) — projects=${registeredProjects.size}`);
19402
19421
  (async () => {
19403
19422
  try {
19404
19423
  const db = openTimerDatabaseOrNull("maintenance tick");
@@ -19448,6 +19467,9 @@ function persistTickOutcome(db, failure) {
19448
19467
  }
19449
19468
  }
19450
19469
  async function runMessageHistoryMaintenance(db) {
19470
+ const prunedIdentities = await pruneMissingProjectIdentities();
19471
+ if (prunedIdentities > 0)
19472
+ log2(`[magic-context] pruned ${prunedIdentities} missing project identities`);
19451
19473
  try {
19452
19474
  await drainStaleLkgSlots(db);
19453
19475
  } catch (error) {
@@ -19681,10 +19703,20 @@ async function sweepGitCommits(args) {
19681
19703
  const holderId = crypto.randomUUID();
19682
19704
  const lease = acquireGitSweepLease(db, projectIdentity, holderId);
19683
19705
  if (!lease.acquired) {
19684
- const reason = lease.reason === "cooldown_active" ? `cooldown active until ${lease.nextAllowedAt}` : `lease held by ${lease.leaseHolder ?? "another holder"} until ${lease.leaseExpiresAt ?? "unknown"}`;
19706
+ if (lease.reason === "cooldown_active")
19707
+ return;
19708
+ const reason = `lease held by ${lease.leaseHolder ?? "another holder"} until ${lease.leaseExpiresAt ?? "unknown"}`;
19685
19709
  log2(`[git-commits] sweep skipped for ${projectIdentity}: ${reason}`);
19686
19710
  return;
19687
19711
  }
19712
+ if (projectIdentity.startsWith("dir:")) {
19713
+ if (parkGitSweepNonIndexable(db, projectIdentity, holderId)) {
19714
+ log2(`[git-commits] sweep parked for ${projectIdentity}: directory identity has no commits`);
19715
+ } else {
19716
+ releaseGitSweepLease(db, projectIdentity, holderId);
19717
+ }
19718
+ return;
19719
+ }
19688
19720
  const startedAt = Date.now();
19689
19721
  const stopRenewal = startGitSweepLeaseRenewal(db, projectIdentity, holderId);
19690
19722
  log2(`[git-commits] sweep starting for ${projectIdentity} (sinceDays=${gitCommitIndexing.since_days} maxCommits=${gitCommitIndexing.max_commits})`);
@@ -20653,6 +20685,7 @@ class PiSubagentRunner {
20653
20685
  delete parentEnv.MAGIC_CONTEXT_HISTORIAN_TEMPERATURE;
20654
20686
  delete parentEnv[HISTORIAN_MAX_OUTPUT_TOKENS_ENV2];
20655
20687
  delete parentEnv[SUBAGENT_OUTPUT_CEILING_ENV2];
20688
+ delete parentEnv[HISTORIAN_CACHE_TAIL_ENV2];
20656
20689
  try {
20657
20690
  child = inRootAsyncContext2(() => this.spawnImpl(this.invocation.command, [...this.invocation.prefixArgs, ...this.extraArgs, ...args], {
20658
20691
  windowsHide: true,
@@ -20661,6 +20694,7 @@ class PiSubagentRunner {
20661
20694
  ...parentEnv,
20662
20695
  [MAGIC_CONTEXT_PI_SUBAGENT_ENV]: "1",
20663
20696
  MAGIC_CONTEXT_SUBAGENT_PROMPT_FILE: systemPromptPath,
20697
+ [HISTORIAN_CACHE_TAIL_ENV2]: /^(magic-context-)?historian(?:-recomp)?$/.test(options.agent) && options.accountingSubagent !== "historian_editor" ? options.cacheTail : undefined,
20664
20698
  MAGIC_CONTEXT_SUBAGENT_BUDGET_GUARD_FILE: budgetGuardFile,
20665
20699
  MAGIC_CONTEXT_SUBAGENT_PROVENANCE_ONLY: /^(magic-context-)?historian(?:-recomp|-editor)?$/.test(options.agent) ? undefined : "1",
20666
20700
  ...options.temperature !== undefined ? {
@@ -24214,27 +24248,43 @@ function buildReplacementContent(tagId) {
24214
24248
  return `[dropped §${tagId}§]`;
24215
24249
  }
24216
24250
  function applyNewToolDrop(target, options) {
24217
- if (options.persist && (target?.prepareDrop?.() ?? target?.canDrop?.()) === false) {
24251
+ const plan = planNewToolDrop(target, options, !!options.persist);
24252
+ if (!plan)
24218
24253
  return { result: "incomplete", mode: "full" };
24254
+ options.persist?.(plan.mode);
24255
+ return plan.apply();
24256
+ }
24257
+ function planNewToolDrop(target, options, prepare, plannedRemovals) {
24258
+ if (prepare && (target?.prepareDrop?.() ?? target?.canDrop?.()) === false) {
24259
+ return;
24219
24260
  }
24220
24261
  if (!target) {
24221
- options.persist?.("full");
24222
- return { result: "absent", mode: "full" };
24262
+ return { mode: "full", apply: () => ({ result: "absent", mode: "full" }) };
24223
24263
  }
24224
- if (options.inWindow && hasSmallToolInput(target) || options.keepSkeleton === true || target.cannotRemove?.() === true || target.wouldStrandConversationEnd?.() === true) {
24264
+ if (options.inWindow && hasSmallToolInput(target) || options.keepSkeleton === true || target.cannotRemove?.() === true || target.wouldStrandConversationEnd?.(plannedRemovals) === true) {
24225
24265
  const strip = target.hasAttachments?.() === true;
24226
- options.persist?.(strip ? "skeleton_stripped" : "skeleton_real");
24266
+ const mode = strip ? "skeleton_stripped" : "skeleton_real";
24227
24267
  return {
24228
- result: (strip ? target.skeletonStripped?.() : target.skeletonReal?.()) ?? "absent",
24229
- mode: strip ? "skeleton_stripped" : "skeleton_real"
24268
+ mode,
24269
+ apply: () => ({
24270
+ result: (strip ? target.skeletonStripped?.() : target.skeletonReal?.()) ?? "absent",
24271
+ mode
24272
+ })
24230
24273
  };
24231
24274
  }
24232
- options.persist?.("full");
24233
- const result = target.drop?.() ?? "absent";
24234
- if (result === "truncated" && target.hasAttachments?.() === true) {
24235
- return { result: target.skeletonStripped?.() ?? "absent", mode: "skeleton_stripped" };
24236
- }
24237
- return { result, mode: result === "truncated" ? "skeleton_real" : "full" };
24275
+ return {
24276
+ mode: "full",
24277
+ apply: () => {
24278
+ const result = target.drop?.() ?? "absent";
24279
+ if (result === "truncated" && target.hasAttachments?.() === true) {
24280
+ return {
24281
+ result: target.skeletonStripped?.() ?? "absent",
24282
+ mode: "skeleton_stripped"
24283
+ };
24284
+ }
24285
+ return { result, mode: result === "truncated" ? "skeleton_real" : "full" };
24286
+ }
24287
+ };
24238
24288
  }
24239
24289
  function applyPersistedToolDrop(db, sessionId, tagNumber, target, options, afterWrite) {
24240
24290
  return applyNewToolDrop(target, {
@@ -24323,6 +24373,7 @@ function prepareLegacyToolSkeletonConversions(db, sessionId, targets, options =
24323
24373
  }
24324
24374
  };
24325
24375
  }
24376
+ var PENDING_OPERATION_CHUNK_SIZE = 64;
24326
24377
  function applyPendingOperations(sessionId, db, targets, protectedTagIds, preloadedTags, preloadedPendingOps, syntheticPendingOps = [], editMarkerTagIds = new Set, onTagReduced, onBatchComplete, onEligibilitySnapshot) {
24327
24378
  let didMutateMessage = false;
24328
24379
  let diagnostics;
@@ -24332,153 +24383,184 @@ function applyPendingOperations(sessionId, db, targets, protectedTagIds, preload
24332
24383
  diagnostics.reasons[reason] = (diagnostics.reasons[reason] ?? 0) + 1;
24333
24384
  };
24334
24385
  let admitted = false;
24335
- try {
24336
- db.transaction(() => {
24337
- admitted = true;
24338
- }).immediate();
24339
- {
24340
- const pendingOps = preloadedPendingOps ?? getPendingOps(db, sessionId);
24341
- onEligibilitySnapshot?.(capturePendingDropEligibility(targets, protectedTagIds));
24342
- const tags = preloadedTags ?? getTagsByNumbers2(db, sessionId, [
24343
- ...new Set([
24344
- ...pendingOps.map((op) => op.tagId),
24345
- ...syntheticPendingOps.map((op) => op.tagId)
24346
- ])
24347
- ]);
24348
- const tagById = new Map(tags.map((tag) => [tag.tagNumber, tag]));
24349
- const tagStatusById = new Map(tags.map((tag) => [tag.tagNumber, tag.status]));
24350
- const tagTypeById = new Map(tags.map((tag) => [tag.tagNumber, tag.type]));
24351
- const opsToApply = [
24352
- ...pendingOps.map((op) => ({ op, synthetic: false })),
24353
- ...syntheticPendingOps.map((op) => ({ op, synthetic: true }))
24354
- ];
24355
- diagnostics = {
24356
- source: pendingOps.length > 0 && syntheticPendingOps.length > 0 ? "mixed" : syntheticPendingOps.length > 0 ? "synthetic" : "pending",
24357
- total: opsToApply.length,
24358
- mutated: 0,
24359
- persistedWithoutMutation: 0,
24360
- reasons: {}
24361
- };
24362
- const skeletonWindow = new Set(preloadedTags ? tags.filter((tag) => tag.type === "tool").map((tag) => tag.tagNumber).sort((left, right) => right - left).slice(0, RECENT_TOOL_SKELETON_WINDOW) : getNewestToolTagNumbers2(db, sessionId, RECENT_TOOL_SKELETON_WINDOW));
24363
- for (const { op: pendingOp, synthetic } of opsToApply) {
24364
- let operationMutated = false;
24365
- const tagStatus = tagStatusById.get(pendingOp.tagId);
24366
- if (tagStatus === "compacted" || tagStatus === "dropped") {
24367
- reject("already_reduced");
24368
- if (!synthetic)
24369
- removePendingOp(db, sessionId, pendingOp.tagId);
24370
- continue;
24371
- }
24372
- const target = targets.get(pendingOp.tagId);
24373
- const veto = pendingDropVeto(pendingOp.tagId, target, protectedTagIds);
24374
- if (veto) {
24375
- reject(veto);
24376
- continue;
24377
- }
24378
- const isToolTag = tagTypeById.get(pendingOp.tagId) === "tool";
24379
- if (synthetic) {
24380
- if (!isToolTag) {
24381
- reject("synthetic_non_tool");
24382
- continue;
24383
- }
24384
- if (target?.canDrop?.() !== true) {
24385
- reject("synthetic_target_not_droppable");
24386
- continue;
24387
- }
24386
+ let startedAt = 0;
24387
+ const mutations = [];
24388
+ let drainChunk;
24389
+ let hasMore = false;
24390
+ do {
24391
+ admitted = false;
24392
+ startedAt = 0;
24393
+ try {
24394
+ db.transaction(() => {
24395
+ admitted = true;
24396
+ startedAt = performance.now();
24397
+ if (drainChunk) {
24398
+ drainChunk();
24399
+ return;
24388
24400
  }
24389
- const persistDrop = (mode) => {
24390
- let startedAt = 0;
24391
- try {
24392
- db.transaction(() => {
24393
- startedAt = performance.now();
24394
- markTagDropped2(db, sessionId, pendingOp.tagId, mode);
24401
+ const pendingOps = preloadedPendingOps ?? getPendingOps(db, sessionId);
24402
+ onEligibilitySnapshot?.(capturePendingDropEligibility(targets, protectedTagIds));
24403
+ const tags = preloadedTags ?? getTagsByNumbers2(db, sessionId, [
24404
+ ...new Set([
24405
+ ...pendingOps.map((op) => op.tagId),
24406
+ ...syntheticPendingOps.map((op) => op.tagId)
24407
+ ])
24408
+ ]);
24409
+ const tagById = new Map(tags.map((tag) => [tag.tagNumber, tag]));
24410
+ const tagStatusById = new Map(tags.map((tag) => [tag.tagNumber, tag.status]));
24411
+ const tagTypeById = new Map(tags.map((tag) => [tag.tagNumber, tag.type]));
24412
+ const opsToApply = [
24413
+ ...pendingOps.map((op) => ({ op, synthetic: false })),
24414
+ ...syntheticPendingOps.map((op) => ({ op, synthetic: true }))
24415
+ ];
24416
+ diagnostics = {
24417
+ source: pendingOps.length > 0 && syntheticPendingOps.length > 0 ? "mixed" : syntheticPendingOps.length > 0 ? "synthetic" : "pending",
24418
+ total: opsToApply.length,
24419
+ mutated: 0,
24420
+ persistedWithoutMutation: 0,
24421
+ reasons: {}
24422
+ };
24423
+ const skeletonWindow = new Set(preloadedTags ? tags.filter((tag) => tag.type === "tool").map((tag) => tag.tagNumber).sort((left, right) => right - left).slice(0, RECENT_TOOL_SKELETON_WINDOW) : getNewestToolTagNumbers2(db, sessionId, RECENT_TOOL_SKELETON_WINDOW));
24424
+ const plannedRemovals = new Set;
24425
+ const removedTagIds = new Set;
24426
+ const record = (mutated) => {
24427
+ if (!diagnostics)
24428
+ return;
24429
+ if (mutated) {
24430
+ didMutateMessage = true;
24431
+ diagnostics.mutated += 1;
24432
+ } else
24433
+ diagnostics.persistedWithoutMutation += 1;
24434
+ };
24435
+ let cursor = 0;
24436
+ drainChunk = () => {
24437
+ const chunk = opsToApply.slice(cursor, cursor + PENDING_OPERATION_CHUNK_SIZE);
24438
+ cursor += chunk.length;
24439
+ hasMore = cursor < opsToApply.length;
24440
+ for (const { op: pendingOp, synthetic } of chunk) {
24441
+ const tagStatus = tagStatusById.get(pendingOp.tagId);
24442
+ if (tagStatus === "compacted" || tagStatus === "dropped") {
24443
+ reject("already_reduced");
24395
24444
  if (!synthetic)
24396
24445
  removePendingOp(db, sessionId, pendingOp.tagId);
24397
- }).immediate();
24398
- } finally {
24399
- if (startedAt)
24400
- logSlowWriteTransaction("apply_pending_operation", startedAt);
24401
- }
24402
- };
24403
- let shouldPersistDrop = false;
24404
- if (isToolTag) {
24405
- if (editMarkerTagIds.has(pendingOp.tagId)) {
24406
- const strip = target?.hasAttachments?.() === true;
24407
- if (target?.canDrop?.() === false || target?.prepareDrop?.() === false || !target) {
24408
- reject("edit_marker_incomplete");
24409
24446
  continue;
24410
24447
  }
24411
- persistDrop(strip ? "edit_marker_stripped" : "edit_marker");
24412
- const markResult = (strip ? target?.editMarkerStripped?.() : target?.editMarker?.()) ?? "absent";
24413
- if (markResult === "incomplete" || markResult === "absent") {
24414
- reject(`edit_marker_${markResult}`);
24448
+ if (removedTagIds.has(pendingOp.tagId)) {
24449
+ reject("drop_incomplete");
24415
24450
  continue;
24416
24451
  }
24417
- didMutateMessage = true;
24418
- operationMutated = true;
24419
- onTagReduced?.({ tagNumber: pendingOp.tagId, mode: "edit_marker" });
24420
- shouldPersistDrop = true;
24421
- } else {
24422
- const { result: dropResult, mode: appliedMode } = applyNewToolDrop(target, {
24423
- inWindow: skeletonWindow.has(pendingOp.tagId),
24424
- persist: persistDrop
24425
- });
24426
- if (dropResult === "incomplete" || synthetic && dropResult !== "removed" && dropResult !== "truncated") {
24427
- reject(`drop_${dropResult}`);
24452
+ const target = targets.get(pendingOp.tagId);
24453
+ const veto = pendingDropVeto(pendingOp.tagId, target, protectedTagIds);
24454
+ if (veto) {
24455
+ reject(veto);
24428
24456
  continue;
24429
24457
  }
24430
- if (dropResult === "removed" || dropResult === "truncated") {
24431
- didMutateMessage = true;
24432
- operationMutated = true;
24433
- onTagReduced?.({
24434
- tagNumber: pendingOp.tagId,
24435
- mode: appliedMode === "full" ? "full" : "truncated"
24458
+ const isToolTag = tagTypeById.get(pendingOp.tagId) === "tool";
24459
+ if (synthetic) {
24460
+ if (!isToolTag) {
24461
+ reject("synthetic_non_tool");
24462
+ continue;
24463
+ }
24464
+ if (target?.canDrop?.() !== true) {
24465
+ reject("synthetic_target_not_droppable");
24466
+ continue;
24467
+ }
24468
+ }
24469
+ const persistDrop = (mode) => {
24470
+ markTagDropped2(db, sessionId, pendingOp.tagId, mode);
24471
+ if (!synthetic)
24472
+ removePendingOp(db, sessionId, pendingOp.tagId);
24473
+ };
24474
+ if (isToolTag) {
24475
+ if (editMarkerTagIds.has(pendingOp.tagId)) {
24476
+ const strip = target?.hasAttachments?.() === true;
24477
+ if (target?.canDrop?.() === false || target?.prepareDrop?.() === false || !target) {
24478
+ reject("edit_marker_incomplete");
24479
+ continue;
24480
+ }
24481
+ persistDrop(strip ? "edit_marker_stripped" : "edit_marker");
24482
+ mutations.push(() => {
24483
+ const markResult = (strip ? target?.editMarkerStripped?.() : target?.editMarker?.()) ?? "absent";
24484
+ if (markResult === "incomplete" || markResult === "absent") {
24485
+ reject(`edit_marker_${markResult}`);
24486
+ return;
24487
+ }
24488
+ record(true);
24489
+ onTagReduced?.({
24490
+ tagNumber: pendingOp.tagId,
24491
+ mode: "edit_marker"
24492
+ });
24493
+ });
24494
+ } else {
24495
+ const plan = planNewToolDrop(target, { inWindow: skeletonWindow.has(pendingOp.tagId) }, true, plannedRemovals);
24496
+ if (!plan) {
24497
+ reject("drop_incomplete");
24498
+ continue;
24499
+ }
24500
+ persistDrop(plan.mode);
24501
+ if (plan.mode === "full" && target?.drop) {
24502
+ target.notePlannedRemoval?.(plannedRemovals);
24503
+ removedTagIds.add(pendingOp.tagId);
24504
+ }
24505
+ mutations.push(() => {
24506
+ const { result: dropResult, mode: appliedMode } = plan.apply();
24507
+ if (dropResult === "incomplete" || synthetic && dropResult !== "removed" && dropResult !== "truncated") {
24508
+ reject(`drop_${dropResult}`);
24509
+ return;
24510
+ }
24511
+ if (dropResult === "removed" || dropResult === "truncated") {
24512
+ record(true);
24513
+ onTagReduced?.({
24514
+ tagNumber: pendingOp.tagId,
24515
+ mode: appliedMode === "full" ? "full" : "truncated"
24516
+ });
24517
+ } else {
24518
+ reject(`drop_${dropResult}`);
24519
+ record(false);
24520
+ }
24521
+ });
24522
+ }
24523
+ } else if (target) {
24524
+ const replacement = buildReplacementContent(pendingOp.tagId);
24525
+ persistDrop();
24526
+ mutations.push(() => {
24527
+ const priorContent = target.getContent?.();
24528
+ const changed = target.setContent(replacement);
24529
+ record(changed);
24530
+ if (changed) {
24531
+ const originalCharacters = typeof priorContent === "string" ? priorContent.length : tagById.get(pendingOp.tagId)?.byteSize ?? replacement.length;
24532
+ onTagReduced?.({
24533
+ tagNumber: pendingOp.tagId,
24534
+ mode: "partial",
24535
+ removedCharacters: Math.max(0, originalCharacters - replacement.length)
24536
+ });
24537
+ } else {
24538
+ reject("content_unchanged");
24539
+ }
24436
24540
  });
24437
- } else {
24438
- reject(`drop_${dropResult}`);
24541
+ } else if (!synthetic) {
24542
+ reject("target_absent");
24543
+ persistDrop();
24544
+ mutations.push(() => record(false));
24439
24545
  }
24440
- shouldPersistDrop = true;
24441
- }
24442
- } else if (target) {
24443
- const replacement = buildReplacementContent(pendingOp.tagId);
24444
- const priorContent = target.getContent?.();
24445
- persistDrop();
24446
- const changed = target.setContent(replacement);
24447
- if (changed) {
24448
- didMutateMessage = true;
24449
- operationMutated = true;
24450
- const originalCharacters = typeof priorContent === "string" ? priorContent.length : tagById.get(pendingOp.tagId)?.byteSize ?? replacement.length;
24451
- onTagReduced?.({
24452
- tagNumber: pendingOp.tagId,
24453
- mode: "partial",
24454
- removedCharacters: Math.max(0, originalCharacters - replacement.length)
24455
- });
24456
- } else {
24457
- reject("content_unchanged");
24458
24546
  }
24459
- shouldPersistDrop = true;
24460
- } else if (!synthetic) {
24461
- reject("target_absent");
24462
- persistDrop();
24463
- shouldPersistDrop = true;
24464
- }
24465
- if (!shouldPersistDrop) {
24466
- reject("not_persisted");
24467
- continue;
24468
- }
24469
- if (operationMutated)
24470
- diagnostics.mutated += 1;
24471
- else
24472
- diagnostics.persistedWithoutMutation += 1;
24547
+ };
24548
+ drainChunk();
24549
+ }).immediate();
24550
+ } catch (error) {
24551
+ if (!admitted && /database (?:table )?is locked|sqlite_(busy|locked)/i.test(String(error))) {
24552
+ sessionLog2(sessionId, "pending operations write admission busy; retaining remaining operations for retry");
24553
+ hasMore = false;
24554
+ } else {
24555
+ throw error;
24473
24556
  }
24557
+ } finally {
24558
+ if (startedAt)
24559
+ logSlowWriteTransaction("apply_pending_operations", startedAt);
24474
24560
  }
24475
- } catch (error) {
24476
- if (!admitted && /database (?:table )?is locked|sqlite_(busy|locked)/i.test(String(error))) {
24477
- sessionLog2(sessionId, "pending operations write admission busy; retaining prior bytes for retry");
24478
- return false;
24479
- }
24480
- throw error;
24481
- }
24561
+ for (const mutate of mutations.splice(0))
24562
+ mutate();
24563
+ } while (hasMore);
24482
24564
  if (diagnostics) {
24483
24565
  onBatchComplete?.(diagnostics);
24484
24566
  if (diagnostics.total > 0 && diagnostics.mutated === 0) {
@@ -26620,6 +26702,41 @@ ${typeof state?.output === "string" ? state.output : JSON.stringify(state?.outpu
26620
26702
  };
26621
26703
  }
26622
26704
 
26705
+ // ../plugin/src/hooks/magic-context/transform-stage-logger.ts
26706
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
26707
+ var SLOW_STAGE_MS = 100;
26708
+ var passes = new AsyncLocalStorage2;
26709
+ async function withTransformTimingSummary(run) {
26710
+ const pass = {
26711
+ stages: [],
26712
+ slow: [],
26713
+ closed: false,
26714
+ sessionId: undefined
26715
+ };
26716
+ return passes.run(pass, async () => {
26717
+ try {
26718
+ return await run();
26719
+ } finally {
26720
+ pass.closed = true;
26721
+ if (pass.sessionId)
26722
+ sessionLog2(pass.sessionId, `transform stages: ${pass.stages.join(" ")} slow>=${SLOW_STAGE_MS}ms=[${pass.slow.join(",")}]`);
26723
+ }
26724
+ });
26725
+ }
26726
+ function logTransformElapsed(sessionId, stage, elapsedMs, extra) {
26727
+ const elapsed = elapsedMs.toFixed(1);
26728
+ const suffix = extra ? ` ${extra}` : "";
26729
+ const pass = passes.getStore();
26730
+ if (pass && !pass.closed) {
26731
+ pass.sessionId = sessionId;
26732
+ pass.stages.push(`${stage}=${elapsed}ms${extra ? `(${extra})` : ""}`);
26733
+ if (elapsedMs >= SLOW_STAGE_MS)
26734
+ pass.slow.push(stage);
26735
+ }
26736
+ if (elapsedMs >= SLOW_STAGE_MS)
26737
+ sessionLog2(sessionId, `transform stage: stage=${stage} elapsed=${elapsed}ms${suffix}`);
26738
+ }
26739
+
26623
26740
  // ../plugin/src/shared/recovery-reasoning-prefix.ts
26624
26741
  function recoveryReasoningPrefixes(messages, partsKey) {
26625
26742
  const prefixes = [];
@@ -27065,12 +27182,12 @@ class ConversationEndGuard {
27065
27182
  this.end = message;
27066
27183
  }
27067
27184
  }
27068
- wouldStrand(occurrences) {
27185
+ wouldStrand(occurrences, plannedRemovals) {
27069
27186
  const end = this.end;
27070
27187
  if (!end || !occurrences.some((occ) => occ.message === end))
27071
27188
  return false;
27072
27189
  const leaving = new Set(occurrences.map((occ) => partKey(occ.part)));
27073
- const survives = (part) => !leaving.has(partKey(part)) && !this.removed.has(partKey(part));
27190
+ const survives = (part) => !leaving.has(partKey(part)) && !this.removed.has(partKey(part)) && !plannedRemovals?.has(partKey(part));
27074
27191
  return end.info.role === "assistant" ? !end.parts.some((part) => part.kind === "tool_result" && survives(part)) : !end.parts.some((part) => partHasContent(part) && survives(part));
27075
27192
  }
27076
27193
  noteRemoved(occurrences) {
@@ -27397,8 +27514,12 @@ function buildAggregateTarget(tagId, occurrences, requiresToolArcSkeleton, conve
27397
27514
  }
27398
27515
  return null;
27399
27516
  },
27400
- wouldStrandConversationEnd() {
27401
- return conversationEnd?.wouldStrand(occurrences) ?? false;
27517
+ wouldStrandConversationEnd(plannedRemovals) {
27518
+ return conversationEnd?.wouldStrand(occurrences, plannedRemovals) ?? false;
27519
+ },
27520
+ notePlannedRemoval(plannedRemovals) {
27521
+ for (const occ of occurrences)
27522
+ plannedRemovals.add(partKey(occ.part));
27402
27523
  },
27403
27524
  editMarker() {
27404
27525
  const sentinel = `[dropped §${tagId}§]`;
@@ -27486,7 +27607,8 @@ function buildToolTarget(part, message, tagId) {
27486
27607
  }
27487
27608
 
27488
27609
  // ../plugin/src/hooks/magic-context/auto-search-deadline.ts
27489
- var AUTO_SEARCH_TIMEOUT_MS = 3000;
27610
+ var AUTO_SEARCH_TIMEOUT_MS = 1000;
27611
+ var AUTO_SEARCH_VECTOR_TIMEOUT_MS = 200;
27490
27612
  var operationDeadlines = new WeakMap;
27491
27613
  function autoSearchDeadlineUnixMs(signal) {
27492
27614
  const deadline = signal && operationDeadlines.get(signal);
@@ -27599,32 +27721,32 @@ function formatAge(committedAtMs) {
27599
27721
  const years = Math.floor(days / 365);
27600
27722
  return years === 1 ? "1y ago" : `${years}y ago`;
27601
27723
  }
27602
- function renderFragment(result, charCap, wordRules) {
27724
+ function renderFragment(result, charCap, wordRules, query) {
27603
27725
  switch (result.source) {
27604
27726
  case "memory": {
27605
- const compressed = cavemanCompress(result.content, "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27727
+ const compressed = cavemanCompress(cleanSearchText(result.content), "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27606
27728
  return truncate2(compressed, charCap);
27607
27729
  }
27608
27730
  case "git_commit": {
27609
- const subject = result.content.split(/\r?\n/)[0] ?? result.content;
27731
+ const subject = cleanSearchText(result.content.split(/\r?\n/)[0] ?? result.content);
27610
27732
  const body = truncate2(subject, Math.max(10, charCap - 20));
27611
27733
  return `commit ${result.shortSha} ${formatAge(result.committedAtMs)}: ${body}`;
27612
27734
  }
27613
27735
  case "message": {
27614
- const compressed = cavemanCompress(result.content, "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27736
+ const compressed = cavemanCompress(query ? matchedSearchSnippet(result.content, query, charCap) : cleanSearchText(result.content), "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27615
27737
  return truncate2(compressed, charCap);
27616
27738
  }
27617
27739
  case "compartment": {
27618
- const source = result.snippet ?? result.title;
27740
+ const source = cleanSearchText(result.title);
27619
27741
  const compressed = cavemanCompress(source, "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27620
27742
  return truncate2(compressed, charCap);
27621
27743
  }
27622
27744
  case "primer": {
27623
- const compressed = cavemanCompress(result.content, "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27745
+ const compressed = cavemanCompress(cleanSearchText(result.content), "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27624
27746
  return truncate2(compressed, charCap);
27625
27747
  }
27626
27748
  case "note": {
27627
- const compressed = cavemanCompress(result.content, "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27749
+ const compressed = cavemanCompress(cleanSearchText(result.content), "ultra", CURRENT_CAVEMAN_RULES, wordRules);
27628
27750
  return truncate2(compressed, charCap);
27629
27751
  }
27630
27752
  }
@@ -27635,7 +27757,7 @@ function buildAutoSearchHint(results, options = {}) {
27635
27757
  const picks = results.slice(0, maxFragments);
27636
27758
  const lines = [];
27637
27759
  for (const result of picks) {
27638
- const fragment = renderFragment(result, fragmentCharCap, options.wordRules ?? "english");
27760
+ const fragment = renderFragment(result, fragmentCharCap, options.wordRules ?? "english", options.query ?? "");
27639
27761
  if (fragment.length === 0)
27640
27762
  continue;
27641
27763
  lines.push(`- ${fragment}`);
@@ -27658,6 +27780,9 @@ ${trimmedBody}…
27658
27780
  }
27659
27781
  return wrapped;
27660
27782
  }
27783
+ function selectAutoSearchResults(results, threshold) {
27784
+ return results.filter((result) => (result.relevanceScore ?? result.score) >= threshold);
27785
+ }
27661
27786
 
27662
27787
  // ../plugin/src/hooks/magic-context/auto-search-worker-client.ts
27663
27788
  import { Worker } from "node:worker_threads";
@@ -27753,6 +27878,63 @@ async function searchAutoHint(db, sessionId, projectPath, query, options, entry
27753
27878
  return executeAutoSearchWorker(db, sessionId, projectPath, query, options, entry, "search");
27754
27879
  }
27755
27880
  var pendingBackfills = new WeakMap;
27881
+ var searchWorkers = new WeakMap;
27882
+ var liveSearchWorkers = new Set;
27883
+ var MAX_IDLE_SEARCH_WORKERS = 4;
27884
+ var SEARCH_WORKER_IDLE_MS = 5 * 60000;
27885
+ var workersCreated = 0;
27886
+ var workersReused = 0;
27887
+ async function acquireSearchWorker(db, key, entry, input, signal) {
27888
+ const pool = searchWorkers.get(db) ?? new Map;
27889
+ searchWorkers.set(db, pool);
27890
+ while (!signal.aborted) {
27891
+ let slot = pool.get(key);
27892
+ if (slot?.busy) {
27893
+ const current = slot;
27894
+ await new Promise((resolve) => {
27895
+ const done = () => {
27896
+ signal.removeEventListener("abort", done);
27897
+ resolve();
27898
+ };
27899
+ signal.addEventListener("abort", done, { once: true });
27900
+ current.busy?.then(done);
27901
+ if (signal.aborted)
27902
+ done();
27903
+ });
27904
+ continue;
27905
+ }
27906
+ const fresh = !slot;
27907
+ if (!slot) {
27908
+ const worker = inRootAsyncContext2(() => new Worker(entry, { workerData: { ...input, persistent: true } }));
27909
+ slot = {
27910
+ worker,
27911
+ dispose() {
27912
+ clearTimeout(this.idleTimer);
27913
+ if (pool.get(key) === this)
27914
+ pool.delete(key);
27915
+ liveSearchWorkers.delete(this);
27916
+ worker.terminate();
27917
+ worker.unref();
27918
+ }
27919
+ };
27920
+ workersCreated++;
27921
+ pool.set(key, slot);
27922
+ liveSearchWorkers.add(slot);
27923
+ const current = slot;
27924
+ worker.on("error", () => current.dispose());
27925
+ worker.on("exit", () => current.dispose());
27926
+ } else
27927
+ workersReused++;
27928
+ clearTimeout(slot.idleTimer);
27929
+ const current = slot;
27930
+ current.busy = new Promise((resolve) => {
27931
+ current.release = resolve;
27932
+ });
27933
+ current.worker.ref();
27934
+ return { slot: current, fresh };
27935
+ }
27936
+ return null;
27937
+ }
27756
27938
  function queueAutoSearchBackfill(db, sessionId, projectPath, query) {
27757
27939
  if (isEmbeddingHostBusy2())
27758
27940
  return Promise.resolve();
@@ -27817,6 +27999,7 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
27817
27999
  query,
27818
28000
  options: {
27819
28001
  ...serializable,
28002
+ autoHint: true,
27820
28003
  backfillMemoryEmbeddings: false,
27821
28004
  countRetrievals: false,
27822
28005
  measurementDisabled: true
@@ -27826,8 +28009,20 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
27826
28009
  snapshot,
27827
28010
  deadlineUnixMs: autoSearchDeadlineUnixMs(deadlineSignal)
27828
28011
  };
28012
+ let lease;
28013
+ try {
28014
+ lease = await acquireSearchWorker(db, `${input.harness}\x00${projectPath}\x00${job}\x00${entry.href}`, entry, input, signal);
28015
+ } catch (error) {
28016
+ lifecycle.done();
28017
+ throw error;
28018
+ }
28019
+ if (!lease) {
28020
+ lifecycle.done();
28021
+ return [];
28022
+ }
28023
+ const { slot, fresh } = lease;
28024
+ const worker = slot.worker;
27829
28025
  return new Promise((resolve, reject) => {
27830
- const worker = inRootAsyncContext2(() => new Worker(entry, { workerData: input }));
27831
28026
  let settled = false;
27832
28027
  const finish = (results, error) => {
27833
28028
  if (settled)
@@ -27836,7 +28031,25 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
27836
28031
  signal?.removeEventListener("abort", abort);
27837
28032
  lifecycle.signal.removeEventListener("abort", abort);
27838
28033
  lifecycle.done();
27839
- worker.terminate();
28034
+ worker.off("message", onMessage);
28035
+ worker.off("error", onError);
28036
+ worker.off("exit", onExit);
28037
+ if (error || signal.aborted || entry.href !== defaultAutoSearchWorkerEntry().href)
28038
+ slot.dispose();
28039
+ else {
28040
+ slot.idleTimer = setTimeout(() => slot.dispose(), SEARCH_WORKER_IDLE_MS);
28041
+ slot.idleTimer.unref?.();
28042
+ }
28043
+ const release = slot.release;
28044
+ slot.busy = undefined;
28045
+ slot.release = undefined;
28046
+ release?.();
28047
+ const reusable = liveSearchWorkers.delete(slot);
28048
+ if (reusable)
28049
+ liveSearchWorkers.add(slot);
28050
+ const idle = [...liveSearchWorkers].filter((candidate) => !candidate.busy);
28051
+ for (const prior of idle.slice(0, Math.max(0, idle.length - MAX_IDLE_SEARCH_WORKERS)))
28052
+ prior.dispose();
27840
28053
  worker.unref();
27841
28054
  if (error)
27842
28055
  reject(error);
@@ -27846,9 +28059,9 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
27846
28059
  const abort = () => finish([]);
27847
28060
  signal?.addEventListener("abort", abort, { once: true });
27848
28061
  lifecycle.signal.addEventListener("abort", abort, { once: true });
27849
- worker.on("error", (error) => finish([], error));
27850
- worker.on("exit", (code) => finish([], new Error(`auto-search worker exited (${code})`)));
27851
- worker.on("message", async (reply) => {
28062
+ const onError = (error) => finish([], error);
28063
+ const onExit = (code) => finish([], new Error(`auto-search worker exited (${code})`));
28064
+ const onMessage = async (reply) => {
27852
28065
  if (settled)
27853
28066
  return;
27854
28067
  if (reply.kind === "result") {
@@ -27867,7 +28080,7 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
27867
28080
  const response = { id: reply.id };
27868
28081
  try {
27869
28082
  if (reply.kind === "query")
27870
- response.result = await (job === "backfill" ? embedTextForProject2(projectPath, reply.text, signal, "query") : embedQuery?.(reply.text, signal)) ?? null;
28083
+ response.result = await (job === "backfill" ? embedTextForProject2(projectPath, reply.text, signal, "query") : boundedHintEmbedding(embedQuery, reply.text, signal)) ?? null;
27871
28084
  else {
27872
28085
  const passage = await embedBatchForProject2(projectPath, reply.texts, signal, reply.purpose);
27873
28086
  const current = getProjectEmbeddingSnapshot2(projectPath);
@@ -27882,13 +28095,49 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
27882
28095
  response.error = error instanceof Error ? error.message : String(error);
27883
28096
  }
27884
28097
  response.embeddingHostBusy = isEmbeddingHostBusy2();
27885
- if (!settled && !signal?.aborted)
27886
- worker.postMessage(response);
27887
- });
28098
+ if (!settled && !signal?.aborted) {
28099
+ try {
28100
+ worker.postMessage(response);
28101
+ } catch (error) {
28102
+ finish([], error instanceof Error ? error : new Error(String(error)));
28103
+ }
28104
+ }
28105
+ };
28106
+ worker.on("error", onError);
28107
+ worker.on("exit", onExit);
28108
+ worker.on("message", onMessage);
27888
28109
  if (signal?.aborted)
27889
28110
  abort();
28111
+ else if (!fresh) {
28112
+ try {
28113
+ worker.postMessage({ kind: "run", input });
28114
+ } catch (error) {
28115
+ finish([], error instanceof Error ? error : new Error(String(error)));
28116
+ }
28117
+ }
27890
28118
  });
27891
28119
  }
28120
+ async function boundedHintEmbedding(embed, text, signal) {
28121
+ const controller = new AbortController;
28122
+ const combined = AbortSignal.any([signal, controller.signal]);
28123
+ let timer;
28124
+ try {
28125
+ return await Promise.race([
28126
+ Promise.resolve().then(() => embed?.(text, combined) ?? null),
28127
+ new Promise((resolve) => {
28128
+ timer = setTimeout(() => {
28129
+ resolve(null);
28130
+ controller.abort();
28131
+ }, AUTO_SEARCH_VECTOR_TIMEOUT_MS);
28132
+ })
28133
+ ]);
28134
+ } finally {
28135
+ clearTimeout(timer);
28136
+ }
28137
+ }
28138
+ function defaultAutoSearchWorkerEntry() {
28139
+ return new URL(new URL(import.meta.url).pathname.endsWith(".ts") ? "./auto-search-worker.ts" : "./auto-search-worker.js", import.meta.url);
28140
+ }
27892
28141
  var messageSearchWorkerFallbacks = new Map;
27893
28142
 
27894
28143
  // src/auto-search-pi.ts
@@ -28078,6 +28327,8 @@ async function runAutoSearchHintForPi(args) {
28078
28327
  const gitCommitsEnabled = snapshot?.gitCommitEnabled ?? false;
28079
28328
  const searchOptions = {
28080
28329
  limit: 10,
28330
+ maxMessageOrdinal: Math.max(0, getLastCompartmentEndMessage(db, sessionId)),
28331
+ excludeMessageId: userMsgId,
28081
28332
  memoryEnabled,
28082
28333
  embeddingEnabled,
28083
28334
  gitCommitsEnabled,
@@ -28119,13 +28370,15 @@ async function runAutoSearchHintForPi(args) {
28119
28370
  return messages;
28120
28371
  }
28121
28372
  const scoreThreshold = options.scoreThreshold ?? DEFAULT_SCORE_THRESHOLD;
28122
- if (results[0].score < scoreThreshold) {
28123
- sessionLog2(sessionId, `auto-search: top score ${results[0].score.toFixed(3)} below threshold ${scoreThreshold}`);
28373
+ results = selectAutoSearchResults(results, scoreThreshold);
28374
+ if (results.length === 0) {
28375
+ sessionLog2(sessionId, `auto-search: no fragment meets relevance threshold ${scoreThreshold}`);
28124
28376
  await writeNoHintAndReconcile("below-threshold");
28125
28377
  return messages;
28126
28378
  }
28127
28379
  const hintText = buildAutoSearchHint(results, {
28128
- wordRules: options.wordRules
28380
+ wordRules: options.wordRules,
28381
+ query: rawPrompt
28129
28382
  });
28130
28383
  if (!hintText) {
28131
28384
  await writeNoHintAndReconcile("empty");
@@ -28558,6 +28811,7 @@ async function runHistorianSubagentWithTransientRetriesGuarded(args) {
28558
28811
  args.onDispatch?.();
28559
28812
  result = await args.runner.run({
28560
28813
  ...args.options,
28814
+ cacheTail: retryIndex > 0 ? "write" : args.options.cacheTail ?? "skip",
28561
28815
  fallbackModels: undefined
28562
28816
  });
28563
28817
  } catch (error) {
@@ -28585,7 +28839,7 @@ async function runHistorianSubagentWithTransientRetriesGuarded(args) {
28585
28839
  }
28586
28840
  return historianAbortResult(startedAt);
28587
28841
  }
28588
- function buildHistorianFallbackChain(primaryModel, primaryThinkingLevel, fallbackModels, fallbackModelId) {
28842
+ function buildHistorianFallbackChain(primaryModel, primaryThinkingLevel, fallbackModels) {
28589
28843
  const seen = new Set([
28590
28844
  `${primaryModel}\x00${primaryThinkingLevel ?? ""}`
28591
28845
  ]);
@@ -28596,13 +28850,7 @@ function buildHistorianFallbackChain(primaryModel, primaryThinkingLevel, fallbac
28596
28850
  if (!entry.model || seen.has(key))
28597
28851
  continue;
28598
28852
  seen.add(key);
28599
- chain.push({ entry, kind: "configured" });
28600
- }
28601
- if (fallbackModelId) {
28602
- const entry = { model: fallbackModelId };
28603
- const key = `${entry.model}\x00`;
28604
- if (!seen.has(key))
28605
- chain.push({ entry, kind: "session" });
28853
+ chain.push(entry);
28606
28854
  }
28607
28855
  return chain;
28608
28856
  }
@@ -29005,9 +29253,11 @@ ${chunkText}`,
29005
29253
  });
29006
29254
  let validatedPass = await validateHistorianResult(firstResult, sessionId, chunk, priorCompartments, sequenceOffset);
29007
29255
  let validatedDraftText = firstResult.ok ? firstResult.assistantText : null;
29256
+ let fallbackPrompt = prompt;
29008
29257
  if (validatedPass.kind === "validation-failed") {
29009
29258
  sessionLog2(sessionId, `historian: first pass validation failed, retrying with repair prompt: ${validatedPass.error}`);
29010
29259
  const repairPrompt = buildHistorianRepairPrompt(prompt, validatedPass.rawText, validatedPass.error, deps.language);
29260
+ fallbackPrompt = repairPrompt;
29011
29261
  const repairResult = await runHistorianSubagentWithTransientRetries({
29012
29262
  runner,
29013
29263
  sessionId,
@@ -29034,31 +29284,31 @@ ${chunkText}`,
29034
29284
  validatedDraftText = repairResult.assistantText;
29035
29285
  }
29036
29286
  }
29037
- const fallbackChain = buildHistorianFallbackChain(historianModel, thinkingLevel, fallbackModels, fallbackModelId);
29287
+ const fallbackChain = buildHistorianFallbackChain(historianModel, thinkingLevel, fallbackModels);
29038
29288
  if (validatedPass.kind !== "ok" && !(validatedPass.kind === "spawn-failed" && validatedPass.reason === "abort") && fallbackChain.length > 0) {
29039
29289
  for (let i = 0;i < fallbackChain.length; i += 1) {
29040
29290
  const candidate = fallbackChain[i];
29041
- sessionLog2(sessionId, `historian: escalating to ${candidate.kind === "session" ? "session-model last resort" : "configured fallback model"} ${candidate.entry.model}`);
29291
+ sessionLog2(sessionId, `historian: escalating to configured fallback model ${candidate.model}`);
29042
29292
  const fbResult = await runHistorianSubagentWithTransientRetries({
29043
29293
  runner,
29044
29294
  sessionId,
29045
- passLabel: candidate.kind === "session" ? "fallback-session" : "fallback",
29295
+ passLabel: "fallback",
29046
29296
  retryBackoffMs,
29047
29297
  options: {
29048
29298
  agent: HISTORIAN_AGENT_NAME,
29049
29299
  systemPrompt: historianSystemPrompt,
29050
- userMessage: prompt,
29051
- model: candidate.entry.model,
29300
+ userMessage: fallbackPrompt,
29301
+ model: candidate.model,
29052
29302
  timeoutMs: historianTimeoutMs,
29053
29303
  cwd: directory,
29054
29304
  signal,
29055
- thinkingLevel: candidate.entry.qualifier,
29305
+ thinkingLevel: candidate.qualifier,
29056
29306
  temperature,
29057
29307
  maxOutputTokens,
29058
29308
  onProgress: buildProgressLogger("fallback"),
29059
29309
  accountingSessionId: sessionId,
29060
29310
  accountingSubagent: "historian",
29061
- accountingTask: candidate.kind === "session" ? "fallback-session" : "fallback"
29311
+ accountingTask: "fallback"
29062
29312
  }
29063
29313
  });
29064
29314
  const fbPass = await validateHistorianResult(fbResult, sessionId, chunk, priorCompartments, sequenceOffset);
@@ -34028,6 +34278,54 @@ function isSentinelPart(part) {
34028
34278
  return p.type === "text" && typeof p.text === "string" && (p.text === "" || p.text === WHOLE_MESSAGE_PLACEHOLDER_TEXT);
34029
34279
  }
34030
34280
 
34281
+ // src/pi-append-compaction.ts
34282
+ var loggedIncompatibleHost = false;
34283
+ var failedWrites = new WeakMap;
34284
+ function adaptPiAppendCompaction(sessionManager) {
34285
+ if (!sessionManager || typeof sessionManager !== "object")
34286
+ return;
34287
+ const host = sessionManager;
34288
+ const append = host.appendCompaction;
34289
+ if (typeof append !== "function")
34290
+ return;
34291
+ if (failedWrites.has(host))
34292
+ return;
34293
+ const harness = resolvePiHarnessKind();
34294
+ if ((harness === "omp" ? append.length !== 4 && append.length !== 7 : append.length !== 5 && append.length !== 6) || typeof host.getEntry !== "function") {
34295
+ if (!loggedIncompatibleHost) {
34296
+ loggedIncompatibleHost = true;
34297
+ log2(`ERROR: compaction marker disabled: incompatible host binding harness=${harness} arity=${append.length} getEntry=${typeof host.getEntry}; leaving marker pending`);
34298
+ }
34299
+ return;
34300
+ }
34301
+ const getEntry = host.getEntry.bind(host);
34302
+ return (summary, firstKeptEntryId, tokensBefore, details, fromHook) => {
34303
+ const previousFailure = failedWrites.get(host);
34304
+ if (previousFailure)
34305
+ throw previousFailure;
34306
+ let id;
34307
+ if (harness === "pi") {
34308
+ id = append.call(host, summary, firstKeptEntryId, tokensBefore, details, fromHook);
34309
+ } else if (append.length === 7) {
34310
+ id = append.call(host, summary, undefined, firstKeptEntryId, tokensBefore, details, fromHook);
34311
+ } else {
34312
+ id = append.call(host, summary, undefined, firstKeptEntryId, tokensBefore, { details, fromExtension: fromHook });
34313
+ }
34314
+ try {
34315
+ const entry = typeof id === "string" && id.length > 0 ? getEntry(id) : undefined;
34316
+ if (entry?.type !== "compaction" || entry.firstKeptEntryId !== firstKeptEntryId || entry.details?.source !== "magic-context") {
34317
+ throw new Error("Compaction marker read-back mismatch");
34318
+ }
34319
+ } catch {
34320
+ const error = new Error("Compaction marker read-back failed; writes disabled for this session manager until restart");
34321
+ failedWrites.set(host, error);
34322
+ log2(`ERROR: compaction marker read-back failed harness=${harness} compactionId=${typeof id === "string" ? id : "<missing>"} expectedFirstKept=${firstKeptEntryId}; further writes disabled for this session manager until restart; leaving marker pending`);
34323
+ throw error;
34324
+ }
34325
+ return id;
34326
+ };
34327
+ }
34328
+
34031
34329
  // src/pi-context-budget.ts
34032
34330
  var PI_CONTEXT_BUDGET = {
34033
34331
  hostDeadlineMs: 30000,
@@ -34352,6 +34650,87 @@ function isOmpUnmanagedCaptureRequest(payload) {
34352
34650
  // src/pi-dispatch-chain.ts
34353
34651
  import { isDeepStrictEqual } from "node:util";
34354
34652
 
34653
+ // src/pi-dispatch-agent-history.ts
34654
+ function object(value) {
34655
+ return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
34656
+ }
34657
+ var DEVELOPER_ORIGIN = Symbol("custom developer history");
34658
+ function piIsDeveloperHistory(message) {
34659
+ return message.role === "developer" || Reflect.get(message, DEVELOPER_ORIGIN) === true;
34660
+ }
34661
+ function piAgentHistory(messages, api) {
34662
+ return messages.flatMap((value) => {
34663
+ const message = object(value);
34664
+ if (!message)
34665
+ return [value];
34666
+ if (message.role === "compactionSummary") {
34667
+ return [
34668
+ {
34669
+ ...message,
34670
+ role: "user",
34671
+ content: message.blocks !== undefined ? [
34672
+ { type: "text", text: message.summary },
34673
+ ...Array.isArray(message.blocks) ? message.blocks : []
34674
+ ] : [
34675
+ { type: "text", text: "Compaction context envelope" },
34676
+ ...Array.isArray(message.images) ? message.images : []
34677
+ ]
34678
+ }
34679
+ ];
34680
+ }
34681
+ if (message.role !== "custom" && message.role !== "hookMessage")
34682
+ return [value];
34683
+ if (typeof message.content !== "string" && !Array.isArray(message.content))
34684
+ return [];
34685
+ const content = typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
34686
+ const userInvoked = message.role === "custom" && message.attribution === "user";
34687
+ if (userInvoked && message.customType === "collab-prompt") {
34688
+ const text = content.filter((part) => object(part)?.type === "text").map((part) => object(part)?.text).join(`
34689
+ `);
34690
+ return [
34691
+ {
34692
+ ...message,
34693
+ role: "user",
34694
+ content: [
34695
+ {
34696
+ type: "text",
34697
+ text: text.length ? "User interjection envelope" : ""
34698
+ },
34699
+ ...content.filter((part) => object(part)?.type === "image")
34700
+ ]
34701
+ }
34702
+ ];
34703
+ }
34704
+ if (userInvoked && message.customType === "skill-prompt")
34705
+ return [{ ...message, role: "user", content }];
34706
+ const images = content.filter((part) => object(part)?.type === "image");
34707
+ const developerRole = api === "ollama-chat" && message.attribution !== "user" ? "developer" : "user";
34708
+ if (!images.length)
34709
+ return [
34710
+ { ...message, role: developerRole, content, [DEVELOPER_ORIGIN]: true }
34711
+ ];
34712
+ const texts = content.filter((part) => object(part)?.type === "text");
34713
+ return [
34714
+ ...texts.length ? [
34715
+ {
34716
+ ...message,
34717
+ role: developerRole,
34718
+ content: texts,
34719
+ [DEVELOPER_ORIGIN]: true
34720
+ }
34721
+ ] : [],
34722
+ {
34723
+ ...message,
34724
+ role: "user",
34725
+ content: [
34726
+ { type: "text", text: `Images attached to ${message.customType}.` },
34727
+ ...images
34728
+ ]
34729
+ }
34730
+ ];
34731
+ });
34732
+ }
34733
+
34355
34734
  // src/pi-dispatch-tool-ids.ts
34356
34735
  function record4(value) {
34357
34736
  return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
@@ -34423,20 +34802,20 @@ function piToolCallWireId(id, model, source) {
34423
34802
  }
34424
34803
 
34425
34804
  // src/pi-dispatch-chain-tail.ts
34426
- function object(value) {
34805
+ function object2(value) {
34427
34806
  return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
34428
34807
  }
34429
34808
  var OMITTED_IMAGE = "[image omitted: model does not support vision]";
34430
34809
  function piEncodeChainTail(messages, assistant, responseItems, model) {
34431
- const harmony = object(model.identity)?.class === "gpt-oss";
34810
+ const harmony = object2(model.identity)?.class === "gpt-oss";
34432
34811
  const text = (value) => {
34433
34812
  const wellFormed = value.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "�");
34434
34813
  return harmony ? wellFormed.replace(/<\|(start|end|message|channel|constrain|return|call)\|>/g, "<\\|$1\\|>") : wellFormed;
34435
34814
  };
34436
34815
  const supportsImages = model.input?.includes("image") === true;
34437
34816
  const image = (block) => {
34438
- const detail = block.detail === "original" && object(model.compat)?.supportsImageDetailOriginal !== true ? "auto" : block.detail ?? "auto";
34439
- const file = object(block.providerFile);
34817
+ const detail = block.detail === "original" && object2(model.compat)?.supportsImageDetailOriginal !== true ? "auto" : block.detail ?? "auto";
34818
+ const file = object2(block.providerFile);
34440
34819
  if (file?.provider === "openai" && typeof file.id === "string" && file.id)
34441
34820
  return { type: "input_image", detail, file_id: file.id };
34442
34821
  if (typeof block.url !== "string" && (typeof block.mimeType !== "string" || typeof block.data !== "string"))
@@ -34449,9 +34828,21 @@ function piEncodeChainTail(messages, assistant, responseItems, model) {
34449
34828
  };
34450
34829
  const items = [];
34451
34830
  for (const value of messages) {
34452
- const message = object(value);
34831
+ let message = object2(value);
34453
34832
  if (!message || message.providerPayload !== undefined)
34454
34833
  return;
34834
+ if (message.role === "custom" || message.role === "hookMessage") {
34835
+ if (typeof message.customType !== "string" || message.attribution === "user")
34836
+ return;
34837
+ const converted = piAgentHistory([message], model.api);
34838
+ const developer = object2(converted[0]);
34839
+ if (converted.length !== 1 || !developer || !piIsDeveloperHistory(developer))
34840
+ return;
34841
+ message = {
34842
+ ...developer,
34843
+ role: model.api === "openai-codex-responses" ? "developer" : "user"
34844
+ };
34845
+ }
34455
34846
  const user = message.role === "user" || message.role === "developer";
34456
34847
  if (!user && message.role !== "toolResult")
34457
34848
  return;
@@ -34462,7 +34853,7 @@ function piEncodeChainTail(messages, assistant, responseItems, model) {
34462
34853
  const images = [];
34463
34854
  const ordered = [];
34464
34855
  for (const value of content) {
34465
- const block = object(value);
34856
+ const block = object2(value);
34466
34857
  if (block?.type === "text" && typeof block.text === "string") {
34467
34858
  texts.push(block.text);
34468
34859
  if (!user || block.text.trim())
@@ -34490,7 +34881,7 @@ function piEncodeChainTail(messages, assistant, responseItems, model) {
34490
34881
  if (typeof message.toolCallId !== "string" || message.providerMetadata !== undefined)
34491
34882
  return;
34492
34883
  const id = piToolCallWireId(message.toolCallId, model, assistant);
34493
- const call = responseItems.map(object).find((item) => item?.call_id === id);
34884
+ const call = responseItems.map(object2).find((item) => item?.call_id === id);
34494
34885
  if (!id || call?.type !== "function_call" && call?.type !== "custom_tool_call")
34495
34886
  return;
34496
34887
  const joined = texts.join(`
@@ -34507,7 +34898,7 @@ ${OMITTED_IMAGE}` : OMITTED_IMAGE : joined);
34507
34898
  }
34508
34899
 
34509
34900
  // src/pi-dispatch-chain.ts
34510
- function object2(value) {
34901
+ function object3(value) {
34511
34902
  return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
34512
34903
  }
34513
34904
  function piChainCapableApi(api) {
@@ -34521,14 +34912,14 @@ function piPreparedHistory(messages, model) {
34521
34912
  if (!fields)
34522
34913
  return;
34523
34914
  snapshots.push({ id: "", fields });
34524
- const source = object2(message);
34915
+ const source = object3(message);
34525
34916
  responseIds.push(source?.role === "assistant" && typeof source.responseId === "string" && source.responseId.length > 0 ? source.responseId : undefined);
34526
34917
  }
34527
34918
  const prepared = { snapshots, responseIds };
34528
34919
  let index = responseIds.length - 1;
34529
34920
  while (index >= 0 && responseIds[index] === undefined)
34530
34921
  index--;
34531
- const assistant = object2(messages[index]);
34922
+ const assistant = object3(messages[index]);
34532
34923
  const response = piChainResponse(assistant);
34533
34924
  if (model && assistant && response) {
34534
34925
  const delta = piEncodeChainTail(messages.slice(index + 1), assistant, response.responseItems, model);
@@ -34538,7 +34929,7 @@ function piPreparedHistory(messages, model) {
34538
34929
  return prepared;
34539
34930
  }
34540
34931
  function piChainedRequest(payload) {
34541
- const body = object2(payload);
34932
+ const body = object3(payload);
34542
34933
  if (!body || body.previous_response_id === undefined)
34543
34934
  return;
34544
34935
  const id = body.previous_response_id;
@@ -34547,14 +34938,14 @@ function piChainedRequest(payload) {
34547
34938
  return { previousResponseId: id, delta: body.input };
34548
34939
  }
34549
34940
  function piChainResponse(message) {
34550
- const source = object2(message);
34941
+ const source = object3(message);
34551
34942
  if (source?.role !== "assistant")
34552
34943
  return;
34553
34944
  if (source.stopReason !== "stop" && source.stopReason !== "toolUse")
34554
34945
  return;
34555
34946
  if (typeof source.responseId !== "string" || !source.responseId)
34556
34947
  return;
34557
- const payload = object2(source.providerPayload);
34948
+ const payload = object3(source.providerPayload);
34558
34949
  if (payload?.type !== "openaiResponsesHistory" || payload.dt !== true || !Array.isArray(payload.items))
34559
34950
  return;
34560
34951
  return {
@@ -34744,74 +35135,6 @@ function piHasManagedSignature(payload, signature) {
34744
35135
  return false;
34745
35136
  }
34746
35137
 
34747
- // src/pi-dispatch-agent-history.ts
34748
- function object3(value) {
34749
- return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
34750
- }
34751
- function piAgentHistory(messages, api) {
34752
- return messages.flatMap((value) => {
34753
- const message = object3(value);
34754
- if (!message)
34755
- return [value];
34756
- if (message.role === "compactionSummary") {
34757
- return [
34758
- {
34759
- ...message,
34760
- role: "user",
34761
- content: message.blocks !== undefined ? [
34762
- { type: "text", text: message.summary },
34763
- ...Array.isArray(message.blocks) ? message.blocks : []
34764
- ] : [
34765
- { type: "text", text: "Compaction context envelope" },
34766
- ...Array.isArray(message.images) ? message.images : []
34767
- ]
34768
- }
34769
- ];
34770
- }
34771
- if (message.role !== "custom" && message.role !== "hookMessage")
34772
- return [value];
34773
- if (typeof message.content !== "string" && !Array.isArray(message.content))
34774
- return [];
34775
- const content = typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
34776
- const userInvoked = message.role === "custom" && message.attribution === "user";
34777
- if (userInvoked && message.customType === "collab-prompt") {
34778
- const text = content.filter((part) => object3(part)?.type === "text").map((part) => object3(part)?.text).join(`
34779
- `);
34780
- return [
34781
- {
34782
- ...message,
34783
- role: "user",
34784
- content: [
34785
- {
34786
- type: "text",
34787
- text: text.length ? "User interjection envelope" : ""
34788
- },
34789
- ...content.filter((part) => object3(part)?.type === "image")
34790
- ]
34791
- }
34792
- ];
34793
- }
34794
- if (userInvoked && message.customType === "skill-prompt")
34795
- return [{ ...message, role: "user", content }];
34796
- const images = content.filter((part) => object3(part)?.type === "image");
34797
- const developerRole = api === "ollama-chat" && message.attribution !== "user" ? "developer" : "user";
34798
- if (!images.length)
34799
- return [{ ...message, role: developerRole, content }];
34800
- const texts = content.filter((part) => object3(part)?.type === "text");
34801
- return [
34802
- ...texts.length ? [{ ...message, role: developerRole, content: texts }] : [],
34803
- {
34804
- ...message,
34805
- role: "user",
34806
- content: [
34807
- { type: "text", text: `Images attached to ${message.customType}.` },
34808
- ...images
34809
- ]
34810
- }
34811
- ];
34812
- });
34813
- }
34814
-
34815
35138
  // src/pi-dispatch-chat-history.ts
34816
35139
  var MAX_EXACT_UNITS = 32 * 1024 * 1024;
34817
35140
  var IMAGE_CAPTION = "Attached image(s) from tool result:";
@@ -35282,7 +35605,7 @@ function messageStructure(value) {
35282
35605
  "bashExecution",
35283
35606
  "pythonExecution"
35284
35607
  ].includes(String(message.role)) ? "user" : message.role;
35285
- if (role !== "user" && role !== "assistant" && role !== "toolResult")
35608
+ if (role !== "user" && role !== "assistant" && role !== "toolResult" && role !== "system")
35286
35609
  return;
35287
35610
  const row = { role, calls: [], results: [] };
35288
35611
  if (ORPHAN_NOTE in message && typeof message.content === "string") {
@@ -35511,6 +35834,28 @@ function buildPiPreparedStructure(messages, model, prepareUser) {
35511
35834
  }
35512
35835
  if (!valid)
35513
35836
  return;
35837
+ const positionedRows = [...byIndex];
35838
+ for (let position = positionedRows.length - 1;position >= 0; position--) {
35839
+ const [index, row] = positionedRows[position];
35840
+ const source = object7(projectedMessages[index]);
35841
+ if (!source)
35842
+ return;
35843
+ const content = source.content;
35844
+ const parts = typeof content === "string" ? [{ type: "text", text: content }] : content;
35845
+ const textOnly = Array.isArray(parts) && parts.every((part) => object7(part)?.type === "text" && typeof object7(part)?.text === "string");
35846
+ if (piIsDeveloperHistory(source) && source.attribution !== "user") {
35847
+ const previous = rows[position - 1];
35848
+ const next = rows[position + 1];
35849
+ if (model?.api === "ollama-chat" || model?.api === "anthropic-messages" && object7(model.compat)?.supportsMidConversationSystem === true && textOnly && (previous?.role === "user" || previous?.role === "toolResult") && (!next || next.role === "assistant"))
35850
+ row.role = "system";
35851
+ }
35852
+ if (row.role === "system") {
35853
+ if (!textOnly)
35854
+ return;
35855
+ const texts = parts.map((part) => part.text);
35856
+ row.systemText = model?.api === "anthropic-messages" ? texts.filter((text) => text.trim()).map((text) => new TextDecoder().decode(new TextEncoder().encode(text))) : texts;
35857
+ }
35858
+ }
35514
35859
  const responsesApi = responsesLane(model?.api);
35515
35860
  let responsesMessages = responsesApi ? [] : undefined;
35516
35861
  if (responsesMessages) {
@@ -35684,7 +36029,7 @@ function payloadStructure(payload, ollama = false, responsesItems = false, image
35684
36029
  continue;
35685
36030
  }
35686
36031
  }
35687
- const row = messageStructure(ollama && message.role === "system" ? { ...message, role: "user" } : message);
36032
+ const row = messageStructure(message);
35688
36033
  if (!row)
35689
36034
  return;
35690
36035
  row.wireContent = message.content;
@@ -35823,7 +36168,7 @@ function safeRole(row, readable) {
35823
36168
  return "unreadable";
35824
36169
  if (!row)
35825
36170
  return "missing";
35826
- return ["user", "assistant", "toolResult"].includes(row.role) ? row.role : "unknown";
36171
+ return ["user", "assistant", "toolResult", "system"].includes(row.role) ? row.role : "unknown";
35827
36172
  }
35828
36173
  function safeCount(value) {
35829
36174
  return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : "unreadable";
@@ -35903,6 +36248,14 @@ function piPreparedStructureMismatch(payload, prepared, onImageTrim) {
35903
36248
  const actual = rows[i];
35904
36249
  if (actual.role !== expected.role)
35905
36250
  return structureDetail(i, expectedRows, rows, "role");
36251
+ if (expected.systemText !== undefined) {
36252
+ const texts = expected.systemText;
36253
+ const content = actual.wireContent;
36254
+ const actualTexts = typeof content === "string" ? [content] : Array.isArray(content) && content.every((part) => object7(part)?.type === "text" && typeof object7(part)?.text === "string") ? content.map((part) => part.text) : undefined;
36255
+ if (!actualTexts || (typeof content === "string" ? content !== texts.join(`
36256
+ `) : JSON.stringify(actualTexts) !== JSON.stringify(texts)))
36257
+ return structureDetail(i, expectedRows, rows, "content");
36258
+ }
35906
36259
  if (imageProjection && !piMatchesImages(actual.images ?? [], expected.images ?? []))
35907
36260
  return structureDetail(i, expectedRows, rows, "content");
35908
36261
  if (expected.responseTexts !== undefined && (expected.responseTexts.length !== actual.responseTexts?.length || expected.responseTexts.some((text, index) => text !== actual.responseTexts?.[index])))
@@ -41238,13 +41591,12 @@ function buildPiTextIdentityPlan(db, sessionId, tagger, transcript, reuseCandida
41238
41591
  }
41239
41592
  var piBranchProjectionBySession = new Map;
41240
41593
  var piBranchLookupByProjection = new WeakMap;
41241
- function logTransformElapsed(sessionId, stage, elapsedMs, extra) {
41242
- const suffix = extra ? ` ${extra}` : "";
41594
+ function logTransformElapsed2(sessionId, stage, elapsedMs, extra) {
41243
41595
  recordPiTransformTiming({ sessionId, stage, elapsedMs, extra });
41244
- sessionLog2(sessionId, `transform stage: stage=${stage} elapsed=${elapsedMs.toFixed(1)}ms${suffix}`);
41596
+ logTransformElapsed(sessionId, stage, elapsedMs, extra);
41245
41597
  }
41246
41598
  function logTransformTiming(sessionId, stage, start, extra) {
41247
- logTransformElapsed(sessionId, stage, performance.now() - start, extra);
41599
+ logTransformElapsed2(sessionId, stage, performance.now() - start, extra);
41248
41600
  }
41249
41601
  function piVariantChangeBustsProviderCache(providerID, modelID) {
41250
41602
  return variantChangeBustsProviderCache(providerID, modelID);
@@ -41883,7 +42235,7 @@ function stripMcOwnedPiCompactionSummary(messages, entryIds, branchEntries) {
41883
42235
  break;
41884
42236
  }
41885
42237
  }
41886
- if (compaction?.fromHook !== true)
42238
+ if (compaction?.fromHook !== true && compaction?.fromExtension !== true)
41887
42239
  return false;
41888
42240
  const details = compaction.details;
41889
42241
  if (details?.source !== "magic-context")
@@ -42671,7 +43023,7 @@ function loadPiHistorianStateSnapshot(db, sessionId) {
42671
43023
  }
42672
43024
  function registerPiContextHandler(pi, baseOptions) {
42673
43025
  const tagger = createTagger();
42674
- const lkgCoordinator = createPiLkgCoordinator(baseOptions.db, undefined, ({ sessionId, elapsedMs, reusedPrefix }) => logTransformElapsed(sessionId, "lkgCapture", elapsedMs, `reusedPrefix=${reusedPrefix}`));
43026
+ const lkgCoordinator = createPiLkgCoordinator(baseOptions.db, undefined, ({ sessionId, elapsedMs, reusedPrefix }) => logTransformElapsed2(sessionId, "lkgCapture", elapsedMs, `reusedPrefix=${reusedPrefix}`));
42675
43027
  const schedulerCache = new WeakMap;
42676
43028
  const DEFAULT_SCHEDULER_CONFIG = {
42677
43029
  executeThresholdPercentage: 65
@@ -42708,7 +43060,7 @@ function registerPiContextHandler(pi, baseOptions) {
42708
43060
  variantChangeLogObserverForTests?.(message);
42709
43061
  sessionLog2(sessionId, message);
42710
43062
  });
42711
- const contextHandler = async (event, ctx, budget) => {
43063
+ const runHandler = async (event, ctx, budget) => {
42712
43064
  budget.assertOutcome();
42713
43065
  const passSessionId = ctx.sessionManager ? resolveSessionId2(ctx) : undefined;
42714
43066
  const generation = Symbol();
@@ -43731,10 +44083,7 @@ function registerPiContextHandler(pi, baseOptions) {
43731
44083
  messages: inputForThinkingFit,
43732
44084
  forceSignedRebuildForFit: true,
43733
44085
  forceMaterialization: true,
43734
- contextUsage: {
43735
- ...pipelineArgs.contextUsage,
43736
- percentage: Math.max(95, pipelineArgs.contextUsage.percentage)
43737
- }
44086
+ contextUsage: pipelineArgs.contextUsage
43738
44087
  }));
43739
44088
  observePipelineResultForTests(sessionId, result);
43740
44089
  fitReclaimedThisPass = true;
@@ -43845,7 +44194,7 @@ function registerPiContextHandler(pi, baseOptions) {
43845
44194
  sessionMeta: freshMeta
43846
44195
  }) : undefined,
43847
44196
  contextUsage: {
43848
- percentage: fitCase === "over-window-proof" ? Math.max(95, usagePercentage) : usagePercentage,
44197
+ percentage: usagePercentage,
43849
44198
  inputTokens: usageInputTokens
43850
44199
  }
43851
44200
  }));
@@ -44095,6 +44444,7 @@ function registerPiContextHandler(pi, baseOptions) {
44095
44444
  return;
44096
44445
  }
44097
44446
  };
44447
+ const contextHandler = (...args) => withTransformTimingSummary(async () => await runHandler(...args) ?? undefined);
44098
44448
  registerPiGuardedContext(pi, contextHandler, {
44099
44449
  compactionOff: (ctx) => (baseOptions.resolveForProject?.(ctx.cwd) ?? baseOptions).compactionOff === true,
44100
44450
  onRefusal: (ctx, budget, message, isCurrent) => {
@@ -44347,10 +44697,7 @@ function spawnPiHistorianRun(args) {
44347
44697
  historian.onStatusChange?.(ctx, sessionId);
44348
44698
  }
44349
44699
  function resolvePiAppendCompaction(ctx) {
44350
- const sm = ctx.sessionManager;
44351
- if (typeof sm?.appendCompaction !== "function")
44352
- return;
44353
- return sm.appendCompaction.bind(sm);
44700
+ return adaptPiAppendCompaction(ctx.sessionManager);
44354
44701
  }
44355
44702
  function guardedPiAppendCompaction(ctx, assertCurrentPass) {
44356
44703
  const append = resolvePiAppendCompaction(ctx);
@@ -46399,10 +46746,12 @@ function createPiHistorianClient(args) {
46399
46746
  }
46400
46747
  const promptText = extractPromptText(body.parts);
46401
46748
  const modelOverride = readBodyModel(body);
46749
+ const cacheTail = input && typeof input === "object" && "cacheTail" in input ? input.cacheTail : undefined;
46402
46750
  const result = await args.runner.run({
46403
46751
  agent: "magic-context-historian",
46404
46752
  systemPrompt: args.systemPrompt,
46405
46753
  userMessage: promptText,
46754
+ ...cacheTail === "write" || cacheTail === "skip" ? { cacheTail } : {},
46406
46755
  model: modelOverride?.model ?? args.model,
46407
46756
  fallbackModels: modelOverride ? undefined : args.fallbackModels,
46408
46757
  timeoutMs: args.timeoutMs,
@@ -49751,9 +50100,9 @@ var PI_STARTUP_MAINTENANCE_SCHEDULED = Symbol.for("magic-context.pi.startup-main
49751
50100
  function getPiChildInitContext() {
49752
50101
  const globals = globalThis;
49753
50102
  const existing = globals[PI_CHILD_INIT_CONTEXT];
49754
- if (existing instanceof AsyncLocalStorage2)
50103
+ if (existing instanceof AsyncLocalStorage3)
49755
50104
  return existing;
49756
- const context = new AsyncLocalStorage2;
50105
+ const context = new AsyncLocalStorage3;
49757
50106
  globals[PI_CHILD_INIT_CONTEXT] = context;
49758
50107
  return context;
49759
50108
  }