@pi-unipi/background-tasks 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -0
- package/extensions/anthropic-attribution.ts +1 -0
- package/extensions/delegate-child.ts +1 -0
- package/extensions/fusion-child.ts +1 -0
- package/package.json +40 -0
- package/src/__tests__/anthropic-attribution.test.ts +195 -0
- package/src/__tests__/config.test.ts +137 -0
- package/src/__tests__/core.test.ts +493 -0
- package/src/__tests__/delegate-artifacts.test.ts +528 -0
- package/src/__tests__/delegate-budget.test.ts +456 -0
- package/src/__tests__/delegate-launch.test.ts +676 -0
- package/src/__tests__/delegate-result-package.test.ts +350 -0
- package/src/__tests__/delegate-seed.test.ts +392 -0
- package/src/__tests__/durable-fs.test.ts +559 -0
- package/src/__tests__/extension-api.test.ts +579 -0
- package/src/__tests__/fusion-artifacts.test.ts +1039 -0
- package/src/__tests__/fusion-budget.test.ts +1356 -0
- package/src/__tests__/fusion-claude-cache.test.ts +320 -0
- package/src/__tests__/fusion-config.test.ts +335 -0
- package/src/__tests__/fusion-context-prompts.test.ts +670 -0
- package/src/__tests__/fusion-evaluation.test.ts +315 -0
- package/src/__tests__/fusion-extraction-equivalence.test.ts +58 -0
- package/src/__tests__/fusion-golden-bytes.test.ts +35 -0
- package/src/__tests__/fusion-high-cardinality.test.ts +192 -0
- package/src/__tests__/fusion-model-selector.test.ts +205 -0
- package/src/__tests__/fusion-orchestrator.test.ts +1194 -0
- package/src/__tests__/fusion-rpc.test.ts +369 -0
- package/src/__tests__/fusion-sdk.test.ts +1226 -0
- package/src/__tests__/fusion-v5-core.test.ts +219 -0
- package/src/__tests__/fusion-validate-orchestrator.test.ts +240 -0
- package/src/__tests__/fusion-web-fetch.test.ts +485 -0
- package/src/__tests__/fusion-workflows.test.ts +59 -0
- package/src/__tests__/helpers/delegate-deterministic-seed.ts +109 -0
- package/src/__tests__/helpers/delegate-seed-subprocess.ts +10 -0
- package/src/__tests__/helpers/fusion-canonical-subprocess.ts +21 -0
- package/src/__tests__/helpers/fusion-canonical.ts +140 -0
- package/src/__tests__/helpers/fusion-fake-pi.ts +279 -0
- package/src/__tests__/helpers/fusion-golden-corpus.ts +500 -0
- package/src/__tests__/helpers/fusion-high-cardinality.ts +140 -0
- package/src/__tests__/helpers/normalize.ts +22 -0
- package/src/__tests__/helpers/pi-hook-contract-evidence.json +18 -0
- package/src/__tests__/pi-launch.test.ts +202 -0
- package/src/__tests__/registry.test.ts +1580 -0
- package/src/__tests__/scripted-provider/delegate-ambient-provider.test.ts +130 -0
- package/src/__tests__/scripted-provider/delegate-child-guard.test.ts +631 -0
- package/src/__tests__/scripted-provider/delegate-guard-provider.ts +403 -0
- package/src/__tests__/scripted-provider/follow-up.test.ts +448 -0
- package/src/__tests__/scripted-provider/fusion-output-recovery.test.ts +132 -0
- package/src/__tests__/scripted-provider/fusion-reason.test.ts +310 -0
- package/src/__tests__/scripted-provider/fusion-runtime-guard.test.ts +163 -0
- package/src/__tests__/scripted-provider/hook-contract-provider.ts +179 -0
- package/src/__tests__/scripted-provider/hook-probe-a.ts +3 -0
- package/src/__tests__/scripted-provider/hook-probe-b.ts +3 -0
- package/src/__tests__/scripted-provider/hook-probe-extension.ts +126 -0
- package/src/__tests__/scripted-provider/output-recovery-provider.ts +153 -0
- package/src/__tests__/scripted-provider/pi-hook-contract-evidence.json +18 -0
- package/src/__tests__/scripted-provider/pi-hook-contract.test.ts +477 -0
- package/src/__tests__/scripted-provider/runtime-guard-probe.ts +28 -0
- package/src/__tests__/scripted-provider/runtime-guard-provider.ts +49 -0
- package/src/__tests__/scripted-provider/scripted-provider-extension.ts +408 -0
- package/src/__tests__/task-manager.test.ts +479 -0
- package/src/__tests__/windows-taskkill.test.ts +161 -0
- package/src/anthropic-attribution-path.ts +21 -0
- package/src/anthropic-attribution.ts +1983 -0
- package/src/attested-pi-run.ts +612 -0
- package/src/child-process.ts +55 -0
- package/src/common.ts +8 -0
- package/src/config.ts +292 -0
- package/src/context-parent-snapshot.ts +142 -0
- package/src/context-token-budget.ts +903 -0
- package/src/context-visible-conversation-v2.ts +551 -0
- package/src/delegate/artifacts.ts +487 -0
- package/src/delegate/budget.ts +415 -0
- package/src/delegate/hook-contract-evidence.json +18 -0
- package/src/delegate/hook-contract.ts +154 -0
- package/src/delegate/launch.ts +497 -0
- package/src/delegate/result-package.ts +459 -0
- package/src/delegate/runner.ts +449 -0
- package/src/delegate/seed.ts +423 -0
- package/src/delegate/types.ts +323 -0
- package/src/delegate-child-extension.ts +978 -0
- package/src/delegate-extension.ts +806 -0
- package/src/durable-fs.ts +386 -0
- package/src/extension-api.ts +548 -0
- package/src/fixtures/delegate-context-incident.json +17 -0
- package/src/fixtures/fusion-golden-bytes.json +310 -0
- package/src/fixtures/fusion-validate-golden-bytes.json +282 -0
- package/src/fusion/artifacts.ts +967 -0
- package/src/fusion/budget.ts +1162 -0
- package/src/fusion/child-protocol.ts +305 -0
- package/src/fusion/claude-cache.ts +207 -0
- package/src/fusion/clean-context.ts +91 -0
- package/src/fusion/config.ts +449 -0
- package/src/fusion/context.ts +265 -0
- package/src/fusion/evaluation.ts +800 -0
- package/src/fusion/orchestrator.ts +1288 -0
- package/src/fusion/output-contract.ts +34 -0
- package/src/fusion/pi-child.ts +2373 -0
- package/src/fusion/prompts.ts +345 -0
- package/src/fusion/result-package.ts +959 -0
- package/src/fusion/source-policy.ts +257 -0
- package/src/fusion/types.ts +1139 -0
- package/src/fusion/web-fetch.ts +1060 -0
- package/src/fusion/workflows.ts +184 -0
- package/src/fusion-child-extension.ts +1052 -0
- package/src/fusion-extension.ts +1293 -0
- package/src/index.ts +295 -0
- package/src/pi-launch.ts +225 -0
- package/src/registry.ts +2424 -0
- package/src/settings-overlay.ts +208 -0
- package/src/task-manager.ts +774 -0
- package/src/tools.ts +530 -0
- package/src/turndown.d.ts +15 -0
- package/src/types.ts +963 -0
- package/src/ui/fusion-model-selector.ts +322 -0
- package/src/windows-taskkill.ts +250 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import {
|
|
5
|
+
DELEGATE_FRAMING_RESERVE_TOKENS,
|
|
6
|
+
DELEGATE_INLINE_ANSWER_BYTES,
|
|
7
|
+
DELEGATE_MIN_CONTEXT_WINDOW_TOKENS,
|
|
8
|
+
DELEGATE_MIN_USABLE_INPUT_TOKENS,
|
|
9
|
+
DELEGATE_RESERVED_OUTPUT_TOKENS,
|
|
10
|
+
DELEGATE_SAFETY_RESERVE_TOKENS,
|
|
11
|
+
assertDelegateAdmission,
|
|
12
|
+
delegateAllowedInputTokens,
|
|
13
|
+
evaluateDelegateRuntimeBudget,
|
|
14
|
+
planDelegateAdmission,
|
|
15
|
+
resolveDelegateLimits,
|
|
16
|
+
} from '../delegate/budget.js';
|
|
17
|
+
import { DelegateError, type DelegatePinnedRoute } from '../delegate/types.js';
|
|
18
|
+
import {
|
|
19
|
+
TOKEN_BUDGET_AFFINE_F_TOKENS,
|
|
20
|
+
TOKEN_BUDGET_FAMILY_CALIBRATIONS,
|
|
21
|
+
TOKEN_BUDGET_PROVABLE_RATE_X100,
|
|
22
|
+
estimateInputTokens,
|
|
23
|
+
maxKnownTextBytesForTokens,
|
|
24
|
+
} from '../context-token-budget.js';
|
|
25
|
+
|
|
26
|
+
function route(
|
|
27
|
+
contextWindow: number,
|
|
28
|
+
provider = 'openai-codex',
|
|
29
|
+
model = 'gpt-5.5',
|
|
30
|
+
): DelegatePinnedRoute {
|
|
31
|
+
return {
|
|
32
|
+
provider,
|
|
33
|
+
model,
|
|
34
|
+
qualified_id: `${provider}/${model}`,
|
|
35
|
+
context_window_tokens: contextWindow,
|
|
36
|
+
thinking_level: 'medium',
|
|
37
|
+
origin: 'explicit',
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const INCIDENT = JSON.parse(
|
|
42
|
+
readFileSync(new URL('../fixtures/delegate-context-incident.json', import.meta.url), 'utf8'),
|
|
43
|
+
) as Record<string, unknown>;
|
|
44
|
+
|
|
45
|
+
function incidentNumber(key: string): number {
|
|
46
|
+
const value = INCIDENT[key];
|
|
47
|
+
if (typeof value !== 'number') throw new Error(`incident fixture ${key} must be numeric`);
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const LIMITS = {
|
|
52
|
+
max_turns: 24,
|
|
53
|
+
max_tool_calls: 120,
|
|
54
|
+
timeout_seconds: 900,
|
|
55
|
+
max_tool_result_bytes: 65_536,
|
|
56
|
+
max_total_tool_output_bytes: 67_108_864,
|
|
57
|
+
max_answer_bytes: 4_194_304,
|
|
58
|
+
allowed_input_tokens: 0,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function maxDelegateKnownBytes(pinnedRoute: DelegatePinnedRoute): number {
|
|
62
|
+
const allowed = delegateAllowedInputTokens(pinnedRoute);
|
|
63
|
+
const probe = planDelegateAdmission({
|
|
64
|
+
route: pinnedRoute,
|
|
65
|
+
childPrompt: 'x '.repeat(30_000),
|
|
66
|
+
childSystemPrompt: '',
|
|
67
|
+
limits: LIMITS,
|
|
68
|
+
});
|
|
69
|
+
const rate = probe.route.rate_source;
|
|
70
|
+
return Math.floor(
|
|
71
|
+
((allowed - rate.affine_f_tokens) * rate.effective_rate_bytes_per_token_x100) /
|
|
72
|
+
100,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function normalText(bytes: number): string {
|
|
77
|
+
return `${'x '.repeat(Math.floor(bytes / 2))}${bytes % 2 === 0 ? '' : 'x'}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function delegateTokenForecast(bytes: number, allowedInputTokens: number): number {
|
|
81
|
+
return estimateInputTokens({
|
|
82
|
+
family: 'openai-codex',
|
|
83
|
+
allowedInputTokens,
|
|
84
|
+
scope: 'delegate_launch',
|
|
85
|
+
segments: [{ kind: 'known_text', bytes, multibyteBytes: 0, denseBytes: 0 }],
|
|
86
|
+
}).tokens;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
void describe('delegate route capacity', () => {
|
|
90
|
+
void it('subtracts every documented reserve from the context window', () => {
|
|
91
|
+
const window = 200_000;
|
|
92
|
+
assert.equal(
|
|
93
|
+
delegateAllowedInputTokens(route(window)),
|
|
94
|
+
window -
|
|
95
|
+
DELEGATE_RESERVED_OUTPUT_TOKENS -
|
|
96
|
+
DELEGATE_FRAMING_RESERVE_TOKENS -
|
|
97
|
+
DELEGATE_SAFETY_RESERVE_TOKENS,
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
void it('accepts exactly the documented minimum window and rejects one token below', () => {
|
|
102
|
+
assert.equal(
|
|
103
|
+
delegateAllowedInputTokens(route(DELEGATE_MIN_CONTEXT_WINDOW_TOKENS)),
|
|
104
|
+
DELEGATE_MIN_USABLE_INPUT_TOKENS,
|
|
105
|
+
);
|
|
106
|
+
assert.throws(
|
|
107
|
+
() => delegateAllowedInputTokens(route(DELEGATE_MIN_CONTEXT_WINDOW_TOKENS - 1)),
|
|
108
|
+
(error: unknown) =>
|
|
109
|
+
error instanceof DelegateError &&
|
|
110
|
+
error.code === 'route_capacity_unknown' &&
|
|
111
|
+
error.childCreated === false,
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
void it('never assumes a default window for unusable capacity', () => {
|
|
116
|
+
for (const window of [0, -1, Number.NaN, Number.POSITIVE_INFINITY, 1.5]) {
|
|
117
|
+
assert.throws(
|
|
118
|
+
() => delegateAllowedInputTokens(route(window)),
|
|
119
|
+
(error: unknown) =>
|
|
120
|
+
error instanceof DelegateError && error.code === 'route_capacity_unknown',
|
|
121
|
+
`window ${String(window)} must be refused, not defaulted`,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
void describe('delegate launch admission', () => {
|
|
128
|
+
void it('fits when the seed is comfortably under the route allowance', () => {
|
|
129
|
+
const pinnedRoute = route(200_000);
|
|
130
|
+
const plan = planDelegateAdmission({
|
|
131
|
+
route: pinnedRoute,
|
|
132
|
+
childPrompt: 'x'.repeat(1000),
|
|
133
|
+
childSystemPrompt: 'y'.repeat(500),
|
|
134
|
+
limits: LIMITS,
|
|
135
|
+
});
|
|
136
|
+
assert.equal(plan.fits, true);
|
|
137
|
+
assert.equal(plan.schema_version, 'pi-background-tasks.delegate-budget-plan.v3');
|
|
138
|
+
assert.equal(plan.policy.id, 'delegate-budget-policy-v3');
|
|
139
|
+
assert.equal(plan.child_prompt_utf8_bytes, 1000);
|
|
140
|
+
assert.equal(plan.system_prompt_utf8_bytes, 500);
|
|
141
|
+
assert.equal(plan.launch_utf8_bytes, 1500);
|
|
142
|
+
assert.equal(plan.launch_input_tokens_upper_bound, plan.estimate.tokens);
|
|
143
|
+
assert.equal(plan.route.family, 'openai-codex');
|
|
144
|
+
assert.equal(plan.route.rate_source.source, 'delegate_conservative');
|
|
145
|
+
assert.doesNotThrow(() => {
|
|
146
|
+
assertDelegateAdmission(plan);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
void it('accepts exactly at the affine boundary and rejects one byte past it', () => {
|
|
151
|
+
const pinnedRoute = route(200_000);
|
|
152
|
+
const exactBytes = maxDelegateKnownBytes(pinnedRoute);
|
|
153
|
+
const atLimit = planDelegateAdmission({
|
|
154
|
+
route: pinnedRoute,
|
|
155
|
+
childPrompt: normalText(exactBytes),
|
|
156
|
+
childSystemPrompt: '',
|
|
157
|
+
limits: LIMITS,
|
|
158
|
+
});
|
|
159
|
+
assert.equal(atLimit.fits, true);
|
|
160
|
+
assert.equal(atLimit.signed_headroom_tokens, 0);
|
|
161
|
+
assert.doesNotThrow(() => {
|
|
162
|
+
assertDelegateAdmission(atLimit);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const overLimit = planDelegateAdmission({
|
|
166
|
+
route: pinnedRoute,
|
|
167
|
+
childPrompt: normalText(exactBytes + 1),
|
|
168
|
+
childSystemPrompt: '',
|
|
169
|
+
limits: LIMITS,
|
|
170
|
+
});
|
|
171
|
+
assert.equal(overLimit.fits, false);
|
|
172
|
+
assert.throws(
|
|
173
|
+
() => {
|
|
174
|
+
assertDelegateAdmission(overLimit);
|
|
175
|
+
},
|
|
176
|
+
(error: unknown) =>
|
|
177
|
+
error instanceof DelegateError &&
|
|
178
|
+
error.code === 'seed_budget_exceeded' &&
|
|
179
|
+
error.childCreated === false,
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
void it('counts the child system prompt as input, not as free framing', () => {
|
|
184
|
+
const pinnedRoute = route(200_000);
|
|
185
|
+
const seedBytes = maxDelegateKnownBytes(pinnedRoute);
|
|
186
|
+
const plan = planDelegateAdmission({
|
|
187
|
+
route: pinnedRoute,
|
|
188
|
+
childPrompt: normalText(seedBytes),
|
|
189
|
+
childSystemPrompt: 'y ',
|
|
190
|
+
limits: LIMITS,
|
|
191
|
+
});
|
|
192
|
+
assert.equal(plan.fits, false, 'the system prompt must consume the same allowance');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
void it('measures multi-byte UTF-8 by bytes and persists the provable advisory ceiling', () => {
|
|
196
|
+
const emoji = '👩👩👧👦';
|
|
197
|
+
const plan = planDelegateAdmission({
|
|
198
|
+
route: route(200_000),
|
|
199
|
+
childPrompt: emoji.repeat(100),
|
|
200
|
+
childSystemPrompt: '',
|
|
201
|
+
limits: LIMITS,
|
|
202
|
+
});
|
|
203
|
+
assert.equal(plan.child_prompt_utf8_bytes, Buffer.byteLength(emoji.repeat(100), 'utf8'));
|
|
204
|
+
assert.ok(plan.child_prompt_utf8_bytes > emoji.repeat(100).length);
|
|
205
|
+
assert.equal(plan.estimate.byte_class_breakdown.multibyte_bytes, plan.child_prompt_utf8_bytes);
|
|
206
|
+
assert.equal(plan.estimate.perSegment[0]?.multibyte_provable_tokens, plan.child_prompt_utf8_bytes);
|
|
207
|
+
assert.ok((plan.estimate.perSegment[0]?.multibyte_tokens ?? 0) < plan.child_prompt_utf8_bytes);
|
|
208
|
+
assert.equal(plan.dominant_byte_class, 'multibyte');
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
void it('scope guard refuses the codex 23,674-byte delegate floor case', () => {
|
|
212
|
+
const pinnedRoute = route(DELEGATE_MIN_CONTEXT_WINDOW_TOKENS, 'openai-codex', 'gpt-5.5');
|
|
213
|
+
assert.equal(delegateAllowedInputTokens(pinnedRoute), 8192);
|
|
214
|
+
const plan = planDelegateAdmission({
|
|
215
|
+
route: pinnedRoute,
|
|
216
|
+
childPrompt: 'x'.repeat(23_674),
|
|
217
|
+
childSystemPrompt: '',
|
|
218
|
+
limits: LIMITS,
|
|
219
|
+
});
|
|
220
|
+
assert.equal(plan.route.rate_source.source, 'delegate_conservative');
|
|
221
|
+
assert.equal(plan.fits, false);
|
|
222
|
+
assert.throws(
|
|
223
|
+
() => assertDelegateAdmission(plan),
|
|
224
|
+
(error: unknown) => error instanceof DelegateError && error.code === 'seed_budget_exceeded',
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
void it('scope guard refuses the anthropic 13,286-byte delegate zero-headroom case', () => {
|
|
229
|
+
const pinnedRoute = route(DELEGATE_MIN_CONTEXT_WINDOW_TOKENS, 'anthropic', 'claude-opus-5');
|
|
230
|
+
assert.equal(delegateAllowedInputTokens(pinnedRoute), 8192);
|
|
231
|
+
const beforeAnthropicForecast = estimateInputTokens({
|
|
232
|
+
family: 'anthropic',
|
|
233
|
+
allowedInputTokens: 8192,
|
|
234
|
+
scope: 'fusion',
|
|
235
|
+
segments: [{ kind: 'known_text', bytes: 13_286, multibyteBytes: 0, denseBytes: 0 }],
|
|
236
|
+
}).tokens;
|
|
237
|
+
assert.equal(beforeAnthropicForecast, 8_192);
|
|
238
|
+
const plan = planDelegateAdmission({
|
|
239
|
+
route: pinnedRoute,
|
|
240
|
+
childPrompt: 'x'.repeat(13_286),
|
|
241
|
+
childSystemPrompt: '',
|
|
242
|
+
limits: LIMITS,
|
|
243
|
+
});
|
|
244
|
+
assert.equal(plan.route.rate_source.source, 'delegate_conservative');
|
|
245
|
+
assert.equal(plan.route.rate_source.backed, true);
|
|
246
|
+
assert.equal(plan.route.rate_source.effective_rate_bytes_per_token_x100, TOKEN_BUDGET_PROVABLE_RATE_X100);
|
|
247
|
+
assert.equal(plan.launch_input_tokens_upper_bound, 13_798);
|
|
248
|
+
assert.equal(plan.fits, false);
|
|
249
|
+
assert.throws(
|
|
250
|
+
() => assertDelegateAdmission(plan),
|
|
251
|
+
(error: unknown) => {
|
|
252
|
+
assert.ok(error instanceof DelegateError);
|
|
253
|
+
assert.equal(error.code, 'seed_budget_exceeded');
|
|
254
|
+
assert.equal(error.budget?.rate_source.source, 'delegate_conservative');
|
|
255
|
+
assert.equal(error.budget?.backed, true);
|
|
256
|
+
assert.equal(error.budget?.dominant_byte_class, 'normal');
|
|
257
|
+
assert.match(error.message, /backed=true/);
|
|
258
|
+
assert.match(error.message, /dominant_byte_class=normal/);
|
|
259
|
+
return true;
|
|
260
|
+
},
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
void it('replays the incident byte-class contract with calibrated runway and a provable counter-forecast', () => {
|
|
265
|
+
const promptBytes = incidentNumber('child_prompt_utf8_bytes');
|
|
266
|
+
const multibyteBytes = incidentNumber('launch_multibyte_bytes');
|
|
267
|
+
assert.equal(multibyteBytes % Buffer.byteLength('€', 'utf8'), 0);
|
|
268
|
+
const childPrompt = `${normalText(promptBytes - multibyteBytes)}${'€'.repeat(multibyteBytes / 3)}`;
|
|
269
|
+
const childSystemPrompt = normalText(incidentNumber('system_prompt_utf8_bytes'));
|
|
270
|
+
assert.equal(Buffer.byteLength(childPrompt, 'utf8'), promptBytes);
|
|
271
|
+
const pinnedRoute = route(
|
|
272
|
+
incidentNumber('context_window_tokens'),
|
|
273
|
+
'openai-codex',
|
|
274
|
+
'gpt-5.6-sol',
|
|
275
|
+
);
|
|
276
|
+
const plan = planDelegateAdmission({
|
|
277
|
+
route: pinnedRoute,
|
|
278
|
+
childPrompt,
|
|
279
|
+
childSystemPrompt,
|
|
280
|
+
limits: LIMITS,
|
|
281
|
+
});
|
|
282
|
+
assert.equal(plan.launch_utf8_bytes, incidentNumber('launch_utf8_bytes'));
|
|
283
|
+
assert.equal(plan.byte_class_breakdown.normal_bytes, incidentNumber('launch_normal_bytes'));
|
|
284
|
+
assert.equal(plan.byte_class_breakdown.multibyte_bytes, multibyteBytes);
|
|
285
|
+
assert.equal(plan.route.allowed_input_tokens, incidentNumber('allowed_input_tokens'));
|
|
286
|
+
assert.equal(plan.route.rate_source.source, 'calibrated_large_window');
|
|
287
|
+
assert.equal(
|
|
288
|
+
plan.route.rate_source.effective_rate_bytes_per_token_x100,
|
|
289
|
+
incidentNumber('calibrated_rate_bytes_per_token_x100'),
|
|
290
|
+
);
|
|
291
|
+
assert.equal(plan.fits, true);
|
|
292
|
+
assert.ok(plan.launch_input_tokens_upper_bound < 70_000);
|
|
293
|
+
assert.equal(
|
|
294
|
+
plan.conservative_estimate.tokens,
|
|
295
|
+
incidentNumber('old_one_byte_normal_two_byte_multibyte_forecast'),
|
|
296
|
+
);
|
|
297
|
+
assert.equal(
|
|
298
|
+
plan.conservative_launch_input_tokens,
|
|
299
|
+
incidentNumber('provable_one_byte_per_token_forecast'),
|
|
300
|
+
);
|
|
301
|
+
assert.equal(plan.conservative_launch_fits, true);
|
|
302
|
+
assert.ok(plan.retained_growth_budget_tokens > 240_000);
|
|
303
|
+
assert.equal(
|
|
304
|
+
plan.finalization_input_reserve_tokens,
|
|
305
|
+
incidentNumber('finalization_reserve_tokens'),
|
|
306
|
+
);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
void it('publishes the provable ceiling for multibyte-heavy conservative forecasts', () => {
|
|
310
|
+
const pinnedRoute = route(128_672, 'openai-codex', 'gpt-5.6-sol');
|
|
311
|
+
const plan = planDelegateAdmission({
|
|
312
|
+
route: pinnedRoute,
|
|
313
|
+
childPrompt: '€'.repeat(40_000),
|
|
314
|
+
childSystemPrompt: '',
|
|
315
|
+
limits: LIMITS,
|
|
316
|
+
});
|
|
317
|
+
assert.equal(plan.route.allowed_input_tokens, 100_000);
|
|
318
|
+
assert.ok(
|
|
319
|
+
plan.conservative_estimate.tokens < plan.route.allowed_input_tokens,
|
|
320
|
+
'the legacy 2 B/token multibyte diagnostic demonstrates the original false fit',
|
|
321
|
+
);
|
|
322
|
+
assert.equal(plan.conservative_launch_input_tokens, 120_512);
|
|
323
|
+
assert.equal(plan.conservative_launch_fits, false);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
void it('unknown family is loud and no looser than calibrated families', () => {
|
|
327
|
+
const unknownRoute = route(200_000, 'mystery-provider', 'mystery-model');
|
|
328
|
+
const plan = planDelegateAdmission({
|
|
329
|
+
route: unknownRoute,
|
|
330
|
+
childPrompt: 'x',
|
|
331
|
+
childSystemPrompt: '',
|
|
332
|
+
limits: LIMITS,
|
|
333
|
+
});
|
|
334
|
+
const calibratedRates = Object.values(TOKEN_BUDGET_FAMILY_CALIBRATIONS)
|
|
335
|
+
.filter((entry) => entry.provenance.backed)
|
|
336
|
+
.map((entry) => entry.rate_bytes_per_token_x100);
|
|
337
|
+
assert.equal(plan.route.family, 'unknown');
|
|
338
|
+
assert.equal(plan.route.rate_source.backed, false);
|
|
339
|
+
assert.ok(plan.route.rate_source.warning?.includes('unknown provider'));
|
|
340
|
+
assert.ok(
|
|
341
|
+
plan.route.rate_source.effective_rate_bytes_per_token_x100 <= Math.min(...calibratedRates),
|
|
342
|
+
);
|
|
343
|
+
const oversized = planDelegateAdmission({
|
|
344
|
+
route: unknownRoute,
|
|
345
|
+
childPrompt: 'x'.repeat(delegateAllowedInputTokens(unknownRoute)),
|
|
346
|
+
childSystemPrompt: '',
|
|
347
|
+
limits: LIMITS,
|
|
348
|
+
});
|
|
349
|
+
assert.throws(
|
|
350
|
+
() => assertDelegateAdmission(oversized),
|
|
351
|
+
(error: unknown) => {
|
|
352
|
+
assert.ok(error instanceof DelegateError);
|
|
353
|
+
assert.match(error.message, /unknown provider/);
|
|
354
|
+
return true;
|
|
355
|
+
},
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
void it('states what was preserved and how to remediate, and never clamps', () => {
|
|
360
|
+
const pinnedRoute = route(200_000);
|
|
361
|
+
const plan = planDelegateAdmission({
|
|
362
|
+
route: pinnedRoute,
|
|
363
|
+
childPrompt: 'x'.repeat(maxDelegateKnownBytes(pinnedRoute) * 2),
|
|
364
|
+
childSystemPrompt: '',
|
|
365
|
+
limits: LIMITS,
|
|
366
|
+
});
|
|
367
|
+
try {
|
|
368
|
+
assertDelegateAdmission(plan);
|
|
369
|
+
assert.fail('oversized admission must throw');
|
|
370
|
+
} catch (error) {
|
|
371
|
+
assert.ok(error instanceof DelegateError);
|
|
372
|
+
assert.equal(error.code, 'seed_budget_exceeded');
|
|
373
|
+
assert.equal(error.childCreated, false);
|
|
374
|
+
assert.match(error.message, /No child process, child session, or artifact was created/);
|
|
375
|
+
assert.match(error.message, /Nothing was clipped, dropped, or substituted/);
|
|
376
|
+
assert.ok(error.remediation.length > 0);
|
|
377
|
+
assert.match(error.describe(), /Child process created: no/);
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
void describe('delegate runtime governor', () => {
|
|
383
|
+
void it('is total and never throws from inside a hook', () => {
|
|
384
|
+
const pinnedRoute = route(200_000);
|
|
385
|
+
for (const bytes of [0, 1, 10_000, 10_000_000]) {
|
|
386
|
+
const verdict = evaluateDelegateRuntimeBudget(
|
|
387
|
+
{ retainedInputBytes: bytes, retainedInputMultibyteBytes: 0, retainedInputDenseBytes: 0 },
|
|
388
|
+
5_000,
|
|
389
|
+
pinnedRoute,
|
|
390
|
+
);
|
|
391
|
+
assert.equal(verdict.measuredTokens, delegateTokenForecast(bytes, 5_000));
|
|
392
|
+
assert.equal(verdict.allowedTokens, 5_000);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
void it('permits exactly the allowance and refuses one token past it', () => {
|
|
397
|
+
const allowed = 1_000;
|
|
398
|
+
const pinnedRoute = route(allowed + 28_672);
|
|
399
|
+
const exactBytes = maxKnownTextBytesForTokens({
|
|
400
|
+
family: 'openai-codex',
|
|
401
|
+
allowedInputTokens: allowed,
|
|
402
|
+
scope: 'delegate_launch',
|
|
403
|
+
});
|
|
404
|
+
const exact = evaluateDelegateRuntimeBudget(
|
|
405
|
+
{
|
|
406
|
+
retainedInputBytes: exactBytes,
|
|
407
|
+
retainedInputMultibyteBytes: 0,
|
|
408
|
+
retainedInputDenseBytes: 0,
|
|
409
|
+
},
|
|
410
|
+
allowed,
|
|
411
|
+
pinnedRoute,
|
|
412
|
+
);
|
|
413
|
+
assert.equal(exact.withinBudget, true);
|
|
414
|
+
assert.equal(exact.overageTokens, 0);
|
|
415
|
+
|
|
416
|
+
const over = evaluateDelegateRuntimeBudget(
|
|
417
|
+
{
|
|
418
|
+
retainedInputBytes: exactBytes + 1,
|
|
419
|
+
retainedInputMultibyteBytes: 0,
|
|
420
|
+
retainedInputDenseBytes: 0,
|
|
421
|
+
},
|
|
422
|
+
allowed,
|
|
423
|
+
pinnedRoute,
|
|
424
|
+
);
|
|
425
|
+
assert.equal(over.withinBudget, false);
|
|
426
|
+
assert.equal(over.overageTokens, 1);
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
void describe('delegate limits', () => {
|
|
431
|
+
void it('applies documented defaults and derives the route allowance', () => {
|
|
432
|
+
const limits = resolveDelegateLimits(route(200_000));
|
|
433
|
+
assert.equal(limits.max_turns, 24);
|
|
434
|
+
assert.equal(limits.max_tool_calls, 120);
|
|
435
|
+
assert.equal(limits.timeout_seconds, 1200);
|
|
436
|
+
assert.equal(limits.allowed_input_tokens, delegateAllowedInputTokens(route(200_000)));
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
void it('rejects non-positive and non-integer overrides loudly', () => {
|
|
440
|
+
for (const value of [0, -1, 1.5, Number.NaN]) {
|
|
441
|
+
assert.throws(
|
|
442
|
+
() => resolveDelegateLimits(route(200_000), { maxTurns: value }),
|
|
443
|
+
(error: unknown) => error instanceof DelegateError && error.code === 'invalid_arguments',
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
void it('keeps the inline answer cap strictly below the answer capture cap', () => {
|
|
449
|
+
const limits = resolveDelegateLimits(route(200_000));
|
|
450
|
+
assert.ok(
|
|
451
|
+
DELEGATE_INLINE_ANSWER_BYTES < limits.max_answer_bytes,
|
|
452
|
+
'an answer must be capturable even when it is too large to inline',
|
|
453
|
+
);
|
|
454
|
+
assert.equal(TOKEN_BUDGET_AFFINE_F_TOKENS, 512);
|
|
455
|
+
});
|
|
456
|
+
});
|