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

umputun / ralphex / 21656838493

04 Feb 2026 03:06AM UTC coverage: 80.72%. First build
21656838493

push

github

Claude
feat: add finalize step for optional post-completion actions

Add optional finalize phase that runs after successful review phases
(ModeFull, ModeReview, ModeCodexOnly). Disabled by default.

- Add PhaseFinalize constant with task color (green)
- Add finalize_enabled config option (default: false)
- Add finalize.txt prompt with commit rebase workflow
- Implement runFinalize() method (best-effort, runs once)
- Call finalize from review-completing modes
- Add tests for finalize step behavior
- Document in CLAUDE.md and README.md

46 of 52 new or added lines in 6 files covered. (88.46%)

4283 of 5306 relevant lines covered (80.72%)

134.64 hits per line

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

85.17
/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
        ModeTasksOnly Mode = "tasks-only" // run only task phase, skip all reviews
28
        ModePlan      Mode = "plan"       // interactive plan creation mode
29
)
30

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

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

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

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

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

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

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

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

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

131
        return NewWithExecutors(cfg, log, claudeExec, codexExec)
1✔
132
}
133

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

142
        // determine task retry count from config
143
        // appConfig.TaskRetryCountSet means user explicitly set it (even to 0 for no retries)
144
        retryCount := 1
54✔
145
        if cfg.AppConfig != nil && cfg.AppConfig.TaskRetryCountSet {
96✔
146
                retryCount = cfg.TaskRetryCount
42✔
147
        } else if cfg.TaskRetryCount > 0 {
55✔
148
                retryCount = cfg.TaskRetryCount
1✔
149
        }
1✔
150

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

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

166
// Run executes the main loop based on configured mode.
167
func (r *Runner) Run(ctx context.Context) error {
44✔
168
        switch r.cfg.Mode {
44✔
169
        case ModeFull:
13✔
170
                return r.runFull(ctx)
13✔
171
        case ModeReview:
6✔
172
                return r.runReviewOnly(ctx)
6✔
173
        case ModeCodexOnly:
4✔
174
                return r.runCodexOnly(ctx)
4✔
175
        case ModeTasksOnly:
4✔
176
                return r.runTasksOnly(ctx)
4✔
177
        case ModePlan:
16✔
178
                return r.runPlanCreation(ctx)
16✔
179
        default:
1✔
180
                return fmt.Errorf("unknown mode: %s", r.cfg.Mode)
1✔
181
        }
182
}
183

184
// runFull executes the complete pipeline: tasks → review → codex → review.
185
func (r *Runner) runFull(ctx context.Context) error {
13✔
186
        if r.cfg.PlanFile == "" {
14✔
187
                return errors.New("plan file required for full mode")
1✔
188
        }
1✔
189

190
        // phase 1: task execution
191
        r.log.SetPhase(PhaseTask)
12✔
192
        r.log.PrintRaw("starting task execution phase\n")
12✔
193

12✔
194
        if err := r.runTaskPhase(ctx); err != nil {
18✔
195
                return fmt.Errorf("task phase: %w", err)
6✔
196
        }
6✔
197

198
        // phase 2: first review pass - address ALL findings
199
        r.log.SetPhase(PhaseReview)
6✔
200
        r.log.PrintSection(NewGenericSection("claude review 0: all findings"))
6✔
201

6✔
202
        if err := r.runClaudeReview(ctx, r.replacePromptVariables(r.cfg.AppConfig.ReviewFirstPrompt)); err != nil {
6✔
203
                return fmt.Errorf("first review: %w", err)
×
204
        }
×
205

206
        // phase 2.1: claude review loop (critical/major) before codex
207
        if err := r.runClaudeReviewLoop(ctx); err != nil {
6✔
208
                return fmt.Errorf("pre-codex review loop: %w", err)
×
209
        }
×
210

211
        // phase 2.5: codex external review loop
212
        r.log.SetPhase(PhaseCodex)
6✔
213
        r.log.PrintSection(NewGenericSection("codex external review"))
6✔
214

6✔
215
        if err := r.runCodexLoop(ctx); err != nil {
6✔
216
                return fmt.Errorf("codex loop: %w", err)
×
217
        }
×
218

219
        // phase 3: claude review loop (critical/major) after codex
220
        r.log.SetPhase(PhaseReview)
6✔
221

6✔
222
        if err := r.runClaudeReviewLoop(ctx); err != nil {
6✔
223
                return fmt.Errorf("post-codex review loop: %w", err)
×
224
        }
×
225

226
        // optional finalize step (best-effort)
227
        r.runFinalize(ctx)
6✔
228

6✔
229
        r.log.Print("all phases completed successfully")
6✔
230
        return nil
6✔
231
}
232

233
// runReviewOnly executes only the review pipeline: review → codex → review.
234
func (r *Runner) runReviewOnly(ctx context.Context) error {
6✔
235
        // phase 1: first review
6✔
236
        r.log.SetPhase(PhaseReview)
6✔
237
        r.log.PrintSection(NewGenericSection("claude review 0: all findings"))
6✔
238

6✔
239
        if err := r.runClaudeReview(ctx, r.replacePromptVariables(r.cfg.AppConfig.ReviewFirstPrompt)); err != nil {
7✔
240
                return fmt.Errorf("first review: %w", err)
1✔
241
        }
1✔
242

243
        // phase 1.1: claude review loop (critical/major) before codex
244
        if err := r.runClaudeReviewLoop(ctx); err != nil {
6✔
245
                return fmt.Errorf("pre-codex review loop: %w", err)
1✔
246
        }
1✔
247

248
        // phase 2: codex external review loop
249
        r.log.SetPhase(PhaseCodex)
4✔
250
        r.log.PrintSection(NewGenericSection("codex external review"))
4✔
251

4✔
252
        if err := r.runCodexLoop(ctx); err != nil {
6✔
253
                return fmt.Errorf("codex loop: %w", err)
2✔
254
        }
2✔
255

256
        // phase 3: claude review loop (critical/major) after codex
257
        r.log.SetPhase(PhaseReview)
2✔
258

2✔
259
        if err := r.runClaudeReviewLoop(ctx); err != nil {
2✔
260
                return fmt.Errorf("post-codex review loop: %w", err)
×
261
        }
×
262

263
        // optional finalize step (best-effort)
264
        r.runFinalize(ctx)
2✔
265

2✔
266
        r.log.Print("review phases completed successfully")
2✔
267
        return nil
2✔
268
}
269

270
// runCodexOnly executes only the codex pipeline: codex → review.
271
func (r *Runner) runCodexOnly(ctx context.Context) error {
4✔
272
        // phase 1: codex external review loop
4✔
273
        r.log.SetPhase(PhaseCodex)
4✔
274
        r.log.PrintSection(NewGenericSection("codex external review"))
4✔
275

4✔
276
        if err := r.runCodexLoop(ctx); err != nil {
4✔
277
                return fmt.Errorf("codex loop: %w", err)
×
278
        }
×
279

280
        // phase 2: claude review loop (critical/major) after codex
281
        r.log.SetPhase(PhaseReview)
4✔
282

4✔
283
        if err := r.runClaudeReviewLoop(ctx); err != nil {
4✔
284
                return fmt.Errorf("post-codex review loop: %w", err)
×
285
        }
×
286

287
        // optional finalize step (best-effort)
288
        r.runFinalize(ctx)
4✔
289

4✔
290
        r.log.Print("codex phases completed successfully")
4✔
291
        return nil
4✔
292
}
293

294
// runTasksOnly executes only task phase, skipping all reviews.
295
func (r *Runner) runTasksOnly(ctx context.Context) error {
4✔
296
        if r.cfg.PlanFile == "" {
5✔
297
                return errors.New("plan file required for tasks-only mode")
1✔
298
        }
1✔
299

300
        r.log.SetPhase(PhaseTask)
3✔
301
        r.log.PrintRaw("starting task execution phase\n")
3✔
302

3✔
303
        if err := r.runTaskPhase(ctx); err != nil {
4✔
304
                return fmt.Errorf("task phase: %w", err)
1✔
305
        }
1✔
306

307
        r.log.Print("task execution completed successfully")
2✔
308
        return nil
2✔
309
}
310

311
// runTaskPhase executes tasks until completion or max iterations.
312
// executes ONE Task section per iteration.
313
func (r *Runner) runTaskPhase(ctx context.Context) error {
15✔
314
        prompt := r.replacePromptVariables(r.cfg.AppConfig.TaskPrompt)
15✔
315
        retryCount := 0
15✔
316

15✔
317
        for i := 1; i <= r.cfg.MaxIterations; i++ {
34✔
318
                select {
19✔
319
                case <-ctx.Done():
1✔
320
                        return fmt.Errorf("task phase: %w", ctx.Err())
1✔
321
                default:
18✔
322
                }
323

324
                r.log.PrintSection(NewTaskIterationSection(i))
18✔
325

18✔
326
                result := r.claude.Run(ctx, prompt)
18✔
327
                if result.Error != nil {
20✔
328
                        if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
3✔
329
                                return err
1✔
330
                        }
1✔
331
                        return fmt.Errorf("claude execution: %w", result.Error)
1✔
332
                }
333

334
                if result.Signal == SignalCompleted {
24✔
335
                        // verify plan actually has no uncompleted checkboxes
8✔
336
                        if r.hasUncompletedTasks() {
8✔
337
                                r.log.Print("warning: completion signal received but plan still has [ ] items, continuing...")
×
338
                                continue
×
339
                        }
340
                        r.log.PrintRaw("\nall tasks completed, starting code review...\n")
8✔
341
                        return nil
8✔
342
                }
343

344
                if result.Signal == SignalFailed {
13✔
345
                        if retryCount < r.taskRetryCount {
7✔
346
                                r.log.Print("task failed, retrying...")
2✔
347
                                retryCount++
2✔
348
                                time.Sleep(r.iterationDelay)
2✔
349
                                continue
2✔
350
                        }
351
                        return errors.New("task execution failed after retry (FAILED signal received)")
3✔
352
                }
353

354
                retryCount = 0
3✔
355
                // continue with same prompt - it reads from plan file each time
3✔
356
                time.Sleep(r.iterationDelay)
3✔
357
        }
358

359
        return fmt.Errorf("max iterations (%d) reached without completion", r.cfg.MaxIterations)
1✔
360
}
361

362
// runClaudeReview runs Claude review with the given prompt until REVIEW_DONE.
363
func (r *Runner) runClaudeReview(ctx context.Context, prompt string) error {
12✔
364
        result := r.claude.Run(ctx, prompt)
12✔
365
        if result.Error != nil {
12✔
366
                if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
×
367
                        return err
×
368
                }
×
369
                return fmt.Errorf("claude execution: %w", result.Error)
×
370
        }
371

372
        if result.Signal == SignalFailed {
13✔
373
                return errors.New("review failed (FAILED signal received)")
1✔
374
        }
1✔
375

376
        if !IsReviewDone(result.Signal) {
11✔
377
                r.log.Print("warning: first review pass did not complete cleanly, continuing...")
×
378
        }
×
379

380
        return nil
11✔
381
}
382

383
// runClaudeReviewLoop runs claude review iterations using second review prompt.
384
func (r *Runner) runClaudeReviewLoop(ctx context.Context) error {
23✔
385
        // review iterations = 10% of max_iterations (min 3)
23✔
386
        maxReviewIterations := max(3, r.cfg.MaxIterations/10)
23✔
387

23✔
388
        for i := 1; i <= maxReviewIterations; i++ {
46✔
389
                select {
23✔
390
                case <-ctx.Done():
×
391
                        return fmt.Errorf("review: %w", ctx.Err())
×
392
                default:
23✔
393
                }
394

395
                r.log.PrintSection(NewClaudeReviewSection(i, ": critical/major"))
23✔
396

23✔
397
                result := r.claude.Run(ctx, r.replacePromptVariables(r.cfg.AppConfig.ReviewSecondPrompt))
23✔
398
                if result.Error != nil {
24✔
399
                        if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
2✔
400
                                return err
1✔
401
                        }
1✔
402
                        return fmt.Errorf("claude execution: %w", result.Error)
×
403
                }
404

405
                if result.Signal == SignalFailed {
22✔
406
                        return errors.New("review failed (FAILED signal received)")
×
407
                }
×
408

409
                if IsReviewDone(result.Signal) {
44✔
410
                        r.log.Print("claude review complete - no more findings")
22✔
411
                        return nil
22✔
412
                }
22✔
413

414
                r.log.Print("issues fixed, running another review iteration...")
×
415
                time.Sleep(r.iterationDelay)
×
416
        }
417

418
        r.log.Print("max claude review iterations reached, continuing...")
×
419
        return nil
×
420
}
421

422
// runCodexLoop runs the codex-claude review loop until no findings.
423
func (r *Runner) runCodexLoop(ctx context.Context) error {
14✔
424
        // skip codex phase if disabled
14✔
425
        if !r.cfg.CodexEnabled {
21✔
426
                r.log.Print("codex review disabled, skipping...")
7✔
427
                return nil
7✔
428
        }
7✔
429

430
        // codex iterations = 20% of max_iterations (min 3)
431
        maxCodexIterations := max(3, r.cfg.MaxIterations/5)
7✔
432

7✔
433
        var claudeResponse string // first iteration has no prior response
7✔
434

7✔
435
        for i := 1; i <= maxCodexIterations; i++ {
14✔
436
                select {
7✔
437
                case <-ctx.Done():
×
438
                        return fmt.Errorf("codex loop: %w", ctx.Err())
×
439
                default:
7✔
440
                }
441

442
                r.log.PrintSection(NewCodexIterationSection(i))
7✔
443

7✔
444
                // run codex analysis
7✔
445
                codexResult := r.codex.Run(ctx, r.buildCodexPrompt(i == 1, claudeResponse))
7✔
446
                if codexResult.Error != nil {
9✔
447
                        if err := r.handlePatternMatchError(codexResult.Error, "codex"); err != nil {
3✔
448
                                return err
1✔
449
                        }
1✔
450
                        return fmt.Errorf("codex execution: %w", codexResult.Error)
1✔
451
                }
452

453
                if codexResult.Output == "" {
7✔
454
                        r.log.Print("codex review returned no output, skipping...")
2✔
455
                        break
2✔
456
                }
457

458
                // show codex findings summary before Claude evaluation
459
                r.showCodexSummary(codexResult.Output)
3✔
460

3✔
461
                // pass codex output to claude for evaluation and fixing
3✔
462
                r.log.SetPhase(PhaseClaudeEval)
3✔
463
                r.log.PrintSection(NewClaudeEvalSection())
3✔
464
                claudeResult := r.claude.Run(ctx, r.buildCodexEvaluationPrompt(codexResult.Output))
3✔
465

3✔
466
                // restore codex phase for next iteration
3✔
467
                r.log.SetPhase(PhaseCodex)
3✔
468
                if claudeResult.Error != nil {
3✔
469
                        if err := r.handlePatternMatchError(claudeResult.Error, "claude"); err != nil {
×
470
                                return err
×
471
                        }
×
472
                        return fmt.Errorf("claude execution: %w", claudeResult.Error)
×
473
                }
474

475
                claudeResponse = claudeResult.Output
3✔
476

3✔
477
                // exit only when claude sees "no findings" from codex
3✔
478
                if IsCodexDone(claudeResult.Signal) {
6✔
479
                        r.log.Print("codex review complete - no more findings")
3✔
480
                        return nil
3✔
481
                }
3✔
482

483
                time.Sleep(r.iterationDelay)
×
484
        }
485

486
        r.log.Print("max codex iterations reached, continuing to next phase...")
2✔
487
        return nil
2✔
488
}
489

490
// buildCodexPrompt creates the prompt for codex review.
491
func (r *Runner) buildCodexPrompt(isFirst bool, claudeResponse string) string {
8✔
492
        // build plan context if available
8✔
493
        planContext := ""
8✔
494
        if r.cfg.PlanFile != "" {
11✔
495
                planContext = fmt.Sprintf(`
3✔
496
## Plan Context
3✔
497
The code implements the plan at: %s
3✔
498

3✔
499
---
3✔
500
`, r.resolvePlanFilePath())
3✔
501
        }
3✔
502

503
        // different diff command based on iteration
504
        var diffInstruction, diffDescription string
8✔
505
        if isFirst {
16✔
506
                defaultBranch := r.getDefaultBranch()
8✔
507
                diffInstruction = fmt.Sprintf("Run: git diff %s...HEAD", defaultBranch)
8✔
508
                diffDescription = fmt.Sprintf("code changes between %s and HEAD branch", defaultBranch)
8✔
509
        } else {
8✔
510
                diffInstruction = "Run: git diff"
×
511
                diffDescription = "uncommitted changes (Claude's fixes from previous iteration)"
×
512
        }
×
513

514
        basePrompt := fmt.Sprintf(`%sReview the %s.
8✔
515

8✔
516
%s
8✔
517

8✔
518
Analyze for:
8✔
519
- Bugs and logic errors
8✔
520
- Security vulnerabilities
8✔
521
- Race conditions
8✔
522
- Error handling gaps
8✔
523
- Code quality issues
8✔
524

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

8✔
527
        if claudeResponse != "" {
8✔
528
                return fmt.Sprintf(`%s
×
529

×
530
---
×
531
PREVIOUS REVIEW CONTEXT:
×
532
Claude (previous reviewer) responded to your findings:
×
533

×
534
%s
×
535

×
536
Re-evaluate considering Claude's arguments. If Claude's fixes are correct, acknowledge them.
×
537
If Claude's arguments are invalid, explain why the issues still exist.`, basePrompt, claudeResponse)
×
538
        }
×
539

540
        return basePrompt
8✔
541
}
542

543
// hasUncompletedTasks checks if plan file has any uncompleted checkboxes.
544
func (r *Runner) hasUncompletedTasks() bool {
13✔
545
        content, err := os.ReadFile(r.resolvePlanFilePath())
13✔
546
        if err != nil {
13✔
547
                return true // assume incomplete if can't read
×
548
        }
×
549

550
        // look for uncompleted checkbox pattern: [ ] (not [x])
551
        for line := range strings.SplitSeq(string(content), "\n") {
43✔
552
                trimmed := strings.TrimSpace(line)
30✔
553
                if strings.HasPrefix(trimmed, "- [ ]") {
33✔
554
                        return true
3✔
555
                }
3✔
556
        }
557
        return false
10✔
558
}
559

560
// showCodexSummary displays a condensed summary of codex output before Claude evaluation.
561
// extracts text until first code block or 500 chars, whichever is shorter.
562
func (r *Runner) showCodexSummary(output string) {
3✔
563
        summary := output
3✔
564

3✔
565
        // trim to first code block if present
3✔
566
        if idx := strings.Index(summary, "```"); idx > 0 {
3✔
567
                summary = summary[:idx]
×
568
        }
×
569

570
        // limit to 5000 chars
571
        if len(summary) > 5000 {
3✔
572
                summary = summary[:5000] + "..."
×
573
        }
×
574

575
        summary = strings.TrimSpace(summary)
3✔
576
        if summary == "" {
3✔
577
                return
×
578
        }
×
579

580
        r.log.Print("codex findings:")
3✔
581
        for line := range strings.SplitSeq(summary, "\n") {
6✔
582
                if strings.TrimSpace(line) == "" {
3✔
583
                        continue
×
584
                }
585
                r.log.PrintAligned("  " + line)
3✔
586
        }
587
}
588

589
// ErrUserRejectedPlan is returned when user rejects the plan draft.
590
var ErrUserRejectedPlan = errors.New("user rejected plan")
591

592
// draftReviewResult holds the result of draft review handling.
593
type draftReviewResult struct {
594
        handled  bool   // true if draft was found and handled
595
        feedback string // revision feedback (non-empty only for "revise" action)
596
        err      error  // error if review failed or user rejected
597
}
598

599
// handlePlanDraft processes PLAN_DRAFT signal if present in output.
600
// returns result indicating whether draft was handled and any feedback/errors.
601
func (r *Runner) handlePlanDraft(ctx context.Context, output string) draftReviewResult {
15✔
602
        planContent, draftErr := ParsePlanDraftPayload(output)
15✔
603
        if draftErr != nil {
24✔
604
                // log malformed signals (but not "no signal" which is expected)
9✔
605
                if !errors.Is(draftErr, ErrNoPlanDraftSignal) {
10✔
606
                        r.log.Print("warning: %v", draftErr)
1✔
607
                }
1✔
608
                return draftReviewResult{handled: false}
9✔
609
        }
610

611
        r.log.Print("plan draft ready for review")
6✔
612

6✔
613
        action, feedback, askErr := r.inputCollector.AskDraftReview(ctx, "Review the plan draft", planContent)
6✔
614
        if askErr != nil {
7✔
615
                return draftReviewResult{handled: true, err: fmt.Errorf("collect draft review: %w", askErr)}
1✔
616
        }
1✔
617

618
        // log the draft review action and feedback to progress file
619
        r.log.LogDraftReview(action, feedback)
5✔
620

5✔
621
        switch action {
5✔
622
        case "accept":
3✔
623
                r.log.Print("draft accepted, continuing to write plan file...")
3✔
624
                return draftReviewResult{handled: true}
3✔
625
        case "revise":
1✔
626
                r.log.Print("revision requested, re-running with feedback...")
1✔
627
                return draftReviewResult{handled: true, feedback: feedback}
1✔
628
        case "reject":
1✔
629
                r.log.Print("plan rejected by user")
1✔
630
                return draftReviewResult{handled: true, err: ErrUserRejectedPlan}
1✔
631
        }
632

633
        return draftReviewResult{handled: true}
×
634
}
635

636
// handlePlanQuestion processes QUESTION signal if present in output.
637
// returns true if question was found and handled, false otherwise.
638
// returns error if question handling failed.
639
func (r *Runner) handlePlanQuestion(ctx context.Context, output string) (bool, error) {
9✔
640
        question, err := ParseQuestionPayload(output)
9✔
641
        if err != nil {
15✔
642
                // log malformed signals (but not "no signal" which is expected)
6✔
643
                if !errors.Is(err, ErrNoQuestionSignal) {
6✔
644
                        r.log.Print("warning: %v", err)
×
645
                }
×
646
                return false, nil
6✔
647
        }
648

649
        r.log.LogQuestion(question.Question, question.Options)
3✔
650

3✔
651
        answer, askErr := r.inputCollector.AskQuestion(ctx, question.Question, question.Options)
3✔
652
        if askErr != nil {
4✔
653
                return true, fmt.Errorf("collect answer: %w", askErr)
1✔
654
        }
1✔
655

656
        r.log.LogAnswer(answer)
2✔
657
        return true, nil
2✔
658
}
659

660
// runPlanCreation executes the interactive plan creation loop.
661
// the loop continues until PLAN_READY signal or max iterations reached.
662
// handles QUESTION signals for Q&A and PLAN_DRAFT signals for draft review.
663
func (r *Runner) runPlanCreation(ctx context.Context) error {
16✔
664
        if r.cfg.PlanDescription == "" {
17✔
665
                return errors.New("plan description required for plan mode")
1✔
666
        }
1✔
667
        if r.inputCollector == nil {
16✔
668
                return errors.New("input collector required for plan mode")
1✔
669
        }
1✔
670

671
        r.log.SetPhase(PhasePlan)
14✔
672
        r.log.PrintRaw("starting interactive plan creation\n")
14✔
673
        r.log.Print("plan request: %s", r.cfg.PlanDescription)
14✔
674

14✔
675
        // plan iterations use 20% of max_iterations (min 5)
14✔
676
        maxPlanIterations := max(5, r.cfg.MaxIterations/5)
14✔
677

14✔
678
        // track revision feedback for context in next iteration
14✔
679
        var lastRevisionFeedback string
14✔
680

14✔
681
        for i := 1; i <= maxPlanIterations; i++ {
39✔
682
                select {
25✔
683
                case <-ctx.Done():
1✔
684
                        return fmt.Errorf("plan creation: %w", ctx.Err())
1✔
685
                default:
24✔
686
                }
687

688
                r.log.PrintSection(NewPlanIterationSection(i))
24✔
689

24✔
690
                prompt := r.buildPlanPrompt()
24✔
691
                // append revision feedback context if present
24✔
692
                if lastRevisionFeedback != "" {
25✔
693
                        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✔
694
                        lastRevisionFeedback = "" // clear after use
1✔
695
                }
1✔
696

697
                result := r.claude.Run(ctx, prompt)
24✔
698
                if result.Error != nil {
26✔
699
                        if err := r.handlePatternMatchError(result.Error, "claude"); err != nil {
3✔
700
                                return err
1✔
701
                        }
1✔
702
                        return fmt.Errorf("claude execution: %w", result.Error)
1✔
703
                }
704

705
                if result.Signal == SignalFailed {
23✔
706
                        return errors.New("plan creation failed (FAILED signal received)")
1✔
707
                }
1✔
708

709
                // check for PLAN_READY signal
710
                if IsPlanReady(result.Signal) {
27✔
711
                        r.log.Print("plan creation completed")
6✔
712
                        return nil
6✔
713
                }
6✔
714

715
                // check for PLAN_DRAFT signal - present draft for user review
716
                draftResult := r.handlePlanDraft(ctx, result.Output)
15✔
717
                if draftResult.err != nil {
17✔
718
                        return draftResult.err
2✔
719
                }
2✔
720
                if draftResult.handled {
17✔
721
                        lastRevisionFeedback = draftResult.feedback
4✔
722
                        time.Sleep(r.iterationDelay)
4✔
723
                        continue
4✔
724
                }
725

726
                // check for QUESTION signal
727
                handled, err := r.handlePlanQuestion(ctx, result.Output)
9✔
728
                if err != nil {
10✔
729
                        return err
1✔
730
                }
1✔
731
                if handled {
10✔
732
                        time.Sleep(r.iterationDelay)
2✔
733
                        continue
2✔
734
                }
735

736
                // no question, no draft, and no completion - continue
737
                time.Sleep(r.iterationDelay)
6✔
738
        }
739

740
        return fmt.Errorf("max plan iterations (%d) reached without completion", maxPlanIterations)
1✔
741
}
742

743
// handlePatternMatchError checks if err is a PatternMatchError and logs appropriate messages.
744
// Returns the error if it's a pattern match (to trigger graceful exit), nil otherwise.
745
func (r *Runner) handlePatternMatchError(err error, tool string) error {
7✔
746
        var patternErr *executor.PatternMatchError
7✔
747
        if errors.As(err, &patternErr) {
11✔
748
                r.log.Print("error: detected %q in %s output", patternErr.Pattern, tool)
4✔
749
                r.log.Print("run '%s' for more information", patternErr.HelpCmd)
4✔
750
                return err
4✔
751
        }
4✔
752
        return nil
3✔
753
}
754

755
// runFinalize executes the optional finalize step after successful reviews.
756
// runs once, best-effort: failures are logged but don't block success.
757
func (r *Runner) runFinalize(ctx context.Context) {
12✔
758
        if !r.cfg.FinalizeEnabled {
19✔
759
                return
7✔
760
        }
7✔
761

762
        r.log.SetPhase(PhaseFinalize)
5✔
763
        r.log.PrintSection(NewGenericSection("finalize step"))
5✔
764

5✔
765
        prompt := r.replacePromptVariables(r.cfg.AppConfig.FinalizePrompt)
5✔
766
        result := r.claude.Run(ctx, prompt)
5✔
767

5✔
768
        if result.Error != nil {
6✔
769
                // check for pattern match (rate limit) - log but don't fail (best-effort)
1✔
770
                var patternErr *executor.PatternMatchError
1✔
771
                if errors.As(result.Error, &patternErr) {
1✔
NEW
772
                        r.log.Print("finalize step: detected %q in claude output", patternErr.Pattern)
×
NEW
773
                        r.log.Print("run '%s' for more information", patternErr.HelpCmd)
×
NEW
774
                        return
×
NEW
775
                }
×
776
                // best-effort: log error but don't fail
777
                r.log.Print("finalize step failed: %v", result.Error)
1✔
778
                return
1✔
779
        }
780

781
        if result.Signal == SignalFailed {
5✔
782
                r.log.Print("finalize step reported failure (non-blocking)")
1✔
783
                return
1✔
784
        }
1✔
785

786
        r.log.Print("finalize step completed")
3✔
787
}
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