Skip to content

Code agent should detect existing PRs for an issue and exit early #844

@fullsend-ai-retro

Description

@fullsend-ai-retro

What happened

For issue #834, the code agent was dispatched at 17:21 UTC. Meanwhile, the human (ralphbean) authored the fix using Claude Sonnet 4.5 and opened PR #835 at 17:26 UTC — roughly 5 minutes after dispatch. The code agent ran to completion (producing a 78KB artifact) but correctly did not create a competing PR.

While the agent avoided the worst outcome (a competing PR), it still consumed compute and tokens for work that was unnecessary. For a simple one-line fix, this waste is minimal, but for complex issues the cost could be significant.

What could go better

The code agent could check at startup whether a PR already exists for the issue it's working on (by searching for PRs that reference the issue number or checking for branches matching the agent/{issue}-* pattern or human PRs linked to the issue). If a PR is found, the agent could exit early with a log message.

Confidence: Medium. This optimization has a clear benefit (token savings) but the implementation needs care: (a) the human's PR might not exist yet when the agent starts but could appear mid-run, (b) the existing PR might be a draft or WIP that the agent should still work on, (c) race conditions between dispatch and PR creation. The code agent already handled this case gracefully by not creating a competing PR, so the current behavior is acceptable — this is an optimization, not a bug fix.

Proposed change

Add an early-exit check to the code agent's startup sequence (likely in the agent definition or harness pre-flight):

  1. Before starting work, query gh pr list --search "issue:{ISSUE_NUMBER}" --repo {REPO} or check for open PRs whose body references the issue.
  2. If a non-draft PR exists that addresses the issue, log a message and exit with success (no artifact, no PR).
  3. If a draft PR exists, proceed with work (the draft may be abandoned).

This check should be in the code agent's harness configuration in .fullsend, not in the platform code, since different orgs may want different behavior.

Validation criteria

When a human opens a PR for an issue before the code agent starts its main work loop, the code agent exits within 30 seconds instead of running to completion. Validate on the next 3 instances where a human beats the code agent to a fix.


Generated by retro agent from #835

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions