Repository navigation
feat(coderd): add chatd automation admission seam and queue promotion guard - #30096
Conversation
… guard Adds the backend seam a later Publish operation needs to admit automation messages inside chatd's own transactions, and a guard that drops stale automation rows instead of promoting them. Nothing in production calls the seam yet; ordinary chats behave as before. - AdmitInTx: chatstate.AdmitFunc on CreateChatInput and SendMessageInput, forwarded from chatd.CreateOptions and SendMessageOptions. It runs after the chat row is inserted or locked and before the message is written; its provenance is validated and stamped on the message. An error rolls everything back. - Provenance: chatstate.Message.Automation is written to chat_queued_messages (automation_id, input_id, queue_generation) and chat_messages (automation_id, input_id), and promotions copy it from the queued row to the history row. - Promotion guard: chatstate.LockAutomations locks automations FOR UPDATE in ascending id order as chatd (new GetChatAutomationsByIDsForUpdate query and chatd read permission). FinishTurn, FinishInterruption, and SendMessage from E1 delete heads whose automation is missing, disabled, or on another queue generation and promote the next passing head; an emptied queue lands in waiting. Explicit promotion guards only the target, deletes it on failure, changes nothing else, and chatd reports it as not found. - ChatMachine.Update retries the whole transaction (3 attempts) on a deadlock abort, only when the attempt took automation locks. Adds database.IsDeadlockError. - The transition matrix gains the outputs the guard can produce, and ARCHITECTURE.md gets TODO (CODAGT-1208) notes for the human author.
…d tighten comments Count initial user messages once instead of checking twice with a duplicated error, and drop comments that referenced unimplemented follow-up work or overstated where the promotion guard applies.
Three negative controls survived the branch's guard tests: - Dropping the guard's enabled check: the Disabled reason also bumps queue_generation, so the generation check hid it. Add a DisabledSameGeneration reason. - Deleting every stale row instead of only the rejected heads: no stale row sat behind the promoted head. Queue one there and assert it stays. - Taking automation locks out of order: Update's deadlock retry hid the aborts. Count real deadlock aborts below Update in the concurrency test and require zero.
…a-rbac-audit' into tk/codagt-1208-automations-seam-guard
|
@codex review |
Codex Review SummaryThis 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: 58cb17d916
ℹ️ 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".
…n guard Replace the CODAGT-1208 TODO notes in ARCHITECTURE.md with prose: the AdmitInTx callback on Create and SendMessage, provenance on queued and history rows, the queue promotion guard on head and explicit promotion, the chat-then-automation lock order, and the deadlock retry in ChatMachine.Update. The state diagram gains the guard's new outcomes. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
|
@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". |
…o tk/codagt-1208-automations-seam-guard Brings in main's chatd tracing work (#29769-#29771) through the updated parent. Two chatd conflicts were resolved by hand: - chatstate/transitions.go: PromoteQueuedMessageResult keeps both the seam's Rejected flag and main's PromotedQueuedAt. PromotedQueuedAt comes from the head chosen by the promotion guard (nextPromotableQueueHead), so dropped stale rows never count as queue wait. It stays zero for a rejected promotion and for the sendMessageE1 case where the guard dropped every older row and the new message went straight into history. - chatd.go PromoteQueued (textual merge, compile break): main dropped refreshedOK because the chat is always refreshed on success. The seam's deadlock-retry reset now resets promotedQueuedAt instead, and the rejected path still returns ErrQueuedMessageNotFound before the publish and the queue-wait metric. Generated files were regenerated with make gen (no diff). --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
|
sync with main d9c13dc: hand-resolved chatstate/transitions.go (guard + PromotedQueuedAt) and a chatd.go PromoteQueued compile break (refreshedOK removed by main); resolution note in the merge commit
Generated with |
|
@codex review Scope: only the conflict resolution in merge commit f907cf8 (sync with main d9c13dc). Please do not re-review main's code or the unchanged stack code.
Generated with |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
…o tk/codagt-1208-automations-seam-guard No conflicts. make gen produced no diff. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
|
sync with main 35abc22: no content conflicts; make gen produced no diff
Generated with |
ibetitsmike
left a comment
There was a problem hiding this comment.
Xum (an AI coding agent) prepared and posted this review on behalf of Mike. Each finding came from automated review passes and was re-verified against the code; please push back on anything that looks wrong.
The admission seam, provenance stamping, and promotion guard match CODAGT-1208 and the RFC: all four promotion sites (sendMessageE1, PromoteQueuedMessage, FinishTurn, FinishInterruption) go through the guard, guard deletes bump queue_version through the existing trigger so streams refresh the queue, and the Update retry closures reset their results. One small contract nit on the AdmitFunc doc.
Verdict: no blocking issues
Severity: P0 0, P1 0, P2 0, P3 1
Review coverage
- code-review Standards: 0 findings (ARCHITECTURE.md prose already handled in the resolved Codex thread)
- code-review Spec: 0 findings; adjudicator added 1 P3 against the RFC lock-order paragraph
- frontend-review: skipped, no files under site/
- coder-frontend-review: skipped, no files under site/
| // The callback must use only the supplied transaction store, must not | ||
| // make network calls, and must lock automations only through | ||
| // [LockAutomations] so every automation lock follows one order: the chat | ||
| // row first, then automations in ascending id order. |
There was a problem hiding this comment.
P3: AdmitFunc lock-order contract is not sufficient on its own
Locking only through LockAutomations does not give one order across calls: on the E1 path, SendMessage runs the callback (transitions.go:532) before sendMessageE1 calls nextPromotableQueueHead, which locks the queue's automations in a second LockAutomations call (automation.go:206). A callback that locks only its own automation X, on a chat whose queue head belongs to automation A < X, takes X then A, which is the "acceptance whose target chat has a queue head from another automation" case the RFC says must be locked in ascending order. The Publish callback later in the stack handles this by locking its automation together with the queued rows' automations, but nothing in this contract tells a future caller to do that, and the Update deadlock retry only papers over it.
Fix: State in the doc that a callback which locks automations must pass its own automation and every queued row's automation to a single LockAutomations call.
There was a problem hiding this comment.
Fixed in 6c8276a: the AdmitFunc doc and the Lock order paragraph in ARCHITECTURE.md now say the order holds only within one LockAutomations call, and that a callback which locks automations must pass its own automation and every queued row's automation in a single call. The production callback (admitAutomation, #30127) already does this.
Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high
…o tk/codagt-1208-automations-seam-guard Resolved coderd/x/chatd/ARCHITECTURE.md: kept this layer's guard sentence for PromoteQueuedMessage and main's unowned Interrupt sentence, and noted that the FinishInterruption main now applies for an unowned running chat (Interrupt and SendMessage with interrupt) promotes through the queue promotion guard. transitions.go merged cleanly: main's finishUnownedInterruption calls FinishInterruption, which already picks the head with nextPromotableQueueHead. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
… the queue guard Own the chat before Interrupt in the FinishInterruption guard test and cover the inline unowned Interrupt and SendMessage(interrupt) paths. SendMessage(interrupt) from R1 no longer reports the new message as queued when the guard drops every older row and it is promoted. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
…admission callbacks LockAutomations orders locks only within one call, and SendMessage from E1 locks the queue's automations after the callback returns. A callback that locks automations must lock its own and every queued row's automation in a single call. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
|
sync with main d11809f: resolved ARCHITECTURE.md (guard + main's unowned interrupt); main's inline FinishInterruption for unowned running chats now has guard coverage, and SendMessage(interrupt) from R1 no longer reports a promoted message as queued (036b462); stated the single LockAutomations call rule for admission callbacks (Mike's review, 6c8276a)
Generated with |
|
@codex review Scope: only the merge resolution and follow-ups for main d11809f (capacity enforcement), commits 6b30c37, 036b462 and 6c8276a. Please do not re-review main's code or unchanged stack code.
Generated with |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
ibetitsmike
left a comment
There was a problem hiding this comment.
Xum re-reviewed this PR on behalf of @ibetitsmike using GPT-6.1 Sol (high) Exec subagents.
Reviewed the full pinned layer, including the corrected lock contract and unowned-interrupt guard coverage; the prior lock-contract and architecture-TODO findings are fixed at this head. New findings: standards 0, spec 0; frontend is not applicable. The existing lock-contract thread remains open administratively, and the corrected contract still needs propagation into the upper stack pins. Focused chatstate, chatd, SQL-insert and authorization tests passed, and the saved layer diff matches the pinned head with a clean whitespace check.
…o tk/codagt-1208-automations-seam-guard No conflicts. make gen produced no diff. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
|
sync with main a328bee: merged #30085 (main a328bee with chat projects; no conflicts in this layer)
Generated with |
ibetitsmike
left a comment
There was a problem hiding this comment.
Xum validated this PR and approved it on behalf of @ibetitsmike.
Validated head 4bfe28f6c29e. The reviewed admission, ordered-lock, queue-promotion and provenance contracts remain intact after the restack. Fresh guard/concurrency/transition-matrix, insertion-order and authorization tests passed. Current required CI passed; accepted stale-queue and archived-cleanup follow-ups remain unchanged. Approval is limited to this layer.
…o tk/codagt-1208-automations-seam-guard Resolved coderd/x/chatd/chatstate/transitions.go: main renamed sendMessageInterruptRunning to sendMessageInterrupt, made it also handle unowned I0/I1 chats, set FinishedInterruption, and cleared QueuedMessage only when starting from R0 or I0. Kept main's name, doc comment and FinishedInterruption, and kept this layer's check that clears QueuedMessage when the promoted message is the new message itself. That check covers R0 and I0, and also R1 and I1 when the queue promotion guard deletes every older row, which main's from-state check would report as still queued. The from parameter is no longer needed. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
…hats Main now finishes the interruption inline when Interrupt or SendMessage(busy_behavior=interrupt) reaches an unowned chat in I0 or I1. TestQueuePromotionGuard_UnownedInterrupt now runs every case from both an unowned running chat and an unowned interrupting chat (interrupted, then abandoned), so the guard is exercised on that path too. Without the promoted-message check in sendMessageInterrupt, the interrupting SendMessage cases fail: the promoted new message is reported as queued. ARCHITECTURE.md now names interrupting chats wherever it describes the inline FinishInterruption and its guard, and states when the response reports m as not queued. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
|
merged #30085 (main 15382eb); resolved sendMessageInterrupt against main's inline finish for unowned interrupting chats, kept the guard-aware queued check; guard tests now also start from an unowned interrupting chat
Generated with |
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
ibetitsmike
left a comment
There was a problem hiding this comment.
Xum validated this PR and approved it on behalf of @ibetitsmike.
Head 914196e0121438f037acc3cef84d81a5ad0b0b63 and its base, body, and complete layer diff are unchanged from the completed source validation. Prior independent Standards and Spec review dispositions, including accepted deferrals, remain valid; this layer has no frontend changes.
The pinned uncached validation passed 18 runtime roots, two SQL ordering roots, authorization cases, and seven focused race roots repeated three times. Production-behavior controls failed when the guard or interrupt-route wiring was removed, then the actual source passed. These checks were executed in the prior validation cycle, not rerun or relabeled fresh now.
The previous required/race CI failure was held, not waived. The fresh exact-head approval gate now has required=SUCCESS and all observed checks passed or skipped. This approves only this layer, not the whole stack. No new full local suite, lint/generation, frontend, or remote UAT was run.
…o tk/codagt-1208-automations-seam-guard No conflicts. make gen produced no diff. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
Generated with |
…8-automations-seam-guard Brings main ff158c0 into this layer. No conflicts. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
Adds the backend-only seam that lets a later
Publishoperation admit an automation message inside chatd's existing create-chat and send-message transactions, and the guard that drops stale automation input when the queue promotes it. Only Go test callbacks use the seam in this PR, so ordinary chat behavior does not change.Closes CODAGT-1208
Part of CODAGT-1135
ARCHITECTURE.md prose was drafted by an agent at the user's request; the user will review it.
Changes
chatstate.AdmitFuncis a nil-defaultAdmitInTxfield onchatd.CreateOptions,chatd.SendMessageOptions,chatstate.CreateChatInput, andchatstate.SendMessageInput.CreateChatruns it after inserting the chat and before the initial history, and requires exactly one initial user message.SendMessageruns it on the locked chat before any write. The callback returns anAutomationProvenance(automation ID, input ID, queue generation), which is validated and stamped on the message. A callback error rolls back the whole transaction, so no chat, message, or queue row survives.Server.CreateChatandServer.SendMessagekeep their signatures.chatstate.Message.Automationis written to queued rows (automation_id,input_id,queue_generation) and history rows (automation_id,input_id).InsertChatMessagesandInsertChatQueuedMessageWithCreatorgain the matching parameters, andmessageFromQueuedRowcopies them on promotion.chatstate.LockAutomationsis the only way to lock automation rows: it locks themFOR UPDATEin ascending ID order, running as chatd. The chatd subject gains read onchat_automation. A queued automation row passes only if its automation exists, is enabled, and has the row'squeue_generation. Two helpers apply the check:nextPromotableQueueHeadservessendMessageE1,FinishTurn, andFinishInterruption. A head without an automation costs no extra query. Otherwise it locks every automation in the queue up front, deletes failing heads until one passes, and leaves rows behind that head alone. If the queue empties,FinishTurnandFinishInterruptionland inwaiting, as fromR0andI0.guardQueuedRowsserves explicitPromoteQueuedMessage. A failing row is deleted and the transition returnsRejectedwithout an error, so the delete commits.Server.PromoteQueuedthen returnsErrQueuedMessageNotFound(HTTP 404) without reordering, substituting a message, or changing status.DeliverSteerMessagesand the steer batches from chore(coderd): store a delivery mode on queued chat messages #30073/feat(coderd/x/chatd): deliver steer messages before each model call #30074) add oneguardQueuedRowscall each, whichever PR lands second.database.IsDeadlockErroris new.ChatMachine.Updatereruns the transaction, at most 3 attempts with a fresh publish buffer each, only when an attempt that took automation locks hit a deadlock abort. Attempts without automation locks, and nested Updates, never retry. TheSendMessageandPromoteQueuedclosures reset their results so a rerun is safe.R1 -> FinishTurn -> WandE1 -> SendMessage -> R0.ARCHITECTURE.md. Describes the admission seam, provenance, the queue promotion guard, the lock order, and the deadlock retry, and adds the guard's outcomes to the state diagram.Validation
LockAutomationsworks through dbauthz.Updateretries only when an attempt that took automation locks hits a deadlock.Generated with
xum• Model:anthropic:claude-opus-5-5• Thinking:high