Skip to content

fix: Add completion detection to stop agent when all features pass - #17

Closed
Bortus-AI wants to merge 1 commit into
AutoForgeAI:masterfrom
Bortus-AI:fix/agent-completion-detection
Closed

Bortus-AI wants to merge 1 commit into
AutoForgeAI:masterfrom
Bortus-AI:fix/agent-completion-detection

Conversation

@Bortus-AI

@Bortus-AI Bortus-AI commented Jan 8, 2026 •

Copy link
Copy Markdown

Summary

  • Add is_project_complete() helper function to detect when all features are passing
  • Add completion check in agent loop to stop gracefully when project is done
  • Update coding prompt to instruct agent what to do when all features are complete

Problem

Previously, the agent loop would continue indefinitely even after all features were passing, resulting in:

  • Wasted API tokens running unnecessary sessions
  • Potential for the agent to run endless regression tests without stopping
  • No clear signal to the orchestrator that work was complete

Solution

Three-part fix:

  1. progress.py: Added is_project_complete() function that returns True when:

    • Total features > 0
    • All features are passing (passing == total)
    • No features are in-progress
  2. agent.py: Added completion check in the main loop (after status == "continue") that:

    • Calls is_project_complete() before auto-continuing
    • Prints a clear "ALL FEATURES COMPLETE!" message
    • Breaks out of the loop to stop the agent
  3. coding_prompt.template.md: Added explicit instructions explaining what the agent should do when feature_get_next returns {"error": "All features are passing! No more work to do."} - run final regression tests, commit, update progress notes, and end the session.

Test plan

  • Run agent on a project with all features already passing - should stop immediately
  • Run agent on a project with pending features - should continue working as before
  • Verify completion message is displayed when agent detects all features complete

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Automatic completion detection: the system now identifies when all features and tests pass and the project is complete.
    • Graceful session termination: upon completion, confirmation messages are displayed and the session automatically ends, eliminating unnecessary iterations.

✏️ Tip: You can customize this high-level summary in your review settings.

Previously, the agent loop would continue indefinitely even after all
features were passing, wasting tokens and resources.

Changes:
- Add is_project_complete() helper function to progress.py that checks
  if all features are passing and none are in progress
- Add completion check in agent.py's main loop to break out when the
  project is complete, with a clear completion message
- Update coding_prompt.template.md to instruct the agent what to do
  when feature_get_next returns "All features are passing"

This ensures the agent stops gracefully when the project is complete
rather than continuing to run unnecessary sessions.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jan 8, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR introduces a project completion detection mechanism. It adds a new is_project_complete() function to check if all tests pass with no in-progress features, integrates this check into the agent loop to exit when complete, and updates the prompt template to include completion guidance when the feature retrieval indicates no remaining work.

Changes

Cohort / File(s) Summary
Completion Detection Logic
progress.py
Added public function is_project_complete(project_dir) that returns True when total tests exist, all pass, and none are in-progress; otherwise False. Leverages existing count_passing_tests() helper.
Agent Loop Control
agent.py
Imported is_project_complete and added conditional check after each session to exit the agent loop early when project completion is detected, preventing unnecessary iterations.
Prompt Template
.claude/templates/coding_prompt.template.md
Added guidance block for handling feature retrieval completion message, outlining final steps (congratulations, regression tests, final commit, progress update) and indicating orchestrator session termination.

Sequence Diagram

sequenceDiagram
    participant Agent as Agent Loop
    participant Session as Session Handler
    participant Complete as is_project_complete()
    participant Tests as Test System

    Agent->>Session: Execute agent session
    Session-->>Agent: Return status "continue"
    Agent->>Complete: Check is_project_complete(project_dir)
    Complete->>Tests: count_passing_tests(project_dir)
    Tests-->>Complete: (passing, in_progress, total)
    Complete-->>Agent: True if all pass & none in-progress
    
    alt Project Complete
        Agent->>Agent: Print completion messages
        Agent->>Agent: Break agent loop
    else Project Incomplete
        Agent->>Agent: Continue to next iteration
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Poem

🐰 A hop, skip, and test complete,
When features pass from head to feet,
The agent knows: no more to do,
So off it hops, its work through and through! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding completion detection logic to terminate the agent when all features pass, which directly aligns with the modifications across all three files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 122f03d and 28b3caf.

📒 Files selected for processing (3)
  • .claude/templates/coding_prompt.template.md
  • agent.py
  • progress.py
🧰 Additional context used
🧬 Code graph analysis (1)
agent.py (1)
progress.py (1)
  • is_project_complete (92-108)
🔇 Additional comments (4)
agent.py (2)

23-23: LGTM! Clean import of the new completion check function.

The import is correctly added and maintains alphabetical ordering.


198-206: Excellent completion detection logic!

The placement is optimal—checking after a successful session but before the auto-continue delay prevents unnecessary iterations and token usage. The completion messages are clear and informative.

The logic correctly handles edge cases:

  • Won't trigger if features are in-progress
  • Won't trigger on an empty project (total == 0)
  • Skipped when status is "error" (correct behavior)

This works well with the template guidance in the coding prompt, creating a defense-in-depth approach where both the agent (via feature_get_next) and the orchestrator can detect completion.

progress.py (1)

92-108: Excellent implementation of the completion check!

The logic is clear, concise, and correctly handles all edge cases:

  • Returns False for an empty project (total == 0)
  • Returns False if any features are still in-progress (prevents race conditions)
  • Returns False if not all features are passing
  • Safely returns False on database errors (via count_passing_tests error handling)

The three-condition check provides robust defense against premature completion detection.

.claude/templates/coding_prompt.template.md (1)

104-117: Clear and well-aligned completion guidance!

This guidance complements the completion detection in agent.py, creating a robust two-layer approach:

  • Layer 1 (here): The agent detects completion when feature_get_next returns the "all features passing" error
  • Layer 2 (agent.py): The orchestrator checks is_project_complete() after each session

This defense-in-depth design ensures the agent stops reliably, even if one layer doesn't trigger. The step-by-step instructions are clear and actionable, addressing the PR objective of preventing endless loops after completion.


Comment @coderabbitai help to get the list of available commands and usage tips.

@Bortus-AI

Copy link
Copy Markdown
Author

@leonvanzyl can we get this merged?

@Bortus-AI Bortus-AI closed this Jan 10, 2026
rudiheydra added a commit to rudiheydra/AutoBuildr that referenced this pull request Jan 27, 2026
The acceptance_results field was typed as list[dict[str, Any]] but the
database stores dict[str, Any] (Record/object format where keys are
validator types). This caused a Pydantic ValidationError when listing
agent runs via GET /api/agent-runs.

- Changed AgentRunResponse.acceptance_results type to dict[str, Any]
- Matches the actual data format stored in the database
- Aligns with frontend TypeScript types (Record<string, ...>)

Fixes regression in Feature AutoForgeAI#17: GET /api/agent-runs List Runs Endpoint

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant