Skip to content

fix: keep a repo pod busy and a PR run unflagged while its agent still runs - #833

Open
hfreire wants to merge 5 commits into
jonwiggins:mainfrom
hfreire:fix/repo-pod-idle-kills-running-agent
Open

hfreire wants to merge 5 commits into
jonwiggins:mainfrom
hfreire:fix/repo-pod-idle-kills-running-agent

Conversation

@hfreire

@hfreire hfreire commented Nov 19, 2026 •

Copy link
Copy Markdown

Summary

A conflict-resolve run on an existing PR got flagged needs_attention for the very conflicts it was started to fix, two seconds after it started. Ten minutes later the repo pod was reaped with the agent still working in it. I saw this on a real run: provisioning to running to pr_opened to needs_attention within 2s, agent logging until 14:10, pod killed around 14:11.

Two causes:

  • The run already has a PR url, so decideRunning promotes it to pr_opened immediately. decideFromPrStatus then sees mergeable === false and, with auto-resume off or exhausted, moves it to needs_attention. It never looks at whether an agent is still attached.
  • reconcileActiveTaskCounts only counts tasks in running/provisioning. Once the task was pr_opened/needs_attention the pod's active_count was reset to 0 while the agent was still streamed, and idlePods then picked it up 10 minutes after that reset.

Changes

  • reconcile-repo.ts: after the merged/closed checks, decideFromPrStatus returns a noop (agent_still_running) while an instance holds the run's attachment and the agent's last activity is within the stall threshold. The lease renews on a timer, so the activity bound is what stops a hung agent from holding the PR forever. Merged and closed PRs still resolve immediately. The wait returns before any status is written, so a conflict or CI failure still triggers its follow-up once the agent is done.
  • repo-pool-service.ts: reconcileActiveTaskCounts also counts tasks with a live attach_lease_until whose last_activity_at is within the stall threshold, whatever their state. A hung agent's pod still goes idle.
  • docs/reconciliation.md and CHANGELOG.md describe the new timing.

Known limits: an agent that keeps emitting events and never exits holds the follow-through for as long as it runs, and a single tool call longer than the stall threshold ends the wait early. Runs on a local machine are unchanged.

Testing

Unit tests for the reconciler (attached and active agent waits, attached but silent past the stall threshold or with no activity goes ahead, auto-merge and review launch wait too, merged PR still completes) and an integration test for the count (live lease keeps the pod busy, expired lease or silent agent frees it; leases are written from Postgres now() so host clock drift can't flip them). Both failed before the change.

  • pnpm check:push passes (every check CI runs, sized to the change; pnpm lint and pnpm knip are the quick local checks)

Locally, lint, typecheck, knip, format and the full @optio/api and @optio/shared unit suites pass, plus the repo-pool integration test. A full check:push on my Mac fails on time-sensitive tests (webhook delivery, schedule triggers, usage ledger) that also fail on a clean main there: the Mac clock drifts tens of ms behind the podman VM's, and trigger-delivery compares Postgres now() against Date.now(). They pass on main once the clocks agree, so I'm leaving the box unticked and relying on CI here.

Related

None.

@jonwiggins

Copy link
Copy Markdown
Owner

Thanks for this, and for the careful write-up. The diagnosis checks out against the code: a run started on an already-open PR is promoted to pr_opened on its first pass, decideFromPrStatus never looks at the attachment, and reconcileActiveTaskCounts stops counting the run, so the pod reads idle. Both changes do what they say. This is not a blocking review: the change alters when PR follow-through fires for every Repo Task whose agent is still attached after its PR exists, so whether this is the right place to fix it is the maintainer's call. One thing I'd like addressed whichever way that goes.

To address

  1. packages/shared/src/reconcile/reconcile-repo.ts:454 — the wait has no upper bound. The hold renews its lease on a timer (run-attachment.ts, every ATTACH_RENEW_MS), not on agent output, and stall detection only runs in decideRunning (computeHeartbeat reports nothing unless the state is running). So an agent that opens its PR and then never exits (hung process, a gh pr checks --watch that never returns) now holds off review launch, auto-merge, auto-resume and the needs_attention flag for as long as the API instance is up, where before those went ahead. With the repo-pool-service.ts change the same run also keeps its pod slot and keeps the pod from being reaped, which used to be the accidental backstop. Something should end the wait: for example only deferring while the run shows recent activity (lastActivityAt within the stall threshold), or applying the stall check to an attached pr_opened run. A reconciler unit test for "attached but silent past the threshold" would pin it.

Non-blocking

  • The CI/"failing" edge is not preserved the way the description says: the PR watcher writes prChecksStatus to the row on every poll while the run is pr_opened, so by the time the agent exits prev.checks may already be failing. That is how it behaved before this PR too; the conflicts edge does survive (the watcher never writes conflicts itself). Worth rewording, not fixing here.
  • repo-pool-service.int.test.ts builds its leases from the API host's clock and the query compares them with Postgres now(). It failed three times in a row for me ("frees a pod…": expected 0, got 1) and then passed 11 times; I could not reproduce or explain it, so treat this as a question. Writing the lease in SQL (now() ± interval) would take the two clocks out of it, the same drift you describe on your Mac.
  • An attached pr_opened run whose API instance goes away is not re-attached (claimAttachment and the attach sweep only take provisioning / running), so after the lease lapses the flag and the pod count behave as before this PR. Pre-existing; mentioning it because it is the same root: the run leaves running while its agent works.

Verified: read the diff with its callers (snapshot builder, run attachment, task worker settle path, PR watcher, pod pool). The reconciler unit test fails with reconcile-repo.ts reverted to the base and passes on the PR; the integration test's "keeps a pod busy" case fails with repo-pool-service.ts reverted and passes on the PR. CI on this commit: every suite green; Checks is red only on the dependency audit (three Next.js advisories fixed on main by #834 after this branch was cut), so a rebase onto main clears it.

Reviewed by Claude on the maintainer's behalf.

@jonwiggins jonwiggins added the maintainer-decision Product or UX call: left for the maintainer label Nov 21, 2026
@hfreire
hfreire force-pushed the fix/repo-pod-idle-kills-running-agent branch from 4712163 to b985252 Compare November 23, 2026 10:52
@hfreire

hfreire commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Thanks for the thorough review.

  • Unbounded wait: fixed in both places. The reconciler (b985252) and the pod count (ba1b6c1) now also require the agent's last activity to be within the stall threshold. So an attached agent that goes silent, or never reported activity, no longer holds back review, auto-merge, auto-resume or the flag, and its pod can go idle and be reaped. There are reconciler tests for both cases and an integration test for the silent-pod case.
  • CI edge: you're right, the watcher overwrites prChecksStatus. I reworded the description; only the conflicts edge is preserved.
  • Integration test: leases are now written as now() ± interval in SQL. My Mac drifts tens of ms from the podman VM and it still passes, which fits your 3-fail/11-pass run.
  • Rebased onto main for fix(deps): update Next.js to 15.5.29 for three security advisories #834's audit fix.

@hfreire

hfreire commented Nov 25, 2026

Copy link
Copy Markdown
Author

Friendly ping: the review points are addressed in b985252 and ba1b6c1. CI needs a maintainer approval to run on the new commits when you get a chance.

…l runs

A run started on an already-open PR is promoted to pr_opened straight away. Two things then misread it: the reconciler flagged the PR's conflicts/CI as needs_attention while the agent was still fixing them, and the active-count reconcile only counted running/provisioning tasks, so the pod read idle and the reaper killed it mid-run.
The attach lease renews on a timer, so a hung agent kept its PR run out of
review, auto-merge, auto-resume and needs_attention indefinitely. Only wait
while its last activity is within the stall threshold. The repo-pool
integration test now builds leases from Postgres now(), not the host clock.
The lease renews on a timer, so counting any live lease kept a hung
pr_opened agent's pod busy and unreapable. Count it only while its last
activity is within the stall threshold.
@hfreire
hfreire force-pushed the fix/repo-pod-idle-kills-running-agent branch from ba1b6c1 to b6e569f Compare December 1, 2026 09:15
@jonwiggins

Copy link
Copy Markdown
Owner

Re-review at b6e569fb. The one request from last time is met and I found no new bug. Verdict stays maintainer: on the code this can merge; what is left is whether PR follow-through should wait for the agent.

Earlier requests

  1. The wait has no upper bound — met. reconcile-repo.ts:472-481 now also requires lastActivityAt to be set and newer than the stall threshold, and repo-pool-service.ts:1503-1510 applies the same condition to the pod count. The bound is real: computeHeartbeat passes lastActivityAt through in every state and updateTaskActivity has no state filter. The "attached but silent" and "no recorded activity" reconciler tests are there.
  2. CI-edge wording — met, and now stale the other way: since fix(api): resume a PR whose CI fails when the PR watcher is the one to notice #843 the PR watcher no longer writes prChecksStatus, so the description's "the CI edge can be lost" is no longer true on this base. The hold returns before any status patch, so every edge survives it.
  3. Integration test clocks — met (leases and activity written as now() ± interval in SQL).

What changes for ordinary runs (the decision)

  • A run is promoted to pr_opened as soon as its PR url is on the row. On main, review launch, auto-merge, auto-resume and the needs_attention flag can fire while the agent is still wrapping up; here they wait until the agent exits (plus up to one watcher poll, 30 s) or goes quiet for OPTIO_STALL_THRESHOLD_MS.
  • An agent that keeps emitting events and never exits holds them off with no bound: stall detection doesn't run in pr_opened and there is no run-time cap. A human merge, close or cancel still gets through.
  • A single tool call longer than the threshold (a long test run, gh pr checks --watch) ends the hold mid-work, so the original symptom can still occur then.
  • Local runs (run_target = local) are untouched by both changes, so a conflict-resolve run on a machine is still flagged at once.

Non-blocking

  • No CHANGELOG.md entry for a user-visible timing change, and docs/reconciliation.md (the pr_opened and attachment sections) isn't updated. Please add both.
  • The first sentence of reconcileActiveTaskCounts's docstring ("actual running/provisioning tasks") and the description's CI-edge paragraph are stale.
  • repo-pool-service.test.ts asserts the bound as 300_000, so it breaks where OPTIO_STALL_THRESHOLD_MS is set in the environment.
  • Untested: the hold with autoMerge or reviewEnabled on.

Verified: read the three commits with the reconciler's state dispatch, the snapshot's heartbeat and execution fields, transitionTask / updateTaskActivity, the activity flusher, the task worker's stream section, the PR watcher loop and the pod pool's slots and idle reaper. The diff touches two source files and three test files, nothing in .github/, scripts, manifests or test setup, so I've approved the CI run for this head; nothing has executed the new tests yet, and the result decides whether this is ready.

Reviewed by Claude on the maintainer's behalf.

Also cover the wait with auto-merge and review launch on, and stop the pod
count test depending on OPTIO_STALL_THRESHOLD_MS being unset.
@hfreire

hfreire commented Dec 3, 2026

Copy link
Copy Markdown
Author

Thanks, the non-blocking points are done in 96625f6: changelog entry, docs/reconciliation.md, the docstring, a pod count test that no longer depends on the environment, and tests for the wait with auto-merge and review launch on. I also fixed the CI-edge paragraph in the description and listed the two limits you found there.

I merged main in rather than rebasing, so the commits you reviewed keep their hashes. The CI run you approved for b6e569f never started any jobs, so the new head probably needs approving again.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer-decision Product or UX call: left for the maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants