Auto-archive on PR close archives long-lived "hub" sessions prematurely — scope it to PR ownership
Summary
The "Auto-archive on PR close" setting archives sessions that aren't finished units of work — specifically long-lived sessions whose role is to merge other sessions' PRs. When one of those other PRs closes, the still-active session is archived out from under the user.
Repro
- Enable Settings → Auto-archive on PR close.
- Keep one long-lived session open as a "hub" — you use it to review and merge other sessions' PRs (e.g. you run
gh pr merge <N>there, or use a tool/skill that merges another session's PR). - Have a second, separate session that owns a feature branch + PR. Merge that PR from the hub session.
- Observed: the hub session gets archived when the other session's PR closes — even though it has unfinished work and never owned that PR.
- Expected: only the session that owns the merged PR (the one whose worktree branch is that PR's head branch) should auto-archive.
Suspected root cause
The trigger appears keyed on association ("a PR this session interacted with closed") rather than ownership ("this session's own branch PR merged").
Proposed fix — ownership-scoped trigger
Auto-archive a session only when ALL of these hold:
- the session's working directory is a dedicated git worktree (not the main checkout / repo root), and
- the merged PR's head branch == that worktree's checked-out branch (the session owns the PR; it didn't merely run the merge on someone else's), and
- the session has no other open PR whose head branch it owns.
Sessions running from the repo root (which own no feature branch) would then never auto-archive — the desired behavior for hub / coordinator / inbox sessions.
Secondary request (optional)
Scheduled / recurring agents spawn one retained session per run; these pile up with no owning PR, so the ownership rule above won't reach them. A "keep the latest N runs per scheduled task, archive older" retention option would clear them separately.
Note for any programmatic workaround
The archive_session MCP tool is unavailable in unsupervised / headless mode, so this can't be worked around by a scheduled agent — the fix needs to live in the in-app auto-archive feature itself.