pi-crew 0.1.37 → 0.1.39

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 (162) hide show
  1. package/AGENTS.md +1 -1
  2. package/CHANGELOG.md +27 -0
  3. package/README.md +5 -0
  4. package/agents/analyst.md +11 -11
  5. package/agents/critic.md +11 -11
  6. package/agents/executor.md +11 -11
  7. package/agents/explorer.md +11 -11
  8. package/agents/planner.md +11 -11
  9. package/agents/reviewer.md +11 -11
  10. package/agents/security-reviewer.md +11 -11
  11. package/agents/test-engineer.md +11 -11
  12. package/agents/verifier.md +11 -11
  13. package/agents/writer.md +11 -11
  14. package/docs/refactor-tasks-phase3.md +394 -394
  15. package/docs/refactor-tasks-phase4.md +564 -564
  16. package/docs/refactor-tasks-phase5.md +402 -402
  17. package/docs/refactor-tasks-phase6.md +662 -662
  18. package/docs/research-extension-examples.md +297 -297
  19. package/docs/research-extension-system.md +324 -324
  20. package/docs/research-optimization-plan.md +548 -548
  21. package/docs/research-pi-coding-agent.md +357 -357
  22. package/docs/research-source-pi-crew-reference.md +174 -174
  23. package/docs/resource-formats.md +10 -8
  24. package/docs/runtime-flow.md +148 -148
  25. package/docs/source-runtime-refactor-map.md +83 -83
  26. package/docs/usage.md +6 -0
  27. package/index.ts +6 -6
  28. package/package.json +3 -3
  29. package/schema.json +2 -2
  30. package/src/agents/agent-serializer.ts +34 -34
  31. package/src/config/config.ts +8 -4
  32. package/src/extension/cross-extension-rpc.ts +82 -82
  33. package/src/extension/import-index.ts +18 -2
  34. package/src/extension/register.ts +11 -1
  35. package/src/extension/registration/compaction-guard.ts +125 -125
  36. package/src/extension/registration/subagent-helpers.ts +30 -6
  37. package/src/extension/registration/subagent-tools.ts +8 -3
  38. package/src/extension/result-watcher.ts +98 -98
  39. package/src/extension/run-import.ts +12 -2
  40. package/src/extension/run-index.ts +12 -2
  41. package/src/extension/run-maintenance.ts +24 -24
  42. package/src/extension/team-tool/api.ts +54 -14
  43. package/src/extension/team-tool/cancel.ts +31 -31
  44. package/src/extension/team-tool/doctor.ts +179 -179
  45. package/src/extension/team-tool/inspect.ts +41 -41
  46. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  47. package/src/extension/team-tool/plan.ts +19 -19
  48. package/src/extension/team-tool/status.ts +73 -73
  49. package/src/observability/correlation.ts +35 -35
  50. package/src/observability/event-to-metric.ts +54 -54
  51. package/src/observability/exporters/adapter.ts +24 -24
  52. package/src/observability/exporters/otlp-exporter.ts +65 -65
  53. package/src/observability/exporters/prometheus-exporter.ts +47 -47
  54. package/src/observability/metric-registry.ts +72 -72
  55. package/src/observability/metric-retention.ts +46 -46
  56. package/src/observability/metric-sink.ts +51 -51
  57. package/src/observability/metrics-primitives.ts +166 -166
  58. package/src/prompt/prompt-runtime.ts +68 -68
  59. package/src/runtime/agent-control.ts +64 -64
  60. package/src/runtime/agent-memory.ts +72 -72
  61. package/src/runtime/agent-observability.ts +114 -113
  62. package/src/runtime/async-marker.ts +26 -26
  63. package/src/runtime/background-runner.ts +53 -53
  64. package/src/runtime/crash-recovery.ts +56 -56
  65. package/src/runtime/crew-agent-records.ts +54 -9
  66. package/src/runtime/crew-agent-runtime.ts +58 -58
  67. package/src/runtime/deadletter.ts +36 -36
  68. package/src/runtime/direct-run.ts +35 -35
  69. package/src/runtime/foreground-control.ts +82 -82
  70. package/src/runtime/green-contract.ts +46 -46
  71. package/src/runtime/group-join.ts +88 -88
  72. package/src/runtime/heartbeat-gradient.ts +28 -28
  73. package/src/runtime/heartbeat-watcher.ts +80 -80
  74. package/src/runtime/live-agent-control.ts +87 -78
  75. package/src/runtime/live-agent-manager.ts +85 -85
  76. package/src/runtime/live-control-realtime.ts +36 -36
  77. package/src/runtime/live-session-runtime.ts +299 -299
  78. package/src/runtime/manifest-cache.ts +248 -212
  79. package/src/runtime/model-fallback.ts +261 -261
  80. package/src/runtime/parallel-research.ts +44 -44
  81. package/src/runtime/parallel-utils.ts +99 -99
  82. package/src/runtime/pi-json-output.ts +111 -111
  83. package/src/runtime/policy-engine.ts +78 -78
  84. package/src/runtime/post-exit-stdio-guard.ts +86 -86
  85. package/src/runtime/process-status.ts +56 -56
  86. package/src/runtime/progress-event-coalescer.ts +43 -43
  87. package/src/runtime/recovery-recipes.ts +74 -74
  88. package/src/runtime/retry-executor.ts +59 -59
  89. package/src/runtime/role-permission.ts +39 -39
  90. package/src/runtime/session-usage.ts +79 -79
  91. package/src/runtime/sidechain-output.ts +28 -28
  92. package/src/runtime/subagent-manager.ts +80 -12
  93. package/src/runtime/task-display.ts +38 -38
  94. package/src/runtime/task-output-context.ts +127 -106
  95. package/src/runtime/task-runner/live-executor.ts +98 -98
  96. package/src/runtime/task-runner/progress.ts +111 -111
  97. package/src/runtime/task-runner/result-utils.ts +14 -14
  98. package/src/runtime/task-runner/state-helpers.ts +22 -22
  99. package/src/runtime/team-runner.ts +1 -1
  100. package/src/runtime/worker-heartbeat.ts +21 -21
  101. package/src/runtime/worker-startup.ts +57 -57
  102. package/src/schema/config-schema.ts +21 -21
  103. package/src/schema/team-tool-schema.ts +100 -100
  104. package/src/state/artifact-store.ts +122 -108
  105. package/src/state/contracts.ts +105 -105
  106. package/src/state/jsonl-writer.ts +77 -77
  107. package/src/state/mailbox.ts +67 -22
  108. package/src/state/state-store.ts +36 -5
  109. package/src/state/task-claims.ts +42 -42
  110. package/src/state/usage.ts +29 -29
  111. package/src/subagents/async-entry.ts +1 -1
  112. package/src/subagents/index.ts +3 -3
  113. package/src/subagents/live/control.ts +1 -1
  114. package/src/subagents/live/manager.ts +1 -1
  115. package/src/subagents/live/realtime.ts +1 -1
  116. package/src/subagents/live/session-runtime.ts +1 -1
  117. package/src/subagents/manager.ts +1 -1
  118. package/src/subagents/spawn.ts +1 -1
  119. package/src/teams/discover-teams.ts +27 -5
  120. package/src/teams/team-serializer.ts +38 -36
  121. package/src/types/diff.d.ts +18 -18
  122. package/src/ui/crew-footer.ts +101 -101
  123. package/src/ui/crew-select-list.ts +111 -111
  124. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  125. package/src/ui/dynamic-border.ts +25 -25
  126. package/src/ui/layout-primitives.ts +106 -106
  127. package/src/ui/loaders.ts +158 -158
  128. package/src/ui/mascot.ts +441 -441
  129. package/src/ui/render-diff.ts +119 -119
  130. package/src/ui/run-dashboard.ts +5 -2
  131. package/src/ui/run-snapshot-cache.ts +19 -8
  132. package/src/ui/spinner.ts +17 -17
  133. package/src/ui/status-colors.ts +54 -54
  134. package/src/ui/syntax-highlight.ts +116 -116
  135. package/src/ui/transcript-viewer.ts +15 -1
  136. package/src/utils/completion-dedupe.ts +63 -63
  137. package/src/utils/file-coalescer.ts +84 -84
  138. package/src/utils/frontmatter.ts +36 -36
  139. package/src/utils/fs-watch.ts +31 -31
  140. package/src/utils/git.ts +262 -262
  141. package/src/utils/ids.ts +12 -12
  142. package/src/utils/names.ts +26 -26
  143. package/src/utils/paths.ts +3 -2
  144. package/src/utils/safe-paths.ts +34 -0
  145. package/src/utils/sleep.ts +32 -32
  146. package/src/utils/timings.ts +31 -31
  147. package/src/utils/visual.ts +159 -159
  148. package/src/workflows/discover-workflows.ts +30 -3
  149. package/src/workflows/validate-workflow.ts +40 -40
  150. package/src/worktree/branch-freshness.ts +45 -45
  151. package/teams/default.team.md +12 -12
  152. package/teams/fast-fix.team.md +11 -11
  153. package/teams/implementation.team.md +18 -18
  154. package/teams/parallel-research.team.md +14 -14
  155. package/teams/research.team.md +11 -11
  156. package/teams/review.team.md +12 -12
  157. package/workflows/default.workflow.md +29 -29
  158. package/workflows/fast-fix.workflow.md +22 -22
  159. package/workflows/implementation.workflow.md +38 -38
  160. package/workflows/parallel-research.workflow.md +46 -46
  161. package/workflows/research.workflow.md +22 -22
  162. package/workflows/review.workflow.md +30 -30
@@ -1,56 +1,56 @@
1
- import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
2
- import type { MetricRegistry } from "../observability/metric-registry.ts";
3
- import { appendEvent, scanSequence } from "../state/event-log.ts";
4
- import { loadRunManifestById, saveRunTasks, updateRunStatus } from "../state/state-store.ts";
5
- import type { TeamTaskState } from "../state/types.ts";
6
- import { isWorkerHeartbeatStale } from "./worker-heartbeat.ts";
7
- import type { ManifestCache } from "./manifest-cache.ts";
8
- import { checkProcessLiveness } from "./process-status.ts";
9
-
10
- export interface RecoveryPlan {
11
- runId: string;
12
- resumableTasks: string[];
13
- preservedTasks: string[];
14
- lastEventSeq: number;
15
- }
16
-
17
- function isTerminalTask(task: TeamTaskState): boolean {
18
- return task.status === "completed" || task.status === "failed" || task.status === "cancelled" || task.status === "skipped";
19
- }
20
-
21
- function shouldRecoverTask(task: TeamTaskState, deadMs: number): boolean {
22
- if (task.status !== "running") return false;
23
- if (!task.heartbeat) return true;
24
- return task.heartbeat.alive === false || isWorkerHeartbeatStale(task.heartbeat, deadMs);
25
- }
26
-
27
- export function detectInterruptedRuns(cwd: string, manifestCache: ManifestCache, deadMs = 300_000): RecoveryPlan[] {
28
- const plans: RecoveryPlan[] = [];
29
- for (const manifest of manifestCache.list(50)) {
30
- if (manifest.status !== "running") continue;
31
- if (manifest.async?.pid !== undefined && checkProcessLiveness(manifest.async.pid).alive) continue;
32
- const loaded = loadRunManifestById(cwd, manifest.runId);
33
- if (!loaded) continue;
34
- const resumableTasks = loaded.tasks.filter((task) => shouldRecoverTask(task, deadMs)).map((task) => task.id);
35
- if (!resumableTasks.length) continue;
36
- plans.push({ runId: manifest.runId, resumableTasks, preservedTasks: loaded.tasks.filter(isTerminalTask).map((task) => task.id), lastEventSeq: scanSequence(loaded.manifest.eventsPath) });
37
- }
38
- return plans;
39
- }
40
-
41
- export async function applyRecoveryPlan(plan: RecoveryPlan, ctx: Pick<ExtensionContext, "cwd">, registry?: MetricRegistry): Promise<void> {
42
- const loaded = loadRunManifestById(ctx.cwd, plan.runId);
43
- if (!loaded) throw new Error(`Run '${plan.runId}' not found.`);
44
- const reset = new Set(plan.resumableTasks);
45
- const tasks = loaded.tasks.map((task) => reset.has(task.id) ? { ...task, status: "queued" as const, startedAt: undefined, finishedAt: undefined, error: undefined, heartbeat: undefined } : task);
46
- saveRunTasks(loaded.manifest, tasks);
47
- appendEvent(loaded.manifest.eventsPath, { type: "crew.run.resumed", runId: plan.runId, message: `Recovered ${plan.resumableTasks.length} interrupted task(s).`, data: { recoveredFromSeq: plan.lastEventSeq, resumableTasks: plan.resumableTasks } });
48
- registry?.counter("crew.run.count", "Total runs by status").inc({ status: "resumed" });
49
- }
50
-
51
- export function declineRecoveryPlan(plan: RecoveryPlan, ctx: Pick<ExtensionContext, "cwd">): void {
52
- const loaded = loadRunManifestById(ctx.cwd, plan.runId);
53
- if (!loaded) throw new Error(`Run '${plan.runId}' not found.`);
54
- updateRunStatus(loaded.manifest, "cancelled", "interrupted-not-resumed");
55
- appendEvent(loaded.manifest.eventsPath, { type: "crew.run.recovery_declined", runId: plan.runId, message: "Interrupted run was not resumed.", data: { recoveredFromSeq: plan.lastEventSeq } });
56
- }
1
+ import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
2
+ import type { MetricRegistry } from "../observability/metric-registry.ts";
3
+ import { appendEvent, scanSequence } from "../state/event-log.ts";
4
+ import { loadRunManifestById, saveRunTasks, updateRunStatus } from "../state/state-store.ts";
5
+ import type { TeamTaskState } from "../state/types.ts";
6
+ import { isWorkerHeartbeatStale } from "./worker-heartbeat.ts";
7
+ import type { ManifestCache } from "./manifest-cache.ts";
8
+ import { checkProcessLiveness } from "./process-status.ts";
9
+
10
+ export interface RecoveryPlan {
11
+ runId: string;
12
+ resumableTasks: string[];
13
+ preservedTasks: string[];
14
+ lastEventSeq: number;
15
+ }
16
+
17
+ function isTerminalTask(task: TeamTaskState): boolean {
18
+ return task.status === "completed" || task.status === "failed" || task.status === "cancelled" || task.status === "skipped";
19
+ }
20
+
21
+ function shouldRecoverTask(task: TeamTaskState, deadMs: number): boolean {
22
+ if (task.status !== "running") return false;
23
+ if (!task.heartbeat) return true;
24
+ return task.heartbeat.alive === false || isWorkerHeartbeatStale(task.heartbeat, deadMs);
25
+ }
26
+
27
+ export function detectInterruptedRuns(cwd: string, manifestCache: ManifestCache, deadMs = 300_000): RecoveryPlan[] {
28
+ const plans: RecoveryPlan[] = [];
29
+ for (const manifest of manifestCache.list(50)) {
30
+ if (manifest.status !== "running") continue;
31
+ if (manifest.async?.pid !== undefined && checkProcessLiveness(manifest.async.pid).alive) continue;
32
+ const loaded = loadRunManifestById(cwd, manifest.runId);
33
+ if (!loaded) continue;
34
+ const resumableTasks = loaded.tasks.filter((task) => shouldRecoverTask(task, deadMs)).map((task) => task.id);
35
+ if (!resumableTasks.length) continue;
36
+ plans.push({ runId: manifest.runId, resumableTasks, preservedTasks: loaded.tasks.filter(isTerminalTask).map((task) => task.id), lastEventSeq: scanSequence(loaded.manifest.eventsPath) });
37
+ }
38
+ return plans;
39
+ }
40
+
41
+ export async function applyRecoveryPlan(plan: RecoveryPlan, ctx: Pick<ExtensionContext, "cwd">, registry?: MetricRegistry): Promise<void> {
42
+ const loaded = loadRunManifestById(ctx.cwd, plan.runId);
43
+ if (!loaded) throw new Error(`Run '${plan.runId}' not found.`);
44
+ const reset = new Set(plan.resumableTasks);
45
+ const tasks = loaded.tasks.map((task) => reset.has(task.id) ? { ...task, status: "queued" as const, startedAt: undefined, finishedAt: undefined, error: undefined, heartbeat: undefined } : task);
46
+ saveRunTasks(loaded.manifest, tasks);
47
+ appendEvent(loaded.manifest.eventsPath, { type: "crew.run.resumed", runId: plan.runId, message: `Recovered ${plan.resumableTasks.length} interrupted task(s).`, data: { recoveredFromSeq: plan.lastEventSeq, resumableTasks: plan.resumableTasks } });
48
+ registry?.counter("crew.run.count", "Total runs by status").inc({ status: "resumed" });
49
+ }
50
+
51
+ export function declineRecoveryPlan(plan: RecoveryPlan, ctx: Pick<ExtensionContext, "cwd">): void {
52
+ const loaded = loadRunManifestById(ctx.cwd, plan.runId);
53
+ if (!loaded) throw new Error(`Run '${plan.runId}' not found.`);
54
+ updateRunStatus(loaded.manifest, "cancelled", "interrupted-not-resumed");
55
+ appendEvent(loaded.manifest.eventsPath, { type: "crew.run.recovery_declined", runId: plan.runId, message: "Interrupted run was not resumed.", data: { recoveredFromSeq: plan.lastEventSeq } });
56
+ }
@@ -6,13 +6,45 @@ import { readJsonFileCoalesced } from "../utils/file-coalescer.ts";
6
6
  import type { CrewAgentProgress, CrewAgentRecord, CrewRuntimeKind } from "./crew-agent-runtime.ts";
7
7
  import { taskStatusToAgentStatus } from "./crew-agent-runtime.ts";
8
8
  import { logInternalError } from "../utils/internal-error.ts";
9
+ import { assertSafePathId, resolveRealContainedPath } from "../utils/safe-paths.ts";
9
10
 
10
11
  export function agentsPath(manifest: TeamRunManifest): string {
11
12
  return path.join(manifest.stateRoot, "agents.json");
12
13
  }
13
14
 
15
+ export function agentsRoot(manifest: TeamRunManifest): string {
16
+ return path.join(manifest.stateRoot, "agents");
17
+ }
18
+
19
+ function safeAgentTaskId(taskId: string): string {
20
+ return assertSafePathId("taskId", taskId.includes(":") ? taskId.split(":").pop()! : taskId);
21
+ }
22
+
14
23
  export function agentStateDir(manifest: TeamRunManifest, taskId: string): string {
15
- return path.join(manifest.stateRoot, "agents", taskId);
24
+ return path.join(agentsRoot(manifest), safeAgentTaskId(taskId));
25
+ }
26
+
27
+ export function ensureAgentStateDir(manifest: TeamRunManifest, taskId: string): string {
28
+ const root = agentsRoot(manifest);
29
+ fs.mkdirSync(root, { recursive: true });
30
+ if (fs.lstatSync(root).isSymbolicLink()) throw new Error(`Invalid agents root: ${root}`);
31
+ const dir = agentStateDir(manifest, taskId);
32
+ fs.mkdirSync(dir, { recursive: true });
33
+ if (fs.lstatSync(dir).isSymbolicLink()) throw new Error(`Invalid agent state directory: ${dir}`);
34
+ resolveRealContainedPath(root, path.basename(dir));
35
+ return dir;
36
+ }
37
+
38
+ function safeExistingAgentFile(manifest: TeamRunManifest, taskId: string, fileName: string): string {
39
+ const filePath = path.join(agentStateDir(manifest, taskId), fileName);
40
+ if (!fs.existsSync(filePath)) return filePath;
41
+ if (fs.lstatSync(filePath).isSymbolicLink()) throw new Error(`Invalid agent state file: ${filePath}`);
42
+ return resolveRealContainedPath(agentsRoot(manifest), path.join(safeAgentTaskId(taskId), fileName));
43
+ }
44
+
45
+ export function agentStateFile(manifest: TeamRunManifest, taskId: string, fileName: string): string {
46
+ ensureAgentStateDir(manifest, taskId);
47
+ return safeExistingAgentFile(manifest, taskId, fileName);
16
48
  }
17
49
 
18
50
  export function agentStatusPath(manifest: TeamRunManifest, taskId: string): string {
@@ -51,13 +83,16 @@ export function upsertCrewAgent(manifest: TeamRunManifest, record: CrewAgentReco
51
83
  }
52
84
 
53
85
  export function writeCrewAgentStatus(manifest: TeamRunManifest, record: CrewAgentRecord): void {
54
- fs.mkdirSync(agentStateDir(manifest, record.taskId), { recursive: true });
86
+ ensureAgentStateDir(manifest, record.taskId);
55
87
  atomicWriteJson(agentStatusPath(manifest, record.taskId), record);
56
88
  }
57
89
 
58
90
  export function readCrewAgentStatus(manifest: TeamRunManifest, taskOrAgentId: string): CrewAgentRecord | undefined {
59
- const taskId = taskOrAgentId.includes(":") ? taskOrAgentId.split(":").pop()! : taskOrAgentId;
60
- return readJsonFile<CrewAgentRecord>(agentStatusPath(manifest, taskId));
91
+ try {
92
+ return readJsonFile<CrewAgentRecord>(safeExistingAgentFile(manifest, taskOrAgentId, "status.json"));
93
+ } catch {
94
+ return undefined;
95
+ }
61
96
  }
62
97
 
63
98
  const agentEventSeqCache = new Map<string, { size: number; mtimeMs: number; seq: number }>();
@@ -83,8 +118,8 @@ function nextAgentEventSeq(filePath: string): number {
83
118
  }
84
119
 
85
120
  export function appendCrewAgentEvent(manifest: TeamRunManifest, taskId: string, event: unknown): void {
86
- fs.mkdirSync(agentStateDir(manifest, taskId), { recursive: true });
87
- const filePath = agentEventsPath(manifest, taskId);
121
+ ensureAgentStateDir(manifest, taskId);
122
+ const filePath = agentStateFile(manifest, taskId, "events.jsonl");
88
123
  const seq = nextAgentEventSeq(filePath);
89
124
  fs.appendFileSync(filePath, `${JSON.stringify({ seq, time: new Date().toISOString(), event })}\n`, "utf-8");
90
125
  try {
@@ -105,8 +140,18 @@ export function readCrewAgentEvents(manifest: TeamRunManifest, taskId: string):
105
140
  }
106
141
 
107
142
  export function readCrewAgentEventsCursor(manifest: TeamRunManifest, taskId: string, options: CrewAgentEventCursorOptions = {}): { path: string; events: unknown[]; nextSeq: number; total: number } {
108
- const filePath = agentEventsPath(manifest, taskId);
143
+ let filePath: string;
144
+ try {
145
+ filePath = agentEventsPath(manifest, taskId);
146
+ } catch {
147
+ return { path: "", events: [], nextSeq: options.sinceSeq ?? 0, total: 0 };
148
+ }
109
149
  if (!fs.existsSync(filePath)) return { path: filePath, events: [], nextSeq: options.sinceSeq ?? 0, total: 0 };
150
+ try {
151
+ filePath = safeExistingAgentFile(manifest, taskId, "events.jsonl");
152
+ } catch {
153
+ return { path: "", events: [], nextSeq: options.sinceSeq ?? 0, total: 0 };
154
+ }
110
155
  const sinceSeq = typeof options.sinceSeq === "number" && Number.isInteger(options.sinceSeq) && options.sinceSeq >= 0 ? options.sinceSeq : 0;
111
156
  const limit = typeof options.limit === "number" && Number.isInteger(options.limit) && options.limit >= 0 ? options.limit : undefined;
112
157
  const parsed = fs.readFileSync(filePath, "utf-8").split(/\r?\n/).filter(Boolean).map((line, index) => {
@@ -126,8 +171,8 @@ export function readCrewAgentEventsCursor(manifest: TeamRunManifest, taskId: str
126
171
 
127
172
  export function appendCrewAgentOutput(manifest: TeamRunManifest, taskId: string, text: string): void {
128
173
  if (!text.trim()) return;
129
- fs.mkdirSync(agentStateDir(manifest, taskId), { recursive: true });
130
- fs.appendFileSync(agentOutputPath(manifest, taskId), `${text}\n`, "utf-8");
174
+ ensureAgentStateDir(manifest, taskId);
175
+ fs.appendFileSync(agentStateFile(manifest, taskId, "output.log"), `${text}\n`, "utf-8");
131
176
  }
132
177
 
133
178
  export function emptyCrewAgentProgress(): CrewAgentProgress {
@@ -1,58 +1,58 @@
1
- import type { TeamTaskStatus } from "../state/contracts.ts";
2
- import type { ModelRoutingState, UsageState } from "../state/types.ts";
3
-
4
- export type CrewRuntimeKind = "scaffold" | "child-process" | "live-session";
5
- export type CrewAgentStatus = "queued" | "running" | "completed" | "failed" | "cancelled" | "stopped";
6
-
7
- export interface CrewAgentRecentTool {
8
- tool: string;
9
- args?: string;
10
- endedAt: string;
11
- }
12
-
13
- export interface CrewAgentProgress {
14
- currentTool?: string;
15
- currentToolArgs?: string;
16
- currentToolStartedAt?: string;
17
- recentTools: CrewAgentRecentTool[];
18
- recentOutput: string[];
19
- toolCount: number;
20
- tokens?: number;
21
- turns?: number;
22
- durationMs?: number;
23
- lastActivityAt?: string;
24
- activityState?: "active" | "needs_attention" | "stale";
25
- failedTool?: string;
26
- }
27
-
28
- export interface CrewAgentRecord {
29
- id: string;
30
- runId: string;
31
- taskId: string;
32
- agent: string;
33
- role: string;
34
- runtime: CrewRuntimeKind;
35
- status: CrewAgentStatus;
36
- startedAt: string;
37
- completedAt?: string;
38
- resultArtifactPath?: string;
39
- transcriptPath?: string;
40
- statusPath?: string;
41
- eventsPath?: string;
42
- outputPath?: string;
43
- toolUses?: number;
44
- jsonEvents?: number;
45
- model?: string;
46
- routing?: ModelRoutingState;
47
- usage?: UsageState;
48
- progress?: CrewAgentProgress;
49
- error?: string;
50
- }
51
-
52
- export function taskStatusToAgentStatus(status: TeamTaskStatus): CrewAgentStatus {
53
- if (status === "completed") return "completed";
54
- if (status === "failed") return "failed";
55
- if (status === "cancelled" || status === "skipped") return "cancelled";
56
- if (status === "running") return "running";
57
- return "queued";
58
- }
1
+ import type { TeamTaskStatus } from "../state/contracts.ts";
2
+ import type { ModelRoutingState, UsageState } from "../state/types.ts";
3
+
4
+ export type CrewRuntimeKind = "scaffold" | "child-process" | "live-session";
5
+ export type CrewAgentStatus = "queued" | "running" | "completed" | "failed" | "cancelled" | "stopped";
6
+
7
+ export interface CrewAgentRecentTool {
8
+ tool: string;
9
+ args?: string;
10
+ endedAt: string;
11
+ }
12
+
13
+ export interface CrewAgentProgress {
14
+ currentTool?: string;
15
+ currentToolArgs?: string;
16
+ currentToolStartedAt?: string;
17
+ recentTools: CrewAgentRecentTool[];
18
+ recentOutput: string[];
19
+ toolCount: number;
20
+ tokens?: number;
21
+ turns?: number;
22
+ durationMs?: number;
23
+ lastActivityAt?: string;
24
+ activityState?: "active" | "needs_attention" | "stale";
25
+ failedTool?: string;
26
+ }
27
+
28
+ export interface CrewAgentRecord {
29
+ id: string;
30
+ runId: string;
31
+ taskId: string;
32
+ agent: string;
33
+ role: string;
34
+ runtime: CrewRuntimeKind;
35
+ status: CrewAgentStatus;
36
+ startedAt: string;
37
+ completedAt?: string;
38
+ resultArtifactPath?: string;
39
+ transcriptPath?: string;
40
+ statusPath?: string;
41
+ eventsPath?: string;
42
+ outputPath?: string;
43
+ toolUses?: number;
44
+ jsonEvents?: number;
45
+ model?: string;
46
+ routing?: ModelRoutingState;
47
+ usage?: UsageState;
48
+ progress?: CrewAgentProgress;
49
+ error?: string;
50
+ }
51
+
52
+ export function taskStatusToAgentStatus(status: TeamTaskStatus): CrewAgentStatus {
53
+ if (status === "completed") return "completed";
54
+ if (status === "failed") return "failed";
55
+ if (status === "cancelled" || status === "skipped") return "cancelled";
56
+ if (status === "running") return "running";
57
+ return "queued";
58
+ }
@@ -1,36 +1,36 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { TeamRunManifest } from "../state/types.ts";
4
-
5
- export type DeadletterReason = "max-retries" | "heartbeat-dead" | "manual";
6
-
7
- export interface DeadletterEntry {
8
- taskId: string;
9
- runId: string;
10
- reason: DeadletterReason;
11
- attempts: number;
12
- lastError?: string;
13
- timestamp: string;
14
- }
15
-
16
- export function deadletterPath(manifest: TeamRunManifest): string {
17
- return path.join(manifest.stateRoot, "deadletter.jsonl");
18
- }
19
-
20
- export function appendDeadletter(manifest: TeamRunManifest, entry: DeadletterEntry): void {
21
- fs.mkdirSync(manifest.stateRoot, { recursive: true });
22
- fs.appendFileSync(deadletterPath(manifest), `${JSON.stringify(entry)}\n`, "utf-8");
23
- }
24
-
25
- export function readDeadletter(manifest: TeamRunManifest): DeadletterEntry[] {
26
- const filePath = deadletterPath(manifest);
27
- if (!fs.existsSync(filePath)) return [];
28
- return fs.readFileSync(filePath, "utf-8").split(/\r?\n/).filter(Boolean).flatMap((line) => {
29
- try {
30
- const parsed = JSON.parse(line) as DeadletterEntry;
31
- return parsed && typeof parsed.taskId === "string" && typeof parsed.runId === "string" ? [parsed] : [];
32
- } catch {
33
- return [];
34
- }
35
- });
36
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { TeamRunManifest } from "../state/types.ts";
4
+
5
+ export type DeadletterReason = "max-retries" | "heartbeat-dead" | "manual";
6
+
7
+ export interface DeadletterEntry {
8
+ taskId: string;
9
+ runId: string;
10
+ reason: DeadletterReason;
11
+ attempts: number;
12
+ lastError?: string;
13
+ timestamp: string;
14
+ }
15
+
16
+ export function deadletterPath(manifest: TeamRunManifest): string {
17
+ return path.join(manifest.stateRoot, "deadletter.jsonl");
18
+ }
19
+
20
+ export function appendDeadletter(manifest: TeamRunManifest, entry: DeadletterEntry): void {
21
+ fs.mkdirSync(manifest.stateRoot, { recursive: true });
22
+ fs.appendFileSync(deadletterPath(manifest), `${JSON.stringify(entry)}\n`, "utf-8");
23
+ }
24
+
25
+ export function readDeadletter(manifest: TeamRunManifest): DeadletterEntry[] {
26
+ const filePath = deadletterPath(manifest);
27
+ if (!fs.existsSync(filePath)) return [];
28
+ return fs.readFileSync(filePath, "utf-8").split(/\r?\n/).filter(Boolean).flatMap((line) => {
29
+ try {
30
+ const parsed = JSON.parse(line) as DeadletterEntry;
31
+ return parsed && typeof parsed.taskId === "string" && typeof parsed.runId === "string" ? [parsed] : [];
32
+ } catch {
33
+ return [];
34
+ }
35
+ });
36
+ }
@@ -1,35 +1,35 @@
1
- import type { AgentConfig } from "../agents/agent-config.ts";
2
- import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
3
- import type { TeamConfig } from "../teams/team-config.ts";
4
- import type { WorkflowConfig } from "../workflows/workflow-config.ts";
5
-
6
- export function isDirectRun(manifest: Pick<TeamRunManifest, "team" | "workflow">): boolean {
7
- return manifest.workflow === "direct-agent";
8
- }
9
-
10
- export function directTeamAndWorkflowFromRun(manifest: TeamRunManifest, tasks: TeamTaskState[], agents: AgentConfig[]): { team: TeamConfig; workflow: WorkflowConfig } | undefined {
11
- if (!isDirectRun(manifest)) return undefined;
12
- const firstTask = tasks[0];
13
- const agentName = firstTask?.agent ?? (manifest.team.replace(/^direct-/, "") || "executor");
14
- const agent = agents.find((candidate) => candidate.name === agentName);
15
- const role = firstTask?.role ?? "agent";
16
- const stepId = firstTask?.stepId ?? "01_agent";
17
- return {
18
- team: {
19
- name: manifest.team,
20
- description: `Direct subagent run for ${agentName}`,
21
- source: "builtin",
22
- filePath: "<generated>",
23
- roles: [{ name: role, agent: agentName, description: agent?.description }],
24
- defaultWorkflow: "direct-agent",
25
- workspaceMode: manifest.workspaceMode,
26
- },
27
- workflow: {
28
- name: manifest.workflow ?? "direct-agent",
29
- description: `Direct task for ${agentName}`,
30
- source: "builtin",
31
- filePath: "<generated>",
32
- steps: [{ id: stepId, role, task: "{goal}", model: firstTask?.model }],
33
- },
34
- };
35
- }
1
+ import type { AgentConfig } from "../agents/agent-config.ts";
2
+ import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
3
+ import type { TeamConfig } from "../teams/team-config.ts";
4
+ import type { WorkflowConfig } from "../workflows/workflow-config.ts";
5
+
6
+ export function isDirectRun(manifest: Pick<TeamRunManifest, "team" | "workflow">): boolean {
7
+ return manifest.workflow === "direct-agent";
8
+ }
9
+
10
+ export function directTeamAndWorkflowFromRun(manifest: TeamRunManifest, tasks: TeamTaskState[], agents: AgentConfig[]): { team: TeamConfig; workflow: WorkflowConfig } | undefined {
11
+ if (!isDirectRun(manifest)) return undefined;
12
+ const firstTask = tasks[0];
13
+ const agentName = firstTask?.agent ?? (manifest.team.replace(/^direct-/, "") || "executor");
14
+ const agent = agents.find((candidate) => candidate.name === agentName);
15
+ const role = firstTask?.role ?? "agent";
16
+ const stepId = firstTask?.stepId ?? "01_agent";
17
+ return {
18
+ team: {
19
+ name: manifest.team,
20
+ description: `Direct subagent run for ${agentName}`,
21
+ source: "builtin",
22
+ filePath: "<generated>",
23
+ roles: [{ name: role, agent: agentName, description: agent?.description }],
24
+ defaultWorkflow: "direct-agent",
25
+ workspaceMode: manifest.workspaceMode,
26
+ },
27
+ workflow: {
28
+ name: manifest.workflow ?? "direct-agent",
29
+ description: `Direct task for ${agentName}`,
30
+ source: "builtin",
31
+ filePath: "<generated>",
32
+ steps: [{ id: stepId, role, task: "{goal}", model: firstTask?.model }],
33
+ },
34
+ };
35
+ }
@@ -1,82 +1,82 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import { appendEvent } from "../state/event-log.ts";
4
- import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
5
- import { checkProcessLiveness, isActiveRunStatus } from "./process-status.ts";
6
- import { readCrewAgents } from "./crew-agent-records.ts";
7
-
8
- export type ForegroundControlRequestType = "interrupt" | "status";
9
-
10
- export interface ForegroundControlStatus {
11
- runId: string;
12
- status: TeamRunManifest["status"];
13
- active: boolean;
14
- asyncPid?: number;
15
- asyncAlive?: boolean;
16
- runningTasks: string[];
17
- runningAgents: string[];
18
- controlPath: string;
19
- lastRequest?: ForegroundControlRequest;
20
- }
21
-
22
- export interface ForegroundControlRequest {
23
- id: string;
24
- type: ForegroundControlRequestType;
25
- createdAt: string;
26
- reason: string;
27
- acknowledged: boolean;
28
- }
29
-
30
- export function foregroundControlPath(manifest: TeamRunManifest): string {
31
- return path.join(manifest.stateRoot, "foreground-control.json");
32
- }
33
-
34
- function readLastRequest(controlPath: string): ForegroundControlRequest | undefined {
35
- if (!fs.existsSync(controlPath)) return undefined;
36
- try {
37
- const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
38
- return parsed.requests?.at(-1);
39
- } catch {
40
- return undefined;
41
- }
42
- }
43
-
44
- export function readForegroundControlStatus(manifest: TeamRunManifest, tasks: TeamTaskState[]): ForegroundControlStatus {
45
- const controlPath = foregroundControlPath(manifest);
46
- const asyncAlive = manifest.async?.pid !== undefined ? checkProcessLiveness(manifest.async.pid).alive : undefined;
47
- return {
48
- runId: manifest.runId,
49
- status: manifest.status,
50
- active: isActiveRunStatus(manifest.status),
51
- asyncPid: manifest.async?.pid,
52
- asyncAlive,
53
- runningTasks: tasks.filter((task) => task.status === "running").map((task) => task.id),
54
- runningAgents: readCrewAgents(manifest).filter((agent) => agent.status === "running").map((agent) => agent.id),
55
- controlPath,
56
- lastRequest: readLastRequest(controlPath),
57
- };
58
- }
59
-
60
- export function writeForegroundInterruptRequest(manifest: TeamRunManifest, reason = "User requested foreground interrupt."): ForegroundControlRequest {
61
- const controlPath = foregroundControlPath(manifest);
62
- let requests: ForegroundControlRequest[] = [];
63
- if (fs.existsSync(controlPath)) {
64
- try {
65
- const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
66
- requests = Array.isArray(parsed.requests) ? parsed.requests : [];
67
- } catch {
68
- requests = [];
69
- }
70
- }
71
- const request: ForegroundControlRequest = {
72
- id: `fg_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`,
73
- type: "interrupt",
74
- createdAt: new Date().toISOString(),
75
- reason,
76
- acknowledged: false,
77
- };
78
- fs.mkdirSync(path.dirname(controlPath), { recursive: true });
79
- fs.writeFileSync(controlPath, `${JSON.stringify({ requests: [...requests, request] }, null, 2)}\n`, "utf-8");
80
- appendEvent(manifest.eventsPath, { type: "foreground.interrupt_requested", runId: manifest.runId, message: reason, data: { requestId: request.id, controlPath } });
81
- return request;
82
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { appendEvent } from "../state/event-log.ts";
4
+ import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
5
+ import { checkProcessLiveness, isActiveRunStatus } from "./process-status.ts";
6
+ import { readCrewAgents } from "./crew-agent-records.ts";
7
+
8
+ export type ForegroundControlRequestType = "interrupt" | "status";
9
+
10
+ export interface ForegroundControlStatus {
11
+ runId: string;
12
+ status: TeamRunManifest["status"];
13
+ active: boolean;
14
+ asyncPid?: number;
15
+ asyncAlive?: boolean;
16
+ runningTasks: string[];
17
+ runningAgents: string[];
18
+ controlPath: string;
19
+ lastRequest?: ForegroundControlRequest;
20
+ }
21
+
22
+ export interface ForegroundControlRequest {
23
+ id: string;
24
+ type: ForegroundControlRequestType;
25
+ createdAt: string;
26
+ reason: string;
27
+ acknowledged: boolean;
28
+ }
29
+
30
+ export function foregroundControlPath(manifest: TeamRunManifest): string {
31
+ return path.join(manifest.stateRoot, "foreground-control.json");
32
+ }
33
+
34
+ function readLastRequest(controlPath: string): ForegroundControlRequest | undefined {
35
+ if (!fs.existsSync(controlPath)) return undefined;
36
+ try {
37
+ const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
38
+ return parsed.requests?.at(-1);
39
+ } catch {
40
+ return undefined;
41
+ }
42
+ }
43
+
44
+ export function readForegroundControlStatus(manifest: TeamRunManifest, tasks: TeamTaskState[]): ForegroundControlStatus {
45
+ const controlPath = foregroundControlPath(manifest);
46
+ const asyncAlive = manifest.async?.pid !== undefined ? checkProcessLiveness(manifest.async.pid).alive : undefined;
47
+ return {
48
+ runId: manifest.runId,
49
+ status: manifest.status,
50
+ active: isActiveRunStatus(manifest.status),
51
+ asyncPid: manifest.async?.pid,
52
+ asyncAlive,
53
+ runningTasks: tasks.filter((task) => task.status === "running").map((task) => task.id),
54
+ runningAgents: readCrewAgents(manifest).filter((agent) => agent.status === "running").map((agent) => agent.id),
55
+ controlPath,
56
+ lastRequest: readLastRequest(controlPath),
57
+ };
58
+ }
59
+
60
+ export function writeForegroundInterruptRequest(manifest: TeamRunManifest, reason = "User requested foreground interrupt."): ForegroundControlRequest {
61
+ const controlPath = foregroundControlPath(manifest);
62
+ let requests: ForegroundControlRequest[] = [];
63
+ if (fs.existsSync(controlPath)) {
64
+ try {
65
+ const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
66
+ requests = Array.isArray(parsed.requests) ? parsed.requests : [];
67
+ } catch {
68
+ requests = [];
69
+ }
70
+ }
71
+ const request: ForegroundControlRequest = {
72
+ id: `fg_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`,
73
+ type: "interrupt",
74
+ createdAt: new Date().toISOString(),
75
+ reason,
76
+ acknowledged: false,
77
+ };
78
+ fs.mkdirSync(path.dirname(controlPath), { recursive: true });
79
+ fs.writeFileSync(controlPath, `${JSON.stringify({ requests: [...requests, request] }, null, 2)}\n`, "utf-8");
80
+ appendEvent(manifest.eventsPath, { type: "foreground.interrupt_requested", runId: manifest.runId, message: reason, data: { requestId: request.id, controlPath } });
81
+ return request;
82
+ }