• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

umputun / ralphex / 21499069074

29 Jan 2026 11:55PM UTC coverage: 80.619% (+0.6%) from 79.976%
21499069074

push

github

umputun
docs: update CLAUDE.md with PLAN_DRAFT workflow and move plan to completed

4218 of 5232 relevant lines covered (80.62%)

125.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.58
/pkg/processor/runner.go
1
// Package processor provides the main orchestration loop for ralphex execution.
2
package processor
3

4
import (
5
        "context"
6
        "errors"
7
        "fmt"
8
        "os"
9
        "os/exec"
10
        "strings"
11
        "time"
12

13
        "github.com/umputun/ralphex/pkg/config"
14
        "github.com/umputun/ralphex/pkg/executor"
15
)
16

17
// DefaultIterationDelay is the pause between iterations to allow system to settle.
18
const DefaultIterationDelay = 2 * time.Second
19

20
// Mode represents the execution mode.
21
type Mode string
22

23
const (
24
        ModeFull      Mode = "full"       // full execution: tasks + reviews + codex
25
        ModeReview    Mode = "review"     // skip tasks, run full review pipeline
26
        ModeCodexOnly Mode = "codex-only" // skip tasks and first review, run only codex loop
27
        ModePlan      Mode = "plan"       // interactive plan creation mode
28
)
29

30
// Config holds runner configuration.
31
type Config struct {
32
        PlanFile         string         // path to plan file (required for full mode)
33
        PlanDescription  string         // plan description for interactive plan creation mode
34
        ProgressPath     string         // path to progress file
35
        Mode             Mode           // execution mode
36
        MaxIterations    int            // maximum iterations for task phase
37
        Debug            bool           // enable debug output
38
        NoColor          bool           // disable color output
39
        IterationDelayMs int            // delay between iterations in milliseconds
40
        TaskRetryCount   int            // number of times to retry failed tasks
41
        CodexEnabled     bool           // whether codex review is enabled
42
        DefaultBranch    string         // default branch name (detected from repo)
43
        AppConfig        *config.Config // full application config (for executors and prompts)
44
}
45

46
//go:generate moq -out mocks/executor.go -pkg mocks -skip-ensure -fmt goimports . Executor
47
//go:generate moq -out mocks/logger.go -pkg mocks -skip-ensure -fmt goimports . Logger
48
//go:generate moq -out mocks/input_collector.go -pkg mocks -skip-ensure -fmt goimports . InputCollector
49

50
// Executor runs CLI commands and returns results.
51
type Executor interface {
52
        Run(ctx context.Context, prompt string) executor.Result
53
}
54

55
// Logger provides logging functionality.
56
type Logger interface {
57
        SetPhase(phase Phase)
58
        Print(format string, args ...any)
59
        PrintRaw(format string, args ...any)
60
        PrintSection(section Section)
61
        PrintAligned(text string)
62
        LogQuestion(question string, options []string)
63
        LogAnswer(answer string)
64
        LogDraftReview(action string, feedback string)
65
        Path() string
66
}
67

68
// InputCollector provides interactive input collection for plan creation.
69
type InputCollector interface {
70
        AskQuestion(ctx context.Context, question string, options []string) (string, error)
71
        AskDraftReview(ctx context.Context, question string, planContent string) (action string, feedback string, err error)
72
}
73

74
// Runner orchestrates the execution loop.
75
type Runner struct {
76
        cfg            Config
77
        log            Logger
78
        claude         Executor
79
        codex          Executor
80
        inputCollector InputCollector
81
        iterationDelay time.Duration
82
        taskRetryCount int
83
}
84

85
// New creates a new Runner with the given configuration.
86
// If codex is enabled but the binary is not found in PATH, it is automatically disabled with a warning.
87
func New(cfg Config, log Logger) *Runner {
1✔
88
        // build claude executor with config values
1✔
89
        claudeExec := &executor.ClaudeExecutor{
1✔
90
                OutputHandler: func(text string) {
1✔
91
                        log.PrintAligned(text)
×
92
                },
×
93
                Debug: cfg.Debug,
94
        }
95
        if cfg.AppConfig != nil {
2✔
96
                claudeExec.Command = cfg.AppConfig.ClaudeCommand
1✔
97
                claudeExec.Args = cfg.AppConfig.ClaudeArgs
1✔
98
                claudeExec.ErrorPatterns = cfg.AppConfig.ClaudeErrorPatterns
1✔
99
        }
1✔
100

101
        // build codex executor with config values
102
        codexExec := &executor.CodexExecutor{
1✔
103
                OutputHandler: func(text string) {
1✔
104
                        log.PrintAligned(text)
×
105
                },
×
106
                Debug: cfg.Debug,
107
        }
108
        if cfg.AppConfig != nil {
2✔
109
                codexExec.Command = cfg.AppConfig.CodexCommand
1✔
110
                codexExec.Model = cfg.AppConfig.CodexModel
1✔
111
                codexExec.ReasoningEffort = cfg.AppConfig.CodexReasoningEffort
1✔
112
                codexExec.TimeoutMs = cfg.AppConfig.CodexTimeoutMs
1✔
113
                codexExec.Sandbox = cfg.AppConfig.CodexSandbox
1✔
114
                codexExec.ErrorPatterns = cfg.AppConfig.CodexErrorPatterns
1✔
115
        }
1✔
116

117
        // auto-disable codex if the binary is not installed
118
        if cfg.CodexEnabled {
2✔
119
                codexCmd := codexExec.Command
1✔
120
                if codexCmd == "" {
1✔
121
                        codexCmd = "codex"
×
122
                }
×
123
                if _, err := exec.LookPath(codexCmd); err != nil {
2✔
124
                        log.Print("warning: codex not found (%s: %v), disabling codex review phase", codexCmd, err)
1✔
125
                        cfg.CodexEnabled = false
1✔
126
                }
1✔
127
        }
128

129
        return NewWithExecutors(cfg, log, claudeExec, codexExec)
1✔
130
}
131

132
// NewWithExecutors creates a new Runner with custom executors (for testing).
133
func NewWithExecutors(cfg Config, log Logger, claude, codex Executor) *Runner {
44✔
134
        // determine iteration delay from config or default
44✔
135
        iterDelay := DefaultIterationDelay
44✔
136
        if cfg.IterationDelayMs > 0 {
60✔
137
                iterDelay = time.Duration(cfg.IterationDelayMs) * time.Millisecond
16✔
138
        }
16✔
139

140
        // determine task retry count from config
141
        // appConfig.TaskRetryCountSet means user explicitly set it (even to 0 for no retries)
142
        retryCount := 1
44✔
143
        if cfg.AppConfig != nil && cfg.AppConfig.TaskRetryCountSet {
77✔
144
                retryCount = cfg.TaskRetryCount
33✔
145
        } else if cfg.TaskRetryCount > 0 {
45✔
146
                retryCount = cfg.TaskRetryCount
1✔
147
        }
1✔
148

149
        return &Runner{
44✔
150
                cfg:            cfg,
44✔
151
                log:            log,
44✔
152
                claude:         claude,
44✔
153
                codex:          codex,
44✔
154
                iterationDelay: iterDelay,
44✔
155
                taskRetryCount: retryCount,
44✔
156
        }
44✔
157
}
158

159
// SetInputCollector sets the input collector for plan creation mode.
160
func (r *Runner) SetInputCollector(c InputCollector) {
15✔
161
        r.inputCollector = c
15✔
162
}
15✔
163

164
// Run executes the main loop based on configured mode.
165
func (r *Runner) Run(ctx context.Context) error {
34✔
166
        switch r.cfg.Mode {
34✔
167
        case ModeFull:
9✔
168
                return r.runFull(ctx)
9✔
169
        case ModeReview:
5✔
170
                return r.runReviewOnly(ctx)
5✔
171
        case ModeCodexOnly:
3✔
172
                return r.runCodexOnly(ctx)
3✔
173
        case ModePlan:
16✔
174
                return r.runPlanCreation(ctx)
16✔
175
        default:
1✔
176
                return fmt.Errorf("unknown mode: %s", r.cfg.Mode)
1✔
177
        }
178
}
179

180
// runFull executes the complete pipeline: tasks → review → codex → review.
181
func (r *Runner) runFull(ctx context.Context) error {
9✔
182
        if r.cfg.PlanFile == "" {
10✔
183
                return errors.New("plan file required for full mode")
1✔
184
        }
1✔
185

186
        // phase 1: task execution
187
        r.log.SetPhase(PhaseTask)
8✔
188
        r.log.PrintRaw("starting task execution phase\n")
8✔
189

8✔
190
        if err := r.runTaskPhase(ctx); err != nil {
14✔
191
                return fmt.Errorf("task phase: %w", err)
6✔
192
        }
6✔
193

194
        // phase 2: first review pass - address ALL findings
195
        r.log.SetPhase(PhaseReview)
2✔
196
        r.log.PrintSection(NewGenericSection("claude review 0: all findings"))
2✔
197

2✔
198
        if err := r.runClaudeReview(ctx, r.replacePromptVariables(r.cfg.AppConfig.ReviewFirstPrompt)); err != nil {
2✔
199
                return fmt.Errorf("first review: %w", err)
×
200
        }
×
201

202
        // phase 2.1: claude review loop (critical/major) before codex
203
        if err := r.runClaudeReviewLoop(ctx); err != nil {
2✔
204
                return fmt.Errorf("pre-codex review loop: %w", err)
×
205
        }
×
206

207
        // phase 2.5: codex external review loop
208
        r.log.SetPhase(PhaseCodex)
2✔
209
        r.log.PrintSection(NewGenericSection("codex external review"))
2✔
210

2✔
211
        if err := r.runCodexLoop(ctx); err != nil {
2✔
212
                return fmt.Errorf("codex loop: %w", err)
×
213
        }
×
214

215
        // phase 3: claude review loop (critical/major) after codex
216
        r.log.SetPhase(PhaseReview)
2✔
217

2✔
218
        if err := r.runClaudeReviewLoop(ctx); err != nil {
2✔
219
                return fmt.Errorf("post-codex review loop: %w", err)
×
220
        }
×
221

222
        r.log.Print("all phases completed successfully")
2✔
223
        return nil
2✔
224
}
225

226
// runReviewOnly executes only the review pipeline: review → codex → review.
227
func (r *Runner) runReviewOnly(ctx context.Context) error {
5✔
228
        // phase 1: first review
5✔
229
        r.log.SetPhase(PhaseReview)
5✔
230
        r.log.PrintSection(NewGenericSection("claude review 0: all findings"))
5✔
231

5✔
232
        if err := r.runClaudeReview(ctx, r.replacePromptVariables(r.cfg.AppConfig.ReviewFirstPrompt)); err != nil {
6✔
233
                return fmt.Errorf("first review: %w", err)
1✔
234
        }
1✔
235

236
        // phase 1.1: claude review loop (critical/major) before codex
237
        if err := r.runClaudeReviewLoop(ctx); err != nil {
5✔
238
                return fmt.Errorf("pre-codex review loop: %w", err)
1✔
239
        }
1✔
240

241
        // phase 2: codex external review loop
242
        r.log.SetPhase(PhaseCodex)
3✔
243
        r.log.PrintSection(NewGenericSection("codex external review"))
3✔
244

3✔
245
        if err := r.runCodexLoop(ctx); err != nil {
5✔
246
                return fmt.Errorf("codex loop: %w", err)
2✔
247
        }
2✔
248

249
        // phase 3: claude review loop (critical/major) after codex
250
        r.log.SetPhase(PhaseReview)
1✔
251

1✔
252
        if err := r.runClaudeReviewLoop(ctx); err != nil {
1✔
253
                return fmt.Errorf("post-codex review loop: %w", err)
×
254
        }
×
255

256
        r.log.Print("review phases completed successfully")
1✔
257
        return nil
1✔
258
}
259

260
// runCodexOnly executes only the codex pipeline: codex → review.
261
func (r *Runner) runCodexOnly(ctx context.Context) error {
3✔
262
        // phase 1: codex external review loop
3✔
263
        r.log.SetPhase(PhaseCodex)
3✔
264
        r.log.PrintSection(NewGenericSection("codex external review"))
3✔
265

3✔
266
        if err := r.runCodexLoop(ctx); err != nil {
3✔
267
                return fmt.Errorf("codex loop: %w", err)
×
268
        }
×
269

270
        // phase 2: claude review loop (critical/major) after codex
271
        r.log.SetPhase(PhaseReview)
3✔
272

3✔
273
        if err := r.runClaudeReviewLoop(ctx); err != nil {
3✔
274
                return fmt.Errorf("post-codex review loop: %w", err)
×
275
        }
×
276

277
        r.log.Print("codex phases completed successfully")
3✔
278
        return nil
3✔
279
}
280

281
// runTaskPhase executes tasks until completion or max iterations.
282
// executes ONE Task section per iteration.
283
func (r *Runner) runTaskPhase(ctx context.Context) error {
8✔
284
        prompt := r.replacePromptVariables(r.cfg.AppConfig.TaskPrompt)
8✔
285
        retryCount := 0
8✔
286

8✔
287
        for i := 1; i <= r.cfg.MaxIterations; i++ {
20✔
288
                select {
12✔
289
                case <-ctx.Done():
1✔
290
                        return fmt.Errorf("task phase: %w", ctx.Err())
1✔
291
                default:
11✔
292
                }
293

294
                r.log.PrintSection(NewTaskIterationSection(i))
11✔
295

11✔
296
                result := r.claude.Run(ctx, prompt)
11✔
297
                if result.Error != nil {
13✔
298
                        if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
3✔
299
                                return err
1✔
300
                        }
1✔
301
                        return fmt.Errorf("claude execution: %w", result.Error)
1✔
302
                }
303

304
                if result.Signal == SignalCompleted {
11✔
305
                        // verify plan actually has no uncompleted checkboxes
2✔
306
                        if r.hasUncompletedTasks() {
2✔
307
                                r.log.Print("warning: completion signal received but plan still has [ ] items, continuing...")
×
308
                                continue
×
309
                        }
310
                        r.log.PrintRaw("\nall tasks completed, starting code review...\n")
2✔
311
                        return nil
2✔
312
                }
313

314
                if result.Signal == SignalFailed {
11✔
315
                        if retryCount < r.taskRetryCount {
6✔
316
                                r.log.Print("task failed, retrying...")
2✔
317
                                retryCount++
2✔
318
                                time.Sleep(r.iterationDelay)
2✔
319
                                continue
2✔
320
                        }
321
                        return errors.New("task execution failed after retry (FAILED signal received)")
2✔
322
                }
323

324
                retryCount = 0
3✔
325
                // continue with same prompt - it reads from plan file each time
3✔
326
                time.Sleep(r.iterationDelay)
3✔
327
        }
328

329
        return fmt.Errorf("max iterations (%d) reached without completion", r.cfg.MaxIterations)
1✔
330
}
331

332
// runClaudeReview runs Claude review with the given prompt until REVIEW_DONE.
333
func (r *Runner) runClaudeReview(ctx context.Context, prompt string) error {
7✔
334
        result := r.claude.Run(ctx, prompt)
7✔
335
        if result.Error != nil {
7✔
336
                if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
×
337
                        return err
×
338
                }
×
339
                return fmt.Errorf("claude execution: %w", result.Error)
×
340
        }
341

342
        if result.Signal == SignalFailed {
8✔
343
                return errors.New("review failed (FAILED signal received)")
1✔
344
        }
1✔
345

346
        if !IsReviewDone(result.Signal) {
6✔
347
                r.log.Print("warning: first review pass did not complete cleanly, continuing...")
×
348
        }
×
349

350
        return nil
6✔
351
}
352

353
// runClaudeReviewLoop runs claude review iterations using second review prompt.
354
func (r *Runner) runClaudeReviewLoop(ctx context.Context) error {
12✔
355
        // review iterations = 10% of max_iterations (min 3)
12✔
356
        maxReviewIterations := max(3, r.cfg.MaxIterations/10)
12✔
357

12✔
358
        for i := 1; i <= maxReviewIterations; i++ {
24✔
359
                select {
12✔
360
                case <-ctx.Done():
×
361
                        return fmt.Errorf("review: %w", ctx.Err())
×
362
                default:
12✔
363
                }
364

365
                r.log.PrintSection(NewClaudeReviewSection(i, ": critical/major"))
12✔
366

12✔
367
                result := r.claude.Run(ctx, r.replacePromptVariables(r.cfg.AppConfig.ReviewSecondPrompt))
12✔
368
                if result.Error != nil {
13✔
369
                        if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
2✔
370
                                return err
1✔
371
                        }
1✔
372
                        return fmt.Errorf("claude execution: %w", result.Error)
×
373
                }
374

375
                if result.Signal == SignalFailed {
11✔
376
                        return errors.New("review failed (FAILED signal received)")
×
377
                }
×
378

379
                if IsReviewDone(result.Signal) {
22✔
380
                        r.log.Print("claude review complete - no more findings")
11✔
381
                        return nil
11✔
382
                }
11✔
383

384
                r.log.Print("issues fixed, running another review iteration...")
×
385
                time.Sleep(r.iterationDelay)
×
386
        }
387

388
        r.log.Print("max claude review iterations reached, continuing...")
×
389
        return nil
×
390
}
391

392
// runCodexLoop runs the codex-claude review loop until no findings.
393
func (r *Runner) runCodexLoop(ctx context.Context) error {
8✔
394
        // skip codex phase if disabled
8✔
395
        if !r.cfg.CodexEnabled {
9✔
396
                r.log.Print("codex review disabled, skipping...")
1✔
397
                return nil
1✔
398
        }
1✔
399

400
        // codex iterations = 20% of max_iterations (min 3)
401
        maxCodexIterations := max(3, r.cfg.MaxIterations/5)
7✔
402

7✔
403
        var claudeResponse string // first iteration has no prior response
7✔
404

7✔
405
        for i := 1; i <= maxCodexIterations; i++ {
14✔
406
                select {
7✔
407
                case <-ctx.Done():
×
408
                        return fmt.Errorf("codex loop: %w", ctx.Err())
×
409
                default:
7✔
410
                }
411

412
                r.log.PrintSection(NewCodexIterationSection(i))
7✔
413

7✔
414
                // run codex analysis
7✔
415
                codexResult := r.codex.Run(ctx, r.buildCodexPrompt(i == 1, claudeResponse))
7✔
416
                if codexResult.Error != nil {
9✔
417
                        if err := r.handlePatternMatchError(codexResult.Error, "codex"); err != nil {
3✔
418
                                return err
1✔
419
                        }
1✔
420
                        return fmt.Errorf("codex execution: %w", codexResult.Error)
1✔
421
                }
422

423
                if codexResult.Output == "" {
7✔
424
                        r.log.Print("codex review returned no output, skipping...")
2✔
425
                        break
2✔
426
                }
427

428
                // show codex findings summary before Claude evaluation
429
                r.showCodexSummary(codexResult.Output)
3✔
430

3✔
431
                // pass codex output to claude for evaluation and fixing
3✔
432
                r.log.SetPhase(PhaseClaudeEval)
3✔
433
                r.log.PrintSection(NewClaudeEvalSection())
3✔
434
                claudeResult := r.claude.Run(ctx, r.buildCodexEvaluationPrompt(codexResult.Output))
3✔
435

3✔
436
                // restore codex phase for next iteration
3✔
437
                r.log.SetPhase(PhaseCodex)
3✔
438
                if claudeResult.Error != nil {
3✔
439
                        if err := r.handlePatternMatchError(claudeResult.Error, "claude"); err != nil {
×
440
                                return err
×
441
                        }
×
442
                        return fmt.Errorf("claude execution: %w", claudeResult.Error)
×
443
                }
444

445
                claudeResponse = claudeResult.Output
3✔
446

3✔
447
                // exit only when claude sees "no findings" from codex
3✔
448
                if IsCodexDone(claudeResult.Signal) {
6✔
449
                        r.log.Print("codex review complete - no more findings")
3✔
450
                        return nil
3✔
451
                }
3✔
452

453
                time.Sleep(r.iterationDelay)
×
454
        }
455

456
        r.log.Print("max codex iterations reached, continuing to next phase...")
2✔
457
        return nil
2✔
458
}
459

460
// buildCodexPrompt creates the prompt for codex review.
461
func (r *Runner) buildCodexPrompt(isFirst bool, claudeResponse string) string {
8✔
462
        // build plan context if available
8✔
463
        planContext := ""
8✔
464
        if r.cfg.PlanFile != "" {
11✔
465
                planContext = fmt.Sprintf(`
3✔
466
## Plan Context
3✔
467
The code implements the plan at: %s
3✔
468

3✔
469
---
3✔
470
`, r.resolvePlanFilePath())
3✔
471
        }
3✔
472

473
        // different diff command based on iteration
474
        var diffInstruction, diffDescription string
8✔
475
        if isFirst {
16✔
476
                defaultBranch := r.getDefaultBranch()
8✔
477
                diffInstruction = fmt.Sprintf("Run: git diff %s...HEAD", defaultBranch)
8✔
478
                diffDescription = fmt.Sprintf("code changes between %s and HEAD branch", defaultBranch)
8✔
479
        } else {
8✔
480
                diffInstruction = "Run: git diff"
×
481
                diffDescription = "uncommitted changes (Claude's fixes from previous iteration)"
×
482
        }
×
483

484
        basePrompt := fmt.Sprintf(`%sReview the %s.
8✔
485

8✔
486
%s
8✔
487

8✔
488
Analyze for:
8✔
489
- Bugs and logic errors
8✔
490
- Security vulnerabilities
8✔
491
- Race conditions
8✔
492
- Error handling gaps
8✔
493
- Code quality issues
8✔
494

8✔
495
Report findings with file:line references. If no issues found, say "NO ISSUES FOUND".`, planContext, diffDescription, diffInstruction)
8✔
496

8✔
497
        if claudeResponse != "" {
8✔
498
                return fmt.Sprintf(`%s
×
499

×
500
---
×
501
PREVIOUS REVIEW CONTEXT:
×
502
Claude (previous reviewer) responded to your findings:
×
503

×
504
%s
×
505

×
506
Re-evaluate considering Claude's arguments. If Claude's fixes are correct, acknowledge them.
×
507
If Claude's arguments are invalid, explain why the issues still exist.`, basePrompt, claudeResponse)
×
508
        }
×
509

510
        return basePrompt
8✔
511
}
512

513
// hasUncompletedTasks checks if plan file has any uncompleted checkboxes.
514
func (r *Runner) hasUncompletedTasks() bool {
7✔
515
        content, err := os.ReadFile(r.resolvePlanFilePath())
7✔
516
        if err != nil {
7✔
517
                return true // assume incomplete if can't read
×
518
        }
×
519

520
        // look for uncompleted checkbox pattern: [ ] (not [x])
521
        for line := range strings.SplitSeq(string(content), "\n") {
24✔
522
                trimmed := strings.TrimSpace(line)
17✔
523
                if strings.HasPrefix(trimmed, "- [ ]") {
20✔
524
                        return true
3✔
525
                }
3✔
526
        }
527
        return false
4✔
528
}
529

530
// showCodexSummary displays a condensed summary of codex output before Claude evaluation.
531
// extracts text until first code block or 500 chars, whichever is shorter.
532
func (r *Runner) showCodexSummary(output string) {
3✔
533
        summary := output
3✔
534

3✔
535
        // trim to first code block if present
3✔
536
        if idx := strings.Index(summary, "```"); idx > 0 {
3✔
537
                summary = summary[:idx]
×
538
        }
×
539

540
        // limit to 5000 chars
541
        if len(summary) > 5000 {
3✔
542
                summary = summary[:5000] + "..."
×
543
        }
×
544

545
        summary = strings.TrimSpace(summary)
3✔
546
        if summary == "" {
3✔
547
                return
×
548
        }
×
549

550
        r.log.Print("codex findings:")
3✔
551
        for line := range strings.SplitSeq(summary, "\n") {
6✔
552
                if strings.TrimSpace(line) == "" {
3✔
553
                        continue
×
554
                }
555
                r.log.PrintAligned("  " + line)
3✔
556
        }
557
}
558

559
// ErrUserRejectedPlan is returned when user rejects the plan draft.
560
var ErrUserRejectedPlan = errors.New("user rejected plan")
561

562
// draftReviewResult holds the result of draft review handling.
563
type draftReviewResult struct {
564
        handled  bool   // true if draft was found and handled
565
        feedback string // revision feedback (non-empty only for "revise" action)
566
        err      error  // error if review failed or user rejected
567
}
568

569
// handlePlanDraft processes PLAN_DRAFT signal if present in output.
570
// returns result indicating whether draft was handled and any feedback/errors.
571
func (r *Runner) handlePlanDraft(ctx context.Context, output string) draftReviewResult {
15✔
572
        planContent, draftErr := ParsePlanDraftPayload(output)
15✔
573
        if draftErr != nil {
24✔
574
                // log malformed signals (but not "no signal" which is expected)
9✔
575
                if !errors.Is(draftErr, ErrNoPlanDraftSignal) {
10✔
576
                        r.log.Print("warning: %v", draftErr)
1✔
577
                }
1✔
578
                return draftReviewResult{handled: false}
9✔
579
        }
580

581
        r.log.Print("plan draft ready for review")
6✔
582

6✔
583
        action, feedback, askErr := r.inputCollector.AskDraftReview(ctx, "Review the plan draft", planContent)
6✔
584
        if askErr != nil {
7✔
585
                return draftReviewResult{handled: true, err: fmt.Errorf("collect draft review: %w", askErr)}
1✔
586
        }
1✔
587

588
        // log the draft review action and feedback to progress file
589
        r.log.LogDraftReview(action, feedback)
5✔
590

5✔
591
        switch action {
5✔
592
        case "accept":
3✔
593
                r.log.Print("draft accepted, continuing to write plan file...")
3✔
594
                return draftReviewResult{handled: true}
3✔
595
        case "revise":
1✔
596
                r.log.Print("revision requested, re-running with feedback...")
1✔
597
                return draftReviewResult{handled: true, feedback: feedback}
1✔
598
        case "reject":
1✔
599
                r.log.Print("plan rejected by user")
1✔
600
                return draftReviewResult{handled: true, err: ErrUserRejectedPlan}
1✔
601
        }
602

603
        return draftReviewResult{handled: true}
×
604
}
605

606
// handlePlanQuestion processes QUESTION signal if present in output.
607
// returns true if question was found and handled, false otherwise.
608
// returns error if question handling failed.
609
func (r *Runner) handlePlanQuestion(ctx context.Context, output string) (bool, error) {
9✔
610
        question, err := ParseQuestionPayload(output)
9✔
611
        if err != nil {
15✔
612
                // log malformed signals (but not "no signal" which is expected)
6✔
613
                if !errors.Is(err, ErrNoQuestionSignal) {
6✔
614
                        r.log.Print("warning: %v", err)
×
615
                }
×
616
                return false, nil
6✔
617
        }
618

619
        r.log.LogQuestion(question.Question, question.Options)
3✔
620

3✔
621
        answer, askErr := r.inputCollector.AskQuestion(ctx, question.Question, question.Options)
3✔
622
        if askErr != nil {
4✔
623
                return true, fmt.Errorf("collect answer: %w", askErr)
1✔
624
        }
1✔
625

626
        r.log.LogAnswer(answer)
2✔
627
        return true, nil
2✔
628
}
629

630
// runPlanCreation executes the interactive plan creation loop.
631
// the loop continues until PLAN_READY signal or max iterations reached.
632
// handles QUESTION signals for Q&A and PLAN_DRAFT signals for draft review.
633
func (r *Runner) runPlanCreation(ctx context.Context) error {
16✔
634
        if r.cfg.PlanDescription == "" {
17✔
635
                return errors.New("plan description required for plan mode")
1✔
636
        }
1✔
637
        if r.inputCollector == nil {
16✔
638
                return errors.New("input collector required for plan mode")
1✔
639
        }
1✔
640

641
        r.log.SetPhase(PhasePlan)
14✔
642
        r.log.PrintRaw("starting interactive plan creation\n")
14✔
643
        r.log.Print("plan request: %s", r.cfg.PlanDescription)
14✔
644

14✔
645
        // plan iterations use 20% of max_iterations (min 5)
14✔
646
        maxPlanIterations := max(5, r.cfg.MaxIterations/5)
14✔
647

14✔
648
        // track revision feedback for context in next iteration
14✔
649
        var lastRevisionFeedback string
14✔
650

14✔
651
        for i := 1; i <= maxPlanIterations; i++ {
39✔
652
                select {
25✔
653
                case <-ctx.Done():
1✔
654
                        return fmt.Errorf("plan creation: %w", ctx.Err())
1✔
655
                default:
24✔
656
                }
657

658
                r.log.PrintSection(NewPlanIterationSection(i))
24✔
659

24✔
660
                prompt := r.buildPlanPrompt()
24✔
661
                // append revision feedback context if present
24✔
662
                if lastRevisionFeedback != "" {
25✔
663
                        prompt = fmt.Sprintf("%s\n\n---\nPREVIOUS DRAFT FEEDBACK:\nUser requested revisions with this feedback:\n%s\n\nPlease revise the plan accordingly and present a new PLAN_DRAFT.", prompt, lastRevisionFeedback)
1✔
664
                        lastRevisionFeedback = "" // clear after use
1✔
665
                }
1✔
666

667
                result := r.claude.Run(ctx, prompt)
24✔
668
                if result.Error != nil {
26✔
669
                        if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
3✔
670
                                return err
1✔
671
                        }
1✔
672
                        return fmt.Errorf("claude execution: %w", result.Error)
1✔
673
                }
674

675
                if result.Signal == SignalFailed {
23✔
676
                        return errors.New("plan creation failed (FAILED signal received)")
1✔
677
                }
1✔
678

679
                // check for PLAN_READY signal
680
                if IsPlanReady(result.Signal) {
27✔
681
                        r.log.Print("plan creation completed")
6✔
682
                        return nil
6✔
683
                }
6✔
684

685
                // check for PLAN_DRAFT signal - present draft for user review
686
                draftResult := r.handlePlanDraft(ctx, result.Output)
15✔
687
                if draftResult.err != nil {
17✔
688
                        return draftResult.err
2✔
689
                }
2✔
690
                if draftResult.handled {
17✔
691
                        lastRevisionFeedback = draftResult.feedback
4✔
692
                        time.Sleep(r.iterationDelay)
4✔
693
                        continue
4✔
694
                }
695

696
                // check for QUESTION signal
697
                handled, err := r.handlePlanQuestion(ctx, result.Output)
9✔
698
                if err != nil {
10✔
699
                        return err
1✔
700
                }
1✔
701
                if handled {
10✔
702
                        time.Sleep(r.iterationDelay)
2✔
703
                        continue
2✔
704
                }
705

706
                // no question, no draft, and no completion - continue
707
                time.Sleep(r.iterationDelay)
6✔
708
        }
709

710
        return fmt.Errorf("max plan iterations (%d) reached without completion", maxPlanIterations)
1✔
711
}
712

713
// handlePatternMatchError checks if err is a PatternMatchError and logs appropriate messages.
714
// Returns the error if it's a pattern match (to trigger graceful exit), nil otherwise.
715
func (r *Runner) handlePatternMatchError(err error, tool string) error {
7✔
716
        var patternErr *executor.PatternMatchError
7✔
717
        if errors.As(err, &patternErr) {
11✔
718
                r.log.Print("error: detected %q in %s output", patternErr.Pattern, tool)
4✔
719
                r.log.Print("run '%s' for more information", patternErr.HelpCmd)
4✔
720
                return err
4✔
721
        }
4✔
722
        return nil
3✔
723
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc