Handing a Half-Finished Job From My Session to a Headless Agent

An interactive session and a scheduled agent share one subscription and one repo. When the session hits its limit mid-ship, a queued resume job must start exactly once, only after the session stops, and know what was done. The mechanism is a lease, a ledger, and a prompt that re-enqueues itself.

I run an autonomous loop where scheduled agent sessions build and operate small SaaS products. The loop runs headless, on a scheduler that starts runs only when the shared Claude subscription has headroom. But not all work is headless. Sometimes I drive an interactive Claude session myself, in the same repo, on the same subscription. And that combination produces an awkward class of failure.

Here is the shape of it. I am mid-ship in an interactive session: streams delegated, code integrating, deploy next. The session limit hits. The work is half done and the state of it lives in my session's context, which is now a pumpkin. The loop's scheduler will happily start a run the next time it sees headroom. If nobody tells it about the half-finished ship, one of two bad things happens: the run starts while my session's subagents are still writing files, and they stomp on each other; or it never starts at all, because nothing in the queue says there is work.

The fix took two commits and three mechanisms: a lease so the queued job waits, a ledger so the work outlives the session that did it, and a resume prompt that keeps itself alive. Commits 3055fd1 and 7bb62c8, both 2026-10-07.

The lease: wait until the session is really done

A queued job can opt into a lease: lease: { name, idle_minutes }. The scheduler holds the job while either of two conditions says the interactive session is still working.

First condition: a lease file exists and is unexpired. The holder (my session, or anything driving work) renews it as it goes with one command, node loop/lease.mjs hold <name> [minutes], capped at 720 minutes so a forgotten lease ages out within half a day. This is the explicit channel: the session says "I am alive".

Second condition is the interesting one, because the explicit channel fails exactly when you need it most. When the session limit hits, the session dies without releasing anything. So the scheduler also reads the agent ledger (more on that below) and holds the job while any claude-owned stream is in state "running" and wrote an event within the idle window (default 30 minutes; subagents checkpoint every 20). A session that died at its limit leaves a trail of ledger events, and the job waits until that trail goes quiet.

One asymmetry was a deliberate call: Codex and pi streams never hold a lease. The lease exists to prevent Claude-on-Claude collisions on the shared subscription. Codex and pi run on their own budgets and survive a Claude session limit, so their being "active" is not a reason to hold the job. Encoding "which workers does a limit actually kill" into the hold condition was the part I got wrong in my head before I wrote it down.

The lease fails open: an unreadable lease file or an unreadable ledger never holds a job. That is the right default for a scheduler, because the scheduler already re-reads everything on its next tick 20 minutes later. Failing closed would turn a transient read error into a silently stalled queue, and a stalled queue with no error is worse than a premature start that the run lock and ledger make recoverable.

The ledger: the work outlives the session

The ledger is an append-only JSONL file, one line per event, and it is the shared memory for every delegated stream: Claude subagents, Codex exec, pi. Five event kinds: start, note, done, fail, validated. A stream that has started but has no closing event older than 30 minutes is "stale". Two rules make it more than a log.

"Done" is not "accepted". A stream records done when it thinks it finished. The stream stays in state UNVALIDATED until the orchestrator separately records validated with a pass or fail. The distinction exists because the thing that writes code is not the thing that checks it, and a ledger that conflates "claimed done" with "verified done" would let a half-checked ship look finished to the next run that reads it.

The delegate records its own outcome. When work goes to Codex through the delegate wrapper, the wrapper itself writes the final event to the ledger, not the orchestrator. This came out of a specific failure mode: the orchestrator is itself a model session that can die at its own session limit, mid-delegation. If the orchestrator is the only writer, its death erases the outcome of the work it delegated. Commit 3055fd1's message is exactly this: a session limit or crash in the orchestrator loses nothing, because the wrapper records the outcome itself.

A status command folds the events into one row per stream: state, age, whether it needs triage, validation, or a fix. On any restart, the first thing the loop does is read that table. "What was in flight when everything stopped" stops being a question about anyone's memory.

The resume prompt: a job that keeps itself alive

The third mechanism is the queue job itself. When the session limit killed my interactive ship, I queued a resume job with a lease on the stream name, and its prompt was written to survive: validate the ledger streams, integrate what they produced, review it, deploy, and if the product is not live, re-enqueue itself and wait for the next tick with headroom.

The re-enqueue is the piece that makes the whole thing robust to the subscription's rhythms. A resume job that dies with its run dies at exactly the moment the limit hits again. A resume job that re-enqueues itself treats the queue, not a single run, as the unit of persistence. Each run picks up where the ledger says things stand, does a bounded amount of work, records its outcome, and either finishes or leaves a fresh, accurate trail for the next run.

The sequence, end to end:

WhenWhat happensWho knows
Interactive session, mid-shipstreams delegated, ledger events written every checkpointledger
Session limit hitssession dies, no release, no cleanupnobody, yet
Scheduler tick, no headroomjob with lease is held (ledger trail fresh)scheduler log
Headroom after resetledger trail older than idle window, lease quiet: job eligible, run startsscheduler log
Run reads ledgersees each stream's state: done, stale, failed, unvalidatedthe run
Run validates, integrates, deploys, re-enqueues until liveevery outcome recorded by the workers themselvesledger

Why two channels instead of one

The obvious first design is a single file: a "current work" pointer the session writes and the scheduler reads. It fails for the same reason a newest-wins reader fails: it assumes the producer and the consumer die in a fixed order. Sessions die without writing. The two-channel design (explicit renewal plus a derived heartbeat from the ledger) exists because each channel covers the other's death mode. The lease file covers "session alive but quiet"; the ledger covers "session died without saying so".

If you are building coordination between an interactive session and a scheduler, the checklist I would hand you now: make the hold condition derivable from artifacts the worker already writes, never from its cooperation; make delegates write their own outcomes; distinguish claimed-done from verified-done; fail the hold open and the budget closed; and write the resume prompt as a loop over the ledger, not a continuation of a dead context. The limit will hit at the worst moment. Design for that moment, not for the polite ones.