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.
Files changed (178) hide show
  1. package/CHANGELOG.md +97 -0
  2. package/README.md +5 -5
  3. package/agents/analyst.md +11 -11
  4. package/agents/critic.md +11 -11
  5. package/agents/executor.md +11 -11
  6. package/agents/explorer.md +11 -11
  7. package/agents/planner.md +11 -11
  8. package/agents/reviewer.md +11 -11
  9. package/agents/security-reviewer.md +11 -11
  10. package/agents/test-engineer.md +11 -11
  11. package/agents/verifier.md +11 -11
  12. package/agents/writer.md +11 -11
  13. package/docs/next-upgrade-roadmap.md +808 -0
  14. package/docs/research/AGENT-EXECUTION-ARCHITECTURE.md +261 -0
  15. package/docs/research/AGENT-LIFECYCLE-COMPARISON.md +111 -0
  16. package/docs/research/AUDIT_OH_MY_PI.md +261 -0
  17. package/docs/research/AUDIT_PI_CREW.md +457 -0
  18. package/docs/research/CAVEMAN-DEEP-RESEARCH.md +281 -0
  19. package/docs/research/COMPARISON_OH_MY_PI_VS_PI_CREW.md +264 -0
  20. package/docs/research/DEEP-RESEARCH-PI-POWERBAR.md +343 -0
  21. package/docs/research/DEEP_RESEARCH_SUBAGENT_ARCHITECTURE.md +480 -0
  22. package/docs/research/GAP_CLOSURE_IMPLEMENTATION_PLAN.md +354 -0
  23. package/docs/research/IMPLEMENTATION_PLAN.md +385 -0
  24. package/docs/research/LIVE-SESSION-PRODUCTION-READY-PLAN.md +502 -0
  25. package/docs/research/OH-MY-PI-DEEP-RESEARCH-v14.7.6.md +266 -0
  26. package/docs/research/REMAINING-GAPS-PLAN.md +363 -0
  27. package/docs/research/SESSION-SUMMARY-2026-05-08.md +146 -0
  28. package/docs/research/UI-RESPONSIVENESS-AUDIT.md +173 -0
  29. package/docs/research-awesome-agent-skills-distillation.md +100 -0
  30. package/docs/research-oh-my-pi-distillation.md +369 -0
  31. package/docs/source-runtime-refactor-map.md +24 -0
  32. package/docs/usage.md +3 -3
  33. package/install.mjs +52 -8
  34. package/package.json +99 -98
  35. package/schema.json +10 -1
  36. package/skills/async-worker-recovery/SKILL.md +42 -0
  37. package/skills/context-artifact-hygiene/SKILL.md +52 -0
  38. package/skills/delegation-patterns/SKILL.md +54 -0
  39. package/skills/mailbox-interactive/SKILL.md +40 -0
  40. package/skills/model-routing-context/SKILL.md +39 -0
  41. package/skills/multi-perspective-review/SKILL.md +58 -0
  42. package/skills/observability-reliability/SKILL.md +41 -0
  43. package/skills/orchestration/SKILL.md +157 -0
  44. package/skills/ownership-session-security/SKILL.md +41 -0
  45. package/skills/pi-extension-lifecycle/SKILL.md +39 -0
  46. package/skills/requirements-to-task-packet/SKILL.md +63 -0
  47. package/skills/resource-discovery-config/SKILL.md +41 -0
  48. package/skills/runtime-state-reader/SKILL.md +44 -0
  49. package/skills/secure-agent-orchestration-review/SKILL.md +45 -0
  50. package/skills/state-mutation-locking/SKILL.md +42 -0
  51. package/skills/systematic-debugging/SKILL.md +67 -0
  52. package/skills/ui-render-performance/SKILL.md +39 -0
  53. package/skills/verification-before-done/SKILL.md +57 -0
  54. package/skills/worktree-isolation/SKILL.md +39 -0
  55. package/src/agents/agent-config.ts +6 -0
  56. package/src/agents/agent-search.ts +98 -0
  57. package/src/agents/agent-serializer.ts +38 -34
  58. package/src/agents/discover-agents.ts +29 -15
  59. package/src/config/config.ts +72 -24
  60. package/src/config/defaults.ts +25 -0
  61. package/src/extension/autonomous-policy.ts +26 -33
  62. package/src/extension/help.ts +1 -0
  63. package/src/extension/management.ts +5 -0
  64. package/src/extension/project-init.ts +62 -2
  65. package/src/extension/register.ts +69 -22
  66. package/src/extension/registration/commands.ts +64 -25
  67. package/src/extension/registration/compaction-guard.ts +1 -1
  68. package/src/extension/registration/subagent-helpers.ts +8 -0
  69. package/src/extension/registration/subagent-tools.ts +149 -148
  70. package/src/extension/registration/team-tool.ts +14 -10
  71. package/src/extension/run-index.ts +35 -21
  72. package/src/extension/run-maintenance.ts +30 -5
  73. package/src/extension/team-tool/api.ts +47 -9
  74. package/src/extension/team-tool/cancel.ts +109 -5
  75. package/src/extension/team-tool/context.ts +8 -0
  76. package/src/extension/team-tool/intent-policy.ts +42 -0
  77. package/src/extension/team-tool/lifecycle-actions.ts +120 -79
  78. package/src/extension/team-tool/parallel-dispatch.ts +156 -0
  79. package/src/extension/team-tool/respond.ts +46 -18
  80. package/src/extension/team-tool/run.ts +55 -12
  81. package/src/extension/team-tool/status.ts +13 -2
  82. package/src/extension/team-tool-types.ts +3 -0
  83. package/src/extension/team-tool.ts +45 -14
  84. package/src/hooks/registry.ts +61 -0
  85. package/src/hooks/types.ts +41 -0
  86. package/src/observability/event-to-metric.ts +8 -1
  87. package/src/runtime/agent-control.ts +169 -63
  88. package/src/runtime/async-runner.ts +3 -1
  89. package/src/runtime/background-runner.ts +78 -53
  90. package/src/runtime/cancellation-token.ts +89 -0
  91. package/src/runtime/cancellation.ts +61 -0
  92. package/src/runtime/capability-inventory.ts +116 -0
  93. package/src/runtime/child-pi.ts +458 -444
  94. package/src/runtime/code-summary.ts +247 -0
  95. package/src/runtime/crash-recovery.ts +182 -0
  96. package/src/runtime/crew-agent-records.ts +70 -10
  97. package/src/runtime/crew-agent-runtime.ts +1 -0
  98. package/src/runtime/custom-tools/irc-tool.ts +201 -0
  99. package/src/runtime/custom-tools/submit-result-tool.ts +90 -0
  100. package/src/runtime/deadletter.ts +1 -0
  101. package/src/runtime/delivery-coordinator.ts +48 -25
  102. package/src/runtime/effectiveness.ts +81 -0
  103. package/src/runtime/event-stream-bridge.ts +90 -0
  104. package/src/runtime/live-agent-control.ts +2 -1
  105. package/src/runtime/live-agent-manager.ts +179 -85
  106. package/src/runtime/live-control-realtime.ts +1 -1
  107. package/src/runtime/live-extension-bridge.ts +150 -0
  108. package/src/runtime/live-irc.ts +92 -0
  109. package/src/runtime/live-session-health.ts +100 -0
  110. package/src/runtime/live-session-runtime.ts +599 -305
  111. package/src/runtime/manifest-cache.ts +17 -2
  112. package/src/runtime/mcp-proxy.ts +113 -0
  113. package/src/runtime/model-fallback.ts +6 -4
  114. package/src/runtime/notebook-helpers.ts +90 -0
  115. package/src/runtime/orphan-sentinel.ts +7 -0
  116. package/src/runtime/output-validator.ts +187 -0
  117. package/src/runtime/parallel-utils.ts +57 -0
  118. package/src/runtime/parent-guard.ts +80 -0
  119. package/src/runtime/pi-args.ts +18 -3
  120. package/src/runtime/process-status.ts +5 -1
  121. package/src/runtime/prose-compressor.ts +164 -0
  122. package/src/runtime/result-extractor.ts +121 -0
  123. package/src/runtime/retry-executor.ts +81 -64
  124. package/src/runtime/runtime-resolver.ts +23 -10
  125. package/src/runtime/semaphore.ts +131 -0
  126. package/src/runtime/sensitive-paths.ts +92 -0
  127. package/src/runtime/skill-instructions.ts +222 -0
  128. package/src/runtime/stale-reconciler.ts +4 -14
  129. package/src/runtime/stream-preview.ts +177 -0
  130. package/src/runtime/subagent-manager.ts +6 -2
  131. package/src/runtime/subprocess-tool-registry.ts +67 -0
  132. package/src/runtime/task-output-context.ts +177 -127
  133. package/src/runtime/task-runner/capabilities.ts +78 -0
  134. package/src/runtime/task-runner/live-executor.ts +107 -101
  135. package/src/runtime/task-runner/prompt-builder.ts +72 -8
  136. package/src/runtime/task-runner/prompt-pipeline.ts +64 -0
  137. package/src/runtime/task-runner/run-projection.ts +104 -0
  138. package/src/runtime/task-runner.ts +115 -5
  139. package/src/runtime/team-runner.ts +134 -19
  140. package/src/runtime/workspace-tree.ts +298 -0
  141. package/src/runtime/yield-handler.ts +189 -0
  142. package/src/schema/config-schema.ts +7 -0
  143. package/src/schema/team-tool-schema.ts +14 -4
  144. package/src/skills/discover-skills.ts +67 -0
  145. package/src/state/active-run-registry.ts +167 -0
  146. package/src/state/artifact-store.ts +4 -1
  147. package/src/state/atomic-write.ts +50 -1
  148. package/src/state/blob-store.ts +117 -0
  149. package/src/state/contracts.ts +2 -1
  150. package/src/state/event-log-rotation.ts +158 -0
  151. package/src/state/event-log.ts +52 -2
  152. package/src/state/mailbox.ts +129 -9
  153. package/src/state/state-store.ts +32 -5
  154. package/src/state/types.ts +64 -2
  155. package/src/teams/team-config.ts +1 -0
  156. package/src/ui/agent-management-overlay.ts +144 -0
  157. package/src/ui/crew-widget.ts +15 -5
  158. package/src/ui/dashboard-panes/cancellation-pane.ts +43 -0
  159. package/src/ui/dashboard-panes/capability-pane.ts +60 -0
  160. package/src/ui/dashboard-panes/mailbox-pane.ts +35 -11
  161. package/src/ui/dashboard-panes/progress-pane.ts +2 -0
  162. package/src/ui/live-run-sidebar.ts +4 -0
  163. package/src/ui/powerbar-publisher.ts +77 -15
  164. package/src/ui/render-coalescer.ts +51 -0
  165. package/src/ui/run-dashboard.ts +4 -0
  166. package/src/ui/run-event-bus.ts +209 -0
  167. package/src/ui/run-snapshot-cache.ts +78 -18
  168. package/src/ui/snapshot-types.ts +10 -0
  169. package/src/ui/transcript-entries.ts +258 -0
  170. package/src/utils/ids.ts +5 -0
  171. package/src/utils/incremental-reader.ts +104 -0
  172. package/src/utils/paths.ts +4 -2
  173. package/src/utils/scan-cache.ts +137 -0
  174. package/src/utils/sse-parser.ts +134 -0
  175. package/src/utils/task-name-generator.ts +337 -0
  176. package/src/utils/visual.ts +33 -2
  177. package/src/workflows/workflow-config.ts +1 -0
  178. 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
- export interface LiveSessionSpawnInput {
16
- manifest: TeamRunManifest;
17
- task: TeamTaskState;
18
- step: WorkflowStep;
19
- agent: AgentConfig;
20
- prompt: string;
21
- signal?: AbortSignal;
22
- transcriptPath?: string;
23
- onEvent?: (event: unknown) => void;
24
- onOutput?: (text: string) => void;
25
- runtimeConfig?: CrewRuntimeConfig;
26
- parentContext?: string;
27
- parentModel?: unknown;
28
- modelRegistry?: unknown;
29
- isCurrent?: () => boolean;
30
- }
31
-
32
- export interface LiveSessionRunResult {
33
- available: true;
34
- exitCode: number | null;
35
- stdout: string;
36
- stderr: string;
37
- jsonEvents: number;
38
- usage?: UsageState;
39
- error?: string;
40
- }
41
-
42
- export interface LiveSessionUnavailableResult {
43
- available: false;
44
- reason: string;
45
- }
46
-
47
- export interface LiveSessionPlannedResult {
48
- available: true;
49
- reason: string;
50
- }
51
-
52
- type LiveSessionModule = Record<string, unknown> & {
53
- createAgentSession?: (options?: Record<string, unknown>) => Promise<{ session: LiveSessionLike; modelFallbackMessage?: string }>;
54
- DefaultResourceLoader?: new (options: Record<string, unknown>) => { reload?: () => Promise<void> };
55
- SessionManager?: { inMemory?: (cwd?: string) => unknown; create?: (cwd?: string, sessionDir?: string) => unknown };
56
- SettingsManager?: { create?: (cwd?: string, agentDir?: string) => unknown };
57
- getAgentDir?: () => string;
58
- };
59
-
60
- type LiveSessionLike = {
61
- subscribe?: (listener: (event: unknown) => void) => (() => void);
62
- prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>;
63
- steer?: (text: string) => Promise<void>;
64
- abort?: () => Promise<void> | void;
65
- getStats?: () => unknown;
66
- stats?: unknown;
67
- bindExtensions?: (bindings?: Record<string, unknown>) => Promise<void>;
68
- getActiveToolNames?: () => string[];
69
- setActiveToolsByName?: (names: string[]) => void;
70
- };
71
-
72
- function appendTranscript(filePath: string | undefined, event: unknown): void {
73
- if (!filePath) return;
74
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
75
- fs.appendFileSync(filePath, `${JSON.stringify(redactSecrets(event))}\n`, "utf-8");
76
- }
77
-
78
- function asRecord(value: unknown): Record<string, unknown> | undefined {
79
- return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
80
- }
81
-
82
- function textFromContent(content: unknown): string[] {
83
- if (typeof content === "string") return [content];
84
- if (!Array.isArray(content)) return [];
85
- return content.flatMap((part) => {
86
- const obj = asRecord(part);
87
- if (!obj) return [];
88
- if (obj.type === "text" && typeof obj.text === "string") return [obj.text];
89
- if (typeof obj.content === "string") return [obj.content];
90
- return [];
91
- });
92
- }
93
-
94
- function eventText(event: unknown): string[] {
95
- const obj = asRecord(event);
96
- if (!obj) return [];
97
- const text: string[] = [];
98
- if (typeof obj.text === "string") text.push(obj.text);
99
- text.push(...textFromContent(obj.content));
100
- const message = asRecord(obj.message);
101
- if (message) text.push(...textFromContent(message.content));
102
- return text.filter((entry) => entry.trim());
103
- }
104
-
105
- function finalAssistantText(event: unknown): string[] {
106
- const obj = asRecord(event);
107
- if (!obj || obj.type !== "message_end") return [];
108
- const message = asRecord(obj.message);
109
- if (message?.role !== "assistant") return [];
110
- return textFromContent(message.content);
111
- }
112
-
113
- function numberField(obj: Record<string, unknown> | undefined, keys: string[]): number | undefined {
114
- if (!obj) return undefined;
115
- for (const key of keys) {
116
- const value = obj[key];
117
- if (typeof value === "number" && Number.isFinite(value)) return value;
118
- }
119
- return undefined;
120
- }
121
-
122
- function modelFromRegistry(modelRegistry: unknown, modelId: string | undefined): unknown {
123
- if (!modelId || !modelId.includes("/")) return undefined;
124
- const registry = asRecord(modelRegistry);
125
- const find = registry?.find;
126
- if (typeof find !== "function") return undefined;
127
- const [provider, ...modelParts] = modelId.split("/");
128
- const id = modelParts.join("/");
129
- try {
130
- return find.call(modelRegistry, provider, id);
131
- } catch {
132
- return undefined;
133
- }
134
- }
135
-
136
- function liveSystemPrompt(input: LiveSessionSpawnInput): string {
137
- 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"))) : "";
138
- return [
139
- "# pi-crew Live Subagent",
140
- `Run ID: ${input.manifest.runId}`,
141
- `Task ID: ${input.task.id}`,
142
- `Role: ${input.task.role}`,
143
- `Agent: ${input.agent.name}`,
144
- `Working directory: ${input.task.cwd}`,
145
- "",
146
- input.agent.systemPrompt || "Follow the user task exactly and report verification evidence.",
147
- memory ? `\n${memory}` : "",
148
- ].filter(Boolean).join("\n");
149
- }
150
-
151
- function filterActiveTools(session: LiveSessionLike, agent: AgentConfig): void {
152
- if (typeof session.getActiveToolNames !== "function" || typeof session.setActiveToolsByName !== "function") return;
153
- const recursiveTools = new Set(["team", "Team", "Agent", "get_subagent_result", "steer_subagent"]);
154
- const allowed = agent.tools?.length ? new Set(agent.tools) : undefined;
155
- const active = session.getActiveToolNames().filter((name) => !recursiveTools.has(name) && (!allowed || allowed.has(name)));
156
- session.setActiveToolsByName(active);
157
- }
158
-
159
- function usageFromStats(stats: unknown): UsageState | undefined {
160
- const obj = asRecord(stats);
161
- if (!obj) return undefined;
162
- const input = numberField(obj, ["input", "inputTokens", "input_tokens"]);
163
- const output = numberField(obj, ["output", "outputTokens", "output_tokens"]);
164
- const cacheRead = numberField(obj, ["cacheRead", "cache_read"]);
165
- const cacheWrite = numberField(obj, ["cacheWrite", "cache_write"]);
166
- const cost = numberField(obj, ["cost"]);
167
- const turns = numberField(obj, ["turns", "turnCount", "turn_count"]);
168
- return [input, output, cacheRead, cacheWrite, cost, turns].some((value) => value !== undefined) ? { input, output, cacheRead, cacheWrite, cost, turns } : undefined;
169
- }
170
-
171
- export async function probeLiveSessionRuntime(): Promise<LiveSessionUnavailableResult | LiveSessionPlannedResult> {
172
- const availability = await isLiveSessionRuntimeAvailable();
173
- if (!availability.available) return { available: false, reason: availability.reason ?? "Live-session runtime is unavailable." };
174
- return { available: true, reason: "Live-session SDK exports are available and pi-crew can run experimental in-process live agents when runtime.mode=live-session." };
175
- }
176
-
177
- export async function runLiveSessionTask(input: LiveSessionSpawnInput): Promise<LiveSessionRunResult> {
178
- const isCurrent = input.isCurrent ?? (() => true);
179
- if (process.env.PI_CREW_MOCK_LIVE_SESSION === "success") {
180
- const agentId = `${input.manifest.runId}:${input.task.id}`;
181
- const inherited = input.runtimeConfig?.inheritContext === true && input.parentContext ? ` with inherited context: ${input.parentContext}` : "";
182
- const event = { type: "message_end", message: { role: "assistant", content: [{ type: "text", text: `Mock live-session success for ${input.agent.name}${inherited}` }] } };
183
- const mockSession = { steer: async () => {}, prompt: async () => {}, abort: async () => {} };
184
- registerLiveAgent({ agentId, runId: input.manifest.runId, taskId: input.task.id, session: mockSession, status: "running" });
185
- appendTranscript(input.transcriptPath, event);
186
- const sidechainPath = sidechainOutputPath(input.manifest.stateRoot, input.task.id);
187
- writeSidechainEntry(sidechainPath, { agentId, type: "user", message: { role: "user", content: input.prompt }, cwd: input.task.cwd });
188
- writeSidechainEntry(sidechainPath, { agentId, type: "message", message: event, cwd: input.task.cwd });
189
- if (isCurrent()) input.onEvent?.(event);
190
- const stdout = `Mock live-session success for ${input.agent.name}${inherited}`;
191
- if (isCurrent()) input.onOutput?.(stdout);
192
- updateLiveAgentStatus(agentId, "completed");
193
- return { available: true, exitCode: 0, stdout, stderr: "", jsonEvents: 1 };
194
- }
195
- const availability = await isLiveSessionRuntimeAvailable();
196
- if (!availability.available) return { available: true, exitCode: 1, stdout: "", stderr: availability.reason ?? "Live-session runtime unavailable.", jsonEvents: 0, error: availability.reason };
197
- const mod = await import("@mariozechner/pi-coding-agent") as LiveSessionModule;
198
- if (typeof mod.createAgentSession !== "function") return { available: true, exitCode: 1, stdout: "", stderr: "createAgentSession export is unavailable.", jsonEvents: 0, error: "createAgentSession export is unavailable." };
199
- let session: LiveSessionLike | undefined;
200
- let unsubscribe: (() => void) | undefined;
201
- let unsubscribeControlRealtime: (() => void) | undefined;
202
- let controlTimer: ReturnType<typeof setInterval> | undefined;
203
- let stdout = "";
204
- let jsonEvents = 0;
205
- try {
206
- const agentDir = typeof mod.getAgentDir === "function" ? mod.getAgentDir() : undefined;
207
- let resourceLoader: unknown;
208
- if (mod.DefaultResourceLoader && agentDir) {
209
- resourceLoader = new mod.DefaultResourceLoader({
210
- cwd: input.task.cwd,
211
- agentDir,
212
- noPromptTemplates: true,
213
- noThemes: true,
214
- noContextFiles: input.runtimeConfig?.inheritContext !== true,
215
- systemPromptOverride: () => liveSystemPrompt(input),
216
- appendSystemPromptOverride: () => [],
217
- });
218
- await (resourceLoader as { reload?: () => Promise<void> }).reload?.();
219
- }
220
- const resolvedModel = modelFromRegistry(input.modelRegistry, input.agent.model) ?? input.parentModel;
221
- const created = await mod.createAgentSession({
222
- cwd: input.task.cwd,
223
- ...(agentDir ? { agentDir } : {}),
224
- ...(resourceLoader ? { resourceLoader } : {}),
225
- ...(mod.SessionManager?.inMemory ? { sessionManager: mod.SessionManager.inMemory(input.task.cwd) } : {}),
226
- ...(mod.SettingsManager?.create && agentDir ? { settingsManager: mod.SettingsManager.create(input.task.cwd, agentDir) } : {}),
227
- ...(input.modelRegistry ? { modelRegistry: input.modelRegistry } : {}),
228
- ...(resolvedModel ? { model: resolvedModel } : {}),
229
- ...(input.agent.thinking ? { thinkingLevel: input.agent.thinking } : {}),
230
- });
231
- session = created.session;
232
- filterActiveTools(session, input.agent);
233
- await session.bindExtensions?.({});
234
- const agentId = `${input.manifest.runId}:${input.task.id}`;
235
- registerLiveAgent({ agentId, runId: input.manifest.runId, taskId: input.task.id, session, status: "running" });
236
- let controlCursor: LiveAgentControlCursor = { offset: 0 };
237
- const seenControlRequestIds = new Set<string>();
238
- let controlBusy = false;
239
- const pollControl = async () => {
240
- if (!isCurrent() || controlBusy || !session) return;
241
- controlBusy = true;
242
- try {
243
- controlCursor = await applyLiveAgentControlRequests({ manifest: input.manifest, taskId: input.task.id, agentId, session, cursor: controlCursor, seenRequestIds: seenControlRequestIds });
244
- } finally {
245
- controlBusy = false;
246
- }
247
- };
248
- unsubscribeControlRealtime = subscribeLiveControlRealtime((request) => {
249
- if (!isCurrent() || request.runId !== input.manifest.runId || request.taskId !== input.task.id || !session) return;
250
- void applyLiveAgentControlRequest({ request, taskId: input.task.id, agentId, session, seenRequestIds: seenControlRequestIds });
251
- });
252
- await pollControl();
253
- controlTimer = setInterval(() => {
254
- if (isCurrent()) void pollControl();
255
- }, 500);
256
- let turnCount = 0;
257
- let softLimitReached = false;
258
- const maxTurns = input.runtimeConfig?.maxTurns;
259
- const graceTurns = input.runtimeConfig?.graceTurns ?? 5;
260
- const sidechainPath = sidechainOutputPath(input.manifest.stateRoot, input.task.id);
261
- writeSidechainEntry(sidechainPath, { agentId, type: "user", message: { role: "user", content: input.prompt }, cwd: input.task.cwd });
262
- if (typeof session.subscribe === "function") {
263
- unsubscribe = session.subscribe((event) => {
264
- if (!isCurrent()) return;
265
- jsonEvents += 1;
266
- appendTranscript(input.transcriptPath, event);
267
- const sidechainType = eventToSidechainType(event);
268
- if (sidechainType) writeSidechainEntry(sidechainPath, { agentId, type: sidechainType, message: event, cwd: input.task.cwd });
269
- const obj = asRecord(event);
270
- if (obj?.type === "turn_end") {
271
- turnCount += 1;
272
- if (maxTurns !== undefined && !softLimitReached && turnCount >= maxTurns) {
273
- softLimitReached = true;
274
- void session?.steer?.("You have reached your turn limit. Wrap up immediately — provide your final answer now.");
275
- } else if (maxTurns !== undefined && softLimitReached && turnCount >= maxTurns + graceTurns) {
276
- void session?.abort?.();
277
- }
278
- }
279
- input.onEvent?.(event);
280
- const text = [...eventText(event), ...finalAssistantText(event)].join("\n");
281
- if (text.trim()) {
282
- stdout += `${text}\n`;
283
- input.onOutput?.(text);
284
- }
285
- });
286
- }
287
- if (input.signal) {
288
- if (input.signal.aborted) await session.abort?.();
289
- else input.signal.addEventListener("abort", () => { void session?.abort?.(); }, { once: true });
290
- }
291
- const effectivePrompt = input.runtimeConfig?.inheritContext === true && input.parentContext ? `${input.parentContext}\n\n---\n# Live Subagent Task\n${input.prompt}` : input.prompt;
292
- await session.prompt?.(effectivePrompt, { source: "api", expandPromptTemplates: false });
293
- const usage = usageFromStats(typeof session.getStats === "function" ? session.getStats() : session.stats);
294
- updateLiveAgentStatus(agentId, "completed");
295
- return { available: true, exitCode: 0, stdout: stdout.trim(), stderr: created.modelFallbackMessage ?? "", jsonEvents, usage };
296
- } catch (error) {
297
- const message = error instanceof Error ? error.message : String(error);
298
- updateLiveAgentStatus(`${input.manifest.runId}:${input.task.id}`, "failed");
299
- return { available: true, exitCode: 1, stdout: stdout.trim(), stderr: message, jsonEvents, error: message };
300
- } finally {
301
- if (controlTimer) clearInterval(controlTimer);
302
- unsubscribeControlRealtime?.();
303
- unsubscribe?.();
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
+ }