pi-crew 0.1.45 → 0.1.49
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 +97 -0
- package/README.md +5 -5
- package/agents/analyst.md +11 -11
- package/agents/critic.md +11 -11
- package/agents/executor.md +11 -11
- package/agents/explorer.md +11 -11
- package/agents/planner.md +11 -11
- package/agents/reviewer.md +11 -11
- package/agents/security-reviewer.md +11 -11
- package/agents/test-engineer.md +11 -11
- package/agents/verifier.md +11 -11
- package/agents/writer.md +11 -11
- package/docs/next-upgrade-roadmap.md +808 -0
- package/docs/research/AGENT-EXECUTION-ARCHITECTURE.md +261 -0
- package/docs/research/AGENT-LIFECYCLE-COMPARISON.md +111 -0
- package/docs/research/AUDIT_OH_MY_PI.md +261 -0
- package/docs/research/AUDIT_PI_CREW.md +457 -0
- package/docs/research/CAVEMAN-DEEP-RESEARCH.md +281 -0
- package/docs/research/COMPARISON_OH_MY_PI_VS_PI_CREW.md +264 -0
- package/docs/research/DEEP-RESEARCH-PI-POWERBAR.md +343 -0
- package/docs/research/DEEP_RESEARCH_SUBAGENT_ARCHITECTURE.md +480 -0
- package/docs/research/GAP_CLOSURE_IMPLEMENTATION_PLAN.md +354 -0
- package/docs/research/IMPLEMENTATION_PLAN.md +385 -0
- package/docs/research/LIVE-SESSION-PRODUCTION-READY-PLAN.md +502 -0
- package/docs/research/OH-MY-PI-DEEP-RESEARCH-v14.7.6.md +266 -0
- package/docs/research/REMAINING-GAPS-PLAN.md +363 -0
- package/docs/research/SESSION-SUMMARY-2026-05-08.md +146 -0
- package/docs/research/UI-RESPONSIVENESS-AUDIT.md +173 -0
- package/docs/research-awesome-agent-skills-distillation.md +100 -0
- package/docs/research-oh-my-pi-distillation.md +369 -0
- package/docs/source-runtime-refactor-map.md +24 -0
- package/docs/usage.md +3 -3
- package/install.mjs +52 -8
- package/package.json +99 -98
- package/schema.json +10 -1
- package/skills/async-worker-recovery/SKILL.md +42 -0
- package/skills/context-artifact-hygiene/SKILL.md +52 -0
- package/skills/delegation-patterns/SKILL.md +54 -0
- package/skills/mailbox-interactive/SKILL.md +40 -0
- package/skills/model-routing-context/SKILL.md +39 -0
- package/skills/multi-perspective-review/SKILL.md +58 -0
- package/skills/observability-reliability/SKILL.md +41 -0
- package/skills/orchestration/SKILL.md +157 -0
- package/skills/ownership-session-security/SKILL.md +41 -0
- package/skills/pi-extension-lifecycle/SKILL.md +39 -0
- package/skills/requirements-to-task-packet/SKILL.md +63 -0
- package/skills/resource-discovery-config/SKILL.md +41 -0
- package/skills/runtime-state-reader/SKILL.md +44 -0
- package/skills/secure-agent-orchestration-review/SKILL.md +45 -0
- package/skills/state-mutation-locking/SKILL.md +42 -0
- package/skills/systematic-debugging/SKILL.md +67 -0
- package/skills/ui-render-performance/SKILL.md +39 -0
- package/skills/verification-before-done/SKILL.md +57 -0
- package/skills/worktree-isolation/SKILL.md +39 -0
- package/src/agents/agent-config.ts +6 -0
- package/src/agents/agent-search.ts +98 -0
- package/src/agents/agent-serializer.ts +38 -34
- package/src/agents/discover-agents.ts +29 -15
- package/src/config/config.ts +72 -24
- package/src/config/defaults.ts +25 -0
- package/src/extension/autonomous-policy.ts +26 -33
- package/src/extension/help.ts +1 -0
- package/src/extension/management.ts +5 -0
- package/src/extension/project-init.ts +62 -2
- package/src/extension/register.ts +69 -22
- package/src/extension/registration/commands.ts +64 -25
- package/src/extension/registration/compaction-guard.ts +1 -1
- package/src/extension/registration/subagent-helpers.ts +8 -0
- package/src/extension/registration/subagent-tools.ts +149 -148
- package/src/extension/registration/team-tool.ts +14 -10
- package/src/extension/run-index.ts +35 -21
- package/src/extension/run-maintenance.ts +30 -5
- package/src/extension/team-tool/api.ts +47 -9
- package/src/extension/team-tool/cancel.ts +109 -5
- package/src/extension/team-tool/context.ts +8 -0
- package/src/extension/team-tool/intent-policy.ts +42 -0
- package/src/extension/team-tool/lifecycle-actions.ts +120 -79
- package/src/extension/team-tool/parallel-dispatch.ts +156 -0
- package/src/extension/team-tool/respond.ts +46 -18
- package/src/extension/team-tool/run.ts +55 -12
- package/src/extension/team-tool/status.ts +13 -2
- package/src/extension/team-tool-types.ts +3 -0
- package/src/extension/team-tool.ts +45 -14
- package/src/hooks/registry.ts +61 -0
- package/src/hooks/types.ts +41 -0
- package/src/observability/event-to-metric.ts +8 -1
- package/src/runtime/agent-control.ts +169 -63
- package/src/runtime/async-runner.ts +3 -1
- package/src/runtime/background-runner.ts +78 -53
- package/src/runtime/cancellation-token.ts +89 -0
- package/src/runtime/cancellation.ts +61 -0
- package/src/runtime/capability-inventory.ts +116 -0
- package/src/runtime/child-pi.ts +458 -444
- package/src/runtime/code-summary.ts +247 -0
- package/src/runtime/crash-recovery.ts +182 -0
- package/src/runtime/crew-agent-records.ts +70 -10
- package/src/runtime/crew-agent-runtime.ts +1 -0
- package/src/runtime/custom-tools/irc-tool.ts +201 -0
- package/src/runtime/custom-tools/submit-result-tool.ts +90 -0
- package/src/runtime/deadletter.ts +1 -0
- package/src/runtime/delivery-coordinator.ts +48 -25
- package/src/runtime/effectiveness.ts +81 -0
- package/src/runtime/event-stream-bridge.ts +90 -0
- package/src/runtime/live-agent-control.ts +2 -1
- package/src/runtime/live-agent-manager.ts +179 -85
- package/src/runtime/live-control-realtime.ts +1 -1
- package/src/runtime/live-extension-bridge.ts +150 -0
- package/src/runtime/live-irc.ts +92 -0
- package/src/runtime/live-session-health.ts +100 -0
- package/src/runtime/live-session-runtime.ts +599 -305
- package/src/runtime/manifest-cache.ts +17 -2
- package/src/runtime/mcp-proxy.ts +113 -0
- package/src/runtime/model-fallback.ts +6 -4
- package/src/runtime/notebook-helpers.ts +90 -0
- package/src/runtime/orphan-sentinel.ts +7 -0
- package/src/runtime/output-validator.ts +187 -0
- package/src/runtime/parallel-utils.ts +57 -0
- package/src/runtime/parent-guard.ts +80 -0
- package/src/runtime/pi-args.ts +18 -3
- package/src/runtime/process-status.ts +5 -1
- package/src/runtime/prose-compressor.ts +164 -0
- package/src/runtime/result-extractor.ts +121 -0
- package/src/runtime/retry-executor.ts +81 -64
- package/src/runtime/runtime-resolver.ts +23 -10
- package/src/runtime/semaphore.ts +131 -0
- package/src/runtime/sensitive-paths.ts +92 -0
- package/src/runtime/skill-instructions.ts +222 -0
- package/src/runtime/stale-reconciler.ts +4 -14
- package/src/runtime/stream-preview.ts +177 -0
- package/src/runtime/subagent-manager.ts +6 -2
- package/src/runtime/subprocess-tool-registry.ts +67 -0
- package/src/runtime/task-output-context.ts +177 -127
- package/src/runtime/task-runner/capabilities.ts +78 -0
- package/src/runtime/task-runner/live-executor.ts +107 -101
- package/src/runtime/task-runner/prompt-builder.ts +72 -8
- package/src/runtime/task-runner/prompt-pipeline.ts +64 -0
- package/src/runtime/task-runner/run-projection.ts +104 -0
- package/src/runtime/task-runner.ts +115 -5
- package/src/runtime/team-runner.ts +134 -19
- package/src/runtime/workspace-tree.ts +298 -0
- package/src/runtime/yield-handler.ts +189 -0
- package/src/schema/config-schema.ts +7 -0
- package/src/schema/team-tool-schema.ts +14 -4
- package/src/skills/discover-skills.ts +67 -0
- package/src/state/active-run-registry.ts +167 -0
- package/src/state/artifact-store.ts +4 -1
- package/src/state/atomic-write.ts +50 -1
- package/src/state/blob-store.ts +117 -0
- package/src/state/contracts.ts +2 -1
- package/src/state/event-log-rotation.ts +158 -0
- package/src/state/event-log.ts +52 -2
- package/src/state/mailbox.ts +129 -9
- package/src/state/state-store.ts +32 -5
- package/src/state/types.ts +64 -2
- package/src/teams/team-config.ts +1 -0
- package/src/ui/agent-management-overlay.ts +144 -0
- package/src/ui/crew-widget.ts +15 -5
- package/src/ui/dashboard-panes/cancellation-pane.ts +43 -0
- package/src/ui/dashboard-panes/capability-pane.ts +60 -0
- package/src/ui/dashboard-panes/mailbox-pane.ts +35 -11
- package/src/ui/dashboard-panes/progress-pane.ts +2 -0
- package/src/ui/live-run-sidebar.ts +4 -0
- package/src/ui/powerbar-publisher.ts +77 -15
- package/src/ui/render-coalescer.ts +51 -0
- package/src/ui/run-dashboard.ts +4 -0
- package/src/ui/run-event-bus.ts +209 -0
- package/src/ui/run-snapshot-cache.ts +78 -18
- package/src/ui/snapshot-types.ts +10 -0
- package/src/ui/transcript-entries.ts +258 -0
- package/src/utils/ids.ts +5 -0
- package/src/utils/incremental-reader.ts +104 -0
- package/src/utils/paths.ts +4 -2
- package/src/utils/scan-cache.ts +137 -0
- package/src/utils/sse-parser.ts +134 -0
- package/src/utils/task-name-generator.ts +337 -0
- package/src/utils/visual.ts +33 -2
- package/src/workflows/workflow-config.ts +1 -0
- package/src/worktree/cleanup.ts +2 -1
package/src/runtime/child-pi.ts
CHANGED
|
@@ -1,444 +1,458 @@
|
|
|
1
|
-
import { spawn, type ChildProcess, type SpawnOptions } from "node:child_process";
|
|
2
|
-
import * as fs from "node:fs";
|
|
3
|
-
import * as path from "node:path";
|
|
4
|
-
import type { AgentConfig } from "../agents/agent-config.ts";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (record
|
|
146
|
-
return
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (record.type === "
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (record.type
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
this.
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
let
|
|
300
|
-
let
|
|
301
|
-
let
|
|
302
|
-
let
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
1
|
+
import { spawn, type ChildProcess, type SpawnOptions } from "node:child_process";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import type { AgentConfig } from "../agents/agent-config.ts";
|
|
5
|
+
import type { WorkerExitStatus } from "../state/types.ts";
|
|
6
|
+
import { buildPiWorkerArgs, checkCrewDepth, cleanupTempDir } from "./pi-args.ts";
|
|
7
|
+
import { getPiSpawnCommand } from "./pi-spawn.ts";
|
|
8
|
+
import { DEFAULT_CHILD_PI } from "../config/defaults.ts";
|
|
9
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
10
|
+
import { attachPostExitStdioGuard, trySignalChild } from "./post-exit-stdio-guard.ts";
|
|
11
|
+
import { redactJsonLine } from "../utils/redaction.ts";
|
|
12
|
+
|
|
13
|
+
const POST_EXIT_STDIO_GUARD_MS = DEFAULT_CHILD_PI.postExitStdioGuardMs;
|
|
14
|
+
const FINAL_DRAIN_MS = DEFAULT_CHILD_PI.finalDrainMs;
|
|
15
|
+
const HARD_KILL_MS = DEFAULT_CHILD_PI.hardKillMs;
|
|
16
|
+
const RESPONSE_TIMEOUT_MS = DEFAULT_CHILD_PI.responseTimeoutMs;
|
|
17
|
+
const MAX_CAPTURE_BYTES = DEFAULT_CHILD_PI.maxCaptureBytes;
|
|
18
|
+
const MAX_ASSISTANT_TEXT_CHARS = DEFAULT_CHILD_PI.maxAssistantTextChars;
|
|
19
|
+
const MAX_TOOL_RESULT_CHARS = DEFAULT_CHILD_PI.maxToolResultChars;
|
|
20
|
+
const MAX_TOOL_INPUT_CHARS = DEFAULT_CHILD_PI.maxToolInputChars;
|
|
21
|
+
const MAX_COMPACT_CONTENT_CHARS = DEFAULT_CHILD_PI.maxCompactContentChars;
|
|
22
|
+
const activeChildProcesses = new Map<number, ChildProcess>();
|
|
23
|
+
const childHardKillTimers = new Map<number, NodeJS.Timeout>();
|
|
24
|
+
|
|
25
|
+
function appendBoundedTail(current: string, chunk: string, maxBytes = MAX_CAPTURE_BYTES): string {
|
|
26
|
+
const combined = current + chunk;
|
|
27
|
+
if (Buffer.byteLength(combined, "utf-8") <= maxBytes) return combined;
|
|
28
|
+
let tail = combined.slice(Math.max(0, combined.length - maxBytes));
|
|
29
|
+
while (Buffer.byteLength(tail, "utf-8") > maxBytes) tail = tail.slice(1024);
|
|
30
|
+
return `[pi-crew captured output truncated to last ${Math.round(maxBytes / 1024)} KiB]\n${tail}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function clearHardKillTimer(pid: number | undefined): void {
|
|
34
|
+
if (!pid) return;
|
|
35
|
+
const timer = childHardKillTimers.get(pid);
|
|
36
|
+
if (!timer) return;
|
|
37
|
+
clearTimeout(timer);
|
|
38
|
+
childHardKillTimers.delete(pid);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function killProcessTree(pid: number | undefined, child?: ChildProcess): void {
|
|
42
|
+
if (!pid || !Number.isInteger(pid) || pid <= 0) return;
|
|
43
|
+
if (child && child.exitCode !== null) return;
|
|
44
|
+
try {
|
|
45
|
+
if (process.platform === "win32") {
|
|
46
|
+
spawn("taskkill", ["/pid", String(pid), "/t", "/f"], { stdio: "ignore", windowsHide: true });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
process.kill(-pid, "SIGTERM");
|
|
51
|
+
} catch (error) {
|
|
52
|
+
logInternalError("child-pi.sigterm", error, `pid=${pid}`);
|
|
53
|
+
try {
|
|
54
|
+
process.kill(pid, "SIGTERM");
|
|
55
|
+
} catch (fallbackError) {
|
|
56
|
+
logInternalError("child-pi.sigterm-absolute", fallbackError, `pid=${pid}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
clearHardKillTimer(pid);
|
|
60
|
+
const hardKillTimer = setTimeout(() => {
|
|
61
|
+
try {
|
|
62
|
+
process.kill(-pid, "SIGKILL");
|
|
63
|
+
} catch (error) {
|
|
64
|
+
logInternalError("child-pi.sigkill", error, `pid=${pid}`);
|
|
65
|
+
try {
|
|
66
|
+
process.kill(pid, "SIGKILL");
|
|
67
|
+
} catch (fallbackError) {
|
|
68
|
+
logInternalError("child-pi.sigkill-absolute", fallbackError, `pid=${pid}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
childHardKillTimers.delete(pid);
|
|
72
|
+
}, HARD_KILL_MS);
|
|
73
|
+
hardKillTimer.unref();
|
|
74
|
+
child?.once("exit", () => clearHardKillTimer(pid));
|
|
75
|
+
childHardKillTimers.set(pid, hardKillTimer);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
logInternalError("child-pi.kill-process-tree", error, `pid=${pid}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function terminateActiveChildPiProcesses(): number {
|
|
82
|
+
const entries = [...activeChildProcesses.entries()];
|
|
83
|
+
for (const [pid, child] of entries) killProcessTree(pid, child);
|
|
84
|
+
return entries.length;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface ChildPiRunInput {
|
|
88
|
+
cwd: string;
|
|
89
|
+
task: string;
|
|
90
|
+
agent: AgentConfig;
|
|
91
|
+
model?: string;
|
|
92
|
+
skillPaths?: string[];
|
|
93
|
+
signal?: AbortSignal;
|
|
94
|
+
transcriptPath?: string;
|
|
95
|
+
onStdoutLine?: (line: string) => void;
|
|
96
|
+
onJsonEvent?: (event: unknown) => void;
|
|
97
|
+
onSpawn?: (pid: number) => void;
|
|
98
|
+
maxDepth?: number;
|
|
99
|
+
finalDrainMs?: number;
|
|
100
|
+
hardKillMs?: number;
|
|
101
|
+
responseTimeoutMs?: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface ChildPiRunResult {
|
|
105
|
+
exitCode: number | null;
|
|
106
|
+
stdout: string;
|
|
107
|
+
stderr: string;
|
|
108
|
+
error?: string;
|
|
109
|
+
exitStatus?: WorkerExitStatus;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function buildChildPiSpawnOptions(cwd: string, env: NodeJS.ProcessEnv): SpawnOptions {
|
|
113
|
+
return {
|
|
114
|
+
cwd,
|
|
115
|
+
env: { ...env, PI_CREW_PARENT_PID: String(process.pid) },
|
|
116
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
117
|
+
detached: process.platform !== "win32",
|
|
118
|
+
windowsHide: true,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function appendTranscript(input: ChildPiRunInput, line: string): void {
|
|
123
|
+
if (!input.transcriptPath) return;
|
|
124
|
+
fs.mkdirSync(path.dirname(input.transcriptPath), { recursive: true });
|
|
125
|
+
fs.appendFileSync(input.transcriptPath, `${redactJsonLine(line)}\n`, "utf-8");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function compactString(value: string, maxChars = MAX_COMPACT_CONTENT_CHARS): string {
|
|
129
|
+
if (value.length <= maxChars) return value;
|
|
130
|
+
return `${value.slice(0, maxChars)}\n[pi-crew compacted ${value.length - maxChars} chars]`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function compactValue(value: unknown): unknown {
|
|
134
|
+
if (typeof value === "string") return compactString(value);
|
|
135
|
+
if (Array.isArray(value)) return value.slice(0, 20).map(compactValue);
|
|
136
|
+
const record = asRecord(value);
|
|
137
|
+
if (!record) return value;
|
|
138
|
+
const compacted: Record<string, unknown> = {};
|
|
139
|
+
for (const [key, entry] of Object.entries(record).slice(0, 20)) compacted[key] = compactValue(entry);
|
|
140
|
+
return compacted;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function compactContentPart(part: unknown): unknown | undefined {
|
|
144
|
+
const record = asRecord(part);
|
|
145
|
+
if (!record) return undefined;
|
|
146
|
+
if (record.type === "text") return { type: "text", text: typeof record.text === "string" ? compactString(record.text, MAX_ASSISTANT_TEXT_CHARS) : "" };
|
|
147
|
+
if (record.type === "toolCall") return { type: "toolCall", name: record.name, input: compactValue(typeof record.input === "string" ? compactString(record.input, MAX_TOOL_INPUT_CHARS) : record.input) };
|
|
148
|
+
if (record.type === "toolResult") return { type: "toolResult", name: record.name, content: compactValue(typeof record.content === "string" ? compactString(record.content, MAX_TOOL_RESULT_CHARS) : record.content) };
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function compactChildPiEvent(event: unknown): unknown | undefined {
|
|
153
|
+
const record = asRecord(event);
|
|
154
|
+
if (!record) return undefined;
|
|
155
|
+
if (record.type === "message_update") return undefined;
|
|
156
|
+
if (record.type === "tool_execution_start" || record.type === "tool_execution_end") {
|
|
157
|
+
return { type: record.type, toolName: record.toolName, args: record.args };
|
|
158
|
+
}
|
|
159
|
+
if (record.type === "tool_result_end" || record.type === "message_end" || record.type === "message") {
|
|
160
|
+
const message = asRecord(record.message);
|
|
161
|
+
if (message?.role === "user" || message?.role === "system") return undefined;
|
|
162
|
+
const content = Array.isArray(message?.content) ? message.content.map(compactContentPart).filter((part) => part !== undefined) : undefined;
|
|
163
|
+
return {
|
|
164
|
+
type: record.type,
|
|
165
|
+
...(typeof record.text === "string" ? { text: record.text } : {}),
|
|
166
|
+
...(message ? { message: { role: message.role, ...(content ? { content } : {}), usage: message.usage, model: message.model, errorMessage: message.errorMessage, stopReason: message.stopReason } } : {}),
|
|
167
|
+
usage: record.usage,
|
|
168
|
+
model: record.model,
|
|
169
|
+
provider: record.provider,
|
|
170
|
+
stopReason: record.stopReason,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return record.type ? { type: record.type } : undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function displayTextFromCompactEvent(event: unknown): string | undefined {
|
|
177
|
+
const record = asRecord(event);
|
|
178
|
+
if (!record) return undefined;
|
|
179
|
+
if (record.type === "tool_execution_start") {
|
|
180
|
+
return typeof record.toolName === "string" ? `tool: ${record.toolName}` : "tool started";
|
|
181
|
+
}
|
|
182
|
+
if (record.type !== "message" && record.type !== "message_end") return undefined;
|
|
183
|
+
const message = asRecord(record.message);
|
|
184
|
+
if (message?.role !== undefined && message.role !== "assistant") return undefined;
|
|
185
|
+
const content = Array.isArray(message?.content) ? message.content : [];
|
|
186
|
+
const text = content.flatMap((part) => {
|
|
187
|
+
const item = asRecord(part);
|
|
188
|
+
return item?.type === "text" && typeof item.text === "string" ? [item.text] : [];
|
|
189
|
+
}).join("\n").trim();
|
|
190
|
+
return text || (typeof record.text === "string" ? record.text : undefined);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function compactChildPiLine(line: string): { persistedLine: string; event?: unknown; displayLine?: string; json: boolean } {
|
|
194
|
+
try {
|
|
195
|
+
const parsed = JSON.parse(line);
|
|
196
|
+
const compact = compactChildPiEvent(parsed);
|
|
197
|
+
return { json: true, event: compact, persistedLine: compact ? JSON.stringify(compact) : "", displayLine: displayTextFromCompactEvent(compact) };
|
|
198
|
+
} catch {
|
|
199
|
+
return { json: false, persistedLine: line, displayLine: line };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export class ChildPiLineObserver {
|
|
204
|
+
private buffer = "";
|
|
205
|
+
private readonly input: ChildPiRunInput;
|
|
206
|
+
|
|
207
|
+
constructor(input: ChildPiRunInput) {
|
|
208
|
+
this.input = input;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
observe(text: string): void {
|
|
212
|
+
this.buffer += text;
|
|
213
|
+
const lines = this.buffer.split(/\r?\n/);
|
|
214
|
+
this.buffer = lines.pop() ?? "";
|
|
215
|
+
for (const line of lines) this.emitLine(line);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
flush(): void {
|
|
219
|
+
if (!this.buffer) return;
|
|
220
|
+
const line = this.buffer;
|
|
221
|
+
this.buffer = "";
|
|
222
|
+
this.emitLine(line);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private emitLine(line: string): void {
|
|
226
|
+
if (!line.trim()) return;
|
|
227
|
+
const compact = compactChildPiLine(line);
|
|
228
|
+
if (compact.event !== undefined) {
|
|
229
|
+
try {
|
|
230
|
+
this.input.onJsonEvent?.(compact.event);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
logInternalError("child-pi.on-json-event", error, `line=${compact.persistedLine ?? compact.displayLine ?? ""}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (compact.persistedLine) appendTranscript(this.input, compact.persistedLine);
|
|
236
|
+
if (compact.displayLine?.trim()) {
|
|
237
|
+
try {
|
|
238
|
+
this.input.onStdoutLine?.(compact.displayLine);
|
|
239
|
+
} catch (error) {
|
|
240
|
+
logInternalError("child-pi.on-stdout-line", error, `line=${compact.displayLine}`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Mock-only path — real code path reuses a single observer. */
|
|
247
|
+
function observeStdoutChunk(input: ChildPiRunInput, text: string): void {
|
|
248
|
+
const observer = new ChildPiLineObserver(input);
|
|
249
|
+
observer.observe(text);
|
|
250
|
+
observer.flush();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
254
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function isFinalAssistantEvent(event: unknown): boolean {
|
|
258
|
+
const obj = asRecord(event);
|
|
259
|
+
if (!obj || obj.type !== "message_end") return false;
|
|
260
|
+
const message = asRecord(obj.message);
|
|
261
|
+
const role = message?.role;
|
|
262
|
+
if (role !== undefined && role !== "assistant") return false;
|
|
263
|
+
const stopReason = typeof message?.stopReason === "string" ? message.stopReason : typeof obj.stopReason === "string" ? obj.stopReason : undefined;
|
|
264
|
+
if (stopReason !== undefined && stopReason !== "stop") return false;
|
|
265
|
+
const content = Array.isArray(message?.content) ? message.content : [];
|
|
266
|
+
return !content.some((part) => asRecord(part)?.type === "toolCall");
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResult> {
|
|
270
|
+
const depth = checkCrewDepth(input.maxDepth);
|
|
271
|
+
if (depth.blocked) return { exitCode: 1, stdout: "", stderr: `pi-crew depth guard blocked child worker: depth ${depth.depth} >= max ${depth.maxDepth}` };
|
|
272
|
+
const mock = process.env.PI_TEAMS_MOCK_CHILD_PI;
|
|
273
|
+
if (mock) {
|
|
274
|
+
if (mock === "success") {
|
|
275
|
+
const stdout = `Mock child Pi success for ${input.agent.name}\n`;
|
|
276
|
+
observeStdoutChunk(input, stdout);
|
|
277
|
+
return { exitCode: 0, stdout, stderr: "" };
|
|
278
|
+
}
|
|
279
|
+
if (mock === "json-success" || mock === "adaptive-plan") {
|
|
280
|
+
const text = mock === "adaptive-plan" && input.task.includes("ADAPTIVE_PLAN_JSON_START")
|
|
281
|
+
? `Adaptive mock plan\nADAPTIVE_PLAN_JSON_START\n${JSON.stringify({ phases: [{ name: "research", tasks: [{ role: "explorer", task: "Explore adaptive target" }, { role: "analyst", task: "Analyze adaptive target" }, { role: "planner", task: "Plan adaptive target" }] }, { name: "build", tasks: [{ role: "executor", task: "Implement adaptive target" }] }, { name: "check", tasks: [{ role: "reviewer", task: "Review adaptive target" }, { role: "test-engineer", task: "Test adaptive target" }, { role: "writer", task: "Summarize adaptive target" }] }] })}\nADAPTIVE_PLAN_JSON_END`
|
|
282
|
+
: `Mock JSON success for ${input.agent.name}`;
|
|
283
|
+
const stdout = `${JSON.stringify({ type: "message", message: { role: "assistant", content: [{ type: "text", text }] } })}\n${JSON.stringify({ type: "message_end", usage: { input: 10, output: 5, cost: 0.001, turns: 1 } })}\n`;
|
|
284
|
+
observeStdoutChunk(input, stdout);
|
|
285
|
+
return { exitCode: 0, stdout, stderr: "" };
|
|
286
|
+
}
|
|
287
|
+
if (mock === "retryable-failure") return { exitCode: 1, stdout: "", stderr: "rate limit: mock failure" };
|
|
288
|
+
return { exitCode: 1, stdout: "", stderr: `mock failure: ${mock}` };
|
|
289
|
+
}
|
|
290
|
+
const built = buildPiWorkerArgs({ task: input.task, agent: input.agent, model: input.model, sessionEnabled: false, maxDepth: input.maxDepth, skillPaths: input.skillPaths });
|
|
291
|
+
const spawnSpec = getPiSpawnCommand(built.args);
|
|
292
|
+
try {
|
|
293
|
+
return await new Promise<ChildPiRunResult>((resolve) => {
|
|
294
|
+
const child = spawn(spawnSpec.command, spawnSpec.args, buildChildPiSpawnOptions(input.cwd, { ...process.env, ...built.env }));
|
|
295
|
+
if (child.pid) {
|
|
296
|
+
activeChildProcesses.set(child.pid, child);
|
|
297
|
+
input.onSpawn?.(child.pid);
|
|
298
|
+
}
|
|
299
|
+
let stdout = "";
|
|
300
|
+
let stderr = "";
|
|
301
|
+
let settled = false;
|
|
302
|
+
let childExited = false;
|
|
303
|
+
let postExitGuardCleanup: (() => void) | undefined;
|
|
304
|
+
let finalDrainTimer: NodeJS.Timeout | undefined;
|
|
305
|
+
let hardKillTimer: NodeJS.Timeout | undefined;
|
|
306
|
+
let noResponseTimer: NodeJS.Timeout | undefined;
|
|
307
|
+
const finalDrainMs = input.finalDrainMs ?? FINAL_DRAIN_MS;
|
|
308
|
+
const hardKillMs = input.hardKillMs ?? HARD_KILL_MS;
|
|
309
|
+
const responseTimeoutEnv = Number.parseInt(process.env.PI_TEAMS_CHILD_RESPONSE_TIMEOUT_MS ?? "", 10);
|
|
310
|
+
const responseTimeoutMs = Number.isFinite(responseTimeoutEnv) && responseTimeoutEnv >= 0 ? responseTimeoutEnv : input.responseTimeoutMs ?? RESPONSE_TIMEOUT_MS;
|
|
311
|
+
let responseTimeoutHit = false;
|
|
312
|
+
let forcedFinalDrain = false;
|
|
313
|
+
let abortRequested = input.signal?.aborted === true;
|
|
314
|
+
let hardKilled = false;
|
|
315
|
+
const cleanupErrors: string[] = [];
|
|
316
|
+
const restartNoResponseTimer = (): void => {
|
|
317
|
+
if (responseTimeoutMs <= 0) return;
|
|
318
|
+
if (noResponseTimer) clearTimeout(noResponseTimer);
|
|
319
|
+
noResponseTimer = setTimeout(() => {
|
|
320
|
+
responseTimeoutHit = true;
|
|
321
|
+
killProcessTree(child.pid, child);
|
|
322
|
+
try {
|
|
323
|
+
child.kill(process.platform === "win32" ? undefined : "SIGTERM");
|
|
324
|
+
} catch (error) {
|
|
325
|
+
logInternalError("child-pi.response-timeout-term", error, `pid=${child.pid}`);
|
|
326
|
+
}
|
|
327
|
+
}, responseTimeoutMs);
|
|
328
|
+
noResponseTimer.unref();
|
|
329
|
+
};
|
|
330
|
+
const clearNoResponseTimer = (): void => {
|
|
331
|
+
if (noResponseTimer) clearTimeout(noResponseTimer);
|
|
332
|
+
noResponseTimer = undefined;
|
|
333
|
+
};
|
|
334
|
+
restartNoResponseTimer();
|
|
335
|
+
const lineObserver = new ChildPiLineObserver({
|
|
336
|
+
...input,
|
|
337
|
+
onStdoutLine: (line) => {
|
|
338
|
+
restartNoResponseTimer();
|
|
339
|
+
stdout = appendBoundedTail(stdout, `${line}\n`);
|
|
340
|
+
input.onStdoutLine?.(line);
|
|
341
|
+
},
|
|
342
|
+
onJsonEvent: (event) => {
|
|
343
|
+
restartNoResponseTimer();
|
|
344
|
+
input.onJsonEvent?.(event);
|
|
345
|
+
if (!isFinalAssistantEvent(event) || childExited || settled || finalDrainTimer) return;
|
|
346
|
+
finalDrainTimer = setTimeout(() => {
|
|
347
|
+
if (settled || childExited) return;
|
|
348
|
+
forcedFinalDrain = true;
|
|
349
|
+
try {
|
|
350
|
+
child.kill(process.platform === "win32" ? undefined : "SIGTERM");
|
|
351
|
+
} catch (error) {
|
|
352
|
+
logInternalError("child-pi.final-drain-term", error, `pid=${child.pid}`);
|
|
353
|
+
}
|
|
354
|
+
hardKillTimer = setTimeout(() => {
|
|
355
|
+
if (settled || childExited) return;
|
|
356
|
+
try {
|
|
357
|
+
hardKilled = true;
|
|
358
|
+
child.kill(process.platform === "win32" ? undefined : "SIGKILL");
|
|
359
|
+
} catch (error) {
|
|
360
|
+
logInternalError("child-pi.final-drain-kill", error, `pid=${child.pid}`);
|
|
361
|
+
}
|
|
362
|
+
}, hardKillMs);
|
|
363
|
+
hardKillTimer.unref();
|
|
364
|
+
}, finalDrainMs);
|
|
365
|
+
finalDrainTimer.unref();
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
const clearFinalDrainTimers = (): void => {
|
|
370
|
+
if (finalDrainTimer) clearTimeout(finalDrainTimer);
|
|
371
|
+
if (hardKillTimer) clearTimeout(hardKillTimer);
|
|
372
|
+
finalDrainTimer = undefined;
|
|
373
|
+
hardKillTimer = undefined;
|
|
374
|
+
};
|
|
375
|
+
const clearPostExitGuard = (): void => {
|
|
376
|
+
if (postExitGuardCleanup) {
|
|
377
|
+
postExitGuardCleanup();
|
|
378
|
+
postExitGuardCleanup = undefined;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
const clearChildPiTimeouts = (): void => {
|
|
382
|
+
clearNoResponseTimer();
|
|
383
|
+
clearFinalDrainTimers();
|
|
384
|
+
clearPostExitGuard();
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const settle = (result: ChildPiRunResult): void => {
|
|
388
|
+
if (settled) return;
|
|
389
|
+
settled = true;
|
|
390
|
+
clearChildPiTimeouts();
|
|
391
|
+
lineObserver.flush();
|
|
392
|
+
input.signal?.removeEventListener("abort", abort);
|
|
393
|
+
try {
|
|
394
|
+
cleanupTempDir(built.tempDir);
|
|
395
|
+
} catch (error) {
|
|
396
|
+
cleanupErrors.push(error instanceof Error ? error.message : String(error));
|
|
397
|
+
}
|
|
398
|
+
resolve({ ...result, exitStatus: result.exitStatus ?? { exitCode: result.exitCode, cancelled: abortRequested, timedOut: responseTimeoutHit, killed: hardKilled, cleanupErrors, finalDrainMs } });
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
const abort = (): void => {
|
|
402
|
+
abortRequested = true;
|
|
403
|
+
killProcessTree(child.pid, child);
|
|
404
|
+
if (process.platform !== "win32") {
|
|
405
|
+
trySignalChild(child, "SIGTERM");
|
|
406
|
+
}
|
|
407
|
+
try {
|
|
408
|
+
child.kill(process.platform === "win32" ? undefined : "SIGTERM");
|
|
409
|
+
} catch {
|
|
410
|
+
// Ignore kill races.
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
input.signal?.addEventListener("abort", abort, { once: true });
|
|
415
|
+
child.stdout?.on("data", (chunk: Buffer) => {
|
|
416
|
+
restartNoResponseTimer();
|
|
417
|
+
lineObserver.observe(chunk.toString("utf-8"));
|
|
418
|
+
});
|
|
419
|
+
child.stderr?.on("data", (chunk: Buffer) => {
|
|
420
|
+
restartNoResponseTimer();
|
|
421
|
+
stderr = appendBoundedTail(stderr, chunk.toString("utf-8"));
|
|
422
|
+
});
|
|
423
|
+
child.on("error", (error) => {
|
|
424
|
+
settle({ exitCode: null, stdout, stderr, error: error.message });
|
|
425
|
+
});
|
|
426
|
+
child.on("exit", () => {
|
|
427
|
+
if (child.pid) {
|
|
428
|
+
activeChildProcesses.delete(child.pid);
|
|
429
|
+
clearHardKillTimer(child.pid);
|
|
430
|
+
}
|
|
431
|
+
childExited = true;
|
|
432
|
+
clearNoResponseTimer();
|
|
433
|
+
clearFinalDrainTimers();
|
|
434
|
+
if (!postExitGuardCleanup) {
|
|
435
|
+
postExitGuardCleanup = attachPostExitStdioGuard(child, {
|
|
436
|
+
idleMs: POST_EXIT_STDIO_GUARD_MS,
|
|
437
|
+
hardMs: HARD_KILL_MS,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
child.on("close", (exitCode) => {
|
|
442
|
+
if (child.pid) {
|
|
443
|
+
activeChildProcesses.delete(child.pid);
|
|
444
|
+
clearHardKillTimer(child.pid);
|
|
445
|
+
}
|
|
446
|
+
const timeoutError = responseTimeoutHit && !stderr.trim() ? { error: `Child Pi produced no new output for ${responseTimeoutMs}ms; process was terminated as unresponsive.` } : undefined;
|
|
447
|
+
const finalExitCode = forcedFinalDrain && !timeoutError ? 0 : exitCode;
|
|
448
|
+
// A final assistant event is the child Pi contract for "the worker produced its answer".
|
|
449
|
+
// Some Pi processes can linger during post-final cleanup/stdio shutdown; finalDrain terminates
|
|
450
|
+
// that lingering process so the parent can continue, but it must not turn a completed
|
|
451
|
+
// subagent answer into a failed task. Real pre-final response timeouts still report errors.
|
|
452
|
+
settle({ exitCode: finalExitCode, stdout, stderr, ...(timeoutError ? { error: timeoutError.error } : {}), exitStatus: { exitCode: finalExitCode, cancelled: abortRequested, timedOut: responseTimeoutHit, killed: hardKilled, cleanupErrors, finalDrainMs } });
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
} finally {
|
|
456
|
+
cleanupTempDir(built.tempDir);
|
|
457
|
+
}
|
|
458
|
+
}
|