@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,459 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { canonicalJson } from '../attested-pi-run.js';
|
|
3
|
+
import {
|
|
4
|
+
DELEGATE_RECEIPT_SCHEMA_VERSION,
|
|
5
|
+
DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION,
|
|
6
|
+
DelegateError,
|
|
7
|
+
type DelegateAnswerBlock,
|
|
8
|
+
type DelegateResultPackageV1,
|
|
9
|
+
type DelegateRouteAttestation,
|
|
10
|
+
type DelegateSpillReceipt,
|
|
11
|
+
type DelegateUsageReport,
|
|
12
|
+
} from './types.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The delegate answer data plane.
|
|
16
|
+
*
|
|
17
|
+
* A delegate child commits exactly one self-contained package: temp-write,
|
|
18
|
+
* fsync, rename, directory fsync. The rename is the commit point. A package
|
|
19
|
+
* present under its final name is complete; its absence means no answer was
|
|
20
|
+
* accepted. There is no second channel to reconcile, so a child that dies after
|
|
21
|
+
* emitting bytes but before committing can never be mistaken for a success.
|
|
22
|
+
*
|
|
23
|
+
* Every acceptance path verifies per-block and aggregate SHA-256 before it
|
|
24
|
+
* returns a single byte, and it returns bytes from the same buffer it verified
|
|
25
|
+
* rather than re-reading the file.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
export const DELEGATE_RESULT_PACKAGE_FILENAME = 'result.json';
|
|
29
|
+
|
|
30
|
+
function sha256Bytes(bytes: Buffer): string {
|
|
31
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isRecord(value: unknown): value is Record<PropertyKey, unknown> {
|
|
35
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function fail(message: string, code: DelegateError['code'], taskId?: string): never {
|
|
39
|
+
const details: ConstructorParameters<typeof DelegateError>[1] = { code, childCreated: true };
|
|
40
|
+
if (taskId !== undefined) details.taskId = taskId;
|
|
41
|
+
throw new DelegateError(message, details);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Reject text that cannot round-trip through UTF-8.
|
|
46
|
+
*
|
|
47
|
+
* A lone surrogate would be silently replaced with U+FFFD by a naive encode,
|
|
48
|
+
* changing the answer bytes without anyone noticing. That is exactly the class
|
|
49
|
+
* of silent corruption this package refuses, so it is a loud typed failure.
|
|
50
|
+
* Well-formed U+2028/U+2029 and unnormalized sequences are preserved untouched.
|
|
51
|
+
*/
|
|
52
|
+
export function assertWellFormedUtf8(text: string, label: string): Buffer {
|
|
53
|
+
const encoded = Buffer.from(text, 'utf8');
|
|
54
|
+
if (encoded.toString('utf8') !== text) {
|
|
55
|
+
fail(
|
|
56
|
+
`${label} contains text that cannot be represented in UTF-8 without substitution (lone surrogate); the answer is not silently repaired`,
|
|
57
|
+
'child_result_encoding_invalid',
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return encoded;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface BuildDelegateResultPackageInput {
|
|
64
|
+
taskId: string;
|
|
65
|
+
launchNonce: string;
|
|
66
|
+
seedSha256: string;
|
|
67
|
+
directiveSha256: string;
|
|
68
|
+
route: { provider: string; model: string };
|
|
69
|
+
routeAttestations: readonly DelegateRouteAttestation[];
|
|
70
|
+
stopReason: string;
|
|
71
|
+
turns: number;
|
|
72
|
+
toolCalls: number;
|
|
73
|
+
usage: DelegateUsageReport;
|
|
74
|
+
answerBlocks: readonly string[];
|
|
75
|
+
spilledArtifacts: readonly DelegateSpillReceipt[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function buildDelegateResultPackage(
|
|
79
|
+
input: BuildDelegateResultPackageInput,
|
|
80
|
+
): DelegateResultPackageV1 {
|
|
81
|
+
const blocks: DelegateAnswerBlock[] = input.answerBlocks.map((text, index) => {
|
|
82
|
+
const bytes = assertWellFormedUtf8(text, `delegate answer block ${String(index)}`);
|
|
83
|
+
return {
|
|
84
|
+
kind: 'text',
|
|
85
|
+
byte_length: bytes.length,
|
|
86
|
+
sha256: sha256Bytes(bytes),
|
|
87
|
+
data_base64: bytes.toString('base64'),
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
const aggregate = Buffer.concat(
|
|
91
|
+
blocks.map((block) => Buffer.from(block.data_base64, 'base64')),
|
|
92
|
+
);
|
|
93
|
+
return {
|
|
94
|
+
schema_version: DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION,
|
|
95
|
+
task_id: input.taskId,
|
|
96
|
+
launch_nonce: input.launchNonce,
|
|
97
|
+
seed_sha256: input.seedSha256,
|
|
98
|
+
directive_sha256: input.directiveSha256,
|
|
99
|
+
route: { provider: input.route.provider, model: input.route.model },
|
|
100
|
+
route_attestations: input.routeAttestations,
|
|
101
|
+
stop_reason: input.stopReason,
|
|
102
|
+
turns: input.turns,
|
|
103
|
+
tool_calls: input.toolCalls,
|
|
104
|
+
usage: input.usage,
|
|
105
|
+
answer: {
|
|
106
|
+
encoding: 'utf-8',
|
|
107
|
+
byte_length: aggregate.length,
|
|
108
|
+
sha256: sha256Bytes(aggregate),
|
|
109
|
+
blocks,
|
|
110
|
+
},
|
|
111
|
+
spilled_artifacts: input.spilledArtifacts,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function serializeDelegateResultPackage(pkg: DelegateResultPackageV1): string {
|
|
116
|
+
return `${canonicalJson(pkg)}\n`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface VerifiedDelegateResult {
|
|
120
|
+
package: DelegateResultPackageV1;
|
|
121
|
+
/** Verified answer bytes, taken from the same buffer whose hashes were checked. */
|
|
122
|
+
answer: string;
|
|
123
|
+
answerBytes: Buffer;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface DelegateResultExpectation {
|
|
127
|
+
taskId: string;
|
|
128
|
+
launchNonce: string;
|
|
129
|
+
seedSha256: string;
|
|
130
|
+
route: { provider: string; model: string };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function requireString(record: Record<PropertyKey, unknown>, key: string, taskId: string): string {
|
|
134
|
+
const value = record[key];
|
|
135
|
+
if (typeof value !== 'string')
|
|
136
|
+
fail(`delegate result package field ${key} must be a string`, 'child_result_invalid', taskId);
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function requireInteger(record: Record<PropertyKey, unknown>, key: string, taskId: string): number {
|
|
141
|
+
const value = record[key];
|
|
142
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0)
|
|
143
|
+
fail(
|
|
144
|
+
`delegate result package field ${key} must be a non-negative safe integer`,
|
|
145
|
+
'child_result_invalid',
|
|
146
|
+
taskId,
|
|
147
|
+
);
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const SHA256_PATTERN = /^[0-9a-f]{64}$/;
|
|
152
|
+
|
|
153
|
+
function requireSha256(record: Record<PropertyKey, unknown>, key: string, taskId: string): string {
|
|
154
|
+
const value = requireString(record, key, taskId);
|
|
155
|
+
if (!SHA256_PATTERN.test(value))
|
|
156
|
+
fail(
|
|
157
|
+
`delegate result package field ${key} must be a lowercase SHA-256 digest`,
|
|
158
|
+
'child_result_invalid',
|
|
159
|
+
taskId,
|
|
160
|
+
);
|
|
161
|
+
return value;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function parseUsage(value: unknown, taskId: string): DelegateUsageReport {
|
|
165
|
+
if (!isRecord(value))
|
|
166
|
+
fail('delegate result package usage must be an object', 'child_result_invalid', taskId);
|
|
167
|
+
const status = value['status'];
|
|
168
|
+
if (status === 'unavailable') {
|
|
169
|
+
const reason = value['reason'];
|
|
170
|
+
if (typeof reason !== 'string')
|
|
171
|
+
fail(
|
|
172
|
+
'delegate result package unavailable usage must state a reason',
|
|
173
|
+
'child_result_invalid',
|
|
174
|
+
taskId,
|
|
175
|
+
);
|
|
176
|
+
return { status: 'unavailable', reason };
|
|
177
|
+
}
|
|
178
|
+
if (status !== 'observed')
|
|
179
|
+
fail(
|
|
180
|
+
'delegate result package usage status must be observed or unavailable',
|
|
181
|
+
'child_result_invalid',
|
|
182
|
+
taskId,
|
|
183
|
+
);
|
|
184
|
+
const usage = value['usage'];
|
|
185
|
+
if (!isRecord(usage))
|
|
186
|
+
fail('delegate result package observed usage must be an object', 'child_result_invalid', taskId);
|
|
187
|
+
const cost = usage['cost'];
|
|
188
|
+
if (!isRecord(cost))
|
|
189
|
+
fail(
|
|
190
|
+
'delegate result package observed usage must carry a complete cost breakdown',
|
|
191
|
+
'child_result_invalid',
|
|
192
|
+
taskId,
|
|
193
|
+
);
|
|
194
|
+
const number = (record: Record<PropertyKey, unknown>, key: string): number => {
|
|
195
|
+
const raw = record[key];
|
|
196
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw) || raw < 0)
|
|
197
|
+
fail(
|
|
198
|
+
`delegate result package usage.${key} must be a non-negative finite number`,
|
|
199
|
+
'child_result_invalid',
|
|
200
|
+
taskId,
|
|
201
|
+
);
|
|
202
|
+
return raw;
|
|
203
|
+
};
|
|
204
|
+
return {
|
|
205
|
+
status: 'observed',
|
|
206
|
+
usage: {
|
|
207
|
+
input: number(usage, 'input'),
|
|
208
|
+
output: number(usage, 'output'),
|
|
209
|
+
cacheRead: number(usage, 'cacheRead'),
|
|
210
|
+
cacheWrite: number(usage, 'cacheWrite'),
|
|
211
|
+
totalTokens: number(usage, 'totalTokens'),
|
|
212
|
+
cost: {
|
|
213
|
+
input: number(cost, 'input'),
|
|
214
|
+
output: number(cost, 'output'),
|
|
215
|
+
cacheRead: number(cost, 'cacheRead'),
|
|
216
|
+
cacheWrite: number(cost, 'cacheWrite'),
|
|
217
|
+
total: number(cost, 'total'),
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function parseAttestations(
|
|
224
|
+
value: unknown,
|
|
225
|
+
taskId: string,
|
|
226
|
+
): readonly DelegateRouteAttestation[] {
|
|
227
|
+
if (!Array.isArray(value))
|
|
228
|
+
fail(
|
|
229
|
+
'delegate result package route_attestations must be an array',
|
|
230
|
+
'route_attestation_missing',
|
|
231
|
+
taskId,
|
|
232
|
+
);
|
|
233
|
+
if (value.length === 0)
|
|
234
|
+
fail(
|
|
235
|
+
'delegate result package carries no route attestation, so the route the child actually used cannot be proven',
|
|
236
|
+
'route_attestation_missing',
|
|
237
|
+
taskId,
|
|
238
|
+
);
|
|
239
|
+
return value.map((entry) => {
|
|
240
|
+
if (!isRecord(entry))
|
|
241
|
+
fail('delegate route attestation must be an object', 'route_attestation_missing', taskId);
|
|
242
|
+
return {
|
|
243
|
+
provider: requireString(entry, 'provider', taskId),
|
|
244
|
+
model: requireString(entry, 'model', taskId),
|
|
245
|
+
stop_reason: requireString(entry, 'stop_reason', taskId),
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function parseSpillContentFormat(
|
|
251
|
+
value: unknown,
|
|
252
|
+
taskId: string,
|
|
253
|
+
): DelegateSpillReceipt['content_format'] {
|
|
254
|
+
if (value === undefined) return undefined;
|
|
255
|
+
if (
|
|
256
|
+
value !== 'single_text_utf8' &&
|
|
257
|
+
value !== 'tool_result_content_json_v1' &&
|
|
258
|
+
value !== 'opaque_bytes'
|
|
259
|
+
) {
|
|
260
|
+
fail('delegate spill receipt content_format is invalid', 'child_result_invalid', taskId);
|
|
261
|
+
}
|
|
262
|
+
return value;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function parseSpillReceipts(value: unknown, taskId: string): readonly DelegateSpillReceipt[] {
|
|
266
|
+
if (!Array.isArray(value))
|
|
267
|
+
fail('delegate result package spilled_artifacts must be an array', 'child_result_invalid', taskId);
|
|
268
|
+
return value.map((entry) => {
|
|
269
|
+
if (!isRecord(entry))
|
|
270
|
+
fail('delegate spill receipt must be an object', 'child_result_invalid', taskId);
|
|
271
|
+
if (entry['schema_version'] !== DELEGATE_RECEIPT_SCHEMA_VERSION)
|
|
272
|
+
fail('delegate spill receipt schema_version mismatch', 'child_result_invalid', taskId);
|
|
273
|
+
return {
|
|
274
|
+
schema_version: DELEGATE_RECEIPT_SCHEMA_VERSION,
|
|
275
|
+
artifact: requireString(entry, 'artifact', taskId),
|
|
276
|
+
tool_name: requireString(entry, 'tool_name', taskId),
|
|
277
|
+
tool_call_id: requireString(entry, 'tool_call_id', taskId),
|
|
278
|
+
turn_sequence: requireInteger(entry, 'turn_sequence', taskId),
|
|
279
|
+
source_call_index: requireInteger(entry, 'source_call_index', taskId),
|
|
280
|
+
byte_length: requireInteger(entry, 'byte_length', taskId),
|
|
281
|
+
sha256: requireSha256(entry, 'sha256', taskId),
|
|
282
|
+
content_format: parseSpillContentFormat(entry['content_format'], taskId),
|
|
283
|
+
};
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Parse and fully verify a committed result package.
|
|
289
|
+
*
|
|
290
|
+
* Verification order matters: identity, then route, then per-block hashes, then
|
|
291
|
+
* the aggregate hash. Only after every check passes are answer bytes produced,
|
|
292
|
+
* and they come from the buffer that was hashed.
|
|
293
|
+
*/
|
|
294
|
+
export function verifyDelegateResultPackage(
|
|
295
|
+
raw: string,
|
|
296
|
+
expected: DelegateResultExpectation,
|
|
297
|
+
): VerifiedDelegateResult {
|
|
298
|
+
let parsed: unknown;
|
|
299
|
+
try {
|
|
300
|
+
parsed = JSON.parse(raw);
|
|
301
|
+
} catch (error) {
|
|
302
|
+
fail(
|
|
303
|
+
`delegate result package is not valid JSON: ${error instanceof Error ? error.message : String(error)}`,
|
|
304
|
+
'child_result_invalid',
|
|
305
|
+
expected.taskId,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
if (!isRecord(parsed))
|
|
309
|
+
fail('delegate result package must be a JSON object', 'child_result_invalid', expected.taskId);
|
|
310
|
+
if (parsed['schema_version'] !== DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION)
|
|
311
|
+
fail(
|
|
312
|
+
`delegate result package schema_version must be ${DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION}`,
|
|
313
|
+
'child_result_invalid',
|
|
314
|
+
expected.taskId,
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const taskId = requireString(parsed, 'task_id', expected.taskId);
|
|
318
|
+
const launchNonce = requireString(parsed, 'launch_nonce', expected.taskId);
|
|
319
|
+
if (taskId !== expected.taskId || launchNonce !== expected.launchNonce)
|
|
320
|
+
fail(
|
|
321
|
+
'delegate result package identity does not match the launched task; a stale or foreign package is never accepted',
|
|
322
|
+
'child_result_invalid',
|
|
323
|
+
expected.taskId,
|
|
324
|
+
);
|
|
325
|
+
const seedSha256 = requireSha256(parsed, 'seed_sha256', expected.taskId);
|
|
326
|
+
if (seedSha256 !== expected.seedSha256)
|
|
327
|
+
fail(
|
|
328
|
+
`delegate result package was produced from a different seed: expected ${expected.seedSha256}, package declares ${seedSha256}`,
|
|
329
|
+
'seed_hash_mismatch',
|
|
330
|
+
expected.taskId,
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
const routeRecord = parsed['route'];
|
|
334
|
+
if (!isRecord(routeRecord))
|
|
335
|
+
fail('delegate result package route must be an object', 'child_result_invalid', expected.taskId);
|
|
336
|
+
const provider = requireString(routeRecord, 'provider', expected.taskId);
|
|
337
|
+
const model = requireString(routeRecord, 'model', expected.taskId);
|
|
338
|
+
if (provider !== expected.route.provider || model !== expected.route.model)
|
|
339
|
+
fail(
|
|
340
|
+
`delegate result package route ${provider}/${model} does not match the pinned route ${expected.route.provider}/${expected.route.model}`,
|
|
341
|
+
'route_mismatch',
|
|
342
|
+
expected.taskId,
|
|
343
|
+
);
|
|
344
|
+
const attestations = parseAttestations(parsed['route_attestations'], expected.taskId);
|
|
345
|
+
for (const attestation of attestations) {
|
|
346
|
+
if (attestation.provider !== expected.route.provider || attestation.model !== expected.route.model)
|
|
347
|
+
fail(
|
|
348
|
+
`delegate child produced an assistant message on ${attestation.provider}/${attestation.model}, but the pinned route is ${expected.route.provider}/${expected.route.model}`,
|
|
349
|
+
'route_mismatch',
|
|
350
|
+
expected.taskId,
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const answerRecord = parsed['answer'];
|
|
355
|
+
if (!isRecord(answerRecord))
|
|
356
|
+
fail('delegate result package answer must be an object', 'child_result_invalid', expected.taskId);
|
|
357
|
+
if (answerRecord['encoding'] !== 'utf-8')
|
|
358
|
+
fail(
|
|
359
|
+
'delegate result package answer encoding must be utf-8',
|
|
360
|
+
'child_result_encoding_invalid',
|
|
361
|
+
expected.taskId,
|
|
362
|
+
);
|
|
363
|
+
const declaredLength = requireInteger(answerRecord, 'byte_length', expected.taskId);
|
|
364
|
+
const declaredSha = requireSha256(answerRecord, 'sha256', expected.taskId);
|
|
365
|
+
const blocksValue = answerRecord['blocks'];
|
|
366
|
+
if (!Array.isArray(blocksValue))
|
|
367
|
+
fail('delegate result package answer blocks must be an array', 'child_result_invalid', expected.taskId);
|
|
368
|
+
|
|
369
|
+
const blocks: DelegateAnswerBlock[] = [];
|
|
370
|
+
const buffers: Buffer[] = [];
|
|
371
|
+
for (const [index, entry] of blocksValue.entries()) {
|
|
372
|
+
if (!isRecord(entry))
|
|
373
|
+
fail(
|
|
374
|
+
`delegate answer block ${String(index)} must be an object`,
|
|
375
|
+
'child_result_invalid',
|
|
376
|
+
expected.taskId,
|
|
377
|
+
);
|
|
378
|
+
if (entry['kind'] !== 'text')
|
|
379
|
+
fail(
|
|
380
|
+
`delegate answer block ${String(index)} kind must be text`,
|
|
381
|
+
'child_result_invalid',
|
|
382
|
+
expected.taskId,
|
|
383
|
+
);
|
|
384
|
+
const byteLength = requireInteger(entry, 'byte_length', expected.taskId);
|
|
385
|
+
const sha = requireSha256(entry, 'sha256', expected.taskId);
|
|
386
|
+
const dataBase64 = requireString(entry, 'data_base64', expected.taskId);
|
|
387
|
+
const bytes = Buffer.from(dataBase64, 'base64');
|
|
388
|
+
// Strict base64: a re-encode round trip catches padding and alphabet abuse
|
|
389
|
+
// that Buffer.from would otherwise absorb silently.
|
|
390
|
+
if (bytes.toString('base64') !== dataBase64)
|
|
391
|
+
fail(
|
|
392
|
+
`delegate answer block ${String(index)} is not strict base64`,
|
|
393
|
+
'child_result_invalid',
|
|
394
|
+
expected.taskId,
|
|
395
|
+
);
|
|
396
|
+
if (bytes.length !== byteLength)
|
|
397
|
+
fail(
|
|
398
|
+
`delegate answer block ${String(index)} declares ${String(byteLength)} bytes but carries ${String(bytes.length)}`,
|
|
399
|
+
'answer_hash_mismatch',
|
|
400
|
+
expected.taskId,
|
|
401
|
+
);
|
|
402
|
+
const actual = sha256Bytes(bytes);
|
|
403
|
+
if (actual !== sha)
|
|
404
|
+
fail(
|
|
405
|
+
`delegate answer block ${String(index)} hash mismatch: declared ${sha}, computed ${actual}`,
|
|
406
|
+
'answer_hash_mismatch',
|
|
407
|
+
expected.taskId,
|
|
408
|
+
);
|
|
409
|
+
blocks.push({ kind: 'text', byte_length: byteLength, sha256: sha, data_base64: dataBase64 });
|
|
410
|
+
buffers.push(bytes);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const aggregate = Buffer.concat(buffers);
|
|
414
|
+
if (aggregate.length !== declaredLength)
|
|
415
|
+
fail(
|
|
416
|
+
`delegate answer declares ${String(declaredLength)} bytes but its blocks total ${String(aggregate.length)}`,
|
|
417
|
+
'answer_hash_mismatch',
|
|
418
|
+
expected.taskId,
|
|
419
|
+
);
|
|
420
|
+
const aggregateSha = sha256Bytes(aggregate);
|
|
421
|
+
if (aggregateSha !== declaredSha)
|
|
422
|
+
fail(
|
|
423
|
+
`delegate answer aggregate hash mismatch: declared ${declaredSha}, computed ${aggregateSha}`,
|
|
424
|
+
'answer_hash_mismatch',
|
|
425
|
+
expected.taskId,
|
|
426
|
+
);
|
|
427
|
+
const answer = aggregate.toString('utf8');
|
|
428
|
+
if (!Buffer.from(answer, 'utf8').equals(aggregate))
|
|
429
|
+
fail(
|
|
430
|
+
'delegate answer bytes are not well-formed UTF-8; they are preserved on disk and never returned with substitution characters',
|
|
431
|
+
'child_result_encoding_invalid',
|
|
432
|
+
expected.taskId,
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
return {
|
|
436
|
+
package: {
|
|
437
|
+
schema_version: DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION,
|
|
438
|
+
task_id: taskId,
|
|
439
|
+
launch_nonce: launchNonce,
|
|
440
|
+
seed_sha256: seedSha256,
|
|
441
|
+
directive_sha256: requireSha256(parsed, 'directive_sha256', expected.taskId),
|
|
442
|
+
route: { provider, model },
|
|
443
|
+
route_attestations: attestations,
|
|
444
|
+
stop_reason: requireString(parsed, 'stop_reason', expected.taskId),
|
|
445
|
+
turns: requireInteger(parsed, 'turns', expected.taskId),
|
|
446
|
+
tool_calls: requireInteger(parsed, 'tool_calls', expected.taskId),
|
|
447
|
+
usage: parseUsage(parsed['usage'], expected.taskId),
|
|
448
|
+
answer: {
|
|
449
|
+
encoding: 'utf-8',
|
|
450
|
+
byte_length: declaredLength,
|
|
451
|
+
sha256: declaredSha,
|
|
452
|
+
blocks,
|
|
453
|
+
},
|
|
454
|
+
spilled_artifacts: parseSpillReceipts(parsed['spilled_artifacts'], expected.taskId),
|
|
455
|
+
},
|
|
456
|
+
answer,
|
|
457
|
+
answerBytes: aggregate,
|
|
458
|
+
};
|
|
459
|
+
}
|