pi-crew 0.1.30 → 0.1.32

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 (159) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +80 -27
  3. package/agents/analyst.md +11 -11
  4. package/agents/critic.md +11 -11
  5. package/agents/executor.md +11 -11
  6. package/agents/explorer.md +11 -11
  7. package/agents/planner.md +11 -11
  8. package/agents/reviewer.md +11 -11
  9. package/agents/security-reviewer.md +11 -11
  10. package/agents/test-engineer.md +11 -11
  11. package/agents/verifier.md +11 -11
  12. package/agents/writer.md +11 -11
  13. package/docs/architecture.md +173 -164
  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 -0
  19. package/docs/research-extension-system.md +324 -0
  20. package/docs/research-optimization-plan.md +548 -0
  21. package/docs/research-pi-coding-agent.md +357 -0
  22. package/docs/resource-formats.md +4 -3
  23. package/docs/runtime-flow.md +148 -148
  24. package/docs/source-runtime-refactor-map.md +83 -83
  25. package/docs/usage.md +3 -3
  26. package/index.ts +6 -6
  27. package/package.json +1 -1
  28. package/schema.json +53 -1
  29. package/skills/git-master/SKILL.md +24 -19
  30. package/skills/read-only-explorer/SKILL.md +26 -21
  31. package/skills/safe-bash/SKILL.md +21 -16
  32. package/skills/task-packet/SKILL.md +28 -23
  33. package/skills/verify-evidence/SKILL.md +27 -22
  34. package/src/agents/agent-serializer.ts +34 -34
  35. package/src/agents/discover-agents.ts +102 -102
  36. package/src/config/config.ts +14 -1
  37. package/src/config/defaults.ts +3 -2
  38. package/src/extension/cross-extension-rpc.ts +82 -82
  39. package/src/extension/import-index.ts +4 -3
  40. package/src/extension/management.ts +2 -2
  41. package/src/extension/project-init.ts +9 -7
  42. package/src/extension/register.ts +63 -0
  43. package/src/extension/registration/artifact-cleanup.ts +15 -14
  44. package/src/extension/registration/commands.ts +208 -208
  45. package/src/extension/registration/compaction-guard.ts +125 -0
  46. package/src/extension/registration/subagent-tools.ts +27 -8
  47. package/src/extension/registration/team-tool.ts +61 -44
  48. package/src/extension/result-watcher.ts +98 -98
  49. package/src/extension/run-import.ts +4 -4
  50. package/src/extension/run-index.ts +14 -14
  51. package/src/extension/run-maintenance.ts +24 -24
  52. package/src/extension/team-tool/api.ts +3 -0
  53. package/src/extension/team-tool/cancel.ts +31 -31
  54. package/src/extension/team-tool/doctor.ts +179 -178
  55. package/src/extension/team-tool/inspect.ts +41 -41
  56. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  57. package/src/extension/team-tool/plan.ts +19 -19
  58. package/src/extension/team-tool/status.ts +73 -73
  59. package/src/prompt/prompt-runtime.ts +68 -68
  60. package/src/runtime/agent-control.ts +64 -64
  61. package/src/runtime/agent-memory.ts +72 -72
  62. package/src/runtime/agent-observability.ts +113 -113
  63. package/src/runtime/async-marker.ts +26 -26
  64. package/src/runtime/background-runner.ts +53 -53
  65. package/src/runtime/crew-agent-runtime.ts +58 -58
  66. package/src/runtime/direct-run.ts +35 -35
  67. package/src/runtime/foreground-control.ts +82 -82
  68. package/src/runtime/green-contract.ts +46 -46
  69. package/src/runtime/group-join.ts +88 -88
  70. package/src/runtime/live-agent-control.ts +78 -78
  71. package/src/runtime/live-agent-manager.ts +85 -85
  72. package/src/runtime/live-control-realtime.ts +36 -36
  73. package/src/runtime/live-session-runtime.ts +299 -299
  74. package/src/runtime/manifest-cache.ts +212 -214
  75. package/src/runtime/model-fallback.ts +261 -261
  76. package/src/runtime/parallel-research.ts +44 -44
  77. package/src/runtime/parallel-utils.ts +99 -99
  78. package/src/runtime/pi-json-output.ts +111 -111
  79. package/src/runtime/pi-spawn.ts +96 -96
  80. package/src/runtime/policy-engine.ts +78 -78
  81. package/src/runtime/post-exit-stdio-guard.ts +86 -86
  82. package/src/runtime/process-status.ts +56 -56
  83. package/src/runtime/progress-event-coalescer.ts +43 -43
  84. package/src/runtime/recovery-recipes.ts +74 -74
  85. package/src/runtime/role-permission.ts +39 -39
  86. package/src/runtime/session-usage.ts +79 -79
  87. package/src/runtime/sidechain-output.ts +28 -28
  88. package/src/runtime/subagent-manager.ts +29 -2
  89. package/src/runtime/task-display.ts +38 -38
  90. package/src/runtime/task-output-context.ts +106 -106
  91. package/src/runtime/task-packet.ts +84 -84
  92. package/src/runtime/task-runner/live-executor.ts +98 -98
  93. package/src/runtime/task-runner/progress.ts +111 -111
  94. package/src/runtime/task-runner/prompt-builder.ts +72 -72
  95. package/src/runtime/task-runner/result-utils.ts +14 -14
  96. package/src/runtime/task-runner/state-helpers.ts +22 -22
  97. package/src/runtime/worker-heartbeat.ts +21 -21
  98. package/src/runtime/worker-startup.ts +57 -57
  99. package/src/schema/config-schema.ts +12 -0
  100. package/src/schema/team-tool-schema.ts +100 -100
  101. package/src/state/artifact-store.ts +108 -108
  102. package/src/state/contracts.ts +105 -105
  103. package/src/state/jsonl-writer.ts +77 -77
  104. package/src/state/state-store.ts +8 -9
  105. package/src/state/task-claims.ts +42 -42
  106. package/src/state/types.ts +180 -180
  107. package/src/state/usage.ts +29 -29
  108. package/src/subagents/async-entry.ts +1 -1
  109. package/src/subagents/index.ts +3 -3
  110. package/src/subagents/live/control.ts +1 -1
  111. package/src/subagents/live/manager.ts +1 -1
  112. package/src/subagents/live/realtime.ts +1 -1
  113. package/src/subagents/live/session-runtime.ts +1 -1
  114. package/src/subagents/manager.ts +1 -1
  115. package/src/subagents/spawn.ts +1 -1
  116. package/src/teams/discover-teams.ts +2 -2
  117. package/src/teams/team-serializer.ts +36 -36
  118. package/src/types/diff.d.ts +18 -0
  119. package/src/ui/crew-footer.ts +101 -101
  120. package/src/ui/crew-select-list.ts +111 -111
  121. package/src/ui/crew-widget.ts +285 -285
  122. package/src/ui/dynamic-border.ts +25 -25
  123. package/src/ui/layout-primitives.ts +106 -106
  124. package/src/ui/loaders.ts +158 -158
  125. package/src/ui/mascot.ts +441 -441
  126. package/src/ui/powerbar-publisher.ts +94 -94
  127. package/src/ui/render-diff.ts +119 -119
  128. package/src/ui/run-dashboard.ts +372 -372
  129. package/src/ui/status-colors.ts +54 -54
  130. package/src/ui/syntax-highlight.ts +116 -116
  131. package/src/ui/transcript-viewer.ts +302 -302
  132. package/src/utils/completion-dedupe.ts +63 -63
  133. package/src/utils/file-coalescer.ts +84 -84
  134. package/src/utils/frontmatter.ts +36 -36
  135. package/src/utils/fs-watch.ts +31 -31
  136. package/src/utils/git.ts +262 -262
  137. package/src/utils/ids.ts +12 -12
  138. package/src/utils/names.ts +26 -26
  139. package/src/utils/paths.ts +34 -7
  140. package/src/utils/sleep.ts +32 -32
  141. package/src/utils/timings.ts +31 -31
  142. package/src/utils/visual.ts +159 -159
  143. package/src/workflows/discover-workflows.ts +2 -2
  144. package/src/workflows/validate-workflow.ts +40 -40
  145. package/src/worktree/branch-freshness.ts +45 -45
  146. package/src/worktree/cleanup.ts +71 -69
  147. package/src/worktree/worktree-manager.ts +3 -1
  148. package/teams/default.team.md +12 -12
  149. package/teams/fast-fix.team.md +11 -11
  150. package/teams/implementation.team.md +18 -18
  151. package/teams/parallel-research.team.md +14 -14
  152. package/teams/research.team.md +11 -11
  153. package/teams/review.team.md +12 -12
  154. package/workflows/default.workflow.md +29 -29
  155. package/workflows/fast-fix.workflow.md +22 -22
  156. package/workflows/implementation.workflow.md +38 -38
  157. package/workflows/parallel-research.workflow.md +46 -46
  158. package/workflows/research.workflow.md +22 -22
  159. package/workflows/review.workflow.md +30 -30
@@ -1,106 +1,106 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { ArtifactDescriptor, TeamRunManifest, TeamTaskState } from "../state/types.ts";
4
- import { writeArtifact } from "../state/artifact-store.ts";
5
- import type { WorkflowStep } from "../workflows/workflow-config.ts";
6
-
7
- export interface DependencyOutputContext {
8
- dependencies: Array<{ taskId: string; title: string; status: string; result?: string; resultPath?: string }>;
9
- sharedReads: Array<{ name: string; path: string; content: string }>;
10
- }
11
-
12
- function readIfSmall(filePath: string, maxBytes = 24_000): string | undefined {
13
- try {
14
- const stat = fs.statSync(filePath);
15
- if (stat.size > maxBytes) return `${fs.readFileSync(filePath, "utf-8").slice(0, maxBytes)}\n\n...(truncated ${stat.size - maxBytes} bytes)`;
16
- return fs.readFileSync(filePath, "utf-8");
17
- } catch {
18
- return undefined;
19
- }
20
- }
21
-
22
- export function sharedPath(manifest: TeamRunManifest, name: string): string {
23
- return path.join(manifest.artifactsRoot, "shared", name);
24
- }
25
-
26
- export function collectDependencyOutputContext(manifest: TeamRunManifest, tasks: TeamTaskState[], task: TeamTaskState, step: WorkflowStep): DependencyOutputContext {
27
- const byStep = new Map(tasks.map((item) => [item.stepId, item]).filter((entry): entry is [string, TeamTaskState] => Boolean(entry[0])));
28
- const byId = new Map(tasks.map((item) => [item.id, item]));
29
- const dependencies = task.dependsOn.map((dep) => byStep.get(dep) ?? byId.get(dep)).filter((item): item is TeamTaskState => Boolean(item)).map((item) => ({
30
- taskId: item.id,
31
- title: item.title,
32
- status: item.status,
33
- resultPath: item.resultArtifact?.path,
34
- result: item.resultArtifact ? readIfSmall(item.resultArtifact.path) : undefined,
35
- }));
36
- const sharedReads = (step.reads === false ? [] : step.reads ?? []).map((name) => {
37
- const filePath = sharedPath(manifest, name);
38
- return { name, path: filePath, content: readIfSmall(filePath) ?? "" };
39
- }).filter((item) => item.content.trim().length > 0);
40
- return { dependencies, sharedReads };
41
- }
42
-
43
- export function renderDependencyOutputContext(context: DependencyOutputContext): string {
44
- const parts: string[] = [];
45
- if (context.dependencies.length) {
46
- parts.push("# Dependency Outputs", "");
47
- for (const dep of context.dependencies) {
48
- parts.push(`## ${dep.taskId} (${dep.title})`, `Status: ${dep.status}`, dep.resultPath ? `Result artifact: ${dep.resultPath}` : "", "", dep.result?.trim() || "(no result output)", "");
49
- }
50
- }
51
- if (context.sharedReads.length) {
52
- parts.push("# Shared Run Context Reads", "");
53
- for (const read of context.sharedReads) parts.push(`## shared/${read.name}`, `Path: ${read.path}`, "", read.content.trim(), "");
54
- }
55
- return parts.join("\n").trim();
56
- }
57
-
58
- export function writeTaskSharedOutput(manifest: TeamRunManifest, step: WorkflowStep, task: TeamTaskState): ArtifactDescriptor | undefined {
59
- if (step.output === false) return undefined;
60
- const name = step.output || `${task.id}.md`;
61
- const source = task.resultArtifact ? readIfSmall(task.resultArtifact.path, 80_000) : undefined;
62
- if (!source) return undefined;
63
- return writeArtifact(manifest.artifactsRoot, {
64
- kind: "metadata",
65
- relativePath: `shared/${name}`,
66
- producer: task.id,
67
- content: source.endsWith("\n") ? source : `${source}\n`,
68
- });
69
- }
70
-
71
- export function writeTaskInputsArtifact(manifest: TeamRunManifest, task: TeamTaskState, context: DependencyOutputContext): ArtifactDescriptor {
72
- return writeArtifact(manifest.artifactsRoot, {
73
- kind: "metadata",
74
- relativePath: `metadata/${task.id}.inputs.json`,
75
- producer: task.id,
76
- content: `${JSON.stringify(context, null, 2)}\n`,
77
- });
78
- }
79
-
80
- export function aggregateTaskOutputs(tasks: TeamTaskState[]): string {
81
- return tasks.map((task, index) => {
82
- const body = task.resultArtifact ? readIfSmall(task.resultArtifact.path, 40_000) : undefined;
83
- const hasBody = Boolean(body?.trim());
84
- const expectedMissing = task.resultArtifact && !fs.existsSync(task.resultArtifact.path);
85
- const status = task.status === "skipped"
86
- ? "SKIPPED"
87
- : task.status === "failed"
88
- ? `FAILED${task.exitCode !== undefined ? ` (exit code ${task.exitCode ?? "null"})` : ""}${task.error ? `: ${task.error}` : ""}`
89
- : expectedMissing
90
- ? `EMPTY OUTPUT (expected result artifact missing: ${task.resultArtifact?.path})`
91
- : !hasBody
92
- ? "EMPTY OUTPUT (no textual response returned)"
93
- : task.status.toUpperCase();
94
- return [
95
- `=== Task ${index + 1}: ${task.id} (${task.agent}) ===`,
96
- `Status: ${status}`,
97
- task.role ? `Role: ${task.role}` : "",
98
- task.resultArtifact?.path ? `Result artifact: ${task.resultArtifact.path}` : "",
99
- task.logArtifact?.path ? `Log artifact: ${task.logArtifact.path}` : "",
100
- task.transcriptArtifact?.path ? `Transcript: ${task.transcriptArtifact.path}` : "",
101
- task.usage ? `Usage: ${JSON.stringify(task.usage)}` : "",
102
- "",
103
- hasBody ? body!.trim() : status,
104
- ].filter(Boolean).join("\n");
105
- }).join("\n\n");
106
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { ArtifactDescriptor, TeamRunManifest, TeamTaskState } from "../state/types.ts";
4
+ import { writeArtifact } from "../state/artifact-store.ts";
5
+ import type { WorkflowStep } from "../workflows/workflow-config.ts";
6
+
7
+ export interface DependencyOutputContext {
8
+ dependencies: Array<{ taskId: string; title: string; status: string; result?: string; resultPath?: string }>;
9
+ sharedReads: Array<{ name: string; path: string; content: string }>;
10
+ }
11
+
12
+ function readIfSmall(filePath: string, maxBytes = 24_000): string | undefined {
13
+ try {
14
+ const stat = fs.statSync(filePath);
15
+ if (stat.size > maxBytes) return `${fs.readFileSync(filePath, "utf-8").slice(0, maxBytes)}\n\n...(truncated ${stat.size - maxBytes} bytes)`;
16
+ return fs.readFileSync(filePath, "utf-8");
17
+ } catch {
18
+ return undefined;
19
+ }
20
+ }
21
+
22
+ export function sharedPath(manifest: TeamRunManifest, name: string): string {
23
+ return path.join(manifest.artifactsRoot, "shared", name);
24
+ }
25
+
26
+ export function collectDependencyOutputContext(manifest: TeamRunManifest, tasks: TeamTaskState[], task: TeamTaskState, step: WorkflowStep): DependencyOutputContext {
27
+ const byStep = new Map(tasks.map((item) => [item.stepId, item]).filter((entry): entry is [string, TeamTaskState] => Boolean(entry[0])));
28
+ const byId = new Map(tasks.map((item) => [item.id, item]));
29
+ const dependencies = task.dependsOn.map((dep) => byStep.get(dep) ?? byId.get(dep)).filter((item): item is TeamTaskState => Boolean(item)).map((item) => ({
30
+ taskId: item.id,
31
+ title: item.title,
32
+ status: item.status,
33
+ resultPath: item.resultArtifact?.path,
34
+ result: item.resultArtifact ? readIfSmall(item.resultArtifact.path) : undefined,
35
+ }));
36
+ const sharedReads = (step.reads === false ? [] : step.reads ?? []).map((name) => {
37
+ const filePath = sharedPath(manifest, name);
38
+ return { name, path: filePath, content: readIfSmall(filePath) ?? "" };
39
+ }).filter((item) => item.content.trim().length > 0);
40
+ return { dependencies, sharedReads };
41
+ }
42
+
43
+ export function renderDependencyOutputContext(context: DependencyOutputContext): string {
44
+ const parts: string[] = [];
45
+ if (context.dependencies.length) {
46
+ parts.push("# Dependency Outputs", "");
47
+ for (const dep of context.dependencies) {
48
+ parts.push(`## ${dep.taskId} (${dep.title})`, `Status: ${dep.status}`, dep.resultPath ? `Result artifact: ${dep.resultPath}` : "", "", dep.result?.trim() || "(no result output)", "");
49
+ }
50
+ }
51
+ if (context.sharedReads.length) {
52
+ parts.push("# Shared Run Context Reads", "");
53
+ for (const read of context.sharedReads) parts.push(`## shared/${read.name}`, `Path: ${read.path}`, "", read.content.trim(), "");
54
+ }
55
+ return parts.join("\n").trim();
56
+ }
57
+
58
+ export function writeTaskSharedOutput(manifest: TeamRunManifest, step: WorkflowStep, task: TeamTaskState): ArtifactDescriptor | undefined {
59
+ if (step.output === false) return undefined;
60
+ const name = step.output || `${task.id}.md`;
61
+ const source = task.resultArtifact ? readIfSmall(task.resultArtifact.path, 80_000) : undefined;
62
+ if (!source) return undefined;
63
+ return writeArtifact(manifest.artifactsRoot, {
64
+ kind: "metadata",
65
+ relativePath: `shared/${name}`,
66
+ producer: task.id,
67
+ content: source.endsWith("\n") ? source : `${source}\n`,
68
+ });
69
+ }
70
+
71
+ export function writeTaskInputsArtifact(manifest: TeamRunManifest, task: TeamTaskState, context: DependencyOutputContext): ArtifactDescriptor {
72
+ return writeArtifact(manifest.artifactsRoot, {
73
+ kind: "metadata",
74
+ relativePath: `metadata/${task.id}.inputs.json`,
75
+ producer: task.id,
76
+ content: `${JSON.stringify(context, null, 2)}\n`,
77
+ });
78
+ }
79
+
80
+ export function aggregateTaskOutputs(tasks: TeamTaskState[]): string {
81
+ return tasks.map((task, index) => {
82
+ const body = task.resultArtifact ? readIfSmall(task.resultArtifact.path, 40_000) : undefined;
83
+ const hasBody = Boolean(body?.trim());
84
+ const expectedMissing = task.resultArtifact && !fs.existsSync(task.resultArtifact.path);
85
+ const status = task.status === "skipped"
86
+ ? "SKIPPED"
87
+ : task.status === "failed"
88
+ ? `FAILED${task.exitCode !== undefined ? ` (exit code ${task.exitCode ?? "null"})` : ""}${task.error ? `: ${task.error}` : ""}`
89
+ : expectedMissing
90
+ ? `EMPTY OUTPUT (expected result artifact missing: ${task.resultArtifact?.path})`
91
+ : !hasBody
92
+ ? "EMPTY OUTPUT (no textual response returned)"
93
+ : task.status.toUpperCase();
94
+ return [
95
+ `=== Task ${index + 1}: ${task.id} (${task.agent}) ===`,
96
+ `Status: ${status}`,
97
+ task.role ? `Role: ${task.role}` : "",
98
+ task.resultArtifact?.path ? `Result artifact: ${task.resultArtifact.path}` : "",
99
+ task.logArtifact?.path ? `Log artifact: ${task.logArtifact.path}` : "",
100
+ task.transcriptArtifact?.path ? `Transcript: ${task.transcriptArtifact.path}` : "",
101
+ task.usage ? `Usage: ${JSON.stringify(task.usage)}` : "",
102
+ "",
103
+ hasBody ? body!.trim() : status,
104
+ ].filter(Boolean).join("\n");
105
+ }).join("\n\n");
106
+ }
@@ -1,84 +1,84 @@
1
- import * as path from "node:path";
2
- import type { TeamRunManifest, TaskPacket, TaskScope, VerificationContract } from "../state/types.ts";
3
- import type { WorkflowStep } from "../workflows/workflow-config.ts";
4
-
5
- export interface BuildTaskPacketInput {
6
- manifest: TeamRunManifest;
7
- step: WorkflowStep;
8
- taskId: string;
9
- cwd: string;
10
- worktreePath?: string;
11
- }
12
-
13
- export interface TaskPacketValidationResult {
14
- valid: boolean;
15
- errors: string[];
16
- }
17
-
18
- export function inferTaskScope(step: WorkflowStep): TaskScope {
19
- const reads = step.reads === false ? [] : step.reads ?? [];
20
- if (reads.length === 1) return "single_file";
21
- if (reads.length > 1) return "module";
22
- return "workspace";
23
- }
24
-
25
- export function defaultVerificationContract(step: WorkflowStep): VerificationContract {
26
- return {
27
- requiredGreenLevel: step.verify ? "targeted" : "none",
28
- commands: [],
29
- allowManualEvidence: true,
30
- };
31
- }
32
-
33
- export function buildTaskPacket(input: BuildTaskPacketInput): TaskPacket {
34
- const scope = inferTaskScope(input.step);
35
- const reads = input.step.reads === false ? [] : input.step.reads ?? [];
36
- const scopePath = reads.length === 1 ? reads[0] : reads.length > 1 ? reads.join(", ") : undefined;
37
- return {
38
- objective: input.step.task.replaceAll("{goal}", input.manifest.goal),
39
- scope,
40
- scopePath,
41
- repo: path.basename(input.manifest.cwd) || input.manifest.cwd,
42
- worktree: input.worktreePath,
43
- branchPolicy: input.manifest.workspaceMode === "worktree" ? "Use the assigned task worktree and avoid modifying the leader checkout." : "Use the current checkout; do not create branches unless explicitly requested.",
44
- acceptanceTests: [],
45
- commitPolicy: "Do not commit unless explicitly requested by the user or workflow.",
46
- reportingContract: "Report changed files, verification evidence, blockers, and next recommended action.",
47
- escalationPolicy: "Stop and report if scope is ambiguous, destructive action is needed, permissions are missing, or verification cannot be completed.",
48
- constraints: [
49
- "Stay within the assigned task scope.",
50
- "Do not claim completion without verification evidence.",
51
- "Use mailbox/API state for coordination when available.",
52
- ],
53
- expectedArtifacts: ["prompt", "result", "verification"],
54
- verification: defaultVerificationContract(input.step),
55
- };
56
- }
57
-
58
- export function validateTaskPacket(packet: TaskPacket): TaskPacketValidationResult {
59
- const errors: string[] = [];
60
- if (!packet.objective.trim()) errors.push("objective must not be empty");
61
- if (!packet.repo.trim()) errors.push("repo must not be empty");
62
- if (!packet.branchPolicy.trim()) errors.push("branchPolicy must not be empty");
63
- if (!packet.commitPolicy.trim()) errors.push("commitPolicy must not be empty");
64
- if (!packet.reportingContract.trim()) errors.push("reportingContract must not be empty");
65
- if (!packet.escalationPolicy.trim()) errors.push("escalationPolicy must not be empty");
66
- if ((packet.scope === "module" || packet.scope === "single_file" || packet.scope === "custom") && !packet.scopePath?.trim()) {
67
- errors.push(`scopePath is required for scope '${packet.scope}'`);
68
- }
69
- for (const [index, test] of packet.acceptanceTests.entries()) {
70
- if (!test.trim()) errors.push(`acceptanceTests contains an empty value at index ${index}`);
71
- }
72
- return { valid: errors.length === 0, errors };
73
- }
74
-
75
- export function renderTaskPacket(packet: TaskPacket): string {
76
- return [
77
- "# Task Packet",
78
- "",
79
- "```json",
80
- JSON.stringify(packet, null, 2),
81
- "```",
82
- "",
83
- ].join("\n");
84
- }
1
+ import * as path from "node:path";
2
+ import type { TeamRunManifest, TaskPacket, TaskScope, VerificationContract } from "../state/types.ts";
3
+ import type { WorkflowStep } from "../workflows/workflow-config.ts";
4
+
5
+ export interface BuildTaskPacketInput {
6
+ manifest: TeamRunManifest;
7
+ step: WorkflowStep;
8
+ taskId: string;
9
+ cwd: string;
10
+ worktreePath?: string;
11
+ }
12
+
13
+ export interface TaskPacketValidationResult {
14
+ valid: boolean;
15
+ errors: string[];
16
+ }
17
+
18
+ export function inferTaskScope(step: WorkflowStep): TaskScope {
19
+ const reads = step.reads === false ? [] : step.reads ?? [];
20
+ if (reads.length === 1) return "single_file";
21
+ if (reads.length > 1) return "module";
22
+ return "workspace";
23
+ }
24
+
25
+ export function defaultVerificationContract(step: WorkflowStep): VerificationContract {
26
+ return {
27
+ requiredGreenLevel: step.verify ? "targeted" : "none",
28
+ commands: [],
29
+ allowManualEvidence: true,
30
+ };
31
+ }
32
+
33
+ export function buildTaskPacket(input: BuildTaskPacketInput): TaskPacket {
34
+ const scope = inferTaskScope(input.step);
35
+ const reads = input.step.reads === false ? [] : input.step.reads ?? [];
36
+ const scopePath = reads.length === 1 ? reads[0] : reads.length > 1 ? reads.join(", ") : undefined;
37
+ return {
38
+ objective: input.step.task.replaceAll("{goal}", input.manifest.goal),
39
+ scope,
40
+ scopePath,
41
+ repo: path.basename(input.manifest.cwd) || input.manifest.cwd,
42
+ worktree: input.worktreePath,
43
+ branchPolicy: input.manifest.workspaceMode === "worktree" ? "Use the assigned task worktree and avoid modifying the leader checkout." : "Use the current checkout; do not create branches unless explicitly requested.",
44
+ acceptanceTests: [],
45
+ commitPolicy: "Do not commit unless explicitly requested by the user or workflow.",
46
+ reportingContract: "Report changed files, verification evidence, blockers, and next recommended action.",
47
+ escalationPolicy: "Stop and report if scope is ambiguous, destructive action is needed, permissions are missing, or verification cannot be completed.",
48
+ constraints: [
49
+ "Stay within the assigned task scope.",
50
+ "Do not claim completion without verification evidence.",
51
+ "Use mailbox/API state for coordination when available.",
52
+ ],
53
+ expectedArtifacts: ["prompt", "result", "verification"],
54
+ verification: defaultVerificationContract(input.step),
55
+ };
56
+ }
57
+
58
+ export function validateTaskPacket(packet: TaskPacket): TaskPacketValidationResult {
59
+ const errors: string[] = [];
60
+ if (!packet.objective.trim()) errors.push("objective must not be empty");
61
+ if (!packet.repo.trim()) errors.push("repo must not be empty");
62
+ if (!packet.branchPolicy.trim()) errors.push("branchPolicy must not be empty");
63
+ if (!packet.commitPolicy.trim()) errors.push("commitPolicy must not be empty");
64
+ if (!packet.reportingContract.trim()) errors.push("reportingContract must not be empty");
65
+ if (!packet.escalationPolicy.trim()) errors.push("escalationPolicy must not be empty");
66
+ if ((packet.scope === "module" || packet.scope === "single_file" || packet.scope === "custom") && !packet.scopePath?.trim()) {
67
+ errors.push(`scopePath is required for scope '${packet.scope}'`);
68
+ }
69
+ for (const [index, test] of packet.acceptanceTests.entries()) {
70
+ if (!test.trim()) errors.push(`acceptanceTests contains an empty value at index ${index}`);
71
+ }
72
+ return { valid: errors.length === 0, errors };
73
+ }
74
+
75
+ export function renderTaskPacket(packet: TaskPacket): string {
76
+ return [
77
+ "# Task Packet",
78
+ "",
79
+ "```json",
80
+ JSON.stringify(packet, null, 2),
81
+ "```",
82
+ "",
83
+ ].join("\n");
84
+ }
@@ -1,98 +1,98 @@
1
- import * as fs from "node:fs";
2
- import type { AgentConfig } from "../../agents/agent-config.ts";
3
- import type { CrewRuntimeConfig } from "../../config/config.ts";
4
- import { writeArtifact } from "../../state/artifact-store.ts";
5
- import { appendEvent } from "../../state/event-log.ts";
6
- import type { ArtifactDescriptor, TeamRunManifest, TeamTaskState } from "../../state/types.ts";
7
- import type { WorkflowStep } from "../../workflows/workflow-config.ts";
8
- import { appendCrewAgentEvent, appendCrewAgentOutput, emptyCrewAgentProgress, recordFromTask, upsertCrewAgent } from "../crew-agent-records.ts";
9
- import { createStartupEvidence, type WorkerStartupEvidence } from "../worker-startup.ts";
10
- import { runLiveSessionTask } from "../live-session-runtime.ts";
11
- import { shouldAppendProgressEventUpdate, type ProgressEventSummary } from "../progress-event-coalescer.ts";
12
- import { applyAgentProgressEvent, applyUsageToProgress, progressEventSummary, shouldFlushProgressEvent } from "./progress.ts";
13
- import type { ParsedPiJsonOutput } from "../pi-json-output.ts";
14
-
15
- export interface RunLiveTaskInput {
16
- manifest: TeamRunManifest;
17
- tasks: TeamTaskState[];
18
- task: TeamTaskState;
19
- step: WorkflowStep;
20
- agent: AgentConfig;
21
- prompt: string;
22
- signal?: AbortSignal;
23
- runtimeConfig?: CrewRuntimeConfig;
24
- parentContext?: string;
25
- parentModel?: unknown;
26
- modelRegistry?: unknown;
27
- }
28
-
29
- export interface RunLiveTaskOutput {
30
- task: TeamTaskState;
31
- tasks: TeamTaskState[];
32
- startupEvidence: WorkerStartupEvidence;
33
- exitCode: number | null;
34
- error?: string;
35
- parsedOutput?: ParsedPiJsonOutput;
36
- resultArtifact: ArtifactDescriptor;
37
- logArtifact?: ArtifactDescriptor;
38
- transcriptArtifact?: ArtifactDescriptor;
39
- }
40
-
41
- function updateTask(tasks: TeamTaskState[], updated: TeamTaskState): TeamTaskState[] {
42
- return tasks.map((task) => task.id === updated.id ? updated : task);
43
- }
44
-
45
- export async function runLiveTask(input: RunLiveTaskInput): Promise<RunLiveTaskOutput> {
46
- const { manifest, step, agent, prompt } = input;
47
- let task = input.task;
48
- let tasks = input.tasks;
49
- const transcriptPath = `${manifest.artifactsRoot}/transcripts/${task.id}.jsonl`;
50
- let lastAgentRecordPersistedAt = 0;
51
- let lastRunProgressPersistedAt = 0;
52
- let lastRunProgressSummary: ProgressEventSummary | undefined;
53
- const persistLiveProgress = (event: unknown, force = false): void => {
54
- const now = Date.now();
55
- if (force || shouldFlushProgressEvent(event) || now - lastAgentRecordPersistedAt >= 500) {
56
- upsertCrewAgent(manifest, recordFromTask(manifest, task, "live-session"));
57
- lastAgentRecordPersistedAt = now;
58
- }
59
- const summary = progressEventSummary(task, event);
60
- const decision = shouldAppendProgressEventUpdate({ previous: lastRunProgressSummary, next: summary, nowMs: now, lastAppendMs: lastRunProgressPersistedAt || undefined, minIntervalMs: 1000, force });
61
- if (decision.shouldAppend) {
62
- appendEvent(manifest.eventsPath, { type: "task.progress", runId: manifest.runId, taskId: task.id, data: { ...summary, coalesceReason: decision.reason } });
63
- lastRunProgressSummary = summary;
64
- lastRunProgressPersistedAt = now;
65
- }
66
- };
67
- const attemptStartedAt = new Date();
68
- const liveResult = await runLiveSessionTask({
69
- manifest,
70
- task,
71
- step,
72
- agent,
73
- prompt,
74
- signal: input.signal,
75
- transcriptPath,
76
- runtimeConfig: input.runtimeConfig,
77
- parentContext: input.parentContext,
78
- parentModel: input.parentModel,
79
- modelRegistry: input.modelRegistry,
80
- onOutput: (text) => appendCrewAgentOutput(manifest, task.id, text),
81
- onEvent: (event) => {
82
- appendCrewAgentEvent(manifest, task.id, event);
83
- task = { ...task, agentProgress: applyAgentProgressEvent(task.agentProgress ?? emptyCrewAgentProgress(), event, task.startedAt) };
84
- tasks = updateTask(tasks, task);
85
- persistLiveProgress(event);
86
- },
87
- });
88
- const startupEvidence = createStartupEvidence({ command: "live-session", startedAt: attemptStartedAt, finishedAt: new Date(), promptSentAt: attemptStartedAt, promptAccepted: liveResult.exitCode === 0 && !liveResult.error, stderr: liveResult.stderr, error: liveResult.error, exitCode: liveResult.exitCode });
89
- const exitCode = liveResult.exitCode;
90
- const error = liveResult.error || (liveResult.exitCode && liveResult.exitCode !== 0 ? liveResult.stderr || `Live session exited with ${liveResult.exitCode}` : undefined);
91
- const parsedOutput = { finalText: liveResult.stdout, textEvents: liveResult.stdout ? [liveResult.stdout] : [], jsonEvents: liveResult.jsonEvents, usage: liveResult.usage };
92
- if (liveResult.usage) task = { ...task, usage: liveResult.usage, agentProgress: applyUsageToProgress(task.agentProgress, liveResult.usage) };
93
- persistLiveProgress({ type: "attempt_finished" }, true);
94
- const resultArtifact = writeArtifact(manifest.artifactsRoot, { kind: "result", relativePath: `results/${task.id}.txt`, content: liveResult.stdout || liveResult.stderr || "(no output)", producer: task.id });
95
- const logArtifact = writeArtifact(manifest.artifactsRoot, { kind: "log", relativePath: `logs/${task.id}.log`, content: [`runtime=live-session`, `finalExitCode=${exitCode ?? "null"}`, `jsonEvents=${liveResult.jsonEvents}`, liveResult.usage ? `usage=${JSON.stringify(liveResult.usage)}` : "", "", "STDOUT:", liveResult.stdout, "", "STDERR:", liveResult.stderr].join("\n"), producer: task.id });
96
- const transcriptArtifact = fs.existsSync(transcriptPath) ? writeArtifact(manifest.artifactsRoot, { kind: "log", relativePath: `transcripts/${task.id}.jsonl`, content: fs.readFileSync(transcriptPath, "utf-8"), producer: task.id }) : undefined;
97
- return { task, tasks, startupEvidence, exitCode, error: error || undefined, parsedOutput, resultArtifact, logArtifact, transcriptArtifact };
98
- }
1
+ import * as fs from "node:fs";
2
+ import type { AgentConfig } from "../../agents/agent-config.ts";
3
+ import type { CrewRuntimeConfig } from "../../config/config.ts";
4
+ import { writeArtifact } from "../../state/artifact-store.ts";
5
+ import { appendEvent } from "../../state/event-log.ts";
6
+ import type { ArtifactDescriptor, TeamRunManifest, TeamTaskState } from "../../state/types.ts";
7
+ import type { WorkflowStep } from "../../workflows/workflow-config.ts";
8
+ import { appendCrewAgentEvent, appendCrewAgentOutput, emptyCrewAgentProgress, recordFromTask, upsertCrewAgent } from "../crew-agent-records.ts";
9
+ import { createStartupEvidence, type WorkerStartupEvidence } from "../worker-startup.ts";
10
+ import { runLiveSessionTask } from "../live-session-runtime.ts";
11
+ import { shouldAppendProgressEventUpdate, type ProgressEventSummary } from "../progress-event-coalescer.ts";
12
+ import { applyAgentProgressEvent, applyUsageToProgress, progressEventSummary, shouldFlushProgressEvent } from "./progress.ts";
13
+ import type { ParsedPiJsonOutput } from "../pi-json-output.ts";
14
+
15
+ export interface RunLiveTaskInput {
16
+ manifest: TeamRunManifest;
17
+ tasks: TeamTaskState[];
18
+ task: TeamTaskState;
19
+ step: WorkflowStep;
20
+ agent: AgentConfig;
21
+ prompt: string;
22
+ signal?: AbortSignal;
23
+ runtimeConfig?: CrewRuntimeConfig;
24
+ parentContext?: string;
25
+ parentModel?: unknown;
26
+ modelRegistry?: unknown;
27
+ }
28
+
29
+ export interface RunLiveTaskOutput {
30
+ task: TeamTaskState;
31
+ tasks: TeamTaskState[];
32
+ startupEvidence: WorkerStartupEvidence;
33
+ exitCode: number | null;
34
+ error?: string;
35
+ parsedOutput?: ParsedPiJsonOutput;
36
+ resultArtifact: ArtifactDescriptor;
37
+ logArtifact?: ArtifactDescriptor;
38
+ transcriptArtifact?: ArtifactDescriptor;
39
+ }
40
+
41
+ function updateTask(tasks: TeamTaskState[], updated: TeamTaskState): TeamTaskState[] {
42
+ return tasks.map((task) => task.id === updated.id ? updated : task);
43
+ }
44
+
45
+ export async function runLiveTask(input: RunLiveTaskInput): Promise<RunLiveTaskOutput> {
46
+ const { manifest, step, agent, prompt } = input;
47
+ let task = input.task;
48
+ let tasks = input.tasks;
49
+ const transcriptPath = `${manifest.artifactsRoot}/transcripts/${task.id}.jsonl`;
50
+ let lastAgentRecordPersistedAt = 0;
51
+ let lastRunProgressPersistedAt = 0;
52
+ let lastRunProgressSummary: ProgressEventSummary | undefined;
53
+ const persistLiveProgress = (event: unknown, force = false): void => {
54
+ const now = Date.now();
55
+ if (force || shouldFlushProgressEvent(event) || now - lastAgentRecordPersistedAt >= 500) {
56
+ upsertCrewAgent(manifest, recordFromTask(manifest, task, "live-session"));
57
+ lastAgentRecordPersistedAt = now;
58
+ }
59
+ const summary = progressEventSummary(task, event);
60
+ const decision = shouldAppendProgressEventUpdate({ previous: lastRunProgressSummary, next: summary, nowMs: now, lastAppendMs: lastRunProgressPersistedAt || undefined, minIntervalMs: 1000, force });
61
+ if (decision.shouldAppend) {
62
+ appendEvent(manifest.eventsPath, { type: "task.progress", runId: manifest.runId, taskId: task.id, data: { ...summary, coalesceReason: decision.reason } });
63
+ lastRunProgressSummary = summary;
64
+ lastRunProgressPersistedAt = now;
65
+ }
66
+ };
67
+ const attemptStartedAt = new Date();
68
+ const liveResult = await runLiveSessionTask({
69
+ manifest,
70
+ task,
71
+ step,
72
+ agent,
73
+ prompt,
74
+ signal: input.signal,
75
+ transcriptPath,
76
+ runtimeConfig: input.runtimeConfig,
77
+ parentContext: input.parentContext,
78
+ parentModel: input.parentModel,
79
+ modelRegistry: input.modelRegistry,
80
+ onOutput: (text) => appendCrewAgentOutput(manifest, task.id, text),
81
+ onEvent: (event) => {
82
+ appendCrewAgentEvent(manifest, task.id, event);
83
+ task = { ...task, agentProgress: applyAgentProgressEvent(task.agentProgress ?? emptyCrewAgentProgress(), event, task.startedAt) };
84
+ tasks = updateTask(tasks, task);
85
+ persistLiveProgress(event);
86
+ },
87
+ });
88
+ const startupEvidence = createStartupEvidence({ command: "live-session", startedAt: attemptStartedAt, finishedAt: new Date(), promptSentAt: attemptStartedAt, promptAccepted: liveResult.exitCode === 0 && !liveResult.error, stderr: liveResult.stderr, error: liveResult.error, exitCode: liveResult.exitCode });
89
+ const exitCode = liveResult.exitCode;
90
+ const error = liveResult.error || (liveResult.exitCode && liveResult.exitCode !== 0 ? liveResult.stderr || `Live session exited with ${liveResult.exitCode}` : undefined);
91
+ const parsedOutput = { finalText: liveResult.stdout, textEvents: liveResult.stdout ? [liveResult.stdout] : [], jsonEvents: liveResult.jsonEvents, usage: liveResult.usage };
92
+ if (liveResult.usage) task = { ...task, usage: liveResult.usage, agentProgress: applyUsageToProgress(task.agentProgress, liveResult.usage) };
93
+ persistLiveProgress({ type: "attempt_finished" }, true);
94
+ const resultArtifact = writeArtifact(manifest.artifactsRoot, { kind: "result", relativePath: `results/${task.id}.txt`, content: liveResult.stdout || liveResult.stderr || "(no output)", producer: task.id });
95
+ const logArtifact = writeArtifact(manifest.artifactsRoot, { kind: "log", relativePath: `logs/${task.id}.log`, content: [`runtime=live-session`, `finalExitCode=${exitCode ?? "null"}`, `jsonEvents=${liveResult.jsonEvents}`, liveResult.usage ? `usage=${JSON.stringify(liveResult.usage)}` : "", "", "STDOUT:", liveResult.stdout, "", "STDERR:", liveResult.stderr].join("\n"), producer: task.id });
96
+ const transcriptArtifact = fs.existsSync(transcriptPath) ? writeArtifact(manifest.artifactsRoot, { kind: "log", relativePath: `transcripts/${task.id}.jsonl`, content: fs.readFileSync(transcriptPath, "utf-8"), producer: task.id }) : undefined;
97
+ return { task, tasks, startupEvidence, exitCode, error: error || undefined, parsedOutput, resultArtifact, logArtifact, transcriptArtifact };
98
+ }