pi-background-tasks 0.7.6 → 0.7.7
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/PUBLISHING.md +7 -7
- package/README.md +177 -13
- package/TESTING.md +94 -0
- package/TEST_PLAN.md +21 -4
- package/extensions/delegate-child.ts +1 -0
- package/package.json +4 -3
- package/src/core/common.ts +41 -0
- package/src/core/context/parent-snapshot.ts +142 -0
- package/src/core/context/token-budget.ts +890 -0
- package/src/core/context/visible-conversation-v2.ts +551 -0
- package/src/core/delegate/artifacts.ts +479 -0
- package/src/core/delegate/budget.ts +370 -0
- package/src/core/delegate/hook-contract-evidence.json +18 -0
- package/src/core/delegate/hook-contract.ts +153 -0
- package/src/core/delegate/launch.ts +459 -0
- package/src/core/delegate/result-package.ts +443 -0
- package/src/core/delegate/runner.ts +406 -0
- package/src/core/delegate/seed.ts +411 -0
- package/src/core/delegate/types.ts +304 -0
- package/src/core/fusion/artifacts.ts +15 -0
- package/src/core/fusion/budget.ts +444 -54
- package/src/core/fusion/context.ts +108 -509
- package/src/core/fusion/orchestrator.ts +116 -5
- package/src/core/fusion/prompts.ts +5 -1
- package/src/core/fusion/types.ts +154 -36
- package/src/core/registry.ts +174 -0
- package/src/delegate-child-extension.ts +673 -0
- package/src/delegate-extension.ts +587 -0
- package/src/extension.ts +10 -0
- package/src/fusion-extension.ts +2 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import type { Usage } from '@earendil-works/pi-ai';
|
|
2
|
+
import type {
|
|
3
|
+
TokenBudgetByteClassBreakdown,
|
|
4
|
+
TokenBudgetDominantByteClass,
|
|
5
|
+
TokenBudgetFamily,
|
|
6
|
+
TokenBudgetRateSource,
|
|
7
|
+
} from '../context/token-budget.js';
|
|
8
|
+
import type {
|
|
9
|
+
ContextProjectionMapEntry,
|
|
10
|
+
OmittedEventRecord,
|
|
11
|
+
ProjectionAccounting,
|
|
12
|
+
ProjectionEntry,
|
|
13
|
+
} from '../context/visible-conversation-v2.js';
|
|
14
|
+
|
|
15
|
+
export const DELEGATE_SEED_SCHEMA_VERSION = 'pi-background-tasks.delegate-seed.v1' as const;
|
|
16
|
+
export const DELEGATE_LEDGER_SCHEMA_VERSION = 'pi-background-tasks.delegate-ledger.v1' as const;
|
|
17
|
+
export const DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION =
|
|
18
|
+
'pi-background-tasks.delegate-result.v1' as const;
|
|
19
|
+
export const DELEGATE_RECEIPT_SCHEMA_VERSION = 'pi-background-tasks.delegate-receipt.v1' as const;
|
|
20
|
+
export const DELEGATE_BUDGET_PLAN_SCHEMA_VERSION =
|
|
21
|
+
'pi-background-tasks.delegate-budget-plan.v2' as const;
|
|
22
|
+
export const DELEGATE_MANIFEST_SCHEMA_VERSION = 'pi-background-tasks.delegate-manifest.v1' as const;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Delegate's own context policy id. It shares the frozen
|
|
26
|
+
* `visible-conversation-ledger-v2` transform with Fusion but is a distinct
|
|
27
|
+
* consumer identity, so a delegate artifact can never be mistaken for a Fusion
|
|
28
|
+
* artifact and neither can claim the other's provenance.
|
|
29
|
+
*/
|
|
30
|
+
export const DELEGATE_CONTEXT_POLICY_ID = 'delegate-inspect-v1';
|
|
31
|
+
export const DELEGATE_BRANCH_FILTER_ID = 'exclude-active-delegate-batch-v1';
|
|
32
|
+
export const DELEGATE_TOOL_NAME = 'bg_delegate';
|
|
33
|
+
export const DELEGATE_RESULT_TOOL_NAME = 'bg_result';
|
|
34
|
+
|
|
35
|
+
export const DELEGATE_CAPABILITIES = ['inspect'] as const;
|
|
36
|
+
export type DelegateCapability = (typeof DELEGATE_CAPABILITIES)[number];
|
|
37
|
+
|
|
38
|
+
export const DELEGATE_AUTO_DELIVER_MODES = ['never', 'when_small', 'always'] as const;
|
|
39
|
+
export type DelegateAutoDeliverMode = (typeof DELEGATE_AUTO_DELIVER_MODES)[number];
|
|
40
|
+
|
|
41
|
+
export const DELEGATE_DELIVERY_MODES = ['inline', 'artifact'] as const;
|
|
42
|
+
export type DelegateDeliveryMode = (typeof DELEGATE_DELIVERY_MODES)[number];
|
|
43
|
+
|
|
44
|
+
export interface DelegateRoute {
|
|
45
|
+
provider: string;
|
|
46
|
+
model: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface DelegatePinnedRoute extends DelegateRoute {
|
|
50
|
+
qualified_id: string;
|
|
51
|
+
context_window_tokens: number;
|
|
52
|
+
thinking_level: string;
|
|
53
|
+
/** Whether the route came from the parent's current model or an explicit argument. */
|
|
54
|
+
origin: 'parent_current' | 'explicit';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface DelegateBudgetRouteSource {
|
|
58
|
+
family: TokenBudgetFamily;
|
|
59
|
+
rate_source: TokenBudgetRateSource;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface DelegateContextPolicyDescriptor {
|
|
63
|
+
id: typeof DELEGATE_CONTEXT_POLICY_ID;
|
|
64
|
+
transform: 'visible-conversation-ledger-v2';
|
|
65
|
+
version: 1;
|
|
66
|
+
receipt_format: 'omitted_activity.v2';
|
|
67
|
+
user_text: 'verbatim';
|
|
68
|
+
assistant_text: 'verbatim';
|
|
69
|
+
assistant_thinking: 'ledger_only';
|
|
70
|
+
tool_call_arguments: 'ledger_only';
|
|
71
|
+
tool_results: 'ledger_only';
|
|
72
|
+
tool_payload_preview_bytes: 0;
|
|
73
|
+
images: 'marker_or_ledger_only';
|
|
74
|
+
unknown_block_behavior: 'error';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface DelegateBranchFilterDescriptor {
|
|
78
|
+
id: typeof DELEGATE_BRANCH_FILTER_ID;
|
|
79
|
+
tool_name: typeof DELEGATE_TOOL_NAME;
|
|
80
|
+
tool_call_id: string | null;
|
|
81
|
+
active_tool_call_leaf_excluded: boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface DelegateConversationProjection {
|
|
85
|
+
policy: DelegateContextPolicyDescriptor;
|
|
86
|
+
branch_filter: DelegateBranchFilterDescriptor;
|
|
87
|
+
entries: readonly ProjectionEntry[];
|
|
88
|
+
accounting: ProjectionAccounting;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface DelegateLedgerV1 {
|
|
92
|
+
schema_version: typeof DELEGATE_LEDGER_SCHEMA_VERSION;
|
|
93
|
+
policy_id: typeof DELEGATE_CONTEXT_POLICY_ID;
|
|
94
|
+
transform: 'visible-conversation-ledger-v2';
|
|
95
|
+
entries: readonly OmittedEventRecord[];
|
|
96
|
+
projection_map: readonly ContextProjectionMapEntry[];
|
|
97
|
+
root_sha256: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface DelegateTaskDirective {
|
|
101
|
+
/** Verbatim operator/agent prompt. Always authoritative over projected history. */
|
|
102
|
+
text: string;
|
|
103
|
+
sha256: string;
|
|
104
|
+
authority: 'explicit_text';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface DelegateSeedV1 {
|
|
108
|
+
schema_version: typeof DELEGATE_SEED_SCHEMA_VERSION;
|
|
109
|
+
task_id: string;
|
|
110
|
+
launch_nonce: string;
|
|
111
|
+
cwd: string;
|
|
112
|
+
capability: DelegateCapability;
|
|
113
|
+
route: DelegatePinnedRoute;
|
|
114
|
+
parent_system_prompt: string;
|
|
115
|
+
parent_leaf_id: string | null;
|
|
116
|
+
directive: DelegateTaskDirective;
|
|
117
|
+
conversation_projection: DelegateConversationProjection;
|
|
118
|
+
limits: DelegateLimits;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface DelegateLimits {
|
|
122
|
+
max_turns: number;
|
|
123
|
+
max_tool_calls: number;
|
|
124
|
+
timeout_seconds: number;
|
|
125
|
+
/** Per-tool-result transcript cap; larger results spill to hashed artifacts. */
|
|
126
|
+
max_tool_result_bytes: number;
|
|
127
|
+
/** Cumulative spilled+inline tool output across the whole run. */
|
|
128
|
+
max_total_tool_output_bytes: number;
|
|
129
|
+
/** Cap on the child's captured final answer. */
|
|
130
|
+
max_answer_bytes: number;
|
|
131
|
+
/** Usable input tokens for the pinned route after reserves. */
|
|
132
|
+
allowed_input_tokens: number;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface DelegateAnswerBlock {
|
|
136
|
+
kind: 'text';
|
|
137
|
+
byte_length: number;
|
|
138
|
+
sha256: string;
|
|
139
|
+
data_base64: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface DelegateRouteAttestation {
|
|
143
|
+
provider: string;
|
|
144
|
+
model: string;
|
|
145
|
+
stop_reason: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Usage that is explicitly absent is reported as absent.
|
|
150
|
+
*
|
|
151
|
+
* A child that never produced a usable usage record must not be reported as
|
|
152
|
+
* having cost zero, so the status is carried alongside the value.
|
|
153
|
+
*/
|
|
154
|
+
export type DelegateUsageReport =
|
|
155
|
+
| { status: 'observed'; usage: Usage }
|
|
156
|
+
| { status: 'unavailable'; reason: string };
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The single atomically-committed answer data plane.
|
|
160
|
+
*
|
|
161
|
+
* The child writes exactly this document to a temporary file, fsyncs it, and
|
|
162
|
+
* renames it into place. The rename is the commit point: a package that exists
|
|
163
|
+
* under its final name is complete, and one that does not exist means the child
|
|
164
|
+
* produced no accepted answer. There is no second channel to reconcile.
|
|
165
|
+
*/
|
|
166
|
+
export interface DelegateResultPackageV1 {
|
|
167
|
+
schema_version: typeof DELEGATE_RESULT_PACKAGE_SCHEMA_VERSION;
|
|
168
|
+
task_id: string;
|
|
169
|
+
launch_nonce: string;
|
|
170
|
+
seed_sha256: string;
|
|
171
|
+
directive_sha256: string;
|
|
172
|
+
route: DelegateRoute;
|
|
173
|
+
route_attestations: readonly DelegateRouteAttestation[];
|
|
174
|
+
stop_reason: string;
|
|
175
|
+
turns: number;
|
|
176
|
+
tool_calls: number;
|
|
177
|
+
usage: DelegateUsageReport;
|
|
178
|
+
answer: {
|
|
179
|
+
encoding: 'utf-8';
|
|
180
|
+
byte_length: number;
|
|
181
|
+
sha256: string;
|
|
182
|
+
blocks: readonly DelegateAnswerBlock[];
|
|
183
|
+
};
|
|
184
|
+
spilled_artifacts: readonly DelegateSpillReceipt[];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface DelegateSpillReceipt {
|
|
188
|
+
schema_version: typeof DELEGATE_RECEIPT_SCHEMA_VERSION;
|
|
189
|
+
artifact: string;
|
|
190
|
+
tool_name: string;
|
|
191
|
+
tool_call_id: string;
|
|
192
|
+
turn_sequence: number;
|
|
193
|
+
source_call_index: number;
|
|
194
|
+
byte_length: number;
|
|
195
|
+
sha256: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export const DELEGATE_ERROR_CODES = [
|
|
199
|
+
// Admission failures. No child process exists in these states.
|
|
200
|
+
'delegate_hook_contract_unsupported',
|
|
201
|
+
'delegate_isolation_unsupported',
|
|
202
|
+
'route_unresolved',
|
|
203
|
+
'route_capacity_unknown',
|
|
204
|
+
'seed_projection_failed',
|
|
205
|
+
'seed_budget_exceeded',
|
|
206
|
+
'seed_persist_failed',
|
|
207
|
+
'invalid_arguments',
|
|
208
|
+
// Launch and execution.
|
|
209
|
+
'child_spawn_failed',
|
|
210
|
+
'child_startup_failed',
|
|
211
|
+
'child_timeout',
|
|
212
|
+
'child_cancelled',
|
|
213
|
+
'child_turn_limit',
|
|
214
|
+
'child_tool_call_limit',
|
|
215
|
+
'child_exited_without_commit',
|
|
216
|
+
// Budget, split by which budget was exhausted.
|
|
217
|
+
'provider_context_budget_exhausted',
|
|
218
|
+
'aggregate_tool_output_cap',
|
|
219
|
+
'child_model_output_limit',
|
|
220
|
+
'child_capture_limit',
|
|
221
|
+
// Integrity.
|
|
222
|
+
'child_result_invalid',
|
|
223
|
+
'child_result_encoding_invalid',
|
|
224
|
+
'route_attestation_missing',
|
|
225
|
+
'route_mismatch',
|
|
226
|
+
'seed_hash_mismatch',
|
|
227
|
+
'answer_hash_mismatch',
|
|
228
|
+
'artifact_spill_failed',
|
|
229
|
+
'artifact_read_failed',
|
|
230
|
+
'artifact_error',
|
|
231
|
+
// Retrieval states and outcomes.
|
|
232
|
+
'result_not_ready',
|
|
233
|
+
'result_unavailable',
|
|
234
|
+
'result_too_large_for_inline',
|
|
235
|
+
'task_unknown',
|
|
236
|
+
] as const;
|
|
237
|
+
|
|
238
|
+
export type DelegateErrorCode = (typeof DELEGATE_ERROR_CODES)[number];
|
|
239
|
+
|
|
240
|
+
export interface DelegateBudgetErrorDetail {
|
|
241
|
+
measurement_kind: 'launch_admission' | 'runtime_context';
|
|
242
|
+
measured_utf8_bytes: number;
|
|
243
|
+
measured_input_tokens_upper_bound: number;
|
|
244
|
+
allowed_input_tokens: number;
|
|
245
|
+
rate_source: TokenBudgetRateSource;
|
|
246
|
+
backed: boolean;
|
|
247
|
+
dominant_byte_class: TokenBudgetDominantByteClass;
|
|
248
|
+
byte_class_breakdown: TokenBudgetByteClassBreakdown;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export interface DelegateErrorDetails {
|
|
252
|
+
code: DelegateErrorCode;
|
|
253
|
+
/** True only when an OS process was actually created. Admission failures are false. */
|
|
254
|
+
childCreated?: boolean;
|
|
255
|
+
taskId?: string;
|
|
256
|
+
artifactDir?: string;
|
|
257
|
+
budget?: DelegateBudgetErrorDetail;
|
|
258
|
+
/** What is preserved on disk despite the failure. */
|
|
259
|
+
preserved?: readonly string[];
|
|
260
|
+
/** Concrete operator actions. */
|
|
261
|
+
remediation?: readonly string[];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Typed delegate failure.
|
|
266
|
+
*
|
|
267
|
+
* Every instance states what happened, what was preserved, and what the operator
|
|
268
|
+
* can do. There is no untyped delegate failure path.
|
|
269
|
+
*/
|
|
270
|
+
export class DelegateError extends Error {
|
|
271
|
+
readonly code: DelegateErrorCode;
|
|
272
|
+
readonly childCreated: boolean;
|
|
273
|
+
readonly taskId: string | undefined;
|
|
274
|
+
readonly artifactDir: string | undefined;
|
|
275
|
+
readonly budget: DelegateBudgetErrorDetail | undefined;
|
|
276
|
+
readonly preserved: readonly string[];
|
|
277
|
+
readonly remediation: readonly string[];
|
|
278
|
+
|
|
279
|
+
constructor(message: string, details: DelegateErrorDetails) {
|
|
280
|
+
super(message);
|
|
281
|
+
this.name = 'DelegateError';
|
|
282
|
+
this.code = details.code;
|
|
283
|
+
this.childCreated = details.childCreated ?? false;
|
|
284
|
+
this.taskId = details.taskId;
|
|
285
|
+
this.artifactDir = details.artifactDir;
|
|
286
|
+
this.budget = details.budget;
|
|
287
|
+
this.preserved = details.preserved ?? [];
|
|
288
|
+
this.remediation = details.remediation ?? [];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Operator-facing rendering: cause, preserved evidence, and next action. */
|
|
292
|
+
describe(): string {
|
|
293
|
+
const lines = [`[${this.code}] ${this.message}`];
|
|
294
|
+
lines.push(`Child process created: ${this.childCreated ? 'yes' : 'no'}`);
|
|
295
|
+
if (this.artifactDir !== undefined) lines.push(`Artifacts: ${this.artifactDir}`);
|
|
296
|
+
lines.push(
|
|
297
|
+
this.preserved.length > 0
|
|
298
|
+
? `Preserved: ${this.preserved.join(', ')}`
|
|
299
|
+
: 'Preserved: nothing was written for this failure',
|
|
300
|
+
);
|
|
301
|
+
if (this.remediation.length > 0) lines.push(`Remediation: ${this.remediation.join(' ')}`);
|
|
302
|
+
return lines.join('\n');
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type FusionArtifactRef,
|
|
14
14
|
type FusionAttemptArtifactRecord,
|
|
15
15
|
type FusionBudgetPlanV1,
|
|
16
|
+
type FusionCalibrationViolation,
|
|
16
17
|
type FusionCandidateId,
|
|
17
18
|
type FusionContextOmissionLedgerV2,
|
|
18
19
|
type FusionChildRunResult,
|
|
@@ -178,6 +179,10 @@ function responseName(prefix: string, kind: 'md' | 'txt'): string {
|
|
|
178
179
|
return `${prefix}.response.${kind}`;
|
|
179
180
|
}
|
|
180
181
|
|
|
182
|
+
function calibrationViolationName(prefix: string): string {
|
|
183
|
+
return `${prefix}.calibration-violation.json`;
|
|
184
|
+
}
|
|
185
|
+
|
|
181
186
|
export class FusionArtifactStore {
|
|
182
187
|
private readonly runDirAbs: string;
|
|
183
188
|
private readonly runDirDisplay: string;
|
|
@@ -352,6 +357,16 @@ export class FusionArtifactStore {
|
|
|
352
357
|
});
|
|
353
358
|
}
|
|
354
359
|
|
|
360
|
+
async recordCalibrationViolation(input: {
|
|
361
|
+
stage: FusionStage;
|
|
362
|
+
slot?: 1 | 2 | 3;
|
|
363
|
+
attempt: number;
|
|
364
|
+
violation: FusionCalibrationViolation;
|
|
365
|
+
}): Promise<FusionArtifactRef> {
|
|
366
|
+
const prefix = attemptPrefix(input.stage, input.slot, input.attempt);
|
|
367
|
+
return this.writeArtifact(calibrationViolationName(prefix), `${canonicalJson(input.violation)}\n`);
|
|
368
|
+
}
|
|
369
|
+
|
|
355
370
|
async recordFailedAttempt(input: RecordFusionFailedAttemptInput): Promise<void> {
|
|
356
371
|
const prefix = attemptPrefix(input.stage, input.slot, input.attempt);
|
|
357
372
|
const promptRef = await this.writeArtifact(`${prefix}.prompt.txt`, input.prompt);
|