@abelxiaoxing/cadence 1.8.4 → 2.0.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/README.md +324 -117
- package/THIRD_PARTY_NOTICES.md +15 -0
- package/agents/design-explorer.md +14 -18
- package/agents/diagnosis-worker.md +14 -14
- package/agents/implementation-worker.md +16 -17
- package/config/workflow-task.example.json +7 -0
- package/docs/remote-access.md +60 -0
- package/docs/reuse-and-distribution.md +44 -0
- package/docs/service-management.md +108 -0
- package/licenses/bundled-web.txt +6245 -0
- package/licenses/pi-web-ui-MIT.txt +21 -0
- package/licenses/pi-web-ui-reuse.md +22 -0
- package/package.json +94 -21
- package/src/index.ts +21 -1802
- package/src/native-build.ts +6 -0
- package/src/native-client.ts +103 -0
- package/src/native-export.ts +58 -0
- package/src/native-history.ts +359 -0
- package/src/operator-cli.mjs +453 -1937
- package/src/plan-proposal.ts +87 -0
- package/src/sdk-worker.mjs +549 -0
- package/src/service/contracts/active.ts +82 -0
- package/src/service/contracts/budget.ts +148 -0
- package/src/service/contracts/build.ts +67 -0
- package/src/service/contracts/capture.ts +48 -0
- package/src/service/contracts/core.ts +33 -0
- package/src/service/contracts/current.ts +261 -0
- package/src/service/contracts/domain.ts +68 -0
- package/src/service/contracts/history.ts +2245 -0
- package/src/service/contracts/models.ts +139 -0
- package/src/service/coordination.ts +862 -0
- package/src/service/private-files.ts +204 -0
- package/src/service-cli.mjs +5592 -0
- package/src/service-client.ts +747 -0
- package/src/service-contract.ts +245 -0
- package/src/service-host.mjs +22382 -0
- package/src/service-http-client.ts +425 -0
- package/src/service-wire.ts +372 -0
- package/src/task-shell.ts +1045 -0
- package/src/task-store.ts +479 -0
- package/src/task-types.ts +184 -0
- package/src/verification-diagnostics.ts +29 -72
- package/src/worker-capture.mjs +1197 -0
- package/src/worker-public.mjs +1753 -0
- package/src/worker-redaction.mjs +608 -0
- package/src/worker-tools.mjs +202 -0
- package/web/dist/assets/KaTeX_AMS-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_AMS-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_AMS-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.woff +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.ttf +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.woff +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/web/dist/assets/cadence.css +2 -0
- package/web/dist/assets/cadence.js +7 -0
- package/web/dist/assets/markdown.js +296 -0
- package/web/dist/assets/react.js +9 -0
- package/web/dist/assets/rolldown-runtime.js +1 -0
- package/web/dist/assets/timeline-client.js +41 -0
- package/web/dist/assets/vis-timeline.css +1274 -0
- package/web/dist/assets/vis-timeline.js +32 -0
- package/web/dist/index.html +18 -0
- package/web/dist/manifest.json +505 -0
- package/config/plan-draft.example.json +0 -125
- package/config/plan-draft.multiple-tasks.example.json +0 -375
- package/config/plan-draft.quick.example.json +0 -78
- package/prompts/abel-design.md +0 -382
- package/prompts/abel-diagnose.md +0 -71
- package/prompts/abel-implement.md +0 -284
- package/prompts/abel-init.md +0 -52
- package/src/activation.ts +0 -44
- package/src/activity-contracts.ts +0 -82
- package/src/agent-registry.ts +0 -53
- package/src/apply-transaction.ts +0 -1437
- package/src/artifact-store.ts +0 -213
- package/src/build-windows-job.mjs +0 -95
- package/src/candidate-artifact.ts +0 -143
- package/src/candidate-context.ts +0 -289
- package/src/candidate-patch.ts +0 -271
- package/src/candidate-workspace.ts +0 -80
- package/src/canonical.ts +0 -16
- package/src/change-contract-schema.ts +0 -191
- package/src/change-contract.ts +0 -324
- package/src/change-verification.ts +0 -1617
- package/src/contracts.ts +0 -2466
- package/src/control-contracts.ts +0 -382
- package/src/delivery-compiler.ts +0 -1805
- package/src/delivery-revision.ts +0 -42
- package/src/design-control.ts +0 -1299
- package/src/design-diagnostics.ts +0 -181
- package/src/design-journal.ts +0 -1773
- package/src/directory-sync.ts +0 -36
- package/src/durable-contracts.ts +0 -227
- package/src/durable-workflow.ts +0 -822
- package/src/evidence-draft.ts +0 -122
- package/src/execution-profile.ts +0 -64
- package/src/execution-retention.ts +0 -77
- package/src/file-snapshot.ts +0 -163
- package/src/implement-continuation.ts +0 -559
- package/src/implement-graph.ts +0 -690
- package/src/implement-plan.ts +0 -72
- package/src/install-config.d.mts +0 -6
- package/src/isolation-backend.ts +0 -353
- package/src/model-source.ts +0 -22
- package/src/openspec-cli.ts +0 -494
- package/src/operator-cli-entry.mjs +0 -24
- package/src/operator-tools.ts +0 -309
- package/src/package-candidate-process.ts +0 -206
- package/src/package-candidate.ts +0 -24
- package/src/package-delivery.ts +0 -486
- package/src/package-state.ts +0 -320
- package/src/package-verification.ts +0 -635
- package/src/package-workflow.ts +0 -305
- package/src/packet-runtime.ts +0 -459
- package/src/phase-execution.ts +0 -2163
- package/src/pi-adapter.ts +0 -19
- package/src/plan-draft-summary.ts +0 -527
- package/src/plan-draft.ts +0 -502
- package/src/remove-path.ts +0 -32
- package/src/run-state.ts +0 -420
- package/src/run-store.ts +0 -1179
- package/src/safe-path.ts +0 -77
- package/src/scoped-grep-worker.mjs +0 -23
- package/src/scoped-tools.ts +0 -775
- package/src/single-task-draft.ts +0 -142
- package/src/sqlite-schema.ts +0 -171
- package/src/state-root.ts +0 -215
- package/src/storage-schema.ts +0 -360
- package/src/subagent-activity.ts +0 -1417
- package/src/subagent-process.ts +0 -476
- package/src/task-ledger.ts +0 -1338
- package/src/transport-budget.ts +0 -50
- package/src/verification-capability.ts +0 -1191
- package/src/verification-environment-io.ts +0 -389
- package/src/verification-environment.ts +0 -162
- package/src/verification-identity.ts +0 -77
- package/src/verification-scan.ts +0 -64
- package/src/windows-job-backend.ts +0 -419
- package/src/windows-job.c +0 -149
- package/src/workflow-engine.ts +0 -28
- package/src/workflow-policy.ts +0 -1670
- package/src/workflow-recovery-policy.ts +0 -97
- package/src/workflow-scheduling.ts +0 -105
- package/src/workflow-state-machine.ts +0 -4341
- package/src/workflow-status.ts +0 -576
- package/src/workspace-io-entry.mjs +0 -76
- package/src/workspace-io-worker.mjs +0 -1312
- package/src/workspace-io.ts +0 -89
- package/src/workspace-store.ts +0 -634
package/src/packet-runtime.ts
DELETED
|
@@ -1,459 +0,0 @@
|
|
|
1
|
-
import type { Usage } from "@earendil-works/pi-ai";
|
|
2
|
-
import { Activation, type ActivationState } from "./activation.ts";
|
|
3
|
-
import { loadAgentDefinitions } from "./agent-registry.ts";
|
|
4
|
-
import {
|
|
5
|
-
type ChildFailure,
|
|
6
|
-
LIMITS,
|
|
7
|
-
type PacketEnvelope,
|
|
8
|
-
validateDiffResult,
|
|
9
|
-
validateEvidenceResult,
|
|
10
|
-
validatePacketEnvelope,
|
|
11
|
-
} from "./contracts.ts";
|
|
12
|
-
import type { PackageContext } from "./model-source.ts";
|
|
13
|
-
import type { SubagentRole } from "./subagent-process.ts";
|
|
14
|
-
import {
|
|
15
|
-
buildSubagentPrompt,
|
|
16
|
-
runSubagentProcess,
|
|
17
|
-
type SubagentProcessResult,
|
|
18
|
-
} from "./subagent-process.ts";
|
|
19
|
-
|
|
20
|
-
export const PACKET_ACTIONS = ["run", "cancel", "finish"] as const;
|
|
21
|
-
export type PacketAction = (typeof PACKET_ACTIONS)[number];
|
|
22
|
-
|
|
23
|
-
import type {
|
|
24
|
-
PacketActivityEvent,
|
|
25
|
-
PacketActivityObserver,
|
|
26
|
-
PacketActivityState,
|
|
27
|
-
} from "./activity-contracts.ts";
|
|
28
|
-
|
|
29
|
-
export type {
|
|
30
|
-
PacketActivityEvent,
|
|
31
|
-
PacketActivityObserver,
|
|
32
|
-
PacketActivityState,
|
|
33
|
-
PacketFailureReason,
|
|
34
|
-
} from "./activity-contracts.ts";
|
|
35
|
-
|
|
36
|
-
export type PacketDispatchResult =
|
|
37
|
-
| {
|
|
38
|
-
ok: true;
|
|
39
|
-
action: PacketAction;
|
|
40
|
-
result?: unknown;
|
|
41
|
-
usage?: Usage;
|
|
42
|
-
}
|
|
43
|
-
| {
|
|
44
|
-
ok: false;
|
|
45
|
-
notReady?: true;
|
|
46
|
-
error: string;
|
|
47
|
-
failure?: ChildFailure;
|
|
48
|
-
usage?: Usage;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export interface PacketRuntimeOptions {
|
|
52
|
-
activation?: Activation;
|
|
53
|
-
concurrency?: number;
|
|
54
|
-
childRunner?: PacketChildRunner;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type PacketContext = PackageContext;
|
|
58
|
-
|
|
59
|
-
export type PacketChildResult =
|
|
60
|
-
| { ok: true; result: unknown; usage?: Usage }
|
|
61
|
-
| { ok: false; error: string; failure: ChildFailure; usage?: Usage };
|
|
62
|
-
|
|
63
|
-
export type PacketChildRunner = (input: {
|
|
64
|
-
packet: PacketEnvelope;
|
|
65
|
-
context: PacketContext;
|
|
66
|
-
signal: AbortSignal;
|
|
67
|
-
}) => Promise<PacketChildResult>;
|
|
68
|
-
|
|
69
|
-
interface WaitingPacket {
|
|
70
|
-
controller: AbortController;
|
|
71
|
-
resolve(acquired: boolean): void;
|
|
72
|
-
onAbort(): void;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function cancelledResult(signal: AbortSignal): PacketDispatchResult {
|
|
76
|
-
const reason = signal.reason;
|
|
77
|
-
return {
|
|
78
|
-
ok: false,
|
|
79
|
-
error:
|
|
80
|
-
reason instanceof Error
|
|
81
|
-
? reason.message
|
|
82
|
-
: typeof reason === "string"
|
|
83
|
-
? reason
|
|
84
|
-
: "packet cancelled",
|
|
85
|
-
failure: { kind: "cancelled", code: "cancelled" },
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function failureState(failure: ChildFailure): PacketActivityState {
|
|
90
|
-
return failure.kind === "cancelled"
|
|
91
|
-
? "cancelled"
|
|
92
|
-
: failure.kind === "transport" && failure.code === "child-timeout"
|
|
93
|
-
? "timed-out"
|
|
94
|
-
: "failed";
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
type ParsedChildResult =
|
|
98
|
-
| { ok: true; result: unknown }
|
|
99
|
-
| { ok: false; reason: string };
|
|
100
|
-
|
|
101
|
-
function parsePacketResult(
|
|
102
|
-
packet: PacketEnvelope,
|
|
103
|
-
text: string,
|
|
104
|
-
): ParsedChildResult {
|
|
105
|
-
const fenced = [...text.matchAll(/```(?:json)?\s*([\s\S]*?)```/giu)];
|
|
106
|
-
let value: unknown;
|
|
107
|
-
for (let index = fenced.length - 1; index >= 0; index -= 1) {
|
|
108
|
-
try {
|
|
109
|
-
value = JSON.parse(fenced[index]?.[1]?.trim() ?? "");
|
|
110
|
-
break;
|
|
111
|
-
} catch {
|
|
112
|
-
// Keep looking for an earlier valid block; model text is not authority.
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
116
|
-
return { ok: false, reason: "child-result-json-invalid" };
|
|
117
|
-
const candidate = value as Record<string, unknown>;
|
|
118
|
-
if (packet.output === "evidence") {
|
|
119
|
-
const checked = validateEvidenceResult(candidate);
|
|
120
|
-
return checked.ok
|
|
121
|
-
? { ok: true, result: candidate }
|
|
122
|
-
: { ok: false, reason: checked.reason ?? "child-evidence-invalid" };
|
|
123
|
-
}
|
|
124
|
-
const checked = validateDiffResult(candidate);
|
|
125
|
-
return checked.ok
|
|
126
|
-
? { ok: true, result: candidate }
|
|
127
|
-
: { ok: false, reason: checked.reason ?? "child-diff-invalid" };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export class PacketRuntime {
|
|
131
|
-
readonly activation: Activation;
|
|
132
|
-
readonly limits = LIMITS;
|
|
133
|
-
readonly #concurrency: number;
|
|
134
|
-
readonly #childRunner?: PacketChildRunner;
|
|
135
|
-
readonly #controllers = new Set<AbortController>();
|
|
136
|
-
readonly #operations = new Set<Promise<PacketDispatchResult>>();
|
|
137
|
-
readonly #waiting: WaitingPacket[] = [];
|
|
138
|
-
#active = 0;
|
|
139
|
-
#sequence = 0;
|
|
140
|
-
|
|
141
|
-
constructor(options: PacketRuntimeOptions) {
|
|
142
|
-
this.activation = options.activation ?? new Activation();
|
|
143
|
-
this.#childRunner = options.childRunner;
|
|
144
|
-
this.#concurrency = Math.max(
|
|
145
|
-
1,
|
|
146
|
-
Math.min(
|
|
147
|
-
options.concurrency ?? LIMITS.maxActiveChildSessions,
|
|
148
|
-
LIMITS.maxActiveChildSessions,
|
|
149
|
-
),
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
validateRequest(value: unknown) {
|
|
154
|
-
return validatePacketEnvelope(value);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async execute(
|
|
158
|
-
action: string,
|
|
159
|
-
params: { request?: unknown },
|
|
160
|
-
context?: PacketContext,
|
|
161
|
-
signal?: AbortSignal,
|
|
162
|
-
observer?: PacketActivityObserver,
|
|
163
|
-
): Promise<PacketDispatchResult> {
|
|
164
|
-
if (!(PACKET_ACTIONS as readonly string[]).includes(action)) {
|
|
165
|
-
throw new Error(`unknown packet action: ${String(action)}`);
|
|
166
|
-
}
|
|
167
|
-
if (!this.activation.isActive()) {
|
|
168
|
-
return { ok: false, notReady: true, error: "dispatcher is not active" };
|
|
169
|
-
}
|
|
170
|
-
if (action === "cancel") {
|
|
171
|
-
await this.cancel();
|
|
172
|
-
return { ok: true, action };
|
|
173
|
-
}
|
|
174
|
-
if (action === "finish") {
|
|
175
|
-
await this.drain();
|
|
176
|
-
return { ok: true, action };
|
|
177
|
-
}
|
|
178
|
-
const operation = this.#run(params.request, context, signal, observer);
|
|
179
|
-
this.#operations.add(operation);
|
|
180
|
-
try {
|
|
181
|
-
return await operation;
|
|
182
|
-
} finally {
|
|
183
|
-
this.#operations.delete(operation);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async #run(
|
|
188
|
-
request: unknown,
|
|
189
|
-
context?: PacketContext,
|
|
190
|
-
parentSignal?: AbortSignal,
|
|
191
|
-
observer?: PacketActivityObserver,
|
|
192
|
-
): Promise<PacketDispatchResult> {
|
|
193
|
-
if (!context) return { ok: false, error: "run requires extension context" };
|
|
194
|
-
const validation = validatePacketEnvelope(request);
|
|
195
|
-
if (!validation.ok) return { ok: false, error: validation.reason };
|
|
196
|
-
const packet = validation.value;
|
|
197
|
-
const sequence = ++this.#sequence;
|
|
198
|
-
this.#notify(packet, observer, sequence, "queued");
|
|
199
|
-
|
|
200
|
-
const controller = new AbortController();
|
|
201
|
-
this.#controllers.add(controller);
|
|
202
|
-
const forwardCancellation = () => {
|
|
203
|
-
if (!controller.signal.aborted) controller.abort(parentSignal?.reason);
|
|
204
|
-
};
|
|
205
|
-
if (parentSignal?.aborted) forwardCancellation();
|
|
206
|
-
else
|
|
207
|
-
parentSignal?.addEventListener("abort", forwardCancellation, {
|
|
208
|
-
once: true,
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
let acquired = false;
|
|
212
|
-
try {
|
|
213
|
-
acquired = await this.#acquire(controller);
|
|
214
|
-
if (!acquired || controller.signal.aborted) {
|
|
215
|
-
this.#notify(packet, observer, sequence, "cancelled");
|
|
216
|
-
return cancelledResult(controller.signal);
|
|
217
|
-
}
|
|
218
|
-
if (this.#childRunner) {
|
|
219
|
-
this.#notify(packet, observer, sequence, "running");
|
|
220
|
-
}
|
|
221
|
-
const child = await this.#dispatchChild(
|
|
222
|
-
packet,
|
|
223
|
-
context,
|
|
224
|
-
controller.signal,
|
|
225
|
-
(activity) =>
|
|
226
|
-
this.#notify(packet, observer, sequence, activity.state, {
|
|
227
|
-
attempt: activity.attempt,
|
|
228
|
-
maxAttempts: activity.maxAttempts,
|
|
229
|
-
}),
|
|
230
|
-
);
|
|
231
|
-
if (!child.ok) {
|
|
232
|
-
const state = failureState(child.failure);
|
|
233
|
-
this.#notify(packet, observer, sequence, state);
|
|
234
|
-
return {
|
|
235
|
-
ok: false,
|
|
236
|
-
error: child.error,
|
|
237
|
-
failure: child.failure,
|
|
238
|
-
...(child.usage ? { usage: child.usage } : {}),
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
this.#notify(packet, observer, sequence, "completed");
|
|
242
|
-
return {
|
|
243
|
-
ok: true,
|
|
244
|
-
action: "run",
|
|
245
|
-
result: child.result,
|
|
246
|
-
...(child.usage ? { usage: child.usage } : {}),
|
|
247
|
-
};
|
|
248
|
-
} catch (error) {
|
|
249
|
-
this.#notify(
|
|
250
|
-
packet,
|
|
251
|
-
observer,
|
|
252
|
-
sequence,
|
|
253
|
-
controller.signal.aborted ? "cancelled" : "failed",
|
|
254
|
-
);
|
|
255
|
-
throw error;
|
|
256
|
-
} finally {
|
|
257
|
-
parentSignal?.removeEventListener("abort", forwardCancellation);
|
|
258
|
-
this.#controllers.delete(controller);
|
|
259
|
-
if (acquired) this.#release();
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
async #dispatchChild(
|
|
264
|
-
packet: PacketEnvelope,
|
|
265
|
-
context: PacketContext,
|
|
266
|
-
signal: AbortSignal,
|
|
267
|
-
onActivity?: (event: {
|
|
268
|
-
state: PacketActivityState;
|
|
269
|
-
attempt: number;
|
|
270
|
-
maxAttempts: number;
|
|
271
|
-
}) => void,
|
|
272
|
-
): Promise<PacketChildResult> {
|
|
273
|
-
if (signal.aborted)
|
|
274
|
-
return {
|
|
275
|
-
ok: false,
|
|
276
|
-
error: "packet cancelled",
|
|
277
|
-
failure: { kind: "cancelled", code: "cancelled" },
|
|
278
|
-
};
|
|
279
|
-
if (this.#childRunner)
|
|
280
|
-
return this.#childRunner({
|
|
281
|
-
packet: structuredClone(packet),
|
|
282
|
-
context,
|
|
283
|
-
signal,
|
|
284
|
-
});
|
|
285
|
-
const agent = loadAgentDefinitions().find(
|
|
286
|
-
(candidate) => candidate.role === packet.role,
|
|
287
|
-
);
|
|
288
|
-
if (!agent) throw new Error("packet-agent-unavailable");
|
|
289
|
-
const role = packet.role as SubagentRole;
|
|
290
|
-
const result = await runSubagentProcess({
|
|
291
|
-
role,
|
|
292
|
-
cwd: context.cwd,
|
|
293
|
-
model: context.model
|
|
294
|
-
? { provider: context.model.provider, id: context.model.id }
|
|
295
|
-
: undefined,
|
|
296
|
-
prompt: buildSubagentPrompt({
|
|
297
|
-
role,
|
|
298
|
-
agentContent: agent.content,
|
|
299
|
-
objective: packet.objective,
|
|
300
|
-
context: [
|
|
301
|
-
packet.context.agents,
|
|
302
|
-
packet.context.contract,
|
|
303
|
-
JSON.stringify({
|
|
304
|
-
id: packet.id,
|
|
305
|
-
phase: packet.phase,
|
|
306
|
-
read: packet.declared.read,
|
|
307
|
-
write: packet.declared.write,
|
|
308
|
-
output: packet.output,
|
|
309
|
-
}),
|
|
310
|
-
].join("\n\n"),
|
|
311
|
-
}),
|
|
312
|
-
signal,
|
|
313
|
-
onEvent: (event) => {
|
|
314
|
-
if (event.type === "agent_start")
|
|
315
|
-
onActivity?.({
|
|
316
|
-
state: "waiting-first-response",
|
|
317
|
-
attempt: 1,
|
|
318
|
-
maxAttempts: 1,
|
|
319
|
-
});
|
|
320
|
-
if (event.type === "tool_execution_start")
|
|
321
|
-
onActivity?.({ state: "running", attempt: 1, maxAttempts: 1 });
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
return this.#processResult(packet, result);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
#processResult(
|
|
328
|
-
packet: PacketEnvelope,
|
|
329
|
-
result: SubagentProcessResult,
|
|
330
|
-
): PacketChildResult {
|
|
331
|
-
if (result.status !== "completed") {
|
|
332
|
-
const failure: ChildFailure =
|
|
333
|
-
result.status === "cancelled"
|
|
334
|
-
? { kind: "cancelled", code: "cancelled" }
|
|
335
|
-
: result.status === "timed-out"
|
|
336
|
-
? {
|
|
337
|
-
kind: "transport",
|
|
338
|
-
code: "child-timeout",
|
|
339
|
-
stage: "child-timeout",
|
|
340
|
-
}
|
|
341
|
-
: result.status === "output-limit"
|
|
342
|
-
? {
|
|
343
|
-
kind: "result-limit",
|
|
344
|
-
limitBytes: LIMITS.maxCompleteResultBytes,
|
|
345
|
-
}
|
|
346
|
-
: {
|
|
347
|
-
kind: "transport",
|
|
348
|
-
code: "transport-failure",
|
|
349
|
-
stage: "child-provider-stream",
|
|
350
|
-
};
|
|
351
|
-
return {
|
|
352
|
-
ok: false,
|
|
353
|
-
error: result.error ?? result.status,
|
|
354
|
-
failure,
|
|
355
|
-
...(result.usage ? { usage: result.usage } : {}),
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
const parsed = parsePacketResult(packet, result.finalText);
|
|
359
|
-
if (!parsed.ok)
|
|
360
|
-
return {
|
|
361
|
-
ok: false,
|
|
362
|
-
error: parsed.reason,
|
|
363
|
-
failure: {
|
|
364
|
-
kind: "artifact",
|
|
365
|
-
code: "invalid-structural-result",
|
|
366
|
-
stage: "child-provider-stream",
|
|
367
|
-
},
|
|
368
|
-
...(result.usage ? { usage: result.usage } : {}),
|
|
369
|
-
};
|
|
370
|
-
return {
|
|
371
|
-
ok: true,
|
|
372
|
-
result: parsed.result,
|
|
373
|
-
...(result.usage ? { usage: result.usage } : {}),
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
#notify(
|
|
378
|
-
packet: PacketEnvelope,
|
|
379
|
-
observer: PacketActivityObserver | undefined,
|
|
380
|
-
sequence: number,
|
|
381
|
-
state: PacketActivityState,
|
|
382
|
-
metadata: Pick<
|
|
383
|
-
PacketActivityEvent,
|
|
384
|
-
"attempt" | "maxAttempts" | "code" | "wait"
|
|
385
|
-
> = {},
|
|
386
|
-
): void {
|
|
387
|
-
if (!observer) return;
|
|
388
|
-
const event: PacketActivityEvent = {
|
|
389
|
-
state,
|
|
390
|
-
requestId: packet.id,
|
|
391
|
-
role: packet.role,
|
|
392
|
-
phase: packet.phase,
|
|
393
|
-
objective: packet.objective,
|
|
394
|
-
sequence,
|
|
395
|
-
...metadata,
|
|
396
|
-
...(state === "cancelled"
|
|
397
|
-
? { failureReason: "subagent cancelled" as const }
|
|
398
|
-
: state === "timed-out"
|
|
399
|
-
? { failureReason: "phase timed out" as const }
|
|
400
|
-
: state === "failed"
|
|
401
|
-
? { failureReason: "subagent failed" as const }
|
|
402
|
-
: {}),
|
|
403
|
-
};
|
|
404
|
-
try {
|
|
405
|
-
const result = observer(event);
|
|
406
|
-
if (result instanceof Promise) void result.catch(() => undefined);
|
|
407
|
-
} catch {
|
|
408
|
-
// Presentation observers cannot alter execution.
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
#acquire(controller: AbortController): Promise<boolean> {
|
|
413
|
-
if (controller.signal.aborted) return Promise.resolve(false);
|
|
414
|
-
if (this.#active < this.#concurrency) {
|
|
415
|
-
this.#active += 1;
|
|
416
|
-
return Promise.resolve(true);
|
|
417
|
-
}
|
|
418
|
-
return new Promise<boolean>((resolve) => {
|
|
419
|
-
let waiting: WaitingPacket;
|
|
420
|
-
const onAbort = () => {
|
|
421
|
-
const index = this.#waiting.indexOf(waiting);
|
|
422
|
-
if (index >= 0) this.#waiting.splice(index, 1);
|
|
423
|
-
resolve(false);
|
|
424
|
-
};
|
|
425
|
-
waiting = { controller, resolve, onAbort };
|
|
426
|
-
this.#waiting.push(waiting);
|
|
427
|
-
controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
#release(): void {
|
|
432
|
-
this.#active -= 1;
|
|
433
|
-
while (this.#waiting.length > 0) {
|
|
434
|
-
const next = this.#waiting.shift();
|
|
435
|
-
if (!next || next.controller.signal.aborted) continue;
|
|
436
|
-
next.controller.signal.removeEventListener("abort", next.onAbort);
|
|
437
|
-
this.#active += 1;
|
|
438
|
-
next.resolve(true);
|
|
439
|
-
break;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
async cancel(): Promise<void> {
|
|
444
|
-
const reason = new Error("packet runtime cancelled");
|
|
445
|
-
for (const controller of this.#controllers) {
|
|
446
|
-
if (!controller.signal.aborted) controller.abort(reason);
|
|
447
|
-
}
|
|
448
|
-
await Promise.allSettled([...this.#operations]);
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
async drain(): Promise<void> {
|
|
452
|
-
this.activation.drain();
|
|
453
|
-
await this.cancel();
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
get state(): ActivationState {
|
|
457
|
-
return this.activation.state;
|
|
458
|
-
}
|
|
459
|
-
}
|