Repository navigation
feat: the workspace agent reports chat process events - #376
Conversation
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d1020fd84c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c97c875907
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…rop resent output reports The final poll of a process watch now waits until the agent says, after the expiry plus a margin, that it holds no unacked event. The agent says so with an empty ReportChatProcessEvents every 30 s while a chat process runs, and coderd records it per watch. A connected agent whose reports keep failing no longer passes for one that reported. A report of matching lines carries the agent's match time, kept in spec.output_at, so a resent report fires nothing and does not count against output_max_events.
…ocess-events-825b2d7c # Conflicts: # coderd/database/forkdb/sqlc.yaml
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e37ef8a451
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… raw output and send valid UTF-8
…ocess-events-825b2d7c
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eba9c4fdf3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…nforce the output cool-down at delivery
…ocess-events-825b2d7c # Conflicts: # coderd/database/forkdb/sqlc.yaml
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 95b794c5a3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… watches, keep report times increasing, drop stale exits of a reused process ID
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…ocess-events-825b2d7c # Conflicts: # agent/proto/agent.pb.go # coderd/agentapi/api.go # coderd/workspaceagentsrpc.go
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Merged at head Gates on this exact head:
Tests I ran on
Review notes:
Not exercised:
|
Closes #295
Part of #347: removing the process read from
deliverEventsWakeup, withwakeupReadFitsandwithWakeupDeadline, also resolves item 1 (a read that did not fit the time left returned nil and re-ran at once) and the quartz nit of item 5 (the delivery deadline usedtime.Now). #347 has its own PR for the rest.The workspace agent now reports when chat background processes exit and when their output matches a pattern. chatd no longer dials the agent every 15 seconds. An agent that is offline can no longer delay the wake-ups of other chats.
What changed
ReportChatProcessEventsis added toagent/proto/agent.proto. It is served as its own fork DRPC service (agent/proto/fork_agent_drpc.go), so the numbered Agent API version stays at 2.16. Bumping the version would put a fork number into a sequence upstream owns and make every sync conflict. A coderd without the RPC answers with an unknown RPC error. The agent then logs at debug level and backs off.agent/agentproc/fork_process_events.goqueues one exit event for each process started with a chat ID. The event carries the code (-1 for a signal), the last 8 KiB of output and the exit time.agent/fork_process_events.gosends the queue over the Agent API connection in batches of up to 100. An event is removed only after coderd acks it, and failed calls retry with backoff from 1 s to 1 min. Whatever a lost connection left unacked goes out on the next connection. While a chat process runs and nothing waits for an ack, the agent sends two empty reports every 30 s. Each one sends back theacked_atof the response before it aslast_acked_at, and coderd records that time, so an empty report tells coderd that every exit queued before that time of coderd was acked. Output-pattern watches are registered over the existing agent connection (POST /api/v0/processes/{id}/watch). The agent reads the output every second and reports new matching lines, subject to the cooldown. It does not count its reports: coderd merges several into one message, so the response names the output events that no open watch takes lines for (closed_output_events), and the agent stops those watches.coderd/agentapi/fork_process_events.gofires the open watches that match the chat, process and reporting agent (FireChatProcessWatches), then pokes the waker for each chat. Events that match no watch change nothing. A delivery uses the fired payload it read as a guard (MarkChatWakeupWatchDelivered), so a report that lands during a delivery is not lost.coderd/x/chatd/fork_process_events.gono longer reads processes on a tick or during a delivery. The tool first commits the watch rows, then registers them with the agent and reads the process state once in that same request. The exit fires its watch from whichever path comes first. Both paths write the same payload andfired_atkeeps the first write, so the chat gets one message whichever order they run in. Every 5 minutes, a set-based sweep (MarkLostChatProcessWatches) fires as lost the watches whose workspace stopped or was rebuilt, or whose agent restarted after the watch began.01a1e710-..._chat_wakeup_watches_open_process_idx(a partial index on open process watches). No existing node is edited.coderd/x/chatd/ARCHITECTURE.mdand added### Background process events. I also updateddocs/fork/pstack-adapter.md.Old agents (criterion 5)
An agent too old to report process events either has no
ForkProcessEventsConnor answers the watch route with 404. In that case the tool fails the call with an error, and the tool description states the limit. I picked this over a capability flag because it is the honest and simple choice:schedule_wakeupor restart the workspace.Experiment gating
The feature stays behind
fork-chat-wakeups. The agent always reports, and coderd decides what to do with the report. With the experiment off,coderd/fork_process_events.gopasses no options, and the handler acks every report without writing anything. Nothing creates or reads process watches when the experiment is off, so the old 15-second path did not need to survive.Known limits
started_atwith the creation time of the watch. If the agent's clock runs far ahead of coderd's, a watch created right after the agent started can be marked lost.acked_atfrom before it. The poll waits at most 10 minutes; an agent that stays away or keeps failing its reports longer loses an exit it queued before the expiry.processExitedBycomparedExitedAt+1).Tests
All Go runs were done under
flock ~/.fork-go.lock, on the tree rebased onto main99d1b24aed, unless noted. After merging main76b39d63c8(#369) the chatd, forkdb, agentapi, agentproc and dbpurge packages and the dbauthz suite were run again at count=1 and passed, with no generate drift.TestForkReportProcessExitchecks that the exit is reported exactly once with its code. After a simulated failed ack, it checks that the exit is reported a second time.TestForkReportProcessEventsReconnectchecks that the unacked events are resent on a new connection.TestReportChatProcessEventschecks that an exit for a watched process setsfired_atand pokes the waker, and that an unwatched exit changes nothing.TestWakeupProcessAgentUnreachable, every agent dial fails, yet an interval wake-up of another chat is delivered on the first pass. The poke delivers the exit with zero dials.TestWaitForEventsProcessExitOrderruns "watch row committed, process exits, state read" in both orders and asserts one message.TestWakeupEventsProcessOutcomesadds theAgentRestartedandAgentStartedBeforecases to the lost-watch sweep tests.TestWakeupFinalPollProcesseschecks that the final poll waits while the agent never said, or said only before expiry plus the margin, that it holds no unacked event. It completes once the agent said so after the margin, or once the 10-minute window has passed. InReportAfterRetry, an exit from before the expiry that is reported after the first final poll is delivered once.MarkChatProcessWatchesReported).TestForkReportProcessEventsHeartbeatchecks that no empty report goes out while no chat process runs, that a pending exit whose ack was lost goes out before the empty report, and that the next tick sends another.TestReportChatProcessEventschecks that an empty report writesreported_atand pokes nobody.TestMarkChatProcessWatchesReportedchecks that only open, unended watches of active wake-ups on that agent are marked.process_output_max_events:FireChatProcessWatcheskeeps the agent's match time inspec.output_atand ignores a report that is not newer.TestFireChatProcessWatchesandTestReportChatProcessEventsOutputResentcheck that a resent report changes nothing, before and after a delivery, and that a later one still fires.e37ef8a451):FireChatProcessWatches,MarkChatProcessWatchesReportedandMarkLostChatProcessWatchesnow acceptpausedas well asactive.TestWakeupEventsProcessExitWhilePausedreports an exit while paused, resumes, and checks one delivered message.TestFireChatProcessWatchesandTestMarkChatProcessWatchesReportedcheck that paused counts and canceled does not.TestForkEnqueueCapchecks that an exit is never dropped, that the oldest output match goes first, and that an exit past the cap with only exits queued keeps its code but not its tail.forkRaw) instead of the formatted output.TestForkNewLines,TestForkRawandTestForkOutputWatchOpenLineAcrossHeadcheck an open line that spans the head buffer.strings.ToValidUTF8.TestForkInvalidUTF8marshals a report built from invalid bytes.Commands and results:
go test -count=20 -race -run TestFork ./agent/agentproc/: PASSgo test -count=20 -race -run TestReportChatProcessEvents ./coderd/agentapi/: PASSgo test -count=20 -race -run TestFireChatProcessWatches ./coderd/database/forkdb/: PASSgo test -count=20 -race -run 'TestWakeupProcess|TestWaitForEventsProcess|TestWakeupEventsProcess|TestProcess' ./coderd/x/chatd/: PASS-count=20runs above were done on base57bea8f79a, before the rebase. The rebase brought in fix(chatd): adopt a workflow step another runner journaled, and fix fork-ci flakes #375, which touches none of these files.go test -count=1 ./coderd/x/chatd/: PASSgo test -count=1 ./coderd/database/forkdb/... ./coderd/agentapi/ ./agent/agentproc/: PASSgo test -count=1 -run TestMethodTestSuite ./coderd/database/dbauthz/: PASSgo test -count=1 -race -run 'TestWaitForEvents|TestWakeup|TestProcess' ./coderd/x/chatd/(after the last edit): PASSc97c875907:go test -count=1 ./coderd/x/chatd/PASS.go test -count=20 -race -run 'TestWakeupFinalPollProcesses|TestWakeupEventsProcess|TestWakeupProcess|TestWaitForEventsProcess' ./coderd/x/chatd/PASS.go test -count=1 ./coderd/x/chatd/: PASSgo test -count=1 -run TestMethodTestSuite ./coderd/database/dbauthz/: PASSgo test -count=20 -race -run TestFork ./agent/agentproc/: PASSgo test -count=20 -race -run TestReportChatProcessEvents ./coderd/agentapi/: PASSgo test -count=20 -race -run 'TestFireChatProcessWatches|TestMarkChatProcessWatchesReported' ./coderd/database/forkdb/: PASSgo test -count=20 -race -run 'TestWakeupFinalPollProcesses|TestWakeupEventsProcess|TestWakeupProcess|TestWaitForEventsProcess|TestProcess' ./coderd/x/chatd/: PASSgo test -count=1 ./coderd/x/chatd/ ./coderd/database/forkdb/... ./coderd/agentapi/ ./agent/agentproc/ ./coderd/database/dbpurge/: PASSgo test -count=1 -run TestMethodTestSuite ./coderd/database/dbauthz/: PASSgo test -count=20 -race -run TestFork ./agent/agentproc/: PASSgo test -count=20 -race -run 'TestFireChatProcessWatches|TestMarkChatProcessWatchesReported' ./coderd/database/forkdb/: PASSgo test -count=20 -race -run 'TestWakeupEventsProcess|TestWakeupFinalPollProcesses|TestWaitForEventsProcess|TestWakeupProcess|TestProcess' ./coderd/x/chatd/: PASSd310975509(perf(coderd/x/chatd): pack path cache, one linkable plugins query, subscribe before a step wait #370): the chatd, forkdb, agentapi and agentproc packages at count=1 and the dbauthz suite again: PASS, with no generate drift.497bbb6a70(perf: purge old fork rows and bound wake-up and report reads #368, perf(coderd/x/chatd): count notify_user sends in a fork table #369), on head95b794c5a3, cold build cache:generate.shno drift,chatdmig checkok,go build ./enterprise/cmd/coderok,go vetok,go test -count=1 ./coderd/x/chatd/ ./coderd/database/forkdb/... ./coderd/agentapi/ ./agent/agentproc/ ./coderd/database/dbpurge/ ./coderd/database/migrations/chatdmig/PASS, dbauthzTestMethodTestSuitePASS,-count=20 -raceon agentprocTestFork, forkdbTestFireChatProcessWatches|TestMarkChatProcessWatchesReported, chatdTestWakeupEventsProcess|TestWakeupFinalPollProcesses|TestWaitForEventsProcess|TestWakeupProcess|TestProcessand agentapiReportChatProcessEventsPASS, new-code golangci 0 issues,make lint/markdown lint/emdash lint/style-claims lint/architecturepass. The merge kept both new chatdmig nodes of main and this PR side by side insqlc.yaml; neither edits an existing node.eba9c4fdf3), three P2 findings, all regressions of this PR, fixed inf92b24b196:forkEnqueuenow drops the oldest exit past 10000 exits.TestForkEnqueueCapfills the queue with 10000 exits, adds one more and checks that the oldest is gone and the new one is last.TestForkHandleWatchProcess/SameWakeupWatchesOncechecks that a repeat of one wake-up adds nothing, that other wake-ups each get a watch, and that a fifth stops the oldest.ListDueChatWakeupsand the process case of the wake-up now skip fired output lines whilelast_run_at + output_cooldown_secondsis after now, and the run time is set to the end of the cool-down.TestWakeupEventsProcessOutputCooldownchecks that lines fired during the cool-down are not due, that the run time is the cool-down end, and that they are delivered then in one message.95b794c5a3), fixed in0f7d6c5584and76108de735(the limit case ofTestFireChatProcessWatches). The proto messages changed again (OutputMatch.lastremoved,last_acked_at,acked_atandclosed_output_eventsadded); no agent with the earlier shape shipped.acked_at, the next report sends it back, and coderd recordsmin(last_acked_at, now); a report without it, the first of a connection, records nothing.TestForkReportProcessEventsHeartbeatchecks two empty reports per tick, each with theacked_atof the response before it, and nolast_acked_atbefore any ack.TestReportChatProcessEventschecks that a report withoutlast_acked_atchanges nothing, that one with it records that time and not the time of receipt, that a future one is cut to now, and that the response carriesacked_at, with the experiment off too.process_output_max_eventsreports, while coderd merges reports into fewer messages. The agent no longer counts; coderd answers withclosed_output_eventsfromChatProcessWatchTakesLines.TestForkOutputWatchchecks that the watch goes on after reports coderd does not close and ends when coderd closes it.TestReportChatProcessEventsOutputResentchecks that a resent report is not closed, that a report for an unknown wake-up is closed by its index in a batch, and that a report after the exit is closed.TestFireChatProcessWatcheschecksChatProcessWatchTakesLinesfor another wake-up, a watch at its limit and an ended watch.forkReportTimekeeps them increasing.TestForkReportTimechecks it.TestForkProcessIDReusechecks that the stale event of that chat goes and the one of another chat stays.... [truncated]marker: fixed to the 4 KiB cut after the match, with no marker.generate.shno drift,chatdmig checkok, build ok, vet ok, the five packages at count=1 PASS, dbauthzTestMethodTestSuitePASS, the four-count=20 -raceruns PASS, new-code golangci 0 issues, the markdown lints pass.go run ./coderd/database/migrations/chatdmig/cmd/chatdmig check: okgo build ./enterprise/cmd/coder: ok./coderd/database/forkdb/generate.sh: no driftgolangci-lint --config=.golangci.new-code.yaml --new-from-merge-base=origin/mainon the changed packages: 0 issuesmake lint/markdown lint/emdash lint/style-claims lint/architecture lint/check-experiment-keys lint/migrations: pass (the last round reranlint/markdown lint/emdash lint/style-claims lint/architecture)Negative controls (patched, saw FAIL, restored and checked with
cmp):TestForkReportProcessExitfails because the second report never comes.TestReportChatProcessEventsfails because the poke never arrives.TestWakeupFinalPollProcessescases fail, among themReportAfterRetry. (Rerun on the new head withprocessReportedalways true:OnTimeLines,LateNotFiredNeverReported,LateNotFiredReportedBeforeExpiry,LateNotFiredReportedInMarginandReportAfterRetryfail.)FireChatProcessWatchesdrops theoutput_atcondition,TestFireChatProcessWatchesandTestReportChatProcessEventsOutputResentfail.activewake-ups,TestWakeupEventsProcessExitWhilePausedfails because the fire returns no row.TestForkEnqueueCapfails. If the output watch reads the formatted output,TestForkOutputWatchOpenLineAcrossHeadfails with an unexpected report of an open line. WithoutToValidUTF8,TestForkInvalidUTF8fails on marshal.ListDueChatWakeupsand the wake-up,TestWakeupEventsProcessOutputCooldownfails with two messages instead of one. If a new wake-up replaces the output watches of a process,SameWakeupWatchesOncefails (1 watch, want 4). Without the 10000 exit bound,TestForkEnqueueCapfails because the oldest exit stays.TestForkReportProcessEventsHeartbeatfails waiting for it. If it sends one while no chat process runs, the test fails with an unexpected report.last_acked_at,TestReportChatProcessEventsfails (10 s off). If the agent sends one empty report per tick, or never sends backacked_at,TestForkReportProcessEventsHeartbeatfails. If the agent ignoresclosed_output_events,TestForkOutputWatchfails because the watch never ends. IfforkReportTimereturns now,TestForkReportTimefails. Without the drop on start,TestForkProcessIDReusefails. IfChatProcessWatchTakesLinesand the fire query allow one delivery pastoutput_max_events,TestFireChatProcessWatchesfails at the limit case.Not exercised