Skip to content

PR detection misses sessions when agent creates PR from a branch different from stored session branch #297

@harshitsinghbhandari

Description

@harshitsinghbhandari

Problem

AO can miss a PR created by a managed session when the PR head branch does not match the session's stored branch.

Observed with session ao-agents-10:

  • Stored session branch in ~/.ao/data/ao.db: ao/ao-agents-10
  • Created PR: chore: empty randomass PR #296
  • PR head branch: chore/empty-randomass-20260617215922
  • Daemon log repeatedly reported:
scm observer: no PR detected for branch session=ao-agents-10 branch=ao/ao-agents-10 err="scm: not found: no open PR for branch ao/ao-agents-10"

The PR existed and was open, but the observer did not attribute it to the session because the head branch did not equal or descend from the stored session branch.

Manual recovery worked:

ao session claim-pr ao-agents-10 296 --json

That attached PR #296 to the session successfully.

Why this matters

Agents or humans working inside a managed session can create a new branch manually before opening a PR. In that case, AO's branch-prefix discovery misses the PR even though it was created from the session's worktree. The dashboard then shows no PR until someone manually claims it.

Current behavior

The SCM observer detects PRs by matching open PR source branches to each live session's stored branch:

  • exact match: sourceBranch == sessionBranch
  • stacked match: sourceBranch starts with sessionBranch + "/"

This is generally correct for AO-created branches, but fails when the live worktree branch has changed and the session row was not updated.

Expected behavior

AO should either:

  1. keep the session's stored branch synchronized when a managed session changes branches, or
  2. detect/claim PRs created from the session's current worktree branch even if it differs from the original stored branch, or
  3. surface an actionable warning/recovery path when a PR exists on the current worktree branch but cannot be attributed.

Possible fix directions

  • Ensure the git wrapper or hook path updates the Go daemon's durable sessions.branch, not just legacy metadata files.
  • During SCM observation, optionally compare the current worktree branch for live sessions against the stored branch and refresh it when safe.
  • Add a CLI/UI hint when GET /sessions/{id}/pr is empty but the current checked-out branch has an open PR.
  • Consider having gh pr create wrapper call the daemon claim-pr endpoint directly when AO_SESSION_ID is available.

Supporting evidence

sessions row showed:

ao-agents-10 | branch=ao/ao-agents-10 | workspace_path=~/.ao/data/worktrees/ao-agents/ao-agents-10

PR #296 showed:

{
  "number": 296,
  "state": "OPEN",
  "headRefName": "chore/empty-randomass-20260617215922",
  "baseRefName": "main",
  "isDraft": false
}

Manual claim response:

{
  "ok": true,
  "sessionId": "ao-agents-10",
  "prs": [
    {
      "url": "https://github.com/aoagents/ReverbCode/pull/296",
      "number": 296,
      "state": "open",
      "ci": "passing",
      "review": "none",
      "mergeability": "mergeable",
      "reviewComments": false
    }
  ],
  "branchChanged": false,
  "takenOverFrom": []
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions