@cortexkit/pi-magic-context 0.49.13 → 0.49.14

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.
package/dist/index.js CHANGED
@@ -247,7 +247,7 @@ import {
247
247
  registerTodoStateLifecycle,
248
248
  syncCtxMemoryToolEnabled,
249
249
  registerMagicContextTools
250
- } from "./index-aypx5h57.js";
250
+ } from "./index-65kty5te.js";
251
251
  import {
252
252
  canonicalModelIdentity,
253
253
  piModelRefToCanonical,
@@ -287,7 +287,7 @@ import {
287
287
  openCurrentDatabase,
288
288
  openDatabaseAsync,
289
289
  closeDatabase
290
- } from "./index-t27xk4r7.js";
290
+ } from "./index-ccsscaap.js";
291
291
  import {
292
292
  setHomeProjectPermission,
293
293
  ProjectIdentityError,
@@ -457,7 +457,7 @@ import {
457
457
  getRawSessionTagKeysThrough2,
458
458
  getLegacyProtectedTailStartOrdinal2,
459
459
  readSessionChunk2
460
- } from "./index-53xdqb3d.js";
460
+ } from "./index-75ah9413.js";
461
461
  import {
462
462
  UNKNOWN_FIT_RATIO,
463
463
  hasMeasuredDecisionCalibration,
@@ -544,7 +544,7 @@ import {
544
544
  getToolTagNumberByOwner2,
545
545
  getNullOwnerToolTag2,
546
546
  adoptNullOwnerToolTag2
547
- } from "./index-ed9d2ded.js";
547
+ } from "./index-j0n09wtp.js";
548
548
  import {
549
549
  textMentionsRecentCommit,
550
550
  hasMeaningfulUserText2,
@@ -576,11 +576,7 @@ import {
576
576
  isKnownAutocommit,
577
577
  observeTemporalDecisions,
578
578
  freezeTemporalDecisions,
579
- tagRevisionIncarnationSql,
580
- tagRevisionCountSql,
581
- tagRevisionKey,
582
- tagIdentityRevisionKey,
583
- TAG_INSERT_CONFLICT_ROWS
579
+ tagIdentityRevisionKey
584
580
  } from "./index-jrap43gc.js";
585
581
  import {
586
582
  resolveOpenCodeDbPath,
@@ -620,7 +616,7 @@ import {
620
616
  indexSingleMessage2,
621
617
  indexMessagesAfterOrdinal2,
622
618
  sweepOrphanedOpenCodeMessageIndexes2
623
- } from "./index-vv1j5cak.js";
619
+ } from "./index-a4pk5yv9.js";
624
620
  import {
625
621
  formatSynapseLaneDescriptor
626
622
  } from "./index-6dz2ed1v.js";
@@ -686,7 +682,7 @@ import {
686
682
  expandWorkspaceIdentitySetWithAliases,
687
683
  sourceNameForMemory,
688
684
  computeWorkspaceEpochFingerprint
689
- } from "./index-3cnarwf8.js";
685
+ } from "./index-bet4sd3e.js";
690
686
  import {
691
687
  configureContextDatabasePragmas2
692
688
  } from "./index-fapenpch.js";
@@ -736,7 +732,7 @@ import {
736
732
  normalizeVerificationFiles,
737
733
  indexCommitsForProject,
738
734
  embedUnembeddedCommits
739
- } from "./index-wp9q91kn.js";
735
+ } from "./index-0awvkeb5.js";
740
736
  import {
741
737
  setEmbeddingSessionBusy2,
742
738
  isEmbeddingHostBusy2
@@ -780,6 +776,17 @@ import {
780
776
  SubagentTelemetry,
781
777
  readHostSystemPrompt
782
778
  } from "./index-y29y2j2w.js";
779
+ import {
780
+ PiTagHydrationError,
781
+ getPiTagSnapshotView,
782
+ getPiActiveTagSnapshotView,
783
+ readPiFallbackTagRows,
784
+ prewarmPiTagSnapshot,
785
+ cancelPiTagSnapshotSession,
786
+ drainPiTagSnapshotSession,
787
+ assertPiTagSnapshotReady,
788
+ ensurePiTagSnapshotReady
789
+ } from "./index-1vw4f2pe.js";
783
790
  import"./index-f18ec69t.js";
784
791
  import {
785
792
  __toESM
@@ -3499,7 +3506,7 @@ function startHostStallProfilerSwitch() {
3499
3506
  // package.json
3500
3507
  var package_default = {
3501
3508
  name: "@cortexkit/pi-magic-context",
3502
- version: "0.49.13",
3509
+ version: "0.49.14",
3503
3510
  type: "module",
3504
3511
  description: "Pi and OMP coding agent extension for Magic Context — cross-session memory and context management",
3505
3512
  main: "dist/index.js",
@@ -3528,7 +3535,7 @@ var package_default = {
3528
3535
  ],
3529
3536
  "//": "The build intentionally bundles typebox (no --external typebox) and keeps it a regular dependency. OMP's extension loader rewrites every bare `typebox` import to its own omptype-based shim, whose schemas are callable objects rather than JSON Schema; they fail structuredClone during tool registration, so an external typebox import stops Magic Context loading on OMP. src/typebox-bundling.test.ts guards this.",
3530
3537
  scripts: {
3531
- build: "bun ../../scripts/check-bun.mjs && tsc -p ../retina-local-fs/tsconfig.build.json && bun ../../scripts/clean-dist-chunks.mjs dist index.js subagent-entry.js historian-calibration-extension.js embedding-worker-process.js raw-ordinal-warmup-worker.js transformers-web.js transformers-node-wasm.js && bun build ../plugin/src/features/magic-context/memory/transformers-web-entry.ts --outfile dist/transformers-web.js --target browser --format esm --external onnxruntime-web && bun ../plugin/scripts/build-transformers-node-wasm.ts dist && bun build src/index.ts src/subagent-entry.ts src/historian-calibration-extension.ts ../plugin/src/features/magic-context/memory/embedding-worker.ts ../plugin/src/features/magic-context/memory/embedding-worker-process.ts ../plugin/src/features/magic-context/migration-worker.ts ../plugin/src/hooks/magic-context/auto-search-worker.ts ../plugin/src/hooks/magic-context/raw-ordinal-warmup-worker.ts ../plugin/src/features/magic-context/compaction-marker-candidates-worker.ts ../plugin/src/features/magic-context/chunk-embedding-write-worker.ts --outdir dist --entry-naming '[name].[ext]' --target node --format esm --splitting --external @earendil-works/pi-coding-agent --external @oh-my-pi/pi-coding-agent --external @earendil-works/pi-tui --external onnxruntime-node --external onnxruntime-web --external sharp --external bun:ffi --external node:sqlite",
3538
+ build: "bun ../../scripts/check-bun.mjs && tsc -p ../retina-local-fs/tsconfig.build.json && bun ../../scripts/clean-dist-chunks.mjs dist index.js subagent-entry.js historian-calibration-extension.js embedding-worker-process.js raw-ordinal-warmup-worker.js tag-snapshot-worker-pi.js transformers-web.js transformers-node-wasm.js && bun build ../plugin/src/features/magic-context/memory/transformers-web-entry.ts --outfile dist/transformers-web.js --target browser --format esm --external onnxruntime-web && bun ../plugin/scripts/build-transformers-node-wasm.ts dist && bun build src/index.ts src/tag-snapshot-worker-pi.ts src/subagent-entry.ts src/historian-calibration-extension.ts ../plugin/src/features/magic-context/memory/embedding-worker.ts ../plugin/src/features/magic-context/memory/embedding-worker-process.ts ../plugin/src/features/magic-context/migration-worker.ts ../plugin/src/hooks/magic-context/auto-search-worker.ts ../plugin/src/hooks/magic-context/raw-ordinal-warmup-worker.ts ../plugin/src/features/magic-context/compaction-marker-candidates-worker.ts ../plugin/src/features/magic-context/chunk-embedding-write-worker.ts --outdir dist --entry-naming '[name].[ext]' --target node --format esm --splitting --external @earendil-works/pi-coding-agent --external @oh-my-pi/pi-coding-agent --external @earendil-works/pi-tui --external onnxruntime-node --external onnxruntime-web --external sharp --external bun:ffi --external node:sqlite",
3532
3539
  "build:e2e-argv": `bun ../../scripts/clean-dist-chunks.mjs ../../tests/docker/.generated subagent-runner-e2e.mjs && bun build src/subagent-runner.ts --outdir ../../tests/docker/.generated --entry-naming 'subagent-runner-e2e.mjs' --splitting --target node --format esm --external node:sqlite && node --input-type=module -e 'const { __test } = await import("../../tests/docker/.generated/subagent-runner-e2e.mjs"); if (typeof __test.buildArgs !== "function" || typeof __test.resolvePiInvocation !== "function") throw new Error("argv renderer exports missing"); console.log("Node argv renderer: 2 exports loaded")'`,
3533
3540
  typecheck: "tsc -p ../retina-local-fs/tsconfig.build.json && tsc --noEmit",
3534
3541
  test: "bun ../../scripts/check-bun.mjs && bun install --frozen-lockfile && BUN_JSC_useOMGJIT=0 bun test --parallel=4 --timeout 30000",
@@ -3537,7 +3544,7 @@ var package_default = {
3537
3544
  "lint:fix": "biome check --write src",
3538
3545
  format: "biome format --write src",
3539
3546
  "format:check": "biome format src",
3540
- clean: "bun ../../scripts/clean-dist-chunks.mjs dist index.js subagent-entry.js historian-calibration-extension.js embedding-worker.js embedding-worker-process.js migration-worker.js raw-ordinal-warmup-worker.js transformers-web.js transformers-node-wasm.js",
3547
+ clean: "bun ../../scripts/clean-dist-chunks.mjs dist index.js subagent-entry.js historian-calibration-extension.js embedding-worker.js embedding-worker-process.js migration-worker.js raw-ordinal-warmup-worker.js tag-snapshot-worker-pi.js transformers-web.js transformers-node-wasm.js",
3541
3548
  prepublishOnly: "bun run build"
3542
3549
  },
3543
3550
  "//test": "Test runs cap file-isolated workers at four (one for test:serial) and disable JSC's optimizing WebAssembly tier so per-file isolation does not pay its compilation cost on every file. These are not production settings and do not change host process engine options.",
@@ -3802,6 +3809,7 @@ async function inheritPiCloneSession(event, ctx, deps, isStartup) {
3802
3809
  deps.signalPendingMarker(destinationSessionId);
3803
3810
  }
3804
3811
  writeLog(`[magic-context][pi] clone-inheritance: migrated compartments=${result.compartmentsCopied} tags=${result.tagsCopied} notes=${result.notesCopied} facts=${result.factsCopied} source=${sourceSessionId} dest=${destinationSessionId} reason=${isStartup ? "startup" : event.reason}`);
3812
+ deps.onInherited?.(destinationSessionId);
3805
3813
  return result;
3806
3814
  } catch (error) {
3807
3815
  logCloneStartFailureOnce(destinationSessionId, () => writeLog(`[magic-context][pi] clone-inheritance: failed source=${sourceSessionId} dest=${destinationSessionId} stage=${stage} error=${error instanceof Error ? error.message : String(error)}; run /ctx-wrapup to rebuild, or re-clone`));
@@ -12693,7 +12701,7 @@ function getAsyncModule() {
12693
12701
  await beforeModuleLoad();
12694
12702
  const [{ default: singlefileAsyncifyVariant }, { newQuickJSAsyncWASMModuleFromVariant }] = await importPluginModule(() => Promise.all([
12695
12703
  import("./index-e7vx3byk.js").then((m)=>__toESM(m.default,1)),
12696
- import("./index-e6xnpach.js")
12704
+ import("./index-b7p25tm0.js")
12697
12705
  ]));
12698
12706
  const module = await newQuickJSAsyncWASMModuleFromVariant(singlefileAsyncifyVariant);
12699
12707
  return module;
@@ -14369,7 +14377,8 @@ function currentManifest(text) {
14369
14377
  return start < 0 ? text : text.slice(start);
14370
14378
  }
14371
14379
  function parseMapMemoriesCompletion(text, completion, expectedIds) {
14372
- return validateMapMemoriesManifest(currentManifest(assistantText(completion.messages ?? []) || text), expectedIds);
14380
+ const source = /<mappings\b/i.test(text) ? text : assistantText(completion.messages ?? []) || text;
14381
+ return validateMapMemoriesManifest(currentManifest(source), expectedIds);
14373
14382
  }
14374
14383
  function validateClientMapMemories(messages, expectedIds) {
14375
14384
  if (hasLengthCappedOutput(messages)) {
@@ -24150,6 +24159,31 @@ function computePiWorkMetrics(sessionEntries) {
24150
24159
  return { newWorkTokens, totalInputTokens };
24151
24160
  }
24152
24161
 
24162
+ // ../plugin/src/hooks/magic-context/pending-drop-eligibility.ts
24163
+ function pendingDropVeto(tagNumber, target, protectedTagIds) {
24164
+ if (protectedTagIds.has(tagNumber))
24165
+ return "protected";
24166
+ if (target?.thinkingDropProtected)
24167
+ return "active_thinking";
24168
+ return null;
24169
+ }
24170
+ function capturePendingDropEligibility(targets, protectedTagIds) {
24171
+ const eligible = new Set;
24172
+ for (const [number, target] of targets) {
24173
+ if (pendingDropVeto(number, target, protectedTagIds))
24174
+ continue;
24175
+ if (target.drop) {
24176
+ if (target.canDrop?.() === true)
24177
+ eligible.add(number);
24178
+ } else {
24179
+ const content = target.getContent?.();
24180
+ if (typeof content === "string" && content.length > `[dropped §${number}§]`.length)
24181
+ eligible.add(number);
24182
+ }
24183
+ }
24184
+ return eligible;
24185
+ }
24186
+
24153
24187
  // ../plugin/src/hooks/magic-context/apply-operations.ts
24154
24188
  var RECENT_TOOL_SKELETON_WINDOW = 20;
24155
24189
  function buildReplacementContent(tagId) {
@@ -24265,7 +24299,7 @@ function prepareLegacyToolSkeletonConversions(db, sessionId, targets, options =
24265
24299
  }
24266
24300
  };
24267
24301
  }
24268
- function applyPendingOperations(sessionId, db, targets, protectedTagIds, preloadedTags, preloadedPendingOps, syntheticPendingOps = [], editMarkerTagIds = new Set, onTagReduced, onBatchComplete) {
24302
+ function applyPendingOperations(sessionId, db, targets, protectedTagIds, preloadedTags, preloadedPendingOps, syntheticPendingOps = [], editMarkerTagIds = new Set, onTagReduced, onBatchComplete, onEligibilitySnapshot) {
24269
24303
  let didMutateMessage = false;
24270
24304
  let diagnostics;
24271
24305
  const reject = (reason) => {
@@ -24280,6 +24314,7 @@ function applyPendingOperations(sessionId, db, targets, protectedTagIds, preload
24280
24314
  }).immediate();
24281
24315
  {
24282
24316
  const pendingOps = preloadedPendingOps ?? getPendingOps(db, sessionId);
24317
+ onEligibilitySnapshot?.(capturePendingDropEligibility(targets, protectedTagIds));
24283
24318
  const tags = preloadedTags ?? getTagsByNumbers2(db, sessionId, [
24284
24319
  ...new Set([
24285
24320
  ...pendingOps.map((op) => op.tagId),
@@ -24310,13 +24345,10 @@ function applyPendingOperations(sessionId, db, targets, protectedTagIds, preload
24310
24345
  removePendingOp(db, sessionId, pendingOp.tagId);
24311
24346
  continue;
24312
24347
  }
24313
- if (protectedTagIds.has(pendingOp.tagId)) {
24314
- reject("protected");
24315
- continue;
24316
- }
24317
24348
  const target = targets.get(pendingOp.tagId);
24318
- if (target?.thinkingDropProtected) {
24319
- reject("active_thinking");
24349
+ const veto = pendingDropVeto(pendingOp.tagId, target, protectedTagIds);
24350
+ if (veto) {
24351
+ reject(veto);
24320
24352
  continue;
24321
24353
  }
24322
24354
  const isToolTag = tagTypeById.get(pendingOp.tagId) === "tool";
@@ -25522,12 +25554,12 @@ function estimateUntaggedInMemoryTailUpperBound(db, sessionId, inMemoryTail, tag
25522
25554
  function getProactiveCompartmentTriggerPercentage(executeThresholdPercentage) {
25523
25555
  return Math.max(0, executeThresholdPercentage - PROACTIVE_TRIGGER_OFFSET_PERCENTAGE);
25524
25556
  }
25525
- function estimateProjectedPostDropPercentage(db, sessionId, usage, activeTags, keepReasoningTokens, clearedReasoningThroughTag, canClearReasoning, liveBudgetCutoff) {
25557
+ function estimateProjectedPostDropPercentage(db, sessionId, usage, activeTags, keepReasoningTokens, clearedReasoningThroughTag, canClearReasoning, liveBudgetCutoff, eligiblePendingDropTagIds) {
25526
25558
  const totalActiveBytes = activeTags.reduce((sum, tag) => sum + tag.byteSize + tag.reasoningByteSize, 0);
25527
25559
  if (totalActiveBytes === 0)
25528
25560
  return null;
25529
25561
  let droppableBytes = 0;
25530
- const pendingDrops = getPendingOps(db, sessionId).filter((op) => op.operation === "drop");
25562
+ const pendingDrops = getPendingOps(db, sessionId).filter((op) => op.operation === "drop" && eligiblePendingDropTagIds?.has(op.tagId) === true);
25531
25563
  const pendingDropTagIds = new Set(pendingDrops.map((op) => op.tagId));
25532
25564
  if (pendingDrops.length > 0) {
25533
25565
  droppableBytes += activeTags.filter((tag) => pendingDropTagIds.has(tag.tagNumber)).reduce((sum, tag) => sum + tag.byteSize + tag.reasoningByteSize, 0);
@@ -25656,7 +25688,7 @@ function getUnsummarizedTailInfo(db, sessionId, triggerBudget, usage, executeThr
25656
25688
  function formatProjectedPostDropPercentage(value) {
25657
25689
  return value === null ? "none" : `${value.toFixed(1)}%`;
25658
25690
  }
25659
- function checkCompartmentTrigger(db, sessionId, sessionMeta, usage, _previousPercentage, executeThresholdPercentage, triggerBudget, keepReasoningTokens, commitClusterTrigger, preloadedActiveTags, contextLimit, inMemoryTail, taggerFloorOverride, reasoningProjection, reclaimRide) {
25691
+ function checkCompartmentTrigger(db, sessionId, sessionMeta, usage, _previousPercentage, executeThresholdPercentage, triggerBudget, keepReasoningTokens, commitClusterTrigger, preloadedActiveTags, contextLimit, inMemoryTail, taggerFloorOverride, reasoningProjection, reclaimRide, eligiblePendingDropTagIds) {
25660
25692
  if (sessionMeta.compartmentInProgress) {
25661
25693
  sessionLog2(sessionId, `compartment trigger: skipped — historian already in progress (usage=${usage.percentage.toFixed(1)}%)`);
25662
25694
  return { shouldFire: false };
@@ -25720,7 +25752,7 @@ function checkCompartmentTrigger(db, sessionId, sessionMeta, usage, _previousPer
25720
25752
  return { shouldFire: false };
25721
25753
  }
25722
25754
  const canClearReasoning = reasoningProjection?.canClearReasoning ?? modelAcceptsEmptyContent(reasoningProjection?.providerID);
25723
- const projectedPostDropPercentage = estimateProjectedPostDropPercentage(db, sessionId, usage, preloadedActiveTags ?? getActiveTagsBySession2(db, sessionId), keepReasoningTokens, sessionMeta.clearedReasoningThroughTag, canClearReasoning, reasoningProjection?.budgetCutoff);
25755
+ const projectedPostDropPercentage = estimateProjectedPostDropPercentage(db, sessionId, usage, preloadedActiveTags ?? getActiveTagsBySession2(db, sessionId), keepReasoningTokens, sessionMeta.clearedReasoningThroughTag, canClearReasoning, reasoningProjection?.budgetCutoff, eligiblePendingDropTagIds);
25724
25756
  const relativePostDropTarget = executeThresholdPercentage * POST_DROP_TARGET_RATIO;
25725
25757
  const rideLabel = reclaimRide ? reclaimRideLabel(reclaimRide) : "ride=none";
25726
25758
  const dropsCanLand = reclaimRide !== undefined && hasReclaimRide(reclaimRide);
@@ -25804,6 +25836,20 @@ function checkCompartmentTrigger(db, sessionId, sessionMeta, usage, _previousPer
25804
25836
  }
25805
25837
 
25806
25838
  // ../plugin/src/hooks/magic-context/degraded-pass-refusal.ts
25839
+ function describeDegradedPassFailure(site, cause) {
25840
+ const details = [site];
25841
+ const seen = new Set;
25842
+ while (cause !== undefined && cause !== null && !seen.has(cause)) {
25843
+ seen.add(cause);
25844
+ const current = cause;
25845
+ const detail = typeof current.site === "string" ? current.site : typeof current.message === "string" ? current.message : String(cause);
25846
+ if (!details.includes(detail))
25847
+ details.push(detail);
25848
+ cause = current.cause;
25849
+ }
25850
+ return `Magic Context could not safely prepare this turn [${userFacingFailureCode("transform_pass_degraded")}] (${details.join(": ")}). This request was not sent.`;
25851
+ }
25852
+
25807
25853
  class DegradedPassRefusalError extends Error {
25808
25854
  site;
25809
25855
  code = "DEGRADED_PASS_REFUSAL";
@@ -25811,7 +25857,7 @@ class DegradedPassRefusalError extends Error {
25811
25857
  estimatedTokens;
25812
25858
  contextLimitTokens;
25813
25859
  constructor(site, options) {
25814
- super(renderUserFacingFailure("transform_pass_degraded", "plain"), {
25860
+ super(describeDegradedPassFailure(site, options?.cause), {
25815
25861
  cause: options?.cause
25816
25862
  });
25817
25863
  this.site = site;
@@ -30595,246 +30641,6 @@ function detectRecentCommit(messages) {
30595
30641
  return false;
30596
30642
  }
30597
30643
 
30598
- // src/tag-snapshot-pi.ts
30599
- function createPiTagSnapshotReader(db) {
30600
- const existing = readers.get(db);
30601
- if (existing)
30602
- return existing;
30603
- db.exec(`
30604
- CREATE TEMP TABLE IF NOT EXISTS pi_tag_revision (revision INTEGER NOT NULL);
30605
- INSERT INTO pi_tag_revision SELECT 0 WHERE NOT EXISTS (SELECT 1 FROM pi_tag_revision);
30606
- CREATE TEMP TABLE IF NOT EXISTS pi_tag_session_revision (session_id TEXT PRIMARY KEY, revision INTEGER NOT NULL);
30607
- CREATE TEMP TABLE IF NOT EXISTS pi_tag_watch (session_id TEXT PRIMARY KEY);
30608
- CREATE TEMP TABLE IF NOT EXISTS pi_tag_changes (session_id TEXT, tag_number INTEGER, PRIMARY KEY (session_id, tag_number));
30609
- CREATE TEMP TABLE IF NOT EXISTS pi_tag_version_own (session_id TEXT PRIMARY KEY, count INTEGER NOT NULL);
30610
- DROP TRIGGER IF EXISTS temp.pi_tag_version_ai;
30611
- DROP TRIGGER IF EXISTS temp.pi_tag_version_ad;
30612
- DROP TRIGGER IF EXISTS temp.pi_tag_version_au;
30613
- DROP TRIGGER IF EXISTS temp.pi_tag_insert;
30614
- DROP TRIGGER IF EXISTS temp.pi_tag_update;
30615
- DROP TRIGGER IF EXISTS temp.pi_tag_delete;
30616
- DROP TRIGGER IF EXISTS temp.pi_tag_insert_conflicts;
30617
- CREATE TEMP TRIGGER pi_tag_insert_conflicts BEFORE INSERT ON main.tags BEGIN
30618
- UPDATE pi_tag_revision SET revision = revision + (SELECT COUNT(*) FROM (${TAG_INSERT_CONFLICT_ROWS}));
30619
- INSERT INTO pi_tag_session_revision
30620
- SELECT session_id, COUNT(*) FROM (${TAG_INSERT_CONFLICT_ROWS}) GROUP BY session_id
30621
- ON CONFLICT(session_id) DO UPDATE SET revision = revision + excluded.revision;
30622
- INSERT INTO pi_tag_changes
30623
- SELECT session_id, tag_number FROM (${TAG_INSERT_CONFLICT_ROWS}) AS conflicts
30624
- WHERE EXISTS (SELECT 1 FROM pi_tag_watch WHERE session_id = conflicts.session_id)
30625
- AND NOT EXISTS (SELECT 1 FROM pi_tag_changes WHERE session_id = conflicts.session_id AND tag_number = conflicts.tag_number)
30626
- GROUP BY session_id, tag_number;
30627
- END;
30628
- CREATE TEMP TRIGGER IF NOT EXISTS pi_tag_insert AFTER INSERT ON main.tags BEGIN
30629
- UPDATE pi_tag_revision SET revision = revision + 1;
30630
- INSERT INTO pi_tag_session_revision VALUES (NEW.session_id, 1) ON CONFLICT(session_id) DO UPDATE SET revision = revision + 1;
30631
- INSERT INTO pi_tag_changes SELECT NEW.session_id, NEW.tag_number WHERE EXISTS (SELECT 1 FROM pi_tag_watch WHERE session_id = NEW.session_id) AND NOT EXISTS (SELECT 1 FROM pi_tag_changes WHERE session_id = NEW.session_id AND tag_number = NEW.tag_number);
30632
- END;
30633
- CREATE TEMP TRIGGER IF NOT EXISTS pi_tag_update AFTER UPDATE ON main.tags BEGIN
30634
- UPDATE pi_tag_revision SET revision = revision + 1;
30635
- INSERT INTO pi_tag_session_revision VALUES (OLD.session_id, 1) ON CONFLICT(session_id) DO UPDATE SET revision = revision + 1;
30636
- INSERT INTO pi_tag_session_revision SELECT NEW.session_id, 1 WHERE NEW.session_id IS NOT OLD.session_id ON CONFLICT(session_id) DO UPDATE SET revision = revision + 1;
30637
- INSERT INTO pi_tag_changes SELECT OLD.session_id, OLD.tag_number WHERE EXISTS (SELECT 1 FROM pi_tag_watch WHERE session_id = OLD.session_id) AND NOT EXISTS (SELECT 1 FROM pi_tag_changes WHERE session_id = OLD.session_id AND tag_number = OLD.tag_number);
30638
- INSERT INTO pi_tag_changes SELECT NEW.session_id, NEW.tag_number WHERE EXISTS (SELECT 1 FROM pi_tag_watch WHERE session_id = NEW.session_id) AND NOT EXISTS (SELECT 1 FROM pi_tag_changes WHERE session_id = NEW.session_id AND tag_number = NEW.tag_number);
30639
- END;
30640
- CREATE TEMP TRIGGER IF NOT EXISTS pi_tag_delete AFTER DELETE ON main.tags BEGIN
30641
- UPDATE pi_tag_revision SET revision = revision + 1;
30642
- INSERT INTO pi_tag_session_revision VALUES (OLD.session_id, 1) ON CONFLICT(session_id) DO UPDATE SET revision = revision + 1;
30643
- INSERT INTO pi_tag_changes SELECT OLD.session_id, OLD.tag_number WHERE EXISTS (SELECT 1 FROM pi_tag_watch WHERE session_id = OLD.session_id) AND NOT EXISTS (SELECT 1 FROM pi_tag_changes WHERE session_id = OLD.session_id AND tag_number = OLD.tag_number);
30644
- END;
30645
- CREATE TEMP TRIGGER pi_tag_version_ai AFTER INSERT ON main.tags BEGIN
30646
- INSERT INTO pi_tag_version_own VALUES (NEW.session_id, 1) ON CONFLICT(session_id) DO UPDATE SET count = count + 1;
30647
- END;
30648
- CREATE TEMP TRIGGER pi_tag_version_ad AFTER DELETE ON main.tags BEGIN
30649
- INSERT INTO pi_tag_version_own VALUES (OLD.session_id, 1) ON CONFLICT(session_id) DO UPDATE SET count = count + 1;
30650
- END;
30651
- CREATE TEMP TRIGGER pi_tag_version_au
30652
- AFTER UPDATE OF session_id, message_id, tag_number, type, tool_owner_message_id, status ON main.tags BEGIN
30653
- INSERT INTO pi_tag_version_own VALUES (OLD.session_id, 1) ON CONFLICT(session_id) DO UPDATE SET count = count + 1;
30654
- INSERT INTO pi_tag_version_own SELECT NEW.session_id, 1 WHERE NEW.session_id != OLD.session_id
30655
- ON CONFLICT(session_id) DO UPDATE SET count = count + 1;
30656
- END;
30657
- `);
30658
- const versionStamp = db.prepare(`SELECT
30659
- COALESCE((SELECT tags_version FROM session_meta WHERE session_id = ?), 0) AS durable,
30660
- COALESCE((SELECT count FROM pi_tag_version_own WHERE session_id = ?), 0) AS own`);
30661
- const revision = db.prepare(`WITH stamp AS (SELECT
30662
- COALESCE((SELECT revision FROM pi_tag_session_revision WHERE session_id = ?), 0) AS own,
30663
- COALESCE((SELECT value FROM schema_migrations_meta WHERE key = ?), '0') AS total)
30664
- SELECT CAST(own AS TEXT) AS local,
30665
- (${tagRevisionIncarnationSql("total")}) || ':' || CAST(${tagRevisionCountSql("total")} - own AS TEXT) AS foreign_revision FROM stamp`);
30666
- revisionReaders.set(db, (sessionId) => {
30667
- const transaction = db;
30668
- if (transaction.inTransaction || transaction.isTransaction)
30669
- return null;
30670
- const row = revision.get(sessionId, tagRevisionKey(sessionId));
30671
- return [row.local, row.foreign_revision];
30672
- });
30673
- const changed = db.prepare("SELECT tag_number FROM pi_tag_changes WHERE session_id = ? ORDER BY tag_number");
30674
- const clearChanged = db.prepare("DELETE FROM pi_tag_changes WHERE session_id = ?");
30675
- const watch = db.prepare("INSERT OR IGNORE INTO pi_tag_watch VALUES (?)");
30676
- const unwatch = db.prepare("DELETE FROM pi_tag_watch WHERE session_id = ?");
30677
- const cache = new Map;
30678
- const fallbackRows = db.prepare(`SELECT ${FALLBACK_ROW_COLUMNS} FROM tags WHERE session_id = ? AND ${FALLBACK_ROW_PREDICATE}`);
30679
- const fallbackRowsByNumbers = db.prepare(`SELECT ${FALLBACK_ROW_COLUMNS} FROM tags WHERE session_id = ?
30680
- AND tag_number IN (SELECT value FROM json_each(?)) AND ${FALLBACK_ROW_PREDICATE}`);
30681
- const sync = (sessionId) => {
30682
- const transaction = db;
30683
- if (transaction.inTransaction || transaction.isTransaction)
30684
- return null;
30685
- const [local, external] = revisionReaders.get(db)?.(sessionId) ?? [
30686
- "0",
30687
- "0:0"
30688
- ];
30689
- const stamp = versionStamp.get(sessionId, sessionId);
30690
- let snapshot = cache.get(sessionId);
30691
- if (!snapshot || snapshot.foreignRevision !== external) {
30692
- const tags = snapshot ? reuseUnchangedRows(snapshot.tags, getTagsBySession2(db, sessionId)) : getTagsBySession2(db, sessionId);
30693
- snapshot = {
30694
- revision: local,
30695
- foreignRevision: external,
30696
- tags,
30697
- active: tags.filter(isActive),
30698
- durableVersion: stamp.durable,
30699
- ownVersion: stamp.own
30700
- };
30701
- if (cache.size >= 100 && !cache.has(sessionId)) {
30702
- const oldest = cache.keys().next().value;
30703
- if (oldest !== undefined) {
30704
- cache.delete(oldest);
30705
- unwatch.run(oldest);
30706
- clearChanged.run(oldest);
30707
- }
30708
- }
30709
- cache.set(sessionId, snapshot);
30710
- for (const tag of snapshot.tags)
30711
- Object.freeze(tag);
30712
- watch.run(sessionId);
30713
- } else if (snapshot.revision !== local) {
30714
- const numbers = changed.all(sessionId).map((row) => row.tag_number);
30715
- if (numbers.length > 0) {
30716
- const dirty = new Set(numbers);
30717
- const replacements = getTagsByNumbers2(db, sessionId, numbers);
30718
- for (const tag of replacements)
30719
- Object.freeze(tag);
30720
- snapshot.tags = mergeChangedRows(snapshot.tags, dirty, replacements);
30721
- snapshot.active = mergeChangedRows(snapshot.active, dirty, replacements.filter(isActive));
30722
- if (!numbers.every(Number.isSafeInteger)) {
30723
- snapshot.fallback = undefined;
30724
- } else if (snapshot.fallback) {
30725
- snapshot.fallback = [
30726
- ...snapshot.fallback.filter((row) => !dirty.has(row.tag_number)),
30727
- ...fallbackRowsByNumbers.all(sessionId, JSON.stringify(numbers))
30728
- ];
30729
- }
30730
- }
30731
- snapshot.revision = local;
30732
- }
30733
- if (stamp.durable - snapshot.durableVersion !== stamp.own - snapshot.ownVersion) {
30734
- snapshot.fallback = undefined;
30735
- }
30736
- snapshot.durableVersion = stamp.durable;
30737
- snapshot.ownVersion = stamp.own;
30738
- clearChanged.run(sessionId);
30739
- return snapshot;
30740
- };
30741
- const read = (sessionId, borrowed = false) => {
30742
- const snapshot = sync(sessionId);
30743
- if (!snapshot)
30744
- return getTagsBySession2(db, sessionId);
30745
- return borrowed ? snapshot.tags : snapshot.tags.map((tag) => ({ ...tag }));
30746
- };
30747
- entryReaders.set(db, sync);
30748
- fallbackReaders.set(db, (sessionId) => {
30749
- const snapshot = sync(sessionId);
30750
- if (!snapshot)
30751
- return null;
30752
- snapshot.fallback ??= fallbackRows.all(sessionId);
30753
- return snapshot.fallback;
30754
- });
30755
- readers.set(db, read);
30756
- return read;
30757
- }
30758
- var FALLBACK_ROW_COLUMNS = "id, tag_number, type, message_id, tool_owner_message_id, entry_fingerprint";
30759
- var FALLBACK_ROW_PREDICATE = `((type = 'message' AND message_id LIKE 'pi-msg-%')
30760
- OR (type = 'tool' AND tool_owner_message_id LIKE 'pi-msg-%'))`;
30761
- var isActive = (tag) => tag.status === "active";
30762
- function sameRow(left, right, keys) {
30763
- for (const key of keys)
30764
- if (!Object.is(left[key], right[key]))
30765
- return false;
30766
- return true;
30767
- }
30768
- function reuseUnchangedRows(cached, fresh) {
30769
- const first = fresh[0];
30770
- if (!first)
30771
- return fresh;
30772
- const keys = Object.keys(first);
30773
- let index = 0;
30774
- for (let position = 0;position < fresh.length; position += 1) {
30775
- const row = fresh[position];
30776
- if (!row)
30777
- continue;
30778
- while (index < cached.length) {
30779
- const old = cached[index];
30780
- if (!old || old.tagNumber > row.tagNumber || old.tagNumber === row.tagNumber && (old.id ?? 0) >= (row.id ?? 0))
30781
- break;
30782
- index += 1;
30783
- }
30784
- const old = cached[index];
30785
- if (old && old.id === row.id && sameRow(old, row, keys))
30786
- fresh[position] = old;
30787
- }
30788
- return fresh;
30789
- }
30790
- function mergeChangedRows(rows, dirty, replacements) {
30791
- const highest = rows.at(-1)?.tagNumber ?? -1;
30792
- let appendOnly = true;
30793
- for (const number of dirty) {
30794
- if (number <= highest) {
30795
- appendOnly = false;
30796
- break;
30797
- }
30798
- }
30799
- if (appendOnly)
30800
- return [...rows, ...replacements];
30801
- const unchanged = rows.filter((tag) => !dirty.has(tag.tagNumber));
30802
- const merged = [];
30803
- let index = 0;
30804
- for (const tag of unchanged) {
30805
- while (index < replacements.length) {
30806
- const next = replacements[index];
30807
- if (!next)
30808
- break;
30809
- if (next.tagNumber > tag.tagNumber || next.tagNumber === tag.tagNumber && (next.id ?? 0) > (tag.id ?? 0))
30810
- break;
30811
- merged.push(next);
30812
- index += 1;
30813
- }
30814
- merged.push(tag);
30815
- }
30816
- merged.push(...replacements.slice(index));
30817
- return merged;
30818
- }
30819
- var entryReaders = new WeakMap;
30820
- var fallbackReaders = new WeakMap;
30821
- var readers = new WeakMap;
30822
- var revisionReaders = new WeakMap;
30823
- function getPiTagSnapshotView(db, sessionId) {
30824
- return createPiTagSnapshotReader(db)(sessionId, true);
30825
- }
30826
- function getPiActiveTagSnapshotView(db, sessionId, passTags) {
30827
- createPiTagSnapshotReader(db);
30828
- const snapshot = entryReaders.get(db)?.(sessionId);
30829
- if (snapshot && (!passTags || snapshot.tags === passTags))
30830
- return snapshot.active.slice();
30831
- return (passTags ?? getTagsBySession2(db, sessionId)).filter(isActive);
30832
- }
30833
- function readPiFallbackTagRows(db, sessionId) {
30834
- createPiTagSnapshotReader(db);
30835
- return fallbackReaders.get(db)?.(sessionId) ?? null;
30836
- }
30837
-
30838
30644
  // src/fallback-tag-probes-pi.ts
30839
30645
  function probe(db, sessionId, kind) {
30840
30646
  const rows = readPiFallbackTagRows(db, sessionId);
@@ -34256,6 +34062,9 @@ class PiContextBudget {
34256
34062
  writerAllowance() {
34257
34063
  return Math.max(0, Math.min(PI_CONTEXT_BUDGET.writerMs, this.remainingWork() - PI_CONTEXT_BUDGET.completionReserveMs));
34258
34064
  }
34065
+ hydrationAllowance() {
34066
+ return Math.max(0, Math.min(this.remainingWork() - PI_CONTEXT_BUDGET.completionReserveMs, this.remainingOutcome() - PI_CONTEXT_BUDGET.completionReserveMs));
34067
+ }
34259
34068
  assertOutcome = () => {
34260
34069
  this.assertOwner();
34261
34070
  this.signal?.throwIfAborted();
@@ -35933,550 +35742,825 @@ function piPreparedStructureMismatch(payload, prepared, onImageTrim) {
35933
35742
  }
35934
35743
  }
35935
35744
 
35936
- // src/pi-context-refusal.ts
35937
- var ENTRY_TYPE = "magic-context-turn-refused";
35938
- var RETRY_MESSAGE = "Magic Context could not safely prepare this turn; send your message again.";
35939
- var MAX_UNSENDABLE_NONCES_PER_SESSION = 32;
35940
- var receiptMaps = new Set;
35941
- function clearPiContextReceipt(sessionId) {
35942
- for (const receipts of receiptMaps)
35943
- receipts.delete(sessionId);
35944
- }
35945
- function createPiContextOwnershipGuards(operations, key, active, budget, owner) {
35946
- const sideTurn = budget.sideTurn;
35947
- const signal = budget.signal;
35948
- const isOwned = () => sideTurn ? operations.get(key) === owner : operations.get(key) === active && active.pass === budget;
35949
- const isCurrent = () => isOwned() && (sideTurn || !active.ended);
35950
- const assertOwner = () => {
35951
- if (!isCurrent())
35952
- throw new PiContextSupersededError("Pi context pass superseded: no longer valid for this operation", active.ended ? "agent_end" : operations.get(key) !== (sideTurn ? owner : active) ? "operation replacement or cache eviction" : "newer context pass");
35953
- signal?.throwIfAborted();
35745
+ // src/pi-lkg.ts
35746
+ var piLkgSessionStates = new Map;
35747
+ function completedProviderUsage(message) {
35748
+ if (!isRecord(message) || message.role !== "assistant" || !["stop", "length", "toolUse"].includes(String(message.stopReason)) || typeof message.provider !== "string" || !message.provider || typeof message.model !== "string" || !message.model || typeof message.timestamp !== "number" || !Number.isFinite(message.timestamp) || !isRecord(message.usage))
35749
+ return;
35750
+ const usage = message.usage;
35751
+ const counts = [usage.input, usage.cacheRead, usage.cacheWrite];
35752
+ if (!counts.every((count) => typeof count === "number" && Number.isSafeInteger(count) && count >= 0))
35753
+ return;
35754
+ let inputTokens = usage.input + usage.cacheRead + usage.cacheWrite;
35755
+ if (typeof usage.totalTokens === "number" && Number.isSafeInteger(usage.totalTokens) && typeof usage.output === "number" && Number.isSafeInteger(usage.output) && usage.output >= 0) {
35756
+ inputTokens = Math.max(inputTokens, usage.totalTokens - usage.output);
35757
+ }
35758
+ if (!Number.isSafeInteger(inputTokens) || inputTokens <= 0)
35759
+ return;
35760
+ return {
35761
+ inputTokens,
35762
+ modelKey: `${message.provider}/${message.model}`,
35763
+ providerKey: message.provider,
35764
+ timestamp: message.timestamp,
35765
+ signature: JSON.stringify([
35766
+ message.provider,
35767
+ message.model,
35768
+ message.timestamp,
35769
+ message.stopReason,
35770
+ usage.input,
35771
+ usage.cacheRead,
35772
+ usage.cacheWrite,
35773
+ usage.output,
35774
+ usage.totalTokens
35775
+ ])
35954
35776
  };
35955
- return { isOwned, isCurrent, assertOwner };
35956
35777
  }
35957
- function releasePiOperationPayloads(operation) {
35958
- operation.inputKeys = undefined;
35959
- operation.inputMessages = undefined;
35960
- operation.inputBranch = undefined;
35961
- operation.dispatchFields = undefined;
35962
- operation.managedSignature = undefined;
35963
- operation.preparedStructure = undefined;
35778
+ function notePiLkgProviderUsage(sessionId, parentInputId, message) {
35779
+ try {
35780
+ const state = piLkgSessionStates.get(sessionId);
35781
+ const request = state?.capturedRequest;
35782
+ const usage = completedProviderUsage(message);
35783
+ if (!request || request.usage || !usage || request.captureSequence !== state?.captureSequence || !parentInputId || parentInputId.startsWith("pi-lkg-unmapped:") || parentInputId !== request.inputs.at(-1)?.id || usage.timestamp < request.capturedAt || usage.modelKey !== request.modelKey || usage.providerKey !== request.providerKey)
35784
+ return false;
35785
+ request.usage = {
35786
+ signature: usage.signature,
35787
+ inputTokens: usage.inputTokens
35788
+ };
35789
+ if (state)
35790
+ state.measuredRequest = { ...request, usage: request.usage };
35791
+ return true;
35792
+ } catch {
35793
+ return false;
35794
+ }
35964
35795
  }
35965
- function operationKey(ctx) {
35966
- return ctx.sessionManager?.getSessionId?.() ?? ctx.sessionManager ?? "no-session";
35796
+ function clearPiLkgSessionState(sessionId) {
35797
+ const state = piLkgSessionStates.get(sessionId);
35798
+ if (state)
35799
+ state.captureSequence += 1;
35800
+ piLkgSessionStates.delete(sessionId);
35801
+ clearLkgFenceProcessState(sessionId);
35967
35802
  }
35968
- function messageKey(message) {
35969
- if (!message || typeof message !== "object")
35970
- return;
35971
- const { role, timestamp } = message;
35972
- return typeof role === "string" && typeof timestamp === "number" ? `${role}:${timestamp}` : undefined;
35803
+
35804
+ class PiLkgUncapturable extends Error {
35805
+ reason;
35806
+ constructor(reason, message, cause) {
35807
+ super(message, { cause });
35808
+ this.reason = reason;
35809
+ }
35973
35810
  }
35974
- function endBelongsToEarlierRun(operation, event) {
35975
- if (!operation.pass || operation.dispatched)
35811
+ function isTransientPiStorageError(error) {
35812
+ if (!error || typeof error !== "object")
35976
35813
  return false;
35977
- const end = event;
35978
- if (end?.willContinue === true)
35814
+ const candidate = error;
35815
+ if (candidate.code === "SQLITE_BUSY" || candidate.code === "SQLITE_LOCKED" || candidate.code === "SQLITE_BUSY_SNAPSHOT") {
35979
35816
  return true;
35980
- if (!operation.inputKeys || !Array.isArray(end?.messages))
35981
- return false;
35982
- const ended = new Set;
35983
- for (const message of end.messages) {
35984
- const key = messageKey(message);
35985
- if (key)
35986
- ended.add(key);
35987
35817
  }
35988
- if (ended.size === 0)
35989
- return false;
35990
- return operation.inputKeys.some((key) => !ended.has(key));
35818
+ return typeof candidate.message === "string" && /database is locked|database table is locked|sqlite_(busy|locked)/i.test(candidate.message);
35991
35819
  }
35992
- function messageSnapshots(messages) {
35993
- const snapshots = [];
35994
- for (const message of messages) {
35995
- const id = messageKey(message);
35996
- const fields = lkgContentFields(message);
35997
- if (!id || !fields)
35820
+ function reconcilePiLkgEntryIds(resolved, alignedProjection) {
35821
+ if (!resolved || !alignedProjection || resolved.length === 0)
35822
+ return resolved;
35823
+ const result = [...resolved];
35824
+ const anchors = [];
35825
+ let projectionCursor = 0;
35826
+ for (let messageIndex = 0;messageIndex < resolved.length; messageIndex += 1) {
35827
+ const id = resolved[messageIndex];
35828
+ if (typeof id !== "string")
35829
+ continue;
35830
+ let projectionIndex = -1;
35831
+ for (let index = projectionCursor;index < alignedProjection.length; index += 1) {
35832
+ if (alignedProjection[index] === id) {
35833
+ projectionIndex = index;
35834
+ break;
35835
+ }
35836
+ }
35837
+ if (projectionIndex < 0)
35838
+ return resolved;
35839
+ anchors.push({ messageIndex, projectionIndex });
35840
+ projectionCursor = projectionIndex + 1;
35841
+ }
35842
+ if (anchors.length === 0)
35843
+ return resolved;
35844
+ const fillEqualSpan = (messageStart, messageEnd, projectionStart, projectionEnd) => {
35845
+ if (messageEnd - messageStart !== projectionEnd - projectionStart)
35998
35846
  return;
35999
- snapshots.push({ id, fields });
35847
+ for (let offset = 0;offset < messageEnd - messageStart; offset += 1) {
35848
+ result[messageStart + offset] ??= alignedProjection[projectionStart + offset];
35849
+ }
35850
+ };
35851
+ let previousMessageIndex = -1;
35852
+ let previousProjectionIndex = -1;
35853
+ for (const anchor of anchors) {
35854
+ fillEqualSpan(previousMessageIndex + 1, anchor.messageIndex, previousProjectionIndex + 1, anchor.projectionIndex);
35855
+ previousMessageIndex = anchor.messageIndex;
35856
+ previousProjectionIndex = anchor.projectionIndex;
36000
35857
  }
36001
- return snapshots.length ? snapshots : undefined;
35858
+ fillEqualSpan(previousMessageIndex + 1, resolved.length, previousProjectionIndex + 1, alignedProjection.length);
35859
+ return result;
36002
35860
  }
36003
- function inputBranch(ctx, inputs) {
36004
- try {
36005
- const branch = ctx.sessionManager.getBranch();
36006
- const entries = branch.filter((entry) => entry.type === "message" || entry.type === "custom_message");
36007
- const anchorId = entries.at(-1)?.id;
36008
- const native = messageSnapshots(entries.map((entry) => {
36009
- if (entry.type === "message")
36010
- return entry.message;
36011
- const attribution = entry.attribution;
36012
- const timestamp = new Date(entry.timestamp).getTime();
36013
- if (!Number.isFinite(timestamp) || !entry.customType || typeof entry.content !== "string" && !Array.isArray(entry.content) || attribution !== undefined && attribution !== "agent" && attribution !== "user")
36014
- return;
36015
- return {
36016
- role: "custom",
36017
- customType: entry.customType,
36018
- content: entry.content,
36019
- display: entry.display,
36020
- details: entry.details,
36021
- attribution,
36022
- timestamp
36023
- };
36024
- }));
36025
- inputs ??= native;
36026
- if (!anchorId || !native || !inputs || native.length > inputs.length || exactReusablePrefix(native, inputs) !== native.length)
36027
- return;
36028
- return {
36029
- anchorId,
36030
- anchorIndex: native.length - 1,
36031
- entryIds: entries.map((entry) => entry.id),
36032
- messages: inputs
36033
- };
36034
- } catch {
36035
- return;
35861
+ function piStorageErrorReason(error) {
35862
+ const message = error instanceof Error ? error.message : String(error);
35863
+ if (error && typeof error === "object") {
35864
+ const code = error.code;
35865
+ if (code === "SQLITE_BUSY" || code === "SQLITE_LOCKED" || code === "SQLITE_BUSY_SNAPSHOT") {
35866
+ return code;
35867
+ }
35868
+ }
35869
+ if (/database is locked|database table is locked/i.test(message)) {
35870
+ return "SQLITE_BUSY_OR_LOCKED";
35871
+ }
35872
+ if (error && typeof error === "object") {
35873
+ const code = error.code;
35874
+ if (typeof code === "string" && code.length > 0)
35875
+ return code;
36036
35876
  }
35877
+ return message;
36037
35878
  }
36038
- function dispatchedEndBelongsToEarlierRun(operation, event, ctx) {
36039
- if (!operation.dispatched || !operation.started || !operation.ready || !operation.pass?.completed || operation.refused || operation.pass.abandoned || !operation.inputBranch)
36040
- return false;
36041
- try {
36042
- if (ctx.isIdle() !== false)
36043
- return false;
36044
- const current = inputBranch(ctx);
36045
- const prepared = operation.inputBranch;
36046
- if (!current || current.entryIds[prepared.anchorIndex] !== prepared.anchorId || current.messages.length !== prepared.messages.length || exactReusablePrefix(current.messages, prepared.messages) !== prepared.messages.length)
36047
- return false;
36048
- const messages = event?.messages;
36049
- const ended = Array.isArray(messages) ? messageSnapshots(messages) : undefined;
36050
- return !!(ended && ended.length <= prepared.anchorIndex && ended.length < prepared.messages.length && exactReusablePrefix(prepared.messages, ended) === ended.length);
36051
- } catch {
35879
+ function plainJsonFields(value) {
35880
+ const fields = [];
35881
+ const seen = new WeakSet;
35882
+ const visit = (child) => {
35883
+ if (child === null)
35884
+ fields.push(LKG_SNAPSHOT_NULL);
35885
+ else if (typeof child === "string")
35886
+ fields.push(LKG_SNAPSHOT_STRING, child);
35887
+ else if (typeof child === "number")
35888
+ fields.push(LKG_SNAPSHOT_NUMBER, child);
35889
+ else if (typeof child === "boolean")
35890
+ fields.push(LKG_SNAPSHOT_BOOLEAN, child);
35891
+ else if (typeof child !== "object")
35892
+ fields.push(LKG_SNAPSHOT_UNDEFINED);
35893
+ else {
35894
+ if (seen.has(child))
35895
+ return false;
35896
+ const array = Array.isArray(child);
35897
+ const prototype = Object.getPrototypeOf(child);
35898
+ if (!array && prototype !== Object.prototype && prototype !== null || "toJSON" in child)
35899
+ return false;
35900
+ const keys = Object.keys(child);
35901
+ if (array && keys.length !== child.length)
35902
+ return false;
35903
+ seen.add(child);
35904
+ fields.push(array ? LKG_SNAPSHOT_ARRAY : LKG_SNAPSHOT_OBJECT);
35905
+ const countIndex = fields.length;
35906
+ fields.push(array ? child.length : 0);
35907
+ let count = 0;
35908
+ for (const key of keys) {
35909
+ const descriptor = Object.getOwnPropertyDescriptor(child, key);
35910
+ if (!descriptor || !("value" in descriptor) || array && key !== String(count))
35911
+ return false;
35912
+ const entry = descriptor.value;
35913
+ if (!array && (entry === undefined || typeof entry === "function" || typeof entry === "symbol"))
35914
+ continue;
35915
+ count++;
35916
+ if (!array)
35917
+ fields.push(LKG_SNAPSHOT_KEY, key);
35918
+ if (!visit(entry))
35919
+ return false;
35920
+ }
35921
+ if (!array)
35922
+ fields[countIndex] = count;
35923
+ seen.delete(child);
35924
+ }
35925
+ return true;
35926
+ };
35927
+ if (!visit(value))
35928
+ return null;
35929
+ const normalized = contentSnapshotValue(value);
35930
+ return normalized === value ? fields : lkgContentFields(normalized);
35931
+ }
35932
+ function plainOutputFields(messages) {
35933
+ if ("toJSON" in messages || Object.keys(messages).length !== messages.length)
35934
+ return null;
35935
+ const outputs = [];
35936
+ for (let index = 0;index < messages.length; index++) {
35937
+ const descriptor = Object.getOwnPropertyDescriptor(messages, String(index));
35938
+ if (!descriptor || !("value" in descriptor))
35939
+ return null;
35940
+ const fields = plainJsonFields(descriptor.value);
35941
+ if (!fields)
35942
+ return null;
35943
+ outputs.push({ id: String(index), fields });
35944
+ }
35945
+ return outputs;
35946
+ }
35947
+ function snapshotInputs(messages, entryIds, omitsBookkeeping) {
35948
+ if (!entryIds || entryIds.length !== messages.length) {
35949
+ return { inputs: [], failure: "lkg_entry_ids_unavailable" };
35950
+ }
35951
+ const firstStableIndex = entryIds.findIndex((id) => typeof id === "string" && id.length > 0);
35952
+ if (firstStableIndex < 0) {
35953
+ return { inputs: [], failure: "lkg_entry_ids_unavailable" };
35954
+ }
35955
+ const inputs = [];
35956
+ const seen = new Set;
35957
+ for (let index = 0;index < entryIds.length; index += 1) {
35958
+ const fields = lkgContentFields(omitsBookkeeping ? piProviderInput(messages[index]) : messages[index]);
35959
+ if (!fields)
35960
+ return { inputs: [], failure: "lkg_content_snapshot_failed" };
35961
+ const id = entryIds[index] || `pi-lkg-unmapped:${lkgContentDigestFromFields(fields)}`;
35962
+ if (seen.has(id))
35963
+ return { inputs: [], failure: "lkg_duplicate_entry_id" };
35964
+ seen.add(id);
35965
+ inputs.push({
35966
+ id,
35967
+ messageIndex: index,
35968
+ fields,
35969
+ providerUsageSignature: completedProviderUsage(messages[index])?.signature
35970
+ });
35971
+ }
35972
+ return { inputs, failure: null };
35973
+ }
35974
+ var bookkeepingOmittingApis = new Set([
35975
+ "anthropic-messages",
35976
+ "google-generative-ai",
35977
+ "openai-completions",
35978
+ "openai-responses",
35979
+ "openai-codex-responses"
35980
+ ]);
35981
+ function omitsRootBookkeeping(args) {
35982
+ if (args.transport != null)
36052
35983
  return false;
35984
+ let api = args.apiKey;
35985
+ if (api === undefined) {
35986
+ const apis = new Set(args.messages.flatMap((message) => {
35987
+ const value = message;
35988
+ return value?.role === "assistant" && typeof value.api === "string" ? [value.api] : [];
35989
+ }));
35990
+ if (apis.size === 1)
35991
+ api = [...apis][0];
36053
35992
  }
35993
+ return typeof api === "string" && bookkeepingOmittingApis.has(api);
36054
35994
  }
36055
- function inputMessageKeys(event) {
36056
- try {
36057
- const messages = event.messages;
36058
- if (!Array.isArray(messages))
35995
+ function piProviderInput(message) {
35996
+ if (!message || typeof message !== "object" || Array.isArray(message))
35997
+ return message;
35998
+ const descriptors = Object.getOwnPropertyDescriptors(message);
35999
+ delete descriptors.completedAt;
36000
+ delete descriptors.contextSnapshot;
36001
+ return Object.create(Object.getPrototypeOf(message), descriptors);
36002
+ }
36003
+ function restorePiLkgInputForFit(snapshot, branchEntries) {
36004
+ if (!snapshot || snapshot.preparationFailure || !branchEntries || !snapshot.inputs.length)
36005
+ return;
36006
+ const byId = new Map;
36007
+ for (const value of branchEntries) {
36008
+ const entry = value;
36009
+ if (entry?.type !== "message" || typeof entry.id !== "string")
36010
+ continue;
36011
+ if (byId.has(entry.id))
36059
36012
  return;
36060
- const keys = [];
36061
- for (const message of messages) {
36062
- const key = messageKey(message);
36063
- if (key)
36064
- keys.push(key);
36065
- }
36066
- return keys;
36013
+ byId.set(entry.id, entry.message);
36014
+ }
36015
+ const input = [];
36016
+ for (const saved of snapshot.inputs) {
36017
+ if (!byId.has(saved.id))
36018
+ return;
36019
+ const message = byId.get(saved.id);
36020
+ if (!message || typeof message !== "object" || Array.isArray(message) || Object.hasOwn(message, "parts"))
36021
+ return;
36022
+ const fields = plainJsonFields(snapshot.omitsBookkeeping ? piProviderInput(message) : message);
36023
+ if (!fields || fields.length !== saved.fields.length || fields.some((field, i) => field !== saved.fields[i]))
36024
+ return;
36025
+ input.push(message);
36026
+ }
36027
+ try {
36028
+ return structuredClone(input);
36067
36029
  } catch {
36068
36030
  return;
36069
36031
  }
36070
36032
  }
36071
- function registerPiGuardedContext(pi, handler, options = {}) {
36072
- const operations = new Map;
36073
- const sideSessions = new Map;
36074
- receiptMaps.add(operations);
36075
- const newOperation = () => ({
36076
- retainedAfterEnd: false,
36077
- dispatched: false,
36078
- started: false,
36079
- ready: false,
36080
- refused: false,
36081
- ended: false
36082
- });
36083
- const lifecycle = new Map;
36084
- receiptMaps.add(lifecycle);
36085
- const lifecycleCounts = (key) => {
36086
- let counts = lifecycle.get(key);
36087
- if (!counts) {
36088
- counts = { starts: 0, ends: 0 };
36089
- lifecycle.set(key, counts);
36033
+ function extendsMeasuredRequest(measuredJson, nextJson) {
36034
+ return measuredJson === nextJson || measuredJson.length > 2 && nextJson.startsWith(`${measuredJson.slice(0, -1)},`);
36035
+ }
36036
+ function createPiLkgCoordinator(db, scheduleCapture = (capture) => setImmediate(capture), onCaptureTiming) {
36037
+ registerLkgPersistence(createDbLkgPersistence(db));
36038
+ const stateFor = (sessionId) => {
36039
+ let state = piLkgSessionStates.get(sessionId);
36040
+ if (!state) {
36041
+ state = {
36042
+ captureSequence: 0,
36043
+ syncCaptureRequired: false,
36044
+ acceptedInputs: null,
36045
+ acceptedSlot: null,
36046
+ outputSnapshot: null
36047
+ };
36048
+ piLkgSessionStates.set(sessionId, state);
36090
36049
  }
36091
- return counts;
36050
+ return state;
36092
36051
  };
36093
- const fenceEnabled = resolvePiHarnessKind() === "omp";
36094
- const unsendableNonces = new Map;
36095
- receiptMaps.add(unsendableNonces);
36096
- const structureWarnings = new Map;
36097
- receiptMaps.add(structureWarnings);
36098
- const imageTrimNotices = new Map;
36099
- receiptMaps.add(imageTrimNotices);
36100
- const refuse = (ctx, budget, reason, isCurrent) => {
36101
- if (!isCurrent()) {
36102
- log2(`[magic-context][pi] DISCARDED CONTEXT RESULT ${budget.diagnostic()}`, reason);
36103
- return;
36104
- }
36105
- budget.abandoned = true;
36106
- budget.recovery = budget.recovery === "not attempted" ? "no managed result; refused" : budget.recovery;
36107
- const identityConflict = findPiTagIdentityConflict2(reason);
36108
- const message = identityConflict ? `${identityConflict.message}. ${budget.diagnostic()}` : `${RETRY_MESSAGE} ${budget.diagnostic()} (${reason instanceof Error ? reason.message : String(reason)})`;
36109
- log2("[magic-context][pi] CONTEXT REFUSED", message);
36110
- try {
36111
- ctx.ui?.notify(message, "error");
36112
- } catch (error) {
36113
- log2("[magic-context][pi] refusal notice failed", error);
36114
- }
36115
- try {
36116
- pi.appendEntry(ENTRY_TYPE, { message });
36117
- } catch (error) {
36118
- log2("[magic-context][pi] refusal entry failed", error);
36119
- } finally {
36120
- if (!budget.sideTurn)
36121
- ctx.abort();
36052
+ const recoveryFor = (snapshot) => {
36053
+ const state = stateFor(snapshot.sessionId);
36054
+ const prefix = state.recoveryPrefix;
36055
+ if (!prefix) {
36056
+ const slot = getSlot(snapshot.sessionId);
36057
+ return lkgRecoveryFenceUnclean(snapshot.sessionId) || slot && lkgFenceRequiresRebuild(snapshot.sessionId, state.acceptedInputs ? slot : loadPersistedLkgSlot(db, snapshot.sessionId) ?? slot, !!state.acceptedInputs) ? { rebuildReason: "lkg_restart_rebuild" } : {};
36122
36058
  }
36123
- try {
36124
- options.onRefusal?.(ctx, budget, message, isCurrent);
36125
- } catch (error) {
36126
- log2("[magic-context][pi] refusal diagnostic failed", error);
36059
+ const durableSlot = loadPersistedLkgSlot(db, snapshot.sessionId) ?? getSlot(snapshot.sessionId);
36060
+ if (snapshot.preparationFailure || !durableSlot || !ownsLkgDirtyFence(snapshot.sessionId, prefix.fenceRevision) || prefix.modelKey !== snapshot.modelKey || prefix.providerKey !== snapshot.providerKey || JSON.stringify(prefix.systemEntries) !== JSON.stringify(snapshot.systemEntries) || exactReusablePrefix(snapshot.inputs, prefix.inputs) !== prefix.inputs.length)
36061
+ return { rebuildReason: "lkg_recovery_input_rebuild" };
36062
+ return { prefix };
36063
+ };
36064
+ const recoveryEntryIds = (snapshot) => {
36065
+ const anchor = snapshot.replayAnchorInputIndex;
36066
+ const slot = getSlot(snapshot.sessionId);
36067
+ if (snapshot.preparationFailure || anchor === null || !slot?.piOutputEntryIds)
36068
+ throw new PiLkgUncapturable("lkg_output_mapping_unavailable", "Cannot retain recovered output ownership");
36069
+ const surviving = new Set(snapshot.inputs.map((input) => input.id));
36070
+ return [
36071
+ ...slot.piOutputEntryIds.filter((id) => id === null || surviving.has(id)),
36072
+ ...snapshot.inputs.slice(anchor + 1).map((input) => input.id)
36073
+ ];
36074
+ };
36075
+ const noteRecovery = (snapshot, messages, entryIds) => {
36076
+ const state = stateFor(snapshot.sessionId);
36077
+ const slot = getSlot(snapshot.sessionId);
36078
+ if (!slot)
36079
+ throw new PiLkgUncapturable("lkg_miss", "Cannot retain recovery without a saved request");
36080
+ const anchor = snapshot.replayAnchorInputIndex;
36081
+ if (anchor === null)
36082
+ throw new PiLkgUncapturable("lkg_miss", "Cannot retain recovery without an input anchor");
36083
+ const outputEntryIds = entryIds ?? recoveryEntryIds(snapshot);
36084
+ if (outputEntryIds.length !== messages.length)
36085
+ throw new PiLkgUncapturable("lkg_output_mapping_unavailable", "Recovered output ownership does not match the served array");
36086
+ state.recoveryPrefix = {
36087
+ fenceRevision: markLkgRecoveryDirty({
36088
+ sessionId: snapshot.sessionId,
36089
+ slot,
36090
+ processLocalProof: !!state.acceptedInputs,
36091
+ ownDirtyRevision: state.recoveryPrefix?.fenceRevision
36092
+ }),
36093
+ modelKey: snapshot.modelKey,
36094
+ providerKey: snapshot.providerKey,
36095
+ systemEntries: snapshot.systemEntries,
36096
+ inputs: snapshot.inputs,
36097
+ json: JSON.stringify(messages),
36098
+ outputEntryIds,
36099
+ untaggedInputIds: new Set([
36100
+ ...state.recoveryPrefix?.untaggedInputIds ?? [],
36101
+ ...snapshot.inputs.slice(anchor + 1).map((input) => input.id)
36102
+ ])
36103
+ };
36104
+ };
36105
+ const beginPass = (args) => {
36106
+ const omitsBookkeeping = omitsRootBookkeeping(args);
36107
+ const snapped = snapshotInputs(args.messages, args.entryIds, omitsBookkeeping);
36108
+ const slot = getSlot(args.sessionId);
36109
+ if (snapped.failure || !slot) {
36110
+ return {
36111
+ omitsBookkeeping,
36112
+ sessionId: args.sessionId,
36113
+ inputs: snapped.inputs,
36114
+ preparationFailure: snapped.failure,
36115
+ replayFailure: snapped.failure ?? "lkg_miss",
36116
+ replayAnchorInputIndex: null,
36117
+ pristineTail: null,
36118
+ modelKey: args.modelKey,
36119
+ providerKey: args.providerKey
36120
+ };
36121
+ }
36122
+ const stableAnchorIndex = snapped.inputs.findIndex((input) => input.id === slot.lastInputMessageId);
36123
+ if (stableAnchorIndex < 0) {
36124
+ return {
36125
+ omitsBookkeeping,
36126
+ sessionId: args.sessionId,
36127
+ inputs: snapped.inputs,
36128
+ preparationFailure: null,
36129
+ replayFailure: "lkg_invalidated_reshape",
36130
+ replayAnchorInputIndex: null,
36131
+ pristineTail: null,
36132
+ modelKey: args.modelKey,
36133
+ providerKey: args.providerKey
36134
+ };
36135
+ }
36136
+ const messageAnchorIndex = snapped.inputs[stableAnchorIndex]?.messageIndex;
36137
+ if (messageAnchorIndex === undefined) {
36138
+ return {
36139
+ omitsBookkeeping,
36140
+ sessionId: args.sessionId,
36141
+ inputs: snapped.inputs,
36142
+ preparationFailure: null,
36143
+ replayFailure: "lkg_invalidated_reshape",
36144
+ replayAnchorInputIndex: null,
36145
+ pristineTail: null,
36146
+ modelKey: args.modelKey,
36147
+ providerKey: args.providerKey
36148
+ };
36149
+ }
36150
+ try {
36151
+ return {
36152
+ omitsBookkeeping,
36153
+ sessionId: args.sessionId,
36154
+ inputs: snapped.inputs,
36155
+ preparationFailure: null,
36156
+ replayFailure: null,
36157
+ replayAnchorInputIndex: stableAnchorIndex,
36158
+ pristineTail: structuredClone(args.messages.slice(messageAnchorIndex + 1)),
36159
+ modelKey: args.modelKey,
36160
+ providerKey: args.providerKey
36161
+ };
36162
+ } catch {
36163
+ return {
36164
+ omitsBookkeeping,
36165
+ sessionId: args.sessionId,
36166
+ inputs: snapped.inputs,
36167
+ preparationFailure: null,
36168
+ replayFailure: "lkg_tail_snapshot_failed",
36169
+ replayAnchorInputIndex: null,
36170
+ pristineTail: null,
36171
+ modelKey: args.modelKey,
36172
+ providerKey: args.providerKey
36173
+ };
36127
36174
  }
36128
36175
  };
36129
- if (fenceEnabled) {
36130
- pi.on("agent_start", (_event, ctx) => {
36131
- const key = operationKey(ctx);
36132
- lifecycleCounts(key).starts++;
36133
- const pending = operations.get(key);
36134
- const active = pending && !pending.started && !pending.ended ? pending : newOperation();
36135
- active.started = true;
36136
- operations.set(key, active);
36137
- });
36138
- pi.on("agent_end", (event, ctx) => {
36139
- const key = operationKey(ctx);
36140
- const lifecycleSeen = lifecycleCounts(key);
36141
- lifecycleSeen.ends = Math.min(lifecycleSeen.ends + 1, lifecycleSeen.starts);
36142
- const counts = sideSessions.get(key);
36143
- if (counts)
36144
- log2(`[magic-context][pi] OMP context counts session=${String(key)} side_contexts=${counts.sides} context_passes=${counts.contexts} dispatch_fence=${counts.sides ? "latched off" : "on"}`);
36145
- const operation = operations.get(key);
36146
- if (operation) {
36147
- if (endBelongsToEarlierRun(operation, event) || lifecycleSeen.starts > lifecycleSeen.ends && dispatchedEndBelongsToEarlierRun(operation, event, ctx)) {
36148
- if (operation.dispatched)
36149
- operation.retainedAfterEnd = true;
36150
- if (lifecycleSeen.starts <= lifecycleSeen.ends)
36151
- operation.started = false;
36152
- log2(`[magic-context][pi] agent_end attributed to an earlier run; keeping the latest context pass session=${String(key)} ${operation.pass?.diagnostic() ?? ""}`);
36153
- return;
36154
- }
36155
- operation.ended = true;
36156
- operation.ready = false;
36157
- releasePiOperationPayloads(operation);
36158
- }
36159
- });
36160
- pi.on("before_provider_request", (event, ctx) => {
36161
- const cancelledOperation = operations.get(operationKey(ctx));
36162
- const signal = ctx.signal;
36163
- if (signal ? signal.aborted : cancelledOperation?.pass?.isCancelled()) {
36164
- cancelledOperation?.pass?.cancel();
36165
- if (cancelledOperation)
36166
- cancelledOperation.ready = false;
36176
+ const measuredPrefixFor = (snapshot, slot, parentOf) => {
36177
+ const request = stateFor(snapshot.sessionId).measuredRequest;
36178
+ const anchor = snapshot.replayAnchorInputIndex;
36179
+ if (!request?.usage || !slot || !parentOf || anchor === null || !snapshot.pristineTail || !extendsMeasuredRequest(request.jsonPrefix, slot.jsonPrefix) || !request.modelKey || request.modelKey !== slot.modelKey || request.modelKey !== snapshot.modelKey || request.providerKey !== slot.providerKey || request.providerKey !== snapshot.providerKey || snapshot.inputs[anchor]?.id !== slot.lastInputMessageId || exactReusablePrefix(snapshot.inputs, request.inputs) !== request.inputs.length)
36180
+ return;
36181
+ const assistant = snapshot.inputs[request.inputs.length];
36182
+ const assistantId = assistant?.id;
36183
+ if (!assistantId || assistantId.startsWith("pi-lkg-unmapped:") || assistant.providerUsageSignature !== request.usage.signature)
36184
+ return;
36185
+ let appendedMessages;
36186
+ try {
36187
+ if (parentOf(assistantId) !== request.inputs.at(-1)?.id)
36167
36188
  return;
36168
- }
36169
- if (options.compactionOff?.(ctx))
36189
+ const measuredLength = JSON.parse(request.jsonPrefix).length;
36190
+ const prefix = JSON.parse(slot.jsonPrefix);
36191
+ const replyId = measuredLength < prefix.length ? slot.piOutputEntryIds?.[measuredLength] : snapshot.inputs[anchor + 1]?.id;
36192
+ if (replyId !== assistantId)
36170
36193
  return;
36171
- const payload = event.payload;
36172
- const key = operationKey(ctx);
36173
- if (sideSessions.get(key)?.sides)
36194
+ appendedMessages = [
36195
+ ...prefix.slice(measuredLength),
36196
+ ...snapshot.pristineTail
36197
+ ];
36198
+ if (completedProviderUsage(appendedMessages[0])?.signature !== request.usage.signature)
36174
36199
  return;
36175
- const nonces = unsendableNonces.get(key);
36176
- if (nonces?.size) {
36177
- const serializedPayload = JSON.stringify(payload);
36178
- if (serializedPayload && [...nonces].some((nonce) => serializedPayload.includes(nonce))) {
36179
- const budget = new PiContextBudget;
36180
- budget.stage = "provider dispatch";
36181
- refuse(ctx, budget, "superseded context fallback is not a managed request", () => true);
36182
- return;
36183
- }
36200
+ } catch {
36201
+ return;
36202
+ }
36203
+ return {
36204
+ modelKey: piModelRefToCanonical(request.modelKey).toLowerCase(),
36205
+ inputTokens: request.usage.inputTokens,
36206
+ envelopeSignature: request.envelopeSignature,
36207
+ appendedMessages,
36208
+ previousMessages: JSON.parse(request.jsonPrefix)
36209
+ };
36210
+ };
36211
+ const replay = (snapshot, parentOf) => {
36212
+ if (snapshot.replayFailure) {
36213
+ if (snapshot.replayFailure === "lkg_invalidated_reshape") {
36214
+ dropSlot(snapshot.sessionId, snapshot.replayFailure);
36215
+ stateFor(snapshot.sessionId).acceptedInputs = null;
36184
36216
  }
36185
- if (isOmpAdvisorRequest(payload))
36186
- return;
36187
- let operation = operations.get(key);
36188
- if (!operation) {
36189
- operation = newOperation();
36190
- operations.set(key, operation);
36217
+ return { ok: false, reason: snapshot.replayFailure };
36218
+ }
36219
+ if (snapshot.replayAnchorInputIndex === null || snapshot.pristineTail === null) {
36220
+ return { ok: false, reason: "lkg_miss" };
36221
+ }
36222
+ const slot = getSlot(snapshot.sessionId);
36223
+ const start = slot?.inputIdSeq.indexOf(snapshot.inputs[0]?.id ?? "") ?? -1;
36224
+ if (slot && start > 0) {
36225
+ const ownership = slot.piOutputEntryIds;
36226
+ if (!ownership)
36227
+ return { ok: false, reason: "lkg_output_mapping_unavailable" };
36228
+ if (slot.modelKey !== snapshot.modelKey || slot.providerKey !== snapshot.providerKey)
36229
+ return { ok: false, reason: "lkg_model_mismatch" };
36230
+ const surviving = snapshot.inputs.slice(0, snapshot.replayAnchorInputIndex + 1);
36231
+ if (surviving.length !== slot.inputIdSeq.length - start || surviving.some((input, index) => input.id !== slot.inputIdSeq[start + index]))
36232
+ return { ok: false, reason: "lkg_invalidated_reshape" };
36233
+ if (surviving.some((input, index) => !lkgFieldsMatchDigest(input.fields, slot.inputContentDigests[start + index])))
36234
+ return { ok: false, reason: "lkg_content_mismatch" };
36235
+ const removed = new Set(slot.inputIdSeq.slice(0, start));
36236
+ const prefix = JSON.parse(slot.jsonPrefix);
36237
+ const measuredPrefix = measuredPrefixFor(snapshot, slot, parentOf);
36238
+ return {
36239
+ ok: true,
36240
+ ...measuredPrefix ? { measuredPrefix } : {},
36241
+ messages: [
36242
+ ...prefix.filter((message, index) => isPiSystemEntry(message) || ownership[index] === null || !removed.has(ownership[index] ?? "")),
36243
+ ...snapshot.pristineTail
36244
+ ]
36245
+ };
36246
+ }
36247
+ const prefixInputs = snapshot.inputs.slice(0, snapshot.replayAnchorInputIndex + 1);
36248
+ const entry = {
36249
+ pristineTail: snapshot.pristineTail,
36250
+ entryInputIds: snapshot.inputs.map((input) => input.id),
36251
+ entryContentDigests: prefixInputs.map((input) => lkgContentDigestFromFields(input.fields)),
36252
+ ...slot?.inputContentDigests.some(isLegacyLkgDigest) ? {
36253
+ entryLegacyContentDigests: prefixInputs.map((input) => legacyLkgDigestFromFields(input.fields))
36254
+ } : {},
36255
+ anchorIndex: snapshot.replayAnchorInputIndex
36256
+ };
36257
+ const result = replayLkg({
36258
+ sessionId: snapshot.sessionId,
36259
+ messages: [],
36260
+ modelKey: snapshot.modelKey,
36261
+ providerKey: snapshot.providerKey,
36262
+ entry,
36263
+ skipSeamValidation: true
36264
+ });
36265
+ if (!result.ok)
36266
+ stateFor(snapshot.sessionId).acceptedInputs = null;
36267
+ const measuredPrefix = result.ok ? measuredPrefixFor(snapshot, slot, parentOf) : undefined;
36268
+ return result.ok && measuredPrefix ? { ...result, measuredPrefix } : result;
36269
+ };
36270
+ const captureAppliedPass = (args) => {
36271
+ args.assertCurrentPass?.();
36272
+ const { snapshot } = args;
36273
+ if (snapshot.preparationFailure || snapshot.inputs.length === 0) {
36274
+ if (args.publishServedNumbers)
36275
+ throw new PiLkgUncapturable(snapshot.preparationFailure ?? "lkg_inputs_empty", "Pi LKG input preparation failed");
36276
+ return;
36277
+ }
36278
+ const state = stateFor(snapshot.sessionId);
36279
+ let jsonPrefix;
36280
+ try {
36281
+ const detachedOutputs = plainOutputFields(args.outputMessages);
36282
+ const plainOutput = detachedOutputs !== null;
36283
+ const outputs = detachedOutputs ?? [];
36284
+ const priorOutput = plainOutput ? state.outputSnapshot : null;
36285
+ const prefix = exactReusablePrefix(outputs, priorOutput?.inputs ?? null);
36286
+ const jsonMessages = [
36287
+ ...priorOutput?.jsonMessages.slice(0, prefix) ?? [],
36288
+ ...plainOutput ? args.outputMessages.slice(prefix).map((message) => JSON.stringify(message) ?? "null") : []
36289
+ ];
36290
+ jsonPrefix = !plainOutput ? JSON.stringify(args.outputMessages) : priorOutput && prefix === outputs.length && prefix === priorOutput.inputs.length ? priorOutput.json : `[${jsonMessages.join(",")}]`;
36291
+ args.assertCurrentPass?.();
36292
+ state.outputSnapshot = plainOutput ? {
36293
+ inputs: outputs,
36294
+ jsonMessages,
36295
+ json: jsonPrefix
36296
+ } : null;
36297
+ } catch (error) {
36298
+ args.assertCurrentPass?.();
36299
+ if (args.publishServedNumbers)
36300
+ throw new PiLkgUncapturable("lkg_output_serialize_failed", error instanceof Error ? error.message : String(error), error);
36301
+ dropSlot(snapshot.sessionId, "lkg_snapshot_serialize_failed");
36302
+ const failedState = stateFor(snapshot.sessionId);
36303
+ failedState.syncCaptureRequired = true;
36304
+ failedState.acceptedInputs = null;
36305
+ sessionLog2(snapshot.sessionId, "LKG SNAPSHOT PREPARATION FAILED; forcing synchronous capture on the next applied pass:", error);
36306
+ return;
36307
+ }
36308
+ const idsByDigest = new Map;
36309
+ if (!args.outputEntryIds)
36310
+ for (const input of snapshot.inputs) {
36311
+ const digest = lkgContentDigestFromFields(input.fields);
36312
+ idsByDigest.set(digest, idsByDigest.has(digest) ? undefined : input.id);
36191
36313
  }
36192
- const active = operation;
36193
- const isCurrent = () => operations.get(key) === active;
36194
- const capture = isOmpUnmanagedCaptureRequest(payload);
36195
- const budget = (capture ? undefined : active.pass) ?? new PiContextBudget;
36196
- if (active.refused && active.pass === budget) {
36197
- ctx.abort();
36314
+ const inferredIds = args.outputEntryIds ?? args.outputMessages.map((message) => {
36315
+ const fields = lkgContentFields(message);
36316
+ if (!fields)
36317
+ return;
36318
+ const digest = lkgContentDigestFromFields(fields);
36319
+ return idsByDigest.get(digest);
36320
+ });
36321
+ const outputIds = args.outputEntryIds ?? inferredIds;
36322
+ const inputIds = new Set(snapshot.inputs.map((input) => input.id));
36323
+ const ownership = outputIds.length === args.outputMessages.length && outputIds.every((id) => id === null || typeof id === "string" && inputIds.has(id)) ? [...outputIds] : undefined;
36324
+ args.assertCurrentPass?.();
36325
+ state.captureSequence += 1;
36326
+ const plan = {
36327
+ sessionId: snapshot.sessionId,
36328
+ inputs: snapshot.inputs,
36329
+ jsonPrefix,
36330
+ modelKey: snapshot.modelKey,
36331
+ providerKey: snapshot.providerKey,
36332
+ capturedAt: Date.now(),
36333
+ captureSequence: state.captureSequence
36334
+ };
36335
+ const noteCapturedRequest = () => {
36336
+ if (state.measuredRequest && (state.measuredRequest.modelKey !== plan.modelKey || state.measuredRequest.providerKey !== plan.providerKey || state.measuredRequest.envelopeSignature !== args.hostEnvelopeSignature || !extendsMeasuredRequest(state.measuredRequest.jsonPrefix, jsonPrefix)))
36337
+ state.measuredRequest = undefined;
36338
+ state.capturedRequest = args.hostEnvelopeSignature ? { ...plan, envelopeSignature: args.hostEnvelopeSignature } : undefined;
36339
+ };
36340
+ if (!args.publishServedNumbers)
36341
+ noteCapturedRequest();
36342
+ const livePrior = getSlot(snapshot.sessionId);
36343
+ const unchanged = livePrior && livePrior.modelKey === plan.modelKey && livePrior.providerKey === plan.providerKey && livePrior.jsonPrefix === plan.jsonPrefix && livePrior.inputIdSeq.length === plan.inputs.length && exactReusablePrefix(plan.inputs, state.acceptedInputs) === plan.inputs.length && JSON.stringify(livePrior.piOutputEntryIds) === JSON.stringify(ownership);
36344
+ args.assertCurrentPass?.();
36345
+ if (unchanged && !state.syncCaptureRequired && !args.publishServedNumbers) {
36346
+ const kept = {
36347
+ ...livePrior,
36348
+ capturedAt: plan.capturedAt,
36349
+ captureSequence: plan.captureSequence
36350
+ };
36351
+ if (captureSlot(plan.sessionId, kept))
36352
+ state.acceptedSlot = kept;
36353
+ else
36354
+ state.syncCaptureRequired = true;
36355
+ }
36356
+ if (args.cacheBusting && !unchanged) {
36357
+ dropSlot(snapshot.sessionId, "lkg_cache_bust_pending_capture");
36358
+ }
36359
+ const commit = () => {
36360
+ try {
36361
+ args.assertCurrentPass?.();
36362
+ } catch (error) {
36363
+ if (args.publishServedNumbers)
36364
+ throw error;
36365
+ sessionLog2(plan.sessionId, "DISCARDED CONTEXT RESULT: queued LKG capture", error);
36198
36366
  return;
36199
36367
  }
36200
- let refusalOrigin;
36368
+ if (plan.captureSequence !== state.captureSequence)
36369
+ return;
36370
+ const startedAt = performance.now();
36371
+ let reusedPrefix = 0;
36201
36372
  try {
36202
- if (capture)
36203
- throw new Error("no context pass ran for OMP auto-learn capture; raw conversation refused");
36204
- if (!active.pass)
36205
- throw new Error("no context pass ran for this operation");
36206
- if (!budget.completed && (budget.abandoned || budget.elapsed() >= PI_CONTEXT_BUDGET.outcomeMs))
36207
- throw new PiContextDeadlineError(budget.diagnostic());
36208
- if (active.ended) {
36209
- refusalOrigin = "agent_end";
36210
- throw new Error("the run that owned the latest context pass has ended; no managed result for this request");
36373
+ const inputIdSeq = plan.inputs.map((input) => input.id);
36374
+ const prior = getSlot(plan.sessionId);
36375
+ const digestPrior = prior ?? state.acceptedSlot;
36376
+ const reusePrior = digestPrior?.inputContentSignatures !== undefined && digestPrior.modelKey === plan.modelKey && digestPrior.providerKey === plan.providerKey ? {
36377
+ slot: digestPrior,
36378
+ signatures: digestPrior.inputContentSignatures
36379
+ } : undefined;
36380
+ const reusablePrefix = reusePrior ? exactReusablePrefix(plan.inputs, state.acceptedInputs) : 0;
36381
+ const inputContentSignatures = [
36382
+ ...reusePrior ? reusePrior.signatures.slice(0, reusablePrefix) : [],
36383
+ ...plan.inputs.slice(reusablePrefix).map((input) => signatureForFields(input.fields))
36384
+ ];
36385
+ const verifiedOwnPrefix = reusePrior && digestPrior === state.acceptedSlot && reusePrior.slot.inputIdSeq.length === reusePrior.signatures.length && reusePrior.signatures.length === reusePrior.slot.inputContentDigests.length;
36386
+ let digestPrefix = 0;
36387
+ if (verifiedOwnPrefix && reusePrior) {
36388
+ while (digestPrefix < reusablePrefix && !isLegacyLkgDigest(reusePrior.slot.inputContentDigests[digestPrefix] ?? ""))
36389
+ digestPrefix++;
36211
36390
  }
36212
- if (!budget.completed || !active.ready || active.refused || budget.abandoned)
36213
- throw new Error("context pass ran without a managed context receipt");
36214
- if (!piHasManagedSignature(payload, active.managedSignature))
36215
- throw new Error("provider request does not match this pass's managed dispatch (missing current receipt signature)");
36216
- const fields = piConversationFields(payload);
36217
- if (!fields)
36218
- throw new Error("provider conversation identity could not be established");
36219
- const body = payload;
36220
- const skipReason = active.preparedStructure ? active.preparedStructure.structuralSkipReason ?? (active.preparedStructure.api && body?.messages === undefined && Array.isArray(body?.input) && !active.preparedStructure.responsesMessages ? "provider selected Responses items without a matching history plan" : undefined) : "prepared history representation is not modeled";
36221
- if (skipReason && !structureWarnings.has(key)) {
36222
- structureWarnings.set(key, true);
36223
- log2(`[magic-context][pi] OMP structural comparison skipped: ${skipReason}; stale prepared requests are not detected on this pass; session=${String(key)}`);
36391
+ const incremental = verifiedOwnPrefix && reusePrior ? {
36392
+ reusedPrefix: digestPrefix,
36393
+ digests: [
36394
+ ...reusePrior.slot.inputContentDigests.slice(0, digestPrefix),
36395
+ ...plan.inputs.slice(digestPrefix).map((input) => lkgContentDigestFromFields(input.fields))
36396
+ ]
36397
+ } : incrementalLkgContentDigests(plan.inputs.map((input, index) => ({
36398
+ id: input.id,
36399
+ signature: inputContentSignatures[index] ?? "",
36400
+ fields: input.fields
36401
+ })), reusePrior ? {
36402
+ ids: reusePrior.slot.inputIdSeq.slice(0, reusablePrefix),
36403
+ signatures: reusePrior.signatures.slice(0, reusablePrefix),
36404
+ digests: reusePrior.slot.inputContentDigests.slice(0, reusablePrefix)
36405
+ } : undefined);
36406
+ reusedPrefix = incremental.reusedPrefix;
36407
+ if (!args.publishServedNumbers && !state.syncCaptureRequired && prior && reusedPrefix === plan.inputs.length && prior.inputIdSeq.length === plan.inputs.length && prior.jsonPrefix === plan.jsonPrefix && JSON.stringify(prior.piOutputEntryIds) === JSON.stringify(ownership)) {
36408
+ args.assertCurrentPass?.();
36409
+ state.acceptedInputs = plan.inputs;
36410
+ return;
36224
36411
  }
36225
- const structureMismatch = skipReason ? undefined : piPreparedStructureMismatch(payload, active.preparedStructure, (counts) => {
36226
- if (imageTrimNotices.has(key))
36227
- return;
36228
- imageTrimNotices.set(key, true);
36229
- log2(`[magic-context][pi] OMP provider images trimmed: planned=${counts.planned} actual=${counts.actual} trimmed=${counts.trimmed}`);
36230
- });
36231
- if (structureMismatch)
36232
- throw new Error(`provider request does not match this pass's managed dispatch (prepared turn structure); ${structureMismatch}`);
36233
- if (active.dispatched) {
36234
- if (!active.dispatchFields || exactReusablePrefix([{ id: "conversation", fields }], [{ id: "conversation", fields: active.dispatchFields }]) !== 1)
36235
- throw new Error("provider request does not match this pass's managed dispatch");
36412
+ const slot = {
36413
+ jsonPrefix: plan.jsonPrefix,
36414
+ piOutputEntryIds: ownership,
36415
+ inputIdSeq,
36416
+ inputContentDigests: incremental.digests,
36417
+ inputContentSignatures,
36418
+ lastInputMessageId: plan.inputs.at(-1)?.id ?? "",
36419
+ modelKey: plan.modelKey,
36420
+ providerKey: plan.providerKey,
36421
+ capturedAt: plan.capturedAt,
36422
+ captureSequence: plan.captureSequence
36423
+ };
36424
+ args.assertCurrentPass?.();
36425
+ if (args.publishServedNumbers) {
36426
+ if (lkgSlotRejection(plan.sessionId, slot))
36427
+ throw new PiLkgUncapturable("lkg_slot_rejected", "LKG slot rejected the Pi snapshot");
36428
+ if (!saveLkgSlotToDb(db, plan.sessionId, slot, args.publishServedNumbers, {
36429
+ rebuild: args.releaseRecovery,
36430
+ retainDirtyRevision: !args.releaseRecovery ? state.recoveryPrefix?.fenceRevision : undefined
36431
+ })) {
36432
+ throw new Error("Pi LKG/served publication failed");
36433
+ }
36434
+ args.assertCurrentPass?.();
36236
36435
  }
36237
- if (!active.dispatched && active.inputMessages && (!active.inputBranch || active.inputBranch.anchorIndex < active.inputMessages.length - 1)) {
36238
- const previous = active.inputBranch;
36239
- const persisted = inputBranch(ctx, active.inputMessages);
36240
- if (!previous || persisted && persisted.anchorIndex > previous.anchorIndex && persisted.entryIds[previous.anchorIndex] === previous.anchorId)
36241
- active.inputBranch = persisted;
36436
+ if (!captureSlot(plan.sessionId, slot)) {
36437
+ throw new Error("LKG slot rejected the Pi snapshot");
36242
36438
  }
36243
- if (!active.dispatched)
36244
- active.dispatchFields = fields;
36245
- active.dispatched = true;
36439
+ state.acceptedInputs = plan.inputs;
36440
+ state.acceptedSlot = slot;
36441
+ if (state.recoveryPrefix) {
36442
+ state.recoveryPrefix = args.releaseRecovery ? undefined : {
36443
+ ...state.recoveryPrefix,
36444
+ inputs: plan.inputs,
36445
+ json: plan.jsonPrefix,
36446
+ outputEntryIds: ownership ?? state.recoveryPrefix.outputEntryIds
36447
+ };
36448
+ }
36449
+ const persisted = args.publishServedNumbers ? true : saveLkgSlotToDb(db, plan.sessionId, slot, undefined, {
36450
+ rebuild: args.releaseRecovery,
36451
+ retainDirtyRevision: state.recoveryPrefix?.fenceRevision
36452
+ });
36453
+ state.syncCaptureRequired = !persisted;
36454
+ if (args.publishServedNumbers)
36455
+ noteCapturedRequest();
36246
36456
  } catch (error) {
36247
- if (signal ? signal.aborted : budget.isCancelled(error)) {
36248
- budget.cancel();
36249
- active.ready = false;
36457
+ if (args.publishServedNumbers) {
36458
+ state.syncCaptureRequired = true;
36459
+ state.acceptedInputs = null;
36460
+ throw error;
36461
+ }
36462
+ try {
36463
+ args.assertCurrentPass?.();
36464
+ } catch (discarded) {
36465
+ sessionLog2(plan.sessionId, "DISCARDED CONTEXT RESULT: LKG publication", discarded);
36250
36466
  return;
36251
36467
  }
36252
- active.refused = true;
36253
- budget.stage = refusalOrigin ? `provider dispatch (origin=${refusalOrigin}; context stage=${budget.stage})` : "provider dispatch";
36254
- refuse(ctx, budget, error, isCurrent);
36255
- }
36256
- });
36257
- }
36258
- pi.registerEntryRenderer?.(ENTRY_TYPE, (entry) => new Text2(entry.data?.message ?? RETRY_MESSAGE, 0, 0));
36259
- pi.on("context", async (event, ctx) => {
36260
- if (fenceEnabled && isOmpDumpContext())
36261
- return { messages: event.messages };
36262
- const budget = new PiContextBudget;
36263
- budget.signal = event.signal ?? ctx.signal;
36264
- budget.sideTurn = fenceEnabled && isOmpSideContext(event);
36265
- const key = operationKey(ctx);
36266
- if (fenceEnabled) {
36267
- let counts = sideSessions.get(key);
36268
- if (!counts) {
36269
- counts = { contexts: 0, sides: 0 };
36270
- sideSessions.set(key, counts);
36271
- }
36272
- counts.contexts++;
36273
- if (budget.sideTurn && ++counts.sides === 1)
36274
- log2(`[magic-context][pi] OMP dispatch fence disabled for session ${String(key)}: verified ephemeral context; session-wide abort cannot distinguish provider operations (side_contexts=${counts.sides} context_passes=${counts.contexts})`);
36275
- }
36276
- const owner = budget.sideTurn ? operations.get(key) : undefined;
36277
- let operation = operations.get(key);
36278
- if (budget.sideTurn || !operation || operation.ended || !fenceEnabled) {
36279
- operation = newOperation();
36280
- if (!budget.sideTurn)
36281
- operations.set(key, operation);
36282
- }
36283
- const active = operation;
36284
- active.pass = budget;
36285
- releasePiOperationPayloads(active);
36286
- active.retainedAfterEnd = false;
36287
- active.dispatched = false;
36288
- active.ready = false;
36289
- active.refused = false;
36290
- const { isOwned, isCurrent, assertOwner } = createPiContextOwnershipGuards(operations, key, active, budget, owner);
36291
- budget.assertOwner = assertOwner;
36292
- try {
36293
- budget.assertOutcome();
36294
- let inputs;
36295
- if (fenceEnabled && !budget.sideTurn) {
36296
- active.inputKeys = inputMessageKeys(event);
36297
- inputs = messageSnapshots(event.messages);
36298
- active.inputMessages = inputs;
36299
- }
36300
- const result = await budget.waitMandatory(Promise.resolve(withSqliteTransformPass2(() => handler(event, ctx, budget))));
36301
- if (result?.messages) {
36302
- const prepared = preparePiDispatch(result.messages, fenceEnabled ? ctx.model : undefined, fenceEnabled);
36303
- if (prepared.messages !== result.messages)
36304
- result.messages = prepared.messages;
36305
- active.managedSignature = piManagedSignature(result.messages);
36306
- active.preparedStructure = prepared.structure;
36307
- budget.completed = true;
36308
- active.ready = true;
36309
- if (inputs)
36310
- active.inputBranch = inputBranch(ctx, inputs);
36311
- }
36312
- return result;
36313
- } catch (error) {
36314
- if (budget.isCancelled(error)) {
36315
- budget.cancel();
36316
- if (isOwned())
36317
- active.ready = false;
36318
- return;
36319
- }
36320
- if (error instanceof PiContextSupersededError || !isCurrent()) {
36321
- if (fenceEnabled && !budget.sideTurn && !sideSessions.get(key)?.sides && !options.compactionOff?.(ctx)) {
36322
- const nonce = crypto.randomUUID();
36323
- let nonces = unsendableNonces.get(key);
36324
- if (!nonces) {
36325
- nonces = new Set;
36326
- unsendableNonces.set(key, nonces);
36327
- }
36328
- nonces.add(nonce);
36329
- if (nonces.size > MAX_UNSENDABLE_NONCES_PER_SESSION) {
36330
- const oldest = nonces.values().next().value;
36331
- if (oldest !== undefined)
36332
- nonces.delete(oldest);
36333
- }
36334
- event.messages.push({
36335
- role: "user",
36336
- content: [
36337
- {
36338
- type: "text",
36339
- text: `Magic Context did not prepare this request for the current turn. ${nonce}`
36340
- }
36341
- ],
36342
- timestamp: Date.now()
36468
+ if (plan.captureSequence !== state.captureSequence)
36469
+ return;
36470
+ dropSlot(plan.sessionId, "lkg_async_capture_failed");
36471
+ state.syncCaptureRequired = true;
36472
+ state.acceptedInputs = null;
36473
+ sessionLog2(plan.sessionId, "LKG ASYNC CAPTURE FAILED; forcing synchronous capture on the next applied pass:", error);
36474
+ } finally {
36475
+ try {
36476
+ onCaptureTiming?.({
36477
+ sessionId: plan.sessionId,
36478
+ elapsedMs: performance.now() - startedAt,
36479
+ reusedPrefix
36343
36480
  });
36344
- }
36345
- log2(`[magic-context][pi] DISCARDED CONTEXT RESULT ${budget.diagnostic()}`, error);
36346
- throw error;
36481
+ } catch {}
36347
36482
  }
36348
- log2("[magic-context][pi] turn refused", error);
36349
- if (typeof ctx.abort !== "function") {
36350
- budget.abandoned = true;
36351
- options.onRefusal?.(ctx, budget, error instanceof Error ? error.message : String(error), isOwned);
36352
- throw error;
36483
+ };
36484
+ if (args.publishServedNumbers || state.syncCaptureRequired) {
36485
+ commit();
36486
+ } else {
36487
+ try {
36488
+ scheduleCapture(commit);
36489
+ } catch (error) {
36490
+ dropSlot(plan.sessionId, "lkg_capture_schedule_failed");
36491
+ state.syncCaptureRequired = true;
36492
+ state.acceptedInputs = null;
36493
+ sessionLog2(plan.sessionId, "LKG CAPTURE SCHEDULE FAILED; forcing synchronous capture on the next applied pass:", error);
36353
36494
  }
36354
- active.refused = true;
36355
- refuse(ctx, budget, error, isOwned);
36356
- return { messages: event.messages };
36357
- } finally {
36358
- if (!fenceEnabled || budget.sideTurn)
36359
- releasePiOperationPayloads(active);
36360
36495
  }
36361
- });
36362
- }
36363
-
36364
- // src/pi-fallback-index.ts
36365
- function createPiFallbackIndex() {
36366
- const prior = [];
36367
- let priorHadDuplicateIds = false;
36368
- const byHeader = new Map;
36369
- const positions = new Map;
36370
- const owners = new Map;
36371
- let rebuilt = 0;
36496
+ return state.outputSnapshot ?? undefined;
36497
+ };
36372
36498
  return {
36373
- get rebuiltMessages() {
36374
- return rebuilt;
36499
+ noteRecovery,
36500
+ recoveryEntryIds,
36501
+ recoveryFor,
36502
+ beginPass(args) {
36503
+ return {
36504
+ ...beginPass(args),
36505
+ systemEntries: args.messages.filter(isPiSystemEntry).map((message) => JSON.stringify(message))
36506
+ };
36375
36507
  },
36376
- update(args) {
36377
- const selected = new Map;
36378
- const guardIndexes = [];
36379
- const inputs = [];
36380
- const seen = new Set;
36381
- let duplicate = false;
36382
- for (let index = 0;index < args.messages.length; index++) {
36383
- const message = args.messages[index];
36384
- const id = args.resolveId(message, index) ?? `unmapped:${index}`;
36385
- if (seen.has(id))
36386
- duplicate = true;
36387
- seen.add(id);
36388
- const fields = (message && typeof message === "object" ? args.fields?.get(message) : undefined) ?? lkgContentFields(message && typeof message === "object" ? {
36389
- role: message.role,
36390
- timestamp: message.timestamp,
36391
- responseId: message.responseId,
36392
- toolCallId: message.toolCallId,
36393
- content: message.content
36394
- } : message);
36395
- inputs.push({ id, fields: fields ?? [Symbol()] });
36396
- }
36397
- let prefix = priorHadDuplicateIds ? 0 : exactReusablePrefix(inputs, prior);
36398
- if (duplicate) {
36399
- prefix = 0;
36400
- selected.clear();
36401
- }
36402
- priorHadDuplicateIds = duplicate;
36403
- for (let index = 0;index < prefix; index++) {
36404
- const old = prior[index];
36405
- if (old && !args.reusable?.has(old.id)) {
36406
- guardIndexes.push(index);
36407
- if (old.fingerprint)
36408
- selected.set(old.id, old.fingerprint);
36409
- }
36410
- }
36411
- for (const old of prior.slice(prefix)) {
36412
- positions.delete(old.id);
36413
- if (old.header) {
36414
- const bucket = byHeader.get(old.header);
36415
- bucket?.delete(old);
36416
- if (!bucket?.size)
36417
- byHeader.delete(old.header);
36418
- }
36419
- for (const [key, ids] of old.owners) {
36420
- const bucket = owners.get(key);
36421
- for (const id of ids)
36422
- bucket?.delete(id);
36423
- if (!bucket?.size)
36424
- owners.delete(key);
36425
- }
36508
+ replay(snapshot, parentOf) {
36509
+ const result = replay(snapshot, parentOf);
36510
+ if (!result.ok)
36511
+ return result;
36512
+ const systems = result.messages.filter(isPiSystemEntry).map((message) => JSON.stringify(message));
36513
+ let cursor = 0;
36514
+ for (const expected of snapshot.systemEntries ?? []) {
36515
+ const index = systems.indexOf(expected, cursor);
36516
+ if (index < 0)
36517
+ return { ok: false, reason: "lkg_system_state_mismatch" };
36518
+ cursor = index + 1;
36426
36519
  }
36427
- prior.length = prefix;
36428
- for (let index = prefix;index < args.messages.length; index++) {
36429
- guardIndexes.push(index);
36430
- const message = args.messages[index];
36431
- const input = inputs[index];
36432
- if (!input)
36433
- continue;
36434
- const record = {
36435
- ...input,
36436
- header: args.header(message),
36437
- fingerprint: input.id.startsWith("unmapped:") ? null : args.fingerprint(message),
36438
- owners: input.id.startsWith("unmapped:") ? new Map : args.toolOwners(message, input.id)
36439
- };
36440
- rebuilt++;
36441
- prior.push(record);
36442
- if (!positions.has(record.id))
36443
- positions.set(record.id, index);
36444
- if (record.header) {
36445
- const bucket = byHeader.get(record.header) ?? new Set;
36446
- bucket.add(record);
36447
- byHeader.set(record.header, bucket);
36448
- }
36449
- for (const [key, ids] of record.owners) {
36450
- const bucket = owners.get(key) ?? new Set;
36451
- for (const id of ids)
36452
- bucket.add(id);
36453
- owners.set(key, bucket);
36454
- }
36455
- if (!args.reusable?.has(record.id) && record.fingerprint)
36456
- selected.set(record.id, record.fingerprint);
36520
+ return result;
36521
+ },
36522
+ captureAppliedPass,
36523
+ measureOutgoingPrefix(snapshot, messages, parentOf) {
36524
+ try {
36525
+ const slot = getSlot(snapshot.sessionId);
36526
+ const measured = measuredPrefixFor(snapshot, slot, parentOf);
36527
+ if (!slot || !measured)
36528
+ return;
36529
+ const prefix = JSON.parse(slot.jsonPrefix);
36530
+ if (!Array.isArray(prefix) || messages.length < prefix.length || JSON.stringify(messages.slice(0, prefix.length)) !== slot.jsonPrefix)
36531
+ return;
36532
+ const measuredRequest = stateFor(snapshot.sessionId).measuredRequest;
36533
+ if (!measuredRequest)
36534
+ return;
36535
+ const measuredLength = JSON.parse(measuredRequest.jsonPrefix).length;
36536
+ const appendedMessages = messages.slice(measuredLength);
36537
+ if (completedProviderUsage(appendedMessages[0])?.signature !== measuredRequest.usage.signature)
36538
+ return;
36539
+ return { ...measured, appendedMessages };
36540
+ } catch {
36541
+ return;
36457
36542
  }
36458
- return {
36459
- positions,
36460
- owners,
36461
- changedFrom: prefix,
36462
- guardIndexes,
36463
- fingerprints(headers) {
36464
- const result = new Map(selected);
36465
- const records = headers ? [...headers].flatMap((header) => [
36466
- ...byHeader.get(header) ?? []
36467
- ]) : headers === null ? prior : [];
36468
- for (const record of records)
36469
- if (record.fingerprint)
36470
- result.set(record.id, record.fingerprint);
36471
- return result;
36472
- }
36473
- };
36474
36543
  }
36475
36544
  };
36476
36545
  }
36477
-
36478
- // src/pi-fit-reclaim.ts
36479
- import { createHash as createHash12, randomUUID as randomUUID4 } from "node:crypto";
36546
+ function resolvePiLkgOutputEntryIds(messages, syntheticLeadingCount, entryId) {
36547
+ const ids = messages.map((message, index) => index < syntheticLeadingCount && !isPiSystemEntry(message) ? null : isRecord(message) ? entryId(message) : undefined);
36548
+ const syntheticOwners = new Map;
36549
+ for (let index = 0;index < messages.length; index++) {
36550
+ const message = messages[index];
36551
+ const owner = ids[index];
36552
+ if (typeof owner !== "string" || !isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content))
36553
+ continue;
36554
+ for (const part of message.content) {
36555
+ if (isRecord(part) && part.type === "toolCall" && part.syntheticTodoMarker === true && typeof part.id === "string")
36556
+ syntheticOwners.set(part.id, syntheticOwners.has(part.id) ? undefined : owner);
36557
+ }
36558
+ }
36559
+ return ids.map((id, index) => {
36560
+ const message = messages[index];
36561
+ return id === undefined && isRecord(message) && message.role === "toolResult" && message.syntheticTodoMarker === true && typeof message.toolCallId === "string" ? syntheticOwners.get(message.toolCallId) : id;
36562
+ });
36563
+ }
36480
36564
 
36481
36565
  // src/tokenize-pi-messages.ts
36482
36566
  var PI_IMAGE_TOKENS = 1200;
@@ -36701,973 +36785,910 @@ function isTokenCacheSafeMessage(value) {
36701
36785
  return isPlainJsonData(value.content, new Set);
36702
36786
  }
36703
36787
  function isJsonVisibleDataProperty(value, key) {
36704
- if (!(key in value))
36705
- return true;
36706
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
36707
- return descriptor !== undefined && descriptor.enumerable === true && "value" in descriptor;
36708
- }
36709
- function isPlainJsonData(value, seen) {
36710
- if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "undefined") {
36711
- return true;
36712
- }
36713
- if (typeof value !== "object" || seen.has(value))
36714
- return false;
36715
- const prototype = Object.getPrototypeOf(value);
36716
- if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
36717
- return false;
36718
- }
36719
- if ("toJSON" in value)
36720
- return false;
36721
- seen.add(value);
36722
- try {
36723
- for (const key of Reflect.ownKeys(value)) {
36724
- if (Array.isArray(value) && key === "length")
36725
- continue;
36726
- if (typeof key !== "string")
36727
- return false;
36728
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
36729
- if (!descriptor?.enumerable || !("value" in descriptor)) {
36730
- return false;
36731
- }
36732
- if (!isPlainJsonData(descriptor.value, seen))
36733
- return false;
36734
- }
36735
- return true;
36736
- } finally {
36737
- seen.delete(value);
36738
- }
36739
- }
36740
- function safeJsonStringify2(value) {
36741
- try {
36742
- return JSON.stringify(value);
36743
- } catch {
36744
- return "";
36745
- }
36746
- }
36747
-
36748
- // src/pi-fit-reclaim.ts
36749
- var pendingInput = new Set;
36750
- var KEY2 = "piUpperEstimateReclaim";
36751
- function piFitOverflowPossible(args) {
36752
- if (!args.hasUI && args.inputTokens <= 0 || args.recoveryArmed || args.forceMaterialization || args.imported || args.coldInjection || args.cacheBusting || pendingInput.has(args.sessionId))
36753
- return true;
36754
- if (!args.contextLimit || !Number.isFinite(args.contextLimit))
36755
- return false;
36756
- const headroom = args.contextLimit * ((args.forcePercentage ?? 85) / 100) - args.inputTokens;
36757
- let appendedBytes = 0;
36758
- try {
36759
- for (const message of args.appendedMessages ?? []) {
36760
- appendedBytes += piFitMessageBytes(message);
36761
- if (2 * appendedBytes >= headroom)
36762
- return true;
36763
- }
36764
- } catch {
36765
- return true;
36766
- }
36767
- return false;
36768
- }
36769
- function clearPiUpperReclaimSession(sessionId) {
36770
- pendingInput.delete(sessionId);
36771
- }
36772
- function requestPiFitReclaimInput(sessionId) {
36773
- pendingInput.add(sessionId);
36774
- }
36775
- function clearPiFitReclaimInput(sessionId) {
36776
- pendingInput.delete(sessionId);
36777
- }
36778
- function piUpperReclaimCursor(args) {
36779
- const head = args.head;
36780
- return {
36781
- sessionId: args.sessionId,
36782
- modelKey: args.modelKey,
36783
- contextWindow: args.contextWindow ?? null,
36784
- head: head ? createHash12("sha256").update(JSON.stringify([
36785
- Buffer.from(head.cachedM0Bytes ?? []).toString("base64"),
36786
- Buffer.from(head.cachedM1Bytes ?? []).toString("base64"),
36787
- head.cachedM0MuralDataUrl ?? null
36788
- ])).digest("hex") : null,
36789
- recoveryArmedAt: getEmergencyRecoveryArmedAt(args.sessionId)
36790
- };
36791
- }
36792
- function claimFrom(value, sessionId) {
36793
- const claim = value;
36794
- return claim?.sessionId === sessionId && (typeof claim.modelKey === "string" || claim.modelKey === null) ? claim : null;
36795
- }
36796
- function canReclaim(previous, cursor) {
36797
- return !previous || previous.modelKey !== cursor.modelKey || (previous.contextWindow ?? null) !== cursor.contextWindow || (previous.head ?? null) !== cursor.head || cursor.recoveryArmedAt !== null && cursor.recoveryArmedAt !== previous.recoveryArmedAt;
36798
- }
36799
- function shouldReclaimPiUpperEstimate(db, cursor) {
36800
- if (!cursor)
36801
- return false;
36802
- return canReclaim(claimFrom(readReplayEnvelope(db, cursor.sessionId)[KEY2], cursor.sessionId), cursor);
36803
- }
36804
- function reservePiUpperEstimateReclaim(db, cursor) {
36805
- const reservation = randomUUID4();
36806
- return db.transaction(() => {
36807
- let claimed = false;
36808
- if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
36809
- claimed = canReclaim(claimFrom(doc[KEY2], cursor.sessionId), cursor);
36810
- if (!claimed)
36811
- return false;
36812
- doc.version = 2;
36813
- doc[KEY2] = { ...cursor, reservation };
36814
- return true;
36815
- }))
36816
- throw new Error("Could not reserve upper-estimate reclaim");
36817
- return claimed ? reservation : undefined;
36818
- }).immediate();
36819
- }
36820
- function recordPiUpperEstimateReclaim(db, cursor, reservation) {
36821
- const owner = reservation ?? reservePiUpperEstimateReclaim(db, cursor);
36822
- if (!owner)
36823
- return;
36824
- if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
36825
- if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== owner)
36826
- return false;
36827
- doc.version = 2;
36828
- doc[KEY2] = cursor;
36829
- return true;
36830
- }))
36831
- throw new Error("Could not persist upper-estimate reclaim cursor");
36832
- pendingInput.delete(cursor.sessionId);
36833
- }
36834
- function releasePiUpperEstimateReclaim(db, cursor, reservation) {
36835
- if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
36836
- if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== reservation)
36837
- return false;
36838
- delete doc[KEY2];
36839
- return true;
36840
- }))
36841
- throw new Error("Could not release upper-estimate reclaim");
36842
- }
36843
- function resetPiUpperEstimateReclaim(db, sessionId) {
36844
- if (!claimFrom(readReplayEnvelope(db, sessionId)[KEY2], sessionId))
36845
- return;
36846
- if (!updateReplayDocument(db, sessionId, (doc) => {
36847
- delete doc[KEY2];
36848
- return true;
36849
- }))
36850
- throw new Error("Could not clear upper-estimate reclaim cursor");
36851
- }
36852
-
36853
- // src/pi-lkg.ts
36854
- var piLkgSessionStates = new Map;
36855
- function completedProviderUsage(message) {
36856
- if (!isRecord(message) || message.role !== "assistant" || !["stop", "length", "toolUse"].includes(String(message.stopReason)) || typeof message.provider !== "string" || !message.provider || typeof message.model !== "string" || !message.model || typeof message.timestamp !== "number" || !Number.isFinite(message.timestamp) || !isRecord(message.usage))
36857
- return;
36858
- const usage = message.usage;
36859
- const counts = [usage.input, usage.cacheRead, usage.cacheWrite];
36860
- if (!counts.every((count) => typeof count === "number" && Number.isSafeInteger(count) && count >= 0))
36861
- return;
36862
- let inputTokens = usage.input + usage.cacheRead + usage.cacheWrite;
36863
- if (typeof usage.totalTokens === "number" && Number.isSafeInteger(usage.totalTokens) && typeof usage.output === "number" && Number.isSafeInteger(usage.output) && usage.output >= 0) {
36864
- inputTokens = Math.max(inputTokens, usage.totalTokens - usage.output);
36865
- }
36866
- if (!Number.isSafeInteger(inputTokens) || inputTokens <= 0)
36867
- return;
36868
- return {
36869
- inputTokens,
36870
- modelKey: `${message.provider}/${message.model}`,
36871
- providerKey: message.provider,
36872
- timestamp: message.timestamp,
36873
- signature: JSON.stringify([
36874
- message.provider,
36875
- message.model,
36876
- message.timestamp,
36877
- message.stopReason,
36878
- usage.input,
36879
- usage.cacheRead,
36880
- usage.cacheWrite,
36881
- usage.output,
36882
- usage.totalTokens
36883
- ])
36884
- };
36788
+ if (!(key in value))
36789
+ return true;
36790
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
36791
+ return descriptor !== undefined && descriptor.enumerable === true && "value" in descriptor;
36885
36792
  }
36886
- function notePiLkgProviderUsage(sessionId, parentInputId, message) {
36793
+ function isPlainJsonData(value, seen) {
36794
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "undefined") {
36795
+ return true;
36796
+ }
36797
+ if (typeof value !== "object" || seen.has(value))
36798
+ return false;
36799
+ const prototype = Object.getPrototypeOf(value);
36800
+ if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
36801
+ return false;
36802
+ }
36803
+ if ("toJSON" in value)
36804
+ return false;
36805
+ seen.add(value);
36887
36806
  try {
36888
- const state = piLkgSessionStates.get(sessionId);
36889
- const request = state?.capturedRequest;
36890
- const usage = completedProviderUsage(message);
36891
- if (!request || request.usage || !usage || request.captureSequence !== state?.captureSequence || !parentInputId || parentInputId.startsWith("pi-lkg-unmapped:") || parentInputId !== request.inputs.at(-1)?.id || usage.timestamp < request.capturedAt || usage.modelKey !== request.modelKey || usage.providerKey !== request.providerKey)
36892
- return false;
36893
- request.usage = {
36894
- signature: usage.signature,
36895
- inputTokens: usage.inputTokens
36896
- };
36897
- if (state)
36898
- state.measuredRequest = { ...request, usage: request.usage };
36807
+ for (const key of Reflect.ownKeys(value)) {
36808
+ if (Array.isArray(value) && key === "length")
36809
+ continue;
36810
+ if (typeof key !== "string")
36811
+ return false;
36812
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
36813
+ if (!descriptor?.enumerable || !("value" in descriptor)) {
36814
+ return false;
36815
+ }
36816
+ if (!isPlainJsonData(descriptor.value, seen))
36817
+ return false;
36818
+ }
36899
36819
  return true;
36820
+ } finally {
36821
+ seen.delete(value);
36822
+ }
36823
+ }
36824
+ function safeJsonStringify2(value) {
36825
+ try {
36826
+ return JSON.stringify(value);
36900
36827
  } catch {
36901
- return false;
36828
+ return "";
36902
36829
  }
36903
36830
  }
36904
- function clearPiLkgSessionState(sessionId) {
36905
- const state = piLkgSessionStates.get(sessionId);
36906
- if (state)
36907
- state.captureSequence += 1;
36908
- piLkgSessionStates.delete(sessionId);
36909
- clearLkgFenceProcessState(sessionId);
36831
+
36832
+ // src/pi-raw-fallback.ts
36833
+ class PiStorageBusyError extends Error {
36834
+ code = "PI_STORAGE_BUSY";
36835
+ recoverable = true;
36836
+ constructor(options) {
36837
+ super("Magic Context storage is busy; send your message again", options);
36838
+ this.name = "PiStorageBusyError";
36839
+ }
36910
36840
  }
36911
36841
 
36912
- class PiLkgUncapturable extends Error {
36913
- reason;
36914
- constructor(reason, message, cause) {
36915
- super(message, { cause });
36916
- this.reason = reason;
36842
+ class PiDegradedPassError extends Error {
36843
+ site;
36844
+ code = "PI_DEGRADED_PASS";
36845
+ recoverable = true;
36846
+ constructor(site, options) {
36847
+ super(describeDegradedPassFailure(site, options?.cause), options);
36848
+ this.site = site;
36849
+ this.name = "PiDegradedPassError";
36917
36850
  }
36918
36851
  }
36919
- function isTransientPiStorageError(error) {
36920
- if (!error || typeof error !== "object")
36921
- return false;
36922
- const candidate = error;
36923
- if (candidate.code === "SQLITE_BUSY" || candidate.code === "SQLITE_LOCKED" || candidate.code === "SQLITE_BUSY_SNAPSHOT") {
36924
- return true;
36852
+ function piFallbackRefusal(site, cause) {
36853
+ const seen = new Set;
36854
+ let current = cause;
36855
+ while (current && typeof current === "object" && !seen.has(current)) {
36856
+ seen.add(current);
36857
+ if (current instanceof PiStorageBusyError || isTransientSqliteError2(current) || isTransientPiStorageError(current))
36858
+ return new PiStorageBusyError({ cause });
36859
+ current = current.cause;
36925
36860
  }
36926
- return typeof candidate.message === "string" && /database is locked|database table is locked|sqlite_(busy|locked)/i.test(candidate.message);
36861
+ return cause instanceof PiDegradedPassError ? cause : new PiDegradedPassError(site, { cause });
36927
36862
  }
36928
- function reconcilePiLkgEntryIds(resolved, alignedProjection) {
36929
- if (!resolved || !alignedProjection || resolved.length === 0)
36930
- return resolved;
36931
- const result = [...resolved];
36932
- const anchors = [];
36933
- let projectionCursor = 0;
36934
- for (let messageIndex = 0;messageIndex < resolved.length; messageIndex += 1) {
36935
- const id = resolved[messageIndex];
36936
- if (typeof id !== "string")
36937
- continue;
36938
- let projectionIndex = -1;
36939
- for (let index = projectionCursor;index < alignedProjection.length; index += 1) {
36940
- if (alignedProjection[index] === id) {
36941
- projectionIndex = index;
36863
+ function estimatePiOutgoingInputTokens(messages, observed, measuredPrefix, contextLimit) {
36864
+ if (!observed || !Number.isFinite(observed.systemTokens) || observed.systemTokens <= 0 || !Number.isFinite(observed.toolDefinitionTokens) || observed.toolDefinitionTokens < 0 || !hasTokenizerForFit())
36865
+ return { tokens: 0, trusted: false };
36866
+ const knownRole = (message) => {
36867
+ if (!message || typeof message !== "object")
36868
+ return false;
36869
+ return ["user", "assistant", "toolResult", "custom"].includes(message.role ?? "");
36870
+ };
36871
+ const countable = (message) => {
36872
+ if (!knownRole(message))
36873
+ return false;
36874
+ const m = message;
36875
+ return typeof m.content === "string" || Array.isArray(m.content) && m.content.every((p) => p && typeof p === "object" && ["text", "thinking", "redacted_thinking", "toolCall"].includes(String(p.type)));
36876
+ };
36877
+ const complete = messages.every(countable);
36878
+ const measuredForBounds = measuredPrefix && measuredPrefix.modelKey === observed.modelKey && observed.envelopeSignature && measuredPrefix.envelopeSignature === observed.envelopeSignature && Number.isSafeInteger(measuredPrefix.inputTokens) && measuredPrefix.inputTokens > 0 ? measuredPrefix : undefined;
36879
+ if (contextLimit && Number.isFinite(contextLimit) && contextLimit > 0) {
36880
+ const priced = measuredForBounds?.appendedMessages ?? messages;
36881
+ const baseline = measuredForBounds?.inputTokens ?? 0;
36882
+ let contentBytes = 0;
36883
+ for (const rawMessage of priced.filter(knownRole)) {
36884
+ const message = rawMessage;
36885
+ if (typeof message.content === "string")
36886
+ contentBytes += Buffer.byteLength(message.content);
36887
+ else if (Array.isArray(message.content))
36888
+ for (const part of message.content) {
36889
+ if (!part || typeof part !== "object")
36890
+ continue;
36891
+ for (const value of [
36892
+ part.type === "text" ? part.text : undefined,
36893
+ part.type === "thinking" && !part.redacted ? part.thinking : undefined,
36894
+ part.type === "toolCall" ? part.name : undefined
36895
+ ])
36896
+ if (typeof value === "string")
36897
+ contentBytes += Buffer.byteLength(value);
36898
+ if (part.type === "toolCall" && part.arguments !== undefined)
36899
+ contentBytes += Buffer.byteLength(JSON.stringify(part.arguments));
36900
+ }
36901
+ if (baseline + Math.ceil(contentBytes / 4) > contextLimit)
36942
36902
  break;
36943
- }
36944
36903
  }
36945
- if (projectionIndex < 0)
36946
- return resolved;
36947
- anchors.push({ messageIndex, projectionIndex });
36948
- projectionCursor = projectionIndex + 1;
36904
+ const lower = baseline + Math.ceil(contentBytes / 4);
36905
+ if (lower > contextLimit)
36906
+ return {
36907
+ tokens: lower,
36908
+ trusted: false,
36909
+ refusalGrade: true,
36910
+ refusalTokens: lower,
36911
+ refusalBasis: "byte-bound"
36912
+ };
36913
+ const envelope = measuredForBounds ? baseline : providerMass({
36914
+ system: observed.systemTokens,
36915
+ tools: observed.toolDefinitionTokens
36916
+ }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true);
36917
+ const fit = observed.calibration ?? calibrationForModelKey(observed.modelKey);
36918
+ const upper = envelope + Math.max(UNKNOWN_FIT_RATIO, fit.toolsRatio, fit.proseRatio) * priced.reduce((sum, message) => sum + piFitMessageBytes(message), 0);
36919
+ if (upper <= contextLimit)
36920
+ return {
36921
+ tokens: upper,
36922
+ trusted: true,
36923
+ refusalGrade: false,
36924
+ refusalBasis: "byte-bound"
36925
+ };
36949
36926
  }
36950
- if (anchors.length === 0)
36951
- return resolved;
36952
- const fillEqualSpan = (messageStart, messageEnd, projectionStart, projectionEnd) => {
36953
- if (messageEnd - messageStart !== projectionEnd - projectionStart)
36954
- return;
36955
- for (let offset = 0;offset < messageEnd - messageStart; offset += 1) {
36956
- result[messageStart + offset] ??= alignedProjection[projectionStart + offset];
36957
- }
36958
- };
36959
- let previousMessageIndex = -1;
36960
- let previousProjectionIndex = -1;
36961
- for (const anchor of anchors) {
36962
- fillEqualSpan(previousMessageIndex + 1, anchor.messageIndex, previousProjectionIndex + 1, anchor.projectionIndex);
36963
- previousMessageIndex = anchor.messageIndex;
36964
- previousProjectionIndex = anchor.projectionIndex;
36927
+ if (!complete) {
36928
+ return {
36929
+ tokens: providerMass({
36930
+ system: observed.systemTokens,
36931
+ tools: observed.toolDefinitionTokens + messages.reduce((sum, message) => sum + piFitMessageBytes(message), 0)
36932
+ }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true),
36933
+ trusted: true,
36934
+ refusalGrade: false
36935
+ };
36965
36936
  }
36966
- fillEqualSpan(previousMessageIndex + 1, resolved.length, previousProjectionIndex + 1, alignedProjection.length);
36967
- return result;
36937
+ const bounded = messages.some((rawMessage) => {
36938
+ const content = rawMessage.content;
36939
+ if (typeof content === "string")
36940
+ return tokenCountUsesByteBound(content);
36941
+ return content.some((part) => [
36942
+ part.text,
36943
+ part.redacted ? undefined : part.thinking,
36944
+ part.name,
36945
+ part.arguments === undefined ? undefined : JSON.stringify(part.arguments)
36946
+ ].some((value) => typeof value === "string" && tokenCountUsesByteBound(value)));
36947
+ });
36948
+ if (bounded)
36949
+ return {
36950
+ tokens: providerMass({
36951
+ system: observed.systemTokens,
36952
+ tools: observed.toolDefinitionTokens + messages.reduce((sum, message) => sum + piFitMessageBytes(message), 0)
36953
+ }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true),
36954
+ trusted: true,
36955
+ refusalGrade: false,
36956
+ refusalBasis: "byte-bound"
36957
+ };
36958
+ const raw = tokenizePiMessages([...messages]);
36959
+ const calibration = observed.calibration ?? calibrationForModelKey(observed.modelKey);
36960
+ const refusalCalibration = calibrationForModelKey(observed.modelKey);
36961
+ const tokens = providerMass({
36962
+ system: observed.systemTokens,
36963
+ tools: observed.toolDefinitionTokens + raw.toolCall,
36964
+ prose: raw.conversation
36965
+ }, calibration, true);
36966
+ const refusalGrade = Number.isFinite(tokens) && messages.every((rawMessage) => {
36967
+ const content = rawMessage.content;
36968
+ if (typeof content === "string")
36969
+ return !tokenCountUsesByteBound(content);
36970
+ return content.every((part) => [
36971
+ part.text,
36972
+ part.redacted ? undefined : part.thinking,
36973
+ part.name,
36974
+ part.arguments === undefined ? undefined : JSON.stringify(part.arguments)
36975
+ ].every((value) => typeof value !== "string" || !tokenCountUsesByteBound(value)));
36976
+ }) && hasMeasuredDecisionCalibration(refusalCalibration) && observed.toolDefinitionsMeasured === true && Number.isFinite(observed.refusalToolDefinitionTokens) && observed.refusalToolDefinitionTokens >= 0;
36977
+ const measured = measuredPrefix && measuredPrefix.modelKey === observed.modelKey && observed.envelopeSignature && measuredPrefix.envelopeSignature === observed.envelopeSignature && Number.isSafeInteger(measuredPrefix.inputTokens) && measuredPrefix.inputTokens > 0 ? measuredPrefix : undefined;
36978
+ const tail = measured ? tokenizePiMessages([...measured.appendedMessages]) : raw;
36979
+ const refusalTokens = refusalGrade ? (measured?.inputTokens ?? 0) + providerMass({
36980
+ system: measured ? 0 : observed.systemTokens,
36981
+ tools: (measured ? 0 : observed.refusalToolDefinitionTokens) + tail.toolCall,
36982
+ prose: tail.conversation
36983
+ }, refusalCalibration) : undefined;
36984
+ return {
36985
+ tokens,
36986
+ trusted: Number.isFinite(tokens),
36987
+ refusalGrade,
36988
+ refusalTokens,
36989
+ refusalBasis: refusalGrade ? measured ? "provider-prefix" : "calibrated" : undefined
36990
+ };
36968
36991
  }
36969
- function piStorageErrorReason(error) {
36970
- const message = error instanceof Error ? error.message : String(error);
36971
- if (error && typeof error === "object") {
36972
- const code = error.code;
36973
- if (code === "SQLITE_BUSY" || code === "SQLITE_LOCKED" || code === "SQLITE_BUSY_SNAPSHOT") {
36974
- return code;
36992
+ function assertPiRawFallbackFits(messages, contextLimit, log, cause, observed, measuredPrefix) {
36993
+ if (!contextLimit || !Number.isFinite(contextLimit) || !observed || !Number.isFinite(observed.systemTokens) || observed.systemTokens <= 0 || !Number.isFinite(observed.toolDefinitionTokens) || observed.toolDefinitionTokens < 0 || observed.envelopeBytes !== undefined && (!Number.isFinite(observed.envelopeBytes) || observed.envelopeBytes < 0)) {
36994
+ log("raw_fallback_refused completeness=partial");
36995
+ throw piFallbackRefusal("fallback-metadata-incomplete", cause);
36996
+ }
36997
+ const measured = measuredPrefix && measuredPrefix.modelKey === observed.modelKey && observed.envelopeSignature && measuredPrefix.envelopeSignature === observed.envelopeSignature && Number.isSafeInteger(measuredPrefix.inputTokens) && measuredPrefix.inputTokens > 0 ? measuredPrefix : undefined;
36998
+ const baseInputTokens = measured?.inputTokens ?? 0;
36999
+ const pricedMessages = measured?.appendedMessages ?? messages;
37000
+ log(`lkg_fit_basis=${measured ? "provider_input" : "host_metadata"} measured_input=${baseInputTokens}`);
37001
+ let bytes = measured ? 1 : 1 + (observed.envelopeBytes ?? 20 + 4 * (observed.systemTokens + observed.toolDefinitionTokens));
37002
+ let serializationFailed = false;
37003
+ try {
37004
+ for (const message of pricedMessages) {
37005
+ bytes += piFitMessageBytes(message) + 1;
37006
+ if (baseInputTokens + Math.ceil(bytes / 4) > contextLimit)
37007
+ break;
36975
37008
  }
37009
+ } catch {
37010
+ serializationFailed = true;
36976
37011
  }
36977
- if (/database is locked|database table is locked/i.test(message)) {
36978
- return "SQLITE_BUSY_OR_LOCKED";
37012
+ const proxyTokens = baseInputTokens + Math.ceil(bytes / 4);
37013
+ if (serializationFailed || proxyTokens > contextLimit) {
37014
+ log(`raw_fallback_over_context_limit proxy_bytes=${bytes} proxy_tokens=${proxyTokens} limit=${contextLimit} early_abort=true serialization_failed=${serializationFailed}`);
37015
+ throw piFallbackRefusal(serializationFailed ? "fallback-serialization-failed" : "fallback-over-context-limit", cause);
36979
37016
  }
36980
- if (error && typeof error === "object") {
36981
- const code = error.code;
36982
- if (typeof code === "string" && code.length > 0)
36983
- return code;
37017
+ const complete = pricedMessages.every((message) => {
37018
+ if (!message || typeof message !== "object")
37019
+ return false;
37020
+ const m = message;
37021
+ return ["user", "assistant", "toolResult", "custom"].includes(m.role ?? "") && (typeof m.content === "string" || Array.isArray(m.content) && m.content.every((p) => p !== null && typeof p === "object" && ["text", "thinking", "redacted_thinking", "toolCall"].includes(String(p.type))));
37022
+ });
37023
+ const raw = tokenizePiMessages([...pricedMessages]);
37024
+ if (!hasTokenizerForFit()) {
37025
+ log("raw_fallback_refused completeness=partial tokenizer=unavailable");
37026
+ throw piFallbackRefusal("fallback-tokenizer-unavailable", cause);
37027
+ }
37028
+ const tokens = baseInputTokens + providerMass({
37029
+ system: measured ? 0 : observed.systemTokens,
37030
+ tools: (measured ? 0 : observed.toolDefinitionTokens) + raw.toolCall,
37031
+ prose: raw.conversation
37032
+ }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true);
37033
+ if (!complete || !Number.isFinite(tokens) || tokens <= 0 || tokens > contextLimit || serializationFailed || proxyTokens > contextLimit) {
37034
+ log(`raw_fallback_over_context_limit proxy_bytes=${bytes} proxy_tokens=${proxyTokens} limit=${contextLimit} early_abort=true serialization_failed=${serializationFailed}`);
37035
+ throw piFallbackRefusal(complete ? "fallback-over-context-limit" : "fallback-messages-incomplete", cause);
36984
37036
  }
36985
- return message;
36986
37037
  }
36987
- function plainJsonFields(value) {
36988
- const fields = [];
36989
- const seen = new WeakSet;
36990
- const visit = (child) => {
36991
- if (child === null)
36992
- fields.push(LKG_SNAPSHOT_NULL);
36993
- else if (typeof child === "string")
36994
- fields.push(LKG_SNAPSHOT_STRING, child);
36995
- else if (typeof child === "number")
36996
- fields.push(LKG_SNAPSHOT_NUMBER, child);
36997
- else if (typeof child === "boolean")
36998
- fields.push(LKG_SNAPSHOT_BOOLEAN, child);
36999
- else if (typeof child !== "object")
37000
- fields.push(LKG_SNAPSHOT_UNDEFINED);
37001
- else {
37002
- if (seen.has(child))
37003
- return false;
37004
- const array = Array.isArray(child);
37005
- const prototype = Object.getPrototypeOf(child);
37006
- if (!array && prototype !== Object.prototype && prototype !== null || "toJSON" in child)
37007
- return false;
37008
- const keys = Object.keys(child);
37009
- if (array && keys.length !== child.length)
37010
- return false;
37011
- seen.add(child);
37012
- fields.push(array ? LKG_SNAPSHOT_ARRAY : LKG_SNAPSHOT_OBJECT);
37013
- const countIndex = fields.length;
37014
- fields.push(array ? child.length : 0);
37015
- let count = 0;
37016
- for (const key of keys) {
37017
- const descriptor = Object.getOwnPropertyDescriptor(child, key);
37018
- if (!descriptor || !("value" in descriptor) || array && key !== String(count))
37019
- return false;
37020
- const entry = descriptor.value;
37021
- if (!array && (entry === undefined || typeof entry === "function" || typeof entry === "symbol"))
37022
- continue;
37023
- count++;
37024
- if (!array)
37025
- fields.push(LKG_SNAPSHOT_KEY, key);
37026
- if (!visit(entry))
37027
- return false;
37028
- }
37029
- if (!array)
37030
- fields[countIndex] = count;
37031
- seen.delete(child);
37032
- }
37033
- return true;
37038
+ function classifyPiOutgoingFit(estimate, limit) {
37039
+ if (!limit || !Number.isFinite(limit) || limit <= 0)
37040
+ return "fits-or-unknown";
37041
+ if (estimate.refusalGrade && typeof estimate.refusalTokens === "number" && Number.isFinite(estimate.refusalTokens) && estimate.refusalTokens > limit)
37042
+ return "over-window-proof";
37043
+ return estimate.trusted && Number.isFinite(estimate.tokens) && estimate.tokens > limit ? "upper-estimate-pressure" : "fits-or-unknown";
37044
+ }
37045
+
37046
+ // src/pi-context-refusal.ts
37047
+ var ENTRY_TYPE = "magic-context-turn-refused";
37048
+ var RETRY_MESSAGE = "Magic Context could not safely prepare this turn; send your message again.";
37049
+ var MAX_UNSENDABLE_NONCES_PER_SESSION = 32;
37050
+ var receiptMaps = new Set;
37051
+ function clearPiContextReceipt(sessionId) {
37052
+ for (const receipts of receiptMaps)
37053
+ receipts.delete(sessionId);
37054
+ }
37055
+ function createPiContextOwnershipGuards(operations, key, active, budget, owner) {
37056
+ const sideTurn = budget.sideTurn;
37057
+ const signal = budget.signal;
37058
+ const isOwned = () => sideTurn ? operations.get(key) === owner : operations.get(key) === active && active.pass === budget;
37059
+ const isCurrent = () => isOwned() && (sideTurn || !active.ended);
37060
+ const assertOwner = () => {
37061
+ if (!isCurrent())
37062
+ throw new PiContextSupersededError("Pi context pass superseded: no longer valid for this operation", active.ended ? "agent_end" : operations.get(key) !== (sideTurn ? owner : active) ? "operation replacement or cache eviction" : "newer context pass");
37063
+ signal?.throwIfAborted();
37034
37064
  };
37035
- if (!visit(value))
37036
- return null;
37037
- const normalized = contentSnapshotValue(value);
37038
- return normalized === value ? fields : lkgContentFields(normalized);
37065
+ return { isOwned, isCurrent, assertOwner };
37039
37066
  }
37040
- function plainOutputFields(messages) {
37041
- if ("toJSON" in messages || Object.keys(messages).length !== messages.length)
37042
- return null;
37043
- const outputs = [];
37044
- for (let index = 0;index < messages.length; index++) {
37045
- const descriptor = Object.getOwnPropertyDescriptor(messages, String(index));
37046
- if (!descriptor || !("value" in descriptor))
37047
- return null;
37048
- const fields = plainJsonFields(descriptor.value);
37049
- if (!fields)
37050
- return null;
37051
- outputs.push({ id: String(index), fields });
37052
- }
37053
- return outputs;
37067
+ function releasePiOperationPayloads(operation) {
37068
+ operation.inputKeys = undefined;
37069
+ operation.inputMessages = undefined;
37070
+ operation.inputBranch = undefined;
37071
+ operation.dispatchFields = undefined;
37072
+ operation.managedSignature = undefined;
37073
+ operation.preparedStructure = undefined;
37054
37074
  }
37055
- function snapshotInputs(messages, entryIds, omitsBookkeeping) {
37056
- if (!entryIds || entryIds.length !== messages.length) {
37057
- return { inputs: [], failure: "lkg_entry_ids_unavailable" };
37058
- }
37059
- const firstStableIndex = entryIds.findIndex((id) => typeof id === "string" && id.length > 0);
37060
- if (firstStableIndex < 0) {
37061
- return { inputs: [], failure: "lkg_entry_ids_unavailable" };
37062
- }
37063
- const inputs = [];
37064
- const seen = new Set;
37065
- for (let index = 0;index < entryIds.length; index += 1) {
37066
- const fields = lkgContentFields(omitsBookkeeping ? piProviderInput(messages[index]) : messages[index]);
37067
- if (!fields)
37068
- return { inputs: [], failure: "lkg_content_snapshot_failed" };
37069
- const id = entryIds[index] || `pi-lkg-unmapped:${lkgContentDigestFromFields(fields)}`;
37070
- if (seen.has(id))
37071
- return { inputs: [], failure: "lkg_duplicate_entry_id" };
37072
- seen.add(id);
37073
- inputs.push({
37074
- id,
37075
- messageIndex: index,
37076
- fields,
37077
- providerUsageSignature: completedProviderUsage(messages[index])?.signature
37078
- });
37079
- }
37080
- return { inputs, failure: null };
37075
+ function operationKey(ctx) {
37076
+ return ctx.sessionManager?.getSessionId?.() ?? ctx.sessionManager ?? "no-session";
37081
37077
  }
37082
- var bookkeepingOmittingApis = new Set([
37083
- "anthropic-messages",
37084
- "google-generative-ai",
37085
- "openai-completions",
37086
- "openai-responses",
37087
- "openai-codex-responses"
37088
- ]);
37089
- function omitsRootBookkeeping(args) {
37090
- if (args.transport != null)
37078
+ function messageKey(message) {
37079
+ if (!message || typeof message !== "object")
37080
+ return;
37081
+ const { role, timestamp } = message;
37082
+ return typeof role === "string" && typeof timestamp === "number" ? `${role}:${timestamp}` : undefined;
37083
+ }
37084
+ function endBelongsToEarlierRun(operation, event) {
37085
+ if (!operation.pass || operation.dispatched)
37091
37086
  return false;
37092
- let api = args.apiKey;
37093
- if (api === undefined) {
37094
- const apis = new Set(args.messages.flatMap((message) => {
37095
- const value = message;
37096
- return value?.role === "assistant" && typeof value.api === "string" ? [value.api] : [];
37097
- }));
37098
- if (apis.size === 1)
37099
- api = [...apis][0];
37087
+ const end = event;
37088
+ if (end?.willContinue === true)
37089
+ return true;
37090
+ if (!operation.inputKeys || !Array.isArray(end?.messages))
37091
+ return false;
37092
+ const ended = new Set;
37093
+ for (const message of end.messages) {
37094
+ const key = messageKey(message);
37095
+ if (key)
37096
+ ended.add(key);
37100
37097
  }
37101
- return typeof api === "string" && bookkeepingOmittingApis.has(api);
37098
+ if (ended.size === 0)
37099
+ return false;
37100
+ return operation.inputKeys.some((key) => !ended.has(key));
37102
37101
  }
37103
- function piProviderInput(message) {
37104
- if (!message || typeof message !== "object" || Array.isArray(message))
37105
- return message;
37106
- const descriptors = Object.getOwnPropertyDescriptors(message);
37107
- delete descriptors.completedAt;
37108
- delete descriptors.contextSnapshot;
37109
- return Object.create(Object.getPrototypeOf(message), descriptors);
37102
+ function messageSnapshots(messages) {
37103
+ const snapshots = [];
37104
+ for (const message of messages) {
37105
+ const id = messageKey(message);
37106
+ const fields = lkgContentFields(message);
37107
+ if (!id || !fields)
37108
+ return;
37109
+ snapshots.push({ id, fields });
37110
+ }
37111
+ return snapshots.length ? snapshots : undefined;
37110
37112
  }
37111
- function restorePiLkgInputForFit(snapshot, branchEntries) {
37112
- if (!snapshot || snapshot.preparationFailure || !branchEntries || !snapshot.inputs.length)
37113
+ function inputBranch(ctx, inputs) {
37114
+ try {
37115
+ const branch = ctx.sessionManager.getBranch();
37116
+ const entries = branch.filter((entry) => entry.type === "message" || entry.type === "custom_message");
37117
+ const anchorId = entries.at(-1)?.id;
37118
+ const native = messageSnapshots(entries.map((entry) => {
37119
+ if (entry.type === "message")
37120
+ return entry.message;
37121
+ const attribution = entry.attribution;
37122
+ const timestamp = new Date(entry.timestamp).getTime();
37123
+ if (!Number.isFinite(timestamp) || !entry.customType || typeof entry.content !== "string" && !Array.isArray(entry.content) || attribution !== undefined && attribution !== "agent" && attribution !== "user")
37124
+ return;
37125
+ return {
37126
+ role: "custom",
37127
+ customType: entry.customType,
37128
+ content: entry.content,
37129
+ display: entry.display,
37130
+ details: entry.details,
37131
+ attribution,
37132
+ timestamp
37133
+ };
37134
+ }));
37135
+ inputs ??= native;
37136
+ if (!anchorId || !native || !inputs || native.length > inputs.length || exactReusablePrefix(native, inputs) !== native.length)
37137
+ return;
37138
+ return {
37139
+ anchorId,
37140
+ anchorIndex: native.length - 1,
37141
+ entryIds: entries.map((entry) => entry.id),
37142
+ messages: inputs
37143
+ };
37144
+ } catch {
37113
37145
  return;
37114
- const byId = new Map;
37115
- for (const value of branchEntries) {
37116
- const entry = value;
37117
- if (entry?.type !== "message" || typeof entry.id !== "string")
37118
- continue;
37119
- if (byId.has(entry.id))
37120
- return;
37121
- byId.set(entry.id, entry.message);
37122
37146
  }
37123
- const input = [];
37124
- for (const saved of snapshot.inputs) {
37125
- if (!byId.has(saved.id))
37126
- return;
37127
- const message = byId.get(saved.id);
37128
- if (!message || typeof message !== "object" || Array.isArray(message) || Object.hasOwn(message, "parts"))
37129
- return;
37130
- const fields = plainJsonFields(snapshot.omitsBookkeeping ? piProviderInput(message) : message);
37131
- if (!fields || fields.length !== saved.fields.length || fields.some((field, i) => field !== saved.fields[i]))
37132
- return;
37133
- input.push(message);
37147
+ }
37148
+ function dispatchedEndBelongsToEarlierRun(operation, event, ctx) {
37149
+ if (!operation.dispatched || !operation.started || !operation.ready || !operation.pass?.completed || operation.refused || operation.pass.abandoned || !operation.inputBranch)
37150
+ return false;
37151
+ try {
37152
+ if (ctx.isIdle() !== false)
37153
+ return false;
37154
+ const current = inputBranch(ctx);
37155
+ const prepared = operation.inputBranch;
37156
+ if (!current || current.entryIds[prepared.anchorIndex] !== prepared.anchorId || current.messages.length !== prepared.messages.length || exactReusablePrefix(current.messages, prepared.messages) !== prepared.messages.length)
37157
+ return false;
37158
+ const messages = event?.messages;
37159
+ const ended = Array.isArray(messages) ? messageSnapshots(messages) : undefined;
37160
+ return !!(ended && ended.length <= prepared.anchorIndex && ended.length < prepared.messages.length && exactReusablePrefix(prepared.messages, ended) === ended.length);
37161
+ } catch {
37162
+ return false;
37134
37163
  }
37164
+ }
37165
+ function inputMessageKeys(event) {
37135
37166
  try {
37136
- return structuredClone(input);
37167
+ const messages = event.messages;
37168
+ if (!Array.isArray(messages))
37169
+ return;
37170
+ const keys = [];
37171
+ for (const message of messages) {
37172
+ const key = messageKey(message);
37173
+ if (key)
37174
+ keys.push(key);
37175
+ }
37176
+ return keys;
37137
37177
  } catch {
37138
37178
  return;
37139
37179
  }
37140
37180
  }
37141
- function extendsMeasuredRequest(measuredJson, nextJson) {
37142
- return measuredJson === nextJson || measuredJson.length > 2 && nextJson.startsWith(`${measuredJson.slice(0, -1)},`);
37143
- }
37144
- function createPiLkgCoordinator(db, scheduleCapture = (capture) => setImmediate(capture), onCaptureTiming) {
37145
- registerLkgPersistence(createDbLkgPersistence(db));
37146
- const stateFor = (sessionId) => {
37147
- let state = piLkgSessionStates.get(sessionId);
37148
- if (!state) {
37149
- state = {
37150
- captureSequence: 0,
37151
- syncCaptureRequired: false,
37152
- acceptedInputs: null,
37153
- acceptedSlot: null,
37154
- outputSnapshot: null
37155
- };
37156
- piLkgSessionStates.set(sessionId, state);
37157
- }
37158
- return state;
37159
- };
37160
- const recoveryFor = (snapshot) => {
37161
- const state = stateFor(snapshot.sessionId);
37162
- const prefix = state.recoveryPrefix;
37163
- if (!prefix) {
37164
- const slot = getSlot(snapshot.sessionId);
37165
- return lkgRecoveryFenceUnclean(snapshot.sessionId) || slot && lkgFenceRequiresRebuild(snapshot.sessionId, state.acceptedInputs ? slot : loadPersistedLkgSlot(db, snapshot.sessionId) ?? slot, !!state.acceptedInputs) ? { rebuildReason: "lkg_restart_rebuild" } : {};
37181
+ function registerPiGuardedContext(pi, handler, options = {}) {
37182
+ const operations = new Map;
37183
+ const sideSessions = new Map;
37184
+ receiptMaps.add(operations);
37185
+ const newOperation = () => ({
37186
+ retainedAfterEnd: false,
37187
+ dispatched: false,
37188
+ started: false,
37189
+ ready: false,
37190
+ refused: false,
37191
+ ended: false
37192
+ });
37193
+ const lifecycle = new Map;
37194
+ receiptMaps.add(lifecycle);
37195
+ const lifecycleCounts = (key) => {
37196
+ let counts = lifecycle.get(key);
37197
+ if (!counts) {
37198
+ counts = { starts: 0, ends: 0 };
37199
+ lifecycle.set(key, counts);
37166
37200
  }
37167
- const durableSlot = loadPersistedLkgSlot(db, snapshot.sessionId) ?? getSlot(snapshot.sessionId);
37168
- if (snapshot.preparationFailure || !durableSlot || !ownsLkgDirtyFence(snapshot.sessionId, prefix.fenceRevision) || prefix.modelKey !== snapshot.modelKey || prefix.providerKey !== snapshot.providerKey || JSON.stringify(prefix.systemEntries) !== JSON.stringify(snapshot.systemEntries) || exactReusablePrefix(snapshot.inputs, prefix.inputs) !== prefix.inputs.length)
37169
- return { rebuildReason: "lkg_recovery_input_rebuild" };
37170
- return { prefix };
37171
- };
37172
- const recoveryEntryIds = (snapshot) => {
37173
- const anchor = snapshot.replayAnchorInputIndex;
37174
- const slot = getSlot(snapshot.sessionId);
37175
- if (snapshot.preparationFailure || anchor === null || !slot?.piOutputEntryIds)
37176
- throw new PiLkgUncapturable("lkg_output_mapping_unavailable", "Cannot retain recovered output ownership");
37177
- const surviving = new Set(snapshot.inputs.map((input) => input.id));
37178
- return [
37179
- ...slot.piOutputEntryIds.filter((id) => id === null || surviving.has(id)),
37180
- ...snapshot.inputs.slice(anchor + 1).map((input) => input.id)
37181
- ];
37182
- };
37183
- const noteRecovery = (snapshot, messages, entryIds) => {
37184
- const state = stateFor(snapshot.sessionId);
37185
- const slot = getSlot(snapshot.sessionId);
37186
- if (!slot)
37187
- throw new PiLkgUncapturable("lkg_miss", "Cannot retain recovery without a saved request");
37188
- const anchor = snapshot.replayAnchorInputIndex;
37189
- if (anchor === null)
37190
- throw new PiLkgUncapturable("lkg_miss", "Cannot retain recovery without an input anchor");
37191
- const outputEntryIds = entryIds ?? recoveryEntryIds(snapshot);
37192
- if (outputEntryIds.length !== messages.length)
37193
- throw new PiLkgUncapturable("lkg_output_mapping_unavailable", "Recovered output ownership does not match the served array");
37194
- state.recoveryPrefix = {
37195
- fenceRevision: markLkgRecoveryDirty({
37196
- sessionId: snapshot.sessionId,
37197
- slot,
37198
- processLocalProof: !!state.acceptedInputs,
37199
- ownDirtyRevision: state.recoveryPrefix?.fenceRevision
37200
- }),
37201
- modelKey: snapshot.modelKey,
37202
- providerKey: snapshot.providerKey,
37203
- systemEntries: snapshot.systemEntries,
37204
- inputs: snapshot.inputs,
37205
- json: JSON.stringify(messages),
37206
- outputEntryIds,
37207
- untaggedInputIds: new Set([
37208
- ...state.recoveryPrefix?.untaggedInputIds ?? [],
37209
- ...snapshot.inputs.slice(anchor + 1).map((input) => input.id)
37210
- ])
37211
- };
37201
+ return counts;
37212
37202
  };
37213
- const beginPass = (args) => {
37214
- const omitsBookkeeping = omitsRootBookkeeping(args);
37215
- const snapped = snapshotInputs(args.messages, args.entryIds, omitsBookkeeping);
37216
- const slot = getSlot(args.sessionId);
37217
- if (snapped.failure || !slot) {
37218
- return {
37219
- omitsBookkeeping,
37220
- sessionId: args.sessionId,
37221
- inputs: snapped.inputs,
37222
- preparationFailure: snapped.failure,
37223
- replayFailure: snapped.failure ?? "lkg_miss",
37224
- replayAnchorInputIndex: null,
37225
- pristineTail: null,
37226
- modelKey: args.modelKey,
37227
- providerKey: args.providerKey
37228
- };
37229
- }
37230
- const stableAnchorIndex = snapped.inputs.findIndex((input) => input.id === slot.lastInputMessageId);
37231
- if (stableAnchorIndex < 0) {
37232
- return {
37233
- omitsBookkeeping,
37234
- sessionId: args.sessionId,
37235
- inputs: snapped.inputs,
37236
- preparationFailure: null,
37237
- replayFailure: "lkg_invalidated_reshape",
37238
- replayAnchorInputIndex: null,
37239
- pristineTail: null,
37240
- modelKey: args.modelKey,
37241
- providerKey: args.providerKey
37242
- };
37243
- }
37244
- const messageAnchorIndex = snapped.inputs[stableAnchorIndex]?.messageIndex;
37245
- if (messageAnchorIndex === undefined) {
37246
- return {
37247
- omitsBookkeeping,
37248
- sessionId: args.sessionId,
37249
- inputs: snapped.inputs,
37250
- preparationFailure: null,
37251
- replayFailure: "lkg_invalidated_reshape",
37252
- replayAnchorInputIndex: null,
37253
- pristineTail: null,
37254
- modelKey: args.modelKey,
37255
- providerKey: args.providerKey
37256
- };
37203
+ const fenceEnabled = resolvePiHarnessKind() === "omp";
37204
+ const unsendableNonces = new Map;
37205
+ receiptMaps.add(unsendableNonces);
37206
+ const structureWarnings = new Map;
37207
+ receiptMaps.add(structureWarnings);
37208
+ const imageTrimNotices = new Map;
37209
+ receiptMaps.add(imageTrimNotices);
37210
+ const refuse = (ctx, budget, reason, isCurrent) => {
37211
+ if (!isCurrent()) {
37212
+ log2(`[magic-context][pi] DISCARDED CONTEXT RESULT ${budget.diagnostic()}`, reason);
37213
+ return;
37257
37214
  }
37215
+ budget.abandoned = true;
37216
+ budget.recovery = budget.recovery === "not attempted" ? "no managed result; refused" : budget.recovery;
37217
+ const identityConflict = findPiTagIdentityConflict2(reason);
37218
+ const message = identityConflict ? `${identityConflict.message}. ${budget.diagnostic()}` : reason instanceof PiDegradedPassError ? `${reason.message} ${budget.diagnostic()}` : `${RETRY_MESSAGE} ${budget.diagnostic()} (${reason instanceof Error ? reason.message : String(reason)})`;
37219
+ log2("[magic-context][pi] CONTEXT REFUSED", message);
37258
37220
  try {
37259
- return {
37260
- omitsBookkeeping,
37261
- sessionId: args.sessionId,
37262
- inputs: snapped.inputs,
37263
- preparationFailure: null,
37264
- replayFailure: null,
37265
- replayAnchorInputIndex: stableAnchorIndex,
37266
- pristineTail: structuredClone(args.messages.slice(messageAnchorIndex + 1)),
37267
- modelKey: args.modelKey,
37268
- providerKey: args.providerKey
37269
- };
37270
- } catch {
37271
- return {
37272
- omitsBookkeeping,
37273
- sessionId: args.sessionId,
37274
- inputs: snapped.inputs,
37275
- preparationFailure: null,
37276
- replayFailure: "lkg_tail_snapshot_failed",
37277
- replayAnchorInputIndex: null,
37278
- pristineTail: null,
37279
- modelKey: args.modelKey,
37280
- providerKey: args.providerKey
37281
- };
37221
+ ctx.ui?.notify(message, "error");
37222
+ } catch (error) {
37223
+ log2("[magic-context][pi] refusal notice failed", error);
37282
37224
  }
37283
- };
37284
- const measuredPrefixFor = (snapshot, slot, parentOf) => {
37285
- const request = stateFor(snapshot.sessionId).measuredRequest;
37286
- const anchor = snapshot.replayAnchorInputIndex;
37287
- if (!request?.usage || !slot || !parentOf || anchor === null || !snapshot.pristineTail || !extendsMeasuredRequest(request.jsonPrefix, slot.jsonPrefix) || !request.modelKey || request.modelKey !== slot.modelKey || request.modelKey !== snapshot.modelKey || request.providerKey !== slot.providerKey || request.providerKey !== snapshot.providerKey || snapshot.inputs[anchor]?.id !== slot.lastInputMessageId || exactReusablePrefix(snapshot.inputs, request.inputs) !== request.inputs.length)
37288
- return;
37289
- const assistant = snapshot.inputs[request.inputs.length];
37290
- const assistantId = assistant?.id;
37291
- if (!assistantId || assistantId.startsWith("pi-lkg-unmapped:") || assistant.providerUsageSignature !== request.usage.signature)
37292
- return;
37293
- let appendedMessages;
37294
37225
  try {
37295
- if (parentOf(assistantId) !== request.inputs.at(-1)?.id)
37296
- return;
37297
- const measuredLength = JSON.parse(request.jsonPrefix).length;
37298
- const prefix = JSON.parse(slot.jsonPrefix);
37299
- const replyId = measuredLength < prefix.length ? slot.piOutputEntryIds?.[measuredLength] : snapshot.inputs[anchor + 1]?.id;
37300
- if (replyId !== assistantId)
37301
- return;
37302
- appendedMessages = [
37303
- ...prefix.slice(measuredLength),
37304
- ...snapshot.pristineTail
37305
- ];
37306
- if (completedProviderUsage(appendedMessages[0])?.signature !== request.usage.signature)
37307
- return;
37308
- } catch {
37309
- return;
37310
- }
37311
- return {
37312
- modelKey: piModelRefToCanonical(request.modelKey).toLowerCase(),
37313
- inputTokens: request.usage.inputTokens,
37314
- envelopeSignature: request.envelopeSignature,
37315
- appendedMessages,
37316
- previousMessages: JSON.parse(request.jsonPrefix)
37317
- };
37318
- };
37319
- const replay = (snapshot, parentOf) => {
37320
- if (snapshot.replayFailure) {
37321
- if (snapshot.replayFailure === "lkg_invalidated_reshape") {
37322
- dropSlot(snapshot.sessionId, snapshot.replayFailure);
37323
- stateFor(snapshot.sessionId).acceptedInputs = null;
37324
- }
37325
- return { ok: false, reason: snapshot.replayFailure };
37326
- }
37327
- if (snapshot.replayAnchorInputIndex === null || snapshot.pristineTail === null) {
37328
- return { ok: false, reason: "lkg_miss" };
37329
- }
37330
- const slot = getSlot(snapshot.sessionId);
37331
- const start = slot?.inputIdSeq.indexOf(snapshot.inputs[0]?.id ?? "") ?? -1;
37332
- if (slot && start > 0) {
37333
- const ownership = slot.piOutputEntryIds;
37334
- if (!ownership)
37335
- return { ok: false, reason: "lkg_output_mapping_unavailable" };
37336
- if (slot.modelKey !== snapshot.modelKey || slot.providerKey !== snapshot.providerKey)
37337
- return { ok: false, reason: "lkg_model_mismatch" };
37338
- const surviving = snapshot.inputs.slice(0, snapshot.replayAnchorInputIndex + 1);
37339
- if (surviving.length !== slot.inputIdSeq.length - start || surviving.some((input, index) => input.id !== slot.inputIdSeq[start + index]))
37340
- return { ok: false, reason: "lkg_invalidated_reshape" };
37341
- if (surviving.some((input, index) => !lkgFieldsMatchDigest(input.fields, slot.inputContentDigests[start + index])))
37342
- return { ok: false, reason: "lkg_content_mismatch" };
37343
- const removed = new Set(slot.inputIdSeq.slice(0, start));
37344
- const prefix = JSON.parse(slot.jsonPrefix);
37345
- const measuredPrefix = measuredPrefixFor(snapshot, slot, parentOf);
37346
- return {
37347
- ok: true,
37348
- ...measuredPrefix ? { measuredPrefix } : {},
37349
- messages: [
37350
- ...prefix.filter((message, index) => isPiSystemEntry(message) || ownership[index] === null || !removed.has(ownership[index] ?? "")),
37351
- ...snapshot.pristineTail
37352
- ]
37353
- };
37354
- }
37355
- const prefixInputs = snapshot.inputs.slice(0, snapshot.replayAnchorInputIndex + 1);
37356
- const entry = {
37357
- pristineTail: snapshot.pristineTail,
37358
- entryInputIds: snapshot.inputs.map((input) => input.id),
37359
- entryContentDigests: prefixInputs.map((input) => lkgContentDigestFromFields(input.fields)),
37360
- ...slot?.inputContentDigests.some(isLegacyLkgDigest) ? {
37361
- entryLegacyContentDigests: prefixInputs.map((input) => legacyLkgDigestFromFields(input.fields))
37362
- } : {},
37363
- anchorIndex: snapshot.replayAnchorInputIndex
37364
- };
37365
- const result = replayLkg({
37366
- sessionId: snapshot.sessionId,
37367
- messages: [],
37368
- modelKey: snapshot.modelKey,
37369
- providerKey: snapshot.providerKey,
37370
- entry,
37371
- skipSeamValidation: true
37372
- });
37373
- if (!result.ok)
37374
- stateFor(snapshot.sessionId).acceptedInputs = null;
37375
- const measuredPrefix = result.ok ? measuredPrefixFor(snapshot, slot, parentOf) : undefined;
37376
- return result.ok && measuredPrefix ? { ...result, measuredPrefix } : result;
37377
- };
37378
- const captureAppliedPass = (args) => {
37379
- args.assertCurrentPass?.();
37380
- const { snapshot } = args;
37381
- if (snapshot.preparationFailure || snapshot.inputs.length === 0) {
37382
- if (args.publishServedNumbers)
37383
- throw new PiLkgUncapturable(snapshot.preparationFailure ?? "lkg_inputs_empty", "Pi LKG input preparation failed");
37384
- return;
37226
+ pi.appendEntry(ENTRY_TYPE, { message });
37227
+ } catch (error) {
37228
+ log2("[magic-context][pi] refusal entry failed", error);
37229
+ } finally {
37230
+ if (!budget.sideTurn)
37231
+ ctx.abort();
37385
37232
  }
37386
- const state = stateFor(snapshot.sessionId);
37387
- let jsonPrefix;
37388
37233
  try {
37389
- const detachedOutputs = plainOutputFields(args.outputMessages);
37390
- const plainOutput = detachedOutputs !== null;
37391
- const outputs = detachedOutputs ?? [];
37392
- const priorOutput = plainOutput ? state.outputSnapshot : null;
37393
- const prefix = exactReusablePrefix(outputs, priorOutput?.inputs ?? null);
37394
- const jsonMessages = [
37395
- ...priorOutput?.jsonMessages.slice(0, prefix) ?? [],
37396
- ...plainOutput ? args.outputMessages.slice(prefix).map((message) => JSON.stringify(message) ?? "null") : []
37397
- ];
37398
- jsonPrefix = !plainOutput ? JSON.stringify(args.outputMessages) : priorOutput && prefix === outputs.length && prefix === priorOutput.inputs.length ? priorOutput.json : `[${jsonMessages.join(",")}]`;
37399
- args.assertCurrentPass?.();
37400
- state.outputSnapshot = plainOutput ? {
37401
- inputs: outputs,
37402
- jsonMessages,
37403
- json: jsonPrefix
37404
- } : null;
37234
+ options.onRefusal?.(ctx, budget, message, isCurrent);
37405
37235
  } catch (error) {
37406
- args.assertCurrentPass?.();
37407
- if (args.publishServedNumbers)
37408
- throw new PiLkgUncapturable("lkg_output_serialize_failed", error instanceof Error ? error.message : String(error), error);
37409
- dropSlot(snapshot.sessionId, "lkg_snapshot_serialize_failed");
37410
- const failedState = stateFor(snapshot.sessionId);
37411
- failedState.syncCaptureRequired = true;
37412
- failedState.acceptedInputs = null;
37413
- sessionLog2(snapshot.sessionId, "LKG SNAPSHOT PREPARATION FAILED; forcing synchronous capture on the next applied pass:", error);
37414
- return;
37236
+ log2("[magic-context][pi] refusal diagnostic failed", error);
37415
37237
  }
37416
- const idsByDigest = new Map;
37417
- if (!args.outputEntryIds)
37418
- for (const input of snapshot.inputs) {
37419
- const digest = lkgContentDigestFromFields(input.fields);
37420
- idsByDigest.set(digest, idsByDigest.has(digest) ? undefined : input.id);
37421
- }
37422
- const inferredIds = args.outputEntryIds ?? args.outputMessages.map((message) => {
37423
- const fields = lkgContentFields(message);
37424
- if (!fields)
37425
- return;
37426
- const digest = lkgContentDigestFromFields(fields);
37427
- return idsByDigest.get(digest);
37238
+ };
37239
+ if (fenceEnabled) {
37240
+ pi.on("agent_start", (_event, ctx) => {
37241
+ const key = operationKey(ctx);
37242
+ lifecycleCounts(key).starts++;
37243
+ const pending = operations.get(key);
37244
+ const active = pending && !pending.started && !pending.ended ? pending : newOperation();
37245
+ active.started = true;
37246
+ operations.set(key, active);
37428
37247
  });
37429
- const outputIds = args.outputEntryIds ?? inferredIds;
37430
- const inputIds = new Set(snapshot.inputs.map((input) => input.id));
37431
- const ownership = outputIds.length === args.outputMessages.length && outputIds.every((id) => id === null || typeof id === "string" && inputIds.has(id)) ? [...outputIds] : undefined;
37432
- args.assertCurrentPass?.();
37433
- state.captureSequence += 1;
37434
- const plan = {
37435
- sessionId: snapshot.sessionId,
37436
- inputs: snapshot.inputs,
37437
- jsonPrefix,
37438
- modelKey: snapshot.modelKey,
37439
- providerKey: snapshot.providerKey,
37440
- capturedAt: Date.now(),
37441
- captureSequence: state.captureSequence
37442
- };
37443
- const noteCapturedRequest = () => {
37444
- if (state.measuredRequest && (state.measuredRequest.modelKey !== plan.modelKey || state.measuredRequest.providerKey !== plan.providerKey || state.measuredRequest.envelopeSignature !== args.hostEnvelopeSignature || !extendsMeasuredRequest(state.measuredRequest.jsonPrefix, jsonPrefix)))
37445
- state.measuredRequest = undefined;
37446
- state.capturedRequest = args.hostEnvelopeSignature ? { ...plan, envelopeSignature: args.hostEnvelopeSignature } : undefined;
37447
- };
37448
- if (!args.publishServedNumbers)
37449
- noteCapturedRequest();
37450
- const livePrior = getSlot(snapshot.sessionId);
37451
- const unchanged = livePrior && livePrior.modelKey === plan.modelKey && livePrior.providerKey === plan.providerKey && livePrior.jsonPrefix === plan.jsonPrefix && livePrior.inputIdSeq.length === plan.inputs.length && exactReusablePrefix(plan.inputs, state.acceptedInputs) === plan.inputs.length && JSON.stringify(livePrior.piOutputEntryIds) === JSON.stringify(ownership);
37452
- args.assertCurrentPass?.();
37453
- if (unchanged && !state.syncCaptureRequired && !args.publishServedNumbers) {
37454
- const kept = {
37455
- ...livePrior,
37456
- capturedAt: plan.capturedAt,
37457
- captureSequence: plan.captureSequence
37458
- };
37459
- if (captureSlot(plan.sessionId, kept))
37460
- state.acceptedSlot = kept;
37461
- else
37462
- state.syncCaptureRequired = true;
37463
- }
37464
- if (args.cacheBusting && !unchanged) {
37465
- dropSlot(snapshot.sessionId, "lkg_cache_bust_pending_capture");
37466
- }
37467
- const commit = () => {
37468
- try {
37469
- args.assertCurrentPass?.();
37470
- } catch (error) {
37471
- if (args.publishServedNumbers)
37472
- throw error;
37473
- sessionLog2(plan.sessionId, "DISCARDED CONTEXT RESULT: queued LKG capture", error);
37474
- return;
37475
- }
37476
- if (plan.captureSequence !== state.captureSequence)
37477
- return;
37478
- const startedAt = performance.now();
37479
- let reusedPrefix = 0;
37480
- try {
37481
- const inputIdSeq = plan.inputs.map((input) => input.id);
37482
- const prior = getSlot(plan.sessionId);
37483
- const digestPrior = prior ?? state.acceptedSlot;
37484
- const reusePrior = digestPrior?.inputContentSignatures !== undefined && digestPrior.modelKey === plan.modelKey && digestPrior.providerKey === plan.providerKey ? {
37485
- slot: digestPrior,
37486
- signatures: digestPrior.inputContentSignatures
37487
- } : undefined;
37488
- const reusablePrefix = reusePrior ? exactReusablePrefix(plan.inputs, state.acceptedInputs) : 0;
37489
- const inputContentSignatures = [
37490
- ...reusePrior ? reusePrior.signatures.slice(0, reusablePrefix) : [],
37491
- ...plan.inputs.slice(reusablePrefix).map((input) => signatureForFields(input.fields))
37492
- ];
37493
- const verifiedOwnPrefix = reusePrior && digestPrior === state.acceptedSlot && reusePrior.slot.inputIdSeq.length === reusePrior.signatures.length && reusePrior.signatures.length === reusePrior.slot.inputContentDigests.length;
37494
- let digestPrefix = 0;
37495
- if (verifiedOwnPrefix && reusePrior) {
37496
- while (digestPrefix < reusablePrefix && !isLegacyLkgDigest(reusePrior.slot.inputContentDigests[digestPrefix] ?? ""))
37497
- digestPrefix++;
37498
- }
37499
- const incremental = verifiedOwnPrefix && reusePrior ? {
37500
- reusedPrefix: digestPrefix,
37501
- digests: [
37502
- ...reusePrior.slot.inputContentDigests.slice(0, digestPrefix),
37503
- ...plan.inputs.slice(digestPrefix).map((input) => lkgContentDigestFromFields(input.fields))
37504
- ]
37505
- } : incrementalLkgContentDigests(plan.inputs.map((input, index) => ({
37506
- id: input.id,
37507
- signature: inputContentSignatures[index] ?? "",
37508
- fields: input.fields
37509
- })), reusePrior ? {
37510
- ids: reusePrior.slot.inputIdSeq.slice(0, reusablePrefix),
37511
- signatures: reusePrior.signatures.slice(0, reusablePrefix),
37512
- digests: reusePrior.slot.inputContentDigests.slice(0, reusablePrefix)
37513
- } : undefined);
37514
- reusedPrefix = incremental.reusedPrefix;
37515
- if (!args.publishServedNumbers && !state.syncCaptureRequired && prior && reusedPrefix === plan.inputs.length && prior.inputIdSeq.length === plan.inputs.length && prior.jsonPrefix === plan.jsonPrefix && JSON.stringify(prior.piOutputEntryIds) === JSON.stringify(ownership)) {
37516
- args.assertCurrentPass?.();
37517
- state.acceptedInputs = plan.inputs;
37248
+ pi.on("agent_end", (event, ctx) => {
37249
+ const key = operationKey(ctx);
37250
+ const lifecycleSeen = lifecycleCounts(key);
37251
+ lifecycleSeen.ends = Math.min(lifecycleSeen.ends + 1, lifecycleSeen.starts);
37252
+ const counts = sideSessions.get(key);
37253
+ if (counts)
37254
+ log2(`[magic-context][pi] OMP context counts session=${String(key)} side_contexts=${counts.sides} context_passes=${counts.contexts} dispatch_fence=${counts.sides ? "latched off" : "on"}`);
37255
+ const operation = operations.get(key);
37256
+ if (operation) {
37257
+ if (endBelongsToEarlierRun(operation, event) || lifecycleSeen.starts > lifecycleSeen.ends && dispatchedEndBelongsToEarlierRun(operation, event, ctx)) {
37258
+ if (operation.dispatched)
37259
+ operation.retainedAfterEnd = true;
37260
+ if (lifecycleSeen.starts <= lifecycleSeen.ends)
37261
+ operation.started = false;
37262
+ log2(`[magic-context][pi] agent_end attributed to an earlier run; keeping the latest context pass session=${String(key)} ${operation.pass?.diagnostic() ?? ""}`);
37518
37263
  return;
37519
37264
  }
37520
- const slot = {
37521
- jsonPrefix: plan.jsonPrefix,
37522
- piOutputEntryIds: ownership,
37523
- inputIdSeq,
37524
- inputContentDigests: incremental.digests,
37525
- inputContentSignatures,
37526
- lastInputMessageId: plan.inputs.at(-1)?.id ?? "",
37527
- modelKey: plan.modelKey,
37528
- providerKey: plan.providerKey,
37529
- capturedAt: plan.capturedAt,
37530
- captureSequence: plan.captureSequence
37531
- };
37532
- args.assertCurrentPass?.();
37533
- if (args.publishServedNumbers) {
37534
- if (lkgSlotRejection(plan.sessionId, slot))
37535
- throw new PiLkgUncapturable("lkg_slot_rejected", "LKG slot rejected the Pi snapshot");
37536
- if (!saveLkgSlotToDb(db, plan.sessionId, slot, args.publishServedNumbers, {
37537
- rebuild: args.releaseRecovery,
37538
- retainDirtyRevision: !args.releaseRecovery ? state.recoveryPrefix?.fenceRevision : undefined
37539
- })) {
37540
- throw new Error("Pi LKG/served publication failed");
37541
- }
37542
- args.assertCurrentPass?.();
37265
+ operation.ended = true;
37266
+ operation.ready = false;
37267
+ releasePiOperationPayloads(operation);
37268
+ }
37269
+ });
37270
+ pi.on("before_provider_request", (event, ctx) => {
37271
+ const cancelledOperation = operations.get(operationKey(ctx));
37272
+ const signal = ctx.signal;
37273
+ if (signal ? signal.aborted : cancelledOperation?.pass?.isCancelled()) {
37274
+ cancelledOperation?.pass?.cancel();
37275
+ if (cancelledOperation)
37276
+ cancelledOperation.ready = false;
37277
+ return;
37278
+ }
37279
+ if (options.compactionOff?.(ctx))
37280
+ return;
37281
+ const payload = event.payload;
37282
+ const key = operationKey(ctx);
37283
+ if (sideSessions.get(key)?.sides)
37284
+ return;
37285
+ const nonces = unsendableNonces.get(key);
37286
+ if (nonces?.size) {
37287
+ const serializedPayload = JSON.stringify(payload);
37288
+ if (serializedPayload && [...nonces].some((nonce) => serializedPayload.includes(nonce))) {
37289
+ const budget = new PiContextBudget;
37290
+ budget.stage = "provider dispatch";
37291
+ refuse(ctx, budget, "superseded context fallback is not a managed request", () => true);
37292
+ return;
37543
37293
  }
37544
- if (!captureSlot(plan.sessionId, slot)) {
37545
- throw new Error("LKG slot rejected the Pi snapshot");
37294
+ }
37295
+ if (isOmpAdvisorRequest(payload))
37296
+ return;
37297
+ let operation = operations.get(key);
37298
+ if (!operation) {
37299
+ operation = newOperation();
37300
+ operations.set(key, operation);
37301
+ }
37302
+ const active = operation;
37303
+ const isCurrent = () => operations.get(key) === active;
37304
+ const capture = isOmpUnmanagedCaptureRequest(payload);
37305
+ const budget = (capture ? undefined : active.pass) ?? new PiContextBudget;
37306
+ if (active.refused && active.pass === budget) {
37307
+ ctx.abort();
37308
+ return;
37309
+ }
37310
+ let refusalOrigin;
37311
+ try {
37312
+ if (capture)
37313
+ throw new Error("no context pass ran for OMP auto-learn capture; raw conversation refused");
37314
+ if (!active.pass)
37315
+ throw new Error("no context pass ran for this operation");
37316
+ if (!budget.completed && (budget.abandoned || budget.elapsed() >= PI_CONTEXT_BUDGET.outcomeMs))
37317
+ throw new PiContextDeadlineError(budget.diagnostic());
37318
+ if (active.ended) {
37319
+ refusalOrigin = "agent_end";
37320
+ throw new Error("the run that owned the latest context pass has ended; no managed result for this request");
37546
37321
  }
37547
- state.acceptedInputs = plan.inputs;
37548
- state.acceptedSlot = slot;
37549
- if (state.recoveryPrefix) {
37550
- state.recoveryPrefix = args.releaseRecovery ? undefined : {
37551
- ...state.recoveryPrefix,
37552
- inputs: plan.inputs,
37553
- json: plan.jsonPrefix,
37554
- outputEntryIds: ownership ?? state.recoveryPrefix.outputEntryIds
37555
- };
37322
+ if (!budget.completed || !active.ready || active.refused || budget.abandoned)
37323
+ throw new Error("context pass ran without a managed context receipt");
37324
+ if (!piHasManagedSignature(payload, active.managedSignature))
37325
+ throw new Error("provider request does not match this pass's managed dispatch (missing current receipt signature)");
37326
+ const fields = piConversationFields(payload);
37327
+ if (!fields)
37328
+ throw new Error("provider conversation identity could not be established");
37329
+ const body = payload;
37330
+ const skipReason = active.preparedStructure ? active.preparedStructure.structuralSkipReason ?? (active.preparedStructure.api && body?.messages === undefined && Array.isArray(body?.input) && !active.preparedStructure.responsesMessages ? "provider selected Responses items without a matching history plan" : undefined) : "prepared history representation is not modeled";
37331
+ if (skipReason && !structureWarnings.has(key)) {
37332
+ structureWarnings.set(key, true);
37333
+ log2(`[magic-context][pi] OMP structural comparison skipped: ${skipReason}; stale prepared requests are not detected on this pass; session=${String(key)}`);
37556
37334
  }
37557
- const persisted = args.publishServedNumbers ? true : saveLkgSlotToDb(db, plan.sessionId, slot, undefined, {
37558
- rebuild: args.releaseRecovery,
37559
- retainDirtyRevision: state.recoveryPrefix?.fenceRevision
37335
+ const structureMismatch = skipReason ? undefined : piPreparedStructureMismatch(payload, active.preparedStructure, (counts) => {
37336
+ if (imageTrimNotices.has(key))
37337
+ return;
37338
+ imageTrimNotices.set(key, true);
37339
+ log2(`[magic-context][pi] OMP provider images trimmed: planned=${counts.planned} actual=${counts.actual} trimmed=${counts.trimmed}`);
37560
37340
  });
37561
- state.syncCaptureRequired = !persisted;
37562
- if (args.publishServedNumbers)
37563
- noteCapturedRequest();
37564
- } catch (error) {
37565
- if (args.publishServedNumbers) {
37566
- state.syncCaptureRequired = true;
37567
- state.acceptedInputs = null;
37568
- throw error;
37341
+ if (structureMismatch)
37342
+ throw new Error(`provider request does not match this pass's managed dispatch (prepared turn structure); ${structureMismatch}`);
37343
+ if (active.dispatched) {
37344
+ if (!active.dispatchFields || exactReusablePrefix([{ id: "conversation", fields }], [{ id: "conversation", fields: active.dispatchFields }]) !== 1)
37345
+ throw new Error("provider request does not match this pass's managed dispatch");
37569
37346
  }
37570
- try {
37571
- args.assertCurrentPass?.();
37572
- } catch (discarded) {
37573
- sessionLog2(plan.sessionId, "DISCARDED CONTEXT RESULT: LKG publication", discarded);
37574
- return;
37347
+ if (!active.dispatched && active.inputMessages && (!active.inputBranch || active.inputBranch.anchorIndex < active.inputMessages.length - 1)) {
37348
+ const previous = active.inputBranch;
37349
+ const persisted = inputBranch(ctx, active.inputMessages);
37350
+ if (!previous || persisted && persisted.anchorIndex > previous.anchorIndex && persisted.entryIds[previous.anchorIndex] === previous.anchorId)
37351
+ active.inputBranch = persisted;
37575
37352
  }
37576
- if (plan.captureSequence !== state.captureSequence)
37353
+ if (!active.dispatched)
37354
+ active.dispatchFields = fields;
37355
+ active.dispatched = true;
37356
+ } catch (error) {
37357
+ if (signal ? signal.aborted : budget.isCancelled(error)) {
37358
+ budget.cancel();
37359
+ active.ready = false;
37577
37360
  return;
37578
- dropSlot(plan.sessionId, "lkg_async_capture_failed");
37579
- state.syncCaptureRequired = true;
37580
- state.acceptedInputs = null;
37581
- sessionLog2(plan.sessionId, "LKG ASYNC CAPTURE FAILED; forcing synchronous capture on the next applied pass:", error);
37582
- } finally {
37583
- try {
37584
- onCaptureTiming?.({
37585
- sessionId: plan.sessionId,
37586
- elapsedMs: performance.now() - startedAt,
37587
- reusedPrefix
37361
+ }
37362
+ active.refused = true;
37363
+ budget.stage = refusalOrigin ? `provider dispatch (origin=${refusalOrigin}; context stage=${budget.stage})` : "provider dispatch";
37364
+ refuse(ctx, budget, error, isCurrent);
37365
+ }
37366
+ });
37367
+ }
37368
+ pi.registerEntryRenderer?.(ENTRY_TYPE, (entry) => new Text2(entry.data?.message ?? RETRY_MESSAGE, 0, 0));
37369
+ pi.on("context", async (event, ctx) => {
37370
+ if (fenceEnabled && isOmpDumpContext())
37371
+ return { messages: event.messages };
37372
+ const budget = new PiContextBudget;
37373
+ budget.signal = event.signal ?? ctx.signal;
37374
+ budget.sideTurn = fenceEnabled && isOmpSideContext(event);
37375
+ const key = operationKey(ctx);
37376
+ if (fenceEnabled) {
37377
+ let counts = sideSessions.get(key);
37378
+ if (!counts) {
37379
+ counts = { contexts: 0, sides: 0 };
37380
+ sideSessions.set(key, counts);
37381
+ }
37382
+ counts.contexts++;
37383
+ if (budget.sideTurn && ++counts.sides === 1)
37384
+ log2(`[magic-context][pi] OMP dispatch fence disabled for session ${String(key)}: verified ephemeral context; session-wide abort cannot distinguish provider operations (side_contexts=${counts.sides} context_passes=${counts.contexts})`);
37385
+ }
37386
+ const owner = budget.sideTurn ? operations.get(key) : undefined;
37387
+ let operation = operations.get(key);
37388
+ if (budget.sideTurn || !operation || operation.ended || !fenceEnabled) {
37389
+ operation = newOperation();
37390
+ if (!budget.sideTurn)
37391
+ operations.set(key, operation);
37392
+ }
37393
+ const active = operation;
37394
+ active.pass = budget;
37395
+ releasePiOperationPayloads(active);
37396
+ active.retainedAfterEnd = false;
37397
+ active.dispatched = false;
37398
+ active.ready = false;
37399
+ active.refused = false;
37400
+ const { isOwned, isCurrent, assertOwner } = createPiContextOwnershipGuards(operations, key, active, budget, owner);
37401
+ budget.assertOwner = assertOwner;
37402
+ try {
37403
+ budget.assertOutcome();
37404
+ let inputs;
37405
+ if (fenceEnabled && !budget.sideTurn) {
37406
+ active.inputKeys = inputMessageKeys(event);
37407
+ inputs = messageSnapshots(event.messages);
37408
+ active.inputMessages = inputs;
37409
+ }
37410
+ const result = await budget.waitMandatory(Promise.resolve(withSqliteTransformPass2(() => handler(event, ctx, budget))));
37411
+ if (result?.messages) {
37412
+ const prepared = preparePiDispatch(result.messages, fenceEnabled ? ctx.model : undefined, fenceEnabled);
37413
+ if (prepared.messages !== result.messages)
37414
+ result.messages = prepared.messages;
37415
+ active.managedSignature = piManagedSignature(result.messages);
37416
+ active.preparedStructure = prepared.structure;
37417
+ budget.completed = true;
37418
+ active.ready = true;
37419
+ if (inputs)
37420
+ active.inputBranch = inputBranch(ctx, inputs);
37421
+ }
37422
+ return result;
37423
+ } catch (error) {
37424
+ if (budget.isCancelled(error)) {
37425
+ budget.cancel();
37426
+ if (isOwned())
37427
+ active.ready = false;
37428
+ return;
37429
+ }
37430
+ if (error instanceof PiContextSupersededError || !isCurrent()) {
37431
+ if (fenceEnabled && !budget.sideTurn && !sideSessions.get(key)?.sides && !options.compactionOff?.(ctx)) {
37432
+ const nonce = crypto.randomUUID();
37433
+ let nonces = unsendableNonces.get(key);
37434
+ if (!nonces) {
37435
+ nonces = new Set;
37436
+ unsendableNonces.set(key, nonces);
37437
+ }
37438
+ nonces.add(nonce);
37439
+ if (nonces.size > MAX_UNSENDABLE_NONCES_PER_SESSION) {
37440
+ const oldest = nonces.values().next().value;
37441
+ if (oldest !== undefined)
37442
+ nonces.delete(oldest);
37443
+ }
37444
+ event.messages.push({
37445
+ role: "user",
37446
+ content: [
37447
+ {
37448
+ type: "text",
37449
+ text: `Magic Context did not prepare this request for the current turn. ${nonce}`
37450
+ }
37451
+ ],
37452
+ timestamp: Date.now()
37588
37453
  });
37589
- } catch {}
37454
+ }
37455
+ log2(`[magic-context][pi] DISCARDED CONTEXT RESULT ${budget.diagnostic()}`, error);
37456
+ throw error;
37590
37457
  }
37591
- };
37592
- if (args.publishServedNumbers || state.syncCaptureRequired) {
37593
- commit();
37594
- } else {
37595
- try {
37596
- scheduleCapture(commit);
37597
- } catch (error) {
37598
- dropSlot(plan.sessionId, "lkg_capture_schedule_failed");
37599
- state.syncCaptureRequired = true;
37600
- state.acceptedInputs = null;
37601
- sessionLog2(plan.sessionId, "LKG CAPTURE SCHEDULE FAILED; forcing synchronous capture on the next applied pass:", error);
37458
+ log2("[magic-context][pi] turn refused", error);
37459
+ if (typeof ctx.abort !== "function") {
37460
+ budget.abandoned = true;
37461
+ options.onRefusal?.(ctx, budget, error instanceof Error ? error.message : String(error), isOwned);
37462
+ throw error;
37602
37463
  }
37464
+ active.refused = true;
37465
+ refuse(ctx, budget, error, isOwned);
37466
+ return { messages: event.messages };
37467
+ } finally {
37468
+ if (!fenceEnabled || budget.sideTurn)
37469
+ releasePiOperationPayloads(active);
37603
37470
  }
37604
- return state.outputSnapshot ?? undefined;
37605
- };
37471
+ });
37472
+ }
37473
+
37474
+ // src/pi-fallback-index.ts
37475
+ function createPiFallbackIndex() {
37476
+ const prior = [];
37477
+ let priorHadDuplicateIds = false;
37478
+ const byHeader = new Map;
37479
+ const positions = new Map;
37480
+ const owners = new Map;
37481
+ let rebuilt = 0;
37606
37482
  return {
37607
- noteRecovery,
37608
- recoveryEntryIds,
37609
- recoveryFor,
37610
- beginPass(args) {
37611
- return {
37612
- ...beginPass(args),
37613
- systemEntries: args.messages.filter(isPiSystemEntry).map((message) => JSON.stringify(message))
37614
- };
37483
+ get rebuiltMessages() {
37484
+ return rebuilt;
37615
37485
  },
37616
- replay(snapshot, parentOf) {
37617
- const result = replay(snapshot, parentOf);
37618
- if (!result.ok)
37619
- return result;
37620
- const systems = result.messages.filter(isPiSystemEntry).map((message) => JSON.stringify(message));
37621
- let cursor = 0;
37622
- for (const expected of snapshot.systemEntries ?? []) {
37623
- const index = systems.indexOf(expected, cursor);
37624
- if (index < 0)
37625
- return { ok: false, reason: "lkg_system_state_mismatch" };
37626
- cursor = index + 1;
37486
+ update(args) {
37487
+ const selected = new Map;
37488
+ const guardIndexes = [];
37489
+ const inputs = [];
37490
+ const seen = new Set;
37491
+ let duplicate = false;
37492
+ for (let index = 0;index < args.messages.length; index++) {
37493
+ const message = args.messages[index];
37494
+ const id = args.resolveId(message, index) ?? `unmapped:${index}`;
37495
+ if (seen.has(id))
37496
+ duplicate = true;
37497
+ seen.add(id);
37498
+ const fields = (message && typeof message === "object" ? args.fields?.get(message) : undefined) ?? lkgContentFields(message && typeof message === "object" ? {
37499
+ role: message.role,
37500
+ timestamp: message.timestamp,
37501
+ responseId: message.responseId,
37502
+ toolCallId: message.toolCallId,
37503
+ content: message.content
37504
+ } : message);
37505
+ inputs.push({ id, fields: fields ?? [Symbol()] });
37627
37506
  }
37628
- return result;
37629
- },
37630
- captureAppliedPass,
37631
- measureOutgoingPrefix(snapshot, messages, parentOf) {
37632
- try {
37633
- const slot = getSlot(snapshot.sessionId);
37634
- const measured = measuredPrefixFor(snapshot, slot, parentOf);
37635
- if (!slot || !measured)
37636
- return;
37637
- const prefix = JSON.parse(slot.jsonPrefix);
37638
- if (!Array.isArray(prefix) || messages.length < prefix.length || JSON.stringify(messages.slice(0, prefix.length)) !== slot.jsonPrefix)
37639
- return;
37640
- const measuredRequest = stateFor(snapshot.sessionId).measuredRequest;
37641
- if (!measuredRequest)
37642
- return;
37643
- const measuredLength = JSON.parse(measuredRequest.jsonPrefix).length;
37644
- const appendedMessages = messages.slice(measuredLength);
37645
- if (completedProviderUsage(appendedMessages[0])?.signature !== measuredRequest.usage.signature)
37646
- return;
37647
- return { ...measured, appendedMessages };
37648
- } catch {
37649
- return;
37507
+ let prefix = priorHadDuplicateIds ? 0 : exactReusablePrefix(inputs, prior);
37508
+ if (duplicate) {
37509
+ prefix = 0;
37510
+ selected.clear();
37650
37511
  }
37512
+ priorHadDuplicateIds = duplicate;
37513
+ for (let index = 0;index < prefix; index++) {
37514
+ const old = prior[index];
37515
+ if (old && !args.reusable?.has(old.id)) {
37516
+ guardIndexes.push(index);
37517
+ if (old.fingerprint)
37518
+ selected.set(old.id, old.fingerprint);
37519
+ }
37520
+ }
37521
+ for (const old of prior.slice(prefix)) {
37522
+ positions.delete(old.id);
37523
+ if (old.header) {
37524
+ const bucket = byHeader.get(old.header);
37525
+ bucket?.delete(old);
37526
+ if (!bucket?.size)
37527
+ byHeader.delete(old.header);
37528
+ }
37529
+ for (const [key, ids] of old.owners) {
37530
+ const bucket = owners.get(key);
37531
+ for (const id of ids)
37532
+ bucket?.delete(id);
37533
+ if (!bucket?.size)
37534
+ owners.delete(key);
37535
+ }
37536
+ }
37537
+ prior.length = prefix;
37538
+ for (let index = prefix;index < args.messages.length; index++) {
37539
+ guardIndexes.push(index);
37540
+ const message = args.messages[index];
37541
+ const input = inputs[index];
37542
+ if (!input)
37543
+ continue;
37544
+ const record = {
37545
+ ...input,
37546
+ header: args.header(message),
37547
+ fingerprint: input.id.startsWith("unmapped:") ? null : args.fingerprint(message),
37548
+ owners: input.id.startsWith("unmapped:") ? new Map : args.toolOwners(message, input.id)
37549
+ };
37550
+ rebuilt++;
37551
+ prior.push(record);
37552
+ if (!positions.has(record.id))
37553
+ positions.set(record.id, index);
37554
+ if (record.header) {
37555
+ const bucket = byHeader.get(record.header) ?? new Set;
37556
+ bucket.add(record);
37557
+ byHeader.set(record.header, bucket);
37558
+ }
37559
+ for (const [key, ids] of record.owners) {
37560
+ const bucket = owners.get(key) ?? new Set;
37561
+ for (const id of ids)
37562
+ bucket.add(id);
37563
+ owners.set(key, bucket);
37564
+ }
37565
+ if (!args.reusable?.has(record.id) && record.fingerprint)
37566
+ selected.set(record.id, record.fingerprint);
37567
+ }
37568
+ return {
37569
+ positions,
37570
+ owners,
37571
+ changedFrom: prefix,
37572
+ guardIndexes,
37573
+ fingerprints(headers) {
37574
+ const result = new Map(selected);
37575
+ const records = headers ? [...headers].flatMap((header) => [
37576
+ ...byHeader.get(header) ?? []
37577
+ ]) : headers === null ? prior : [];
37578
+ for (const record of records)
37579
+ if (record.fingerprint)
37580
+ result.set(record.id, record.fingerprint);
37581
+ return result;
37582
+ }
37583
+ };
37651
37584
  }
37652
37585
  };
37653
37586
  }
37654
- function resolvePiLkgOutputEntryIds(messages, syntheticLeadingCount, entryId) {
37655
- const ids = messages.map((message, index) => index < syntheticLeadingCount && !isPiSystemEntry(message) ? null : isRecord(message) ? entryId(message) : undefined);
37656
- const syntheticOwners = new Map;
37657
- for (let index = 0;index < messages.length; index++) {
37658
- const message = messages[index];
37659
- const owner = ids[index];
37660
- if (typeof owner !== "string" || !isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content))
37661
- continue;
37662
- for (const part of message.content) {
37663
- if (isRecord(part) && part.type === "toolCall" && part.syntheticTodoMarker === true && typeof part.id === "string")
37664
- syntheticOwners.set(part.id, syntheticOwners.has(part.id) ? undefined : owner);
37587
+
37588
+ // src/pi-fit-reclaim.ts
37589
+ import { createHash as createHash12, randomUUID as randomUUID4 } from "node:crypto";
37590
+ var pendingInput = new Set;
37591
+ var KEY2 = "piUpperEstimateReclaim";
37592
+ function piFitOverflowPossible(args) {
37593
+ if (!args.hasUI && args.inputTokens <= 0 || args.recoveryArmed || args.forceMaterialization || args.imported || args.coldInjection || args.cacheBusting || pendingInput.has(args.sessionId))
37594
+ return true;
37595
+ if (!args.contextLimit || !Number.isFinite(args.contextLimit))
37596
+ return false;
37597
+ const headroom = args.contextLimit * ((args.forcePercentage ?? 85) / 100) - args.inputTokens;
37598
+ let appendedBytes = 0;
37599
+ try {
37600
+ for (const message of args.appendedMessages ?? []) {
37601
+ appendedBytes += piFitMessageBytes(message);
37602
+ if (2 * appendedBytes >= headroom)
37603
+ return true;
37665
37604
  }
37605
+ } catch {
37606
+ return true;
37666
37607
  }
37667
- return ids.map((id, index) => {
37668
- const message = messages[index];
37669
- return id === undefined && isRecord(message) && message.role === "toolResult" && message.syntheticTodoMarker === true && typeof message.toolCallId === "string" ? syntheticOwners.get(message.toolCallId) : id;
37670
- });
37608
+ return false;
37609
+ }
37610
+ function clearPiUpperReclaimSession(sessionId) {
37611
+ pendingInput.delete(sessionId);
37612
+ }
37613
+ function requestPiFitReclaimInput(sessionId) {
37614
+ pendingInput.add(sessionId);
37615
+ }
37616
+ function clearPiFitReclaimInput(sessionId) {
37617
+ pendingInput.delete(sessionId);
37618
+ }
37619
+ function piUpperReclaimCursor(args) {
37620
+ const head = args.head;
37621
+ return {
37622
+ sessionId: args.sessionId,
37623
+ modelKey: args.modelKey,
37624
+ contextWindow: args.contextWindow ?? null,
37625
+ head: head ? createHash12("sha256").update(JSON.stringify([
37626
+ Buffer.from(head.cachedM0Bytes ?? []).toString("base64"),
37627
+ Buffer.from(head.cachedM1Bytes ?? []).toString("base64"),
37628
+ head.cachedM0MuralDataUrl ?? null
37629
+ ])).digest("hex") : null,
37630
+ recoveryArmedAt: getEmergencyRecoveryArmedAt(args.sessionId)
37631
+ };
37632
+ }
37633
+ function claimFrom(value, sessionId) {
37634
+ const claim = value;
37635
+ return claim?.sessionId === sessionId && (typeof claim.modelKey === "string" || claim.modelKey === null) ? claim : null;
37636
+ }
37637
+ function canReclaim(previous, cursor) {
37638
+ return !previous || previous.modelKey !== cursor.modelKey || (previous.contextWindow ?? null) !== cursor.contextWindow || (previous.head ?? null) !== cursor.head || cursor.recoveryArmedAt !== null && cursor.recoveryArmedAt !== previous.recoveryArmedAt;
37639
+ }
37640
+ function shouldReclaimPiUpperEstimate(db, cursor) {
37641
+ if (!cursor)
37642
+ return false;
37643
+ return canReclaim(claimFrom(readReplayEnvelope(db, cursor.sessionId)[KEY2], cursor.sessionId), cursor);
37644
+ }
37645
+ function reservePiUpperEstimateReclaim(db, cursor) {
37646
+ const reservation = randomUUID4();
37647
+ return db.transaction(() => {
37648
+ let claimed = false;
37649
+ if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
37650
+ claimed = canReclaim(claimFrom(doc[KEY2], cursor.sessionId), cursor);
37651
+ if (!claimed)
37652
+ return false;
37653
+ doc.version = 2;
37654
+ doc[KEY2] = { ...cursor, reservation };
37655
+ return true;
37656
+ }))
37657
+ throw new Error("Could not reserve upper-estimate reclaim");
37658
+ return claimed ? reservation : undefined;
37659
+ }).immediate();
37660
+ }
37661
+ function recordPiUpperEstimateReclaim(db, cursor, reservation) {
37662
+ const owner = reservation ?? reservePiUpperEstimateReclaim(db, cursor);
37663
+ if (!owner)
37664
+ return;
37665
+ if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
37666
+ if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== owner)
37667
+ return false;
37668
+ doc.version = 2;
37669
+ doc[KEY2] = cursor;
37670
+ return true;
37671
+ }))
37672
+ throw new Error("Could not persist upper-estimate reclaim cursor");
37673
+ pendingInput.delete(cursor.sessionId);
37674
+ }
37675
+ function releasePiUpperEstimateReclaim(db, cursor, reservation) {
37676
+ if (!updateReplayDocument(db, cursor.sessionId, (doc) => {
37677
+ if (claimFrom(doc[KEY2], cursor.sessionId)?.reservation !== reservation)
37678
+ return false;
37679
+ delete doc[KEY2];
37680
+ return true;
37681
+ }))
37682
+ throw new Error("Could not release upper-estimate reclaim");
37683
+ }
37684
+ function resetPiUpperEstimateReclaim(db, sessionId) {
37685
+ if (!claimFrom(readReplayEnvelope(db, sessionId)[KEY2], sessionId))
37686
+ return;
37687
+ if (!updateReplayDocument(db, sessionId, (doc) => {
37688
+ delete doc[KEY2];
37689
+ return true;
37690
+ }))
37691
+ throw new Error("Could not clear upper-estimate reclaim cursor");
37671
37692
  }
37672
37693
 
37673
37694
  // src/pi-lkg-fit-envelope.ts
@@ -37749,7 +37770,15 @@ import { createHash as createHash15 } from "node:crypto";
37749
37770
 
37750
37771
  // src/served-array-ledger.ts
37751
37772
  import { createHash as createHash14 } from "node:crypto";
37752
- import { readFileSync as readFileSync11, statSync as statSync6 } from "node:fs";
37773
+ import {
37774
+ closeSync as closeSync2,
37775
+ fstatSync,
37776
+ ftruncateSync,
37777
+ openSync as openSync2,
37778
+ readFileSync as readFileSync11,
37779
+ readSync as readSync2,
37780
+ statSync as statSync6
37781
+ } from "node:fs";
37753
37782
  import * as path12 from "node:path";
37754
37783
 
37755
37784
  // src/pi-served-identities.ts
@@ -37780,9 +37809,12 @@ function parseNumbers(value) {
37780
37809
  }
37781
37810
  return value;
37782
37811
  }
37783
- function readPiCommittedServedNumbers(db, sessionId) {
37812
+ function readPiCommittedServedNumbers(db, sessionId, requireEnvelope = false) {
37784
37813
  try {
37785
- return parseNumbers(readReplayEnvelope(db, sessionId).piServedTagNumbers);
37814
+ const numbers = readReplayEnvelope(db, sessionId).piServedTagNumbers;
37815
+ if (requireEnvelope && numbers === undefined)
37816
+ throw new PiServedEnvelopeUnavailable("No committed Pi served-number envelope for torn legacy tail");
37817
+ return parseNumbers(numbers);
37786
37818
  } catch (error) {
37787
37819
  if (error instanceof ReplayDocumentError) {
37788
37820
  throw new PiServedEnvelopeUnavailable("Pi served-number envelope is unreadable", { cause: error });
@@ -37815,6 +37847,102 @@ var LEDGER_DIRECTORY = "pi-served-array-digests";
37815
37847
  var BODY_DIRECTORY = "pi-served-array-bodies";
37816
37848
  var IDENTITY_DIRECTORY = "pi-served-tag-numbers";
37817
37849
  var FLUSH_DELAY_MS = 25;
37850
+ var zeroTailWarnings = new BoundedSessionMap(1000);
37851
+ function noteZeroTail(sessionId, filePath, total, zeros) {
37852
+ const warnings = zeroTailWarnings.get(sessionId) ?? new Set;
37853
+ const key = `${filePath}:${total}:${zeros}`;
37854
+ if (warnings.has(key))
37855
+ return;
37856
+ warnings.add(key);
37857
+ zeroTailWarnings.set(sessionId, warnings);
37858
+ log2(`[magic-context][pi] WARNING unwritten served-ledger tail session=${sessionId} file=${filePath} ignored_zero_bytes=${zeros} retained_bytes=${total - zeros} total_bytes=${total}`);
37859
+ }
37860
+
37861
+ class PiServedTailUnavailable extends PiServedEnvelopeUnavailable {
37862
+ completeNumbers;
37863
+ constructor(completeNumbers) {
37864
+ super("Torn Pi served-number tail; committed served-number envelope required to prevent identity reuse");
37865
+ this.completeNumbers = completeNumbers;
37866
+ }
37867
+ }
37868
+ function readLedgerLines(filePath, sessionId) {
37869
+ let bytes;
37870
+ try {
37871
+ bytes = readFileSync11(filePath);
37872
+ } catch (error) {
37873
+ if (error.code === "ENOENT")
37874
+ return { lines: [], torn: false };
37875
+ throw new PiServedEnvelopeUnavailable(`Pi served ledger unreadable: ${filePath}`, { cause: error });
37876
+ }
37877
+ let end = bytes.length;
37878
+ while (end > 0 && bytes[end - 1] === 0)
37879
+ end--;
37880
+ if (end < bytes.length && (end === 0 || bytes[end - 1] === 10)) {
37881
+ noteZeroTail(sessionId, filePath, bytes.length, bytes.length - end);
37882
+ bytes = bytes.subarray(0, end);
37883
+ }
37884
+ const boundary = bytes.lastIndexOf(10) + 1;
37885
+ return {
37886
+ lines: bytes.subarray(0, boundary).toString("utf8").split(`
37887
+ `),
37888
+ torn: boundary !== bytes.length
37889
+ };
37890
+ }
37891
+ function parseLedgerRow(line, filePath) {
37892
+ try {
37893
+ const row = JSON.parse(line);
37894
+ if (row && typeof row === "object" && !Array.isArray(row))
37895
+ return row;
37896
+ } catch (error) {
37897
+ throw new PiServedEnvelopeUnavailable(`Corrupt Pi served ledger JSON record: ${filePath}`, { cause: error });
37898
+ }
37899
+ throw new PiServedEnvelopeUnavailable(`Invalid Pi served ledger record: ${filePath}`);
37900
+ }
37901
+ function repairZeroTail(filePath, allowTornIdentity = false) {
37902
+ let fd;
37903
+ try {
37904
+ fd = openSync2(filePath, "r+");
37905
+ } catch (error) {
37906
+ if (error.code === "ENOENT")
37907
+ return;
37908
+ throw error;
37909
+ }
37910
+ try {
37911
+ const size = fstatSync(fd).size;
37912
+ let end = size;
37913
+ const chunk = Buffer.alloc(Math.min(size, 4096));
37914
+ while (end > 0) {
37915
+ const start = Math.max(0, end - chunk.length);
37916
+ const count = readSync2(fd, chunk, 0, end - start, start);
37917
+ if (count !== end - start)
37918
+ throw new Error("Pi served ledger changed during tail repair");
37919
+ let index = count - 1;
37920
+ while (index >= 0 && chunk[index] === 0)
37921
+ index--;
37922
+ if (index >= 0) {
37923
+ end = start + index + 1;
37924
+ if (chunk[index] === 10)
37925
+ break;
37926
+ if (!allowTornIdentity)
37927
+ throw new PiServedEnvelopeUnavailable(`Torn Pi served ledger tail; refusing append: ${filePath}`);
37928
+ const newline = chunk.lastIndexOf(10, index);
37929
+ if (newline >= 0) {
37930
+ end = start + newline + 1;
37931
+ break;
37932
+ }
37933
+ end = start;
37934
+ } else
37935
+ end = start;
37936
+ }
37937
+ if (end !== size) {
37938
+ if (fstatSync(fd).size !== size)
37939
+ throw new Error("Pi served ledger changed during tail repair");
37940
+ ftruncateSync(fd, end);
37941
+ }
37942
+ } finally {
37943
+ closeSync2(fd);
37944
+ }
37945
+ }
37818
37946
 
37819
37947
  class PiServedIdentityError extends Error {
37820
37948
  constructor(cause) {
@@ -37851,7 +37979,17 @@ function clearPiServedArraySession(sessionId) {
37851
37979
  }
37852
37980
  function getPiServedTagNumbers(sessionId, storageDir = getMagicContextStorageDir(), db) {
37853
37981
  if (db) {
37854
- const legacy = getPiServedTagNumbers(sessionId, storageDir);
37982
+ let legacy;
37983
+ try {
37984
+ legacy = getPiServedTagNumbers(sessionId, storageDir);
37985
+ } catch (error) {
37986
+ if (!(error instanceof PiServedTailUnavailable))
37987
+ throw error;
37988
+ return new Set([
37989
+ ...error.completeNumbers,
37990
+ ...readPiCommittedServedNumbers(db, sessionId, true)
37991
+ ]);
37992
+ }
37855
37993
  try {
37856
37994
  return new Set([
37857
37995
  ...legacy,
@@ -37868,26 +38006,20 @@ function getPiServedTagNumbers(sessionId, storageDir = getMagicContextStorageDir
37868
38006
  const cached = servedTagNumbersBySession.get(sessionId);
37869
38007
  if (cached?.path === filePath && sameIdentityFile(cached.stamp, stamp))
37870
38008
  return cached.numbers;
37871
- let text;
37872
- try {
37873
- text = readFileSync11(filePath, "utf8");
37874
- } catch (error) {
37875
- if (error.code !== "ENOENT")
37876
- throw error;
37877
- text = "";
37878
- }
38009
+ const { lines, torn } = readLedgerLines(filePath, sessionId);
37879
38010
  const numbers = new Set;
37880
- for (const line of text.split(`
37881
- `)) {
38011
+ for (const line of lines) {
37882
38012
  if (!line.trim())
37883
38013
  continue;
37884
- const row = JSON.parse(line);
38014
+ const row = parseLedgerRow(line, filePath);
37885
38015
  if (row.version !== 1 || row.session_id !== sessionId || !Array.isArray(row.tag_numbers) || !row.tag_numbers.every((number) => Number.isSafeInteger(number) && number > 0)) {
37886
- throw new Error("Invalid durable Pi served-number record; refusing identity adoption");
38016
+ throw new PiServedEnvelopeUnavailable("Invalid durable Pi served-number record; refusing identity adoption");
37887
38017
  }
37888
38018
  for (const number of row.tag_numbers)
37889
38019
  numbers.add(number);
37890
38020
  }
38021
+ if (torn)
38022
+ throw new PiServedTailUnavailable(numbers);
37891
38023
  servedTagNumbersBySession.set(sessionId, { path: filePath, numbers, stamp });
37892
38024
  return numbers;
37893
38025
  }
@@ -37895,13 +38027,20 @@ function getPiLastServedDigest(sessionId, storageDir = getMagicContextStorageDir
37895
38027
  const previous = previousBySession.get(sessionId);
37896
38028
  if (previous)
37897
38029
  return previous.digest;
37898
- try {
37899
- const lines = readFileSync11(getPiServedArrayLedgerPath(sessionId, storageDir), "utf8").trim().split(`
37900
- `);
37901
- const last = JSON.parse(lines.at(-1) ?? "");
37902
- if (last.version === 1 && last.session_id === sessionId && /^[a-f0-9]{64}$/.test(last.sha256))
37903
- return last.sha256;
37904
- } catch {}
38030
+ const filePath = getPiServedArrayLedgerPath(sessionId, storageDir);
38031
+ const { lines, torn } = readLedgerLines(filePath, sessionId);
38032
+ let digest;
38033
+ for (const line of lines) {
38034
+ if (!line.trim())
38035
+ continue;
38036
+ const last = parseLedgerRow(line, filePath);
38037
+ if (last.version !== 1 || last.session_id !== sessionId || typeof last.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(last.sha256) || !Number.isSafeInteger(last.sequence) || Number(last.sequence) < 1 || !Number.isSafeInteger(last.message_count) || Number(last.message_count) < 0)
38038
+ throw new PiServedEnvelopeUnavailable(`Invalid Pi served digest record: ${filePath}`);
38039
+ digest = last.sha256;
38040
+ }
38041
+ if (torn)
38042
+ throw new PiServedEnvelopeUnavailable("Torn Pi served digest tail; exact-byte replay evidence unavailable");
38043
+ return digest;
37905
38044
  }
37906
38045
  function getPiLastServedArray(sessionId) {
37907
38046
  const previous = previousBySession.get(sessionId);
@@ -37971,6 +38110,7 @@ function recordWriteFailure(error) {
37971
38110
  function appendPendingLines(filePath, lines) {
37972
38111
  try {
37973
38112
  ensureStorageDirectorySync(path12.dirname(filePath));
38113
+ repairZeroTail(filePath);
37974
38114
  writeStorageFileSync(filePath, lines.join(""), {
37975
38115
  encoding: "utf8",
37976
38116
  flag: "a"
@@ -38027,7 +38167,17 @@ function capturePiServedArray(sessionId, messages, options = {}) {
38027
38167
  const digest = sha256(serializedArray);
38028
38168
  const storageDir = options.storageDir ?? getMagicContextStorageDir();
38029
38169
  identityPersistenceFailed = true;
38030
- const previousNumbers = options.recoveryWithoutTagState ? new Set : getPiServedTagNumbers(sessionId, storageDir);
38170
+ let previousNumbers = new Set;
38171
+ if (!options.recoveryWithoutTagState) {
38172
+ try {
38173
+ previousNumbers = getPiServedTagNumbers(sessionId, storageDir);
38174
+ } catch (error) {
38175
+ if (!(error instanceof PiServedTailUnavailable) || !options.db)
38176
+ throw error;
38177
+ readPiCommittedServedNumbers(options.db, sessionId, true);
38178
+ previousNumbers = error.completeNumbers;
38179
+ }
38180
+ }
38031
38181
  const managedNumbers = options.recoveryWithoutTagState ? [] : options.servedTagNumbers ?? [];
38032
38182
  identityPersistenceFailed = false;
38033
38183
  const served = new Set(previousNumbers);
@@ -38068,6 +38218,7 @@ function capturePiServedArray(sessionId, messages, options = {}) {
38068
38218
  identityPersistenceFailed = true;
38069
38219
  ensureStorageDirectorySync(path12.dirname(identityPath));
38070
38220
  options.assertCurrentPass?.();
38221
+ repairZeroTail(identityPath, !!options.db);
38071
38222
  writeStorageFileSync(identityPath, `${JSON.stringify({ version: 1, session_id: sessionId, tag_numbers: newNumbers })}
38072
38223
  `, {
38073
38224
  encoding: "utf8",
@@ -38572,209 +38723,6 @@ function resolvePiProvenInputFloor(args) {
38572
38723
  return observed;
38573
38724
  }
38574
38725
 
38575
- // src/pi-raw-fallback.ts
38576
- class PiStorageBusyError extends Error {
38577
- code = "PI_STORAGE_BUSY";
38578
- recoverable = true;
38579
- constructor(options) {
38580
- super("Magic Context storage is busy; send your message again", options);
38581
- this.name = "PiStorageBusyError";
38582
- }
38583
- }
38584
-
38585
- class PiDegradedPassError extends Error {
38586
- site;
38587
- code = "PI_DEGRADED_PASS";
38588
- recoverable = true;
38589
- constructor(site, options) {
38590
- super("Magic Context could not finish preparing this turn; send your message again", options);
38591
- this.site = site;
38592
- this.name = "PiDegradedPassError";
38593
- }
38594
- }
38595
- function estimatePiOutgoingInputTokens(messages, observed, measuredPrefix, contextLimit) {
38596
- if (!observed || !Number.isFinite(observed.systemTokens) || observed.systemTokens <= 0 || !Number.isFinite(observed.toolDefinitionTokens) || observed.toolDefinitionTokens < 0 || !hasTokenizerForFit())
38597
- return { tokens: 0, trusted: false };
38598
- const knownRole = (message) => {
38599
- if (!message || typeof message !== "object")
38600
- return false;
38601
- return ["user", "assistant", "toolResult", "custom"].includes(message.role ?? "");
38602
- };
38603
- const countable = (message) => {
38604
- if (!knownRole(message))
38605
- return false;
38606
- const m = message;
38607
- return typeof m.content === "string" || Array.isArray(m.content) && m.content.every((p) => p && typeof p === "object" && ["text", "thinking", "redacted_thinking", "toolCall"].includes(String(p.type)));
38608
- };
38609
- const complete = messages.every(countable);
38610
- const measuredForBounds = measuredPrefix && measuredPrefix.modelKey === observed.modelKey && observed.envelopeSignature && measuredPrefix.envelopeSignature === observed.envelopeSignature && Number.isSafeInteger(measuredPrefix.inputTokens) && measuredPrefix.inputTokens > 0 ? measuredPrefix : undefined;
38611
- if (contextLimit && Number.isFinite(contextLimit) && contextLimit > 0) {
38612
- const priced = measuredForBounds?.appendedMessages ?? messages;
38613
- const baseline = measuredForBounds?.inputTokens ?? 0;
38614
- let contentBytes = 0;
38615
- for (const rawMessage of priced.filter(knownRole)) {
38616
- const message = rawMessage;
38617
- if (typeof message.content === "string")
38618
- contentBytes += Buffer.byteLength(message.content);
38619
- else if (Array.isArray(message.content))
38620
- for (const part of message.content) {
38621
- if (!part || typeof part !== "object")
38622
- continue;
38623
- for (const value of [
38624
- part.type === "text" ? part.text : undefined,
38625
- part.type === "thinking" && !part.redacted ? part.thinking : undefined,
38626
- part.type === "toolCall" ? part.name : undefined
38627
- ])
38628
- if (typeof value === "string")
38629
- contentBytes += Buffer.byteLength(value);
38630
- if (part.type === "toolCall" && part.arguments !== undefined)
38631
- contentBytes += Buffer.byteLength(JSON.stringify(part.arguments));
38632
- }
38633
- if (baseline + Math.ceil(contentBytes / 4) > contextLimit)
38634
- break;
38635
- }
38636
- const lower = baseline + Math.ceil(contentBytes / 4);
38637
- if (lower > contextLimit)
38638
- return {
38639
- tokens: lower,
38640
- trusted: false,
38641
- refusalGrade: true,
38642
- refusalTokens: lower,
38643
- refusalBasis: "byte-bound"
38644
- };
38645
- const envelope = measuredForBounds ? baseline : providerMass({
38646
- system: observed.systemTokens,
38647
- tools: observed.toolDefinitionTokens
38648
- }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true);
38649
- const fit = observed.calibration ?? calibrationForModelKey(observed.modelKey);
38650
- const upper = envelope + Math.max(UNKNOWN_FIT_RATIO, fit.toolsRatio, fit.proseRatio) * priced.reduce((sum, message) => sum + piFitMessageBytes(message), 0);
38651
- if (upper <= contextLimit)
38652
- return {
38653
- tokens: upper,
38654
- trusted: true,
38655
- refusalGrade: false,
38656
- refusalBasis: "byte-bound"
38657
- };
38658
- }
38659
- if (!complete) {
38660
- return {
38661
- tokens: providerMass({
38662
- system: observed.systemTokens,
38663
- tools: observed.toolDefinitionTokens + messages.reduce((sum, message) => sum + piFitMessageBytes(message), 0)
38664
- }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true),
38665
- trusted: true,
38666
- refusalGrade: false
38667
- };
38668
- }
38669
- const bounded = messages.some((rawMessage) => {
38670
- const content = rawMessage.content;
38671
- if (typeof content === "string")
38672
- return tokenCountUsesByteBound(content);
38673
- return content.some((part) => [
38674
- part.text,
38675
- part.redacted ? undefined : part.thinking,
38676
- part.name,
38677
- part.arguments === undefined ? undefined : JSON.stringify(part.arguments)
38678
- ].some((value) => typeof value === "string" && tokenCountUsesByteBound(value)));
38679
- });
38680
- if (bounded)
38681
- return {
38682
- tokens: providerMass({
38683
- system: observed.systemTokens,
38684
- tools: observed.toolDefinitionTokens + messages.reduce((sum, message) => sum + piFitMessageBytes(message), 0)
38685
- }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true),
38686
- trusted: true,
38687
- refusalGrade: false,
38688
- refusalBasis: "byte-bound"
38689
- };
38690
- const raw = tokenizePiMessages([...messages]);
38691
- const calibration = observed.calibration ?? calibrationForModelKey(observed.modelKey);
38692
- const refusalCalibration = calibrationForModelKey(observed.modelKey);
38693
- const tokens = providerMass({
38694
- system: observed.systemTokens,
38695
- tools: observed.toolDefinitionTokens + raw.toolCall,
38696
- prose: raw.conversation
38697
- }, calibration, true);
38698
- const refusalGrade = Number.isFinite(tokens) && messages.every((rawMessage) => {
38699
- const content = rawMessage.content;
38700
- if (typeof content === "string")
38701
- return !tokenCountUsesByteBound(content);
38702
- return content.every((part) => [
38703
- part.text,
38704
- part.redacted ? undefined : part.thinking,
38705
- part.name,
38706
- part.arguments === undefined ? undefined : JSON.stringify(part.arguments)
38707
- ].every((value) => typeof value !== "string" || !tokenCountUsesByteBound(value)));
38708
- }) && hasMeasuredDecisionCalibration(refusalCalibration) && observed.toolDefinitionsMeasured === true && Number.isFinite(observed.refusalToolDefinitionTokens) && observed.refusalToolDefinitionTokens >= 0;
38709
- const measured = measuredPrefix && measuredPrefix.modelKey === observed.modelKey && observed.envelopeSignature && measuredPrefix.envelopeSignature === observed.envelopeSignature && Number.isSafeInteger(measuredPrefix.inputTokens) && measuredPrefix.inputTokens > 0 ? measuredPrefix : undefined;
38710
- const tail = measured ? tokenizePiMessages([...measured.appendedMessages]) : raw;
38711
- const refusalTokens = refusalGrade ? (measured?.inputTokens ?? 0) + providerMass({
38712
- system: measured ? 0 : observed.systemTokens,
38713
- tools: (measured ? 0 : observed.refusalToolDefinitionTokens) + tail.toolCall,
38714
- prose: tail.conversation
38715
- }, refusalCalibration) : undefined;
38716
- return {
38717
- tokens,
38718
- trusted: Number.isFinite(tokens),
38719
- refusalGrade,
38720
- refusalTokens,
38721
- refusalBasis: refusalGrade ? measured ? "provider-prefix" : "calibrated" : undefined
38722
- };
38723
- }
38724
- function assertPiRawFallbackFits(messages, contextLimit, log, cause, observed, measuredPrefix) {
38725
- if (!contextLimit || !Number.isFinite(contextLimit) || !observed || !Number.isFinite(observed.systemTokens) || observed.systemTokens <= 0 || !Number.isFinite(observed.toolDefinitionTokens) || observed.toolDefinitionTokens < 0 || observed.envelopeBytes !== undefined && (!Number.isFinite(observed.envelopeBytes) || observed.envelopeBytes < 0)) {
38726
- log("raw_fallback_refused completeness=partial");
38727
- throw new PiStorageBusyError({ cause });
38728
- }
38729
- const measured = measuredPrefix && measuredPrefix.modelKey === observed.modelKey && observed.envelopeSignature && measuredPrefix.envelopeSignature === observed.envelopeSignature && Number.isSafeInteger(measuredPrefix.inputTokens) && measuredPrefix.inputTokens > 0 ? measuredPrefix : undefined;
38730
- const baseInputTokens = measured?.inputTokens ?? 0;
38731
- const pricedMessages = measured?.appendedMessages ?? messages;
38732
- log(`lkg_fit_basis=${measured ? "provider_input" : "host_metadata"} measured_input=${baseInputTokens}`);
38733
- let bytes = measured ? 1 : 1 + (observed.envelopeBytes ?? 20 + 4 * (observed.systemTokens + observed.toolDefinitionTokens));
38734
- let serializationFailed = false;
38735
- try {
38736
- for (const message of pricedMessages) {
38737
- bytes += piFitMessageBytes(message) + 1;
38738
- if (baseInputTokens + Math.ceil(bytes / 4) > contextLimit)
38739
- break;
38740
- }
38741
- } catch {
38742
- serializationFailed = true;
38743
- }
38744
- const proxyTokens = baseInputTokens + Math.ceil(bytes / 4);
38745
- if (serializationFailed || proxyTokens > contextLimit) {
38746
- log(`raw_fallback_over_context_limit proxy_bytes=${bytes} proxy_tokens=${proxyTokens} limit=${contextLimit} early_abort=true serialization_failed=${serializationFailed}`);
38747
- throw new PiStorageBusyError({ cause });
38748
- }
38749
- const complete = pricedMessages.every((message) => {
38750
- if (!message || typeof message !== "object")
38751
- return false;
38752
- const m = message;
38753
- return ["user", "assistant", "toolResult", "custom"].includes(m.role ?? "") && (typeof m.content === "string" || Array.isArray(m.content) && m.content.every((p) => p !== null && typeof p === "object" && ["text", "thinking", "redacted_thinking", "toolCall"].includes(String(p.type))));
38754
- });
38755
- const raw = tokenizePiMessages([...pricedMessages]);
38756
- if (!hasTokenizerForFit()) {
38757
- log("raw_fallback_refused completeness=partial tokenizer=unavailable");
38758
- throw new PiStorageBusyError({ cause });
38759
- }
38760
- const tokens = baseInputTokens + providerMass({
38761
- system: measured ? 0 : observed.systemTokens,
38762
- tools: (measured ? 0 : observed.toolDefinitionTokens) + raw.toolCall,
38763
- prose: raw.conversation
38764
- }, observed.calibration ?? calibrationForModelKey(observed.modelKey), true);
38765
- if (!complete || !Number.isFinite(tokens) || tokens <= 0 || tokens > contextLimit || serializationFailed || proxyTokens > contextLimit) {
38766
- log(`raw_fallback_over_context_limit proxy_bytes=${bytes} proxy_tokens=${proxyTokens} limit=${contextLimit} early_abort=true serialization_failed=${serializationFailed}`);
38767
- throw new PiStorageBusyError({ cause });
38768
- }
38769
- }
38770
- function classifyPiOutgoingFit(estimate, limit) {
38771
- if (!limit || !Number.isFinite(limit) || limit <= 0)
38772
- return "fits-or-unknown";
38773
- if (estimate.refusalGrade && typeof estimate.refusalTokens === "number" && Number.isFinite(estimate.refusalTokens) && estimate.refusalTokens > limit)
38774
- return "over-window-proof";
38775
- return estimate.trusted && Number.isFinite(estimate.tokens) && estimate.tokens > limit ? "upper-estimate-pressure" : "fits-or-unknown";
38776
- }
38777
-
38778
38726
  // src/pi-tag-identity-repair.ts
38779
38727
  var PI_TAG_IDENTITY_REPAIR_REASON = "tag_identity_repair";
38780
38728
  var unreadableLedgerLogged = new Set;
@@ -42578,6 +42526,14 @@ function registerPiContextHandler(pi, baseOptions) {
42578
42526
  const fullWireMessageCount = rawMessageCount;
42579
42527
  const options = baseOptions.resolveForProject?.(projectDirectory) ?? baseOptions;
42580
42528
  lkgOptions = options;
42529
+ const projectIdentity = resolveProjectIdentityForSession(projectDirectory, options.allowHomeProject) ?? "";
42530
+ withoutSqliteTransformPass2(() => options.maybeAutoEmbedSession?.(sessionId, projectDirectory, projectIdentity));
42531
+ if (!options.compactionOff) {
42532
+ budget.stage = "tag snapshot hydration";
42533
+ const hydration = ensurePiTagSnapshotReady(options.db, sessionId, budget);
42534
+ if (hydration)
42535
+ await guardAwait(hydration);
42536
+ }
42581
42537
  const deliveryModelKey = resolvePiContextModelKey(ctx);
42582
42538
  if (!budget.sideTurn && deliveryModelKey) {
42583
42539
  const parentId = ctx.sessionManager.getLeafId?.();
@@ -42586,8 +42542,6 @@ function registerPiContextHandler(pi, baseOptions) {
42586
42542
  const schedulerConfig = options.scheduler ?? DEFAULT_SCHEDULER_CONFIG;
42587
42543
  const scheduler = schedulerFor(options);
42588
42544
  lkgCompactionOff = options.compactionOff === true;
42589
- const projectIdentity = resolveProjectIdentityForSession(projectDirectory, options.allowHomeProject) ?? "";
42590
- withoutSqliteTransformPass2(() => options.maybeAutoEmbedSession?.(sessionId, projectDirectory, projectIdentity));
42591
42545
  logTransformTiming(sessionId, "findSessionId", tFindSession, `messages=${event.messages.length}`);
42592
42546
  budget.stage = "branch projection";
42593
42547
  const tEntryBranch = performance.now();
@@ -42657,6 +42611,8 @@ function registerPiContextHandler(pi, baseOptions) {
42657
42611
  jitter: true,
42658
42612
  beforeRetry: (error) => {
42659
42613
  assertCurrentPass();
42614
+ if (!lkgCompactionOff)
42615
+ assertPiTagSnapshotReady(options.db, sessionId);
42660
42616
  if (checkedReplay)
42661
42617
  return;
42662
42618
  checkedReplay = true;
@@ -43145,6 +43101,8 @@ function registerPiContextHandler(pi, baseOptions) {
43145
43101
  throw new PiLkgAdmissionReplay(replay.messages, "held tag-identity rebuild: replaying the served prefix", "held_identity_repair");
43146
43102
  }
43147
43103
  };
43104
+ if (!options.compactionOff)
43105
+ assertPiTagSnapshotReady(options.db, sessionId);
43148
43106
  let result = await budget.waitMandatory(runPipeline(pipelineArgs));
43149
43107
  observePipelineResultForTests(sessionId, result);
43150
43108
  logTransformTiming(sessionId, "runPipeline", tRunPipeline);
@@ -43200,6 +43158,7 @@ function registerPiContextHandler(pi, baseOptions) {
43200
43158
  historian,
43201
43159
  isFirstContextPassForSession,
43202
43160
  activeTags: result.activeTags,
43161
+ eligiblePendingDropTagIds: result.eligiblePendingDropTagIds,
43203
43162
  rawMessageProvider,
43204
43163
  ordinalAlignment: ordinalAlignment(),
43205
43164
  taggerFloor,
@@ -43596,6 +43555,7 @@ function registerPiContextHandler(pi, baseOptions) {
43596
43555
  startGuard: budget.assert,
43597
43556
  isFirstContextPassForSession: false,
43598
43557
  activeTags: result.activeTags,
43558
+ eligiblePendingDropTagIds: result.eligiblePendingDropTagIds,
43599
43559
  rawMessageProvider,
43600
43560
  ordinalAlignment: ordinalAlignment(),
43601
43561
  taggerFloor,
@@ -43686,7 +43646,7 @@ function registerPiContextHandler(pi, baseOptions) {
43686
43646
  readPiCommittedServedNumbers(options.db, sessionId);
43687
43647
  serializedOutput = lkgCoordinator.captureAppliedPass({
43688
43648
  publishServedNumbers: () => publishPiServedNumbers(options.db, sessionId, [
43689
- ...getPiServedTagNumbers(sessionId),
43649
+ ...getPiServedTagNumbers(sessionId, undefined, options.db),
43690
43650
  ...result.servedTagNumbers ?? []
43691
43651
  ]),
43692
43652
  assertCurrentPass: budget.assertOutcome,
@@ -43714,7 +43674,7 @@ function registerPiContextHandler(pi, baseOptions) {
43714
43674
  options.db.transaction(() => {
43715
43675
  budget.assertOutcome();
43716
43676
  publishPiServedNumbers(options.db, sessionId, [
43717
- ...getPiServedTagNumbers(sessionId),
43677
+ ...getPiServedTagNumbers(sessionId, undefined, options.db),
43718
43678
  ...result.servedTagNumbers ?? []
43719
43679
  ]);
43720
43680
  }).immediate();
@@ -43759,15 +43719,25 @@ function registerPiContextHandler(pi, baseOptions) {
43759
43719
  } catch (err) {
43760
43720
  if (budget.isCancelled(err))
43761
43721
  throw err;
43722
+ if (err instanceof PiTagHydrationError) {
43723
+ budget.failureReason = err.message;
43724
+ if (sessionIdForError) {
43725
+ try {
43726
+ prewarmPiTagSnapshot(lkgOptions.db, sessionIdForError);
43727
+ } catch {}
43728
+ }
43729
+ throw err;
43730
+ }
43762
43731
  budget.assertOutcome();
43763
43732
  budget.stage = "recovery";
43764
43733
  if (err instanceof PiServedIdentityError) {
43765
43734
  if (sessionIdForError)
43766
43735
  clearPiLkgSessionState(sessionIdForError);
43767
- throw new PiStorageBusyError({ cause: err });
43736
+ throw piFallbackRefusal("served-identity-unavailable", err);
43768
43737
  }
43769
43738
  if (err instanceof PiLkgAdmissionReplay && sessionIdForError) {
43770
43739
  budget.assertOutcome();
43740
+ assertPiTagSnapshotReady(lkgOptions.db, sessionIdForError);
43771
43741
  if (lkgPassSnapshot && err.trigger === "writer_busy")
43772
43742
  lkgCoordinator.noteRecovery(lkgPassSnapshot, err.messages);
43773
43743
  if (!budget.sideTurn)
@@ -43818,7 +43788,8 @@ function registerPiContextHandler(pi, baseOptions) {
43818
43788
  canApply: false
43819
43789
  })
43820
43790
  });
43821
- const drops = getTagsBySession2(lkgOptions.db, sessionIdForError).filter((tag) => tag.status === "dropped");
43791
+ assertPiTagSnapshotReady(lkgOptions.db, sessionIdForError);
43792
+ const drops = getPiTagSnapshotView(lkgOptions.db, sessionIdForError).filter((tag) => tag.status === "dropped");
43822
43793
  const changed = applyFlushedStatuses(sessionIdForError, lkgOptions.db, buildPersistedDropTargets(recoveryTranscript, drops, {
43823
43794
  allowReasoningPrefixRewrite: true
43824
43795
  }), drops);
@@ -43849,6 +43820,8 @@ function registerPiContextHandler(pi, baseOptions) {
43849
43820
  }
43850
43821
  logPiLkgRecovery(sessionIdForError, `${failureLabel} ${piStorageErrorReason(err)}: LKG unavailable (${replay.reason}); refusing unreduced ${rawMessageCount}-message input`);
43851
43822
  } catch (replayError) {
43823
+ if (replayError instanceof PiTagHydrationError)
43824
+ throw replayError;
43852
43825
  budget.assertOutcome();
43853
43826
  if (replayError instanceof DegradedPassRefusalError)
43854
43827
  throw replayError;
@@ -43865,11 +43838,7 @@ function registerPiContextHandler(pi, baseOptions) {
43865
43838
  if (err instanceof PiDegradedPassError && err.site === "outgoing-over-window")
43866
43839
  throw contextRefusalError("The request exceeds the model's context window after reclaim, and no fitting saved request is available; this turn was not sent.");
43867
43840
  if (!lkgCompactionOff && sessionIdForError) {
43868
- throw new PiStorageBusyError({
43869
- cause: err instanceof PiDegradedPassError ? err : new PiDegradedPassError("context-handler-failed", {
43870
- cause: err
43871
- })
43872
- });
43841
+ throw piFallbackRefusal("context-handler-failed", err);
43873
43842
  }
43874
43843
  log2(`[magic-context][pi] context handler failed (continuing without mutation): ${message}`, stack);
43875
43844
  return;
@@ -44363,7 +44332,7 @@ Historian previously failed ${failureCount} time(s), so Magic Context is retryin
44363
44332
  force: usage.percentage >= historianForceMaterializationPercentage && (usage.percentage >= 95 || getEmergencyInputSample(db, sessionId) === 0),
44364
44333
  explicitFlush: hasPendingMaterialization(sessionId),
44365
44334
  publishedHistory: args.publishedHistoryRide
44366
- });
44335
+ }, args.eligiblePendingDropTagIds);
44367
44336
  if (!trigger.shouldFire) {
44368
44337
  sessionLog2(sessionId, `historian trigger eval: shouldFire=false (no trigger condition met)`);
44369
44338
  try {
@@ -44571,6 +44540,7 @@ async function runPipeline(args) {
44571
44540
  let materializationSatisfiedThisPass = false;
44572
44541
  let pendingOpsAppliedThisPass = false;
44573
44542
  let pendingOpsDidMutate = false;
44543
+ let eligiblePendingDropTagIds;
44574
44544
  let heuristicOrReasoningDidMutate = false;
44575
44545
  let prefixEditBesidesReasoningTrim = false;
44576
44546
  const recordFirstApplicationWireEdit = (beforeNewerThinking) => {
@@ -45074,7 +45044,9 @@ async function runPipeline(args) {
45074
45044
  ...args.contextUsage.percentage >= 95 ? [] : protectedTagNumbersForPass,
45075
45045
  ...protectedToolTags,
45076
45046
  ...identityAdoption.protectedTagNumbers
45077
- ]), pendingOperationTags, pendingOps, [], new Set, (reduction) => droppedTokenReductions.push(reduction));
45047
+ ]), pendingOperationTags, pendingOps, [], new Set, (reduction) => droppedTokenReductions.push(reduction), undefined, (eligible) => {
45048
+ eligiblePendingDropTagIds = eligible;
45049
+ });
45078
45050
  if (pendingOpsDidMutate) {
45079
45051
  droppedCount += pendingOps.length;
45080
45052
  }
@@ -45805,6 +45777,7 @@ async function runPipeline(args) {
45805
45777
  emergency,
45806
45778
  bustedThisPass,
45807
45779
  agentDropsAppliedThisPass: pendingOpsDidMutate,
45780
+ eligiblePendingDropTagIds,
45808
45781
  prefixEditBesidesReasoningTrim: !!args.recoveryRebuildReason || prefixEditBesidesReasoningTrim || firstRenderBust || args.isCacheBusting || hasPendingMaterializeSignal || deferredMaterializationConsumedThisPass || foldBustsServedPrefixThisPass || publishedM1RefreshedThisPass || materialized || historyWasConsumedThisPass || pendingOpsDidMutate || autoReclaimDidMutateThisPass || emergency,
45809
45782
  targetCount: targets.size,
45810
45783
  reasoningWatermark: args.sessionMeta.clearedReasoningThroughTag ?? 0,
@@ -46037,6 +46010,7 @@ function clearPiCompactionOffInMemoryState(sessionId) {
46037
46010
  clearPiChannel1State(sessionId);
46038
46011
  }
46039
46012
  function clearContextHandlerSession(sessionId) {
46013
+ cancelPiTagSnapshotSession(sessionId);
46040
46014
  lastServedSystemHashBySession.delete(sessionId);
46041
46015
  clearPiHistoryBoundaryRepairSession(sessionId);
46042
46016
  clearPiUpperReclaimSession(sessionId);
@@ -50228,7 +50202,8 @@ async function startPiMagicContextRuntime(pi, database, dbPath) {
50228
50202
  info(compactionOff ? todowriteEnabled ? "registered tools: ctx_search, ctx_memory, ctx_note, ctx_expand, todowrite; registered /todos (ctx_reduce unavailable in compaction-off mode)" : "registered tools: ctx_search, ctx_memory, ctx_note, ctx_expand (ctx_reduce unavailable in compaction-off mode; todowrite disabled)" : todowriteEnabled ? "registered tools: ctx_search, ctx_memory, ctx_note, ctx_expand, todowrite, ctx_reduce; registered /todos" : "registered tools: ctx_search, ctx_memory, ctx_note, ctx_expand, ctx_reduce (todowrite disabled)");
50229
50203
  registerPiCloneSessionTransitions(pi.on.bind(pi), {
50230
50204
  db,
50231
- signalPendingMarker: signalPiDeferredCompactionMarkerDrain
50205
+ signalPendingMarker: signalPiDeferredCompactionMarkerDrain,
50206
+ onInherited: (sessionId) => prewarmPiTagSnapshot(db, sessionId)
50232
50207
  });
50233
50208
  pi.on("session_start", async (event, ctx) => {
50234
50209
  bindStaleBuildNotice(ctx, import.meta.url);
@@ -50249,7 +50224,8 @@ async function startPiMagicContextRuntime(pi, database, dbPath) {
50249
50224
  }
50250
50225
  await handlePiCloneSessionStart(event, ctx, {
50251
50226
  db,
50252
- signalPendingMarker: signalPiDeferredCompactionMarkerDrain
50227
+ signalPendingMarker: signalPiDeferredCompactionMarkerDrain,
50228
+ onInherited: (sessionId) => prewarmPiTagSnapshot(db, sessionId)
50253
50229
  });
50254
50230
  const sessionId = resolveSessionId(ctx);
50255
50231
  if (!piMarkerRehydrationLogged && pendingPiMarkerSessions.length > 0 && sessionId) {
@@ -50265,6 +50241,8 @@ async function startPiMagicContextRuntime(pi, database, dbPath) {
50265
50241
  modelKey: canonicalPiModelKey(model.provider, model.id)
50266
50242
  });
50267
50243
  }
50244
+ if (sessionId)
50245
+ prewarmPiTagSnapshot(db, sessionId);
50268
50246
  });
50269
50247
  pi.on("agent_start", (_event, ctx) => {
50270
50248
  setEmbeddingSessionBusy2(ctx.sessionManager.getSessionId(), true);
@@ -50811,6 +50789,7 @@ async function startPiMagicContextRuntime(pi, database, dbPath) {
50811
50789
  clearPiSystemPromptSession(sessionId);
50812
50790
  promptSurfaceGuidanceEpochs.clear(sessionId);
50813
50791
  clearContextHandlerSession(sessionId);
50792
+ await withTimeout(drainPiTagSnapshotSession(sessionId), SHUTDOWN_DRAIN_MS);
50814
50793
  }
50815
50794
  } catch {}
50816
50795
  activePiRuntimes.delete(dreamerRegistrationOwner);
@@ -50832,6 +50811,9 @@ async function startPiMagicContextRuntime(pi, database, dbPath) {
50832
50811
  const outgoingSessionId = switchOutgoingSessionId;
50833
50812
  switchOutgoingSessionId = undefined;
50834
50813
  try {
50814
+ const incomingSessionId = readSessionId(ctx);
50815
+ if (incomingSessionId && _event?.reason !== "fork")
50816
+ prewarmPiTagSnapshot(db, incomingSessionId);
50835
50817
  if (!outgoingSessionId || outgoingSessionId === readSessionId(ctx))
50836
50818
  return;
50837
50819
  clearPiSystemPromptSession(outgoingSessionId);