Skip to content

Sessions stop while their agents wait for background tasks #87

Closed

Description

@fctl-sh

What is wrong

An agent that starts background work and ends its turn to wait for it gets stopped by the idle timeout, and the work dies with it. Background work here means a shell command with run_in_background, a subagent, or a Monitor. Ten minutes after the agent's last turn the session stops, its sandbox stops, and everything still running in it is gone. Nothing in the session says so.

In session https://app.fctl.sh/s/ses_01M57YPG1ATK9ARAXTZG43G51J (#16), the agent had two things running in the background: a review subagent, and cargo xtask dev for screenshots. It ended its turn to wait for the review, and the person asked it to open the pull requests once the review finished. The session stopped while both still ran. When the person's next message resumed it, Claude Code reported:

Background agent "Review the images branch diff" didn't finish before the previous session ended

Background shell command didn't finish before the previous session ended

The person had to say "Please continue", and the agent had to resume the review and start the dev stack again.

A related problem: when background work finishes while the session is still up, Claude Code tells the agent with a task notification, and can start a turn on its own to handle it. fctl-agent doesn't notice:

  • the session keeps saying "Waiting for a message";
  • the idle clock keeps running from the end of the previous turn;
  • when the clock runs out, the stop ends Claude at once instead of interrupting the turn.

How it works now

In crates/fctl-agent/src/supervisor.rs:

  • A turn ends: on_claude_message makes the session idle ("Waiting for a message") on a result with no queued_turn_count, and set_status starts idle_since.
  • A turn starts: only send_content marks a turn active (turn_active) and clears idle_since. It runs for a message from a person, or from fctl itself, like a continuation after a usage limit.
  • The idle timeout: once idle_since is older than the project's idle timeout (idle_timeout_minutes, 10 by default), tick calls begin_stop(StopReason::Idle, …). With no active turn, that calls finish() right away. Claude ends, and the host stops the sandbox (ADR 0013).

Claude Code reports background work in its stream-json output. fctl-agent journals these events but nothing reads them (Claude Code 2.1.302; 2.1.100 has the same events):

  • {"type":"system","subtype":"task_started","task_id":…,"tool_use_id":…,"description":…,"task_type":…,"is_backgrounded":…} when a task starts;
  • {"type":"system","subtype":"task_notification","task_id":…,"status":…,"summary":…,"output_file":…} when it ends, for any reason;
  • {"type":"system","subtype":"session_state_changed","state":…} (running, idle, …) when a turn starts or ends, including one Claude starts itself. It is sent only when CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS is set.

What should happen

  • fctl-agent keeps track of Claude's background tasks: each task_started without its task_notification yet. While one runs, the session isn't idle: the idle clock starts when the last one ends, and the session says why it waits ("Waiting for 2 background tasks").
  • A turn Claude starts on its own counts as a turn. The session shows it running and the idle clock stops. Signals for it: session_state_changed with CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS=1, or the first message after a result. A stop then interrupts the turn, as for any other.
  • A limit, so that a forgotten task (a dev server, a tail -f) doesn't hold a runner's slot for ever. After a set time with background work and no turn, the session stops anyway and says which tasks it stopped.
  • When a session stops with background tasks still running (that limit, a person's stop, a drain, an uninstall), the transcript names them. The message that resumes the session tells the agent they didn't finish.
  • Agents' instructions (prompt.rs, ~/AGENTS.md) say how long background work may run after a turn ends, so that agents know when waiting is safe.

Compatibility

This changes fctl-agent only. A session's reason for waiting is text, so "Waiting for 2 background tasks" needs no protocol change. Hosts and the control plane see a session that runs or waits, as now. Agents of older versions keep stopping at the idle timeout. Before relying on the events, check which Claude Code versions send task_started, task_notification and session_state_changed. 2.1.100 already has all three, below the 2.1.101 that fctl requires (#16).

Decisions for a person

  • The limit: how long may background work keep a session, and its runner's slot, after the last turn? A fixed time (an hour, say), a multiple of the project's idle timeout, or a project setting?
  • Capacity: should a session that only waits for background tasks count against its runner's capacity like a running one? It keeps its sandbox either way.

Session: https://app.fctl.sh/s/ses_01M57YPG1ATK9ARAXTZG43G51J

Activity

  1. added
    bug
    agents
    on Oct 18, 2026
  2. added a commit that references this issue on Oct 18, 2026
    c4569c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentsbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions