@xzzpig/pi-subagents 0.8.0 → 0.10.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/CHANGELOG.md +477 -0
- package/README.md +2 -2
- package/agents/claude-code-writer.md +15 -0
- package/agents/claude-code.md +15 -0
- package/agents/codex-exec-writer.md +15 -0
- package/agents/codex-exec.md +15 -0
- package/agents/cursor-agent-writer.md +14 -0
- package/agents/cursor-agent.md +14 -0
- package/agents/reviewer.md +1 -1
- package/agents/scout.md +1 -1
- package/docs/agents.md +195 -39
- package/docs/configuration.md +81 -22
- package/docs/extension-api.md +69 -14
- package/docs/missions.md +2 -0
- package/docs/models.md +32 -9
- package/docs/observability.md +22 -20
- package/docs/tool-reference.md +123 -14
- package/docs/watchdog.md +92 -114
- package/docs/workflows.md +142 -4
- package/install.mjs +1 -1
- package/package.json +7 -6
- package/prompts/review-loop.md +2 -2
- package/skills/council-mode/SKILL.md +48 -243
- package/skills/council-mode/references/pass-contracts.md +150 -0
- package/skills/pi-subagents/SKILL.md +89 -35
- package/skills/pi-subagents/references/constraints-and-recipes.md +30 -234
- package/skills/pi-subagents/references/execution-controls.md +91 -15
- package/skills/pi-subagents/references/management-authoring-rpc.md +5 -5
- package/skills/pi-subagents/references/multi-lane-orchestration.md +13 -1
- package/skills/pi-subagents/references/prompting-and-roles.md +59 -31
- package/skills/pi-subagents/references/review-and-validation.md +73 -0
- package/src/agents/agent-management.ts +384 -115
- package/src/agents/agent-refinements.ts +4 -4
- package/src/agents/agent-serializer.ts +14 -2
- package/src/agents/agents.ts +935 -372
- package/src/agents/builtin-names.ts +6 -0
- package/src/agents/runtime-agent-events.ts +70 -0
- package/src/agents/runtime-agent-registry.ts +42 -21
- package/src/api/agents.ts +10 -5
- package/src/api/background-work.ts +12 -3
- package/src/api/capability-ceiling.ts +0 -1
- package/src/api/{pi-args.ts → child-tool-plan.ts} +1 -1
- package/src/api/delegation.ts +0 -7
- package/src/api/external-runs.ts +67 -4
- package/src/api/preflight.ts +77 -21
- package/src/api/shared-types.ts +6 -0
- package/src/extension/config.ts +90 -0
- package/src/extension/doctor.ts +5 -13
- package/src/extension/fanout-child.ts +14 -14
- package/src/extension/index.ts +115 -37
- package/src/extension/public-execution.ts +103 -15
- package/src/extension/rpc.ts +120 -25
- package/src/extension/schemas.ts +65 -29
- package/src/extension/tool-description.ts +48 -26
- package/src/inspectors/herdr/actions.ts +2 -1
- package/src/inspectors/herdr/inspector-runner.ts +21 -23
- package/src/inspectors/herdr/session-roots-codec.ts +42 -0
- package/src/integrations/herdr-status.ts +51 -3
- package/src/intercom/native-supervisor-channel.ts +138 -60
- package/src/intercom/result-intercom.ts +7 -0
- package/src/intercom/supervisor-ui.ts +243 -0
- package/src/profiles/profiles.ts +5 -6
- package/src/runs/background/active-async-capacity.ts +103 -18
- package/src/runs/background/async-execution.ts +417 -133
- package/src/runs/background/async-job-tracker.ts +95 -15
- package/src/runs/background/async-resume.ts +60 -28
- package/src/runs/background/async-retention.ts +20 -3
- package/src/runs/background/async-status-snapshot.ts +23 -261
- package/src/runs/background/async-status.ts +204 -14
- package/src/runs/background/auto-drain.ts +1 -1
- package/src/runs/background/chain-append.ts +8 -3
- package/src/runs/background/chain-root-attachment.ts +74 -8
- package/src/runs/background/control-channel.ts +4 -205
- package/src/runs/background/fleet-view.ts +38 -22
- package/src/runs/background/inspect-rpc.ts +8 -8
- package/src/runs/background/notify.ts +238 -12
- package/src/runs/background/process-terminal.ts +16 -0
- package/src/runs/background/result-delivery-ownership.ts +45 -0
- package/src/runs/background/result-files.ts +28 -14
- package/src/runs/background/result-watcher.ts +39 -16
- package/src/runs/background/resume-guidance.ts +2 -2
- package/src/runs/background/retained-children.ts +1 -1
- package/src/runs/background/run-child-session.ts +613 -0
- package/src/runs/background/run-status.ts +84 -17
- package/src/runs/background/runner-aliases.ts +125 -0
- package/src/runs/background/runner-child-sessions.ts +31 -0
- package/src/runs/background/scheduled-runs.ts +174 -18
- package/src/runs/background/stale-run-reconciler.ts +10 -4
- package/src/runs/background/steering.ts +8 -15
- package/src/runs/background/subagent-runner.ts +718 -1161
- package/src/runs/background/subagent-wait.ts +86 -31
- package/src/runs/background/terminal-run-index.ts +1 -1
- package/src/runs/background/wait-completions.ts +43 -2
- package/src/runs/background/wait-config.ts +23 -9
- package/src/runs/background/wait-tool.ts +26 -19
- package/src/runs/foreground/async-steering-action.ts +3 -19
- package/src/runs/foreground/execution.ts +489 -499
- package/src/runs/foreground/foreground-control.ts +13 -0
- package/src/runs/foreground/foreground-history.ts +23 -1
- package/src/runs/foreground/subagent-executor.ts +1223 -402
- package/src/runs/foreground/workflow-detach-reconcile.ts +140 -115
- package/src/runs/foreground/workflow-foreground-steering.ts +24 -98
- package/src/runs/shared/abort-recovery.ts +119 -0
- package/src/runs/shared/acceptance.ts +138 -36
- package/src/runs/shared/async-status-projection.ts +565 -0
- package/src/runs/shared/background-process-options.ts +9 -0
- package/src/runs/shared/capability-ceiling.ts +2 -2
- package/src/runs/shared/child-hooks.ts +25 -0
- package/src/runs/shared/child-identity.ts +32 -6
- package/src/runs/shared/child-launch-plan.ts +151 -0
- package/src/runs/shared/child-launch.ts +364 -0
- package/src/runs/shared/child-lifecycle.ts +25 -0
- package/src/runs/shared/child-runtime-config.ts +126 -0
- package/src/runs/shared/child-session.ts +366 -0
- package/src/runs/shared/child-tool-plan.ts +632 -0
- package/src/runs/shared/claude-code-adapter.ts +133 -0
- package/src/runs/shared/codex-exec-adapter.ts +129 -0
- package/src/runs/shared/completion-evidence.ts +89 -0
- package/src/runs/shared/completion-guard.ts +6 -5
- package/src/runs/shared/cursor-agent-adapter.ts +114 -0
- package/src/runs/shared/dynamic-fanout.ts +3 -3
- package/src/runs/shared/external-cli-contract.ts +167 -0
- package/src/runs/shared/external-cli-preflight.ts +138 -0
- package/src/runs/shared/external-cli-runner.ts +348 -55
- package/src/runs/shared/fast-mode-extension.ts +5 -5
- package/src/runs/shared/host-step-status.ts +231 -0
- package/src/runs/shared/lane-metadata.ts +126 -0
- package/src/runs/shared/launch-cwd.ts +16 -0
- package/src/runs/shared/long-running-guard.ts +2 -1
- package/src/runs/shared/mcp-config-sources.ts +422 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +222 -158
- package/src/runs/shared/mcp-direct-tool-grant.ts +194 -0
- package/src/runs/shared/model-exclusions.ts +139 -9
- package/src/runs/shared/model-fallback.ts +146 -14
- package/src/runs/shared/mutation-evidence.ts +57 -3
- package/src/runs/shared/nested-events.ts +99 -77
- package/src/runs/shared/nested-path.ts +0 -14
- package/src/runs/shared/nested-render.ts +9 -5
- package/src/runs/shared/orca-progress-tabs.ts +12 -7
- package/src/runs/shared/parallel-handoff.ts +423 -7
- package/src/runs/shared/parallel-utils.ts +20 -7
- package/src/runs/shared/permissions.ts +1 -14
- package/src/runs/shared/pi-spawn.ts +69 -35
- package/src/runs/shared/process-signal.ts +16 -0
- package/src/runs/shared/run-fanout-budget.ts +0 -13
- package/src/runs/shared/run-history.ts +21 -1
- package/src/runs/shared/runtime-acknowledged-extensions.ts +0 -27
- package/src/runs/shared/sandbox-profile-guard.ts +85 -0
- package/src/runs/shared/sandbox-startup-diagnostics.ts +54 -0
- package/src/runs/shared/single-output.ts +89 -22
- package/src/runs/shared/structured-output.ts +49 -9
- package/src/runs/shared/subagent-control.ts +6 -2
- package/src/runs/shared/subagent-prompt-runtime.ts +188 -365
- package/src/runs/shared/task-intent.ts +21 -7
- package/src/runs/shared/tool-availability.ts +18 -62
- package/src/runs/shared/tool-budget.ts +0 -14
- package/src/runs/shared/tool-timeout.ts +1 -1
- package/src/runs/shared/workflow-graph.ts +16 -0
- package/src/runs/shared/worktree-cleanup-plan.ts +866 -0
- package/src/runs/shared/worktree.ts +571 -62
- package/src/shared/atomic-json.ts +3 -1
- package/src/shared/child-session-name.ts +46 -0
- package/src/shared/extension-context.ts +24 -0
- package/src/shared/fork-context.ts +21 -12
- package/src/shared/fork-session-cwd.ts +27 -0
- package/src/shared/formatters.ts +18 -3
- package/src/shared/jsonl-writer.ts +11 -0
- package/src/shared/launch-contract.ts +11 -1
- package/src/shared/pruned-fork.ts +450 -0
- package/src/shared/sandbox-profile.ts +23 -0
- package/src/shared/session-file-trust.ts +19 -0
- package/src/shared/session-tokens.ts +14 -3
- package/src/shared/settings.ts +19 -105
- package/src/shared/shortcuts.ts +17 -0
- package/src/shared/thinking-ceiling.ts +0 -6
- package/src/shared/types.ts +545 -70
- package/src/shared/utils.ts +60 -93
- package/src/shared/workflow-child-permit.ts +207 -0
- package/src/slash/delegation-adapters.ts +1 -9
- package/src/slash/delegation-request.ts +0 -4
- package/src/slash/prompt-template-bridge.ts +37 -1
- package/src/slash/slash-bridge.ts +1 -2
- package/src/slash/slash-commands.ts +383 -116
- package/src/slash/slash-live-state.ts +22 -11
- package/src/slash/subagents-admin.ts +29 -12
- package/src/tui/fleet-native-transcript.ts +4 -0
- package/src/tui/fleet-status.ts +217 -82
- package/src/tui/fleet-transcript.ts +11 -5
- package/src/tui/fleet.ts +54 -28
- package/src/tui/render-helpers.ts +31 -0
- package/src/tui/render.ts +1429 -163
- package/src/watchdog/change-signature.ts +40 -1
- package/src/watchdog/child-status.ts +54 -34
- package/src/watchdog/diff-tool.ts +77 -0
- package/src/watchdog/emission-guard.ts +5 -3
- package/src/watchdog/guidance.ts +20 -0
- package/src/watchdog/register-child.ts +28 -25
- package/src/watchdog/register-main.ts +10 -9
- package/src/watchdog/render.ts +4 -5
- package/src/watchdog/review.ts +15 -4
- package/src/watchdog/rules.ts +70 -0
- package/src/watchdog/runtime.ts +75 -92
- package/src/watchdog/scope.ts +0 -11
- package/src/watchdog/settings.ts +48 -104
- package/src/watchdog/turn-delta.ts +1 -1
- package/src/watchdog/types.ts +18 -32
- package/src/watchdog/warning-format.ts +0 -1
- package/src/workflows/chat-progress.ts +7 -3
- package/src/workflows/host-command.ts +235 -0
- package/src/workflows/scripted-workflow.ts +988 -59
- package/src/workflows/workflow-checklist.ts +439 -0
- package/src/workflows/workflow-child-summary.ts +121 -0
- package/src/workflows/workflow-preflight.ts +297 -0
- package/src/workflows/workflow-receipt.ts +232 -6
- package/src/workflows/workflow-resources.ts +150 -0
- package/src/workflows/workflow-settlement.ts +246 -0
- package/src/runs/shared/child-protocol.ts +0 -401
- package/src/runs/shared/pi-args.ts +0 -935
- package/src/runs/shared/subagent-startup-retry.ts +0 -116
- package/src/runs/shared/turn-budget.ts +0 -98
- package/src/shared/post-exit-stdio-guard.ts +0 -85
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
|
-
import type { ProtocolOutputLimit } from "../../shared/types.ts";
|
|
3
|
-
|
|
4
|
-
export type { ProtocolOutputLimit } from "../../shared/types.ts";
|
|
5
|
-
|
|
6
|
-
export const MAX_CHILD_PENDING_LINE_BYTES = 16 * 1024 * 1024;
|
|
7
|
-
export const MAX_CHILD_STDERR_BYTES = 128 * 1024;
|
|
8
|
-
const MAX_PROTOCOL_DIAGNOSTIC_BYTES = 4096;
|
|
9
|
-
const MAX_PROJECTED_JSON_DEPTH = 256;
|
|
10
|
-
|
|
11
|
-
export interface OversizedLineProjection {
|
|
12
|
-
push(chunk: Buffer): boolean;
|
|
13
|
-
finish(): string | undefined;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface OversizedLineProjector {
|
|
17
|
-
accepts(prefix: string): boolean;
|
|
18
|
-
create(): OversizedLineProjection;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type JsonContainer =
|
|
22
|
-
| { type: "array"; state: "value-or-end" | "value" | "comma-or-end" }
|
|
23
|
-
| { type: "object"; state: "key-or-end" | "key" | "colon" | "value" | "comma-or-end"; key?: string };
|
|
24
|
-
|
|
25
|
-
type JsonToken =
|
|
26
|
-
| { type: "string"; role: "key" | "value"; value: string; capture: boolean; escape: boolean; unicodeDigits: number; unicodeValue: string }
|
|
27
|
-
| { type: "literal"; expected: string; index: number; value: boolean | null }
|
|
28
|
-
| { type: "number"; phase: "minus" | "zero" | "int" | "dot" | "frac" | "exp" | "exp-sign" | "exp-digits" };
|
|
29
|
-
|
|
30
|
-
function createPiAggregateProjection(): OversizedLineProjection {
|
|
31
|
-
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
32
|
-
const stack: JsonContainer[] = [];
|
|
33
|
-
let rootState: "value" | "end" = "value";
|
|
34
|
-
let token: JsonToken | undefined;
|
|
35
|
-
let valid = true;
|
|
36
|
-
let eventType: string | undefined;
|
|
37
|
-
let willRetry: boolean | undefined;
|
|
38
|
-
|
|
39
|
-
const parent = (): JsonContainer | undefined => stack.at(-1);
|
|
40
|
-
const isTopLevelField = (key: string | undefined): boolean => stack.length === 1 && parent()?.type === "object" && (key === "type" || key === "willRetry");
|
|
41
|
-
|
|
42
|
-
const completeValue = (value?: string | boolean | null): void => {
|
|
43
|
-
const container = parent();
|
|
44
|
-
if (!container) {
|
|
45
|
-
rootState = "end";
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (container.type === "object") {
|
|
49
|
-
if (stack.length === 1 && container.key === "type" && typeof value === "string") eventType = value;
|
|
50
|
-
if (stack.length === 1 && container.key === "willRetry" && typeof value === "boolean") willRetry = value;
|
|
51
|
-
container.key = undefined;
|
|
52
|
-
container.state = "comma-or-end";
|
|
53
|
-
} else container.state = "comma-or-end";
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const startValue = (char: string): boolean => {
|
|
57
|
-
const container = parent();
|
|
58
|
-
const key = container?.type === "object" ? container.key : undefined;
|
|
59
|
-
if (isTopLevelField(key)) {
|
|
60
|
-
if (key === "type") eventType = undefined;
|
|
61
|
-
else willRetry = undefined;
|
|
62
|
-
}
|
|
63
|
-
if (char === "{" || char === "[") {
|
|
64
|
-
if (stack.length >= MAX_PROJECTED_JSON_DEPTH) return false;
|
|
65
|
-
stack.push(char === "{" ? { type: "object", state: "key-or-end" } : { type: "array", state: "value-or-end" });
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
if (char === '"') {
|
|
69
|
-
token = { type: "string", role: "value", value: "", capture: key === "type" && stack.length === 1, escape: false, unicodeDigits: 0, unicodeValue: "" };
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
if (char === "t") token = { type: "literal", expected: "true", index: 1, value: true };
|
|
73
|
-
else if (char === "f") token = { type: "literal", expected: "false", index: 1, value: false };
|
|
74
|
-
else if (char === "n") token = { type: "literal", expected: "null", index: 1, value: null };
|
|
75
|
-
else if (char === "-") token = { type: "number", phase: "minus" };
|
|
76
|
-
else if (char === "0") token = { type: "number", phase: "zero" };
|
|
77
|
-
else if (char >= "1" && char <= "9") token = { type: "number", phase: "int" };
|
|
78
|
-
else return false;
|
|
79
|
-
return true;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const closeContainer = (): true => {
|
|
83
|
-
stack.pop();
|
|
84
|
-
completeValue();
|
|
85
|
-
return true;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const processChar = (char: string): boolean => {
|
|
89
|
-
if (token?.type === "string") {
|
|
90
|
-
if (token.unicodeDigits > 0) {
|
|
91
|
-
if (!/[0-9a-fA-F]/.test(char)) return false;
|
|
92
|
-
token.unicodeValue += char;
|
|
93
|
-
token.unicodeDigits--;
|
|
94
|
-
if (token.unicodeDigits === 0 && token.capture) {
|
|
95
|
-
if (token.value.length >= 64) return false;
|
|
96
|
-
token.value += String.fromCharCode(Number.parseInt(token.unicodeValue, 16));
|
|
97
|
-
}
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
if (token.escape) {
|
|
101
|
-
token.escape = false;
|
|
102
|
-
if (char === "u") { token.unicodeDigits = 4; token.unicodeValue = ""; return true; }
|
|
103
|
-
if (!'"\\/bfnrt'.includes(char)) return false;
|
|
104
|
-
if (token.capture) {
|
|
105
|
-
if (token.value.length >= 64) return false;
|
|
106
|
-
token.value += ({ b: "\b", f: "\f", n: "\n", r: "\r", t: "\t" } as Record<string, string>)[char] ?? char;
|
|
107
|
-
}
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
if (char === "\\") { token.escape = true; return true; }
|
|
111
|
-
if (char === '"') {
|
|
112
|
-
const finished = token;
|
|
113
|
-
token = undefined;
|
|
114
|
-
if (finished.role === "key") {
|
|
115
|
-
const container = parent();
|
|
116
|
-
if (!container || container.type !== "object") return false;
|
|
117
|
-
container.key = finished.value;
|
|
118
|
-
container.state = "colon";
|
|
119
|
-
} else completeValue(finished.capture ? finished.value : undefined);
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
if (char.charCodeAt(0) < 0x20) return false;
|
|
123
|
-
if (token.capture) {
|
|
124
|
-
if (token.value.length >= 64) return false;
|
|
125
|
-
token.value += char;
|
|
126
|
-
}
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
if (token?.type === "literal") {
|
|
130
|
-
if (char !== token.expected[token.index]) return false;
|
|
131
|
-
token.index++;
|
|
132
|
-
if (token.index === token.expected.length) {
|
|
133
|
-
const value = token.value;
|
|
134
|
-
token = undefined;
|
|
135
|
-
completeValue(value);
|
|
136
|
-
}
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
if (token?.type === "number") {
|
|
140
|
-
const phase = token.phase;
|
|
141
|
-
if (phase === "minus") {
|
|
142
|
-
if (char === "0") token.phase = "zero";
|
|
143
|
-
else if (char >= "1" && char <= "9") token.phase = "int";
|
|
144
|
-
else return false;
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
if (phase === "zero" || phase === "int") {
|
|
148
|
-
if (char >= "0" && char <= "9") { if (phase === "zero") return false; return true; }
|
|
149
|
-
if (char === ".") { token.phase = "dot"; return true; }
|
|
150
|
-
if (char === "e" || char === "E") { token.phase = "exp"; return true; }
|
|
151
|
-
} else if (phase === "dot") {
|
|
152
|
-
if (char >= "0" && char <= "9") { token.phase = "frac"; return true; }
|
|
153
|
-
return false;
|
|
154
|
-
} else if (phase === "frac") {
|
|
155
|
-
if (char >= "0" && char <= "9") return true;
|
|
156
|
-
if (char === "e" || char === "E") { token.phase = "exp"; return true; }
|
|
157
|
-
} else if (phase === "exp") {
|
|
158
|
-
if (char === "+" || char === "-") { token.phase = "exp-sign"; return true; }
|
|
159
|
-
if (char >= "0" && char <= "9") { token.phase = "exp-digits"; return true; }
|
|
160
|
-
return false;
|
|
161
|
-
} else if (phase === "exp-sign") {
|
|
162
|
-
if (char >= "0" && char <= "9") { token.phase = "exp-digits"; return true; }
|
|
163
|
-
return false;
|
|
164
|
-
} else if (phase === "exp-digits" && char >= "0" && char <= "9") return true;
|
|
165
|
-
if (!["zero", "int", "frac", "exp-digits"].includes(phase)) return false;
|
|
166
|
-
token = undefined;
|
|
167
|
-
completeValue();
|
|
168
|
-
return processChar(char);
|
|
169
|
-
}
|
|
170
|
-
if (char === " " || char === "\t" || char === "\r" || char === "\n") return true;
|
|
171
|
-
const container = parent();
|
|
172
|
-
if (!container) return rootState === "value" ? startValue(char) : false;
|
|
173
|
-
if (container.type === "object") {
|
|
174
|
-
if (container.state === "key-or-end" || container.state === "key") {
|
|
175
|
-
if (char === "}" && container.state === "key-or-end") return closeContainer();
|
|
176
|
-
if (char !== '"') return false;
|
|
177
|
-
token = { type: "string", role: "key", value: "", capture: stack.length === 1, escape: false, unicodeDigits: 0, unicodeValue: "" };
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
if (container.state === "colon") {
|
|
181
|
-
if (char !== ":") return false;
|
|
182
|
-
container.state = "value";
|
|
183
|
-
return true;
|
|
184
|
-
}
|
|
185
|
-
if (container.state === "value") return startValue(char);
|
|
186
|
-
if (char === ",") { container.state = "key"; return true; }
|
|
187
|
-
if (char === "}") return closeContainer();
|
|
188
|
-
return false;
|
|
189
|
-
}
|
|
190
|
-
if (container.state === "value-or-end" || container.state === "value") {
|
|
191
|
-
if (char === "]" && container.state === "value-or-end") return closeContainer();
|
|
192
|
-
return startValue(char);
|
|
193
|
-
}
|
|
194
|
-
if (char === ",") { container.state = "value"; return true; }
|
|
195
|
-
if (char === "]") return closeContainer();
|
|
196
|
-
return false;
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
const processText = (text: string): boolean => {
|
|
200
|
-
for (const char of text) if (!processChar(char)) return false;
|
|
201
|
-
return true;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
return {
|
|
205
|
-
push(chunk) {
|
|
206
|
-
if (!valid) return false;
|
|
207
|
-
try { valid = processText(decoder.decode(chunk, { stream: true })); }
|
|
208
|
-
catch { valid = false; }
|
|
209
|
-
return valid;
|
|
210
|
-
},
|
|
211
|
-
finish() {
|
|
212
|
-
try { valid = valid && processText(decoder.decode()); }
|
|
213
|
-
catch { valid = false; }
|
|
214
|
-
if (token?.type === "number" && ["zero", "int", "frac", "exp-digits"].includes(token.phase)) {
|
|
215
|
-
token = undefined;
|
|
216
|
-
completeValue();
|
|
217
|
-
}
|
|
218
|
-
if (!valid || token || stack.length !== 0 || rootState !== "end") return undefined;
|
|
219
|
-
if (eventType === "turn_end") return '{"type":"turn_end"}';
|
|
220
|
-
if (eventType === "agent_end" && typeof willRetry === "boolean") return JSON.stringify({ type: "agent_end", willRetry });
|
|
221
|
-
return undefined;
|
|
222
|
-
},
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Pi JSON mode emits granular message/tool events followed by aggregate
|
|
228
|
-
* `turn_end` and `agent_end` events that duplicate those payloads. Parallel
|
|
229
|
-
* image reads can make one aggregate record exceed the child line limit even
|
|
230
|
-
* though every granular event was valid. Replace only syntactically valid,
|
|
231
|
-
* redundant records with the lifecycle fields the runners consume.
|
|
232
|
-
*/
|
|
233
|
-
export const PI_AGGREGATE_EVENT_PROJECTOR: OversizedLineProjector = {
|
|
234
|
-
accepts(prefix) {
|
|
235
|
-
return prefix.startsWith('{"type":"turn_end"') || prefix.startsWith('{"type":"agent_end"');
|
|
236
|
-
},
|
|
237
|
-
create: createPiAggregateProjection,
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
export function formatProtocolOutputLimit(limit: ProtocolOutputLimit): string {
|
|
241
|
-
return `${limit.code}: child ${limit.stream} line exceeded ${limit.limitBytes} bytes (observed at least ${limit.observedBytes} bytes without a newline).`;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export function createBoundedLineReader(options: {
|
|
245
|
-
stream?: "stdout" | "stderr";
|
|
246
|
-
maxPendingLineBytes?: number;
|
|
247
|
-
oversizedLineProjector?: OversizedLineProjector;
|
|
248
|
-
onLine: (line: string) => void;
|
|
249
|
-
onLimit: (limit: ProtocolOutputLimit) => void;
|
|
250
|
-
}): {
|
|
251
|
-
push(chunk: Buffer | string): void;
|
|
252
|
-
end(): void;
|
|
253
|
-
exceeded(): boolean;
|
|
254
|
-
} {
|
|
255
|
-
const maxPendingLineBytes = options.maxPendingLineBytes ?? MAX_CHILD_PENDING_LINE_BYTES;
|
|
256
|
-
if (!Number.isInteger(maxPendingLineBytes) || maxPendingLineBytes < 1) {
|
|
257
|
-
throw new Error("maxPendingLineBytes must be a positive integer.");
|
|
258
|
-
}
|
|
259
|
-
let pending: Buffer[] = [];
|
|
260
|
-
let pendingBytes = 0;
|
|
261
|
-
let projectedPrefix: Buffer<ArrayBufferLike> = Buffer.alloc(0);
|
|
262
|
-
let projectedTail: Buffer<ArrayBufferLike> = Buffer.alloc(0);
|
|
263
|
-
let projectedBytes = 0;
|
|
264
|
-
let projection: OversizedLineProjection | undefined;
|
|
265
|
-
let projectingOversizedLine = false;
|
|
266
|
-
let limitExceeded = false;
|
|
267
|
-
|
|
268
|
-
const diagnosticTail = (prior: Buffer, segment: Buffer): Buffer => {
|
|
269
|
-
const tailFromSegment = segment.subarray(Math.max(0, segment.length - MAX_PROTOCOL_DIAGNOSTIC_BYTES));
|
|
270
|
-
return tailFromSegment.length === MAX_PROTOCOL_DIAGNOSTIC_BYTES
|
|
271
|
-
? tailFromSegment
|
|
272
|
-
: Buffer.concat([prior.subarray(Math.max(0, prior.length - (MAX_PROTOCOL_DIAGNOSTIC_BYTES - tailFromSegment.length))), tailFromSegment]);
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
const failLimit = (observedBytes: number, prefix: Buffer, tail: Buffer): false => {
|
|
276
|
-
limitExceeded = true;
|
|
277
|
-
pending = [];
|
|
278
|
-
pendingBytes = 0;
|
|
279
|
-
projectingOversizedLine = false;
|
|
280
|
-
projection = undefined;
|
|
281
|
-
projectedPrefix = Buffer.alloc(0);
|
|
282
|
-
projectedTail = Buffer.alloc(0);
|
|
283
|
-
projectedBytes = 0;
|
|
284
|
-
options.onLimit({
|
|
285
|
-
code: "protocol_output_limit",
|
|
286
|
-
stream: options.stream ?? "stdout",
|
|
287
|
-
limitBytes: maxPendingLineBytes,
|
|
288
|
-
observedBytes,
|
|
289
|
-
diagnosticPrefix: prefix.toString("utf8"),
|
|
290
|
-
diagnosticTail: tail.toString("utf8"),
|
|
291
|
-
});
|
|
292
|
-
return false;
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
const finishLine = (): void => {
|
|
296
|
-
if (projectingOversizedLine) {
|
|
297
|
-
const projected = projection?.finish();
|
|
298
|
-
if (projected === undefined) {
|
|
299
|
-
failLimit(projectedBytes, projectedPrefix, projectedTail);
|
|
300
|
-
} else {
|
|
301
|
-
options.onLine(projected);
|
|
302
|
-
}
|
|
303
|
-
} else if (pendingBytes > 0) {
|
|
304
|
-
options.onLine(Buffer.concat(pending, pendingBytes).toString("utf8"));
|
|
305
|
-
}
|
|
306
|
-
pending = [];
|
|
307
|
-
pendingBytes = 0;
|
|
308
|
-
projectingOversizedLine = false;
|
|
309
|
-
projection = undefined;
|
|
310
|
-
projectedPrefix = Buffer.alloc(0);
|
|
311
|
-
projectedTail = Buffer.alloc(0);
|
|
312
|
-
projectedBytes = 0;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
const append = (segment: Buffer): boolean => {
|
|
316
|
-
if (segment.length === 0) return true;
|
|
317
|
-
if (projectingOversizedLine) {
|
|
318
|
-
projectedBytes += segment.length;
|
|
319
|
-
projectedTail = diagnosticTail(projectedTail, segment);
|
|
320
|
-
return projection?.push(segment) === true || failLimit(projectedBytes, projectedPrefix, projectedTail);
|
|
321
|
-
}
|
|
322
|
-
const observedBytes = pendingBytes + segment.length;
|
|
323
|
-
if (observedBytes > maxPendingLineBytes) {
|
|
324
|
-
const prior = pendingBytes > 0 ? Buffer.concat(pending, pendingBytes) : Buffer.alloc(0);
|
|
325
|
-
const prefixFromPrior = prior.subarray(0, MAX_PROTOCOL_DIAGNOSTIC_BYTES);
|
|
326
|
-
const prefix = prefixFromPrior.length === MAX_PROTOCOL_DIAGNOSTIC_BYTES
|
|
327
|
-
? prefixFromPrior
|
|
328
|
-
: Buffer.concat([prefixFromPrior, segment.subarray(0, MAX_PROTOCOL_DIAGNOSTIC_BYTES - prefixFromPrior.length)]);
|
|
329
|
-
const tail = diagnosticTail(prior, segment);
|
|
330
|
-
if (options.oversizedLineProjector?.accepts(prefix.toString("utf8"))) {
|
|
331
|
-
const candidate = options.oversizedLineProjector.create();
|
|
332
|
-
if (!candidate.push(prior) || !candidate.push(segment)) return failLimit(observedBytes, prefix, tail);
|
|
333
|
-
pending = [];
|
|
334
|
-
pendingBytes = 0;
|
|
335
|
-
projectingOversizedLine = true;
|
|
336
|
-
projection = candidate;
|
|
337
|
-
projectedPrefix = prefix;
|
|
338
|
-
projectedTail = tail;
|
|
339
|
-
projectedBytes = observedBytes;
|
|
340
|
-
return true;
|
|
341
|
-
}
|
|
342
|
-
return failLimit(observedBytes, prefix, tail);
|
|
343
|
-
}
|
|
344
|
-
pending.push(segment);
|
|
345
|
-
pendingBytes = observedBytes;
|
|
346
|
-
return true;
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
return {
|
|
350
|
-
push(chunk) {
|
|
351
|
-
if (limitExceeded) return;
|
|
352
|
-
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
353
|
-
let start = 0;
|
|
354
|
-
for (let index = 0; index < bytes.length; index++) {
|
|
355
|
-
if (bytes[index] !== 0x0a) continue;
|
|
356
|
-
if (!append(bytes.subarray(start, index))) return;
|
|
357
|
-
finishLine();
|
|
358
|
-
if (limitExceeded) return;
|
|
359
|
-
start = index + 1;
|
|
360
|
-
}
|
|
361
|
-
append(bytes.subarray(start));
|
|
362
|
-
},
|
|
363
|
-
end() {
|
|
364
|
-
if (!limitExceeded) finishLine();
|
|
365
|
-
},
|
|
366
|
-
exceeded: () => limitExceeded,
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function trimToUtf8Boundary(buffer: Buffer, maxBytes: number): Buffer {
|
|
371
|
-
if (buffer.length <= maxBytes) return buffer;
|
|
372
|
-
let start = buffer.length - maxBytes;
|
|
373
|
-
while (start < buffer.length && (buffer[start]! & 0xc0) === 0x80) start++;
|
|
374
|
-
return buffer.subarray(start);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
export function createBoundedByteTail(maxBytes = MAX_CHILD_STDERR_BYTES): {
|
|
378
|
-
push(chunk: Buffer | string): void;
|
|
379
|
-
text(): string;
|
|
380
|
-
byteLength(): number;
|
|
381
|
-
} {
|
|
382
|
-
if (!Number.isInteger(maxBytes) || maxBytes < 1) throw new Error("maxBytes must be a positive integer.");
|
|
383
|
-
let tail: Buffer<ArrayBufferLike> = Buffer.alloc(0);
|
|
384
|
-
return {
|
|
385
|
-
push(chunk) {
|
|
386
|
-
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
387
|
-
tail = trimToUtf8Boundary(Buffer.concat([tail, bytes]), maxBytes);
|
|
388
|
-
},
|
|
389
|
-
text: () => tail.toString("utf8"),
|
|
390
|
-
byteLength: () => tail.length,
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
export type ChildLifecycleAction = "start-drain" | "cancel-drain" | "none";
|
|
395
|
-
|
|
396
|
-
export function projectChildLifecycle(event: { type?: string; willRetry?: unknown }, terminalAssistantStop = false): ChildLifecycleAction {
|
|
397
|
-
if (event.type === "agent_end" && event.willRetry === true) return "cancel-drain";
|
|
398
|
-
if (event.type === "agent_settled") return "start-drain";
|
|
399
|
-
if (terminalAssistantStop) return "start-drain";
|
|
400
|
-
return "none";
|
|
401
|
-
}
|