pi-background-tasks 2.4.2 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BACKGROUND-TASKS-INSTRUCTIONS.md +1 -1
- package/README.md +70 -25
- package/TESTING.md +42 -2
- package/TEST_PLAN.md +7 -4
- package/dist/extensions/anthropic-attribution-child.js +4 -0
- package/dist/extensions/anthropic-attribution-child.js.map +1 -0
- package/dist/extensions/anthropic-attribution.js +107 -0
- package/dist/extensions/anthropic-attribution.js.map +1 -0
- package/dist/extensions/background-tasks.js +2 -0
- package/dist/extensions/background-tasks.js.map +1 -0
- package/dist/extensions/delegate-child.js +2 -0
- package/dist/extensions/delegate-child.js.map +1 -0
- package/dist/extensions/fusion-child.js +2 -0
- package/dist/extensions/fusion-child.js.map +1 -0
- package/dist/package.json +5 -0
- package/dist/src/core/anthropic-attribution-path.js +24 -0
- package/dist/src/core/anthropic-attribution-path.js.map +1 -0
- package/dist/src/core/anthropic-attribution.js +2247 -0
- package/dist/src/core/anthropic-attribution.js.map +1 -0
- package/dist/src/core/attested-pi-contract.js +5 -0
- package/dist/src/core/attested-pi-contract.js.map +1 -0
- package/dist/src/core/attested-pi-run.js +749 -0
- package/dist/src/core/attested-pi-run.js.map +1 -0
- package/dist/src/core/canonical-json.js +19 -0
- package/dist/src/core/canonical-json.js.map +1 -0
- package/dist/src/core/common.js +787 -0
- package/dist/src/core/common.js.map +1 -0
- package/dist/src/core/config.js +78 -0
- package/dist/src/core/config.js.map +1 -0
- package/dist/src/core/context/parent-snapshot.js +75 -0
- package/dist/src/core/context/parent-snapshot.js.map +1 -0
- package/dist/src/core/context/token-budget.js +631 -0
- package/dist/src/core/context/token-budget.js.map +1 -0
- package/dist/src/core/context/visible-conversation-v2.js +390 -0
- package/dist/src/core/context/visible-conversation-v2.js.map +1 -0
- package/dist/src/core/delegate/artifacts.js +357 -0
- package/dist/src/core/delegate/artifacts.js.map +1 -0
- package/dist/src/core/delegate/budget.js +266 -0
- package/dist/src/core/delegate/budget.js.map +1 -0
- package/dist/src/core/delegate/facade-contract.js +7 -0
- package/dist/src/core/delegate/facade-contract.js.map +1 -0
- package/dist/src/core/delegate/hook-contract-evidence.json +18 -0
- package/dist/src/core/delegate/hook-contract.js +124 -0
- package/dist/src/core/delegate/hook-contract.js.map +1 -0
- package/dist/src/core/delegate/launch.js +366 -0
- package/dist/src/core/delegate/launch.js.map +1 -0
- package/dist/src/core/delegate/result-package.js +293 -0
- package/dist/src/core/delegate/result-package.js.map +1 -0
- package/dist/src/core/delegate/runner.js +398 -0
- package/dist/src/core/delegate/runner.js.map +1 -0
- package/dist/src/core/delegate/seed.js +334 -0
- package/dist/src/core/delegate/seed.js.map +1 -0
- package/dist/src/core/delegate/types.js +103 -0
- package/dist/src/core/delegate/types.js.map +1 -0
- package/dist/src/core/durable-fs.js +372 -0
- package/dist/src/core/durable-fs.js.map +1 -0
- package/dist/src/core/extension-api.js +382 -0
- package/dist/src/core/extension-api.js.map +1 -0
- package/dist/src/core/fusion/artifacts.js +654 -0
- package/dist/src/core/fusion/artifacts.js.map +1 -0
- package/dist/src/core/fusion/budget.js +949 -0
- package/dist/src/core/fusion/budget.js.map +1 -0
- package/dist/src/core/fusion/child-protocol.js +176 -0
- package/dist/src/core/fusion/child-protocol.js.map +1 -0
- package/dist/src/core/fusion/claude-cache.js +156 -0
- package/dist/src/core/fusion/claude-cache.js.map +1 -0
- package/dist/src/core/fusion/clean-context.js +64 -0
- package/dist/src/core/fusion/clean-context.js.map +1 -0
- package/dist/src/core/fusion/config.js +349 -0
- package/dist/src/core/fusion/config.js.map +1 -0
- package/dist/src/core/fusion/context.js +184 -0
- package/dist/src/core/fusion/context.js.map +1 -0
- package/dist/src/core/fusion/evaluation.js +696 -0
- package/dist/src/core/fusion/evaluation.js.map +1 -0
- package/dist/src/core/fusion/facade-contract.js +3 -0
- package/dist/src/core/fusion/facade-contract.js.map +1 -0
- package/dist/src/core/fusion/orchestrator.js +867 -0
- package/dist/src/core/fusion/orchestrator.js.map +1 -0
- package/dist/src/core/fusion/output-contract.js +27 -0
- package/dist/src/core/fusion/output-contract.js.map +1 -0
- package/dist/src/core/fusion/pi-child.js +1741 -0
- package/dist/src/core/fusion/pi-child.js.map +1 -0
- package/dist/src/core/fusion/prompts.js +260 -0
- package/dist/src/core/fusion/prompts.js.map +1 -0
- package/dist/src/core/fusion/result-package.js +857 -0
- package/dist/src/core/fusion/result-package.js.map +1 -0
- package/dist/src/core/fusion/source-policy.js +259 -0
- package/dist/src/core/fusion/source-policy.js.map +1 -0
- package/dist/src/core/fusion/types.js +207 -0
- package/dist/src/core/fusion/types.js.map +1 -0
- package/dist/src/core/fusion/web-fetch.js +777 -0
- package/dist/src/core/fusion/web-fetch.js.map +1 -0
- package/dist/src/core/fusion/workflows.js +126 -0
- package/dist/src/core/fusion/workflows.js.map +1 -0
- package/dist/src/core/lazy-module.js +181 -0
- package/dist/src/core/lazy-module.js.map +1 -0
- package/dist/src/core/pi-launch.js +431 -0
- package/dist/src/core/pi-launch.js.map +1 -0
- package/dist/src/core/registry.js +3331 -0
- package/dist/src/core/registry.js.map +1 -0
- package/dist/src/core/reload-shell-owner.js +1396 -0
- package/dist/src/core/reload-shell-owner.js.map +1 -0
- package/dist/src/core/shell-policy.js +80 -0
- package/dist/src/core/shell-policy.js.map +1 -0
- package/dist/src/core/task-durable.js +51 -0
- package/dist/src/core/task-durable.js.map +1 -0
- package/dist/src/core/update-check.js +92 -0
- package/dist/src/core/update-check.js.map +1 -0
- package/dist/src/core/windows-taskkill.js +185 -0
- package/dist/src/core/windows-taskkill.js.map +1 -0
- package/dist/src/delegate-child-extension.js +776 -0
- package/dist/src/delegate-child-extension.js.map +1 -0
- package/dist/src/delegate-extension.js +746 -0
- package/dist/src/delegate-extension.js.map +1 -0
- package/dist/src/extension.js +953 -0
- package/dist/src/extension.js.map +1 -0
- package/dist/src/fusion-child-extension.js +760 -0
- package/dist/src/fusion-child-extension.js.map +1 -0
- package/dist/src/fusion-extension.js +1030 -0
- package/dist/src/fusion-extension.js.map +1 -0
- package/dist/src/ui/background-tasks-manager.js +689 -0
- package/dist/src/ui/background-tasks-manager.js.map +1 -0
- package/dist/src/ui/fusion-model-selector.js +277 -0
- package/dist/src/ui/fusion-model-selector.js.map +1 -0
- package/docs/INDEX.md +35 -33
- package/docs/api/eventbus-v1.md +21 -5
- package/docs/choose-a-workflow.md +2 -0
- package/docs/commands/bg-clear.md +3 -3
- package/docs/commands/bg-update.md +3 -3
- package/docs/commands/bg.md +18 -6
- package/docs/commands/claude-cache.md +6 -6
- package/docs/commands/fusion-models.md +49 -4
- package/docs/commands/fusion.md +3 -3
- package/docs/commands/jobs.md +3 -3
- package/docs/commands/kill.md +3 -3
- package/docs/commands/logs.md +3 -3
- package/docs/commands/task-manager.md +5 -5
- package/docs/concepts/completion-delivery.md +6 -2
- package/docs/getting-started.md +7 -1
- package/docs/manifest.json +228 -42
- package/docs/operations/configuration.md +58 -5
- package/docs/operations/releasing.md +4 -2
- package/docs/operations/testing.md +22 -0
- package/docs/operations/troubleshooting.md +6 -2
- package/docs/read-before-edit.md +44 -31
- package/docs/reference/runtime-contracts.md +87 -51
- package/docs/reference/shortcuts-and-dock.md +28 -14
- package/docs/subsystems/anthropic-attribution.md +35 -13
- package/docs/subsystems/attested-pi-runs.md +12 -7
- package/docs/subsystems/background-task-runtime.md +53 -11
- package/docs/subsystems/child-launch-durability-and-safety.md +28 -9
- package/docs/subsystems/delegation.md +16 -3
- package/docs/subsystems/docs-freshness-gate.md +14 -7
- package/docs/subsystems/fusion.md +36 -11
- package/docs/subsystems/host-ui-and-telemetry.md +22 -9
- package/docs/tools/bg_delegate.md +3 -1
- package/docs/tools/bg_kill.md +3 -1
- package/docs/tools/bg_logs.md +3 -1
- package/docs/tools/bg_result.md +5 -3
- package/docs/tools/bg_run.md +31 -5
- package/docs/tools/bg_run_pi_attested.md +3 -1
- package/docs/tools/bg_status.md +3 -1
- package/docs/tools/fusion_investigate.md +3 -1
- package/docs/tools/fusion_reason.md +3 -1
- package/docs/tools/fusion_research.md +3 -1
- package/docs/tools/fusion_validate.md +3 -1
- package/extensions/anthropic-attribution-child.ts +3 -0
- package/extensions/anthropic-attribution.ts +166 -1
- package/package.json +12 -10
- package/src/core/anthropic-attribution-path.ts +5 -4
- package/src/core/anthropic-attribution.ts +1505 -237
- package/src/core/attested-pi-contract.ts +4 -0
- package/src/core/attested-pi-run.ts +468 -81
- package/src/core/canonical-json.ts +21 -0
- package/src/core/common.ts +450 -40
- package/src/core/config.ts +121 -0
- package/src/core/context/visible-conversation-v2.ts +3 -6
- package/src/core/delegate/artifacts.ts +13 -9
- package/src/core/delegate/budget.ts +13 -6
- package/src/core/delegate/facade-contract.ts +6 -0
- package/src/core/delegate/result-package.ts +32 -14
- package/src/core/delegate/runner.ts +129 -58
- package/src/core/delegate/seed.ts +5 -7
- package/src/core/durable-fs.ts +178 -30
- package/src/core/extension-api.ts +36 -8
- package/src/core/fusion/artifacts.ts +17 -16
- package/src/core/fusion/clean-context.ts +8 -5
- package/src/core/fusion/config.ts +2 -1
- package/src/core/fusion/context.ts +2 -6
- package/src/core/fusion/facade-contract.ts +2 -0
- package/src/core/fusion/orchestrator.ts +3 -3
- package/src/core/fusion/prompts.ts +1 -1
- package/src/core/fusion/result-package.ts +259 -72
- package/src/core/fusion/source-policy.ts +43 -17
- package/src/core/lazy-module.ts +215 -0
- package/src/core/pi-launch.ts +390 -65
- package/src/core/registry.ts +2010 -381
- package/src/core/reload-shell-owner.ts +1662 -0
- package/src/core/shell-policy.ts +134 -0
- package/src/core/task-durable.ts +67 -0
- package/src/delegate-extension.ts +404 -144
- package/src/extension.ts +387 -114
- package/src/fusion-extension.ts +202 -96
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { canonicalJson } from '../canonical-json.js';
|
|
3
|
+
import { UnsupportedConversationBlockError, projectVisibleConversationV2, } from '../context/visible-conversation-v2.js';
|
|
4
|
+
import { snapshotParentConversation, } from '../context/parent-snapshot.js';
|
|
5
|
+
import { DELEGATE_BRANCH_FILTER_ID, DELEGATE_CONTEXT_POLICY_ID, DELEGATE_LEDGER_SCHEMA_VERSION, DELEGATE_SEED_SCHEMA_VERSION, DELEGATE_TOOL_NAME, DelegateError, } from './types.js';
|
|
6
|
+
function sha256Text(value) {
|
|
7
|
+
return createHash('sha256').update(Buffer.from(value, 'utf8')).digest('hex');
|
|
8
|
+
}
|
|
9
|
+
function policyDescriptor() {
|
|
10
|
+
return {
|
|
11
|
+
id: DELEGATE_CONTEXT_POLICY_ID,
|
|
12
|
+
transform: 'visible-conversation-ledger-v2',
|
|
13
|
+
version: 1,
|
|
14
|
+
receipt_format: 'omitted_activity.v2',
|
|
15
|
+
user_text: 'verbatim',
|
|
16
|
+
assistant_text: 'verbatim',
|
|
17
|
+
assistant_thinking: 'ledger_only',
|
|
18
|
+
tool_call_arguments: 'ledger_only',
|
|
19
|
+
tool_results: 'ledger_only',
|
|
20
|
+
tool_payload_preview_bytes: 0,
|
|
21
|
+
images: 'marker_or_ledger_only',
|
|
22
|
+
unknown_block_behavior: 'error',
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Seal the shared transform under delegate's own envelope.
|
|
27
|
+
*
|
|
28
|
+
* Delegate never imports Fusion's canonical-input builder and never emits
|
|
29
|
+
* `fusion-input.v4`. The ledger root commits only to ledger rows, so the two
|
|
30
|
+
* consumers can seal identical bodies without either affecting the other.
|
|
31
|
+
*/
|
|
32
|
+
function sealDelegateProjection(projected, branchFilter) {
|
|
33
|
+
return {
|
|
34
|
+
projection: {
|
|
35
|
+
policy: policyDescriptor(),
|
|
36
|
+
branch_filter: branchFilter,
|
|
37
|
+
entries: projected.entries,
|
|
38
|
+
accounting: projected.accounting,
|
|
39
|
+
},
|
|
40
|
+
ledger: {
|
|
41
|
+
schema_version: DELEGATE_LEDGER_SCHEMA_VERSION,
|
|
42
|
+
policy_id: DELEGATE_CONTEXT_POLICY_ID,
|
|
43
|
+
transform: 'visible-conversation-ledger-v2',
|
|
44
|
+
entries: projected.ledger.entries,
|
|
45
|
+
projection_map: projected.ledger.projection_map,
|
|
46
|
+
root_sha256: projected.ledger.root_sha256,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Build the frozen delegate seed.
|
|
52
|
+
*
|
|
53
|
+
* The projection excludes the assistant message carrying the in-flight
|
|
54
|
+
* `bg_delegate` call. That message is excluded as a whole, so when several
|
|
55
|
+
* `bg_delegate` calls share one assistant message every sibling call is excluded
|
|
56
|
+
* for every child: two delegates launched together receive identical projected
|
|
57
|
+
* history, and neither can observe the other's arguments.
|
|
58
|
+
*
|
|
59
|
+
* The directive is authoritative; projected history is supporting, untrusted
|
|
60
|
+
* context. That relationship is stated to the child explicitly in its system
|
|
61
|
+
* prompt, and recorded structurally here as `authority: 'explicit_text'`.
|
|
62
|
+
*/
|
|
63
|
+
export function buildDelegateSeed(ctx, options) {
|
|
64
|
+
const directiveText = options.directive;
|
|
65
|
+
if (directiveText.trim().length === 0) {
|
|
66
|
+
throw new DelegateError('bg_delegate prompt must not be blank', {
|
|
67
|
+
code: 'invalid_arguments',
|
|
68
|
+
childCreated: false,
|
|
69
|
+
remediation: ['Provide a non-blank prompt describing what the delegate should investigate.'],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const snapshotOptions = {
|
|
73
|
+
toolName: DELEGATE_TOOL_NAME,
|
|
74
|
+
excludeActiveToolCallLeaf: true,
|
|
75
|
+
};
|
|
76
|
+
if (options.toolCallId !== undefined)
|
|
77
|
+
snapshotOptions.toolCallId = options.toolCallId;
|
|
78
|
+
let snapshot;
|
|
79
|
+
let projected;
|
|
80
|
+
try {
|
|
81
|
+
snapshot = snapshotParentConversation(ctx, snapshotOptions);
|
|
82
|
+
projected = projectVisibleConversationV2(snapshot.messages);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error instanceof UnsupportedConversationBlockError) {
|
|
86
|
+
throw new DelegateError(`bg_delegate could not project the parent conversation: ${error.message}`, {
|
|
87
|
+
code: 'seed_projection_failed',
|
|
88
|
+
childCreated: false,
|
|
89
|
+
remediation: [
|
|
90
|
+
'The parent conversation contains a block type this package version does not know how to project.',
|
|
91
|
+
'Nothing was dropped or guessed and no child was created. Report the block type so the transform can be versioned.',
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
const branchFilter = {
|
|
98
|
+
id: DELEGATE_BRANCH_FILTER_ID,
|
|
99
|
+
tool_name: DELEGATE_TOOL_NAME,
|
|
100
|
+
tool_call_id: options.toolCallId ?? null,
|
|
101
|
+
active_tool_call_leaf_excluded: snapshot.activeToolCallLeafExcluded,
|
|
102
|
+
};
|
|
103
|
+
const sealed = sealDelegateProjection(projected, branchFilter);
|
|
104
|
+
const directive = {
|
|
105
|
+
text: directiveText,
|
|
106
|
+
sha256: sha256Text(directiveText),
|
|
107
|
+
authority: 'explicit_text',
|
|
108
|
+
};
|
|
109
|
+
const seed = {
|
|
110
|
+
schema_version: DELEGATE_SEED_SCHEMA_VERSION,
|
|
111
|
+
task_id: options.taskId,
|
|
112
|
+
launch_nonce: options.launchNonce,
|
|
113
|
+
cwd: ctx.cwd,
|
|
114
|
+
capability: options.capability,
|
|
115
|
+
extension_mode: options.extensionMode,
|
|
116
|
+
route: options.route,
|
|
117
|
+
parent_system_prompt: ctx.getSystemPrompt(),
|
|
118
|
+
parent_leaf_id: snapshot.leafId,
|
|
119
|
+
directive,
|
|
120
|
+
conversation_projection: sealed.projection,
|
|
121
|
+
limits: options.limits,
|
|
122
|
+
};
|
|
123
|
+
const serialized = canonicalJson(seed);
|
|
124
|
+
return { seed, serialized, sha256: sha256Text(serialized), ledger: sealed.ledger };
|
|
125
|
+
}
|
|
126
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
127
|
+
/**
|
|
128
|
+
* Receive-side seed validation performed inside the child before its first model
|
|
129
|
+
* call. A seed that does not match its declared length, hash, schema, task id,
|
|
130
|
+
* and launch nonce is a loud refusal, never a best-effort continue.
|
|
131
|
+
*/
|
|
132
|
+
export function verifyDelegateSeedBytes(raw, expected) {
|
|
133
|
+
const actual = sha256Text(raw);
|
|
134
|
+
if (actual !== expected.sha256) {
|
|
135
|
+
throw new DelegateError(`delegate seed hash mismatch: expected ${expected.sha256}, received ${actual}`, {
|
|
136
|
+
code: 'seed_hash_mismatch',
|
|
137
|
+
childCreated: true,
|
|
138
|
+
taskId: expected.taskId,
|
|
139
|
+
remediation: ['The seed bytes delivered to the child differ from the persisted seed.'],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
let parsed;
|
|
143
|
+
try {
|
|
144
|
+
parsed = JSON.parse(raw);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
throw new DelegateError(`delegate seed is not valid JSON: ${error instanceof Error ? error.message : String(error)}`, { code: 'seed_hash_mismatch', childCreated: true, taskId: expected.taskId });
|
|
148
|
+
}
|
|
149
|
+
if (!isRecord(parsed)) {
|
|
150
|
+
throw new DelegateError('delegate seed must be a JSON object', {
|
|
151
|
+
code: 'seed_hash_mismatch',
|
|
152
|
+
childCreated: true,
|
|
153
|
+
taskId: expected.taskId,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
if (parsed['schema_version'] !== DELEGATE_SEED_SCHEMA_VERSION) {
|
|
157
|
+
throw new DelegateError(`delegate seed schema_version must be ${DELEGATE_SEED_SCHEMA_VERSION}`, { code: 'seed_hash_mismatch', childCreated: true, taskId: expected.taskId });
|
|
158
|
+
}
|
|
159
|
+
if (parsed['task_id'] !== expected.taskId || parsed['launch_nonce'] !== expected.launchNonce) {
|
|
160
|
+
throw new DelegateError('delegate seed task identity does not match this child', {
|
|
161
|
+
code: 'seed_hash_mismatch',
|
|
162
|
+
childCreated: true,
|
|
163
|
+
taskId: expected.taskId,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
// The hash pins the exact bytes; this rebuild pins the shape the child is
|
|
167
|
+
// allowed to act on. Every field the child reads is validated and copied
|
|
168
|
+
// explicitly, so a structurally malformed seed cannot reach the agent loop by
|
|
169
|
+
// way of an unchecked assertion.
|
|
170
|
+
return rebuildSeed(parsed, expected.taskId);
|
|
171
|
+
}
|
|
172
|
+
function requireString(record, key, taskId) {
|
|
173
|
+
const value = record[key];
|
|
174
|
+
if (typeof value !== 'string') {
|
|
175
|
+
throw new DelegateError(`delegate seed field ${key} must be a string`, {
|
|
176
|
+
code: 'seed_hash_mismatch',
|
|
177
|
+
childCreated: true,
|
|
178
|
+
taskId,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
function requireRecord(record, key, taskId) {
|
|
184
|
+
const value = record[key];
|
|
185
|
+
if (!isRecord(value)) {
|
|
186
|
+
throw new DelegateError(`delegate seed field ${key} must be an object`, {
|
|
187
|
+
code: 'seed_hash_mismatch',
|
|
188
|
+
childCreated: true,
|
|
189
|
+
taskId,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
194
|
+
function requireNonNegativeInteger(record, key, taskId) {
|
|
195
|
+
const value = record[key];
|
|
196
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) {
|
|
197
|
+
throw new DelegateError(`delegate seed field ${key} must be a non-negative safe integer`, {
|
|
198
|
+
code: 'seed_hash_mismatch',
|
|
199
|
+
childCreated: true,
|
|
200
|
+
taskId,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
/** Explicit, field-by-field reconstruction of the seed fields the child consumes. */
|
|
206
|
+
function rebuildSeed(record, taskId) {
|
|
207
|
+
const capability = requireString(record, 'capability', taskId);
|
|
208
|
+
if (capability !== 'inspect') {
|
|
209
|
+
throw new DelegateError(`delegate seed capability ${capability} is not supported`, {
|
|
210
|
+
code: 'delegate_isolation_unsupported',
|
|
211
|
+
childCreated: true,
|
|
212
|
+
taskId,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
const extensionMode = requireString(record, 'extension_mode', taskId);
|
|
216
|
+
if (extensionMode !== 'isolated' && extensionMode !== 'ambient') {
|
|
217
|
+
throw new DelegateError(`delegate seed extension mode ${extensionMode} is not recognised`, {
|
|
218
|
+
code: 'seed_hash_mismatch',
|
|
219
|
+
childCreated: true,
|
|
220
|
+
taskId,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
const routeRecord = requireRecord(record, 'route', taskId);
|
|
224
|
+
const routeOrigin = requireString(routeRecord, 'origin', taskId);
|
|
225
|
+
if (routeOrigin !== 'parent_current' && routeOrigin !== 'explicit') {
|
|
226
|
+
throw new DelegateError(`delegate seed route origin ${routeOrigin} is not recognised`, {
|
|
227
|
+
code: 'seed_hash_mismatch',
|
|
228
|
+
childCreated: true,
|
|
229
|
+
taskId,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
const route = {
|
|
233
|
+
provider: requireString(routeRecord, 'provider', taskId),
|
|
234
|
+
model: requireString(routeRecord, 'model', taskId),
|
|
235
|
+
qualified_id: requireString(routeRecord, 'qualified_id', taskId),
|
|
236
|
+
context_window_tokens: requireNonNegativeInteger(routeRecord, 'context_window_tokens', taskId),
|
|
237
|
+
thinking_level: requireString(routeRecord, 'thinking_level', taskId),
|
|
238
|
+
origin: routeOrigin,
|
|
239
|
+
};
|
|
240
|
+
const limitsRecord = requireRecord(record, 'limits', taskId);
|
|
241
|
+
const limits = {
|
|
242
|
+
max_turns: requireNonNegativeInteger(limitsRecord, 'max_turns', taskId),
|
|
243
|
+
max_tool_calls: requireNonNegativeInteger(limitsRecord, 'max_tool_calls', taskId),
|
|
244
|
+
timeout_seconds: requireNonNegativeInteger(limitsRecord, 'timeout_seconds', taskId),
|
|
245
|
+
max_tool_result_bytes: requireNonNegativeInteger(limitsRecord, 'max_tool_result_bytes', taskId),
|
|
246
|
+
max_total_tool_output_bytes: requireNonNegativeInteger(limitsRecord, 'max_total_tool_output_bytes', taskId),
|
|
247
|
+
max_answer_bytes: requireNonNegativeInteger(limitsRecord, 'max_answer_bytes', taskId),
|
|
248
|
+
allowed_input_tokens: requireNonNegativeInteger(limitsRecord, 'allowed_input_tokens', taskId),
|
|
249
|
+
};
|
|
250
|
+
const directiveRecord = requireRecord(record, 'directive', taskId);
|
|
251
|
+
const authority = requireString(directiveRecord, 'authority', taskId);
|
|
252
|
+
if (authority !== 'explicit_text') {
|
|
253
|
+
throw new DelegateError(`delegate seed directive authority ${authority} is not recognised`, {
|
|
254
|
+
code: 'seed_hash_mismatch',
|
|
255
|
+
childCreated: true,
|
|
256
|
+
taskId,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
const directive = {
|
|
260
|
+
text: requireString(directiveRecord, 'text', taskId),
|
|
261
|
+
sha256: requireString(directiveRecord, 'sha256', taskId),
|
|
262
|
+
authority,
|
|
263
|
+
};
|
|
264
|
+
if (sha256Text(directive.text) !== directive.sha256) {
|
|
265
|
+
throw new DelegateError('delegate seed directive hash does not match its text', {
|
|
266
|
+
code: 'seed_hash_mismatch',
|
|
267
|
+
childCreated: true,
|
|
268
|
+
taskId,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
const parentLeafId = record['parent_leaf_id'];
|
|
272
|
+
if (parentLeafId !== null && typeof parentLeafId !== 'string') {
|
|
273
|
+
throw new DelegateError('delegate seed parent_leaf_id must be a string or null', {
|
|
274
|
+
code: 'seed_hash_mismatch',
|
|
275
|
+
childCreated: true,
|
|
276
|
+
taskId,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
const projection = requireRecord(record, 'conversation_projection', taskId);
|
|
280
|
+
const projectionPolicy = requireRecord(projection, 'policy', taskId);
|
|
281
|
+
if (requireString(projectionPolicy, 'id', taskId) !== DELEGATE_CONTEXT_POLICY_ID) {
|
|
282
|
+
throw new DelegateError('delegate seed projection policy id is not the delegate policy', {
|
|
283
|
+
code: 'seed_hash_mismatch',
|
|
284
|
+
childCreated: true,
|
|
285
|
+
taskId,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
if (!Array.isArray(projection['entries'])) {
|
|
289
|
+
throw new DelegateError('delegate seed projection entries must be an array', {
|
|
290
|
+
code: 'seed_hash_mismatch',
|
|
291
|
+
childCreated: true,
|
|
292
|
+
taskId,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
schema_version: DELEGATE_SEED_SCHEMA_VERSION,
|
|
297
|
+
task_id: requireString(record, 'task_id', taskId),
|
|
298
|
+
launch_nonce: requireString(record, 'launch_nonce', taskId),
|
|
299
|
+
cwd: requireString(record, 'cwd', taskId),
|
|
300
|
+
capability,
|
|
301
|
+
extension_mode: extensionMode,
|
|
302
|
+
route,
|
|
303
|
+
parent_system_prompt: requireString(record, 'parent_system_prompt', taskId),
|
|
304
|
+
parent_leaf_id: parentLeafId,
|
|
305
|
+
directive,
|
|
306
|
+
conversation_projection: readDelegateProjection(projection),
|
|
307
|
+
limits,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* The projection body is already hash-pinned by the seed digest verified above,
|
|
312
|
+
* so it is carried through without re-deriving values that would only duplicate
|
|
313
|
+
* the parent's computation.
|
|
314
|
+
*/
|
|
315
|
+
function readDelegateProjection(record) {
|
|
316
|
+
const projection = record;
|
|
317
|
+
if (!isDelegateProjection(projection)) {
|
|
318
|
+
throw new DelegateError('delegate seed conversation_projection is malformed', {
|
|
319
|
+
code: 'seed_hash_mismatch',
|
|
320
|
+
childCreated: true,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
return projection;
|
|
324
|
+
}
|
|
325
|
+
function isDelegateProjection(value) {
|
|
326
|
+
if (!isRecord(value))
|
|
327
|
+
return false;
|
|
328
|
+
if (!isRecord(value['policy']) || !isRecord(value['branch_filter']))
|
|
329
|
+
return false;
|
|
330
|
+
if (!isRecord(value['accounting']))
|
|
331
|
+
return false;
|
|
332
|
+
return Array.isArray(value['entries']);
|
|
333
|
+
}
|
|
334
|
+
//# sourceMappingURL=seed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed.js","sourceRoot":"","sources":["../../../../src/core/delegate/seed.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EACL,iCAAiC,EACjC,4BAA4B,GAE7B,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EACL,0BAA0B,GAG3B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,yBAAyB,EACzB,0BAA0B,EAC1B,8BAA8B,EAC9B,4BAA4B,EAC5B,kBAAkB,EAClB,aAAa,GAWd,MAAM,YAAY,CAAC;AAqBpB,SAAS,UAAU,CAAC,KAAa;IAC/B,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC/E,CAAC;AAED,SAAS,gBAAgB;IACvB,OAAO;QACL,EAAE,EAAE,0BAA0B;QAC9B,SAAS,EAAE,gCAAgC;QAC3C,OAAO,EAAE,CAAC;QACV,cAAc,EAAE,qBAAqB;QACrC,SAAS,EAAE,UAAU;QACrB,cAAc,EAAE,UAAU;QAC1B,kBAAkB,EAAE,aAAa;QACjC,mBAAmB,EAAE,aAAa;QAClC,YAAY,EAAE,aAAa;QAC3B,0BAA0B,EAAE,CAAC;QAC7B,MAAM,EAAE,uBAAuB;QAC/B,sBAAsB,EAAE,OAAO;KAChC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,sBAAsB,C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oDAAoD,EAAE;YAC5E,IAAI,EAAE,oBAAoB;YAC1B,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;IACL,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAc;IAC1C,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACnC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IAClF,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IACjD,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;AACzC,CAAC","sourcesContent":["import { createHash } from 'node:crypto';\nimport { canonicalJson } from '../canonical-json.js';\nimport {\n UnsupportedConversationBlockError,\n projectVisibleConversationV2,\n type ProjectedConversationV2,\n} from '../context/visible-conversation-v2.js';\nimport {\n snapshotParentConversation,\n type ParentContextSource,\n type ParentSnapshotOptions,\n} from '../context/parent-snapshot.js';\nimport {\n DELEGATE_BRANCH_FILTER_ID,\n DELEGATE_CONTEXT_POLICY_ID,\n DELEGATE_LEDGER_SCHEMA_VERSION,\n DELEGATE_SEED_SCHEMA_VERSION,\n DELEGATE_TOOL_NAME,\n DelegateError,\n type DelegateBranchFilterDescriptor,\n type DelegateCapability,\n type DelegateContextPolicyDescriptor,\n type DelegateConversationProjection,\n type DelegateLedgerV1,\n type DelegateExtensionMode,\n type DelegateLimits,\n type DelegatePinnedRoute,\n type DelegateSeedV1,\n type DelegateTaskDirective,\n} from './types.js';\n\nexport interface BuildDelegateSeedOptions {\n taskId: string;\n launchNonce: string;\n toolCallId: string | undefined;\n directive: string;\n capability: DelegateCapability;\n extensionMode: DelegateExtensionMode;\n route: DelegatePinnedRoute;\n limits: DelegateLimits;\n}\n\nexport interface BuiltDelegateSeed {\n seed: DelegateSeedV1;\n /** Exact bytes persisted and written to the child. Nothing re-serializes them. */\n serialized: string;\n sha256: string;\n ledger: DelegateLedgerV1;\n}\n\nfunction sha256Text(value: string): string {\n return createHash('sha256').update(Buffer.from(value, 'utf8')).digest('hex');\n}\n\nfunction policyDescriptor(): DelegateContextPolicyDescriptor {\n return {\n id: DELEGATE_CONTEXT_POLICY_ID,\n transform: 'visible-conversation-ledger-v2',\n version: 1,\n receipt_format: 'omitted_activity.v2',\n user_text: 'verbatim',\n assistant_text: 'verbatim',\n assistant_thinking: 'ledger_only',\n tool_call_arguments: 'ledger_only',\n tool_results: 'ledger_only',\n tool_payload_preview_bytes: 0,\n images: 'marker_or_ledger_only',\n unknown_block_behavior: 'error',\n };\n}\n\n/**\n * Seal the shared transform under delegate's own envelope.\n *\n * Delegate never imports Fusion's canonical-input builder and never emits\n * `fusion-input.v4`. The ledger root commits only to ledger rows, so the two\n * consumers can seal identical bodies without either affecting the other.\n */\nfunction sealDelegateProjection(\n projected: ProjectedConversationV2,\n branchFilter: DelegateBranchFilterDescriptor,\n): { projection: DelegateConversationProjection; ledger: DelegateLedgerV1 } {\n return {\n projection: {\n policy: policyDescriptor(),\n branch_filter: branchFilter,\n entries: projected.entries,\n accounting: projected.accounting,\n },\n ledger: {\n schema_version: DELEGATE_LEDGER_SCHEMA_VERSION,\n policy_id: DELEGATE_CONTEXT_POLICY_ID,\n transform: 'visible-conversation-ledger-v2',\n entries: projected.ledger.entries,\n projection_map: projected.ledger.projection_map,\n root_sha256: projected.ledger.root_sha256,\n },\n };\n}\n\n/**\n * Build the frozen delegate seed.\n *\n * The projection excludes the assistant message carrying the in-flight\n * `bg_delegate` call. That message is excluded as a whole, so when several\n * `bg_delegate` calls share one assistant message every sibling call is excluded\n * for every child: two delegates launched together receive identical projected\n * history, and neither can observe the other's arguments.\n *\n * The directive is authoritative; projected history is supporting, untrusted\n * context. That relationship is stated to the child explicitly in its system\n * prompt, and recorded structurally here as `authority: 'explicit_text'`.\n */\nexport function buildDelegateSeed(\n ctx: ParentContextSource,\n options: BuildDelegateSeedOptions,\n): BuiltDelegateSeed {\n const directiveText = options.directive;\n if (directiveText.trim().length === 0) {\n throw new DelegateError('bg_delegate prompt must not be blank', {\n code: 'invalid_arguments',\n childCreated: false,\n remediation: ['Provide a non-blank prompt describing what the delegate should investigate.'],\n });\n }\n const snapshotOptions: ParentSnapshotOptions = {\n toolName: DELEGATE_TOOL_NAME,\n excludeActiveToolCallLeaf: true,\n };\n if (options.toolCallId !== undefined) snapshotOptions.toolCallId = options.toolCallId;\n\n let snapshot: ReturnType<typeof snapshotParentConversation>;\n let projected: ProjectedConversationV2;\n try {\n snapshot = snapshotParentConversation(ctx, snapshotOptions);\n projected = projectVisibleConversationV2(snapshot.messages);\n } catch (error) {\n if (error instanceof UnsupportedConversationBlockError) {\n throw new DelegateError(\n `bg_delegate could not project the parent conversation: ${error.message}`,\n {\n code: 'seed_projection_failed',\n childCreated: false,\n remediation: [\n 'The parent conversation contains a block type this package version does not know how to project.',\n 'Nothing was dropped or guessed and no child was created. Report the block type so the transform can be versioned.',\n ],\n },\n );\n }\n throw error;\n }\n\n const branchFilter: DelegateBranchFilterDescriptor = {\n id: DELEGATE_BRANCH_FILTER_ID,\n tool_name: DELEGATE_TOOL_NAME,\n tool_call_id: options.toolCallId ?? null,\n active_tool_call_leaf_excluded: snapshot.activeToolCallLeafExcluded,\n };\n const sealed = sealDelegateProjection(projected, branchFilter);\n const directive: DelegateTaskDirective = {\n text: directiveText,\n sha256: sha256Text(directiveText),\n authority: 'explicit_text',\n };\n const seed: DelegateSeedV1 = {\n schema_version: DELEGATE_SEED_SCHEMA_VERSION,\n task_id: options.taskId,\n launch_nonce: options.launchNonce,\n cwd: ctx.cwd,\n capability: options.capability,\n extension_mode: options.extensionMode,\n route: options.route,\n parent_system_prompt: ctx.getSystemPrompt(),\n parent_leaf_id: snapshot.leafId,\n directive,\n conversation_projection: sealed.projection,\n limits: options.limits,\n };\n const serialized = canonicalJson(seed);\n return { seed, serialized, sha256: sha256Text(serialized), ledger: sealed.ledger };\n}\n\nconst isRecord = (value: unknown): value is Record<PropertyKey, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/**\n * Receive-side seed validation performed inside the child before its first model\n * call. A seed that does not match its declared length, hash, schema, task id,\n * and launch nonce is a loud refusal, never a best-effort continue.\n */\nexport function verifyDelegateSeedBytes(\n raw: string,\n expected: { sha256: string; taskId: string; launchNonce: string },\n): DelegateSeedV1 {\n const actual = sha256Text(raw);\n if (actual !== expected.sha256) {\n throw new DelegateError(\n `delegate seed hash mismatch: expected ${expected.sha256}, received ${actual}`,\n {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId: expected.taskId,\n remediation: ['The seed bytes delivered to the child differ from the persisted seed.'],\n },\n );\n }\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (error) {\n throw new DelegateError(\n `delegate seed is not valid JSON: ${error instanceof Error ? error.message : String(error)}`,\n { code: 'seed_hash_mismatch', childCreated: true, taskId: expected.taskId },\n );\n }\n if (!isRecord(parsed)) {\n throw new DelegateError('delegate seed must be a JSON object', {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId: expected.taskId,\n });\n }\n if (parsed['schema_version'] !== DELEGATE_SEED_SCHEMA_VERSION) {\n throw new DelegateError(\n `delegate seed schema_version must be ${DELEGATE_SEED_SCHEMA_VERSION}`,\n { code: 'seed_hash_mismatch', childCreated: true, taskId: expected.taskId },\n );\n }\n if (parsed['task_id'] !== expected.taskId || parsed['launch_nonce'] !== expected.launchNonce) {\n throw new DelegateError('delegate seed task identity does not match this child', {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId: expected.taskId,\n });\n }\n // The hash pins the exact bytes; this rebuild pins the shape the child is\n // allowed to act on. Every field the child reads is validated and copied\n // explicitly, so a structurally malformed seed cannot reach the agent loop by\n // way of an unchecked assertion.\n return rebuildSeed(parsed, expected.taskId);\n}\n\nfunction requireString(record: Record<PropertyKey, unknown>, key: string, taskId: string): string {\n const value = record[key];\n if (typeof value !== 'string') {\n throw new DelegateError(`delegate seed field ${key} must be a string`, {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n return value;\n}\n\nfunction requireRecord(\n record: Record<PropertyKey, unknown>,\n key: string,\n taskId: string,\n): Record<PropertyKey, unknown> {\n const value = record[key];\n if (!isRecord(value)) {\n throw new DelegateError(`delegate seed field ${key} must be an object`, {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n return value;\n}\n\nfunction requireNonNegativeInteger(\n record: Record<PropertyKey, unknown>,\n key: string,\n taskId: string,\n): number {\n const value = record[key];\n if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) {\n throw new DelegateError(`delegate seed field ${key} must be a non-negative safe integer`, {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n return value;\n}\n\n/** Explicit, field-by-field reconstruction of the seed fields the child consumes. */\nfunction rebuildSeed(record: Record<PropertyKey, unknown>, taskId: string): DelegateSeedV1 {\n const capability = requireString(record, 'capability', taskId);\n if (capability !== 'inspect') {\n throw new DelegateError(`delegate seed capability ${capability} is not supported`, {\n code: 'delegate_isolation_unsupported',\n childCreated: true,\n taskId,\n });\n }\n const extensionMode = requireString(record, 'extension_mode', taskId);\n if (extensionMode !== 'isolated' && extensionMode !== 'ambient') {\n throw new DelegateError(`delegate seed extension mode ${extensionMode} is not recognised`, {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n const routeRecord = requireRecord(record, 'route', taskId);\n const routeOrigin = requireString(routeRecord, 'origin', taskId);\n if (routeOrigin !== 'parent_current' && routeOrigin !== 'explicit') {\n throw new DelegateError(`delegate seed route origin ${routeOrigin} is not recognised`, {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n const route: DelegatePinnedRoute = {\n provider: requireString(routeRecord, 'provider', taskId),\n model: requireString(routeRecord, 'model', taskId),\n qualified_id: requireString(routeRecord, 'qualified_id', taskId),\n context_window_tokens: requireNonNegativeInteger(routeRecord, 'context_window_tokens', taskId),\n thinking_level: requireString(routeRecord, 'thinking_level', taskId),\n origin: routeOrigin,\n };\n const limitsRecord = requireRecord(record, 'limits', taskId);\n const limits: DelegateLimits = {\n max_turns: requireNonNegativeInteger(limitsRecord, 'max_turns', taskId),\n max_tool_calls: requireNonNegativeInteger(limitsRecord, 'max_tool_calls', taskId),\n timeout_seconds: requireNonNegativeInteger(limitsRecord, 'timeout_seconds', taskId),\n max_tool_result_bytes: requireNonNegativeInteger(limitsRecord, 'max_tool_result_bytes', taskId),\n max_total_tool_output_bytes: requireNonNegativeInteger(\n limitsRecord,\n 'max_total_tool_output_bytes',\n taskId,\n ),\n max_answer_bytes: requireNonNegativeInteger(limitsRecord, 'max_answer_bytes', taskId),\n allowed_input_tokens: requireNonNegativeInteger(limitsRecord, 'allowed_input_tokens', taskId),\n };\n const directiveRecord = requireRecord(record, 'directive', taskId);\n const authority = requireString(directiveRecord, 'authority', taskId);\n if (authority !== 'explicit_text') {\n throw new DelegateError(`delegate seed directive authority ${authority} is not recognised`, {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n const directive: DelegateTaskDirective = {\n text: requireString(directiveRecord, 'text', taskId),\n sha256: requireString(directiveRecord, 'sha256', taskId),\n authority,\n };\n if (sha256Text(directive.text) !== directive.sha256) {\n throw new DelegateError('delegate seed directive hash does not match its text', {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n const parentLeafId = record['parent_leaf_id'];\n if (parentLeafId !== null && typeof parentLeafId !== 'string') {\n throw new DelegateError('delegate seed parent_leaf_id must be a string or null', {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n const projection = requireRecord(record, 'conversation_projection', taskId);\n const projectionPolicy = requireRecord(projection, 'policy', taskId);\n if (requireString(projectionPolicy, 'id', taskId) !== DELEGATE_CONTEXT_POLICY_ID) {\n throw new DelegateError('delegate seed projection policy id is not the delegate policy', {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n if (!Array.isArray(projection['entries'])) {\n throw new DelegateError('delegate seed projection entries must be an array', {\n code: 'seed_hash_mismatch',\n childCreated: true,\n taskId,\n });\n }\n return {\n schema_version: DELEGATE_SEED_SCHEMA_VERSION,\n task_id: requireString(record, 'task_id', taskId),\n launch_nonce: requireString(record, 'launch_nonce', taskId),\n cwd: requireString(record, 'cwd', taskId),\n capability,\n extension_mode: extensionMode,\n route,\n parent_system_prompt: requireString(record, 'parent_system_prompt', taskId),\n parent_leaf_id: parentLeafId,\n directive,\n conversation_projection: readDelegateProjection(projection),\n limits,\n };\n}\n\n/**\n * The projection body is already hash-pinned by the seed digest verified above,\n * so it is carried through without re-deriving values that would only duplicate\n * the parent's computation.\n */\nfunction readDelegateProjection(\n record: Record<PropertyKey, unknown>,\n): DelegateConversationProjection {\n const projection: unknown = record;\n if (!isDelegateProjection(projection)) {\n throw new DelegateError('delegate seed conversation_projection is malformed', {\n code: 'seed_hash_mismatch',\n childCreated: true,\n });\n }\n return projection;\n}\n\nfunction isDelegateProjection(value: unknown): value is DelegateConversationProjection {\n if (!isRecord(value)) return false;\n if (!isRecord(value['policy']) || !isRecord(value['branch_filter'])) return false;\n if (!isRecord(value['accounting'])) return false;\n return Array.isArray(value['entries']);\n}\n"]}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export const DELEGATE_SEED_SCHEMA_VERSION = 'pi-background-tasks.delegate-seed.v2';
|
|
2
|
+
export const DELEGATE_LEDGER_SCHEMA_VERSION = 'pi-background-tasks.delegate-ledger.v1';
|
|
3
|
+
export const DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION = 'pi-background-tasks.delegate-result.v1';
|
|
4
|
+
export const DELEGATE_RECEIPT_SCHEMA_VERSION = 'pi-background-tasks.delegate-receipt.v1';
|
|
5
|
+
export const DELEGATE_BUDGET_PLAN_SCHEMA_VERSION = 'pi-background-tasks.delegate-budget-plan.v3';
|
|
6
|
+
export const DELEGATE_MANIFEST_SCHEMA_VERSION = 'pi-background-tasks.delegate-manifest.v2';
|
|
7
|
+
/**
|
|
8
|
+
* Delegate's own context policy id. It shares the frozen
|
|
9
|
+
* `visible-conversation-ledger-v2` transform with Fusion but is a distinct
|
|
10
|
+
* consumer identity, so a delegate artifact can never be mistaken for a Fusion
|
|
11
|
+
* artifact and neither can claim the other's provenance.
|
|
12
|
+
*/
|
|
13
|
+
export const DELEGATE_CONTEXT_POLICY_ID = 'delegate-inspect-v1';
|
|
14
|
+
export const DELEGATE_BRANCH_FILTER_ID = 'exclude-active-delegate-batch-v1';
|
|
15
|
+
export const DELEGATE_TOOL_NAME = 'bg_delegate';
|
|
16
|
+
export const DELEGATE_RESULT_TOOL_NAME = 'bg_result';
|
|
17
|
+
export const DELEGATE_CAPABILITIES = ['inspect'];
|
|
18
|
+
/**
|
|
19
|
+
* Controls only Pi's ambient extension discovery for delegate children.
|
|
20
|
+
* Tool and project-resource restrictions remain independently enforced.
|
|
21
|
+
*/
|
|
22
|
+
export const DELEGATE_EXTENSION_MODES = ['isolated', 'ambient'];
|
|
23
|
+
export const DELEGATE_AUTO_DELIVER_MODES = ['never', 'when_small', 'always'];
|
|
24
|
+
export const DELEGATE_DELIVERY_MODES = ['inline', 'artifact'];
|
|
25
|
+
export const DELEGATE_ERROR_CODES = [
|
|
26
|
+
// Admission failures. No child process exists in these states.
|
|
27
|
+
'delegate_hook_contract_unsupported',
|
|
28
|
+
'delegate_isolation_unsupported',
|
|
29
|
+
'route_unresolved',
|
|
30
|
+
'route_capacity_unknown',
|
|
31
|
+
'seed_projection_failed',
|
|
32
|
+
'seed_budget_exceeded',
|
|
33
|
+
'seed_persist_failed',
|
|
34
|
+
'invalid_arguments',
|
|
35
|
+
// Launch and execution.
|
|
36
|
+
'child_spawn_failed',
|
|
37
|
+
'child_startup_failed',
|
|
38
|
+
'child_timeout',
|
|
39
|
+
'child_cancelled',
|
|
40
|
+
'child_turn_limit',
|
|
41
|
+
'child_tool_call_limit',
|
|
42
|
+
'child_exited_without_commit',
|
|
43
|
+
// Budget, split by which budget was exhausted.
|
|
44
|
+
'provider_context_budget_exhausted',
|
|
45
|
+
'aggregate_tool_output_cap',
|
|
46
|
+
'child_model_output_limit',
|
|
47
|
+
'child_capture_limit',
|
|
48
|
+
// Integrity.
|
|
49
|
+
'child_result_invalid',
|
|
50
|
+
'child_result_encoding_invalid',
|
|
51
|
+
'route_attestation_missing',
|
|
52
|
+
'route_mismatch',
|
|
53
|
+
'seed_hash_mismatch',
|
|
54
|
+
'answer_hash_mismatch',
|
|
55
|
+
'artifact_spill_failed',
|
|
56
|
+
'artifact_read_failed',
|
|
57
|
+
'artifact_error',
|
|
58
|
+
// Retrieval states and outcomes.
|
|
59
|
+
'result_not_ready',
|
|
60
|
+
'result_unavailable',
|
|
61
|
+
'result_too_large_for_inline',
|
|
62
|
+
'task_unknown',
|
|
63
|
+
];
|
|
64
|
+
/**
|
|
65
|
+
* Typed delegate failure.
|
|
66
|
+
*
|
|
67
|
+
* Every instance states what happened, what was preserved, and what the operator
|
|
68
|
+
* can do. There is no untyped delegate failure path.
|
|
69
|
+
*/
|
|
70
|
+
export class DelegateError extends Error {
|
|
71
|
+
code;
|
|
72
|
+
childCreated;
|
|
73
|
+
taskId;
|
|
74
|
+
artifactDir;
|
|
75
|
+
budget;
|
|
76
|
+
preserved;
|
|
77
|
+
remediation;
|
|
78
|
+
constructor(message, details) {
|
|
79
|
+
super(message);
|
|
80
|
+
this.name = 'DelegateError';
|
|
81
|
+
this.code = details.code;
|
|
82
|
+
this.childCreated = details.childCreated ?? false;
|
|
83
|
+
this.taskId = details.taskId;
|
|
84
|
+
this.artifactDir = details.artifactDir;
|
|
85
|
+
this.budget = details.budget;
|
|
86
|
+
this.preserved = details.preserved ?? [];
|
|
87
|
+
this.remediation = details.remediation ?? [];
|
|
88
|
+
}
|
|
89
|
+
/** Operator-facing rendering: cause, preserved evidence, and next action. */
|
|
90
|
+
describe() {
|
|
91
|
+
const lines = [`[${this.code}] ${this.message}`];
|
|
92
|
+
lines.push(`Child process created: ${this.childCreated ? 'yes' : 'no'}`);
|
|
93
|
+
if (this.artifactDir !== undefined)
|
|
94
|
+
lines.push(`Artifacts: ${this.artifactDir}`);
|
|
95
|
+
lines.push(this.preserved.length > 0
|
|
96
|
+
? `Preserved: ${this.preserved.join(', ')}`
|
|
97
|
+
: 'Preserved: nothing was written for this failure');
|
|
98
|
+
if (this.remediation.length > 0)
|
|
99
|
+
lines.push(`Remediation: ${this.remediation.join(' ')}`);
|
|
100
|
+
return lines.join('\n');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/core/delegate/types.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,MAAM,4BAA4B,GAAG,sCAA+C,CAAC;AAC5F,MAAM,CAAC,MAAM,8BAA8B,GAAG,wCAAiD,CAAC;AAChG,MAAM,CAAC,MAAM,sCAAsC,GACjD,wCAAiD,CAAC;AACpD,MAAM,CAAC,MAAM,+BAA+B,GAAG,yCAAkD,CAAC;AAClG,MAAM,CAAC,MAAM,mCAAmC,GAC9C,6CAAsD,CAAC;AACzD,MAAM,CAAC,MAAM,gCAAgC,GAAG,0CAAmD,CAAC;AAEpG;;;;;GAKG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,qBAAqB,CAAC;AAChE,MAAM,CAAC,MAAM,yBAAyB,GAAG,kCAAkC,CAAC;AAC5E,MAAM,CAAC,MAAM,kBAAkB,GAAG,aAAa,CAAC;AAChD,MAAM,CAAC,MAAM,yBAAyB,GAAG,WAAW,CAAC;AAErD,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,SAAS,CAAU,CAAC;AAG1D;;;GAGG;AACH,MAAM,CAA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;QAClD,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;QACvC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC;QACzC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,EAAE,CAAC;IAC/C,CAAC;IAED,6EAA6E;IAC7E,QAAQ;QACN,MAAM,KAAK,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QACjD,KAAK,CAAC,IAAI,CAAC,0BAA0B,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QACzE,IAAI,IAAI,CAAC,WAAW,KAAK,SAAS;YAAE,KAAK,CAAC,IAAI,CAAC,cAAc,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;QACjF,KAAK,CAAC,IAAI,CACR,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC;YACvB,CAAC,CAAC,cAAc,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YAC3C,CAAC,CAAC,iDAAiD,CACtD,CAAC;QACF,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,gBAAgB,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC1F,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;CACF","sourcesContent":["import type { Usage } from '@earendil-works/pi-ai';\nimport type {\n TokenBudgetByteClassBreakdown,\n TokenBudgetDominantByteClass,\n TokenBudgetFamily,\n TokenBudgetRateSource,\n} from '../context/token-budget.js';\nimport type {\n ContextProjectionMapEntry,\n OmittedEventRecord,\n ProjectionAccounting,\n ProjectionEntry,\n} from '../context/visible-conversation-v2.js';\n\nexport const DELEGATE_SEED_SCHEMA_VERSION = 'pi-background-tasks.delegate-seed.v2' as const;\nexport const DELEGATE_LEDGER_SCHEMA_VERSION = 'pi-background-tasks.delegate-ledger.v1' as const;\nexport const DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION =\n 'pi-background-tasks.delegate-result.v1' as const;\nexport const DELEGATE_RECEIPT_SCHEMA_VERSION = 'pi-background-tasks.delegate-receipt.v1' as const;\nexport const DELEGATE_BUDGET_PLAN_SCHEMA_VERSION =\n 'pi-background-tasks.delegate-budget-plan.v3' as const;\nexport const DELEGATE_MANIFEST_SCHEMA_VERSION = 'pi-background-tasks.delegate-manifest.v2' as const;\n\n/**\n * Delegate's own context policy id. It shares the frozen\n * `visible-conversation-ledger-v2` transform with Fusion but is a distinct\n * consumer identity, so a delegate artifact can never be mistaken for a Fusion\n * artifact and neither can claim the other's provenance.\n */\nexport const DELEGATE_CONTEXT_POLICY_ID = 'delegate-inspect-v1';\nexport const DELEGATE_BRANCH_FILTER_ID = 'exclude-active-delegate-batch-v1';\nexport const DELEGATE_TOOL_NAME = 'bg_delegate';\nexport const DELEGATE_RESULT_TOOL_NAME = 'bg_result';\n\nexport const DELEGATE_CAPABILITIES = ['inspect'] as const;\nexport type DelegateCapability = (typeof DELEGATE_CAPABILITIES)[number];\n\n/**\n * Controls only Pi's ambient extension discovery for delegate children.\n * Tool and project-resource restrictions remain independently enforced.\n */\nexport const DELEGATE_EXTENSION_MODES = ['isolated', 'ambient'] as const;\nexport type DelegateExtensionMode = (typeof DELEGATE_EXTENSION_MODES)[number];\n\nexport const DELEGATE_AUTO_DELIVER_MODES = ['never', 'when_small', 'always'] as const;\nexport type DelegateAutoDeliverMode = (typeof DELEGATE_AUTO_DELIVER_MODES)[number];\n\nexport const DELEGATE_DELIVERY_MODES = ['inline', 'artifact'] as const;\nexport type DelegateDeliveryMode = (typeof DELEGATE_DELIVERY_MODES)[number];\n\nexport interface DelegateRoute {\n provider: string;\n model: string;\n}\n\nexport interface DelegatePinnedRoute extends DelegateRoute {\n qualified_id: string;\n context_window_tokens: number;\n thinking_level: string;\n /** Whether the route came from the parent's current model or an explicit argument. */\n origin: 'parent_current' | 'explicit';\n}\n\nexport interface DelegateBudgetRouteSource {\n family: TokenBudgetFamily;\n rate_source: TokenBudgetRateSource;\n conservative_rate_source?: TokenBudgetRateSource | undefined;\n}\n\nexport interface DelegateContextPolicyDescriptor {\n id: typeof DELEGATE_CONTEXT_POLICY_ID;\n transform: 'visible-conversation-ledger-v2';\n version: 1;\n receipt_format: 'omitted_activity.v2';\n user_text: 'verbatim';\n assistant_text: 'verbatim';\n assistant_thinking: 'ledger_only';\n tool_call_arguments: 'ledger_only';\n tool_results: 'ledger_only';\n tool_payload_preview_bytes: 0;\n images: 'marker_or_ledger_only';\n unknown_block_behavior: 'error';\n}\n\nexport interface DelegateBranchFilterDescriptor {\n id: typeof DELEGATE_BRANCH_FILTER_ID;\n tool_name: typeof DELEGATE_TOOL_NAME;\n tool_call_id: string | null;\n active_tool_call_leaf_excluded: boolean;\n}\n\nexport interface DelegateConversationProjection {\n policy: DelegateContextPolicyDescriptor;\n branch_filter: DelegateBranchFilterDescriptor;\n entries: readonly ProjectionEntry[];\n accounting: ProjectionAccounting;\n}\n\nexport interface DelegateLedgerV1 {\n schema_version: typeof DELEGATE_LEDGER_SCHEMA_VERSION;\n policy_id: typeof DELEGATE_CONTEXT_POLICY_ID;\n transform: 'visible-conversation-ledger-v2';\n entries: readonly OmittedEventRecord[];\n projection_map: readonly ContextProjectionMapEntry[];\n root_sha256: string;\n}\n\nexport interface DelegateTaskDirective {\n /** Verbatim operator/agent prompt. Always authoritative over projected history. */\n text: string;\n sha256: string;\n authority: 'explicit_text';\n}\n\nexport interface DelegateSeedV1 {\n schema_version: typeof DELEGATE_SEED_SCHEMA_VERSION;\n task_id: string;\n launch_nonce: string;\n cwd: string;\n capability: DelegateCapability;\n extension_mode: DelegateExtensionMode;\n route: DelegatePinnedRoute;\n parent_system_prompt: string;\n parent_leaf_id: string | null;\n directive: DelegateTaskDirective;\n conversation_projection: DelegateConversationProjection;\n limits: DelegateLimits;\n}\n\nexport interface DelegateLimits {\n max_turns: number;\n max_tool_calls: number;\n timeout_seconds: number;\n /** Per-tool-result transcript cap; larger results spill to hashed artifacts. */\n max_tool_result_bytes: number;\n /** Cumulative spilled+inline tool output across the whole run. */\n max_total_tool_output_bytes: number;\n /** Cap on the child's captured final answer. */\n max_answer_bytes: number;\n /** Usable input tokens for the pinned route after reserves. */\n allowed_input_tokens: number;\n}\n\nexport interface DelegateAnswerBlock {\n kind: 'text';\n byte_length: number;\n sha256: string;\n data_base64: string;\n}\n\nexport interface DelegateRouteAttestation {\n provider: string;\n model: string;\n stop_reason: string;\n}\n\n/**\n * Usage that is explicitly absent is reported as absent.\n *\n * A child that never produced a usable usage record must not be reported as\n * having cost zero, so the status is carried alongside the value.\n */\nexport type DelegateUsageReport =\n | { status: 'observed'; usage: Usage }\n | { status: 'unavailable'; reason: string };\n\n/**\n * The single atomically-committed answer data plane.\n *\n * The child writes exactly this document to a temporary file, fsyncs it, and\n * renames it into place. The rename is the commit point: a package that exists\n * under its final name is complete, and one that does not exist means the child\n * produced no accepted answer. There is no second channel to reconcile.\n */\nexport interface DelegateResultPackageV1 {\n schema_version: typeof DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION;\n task_id: string;\n launch_nonce: string;\n seed_sha256: string;\n directive_sha256: string;\n route: DelegateRoute;\n route_attestations: readonly DelegateRouteAttestation[];\n stop_reason: string;\n turns: number;\n tool_calls: number;\n usage: DelegateUsageReport;\n answer: {\n encoding: 'utf-8';\n byte_length: number;\n sha256: string;\n blocks: readonly DelegateAnswerBlock[];\n };\n spilled_artifacts: readonly DelegateSpillReceipt[];\n}\n\nexport type DelegateSpillContentFormat =\n | 'single_text_utf8'\n | 'tool_result_content_json_v1'\n | 'opaque_bytes';\n\nexport interface DelegateSpillReceipt {\n schema_version: typeof DELEGATE_RECEIPT_SCHEMA_VERSION;\n artifact: string;\n tool_name: string;\n tool_call_id: string;\n turn_sequence: number;\n source_call_index: number;\n byte_length: number;\n sha256: string;\n /**\n * Encoding of the hashed artifact bytes. Optional only for compatibility\n * with v1 receipts written before content formats were recorded.\n */\n content_format?: DelegateSpillContentFormat | undefined;\n}\n\nexport const DELEGATE_ERROR_CODES = [\n // Admission failures. No child process exists in these states.\n 'delegate_hook_contract_unsupported',\n 'delegate_isolation_unsupported',\n 'route_unresolved',\n 'route_capacity_unknown',\n 'seed_projection_failed',\n 'seed_budget_exceeded',\n 'seed_persist_failed',\n 'invalid_arguments',\n // Launch and execution.\n 'child_spawn_failed',\n 'child_startup_failed',\n 'child_timeout',\n 'child_cancelled',\n 'child_turn_limit',\n 'child_tool_call_limit',\n 'child_exited_without_commit',\n // Budget, split by which budget was exhausted.\n 'provider_context_budget_exhausted',\n 'aggregate_tool_output_cap',\n 'child_model_output_limit',\n 'child_capture_limit',\n // Integrity.\n 'child_result_invalid',\n 'child_result_encoding_invalid',\n 'route_attestation_missing',\n 'route_mismatch',\n 'seed_hash_mismatch',\n 'answer_hash_mismatch',\n 'artifact_spill_failed',\n 'artifact_read_failed',\n 'artifact_error',\n // Retrieval states and outcomes.\n 'result_not_ready',\n 'result_unavailable',\n 'result_too_large_for_inline',\n 'task_unknown',\n] as const;\n\nexport type DelegateErrorCode = (typeof DELEGATE_ERROR_CODES)[number];\n\nexport interface DelegateBudgetErrorDetail {\n measurement_kind: 'launch_admission' | 'runtime_context';\n measured_utf8_bytes: number;\n measured_input_tokens_upper_bound: number;\n allowed_input_tokens: number;\n rate_source: TokenBudgetRateSource;\n backed: boolean;\n dominant_byte_class: TokenBudgetDominantByteClass;\n byte_class_breakdown: TokenBudgetByteClassBreakdown;\n}\n\nexport interface DelegateErrorDetails {\n code: DelegateErrorCode;\n /** True only when an OS process was actually created. Admission failures are false. */\n childCreated?: boolean;\n taskId?: string;\n artifactDir?: string;\n budget?: DelegateBudgetErrorDetail;\n /** What is preserved on disk despite the failure. */\n preserved?: readonly string[];\n /** Concrete operator actions. */\n remediation?: readonly string[];\n}\n\n/**\n * Typed delegate failure.\n *\n * Every instance states what happened, what was preserved, and what the operator\n * can do. There is no untyped delegate failure path.\n */\nexport class DelegateError extends Error {\n readonly code: DelegateErrorCode;\n readonly childCreated: boolean;\n readonly taskId: string | undefined;\n readonly artifactDir: string | undefined;\n readonly budget: DelegateBudgetErrorDetail | undefined;\n readonly preserved: readonly string[];\n readonly remediation: readonly string[];\n\n constructor(message: string, details: DelegateErrorDetails) {\n super(message);\n this.name = 'DelegateError';\n this.code = details.code;\n this.childCreated = details.childCreated ?? false;\n this.taskId = details.taskId;\n this.artifactDir = details.artifactDir;\n this.budget = details.budget;\n this.preserved = details.preserved ?? [];\n this.remediation = details.remediation ?? [];\n }\n\n /** Operator-facing rendering: cause, preserved evidence, and next action. */\n describe(): string {\n const lines = [`[${this.code}] ${this.message}`];\n lines.push(`Child process created: ${this.childCreated ? 'yes' : 'no'}`);\n if (this.artifactDir !== undefined) lines.push(`Artifacts: ${this.artifactDir}`);\n lines.push(\n this.preserved.length > 0\n ? `Preserved: ${this.preserved.join(', ')}`\n : 'Preserved: nothing was written for this failure',\n );\n if (this.remediation.length > 0) lines.push(`Remediation: ${this.remediation.join(' ')}`);\n return lines.join('\\n');\n }\n}\n"]}
|