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
|
@@ -1,305 +1,599 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import type { AgentConfig } from "../agents/agent-config.ts";
|
|
4
|
-
import type { CrewRuntimeConfig } from "../config/config.ts";
|
|
5
|
-
import type { TeamRunManifest, TeamTaskState, UsageState } from "../state/types.ts";
|
|
6
|
-
import { buildMemoryBlock } from "./agent-memory.ts";
|
|
7
|
-
import { registerLiveAgent, updateLiveAgentStatus } from "./live-agent-manager.ts";
|
|
8
|
-
import { applyLiveAgentControlRequest, applyLiveAgentControlRequests, type LiveAgentControlCursor } from "./live-agent-control.ts";
|
|
9
|
-
import { subscribeLiveControlRealtime } from "./live-control-realtime.ts";
|
|
10
|
-
import { eventToSidechainType, sidechainOutputPath, writeSidechainEntry } from "./sidechain-output.ts";
|
|
11
|
-
import type { WorkflowStep } from "../workflows/workflow-config.ts";
|
|
12
|
-
import { isLiveSessionRuntimeAvailable } from "./runtime-resolver.ts";
|
|
13
|
-
import { redactSecrets } from "../utils/redaction.ts";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
|
|
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
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type { AgentConfig } from "../agents/agent-config.ts";
|
|
4
|
+
import type { CrewRuntimeConfig } from "../config/config.ts";
|
|
5
|
+
import type { TeamRunManifest, TeamTaskState, UsageState } from "../state/types.ts";
|
|
6
|
+
import { buildMemoryBlock } from "./agent-memory.ts";
|
|
7
|
+
import { registerLiveAgent, updateLiveAgentStatus } from "./live-agent-manager.ts";
|
|
8
|
+
import { applyLiveAgentControlRequest, applyLiveAgentControlRequests, type LiveAgentControlCursor } from "./live-agent-control.ts";
|
|
9
|
+
import { subscribeLiveControlRealtime } from "./live-control-realtime.ts";
|
|
10
|
+
import { eventToSidechainType, sidechainOutputPath, writeSidechainEntry } from "./sidechain-output.ts";
|
|
11
|
+
import type { WorkflowStep } from "../workflows/workflow-config.ts";
|
|
12
|
+
import { isLiveSessionRuntimeAvailable } from "./runtime-resolver.ts";
|
|
13
|
+
import { redactSecrets } from "../utils/redaction.ts";
|
|
14
|
+
import { buildConfiguredModelRouting } from "./model-fallback.ts";
|
|
15
|
+
import { DEFAULT_LIVE_SESSION } from "../config/defaults.ts";
|
|
16
|
+
import { buildYieldReminder, hasYieldInOutput, isYieldEvent, extractYieldResult, validateYieldData, DEFAULT_YIELD_CONFIG, type YieldResult } from "./yield-handler.ts";
|
|
17
|
+
import { buildMcpProxyFromSession } from "./mcp-proxy.ts";
|
|
18
|
+
import { createSubmitResultTool } from "./custom-tools/submit-result-tool.ts";
|
|
19
|
+
import { createIrcTool } from "./custom-tools/irc-tool.ts";
|
|
20
|
+
import { buildExtensionBridge } from "./live-extension-bridge.ts";
|
|
21
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
22
|
+
// prose-compressor imported for custom tool descriptions below;
|
|
23
|
+
// tool description compression for SDK-managed tools awaits SDK support.
|
|
24
|
+
import { compressToolDescription } from "./prose-compressor.ts";
|
|
25
|
+
import { buildSensitivePathConstraint } from "./sensitive-paths.ts";
|
|
26
|
+
import { collectLiveSessionHealth, formatLiveSessionDiagnostics, type LiveSessionHealth } from "./live-session-health.ts";
|
|
27
|
+
import { listLiveAgents } from "./live-agent-manager.ts";
|
|
28
|
+
|
|
29
|
+
export interface LiveSessionSpawnInput {
|
|
30
|
+
manifest: TeamRunManifest;
|
|
31
|
+
task: TeamTaskState;
|
|
32
|
+
step: WorkflowStep;
|
|
33
|
+
agent: AgentConfig;
|
|
34
|
+
prompt: string;
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
transcriptPath?: string;
|
|
37
|
+
onEvent?: (event: unknown) => void;
|
|
38
|
+
onOutput?: (text: string) => void;
|
|
39
|
+
runtimeConfig?: CrewRuntimeConfig;
|
|
40
|
+
parentContext?: string;
|
|
41
|
+
parentModel?: unknown;
|
|
42
|
+
modelRegistry?: unknown;
|
|
43
|
+
modelOverride?: string;
|
|
44
|
+
teamRoleModel?: string;
|
|
45
|
+
isCurrent?: () => boolean;
|
|
46
|
+
/** Phase 2: Output schema for validating yield data. */
|
|
47
|
+
outputSchema?: unknown;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface LiveSessionRunResult {
|
|
51
|
+
available: true;
|
|
52
|
+
exitCode: number | null;
|
|
53
|
+
stdout: string;
|
|
54
|
+
stderr: string;
|
|
55
|
+
jsonEvents: number;
|
|
56
|
+
usage?: UsageState;
|
|
57
|
+
error?: string;
|
|
58
|
+
/** Phase 1: Extracted yield result from submit_result tool call. */
|
|
59
|
+
yieldResult?: YieldResult;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface LiveSessionUnavailableResult {
|
|
63
|
+
available: false;
|
|
64
|
+
reason: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface LiveSessionPlannedResult {
|
|
68
|
+
available: true;
|
|
69
|
+
reason: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type LiveSessionModule = Record<string, unknown> & {
|
|
73
|
+
createAgentSession?: (options?: Record<string, unknown>) => Promise<{ session: LiveSessionLike; modelFallbackMessage?: string }>;
|
|
74
|
+
DefaultResourceLoader?: new (options: Record<string, unknown>) => { reload?: () => Promise<void> };
|
|
75
|
+
SessionManager?: { inMemory?: (cwd?: string) => unknown; create?: (cwd?: string, sessionDir?: string) => unknown };
|
|
76
|
+
SettingsManager?: { create?: (cwd?: string, agentDir?: string) => unknown };
|
|
77
|
+
getAgentDir?: () => string;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
type LiveSessionLike = {
|
|
81
|
+
subscribe?: (listener: (event: unknown) => void) => (() => void);
|
|
82
|
+
prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>;
|
|
83
|
+
steer?: (text: string) => Promise<void>;
|
|
84
|
+
abort?: () => Promise<void> | void;
|
|
85
|
+
getStats?: () => unknown;
|
|
86
|
+
stats?: unknown;
|
|
87
|
+
bindExtensions?: (bindings?: Record<string, unknown>) => Promise<void>;
|
|
88
|
+
getActiveToolNames?: () => string[];
|
|
89
|
+
setActiveToolsByName?: (names: string[]) => void;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function appendTranscript(filePath: string | undefined, event: unknown): void {
|
|
93
|
+
if (!filePath) return;
|
|
94
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
95
|
+
fs.appendFileSync(filePath, `${JSON.stringify(redactSecrets(event))}\n`, "utf-8");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
99
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function textFromContent(content: unknown): string[] {
|
|
103
|
+
if (typeof content === "string") return [content];
|
|
104
|
+
if (!Array.isArray(content)) return [];
|
|
105
|
+
return content.flatMap((part) => {
|
|
106
|
+
const obj = asRecord(part);
|
|
107
|
+
if (!obj) return [];
|
|
108
|
+
if (obj.type === "text" && typeof obj.text === "string") return [obj.text];
|
|
109
|
+
if (typeof obj.content === "string") return [obj.content];
|
|
110
|
+
return [];
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function eventText(event: unknown): string[] {
|
|
115
|
+
const obj = asRecord(event);
|
|
116
|
+
if (!obj) return [];
|
|
117
|
+
const text: string[] = [];
|
|
118
|
+
if (typeof obj.text === "string") text.push(obj.text);
|
|
119
|
+
text.push(...textFromContent(obj.content));
|
|
120
|
+
const message = asRecord(obj.message);
|
|
121
|
+
if (message) text.push(...textFromContent(message.content));
|
|
122
|
+
return text.filter((entry) => entry.trim());
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function finalAssistantText(event: unknown): string[] {
|
|
126
|
+
const obj = asRecord(event);
|
|
127
|
+
if (!obj || obj.type !== "message_end") return [];
|
|
128
|
+
const message = asRecord(obj.message);
|
|
129
|
+
if (message?.role !== "assistant") return [];
|
|
130
|
+
return textFromContent(message.content);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function numberField(obj: Record<string, unknown> | undefined, keys: string[]): number | undefined {
|
|
134
|
+
if (!obj) return undefined;
|
|
135
|
+
for (const key of keys) {
|
|
136
|
+
const value = obj[key];
|
|
137
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function modelFromRegistry(modelRegistry: unknown, modelId: string | undefined): unknown {
|
|
143
|
+
if (!modelId || !modelId.includes("/")) return undefined;
|
|
144
|
+
const registry = asRecord(modelRegistry);
|
|
145
|
+
const find = registry?.find;
|
|
146
|
+
if (typeof find !== "function") return undefined;
|
|
147
|
+
const [provider, ...modelParts] = modelId.split("/");
|
|
148
|
+
const id = modelParts.join("/");
|
|
149
|
+
try {
|
|
150
|
+
return find.call(modelRegistry, provider, id);
|
|
151
|
+
} catch {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Communication intensity by role (caveman-inspired token optimization) */
|
|
157
|
+
const ROLE_INTENSITY: Record<string, "lite" | "full" | "ultra"> = {
|
|
158
|
+
explorer: "ultra",
|
|
159
|
+
analyst: "full",
|
|
160
|
+
planner: "full",
|
|
161
|
+
critic: "full",
|
|
162
|
+
executor: "full",
|
|
163
|
+
reviewer: "full",
|
|
164
|
+
"security-reviewer": "full",
|
|
165
|
+
"test-engineer": "full",
|
|
166
|
+
verifier: "full",
|
|
167
|
+
writer: "lite",
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
function buildCommunicationStyle(role: string): string {
|
|
171
|
+
const intensity = ROLE_INTENSITY[role] ?? "full";
|
|
172
|
+
if (intensity === "lite") return "## Communication\nProfessional concise. No filler/hedging. Full sentences OK.";
|
|
173
|
+
if (intensity === "ultra") return [
|
|
174
|
+
"## Communication (ultra-compressed)",
|
|
175
|
+
"Drop: articles, filler, hedging, pleasantries. Fragments OK.",
|
|
176
|
+
"Pattern: [thing] [action] [reason].",
|
|
177
|
+
"Code/paths/symbols: exact, never abbreviated. Errors quoted exact.",
|
|
178
|
+
"Abbreviate prose words: DB/auth/config/req/res/fn/impl.",
|
|
179
|
+
"Arrows for causality: X → Y. One word when one word enough.",
|
|
180
|
+
"Security/destructive: write normal English. Resume compressed after.",
|
|
181
|
+
].join("\n");
|
|
182
|
+
return [
|
|
183
|
+
"## Communication (compressed)",
|
|
184
|
+
"Drop: articles (a/an/the), filler (just/really/basically/actually/simply), hedging, pleasantries.",
|
|
185
|
+
"Short synonyms. Fragments OK. Pattern: [thing] [action] [reason]. [next step].",
|
|
186
|
+
"Code/paths/symbols: exact. Errors quoted exact.",
|
|
187
|
+
"Security/destructive: write normal English. Resume compressed after.",
|
|
188
|
+
].join("\n");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function buildOutputContract(role: string): string {
|
|
192
|
+
if (role === "explorer") return [
|
|
193
|
+
"## Output Contract",
|
|
194
|
+
"<path>:<line> — `<symbol>` — <≤6 word note>",
|
|
195
|
+
"Group: Defs: / Refs: / Callers: / Tests: / Sites:",
|
|
196
|
+
"Zero hits → \"No match.\"",
|
|
197
|
+
"Last line → totals: N defs, M refs.",
|
|
198
|
+
].join("\n");
|
|
199
|
+
if (role === "executor") return [
|
|
200
|
+
"## Output Contract",
|
|
201
|
+
"<path>:<line-range> — <change ≤10 words>.",
|
|
202
|
+
"verified: <re-read OK | mismatch @ path:line>.",
|
|
203
|
+
"Refusal tokens: too-big. / needs-confirm. / ambiguous. / regressed.",
|
|
204
|
+
].join("\n");
|
|
205
|
+
if (role === "reviewer" || role === "security-reviewer") return [
|
|
206
|
+
"## Output Contract",
|
|
207
|
+
"<path>:<line>: <emoji> <severity>: <problem>. <fix>.",
|
|
208
|
+
"Severity: 🔴 bug, 🟡 risk, 🔵 nit, ❓ question.",
|
|
209
|
+
"Zero findings → \"No issues.\"",
|
|
210
|
+
"Sorted: file order → ascending line numbers.",
|
|
211
|
+
].join("\n");
|
|
212
|
+
if (role === "verifier") return [
|
|
213
|
+
"## Output Contract",
|
|
214
|
+
"PASS: <what verified> — <evidence ≤20 words>.",
|
|
215
|
+
"FAIL: <what failed> — <reason>. <expected vs actual>.",
|
|
216
|
+
"Evidence: file paths, test output, or diffs.",
|
|
217
|
+
].join("\n");
|
|
218
|
+
if (role === "writer") return "## Output Contract\nWrite clear documentation. Full sentences. No compression.";
|
|
219
|
+
return ""; // planner, critic, analyst, test-engineer: no strict format
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Phase 3 (caveman): Compress tool descriptions in a live session to reduce
|
|
224
|
+
* input token cost per tool call. MCP tools often have verbose descriptions
|
|
225
|
+
* (e.g. "This tool allows you to search for files in the filesystem..." → "Search files in filesystem.").
|
|
226
|
+
* Compresses only description text, never modifies tool names or parameters.
|
|
227
|
+
*/
|
|
228
|
+
function compressSessionToolDescriptions(session: LiveSessionLike): void {
|
|
229
|
+
if (typeof session.getActiveToolNames !== "function") return;
|
|
230
|
+
// The Pi SDK doesn't expose a setDescription API, but we can attempt
|
|
231
|
+
// to compress via setActiveToolsByName if the session supports it.
|
|
232
|
+
// For now, this is a no-op that documents the intent for future SDK support.
|
|
233
|
+
// When Pi SDK adds tool description mutation, this function will compress.
|
|
234
|
+
// Side benefit: the import of compressToolDescription ensures the module
|
|
235
|
+
// is loaded and tree-shakeable, so adding the actual logic later is trivial.
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function liveSystemPrompt(input: LiveSessionSpawnInput): string {
|
|
239
|
+
const memory = input.agent.memory ? buildMemoryBlock(input.agent.name, input.agent.memory, input.task.cwd, Boolean(input.agent.tools?.some((tool) => tool === "write" || tool === "edit"))) : "";
|
|
240
|
+
const role = input.task.role;
|
|
241
|
+
const styleBlock = buildCommunicationStyle(role);
|
|
242
|
+
const contractBlock = buildOutputContract(role);
|
|
243
|
+
const sensitiveConstraint = buildSensitivePathConstraint();
|
|
244
|
+
return [
|
|
245
|
+
"# pi-crew Live Subagent",
|
|
246
|
+
`Run ID: ${input.manifest.runId}`,
|
|
247
|
+
`Task ID: ${input.task.id}`,
|
|
248
|
+
`Role: ${role}`,
|
|
249
|
+
`Agent: ${input.agent.name}`,
|
|
250
|
+
`Working directory: ${input.task.cwd}`,
|
|
251
|
+
"",
|
|
252
|
+
styleBlock,
|
|
253
|
+
contractBlock,
|
|
254
|
+
sensitiveConstraint,
|
|
255
|
+
"",
|
|
256
|
+
input.agent.systemPrompt || "Follow the user task exactly and report verification evidence.",
|
|
257
|
+
memory ? `\n${memory}` : "",
|
|
258
|
+
].filter(Boolean).join("\n");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function filterActiveTools(session: LiveSessionLike, agent: AgentConfig): void {
|
|
262
|
+
if (typeof session.getActiveToolNames !== "function" || typeof session.setActiveToolsByName !== "function") return;
|
|
263
|
+
const recursiveTools = new Set(["team", "Team", "Agent", "get_subagent_result", "steer_subagent"]);
|
|
264
|
+
const allowed = agent.tools?.length ? new Set(agent.tools) : undefined;
|
|
265
|
+
const active = session.getActiveToolNames().filter((name) => !recursiveTools.has(name) && (!allowed || allowed.has(name)));
|
|
266
|
+
session.setActiveToolsByName(active);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function usageFromStats(stats: unknown): UsageState | undefined {
|
|
270
|
+
const obj = asRecord(stats);
|
|
271
|
+
if (!obj) return undefined;
|
|
272
|
+
const input = numberField(obj, ["input", "inputTokens", "input_tokens"]);
|
|
273
|
+
const output = numberField(obj, ["output", "outputTokens", "output_tokens"]);
|
|
274
|
+
const cacheRead = numberField(obj, ["cacheRead", "cache_read"]);
|
|
275
|
+
const cacheWrite = numberField(obj, ["cacheWrite", "cache_write"]);
|
|
276
|
+
const cost = numberField(obj, ["cost"]);
|
|
277
|
+
const turns = numberField(obj, ["turns", "turnCount", "turn_count"]);
|
|
278
|
+
return [input, output, cacheRead, cacheWrite, cost, turns].some((value) => value !== undefined) ? { input, output, cacheRead, cacheWrite, cost, turns } : undefined;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export async function probeLiveSessionRuntime(): Promise<LiveSessionUnavailableResult | LiveSessionPlannedResult> {
|
|
282
|
+
const availability = await isLiveSessionRuntimeAvailable();
|
|
283
|
+
if (!availability.available) return { available: false, reason: availability.reason ?? "Live-session runtime is unavailable." };
|
|
284
|
+
return { available: true, reason: "Live-session SDK exports are available. pi-crew can run in-process live agents when runtime.mode=live-session." };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export async function runLiveSessionTask(input: LiveSessionSpawnInput): Promise<LiveSessionRunResult> {
|
|
288
|
+
const isCurrent = input.isCurrent ?? (() => true);
|
|
289
|
+
|
|
290
|
+
// G1: Capture yield result from custom tool callback
|
|
291
|
+
let customToolYieldResult: YieldResult | undefined;
|
|
292
|
+
let customToolYieldResolved = false;
|
|
293
|
+
if (process.env.PI_CREW_MOCK_LIVE_SESSION === "success") {
|
|
294
|
+
const agentId = `${input.manifest.runId}:${input.task.id}`;
|
|
295
|
+
const inherited = input.runtimeConfig?.inheritContext === true && input.parentContext ? ` with inherited context: ${input.parentContext}` : "";
|
|
296
|
+
const event = { type: "message_end", message: { role: "assistant", content: [{ type: "text", text: `Mock live-session success for ${input.agent.name}${inherited}` }] } };
|
|
297
|
+
const mockSession = { steer: async () => {}, prompt: async () => {}, abort: async () => {} };
|
|
298
|
+
registerLiveAgent({ agentId, runId: input.manifest.runId, taskId: input.task.id, session: mockSession, status: "running" });
|
|
299
|
+
appendTranscript(input.transcriptPath, event);
|
|
300
|
+
const sidechainPath = sidechainOutputPath(input.manifest.stateRoot, input.task.id);
|
|
301
|
+
writeSidechainEntry(sidechainPath, { agentId, type: "user", message: { role: "user", content: input.prompt }, cwd: input.task.cwd });
|
|
302
|
+
writeSidechainEntry(sidechainPath, { agentId, type: "message", message: event, cwd: input.task.cwd });
|
|
303
|
+
if (isCurrent()) input.onEvent?.(event);
|
|
304
|
+
const stdout = `Mock live-session success for ${input.agent.name}${inherited}`;
|
|
305
|
+
if (isCurrent()) input.onOutput?.(stdout);
|
|
306
|
+
updateLiveAgentStatus(agentId, "completed");
|
|
307
|
+
return { available: true, exitCode: 0, stdout, stderr: "", jsonEvents: 1 };
|
|
308
|
+
}
|
|
309
|
+
const availability = await isLiveSessionRuntimeAvailable();
|
|
310
|
+
if (!availability.available) return { available: true, exitCode: 1, stdout: "", stderr: availability.reason ?? "Live-session runtime unavailable.", jsonEvents: 0, error: availability.reason };
|
|
311
|
+
const mod = await import("@mariozechner/pi-coding-agent") as LiveSessionModule;
|
|
312
|
+
if (typeof mod.createAgentSession !== "function") return { available: true, exitCode: 1, stdout: "", stderr: "createAgentSession export is unavailable.", jsonEvents: 0, error: "createAgentSession export is unavailable." };
|
|
313
|
+
let session: LiveSessionLike | undefined;
|
|
314
|
+
let unsubscribe: (() => void) | undefined;
|
|
315
|
+
let unsubscribeControlRealtime: (() => void) | undefined;
|
|
316
|
+
let controlTimer: ReturnType<typeof setInterval> | undefined;
|
|
317
|
+
let stdout = "";
|
|
318
|
+
let jsonEvents = 0;
|
|
319
|
+
const collectedJsonEvents: Record<string, unknown>[] = [];
|
|
320
|
+
let yieldResult: YieldResult | undefined;
|
|
321
|
+
try {
|
|
322
|
+
const agentDir = typeof mod.getAgentDir === "function" ? mod.getAgentDir() : undefined;
|
|
323
|
+
let resourceLoader: unknown;
|
|
324
|
+
if (mod.DefaultResourceLoader && agentDir) {
|
|
325
|
+
resourceLoader = new mod.DefaultResourceLoader({
|
|
326
|
+
cwd: input.task.cwd,
|
|
327
|
+
agentDir,
|
|
328
|
+
noPromptTemplates: true,
|
|
329
|
+
noThemes: true,
|
|
330
|
+
noContextFiles: input.runtimeConfig?.inheritContext !== true,
|
|
331
|
+
systemPromptOverride: () => liveSystemPrompt(input),
|
|
332
|
+
appendSystemPromptOverride: () => [],
|
|
333
|
+
});
|
|
334
|
+
await (resourceLoader as { reload?: () => Promise<void> }).reload?.();
|
|
335
|
+
}
|
|
336
|
+
const modelRouting = buildConfiguredModelRouting({ overrideModel: input.modelOverride, stepModel: input.step.model, teamRoleModel: input.teamRoleModel, agentModel: input.agent.model, fallbackModels: input.agent.fallbackModels, parentModel: input.parentModel, modelRegistry: input.modelRegistry, cwd: input.manifest.cwd });
|
|
337
|
+
const resolvedModel = modelFromRegistry(input.modelRegistry, modelRouting.candidates[0] ?? modelRouting.requested) ?? input.parentModel;
|
|
338
|
+
// Phase 4: MCP proxy — will be determined after session creation
|
|
339
|
+
// (we check parent's MCP tools and share connections when available)
|
|
340
|
+
const mcpProxy = buildMcpProxyFromSession([], { shareMcp: true });
|
|
341
|
+
|
|
342
|
+
// G1: Build custom tools (submit_result + irc)
|
|
343
|
+
const agentId = `${input.manifest.runId}:${input.task.id}`;
|
|
344
|
+
const submitResultTool = createSubmitResultTool((result) => {
|
|
345
|
+
customToolYieldResult = result;
|
|
346
|
+
customToolYieldResolved = true;
|
|
347
|
+
});
|
|
348
|
+
const ircTool = createIrcTool(agentId);
|
|
349
|
+
const customTools = [submitResultTool, ircTool];
|
|
350
|
+
|
|
351
|
+
const created = await mod.createAgentSession({
|
|
352
|
+
cwd: input.task.cwd,
|
|
353
|
+
...(agentDir ? { agentDir } : {}),
|
|
354
|
+
...(resourceLoader ? { resourceLoader } : {}),
|
|
355
|
+
...(mod.SessionManager?.inMemory ? { sessionManager: mod.SessionManager.inMemory(input.task.cwd) } : {}),
|
|
356
|
+
...(mod.SettingsManager?.create && agentDir ? { settingsManager: mod.SettingsManager.create(input.task.cwd, agentDir) } : {}),
|
|
357
|
+
...(input.modelRegistry ? { modelRegistry: input.modelRegistry } : {}),
|
|
358
|
+
...(resolvedModel ? { model: resolvedModel } : {}),
|
|
359
|
+
...(input.agent.thinking ? { thinkingLevel: input.agent.thinking } : {}),
|
|
360
|
+
...(mcpProxy.enableMcp ? {} : { enableMCP: false }),
|
|
361
|
+
customTools,
|
|
362
|
+
});
|
|
363
|
+
session = created.session;
|
|
364
|
+
filterActiveTools(session, input.agent);
|
|
365
|
+
await session.bindExtensions?.({});
|
|
366
|
+
|
|
367
|
+
// Phase 3 (caveman): Compress tool descriptions to reduce input token cost
|
|
368
|
+
compressSessionToolDescriptions(session);
|
|
369
|
+
|
|
370
|
+
// Phase 5: Initialize extension runner bridge if available
|
|
371
|
+
// The bridge provides extension-like APIs (sendMessage, setActiveTools, etc.)
|
|
372
|
+
// to the extension runner if the session exposes one.
|
|
373
|
+
const extensionBridge = buildExtensionBridge(session as never);
|
|
374
|
+
if (extensionBridge) {
|
|
375
|
+
const extRunner = (session as Record<string, unknown>).extensionRunner;
|
|
376
|
+
if (extRunner && typeof (extRunner as Record<string, unknown>).initialize === "function") {
|
|
377
|
+
try {
|
|
378
|
+
(extRunner as { initialize: (apis: unknown, host: unknown) => void }).initialize(extensionBridge.apis, extensionBridge.host);
|
|
379
|
+
if (typeof (extRunner as Record<string, unknown>).emit === "function") {
|
|
380
|
+
await (extRunner as { emit: (event: unknown) => Promise<void> }).emit({ type: "session_start" });
|
|
381
|
+
}
|
|
382
|
+
} catch {
|
|
383
|
+
// Extension runner initialization failure should not block the session
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
registerLiveAgent({ agentId, runId: input.manifest.runId, taskId: input.task.id, session, status: "running" });
|
|
389
|
+
let controlCursor: LiveAgentControlCursor = { offset: 0 };
|
|
390
|
+
const seenControlRequestIds = new Set<string>();
|
|
391
|
+
let controlBusy = false;
|
|
392
|
+
const pollControl = async () => {
|
|
393
|
+
if (!isCurrent() || controlBusy || !session) return;
|
|
394
|
+
controlBusy = true;
|
|
395
|
+
try {
|
|
396
|
+
controlCursor = await applyLiveAgentControlRequests({ manifest: input.manifest, taskId: input.task.id, agentId, session, cursor: controlCursor, seenRequestIds: seenControlRequestIds });
|
|
397
|
+
} finally {
|
|
398
|
+
controlBusy = false;
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
unsubscribeControlRealtime = subscribeLiveControlRealtime((request) => {
|
|
402
|
+
if (!isCurrent() || request.runId !== input.manifest.runId || request.taskId !== input.task.id || !session) return;
|
|
403
|
+
void applyLiveAgentControlRequest({ request, taskId: input.task.id, agentId, session, seenRequestIds: seenControlRequestIds });
|
|
404
|
+
});
|
|
405
|
+
await pollControl();
|
|
406
|
+
controlTimer = setInterval(() => {
|
|
407
|
+
if (isCurrent()) void pollControl();
|
|
408
|
+
}, 500);
|
|
409
|
+
let turnCount = 0;
|
|
410
|
+
let softLimitReached = false;
|
|
411
|
+
const maxTurns = input.runtimeConfig?.maxTurns;
|
|
412
|
+
const graceTurns = input.runtimeConfig?.graceTurns ?? 5;
|
|
413
|
+
const sidechainPath = sidechainOutputPath(input.manifest.stateRoot, input.task.id);
|
|
414
|
+
writeSidechainEntry(sidechainPath, { agentId, type: "user", message: { role: "user", content: input.prompt }, cwd: input.task.cwd });
|
|
415
|
+
if (typeof session.subscribe === "function") {
|
|
416
|
+
unsubscribe = session.subscribe((event) => {
|
|
417
|
+
if (!isCurrent()) return;
|
|
418
|
+
jsonEvents += 1;
|
|
419
|
+
appendTranscript(input.transcriptPath, event);
|
|
420
|
+
const sidechainType = eventToSidechainType(event);
|
|
421
|
+
if (sidechainType) writeSidechainEntry(sidechainPath, { agentId, type: sidechainType, message: event, cwd: input.task.cwd });
|
|
422
|
+
const obj = asRecord(event);
|
|
423
|
+
if (obj?.type === "turn_end") {
|
|
424
|
+
turnCount += 1;
|
|
425
|
+
if (maxTurns !== undefined && !softLimitReached && turnCount >= maxTurns) {
|
|
426
|
+
softLimitReached = true;
|
|
427
|
+
void session?.steer?.("You have reached your turn limit. Wrap up immediately — provide your final answer now.");
|
|
428
|
+
} else if (maxTurns !== undefined && softLimitReached && turnCount >= maxTurns + graceTurns) {
|
|
429
|
+
void session?.abort?.();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
input.onEvent?.(event);
|
|
433
|
+
const text = [...eventText(event), ...finalAssistantText(event)].join("\n");
|
|
434
|
+
if (text.trim()) {
|
|
435
|
+
stdout += `${text}\n`;
|
|
436
|
+
input.onOutput?.(text);
|
|
437
|
+
}
|
|
438
|
+
// Phase 1: collect events for yield detection
|
|
439
|
+
if (event && typeof event === "object" && !Array.isArray(event)) {
|
|
440
|
+
collectedJsonEvents.push(event as Record<string, unknown>);
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
if (input.signal) {
|
|
445
|
+
if (input.signal.aborted) await session.abort?.();
|
|
446
|
+
else input.signal.addEventListener("abort", () => { void session?.abort?.(); }, { once: true });
|
|
447
|
+
}
|
|
448
|
+
const effectivePrompt = input.runtimeConfig?.inheritContext === true && input.parentContext ? `${input.parentContext}\n\n---\n# Live Subagent Task\n${input.prompt}` : input.prompt;
|
|
449
|
+
|
|
450
|
+
// Phase 3: Wrap session.prompt with timeout for graceful cancellation
|
|
451
|
+
const sessionTimeoutMs = DEFAULT_LIVE_SESSION.responseTimeoutMs;
|
|
452
|
+
const promptPromise = session.prompt?.(effectivePrompt, { source: "api", expandPromptTemplates: false });
|
|
453
|
+
if (promptPromise) {
|
|
454
|
+
const timeoutPromise = new Promise<void>((_, reject) => {
|
|
455
|
+
const timer = setTimeout(() => reject(new Error(`Live-session timed out after ${sessionTimeoutMs}ms`)), sessionTimeoutMs);
|
|
456
|
+
timer.unref();
|
|
457
|
+
input.signal?.addEventListener("abort", () => clearTimeout(timer), { once: true });
|
|
458
|
+
});
|
|
459
|
+
try {
|
|
460
|
+
await Promise.race([promptPromise, timeoutPromise]);
|
|
461
|
+
} catch (promptError) {
|
|
462
|
+
const msg = promptError instanceof Error ? promptError.message : String(promptError);
|
|
463
|
+
if (msg.includes("timed out")) {
|
|
464
|
+
await session.abort?.();
|
|
465
|
+
updateLiveAgentStatus(agentId, "failed");
|
|
466
|
+
return { available: true, exitCode: 1, stdout: stdout.trim(), stderr: msg, jsonEvents, error: msg };
|
|
467
|
+
}
|
|
468
|
+
throw promptError;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// --- Phase 1: Yield enforcement loop ---
|
|
473
|
+
// After the initial prompt completes, check if the worker called submit_result.
|
|
474
|
+
// Priority: 1) custom tool callback (G1), 2) JSON event detection (legacy).
|
|
475
|
+
const yieldConfig = input.runtimeConfig?.yield ?? { enabled: DEFAULT_YIELD_CONFIG.enabled };
|
|
476
|
+
const yieldEnabled = yieldConfig.enabled !== false;
|
|
477
|
+
if (yieldEnabled && session) {
|
|
478
|
+
// Check custom tool callback first (G1)
|
|
479
|
+
if (customToolYieldResolved && customToolYieldResult) {
|
|
480
|
+
yieldResult = customToolYieldResult;
|
|
481
|
+
} else {
|
|
482
|
+
// Legacy: detect from JSON events
|
|
483
|
+
const alreadyYielded = hasYieldInOutput(collectedJsonEvents);
|
|
484
|
+
if (alreadyYielded) {
|
|
485
|
+
const yieldEvent = collectedJsonEvents.find((e) => isYieldEvent(e));
|
|
486
|
+
if (yieldEvent) yieldResult = extractYieldResult(yieldEvent);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
// Phase 2: Validate yield data against output schema if provided
|
|
490
|
+
let schemaFailures = 0;
|
|
491
|
+
const maxSchemaFailures = 2;
|
|
492
|
+
if (yieldResult && input.outputSchema) {
|
|
493
|
+
const validation = await validateYieldData(yieldResult.structuredData, input.outputSchema);
|
|
494
|
+
if (!validation.valid) {
|
|
495
|
+
schemaFailures++;
|
|
496
|
+
yieldResult = undefined;
|
|
497
|
+
customToolYieldResolved = false;
|
|
498
|
+
const schemaReminder = `Your submit_result data did not match the required schema: ${validation.error}. Please fix and call submit_result again with valid data.`;
|
|
499
|
+
try {
|
|
500
|
+
await session.prompt?.(schemaReminder, { source: "api", expandPromptTemplates: false });
|
|
501
|
+
} catch {
|
|
502
|
+
/* ignore */
|
|
503
|
+
}
|
|
504
|
+
await new Promise((resolve) => setTimeout(resolve, DEFAULT_LIVE_SESSION.yieldPollIntervalMs));
|
|
505
|
+
// Check again after schema reminder
|
|
506
|
+
if (customToolYieldResolved && customToolYieldResult) {
|
|
507
|
+
yieldResult = customToolYieldResult;
|
|
508
|
+
} else {
|
|
509
|
+
const newEvents = collectedJsonEvents.slice(-10);
|
|
510
|
+
if (hasYieldInOutput(newEvents)) {
|
|
511
|
+
const yieldEvent = newEvents.find((e) => isYieldEvent(e));
|
|
512
|
+
if (yieldEvent) {
|
|
513
|
+
const candidate = extractYieldResult(yieldEvent);
|
|
514
|
+
if (candidate && input.outputSchema) {
|
|
515
|
+
const revalidation = await validateYieldData(candidate.structuredData, input.outputSchema);
|
|
516
|
+
if (revalidation.valid || schemaFailures >= maxSchemaFailures) {
|
|
517
|
+
yieldResult = candidate;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
// Reminder loop — only if yield not yet received
|
|
526
|
+
const maxReminders = yieldConfig.maxReminders ?? DEFAULT_LIVE_SESSION.maxYieldRetries;
|
|
527
|
+
let retryCount = 0;
|
|
528
|
+
while (!customToolYieldResolved && !yieldResult && retryCount < maxReminders && !input.signal?.aborted) {
|
|
529
|
+
retryCount++;
|
|
530
|
+
const reminder = buildYieldReminder(retryCount, maxReminders, yieldConfig.reminderPrompt);
|
|
531
|
+
try {
|
|
532
|
+
// G6: Constrain tool set to submit_result before sending reminder
|
|
533
|
+
const prevTools = typeof session.getActiveToolNames === "function" ? session.getActiveToolNames() : [];
|
|
534
|
+
if (typeof session.setActiveToolsByName === "function" && prevTools.length > 0) {
|
|
535
|
+
session.setActiveToolsByName(["submit_result"]);
|
|
536
|
+
}
|
|
537
|
+
await session.prompt?.(reminder, { source: "api", expandPromptTemplates: false });
|
|
538
|
+
// Restore previous tools
|
|
539
|
+
if (typeof session.setActiveToolsByName === "function" && prevTools.length > 0) {
|
|
540
|
+
session.setActiveToolsByName(prevTools);
|
|
541
|
+
}
|
|
542
|
+
} catch {
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
const pollInterval = DEFAULT_LIVE_SESSION.yieldPollIntervalMs;
|
|
546
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
547
|
+
// Check custom tool callback
|
|
548
|
+
if (customToolYieldResolved && customToolYieldResult) {
|
|
549
|
+
yieldResult = customToolYieldResult;
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
// Legacy: check JSON events
|
|
553
|
+
if (hasYieldInOutput(collectedJsonEvents.slice(-10))) {
|
|
554
|
+
const yieldEvent = collectedJsonEvents.slice(-10).find((e) => isYieldEvent(e));
|
|
555
|
+
if (yieldEvent) yieldResult = extractYieldResult(yieldEvent);
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (!customToolYieldResolved && !yieldResult && !input.signal?.aborted && retryCount >= maxReminders) {
|
|
560
|
+
input.onEvent?.({ type: "task.attention", runId: input.manifest.runId, taskId: input.task.id, message: "Live-session worker completed without calling submit_result tool.", data: { activityState: "needs_attention", reason: "no_yield", attempts: retryCount } });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const usage = usageFromStats(typeof session.getStats === "function" ? session.getStats() : session.stats);
|
|
565
|
+
updateLiveAgentStatus(agentId, "completed");
|
|
566
|
+
return { available: true, exitCode: 0, stdout: stdout.trim(), stderr: created.modelFallbackMessage ?? "", jsonEvents, usage, yieldResult };
|
|
567
|
+
} catch (error) {
|
|
568
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
569
|
+
|
|
570
|
+
// Phase 8: Log diagnostics on failure
|
|
571
|
+
try {
|
|
572
|
+
const agents = listLiveAgents();
|
|
573
|
+
const health = collectLiveSessionHealth(agents, () => undefined);
|
|
574
|
+
const diagnostics = formatLiveSessionDiagnostics(health);
|
|
575
|
+
input.onEvent?.({ type: "live-session.diagnostics", data: diagnostics });
|
|
576
|
+
} catch (diagError) {
|
|
577
|
+
logInternalError("live-session.diagnostics", diagError);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
updateLiveAgentStatus(`${input.manifest.runId}:${input.task.id}`, "failed");
|
|
581
|
+
return { available: true, exitCode: 1, stdout: stdout.trim(), stderr: message, jsonEvents, error: message };
|
|
582
|
+
} finally {
|
|
583
|
+
// H6: Unsubscribe listeners FIRST before clearing timer to prevent race
|
|
584
|
+
unsubscribe?.();
|
|
585
|
+
unsubscribeControlRealtime?.();
|
|
586
|
+
if (controlTimer) clearInterval(controlTimer);
|
|
587
|
+
|
|
588
|
+
// Phase 8: Emit final health snapshot
|
|
589
|
+
try {
|
|
590
|
+
const agents = listLiveAgents();
|
|
591
|
+
if (agents.length > 0) {
|
|
592
|
+
const health = collectLiveSessionHealth(agents, () => undefined);
|
|
593
|
+
input.onEvent?.({ type: "live-session.health", data: health });
|
|
594
|
+
}
|
|
595
|
+
} catch (healthError) {
|
|
596
|
+
logInternalError("live-session.health-snapshot", healthError);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|