@retaw106/mmc-oc 2.0.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1 -1
- package/dist/config/live-run-config.d.ts.map +1 -1
- package/dist/config-init.js +1 -1
- package/dist/{context-95zkksn3.js → context-cx3y8abt.js} +2602 -1594
- package/dist/features/magic-context/memory/types.d.ts +1 -0
- package/dist/features/magic-context/memory/types.d.ts.map +1 -1
- package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
- package/dist/features/magic-context/session-project-storage.d.ts +8 -2
- package/dist/features/magic-context/session-project-storage.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +12 -0
- package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-session.d.ts +6 -0
- package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
- package/dist/features/magic-context/storage-tags.d.ts +2 -2
- package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
- package/dist/features/magic-context/transform-decision-log.d.ts +3 -1
- package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-types.d.ts +3 -5
- package/dist/hooks/magic-context/compartment-runner-types.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner.d.ts +2 -1
- package/dist/hooks/magic-context/compartment-runner.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-trigger.d.ts.map +1 -1
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +2 -0
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
- package/dist/hooks/magic-context/heuristic-cleanup.d.ts.map +1 -1
- package/dist/hooks/magic-context/historian-drain-gate.d.ts +11 -0
- package/dist/hooks/magic-context/historian-drain-gate.d.ts.map +1 -0
- package/dist/hooks/magic-context/lkg-replay-fit.d.ts +118 -0
- package/dist/hooks/magic-context/lkg-replay-fit.d.ts.map +1 -0
- package/dist/hooks/magic-context/lkg-replay.d.ts +2 -0
- package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -1
- package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-compartment-phase.d.ts +3 -28
- package/dist/hooks/magic-context/transform-compartment-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/{index-fg34g6a5.js → index-1j34a56j.js} +49 -8
- package/dist/{index-5pg0acf7.js → index-amycb7b1.js} +1 -1
- package/dist/{index-1mjhzqq7.js → index-dr0gepzw.js} +86 -23
- package/dist/{index-xt7y10x6.js → index-n0vnzz6h.js} +1 -1
- package/dist/{index-977gxfz0.js → index-ramz5kkm.js} +1 -1
- package/dist/{index-bdt9my9y.js → index-wtb9hzk6.js} +130 -50
- package/dist/index.js +1 -1
- package/dist/{message-time-backfill-zfernxbw.js → message-time-backfill-phrwp2fz.js} +1 -1
- package/dist/{read-session-chunk-hp464g13.js → read-session-chunk-82rkqa8j.js} +3 -3
- package/dist/{recomp-orchestrator-3qxeqwb9.js → recomp-orchestrator-vc5dkzy2.js} +6 -6
- package/dist/{send-session-notification-y29y9fsr.js → send-session-notification-rnc4nrsr.js} +3 -3
- package/dist/shared/harness.d.ts +1 -1
- package/dist/shared/harness.d.ts.map +1 -1
- package/dist/shared/historian-tool-expansions.d.ts +1 -1
- package/dist/shared/historian-tool-expansions.d.ts.map +1 -1
- package/dist/shared/historian-tool-template.d.ts +2 -2
- package/dist/shared/historian-tool-template.d.ts.map +1 -1
- package/dist/shared/model-resolution.d.ts +1 -1
- package/dist/shared/model-resolution.d.ts.map +1 -1
- package/dist/shared/sqlite.d.ts +14 -1
- package/dist/shared/sqlite.d.ts.map +1 -1
- package/dist/tools/ctx-expand/constants.d.ts +1 -1
- package/dist/tools/ctx-expand/constants.d.ts.map +1 -1
- package/dist/tools/ctx-expand/render.d.ts +3 -2
- package/dist/tools/ctx-expand/render.d.ts.map +1 -1
- package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
- package/dist/v2/fold/host-media.d.ts +4 -0
- package/dist/v2/fold/host-media.d.ts.map +1 -1
- package/dist/v2/fold/memory-cache.d.ts +70 -0
- package/dist/v2/fold/memory-cache.d.ts.map +1 -0
- package/dist/v2/fold/native-codec.d.ts +10 -0
- package/dist/v2/fold/native-codec.d.ts.map +1 -0
- package/dist/v2/fold/native-replay.d.ts +58 -0
- package/dist/v2/fold/native-replay.d.ts.map +1 -0
- package/dist/v2/fold/policy.d.ts +24 -0
- package/dist/v2/fold/policy.d.ts.map +1 -0
- package/dist/v2/hidden-child-record.d.ts +6 -0
- package/dist/v2/hidden-child-record.d.ts.map +1 -1
- package/dist/v2/hidden-completion.d.ts.map +1 -1
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/hooks/types.d.ts +6 -0
- package/dist/v2/hooks/types.d.ts.map +1 -1
- package/dist/v2/server.js +1819 -574
- package/dist/v2/store-reader.d.ts +16 -0
- package/dist/v2/store-reader.d.ts.map +1 -1
- package/dist/web/api.d.ts.map +1 -1
- package/dist/web/context-store.d.ts +2 -0
- package/dist/web/context-store.d.ts.map +1 -1
- package/dist/web/fixtures.d.ts +41 -0
- package/dist/web/fixtures.d.ts.map +1 -1
- package/dist/web/host-opencode.d.ts +9 -0
- package/dist/web/host-opencode.d.ts.map +1 -1
- package/dist/web/main.js +873 -64
- package/dist/web/memories.d.ts +18 -2
- package/dist/web/memories.d.ts.map +1 -1
- package/dist/web/ui/panel.css +3 -0
- package/dist/web/ui/panel.js +151 -80
- package/dist/web/views.d.ts +71 -9
- package/dist/web/views.d.ts.map +1 -1
- package/dist/{wrapup-orchestrator-wgb2nd19.js → wrapup-orchestrator-x9xamts2.js} +6 -6
- package/package.json +1 -1
- package/src/config/live-run-config.ts +28 -0
- package/src/shared/harness.ts +1 -1
- package/src/shared/historian-tool-defaults.json +6 -6
- package/src/shared/historian-tool-expansions.ts +65 -8
- package/src/shared/historian-tool-preview-defaults.json +13 -0
- package/src/shared/historian-tool-template.ts +48 -14
- package/src/shared/model-resolution.ts +1 -1
- package/src/shared/prompt-surface-a1-golden.md +2 -2
- package/src/shared/sqlite-acquisition-routing.test.ts +18 -0
- package/src/shared/sqlite.ts +100 -15
- 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
|
|
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
|
-
|
|
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) : "",
|
|
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 ? "; " : ", ")),
|
|
7930
|
+
return truncate(elements.join(node.join ?? (node.each ? "; " : ", ")), cap, legacyPreview);
|
|
7916
7931
|
}
|
|
7917
|
-
return truncate(scalar(value),
|
|
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
|
|
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 =
|
|
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,
|
|
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 >=
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
21098
|
+
boundary = arc.invOrdinal;
|
|
21099
|
+
break;
|
|
20946
21100
|
}
|
|
20947
21101
|
if (arc.invOrdinal >= boundary) {
|
|
20948
|
-
|
|
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
|
|
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
|
|
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
|
|
30607
|
+
function findSessionMeta(db, sessionId) {
|
|
30415
30608
|
const result = db.prepare(`SELECT ${getSessionMetaSelectColumns(db)} FROM session_meta WHERE session_id = ?`).get(sessionId);
|
|
30416
|
-
|
|
30417
|
-
|
|
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
|
|
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 =
|
|
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
|
|
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:
|
|
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
|
|
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${
|
|
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
|
-
|
|
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:
|
|
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
|
|
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,
|
|
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 =
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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 =
|
|
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 =
|
|
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.
|
|
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 &&
|
|
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:
|
|
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(),
|
|
@@ -52574,6 +52961,8 @@ function applyHeuristicCleanup(sessionId, db, targets, messageTagNumbers, config
|
|
|
52574
52961
|
continue;
|
|
52575
52962
|
const strippedSource = stripTagPrefix(stripped);
|
|
52576
52963
|
if (strippedSource.trim().length === 0) {
|
|
52964
|
+
if (target.thinkingDropProtected)
|
|
52965
|
+
continue;
|
|
52577
52966
|
const dropResult = target.drop?.() ?? "absent";
|
|
52578
52967
|
const replacement = `[dropped §${tag.tagNumber}§]`;
|
|
52579
52968
|
const didReplace = dropResult === "absent" ? target.setContent(replacement) : false;
|
|
@@ -55062,19 +55451,33 @@ function createTransform(deps) {
|
|
|
55062
55451
|
if (false) {}
|
|
55063
55452
|
return false;
|
|
55064
55453
|
};
|
|
55065
|
-
const startRecoveryRun = () => {
|
|
55066
|
-
|
|
55067
|
-
|
|
55068
|
-
|
|
55069
|
-
|
|
55070
|
-
|
|
55071
|
-
|
|
55072
|
-
|
|
55454
|
+
const startRecoveryRun = (onReady) => {
|
|
55455
|
+
if (isHistorianDrainBudgetSpent({
|
|
55456
|
+
db,
|
|
55457
|
+
sessionId,
|
|
55458
|
+
contextLimit: boundaryContextLimit,
|
|
55459
|
+
executeThresholdPercentage: boundaryExecuteThreshold,
|
|
55460
|
+
usagePercentage: boundaryUsageForProtectedTail.percentage
|
|
55461
|
+
}))
|
|
55073
55462
|
return false;
|
|
55074
|
-
|
|
55075
|
-
|
|
55463
|
+
if (!canRunCompartments || !deps.client && !deps.hiddenCompletionExecutor || getActiveCompartmentRun(sessionId))
|
|
55464
|
+
return false;
|
|
55465
|
+
const prepareRecoveryBoundary = () => {
|
|
55466
|
+
const scale = emergencyUsagePercentageEarly >= 95 ? 0.25 : 0.5;
|
|
55467
|
+
let boundarySnapshot = getRunnableBoundaryForCompartment();
|
|
55468
|
+
if (!boundarySnapshot || !hasRunnableCompartmentWindow(boundarySnapshot)) {
|
|
55469
|
+
boundarySnapshot = getRunnableBoundaryForCompartment(scale);
|
|
55470
|
+
}
|
|
55471
|
+
if (false) {}
|
|
55472
|
+
if (!boundarySnapshot || !hasRunnableCompartmentWindow(boundarySnapshot))
|
|
55473
|
+
return null;
|
|
55474
|
+
onReady?.();
|
|
55475
|
+
return boundarySnapshot;
|
|
55476
|
+
};
|
|
55477
|
+
const urgent = emergencyUsagePercentageEarly >= 95;
|
|
55478
|
+
const boundarySnapshot = urgent ? prepareRecoveryBoundary() : undefined;
|
|
55479
|
+
if (urgent && !boundarySnapshot)
|
|
55076
55480
|
return false;
|
|
55077
|
-
}
|
|
55078
55481
|
updateSessionMeta(db, sessionId, { compartmentInProgress: true });
|
|
55079
55482
|
startCompartmentAgent({
|
|
55080
55483
|
client: deps.client,
|
|
@@ -55083,7 +55486,7 @@ function createTransform(deps) {
|
|
|
55083
55486
|
db,
|
|
55084
55487
|
sessionId,
|
|
55085
55488
|
historianChunkTokens: historianRun?.chunkTokens ?? deps.getHistorianChunkTokens?.() ?? 20000,
|
|
55086
|
-
boundarySnapshot,
|
|
55489
|
+
boundarySnapshot: boundarySnapshot ?? undefined,
|
|
55087
55490
|
currentContextLimit: boundaryContextLimit,
|
|
55088
55491
|
historyBudgetTokens,
|
|
55089
55492
|
historianTimeoutMs: historianRun?.timeoutMs ?? deps.historianTimeoutMs,
|
|
@@ -55103,7 +55506,7 @@ function createTransform(deps) {
|
|
|
55103
55506
|
deferredHistoryRefreshSessions.add(sid);
|
|
55104
55507
|
deferredMaterializationSessions.add(sid);
|
|
55105
55508
|
}
|
|
55106
|
-
});
|
|
55509
|
+
}, undefined, urgent ? undefined : prepareRecoveryBoundary);
|
|
55107
55510
|
return true;
|
|
55108
55511
|
};
|
|
55109
55512
|
if (fullFeatureMode && !compactionOff && historianFailureState.failureCount > 0 && emergencyUsagePercentageEarly >= 95 && !recoveryNoHeadEscapeActive) {
|
|
@@ -55117,13 +55520,15 @@ function createTransform(deps) {
|
|
|
55117
55520
|
sessionLog(sessionId, "transform: emergency recovery remains armed — no complete eligible head before protected tail");
|
|
55118
55521
|
}
|
|
55119
55522
|
sessionLog(sessionId, `EMERGENCY: historian recovery requested at ${emergencyPercentage}%, failures: ${historianFailureState.failureCount}`);
|
|
55120
|
-
} else if (fullFeatureMode && !compactionOff && isFirstTransformPassForSession && historianFailureState.failureCount > 0
|
|
55121
|
-
|
|
55122
|
-
|
|
55123
|
-
|
|
55523
|
+
} else if (fullFeatureMode && !compactionOff && isFirstTransformPassForSession && historianFailureState.failureCount > 0) {
|
|
55524
|
+
startRecoveryRun(() => {
|
|
55525
|
+
sessionLog(sessionId, `transform: historian recovery triggered on session load after ${historianFailureState.failureCount} failure(s)`);
|
|
55526
|
+
if (deps.client) {
|
|
55527
|
+
withoutSqliteTransformPass(() => sendStatusNotification(deps.client, sessionId, `## Historian recovery
|
|
55124
55528
|
|
|
55125
55529
|
Historian previously failed ${historianFailureState.failureCount} time(s), so Magic Context is retrying history comparting immediately after restart.`, notificationParams));
|
|
55126
|
-
|
|
55530
|
+
}
|
|
55531
|
+
});
|
|
55127
55532
|
}
|
|
55128
55533
|
logTransformTiming(sessionId, "emergencyRecoveryBlock", tFirstPass);
|
|
55129
55534
|
const memoryProjectDirectory = compartmentDirectory || process.cwd();
|
|
@@ -55670,7 +56075,9 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
55670
56075
|
input: lkgInput,
|
|
55671
56076
|
output: messages,
|
|
55672
56077
|
modelKey,
|
|
55673
|
-
providerKey
|
|
56078
|
+
providerKey,
|
|
56079
|
+
systemPromptTokens: sessionMeta.systemPromptTokens,
|
|
56080
|
+
agentName: notificationParams?.agent
|
|
55674
56081
|
});
|
|
55675
56082
|
if (captured) {
|
|
55676
56083
|
const capturedSlot = getInMemorySlot(sessionId);
|
|
@@ -55964,6 +56371,7 @@ var init_transform = __esm(async () => {
|
|
|
55964
56371
|
init_ctx_reduce_availability(),
|
|
55965
56372
|
init_degraded_pass_refusal(),
|
|
55966
56373
|
init_event_resolvers(),
|
|
56374
|
+
init_historian_drain_gate(),
|
|
55967
56375
|
init_inject_compartments(),
|
|
55968
56376
|
init_protected_tail_boundary(),
|
|
55969
56377
|
init_read_session_chunk(),
|
|
@@ -58598,7 +59006,8 @@ function hostUsesMediaAssets() {
|
|
|
58598
59006
|
schemaUsesAssets.set(messageClass, uses);
|
|
58599
59007
|
return uses;
|
|
58600
59008
|
}
|
|
58601
|
-
function decodeThroughHostSchema(
|
|
59009
|
+
function decodeThroughHostSchema(encoded) {
|
|
59010
|
+
const source = encoded.source;
|
|
58602
59011
|
const messageClass = rememberedMessageClass;
|
|
58603
59012
|
if (!messageClass)
|
|
58604
59013
|
return "no host message class seen";
|
|
@@ -58618,7 +59027,7 @@ function decodeThroughHostSchema(source) {
|
|
|
58618
59027
|
const run = decode?.run;
|
|
58619
59028
|
if (typeof run !== "function")
|
|
58620
59029
|
return "host media schema has no decode transformation";
|
|
58621
|
-
const result = run.call(decode, { _tag: "Some", value:
|
|
59030
|
+
const result = run.call(decode, { _tag: "Some", value: encoded }, {});
|
|
58622
59031
|
const option = result?._tag === "Success" ? result.value : undefined;
|
|
58623
59032
|
const asset = option?._tag === "Some" ? option.value : undefined;
|
|
58624
59033
|
if (!isClassInstance(asset) || asset.source !== source)
|
|
@@ -58626,18 +59035,30 @@ function decodeThroughHostSchema(source) {
|
|
|
58626
59035
|
return asset;
|
|
58627
59036
|
}
|
|
58628
59037
|
function hostMediaAsset(data, mediaType) {
|
|
58629
|
-
|
|
59038
|
+
return hostMediaFromEncoded({ source: { type: "base64", data, mediaType } });
|
|
59039
|
+
}
|
|
59040
|
+
function hostMediaFromEncoded(value) {
|
|
59041
|
+
if (!isRecord6(value.source))
|
|
59042
|
+
return "missing Media.Asset source";
|
|
59043
|
+
const encoded = structuredClone(value);
|
|
59044
|
+
if (encoded.source.type === "bytes") {
|
|
59045
|
+
if (typeof encoded.source.data !== "string")
|
|
59046
|
+
return "bytes media has no JSON codec payload";
|
|
59047
|
+
encoded.source.data = new Uint8Array(Buffer.from(encoded.source.data, "base64"));
|
|
59048
|
+
}
|
|
59049
|
+
if (!["bytes", "base64", "url", "ref"].includes(String(encoded.source.type)))
|
|
59050
|
+
return "unknown Media.Asset source";
|
|
58630
59051
|
let first = "no Media.Asset seen in this process";
|
|
58631
59052
|
if (rememberedAssetClass) {
|
|
58632
59053
|
try {
|
|
58633
|
-
return new rememberedAssetClass(
|
|
59054
|
+
return new rememberedAssetClass(encoded);
|
|
58634
59055
|
} catch (error) {
|
|
58635
59056
|
first = `remembered asset class threw: ${String(error)}`;
|
|
58636
59057
|
}
|
|
58637
59058
|
}
|
|
58638
59059
|
let second;
|
|
58639
59060
|
try {
|
|
58640
|
-
const decoded = decodeThroughHostSchema(
|
|
59061
|
+
const decoded = decodeThroughHostSchema(encoded);
|
|
58641
59062
|
if (typeof decoded !== "string")
|
|
58642
59063
|
return decoded;
|
|
58643
59064
|
second = decoded;
|
|
@@ -58663,7 +59084,7 @@ var init_markers = __esm(() => {
|
|
|
58663
59084
|
});
|
|
58664
59085
|
|
|
58665
59086
|
// src/v2/fold/owner.ts
|
|
58666
|
-
import { createHash as
|
|
59087
|
+
import { createHash as createHash20 } from "node:crypto";
|
|
58667
59088
|
|
|
58668
59089
|
class FoldOwner {
|
|
58669
59090
|
storage;
|
|
@@ -58742,9 +59163,638 @@ class FoldOwner {
|
|
|
58742
59163
|
});
|
|
58743
59164
|
}
|
|
58744
59165
|
}
|
|
58745
|
-
var foldDigest = (value) =>
|
|
59166
|
+
var foldDigest = (value) => createHash20("sha256").update(value).digest("hex");
|
|
58746
59167
|
var init_owner = () => {};
|
|
58747
59168
|
|
|
59169
|
+
// src/v2/fold/memory-cache.ts
|
|
59170
|
+
import { randomUUID as randomUUID4 } from "node:crypto";
|
|
59171
|
+
|
|
59172
|
+
class NativeFoldCache {
|
|
59173
|
+
resolveDB;
|
|
59174
|
+
sourceID = randomUUID4();
|
|
59175
|
+
tails = new Map;
|
|
59176
|
+
nativeRows = new Map;
|
|
59177
|
+
admissions = new Map;
|
|
59178
|
+
retention = new Map;
|
|
59179
|
+
evictionListeners = new Set;
|
|
59180
|
+
budget;
|
|
59181
|
+
constructor(resolveDB, budget = DEFAULT_NATIVE_FOLD_BUDGET) {
|
|
59182
|
+
this.resolveDB = resolveDB;
|
|
59183
|
+
if (!Number.isSafeInteger(budget.maxSessionBytes) || budget.maxSessionBytes < 1 || !Number.isSafeInteger(budget.maxSessions) || budget.maxSessions < 1)
|
|
59184
|
+
throw new Error("Native fold cache budgets must be positive integers");
|
|
59185
|
+
this.budget = { ...budget };
|
|
59186
|
+
}
|
|
59187
|
+
onEvict(listener) {
|
|
59188
|
+
this.evictionListeners.add(listener);
|
|
59189
|
+
}
|
|
59190
|
+
touch(sessionID) {
|
|
59191
|
+
const state = this.retention.get(sessionID) ?? { active: 0, bytes: 0, replayBytes: 0 };
|
|
59192
|
+
this.retention.delete(sessionID);
|
|
59193
|
+
this.retention.set(sessionID, state);
|
|
59194
|
+
return state;
|
|
59195
|
+
}
|
|
59196
|
+
hold(sessionID) {
|
|
59197
|
+
const state = this.touch(sessionID);
|
|
59198
|
+
state.active++;
|
|
59199
|
+
return () => {
|
|
59200
|
+
state.active--;
|
|
59201
|
+
this.enforceBudget();
|
|
59202
|
+
};
|
|
59203
|
+
}
|
|
59204
|
+
accountReplay(sessionID, bytes) {
|
|
59205
|
+
this.touch(sessionID).replayBytes = bytes;
|
|
59206
|
+
this.recount(sessionID);
|
|
59207
|
+
}
|
|
59208
|
+
usage() {
|
|
59209
|
+
return {
|
|
59210
|
+
sessions: this.retention.size,
|
|
59211
|
+
bytes: [...this.retention.values()].reduce((total, state) => total + state.bytes, 0)
|
|
59212
|
+
};
|
|
59213
|
+
}
|
|
59214
|
+
recount(sessionID) {
|
|
59215
|
+
const state = this.touch(sessionID);
|
|
59216
|
+
let bytes = 256 + stringBytes(sessionID) + state.replayBytes;
|
|
59217
|
+
const tail = this.tails.get(sessionID);
|
|
59218
|
+
if (tail)
|
|
59219
|
+
bytes += 128 + stringBytes(tail.data) + stringBytes(tail.digest);
|
|
59220
|
+
for (const row of this.nativeRows.get(sessionID)?.values() ?? [])
|
|
59221
|
+
bytes += 256 + stringBytes(row.id) + stringBytes(row.type) + stringBytes(row.data) + stringBytes(row.digest);
|
|
59222
|
+
for (const row of this.admissions.get(sessionID)?.values() ?? [])
|
|
59223
|
+
bytes += 256 + stringBytes(row.admission_id) + stringBytes(row.status) + stringBytes(row.owner_token) + stringBytes(row.data) + stringBytes(row.payload) + stringBytes(row.digest);
|
|
59224
|
+
state.bytes = bytes;
|
|
59225
|
+
this.enforceBudget();
|
|
59226
|
+
}
|
|
59227
|
+
enforceBudget() {
|
|
59228
|
+
for (const [id, state] of this.retention)
|
|
59229
|
+
if (!state.active && state.bytes > this.budget.maxSessionBytes)
|
|
59230
|
+
this.evict(id);
|
|
59231
|
+
for (const [id, state] of this.retention) {
|
|
59232
|
+
if (this.retention.size <= this.budget.maxSessions)
|
|
59233
|
+
break;
|
|
59234
|
+
if (!state.active)
|
|
59235
|
+
this.evict(id);
|
|
59236
|
+
}
|
|
59237
|
+
}
|
|
59238
|
+
evict(sessionID) {
|
|
59239
|
+
this.tails.delete(sessionID);
|
|
59240
|
+
this.nativeRows.delete(sessionID);
|
|
59241
|
+
this.admissions.delete(sessionID);
|
|
59242
|
+
this.retention.delete(sessionID);
|
|
59243
|
+
for (const listener of this.evictionListeners)
|
|
59244
|
+
listener(sessionID);
|
|
59245
|
+
}
|
|
59246
|
+
get db() {
|
|
59247
|
+
return this.resolveDB();
|
|
59248
|
+
}
|
|
59249
|
+
tail(sessionID) {
|
|
59250
|
+
const record = this.tails.get(sessionID);
|
|
59251
|
+
if (record)
|
|
59252
|
+
this.touch(sessionID);
|
|
59253
|
+
return record && { ...record };
|
|
59254
|
+
}
|
|
59255
|
+
async saveTail(sessionID, data, rows) {
|
|
59256
|
+
this.tails.set(sessionID, {
|
|
59257
|
+
revision: (this.tails.get(sessionID)?.revision ?? -1) + 1,
|
|
59258
|
+
data,
|
|
59259
|
+
digest: foldDigest(data)
|
|
59260
|
+
});
|
|
59261
|
+
const kept = this.nativeRows.get(sessionID) ?? new Map;
|
|
59262
|
+
for (const row of rows)
|
|
59263
|
+
kept.set(row.id, Object.freeze({ ...row, revision: (kept.get(row.id)?.revision ?? -1) + 1 }));
|
|
59264
|
+
this.nativeRows.set(sessionID, kept);
|
|
59265
|
+
this.recount(sessionID);
|
|
59266
|
+
}
|
|
59267
|
+
replaceRows(sessionID, through, rows, after = -1) {
|
|
59268
|
+
const kept = this.nativeRows.get(sessionID) ?? new Map;
|
|
59269
|
+
for (const [id, row] of kept)
|
|
59270
|
+
if (row.seq > after && row.seq <= through)
|
|
59271
|
+
kept.delete(id);
|
|
59272
|
+
for (const row of rows)
|
|
59273
|
+
kept.set(row.id, Object.freeze({ ...row, revision: 0 }));
|
|
59274
|
+
this.nativeRows.set(sessionID, kept);
|
|
59275
|
+
this.recount(sessionID);
|
|
59276
|
+
}
|
|
59277
|
+
truncate(sessionID, through) {
|
|
59278
|
+
const kept = this.nativeRows.get(sessionID);
|
|
59279
|
+
if (kept) {
|
|
59280
|
+
for (const [id, row] of kept)
|
|
59281
|
+
if (row.seq > through)
|
|
59282
|
+
kept.delete(id);
|
|
59283
|
+
}
|
|
59284
|
+
if (kept)
|
|
59285
|
+
this.recount(sessionID);
|
|
59286
|
+
}
|
|
59287
|
+
sequenceForID(sessionID, id) {
|
|
59288
|
+
return this.nativeRows.get(sessionID)?.get(id)?.seq;
|
|
59289
|
+
}
|
|
59290
|
+
containsRows(sessionID, ids) {
|
|
59291
|
+
const kept = this.nativeRows.get(sessionID);
|
|
59292
|
+
return ids.every((id) => kept?.has(id));
|
|
59293
|
+
}
|
|
59294
|
+
rows(sessionID, after, through) {
|
|
59295
|
+
if (this.retention.has(sessionID))
|
|
59296
|
+
this.touch(sessionID);
|
|
59297
|
+
return [...this.nativeRows.get(sessionID)?.values() ?? []].filter((row) => row.seq > after && row.seq <= through).sort((a, b) => a.seq - b.seq).map((row) => ({ ...row }));
|
|
59298
|
+
}
|
|
59299
|
+
admission(sessionID, id) {
|
|
59300
|
+
const record = this.admissions.get(sessionID)?.get(id);
|
|
59301
|
+
if (record)
|
|
59302
|
+
this.touch(sessionID);
|
|
59303
|
+
return record && { ...record };
|
|
59304
|
+
}
|
|
59305
|
+
put(sessionID, id, record) {
|
|
59306
|
+
const kept = this.admissions.get(sessionID) ?? new Map;
|
|
59307
|
+
kept.set(id, Object.freeze({ ...record }));
|
|
59308
|
+
this.admissions.set(sessionID, kept);
|
|
59309
|
+
this.recount(sessionID);
|
|
59310
|
+
}
|
|
59311
|
+
async supply(sessionID, id, payload) {
|
|
59312
|
+
const old = this.admission(sessionID, id);
|
|
59313
|
+
this.put(sessionID, id, {
|
|
59314
|
+
admission_id: id,
|
|
59315
|
+
revision: (old?.revision ?? -1) + 1,
|
|
59316
|
+
status: old?.status ?? "supplied",
|
|
59317
|
+
owner_token: old?.owner_token ?? null,
|
|
59318
|
+
data: old?.data ?? "{}",
|
|
59319
|
+
payload,
|
|
59320
|
+
digest: foldDigest(payload),
|
|
59321
|
+
logged_at: old?.logged_at ?? null
|
|
59322
|
+
});
|
|
59323
|
+
}
|
|
59324
|
+
latestAttempt(sessionID) {
|
|
59325
|
+
const record = [...this.admissions.get(sessionID)?.values() ?? []].reverse().find((row) => JSON.parse(row.data).after !== undefined);
|
|
59326
|
+
return record && { ...record };
|
|
59327
|
+
}
|
|
59328
|
+
async claim(sessionID, id, data, owner, _now) {
|
|
59329
|
+
const old = this.admission(sessionID, id);
|
|
59330
|
+
if (old?.owner_token || old?.logged_at !== null && old?.logged_at !== undefined)
|
|
59331
|
+
return false;
|
|
59332
|
+
this.put(sessionID, id, {
|
|
59333
|
+
admission_id: id,
|
|
59334
|
+
revision: 0,
|
|
59335
|
+
status: "pending",
|
|
59336
|
+
owner_token: owner,
|
|
59337
|
+
data,
|
|
59338
|
+
payload: old?.payload ?? null,
|
|
59339
|
+
digest: old?.digest ?? null,
|
|
59340
|
+
logged_at: null
|
|
59341
|
+
});
|
|
59342
|
+
return true;
|
|
59343
|
+
}
|
|
59344
|
+
async finish(sessionID, id, owner, status, now) {
|
|
59345
|
+
const old = this.admission(sessionID, id);
|
|
59346
|
+
if (!old || old.owner_token !== owner || old.status !== "pending")
|
|
59347
|
+
return false;
|
|
59348
|
+
this.put(sessionID, id, { ...old, status, owner_token: null, logged_at: now });
|
|
59349
|
+
return true;
|
|
59350
|
+
}
|
|
59351
|
+
async forget(sessionID) {
|
|
59352
|
+
this.evict(sessionID);
|
|
59353
|
+
}
|
|
59354
|
+
}
|
|
59355
|
+
function nativeFoldCache(db, budget) {
|
|
59356
|
+
return new NativeFoldCache(typeof db === "function" ? db : () => db, budget);
|
|
59357
|
+
}
|
|
59358
|
+
var DEFAULT_NATIVE_FOLD_BUDGET, stringBytes = (value) => (value?.length ?? 0) * 2;
|
|
59359
|
+
var init_memory_cache = __esm(() => {
|
|
59360
|
+
init_owner();
|
|
59361
|
+
DEFAULT_NATIVE_FOLD_BUDGET = Object.freeze({
|
|
59362
|
+
maxSessionBytes: 16 * 1024 * 1024,
|
|
59363
|
+
maxSessions: 16
|
|
59364
|
+
});
|
|
59365
|
+
});
|
|
59366
|
+
|
|
59367
|
+
// src/v2/tool-result.ts
|
|
59368
|
+
function isToolError(part) {
|
|
59369
|
+
const result = part.result;
|
|
59370
|
+
return part.resultType === "error" || result?.type === "error" || result !== null && typeof result === "object" && "error" in result;
|
|
59371
|
+
}
|
|
59372
|
+
function toolErrorText(result) {
|
|
59373
|
+
const value = result.value === undefined ? result : result.value;
|
|
59374
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
59375
|
+
}
|
|
59376
|
+
function toolStateContent(state) {
|
|
59377
|
+
if (typeof state.output === "string")
|
|
59378
|
+
return state.output;
|
|
59379
|
+
if (state.status === "error")
|
|
59380
|
+
return toolErrorText({ error: state.error, content: state.content ?? [] });
|
|
59381
|
+
if (typeof state.content === "string")
|
|
59382
|
+
return state.content;
|
|
59383
|
+
if (!Array.isArray(state.content))
|
|
59384
|
+
return "";
|
|
59385
|
+
return state.content.map((part) => {
|
|
59386
|
+
if (typeof part === "string")
|
|
59387
|
+
return part;
|
|
59388
|
+
if (!part || typeof part !== "object")
|
|
59389
|
+
return "";
|
|
59390
|
+
const value = part;
|
|
59391
|
+
if (typeof value.text === "string")
|
|
59392
|
+
return value.text;
|
|
59393
|
+
if (typeof value.value === "string")
|
|
59394
|
+
return value.value;
|
|
59395
|
+
return "";
|
|
59396
|
+
}).filter(Boolean).join(`
|
|
59397
|
+
`);
|
|
59398
|
+
}
|
|
59399
|
+
|
|
59400
|
+
// src/v2/hooks/payload.ts
|
|
59401
|
+
function rejectContentPart(message, part, reason) {
|
|
59402
|
+
const type = typeof part.type === "string" ? part.type : "<missing>";
|
|
59403
|
+
const text = `v2 context omitted a content part the host would reject: type=${type} message=${String(message.info.id ?? "<head>")} reason=${reason}`;
|
|
59404
|
+
if (typeof message.info.sessionID === "string")
|
|
59405
|
+
sessionLog(message.info.sessionID, text);
|
|
59406
|
+
else
|
|
59407
|
+
log(text);
|
|
59408
|
+
}
|
|
59409
|
+
function isHostInstance(value) {
|
|
59410
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
59411
|
+
return false;
|
|
59412
|
+
const prototype = Object.getPrototypeOf(value);
|
|
59413
|
+
return prototype !== Object.prototype && prototype !== null;
|
|
59414
|
+
}
|
|
59415
|
+
function containsHostInstance(value) {
|
|
59416
|
+
if (isHostInstance(value))
|
|
59417
|
+
return true;
|
|
59418
|
+
if (Array.isArray(value))
|
|
59419
|
+
return value.some(containsHostInstance);
|
|
59420
|
+
if (value && typeof value === "object")
|
|
59421
|
+
return Object.values(value).some(containsHostInstance);
|
|
59422
|
+
return false;
|
|
59423
|
+
}
|
|
59424
|
+
function clonePreservingInstances(value) {
|
|
59425
|
+
if (isHostInstance(value))
|
|
59426
|
+
return value;
|
|
59427
|
+
if (Array.isArray(value))
|
|
59428
|
+
return value.map(clonePreservingInstances);
|
|
59429
|
+
if (value && typeof value === "object") {
|
|
59430
|
+
const copy = {};
|
|
59431
|
+
for (const [key, entry] of Object.entries(value))
|
|
59432
|
+
copy[key] = clonePreservingInstances(entry);
|
|
59433
|
+
return copy;
|
|
59434
|
+
}
|
|
59435
|
+
return value;
|
|
59436
|
+
}
|
|
59437
|
+
function contentKey(value) {
|
|
59438
|
+
const plain = (entry) => {
|
|
59439
|
+
if (ArrayBuffer.isView(entry)) {
|
|
59440
|
+
return {
|
|
59441
|
+
bytes: Buffer.from(entry.buffer, entry.byteOffset, entry.byteLength).toString("base64")
|
|
59442
|
+
};
|
|
59443
|
+
}
|
|
59444
|
+
if (Array.isArray(entry))
|
|
59445
|
+
return entry.map(plain);
|
|
59446
|
+
if (entry && typeof entry === "object") {
|
|
59447
|
+
return Object.fromEntries(Object.entries(entry).map(([key, nested]) => [key, plain(nested)]));
|
|
59448
|
+
}
|
|
59449
|
+
return entry;
|
|
59450
|
+
};
|
|
59451
|
+
return JSON.stringify(plain(value));
|
|
59452
|
+
}
|
|
59453
|
+
function fileContentValue(result) {
|
|
59454
|
+
const value = result?.value;
|
|
59455
|
+
if (result?.type !== "content" || !Array.isArray(value))
|
|
59456
|
+
return;
|
|
59457
|
+
return value.some((item) => item?.type === "file") ? value : undefined;
|
|
59458
|
+
}
|
|
59459
|
+
function contentValueText(value) {
|
|
59460
|
+
return value.filter((item) => item?.type === "text" && typeof item.text === "string").map((item) => item.text).join(`
|
|
59461
|
+
`);
|
|
59462
|
+
}
|
|
59463
|
+
function contentValueAttachments(value) {
|
|
59464
|
+
return value.filter((item) => item?.type === "file").map((item) => ({
|
|
59465
|
+
type: "file",
|
|
59466
|
+
mime: item.mime,
|
|
59467
|
+
url: item.uri,
|
|
59468
|
+
...typeof item.name === "string" ? { filename: item.name } : {}
|
|
59469
|
+
}));
|
|
59470
|
+
}
|
|
59471
|
+
function rebuildFileContent(value, output) {
|
|
59472
|
+
if (isDroppedToolOutput(output))
|
|
59473
|
+
return { type: "text", value: output };
|
|
59474
|
+
const rebuilt = [];
|
|
59475
|
+
let placed = false;
|
|
59476
|
+
for (const item of value) {
|
|
59477
|
+
if (item?.type !== "text") {
|
|
59478
|
+
rebuilt.push(item);
|
|
59479
|
+
continue;
|
|
59480
|
+
}
|
|
59481
|
+
if (placed)
|
|
59482
|
+
continue;
|
|
59483
|
+
placed = true;
|
|
59484
|
+
rebuilt.push({ ...item, text: output });
|
|
59485
|
+
}
|
|
59486
|
+
if (!placed && output)
|
|
59487
|
+
rebuilt.unshift({ type: "text", text: output });
|
|
59488
|
+
return { type: "content", value: rebuilt };
|
|
59489
|
+
}
|
|
59490
|
+
function adaptPayload(draft, admittedIDs = new Set) {
|
|
59491
|
+
const existingHeads = new Map(draft.messages.filter((m) => HEAD_IDS.some((id) => m.id === id)).map((m) => [m.id, m]));
|
|
59492
|
+
const source = draft.messages.filter((m) => !existingHeads.has(m.id));
|
|
59493
|
+
const results = new Map;
|
|
59494
|
+
const hostParts = new Map;
|
|
59495
|
+
const hostPartTypes = new Set;
|
|
59496
|
+
for (const message of source) {
|
|
59497
|
+
for (const part of message.content) {
|
|
59498
|
+
if (part.type !== "tool-call" && part.type !== "tool-result" && part.type !== "tool" && containsHostInstance(part)) {
|
|
59499
|
+
const key = contentKey(part);
|
|
59500
|
+
if (!hostParts.has(key))
|
|
59501
|
+
hostParts.set(key, part);
|
|
59502
|
+
hostPartTypes.add(part.type);
|
|
59503
|
+
}
|
|
59504
|
+
if (part.type === "tool-result" && typeof part.id === "string") {
|
|
59505
|
+
const queue = results.get(part.id) ?? [];
|
|
59506
|
+
queue.push({ part, message });
|
|
59507
|
+
results.set(part.id, queue);
|
|
59508
|
+
}
|
|
59509
|
+
}
|
|
59510
|
+
}
|
|
59511
|
+
const paired = new Set;
|
|
59512
|
+
const bridges = new Map;
|
|
59513
|
+
const originals = new Map;
|
|
59514
|
+
const bridgesByMessageID = new Map;
|
|
59515
|
+
const bridgesByCarrier = new Map;
|
|
59516
|
+
const originalsByID = new Map;
|
|
59517
|
+
const bridgesFor = (message) => typeof message.info.id === "string" ? bridgesByMessageID.get(message.info.id) : bridgesByCarrier.get(message);
|
|
59518
|
+
const nativeTool = (call, result) => {
|
|
59519
|
+
const hostResult = result?.part.result;
|
|
59520
|
+
const failed = result !== undefined && isToolError(result.part);
|
|
59521
|
+
const value = hostResult?.value;
|
|
59522
|
+
const files = fileContentValue(hostResult);
|
|
59523
|
+
const output = failed && hostResult ? toolErrorText(hostResult) : files ? contentValueText(files) : typeof value === "string" ? value : value === undefined ? "" : JSON.stringify(value);
|
|
59524
|
+
const part = {
|
|
59525
|
+
type: "tool",
|
|
59526
|
+
callID: call?.id ?? result?.part.id,
|
|
59527
|
+
tool: call?.name ?? result?.part.name,
|
|
59528
|
+
state: {
|
|
59529
|
+
input: structuredClone(call?.input ?? {}),
|
|
59530
|
+
status: failed ? "error" : result ? "completed" : "running",
|
|
59531
|
+
...result ? { output } : {},
|
|
59532
|
+
...result && (call?.name ?? result.part.name) === "question" ? { metadata: { userAnswer: true } } : {},
|
|
59533
|
+
...files ? { attachments: contentValueAttachments(files) } : {}
|
|
59534
|
+
}
|
|
59535
|
+
};
|
|
59536
|
+
bridges.set(part, {
|
|
59537
|
+
call,
|
|
59538
|
+
result: result?.part,
|
|
59539
|
+
resultMessage: result?.message,
|
|
59540
|
+
output,
|
|
59541
|
+
files
|
|
59542
|
+
});
|
|
59543
|
+
if (result)
|
|
59544
|
+
paired.add(result.part);
|
|
59545
|
+
return part;
|
|
59546
|
+
};
|
|
59547
|
+
const convertedTool = (source) => {
|
|
59548
|
+
const sourceState = source.state && typeof source.state === "object" ? source.state : {};
|
|
59549
|
+
const output = toolStateContent(sourceState);
|
|
59550
|
+
const { content: _content, ...projectedState } = structuredClone(sourceState);
|
|
59551
|
+
const part = {
|
|
59552
|
+
type: "tool",
|
|
59553
|
+
callID: source.callID ?? source.id,
|
|
59554
|
+
tool: source.tool ?? source.name,
|
|
59555
|
+
state: { ...projectedState, output }
|
|
59556
|
+
};
|
|
59557
|
+
bridges.set(part, { native: source, output });
|
|
59558
|
+
return part;
|
|
59559
|
+
};
|
|
59560
|
+
const callParts = new Map;
|
|
59561
|
+
for (const message of source) {
|
|
59562
|
+
for (const part of message.content) {
|
|
59563
|
+
if (part.type === "tool-call") {
|
|
59564
|
+
callParts.set(part, nativeTool(part, results.get(String(part.id))?.shift()));
|
|
59565
|
+
} else if (part.type === "tool") {
|
|
59566
|
+
callParts.set(part, convertedTool(part));
|
|
59567
|
+
}
|
|
59568
|
+
}
|
|
59569
|
+
}
|
|
59570
|
+
const messages = [];
|
|
59571
|
+
for (const message of source) {
|
|
59572
|
+
const parts = [];
|
|
59573
|
+
for (const content of message.content) {
|
|
59574
|
+
if (paired.has(content))
|
|
59575
|
+
continue;
|
|
59576
|
+
const part = callParts.get(content) ?? (content.type === "tool-result" ? nativeTool(undefined, { part: content, message }) : clonePreservingInstances(content));
|
|
59577
|
+
if (message.id && admittedIDs.has(message.id))
|
|
59578
|
+
part.synthetic = true;
|
|
59579
|
+
parts.push(part);
|
|
59580
|
+
}
|
|
59581
|
+
if (!parts.length && message.content.length)
|
|
59582
|
+
continue;
|
|
59583
|
+
const byCallID = new Map;
|
|
59584
|
+
for (const part of parts) {
|
|
59585
|
+
const bridge = bridges.get(part);
|
|
59586
|
+
if (bridge && typeof part.callID === "string")
|
|
59587
|
+
byCallID.set(part.callID, bridge);
|
|
59588
|
+
}
|
|
59589
|
+
if (typeof message.id === "string") {
|
|
59590
|
+
bridgesByMessageID.set(message.id, byCallID);
|
|
59591
|
+
originalsByID.set(message.id, message);
|
|
59592
|
+
}
|
|
59593
|
+
const mapped = {
|
|
59594
|
+
info: {
|
|
59595
|
+
id: message.id,
|
|
59596
|
+
sessionID: draft.sessionID,
|
|
59597
|
+
role: message.role,
|
|
59598
|
+
...{
|
|
59599
|
+
providerID: draft.model.providerID,
|
|
59600
|
+
modelID: draft.model.id,
|
|
59601
|
+
agent: draft.agent
|
|
59602
|
+
}
|
|
59603
|
+
},
|
|
59604
|
+
parts
|
|
59605
|
+
};
|
|
59606
|
+
originals.set(mapped, message);
|
|
59607
|
+
if (typeof message.id !== "string")
|
|
59608
|
+
bridgesByCarrier.set(mapped, byCallID);
|
|
59609
|
+
messages.push(mapped);
|
|
59610
|
+
}
|
|
59611
|
+
return {
|
|
59612
|
+
messages,
|
|
59613
|
+
commit() {
|
|
59614
|
+
let head = 0;
|
|
59615
|
+
const rendered = [];
|
|
59616
|
+
for (const message of messages) {
|
|
59617
|
+
const original = originals.get(message) ?? (typeof message.info.id === "string" ? originalsByID.get(message.info.id) : undefined);
|
|
59618
|
+
const id = message.info.syntheticHead ? HEAD_IDS[head++] : original?.id;
|
|
59619
|
+
const content = [];
|
|
59620
|
+
const following = [];
|
|
59621
|
+
for (const candidate of message.parts) {
|
|
59622
|
+
const part = candidate;
|
|
59623
|
+
const bridge = bridges.get(part) ?? (part.type === "tool" && typeof part.callID === "string" ? bridgesFor(message)?.get(part.callID) : undefined);
|
|
59624
|
+
if (!bridge) {
|
|
59625
|
+
if (part.type === "tool") {
|
|
59626
|
+
const state = part.state;
|
|
59627
|
+
if (typeof part.callID !== "string" || typeof part.tool !== "string" || !state || typeof state !== "object") {
|
|
59628
|
+
rejectContentPart(message, part, "tool has no call ID, name, or state");
|
|
59629
|
+
continue;
|
|
59630
|
+
}
|
|
59631
|
+
content.push({
|
|
59632
|
+
type: "tool-call",
|
|
59633
|
+
id: part.callID,
|
|
59634
|
+
name: part.tool,
|
|
59635
|
+
input: state.input
|
|
59636
|
+
});
|
|
59637
|
+
if (state.status === "completed" || state.status === "error") {
|
|
59638
|
+
following.push({
|
|
59639
|
+
role: "tool",
|
|
59640
|
+
content: [
|
|
59641
|
+
{
|
|
59642
|
+
type: "tool-result",
|
|
59643
|
+
id: part.callID,
|
|
59644
|
+
name: part.tool,
|
|
59645
|
+
result: {
|
|
59646
|
+
type: state.status === "error" ? "error" : "text",
|
|
59647
|
+
value: toolStateContent(state)
|
|
59648
|
+
}
|
|
59649
|
+
}
|
|
59650
|
+
]
|
|
59651
|
+
});
|
|
59652
|
+
}
|
|
59653
|
+
continue;
|
|
59654
|
+
}
|
|
59655
|
+
if (part.type === "file") {
|
|
59656
|
+
const mime = part.mime;
|
|
59657
|
+
const url = part.url;
|
|
59658
|
+
const prefix = `data:${mime};base64,`;
|
|
59659
|
+
if (typeof mime !== "string" || !/^image\/(png|jpeg|gif|webp)$/.test(mime) || typeof url !== "string" || !url.startsWith(prefix)) {
|
|
59660
|
+
rejectContentPart(message, part, "file is not an inline supported image");
|
|
59661
|
+
continue;
|
|
59662
|
+
}
|
|
59663
|
+
const asset = hostMediaAsset(url.slice(prefix.length), mime);
|
|
59664
|
+
if (typeof asset === "string") {
|
|
59665
|
+
rejectContentPart(message, part, `host media unavailable: ${asset}`);
|
|
59666
|
+
continue;
|
|
59667
|
+
}
|
|
59668
|
+
content.push({ type: "media", media: asset });
|
|
59669
|
+
continue;
|
|
59670
|
+
}
|
|
59671
|
+
if (!HOST_CONTENT_TYPES.has(String(part.type))) {
|
|
59672
|
+
rejectContentPart(message, part, "unknown host content type");
|
|
59673
|
+
continue;
|
|
59674
|
+
}
|
|
59675
|
+
const { synthetic: _synthetic, ...clean } = part;
|
|
59676
|
+
const original = hostPartTypes.has(clean.type) && !containsHostInstance(clean) ? hostParts.get(contentKey(clean)) : undefined;
|
|
59677
|
+
content.push(original ?? clean);
|
|
59678
|
+
continue;
|
|
59679
|
+
}
|
|
59680
|
+
const state = part.state;
|
|
59681
|
+
if (bridge.native) {
|
|
59682
|
+
const native = bridge.native;
|
|
59683
|
+
const callID = String(native.id ?? native.callID ?? part.callID);
|
|
59684
|
+
const name = String(native.name ?? native.tool ?? part.tool);
|
|
59685
|
+
content.push({ type: "tool-call", id: callID, name, input: state.input });
|
|
59686
|
+
if (state.status === "completed" || state.status === "error") {
|
|
59687
|
+
following.push({
|
|
59688
|
+
role: "tool",
|
|
59689
|
+
content: [
|
|
59690
|
+
{
|
|
59691
|
+
type: "tool-result",
|
|
59692
|
+
id: callID,
|
|
59693
|
+
name,
|
|
59694
|
+
result: {
|
|
59695
|
+
type: state.status === "error" ? "error" : "text",
|
|
59696
|
+
value: state.output
|
|
59697
|
+
}
|
|
59698
|
+
}
|
|
59699
|
+
]
|
|
59700
|
+
});
|
|
59701
|
+
}
|
|
59702
|
+
continue;
|
|
59703
|
+
}
|
|
59704
|
+
if (bridge.call)
|
|
59705
|
+
content.push({ ...bridge.call, input: state.input });
|
|
59706
|
+
if (bridge.result && bridge.resultMessage) {
|
|
59707
|
+
const result = {
|
|
59708
|
+
...bridge.result,
|
|
59709
|
+
result: state.output === bridge.output ? bridge.result.result : bridge.files && typeof state.output === "string" ? rebuildFileContent(bridge.files, state.output) : {
|
|
59710
|
+
type: state.status === "error" ? "error" : "text",
|
|
59711
|
+
value: state.output
|
|
59712
|
+
}
|
|
59713
|
+
};
|
|
59714
|
+
if (bridge.resultMessage === original)
|
|
59715
|
+
content.push(result);
|
|
59716
|
+
else
|
|
59717
|
+
following.push({ ...bridge.resultMessage, content: [result] });
|
|
59718
|
+
}
|
|
59719
|
+
}
|
|
59720
|
+
if (content.length || !original?.content.length) {
|
|
59721
|
+
const value = {
|
|
59722
|
+
...original,
|
|
59723
|
+
id,
|
|
59724
|
+
role: message.info.role ?? "user",
|
|
59725
|
+
content
|
|
59726
|
+
};
|
|
59727
|
+
const existing = id ? existingHeads.get(id) : undefined;
|
|
59728
|
+
rendered.push(existing ? Object.assign(existing, { content }) : value);
|
|
59729
|
+
}
|
|
59730
|
+
rendered.push(...following);
|
|
59731
|
+
}
|
|
59732
|
+
draft.messages.splice(0, draft.messages.length, ...rendered);
|
|
59733
|
+
}
|
|
59734
|
+
};
|
|
59735
|
+
}
|
|
59736
|
+
var HEAD_IDS, HOST_CONTENT_TYPES;
|
|
59737
|
+
var init_payload = __esm(() => {
|
|
59738
|
+
init_ctx_reduce_nudge();
|
|
59739
|
+
init_logger();
|
|
59740
|
+
init_host_media();
|
|
59741
|
+
HEAD_IDS = ["__magic_context_v2_m0__", "__magic_context_v2_m1__"];
|
|
59742
|
+
HOST_CONTENT_TYPES = new Set([
|
|
59743
|
+
"text",
|
|
59744
|
+
"media",
|
|
59745
|
+
"tool-call",
|
|
59746
|
+
"tool-result",
|
|
59747
|
+
"reasoning",
|
|
59748
|
+
"compaction",
|
|
59749
|
+
"effort"
|
|
59750
|
+
]);
|
|
59751
|
+
});
|
|
59752
|
+
|
|
59753
|
+
// src/v2/fold/native-codec.ts
|
|
59754
|
+
function encodeNative(messages) {
|
|
59755
|
+
for (const message of messages)
|
|
59756
|
+
for (const part of message.content) {
|
|
59757
|
+
if (part.type !== "media" || !part.media)
|
|
59758
|
+
continue;
|
|
59759
|
+
const asset = part.media;
|
|
59760
|
+
if (typeof asset.toJSON !== "function" || asset.headers !== undefined)
|
|
59761
|
+
throw new NativeReplayUnavailable("Native media has no lossless replay codec");
|
|
59762
|
+
const encoded = asset.toJSON();
|
|
59763
|
+
const revived = hostMediaFromEncoded(encoded);
|
|
59764
|
+
if (typeof revived === "string" || JSON.stringify(revived) !== JSON.stringify(asset))
|
|
59765
|
+
throw new NativeReplayUnavailable("Native media cannot be decoded losslessly before admission");
|
|
59766
|
+
}
|
|
59767
|
+
const encoded = JSON.parse(JSON.stringify(messages));
|
|
59768
|
+
const freeze = (value) => {
|
|
59769
|
+
if (!value || typeof value !== "object")
|
|
59770
|
+
return;
|
|
59771
|
+
for (const item of Object.values(value))
|
|
59772
|
+
freeze(item);
|
|
59773
|
+
Object.freeze(value);
|
|
59774
|
+
};
|
|
59775
|
+
freeze(encoded);
|
|
59776
|
+
return encoded;
|
|
59777
|
+
}
|
|
59778
|
+
function reviveNative(messages) {
|
|
59779
|
+
const result = structuredClone([...messages]);
|
|
59780
|
+
for (const message of result)
|
|
59781
|
+
for (const part of message.content) {
|
|
59782
|
+
if (part.type !== "media" || !part.media)
|
|
59783
|
+
continue;
|
|
59784
|
+
const asset = hostMediaFromEncoded(part.media);
|
|
59785
|
+
if (typeof asset === "string")
|
|
59786
|
+
throw new NativeReplayUnavailable(asset);
|
|
59787
|
+
part.media = asset;
|
|
59788
|
+
}
|
|
59789
|
+
return result;
|
|
59790
|
+
}
|
|
59791
|
+
var NativeReplayUnavailable;
|
|
59792
|
+
var init_native_codec = __esm(() => {
|
|
59793
|
+
init_host_media();
|
|
59794
|
+
NativeReplayUnavailable = class NativeReplayUnavailable extends Error {
|
|
59795
|
+
};
|
|
59796
|
+
});
|
|
59797
|
+
|
|
58748
59798
|
// src/v2/hooks/storage-notice.ts
|
|
58749
59799
|
function formatStorageRefusalNotice(reason) {
|
|
58750
59800
|
return `${STORAGE_NOTICE_PREFIX}This turn was not sent to the model. ${formatFailClosedBlockingSummary(reason)}`;
|
|
@@ -58978,6 +60028,411 @@ var init_restore = __esm(() => {
|
|
|
58978
60028
|
imageMimes = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
|
|
58979
60029
|
});
|
|
58980
60030
|
|
|
60031
|
+
// src/v2/fold/native-replay.ts
|
|
60032
|
+
function nativeRowID(message) {
|
|
60033
|
+
return coordinates.get(message) ?? message.id;
|
|
60034
|
+
}
|
|
60035
|
+
function copyNativeInput(messages) {
|
|
60036
|
+
const copies = reviveNative(encodeNative(messages));
|
|
60037
|
+
copies.forEach((copy, i) => {
|
|
60038
|
+
const original = messages[i];
|
|
60039
|
+
const id = original && nativeRowID(original);
|
|
60040
|
+
if (id !== undefined)
|
|
60041
|
+
coordinates.set(copy, id);
|
|
60042
|
+
});
|
|
60043
|
+
return copies;
|
|
60044
|
+
}
|
|
60045
|
+
function isLocalCheckpoint(row) {
|
|
60046
|
+
return row.type === "compaction" && row.data.status === "completed" && row.data.providerContext === undefined;
|
|
60047
|
+
}
|
|
60048
|
+
|
|
60049
|
+
class NativeFoldReplay {
|
|
60050
|
+
storage;
|
|
60051
|
+
openReader;
|
|
60052
|
+
changed = new Set;
|
|
60053
|
+
dirty = new Set;
|
|
60054
|
+
unavailable = new Set;
|
|
60055
|
+
metadata = new Map;
|
|
60056
|
+
metadataThrough = new Map;
|
|
60057
|
+
loaded = new Map;
|
|
60058
|
+
sources = new Map;
|
|
60059
|
+
constructor(storage, openReader) {
|
|
60060
|
+
this.storage = storage;
|
|
60061
|
+
this.openReader = openReader;
|
|
60062
|
+
storage.onEvict((sessionID) => this.clear(sessionID));
|
|
60063
|
+
}
|
|
60064
|
+
account(sessionID) {
|
|
60065
|
+
const source = this.sources.get(sessionID);
|
|
60066
|
+
let bytes = 256 + (source?.length ?? 0) * 2;
|
|
60067
|
+
for (const row of this.metadata.get(sessionID) ?? [])
|
|
60068
|
+
bytes += 128 + (row.id.length + row.type.length) * 2;
|
|
60069
|
+
this.storage.accountReplay(sessionID, bytes);
|
|
60070
|
+
}
|
|
60071
|
+
async observeSource(sessionID, reader) {
|
|
60072
|
+
const source = reader.hostIdentity();
|
|
60073
|
+
const prior = this.sources.get(sessionID);
|
|
60074
|
+
if (prior && prior !== source)
|
|
60075
|
+
await this.forget(sessionID);
|
|
60076
|
+
this.sources.set(sessionID, source);
|
|
60077
|
+
this.account(sessionID);
|
|
60078
|
+
}
|
|
60079
|
+
onEvent(event) {
|
|
60080
|
+
const sessionID = event.data?.sessionID;
|
|
60081
|
+
if (sessionID && (event.type?.startsWith("session.revert.") || event.type === "session.message.content.updated"))
|
|
60082
|
+
this.invalidate(sessionID);
|
|
60083
|
+
}
|
|
60084
|
+
invalidate(sessionID, suppressNomination = false) {
|
|
60085
|
+
if (suppressNomination)
|
|
60086
|
+
this.unavailable.add(sessionID);
|
|
60087
|
+
this.dirty.add(sessionID);
|
|
60088
|
+
this.changed.add(sessionID);
|
|
60089
|
+
this.metadata.delete(sessionID);
|
|
60090
|
+
this.metadataThrough.delete(sessionID);
|
|
60091
|
+
this.account(sessionID);
|
|
60092
|
+
}
|
|
60093
|
+
tail(sessionID) {
|
|
60094
|
+
const record = this.storage.tail(sessionID);
|
|
60095
|
+
return record ? JSON.parse(record.data) : undefined;
|
|
60096
|
+
}
|
|
60097
|
+
async baseline(sessionID) {
|
|
60098
|
+
return this.tail(sessionID)?.baseline;
|
|
60099
|
+
}
|
|
60100
|
+
rendered(row, model) {
|
|
60101
|
+
return restoreRow(row, model, hostUsesMediaAssets() ? {
|
|
60102
|
+
asset: hostMediaAsset,
|
|
60103
|
+
unavailable: () => {}
|
|
60104
|
+
} : undefined);
|
|
60105
|
+
}
|
|
60106
|
+
record(row, messages) {
|
|
60107
|
+
const data = JSON.stringify(encodeNative(messages));
|
|
60108
|
+
return { id: row.id, type: row.type, seq: row.seq, data, digest: foldDigest(data) };
|
|
60109
|
+
}
|
|
60110
|
+
records(sessionID, native, reader) {
|
|
60111
|
+
let metadata = this.metadata.get(sessionID);
|
|
60112
|
+
const through = reader.latestSequence(sessionID);
|
|
60113
|
+
const known = new Set(metadata?.map((row) => row.id));
|
|
60114
|
+
if (!metadata || (this.metadataThrough.get(sessionID) ?? -1) < through || native.some((message) => message.id && !known.has(message.id))) {
|
|
60115
|
+
metadata = reader.rowStampsThrough(sessionID, through).filter(conversational);
|
|
60116
|
+
this.metadata.set(sessionID, metadata);
|
|
60117
|
+
this.metadataThrough.set(sessionID, through);
|
|
60118
|
+
}
|
|
60119
|
+
const byID = new Map(metadata.map((row) => [row.id, row]));
|
|
60120
|
+
const grouped = new Map;
|
|
60121
|
+
const anchorIndex = native.findIndex((message) => {
|
|
60122
|
+
const id = nativeRowID(message);
|
|
60123
|
+
return id !== undefined && byID.has(id);
|
|
60124
|
+
});
|
|
60125
|
+
const leading = native.slice(0, anchorIndex < 0 ? native.length : anchorIndex).filter((message) => !nativeRowID(message) && message.role === "system");
|
|
60126
|
+
const anchorMessage = native[anchorIndex];
|
|
60127
|
+
const anchorID = anchorMessage && nativeRowID(anchorMessage);
|
|
60128
|
+
const anchor = anchorID ? byID.get(anchorID) : undefined;
|
|
60129
|
+
const leadingRows = anchor ? metadata.filter((row) => row.type === "system" && row.seq < anchor.seq).slice(-leading.length) : [];
|
|
60130
|
+
const leadingIDs = new Map(leading.map((message, i) => [message, leadingRows[i]?.id]));
|
|
60131
|
+
let cursor = -1;
|
|
60132
|
+
let owner;
|
|
60133
|
+
for (const message of native) {
|
|
60134
|
+
let id = nativeRowID(message);
|
|
60135
|
+
if (!id && message.role === "system") {
|
|
60136
|
+
if (leadingIDs.has(message)) {
|
|
60137
|
+
id = leadingIDs.get(message);
|
|
60138
|
+
if (!id)
|
|
60139
|
+
continue;
|
|
60140
|
+
} else
|
|
60141
|
+
id = metadata.find((row) => row.type === "system" && row.seq > cursor)?.id;
|
|
60142
|
+
}
|
|
60143
|
+
if (!id && message.role === "tool")
|
|
60144
|
+
id = owner;
|
|
60145
|
+
const row = id ? byID.get(id) : undefined;
|
|
60146
|
+
if (!row)
|
|
60147
|
+
throw new NativeReplayUnavailable("Native carrier has no source row coordinate");
|
|
60148
|
+
cursor = row.seq;
|
|
60149
|
+
coordinates.set(message, row.id);
|
|
60150
|
+
if (message.role !== "tool")
|
|
60151
|
+
owner = row.id;
|
|
60152
|
+
const messages = grouped.get(row) ?? [];
|
|
60153
|
+
messages.push(message);
|
|
60154
|
+
grouped.set(row, messages);
|
|
60155
|
+
}
|
|
60156
|
+
return [...grouped].map(([row, messages]) => this.record(row, messages));
|
|
60157
|
+
}
|
|
60158
|
+
async capture(draft, native) {
|
|
60159
|
+
const release = this.storage.hold(draft.sessionID);
|
|
60160
|
+
try {
|
|
60161
|
+
rememberHostMedia(native);
|
|
60162
|
+
const reader = this.openReader(draft.sessionID);
|
|
60163
|
+
try {
|
|
60164
|
+
const rows = this.records(draft.sessionID, native, reader);
|
|
60165
|
+
const baseline = draft.messages.find((message) => message.id === HEAD_IDS[0])?.content.find((part) => part.type === "text")?.text;
|
|
60166
|
+
const through = Math.max(-1, ...rows.map((row) => row.seq));
|
|
60167
|
+
const tail = {
|
|
60168
|
+
model: `${draft.model.providerID}/${draft.model.id}`,
|
|
60169
|
+
sourceSeq: through,
|
|
60170
|
+
baseline: typeof baseline === "string" ? baseline : this.tail(draft.sessionID)?.baseline
|
|
60171
|
+
};
|
|
60172
|
+
await this.storage.saveTail(draft.sessionID, JSON.stringify(tail), rows);
|
|
60173
|
+
this.storage.truncate(draft.sessionID, through);
|
|
60174
|
+
this.loaded.set(draft.sessionID, through);
|
|
60175
|
+
this.unavailable.delete(draft.sessionID);
|
|
60176
|
+
this.changed.delete(draft.sessionID);
|
|
60177
|
+
} finally {
|
|
60178
|
+
reader.close();
|
|
60179
|
+
}
|
|
60180
|
+
} catch {
|
|
60181
|
+
this.unavailable.add(draft.sessionID);
|
|
60182
|
+
this.invalidate(draft.sessionID);
|
|
60183
|
+
} finally {
|
|
60184
|
+
this.account(draft.sessionID);
|
|
60185
|
+
release();
|
|
60186
|
+
}
|
|
60187
|
+
}
|
|
60188
|
+
async canReplay(sessionID, _reader) {
|
|
60189
|
+
return !this.unavailable.has(sessionID) && typeof await this.baseline(sessionID) === "string";
|
|
60190
|
+
}
|
|
60191
|
+
async supply(args) {
|
|
60192
|
+
const release = this.storage.hold(args.draft.sessionID);
|
|
60193
|
+
try {
|
|
60194
|
+
return await this.supplyHeld(args);
|
|
60195
|
+
} finally {
|
|
60196
|
+
this.account(args.draft.sessionID);
|
|
60197
|
+
release();
|
|
60198
|
+
}
|
|
60199
|
+
}
|
|
60200
|
+
async supplyHeld(args) {
|
|
60201
|
+
const { draft, reader, summary } = args;
|
|
60202
|
+
const tail = this.tail(draft.sessionID);
|
|
60203
|
+
const id = reader.latestRunningCompaction(draft.sessionID)?.id;
|
|
60204
|
+
if (!id || !tail || tail.model !== `${draft.model.providerID}/${draft.model.id}`)
|
|
60205
|
+
return false;
|
|
60206
|
+
const through = reader.latestSequence(draft.sessionID);
|
|
60207
|
+
const after = this.dirty.has(draft.sessionID) ? -1 : tail.sourceSeq;
|
|
60208
|
+
const fresh = reader.range(draft.sessionID, after, through).filter(conversational);
|
|
60209
|
+
const records = fresh.map((row) => this.record(row, this.rendered(row, draft.model)));
|
|
60210
|
+
if (this.dirty.has(draft.sessionID))
|
|
60211
|
+
this.storage.replaceRows(draft.sessionID, through, records);
|
|
60212
|
+
await this.storage.saveTail(draft.sessionID, JSON.stringify({
|
|
60213
|
+
...tail,
|
|
60214
|
+
sourceSeq: Math.max(tail.sourceSeq, ...fresh.map((row) => row.seq))
|
|
60215
|
+
}), records);
|
|
60216
|
+
this.dirty.delete(draft.sessionID);
|
|
60217
|
+
this.metadata.delete(draft.sessionID);
|
|
60218
|
+
this.metadataThrough.delete(draft.sessionID);
|
|
60219
|
+
this.loaded.set(draft.sessionID, through);
|
|
60220
|
+
const snapshot = {
|
|
60221
|
+
source: this.storage.sourceID,
|
|
60222
|
+
sessionID: draft.sessionID,
|
|
60223
|
+
admissionID: id,
|
|
60224
|
+
summary
|
|
60225
|
+
};
|
|
60226
|
+
await this.storage.supply(draft.sessionID, id, JSON.stringify(snapshot));
|
|
60227
|
+
return true;
|
|
60228
|
+
}
|
|
60229
|
+
async restore(sessionID, cut, model, options = {}) {
|
|
60230
|
+
const release = this.storage.hold(sessionID);
|
|
60231
|
+
try {
|
|
60232
|
+
return await this.restoreHeld(sessionID, cut, model, options);
|
|
60233
|
+
} finally {
|
|
60234
|
+
this.account(sessionID);
|
|
60235
|
+
release();
|
|
60236
|
+
}
|
|
60237
|
+
}
|
|
60238
|
+
async restoreHeld(sessionID, cut, model, options) {
|
|
60239
|
+
if (!isLocalCheckpoint(cut))
|
|
60240
|
+
return;
|
|
60241
|
+
const resolvedModel = {
|
|
60242
|
+
providerID: model.slice(0, model.indexOf("/")),
|
|
60243
|
+
id: model.slice(model.indexOf("/") + 1)
|
|
60244
|
+
};
|
|
60245
|
+
const tail = this.tail(sessionID);
|
|
60246
|
+
if (tail && tail.model !== model)
|
|
60247
|
+
this.invalidate(sessionID);
|
|
60248
|
+
let after = options.after ?? -1;
|
|
60249
|
+
if (options.bounded && options.after === undefined && !this.changed.has(sessionID)) {
|
|
60250
|
+
const state = getOrCreateSessionMeta(this.storage.db, sessionID);
|
|
60251
|
+
const max = this.storage.db.prepare("SELECT COALESCE(MAX(id),0) AS id FROM m0_mutation_log WHERE session_id=?").get(sessionID);
|
|
60252
|
+
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;
|
|
60253
|
+
if (max.id <= (state.cachedM0MaxMutationId ?? 0) && boundary)
|
|
60254
|
+
after = this.storage.sequenceForID(sessionID, boundary) ?? -1;
|
|
60255
|
+
}
|
|
60256
|
+
const through = cut.seq - 1;
|
|
60257
|
+
let reader;
|
|
60258
|
+
try {
|
|
60259
|
+
let metadata = this.metadata.get(sessionID);
|
|
60260
|
+
if (!metadata || (this.metadataThrough.get(sessionID) ?? -1) < through) {
|
|
60261
|
+
reader = this.openReader(sessionID);
|
|
60262
|
+
metadata = reader.rowStampsThrough(sessionID, through).filter(conversational);
|
|
60263
|
+
this.metadata.set(sessionID, metadata);
|
|
60264
|
+
this.metadataThrough.set(sessionID, through);
|
|
60265
|
+
}
|
|
60266
|
+
const required = metadata.filter((row) => row.seq > after && row.seq <= through);
|
|
60267
|
+
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))) {
|
|
60268
|
+
reader ??= this.openReader(sessionID);
|
|
60269
|
+
const fresh = reader.range(sessionID, after, through).filter(conversational);
|
|
60270
|
+
const records = fresh.map((row) => this.record(row, this.rendered(row, resolvedModel)));
|
|
60271
|
+
const restored = new Set(records.map((row) => row.id));
|
|
60272
|
+
if (required.some((row) => !restored.has(row.id)))
|
|
60273
|
+
throw new NativeReplayUnavailable("Host restore omitted a required source row");
|
|
60274
|
+
this.storage.replaceRows(sessionID, through, records, after);
|
|
60275
|
+
await this.storage.saveTail(sessionID, JSON.stringify({
|
|
60276
|
+
model,
|
|
60277
|
+
sourceSeq: through,
|
|
60278
|
+
baseline: tail?.baseline ?? cut.data.summary
|
|
60279
|
+
}), []);
|
|
60280
|
+
this.loaded.set(sessionID, through);
|
|
60281
|
+
this.dirty.delete(sessionID);
|
|
60282
|
+
}
|
|
60283
|
+
} finally {
|
|
60284
|
+
reader?.close();
|
|
60285
|
+
}
|
|
60286
|
+
const messages = [];
|
|
60287
|
+
for (const row of this.storage.rows(sessionID, after, cut.seq - 1)) {
|
|
60288
|
+
const revived = reviveNative(JSON.parse(row.data));
|
|
60289
|
+
for (const message of revived)
|
|
60290
|
+
coordinates.set(message, row.id);
|
|
60291
|
+
messages.push(...revived);
|
|
60292
|
+
}
|
|
60293
|
+
return messages;
|
|
60294
|
+
}
|
|
60295
|
+
async restoreRead(sessionID, cut, model) {
|
|
60296
|
+
const reader = this.openReader(sessionID);
|
|
60297
|
+
try {
|
|
60298
|
+
return reader.range(sessionID, -1, cut.seq - 1).filter(conversational).flatMap((row) => {
|
|
60299
|
+
const messages = this.rendered(row, model);
|
|
60300
|
+
for (const message of messages)
|
|
60301
|
+
coordinates.set(message, row.id);
|
|
60302
|
+
return messages;
|
|
60303
|
+
});
|
|
60304
|
+
} finally {
|
|
60305
|
+
reader.close();
|
|
60306
|
+
}
|
|
60307
|
+
}
|
|
60308
|
+
sourceChanged(sessionID) {
|
|
60309
|
+
return this.changed.has(sessionID);
|
|
60310
|
+
}
|
|
60311
|
+
clear(sessionID) {
|
|
60312
|
+
this.sources.delete(sessionID);
|
|
60313
|
+
this.metadata.delete(sessionID);
|
|
60314
|
+
this.metadataThrough.delete(sessionID);
|
|
60315
|
+
this.loaded.delete(sessionID);
|
|
60316
|
+
this.unavailable.delete(sessionID);
|
|
60317
|
+
this.dirty.delete(sessionID);
|
|
60318
|
+
this.changed.delete(sessionID);
|
|
60319
|
+
}
|
|
60320
|
+
async forget(sessionID) {
|
|
60321
|
+
await this.storage.forget(sessionID);
|
|
60322
|
+
}
|
|
60323
|
+
}
|
|
60324
|
+
var coordinates, conversational = (row) => RAW_MESSAGE_TYPES.includes(row.type) || row.type === "location-switched";
|
|
60325
|
+
var init_native_replay = __esm(async () => {
|
|
60326
|
+
init_payload();
|
|
60327
|
+
init_host_media();
|
|
60328
|
+
init_native_codec();
|
|
60329
|
+
init_owner();
|
|
60330
|
+
init_restore();
|
|
60331
|
+
await __promiseAll([
|
|
60332
|
+
init_storage(),
|
|
60333
|
+
init_store_reader()
|
|
60334
|
+
]);
|
|
60335
|
+
coordinates = new WeakMap;
|
|
60336
|
+
});
|
|
60337
|
+
|
|
60338
|
+
// src/v2/fold/policy.ts
|
|
60339
|
+
import { randomUUID as randomUUID5 } from "node:crypto";
|
|
60340
|
+
|
|
60341
|
+
class HostFoldPolicy {
|
|
60342
|
+
deps;
|
|
60343
|
+
active = new Set;
|
|
60344
|
+
owner = randomUUID5();
|
|
60345
|
+
constructor(deps) {
|
|
60346
|
+
this.deps = deps;
|
|
60347
|
+
}
|
|
60348
|
+
owned(sessionID, cut, id) {
|
|
60349
|
+
const row = this.deps.storage.admission(sessionID, id);
|
|
60350
|
+
if (!isLocalCheckpoint(cut) || cut.id !== id || !row?.payload || row.digest !== foldDigest(row.payload))
|
|
60351
|
+
return false;
|
|
60352
|
+
const snapshot = JSON.parse(row.payload);
|
|
60353
|
+
return snapshot.summary === cut.data.summary && snapshot.source === this.deps.storage.sourceID && snapshot.sessionID === sessionID && snapshot.admissionID === id;
|
|
60354
|
+
}
|
|
60355
|
+
async idle(sessionID) {
|
|
60356
|
+
const { storage, session, threshold } = this.deps;
|
|
60357
|
+
if (!Number.isSafeInteger(threshold) || threshold <= 0 || typeof session.compact !== "function" || this.active.has(sessionID))
|
|
60358
|
+
return;
|
|
60359
|
+
this.active.add(sessionID);
|
|
60360
|
+
const now = this.deps.now ?? Date.now;
|
|
60361
|
+
let attempt;
|
|
60362
|
+
let claimed = false;
|
|
60363
|
+
try {
|
|
60364
|
+
const reader = this.deps.openReader();
|
|
60365
|
+
try {
|
|
60366
|
+
const cut = reader.latestCompaction(sessionID);
|
|
60367
|
+
const previous = storage.latestAttempt(sessionID);
|
|
60368
|
+
const old = previous ? JSON.parse(previous.data) : undefined;
|
|
60369
|
+
if (cut && previous?.status === "pending" && old && this.owned(sessionID, cut, old.id)) {
|
|
60370
|
+
if (await storage.claim(sessionID, old.id, previous.data, this.owner, now()) && await storage.finish(sessionID, old.id, this.owner, "completed", now()))
|
|
60371
|
+
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`);
|
|
60372
|
+
return;
|
|
60373
|
+
}
|
|
60374
|
+
const after = cut?.seq ?? -1;
|
|
60375
|
+
const before = reader.storedRowsAfter(sessionID, after);
|
|
60376
|
+
if (before < threshold || !await this.deps.canReplay(sessionID, reader))
|
|
60377
|
+
return;
|
|
60378
|
+
const retry = old?.after === after ? old.attempt + (previous?.status === "failed" || previous?.status === "coalesced" ? 1 : 0) : 0;
|
|
60379
|
+
const id = old?.after === after && previous?.status === "pending" ? old.id : `msg_mc_fold_${foldDigest(`${storage.sourceID}/${sessionID}/${after}/${retry}`).slice(0, 24)}`;
|
|
60380
|
+
attempt = {
|
|
60381
|
+
after,
|
|
60382
|
+
before,
|
|
60383
|
+
watermark: reader.latestSequence(sessionID),
|
|
60384
|
+
attempt: retry,
|
|
60385
|
+
id,
|
|
60386
|
+
started: now()
|
|
60387
|
+
};
|
|
60388
|
+
claimed = await storage.claim(sessionID, id, JSON.stringify(attempt), this.owner, now());
|
|
60389
|
+
if (!claimed)
|
|
60390
|
+
return;
|
|
60391
|
+
} finally {
|
|
60392
|
+
reader.close();
|
|
60393
|
+
}
|
|
60394
|
+
const admitted = await session.compact({
|
|
60395
|
+
sessionID,
|
|
60396
|
+
id: attempt.id,
|
|
60397
|
+
delivery: "queue"
|
|
60398
|
+
});
|
|
60399
|
+
await session.wait({ sessionID });
|
|
60400
|
+
const settled = this.deps.openReader();
|
|
60401
|
+
try {
|
|
60402
|
+
const cut = settled.latestCompaction(sessionID);
|
|
60403
|
+
if (admitted?.id !== attempt.id || !cut || cut.seq <= attempt.after || !this.owned(sessionID, cut, attempt.id)) {
|
|
60404
|
+
if (await storage.finish(sessionID, attempt.id, this.owner, "coalesced", now()))
|
|
60405
|
+
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`);
|
|
60406
|
+
return;
|
|
60407
|
+
}
|
|
60408
|
+
const after = settled.storedRowsAfter(sessionID, cut.seq);
|
|
60409
|
+
if (await storage.finish(sessionID, attempt.id, this.owner, "completed", now()))
|
|
60410
|
+
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`);
|
|
60411
|
+
} finally {
|
|
60412
|
+
settled.close();
|
|
60413
|
+
}
|
|
60414
|
+
} catch (error) {
|
|
60415
|
+
if (claimed && attempt) {
|
|
60416
|
+
let finished = false;
|
|
60417
|
+
try {
|
|
60418
|
+
finished = await storage.finish(sessionID, attempt.id, this.owner, "failed", now());
|
|
60419
|
+
} catch {
|
|
60420
|
+
finished = true;
|
|
60421
|
+
}
|
|
60422
|
+
if (finished)
|
|
60423
|
+
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))}`);
|
|
60424
|
+
} else
|
|
60425
|
+
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))}`);
|
|
60426
|
+
} finally {
|
|
60427
|
+
this.active.delete(sessionID);
|
|
60428
|
+
}
|
|
60429
|
+
}
|
|
60430
|
+
}
|
|
60431
|
+
var init_policy = __esm(async () => {
|
|
60432
|
+
init_owner();
|
|
60433
|
+
await init_native_replay();
|
|
60434
|
+
});
|
|
60435
|
+
|
|
58981
60436
|
// src/v2/fork-inheritance.ts
|
|
58982
60437
|
function forkBoundarySeq(origin, sequenceForId) {
|
|
58983
60438
|
const seq = sequenceForId(origin.parentSessionID, origin.boundary.messageID);
|
|
@@ -59106,11 +60561,7 @@ function keptUnderRetention(keepSubagents, child) {
|
|
|
59106
60561
|
return keepSubagents && (child.ever_settled === true || child.role === "historian");
|
|
59107
60562
|
}
|
|
59108
60563
|
function roleTitle(role) {
|
|
59109
|
-
|
|
59110
|
-
return "Magic Context maintainer A";
|
|
59111
|
-
if (role === "maintainer-b")
|
|
59112
|
-
return "Magic Context maintainer B";
|
|
59113
|
-
return "Magic Context historian";
|
|
60564
|
+
return HIDDEN_CHILD_ROLE_TITLES[role];
|
|
59114
60565
|
}
|
|
59115
60566
|
function roleAgent(role) {
|
|
59116
60567
|
if (role === "maintainer-a")
|
|
@@ -59157,9 +60608,15 @@ function errorText(value) {
|
|
|
59157
60608
|
return String(value);
|
|
59158
60609
|
}
|
|
59159
60610
|
}
|
|
60611
|
+
var HIDDEN_CHILD_ROLE_TITLES;
|
|
59160
60612
|
var init_hidden_child_record = __esm(() => {
|
|
59161
60613
|
init_memory_maintainer();
|
|
59162
60614
|
init_hidden_child();
|
|
60615
|
+
HIDDEN_CHILD_ROLE_TITLES = {
|
|
60616
|
+
"maintainer-a": "Magic Context maintainer A",
|
|
60617
|
+
"maintainer-b": "Magic Context maintainer B",
|
|
60618
|
+
historian: "Magic Context historian"
|
|
60619
|
+
};
|
|
59163
60620
|
});
|
|
59164
60621
|
|
|
59165
60622
|
// src/v2/hidden-child-native.ts
|
|
@@ -59686,7 +61143,8 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
59686
61143
|
aborted
|
|
59687
61144
|
]);
|
|
59688
61145
|
if (!attempt.shaped) {
|
|
59689
|
-
|
|
61146
|
+
const task = run.identity.title.replace(/^magic-context-dream-/, "");
|
|
61147
|
+
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
61148
|
}
|
|
59691
61149
|
const text = assistantText(row);
|
|
59692
61150
|
const system = typeof request.body.system === "string" ? request.body.system : run.identity.system;
|
|
@@ -60010,392 +61468,6 @@ class DeletedSessionTombstones {
|
|
|
60010
61468
|
}
|
|
60011
61469
|
}
|
|
60012
61470
|
|
|
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
61471
|
// src/v2/hooks/generate.ts
|
|
60400
61472
|
class V2GenerateReplay {
|
|
60401
61473
|
prefixes = new Map;
|
|
@@ -60445,9 +61517,9 @@ var init_hidden_terminal_error = __esm(() => {
|
|
|
60445
61517
|
});
|
|
60446
61518
|
|
|
60447
61519
|
// src/v2/hooks/lkg-system.ts
|
|
60448
|
-
import { createHash as
|
|
60449
|
-
function
|
|
60450
|
-
return
|
|
61520
|
+
import { createHash as createHash21 } from "node:crypto";
|
|
61521
|
+
function identity2(slot) {
|
|
61522
|
+
return createHash21("sha256").update(JSON.stringify({
|
|
60451
61523
|
capturedAt: slot.capturedAt,
|
|
60452
61524
|
captureSequence: slot.captureSequence,
|
|
60453
61525
|
rowVersion: slot.rowVersion,
|
|
@@ -60462,14 +61534,14 @@ class V2LkgSystemReplay {
|
|
|
60462
61534
|
systems = new BoundedSessionMap(1000);
|
|
60463
61535
|
capture(sessionId, slot, input, served) {
|
|
60464
61536
|
this.systems.set(sessionId, {
|
|
60465
|
-
slot:
|
|
61537
|
+
slot: identity2(slot),
|
|
60466
61538
|
input: JSON.stringify(input),
|
|
60467
61539
|
served: structuredClone(served)
|
|
60468
61540
|
});
|
|
60469
61541
|
}
|
|
60470
61542
|
restore(sessionId, slot, input, target) {
|
|
60471
61543
|
const saved = this.systems.get(sessionId);
|
|
60472
|
-
if (!slot || !saved || saved.slot !==
|
|
61544
|
+
if (!slot || !saved || saved.slot !== identity2(slot) || saved.input !== JSON.stringify(input))
|
|
60473
61545
|
return false;
|
|
60474
61546
|
target.splice(0, target.length, ...structuredClone(saved.served));
|
|
60475
61547
|
return true;
|
|
@@ -60662,7 +61734,7 @@ function hasRequiredSessionAPI(session) {
|
|
|
60662
61734
|
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
61735
|
|
|
60664
61736
|
// src/features/magic-context/session-project-backfill.ts
|
|
60665
|
-
import { randomUUID as
|
|
61737
|
+
import { randomUUID as randomUUID6 } from "node:crypto";
|
|
60666
61738
|
import { existsSync as existsSync14 } from "node:fs";
|
|
60667
61739
|
function ensureBackfillStateTable2(db) {
|
|
60668
61740
|
db.exec(`
|
|
@@ -60799,7 +61871,7 @@ async function runSessionProjectBackfill(db, source, options = {}) {
|
|
|
60799
61871
|
const now = options.now ?? Date.now;
|
|
60800
61872
|
const resolveIdentity = options.resolveIdentity ?? ((directory) => resolveProjectIdentityForSession(directory, options.allowHomeProject));
|
|
60801
61873
|
const yieldFn = options.yieldFn ?? defaultYieldFn;
|
|
60802
|
-
const holderId = options.holderId ??
|
|
61874
|
+
const holderId = options.holderId ?? randomUUID6();
|
|
60803
61875
|
const readPage = createPageReader(source);
|
|
60804
61876
|
const result = {
|
|
60805
61877
|
status: "completed",
|
|
@@ -62222,7 +63294,7 @@ __export(exports_regexes2, {
|
|
|
62222
63294
|
extendedDuration: () => extendedDuration,
|
|
62223
63295
|
guid: () => guid2,
|
|
62224
63296
|
hex: () => hex,
|
|
62225
|
-
hostname: () =>
|
|
63297
|
+
hostname: () => hostname2,
|
|
62226
63298
|
html5Email: () => html5Email,
|
|
62227
63299
|
idnEmail: () => idnEmail,
|
|
62228
63300
|
integer: () => integer2,
|
|
@@ -62292,7 +63364,7 @@ var cuid3, cuid22, ulid2, xid2, ksuid2, nanoid2, duration3, extendedDuration, gu
|
|
|
62292
63364
|
if (!version)
|
|
62293
63365
|
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
63366
|
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,
|
|
63367
|
+
}, 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
63368
|
const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
|
|
62297
63369
|
return new RegExp(`^${regex}$`);
|
|
62298
63370
|
}, 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 +63393,7 @@ var init_regexes2 = __esm(() => {
|
|
|
62321
63393
|
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
63394
|
base642 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
|
|
62323
63395
|
base64url2 = /^[A-Za-z0-9_-]*$/;
|
|
62324
|
-
|
|
63396
|
+
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
63397
|
domain = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
|
|
62326
63398
|
e1642 = /^\+(?:[0-9]){6,14}[0-9]$/;
|
|
62327
63399
|
date3 = /* @__PURE__ */ new RegExp(`^${dateSource2}$`);
|
|
@@ -63432,7 +64504,7 @@ var init_schemas3 = __esm(() => {
|
|
|
63432
64504
|
code: "invalid_format",
|
|
63433
64505
|
format: "url",
|
|
63434
64506
|
note: "Invalid hostname",
|
|
63435
|
-
pattern:
|
|
64507
|
+
pattern: hostname2.source,
|
|
63436
64508
|
input: payload.value,
|
|
63437
64509
|
inst,
|
|
63438
64510
|
continue: !def.abort
|
|
@@ -72706,8 +73778,8 @@ function jwt(params) {
|
|
|
72706
73778
|
function stringFormat(format, fnOrRegex, _params = {}) {
|
|
72707
73779
|
return _stringFormat2(ZodCustomStringFormat, format, fnOrRegex, _params);
|
|
72708
73780
|
}
|
|
72709
|
-
function
|
|
72710
|
-
return _stringFormat2(ZodCustomStringFormat, "hostname",
|
|
73781
|
+
function hostname3(_params) {
|
|
73782
|
+
return _stringFormat2(ZodCustomStringFormat, "hostname", hostname2, _params);
|
|
72711
73783
|
}
|
|
72712
73784
|
function hex2(_params) {
|
|
72713
73785
|
return _stringFormat2(ZodCustomStringFormat, "hex", hex, _params);
|
|
@@ -73792,7 +74864,7 @@ __export(exports_external2, {
|
|
|
73792
74864
|
guid: () => guid3,
|
|
73793
74865
|
hash: () => hash,
|
|
73794
74866
|
hex: () => hex2,
|
|
73795
|
-
hostname: () =>
|
|
74867
|
+
hostname: () => hostname3,
|
|
73796
74868
|
httpUrl: () => httpUrl,
|
|
73797
74869
|
includes: () => _includes2,
|
|
73798
74870
|
instanceof: () => _instanceof,
|
|
@@ -74000,7 +75072,7 @@ Returns the raw transcript as [N] U:/A: lines, capped at ~15K tokens; an oversiz
|
|
|
74000
75072
|
|
|
74001
75073
|
Finer recovery:
|
|
74002
75074
|
- verbose=true lists each message separately with its ordinal and a per-part preview (tool calls with output sizes) so you can pick one.
|
|
74003
|
-
- message=N returns that one message in full — every text part and every tool call's complete input and output — from stored history. This is the way back to a tool output you released with mmc_reduce; if the message
|
|
75075
|
+
- message=N returns that one message in full — every text part and every tool call's complete input and output — from stored history. This is the way back to a tool output you released with mmc_reduce; if the message is no longer in stored history it says so.`, CTX_EXPAND_TOKEN_BUDGET = 15000;
|
|
74004
75076
|
|
|
74005
75077
|
// src/features/magic-context/tag-input.ts
|
|
74006
75078
|
function parseTagInput(value) {
|
|
@@ -74073,7 +75145,7 @@ function resolveCtxExpandMode(args, domain) {
|
|
|
74073
75145
|
var init_mode = () => {};
|
|
74074
75146
|
|
|
74075
75147
|
// src/tools/ctx-expand/render.ts
|
|
74076
|
-
import { createHash as
|
|
75148
|
+
import { createHash as createHash22 } from "node:crypto";
|
|
74077
75149
|
function isRecord7(value) {
|
|
74078
75150
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
74079
75151
|
}
|
|
@@ -74162,7 +75234,7 @@ function reasoningOf(part) {
|
|
|
74162
75234
|
function renderPartPreview(part, expandTools) {
|
|
74163
75235
|
if (!isRecord7(part))
|
|
74164
75236
|
return null;
|
|
74165
|
-
const expansion = expandToolPart(part, expandTools);
|
|
75237
|
+
const expansion = expandToolPart(part, expandTools, true);
|
|
74166
75238
|
if (expansion !== null)
|
|
74167
75239
|
return ` • tool ${part.tool}: ${expansion}`;
|
|
74168
75240
|
const text = textOf(part);
|
|
@@ -74226,7 +75298,7 @@ function renderMessageByOrdinal(sessionId, ordinal) {
|
|
|
74226
75298
|
});
|
|
74227
75299
|
const msg = found[0];
|
|
74228
75300
|
if (!msg) {
|
|
74229
|
-
return `No message at ordinal ${ordinal} in this session's stored history — it
|
|
75301
|
+
return `No message at ordinal ${ordinal} is currently in this session's stored history — ` + `it may have been removed by a session prune/revert, or the ordinal may be wrong. ` + `Re-run the tool if you still need the data.`;
|
|
74230
75302
|
}
|
|
74231
75303
|
const rendered = msg.parts.map(renderPartFull).filter((l) => l !== null);
|
|
74232
75304
|
const lines = [`[${msg.ordinal}] ${roleLabel(msg.role)} — full recovery:`, ""];
|
|
@@ -74297,9 +75369,12 @@ function renderItemByTag(db, sessionId, number, textIndexDomain = "part") {
|
|
|
74297
75369
|
const index = scoped ? Number(scoped[2]) : 0;
|
|
74298
75370
|
const piText = message.parts.filter((part) => isRecord7(part) && part.type === "text");
|
|
74299
75371
|
const part = message.parts[index];
|
|
74300
|
-
const matching = derived ? piText.filter((part) => isRecord7(part) && typeof part.text === "string" &&
|
|
75372
|
+
const matching = derived ? piText.filter((part) => isRecord7(part) && typeof part.text === "string" && createHash22("sha256").update(part.text).digest("hex") === derived[3]) : [];
|
|
74301
75373
|
const selected = derived ? matching[Number(derived[4])] : textIndexDomain === "text" ? piText[index] : part;
|
|
74302
|
-
|
|
75374
|
+
if (!selected)
|
|
75375
|
+
return `Tag ${number}'s original text part is no longer in stored history.`;
|
|
75376
|
+
const renderedPart = renderPartFull(selected);
|
|
75377
|
+
return renderedPart !== null ? renderedPart : `Tag ${number}'s stored text part has no renderable content (empty or structural-only); the requested content was not recovered.`;
|
|
74303
75378
|
}
|
|
74304
75379
|
var init_render = __esm(async () => {
|
|
74305
75380
|
init_read_session_formatting();
|
|
@@ -74325,52 +75400,57 @@ function createCtxExpandTool(deps) {
|
|
|
74325
75400
|
message: "number"
|
|
74326
75401
|
});
|
|
74327
75402
|
const sessionId = toolContext.sessionID;
|
|
74328
|
-
|
|
74329
|
-
|
|
74330
|
-
|
|
74331
|
-
|
|
74332
|
-
|
|
74333
|
-
|
|
74334
|
-
|
|
74335
|
-
|
|
74336
|
-
|
|
74337
|
-
|
|
74338
|
-
|
|
74339
|
-
|
|
74340
|
-
|
|
74341
|
-
|
|
74342
|
-
|
|
74343
|
-
|
|
74344
|
-
|
|
74345
|
-
|
|
74346
|
-
|
|
74347
|
-
|
|
74348
|
-
|
|
74349
|
-
|
|
74350
|
-
|
|
74351
|
-
|
|
74352
|
-
|
|
74353
|
-
|
|
74354
|
-
|
|
74355
|
-
|
|
74356
|
-
|
|
74357
|
-
|
|
75403
|
+
try {
|
|
75404
|
+
const mode = resolveCtxExpandMode(args, "positive");
|
|
75405
|
+
if (mode.kind === "error") {
|
|
75406
|
+
return mode.message;
|
|
75407
|
+
}
|
|
75408
|
+
if (mode.kind === "tag") {
|
|
75409
|
+
return renderItemByTag(deps.db, sessionId, mode.tag);
|
|
75410
|
+
}
|
|
75411
|
+
if (mode.kind === "message") {
|
|
75412
|
+
return renderMessageByOrdinal(sessionId, mode.message);
|
|
75413
|
+
}
|
|
75414
|
+
const { start, end, verbose } = mode;
|
|
75415
|
+
const lastCompartmentEnd = getLastCompartmentEndMessage(deps.db, sessionId);
|
|
75416
|
+
if (lastCompartmentEnd >= 0 && start > lastCompartmentEnd) {
|
|
75417
|
+
return `Range ${start}-${end} is entirely within the live tail (after the last compacted message ${lastCompartmentEnd}); those messages are already visible in context.`;
|
|
75418
|
+
}
|
|
75419
|
+
const effectiveEnd = lastCompartmentEnd >= 0 ? Math.min(end, lastCompartmentEnd) : end;
|
|
75420
|
+
if (verbose) {
|
|
75421
|
+
const v = renderVerboseRange(sessionId, start, effectiveEnd, CTX_EXPAND_TOKEN_BUDGET, deps.expandTools);
|
|
75422
|
+
if (!v.text) {
|
|
75423
|
+
return `No messages found in range ${start}-${effectiveEnd}. The range may be outside this session's history.`;
|
|
75424
|
+
}
|
|
75425
|
+
const out = [
|
|
75426
|
+
`Messages ${start}-${v.lastOrdinal} (verbose). Recover any one in full with mmc_expand(message=<ordinal>):`,
|
|
75427
|
+
"",
|
|
75428
|
+
v.text
|
|
75429
|
+
];
|
|
75430
|
+
if (v.truncated) {
|
|
75431
|
+
out.push("", `Truncated at message ${v.lastOrdinal} (budget: ~${CTX_EXPAND_TOKEN_BUDGET} tokens). Call again with start=${v.lastOrdinal + 1} end=${effectiveEnd} verbose=true for more.`);
|
|
75432
|
+
}
|
|
75433
|
+
return out.join(`
|
|
74358
75434
|
`);
|
|
74359
|
-
|
|
74360
|
-
|
|
74361
|
-
|
|
74362
|
-
|
|
74363
|
-
|
|
74364
|
-
|
|
74365
|
-
|
|
74366
|
-
lines.push("");
|
|
74367
|
-
lines.push(chunk.text);
|
|
74368
|
-
if (chunk.endIndex < effectiveEnd) {
|
|
75435
|
+
}
|
|
75436
|
+
const chunk = readSessionChunk(sessionId, CTX_EXPAND_TOKEN_BUDGET, start, effectiveEnd + 1, { expand: false });
|
|
75437
|
+
if (!chunk.text || chunk.messageCount === 0) {
|
|
75438
|
+
return `No messages found in range ${start}-${end}. The range may be outside this session's history.`;
|
|
75439
|
+
}
|
|
75440
|
+
const lines = [];
|
|
75441
|
+
lines.push(`Messages ${chunk.startIndex}-${chunk.endIndex} (${chunk.messageCount} messages, ~${chunk.tokenEstimate} tokens):`);
|
|
74369
75442
|
lines.push("");
|
|
74370
|
-
lines.push(
|
|
74371
|
-
|
|
74372
|
-
|
|
75443
|
+
lines.push(chunk.text);
|
|
75444
|
+
if (chunk.endIndex < effectiveEnd) {
|
|
75445
|
+
lines.push("");
|
|
75446
|
+
lines.push(`Truncated at message ${chunk.endIndex} (budget: ~${CTX_EXPAND_TOKEN_BUDGET} tokens). Call again with start=${chunk.endIndex + 1} end=${effectiveEnd} for more.`);
|
|
75447
|
+
}
|
|
75448
|
+
return lines.join(`
|
|
74373
75449
|
`);
|
|
75450
|
+
} catch (error) {
|
|
75451
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
75452
|
+
return `Error: mmc_expand failed to read this session's stored history: ${message}`;
|
|
75453
|
+
}
|
|
74374
75454
|
}
|
|
74375
75455
|
});
|
|
74376
75456
|
}
|
|
@@ -74445,7 +75525,7 @@ var init_memory_visibility = __esm(() => {
|
|
|
74445
75525
|
|
|
74446
75526
|
// src/tools/ctx-memory/types.ts
|
|
74447
75527
|
var CTX_MEMORY_ACTIONS;
|
|
74448
|
-
var
|
|
75528
|
+
var init_types2 = __esm(() => {
|
|
74449
75529
|
CTX_MEMORY_ACTIONS = ["write", "update", "archive", "merge", "get"];
|
|
74450
75530
|
});
|
|
74451
75531
|
|
|
@@ -74469,7 +75549,7 @@ var init_verification_recording = __esm(() => {
|
|
|
74469
75549
|
|
|
74470
75550
|
// src/tools/ctx-memory/tools.ts
|
|
74471
75551
|
function isMemoryCategory2(value) {
|
|
74472
|
-
return
|
|
75552
|
+
return MEMORY_CATEGORIES2.has(value);
|
|
74473
75553
|
}
|
|
74474
75554
|
function getAllowedActions(deps) {
|
|
74475
75555
|
const allowed = deps.allowedActions?.length ? deps.allowedActions : CTX_MEMORY_ACTIONS;
|
|
@@ -74871,7 +75951,7 @@ function createCtxMemoryTools(deps) {
|
|
|
74871
75951
|
[CTX_MEMORY_TOOL_NAME]: createCtxMemoryTool(deps)
|
|
74872
75952
|
};
|
|
74873
75953
|
}
|
|
74874
|
-
var
|
|
75954
|
+
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
75955
|
var init_tools2 = __esm(async () => {
|
|
74876
75956
|
init_dist();
|
|
74877
75957
|
init_memory_visibility();
|
|
@@ -74879,7 +75959,7 @@ var init_tools2 = __esm(async () => {
|
|
|
74879
75959
|
init_project_identity();
|
|
74880
75960
|
init_user_facing_codes();
|
|
74881
75961
|
init_unwrap_imitated_reduced_args();
|
|
74882
|
-
|
|
75962
|
+
init_types2();
|
|
74883
75963
|
init_verification_recording();
|
|
74884
75964
|
await __promiseAll([
|
|
74885
75965
|
init_context_authority(),
|
|
@@ -74887,7 +75967,7 @@ var init_tools2 = __esm(async () => {
|
|
|
74887
75967
|
init_storage_memory(),
|
|
74888
75968
|
init_storage()
|
|
74889
75969
|
]);
|
|
74890
|
-
|
|
75970
|
+
MEMORY_CATEGORIES2 = new Set(CATEGORY_PRIORITY);
|
|
74891
75971
|
ctxMemoryArgsShape = {
|
|
74892
75972
|
action: tool.schema.enum([...CTX_MEMORY_ACTIONS]).optional().describe("write | update | archive | merge | get"),
|
|
74893
75973
|
content: tool.schema.string().optional().describe("The memory text — one standalone fact (write, update, merge)."),
|
|
@@ -74901,7 +75981,7 @@ var init_tools2 = __esm(async () => {
|
|
|
74901
75981
|
// src/tools/ctx-memory/index.ts
|
|
74902
75982
|
var init_ctx_memory = __esm(async () => {
|
|
74903
75983
|
init_tools2();
|
|
74904
|
-
|
|
75984
|
+
init_types2();
|
|
74905
75985
|
});
|
|
74906
75986
|
|
|
74907
75987
|
// src/tools/ctx-note/constants.ts
|
|
@@ -75934,6 +77014,8 @@ async function registerContext(context) {
|
|
|
75934
77014
|
return;
|
|
75935
77015
|
}
|
|
75936
77016
|
const folds = new FoldOwner(context.storage);
|
|
77017
|
+
const nativeFoldMemory = !compactionOff && process.env.MMC_OC2_INVISIBLE_FOLD === "1" ? nativeFoldCache(() => storage.require()) : undefined;
|
|
77018
|
+
const nativeFoldReplay = nativeFoldMemory ? new NativeFoldReplay(nativeFoldMemory, () => openStoreReader()) : undefined;
|
|
75937
77019
|
setOutputReserveConfig(config.output_reserve);
|
|
75938
77020
|
const queriedModels = new Set;
|
|
75939
77021
|
const rawLimits = new Map;
|
|
@@ -75965,7 +77047,7 @@ async function registerContext(context) {
|
|
|
75965
77047
|
let storageRecoveryAnnounced = false;
|
|
75966
77048
|
const storageNoticeBySession = new Map;
|
|
75967
77049
|
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)));
|
|
77050
|
+
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
77051
|
};
|
|
75970
77052
|
const noticeStorageRefusal = (sessionID, reason) => {
|
|
75971
77053
|
const message = formatStorageRefusalNotice(reason);
|
|
@@ -76232,6 +77314,14 @@ async function registerContext(context) {
|
|
|
76232
77314
|
}
|
|
76233
77315
|
return unsafe;
|
|
76234
77316
|
};
|
|
77317
|
+
const hostFolds = nativeFoldReplay && nativeFoldMemory ? new HostFoldPolicy({
|
|
77318
|
+
session: context.session,
|
|
77319
|
+
storage: nativeFoldMemory,
|
|
77320
|
+
openReader: openStoreReader,
|
|
77321
|
+
canReplay: (sessionID, reader) => nativeFoldReplay.canReplay(sessionID, reader),
|
|
77322
|
+
threshold: Number(process.env.MMC_OC2_HOST_FOLD_ROWS ?? 1000),
|
|
77323
|
+
log: sessionLog
|
|
77324
|
+
}) : undefined;
|
|
76235
77325
|
const usageController = new AbortController;
|
|
76236
77326
|
const usageDone = (async () => {
|
|
76237
77327
|
try {
|
|
@@ -76242,6 +77332,10 @@ async function registerContext(context) {
|
|
|
76242
77332
|
if (!event.data?.sessionID)
|
|
76243
77333
|
continue;
|
|
76244
77334
|
const sessionID = event.data.sessionID;
|
|
77335
|
+
if (event.type?.startsWith("session.revert.") || event.type === "session.message.content.updated") {
|
|
77336
|
+
nativeFoldReplay?.onEvent(event);
|
|
77337
|
+
restoredRows.forget(sessionID);
|
|
77338
|
+
}
|
|
76245
77339
|
if (event.type === "session.error" || event.type === "session.execution.failed") {
|
|
76246
77340
|
const error = hiddenTerminalError(event);
|
|
76247
77341
|
if (error !== undefined)
|
|
@@ -76267,6 +77361,7 @@ async function registerContext(context) {
|
|
|
76267
77361
|
pendingMaterializationSessions.delete(sessionID);
|
|
76268
77362
|
lastHeuristicsTurnId.delete(sessionID);
|
|
76269
77363
|
restoredRows.forget(sessionID);
|
|
77364
|
+
await nativeFoldReplay?.forget(sessionID);
|
|
76270
77365
|
generateReplay.forget(sessionID);
|
|
76271
77366
|
systemPromptRefreshSessions.delete(sessionID);
|
|
76272
77367
|
systemPrompt?.clearSession(sessionID);
|
|
@@ -76279,11 +77374,14 @@ async function registerContext(context) {
|
|
|
76279
77374
|
if (event.type !== "session.execution.succeeded")
|
|
76280
77375
|
continue;
|
|
76281
77376
|
const model = liveModels.get(sessionID);
|
|
76282
|
-
if (model)
|
|
77377
|
+
if (model) {
|
|
76283
77378
|
await recordUsage({
|
|
76284
77379
|
sessionID,
|
|
76285
77380
|
model: { providerID: model.providerID, id: model.modelID }
|
|
76286
77381
|
});
|
|
77382
|
+
if (!compactionOff && typeof await nativeFoldReplay?.baseline(sessionID) === "string")
|
|
77383
|
+
hostFolds?.idle(sessionID);
|
|
77384
|
+
}
|
|
76287
77385
|
}
|
|
76288
77386
|
} catch (error) {
|
|
76289
77387
|
if (!usageController.signal.aborted)
|
|
@@ -76318,10 +77416,32 @@ async function registerContext(context) {
|
|
|
76318
77416
|
}
|
|
76319
77417
|
if (compactionOff)
|
|
76320
77418
|
return;
|
|
77419
|
+
if (nativeFoldReplay)
|
|
77420
|
+
rememberHostMedia(draft.messages);
|
|
76321
77421
|
const reader = openStoreReader();
|
|
76322
77422
|
try {
|
|
76323
77423
|
const watermark = reader.latestSequenceForIds(draft.sessionID, draft.messages.flatMap((message) => message.id ? [message.id] : []));
|
|
76324
77424
|
const running = reader.latestRunningCompaction(draft.sessionID);
|
|
77425
|
+
if (nativeFoldReplay) {
|
|
77426
|
+
try {
|
|
77427
|
+
if (await nativeFoldReplay.canReplay(draft.sessionID, reader)) {
|
|
77428
|
+
const state = getOrCreateSessionMeta(storage.require(), draft.sessionID);
|
|
77429
|
+
const summary = await nativeFoldReplay.baseline(draft.sessionID) ?? state.cachedM0Bytes?.toString("utf8") ?? "<session-history></session-history>";
|
|
77430
|
+
if (await nativeFoldReplay.supply({
|
|
77431
|
+
draft,
|
|
77432
|
+
reader,
|
|
77433
|
+
previousCut: reader.latestCompaction(draft.sessionID),
|
|
77434
|
+
summary
|
|
77435
|
+
})) {
|
|
77436
|
+
draft.result = { summary };
|
|
77437
|
+
return;
|
|
77438
|
+
}
|
|
77439
|
+
}
|
|
77440
|
+
} catch (error) {
|
|
77441
|
+
nativeFoldReplay.invalidate(draft.sessionID, true);
|
|
77442
|
+
sessionLog(draft.sessionID, `v2 host fold: legacy compaction reason=${JSON.stringify(String(error))}`);
|
|
77443
|
+
}
|
|
77444
|
+
}
|
|
76325
77445
|
const fold = await folds.supply({
|
|
76326
77446
|
sessionID: draft.sessionID,
|
|
76327
77447
|
watermark,
|
|
@@ -76368,12 +77488,88 @@ async function registerContext(context) {
|
|
|
76368
77488
|
withoutSqliteTransformPass(() => warmModelLimitCacheFromCatalog(context));
|
|
76369
77489
|
agents.set(draft.sessionID, draft.agent);
|
|
76370
77490
|
applyV2PromptSurfaceTools(draft, promptSurfaceRuntime, config.prompt_surface);
|
|
77491
|
+
const admissionDb = db ?? storage.current();
|
|
77492
|
+
const replayFits = (messages) => {
|
|
77493
|
+
const system = structuredClone(draft.system);
|
|
77494
|
+
if (!admissionDb || !draft.tools || !lkgSystems.restore(draft.sessionID, getSlot(draft.sessionID), draft.system, system))
|
|
77495
|
+
return false;
|
|
77496
|
+
const limit = lkgReplayLimit({
|
|
77497
|
+
db: admissionDb,
|
|
77498
|
+
sessionId: draft.sessionID,
|
|
77499
|
+
model: {
|
|
77500
|
+
providerID: draft.model.providerID,
|
|
77501
|
+
modelID: draft.model.id
|
|
77502
|
+
},
|
|
77503
|
+
modelKey: `${draft.model.providerID}/${draft.model.id}`
|
|
77504
|
+
});
|
|
77505
|
+
if (limit === undefined)
|
|
77506
|
+
return false;
|
|
77507
|
+
let toolDefinitionTokens = 0;
|
|
77508
|
+
for (const tool of Object.values(draft.tools)) {
|
|
77509
|
+
if (!tool || typeof tool.description !== "string" || !tool.input)
|
|
77510
|
+
return false;
|
|
77511
|
+
const schema = JSON.stringify(tool.input);
|
|
77512
|
+
if (!schema)
|
|
77513
|
+
return false;
|
|
77514
|
+
toolDefinitionTokens += estimateTokens(tool.description) + estimateTokens(schema);
|
|
77515
|
+
}
|
|
77516
|
+
return measureLkgReplay({
|
|
77517
|
+
messages,
|
|
77518
|
+
limit,
|
|
77519
|
+
estimate: () => estimateFinalWireInputTokens({
|
|
77520
|
+
messages,
|
|
77521
|
+
toolDefinitionTokens,
|
|
77522
|
+
systemPromptTokens: estimateTokens(JSON.stringify(system)),
|
|
77523
|
+
providerID: draft.model.providerID,
|
|
77524
|
+
modelID: draft.model.id,
|
|
77525
|
+
agentName: draft.agent
|
|
77526
|
+
})
|
|
77527
|
+
}).fit === "under";
|
|
77528
|
+
};
|
|
76371
77529
|
let postFold = false;
|
|
76372
77530
|
try {
|
|
76373
|
-
const admissionDb = db ?? storage.current();
|
|
76374
77531
|
if (!compactionOff && admissionDb)
|
|
76375
77532
|
await withAsyncPrivilegedWriter(admissionDb, () => {
|
|
76376
77533
|
return;
|
|
77534
|
+
}, {
|
|
77535
|
+
beforeRetry: (_error) => {
|
|
77536
|
+
let replayBlocked = false;
|
|
77537
|
+
const mapped = adaptPayload(draft);
|
|
77538
|
+
try {
|
|
77539
|
+
const overflowDb = db;
|
|
77540
|
+
if (overflowDb !== undefined && overflowDb !== null && (isEmergencyRecoveryArmed(draft.sessionID) || getOverflowState(overflowDb, draft.sessionID).needsEmergencyRecovery)) {
|
|
77541
|
+
replayBlocked = true;
|
|
77542
|
+
sessionLog(draft.sessionID, "lkg_emergency_armed");
|
|
77543
|
+
} else {
|
|
77544
|
+
const keys = resolveLkgModelKeys(mapped.messages);
|
|
77545
|
+
const replay = replayLkg({
|
|
77546
|
+
sessionId: draft.sessionID,
|
|
77547
|
+
messages: mapped.messages,
|
|
77548
|
+
modelKey: keys.modelKey,
|
|
77549
|
+
providerKey: keys.providerKey
|
|
77550
|
+
});
|
|
77551
|
+
if (replay.ok && !replayFits(replay.messages)) {
|
|
77552
|
+
replayBlocked = true;
|
|
77553
|
+
sessionLog(draft.sessionID, "lkg_replay_does_not_fit");
|
|
77554
|
+
} else if (replay.ok) {
|
|
77555
|
+
restoreLkgSystem();
|
|
77556
|
+
mapped.messages.splice(0, mapped.messages.length, ...replay.messages);
|
|
77557
|
+
mapped.commit();
|
|
77558
|
+
sessionLog(draft.sessionID, "lkg_replay_served");
|
|
77559
|
+
throw new V2LkgAdmissionReplay;
|
|
77560
|
+
} else {
|
|
77561
|
+
sessionLog(draft.sessionID, replay.reason);
|
|
77562
|
+
}
|
|
77563
|
+
}
|
|
77564
|
+
} catch (replayError) {
|
|
77565
|
+
if (replayError instanceof V2LkgAdmissionReplay)
|
|
77566
|
+
throw replayError;
|
|
77567
|
+
replayBlocked = true;
|
|
77568
|
+
sessionLog(draft.sessionID, "lkg_replay_unavailable", replayError);
|
|
77569
|
+
}
|
|
77570
|
+
if (replayBlocked)
|
|
77571
|
+
sessionLog(draft.sessionID, "lkg_replay_declined");
|
|
77572
|
+
}
|
|
76377
77573
|
});
|
|
76378
77574
|
if (admissionDb)
|
|
76379
77575
|
hideSubagentTools(draft, admissionDb);
|
|
@@ -76528,7 +77724,28 @@ async function registerContext(context) {
|
|
|
76528
77724
|
postFold = cut !== undefined;
|
|
76529
77725
|
if (cut && !incoming)
|
|
76530
77726
|
throw new Error("The host checkpoint disappeared from the context draft");
|
|
76531
|
-
|
|
77727
|
+
let nativeRestored;
|
|
77728
|
+
if (cut && nativeFoldReplay && isLocalCheckpoint(cut)) {
|
|
77729
|
+
try {
|
|
77730
|
+
nativeRestored = await nativeFoldReplay.restore(draft.sessionID, cut, `${draft.model.providerID}/${draft.model.id}`, { bounded: true });
|
|
77731
|
+
} catch (error) {
|
|
77732
|
+
nativeFoldReplay.invalidate(draft.sessionID, true);
|
|
77733
|
+
sessionLog(draft.sessionID, "v2 host fold: cache unavailable; restoring host rows", error);
|
|
77734
|
+
nativeRestored = await nativeFoldReplay.restoreRead(draft.sessionID, cut, draft.model);
|
|
77735
|
+
}
|
|
77736
|
+
}
|
|
77737
|
+
if (nativeRestored && nativeFoldReplay?.sourceChanged(draft.sessionID)) {
|
|
77738
|
+
historyRefreshSessions.add(draft.sessionID);
|
|
77739
|
+
pendingMaterializationSessions.add(draft.sessionID);
|
|
77740
|
+
}
|
|
77741
|
+
if (cut && incoming && nativeRestored) {
|
|
77742
|
+
const present = new Set(draft.messages.flatMap((message) => message.id ? [message.id] : []));
|
|
77743
|
+
const hidden = nativeRestored.filter((message) => {
|
|
77744
|
+
const id = nativeRowID(message);
|
|
77745
|
+
return !id || !present.has(id);
|
|
77746
|
+
});
|
|
77747
|
+
draft.messages.splice(0, draft.messages.length, ...hidden, ...draft.messages.filter((message) => message !== incoming));
|
|
77748
|
+
} else if (cut && incoming) {
|
|
76532
77749
|
const identity = await folds.observe({
|
|
76533
77750
|
sessionID: draft.sessionID,
|
|
76534
77751
|
cutSeq: cut.seq,
|
|
@@ -76555,6 +77772,14 @@ async function registerContext(context) {
|
|
|
76555
77772
|
} finally {
|
|
76556
77773
|
reader.close();
|
|
76557
77774
|
}
|
|
77775
|
+
let nativeMessages;
|
|
77776
|
+
if (nativeFoldReplay) {
|
|
77777
|
+
try {
|
|
77778
|
+
nativeMessages = copyNativeInput(draft.messages);
|
|
77779
|
+
} catch {
|
|
77780
|
+
nativeFoldReplay.invalidate(draft.sessionID, true);
|
|
77781
|
+
}
|
|
77782
|
+
}
|
|
76558
77783
|
const mapped = adaptPayload(draft, admitted);
|
|
76559
77784
|
beginV2LkgRequest(draft.sessionID, `${draft.model.providerID}/${draft.model.id}`, latestLkgResponseIds.get(draft.sessionID));
|
|
76560
77785
|
await transform({}, mapped);
|
|
@@ -76580,6 +77805,14 @@ ${baseline}
|
|
|
76580
77805
|
draft.messages.splice(0, draft.messages.length, checkpoint, ...draft.messages.filter((message) => message !== head));
|
|
76581
77806
|
}
|
|
76582
77807
|
}
|
|
77808
|
+
if (nativeMessages && !checkpoint) {
|
|
77809
|
+
try {
|
|
77810
|
+
await nativeFoldReplay?.capture(draft, nativeMessages);
|
|
77811
|
+
} catch (error) {
|
|
77812
|
+
nativeFoldReplay?.invalidate(draft.sessionID, true);
|
|
77813
|
+
sessionLog(draft.sessionID, "v2 host fold: optional capture unavailable", error);
|
|
77814
|
+
}
|
|
77815
|
+
}
|
|
76583
77816
|
const capturedSlot = getSlot(draft.sessionID);
|
|
76584
77817
|
if (capturedSlot && (!slotAtEntry || capturedSlot.capturedAt !== slotAtEntry.capturedAt || capturedSlot.captureSequence !== slotAtEntry.captureSequence || capturedSlot.jsonPrefix !== slotAtEntry.jsonPrefix)) {
|
|
76585
77818
|
lkgSystems.capture(draft.sessionID, capturedSlot, systemAtEntry, draft.system);
|
|
@@ -76587,6 +77820,8 @@ ${baseline}
|
|
|
76587
77820
|
} catch (error) {
|
|
76588
77821
|
if (error instanceof V2ContextRefusal)
|
|
76589
77822
|
throw error;
|
|
77823
|
+
if (error instanceof V2LkgAdmissionReplay)
|
|
77824
|
+
return;
|
|
76590
77825
|
if (!compactionOff && (isTransientSqliteError(error) || error instanceof StorageBusyRefusalError)) {
|
|
76591
77826
|
if (isTransientSqliteError(error)) {
|
|
76592
77827
|
try {
|
|
@@ -76614,13 +77849,16 @@ ${baseline}
|
|
|
76614
77849
|
modelKey: keys.modelKey,
|
|
76615
77850
|
providerKey: keys.providerKey
|
|
76616
77851
|
});
|
|
76617
|
-
if (replay.ok) {
|
|
77852
|
+
if (replay.ok && !replayFits(replay.messages)) {
|
|
77853
|
+
sessionLog(draft.sessionID, "lkg_replay_does_not_fit");
|
|
77854
|
+
} else if (replay.ok) {
|
|
76618
77855
|
restoreLkgSystem();
|
|
76619
77856
|
replayMapped.messages.splice(0, replayMapped.messages.length, ...replay.messages);
|
|
76620
77857
|
replayMapped.commit();
|
|
76621
77858
|
return;
|
|
77859
|
+
} else {
|
|
77860
|
+
sessionLog(draft.sessionID, replay.reason);
|
|
76622
77861
|
}
|
|
76623
|
-
sessionLog(draft.sessionID, replay.reason);
|
|
76624
77862
|
}
|
|
76625
77863
|
} catch (replayError) {
|
|
76626
77864
|
replayBlocked = true;
|
|
@@ -76774,7 +78012,7 @@ ${baseline}
|
|
|
76774
78012
|
}
|
|
76775
78013
|
};
|
|
76776
78014
|
}
|
|
76777
|
-
var HIDDEN_SESSION_ERROR_GRACE_MS = 50;
|
|
78015
|
+
var HIDDEN_SESSION_ERROR_GRACE_MS = 50, V2LkgAdmissionReplay;
|
|
76778
78016
|
var init_context = __esm(async () => {
|
|
76779
78017
|
init_memory_maintainer();
|
|
76780
78018
|
init_config();
|
|
@@ -76789,6 +78027,7 @@ var init_context = __esm(async () => {
|
|
|
76789
78027
|
init_derive_budgets();
|
|
76790
78028
|
init_dropped_input_guard();
|
|
76791
78029
|
init_emergency_fail_closed();
|
|
78030
|
+
init_final_wire_token_estimate();
|
|
76792
78031
|
init_lkg_measured_request();
|
|
76793
78032
|
init_lkg_persist();
|
|
76794
78033
|
init_lkg_replay();
|
|
@@ -76806,6 +78045,7 @@ var init_context = __esm(async () => {
|
|
|
76806
78045
|
init_parameter_descriptions();
|
|
76807
78046
|
init_host_media();
|
|
76808
78047
|
init_markers();
|
|
78048
|
+
init_memory_cache();
|
|
76809
78049
|
init_owner();
|
|
76810
78050
|
init_restore();
|
|
76811
78051
|
init_hidden_child_cleanup();
|
|
@@ -76835,6 +78075,7 @@ var init_context = __esm(async () => {
|
|
|
76835
78075
|
init_history_boundary_repair(),
|
|
76836
78076
|
init_hook_handlers(),
|
|
76837
78077
|
init_inject_compartments(),
|
|
78078
|
+
init_lkg_replay_fit(),
|
|
76838
78079
|
init_protected_tail_boundary(),
|
|
76839
78080
|
init_read_session_chunk(),
|
|
76840
78081
|
init_storage_busy_refusal(),
|
|
@@ -76843,6 +78084,8 @@ var init_context = __esm(async () => {
|
|
|
76843
78084
|
init_rpc_handlers(),
|
|
76844
78085
|
init_subagent_tool_policy(),
|
|
76845
78086
|
init_sqlite(),
|
|
78087
|
+
init_native_replay(),
|
|
78088
|
+
init_policy(),
|
|
76846
78089
|
init_fork_inheritance(),
|
|
76847
78090
|
init_store_reader(),
|
|
76848
78091
|
init_channel2(),
|
|
@@ -76851,6 +78094,8 @@ var init_context = __esm(async () => {
|
|
|
76851
78094
|
init_tools6(),
|
|
76852
78095
|
init_usage_persist()
|
|
76853
78096
|
]);
|
|
78097
|
+
V2LkgAdmissionReplay = class V2LkgAdmissionReplay extends Error {
|
|
78098
|
+
};
|
|
76854
78099
|
});
|
|
76855
78100
|
|
|
76856
78101
|
// src/v2/server.ts
|