Skip to content

[Phase 1] Improve agent execution resilience: file conflicts and verification recovery #341

Description

@frankbria

Problem

When executing tasks, the agent fails ungracefully in two common scenarios:

1. File Conflicts (file_create on existing files)

When a task is re-executed (e.g., after a previous failed run), the agent's plan still uses file_create for files that already exist from the previous attempt. This causes immediate step failures:

Step 1: src/task_tracker/exceptions.py → Failed: File already exists
Step 2: src/task_tracker/validators.py → Failed: File already exists

Expected behavior: The agent should detect existing files and fall back to file_edit or file_overwrite, or the planner should check for existing files before choosing the operation.

2. Cascading Verification Failures

After each file change, the agent runs incremental verification (ruff). When verification fails, the agent marks the step as failed and moves on, but downstream steps that depend on the failed file also fail:

Step 3: src/task_tracker/storage.py → stdout → Failed: Code verification failed
Step 4: src/task_tracker/commands/add.py → stdout → Failed: Code verification failed
Step 5: src/task_tracker/commands/delete.py → stdout → Failed: Code verification failed
... (all remaining steps fail)

This wastes significant time and API tokens — in the observed case, 893 seconds of execution with 20 steps all failing.

Expected behavior: The agent should either:

  • Fix verification errors inline before proceeding (self-correction loop exists but doesn't trigger for incremental verification)
  • Abort early after N consecutive failures instead of continuing through all 20 steps
  • Re-plan remaining steps accounting for the verification failures

Proposed Improvements

File Conflict Handling

  • In executor.py file_create: if file exists, fall back to overwrite (or file_edit if content differs)
  • In planner.py: scan workspace for existing files before generating the plan, prefer file_edit when target exists
  • Add a workspace cleanup command (cf work clean <task-id>) to remove artifacts from failed runs

Verification Recovery

  • Apply self-correction loop to incremental verification failures (currently only runs on final verification)
  • Add early abort threshold: if 3+ consecutive steps fail verification, pause and re-plan or escalate to blocker
  • Track cumulative failure cost (time + tokens) and abort when threshold exceeded

Observability

  • Log which verification gate failed and the specific error (currently just "Code verification failed")
  • Surface verification error details in SSE output events so the Execution Monitor shows what went wrong

Context

Observed during cf-test workspace execution where all 12 tasks failed. 10 tasks had file_create conflicts from previous runs, and verification failures cascaded through all remaining steps.

Related

  • Agent self-correction loop: core/agent.py _run_final_verification() / _attempt_verification_fix()
  • Executor file operations: core/executor.py
  • Verification gates: core/gates.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    phase-1Phase 1: CLI Foundation Completion

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions