---
name: "get_agent_output stream always times out for running agents"
---

# Bug 131: get_agent_output stream always times out for running agents

## Description

The `get_agent_output` MCP tool consistently returns "Stream timed out; call again to continue" even when the agent process is actively running, making API calls, and committing work. The `list_agents` call shows the agent as `running` with `session_id: null` throughout its entire execution, only populating the session_id after the process exits. This makes it impossible to observe agent progress in real time via MCP.

## How to Reproduce

1. Start an agent on a story (e.g. `start_agent` with `coder-1`)
2. Confirm the claude process is running (`ps aux | grep claude`)
3. Call `get_agent_output` with the story_id and agent_name
4. Observe it returns "Stream timed out" every time, regardless of timeout_ms value (tested up to 10000ms)
5. `list_agents` shows `session_id: null` throughout
6. Agent completes its work and commits without ever producing observable output

## Actual Result

`get_agent_output` never returns any events. `session_id` stays null while the agent is running. The only way to observe progress is to poll the worktree's git log directly.

## Expected Result

`get_agent_output` streams back text tokens and status events from the running agent in real time. `session_id` is populated once the agent's first streaming event arrives.

## Reopened — Previous Fix Did Not Work

This was archived after a coder pass but the bug is still present. With 3 agents actively running:
- `get_agent_output` returned 141 events on one call, then 0 events on the next call with a 5s timeout
- None of the events contained text output — only metadata/status events
- The server logs (`get_server_logs`) DO show agent activity (spawn commands, MCP calls), so the agents are working — the output just isn't being captured/forwarded

### Investigation needed

The coder needs to trace the full data path:
1. How does `run_agent_pty_streaming` (server/src/agents.rs) capture PTY output from the claude process?
2. How are those events published to the broadcast channel that `get_agent_output` subscribes to?
3. Is the broadcast channel being created before the agent starts producing output, or is there a race where early events are lost?
4. Are text tokens from the PTY being sent as `AgentEvent` variants that `get_agent_output` actually serializes, or are they filtered out?

## Acceptance Criteria

- [ ] get_agent_output returns streaming text events while an agent is actively running
- [ ] session_id is populated in list_agents shortly after agent spawn
- [ ] Calling get_agent_output multiple times yields incremental output from the agent
