Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(coderd/database/dbtestutil): drive every guarded writer to a rej…
…ection

The completeness test proved an override exists per writer query but
not that it rejects; only two of the thirteen were ever driven to the
guard error. A table now calls each writer on the root handle and the
completeness test requires the table to name every override, so a
future override that drops the check fails a test instead of passing
silently. A nested InTx case covers the one branch that shares the
outer allocation set.

Also widen the writer query pattern to ONLY and MERGE INTO forms, and
rename require, mark, covered and rejections to say what they hold.
  • Loading branch information
mafredri committed Sep 11, 2026
commit 6326a0aade8b8eeeb79a462d38663eaca4190236
46 changes: 24 additions & 22 deletions coderd/database/dbtestutil/chatwriteguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"slices"
"sync"

"github.com/google/uuid"
Expand Down Expand Up @@ -42,9 +43,7 @@ func (r *chatWriteRecorder) add(method string, chatID uuid.UUID) error {
func (r *chatWriteRecorder) list() []chatWriteRejection {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]chatWriteRejection, len(r.rejections))
copy(out, r.rejections)
return out
return slices.Clone(r.rejections)
}

// reset discards the recorded rejections.
Expand Down Expand Up @@ -94,17 +93,20 @@ func (g *chatWriteGuard) InTx(fn func(database.Store) error, opts *database.TxOp
}, opts)
}

func (g *chatWriteGuard) mark(chatID uuid.UUID) {
// markAllocated records that this transaction allocated a snapshot for
// chatID. The root handle has no allocation set, so the call is a no-op
// there and its guarded writes stay rejected.
func (g *chatWriteGuard) markAllocated(chatID uuid.UUID) {
g.mu.Lock()
defer g.mu.Unlock()
if g.allocated != nil {
g.allocated[chatID] = struct{}{}
}
}

// require returns the rejection error unless this transaction allocated a
// snapshot for chatID.
func (g *chatWriteGuard) require(method string, chatID uuid.UUID) error {
// requireSnapshot returns the rejection error unless this transaction
// allocated a snapshot for chatID.
func (g *chatWriteGuard) requireSnapshot(method string, chatID uuid.UUID) error {
g.mu.Lock()
_, ok := g.allocated[chatID]
g.mu.Unlock()
Expand All @@ -117,21 +119,21 @@ func (g *chatWriteGuard) require(method string, chatID uuid.UUID) error {
func (g *chatWriteGuard) InsertChat(ctx context.Context, arg database.InsertChatParams) (database.Chat, error) {
chat, err := g.Store.InsertChat(ctx, arg)
if err == nil {
g.mark(chat.ID)
g.markAllocated(chat.ID)
}
return chat, err
}

func (g *chatWriteGuard) LockChatAndBumpSnapshotVersion(ctx context.Context, id uuid.UUID) (database.Chat, error) {
chat, err := g.Store.LockChatAndBumpSnapshotVersion(ctx, id)
if err == nil {
g.mark(id)
g.markAllocated(id)
}
return chat, err
}

func (g *chatWriteGuard) InsertChatMessages(ctx context.Context, arg database.InsertChatMessagesParams) ([]database.InsertChatMessagesRow, error) {
if err := g.require("InsertChatMessages", arg.ChatID); err != nil {
if err := g.requireSnapshot("InsertChatMessages", arg.ChatID); err != nil {
return nil, err
}
return g.Store.InsertChatMessages(ctx, arg)
Expand All @@ -149,85 +151,85 @@ func (g *chatWriteGuard) SoftDeleteChatMessageByID(ctx context.Context, id int64
return xerrors.Errorf("resolve chat for message %d: %w", id, err)
}
if err == nil {
if err := g.require("SoftDeleteChatMessageByID", msg.ChatID); err != nil {
if err := g.requireSnapshot("SoftDeleteChatMessageByID", msg.ChatID); err != nil {
return err
}
}
return g.Store.SoftDeleteChatMessageByID(ctx, id)
}

func (g *chatWriteGuard) SoftDeleteChatMessagesAfterID(ctx context.Context, arg database.SoftDeleteChatMessagesAfterIDParams) error {
if err := g.require("SoftDeleteChatMessagesAfterID", arg.ChatID); err != nil {
if err := g.requireSnapshot("SoftDeleteChatMessagesAfterID", arg.ChatID); err != nil {
return err
}
return g.Store.SoftDeleteChatMessagesAfterID(ctx, arg)
}

func (g *chatWriteGuard) SoftDeleteContextFileMessages(ctx context.Context, chatID uuid.UUID) error {
if err := g.require("SoftDeleteContextFileMessages", chatID); err != nil {
if err := g.requireSnapshot("SoftDeleteContextFileMessages", chatID); err != nil {
return err
}
return g.Store.SoftDeleteContextFileMessages(ctx, chatID)
}

func (g *chatWriteGuard) InsertChatQueuedMessage(ctx context.Context, arg database.InsertChatQueuedMessageParams) (database.ChatQueuedMessage, error) {
if err := g.require("InsertChatQueuedMessage", arg.ChatID); err != nil {
if err := g.requireSnapshot("InsertChatQueuedMessage", arg.ChatID); err != nil {
return database.ChatQueuedMessage{}, err
}
return g.Store.InsertChatQueuedMessage(ctx, arg)
}

func (g *chatWriteGuard) InsertChatQueuedMessageWithCreator(ctx context.Context, arg database.InsertChatQueuedMessageWithCreatorParams) (database.ChatQueuedMessage, error) {
if err := g.require("InsertChatQueuedMessageWithCreator", arg.ChatID); err != nil {
if err := g.requireSnapshot("InsertChatQueuedMessageWithCreator", arg.ChatID); err != nil {
return database.ChatQueuedMessage{}, err
}
return g.Store.InsertChatQueuedMessageWithCreator(ctx, arg)
}

func (g *chatWriteGuard) DeleteChatQueuedMessage(ctx context.Context, arg database.DeleteChatQueuedMessageParams) error {
if err := g.require("DeleteChatQueuedMessage", arg.ChatID); err != nil {
if err := g.requireSnapshot("DeleteChatQueuedMessage", arg.ChatID); err != nil {
return err
}
return g.Store.DeleteChatQueuedMessage(ctx, arg)
}

func (g *chatWriteGuard) DeleteChatQueuedMessageReturningCount(ctx context.Context, arg database.DeleteChatQueuedMessageReturningCountParams) (int64, error) {
if err := g.require("DeleteChatQueuedMessageReturningCount", arg.ChatID); err != nil {
if err := g.requireSnapshot("DeleteChatQueuedMessageReturningCount", arg.ChatID); err != nil {
return 0, err
}
return g.Store.DeleteChatQueuedMessageReturningCount(ctx, arg)
}

func (g *chatWriteGuard) DeleteAllChatQueuedMessages(ctx context.Context, chatID uuid.UUID) error {
if err := g.require("DeleteAllChatQueuedMessages", chatID); err != nil {
if err := g.requireSnapshot("DeleteAllChatQueuedMessages", chatID); err != nil {
return err
}
return g.Store.DeleteAllChatQueuedMessages(ctx, chatID)
}

func (g *chatWriteGuard) DeleteAllChatQueuedMessagesReturningCount(ctx context.Context, chatID uuid.UUID) (int64, error) {
if err := g.require("DeleteAllChatQueuedMessagesReturningCount", chatID); err != nil {
if err := g.requireSnapshot("DeleteAllChatQueuedMessagesReturningCount", chatID); err != nil {
return 0, err
}
return g.Store.DeleteAllChatQueuedMessagesReturningCount(ctx, chatID)
}

func (g *chatWriteGuard) PopNextQueuedMessage(ctx context.Context, chatID uuid.UUID) (database.ChatQueuedMessage, error) {
if err := g.require("PopNextQueuedMessage", chatID); err != nil {
if err := g.requireSnapshot("PopNextQueuedMessage", chatID); err != nil {
return database.ChatQueuedMessage{}, err
}
return g.Store.PopNextQueuedMessage(ctx, chatID)
}

func (g *chatWriteGuard) ReorderChatQueuedMessageToFront(ctx context.Context, arg database.ReorderChatQueuedMessageToFrontParams) (int64, error) {
if err := g.require("ReorderChatQueuedMessageToFront", arg.ChatID); err != nil {
if err := g.requireSnapshot("ReorderChatQueuedMessageToFront", arg.ChatID); err != nil {
return 0, err
}
return g.Store.ReorderChatQueuedMessageToFront(ctx, arg)
}

func (g *chatWriteGuard) ReorderChatQueuedMessageToHead(ctx context.Context, arg database.ReorderChatQueuedMessageToHeadParams) (int64, error) {
if err := g.require("ReorderChatQueuedMessageToHead", arg.ChatID); err != nil {
if err := g.requireSnapshot("ReorderChatQueuedMessageToHead", arg.ChatID); err != nil {
return 0, err
}
return g.Store.ReorderChatQueuedMessageToHead(ctx, arg)
Expand Down
6 changes: 3 additions & 3 deletions coderd/database/dbtestutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
}
// Unit tests should not retry serial transaction failures.
db = database.New(sqlDB, database.WithSerialRetryCount(1))
rejections := &chatWriteRecorder{}
db = newChatWriteGuard(db, rejections)
recorder := &chatWriteRecorder{}
db = newChatWriteGuard(db, recorder)
t.Cleanup(func() {
for _, r := range rejections.list() {
for _, r := range recorder.list() {
t.Errorf("chat write guard rejected %s for chat %s outside a chat state transition; "+

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 [CRF-4] The cleanup failure message names the problem and where to look but drops the fix, and this is the one message the developer actually reads. (Leorio P3, Gon Note)

The cleanup path exists precisely for the case where the caller dropped the returned error. In that case the developer never sees the add message; the cleanup line is all they get. So the remedy is missing exactly when it is the only message on screen.

The returned error from add prescribes the treatment ("write history through chatstate.ChatMachine.Update or CreateChat, or dbgen in tests"); this cleanup message says "the call site is the assertion that received this error, otherwise search for callers of ..." and stops. That first clause is also wrong for this path: if an assertion had received the error, the test would have failed there and this line would not fire. When it fires, no assertion received the error, so the clause sends the reader looking for something that does not exist. Give the cleanup message the same content the returned error carries: name the method, the chat, and dbgen.ChatMessage/dbgen.ChatQueuedMessage (or chatstate.ChatMachine.Update/CreateChat) as the fix. Gon separately noted the two messages are maintained independently and will drift.

πŸ€–

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping the text. The cleanup sentence was proposed during implementation and accepted by the orchestrator together with the decision not to capture call stacks. Its second clause, "otherwise search for callers of ", is the instruction for the dropped-error path; the first clause covers the case where the error was asserted on and the cleanup still runs (the assertion failed the test first, and the cleanup line points back at it). The fix text lives in the returned error so the two messages carry different information rather than duplicate it. Leaving open for the human reviewer.

πŸ€– Posted using /amend-review skill via Coder Agents.

"the call site is the assertion that received this error, otherwise search for callers of %s",
r.method, r.chatID, r.method)
Expand Down
Loading
Loading