Skip to content

fix(agent): keep sessions running while Claude takes subagents' reports - #217

Merged
kalaspuff merged 1 commit into
mainfrom
fctl/143-fix-https-github-com-hyperstructure-fctl
Oct 22, 2026
Merged

kalaspuff merged 1 commit into
mainfrom
fctl/143-fix-https-github-com-hyperstructure-fctl

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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

Claude Code 2.1.308 says `running` from the turn that starts subagents in
the background until they have all reported and the last turn that takes
their reports has ended, so those turns start without a `running` of
their own. fctl-agent read Claude Code's state as a report of each turn,
and stopped reading turns from messages once it had one: it missed those
turns, said the session waited, and started the idle clock when the last
subagent ended, in the middle of a turn.

fctl-agent now tells a turn Claude Code starts on its own by its first
message (`system/init`, or the conversation's first message after a
`result`) whether or not Claude Code reports its state, and the idle
clock waits for Claude Code's `idle` as well as for background tasks, an
hour after the last turn at most.

fake-claude says `running` as Claude Code 2.1.308 does for a subagent in
the background and starts each turn with `system/init`, and the smoke
test leaves a subagent working in the background too and checks that the
session runs for the turns that take the results.

Fixes #213
@fctl-sh
fctl-sh Bot requested a review from kalaspuff as a code owner October 22, 2026 20:16
@kalaspuff
kalaspuff merged commit b586d3f into main Oct 22, 2026
9 of 10 checks passed
@kalaspuff
kalaspuff deleted the fctl/143-fix-https-github-com-hyperstructure-fctl branch October 22, 2026 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Sessions stop as idle in the middle of a turn Claude starts for background work's results

1 participant