Feature design: verification-failure self-correction loop #174

Closed
opened 2026-04-30 10:18:33 -07:00 by romaine-life-app[bot] · 1 comment
romaine-life-app[bot] commented 2026-04-30 10:18:33 -07:00 (Migrated from github.com)

Goal

When the issue-agent's own verification phase aborts with a fixable failure, the agent should re-enter the implementation phase automatically with the verification artifact as feedback, rather than dumping the diagnostic and waiting for a human to re-trigger or comment. This is the loop that defines "agentic" in the modern sense — AlphaCode, Devin, Cursor agent mode, Aider all have some form of it.

This issue is a design doc, not an implementation. Phasing at the bottom calls out an MVP slice that's useful on its own.

This is the prerequisite for the PR-comment-driven revise loop in #173 — the verification-failure loop covers self-correction before a PR is opened, the comment loop covers human-in-the-loop iteration after. Build this one first; both are useful, but a non-self-correcting agent escalates fixable failures to the human and never gets the work done.

Concrete trigger that motivates this

Run 25178505492 on issue #142 aborted with claimed_result_not_observed: the verification phase honestly read vigor gained: 88 from the screenshot and the wrapper rejected it (the new expected_text contract from #172 working as designed). The agent's own issue-agent-verification.md even sketched the likely root cause:

"The implementation patch AkabekoBeforeSideTurnStartPatch likely fires twice per combat-start event… RecordAkabekoVigorGained(8) is called twice"

That's enough information for an implementation re-attempt to plausibly fix the bug. Today the run just aborts. This is exactly the gap the self-correction loop closes.

What's already in place

The schema is half-supported by today's contracts:

  • Each phase result has a retryable: true|false field — designed exactly for this.
  • Abort reasons partition naturally into "implementation can fix this" vs "environment broke":
    • Impl-fixable (loop should retry): unit_tests_failed, target_evidence_missing, claimed_result_not_observed, screenshot_not_relevant
    • Environment (don't retry, escalate to human): scenario_setup_failed, mcp_state_mismatch, game_state_unreachable, metadata_unavailable, mcp_capability_missing
    • Scope (don't retry, abort): validation_plan_impossible, change_too_large, requires_architecture_change
  • Phase artifacts (-test-plan, -implementation, -verification) already get distinct names — adding a -attempt{N} suffix is mechanical.

What's missing

  1. Workflow loop control. After verification, the wrapper has to branch on (status, abort_reason, retryable, attempt < max_attempts). If retryable + impl-fixable + budget-left, jump back to implementation with the verification artifact as additional input. Today the workflow is a strict DAG.

  2. Implementation prompt awareness of priors. "You've tried this before, here's what failed and why" — the agent must read the prior attempt's verification.json, evidence_results, observed_text, and abort_reason, and produce a different implementation. Without this it'll just repeat the same code. The prompt needs an explicit "do not produce the same change as last attempt" rule plus structured access to the prior attempt's artifacts.

  3. Iteration ledger. Each attempt needs distinct artifacts (-attempt1, -attempt2, etc.), and the PR description / final summary needs to show the trail: "attempt 1 failed at unit_tests_failed (test X failed because Y), attempt 2 fixed X but failed verification at target_evidence_missing, attempt 3 passed."

  4. Test-plan revision (defer to v2). Sometimes the test plan is wrong, not the implementation (e.g. an unrealistic expected_text that the implementation can't physically satisfy because the contract was loose-translated wrong). The pure implementation-loop can't fix that — needs an escalation: after N implementation retries against the same test plan, escalate to a plan-revision phase. More complex; leave for v2.

  5. Budget caps. Per-attempt budget envelope (existing MaxBudgetUsd per phase) plus a per-run cumulative ceiling. Today's $15-per-phase cap × up to 4 phases × multiple attempts can blow up cost. Default cumulative cap proposal: $25 per issue (3-4 attempts at typical phase costs).

  6. "Same vs new branch" decision. Each attempt could push another commit to the same branch (preserving history) or rebase (clean diff). Recommendation: same branch, additive commits — that's how a real engineer would work, and it gives the next attempt's prompt access to the diff history of prior attempts as additional context.

MVP slice

Cleanest first cut:

  • Add a max_implementation_attempts workflow input (default 2).
  • After the verification phase, the wrapper inspects verification.json. If status=abort && retryable=true && abort_reason ∈ {unit_tests_failed, target_evidence_missing, claimed_result_not_observed, screenshot_not_relevant} AND attempt < max, set an env flag and re-run implementation + verification with the prior verification artifact in the prompt.
  • Implementation prompt grows a "If prior_verification.json exists, read it first; treat its abort_reason and notes as your top priority. Do not produce the same change as last attempt — diff what you write against the existing branch and reject if identical." rule.
  • Artifacts named with -attempt{N} suffix (path or filename — TBD).
  • Final summary lists each attempt's outcome ("attempt 1: abort/claimed_result_not_observed; attempt 2: pass").

Estimate: ~2 days at our recent pace.

Full feature

Slice What's in it Est.
MVP Loop control + prior-verification awareness + per-attempt artifacts + final summary trail ~2 days
Multi-attempt history in PR description One-line ledger entry per attempt, survives rebases (same idea as #173) +0.5 day
Budget caps Per-attempt + cumulative; abort if next attempt would exceed +0.5 day
Diff-rejection guard Wrapper-level check that attempt N's diff is genuinely different from attempt N-1's; abort if identical (catches "agent gave up and re-emitted the same code") +1 day
Test-plan revision escalation After M impl-only retries fail, allow ONE plan-revision attempt with the cumulative verification feedback; then up to M more impl retries against the new plan +2 days
Polish Edge cases (verification phase itself fails for environmental reasons mid-loop, lease expires mid-retry, branch-protection interactions), Pester coverage of the loop logic +1 day
Total full feature ~7 days

Cross-cutting

  • Telemetry: each attempt's full artifact set preserved separately so a reviewer can see the trail. The Get-ExitCodeText and Get-ClaudeCostSummary aggregations need to roll up across attempts.
  • Cost reporting: final summary line — "issue-agent spent $X.YZ across N attempts; budget remaining $A.BC."
  • Failure cascade: if all max_attempts attempts fail, the run aborts cleanly. Issue gets a comment with the trail. No PR opened.
  • Interaction with #173 (PR-comment revise): a self-corrected PR that opens after attempt 3 should still be revisable via comment — the two loops are independent. The PR-comment loop's revision counter should NOT include the pre-PR self-correction attempts.

Risks

  • Cost runaway. Each attempt is a full plan+impl+verify cycle (or impl+verify if test plan is fixed). Budget caps mitigate but don't eliminate — easy to set max_attempts=5 and watch a single issue eat $50. The default cap should be conservative.
  • Convergence on a wrong fix. Without diff-rejection, an agent that has plausibly-correct code from the start might iterate to worse code chasing a verification artifact. The diff-rejection guard helps; a per-attempt "did the unit-test pass count regress?" check would help more.
  • Verification phase itself is unreliable. If verification flakily reads "88" sometimes and "8" other times due to OCR / timing / state-pollution issues, the loop runs N times unnecessarily and either burns budget or eventually catches a flaky pass and ships a buggy PR. Verification flake-detection is a separate problem this loop assumes is solved.
  • Implementation "fix" that regresses other tests. The agent fixes the failing test but breaks two others. Need to ensure the unit-test gate compares the FULL pass set, not just the previously-failing test. Existing 99/99 contract already does this.

Decisions to make before starting

  • Default max_implementation_attempts (proposal: 2).
  • Default per-issue cumulative cost cap (proposal: $25).
  • Whether to enable test-plan revision in MVP or defer (proposal: defer).
  • Where the prior-verification artifact lives in the implementation phase prompt (full inline vs. file-path-only-and-have-the-agent-read-it).
  • Diff-rejection: include in MVP or defer (proposal: defer to "polish").
  • Whether to expose max_implementation_attempts as a per-issue label (e.g. agent-max-attempts:3) or only as a workflow-default input.
## Goal When the issue-agent's own verification phase aborts with a fixable failure, the agent should re-enter the implementation phase automatically with the verification artifact as feedback, rather than dumping the diagnostic and waiting for a human to re-trigger or comment. This is the loop that defines "agentic" in the modern sense — AlphaCode, Devin, Cursor agent mode, Aider all have some form of it. This issue is a design doc, not an implementation. Phasing at the bottom calls out an MVP slice that's useful on its own. This is the **prerequisite** for the PR-comment-driven revise loop in #173 — the verification-failure loop covers self-correction *before* a PR is opened, the comment loop covers human-in-the-loop iteration *after*. Build this one first; both are useful, but a non-self-correcting agent escalates fixable failures to the human and never gets the work done. ## Concrete trigger that motivates this [Run 25178505492](https://github.com/nelsong6/spirelens/actions/runs/25178505492) on issue #142 aborted with `claimed_result_not_observed`: the verification phase honestly read `vigor gained: 88` from the screenshot and the wrapper rejected it (the new `expected_text` contract from #172 working as designed). The agent's own `issue-agent-verification.md` even sketched the likely root cause: > "The implementation patch `AkabekoBeforeSideTurnStartPatch` likely fires twice per combat-start event… `RecordAkabekoVigorGained(8)` is called twice" That's enough information for an implementation re-attempt to plausibly fix the bug. Today the run just aborts. This is exactly the gap the self-correction loop closes. ## What's already in place The schema is half-supported by today's contracts: - Each phase result has a `retryable: true|false` field — designed exactly for this. - Abort reasons partition naturally into "implementation can fix this" vs "environment broke": - **Impl-fixable** (loop should retry): `unit_tests_failed`, `target_evidence_missing`, `claimed_result_not_observed`, `screenshot_not_relevant` - **Environment** (don't retry, escalate to human): `scenario_setup_failed`, `mcp_state_mismatch`, `game_state_unreachable`, `metadata_unavailable`, `mcp_capability_missing` - **Scope** (don't retry, abort): `validation_plan_impossible`, `change_too_large`, `requires_architecture_change` - Phase artifacts (`-test-plan`, `-implementation`, `-verification`) already get distinct names — adding a `-attempt{N}` suffix is mechanical. ## What's missing 1. **Workflow loop control.** After verification, the wrapper has to branch on `(status, abort_reason, retryable, attempt < max_attempts)`. If retryable + impl-fixable + budget-left, jump back to implementation with the verification artifact as additional input. Today the workflow is a strict DAG. 2. **Implementation prompt awareness of priors.** "You've tried this before, here's what failed and why" — the agent must read the prior attempt's `verification.json`, `evidence_results`, `observed_text`, and `abort_reason`, and produce a *different* implementation. Without this it'll just repeat the same code. The prompt needs an explicit "do not produce the same change as last attempt" rule plus structured access to the prior attempt's artifacts. 3. **Iteration ledger.** Each attempt needs distinct artifacts (`-attempt1`, `-attempt2`, etc.), and the PR description / final summary needs to show the trail: "attempt 1 failed at unit_tests_failed (test X failed because Y), attempt 2 fixed X but failed verification at target_evidence_missing, attempt 3 passed." 4. **Test-plan revision (defer to v2).** Sometimes the *test plan* is wrong, not the implementation (e.g. an unrealistic `expected_text` that the implementation can't physically satisfy because the contract was loose-translated wrong). The pure implementation-loop can't fix that — needs an escalation: after N implementation retries against the same test plan, escalate to a plan-revision phase. More complex; leave for v2. 5. **Budget caps.** Per-attempt budget envelope (existing `MaxBudgetUsd` per phase) plus a per-run cumulative ceiling. Today's $15-per-phase cap × up to 4 phases × multiple attempts can blow up cost. Default cumulative cap proposal: $25 per issue (3-4 attempts at typical phase costs). 6. **"Same vs new branch" decision.** Each attempt could push another commit to the same branch (preserving history) or rebase (clean diff). Recommendation: same branch, additive commits — that's how a real engineer would work, and it gives the next attempt's prompt access to the diff history of prior attempts as additional context. ## MVP slice Cleanest first cut: - Add a `max_implementation_attempts` workflow input (default `2`). - After the verification phase, the wrapper inspects `verification.json`. If `status=abort && retryable=true && abort_reason ∈ {unit_tests_failed, target_evidence_missing, claimed_result_not_observed, screenshot_not_relevant}` AND `attempt < max`, set an env flag and re-run implementation + verification with the prior verification artifact in the prompt. - Implementation prompt grows a "If `prior_verification.json` exists, read it first; treat its `abort_reason` and `notes` as your top priority. Do not produce the same change as last attempt — diff what you write against the existing branch and reject if identical." rule. - Artifacts named with `-attempt{N}` suffix (path or filename — TBD). - Final summary lists each attempt's outcome ("attempt 1: abort/claimed_result_not_observed; attempt 2: pass"). Estimate: ~2 days at our recent pace. ## Full feature | Slice | What's in it | Est. | |---|---|---| | **MVP** | Loop control + prior-verification awareness + per-attempt artifacts + final summary trail | ~2 days | | **Multi-attempt history in PR description** | One-line ledger entry per attempt, survives rebases (same idea as #173) | +0.5 day | | **Budget caps** | Per-attempt + cumulative; abort if next attempt would exceed | +0.5 day | | **Diff-rejection guard** | Wrapper-level check that attempt N's diff is genuinely different from attempt N-1's; abort if identical (catches "agent gave up and re-emitted the same code") | +1 day | | **Test-plan revision escalation** | After M impl-only retries fail, allow ONE plan-revision attempt with the cumulative verification feedback; then up to M more impl retries against the new plan | +2 days | | **Polish** | Edge cases (verification phase itself fails for environmental reasons mid-loop, lease expires mid-retry, branch-protection interactions), Pester coverage of the loop logic | +1 day | | **Total full feature** | | **~7 days** | ## Cross-cutting - **Telemetry**: each attempt's full artifact set preserved separately so a reviewer can see the trail. The `Get-ExitCodeText` and `Get-ClaudeCostSummary` aggregations need to roll up across attempts. - **Cost reporting**: final summary line — "issue-agent spent $X.YZ across N attempts; budget remaining $A.BC." - **Failure cascade**: if all `max_attempts` attempts fail, the run aborts cleanly. Issue gets a comment with the trail. No PR opened. - **Interaction with #173 (PR-comment revise)**: a self-corrected PR that opens after attempt 3 should still be revisable via comment — the two loops are independent. The PR-comment loop's revision counter should NOT include the pre-PR self-correction attempts. ## Risks - **Cost runaway.** Each attempt is a full plan+impl+verify cycle (or impl+verify if test plan is fixed). Budget caps mitigate but don't eliminate — easy to set `max_attempts=5` and watch a single issue eat $50. The default cap should be conservative. - **Convergence on a wrong fix.** Without diff-rejection, an agent that has plausibly-correct code from the start might iterate to *worse* code chasing a verification artifact. The diff-rejection guard helps; a per-attempt "did the unit-test pass count regress?" check would help more. - **Verification phase itself is unreliable.** If verification flakily reads "88" sometimes and "8" other times due to OCR / timing / state-pollution issues, the loop runs N times unnecessarily and either burns budget or eventually catches a flaky pass and ships a buggy PR. Verification flake-detection is a separate problem this loop assumes is solved. - **Implementation "fix" that regresses other tests.** The agent fixes the failing test but breaks two others. Need to ensure the unit-test gate compares the FULL pass set, not just the previously-failing test. Existing 99/99 contract already does this. ## Decisions to make before starting - Default `max_implementation_attempts` (proposal: 2). - Default per-issue cumulative cost cap (proposal: $25). - Whether to enable test-plan revision in MVP or defer (proposal: defer). - Where the prior-verification artifact lives in the implementation phase prompt (full inline vs. file-path-only-and-have-the-agent-read-it). - Diff-rejection: include in MVP or defer (proposal: defer to "polish"). - Whether to expose `max_implementation_attempts` as a per-issue label (e.g. `agent-max-attempts:3`) or only as a workflow-default input.
nelsong6 commented 2026-07-05 15:45:21 -07:00 (Migrated from github.com)

Closing during repository issue cleanup. If this becomes active work again, we can reopen or recreate it as a fresh, specific task.

Closing during repository issue cleanup. If this becomes active work again, we can reopen or recreate it as a fresh, specific task.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
romaine-life/spirelens#174
No description provided.