pi-crew 0.1.44 → 0.1.45

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 (142) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/agents/analyst.md +11 -11
  3. package/agents/critic.md +11 -11
  4. package/agents/executor.md +11 -11
  5. package/agents/explorer.md +11 -11
  6. package/agents/planner.md +11 -11
  7. package/agents/reviewer.md +11 -11
  8. package/agents/security-reviewer.md +11 -11
  9. package/agents/test-engineer.md +11 -11
  10. package/agents/verifier.md +11 -11
  11. package/agents/writer.md +11 -11
  12. package/docs/refactor-tasks-phase3.md +394 -394
  13. package/docs/refactor-tasks-phase4.md +564 -564
  14. package/docs/refactor-tasks-phase5.md +402 -402
  15. package/docs/refactor-tasks-phase6.md +662 -662
  16. package/docs/research-extension-examples.md +297 -297
  17. package/docs/research-extension-system.md +324 -324
  18. package/docs/research-optimization-plan.md +548 -548
  19. package/docs/research-phase10-distillation.md +198 -198
  20. package/docs/research-phase11-distillation.md +201 -201
  21. package/docs/research-pi-coding-agent.md +357 -357
  22. package/docs/research-source-pi-crew-reference.md +174 -174
  23. package/docs/runtime-flow.md +148 -148
  24. package/docs/source-runtime-refactor-map.md +83 -83
  25. package/index.ts +6 -6
  26. package/package.json +1 -1
  27. package/src/agents/agent-serializer.ts +34 -34
  28. package/src/extension/cross-extension-rpc.ts +82 -82
  29. package/src/extension/register.ts +8 -1
  30. package/src/extension/registration/commands.ts +18 -2
  31. package/src/extension/registration/compaction-guard.ts +125 -125
  32. package/src/extension/registration/subagent-tools.ts +148 -148
  33. package/src/extension/registration/team-tool.ts +26 -8
  34. package/src/extension/run-bundle-schema.ts +89 -89
  35. package/src/extension/run-maintenance.ts +43 -43
  36. package/src/extension/team-tool/cancel.ts +105 -102
  37. package/src/extension/team-tool/context.ts +1 -0
  38. package/src/extension/team-tool/handle-settings.ts +188 -188
  39. package/src/extension/team-tool/inspect.ts +41 -41
  40. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  41. package/src/extension/team-tool/plan.ts +19 -19
  42. package/src/extension/team-tool/respond.ts +83 -66
  43. package/src/extension/team-tool/run.ts +1 -0
  44. package/src/i18n.ts +184 -184
  45. package/src/observability/exporters/otlp-exporter.ts +77 -77
  46. package/src/prompt/prompt-runtime.ts +72 -72
  47. package/src/runtime/agent-control.ts +63 -63
  48. package/src/runtime/agent-memory.ts +72 -72
  49. package/src/runtime/agent-observability.ts +114 -114
  50. package/src/runtime/async-marker.ts +26 -26
  51. package/src/runtime/attention-events.ts +28 -28
  52. package/src/runtime/background-runner.ts +53 -53
  53. package/src/runtime/child-pi.ts +444 -444
  54. package/src/runtime/completion-guard.ts +190 -190
  55. package/src/runtime/crew-agent-records.ts +8 -0
  56. package/src/runtime/delivery-coordinator.ts +153 -142
  57. package/src/runtime/direct-run.ts +35 -35
  58. package/src/runtime/foreground-control.ts +82 -82
  59. package/src/runtime/green-contract.ts +46 -46
  60. package/src/runtime/group-join.ts +106 -106
  61. package/src/runtime/heartbeat-gradient.ts +28 -28
  62. package/src/runtime/heartbeat-watcher.ts +124 -124
  63. package/src/runtime/live-agent-control.ts +87 -87
  64. package/src/runtime/live-agent-manager.ts +85 -85
  65. package/src/runtime/live-control-realtime.ts +36 -36
  66. package/src/runtime/live-session-runtime.ts +305 -305
  67. package/src/runtime/overflow-recovery.ts +175 -156
  68. package/src/runtime/parallel-research.ts +44 -44
  69. package/src/runtime/pi-json-output.ts +111 -111
  70. package/src/runtime/policy-engine.ts +79 -79
  71. package/src/runtime/progress-event-coalescer.ts +43 -43
  72. package/src/runtime/recovery-recipes.ts +74 -74
  73. package/src/runtime/retry-executor.ts +64 -64
  74. package/src/runtime/role-permission.ts +39 -39
  75. package/src/runtime/session-resources.ts +25 -25
  76. package/src/runtime/session-snapshot.ts +59 -59
  77. package/src/runtime/session-usage.ts +79 -79
  78. package/src/runtime/sidechain-output.ts +29 -29
  79. package/src/runtime/stale-reconciler.ts +199 -179
  80. package/src/runtime/supervisor-contact.ts +59 -59
  81. package/src/runtime/task-display.ts +38 -38
  82. package/src/runtime/task-output-context.ts +127 -127
  83. package/src/runtime/task-runner/live-executor.ts +101 -101
  84. package/src/runtime/task-runner/progress.ts +119 -119
  85. package/src/runtime/task-runner/result-utils.ts +14 -14
  86. package/src/runtime/task-runner/state-helpers.ts +22 -22
  87. package/src/runtime/team-runner.ts +13 -4
  88. package/src/runtime/worker-heartbeat.ts +21 -21
  89. package/src/runtime/worker-startup.ts +57 -57
  90. package/src/state/state-store.ts +43 -0
  91. package/src/state/task-claims.ts +44 -44
  92. package/src/state/types.ts +2 -0
  93. package/src/state/usage.ts +29 -29
  94. package/src/subagents/async-entry.ts +1 -1
  95. package/src/subagents/index.ts +3 -3
  96. package/src/subagents/live/control.ts +1 -1
  97. package/src/subagents/live/manager.ts +1 -1
  98. package/src/subagents/live/realtime.ts +1 -1
  99. package/src/subagents/live/session-runtime.ts +1 -1
  100. package/src/subagents/manager.ts +1 -1
  101. package/src/subagents/spawn.ts +1 -1
  102. package/src/teams/team-serializer.ts +38 -38
  103. package/src/types/diff.d.ts +18 -18
  104. package/src/ui/crew-footer.ts +101 -101
  105. package/src/ui/crew-select-list.ts +111 -111
  106. package/src/ui/crew-widget.ts +5 -1
  107. package/src/ui/dashboard-panes/mailbox-pane.ts +2 -1
  108. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  109. package/src/ui/dynamic-border.ts +25 -25
  110. package/src/ui/layout-primitives.ts +106 -106
  111. package/src/ui/loaders.ts +158 -158
  112. package/src/ui/powerbar-publisher.ts +1 -1
  113. package/src/ui/render-diff.ts +119 -119
  114. package/src/ui/render-scheduler.ts +143 -143
  115. package/src/ui/run-snapshot-cache.ts +56 -37
  116. package/src/ui/snapshot-types.ts +5 -0
  117. package/src/ui/spinner.ts +17 -17
  118. package/src/ui/status-colors.ts +58 -58
  119. package/src/ui/syntax-highlight.ts +116 -116
  120. package/src/utils/atomic-write.ts +33 -33
  121. package/src/utils/completion-dedupe.ts +63 -63
  122. package/src/utils/frontmatter.ts +68 -68
  123. package/src/utils/git.ts +262 -262
  124. package/src/utils/ids.ts +12 -12
  125. package/src/utils/names.ts +27 -27
  126. package/src/utils/redaction.ts +44 -44
  127. package/src/utils/safe-paths.ts +47 -47
  128. package/src/utils/sleep.ts +32 -32
  129. package/src/workflows/validate-workflow.ts +40 -40
  130. package/src/worktree/branch-freshness.ts +45 -45
  131. package/teams/default.team.md +12 -12
  132. package/teams/fast-fix.team.md +11 -11
  133. package/teams/implementation.team.md +18 -18
  134. package/teams/parallel-research.team.md +14 -14
  135. package/teams/research.team.md +11 -11
  136. package/teams/review.team.md +12 -12
  137. package/workflows/default.workflow.md +29 -29
  138. package/workflows/fast-fix.workflow.md +22 -22
  139. package/workflows/implementation.workflow.md +38 -38
  140. package/workflows/parallel-research.workflow.md +46 -46
  141. package/workflows/research.workflow.md +22 -22
  142. package/workflows/review.workflow.md +30 -30
@@ -1,114 +1,114 @@
1
- import * as fs from "node:fs";
2
- import type { TeamRunManifest } from "../state/types.ts";
3
- import { agentOutputPath, readCrewAgents } from "./crew-agent-records.ts";
4
- import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
5
-
6
- const TOOL_LABELS: Record<string, string> = {
7
- read: "reading",
8
- bash: "running command",
9
- edit: "editing",
10
- write: "writing",
11
- grep: "searching",
12
- find: "finding files",
13
- ls: "listing",
14
- };
15
-
16
- export interface TextTailResult {
17
- path: string;
18
- text: string;
19
- bytes: number;
20
- truncated: boolean;
21
- }
22
-
23
- export function readTextTail(filePath: string, maxBytes = 64_000): TextTailResult {
24
- if (!fs.existsSync(filePath)) return { path: filePath, text: "", bytes: 0, truncated: false };
25
- const stat = fs.statSync(filePath);
26
- const bytesToRead = Math.min(stat.size, Math.max(0, maxBytes));
27
- const fd = fs.openSync(filePath, "r");
28
- try {
29
- const buffer = Buffer.alloc(bytesToRead);
30
- fs.readSync(fd, buffer, 0, bytesToRead, stat.size - bytesToRead);
31
- return { path: filePath, text: buffer.toString("utf-8"), bytes: stat.size, truncated: stat.size > bytesToRead };
32
- } finally {
33
- fs.closeSync(fd);
34
- }
35
- }
36
-
37
- function compactDuration(ms: number | undefined): string | undefined {
38
- if (ms === undefined || !Number.isFinite(ms)) return undefined;
39
- if (ms < 1000) return `${Math.round(ms)}ms`;
40
- if (ms < 60_000) return `${(ms / 1000).toFixed(1)}s`;
41
- return `${Math.floor(ms / 60_000)}m${Math.floor((ms % 60_000) / 1000)}s`;
42
- }
43
-
44
- function ageBetween(start: string | undefined, end: string | undefined): string | undefined {
45
- if (!start) return undefined;
46
- const stop = end ? new Date(end).getTime() : Date.now();
47
- const ms = Math.max(0, stop - new Date(start).getTime());
48
- return compactDuration(ms);
49
- }
50
-
51
- function activityText(agent: CrewAgentRecord): string {
52
- const parts: string[] = [];
53
- if (agent.progress?.activityState) parts.push(agent.progress.activityState);
54
- if (agent.progress?.currentTool) parts.push(TOOL_LABELS[agent.progress.currentTool] ?? `tool=${agent.progress.currentTool}`);
55
- if (agent.toolUses !== undefined) parts.push(`tools=${agent.toolUses}`);
56
- if (agent.progress?.tokens !== undefined) parts.push(`tokens=${agent.progress.tokens}`);
57
- if (agent.progress?.turns !== undefined) parts.push(`turns=${agent.progress.turns}`);
58
- const duration = compactDuration(agent.progress?.durationMs) ?? ageBetween(agent.startedAt, agent.completedAt);
59
- if (duration) parts.push(duration);
60
- if (agent.progress?.failedTool) parts.push(`failedTool=${agent.progress.failedTool}`);
61
- if (agent.progress?.recentOutput?.length) parts.push(`last=${agent.progress.recentOutput.at(-1)}`);
62
- return parts.join(" ") || "idle";
63
- }
64
-
65
- function statusGlyph(status: CrewAgentRecord["status"]): string {
66
- if (status === "completed") return "✓";
67
- if (status === "failed") return "✗";
68
- if (status === "running") return "▶";
69
- if (status === "cancelled" || status === "stopped") return "■";
70
- return "·";
71
- }
72
-
73
- function outputWarning(manifest: TeamRunManifest, agent: CrewAgentRecord): string {
74
- if (agent.status !== "completed") return "";
75
- try {
76
- const outputPath = agentOutputPath(manifest, agent.taskId);
77
- if (!fs.existsSync(outputPath)) return " no-output";
78
- return fs.statSync(outputPath).size === 0 ? " no-output" : "";
79
- } catch {
80
- return " no-output";
81
- }
82
- }
83
-
84
- function agentLine(manifest: TeamRunManifest, agent: CrewAgentRecord): string {
85
- return `- ${statusGlyph(agent.status)} ${agent.taskId} ${agent.role} → ${agent.agent} · ${agent.status} · ${agent.runtime} · ${activityText(agent)}${outputWarning(manifest, agent)}${agent.error ? ` · error=${agent.error}` : ""}`;
86
- }
87
-
88
- export function buildAgentDashboard(manifest: TeamRunManifest): { text: string; groups: Record<string, CrewAgentRecord[]> } {
89
- const agents = readCrewAgents(manifest);
90
- const groups: Record<string, CrewAgentRecord[]> = {
91
- running: agents.filter((agent) => agent.status === "running"),
92
- queued: agents.filter((agent) => agent.status === "queued"),
93
- recent: agents.filter((agent) => agent.status !== "running" && agent.status !== "queued"),
94
- };
95
- const lines = [
96
- `Crew agents for ${manifest.runId}`,
97
- `Run: ${manifest.status} · ${manifest.team}/${manifest.workflow ?? "none"} · agents=${agents.length}`,
98
- `Counts: running=${groups.running.length}, queued=${groups.queued.length}, recent=${groups.recent.length}`,
99
- "",
100
- "## Running",
101
- ...(groups.running.length ? groups.running.map((agent) => agentLine(manifest, agent)) : ["- (none)"]),
102
- "",
103
- "## Queued",
104
- ...(groups.queued.length ? groups.queued.map((agent) => agentLine(manifest, agent)) : ["- (none)"]),
105
- "",
106
- "## Recent",
107
- ...(groups.recent.length ? groups.recent.slice(-10).map((agent) => agentLine(manifest, agent)) : ["- (none)"]),
108
- ];
109
- return { text: lines.join("\n"), groups };
110
- }
111
-
112
- export function readAgentOutput(manifest: TeamRunManifest, taskId: string, maxBytes?: number): TextTailResult {
113
- return readTextTail(agentOutputPath(manifest, taskId), maxBytes);
114
- }
1
+ import * as fs from "node:fs";
2
+ import type { TeamRunManifest } from "../state/types.ts";
3
+ import { agentOutputPath, readCrewAgents } from "./crew-agent-records.ts";
4
+ import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
5
+
6
+ const TOOL_LABELS: Record<string, string> = {
7
+ read: "reading",
8
+ bash: "running command",
9
+ edit: "editing",
10
+ write: "writing",
11
+ grep: "searching",
12
+ find: "finding files",
13
+ ls: "listing",
14
+ };
15
+
16
+ export interface TextTailResult {
17
+ path: string;
18
+ text: string;
19
+ bytes: number;
20
+ truncated: boolean;
21
+ }
22
+
23
+ export function readTextTail(filePath: string, maxBytes = 64_000): TextTailResult {
24
+ if (!fs.existsSync(filePath)) return { path: filePath, text: "", bytes: 0, truncated: false };
25
+ const stat = fs.statSync(filePath);
26
+ const bytesToRead = Math.min(stat.size, Math.max(0, maxBytes));
27
+ const fd = fs.openSync(filePath, "r");
28
+ try {
29
+ const buffer = Buffer.alloc(bytesToRead);
30
+ fs.readSync(fd, buffer, 0, bytesToRead, stat.size - bytesToRead);
31
+ return { path: filePath, text: buffer.toString("utf-8"), bytes: stat.size, truncated: stat.size > bytesToRead };
32
+ } finally {
33
+ fs.closeSync(fd);
34
+ }
35
+ }
36
+
37
+ function compactDuration(ms: number | undefined): string | undefined {
38
+ if (ms === undefined || !Number.isFinite(ms)) return undefined;
39
+ if (ms < 1000) return `${Math.round(ms)}ms`;
40
+ if (ms < 60_000) return `${(ms / 1000).toFixed(1)}s`;
41
+ return `${Math.floor(ms / 60_000)}m${Math.floor((ms % 60_000) / 1000)}s`;
42
+ }
43
+
44
+ function ageBetween(start: string | undefined, end: string | undefined): string | undefined {
45
+ if (!start) return undefined;
46
+ const stop = end ? new Date(end).getTime() : Date.now();
47
+ const ms = Math.max(0, stop - new Date(start).getTime());
48
+ return compactDuration(ms);
49
+ }
50
+
51
+ function activityText(agent: CrewAgentRecord): string {
52
+ const parts: string[] = [];
53
+ if (agent.progress?.activityState) parts.push(agent.progress.activityState);
54
+ if (agent.progress?.currentTool) parts.push(TOOL_LABELS[agent.progress.currentTool] ?? `tool=${agent.progress.currentTool}`);
55
+ if (agent.toolUses !== undefined) parts.push(`tools=${agent.toolUses}`);
56
+ if (agent.progress?.tokens !== undefined) parts.push(`tokens=${agent.progress.tokens}`);
57
+ if (agent.progress?.turns !== undefined) parts.push(`turns=${agent.progress.turns}`);
58
+ const duration = compactDuration(agent.progress?.durationMs) ?? ageBetween(agent.startedAt, agent.completedAt);
59
+ if (duration) parts.push(duration);
60
+ if (agent.progress?.failedTool) parts.push(`failedTool=${agent.progress.failedTool}`);
61
+ if (agent.progress?.recentOutput?.length) parts.push(`last=${agent.progress.recentOutput.at(-1)}`);
62
+ return parts.join(" ") || "idle";
63
+ }
64
+
65
+ function statusGlyph(status: CrewAgentRecord["status"]): string {
66
+ if (status === "completed") return "✓";
67
+ if (status === "failed") return "✗";
68
+ if (status === "running") return "▶";
69
+ if (status === "cancelled" || status === "stopped") return "■";
70
+ return "·";
71
+ }
72
+
73
+ function outputWarning(manifest: TeamRunManifest, agent: CrewAgentRecord): string {
74
+ if (agent.status !== "completed") return "";
75
+ try {
76
+ const outputPath = agentOutputPath(manifest, agent.taskId);
77
+ if (!fs.existsSync(outputPath)) return " no-output";
78
+ return fs.statSync(outputPath).size === 0 ? " no-output" : "";
79
+ } catch {
80
+ return " no-output";
81
+ }
82
+ }
83
+
84
+ function agentLine(manifest: TeamRunManifest, agent: CrewAgentRecord): string {
85
+ return `- ${statusGlyph(agent.status)} ${agent.taskId} ${agent.role} → ${agent.agent} · ${agent.status} · ${agent.runtime} · ${activityText(agent)}${outputWarning(manifest, agent)}${agent.error ? ` · error=${agent.error}` : ""}`;
86
+ }
87
+
88
+ export function buildAgentDashboard(manifest: TeamRunManifest): { text: string; groups: Record<string, CrewAgentRecord[]> } {
89
+ const agents = readCrewAgents(manifest);
90
+ const groups: Record<string, CrewAgentRecord[]> = {
91
+ running: agents.filter((agent) => agent.status === "running"),
92
+ queued: agents.filter((agent) => agent.status === "queued"),
93
+ recent: agents.filter((agent) => agent.status !== "running" && agent.status !== "queued"),
94
+ };
95
+ const lines = [
96
+ `Crew agents for ${manifest.runId}`,
97
+ `Run: ${manifest.status} · ${manifest.team}/${manifest.workflow ?? "none"} · agents=${agents.length}`,
98
+ `Counts: running=${groups.running.length}, queued=${groups.queued.length}, recent=${groups.recent.length}`,
99
+ "",
100
+ "## Running",
101
+ ...(groups.running.length ? groups.running.map((agent) => agentLine(manifest, agent)) : ["- (none)"]),
102
+ "",
103
+ "## Queued",
104
+ ...(groups.queued.length ? groups.queued.map((agent) => agentLine(manifest, agent)) : ["- (none)"]),
105
+ "",
106
+ "## Recent",
107
+ ...(groups.recent.length ? groups.recent.slice(-10).map((agent) => agentLine(manifest, agent)) : ["- (none)"]),
108
+ ];
109
+ return { text: lines.join("\n"), groups };
110
+ }
111
+
112
+ export function readAgentOutput(manifest: TeamRunManifest, taskId: string, maxBytes?: number): TextTailResult {
113
+ return readTextTail(agentOutputPath(manifest, taskId), maxBytes);
114
+ }
@@ -1,26 +1,26 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import { atomicWriteJson } from "../state/atomic-write.ts";
4
- import type { TeamRunManifest } from "../state/types.ts";
5
-
6
- export interface AsyncStartMarker {
7
- pid: number;
8
- startedAt: string;
9
- }
10
-
11
- export function asyncStartMarkerPath(manifest: Pick<TeamRunManifest, "stateRoot">): string {
12
- return path.join(manifest.stateRoot, "async.pid");
13
- }
14
-
15
- export function writeAsyncStartMarker(manifest: Pick<TeamRunManifest, "stateRoot">, marker: AsyncStartMarker): void {
16
- atomicWriteJson(asyncStartMarkerPath(manifest), marker);
17
- }
18
-
19
- export function hasAsyncStartMarker(manifest: Pick<TeamRunManifest, "stateRoot">): boolean {
20
- try {
21
- const raw = JSON.parse(fs.readFileSync(asyncStartMarkerPath(manifest), "utf-8")) as Partial<AsyncStartMarker>;
22
- return typeof raw.pid === "number" && Number.isInteger(raw.pid) && raw.pid > 0 && typeof raw.startedAt === "string" && raw.startedAt.length > 0;
23
- } catch {
24
- return false;
25
- }
26
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { atomicWriteJson } from "../state/atomic-write.ts";
4
+ import type { TeamRunManifest } from "../state/types.ts";
5
+
6
+ export interface AsyncStartMarker {
7
+ pid: number;
8
+ startedAt: string;
9
+ }
10
+
11
+ export function asyncStartMarkerPath(manifest: Pick<TeamRunManifest, "stateRoot">): string {
12
+ return path.join(manifest.stateRoot, "async.pid");
13
+ }
14
+
15
+ export function writeAsyncStartMarker(manifest: Pick<TeamRunManifest, "stateRoot">, marker: AsyncStartMarker): void {
16
+ atomicWriteJson(asyncStartMarkerPath(manifest), marker);
17
+ }
18
+
19
+ export function hasAsyncStartMarker(manifest: Pick<TeamRunManifest, "stateRoot">): boolean {
20
+ try {
21
+ const raw = JSON.parse(fs.readFileSync(asyncStartMarkerPath(manifest), "utf-8")) as Partial<AsyncStartMarker>;
22
+ return typeof raw.pid === "number" && Number.isInteger(raw.pid) && raw.pid > 0 && typeof raw.startedAt === "string" && raw.startedAt.length > 0;
23
+ } catch {
24
+ return false;
25
+ }
26
+ }
@@ -1,28 +1,28 @@
1
- import { appendEvent, readEvents } from "../state/event-log.ts";
2
- import type { CrewAttentionEventData, TeamRunManifest } from "../state/types.ts";
3
-
4
- export interface AppendTaskAttentionInput {
5
- manifest: TeamRunManifest;
6
- taskId?: string;
7
- message: string;
8
- data: CrewAttentionEventData;
9
- }
10
-
11
- export function appendTaskAttentionEvent(input: AppendTaskAttentionInput): boolean {
12
- const recent = readEvents(input.manifest.eventsPath).slice(-200);
13
- const dedupKey = `${input.taskId ?? ""}:${input.data.reason}:${input.data.activityState}`;
14
- const duplicate = recent.some(
15
- (event) =>
16
- event.type === "task.attention" &&
17
- `${event.taskId ?? ""}:${event.data?.reason ?? ""}:${event.data?.activityState ?? ""}` === dedupKey,
18
- );
19
- if (duplicate) return false;
20
- appendEvent(input.manifest.eventsPath, {
21
- type: "task.attention",
22
- runId: input.manifest.runId,
23
- taskId: input.taskId,
24
- message: input.message,
25
- data: { ...input.data },
26
- });
27
- return true;
28
- }
1
+ import { appendEvent, readEvents } from "../state/event-log.ts";
2
+ import type { CrewAttentionEventData, TeamRunManifest } from "../state/types.ts";
3
+
4
+ export interface AppendTaskAttentionInput {
5
+ manifest: TeamRunManifest;
6
+ taskId?: string;
7
+ message: string;
8
+ data: CrewAttentionEventData;
9
+ }
10
+
11
+ export function appendTaskAttentionEvent(input: AppendTaskAttentionInput): boolean {
12
+ const recent = readEvents(input.manifest.eventsPath).slice(-200);
13
+ const dedupKey = `${input.taskId ?? ""}:${input.data.reason}:${input.data.activityState}`;
14
+ const duplicate = recent.some(
15
+ (event) =>
16
+ event.type === "task.attention" &&
17
+ `${event.taskId ?? ""}:${event.data?.reason ?? ""}:${event.data?.activityState ?? ""}` === dedupKey,
18
+ );
19
+ if (duplicate) return false;
20
+ appendEvent(input.manifest.eventsPath, {
21
+ type: "task.attention",
22
+ runId: input.manifest.runId,
23
+ taskId: input.taskId,
24
+ message: input.message,
25
+ data: { ...input.data },
26
+ });
27
+ return true;
28
+ }
@@ -1,53 +1,53 @@
1
- import { allAgents, discoverAgents } from "../agents/discover-agents.ts";
2
- import { allTeams, discoverTeams } from "../teams/discover-teams.ts";
3
- import { appendEvent } from "../state/event-log.ts";
4
- import { loadRunManifestById, updateRunStatus } from "../state/state-store.ts";
5
- import { allWorkflows, discoverWorkflows } from "../workflows/discover-workflows.ts";
6
- import { loadConfig } from "../config/config.ts";
7
- import { executeTeamRun } from "./team-runner.ts";
8
- import { resolveCrewRuntime } from "./runtime-resolver.ts";
9
- import { directTeamAndWorkflowFromRun } from "./direct-run.ts";
10
- import { expandParallelResearchWorkflow } from "./parallel-research.ts";
11
- import { writeAsyncStartMarker } from "./async-marker.ts";
12
-
13
- function argValue(name: string): string | undefined {
14
- const index = process.argv.indexOf(name);
15
- if (index === -1) return undefined;
16
- return process.argv[index + 1];
17
- }
18
-
19
- async function main(): Promise<void> {
20
- const cwd = argValue("--cwd");
21
- const runId = argValue("--run-id");
22
- if (!cwd || !runId) throw new Error("Usage: background-runner.ts --cwd <cwd> --run-id <runId>");
23
-
24
- const loaded = loadRunManifestById(cwd, runId);
25
- if (!loaded) throw new Error(`Run '${runId}' not found.`);
26
- let { manifest, tasks } = loaded;
27
- appendEvent(manifest.eventsPath, { type: "async.started", runId: manifest.runId, data: { pid: process.pid } });
28
- writeAsyncStartMarker(manifest, { pid: process.pid, startedAt: new Date().toISOString() });
29
-
30
- try {
31
- const agents = allAgents(discoverAgents(cwd));
32
- const direct = directTeamAndWorkflowFromRun(manifest, tasks, agents);
33
- const team = direct?.team ?? allTeams(discoverTeams(cwd)).find((candidate) => candidate.name === manifest.team);
34
- if (!team) throw new Error(`Team '${manifest.team}' not found.`);
35
- const baseWorkflow = direct?.workflow ?? allWorkflows(discoverWorkflows(cwd)).find((candidate) => candidate.name === manifest.workflow);
36
- if (!baseWorkflow) throw new Error(`Workflow '${manifest.workflow ?? ""}' not found.`);
37
- const workflow = expandParallelResearchWorkflow(baseWorkflow, cwd);
38
- const loadedConfig = loadConfig(cwd);
39
- const runtime = await resolveCrewRuntime(loadedConfig.config);
40
- const executeWorkers = runtime.kind !== "scaffold";
41
- const result = await executeTeamRun({ manifest, tasks, team, workflow, agents, executeWorkers, limits: loadedConfig.config.limits, runtime, runtimeConfig: loadedConfig.config.runtime });
42
- manifest = result.manifest;
43
- tasks = result.tasks;
44
- appendEvent(manifest.eventsPath, { type: "async.completed", runId: manifest.runId, data: { status: manifest.status, tasks: tasks.length } });
45
- } catch (error) {
46
- const message = error instanceof Error ? error.message : String(error);
47
- manifest = updateRunStatus(manifest, "failed", message);
48
- appendEvent(manifest.eventsPath, { type: "async.failed", runId: manifest.runId, message });
49
- process.exitCode = 1;
50
- }
51
- }
52
-
53
- await main();
1
+ import { allAgents, discoverAgents } from "../agents/discover-agents.ts";
2
+ import { allTeams, discoverTeams } from "../teams/discover-teams.ts";
3
+ import { appendEvent } from "../state/event-log.ts";
4
+ import { loadRunManifestById, updateRunStatus } from "../state/state-store.ts";
5
+ import { allWorkflows, discoverWorkflows } from "../workflows/discover-workflows.ts";
6
+ import { loadConfig } from "../config/config.ts";
7
+ import { executeTeamRun } from "./team-runner.ts";
8
+ import { resolveCrewRuntime } from "./runtime-resolver.ts";
9
+ import { directTeamAndWorkflowFromRun } from "./direct-run.ts";
10
+ import { expandParallelResearchWorkflow } from "./parallel-research.ts";
11
+ import { writeAsyncStartMarker } from "./async-marker.ts";
12
+
13
+ function argValue(name: string): string | undefined {
14
+ const index = process.argv.indexOf(name);
15
+ if (index === -1) return undefined;
16
+ return process.argv[index + 1];
17
+ }
18
+
19
+ async function main(): Promise<void> {
20
+ const cwd = argValue("--cwd");
21
+ const runId = argValue("--run-id");
22
+ if (!cwd || !runId) throw new Error("Usage: background-runner.ts --cwd <cwd> --run-id <runId>");
23
+
24
+ const loaded = loadRunManifestById(cwd, runId);
25
+ if (!loaded) throw new Error(`Run '${runId}' not found.`);
26
+ let { manifest, tasks } = loaded;
27
+ appendEvent(manifest.eventsPath, { type: "async.started", runId: manifest.runId, data: { pid: process.pid } });
28
+ writeAsyncStartMarker(manifest, { pid: process.pid, startedAt: new Date().toISOString() });
29
+
30
+ try {
31
+ const agents = allAgents(discoverAgents(cwd));
32
+ const direct = directTeamAndWorkflowFromRun(manifest, tasks, agents);
33
+ const team = direct?.team ?? allTeams(discoverTeams(cwd)).find((candidate) => candidate.name === manifest.team);
34
+ if (!team) throw new Error(`Team '${manifest.team}' not found.`);
35
+ const baseWorkflow = direct?.workflow ?? allWorkflows(discoverWorkflows(cwd)).find((candidate) => candidate.name === manifest.workflow);
36
+ if (!baseWorkflow) throw new Error(`Workflow '${manifest.workflow ?? ""}' not found.`);
37
+ const workflow = expandParallelResearchWorkflow(baseWorkflow, cwd);
38
+ const loadedConfig = loadConfig(cwd);
39
+ const runtime = await resolveCrewRuntime(loadedConfig.config);
40
+ const executeWorkers = runtime.kind !== "scaffold";
41
+ const result = await executeTeamRun({ manifest, tasks, team, workflow, agents, executeWorkers, limits: loadedConfig.config.limits, runtime, runtimeConfig: loadedConfig.config.runtime });
42
+ manifest = result.manifest;
43
+ tasks = result.tasks;
44
+ appendEvent(manifest.eventsPath, { type: "async.completed", runId: manifest.runId, data: { status: manifest.status, tasks: tasks.length } });
45
+ } catch (error) {
46
+ const message = error instanceof Error ? error.message : String(error);
47
+ manifest = updateRunStatus(manifest, "failed", message);
48
+ appendEvent(manifest.eventsPath, { type: "async.failed", runId: manifest.runId, message });
49
+ process.exitCode = 1;
50
+ }
51
+ }
52
+
53
+ await main();