@retaw106/mmc-oc 2.0.0 → 2.1.0

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 (100) hide show
  1. package/dist/cli.js +1 -1
  2. package/dist/config/live-run-config.d.ts.map +1 -1
  3. package/dist/config-init.js +1 -1
  4. package/dist/{context-95zkksn3.js → context-vhcstgs3.js} +1812 -814
  5. package/dist/features/magic-context/memory/types.d.ts +1 -0
  6. package/dist/features/magic-context/memory/types.d.ts.map +1 -1
  7. package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
  8. package/dist/features/magic-context/session-project-storage.d.ts +8 -2
  9. package/dist/features/magic-context/session-project-storage.d.ts.map +1 -1
  10. package/dist/features/magic-context/storage-meta-persisted.d.ts +12 -0
  11. package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
  12. package/dist/features/magic-context/storage-meta-session.d.ts +6 -0
  13. package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
  14. package/dist/features/magic-context/storage-tags.d.ts +2 -2
  15. package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
  16. package/dist/features/magic-context/transform-decision-log.d.ts +3 -1
  17. package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
  18. package/dist/hooks/magic-context/compartment-runner-types.d.ts +3 -5
  19. package/dist/hooks/magic-context/compartment-runner-types.d.ts.map +1 -1
  20. package/dist/hooks/magic-context/compartment-runner.d.ts +2 -1
  21. package/dist/hooks/magic-context/compartment-runner.d.ts.map +1 -1
  22. package/dist/hooks/magic-context/compartment-trigger.d.ts.map +1 -1
  23. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +2 -0
  24. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
  25. package/dist/hooks/magic-context/historian-drain-gate.d.ts +11 -0
  26. package/dist/hooks/magic-context/historian-drain-gate.d.ts.map +1 -0
  27. package/dist/hooks/magic-context/lkg-replay-fit.d.ts +118 -0
  28. package/dist/hooks/magic-context/lkg-replay-fit.d.ts.map +1 -0
  29. package/dist/hooks/magic-context/lkg-replay.d.ts +2 -0
  30. package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -1
  31. package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
  33. package/dist/hooks/magic-context/transform-compartment-phase.d.ts +3 -28
  34. package/dist/hooks/magic-context/transform-compartment-phase.d.ts.map +1 -1
  35. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  36. package/dist/{index-fg34g6a5.js → index-1j34a56j.js} +49 -8
  37. package/dist/{index-5pg0acf7.js → index-amycb7b1.js} +1 -1
  38. package/dist/{index-1mjhzqq7.js → index-dr0gepzw.js} +86 -23
  39. package/dist/{index-xt7y10x6.js → index-n0vnzz6h.js} +1 -1
  40. package/dist/{index-977gxfz0.js → index-ramz5kkm.js} +1 -1
  41. package/dist/{index-bdt9my9y.js → index-wtb9hzk6.js} +130 -50
  42. package/dist/index.js +1 -1
  43. package/dist/{message-time-backfill-zfernxbw.js → message-time-backfill-phrwp2fz.js} +1 -1
  44. package/dist/{read-session-chunk-hp464g13.js → read-session-chunk-82rkqa8j.js} +3 -3
  45. package/dist/{recomp-orchestrator-3qxeqwb9.js → recomp-orchestrator-vc5dkzy2.js} +6 -6
  46. package/dist/{send-session-notification-y29y9fsr.js → send-session-notification-rnc4nrsr.js} +3 -3
  47. package/dist/shared/harness.d.ts +1 -1
  48. package/dist/shared/harness.d.ts.map +1 -1
  49. package/dist/shared/historian-tool-expansions.d.ts +1 -1
  50. package/dist/shared/historian-tool-expansions.d.ts.map +1 -1
  51. package/dist/shared/historian-tool-template.d.ts +2 -2
  52. package/dist/shared/historian-tool-template.d.ts.map +1 -1
  53. package/dist/shared/model-resolution.d.ts +1 -1
  54. package/dist/shared/model-resolution.d.ts.map +1 -1
  55. package/dist/shared/sqlite.d.ts +14 -1
  56. package/dist/shared/sqlite.d.ts.map +1 -1
  57. package/dist/tools/ctx-expand/render.d.ts.map +1 -1
  58. package/dist/v2/fold/host-media.d.ts +4 -0
  59. package/dist/v2/fold/host-media.d.ts.map +1 -1
  60. package/dist/v2/fold/memory-cache.d.ts +70 -0
  61. package/dist/v2/fold/memory-cache.d.ts.map +1 -0
  62. package/dist/v2/fold/native-codec.d.ts +10 -0
  63. package/dist/v2/fold/native-codec.d.ts.map +1 -0
  64. package/dist/v2/fold/native-replay.d.ts +58 -0
  65. package/dist/v2/fold/native-replay.d.ts.map +1 -0
  66. package/dist/v2/fold/policy.d.ts +24 -0
  67. package/dist/v2/fold/policy.d.ts.map +1 -0
  68. package/dist/v2/hidden-child-record.d.ts +6 -0
  69. package/dist/v2/hidden-child-record.d.ts.map +1 -1
  70. package/dist/v2/hidden-completion.d.ts.map +1 -1
  71. package/dist/v2/hooks/context.d.ts.map +1 -1
  72. package/dist/v2/hooks/types.d.ts +6 -0
  73. package/dist/v2/hooks/types.d.ts.map +1 -1
  74. package/dist/v2/server.js +1763 -528
  75. package/dist/v2/store-reader.d.ts +16 -0
  76. package/dist/v2/store-reader.d.ts.map +1 -1
  77. package/dist/web/api.d.ts.map +1 -1
  78. package/dist/web/fixtures.d.ts +41 -0
  79. package/dist/web/fixtures.d.ts.map +1 -1
  80. package/dist/web/host-opencode.d.ts +9 -0
  81. package/dist/web/host-opencode.d.ts.map +1 -1
  82. package/dist/web/main.js +705 -53
  83. package/dist/web/memories.d.ts +18 -2
  84. package/dist/web/memories.d.ts.map +1 -1
  85. package/dist/web/ui/panel.css +2 -0
  86. package/dist/web/ui/panel.js +135 -77
  87. package/dist/web/views.d.ts +50 -9
  88. package/dist/web/views.d.ts.map +1 -1
  89. package/dist/{wrapup-orchestrator-wgb2nd19.js → wrapup-orchestrator-x9xamts2.js} +6 -6
  90. package/package.json +1 -1
  91. package/src/config/live-run-config.ts +28 -0
  92. package/src/shared/harness.ts +1 -1
  93. package/src/shared/historian-tool-defaults.json +6 -6
  94. package/src/shared/historian-tool-expansions.ts +65 -8
  95. package/src/shared/historian-tool-preview-defaults.json +13 -0
  96. package/src/shared/historian-tool-template.ts +48 -14
  97. package/src/shared/model-resolution.ts +1 -1
  98. package/src/shared/sqlite-acquisition-routing.test.ts +18 -0
  99. package/src/shared/sqlite.ts +100 -15
  100. package/src/shared/write-transaction-attribution.test.ts +2 -2
package/dist/v2/server.js CHANGED
@@ -7803,7 +7803,7 @@ class Parser {
7803
7803
  } else
7804
7804
  break;
7805
7805
  }
7806
- const expression = { steps, cap: 300 };
7806
+ const expression = { steps };
7807
7807
  if (this.take(".each(")) {
7808
7808
  if (relative)
7809
7809
  return this.error();
@@ -7869,9 +7869,23 @@ function compiled(source) {
7869
7869
  function oneLine(value) {
7870
7870
  return value.replace(/[\r\n\u2028\u2029]+/g, " ");
7871
7871
  }
7872
- function truncate(value, cap) {
7872
+ function truncate(value, cap, legacyPreview = false) {
7873
7873
  const chars = Array.from(oneLine(value));
7874
- return chars.length > cap ? `${chars.slice(0, cap).join("")}…` : chars.join("");
7874
+ if (cap === undefined || chars.length <= cap)
7875
+ return chars.join("");
7876
+ if (legacyPreview)
7877
+ return `${chars.slice(0, cap).join("")}…`;
7878
+ let end = 0;
7879
+ for (let i = 0;i < Math.min(cap, chars.length); i++) {
7880
+ if (!".!?。!?".includes(chars[i]))
7881
+ continue;
7882
+ let next = i + 1;
7883
+ while (next < chars.length && `"'”’)]`.includes(chars[next]))
7884
+ next++;
7885
+ if (next <= cap && ("。!?".includes(chars[i]) || next === chars.length || /[ \t]/.test(chars[next])))
7886
+ end = next;
7887
+ }
7888
+ return `${end ? `${chars.slice(0, end).join("")} ` : ""}[… ${chars.length - end} more characters]`;
7875
7889
  }
7876
7890
  function scalar(value) {
7877
7891
  if (value === undefined)
@@ -7888,10 +7902,11 @@ function compactJson(value) {
7888
7902
  function field(value, key) {
7889
7903
  return value !== null && typeof value === "object" && !Array.isArray(value) && Object.hasOwn(value, key) ? value[key] : undefined;
7890
7904
  }
7891
- function render(nodes, root) {
7905
+ function render(nodes, root, legacyPreview) {
7892
7906
  return nodes.map((node) => {
7893
7907
  if (typeof node === "string")
7894
7908
  return node;
7909
+ const cap = node.cap ?? (legacyPreview ? 300 : undefined);
7895
7910
  let value = root;
7896
7911
  let list = false;
7897
7912
  for (const step of node.steps) {
@@ -7905,19 +7920,19 @@ function render(nodes, root) {
7905
7920
  }
7906
7921
  }
7907
7922
  if (node.count)
7908
- return truncate(Array.isArray(value) ? String(value.length) : "", node.cap);
7923
+ return truncate(Array.isArray(value) ? String(value.length) : "", cap, legacyPreview);
7909
7924
  if (node.each || list || node.join !== undefined) {
7910
7925
  if (!Array.isArray(value))
7911
7926
  return "";
7912
- const elements = value.slice(0, 10).map((item) => truncate(node.each ? render(node.each, item) : scalar(item), 300));
7913
- if (value.length > 10)
7927
+ const elements = value.slice(0, legacyPreview ? 10 : undefined).map((item) => truncate(node.each ? render(node.each, item, legacyPreview) : scalar(item), legacyPreview ? 300 : undefined, legacyPreview));
7928
+ if (legacyPreview && value.length > 10)
7914
7929
  elements.push(`… +${value.length - 10} more`);
7915
- return truncate(elements.join(node.join ?? (node.each ? "; " : ", ")), node.cap);
7930
+ return truncate(elements.join(node.join ?? (node.each ? "; " : ", ")), cap, legacyPreview);
7916
7931
  }
7917
- return truncate(scalar(value), node.cap);
7932
+ return truncate(scalar(value), cap, legacyPreview);
7918
7933
  }).join("");
7919
7934
  }
7920
- function renderToolTemplate(source, input, output) {
7935
+ function renderToolTemplate(source, input, output, legacyPreview = false) {
7921
7936
  const nodes = compiled(source);
7922
7937
  if (!nodes)
7923
7938
  return null;
@@ -7932,8 +7947,8 @@ function renderToolTemplate(source, input, output) {
7932
7947
  }
7933
7948
  }
7934
7949
  const root = { input, output: structured };
7935
- const bareOutputNodes = nodes.map((node) => typeof node !== "string" && node.steps.length === 1 && ("field" in node.steps[0]) && node.steps[0].field === "output" && !node.each && !node.count && node.join === undefined ? truncate(text ?? "", node.cap) : node);
7936
- return truncate(render(bareOutputNodes, root), 1000);
7950
+ const bareOutputNodes = nodes.map((node) => typeof node !== "string" && node.steps.length === 1 && ("field" in node.steps[0]) && node.steps[0].field === "output" && !node.each && !node.count && node.join === undefined ? truncate(text ?? "", node.cap ?? (legacyPreview ? 300 : undefined), legacyPreview) : node);
7951
+ return truncate(render(bareOutputNodes, root, legacyPreview), legacyPreview ? 1000 : undefined, legacyPreview);
7937
7952
  }
7938
7953
  var templates;
7939
7954
  var init_historian_tool_template = __esm(() => {
@@ -9821,6 +9836,10 @@ function sampleLiveConfig(boot, fresh) {
9821
9836
  let value = fresh;
9822
9837
  for (const part of parts)
9823
9838
  value = value?.[part];
9839
+ if (value === undefined) {
9840
+ removeLeaf(result, parts);
9841
+ continue;
9842
+ }
9824
9843
  let target = result;
9825
9844
  for (const part of parts.slice(0, -1)) {
9826
9845
  const child = target[part];
@@ -9834,6 +9853,26 @@ function sampleLiveConfig(boot, fresh) {
9834
9853
  }
9835
9854
  return result;
9836
9855
  }
9856
+ function removeLeaf(root, parts) {
9857
+ const parents = parts.slice(0, -1);
9858
+ let node = root;
9859
+ for (const part of parents) {
9860
+ const child = node[part];
9861
+ if (!child || typeof child !== "object" || Array.isArray(child))
9862
+ return;
9863
+ node = child;
9864
+ }
9865
+ const leaf = parts.at(-1);
9866
+ if (leaf === undefined || !Object.hasOwn(node, leaf))
9867
+ return;
9868
+ let target = root;
9869
+ for (const part of parents) {
9870
+ const copy = { ...target[part] };
9871
+ target[part] = copy;
9872
+ target = copy;
9873
+ }
9874
+ delete target[leaf];
9875
+ }
9837
9876
  function changedLiveKeys(previous, next) {
9838
9877
  const at = (config, path) => path.split(".").reduce((node, part) => node?.[part], config);
9839
9878
  return LIVE_RELOAD_CONFIG_PATHS.filter((path) => JSON.stringify(at(previous, path)) !== JSON.stringify(at(next, path)));
@@ -10614,6 +10653,8 @@ function installTransactionRouting(db, readonly) {
10614
10653
  configurable: true,
10615
10654
  writable: true,
10616
10655
  value: (sql) => {
10656
+ if (/^\s*(?:COMMIT|END)\b/i.test(sql))
10657
+ assertTransformWrite();
10617
10658
  if (/^\s*BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)(?:\s+TRANSACTION)?\s*;?\s*$/i.test(sql)) {
10618
10659
  openWriterTransactions.delete(db);
10619
10660
  if (transformPassScope.getStore()?.active || backgroundWriterScope.getStore())
@@ -10822,6 +10863,15 @@ function getSqliteMemoryStats() {
10822
10863
  connections
10823
10864
  };
10824
10865
  }
10866
+ function closeReleasingStatements(close, args) {
10867
+ if (args[0] === true)
10868
+ return close(true);
10869
+ try {
10870
+ return close(true);
10871
+ } catch {
10872
+ return close(...args);
10873
+ }
10874
+ }
10825
10875
  function ownPreparedStatement(db, statement) {
10826
10876
  const finalizable = statement;
10827
10877
  if (typeof finalizable.finalize !== "function")
@@ -10845,7 +10895,7 @@ function ownPreparedStatement(db, statement) {
10845
10895
  }
10846
10896
  }
10847
10897
  references.clear();
10848
- const result = close(...args);
10898
+ const result = closeReleasingStatements(close, args);
10849
10899
  if (failure)
10850
10900
  throw failure;
10851
10901
  return result;
@@ -10895,6 +10945,10 @@ function withSqliteTransformPass(operation) {
10895
10945
  function withoutSqliteTransformPass(operation) {
10896
10946
  return transformPassScope.run(undefined, operation);
10897
10947
  }
10948
+ function assertTransformWrite() {
10949
+ if (!backgroundWriterScope.getStore())
10950
+ transformPassScope.getStore()?.guard?.assert();
10951
+ }
10898
10952
  function isTransientSqliteError(error) {
10899
10953
  if (!error || typeof error !== "object")
10900
10954
  return false;
@@ -10906,6 +10960,7 @@ function foregroundWaitLease() {
10906
10960
  return lease?.active && !admissionScope.getStore() && !backgroundWriterScope.getStore() ? lease : undefined;
10907
10961
  }
10908
10962
  function withAutocommitTimeout(db, operation) {
10963
+ assertTransformWrite();
10909
10964
  const lease = foregroundWaitLease();
10910
10965
  if (isInTransaction(db) || !lease && !backgroundWriterScope.getStore())
10911
10966
  return operation();
@@ -10926,6 +10981,7 @@ function withAutocommitTimeout(db, operation) {
10926
10981
  }
10927
10982
  }
10928
10983
  function acquireShort(db, acquire, site) {
10984
+ assertTransformWrite();
10929
10985
  const started = performance.now();
10930
10986
  const previous = pragmaNumber(db, "busy_timeout");
10931
10987
  const outer = writerAcquisitionScope.getStore();
@@ -10937,7 +10993,7 @@ function acquireShort(db, acquire, site) {
10937
10993
  let acquiredAt;
10938
10994
  try {
10939
10995
  const lease = foregroundWaitLease();
10940
- const timeout = lease ? Math.floor(lease.remainingWaitMs) : SHORT_BUSY_TIMEOUT_MS;
10996
+ const timeout = Math.floor(Math.min(lease ? lease.remainingWaitMs : SHORT_BUSY_TIMEOUT_MS, owner?.remainingMs?.() ?? lease?.guard?.remainingMs() ?? Infinity));
10941
10997
  db.exec(`PRAGMA busy_timeout=${timeout}`);
10942
10998
  acquire();
10943
10999
  acquiredAt = performance.now();
@@ -11010,29 +11066,53 @@ async function beginSqliteWriterAsync(db, site) {
11010
11066
  }
11011
11067
  }
11012
11068
  }
11013
- async function withAsyncPrivilegedWriter(db, operation) {
11069
+ async function withAsyncPrivilegedWriter(db, operation, options = {}) {
11014
11070
  const started = performance.now();
11071
+ const budgetMs = Math.max(0, Math.min(FOREGROUND_ACQUISITION_BUDGET_MS, options.budgetMs ?? Infinity));
11015
11072
  let attempts = 0;
11016
11073
  for (;; ) {
11074
+ options.signal?.throwIfAborted();
11075
+ options.assertCurrentPass?.();
11017
11076
  attempts++;
11077
+ let entered = false;
11018
11078
  try {
11019
11079
  const acquisition = {
11020
11080
  site: "privileged_writer",
11021
11081
  lane: "foreground",
11022
11082
  startedAt: started,
11023
11083
  attempts,
11024
- claimed: false
11084
+ claimed: false,
11085
+ remainingMs: () => Math.max(0, budgetMs - (performance.now() - started))
11025
11086
  };
11026
- return admissionScope.run(true, () => withSqliteTransformPass(() => writerAcquisitionScope.run(acquisition, () => withPrivilegedWriter(db, operation))));
11087
+ return admissionScope.run(true, () => withSqliteTransformPass(() => writerAcquisitionScope.run(acquisition, () => withPrivilegedWriter(db, () => {
11088
+ entered = true;
11089
+ return operation();
11090
+ }))));
11027
11091
  } catch (error) {
11028
- if (!(error instanceof SqliteAcquisitionBusyError))
11092
+ if (entered || !(error instanceof SqliteAcquisitionBusyError))
11029
11093
  throw error;
11094
+ options.beforeRetry?.(error);
11030
11095
  const elapsed = performance.now() - started;
11031
- if (elapsed >= FOREGROUND_ACQUISITION_BUDGET_MS) {
11096
+ if (elapsed >= budgetMs) {
11032
11097
  reportSqliteDiagnostic(`sqlite writer site=privileged_writer lane=foreground acquire_ms=${Math.round(elapsed)} attempts=${attempts} outcome=busy`);
11033
11098
  throw error;
11034
11099
  }
11035
- await new Promise((resolve) => setTimeout(resolve, Math.min(attempts === 1 ? 500 : 1000, FOREGROUND_ACQUISITION_BUDGET_MS - elapsed)));
11100
+ const backoff = attempts === 1 ? 500 : 1000;
11101
+ const delay = Math.min(options.jitter ? backoff * (0.8 + Math.random() * 0.4) : backoff, budgetMs - elapsed);
11102
+ await new Promise((resolve, reject) => {
11103
+ const abort = () => {
11104
+ clearTimeout(timer);
11105
+ options.signal?.removeEventListener("abort", abort);
11106
+ reject(options.signal?.reason ?? new Error("Writer wait aborted"));
11107
+ };
11108
+ const timer = setTimeout(() => {
11109
+ options.signal?.removeEventListener("abort", abort);
11110
+ resolve();
11111
+ }, delay);
11112
+ options.signal?.addEventListener("abort", abort, { once: true });
11113
+ if (options.signal?.aborted)
11114
+ abort();
11115
+ });
11036
11116
  }
11037
11117
  }
11038
11118
  }
@@ -11947,6 +12027,27 @@ var init_storage_memory_fts = __esm(async () => {
11947
12027
  function clearMemoryVerifications(db, memoryId) {
11948
12028
  db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?").run(memoryId);
11949
12029
  }
12030
+
12031
+ // src/features/magic-context/memory/types.ts
12032
+ var MEMORY_CATEGORY_RECORD, MEMORY_CATEGORIES;
12033
+ var init_types = __esm(() => {
12034
+ MEMORY_CATEGORY_RECORD = {
12035
+ PROJECT_RULES: true,
12036
+ ARCHITECTURE: true,
12037
+ CONFIG_VALUES: true,
12038
+ ARCHITECTURE_DECISIONS: true,
12039
+ CONSTRAINTS: true,
12040
+ CONFIG_DEFAULTS: true,
12041
+ NAMING: true,
12042
+ USER_PREFERENCES: true,
12043
+ USER_DIRECTIVES: true,
12044
+ ENVIRONMENT: true,
12045
+ WORKFLOW_RULES: true,
12046
+ KNOWN_ISSUES: true
12047
+ };
12048
+ MEMORY_CATEGORIES = Object.keys(MEMORY_CATEGORY_RECORD);
12049
+ });
12050
+
11950
12051
  // src/features/magic-context/memory/verification-paths.ts
11951
12052
  import { execFile } from "node:child_process";
11952
12053
  import { promisify } from "node:util";
@@ -11963,6 +12064,7 @@ var init_memory = __esm(async () => {
11963
12064
  init_project_identity();
11964
12065
  init_storage_memory();
11965
12066
  init_storage_memory_fts();
12067
+ init_types();
11966
12068
  init_verification_paths();
11967
12069
  });
11968
12070
 
@@ -14102,7 +14204,8 @@ var init_overflow_detection = __esm(() => {
14102
14204
  PREFIX_BOUND_THINKING_MODELS = [
14103
14205
  ["fable", 5, 1],
14104
14206
  ["opus", 5, 5],
14105
- ["sonnet", 5, 5]
14207
+ ["sonnet", 5, 5],
14208
+ ["haiku", 5, 5]
14106
14209
  ];
14107
14210
  PREFIX_BOUND_THINKING_PATTERN = new RegExp(`(?:^|[-_.:/])(?:${PREFIX_BOUND_THINKING_MODELS.map(([family, major, minor]) => `${family}[-_.]?${major}[-_.]${minor}`).join("|")})(?:$|[-_.:/@])`, "i");
14108
14211
  });
@@ -19574,6 +19677,24 @@ var init_storage_tags = __esm(() => {
19574
19677
  var historian_tool_defaults_default;
19575
19678
  var init_historian_tool_defaults = __esm(() => {
19576
19679
  historian_tool_defaults_default = {
19680
+ todowrite: 'Todos (${input.todos.count}): ${input.todos.each("${status}: ${content}")}',
19681
+ question: 'Asked user: ${input.questions[*].question.join(" / ")} → ${output}',
19682
+ task: "Task ${input.subagent_type}: ${input.prompt.truncate(600)} → ${output.truncate(200)}",
19683
+ ask: 'Asked: ${input.question} [options: ${input.options.join(" / ")}] ${input.text}${input.resolution} → ${output.truncate(300)}',
19684
+ peer_send: "PM ${input.recipient}: ${input.message}",
19685
+ board: 'Board ${input.verb}${input.board_lane}: ${input.ops.each("${op} ${lane_label} ${item}${props.text}${lane.items} ${state}${props.state}${lane.status}${id}${title}")}',
19686
+ room: "Room ${input.action} ${input.room_label}: ${input.room_detail}",
19687
+ work: "Work ${input.action} ${input.title}${input.id}: ${input.prompt.truncate(600)}${input.notes.truncate(600)} → ${output.truncate(160)}",
19688
+ knowhow: "Looked up how-to: ${input.query}${input.id}",
19689
+ mmc_note: "Note ${input.action} ${input.note_ids}: ${input.content} → ${output.truncate(160)}",
19690
+ mmc_memory: "Memory ${input.action} ${input.category} ${input.ids}: ${input.content} → ${output.truncate(160)}"
19691
+ };
19692
+ });
19693
+
19694
+ // src/shared/historian-tool-preview-defaults.json
19695
+ var historian_tool_preview_defaults_default;
19696
+ var init_historian_tool_preview_defaults = __esm(() => {
19697
+ historian_tool_preview_defaults_default = {
19577
19698
  todowrite: 'Todos (${input.todos.count}): ${input.todos.each("${status}: ${content}")}',
19578
19699
  question: 'Asked user: ${input.questions[*].question.join(" / ")} → ${output}',
19579
19700
  task: "Task ${input.subagent_type}: ${input.prompt} → ${output.truncate(200)}",
@@ -19589,20 +19710,52 @@ var init_historian_tool_defaults = __esm(() => {
19589
19710
  });
19590
19711
 
19591
19712
  // src/shared/historian-tool-expansions.ts
19592
- function expandToolPart(part, overrides) {
19713
+ function builtinInput(tool, input) {
19714
+ if (!input || typeof input !== "object")
19715
+ return input;
19716
+ const i = input;
19717
+ if (tool === "peer_send") {
19718
+ const external = i.external;
19719
+ const recipient = i.reply_to_pmid ? `reply to ${i.reply_to_pmid}` : i.agent || i.agent_id ? `to ${i.agent || i.agent_id}` : external?.session_id || i.session_id ? `to session ${external?.session_id || i.session_id}` : "to unknown recipient";
19720
+ return { ...i, recipient };
19721
+ }
19722
+ if (tool === "room") {
19723
+ return {
19724
+ ...i,
19725
+ room_label: i.room_id || i.title,
19726
+ room_detail: i.action === "create" || i.action === "invite" ? `invitees: ${renderToolTemplate("${input.invitees}", i)}` : i.text
19727
+ };
19728
+ }
19729
+ if (tool === "board") {
19730
+ return {
19731
+ ...i,
19732
+ board_lane: i.lane ? ` ${i.lane}` : "",
19733
+ ops: Array.isArray(i.ops) ? i.ops.map((op) => op && typeof op === "object" ? {
19734
+ ...op,
19735
+ lane_label: typeof op.lane === "string" ? op.lane : op.lane?.title || op.lane?.id
19736
+ } : op) : i.ops
19737
+ };
19738
+ }
19739
+ return input;
19740
+ }
19741
+ function expandToolPart(part, overrides, legacyPreview = false) {
19593
19742
  if (!part || typeof part !== "object")
19594
19743
  return null;
19595
19744
  const p = part;
19596
19745
  if (p.type !== "tool" || typeof p.tool !== "string")
19597
19746
  return null;
19598
- const template = overrides && Object.hasOwn(overrides, p.tool) ? overrides[p.tool] : Object.hasOwn(historian_tool_defaults_default, p.tool) ? historian_tool_defaults_default[p.tool] : undefined;
19747
+ const builtinDefaults = legacyPreview ? historian_tool_preview_defaults_default : historian_tool_defaults_default;
19748
+ const overridden = overrides && Object.hasOwn(overrides, p.tool);
19749
+ const template = overridden ? overrides[p.tool] : Object.hasOwn(builtinDefaults, p.tool) ? builtinDefaults[p.tool] : undefined;
19599
19750
  if (typeof template !== "string")
19600
19751
  return null;
19601
19752
  const state = p.state;
19602
- return renderToolTemplate(template, state?.input, state?.output ?? state?.error);
19753
+ const input = overridden || legacyPreview ? state?.input : builtinInput(p.tool, state?.input);
19754
+ return renderToolTemplate(template, input, state?.output ?? state?.error, legacyPreview);
19603
19755
  }
19604
19756
  var init_historian_tool_expansions = __esm(() => {
19605
19757
  init_historian_tool_defaults();
19758
+ init_historian_tool_preview_defaults();
19606
19759
  init_historian_tool_template();
19607
19760
  });
19608
19761
 
@@ -20935,20 +21088,22 @@ function fenceBoundaryForCompletedToolArcs(candidate, arcs, publicationFloorOrdi
20935
21088
  return minInvocation < publicationFloorOrdinal ? maxResult + 1 : minInvocation;
20936
21089
  }
20937
21090
  function fenceBoundaryForToolArcs(candidate, arcs, lastCompartmentEndOrdinal, recentOpenArcCutoff) {
20938
- const boundary = fenceBoundaryForCompletedToolArcs(candidate, arcs, lastCompartmentEndOrdinal + 1);
21091
+ let boundary = fenceBoundaryForCompletedToolArcs(candidate, arcs, lastCompartmentEndOrdinal + 1);
20939
21092
  for (const arc of arcs) {
20940
21093
  if (arc.resOrdinal !== null)
20941
21094
  continue;
20942
21095
  if (arc.invOrdinal < recentOpenArcCutoff)
20943
21096
  continue;
20944
21097
  if (arc.invOrdinal >= lastCompartmentEndOrdinal + 1 && arc.invOrdinal < boundary) {
20945
- return arc.invOrdinal;
21098
+ boundary = arc.invOrdinal;
21099
+ break;
20946
21100
  }
20947
21101
  if (arc.invOrdinal >= boundary) {
20948
- return arc.invOrdinal;
21102
+ boundary = arc.invOrdinal;
21103
+ break;
20949
21104
  }
20950
21105
  }
20951
- return boundary;
21106
+ return fenceBoundaryForCompletedToolArcs(boundary, arcs, 1);
20952
21107
  }
20953
21108
  function tokenForMessage(message, options) {
20954
21109
  const key = messageCacheKey(message, options);
@@ -21683,7 +21838,7 @@ function estimateFinalWireInputTokens(input) {
21683
21838
  total.toolCall += next.toolCall;
21684
21839
  return total;
21685
21840
  }, { conversation: 0, toolCall: 0 });
21686
- const measuredToolDefinitions = input.providerID && input.modelID ? getMeasuredToolDefinitionTokens(input.providerID, input.modelID, input.agentName) : undefined;
21841
+ const measuredToolDefinitions = input.toolDefinitionTokens ?? (input.providerID && input.modelID ? getMeasuredToolDefinitionTokens(input.providerID, input.modelID, input.agentName) : undefined);
21687
21842
  const calibration = resolveDecisionCalibration(input.providerID, input.modelID);
21688
21843
  const largestToolDefinitions = measuredToolDefinitions === undefined ? getLargestMeasuredToolDefinitionTokens() : undefined;
21689
21844
  const toolDefinitions = measuredToolDefinitions ?? (largestToolDefinitions === undefined ? undefined : Math.ceil(largestToolDefinitions * UNKNOWN_FIT_RATIO / (calibration.seeded ? calibration.toolsRatio : UNKNOWN_FIT_RATIO)));
@@ -25349,6 +25504,41 @@ function emergencyDrainExitThreshold(executeThresholdPercentage) {
25349
25504
  }
25350
25505
  return Math.max(0, executeThresholdPercentage - EMERGENCY_DRAIN_EXIT_MARGIN);
25351
25506
  }
25507
+ function resolvedEmergencyDrainLatch(meta, usagePercentage, executeThresholdPercentage, now) {
25508
+ const { forceMaterializationPercentage } = escalationBands(executeThresholdPercentage);
25509
+ if (usagePercentage >= forceMaterializationPercentage)
25510
+ return meta.emergencyDrainActive > 0 ? meta.emergencyDrainActive : now;
25511
+ if (usagePercentage < emergencyDrainExitThreshold(executeThresholdPercentage) || now - meta.emergencyDrainActive > EMERGENCY_DRAIN_MAX_LATCH_MS)
25512
+ return 0;
25513
+ return meta.emergencyDrainActive;
25514
+ }
25515
+ function drainFailureBackoffActive(meta, now) {
25516
+ return meta.historianDrainFailureAt > 0 && meta.historianDrainFailureAt <= now && now - meta.historianDrainFailureAt < EMERGENCY_DRAIN_FAILURE_BACKOFF_MS;
25517
+ }
25518
+ function getProtectedTailDrainBudgetSkip(args) {
25519
+ const now = args.now ?? Date.now();
25520
+ const meta = loadProtectedTailMeta(args.db, args.sessionId);
25521
+ const latch = resolvedEmergencyDrainLatch(meta, args.usagePercentage, args.executeThresholdPercentage, now);
25522
+ if (latch !== meta.emergencyDrainActive) {
25523
+ args.db.prepare("UPDATE session_meta SET emergency_drain_active = ? WHERE session_id = ? AND emergency_drain_active = ?").run(latch, args.sessionId, meta.emergencyDrainActive);
25524
+ }
25525
+ const startedAt = meta.protectedTailDrainWindowStartedAt;
25526
+ if (startedAt <= 0 || startedAt > now || now - startedAt >= DRAIN_WINDOW_MS)
25527
+ return null;
25528
+ const limitTokens = protectedTailWindowBudget(args.usagePercentage, args.usable, args.perRunCap);
25529
+ if (meta.protectedTailDrainTokens < limitTokens)
25530
+ return null;
25531
+ if (latch > 0 && !drainFailureBackoffActive(meta, now))
25532
+ return null;
25533
+ return {
25534
+ windowStartedAt: startedAt,
25535
+ resetsAt: startedAt + DRAIN_WINDOW_MS,
25536
+ nextEligibleAt: latch > 0 ? Math.min(startedAt + DRAIN_WINDOW_MS, meta.historianDrainFailureAt + EMERGENCY_DRAIN_FAILURE_BACKOFF_MS) : startedAt + DRAIN_WINDOW_MS,
25537
+ resetInMs: startedAt + DRAIN_WINDOW_MS - now,
25538
+ spentTokens: meta.protectedTailDrainTokens,
25539
+ limitTokens
25540
+ };
25541
+ }
25352
25542
  function reserveProtectedTailDrainTokens(args) {
25353
25543
  const now = args.now ?? Date.now();
25354
25544
  const requested = Math.max(0, Math.floor(args.trueRawTokens));
@@ -25380,17 +25570,7 @@ function reserveProtectedTailDrainTokens(args) {
25380
25570
  WHERE session_id = ?`).run(now, args.sessionId);
25381
25571
  meta = loadProtectedTailMeta(args.db, args.sessionId);
25382
25572
  }
25383
- const exitThreshold = emergencyDrainExitThreshold(args.executeThresholdPercentage);
25384
- let latchActiveSince = meta.emergencyDrainActive;
25385
- const { forceMaterializationPercentage } = escalationBands(args.executeThresholdPercentage);
25386
- if (args.usagePercentage >= forceMaterializationPercentage) {
25387
- if (latchActiveSince <= 0)
25388
- latchActiveSince = now;
25389
- } else if (latchActiveSince > 0) {
25390
- const expired = now - latchActiveSince > EMERGENCY_DRAIN_MAX_LATCH_MS;
25391
- if (args.usagePercentage < exitThreshold || expired)
25392
- latchActiveSince = 0;
25393
- }
25573
+ const latchActiveSince = resolvedEmergencyDrainLatch(meta, args.usagePercentage, args.executeThresholdPercentage, now);
25394
25574
  if (latchActiveSince !== meta.emergencyDrainActive) {
25395
25575
  args.db.prepare("UPDATE session_meta SET emergency_drain_active = ? WHERE session_id = ?").run(latchActiveSince, args.sessionId);
25396
25576
  }
@@ -25399,7 +25579,7 @@ function reserveProtectedTailDrainTokens(args) {
25399
25579
  const remaining = Math.max(0, budget - meta.protectedTailDrainTokens);
25400
25580
  let reserved = Math.min(requested, args.perRunCap, remaining);
25401
25581
  let bypass = false;
25402
- const inFailureBackoff = meta.historianDrainFailureAt > 0 && meta.historianDrainFailureAt <= now && now - meta.historianDrainFailureAt < EMERGENCY_DRAIN_FAILURE_BACKOFF_MS;
25582
+ const inFailureBackoff = drainFailureBackoffActive(meta, now);
25403
25583
  if (reserved <= 0 && latchActive && !inFailureBackoff) {
25404
25584
  reserved = Math.min(requested, args.perRunCap);
25405
25585
  bypass = true;
@@ -29841,7 +30021,9 @@ var init_storage_db = __esm(async () => {
29841
30021
  channel2HealStatements = new WeakMap;
29842
30022
  });
29843
30023
  // src/v2/store-reader.ts
30024
+ import { createHash as createHash13 } from "node:crypto";
29844
30025
  import { statSync as statSync8 } from "node:fs";
30026
+ import { hostname } from "node:os";
29845
30027
  import { resolve as resolve7 } from "node:path";
29846
30028
  function gaDatabasePath(dataHome, channel = "latest", env = process.env) {
29847
30029
  return resolveOpenCodeDbPath("v2", { dataHome, channel, env }).path;
@@ -29910,6 +30092,8 @@ function decode2(row) {
29910
30092
  return { ...row, data };
29911
30093
  }
29912
30094
  function connect(path, identity = "") {
30095
+ const stat = statSync8(path);
30096
+ const acquiredIdentity = identity || `${stat.dev}:${stat.ino}:${stat.birthtimeMs}`;
29913
30097
  const db = new Database(path, { readonly: true, fileMustExist: true });
29914
30098
  try {
29915
30099
  assertOpenCodeStoreGeneration(db, "v2", path);
@@ -29921,7 +30105,7 @@ function connect(path, identity = "") {
29921
30105
  counters.openReaders += 1;
29922
30106
  counters.readersOpened += 1;
29923
30107
  counters.maxOpenReaders = Math.max(counters.maxOpenReaders, counters.openReaders);
29924
- return { db, statements: new Map, identity, leases: 0, retired: false };
30108
+ return { db, statements: new Map, identity: acquiredIdentity, leases: 0, retired: false };
29925
30109
  }
29926
30110
  function closeConnection(connection) {
29927
30111
  connection.statements.clear();
@@ -29993,15 +30177,20 @@ class V2StoreReaderPool {
29993
30177
  }
29994
30178
 
29995
30179
  class V2StoreReader {
30180
+ path;
29996
30181
  pool;
29997
30182
  db;
29998
30183
  connection;
29999
30184
  closed = false;
30000
30185
  constructor(path, pool) {
30186
+ this.path = path;
30001
30187
  this.pool = pool;
30002
30188
  this.connection = pool ? pool.acquire(path) : connect(path);
30003
30189
  this.db = this.connection.db;
30004
30190
  }
30191
+ hostIdentity() {
30192
+ return createHash13("sha256").update(JSON.stringify([hostname(), resolve7(this.path), this.connection.identity])).digest("hex");
30193
+ }
30005
30194
  prepare(sql) {
30006
30195
  if (this.closed)
30007
30196
  throw new Error("Store reader is closed");
@@ -30097,6 +30286,10 @@ class V2StoreReader {
30097
30286
  return null;
30098
30287
  return typeof row.parent_id === "string" && row.parent_id.length > 0;
30099
30288
  }
30289
+ storedRowsAfter(sessionID, after) {
30290
+ const row = this.prepare("SELECT COUNT(*) AS count FROM session_message WHERE session_id = ? AND seq > ?").get(sessionID, after);
30291
+ return row.count;
30292
+ }
30100
30293
  storedMessageCount(sessionID) {
30101
30294
  return trackDecodeOperation("storedMessageCount", () => {
30102
30295
  const row = this.prepare("SELECT COUNT(*) AS count FROM session_message WHERE session_id = ?").get(sessionID);
@@ -30411,11 +30604,14 @@ function getSessionMetaSelectColumns(db) {
30411
30604
  sessionMetaSelectColumnsCache.set(db, projection);
30412
30605
  return projection;
30413
30606
  }
30414
- function getOrCreateSessionMeta(db, sessionId) {
30607
+ function findSessionMeta(db, sessionId) {
30415
30608
  const result = db.prepare(`SELECT ${getSessionMetaSelectColumns(db)} FROM session_meta WHERE session_id = ?`).get(sessionId);
30416
- if (isSessionMetaRow(result)) {
30417
- return toSessionMeta(result);
30418
- }
30609
+ return isSessionMetaRow(result) ? toSessionMeta(result) : undefined;
30610
+ }
30611
+ function getOrCreateSessionMeta(db, sessionId) {
30612
+ const existing = findSessionMeta(db, sessionId);
30613
+ if (existing)
30614
+ return existing;
30419
30615
  const harness = getHarness();
30420
30616
  const defaults = getDefaultSessionMeta(sessionId);
30421
30617
  const fallbackSubagent = harness === "opencode" ? resolveIsSubagentFromOpenCodeDb(sessionId) : harness === "opencode2" ? resolveIsSubagentFromOpenCode2Db(sessionId) : null;
@@ -30948,7 +31144,7 @@ var init_storage_v22_backfill_failures = __esm(() => {
30948
31144
  });
30949
31145
 
30950
31146
  // src/features/magic-context/workspaces.ts
30951
- import { createHash as createHash13 } from "node:crypto";
31147
+ import { createHash as createHash14 } from "node:crypto";
30952
31148
  function tableExists3(db, tableName) {
30953
31149
  const row = db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name = ? LIMIT 1").get(tableName);
30954
31150
  return Boolean(row);
@@ -31132,7 +31328,7 @@ function computeWorkspaceEpochFingerprint(db, identities) {
31132
31328
  const canonical = uniqueSorted(identities.filter((identity) => identity.length > 0));
31133
31329
  const epochs = getEpochMap(db, canonical);
31134
31330
  const shareCategories = selectWorkspaceShareCategories(db, canonical);
31135
- const hash = createHash13("sha256");
31331
+ const hash = createHash14("sha256");
31136
31332
  hash.update("share_categories", "utf8");
31137
31333
  hash.update("\x00");
31138
31334
  hash.update(shareCategories === null ? "NO_WORKSPACE" : JSON.stringify(shareCategories), "utf8");
@@ -33249,7 +33445,7 @@ var init_compartment_runner_types = __esm(() => {
33249
33445
  });
33250
33446
 
33251
33447
  // src/v2/hooks/hidden-child.ts
33252
- import { createHash as createHash14 } from "node:crypto";
33448
+ import { createHash as createHash15 } from "node:crypto";
33253
33449
  function hiddenAgentAllowedTools(agent) {
33254
33450
  return agent !== undefined && Object.hasOwn(AGENT_TOOL_ALLOWLIST, agent) ? AGENT_TOOL_ALLOWLIST[agent] : undefined;
33255
33451
  }
@@ -33337,7 +33533,7 @@ function markerCandidates(text) {
33337
33533
  function textDigest(text) {
33338
33534
  return {
33339
33535
  length: text.length,
33340
- sha256: createHash14("sha256").update(text).digest("hex").slice(0, 16)
33536
+ sha256: createHash15("sha256").update(text).digest("hex").slice(0, 16)
33341
33537
  };
33342
33538
  }
33343
33539
  function firstDifference(a, b) {
@@ -34040,10 +34236,10 @@ var init_shared = __esm(() => {
34040
34236
  });
34041
34237
 
34042
34238
  // src/features/magic-context/compaction-marker.ts
34043
- import { createHash as createHash15 } from "node:crypto";
34239
+ import { createHash as createHash16 } from "node:crypto";
34044
34240
  import { existsSync as existsSync11 } from "node:fs";
34045
34241
  function deterministicBase62(seed, length) {
34046
- let value = BigInt(`0x${createHash15("sha256").update(seed).digest("hex")}`);
34242
+ let value = BigInt(`0x${createHash16("sha256").update(seed).digest("hex")}`);
34047
34243
  const chars = Array(length);
34048
34244
  for (let index = length - 1;index >= 0; index -= 1) {
34049
34245
  chars[index] = BASE62_CHARS[Number(value % 62n)];
@@ -42166,12 +42362,14 @@ function applyHeadCap(args) {
42166
42362
  diagnostics.completedRefence.tokenMass = index.rangeTokens(Math.min(...component.map((arc) => arc.invocation)), Math.max(...component.map((arc) => arc.result)) + 1);
42167
42363
  });
42168
42364
  diagnostics.completedRefence.to = end;
42169
- diagnostics.eligibleEnd = end <= offset ? offset : Math.min(end, protectedTailStart);
42365
+ if (end > protectedTailStart)
42366
+ end = offset;
42367
+ diagnostics.eligibleEnd = end <= offset ? offset : end;
42170
42368
  args.observeDiagnostics?.(diagnostics);
42171
42369
  if (end <= offset && offset < protectedTailStart) {
42172
42370
  return { eligibleEndOrdinal: offset, oversizeAtomicUnit };
42173
42371
  }
42174
- return { eligibleEndOrdinal: Math.min(end, protectedTailStart), oversizeAtomicUnit };
42372
+ return { eligibleEndOrdinal: end, oversizeAtomicUnit };
42175
42373
  }
42176
42374
  function resolveProtectedTailBoundary(ctx) {
42177
42375
  const createdAt = ctx.createdAt ?? Date.now();
@@ -42326,6 +42524,7 @@ function resolveProtectedTailBoundary(ctx) {
42326
42524
  }
42327
42525
  protectedTailStart = clampOrdinal(protectedTailStart, rawMessageCount);
42328
42526
  protectedTailStart = retreatPastHostUnservedRows(messages, protectedTailStart, offset);
42527
+ protectedTailStart = Math.max(offset, fenceBoundaryForCompletedToolArcs(protectedTailStart, arcs, 1));
42329
42528
  const perRunCap = selectPerRunCap({
42330
42529
  usagePercentage,
42331
42530
  N: scaledN,
@@ -42345,7 +42544,8 @@ function resolveProtectedTailBoundary(ctx) {
42345
42544
  capTokens: perRunCap,
42346
42545
  recentOpenArcCutoff
42347
42546
  });
42348
- const eligibleEndOrdinal = retreatPastHostUnservedRows(messages, head.eligibleEndOrdinal, offset);
42547
+ const servedEndOrdinal = retreatPastHostUnservedRows(messages, head.eligibleEndOrdinal, offset);
42548
+ const eligibleEndOrdinal = Math.max(offset, fenceBoundaryForCompletedToolArcs(servedEndOrdinal, arcs, 1));
42349
42549
  const rawRangeFingerprint = computeRawRangeFingerprint(messages, offset, eligibleEndOrdinal);
42350
42550
  return {
42351
42551
  sessionId: ctx.sessionId,
@@ -42373,7 +42573,7 @@ function resolveProtectedTailBoundary(ctx) {
42373
42573
  cacheNamespace: ctx.cacheNamespace,
42374
42574
  createdAt,
42375
42575
  rawRangeFingerprint,
42376
- trueRawEligibleTokens: index.rangeTokens(offset, protectedTailStart),
42576
+ trueRawEligibleTokens: index.rangeTokens(offset, eligibleEndOrdinal),
42377
42577
  oversizeAtomicUnit: head.oversizeAtomicUnit,
42378
42578
  boundaryReason,
42379
42579
  diagnostics: boundaryDiagnostics({
@@ -42604,6 +42804,8 @@ function getRawHistoryEligibility(db, sessionId) {
42604
42804
  function hasRunnableCompartmentWindow(snapshot) {
42605
42805
  if (snapshot.offset >= snapshot.protectedTailStart)
42606
42806
  return false;
42807
+ if (snapshot.eligibleEndOrdinal <= snapshot.offset)
42808
+ return false;
42607
42809
  const forceMaterializationPercentage = escalationBands(snapshot.executeThresholdPercentage).forceMaterializationPercentage;
42608
42810
  if (snapshot.usagePercentage >= forceMaterializationPercentage || snapshot.emergencyTailScale) {
42609
42811
  return snapshot.trueRawEligibleTokens >= deriveMinForceEligibleTokens(snapshot.N) || snapshot.eligibleEndOrdinal > snapshot.offset;
@@ -44633,10 +44835,10 @@ function startLeaseRenewal(deps, holderId) {
44633
44835
  }
44634
44836
  }, COMPARTMENT_LEASE_RENEWAL_MS);
44635
44837
  }
44636
- function startCompartmentAgent(deps, runAgent = runCompartmentAgent) {
44637
- withoutSqliteTransformPass(() => startBackgroundCompartmentAgent(deps, runAgent));
44838
+ function startCompartmentAgent(deps, runAgent = runCompartmentAgent, prepareBoundary) {
44839
+ withoutSqliteTransformPass(() => startBackgroundCompartmentAgent(deps, runAgent, prepareBoundary));
44638
44840
  }
44639
- function startBackgroundCompartmentAgent(deps, runAgent) {
44841
+ function startBackgroundCompartmentAgent(deps, runAgent, prepareBoundary) {
44640
44842
  const existing = activeRuns.get(deps.sessionId);
44641
44843
  if (existing) {
44642
44844
  return;
@@ -44661,15 +44863,33 @@ function startBackgroundCompartmentAgent(deps, runAgent) {
44661
44863
  return;
44662
44864
  }
44663
44865
  const renewal = startLeaseRenewal(deps, holderId);
44664
- let realRunStarted = false;
44665
44866
  const runnerDeps = withPublishedCallback({
44666
44867
  ...deps,
44667
- compartmentLeaseHolderId: holderId,
44668
- onHistorianRunStarted: () => {
44669
- realRunStarted = true;
44670
- }
44868
+ compartmentLeaseHolderId: holderId
44671
44869
  });
44672
- const promise = runAgent(runnerDeps).catch((err) => {
44870
+ const promise = new Promise((resolve) => setTimeout(resolve, 0)).then(() => withRawSessionMessageCache(() => {
44871
+ const snapshot = deps.boundarySnapshot;
44872
+ try {
44873
+ primeTailRawMessageCache({
44874
+ sessionId: deps.sessionId,
44875
+ lastCompartmentEnd: snapshot?.lastCompartmentEndMessageId !== undefined ? snapshot.offset - 1 : getLastCompartmentEndMessage(deps.db, deps.sessionId),
44876
+ anchorMessageId: snapshot?.lastCompartmentEndMessageId !== undefined ? snapshot.lastCompartmentEndMessageId : getLastCompartmentEndMessageId(deps.db, deps.sessionId)
44877
+ });
44878
+ } catch (error) {
44879
+ sessionLog(deps.sessionId, "compartment agent: tail prime failed (non-fatal):", error);
44880
+ }
44881
+ if (prepareBoundary) {
44882
+ const boundary = prepareBoundary();
44883
+ if (!boundary) {
44884
+ updateSessionMeta(deps.db, deps.sessionId, {
44885
+ compartmentInProgress: false
44886
+ });
44887
+ return;
44888
+ }
44889
+ runnerDeps.boundarySnapshot = boundary;
44890
+ }
44891
+ return runAgent(runnerDeps);
44892
+ })).catch((err) => {
44673
44893
  sessionLog(deps.sessionId, "compartment agent: unhandled rejection:", err);
44674
44894
  try {
44675
44895
  updateSessionMeta(deps.db, deps.sessionId, { compartmentInProgress: false });
@@ -44686,9 +44906,6 @@ function startBackgroundCompartmentAgent(deps, runAgent) {
44686
44906
  }
44687
44907
  });
44688
44908
  activeRuns.set(deps.sessionId, { promise, published: false, kind: "incremental" });
44689
- if (!realRunStarted && activeRuns.get(deps.sessionId)?.promise === promise) {
44690
- activeRuns.delete(deps.sessionId);
44691
- }
44692
44909
  }
44693
44910
  async function executeContextRecompWithResult(deps, options = {}) {
44694
44911
  const { sessionId } = deps;
@@ -44756,11 +44973,13 @@ var init_compartment_runner = __esm(async () => {
44756
44973
  init_compartment_lease();
44757
44974
  init_logger();
44758
44975
  await __promiseAll([
44976
+ init_compartment_storage(),
44759
44977
  init_storage_meta(),
44760
44978
  init_sqlite(),
44761
44979
  init_compartment_runner_incremental(),
44762
44980
  init_compartment_runner_partial_recomp(),
44763
44981
  init_compartment_runner_recomp(),
44982
+ init_read_session_chunk(),
44764
44983
  init_compartment_runner_incremental()
44765
44984
  ]);
44766
44985
  activeRuns = new Map;
@@ -47586,13 +47805,54 @@ function isSuccessfulProviderCompletion(input) {
47586
47805
  }
47587
47806
 
47588
47807
  // src/hooks/magic-context/lkg-measured-request.ts
47808
+ import { createHash as createHash17 } from "node:crypto";
47589
47809
  function lkgProviderInputTotal(tokens) {
47590
47810
  const counts = [tokens?.input ?? 0, tokens?.cache?.read ?? 0, tokens?.cache?.write ?? 0];
47591
47811
  return counts.every((count) => Number.isSafeInteger(count) && count >= 0) ? counts.reduce((sum, count) => sum + count, 0) : Number.NaN;
47592
47812
  }
47813
+ function identity(slot) {
47814
+ return createHash17("sha256").update(JSON.stringify([
47815
+ slot.capturedAt,
47816
+ slot.captureSequence,
47817
+ slot.rowVersion,
47818
+ slot.modelKey,
47819
+ slot.providerKey,
47820
+ slot.inputIdSeq,
47821
+ slot.inputContentDigests
47822
+ ])).update(slot.jsonPrefix).digest("hex");
47823
+ }
47824
+ function envelope(modelKey, systemPromptTokens, agentName) {
47825
+ const slash = modelKey.indexOf("/");
47826
+ return JSON.stringify([
47827
+ modelKey,
47828
+ systemPromptTokens,
47829
+ agentName,
47830
+ getMeasuredToolDefinitionTokens(modelKey.slice(0, slash), modelKey.slice(slash + 1), agentName)
47831
+ ]);
47832
+ }
47593
47833
  function beginV2LkgRequest(sessionId, modelKey, previousResponseId) {
47594
47834
  requests.set(sessionId, { kind: "v2", previousResponseId, startedAt: Date.now(), modelKey });
47595
47835
  }
47836
+ function claimLkgRequestIdentity(sessionId) {
47837
+ const request = requests.get(sessionId);
47838
+ if (!request || claimedRequests.has(request))
47839
+ return;
47840
+ claimedRequests.add(request);
47841
+ return request;
47842
+ }
47843
+ function noteCapturedLkgRequest(args) {
47844
+ const request = args.request;
47845
+ if (!request || request.modelKey !== args.slot.modelKey) {
47846
+ captures.delete(args.sessionId);
47847
+ return;
47848
+ }
47849
+ captures.set(args.sessionId, {
47850
+ request,
47851
+ slot: identity(args.slot),
47852
+ prefixCount: JSON.parse(args.slot.jsonPrefix).length,
47853
+ envelope: envelope(request.modelKey, args.systemPromptTokens, args.agentName)
47854
+ });
47855
+ }
47596
47856
  function noteLkgProviderResponse(args) {
47597
47857
  if (!args.responseId || !args.modelKey)
47598
47858
  return;
@@ -47627,7 +47887,7 @@ var init_lkg_measured_request = __esm(() => {
47627
47887
  });
47628
47888
 
47629
47889
  // src/hooks/magic-context/lkg-persist.ts
47630
- import { createHash as createHash16 } from "node:crypto";
47890
+ import { createHash as createHash18 } from "node:crypto";
47631
47891
  function parseStringArray(value) {
47632
47892
  if (typeof value !== "string")
47633
47893
  return null;
@@ -47739,7 +47999,7 @@ function rememberFingerprint(db, sessionId, fingerprint) {
47739
47999
  saved.set(sessionId, fingerprint);
47740
48000
  }
47741
48001
  function slotFingerprint(slot) {
47742
- return createHash16("sha256").update(JSON.stringify([
48002
+ return createHash18("sha256").update(JSON.stringify([
47743
48003
  slot.jsonPrefix,
47744
48004
  slot.inputIdSeq,
47745
48005
  slot.inputContentDigests,
@@ -48192,7 +48452,7 @@ function captureLkgSlot(args) {
48192
48452
  if (!built)
48193
48453
  return false;
48194
48454
  const modelKeys = canonicalLkgModelKeys(args.modelKey, args.providerKey);
48195
- return captureSlot(args.sessionId, {
48455
+ const slot = {
48196
48456
  jsonPrefix: built.jsonPrefix,
48197
48457
  inputIdSeq: built.inputIdSeq,
48198
48458
  inputContentDigests: built.inputContentDigests,
@@ -48200,7 +48460,17 @@ function captureLkgSlot(args) {
48200
48460
  modelKey: modelKeys.modelKey,
48201
48461
  providerKey: modelKeys.providerKey,
48202
48462
  capturedAt: args.capturedAt ?? Date.now()
48203
- });
48463
+ };
48464
+ const captured = captureSlot(args.sessionId, slot);
48465
+ if (captured)
48466
+ noteCapturedLkgRequest({
48467
+ sessionId: args.sessionId,
48468
+ slot,
48469
+ request: built.anchorIndex === args.input.length - 1 ? claimLkgRequestIdentity(args.sessionId) : undefined,
48470
+ systemPromptTokens: args.systemPromptTokens ?? 0,
48471
+ agentName: args.agentName
48472
+ });
48473
+ return captured;
48204
48474
  }
48205
48475
  function entryIdsAreValid(slot, entryIds) {
48206
48476
  if (slot.inputIdSeq.length === 0 || entryIds.length < slot.inputIdSeq.length)
@@ -48427,9 +48697,58 @@ function replayLkg(args) {
48427
48697
  }
48428
48698
  var init_lkg_replay = __esm(() => {
48429
48699
  init_harness_provider_map();
48700
+ init_lkg_measured_request();
48430
48701
  init_lkg_slot();
48431
48702
  });
48432
48703
 
48704
+ // src/hooks/magic-context/lkg-replay-fit.ts
48705
+ function lkgReplayLimit(args) {
48706
+ const { db, sessionId, model } = args;
48707
+ const trusted = model ? resolveTrustedContextLimit(model.providerID, model.modelID, { db, sessionID: sessionId }) : undefined;
48708
+ if (trusted !== undefined && trusted > 0)
48709
+ return trusted;
48710
+ const detected = getOverflowState(db, sessionId, args.modelKey).detectedContextLimit;
48711
+ return detected > 0 ? detected : undefined;
48712
+ }
48713
+ function measureLkgReplay(args) {
48714
+ const baseline = args.measuredInputTokens ?? 0;
48715
+ const tailLimit = Math.max(0, args.limit - baseline);
48716
+ const proxy = wireContentBytes(args.messages, tailLimit * RAW_FALLBACK_BYTES_PER_CONTEXT_TOKEN, RAW_FALLBACK_BYTES_PER_CONTEXT_TOKEN);
48717
+ const skipped = { proxy, estimatorRan: false, trusted: false };
48718
+ if (proxy === null)
48719
+ return { fit: "unproven", tokens: null, ...skipped };
48720
+ const proxyTokens = baseline + Math.ceil(proxy.bytes / RAW_FALLBACK_BYTES_PER_CONTEXT_TOKEN);
48721
+ if (proxy.aborted || proxyTokens > args.limit) {
48722
+ return { fit: "over", tokens: null, proxyTokens, ...skipped };
48723
+ }
48724
+ let estimate;
48725
+ try {
48726
+ estimate = args.estimate();
48727
+ } catch {
48728
+ return { fit: "unproven", tokens: null, ...skipped };
48729
+ }
48730
+ const seen = { proxy, estimatorRan: true, trusted: estimate.trusted };
48731
+ const tokens = baseline + estimate.tokens;
48732
+ if (!estimate.trusted || !Number.isFinite(tokens) || tokens <= 0 || estimate.tokens < 0) {
48733
+ return {
48734
+ fit: "unproven",
48735
+ tokens: Number.isFinite(estimate.tokens) ? estimate.tokens : null,
48736
+ ...seen
48737
+ };
48738
+ }
48739
+ return tokens > args.limit ? { fit: "over", tokens, proxyTokens, ...seen } : { fit: "under", tokens, ...seen };
48740
+ }
48741
+ var RAW_FALLBACK_BYTES_PER_CONTEXT_TOKEN = 4;
48742
+ var init_lkg_replay_fit = __esm(async () => {
48743
+ init_final_wire_token_estimate();
48744
+ init_lkg_measured_request();
48745
+ init_lkg_slot();
48746
+ await __promiseAll([
48747
+ init_storage_meta_persisted(),
48748
+ init_event_resolvers()
48749
+ ]);
48750
+ });
48751
+
48433
48752
  // src/hooks/magic-context/storage-busy-refusal.ts
48434
48753
  function describeStorageBusyCause(cause) {
48435
48754
  let original = cause;
@@ -48584,7 +48903,7 @@ Some things on the desk are Magic Context's own markings, not conversation. \`<s
48584
48903
  });
48585
48904
 
48586
48905
  // src/hooks/magic-context/system-prompt-hash.ts
48587
- import { createHash as createHash17 } from "node:crypto";
48906
+ import { createHash as createHash19 } from "node:crypto";
48588
48907
  function clearSystemPromptHashSession(sessionId, handleMaps) {
48589
48908
  handleMaps.stickyDateBySession.delete(sessionId);
48590
48909
  handleMaps.cachedDocsBySession.delete(sessionId);
@@ -48678,7 +48997,7 @@ function createSystemPromptHashHandler(deps) {
48678
48997
  }
48679
48998
  const stickyDate = stickyDateBySession.get(sessionId);
48680
48999
  const stableCandidate = currentDate && stickyDate && currentDate !== stickyDate ? liveSystemContent.replace(DATE_PATTERN_ALL, stickyDate) : liveSystemContent;
48681
- const stableCandidateHash = createHash17("md5").update(promptSurfaceHashMaterial(stableCandidate, promptSurface.preset)).digest("hex");
49000
+ const stableCandidateHash = createHash19("md5").update(promptSurfaceHashMaterial(stableCandidate, promptSurface.preset)).digest("hex");
48682
49001
  const contentOrPresetChanged = hasPersistedHash && stableCandidateHash !== previousHash;
48683
49002
  const dateMayAdvance = isCacheBusting || idleCacheExpired || contentOrPresetChanged;
48684
49003
  if (currentDate && !stickyDate) {
@@ -48698,7 +49017,7 @@ function createSystemPromptHashHandler(deps) {
48698
49017
  `);
48699
49018
  if (!availability.frozen || !modelKey)
48700
49019
  return;
48701
- const currentHash = createHash17("md5").update(promptSurfaceHashMaterial(systemContent, promptSurface.preset)).digest("hex");
49020
+ const currentHash = createHash19("md5").update(promptSurfaceHashMaterial(systemContent, promptSurface.preset)).digest("hex");
48702
49021
  if (!sessionMetaEarly) {
48703
49022
  return;
48704
49023
  }
@@ -48937,10 +49256,14 @@ function recordSessionProjectIdentity(db, sessionId, projectPath) {
48937
49256
  return;
48938
49257
  const harness = getHarness();
48939
49258
  const now = Date.now();
49259
+ let previousProjectPath;
48940
49260
  db.transaction(() => {
49261
+ const existing = db.prepare("SELECT project_path FROM session_projects WHERE session_id = ? AND harness = ?").get(sessionId, harness);
49262
+ previousProjectPath = existing?.project_path;
48941
49263
  getUpsertSessionProjectStatement(db).run(sessionId, harness, projectPath, now);
48942
49264
  getRepairSessionChunkProjectStatement(db).run(projectPath, sessionId, harness, projectPath, SESSION_CHUNK_REPAIR_BATCH_SIZE);
48943
49265
  }).immediate();
49266
+ return previousProjectPath === projectPath ? undefined : { previousProjectPath, projectPath };
48944
49267
  }
48945
49268
  function hasRecordedSessionProjectIdentity(db, sessionId) {
48946
49269
  const row = db.prepare("SELECT 1 AS found FROM session_projects WHERE session_id = ? AND harness = ?").get(sessionId, getHarness());
@@ -48979,6 +49302,7 @@ function recordPendingTransformDecision(sessionId, decision) {
48979
49302
  }
48980
49303
  var canonicalReasons, piReasonAliases, sharedReasonAliases, pendingDecisionBySession, pendingPiDecisionBySession, lastBoundMessageIdBySession, scheduledWriteTokensBySession;
48981
49304
  var init_transform_decision_log = __esm(async () => {
49305
+ init_logger();
48982
49306
  await __promiseAll([
48983
49307
  init_sqlite(),
48984
49308
  init_storage_db()
@@ -49407,6 +49731,37 @@ var init_compaction_off_transition = __esm(async () => {
49407
49731
  };
49408
49732
  });
49409
49733
 
49734
+ // src/hooks/magic-context/historian-drain-gate.ts
49735
+ function isHistorianDrainBudgetSpent(args) {
49736
+ const target = deriveProtectedTailTokenTarget(args);
49737
+ const state = getProtectedTailDrainBudgetSkip({
49738
+ ...args,
49739
+ usable: target.usable,
49740
+ perRunCap: selectPerRunCap({ ...args, N: target.N })
49741
+ });
49742
+ if (!state)
49743
+ return false;
49744
+ let windows = loggedWindows.get(args.db);
49745
+ if (!windows) {
49746
+ windows = new Map;
49747
+ loggedWindows.set(args.db, windows);
49748
+ }
49749
+ if (windows.get(args.sessionId) !== state.resetsAt) {
49750
+ windows.set(args.sessionId, state.resetsAt);
49751
+ sessionLog(args.sessionId, `historian skip: internal drain budget spent (${state.spentTokens}/${state.limitTokens} tokens); next eligible at ${new Date(state.nextEligibleAt).toISOString()}`);
49752
+ }
49753
+ return true;
49754
+ }
49755
+ var loggedWindows;
49756
+ var init_historian_drain_gate = __esm(async () => {
49757
+ init_logger();
49758
+ await __promiseAll([
49759
+ init_storage_meta_persisted(),
49760
+ init_protected_tail_boundary()
49761
+ ]);
49762
+ loggedWindows = new WeakMap;
49763
+ });
49764
+
49410
49765
  // src/features/magic-context/storage-reasoning-removal.ts
49411
49766
  function invalidState(sessionId) {
49412
49767
  return new Error(`invalid persisted reasoning removal state for session ${sessionId}`);
@@ -50402,7 +50757,7 @@ function getUnsummarizedTailInfo(db, sessionId, triggerBudget, usage, executeThr
50402
50757
  taggerFloor,
50403
50758
  storedTotalsExcludeToolTags
50404
50759
  });
50405
- const hasProtectedEligibleHead = boundary.offset < boundary.protectedTailStart;
50760
+ const hasProtectedEligibleHead = boundary.offset < Math.min(boundary.protectedTailStart, boundary.eligibleEndOrdinal);
50406
50761
  if (!hasProtectedEligibleHead) {
50407
50762
  return {
50408
50763
  ...TAIL_INFO_DEFAULTS,
@@ -50414,7 +50769,7 @@ function getUnsummarizedTailInfo(db, sessionId, triggerBudget, usage, executeThr
50414
50769
  const seed = sessionDecisionCalibration(db, sessionId);
50415
50770
  const sourceRatio = Math.max(seed.proseRatio, seed.toolsRatio);
50416
50771
  const scanBudget = Math.floor(Math.max(MIN_PROACTIVE_TAIL_TOKEN_ESTIMATE, triggerBudget * TAIL_SIZE_TRIGGER_MULTIPLIER) / sourceRatio);
50417
- const chunk = readSessionChunk(sessionId, scanBudget, rawEligibility.offset, boundary.protectedTailStart);
50772
+ const chunk = readSessionChunk(sessionId, scanBudget, rawEligibility.offset, boundary.eligibleEndOrdinal);
50418
50773
  const isMeaningful = chunk.hasMore || boundary.trueRawEligibleTokens >= MIN_PROACTIVE_TAIL_TOKEN_ESTIMATE || Math.ceil(chunk.tokenEstimate * sourceRatio) >= MIN_PROACTIVE_TAIL_TOKEN_ESTIMATE || chunk.messageCount >= MIN_PROACTIVE_TAIL_MESSAGE_COUNT;
50419
50774
  return {
50420
50775
  nextStartOrdinal: rawEligibility.offset,
@@ -50441,6 +50796,14 @@ function checkCompartmentTrigger(db, sessionId, sessionMeta, usage, _previousPer
50441
50796
  sessionLog(sessionId, `compartment trigger: skipped — historian already in progress (usage=${usage.percentage.toFixed(1)}%)`);
50442
50797
  return { shouldFire: false };
50443
50798
  }
50799
+ if (isHistorianDrainBudgetSpent({
50800
+ db,
50801
+ sessionId,
50802
+ usagePercentage: usage.percentage,
50803
+ contextLimit: resolveBoundaryContextLimit(usage, contextLimit),
50804
+ executeThresholdPercentage
50805
+ }))
50806
+ return { shouldFire: false };
50444
50807
  const lazyInMemoryTail = typeof inMemoryTail === "function" ? inMemoryTail : undefined;
50445
50808
  let resolvedInMemoryTail = typeof inMemoryTail === "function" ? undefined : inMemoryTail;
50446
50809
  if (resolvedInMemoryTail) {
@@ -50587,6 +50950,7 @@ var init_compartment_trigger = __esm(async () => {
50587
50950
  await __promiseAll([
50588
50951
  init_compartment_storage(),
50589
50952
  init_storage(),
50953
+ init_historian_drain_gate(),
50590
50954
  init_protected_tail_boundary(),
50591
50955
  init_read_session_chunk(),
50592
50956
  init_reasoning_budget()
@@ -51002,8 +51366,24 @@ function historianJoinFailClosedMessage(input) {
51002
51366
  return `historian did not complete within ${seconds} s: ${reason}`;
51003
51367
  }
51004
51368
  function runCompartmentPhase(args) {
51369
+ if (args.historianRunnable !== false && !args.compactionOff && args.canRunCompartments && !getActiveCompartmentRun(args.sessionId) && (args.sessionMeta.compartmentInProgress || args.contextUsage.percentage >= BLOCK_UNTIL_DONE_PERCENTAGE) && isHistorianDrainBudgetSpent({
51370
+ db: args.db,
51371
+ sessionId: args.sessionId,
51372
+ contextLimit: args.boundaryContextLimit,
51373
+ executeThresholdPercentage: args.boundaryExecuteThresholdPercentage,
51374
+ usagePercentage: args.boundaryUsage.percentage
51375
+ })) {
51376
+ if (args.sessionMeta.compartmentInProgress) {
51377
+ updateSessionMeta(args.db, args.sessionId, { compartmentInProgress: false });
51378
+ }
51379
+ return runCompartmentPhaseImpl({
51380
+ ...args,
51381
+ canRunCompartments: false,
51382
+ sessionMeta: { compartmentInProgress: false }
51383
+ });
51384
+ }
51005
51385
  const historianRunnable = args.historianRunnable !== false;
51006
- const willReadRawHistory = historianRunnable && !args.compactionOff && args.canRunCompartments && getActiveCompartmentRun(args.sessionId) === undefined && (args.sessionMeta.compartmentInProgress || args.contextUsage.percentage >= BLOCK_UNTIL_DONE_PERCENTAGE);
51386
+ const willReadRawHistory = historianRunnable && !args.compactionOff && args.canRunCompartments && getActiveCompartmentRun(args.sessionId) === undefined && args.contextUsage.percentage >= BLOCK_UNTIL_DONE_PERCENTAGE;
51007
51387
  if (!willReadRawHistory) {
51008
51388
  return runCompartmentPhaseImpl(args);
51009
51389
  }
@@ -51129,7 +51509,7 @@ async function runCompartmentPhaseImpl(args) {
51129
51509
  compartmentInProgress = false;
51130
51510
  }
51131
51511
  if (historianRunnable && args.canRunCompartments && args.sessionMeta.compartmentInProgress && !getActiveCompartmentRun(args.sessionId)) {
51132
- if (!hasEligibleHistoryForCompartment()) {
51512
+ if (args.contextUsage.percentage >= BLOCK_UNTIL_DONE_PERCENTAGE && !hasEligibleHistoryForCompartment()) {
51133
51513
  sessionLog(args.sessionId, `transform: skipping compartment start, no eligible history before protected tail (beyond ${lastObservedCompartmentEnd})`);
51134
51514
  updateSessionMeta(args.db, args.sessionId, { compartmentInProgress: false });
51135
51515
  compartmentInProgress = false;
@@ -51146,7 +51526,7 @@ async function runCompartmentPhaseImpl(args) {
51146
51526
  db: args.db,
51147
51527
  sessionId: args.sessionId,
51148
51528
  historianChunkTokens: args.historianChunkTokens,
51149
- boundarySnapshot: getBoundarySnapshotForCompartment() ?? undefined,
51529
+ boundarySnapshot: args.preResolvedBoundarySnapshot,
51150
51530
  currentContextLimit: args.boundaryContextLimit,
51151
51531
  historyBudgetTokens: args.historyBudgetTokens,
51152
51532
  historianTimeoutMs: args.historianTimeoutMs,
@@ -51164,6 +51544,12 @@ async function runCompartmentPhaseImpl(args) {
51164
51544
  autoPromote: args.autoPromote,
51165
51545
  onCompartmentStatePublished: args.onCompartmentStatePublished,
51166
51546
  preserveInjectionCacheUntilConsumed: true
51547
+ }, undefined, () => {
51548
+ const snapshot = getBoundarySnapshotForCompartment();
51549
+ if (snapshot && hasRunnableCompartmentWindow(snapshot))
51550
+ return snapshot;
51551
+ sessionLog(args.sessionId, `transform: skipping compartment start, no eligible history before protected tail (beyond ${lastObservedCompartmentEnd})`);
51552
+ return null;
51167
51553
  });
51168
51554
  compartmentInProgress = true;
51169
51555
  }
@@ -51237,6 +51623,7 @@ var init_transform_compartment_phase = __esm(async () => {
51237
51623
  init_sqlite(),
51238
51624
  init_compartment_runner(),
51239
51625
  init_compartment_trigger(),
51626
+ init_historian_drain_gate(),
51240
51627
  init_inject_compartments(),
51241
51628
  init_protected_tail_boundary(),
51242
51629
  init_read_session_chunk(),
@@ -55062,19 +55449,33 @@ function createTransform(deps) {
55062
55449
  if (false) {}
55063
55450
  return false;
55064
55451
  };
55065
- const startRecoveryRun = () => {
55066
- const scale = emergencyUsagePercentageEarly >= 95 ? 0.25 : 0.5;
55067
- let boundarySnapshot = getRunnableBoundaryForCompartment();
55068
- if (!boundarySnapshot || !hasRunnableCompartmentWindow(boundarySnapshot)) {
55069
- boundarySnapshot = getRunnableBoundaryForCompartment(scale);
55070
- }
55071
- if (false) {}
55072
- if (!canRunCompartments || !deps.client && !deps.hiddenCompletionExecutor || !boundarySnapshot || !hasRunnableCompartmentWindow(boundarySnapshot)) {
55452
+ const startRecoveryRun = (onReady) => {
55453
+ if (isHistorianDrainBudgetSpent({
55454
+ db,
55455
+ sessionId,
55456
+ contextLimit: boundaryContextLimit,
55457
+ executeThresholdPercentage: boundaryExecuteThreshold,
55458
+ usagePercentage: boundaryUsageForProtectedTail.percentage
55459
+ }))
55073
55460
  return false;
55074
- }
55075
- if (getActiveCompartmentRun(sessionId)) {
55461
+ if (!canRunCompartments || !deps.client && !deps.hiddenCompletionExecutor || getActiveCompartmentRun(sessionId))
55462
+ return false;
55463
+ const prepareRecoveryBoundary = () => {
55464
+ const scale = emergencyUsagePercentageEarly >= 95 ? 0.25 : 0.5;
55465
+ let boundarySnapshot = getRunnableBoundaryForCompartment();
55466
+ if (!boundarySnapshot || !hasRunnableCompartmentWindow(boundarySnapshot)) {
55467
+ boundarySnapshot = getRunnableBoundaryForCompartment(scale);
55468
+ }
55469
+ if (false) {}
55470
+ if (!boundarySnapshot || !hasRunnableCompartmentWindow(boundarySnapshot))
55471
+ return null;
55472
+ onReady?.();
55473
+ return boundarySnapshot;
55474
+ };
55475
+ const urgent = emergencyUsagePercentageEarly >= 95;
55476
+ const boundarySnapshot = urgent ? prepareRecoveryBoundary() : undefined;
55477
+ if (urgent && !boundarySnapshot)
55076
55478
  return false;
55077
- }
55078
55479
  updateSessionMeta(db, sessionId, { compartmentInProgress: true });
55079
55480
  startCompartmentAgent({
55080
55481
  client: deps.client,
@@ -55083,7 +55484,7 @@ function createTransform(deps) {
55083
55484
  db,
55084
55485
  sessionId,
55085
55486
  historianChunkTokens: historianRun?.chunkTokens ?? deps.getHistorianChunkTokens?.() ?? 20000,
55086
- boundarySnapshot,
55487
+ boundarySnapshot: boundarySnapshot ?? undefined,
55087
55488
  currentContextLimit: boundaryContextLimit,
55088
55489
  historyBudgetTokens,
55089
55490
  historianTimeoutMs: historianRun?.timeoutMs ?? deps.historianTimeoutMs,
@@ -55103,7 +55504,7 @@ function createTransform(deps) {
55103
55504
  deferredHistoryRefreshSessions.add(sid);
55104
55505
  deferredMaterializationSessions.add(sid);
55105
55506
  }
55106
- });
55507
+ }, undefined, urgent ? undefined : prepareRecoveryBoundary);
55107
55508
  return true;
55108
55509
  };
55109
55510
  if (fullFeatureMode && !compactionOff && historianFailureState.failureCount > 0 && emergencyUsagePercentageEarly >= 95 && !recoveryNoHeadEscapeActive) {
@@ -55117,13 +55518,15 @@ function createTransform(deps) {
55117
55518
  sessionLog(sessionId, "transform: emergency recovery remains armed — no complete eligible head before protected tail");
55118
55519
  }
55119
55520
  sessionLog(sessionId, `EMERGENCY: historian recovery requested at ${emergencyPercentage}%, failures: ${historianFailureState.failureCount}`);
55120
- } else if (fullFeatureMode && !compactionOff && isFirstTransformPassForSession && historianFailureState.failureCount > 0 && getEligibleHistoryForCompartment() && startRecoveryRun()) {
55121
- sessionLog(sessionId, `transform: historian recovery triggered on session load after ${historianFailureState.failureCount} failure(s)`);
55122
- if (deps.client) {
55123
- withoutSqliteTransformPass(() => sendStatusNotification(deps.client, sessionId, `## Historian recovery
55521
+ } else if (fullFeatureMode && !compactionOff && isFirstTransformPassForSession && historianFailureState.failureCount > 0) {
55522
+ startRecoveryRun(() => {
55523
+ sessionLog(sessionId, `transform: historian recovery triggered on session load after ${historianFailureState.failureCount} failure(s)`);
55524
+ if (deps.client) {
55525
+ withoutSqliteTransformPass(() => sendStatusNotification(deps.client, sessionId, `## Historian recovery
55124
55526
 
55125
55527
  Historian previously failed ${historianFailureState.failureCount} time(s), so Magic Context is retrying history comparting immediately after restart.`, notificationParams));
55126
- }
55528
+ }
55529
+ });
55127
55530
  }
55128
55531
  logTransformTiming(sessionId, "emergencyRecoveryBlock", tFirstPass);
55129
55532
  const memoryProjectDirectory = compartmentDirectory || process.cwd();
@@ -55670,7 +56073,9 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
55670
56073
  input: lkgInput,
55671
56074
  output: messages,
55672
56075
  modelKey,
55673
- providerKey
56076
+ providerKey,
56077
+ systemPromptTokens: sessionMeta.systemPromptTokens,
56078
+ agentName: notificationParams?.agent
55674
56079
  });
55675
56080
  if (captured) {
55676
56081
  const capturedSlot = getInMemorySlot(sessionId);
@@ -55964,6 +56369,7 @@ var init_transform = __esm(async () => {
55964
56369
  init_ctx_reduce_availability(),
55965
56370
  init_degraded_pass_refusal(),
55966
56371
  init_event_resolvers(),
56372
+ init_historian_drain_gate(),
55967
56373
  init_inject_compartments(),
55968
56374
  init_protected_tail_boundary(),
55969
56375
  init_read_session_chunk(),
@@ -58598,7 +59004,8 @@ function hostUsesMediaAssets() {
58598
59004
  schemaUsesAssets.set(messageClass, uses);
58599
59005
  return uses;
58600
59006
  }
58601
- function decodeThroughHostSchema(source) {
59007
+ function decodeThroughHostSchema(encoded) {
59008
+ const source = encoded.source;
58602
59009
  const messageClass = rememberedMessageClass;
58603
59010
  if (!messageClass)
58604
59011
  return "no host message class seen";
@@ -58618,7 +59025,7 @@ function decodeThroughHostSchema(source) {
58618
59025
  const run = decode?.run;
58619
59026
  if (typeof run !== "function")
58620
59027
  return "host media schema has no decode transformation";
58621
- const result = run.call(decode, { _tag: "Some", value: { source } }, {});
59028
+ const result = run.call(decode, { _tag: "Some", value: encoded }, {});
58622
59029
  const option = result?._tag === "Success" ? result.value : undefined;
58623
59030
  const asset = option?._tag === "Some" ? option.value : undefined;
58624
59031
  if (!isClassInstance(asset) || asset.source !== source)
@@ -58626,18 +59033,30 @@ function decodeThroughHostSchema(source) {
58626
59033
  return asset;
58627
59034
  }
58628
59035
  function hostMediaAsset(data, mediaType) {
58629
- const source = { type: "base64", data, mediaType };
59036
+ return hostMediaFromEncoded({ source: { type: "base64", data, mediaType } });
59037
+ }
59038
+ function hostMediaFromEncoded(value) {
59039
+ if (!isRecord6(value.source))
59040
+ return "missing Media.Asset source";
59041
+ const encoded = structuredClone(value);
59042
+ if (encoded.source.type === "bytes") {
59043
+ if (typeof encoded.source.data !== "string")
59044
+ return "bytes media has no JSON codec payload";
59045
+ encoded.source.data = new Uint8Array(Buffer.from(encoded.source.data, "base64"));
59046
+ }
59047
+ if (!["bytes", "base64", "url", "ref"].includes(String(encoded.source.type)))
59048
+ return "unknown Media.Asset source";
58630
59049
  let first = "no Media.Asset seen in this process";
58631
59050
  if (rememberedAssetClass) {
58632
59051
  try {
58633
- return new rememberedAssetClass({ source });
59052
+ return new rememberedAssetClass(encoded);
58634
59053
  } catch (error) {
58635
59054
  first = `remembered asset class threw: ${String(error)}`;
58636
59055
  }
58637
59056
  }
58638
59057
  let second;
58639
59058
  try {
58640
- const decoded = decodeThroughHostSchema(source);
59059
+ const decoded = decodeThroughHostSchema(encoded);
58641
59060
  if (typeof decoded !== "string")
58642
59061
  return decoded;
58643
59062
  second = decoded;
@@ -58663,7 +59082,7 @@ var init_markers = __esm(() => {
58663
59082
  });
58664
59083
 
58665
59084
  // src/v2/fold/owner.ts
58666
- import { createHash as createHash18 } from "node:crypto";
59085
+ import { createHash as createHash20 } from "node:crypto";
58667
59086
 
58668
59087
  class FoldOwner {
58669
59088
  storage;
@@ -58742,9 +59161,638 @@ class FoldOwner {
58742
59161
  });
58743
59162
  }
58744
59163
  }
58745
- var foldDigest = (value) => createHash18("sha256").update(value).digest("hex");
59164
+ var foldDigest = (value) => createHash20("sha256").update(value).digest("hex");
58746
59165
  var init_owner = () => {};
58747
59166
 
59167
+ // src/v2/fold/memory-cache.ts
59168
+ import { randomUUID as randomUUID4 } from "node:crypto";
59169
+
59170
+ class NativeFoldCache {
59171
+ resolveDB;
59172
+ sourceID = randomUUID4();
59173
+ tails = new Map;
59174
+ nativeRows = new Map;
59175
+ admissions = new Map;
59176
+ retention = new Map;
59177
+ evictionListeners = new Set;
59178
+ budget;
59179
+ constructor(resolveDB, budget = DEFAULT_NATIVE_FOLD_BUDGET) {
59180
+ this.resolveDB = resolveDB;
59181
+ if (!Number.isSafeInteger(budget.maxSessionBytes) || budget.maxSessionBytes < 1 || !Number.isSafeInteger(budget.maxSessions) || budget.maxSessions < 1)
59182
+ throw new Error("Native fold cache budgets must be positive integers");
59183
+ this.budget = { ...budget };
59184
+ }
59185
+ onEvict(listener) {
59186
+ this.evictionListeners.add(listener);
59187
+ }
59188
+ touch(sessionID) {
59189
+ const state = this.retention.get(sessionID) ?? { active: 0, bytes: 0, replayBytes: 0 };
59190
+ this.retention.delete(sessionID);
59191
+ this.retention.set(sessionID, state);
59192
+ return state;
59193
+ }
59194
+ hold(sessionID) {
59195
+ const state = this.touch(sessionID);
59196
+ state.active++;
59197
+ return () => {
59198
+ state.active--;
59199
+ this.enforceBudget();
59200
+ };
59201
+ }
59202
+ accountReplay(sessionID, bytes) {
59203
+ this.touch(sessionID).replayBytes = bytes;
59204
+ this.recount(sessionID);
59205
+ }
59206
+ usage() {
59207
+ return {
59208
+ sessions: this.retention.size,
59209
+ bytes: [...this.retention.values()].reduce((total, state) => total + state.bytes, 0)
59210
+ };
59211
+ }
59212
+ recount(sessionID) {
59213
+ const state = this.touch(sessionID);
59214
+ let bytes = 256 + stringBytes(sessionID) + state.replayBytes;
59215
+ const tail = this.tails.get(sessionID);
59216
+ if (tail)
59217
+ bytes += 128 + stringBytes(tail.data) + stringBytes(tail.digest);
59218
+ for (const row of this.nativeRows.get(sessionID)?.values() ?? [])
59219
+ bytes += 256 + stringBytes(row.id) + stringBytes(row.type) + stringBytes(row.data) + stringBytes(row.digest);
59220
+ for (const row of this.admissions.get(sessionID)?.values() ?? [])
59221
+ bytes += 256 + stringBytes(row.admission_id) + stringBytes(row.status) + stringBytes(row.owner_token) + stringBytes(row.data) + stringBytes(row.payload) + stringBytes(row.digest);
59222
+ state.bytes = bytes;
59223
+ this.enforceBudget();
59224
+ }
59225
+ enforceBudget() {
59226
+ for (const [id, state] of this.retention)
59227
+ if (!state.active && state.bytes > this.budget.maxSessionBytes)
59228
+ this.evict(id);
59229
+ for (const [id, state] of this.retention) {
59230
+ if (this.retention.size <= this.budget.maxSessions)
59231
+ break;
59232
+ if (!state.active)
59233
+ this.evict(id);
59234
+ }
59235
+ }
59236
+ evict(sessionID) {
59237
+ this.tails.delete(sessionID);
59238
+ this.nativeRows.delete(sessionID);
59239
+ this.admissions.delete(sessionID);
59240
+ this.retention.delete(sessionID);
59241
+ for (const listener of this.evictionListeners)
59242
+ listener(sessionID);
59243
+ }
59244
+ get db() {
59245
+ return this.resolveDB();
59246
+ }
59247
+ tail(sessionID) {
59248
+ const record = this.tails.get(sessionID);
59249
+ if (record)
59250
+ this.touch(sessionID);
59251
+ return record && { ...record };
59252
+ }
59253
+ async saveTail(sessionID, data, rows) {
59254
+ this.tails.set(sessionID, {
59255
+ revision: (this.tails.get(sessionID)?.revision ?? -1) + 1,
59256
+ data,
59257
+ digest: foldDigest(data)
59258
+ });
59259
+ const kept = this.nativeRows.get(sessionID) ?? new Map;
59260
+ for (const row of rows)
59261
+ kept.set(row.id, Object.freeze({ ...row, revision: (kept.get(row.id)?.revision ?? -1) + 1 }));
59262
+ this.nativeRows.set(sessionID, kept);
59263
+ this.recount(sessionID);
59264
+ }
59265
+ replaceRows(sessionID, through, rows, after = -1) {
59266
+ const kept = this.nativeRows.get(sessionID) ?? new Map;
59267
+ for (const [id, row] of kept)
59268
+ if (row.seq > after && row.seq <= through)
59269
+ kept.delete(id);
59270
+ for (const row of rows)
59271
+ kept.set(row.id, Object.freeze({ ...row, revision: 0 }));
59272
+ this.nativeRows.set(sessionID, kept);
59273
+ this.recount(sessionID);
59274
+ }
59275
+ truncate(sessionID, through) {
59276
+ const kept = this.nativeRows.get(sessionID);
59277
+ if (kept) {
59278
+ for (const [id, row] of kept)
59279
+ if (row.seq > through)
59280
+ kept.delete(id);
59281
+ }
59282
+ if (kept)
59283
+ this.recount(sessionID);
59284
+ }
59285
+ sequenceForID(sessionID, id) {
59286
+ return this.nativeRows.get(sessionID)?.get(id)?.seq;
59287
+ }
59288
+ containsRows(sessionID, ids) {
59289
+ const kept = this.nativeRows.get(sessionID);
59290
+ return ids.every((id) => kept?.has(id));
59291
+ }
59292
+ rows(sessionID, after, through) {
59293
+ if (this.retention.has(sessionID))
59294
+ this.touch(sessionID);
59295
+ return [...this.nativeRows.get(sessionID)?.values() ?? []].filter((row) => row.seq > after && row.seq <= through).sort((a, b) => a.seq - b.seq).map((row) => ({ ...row }));
59296
+ }
59297
+ admission(sessionID, id) {
59298
+ const record = this.admissions.get(sessionID)?.get(id);
59299
+ if (record)
59300
+ this.touch(sessionID);
59301
+ return record && { ...record };
59302
+ }
59303
+ put(sessionID, id, record) {
59304
+ const kept = this.admissions.get(sessionID) ?? new Map;
59305
+ kept.set(id, Object.freeze({ ...record }));
59306
+ this.admissions.set(sessionID, kept);
59307
+ this.recount(sessionID);
59308
+ }
59309
+ async supply(sessionID, id, payload) {
59310
+ const old = this.admission(sessionID, id);
59311
+ this.put(sessionID, id, {
59312
+ admission_id: id,
59313
+ revision: (old?.revision ?? -1) + 1,
59314
+ status: old?.status ?? "supplied",
59315
+ owner_token: old?.owner_token ?? null,
59316
+ data: old?.data ?? "{}",
59317
+ payload,
59318
+ digest: foldDigest(payload),
59319
+ logged_at: old?.logged_at ?? null
59320
+ });
59321
+ }
59322
+ latestAttempt(sessionID) {
59323
+ const record = [...this.admissions.get(sessionID)?.values() ?? []].reverse().find((row) => JSON.parse(row.data).after !== undefined);
59324
+ return record && { ...record };
59325
+ }
59326
+ async claim(sessionID, id, data, owner, _now) {
59327
+ const old = this.admission(sessionID, id);
59328
+ if (old?.owner_token || old?.logged_at !== null && old?.logged_at !== undefined)
59329
+ return false;
59330
+ this.put(sessionID, id, {
59331
+ admission_id: id,
59332
+ revision: 0,
59333
+ status: "pending",
59334
+ owner_token: owner,
59335
+ data,
59336
+ payload: old?.payload ?? null,
59337
+ digest: old?.digest ?? null,
59338
+ logged_at: null
59339
+ });
59340
+ return true;
59341
+ }
59342
+ async finish(sessionID, id, owner, status, now) {
59343
+ const old = this.admission(sessionID, id);
59344
+ if (!old || old.owner_token !== owner || old.status !== "pending")
59345
+ return false;
59346
+ this.put(sessionID, id, { ...old, status, owner_token: null, logged_at: now });
59347
+ return true;
59348
+ }
59349
+ async forget(sessionID) {
59350
+ this.evict(sessionID);
59351
+ }
59352
+ }
59353
+ function nativeFoldCache(db, budget) {
59354
+ return new NativeFoldCache(typeof db === "function" ? db : () => db, budget);
59355
+ }
59356
+ var DEFAULT_NATIVE_FOLD_BUDGET, stringBytes = (value) => (value?.length ?? 0) * 2;
59357
+ var init_memory_cache = __esm(() => {
59358
+ init_owner();
59359
+ DEFAULT_NATIVE_FOLD_BUDGET = Object.freeze({
59360
+ maxSessionBytes: 16 * 1024 * 1024,
59361
+ maxSessions: 16
59362
+ });
59363
+ });
59364
+
59365
+ // src/v2/tool-result.ts
59366
+ function isToolError(part) {
59367
+ const result = part.result;
59368
+ return part.resultType === "error" || result?.type === "error" || result !== null && typeof result === "object" && "error" in result;
59369
+ }
59370
+ function toolErrorText(result) {
59371
+ const value = result.value === undefined ? result : result.value;
59372
+ return typeof value === "string" ? value : JSON.stringify(value);
59373
+ }
59374
+ function toolStateContent(state) {
59375
+ if (typeof state.output === "string")
59376
+ return state.output;
59377
+ if (state.status === "error")
59378
+ return toolErrorText({ error: state.error, content: state.content ?? [] });
59379
+ if (typeof state.content === "string")
59380
+ return state.content;
59381
+ if (!Array.isArray(state.content))
59382
+ return "";
59383
+ return state.content.map((part) => {
59384
+ if (typeof part === "string")
59385
+ return part;
59386
+ if (!part || typeof part !== "object")
59387
+ return "";
59388
+ const value = part;
59389
+ if (typeof value.text === "string")
59390
+ return value.text;
59391
+ if (typeof value.value === "string")
59392
+ return value.value;
59393
+ return "";
59394
+ }).filter(Boolean).join(`
59395
+ `);
59396
+ }
59397
+
59398
+ // src/v2/hooks/payload.ts
59399
+ function rejectContentPart(message, part, reason) {
59400
+ const type = typeof part.type === "string" ? part.type : "<missing>";
59401
+ const text = `v2 context omitted a content part the host would reject: type=${type} message=${String(message.info.id ?? "<head>")} reason=${reason}`;
59402
+ if (typeof message.info.sessionID === "string")
59403
+ sessionLog(message.info.sessionID, text);
59404
+ else
59405
+ log(text);
59406
+ }
59407
+ function isHostInstance(value) {
59408
+ if (!value || typeof value !== "object" || Array.isArray(value))
59409
+ return false;
59410
+ const prototype = Object.getPrototypeOf(value);
59411
+ return prototype !== Object.prototype && prototype !== null;
59412
+ }
59413
+ function containsHostInstance(value) {
59414
+ if (isHostInstance(value))
59415
+ return true;
59416
+ if (Array.isArray(value))
59417
+ return value.some(containsHostInstance);
59418
+ if (value && typeof value === "object")
59419
+ return Object.values(value).some(containsHostInstance);
59420
+ return false;
59421
+ }
59422
+ function clonePreservingInstances(value) {
59423
+ if (isHostInstance(value))
59424
+ return value;
59425
+ if (Array.isArray(value))
59426
+ return value.map(clonePreservingInstances);
59427
+ if (value && typeof value === "object") {
59428
+ const copy = {};
59429
+ for (const [key, entry] of Object.entries(value))
59430
+ copy[key] = clonePreservingInstances(entry);
59431
+ return copy;
59432
+ }
59433
+ return value;
59434
+ }
59435
+ function contentKey(value) {
59436
+ const plain = (entry) => {
59437
+ if (ArrayBuffer.isView(entry)) {
59438
+ return {
59439
+ bytes: Buffer.from(entry.buffer, entry.byteOffset, entry.byteLength).toString("base64")
59440
+ };
59441
+ }
59442
+ if (Array.isArray(entry))
59443
+ return entry.map(plain);
59444
+ if (entry && typeof entry === "object") {
59445
+ return Object.fromEntries(Object.entries(entry).map(([key, nested]) => [key, plain(nested)]));
59446
+ }
59447
+ return entry;
59448
+ };
59449
+ return JSON.stringify(plain(value));
59450
+ }
59451
+ function fileContentValue(result) {
59452
+ const value = result?.value;
59453
+ if (result?.type !== "content" || !Array.isArray(value))
59454
+ return;
59455
+ return value.some((item) => item?.type === "file") ? value : undefined;
59456
+ }
59457
+ function contentValueText(value) {
59458
+ return value.filter((item) => item?.type === "text" && typeof item.text === "string").map((item) => item.text).join(`
59459
+ `);
59460
+ }
59461
+ function contentValueAttachments(value) {
59462
+ return value.filter((item) => item?.type === "file").map((item) => ({
59463
+ type: "file",
59464
+ mime: item.mime,
59465
+ url: item.uri,
59466
+ ...typeof item.name === "string" ? { filename: item.name } : {}
59467
+ }));
59468
+ }
59469
+ function rebuildFileContent(value, output) {
59470
+ if (isDroppedToolOutput(output))
59471
+ return { type: "text", value: output };
59472
+ const rebuilt = [];
59473
+ let placed = false;
59474
+ for (const item of value) {
59475
+ if (item?.type !== "text") {
59476
+ rebuilt.push(item);
59477
+ continue;
59478
+ }
59479
+ if (placed)
59480
+ continue;
59481
+ placed = true;
59482
+ rebuilt.push({ ...item, text: output });
59483
+ }
59484
+ if (!placed && output)
59485
+ rebuilt.unshift({ type: "text", text: output });
59486
+ return { type: "content", value: rebuilt };
59487
+ }
59488
+ function adaptPayload(draft, admittedIDs = new Set) {
59489
+ const existingHeads = new Map(draft.messages.filter((m) => HEAD_IDS.some((id) => m.id === id)).map((m) => [m.id, m]));
59490
+ const source = draft.messages.filter((m) => !existingHeads.has(m.id));
59491
+ const results = new Map;
59492
+ const hostParts = new Map;
59493
+ const hostPartTypes = new Set;
59494
+ for (const message of source) {
59495
+ for (const part of message.content) {
59496
+ if (part.type !== "tool-call" && part.type !== "tool-result" && part.type !== "tool" && containsHostInstance(part)) {
59497
+ const key = contentKey(part);
59498
+ if (!hostParts.has(key))
59499
+ hostParts.set(key, part);
59500
+ hostPartTypes.add(part.type);
59501
+ }
59502
+ if (part.type === "tool-result" && typeof part.id === "string") {
59503
+ const queue = results.get(part.id) ?? [];
59504
+ queue.push({ part, message });
59505
+ results.set(part.id, queue);
59506
+ }
59507
+ }
59508
+ }
59509
+ const paired = new Set;
59510
+ const bridges = new Map;
59511
+ const originals = new Map;
59512
+ const bridgesByMessageID = new Map;
59513
+ const bridgesByCarrier = new Map;
59514
+ const originalsByID = new Map;
59515
+ const bridgesFor = (message) => typeof message.info.id === "string" ? bridgesByMessageID.get(message.info.id) : bridgesByCarrier.get(message);
59516
+ const nativeTool = (call, result) => {
59517
+ const hostResult = result?.part.result;
59518
+ const failed = result !== undefined && isToolError(result.part);
59519
+ const value = hostResult?.value;
59520
+ const files = fileContentValue(hostResult);
59521
+ const output = failed && hostResult ? toolErrorText(hostResult) : files ? contentValueText(files) : typeof value === "string" ? value : value === undefined ? "" : JSON.stringify(value);
59522
+ const part = {
59523
+ type: "tool",
59524
+ callID: call?.id ?? result?.part.id,
59525
+ tool: call?.name ?? result?.part.name,
59526
+ state: {
59527
+ input: structuredClone(call?.input ?? {}),
59528
+ status: failed ? "error" : result ? "completed" : "running",
59529
+ ...result ? { output } : {},
59530
+ ...result && (call?.name ?? result.part.name) === "question" ? { metadata: { userAnswer: true } } : {},
59531
+ ...files ? { attachments: contentValueAttachments(files) } : {}
59532
+ }
59533
+ };
59534
+ bridges.set(part, {
59535
+ call,
59536
+ result: result?.part,
59537
+ resultMessage: result?.message,
59538
+ output,
59539
+ files
59540
+ });
59541
+ if (result)
59542
+ paired.add(result.part);
59543
+ return part;
59544
+ };
59545
+ const convertedTool = (source) => {
59546
+ const sourceState = source.state && typeof source.state === "object" ? source.state : {};
59547
+ const output = toolStateContent(sourceState);
59548
+ const { content: _content, ...projectedState } = structuredClone(sourceState);
59549
+ const part = {
59550
+ type: "tool",
59551
+ callID: source.callID ?? source.id,
59552
+ tool: source.tool ?? source.name,
59553
+ state: { ...projectedState, output }
59554
+ };
59555
+ bridges.set(part, { native: source, output });
59556
+ return part;
59557
+ };
59558
+ const callParts = new Map;
59559
+ for (const message of source) {
59560
+ for (const part of message.content) {
59561
+ if (part.type === "tool-call") {
59562
+ callParts.set(part, nativeTool(part, results.get(String(part.id))?.shift()));
59563
+ } else if (part.type === "tool") {
59564
+ callParts.set(part, convertedTool(part));
59565
+ }
59566
+ }
59567
+ }
59568
+ const messages = [];
59569
+ for (const message of source) {
59570
+ const parts = [];
59571
+ for (const content of message.content) {
59572
+ if (paired.has(content))
59573
+ continue;
59574
+ const part = callParts.get(content) ?? (content.type === "tool-result" ? nativeTool(undefined, { part: content, message }) : clonePreservingInstances(content));
59575
+ if (message.id && admittedIDs.has(message.id))
59576
+ part.synthetic = true;
59577
+ parts.push(part);
59578
+ }
59579
+ if (!parts.length && message.content.length)
59580
+ continue;
59581
+ const byCallID = new Map;
59582
+ for (const part of parts) {
59583
+ const bridge = bridges.get(part);
59584
+ if (bridge && typeof part.callID === "string")
59585
+ byCallID.set(part.callID, bridge);
59586
+ }
59587
+ if (typeof message.id === "string") {
59588
+ bridgesByMessageID.set(message.id, byCallID);
59589
+ originalsByID.set(message.id, message);
59590
+ }
59591
+ const mapped = {
59592
+ info: {
59593
+ id: message.id,
59594
+ sessionID: draft.sessionID,
59595
+ role: message.role,
59596
+ ...{
59597
+ providerID: draft.model.providerID,
59598
+ modelID: draft.model.id,
59599
+ agent: draft.agent
59600
+ }
59601
+ },
59602
+ parts
59603
+ };
59604
+ originals.set(mapped, message);
59605
+ if (typeof message.id !== "string")
59606
+ bridgesByCarrier.set(mapped, byCallID);
59607
+ messages.push(mapped);
59608
+ }
59609
+ return {
59610
+ messages,
59611
+ commit() {
59612
+ let head = 0;
59613
+ const rendered = [];
59614
+ for (const message of messages) {
59615
+ const original = originals.get(message) ?? (typeof message.info.id === "string" ? originalsByID.get(message.info.id) : undefined);
59616
+ const id = message.info.syntheticHead ? HEAD_IDS[head++] : original?.id;
59617
+ const content = [];
59618
+ const following = [];
59619
+ for (const candidate of message.parts) {
59620
+ const part = candidate;
59621
+ const bridge = bridges.get(part) ?? (part.type === "tool" && typeof part.callID === "string" ? bridgesFor(message)?.get(part.callID) : undefined);
59622
+ if (!bridge) {
59623
+ if (part.type === "tool") {
59624
+ const state = part.state;
59625
+ if (typeof part.callID !== "string" || typeof part.tool !== "string" || !state || typeof state !== "object") {
59626
+ rejectContentPart(message, part, "tool has no call ID, name, or state");
59627
+ continue;
59628
+ }
59629
+ content.push({
59630
+ type: "tool-call",
59631
+ id: part.callID,
59632
+ name: part.tool,
59633
+ input: state.input
59634
+ });
59635
+ if (state.status === "completed" || state.status === "error") {
59636
+ following.push({
59637
+ role: "tool",
59638
+ content: [
59639
+ {
59640
+ type: "tool-result",
59641
+ id: part.callID,
59642
+ name: part.tool,
59643
+ result: {
59644
+ type: state.status === "error" ? "error" : "text",
59645
+ value: toolStateContent(state)
59646
+ }
59647
+ }
59648
+ ]
59649
+ });
59650
+ }
59651
+ continue;
59652
+ }
59653
+ if (part.type === "file") {
59654
+ const mime = part.mime;
59655
+ const url = part.url;
59656
+ const prefix = `data:${mime};base64,`;
59657
+ if (typeof mime !== "string" || !/^image\/(png|jpeg|gif|webp)$/.test(mime) || typeof url !== "string" || !url.startsWith(prefix)) {
59658
+ rejectContentPart(message, part, "file is not an inline supported image");
59659
+ continue;
59660
+ }
59661
+ const asset = hostMediaAsset(url.slice(prefix.length), mime);
59662
+ if (typeof asset === "string") {
59663
+ rejectContentPart(message, part, `host media unavailable: ${asset}`);
59664
+ continue;
59665
+ }
59666
+ content.push({ type: "media", media: asset });
59667
+ continue;
59668
+ }
59669
+ if (!HOST_CONTENT_TYPES.has(String(part.type))) {
59670
+ rejectContentPart(message, part, "unknown host content type");
59671
+ continue;
59672
+ }
59673
+ const { synthetic: _synthetic, ...clean } = part;
59674
+ const original = hostPartTypes.has(clean.type) && !containsHostInstance(clean) ? hostParts.get(contentKey(clean)) : undefined;
59675
+ content.push(original ?? clean);
59676
+ continue;
59677
+ }
59678
+ const state = part.state;
59679
+ if (bridge.native) {
59680
+ const native = bridge.native;
59681
+ const callID = String(native.id ?? native.callID ?? part.callID);
59682
+ const name = String(native.name ?? native.tool ?? part.tool);
59683
+ content.push({ type: "tool-call", id: callID, name, input: state.input });
59684
+ if (state.status === "completed" || state.status === "error") {
59685
+ following.push({
59686
+ role: "tool",
59687
+ content: [
59688
+ {
59689
+ type: "tool-result",
59690
+ id: callID,
59691
+ name,
59692
+ result: {
59693
+ type: state.status === "error" ? "error" : "text",
59694
+ value: state.output
59695
+ }
59696
+ }
59697
+ ]
59698
+ });
59699
+ }
59700
+ continue;
59701
+ }
59702
+ if (bridge.call)
59703
+ content.push({ ...bridge.call, input: state.input });
59704
+ if (bridge.result && bridge.resultMessage) {
59705
+ const result = {
59706
+ ...bridge.result,
59707
+ result: state.output === bridge.output ? bridge.result.result : bridge.files && typeof state.output === "string" ? rebuildFileContent(bridge.files, state.output) : {
59708
+ type: state.status === "error" ? "error" : "text",
59709
+ value: state.output
59710
+ }
59711
+ };
59712
+ if (bridge.resultMessage === original)
59713
+ content.push(result);
59714
+ else
59715
+ following.push({ ...bridge.resultMessage, content: [result] });
59716
+ }
59717
+ }
59718
+ if (content.length || !original?.content.length) {
59719
+ const value = {
59720
+ ...original,
59721
+ id,
59722
+ role: message.info.role ?? "user",
59723
+ content
59724
+ };
59725
+ const existing = id ? existingHeads.get(id) : undefined;
59726
+ rendered.push(existing ? Object.assign(existing, { content }) : value);
59727
+ }
59728
+ rendered.push(...following);
59729
+ }
59730
+ draft.messages.splice(0, draft.messages.length, ...rendered);
59731
+ }
59732
+ };
59733
+ }
59734
+ var HEAD_IDS, HOST_CONTENT_TYPES;
59735
+ var init_payload = __esm(() => {
59736
+ init_ctx_reduce_nudge();
59737
+ init_logger();
59738
+ init_host_media();
59739
+ HEAD_IDS = ["__magic_context_v2_m0__", "__magic_context_v2_m1__"];
59740
+ HOST_CONTENT_TYPES = new Set([
59741
+ "text",
59742
+ "media",
59743
+ "tool-call",
59744
+ "tool-result",
59745
+ "reasoning",
59746
+ "compaction",
59747
+ "effort"
59748
+ ]);
59749
+ });
59750
+
59751
+ // src/v2/fold/native-codec.ts
59752
+ function encodeNative(messages) {
59753
+ for (const message of messages)
59754
+ for (const part of message.content) {
59755
+ if (part.type !== "media" || !part.media)
59756
+ continue;
59757
+ const asset = part.media;
59758
+ if (typeof asset.toJSON !== "function" || asset.headers !== undefined)
59759
+ throw new NativeReplayUnavailable("Native media has no lossless replay codec");
59760
+ const encoded = asset.toJSON();
59761
+ const revived = hostMediaFromEncoded(encoded);
59762
+ if (typeof revived === "string" || JSON.stringify(revived) !== JSON.stringify(asset))
59763
+ throw new NativeReplayUnavailable("Native media cannot be decoded losslessly before admission");
59764
+ }
59765
+ const encoded = JSON.parse(JSON.stringify(messages));
59766
+ const freeze = (value) => {
59767
+ if (!value || typeof value !== "object")
59768
+ return;
59769
+ for (const item of Object.values(value))
59770
+ freeze(item);
59771
+ Object.freeze(value);
59772
+ };
59773
+ freeze(encoded);
59774
+ return encoded;
59775
+ }
59776
+ function reviveNative(messages) {
59777
+ const result = structuredClone([...messages]);
59778
+ for (const message of result)
59779
+ for (const part of message.content) {
59780
+ if (part.type !== "media" || !part.media)
59781
+ continue;
59782
+ const asset = hostMediaFromEncoded(part.media);
59783
+ if (typeof asset === "string")
59784
+ throw new NativeReplayUnavailable(asset);
59785
+ part.media = asset;
59786
+ }
59787
+ return result;
59788
+ }
59789
+ var NativeReplayUnavailable;
59790
+ var init_native_codec = __esm(() => {
59791
+ init_host_media();
59792
+ NativeReplayUnavailable = class NativeReplayUnavailable extends Error {
59793
+ };
59794
+ });
59795
+
58748
59796
  // src/v2/hooks/storage-notice.ts
58749
59797
  function formatStorageRefusalNotice(reason) {
58750
59798
  return `${STORAGE_NOTICE_PREFIX}This turn was not sent to the model. ${formatFailClosedBlockingSummary(reason)}`;
@@ -58978,6 +60026,411 @@ var init_restore = __esm(() => {
58978
60026
  imageMimes = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
58979
60027
  });
58980
60028
 
60029
+ // src/v2/fold/native-replay.ts
60030
+ function nativeRowID(message) {
60031
+ return coordinates.get(message) ?? message.id;
60032
+ }
60033
+ function copyNativeInput(messages) {
60034
+ const copies = reviveNative(encodeNative(messages));
60035
+ copies.forEach((copy, i) => {
60036
+ const original = messages[i];
60037
+ const id = original && nativeRowID(original);
60038
+ if (id !== undefined)
60039
+ coordinates.set(copy, id);
60040
+ });
60041
+ return copies;
60042
+ }
60043
+ function isLocalCheckpoint(row) {
60044
+ return row.type === "compaction" && row.data.status === "completed" && row.data.providerContext === undefined;
60045
+ }
60046
+
60047
+ class NativeFoldReplay {
60048
+ storage;
60049
+ openReader;
60050
+ changed = new Set;
60051
+ dirty = new Set;
60052
+ unavailable = new Set;
60053
+ metadata = new Map;
60054
+ metadataThrough = new Map;
60055
+ loaded = new Map;
60056
+ sources = new Map;
60057
+ constructor(storage, openReader) {
60058
+ this.storage = storage;
60059
+ this.openReader = openReader;
60060
+ storage.onEvict((sessionID) => this.clear(sessionID));
60061
+ }
60062
+ account(sessionID) {
60063
+ const source = this.sources.get(sessionID);
60064
+ let bytes = 256 + (source?.length ?? 0) * 2;
60065
+ for (const row of this.metadata.get(sessionID) ?? [])
60066
+ bytes += 128 + (row.id.length + row.type.length) * 2;
60067
+ this.storage.accountReplay(sessionID, bytes);
60068
+ }
60069
+ async observeSource(sessionID, reader) {
60070
+ const source = reader.hostIdentity();
60071
+ const prior = this.sources.get(sessionID);
60072
+ if (prior && prior !== source)
60073
+ await this.forget(sessionID);
60074
+ this.sources.set(sessionID, source);
60075
+ this.account(sessionID);
60076
+ }
60077
+ onEvent(event) {
60078
+ const sessionID = event.data?.sessionID;
60079
+ if (sessionID && (event.type?.startsWith("session.revert.") || event.type === "session.message.content.updated"))
60080
+ this.invalidate(sessionID);
60081
+ }
60082
+ invalidate(sessionID, suppressNomination = false) {
60083
+ if (suppressNomination)
60084
+ this.unavailable.add(sessionID);
60085
+ this.dirty.add(sessionID);
60086
+ this.changed.add(sessionID);
60087
+ this.metadata.delete(sessionID);
60088
+ this.metadataThrough.delete(sessionID);
60089
+ this.account(sessionID);
60090
+ }
60091
+ tail(sessionID) {
60092
+ const record = this.storage.tail(sessionID);
60093
+ return record ? JSON.parse(record.data) : undefined;
60094
+ }
60095
+ async baseline(sessionID) {
60096
+ return this.tail(sessionID)?.baseline;
60097
+ }
60098
+ rendered(row, model) {
60099
+ return restoreRow(row, model, hostUsesMediaAssets() ? {
60100
+ asset: hostMediaAsset,
60101
+ unavailable: () => {}
60102
+ } : undefined);
60103
+ }
60104
+ record(row, messages) {
60105
+ const data = JSON.stringify(encodeNative(messages));
60106
+ return { id: row.id, type: row.type, seq: row.seq, data, digest: foldDigest(data) };
60107
+ }
60108
+ records(sessionID, native, reader) {
60109
+ let metadata = this.metadata.get(sessionID);
60110
+ const through = reader.latestSequence(sessionID);
60111
+ const known = new Set(metadata?.map((row) => row.id));
60112
+ if (!metadata || (this.metadataThrough.get(sessionID) ?? -1) < through || native.some((message) => message.id && !known.has(message.id))) {
60113
+ metadata = reader.rowStampsThrough(sessionID, through).filter(conversational);
60114
+ this.metadata.set(sessionID, metadata);
60115
+ this.metadataThrough.set(sessionID, through);
60116
+ }
60117
+ const byID = new Map(metadata.map((row) => [row.id, row]));
60118
+ const grouped = new Map;
60119
+ const anchorIndex = native.findIndex((message) => {
60120
+ const id = nativeRowID(message);
60121
+ return id !== undefined && byID.has(id);
60122
+ });
60123
+ const leading = native.slice(0, anchorIndex < 0 ? native.length : anchorIndex).filter((message) => !nativeRowID(message) && message.role === "system");
60124
+ const anchorMessage = native[anchorIndex];
60125
+ const anchorID = anchorMessage && nativeRowID(anchorMessage);
60126
+ const anchor = anchorID ? byID.get(anchorID) : undefined;
60127
+ const leadingRows = anchor ? metadata.filter((row) => row.type === "system" && row.seq < anchor.seq).slice(-leading.length) : [];
60128
+ const leadingIDs = new Map(leading.map((message, i) => [message, leadingRows[i]?.id]));
60129
+ let cursor = -1;
60130
+ let owner;
60131
+ for (const message of native) {
60132
+ let id = nativeRowID(message);
60133
+ if (!id && message.role === "system") {
60134
+ if (leadingIDs.has(message)) {
60135
+ id = leadingIDs.get(message);
60136
+ if (!id)
60137
+ continue;
60138
+ } else
60139
+ id = metadata.find((row) => row.type === "system" && row.seq > cursor)?.id;
60140
+ }
60141
+ if (!id && message.role === "tool")
60142
+ id = owner;
60143
+ const row = id ? byID.get(id) : undefined;
60144
+ if (!row)
60145
+ throw new NativeReplayUnavailable("Native carrier has no source row coordinate");
60146
+ cursor = row.seq;
60147
+ coordinates.set(message, row.id);
60148
+ if (message.role !== "tool")
60149
+ owner = row.id;
60150
+ const messages = grouped.get(row) ?? [];
60151
+ messages.push(message);
60152
+ grouped.set(row, messages);
60153
+ }
60154
+ return [...grouped].map(([row, messages]) => this.record(row, messages));
60155
+ }
60156
+ async capture(draft, native) {
60157
+ const release = this.storage.hold(draft.sessionID);
60158
+ try {
60159
+ rememberHostMedia(native);
60160
+ const reader = this.openReader(draft.sessionID);
60161
+ try {
60162
+ const rows = this.records(draft.sessionID, native, reader);
60163
+ const baseline = draft.messages.find((message) => message.id === HEAD_IDS[0])?.content.find((part) => part.type === "text")?.text;
60164
+ const through = Math.max(-1, ...rows.map((row) => row.seq));
60165
+ const tail = {
60166
+ model: `${draft.model.providerID}/${draft.model.id}`,
60167
+ sourceSeq: through,
60168
+ baseline: typeof baseline === "string" ? baseline : this.tail(draft.sessionID)?.baseline
60169
+ };
60170
+ await this.storage.saveTail(draft.sessionID, JSON.stringify(tail), rows);
60171
+ this.storage.truncate(draft.sessionID, through);
60172
+ this.loaded.set(draft.sessionID, through);
60173
+ this.unavailable.delete(draft.sessionID);
60174
+ this.changed.delete(draft.sessionID);
60175
+ } finally {
60176
+ reader.close();
60177
+ }
60178
+ } catch {
60179
+ this.unavailable.add(draft.sessionID);
60180
+ this.invalidate(draft.sessionID);
60181
+ } finally {
60182
+ this.account(draft.sessionID);
60183
+ release();
60184
+ }
60185
+ }
60186
+ async canReplay(sessionID, _reader) {
60187
+ return !this.unavailable.has(sessionID) && typeof await this.baseline(sessionID) === "string";
60188
+ }
60189
+ async supply(args) {
60190
+ const release = this.storage.hold(args.draft.sessionID);
60191
+ try {
60192
+ return await this.supplyHeld(args);
60193
+ } finally {
60194
+ this.account(args.draft.sessionID);
60195
+ release();
60196
+ }
60197
+ }
60198
+ async supplyHeld(args) {
60199
+ const { draft, reader, summary } = args;
60200
+ const tail = this.tail(draft.sessionID);
60201
+ const id = reader.latestRunningCompaction(draft.sessionID)?.id;
60202
+ if (!id || !tail || tail.model !== `${draft.model.providerID}/${draft.model.id}`)
60203
+ return false;
60204
+ const through = reader.latestSequence(draft.sessionID);
60205
+ const after = this.dirty.has(draft.sessionID) ? -1 : tail.sourceSeq;
60206
+ const fresh = reader.range(draft.sessionID, after, through).filter(conversational);
60207
+ const records = fresh.map((row) => this.record(row, this.rendered(row, draft.model)));
60208
+ if (this.dirty.has(draft.sessionID))
60209
+ this.storage.replaceRows(draft.sessionID, through, records);
60210
+ await this.storage.saveTail(draft.sessionID, JSON.stringify({
60211
+ ...tail,
60212
+ sourceSeq: Math.max(tail.sourceSeq, ...fresh.map((row) => row.seq))
60213
+ }), records);
60214
+ this.dirty.delete(draft.sessionID);
60215
+ this.metadata.delete(draft.sessionID);
60216
+ this.metadataThrough.delete(draft.sessionID);
60217
+ this.loaded.set(draft.sessionID, through);
60218
+ const snapshot = {
60219
+ source: this.storage.sourceID,
60220
+ sessionID: draft.sessionID,
60221
+ admissionID: id,
60222
+ summary
60223
+ };
60224
+ await this.storage.supply(draft.sessionID, id, JSON.stringify(snapshot));
60225
+ return true;
60226
+ }
60227
+ async restore(sessionID, cut, model, options = {}) {
60228
+ const release = this.storage.hold(sessionID);
60229
+ try {
60230
+ return await this.restoreHeld(sessionID, cut, model, options);
60231
+ } finally {
60232
+ this.account(sessionID);
60233
+ release();
60234
+ }
60235
+ }
60236
+ async restoreHeld(sessionID, cut, model, options) {
60237
+ if (!isLocalCheckpoint(cut))
60238
+ return;
60239
+ const resolvedModel = {
60240
+ providerID: model.slice(0, model.indexOf("/")),
60241
+ id: model.slice(model.indexOf("/") + 1)
60242
+ };
60243
+ const tail = this.tail(sessionID);
60244
+ if (tail && tail.model !== model)
60245
+ this.invalidate(sessionID);
60246
+ let after = options.after ?? -1;
60247
+ if (options.bounded && options.after === undefined && !this.changed.has(sessionID)) {
60248
+ const state = getOrCreateSessionMeta(this.storage.db, sessionID);
60249
+ const max = this.storage.db.prepare("SELECT COALESCE(MAX(id),0) AS id FROM m0_mutation_log WHERE session_id=?").get(sessionID);
60250
+ const boundary = this.storage.db.prepare("SELECT cached_m0_last_baseline_end_message_id AS id FROM session_meta WHERE session_id=?").get(sessionID)?.id;
60251
+ if (max.id <= (state.cachedM0MaxMutationId ?? 0) && boundary)
60252
+ after = this.storage.sequenceForID(sessionID, boundary) ?? -1;
60253
+ }
60254
+ const through = cut.seq - 1;
60255
+ let reader;
60256
+ try {
60257
+ let metadata = this.metadata.get(sessionID);
60258
+ if (!metadata || (this.metadataThrough.get(sessionID) ?? -1) < through) {
60259
+ reader = this.openReader(sessionID);
60260
+ metadata = reader.rowStampsThrough(sessionID, through).filter(conversational);
60261
+ this.metadata.set(sessionID, metadata);
60262
+ this.metadataThrough.set(sessionID, through);
60263
+ }
60264
+ const required = metadata.filter((row) => row.seq > after && row.seq <= through);
60265
+ if (this.dirty.has(sessionID) || !tail || (this.loaded.get(sessionID) ?? -1) < through && !this.storage.admission(sessionID, cut.id) || !this.storage.containsRows(sessionID, required.map((row) => row.id))) {
60266
+ reader ??= this.openReader(sessionID);
60267
+ const fresh = reader.range(sessionID, after, through).filter(conversational);
60268
+ const records = fresh.map((row) => this.record(row, this.rendered(row, resolvedModel)));
60269
+ const restored = new Set(records.map((row) => row.id));
60270
+ if (required.some((row) => !restored.has(row.id)))
60271
+ throw new NativeReplayUnavailable("Host restore omitted a required source row");
60272
+ this.storage.replaceRows(sessionID, through, records, after);
60273
+ await this.storage.saveTail(sessionID, JSON.stringify({
60274
+ model,
60275
+ sourceSeq: through,
60276
+ baseline: tail?.baseline ?? cut.data.summary
60277
+ }), []);
60278
+ this.loaded.set(sessionID, through);
60279
+ this.dirty.delete(sessionID);
60280
+ }
60281
+ } finally {
60282
+ reader?.close();
60283
+ }
60284
+ const messages = [];
60285
+ for (const row of this.storage.rows(sessionID, after, cut.seq - 1)) {
60286
+ const revived = reviveNative(JSON.parse(row.data));
60287
+ for (const message of revived)
60288
+ coordinates.set(message, row.id);
60289
+ messages.push(...revived);
60290
+ }
60291
+ return messages;
60292
+ }
60293
+ async restoreRead(sessionID, cut, model) {
60294
+ const reader = this.openReader(sessionID);
60295
+ try {
60296
+ return reader.range(sessionID, -1, cut.seq - 1).filter(conversational).flatMap((row) => {
60297
+ const messages = this.rendered(row, model);
60298
+ for (const message of messages)
60299
+ coordinates.set(message, row.id);
60300
+ return messages;
60301
+ });
60302
+ } finally {
60303
+ reader.close();
60304
+ }
60305
+ }
60306
+ sourceChanged(sessionID) {
60307
+ return this.changed.has(sessionID);
60308
+ }
60309
+ clear(sessionID) {
60310
+ this.sources.delete(sessionID);
60311
+ this.metadata.delete(sessionID);
60312
+ this.metadataThrough.delete(sessionID);
60313
+ this.loaded.delete(sessionID);
60314
+ this.unavailable.delete(sessionID);
60315
+ this.dirty.delete(sessionID);
60316
+ this.changed.delete(sessionID);
60317
+ }
60318
+ async forget(sessionID) {
60319
+ await this.storage.forget(sessionID);
60320
+ }
60321
+ }
60322
+ var coordinates, conversational = (row) => RAW_MESSAGE_TYPES.includes(row.type) || row.type === "location-switched";
60323
+ var init_native_replay = __esm(async () => {
60324
+ init_payload();
60325
+ init_host_media();
60326
+ init_native_codec();
60327
+ init_owner();
60328
+ init_restore();
60329
+ await __promiseAll([
60330
+ init_storage(),
60331
+ init_store_reader()
60332
+ ]);
60333
+ coordinates = new WeakMap;
60334
+ });
60335
+
60336
+ // src/v2/fold/policy.ts
60337
+ import { randomUUID as randomUUID5 } from "node:crypto";
60338
+
60339
+ class HostFoldPolicy {
60340
+ deps;
60341
+ active = new Set;
60342
+ owner = randomUUID5();
60343
+ constructor(deps) {
60344
+ this.deps = deps;
60345
+ }
60346
+ owned(sessionID, cut, id) {
60347
+ const row = this.deps.storage.admission(sessionID, id);
60348
+ if (!isLocalCheckpoint(cut) || cut.id !== id || !row?.payload || row.digest !== foldDigest(row.payload))
60349
+ return false;
60350
+ const snapshot = JSON.parse(row.payload);
60351
+ return snapshot.summary === cut.data.summary && snapshot.source === this.deps.storage.sourceID && snapshot.sessionID === sessionID && snapshot.admissionID === id;
60352
+ }
60353
+ async idle(sessionID) {
60354
+ const { storage, session, threshold } = this.deps;
60355
+ if (!Number.isSafeInteger(threshold) || threshold <= 0 || typeof session.compact !== "function" || this.active.has(sessionID))
60356
+ return;
60357
+ this.active.add(sessionID);
60358
+ const now = this.deps.now ?? Date.now;
60359
+ let attempt;
60360
+ let claimed = false;
60361
+ try {
60362
+ const reader = this.deps.openReader();
60363
+ try {
60364
+ const cut = reader.latestCompaction(sessionID);
60365
+ const previous = storage.latestAttempt(sessionID);
60366
+ const old = previous ? JSON.parse(previous.data) : undefined;
60367
+ if (cut && previous?.status === "pending" && old && this.owned(sessionID, cut, old.id)) {
60368
+ if (await storage.claim(sessionID, old.id, previous.data, this.owner, now()) && await storage.finish(sessionID, old.id, this.owner, "completed", now()))
60369
+ this.deps.log(sessionID, `v2 host fold: rows_before=${old.before} rows_after=${reader.storedRowsAfter(sessionID, cut.seq)} duration_ms=${now() - old.started} reason=row_threshold id=${cut.id} status=completed`);
60370
+ return;
60371
+ }
60372
+ const after = cut?.seq ?? -1;
60373
+ const before = reader.storedRowsAfter(sessionID, after);
60374
+ if (before < threshold || !await this.deps.canReplay(sessionID, reader))
60375
+ return;
60376
+ const retry = old?.after === after ? old.attempt + (previous?.status === "failed" || previous?.status === "coalesced" ? 1 : 0) : 0;
60377
+ const id = old?.after === after && previous?.status === "pending" ? old.id : `msg_mc_fold_${foldDigest(`${storage.sourceID}/${sessionID}/${after}/${retry}`).slice(0, 24)}`;
60378
+ attempt = {
60379
+ after,
60380
+ before,
60381
+ watermark: reader.latestSequence(sessionID),
60382
+ attempt: retry,
60383
+ id,
60384
+ started: now()
60385
+ };
60386
+ claimed = await storage.claim(sessionID, id, JSON.stringify(attempt), this.owner, now());
60387
+ if (!claimed)
60388
+ return;
60389
+ } finally {
60390
+ reader.close();
60391
+ }
60392
+ const admitted = await session.compact({
60393
+ sessionID,
60394
+ id: attempt.id,
60395
+ delivery: "queue"
60396
+ });
60397
+ await session.wait({ sessionID });
60398
+ const settled = this.deps.openReader();
60399
+ try {
60400
+ const cut = settled.latestCompaction(sessionID);
60401
+ if (admitted?.id !== attempt.id || !cut || cut.seq <= attempt.after || !this.owned(sessionID, cut, attempt.id)) {
60402
+ if (await storage.finish(sessionID, attempt.id, this.owner, "coalesced", now()))
60403
+ this.deps.log(sessionID, `v2 host fold: rows_before=${attempt.before} rows_after=unknown duration_ms=${now() - attempt.started} reason=row_threshold id=${attempt.id} actual_id=${cut?.id ?? admitted?.id ?? "unknown"} status=coalesced`);
60404
+ return;
60405
+ }
60406
+ const after = settled.storedRowsAfter(sessionID, cut.seq);
60407
+ if (await storage.finish(sessionID, attempt.id, this.owner, "completed", now()))
60408
+ this.deps.log(sessionID, `v2 host fold: rows_before=${attempt.before} rows_after=${after} duration_ms=${now() - attempt.started} reason=row_threshold id=${cut.id} status=completed`);
60409
+ } finally {
60410
+ settled.close();
60411
+ }
60412
+ } catch (error) {
60413
+ if (claimed && attempt) {
60414
+ let finished = false;
60415
+ try {
60416
+ finished = await storage.finish(sessionID, attempt.id, this.owner, "failed", now());
60417
+ } catch {
60418
+ finished = true;
60419
+ }
60420
+ if (finished)
60421
+ this.deps.log(sessionID, `v2 host fold: rows_before=${attempt.before} rows_after=unknown duration_ms=${now() - attempt.started} reason=row_threshold id=${attempt.id} status=failed error=${JSON.stringify(String(error))}`);
60422
+ } else
60423
+ this.deps.log(sessionID, `v2 host fold: rows_before=unknown rows_after=unknown duration_ms=0 reason=row_threshold status=failed error=${JSON.stringify(String(error))} cache_error=${JSON.stringify(String(error))}`);
60424
+ } finally {
60425
+ this.active.delete(sessionID);
60426
+ }
60427
+ }
60428
+ }
60429
+ var init_policy = __esm(async () => {
60430
+ init_owner();
60431
+ await init_native_replay();
60432
+ });
60433
+
58981
60434
  // src/v2/fork-inheritance.ts
58982
60435
  function forkBoundarySeq(origin, sequenceForId) {
58983
60436
  const seq = sequenceForId(origin.parentSessionID, origin.boundary.messageID);
@@ -59106,11 +60559,7 @@ function keptUnderRetention(keepSubagents, child) {
59106
60559
  return keepSubagents && (child.ever_settled === true || child.role === "historian");
59107
60560
  }
59108
60561
  function roleTitle(role) {
59109
- if (role === "maintainer-a")
59110
- return "Magic Context maintainer A";
59111
- if (role === "maintainer-b")
59112
- return "Magic Context maintainer B";
59113
- return "Magic Context historian";
60562
+ return HIDDEN_CHILD_ROLE_TITLES[role];
59114
60563
  }
59115
60564
  function roleAgent(role) {
59116
60565
  if (role === "maintainer-a")
@@ -59157,9 +60606,15 @@ function errorText(value) {
59157
60606
  return String(value);
59158
60607
  }
59159
60608
  }
60609
+ var HIDDEN_CHILD_ROLE_TITLES;
59160
60610
  var init_hidden_child_record = __esm(() => {
59161
60611
  init_memory_maintainer();
59162
60612
  init_hidden_child();
60613
+ HIDDEN_CHILD_ROLE_TITLES = {
60614
+ "maintainer-a": "Magic Context maintainer A",
60615
+ "maintainer-b": "Magic Context maintainer B",
60616
+ historian: "Magic Context historian"
60617
+ };
59163
60618
  });
59164
60619
 
59165
60620
  // src/v2/hidden-child-native.ts
@@ -59686,7 +61141,8 @@ async function createV2HiddenCompletionExecutor(host, options) {
59686
61141
  aborted
59687
61142
  ]);
59688
61143
  if (!attempt.shaped) {
59689
- throw new HiddenCompletionRefusal("hidden_prompt_unrecognized", "Host did not dispatch the hidden child context hook", true);
61144
+ const task = run.identity.title.replace(/^magic-context-dream-/, "");
61145
+ throw new HiddenCompletionRefusal("hidden_prompt_unrecognized", `Host did not dispatch the hidden child context hook (task=${task}, agent=${run.identity.agent}, child=${run.child.id}, directory=${run.identity.directory}, stage=context/HiddenChildHook.apply). Check that the child's location loads the owning Magic Context instance; changing models cannot repair missing context shaping.`, true);
59690
61146
  }
59691
61147
  const text = assistantText(row);
59692
61148
  const system = typeof request.body.system === "string" ? request.body.system : run.identity.system;
@@ -60010,392 +61466,6 @@ class DeletedSessionTombstones {
60010
61466
  }
60011
61467
  }
60012
61468
 
60013
- // src/v2/tool-result.ts
60014
- function isToolError(part) {
60015
- const result = part.result;
60016
- return part.resultType === "error" || result?.type === "error" || result !== null && typeof result === "object" && "error" in result;
60017
- }
60018
- function toolErrorText(result) {
60019
- const value = result.value === undefined ? result : result.value;
60020
- return typeof value === "string" ? value : JSON.stringify(value);
60021
- }
60022
- function toolStateContent(state) {
60023
- if (typeof state.output === "string")
60024
- return state.output;
60025
- if (state.status === "error")
60026
- return toolErrorText({ error: state.error, content: state.content ?? [] });
60027
- if (typeof state.content === "string")
60028
- return state.content;
60029
- if (!Array.isArray(state.content))
60030
- return "";
60031
- return state.content.map((part) => {
60032
- if (typeof part === "string")
60033
- return part;
60034
- if (!part || typeof part !== "object")
60035
- return "";
60036
- const value = part;
60037
- if (typeof value.text === "string")
60038
- return value.text;
60039
- if (typeof value.value === "string")
60040
- return value.value;
60041
- return "";
60042
- }).filter(Boolean).join(`
60043
- `);
60044
- }
60045
-
60046
- // src/v2/hooks/payload.ts
60047
- function rejectContentPart(message, part, reason) {
60048
- const type = typeof part.type === "string" ? part.type : "<missing>";
60049
- const text = `v2 context omitted a content part the host would reject: type=${type} message=${String(message.info.id ?? "<head>")} reason=${reason}`;
60050
- if (typeof message.info.sessionID === "string")
60051
- sessionLog(message.info.sessionID, text);
60052
- else
60053
- log(text);
60054
- }
60055
- function isHostInstance(value) {
60056
- if (!value || typeof value !== "object" || Array.isArray(value))
60057
- return false;
60058
- const prototype = Object.getPrototypeOf(value);
60059
- return prototype !== Object.prototype && prototype !== null;
60060
- }
60061
- function containsHostInstance(value) {
60062
- if (isHostInstance(value))
60063
- return true;
60064
- if (Array.isArray(value))
60065
- return value.some(containsHostInstance);
60066
- if (value && typeof value === "object")
60067
- return Object.values(value).some(containsHostInstance);
60068
- return false;
60069
- }
60070
- function clonePreservingInstances(value) {
60071
- if (isHostInstance(value))
60072
- return value;
60073
- if (Array.isArray(value))
60074
- return value.map(clonePreservingInstances);
60075
- if (value && typeof value === "object") {
60076
- const copy = {};
60077
- for (const [key, entry] of Object.entries(value))
60078
- copy[key] = clonePreservingInstances(entry);
60079
- return copy;
60080
- }
60081
- return value;
60082
- }
60083
- function contentKey(value) {
60084
- const plain = (entry) => {
60085
- if (ArrayBuffer.isView(entry)) {
60086
- return {
60087
- bytes: Buffer.from(entry.buffer, entry.byteOffset, entry.byteLength).toString("base64")
60088
- };
60089
- }
60090
- if (Array.isArray(entry))
60091
- return entry.map(plain);
60092
- if (entry && typeof entry === "object") {
60093
- return Object.fromEntries(Object.entries(entry).map(([key, nested]) => [key, plain(nested)]));
60094
- }
60095
- return entry;
60096
- };
60097
- return JSON.stringify(plain(value));
60098
- }
60099
- function fileContentValue(result) {
60100
- const value = result?.value;
60101
- if (result?.type !== "content" || !Array.isArray(value))
60102
- return;
60103
- return value.some((item) => item?.type === "file") ? value : undefined;
60104
- }
60105
- function contentValueText(value) {
60106
- return value.filter((item) => item?.type === "text" && typeof item.text === "string").map((item) => item.text).join(`
60107
- `);
60108
- }
60109
- function contentValueAttachments(value) {
60110
- return value.filter((item) => item?.type === "file").map((item) => ({
60111
- type: "file",
60112
- mime: item.mime,
60113
- url: item.uri,
60114
- ...typeof item.name === "string" ? { filename: item.name } : {}
60115
- }));
60116
- }
60117
- function rebuildFileContent(value, output) {
60118
- if (isDroppedToolOutput(output))
60119
- return { type: "text", value: output };
60120
- const rebuilt = [];
60121
- let placed = false;
60122
- for (const item of value) {
60123
- if (item?.type !== "text") {
60124
- rebuilt.push(item);
60125
- continue;
60126
- }
60127
- if (placed)
60128
- continue;
60129
- placed = true;
60130
- rebuilt.push({ ...item, text: output });
60131
- }
60132
- if (!placed && output)
60133
- rebuilt.unshift({ type: "text", text: output });
60134
- return { type: "content", value: rebuilt };
60135
- }
60136
- function adaptPayload(draft, admittedIDs = new Set) {
60137
- const existingHeads = new Map(draft.messages.filter((m) => HEAD_IDS.some((id) => m.id === id)).map((m) => [m.id, m]));
60138
- const source = draft.messages.filter((m) => !existingHeads.has(m.id));
60139
- const results = new Map;
60140
- const hostParts = new Map;
60141
- const hostPartTypes = new Set;
60142
- for (const message of source) {
60143
- for (const part of message.content) {
60144
- if (part.type !== "tool-call" && part.type !== "tool-result" && part.type !== "tool" && containsHostInstance(part)) {
60145
- const key = contentKey(part);
60146
- if (!hostParts.has(key))
60147
- hostParts.set(key, part);
60148
- hostPartTypes.add(part.type);
60149
- }
60150
- if (part.type === "tool-result" && typeof part.id === "string") {
60151
- const queue = results.get(part.id) ?? [];
60152
- queue.push({ part, message });
60153
- results.set(part.id, queue);
60154
- }
60155
- }
60156
- }
60157
- const paired = new Set;
60158
- const bridges = new Map;
60159
- const originals = new Map;
60160
- const bridgesByMessageID = new Map;
60161
- const bridgesByCarrier = new Map;
60162
- const originalsByID = new Map;
60163
- const bridgesFor = (message) => typeof message.info.id === "string" ? bridgesByMessageID.get(message.info.id) : bridgesByCarrier.get(message);
60164
- const nativeTool = (call, result) => {
60165
- const hostResult = result?.part.result;
60166
- const failed = result !== undefined && isToolError(result.part);
60167
- const value = hostResult?.value;
60168
- const files = fileContentValue(hostResult);
60169
- const output = failed && hostResult ? toolErrorText(hostResult) : files ? contentValueText(files) : typeof value === "string" ? value : value === undefined ? "" : JSON.stringify(value);
60170
- const part = {
60171
- type: "tool",
60172
- callID: call?.id ?? result?.part.id,
60173
- tool: call?.name ?? result?.part.name,
60174
- state: {
60175
- input: structuredClone(call?.input ?? {}),
60176
- status: failed ? "error" : result ? "completed" : "running",
60177
- ...result ? { output } : {},
60178
- ...result && (call?.name ?? result.part.name) === "question" ? { metadata: { userAnswer: true } } : {},
60179
- ...files ? { attachments: contentValueAttachments(files) } : {}
60180
- }
60181
- };
60182
- bridges.set(part, {
60183
- call,
60184
- result: result?.part,
60185
- resultMessage: result?.message,
60186
- output,
60187
- files
60188
- });
60189
- if (result)
60190
- paired.add(result.part);
60191
- return part;
60192
- };
60193
- const convertedTool = (source) => {
60194
- const sourceState = source.state && typeof source.state === "object" ? source.state : {};
60195
- const output = toolStateContent(sourceState);
60196
- const { content: _content, ...projectedState } = structuredClone(sourceState);
60197
- const part = {
60198
- type: "tool",
60199
- callID: source.callID ?? source.id,
60200
- tool: source.tool ?? source.name,
60201
- state: { ...projectedState, output }
60202
- };
60203
- bridges.set(part, { native: source, output });
60204
- return part;
60205
- };
60206
- const callParts = new Map;
60207
- for (const message of source) {
60208
- for (const part of message.content) {
60209
- if (part.type === "tool-call") {
60210
- callParts.set(part, nativeTool(part, results.get(String(part.id))?.shift()));
60211
- } else if (part.type === "tool") {
60212
- callParts.set(part, convertedTool(part));
60213
- }
60214
- }
60215
- }
60216
- const messages = [];
60217
- for (const message of source) {
60218
- const parts = [];
60219
- for (const content of message.content) {
60220
- if (paired.has(content))
60221
- continue;
60222
- const part = callParts.get(content) ?? (content.type === "tool-result" ? nativeTool(undefined, { part: content, message }) : clonePreservingInstances(content));
60223
- if (message.id && admittedIDs.has(message.id))
60224
- part.synthetic = true;
60225
- parts.push(part);
60226
- }
60227
- if (!parts.length && message.content.length)
60228
- continue;
60229
- const byCallID = new Map;
60230
- for (const part of parts) {
60231
- const bridge = bridges.get(part);
60232
- if (bridge && typeof part.callID === "string")
60233
- byCallID.set(part.callID, bridge);
60234
- }
60235
- if (typeof message.id === "string") {
60236
- bridgesByMessageID.set(message.id, byCallID);
60237
- originalsByID.set(message.id, message);
60238
- }
60239
- const mapped = {
60240
- info: {
60241
- id: message.id,
60242
- sessionID: draft.sessionID,
60243
- role: message.role,
60244
- ...{
60245
- providerID: draft.model.providerID,
60246
- modelID: draft.model.id,
60247
- agent: draft.agent
60248
- }
60249
- },
60250
- parts
60251
- };
60252
- originals.set(mapped, message);
60253
- if (typeof message.id !== "string")
60254
- bridgesByCarrier.set(mapped, byCallID);
60255
- messages.push(mapped);
60256
- }
60257
- return {
60258
- messages,
60259
- commit() {
60260
- let head = 0;
60261
- const rendered = [];
60262
- for (const message of messages) {
60263
- const original = originals.get(message) ?? (typeof message.info.id === "string" ? originalsByID.get(message.info.id) : undefined);
60264
- const id = message.info.syntheticHead ? HEAD_IDS[head++] : original?.id;
60265
- const content = [];
60266
- const following = [];
60267
- for (const candidate of message.parts) {
60268
- const part = candidate;
60269
- const bridge = bridges.get(part) ?? (part.type === "tool" && typeof part.callID === "string" ? bridgesFor(message)?.get(part.callID) : undefined);
60270
- if (!bridge) {
60271
- if (part.type === "tool") {
60272
- const state = part.state;
60273
- if (typeof part.callID !== "string" || typeof part.tool !== "string" || !state || typeof state !== "object") {
60274
- rejectContentPart(message, part, "tool has no call ID, name, or state");
60275
- continue;
60276
- }
60277
- content.push({
60278
- type: "tool-call",
60279
- id: part.callID,
60280
- name: part.tool,
60281
- input: state.input
60282
- });
60283
- if (state.status === "completed" || state.status === "error") {
60284
- following.push({
60285
- role: "tool",
60286
- content: [
60287
- {
60288
- type: "tool-result",
60289
- id: part.callID,
60290
- name: part.tool,
60291
- result: {
60292
- type: state.status === "error" ? "error" : "text",
60293
- value: toolStateContent(state)
60294
- }
60295
- }
60296
- ]
60297
- });
60298
- }
60299
- continue;
60300
- }
60301
- if (part.type === "file") {
60302
- const mime = part.mime;
60303
- const url = part.url;
60304
- const prefix = `data:${mime};base64,`;
60305
- if (typeof mime !== "string" || !/^image\/(png|jpeg|gif|webp)$/.test(mime) || typeof url !== "string" || !url.startsWith(prefix)) {
60306
- rejectContentPart(message, part, "file is not an inline supported image");
60307
- continue;
60308
- }
60309
- const asset = hostMediaAsset(url.slice(prefix.length), mime);
60310
- if (typeof asset === "string") {
60311
- rejectContentPart(message, part, `host media unavailable: ${asset}`);
60312
- continue;
60313
- }
60314
- content.push({ type: "media", media: asset });
60315
- continue;
60316
- }
60317
- if (!HOST_CONTENT_TYPES.has(String(part.type))) {
60318
- rejectContentPart(message, part, "unknown host content type");
60319
- continue;
60320
- }
60321
- const { synthetic: _synthetic, ...clean } = part;
60322
- const original = hostPartTypes.has(clean.type) && !containsHostInstance(clean) ? hostParts.get(contentKey(clean)) : undefined;
60323
- content.push(original ?? clean);
60324
- continue;
60325
- }
60326
- const state = part.state;
60327
- if (bridge.native) {
60328
- const native = bridge.native;
60329
- const callID = String(native.id ?? native.callID ?? part.callID);
60330
- const name = String(native.name ?? native.tool ?? part.tool);
60331
- content.push({ type: "tool-call", id: callID, name, input: state.input });
60332
- if (state.status === "completed" || state.status === "error") {
60333
- following.push({
60334
- role: "tool",
60335
- content: [
60336
- {
60337
- type: "tool-result",
60338
- id: callID,
60339
- name,
60340
- result: {
60341
- type: state.status === "error" ? "error" : "text",
60342
- value: state.output
60343
- }
60344
- }
60345
- ]
60346
- });
60347
- }
60348
- continue;
60349
- }
60350
- if (bridge.call)
60351
- content.push({ ...bridge.call, input: state.input });
60352
- if (bridge.result && bridge.resultMessage) {
60353
- const result = {
60354
- ...bridge.result,
60355
- result: state.output === bridge.output ? bridge.result.result : bridge.files && typeof state.output === "string" ? rebuildFileContent(bridge.files, state.output) : {
60356
- type: state.status === "error" ? "error" : "text",
60357
- value: state.output
60358
- }
60359
- };
60360
- if (bridge.resultMessage === original)
60361
- content.push(result);
60362
- else
60363
- following.push({ ...bridge.resultMessage, content: [result] });
60364
- }
60365
- }
60366
- if (content.length || !original?.content.length) {
60367
- const value = {
60368
- ...original,
60369
- id,
60370
- role: message.info.role ?? "user",
60371
- content
60372
- };
60373
- const existing = id ? existingHeads.get(id) : undefined;
60374
- rendered.push(existing ? Object.assign(existing, { content }) : value);
60375
- }
60376
- rendered.push(...following);
60377
- }
60378
- draft.messages.splice(0, draft.messages.length, ...rendered);
60379
- }
60380
- };
60381
- }
60382
- var HEAD_IDS, HOST_CONTENT_TYPES;
60383
- var init_payload = __esm(() => {
60384
- init_ctx_reduce_nudge();
60385
- init_logger();
60386
- init_host_media();
60387
- HEAD_IDS = ["__magic_context_v2_m0__", "__magic_context_v2_m1__"];
60388
- HOST_CONTENT_TYPES = new Set([
60389
- "text",
60390
- "media",
60391
- "tool-call",
60392
- "tool-result",
60393
- "reasoning",
60394
- "compaction",
60395
- "effort"
60396
- ]);
60397
- });
60398
-
60399
61469
  // src/v2/hooks/generate.ts
60400
61470
  class V2GenerateReplay {
60401
61471
  prefixes = new Map;
@@ -60445,9 +61515,9 @@ var init_hidden_terminal_error = __esm(() => {
60445
61515
  });
60446
61516
 
60447
61517
  // src/v2/hooks/lkg-system.ts
60448
- import { createHash as createHash19 } from "node:crypto";
60449
- function identity(slot) {
60450
- return createHash19("sha256").update(JSON.stringify({
61518
+ import { createHash as createHash21 } from "node:crypto";
61519
+ function identity2(slot) {
61520
+ return createHash21("sha256").update(JSON.stringify({
60451
61521
  capturedAt: slot.capturedAt,
60452
61522
  captureSequence: slot.captureSequence,
60453
61523
  rowVersion: slot.rowVersion,
@@ -60462,14 +61532,14 @@ class V2LkgSystemReplay {
60462
61532
  systems = new BoundedSessionMap(1000);
60463
61533
  capture(sessionId, slot, input, served) {
60464
61534
  this.systems.set(sessionId, {
60465
- slot: identity(slot),
61535
+ slot: identity2(slot),
60466
61536
  input: JSON.stringify(input),
60467
61537
  served: structuredClone(served)
60468
61538
  });
60469
61539
  }
60470
61540
  restore(sessionId, slot, input, target) {
60471
61541
  const saved = this.systems.get(sessionId);
60472
- if (!slot || !saved || saved.slot !== identity(slot) || saved.input !== JSON.stringify(input))
61542
+ if (!slot || !saved || saved.slot !== identity2(slot) || saved.input !== JSON.stringify(input))
60473
61543
  return false;
60474
61544
  target.splice(0, target.length, ...structuredClone(saved.served));
60475
61545
  return true;
@@ -60662,7 +61732,7 @@ function hasRequiredSessionAPI(session) {
60662
61732
  var OPENCODE2_SESSION_API_NOTICE = "Magic Context is disabled: OpenCode 2.0.22 or newer is required (session.remove and session.compact). Upgrade OpenCode and restart.";
60663
61733
 
60664
61734
  // src/features/magic-context/session-project-backfill.ts
60665
- import { randomUUID as randomUUID4 } from "node:crypto";
61735
+ import { randomUUID as randomUUID6 } from "node:crypto";
60666
61736
  import { existsSync as existsSync14 } from "node:fs";
60667
61737
  function ensureBackfillStateTable2(db) {
60668
61738
  db.exec(`
@@ -60799,7 +61869,7 @@ async function runSessionProjectBackfill(db, source, options = {}) {
60799
61869
  const now = options.now ?? Date.now;
60800
61870
  const resolveIdentity = options.resolveIdentity ?? ((directory) => resolveProjectIdentityForSession(directory, options.allowHomeProject));
60801
61871
  const yieldFn = options.yieldFn ?? defaultYieldFn;
60802
- const holderId = options.holderId ?? randomUUID4();
61872
+ const holderId = options.holderId ?? randomUUID6();
60803
61873
  const readPage = createPageReader(source);
60804
61874
  const result = {
60805
61875
  status: "completed",
@@ -62222,7 +63292,7 @@ __export(exports_regexes2, {
62222
63292
  extendedDuration: () => extendedDuration,
62223
63293
  guid: () => guid2,
62224
63294
  hex: () => hex,
62225
- hostname: () => hostname,
63295
+ hostname: () => hostname2,
62226
63296
  html5Email: () => html5Email,
62227
63297
  idnEmail: () => idnEmail,
62228
63298
  integer: () => integer2,
@@ -62292,7 +63362,7 @@ var cuid3, cuid22, ulid2, xid2, ksuid2, nanoid2, duration3, extendedDuration, gu
62292
63362
  if (!version)
62293
63363
  return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/;
62294
63364
  return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
62295
- }, uuid4, uuid6, uuid7, email2, html5Email, rfc5322Email, unicodeEmail, idnEmail, browserEmail, _emoji3 = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`, ipv42, ipv62, cidrv42, cidrv62, base642, base64url2, hostname, domain, e1642, dateSource2 = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`, date3, string3 = (params) => {
63365
+ }, uuid4, uuid6, uuid7, email2, html5Email, rfc5322Email, unicodeEmail, idnEmail, browserEmail, _emoji3 = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`, ipv42, ipv62, cidrv42, cidrv62, base642, base64url2, hostname2, domain, e1642, dateSource2 = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`, date3, string3 = (params) => {
62296
63366
  const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
62297
63367
  return new RegExp(`^${regex}$`);
62298
63368
  }, bigint2, integer2, number3, boolean3, _null3, _undefined3, lowercase2, uppercase2, hex, md5_hex, md5_base64, md5_base64url, sha1_hex, sha1_base64, sha1_base64url, sha256_hex, sha256_base64, sha256_base64url, sha384_hex, sha384_base64, sha384_base64url, sha512_hex, sha512_base64, sha512_base64url;
@@ -62321,7 +63391,7 @@ var init_regexes2 = __esm(() => {
62321
63391
  cidrv62 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
62322
63392
  base642 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
62323
63393
  base64url2 = /^[A-Za-z0-9_-]*$/;
62324
- hostname = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
63394
+ hostname2 = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
62325
63395
  domain = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
62326
63396
  e1642 = /^\+(?:[0-9]){6,14}[0-9]$/;
62327
63397
  date3 = /* @__PURE__ */ new RegExp(`^${dateSource2}$`);
@@ -63432,7 +64502,7 @@ var init_schemas3 = __esm(() => {
63432
64502
  code: "invalid_format",
63433
64503
  format: "url",
63434
64504
  note: "Invalid hostname",
63435
- pattern: hostname.source,
64505
+ pattern: hostname2.source,
63436
64506
  input: payload.value,
63437
64507
  inst,
63438
64508
  continue: !def.abort
@@ -72706,8 +73776,8 @@ function jwt(params) {
72706
73776
  function stringFormat(format, fnOrRegex, _params = {}) {
72707
73777
  return _stringFormat2(ZodCustomStringFormat, format, fnOrRegex, _params);
72708
73778
  }
72709
- function hostname2(_params) {
72710
- return _stringFormat2(ZodCustomStringFormat, "hostname", hostname, _params);
73779
+ function hostname3(_params) {
73780
+ return _stringFormat2(ZodCustomStringFormat, "hostname", hostname2, _params);
72711
73781
  }
72712
73782
  function hex2(_params) {
72713
73783
  return _stringFormat2(ZodCustomStringFormat, "hex", hex, _params);
@@ -73792,7 +74862,7 @@ __export(exports_external2, {
73792
74862
  guid: () => guid3,
73793
74863
  hash: () => hash,
73794
74864
  hex: () => hex2,
73795
- hostname: () => hostname2,
74865
+ hostname: () => hostname3,
73796
74866
  httpUrl: () => httpUrl,
73797
74867
  includes: () => _includes2,
73798
74868
  instanceof: () => _instanceof,
@@ -74073,7 +75143,7 @@ function resolveCtxExpandMode(args, domain) {
74073
75143
  var init_mode = () => {};
74074
75144
 
74075
75145
  // src/tools/ctx-expand/render.ts
74076
- import { createHash as createHash20 } from "node:crypto";
75146
+ import { createHash as createHash22 } from "node:crypto";
74077
75147
  function isRecord7(value) {
74078
75148
  return value !== null && typeof value === "object" && !Array.isArray(value);
74079
75149
  }
@@ -74162,7 +75232,7 @@ function reasoningOf(part) {
74162
75232
  function renderPartPreview(part, expandTools) {
74163
75233
  if (!isRecord7(part))
74164
75234
  return null;
74165
- const expansion = expandToolPart(part, expandTools);
75235
+ const expansion = expandToolPart(part, expandTools, true);
74166
75236
  if (expansion !== null)
74167
75237
  return ` • tool ${part.tool}: ${expansion}`;
74168
75238
  const text = textOf(part);
@@ -74297,7 +75367,7 @@ function renderItemByTag(db, sessionId, number, textIndexDomain = "part") {
74297
75367
  const index = scoped ? Number(scoped[2]) : 0;
74298
75368
  const piText = message.parts.filter((part) => isRecord7(part) && part.type === "text");
74299
75369
  const part = message.parts[index];
74300
- const matching = derived ? piText.filter((part) => isRecord7(part) && typeof part.text === "string" && createHash20("sha256").update(part.text).digest("hex") === derived[3]) : [];
75370
+ const matching = derived ? piText.filter((part) => isRecord7(part) && typeof part.text === "string" && createHash22("sha256").update(part.text).digest("hex") === derived[3]) : [];
74301
75371
  const selected = derived ? matching[Number(derived[4])] : textIndexDomain === "text" ? piText[index] : part;
74302
75372
  return selected ? renderPartFull(selected) ?? "" : `Tag ${number}'s original text part is no longer in stored history.`;
74303
75373
  }
@@ -74445,7 +75515,7 @@ var init_memory_visibility = __esm(() => {
74445
75515
 
74446
75516
  // src/tools/ctx-memory/types.ts
74447
75517
  var CTX_MEMORY_ACTIONS;
74448
- var init_types = __esm(() => {
75518
+ var init_types2 = __esm(() => {
74449
75519
  CTX_MEMORY_ACTIONS = ["write", "update", "archive", "merge", "get"];
74450
75520
  });
74451
75521
 
@@ -74469,7 +75539,7 @@ var init_verification_recording = __esm(() => {
74469
75539
 
74470
75540
  // src/tools/ctx-memory/tools.ts
74471
75541
  function isMemoryCategory2(value) {
74472
- return MEMORY_CATEGORIES.has(value);
75542
+ return MEMORY_CATEGORIES2.has(value);
74473
75543
  }
74474
75544
  function getAllowedActions(deps) {
74475
75545
  const allowed = deps.allowedActions?.length ? deps.allowedActions : CTX_MEMORY_ACTIONS;
@@ -74871,7 +75941,7 @@ function createCtxMemoryTools(deps) {
74871
75941
  [CTX_MEMORY_TOOL_NAME]: createCtxMemoryTool(deps)
74872
75942
  };
74873
75943
  }
74874
- var MEMORY_CATEGORIES, GET_NOT_VISIBLE_MESSAGE = (id) => `id ${id}: not found or not visible from this project`, GET_MAX_IDS = 20, DUPLICATE_MEMORY_ERROR = (id) => `Error: Memory content already exists as ID ${id}; merge or archive duplicates instead.`, ctxMemoryArgsShape, ctxMemoryArgsSchema;
75944
+ var MEMORY_CATEGORIES2, GET_NOT_VISIBLE_MESSAGE = (id) => `id ${id}: not found or not visible from this project`, GET_MAX_IDS = 20, DUPLICATE_MEMORY_ERROR = (id) => `Error: Memory content already exists as ID ${id}; merge or archive duplicates instead.`, ctxMemoryArgsShape, ctxMemoryArgsSchema;
74875
75945
  var init_tools2 = __esm(async () => {
74876
75946
  init_dist();
74877
75947
  init_memory_visibility();
@@ -74879,7 +75949,7 @@ var init_tools2 = __esm(async () => {
74879
75949
  init_project_identity();
74880
75950
  init_user_facing_codes();
74881
75951
  init_unwrap_imitated_reduced_args();
74882
- init_types();
75952
+ init_types2();
74883
75953
  init_verification_recording();
74884
75954
  await __promiseAll([
74885
75955
  init_context_authority(),
@@ -74887,7 +75957,7 @@ var init_tools2 = __esm(async () => {
74887
75957
  init_storage_memory(),
74888
75958
  init_storage()
74889
75959
  ]);
74890
- MEMORY_CATEGORIES = new Set(CATEGORY_PRIORITY);
75960
+ MEMORY_CATEGORIES2 = new Set(CATEGORY_PRIORITY);
74891
75961
  ctxMemoryArgsShape = {
74892
75962
  action: tool.schema.enum([...CTX_MEMORY_ACTIONS]).optional().describe("write | update | archive | merge | get"),
74893
75963
  content: tool.schema.string().optional().describe("The memory text — one standalone fact (write, update, merge)."),
@@ -74901,7 +75971,7 @@ var init_tools2 = __esm(async () => {
74901
75971
  // src/tools/ctx-memory/index.ts
74902
75972
  var init_ctx_memory = __esm(async () => {
74903
75973
  init_tools2();
74904
- init_types();
75974
+ init_types2();
74905
75975
  });
74906
75976
 
74907
75977
  // src/tools/ctx-note/constants.ts
@@ -75934,6 +77004,8 @@ async function registerContext(context) {
75934
77004
  return;
75935
77005
  }
75936
77006
  const folds = new FoldOwner(context.storage);
77007
+ const nativeFoldMemory = !compactionOff && process.env.MMC_OC2_INVISIBLE_FOLD === "1" ? nativeFoldCache(() => storage.require()) : undefined;
77008
+ const nativeFoldReplay = nativeFoldMemory ? new NativeFoldReplay(nativeFoldMemory, () => openStoreReader()) : undefined;
75937
77009
  setOutputReserveConfig(config.output_reserve);
75938
77010
  const queriedModels = new Set;
75939
77011
  const rawLimits = new Map;
@@ -75965,7 +77037,7 @@ async function registerContext(context) {
75965
77037
  let storageRecoveryAnnounced = false;
75966
77038
  const storageNoticeBySession = new Map;
75967
77039
  const storeStorageNotice = (sessionID, text, what) => {
75968
- withoutSqliteTransformPass(() => context.session.wait({ sessionID }).then(() => deliverSynthetic(context, sessionID, text)).catch((error) => sessionLog(sessionID, `v2 storage ${what} notice could not be delivered:`, error)));
77040
+ withoutSqliteTransformPass(() => Promise.resolve().then(() => context.session.wait({ sessionID })).then(() => deliverSynthetic(context, sessionID, text)).catch((error) => sessionLog(sessionID, `v2 storage ${what} notice could not be delivered:`, error)));
75969
77041
  };
75970
77042
  const noticeStorageRefusal = (sessionID, reason) => {
75971
77043
  const message = formatStorageRefusalNotice(reason);
@@ -76232,6 +77304,14 @@ async function registerContext(context) {
76232
77304
  }
76233
77305
  return unsafe;
76234
77306
  };
77307
+ const hostFolds = nativeFoldReplay && nativeFoldMemory ? new HostFoldPolicy({
77308
+ session: context.session,
77309
+ storage: nativeFoldMemory,
77310
+ openReader: openStoreReader,
77311
+ canReplay: (sessionID, reader) => nativeFoldReplay.canReplay(sessionID, reader),
77312
+ threshold: Number(process.env.MMC_OC2_HOST_FOLD_ROWS ?? 1000),
77313
+ log: sessionLog
77314
+ }) : undefined;
76235
77315
  const usageController = new AbortController;
76236
77316
  const usageDone = (async () => {
76237
77317
  try {
@@ -76242,6 +77322,10 @@ async function registerContext(context) {
76242
77322
  if (!event.data?.sessionID)
76243
77323
  continue;
76244
77324
  const sessionID = event.data.sessionID;
77325
+ if (event.type?.startsWith("session.revert.") || event.type === "session.message.content.updated") {
77326
+ nativeFoldReplay?.onEvent(event);
77327
+ restoredRows.forget(sessionID);
77328
+ }
76245
77329
  if (event.type === "session.error" || event.type === "session.execution.failed") {
76246
77330
  const error = hiddenTerminalError(event);
76247
77331
  if (error !== undefined)
@@ -76267,6 +77351,7 @@ async function registerContext(context) {
76267
77351
  pendingMaterializationSessions.delete(sessionID);
76268
77352
  lastHeuristicsTurnId.delete(sessionID);
76269
77353
  restoredRows.forget(sessionID);
77354
+ await nativeFoldReplay?.forget(sessionID);
76270
77355
  generateReplay.forget(sessionID);
76271
77356
  systemPromptRefreshSessions.delete(sessionID);
76272
77357
  systemPrompt?.clearSession(sessionID);
@@ -76279,11 +77364,14 @@ async function registerContext(context) {
76279
77364
  if (event.type !== "session.execution.succeeded")
76280
77365
  continue;
76281
77366
  const model = liveModels.get(sessionID);
76282
- if (model)
77367
+ if (model) {
76283
77368
  await recordUsage({
76284
77369
  sessionID,
76285
77370
  model: { providerID: model.providerID, id: model.modelID }
76286
77371
  });
77372
+ if (!compactionOff && typeof await nativeFoldReplay?.baseline(sessionID) === "string")
77373
+ hostFolds?.idle(sessionID);
77374
+ }
76287
77375
  }
76288
77376
  } catch (error) {
76289
77377
  if (!usageController.signal.aborted)
@@ -76318,10 +77406,32 @@ async function registerContext(context) {
76318
77406
  }
76319
77407
  if (compactionOff)
76320
77408
  return;
77409
+ if (nativeFoldReplay)
77410
+ rememberHostMedia(draft.messages);
76321
77411
  const reader = openStoreReader();
76322
77412
  try {
76323
77413
  const watermark = reader.latestSequenceForIds(draft.sessionID, draft.messages.flatMap((message) => message.id ? [message.id] : []));
76324
77414
  const running = reader.latestRunningCompaction(draft.sessionID);
77415
+ if (nativeFoldReplay) {
77416
+ try {
77417
+ if (await nativeFoldReplay.canReplay(draft.sessionID, reader)) {
77418
+ const state = getOrCreateSessionMeta(storage.require(), draft.sessionID);
77419
+ const summary = await nativeFoldReplay.baseline(draft.sessionID) ?? state.cachedM0Bytes?.toString("utf8") ?? "<session-history></session-history>";
77420
+ if (await nativeFoldReplay.supply({
77421
+ draft,
77422
+ reader,
77423
+ previousCut: reader.latestCompaction(draft.sessionID),
77424
+ summary
77425
+ })) {
77426
+ draft.result = { summary };
77427
+ return;
77428
+ }
77429
+ }
77430
+ } catch (error) {
77431
+ nativeFoldReplay.invalidate(draft.sessionID, true);
77432
+ sessionLog(draft.sessionID, `v2 host fold: legacy compaction reason=${JSON.stringify(String(error))}`);
77433
+ }
77434
+ }
76325
77435
  const fold = await folds.supply({
76326
77436
  sessionID: draft.sessionID,
76327
77437
  watermark,
@@ -76368,12 +77478,88 @@ async function registerContext(context) {
76368
77478
  withoutSqliteTransformPass(() => warmModelLimitCacheFromCatalog(context));
76369
77479
  agents.set(draft.sessionID, draft.agent);
76370
77480
  applyV2PromptSurfaceTools(draft, promptSurfaceRuntime, config.prompt_surface);
77481
+ const admissionDb = db ?? storage.current();
77482
+ const replayFits = (messages) => {
77483
+ const system = structuredClone(draft.system);
77484
+ if (!admissionDb || !draft.tools || !lkgSystems.restore(draft.sessionID, getSlot(draft.sessionID), draft.system, system))
77485
+ return false;
77486
+ const limit = lkgReplayLimit({
77487
+ db: admissionDb,
77488
+ sessionId: draft.sessionID,
77489
+ model: {
77490
+ providerID: draft.model.providerID,
77491
+ modelID: draft.model.id
77492
+ },
77493
+ modelKey: `${draft.model.providerID}/${draft.model.id}`
77494
+ });
77495
+ if (limit === undefined)
77496
+ return false;
77497
+ let toolDefinitionTokens = 0;
77498
+ for (const tool of Object.values(draft.tools)) {
77499
+ if (!tool || typeof tool.description !== "string" || !tool.input)
77500
+ return false;
77501
+ const schema = JSON.stringify(tool.input);
77502
+ if (!schema)
77503
+ return false;
77504
+ toolDefinitionTokens += estimateTokens(tool.description) + estimateTokens(schema);
77505
+ }
77506
+ return measureLkgReplay({
77507
+ messages,
77508
+ limit,
77509
+ estimate: () => estimateFinalWireInputTokens({
77510
+ messages,
77511
+ toolDefinitionTokens,
77512
+ systemPromptTokens: estimateTokens(JSON.stringify(system)),
77513
+ providerID: draft.model.providerID,
77514
+ modelID: draft.model.id,
77515
+ agentName: draft.agent
77516
+ })
77517
+ }).fit === "under";
77518
+ };
76371
77519
  let postFold = false;
76372
77520
  try {
76373
- const admissionDb = db ?? storage.current();
76374
77521
  if (!compactionOff && admissionDb)
76375
77522
  await withAsyncPrivilegedWriter(admissionDb, () => {
76376
77523
  return;
77524
+ }, {
77525
+ beforeRetry: (_error) => {
77526
+ let replayBlocked = false;
77527
+ const mapped = adaptPayload(draft);
77528
+ try {
77529
+ const overflowDb = db;
77530
+ if (overflowDb !== undefined && overflowDb !== null && (isEmergencyRecoveryArmed(draft.sessionID) || getOverflowState(overflowDb, draft.sessionID).needsEmergencyRecovery)) {
77531
+ replayBlocked = true;
77532
+ sessionLog(draft.sessionID, "lkg_emergency_armed");
77533
+ } else {
77534
+ const keys = resolveLkgModelKeys(mapped.messages);
77535
+ const replay = replayLkg({
77536
+ sessionId: draft.sessionID,
77537
+ messages: mapped.messages,
77538
+ modelKey: keys.modelKey,
77539
+ providerKey: keys.providerKey
77540
+ });
77541
+ if (replay.ok && !replayFits(replay.messages)) {
77542
+ replayBlocked = true;
77543
+ sessionLog(draft.sessionID, "lkg_replay_does_not_fit");
77544
+ } else if (replay.ok) {
77545
+ restoreLkgSystem();
77546
+ mapped.messages.splice(0, mapped.messages.length, ...replay.messages);
77547
+ mapped.commit();
77548
+ sessionLog(draft.sessionID, "lkg_replay_served");
77549
+ throw new V2LkgAdmissionReplay;
77550
+ } else {
77551
+ sessionLog(draft.sessionID, replay.reason);
77552
+ }
77553
+ }
77554
+ } catch (replayError) {
77555
+ if (replayError instanceof V2LkgAdmissionReplay)
77556
+ throw replayError;
77557
+ replayBlocked = true;
77558
+ sessionLog(draft.sessionID, "lkg_replay_unavailable", replayError);
77559
+ }
77560
+ if (replayBlocked)
77561
+ sessionLog(draft.sessionID, "lkg_replay_declined");
77562
+ }
76377
77563
  });
76378
77564
  if (admissionDb)
76379
77565
  hideSubagentTools(draft, admissionDb);
@@ -76528,7 +77714,28 @@ async function registerContext(context) {
76528
77714
  postFold = cut !== undefined;
76529
77715
  if (cut && !incoming)
76530
77716
  throw new Error("The host checkpoint disappeared from the context draft");
76531
- if (cut && incoming) {
77717
+ let nativeRestored;
77718
+ if (cut && nativeFoldReplay && isLocalCheckpoint(cut)) {
77719
+ try {
77720
+ nativeRestored = await nativeFoldReplay.restore(draft.sessionID, cut, `${draft.model.providerID}/${draft.model.id}`, { bounded: true });
77721
+ } catch (error) {
77722
+ nativeFoldReplay.invalidate(draft.sessionID, true);
77723
+ sessionLog(draft.sessionID, "v2 host fold: cache unavailable; restoring host rows", error);
77724
+ nativeRestored = await nativeFoldReplay.restoreRead(draft.sessionID, cut, draft.model);
77725
+ }
77726
+ }
77727
+ if (nativeRestored && nativeFoldReplay?.sourceChanged(draft.sessionID)) {
77728
+ historyRefreshSessions.add(draft.sessionID);
77729
+ pendingMaterializationSessions.add(draft.sessionID);
77730
+ }
77731
+ if (cut && incoming && nativeRestored) {
77732
+ const present = new Set(draft.messages.flatMap((message) => message.id ? [message.id] : []));
77733
+ const hidden = nativeRestored.filter((message) => {
77734
+ const id = nativeRowID(message);
77735
+ return !id || !present.has(id);
77736
+ });
77737
+ draft.messages.splice(0, draft.messages.length, ...hidden, ...draft.messages.filter((message) => message !== incoming));
77738
+ } else if (cut && incoming) {
76532
77739
  const identity = await folds.observe({
76533
77740
  sessionID: draft.sessionID,
76534
77741
  cutSeq: cut.seq,
@@ -76555,6 +77762,14 @@ async function registerContext(context) {
76555
77762
  } finally {
76556
77763
  reader.close();
76557
77764
  }
77765
+ let nativeMessages;
77766
+ if (nativeFoldReplay) {
77767
+ try {
77768
+ nativeMessages = copyNativeInput(draft.messages);
77769
+ } catch {
77770
+ nativeFoldReplay.invalidate(draft.sessionID, true);
77771
+ }
77772
+ }
76558
77773
  const mapped = adaptPayload(draft, admitted);
76559
77774
  beginV2LkgRequest(draft.sessionID, `${draft.model.providerID}/${draft.model.id}`, latestLkgResponseIds.get(draft.sessionID));
76560
77775
  await transform({}, mapped);
@@ -76580,6 +77795,14 @@ ${baseline}
76580
77795
  draft.messages.splice(0, draft.messages.length, checkpoint, ...draft.messages.filter((message) => message !== head));
76581
77796
  }
76582
77797
  }
77798
+ if (nativeMessages && !checkpoint) {
77799
+ try {
77800
+ await nativeFoldReplay?.capture(draft, nativeMessages);
77801
+ } catch (error) {
77802
+ nativeFoldReplay?.invalidate(draft.sessionID, true);
77803
+ sessionLog(draft.sessionID, "v2 host fold: optional capture unavailable", error);
77804
+ }
77805
+ }
76583
77806
  const capturedSlot = getSlot(draft.sessionID);
76584
77807
  if (capturedSlot && (!slotAtEntry || capturedSlot.capturedAt !== slotAtEntry.capturedAt || capturedSlot.captureSequence !== slotAtEntry.captureSequence || capturedSlot.jsonPrefix !== slotAtEntry.jsonPrefix)) {
76585
77808
  lkgSystems.capture(draft.sessionID, capturedSlot, systemAtEntry, draft.system);
@@ -76587,6 +77810,8 @@ ${baseline}
76587
77810
  } catch (error) {
76588
77811
  if (error instanceof V2ContextRefusal)
76589
77812
  throw error;
77813
+ if (error instanceof V2LkgAdmissionReplay)
77814
+ return;
76590
77815
  if (!compactionOff && (isTransientSqliteError(error) || error instanceof StorageBusyRefusalError)) {
76591
77816
  if (isTransientSqliteError(error)) {
76592
77817
  try {
@@ -76614,13 +77839,16 @@ ${baseline}
76614
77839
  modelKey: keys.modelKey,
76615
77840
  providerKey: keys.providerKey
76616
77841
  });
76617
- if (replay.ok) {
77842
+ if (replay.ok && !replayFits(replay.messages)) {
77843
+ sessionLog(draft.sessionID, "lkg_replay_does_not_fit");
77844
+ } else if (replay.ok) {
76618
77845
  restoreLkgSystem();
76619
77846
  replayMapped.messages.splice(0, replayMapped.messages.length, ...replay.messages);
76620
77847
  replayMapped.commit();
76621
77848
  return;
77849
+ } else {
77850
+ sessionLog(draft.sessionID, replay.reason);
76622
77851
  }
76623
- sessionLog(draft.sessionID, replay.reason);
76624
77852
  }
76625
77853
  } catch (replayError) {
76626
77854
  replayBlocked = true;
@@ -76774,7 +78002,7 @@ ${baseline}
76774
78002
  }
76775
78003
  };
76776
78004
  }
76777
- var HIDDEN_SESSION_ERROR_GRACE_MS = 50;
78005
+ var HIDDEN_SESSION_ERROR_GRACE_MS = 50, V2LkgAdmissionReplay;
76778
78006
  var init_context = __esm(async () => {
76779
78007
  init_memory_maintainer();
76780
78008
  init_config();
@@ -76789,6 +78017,7 @@ var init_context = __esm(async () => {
76789
78017
  init_derive_budgets();
76790
78018
  init_dropped_input_guard();
76791
78019
  init_emergency_fail_closed();
78020
+ init_final_wire_token_estimate();
76792
78021
  init_lkg_measured_request();
76793
78022
  init_lkg_persist();
76794
78023
  init_lkg_replay();
@@ -76806,6 +78035,7 @@ var init_context = __esm(async () => {
76806
78035
  init_parameter_descriptions();
76807
78036
  init_host_media();
76808
78037
  init_markers();
78038
+ init_memory_cache();
76809
78039
  init_owner();
76810
78040
  init_restore();
76811
78041
  init_hidden_child_cleanup();
@@ -76835,6 +78065,7 @@ var init_context = __esm(async () => {
76835
78065
  init_history_boundary_repair(),
76836
78066
  init_hook_handlers(),
76837
78067
  init_inject_compartments(),
78068
+ init_lkg_replay_fit(),
76838
78069
  init_protected_tail_boundary(),
76839
78070
  init_read_session_chunk(),
76840
78071
  init_storage_busy_refusal(),
@@ -76843,6 +78074,8 @@ var init_context = __esm(async () => {
76843
78074
  init_rpc_handlers(),
76844
78075
  init_subagent_tool_policy(),
76845
78076
  init_sqlite(),
78077
+ init_native_replay(),
78078
+ init_policy(),
76846
78079
  init_fork_inheritance(),
76847
78080
  init_store_reader(),
76848
78081
  init_channel2(),
@@ -76851,6 +78084,8 @@ var init_context = __esm(async () => {
76851
78084
  init_tools6(),
76852
78085
  init_usage_persist()
76853
78086
  ]);
78087
+ V2LkgAdmissionReplay = class V2LkgAdmissionReplay extends Error {
78088
+ };
76854
78089
  });
76855
78090
 
76856
78091
  // src/v2/server.ts