rafcode 1.2.0 → 1.3.2
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.
- package/RAF/019-verbose-chronicle/decisions.md +25 -0
- package/RAF/019-verbose-chronicle/input.md +3 -0
- package/RAF/019-verbose-chronicle/outcomes/001-amend-iteration-references.md +25 -0
- package/RAF/019-verbose-chronicle/outcomes/002-verbose-task-name-display.md +31 -0
- package/RAF/019-verbose-chronicle/outcomes/003-verbose-streaming-fix.md +48 -0
- package/RAF/019-verbose-chronicle/outcomes/004-commit-verification-before-halt.md +56 -0
- package/RAF/019-verbose-chronicle/plans/001-amend-iteration-references.md +35 -0
- package/RAF/019-verbose-chronicle/plans/002-verbose-task-name-display.md +38 -0
- package/RAF/019-verbose-chronicle/plans/003-verbose-streaming-fix.md +45 -0
- package/RAF/019-verbose-chronicle/plans/004-commit-verification-before-halt.md +62 -0
- package/dist/commands/do.js +19 -11
- package/dist/commands/do.js.map +1 -1
- package/dist/core/claude-runner.d.ts +52 -1
- package/dist/core/claude-runner.d.ts.map +1 -1
- package/dist/core/claude-runner.js +195 -17
- package/dist/core/claude-runner.js.map +1 -1
- package/dist/core/git.d.ts +15 -0
- package/dist/core/git.d.ts.map +1 -1
- package/dist/core/git.js +44 -0
- package/dist/core/git.js.map +1 -1
- package/dist/parsers/stream-renderer.d.ts +42 -0
- package/dist/parsers/stream-renderer.d.ts.map +1 -0
- package/dist/parsers/stream-renderer.js +100 -0
- package/dist/parsers/stream-renderer.js.map +1 -0
- package/dist/prompts/amend.d.ts.map +1 -1
- package/dist/prompts/amend.js +13 -2
- package/dist/prompts/amend.js.map +1 -1
- package/dist/prompts/execution.d.ts.map +1 -1
- package/dist/prompts/execution.js +1 -3
- package/dist/prompts/execution.js.map +1 -1
- package/dist/prompts/planning.js +2 -2
- package/package.json +1 -1
- package/src/commands/do.ts +20 -11
- package/src/core/claude-runner.ts +270 -17
- package/src/core/git.ts +44 -0
- package/src/parsers/stream-renderer.ts +139 -0
- package/src/prompts/amend.ts +14 -2
- package/src/prompts/execution.ts +1 -3
- package/src/prompts/planning.ts +2 -2
- package/tests/unit/claude-runner.test.ts +567 -1
- package/tests/unit/git-commit-helpers.test.ts +103 -0
- package/tests/unit/plan-command.test.ts +51 -0
- package/tests/unit/stream-renderer.test.ts +286 -0
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
1
2
|
import * as pty from 'node-pty';
|
|
2
3
|
import type { IDisposable } from 'node-pty';
|
|
3
4
|
import { execSync, spawn } from 'node:child_process';
|
|
4
5
|
import { logger } from '../utils/logger.js';
|
|
6
|
+
import { renderStreamEvent } from '../parsers/stream-renderer.js';
|
|
7
|
+
import { getHeadCommitHash, getHeadCommitMessage, isFileCommittedInHead } from './git.js';
|
|
5
8
|
|
|
6
9
|
function getClaudePath(): string {
|
|
7
10
|
try {
|
|
@@ -26,6 +29,27 @@ export interface ClaudeRunnerOptions {
|
|
|
26
29
|
* Claude will still ask planning interview questions.
|
|
27
30
|
*/
|
|
28
31
|
dangerouslySkipPermissions?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Path to the outcome file. When provided, enables completion detection:
|
|
34
|
+
* - Monitors stdout for completion markers (<promise>COMPLETE/FAILED</promise>)
|
|
35
|
+
* - Polls the outcome file for completion markers
|
|
36
|
+
* When detected, starts a grace period before terminating the process,
|
|
37
|
+
* allowing time for git commit operations to complete.
|
|
38
|
+
*/
|
|
39
|
+
outcomeFilePath?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Commit verification context. When provided, the grace period will verify
|
|
42
|
+
* that the expected git commit has been made before terminating.
|
|
43
|
+
* Only applies when a COMPLETE marker is detected (not FAILED).
|
|
44
|
+
*/
|
|
45
|
+
commitContext?: {
|
|
46
|
+
/** HEAD commit hash recorded before task execution began. */
|
|
47
|
+
preExecutionHead: string;
|
|
48
|
+
/** Expected commit message prefix (e.g., "RAF[005:001]"). */
|
|
49
|
+
expectedPrefix: string;
|
|
50
|
+
/** Path to the outcome file that should be committed. */
|
|
51
|
+
outcomeFilePath: string;
|
|
52
|
+
};
|
|
29
53
|
}
|
|
30
54
|
|
|
31
55
|
export interface ClaudeRunnerConfig {
|
|
@@ -50,6 +74,180 @@ const CONTEXT_OVERFLOW_PATTERNS = [
|
|
|
50
74
|
/context window/i,
|
|
51
75
|
];
|
|
52
76
|
|
|
77
|
+
const COMPLETION_MARKER_PATTERN = /<promise>(COMPLETE|FAILED)<\/promise>/i;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Grace period in ms after completion marker is detected before terminating.
|
|
81
|
+
* Allows time for git commit operations to complete.
|
|
82
|
+
*/
|
|
83
|
+
export const COMPLETION_GRACE_PERIOD_MS = 60_000;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Hard maximum grace period in ms. If the commit hasn't landed by this point,
|
|
87
|
+
* the process is killed regardless.
|
|
88
|
+
*/
|
|
89
|
+
export const COMPLETION_HARD_MAX_MS = 180_000;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Interval in ms for polling commit verification after the initial grace period expires.
|
|
93
|
+
*/
|
|
94
|
+
export const COMMIT_POLL_INTERVAL_MS = 10_000;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Interval in ms for polling the outcome file for completion markers.
|
|
98
|
+
*/
|
|
99
|
+
export const OUTCOME_POLL_INTERVAL_MS = 5_000;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Context for commit verification during grace period.
|
|
103
|
+
*/
|
|
104
|
+
export interface CommitContext {
|
|
105
|
+
/** HEAD commit hash recorded before task execution began. */
|
|
106
|
+
preExecutionHead: string;
|
|
107
|
+
/** Expected commit message prefix (e.g., "RAF[005:001]"). */
|
|
108
|
+
expectedPrefix: string;
|
|
109
|
+
/** Path to the outcome file that should be committed. */
|
|
110
|
+
outcomeFilePath: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Monitors for task completion markers in stdout and outcome files.
|
|
115
|
+
* When a marker is detected, starts a grace period before killing the process.
|
|
116
|
+
*/
|
|
117
|
+
interface CompletionDetector {
|
|
118
|
+
/** Check accumulated stdout output for completion markers. */
|
|
119
|
+
checkOutput(output: string): void;
|
|
120
|
+
/** Clean up all timers. Must be called when the process exits. */
|
|
121
|
+
cleanup(): void;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const COMPLETE_MARKER_PATTERN = /<promise>COMPLETE<\/promise>/i;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Verify that the expected commit has been made.
|
|
128
|
+
* Checks: HEAD changed, commit message matches prefix, outcome file is committed.
|
|
129
|
+
*/
|
|
130
|
+
function verifyCommit(commitContext: CommitContext): boolean {
|
|
131
|
+
const currentHead = getHeadCommitHash();
|
|
132
|
+
if (!currentHead || currentHead === commitContext.preExecutionHead) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const message = getHeadCommitMessage();
|
|
137
|
+
if (!message || !message.startsWith(commitContext.expectedPrefix)) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!isFileCommittedInHead(commitContext.outcomeFilePath)) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function createCompletionDetector(
|
|
149
|
+
killFn: () => void,
|
|
150
|
+
outcomeFilePath?: string,
|
|
151
|
+
commitContext?: CommitContext,
|
|
152
|
+
): CompletionDetector {
|
|
153
|
+
let graceHandle: ReturnType<typeof setTimeout> | null = null;
|
|
154
|
+
let commitPollHandle: ReturnType<typeof setInterval> | null = null;
|
|
155
|
+
let hardMaxHandle: ReturnType<typeof setTimeout> | null = null;
|
|
156
|
+
let pollHandle: ReturnType<typeof setInterval> | null = null;
|
|
157
|
+
let initialMtime = 0;
|
|
158
|
+
let detectedMarkerIsComplete = false;
|
|
159
|
+
|
|
160
|
+
// Record initial mtime of outcome file to avoid false positives from previous runs
|
|
161
|
+
if (outcomeFilePath) {
|
|
162
|
+
try {
|
|
163
|
+
if (fs.existsSync(outcomeFilePath)) {
|
|
164
|
+
initialMtime = fs.statSync(outcomeFilePath).mtimeMs;
|
|
165
|
+
}
|
|
166
|
+
} catch {
|
|
167
|
+
// Ignore stat errors
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Called when the initial grace period expires.
|
|
173
|
+
* If commit verification is needed and the commit hasn't landed yet,
|
|
174
|
+
* start polling for the commit up to the hard maximum.
|
|
175
|
+
*/
|
|
176
|
+
function onGracePeriodExpired(): void {
|
|
177
|
+
if (commitContext && detectedMarkerIsComplete) {
|
|
178
|
+
// Check if commit already landed
|
|
179
|
+
if (verifyCommit(commitContext)) {
|
|
180
|
+
logger.debug('Grace period expired - commit verified, terminating Claude process');
|
|
181
|
+
killFn();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Commit not found yet - extend with polling
|
|
186
|
+
logger.debug('Grace period expired but commit not verified - extending with polling');
|
|
187
|
+
const remainingMs = COMPLETION_HARD_MAX_MS - COMPLETION_GRACE_PERIOD_MS;
|
|
188
|
+
|
|
189
|
+
hardMaxHandle = setTimeout(() => {
|
|
190
|
+
logger.warn('Hard maximum grace period reached without commit verification - terminating Claude process');
|
|
191
|
+
if (commitPollHandle) clearInterval(commitPollHandle);
|
|
192
|
+
killFn();
|
|
193
|
+
}, remainingMs);
|
|
194
|
+
|
|
195
|
+
commitPollHandle = setInterval(() => {
|
|
196
|
+
if (commitContext && verifyCommit(commitContext)) {
|
|
197
|
+
logger.debug('Commit verified during extended grace period - terminating Claude process');
|
|
198
|
+
if (commitPollHandle) clearInterval(commitPollHandle);
|
|
199
|
+
if (hardMaxHandle) clearTimeout(hardMaxHandle);
|
|
200
|
+
killFn();
|
|
201
|
+
}
|
|
202
|
+
}, COMMIT_POLL_INTERVAL_MS);
|
|
203
|
+
} else {
|
|
204
|
+
// No commit verification needed (FAILED marker or no context) - kill immediately
|
|
205
|
+
logger.debug('Grace period expired - terminating Claude process');
|
|
206
|
+
killFn();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function startGracePeriod(markerOutput: string): void {
|
|
211
|
+
if (graceHandle) return; // Already started
|
|
212
|
+
detectedMarkerIsComplete = COMPLETE_MARKER_PATTERN.test(markerOutput);
|
|
213
|
+
logger.debug('Completion marker detected - starting grace period before termination');
|
|
214
|
+
graceHandle = setTimeout(onGracePeriodExpired, COMPLETION_GRACE_PERIOD_MS);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function checkOutput(output: string): void {
|
|
218
|
+
if (!graceHandle && COMPLETION_MARKER_PATTERN.test(output)) {
|
|
219
|
+
startGracePeriod(output);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Start outcome file polling if path provided
|
|
224
|
+
if (outcomeFilePath) {
|
|
225
|
+
const filePath = outcomeFilePath;
|
|
226
|
+
pollHandle = setInterval(() => {
|
|
227
|
+
try {
|
|
228
|
+
if (!fs.existsSync(filePath)) return;
|
|
229
|
+
const stat = fs.statSync(filePath);
|
|
230
|
+
if (stat.mtimeMs <= initialMtime) return; // File unchanged from before execution
|
|
231
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
232
|
+
if (COMPLETION_MARKER_PATTERN.test(content)) {
|
|
233
|
+
startGracePeriod(content);
|
|
234
|
+
}
|
|
235
|
+
} catch {
|
|
236
|
+
// Ignore read errors - file may be mid-write
|
|
237
|
+
}
|
|
238
|
+
}, OUTCOME_POLL_INTERVAL_MS);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function cleanup(): void {
|
|
242
|
+
if (graceHandle) clearTimeout(graceHandle);
|
|
243
|
+
if (pollHandle) clearInterval(pollHandle);
|
|
244
|
+
if (commitPollHandle) clearInterval(commitPollHandle);
|
|
245
|
+
if (hardMaxHandle) clearTimeout(hardMaxHandle);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return { checkOutput, cleanup };
|
|
249
|
+
}
|
|
250
|
+
|
|
53
251
|
export class ClaudeRunner {
|
|
54
252
|
private activeProcess: pty.IPty | null = null;
|
|
55
253
|
private killed = false;
|
|
@@ -169,7 +367,7 @@ export class ClaudeRunner {
|
|
|
169
367
|
* - Default timeout is 60 minutes if not specified
|
|
170
368
|
*/
|
|
171
369
|
async run(prompt: string, options: ClaudeRunnerOptions = {}): Promise<RunResult> {
|
|
172
|
-
const { timeout = 60, cwd = process.cwd() } = options;
|
|
370
|
+
const { timeout = 60, cwd = process.cwd(), outcomeFilePath, commitContext } = options;
|
|
173
371
|
// Ensure timeout is a positive number, fallback to 60 minutes
|
|
174
372
|
const validatedTimeout = Number(timeout) > 0 ? Number(timeout) : 60;
|
|
175
373
|
const timeoutMs = validatedTimeout * 60 * 1000;
|
|
@@ -213,11 +411,21 @@ export class ClaudeRunner {
|
|
|
213
411
|
proc.kill('SIGTERM');
|
|
214
412
|
}, timeoutMs);
|
|
215
413
|
|
|
414
|
+
// Set up completion detection (stdout marker + outcome file polling)
|
|
415
|
+
const completionDetector = createCompletionDetector(
|
|
416
|
+
() => proc.kill('SIGTERM'),
|
|
417
|
+
outcomeFilePath,
|
|
418
|
+
commitContext,
|
|
419
|
+
);
|
|
420
|
+
|
|
216
421
|
// Collect stdout
|
|
217
422
|
proc.stdout.on('data', (data) => {
|
|
218
423
|
const text = data.toString();
|
|
219
424
|
output += text;
|
|
220
425
|
|
|
426
|
+
// Check for completion marker to start grace period
|
|
427
|
+
completionDetector.checkOutput(output);
|
|
428
|
+
|
|
221
429
|
// Check for context overflow
|
|
222
430
|
for (const pattern of CONTEXT_OVERFLOW_PATTERNS) {
|
|
223
431
|
if (pattern.test(text)) {
|
|
@@ -236,6 +444,7 @@ export class ClaudeRunner {
|
|
|
236
444
|
|
|
237
445
|
proc.on('close', (exitCode) => {
|
|
238
446
|
clearTimeout(timeoutHandle);
|
|
447
|
+
completionDetector.cleanup();
|
|
239
448
|
this.activeProcess = null;
|
|
240
449
|
|
|
241
450
|
if (stderr) {
|
|
@@ -254,7 +463,8 @@ export class ClaudeRunner {
|
|
|
254
463
|
|
|
255
464
|
/**
|
|
256
465
|
* Run Claude non-interactively with verbose output to stdout.
|
|
257
|
-
* Uses
|
|
466
|
+
* Uses --output-format stream-json --verbose to get real-time streaming
|
|
467
|
+
* of tool calls, file operations, and thinking steps.
|
|
258
468
|
*
|
|
259
469
|
* TIMEOUT BEHAVIOR:
|
|
260
470
|
* - The timeout is applied per individual call to this method
|
|
@@ -264,7 +474,7 @@ export class ClaudeRunner {
|
|
|
264
474
|
* - Default timeout is 60 minutes if not specified
|
|
265
475
|
*/
|
|
266
476
|
async runVerbose(prompt: string, options: ClaudeRunnerOptions = {}): Promise<RunResult> {
|
|
267
|
-
const { timeout = 60, cwd = process.cwd() } = options;
|
|
477
|
+
const { timeout = 60, cwd = process.cwd(), outcomeFilePath, commitContext } = options;
|
|
268
478
|
// Ensure timeout is a positive number, fallback to 60 minutes
|
|
269
479
|
const validatedTimeout = Number(timeout) > 0 ? Number(timeout) : 60;
|
|
270
480
|
const timeoutMs = validatedTimeout * 60 * 1000;
|
|
@@ -277,13 +487,13 @@ export class ClaudeRunner {
|
|
|
277
487
|
|
|
278
488
|
const claudePath = getClaudePath();
|
|
279
489
|
|
|
280
|
-
logger.debug(`Starting Claude execution session (verbose) with model: ${this.model}`);
|
|
490
|
+
logger.debug(`Starting Claude execution session (verbose/stream-json) with model: ${this.model}`);
|
|
281
491
|
logger.debug(`Prompt length: ${prompt.length}, timeout: ${timeoutMs}ms, cwd: ${cwd}`);
|
|
282
492
|
logger.debug(`Claude path: ${claudePath}`);
|
|
283
493
|
|
|
284
494
|
logger.debug('Spawning process...');
|
|
285
|
-
// Use --
|
|
286
|
-
//
|
|
495
|
+
// Use --output-format stream-json --verbose to get real-time streaming events
|
|
496
|
+
// including tool calls, file operations, and intermediate output.
|
|
287
497
|
// --dangerously-skip-permissions bypasses interactive prompts
|
|
288
498
|
// -p enables print mode (non-interactive)
|
|
289
499
|
const proc = spawn(claudePath, [
|
|
@@ -292,6 +502,9 @@ export class ClaudeRunner {
|
|
|
292
502
|
this.model,
|
|
293
503
|
'--append-system-prompt',
|
|
294
504
|
prompt,
|
|
505
|
+
'--output-format',
|
|
506
|
+
'stream-json',
|
|
507
|
+
'--verbose',
|
|
295
508
|
'-p',
|
|
296
509
|
'Execute the task as described in the system prompt.',
|
|
297
510
|
], {
|
|
@@ -311,24 +524,52 @@ export class ClaudeRunner {
|
|
|
311
524
|
proc.kill('SIGTERM');
|
|
312
525
|
}, timeoutMs);
|
|
313
526
|
|
|
314
|
-
//
|
|
527
|
+
// Set up completion detection (stdout marker + outcome file polling)
|
|
528
|
+
const completionDetector = createCompletionDetector(
|
|
529
|
+
() => proc.kill('SIGTERM'),
|
|
530
|
+
outcomeFilePath,
|
|
531
|
+
commitContext,
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
// Buffer for incomplete NDJSON lines (data chunks may split across line boundaries)
|
|
535
|
+
let lineBuffer = '';
|
|
315
536
|
let dataReceived = false;
|
|
537
|
+
|
|
316
538
|
proc.stdout.on('data', (data) => {
|
|
317
539
|
if (!dataReceived) {
|
|
318
540
|
logger.debug('First data chunk received');
|
|
319
541
|
dataReceived = true;
|
|
320
542
|
}
|
|
321
|
-
const text = data.toString();
|
|
322
|
-
output += text;
|
|
323
|
-
process.stdout.write(text);
|
|
324
543
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
544
|
+
lineBuffer += data.toString();
|
|
545
|
+
|
|
546
|
+
// Process complete lines from the NDJSON stream
|
|
547
|
+
let newlineIndex: number;
|
|
548
|
+
while ((newlineIndex = lineBuffer.indexOf('\n')) !== -1) {
|
|
549
|
+
const line = lineBuffer.substring(0, newlineIndex);
|
|
550
|
+
lineBuffer = lineBuffer.substring(newlineIndex + 1);
|
|
551
|
+
|
|
552
|
+
const { display, textContent } = renderStreamEvent(line);
|
|
553
|
+
|
|
554
|
+
if (textContent) {
|
|
555
|
+
output += textContent;
|
|
556
|
+
|
|
557
|
+
// Check for completion marker to start grace period
|
|
558
|
+
completionDetector.checkOutput(output);
|
|
559
|
+
|
|
560
|
+
// Check for context overflow
|
|
561
|
+
for (const pattern of CONTEXT_OVERFLOW_PATTERNS) {
|
|
562
|
+
if (pattern.test(textContent)) {
|
|
563
|
+
contextOverflow = true;
|
|
564
|
+
logger.warn('Context overflow detected');
|
|
565
|
+
proc.kill('SIGTERM');
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
if (display) {
|
|
572
|
+
process.stdout.write(display);
|
|
332
573
|
}
|
|
333
574
|
}
|
|
334
575
|
});
|
|
@@ -339,7 +580,19 @@ export class ClaudeRunner {
|
|
|
339
580
|
});
|
|
340
581
|
|
|
341
582
|
proc.on('close', (exitCode) => {
|
|
583
|
+
// Process any remaining data in the line buffer
|
|
584
|
+
if (lineBuffer.trim()) {
|
|
585
|
+
const { display, textContent } = renderStreamEvent(lineBuffer);
|
|
586
|
+
if (textContent) {
|
|
587
|
+
output += textContent;
|
|
588
|
+
}
|
|
589
|
+
if (display) {
|
|
590
|
+
process.stdout.write(display);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
342
594
|
clearTimeout(timeoutHandle);
|
|
595
|
+
completionDetector.cleanup();
|
|
343
596
|
this.activeProcess = null;
|
|
344
597
|
logger.debug(`Claude exited with code ${exitCode}, output length: ${output.length}, timedOut: ${timedOut}, contextOverflow: ${contextOverflow}`);
|
|
345
598
|
|
package/src/core/git.ts
CHANGED
|
@@ -167,6 +167,50 @@ export function stashChanges(name: string): boolean {
|
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Get the current HEAD commit hash.
|
|
172
|
+
* Returns null if not in a git repo or HEAD doesn't exist.
|
|
173
|
+
*/
|
|
174
|
+
export function getHeadCommitHash(): string | null {
|
|
175
|
+
try {
|
|
176
|
+
return execSync('git rev-parse HEAD', { encoding: 'utf-8', stdio: 'pipe' }).trim() || null;
|
|
177
|
+
} catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Get the current HEAD commit message (first line only).
|
|
184
|
+
* Returns null if not in a git repo or HEAD doesn't exist.
|
|
185
|
+
*/
|
|
186
|
+
export function getHeadCommitMessage(): string | null {
|
|
187
|
+
try {
|
|
188
|
+
return execSync('git log -1 --format=%s', { encoding: 'utf-8', stdio: 'pipe' }).trim() || null;
|
|
189
|
+
} catch {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Check if a file is tracked in the HEAD commit.
|
|
196
|
+
* Returns true if the file appears in the latest commit's tree.
|
|
197
|
+
*/
|
|
198
|
+
export function isFileCommittedInHead(filePath: string): boolean {
|
|
199
|
+
try {
|
|
200
|
+
// Use git ls-tree to check if the file exists in HEAD
|
|
201
|
+
// We need the path relative to the repo root
|
|
202
|
+
const repoRoot = execSync('git rev-parse --show-toplevel', { encoding: 'utf-8', stdio: 'pipe' }).trim();
|
|
203
|
+
const relativePath = path.relative(repoRoot, path.resolve(filePath));
|
|
204
|
+
const result = execSync(`git ls-tree HEAD -- "${relativePath.replace(/"/g, '\\"')}"`, {
|
|
205
|
+
encoding: 'utf-8',
|
|
206
|
+
stdio: 'pipe',
|
|
207
|
+
}).trim();
|
|
208
|
+
return result.length > 0;
|
|
209
|
+
} catch {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
170
214
|
/**
|
|
171
215
|
* Commit planning artifacts (input.md and decisions.md) for a project.
|
|
172
216
|
* Uses commit message format: RAF[NNN] Plan: project-name
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders stream-json events from Claude CLI into human-readable verbose output.
|
|
3
|
+
*
|
|
4
|
+
* Event types from `claude -p --output-format stream-json --verbose`:
|
|
5
|
+
* - system (init): Session initialization info
|
|
6
|
+
* - assistant: Claude's response with text or tool_use content blocks
|
|
7
|
+
* - user: Tool results (tool_result content blocks)
|
|
8
|
+
* - result: Final result with success/failure status
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface StreamEvent {
|
|
12
|
+
type: string;
|
|
13
|
+
subtype?: string;
|
|
14
|
+
message?: {
|
|
15
|
+
content?: ContentBlock[];
|
|
16
|
+
};
|
|
17
|
+
result?: string;
|
|
18
|
+
tool_use_result?: {
|
|
19
|
+
type?: string;
|
|
20
|
+
file?: {
|
|
21
|
+
filePath?: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ContentBlock {
|
|
27
|
+
type: string;
|
|
28
|
+
text?: string;
|
|
29
|
+
name?: string;
|
|
30
|
+
input?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Describes what a tool is doing in human-readable form.
|
|
35
|
+
*/
|
|
36
|
+
function describeToolUse(name: string, input: Record<string, unknown>): string {
|
|
37
|
+
switch (name) {
|
|
38
|
+
case 'Read':
|
|
39
|
+
return `Reading ${input.file_path ?? 'file'}`;
|
|
40
|
+
case 'Write':
|
|
41
|
+
return `Writing ${input.file_path ?? 'file'}`;
|
|
42
|
+
case 'Edit':
|
|
43
|
+
return `Editing ${input.file_path ?? 'file'}`;
|
|
44
|
+
case 'Bash':
|
|
45
|
+
return `Running: ${truncate(String(input.command ?? ''), 120)}`;
|
|
46
|
+
case 'Glob':
|
|
47
|
+
return `Searching files: ${input.pattern ?? ''}`;
|
|
48
|
+
case 'Grep':
|
|
49
|
+
return `Searching for: ${truncate(String(input.pattern ?? ''), 80)}`;
|
|
50
|
+
case 'WebFetch':
|
|
51
|
+
return `Fetching: ${input.url ?? ''}`;
|
|
52
|
+
case 'WebSearch':
|
|
53
|
+
return `Searching web: ${input.query ?? ''}`;
|
|
54
|
+
case 'TodoWrite':
|
|
55
|
+
return 'Updating task list';
|
|
56
|
+
case 'Task':
|
|
57
|
+
return `Launching agent: ${truncate(String(input.description ?? input.prompt ?? ''), 80)}`;
|
|
58
|
+
case 'NotebookEdit':
|
|
59
|
+
return `Editing notebook: ${input.notebook_path ?? ''}`;
|
|
60
|
+
default:
|
|
61
|
+
return `Using tool: ${name}`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function truncate(text: string, maxLen: number): string {
|
|
66
|
+
if (text.length <= maxLen) return text;
|
|
67
|
+
return text.substring(0, maxLen - 3) + '...';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface RenderResult {
|
|
71
|
+
/** Text to display to stdout (may be empty if no display needed) */
|
|
72
|
+
display: string;
|
|
73
|
+
/** Text content to accumulate for output parsing (completion markers, etc.) */
|
|
74
|
+
textContent: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Parse and render a single NDJSON line from stream-json output.
|
|
79
|
+
* Returns display text for stdout and text content for output accumulation.
|
|
80
|
+
*/
|
|
81
|
+
export function renderStreamEvent(line: string): RenderResult {
|
|
82
|
+
if (!line.trim()) {
|
|
83
|
+
return { display: '', textContent: '' };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let event: StreamEvent;
|
|
87
|
+
try {
|
|
88
|
+
event = JSON.parse(line) as StreamEvent;
|
|
89
|
+
} catch {
|
|
90
|
+
// Not valid JSON - pass through raw
|
|
91
|
+
return { display: line + '\n', textContent: line };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
switch (event.type) {
|
|
95
|
+
case 'system':
|
|
96
|
+
return { display: '', textContent: '' };
|
|
97
|
+
|
|
98
|
+
case 'assistant':
|
|
99
|
+
return renderAssistant(event);
|
|
100
|
+
|
|
101
|
+
case 'user':
|
|
102
|
+
// Tool results — skip verbose display (the tool_use already described what's happening)
|
|
103
|
+
return { display: '', textContent: '' };
|
|
104
|
+
|
|
105
|
+
case 'result':
|
|
106
|
+
return renderResult(event);
|
|
107
|
+
|
|
108
|
+
default:
|
|
109
|
+
return { display: '', textContent: '' };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function renderAssistant(event: StreamEvent): RenderResult {
|
|
114
|
+
const content = event.message?.content;
|
|
115
|
+
if (!content || !Array.isArray(content)) {
|
|
116
|
+
return { display: '', textContent: '' };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let display = '';
|
|
120
|
+
let textContent = '';
|
|
121
|
+
|
|
122
|
+
for (const block of content) {
|
|
123
|
+
if (block.type === 'text' && block.text) {
|
|
124
|
+
textContent += block.text;
|
|
125
|
+
display += block.text + '\n';
|
|
126
|
+
} else if (block.type === 'tool_use' && block.name) {
|
|
127
|
+
const description = describeToolUse(block.name, block.input ?? {});
|
|
128
|
+
display += ` → ${description}\n`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { display, textContent };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function renderResult(_event: StreamEvent): RenderResult {
|
|
136
|
+
// The result event's text duplicates the last assistant message,
|
|
137
|
+
// which is already captured. Skip to avoid double-counting.
|
|
138
|
+
return { display: '', textContent: '' };
|
|
139
|
+
}
|
package/src/prompts/amend.ts
CHANGED
|
@@ -35,7 +35,11 @@ export function getAmendPrompt(params: AmendPromptParams): AmendPromptResult {
|
|
|
35
35
|
: '[PENDING]';
|
|
36
36
|
const modifiability =
|
|
37
37
|
task.status === 'completed' ? '[PROTECTED]' : '[MODIFIABLE]';
|
|
38
|
-
|
|
38
|
+
const outcomeRef =
|
|
39
|
+
task.status === 'completed'
|
|
40
|
+
? `\n Outcome: ${projectPath}/outcomes/${task.planFile.replace('plans/', '').replace(/\.md$/, '')}.md`
|
|
41
|
+
: '';
|
|
42
|
+
return `- Task ${task.id}: ${task.taskName} ${status} ${modifiability}${outcomeRef}`;
|
|
39
43
|
})
|
|
40
44
|
.join('\n');
|
|
41
45
|
|
|
@@ -94,6 +98,13 @@ Read the user's description of new tasks and identify what needs to be added. Co
|
|
|
94
98
|
- Dependencies on completed tasks (check the ## Dependencies section in existing plan files)
|
|
95
99
|
- Whether new tasks should reference existing task outcomes
|
|
96
100
|
|
|
101
|
+
**Identifying Follow-up Tasks**: When a new task is a follow-up, fix, or iteration of a previously completed task, you MUST reference the previous task's outcome in the new plan's Context section. This gives the executing agent full context about what was done before.
|
|
102
|
+
|
|
103
|
+
Use this format in the Context section:
|
|
104
|
+
\`This is a follow-up to task NNN. See outcome: {projectPath}/outcomes/NNN-task-name.md\`
|
|
105
|
+
|
|
106
|
+
The outcome file paths for completed tasks are listed above in the Existing Tasks section.
|
|
107
|
+
|
|
97
108
|
### Step 3: Interview the User
|
|
98
109
|
|
|
99
110
|
For EACH new task you identify, use the AskUserQuestion tool to gather:
|
|
@@ -109,7 +120,7 @@ After EACH interview question is answered, append the Q&A pair to the existing d
|
|
|
109
120
|
|
|
110
121
|
Use this format:
|
|
111
122
|
\`\`\`markdown
|
|
112
|
-
## [
|
|
123
|
+
## [Question asked]
|
|
113
124
|
[User's answer]
|
|
114
125
|
\`\`\`
|
|
115
126
|
|
|
@@ -131,6 +142,7 @@ Each plan file should follow this structure:
|
|
|
131
142
|
## Context
|
|
132
143
|
[Why this task is needed, how it fits into the larger project]
|
|
133
144
|
[Reference relevant existing tasks if applicable]
|
|
145
|
+
[For follow-up/fix tasks: "This is a follow-up to task NNN. See outcome: {projectPath}/outcomes/NNN-task-name.md"]
|
|
134
146
|
|
|
135
147
|
## Dependencies
|
|
136
148
|
[Optional section - omit if task has no dependencies]
|
package/src/prompts/execution.ts
CHANGED
|
@@ -164,12 +164,10 @@ First, read the plan file to understand exactly what needs to be done.
|
|
|
164
164
|
### Step 2: Execute the Task
|
|
165
165
|
|
|
166
166
|
Follow the implementation steps in the plan. Key guidelines:
|
|
167
|
-
- **Use the Task tool to delegate work to subagents** - Split complex work into subtasks and use specialized agents (Explore for codebase investigation, Plan for design decisions, general-purpose for implementation)
|
|
168
167
|
- Write clean, maintainable code
|
|
169
168
|
- Follow existing code patterns in the project
|
|
170
169
|
- Add appropriate error handling
|
|
171
|
-
-
|
|
172
|
-
- Follow any CLAUDE.md instructions if present
|
|
170
|
+
- Follow any CLAUDE.md instructions
|
|
173
171
|
${dependencyContextSection}${outcomesSection}
|
|
174
172
|
### Step 3: Verify Completion
|
|
175
173
|
|
package/src/prompts/planning.ts
CHANGED
|
@@ -20,7 +20,7 @@ export function getPlanningPrompt(params: PlanningPromptParams): PlanningPromptR
|
|
|
20
20
|
|
|
21
21
|
## Your Goals
|
|
22
22
|
|
|
23
|
-
1. **Analyze the input** and identify
|
|
23
|
+
1. **Analyze the input** and identify distinct, actionable tasks
|
|
24
24
|
2. **Interview the user** about EACH task to gather specific requirements
|
|
25
25
|
3. **Create plan files** for each task with clear instructions
|
|
26
26
|
|
|
@@ -32,7 +32,7 @@ Project folder: ${projectPath}
|
|
|
32
32
|
|
|
33
33
|
### Step 1: Identify and Order Tasks
|
|
34
34
|
|
|
35
|
-
Based on the project description, identify
|
|
35
|
+
Based on the project description, identify distinct tasks. Each task should:
|
|
36
36
|
- Be independently completable
|
|
37
37
|
- Have a clear outcome
|
|
38
38
|
- Take roughly 10-30 minutes of work for Claude
|