fix: release watched_repos on offboard so re-hire isn't blocked#32
Merged
Conversation
Closes #31. Offboarding (DELETE /agents/{id}) sets status='stopped' but doesn't delete the DB row, so the cascade on watched_repos never fired — and the cross-agent uniqueness from #28 then blocked a new agent under the same user from re-subscribing to the same repo. Stale watcher row tied to a stopped agent permanently occupied the (user, owner, repo) slot. Targeted cleanup in Orchestrator.stop_agent via a new WatchedRepoModel.delete_by_agent. agent_memory, agent_action_log, and reviewed_prs are deliberately left alone — they're the audit trail of what the agent actually did, and offboard means "stop working," not "erase ever existed." Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
michaelzwang13
commented
May 24, 2026
Owner
Author
michaelzwang13
left a comment
There was a problem hiding this comment.
LGTM ✅
Review Summary:
This is a clean, well-documented fix for the offboarding issue (#31).
What I like:
- Correct root cause fix: Releasing
watched_reposbefore the agent status update allows the cascade to work properly - Audit trail preserved: Keeping
agent_memory,agent_action_log, andreviewed_prsis the right call for compliance - Good test coverage: The new test verifies
delete_by_agentis called with the correct agent_id - Clear documentation: Comments reference #31 and #28, explaining the "why"
- Minimal/surgical change: Only 51 lines added, no schema migration needed
The fix correctly balances cleanup (releasing repo subscriptions) with audit preservation (keeping action logs). Code looks ready to merge — would APPROVE if GitHub allowed self-approvals.
This was referenced May 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #31.
What was broken
Offboarding (`DELETE /agents/{id}`) calls `Orchestrator.stop_agent`, which only sets `status='stopped'` and clears the container + token — the DB row stays, so the `on delete cascade` on `watched_repos` never fires.
Combined with #28's cross-agent uniqueness (`unique(user_id, owner, repo)`), a fired employee's stale `watched_repos` row permanently occupied the `(user, owner, repo)` slot. Re-hiring under the same user and re-subscribing to the same repo returned a 409 against a row tied to a stopped agent.
Why #28 is still correct
Two different users can still watch the same repo — the constraint is per `user_id`, not global. So a senior engineer and another senior engineer (different accounts) can both auto-review a junior's PR. Only intra-user duplicates are blocked. The fix is purely about the stale-row-after-stop case.
Fix
`Orchestrator.stop_agent` now calls `WatchedRepoModel.delete_by_agent(agent_id)` before the AgentModel.update that flips the status to `stopped`. Three observations:
Test
One new test in `TestStopAgent` (`test_stop_agent_releases_watched_repos`) patches `WatchedRepoModel.delete_by_agent` and asserts it was called with the right agent_id. Full suite: 142 → 143 passing.
Test plan
🤖 Generated with Claude Code