@tangle-network/agent-runtime 0.350.2 → 0.353.2
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/{activation-CfJMyzEs.js → activation-BkRaG5t0.js} +4 -4
- package/dist/{activation-CfJMyzEs.js.map → activation-BkRaG5t0.js.map} +1 -1
- package/dist/{activation-DyiHrTlS.d.ts → activation-CFS31SWI.d.ts} +2 -2
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +3 -3
- package/dist/{bundle-Ze8pd07a.js → bundle-DeDkJbsr.js} +2 -2
- package/dist/{bundle-Ze8pd07a.js.map → bundle-DeDkJbsr.js.map} +1 -1
- package/dist/candidate-execution/index.js +4 -4
- package/dist/chat-turn.d.ts +2 -0
- package/dist/chat-turn.js +1 -0
- package/dist/{coordination-manager-BkVfWlMz.js → coordination-manager-BrRkc55z.js} +4 -4
- package/dist/{coordination-manager-BkVfWlMz.js.map → coordination-manager-BrRkc55z.js.map} +1 -1
- package/dist/{delegate-DgEie4ad.js → delegate-CAdajnf8.js} +3 -3
- package/dist/{delegate-DgEie4ad.js.map → delegate-CAdajnf8.js.map} +1 -1
- package/dist/durable.d.ts +7 -130
- package/dist/durable.js +24 -952
- package/dist/durable.js.map +1 -1
- package/dist/{errors-wuhGvpE3.js → errors-AzSuGI23.js} +4 -1
- package/dist/errors-AzSuGI23.js.map +1 -0
- package/dist/execution-handle-Bd2_nUB6.js +167 -0
- package/dist/execution-handle-Bd2_nUB6.js.map +1 -0
- package/dist/execution-handle-CujvqgCn.d.ts +130 -0
- package/dist/{graph-Cx0FhKSE.js → graph-CyaIHVGR.js} +7 -6
- package/dist/{graph-Cx0FhKSE.js.map → graph-CyaIHVGR.js.map} +1 -1
- package/dist/{improve-i0HU1-aa.js → improve-DwG-yioQ.js} +10 -9
- package/dist/{improve-i0HU1-aa.js.map → improve-DwG-yioQ.js.map} +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +14 -11
- package/dist/index.js.map +1 -1
- package/dist/intelligence.d.ts +2 -2
- package/dist/intelligence.js +12 -10
- package/dist/intelligence.js.map +1 -1
- package/dist/journal-store-BJPMUydp.js +3925 -0
- package/dist/journal-store-BJPMUydp.js.map +1 -0
- package/dist/kernel.d.ts +1 -1
- package/dist/kernel.js +11 -10
- package/dist/{loop-runner-DvfOXVx9.d.ts → loop-runner-D9rcTNf2.d.ts} +2 -2
- package/dist/{loop-runner-BwN9MhdP.js → loop-runner-DwS3jARE.js} +3 -3
- package/dist/{loop-runner-BwN9MhdP.js.map → loop-runner-DwS3jARE.js.map} +1 -1
- package/dist/loop-runner-bin.d.ts +1 -1
- package/dist/loop-runner-bin.js +1 -1
- package/dist/mcp/bin.js +4 -4
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +8 -8
- package/dist/mcp/memory-bin.js +2 -2
- package/dist/{model-policy-GSxWwP-b.js → model-policy-C-RGAUW4.js} +2 -2
- package/dist/{model-policy-GSxWwP-b.js.map → model-policy-C-RGAUW4.js.map} +1 -1
- package/dist/{prepare-DE8TQnLu.js → prepare-XWwsOh8O.js} +4 -3
- package/dist/{prepare-DE8TQnLu.js.map → prepare-XWwsOh8O.js.map} +1 -1
- package/dist/{profile-CAhZDW2T.js → profile-C01SQZKa.js} +3 -3
- package/dist/{profile-CAhZDW2T.js.map → profile-C01SQZKa.js.map} +1 -1
- package/dist/profiles.d.ts +1 -1
- package/dist/profiles.js +2 -2
- package/dist/{provision-supervisor-Bde_FF0R.js → provision-supervisor-AEnei2Jq.js} +7 -7
- package/dist/{provision-supervisor-Bde_FF0R.js.map → provision-supervisor-AEnei2Jq.js.map} +1 -1
- package/dist/{run-cancellation-BLy_Zz-w.js → run-cancellation-CoJRKvih.js} +2 -2
- package/dist/{run-cancellation-BLy_Zz-w.js.map → run-cancellation-CoJRKvih.js.map} +1 -1
- package/dist/{runtime-6Ec9sHRe.js → runtime-DCYSaYHW.js} +42 -39
- package/dist/{runtime-6Ec9sHRe.js.map → runtime-DCYSaYHW.js.map} +1 -1
- package/dist/{sandbox-profile-compat-BxMQ1VXv.js → sandbox-profile-compat-CXjozdWL.js} +4 -623
- package/dist/{sandbox-profile-compat-BxMQ1VXv.js.map → sandbox-profile-compat-CXjozdWL.js.map} +1 -1
- package/dist/{search-executor-B7g_5Wjl.js → search-executor-B2jc63hZ.js} +14 -11
- package/dist/{search-executor-B7g_5Wjl.js.map → search-executor-B2jc63hZ.js.map} +1 -1
- package/dist/{server-fvEMwGMQ.js → server-CKBTwWM8.js} +6 -6
- package/dist/{server-fvEMwGMQ.js.map → server-CKBTwWM8.js.map} +1 -1
- package/dist/{structural-rollout-B86bSixU.js → structural-rollout-rRT3ypqz.js} +7 -5
- package/dist/{structural-rollout-B86bSixU.js.map → structural-rollout-rRT3ypqz.js.map} +1 -1
- package/dist/{supervise-CaeZcgAW.js → supervise-B3_wtgHS.js} +18 -21
- package/dist/supervise-B3_wtgHS.js.map +1 -0
- package/dist/supervision-discovery-CewADdEs.js +772 -0
- package/dist/supervision-discovery-CewADdEs.js.map +1 -0
- package/dist/{supervisor-C1OePYE-.js → supervisor-C_sDmX5H.js} +156 -3984
- package/dist/supervisor-C_sDmX5H.js.map +1 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +13 -13
- package/dist/{tool-server-CwuYeW_Z.js → tool-server-CZWd_Opp.js} +2 -2
- package/dist/{tool-server-CwuYeW_Z.js.map → tool-server-CZWd_Opp.js.map} +1 -1
- package/dist/{top-snapshot-ChwpTr7A.js → top-snapshot-DcV7VG4m.js} +178 -161
- package/dist/top-snapshot-DcV7VG4m.js.map +1 -0
- package/dist/tui/bin.js +45 -74
- package/dist/tui/bin.js.map +1 -1
- package/dist/tui/index.d.ts +25 -54
- package/dist/tui/index.js +2 -2
- package/dist/{types-Fowuwdz2.d.ts → types-DTHxh4X9.d.ts} +47 -63
- package/dist/util-ebWMWzY0.js +624 -0
- package/dist/util-ebWMWzY0.js.map +1 -0
- package/dist/workspace-store/index.js +9 -3
- package/dist/workspace-store/index.js.map +1 -1
- package/package.json +18 -13
- package/dist/errors-wuhGvpE3.js.map +0 -1
- package/dist/record-events-B2GUgYdz.js +0 -261
- package/dist/record-events-B2GUgYdz.js.map +0 -1
- package/dist/supervise-CaeZcgAW.js.map +0 -1
- package/dist/supervisor-C1OePYE-.js.map +0 -1
- package/dist/top-snapshot-ChwpTr7A.js.map +0 -1
|
@@ -0,0 +1,3925 @@
|
|
|
1
|
+
import { m as ValidationError, r as BackendTransportError } from "./errors-AzSuGI23.js";
|
|
2
|
+
import { D as assertResources, E as addResourceSpend, T as zeroTokenUsage, i as chargedTokens, k as withBudgetResources, l as fnv1a32Hex, m as promptCacheTokenClasses, o as cloneTokenUsage, r as addTokenUsage, u as hasCompleteCacheBreakdown } from "./util-ebWMWzY0.js";
|
|
3
|
+
import { AgentTurnResultSchema, CanonicalStreamEventSchema, RuntimeEventEnvelopeSchema } from "@tangle-network/agent-interface";
|
|
4
|
+
import { isRecord } from "@tangle-network/agent-core";
|
|
5
|
+
import { collectAgentFinalMessageText, collectAgentResponseText } from "@tangle-network/sandbox";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { REDACTION_VERSION, redact } from "@tangle-network/agent-eval/traces";
|
|
8
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
9
|
+
import { LedgerCanonicalizationError, canonicalString, jsonDocument, merkleLeafHash, merkleRoot, merkleSalt, tryAcquireAtomicFileLock } from "@tangle-network/agent-eval/ledger-core";
|
|
10
|
+
import { closeSync, constants, existsSync, fchmodSync, fstatSync, fsyncSync, ftruncateSync, linkSync, lstatSync, openSync, readFileSync, readSync, renameSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
|
|
11
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
12
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
13
|
+
// \0rolldown/runtime.js
|
|
14
|
+
var __defProp = Object.defineProperty;
|
|
15
|
+
var __exportAll = (all, no_symbols) => {
|
|
16
|
+
let target = {};
|
|
17
|
+
for (var name in all) __defProp(target, name, {
|
|
18
|
+
get: all[name],
|
|
19
|
+
enumerable: true
|
|
20
|
+
});
|
|
21
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
22
|
+
return target;
|
|
23
|
+
};
|
|
24
|
+
// src/record-value.ts
|
|
25
|
+
function recordValue(value) {
|
|
26
|
+
return isRecord(value) ? value : void 0;
|
|
27
|
+
}
|
|
28
|
+
// src/runtime/harness-usage.ts
|
|
29
|
+
/**
|
|
30
|
+
* Read and validate one codex `turn.completed` usage record.
|
|
31
|
+
*
|
|
32
|
+
* Every counter must be a non-negative safe integer. Two cross-field invariants hold, and they are
|
|
33
|
+
* what states that the two named counters are SUBSETS of their totals rather than additions to
|
|
34
|
+
* them: `cached_input_tokens <= input_tokens` and `reasoning_output_tokens <= output_tokens`.
|
|
35
|
+
* Measured against the codex CLI: a turn reporting `output_tokens 1523` with
|
|
36
|
+
* `reasoning_output_tokens 1516` answered with about seven tokens of text.
|
|
37
|
+
*
|
|
38
|
+
* `label` names the surface the record came from, so one message serves both readers.
|
|
39
|
+
* Throws `ValidationError` on any violation.
|
|
40
|
+
*/
|
|
41
|
+
function parseCodexUsageRecord(usage, label) {
|
|
42
|
+
const record = recordValue(usage);
|
|
43
|
+
if (record === void 0) throw new ValidationError(`${label}: usage must be an object, received ${describe(usage)}`);
|
|
44
|
+
const inputTokens = naturalNumber(record.input_tokens, "input_tokens", label);
|
|
45
|
+
const cachedInputTokens = naturalNumber(record.cached_input_tokens, "cached_input_tokens", label);
|
|
46
|
+
const outputTokens = naturalNumber(record.output_tokens, "output_tokens", label);
|
|
47
|
+
const reasoningOutputTokens = naturalNumber(record.reasoning_output_tokens, "reasoning_output_tokens", label);
|
|
48
|
+
const cacheWriteInputTokens = record.cache_write_input_tokens === void 0 ? void 0 : naturalNumber(record.cache_write_input_tokens, "cache_write_input_tokens", label);
|
|
49
|
+
if (cachedInputTokens > inputTokens) throw new ValidationError(`${label}: cached_input_tokens exceeds input_tokens`);
|
|
50
|
+
if (reasoningOutputTokens > outputTokens) throw new ValidationError(`${label}: reasoning_output_tokens exceeds output_tokens`);
|
|
51
|
+
return {
|
|
52
|
+
inputTokens,
|
|
53
|
+
cachedInputTokens,
|
|
54
|
+
outputTokens,
|
|
55
|
+
reasoningOutputTokens,
|
|
56
|
+
...cacheWriteInputTokens === void 0 ? {} : { cacheWriteInputTokens }
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Names the wire record every codex decode error is reported against. */
|
|
60
|
+
const codexUsageContext = "codex turn.completed";
|
|
61
|
+
/**
|
|
62
|
+
* codex reports the tokens of a turn inside its own `turn.completed` event, which rides the
|
|
63
|
+
* transport either as `{ type: 'raw', data: { type: 'turn.completed', usage: { … } } }` or as
|
|
64
|
+
* the provider-normalized Interface envelope `{ type: 'raw', data: { type: 'raw', event: {
|
|
65
|
+
* type: 'turn.completed', usage: { … } } } }`. It emits no canonical usage event, so this adapter
|
|
66
|
+
* is a codex worker's only usage source.
|
|
67
|
+
*
|
|
68
|
+
* The record is read by `parseCodexUsageRecord`, the same reader `runLocalHarness` uses on the
|
|
69
|
+
* codex CLI's own stdout, so both surfaces hold one field policy and both cross-field invariants.
|
|
70
|
+
*
|
|
71
|
+
* A `turn.completed` that carries a `usage` member the reader cannot read is a `ValidationError`
|
|
72
|
+
* naming the field. A `turn.completed` with no `usage` member reports no usage at all, which
|
|
73
|
+
* leaves the turn's tokens unknown rather than zero.
|
|
74
|
+
*/
|
|
75
|
+
const decodeCodexTurnUsage = (event) => {
|
|
76
|
+
if (String(event.type ?? "") !== "raw") return void 0;
|
|
77
|
+
const envelope = recordValue(event.data);
|
|
78
|
+
const data = envelope?.type === "raw" && recordValue(envelope.event) !== void 0 ? recordValue(envelope.event) : envelope;
|
|
79
|
+
if (data === void 0 || data.type !== "turn.completed" || data.usage === void 0) return;
|
|
80
|
+
const record = parseCodexUsageRecord(data.usage, codexUsageContext);
|
|
81
|
+
return {
|
|
82
|
+
harness: "codex",
|
|
83
|
+
input: record.inputTokens,
|
|
84
|
+
output: record.outputTokens,
|
|
85
|
+
cachedInput: record.cachedInputTokens,
|
|
86
|
+
reasoningOutput: record.reasoningOutputTokens,
|
|
87
|
+
...record.cacheWriteInputTokens === void 0 ? {} : { cacheWriteInput: record.cacheWriteInputTokens }
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
/** Claude's adapter forwards one delta per response snapshot, keyed and deduplicated there. */
|
|
91
|
+
const decodeClaudeAssistantUsage = (event) => {
|
|
92
|
+
if (String(event.type ?? "") !== "raw") return void 0;
|
|
93
|
+
const envelope = recordValue(event.data);
|
|
94
|
+
const data = envelope?.type === "raw" && recordValue(envelope.event) !== void 0 ? recordValue(envelope.event) : envelope;
|
|
95
|
+
if (data?.type !== "assistant.usage") return void 0;
|
|
96
|
+
if (typeof data.message_id !== "string" || data.message_id.length === 0) throw new ValidationError("claude assistant.usage: missing response identity");
|
|
97
|
+
const usage = recordValue(data.usage);
|
|
98
|
+
if (!usage) throw new ValidationError("claude assistant.usage: usage must be an object");
|
|
99
|
+
const fresh = naturalNumber(usage.input_tokens, "input_tokens", "claude assistant.usage");
|
|
100
|
+
const cachedInput = naturalNumber(usage.cache_read_input_tokens, "cache_read_input_tokens", "claude assistant.usage");
|
|
101
|
+
const cacheWriteInput = naturalNumber(usage.cache_creation_input_tokens, "cache_creation_input_tokens", "claude assistant.usage");
|
|
102
|
+
const output = naturalNumber(usage.output_tokens, "output_tokens", "claude assistant.usage");
|
|
103
|
+
const input = fresh + cachedInput + cacheWriteInput;
|
|
104
|
+
if (!Number.isSafeInteger(input)) throw new ValidationError("claude assistant.usage: total input tokens exceed a safe integer");
|
|
105
|
+
return {
|
|
106
|
+
harness: "claude-code",
|
|
107
|
+
input,
|
|
108
|
+
output,
|
|
109
|
+
cachedInput,
|
|
110
|
+
cacheWriteInput
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* The harness → usage-decoder registry. Add a harness by adding one entry.
|
|
115
|
+
*
|
|
116
|
+
* Keyed by `HarnessType`, the vocabulary the `harness` argument is drawn from, so a key no caller
|
|
117
|
+
* can produce does not compile. A harness absent from this registry reports usage through the
|
|
118
|
+
* canonical events or not at all.
|
|
119
|
+
*/
|
|
120
|
+
const harnessUsageDecoders = {
|
|
121
|
+
codex: decodeCodexTurnUsage,
|
|
122
|
+
"claude-code": decodeClaudeAssistantUsage
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Decode a sandbox event with one harness's adapter, or `undefined` when the event carries no
|
|
126
|
+
* harness-native usage.
|
|
127
|
+
*
|
|
128
|
+
* A NAMED harness reads with that harness's adapter only, and a named harness with no adapter
|
|
129
|
+
* reports nothing. It never falls through to another harness's adapter: a different harness's
|
|
130
|
+
* `turn.completed` decoded as codex would either drop the counters codex does not name or fail on
|
|
131
|
+
* a field codex requires, and both answers would be about the wrong harness. The composite over
|
|
132
|
+
* every registered adapter runs only when the caller cannot name the harness.
|
|
133
|
+
*
|
|
134
|
+
* Throws `ValidationError` when an adapter recognizes the event as its harness's usage carrier and
|
|
135
|
+
* cannot read the numbers.
|
|
136
|
+
*/
|
|
137
|
+
function decodeHarnessUsage(event, harness) {
|
|
138
|
+
if (!event || typeof event !== "object") return void 0;
|
|
139
|
+
if (harness !== void 0) return harnessUsageDecoders[harness]?.(event);
|
|
140
|
+
for (const decode of new Set(Object.values(harnessUsageDecoders))) {
|
|
141
|
+
const usage = decode(event);
|
|
142
|
+
if (usage !== void 0) return usage;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function naturalNumber(value, field, label) {
|
|
146
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw new ValidationError(`${label}: usage.${field} must be a non-negative safe integer, received ${describe(value)}`);
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
149
|
+
function describe(value) {
|
|
150
|
+
if (value === null) return "null";
|
|
151
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
152
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
153
|
+
if (Array.isArray(value)) return "an array";
|
|
154
|
+
return typeof value;
|
|
155
|
+
}
|
|
156
|
+
// src/runtime/sandbox-events.ts
|
|
157
|
+
/**
|
|
158
|
+
* Forward a sandbox event to an optional observer without letting observer
|
|
159
|
+
* behavior affect the run. The observer receives a defensive copy, synchronous
|
|
160
|
+
* throws are swallowed, and returned promises are deliberately not awaited.
|
|
161
|
+
*/
|
|
162
|
+
function notifySandboxEventObserver(event, observer, meta) {
|
|
163
|
+
if (!observer) return;
|
|
164
|
+
try {
|
|
165
|
+
const result = observer(cloneEventForObserver(event), meta);
|
|
166
|
+
if (result && typeof result.then === "function") result.then(void 0, () => {});
|
|
167
|
+
} catch {}
|
|
168
|
+
}
|
|
169
|
+
function cloneEventForObserver(event) {
|
|
170
|
+
try {
|
|
171
|
+
return structuredClone(event);
|
|
172
|
+
} catch {
|
|
173
|
+
return copyPlainSpine(event, /* @__PURE__ */ new WeakMap());
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function copyPlainSpine(value, seen) {
|
|
177
|
+
if (value === null || typeof value !== "object") return value;
|
|
178
|
+
const existing = seen.get(value);
|
|
179
|
+
if (existing !== void 0) return existing;
|
|
180
|
+
if (Array.isArray(value)) {
|
|
181
|
+
const copy = [];
|
|
182
|
+
seen.set(value, copy);
|
|
183
|
+
for (const item of value) copy.push(copyPlainSpine(item, seen));
|
|
184
|
+
return copy;
|
|
185
|
+
}
|
|
186
|
+
const proto = Object.getPrototypeOf(value);
|
|
187
|
+
if (proto !== Object.prototype && proto !== null) return {};
|
|
188
|
+
const copy = {};
|
|
189
|
+
seen.set(value, copy);
|
|
190
|
+
for (const [key, child] of Object.entries(value)) copy[key] = copyPlainSpine(child, seen);
|
|
191
|
+
return copy;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Read the served execution identity off one Sandbox event.
|
|
195
|
+
*
|
|
196
|
+
* The platform reports `effectiveBackend` on `execution.started` and again on the terminal
|
|
197
|
+
* event (`@tangle-network/sandbox`, `EffectiveBackend`). Absence returns `undefined`
|
|
198
|
+
* and must stay unknown — a request is not a receipt, so nothing here may be inferred from
|
|
199
|
+
* what was asked for.
|
|
200
|
+
*/
|
|
201
|
+
function sandboxEventServedBackend(event) {
|
|
202
|
+
if (!event || typeof event !== "object") return void 0;
|
|
203
|
+
const served = recordValue(recordValue(event.data)?.effectiveBackend);
|
|
204
|
+
if (!served) return void 0;
|
|
205
|
+
const provider = firstString(served.provider);
|
|
206
|
+
const model = firstString(served.model);
|
|
207
|
+
const source = firstString(served.source);
|
|
208
|
+
if (provider === void 0 && model === void 0) return void 0;
|
|
209
|
+
return {
|
|
210
|
+
...provider !== void 0 ? { provider } : {},
|
|
211
|
+
...model !== void 0 ? { model } : {},
|
|
212
|
+
...source !== void 0 ? { source } : {}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Fail the execution when the platform reports serving a model other than the exact one asked for.
|
|
217
|
+
*
|
|
218
|
+
* Measured motive (agent-runtime#892, live infrastructure): 6 of 6 boxes whose profile
|
|
219
|
+
* declared `zai-coding-plan/glm-5.2` reported
|
|
220
|
+
* `{"provider":"openai-compat","model":"deepseek/deepseek-v4-flash","source":"environment"}`,
|
|
221
|
+
* while the materialization receipt recorded the declared model as `status: "known"`. Sending
|
|
222
|
+
* `backend.model` makes that substitution unlikely; only reading the report back makes it
|
|
223
|
+
* detectable. A run that cannot say which model produced its evidence must not settle as one
|
|
224
|
+
* that can.
|
|
225
|
+
*
|
|
226
|
+
* Silent when the platform reports no served model: unobserved stays unobserved.
|
|
227
|
+
*/
|
|
228
|
+
function assertSandboxServedModel(event, expected) {
|
|
229
|
+
const wanted = expected?.model?.trim();
|
|
230
|
+
if (!wanted) return;
|
|
231
|
+
const served = sandboxEventServedBackend(event);
|
|
232
|
+
if (served?.model === void 0) return;
|
|
233
|
+
if (sameModelId(served.model, wanted, [served.provider, expected?.provider])) return;
|
|
234
|
+
const attribution = [served.provider !== void 0 ? `provider ${JSON.stringify(served.provider)}` : void 0, served.source !== void 0 ? `source ${JSON.stringify(served.source)}` : void 0].filter((part) => part !== void 0);
|
|
235
|
+
const detail = attribution.length > 0 ? ` (${attribution.join(", ")})` : "";
|
|
236
|
+
throw new Error(`sandbox served model ${JSON.stringify(served.model)}${detail} instead of the exact profile model ${JSON.stringify(wanted)}`);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Do two model ids name the same model, allowing for routing prefixes?
|
|
240
|
+
*
|
|
241
|
+
* Either side may spell the model bare (`glm-5.2`), provider-qualified
|
|
242
|
+
* (`zai-coding-plan/glm-5.2`), or route-qualified, and the platform reports the provider in its
|
|
243
|
+
* own field rather than always in the id. So the comparison drops any known provider prefix and
|
|
244
|
+
* then accepts a `/`-boundary suffix match: a longer route to the SAME leaf model is a routing
|
|
245
|
+
* difference, not a substitution.
|
|
246
|
+
*
|
|
247
|
+
* What it deliberately does NOT absorb is a different leaf: `glm-5.2` against `glm-5.3` stays a
|
|
248
|
+
* mismatch, which is the substitution measured directly against the provider API (a request for
|
|
249
|
+
* `glm-5.2` was served `glm-5.3`). A version suffix is the whole difference between two
|
|
250
|
+
* instruments, so nothing here may treat it as noise.
|
|
251
|
+
*/
|
|
252
|
+
function sameModelId(served, wanted, providers) {
|
|
253
|
+
const a = stripProviderPrefix(served, providers);
|
|
254
|
+
const b = stripProviderPrefix(wanted, providers);
|
|
255
|
+
return a === b || a.endsWith(`/${b}`) || b.endsWith(`/${a}`);
|
|
256
|
+
}
|
|
257
|
+
function stripProviderPrefix(id, providers) {
|
|
258
|
+
let out = id.trim().toLowerCase();
|
|
259
|
+
for (const provider of providers) {
|
|
260
|
+
const prefix = provider?.trim().toLowerCase();
|
|
261
|
+
if (prefix && out.startsWith(`${prefix}/`)) out = out.slice(prefix.length + 1);
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
function firstString(...values) {
|
|
266
|
+
return values.find((value) => typeof value === "string" && value.length > 0);
|
|
267
|
+
}
|
|
268
|
+
const terminalTypes = /* @__PURE__ */ new Set([
|
|
269
|
+
"message.completed",
|
|
270
|
+
"result",
|
|
271
|
+
"final",
|
|
272
|
+
"done"
|
|
273
|
+
]);
|
|
274
|
+
/** True for an event type that ends a sandbox turn. */
|
|
275
|
+
function isSandboxTerminalEvent(type) {
|
|
276
|
+
return terminalTypes.has(type);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Which member of a terminal event's `data` carries its usage receipt.
|
|
280
|
+
*
|
|
281
|
+
* The one place the terminal types legitimately differ, stated rather than implied by two lists:
|
|
282
|
+
* sandbox 0.4.0's `done` reports under `tokenUsage` with the cost at the top level, every other
|
|
283
|
+
* terminal type reports under `usage`.
|
|
284
|
+
*/
|
|
285
|
+
function sandboxTerminalUsageField(type) {
|
|
286
|
+
return type === "done" ? "tokenUsage" : "usage";
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Extract a `RuntimeStreamEvent`-shaped `llm_call` from a sandbox event when
|
|
290
|
+
* the event carries usage/cost data. Returns `undefined` for non-cost events
|
|
291
|
+
* so the kernel can iterate the full stream without branching.
|
|
292
|
+
*
|
|
293
|
+
* Pure by contract: it never throws on a failed run. The terminal truth
|
|
294
|
+
* boundary is the public Sandbox outcome tracker, applied after the complete
|
|
295
|
+
* stream. Post-hoc readers — {@link sumSandboxUsage}, the
|
|
296
|
+
* analyst trace store, the chat projection — must stay able to read a failed
|
|
297
|
+
* turn's events, which is when reading them matters most.
|
|
298
|
+
*
|
|
299
|
+
* Canonical cost-carrying types observed in the wild:
|
|
300
|
+
* - `llm_call` — `data: { model, tokensIn, tokensOut, costUsd, ... }`
|
|
301
|
+
* - `message.completed` / `result` — `data: { usage: { inputTokens,
|
|
302
|
+
* outputTokens, totalCostUsd? } }`
|
|
303
|
+
* - `cost.usage` / `usage` — same shape under a dedicated type
|
|
304
|
+
*
|
|
305
|
+
* Numeric coercion is strict: `Number.isFinite` gates every accumulator write
|
|
306
|
+
* so a sentinel `NaN` from a misbehaving backend cannot poison the ledger.
|
|
307
|
+
*/
|
|
308
|
+
function extractLlmCallEvent(event, agentRunName) {
|
|
309
|
+
const data = canonicalUsageData(event);
|
|
310
|
+
return data === void 0 ? void 0 : buildLlmCall(data, agentRunName);
|
|
311
|
+
}
|
|
312
|
+
function canonicalUsageData(event) {
|
|
313
|
+
if (!event || typeof event !== "object") return void 0;
|
|
314
|
+
const type = String(event.type ?? "");
|
|
315
|
+
const data = event.data && typeof event.data === "object" ? event.data : {};
|
|
316
|
+
if (type === "llm_call" || type === "cost.usage" || type === "usage") return usageReceiptData(data, recordValue(data.usage));
|
|
317
|
+
if (type === "error" || isSandboxTerminalEvent(type)) {
|
|
318
|
+
const field = type === "error" ? "tokenUsage" : sandboxTerminalUsageField(type);
|
|
319
|
+
return usageReceiptData(data, recordValue(data[field]) ?? recordValue(data[field === "usage" ? "tokenUsage" : "usage"]));
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/** A {@link SandboxUsageLedger} for one worker. Pass the worker's harness to decode with that
|
|
323
|
+
* harness's adapter; omit it to try every registered adapter. */
|
|
324
|
+
function createSandboxUsageLedger(harness) {
|
|
325
|
+
let held = [];
|
|
326
|
+
let sawCanonical = false;
|
|
327
|
+
const credited = {};
|
|
328
|
+
const receipts = /* @__PURE__ */ new Map();
|
|
329
|
+
const settledTokens = zeroTokenUsage();
|
|
330
|
+
let turnTokens = zeroTokenUsage();
|
|
331
|
+
let turnUsageUnknown = false;
|
|
332
|
+
let settledUsage;
|
|
333
|
+
return {
|
|
334
|
+
settledUsage: () => settledUsage === void 0 ? void 0 : { ...settledUsage },
|
|
335
|
+
tokenUsage() {
|
|
336
|
+
const total = cloneTokenUsage(settledTokens);
|
|
337
|
+
addTokenUsage(total, turnTokens);
|
|
338
|
+
return total;
|
|
339
|
+
},
|
|
340
|
+
observe(event, agentRunName) {
|
|
341
|
+
const call = extractLlmCallEvent(event, agentRunName);
|
|
342
|
+
if (call) {
|
|
343
|
+
sawCanonical = true;
|
|
344
|
+
const receipt = creditCanonicalUsage(event, call, credited, receipts, (receipt, mode) => {
|
|
345
|
+
refineCanonicalTokens(turnTokens, call, receipt, mode);
|
|
346
|
+
});
|
|
347
|
+
if (receipt?.tokensKnown === false) {
|
|
348
|
+
turnTokens.tokensKnown = false;
|
|
349
|
+
turnUsageUnknown = true;
|
|
350
|
+
}
|
|
351
|
+
return receipt;
|
|
352
|
+
}
|
|
353
|
+
let usage;
|
|
354
|
+
try {
|
|
355
|
+
usage = decodeHarnessUsage(event, harness);
|
|
356
|
+
} catch (err) {
|
|
357
|
+
turnUsageUnknown = true;
|
|
358
|
+
return {
|
|
359
|
+
type: "llm_call",
|
|
360
|
+
model: agentRunName,
|
|
361
|
+
tokensKnown: false,
|
|
362
|
+
usdKnown: false,
|
|
363
|
+
tokensUnknownReason: err instanceof Error ? err.message : String(err)
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
if (usage) held.push(usage);
|
|
367
|
+
},
|
|
368
|
+
settleTurn(agentRunName) {
|
|
369
|
+
const reports = held;
|
|
370
|
+
const canonical = sawCanonical;
|
|
371
|
+
held = [];
|
|
372
|
+
sawCanonical = false;
|
|
373
|
+
const receipt = !canonical && reports.length > 0 ? harnessUsageLlmCall(reports, agentRunName) : void 0;
|
|
374
|
+
if (receipt) creditCanonicalUsage({
|
|
375
|
+
type: "llm_call",
|
|
376
|
+
data: {}
|
|
377
|
+
}, receipt, credited, receipts, (credit, mode) => refineCanonicalTokens(turnTokens, receipt, credit, mode));
|
|
378
|
+
settledUsage = !turnUsageUnknown && credited.tokensIn !== void 0 && credited.tokensOut !== void 0 ? {
|
|
379
|
+
inputTokens: credited.tokensIn,
|
|
380
|
+
outputTokens: credited.tokensOut,
|
|
381
|
+
...credited.totalTokens === void 0 ? {} : { totalTokens: credited.totalTokens },
|
|
382
|
+
...credited.reasoningTokens === void 0 ? {} : { reasoningTokens: credited.reasoningTokens },
|
|
383
|
+
...credited["promptCache.readTokens"] === void 0 ? {} : { cacheReadInputTokens: credited["promptCache.readTokens"] },
|
|
384
|
+
...credited["promptCache.writeTokens"] === void 0 ? {} : { cacheCreationInputTokens: credited["promptCache.writeTokens"] },
|
|
385
|
+
...credited.costUsd === void 0 ? {} : { cost: credited.costUsd }
|
|
386
|
+
} : void 0;
|
|
387
|
+
for (const key of Object.keys(credited)) delete credited[key];
|
|
388
|
+
receipts.clear();
|
|
389
|
+
addTokenUsage(settledTokens, turnTokens);
|
|
390
|
+
turnTokens = zeroTokenUsage();
|
|
391
|
+
turnUsageUnknown = false;
|
|
392
|
+
return receipt;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
/** Shared receipt semantics; an undeclared nonterminal observation is not a cumulative total. */
|
|
397
|
+
function canonicalSandboxUsageMode(event) {
|
|
398
|
+
const declaredMode = recordValue(event.data)?.usageMode;
|
|
399
|
+
return declaredMode === "delta" || declaredMode === "cumulative" ? declaredMode : declaredMode === void 0 && event.type === "llm_call" ? "delta" : declaredMode === void 0 && (event.type === "error" || isSandboxTerminalEvent(String(event.type))) ? "cumulative" : void 0;
|
|
400
|
+
}
|
|
401
|
+
/** Terminal totals replace already credited usage; only per-call receipts add to it. */
|
|
402
|
+
function creditCanonicalUsage(event, call, credited, receipts, onCredit) {
|
|
403
|
+
const mode = canonicalSandboxUsageMode(event);
|
|
404
|
+
const result = { ...call };
|
|
405
|
+
const data = recordValue(event.data) ?? {};
|
|
406
|
+
const usageData = canonicalUsageData(event) ?? {};
|
|
407
|
+
const supplemental = Object.fromEntries(["totalTokens", "reasoningTokens"].flatMap((key) => {
|
|
408
|
+
const value = usageData[key];
|
|
409
|
+
return value === void 0 ? [] : [[key, typeof value === "number" && Number.isFinite(value) ? value : null]];
|
|
410
|
+
}));
|
|
411
|
+
const explicitTokensUnknown = [
|
|
412
|
+
data,
|
|
413
|
+
recordValue(data.usage),
|
|
414
|
+
recordValue(data.tokenUsage)
|
|
415
|
+
].some((receipt) => ["tokensKnown", "tokens_known"].some((key) => receipt?.[key] !== void 0 && receipt[key] !== true));
|
|
416
|
+
if (call.tokensIn === void 0 && call.tokensOut === void 0 && !call.tokensUnknownReason && !explicitTokensUnknown) delete result.tokensKnown;
|
|
417
|
+
let reason = mode === void 0 ? "usage receipt has no declared delta or cumulative semantics" : void 0;
|
|
418
|
+
if (event.id) {
|
|
419
|
+
const signature = JSON.stringify({
|
|
420
|
+
mode,
|
|
421
|
+
call,
|
|
422
|
+
supplemental
|
|
423
|
+
});
|
|
424
|
+
const previous = receipts.get(event.id);
|
|
425
|
+
if (previous === signature) return void 0;
|
|
426
|
+
if (previous !== void 0) return {
|
|
427
|
+
type: "llm_call",
|
|
428
|
+
model: call.model,
|
|
429
|
+
tokensKnown: false,
|
|
430
|
+
usdKnown: false,
|
|
431
|
+
tokensUnknownReason: `usage receipt ${event.id} changed after it was credited`
|
|
432
|
+
};
|
|
433
|
+
receipts.set(event.id, signature);
|
|
434
|
+
}
|
|
435
|
+
const credit = (key, value) => {
|
|
436
|
+
const previous = credited[key] ?? 0;
|
|
437
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
438
|
+
reason = `usage receipt has an invalid ${key}`;
|
|
439
|
+
return 0;
|
|
440
|
+
}
|
|
441
|
+
if (mode === "delta") {
|
|
442
|
+
credited[key] = previous + value;
|
|
443
|
+
return value;
|
|
444
|
+
}
|
|
445
|
+
if (value < previous) reason = `cumulative usage ${key} is below already credited usage`;
|
|
446
|
+
credited[key] = Math.max(previous, value);
|
|
447
|
+
return Math.max(0, value - previous);
|
|
448
|
+
};
|
|
449
|
+
for (const key of [
|
|
450
|
+
"tokensIn",
|
|
451
|
+
"tokensOut",
|
|
452
|
+
"costUsd",
|
|
453
|
+
"estimatedCostUsd"
|
|
454
|
+
]) if (call[key] !== void 0) result[key] = credit(key, call[key]);
|
|
455
|
+
for (const [key, value] of Object.entries(supplemental)) if (typeof value !== "number") reason = `usage receipt has an invalid ${key}`;
|
|
456
|
+
else credit(key, value);
|
|
457
|
+
if (call.promptCache) result.promptCache = Object.fromEntries(Object.entries(call.promptCache).map(([key, value]) => [key, typeof value === "number" ? credit(`promptCache.${key}`, value) : value]));
|
|
458
|
+
if (reason !== void 0) {
|
|
459
|
+
result.usdKnown = false;
|
|
460
|
+
if (call.tokensIn !== void 0 || call.tokensOut !== void 0) {
|
|
461
|
+
result.tokensKnown = false;
|
|
462
|
+
result.tokensUnknownReason = reason;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
onCredit(result, mode);
|
|
466
|
+
return result;
|
|
467
|
+
}
|
|
468
|
+
/** Cache partitions belong to the original receipt, not the smaller incremental charge. */
|
|
469
|
+
function refineCanonicalTokens(tokens, call, receipt, mode) {
|
|
470
|
+
const input = receipt.tokensIn ?? 0;
|
|
471
|
+
const output = receipt.tokensOut ?? 0;
|
|
472
|
+
const cache = promptCacheTokenClasses(call.tokensIn, call.promptCache);
|
|
473
|
+
if (mode !== "cumulative") {
|
|
474
|
+
addTokenUsage(tokens, {
|
|
475
|
+
input,
|
|
476
|
+
output,
|
|
477
|
+
...cache
|
|
478
|
+
});
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
tokens.input += input;
|
|
482
|
+
tokens.output += output;
|
|
483
|
+
if (cache.freshInput !== void 0 && call.tokensIn === tokens.input && receipt.tokensKnown !== false) {
|
|
484
|
+
tokens.freshInput = cache.freshInput;
|
|
485
|
+
tokens.cacheRead = cache.cacheRead;
|
|
486
|
+
tokens.cacheWrite = cache.cacheWrite;
|
|
487
|
+
delete tokens.cacheBreakdownKnown;
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (cache.cacheRead !== void 0) tokens.cacheRead = Math.max(tokens.cacheRead ?? 0, cache.cacheRead);
|
|
491
|
+
if (cache.cacheWrite !== void 0) tokens.cacheWrite = Math.max(tokens.cacheWrite ?? 0, cache.cacheWrite);
|
|
492
|
+
if ((tokens.freshInput ?? 0) + (tokens.cacheRead ?? 0) + (tokens.cacheWrite ?? 0) > tokens.input) {
|
|
493
|
+
delete tokens.freshInput;
|
|
494
|
+
delete tokens.cacheRead;
|
|
495
|
+
delete tokens.cacheWrite;
|
|
496
|
+
}
|
|
497
|
+
if (input > 0 || cache.cacheBreakdownKnown === false) tokens.cacheBreakdownKnown = false;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Fold harness-native usage reports into the canonical `llm_call` the accounting paths read.
|
|
501
|
+
*
|
|
502
|
+
* Every counter a `HarnessUsage` carries beside `input` and `output` CLASSIFIES one of those two
|
|
503
|
+
* totals, so none of them is added to the total it describes:
|
|
504
|
+
*
|
|
505
|
+
* - the prompt-cache counters classify `input` and ride `promptCache`, the convention the
|
|
506
|
+
* terminal `tokenUsage` branch above already follows and `promptCacheTokenClasses` folds,
|
|
507
|
+
* where `freshInput = input - cacheRead - cacheWrite`;
|
|
508
|
+
* - `reasoningOutput` classifies `output`. Codex counts its reasoning tokens INSIDE
|
|
509
|
+
* `output_tokens` — a turn reporting `output_tokens 1523` with `reasoning_output_tokens 1516`
|
|
510
|
+
* answered with about seven tokens of text — and `parseCodexUsageRecord` holds that as an
|
|
511
|
+
* invariant. The canonical `llm_call` carries no reasoning class, so the count is not
|
|
512
|
+
* forwarded to {@link buildLlmCall}. Canonical output totals already include reasoning.
|
|
513
|
+
*
|
|
514
|
+
* A counter no report carried stays absent, because a zero would claim the provider measured none.
|
|
515
|
+
*/
|
|
516
|
+
function harnessUsageLlmCall(reports, agentRunName) {
|
|
517
|
+
let inputTokens = 0;
|
|
518
|
+
let outputTokens = 0;
|
|
519
|
+
let readTokens;
|
|
520
|
+
let writeTokens;
|
|
521
|
+
for (const report of reports) {
|
|
522
|
+
inputTokens += report.input;
|
|
523
|
+
outputTokens += report.output;
|
|
524
|
+
if (report.cachedInput !== void 0) readTokens = (readTokens ?? 0) + report.cachedInput;
|
|
525
|
+
if (report.cacheWriteInput !== void 0) writeTokens = (writeTokens ?? 0) + report.cacheWriteInput;
|
|
526
|
+
}
|
|
527
|
+
const promptCache = {};
|
|
528
|
+
if (readTokens !== void 0) promptCache.readTokens = readTokens;
|
|
529
|
+
if (writeTokens !== void 0) promptCache.writeTokens = writeTokens;
|
|
530
|
+
return buildLlmCall({
|
|
531
|
+
inputTokens,
|
|
532
|
+
outputTokens,
|
|
533
|
+
...Object.keys(promptCache).length > 0 ? { promptCache } : {}
|
|
534
|
+
}, agentRunName);
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Sum the token usage + USD cost of a sandbox turn's events — the one honest way to meter an
|
|
538
|
+
* `openSandboxRun` cell. Folds a {@link SandboxUsageLedger} over the stream, so it reads usage off
|
|
539
|
+
* EVERY backend event shape — the canonical events plus a harness that reports usage only in its
|
|
540
|
+
* own event — and a `runProfileMatrix` dispatch can report it to `ctx.cost`:
|
|
541
|
+
*
|
|
542
|
+
* receipt: (turn) => {
|
|
543
|
+
* const u = sumSandboxUsage(turn.events)
|
|
544
|
+
* return { model, inputTokens: u.input, outputTokens: u.output,
|
|
545
|
+
* ...(u.tokensKnown === false ? { usageUnknown: true } : {}),
|
|
546
|
+
* ...(u.usdKnown !== false && u.costUsd > 0 ? { actualCostUsd: u.costUsd } : {}),
|
|
547
|
+
* ...(u.usdKnown === false ? { costUnknown: true } : {}),
|
|
548
|
+
* ...(u.estimatedCostUsd !== undefined ? { estimatedCostUsd: u.estimatedCostUsd } : {}) }
|
|
549
|
+
* }
|
|
550
|
+
*
|
|
551
|
+
* Without this a cell reads `{tokens:0, cost:0}` and the backend-integrity guard correctly aborts the
|
|
552
|
+
* matrix as a stub. `agentRunName` is the fallback model label for cost-only events (default `'agent'`).
|
|
553
|
+
*
|
|
554
|
+
* Pure by contract, like the ledger it folds: it never throws. A harness receipt the ledger cannot
|
|
555
|
+
* read leaves the result at `tokensKnown: false` with `tokensUnknownReason` carrying the decode
|
|
556
|
+
* message — an unreadable receipt is a different fact from a turn that reported no usage, and a
|
|
557
|
+
* post-hoc reader that threw would lose the whole failed turn it exists to report.
|
|
558
|
+
*/
|
|
559
|
+
function sumSandboxUsage(events, agentRunName = "agent") {
|
|
560
|
+
let input = 0;
|
|
561
|
+
let output = 0;
|
|
562
|
+
let costUsd = 0;
|
|
563
|
+
let estimatedCostUsd = 0;
|
|
564
|
+
let sawEstimate = false;
|
|
565
|
+
let sawCall = false;
|
|
566
|
+
let sawTokenReceipt = false;
|
|
567
|
+
let tokensKnown = true;
|
|
568
|
+
let usdKnown = true;
|
|
569
|
+
let unreadable;
|
|
570
|
+
const ledger = createSandboxUsageLedger();
|
|
571
|
+
const credit = (call) => {
|
|
572
|
+
sawCall = true;
|
|
573
|
+
sawTokenReceipt ||= call.tokensIn !== void 0 || call.tokensOut !== void 0;
|
|
574
|
+
input += call.tokensIn ?? 0;
|
|
575
|
+
output += call.tokensOut ?? 0;
|
|
576
|
+
costUsd += call.costUsd ?? 0;
|
|
577
|
+
if (call.tokensKnown === false) tokensKnown = false;
|
|
578
|
+
if (call.usdKnown === false) usdKnown = false;
|
|
579
|
+
unreadable ??= call.tokensUnknownReason;
|
|
580
|
+
if (call.estimatedCostUsd !== void 0) {
|
|
581
|
+
estimatedCostUsd += call.estimatedCostUsd;
|
|
582
|
+
sawEstimate = true;
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
for (const ev of events) {
|
|
586
|
+
const call = ledger.observe(ev, agentRunName);
|
|
587
|
+
if (call) credit(call);
|
|
588
|
+
}
|
|
589
|
+
const harnessCall = ledger.settleTurn(agentRunName);
|
|
590
|
+
if (harnessCall) credit(harnessCall);
|
|
591
|
+
return {
|
|
592
|
+
input,
|
|
593
|
+
output,
|
|
594
|
+
costUsd,
|
|
595
|
+
...sawTokenReceipt && tokensKnown ? {} : { tokensKnown: false },
|
|
596
|
+
...sawCall && usdKnown ? {} : { usdKnown: false },
|
|
597
|
+
...sawEstimate ? { estimatedCostUsd } : {},
|
|
598
|
+
...unreadable === void 0 ? {} : { tokensUnknownReason: unreadable }
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
function usageReceiptData(envelope, usage = {}) {
|
|
602
|
+
const merged = {
|
|
603
|
+
...usage,
|
|
604
|
+
...envelope
|
|
605
|
+
};
|
|
606
|
+
for (const aliases of [
|
|
607
|
+
[
|
|
608
|
+
"tokensIn",
|
|
609
|
+
"inputTokens",
|
|
610
|
+
"prompt_tokens"
|
|
611
|
+
],
|
|
612
|
+
[
|
|
613
|
+
"tokensOut",
|
|
614
|
+
"outputTokens",
|
|
615
|
+
"completion_tokens"
|
|
616
|
+
],
|
|
617
|
+
[
|
|
618
|
+
"costUsd",
|
|
619
|
+
"totalCostUsd",
|
|
620
|
+
"cost_usd",
|
|
621
|
+
"cost"
|
|
622
|
+
],
|
|
623
|
+
["costProvenance", "cost_provenance"]
|
|
624
|
+
]) {
|
|
625
|
+
const key = aliases.find((alias) => envelope[alias] !== void 0);
|
|
626
|
+
if (key === void 0) continue;
|
|
627
|
+
for (const alias of aliases) delete merged[alias];
|
|
628
|
+
merged[key] = envelope[key];
|
|
629
|
+
}
|
|
630
|
+
for (const aliases of [["tokensKnown", "tokens_known"], [
|
|
631
|
+
"costKnown",
|
|
632
|
+
"cost_known",
|
|
633
|
+
"usdKnown",
|
|
634
|
+
"usd_known"
|
|
635
|
+
]]) {
|
|
636
|
+
const values = [usage, envelope].flatMap((receipt) => aliases.map((key) => receipt[key]).filter((value) => value !== void 0));
|
|
637
|
+
if (values.length > 0) merged[aliases[0]] = values.every((value) => value === true);
|
|
638
|
+
}
|
|
639
|
+
return merged;
|
|
640
|
+
}
|
|
641
|
+
function buildLlmCall(data, agentRunName) {
|
|
642
|
+
const tokensIn = pickFiniteNumber(data, [
|
|
643
|
+
"tokensIn",
|
|
644
|
+
"inputTokens",
|
|
645
|
+
"prompt_tokens"
|
|
646
|
+
]);
|
|
647
|
+
const outputTokens = pickFiniteNumber(data, [
|
|
648
|
+
"tokensOut",
|
|
649
|
+
"outputTokens",
|
|
650
|
+
"completion_tokens"
|
|
651
|
+
]);
|
|
652
|
+
const reasoningTokens = pickFiniteNumber(data, ["reasoningTokens"]);
|
|
653
|
+
const tokensOut = outputTokens ?? reasoningTokens;
|
|
654
|
+
const reportedCostUsd = pickFiniteNumber(data, [
|
|
655
|
+
"costUsd",
|
|
656
|
+
"totalCostUsd",
|
|
657
|
+
"cost_usd",
|
|
658
|
+
"cost"
|
|
659
|
+
]);
|
|
660
|
+
const explicitTokensKnown = data.tokensKnown ?? data.tokens_known;
|
|
661
|
+
const explicitCostKnown = data.costKnown ?? data.cost_known ?? data.usdKnown ?? data.usd_known;
|
|
662
|
+
const costProvenance = data.costProvenance ?? data.cost_provenance;
|
|
663
|
+
const explicitEstimate = pickFiniteNumber(data, ["estimatedCostUsd", "estimated_cost_usd"]);
|
|
664
|
+
const catalogEstimate = costProvenance === "catalog-estimate" ? explicitEstimate ?? reportedCostUsd : explicitEstimate;
|
|
665
|
+
const costUsd = costProvenance === "catalog-estimate" ? void 0 : reportedCostUsd;
|
|
666
|
+
const promptCache = readPromptCacheUsage(data);
|
|
667
|
+
const tokensKnown = explicitTokensKnown !== false && tokensIn !== void 0 && outputTokens !== void 0;
|
|
668
|
+
const usdKnown = explicitCostKnown !== false && costUsd !== void 0 && (costProvenance === void 0 || costProvenance === "provider-receipt" || costProvenance === "billing-receipt");
|
|
669
|
+
const tools = readOfferedTools(data.tools);
|
|
670
|
+
if (tokensIn === void 0 && tokensOut === void 0 && costUsd === void 0 && catalogEstimate === void 0 && promptCache === void 0 && tools === void 0 && explicitTokensKnown !== false && explicitCostKnown !== false) return;
|
|
671
|
+
const event = {
|
|
672
|
+
type: "llm_call",
|
|
673
|
+
model: typeof data.model === "string" && data.model.length > 0 ? data.model : agentRunName
|
|
674
|
+
};
|
|
675
|
+
if (tokensIn !== void 0) event.tokensIn = tokensIn;
|
|
676
|
+
if (tokensOut !== void 0) event.tokensOut = tokensOut;
|
|
677
|
+
if (!tokensKnown) event.tokensKnown = false;
|
|
678
|
+
if (costUsd !== void 0) event.costUsd = costUsd;
|
|
679
|
+
if (!usdKnown) event.usdKnown = false;
|
|
680
|
+
if (catalogEstimate !== void 0) event.estimatedCostUsd = catalogEstimate;
|
|
681
|
+
if (promptCache !== void 0) event.promptCache = promptCache;
|
|
682
|
+
if (tools !== void 0) event.tools = tools;
|
|
683
|
+
return event;
|
|
684
|
+
}
|
|
685
|
+
/** `data.tools`, when it is a non-empty array of non-empty tool-name strings. Anything else
|
|
686
|
+
* (absent, empty, wrong element type) is treated as "the producer did not report its offered
|
|
687
|
+
* set" — never a placeholder empty list, which would read as "offered zero tools" downstream. */
|
|
688
|
+
function readOfferedTools(value) {
|
|
689
|
+
if (!Array.isArray(value) || value.length === 0) return void 0;
|
|
690
|
+
const names = value.filter((v) => typeof v === "string" && v.length > 0);
|
|
691
|
+
return names.length === value.length ? names : void 0;
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Every wire spelling of a prompt-cache READ counter, in precedence order: the canonical name
|
|
695
|
+
* first, then the spellings observed on the paths that reach this extractor — the sandbox terminal
|
|
696
|
+
* `tokenUsage` record, the cli-bridge OpenAI-compatible usage object (which normalizes every
|
|
697
|
+
* backend to Anthropic's `cache_read_input_tokens`), Anthropic verbatim, OpenAI's
|
|
698
|
+
* `prompt_tokens_details.cached_tokens`, and DeepSeek's `prompt_cache_hit_tokens`.
|
|
699
|
+
*/
|
|
700
|
+
const PROMPT_CACHE_READ_KEYS = [
|
|
701
|
+
"readTokens",
|
|
702
|
+
"read_tokens",
|
|
703
|
+
"cacheReadInputTokens",
|
|
704
|
+
"cache_read_input_tokens",
|
|
705
|
+
"cacheRead",
|
|
706
|
+
"cache_read",
|
|
707
|
+
"cachedInputTokens",
|
|
708
|
+
"cachedTokens",
|
|
709
|
+
"cached_tokens",
|
|
710
|
+
"prompt_cache_hit_tokens"
|
|
711
|
+
];
|
|
712
|
+
/**
|
|
713
|
+
* Every wire spelling of a prompt-cache WRITE counter. OpenAI reports no write counter at all, so
|
|
714
|
+
* an OpenAI-shaped usage record matches nothing here and the write stays absent — which is the
|
|
715
|
+
* point: absent is not zero.
|
|
716
|
+
*/
|
|
717
|
+
const PROMPT_CACHE_WRITE_KEYS = [
|
|
718
|
+
"writeTokens",
|
|
719
|
+
"write_tokens",
|
|
720
|
+
"cacheWriteInputTokens",
|
|
721
|
+
"cache_write_input_tokens",
|
|
722
|
+
"cacheCreationInputTokens",
|
|
723
|
+
"cache_creation_input_tokens",
|
|
724
|
+
"cacheWrite",
|
|
725
|
+
"cache_write"
|
|
726
|
+
];
|
|
727
|
+
const PROMPT_CACHE_MISS_KEYS = [
|
|
728
|
+
"missTokens",
|
|
729
|
+
"miss_tokens",
|
|
730
|
+
"prompt_cache_miss_tokens"
|
|
731
|
+
];
|
|
732
|
+
const PROMPT_CACHE_SAVINGS_KEYS = ["readSavingsUsd", "read_savings_usd"];
|
|
733
|
+
/**
|
|
734
|
+
* Read the provider's own prompt-cache accounting off one usage-bearing event payload, in the
|
|
735
|
+
* `PromptCacheUsage` vocabulary the router, driver, and chat-client paths already speak.
|
|
736
|
+
*
|
|
737
|
+
* Two sources, one record. A payload that already carries a `promptCache` / `prompt_cache` object
|
|
738
|
+
* is forwarded VERBATIM — those fields are the provider's own report, and an unrecognized one must
|
|
739
|
+
* stay visible rather than be bucketed away. On top of that, flat provider counters are translated
|
|
740
|
+
* to the canonical names, filling only the names the verbatim object did not already define.
|
|
741
|
+
*
|
|
742
|
+
* A counter the payload does not carry is left out. Defaulting it to zero would assert the provider
|
|
743
|
+
* measured no cache, which is a different fact from a provider that reported nothing — and it is
|
|
744
|
+
* the fact the budget reads to decide whether its charge is a measurement or a bound.
|
|
745
|
+
*/
|
|
746
|
+
function readPromptCacheUsage(data) {
|
|
747
|
+
const declared = finiteMetadata(data.promptCache ?? data.prompt_cache);
|
|
748
|
+
const nested = recordValue(data.promptCache) ?? recordValue(data.prompt_cache);
|
|
749
|
+
const sources = [
|
|
750
|
+
nested,
|
|
751
|
+
data,
|
|
752
|
+
recordValue(data.prompt_tokens_details)
|
|
753
|
+
].filter((s) => s !== void 0);
|
|
754
|
+
const canonical = {};
|
|
755
|
+
const readTokens = pickTokenCount(sources, PROMPT_CACHE_READ_KEYS);
|
|
756
|
+
const writeTokens = pickTokenCount(sources, PROMPT_CACHE_WRITE_KEYS);
|
|
757
|
+
const missTokens = pickTokenCount(sources, PROMPT_CACHE_MISS_KEYS);
|
|
758
|
+
const readSavingsUsd = pickNonNegativeNumber(sources, PROMPT_CACHE_SAVINGS_KEYS);
|
|
759
|
+
const status = nested !== void 0 && typeof nested.status === "string" ? nested.status : void 0;
|
|
760
|
+
if (readTokens !== void 0) canonical.readTokens = readTokens;
|
|
761
|
+
if (writeTokens !== void 0) canonical.writeTokens = writeTokens;
|
|
762
|
+
if (missTokens !== void 0) canonical.missTokens = missTokens;
|
|
763
|
+
if (readSavingsUsd !== void 0) canonical.readSavingsUsd = readSavingsUsd;
|
|
764
|
+
if (status !== void 0) canonical.status = status;
|
|
765
|
+
const merged = {
|
|
766
|
+
...canonical,
|
|
767
|
+
...declared
|
|
768
|
+
};
|
|
769
|
+
return Object.keys(merged).length > 0 ? merged : void 0;
|
|
770
|
+
}
|
|
771
|
+
function pickTokenCount(sources, keys) {
|
|
772
|
+
for (const key of keys) for (const source of sources) {
|
|
773
|
+
const value = source[key];
|
|
774
|
+
if (typeof value === "number" && Number.isSafeInteger(value) && value >= 0) return value;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
function pickNonNegativeNumber(sources, keys) {
|
|
778
|
+
for (const key of keys) for (const source of sources) {
|
|
779
|
+
const value = source[key];
|
|
780
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0) return value;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
function finiteMetadata(value) {
|
|
784
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
785
|
+
const result = {};
|
|
786
|
+
for (const [key, entry] of Object.entries(value)) if (typeof entry === "number" && Number.isFinite(entry) || typeof entry === "string") result[key] = entry;
|
|
787
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
788
|
+
}
|
|
789
|
+
function pickFiniteNumber(data, keys) {
|
|
790
|
+
for (const key of keys) {
|
|
791
|
+
const value = data[key];
|
|
792
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Collect one turn's events, keeping only the newest snapshot of each streamed text or
|
|
797
|
+
* reasoning part.
|
|
798
|
+
*
|
|
799
|
+
* opencode sends `message.part.updated` once per token, and every frame carries the part's
|
|
800
|
+
* whole cumulative text, so keeping every frame holds O(L²) bytes per turn: two concurrent
|
|
801
|
+
* audit workflows took the platform API from 459 MB to an OOM at 1.95 GB in 11 minutes.
|
|
802
|
+
* A snapshot (a frame whose part carries `text`) whose text extends the previous snapshot
|
|
803
|
+
* of the same part replaces it and moves to the newest position, which is the rule the
|
|
804
|
+
* Sandbox SDK's text collectors already apply, so the collected text is unchanged. The kept
|
|
805
|
+
* frame drops its `delta`, because its text already holds every earlier increment and a
|
|
806
|
+
* reader that joins `delta ?? part.text` across frames would otherwise read only the last
|
|
807
|
+
* one. Delta-only frames and every other event are kept as they arrived.
|
|
808
|
+
*/
|
|
809
|
+
function createSandboxTurnEventCollector() {
|
|
810
|
+
const slots = [];
|
|
811
|
+
const snapshots = /* @__PURE__ */ new Map();
|
|
812
|
+
return {
|
|
813
|
+
push(event) {
|
|
814
|
+
const snapshot = partSnapshot(event);
|
|
815
|
+
if (!snapshot) {
|
|
816
|
+
slots.push(event);
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
const previous = snapshots.get(snapshot.key);
|
|
820
|
+
const extends_ = previous !== void 0 && snapshot.text.startsWith(previous.text);
|
|
821
|
+
if (extends_) slots[previous.at] = void 0;
|
|
822
|
+
snapshots.set(snapshot.key, {
|
|
823
|
+
at: slots.length,
|
|
824
|
+
text: snapshot.text
|
|
825
|
+
});
|
|
826
|
+
slots.push(extends_ ? withoutDelta(event) : event);
|
|
827
|
+
},
|
|
828
|
+
events() {
|
|
829
|
+
return slots.filter((event) => event !== void 0);
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
function partSnapshot(event) {
|
|
834
|
+
if (event?.type !== "message.part.updated") return void 0;
|
|
835
|
+
const data = asRecord(event.data);
|
|
836
|
+
const part = asRecord(data?.part) ?? asRecord(asRecord(data?.properties)?.part);
|
|
837
|
+
if (!part || typeof part.id !== "string") return void 0;
|
|
838
|
+
if (part.type !== "text" && part.type !== "reasoning" && part.type !== "thinking") return void 0;
|
|
839
|
+
const text = typeof part.text === "string" ? part.text : typeof part.content === "string" ? part.content : void 0;
|
|
840
|
+
return text === void 0 ? void 0 : {
|
|
841
|
+
key: `${part.type}:${part.id}`,
|
|
842
|
+
text
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
function withoutDelta(event) {
|
|
846
|
+
const data = asRecord(event.data);
|
|
847
|
+
if (!data || !("delta" in data)) return event;
|
|
848
|
+
const { delta: _delta, ...rest } = data;
|
|
849
|
+
return {
|
|
850
|
+
...event,
|
|
851
|
+
data: rest
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
function asRecord(value) {
|
|
855
|
+
return value !== null && typeof value === "object" ? value : void 0;
|
|
856
|
+
}
|
|
857
|
+
// src/runtime/timestamps.ts
|
|
858
|
+
const validTimestamp = "2026-01-01T00:00:00.000Z";
|
|
859
|
+
/** Validate a timestamp with the public runtime envelope contract. */
|
|
860
|
+
function assertRuntimeTimestamp(value, label) {
|
|
861
|
+
try {
|
|
862
|
+
RuntimeEventEnvelopeSchema.parse({
|
|
863
|
+
runId: "runtime",
|
|
864
|
+
eventId: "event",
|
|
865
|
+
sequence: 0,
|
|
866
|
+
occurredAt: value,
|
|
867
|
+
receivedAt: validTimestamp,
|
|
868
|
+
event: {
|
|
869
|
+
type: "status",
|
|
870
|
+
status: "processing"
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
} catch (error) {
|
|
874
|
+
throw new Error(`${label} must be a valid ISO timestamp`, { cause: error });
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
// src/runtime/sandbox-transport-events.ts
|
|
878
|
+
/** Canonical Sandbox/provider wire-envelope decoding and transport identity. */
|
|
879
|
+
const CANONICAL_STREAM_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
880
|
+
"child-task",
|
|
881
|
+
"message.part.updated",
|
|
882
|
+
"tool-heartbeat",
|
|
883
|
+
"tool-slow",
|
|
884
|
+
"model-processing",
|
|
885
|
+
"status",
|
|
886
|
+
"warning",
|
|
887
|
+
"raw",
|
|
888
|
+
"session.updated",
|
|
889
|
+
"interaction",
|
|
890
|
+
"interaction.cancel",
|
|
891
|
+
"plan.submitted"
|
|
892
|
+
]);
|
|
893
|
+
/** Decode one known Agent Interface event from a Sandbox event. */
|
|
894
|
+
function canonicalStreamEventFromSandboxEvent(event) {
|
|
895
|
+
if (!event || typeof event !== "object") return void 0;
|
|
896
|
+
const type = String(event.type ?? "");
|
|
897
|
+
const data = event.data && typeof event.data === "object" ? event.data : {};
|
|
898
|
+
const normalized = data.normalized;
|
|
899
|
+
if (normalized === void 0 && !CANONICAL_STREAM_EVENT_TYPES.has(type)) return void 0;
|
|
900
|
+
return parseCanonicalTransportEvent(type, data, normalized, "sandbox");
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Transport type whose payload is the harness's OWN event, passed through verbatim.
|
|
904
|
+
*
|
|
905
|
+
* A `raw` payload names a harness-native type from a vocabulary the canonical schema does not
|
|
906
|
+
* define (codex emits `thread.started` / `turn.started` / `turn.completed`). The transport type is
|
|
907
|
+
* therefore the only canonical statement such an event makes, and its payload must never be read
|
|
908
|
+
* as a canonical type: the mismatch guard below exists to catch a producer that MISLABELS a
|
|
909
|
+
* canonical event, which a harness-native payload cannot do. A payload that does satisfy the
|
|
910
|
+
* canonical `raw` shape (`backend` + `event`) still parses; anything else is not a canonical
|
|
911
|
+
* event, and the caller reads it off the transport event instead.
|
|
912
|
+
*/
|
|
913
|
+
const harnessNativePayloadType = "raw";
|
|
914
|
+
/** Parse canonical payload fields without treating transport identity as event data. */
|
|
915
|
+
function parseCanonicalTransportEvent(type, data, normalized, source) {
|
|
916
|
+
if (!isRecord(data)) throw new Error(`${source} emitted a canonical event without an object payload`);
|
|
917
|
+
const outerType = String(type ?? "");
|
|
918
|
+
const harnessNative = outerType === harnessNativePayloadType;
|
|
919
|
+
if (normalized !== void 0) {
|
|
920
|
+
if (!harnessNative && typeof normalized === "object" && normalized !== null && "type" in normalized && typeof normalized.type === "string" && normalized.type !== outerType) throw new Error(`${source} canonical event type "${normalized.type}" does not match transport type "${outerType}"`);
|
|
921
|
+
const candidate = CanonicalStreamEventSchema.safeParse(normalized);
|
|
922
|
+
if (!candidate.success) {
|
|
923
|
+
if (harnessNative) return void 0;
|
|
924
|
+
throw new Error(`${source} emitted an invalid normalized canonical event`, { cause: candidate.error });
|
|
925
|
+
}
|
|
926
|
+
return candidate.data;
|
|
927
|
+
}
|
|
928
|
+
const { type: embeddedType, eventId: _eventId, cursor: _cursor, sequence: _sequence, occurredAt: _occurredAt, normalized: _normalized, ...payload } = data;
|
|
929
|
+
if (!harnessNative && embeddedType !== void 0 && embeddedType !== outerType) throw new Error(`${source} canonical event type "${String(embeddedType)}" does not match transport type "${outerType}"`);
|
|
930
|
+
const candidate = CanonicalStreamEventSchema.safeParse({
|
|
931
|
+
...payload,
|
|
932
|
+
type: outerType
|
|
933
|
+
});
|
|
934
|
+
return candidate.success ? candidate.data : void 0;
|
|
935
|
+
}
|
|
936
|
+
/** Extract shared event identity without privileging one provider wire shape. */
|
|
937
|
+
function extractTransportEventIdentity(event) {
|
|
938
|
+
const record = isRecord(event) ? event : {};
|
|
939
|
+
const data = isRecord(record.data) ? record.data : {};
|
|
940
|
+
const providerEvent = isRecord(record.providerEvent) ? record.providerEvent : {};
|
|
941
|
+
const providerData = isRecord(providerEvent.data) ? providerEvent.data : {};
|
|
942
|
+
const eventId = canonicalEventId(record, data, providerEvent, providerData);
|
|
943
|
+
const cursor = stableString(record.cursor ?? data.cursor ?? providerEvent.cursor ?? providerData.cursor);
|
|
944
|
+
const sequence = optionalSequence(record.sequence ?? data.sequence ?? providerEvent.sequence ?? providerData.sequence);
|
|
945
|
+
const occurredAt = optionalTimestamp(record.occurredAt ?? data.occurredAt ?? providerEvent.occurredAt ?? providerData.occurredAt);
|
|
946
|
+
return {
|
|
947
|
+
...eventId === void 0 ? {} : { eventId },
|
|
948
|
+
...cursor === void 0 ? {} : { cursor },
|
|
949
|
+
...sequence === void 0 ? {} : { sequence },
|
|
950
|
+
...occurredAt === void 0 ? {} : { occurredAt }
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
function canonicalEventId(record, data, providerEvent, providerData) {
|
|
954
|
+
const present = [
|
|
955
|
+
["data.eventId", data.eventId],
|
|
956
|
+
["providerEvent.eventId", providerEvent.eventId],
|
|
957
|
+
["providerData.eventId", providerData.eventId],
|
|
958
|
+
["record.eventId", record.eventId]
|
|
959
|
+
].filter(([, value]) => value !== void 0);
|
|
960
|
+
if (present.length === 0) return stableString(record.id ?? providerEvent.id);
|
|
961
|
+
const normalized = present.map(([source, value]) => {
|
|
962
|
+
const eventId = stableString(value);
|
|
963
|
+
if (eventId === void 0) throw new Error(`transport event ${source} must be a stable string`);
|
|
964
|
+
return {
|
|
965
|
+
source,
|
|
966
|
+
eventId
|
|
967
|
+
};
|
|
968
|
+
});
|
|
969
|
+
const first = normalized[0];
|
|
970
|
+
if (normalized.some((candidate) => candidate.eventId !== first.eventId)) throw new Error("transport event canonical identities disagree");
|
|
971
|
+
return first.eventId;
|
|
972
|
+
}
|
|
973
|
+
function stableString(value) {
|
|
974
|
+
return typeof value === "string" && value.length > 0 && value.trim() === value ? value : void 0;
|
|
975
|
+
}
|
|
976
|
+
function finiteNonNegativeInteger(value) {
|
|
977
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
|
|
978
|
+
}
|
|
979
|
+
function optionalSequence(value) {
|
|
980
|
+
if (value === void 0) return void 0;
|
|
981
|
+
if (finiteNonNegativeInteger(value) === void 0) throw new Error("transport event sequence must be a non-negative safe integer");
|
|
982
|
+
return value;
|
|
983
|
+
}
|
|
984
|
+
function optionalTimestamp(value) {
|
|
985
|
+
if (value === void 0) return void 0;
|
|
986
|
+
assertRuntimeTimestamp(value, "occurredAt");
|
|
987
|
+
return value;
|
|
988
|
+
}
|
|
989
|
+
// src/runtime/sandbox-executor-output.ts
|
|
990
|
+
/** Streamed and terminal Sandbox executor output, using one tool-part projector. */
|
|
991
|
+
/**
|
|
992
|
+
* Fresh per-turn {@link SandboxToolPartState} for {@link mapSandboxToolEvent} — an
|
|
993
|
+
* empty call-status map so each turn projects tool frames independently.
|
|
994
|
+
*
|
|
995
|
+
* @experimental
|
|
996
|
+
*/
|
|
997
|
+
function createSandboxToolPartState() {
|
|
998
|
+
return {
|
|
999
|
+
statusByCall: /* @__PURE__ */ new Map(),
|
|
1000
|
+
seq: 0
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
/** Statuses that settle one tool call as a failure. A failed tool call stays a tool result:
|
|
1004
|
+
* the run's terminal state comes from the public Sandbox outcome tracker, never from here. */
|
|
1005
|
+
const TERMINAL_FAILURE = /^(error|errored|failed|failure|cancelled|canceled|timeout|timed_out)$/i;
|
|
1006
|
+
/**
|
|
1007
|
+
* Project one `SandboxEvent` onto the `tool_call` / `tool_result` variants of
|
|
1008
|
+
* `RuntimeStreamEvent` — the tool-part projection `mapSandboxEvent`
|
|
1009
|
+
* deliberately does NOT perform. Opt-in and additive: `mapSandboxEvent`'s
|
|
1010
|
+
* default vocabulary (text/reasoning deltas + `llm_call`) is unchanged;
|
|
1011
|
+
* consumers that need the tool surface (chat UIs rendering tool activity)
|
|
1012
|
+
* compose this projector alongside it — `streamAgentTurn` does exactly that
|
|
1013
|
+
* under its `preserveToolParts` option.
|
|
1014
|
+
*
|
|
1015
|
+
* Handled shapes (observed on the opencode / claude-code sandbox backends):
|
|
1016
|
+
* - `message.part.updated` with `part.type === 'tool'` — stateful: a
|
|
1017
|
+
* `tool_call` on the call id's first frame (args from `state.input` or
|
|
1018
|
+
* `state.metadata.input`), a `tool_result` when the status transitions to
|
|
1019
|
+
* `completed` (result from `state.output` / `metadata.output`) or to a
|
|
1020
|
+
* terminal failure (result is `{ error, status, output? }` — the error
|
|
1021
|
+
* surfaced in-band, never dropped).
|
|
1022
|
+
* - bare `tool*` event types (`tool.call`, `tool_result`, …) — stateless:
|
|
1023
|
+
* `*result*` types project to `tool_result`, the rest to `tool_call`.
|
|
1024
|
+
* - codex `raw` events whose inner `item.started` / `item.updated` /
|
|
1025
|
+
* `item.completed` carries a command, MCP, collaboration, web search or
|
|
1026
|
+
* file change item — stateful on the item id, like a tool part. Other raw
|
|
1027
|
+
* events, including opencode's raw `tool_use` echoes, project nothing.
|
|
1028
|
+
*
|
|
1029
|
+
* Returns `[]` for every non-tool event.
|
|
1030
|
+
*
|
|
1031
|
+
* @experimental
|
|
1032
|
+
*/
|
|
1033
|
+
function mapSandboxToolEvent(event, state) {
|
|
1034
|
+
if (!event || typeof event !== "object") return [];
|
|
1035
|
+
const type = String(event.type ?? "");
|
|
1036
|
+
const data = event.data && typeof event.data === "object" ? event.data : {};
|
|
1037
|
+
if (type === "message.part.updated") {
|
|
1038
|
+
const part = data.part && typeof data.part === "object" ? data.part : {};
|
|
1039
|
+
if (String(part.type ?? "") !== "tool") return [];
|
|
1040
|
+
return projectToolPart(part, state, typeof event.id === "string" ? event.id : void 0);
|
|
1041
|
+
}
|
|
1042
|
+
if (type === "raw") return projectCodexItem(data, state);
|
|
1043
|
+
if (CANONICAL_STREAM_EVENT_TYPES.has(type)) return [];
|
|
1044
|
+
if (type.includes("tool")) {
|
|
1045
|
+
const callId = pickString(data, [
|
|
1046
|
+
"toolCallId",
|
|
1047
|
+
"tool_use_id",
|
|
1048
|
+
"id"
|
|
1049
|
+
]) ?? (typeof event.id === "string" ? event.id : void 0) ?? `sandbox-tool-${++state.seq}`;
|
|
1050
|
+
const toolName = pickString(data, [
|
|
1051
|
+
"name",
|
|
1052
|
+
"toolName",
|
|
1053
|
+
"tool"
|
|
1054
|
+
]) ?? "sandbox_tool";
|
|
1055
|
+
if (type.includes("result")) return [{
|
|
1056
|
+
type: "tool_result",
|
|
1057
|
+
toolName,
|
|
1058
|
+
toolCallId: callId,
|
|
1059
|
+
result: data.output ?? data.result ?? data.content ?? data
|
|
1060
|
+
}];
|
|
1061
|
+
return [{
|
|
1062
|
+
type: "tool_call",
|
|
1063
|
+
toolName,
|
|
1064
|
+
toolCallId: callId,
|
|
1065
|
+
args: data.input ?? data.args ?? {}
|
|
1066
|
+
}];
|
|
1067
|
+
}
|
|
1068
|
+
return [];
|
|
1069
|
+
}
|
|
1070
|
+
function projectToolPart(part, state, eventId) {
|
|
1071
|
+
const callId = pickString(part, [
|
|
1072
|
+
"callID",
|
|
1073
|
+
"callId",
|
|
1074
|
+
"toolCallId",
|
|
1075
|
+
"id"
|
|
1076
|
+
]) ?? eventId ?? `sandbox-tool-${++state.seq}`;
|
|
1077
|
+
const toolName = pickString(part, [
|
|
1078
|
+
"tool",
|
|
1079
|
+
"toolName",
|
|
1080
|
+
"name"
|
|
1081
|
+
]) ?? "sandbox_tool";
|
|
1082
|
+
const toolState = part.state && typeof part.state === "object" ? part.state : {};
|
|
1083
|
+
const metadata = toolState.metadata && typeof toolState.metadata === "object" ? toolState.metadata : {};
|
|
1084
|
+
const status = pickString(toolState, ["status"]) ?? "updated";
|
|
1085
|
+
const previous = state.statusByCall.get(callId);
|
|
1086
|
+
if (previous === "completed" || previous !== void 0 && TERMINAL_FAILURE.test(previous)) return [];
|
|
1087
|
+
const out = [];
|
|
1088
|
+
if (previous === void 0) out.push({
|
|
1089
|
+
type: "tool_call",
|
|
1090
|
+
toolName,
|
|
1091
|
+
toolCallId: callId,
|
|
1092
|
+
args: toolState.input ?? metadata.input ?? {}
|
|
1093
|
+
});
|
|
1094
|
+
state.statusByCall.set(callId, status);
|
|
1095
|
+
if (status === "completed") out.push({
|
|
1096
|
+
type: "tool_result",
|
|
1097
|
+
toolName,
|
|
1098
|
+
toolCallId: callId,
|
|
1099
|
+
result: toolState.output ?? metadata.output ?? ""
|
|
1100
|
+
});
|
|
1101
|
+
else if (TERMINAL_FAILURE.test(status)) {
|
|
1102
|
+
const message = pickString(toolState, ["error", "message"]) ?? pickString(metadata, ["error", "message"]) ?? `sandbox tool ended with status ${status}`;
|
|
1103
|
+
const output = toolState.output ?? metadata.output;
|
|
1104
|
+
out.push({
|
|
1105
|
+
type: "tool_result",
|
|
1106
|
+
toolName,
|
|
1107
|
+
toolCallId: callId,
|
|
1108
|
+
result: {
|
|
1109
|
+
error: message,
|
|
1110
|
+
status,
|
|
1111
|
+
...output !== void 0 ? { output } : {}
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
return out;
|
|
1116
|
+
}
|
|
1117
|
+
/** Codex item types that are tool calls. Reasoning, messages and todo lists are not. */
|
|
1118
|
+
const CODEX_TOOL_ITEMS = /* @__PURE__ */ new Set([
|
|
1119
|
+
"command_execution",
|
|
1120
|
+
"mcp_tool_call",
|
|
1121
|
+
"collab_tool_call",
|
|
1122
|
+
"web_search",
|
|
1123
|
+
"file_change"
|
|
1124
|
+
]);
|
|
1125
|
+
/** Item fields that describe the call's progress or outcome rather than what it was asked to do. */
|
|
1126
|
+
const CODEX_ITEM_OUTCOME_FIELDS = /* @__PURE__ */ new Set([
|
|
1127
|
+
"id",
|
|
1128
|
+
"type",
|
|
1129
|
+
"status",
|
|
1130
|
+
"result",
|
|
1131
|
+
"error",
|
|
1132
|
+
"aggregated_output",
|
|
1133
|
+
"exit_code"
|
|
1134
|
+
]);
|
|
1135
|
+
/**
|
|
1136
|
+
* Project a codex `item.started` / `item.updated` / `item.completed` tool item through the same
|
|
1137
|
+
* per-call state as a tool part, keyed on the item id: one `tool_call` on its first frame and one
|
|
1138
|
+
* `tool_result` when it completes. Tool names follow Eval's codex intake: `server/tool` for MCP.
|
|
1139
|
+
*/
|
|
1140
|
+
function projectCodexItem(data, state) {
|
|
1141
|
+
if (data.backend !== "codex" || !data.event || typeof data.event !== "object") return [];
|
|
1142
|
+
const inner = data.event;
|
|
1143
|
+
const phase = inner.type;
|
|
1144
|
+
if (phase !== "item.started" && phase !== "item.updated" && phase !== "item.completed") return [];
|
|
1145
|
+
if (!inner.item || typeof inner.item !== "object") return [];
|
|
1146
|
+
const item = inner.item;
|
|
1147
|
+
const itemType = pickString(item, ["type"]);
|
|
1148
|
+
if (itemType === void 0 || !CODEX_TOOL_ITEMS.has(itemType)) return [];
|
|
1149
|
+
const error = item.error && typeof item.error === "object" ? pickString(item.error, ["message"]) ?? `${itemType} failed` : void 0;
|
|
1150
|
+
const failed = item.status === "failed" || error !== void 0 || itemType === "command_execution" && typeof item.exit_code === "number" && item.exit_code !== 0;
|
|
1151
|
+
const status = phase !== "item.completed" ? "running" : failed ? "failed" : "completed";
|
|
1152
|
+
const input = Object.fromEntries(Object.entries(item).filter(([key]) => !CODEX_ITEM_OUTCOME_FIELDS.has(key)));
|
|
1153
|
+
const output = item.result ?? item.aggregated_output ?? item.changes;
|
|
1154
|
+
return projectToolPart({
|
|
1155
|
+
...typeof item.id === "string" ? { callID: item.id } : {},
|
|
1156
|
+
tool: codexToolName(itemType, item),
|
|
1157
|
+
state: {
|
|
1158
|
+
status,
|
|
1159
|
+
input,
|
|
1160
|
+
...output === void 0 || output === null ? {} : { output },
|
|
1161
|
+
...status === "failed" ? { error: error ?? `${itemType} failed` } : {}
|
|
1162
|
+
}
|
|
1163
|
+
}, state, void 0);
|
|
1164
|
+
}
|
|
1165
|
+
function codexToolName(itemType, item) {
|
|
1166
|
+
if (itemType === "mcp_tool_call") {
|
|
1167
|
+
const name = [pickString(item, ["server"]), pickString(item, ["tool"])].filter((part) => part !== void 0).join("/");
|
|
1168
|
+
return name === "" ? "mcp" : name;
|
|
1169
|
+
}
|
|
1170
|
+
if (itemType === "collab_tool_call") return pickString(item, ["tool"]) ?? "subagent";
|
|
1171
|
+
if (itemType === "web_search" || itemType === "file_change") return itemType;
|
|
1172
|
+
return pickString(item, ["name"]) ?? itemType;
|
|
1173
|
+
}
|
|
1174
|
+
function pickString(data, keys) {
|
|
1175
|
+
for (const key of keys) {
|
|
1176
|
+
const value = data[key];
|
|
1177
|
+
if (typeof value === "string" && value.length > 0) return value;
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Project one `SandboxEvent` onto the `RuntimeStreamEvent` chat-UX vocabulary,
|
|
1182
|
+
* for runtimes that bridge a sandbox `streamPrompt` into the
|
|
1183
|
+
* `AgentRuntime.act` streaming contract. Returns `undefined` for events that
|
|
1184
|
+
* have no faithful projection — the raw stream is preserved separately for the
|
|
1185
|
+
* `OutputAdapter`, so an unmapped event never loses data.
|
|
1186
|
+
*
|
|
1187
|
+
* Mapped (the task-optional incremental variants — no synthesized task
|
|
1188
|
+
* lifecycle, no guessed tool-part shapes):
|
|
1189
|
+
* - `message.part.updated` text part → `text_delta`
|
|
1190
|
+
* - `message.part.updated` reasoning/thinking part → `reasoning_delta`
|
|
1191
|
+
* - cost-bearing events → `llm_call` (shared with the ledger extractor)
|
|
1192
|
+
*
|
|
1193
|
+
* Tool parts are deliberately NOT mapped here (unchanged default) — compose
|
|
1194
|
+
* {@link mapSandboxToolEvent} alongside when a consumer needs them.
|
|
1195
|
+
*
|
|
1196
|
+
* The opencode backend emits incremental text as
|
|
1197
|
+
* `{ type: 'message.part.updated', data: { part: { type, text }, delta } }`;
|
|
1198
|
+
* `delta` is the increment, `part.text` the running accumulation.
|
|
1199
|
+
*/
|
|
1200
|
+
function mapSandboxEvent(event, opts = {}) {
|
|
1201
|
+
if (!event || typeof event !== "object") return void 0;
|
|
1202
|
+
const type = String(event.type ?? "");
|
|
1203
|
+
const data = event.data && typeof event.data === "object" ? event.data : {};
|
|
1204
|
+
if (type === "message.part.updated") {
|
|
1205
|
+
const part = data.part && typeof data.part === "object" ? data.part : {};
|
|
1206
|
+
const partType = String(part.type ?? "");
|
|
1207
|
+
const text = (typeof data.delta === "string" ? data.delta : void 0) ?? (typeof part.text === "string" ? part.text : void 0);
|
|
1208
|
+
if (text === void 0) return void 0;
|
|
1209
|
+
if (partType === "text") return {
|
|
1210
|
+
type: "text_delta",
|
|
1211
|
+
text
|
|
1212
|
+
};
|
|
1213
|
+
if (partType === "reasoning" || partType === "thinking") return {
|
|
1214
|
+
type: "reasoning_delta",
|
|
1215
|
+
text
|
|
1216
|
+
};
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
return extractLlmCallEvent(event, opts.agentRunName ?? "agent");
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* Project one `SandboxEvent` onto Runtime's executor progress vocabulary: incremental text and
|
|
1223
|
+
* reasoning, tool calls and results, and an interaction request. It composes the existing
|
|
1224
|
+
* projections ({@link mapSandboxEvent}, {@link mapSandboxToolEvent}, and the canonical Agent
|
|
1225
|
+
* Interface decode) so every sandbox-shaped executor publishes live output through one reader.
|
|
1226
|
+
* Usage-bearing events project to nothing here — accounting stays on the `tokens`/`cost`
|
|
1227
|
+
* channels.
|
|
1228
|
+
*
|
|
1229
|
+
* Pass one {@link SandboxToolPartState} per turn so a multi-frame tool call yields one call and
|
|
1230
|
+
* at most one result.
|
|
1231
|
+
*
|
|
1232
|
+
* @experimental
|
|
1233
|
+
*/
|
|
1234
|
+
function sandboxProgressEvents(event, state) {
|
|
1235
|
+
const canonical = canonicalStreamEventFromSandboxEvent(event);
|
|
1236
|
+
if (canonical?.type === "interaction") return [{
|
|
1237
|
+
kind: "interaction",
|
|
1238
|
+
request: canonical.request
|
|
1239
|
+
}];
|
|
1240
|
+
if (canonical?.type === "child-task") return [{
|
|
1241
|
+
kind: "child_task",
|
|
1242
|
+
event: canonical
|
|
1243
|
+
}];
|
|
1244
|
+
const tools = mapSandboxToolEvent(event, state).map((projected) => projected.type === "tool_call" ? {
|
|
1245
|
+
kind: "tool_call",
|
|
1246
|
+
toolName: projected.toolName,
|
|
1247
|
+
...projected.toolCallId === void 0 ? {} : { toolCallId: projected.toolCallId },
|
|
1248
|
+
...projected.args === void 0 ? {} : { args: projected.args }
|
|
1249
|
+
} : {
|
|
1250
|
+
kind: "tool_result",
|
|
1251
|
+
toolName: projected.toolName,
|
|
1252
|
+
...projected.toolCallId === void 0 ? {} : { toolCallId: projected.toolCallId },
|
|
1253
|
+
...projected.result === void 0 ? {} : { result: projected.result }
|
|
1254
|
+
});
|
|
1255
|
+
if (tools.length > 0) return tools;
|
|
1256
|
+
const mapped = mapSandboxEvent(event);
|
|
1257
|
+
if (mapped?.type === "text_delta") return [{
|
|
1258
|
+
kind: "text_delta",
|
|
1259
|
+
text: mapped.text
|
|
1260
|
+
}];
|
|
1261
|
+
if (mapped?.type === "reasoning_delta") return [{
|
|
1262
|
+
kind: "reasoning_delta",
|
|
1263
|
+
text: mapped.text
|
|
1264
|
+
}];
|
|
1265
|
+
return [];
|
|
1266
|
+
}
|
|
1267
|
+
/** Project the complete Sandbox event stream into the standard executor result shape. */
|
|
1268
|
+
function sandboxLeafOutputFromEvents(events) {
|
|
1269
|
+
const content = collectAgentFinalMessageText(events) ?? collectAgentResponseText(events);
|
|
1270
|
+
const toolCalls = sandboxToolCalls(events);
|
|
1271
|
+
const servedBackend = lastServedBackend(events);
|
|
1272
|
+
return {
|
|
1273
|
+
events,
|
|
1274
|
+
content,
|
|
1275
|
+
output: sandboxOutputMarker(content, events.some(carriesText)),
|
|
1276
|
+
...servedBackend === void 0 ? {} : { servedBackend },
|
|
1277
|
+
...toolCalls.length === 0 ? {} : { toolCalls }
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* Classify observed text into the explicit output marker. `sawTextEvent` says whether the stream
|
|
1282
|
+
* carried a text-bearing event at all, which is what separates a box that answered nothing from a
|
|
1283
|
+
* box whose answer was never observed.
|
|
1284
|
+
*/
|
|
1285
|
+
function sandboxOutputMarker(content, sawTextEvent = content !== void 0) {
|
|
1286
|
+
if (content !== void 0 && content.length > 0) return {
|
|
1287
|
+
kind: "text",
|
|
1288
|
+
bytes: Buffer.byteLength(content, "utf8")
|
|
1289
|
+
};
|
|
1290
|
+
return sawTextEvent ? { kind: "empty" } : { kind: "absent" };
|
|
1291
|
+
}
|
|
1292
|
+
/** True when the event carries a text field a turn's answer could come from, empty or not. */
|
|
1293
|
+
function carriesText(event) {
|
|
1294
|
+
if (!event || typeof event !== "object") return false;
|
|
1295
|
+
const data = event.data && typeof event.data === "object" ? event.data : {};
|
|
1296
|
+
if (typeof data.finalText === "string" || typeof data.text === "string") return true;
|
|
1297
|
+
if (typeof data.delta === "string") return true;
|
|
1298
|
+
const part = data.part && typeof data.part === "object" ? data.part : void 0;
|
|
1299
|
+
return part !== void 0 && part.type === "text" && typeof part.text === "string";
|
|
1300
|
+
}
|
|
1301
|
+
/** The last served identity the platform reported across a turn's events, or `undefined`. */
|
|
1302
|
+
function lastServedBackend(events) {
|
|
1303
|
+
let served;
|
|
1304
|
+
for (const event of events) {
|
|
1305
|
+
const next = sandboxEventServedBackend(event);
|
|
1306
|
+
if (next !== void 0) served = next;
|
|
1307
|
+
}
|
|
1308
|
+
return served;
|
|
1309
|
+
}
|
|
1310
|
+
function sandboxToolCalls(events) {
|
|
1311
|
+
const state = createSandboxToolPartState();
|
|
1312
|
+
return events.flatMap((event) => mapSandboxToolEvent(event, state).flatMap((projected) => projected.type === "tool_call" ? [{
|
|
1313
|
+
...projected.toolCallId === void 0 ? {} : { id: projected.toolCallId },
|
|
1314
|
+
name: projected.toolName,
|
|
1315
|
+
arguments: projected.args ?? {}
|
|
1316
|
+
}] : []));
|
|
1317
|
+
}
|
|
1318
|
+
/** Read the final text off a terminal sandbox event, when present. */
|
|
1319
|
+
function terminalTextFromSandboxEvent(event) {
|
|
1320
|
+
if (!event || typeof event !== "object") return void 0;
|
|
1321
|
+
if (!isSandboxTerminalEvent(String(event.type ?? ""))) return void 0;
|
|
1322
|
+
const data = event.data && typeof event.data === "object" ? event.data : {};
|
|
1323
|
+
for (const key of [
|
|
1324
|
+
"finalText",
|
|
1325
|
+
"text",
|
|
1326
|
+
"response",
|
|
1327
|
+
"content"
|
|
1328
|
+
]) {
|
|
1329
|
+
const value = data[key];
|
|
1330
|
+
if (typeof value === "string") return value;
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
// src/runtime/supervise/spend-validation.ts
|
|
1334
|
+
/** Reject malformed ceilings before they can mint capacity through negative reservations. */
|
|
1335
|
+
function assertValidBudget(budget, label = "budget") {
|
|
1336
|
+
const safeInteger = (value, field) => {
|
|
1337
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new Error(`${label}.${field} must be a non-negative safe integer`);
|
|
1338
|
+
};
|
|
1339
|
+
const finiteNonNegative = (value, field) => {
|
|
1340
|
+
if (!Number.isFinite(value) || value < 0) throw new Error(`${label}.${field} must be a non-negative finite number`);
|
|
1341
|
+
};
|
|
1342
|
+
assertResources(budget.resources, "limit", `${label}.resources`);
|
|
1343
|
+
safeInteger(budget.maxIterations, "maxIterations");
|
|
1344
|
+
safeInteger(budget.maxTokens, "maxTokens");
|
|
1345
|
+
if (budget.maxUsd !== void 0) finiteNonNegative(budget.maxUsd, "maxUsd");
|
|
1346
|
+
if (budget.deadlineMs !== void 0) finiteNonNegative(budget.deadlineMs, "deadlineMs");
|
|
1347
|
+
}
|
|
1348
|
+
function assertValidSpend(spend, label) {
|
|
1349
|
+
assertResources(spend.resources, "amount", `${label}.resources`);
|
|
1350
|
+
if (!Number.isSafeInteger(spend.iterations) || spend.iterations < 0) throw new Error(`${label}.iterations must be a non-negative safe integer`);
|
|
1351
|
+
for (const [field, value] of [
|
|
1352
|
+
["input", spend.tokens.input],
|
|
1353
|
+
["output", spend.tokens.output],
|
|
1354
|
+
["freshInput", spend.tokens.freshInput],
|
|
1355
|
+
["cacheRead", spend.tokens.cacheRead],
|
|
1356
|
+
["cacheWrite", spend.tokens.cacheWrite]
|
|
1357
|
+
]) {
|
|
1358
|
+
if (value === void 0) continue;
|
|
1359
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new Error(`${label}.tokens.${field} must be a non-negative safe integer`);
|
|
1360
|
+
}
|
|
1361
|
+
for (const [field, value] of [["tokensKnown", spend.tokens.tokensKnown], ["cacheBreakdownKnown", spend.tokens.cacheBreakdownKnown]]) if (value !== void 0 && value !== false) throw new Error(`${label}.tokens.${field} must be false when present`);
|
|
1362
|
+
const { freshInput, cacheRead, cacheWrite } = spend.tokens;
|
|
1363
|
+
if (freshInput !== void 0 && cacheRead !== void 0 && cacheWrite !== void 0) {
|
|
1364
|
+
const classified = freshInput + cacheRead + cacheWrite;
|
|
1365
|
+
if (spend.tokens.cacheBreakdownKnown !== false) {
|
|
1366
|
+
if (classified !== spend.tokens.input) throw new Error(`${label}.tokens cache classes must sum to input`);
|
|
1367
|
+
} else if (classified > spend.tokens.input) throw new Error(`${label}.tokens cache classes must not exceed input`);
|
|
1368
|
+
}
|
|
1369
|
+
for (const [field, value] of [["usd", spend.usd], ["ms", spend.ms]]) if (!Number.isFinite(value) || value < 0) throw new Error(`${label}.${field} must be a non-negative finite number`);
|
|
1370
|
+
if (spend.usdEstimated !== void 0) {
|
|
1371
|
+
if (!Number.isFinite(spend.usdEstimated) || spend.usdEstimated < 0) throw new Error(`${label}.usdEstimated must be a non-negative finite number`);
|
|
1372
|
+
if (spend.usdEstimated > spend.usd) throw new Error(`${label}.usdEstimated must not exceed ${label}.usd`);
|
|
1373
|
+
if (spend.usdKnown !== false) throw new Error(`${label}.usdEstimated requires ${label}.usdKnown false`);
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
// src/runtime/supervise/budget-ledger.ts
|
|
1377
|
+
/**
|
|
1378
|
+
* The balances of one conserved budget pool, and the arithmetic that moves them. `createBudgetPool`
|
|
1379
|
+
* (`./budget`) owns tickets, the wait queue and the public contract; this module owns the numbers:
|
|
1380
|
+
* `total ≡ free + reserved + committed` per channel, the taint flags that say a balance is a
|
|
1381
|
+
* ceiling rather than a measurement, and the named-resource states.
|
|
1382
|
+
*/
|
|
1383
|
+
function openBudgetLedger(root, runStartedAtMs) {
|
|
1384
|
+
return {
|
|
1385
|
+
usdCapped: root.maxUsd !== void 0,
|
|
1386
|
+
absoluteDeadlineMs: root.deadlineMs === void 0 ? 0 : runStartedAtMs + root.deadlineMs,
|
|
1387
|
+
resources: new Map(Object.entries(root.resources ?? {}).map(([name, value]) => [name, {
|
|
1388
|
+
...value,
|
|
1389
|
+
remaining: value.limit,
|
|
1390
|
+
reserved: 0,
|
|
1391
|
+
committed: 0,
|
|
1392
|
+
known: true
|
|
1393
|
+
}])),
|
|
1394
|
+
freeTokens: root.maxTokens,
|
|
1395
|
+
reservedTokens: 0,
|
|
1396
|
+
committedTokens: 0,
|
|
1397
|
+
tokensTainted: false,
|
|
1398
|
+
cacheBreakdownTainted: false,
|
|
1399
|
+
freeUsd: root.maxUsd ?? 0,
|
|
1400
|
+
reservedUsd: 0,
|
|
1401
|
+
committedUsd: 0,
|
|
1402
|
+
usdTainted: false,
|
|
1403
|
+
usdMeasured: true,
|
|
1404
|
+
freeIterations: root.maxIterations,
|
|
1405
|
+
reservedIterations: 0,
|
|
1406
|
+
committedIterations: 0
|
|
1407
|
+
};
|
|
1408
|
+
}
|
|
1409
|
+
function ledgerValidateUnits(l, spend) {
|
|
1410
|
+
for (const [name, value] of Object.entries(spend.resources ?? {})) {
|
|
1411
|
+
const limit = l.resources.get(name);
|
|
1412
|
+
if (limit && limit.unit !== value.unit) throw new Error(`resource ${name}: unit mismatch`);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
/** Record that `spend` carried an unreported or unclassified channel. */
|
|
1416
|
+
function noteUnknowns(l, spend) {
|
|
1417
|
+
if (spend.tokensKnown === false) l.tokensTainted = true;
|
|
1418
|
+
if (!hasCompleteCacheBreakdown(spend.tokens)) l.cacheBreakdownTainted = true;
|
|
1419
|
+
if (spend.usdKnown === false) l.usdMeasured = false;
|
|
1420
|
+
}
|
|
1421
|
+
/**
|
|
1422
|
+
* Commit named-resource spend and classify it. `allocations` is the settling reservation's own
|
|
1423
|
+
* allocations, or `undefined` for spend holding no reservation (`observe`, restore). Unverifiable
|
|
1424
|
+
* usage is a `fault` (the remaining balance is consumed so admission closes); measured usage above
|
|
1425
|
+
* an allocation is `overspent`; without allocations, usage leaving a root limit overdrawn is
|
|
1426
|
+
* `rootExceeded`. A reservation within its own allocation is never charged with a root overdraw:
|
|
1427
|
+
* a negative balance at its settlement was caused, and reported, by earlier spend.
|
|
1428
|
+
*/
|
|
1429
|
+
function ledgerCommitResources(l, spend, allocations, requireAll = true) {
|
|
1430
|
+
let fault;
|
|
1431
|
+
let rootExceeded;
|
|
1432
|
+
const overspent = [];
|
|
1433
|
+
for (const [name, state] of l.resources) {
|
|
1434
|
+
const value = spend.resources?.[name];
|
|
1435
|
+
if (!requireAll && value === void 0) continue;
|
|
1436
|
+
const allocation = allocations?.[name]?.limit ?? 0;
|
|
1437
|
+
const amount = value?.amount ?? 0;
|
|
1438
|
+
state.reserved -= allocation;
|
|
1439
|
+
const committed = state.committed + amount;
|
|
1440
|
+
const overflow = !Number.isSafeInteger(committed);
|
|
1441
|
+
state.committed = Math.min(committed, Number.MAX_SAFE_INTEGER);
|
|
1442
|
+
state.remaining = state.limit - state.reserved - state.committed;
|
|
1443
|
+
if (value?.known !== true || overflow) {
|
|
1444
|
+
state.known = false;
|
|
1445
|
+
const retained = Math.max(0, state.remaining);
|
|
1446
|
+
state.committed += retained;
|
|
1447
|
+
state.remaining -= retained;
|
|
1448
|
+
fault ??= overflow ? `resource ${name}: amount overflow under an enforced limit` : `resource ${name}: unknown usage under an enforced limit`;
|
|
1449
|
+
} else if (allocations !== void 0) {
|
|
1450
|
+
if (amount > allocation) overspent.push({
|
|
1451
|
+
channel: `resource:${name}`,
|
|
1452
|
+
reserved: allocation,
|
|
1453
|
+
spent: amount
|
|
1454
|
+
});
|
|
1455
|
+
} else if (state.remaining < 0) rootExceeded ??= `resource ${name}: exceeded root limit ${state.limit}`;
|
|
1456
|
+
}
|
|
1457
|
+
return {
|
|
1458
|
+
...fault ? { fault } : {},
|
|
1459
|
+
overspent,
|
|
1460
|
+
...rootExceeded ? { rootExceeded } : {}
|
|
1461
|
+
};
|
|
1462
|
+
}
|
|
1463
|
+
/** Every channel `b` does not fit, measured against the free balance less `keep`. */
|
|
1464
|
+
function ledgerShortfalls(l, b, keep) {
|
|
1465
|
+
const wantTokens = b.maxTokens;
|
|
1466
|
+
const wantUsd = b.maxUsd ?? 0;
|
|
1467
|
+
const wantIterations = b.maxIterations;
|
|
1468
|
+
const shortfalls = [];
|
|
1469
|
+
const short = (channel, requested, free, held, closedByUnknownSpend = false) => {
|
|
1470
|
+
shortfalls.push({
|
|
1471
|
+
channel,
|
|
1472
|
+
requested,
|
|
1473
|
+
free: closedByUnknownSpend ? 0 : Math.max(0, free),
|
|
1474
|
+
...closedByUnknownSpend || held <= 0 ? {} : { held },
|
|
1475
|
+
...closedByUnknownSpend ? { closedByUnknownSpend: true } : {}
|
|
1476
|
+
});
|
|
1477
|
+
};
|
|
1478
|
+
for (const [name, state] of l.resources) {
|
|
1479
|
+
const limit = b.resources[name].limit;
|
|
1480
|
+
if (!state.known) short(`resource:${name}`, limit, 0, 0, true);
|
|
1481
|
+
else if (limit > state.remaining) short(`resource:${name}`, limit, state.remaining, state.reserved);
|
|
1482
|
+
}
|
|
1483
|
+
if (l.usdCapped && l.usdTainted) short("usd", wantUsd, 0, 0, true);
|
|
1484
|
+
const tokensFree = l.freeTokens - (keep?.tokens ?? 0);
|
|
1485
|
+
const iterationsFree = l.freeIterations - (keep?.iterations ?? 0);
|
|
1486
|
+
if (wantTokens > tokensFree) short("tokens", wantTokens, tokensFree, l.reservedTokens);
|
|
1487
|
+
if (wantIterations > iterationsFree) short("iterations", wantIterations, iterationsFree, l.reservedIterations);
|
|
1488
|
+
const usdFree = l.freeUsd - (keep?.usd ?? 0);
|
|
1489
|
+
if (l.usdCapped && !l.usdTainted && wantUsd > usdFree) short("usd", wantUsd, usdFree, l.reservedUsd);
|
|
1490
|
+
return shortfalls;
|
|
1491
|
+
}
|
|
1492
|
+
/** Move `b` from free to reserved. The caller has checked that it fits. */
|
|
1493
|
+
function ledgerTake(l, b) {
|
|
1494
|
+
for (const [name, state] of l.resources) {
|
|
1495
|
+
const amount = b.resources[name].limit;
|
|
1496
|
+
state.remaining -= amount;
|
|
1497
|
+
state.reserved += amount;
|
|
1498
|
+
}
|
|
1499
|
+
l.freeTokens -= b.maxTokens;
|
|
1500
|
+
l.reservedTokens += b.maxTokens;
|
|
1501
|
+
l.freeIterations -= b.maxIterations;
|
|
1502
|
+
l.reservedIterations += b.maxIterations;
|
|
1503
|
+
const wantUsd = b.maxUsd ?? 0;
|
|
1504
|
+
if (wantUsd > 0) {
|
|
1505
|
+
l.freeUsd -= wantUsd;
|
|
1506
|
+
l.reservedUsd += wantUsd;
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
/**
|
|
1510
|
+
* The overspends a settlement of `spent` against `held` decides, before any balance moves. An
|
|
1511
|
+
* OVERSPEND is measured spend above the reservation; unknown dollars under a dollar cap (a fault
|
|
1512
|
+
* the caller reports) are flagged `unknownUnderCap`. `usdBudgeted` is whether the child declared a
|
|
1513
|
+
* dollar ceiling: without `maxUsd` it reserved NO dollars, so under a capped root its dollars are
|
|
1514
|
+
* OBSERVED spend, committed and debited from `free` like `observe`d inference.
|
|
1515
|
+
*/
|
|
1516
|
+
function ledgerClassifyOverspend(l, held, usdBudgeted, spent) {
|
|
1517
|
+
const unknownUnderCap = l.usdCapped && spent.usdKnown === false;
|
|
1518
|
+
const spentTokens = chargedTokens(spent.tokens);
|
|
1519
|
+
const overspent = [];
|
|
1520
|
+
if (spentTokens > held.tokens) overspent.push({
|
|
1521
|
+
channel: "tokens",
|
|
1522
|
+
reserved: held.tokens,
|
|
1523
|
+
spent: spentTokens
|
|
1524
|
+
});
|
|
1525
|
+
if (spent.iterations > held.iterations) overspent.push({
|
|
1526
|
+
channel: "iterations",
|
|
1527
|
+
reserved: held.iterations,
|
|
1528
|
+
spent: spent.iterations
|
|
1529
|
+
});
|
|
1530
|
+
if (!unknownUnderCap && l.usdCapped && usdBudgeted && spent.usd > held.usd) overspent.push({
|
|
1531
|
+
channel: "usd",
|
|
1532
|
+
reserved: held.usd,
|
|
1533
|
+
spent: spent.usd
|
|
1534
|
+
});
|
|
1535
|
+
return {
|
|
1536
|
+
overspent,
|
|
1537
|
+
unknownUnderCap
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
/**
|
|
1541
|
+
* Release the whole reservation `held`, then commit actual spend; the difference is the refund
|
|
1542
|
+
* that flows back to `free`. Unconditional arithmetic: nothing here throws on spend it can read.
|
|
1543
|
+
*/
|
|
1544
|
+
function ledgerSettle(l, held, spent, unknownUnderCap) {
|
|
1545
|
+
noteUnknowns(l, spent);
|
|
1546
|
+
const spentTokens = chargedTokens(spent.tokens);
|
|
1547
|
+
l.reservedTokens -= held.tokens;
|
|
1548
|
+
l.committedTokens += spentTokens;
|
|
1549
|
+
l.freeTokens += held.tokens - spentTokens;
|
|
1550
|
+
l.reservedIterations -= held.iterations;
|
|
1551
|
+
l.committedIterations += spent.iterations;
|
|
1552
|
+
l.freeIterations += held.iterations - spent.iterations;
|
|
1553
|
+
if (l.usdCapped) {
|
|
1554
|
+
l.reservedUsd -= held.usd;
|
|
1555
|
+
l.committedUsd += spent.usd;
|
|
1556
|
+
if (unknownUnderCap) {
|
|
1557
|
+
l.usdTainted = true;
|
|
1558
|
+
l.freeUsd = 0;
|
|
1559
|
+
} else l.freeUsd += held.usd - spent.usd;
|
|
1560
|
+
} else l.committedUsd += spent.usd;
|
|
1561
|
+
return ledgerCommitResources(l, spent, held.resources ?? {});
|
|
1562
|
+
}
|
|
1563
|
+
/**
|
|
1564
|
+
* Direct free → committed debit (no ticket). `free` may go negative on overspend: the readout then
|
|
1565
|
+
* reports exhaustion and the in-loop guard halts the driver. Work with an unreported token count
|
|
1566
|
+
* still debits what IS known and marks the balance incomplete; refusing to record it is the "free
|
|
1567
|
+
* turn" the pool must never see.
|
|
1568
|
+
*/
|
|
1569
|
+
function ledgerObserve(l, spend, unknownUnderCap, requireAllResources) {
|
|
1570
|
+
noteUnknowns(l, spend);
|
|
1571
|
+
const tokens = chargedTokens(spend.tokens);
|
|
1572
|
+
l.freeTokens -= tokens;
|
|
1573
|
+
l.committedTokens += tokens;
|
|
1574
|
+
l.freeIterations -= spend.iterations;
|
|
1575
|
+
l.committedIterations += spend.iterations;
|
|
1576
|
+
l.committedUsd += spend.usd;
|
|
1577
|
+
if (l.usdCapped) l.freeUsd -= spend.usd;
|
|
1578
|
+
if (unknownUnderCap) l.usdTainted = true;
|
|
1579
|
+
return ledgerCommitResources(l, spend, void 0, requireAllResources);
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
* Reconstruct the balances from a prior process's durable record. Measured prior spend is debited
|
|
1583
|
+
* exactly once; a started-without-terminal child is charged at its entire reservation, so restart
|
|
1584
|
+
* never mints capacity and the false known flags stay visible. Direct arithmetic, not `observe`:
|
|
1585
|
+
* the prior spend ALREADY happened, and refusing to record it would be a zero-cost restart.
|
|
1586
|
+
*/
|
|
1587
|
+
function ledgerRestore(l, root, restore) {
|
|
1588
|
+
if (restore.committed !== void 0) {
|
|
1589
|
+
const committed = restore.committed;
|
|
1590
|
+
assertValidSpend(committed, "budget restore committed");
|
|
1591
|
+
ledgerValidateUnits(l, committed);
|
|
1592
|
+
ledgerCommitResources(l, committed);
|
|
1593
|
+
noteUnknowns(l, committed);
|
|
1594
|
+
const tokens = chargedTokens(committed.tokens);
|
|
1595
|
+
l.freeTokens -= tokens;
|
|
1596
|
+
l.committedTokens += tokens;
|
|
1597
|
+
l.freeIterations -= committed.iterations;
|
|
1598
|
+
l.committedIterations += committed.iterations;
|
|
1599
|
+
l.committedUsd += committed.usd;
|
|
1600
|
+
if (l.usdCapped) {
|
|
1601
|
+
l.freeUsd -= committed.usd;
|
|
1602
|
+
if (committed.usdKnown === false) {
|
|
1603
|
+
l.usdTainted = true;
|
|
1604
|
+
if (l.freeUsd > 0) l.committedUsd += l.freeUsd;
|
|
1605
|
+
l.freeUsd = 0;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
for (const [index, uncertain] of (restore.uncertainReservations ?? []).entries()) {
|
|
1610
|
+
assertValidBudget(uncertain, `budget restore uncertainReservations[${index}]`);
|
|
1611
|
+
ledgerCommitResources(l, withBudgetResources({
|
|
1612
|
+
iterations: 0,
|
|
1613
|
+
tokens: zeroTokenUsage(),
|
|
1614
|
+
usd: 0,
|
|
1615
|
+
ms: 0
|
|
1616
|
+
}, root));
|
|
1617
|
+
l.tokensTainted = true;
|
|
1618
|
+
l.usdMeasured = false;
|
|
1619
|
+
l.freeTokens -= uncertain.maxTokens;
|
|
1620
|
+
l.committedTokens += uncertain.maxTokens;
|
|
1621
|
+
l.freeIterations -= uncertain.maxIterations;
|
|
1622
|
+
l.committedIterations += uncertain.maxIterations;
|
|
1623
|
+
if (!l.usdCapped) continue;
|
|
1624
|
+
if (uncertain.maxUsd === void 0) {
|
|
1625
|
+
l.usdTainted = true;
|
|
1626
|
+
l.committedUsd += l.freeUsd > 0 ? l.freeUsd : 0;
|
|
1627
|
+
l.freeUsd = 0;
|
|
1628
|
+
} else {
|
|
1629
|
+
l.freeUsd -= uncertain.maxUsd;
|
|
1630
|
+
l.committedUsd += uncertain.maxUsd;
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
// src/runtime/supervise/usage-metering.ts
|
|
1635
|
+
/** Fold a normalized `UsageEvent` array into a `Spend`. Tokens and usd are separate
|
|
1636
|
+
* channels; iterations come from `'iteration'` events. Pure; `ms` stays zero (the
|
|
1637
|
+
* pool does not read wall-clock). */
|
|
1638
|
+
function spendFromUsageEvents(events) {
|
|
1639
|
+
const totals = newUsageTotals();
|
|
1640
|
+
for (const ev of events) meterUsageEvent(totals, ev);
|
|
1641
|
+
return spendFromUsageTotals(totals);
|
|
1642
|
+
}
|
|
1643
|
+
/** Fresh accounting totals, optionally restored from verified committed spend. */
|
|
1644
|
+
function newUsageTotals(priorSpent) {
|
|
1645
|
+
if (priorSpent !== void 0) assertValidSpend(priorSpent, "prior committed usage");
|
|
1646
|
+
return {
|
|
1647
|
+
...addResourceSpend(priorSpent?.resources),
|
|
1648
|
+
tokens: priorSpent === void 0 ? zeroTokenUsage() : cloneTokenUsage(priorSpent.tokens),
|
|
1649
|
+
tokensKnown: priorSpent?.tokensKnown !== false,
|
|
1650
|
+
...priorSpent?.tokensProvenance === void 0 ? {} : { tokensProvenance: priorSpent.tokensProvenance },
|
|
1651
|
+
usd: priorSpent?.usd ?? 0,
|
|
1652
|
+
usdEstimated: priorSpent?.usdEstimated ?? 0,
|
|
1653
|
+
usdKnown: priorSpent?.usdKnown !== false,
|
|
1654
|
+
iterations: priorSpent?.iterations ?? 0
|
|
1655
|
+
};
|
|
1656
|
+
}
|
|
1657
|
+
/**
|
|
1658
|
+
* Fold ONE usage event into running totals — the single place that decides which event kinds
|
|
1659
|
+
* meter. `progress` carries observed output rather than accounting, so it meters nothing; only
|
|
1660
|
+
* `iteration` advances the iteration count.
|
|
1661
|
+
*/
|
|
1662
|
+
function meterUsageEvent(totals, ev) {
|
|
1663
|
+
if (ev.kind === "resource") {
|
|
1664
|
+
totals.resources = addResourceSpend(totals.resources, Object.fromEntries([[ev.name, {
|
|
1665
|
+
unit: ev.unit,
|
|
1666
|
+
amount: ev.amount,
|
|
1667
|
+
known: ev.known
|
|
1668
|
+
}]])).resources;
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
if (ev.kind === "tokens") {
|
|
1672
|
+
if (ev.mode === "cumulative") {
|
|
1673
|
+
if (ev.input < totals.tokens.input || ev.output < totals.tokens.output) throw new ValidationError("cumulative executor tokens must not decrease");
|
|
1674
|
+
const snapshot = zeroTokenUsage();
|
|
1675
|
+
addTokenUsage(snapshot, {
|
|
1676
|
+
...ev,
|
|
1677
|
+
...ev.freshInput === void 0 && totals.tokens.freshInput !== void 0 ? { freshInput: totals.tokens.freshInput } : {},
|
|
1678
|
+
...ev.cacheRead === void 0 && totals.tokens.cacheRead !== void 0 ? { cacheRead: totals.tokens.cacheRead } : {},
|
|
1679
|
+
...ev.cacheWrite === void 0 && totals.tokens.cacheWrite !== void 0 ? { cacheWrite: totals.tokens.cacheWrite } : {},
|
|
1680
|
+
...totals.tokens.tokensKnown === false ? { tokensKnown: false } : {}
|
|
1681
|
+
});
|
|
1682
|
+
totals.tokens = snapshot;
|
|
1683
|
+
} else addTokenUsage(totals.tokens, ev);
|
|
1684
|
+
if (ev.tokensKnown === false) totals.tokensKnown = false;
|
|
1685
|
+
const source = ev.provenance ?? "stream-receipt";
|
|
1686
|
+
totals.tokensProvenance = totals.tokensProvenance === void 0 || totals.tokensProvenance === source ? source : "mixed";
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1689
|
+
if (ev.kind === "cost") {
|
|
1690
|
+
totals.usd += ev.usd;
|
|
1691
|
+
if (ev.usdKnown === false) {
|
|
1692
|
+
totals.usdEstimated += ev.usdEstimated ?? 0;
|
|
1693
|
+
totals.usdKnown = false;
|
|
1694
|
+
}
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
if (ev.kind === "iteration") totals.iterations += 1;
|
|
1698
|
+
}
|
|
1699
|
+
/** Project folded totals onto the conserved-pool `Spend`. `ms` stays zero — the pool does not
|
|
1700
|
+
* read wall-clock. */
|
|
1701
|
+
function spendFromUsageTotals(totals) {
|
|
1702
|
+
return {
|
|
1703
|
+
...addResourceSpend(totals.resources),
|
|
1704
|
+
iterations: totals.iterations,
|
|
1705
|
+
tokens: totals.tokens,
|
|
1706
|
+
...totals.tokensKnown ? {} : { tokensKnown: false },
|
|
1707
|
+
usd: totals.usd,
|
|
1708
|
+
...totals.usdKnown ? {} : { usdKnown: false },
|
|
1709
|
+
...totals.usdEstimated > 0 ? { usdEstimated: totals.usdEstimated } : {},
|
|
1710
|
+
...totals.tokensProvenance === void 0 || totals.tokensProvenance === "stream-receipt" ? {} : { tokensProvenance: totals.tokensProvenance },
|
|
1711
|
+
ms: 0
|
|
1712
|
+
};
|
|
1713
|
+
}
|
|
1714
|
+
async function foldUsage(events) {
|
|
1715
|
+
if (Array.isArray(events)) return spendFromUsageEvents(events);
|
|
1716
|
+
const totals = newUsageTotals();
|
|
1717
|
+
for await (const ev of events) meterUsageEvent(totals, ev);
|
|
1718
|
+
return spendFromUsageTotals(totals);
|
|
1719
|
+
}
|
|
1720
|
+
// src/runtime/supervise/budget.ts
|
|
1721
|
+
/**
|
|
1722
|
+
* The conserved budget reservation pool — the invariant the whole instrument rests on (critique
|
|
1723
|
+
* M5/B3). One root `Budget` becomes a pool of standard quantities (tokens, usd, iterations) and
|
|
1724
|
+
* caller-named resources plus an absolute deadline. Children reserve atomically at spawn and
|
|
1725
|
+
* reconcile at settle:
|
|
1726
|
+
*
|
|
1727
|
+
* total ≡ free + reserved + committed (for every known quantity)
|
|
1728
|
+
*
|
|
1729
|
+
* `reserve` moves a child's whole ceiling `free` → `reserved` and fails closed when `free` can't
|
|
1730
|
+
* cover it (never read-then-spawn overcommit, so `Σk(treatment) ≡ Σk(blind)` by construction).
|
|
1731
|
+
* `reconcile` releases the reservation, commits ACTUAL spend, and refunds the remainder to `free`.
|
|
1732
|
+
* Tokens and usd are separate channels; iterations are conserved alongside them.
|
|
1733
|
+
*
|
|
1734
|
+
* The token unit is `chargedTokens`: every token counted ONCE, when it first enters the context
|
|
1735
|
+
* (`input − cacheRead + output`, which is `freshInput + cacheWrite + output` under a complete
|
|
1736
|
+
* split). A cached prefix was already charged when written; charging a cache read again made a
|
|
1737
|
+
* declared cap bite about 56x early on a real fleet (cache is 98% of the rolled-up prompt total).
|
|
1738
|
+
* No price weight enters the token channel; money is budgeted on `maxUsd`. The unit is additive,
|
|
1739
|
+
* so a rolled-up report agrees with the pool. Prompt tokens the provider never classified are
|
|
1740
|
+
* charged in full and `readout().cacheBreakdownKnown` reads false (an upper bound on newly-
|
|
1741
|
+
* presented work). The pool trusts reported usage: it is an accounting unit, not a trust boundary.
|
|
1742
|
+
*
|
|
1743
|
+
* Named resources require explicit units, ceilings, and complete executor measurements; missing
|
|
1744
|
+
* or unknown enforced measurements close admission for that resource. The pool trusts those
|
|
1745
|
+
* receipts and meters no external system. `Spend.boxMinutes` is evidence only, with no implicit
|
|
1746
|
+
* resource ceiling; never present estimated box lifetime as a measured resource receipt.
|
|
1747
|
+
*
|
|
1748
|
+
* Pure and deterministic: the run's start instant is supplied; no I/O, wall-clock or RNG read. A
|
|
1749
|
+
* ticket is single-use (fail-loud on double or unknown reconcile). Reconciling an OPEN ticket
|
|
1750
|
+
* always closes it: a fail-loud condition settles the reservation first and throws afterwards, so
|
|
1751
|
+
* no error path strands a reservation past `assertNoOpenTickets`. Measured spend above a
|
|
1752
|
+
* reservation is not fail-loud: the pool commits it, returns a `BudgetViolation`, and refuses
|
|
1753
|
+
* later reservations from the reduced free balance. A child that declared no `maxUsd` reserved no
|
|
1754
|
+
* dollars, so its real dollars are committed as observed spend against the root, not as an
|
|
1755
|
+
* overspend of a $0 ceiling. Unknowable dollar cost under a dollar limit closes the known
|
|
1756
|
+
* token/iteration work, marks the dollar channel unusable and refuses later reservations. An
|
|
1757
|
+
* unknowable TOKEN count records the work, debits what is known, and marks
|
|
1758
|
+
* `readout().tokensKnown` false (a ceiling, not a measurement); tokens are always capped, so one
|
|
1759
|
+
* unreported turn must not end the run.
|
|
1760
|
+
*
|
|
1761
|
+
* A reservation the free balance cannot cover now can WAIT (`reserve(b, holder, { wait: true })`)
|
|
1762
|
+
* when it would fit once open reservations return. It holds nothing while waiting, so
|
|
1763
|
+
* conservation is untouched; waiters are granted in asking order and refused once no open
|
|
1764
|
+
* reservation is left to return any.
|
|
1765
|
+
*
|
|
1766
|
+
* A pool can be RESTORED from a prior process's durable record: measured committed spend is
|
|
1767
|
+
* debited once, and each child journaled as started but never settled is charged at its full
|
|
1768
|
+
* declared ceiling. Restart never mints capacity, unknown flags stay visible, and the original
|
|
1769
|
+
* absolute deadline never slides.
|
|
1770
|
+
*
|
|
1771
|
+
* @experimental
|
|
1772
|
+
*/
|
|
1773
|
+
/** A reconciliation whose spend the pool cannot verify, thrown after the reservation settled.
|
|
1774
|
+
* `budgetViolation` carries any measured overspend on the same settlement. */
|
|
1775
|
+
var BudgetReconcileFault = class extends Error {
|
|
1776
|
+
budgetViolation;
|
|
1777
|
+
constructor(message, budgetViolation) {
|
|
1778
|
+
super(message);
|
|
1779
|
+
this.name = "BudgetReconcileFault";
|
|
1780
|
+
if (budgetViolation !== void 0) this.budgetViolation = budgetViolation;
|
|
1781
|
+
}
|
|
1782
|
+
};
|
|
1783
|
+
/**
|
|
1784
|
+
* A waiting reservation that can never be granted: every open reservation settled and the free
|
|
1785
|
+
* balance still cannot cover it. `shortfalls` are the channels that did not fit at that moment.
|
|
1786
|
+
*/
|
|
1787
|
+
var ReservationWaitRefused = class extends Error {
|
|
1788
|
+
reason = "budget-exhausted";
|
|
1789
|
+
shortfalls;
|
|
1790
|
+
constructor(shortfalls) {
|
|
1791
|
+
super(`budget-exhausted: nothing left running could return enough budget for this worker (${shortfalls.map((shortfall) => `${shortfall.channel} requested ${shortfall.requested}, free ${shortfall.free}`).join("; ")})`);
|
|
1792
|
+
this.name = "ReservationWaitRefused";
|
|
1793
|
+
this.shortfalls = shortfalls;
|
|
1794
|
+
}
|
|
1795
|
+
};
|
|
1796
|
+
/**
|
|
1797
|
+
* Create a conserved reservation pool from a root `Budget`. `runStartedAtMs` is the WALL-CLOCK
|
|
1798
|
+
* instant the root was recorded; the deadline is `runStartedAtMs + root.deadlineMs` (`0` when
|
|
1799
|
+
* none), an absolute instant. A resumed pool passes the ORIGINAL instant.
|
|
1800
|
+
*/
|
|
1801
|
+
function createBudgetPool(root, runStartedAtMs, restore = {}) {
|
|
1802
|
+
assertValidBudget(root, "root budget");
|
|
1803
|
+
if (!Number.isFinite(runStartedAtMs) || runStartedAtMs < 0) throw new Error("budget runStartedAtMs must be a non-negative finite number");
|
|
1804
|
+
const ledger = openBudgetLedger(root, runStartedAtMs);
|
|
1805
|
+
let nextTicketId = 0;
|
|
1806
|
+
const open = /* @__PURE__ */ new Map();
|
|
1807
|
+
/** Tickets admitted to wait for budget, in the order they asked. Each is also in `open`. */
|
|
1808
|
+
const waiting = [];
|
|
1809
|
+
/** Open tickets that hold nothing: waiting, or refused and not yet reconciled. */
|
|
1810
|
+
const holdingNothing = /* @__PURE__ */ new Set();
|
|
1811
|
+
/** Grant waiting tickets in the order they asked while the head fits. When the head does not
|
|
1812
|
+
* fit and no reservation is held that could return budget, nothing can ever cover it: refuse
|
|
1813
|
+
* it, and go on with the next. */
|
|
1814
|
+
function grantWaiting() {
|
|
1815
|
+
while (waiting.length > 0) {
|
|
1816
|
+
const head = waiting[0];
|
|
1817
|
+
const shortfalls = ledgerShortfalls(ledger, head.budget, head.keep);
|
|
1818
|
+
if (shortfalls.length === 0) {
|
|
1819
|
+
waiting.shift();
|
|
1820
|
+
ledgerTake(ledger, head.budget);
|
|
1821
|
+
holdingNothing.delete(head.ticket.id);
|
|
1822
|
+
head.grant();
|
|
1823
|
+
continue;
|
|
1824
|
+
}
|
|
1825
|
+
if (open.size > holdingNothing.size) return;
|
|
1826
|
+
waiting.shift();
|
|
1827
|
+
head.refuse(new ReservationWaitRefused(shortfalls));
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
/** Admit `b` to wait: it holds nothing until settlements return enough, in the order asked. */
|
|
1831
|
+
function admitToWait(b, holder, keep) {
|
|
1832
|
+
const id = nextTicketId++;
|
|
1833
|
+
open.set(id, holder);
|
|
1834
|
+
const ticket = ticketFor(id, b);
|
|
1835
|
+
let grant;
|
|
1836
|
+
let refuse;
|
|
1837
|
+
const granted = new Promise((resolve, reject) => {
|
|
1838
|
+
grant = resolve;
|
|
1839
|
+
refuse = reject;
|
|
1840
|
+
});
|
|
1841
|
+
granted.catch(() => void 0);
|
|
1842
|
+
holdingNothing.add(id);
|
|
1843
|
+
waiting.push({
|
|
1844
|
+
ticket,
|
|
1845
|
+
budget: b,
|
|
1846
|
+
keep,
|
|
1847
|
+
grant,
|
|
1848
|
+
refuse
|
|
1849
|
+
});
|
|
1850
|
+
return {
|
|
1851
|
+
ok: true,
|
|
1852
|
+
ticket,
|
|
1853
|
+
granted
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
function reserve(b, holder = { stage: "admitted" }, options = {}) {
|
|
1857
|
+
assertValidBudget(b, "reservation budget");
|
|
1858
|
+
assertResourceDeclarations(ledger, b);
|
|
1859
|
+
const wantUsd = b.maxUsd ?? 0;
|
|
1860
|
+
const keep = options.keep;
|
|
1861
|
+
const shortfalls = ledgerShortfalls(ledger, b, keep);
|
|
1862
|
+
if (shortfalls.length === 0 && wantUsd > 0 && !ledger.usdCapped) return {
|
|
1863
|
+
ok: false,
|
|
1864
|
+
reason: "usd-unbudgeted"
|
|
1865
|
+
};
|
|
1866
|
+
const queueAhead = options.wait === true && waiting.length > 0;
|
|
1867
|
+
if (shortfalls.length === 0 && !queueAhead) {
|
|
1868
|
+
ledgerTake(ledger, b);
|
|
1869
|
+
const id = nextTicketId++;
|
|
1870
|
+
open.set(id, holder);
|
|
1871
|
+
return {
|
|
1872
|
+
ok: true,
|
|
1873
|
+
ticket: ticketFor(id, b)
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
if (options.wait !== true || !fitsAfterRefunds(shortfalls)) return {
|
|
1877
|
+
ok: false,
|
|
1878
|
+
reason: "budget-exhausted",
|
|
1879
|
+
shortfalls
|
|
1880
|
+
};
|
|
1881
|
+
return admitToWait(b, holder, keep);
|
|
1882
|
+
}
|
|
1883
|
+
function attribute(ticket, holder) {
|
|
1884
|
+
const current = open.get(ticket.id);
|
|
1885
|
+
if (current === void 0) return;
|
|
1886
|
+
open.set(ticket.id, {
|
|
1887
|
+
...current,
|
|
1888
|
+
...holder
|
|
1889
|
+
});
|
|
1890
|
+
}
|
|
1891
|
+
function reconcile(ticket, spent) {
|
|
1892
|
+
if (!open.has(ticket.id)) throw new Error(`budget pool: reconcile of unknown or already-settled ticket ${ticket.id}`);
|
|
1893
|
+
assertValidSpend(spent, `budget pool ticket ${ticket.id} spend`);
|
|
1894
|
+
ledgerValidateUnits(ledger, spent);
|
|
1895
|
+
const heldNothing = holdingNothing.delete(ticket.id);
|
|
1896
|
+
if (heldNothing) {
|
|
1897
|
+
const index = waiting.findIndex((entry) => entry.ticket.id === ticket.id);
|
|
1898
|
+
if (index >= 0) waiting.splice(index, 1);
|
|
1899
|
+
}
|
|
1900
|
+
const held = heldNothing ? {
|
|
1901
|
+
tokens: 0,
|
|
1902
|
+
usd: 0,
|
|
1903
|
+
iterations: 0,
|
|
1904
|
+
resources: void 0
|
|
1905
|
+
} : ticket.reserved;
|
|
1906
|
+
const usdBudgeted = ticket.reserved.usdBudgeted !== false;
|
|
1907
|
+
const { overspent, unknownUnderCap } = ledgerClassifyOverspend(ledger, held, usdBudgeted, spent);
|
|
1908
|
+
let fault = unknownUnderCap ? `ticket ${ticket.id} reported unknown dollar cost under a dollar-capped budget` : void 0;
|
|
1909
|
+
open.delete(ticket.id);
|
|
1910
|
+
const resourceSettlement = ledgerSettle(ledger, held, spent, unknownUnderCap);
|
|
1911
|
+
fault ??= resourceSettlement.fault;
|
|
1912
|
+
overspent.push(...resourceSettlement.overspent);
|
|
1913
|
+
grantWaiting();
|
|
1914
|
+
const violation = overspent.length === 0 ? void 0 : Object.freeze({ overspent: Object.freeze(overspent.map((entry) => Object.freeze(entry))) });
|
|
1915
|
+
if (fault !== void 0) throw new BudgetReconcileFault(`budget pool: ${fault}`, violation);
|
|
1916
|
+
return violation;
|
|
1917
|
+
}
|
|
1918
|
+
function observe(spend, options = {}) {
|
|
1919
|
+
assertValidSpend(spend, "observed spend");
|
|
1920
|
+
ledgerValidateUnits(ledger, spend);
|
|
1921
|
+
const unknownUnderCap = ledger.usdCapped && spend.usdKnown === false;
|
|
1922
|
+
if (unknownUnderCap && options.alreadySpent !== true) throw new ValidationError("budget pool: cannot observe unknown dollar cost under a dollar-capped budget");
|
|
1923
|
+
const settlement = ledgerObserve(ledger, spend, unknownUnderCap, !options.partial);
|
|
1924
|
+
const violation = settlement.fault ?? settlement.rootExceeded;
|
|
1925
|
+
if (violation) throw new ValidationError(`budget pool: ${violation}`);
|
|
1926
|
+
}
|
|
1927
|
+
function readout() {
|
|
1928
|
+
return {
|
|
1929
|
+
...ledger.resources.size ? { resources: Object.fromEntries([...ledger.resources].map(([name, value]) => [name, { ...value }])) } : {},
|
|
1930
|
+
tokensLeft: ledger.freeTokens,
|
|
1931
|
+
tokensKnown: !ledger.tokensTainted,
|
|
1932
|
+
cacheBreakdownKnown: !ledger.cacheBreakdownTainted,
|
|
1933
|
+
usdLeft: ledger.usdCapped ? ledger.usdTainted ? 0 : ledger.freeUsd : 0,
|
|
1934
|
+
usdCapped: ledger.usdCapped,
|
|
1935
|
+
usdKnown: ledger.usdMeasured && !ledger.usdTainted,
|
|
1936
|
+
iterationsLeft: ledger.freeIterations,
|
|
1937
|
+
deadlineMs: ledger.absoluteDeadlineMs,
|
|
1938
|
+
reservedTokens: ledger.reservedTokens
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
function openReservations() {
|
|
1942
|
+
return [...open].map(([ticketId, holder]) => Object.freeze({
|
|
1943
|
+
ticketId,
|
|
1944
|
+
...holder
|
|
1945
|
+
}));
|
|
1946
|
+
}
|
|
1947
|
+
function assertNoOpenTickets() {
|
|
1948
|
+
if (open.size > 0) throw new Error(`budget pool: ${open.size} reservation(s) still open at join barrier (${openReservations().map(describeLeakedReservation).join("; ")}) — conserved-pool invariant violated`);
|
|
1949
|
+
}
|
|
1950
|
+
ledgerRestore(ledger, root, restore);
|
|
1951
|
+
return {
|
|
1952
|
+
reserve,
|
|
1953
|
+
attribute,
|
|
1954
|
+
reconcile,
|
|
1955
|
+
spendFrom: foldUsage,
|
|
1956
|
+
readout,
|
|
1957
|
+
observe,
|
|
1958
|
+
assertNoOpenTickets,
|
|
1959
|
+
openReservations
|
|
1960
|
+
};
|
|
1961
|
+
}
|
|
1962
|
+
/** A reservation must name a limit for exactly the resources the root declared, in its units. */
|
|
1963
|
+
function assertResourceDeclarations(ledger, b) {
|
|
1964
|
+
for (const [name, state] of ledger.resources) {
|
|
1965
|
+
const wanted = b.resources?.[name];
|
|
1966
|
+
if (!wanted) throw new ValidationError(`resource ${name}: child must declare its limit`);
|
|
1967
|
+
if (wanted.unit !== state.unit) throw new ValidationError(`resource ${name}: unit mismatch`);
|
|
1968
|
+
}
|
|
1969
|
+
for (const name of Object.keys(b.resources ?? {})) if (!ledger.resources.has(name)) throw new ValidationError(`resource ${name}: root must declare its limit`);
|
|
1970
|
+
}
|
|
1971
|
+
/** Whether every short channel would fit once the open reservations return what they hold. */
|
|
1972
|
+
function fitsAfterRefunds(shortfalls) {
|
|
1973
|
+
return shortfalls.every((shortfall) => shortfall.closedByUnknownSpend !== true && shortfall.requested <= shortfall.free + (shortfall.held ?? 0));
|
|
1974
|
+
}
|
|
1975
|
+
function ticketFor(id, b) {
|
|
1976
|
+
return {
|
|
1977
|
+
id,
|
|
1978
|
+
reserved: {
|
|
1979
|
+
...b.resources === void 0 ? {} : { resources: Object.fromEntries(Object.entries(b.resources).map(([name, value]) => [name, { ...value }])) },
|
|
1980
|
+
tokens: b.maxTokens,
|
|
1981
|
+
usd: b.maxUsd ?? 0,
|
|
1982
|
+
iterations: b.maxIterations,
|
|
1983
|
+
usdBudgeted: b.maxUsd !== void 0
|
|
1984
|
+
}
|
|
1985
|
+
};
|
|
1986
|
+
}
|
|
1987
|
+
/** Name one leaked reservation by its holder. */
|
|
1988
|
+
function describeLeakedReservation(leak) {
|
|
1989
|
+
const parts = [`ticket ${leak.ticketId}`];
|
|
1990
|
+
if (leak.childId !== void 0) parts.push(`child ${leak.childId}`);
|
|
1991
|
+
if (leak.label !== void 0) parts.push(`label ${leak.label}`);
|
|
1992
|
+
if (leak.assignment !== void 0) parts.push(`assignment ${leak.assignment}`);
|
|
1993
|
+
parts.push(`stage ${leak.stage}`);
|
|
1994
|
+
return parts.join(", ");
|
|
1995
|
+
}
|
|
1996
|
+
// src/redact.ts
|
|
1997
|
+
/**
|
|
1998
|
+
* Redaction for values that may leave the Runtime process. The built-in
|
|
1999
|
+
* redactor is agent-eval's redaction core (`@tangle-network/agent-eval/traces`,
|
|
2000
|
+
* default profile): credentials found by field name or value shape are
|
|
2001
|
+
* replaced whole, email, card, SSN and phone values in place, and token counts
|
|
2002
|
+
* such as `inputTokens` or `max_tokens` are kept. A customer with
|
|
2003
|
+
* domain-specific PII supplies their own `redact` hook.
|
|
2004
|
+
*
|
|
2005
|
+
* This is narrower than `src/sanitize.ts`, which drops fields of the runtime's
|
|
2006
|
+
* *event envelope* unless the caller opts in: here the value is opaque customer
|
|
2007
|
+
* payload, so the scrub is value-shaped, not schema-shaped.
|
|
2008
|
+
*
|
|
2009
|
+
* @experimental
|
|
2010
|
+
*/
|
|
2011
|
+
/**
|
|
2012
|
+
* The built-in redactor. Cycle-safe, depth-bounded and total: it never throws
|
|
2013
|
+
* on customer input and never mutates it.
|
|
2014
|
+
*/
|
|
2015
|
+
function defaultRedactor(value) {
|
|
2016
|
+
return redact(value).value;
|
|
2017
|
+
}
|
|
2018
|
+
/** Stable identity input for saved work that depends on built-in redaction behavior. */
|
|
2019
|
+
function defaultRedactorIdentityMaterial() {
|
|
2020
|
+
return {
|
|
2021
|
+
core: "@tangle-network/agent-eval/traces redact",
|
|
2022
|
+
profile: "default",
|
|
2023
|
+
version: REDACTION_VERSION
|
|
2024
|
+
};
|
|
2025
|
+
}
|
|
2026
|
+
/**
|
|
2027
|
+
* Resolve the redactor a client uses. A caller-supplied hook handles
|
|
2028
|
+
* domain-specific values first, then the built-in scrubber still removes
|
|
2029
|
+
* credentials and personal data. Returning `false` is the explicit opt-out for
|
|
2030
|
+
* already-reviewed public values.
|
|
2031
|
+
*/
|
|
2032
|
+
function resolveRedactor(redact) {
|
|
2033
|
+
if (redact === false) return (value) => value;
|
|
2034
|
+
if (!redact) return defaultRedactor;
|
|
2035
|
+
return (value) => defaultRedactor(redact(value));
|
|
2036
|
+
}
|
|
2037
|
+
// src/runtime/supervise/error-message.ts
|
|
2038
|
+
const maxCauseDepth = 4;
|
|
2039
|
+
const maxMessageLength = 32768;
|
|
2040
|
+
const maxPartLength = 2048;
|
|
2041
|
+
/** A persisted failure must explain its wrappers without exporting credentials or unbounded data. */
|
|
2042
|
+
function errorText(value) {
|
|
2043
|
+
let text;
|
|
2044
|
+
try {
|
|
2045
|
+
text = typeof value === "string" ? value : JSON.stringify(value) ?? String(value);
|
|
2046
|
+
} catch {
|
|
2047
|
+
text = "[unprintable rejection]";
|
|
2048
|
+
}
|
|
2049
|
+
return String(defaultRedactor(text)).slice(0, maxMessageLength);
|
|
2050
|
+
}
|
|
2051
|
+
function errorProperty(error, field) {
|
|
2052
|
+
try {
|
|
2053
|
+
const value = error[field];
|
|
2054
|
+
return value === void 0 ? void 0 : errorText(value).slice(0, field === "name" ? 256 : maxMessageLength);
|
|
2055
|
+
} catch {
|
|
2056
|
+
return `[unreadable error ${field}]`;
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
/** An HTTP status carried on a thrown error, read without trusting its getter. Provider SDKs put
|
|
2060
|
+
* one on their own error classes, so this is how a refusal is recognised outside the transport
|
|
2061
|
+
* taxonomy — for the persisted message here, and for the driver's retry verdict. */
|
|
2062
|
+
function errorHttpStatus(error) {
|
|
2063
|
+
try {
|
|
2064
|
+
const status = Reflect.get(error, "status");
|
|
2065
|
+
return typeof status === "number" && Number.isInteger(status) && status >= 100 && status <= 599 ? status : void 0;
|
|
2066
|
+
} catch {
|
|
2067
|
+
return;
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
/** Shared by child settlements, driver attempts, and the final no-winner result. */
|
|
2071
|
+
function errMessage(error) {
|
|
2072
|
+
const parts = [];
|
|
2073
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2074
|
+
let current = error;
|
|
2075
|
+
for (let depth = 0; depth <= maxCauseDepth; depth += 1) try {
|
|
2076
|
+
if (!(current instanceof Error)) {
|
|
2077
|
+
parts.push(errorText(current).slice(0, maxPartLength));
|
|
2078
|
+
break;
|
|
2079
|
+
}
|
|
2080
|
+
if (seen.has(current)) {
|
|
2081
|
+
parts.push("[circular]");
|
|
2082
|
+
break;
|
|
2083
|
+
}
|
|
2084
|
+
seen.add(current);
|
|
2085
|
+
const status = errorHttpStatus(current);
|
|
2086
|
+
const message = (status === void 0 ? "" : `HTTP ${status}: `) + (errorProperty(current, "message") ?? "");
|
|
2087
|
+
parts.push((depth === 0 ? message : `${errorProperty(current, "name")}: ${message}`).slice(0, maxPartLength));
|
|
2088
|
+
current = current.cause;
|
|
2089
|
+
if (current === void 0 || current === null) break;
|
|
2090
|
+
if (depth === maxCauseDepth) parts.push("[cause chain truncated]");
|
|
2091
|
+
} catch {
|
|
2092
|
+
parts.push("[unreadable error cause]");
|
|
2093
|
+
break;
|
|
2094
|
+
}
|
|
2095
|
+
return parts.join(": caused by ").slice(0, maxMessageLength);
|
|
2096
|
+
}
|
|
2097
|
+
/** The error and its causes (`cause`, and `errors` members), bounded and cycle-safe. */
|
|
2098
|
+
function causeChain(error) {
|
|
2099
|
+
const chain = [];
|
|
2100
|
+
const queue = [error];
|
|
2101
|
+
while (queue.length > 0 && chain.length < 12) {
|
|
2102
|
+
const value = queue.shift();
|
|
2103
|
+
if (typeof value !== "object" || value === null || chain.includes(value)) continue;
|
|
2104
|
+
chain.push(value);
|
|
2105
|
+
try {
|
|
2106
|
+
const errors = Reflect.get(value, "errors");
|
|
2107
|
+
if (Array.isArray(errors)) queue.push(...errors);
|
|
2108
|
+
queue.push(Reflect.get(value, "cause"));
|
|
2109
|
+
} catch {}
|
|
2110
|
+
}
|
|
2111
|
+
return chain;
|
|
2112
|
+
}
|
|
2113
|
+
// src/runtime/supervise/credential-refusal.ts
|
|
2114
|
+
/**
|
|
2115
|
+
* The account owner refused an unsupported credential route before dispatch. This is the
|
|
2116
|
+
* provider SDK's machine contract, not an HTTP status, class-name guess, or prose match.
|
|
2117
|
+
*/
|
|
2118
|
+
function isCredentialRefusal(error) {
|
|
2119
|
+
if (!(error instanceof Error)) return false;
|
|
2120
|
+
try {
|
|
2121
|
+
return Object.getOwnPropertyDescriptor(error, "code")?.value === "model_credential_refused";
|
|
2122
|
+
} catch {
|
|
2123
|
+
return false;
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
/**
|
|
2127
|
+
* The message prefix of agent-provider-tangle's generic credential command failure. The provider
|
|
2128
|
+
* exports no type or constant for it, so the kernel credential test raises it through the
|
|
2129
|
+
* provider's own command resolver and fails when the message drifts.
|
|
2130
|
+
*/
|
|
2131
|
+
const CREDENTIAL_COMMAND_FAILURE = "Tangle credential command failed (";
|
|
2132
|
+
/**
|
|
2133
|
+
* The exit code of agent-provider-tangle's credential command (`acct sandbox resolve`) in the
|
|
2134
|
+
* error or its causes, or undefined.
|
|
2135
|
+
*
|
|
2136
|
+
* The provider reports the command's generic failure as `Tangle credential command failed
|
|
2137
|
+
* (exit N)` with the process's `exitCode` and `terminationSignal` as own fields; a refusal it can
|
|
2138
|
+
* parse (exit 75 or 78) arrives typed instead and is classified on its code. Before any dispatch
|
|
2139
|
+
* this failure ran nothing, and counting it as an unresolved pending invocation stopped 10 nodes
|
|
2140
|
+
* and lost 7 runs between 2026-10-12 and 2026-10-19 (test-acceptance-canary-20261015c met 26 in
|
|
2141
|
+
* 2.2 minutes). It pauses like a capacity refusal, bounded by the deadline, the budget and
|
|
2142
|
+
* cancellation as every pause is.
|
|
2143
|
+
*/
|
|
2144
|
+
function credentialCommandExit(error) {
|
|
2145
|
+
for (const link of causeChain(error)) try {
|
|
2146
|
+
if (!(link instanceof Error) || !link.message.startsWith(CREDENTIAL_COMMAND_FAILURE) || !Object.hasOwn(link, "terminationSignal")) continue;
|
|
2147
|
+
const exitCode = Reflect.get(link, "exitCode");
|
|
2148
|
+
if (typeof exitCode === "number" && Number.isInteger(exitCode) && exitCode > 0) return exitCode;
|
|
2149
|
+
} catch {}
|
|
2150
|
+
}
|
|
2151
|
+
// src/runtime/json-content-hash.ts
|
|
2152
|
+
/**
|
|
2153
|
+
* Stable hash for the trace payload. Not cryptographic — only used so
|
|
2154
|
+
* downstream eval pipelines can group iterations whose task / output is the
|
|
2155
|
+
* same. Bare structural hash; non-JSON values stringify via their `toString`.
|
|
2156
|
+
*/
|
|
2157
|
+
function hashJson(value) {
|
|
2158
|
+
const str = jsonContentText(value);
|
|
2159
|
+
return fnv1a32Hex(str);
|
|
2160
|
+
}
|
|
2161
|
+
/** JSON text with String fallback, including quoted JSON strings and cyclic values. */
|
|
2162
|
+
function jsonContentText(value) {
|
|
2163
|
+
let str;
|
|
2164
|
+
try {
|
|
2165
|
+
str = JSON.stringify(value) ?? String(value);
|
|
2166
|
+
} catch {
|
|
2167
|
+
str = String(value);
|
|
2168
|
+
}
|
|
2169
|
+
return str;
|
|
2170
|
+
}
|
|
2171
|
+
// src/runtime/supervise/executor-outcome.ts
|
|
2172
|
+
const outcomeSchema = AgentTurnResultSchema.pick({
|
|
2173
|
+
success: true,
|
|
2174
|
+
error: true
|
|
2175
|
+
}).extend({
|
|
2176
|
+
errorCode: AgentTurnResultSchema.shape.error,
|
|
2177
|
+
statusCode: z.number().int().min(400).max(599).optional()
|
|
2178
|
+
}).strict();
|
|
2179
|
+
/** Read only the executor envelope; application output and grades are not execution status. */
|
|
2180
|
+
function executorFailure(result) {
|
|
2181
|
+
if (result.outcome === void 0) return void 0;
|
|
2182
|
+
const outcome = outcomeSchema.parse(result.outcome);
|
|
2183
|
+
if (outcome.success) return void 0;
|
|
2184
|
+
return {
|
|
2185
|
+
error: outcome.error ?? "Agent execution failed",
|
|
2186
|
+
...outcome.errorCode === void 0 ? {} : { errorCode: outcome.errorCode },
|
|
2187
|
+
...outcome.statusCode === void 0 ? {} : { statusCode: outcome.statusCode }
|
|
2188
|
+
};
|
|
2189
|
+
}
|
|
2190
|
+
/** The failure text alone, for callers that record a settlement reason. */
|
|
2191
|
+
function executorFailureReason(result) {
|
|
2192
|
+
return executorFailure(result)?.error;
|
|
2193
|
+
}
|
|
2194
|
+
/**
|
|
2195
|
+
* The platform's own failure vocabulary: machine codes and lifecycle messages that the sandbox
|
|
2196
|
+
* runtime, the router, or the egress path put in an executor envelope when the platform, not
|
|
2197
|
+
* the agent's work, ended the execution. Each entry names its evidence: the count is how many
|
|
2198
|
+
* down children carried it on the 2026-09-20 fleet corpus (277 down settlements across 691
|
|
2199
|
+
* runs), every one of them stamped `infra: false` by the hard-coded call site this table
|
|
2200
|
+
* replaced. The table is deliberately the platform's fixed strings, not the agent's prose.
|
|
2201
|
+
*/
|
|
2202
|
+
const PLATFORM_FAILURE_CODES = new Set([
|
|
2203
|
+
"QUOTA_EXCEEDED",
|
|
2204
|
+
"provider_quota_exceeded",
|
|
2205
|
+
"provider_quota_exhausted",
|
|
2206
|
+
"buffer.overflow",
|
|
2207
|
+
"TIMEOUT",
|
|
2208
|
+
"ECONNRESET",
|
|
2209
|
+
"ECONNREFUSED",
|
|
2210
|
+
"ETIMEDOUT",
|
|
2211
|
+
"EPIPE",
|
|
2212
|
+
"EADDRINUSE"
|
|
2213
|
+
].map((code) => code.toLowerCase()));
|
|
2214
|
+
const PLATFORM_FAILURE_MESSAGES = [
|
|
2215
|
+
/^terminated$/u,
|
|
2216
|
+
/Execution exceeded its time limit/u,
|
|
2217
|
+
/Execution interrupted: the agent runtime restarted/u,
|
|
2218
|
+
/Platform key[- ]verification/u,
|
|
2219
|
+
/Platform key delegation failed/u,
|
|
2220
|
+
/Bad Gateway/iu,
|
|
2221
|
+
/Cannot connect to API/u,
|
|
2222
|
+
/Failed to connect to Sandbox API/u,
|
|
2223
|
+
/fetch failed/u,
|
|
2224
|
+
/A sandbox lifecycle operation is already in progress/u,
|
|
2225
|
+
/exceeded its string bound/u,
|
|
2226
|
+
/exceeds its JSON bound/u,
|
|
2227
|
+
/No provider served model/u,
|
|
2228
|
+
/Invalid API key/u,
|
|
2229
|
+
/provider_quota_exceeded|provider_quota_exhausted|QUOTA_EXCEEDED|rate limit/iu,
|
|
2230
|
+
/is at its box cap/u,
|
|
2231
|
+
/could not materialize the seat credential/u,
|
|
2232
|
+
/OpenCode exited while a model step was still in progress/u,
|
|
2233
|
+
/the agent runtime restarted/u
|
|
2234
|
+
];
|
|
2235
|
+
/**
|
|
2236
|
+
* Whether an executor envelope's failure was the platform's, as far as the envelope can say.
|
|
2237
|
+
*
|
|
2238
|
+
* `true` when the code or the message is in the platform's own vocabulary above. `undefined`
|
|
2239
|
+
* otherwise: the harness reported a failure and nothing in the envelope attributes it, so the
|
|
2240
|
+
* settlement carries no `infra` claim at all rather than a false one. This path never answers
|
|
2241
|
+
* `false`, because an executor envelope cannot prove the agent's work was at fault; the throw
|
|
2242
|
+
* path in `scope.ts` still answers `false` for an ordinary thrown result, where the runtime does
|
|
2243
|
+
* know. Until this function existed both envelope sites stamped `infra: false` unconditionally,
|
|
2244
|
+
* and the flag caught 1 of the 78 platform losses it was checked against on the fleet corpus.
|
|
2245
|
+
*/
|
|
2246
|
+
function executorFailureInfra(failure) {
|
|
2247
|
+
const code = failure.errorCode?.toLowerCase();
|
|
2248
|
+
if (code !== void 0 && PLATFORM_FAILURE_CODES.has(code)) return true;
|
|
2249
|
+
return PLATFORM_FAILURE_MESSAGES.some((pattern) => pattern.test(failure.error)) ? true : void 0;
|
|
2250
|
+
}
|
|
2251
|
+
/** Preserve deterministic result pointers across built-in executors and replay.
|
|
2252
|
+
* FNV-1a is an identity convention here, not a cryptographic integrity check. */
|
|
2253
|
+
function contentRef(prefix, value) {
|
|
2254
|
+
const str = jsonContentText(value);
|
|
2255
|
+
return `${prefix}:${fnv1a32Hex(str)}`;
|
|
2256
|
+
}
|
|
2257
|
+
// src/durable/content-address.ts
|
|
2258
|
+
/** Stable content address shared by result and trace artifacts. */
|
|
2259
|
+
function contentAddress(artifact) {
|
|
2260
|
+
return `sha256:${createHash("sha256").update(stableStringify(artifact), "utf-8").digest("hex")}`;
|
|
2261
|
+
}
|
|
2262
|
+
function stableStringify(value) {
|
|
2263
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
|
|
2264
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
2265
|
+
return `{${Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(",")}}`;
|
|
2266
|
+
}
|
|
2267
|
+
// src/runtime/supervise/snapshot.ts
|
|
2268
|
+
/** Deeply detach and freeze untrusted data at a runtime decision boundary. The clone prevents the
|
|
2269
|
+
* caller from mutating it later; the freeze prevents downstream code from mutating the snapshot.
|
|
2270
|
+
* Strings are immutable, so the snapshot shares the caller's strings instead of copying them. */
|
|
2271
|
+
function detachedSnapshot(value, context) {
|
|
2272
|
+
let clone;
|
|
2273
|
+
try {
|
|
2274
|
+
clone = structuredClone(value);
|
|
2275
|
+
} catch (error) {
|
|
2276
|
+
throw new ValidationError(`${context}: input must be structured-cloneable`, { cause: error });
|
|
2277
|
+
}
|
|
2278
|
+
return deepFreeze(shareSourceStrings(value, clone));
|
|
2279
|
+
}
|
|
2280
|
+
/** Detach a value from its caller and freeze it, without the `ValidationError` framing
|
|
2281
|
+
* {@link detachedSnapshot} adds. For a boundary that has already validated its input. */
|
|
2282
|
+
function detachedFrozen(value) {
|
|
2283
|
+
return deepFreeze(shareSourceStrings(value, structuredClone(value)));
|
|
2284
|
+
}
|
|
2285
|
+
/**
|
|
2286
|
+
* Point each string in a fresh structured clone at the equal string in its source.
|
|
2287
|
+
*
|
|
2288
|
+
* `structuredClone` copies every string, and a copied string buys no isolation: only objects and
|
|
2289
|
+
* arrays can change through a shared reference. A spawned profile passes several boundaries, and
|
|
2290
|
+
* each one kept its own copy: in a reproduction of Discovery's hosted coordinator, a child profile
|
|
2291
|
+
* that mounts a 5.3 MB file packet was held four times per spawned child. A scope keeps every
|
|
2292
|
+
* child it spawned, so the heap grew about 22 MB per spawn, and hosted coordinators died with V8
|
|
2293
|
+
* heap OOM after 37 spawns.
|
|
2294
|
+
*
|
|
2295
|
+
* The walk pairs only own data properties of plain objects and arrays. Accessors, Maps, Sets and
|
|
2296
|
+
* other cloned types keep the clone's values. A string is replaced only by a string equal to it,
|
|
2297
|
+
* so the snapshot holds exactly the clone's value. The snapshot keeps the source's strings alive.
|
|
2298
|
+
*/
|
|
2299
|
+
function shareSourceStrings(source, clone) {
|
|
2300
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2301
|
+
const visit = (from, to) => {
|
|
2302
|
+
if (from === null || to === null || typeof from !== "object" || typeof to !== "object") return;
|
|
2303
|
+
const array = Array.isArray(to);
|
|
2304
|
+
if (array !== Array.isArray(from)) return;
|
|
2305
|
+
if (!array && Object.getPrototypeOf(to) !== Object.prototype) return;
|
|
2306
|
+
if (visited.has(to)) return;
|
|
2307
|
+
visited.add(to);
|
|
2308
|
+
const target = to;
|
|
2309
|
+
for (const key of Object.keys(target)) {
|
|
2310
|
+
const own = Object.getOwnPropertyDescriptor(from, key);
|
|
2311
|
+
if (own === void 0 || !("value" in own)) continue;
|
|
2312
|
+
const cloned = target[key];
|
|
2313
|
+
if (typeof cloned === "string") {
|
|
2314
|
+
if (own.value === cloned) target[key] = own.value;
|
|
2315
|
+
} else visit(own.value, cloned);
|
|
2316
|
+
}
|
|
2317
|
+
};
|
|
2318
|
+
visit(source, clone);
|
|
2319
|
+
return clone;
|
|
2320
|
+
}
|
|
2321
|
+
/**
|
|
2322
|
+
* Replace each string in a freshly parsed JSON value with an equal string already in `pool`, and
|
|
2323
|
+
* add the new ones. One recovery pass parses every interrupted child's profile from its own blob,
|
|
2324
|
+
* so children that carry the same file packet would otherwise hold one copy each. The value is
|
|
2325
|
+
* updated in place, so it must belong to the caller; frozen objects are left as they are.
|
|
2326
|
+
*/
|
|
2327
|
+
function shareParsedStrings(value, pool) {
|
|
2328
|
+
const visit = (node) => {
|
|
2329
|
+
if (node === null || typeof node !== "object" || Object.isFrozen(node)) return;
|
|
2330
|
+
if (!Array.isArray(node) && Object.getPrototypeOf(node) !== Object.prototype) return;
|
|
2331
|
+
const target = node;
|
|
2332
|
+
for (const key of Object.keys(target)) {
|
|
2333
|
+
const item = target[key];
|
|
2334
|
+
if (typeof item !== "string") {
|
|
2335
|
+
visit(item);
|
|
2336
|
+
continue;
|
|
2337
|
+
}
|
|
2338
|
+
const shared = pool.get(item);
|
|
2339
|
+
if (shared === void 0) pool.set(item, item);
|
|
2340
|
+
else target[key] = shared;
|
|
2341
|
+
}
|
|
2342
|
+
};
|
|
2343
|
+
visit(value);
|
|
2344
|
+
return value;
|
|
2345
|
+
}
|
|
2346
|
+
function deepFreeze(value, seen = /* @__PURE__ */ new Set()) {
|
|
2347
|
+
if (value === null || typeof value !== "object" || ArrayBuffer.isView(value) || seen.has(value)) return value;
|
|
2348
|
+
seen.add(value);
|
|
2349
|
+
for (const child of Object.values(value)) deepFreeze(child, seen);
|
|
2350
|
+
return Object.freeze(value);
|
|
2351
|
+
}
|
|
2352
|
+
// src/runtime/supervise/trace-root.ts
|
|
2353
|
+
/**
|
|
2354
|
+
* Trace roots: one Merkle root over the events a turn archived, so a single event can be proven
|
|
2355
|
+
* against the hash its journal record carries without reading the rest of the archive.
|
|
2356
|
+
*
|
|
2357
|
+
* The leaves are the archived events in stored order, each hashed with its own random salt, then
|
|
2358
|
+
* one summary leaf that counts the events and the tool calls among them. The artifact keeps every
|
|
2359
|
+
* leaf and salt and is stored under its content address in the run's result blob store, beside
|
|
2360
|
+
* the archive it commits to. A verifier recomputes leaf `i` from `events[i]` of the `outRef` blob
|
|
2361
|
+
* and `salts[i]`, checks it against `leaves[i]`, and checks `merkleRoot(leaves)` against the root
|
|
2362
|
+
* the journal recorded. Leaf identity is `(outRef, index)`: an event's own `id` is a session
|
|
2363
|
+
* buffer offset and is absent on terminal events.
|
|
2364
|
+
*
|
|
2365
|
+
* One event array has one root per node. A result always gets its own. Evidence whose events the
|
|
2366
|
+
* node already rooted gets none: a retained owner's deferred workspace capture stores the turn it
|
|
2367
|
+
* just committed again, with the workspace fields added, under another `outRef`. A second root
|
|
2368
|
+
* over the same events would give one turn two count candidates and two run-root leaves.
|
|
2369
|
+
*
|
|
2370
|
+
* Salts are random, so a trace root can never be recomputed from the archive alone. Recovery and
|
|
2371
|
+
* replay copy the recorded receipt; they never derive a new one.
|
|
2372
|
+
*/
|
|
2373
|
+
/** Schema identifier of a stored {@link TraceRootArtifact}. */
|
|
2374
|
+
const TRACE_ROOT_SCHEMA = "agent-runtime.trace-root.v1";
|
|
2375
|
+
/** Schema identifier of the {@link TraceSummary} carried by a trace root's last leaf. */
|
|
2376
|
+
const TRACE_SUMMARY_SCHEMA = "agent-runtime.trace-summary.v1";
|
|
2377
|
+
/** @internal */
|
|
2378
|
+
function traceRootWriter(blobs, nodeId) {
|
|
2379
|
+
const roots = /* @__PURE__ */ new Map();
|
|
2380
|
+
/** Digests of the event arrays this node holds a stored root for. */
|
|
2381
|
+
const rooted = /* @__PURE__ */ new Set();
|
|
2382
|
+
const persist = async (outRef, out, evidence) => {
|
|
2383
|
+
const events = archivedEvents(out);
|
|
2384
|
+
if (events === void 0) return void 0;
|
|
2385
|
+
if (evidence) {
|
|
2386
|
+
await Promise.allSettled([...roots.values()]);
|
|
2387
|
+
if (rooted.has(await eventsDigest(events))) return void 0;
|
|
2388
|
+
}
|
|
2389
|
+
const stored = await persistTraceRoot(blobs, nodeId, outRef, events);
|
|
2390
|
+
if (stored === void 0) return void 0;
|
|
2391
|
+
rooted.add(stored.digest);
|
|
2392
|
+
return stored.receipt;
|
|
2393
|
+
};
|
|
2394
|
+
const memoized = (outRef, out, evidence) => {
|
|
2395
|
+
const known = roots.get(outRef);
|
|
2396
|
+
if (known !== void 0) return known;
|
|
2397
|
+
const pending = persist(outRef, out, evidence);
|
|
2398
|
+
roots.set(outRef, pending);
|
|
2399
|
+
pending.catch(() => roots.delete(outRef));
|
|
2400
|
+
return pending;
|
|
2401
|
+
};
|
|
2402
|
+
return {
|
|
2403
|
+
write: (outRef, out) => memoized(outRef, out, false),
|
|
2404
|
+
writeEvidence: (outRef, out) => memoized(outRef, out, true),
|
|
2405
|
+
async recorded(outRef) {
|
|
2406
|
+
if (outRef === void 0) return void 0;
|
|
2407
|
+
return roots.get(outRef)?.catch(() => void 0);
|
|
2408
|
+
}
|
|
2409
|
+
};
|
|
2410
|
+
}
|
|
2411
|
+
async function persistTraceRoot(blobs, nodeId, outRef, events) {
|
|
2412
|
+
let built;
|
|
2413
|
+
try {
|
|
2414
|
+
built = await traceRootArtifact(nodeId, outRef, events);
|
|
2415
|
+
} catch (error) {
|
|
2416
|
+
if (error instanceof LedgerCanonicalizationError) return void 0;
|
|
2417
|
+
throw error;
|
|
2418
|
+
}
|
|
2419
|
+
const { artifact, digest } = built;
|
|
2420
|
+
await nextTurn();
|
|
2421
|
+
const ref = contentAddress(artifact);
|
|
2422
|
+
await nextTurn();
|
|
2423
|
+
await blobs.put(ref, artifact);
|
|
2424
|
+
return {
|
|
2425
|
+
receipt: {
|
|
2426
|
+
root: artifact.root,
|
|
2427
|
+
ref,
|
|
2428
|
+
leafCount: artifact.leafCount
|
|
2429
|
+
},
|
|
2430
|
+
digest
|
|
2431
|
+
};
|
|
2432
|
+
}
|
|
2433
|
+
function archivedEvents(out) {
|
|
2434
|
+
if (out === null || typeof out !== "object" || Array.isArray(out)) return void 0;
|
|
2435
|
+
const events = out.events;
|
|
2436
|
+
return Array.isArray(events) ? events : void 0;
|
|
2437
|
+
}
|
|
2438
|
+
/** The longest stretch of hashing between yields to the event loop. Hashing runs about 40 µs per
|
|
2439
|
+
* event, and the largest archived turn holds 65,276 events: unbroken, that blocked the process
|
|
2440
|
+
* for 2.7 s while sibling turns, coordination calls and heartbeats waited. A slice is measured
|
|
2441
|
+
* in time, not events, because one event can be megabytes. */
|
|
2442
|
+
const SLICE_MS = 10;
|
|
2443
|
+
/** Yields to the event loop once a slice has run `SLICE_MS`; `undefined` while it has not. */
|
|
2444
|
+
function slicer() {
|
|
2445
|
+
let started = performance.now();
|
|
2446
|
+
return () => {
|
|
2447
|
+
if (performance.now() - started < SLICE_MS) return void 0;
|
|
2448
|
+
return nextTurn().then(() => {
|
|
2449
|
+
started = performance.now();
|
|
2450
|
+
});
|
|
2451
|
+
};
|
|
2452
|
+
}
|
|
2453
|
+
function nextTurn() {
|
|
2454
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
2455
|
+
}
|
|
2456
|
+
/** The identity of an event array as stored: the digest of each event's stored JSON, in order.
|
|
2457
|
+
* Two arrays with one digest have the same leaves' content. */
|
|
2458
|
+
async function eventsDigest(events) {
|
|
2459
|
+
const hash = createHash("sha256");
|
|
2460
|
+
const slice = slicer();
|
|
2461
|
+
for (const event of events) {
|
|
2462
|
+
const yielded = slice();
|
|
2463
|
+
if (yielded !== void 0) await yielded;
|
|
2464
|
+
hash.update(storedText(event)).update("\n");
|
|
2465
|
+
}
|
|
2466
|
+
return hash.digest("hex");
|
|
2467
|
+
}
|
|
2468
|
+
async function traceRootArtifact(nodeId, outRef, archived) {
|
|
2469
|
+
const salts = [];
|
|
2470
|
+
const leaves = [];
|
|
2471
|
+
const tree = merkleTree();
|
|
2472
|
+
const identity = createHash("sha256");
|
|
2473
|
+
const tools = createSandboxToolPartState();
|
|
2474
|
+
const toolCalls = /* @__PURE__ */ new Map();
|
|
2475
|
+
const slice = slicer();
|
|
2476
|
+
for (const event of archived) {
|
|
2477
|
+
const yielded = slice();
|
|
2478
|
+
if (yielded !== void 0) await yielded;
|
|
2479
|
+
const text = storedText(event);
|
|
2480
|
+
identity.update(text).update("\n");
|
|
2481
|
+
const stored = JSON.parse(text);
|
|
2482
|
+
const salt = merkleSalt();
|
|
2483
|
+
const leaf = merkleLeafHash(canonicalString(jsonDocument(stored)), salt);
|
|
2484
|
+
salts.push(salt);
|
|
2485
|
+
leaves.push(leaf);
|
|
2486
|
+
tree.push(leaf);
|
|
2487
|
+
for (const projected of mapSandboxToolEvent(stored, tools)) {
|
|
2488
|
+
if (projected.type !== "tool_call") continue;
|
|
2489
|
+
toolCalls.set(projected.toolName, (toolCalls.get(projected.toolName) ?? 0) + 1);
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
const summary = {
|
|
2493
|
+
schema: TRACE_SUMMARY_SCHEMA,
|
|
2494
|
+
events: archived.length,
|
|
2495
|
+
toolCalls: Object.fromEntries(toolCalls)
|
|
2496
|
+
};
|
|
2497
|
+
const salt = merkleSalt();
|
|
2498
|
+
const leaf = merkleLeafHash(canonicalString(summary), salt);
|
|
2499
|
+
salts.push(salt);
|
|
2500
|
+
leaves.push(leaf);
|
|
2501
|
+
tree.push(leaf);
|
|
2502
|
+
return {
|
|
2503
|
+
artifact: {
|
|
2504
|
+
schema: TRACE_ROOT_SCHEMA,
|
|
2505
|
+
nodeId,
|
|
2506
|
+
outRef,
|
|
2507
|
+
root: tree.root(),
|
|
2508
|
+
leafCount: leaves.length,
|
|
2509
|
+
salts,
|
|
2510
|
+
leaves,
|
|
2511
|
+
summary
|
|
2512
|
+
},
|
|
2513
|
+
digest: identity.digest("hex")
|
|
2514
|
+
};
|
|
2515
|
+
}
|
|
2516
|
+
/**
|
|
2517
|
+
* `merkleRoot(leaves)` built as the leaves arrive, so its interior hashes run inside the sliced
|
|
2518
|
+
* leaf loop instead of in one block after it (0.4 s for 65,277 leaves). It keeps the perfect
|
|
2519
|
+
* subtrees of RFC 9162, largest first, and joins two with ledger-core's own interior hash,
|
|
2520
|
+
* `merkleRoot([left, right])`; the root folds them from the right, as RFC 9162 splits at the
|
|
2521
|
+
* largest power of two.
|
|
2522
|
+
*/
|
|
2523
|
+
function merkleTree() {
|
|
2524
|
+
const peaks = [];
|
|
2525
|
+
return {
|
|
2526
|
+
push(leaf) {
|
|
2527
|
+
let hash = leaf;
|
|
2528
|
+
let size = 1;
|
|
2529
|
+
while (peaks.at(-1)?.size === size) {
|
|
2530
|
+
hash = merkleRoot([peaks.pop().hash, hash]);
|
|
2531
|
+
size *= 2;
|
|
2532
|
+
}
|
|
2533
|
+
peaks.push({
|
|
2534
|
+
hash,
|
|
2535
|
+
size
|
|
2536
|
+
});
|
|
2537
|
+
},
|
|
2538
|
+
root() {
|
|
2539
|
+
let root = peaks.at(-1).hash;
|
|
2540
|
+
for (let index = peaks.length - 2; index >= 0; index -= 1) root = merkleRoot([peaks[index].hash, root]);
|
|
2541
|
+
return root;
|
|
2542
|
+
}
|
|
2543
|
+
};
|
|
2544
|
+
}
|
|
2545
|
+
/** One array item as `JSON.stringify` writes it: `JSON.parse` of it is what `events[i]` holds. */
|
|
2546
|
+
function storedText(event) {
|
|
2547
|
+
return JSON.stringify(event) ?? "null";
|
|
2548
|
+
}
|
|
2549
|
+
// src/runtime/supervise/upstream-unavailable.ts
|
|
2550
|
+
/**
|
|
2551
|
+
* An upstream that cannot serve NOW: how Runtime recognizes one, and how long an agent waits for it.
|
|
2552
|
+
*
|
|
2553
|
+
* A model provider's exhausted quota, a rate limit, an overload, or the router's own refused
|
|
2554
|
+
* credential is not a fault in the agent: the same request succeeds when capacity returns. A
|
|
2555
|
+
* driver that meets one re-enters after a pause (`runDriverWithRetry`), and a leaf continues in
|
|
2556
|
+
* its environment after the same pause (`providerAsExecutor`). Both read the refusal and the pause
|
|
2557
|
+
* from here, so an outage costs a director and its workers the same number of turns.
|
|
2558
|
+
*
|
|
2559
|
+
* This module imports nothing, so the provider executor can read it without the error classes of
|
|
2560
|
+
* `../../errors`.
|
|
2561
|
+
*/
|
|
2562
|
+
/**
|
|
2563
|
+
* Machine codes that say the upstream cannot serve now rather than that the request is wrong.
|
|
2564
|
+
* Each is a code an upstream publishes, not prose: the router's
|
|
2565
|
+
* (`lib/model-substitution.ts`, `lib/upstream-error-triage.ts`) and the OpenAI- and Anthropic-shaped
|
|
2566
|
+
* error types a relayed body carries.
|
|
2567
|
+
*/
|
|
2568
|
+
const UNAVAILABLE_CODES = /* @__PURE__ */ new Set([
|
|
2569
|
+
"provider_quota_exhausted",
|
|
2570
|
+
"provider_quota_exceeded",
|
|
2571
|
+
"provider_rate_limit",
|
|
2572
|
+
"upstream_unavailable",
|
|
2573
|
+
"provider_key_invalid",
|
|
2574
|
+
"rate_limit_exceeded",
|
|
2575
|
+
"rate_limit_error",
|
|
2576
|
+
"overloaded_error"
|
|
2577
|
+
]);
|
|
2578
|
+
/** 429 Too Many Requests, 503 Service Unavailable, and 529, which Anthropic uses for overload. */
|
|
2579
|
+
const UNAVAILABLE_STATUSES = /* @__PURE__ */ new Set([
|
|
2580
|
+
429,
|
|
2581
|
+
503,
|
|
2582
|
+
529
|
|
2583
|
+
]);
|
|
2584
|
+
/**
|
|
2585
|
+
* A harness that flattens the router's JSON body into its own failure text leaves the router's
|
|
2586
|
+
* code in that text: `opencode execution failed: No provider served model "..."
|
|
2587
|
+
* (provider_quota_exhausted)`. The code is matched as a whole token, and a status only in the
|
|
2588
|
+
* `status code <n>` framing a harness prints, so prose that merely mentions a quota does not match.
|
|
2589
|
+
*/
|
|
2590
|
+
function unavailableSignalInText(text) {
|
|
2591
|
+
for (const token of text.toLowerCase().matchAll(/[a-z_]+/gu)) if (UNAVAILABLE_CODES.has(token[0])) return token[0];
|
|
2592
|
+
const status = text.match(/\bstatus code (\d{3})\b/u)?.[1];
|
|
2593
|
+
return status !== void 0 && UNAVAILABLE_STATUSES.has(Number(status)) ? `http-${status}` : void 0;
|
|
2594
|
+
}
|
|
2595
|
+
/**
|
|
2596
|
+
* The code or status that marks a failed turn outcome as an upstream capacity refusal, or
|
|
2597
|
+
* `undefined`. The outcome's own `errorCode` decides first; the failure text is read only when no
|
|
2598
|
+
* code or numeric status decided, because a harness CLI can report the router's refusal as text.
|
|
2599
|
+
*/
|
|
2600
|
+
function unavailableSignalOfFailure(failure) {
|
|
2601
|
+
const code = failure.errorCode?.toLowerCase();
|
|
2602
|
+
if (code !== void 0 && UNAVAILABLE_CODES.has(code)) return code;
|
|
2603
|
+
if (failure.statusCode !== void 0) return UNAVAILABLE_STATUSES.has(failure.statusCode) ? `http-${failure.statusCode}` : void 0;
|
|
2604
|
+
return unavailableSignalInText(failure.error);
|
|
2605
|
+
}
|
|
2606
|
+
const DEFAULT_UNAVAILABLE_PAUSE_MS = 15e3;
|
|
2607
|
+
const DEFAULT_MAX_UNAVAILABLE_PAUSE_MS = 3e5;
|
|
2608
|
+
/**
|
|
2609
|
+
* The pause after the `consecutive`-th refusal in a row (1-based): the first pause, doubled per
|
|
2610
|
+
* earlier refusal, up to the ceiling. The one rule for a driver that re-enters and a leaf that
|
|
2611
|
+
* continues in its environment, so an outage costs both the same number of turns.
|
|
2612
|
+
*/
|
|
2613
|
+
function unavailablePauseMs(consecutive, policy = {}) {
|
|
2614
|
+
return cappedDoublingMs(consecutive, policy.unavailablePauseMs ?? DEFAULT_UNAVAILABLE_PAUSE_MS, policy.maxUnavailablePauseMs ?? DEFAULT_MAX_UNAVAILABLE_PAUSE_MS);
|
|
2615
|
+
}
|
|
2616
|
+
/** `first` doubled per earlier consecutive wait (`consecutive` is 1-based), up to `ceiling`. */
|
|
2617
|
+
function cappedDoublingMs(consecutive, first, ceiling) {
|
|
2618
|
+
const start = Math.max(0, first);
|
|
2619
|
+
return Math.min(Math.max(start, ceiling), start * 2 ** Math.max(0, consecutive - 1));
|
|
2620
|
+
}
|
|
2621
|
+
/** The longest hold a refusal's own reset time may ask for: a 5-hour plan window plus a margin. */
|
|
2622
|
+
const MAX_RESET_HOLD_MS = 216e5;
|
|
2623
|
+
/** How often a refusal whose reset is beyond {@link MAX_RESET_HOLD_MS} is probed again. */
|
|
2624
|
+
const FAR_RESET_PROBE_MS = 36e5;
|
|
2625
|
+
/**
|
|
2626
|
+
* When a refusal says its quota resets, as epoch ms, or `undefined` when it names no usable time.
|
|
2627
|
+
* The Z.ai Coding Plan's opencode refusal names one in Beijing time with no zone:
|
|
2628
|
+
* `Usage limit reached for 5 hour. Your limit will reset at 2026-10-18 15:17:33`. On Discovery run
|
|
2629
|
+
* magnetar-rust-20261018b, 219 of 324 driver attempts ended on it between 06:04Z and 22:18Z, and
|
|
2630
|
+
* the last refusal came at 22:18Z against `reset at 2026-10-19 06:18:14`, which is 22:18:14Z at
|
|
2631
|
+
* +08:00. A time with its own zone is read as written. A time in the past is no reset time: the
|
|
2632
|
+
* caller keeps its doubling pause. A time more than MAX_RESET_HOLD_MS ahead is held for only
|
|
2633
|
+
* FAR_RESET_PROBE_MS at a time, so a misread zone cannot hold a seat for a day, yet the spent seat
|
|
2634
|
+
* is not asked again every 5 minutes: on magnetar-rust-20261019c a reset beyond 6 h drew about 12
|
|
2635
|
+
* attempts an hour against the refusing seat.
|
|
2636
|
+
*/
|
|
2637
|
+
function unavailableResumeAt(text, nowMs) {
|
|
2638
|
+
const match = text.match(/\breset at (\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}:\d{2})(?:\.\d+)?(Z|[+-]\d{2}:?\d{2})?/u);
|
|
2639
|
+
if (match === null) return void 0;
|
|
2640
|
+
const zone = match[3] ?? (/Usage limit reached for \d+ hour/u.test(text) ? "+08:00" : void 0);
|
|
2641
|
+
if (zone === void 0) return void 0;
|
|
2642
|
+
const at = Date.parse(`${match[1]}T${match[2]}${zone.length === 5 ? `${zone.slice(0, 3)}:${zone.slice(3)}` : zone}`);
|
|
2643
|
+
if (!Number.isFinite(at) || at <= nowMs) return void 0;
|
|
2644
|
+
return at - nowMs > MAX_RESET_HOLD_MS ? nowMs + FAR_RESET_PROBE_MS : at;
|
|
2645
|
+
}
|
|
2646
|
+
/**
|
|
2647
|
+
* Spread a wait over `[ms / 2, ms]`. Every driver a Platform restart cut off fails within the same
|
|
2648
|
+
* seconds; without jitter their doubling schedules stay aligned and they all re-enter together
|
|
2649
|
+
* when the service returns. `random` returns a value in `[0, 1)`; 0 keeps the full wait.
|
|
2650
|
+
*/
|
|
2651
|
+
function jitteredMs(ms, random) {
|
|
2652
|
+
return ms - Math.floor(ms / 2 * Math.min(1, Math.max(0, random())));
|
|
2653
|
+
}
|
|
2654
|
+
// src/runtime/supervise/retained-executor.ts
|
|
2655
|
+
/** Scope owns the durable writer; provider executors only publish sanitized admissions. */
|
|
2656
|
+
const retainedExecutorSeamKey = "runtime.retainedExecutor";
|
|
2657
|
+
/** @internal A capture may finish after cancellation; its durable citation remains appendable. */
|
|
2658
|
+
function executorEvidenceWriter(args) {
|
|
2659
|
+
const traceRoots = args.traceRoots ?? traceRootWriter(args.blobs, args.nodeId);
|
|
2660
|
+
return async (out) => {
|
|
2661
|
+
const snapshot = detachedSnapshot(out, "executor evidence");
|
|
2662
|
+
const outRef = contentAddress(snapshot);
|
|
2663
|
+
await args.blobs.put(outRef, snapshot);
|
|
2664
|
+
const traceRoot = await traceRoots.writeEvidence(outRef, snapshot);
|
|
2665
|
+
await args.journal.appendEvent(args.rootId, {
|
|
2666
|
+
kind: "execution-evidence",
|
|
2667
|
+
id: args.nodeId,
|
|
2668
|
+
outRef,
|
|
2669
|
+
...traceRoot === void 0 ? {} : { traceRoot },
|
|
2670
|
+
seq: args.nextSequence(),
|
|
2671
|
+
at: new Date(args.now()).toISOString()
|
|
2672
|
+
});
|
|
2673
|
+
};
|
|
2674
|
+
}
|
|
2675
|
+
/** @internal The journal writers for `onWorkspaceCapture` and `onWorkspaceCaptureStarted`;
|
|
2676
|
+
* `nextSequence` is the node's own. */
|
|
2677
|
+
function workspaceCaptureWriter(args) {
|
|
2678
|
+
return {
|
|
2679
|
+
onWorkspaceCapture: async (capture) => {
|
|
2680
|
+
await args.journal.appendEvent(args.rootId, {
|
|
2681
|
+
kind: "workspace-capture",
|
|
2682
|
+
id: args.nodeId,
|
|
2683
|
+
...detachedSnapshot(capture, "workspace capture timing"),
|
|
2684
|
+
seq: args.nextSequence(),
|
|
2685
|
+
at: new Date(args.now()).toISOString()
|
|
2686
|
+
});
|
|
2687
|
+
},
|
|
2688
|
+
onWorkspaceCaptureStarted: async (capture) => {
|
|
2689
|
+
await args.journal.appendEvent(args.rootId, {
|
|
2690
|
+
kind: "workspace-capture-started",
|
|
2691
|
+
id: args.nodeId,
|
|
2692
|
+
...detachedSnapshot(capture, "workspace capture start"),
|
|
2693
|
+
seq: args.nextSequence(),
|
|
2694
|
+
at: new Date(args.now()).toISOString()
|
|
2695
|
+
});
|
|
2696
|
+
}
|
|
2697
|
+
};
|
|
2698
|
+
}
|
|
2699
|
+
function retainedExecutorContext(ctx) {
|
|
2700
|
+
return ctx.seams[retainedExecutorSeamKey];
|
|
2701
|
+
}
|
|
2702
|
+
const causeMessages = {
|
|
2703
|
+
unobservable: "retained provider execution requires reconciliation before replacement",
|
|
2704
|
+
"provider-contract": "retained provider response violated its contract; original execution remains unresolved",
|
|
2705
|
+
"request-rejected": "retained provider execution was refused before it ran; nothing to reconcile",
|
|
2706
|
+
transport: "retained provider execution lost its transport; status in doubt",
|
|
2707
|
+
"nested-recovery": "retained nested execution requires recovery before replacement"
|
|
2708
|
+
};
|
|
2709
|
+
/**
|
|
2710
|
+
* agent-provider-tangle's `TangleDispatchNotAdmittedError` (since 3.6.11): Sandbox answered a
|
|
2711
|
+
* dispatch with `dispatched: false` and named another execution that owns the session or turn.
|
|
2712
|
+
* Nothing ran for the request.
|
|
2713
|
+
*/
|
|
2714
|
+
const DISPATCH_NOT_ADMITTED_CODE = "DISPATCH_NOT_ADMITTED";
|
|
2715
|
+
/**
|
|
2716
|
+
* The Sandbox SDK's codes for a box that cannot be provisioned for this profile, sent with HTTP
|
|
2717
|
+
* 422 when the failure is deterministic: a profile file mount outside the box's writable roots,
|
|
2718
|
+
* refused before any box exists, and a profile-file materialization that would fail the same way
|
|
2719
|
+
* again. On Discovery run queens21-h22-capture-root-1-20261019c every child profile mounted
|
|
2720
|
+
* `/work/queens21_count.c` on a read-only root filesystem, and 154 attempts across 4 workers were
|
|
2721
|
+
* retried as a lost transport. The same codes at another status may be transient and are not
|
|
2722
|
+
* read here.
|
|
2723
|
+
*/
|
|
2724
|
+
const PROVISIONING_REFUSAL_CODES = /* @__PURE__ */ new Set(["PROFILE_FILE_MOUNT_OUTSIDE_ROOTS", "PROFILE_FILES_MATERIALIZE_FAILED"]);
|
|
2725
|
+
/** True for an error that is itself a deterministic provisioning refusal (see above). */
|
|
2726
|
+
function isProvisioningRefusal(value) {
|
|
2727
|
+
try {
|
|
2728
|
+
const code = readString(value, "code");
|
|
2729
|
+
const status = readNumber(value, "status") ?? readNumber(value, "statusCode");
|
|
2730
|
+
return code !== void 0 && status === 422 && PROVISIONING_REFUSAL_CODES.has(code);
|
|
2731
|
+
} catch {
|
|
2732
|
+
return false;
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
2735
|
+
/** The contract-error codes that wrap a failed READ rather than name a broken answer. */
|
|
2736
|
+
const readFailureCodes = /* @__PURE__ */ new Set(["RETAINED_RESULT_READ_FAILED", "RETAINED_CONTROL_REF_READ_FAILED"]);
|
|
2737
|
+
/** Socket-level failures, by the codes Node and undici assign, plus the Sandbox SDK's own. */
|
|
2738
|
+
const TRANSPORT_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
2739
|
+
"ECONNRESET",
|
|
2740
|
+
"ECONNREFUSED",
|
|
2741
|
+
"ETIMEDOUT",
|
|
2742
|
+
"ENOTFOUND",
|
|
2743
|
+
"EAI_AGAIN",
|
|
2744
|
+
"EPIPE",
|
|
2745
|
+
"UND_ERR_SOCKET",
|
|
2746
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
2747
|
+
"UND_ERR_HEADERS_TIMEOUT",
|
|
2748
|
+
"UND_ERR_BODY_TIMEOUT",
|
|
2749
|
+
"NETWORK_ERROR"
|
|
2750
|
+
]);
|
|
2751
|
+
function readNumber(value, key) {
|
|
2752
|
+
const read = Reflect.get(value, key);
|
|
2753
|
+
return typeof read === "number" && Number.isFinite(read) ? read : void 0;
|
|
2754
|
+
}
|
|
2755
|
+
function readString(value, key) {
|
|
2756
|
+
const read = Reflect.get(value, key);
|
|
2757
|
+
return typeof read === "string" ? read : void 0;
|
|
2758
|
+
}
|
|
2759
|
+
const credentialAdmissionRefusals = /* @__PURE__ */ new WeakSet();
|
|
2760
|
+
/**
|
|
2761
|
+
* Walk the cause chain (bounded, cycle-safe) and name the first cause whose structure decides.
|
|
2762
|
+
*
|
|
2763
|
+
* `phase` says whether a 4xx or a client deadline can mean "refused before it ran": only at
|
|
2764
|
+
* admission. After admission the same status means the provider cannot resolve what it
|
|
2765
|
+
* admitted, and the honest name is the refusal.
|
|
2766
|
+
*/
|
|
2767
|
+
function classifyRetainedPendingCause(cause, phase = "execution") {
|
|
2768
|
+
return classifyPendingCause(cause, phase);
|
|
2769
|
+
}
|
|
2770
|
+
function classifyPendingCause(cause, phase, onCredentialRefusal) {
|
|
2771
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2772
|
+
const queue = [{
|
|
2773
|
+
value: cause,
|
|
2774
|
+
phase
|
|
2775
|
+
}];
|
|
2776
|
+
let steps = 0;
|
|
2777
|
+
while (queue.length > 0 && steps < 12) {
|
|
2778
|
+
steps += 1;
|
|
2779
|
+
const { value, phase: at } = queue.shift();
|
|
2780
|
+
if (typeof value !== "object" || value === null || seen.has(value)) continue;
|
|
2781
|
+
seen.add(value);
|
|
2782
|
+
if (value instanceof RetainedExecutionPendingError) {
|
|
2783
|
+
if (credentialAdmissionRefusals.has(value)) {
|
|
2784
|
+
if (at === "execution") return "unobservable";
|
|
2785
|
+
onCredentialRefusal?.();
|
|
2786
|
+
}
|
|
2787
|
+
return value.pendingCause;
|
|
2788
|
+
}
|
|
2789
|
+
const name = readString(value, "name");
|
|
2790
|
+
const code = readString(value, "code");
|
|
2791
|
+
const status = readNumber(value, "status") ?? readNumber(value, "statusCode");
|
|
2792
|
+
if (name === "RetainedRunProviderContractError") {
|
|
2793
|
+
if (code === void 0 || !readFailureCodes.has(code)) return "provider-contract";
|
|
2794
|
+
queue.unshift({
|
|
2795
|
+
value: Reflect.get(value, "cause"),
|
|
2796
|
+
phase: "execution"
|
|
2797
|
+
});
|
|
2798
|
+
continue;
|
|
2799
|
+
}
|
|
2800
|
+
if (name === "ZodError" || Array.isArray(Reflect.get(value, "issues")) || code === "JSON_BOUND_VIOLATION") return at === "admission" ? "request-rejected" : "provider-contract";
|
|
2801
|
+
if (code === "DISPATCH_NOT_ADMITTED") return at === "admission" ? "request-rejected" : "provider-contract";
|
|
2802
|
+
if (isProvisioningRefusal(value)) return "request-rejected";
|
|
2803
|
+
if (isCredentialRefusal(value)) {
|
|
2804
|
+
if (at === "execution") return "unobservable";
|
|
2805
|
+
onCredentialRefusal?.();
|
|
2806
|
+
return "request-rejected";
|
|
2807
|
+
}
|
|
2808
|
+
if (code !== void 0 && TRANSPORT_ERROR_CODES.has(code)) return "transport";
|
|
2809
|
+
if (name === "NetworkError" || name === "ServerError") return "transport";
|
|
2810
|
+
if (status === void 0 && at === "admission" && code !== void 0 && UNAVAILABLE_CODES.has(code.toLowerCase())) return "request-rejected";
|
|
2811
|
+
if (status !== void 0) {
|
|
2812
|
+
if (status >= 500) return "transport";
|
|
2813
|
+
if (at === "admission" && status !== 408) return "request-rejected";
|
|
2814
|
+
return "unobservable";
|
|
2815
|
+
}
|
|
2816
|
+
const errors = Reflect.get(value, "errors");
|
|
2817
|
+
if (Array.isArray(errors)) for (const member of errors) queue.push({
|
|
2818
|
+
value: member,
|
|
2819
|
+
phase: at
|
|
2820
|
+
});
|
|
2821
|
+
queue.push({
|
|
2822
|
+
value: Reflect.get(value, "cause"),
|
|
2823
|
+
phase: at
|
|
2824
|
+
});
|
|
2825
|
+
}
|
|
2826
|
+
return "unobservable";
|
|
2827
|
+
}
|
|
2828
|
+
/**
|
|
2829
|
+
* Local observation stopped; the retained provider execution has no accepted terminal result.
|
|
2830
|
+
*
|
|
2831
|
+
* The message names WHICH of the situations in {@link RetainedPendingCause} this is, and the
|
|
2832
|
+
* `pendingCause` field carries it as a value the settlement records, so a reader never splits
|
|
2833
|
+
* the population on the message's wording. The cause chain is kept for the `caused by` receipt.
|
|
2834
|
+
*/
|
|
2835
|
+
var RetainedExecutionPendingError = class extends Error {
|
|
2836
|
+
pendingCause;
|
|
2837
|
+
phase;
|
|
2838
|
+
constructor(cause, pendingCause = "execution") {
|
|
2839
|
+
let credentialRefused = false;
|
|
2840
|
+
const resolved = pendingCause === "admission" || pendingCause === "execution" ? classifyPendingCause(cause, pendingCause, () => {
|
|
2841
|
+
credentialRefused = true;
|
|
2842
|
+
}) : pendingCause;
|
|
2843
|
+
super(causeMessages[resolved], { cause });
|
|
2844
|
+
this.name = "RetainedExecutionPendingError";
|
|
2845
|
+
this.pendingCause = resolved;
|
|
2846
|
+
this.phase = pendingCause === "admission" ? "admission" : "execution";
|
|
2847
|
+
if (credentialRefused) credentialAdmissionRefusals.add(this);
|
|
2848
|
+
}
|
|
2849
|
+
};
|
|
2850
|
+
/** The instruction a keyed continuation runs in the held session: the node's task is unchanged,
|
|
2851
|
+
* and its conversation and workspace are where the budget stop left them. */
|
|
2852
|
+
const BUDGET_CONTINUATION_TASK = "Your previous turn reached its budget and stopped. Your manager gave you more budget to continue the same task in the same session: your conversation so far and the files in your workspace are where you left them. Continue from where you stopped. Do not start over.";
|
|
2853
|
+
/** The execution id of a leaf's later invocation, derived from its `execution-input` sequence so
|
|
2854
|
+
* a recovery derives the same one. A first invocation keeps the node id. */
|
|
2855
|
+
function leafContinuationExecutionId(nodeId, inputSeq) {
|
|
2856
|
+
return `${nodeId}:input:${inputSeq}`;
|
|
2857
|
+
}
|
|
2858
|
+
const retainedPreparations = /* @__PURE__ */ new WeakMap();
|
|
2859
|
+
/** Reconstruct an admitted worker through its original composition boundary. @internal */
|
|
2860
|
+
function registerRetainedExecutorPreparation(factory, prepare) {
|
|
2861
|
+
retainedPreparations.set(factory, prepare);
|
|
2862
|
+
return factory;
|
|
2863
|
+
}
|
|
2864
|
+
function prepareRetainedExecutor(factory, input) {
|
|
2865
|
+
return retainedPreparations.get(factory)?.(input);
|
|
2866
|
+
}
|
|
2867
|
+
// src/runtime/failure-cause.ts
|
|
2868
|
+
const MAX_CAUSE_CHARS = 2048;
|
|
2869
|
+
const MAX_CHAIN_STEPS = 12;
|
|
2870
|
+
const MACHINE_CODE_PATTERN = /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/u;
|
|
2871
|
+
/** @internal Canonical producer-supplied remediation, shared by transport and read models. */
|
|
2872
|
+
const runtimeFailureRemediationSchema = z.object({
|
|
2873
|
+
cause: z.enum([
|
|
2874
|
+
"api_key_selected",
|
|
2875
|
+
"provider_unresolved",
|
|
2876
|
+
"subscription_unverified",
|
|
2877
|
+
"not_authenticated"
|
|
2878
|
+
]),
|
|
2879
|
+
fix: z.string().min(1).max(256),
|
|
2880
|
+
harness: z.string().min(1).max(64),
|
|
2881
|
+
provider: z.string().min(1).max(128).nullable()
|
|
2882
|
+
});
|
|
2883
|
+
/** @internal Validate Runtime-owned failure values on observation transports. */
|
|
2884
|
+
const runtimeFailureCauseSchema = z.object({
|
|
2885
|
+
code: z.enum([
|
|
2886
|
+
"aborted",
|
|
2887
|
+
"deadline-exceeded",
|
|
2888
|
+
"budget-exhausted",
|
|
2889
|
+
"credential-refused",
|
|
2890
|
+
"request-rejected",
|
|
2891
|
+
"upstream-unavailable",
|
|
2892
|
+
"transport",
|
|
2893
|
+
"provider-contract",
|
|
2894
|
+
"retained-unobservable",
|
|
2895
|
+
"nested-recovery",
|
|
2896
|
+
"platform",
|
|
2897
|
+
"work-failed",
|
|
2898
|
+
"unknown"
|
|
2899
|
+
]),
|
|
2900
|
+
owner: z.enum([
|
|
2901
|
+
"provider",
|
|
2902
|
+
"platform",
|
|
2903
|
+
"runtime",
|
|
2904
|
+
"work",
|
|
2905
|
+
"caller",
|
|
2906
|
+
"unknown"
|
|
2907
|
+
]),
|
|
2908
|
+
cause: z.string().max(MAX_CAUSE_CHARS),
|
|
2909
|
+
/** The upstream or provider machine code the failure carried, when it carried one. */
|
|
2910
|
+
providerCode: z.string().regex(MACHINE_CODE_PATTERN).optional(),
|
|
2911
|
+
remediation: runtimeFailureRemediationSchema.readonly().optional()
|
|
2912
|
+
}).readonly();
|
|
2913
|
+
const retainedFailures = {
|
|
2914
|
+
unobservable: {
|
|
2915
|
+
code: "retained-unobservable",
|
|
2916
|
+
owner: "provider"
|
|
2917
|
+
},
|
|
2918
|
+
"provider-contract": {
|
|
2919
|
+
code: "provider-contract",
|
|
2920
|
+
owner: "provider"
|
|
2921
|
+
},
|
|
2922
|
+
"request-rejected": {
|
|
2923
|
+
code: "request-rejected",
|
|
2924
|
+
owner: "caller"
|
|
2925
|
+
},
|
|
2926
|
+
transport: {
|
|
2927
|
+
code: "transport",
|
|
2928
|
+
owner: "platform"
|
|
2929
|
+
},
|
|
2930
|
+
"nested-recovery": {
|
|
2931
|
+
code: "nested-recovery",
|
|
2932
|
+
owner: "runtime"
|
|
2933
|
+
}
|
|
2934
|
+
};
|
|
2935
|
+
function read(value, key) {
|
|
2936
|
+
try {
|
|
2937
|
+
return Reflect.get(value, key);
|
|
2938
|
+
} catch {
|
|
2939
|
+
return;
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
/** Every object in a failure's cause chain and aggregate members, bounded and cycle-safe. */
|
|
2943
|
+
function chain(error) {
|
|
2944
|
+
const out = [];
|
|
2945
|
+
const queue = [error];
|
|
2946
|
+
while (queue.length > 0 && out.length < MAX_CHAIN_STEPS) {
|
|
2947
|
+
const value = queue.shift();
|
|
2948
|
+
if (typeof value !== "object" || value === null || out.includes(value)) continue;
|
|
2949
|
+
out.push(value);
|
|
2950
|
+
const members = read(value, "errors");
|
|
2951
|
+
if (Array.isArray(members)) {
|
|
2952
|
+
const limit = Math.min(members.length, MAX_CHAIN_STEPS);
|
|
2953
|
+
for (let index = 0; index < limit; index++) queue.push(read(members, String(index)));
|
|
2954
|
+
}
|
|
2955
|
+
queue.push(read(value, "cause"));
|
|
2956
|
+
}
|
|
2957
|
+
return out;
|
|
2958
|
+
}
|
|
2959
|
+
const UNATTRIBUTED = {
|
|
2960
|
+
code: "unknown",
|
|
2961
|
+
owner: "unknown"
|
|
2962
|
+
};
|
|
2963
|
+
/** One link's own structure: budget refusal, credential refusal, machine code, HTTP status. */
|
|
2964
|
+
function structuralFailure(link) {
|
|
2965
|
+
if (link instanceof ReservationWaitRefused || read(link, "reason") === "budget-exhausted") return {
|
|
2966
|
+
code: "budget-exhausted",
|
|
2967
|
+
owner: "caller"
|
|
2968
|
+
};
|
|
2969
|
+
const code = upstreamMachineCode([link]);
|
|
2970
|
+
if (isCredentialRefusal(link) || code === "SEAT_NOT_LOGGED_IN") return {
|
|
2971
|
+
code: "credential-refused",
|
|
2972
|
+
owner: "caller"
|
|
2973
|
+
};
|
|
2974
|
+
const status = read(link, "status") ?? read(link, "statusCode");
|
|
2975
|
+
if (code === "MODEL_NOT_FOUND") return {
|
|
2976
|
+
code: "request-rejected",
|
|
2977
|
+
owner: "caller"
|
|
2978
|
+
};
|
|
2979
|
+
if (typeof code === "string" && UNAVAILABLE_CODES.has(code.toLowerCase())) return {
|
|
2980
|
+
code: "upstream-unavailable",
|
|
2981
|
+
owner: "platform"
|
|
2982
|
+
};
|
|
2983
|
+
if (typeof status === "number" && Number.isInteger(status)) {
|
|
2984
|
+
if (UNAVAILABLE_STATUSES.has(status)) return {
|
|
2985
|
+
code: "upstream-unavailable",
|
|
2986
|
+
owner: "platform"
|
|
2987
|
+
};
|
|
2988
|
+
if (status >= 500 || status === 0 || status === 408) return {
|
|
2989
|
+
code: "transport",
|
|
2990
|
+
owner: "platform"
|
|
2991
|
+
};
|
|
2992
|
+
if (status === 401 || status === 403) return {
|
|
2993
|
+
code: "credential-refused",
|
|
2994
|
+
owner: "caller"
|
|
2995
|
+
};
|
|
2996
|
+
if (status >= 400) return {
|
|
2997
|
+
code: "request-rejected",
|
|
2998
|
+
owner: "caller"
|
|
2999
|
+
};
|
|
3000
|
+
}
|
|
3001
|
+
if (typeof code === "string" && TRANSPORT_ERROR_CODES.has(code)) return {
|
|
3002
|
+
code: "transport",
|
|
3003
|
+
owner: "platform"
|
|
3004
|
+
};
|
|
3005
|
+
if (typeof code === "string" && PLATFORM_FAILURE_CODES.has(code.toLowerCase())) return {
|
|
3006
|
+
code: "platform",
|
|
3007
|
+
owner: "platform"
|
|
3008
|
+
};
|
|
3009
|
+
const name = read(link, "name");
|
|
3010
|
+
if (name === "NetworkError" || name === "ServerError") return {
|
|
3011
|
+
code: "transport",
|
|
3012
|
+
owner: "platform"
|
|
3013
|
+
};
|
|
3014
|
+
}
|
|
3015
|
+
/** @internal The same machine-code validation used by public failure values. */
|
|
3016
|
+
function runtimeProviderCode(value) {
|
|
3017
|
+
return typeof value === "string" && MACHINE_CODE_PATTERN.test(value) ? value : void 0;
|
|
3018
|
+
}
|
|
3019
|
+
function upstreamMachineCode(links) {
|
|
3020
|
+
return [
|
|
3021
|
+
"upstreamCode",
|
|
3022
|
+
"errorCode",
|
|
3023
|
+
"code"
|
|
3024
|
+
].flatMap((field) => links.map((link) => runtimeProviderCode(read(link, field)))).find((code) => code !== void 0);
|
|
3025
|
+
}
|
|
3026
|
+
/**
|
|
3027
|
+
* Classify a thrown failure. Pass the caller's signal so a cancellation the caller asked for is
|
|
3028
|
+
* attributed to the caller even when the provider surfaced it as an ordinary error, and say when
|
|
3029
|
+
* the operation's own deadline expired so a blown deadline is never reported as a cancellation.
|
|
3030
|
+
*/
|
|
3031
|
+
function runtimeFailureCause(error, context = {}) {
|
|
3032
|
+
const cause = errMessage(error).slice(0, MAX_CAUSE_CHARS);
|
|
3033
|
+
const caller = context.signal?.aborted ? "aborted" : context.deadlineExpired ? "deadline-exceeded" : void 0;
|
|
3034
|
+
try {
|
|
3035
|
+
const links = chain(error);
|
|
3036
|
+
const providerCode = upstreamMachineCode(links);
|
|
3037
|
+
const upstream = links.find((link) => link instanceof BackendTransportError && link.backend === "bridge" && link.status === 403 && link.upstreamCode === "subscription_required" && link.providerDispatch === "not_started");
|
|
3038
|
+
const remedy = runtimeFailureRemediationSchema.readonly().safeParse(upstream?.remediation);
|
|
3039
|
+
const failure = ({ code, owner }) => ({
|
|
3040
|
+
code,
|
|
3041
|
+
owner,
|
|
3042
|
+
cause,
|
|
3043
|
+
...providerCode === void 0 ? {} : { providerCode },
|
|
3044
|
+
...code === "credential-refused" && providerCode === "subscription_required" && remedy.success ? { remediation: remedy.data } : {}
|
|
3045
|
+
});
|
|
3046
|
+
if (caller !== void 0) return failure({
|
|
3047
|
+
code: caller,
|
|
3048
|
+
owner: "caller"
|
|
3049
|
+
});
|
|
3050
|
+
if (context.signal === void 0 && links.some((link) => read(link, "name") === "AbortError")) return failure({
|
|
3051
|
+
code: "aborted",
|
|
3052
|
+
owner: "caller"
|
|
3053
|
+
});
|
|
3054
|
+
for (const link of links) {
|
|
3055
|
+
if (link instanceof RetainedExecutionPendingError) return failure(retainedFailures[link.pendingCause]);
|
|
3056
|
+
if (read(link, "name") === "RetainedRunProviderContractError") return failure(retainedFailures[classifyRetainedPendingCause(link)]);
|
|
3057
|
+
}
|
|
3058
|
+
for (const link of links) {
|
|
3059
|
+
const classified = structuralFailure(link);
|
|
3060
|
+
if (classified !== void 0) return failure(classified);
|
|
3061
|
+
}
|
|
3062
|
+
return failure(UNATTRIBUTED);
|
|
3063
|
+
} catch {
|
|
3064
|
+
return {
|
|
3065
|
+
...caller === void 0 ? UNATTRIBUTED : {
|
|
3066
|
+
code: caller,
|
|
3067
|
+
owner: "caller"
|
|
3068
|
+
},
|
|
3069
|
+
cause
|
|
3070
|
+
};
|
|
3071
|
+
}
|
|
3072
|
+
}
|
|
3073
|
+
/**
|
|
3074
|
+
* Classify a failure the execution reported without throwing — a Sandbox outcome or executor
|
|
3075
|
+
* envelope with `success: false` — from its machine code and HTTP status. A harness that reports
|
|
3076
|
+
* a failure with neither does not say who must act, so it is `unknown`, never the work's fault.
|
|
3077
|
+
*/
|
|
3078
|
+
function reportedFailureCause(outcome) {
|
|
3079
|
+
return runtimeFailureCause(Object.assign(new Error(outcome.error ?? "execution reported a failure"), {
|
|
3080
|
+
errorCode: outcome.errorCode,
|
|
3081
|
+
statusCode: outcome.statusCode
|
|
3082
|
+
}));
|
|
3083
|
+
}
|
|
3084
|
+
/**
|
|
3085
|
+
* Classify a down settlement from the typed facts Runtime recorded while the cause was still a
|
|
3086
|
+
* value: the retained cause, the failure classified where the executor threw, then `infra: false`,
|
|
3087
|
+
* the throw path's statement that the work itself failed. `infra: true` is not an owner: Runtime
|
|
3088
|
+
* also sets it for cancellations, budget reconcile faults and validation errors, and from the
|
|
3089
|
+
* platform's failure messages, so a settlement with nothing else typed is `unknown`.
|
|
3090
|
+
*/
|
|
3091
|
+
function settlementFailureCause(down) {
|
|
3092
|
+
const cause = errMessage(down.reason).slice(0, MAX_CAUSE_CHARS);
|
|
3093
|
+
if (down.retainedPendingCause !== void 0) return {
|
|
3094
|
+
...retainedFailures[down.retainedPendingCause],
|
|
3095
|
+
cause
|
|
3096
|
+
};
|
|
3097
|
+
if (down.failure !== void 0 && down.failure.code !== "unknown") return {
|
|
3098
|
+
...down.failure,
|
|
3099
|
+
cause
|
|
3100
|
+
};
|
|
3101
|
+
return {
|
|
3102
|
+
...down.infra === false ? {
|
|
3103
|
+
code: "work-failed",
|
|
3104
|
+
owner: "work"
|
|
3105
|
+
} : UNATTRIBUTED,
|
|
3106
|
+
cause,
|
|
3107
|
+
...down.failure?.providerCode === void 0 ? {} : { providerCode: down.failure.providerCode }
|
|
3108
|
+
};
|
|
3109
|
+
}
|
|
3110
|
+
// src/content-digest.ts
|
|
3111
|
+
function sha256(value) {
|
|
3112
|
+
return createHash("sha256").update(value).digest("hex");
|
|
3113
|
+
}
|
|
3114
|
+
// src/durable/run-fence.ts
|
|
3115
|
+
const claims = new AsyncLocalStorage();
|
|
3116
|
+
const publications = new AsyncLocalStorage();
|
|
3117
|
+
/** Bind asynchronous work, including callbacks, to the generation that admitted it. @internal */
|
|
3118
|
+
function runWithWriteClaim(claim, operation) {
|
|
3119
|
+
return claims.run(claim, operation);
|
|
3120
|
+
}
|
|
3121
|
+
/** Capture a store's acquisition once, even when later called from an unrelated emitter. @internal */
|
|
3122
|
+
function captureRunWriteContext() {
|
|
3123
|
+
let claim = claims.getStore();
|
|
3124
|
+
return (operation) => {
|
|
3125
|
+
claim ??= claims.getStore();
|
|
3126
|
+
return claims.run(claim, operation);
|
|
3127
|
+
};
|
|
3128
|
+
}
|
|
3129
|
+
/** Serialize claim changes and file mutations under the same cross-process guard. @internal */
|
|
3130
|
+
function mutateRunClaim(path, operation) {
|
|
3131
|
+
const legacyGuard = `${path}.guard`;
|
|
3132
|
+
if (existsSync(legacyGuard)) throw new Error(`supervisePursuit: cannot mutate ${path}; inspect ${legacyGuard} and remove it only after confirming no lock mutation is active`);
|
|
3133
|
+
const deadline = performance.now() + 5e3;
|
|
3134
|
+
const sleeper = new Int32Array(new SharedArrayBuffer(4));
|
|
3135
|
+
for (;;) {
|
|
3136
|
+
const acquisition = tryAcquireAtomicFileLock({ lockPath: `${path}.mutation` });
|
|
3137
|
+
if (acquisition.acquired) try {
|
|
3138
|
+
return operation();
|
|
3139
|
+
} finally {
|
|
3140
|
+
acquisition.lock.release();
|
|
3141
|
+
}
|
|
3142
|
+
if (performance.now() >= deadline) throw new Error(`run directory mutation is held: ${path}`);
|
|
3143
|
+
Atomics.wait(sleeper, 0, 0, 10);
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
/**
|
|
3147
|
+
* Fence an actual file mutation, not an earlier advisory check. No await occurs between the
|
|
3148
|
+
* ownership check and publication, and acquire/release use this same guard. Unclaimed standalone
|
|
3149
|
+
* stores retain their existing single-writer contract; pursuit-owned work always carries a claim.
|
|
3150
|
+
* @internal
|
|
3151
|
+
*/
|
|
3152
|
+
function fenceRunWrite(operation) {
|
|
3153
|
+
const claim = claims.getStore();
|
|
3154
|
+
if (claim === void 0) return operation();
|
|
3155
|
+
const publication = publications.getStore();
|
|
3156
|
+
if (publication?.active && publication.claim.path === claim.path && publication.claim.generation === claim.generation) return operation();
|
|
3157
|
+
return mutateRunClaim(claim.path, () => {
|
|
3158
|
+
let holder;
|
|
3159
|
+
try {
|
|
3160
|
+
const fd = openSync(claim.path, "r");
|
|
3161
|
+
try {
|
|
3162
|
+
const stat = fstatSync(fd, { bigint: true });
|
|
3163
|
+
if (stat.dev.toString() !== claim.lock.dev || stat.ino.toString() !== claim.lock.ino || stat.birthtimeNs.toString() !== claim.lock.birthtimeNs) throw new Error("run lock inode changed");
|
|
3164
|
+
holder = JSON.parse(readFileSync(fd, "utf8"));
|
|
3165
|
+
} finally {
|
|
3166
|
+
closeSync(fd);
|
|
3167
|
+
}
|
|
3168
|
+
} catch (cause) {
|
|
3169
|
+
throw new Error(`run directory generation ${claim.generation} lost its claim`, { cause });
|
|
3170
|
+
}
|
|
3171
|
+
if (holder?.generation !== claim.generation || holder.lock?.dev !== claim.lock.dev || holder.lock?.ino !== claim.lock.ino || holder.lock?.birthtimeNs !== claim.lock.birthtimeNs) throw new Error(`run directory generation ${claim.generation} lost its claim`);
|
|
3172
|
+
const publication = {
|
|
3173
|
+
claim,
|
|
3174
|
+
active: true
|
|
3175
|
+
};
|
|
3176
|
+
try {
|
|
3177
|
+
return publications.run(publication, operation);
|
|
3178
|
+
} finally {
|
|
3179
|
+
publication.active = false;
|
|
3180
|
+
}
|
|
3181
|
+
});
|
|
3182
|
+
}
|
|
3183
|
+
/** Persistent generation counter survives a clean release of supervise.lock. @internal */
|
|
3184
|
+
function runGenerationPath(runDir) {
|
|
3185
|
+
return resolve(runDir, "supervise.generation");
|
|
3186
|
+
}
|
|
3187
|
+
// src/runtime/supervise/durable-file.ts
|
|
3188
|
+
/**
|
|
3189
|
+
* Small filesystem durability primitives for Runtime-owned state.
|
|
3190
|
+
*
|
|
3191
|
+
* Callers must validate their path and parent directory before invoking these functions.
|
|
3192
|
+
* The exclusive writer protects a no-clobber publication race; the atomic writer replaces one
|
|
3193
|
+
* state file after its temporary contents reach stable storage.
|
|
3194
|
+
*
|
|
3195
|
+
* @internal
|
|
3196
|
+
*/
|
|
3197
|
+
var durable_file_exports = /* @__PURE__ */ __exportAll({
|
|
3198
|
+
appendDurableFile: () => appendDurableFile,
|
|
3199
|
+
assertNoSymlinkDescendant: () => assertNoSymlinkDescendant,
|
|
3200
|
+
publishExclusiveDurableFile: () => publishExclusiveDurableFile,
|
|
3201
|
+
syncDurableDirectory: () => syncDurableDirectory,
|
|
3202
|
+
writeAtomicDurableFile: () => writeAtomicDurableFile
|
|
3203
|
+
});
|
|
3204
|
+
/**
|
|
3205
|
+
* Publish a new file without replacing a concurrent winner. The published entry is a hard link
|
|
3206
|
+
* of the temporary file, so a content function sees the published file's inode.
|
|
3207
|
+
*/
|
|
3208
|
+
function publishExclusiveDurableFile(filePath, content, options = {}) {
|
|
3209
|
+
return fenceRunWrite(() => publishExclusiveDurableFileUnchecked(filePath, content, options));
|
|
3210
|
+
}
|
|
3211
|
+
function publishExclusiveDurableFileUnchecked(filePath, content, options = {}) {
|
|
3212
|
+
const temporaryPath = `${filePath}.${randomUUID()}.tmp`;
|
|
3213
|
+
let operationFailed = false;
|
|
3214
|
+
let operationError;
|
|
3215
|
+
let published = false;
|
|
3216
|
+
try {
|
|
3217
|
+
writeExclusiveDurableFile(temporaryPath, content, options);
|
|
3218
|
+
try {
|
|
3219
|
+
linkSync(temporaryPath, filePath);
|
|
3220
|
+
syncDurableDirectory(dirname(filePath));
|
|
3221
|
+
published = true;
|
|
3222
|
+
} catch (error) {
|
|
3223
|
+
if (!isAlreadyExistsError(error)) throw error;
|
|
3224
|
+
}
|
|
3225
|
+
} catch (error) {
|
|
3226
|
+
operationFailed = true;
|
|
3227
|
+
operationError = error;
|
|
3228
|
+
}
|
|
3229
|
+
let cleanupError;
|
|
3230
|
+
try {
|
|
3231
|
+
unlinkSync(temporaryPath);
|
|
3232
|
+
} catch (error) {
|
|
3233
|
+
if (!isNoEntryError(error)) cleanupError = error;
|
|
3234
|
+
}
|
|
3235
|
+
if (operationFailed) throw operationError;
|
|
3236
|
+
if (cleanupError !== void 0) throw cleanupError;
|
|
3237
|
+
return published;
|
|
3238
|
+
}
|
|
3239
|
+
/** Fsync a directory after publishing a durable file entry. */
|
|
3240
|
+
function syncDurableDirectory(directoryPath) {
|
|
3241
|
+
const fd = openSync(directoryPath, "r");
|
|
3242
|
+
try {
|
|
3243
|
+
fsyncSync(fd);
|
|
3244
|
+
} finally {
|
|
3245
|
+
closeSync(fd);
|
|
3246
|
+
}
|
|
3247
|
+
}
|
|
3248
|
+
/** Fsync one durable file after its contents reach stable storage. */
|
|
3249
|
+
function syncDurableFile(filePath, exactMode) {
|
|
3250
|
+
const fd = openSync(filePath, "r");
|
|
3251
|
+
try {
|
|
3252
|
+
if (exactMode !== void 0 && process.platform !== "win32") fchmodSync(fd, exactMode);
|
|
3253
|
+
fsyncSync(fd);
|
|
3254
|
+
} finally {
|
|
3255
|
+
closeSync(fd);
|
|
3256
|
+
}
|
|
3257
|
+
}
|
|
3258
|
+
/** Append one record with O_APPEND, then fsync the file and containing directory. */
|
|
3259
|
+
function appendDurableFile(filePath, content, options = {}) {
|
|
3260
|
+
fenceRunWrite(() => appendDurableFileUnchecked(filePath, content, options));
|
|
3261
|
+
}
|
|
3262
|
+
function appendDurableFileUnchecked(filePath, content, options = {}) {
|
|
3263
|
+
const flags = constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | (typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0);
|
|
3264
|
+
const fd = openSync(filePath, flags, options.mode ?? 384);
|
|
3265
|
+
try {
|
|
3266
|
+
if (process.platform !== "win32") fchmodSync(fd, options.mode ?? 384);
|
|
3267
|
+
const expectedBytes = Buffer.byteLength(content, "utf8");
|
|
3268
|
+
const writtenBytes = writeSync(fd, content, void 0, "utf8");
|
|
3269
|
+
if (writtenBytes !== expectedBytes) throw new Error(`durable append wrote ${writtenBytes} of ${expectedBytes} bytes to '${filePath}'`);
|
|
3270
|
+
fsyncSync(fd);
|
|
3271
|
+
} finally {
|
|
3272
|
+
closeSync(fd);
|
|
3273
|
+
}
|
|
3274
|
+
syncDurableDirectory(dirname(filePath));
|
|
3275
|
+
}
|
|
3276
|
+
/** Reject a path that escapes its run directory or traverses a symbolic link. */
|
|
3277
|
+
function assertNoSymlinkDescendant(root, target, label = "path") {
|
|
3278
|
+
const base = resolve(root);
|
|
3279
|
+
const exact = resolve(target);
|
|
3280
|
+
const rel = relative(base, exact);
|
|
3281
|
+
if (rel === ".." || rel.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`)) throw new Error(`${label} path escapes its run directory`);
|
|
3282
|
+
if (existsSync(base) && lstatSync(base).isSymbolicLink()) throw new Error(`${label} path contains a symbolic link: ${base}`);
|
|
3283
|
+
let current = base;
|
|
3284
|
+
for (const part of rel.split(/[\\/]/u).filter(Boolean)) {
|
|
3285
|
+
current = join(current, part);
|
|
3286
|
+
if (!existsSync(current)) continue;
|
|
3287
|
+
if (lstatSync(current).isSymbolicLink()) throw new Error(`${label} path contains a symbolic link: ${current}`);
|
|
3288
|
+
}
|
|
3289
|
+
}
|
|
3290
|
+
/** Write, fsync, atomically replace, and fsync one Runtime-owned state file.
|
|
3291
|
+
* preserveMode retains ordinary access permissions despite the current umask; a new
|
|
3292
|
+
* file keeps normal creation semantics. Callers still own path validation and write exclusion. */
|
|
3293
|
+
function writeAtomicDurableFile(filePath, content, options = {}) {
|
|
3294
|
+
fenceRunWrite(() => writeAtomicDurableFileUnchecked(filePath, content, options));
|
|
3295
|
+
}
|
|
3296
|
+
function writeAtomicDurableFileUnchecked(filePath, content, options = {}) {
|
|
3297
|
+
const temporaryPath = `${filePath}.${randomUUID()}.tmp`;
|
|
3298
|
+
let operationFailed = false;
|
|
3299
|
+
let operationError;
|
|
3300
|
+
try {
|
|
3301
|
+
let mode = options.mode;
|
|
3302
|
+
let exactMode = false;
|
|
3303
|
+
if (options.preserveMode) try {
|
|
3304
|
+
mode = statSync(filePath).mode & 511;
|
|
3305
|
+
exactMode = true;
|
|
3306
|
+
} catch (error) {
|
|
3307
|
+
if (!isNoEntryError(error)) throw error;
|
|
3308
|
+
}
|
|
3309
|
+
writeExclusiveDurableFile(temporaryPath, content, { mode }, exactMode);
|
|
3310
|
+
renameSync(temporaryPath, filePath);
|
|
3311
|
+
syncDurableDirectory(dirname(filePath));
|
|
3312
|
+
} catch (error) {
|
|
3313
|
+
operationFailed = true;
|
|
3314
|
+
operationError = error;
|
|
3315
|
+
}
|
|
3316
|
+
let cleanupError;
|
|
3317
|
+
try {
|
|
3318
|
+
unlinkSync(temporaryPath);
|
|
3319
|
+
} catch (error) {
|
|
3320
|
+
if (!isNoEntryError(error)) cleanupError = error;
|
|
3321
|
+
}
|
|
3322
|
+
if (operationFailed) throw operationError;
|
|
3323
|
+
if (cleanupError !== void 0) throw cleanupError;
|
|
3324
|
+
}
|
|
3325
|
+
function writeExclusiveDurableFile(filePath, content, options, exactMode = false) {
|
|
3326
|
+
const fd = openSync(filePath, "wx", options.mode ?? 438);
|
|
3327
|
+
try {
|
|
3328
|
+
const text = typeof content === "string" ? content : content(fstatSync(fd, { bigint: true }));
|
|
3329
|
+
writeFileSync(fd, text, "utf8");
|
|
3330
|
+
} finally {
|
|
3331
|
+
closeSync(fd);
|
|
3332
|
+
}
|
|
3333
|
+
syncDurableFile(filePath, exactMode ? options.mode : void 0);
|
|
3334
|
+
}
|
|
3335
|
+
function isAlreadyExistsError(error) {
|
|
3336
|
+
return error !== null && typeof error === "object" && "code" in error && error.code === "EEXIST";
|
|
3337
|
+
}
|
|
3338
|
+
function isNoEntryError(error) {
|
|
3339
|
+
return error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT";
|
|
3340
|
+
}
|
|
3341
|
+
// src/durable/spawn-journal/event-index.ts
|
|
3342
|
+
/**
|
|
3343
|
+
* A trace root is valid only beside the output it commits to, with a well-formed root, artifact
|
|
3344
|
+
* reference and leaf count (at least the summary leaf). Records written before trace roots carry
|
|
3345
|
+
* none and stay valid.
|
|
3346
|
+
*/
|
|
3347
|
+
function assertTraceRoot(event, root) {
|
|
3348
|
+
if (!("traceRoot" in event) || event.traceRoot === void 0) return;
|
|
3349
|
+
const receipt = event.traceRoot;
|
|
3350
|
+
const fields = receipt !== null && typeof receipt === "object" ? receipt : {};
|
|
3351
|
+
if (typeof event.outRef !== "string" || typeof fields.root !== "string" || !/^sha256:[0-9a-f]{64}$/.test(fields.root) || typeof fields.ref !== "string" || !/^sha256:[0-9a-f]{64}$/.test(fields.ref) || typeof fields.leafCount !== "number" || !Number.isSafeInteger(fields.leafCount) || fields.leafCount < 1) throw new Error(`spawn journal corrupted: ${event.kind} for node '${event.id}' carries a malformed trace root in tree '${root}'`);
|
|
3352
|
+
}
|
|
3353
|
+
/** One validation implementation for memory, durable append, and cold replay. */
|
|
3354
|
+
/** @internal shared with the SQL-backed store. */
|
|
3355
|
+
var SpawnEventIndex = class {
|
|
3356
|
+
root;
|
|
3357
|
+
nodes = /* @__PURE__ */ new Map();
|
|
3358
|
+
cursors = /* @__PURE__ */ new Set();
|
|
3359
|
+
constructor(root) {
|
|
3360
|
+
this.root = root;
|
|
3361
|
+
}
|
|
3362
|
+
assert(event) {
|
|
3363
|
+
const node = this.nodes.get(event.id);
|
|
3364
|
+
this.assertRetained(node, event);
|
|
3365
|
+
assertTraceRoot(event, this.root);
|
|
3366
|
+
if (event.kind === "execution-evidence") {
|
|
3367
|
+
if (!node?.spawned || !/^sha256:[0-9a-f]{64}$/.test(event.outRef)) throw new Error("spawn journal corrupted: executor evidence has no spawned node or valid content reference");
|
|
3368
|
+
}
|
|
3369
|
+
if (event.kind === "materialized") {
|
|
3370
|
+
if (node?.materialized) throw new Error(`spawn journal corrupted: duplicate materialization receipt for node '${event.id}' in tree '${this.root}'`);
|
|
3371
|
+
if (!node?.spawned) throw new Error(`spawn journal corrupted: materialization for node '${event.id}' precedes its spawn in tree '${this.root}'`);
|
|
3372
|
+
}
|
|
3373
|
+
if (event.kind === "seat-materialized") {
|
|
3374
|
+
if (!node?.materialized || node.authoredProfileDigest === void 0 || event.receipt.status !== "known" || event.receipt.authoredProfileDigest !== node.authoredProfileDigest || !Number.isSafeInteger(event.segmentIndex) || event.segmentIndex <= node.latestSeatMaterialization) throw new Error(`spawn journal corrupted: invalid seat materialization for node '${event.id}' in tree '${this.root}'`);
|
|
3375
|
+
}
|
|
3376
|
+
if (event.kind === "execution-bound") {
|
|
3377
|
+
if (node?.bindings.has(event.binding.attemptId)) throw new Error(`spawn journal corrupted: duplicate execution binding for node '${event.id}' attempt '${event.binding.attemptId}' in tree '${this.root}'`);
|
|
3378
|
+
if (!node?.materialized) throw new Error(`spawn journal corrupted: execution binding for node '${event.id}' precedes materialization in tree '${this.root}'`);
|
|
3379
|
+
}
|
|
3380
|
+
if (!outsideCursorNamespace(event) && this.cursors.has(event.seq)) throw new Error(`spawn journal corrupted: duplicate cursor seq ${event.seq} in tree '${this.root}'; the cursor order replay relies on is not unique`);
|
|
3381
|
+
}
|
|
3382
|
+
/** Called after validation; a file writer discards staged state if grouped publication fails. */
|
|
3383
|
+
add(event) {
|
|
3384
|
+
if (!outsideCursorNamespace(event)) this.cursors.add(event.seq);
|
|
3385
|
+
if (event.kind !== "spawned" && event.kind !== "materialized" && event.kind !== "seat-materialized" && event.kind !== "execution-bound" && event.kind !== "execution-input" && event.kind !== "execution-admitted" && event.kind !== "execution-result" && event.kind !== "execution-abandoned" && event.kind !== "environment-teardown" && event.kind !== "environment-terminal" && !closesCursorSlot(event)) return;
|
|
3386
|
+
let node = this.nodes.get(event.id);
|
|
3387
|
+
if (!node) {
|
|
3388
|
+
node = {
|
|
3389
|
+
spawned: false,
|
|
3390
|
+
closed: false,
|
|
3391
|
+
materialized: false,
|
|
3392
|
+
latestSeatMaterialization: 0,
|
|
3393
|
+
bindings: /* @__PURE__ */ new Set(),
|
|
3394
|
+
inputs: /* @__PURE__ */ new Set(),
|
|
3395
|
+
input: false,
|
|
3396
|
+
result: false,
|
|
3397
|
+
abandoned: false,
|
|
3398
|
+
admissions: /* @__PURE__ */ new Map(),
|
|
3399
|
+
unusableEnvironments: /* @__PURE__ */ new Set()
|
|
3400
|
+
};
|
|
3401
|
+
this.nodes.set(event.id, node);
|
|
3402
|
+
}
|
|
3403
|
+
if (event.kind === "environment-teardown") {
|
|
3404
|
+
if (event.destroyed) node.unusableEnvironments.add(event.environmentId);
|
|
3405
|
+
return;
|
|
3406
|
+
}
|
|
3407
|
+
if (event.kind === "environment-terminal") {
|
|
3408
|
+
node.unusableEnvironments.add(event.environmentId);
|
|
3409
|
+
return;
|
|
3410
|
+
}
|
|
3411
|
+
if (event.kind === "execution-admitted" && event.admission.phase === "environment") node.lastEnvironmentId = event.admission.environmentId;
|
|
3412
|
+
if (event.kind === "spawned") node.spawned = true;
|
|
3413
|
+
else if (event.kind === "materialized") {
|
|
3414
|
+
node.materialized = true;
|
|
3415
|
+
node.authoredProfileDigest = event.receipt.authoredProfileDigest;
|
|
3416
|
+
} else if (event.kind === "seat-materialized") node.latestSeatMaterialization = event.segmentIndex;
|
|
3417
|
+
else if (event.kind === "execution-bound") node.bindings.add(event.binding.attemptId);
|
|
3418
|
+
else if (event.kind === "execution-input") {
|
|
3419
|
+
node.inputs.add(event.seq);
|
|
3420
|
+
node.input = true;
|
|
3421
|
+
node.latestInput = event.seq;
|
|
3422
|
+
node.result = false;
|
|
3423
|
+
node.abandoned = false;
|
|
3424
|
+
node.admissions.clear();
|
|
3425
|
+
} else if (event.kind === "execution-admitted") node.admissions.set(event.admission.phase, event.admission);
|
|
3426
|
+
else if (event.kind === "execution-result") node.result = true;
|
|
3427
|
+
else if (event.kind === "execution-abandoned") node.abandoned = true;
|
|
3428
|
+
if (closesCursorSlot(event)) node.closed = true;
|
|
3429
|
+
}
|
|
3430
|
+
assertRetained(node, event) {
|
|
3431
|
+
if (event.kind !== "execution-input" && event.kind !== "execution-admitted" && event.kind !== "execution-result" && event.kind !== "execution-abandoned") return;
|
|
3432
|
+
function fail(reason) {
|
|
3433
|
+
throw new Error(`spawn journal corrupted: retained execution '${event.id}' ${reason}`);
|
|
3434
|
+
}
|
|
3435
|
+
if (!node?.spawned) fail("precedes its spawn");
|
|
3436
|
+
if (node.closed) fail("follows its terminal settlement");
|
|
3437
|
+
if (event.kind === "execution-input") {
|
|
3438
|
+
if (!/^sha256:[0-9a-f]{64}$/.test(event.taskRef)) fail("has an invalid task reference");
|
|
3439
|
+
if (node.inputs.has(event.seq)) fail("has duplicate input sequence");
|
|
3440
|
+
const environment = node.admissions.get("environment");
|
|
3441
|
+
const ranIn = environment?.phase === "environment" ? environment.environmentId : node.lastEnvironmentId;
|
|
3442
|
+
const closed = node.abandoned || ranIn !== void 0 && node.unusableEnvironments.has(ranIn);
|
|
3443
|
+
if (node.input && !node.result && !closed) fail("input replaces an unfinished invocation");
|
|
3444
|
+
return;
|
|
3445
|
+
}
|
|
3446
|
+
if (event.kind === "execution-abandoned") {
|
|
3447
|
+
if (!node.input || node.latestInput !== event.inputSeq) fail("abandons an invocation that is not its latest");
|
|
3448
|
+
if (node.result) fail("abandons an invocation that has a result");
|
|
3449
|
+
if (node.abandoned) fail("abandons an invocation twice");
|
|
3450
|
+
if (!Number.isSafeInteger(event.failures) || event.failures < 1) fail("abandons without a failure count");
|
|
3451
|
+
return;
|
|
3452
|
+
}
|
|
3453
|
+
if (node.abandoned) fail(`${event.kind} follows its abandonment`);
|
|
3454
|
+
if (event.kind === "execution-result") {
|
|
3455
|
+
if (node.result) fail("has duplicate result");
|
|
3456
|
+
if (!node.admissions.has("dispatched")) fail("result precedes dispatch");
|
|
3457
|
+
if (!/^sha256:[0-9a-f]{64}$/.test(event.outRef)) fail("has an invalid result reference");
|
|
3458
|
+
assertValidSpend(event.spent, "retained execution result");
|
|
3459
|
+
executorFailureReason(event);
|
|
3460
|
+
return;
|
|
3461
|
+
}
|
|
3462
|
+
const admission = event.admission;
|
|
3463
|
+
if (!admission || ![
|
|
3464
|
+
"intent",
|
|
3465
|
+
"environment",
|
|
3466
|
+
"dispatched"
|
|
3467
|
+
].includes(admission.phase)) fail("has an invalid admission phase");
|
|
3468
|
+
if (node.admissions.has(admission.phase)) fail("has duplicate admission phase");
|
|
3469
|
+
if (node.result) fail("admission follows result");
|
|
3470
|
+
if (!node.input) fail("admission precedes input");
|
|
3471
|
+
if (admission.phase === "intent") {
|
|
3472
|
+
if (node.admissions.size !== 0) fail("intent follows another admission");
|
|
3473
|
+
return;
|
|
3474
|
+
}
|
|
3475
|
+
const intent = node.admissions.get("intent");
|
|
3476
|
+
if (intent?.phase !== "intent") fail("admission precedes intent");
|
|
3477
|
+
if (admission.idempotencyKey !== intent.idempotencyKey || admission.turnId !== intent.turnId) fail("changes its admitted request identity");
|
|
3478
|
+
if (admission.phase === "environment") {
|
|
3479
|
+
if (admission.provider !== intent.provider || admission.sessionId !== intent.sessionId || admission.executionId !== intent.executionId) fail("changes its admitted execution identity");
|
|
3480
|
+
return;
|
|
3481
|
+
}
|
|
3482
|
+
const environment = node.admissions.get("environment");
|
|
3483
|
+
if (environment?.phase !== "environment") fail("dispatch precedes environment");
|
|
3484
|
+
const control = admission.controlRef;
|
|
3485
|
+
if (!control || control.provider !== intent.provider || control.environmentId !== environment.environmentId || control.sessionId !== intent.sessionId || control.executionId !== intent.executionId) fail("dispatch changes its admitted execution identity");
|
|
3486
|
+
}
|
|
3487
|
+
};
|
|
3488
|
+
/**
|
|
3489
|
+
* The records that CLOSE a node's cursor slot: a settlement or a cancellation. The complement question to {@link outsideCursorNamespace}, and the one a resume needs —
|
|
3490
|
+
* a kind missing here leaves a finished node looking live, so its budget is never reclaimed and
|
|
3491
|
+
* its reservation is replayed as still in doubt.
|
|
3492
|
+
*/
|
|
3493
|
+
function closesCursorSlot(ev) {
|
|
3494
|
+
return ev.kind === "settled" || ev.kind === "cancelled";
|
|
3495
|
+
}
|
|
3496
|
+
const outsideKinds = /* @__PURE__ */ new Set([
|
|
3497
|
+
"spawned",
|
|
3498
|
+
"metered",
|
|
3499
|
+
"materialized",
|
|
3500
|
+
"execution-bound",
|
|
3501
|
+
"execution-input",
|
|
3502
|
+
"execution-admitted",
|
|
3503
|
+
"execution-result",
|
|
3504
|
+
"execution-abandoned",
|
|
3505
|
+
"execution-evidence",
|
|
3506
|
+
"evidence-pending",
|
|
3507
|
+
"progress",
|
|
3508
|
+
"reconciled",
|
|
3509
|
+
"edge",
|
|
3510
|
+
"teardown-unconfirmed",
|
|
3511
|
+
"teardown-pending",
|
|
3512
|
+
"teardown-confirmed",
|
|
3513
|
+
"environment-teardown",
|
|
3514
|
+
"environment-terminal",
|
|
3515
|
+
"workspace-checkpoint-requested",
|
|
3516
|
+
"workspace-checkpoint",
|
|
3517
|
+
"workspace-checkpoint-fork-requested",
|
|
3518
|
+
"workspace-checkpoint-fork-ready",
|
|
3519
|
+
"workspace-checkpoint-fork-teardown",
|
|
3520
|
+
"workspace-checkpoint-capture",
|
|
3521
|
+
"workspace-checkpoint-cleanup",
|
|
3522
|
+
"workspace-restored",
|
|
3523
|
+
"workspace-capture-started",
|
|
3524
|
+
"workspace-capture",
|
|
3525
|
+
"trace-unpropagated",
|
|
3526
|
+
"seat-segment",
|
|
3527
|
+
"seat-materialized",
|
|
3528
|
+
"seat-paused",
|
|
3529
|
+
"run-parked",
|
|
3530
|
+
"root-segment",
|
|
3531
|
+
"root-reentered",
|
|
3532
|
+
"reopened",
|
|
3533
|
+
"paused",
|
|
3534
|
+
"barren-pause",
|
|
3535
|
+
"driver-attempt",
|
|
3536
|
+
"node-inputs-resolved",
|
|
3537
|
+
"edge-verdict",
|
|
3538
|
+
"join-state"
|
|
3539
|
+
]);
|
|
3540
|
+
/** True for a node-CREATION or informational record. Narrows, so a caller keeps the union's arms. */
|
|
3541
|
+
function outsideCursorNamespace(ev) {
|
|
3542
|
+
return outsideKinds.has(ev.kind);
|
|
3543
|
+
}
|
|
3544
|
+
/** True for a record inside the cursor namespace. Narrows to {@link CursorNamespaceEvent}. */
|
|
3545
|
+
function insideCursorNamespace(ev) {
|
|
3546
|
+
return !outsideKinds.has(ev.kind);
|
|
3547
|
+
}
|
|
3548
|
+
// src/durable/jsonl-append.ts
|
|
3549
|
+
/** Commit JSONL bytes and validate their descriptor under one generation fence. @internal */
|
|
3550
|
+
function commitJsonLine(path, record, acknowledge) {
|
|
3551
|
+
const encoded = JSON.stringify(record);
|
|
3552
|
+
if (encoded === void 0) throw new TypeError("JSONL records must be JSON serializable");
|
|
3553
|
+
return fenceRunWrite(() => {
|
|
3554
|
+
const separator = prepareJsonlTail(path);
|
|
3555
|
+
const fd = openSync(path, "a");
|
|
3556
|
+
try {
|
|
3557
|
+
const bytes = Buffer.from(`${separator ? "\n" : ""}${encoded}\n`);
|
|
3558
|
+
let offset = 0;
|
|
3559
|
+
while (offset < bytes.length) {
|
|
3560
|
+
const written = writeSync(fd, bytes, offset, bytes.length - offset);
|
|
3561
|
+
if (written <= 0) throw new Error(`append-only file write made no progress at byte ${offset}`);
|
|
3562
|
+
offset += written;
|
|
3563
|
+
}
|
|
3564
|
+
fsyncSync(fd);
|
|
3565
|
+
return acknowledge?.(fd);
|
|
3566
|
+
} finally {
|
|
3567
|
+
closeSync(fd);
|
|
3568
|
+
}
|
|
3569
|
+
});
|
|
3570
|
+
}
|
|
3571
|
+
/** Recover only an uncommitted tail. Caller holds the generation fence. @internal */
|
|
3572
|
+
function prepareJsonlTail(path) {
|
|
3573
|
+
let fd;
|
|
3574
|
+
try {
|
|
3575
|
+
fd = openSync(path, "r+");
|
|
3576
|
+
} catch (error) {
|
|
3577
|
+
if (error.code === "ENOENT") return false;
|
|
3578
|
+
throw error;
|
|
3579
|
+
}
|
|
3580
|
+
try {
|
|
3581
|
+
const size = fstatSync(fd).size;
|
|
3582
|
+
if (size === 0) return false;
|
|
3583
|
+
const last = Buffer.allocUnsafe(1);
|
|
3584
|
+
readAt(fd, last, size - 1);
|
|
3585
|
+
if (last[0] === 10) return false;
|
|
3586
|
+
const chunks = [];
|
|
3587
|
+
let end = size;
|
|
3588
|
+
let tailStart = 0;
|
|
3589
|
+
while (end > 0) {
|
|
3590
|
+
const start = Math.max(0, end - 65536);
|
|
3591
|
+
const chunk = Buffer.allocUnsafe(end - start);
|
|
3592
|
+
readAt(fd, chunk, start);
|
|
3593
|
+
const newline = chunk.lastIndexOf(10);
|
|
3594
|
+
chunks.push(chunk.subarray(newline + 1));
|
|
3595
|
+
if (newline >= 0) {
|
|
3596
|
+
tailStart = start + newline + 1;
|
|
3597
|
+
break;
|
|
3598
|
+
}
|
|
3599
|
+
end = start;
|
|
3600
|
+
}
|
|
3601
|
+
try {
|
|
3602
|
+
JSON.parse(Buffer.concat(chunks.reverse()).toString("utf8"));
|
|
3603
|
+
return true;
|
|
3604
|
+
} catch (error) {
|
|
3605
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
3606
|
+
ftruncateSync(fd, tailStart);
|
|
3607
|
+
fsyncSync(fd);
|
|
3608
|
+
return false;
|
|
3609
|
+
}
|
|
3610
|
+
} finally {
|
|
3611
|
+
closeSync(fd);
|
|
3612
|
+
}
|
|
3613
|
+
}
|
|
3614
|
+
function readAt(fd, buffer, position) {
|
|
3615
|
+
let offset = 0;
|
|
3616
|
+
while (offset < buffer.length) {
|
|
3617
|
+
const read = readSync(fd, buffer, offset, buffer.length - offset, position + offset);
|
|
3618
|
+
if (read === 0) throw new Error("journal changed while preparing its append tail");
|
|
3619
|
+
offset += read;
|
|
3620
|
+
}
|
|
3621
|
+
}
|
|
3622
|
+
// src/durable/jsonl-file.ts
|
|
3623
|
+
/** Read a fixed file prefix one record at a time, never allocating the whole journal as a string.
|
|
3624
|
+
* A reader cannot chase concurrent appends forever. Missing files are empty only when requested;
|
|
3625
|
+
* corruption, short reads, and other I/O errors remain failures. Closing the iterator closes the fd. */
|
|
3626
|
+
async function* readCommittedJsonLines(path, options = {}) {
|
|
3627
|
+
const fs = await import("node:fs/promises");
|
|
3628
|
+
let handle;
|
|
3629
|
+
try {
|
|
3630
|
+
handle = await fs.open(path, "r");
|
|
3631
|
+
} catch (error) {
|
|
3632
|
+
if (options.allowMissing && isNoEntError(error)) return;
|
|
3633
|
+
throw error;
|
|
3634
|
+
}
|
|
3635
|
+
try {
|
|
3636
|
+
const { size } = await handle.stat();
|
|
3637
|
+
if (size === 0) return;
|
|
3638
|
+
const { StringDecoder } = await import("node:string_decoder");
|
|
3639
|
+
const decoder = new StringDecoder("utf8");
|
|
3640
|
+
const stream = handle.createReadStream({
|
|
3641
|
+
autoClose: false,
|
|
3642
|
+
end: size - 1
|
|
3643
|
+
});
|
|
3644
|
+
const fragments = [];
|
|
3645
|
+
let lineNumber = 1;
|
|
3646
|
+
try {
|
|
3647
|
+
for await (const chunk of stream) {
|
|
3648
|
+
options.onBytes?.(chunk);
|
|
3649
|
+
const text = decoder.write(chunk);
|
|
3650
|
+
let start = 0;
|
|
3651
|
+
let end = text.indexOf("\n", start);
|
|
3652
|
+
while (end !== -1) {
|
|
3653
|
+
fragments.push(text.slice(start, end));
|
|
3654
|
+
const record = parseRecord(fragments.join(""), path, lineNumber++, true, options.allowWhitespaceLines);
|
|
3655
|
+
fragments.length = 0;
|
|
3656
|
+
if (record !== void 0) yield record.value;
|
|
3657
|
+
start = end + 1;
|
|
3658
|
+
end = text.indexOf("\n", start);
|
|
3659
|
+
}
|
|
3660
|
+
if (start < text.length) fragments.push(text.slice(start));
|
|
3661
|
+
}
|
|
3662
|
+
if (stream.bytesRead !== size) throw new Error(`${path}: journal changed while reading its committed prefix`);
|
|
3663
|
+
fragments.push(decoder.end());
|
|
3664
|
+
const record = parseRecord(fragments.join(""), path, lineNumber, false, options.allowWhitespaceLines);
|
|
3665
|
+
if (record !== void 0) yield record.value;
|
|
3666
|
+
} finally {
|
|
3667
|
+
stream.destroy();
|
|
3668
|
+
}
|
|
3669
|
+
} finally {
|
|
3670
|
+
await handle.close();
|
|
3671
|
+
}
|
|
3672
|
+
}
|
|
3673
|
+
/** An invalid unterminated tail may be a torn write; other parse failures are not evidence of one. */
|
|
3674
|
+
function parseRecord(line, source, lineNumber, terminated, allowWhitespaceLines = false) {
|
|
3675
|
+
if (line.length === 0 || allowWhitespaceLines && line.trim().length === 0) return void 0;
|
|
3676
|
+
try {
|
|
3677
|
+
return { value: JSON.parse(line) };
|
|
3678
|
+
} catch (cause) {
|
|
3679
|
+
if (!(cause instanceof SyntaxError)) throw cause;
|
|
3680
|
+
if (!terminated) return void 0;
|
|
3681
|
+
throw new Error(`${source}: malformed JSONL record at line ${lineNumber}`, { cause });
|
|
3682
|
+
}
|
|
3683
|
+
}
|
|
3684
|
+
async function appendCommittedJsonLine(path, record, acknowledge) {
|
|
3685
|
+
return commitJsonLine(path, record, acknowledge);
|
|
3686
|
+
}
|
|
3687
|
+
/** Serialize JSONL file updates across processes and aliases using Eval's existing file lock.
|
|
3688
|
+
* The caller owns validation and the record format. The lock path names the canonical file,
|
|
3689
|
+
* including its first write, so two directory aliases cannot acquire different write heads.
|
|
3690
|
+
* @internal
|
|
3691
|
+
*/
|
|
3692
|
+
async function withJsonlFileLock(path, operation) {
|
|
3693
|
+
const fs = await import("node:fs/promises");
|
|
3694
|
+
const { dirname } = await import("node:path");
|
|
3695
|
+
const { tryAcquireAtomicFileLock } = await import("@tangle-network/agent-eval/ledger-core");
|
|
3696
|
+
await fs.mkdir(dirname(path), { recursive: true });
|
|
3697
|
+
await (await fs.open(path, "a")).close();
|
|
3698
|
+
const canonicalPath = await fs.realpath(path);
|
|
3699
|
+
const deadline = Date.now() + 5e3;
|
|
3700
|
+
for (;;) {
|
|
3701
|
+
const acquisition = tryAcquireAtomicFileLock({ lockPath: `${canonicalPath}.lock` });
|
|
3702
|
+
if (!acquisition.acquired) {
|
|
3703
|
+
if (Date.now() >= deadline) throw new Error(`journal file lock unavailable after 5000ms: ${canonicalPath}`);
|
|
3704
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
3705
|
+
continue;
|
|
3706
|
+
}
|
|
3707
|
+
try {
|
|
3708
|
+
return await operation(canonicalPath);
|
|
3709
|
+
} finally {
|
|
3710
|
+
acquisition.lock.release();
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
}
|
|
3714
|
+
/** Read one keyed registry snapshot without an extra whole-file text allocation.
|
|
3715
|
+
* Historical registry files allow whitespace-only separators; append journals remain strict.
|
|
3716
|
+
* @internal
|
|
3717
|
+
*/
|
|
3718
|
+
async function readCommittedJsonFile(path) {
|
|
3719
|
+
const records = [];
|
|
3720
|
+
for await (const record of readCommittedJsonLines(path, {
|
|
3721
|
+
allowMissing: true,
|
|
3722
|
+
allowWhitespaceLines: true
|
|
3723
|
+
})) records.push(record);
|
|
3724
|
+
return records;
|
|
3725
|
+
}
|
|
3726
|
+
/** Replace one keyed row and publish one durable snapshot. The caller holds the file lock.
|
|
3727
|
+
* Keep unrelated rows in order and put the replacement last. No revision archive is retained.
|
|
3728
|
+
* @internal
|
|
3729
|
+
*/
|
|
3730
|
+
async function upsertCommittedJsonLine(path, record, key) {
|
|
3731
|
+
const encoded = JSON.stringify(record);
|
|
3732
|
+
if (encoded === void 0) throw new Error("JSONL records must be JSON serializable");
|
|
3733
|
+
const snapshot = JSON.parse(encoded);
|
|
3734
|
+
const id = key(snapshot);
|
|
3735
|
+
const updated = [];
|
|
3736
|
+
for await (const prior of readCommittedJsonLines(path, {
|
|
3737
|
+
allowMissing: true,
|
|
3738
|
+
allowWhitespaceLines: true
|
|
3739
|
+
})) if (key(prior) !== id) updated.push(prior);
|
|
3740
|
+
updated.push(snapshot);
|
|
3741
|
+
const { writeAtomicDurableFile } = await Promise.resolve().then(() => durable_file_exports);
|
|
3742
|
+
writeAtomicDurableFile(path, `${updated.map((row) => JSON.stringify(row)).join("\n")}\n`, { preserveMode: true });
|
|
3743
|
+
}
|
|
3744
|
+
/** Recover a torn tail under the same generation fence as durable publications. @internal */
|
|
3745
|
+
async function prepareJsonlAppend(path) {
|
|
3746
|
+
return fenceRunWrite(() => prepareJsonlTail(path));
|
|
3747
|
+
}
|
|
3748
|
+
/** True for a filesystem error meaning "the file is not there yet" — the one condition an
|
|
3749
|
+
* append-only reader treats as an empty log rather than a fault. */
|
|
3750
|
+
function isNoEntError(error) {
|
|
3751
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
3752
|
+
}
|
|
3753
|
+
// src/durable/spawn-journal/record-events.ts
|
|
3754
|
+
/** Decode the file journal's historical single-event and atomic-group envelopes. @internal */
|
|
3755
|
+
function spawnJournalRecordEvents(record) {
|
|
3756
|
+
if (!isRecord(record)) throw new Error("spawn journal corrupted: record must be an object");
|
|
3757
|
+
if (record.kind === "begin") return [];
|
|
3758
|
+
if (record.kind === "event") return [record.event];
|
|
3759
|
+
if (record.kind !== "events") throw new Error(`spawn journal corrupted: unknown record kind '${String(record.kind)}'`);
|
|
3760
|
+
if (!Array.isArray(record.events)) throw new Error("spawn journal group has no events array");
|
|
3761
|
+
return record.events;
|
|
3762
|
+
}
|
|
3763
|
+
// src/durable/spawn-journal/journal-store.ts
|
|
3764
|
+
/**
|
|
3765
|
+
* Append every event as one grouped publication where the journal supports it (`appendEvents`),
|
|
3766
|
+
* preserving legacy per-event appends otherwise — the SQL contexts' fenced head advances once.
|
|
3767
|
+
* @internal
|
|
3768
|
+
*/
|
|
3769
|
+
async function appendSpawnEvents(journal, root, events) {
|
|
3770
|
+
if (journal.appendEvents !== void 0) return journal.appendEvents(root, events);
|
|
3771
|
+
for (const event of events) await journal.appendEvent(root, event);
|
|
3772
|
+
}
|
|
3773
|
+
/**
|
|
3774
|
+
* JSONL on disk. One line per record: the first record is `begin`, subsequent records
|
|
3775
|
+
* are `event` or atomic `events` envelopes. `loadTree` replays the whole file,
|
|
3776
|
+
* filtering by `root`, and applies the same begin-precedes-events + unique-seq
|
|
3777
|
+
* corruption guards as the in-memory impl. Each append fsyncs so a crash between
|
|
3778
|
+
* writes never loses an acknowledged event.
|
|
3779
|
+
*
|
|
3780
|
+
* @stable
|
|
3781
|
+
*/
|
|
3782
|
+
var FileSpawnJournal = class {
|
|
3783
|
+
path;
|
|
3784
|
+
writeContext = captureRunWriteContext();
|
|
3785
|
+
appendTail = Promise.resolve();
|
|
3786
|
+
appendIndex;
|
|
3787
|
+
constructor(path) {
|
|
3788
|
+
this.path = path;
|
|
3789
|
+
}
|
|
3790
|
+
async loadTree(root) {
|
|
3791
|
+
let begun = false;
|
|
3792
|
+
const events = [];
|
|
3793
|
+
const index = new SpawnEventIndex(root);
|
|
3794
|
+
for await (const record of readCommittedJsonLines(this.path, { allowMissing: true })) {
|
|
3795
|
+
if (record.root !== root) continue;
|
|
3796
|
+
if (record.kind === "begin") begun = true;
|
|
3797
|
+
else {
|
|
3798
|
+
if (!begun) throw new Error(`spawn journal corrupted: event for tree '${root}' precedes its begin record`);
|
|
3799
|
+
for (const event of spawnJournalRecordEvents(record)) {
|
|
3800
|
+
index.assert(event);
|
|
3801
|
+
index.add(event);
|
|
3802
|
+
events.push(event);
|
|
3803
|
+
}
|
|
3804
|
+
}
|
|
3805
|
+
}
|
|
3806
|
+
return begun ? events : void 0;
|
|
3807
|
+
}
|
|
3808
|
+
async beginTree(root, at) {
|
|
3809
|
+
return this.serializeAppend(async () => {
|
|
3810
|
+
const state = await this.validationIndex();
|
|
3811
|
+
const existing = state.trees.get(root);
|
|
3812
|
+
if (existing) {
|
|
3813
|
+
if (existing.begunAt !== at) throw new Error(`spawn tree '${root}' already begun in ${this.path} at ${existing.begunAt}; refusing to overwrite with ${at}`);
|
|
3814
|
+
return;
|
|
3815
|
+
}
|
|
3816
|
+
state.stamp = await this.writeRecord({
|
|
3817
|
+
kind: "begin",
|
|
3818
|
+
root,
|
|
3819
|
+
at
|
|
3820
|
+
});
|
|
3821
|
+
state.trees.set(root, {
|
|
3822
|
+
begunAt: at,
|
|
3823
|
+
index: new SpawnEventIndex(root)
|
|
3824
|
+
});
|
|
3825
|
+
});
|
|
3826
|
+
}
|
|
3827
|
+
async appendEvent(root, ev) {
|
|
3828
|
+
const event = detachedSnapshot(ev, "spawn event");
|
|
3829
|
+
return this.serializeAppend(async () => {
|
|
3830
|
+
const state = await this.validationIndex();
|
|
3831
|
+
const tree = state.trees.get(root);
|
|
3832
|
+
if (tree === void 0) throw new Error(`appendEvent called for unknown spawn tree '${root}'; call beginTree first`);
|
|
3833
|
+
tree.index.assert(event);
|
|
3834
|
+
state.stamp = await this.writeRecord({
|
|
3835
|
+
kind: "event",
|
|
3836
|
+
root,
|
|
3837
|
+
event
|
|
3838
|
+
});
|
|
3839
|
+
tree.index.add(event);
|
|
3840
|
+
});
|
|
3841
|
+
}
|
|
3842
|
+
async appendEvents(root, events) {
|
|
3843
|
+
const captured = detachedSnapshot(events, "spawn events");
|
|
3844
|
+
return this.serializeAppend(async () => {
|
|
3845
|
+
const state = await this.validationIndex();
|
|
3846
|
+
const tree = state.trees.get(root);
|
|
3847
|
+
if (tree === void 0) throw new Error(`appendEvents called for unknown spawn tree '${root}'; call beginTree first`);
|
|
3848
|
+
try {
|
|
3849
|
+
for (const event of captured) {
|
|
3850
|
+
tree.index.assert(event);
|
|
3851
|
+
tree.index.add(event);
|
|
3852
|
+
}
|
|
3853
|
+
state.stamp = await this.writeRecord({
|
|
3854
|
+
kind: "events",
|
|
3855
|
+
root,
|
|
3856
|
+
events: captured
|
|
3857
|
+
});
|
|
3858
|
+
} catch (error) {
|
|
3859
|
+
this.appendIndex = void 0;
|
|
3860
|
+
throw error;
|
|
3861
|
+
}
|
|
3862
|
+
});
|
|
3863
|
+
}
|
|
3864
|
+
async validationIndex() {
|
|
3865
|
+
const stamp = await this.fileStamp();
|
|
3866
|
+
if (this.appendIndex && this.appendIndex.stamp === stamp) return this.appendIndex;
|
|
3867
|
+
this.appendIndex = void 0;
|
|
3868
|
+
const trees = /* @__PURE__ */ new Map();
|
|
3869
|
+
if (stamp !== void 0) for await (const record of readCommittedJsonLines(this.path)) if (record.kind === "begin") {
|
|
3870
|
+
if (!trees.has(record.root)) trees.set(record.root, {
|
|
3871
|
+
begunAt: record.at,
|
|
3872
|
+
index: new SpawnEventIndex(record.root)
|
|
3873
|
+
});
|
|
3874
|
+
} else {
|
|
3875
|
+
const tree = trees.get(record.root);
|
|
3876
|
+
if (!tree) throw new Error(`spawn journal corrupted: event for tree '${record.root}' precedes its begin record`);
|
|
3877
|
+
for (const event of spawnJournalRecordEvents(record)) {
|
|
3878
|
+
tree.index.assert(event);
|
|
3879
|
+
tree.index.add(event);
|
|
3880
|
+
}
|
|
3881
|
+
}
|
|
3882
|
+
if (await this.fileStamp() !== stamp) throw new Error("spawn journal changed while rebuilding its append index");
|
|
3883
|
+
this.appendIndex = {
|
|
3884
|
+
stamp,
|
|
3885
|
+
trees
|
|
3886
|
+
};
|
|
3887
|
+
return this.appendIndex;
|
|
3888
|
+
}
|
|
3889
|
+
async fileStamp() {
|
|
3890
|
+
const fs = await import("node:fs/promises");
|
|
3891
|
+
try {
|
|
3892
|
+
return journalFileStamp(await fs.stat(this.path, { bigint: true }));
|
|
3893
|
+
} catch (error) {
|
|
3894
|
+
if (isNoEntError(error)) return void 0;
|
|
3895
|
+
throw error;
|
|
3896
|
+
}
|
|
3897
|
+
}
|
|
3898
|
+
async serializeAppend(operation) {
|
|
3899
|
+
const append = this.appendTail.then(() => this.writeContext(operation));
|
|
3900
|
+
this.appendTail = append.catch(() => void 0);
|
|
3901
|
+
return append;
|
|
3902
|
+
}
|
|
3903
|
+
async writeRecord(record) {
|
|
3904
|
+
const fs = await import("node:fs/promises");
|
|
3905
|
+
const { dirname } = await import("node:path");
|
|
3906
|
+
try {
|
|
3907
|
+
await fs.mkdir(dirname(this.path), { recursive: true });
|
|
3908
|
+
return await appendCommittedJsonLine(this.path, record, (fd) => {
|
|
3909
|
+
const stamp = journalFileStamp(fstatSync(fd, { bigint: true }));
|
|
3910
|
+
if (journalFileStamp(statSync(this.path, { bigint: true })) !== stamp) throw new Error("spawn journal changed while acknowledging its append");
|
|
3911
|
+
return stamp;
|
|
3912
|
+
});
|
|
3913
|
+
} catch (error) {
|
|
3914
|
+
this.appendIndex = void 0;
|
|
3915
|
+
throw error;
|
|
3916
|
+
}
|
|
3917
|
+
}
|
|
3918
|
+
};
|
|
3919
|
+
/** Detect replacement, truncation, and same-size edits; this is not a cross-process write lock. */
|
|
3920
|
+
function journalFileStamp(stat) {
|
|
3921
|
+
return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;
|
|
3922
|
+
}
|
|
3923
|
+
export { traceRootWriter as $, runtimeProviderCode as A, sandboxLeafOutputFromEvents as At, retainedExecutorContext as B, createSandboxUsageLedger as Bt, runGenerationPath as C, spendFromUsageEvents as Ct, runtimeFailureCause as D, createSandboxToolPartState as Dt, reportedFailureCause as E, assertValidSpend as Et, executorEvidenceWriter as F, extractTransportEventIdentity as Ft, cappedDoublingMs as G, sandboxTerminalUsageField as Gt, workspaceCaptureWriter as H, isSandboxTerminalEvent as Ht, isProvisioningRefusal as I, parseCanonicalTransportEvent as It, unavailableResumeAt as J, parseCodexUsageRecord as Jt, jitteredMs as K, sumSandboxUsage as Kt, leafContinuationExecutionId as L, assertSandboxServedModel as Lt, BUDGET_CONTINUATION_TASK as M, sandboxProgressEvents as Mt, DISPATCH_NOT_ADMITTED_CODE as N, terminalTextFromSandboxEvent as Nt, runtimeFailureCauseSchema as O, mapSandboxEvent as Ot, RetainedExecutionPendingError as P, canonicalStreamEventFromSandboxEvent as Pt, TRACE_SUMMARY_SCHEMA as Q, prepareRetainedExecutor as R, canonicalSandboxUsageMode as Rt, mutateRunClaim as S, newUsageTotals as St, sha256 as T, assertValidBudget as Tt, UNAVAILABLE_CODES as U, notifySandboxEventObserver as Ut, retainedExecutorSeamKey as V, extractLlmCallEvent as Vt, UNAVAILABLE_STATUSES as W, sandboxEventServedBackend as Wt, unavailableSignalOfFailure as X, __exportAll as Xt, unavailableSignalInText as Y, recordValue as Yt, TRACE_ROOT_SCHEMA as Z, publishExclusiveDurableFile as _, defaultRedactorIdentityMaterial as _t, isNoEntError as a, executorFailure as at, captureRunWriteContext as b, createBudgetPool as bt, readCommittedJsonLines as c, hashJson as ct, SpawnEventIndex as d, causeChain as dt, detachedFrozen as et, closesCursorSlot as f, errMessage as ft, assertNoSymlinkDescendant as g, defaultRedactor as gt, appendDurableFile as h, errorText as ht, appendCommittedJsonLine as i, contentRef as it, settlementFailureCause as j, sandboxOutputMarker as jt, runtimeFailureRemediationSchema as k, mapSandboxToolEvent as kt, upsertCommittedJsonLine as l, credentialCommandExit as lt, outsideCursorNamespace as m, errorProperty as mt, appendSpawnEvents as n, shareParsedStrings as nt, prepareJsonlAppend as o, executorFailureInfra as ot, insideCursorNamespace as p, errorHttpStatus as pt, unavailablePauseMs as q, decodeHarnessUsage as qt, spawnJournalRecordEvents as r, contentAddress as rt, readCommittedJsonFile as s, executorFailureReason as st, FileSpawnJournal as t, detachedSnapshot as tt, withJsonlFileLock as u, isCredentialRefusal as ut, syncDurableDirectory as v, resolveRedactor as vt, runWithWriteClaim as w, spendFromUsageTotals as wt, fenceRunWrite as x, meterUsageEvent as xt, writeAtomicDurableFile as y, BudgetReconcileFault as yt, registerRetainedExecutorPreparation as z, createSandboxTurnEventCollector as zt };
|
|
3924
|
+
|
|
3925
|
+
//# sourceMappingURL=journal-store-BJPMUydp.js.map
|