@cortexkit/opencode-magic-context 0.44.2 → 0.44.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/dist/{conflict-warning-hook-1msyvb51.js → conflict-warning-hook-hmpzxep6.js} +3 -3
  2. package/dist/{derive-budgets-gd2270s0.js → derive-budgets-ncwn1jk9.js} +4 -2
  3. package/dist/features/magic-context/dreamer/classify.d.ts.map +1 -1
  4. package/dist/features/magic-context/dreamer/hidden-single-shot.d.ts.map +1 -1
  5. package/dist/features/magic-context/dreamer/lease.d.ts +1 -1
  6. package/dist/features/magic-context/dreamer/lease.d.ts.map +1 -1
  7. package/dist/features/magic-context/dreamer/map-memories.d.ts.map +1 -1
  8. package/dist/features/magic-context/dreamer/verify.d.ts.map +1 -1
  9. package/dist/features/magic-context/memory/embedding-openai.d.ts +1 -0
  10. package/dist/features/magic-context/memory/embedding-openai.d.ts.map +1 -1
  11. package/dist/features/magic-context/memory/project-identity.d.ts +8 -4
  12. package/dist/features/magic-context/memory/project-identity.d.ts.map +1 -1
  13. package/dist/features/magic-context/message-index-async.d.ts +1 -1
  14. package/dist/features/magic-context/message-index-async.d.ts.map +1 -1
  15. package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
  16. package/dist/features/magic-context/session-project-storage.d.ts +5 -5
  17. package/dist/features/magic-context/session-project-storage.d.ts.map +1 -1
  18. package/dist/features/magic-context/storage-db.d.ts +6 -1
  19. package/dist/features/magic-context/storage-db.d.ts.map +1 -1
  20. package/dist/features/magic-context/storage-embedding-measurements.d.ts.map +1 -1
  21. package/dist/features/magic-context/v22-deferred-backfill.d.ts.map +1 -1
  22. package/dist/hooks/magic-context/compartment-render-epoch.d.ts +12 -1
  23. package/dist/hooks/magic-context/compartment-render-epoch.d.ts.map +1 -1
  24. package/dist/hooks/magic-context/compartment-runner-historian.d.ts +2 -1
  25. package/dist/hooks/magic-context/compartment-runner-historian.d.ts.map +1 -1
  26. package/dist/hooks/magic-context/compartment-runner-incremental.d.ts.map +1 -1
  27. package/dist/hooks/magic-context/compartment-runner-types.d.ts +1 -0
  28. package/dist/hooks/magic-context/compartment-runner-types.d.ts.map +1 -1
  29. package/dist/hooks/magic-context/derive-budgets.d.ts +4 -0
  30. package/dist/hooks/magic-context/derive-budgets.d.ts.map +1 -1
  31. package/dist/hooks/magic-context/drop-stale-reduce-calls.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/event-resolvers.d.ts +7 -0
  33. package/dist/hooks/magic-context/event-resolvers.d.ts.map +1 -1
  34. package/dist/hooks/magic-context/heuristic-cleanup.d.ts.map +1 -1
  35. package/dist/hooks/magic-context/historian-host-runner.d.ts.map +1 -1
  36. package/dist/hooks/magic-context/inject-compartments.d.ts +5 -0
  37. package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
  38. package/dist/hooks/magic-context/lkg-persist.d.ts +1 -4
  39. package/dist/hooks/magic-context/lkg-persist.d.ts.map +1 -1
  40. package/dist/hooks/magic-context/module-wire.d.ts.map +1 -1
  41. package/dist/hooks/magic-context/producer-window-guard.d.ts +4 -1
  42. package/dist/hooks/magic-context/producer-window-guard.d.ts.map +1 -1
  43. package/dist/hooks/magic-context/read-session-chunk.d.ts +4 -2
  44. package/dist/hooks/magic-context/read-session-chunk.d.ts.map +1 -1
  45. package/dist/hooks/magic-context/read-session-raw.d.ts +2 -2
  46. package/dist/hooks/magic-context/read-session-raw.d.ts.map +1 -1
  47. package/dist/hooks/magic-context/rust-mode-transform.d.ts +1 -0
  48. package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
  49. package/dist/hooks/magic-context/storage-busy-refusal.d.ts +6 -0
  50. package/dist/hooks/magic-context/storage-busy-refusal.d.ts.map +1 -1
  51. package/dist/hooks/magic-context/tokenizer-calibration.d.ts +1 -1
  52. package/dist/hooks/magic-context/tool-drop-target.d.ts.map +1 -1
  53. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +1 -0
  54. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  55. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  56. package/dist/{index-r9vkp4n9.js → index-33q7s9y2.js} +1 -1
  57. package/dist/{index-egkfvhms.js → index-4an2p8qa.js} +80 -14
  58. package/dist/{index-18tjha8g.js → index-638hf49n.js} +1 -1
  59. package/dist/{index-gtzcmv90.js → index-mrc0twf3.js} +12 -6
  60. package/dist/{index-stb5fg12.js → index-msqr3x6m.js} +32 -2
  61. package/dist/{index-y5145x8k.js → index-n96ag4rk.js} +15 -1
  62. package/dist/{index-esxfcz5z.js → index-qm36k34q.js} +56 -13
  63. package/dist/{index-ttdn47aq.js → index-sqp1hz9v.js} +311 -82
  64. package/dist/{index-40s1xz22.js → index-wprsye9w.js} +1 -1
  65. package/dist/{index-3x9caerx.js → index-zx7pmr39.js} +3 -3
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +530 -250
  68. package/dist/plugin/dream-timer.d.ts.map +1 -1
  69. package/dist/plugin/embedding-bootstrap.d.ts.map +1 -1
  70. package/dist/plugin/messages-transform.d.ts.map +1 -1
  71. package/dist/plugin/rpc-handlers.d.ts.map +1 -1
  72. package/dist/{read-session-chunk-wmvgnd24.js → read-session-chunk-339hg90t.js} +3 -3
  73. package/dist/{recomp-orchestrator-rk40ff15.js → recomp-orchestrator-0ers6smx.js} +7 -7
  74. package/dist/{send-session-notification-6xsrcxnp.js → send-session-notification-04hvd54c.js} +2 -2
  75. package/dist/shared/rpc-utils.d.ts +7 -0
  76. package/dist/shared/rpc-utils.d.ts.map +1 -1
  77. package/dist/shared/run-token-log.d.ts +13 -0
  78. package/dist/shared/run-token-log.d.ts.map +1 -0
  79. package/dist/shared/sqlite.d.ts +7 -0
  80. package/dist/shared/sqlite.d.ts.map +1 -1
  81. package/dist/shared/transcript.d.ts +2 -0
  82. package/dist/shared/transcript.d.ts.map +1 -1
  83. package/dist/shared/user-answer.d.ts +4 -0
  84. package/dist/shared/user-answer.d.ts.map +1 -0
  85. package/dist/{transform-postprocess-phase-t7xgdhdf.js → transform-postprocess-phase-ttdkrm6r.js} +7 -7
  86. package/dist/v2/hidden-completion.d.ts +7 -1
  87. package/dist/v2/hidden-completion.d.ts.map +1 -1
  88. package/dist/v2/hooks/context.d.ts.map +1 -1
  89. package/dist/v2/hooks/hidden-terminal-error.d.ts +8 -0
  90. package/dist/v2/hooks/hidden-terminal-error.d.ts.map +1 -0
  91. package/dist/v2/hooks/payload.d.ts.map +1 -1
  92. package/dist/v2/hooks/store.d.ts +1 -1
  93. package/dist/v2/hooks/store.d.ts.map +1 -1
  94. package/dist/v2/hooks/tools.d.ts +1 -1
  95. package/dist/v2/hooks/tools.d.ts.map +1 -1
  96. package/dist/v2/server.js +916 -221
  97. package/dist/v2/store-reader.d.ts +2 -1
  98. package/dist/v2/store-reader.d.ts.map +1 -1
  99. package/dist/{wrapup-orchestrator-h8tr10c6.js → wrapup-orchestrator-x0jvvrqn.js} +8 -8
  100. package/package.json +1 -1
  101. package/src/config/latch-permanence-guard.test.ts +4 -0
  102. package/src/shared/data-path.test.ts +1 -1
  103. package/src/shared/models-dev-cache.test.ts +30 -1
  104. package/src/shared/rpc-utils.test.ts +27 -2
  105. package/src/shared/rpc-utils.ts +44 -15
  106. package/src/shared/run-token-log.test.ts +65 -0
  107. package/src/shared/run-token-log.ts +39 -0
  108. package/src/shared/sqlite-acquisition-routing.test.ts +19 -11
  109. package/src/shared/sqlite-pass-scope.test.ts +115 -144
  110. package/src/shared/sqlite.ts +123 -20
  111. package/src/shared/storage-busy.test.ts +34 -57
  112. package/src/shared/tag-transcript.ts +4 -1
  113. package/src/shared/transcript.ts +3 -0
  114. package/src/shared/user-answer.test.ts +40 -0
  115. package/src/shared/user-answer.ts +17 -0
  116. package/src/shared/write-transaction-attribution.test.ts +13 -11
package/dist/v2/server.js CHANGED
@@ -2901,6 +2901,21 @@ function getSdkOutputLimit(providerID, modelID) {
2901
2901
  loadPersistedApiCacheOnce();
2902
2902
  return lookupMetadataWithTagFallback(apiCache, providerID, modelID)?.outputLimit;
2903
2903
  }
2904
+ function getSdkInputLimit(providerID, modelID) {
2905
+ loadPersistedApiCacheOnce();
2906
+ if (!apiCache)
2907
+ return;
2908
+ const direct = apiCache.get(`${providerID}/${modelID}`)?.inputLimit;
2909
+ if (isSaneLimit(direct))
2910
+ return direct;
2911
+ const colon = modelID.indexOf(":");
2912
+ if (colon > 0) {
2913
+ const tagless = apiCache.get(`${providerID}/${modelID.slice(0, colon)}`)?.inputLimit;
2914
+ if (isSaneLimit(tagless))
2915
+ return tagless;
2916
+ }
2917
+ return;
2918
+ }
2904
2919
  function lookupMetadataWithTagFallback(cache, providerID, modelID) {
2905
2920
  if (!cache)
2906
2921
  return;
@@ -11660,6 +11675,23 @@ import { createHash as createHash3 } from "node:crypto";
11660
11675
  import { existsSync as existsSync6, realpathSync, statSync as statSync4 } from "node:fs";
11661
11676
  import { homedir as homedir6 } from "node:os";
11662
11677
  import path4 from "node:path";
11678
+ function setHomeProjectPermission(allowed) {
11679
+ homeProjectPermission = allowed;
11680
+ }
11681
+ function shouldSkipHomeProjectMemory(directory) {
11682
+ try {
11683
+ assertProjectAllowed(directory, homeProjectPermission);
11684
+ return false;
11685
+ } catch (error) {
11686
+ if (!(error instanceof ProjectIdentityError) || error.errorClass !== "home_project_disabled")
11687
+ throw error;
11688
+ if (!homeProjectSkipLogged) {
11689
+ homeProjectSkipLogged = true;
11690
+ log("[magic-context] home project memory disabled; skipping memory features");
11691
+ }
11692
+ return true;
11693
+ }
11694
+ }
11663
11695
  function asError(error) {
11664
11696
  return error instanceof Error ? error : undefined;
11665
11697
  }
@@ -11723,6 +11755,27 @@ function isGitTimeoutError(error) {
11723
11755
  const signal = getErrorSignal(error);
11724
11756
  return code === "ETIMEDOUT" || signal === "SIGTERM" || signal === "SIGKILL" || getErrorKilled(error);
11725
11757
  }
11758
+ function hasUnbornHead(directory) {
11759
+ const options = {
11760
+ cwd: directory,
11761
+ encoding: "utf8",
11762
+ env: { ...process.env, LC_ALL: "C", LANG: "C" },
11763
+ stdio: ["ignore", "pipe", "pipe"],
11764
+ timeout: GIT_TIMEOUT_MS,
11765
+ windowsHide: true
11766
+ };
11767
+ try {
11768
+ execFileSyncForIdentity("git", ["rev-parse", "--git-dir"], options);
11769
+ try {
11770
+ execFileSyncForIdentity("git", ["rev-parse", "--verify", "--quiet", "HEAD"], options);
11771
+ return false;
11772
+ } catch (error) {
11773
+ return !isGitTimeoutError(error) && typeof error === "object" && error !== null && "status" in error && error.status === 1 && getErrorStderr(error) === "";
11774
+ }
11775
+ } catch {
11776
+ return false;
11777
+ }
11778
+ }
11726
11779
  function classifyGitError(error, rawDirectory) {
11727
11780
  if (isGitTimeoutError(error)) {
11728
11781
  return new ProjectIdentityError("git_timeout", rawDirectory, `git rev-list timed out after ${GIT_TIMEOUT_MS}ms`, asError(error));
@@ -11743,7 +11796,7 @@ function classifyGitError(error, rawDirectory) {
11743
11796
  }
11744
11797
  return new ProjectIdentityError("unknown", rawDirectory, "git rev-list failed while resolving project identity", asError(error));
11745
11798
  }
11746
- function resolveProjectIdentityStrict(directory, allowHomeProject = false) {
11799
+ function resolveProjectIdentityStrict(directory, allowHomeProject = homeProjectPermission) {
11747
11800
  assertProjectAllowed(directory, allowHomeProject);
11748
11801
  const canonical = path4.resolve(directory);
11749
11802
  const cached = identityCache.get(canonical);
@@ -11765,7 +11818,11 @@ function resolveProjectIdentityStrict(directory, allowHomeProject = false) {
11765
11818
  windowsHide: true
11766
11819
  });
11767
11820
  } catch (error) {
11768
- throw classifyGitError(error, directory);
11821
+ const classified = classifyGitError(error, directory);
11822
+ if (classified.errorClass === "not_git_repo" && hasUnbornHead(canonical)) {
11823
+ throw new ProjectIdentityError("no_commits", directory, "Git repository has no commits yet");
11824
+ }
11825
+ throw classified;
11769
11826
  }
11770
11827
  const rootCommit = output.split(`
11771
11828
  `).map((line) => line.trim().slice(0, 64)).filter((line) => /^[0-9a-f]{7,64}$/.test(line)).sort()[0];
@@ -11880,7 +11937,7 @@ function assertProjectAllowed(directory, allowHomeProject) {
11880
11937
  throw new ProjectIdentityError("home_project_disabled", directory, "Home project memory is disabled; set allow_home_project to opt in");
11881
11938
  }
11882
11939
  }
11883
- function resolveProjectIdentity(directory, allowHomeProject = false) {
11940
+ function resolveProjectIdentity(directory, allowHomeProject = homeProjectPermission) {
11884
11941
  assertProjectAllowed(directory, allowHomeProject);
11885
11942
  const canonical = path4.resolve(directory);
11886
11943
  const cachedFallback = directoryFallbackCache.get(canonical);
@@ -11905,6 +11962,8 @@ function resolveProjectIdentity(directory, allowHomeProject = false) {
11905
11962
  } catch (error) {
11906
11963
  if (error instanceof ProjectIdentityError && shouldUseDirectoryFallback(error)) {
11907
11964
  const fallback = directoryFallback(canonical);
11965
+ if (error.errorClass === "no_commits")
11966
+ return fallback;
11908
11967
  const hasGitMetadata = hasGitDir(canonical);
11909
11968
  if (!hasGitMetadata) {
11910
11969
  if (error.errorClass === "permission_denied")
@@ -11926,7 +11985,7 @@ function resolveProjectIdentity(directory, allowHomeProject = false) {
11926
11985
  throw error;
11927
11986
  }
11928
11987
  }
11929
- function resolveProjectIdentityOrFallback(directory, allowHomeProject = false) {
11988
+ function resolveProjectIdentityOrFallback(directory, allowHomeProject = homeProjectPermission) {
11930
11989
  try {
11931
11990
  return resolveProjectIdentity(directory, allowHomeProject);
11932
11991
  } catch (error) {
@@ -12004,7 +12063,7 @@ function describeUnresolvedProjectIdentity(directory) {
12004
12063
  function isUsableProjectIdentity(identity) {
12005
12064
  return typeof identity === "string" && identity.trim().length > 0;
12006
12065
  }
12007
- function resolveProjectIdentityForSession(directory, allowHomeProject = false) {
12066
+ function resolveProjectIdentityForSession(directory, allowHomeProject = homeProjectPermission) {
12008
12067
  const resolvedDirectory = path4.resolve(directory);
12009
12068
  const cacheKey = `${allowHomeProject ? "1" : "0"}\x00${resolvedDirectory}`;
12010
12069
  const cached = sessionIdentityCache.get(cacheKey);
@@ -12033,9 +12092,18 @@ function resolveProjectIdentityForSession(directory, allowHomeProject = false) {
12033
12092
  } catch (error) {
12034
12093
  if (!(error instanceof ProjectIdentityError))
12035
12094
  throw error;
12095
+ const reason = error.errorClass === "git_identity_unavailable" ? pausedIdentityReasons.get(resolvedDirectory) ?? error.errorClass : error.errorClass;
12096
+ pausedIdentityReasons.set(resolvedDirectory, reason);
12097
+ const logKey = `${resolvedDirectory}\x00${reason}`;
12098
+ if (!pausedIdentityLogged.has(logKey)) {
12099
+ pausedIdentityLogged.add(logKey);
12100
+ log(`[magic-context] memory features paused for ${resolvedDirectory}: ${reason}`);
12101
+ }
12036
12102
  identity = undefined;
12037
12103
  }
12038
12104
  }
12105
+ if (identity !== undefined)
12106
+ pausedIdentityReasons.delete(resolvedDirectory);
12039
12107
  sessionIdentityCache.set(cacheKey, {
12040
12108
  identity,
12041
12109
  revalidateAt: identity?.startsWith("git:") === true ? null : nowMs() + TRANSIENT_FAILURE_COOLDOWN_MS
@@ -12076,7 +12144,7 @@ function isLinkedGitWorktree(directory) {
12076
12144
  linkedGitWorktreeCache.set(resolvedDirectory, linked);
12077
12145
  return linked;
12078
12146
  }
12079
- var GIT_TIMEOUT_MS = 5000, TRANSIENT_FAILURE_COOLDOWN_MS, identityCache, linkedGitWorktreeCache, lastKnownGitIdentityCache, directoryFallbackCache, transientFailureCooldown, dubiousOwnershipFallbackDirectories, dubiousOwnershipLoggedDirectories, dubiousOwnershipWarnedDirectories, transientGitIdentityReuseLoggedDirectories, sessionIdentityCache, execFileSyncForIdentity, userHomeDirectoryForIdentity = () => homedir6(), nowMs = () => Date.now(), filesystemProbeObserverForTests, ProjectIdentityError;
12147
+ var GIT_TIMEOUT_MS = 5000, TRANSIENT_FAILURE_COOLDOWN_MS, identityCache, linkedGitWorktreeCache, lastKnownGitIdentityCache, directoryFallbackCache, transientFailureCooldown, dubiousOwnershipFallbackDirectories, dubiousOwnershipLoggedDirectories, dubiousOwnershipWarnedDirectories, transientGitIdentityReuseLoggedDirectories, sessionIdentityCache, homeProjectPermission = false, homeProjectSkipLogged = false, pausedIdentityReasons, pausedIdentityLogged, execFileSyncForIdentity, userHomeDirectoryForIdentity = () => homedir6(), nowMs = () => Date.now(), filesystemProbeObserverForTests, ProjectIdentityError;
12080
12148
  var init_project_identity = __esm(() => {
12081
12149
  init_logger();
12082
12150
  init_project_identity_cache();
@@ -12091,6 +12159,8 @@ var init_project_identity = __esm(() => {
12091
12159
  dubiousOwnershipWarnedDirectories = new Set;
12092
12160
  transientGitIdentityReuseLoggedDirectories = new Set;
12093
12161
  sessionIdentityCache = new Map;
12162
+ pausedIdentityReasons = new Map;
12163
+ pausedIdentityLogged = new Set;
12094
12164
  execFileSyncForIdentity = execFileSync;
12095
12165
  ProjectIdentityError = class ProjectIdentityError extends Error {
12096
12166
  errorClass;
@@ -16122,18 +16192,14 @@ function parseCsvLine(line) {
16122
16192
  }
16123
16193
  function parseTasklistOutput(output) {
16124
16194
  const entries = [];
16125
- let sawHeader = false;
16126
16195
  for (const rawLine of output.split(/\r?\n/)) {
16127
16196
  const line = rawLine.trim();
16128
16197
  if (!line)
16129
16198
  continue;
16130
- if (TASKLIST_NO_TASKS_PATTERN.test(line))
16131
- return [];
16132
16199
  const fields = parseCsvLine(line);
16133
16200
  if (!fields)
16134
16201
  continue;
16135
16202
  if (fields[1]?.trim().toLowerCase() === "pid") {
16136
- sawHeader = true;
16137
16203
  continue;
16138
16204
  }
16139
16205
  const pid = Number(fields[1]);
@@ -16141,11 +16207,11 @@ function parseTasklistOutput(output) {
16141
16207
  continue;
16142
16208
  entries.push({ pid, command: fields[0] });
16143
16209
  }
16144
- return entries.length > 0 || sawHeader ? entries : null;
16210
+ return entries;
16145
16211
  }
16146
16212
  function readWindowsProcess(pid) {
16147
16213
  try {
16148
- const output = rpcIdentityExecFileSync("tasklist", ["/FO", "CSV", "/FI", `PID eq ${pid}`], {
16214
+ const output = rpcIdentityExecFileSync("tasklist", ["/FO", "CSV", "/NH", "/FI", `PID eq ${pid}`], {
16149
16215
  encoding: "utf8",
16150
16216
  timeout: PS_PROBE_TIMEOUT_MS,
16151
16217
  stdio: ["ignore", "pipe", "pipe"],
@@ -16382,7 +16448,11 @@ function tryReadWindowsCimSnapshot(exec) {
16382
16448
  }
16383
16449
  function tryReadWindowsTasklistSnapshot() {
16384
16450
  try {
16385
- const output = execProcessList(rpcProcessListExecFileSync, "tasklist", ["/FO", "CSV"]);
16451
+ const output = execProcessList(rpcProcessListExecFileSync, "tasklist", [
16452
+ "/FO",
16453
+ "CSV",
16454
+ "/NH"
16455
+ ]);
16386
16456
  const entries = parseTasklistOutput(output);
16387
16457
  if (entries === null)
16388
16458
  return null;
@@ -16587,7 +16657,7 @@ function inspectProcessesAsync(forceFresh = false) {
16587
16657
  snapshot = snapshotFromFacts(facts, "cim");
16588
16658
  } catch {}
16589
16659
  if (!snapshot) {
16590
- const entries = parseTasklistOutput(await asyncProcessExec("tasklist", ["/FO", "CSV"], PS_PROBE_TIMEOUT_MS));
16660
+ const entries = parseTasklistOutput(await asyncProcessExec("tasklist", ["/FO", "CSV", "/NH"], PS_PROBE_TIMEOUT_MS));
16591
16661
  if (entries)
16592
16662
  snapshot = snapshotFromFacts(entries.map(({ pid, command }) => ({
16593
16663
  pid,
@@ -16636,13 +16706,25 @@ function inspectProcessesAsync(forceFresh = false) {
16636
16706
  });
16637
16707
  return asyncInspection;
16638
16708
  }
16639
- var ownRpcServerInstanceIds, RPC_IDENTITY_SKEW_TOLERANCE_MS = 120000, LINUX_CLOCK_TICKS_PER_SECOND = 100, PS_PROBE_TIMEOUT_MS = 1000, WINDOWS_CIM_PROBE_TIMEOUT_MS = 5000, WINDOWS_PROCESS_SNAPSHOT_TTL_MS = 2000, MAX_ANCESTOR_WALK_DEPTH = 16, OPEN_CODE_COMMAND_MARKERS, TASKLIST_NO_TASKS_PATTERN, WINDOWS_CIM_COMMAND = "Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,Name,CommandLine,CreationDate | ConvertTo-Json -Compress", PI_HARNESS_ARC_MARKERS, rpcIdentityReadFileSync, rpcIdentityExecFileSync, rpcIdentityProcessKill, rpcProcessListExecFileSync, rpcIdentityPlatform, rpcIdentityNowMs = () => Date.now(), windowsProcessFactsCache = null, windowsProcessSnapshotCache = null, asyncProcessExec, asyncInspection, asyncInspectionExpires = 0, lastAsyncSnapshot = null;
16709
+ function inspectWindowsProcessesSync() {
16710
+ const snapshot = tryReadWindowsCimSnapshot(rpcProcessListExecFileSync) ?? tryReadWindowsTasklistSnapshot();
16711
+ const byPid = new Map(snapshot?.facts.map((fact) => [fact.pid, fact]));
16712
+ return {
16713
+ pi: snapshot ? classifyLivePiSnapshot(snapshot) : { state: "unreadable", processIds: [] },
16714
+ ...snapshot ? { processSnapshot: { source: snapshot.source, facts: snapshot.facts } } : {},
16715
+ evidence: (pid) => ({
16716
+ startTime: byPid.get(pid)?.startTime ?? null,
16717
+ commandLine: byPid.get(pid)?.commandLine ?? byPid.get(pid)?.imageName ?? null
16718
+ }),
16719
+ liveness: (pid) => byPid.has(pid) ? "alive" : snapshot ? "dead" : "inconclusive"
16720
+ };
16721
+ }
16722
+ var ownRpcServerInstanceIds, RPC_IDENTITY_SKEW_TOLERANCE_MS = 120000, LINUX_CLOCK_TICKS_PER_SECOND = 100, PS_PROBE_TIMEOUT_MS = 1000, WINDOWS_CIM_PROBE_TIMEOUT_MS = 5000, WINDOWS_PROCESS_SNAPSHOT_TTL_MS = 2000, MAX_ANCESTOR_WALK_DEPTH = 16, OPEN_CODE_COMMAND_MARKERS, WINDOWS_CIM_COMMAND = "Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,Name,CommandLine,CreationDate | ConvertTo-Json -Compress", PI_HARNESS_ARC_MARKERS, rpcIdentityReadFileSync, rpcIdentityExecFileSync, rpcIdentityProcessKill, rpcProcessListExecFileSync, rpcIdentityPlatform, rpcIdentityNowMs = () => Date.now(), windowsProcessFactsCache = null, windowsProcessSnapshotCache = null, asyncProcessExec, asyncInspection, asyncInspectionExpires = 0, lastAsyncSnapshot = null;
16640
16723
  var init_rpc_utils = __esm(() => {
16641
16724
  init_logger();
16642
16725
  init_pi_executable();
16643
16726
  ownRpcServerInstanceIds = new Set;
16644
16727
  OPEN_CODE_COMMAND_MARKERS = ["opencode", "node", "bun", "electron"];
16645
- TASKLIST_NO_TASKS_PATTERN = /^INFO:\s+No tasks are running which match the specified criteria\.?$/im;
16646
16728
  PI_HARNESS_ARC_MARKERS = [
16647
16729
  "pi-coding-agent",
16648
16730
  "oh-my-pi",
@@ -17362,7 +17444,10 @@ function installTransactionRouting(db, readonly) {
17362
17444
  writable: true,
17363
17445
  value: (sql) => {
17364
17446
  if (/^\s*BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)(?:\s+TRANSACTION)?\s*;?\s*$/i.test(sql)) {
17365
- retryAcquisition(() => nativeExec(sql), sql.trim());
17447
+ if (transformPassScope.getStore()?.active || backgroundWriterScope.getStore())
17448
+ acquireShort(db, () => nativeExec(sql), sql.trim());
17449
+ else
17450
+ nativeExec(sql);
17366
17451
  return db;
17367
17452
  }
17368
17453
  return nativeExec(sql);
@@ -17537,6 +17622,9 @@ function getSqliteMemoryStats() {
17537
17622
  connections
17538
17623
  };
17539
17624
  }
17625
+ function withSqliteBackgroundWriter(operation) {
17626
+ return backgroundWriterScope.run(true, operation);
17627
+ }
17540
17628
  function withSqliteTransformPass(operation) {
17541
17629
  const lease = { active: true };
17542
17630
  return transformPassScope.run(lease, () => {
@@ -17564,22 +17652,71 @@ function isTransientSqliteError(error) {
17564
17652
  const value = error;
17565
17653
  return typeof value.code === "string" && /^(SQLITE_BUSY|SQLITE_LOCKED)(_|$)/.test(value.code) || typeof value.errcode === "number" && [5, 6].includes(value.errcode & 255);
17566
17654
  }
17567
- function retryAcquisition(acquire, stage) {
17568
- if (!transformPassScope.getStore()?.active) {
17655
+ function acquireShort(db, acquire, site) {
17656
+ const started = performance.now();
17657
+ const previous = pragmaNumber(db, "busy_timeout");
17658
+ let acquired = false;
17659
+ try {
17660
+ const timeout = transformPassScope.getStore()?.active && !admissionScope.getStore() && !backgroundWriterScope.getStore() ? FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS : SHORT_BUSY_TIMEOUT_MS;
17661
+ db.exec(`PRAGMA busy_timeout=${timeout}`);
17569
17662
  acquire();
17570
- return;
17663
+ acquired = true;
17664
+ } catch (error) {
17665
+ if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
17666
+ throw new SqliteAcquisitionBusyError(error, site);
17667
+ throw error;
17668
+ } finally {
17669
+ if (previous !== null)
17670
+ db.exec(`PRAGMA busy_timeout=${previous}`);
17671
+ const elapsed = performance.now() - started;
17672
+ if (elapsed >= 250)
17673
+ console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=${site} lane=${transformPassScope.getStore()?.active ? "foreground" : "background"} elapsed=${Math.round(elapsed)}ms attempts=1 outcome=${acquired ? "acquired" : "busy"}`);
17571
17674
  }
17572
- const delays = [500, 1000];
17573
- for (let attempt = 0;; attempt++) {
17675
+ }
17676
+ async function beginSqliteWriterAsync(db, site) {
17677
+ const started = performance.now();
17678
+ let attempts = 0;
17679
+ for (;; ) {
17680
+ attempts++;
17574
17681
  try {
17575
- acquire();
17682
+ withSqliteBackgroundWriter(() => db.exec("BEGIN IMMEDIATE"));
17683
+ const elapsed = performance.now() - started;
17684
+ if (elapsed >= 250)
17685
+ console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=${site} lane=background elapsed=${Math.round(elapsed)}ms attempts=${attempts} outcome=acquired`);
17576
17686
  return;
17577
17687
  } catch (error) {
17578
- if (error instanceof SqliteAcquisitionBusyError || !isTransientSqliteError(error))
17688
+ if (!isTransientSqliteError(error))
17689
+ throw error;
17690
+ const elapsed = performance.now() - started;
17691
+ if (elapsed >= FOREGROUND_ACQUISITION_BUDGET_MS) {
17692
+ console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=${site} lane=background elapsed=${Math.round(elapsed)}ms attempts=${attempts} outcome=busy`);
17693
+ throw error;
17694
+ }
17695
+ await new Promise((resolve) => setTimeout(resolve, Math.min(attempts === 1 ? 500 : 1000, FOREGROUND_ACQUISITION_BUDGET_MS - elapsed)));
17696
+ }
17697
+ }
17698
+ }
17699
+ async function withAsyncPrivilegedWriter(db, operation) {
17700
+ const started = performance.now();
17701
+ let attempts = 0;
17702
+ for (;; ) {
17703
+ attempts++;
17704
+ try {
17705
+ const result = admissionScope.run(true, () => withSqliteTransformPass(() => withPrivilegedWriter(db, operation)));
17706
+ const elapsed = performance.now() - started;
17707
+ if (elapsed >= 250)
17708
+ console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=privileged_writer lane=foreground elapsed=${Math.round(elapsed)}ms attempts=${attempts} outcome=acquired`);
17709
+ return result;
17710
+ } catch (error) {
17711
+ if (!(error instanceof SqliteAcquisitionBusyError))
17579
17712
  throw error;
17580
- if (attempt === delays.length)
17581
- throw new SqliteAcquisitionBusyError(error, stage);
17582
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delays[attempt]);
17713
+ const elapsed = performance.now() - started;
17714
+ if (elapsed >= FOREGROUND_ACQUISITION_BUDGET_MS) {
17715
+ if (elapsed >= 250)
17716
+ console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=privileged_writer lane=foreground elapsed=${Math.round(elapsed)}ms attempts=${attempts} outcome=busy`);
17717
+ throw error instanceof SqliteAcquisitionBusyError ? error : new SqliteAcquisitionBusyError(error);
17718
+ }
17719
+ await new Promise((resolve) => setTimeout(resolve, Math.min(attempts === 1 ? 500 : 1000, FOREGROUND_ACQUISITION_BUDGET_MS - elapsed)));
17583
17720
  }
17584
17721
  }
17585
17722
  }
@@ -17595,7 +17732,13 @@ function withPrivilegedWriter(db, operation) {
17595
17732
  if (nested) {
17596
17733
  db.exec(`SAVEPOINT ${savepoint}`);
17597
17734
  } else {
17598
- retryAcquisition(() => db.exec("BEGIN IMMEDIATE"), "BEGIN IMMEDIATE");
17735
+ try {
17736
+ db.exec("BEGIN IMMEDIATE");
17737
+ } catch (error) {
17738
+ if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
17739
+ throw new SqliteAcquisitionBusyError(error);
17740
+ throw error;
17741
+ }
17599
17742
  }
17600
17743
  privilegeDepth.set(db, previousDepth + 1);
17601
17744
  try {
@@ -17634,7 +17777,7 @@ function withPrivilegedWriter(db, operation) {
17634
17777
  throw error;
17635
17778
  }
17636
17779
  }
17637
- var reportSlowPrivilegedWrite, bunSpec, nodeSpec, SqliteRuntimeUnavailableError, detectedRuntime, isBun, sqliteModule, DatabaseImpl, trackedSqliteConnections, nextSqliteConnectionSequence = 1, TrackedDatabase, Database, privilegeDepth, transformPassScope, SqliteAcquisitionBusyError;
17780
+ var reportSlowPrivilegedWrite, bunSpec, nodeSpec, SqliteRuntimeUnavailableError, detectedRuntime, isBun, sqliteModule, DatabaseImpl, trackedSqliteConnections, nextSqliteConnectionSequence = 1, TrackedDatabase, Database, privilegeDepth, transformPassScope, admissionScope, backgroundWriterScope, SqliteAcquisitionBusyError, SHORT_BUSY_TIMEOUT_MS = 25, FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS = 250, FOREGROUND_ACQUISITION_BUDGET_MS = 16500;
17638
17781
  var init_sqlite = __esm(async () => {
17639
17782
  bunSpec = "bun:" + "sqlite";
17640
17783
  nodeSpec = "node:" + "sqlite";
@@ -17663,11 +17806,13 @@ var init_sqlite = __esm(async () => {
17663
17806
  Database = TrackedDatabase;
17664
17807
  privilegeDepth = new WeakMap;
17665
17808
  transformPassScope = new AsyncLocalStorage;
17809
+ admissionScope = new AsyncLocalStorage;
17810
+ backgroundWriterScope = new AsyncLocalStorage;
17666
17811
  SqliteAcquisitionBusyError = class SqliteAcquisitionBusyError extends Error {
17667
17812
  code = "SQLITE_BUSY";
17668
17813
  stage;
17669
17814
  constructor(cause, stage = "BEGIN IMMEDIATE") {
17670
- super("SQLite writer acquisition remained busy after 3 attempts", { cause });
17815
+ super("SQLite writer acquisition remained busy", { cause });
17671
17816
  this.name = "SqliteAcquisitionBusyError";
17672
17817
  this.stage = stage;
17673
17818
  }
@@ -22218,8 +22363,8 @@ function readRawSessionMessagesFromDb(db, sessionId) {
22218
22363
  };
22219
22364
  });
22220
22365
  }
22221
- function readRawSessionMessagePageFromDb(db, sessionId, afterOrdinal, limit, finalWatermark = Number.MAX_SAFE_INTEGER) {
22222
- const messageRows = readRawMessagePageRows(db, sessionId, afterOrdinal, limit, finalWatermark);
22366
+ function readRawSessionMessagePageFromDb(db, sessionId, afterOrdinal, limit, finalWatermark = Number.MAX_SAFE_INTEGER, after) {
22367
+ const messageRows = readRawMessagePageRows(db, sessionId, afterOrdinal, limit, finalWatermark, after);
22223
22368
  if (messageRows.length === 0)
22224
22369
  return [];
22225
22370
  const placeholders = messageRows.map(() => "?").join(", ");
@@ -22230,15 +22375,22 @@ function readRawSessionMessagePageFromDb(db, sessionId, afterOrdinal, limit, fin
22230
22375
  ORDER BY message_id ASC, time_created ASC, id ASC`).all(sessionId, ...messageRows.map((row) => row.id)).filter(isRawPartRow);
22231
22376
  return assembleRawMessagePage(messageRows, partRows);
22232
22377
  }
22233
- function readRawMessagePageRows(db, sessionId, afterOrdinal, limit, finalWatermark) {
22378
+ function readRawMessagePageRows(db, sessionId, afterOrdinal, limit, finalWatermark, after) {
22234
22379
  const remaining = Math.max(0, Math.floor(finalWatermark) - Math.floor(afterOrdinal));
22235
22380
  const pageSize = Math.min(Math.max(1, Math.floor(limit)), remaining);
22236
22381
  if (pageSize === 0)
22237
22382
  return [];
22383
+ const parameters = [sessionId];
22384
+ if (after)
22385
+ parameters.push(after.timeCreated, after.timeCreated, after.timeCreated, after.id);
22386
+ parameters.push(pageSize);
22387
+ if (!after)
22388
+ parameters.push(Math.max(0, Math.floor(afterOrdinal)));
22238
22389
  return db.prepare(`SELECT id, data, time_created, time_updated
22239
22390
  FROM message
22240
22391
  WHERE session_id = ?
22241
- AND NOT (
22392
+ ${after ? "AND time_created >= ? AND (time_created > ? OR (time_created = ? AND id > ?))" : ""}
22393
+ AND NOT (
22242
22394
  CASE WHEN json_valid(data) = 1
22243
22395
  THEN COALESCE(json_extract(data, '$.summary'), 0)
22244
22396
  ELSE 0 END = 1
@@ -22247,7 +22399,7 @@ function readRawMessagePageRows(db, sessionId, afterOrdinal, limit, finalWaterma
22247
22399
  ELSE '' END = 'stop'
22248
22400
  )
22249
22401
  ORDER BY time_created ASC, id ASC
22250
- LIMIT ? OFFSET ?`).all(sessionId, pageSize, Math.max(0, Math.floor(afterOrdinal))).filter(isRawMessageRow).map((row, index) => ({
22402
+ LIMIT ? ${after ? "" : "OFFSET ?"}`).all(...parameters).filter(isRawMessageRow).map((row, index) => ({
22251
22403
  ...row,
22252
22404
  ordinal: Math.floor(afterOrdinal) + index + 1
22253
22405
  }));
@@ -22274,8 +22426,8 @@ function assembleRawMessagePage(messageRows, partRows) {
22274
22426
  function summaryStringField(jsonPath) {
22275
22427
  return `CASE WHEN json_type(data, '${jsonPath}') = 'text' THEN substr(json_extract(data, '${jsonPath}'), 1, ${RAW_SUMMARY_ARG_MAX_CHARS}) END`;
22276
22428
  }
22277
- function readRawSessionMessageSummaryPageFromDb(db, sessionId, afterOrdinal, limit, finalWatermark = Number.MAX_SAFE_INTEGER) {
22278
- const messageRows = readRawMessagePageRows(db, sessionId, afterOrdinal, limit, finalWatermark);
22429
+ function readRawSessionMessageSummaryPageFromDb(db, sessionId, afterOrdinal, limit, finalWatermark = Number.MAX_SAFE_INTEGER, after) {
22430
+ const messageRows = readRawMessagePageRows(db, sessionId, afterOrdinal, limit, finalWatermark, after);
22279
22431
  if (messageRows.length === 0)
22280
22432
  return [];
22281
22433
  const placeholders = messageRows.map(() => "?").join(", ");
@@ -23143,6 +23295,28 @@ var init_tag_part_guards = __esm(() => {
23143
23295
  init_tag_content_primitives();
23144
23296
  });
23145
23297
 
23298
+ // src/shared/user-answer.ts
23299
+ function hasUserAnswerMetadata(value) {
23300
+ if (!isRecord3(value))
23301
+ return false;
23302
+ if (value.userAnswer === true)
23303
+ return true;
23304
+ if (Array.isArray(value.answers))
23305
+ return true;
23306
+ if (typeof value.answer === "string")
23307
+ return true;
23308
+ if (Array.isArray(value.selectedOptions) || typeof value.customInput === "string")
23309
+ return true;
23310
+ return Array.isArray(value.results) && value.results.some(hasUserAnswerMetadata);
23311
+ }
23312
+ function toolPartHasUserAnswer(part) {
23313
+ if (!isRecord3(part))
23314
+ return false;
23315
+ const state = isRecord3(part.state) ? part.state : undefined;
23316
+ return hasUserAnswerMetadata(state?.metadata) || hasUserAnswerMetadata(part.details);
23317
+ }
23318
+ var init_user_answer = () => {};
23319
+
23146
23320
  // src/features/magic-context/dreamer/token-budget.ts
23147
23321
  function registerBudgetFinalizeChild(sessionId, guard) {
23148
23322
  finalizingChildren.set(sessionId, guard);
@@ -23732,6 +23906,20 @@ var init_tokenizer_calibration_seeds = __esm(() => {
23732
23906
  toolsRatio: 0.850953,
23733
23907
  proseRatio: 1.000017
23734
23908
  },
23909
+ {
23910
+ prefix: "openai/gpt-6-sol",
23911
+ systemRatio: 1.000278,
23912
+ toolsRatio: 0.850953,
23913
+ proseRatio: 1.000017,
23914
+ provenance: "Mirrored from openai/gpt-6-astra; prose inherited-unmeasured. Reported OAuth system/tools counts match GPT-5.5; no independent API-key measurement."
23915
+ },
23916
+ {
23917
+ prefix: "openai/gpt-6.1-sol",
23918
+ systemRatio: 1.000278,
23919
+ toolsRatio: 0.850953,
23920
+ proseRatio: 1.000017,
23921
+ provenance: "Mirrored from openai/gpt-6-astra; prose inherited-unmeasured. Reported OAuth system/tools counts match GPT-5.5; no independent API-key measurement."
23922
+ },
23735
23923
  {
23736
23924
  prefix: "openai/gpt-5",
23737
23925
  systemRatio: 1,
@@ -24085,7 +24273,7 @@ function calibrateBuckets(input) {
24085
24273
  toolCallTokens: toolCalls
24086
24274
  };
24087
24275
  }
24088
- var CALIBRATION_TABLE, NEUTRAL, CALIBRATION_TABLE_REVISION = "2026-09-23-model-id-generation-v2", UNKNOWN_FIT_RATIO;
24276
+ var CALIBRATION_TABLE, NEUTRAL, CALIBRATION_TABLE_REVISION = "2026-09-30-sol-tokenizer-seeds-v3", UNKNOWN_FIT_RATIO;
24089
24277
  var init_tokenizer_calibration = __esm(() => {
24090
24278
  init_tokenizer_calibration_seeds();
24091
24279
  CALIBRATION_TABLE = tokenizer_calibration_seeds_default;
@@ -24733,7 +24921,7 @@ function createToolDropTarget(compositeKey, thinkingParts, index, batch, tagId)
24733
24921
  },
24734
24922
  canDrop: () => {
24735
24923
  const entry = index.get(compositeKey);
24736
- return !!entry && entry.occurrences.length > 0 && entry.hasResult;
24924
+ return !!entry && entry.occurrences.length > 0 && entry.hasResult && !entry.occurrences.some((occurrence) => toolPartHasUserAnswer(occurrence.part));
24737
24925
  },
24738
24926
  requiresToolArcSkeleton: thinkingParts.length > 0 || (index.get(compositeKey)?.occurrences.some((occurrence) => messageHasNativeReasoning(occurrence.message)) ?? false),
24739
24927
  readInput: () => {
@@ -24758,6 +24946,7 @@ function createToolDropTarget(compositeKey, thinkingParts, index, batch, tagId)
24758
24946
  }
24759
24947
  var DROP_PREFIX = "[dropped", IGNORE_PART_TYPES;
24760
24948
  var init_tool_drop_target = __esm(() => {
24949
+ init_user_answer();
24761
24950
  init_dropped_input_guard();
24762
24951
  init_edit_marker();
24763
24952
  init_final_wire_token_estimate();
@@ -24908,6 +25097,8 @@ function getRawSessionMessageOrdinalCount(sessionId) {
24908
25097
  }
24909
25098
  function readRawSessionMessageRangeFromSource(sessionId, fromOrdinal, toOrdinal) {
24910
25099
  const provider = sessionProviders.get(sessionId);
25100
+ if (provider?.iterateMessageRange)
25101
+ return [...provider.iterateMessageRange(fromOrdinal, toOrdinal)];
24911
25102
  if (provider && !provider.readMessagePage) {
24912
25103
  return provider.readMessages().filter((message) => message.ordinal >= fromOrdinal && message.ordinal <= toOrdinal);
24913
25104
  }
@@ -24915,9 +25106,10 @@ function readRawSessionMessageRangeFromSource(sessionId, fromOrdinal, toOrdinal)
24915
25106
  return [];
24916
25107
  const messages = [];
24917
25108
  let afterOrdinal = fromOrdinal - 1;
25109
+ let after;
24918
25110
  while (afterOrdinal < toOrdinal) {
24919
25111
  const limit = Math.min(RAW_MESSAGE_RANGE_PAGE_SIZE, toOrdinal - afterOrdinal);
24920
- const page = provider?.readMessagePage ? provider.readMessagePage(afterOrdinal, limit, toOrdinal) : withReadOnlySessionDb((db) => readRawSessionMessagePageFromDb(db, sessionId, afterOrdinal, limit, toOrdinal));
25112
+ const page = provider?.readMessagePage ? provider.readMessagePage(afterOrdinal, limit, toOrdinal, after) : withReadOnlySessionDb((db) => readRawSessionMessagePageFromDb(db, sessionId, afterOrdinal, limit, toOrdinal, after));
24921
25113
  if (page.length === 0)
24922
25114
  break;
24923
25115
  let nextOrdinal = afterOrdinal;
@@ -24930,6 +25122,8 @@ function readRawSessionMessageRangeFromSource(sessionId, fromOrdinal, toOrdinal)
24930
25122
  if (nextOrdinal <= afterOrdinal)
24931
25123
  break;
24932
25124
  afterOrdinal = nextOrdinal;
25125
+ const last = page.at(-1);
25126
+ after = last ? { timeCreated: last.createdAt ?? 0, id: last.id } : undefined;
24933
25127
  }
24934
25128
  return messages;
24935
25129
  }
@@ -24939,6 +25133,13 @@ function visitRawSessionMessages(sessionId, fromOrdinal, toOrdinal, visit, optio
24939
25133
  if (to < from)
24940
25134
  return;
24941
25135
  const provider = sessionProviders.get(sessionId);
25136
+ if (provider?.iterateMessageRange) {
25137
+ for (const message of provider.iterateMessageRange(from, to)) {
25138
+ if (!visit(message))
25139
+ return;
25140
+ }
25141
+ return;
25142
+ }
24942
25143
  if (provider && !provider.readMessagePage) {
24943
25144
  for (const message of provider.readMessages()) {
24944
25145
  if (message.ordinal < from || message.ordinal > to)
@@ -24952,10 +25153,11 @@ function visitRawSessionMessages(sessionId, fromOrdinal, toOrdinal, visit, optio
24952
25153
  return;
24953
25154
  const pageSize = Math.max(1, Math.floor(options.pageSize ?? RAW_MESSAGE_VISIT_PAGE_SIZE));
24954
25155
  let afterOrdinal = from - 1;
25156
+ let after;
24955
25157
  while (afterOrdinal < to) {
24956
25158
  const limit = Math.min(pageSize, to - afterOrdinal);
24957
25159
  const cursor = afterOrdinal;
24958
- const page = provider?.readMessagePage ? provider.readMessagePage(cursor, limit, to) : withReadOnlySessionDb((db) => options.summary ? readRawSessionMessageSummaryPageFromDb(db, sessionId, cursor, limit, to) : readRawSessionMessagePageFromDb(db, sessionId, cursor, limit, to));
25160
+ const page = provider?.readMessagePage ? provider.readMessagePage(cursor, limit, to, after) : withReadOnlySessionDb((db) => options.summary ? readRawSessionMessageSummaryPageFromDb(db, sessionId, cursor, limit, to, after) : readRawSessionMessagePageFromDb(db, sessionId, cursor, limit, to, after));
24959
25161
  if (page.length === 0)
24960
25162
  return;
24961
25163
  let nextOrdinal = afterOrdinal;
@@ -24969,6 +25171,8 @@ function visitRawSessionMessages(sessionId, fromOrdinal, toOrdinal, visit, optio
24969
25171
  if (nextOrdinal <= afterOrdinal)
24970
25172
  return;
24971
25173
  afterOrdinal = nextOrdinal;
25174
+ const last = page.at(-1);
25175
+ after = last ? { timeCreated: last.createdAt ?? 0, id: last.id } : undefined;
24972
25176
  }
24973
25177
  }
24974
25178
  function readRawSessionMessageRange(sessionId, fromOrdinal, toOrdinal) {
@@ -32692,7 +32896,9 @@ function classifyJunkDiscovery(portFile, raw, staleFiles) {
32692
32896
  log(`[magic-context] removing stale RPC discovery file ${portFile}: ${reason} record older than 10 minutes`);
32693
32897
  return null;
32694
32898
  }
32695
- function inspectRpcServerDiscovery(storageDir, processes) {
32899
+ function inspectRpcServerDiscovery(storageDir, processes, options) {
32900
+ const deadline = options?.deadlineMs === undefined ? Number.POSITIVE_INFINITY : Date.now() + options.deadlineMs;
32901
+ let progressAt = Date.now() + 3000;
32696
32902
  const rpcRoot = join9(storageDir, "rpc");
32697
32903
  let projectEntries;
32698
32904
  try {
@@ -32725,11 +32931,19 @@ function inspectRpcServerDiscovery(storageDir, processes) {
32725
32931
  if (portFiles.length === 0) {
32726
32932
  return { state: "absent", serverPids: [], staleFiles: [] };
32727
32933
  }
32934
+ if (!processes && process.platform === "win32")
32935
+ processes = inspectWindowsProcessesSync();
32728
32936
  const pids = new Set;
32729
32937
  const processByPid = new Map;
32730
32938
  const staleFiles = [];
32731
32939
  const inconclusivePids = new Set;
32732
- for (const portFile of portFiles) {
32940
+ for (const [index, portFile] of portFiles.entries()) {
32941
+ if (Date.now() >= deadline)
32942
+ throw new Error(`RPC holder inspection timed out after ${Math.round((options?.deadlineMs ?? 0) / 1000)} seconds (${index}/${portFiles.length} records checked). Close OpenCode and Pi, then retry.`);
32943
+ if (options?.onProgress && Date.now() >= progressAt) {
32944
+ options.onProgress(index, portFiles.length);
32945
+ progressAt = Date.now() + 3000;
32946
+ }
32733
32947
  let raw;
32734
32948
  try {
32735
32949
  raw = rpcDiscoveryFs.readFileSync(portFile, "utf8");
@@ -32767,7 +32981,7 @@ function inspectRpcServerDiscovery(storageDir, processes) {
32767
32981
  if (!previous || previous.kind === "process" && detected.kind !== "process") {
32768
32982
  processByPid.set(record.pid, detected);
32769
32983
  }
32770
- } else if (identity === "implausible") {
32984
+ } else if (identity === "implausible" && !((processes?.processSnapshot?.source === "cim" || processes?.processSnapshot?.source === "tasklist") && liveness === "alive")) {
32771
32985
  staleFiles.push(portFile);
32772
32986
  } else {
32773
32987
  inconclusivePids.add(record.pid);
@@ -34390,7 +34604,12 @@ function pruneSynapseBatchLedgerForProject(db, projectIdentity, ttlMs = SYNAPSE_
34390
34604
  const ledgerTable = db.prepare("SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = 'synapse_batch_ledger'").get();
34391
34605
  if (!ledgerTable)
34392
34606
  return 0;
34393
- return db.prepare("DELETE FROM synapse_batch_ledger WHERE session_id IN (?, ?) AND updated_at < ?").run(projectIdentity, `shadow:${projectIdentity}`, Date.now() - ttlMs).changes;
34607
+ const shadow = `shadow:${projectIdentity}`;
34608
+ const cutoff = Date.now() - ttlMs;
34609
+ const predicate = "session_id IN (?, ?) AND updated_at < ?";
34610
+ if (!db.prepare(`SELECT 1 FROM synapse_batch_ledger WHERE ${predicate} LIMIT 1`).get(projectIdentity, shadow, cutoff))
34611
+ return 0;
34612
+ return db.prepare(`DELETE FROM synapse_batch_ledger WHERE ${predicate}`).run(projectIdentity, shadow, cutoff).changes;
34394
34613
  }
34395
34614
  var MEASUREMENT_CORPUS_SESSION_ROW_CAP = 2000, SYNAPSE_BATCH_LEDGER_TTL_MS;
34396
34615
  var init_storage_embedding_measurements = __esm(() => {
@@ -36219,6 +36438,21 @@ function resolveKnownHistorianContextLimit(historianModelOverride) {
36219
36438
  const limit = getSdkContextLimit(providerID, modelID, undefined, { reservation: "none" });
36220
36439
  return typeof limit === "number" && limit > 0 ? limit : undefined;
36221
36440
  }
36441
+ function resolveHistorianProducerLimits(modelKey) {
36442
+ if (!modelKey?.includes("/"))
36443
+ return {};
36444
+ const [provider, ...parts] = modelKey.split("/");
36445
+ const model = parts.join("/");
36446
+ if (!provider || !model)
36447
+ return {};
36448
+ const input = getSdkInputLimit(provider, model);
36449
+ const window2 = getSdkWindowGeometry(provider, model)?.derivation.window;
36450
+ const context = window2 ?? (input === undefined ? resolveKnownHistorianContextLimit(modelKey) : undefined);
36451
+ return {
36452
+ ...context !== undefined ? { context } : {},
36453
+ ...input !== undefined ? { input } : {}
36454
+ };
36455
+ }
36222
36456
  function resolveHistorianContextLimit(historianModelOverride) {
36223
36457
  if (typeof historianModelOverride === "string" && historianModelOverride.includes("/")) {
36224
36458
  return resolveKnownHistorianContextLimit(historianModelOverride) ?? DEFAULT_HISTORIAN_CONTEXT_FALLBACK;
@@ -74214,6 +74448,28 @@ var init_embedding_ssrf = __esm(() => {
74214
74448
  });
74215
74449
 
74216
74450
  // src/features/magic-context/memory/embedding-openai.ts
74451
+ function capEmbeddingInput(input, maxInputTokens, purpose) {
74452
+ const ratio = purpose === "query" ? CHUNK_WINDOW_SAFETY_RATIO : 1;
74453
+ const budget = Math.max(1, Math.floor(maxInputTokens * ratio));
74454
+ if (estimateTokens(input) <= budget)
74455
+ return input;
74456
+ let low = 0;
74457
+ let high = input.length;
74458
+ while (low < high) {
74459
+ const mid = Math.ceil((low + high) / 2);
74460
+ if (estimateTokens(input.slice(0, mid)) <= budget)
74461
+ low = mid;
74462
+ else
74463
+ high = mid - 1;
74464
+ }
74465
+ if (low > 0 && low < input.length) {
74466
+ const last = input.charCodeAt(low - 1);
74467
+ const next = input.charCodeAt(low);
74468
+ if (last >= 55296 && last <= 56319 && next >= 56320 && next <= 57343)
74469
+ low -= 1;
74470
+ }
74471
+ return input.slice(0, low);
74472
+ }
74217
74473
  function normalizeEndpoint3(endpoint) {
74218
74474
  return endpoint?.trim().replace(/\/+$/, "") ?? "";
74219
74475
  }
@@ -74236,6 +74492,7 @@ class OpenAICompatibleEmbeddingProvider {
74236
74492
  modelMismatchLogged = false;
74237
74493
  lastFailureReason = null;
74238
74494
  halfOpenProbeInFlight = false;
74495
+ queryTruncationLogged = false;
74239
74496
  constructor(options) {
74240
74497
  this.endpoint = normalizeEndpoint3(options.endpoint);
74241
74498
  this.model = options.model?.trim() ?? "";
@@ -74289,7 +74546,15 @@ class OpenAICompatibleEmbeddingProvider {
74289
74546
  return [];
74290
74547
  }
74291
74548
  const textPrefix = purpose === "query" ? this.queryPrefix : this.documentPrefix;
74292
- const requestTexts = texts.map((text) => `${textPrefix}${text.trim().length === 0 ? " " : text}`);
74549
+ const requestTexts = texts.map((text) => {
74550
+ const input = `${textPrefix}${text.trim().length === 0 ? " " : text}`;
74551
+ const capped = capEmbeddingInput(input, this.maxInputTokens, purpose);
74552
+ if (purpose === "query" && capped.length < input.length && !this.queryTruncationLogged) {
74553
+ log(`[magic-context] embedding query truncated from ${input.length} to ${capped.length} characters to fit max_input_tokens`);
74554
+ this.queryTruncationLogged = true;
74555
+ }
74556
+ return capped;
74557
+ });
74293
74558
  if (!await this.initialize()) {
74294
74559
  return Array.from({ length: texts.length }, () => null);
74295
74560
  }
@@ -74498,8 +74763,10 @@ class OpenAICompatibleEmbeddingProvider {
74498
74763
  }
74499
74764
  var FAILURE_THRESHOLD = 3, FAILURE_WINDOW_MS = 60000, OPEN_DURATION_MS, FETCH_TIMEOUT_MS = 30000;
74500
74765
  var init_embedding_openai = __esm(() => {
74766
+ init_read_session_formatting();
74501
74767
  init_logger();
74502
74768
  init_redaction();
74769
+ init_compartment_chunk_embedding();
74503
74770
  init_embedding_identity();
74504
74771
  init_embedding_model_match();
74505
74772
  init_embedding_ssrf();
@@ -74831,6 +75098,14 @@ var init_embedding_cache = __esm(() => {
74831
75098
  });
74832
75099
 
74833
75100
  // src/features/magic-context/session-project-storage.ts
75101
+ function hasMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath) {
75102
+ let stmt = misScopedProjectChunkStatements.get(db);
75103
+ if (!stmt) {
75104
+ stmt = db.prepare(`SELECT 1 FROM (${MIS_SCOPED_PROJECT_CHUNK_IDS_SQL}) LIMIT 1`);
75105
+ misScopedProjectChunkStatements.set(db, stmt);
75106
+ }
75107
+ return !!stmt.get(projectPath, projectPath);
75108
+ }
74834
75109
  function getUpsertSessionProjectStatement(db) {
74835
75110
  let stmt = upsertSessionProjectStatements.get(db);
74836
75111
  if (!stmt) {
@@ -74866,23 +75141,11 @@ function getRepairProjectChunkProjectStatement(db) {
74866
75141
  if (!stmt) {
74867
75142
  stmt = db.prepare(`UPDATE compartment_chunk_embeddings
74868
75143
  SET project_path = (
74869
- SELECT sp.project_path
74870
- FROM session_projects sp
75144
+ SELECT sp.project_path FROM session_projects sp
74871
75145
  WHERE sp.session_id = compartment_chunk_embeddings.session_id
74872
75146
  AND sp.harness = compartment_chunk_embeddings.harness
74873
- LIMIT 1
74874
75147
  )
74875
- WHERE EXISTS (
74876
- SELECT 1
74877
- FROM session_projects sp
74878
- WHERE sp.session_id = compartment_chunk_embeddings.session_id
74879
- AND sp.harness = compartment_chunk_embeddings.harness
74880
- AND sp.project_path <> compartment_chunk_embeddings.project_path
74881
- AND (
74882
- sp.project_path = ?
74883
- OR compartment_chunk_embeddings.project_path = ?
74884
- )
74885
- )`);
75148
+ WHERE id IN (${MIS_SCOPED_PROJECT_CHUNK_IDS_SQL})`);
74886
75149
  repairProjectChunkProjectStatements.set(db, stmt);
74887
75150
  }
74888
75151
  return stmt;
@@ -74902,14 +75165,24 @@ function recordSessionProjectIdentity(db, sessionId, projectPath) {
74902
75165
  function repairMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath) {
74903
75166
  if (!projectPath)
74904
75167
  return 0;
75168
+ if (!hasMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath))
75169
+ return 0;
74905
75170
  return getRepairProjectChunkProjectStatement(db).run(projectPath, projectPath).changes;
74906
75171
  }
74907
- var SESSION_CHUNK_REPAIR_BATCH_SIZE = 100, upsertSessionProjectStatements, repairSessionChunkProjectStatements, repairProjectChunkProjectStatements;
75172
+ var SESSION_CHUNK_REPAIR_BATCH_SIZE = 100, upsertSessionProjectStatements, repairSessionChunkProjectStatements, repairProjectChunkProjectStatements, misScopedProjectChunkStatements, MIS_SCOPED_PROJECT_CHUNK_IDS_SQL = `
75173
+ SELECT e.id FROM compartment_chunk_embeddings e
75174
+ JOIN session_projects sp ON sp.session_id = e.session_id AND sp.harness = e.harness
75175
+ WHERE e.project_path = ? AND sp.project_path <> e.project_path
75176
+ UNION ALL
75177
+ SELECT e.id FROM session_projects sp
75178
+ JOIN compartment_chunk_embeddings e ON e.session_id = sp.session_id
75179
+ WHERE sp.project_path = ? AND e.harness = sp.harness AND e.project_path <> sp.project_path`;
74908
75180
  var init_session_project_storage = __esm(() => {
74909
75181
  init_project_identity();
74910
75182
  upsertSessionProjectStatements = new WeakMap;
74911
75183
  repairSessionChunkProjectStatements = new WeakMap;
74912
75184
  repairProjectChunkProjectStatements = new WeakMap;
75185
+ misScopedProjectChunkStatements = new WeakMap;
74913
75186
  });
74914
75187
 
74915
75188
  // src/features/magic-context/shadow-backfill-state.ts
@@ -75338,6 +75611,14 @@ function recordActiveEmbeddingIdentity(db, projectIdentity, currentProviderIdent
75338
75611
  if (currentProviderIdentity === OFF_PROVIDER_IDENTITY) {
75339
75612
  return;
75340
75613
  }
75614
+ const scopes = [["chunk", currentChunkIdentity]];
75615
+ if (features.memoryEnabled)
75616
+ scopes.push(["memory", currentProviderIdentity]);
75617
+ if (features.gitCommitEnabled)
75618
+ scopes.push(["commit", currentProviderIdentity]);
75619
+ const active = db.prepare("SELECT 1 FROM embedding_identity_active WHERE project_path = ? AND scope = ? AND model_id = ?");
75620
+ if (scopes.every(([scope, model]) => active.get(projectIdentity, scope, model)) && !hasMisScopedCompartmentChunkEmbeddingsForProject(db, projectIdentity))
75621
+ return;
75341
75622
  const now = Date.now();
75342
75623
  const transactionStartedAt = performance.now();
75343
75624
  db.exec("BEGIN IMMEDIATE");
@@ -75385,7 +75666,9 @@ function registerProjectEmbedding(db, projectIdentity, config, features, sourceD
75385
75666
  observationMode: false
75386
75667
  };
75387
75668
  projectRegistrations.set(projectIdentity, registration);
75388
- persistPrimaryDescriptor(db, registration);
75669
+ if (generationChanged || !db.prepare("SELECT 1 FROM embedding_registrations WHERE project_path = ? AND provider_identity = ? AND chunk_model_id = ? AND generation = ?").get(projectIdentity, providerIdentity, registration.chunkModelId, generation)) {
75670
+ persistPrimaryDescriptor(db, registration);
75671
+ }
75389
75672
  if (!canReuseProvider) {
75390
75673
  disposeProvider(prior?.provider ?? null);
75391
75674
  }
@@ -81925,6 +82208,27 @@ async function teardownChildSession(args) {
81925
82208
  }
81926
82209
  var init_child_session_teardown = () => {};
81927
82210
 
82211
+ // src/shared/run-token-log.ts
82212
+ function count2(value) {
82213
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
82214
+ }
82215
+ function runTokenLog(tokens, maxTokens, finish) {
82216
+ const usage = tokens && typeof tokens === "object" ? tokens : {};
82217
+ const cache = usage.cache && typeof usage.cache === "object" ? usage.cache : {};
82218
+ return {
82219
+ input: count2(usage.input ?? usage.input_tokens),
82220
+ output: count2(usage.output ?? usage.output_tokens),
82221
+ reasoning: count2(usage.reasoning ?? usage.reasoning_tokens),
82222
+ cache_read: count2(usage.cacheRead ?? usage.cache_read ?? usage.cached_input_tokens ?? cache.read),
82223
+ cache_write: count2(usage.cacheWrite ?? usage.cache_write ?? usage.cache_write_tokens ?? cache.write),
82224
+ max_tokens: count2(maxTokens),
82225
+ finish_reason: typeof finish === "string" ? finish : null
82226
+ };
82227
+ }
82228
+ function formatRunTokenLog(tokens) {
82229
+ return `tokens=${JSON.stringify(tokens)}`;
82230
+ }
82231
+
81928
82232
  // src/features/magic-context/schema-fence-probe.ts
81929
82233
  function recordStaleFence(persistedVersion, supportedVersion, reason = "newer_schema") {
81930
82234
  state.consecutiveFailures += 1;
@@ -82314,11 +82618,14 @@ function clampProducerAtomChars(text, maxChars, marker) {
82314
82618
  const tailChars = Math.floor(keptChars / 2);
82315
82619
  return `${text.slice(0, headChars)}${marker}${text.slice(text.length - tailChars)}`;
82316
82620
  }
82317
- function producerInputTokenLimit(contextLimitTokens, maxOutputTokens) {
82318
- if (typeof contextLimitTokens !== "number" || !Number.isFinite(contextLimitTokens) || contextLimitTokens <= 0 || !Number.isFinite(maxOutputTokens) || maxOutputTokens < 0) {
82621
+ function producerInputTokenLimit(contextLimitTokens, maxOutputTokens, inputLimitTokens) {
82622
+ if (!Number.isFinite(maxOutputTokens) || maxOutputTokens < 0)
82319
82623
  return;
82320
- }
82321
- const usableInputTokens = Math.floor(contextLimitTokens - maxOutputTokens);
82624
+ const shared = typeof contextLimitTokens === "number" && Number.isFinite(contextLimitTokens) && contextLimitTokens > 0 ? contextLimitTokens - maxOutputTokens : undefined;
82625
+ const input = typeof inputLimitTokens === "number" && Number.isFinite(inputLimitTokens) && inputLimitTokens > 0 ? inputLimitTokens : undefined;
82626
+ if (shared === undefined && input === undefined)
82627
+ return;
82628
+ const usableInputTokens = Math.floor(Math.min(shared ?? Infinity, input ?? Infinity));
82322
82629
  if (usableInputTokens <= 0)
82323
82630
  return;
82324
82631
  const limit = Math.floor(usableInputTokens * (1 - PRODUCER_WINDOW_REFUSAL_MARGIN));
@@ -82330,15 +82637,15 @@ function historianProducerReserve(window2, configuredOutput, catalogOutput) {
82330
82637
  return window2 === undefined ? 0 : catalogOutputReserve(window2, catalogOutput);
82331
82638
  }
82332
82639
  function producerWindowFailureReason(input) {
82333
- const { producerSourceTokens, contextLimitTokens, maxOutputTokens } = input;
82334
- const producerInputLimitTokens = producerInputTokenLimit(contextLimitTokens, maxOutputTokens);
82335
- if (producerInputLimitTokens === undefined || typeof contextLimitTokens !== "number" || !Number.isFinite(producerSourceTokens) || producerSourceTokens <= 0) {
82640
+ const { producerSourceTokens, contextLimitTokens, inputLimitTokens, maxOutputTokens } = input;
82641
+ const producerInputLimitTokens = producerInputTokenLimit(contextLimitTokens, maxOutputTokens, inputLimitTokens);
82642
+ if (producerInputLimitTokens === undefined || !Number.isFinite(producerSourceTokens) || producerSourceTokens <= 0) {
82336
82643
  return null;
82337
82644
  }
82338
- const usableInputTokens = Math.max(0, Math.floor(contextLimitTokens - maxOutputTokens));
82645
+ const usableInputTokens = Math.floor(Math.min(contextLimitTokens === undefined ? Infinity : Math.max(0, contextLimitTokens - maxOutputTokens), inputLimitTokens ?? Infinity));
82339
82646
  if (producerSourceTokens <= producerInputLimitTokens)
82340
82647
  return null;
82341
- return `producer_source_exceeds_window producer_source_tokens=${Math.round(producerSourceTokens)} usable_input_tokens=${usableInputTokens} producer_input_limit_tokens=${producerInputLimitTokens} context_limit_tokens=${Math.round(contextLimitTokens)} max_output_tokens=${Math.round(maxOutputTokens)} estimator_margin=${PRODUCER_WINDOW_REFUSAL_MARGIN}`;
82648
+ return `producer_source_exceeds_window producer_source_tokens=${Math.round(producerSourceTokens)} usable_input_tokens=${usableInputTokens} producer_input_limit_tokens=${producerInputLimitTokens} context_limit_tokens=${contextLimitTokens === undefined ? "unknown" : Math.round(contextLimitTokens)} max_output_tokens=${Math.round(maxOutputTokens)} estimator_margin=${PRODUCER_WINDOW_REFUSAL_MARGIN}`;
82342
82649
  }
82343
82650
  function splitMarkerPair() {
82344
82651
  return `
@@ -82347,7 +82654,7 @@ ${HISTORIAN_TRUNCATION_MARKER}
82347
82654
  `;
82348
82655
  }
82349
82656
  function fitAtomicHistorianSourceToProducerWindow(args) {
82350
- const producerInputLimitTokens = producerInputTokenLimit(args.contextLimitTokens, args.maxOutputTokens);
82657
+ const producerInputLimitTokens = producerInputTokenLimit(args.contextLimitTokens, args.maxOutputTokens, args.inputLimitTokens);
82351
82658
  const originalTokens = estimateTokens(args.text);
82352
82659
  if (producerInputLimitTokens === undefined || originalTokens < producerInputLimitTokens || producerInputLimitTokens <= 0) {
82353
82660
  return { text: args.text, producerInputLimitTokens, removedTokens: 0 };
@@ -82381,7 +82688,7 @@ function fitAtomicHistorianSourceToProducerWindow(args) {
82381
82688
  };
82382
82689
  }
82383
82690
  function producerPromptFailureReason(input) {
82384
- const limit = producerInputTokenLimit(input.contextLimitTokens, input.maxOutputTokens);
82691
+ const limit = producerInputTokenLimit(input.contextLimitTokens, input.maxOutputTokens, input.inputLimitTokens);
82385
82692
  const tokens = providerMass({ prose: input.sourceLocal, system: input.systemLocal, tools: input.toolsLocal }, calibrationForModelKey(input.modelKey), true);
82386
82693
  if (limit === undefined)
82387
82694
  return null;
@@ -82497,12 +82804,15 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
82497
82804
  throw new Error(`Historian host recorded an assistant error${finish}: ${describeAssistantFailure(assistantFailure.error)}`);
82498
82805
  }
82499
82806
  const text = extractLatestAssistantText(messages);
82807
+ const latest = Array.isArray(messages) ? messages.filter((message) => isRecord3(message) && isRecord3(message.info) && message.info.role === "assistant").sort((left, right) => historianMessageCreatedAt(right) - historianMessageCreatedAt(left))[0] : undefined;
82808
+ const info = latest && isRecord3(latest.info) ? latest.info : {};
82500
82809
  return {
82501
82810
  messages,
82502
82811
  text,
82503
82812
  reasoning: text ? null : extractLatestHistorianReasoning(messages),
82504
82813
  lengthCapped: hasLengthCappedOutput(messages),
82505
- usage: sumTokensFromChildMessages(messages)
82814
+ usage: sumTokensFromChildMessages(messages),
82815
+ tokenLog: runTokenLog(info.tokens, undefined, info.finish ?? info.finish_reason ?? info.finishReason)
82506
82816
  };
82507
82817
  },
82508
82818
  async close(handle, settlement) {
@@ -82521,8 +82831,8 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
82521
82831
  }
82522
82832
  };
82523
82833
  }
82524
- function historianReasoningBudgetDiagnostic(outputTokens) {
82525
- return `historian ran out of output budget while reasoning (length-capped at ${outputTokens} tokens, no text) — set historian.maxTokens or route historian.model to a low-reasoning lane/variant`;
82834
+ function historianReasoningBudgetDiagnostic(outputTokens, tokens) {
82835
+ return `historian ran out of output budget while reasoning (length-capped at ${outputTokens} tokens, no text${tokens ? `; ${formatRunTokenLog(tokens)}` : ""}) — set historian.maxTokens or route historian.model to a low-reasoning lane/variant`;
82526
82836
  }
82527
82837
  async function runValidatedHistorianPass(args) {
82528
82838
  const firstRun = await runHistorianPrompt({
@@ -82711,8 +83021,10 @@ async function runHistorianPrompt(args) {
82711
83021
  const modelKey = selected ? `${selected.providerID}/${selected.modelID}` : undefined;
82712
83022
  const system = withContentLanguageDirective(agentId === HISTORIAN_EDITOR_AGENT ? HISTORIAN_EDITOR_SYSTEM_PROMPT : COMPARTMENT_AGENT_SYSTEM_PROMPT, args.language);
82713
83023
  const contextLimitTokens = selected ? getSdkContextLimit(selected.providerID, selected.modelID, undefined, { reservation: "none" }) : undefined;
82714
- const reserve = historianProducerReserve(contextLimitTokens, args.maxOutputTokens, selected ? getSdkOutputLimit(selected.providerID, selected.modelID) : undefined);
82715
- if (contextLimitTokens !== undefined && producerInputTokenLimit(contextLimitTokens, reserve) === undefined && modelKey && !unknownProducerWindows.has(modelKey)) {
83024
+ const producerLimits = selected ? resolveHistorianProducerLimits(modelKey) : {};
83025
+ const producerContext = producerLimits.context ?? (producerLimits.input === undefined ? contextLimitTokens : undefined);
83026
+ const reserve = historianProducerReserve(producerContext, args.maxOutputTokens, selected ? getSdkOutputLimit(selected.providerID, selected.modelID) : undefined);
83027
+ if (contextLimitTokens !== undefined && producerInputTokenLimit(producerContext, reserve, producerLimits.input) === undefined && modelKey && !unknownProducerWindows.has(modelKey)) {
82716
83028
  unknownProducerWindows.add(modelKey);
82717
83029
  sessionLog(parentSessionId, `producer window inconsistent for ${modelKey}: window=${contextLimitTokens} reserve=${reserve}; sending unguarded`);
82718
83030
  }
@@ -82721,7 +83033,8 @@ async function runHistorianPrompt(args) {
82721
83033
  systemLocal: estimateTokens(system),
82722
83034
  toolsLocal: 0,
82723
83035
  modelKey,
82724
- contextLimitTokens,
83036
+ contextLimitTokens: producerContext,
83037
+ inputLimitTokens: producerLimits.input,
82725
83038
  maxOutputTokens: reserve
82726
83039
  });
82727
83040
  if (failure)
@@ -82755,9 +83068,15 @@ async function runHistorianPrompt(args) {
82755
83068
  }
82756
83069
  completion = await executor.collect(handle, 50);
82757
83070
  const lengthCapped = completion.lengthCapped;
83071
+ const tokens = {
83072
+ ...runTokenLog(undefined, args.maxOutputTokens),
83073
+ ...completion.tokenLog,
83074
+ max_tokens: args.maxOutputTokens ?? null
83075
+ };
83076
+ sessionLog(parentSessionId, `historian response_chars=${(completion.text ?? completion.reasoning ?? "").length} ${formatRunTokenLog(tokens)}`);
82758
83077
  const textResult = completion.text;
82759
83078
  const reasoningResult = textResult ? null : completion.reasoning;
82760
- const emptyError = !textResult && reasoningResult && lengthCapped ? historianReasoningBudgetDiagnostic(completion.usage.output) : !textResult && !reasoningResult ? "Historian returned no assistant output." : !textResult ? "Historian returned reasoning but no assistant text." : lengthCapped ? "Historian returned length-capped output." : null;
83079
+ const emptyError = !textResult && reasoningResult && lengthCapped ? historianReasoningBudgetDiagnostic(completion.usage.output, tokens) : !textResult && !reasoningResult ? "Historian returned no assistant output." : !textResult ? "Historian returned reasoning but no assistant text." : lengthCapped ? `Historian returned length-capped output. ${formatRunTokenLog(tokens)}` : null;
82761
83080
  const invocationId = recordInvocation({
82762
83081
  status: emptyError ? "empty" : "completed",
82763
83082
  messages: completion.messages,
@@ -82943,6 +83262,7 @@ var init_compartment_runner_historian = __esm(async () => {
82943
83262
  init_compartment_prompt();
82944
83263
  init_compartment_runner_types();
82945
83264
  init_compartment_runner_validation();
83265
+ init_derive_budgets();
82946
83266
  init_producer_window_guard();
82947
83267
  init_read_session_formatting();
82948
83268
  await __promiseAll([
@@ -83956,7 +84276,7 @@ var init_render_trigger = __esm(async () => {
83956
84276
  });
83957
84277
 
83958
84278
  // src/hooks/magic-context/compartment-render-epoch.ts
83959
- function encodeCachedM0UpgradeIdentity(upgradeState, compartmentRenderEpoch = COMPARTMENT_RENDER_EPOCH, muralEnabled = null, renderBudgetIdentity = null, memoryRenderEpoch = MEMORY_RENDER_FORMAT_EPOCH) {
84279
+ function encodeCachedM0UpgradeIdentity(upgradeState, compartmentRenderEpoch = COMPARTMENT_RENDER_EPOCH, muralEnabled = null, renderBudgetIdentity = null, memoryRenderEpoch = MEMORY_RENDER_FORMAT_EPOCH, renderedBudgets = null) {
83960
84280
  let encoded = upgradeState ?? "";
83961
84281
  if (compartmentRenderEpoch !== null) {
83962
84282
  encoded += `${EPOCH_COMPONENT_PREFIX}${compartmentRenderEpoch}`;
@@ -83970,6 +84290,9 @@ function encodeCachedM0UpgradeIdentity(upgradeState, compartmentRenderEpoch = CO
83970
84290
  if (renderBudgetIdentity !== null) {
83971
84291
  encoded += `${BUDGET_COMPONENT_PREFIX}${renderBudgetIdentity}`;
83972
84292
  }
84293
+ if (renderedBudgets !== null) {
84294
+ encoded += `${RENDERED_BUDGETS_COMPONENT_PREFIX}${renderedBudgets}`;
84295
+ }
83973
84296
  return encoded.length > 0 ? encoded : null;
83974
84297
  }
83975
84298
  function component(value, prefix) {
@@ -83988,14 +84311,16 @@ function decodeCachedM0UpgradeIdentity(value) {
83988
84311
  compartmentRenderEpoch: null,
83989
84312
  memoryRenderEpoch: null,
83990
84313
  muralEnabled: null,
83991
- renderBudgetIdentity: null
84314
+ renderBudgetIdentity: null,
84315
+ renderedBudgets: null
83992
84316
  };
83993
84317
  }
83994
84318
  const componentIndexes = [
83995
84319
  value.indexOf(EPOCH_COMPONENT_PREFIX),
83996
84320
  value.indexOf(MEMORY_EPOCH_COMPONENT_PREFIX),
83997
84321
  value.indexOf(MURAL_COMPONENT_PREFIX),
83998
- value.indexOf(BUDGET_COMPONENT_PREFIX)
84322
+ value.indexOf(BUDGET_COMPONENT_PREFIX),
84323
+ value.indexOf(RENDERED_BUDGETS_COMPONENT_PREFIX)
83999
84324
  ].filter((index) => index >= 0);
84000
84325
  const identityEnd = componentIndexes.length > 0 ? Math.min(...componentIndexes) : value.length;
84001
84326
  const upgradeState = value.slice(0, identityEnd);
@@ -84005,10 +84330,37 @@ function decodeCachedM0UpgradeIdentity(value) {
84005
84330
  compartmentRenderEpoch: component(value, EPOCH_COMPONENT_PREFIX),
84006
84331
  memoryRenderEpoch: component(value, MEMORY_EPOCH_COMPONENT_PREFIX),
84007
84332
  muralEnabled: muralComponent === "1" ? true : muralComponent === "0" ? false : null,
84008
- renderBudgetIdentity: component(value, BUDGET_COMPONENT_PREFIX)
84333
+ renderBudgetIdentity: component(value, BUDGET_COMPONENT_PREFIX),
84334
+ renderedBudgets: component(value, RENDERED_BUDGETS_COMPONENT_PREFIX)
84009
84335
  };
84010
84336
  }
84011
- var COMPARTMENT_RENDER_EPOCH = "cre2", MEMORY_RENDER_FORMAT_EPOCH = "mre3", EPOCH_COMPONENT_PREFIX = "|compartment-render:", MEMORY_EPOCH_COMPONENT_PREFIX = "|memory-render:", MURAL_COMPONENT_PREFIX = "|mural-enabled:", BUDGET_COMPONENT_PREFIX = "|render-budgets:";
84337
+ function renderBudgetIdentityChanged(cached, current) {
84338
+ const legacy = /^(m[^-]+)-h\d+$/.exec(cached);
84339
+ if (legacy && current.startsWith("m") && current.includes("-hp")) {
84340
+ return legacy[1] !== current.slice(0, current.indexOf("-h"));
84341
+ }
84342
+ return cached !== current;
84343
+ }
84344
+ function renderedBudgetSnapshot(memoryTokens, historyTokens) {
84345
+ return `m${memoryTokens}-h${historyTokens}`;
84346
+ }
84347
+ function readRenderedBudgets(value) {
84348
+ const match = value?.match(/^m(\d+(?:\.\d+)?)-h(\d+(?:\.\d+)?)$/);
84349
+ if (!match)
84350
+ return null;
84351
+ const memory = Number(match[1]);
84352
+ const history = Number(match[2]);
84353
+ return Number.isFinite(memory) && Number.isFinite(history) ? { memory, history } : null;
84354
+ }
84355
+ function renderedBudgetShrinkReason(cached, current) {
84356
+ const before = readRenderedBudgets(cached);
84357
+ const after = readRenderedBudgets(current);
84358
+ if (!before || !after)
84359
+ return null;
84360
+ const shrunk = (oldTokens, newTokens) => oldTokens - newTokens > Math.max(64, oldTokens * 0.01);
84361
+ return shrunk(before.memory, after.memory) || shrunk(before.history, after.history) ? `render_config:budget_shrink(${cached}→${current})` : null;
84362
+ }
84363
+ var COMPARTMENT_RENDER_EPOCH = "cre2", MEMORY_RENDER_FORMAT_EPOCH = "mre3", EPOCH_COMPONENT_PREFIX = "|compartment-render:", MEMORY_EPOCH_COMPONENT_PREFIX = "|memory-render:", MURAL_COMPONENT_PREFIX = "|mural-enabled:", BUDGET_COMPONENT_PREFIX = "|render-budgets:", RENDERED_BUDGETS_COMPONENT_PREFIX = "|rendered-budgets:";
84012
84364
 
84013
84365
  // src/hooks/magic-context/decay-curve.ts
84014
84366
  function tier(compartmentIndex, importance, budgetPressure) {
@@ -84708,8 +85060,8 @@ function lastCompartmentBoundaryId(compartments) {
84708
85060
  const last = compartments.at(-1);
84709
85061
  return last?.endMessageId && last.endMessageId.length > 0 ? last.endMessageId : null;
84710
85062
  }
84711
- function renderBudgetIdentity(memoryBudget, historyBudget) {
84712
- return `m${memoryBudget ?? DEFAULT_MEMORY_BUDGET_TOKENS}-h${historyBudget ?? DEFAULT_HISTORY_BUDGET_TOKENS}`;
85063
+ function renderBudgetIdentity(memoryBudget, historyBudget, historyPolicy) {
85064
+ return `m${memoryBudget ?? DEFAULT_MEMORY_BUDGET_TOKENS}-h${historyPolicy ?? historyBudget ?? DEFAULT_HISTORY_BUDGET_TOKENS}`;
84713
85065
  }
84714
85066
  function readProjectDocsForM0(projectDirectory, injectDocs) {
84715
85067
  return projectDirectory && injectDocs !== false ? readProjectDocsCanonical(projectDirectory) : EMPTY_PROJECT_DOCS;
@@ -84889,7 +85241,8 @@ function readCurrentM0SnapshotMarkersUncached(args) {
84889
85241
  modelKey: piModelRefToCanonical(hard.modelKey),
84890
85242
  projectIdentity: args.projectPath ?? null,
84891
85243
  muralEnabled: args.memoryEnabled !== false && args.muralEnabled === true,
84892
- renderBudgetIdentity: renderBudgetIdentity(args.memoryInjectionBudgetTokens, args.historyBudgetTokens)
85244
+ renderBudgetIdentity: renderBudgetIdentity(args.memoryInjectionBudgetTokens, args.historyBudgetTokens, args.historyBudgetPolicyIdentity),
85245
+ renderedBudgets: renderedBudgetSnapshot(args.memoryInjectionBudgetTokens ?? DEFAULT_MEMORY_BUDGET_TOKENS, args.historyBudgetTokens ?? DEFAULT_HISTORY_BUDGET_TOKENS)
84893
85246
  }
84894
85247
  };
84895
85248
  }
@@ -84903,7 +85256,8 @@ function refreshVolatileMarkerInputs(markers, args) {
84903
85256
  modelKey: hard.modelKey,
84904
85257
  projectIdentity: args.projectPath ?? null,
84905
85258
  muralEnabled: args.memoryEnabled !== false && args.muralEnabled === true,
84906
- renderBudgetIdentity: renderBudgetIdentity(args.memoryInjectionBudgetTokens, args.historyBudgetTokens)
85259
+ renderBudgetIdentity: renderBudgetIdentity(args.memoryInjectionBudgetTokens, args.historyBudgetTokens, args.historyBudgetPolicyIdentity),
85260
+ renderedBudgets: renderedBudgetSnapshot(args.memoryInjectionBudgetTokens ?? DEFAULT_MEMORY_BUDGET_TOKENS, args.historyBudgetTokens ?? DEFAULT_HISTORY_BUDGET_TOKENS)
84907
85261
  };
84908
85262
  }
84909
85263
  function readCurrentM0SnapshotMarkers(args) {
@@ -84973,7 +85327,8 @@ function snapshotMarkersFromCachedM0(state) {
84973
85327
  projectIdentity: state.cachedM0ProjectIdentity ?? null,
84974
85328
  muralHash: state.cachedM0MuralHash ?? null,
84975
85329
  muralEnabled: cachedUpgradeIdentity.muralEnabled,
84976
- renderBudgetIdentity: cachedUpgradeIdentity.renderBudgetIdentity
85330
+ renderBudgetIdentity: cachedUpgradeIdentity.renderBudgetIdentity,
85331
+ renderedBudgets: cachedUpgradeIdentity.renderedBudgets
84977
85332
  };
84978
85333
  }
84979
85334
  function cachedMemoryDerivedSurfacePresent(state) {
@@ -84988,14 +85343,26 @@ function mustMaterialize(args) {
84988
85343
  const current = readCurrentM0SnapshotMarkers(args);
84989
85344
  const cachedUpgradeIdentity = decodeCachedM0UpgradeIdentity(args.state.cachedM0UpgradeState);
84990
85345
  if (args.memoryEnabled === false && cachedMemoryDerivedSurfacePresent(args.state)) {
84991
- return { value: true, reason: "render_config" };
85346
+ return { value: true, reason: "render_config:memory_disabled" };
84992
85347
  }
84993
85348
  if (cachedUpgradeIdentity.compartmentRenderEpoch !== current.compartmentRenderEpoch) {
84994
85349
  return { value: true, reason: "compartment_render_epoch" };
84995
85350
  }
84996
- if (cachedUpgradeIdentity.muralEnabled !== null && cachedUpgradeIdentity.muralEnabled !== current.muralEnabled || cachedUpgradeIdentity.renderBudgetIdentity !== null && cachedUpgradeIdentity.renderBudgetIdentity !== current.renderBudgetIdentity) {
84997
- return { value: true, reason: "render_config" };
85351
+ if (cachedUpgradeIdentity.muralEnabled !== null && cachedUpgradeIdentity.muralEnabled !== current.muralEnabled) {
85352
+ return {
85353
+ value: true,
85354
+ reason: `render_config:mural(${cachedUpgradeIdentity.muralEnabled}→${current.muralEnabled})`
85355
+ };
85356
+ }
85357
+ if (cachedUpgradeIdentity.renderBudgetIdentity !== null && current.renderBudgetIdentity != null && renderBudgetIdentityChanged(cachedUpgradeIdentity.renderBudgetIdentity, current.renderBudgetIdentity)) {
85358
+ return {
85359
+ value: true,
85360
+ reason: `render_config:budget(${cachedUpgradeIdentity.renderBudgetIdentity}→${current.renderBudgetIdentity})`
85361
+ };
84998
85362
  }
85363
+ const budgetShrinkReason = renderedBudgetShrinkReason(cachedUpgradeIdentity.renderedBudgets, current.renderedBudgets);
85364
+ if (budgetShrinkReason)
85365
+ return { value: true, reason: budgetShrinkReason };
84999
85366
  const canonicalHardModelKey = piModelRefToCanonical(hard.modelKey);
85000
85367
  const canonicalCachedModelKey = piModelRefToCanonical(args.state.cachedM0ModelKey ?? "");
85001
85368
  if (canonicalHardModelKey !== "" && canonicalHardModelKey !== canonicalCachedModelKey) {
@@ -85348,7 +85715,7 @@ function applyMarkersToState(state, m0Bytes, markers, m1Bytes) {
85348
85715
  state.cachedM0ProjectDocsHash = markers.projectDocsHash;
85349
85716
  state.cachedM0MaterializedAt = markers.materializedAt;
85350
85717
  state.cachedM0SessionFactsVersion = markers.sessionFactsVersion;
85351
- state.cachedM0UpgradeState = encodeCachedM0UpgradeIdentity(markers.upgradeState, markers.compartmentRenderEpoch, markers.muralEnabled, markers.renderBudgetIdentity, markers.memoryRenderEpoch);
85718
+ state.cachedM0UpgradeState = encodeCachedM0UpgradeIdentity(markers.upgradeState, markers.compartmentRenderEpoch, markers.muralEnabled, markers.renderBudgetIdentity, markers.memoryRenderEpoch, markers.renderedBudgets ?? null);
85352
85719
  state.cachedM0SystemHash = markers.systemHash;
85353
85720
  state.cachedM0ToolSetHash = markers.toolSetHash;
85354
85721
  state.cachedM0ModelKey = markers.modelKey;
@@ -85400,6 +85767,7 @@ function materializeM0(options) {
85400
85767
  muralEnabled: options.muralEnabled,
85401
85768
  memoryInjectionBudgetTokens: options.memoryInjectionBudgetTokens,
85402
85769
  historyBudgetTokens: options.historyBudgetTokens,
85770
+ historyBudgetPolicyIdentity: options.historyBudgetPolicyIdentity,
85403
85771
  hardSignals: options.hardSignals,
85404
85772
  workspaceIdentitySet: {
85405
85773
  identities: workspace.identities,
@@ -85500,7 +85868,8 @@ function materializeM0(options) {
85500
85868
  modelKey: snapshotMarkers.modelKey,
85501
85869
  projectIdentity: projectPath ?? null,
85502
85870
  muralEnabled: snapshotMarkers.muralEnabled,
85503
- renderBudgetIdentity: snapshotMarkers.renderBudgetIdentity
85871
+ renderBudgetIdentity: snapshotMarkers.renderBudgetIdentity,
85872
+ renderedBudgets: snapshotMarkers.renderedBudgets
85504
85873
  };
85505
85874
  const memoryEpochStale = current.workspaceFingerprint !== null || snapshotMarkers.workspaceFingerprint !== null ? current.workspaceFingerprint !== snapshotMarkers.workspaceFingerprint : current.projectMemoryEpoch !== snapshotMarkers.projectMemoryEpoch;
85506
85875
  const stale = memoryEpochStale || current.projectUserProfileVersion !== snapshotMarkers.projectUserProfileVersion || current.maxCompartmentSeq !== snapshotMarkers.maxCompartmentSeq || current.maxMutationId !== snapshotMarkers.maxMutationId || current.maxMemoryMutationId !== snapshotMarkers.maxMemoryMutationId || current.sessionFactsVersion !== snapshotMarkers.sessionFactsVersion || current.upgradeState !== snapshotMarkers.upgradeState || (current.projectIdentity ?? null) !== (snapshotMarkers.projectIdentity ?? null);
@@ -85535,7 +85904,7 @@ function materializeM0(options) {
85535
85904
  projectDocsHash: snapshotMarkers.projectDocsHash,
85536
85905
  materializedAt: snapshotMarkers.materializedAt,
85537
85906
  sessionFactsVersion: snapshotMarkers.sessionFactsVersion,
85538
- upgradeState: encodeCachedM0UpgradeIdentity(snapshotMarkers.upgradeState, snapshotMarkers.compartmentRenderEpoch, snapshotMarkers.muralEnabled, snapshotMarkers.renderBudgetIdentity, snapshotMarkers.memoryRenderEpoch),
85907
+ upgradeState: encodeCachedM0UpgradeIdentity(snapshotMarkers.upgradeState, snapshotMarkers.compartmentRenderEpoch, snapshotMarkers.muralEnabled, snapshotMarkers.renderBudgetIdentity, snapshotMarkers.memoryRenderEpoch, snapshotMarkers.renderedBudgets ?? null),
85539
85908
  systemHash: snapshotMarkers.systemHash,
85540
85909
  toolSetHash: snapshotMarkers.toolSetHash,
85541
85910
  modelKey: snapshotMarkers.modelKey,
@@ -85798,7 +86167,8 @@ function markersFromCachedRow(row) {
85798
86167
  projectIdentity: row.cached_m0_project_identity ?? null,
85799
86168
  muralHash: row.cached_m0_mural_hash ?? null,
85800
86169
  muralEnabled: cachedUpgradeIdentity.muralEnabled,
85801
- renderBudgetIdentity: cachedUpgradeIdentity.renderBudgetIdentity
86170
+ renderBudgetIdentity: cachedUpgradeIdentity.renderBudgetIdentity,
86171
+ renderedBudgets: cachedUpgradeIdentity.renderedBudgets
85802
86172
  };
85803
86173
  }
85804
86174
  function cachedRowMatchesState(row, state) {
@@ -85823,7 +86193,7 @@ function applyCachedRowToState(state, row) {
85823
86193
  state.cachedM0ProjectDocsHash = markers.projectDocsHash;
85824
86194
  state.cachedM0MaterializedAt = markers.materializedAt;
85825
86195
  state.cachedM0SessionFactsVersion = markers.sessionFactsVersion;
85826
- state.cachedM0UpgradeState = encodeCachedM0UpgradeIdentity(markers.upgradeState, markers.compartmentRenderEpoch, markers.muralEnabled, markers.renderBudgetIdentity, markers.memoryRenderEpoch);
86196
+ state.cachedM0UpgradeState = encodeCachedM0UpgradeIdentity(markers.upgradeState, markers.compartmentRenderEpoch, markers.muralEnabled, markers.renderBudgetIdentity, markers.memoryRenderEpoch, markers.renderedBudgets ?? null);
85827
86197
  state.cachedM0SystemHash = markers.systemHash;
85828
86198
  state.cachedM0ToolSetHash = markers.toolSetHash;
85829
86199
  state.cachedM0ModelKey = markers.modelKey;
@@ -85916,6 +86286,7 @@ function renderFreshM0NonPersisted(options) {
85916
86286
  muralEnabled: options.muralEnabled,
85917
86287
  memoryInjectionBudgetTokens: options.memoryInjectionBudgetTokens,
85918
86288
  historyBudgetTokens: options.historyBudgetTokens,
86289
+ historyBudgetPolicyIdentity: options.historyBudgetPolicyIdentity,
85919
86290
  hardSignals: options.hardSignals,
85920
86291
  workspaceIdentitySet: {
85921
86292
  identities: workspace.identities,
@@ -86282,7 +86653,8 @@ function injectM0M1(options) {
86282
86653
  memoryEnabled: options.memoryEnabled,
86283
86654
  muralEnabled: options.muralEnabled,
86284
86655
  memoryInjectionBudgetTokens: options.memoryInjectionBudgetTokens,
86285
- historyBudgetTokens: options.historyBudgetTokens
86656
+ historyBudgetTokens: options.historyBudgetTokens,
86657
+ historyBudgetPolicyIdentity: options.historyBudgetPolicyIdentity
86286
86658
  });
86287
86659
  let rematerialized = false;
86288
86660
  let contentionExhausted = false;
@@ -89179,6 +89551,8 @@ function findDanglingPublicationBoundary(sessionId, compartments, messageExists
89179
89551
  return null;
89180
89552
  }
89181
89553
  async function runCompartmentAgent(deps) {
89554
+ if (shouldSkipHomeProjectMemory(deps.directory ?? process.cwd()))
89555
+ return;
89182
89556
  const {
89183
89557
  client,
89184
89558
  db,
@@ -89409,11 +89783,14 @@ async function runCompartmentAgent(deps) {
89409
89783
  }
89410
89784
  const producerModel = toModelEntry(deps.model)?.model ?? deps.fallbackModelId;
89411
89785
  const modelParts = producerModel?.split("/");
89412
- const producerReserve = historianProducerReserve(deps.historianContextLimit, deps.historianMaxOutputTokens, modelParts && modelParts.length > 1 ? getSdkOutputLimit(modelParts[0], modelParts.slice(1).join("/")) : undefined);
89786
+ const producerLimits = resolveHistorianProducerLimits(producerModel);
89787
+ const producerContext = producerLimits.context ?? (producerLimits.input === undefined ? deps.historianContextLimit : undefined);
89788
+ const producerReserve = historianProducerReserve(producerContext, deps.historianMaxOutputTokens, modelParts && modelParts.length > 1 ? getSdkOutputLimit(modelParts[0], modelParts.slice(1).join("/")) : undefined);
89413
89789
  const fittedAtomicSource = chunk.oversizeAtomicUnit ? fitAtomicHistorianSourceToProducerWindow({
89414
89790
  text: chunk.text,
89415
89791
  resultBoundaries: chunk.toolResultBoundaries,
89416
- contextLimitTokens: deps.historianContextLimit,
89792
+ contextLimitTokens: producerContext,
89793
+ inputLimitTokens: producerLimits.input,
89417
89794
  maxOutputTokens: producerReserve
89418
89795
  }) : null;
89419
89796
  const chunkText = chunk.oversizeAtomicUnit ? fittedAtomicSource?.text ?? chunk.text : truncateHistorianInputIfNeeded(chunk.text, historianChunkTokens);
@@ -89426,10 +89803,11 @@ async function runCompartmentAgent(deps) {
89426
89803
  }
89427
89804
  const producerWindowFailure = producerWindowFailureReason({
89428
89805
  producerSourceTokens,
89429
- contextLimitTokens: deps.historianContextLimit,
89806
+ contextLimitTokens: producerContext,
89807
+ inputLimitTokens: producerLimits.input,
89430
89808
  maxOutputTokens: producerReserve
89431
89809
  });
89432
- if (deps.historianContextLimit !== undefined && producerInputTokenLimit(deps.historianContextLimit, producerReserve) === undefined) {
89810
+ if (deps.historianContextLimit !== undefined && producerInputTokenLimit(producerContext, producerReserve, producerLimits.input) === undefined) {
89433
89811
  logInconsistentProducerWindowOnce(sessionId, producerModel ?? "unknown", deps.historianContextLimit, producerReserve);
89434
89812
  }
89435
89813
  if (producerWindowFailure) {
@@ -89589,7 +89967,7 @@ ${chunkText}`,
89589
89967
  const compartmentTagKeys = await getRawSessionTagKeysThrough(sessionId, lastCompartmentEnd, { db, fromMessageIndex: offset });
89590
89968
  let published = false;
89591
89969
  const transactionStartedAt = startHistorianPublishStage(sessionId, "publish-txn");
89592
- db.exec("BEGIN IMMEDIATE");
89970
+ await beginSqliteWriterAsync(db, "historian-publish");
89593
89971
  try {
89594
89972
  if (!isCompartmentLeaseHeld(db, sessionId, holderId)) {
89595
89973
  db.exec("ROLLBACK");
@@ -89813,6 +90191,7 @@ var init_compartment_runner_incremental = __esm(async () => {
89813
90191
  init_reference_retrieval();
89814
90192
  await __promiseAll([
89815
90193
  init_store_generation_rebase(),
90194
+ init_sqlite(),
89816
90195
  init_memory(),
89817
90196
  init_storage_memory(),
89818
90197
  init_storage(),
@@ -106406,6 +106785,12 @@ function resolveModelKey(providerID, modelID) {
106406
106785
  }
106407
106786
  return piModelRefToCanonical(`${providerID}/${modelID}`);
106408
106787
  }
106788
+ function historyBudgetPolicyIdentity(historyBudgetPercentage, executeThresholdPercentage, modelKey, executeThresholdTokens) {
106789
+ if (!historyBudgetPercentage)
106790
+ return "pdefault";
106791
+ const threshold = resolveExecuteThresholdDetail(executeThresholdPercentage ?? 65, modelKey, 65, { tokensConfig: executeThresholdTokens, contextLimit: Number.MAX_SAFE_INTEGER });
106792
+ return `p${historyBudgetPercentage}:${threshold.mode}:${threshold.absoluteTokens ?? threshold.percentage}`;
106793
+ }
106409
106794
 
106410
106795
  // src/features/magic-context/scheduler.ts
106411
106796
  init_logger();
@@ -107100,28 +107485,6 @@ class EmergencyFailClosedError extends Error {
107100
107485
  this.name = "EmergencyFailClosedError";
107101
107486
  }
107102
107487
  }
107103
- // src/hooks/magic-context/event-payloads.ts
107104
- function nonEmptyString(value) {
107105
- return typeof value === "string" && value.length > 0 ? value : undefined;
107106
- }
107107
- function getSessionErrorInfo(properties) {
107108
- if (!isRecord3(properties))
107109
- return null;
107110
- const sessionID = properties.sessionID;
107111
- if (typeof sessionID !== "string" || sessionID.length === 0)
107112
- return null;
107113
- const error = isRecord3(properties.error) ? properties.error : undefined;
107114
- const data = error && isRecord3(error.data) ? error.data : undefined;
107115
- const model = isRecord3(properties.model) ? properties.model : error && isRecord3(error.model) ? error.model : data && isRecord3(data.model) ? data.model : undefined;
107116
- const providerID = nonEmptyString(properties.providerID) ?? nonEmptyString(model?.providerID) ?? nonEmptyString(model?.provider) ?? nonEmptyString(error?.providerID) ?? nonEmptyString(data?.providerID);
107117
- const modelID = nonEmptyString(properties.modelID) ?? nonEmptyString(model?.modelID) ?? nonEmptyString(model?.id) ?? nonEmptyString(error?.modelID) ?? nonEmptyString(data?.modelID);
107118
- return {
107119
- sessionID,
107120
- error: properties.error,
107121
- ...providerID ? { providerID } : {},
107122
- ...modelID ? { modelID } : {}
107123
- };
107124
- }
107125
107488
 
107126
107489
  // src/features/magic-context/dreamer/verify-read-cap.ts
107127
107490
  var DREAMER_READ_MAX_CHARS = 8000;
@@ -107137,7 +107500,10 @@ function capDreamerReadOutput(input, output) {
107137
107500
 
107138
107501
  // src/features/magic-context/message-index-async.ts
107139
107502
  init_logger();
107140
- await init_message_index();
107503
+ await __promiseAll([
107504
+ init_sqlite(),
107505
+ init_message_index()
107506
+ ]);
107141
107507
  function isDatabaseLockedError(error) {
107142
107508
  if (!error || typeof error !== "object")
107143
107509
  return false;
@@ -107223,7 +107589,7 @@ async function reconcileSessionIndex(db, sessionId, readMessages) {
107223
107589
  const messages = readMessages.readPage ? readMessages.readPage(sessionId, cursor, RECONCILIATION_BATCH_SIZE, finalWatermark) : (fallbackSnapshot ?? []).filter((message) => message.ordinal > cursor && message.ordinal <= pageEnd);
107224
107590
  const retainedMessages = fallbackSnapshot ?? messages;
107225
107591
  heapHolder.activeReconcilerBuffers.set(sessionId, retainedMessages);
107226
- indexMessagesAfterOrdinal(db, sessionId, messages, cursor, pageEnd);
107592
+ withSqliteBackgroundWriter(() => indexMessagesAfterOrdinal(db, sessionId, messages, cursor, pageEnd));
107227
107593
  const nextCursor = getMessageIndexReconciliationStartOrdinal(db, sessionId);
107228
107594
  if (nextCursor <= cursor)
107229
107595
  break;
@@ -107249,6 +107615,8 @@ function scheduleReconciliation(db, sessionId, readMessages) {
107249
107615
  defer(() => {
107250
107616
  reconcileSessionIndex(db, sessionId, readMessages).catch((error) => {
107251
107617
  logIndexingError(sessionId, "reconciliation", error);
107618
+ if (isDatabaseLockedError(error))
107619
+ setTimeout(() => scheduleReconciliation(db, sessionId, readMessages), 1000).unref();
107252
107620
  }).finally(() => {
107253
107621
  heapHolder.reconciliationScheduledSessions.delete(sessionId);
107254
107622
  });
@@ -109675,6 +110043,28 @@ function annotateEmptyTaskOutput(tool, output) {
109675
110043
  ${EMPTY_TASK_OUTPUT_SENTINEL}
109676
110044
  The subagent completed without a final text response. Context-fill truncation may have omitted its final output, or its provider may have emitted reasoning only; inspect the child session and retry with a low-reasoning model or variant.`);
109677
110045
  }
110046
+ // src/hooks/magic-context/event-payloads.ts
110047
+ function nonEmptyString(value) {
110048
+ return typeof value === "string" && value.length > 0 ? value : undefined;
110049
+ }
110050
+ function getSessionErrorInfo(properties) {
110051
+ if (!isRecord3(properties))
110052
+ return null;
110053
+ const sessionID = properties.sessionID;
110054
+ if (typeof sessionID !== "string" || sessionID.length === 0)
110055
+ return null;
110056
+ const error = isRecord3(properties.error) ? properties.error : undefined;
110057
+ const data = error && isRecord3(error.data) ? error.data : undefined;
110058
+ const model = isRecord3(properties.model) ? properties.model : error && isRecord3(error.model) ? error.model : data && isRecord3(data.model) ? data.model : undefined;
110059
+ const providerID = nonEmptyString(properties.providerID) ?? nonEmptyString(model?.providerID) ?? nonEmptyString(model?.provider) ?? nonEmptyString(error?.providerID) ?? nonEmptyString(data?.providerID);
110060
+ const modelID = nonEmptyString(properties.modelID) ?? nonEmptyString(model?.modelID) ?? nonEmptyString(model?.id) ?? nonEmptyString(error?.modelID) ?? nonEmptyString(data?.modelID);
110061
+ return {
110062
+ sessionID,
110063
+ error: properties.error,
110064
+ ...providerID ? { providerID } : {},
110065
+ ...modelID ? { modelID } : {}
110066
+ };
110067
+ }
109678
110068
 
109679
110069
  // src/hooks/magic-context/hook-handlers.ts
109680
110070
  init_lkg_slot();
@@ -111976,10 +112366,12 @@ init_project_embedding_registry();
111976
112366
 
111977
112367
  // src/plugin/embedding-bootstrap.ts
111978
112368
  init_config();
112369
+ init_migrate_config_location();
111979
112370
  init_embedding();
111980
112371
  init_embedding_cache();
111981
112372
  init_project_identity();
111982
112373
  init_logger();
112374
+ import { readFileSync as readFileSync14, statSync as statSync13 } from "node:fs";
111983
112375
 
111984
112376
  // src/plugin/embedding-bootstrap-helpers.ts
111985
112377
  init_embedding();
@@ -112230,12 +112622,41 @@ async function resolveEmbeddingRouting(args) {
112230
112622
  }
112231
112623
 
112232
112624
  // src/plugin/embedding-bootstrap.ts
112233
- async function ensureProjectRegisteredFromOpenCodeDirectory(directory, db) {
112625
+ var configCache = new Map;
112626
+ function loadRegistrationConfig(directory) {
112627
+ const legacy = resolveLegacyConfigSources(directory);
112628
+ const paths = [cortexKitUserConfigBasePath(), cortexKitProjectConfigBasePath(directory)].flatMap((base) => [`${base}.jsonc`, `${base}.json`]).concat(legacy.user.map((source) => source.path), legacy.project.map((source) => source.path));
112629
+ const key = paths.map((path) => {
112630
+ const stat = statSync13(path, { throwIfNoEntry: false });
112631
+ return `${path}:${stat?.mtimeMs ?? "missing"}:${stat?.size ?? 0}`;
112632
+ }).join("|");
112633
+ const cached = configCache.get(directory);
112634
+ if (cached?.key === key)
112635
+ return cached.detailed;
112234
112636
  const detailed = loadPluginConfigDetailed(directory);
112637
+ const dynamic = paths.some((path) => {
112638
+ try {
112639
+ return /\{(?:env|file):/.test(readFileSync14(path, "utf8"));
112640
+ } catch {
112641
+ return false;
112642
+ }
112643
+ });
112644
+ if (!dynamic) {
112645
+ if (configCache.size >= 64) {
112646
+ const oldest = configCache.keys().next().value;
112647
+ if (oldest !== undefined)
112648
+ configCache.delete(oldest);
112649
+ }
112650
+ configCache.set(directory, { key, detailed });
112651
+ } else
112652
+ configCache.delete(directory);
112653
+ return detailed;
112654
+ }
112655
+ async function ensureProjectRegisteredFromOpenCodeDirectory(directory, db) {
112656
+ const detailed = loadRegistrationConfig(directory);
112235
112657
  const projectIdentity = resolveProjectIdentityForSession(directory, detailed.config.allow_home_project);
112236
112658
  if (!projectIdentity)
112237
112659
  return;
112238
- invalidateProject(projectIdentity);
112239
112660
  if (isConfigLoadUntrusted(detailed)) {
112240
112661
  handleUntrustedLoad(db, projectIdentity, directory, detailed);
112241
112662
  return;
@@ -112252,7 +112673,10 @@ async function ensureProjectRegisteredFromOpenCodeDirectory(directory, db) {
112252
112673
  memoryEnabled: detailed.config.memory.enabled,
112253
112674
  gitCommitEnabled: detailed.config.memory.git_commit_indexing.enabled
112254
112675
  };
112255
- registerProjectEmbedding(db, projectIdentity, routing.primary, features, directory);
112676
+ const before = getProjectEmbeddingSnapshot(projectIdentity);
112677
+ const registered = registerProjectEmbedding(db, projectIdentity, routing.primary, features, directory);
112678
+ if (!before || before.providerIdentity !== registered.providerIdentity || before.runtimeFingerprint !== registered.runtimeFingerprint)
112679
+ invalidateProject(projectIdentity);
112256
112680
  if (routing.shadow) {
112257
112681
  registerProjectShadowEmbedding(db, projectIdentity, routing.shadow, directory);
112258
112682
  } else {
@@ -113118,7 +113542,9 @@ await __promiseAll([
113118
113542
  init_compaction_marker_manager(),
113119
113543
  init_compartment_runner()
113120
113544
  ]);
113545
+
113121
113546
  // src/hooks/magic-context/drop-stale-reduce-calls.ts
113547
+ init_user_answer();
113122
113548
  var STALE_TOOL_NAMES = new Set(["ctx_reduce"]);
113123
113549
  function isReduceToolPart(part) {
113124
113550
  if (!isRecord3(part))
@@ -113182,7 +113608,7 @@ function dropStaleReduceCalls(messages, frozenIds, options = {}) {
113182
113608
  const message = messages[i];
113183
113609
  const id = typeof message.info.id === "string" ? message.info.id : undefined;
113184
113610
  const inFrozen = id !== undefined && frozenIds.has(id);
113185
- const isNewDetection = !inFrozen && detect && i < protectedStart && id !== undefined && messageHasReducePart(message);
113611
+ const isNewDetection = !inFrozen && detect && i < protectedStart && id !== undefined && messageHasReducePart(message) && !message.parts.some(toolPartHasUserAnswer);
113186
113612
  if (!inFrozen && !isNewDetection)
113187
113613
  continue;
113188
113614
  const touched = sentinelizeReduceParts(message);
@@ -113600,6 +114026,8 @@ function applyHeuristicCleanup(sessionId, db, targets, messageTagNumbers, config
113600
114026
  for (let i = 0;i < group.length - 1; i++) {
113601
114027
  const tag = group[i];
113602
114028
  const target = targets.get(tag.tagNumber);
114029
+ if (target?.canDrop?.() === false)
114030
+ continue;
113603
114031
  const { result, mode } = applyNewToolDrop(target, { inWindow: false });
113604
114032
  if (result === "incomplete")
113605
114033
  continue;
@@ -115771,6 +116199,7 @@ async function runPostTransformPhase(args) {
115771
116199
  muralEnabled: args.m0M1.muralEnabled,
115772
116200
  memoryInjectionBudgetTokens: args.m0M1.memoryInjectionBudgetTokens,
115773
116201
  historyBudgetTokens: args.m0M1.historyBudgetTokens,
116202
+ historyBudgetPolicyIdentity: args.m0M1.historyBudgetPolicyIdentity,
115774
116203
  hardSignals: args.m0M1.hardSignals
115775
116204
  }) : { value: false, reason: null };
115776
116205
  let preparedPrefix;
@@ -115809,6 +116238,7 @@ async function runPostTransformPhase(args) {
115809
116238
  memoryEnabled: args.m0M1.memoryEnabled,
115810
116239
  memoryInjectionBudgetTokens: args.m0M1.memoryInjectionBudgetTokens,
115811
116240
  historyBudgetTokens: args.m0M1.historyBudgetTokens,
116241
+ historyBudgetPolicyIdentity: args.m0M1.historyBudgetPolicyIdentity,
115812
116242
  temporalAwareness: args.m0M1.temporalAwareness,
115813
116243
  isCacheBustingPass: true,
115814
116244
  contentionFallbackPrefix: cachedPrefixBeforePreflight,
@@ -116189,6 +116619,7 @@ async function runPostTransformPhase(args) {
116189
116619
  memoryEnabled: args.m0M1.memoryEnabled,
116190
116620
  memoryInjectionBudgetTokens: args.m0M1.memoryInjectionBudgetTokens,
116191
116621
  historyBudgetTokens: args.m0M1.historyBudgetTokens,
116622
+ historyBudgetPolicyIdentity: args.m0M1.historyBudgetPolicyIdentity,
116192
116623
  temporalAwareness: args.m0M1.temporalAwareness,
116193
116624
  isCacheBustingPass,
116194
116625
  preparedPrefix,
@@ -116764,6 +117195,16 @@ function servedModuleM0Text(sessionId, readSlot = getSlot) {
116764
117195
  init_logger();
116765
117196
  await init_sqlite();
116766
117197
  var STORAGE_BUSY_MESSAGE = "Magic Context's database is busy (another process held it too long); send your message again.";
117198
+ function describeStorageBusyCause(cause) {
117199
+ let original = cause;
117200
+ for (let depth = 0;original instanceof SqliteAcquisitionBusyError && depth < 5; depth++) {
117201
+ original = original.cause;
117202
+ }
117203
+ if (!(original instanceof Error))
117204
+ return String(original);
117205
+ return original.stack ? `${original.message}
117206
+ ${original.stack}` : original.message;
117207
+ }
116767
117208
 
116768
117209
  class StorageBusyRefusalError extends Error {
116769
117210
  code = "STORAGE_BUSY_REFUSAL";
@@ -116771,8 +117212,7 @@ class StorageBusyRefusalError extends Error {
116771
117212
  constructor(cause, stage) {
116772
117213
  super(STORAGE_BUSY_MESSAGE, { cause });
116773
117214
  this.name = "StorageBusyRefusalError";
116774
- const original = cause instanceof SqliteAcquisitionBusyError ? cause.cause : cause;
116775
- const detail = original instanceof Error ? original.stack ?? original.message : String(original);
117215
+ const detail = describeStorageBusyCause(cause);
116776
117216
  log(`[magic-context] storage-busy refusal stage=${stage}${cause instanceof SqliteAcquisitionBusyError ? ` acquisition=${cause.stage}` : ""}: ${detail}`);
116777
117217
  }
116778
117218
  }
@@ -117219,7 +117659,24 @@ await init_inject_compartments();
117219
117659
 
117220
117660
  // src/hooks/magic-context/lkg-persist.ts
117221
117661
  init_logger();
117662
+ import { createHash as createHash25 } from "node:crypto";
117663
+ var persistedFingerprints = new WeakMap;
117222
117664
  function saveLkgSlotToDb(db, sessionId, slot) {
117665
+ const fingerprint = createHash25("sha256").update(JSON.stringify([
117666
+ slot.jsonPrefix,
117667
+ slot.inputIdSeq,
117668
+ slot.inputContentDigests,
117669
+ slot.inputContentSignatures,
117670
+ slot.piOutputEntryIds,
117671
+ slot.lastInputMessageId,
117672
+ slot.modelKey,
117673
+ slot.providerKey,
117674
+ slot.rowVersion,
117675
+ slot.captureSequence
117676
+ ])).digest("hex");
117677
+ let saved = persistedFingerprints.get(db);
117678
+ if (saved?.get(sessionId) === fingerprint)
117679
+ return true;
117223
117680
  try {
117224
117681
  db.prepare(`INSERT INTO lkg_slots (
117225
117682
  session_id, json_prefix, input_id_seq, input_content_digests,
@@ -117241,6 +117698,16 @@ function saveLkgSlotToDb(db, sessionId, slot) {
117241
117698
  inputIds: slot.inputIdSeq,
117242
117699
  piOutputEntryIds: slot.piOutputEntryIds
117243
117700
  } : slot.inputIdSeq), JSON.stringify(slot.inputContentDigests), slot.inputContentSignatures ? JSON.stringify(slot.inputContentSignatures) : null, slot.lastInputMessageId, slot.modelKey, slot.providerKey, slot.capturedAt, slot.rowVersion ?? null, slot.captureSequence ?? null);
117701
+ if (!saved) {
117702
+ saved = new Map;
117703
+ persistedFingerprints.set(db, saved);
117704
+ }
117705
+ if (saved.size >= 1000) {
117706
+ const oldest = saved.keys().next().value;
117707
+ if (oldest !== undefined)
117708
+ saved.delete(oldest);
117709
+ }
117710
+ saved.set(sessionId, fingerprint);
117244
117711
  return true;
117245
117712
  } catch (error) {
117246
117713
  sessionLog(sessionId, "LKG snapshot persistence failed (in-memory slot retained):", error);
@@ -117827,7 +118294,7 @@ await __promiseAll([
117827
118294
  // src/hooks/magic-context/rust-mode-transform.ts
117828
118295
  init_magic_context();
117829
118296
  await init_context_authority();
117830
- import { createHash as createHash26, randomUUID as randomUUID5 } from "node:crypto";
118297
+ import { createHash as createHash27, randomUUID as randomUUID5 } from "node:crypto";
117831
118298
  init_project_identity();
117832
118299
 
117833
118300
  // src/features/magic-context/memory/single-store-embedding-drain.ts
@@ -118300,13 +118767,16 @@ class HistorianHostRunner {
118300
118767
  this.log(`producer window unknown for ${key}: sending unguarded`);
118301
118768
  }
118302
118769
  const output = provider && parts.length ? getSdkOutputLimit(provider, parts.join("/")) : undefined;
118303
- const reserve = historianProducerReserve(window2, undefined, output);
118770
+ const producerLimits = resolveHistorianProducerLimits(key);
118771
+ const context = producerLimits.context ?? (producerLimits.input === undefined ? window2 : undefined);
118772
+ const reserve = historianProducerReserve(context, undefined, output);
118304
118773
  const failure = producerPromptFailureReason({
118305
118774
  sourceLocal: estimateTokens(claim.user),
118306
118775
  systemLocal: estimateTokens(claim.system),
118307
118776
  toolsLocal: 0,
118308
118777
  modelKey: key,
118309
- contextLimitTokens: window2,
118778
+ contextLimitTokens: context,
118779
+ inputLimitTokens: producerLimits.input,
118310
118780
  maxOutputTokens: reserve
118311
118781
  });
118312
118782
  if (failure)
@@ -118473,6 +118943,7 @@ function timedStateSyncDatabase(db, timing) {
118473
118943
  }
118474
118944
 
118475
118945
  // src/hooks/magic-context/module-wire.ts
118946
+ init_user_answer();
118476
118947
  init_read_session_raw();
118477
118948
  await init_read_session_chunk();
118478
118949
  import * as crypto2 from "node:crypto";
@@ -119054,6 +119525,7 @@ function encodeOpenCodeMessagesToCk(messages) {
119054
119525
  const synthetic = syntheticParts.length > 0 && syntheticParts.every((part) => part !== null && typeof part === "object" && (part.synthetic === true || part.syntheticTodoMarker === true));
119055
119526
  const content = [];
119056
119527
  const recoveryToolTitles = {};
119528
+ const userAnswerBlocks = [];
119057
119529
  for (const partValue of parts) {
119058
119530
  if (partValue === null || typeof partValue !== "object")
119059
119531
  continue;
@@ -119101,6 +119573,8 @@ function encodeOpenCodeMessagesToCk(messages) {
119101
119573
  if (title)
119102
119574
  recoveryToolTitles[callId] = title;
119103
119575
  const output = typeof state.output === "string" ? state.output : typeof state.error === "string" ? state.error : "";
119576
+ if (toolPartHasUserAnswer(part))
119577
+ userAnswerBlocks.push(content.length);
119104
119578
  content.push({
119105
119579
  kind: {
119106
119580
  type: "tool_result",
@@ -119132,9 +119606,12 @@ function encodeOpenCodeMessagesToCk(messages) {
119132
119606
  ck: {
119133
119607
  role,
119134
119608
  content,
119135
- ...Object.keys(recoveryToolTitles).length > 0 ? {
119609
+ ...Object.keys(recoveryToolTitles).length > 0 || userAnswerBlocks.length > 0 ? {
119136
119610
  provider_extras: {
119137
- opencode: { ctx_expand_tool_titles: recoveryToolTitles }
119611
+ opencode: {
119612
+ ...Object.keys(recoveryToolTitles).length > 0 ? { ctx_expand_tool_titles: recoveryToolTitles } : {},
119613
+ ...userAnswerBlocks.length > 0 ? { user_answer_block_indices: userAnswerBlocks } : {}
119614
+ }
119138
119615
  }
119139
119616
  } : {},
119140
119617
  meta: {
@@ -120748,10 +121225,10 @@ function messageCacheSignature(message) {
120748
121225
  const parts = Array.isArray(message.parts) ? message.parts : [];
120749
121226
  const serializedParts = JSON.stringify(parts) ?? "null";
120750
121227
  const serializedMessage = JSON.stringify(message) ?? "null";
120751
- return `${messageIdOf(message) ?? ""}:${parts.length}:${Buffer.byteLength(serializedParts)}:${createHash26("sha256").update(serializedMessage).digest("hex")}`;
121228
+ return `${messageIdOf(message) ?? ""}:${parts.length}:${Buffer.byteLength(serializedParts)}:${createHash27("sha256").update(serializedMessage).digest("hex")}`;
120752
121229
  }
120753
121230
  function advanceWireFingerprint(previous, encoded) {
120754
- return createHash26("sha256").update(previous).update("\\0").update(JSON.stringify(encoded) ?? "null").digest("hex");
121231
+ return createHash27("sha256").update(previous).update("\\0").update(JSON.stringify(encoded) ?? "null").digest("hex");
120755
121232
  }
120756
121233
  function buildWireFingerprint(encoded) {
120757
121234
  let fingerprint = "rust-wire-v1";
@@ -121107,7 +121584,7 @@ function canonicalizeForChecksum(value) {
121107
121584
  return Object.fromEntries(Object.keys(value).sort().map((key) => [key, canonicalizeForChecksum(value[key])]));
121108
121585
  }
121109
121586
  function checksumSeedRows(rows) {
121110
- return createHash26("sha256").update(JSON.stringify(rows.map(canonicalizeForChecksum))).digest("hex");
121587
+ return createHash27("sha256").update(JSON.stringify(rows.map(canonicalizeForChecksum))).digest("hex");
121111
121588
  }
121112
121589
  function authoritySeedRows(db, projectPath, domain) {
121113
121590
  const snapshots = domain === "memories" ? db.prepare("SELECT * FROM memories WHERE project_path = ? ORDER BY id ASC").all(projectPath) : db.prepare(`SELECT n.*
@@ -121335,13 +121812,15 @@ function resolvedHistorianModelLimits(chain) {
121335
121812
  return Object.fromEntries(chain.map((key) => {
121336
121813
  const [provider, ...parts] = key.split("/");
121337
121814
  const output = provider && parts.length ? getSdkOutputLimit(provider, parts.join("/")) : undefined;
121338
- const known = resolveKnownHistorianContextLimit(key);
121815
+ const producerLimits = resolveHistorianProducerLimits(key);
121816
+ const known = producerLimits.input === undefined ? resolveKnownHistorianContextLimit(key) : undefined;
121339
121817
  const learned = provider && parts.length ? getSdkWindowGeometry(provider, parts.join("/"))?.derivation.window : undefined;
121340
- const context = known === undefined ? learned : learned === undefined ? known : Math.min(known, learned);
121818
+ const context = producerLimits.context ?? (known === undefined ? learned : learned === undefined ? known : Math.min(known, learned));
121341
121819
  return [
121342
121820
  key,
121343
121821
  {
121344
121822
  ...context !== undefined ? { context } : {},
121823
+ ...producerLimits.input !== undefined ? { input: producerLimits.input } : {},
121345
121824
  ...output !== undefined ? { output } : {}
121346
121825
  }
121347
121826
  ];
@@ -122744,6 +123223,16 @@ function createRustModeTransform(deps, options) {
122744
123223
  }
122745
123224
  pendingWireCache.nativeOutput = moduleMessages;
122746
123225
  if (!replayedFrozenRepresentation) {
123226
+ if (materializedBoundary && !deps.compactionOff) {
123227
+ try {
123228
+ await withAsyncPrivilegedWriter(deps.db, () => {
123229
+ return;
123230
+ });
123231
+ } catch (error) {
123232
+ if (!isTransientSqliteError(error))
123233
+ throw error;
123234
+ }
123235
+ }
122747
123236
  const postprocess = runRustModePostprocess({
122748
123237
  db: deps.db,
122749
123238
  sessionId,
@@ -122968,7 +123457,7 @@ function createRustModeTransform(deps, options) {
122968
123457
  if (mirrorDrain.rowsApplied > 0) {
122969
123458
  await reembedMirrorInvalidatedMemories(deps.db);
122970
123459
  }
122971
- await drainSingleStoreEmbeddingWatermarks(deps.db);
123460
+ await withSqliteBackgroundWriter(() => drainSingleStoreEmbeddingWatermarks(deps.db));
122972
123461
  if (mirrorDrain.complete) {
122973
123462
  state.memoryMirrorProjectionKey = projectionKey;
122974
123463
  } else if (mirrorDrain.budgetExhausted) {
@@ -123925,7 +124414,9 @@ function createTransform(deps) {
123925
124414
  const persistedUsageFreshForBoundary = persistedUsageBeforeResets && Date.now() - persistedUsageBeforeResets.updatedAt <= 10 * 60 * 1000 && (persistedUsageBeforeResets.lastObservedModelKey === null || currentModelKeyForBoundary === undefined || piModelRefToCanonical(persistedUsageBeforeResets.lastObservedModelKey) === piModelRefToCanonical(currentModelKeyForBoundary)) && (resolvedContextLimit === undefined || persistedUsageBeforeResets.lastUsageContextLimit === 0 || persistedUsageBeforeResets.lastUsageContextLimit === resolvedContextLimit) ? persistedUsageBeforeResets.usage : null;
123926
124415
  const boundaryUsageForProtectedTail = persistedUsageFreshForBoundary ?? contextUsageEarly;
123927
124416
  const boundaryUsageSource = persistedUsageFreshForBoundary ? "persisted" : "live";
123928
- const historyBudgetTokens = resolveHistoryBudgetTokens(deps.historyBudgetPercentage, contextUsageEarly, deps.executeThresholdPercentage, deps.getModelKey?.(sessionId), deps.executeThresholdTokens, resolvedContextLimit);
124417
+ const historyPolicyIdentity = historyBudgetPolicyIdentity(deps.historyBudgetPercentage, deps.executeThresholdPercentage, currentModelKeyForBoundary, deps.executeThresholdTokens);
124418
+ const historyContextLimit = resolvedContextLimit ?? (currentModelKeyForBoundary && persistedUsageBeforeResets?.lastObservedModelKey && piModelRefToCanonical(currentModelKeyForBoundary) === piModelRefToCanonical(persistedUsageBeforeResets.lastObservedModelKey) && persistedUsageBeforeResets.lastUsageContextLimit > 0 ? persistedUsageBeforeResets.lastUsageContextLimit : undefined);
124419
+ const historyBudgetTokens = resolveHistoryBudgetTokens(deps.historyBudgetPercentage, contextUsageEarly, deps.executeThresholdPercentage, deps.getModelKey?.(sessionId), deps.executeThresholdTokens, historyContextLimit);
123929
124420
  const emergencyCeilingLimit = thresholdContextLimit ?? 0;
123930
124421
  const emergencyCeilingTokens = Number.isFinite(emergencyCeilingLimit) && emergencyCeilingLimit > 0 ? Math.floor(emergencyCeilingLimit * (effectiveExecuteThresholdPercentage / 100)) : undefined;
123931
124422
  const schedulerDecision = compactionOff ? "defer" : resolveSchedulerDecision(deps.scheduler, sessionMeta, contextUsageEarly, sessionId, deps.getModelKey?.(sessionId), resolvedContextLimit);
@@ -124299,6 +124790,7 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
124299
124790
  muralEnabled: deps.muralEnabled,
124300
124791
  memoryInjectionBudgetTokens: deps.memoryConfig?.injectionBudgetTokens,
124301
124792
  historyBudgetTokens,
124793
+ historyBudgetPolicyIdentity: historyPolicyIdentity,
124302
124794
  hardSignals: m0HardSignals
124303
124795
  }).value;
124304
124796
  const protectionCacheBustingPass = !compactionOff && (schedulerDecision === "execute" || isCacheBusting || contextUsage.percentage >= forceMaterializationPercentage || deps.pendingMaterializationSessions.has(sessionId) || canConsumeDeferredLate && deferredMaterializationSessions.has(sessionId) || protectionFoldWillBust);
@@ -124422,6 +124914,7 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
124422
124914
  memoryEnabled: deps.memoryConfig?.enabled,
124423
124915
  memoryInjectionBudgetTokens: deps.memoryConfig?.injectionBudgetTokens,
124424
124916
  historyBudgetTokens,
124917
+ historyBudgetPolicyIdentity: historyPolicyIdentity,
124425
124918
  temporalAwareness: deps.experimentalTemporalAwareness,
124426
124919
  hardSignals: m0HardSignals,
124427
124920
  muralEnabled: deps.muralEnabled
@@ -124838,6 +125331,13 @@ function createMessagesTransformHandler(args) {
124838
125331
  }
124839
125332
  })() : null;
124840
125333
  try {
125334
+ if (!args.compactionOff && magicContext) {
125335
+ const admissionDb = openDatabase();
125336
+ if (admissionDb)
125337
+ await withAsyncPrivilegedWriter(admissionDb, () => {
125338
+ return;
125339
+ });
125340
+ }
124841
125341
  await magicContext?.["experimental.chat.messages.transform"]?.(input, output);
124842
125342
  return output.messages;
124843
125343
  } catch (error) {
@@ -125886,7 +126386,9 @@ function resolveConfigValue(cfg, key, modelKey, defaultValue) {
125886
126386
  }
125887
126387
  function buildSidebarSnapshot(db, sessionId, directory, liveSessionState, injectionBudgetTokens, config, moduleStatus, compactionEnabled = true) {
125888
126388
  try {
125889
- const projectIdentity = resolveProjectIdentity(directory);
126389
+ const projectIdentity = resolveProjectIdentityForSession(directory);
126390
+ if (projectIdentity === undefined)
126391
+ throw new ProjectIdentityError("git_identity_unavailable", directory, "Memory features paused while project identity is unavailable");
125890
126392
  const meta = db.prepare("SELECT * FROM session_meta WHERE session_id = ?").get(sessionId);
125891
126393
  const usagePercentage = meta ? Number(meta.last_context_percentage ?? meta.last_usage_percentage ?? 0) : 0;
125892
126394
  const inputTokens = meta ? Number(meta.last_input_tokens ?? 0) : 0;
@@ -126090,11 +126592,16 @@ function buildSidebarSnapshot(db, sessionId, directory, liveSessionState, inject
126090
126592
  };
126091
126593
  return applyStickySnapshotCache(sessionId, fresh);
126092
126594
  } catch (err) {
126093
- log("[rpc] sidebar-snapshot error:", err);
126595
+ if (!(err instanceof ProjectIdentityError))
126596
+ log("[rpc] sidebar-snapshot error:", err);
126094
126597
  throw err;
126095
126598
  }
126096
126599
  }
126097
126600
  function buildSidebarSnapshotRpcResponse(db, sessionId, directory, liveSessionState, injectionBudgetTokens, config, moduleStatus, compactionEnabled = true) {
126601
+ if (shouldSkipHomeProjectMemory(directory))
126602
+ return { sessionId, disabled: true };
126603
+ if (resolveProjectIdentityForSession(directory) === undefined)
126604
+ return { sessionId, disabled: true, paused: true };
126098
126605
  try {
126099
126606
  return buildSidebarSnapshot(db, sessionId, directory, liveSessionState, injectionBudgetTokens, config, moduleStatus, compactionEnabled);
126100
126607
  } catch {
@@ -126111,7 +126618,7 @@ function safeTickFailure(db) {
126111
126618
  function buildStatusDetail(db, sessionId, directory, modelKey, config, liveSessionState, injectionBudgetTokens, moduleStatus, compactionEnabled = true) {
126112
126619
  const base = buildSidebarSnapshot(db, sessionId, directory, liveSessionState, injectionBudgetTokens, config, moduleStatus, compactionEnabled);
126113
126620
  const rustMode = config?.transform_mode === "rust";
126114
- const projectIdentity = rustMode ? resolveProjectIdentity(directory) : null;
126621
+ const projectIdentity = rustMode && !shouldSkipHomeProjectMemory(directory) ? resolveProjectIdentity(directory) : null;
126115
126622
  const moduleMemoryAuthority = moduleStatus?.authority?.memories;
126116
126623
  const moduleMemoryState = moduleMemoryAuthority?.state;
126117
126624
  const moduleFeedHead = moduleStatus?.memory_mirror?.feed_head;
@@ -126554,6 +127061,10 @@ function registerRpcHandlers(rpcServer, args) {
126554
127061
  rpcServer.handle("status-detail", async (params) => {
126555
127062
  const sessionId = String(params.sessionId ?? "");
126556
127063
  const dir = String(params.directory ?? directory);
127064
+ if (shouldSkipHomeProjectMemory(dir))
127065
+ return { sessionId, disabled: true };
127066
+ if (resolveProjectIdentityForSession(dir) === undefined)
127067
+ return { sessionId, disabled: true, paused: true };
126557
127068
  const modelKey = params.modelKey ? String(params.modelKey) : undefined;
126558
127069
  const db = readDatabase();
126559
127070
  if (!db || !sessionId)
@@ -126574,6 +127085,10 @@ function registerRpcHandlers(rpcServer, args) {
126574
127085
  rpcServer.handle("embed-detail", async (params) => {
126575
127086
  const sessionId = String(params.sessionId ?? "");
126576
127087
  const dir = String(params.directory ?? directory);
127088
+ if (shouldSkipHomeProjectMemory(dir))
127089
+ return { sessionId, disabled: true };
127090
+ if (resolveProjectIdentityForSession(dir) === undefined)
127091
+ return { sessionId, disabled: true, paused: true };
126577
127092
  const db = readDatabase();
126578
127093
  if (!db || !sessionId)
126579
127094
  return { error: "unavailable" };
@@ -127064,7 +127579,7 @@ import {
127064
127579
  chmodSync as chmodSync5,
127065
127580
  mkdirSync as mkdirSync11,
127066
127581
  readdirSync as readdirSync4,
127067
- readFileSync as readFileSync15,
127582
+ readFileSync as readFileSync16,
127068
127583
  renameSync as renameSync4,
127069
127584
  rmSync as rmSync2,
127070
127585
  unlinkSync as unlinkSync5,
@@ -127211,7 +127726,7 @@ class MagicContextRpcServer {
127211
127726
  for (const entry of readdirSync4(this.portDir)) {
127212
127727
  if (!entry.startsWith("port-") || !entry.endsWith(".json"))
127213
127728
  continue;
127214
- const record = parseRpcPortFile(readFileSync15(`${this.portDir}/${entry}`, "utf-8"));
127729
+ const record = parseRpcPortFile(readFileSync16(`${this.portDir}/${entry}`, "utf-8"));
127215
127730
  if (!record || record.pid === process.pid)
127216
127731
  continue;
127217
127732
  const processes = await inspectProcessesAsync();
@@ -127603,8 +128118,8 @@ function hostMediaAsset(data, mediaType) {
127603
128118
  }
127604
128119
 
127605
128120
  // src/v2/fold/owner.ts
127606
- import { createHash as createHash27 } from "node:crypto";
127607
- var foldDigest = (value) => createHash27("sha256").update(value).digest("hex");
128121
+ import { createHash as createHash28 } from "node:crypto";
128122
+ var foldDigest = (value) => createHash28("sha256").update(value).digest("hex");
127608
128123
 
127609
128124
  class FoldOwner {
127610
128125
  storage;
@@ -127907,6 +128422,64 @@ ${data.output?.output ?? ""}`
127907
128422
  return [...content.length ? [make("assistant", content)] : [], ...results];
127908
128423
  }
127909
128424
 
128425
+ // src/features/magic-context/dreamer/retrospective-orphan-sweep.ts
128426
+ init_logger();
128427
+ var HISTORIAN_CHILD_TITLE = "magic-context-compartment";
128428
+ var RETROSPECTIVE_CHILD_TITLE = "magic-context-dream-retrospective";
128429
+ var USER_MEMORIES_CHILD_TITLE = "magic-context-dream-user-memories";
128430
+ var CURATE_CHILD_TITLE = "magic-context-dream-curate";
128431
+ var MAINTAIN_DOCS_CHILD_TITLE = "magic-context-dream-maintain-docs";
128432
+ var REFRESH_PRIMERS_CHILD_TITLE = "magic-context-dream-refresh-primers";
128433
+ var MAP_MEMORIES_CHILD_TITLE = "magic-context-dream-map-memories";
128434
+ var VERIFY_CHILD_TITLE = "magic-context-dream-verify";
128435
+ var CLASSIFY_CHILD_TITLE = "magic-context-dream-classify";
128436
+ var COMPRESS_CUES_CHILD_TITLE = "magic-context-dream-compress-cues";
128437
+ var MEMORY_MIGRATION_CHILD_TITLE = "magic-context-memory-migration";
128438
+ var SMART_NOTE_COMPILE_CHILD_TITLE_PREFIX = "magic-context-smart-note-compile-";
128439
+ var SMART_NOTE_CONFIRM_CHILD_TITLE_PREFIX = "magic-context-smart-note-confirm-";
128440
+ var PRIVACY_SENSITIVE_CHILD_TITLE_MATCHES = {
128441
+ exact: [
128442
+ RETROSPECTIVE_CHILD_TITLE,
128443
+ USER_MEMORIES_CHILD_TITLE,
128444
+ CURATE_CHILD_TITLE,
128445
+ MAINTAIN_DOCS_CHILD_TITLE,
128446
+ REFRESH_PRIMERS_CHILD_TITLE,
128447
+ MAP_MEMORIES_CHILD_TITLE,
128448
+ VERIFY_CHILD_TITLE,
128449
+ CLASSIFY_CHILD_TITLE,
128450
+ COMPRESS_CUES_CHILD_TITLE
128451
+ ],
128452
+ prefixes: [SMART_NOTE_COMPILE_CHILD_TITLE_PREFIX, SMART_NOTE_CONFIRM_CHILD_TITLE_PREFIX]
128453
+ };
128454
+ var RETAINABLE_CHILD_TITLE_MATCHES = {
128455
+ exact: [HISTORIAN_CHILD_TITLE, MEMORY_MIGRATION_CHILD_TITLE],
128456
+ prefixes: []
128457
+ };
128458
+ var INTERNAL_CHILD_TITLE_MATCHES = {
128459
+ exact: [
128460
+ ...RETAINABLE_CHILD_TITLE_MATCHES.exact,
128461
+ ...PRIVACY_SENSITIVE_CHILD_TITLE_MATCHES.exact
128462
+ ],
128463
+ prefixes: PRIVACY_SENSITIVE_CHILD_TITLE_MATCHES.prefixes
128464
+ };
128465
+ var DREAMER_DETACHED_WRITER_GRACE_MS = 15 * 60000;
128466
+ function retrospectiveOrphanStaleMs(taskTimeoutMinutes) {
128467
+ const timeoutCandidates = Array.isArray(taskTimeoutMinutes) ? taskTimeoutMinutes : [taskTimeoutMinutes];
128468
+ const maxTimeoutMinutes = Math.max(...timeoutCandidates.map((minutes) => Math.max(1, minutes ?? 20)), 20);
128469
+ const timeoutMs = maxTimeoutMinutes * 60000;
128470
+ return Math.max(60 * 60000, timeoutMs * 3 + DREAMER_DETACHED_WRITER_GRACE_MS);
128471
+ }
128472
+ var HISTORIAN_OUTER_ATTEMPTS = 3;
128473
+ var HISTORIAN_MODEL_SUGGESTION_ATTEMPTS = 2;
128474
+ var HISTORIAN_MAX_RETRY_BACKOFF_MS = 11000;
128475
+ var HISTORIAN_DETACHED_WRITER_GRACE_MS = 15 * 60000;
128476
+ function historianOrphanStaleMs(timeoutMs, fallbackModelCount) {
128477
+ const timeout = Math.max(60000, timeoutMs);
128478
+ const fallbackCount = Math.max(0, Math.floor(fallbackModelCount));
128479
+ const fullAttemptBudget = timeout * HISTORIAN_OUTER_ATTEMPTS * HISTORIAN_MODEL_SUGGESTION_ATTEMPTS * (fallbackCount + 1);
128480
+ return Math.max(60 * 60000, fullAttemptBudget + HISTORIAN_MAX_RETRY_BACKOFF_MS + HISTORIAN_DETACHED_WRITER_GRACE_MS);
128481
+ }
128482
+
127910
128483
  // src/v2/hidden-completion.ts
127911
128484
  init_token_budget();
127912
128485
  init_compartment_runner_types();
@@ -127917,7 +128490,7 @@ init_hidden_child();
127917
128490
  // src/v2/host-service.ts
127918
128491
  init_data_path();
127919
128492
  await init_sqlite();
127920
- import { readdirSync as readdirSync5, readFileSync as readFileSync16 } from "node:fs";
128493
+ import { readdirSync as readdirSync5, readFileSync as readFileSync17 } from "node:fs";
127921
128494
  import { homedir as homedir11 } from "node:os";
127922
128495
  import { join as join17 } from "node:path";
127923
128496
 
@@ -128090,7 +128663,7 @@ class V2StoreReader {
128090
128663
  return { rows, cursor: rows.at(-1)?.seq };
128091
128664
  });
128092
128665
  }
128093
- messagePage(sessionID, afterOrdinal, limit, finalWatermark) {
128666
+ messagePage(sessionID, afterOrdinal, limit, finalWatermark, after) {
128094
128667
  return trackDecodeOperation("messagePage", () => {
128095
128668
  if (!Number.isSafeInteger(afterOrdinal) || afterOrdinal < 0)
128096
128669
  throw new Error("Invalid raw-message ordinal cursor");
@@ -128101,6 +128674,14 @@ class V2StoreReader {
128101
128674
  const pageSize = Math.min(limit, finalWatermark - afterOrdinal);
128102
128675
  if (pageSize <= 0)
128103
128676
  return [];
128677
+ if (after) {
128678
+ const anchor = this.db.prepare("SELECT seq FROM session_message WHERE session_id = ? AND id = ?").get(sessionID, after.id);
128679
+ if (anchor) {
128680
+ return this.db.prepare(`SELECT id, session_id, type, seq, time_created, data
128681
+ FROM session_message WHERE session_id = ? AND seq > ?
128682
+ AND type IN (${RAW_MESSAGE_TYPE_PARAMETERS}) ORDER BY seq ASC LIMIT ?`).all(sessionID, anchor.seq, ...RAW_MESSAGE_TYPES, pageSize).map(decode2);
128683
+ }
128684
+ }
128104
128685
  const maximumSeq = Number.MAX_SAFE_INTEGER;
128105
128686
  const rows = this.db.prepare(V2_MESSAGE_PAGE_SQL).all(afterOrdinal, sessionID, ...RAW_MESSAGE_TYPES, Math.max(0, afterOrdinal - 1), sessionID, ...RAW_MESSAGE_TYPES, finalWatermark - 1, maximumSeq, sessionID, ...RAW_MESSAGE_TYPES, pageSize);
128106
128687
  return rows.map(decode2);
@@ -128335,7 +128916,7 @@ class HostServiceUnavailable extends Error {
128335
128916
  function parseRegistration(path) {
128336
128917
  let text;
128337
128918
  try {
128338
- text = readFileSync16(path, "utf8");
128919
+ text = readFileSync17(path, "utf8");
128339
128920
  } catch {
128340
128921
  return;
128341
128922
  }
@@ -128449,8 +129030,8 @@ var POLL_INTERVAL_MS = 200;
128449
129030
  var REMOVAL_SPACING_MS = 250;
128450
129031
  var RETIRED_CHILDREN_LIMIT = 200;
128451
129032
  var LEGACY_CLEANUP_BOOT_LIMIT = 5;
128452
- function hiddenChildrenMetaKey(projectIdentity) {
128453
- return `${META_PREFIX}${projectIdentity}`;
129033
+ function hiddenChildrenMetaKey(projectIdentity, directory) {
129034
+ return directory === undefined ? `${META_PREFIX}${projectIdentity}` : `${META_PREFIX}${JSON.stringify([projectIdentity, directory])}`;
128454
129035
  }
128455
129036
  function emptyMeta() {
128456
129037
  return { version: 1, active: {}, retired_children: [] };
@@ -128512,9 +129093,49 @@ function parseMeta(value) {
128512
129093
  class HiddenChildStateStore {
128513
129094
  db;
128514
129095
  key;
128515
- constructor(db, projectIdentity) {
129096
+ constructor(db, projectIdentity, directory) {
128516
129097
  this.db = db;
128517
- this.key = hiddenChildrenMetaKey(projectIdentity);
129098
+ this.key = hiddenChildrenMetaKey(projectIdentity, directory);
129099
+ this.legacyKey = hiddenChildrenMetaKey(projectIdentity);
129100
+ }
129101
+ legacyKey;
129102
+ migrateStale(isStale) {
129103
+ this.db.transaction(() => {
129104
+ const legacyRow = this.db.prepare("SELECT value FROM schema_migrations_meta WHERE key = ?").get(this.legacyKey);
129105
+ if (!legacyRow)
129106
+ return;
129107
+ const legacy = parseMeta(legacyRow.value);
129108
+ const scoped = this.read();
129109
+ for (const role of ["historian", "dreamer"]) {
129110
+ const child = legacy.active[role];
129111
+ if (!child || !isStale(child))
129112
+ continue;
129113
+ scoped.retired_children.push({
129114
+ ...child,
129115
+ retired_at: Date.now(),
129116
+ reason: "legacy-directory-scope"
129117
+ });
129118
+ delete legacy.active[role];
129119
+ }
129120
+ const remaining = [];
129121
+ for (const child of legacy.retired_children) {
129122
+ if (isStale(child))
129123
+ scoped.retired_children.push(child);
129124
+ else
129125
+ remaining.push(child);
129126
+ }
129127
+ legacy.retired_children = remaining;
129128
+ const excess = scoped.retired_children.length - RETIRED_CHILDREN_LIMIT;
129129
+ if (excess > 0)
129130
+ scoped.retired_children.splice(0, excess);
129131
+ this.db.prepare(`INSERT INTO schema_migrations_meta (key, value) VALUES (?, ?)
129132
+ ON CONFLICT(key) DO UPDATE SET value = excluded.value`).run(this.key, JSON.stringify(scoped));
129133
+ if (Object.keys(legacy.active).length === 0 && legacy.retired_children.length === 0) {
129134
+ this.db.prepare("DELETE FROM schema_migrations_meta WHERE key = ?").run(this.legacyKey);
129135
+ } else {
129136
+ this.db.prepare("UPDATE schema_migrations_meta SET value = ? WHERE key = ?").run(JSON.stringify(legacy), this.legacyKey);
129137
+ }
129138
+ }).immediate();
128518
129139
  }
128519
129140
  read() {
128520
129141
  const row = this.db.prepare("SELECT value FROM schema_migrations_meta WHERE key = ?").get(this.key);
@@ -128741,6 +129362,12 @@ async function sleepUntilPoll(signal, deadline) {
128741
129362
  }, delay);
128742
129363
  });
128743
129364
  }
129365
+ function isProviderFailure(error) {
129366
+ if (!error || typeof error !== "object")
129367
+ return false;
129368
+ const type = error.type;
129369
+ return typeof type === "string" && (type.startsWith("provider.") || type.toLowerCase().includes("provider"));
129370
+ }
128744
129371
  async function awaitAssistantRow(openReader, readSessionError, stepLimit, childID, afterSeq, deadline, signal) {
128745
129372
  for (;; ) {
128746
129373
  const { assistant, idle } = withReader(openReader, (reader) => ({
@@ -128761,7 +129388,7 @@ async function awaitAssistantRow(openReader, readSessionError, stepLimit, childI
128761
129388
  `terminal_row=${errorText(newIdle)}`,
128762
129389
  `session_error=${sessionError === undefined ? "unavailable" : errorText(sessionError)}`
128763
129390
  ];
128764
- throw sessionError === undefined ? new Error(`Hidden completion failed without a provider error: ${details.join("; ")}`) : new HiddenProviderError(details.join("; "));
129391
+ throw sessionError === undefined || !isProviderFailure(sessionError) ? new Error(`Hidden completion failed: ${details.join("; ")}`) : new HiddenProviderError(details.join("; "));
128765
129392
  }
128766
129393
  if (outcome === "succeeded" && newAssistant)
128767
129394
  return newAssistant;
@@ -128775,7 +129402,7 @@ async function awaitAssistantRow(openReader, readSessionError, stepLimit, childI
128775
129402
  `terminal_row=${errorText(newAssistant)}`,
128776
129403
  `session_error=${sessionError === undefined ? "unavailable" : errorText(sessionError)}`
128777
129404
  ];
128778
- throw sessionError === undefined ? new Error(`Hidden completion failed without a provider error: ${details.join("; ")}`) : new HiddenProviderError(details.join("; "));
129405
+ throw sessionError === undefined || !isProviderFailure(sessionError) ? new Error(`Hidden completion failed: ${details.join("; ")}`) : new HiddenProviderError(details.join("; "));
128779
129406
  }
128780
129407
  if (newAssistant.data.error !== undefined) {
128781
129408
  throw new HiddenProviderError(errorText(newAssistant.data.error), {
@@ -128815,9 +129442,35 @@ function createLateHiddenExecutor(current) {
128815
129442
  }
128816
129443
  async function createV2HiddenCompletionExecutor(host, options) {
128817
129444
  const runs = new WeakMap;
128818
- const store = new HiddenChildStateStore(options.db, options.projectIdentity);
129445
+ const store = new HiddenChildStateStore(options.db, options.projectIdentity, options.directory);
128819
129446
  const generation = options.generation ?? "opencode2";
128820
129447
  const roleTails = new Map;
129448
+ const legacy = options.db.prepare("SELECT value FROM schema_migrations_meta WHERE key = ?").get(hiddenChildrenMetaKey(options.projectIdentity));
129449
+ if (legacy) {
129450
+ const children = parseMeta(legacy.value);
129451
+ const statuses = new Map;
129452
+ for (const child of [...Object.values(children.active), ...children.retired_children]) {
129453
+ if (!child || !host.status)
129454
+ continue;
129455
+ const directory = child.directory ?? options.directory;
129456
+ if (!statuses.has(directory)) {
129457
+ try {
129458
+ statuses.set(directory, await host.status({ directory }));
129459
+ } catch {
129460
+ statuses.set(directory, undefined);
129461
+ }
129462
+ }
129463
+ }
129464
+ store.migrateStale((child) => {
129465
+ const directory = child.directory ?? options.directory;
129466
+ const state = statuses.get(directory);
129467
+ if (host.status && (!state || state[child.id]?.type === "busy" || state[child.id]?.type === "retry"))
129468
+ return false;
129469
+ const last = Math.max(child.created_at, withReader(options.openReader, (reader) => Math.max(reader.latestAssistant(child.id)?.data.time?.created ?? 0, reader.latestIdle(child.id)?.data.time?.created ?? 0)));
129470
+ const staleMs = child.role === "historian" ? historianOrphanStaleMs(20 * 60000, 3) : retrospectiveOrphanStaleMs(undefined);
129471
+ return Date.now() - last > staleMs;
129472
+ });
129473
+ }
128821
129474
  const persisted = store.read();
128822
129475
  for (const child of [...Object.values(persisted.active), ...persisted.retired_children]) {
128823
129476
  if (child)
@@ -129187,6 +129840,7 @@ async function createV2HiddenCompletionExecutor(host, options) {
129187
129840
  }
129188
129841
  run.completion = {
129189
129842
  text,
129843
+ tokenLog: runTokenLog(tokens, run.identity.maxOutputTokens, row.data.finish),
129190
129844
  ...hiddenToolLoop(run.identity) ? { messages: toolLoopMessages(attempt) } : {},
129191
129845
  reasoning: text ? null : assistantReasoning(row),
129192
129846
  usage: reportedInput !== undefined || reportedOutput !== undefined ? {
@@ -129479,8 +130133,8 @@ await __promiseAll([
129479
130133
  init_context_authority(),
129480
130134
  init_memory()
129481
130135
  ]);
129482
- import { createHash as createHash35 } from "node:crypto";
129483
- import { existsSync as existsSync19, readFileSync as readFileSync19 } from "node:fs";
130136
+ import { createHash as createHash36 } from "node:crypto";
130137
+ import { existsSync as existsSync19, readFileSync as readFileSync20 } from "node:fs";
129484
130138
 
129485
130139
  // src/features/magic-context/mural/compress-cues.ts
129486
130140
  init_dreamer();
@@ -129488,10 +130142,11 @@ init_compartment_runner_types();
129488
130142
  init_shared();
129489
130143
  init_logger();
129490
130144
  await init_compartment_runner_historian();
129491
- import { createHash as createHash29 } from "node:crypto";
130145
+ import { createHash as createHash30 } from "node:crypto";
129492
130146
 
129493
130147
  // src/features/magic-context/dreamer/lease.ts
129494
130148
  init_write_transaction_timing();
130149
+ await init_sqlite();
129495
130150
 
129496
130151
  // src/features/magic-context/dreamer/storage-dream-state.ts
129497
130152
  var getDreamStateStatements = new WeakMap;
@@ -129691,7 +130346,7 @@ function startLeaseHeartbeat(db, holderId, leaseKey, onLost, intervalOrAcquisiti
129691
130346
  }
129692
130347
  };
129693
130348
  beat();
129694
- const timer = lost ? undefined : setInterval(beat, intervalMs);
130349
+ const timer = lost ? undefined : setInterval(() => withSqliteBackgroundWriter(beat), intervalMs);
129695
130350
  return {
129696
130351
  stop: () => {
129697
130352
  if (timer)
@@ -129860,7 +130515,7 @@ function getModuleMemoryIdentities(db, projectIdentity, contextIds) {
129860
130515
  }
129861
130516
 
129862
130517
  // src/features/magic-context/dreamer/provider-output-failure.ts
129863
- import { createHash as createHash28 } from "node:crypto";
130518
+ import { createHash as createHash29 } from "node:crypto";
129864
130519
  var MAX_NEAR_ZERO_OUTPUT_TOKENS = 32;
129865
130520
 
129866
130521
  class DreamerProviderOutputFailureError extends Error {
@@ -129917,7 +130572,7 @@ function providerOutputFailureFromInvalidManifest(messages, responseText) {
129917
130572
  const normalized = responseText.trim().replace(/\s+/g, " ").toLowerCase();
129918
130573
  if (!normalized)
129919
130574
  return null;
129920
- const fingerprint = createHash28("sha256").update(normalized).digest("hex").slice(0, 16);
130575
+ const fingerprint = createHash29("sha256").update(normalized).digest("hex").slice(0, 16);
129921
130576
  return new DreamerProviderOutputFailureError(fingerprint, completion.outputTokens, completion.reasoningTokens, responseText);
129922
130577
  }
129923
130578
 
@@ -130329,7 +130984,7 @@ async function applyCuesThroughModule(args, chunk, manifestText, signal) {
130329
130984
  log(`[dreamer] compress-cues: skipped cue for memory ${entry.id} (${failure.reason}; rejection ${rejectionCount}/${CUE_REJECTION_LATCH_THRESHOLD})`);
130330
130985
  }
130331
130986
  }
130332
- const commandId = `mural-cues:${route.moduleCommandId}:${createHash29("sha256").update(chunk.map((candidate) => candidate.memory.id).join(",")).digest("hex").slice(0, 24)}`;
130987
+ const commandId = `mural-cues:${route.moduleCommandId}:${createHash30("sha256").update(chunk.map((candidate) => candidate.memory.id).join(",")).digest("hex").slice(0, 24)}`;
130333
130988
  let response;
130334
130989
  try {
130335
130990
  response = await route.moduleClient.call({
@@ -130433,8 +131088,10 @@ async function runHiddenSingleShotPrompt(args) {
130433
131088
  callContext: args.callContext,
130434
131089
  fetchOutput: () => args.executor.collect(handle, 50),
130435
131090
  validateOutput: (completion) => {
131091
+ const tokens = completion.tokenLog ?? runTokenLog(undefined);
131092
+ sessionLog(args.parentSessionId ?? "dreamer", `${args.callContext} response_chars=${(completion.text ?? completion.reasoning ?? "").length} ${formatRunTokenLog(tokens)}`);
130436
131093
  if (completion.lengthCapped) {
130437
- throw new Error(completion.reasoning && !completion.text ? `${args.callContext} ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text) — set dreamer.maxTokens or use a low-reasoning model` : `${args.callContext} returned length-capped output`);
131094
+ throw new Error(completion.reasoning && !completion.text ? `${args.callContext} ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text; ${formatRunTokenLog(tokens)}) — set dreamer.maxTokens or use a low-reasoning model` : `${args.callContext} returned length-capped output; ${formatRunTokenLog(tokens)}`);
130438
131095
  }
130439
131096
  const text = completion.text;
130440
131097
  if (!text && !args.allowEmpty)
@@ -130913,7 +131570,7 @@ init_dreamer();
130913
131570
  init_language_directive();
130914
131571
  init_compartment_runner_types();
130915
131572
  await init_compartment_runner_historian();
130916
- import { createHash as createHash30 } from "node:crypto";
131573
+ import { createHash as createHash31 } from "node:crypto";
130917
131574
 
130918
131575
  // src/plugin/rust-tool-backends.ts
130919
131576
  function toolCallIdFromContext(context) {
@@ -131304,8 +131961,10 @@ async function classifyOneChunk(args, chunk, anchors, sliceMs, signal) {
131304
131961
  fetchOutput: () => executor.collect(opened, 50),
131305
131962
  validateOutput: (completion) => {
131306
131963
  const messages = completion.messages ?? [];
131964
+ const tokens = completion.tokenLog ?? runTokenLog(undefined);
131965
+ sessionLog(args.parentSessionId ?? "dreamer", `dreamer:classify response_chars=${(completion.text ?? completion.reasoning ?? "").length} ${formatRunTokenLog(tokens)}`);
131307
131966
  if (completion.lengthCapped) {
131308
- throw new Error(completion.reasoning && !completion.text ? `classify ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text) — set dreamer.maxTokens or use a low-reasoning model` : "classify returned length-capped output");
131967
+ throw new Error(completion.reasoning && !completion.text ? `classify ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text; ${formatRunTokenLog(tokens)}) — set dreamer.maxTokens or use a low-reasoning model` : `classify returned length-capped output; ${formatRunTokenLog(tokens)}`);
131309
131968
  }
131310
131969
  const text = completion.text;
131311
131970
  if (!text)
@@ -131382,7 +132041,7 @@ async function runClassifyThroughModule(args, chunk, anchors, signal, runOnHost)
131382
132041
  });
131383
132042
  const modelChain = [args.model, ...args.fallbackModels ?? []].map(toModelEntry).filter((entry) => entry !== undefined).map((entry) => entry.model);
131384
132043
  const resolvedModelChain = [...new Set(modelChain)];
131385
- const commandId = `classify:${args.moduleCommandId ?? Date.now()}:${createHash30("sha256").update(chunk.map((candidate) => candidate.id).join(",")).digest("hex").slice(0, 24)}`;
132044
+ const commandId = `classify:${args.moduleCommandId ?? Date.now()}:${createHash31("sha256").update(chunk.map((candidate) => candidate.id).join(",")).digest("hex").slice(0, 24)}`;
131386
132045
  const runTask = (hostCompletion) => args.moduleClient?.call({
131387
132046
  sessionId: args.moduleSessionId,
131388
132047
  projectRoot: args.moduleProjectRoot,
@@ -131664,13 +132323,13 @@ function takeCurateSafetyRefusalCount(sessionId) {
131664
132323
 
131665
132324
  // src/features/magic-context/dreamer/docs-proposals.ts
131666
132325
  import { execFileSync as execFileSync3 } from "node:child_process";
131667
- import { createHash as createHash31 } from "node:crypto";
132326
+ import { createHash as createHash32 } from "node:crypto";
131668
132327
  import {
131669
132328
  existsSync as existsSync16,
131670
132329
  mkdirSync as mkdirSync12,
131671
132330
  mkdtempSync,
131672
132331
  readdirSync as readdirSync6,
131673
- readFileSync as readFileSync17,
132332
+ readFileSync as readFileSync18,
131674
132333
  realpathSync as realpathSync5,
131675
132334
  renameSync as renameSync5,
131676
132335
  rmSync as rmSync3,
@@ -131679,12 +132338,12 @@ import {
131679
132338
  import { tmpdir as tmpdir2 } from "node:os";
131680
132339
  import { join as join18 } from "node:path";
131681
132340
  var FILES = ["ARCHITECTURE.md", "STRUCTURE.md"];
131682
- var hash = (text) => createHash31("sha256").update(text).digest("hex");
132341
+ var hash = (text) => createHash32("sha256").update(text).digest("hex");
131683
132342
  var proposalDir = (projectDir) => join18(projectDir, ".cortexkit/magic-context/docs-update-proposals");
131684
132343
  function docsBaseHashes(projectDir) {
131685
132344
  return Object.fromEntries(FILES.map((name) => [
131686
132345
  name,
131687
- hash(existsSync16(join18(projectDir, name)) ? readFileSync17(join18(projectDir, name), "utf8") : "")
132346
+ hash(existsSync16(join18(projectDir, name)) ? readFileSync18(join18(projectDir, name), "utf8") : "")
131688
132347
  ]));
131689
132348
  }
131690
132349
  function listPendingDocsProposals(projectDir) {
@@ -131693,7 +132352,7 @@ function listPendingDocsProposals(projectDir) {
131693
132352
  }
131694
132353
  function hasCurrentDocsProposal(projectDir) {
131695
132354
  const hashes = docsBaseHashes(projectDir);
131696
- return listPendingDocsProposals(projectDir).some((path) => Object.entries(hashes).every(([name, value]) => readFileSync17(path, "utf8").includes(`Base SHA-256 ${name}: ${value}`)));
132355
+ return listPendingDocsProposals(projectDir).some((path) => Object.entries(hashes).every(([name, value]) => readFileSync18(path, "utf8").includes(`Base SHA-256 ${name}: ${value}`)));
131697
132356
  }
131698
132357
  function git(projectDir, args) {
131699
132358
  return execFileSync3("git", args, {
@@ -131732,7 +132391,7 @@ function docsChangeSet(projectDir, storedAnchor) {
131732
132391
  const files = [
131733
132392
  ...new Set(changed.flat().filter((file) => file !== "ARCHITECTURE.md" && file !== "STRUCTURE.md" && !/(?:^|[./-])(test|spec)(?:[./-]|$)/.test(file) && !file.endsWith(".lock")))
131734
132393
  ];
131735
- const docs = FILES.map((file) => existsSync16(join18(projectDir, file)) ? readFileSync17(join18(projectDir, file), "utf8") : "").join(`
132394
+ const docs = FILES.map((file) => existsSync16(join18(projectDir, file)) ? readFileSync18(join18(projectDir, file), "utf8") : "").join(`
131736
132395
  `);
131737
132396
  const relevant = files.some((file) => {
131738
132397
  const directory = file.split("/").slice(0, -1).join("/");
@@ -131877,7 +132536,7 @@ function validateDocsProposal(projectDir, output, budget, expected) {
131877
132536
  let tokens = 0;
131878
132537
  let diffs = "";
131879
132538
  for (const file of FILES) {
131880
- const before = existsSync16(join18(projectDir, file)) ? readFileSync17(join18(projectDir, file), "utf8") : "";
132539
+ const before = existsSync16(join18(projectDir, file)) ? readFileSync18(join18(projectDir, file), "utf8") : "";
131881
132540
  let after = before;
131882
132541
  const seen = new Set;
131883
132542
  for (const change of changes.filter((item) => item.file === file)) {
@@ -132471,7 +133130,7 @@ function throwIfAborted2(signal) {
132471
133130
  }
132472
133131
 
132473
133132
  // src/features/magic-context/smart-notes/compiler.ts
132474
- import { createHash as createHash32 } from "node:crypto";
133133
+ import { createHash as createHash33 } from "node:crypto";
132475
133134
  init_shared();
132476
133135
  init_assistant_message_extractor();
132477
133136
  init_child_session_teardown();
@@ -132785,7 +133444,7 @@ function manifestAdvisoryWarnings(code, manifest) {
132785
133444
  return warnings;
132786
133445
  }
132787
133446
  function hashCheck(surfaceCondition, compiledCheck, manifest, checkCron) {
132788
- return createHash32("sha256").update(surfaceCondition ?? "").update("\x00").update(compiledCheck).update("\x00").update(JSON.stringify(manifest)).update("\x00").update(checkCron).digest("hex");
133447
+ return createHash33("sha256").update(surfaceCondition ?? "").update("\x00").update(compiledCheck).update("\x00").update(JSON.stringify(manifest)).update("\x00").update(checkCron).digest("hex");
132789
133448
  }
132790
133449
  function extractJsonObject(output) {
132791
133450
  const fenced = output.match(/```(?:json)?\s*([\s\S]*?)```/i);
@@ -133573,7 +134232,7 @@ async function archiveExpiredMemories(args) {
133573
134232
 
133574
134233
  // src/features/magic-context/dreamer/maintain-docs-protected-enforcement.ts
133575
134234
  init_logger();
133576
- import { existsSync as existsSync17, readFileSync as readFileSync18, writeFileSync as writeFileSync10 } from "node:fs";
134235
+ import { existsSync as existsSync17, readFileSync as readFileSync19, writeFileSync as writeFileSync10 } from "node:fs";
133577
134236
  import { join as join20 } from "node:path";
133578
134237
  var MAINTAIN_DOCS_SNAPSHOT_FILES = ["ARCHITECTURE.md", "STRUCTURE.md"];
133579
134238
  function snapshotMaintainDocsFiles(docsDir) {
@@ -133582,7 +134241,7 @@ function snapshotMaintainDocsFiles(docsDir) {
133582
134241
  const path = join20(docsDir, name);
133583
134242
  try {
133584
134243
  if (existsSync17(path)) {
133585
- snapshot.set(name, readFileSync18(path, "utf8"));
134244
+ snapshot.set(name, readFileSync19(path, "utf8"));
133586
134245
  }
133587
134246
  } catch {}
133588
134247
  }
@@ -133600,10 +134259,10 @@ await __promiseAll([
133600
134259
  init_child_session_spawn(),
133601
134260
  init_memory()
133602
134261
  ]);
133603
- import { createHash as createHash33 } from "node:crypto";
134262
+ import { createHash as createHash34 } from "node:crypto";
133604
134263
 
133605
134264
  // src/features/magic-context/dreamer/map-memories-prompt.ts
133606
- import { existsSync as existsSync18, statSync as statSync13 } from "node:fs";
134265
+ import { existsSync as existsSync18, statSync as statSync14 } from "node:fs";
133607
134266
  import path11 from "node:path";
133608
134267
  var MAP_MEMORIES_SYSTEM_PROMPT = `You are a memory mapper for the magic-context system. You map project memories to the repository files that back them.
133609
134268
 
@@ -133641,7 +134300,7 @@ function extractMemoryCandidatePaths(content, repoDir) {
133641
134300
  if (!abs.startsWith(`${root}/`))
133642
134301
  continue;
133643
134302
  try {
133644
- if (existsSync18(abs) && statSync13(abs).isFile())
134303
+ if (existsSync18(abs) && statSync14(abs).isFile())
133645
134304
  found.add(rel);
133646
134305
  } catch {}
133647
134306
  if (found.size >= MAX_SEED_PATHS_PER_MEMORY)
@@ -133855,6 +134514,11 @@ async function mapOneBatch(args, batch, sliceMs, signal) {
133855
134514
  let agentSessionId = null;
133856
134515
  let promptSettled = false;
133857
134516
  const startedAt = Date.now();
134517
+ let budgetFinalized = false;
134518
+ const onBudgetUpdate = (state) => {
134519
+ budgetFinalized ||= state.finalizeFired;
134520
+ args.onBudgetUpdate?.(state);
134521
+ };
133858
134522
  try {
133859
134523
  const prompt = buildMapMemoriesPrompt(args.projectIdentity, batch);
133860
134524
  if (args.hiddenCompletionExecutor) {
@@ -133867,7 +134531,7 @@ async function mapOneBatch(args, batch, sliceMs, signal) {
133867
134531
  prompt,
133868
134532
  title: "magic-context-dream-map-memories",
133869
134533
  callContext: "dreamer:map-memories",
133870
- metadata: { tokenBudget: args.tokenBudget, onBudgetUpdate: args.onBudgetUpdate },
134534
+ metadata: { tokenBudget: args.tokenBudget, onBudgetUpdate },
133871
134535
  model: args.model,
133872
134536
  fallbackModels: args.fallbackModels,
133873
134537
  timeoutMs: sliceMs,
@@ -133878,9 +134542,12 @@ async function mapOneBatch(args, batch, sliceMs, signal) {
133878
134542
  status: "completed",
133879
134543
  messages: run.completion.messages ?? []
133880
134544
  });
133881
- const outcome = await applyParsedBatchMappings(args, batch, run.validated);
134545
+ const outcome = await applyParsedBatchMappings(args, batch, run.validated, budgetFinalized);
133882
134546
  const returnedIds = new Set(run.validated.map((entry) => entry.id));
133883
- return { ...outcome, requeue: batch.filter((memory) => !returnedIds.has(memory.id)) };
134547
+ return {
134548
+ ...outcome,
134549
+ requeue: budgetFinalized ? [] : batch.filter((memory) => !returnedIds.has(memory.id))
134550
+ };
133884
134551
  }
133885
134552
  const client = args.client;
133886
134553
  if (!client)
@@ -133910,7 +134577,7 @@ async function mapOneBatch(args, batch, sliceMs, signal) {
133910
134577
  }, {
133911
134578
  transport: createPromptAsyncTransport(client, agentSessionId, {
133912
134579
  tokenBudget: args.tokenBudget,
133913
- onBudgetUpdate: args.onBudgetUpdate
134580
+ onBudgetUpdate
133914
134581
  }),
133915
134582
  timeoutMs: sliceMs,
133916
134583
  signal,
@@ -133937,11 +134604,11 @@ async function mapOneBatch(args, batch, sliceMs, signal) {
133937
134604
  });
133938
134605
  promptSettled = true;
133939
134606
  recordInvocation2(args, startedAt, { status: "completed", messages: run.output });
133940
- const outcome = await applyParsedBatchMappings(args, batch, run.validated);
134607
+ const outcome = await applyParsedBatchMappings(args, batch, run.validated, budgetFinalized);
133941
134608
  const returnedIds = new Set(run.validated.filter((entry) => batch.some((memory) => memory.id === entry.id)).map((entry) => entry.id));
133942
134609
  return {
133943
134610
  ...outcome,
133944
- requeue: batch.filter((memory) => !returnedIds.has(memory.id))
134611
+ requeue: budgetFinalized ? [] : batch.filter((memory) => !returnedIds.has(memory.id))
133945
134612
  };
133946
134613
  } catch (error) {
133947
134614
  const desc = describeError(error);
@@ -133976,7 +134643,7 @@ async function mapOneBatch(args, batch, sliceMs, signal) {
133976
134643
  });
133977
134644
  }
133978
134645
  }
133979
- async function applyParsedBatchMappings(args, batch, parsed) {
134646
+ async function applyParsedBatchMappings(args, batch, parsed, budgetFinalized = false) {
133980
134647
  const batchIds = new Set(batch.map((memory) => memory.id));
133981
134648
  const valid = parsed.filter((entry) => batchIds.has(entry.id));
133982
134649
  const unknown = parsed.filter((entry) => !batchIds.has(entry.id));
@@ -133984,9 +134651,12 @@ async function applyParsedBatchMappings(args, batch, parsed) {
133984
134651
  log(`[dreamer] map-memories warning: dropping ${unknown.length} unknown mapping entr${unknown.length === 1 ? "y" : "ies"} outside the current batch (${unknown.map((entry) => entry.id).join(", ")})`);
133985
134652
  }
133986
134653
  assertNoDuplicateManifestIds(valid.map((entry) => entry.id), "mappings");
133987
- if (valid.length * 2 < batch.length) {
134654
+ if (!budgetFinalized && valid.length * 2 < batch.length) {
133988
134655
  throw new Error(`mappings manifest covers ${valid.length}/${batch.length} batch ids after filtering unknown entries; rejecting mostly-wrong manifest`);
133989
134656
  }
134657
+ if (budgetFinalized && valid.length < batch.length) {
134658
+ log(`[dreamer] map-memories: accepted partial manifest after token budget: ${valid.length}/${batch.length}`);
134659
+ }
133990
134660
  if (valid.length === 0)
133991
134661
  return { mapped: 0, independent: 0 };
133992
134662
  const planned = [];
@@ -134065,7 +134735,7 @@ async function applyParsedBatchMappings(args, batch, parsed) {
134065
134735
  memory_project: args.projectIdentity,
134066
134736
  context_store_uuid: args.moduleRoute.moduleContextStoreUuid,
134067
134737
  authority_generation: args.moduleRoute.moduleAuthorityGeneration,
134068
- command_id: `${args.moduleRoute.moduleCommandId}:${createHash33("sha256").update(rows.map((row) => row.memory_id).join(",")).digest("hex").slice(0, 16)}`,
134738
+ command_id: `${args.moduleRoute.moduleCommandId}:${createHash34("sha256").update(rows.map((row) => row.memory_id).join(",")).digest("hex").slice(0, 16)}`,
134069
134739
  rows
134070
134740
  }
134071
134741
  }
@@ -135047,7 +135717,7 @@ await __promiseAll([
135047
135717
  init_child_session_spawn(),
135048
135718
  init_memory()
135049
135719
  ]);
135050
- import { createHash as createHash34 } from "node:crypto";
135720
+ import { createHash as createHash35 } from "node:crypto";
135051
135721
  init_token_budget();
135052
135722
 
135053
135723
  // src/features/magic-context/dreamer/verify-diff.ts
@@ -135543,6 +136213,11 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
135543
136213
  let agentSessionId = null;
135544
136214
  let promptSettled = false;
135545
136215
  const startedAt = Date.now();
136216
+ let budgetFinalized = false;
136217
+ const onBudgetUpdate = (state) => {
136218
+ budgetFinalized ||= state.finalizeFired;
136219
+ args.onBudgetUpdate?.(state);
136220
+ };
135546
136221
  try {
135547
136222
  const headAtPrompt = await readGitHead(args.sessionDirectory);
135548
136223
  const evidence = !args.forceBroad && batch.every((m) => m.verifiedAt) ? await buildVerifyDiffEvidence(args.sessionDirectory, batch) : null;
@@ -135557,7 +136232,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
135557
136232
  prompt,
135558
136233
  title: "magic-context-dream-verify",
135559
136234
  callContext: "dreamer:verify",
135560
- metadata: { tokenBudget: args.tokenBudget, onBudgetUpdate: args.onBudgetUpdate },
136235
+ metadata: { tokenBudget: args.tokenBudget, onBudgetUpdate },
135561
136236
  model: args.model,
135562
136237
  fallbackModels: args.fallbackModels,
135563
136238
  language: args.language,
@@ -135574,7 +136249,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
135574
136249
  status: "completed",
135575
136250
  messages: run.completion.messages ?? []
135576
136251
  });
135577
- return applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt);
136252
+ return applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
135578
136253
  }
135579
136254
  const client = args.client;
135580
136255
  if (!client)
@@ -135604,7 +136279,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
135604
136279
  }, {
135605
136280
  transport: createPromptAsyncTransport(client, agentSessionId, {
135606
136281
  tokenBudget: args.tokenBudget,
135607
- onBudgetUpdate: args.onBudgetUpdate
136282
+ onBudgetUpdate
135608
136283
  }),
135609
136284
  timeoutMs: sliceMs,
135610
136285
  signal,
@@ -135634,7 +136309,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
135634
136309
  });
135635
136310
  promptSettled = true;
135636
136311
  recordInvocation4(args, startedAt, { status: "completed", messages: run.output });
135637
- return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt);
136312
+ return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
135638
136313
  } catch (error) {
135639
136314
  const desc = describeError(error);
135640
136315
  const providerFailure = error instanceof DreamerProviderOutputFailureError ? error : undefined;
@@ -135680,7 +136355,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
135680
136355
  });
135681
136356
  }
135682
136357
  }
135683
- async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt) {
136358
+ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budgetFinalized = false) {
135684
136359
  const batchIds = new Set(batch.map((m) => m.id));
135685
136360
  const batchById = new Map(batch.map((memory) => [memory.id, memory]));
135686
136361
  const valid = {
@@ -135700,9 +136375,12 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt) {
135700
136375
  }
135701
136376
  const validIds = [...valid.verified, ...valid.updated, ...valid.archived, ...valid.skipped].map((entry) => entry.id);
135702
136377
  assertNoDuplicateManifestIds(validIds, "verify");
135703
- if (validIds.length * 2 < batch.length) {
136378
+ if (!budgetFinalized && validIds.length * 2 < batch.length) {
135704
136379
  throw new Error(`verify manifest covers ${validIds.length}/${batch.length} batch ids after filtering unknown entries; rejecting mostly-wrong manifest`);
135705
136380
  }
136381
+ if (budgetFinalized && validIds.length < batch.length) {
136382
+ log(`[dreamer] verify: accepted partial manifest after token budget: ${validIds.length}/${batch.length}`);
136383
+ }
135706
136384
  if (validIds.length === 0) {
135707
136385
  return { verified: 0, updated: 0, archived: 0, skipped: 0, refused: 0 };
135708
136386
  }
@@ -135787,7 +136465,7 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt) {
135787
136465
  memory_project: args.projectIdentity,
135788
136466
  context_store_uuid: args.moduleRoute.moduleContextStoreUuid,
135789
136467
  authority_generation: args.moduleRoute.moduleAuthorityGeneration,
135790
- command_id: `${args.moduleRoute.moduleCommandId}:${createHash34("sha256").update(rows.map((row) => row.memory_id).join(",")).digest("hex").slice(0, 16)}`,
136468
+ command_id: `${args.moduleRoute.moduleCommandId}:${createHash35("sha256").update(rows.map((row) => row.memory_id).join(",")).digest("hex").slice(0, 16)}`,
135791
136469
  rows
135792
136470
  }
135793
136471
  }
@@ -136547,7 +137225,7 @@ function parseFrictionGateVerdict(verdict) {
136547
137225
  }
136548
137226
  function computeRetrospectiveWindowKey(flagged) {
136549
137227
  const anchors = flagged.map((message) => `${message.sessionId}:${message.ts}`).sort().join("|");
136550
- return createHash35("sha256").update(anchors).digest("hex").slice(0, 32);
137228
+ return createHash36("sha256").update(anchors).digest("hex").slice(0, 32);
136551
137229
  }
136552
137230
  function renderFrictionWindow(messages, flaggedOrdinals, radius = 2) {
136553
137231
  const flagged = new Set(flaggedOrdinals);
@@ -137013,8 +137691,8 @@ async function runAgenticTask(config, ctx, helpers) {
137013
137691
  existingDocs,
137014
137692
  docsChangeSet: changes?.text,
137015
137693
  docsContents: task === "maintain-docs" ? {
137016
- architecture: existingDocs?.architecture ? readFileSync19(`${docsDir}/ARCHITECTURE.md`, "utf8") : "(missing)",
137017
- structure: existingDocs?.structure ? readFileSync19(`${docsDir}/STRUCTURE.md`, "utf8") : "(missing)"
137694
+ architecture: existingDocs?.architecture ? readFileSync20(`${docsDir}/ARCHITECTURE.md`, "utf8") : "(missing)",
137695
+ structure: existingDocs?.structure ? readFileSync20(`${docsDir}/STRUCTURE.md`, "utf8") : "(missing)"
137018
137696
  } : undefined,
137019
137697
  docsBudget: config.docsMaxTokens ?? 12000,
137020
137698
  docsCurrentTokens: maintainDocsSnapshot ? Math.ceil([...maintainDocsSnapshot.values()].join("").length / 3.5) : 0,
@@ -137665,10 +138343,19 @@ function startDreamTrigger(context, args) {
137665
138343
  // src/v2/hooks/context.ts
137666
138344
  init_hidden_child();
137667
138345
 
138346
+ // src/v2/hooks/hidden-terminal-error.ts
138347
+ function hiddenTerminalError(event) {
138348
+ if (event.type === "session.execution.failed")
138349
+ return event.data?.error;
138350
+ if (event.type === "session.error")
138351
+ return getSessionErrorInfo(event.data)?.error;
138352
+ return;
138353
+ }
138354
+
137668
138355
  // src/v2/hooks/lkg-system.ts
137669
- import { createHash as createHash36 } from "node:crypto";
138356
+ import { createHash as createHash37 } from "node:crypto";
137670
138357
  function identity(slot) {
137671
- return createHash36("sha256").update(JSON.stringify({
138358
+ return createHash37("sha256").update(JSON.stringify({
137672
138359
  capturedAt: slot.capturedAt,
137673
138360
  captureSequence: slot.captureSequence,
137674
138361
  rowVersion: slot.rowVersion,
@@ -137909,6 +138596,7 @@ function adaptPayload(draft, admittedIDs = new Set) {
137909
138596
  input: structuredClone(call?.input ?? {}),
137910
138597
  status: result ? "completed" : "running",
137911
138598
  ...result ? { output } : {},
138599
+ ...result && (call?.name ?? result.part.name) === "question" ? { metadata: { userAnswer: true } } : {},
137912
138600
  ...files ? { attachments: contentValueAttachments(files) } : {}
137913
138601
  }
137914
138602
  };
@@ -138997,10 +139685,10 @@ function* readV2RawMessagePagesForConversion(openReader, sessionID, pageRows = C
138997
139685
  function createV2RawMessageReader(openReader) {
138998
139686
  const servedBoundaryCache = new Map;
138999
139687
  return {
139000
- readPage: (sessionID, afterOrdinal, limit, finalWatermark) => {
139688
+ readPage: (sessionID, afterOrdinal, limit, finalWatermark, after) => {
139001
139689
  const reader = openReader();
139002
139690
  try {
139003
- return rawMessagePage(reader.messagePage(sessionID, afterOrdinal, limit, finalWatermark), afterOrdinal);
139691
+ return rawMessagePage(reader.messagePage(sessionID, afterOrdinal, limit, finalWatermark, after), afterOrdinal);
139004
139692
  } finally {
139005
139693
  reader.close();
139006
139694
  }
@@ -139094,7 +139782,7 @@ function createV2RawMessageReader(openReader) {
139094
139782
  }
139095
139783
  function createV2RawMessageProvider(reader, sessionID) {
139096
139784
  return {
139097
- readMessagePage: (afterOrdinal, limit, finalWatermark) => reader.readPage(sessionID, afterOrdinal, limit, finalWatermark),
139785
+ readMessagePage: (afterOrdinal, limit, finalWatermark, after) => reader.readPage(sessionID, afterOrdinal, limit, finalWatermark, after),
139098
139786
  readMessageById: (messageID) => reader.findById(sessionID, messageID),
139099
139787
  readMessagePartsById: (messageID) => reader.findPartsById(sessionID, messageID),
139100
139788
  hasMessageById: (messageID) => reader.hasById(sessionID, messageID),
@@ -151434,6 +152122,7 @@ init_agent_disable();
151434
152122
  init_project_identity();
151435
152123
  init_protection_window();
151436
152124
  init_storage_meta_persisted();
152125
+ await init_sqlite();
151437
152126
 
151438
152127
  // src/tools/ctx-expand/tools.ts
151439
152128
  init_compartment_storage();
@@ -153470,7 +154159,7 @@ Keep: user messages (never stamp one for its directive), your own conversation t
153470
154159
 
153471
154160
  Look at each tag before stamping it; never blanket-stamp a range like "1-50". Many small targeted stamps beat one sweep. \`drop\` accepts "3-5", "1,2,9", "1-5,8,12-15".`;
153472
154161
  // src/tools/ctx-reduce/tools.ts
153473
- import { createHash as createHash37 } from "node:crypto";
154162
+ import { createHash as createHash38 } from "node:crypto";
153474
154163
  init_protection_window();
153475
154164
 
153476
154165
  // src/features/magic-context/range-parser.ts
@@ -153544,11 +154233,11 @@ function createCtxReduceTool(deps) {
153544
154233
  const stableId = `oc-${sessionId}-${callId}`;
153545
154234
  if (Buffer.byteLength(stableId) <= 128)
153546
154235
  return stableId;
153547
- return `oc-${createHash37("sha256").update(stableId).digest("hex")}`;
154236
+ return `oc-${createHash38("sha256").update(stableId).digest("hex")}`;
153548
154237
  }
153549
154238
  fallbackCommandSequence += 1;
153550
154239
  const monotonicId = `oc-${sessionId}-${fallbackCommandSequence}`;
153551
- return Buffer.byteLength(monotonicId) <= 128 ? monotonicId : `oc-${createHash37("sha256").update(monotonicId).digest("hex")}`;
154240
+ return Buffer.byteLength(monotonicId) <= 128 ? monotonicId : `oc-${createHash38("sha256").update(monotonicId).digest("hex")}`;
153552
154241
  };
153553
154242
  return tool({
153554
154243
  description: CTX_REDUCE_DESCRIPTION,
@@ -153909,6 +154598,10 @@ async function registerTools(context, db, config, rustToolBackends) {
153909
154598
  input: tool.schema.toJSONSchema(tool.schema.object(definition.args)),
153910
154599
  options: { codemode: false },
153911
154600
  async execute(input, call) {
154601
+ if (name === "ctx_memory" || name === "ctx_note" || name === "ctx_reduce")
154602
+ await withAsyncPrivilegedWriter(db, () => {
154603
+ return;
154604
+ });
153912
154605
  const result = await definition.execute(tool.schema.object(definition.args).parse(input), {
153913
154606
  sessionID: call.sessionID,
153914
154607
  messageID: call.messageID,
@@ -154145,6 +154838,7 @@ async function applyV2SystemPrompt(systemPrompt, draft) {
154145
154838
  async function registerContext(context) {
154146
154839
  const directory = context.location.directory;
154147
154840
  const config = loadPluginConfigDetailed(directory).config;
154841
+ setHomeProjectPermission(config.allow_home_project);
154148
154842
  if (!config.enabled)
154149
154843
  return;
154150
154844
  const liveConfigReader = pluginConfigReader(directory, config);
@@ -154253,6 +154947,7 @@ async function registerContext(context) {
154253
154947
  }, {
154254
154948
  db: database,
154255
154949
  projectIdentity: resolveProjectIdentityForSession(directory, config.allow_home_project) ?? "",
154950
+ directory,
154256
154951
  hook: hiddenChildHook,
154257
154952
  keepSubagents: config.keep_subagents === true,
154258
154953
  ensureAgent: () => hiddenAgentsReady ??= context.agent.reload(),
@@ -154455,8 +155150,8 @@ async function registerContext(context) {
154455
155150
  if (!event.data?.sessionID)
154456
155151
  continue;
154457
155152
  const sessionID = event.data.sessionID;
154458
- if (event.type === "session.error") {
154459
- const error = getSessionErrorInfo(event.data)?.error;
155153
+ if (event.type === "session.error" || event.type === "session.execution.failed") {
155154
+ const error = hiddenTerminalError(event);
154460
155155
  if (error !== undefined)
154461
155156
  hiddenSessionErrors.set(sessionID, error);
154462
155157
  continue;
@@ -154579,7 +155274,7 @@ async function registerContext(context) {
154579
155274
  try {
154580
155275
  const admissionDb = db ?? storage.current();
154581
155276
  if (!compactionOff && admissionDb)
154582
- withPrivilegedWriter(admissionDb, () => {
155277
+ await withAsyncPrivilegedWriter(admissionDb, () => {
154583
155278
  return;
154584
155279
  });
154585
155280
  recordV2ToolDefinitions(draft);
@@ -154965,7 +155660,7 @@ ${baseline}
154965
155660
  }
154966
155661
 
154967
155662
  // src/v2/hooks/update-check.ts
154968
- import { existsSync as existsSync22, readFileSync as readFileSync20 } from "node:fs";
155663
+ import { existsSync as existsSync22, readFileSync as readFileSync21 } from "node:fs";
154969
155664
  import { dirname as dirname11, join as join23 } from "node:path";
154970
155665
  import { fileURLToPath as fileURLToPath2 } from "node:url";
154971
155666
 
@@ -155046,7 +155741,7 @@ function packageInfo() {
155046
155741
  let directory = dirname11(fileURLToPath2(import.meta.url));
155047
155742
  for (;; ) {
155048
155743
  try {
155049
- const pkg = JSON.parse(readFileSync20(join23(directory, "package.json"), "utf8"));
155744
+ const pkg = JSON.parse(readFileSync21(join23(directory, "package.json"), "utf8"));
155050
155745
  if (pkg.name === "@cortexkit/opencode-magic-context" && typeof pkg.version === "string") {
155051
155746
  return {
155052
155747
  version: pkg.version,