Repository navigation
Description
Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.
Summary
wait_for_events with process_ids learns that a background process exited, or printed a matching line, by having the wake-up worker dial the workspace agent and call ProcessOutput every 15 s for every watched process. The agent already knows the moment a process exits and already keys its processes by chat ID. It should tell coderd.
Polling here is the worst kind: it holds the single delivery goroutine for up to 30 s per unreachable agent, so one stopped workspace delays every other wake-up on the replica.
Evidence
fork_process_events.go:296-330processWakeupText: per poll,conns.agentRuns(workspace build lookups),conns.conn(dial),conn.ProcessOutput.fork_process_events.go:178-186: "A dial to an offline agent waits until its context ends", bounded bydialTimeout= 30 s (chatd.go:73); the whole read is bounded byprocessReadTimeout= 30 s (fork_process_events.go:37-40).fork_wait_for_events.go:453-492: the read runs insidedeliverEventsWakeup, on the worker's only delivery goroutine, once per tick per wake-up.fork_process_events.go:41-43: after a failed read the wake-up is due again in 60 s, and fails again for 30 s.- The agent side already has what is needed:
process.go:78-81keys each process by(chatID, id), andagentconn.go:1589-1595showsProcessOutputsupports a blockingwait, so the agent has an exit signal internally.
Latency today: 5 to 10 s typical (measured during the W15 UAT), up to 15 s plus tick time, unbounded while the batch is blocked.
Proposed design
Agent to coderd
Add one RPC to the agent API (agent/proto/agent.proto, service Agent), implemented in a fork file under coderd/agentapi:
message ReportChatProcessEventsRequest {
message Event {
string chat_id = 1;
string process_id = 2;
oneof kind {
Exit exit = 3; // exit code, signal, tail of output (same cap as today's exit message)
OutputMatch output_match = 4; // matching lines since the last report
}
google.protobuf.Timestamp at = 5;
}
repeated Event events = 1;
}
rpc ReportChatProcessEvents(ReportChatProcessEventsRequest) returns (ReportChatProcessEventsResponse);
Agent behavior, in a fork file next to agent/agentproc:
- Exit: report the exit of every process started with a chat ID. No registration. Retry with backoff until acknowledged; keep unacknowledged exits in memory for as long as the agent keeps an exited process (1 hour today).
- On every (re)connect: send the exits it still remembers that were not acknowledged. This is the reconcile for reports lost to a coderd restart.
- Output patterns: chatd registers a pattern at
wait_for_eventstime over the existing agent connection (new endpointPOST /api/v0/processes/{id}/watchwith pattern, cool-down, max events). The agent matches lines as they are written and reports per cool-down. A lost registration (agent restart) loses the process too, which the user already sees as "the agent no longer knows it".
coderd
ReportChatProcessEvents handler, for each event: one UPDATE that sets fired_at and fired_payload on the open process watch rows of (agent_id, process_id) (index chat_wakeup_watches_open_process_idx from #293), then forkWaker.Poke(chat_id). No matching open watch: drop the event. The handler never sends a chat message itself.
Creating the watch without a race
In processWaitEvents (fork_process_events.go:100-137), commit the watch row first, then read the process state once. A process that already exited is marked fired from that read. A process that exits between the commit and the read is covered by the agent's report, which now finds the row.
Workspaces that stop
Today each poll checks whether the agent's build still runs. Replace it with one set-based statement in the waker's sweep: mark as fired, with the existing "workspace stopped or was rebuilt" text, every open process watch whose agent no longer belongs to the workspace's latest running build. Run it every 5 minutes, not per watch.
Removed
processConns, processWakeupText, processReadTimeout, processErrorRetry, and the half-lease guard for process reads.
Acceptance criteria
- An agent test starts a process with a chat ID, lets it exit, and asserts exactly one
ReportChatProcessEventscall with the exit code, and a second call after a simulated failed acknowledgement. - An agentapi test reports an exit for a watched process and asserts the watch row has
fired_atand the waker was poked; an exit for an unwatched process changes nothing. - A chatd test with an agent connection that always fails to dial shows that an interval wake-up of another chat is delivered on time.
- A test orders "watch row committed, process exits, state read" both ways and asserts one message.
- Agents built before this change keep working: an old agent never reports, so its process watches fire only through the creation-time read and the stopped-workspace sweep. State that limit in the tool description, or gate
process_idson the agent's capability advertisement.
Activity
ibetitsmike commentedon Nov 17, 2026
Delegated to a workstream (Opus 5.5, high) on 2026-11-17 00:47Z. Branch
mike/issue-295-process-events, one PR onmainwithCloses #295. Dependencies #293 and #294 are merged.ibetitsmike commentedon Nov 17, 2026
Fixed by #376, merged 2026-11-17 08:03Z as ada0207. The agent now pushes chat process exits and output matches over a new fork Agent API RPC, and chatd no longer dials agents every 15 s. It stays behind
fork-chat-wakeups.Note: workspace agents built before this deploy cannot report process events.
wait_for_eventswith process IDs fails with an error on them until the workspace restarts.