Repository navigation
fix(agent): keep sessions up while agents wait for background tasks - #91
Merged
kalaspuff merged 5 commits intoOct 18, 2026
Merged
Conversation
fctl-agent follows Claude Code's task_started and task_notification
events. While background tasks run after a turn, the session waits for
them ("Waiting for 2 background tasks") and the idle clock starts when
the last one ends. Background work keeps a session up for at most an
hour after its last turn, or the idle timeout when that is longer; then
the session stops and names the tasks it stopped.
Turns Claude starts on its own count as turns: fctl-agent sets
CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS and follows session_state_changed,
or the first message of a turn when Claude Code doesn't report them. A
stop interrupts them, and so does a usage-limit pause.
When a session stops with background tasks running, the transcript
names them, and the next message tells Claude they didn't finish. The
system prompt says how long background work may run after a turn.
Refs #87
`background <seconds>` starts a shell command in the background that ends after that many seconds, and fake-claude takes its result in a turn of its own, as Claude Code does. With CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS set it reports each turn starting and ending (session_state_changed). Refs #87
The architecture's idle sessions, getting started and the project's idle timeout setting say that background work keeps a session up for up to an hour after the turn, and ADR 0018 records why. The session page and the home page show why a waiting session waits when it waits for background tasks. Refs #87
…ng for you The floor's headline counts a session whose agent waits for its background tasks as working, not as waiting for you. The status helpers move to web/src/lib/status.ts. Refs #87
The smoke session also starts a command in the background: the session must wait for it, and the agent must take its result in a turn of its own before the session goes back to waiting for a message. Refs #87
kalaspuff
deleted the
fctl/44-please-fix-https-github-com-hyperstructu
branch
October 18, 2026 19:50
kalaspuff
pushed a commit
that referenced
this pull request
Oct 18, 2026
A stopped session keeps its sandbox for `keep_stopped_hours` (72 by default), and hosts removed sandboxes by age only. With Apple's `container`, a stopped sandbox holds the most its session ever stored, since freed space never goes back to the Mac. So a busy host could fill its disk with sandboxes nobody resumes, and nothing said so before sessions failed (#76). Now a host keeps some disk free for its sandboxes, makes room when it runs low, and says so when it can't. ## What changed **Hosts keep `min_free_disk_gb` free: 20 GiB by default, 0 for none.** - They measure where the runtime keeps the sandboxes' files: - Apple's `container`: its app root, from `container system status --format json` (`paths.appRoot` from 1.4, `appRoot` before), or else `$CONTAINER_APP_ROOT` or `~/Library/Application Support/com.apple.container`. - Lima: `$LIMA_HOME` or `~/.lima`. - Docker: its root directory when that is on this machine. On macOS it's inside the VM, so the host runs `df` in a short-lived container of its own image (`--pull never --network none`). - The process driver: the data directory. - Measuring runs in the background every 30 seconds (in Docker's VM every five minutes), so the control connection never waits on it. Heartbeats report this figure instead of the data directory's free space. - Below the floor, the sweep removes stopped sandboxes before `keep_stopped_hours`, those stopped longest first. It removes one at a time and measures after each, so it stops once there is room. A session that starts in one meanwhile keeps it. The age-based sweep now removes one at a time too, for the same reason. - Meanwhile the host takes no new sessions, only resumes of the sessions whose sandboxes it has. It refuses the others with the reason: "studio-mac has 12.3 GiB of disk free, below the 20 GiB it keeps free (min_free_disk_gb in its host.toml)". - With nothing left to remove, it sweeps again only after a session stops here, rather than listing its sandboxes every 30 seconds. **Scheduling.** - The hello carries the floor (`HostHello.min_free_disk_mb`, optional), and FleetDO compares it with each heartbeat's free disk (`fctl_domain::disk::low`). - A runner below it is treated like a draining one for new sessions. Resumes of sessions whose sandboxes it has still go there; nothing else does. - A session with nowhere to go waits: "Waiting for a runner: the ones online are low on disk", or "…draining or low on disk". - Unlike a drain, idle sessions don't stop early, and the state ends by itself once there is room. FleetDO updates viewers when a runner goes low or recovers, and starts waiting sessions at the next heartbeat. **Where people see it.** - `fctl host status`: a `disk:` line, and `disk` in `--json`. - `fctl host doctor`: checks the disk. - `fctl admin runners`, the runners list (an amber "low on disk" state and line) and the home page ("1 runner online, 1 low on disk"), from the new optional `Runner.min_free_disk_mb`. **Docs.** `docs/cli.md` says what a host does when its disk runs low, and documents the new key. `docs/architecture.md`, `docs/security.md` and `docs/sandbox-images.md` are updated, ADR 0019 is new, and the roadmap item is ticked. `cargo xtask dev`'s host keeps 1 GiB free, so CI runners with less than 20 GiB free still pass the smoke test. ## Decisions to check #76 left these to a person. Here is how I settled them; ADR 0019 records them: - **The floor's default:** a fixed 20 GiB rather than a share of the disk, which would keep 200 GB free on a 2 TB disk. A host with a small disk, like a 64 GB Docker VM, lowers it. - **Removing resumable sandboxes early:** yes, those stopped longest first, and only below the floor. Running sessions are never stopped for disk. - **Not a drain:** a drain is a person's decision, kept with who started it. A low disk comes and goes on its own and must not end someone's drain, so it is a state of its own, derived from the floor and the free disk. - **`CAP_SYS_ADMIN` for Apple sandboxes, so that they can `fstrim`:** not done. Whether the Mac gets the space back needs checking on a Mac first, and the capability widens what root can do in the VM. #92 tracks it. **After upgrading, a host with less than 20 GiB free takes no new sessions** once it has removed its stopped sandboxes, until it has room or its `min_free_disk_gb` is lowered. The runners list and the waiting sessions say why. ## Compatibility Every new field is optional: - Older hosts send no floor. FleetDO schedules them as before, and they keep removing sandboxes by age only. - Older CLIs and web apps ignore `Runner.min_free_disk_mb`. - A new host under an older control plane refuses sessions that need a new sandbox while it is low. Those retry, then fail with its reason. ## Verification **Unit tests:** - `fctl-domain`: the low-disk rule and its wording, and `pick` with runners low on disk, alone or beside draining ones. - `fctl-host`: - the sweep's plan (oldest first) - reading `df`, including filesystem names with spaces - both shapes of Apple's status JSON - where Apple and Docker sandboxes are measured, with stand-in CLIs - the measurement in Docker's VM and its fallback image - the default floor - The daemon, driven through its chores with the process driver: - a low disk removes stopped sandboxes oldest first until there is room, and skips one a chore holds - with nothing left to remove, it refuses a new session and takes one whose sandbox is there - a session that stops during a sweep is removed by the next one (this test fails if the end of a sweep resets the flag) - expired sandboxes go whatever the disk has - Web: `lowOnDisk` and `diskBelow`. **Checks:** `cargo xtask check` and `cargo xtask dev --smoke` pass. **End to end**, with `cargo xtask dev --no-host`, fake-claude and a process-driver host of my own. This sandbox can't mount a small filesystem (no `CAP_SYS_ADMIN`). So I set the floor just above the real free space, and gave stopped sandboxes 2 GiB each with `fallocate`: - **Making room:** three stopped sandboxes A, B and C (oldest first), 3.9 GiB short of a 490 GiB floor. The host removed A (488.2 GiB free), then B (490.2 GiB), took sessions again, and kept C. - **Nothing left to remove:** a 6 GiB file made it short again. The host removed C, logged that nothing was left, and refused new sessions: - `fctl admin runners`, `fctl host status`, `fctl host doctor` and the runners list said why. - A new session D waited with "Waiting for a runner: the ones online are low on disk". - A message to A, whose sandbox was gone, went to its home runner and was refused: "Retrying: disk-host has 486.2 GiB of disk free, below the 490 GiB it keeps free (min_free_disk_gb in its host.toml)". Then it waited too. - The host didn't sweep again in the three minutes it waited. - **Recovery:** after I removed the file, the host logged "491.2 GiB of disk free again: taking new sessions". D and A started at the next heartbeat, A in a new sandbox. - **A resume while low:** with D running, I made the disk low again, stopped D and messaged it right away. It resumed in its own sandbox, with its conversation. **Not run:** the Apple, Docker and Lima drivers, since none runs here. Their measurement is covered by tests with stand-in CLIs, and the app-root detection follows Apple's sources at apple/container@31e3a98. ## Screenshots At c34c52c. Since then only the merge of `main` changed `web/` (#91: how idle sessions count and show on the home page), which these don't show. They show the runners list with a runner below its floor, in both themes and 1440 px wide. | Light | Dark | |---|---| |  |  | The same list at 390 px in the dark theme, and the home page in the light theme, which counts the runner as low on disk while two sessions wait for it: | Phone, dark | Home, light | |---|---| |  |  | Fixes #76 Session: https://app.fctl.sh/s/ses_01M585MBYAZGA7MFA49HPGMQAW --------- Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com>
kalaspuff
pushed a commit
that referenced
this pull request
Oct 18, 2026
In #46, runner `now` went quiet for half an hour while its agent kept working. After ten minutes the fleet marked the session stopped. And when a runner came back with an agent still at work, the fleet told its host to stop that agent "because the session was deleted": the agent was interrupted, and what it did meanwhile stayed in its sandbox's journal, because the session was no longer the runner's and the host's uplink was refused. This does item 7 of #46, and settles the decisions it left. ## What changes - **A session stopped only because its runner was away carries on when the runner comes back with it.** FleetDO remembers the sessions it stops for their runner's absence (`stopped_away`, in its storage). When that runner's hello reports one of them running, and no other run of it has started since, FleetDO assigns it to the runner again and gives the session back its runner. The host's uplink then gets in and replays the agent's journal, so the transcript fills in what the agent did meanwhile. The transcript says "Runner now is back with the agent still running, so the session carries on there." - **So does a session resumed meanwhile that no runner took.** With one runner, a message to the stopped session queues it ("Waiting for a runner"). When the runner comes back, the agent that still runs gets the message, rather than being stopped for a new run. - **Otherwise the old run stops, with a reason that says why**: - `Shutdown` when the session runs on another runner by then; - `Deleted` when its project was deleted; - `User` when someone stopped or archived it. All of them used to get `Deleted`. - **The stopped status says what happens**: "Runner now has been offline for 10 minutes. If it comes back with the agent still running, the session carries on there; a message resumes it on another runner." It used to end "Resume the session to continue elsewhere." - The decisions are pure functions in `fctl_domain::reconcile`, with unit tests. - Docs: `docs/architecture.md` (A runner that goes away), `docs/getting-started.md`, and ADR 0020. ## Found on the way, fixed here - **A session that moved to another runner was marked stopped when its old run ended.** When a runner came back after its session had moved, FleetDO stopped the old run, and the old run's end then marked the session stopped, though it ran on the other runner. FleetDO ignored only the ends of earlier runs on the same runner. It now also ignores a report from a runner the session doesn't run on (`reconcile::ended_earlier`). `main` takes this path too, with `StopReason::Deleted`. - **`fctl attach` printed "runner now is back" just before "stopped"** when the fleet gave up on a runner. SessionDO sent the runner's state before the session's, so `fctl attach` saw no absence for a session it still thought active. It now sends it after the summary. ## Decisions #46 left three questions for a person. The person who started this session asked for what was left of #46 to be done, so this takes the answers suggested in the issue, and ADR 0020 records them: 1. A session stopped only because its runner was away carries on when the runner comes back with it (above). 2. The grace period stays at ten minutes: the session page says at once that a runner is away (#69), and a session stopped for it comes back with its runner. 3. Disk: #93 settled it meanwhile (ADR 0019): hosts keep a floor of free disk, measured where their sandboxes are, including Docker's VM on macOS, and take no new sessions below it. Quotas per sandbox are left out. ## Compatibility Control plane only: no protocol change, no migration (FleetDO's new table is created with `IF NOT EXISTS`), and no host or agent release. Hosts already report their sessions in every hello. FleetDO's internal `ControlState` for SessionDO gains an optional `notice`. ## How I verified it - Unit tests: `fctl_domain::reconcile`: which reported runs carry on, are kept or stop, with which reason, and which ended runs are earlier ones. - `cargo xtask check`, before and after merging `main`. - End to end against `cargo xtask dev`, with its host started on its own so that I could kill it (`SIGKILL`: the process driver's agents keep running), and fake-claude slowed down (`FAKE_CLAUDE_DELAY_MS`). To keep the waits short, cases 1 to 4 and 6 ran on a control plane built with the grace period at one minute; case 5 used the real ten minutes. 1. **Carry on.** The host killed mid-turn. Ten seconds later `fctl attach` said `◌ runner dev-host is away (last heard from just now); the agent's work shows up here when it's back`: #46's item 6, from #69. The fleet then stopped the session with the new wording, while the agent finished its turn in the sandbox: journal at `agent_seq` 15, 5 acknowledged. With the host back, the session went idle, the transcript said the runner is back, and the replay brought the turn: `Bash(ls -la)`, `Write(away.md)`, the commit and the result. 2. **A message while the runner was away, and no other runner.** The message queued the session ("Waiting for a runner"). With the host back, the session carried on, the replay brought the turn the agent had finished meanwhile, and the agent then took the message and wrote its file in the same sandbox and conversation. 3. **Moved elsewhere.** With a second host, `dev-host-2`: the first host killed, the session stopped, and a message resumed it on `dev-host-2` in a new sandbox. When the first host came back, its old run stopped (`Shutdown`; FleetDO logged `ignoring the end of an earlier run`), and the session stayed idle on `dev-host-2`. Before the `SessionEnded` fix it showed "Stopped because the host is shutting down" instead. 4. **Archived while away.** The old run stopped when its host came back (`User`), and the session stayed stopped and archived. 5. **Ten minutes.** Case 1 with the grace period as shipped. The fleet stopped the session 10 minutes 2 seconds after the host was killed. When the host came back, the session carried on and the replay brought the agent's turn. Six seconds later the agent stopped on its own ("Stopped after being idle"): it had finished its turn ten minutes before, and the idle timeout is ten minutes too. 6. **After merging `main`** (#91, #93), case 1 again, with the host and the control plane built from the merge. ## Screenshots At 46cdef2, before merging `main`, which doesn't change these screens. The session page after its runner was killed mid-turn, stopped by the fleet, and brought back, with this branch's control plane and with `main`'s, against the same local state. | Before (`main`) | After | |---|---| |  |  | |  |  | |  |  | - **Before:** "Runner dev-host has been offline for 10 minutes. Resume the session to continue elsewhere." When the runner came back, the fleet stopped the agent ("Stopped (forced)"), and the turn it finished meanwhile never showed: 0 turns, no changes. - **After:** the stopped line says the session may carry on, a blue notice says the runner is back, and the agent's turn follows: listing files, writing `notes.md`, the commit, "Turn finished". The session waits for a message. Refs #46 Session: https://app.fctl.sh/s/ses_01M585PP4R26Y6BKA64816JD1W --------- Co-authored-by: fctl-sh[bot] <343971779+fctl-sh[bot]@users.noreply.github.com> Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com>
This was referenced Oct 22, 2026
Closed
kalaspuff
pushed a commit
that referenced
this pull request
Oct 22, 2026
…ts (#217) Sessions stopped "after being idle" in the middle of a turn that Claude Code started on its own to hand Claude its background subagents' reports (#213). ## What was wrong fctl-agent learned of the turns Claude Code starts on its own from `system/session_state_changed` (`running`). Since #91 it also stopped reading turns from their messages once Claude Code had reported its state. Claude Code 2.1.308 doesn't report each of those turns. From a turn that starts subagents in the background, it says `running` until they have all reported and the last turn that takes their reports has ended, and only then `idle`. The turns in between start with `system/init` and no `running`. fctl-agent took them for waiting: when the last subagent ended mid-turn, it said "Waiting for a message" and started the idle clock, and the idle timeout stopped the session while Claude was still calling tools. Background shell commands aren't affected: 2.1.308 still says `idle` after each turn and `running` before the turn that takes a command's result. I captured 2.1.308's order of events by running it against a stand-in for the Messages API on localhost, with dummy credentials (`ANTHROPIC_BASE_URL` in my test harness only), so nothing reached Anthropic or used anyone's plan. With two subagents in the background: ``` session_state_changed running system/init the person's turn task_started (local_agent) ×2 result no idle: the subagents still run task_notification (A) system/init a turn of Claude Code's own, without running … Claude's messages and tool calls … task_notification (B) in the middle of that turn … result session_state_changed idle ``` Claude Code's own code says the same: its schema describes `idle` as firing "after heldBackResult flushes and the bg-agent do-while exits", and it sends `running` only when its state was `idle`. `CLAUDE_CODE_BG_TASKS_REPORT_RUNNING=0` brings back an `idle` after each turn, but that switch is undocumented, so fctl-agent doesn't rely on it. ## What changed - **fctl-agent tells such a turn by its first message**, whether or not Claude Code reports its state: `system/init`, which starts each turn, or the conversation's first message after a `result` (`tasks::in_turn`, which now counts `system/init`). Its `result` ends it, or `idle`. 2.1.308 sends neither outside a turn: I checked that it sends nothing at startup or when it resumes a conversation, and that an interrupted turn's last messages come before its `result`. - **The idle clock waits for Claude Code's `idle`** as well as for background tasks. A session between a subagent's last report and the turn that takes it doesn't count as idle. If Claude Code went on saying `running` with no turn and no task, the session stops as with background work: an hour after its last turn, or after the idle timeout when that is longer. - **fake-claude** reports its state as 2.1.308 does for a subagent in the background (`running` until the turn that takes its report has ended) and starts each turn with `system/init`, so `cargo xtask dev` shows this flow. - **The smoke test** leaves a subagent working in the background beside the command, and checks that the session runs for both turns that take their results. - The comment in `crates/fctl-agent/src/tasks.rs`, "Idle sessions" in docs/architecture.md, and fake-claude and the smoke test in docs/development.md. No protocol change: hosts and the control plane see a session that runs or waits, as before. The fix reaches hosts with the next release. ## How I verified it - A unit test replays 2.1.308's order above (`turns_that_take_subagents_reports_count_without_running`): the session runs from the turn's `system/init`, keeps running when the second subagent ends mid-turn, and starts the idle clock only at Claude Code's `idle`. On main's code it fails where the session should run: it still says "Waiting for a background task". Two existing tests changed with the behavior, and a new one covers the hour limit. - Real Claude Code 2.1.308 under fctl-agent, in `cargo xtask dev`: a second runner ran the real `claude` against the stand-in, in a project with a one-minute idle timeout. The agent started subagents A and B in the background. The turn that took A's report ran 14 short commands over 85 seconds, and B ended 8 seconds into it. | Time | main's fctl-agent | this branch | |---|---|---| | 0.9 s | turn ends: Waiting for 2 background tasks | the same | | 4.7 s | A ends, Claude Code starts a turn: Waiting for a background task | Running | | 12.7 s | B ends mid-turn: Waiting for a message, the idle clock starts | Running | | 73 s | Stopped after being idle, mid-turn, with no `result` | Running | | 90 s | | the turn ends: Waiting for a message | | 150 s | | Stopped after being idle | - `cargo xtask dev --smoke` passes, and fails with main's fctl-agent at the new step: the session stays idle through the turns that take the command's and the subagent's results. - `cargo xtask check` passes. Fixes #213 Session: https://app.fctl.sh/s/ses_01M5JGVTF2ZK9GF69TFR7VHG4J Co-authored-by: fctl-sh[bot] <343971779+fctl-sh[bot]@users.noreply.github.com>
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.
An agent that started background work (a shell command with
run_in_background, a subagent, a Monitor) and ended its turn to wait for it was stopped by the idle timeout ten minutes later, and the work died with the sandbox. Turns Claude started on its own to take that work's result went unnoticed: the session kept saying "Waiting for a message", the idle clock kept running, and a stop ended Claude at once instead of interrupting the turn.What changed
fctl-agent (
crates/fctl-agent/src/tasks.rs,supervisor.rs)system/task_starteduntil itssystem/task_notification. Claude Code's ownambienthousekeeping doesn't count, and notifications for tasks of an earlier process (sent when Claude Code resumes) are ignored.CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS=1and followssystem/session_state_changed. When a Claude Code doesn't report its turns, the first message of the conversation after aresultstarts one (not a subagent's: those carryparent_tool_use_id). The session shows it running, the idle clock stops, and stops and interrupts reach it. A session that is stopping, or paused for a usage limit, interrupts such a turn.state.json, so it survives the stop.~/AGENTS.md, because~/AGENTS.mdis written only when the sandbox's fctl can rename the session.Web: a waiting session's reason now shows beside its status on the session page and the floor, unless it only waits for a message. The floor's headline counts sessions whose agents wait for background work as working, not as waiting for you (
web/src/lib/status.ts).fake-claude:
background <seconds>starts a background command, and fake-claude takes its result in a turn of its own. It also reports turns whenCLAUDE_CODE_EMIT_SESSION_STATE_EVENTSis set, socargo xtask devshows the whole flow. The smoke test now covers it: the session waits for a three-second background command and the agent takes its result before the session goes back to waiting.Docs: docs/architecture.md (Idle sessions), getting started, the project's idle timeout setting, and ADR 0018.
Decisions to confirm
The issue left two decisions to a person. I took the simplest choice for each; both are recorded in ADR 0018 and easy to change before merging.
Verification
running,task_started,result,idle,task_notification,running,result,idle.task_started,task_notificationandsession_state_changed, the last behindCLAUDE_CODE_EMIT_SESSION_STATE_EVENTS. Print mode runs a queued task notification as a turn of its own, and every registered task gets one terminal notification (completed,failedorstopped).cargo xtask checkpasses, and so doescargo xtask dev --smokewith the new steps.cargo xtask dev, a session's turn ended with a two-minute background command running. The session showed "Waiting for a background task", then took the result in a turn of its own two minutes later and went back to "Waiting for a message". A second session was stopped while a 15-minute command ran. Its transcript named the command, and the message that resumed it reached Claude with the note.Screenshots
At 702bc75, from
cargo xtask dev. Main's web app ran against the same control plane for the before pictures.The session page while its agent waits for a background command, light theme, 1440 px:
The floor with one session waiting for a message and one for its background command. Before, it said both were waiting for you:
A session stopped while its background command ran, then resumed by a message, in the dark theme. The transcript names the command, and the agent's reply begins with the note it got:
The session page at a phone width (390 px), dark theme, where the reason is cut short, as other reasons are:
Compatibility and what's left
Found while working on this, and filed separately: #90 (sessions stop working when their Claude access token expires; Claude Code never asks fctl for a new one).
Fixes #87
Session: https://app.fctl.sh/s/ses_01M585DBQGCCKMY571BGHGZ3C3