Repository navigation
Conversation
2ccb0e5 to
d304912
Compare
7636cf2 to
8a281a2
Compare
…ommit write Every hot chat write in dbauthz (UpdateChatExecutionState, InsertChatMessages, IncrementChatGenerationAttempt, UpdateChatRetryState, and the queue/heartbeat reads) pre-read the chat with the full chats_expanded join to authorize. Those reads run inside the transition callback while the chat row lock is held, so they lengthened every hold on top of the lock's own pre-lock pre-read. Follow the workspace RBAC-in-context pattern (#20662, #21047): cache the chat's RBAC object on the context and let chat-scoped dbauthz methods authorize against it, falling back to the fetch on any failure. The cached object carries only ID, owner, and org, which never change for a chat, and must not carry ACLs since sharing changes at runtime; a cached object for a different chat ID is ignored. ChatMachine.Update caches the object from the lock's own row on the transaction context, so every transition method's write authorizes without a read. The chat HTTP middleware and the worker runner cache it up front so the lock's authorization skips its read too. Every commit write (UpdateChatExecutionState, BumpChatSnapshotVersion, UpdateChatRetryState, IncrementChatGenerationAttempt) now returns the committed chat row together with has_queued and ownership_stale, so ChatMachine.Update publishes from that result instead of reading the chat again under the lock, and UpdateReturning hands the same row to callers. The commit write is the last statement of a transition that can change the row, the queue, or the heartbeat, so the result is exact; Acquire writes its heartbeat first so the lease it evaluates is fresh. Callers that reloaded the chat after a transition use the returned row instead. For callbacks whose only write is CommitStep, the reload ran before Update's bump-only write and returned the pre-bump versions, so the runner hint routed from commitGenerationStep was always dropped and applySessionStartResponse continued with a stale history fence. SendMessage's first-turn fallback title is written after the commit write, so that path returns the title write's row. Through dbauthz, a single transition now reads the chat once (the lock's pre-lock authorization) instead of once per write, and not at all when the caller cached the object or the commit write returned the row.
8a281a2 to
e43ac74
Compare
|
/coder-agents-review |
|
Chat: Review posted | View chat Review history
deep-review v0.13.0 | Round 1 | Last posted: Round 1, 28 findings (2 P2, 9 P3, 9 P4, 8 Nit), COMMENT. Review Finding inventoryFindings
Contested and acknowledgedLaw analysisNot run. Effective additions 687 (threshold 1000). Round logRound 1Full panel (round 1). Netero first pass: 1 P2, 3 P3, 1 P4, 1 Nit, 1 Note, no P0, panel proceeded. Panel: ging-go, killua, knuckle, kurapika, melody, ryosuke, takumi (specialists), pariston, mafuuu (core), bisky, gon, leorio (pool, Bisky required, Gon/Leorio floor), knov (wildcard). Panel size 13 exceeds the 9-12 target because seven specialists (ging-go, killua, knuckle, kurapika, melody, ryosuke, takumi) are triggered. Gon's comment-style P2s downgraded to Nit (convention-only, no behavior consequence). Totals posted: 2 P2, 9 P3, 9 P4, 8 Nit. Reviewed against 0df197b..e43ac74. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This PR caches the chat RBAC object on the context so dbauthz skips the chat pre-read, and makes the four commit writes return the committed row with has_queued and ownership_stale so Update publishes without re-reading. 2 P2, 9 P3, 9 P4, 8 Nit.
The commit message for e43ac74 contradicts itself in two places: it says a transition reads the chat "once (the lock's pre-lock authorization)" and then "not at all when ... the commit write returned the row", and it calls the commit write the last statement "that can change the row" while the next paragraph says SendMessage writes the title after it. Delete "or the commit write returned the row" and name the queue, the heartbeat, and the fields chat:update carries instead of "the row".
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/x/chatd/hook_server.go:85and:100:handleAPIDispatchErrorreads the chat withGetChatByIDbefore and afterFinishError, two reads under the transition lock that the other handlers no longer pay.coderd/x/chatd/worker.go:276and:340:acquireCandidatecallsIsChatHeartbeatStalewith the outerctx, which has no cached RBAC object, so dbauthz reads the chat under the row lock and under the capacity advisory lock.coderd/x/chatd/chatstate/machine.go:16: commit writes use the constantHeartbeatStaleSeconds(30) while acquisition uses the configurableopts.HeartbeatStaleSeconds, so a deployment that changes the option gets ownership hints on a different threshold than acquisition.coderd/x/chatd/chatd.go:1564: the "Row already locked by ChatMachine.Update" comments now sit inUpdateReturningcallbacks.
Notes:
coderd/x/chatd/ARCHITECTURE.md:727: the change is recorded as a TODO, and the TODO says a transition touches the chat row only through its lock and its commit write, which theSendMessagetitle write after the commit write contradicts.coderd/database/queries/chats.sql:2930: the claim that the commit write is the last statement touching the heartbeat is enforced only by statement order inAcquire; nothing fails if a later transition upserts the heartbeat after its commit write.coderd/database/dbauthz/dbauthz.go:186: a user who reaches a chat only through a sharing ACL now pays a denied fast-path evaluation and a debug log line before theGetChatByIDfallback on every chat call.coderd/database/dbauthz/chat_rbac_context.go:13: the fast path is correct only while no query changeschats.owner_idorchats.organization_id; no test or check enforces that.
coderd/x/chatd/generation.go:430
P2 [CRF-1] syncMemoryIndex still reloads the chat inside the callback, before Update's bump-only write, so input.HistoryVersion gets the pre-bump value. (Netero)
CommitStep runs no commit write, so BumpChatSnapshotVersion advances history_version after this read. The next loadChatForTask with requireHistory: true then fails the fence (tasks.go:669) and returns errTaskExpectedExit, which I infer ends the generation task after every memory index commit until the runner dispatches a new one. Use machine.UpdateReturning and drop the reload, as the PR already does for applySessionStartResponse.
🤖
coderd/x/chatd/hooks_internal_test.go:95
P3 [CRF-2] The two defects the PR description says it fixes (the session-start history fence and the commitGenerationStep runner hint) have no regression test. (Netero)
TestApplySessionStartResponse asserts only the inserted messages and passes with the old pre-bump reload, and no changed test exercises commitGenerationStep. Assert that the returned chat's HistoryVersion and SnapshotVersion equal a fresh GetChatByID after applySessionStartResponse, and the same for the chat commitGenerationStep routes.
🤖
coderd/database/queries/chats.sql:3333
P3 [CRF-4] The IsChatHeartbeatStale comment says "chatstate calls this", but this PR removed chatstate's only caller (ownershipStaleOrMissing). (Netero)
The remaining callers are worker.go:277 and :343. Name the worker acquisition path instead.
🤖
coderd/x/chatd/chatstate/transitions.go:227
Nit [CRF-28] refreshed in insertChat now holds a BumpChatSnapshotVersionRow from the commit write, not a reload. (Gon)
Rename to bumped, as dbgen.go:151 does.
🤖
🤖 This review was automatically generated with Coder Agents.
| if err := q.authorizeContext(ctx, action, rbacObj); err == nil { | ||
| return nil | ||
| } | ||
| q.log.Debug(ctx, "fast path authorization failed for chat, using slow path", |
There was a problem hiding this comment.
P3 [CRF-3] The fallback branch of authorizeChatByID, taken when the cached object is denied, has no test coverage. (Netero, Mafuuu)
This branch keeps ACL-shared users working now that ExtractChatParam caches an ACL-free object for every chat request; coverage shows dbauthz.go:190.3,191.30 at count 0. TestChatWriteAuthorization_FastPath uses AlwaysReturn(nil), so it cannot reach it. Add a subtest whose authorizer denies the cached object and allows the fetched one, and assert GetChatByID is called and the write succeeds.
🤖
| AND h.runner_id = chats_expanded.runner_id | ||
| AND h.heartbeat_at > NOW() - (INTERVAL '1 second' * @stale_seconds::int) | ||
| ) | ||
| )::boolean AS ownership_stale |
There was a problem hiding this comment.
P4 [CRF-5] The has_queued/ownership_stale select block is copied into four queries, and its heartbeat predicate is a fifth copy of IsChatHeartbeatStale. (Netero)
A change to the lease rule has to be made in five places, and a missed copy makes Update publish or suppress chat:ownership hints depending on which commit write ran. A SQL function such as chat_ownership_stale(chat_id, runner_id, worker_id, stale_seconds) called from all five sites keeps one definition.
🤖
| var result chatstate.InterruptResult | ||
| machine := p.newChatMachine(chat.ID) | ||
| err := machine.Update(ctx, func(tx *chatstate.Tx, store database.Store) error { | ||
| refreshed, err := machine.UpdateReturning(ctx, func(tx *chatstate.Tx, _ database.Store) error { |
There was a problem hiding this comment.
Nit [CRF-6] InterruptChat and ReconcileInvalidStateChat (line 2628) still call the result refreshed, but it is now the commit write's row. (Netero)
The other converted call sites use committed or committedChat.
🤖
| }) | ||
| } | ||
|
|
||
| func TestChatWriteAuthorization_FastPath(t *testing.T) { |
There was a problem hiding this comment.
P2 [CRF-8] No test proves the authorizeChatByID fast path authorizes anything. (Bisky)
Reproduced: replacing q.authorizeContext(ctx, action, rbacObj) at dbauthz.go:187 with error(nil) leaves TestChatWriteAuthorization_FastPath, TestWithChatRBAC, TestMethodTestSuite/TestChats, and TestUpdateThroughDBAuthz* passing, because this test uses AlwaysReturn(nil) and the round-trip tests run as AsChatd. A regression that skips authorization for every caller with a cached object (HTTP middleware, runner, every Update) would ship with green tests. Add a subtest with a member who has no access to the chat, cache CacheableChatRBAC(chat), call UpdateChatExecutionState, and assert dbauthz.IsNotAuthorizedError(err); in the existing subtest, assert the recorded actor, object, and policy.ActionUpdate.
🤖
| // ACL-dependent grant is never served from the cache. | ||
| // | ||
| // Slow path: fetch the chat and authorize against it. | ||
| func (q *querier) authorizeChatByID(ctx context.Context, chatID uuid.UUID, action policy.Action) error { |
There was a problem hiding this comment.
P3 [CRF-9] The queue and message dbauthz methods that transitions call on tx.ctx still read the chat before authorizing, so the description's "reads the chat once ... instead of once per write" is false for Acquire, queued sends, and FinishTurn with a queued head. (Melody, Knov, Netero)
Reproduced with a scratch test modeled on TestUpdateThroughDBAuthzWithCallerCachedRBACReadsNothing: a queued tx.SendMessage issued 1 GetChatByID, tx.FinishTurn with a queued head issued 3, and Acquire issued 1, against the 0 the existing test asserts for an empty-queue FinishTurn. The methods include UpsertChatHeartbeat, InsertChatQueuedMessageWithCreator, GetChatQueuedMessageHead, GetChatQueuedMessagesByPosition, DeleteChatQueuedMessageReturningCount, DeleteAllChatQueuedMessagesReturningCount, ReorderChatQueuedMessageToHead, GetChatQueuedMessageByID, SoftDeleteChatMessageByID, SoftDeleteChatMessagesAfterID, GetChatMessageByID, GetChatMessagesByChatID, and UpdateChatTitleByID. Route them through authorizeChatByID and extend the round-trip test to a queued FinishTurn, or narrow the database-impact claim to the transitions it covers.
🤖
| // the transaction function succeeds. If the transaction rolls back, | ||
| // the deferred Discard suppresses every buffered publication so | ||
| // subscribers never see uncommitted state. | ||
| // versions. The commit write returns the committed row together with |
There was a problem hiding this comment.
Nit [CRF-24] The new clause about the commit write returning the flags repeats commitResult's doc (line 98); the same applies to the committed field comment (line 74), the recordCommit comment (line 106), and "The lock returned the authoritative row." (line 288). (Gon)
Keep Update's doc to what is published and when, delete the recordCommit comment, and start the line 288 comment with its reason.
🤖
| } | ||
|
|
||
| ctx = context.WithValue(ctx, chatParamContextKey{}, chat) | ||
| // The request already paid for this read; cache the chat's RBAC |
There was a problem hiding this comment.
Nit [CRF-25] "The request already paid for this read" is a metaphor. (Gon)
Rewrite: "Lets dbauthz authorize this request's chat queries without reading the chat again."
🤖
| updated_at = NOW() | ||
| WHERE id = @id::uuid | ||
| RETURNING generation_attempt; | ||
| -- The commit write of RecordGenerationAttempt: advances snapshot_version, |
There was a problem hiding this comment.
Nit [CRF-26] The rewritten IncrementChatGenerationAttempt doc repeats the SET list and drops the (see LockChatForTransition) pointer that UpdateChatRetryState keeps. (Gon)
Restore the pointer to the single-UPDATE rule.
🤖
| // can cache its RBAC object up front so even the lock authorizes without a | ||
| // read: the transition then touches the chat row only through the lock and | ||
| // its commit write. | ||
| func TestUpdateThroughDBAuthzWithCallerCachedRBACReadsNothing(t *testing.T) { |
There was a problem hiding this comment.
Nit [CRF-27] TestUpdateThroughDBAuthzWithCallerCachedRBACReadsNothing asserts only that GetChatByID is zero; it still expects one LockChatForTransition. (Gon)
Rename to TestUpdateThroughDBAuthzWithCallerCachedRBACSkipsGetChatByID.
🤖
| buffer := NewPublishBuffer(m.publisher) | ||
| defer buffer.Discard() | ||
|
|
||
| var committed database.Chat |
There was a problem hiding this comment.
Nit [CRF-29] updateOnce uses committed for a local database.Chat and reads tx.committed (a *commitResult) two lines before assigning one to the other. (Gon)
Rename the local to committedChat, the name chatd.go uses for the same value.
🤖
Two round-trip sources remained under the transition lock after #30267: every hot chat write in dbauthz (
UpdateChatExecutionState,InsertChatMessages,IncrementChatGenerationAttempt,UpdateChatRetryState, the queue and heartbeat reads) pre-read the chat through thechats_expandedjoin to authorize, andUpdatere-read the chat, queue count, and heartbeat after the callback to publish. Following the workspace RBAC-in-context pattern (#20662, #21047), the chat's RBAC object (ID, owner, org; never ACLs, which change at runtime) is cached on the context:Updatecaches it from the lock's own row, and the chat HTTP middleware and the worker runner cache it up front. Every commit write now returns the committed row plushas_queuedandownership_stale, soUpdatepublishes from that result andUpdateReturninghands the same row to callers, replacing the post-writeGetChatByIDreloads in the handlers, task starters, and generation loop.Database impact: through dbauthz a single transition now reads the chat once (the lock's pre-lock authorization) instead of once per write, and not at all when the caller cached the object; the three post-callback reads under the lock are gone. It also fixes two defects #30267 has on its own: the runner hint routed from
commitGenerationStepand the session-start history fence both used a reload taken beforeUpdate's bump-only write.Combined with the other PRs: based on #30267; with #30269 the acquisition pre-check feeds the cache (
CacheableChatRBACFor,owner_id/organization_idonGetChatTransitionState) andUpdateReturninglocks throughlockChat, and with #30270 the finish branch incommitGenerationStepreduces tofinished = true; whichever merges second makes those edits.Depends on #30267.
Series: #30264 harness jitter, #30265 transition metrics, #30266 lock-free stream reads, #30267 single-write transitions, #30268 commit-write result and RBAC cache, #30269 non-blocking acquisition, #30270 finish turn in the step commit.
Generated by Coder Agents on behalf of @cstyan.