-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.ts
More file actions
445 lines (336 loc) · 16 KB
/
executor.ts
File metadata and controls
445 lines (336 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* Task executor — runs individual sprint tasks via pi subagents,
* then orchestrates a post-task review debate.
*
* Model routing:
* Frontend tasks → anthropic/claude-opus-4-6 (no thinking)
* Backend tasks → openai-codex/gpt-5.4 (high)
*
* Tracks token usage and cost from JSON mode events.
*/
import { spawn } from "node:child_process";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import type { SprintTask, TokenUsage } from "./parser.js";
// ---------------------------------------------------------------------------
// Model configuration
// ---------------------------------------------------------------------------
export interface ModelProfile {
model: string;
thinking: string;
tools: string[];
}
export const FRONTEND_PROFILE: ModelProfile = {
model: "anthropic/claude-opus-4-6",
thinking: "none",
tools: ["read", "write", "edit", "bash", "grep", "find", "ls"],
};
export const BACKEND_PROFILE: ModelProfile = {
model: "openai-codex/gpt-5.4",
thinking: "high",
tools: ["read", "write", "edit", "bash", "grep", "find", "ls"],
};
export const DEBATE_TOOLS = ["read", "grep", "find", "ls", "bash"];
export function profileForTask(task: SprintTask): ModelProfile {
return task.isFrontend ? FRONTEND_PROFILE : BACKEND_PROFILE;
}
// ---------------------------------------------------------------------------
// Pi subprocess runner
// ---------------------------------------------------------------------------
export interface SubagentResult {
output: string;
exitCode: number;
model?: string;
durationMs: number;
usage: TokenUsage;
}
function emptyUsage(): TokenUsage {
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0 };
}
function getPiInvocation(args: string[]): { command: string; args: string[] } {
const currentScript = process.argv[1];
if (currentScript && fs.existsSync(currentScript)) {
return { command: process.execPath, args: [currentScript, ...args] };
}
const execName = path.basename(process.execPath).toLowerCase();
const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName);
if (!isGenericRuntime) {
return { command: process.execPath, args };
}
return { command: "pi", args };
}
export async function runPiSubagent(opts: {
prompt: string;
cwd: string;
model?: string;
thinking?: string;
tools?: string[];
systemPromptFile?: string;
signal?: AbortSignal;
onActivity?: (activity: string) => void;
}): Promise<SubagentResult> {
const args: string[] = ["--mode", "json", "-p", "--no-session"];
if (opts.model) args.push("--model", opts.model);
if (opts.thinking) args.push("--thinking", opts.thinking);
if (opts.tools && opts.tools.length > 0) args.push("--tools", opts.tools.join(","));
if (opts.systemPromptFile) args.push("--append-system-prompt", opts.systemPromptFile);
args.push(opts.prompt);
let output = "";
let model: string | undefined;
const usage = emptyUsage();
const startTime = Date.now();
const exitCode = await new Promise<number>((resolve) => {
const invocation = getPiInvocation(args);
const proc = spawn(invocation.command, invocation.args, {
cwd: opts.cwd,
shell: false,
stdio: ["ignore", "pipe", "pipe"],
});
let buffer = "";
const processLine = (line: string) => {
if (!line.trim()) return;
try {
const event = JSON.parse(line);
if (event.type === "message_end" && event.message?.role === "assistant") {
for (const part of event.message.content || []) {
if (part.type === "text") output += part.text;
}
if (event.message.model) model = event.message.model;
// Accumulate token usage across all assistant messages (multi-turn)
const u = event.message.usage;
if (u) {
usage.input += u.input || 0;
usage.output += u.output || 0;
usage.cacheRead += u.cacheRead || 0;
usage.cacheWrite += u.cacheWrite || 0;
if (u.cost) {
usage.cost += typeof u.cost === "number" ? u.cost : (u.cost.total || 0);
}
}
}
// Track tool activity for status updates
if (event.type === "tool_execution_start" && opts.onActivity) {
const name = event.toolName ?? "";
const eArgs = event.args ?? {};
let desc = name;
if (name === "bash") {
const cmd = (eArgs.command as string) || "";
desc = `$ ${cmd.length > 50 ? cmd.slice(0, 50) + "…" : cmd}`;
} else if (name === "read" || name === "write" || name === "edit") {
desc = `${name} ${(eArgs.path as string) || ""}`;
}
opts.onActivity(desc);
}
} catch { /* skip */ }
};
proc.stdout.on("data", (data: Buffer) => {
buffer += data.toString();
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) processLine(line);
});
proc.stderr.on("data", () => {});
proc.on("close", (code: number | null) => {
if (buffer.trim()) processLine(buffer);
resolve(code ?? 1);
});
proc.on("error", () => resolve(1));
if (opts.signal) {
const kill = () => {
proc.kill("SIGTERM");
setTimeout(() => { if (!proc.killed) proc.kill("SIGKILL"); }, 5000);
};
if (opts.signal.aborted) kill();
else opts.signal.addEventListener("abort", kill, { once: true });
}
});
const durationMs = Date.now() - startTime;
return { output, exitCode, model, durationMs, usage };
}
// ---------------------------------------------------------------------------
// Helpers to write temp system prompt files
// ---------------------------------------------------------------------------
export async function writeTempPrompt(name: string, content: string): Promise<{ dir: string; path: string }> {
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "pi-sprint-"));
const p = path.join(dir, `${name}.md`);
await fs.promises.writeFile(p, content, { encoding: "utf-8", mode: 0o600 });
return { dir, path: p };
}
export function cleanupTemp(dir: string, filePath: string) {
try { fs.unlinkSync(filePath); } catch {}
try { fs.rmdirSync(dir); } catch {}
}
// ---------------------------------------------------------------------------
// Token usage helpers
// ---------------------------------------------------------------------------
export function addUsage(target: TokenUsage, source: TokenUsage): void {
target.input += source.input;
target.output += source.output;
target.cacheRead += source.cacheRead;
target.cacheWrite += source.cacheWrite;
target.cost += source.cost;
}
export function formatTokens(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
return `${n}`;
}
export function formatCost(cost: number): string {
if (cost === 0) return "$0.00";
if (cost < 0.01) return `$${cost.toFixed(4)}`;
return `$${cost.toFixed(2)}`;
}
export function formatDurationShort(ms: number): string {
const sec = Math.round(ms / 1000);
if (sec < 60) return `${sec}s`;
const min = Math.floor(sec / 60);
const rem = sec % 60;
if (min < 60) return `${min}m${rem}s`;
const hr = Math.floor(min / 60);
return `${hr}h${min % 60}m`;
}
// ---------------------------------------------------------------------------
// Shared planning & execution principle
// ---------------------------------------------------------------------------
export const BOIL_THE_LAKE_PRINCIPLE = `## Completeness Principle — Boil the Lake
AI-assisted coding makes the marginal cost of completeness near-zero. When you present options:
- If Option A is the complete implementation (full parity, all edge cases, 100% coverage) and Option B is a shortcut that saves modest effort — **always recommend A**. The delta between 80 lines and 150 lines is meaningless with CC+gstack. "Good enough" is the wrong instinct when "complete" costs minutes more.
- **Lake vs. ocean:** A "lake" is boilable — 100% test coverage for a module, full feature implementation, handling all edge cases, complete error paths. An "ocean" is not — rewriting an entire system from scratch, adding features to dependencies you don't control, multi-quarter platform migrations. Recommend boiling lakes. Flag oceans as out of scope.
- **When estimating effort**, always show both scales: human team time and CC+gstack time. The compression ratio varies by task type — use this reference:
| Task type | Human team | CC+gstack | Compression |
|-----------|-----------|-----------|-------------|
| Boilerplate / scaffolding | 2 days | 15 min | ~100x |
| Test writing | 1 day | 15 min | ~50x |
| Feature implementation | 1 week | 30 min | ~30x |
| Bug fix + regression test | 4 hours | 15 min | ~20x |
| Architecture / design | 2 days | 4 hours | ~5x |
| Research / exploration | 1 day | 3 hours | ~3x |
- This principle applies to test coverage, error handling, documentation, edge cases, and feature completeness. Don't skip the last 10% to "save time" — with AI, that 10% costs seconds.
**Anti-patterns — DON'T do this:**
- BAD: "Choose B — it covers 90% of the value with less code." (If A is only 70 lines more, choose A.)
- BAD: "We can skip edge case handling to save time." (Edge case handling costs minutes with CC.)
- BAD: "Let's defer test coverage to a follow-up PR." (Tests are the cheapest lake to boil.)
- BAD: Quoting only human-team effort: "This would take 2 weeks." (Say: "2 weeks human / ~1 hour CC.")`;
// ---------------------------------------------------------------------------
// Execute a single task
// ---------------------------------------------------------------------------
export function buildExecutionPrompt(task: SprintTask, sprintPlan: string): string {
return `You are executing Task ${task.index} of a major sprint initiative. This is a substantial task — expect to create/modify multiple files across the codebase.
## Sprint Plan (for context)
${sprintPlan}
## Your Task
**Task ${task.index}: ${task.title}**
${task.body}
## Instructions
1. Read broadly — understand the full context before making changes. Scan related files, tests, and dependencies.
2. Implement the complete task as described — this is a staff-engineer-level deliverable, not a stub or skeleton.
3. Write thorough tests — unit tests for new logic, integration tests where appropriate. Cover success and error paths.
4. Run the test suite to verify everything passes.
5. If tests fail, fix the issues before finishing.
6. Follow existing code patterns and conventions. Use the project's naming style, error handling approach, and module structure.
This task should result in meaningful, production-quality code. Do not leave TODOs or incomplete implementations.
${BOIL_THE_LAKE_PRINCIPLE}`;
}
export function buildRetryPrompt(task: SprintTask, sprintPlan: string, priorFeedback: string): string {
return `You are completing Task ${task.index} after reviewer feedback. The first pass made substantial progress — this is a refinement pass, not a redo.
## Sprint Plan (for context)
${sprintPlan}
## Your Task
**Task ${task.index}: ${task.title}**
${task.body}
## Previous Attempt Feedback
The following issues were identified. Address the specific points raised:
${priorFeedback}
## Instructions
1. Read the current state of the files — the first pass already created/modified significant code
2. Fix the specific issues raised in the feedback
3. Ensure tests pass
4. Do NOT start from scratch — build on what was already done
5. Focus only on the flagged issues — don't refactor working code
${BOIL_THE_LAKE_PRINCIPLE}`;
}
// ---------------------------------------------------------------------------
// Post-task review debate — biased toward PASS
// ---------------------------------------------------------------------------
export function buildReviewProposalPrompt(task: SprintTask, executionOutput: string, isRetry: boolean, priorFeedback: string | null): string {
const retryContext = isRetry && priorFeedback
? `\n\n## Prior Review Context\n\nThis is a RETRY. The previous attempt received this feedback:\n\n${priorFeedback}\n\nThe implementer was asked to fix these specific issues. Focus your review on whether those issues were addressed. Do NOT re-raise issues that were already flagged and fixed. New minor issues should not block a PASS.`
: "";
return `Review the implementation of Task ${task.index}: "${task.title}".
## Task Description
${task.body}
## Implementation Output
${executionOutput.slice(0, 8000)}
${retryContext}
## Review Guidelines
Your default stance is **PASS**. This is a sprint — forward progress matters more than perfection.
**PASS** the task if:
- The core functionality described in the task was implemented
- It compiles/runs without obvious crashes
- A reasonable attempt at testing was made
**NEEDS_WORK** only if there is a **blocking issue** such as:
- Core functionality is entirely missing (not just incomplete polish)
- The code has a bug that would crash at runtime
- Tests were explicitly required and completely absent
Minor style issues, incomplete docs, or missing edge cases are NOT grounds for NEEDS_WORK.
Conclude with your verdict: **PASS** or **NEEDS_WORK**.`;
}
export function buildReviewCritiquePrompt(task: SprintTask, executionOutput: string, proposalReview: string, isRetry: boolean, priorFeedback: string | null): string {
const retryContext = isRetry && priorFeedback
? `\n\n## Prior Review Context\n\nThis is a RETRY. The previous attempt had these issues:\n\n${priorFeedback}\n\nFocus your investigation on whether those specific issues were fixed. Do not re-flag resolved issues.`
: "";
return `You are the second reviewer for Task ${task.index}: "${task.title}". Another reviewer already investigated. Build on their work.
## Task Description
${task.body}
## Implementation Output (excerpt)
${executionOutput.slice(0, 4000)}
## Primary Reviewer's Findings
${proposalReview}
${retryContext}
## Your Job
The primary reviewer already investigated. Your job is to:
1. **Verify their claims** — they referenced specific files and findings. Read those files yourself. Do their claims hold up?
2. **Fill gaps they missed** — what files, tests, or integration points did they NOT look at? Investigate those.
3. **Check their blind spots** — the primary reviewer is the model that DIDN'T write this code. You DID (or the same model family did). Use your knowledge of the implementation intent to catch things an outside reviewer might misunderstand.
4. **Confirm or challenge their verdict** — if they said PASS, verify. If they said NEEDS_WORK, check if the concern is real.
Do NOT repeat what the primary reviewer already found. Add new information only.
Your default stance is **PASS**. Only flag issues you can point to in actual code.
Give your verdict: **PASS** or **NEEDS_WORK** with evidence.`;
}
export function extractVerdict(review: string): "pass" | "needs_work" {
const upper = review.toUpperCase();
if (upper.includes("NEEDS_WORK")) return "needs_work";
if (upper.includes("**PASS**") || upper.match(/\bPASS\b/)) return "pass";
return "pass";
}
// ---------------------------------------------------------------------------
// Post-task summary (LLM pass for structured record)
// ---------------------------------------------------------------------------
export function buildTaskSummaryPrompt(task: SprintTask): string {
return `Produce a structured summary of the work done for this sprint task.
## Task
**Task ${task.index}: ${task.title}**
${task.body}
## Execution Output
${(task.executionOutput || "(no output)").slice(0, 10000)}
## Reviewer Assessment
${task.reviewProposal || "(none)"}
## Reviewer Critique
${task.reviewCritique || "(none)"}
## Review Verdict
${task.reviewVerdict || "unknown"}
## Instructions
Produce a concise, structured summary in this exact format:
### Files Changed
- List each file that was created, modified, or deleted
### What Was Done
- Bullet points summarizing the concrete implementation work
### Why
- The rationale — what problem this solves, why it was prioritized
### Key Decisions
- Any non-obvious choices made during implementation (architecture, naming, patterns, trade-offs)
### Review Outcome
- One sentence summarizing reviewer consensus and any caveats
Be factual. Reference specific files, functions, and line counts where visible in the output. Do not invent details not present in the execution output.`;
}