pi-crew 0.1.45 → 0.1.46

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 (198) hide show
  1. package/README.md +5 -5
  2. package/agents/analyst.md +1 -1
  3. package/agents/critic.md +1 -1
  4. package/agents/executor.md +1 -1
  5. package/agents/explorer.md +1 -1
  6. package/agents/planner.md +1 -1
  7. package/agents/reviewer.md +1 -1
  8. package/agents/security-reviewer.md +1 -1
  9. package/agents/test-engineer.md +1 -1
  10. package/agents/verifier.md +1 -1
  11. package/agents/writer.md +1 -1
  12. package/docs/next-upgrade-roadmap.md +733 -0
  13. package/docs/refactor-tasks-phase3.md +394 -394
  14. package/docs/refactor-tasks-phase4.md +564 -564
  15. package/docs/refactor-tasks-phase5.md +402 -402
  16. package/docs/refactor-tasks-phase6.md +662 -662
  17. package/docs/research-awesome-agent-skills-distillation.md +100 -0
  18. package/docs/research-extension-examples.md +297 -297
  19. package/docs/research-extension-system.md +324 -324
  20. package/docs/research-oh-my-pi-distillation.md +322 -0
  21. package/docs/research-optimization-plan.md +548 -548
  22. package/docs/research-phase10-distillation.md +198 -198
  23. package/docs/research-phase11-distillation.md +201 -201
  24. package/docs/research-pi-coding-agent.md +357 -357
  25. package/docs/research-source-pi-crew-reference.md +174 -174
  26. package/docs/runtime-flow.md +148 -148
  27. package/docs/source-runtime-refactor-map.md +107 -83
  28. package/docs/usage.md +3 -3
  29. package/index.ts +6 -6
  30. package/install.mjs +52 -8
  31. package/package.json +1 -1
  32. package/schema.json +2 -1
  33. package/skills/async-worker-recovery/SKILL.md +42 -0
  34. package/skills/context-artifact-hygiene/SKILL.md +52 -0
  35. package/skills/delegation-patterns/SKILL.md +54 -0
  36. package/skills/mailbox-interactive/SKILL.md +40 -0
  37. package/skills/model-routing-context/SKILL.md +39 -0
  38. package/skills/multi-perspective-review/SKILL.md +58 -0
  39. package/skills/observability-reliability/SKILL.md +41 -0
  40. package/skills/ownership-session-security/SKILL.md +41 -0
  41. package/skills/pi-extension-lifecycle/SKILL.md +39 -0
  42. package/skills/requirements-to-task-packet/SKILL.md +63 -0
  43. package/skills/resource-discovery-config/SKILL.md +41 -0
  44. package/skills/runtime-state-reader/SKILL.md +44 -0
  45. package/skills/secure-agent-orchestration-review/SKILL.md +45 -0
  46. package/skills/state-mutation-locking/SKILL.md +42 -0
  47. package/skills/systematic-debugging/SKILL.md +67 -0
  48. package/skills/ui-render-performance/SKILL.md +39 -0
  49. package/skills/verification-before-done/SKILL.md +57 -0
  50. package/skills/worktree-isolation/SKILL.md +39 -0
  51. package/src/agents/agent-serializer.ts +34 -34
  52. package/src/agents/discover-agents.ts +12 -11
  53. package/src/config/config.ts +48 -24
  54. package/src/config/defaults.ts +14 -0
  55. package/src/extension/cross-extension-rpc.ts +82 -82
  56. package/src/extension/project-init.ts +62 -2
  57. package/src/extension/register.ts +11 -9
  58. package/src/extension/registration/commands.ts +32 -25
  59. package/src/extension/registration/compaction-guard.ts +125 -125
  60. package/src/extension/registration/subagent-helpers.ts +8 -0
  61. package/src/extension/registration/subagent-tools.ts +149 -148
  62. package/src/extension/registration/team-tool.ts +8 -6
  63. package/src/extension/run-bundle-schema.ts +89 -89
  64. package/src/extension/run-index.ts +13 -5
  65. package/src/extension/run-maintenance.ts +62 -43
  66. package/src/extension/team-tool/api.ts +25 -8
  67. package/src/extension/team-tool/cancel.ts +33 -4
  68. package/src/extension/team-tool/context.ts +5 -0
  69. package/src/extension/team-tool/handle-settings.ts +188 -188
  70. package/src/extension/team-tool/inspect.ts +41 -41
  71. package/src/extension/team-tool/lifecycle-actions.ts +91 -79
  72. package/src/extension/team-tool/plan.ts +19 -19
  73. package/src/extension/team-tool/respond.ts +37 -17
  74. package/src/extension/team-tool/run.ts +52 -10
  75. package/src/extension/team-tool/status.ts +12 -1
  76. package/src/extension/team-tool-types.ts +2 -0
  77. package/src/extension/team-tool.ts +32 -11
  78. package/src/i18n.ts +184 -184
  79. package/src/observability/event-to-metric.ts +8 -1
  80. package/src/observability/exporters/otlp-exporter.ts +77 -77
  81. package/src/prompt/prompt-runtime.ts +72 -72
  82. package/src/runtime/agent-control.ts +63 -63
  83. package/src/runtime/agent-memory.ts +72 -72
  84. package/src/runtime/agent-observability.ts +114 -114
  85. package/src/runtime/async-marker.ts +26 -26
  86. package/src/runtime/attention-events.ts +28 -28
  87. package/src/runtime/background-runner.ts +59 -53
  88. package/src/runtime/cancellation.ts +51 -0
  89. package/src/runtime/child-pi.ts +457 -444
  90. package/src/runtime/completion-guard.ts +190 -190
  91. package/src/runtime/crash-recovery.ts +1 -0
  92. package/src/runtime/crew-agent-records.ts +38 -6
  93. package/src/runtime/deadletter.ts +1 -0
  94. package/src/runtime/delivery-coordinator.ts +46 -25
  95. package/src/runtime/direct-run.ts +35 -35
  96. package/src/runtime/effectiveness.ts +76 -0
  97. package/src/runtime/foreground-control.ts +82 -82
  98. package/src/runtime/green-contract.ts +46 -46
  99. package/src/runtime/group-join.ts +106 -106
  100. package/src/runtime/heartbeat-gradient.ts +28 -28
  101. package/src/runtime/heartbeat-watcher.ts +124 -124
  102. package/src/runtime/live-agent-control.ts +88 -87
  103. package/src/runtime/live-agent-manager.ts +103 -85
  104. package/src/runtime/live-control-realtime.ts +36 -36
  105. package/src/runtime/live-session-runtime.ts +309 -305
  106. package/src/runtime/manifest-cache.ts +17 -2
  107. package/src/runtime/model-fallback.ts +6 -4
  108. package/src/runtime/parallel-research.ts +44 -44
  109. package/src/runtime/pi-args.ts +18 -3
  110. package/src/runtime/pi-json-output.ts +111 -111
  111. package/src/runtime/policy-engine.ts +79 -79
  112. package/src/runtime/process-status.ts +5 -1
  113. package/src/runtime/progress-event-coalescer.ts +43 -43
  114. package/src/runtime/recovery-recipes.ts +74 -74
  115. package/src/runtime/retry-executor.ts +81 -64
  116. package/src/runtime/role-permission.ts +39 -39
  117. package/src/runtime/runtime-resolver.ts +22 -6
  118. package/src/runtime/session-resources.ts +25 -25
  119. package/src/runtime/session-snapshot.ts +59 -59
  120. package/src/runtime/session-usage.ts +79 -79
  121. package/src/runtime/sidechain-output.ts +29 -29
  122. package/src/runtime/skill-instructions.ts +222 -0
  123. package/src/runtime/stale-reconciler.ts +4 -14
  124. package/src/runtime/subagent-manager.ts +3 -0
  125. package/src/runtime/supervisor-contact.ts +59 -59
  126. package/src/runtime/task-display.ts +38 -38
  127. package/src/runtime/task-output-context.ts +127 -127
  128. package/src/runtime/task-runner/capabilities.ts +78 -0
  129. package/src/runtime/task-runner/live-executor.ts +105 -101
  130. package/src/runtime/task-runner/progress.ts +119 -119
  131. package/src/runtime/task-runner/prompt-builder.ts +3 -1
  132. package/src/runtime/task-runner/prompt-pipeline.ts +64 -0
  133. package/src/runtime/task-runner/result-utils.ts +14 -14
  134. package/src/runtime/task-runner/state-helpers.ts +22 -22
  135. package/src/runtime/task-runner.ts +44 -5
  136. package/src/runtime/team-runner.ts +78 -15
  137. package/src/runtime/worker-heartbeat.ts +21 -21
  138. package/src/runtime/worker-startup.ts +57 -57
  139. package/src/schema/config-schema.ts +1 -0
  140. package/src/schema/team-tool-schema.ts +3 -3
  141. package/src/state/active-run-registry.ts +165 -0
  142. package/src/state/contracts.ts +1 -1
  143. package/src/state/mailbox.ts +44 -4
  144. package/src/state/state-store.ts +8 -1
  145. package/src/state/task-claims.ts +44 -44
  146. package/src/state/types.ts +44 -2
  147. package/src/state/usage.ts +29 -29
  148. package/src/subagents/async-entry.ts +1 -1
  149. package/src/subagents/index.ts +3 -3
  150. package/src/subagents/live/control.ts +1 -1
  151. package/src/subagents/live/manager.ts +1 -1
  152. package/src/subagents/live/realtime.ts +1 -1
  153. package/src/subagents/live/session-runtime.ts +1 -1
  154. package/src/subagents/manager.ts +1 -1
  155. package/src/subagents/spawn.ts +1 -1
  156. package/src/teams/team-config.ts +1 -0
  157. package/src/teams/team-serializer.ts +38 -38
  158. package/src/types/diff.d.ts +18 -18
  159. package/src/ui/crew-footer.ts +101 -101
  160. package/src/ui/crew-select-list.ts +111 -111
  161. package/src/ui/crew-widget.ts +4 -3
  162. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  163. package/src/ui/dashboard-panes/progress-pane.ts +2 -0
  164. package/src/ui/dynamic-border.ts +25 -25
  165. package/src/ui/layout-primitives.ts +106 -106
  166. package/src/ui/loaders.ts +158 -158
  167. package/src/ui/render-diff.ts +119 -119
  168. package/src/ui/render-scheduler.ts +143 -143
  169. package/src/ui/run-snapshot-cache.ts +10 -2
  170. package/src/ui/snapshot-types.ts +2 -0
  171. package/src/ui/spinner.ts +17 -17
  172. package/src/ui/status-colors.ts +58 -58
  173. package/src/ui/syntax-highlight.ts +116 -116
  174. package/src/utils/atomic-write.ts +33 -33
  175. package/src/utils/completion-dedupe.ts +63 -63
  176. package/src/utils/frontmatter.ts +68 -68
  177. package/src/utils/git.ts +262 -262
  178. package/src/utils/ids.ts +12 -12
  179. package/src/utils/names.ts +27 -27
  180. package/src/utils/paths.ts +4 -2
  181. package/src/utils/redaction.ts +44 -44
  182. package/src/utils/safe-paths.ts +47 -47
  183. package/src/utils/sleep.ts +32 -32
  184. package/src/workflows/validate-workflow.ts +40 -40
  185. package/src/workflows/workflow-config.ts +1 -0
  186. package/src/worktree/branch-freshness.ts +45 -45
  187. package/teams/default.team.md +12 -12
  188. package/teams/fast-fix.team.md +11 -11
  189. package/teams/implementation.team.md +18 -18
  190. package/teams/parallel-research.team.md +14 -14
  191. package/teams/research.team.md +11 -11
  192. package/teams/review.team.md +12 -12
  193. package/workflows/default.workflow.md +29 -29
  194. package/workflows/fast-fix.workflow.md +22 -22
  195. package/workflows/implementation.workflow.md +38 -38
  196. package/workflows/parallel-research.workflow.md +46 -46
  197. package/workflows/research.workflow.md +22 -22
  198. package/workflows/review.workflow.md +30 -30
@@ -1,305 +1,309 @@
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
+
16
+ export interface LiveSessionSpawnInput {
17
+ manifest: TeamRunManifest;
18
+ task: TeamTaskState;
19
+ step: WorkflowStep;
20
+ agent: AgentConfig;
21
+ prompt: string;
22
+ signal?: AbortSignal;
23
+ transcriptPath?: string;
24
+ onEvent?: (event: unknown) => void;
25
+ onOutput?: (text: string) => void;
26
+ runtimeConfig?: CrewRuntimeConfig;
27
+ parentContext?: string;
28
+ parentModel?: unknown;
29
+ modelRegistry?: unknown;
30
+ modelOverride?: string;
31
+ teamRoleModel?: string;
32
+ isCurrent?: () => boolean;
33
+ }
34
+
35
+ export interface LiveSessionRunResult {
36
+ available: true;
37
+ exitCode: number | null;
38
+ stdout: string;
39
+ stderr: string;
40
+ jsonEvents: number;
41
+ usage?: UsageState;
42
+ error?: string;
43
+ }
44
+
45
+ export interface LiveSessionUnavailableResult {
46
+ available: false;
47
+ reason: string;
48
+ }
49
+
50
+ export interface LiveSessionPlannedResult {
51
+ available: true;
52
+ reason: string;
53
+ }
54
+
55
+ type LiveSessionModule = Record<string, unknown> & {
56
+ createAgentSession?: (options?: Record<string, unknown>) => Promise<{ session: LiveSessionLike; modelFallbackMessage?: string }>;
57
+ DefaultResourceLoader?: new (options: Record<string, unknown>) => { reload?: () => Promise<void> };
58
+ SessionManager?: { inMemory?: (cwd?: string) => unknown; create?: (cwd?: string, sessionDir?: string) => unknown };
59
+ SettingsManager?: { create?: (cwd?: string, agentDir?: string) => unknown };
60
+ getAgentDir?: () => string;
61
+ };
62
+
63
+ type LiveSessionLike = {
64
+ subscribe?: (listener: (event: unknown) => void) => (() => void);
65
+ prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>;
66
+ steer?: (text: string) => Promise<void>;
67
+ abort?: () => Promise<void> | void;
68
+ getStats?: () => unknown;
69
+ stats?: unknown;
70
+ bindExtensions?: (bindings?: Record<string, unknown>) => Promise<void>;
71
+ getActiveToolNames?: () => string[];
72
+ setActiveToolsByName?: (names: string[]) => void;
73
+ };
74
+
75
+ function appendTranscript(filePath: string | undefined, event: unknown): void {
76
+ if (!filePath) return;
77
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
78
+ fs.appendFileSync(filePath, `${JSON.stringify(redactSecrets(event))}\n`, "utf-8");
79
+ }
80
+
81
+ function asRecord(value: unknown): Record<string, unknown> | undefined {
82
+ return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
83
+ }
84
+
85
+ function textFromContent(content: unknown): string[] {
86
+ if (typeof content === "string") return [content];
87
+ if (!Array.isArray(content)) return [];
88
+ return content.flatMap((part) => {
89
+ const obj = asRecord(part);
90
+ if (!obj) return [];
91
+ if (obj.type === "text" && typeof obj.text === "string") return [obj.text];
92
+ if (typeof obj.content === "string") return [obj.content];
93
+ return [];
94
+ });
95
+ }
96
+
97
+ function eventText(event: unknown): string[] {
98
+ const obj = asRecord(event);
99
+ if (!obj) return [];
100
+ const text: string[] = [];
101
+ if (typeof obj.text === "string") text.push(obj.text);
102
+ text.push(...textFromContent(obj.content));
103
+ const message = asRecord(obj.message);
104
+ if (message) text.push(...textFromContent(message.content));
105
+ return text.filter((entry) => entry.trim());
106
+ }
107
+
108
+ function finalAssistantText(event: unknown): string[] {
109
+ const obj = asRecord(event);
110
+ if (!obj || obj.type !== "message_end") return [];
111
+ const message = asRecord(obj.message);
112
+ if (message?.role !== "assistant") return [];
113
+ return textFromContent(message.content);
114
+ }
115
+
116
+ function numberField(obj: Record<string, unknown> | undefined, keys: string[]): number | undefined {
117
+ if (!obj) return undefined;
118
+ for (const key of keys) {
119
+ const value = obj[key];
120
+ if (typeof value === "number" && Number.isFinite(value)) return value;
121
+ }
122
+ return undefined;
123
+ }
124
+
125
+ function modelFromRegistry(modelRegistry: unknown, modelId: string | undefined): unknown {
126
+ if (!modelId || !modelId.includes("/")) return undefined;
127
+ const registry = asRecord(modelRegistry);
128
+ const find = registry?.find;
129
+ if (typeof find !== "function") return undefined;
130
+ const [provider, ...modelParts] = modelId.split("/");
131
+ const id = modelParts.join("/");
132
+ try {
133
+ return find.call(modelRegistry, provider, id);
134
+ } catch {
135
+ return undefined;
136
+ }
137
+ }
138
+
139
+ function liveSystemPrompt(input: LiveSessionSpawnInput): string {
140
+ 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"))) : "";
141
+ return [
142
+ "# pi-crew Live Subagent",
143
+ `Run ID: ${input.manifest.runId}`,
144
+ `Task ID: ${input.task.id}`,
145
+ `Role: ${input.task.role}`,
146
+ `Agent: ${input.agent.name}`,
147
+ `Working directory: ${input.task.cwd}`,
148
+ "",
149
+ input.agent.systemPrompt || "Follow the user task exactly and report verification evidence.",
150
+ memory ? `\n${memory}` : "",
151
+ ].filter(Boolean).join("\n");
152
+ }
153
+
154
+ function filterActiveTools(session: LiveSessionLike, agent: AgentConfig): void {
155
+ if (typeof session.getActiveToolNames !== "function" || typeof session.setActiveToolsByName !== "function") return;
156
+ const recursiveTools = new Set(["team", "Team", "Agent", "get_subagent_result", "steer_subagent"]);
157
+ const allowed = agent.tools?.length ? new Set(agent.tools) : undefined;
158
+ const active = session.getActiveToolNames().filter((name) => !recursiveTools.has(name) && (!allowed || allowed.has(name)));
159
+ session.setActiveToolsByName(active);
160
+ }
161
+
162
+ function usageFromStats(stats: unknown): UsageState | undefined {
163
+ const obj = asRecord(stats);
164
+ if (!obj) return undefined;
165
+ const input = numberField(obj, ["input", "inputTokens", "input_tokens"]);
166
+ const output = numberField(obj, ["output", "outputTokens", "output_tokens"]);
167
+ const cacheRead = numberField(obj, ["cacheRead", "cache_read"]);
168
+ const cacheWrite = numberField(obj, ["cacheWrite", "cache_write"]);
169
+ const cost = numberField(obj, ["cost"]);
170
+ const turns = numberField(obj, ["turns", "turnCount", "turn_count"]);
171
+ return [input, output, cacheRead, cacheWrite, cost, turns].some((value) => value !== undefined) ? { input, output, cacheRead, cacheWrite, cost, turns } : undefined;
172
+ }
173
+
174
+ export async function probeLiveSessionRuntime(): Promise<LiveSessionUnavailableResult | LiveSessionPlannedResult> {
175
+ const availability = await isLiveSessionRuntimeAvailable();
176
+ if (!availability.available) return { available: false, reason: availability.reason ?? "Live-session runtime is unavailable." };
177
+ 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." };
178
+ }
179
+
180
+ export async function runLiveSessionTask(input: LiveSessionSpawnInput): Promise<LiveSessionRunResult> {
181
+ const isCurrent = input.isCurrent ?? (() => true);
182
+ if (process.env.PI_CREW_MOCK_LIVE_SESSION === "success") {
183
+ const agentId = `${input.manifest.runId}:${input.task.id}`;
184
+ const inherited = input.runtimeConfig?.inheritContext === true && input.parentContext ? ` with inherited context: ${input.parentContext}` : "";
185
+ const event = { type: "message_end", message: { role: "assistant", content: [{ type: "text", text: `Mock live-session success for ${input.agent.name}${inherited}` }] } };
186
+ const mockSession = { steer: async () => {}, prompt: async () => {}, abort: async () => {} };
187
+ registerLiveAgent({ agentId, runId: input.manifest.runId, taskId: input.task.id, session: mockSession, status: "running" });
188
+ appendTranscript(input.transcriptPath, event);
189
+ const sidechainPath = sidechainOutputPath(input.manifest.stateRoot, input.task.id);
190
+ writeSidechainEntry(sidechainPath, { agentId, type: "user", message: { role: "user", content: input.prompt }, cwd: input.task.cwd });
191
+ writeSidechainEntry(sidechainPath, { agentId, type: "message", message: event, cwd: input.task.cwd });
192
+ if (isCurrent()) input.onEvent?.(event);
193
+ const stdout = `Mock live-session success for ${input.agent.name}${inherited}`;
194
+ if (isCurrent()) input.onOutput?.(stdout);
195
+ updateLiveAgentStatus(agentId, "completed");
196
+ return { available: true, exitCode: 0, stdout, stderr: "", jsonEvents: 1 };
197
+ }
198
+ const availability = await isLiveSessionRuntimeAvailable();
199
+ if (!availability.available) return { available: true, exitCode: 1, stdout: "", stderr: availability.reason ?? "Live-session runtime unavailable.", jsonEvents: 0, error: availability.reason };
200
+ const mod = await import("@mariozechner/pi-coding-agent") as LiveSessionModule;
201
+ if (typeof mod.createAgentSession !== "function") return { available: true, exitCode: 1, stdout: "", stderr: "createAgentSession export is unavailable.", jsonEvents: 0, error: "createAgentSession export is unavailable." };
202
+ let session: LiveSessionLike | undefined;
203
+ let unsubscribe: (() => void) | undefined;
204
+ let unsubscribeControlRealtime: (() => void) | undefined;
205
+ let controlTimer: ReturnType<typeof setInterval> | undefined;
206
+ let stdout = "";
207
+ let jsonEvents = 0;
208
+ try {
209
+ const agentDir = typeof mod.getAgentDir === "function" ? mod.getAgentDir() : undefined;
210
+ let resourceLoader: unknown;
211
+ if (mod.DefaultResourceLoader && agentDir) {
212
+ resourceLoader = new mod.DefaultResourceLoader({
213
+ cwd: input.task.cwd,
214
+ agentDir,
215
+ noPromptTemplates: true,
216
+ noThemes: true,
217
+ noContextFiles: input.runtimeConfig?.inheritContext !== true,
218
+ systemPromptOverride: () => liveSystemPrompt(input),
219
+ appendSystemPromptOverride: () => [],
220
+ });
221
+ await (resourceLoader as { reload?: () => Promise<void> }).reload?.();
222
+ }
223
+ 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 });
224
+ const resolvedModel = modelFromRegistry(input.modelRegistry, modelRouting.candidates[0] ?? modelRouting.requested) ?? input.parentModel;
225
+ const created = await mod.createAgentSession({
226
+ cwd: input.task.cwd,
227
+ ...(agentDir ? { agentDir } : {}),
228
+ ...(resourceLoader ? { resourceLoader } : {}),
229
+ ...(mod.SessionManager?.inMemory ? { sessionManager: mod.SessionManager.inMemory(input.task.cwd) } : {}),
230
+ ...(mod.SettingsManager?.create && agentDir ? { settingsManager: mod.SettingsManager.create(input.task.cwd, agentDir) } : {}),
231
+ ...(input.modelRegistry ? { modelRegistry: input.modelRegistry } : {}),
232
+ ...(resolvedModel ? { model: resolvedModel } : {}),
233
+ ...(input.agent.thinking ? { thinkingLevel: input.agent.thinking } : {}),
234
+ });
235
+ session = created.session;
236
+ filterActiveTools(session, input.agent);
237
+ await session.bindExtensions?.({});
238
+ const agentId = `${input.manifest.runId}:${input.task.id}`;
239
+ registerLiveAgent({ agentId, runId: input.manifest.runId, taskId: input.task.id, session, status: "running" });
240
+ let controlCursor: LiveAgentControlCursor = { offset: 0 };
241
+ const seenControlRequestIds = new Set<string>();
242
+ let controlBusy = false;
243
+ const pollControl = async () => {
244
+ if (!isCurrent() || controlBusy || !session) return;
245
+ controlBusy = true;
246
+ try {
247
+ controlCursor = await applyLiveAgentControlRequests({ manifest: input.manifest, taskId: input.task.id, agentId, session, cursor: controlCursor, seenRequestIds: seenControlRequestIds });
248
+ } finally {
249
+ controlBusy = false;
250
+ }
251
+ };
252
+ unsubscribeControlRealtime = subscribeLiveControlRealtime((request) => {
253
+ if (!isCurrent() || request.runId !== input.manifest.runId || request.taskId !== input.task.id || !session) return;
254
+ void applyLiveAgentControlRequest({ request, taskId: input.task.id, agentId, session, seenRequestIds: seenControlRequestIds });
255
+ });
256
+ await pollControl();
257
+ controlTimer = setInterval(() => {
258
+ if (isCurrent()) void pollControl();
259
+ }, 500);
260
+ let turnCount = 0;
261
+ let softLimitReached = false;
262
+ const maxTurns = input.runtimeConfig?.maxTurns;
263
+ const graceTurns = input.runtimeConfig?.graceTurns ?? 5;
264
+ const sidechainPath = sidechainOutputPath(input.manifest.stateRoot, input.task.id);
265
+ writeSidechainEntry(sidechainPath, { agentId, type: "user", message: { role: "user", content: input.prompt }, cwd: input.task.cwd });
266
+ if (typeof session.subscribe === "function") {
267
+ unsubscribe = session.subscribe((event) => {
268
+ if (!isCurrent()) return;
269
+ jsonEvents += 1;
270
+ appendTranscript(input.transcriptPath, event);
271
+ const sidechainType = eventToSidechainType(event);
272
+ if (sidechainType) writeSidechainEntry(sidechainPath, { agentId, type: sidechainType, message: event, cwd: input.task.cwd });
273
+ const obj = asRecord(event);
274
+ if (obj?.type === "turn_end") {
275
+ turnCount += 1;
276
+ if (maxTurns !== undefined && !softLimitReached && turnCount >= maxTurns) {
277
+ softLimitReached = true;
278
+ void session?.steer?.("You have reached your turn limit. Wrap up immediately — provide your final answer now.");
279
+ } else if (maxTurns !== undefined && softLimitReached && turnCount >= maxTurns + graceTurns) {
280
+ void session?.abort?.();
281
+ }
282
+ }
283
+ input.onEvent?.(event);
284
+ const text = [...eventText(event), ...finalAssistantText(event)].join("\n");
285
+ if (text.trim()) {
286
+ stdout += `${text}\n`;
287
+ input.onOutput?.(text);
288
+ }
289
+ });
290
+ }
291
+ if (input.signal) {
292
+ if (input.signal.aborted) await session.abort?.();
293
+ else input.signal.addEventListener("abort", () => { void session?.abort?.(); }, { once: true });
294
+ }
295
+ const effectivePrompt = input.runtimeConfig?.inheritContext === true && input.parentContext ? `${input.parentContext}\n\n---\n# Live Subagent Task\n${input.prompt}` : input.prompt;
296
+ await session.prompt?.(effectivePrompt, { source: "api", expandPromptTemplates: false });
297
+ const usage = usageFromStats(typeof session.getStats === "function" ? session.getStats() : session.stats);
298
+ updateLiveAgentStatus(agentId, "completed");
299
+ return { available: true, exitCode: 0, stdout: stdout.trim(), stderr: created.modelFallbackMessage ?? "", jsonEvents, usage };
300
+ } catch (error) {
301
+ const message = error instanceof Error ? error.message : String(error);
302
+ updateLiveAgentStatus(`${input.manifest.runId}:${input.task.id}`, "failed");
303
+ return { available: true, exitCode: 1, stdout: stdout.trim(), stderr: message, jsonEvents, error: message };
304
+ } finally {
305
+ if (controlTimer) clearInterval(controlTimer);
306
+ unsubscribeControlRealtime?.();
307
+ unsubscribe?.();
308
+ }
309
+ }