Skip to content

Deliver messages to a task whose agent quit but whose tab is still open - #584

Merged
pallaoro merged 4 commits into
mainfrom
messages-queue-behind-a-pane-whose-agent-quit
Nov 30, 2026
Merged

pallaoro merged 4 commits into
mainfrom
messages-queue-behind-a-pane-whose-agent-quit

Conversation

@pallaoro

@pallaoro pallaoro commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

The bug

A message sent to a task (from the Lead, the user or another task's agent) was never delivered while that task had a tab whose agent quit and left its pane open as a shell. The sender was told "queued: its agent is busy, so it gets the message as soon as it can take it", and the message stayed pending for as long as the tab stayed open. The PTY daemon outlives app restarts and the reaper only takes idle sessions, so that could be days.

Why: liveAgentSession() in packages/server/src/peer-messages.ts took any non-shell session with a live PTY as the task's agent, whatever its status. A pane whose agent quit is stopped on a live PTY (applyAgentQuit, pty/agent-quit.ts), and holdsWork() in pty/reap.ts is true for every status but idle, so the plan was wait on every sweep. list_peers reported such a task as idle instead of not_running.

The fix

  • liveAgentSession() skips a stopped pane, as liveAgentIds (services.ts), liveTabOn (sessions.ts), applyAgentQuit and sessionAlive (loops/session-liveness.ts) already do. Such a task now gets its agent started in a new tab with the message, resuming its conversation; the shell is left open.
  • A Start in a stopped pane is an agent the user ran there again by hand. sessionStatusAfter() in engine.ts kept stopped on a Start, so with the first change alone a message arriving before that agent's first activity would have started a second agent beside it, where before it reached the listener already waiting. It now makes the session idle, and the card is redrawn so its tab shows the agent again, not a shell.
  • A stopped pane whose listener is asking for messages still counts as the task's agent: OpenCode reports no Start at all, and Claude's listener can ask before its Start is recorded.

Verified

  • New tests in packages/server/test/peer-messages.test.ts ("A task whose agent quit but whose tab is still open") and session-status.test.ts. Three of them fail on main; the other two (OpenCode's listener in that pane, another busy agent of the same task) pass on main and guard against regressions from this change.

  • End to end with a real engine, PTY daemon, hook server and launch line, against a stand-in claude that quits on cue: the agent quits and its pane stays (stopped, PTY live); a Lead message is answered "Ateam is starting it" and resumes the conversation in a new tab with the message, no error, shell left open; claude typed into the shell reports Start, the session becomes idle and the card is redrawn with an agent tab; a message after that starts no second agent. 9/9 checks.

  • bun test in packages/server: 1029 pass, 1 fail. The failure is system:hello handshake, which timed out at 5 s probing installed agents on a busy machine; it passes alone with a longer timeout and does not touch this code.

  • tsc --noEmit clean. Biome is clean on the changed files except two findings in engine.ts (import order, one line wrap) that are on main already.

  • With main merged in (Tell the Lead and the user when a message never reached a task's agent #585, A message to a task whose conversation is gone starts a new one instead of being lost #586): bun test in packages/server 1052 pass, 0 fail (30 s per-test timeout). A message to a task whose conversation is gone starts a new one instead of being lost #586 leaves a tab whose new conversation never started open as a stopped shell; the end-to-end run of that case with the manual close of that tab removed passed 27/27: the tab stays open, the next message is answered "Ateam is starting it" and reopens the task's real conversation.

Not covered

  • Codex run again by hand in a quit pane without its trusted hooks reports no Start and has no listener, so it stays a shell to the messenger until its first turn ends; a message in that window starts the task's agent in a new tab.
  • A hand-run agent left at its prompt for two hours is now idle, so the reaper reclaims it like any other idle agent; before, it stayed stopped and was never reaped.

An agent that quits leaves its pane open as a shell, its session `stopped`
on a live PTY (pty/agent-quit.ts). The peer messenger still took that pane
for the task's live agent, found it busy (holdsWork is true for every status
but idle) and queued every message to the task for as long as the tab stayed
open, telling the sender "its agent is busy". list_peers called it idle.

- liveAgentSession() skips a `stopped` pane, as liveAgentIds, liveTabOn,
  applyAgentQuit and sessionAlive already do: such a task gets its agent
  started in a new tab with the message, and the shell is left alone.
- A Start in a `stopped` pane is an agent the user ran there again by hand:
  sessionStatusAfter() makes it `idle`, and the card is redrawn. Kept
  `stopped`, it would have been skipped while its listener was already
  waiting, and a message would have started a second agent.
- A `stopped` pane whose listener is asking still counts: OpenCode reports
  no Start, and Claude's listener can ask before its Start is recorded.
pallaoro added a commit that referenced this pull request Nov 22, 2026
A message to a task whose agent quit with its tab left open as a shell is
fixed in "Deliver messages to a task whose agent quit but whose tab is still
open" (#584), so this change no longer carries its own version of it: the
liveAgentSession filter, the Start on a stopped session, the listener's status
write and their tests go back to main's.
pallaoro added a commit that referenced this pull request Nov 22, 2026
#585)

* Tell the Lead and the user when a message never reached a task's agent

A message from the Lead chat or the user (a reply, an answer to a question)
that never reached the task's agent was recorded and logged only, so the Lead
went on promising an answer that could not come and the card looked done.

Now, when Ateam starts an agent for such a message and that agent cannot
start, quits before starting up, or is still not up after two minutes (most
likely held at a prompt in its terminal), the task records it
(tasks.undelivered, TaskDTO.undelivered) and its card goes to Needs You. The
Lead chat that sent it shows a notice on the task, and the Lead's agent hears
it with the user's next message. The Mac shows a banner once, and the task's
panel shows why. The record clears once an agent of the task shows up again.
The idle-archive sweep counts it as activity, so a month-old task whose
conversation Claude Code dropped is not archived the hour after.

A message to a task whose agent quit but whose tab is still open as a shell no
longer waits on that shell for good: it starts the agent in a new tab. An
agent the user starts by hand in that tab counts as up and takes it instead.

* Leave the quit-tab fix to its own PR

A message to a task whose agent quit with its tab left open as a shell is
fixed in "Deliver messages to a task whose agent quit but whose tab is still
open" (#584), so this change no longer carries its own version of it: the
liveAgentSession filter, the Start on a stopped session, the listener's status
write and their tests go back to main's.
…-a-pane-whose-agent-quit

# Conflicts:
#	packages/server/test/peer-messages.test.ts
…-a-pane-whose-agent-quit

# Conflicts:
#	packages/server/test/peer-messages.test.ts
…-a-pane-whose-agent-quit

# Conflicts:
#	packages/server/src/peer-messages.ts
#	packages/server/test/peer-messages.test.ts
@pallaoro
pallaoro merged commit 7be7c0a into main Nov 30, 2026
1 check passed
pallaoro added a commit that referenced this pull request Nov 30, 2026
Since #584 a Start in a pane whose agent quit (`stopped`) makes it an idle
agent again. Its row kept the conversation id of the agent that quit:
adoptReportedConversation never replaces an id, and never adopts one for
Claude. A restart (the user's own words to a Claude with no mod, or a Codex
with no listener) then killed the hand-run agent and its shell and reopened
the conversation that quit.

On a Start in a `stopped` pane, the row now takes the id the agent reports.
Any other event, and a Start in a pane whose agent is still there (a resume
or a compaction), leave the id as before.
pallaoro added a commit that referenced this pull request Dec 1, 2026
…632)

* Deliver messages to a task whose agent quit but whose tab is still open

An agent that quits leaves its pane open as a shell, its session `stopped`
on a live PTY (pty/agent-quit.ts). The peer messenger still took that pane
for the task's live agent, found it busy (holdsWork is true for every status
but idle) and queued every message to the task for as long as the tab stayed
open, telling the sender "its agent is busy". list_peers called it idle.

- liveAgentSession() skips a `stopped` pane, as liveAgentIds, liveTabOn,
  applyAgentQuit and sessionAlive already do: such a task gets its agent
  started in a new tab with the message, and the shell is left alone.
- A Start in a `stopped` pane is an agent the user ran there again by hand:
  sessionStatusAfter() makes it `idle`, and the card is redrawn. Kept
  `stopped`, it would have been skipped while its listener was already
  waiting, and a message would have started a second agent.
- A `stopped` pane whose listener is asking still counts: OpenCode reports
  no Start, and Claude's listener can ask before its Start is recorded.

* A pane revived by hand takes the conversation its new agent reports

Since #584 a Start in a pane whose agent quit (`stopped`) makes it an idle
agent again. Its row kept the conversation id of the agent that quit:
adoptReportedConversation never replaces an id, and never adopts one for
Claude. A restart (the user's own words to a Claude with no mod, or a Codex
with no listener) then killed the hand-run agent and its shell and reopened
the conversation that quit.

On a Start in a `stopped` pane, the row now takes the id the agent reports.
Any other event, and a Start in a pane whose agent is still there (a resume
or a compaction), leave the id as before.
pallaoro added a commit that referenced this pull request Dec 1, 2026
An agent that quits leaves its pane open as a shell, its session `stopped`
on a live PTY (pty/agent-quit.ts). Each place that cares wrote its own test
of that, and each one that missed it was a bug (#584 queued every message
behind such a pane; #632 had to add one more).

- runsAgent(session, listening) in protocol/session-tabs.ts is the one
  answer: not a shell row, and not `stopped` unless its message listener is
  asking (an agent run again by hand: OpenCode sends no Start).
- liveAgent(session, isLive, listening) in pty/agent-quit.ts adds "not
  ended and its terminal is open" for callers holding the PTY daemon.
- Moved onto them: taskSessionAgents, createSessionInCallerTask,
  liveTabOn, applyAgentQuit, sessionAlive, tileSessions, the peer
  messenger (liveAgentSession, addressedSession, the asking-tab check),
  adoptReportedConversation, the browser relay check, the Mac link's
  task-session check, and the Start handler in the engine.
- A guard test fails on any other comparison of a session's `status` with
  "stopped" outside session-tabs.ts.

Behaviour changes, both small:
- sessionAlive (the board organizer's and triage's "does a live agent own
  this card") no longer counts a plain shell tab as a live agent. A shell
  row is never `stopped`, so a task with only a shell open read as having
  a live agent.
- The browser relay check also refuses a shell row, which holds no relay
  token.

Which agent's conversation a row holds (engine launch target, restore) is
a different question and still reads `agentId`; the comments say so.

Tests: runsAgent, liveAgent, sessionAlive, the quit-pane browser case and
the guard. Run against a real engine and PTY daemon with the quit-pane
harness: 10/10.
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