Skip to content

chatd: wake a chat when a background process exits or prints a matching line #248

Closed

Description

@ibetitsmike

Xum created this issue on behalf of @ibetitsmike.

Part of moving the pstack lead ("Pstack General", Xum workspace 8ac7791132) to a Coder Agents chat on this fork.

Gap

execute can run a command in the background and process_output reads it, but nothing wakes the chat when that process exits or prints a line. wait_for_events watches only direct children and one GitHub pull request (waitForEventsArgs, coderd/x/chatd/fork_wait_for_events.go:37).

The lead and ops end their turns on background watchers and get woken by them:

Workspace Background bash with a monitor Monitor wakes
Lead 358 250
Fork ops not counted 903

The watchers are not all pull requests: "Deploy watcher" (152 runs, autodeploy log and buildinfo), fork-ci on main, slot guards, Codex verdict watchers and long validation runs. schedule_wakeup polling is the only substitute, at one model turn per poll and against the limit of 288 wake-up runs per chat each 24 hours.

Wanted

  • wait_for_events takes background process IDs from execute.
  • The chat wakes when a watched process exits, with its exit code and the tail of its output.
  • Optional: a line pattern that wakes the chat on a matching output line, with a cool-down and a maximum number of events.
  • The wake-up never interrupts a turn, as today.

Acceptance

  • A chat starts sleep 90; echo DONE in the background, calls wait_for_events and ends its turn. It gets one message after the exit, with exit code 0 and DONE.
  • A watch on a process that already exited wakes the chat at once.
  • The watch survives a coderd restart, or the chat gets a message that the watch was lost.

Activity

  1. added
    enhancement
    pstack-parity
    on Nov 12, 2026
  2. ibetitsmike commented on Nov 12, 2026

    @ibetitsmike
    OwnerAuthor

    Xum posted this comment on behalf of @ibetitsmike.

    Tracked in #255.

  3. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    W15: opened #263 (PR 1 of 3: process exit wake-ups for wait_for_events). Output-pattern wake-ups and the UI follow.

    Xum posted this on behalf of @ibetitsmike.

  4. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    Status: #263 (PR1, wake on background process exit) is READY on head 63e228f: fork-ci green, Codex clean, 0 open threads, dev-build UAT PASS 5/5 (including the coderd restart case). Output-pattern wakes and the site status pill follow in later PRs.

    Xum posted this on behalf of @ibetitsmike.

  5. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    Status: #263 (PR1, wake on process exit) merged as addead5. Starting PR2: output-pattern wake-ups.

    Xum posted this on behalf of @ibetitsmike.

  6. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    Status: opened #269 (PR2, wake on matching output lines of a watched process).

    Xum posted this on behalf of @ibetitsmike.

  7. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    Xum posted this on behalf of @ibetitsmike.

    W15: sent READY for #269 (output-pattern wake-ups) at 413c4bb: fork-ci green, Codex clean, 0 open threads, container UAT PASS.

  8. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    Xum posted this on behalf of @ibetitsmike.

    #269 merged as 72d8af0. Follow-ups from its Codex review, kept out of that PR:

    1. Line positions after the agent shortens output. When the agent's head/tail buffer drops bytes or shortens long lines, processNewLines maps the shown output back to original byte positions with a fixed offset. The cursor can then land past a matching line, so later matches are skipped, and a line crossing the head boundary can count as complete. A fix needs the agent to return raw byte offsets in ProcessOutputResponse (coderd/x/chatd/fork_process_events.go, processNewLines).
    2. A failed match message is dropped when the process ends. If sending a match message fails and the process exits (or the watch is lost) before the next poll, the next poll sends only the exit message and completes the entry, so the matched batch never arrives as its own message. The exit tail still shows it unless the output is long (processWakeupText, the output.Running branch).
    3. Windows line endings. A line read from \r\n output keeps its trailing \r, so end-anchored patterns such as ^READY$ never match. Strip one trailing \r before matching and display (processNewLines).

    Starting PR3: the site UI for process wake-ups, including the status pill deferred from #263.

  9. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    W15 status: PR3 is open as #270 (site UI for process wake-ups, which includes the pill P2 deferred from #263). Head is f62e780da9. Host pnpm check, tsc and the targeted Vitest run (309 tests) passed, and frontend-deep-review has no blocking or should-fix findings left. Next: fork-ci, Codex review and :3100 UAT through Fork ops.

    Xum posted this on behalf of @ibetitsmike.

  10. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    W15 status: #270 is READY at f62e780da9. fork-ci passed, Codex found no major issues, and Fork ops' :3100 UAT passed 4/4 (process card with a pattern, status pill text, two processes, child-agent regression). It's waiting for the lead to merge.

    Xum posted this on behalf of @ibetitsmike.

  11. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    W15 closing status: all three PRs for process wake-ups are merged.

    The three open PR2 follow-ups (raw byte offsets, replaying a failed match message, CRLF) are in this comment. I'm leaving the issue open for Mike to close.

    Xum posted this on behalf of @ibetitsmike.

  12. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    Xum closed this issue on behalf of @ibetitsmike.

    Done. #263, #269 and #270 are merged, and main 0d8bea4783 is deployed to the daily server, where it passed the post-deploy checks. The three PR2 follow-ups are listed in this comment. The ARCHITECTURE.md TODOs from #263 and #269 are being filled in by a separate docs PR.

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

    enhancementpstack-parity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions