Skip to content

chatd, agent: the agent reports process exits and matching output instead of chatd dialing it every 15 s #295

Closed

Description

@ibetitsmike

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

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_events time over the existing agent connection (new endpoint POST /api/v0/processes/{id}/watch with 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 ReportChatProcessEvents call 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_at and 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_ids on the agent's capability advertisement.

Depends on

#293, #294.

Activity

  1. added
    enhancement
    performance
    on Nov 14, 2026
  2. ibetitsmike commented on Nov 17, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Delegated to a workstream (Opus 5.5, high) on 2026-11-17 00:47Z. Branch mike/issue-295-process-events, one PR on main with Closes #295. Dependencies #293 and #294 are merged.

  3. ibetitsmike commented on Nov 17, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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_events with process IDs fails with an error on them until the workspace restarts.

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

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions