Skip to content

fix(runner): reap orphaned episode runs and end an orphaned run's instance - #950

Open
andrewpark-scaleai wants to merge 3 commits into
mainfrom
andrew-park/reap-orphaned-runs
Open

andrewpark-scaleai wants to merge 3 commits into
mainfrom
andrew-park/reap-orphaned-runs

Conversation

@andrewpark-scaleai

@andrewpark-scaleai andrewpark-scaleai commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

A run whose process died could stay in flight forever. The local runner's reconciler only reaped runs recorded as local, so an episode whose trainer died stayed RUNNING. When it did reap a run, it failed only the run record and left the task instance running. The explorer then never reported the instance done, and teardown refused it without --force.

What changed

  • The reconciler covers local and episode runs. For each dead run it ends the instance first, then the run. The instance write is a compare-and-swap that only touches a running instance. It holds back while any other run of that instance is alive, or while the target run has renewed its lease since the reconciler listed it.
  • A gone pid on the reconciler's own host counts only once the run's lease has also passed. This stops processes in other pid namespaces that report the same host name from all looking dead at once.
  • A live episode marks its run seen every OWNER_SEEN_SECONDS, through teardown.
  • A TLA+ model of owners, runs, instances, the reconciler, and lease stalls lives in tst/formal/run_orphan_reaping/, with AGENTS.md and a check.sh that pins TLC and checks 12 configs against their expected results.

Scope

  • Same-host orphans are now reaped up to about one lease (two minutes) later than before.
  • Left out, and listed in AGENTS.md: a lease renewal in the one round trip between the re-check and the write, and a process that stalls past its lease before it registers its instance.

Blast Radius

Only the reconciler and the episode loop change. A live run is never failed: the write re-checks liveness inside the compare-and-swap, and real end writes (record_task_failure, record_task_cancelled) still overwrite the reconciler's failed.

Verification

  • New tests in tst/unit/runner/local_runner_test.py and tst/unit/task/episode/run_test.py fail on main (for example 'running' == 'failed' for the instance, and the episode run left RUNNING) and pass here. tst/unit/runner and tst/unit/task/episode give 163 passed, and make lint passes.
  • bash tst/formal/run_orphan_reaping/check.sh exits 0. Every config gives its expected result. The fixed configs hold every rule (for example 222,709 states with stalls), and each config that removes one guard breaks the rule that guard protects.

Confidence Score: 5/5 · The PR appears safe to merge within its documented lease limits.RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge within its documented lease limits.

Summary

The PR ends orphaned local and episode runs and fails their running instances first.

  • The runner ends orphaned runs and their task instances together.
  • Contributors can check orphan cleanup across modeled run scenarios.

Review was based on source inspection; tests and TLC were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[List active local and episode runs] --> B{Owner alive?}
  B -->|Yes| C[Leave unchanged]
  B -->|No| D[Read current runs sharing the instance]
  D --> E{Run ended or a live run holds it?}
  E -->|Yes| C
  E -->|No| F[Fail instance only if still running]
  F --> G[Fail orphaned run]
Loading

Reviews (2) · Last reviewed commit: "chore: add the run-orphan-reaping TLA+ m..." · Reviewed by Greptile

@andrewpark-scaleai
andrewpark-scaleai requested a review from a team as a code owner November 29, 2026 05:43
Comment thread src/agent_env/runner/store.py Resolved
Comment thread tst/formal/run_orphan_reaping/check.sh Resolved
A started LocalRunner fails a run left in flight by a process that is gone, but only runs
recorded by the local runner, so an episode whose trainer died stays RUNNING; and it leaves
the run's task instance running, so the instance never reads as done. A resumed instance
must not be ended while the run resuming it is live, and must be once that run ends too.
A live episode must keep marking its run seen, as the lease on another host needs.

A run whose owner marks it seen after the reconciler listed it, and a run on this host name
whose pid is not visible but whose seen mark is fresh, must both be left running with their
instances. An instance that already ended is left alone when its dead run is failed.
A started LocalRunner failed a run left in flight by a process that is gone, but it queried only
its own runner's records, so an episode whose trainer died stayed RUNNING for good. It also failed
only the run record and left the run's task instance running, so the explorer's instance progress
never reported it done and teardown needed --force.

The reconciler now covers episode runs as well, which record their owning process the same way, and
ends the instance failed before it fails the run, in one compare-and-swap that writes only a running
instance. Each read inside it re-reads the run index: the write is cancelled when the run has ended,
when its owner marked it seen since the reconciler listed it, or when another live run (one that
resumed the instance) holds it. A run whose instance is held is left in flight for a later pass, so
the instance is still ended if the holder ends without touching it. A live episode now marks its run
seen as a LocalRunner does, so a reconciler on another host does not fail it once the lease runs out.

On the reconciler's host, a pid that looks gone now counts only once the owner's lease has lapsed
too. A host name can be shared across pid namespaces, as by containers on the host's network, so a
pid check alone would fail every live episode there in one pass.
RunOrphanReaping.tla models how a started LocalRunner fails runs whose process died, episodes included, and ends their task instances, against the processes running them, a run that resumes the instance, a process that stalls past its lease and comes back, and an episode that shares the reconciler's host name. Five configs check the code; seven more break each rule, before the fix or with one guard removed. check.sh runs every config against its expected outcome, and AGENTS.md gives the rules, the code each action covers, and how to read a failure.
@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrew-park/reap-orphaned-runs branch from f53ac0b to f16351a Compare November 29, 2026 06:07

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant