Repository navigation
feat: use the state machine in chatd http endpoints - #25923
Conversation
Docs preview📖 View docs preview for |
ffbb99e to
f20c569
Compare
5d6ca0a to
723e393
Compare
| func TestNewRequiresPubsub(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| require.PanicsWithValue(t, "chatd: Pubsub is nil", func() { | ||
| _ = New(Config{}) | ||
| }) | ||
| } | ||
|
|
There was a problem hiding this comment.
I'm less sure of the value of testing that a panic happens under specific circumstances, and more sure of the value of not having a panic at all. As long as it's abundantly obvious that you need a pubsub then we probably don't need this. So I would rather we had New(pb database.Pubsub, chatd.Config{}) or similar.
There was a problem hiding this comment.
changed to New(pb database.Pubsub, chatd.Config{})
|
|
||
| // ptrTo returns a pointer to the given value. Helper for the | ||
| // pointer-shaped UpdateChatRequest fields. | ||
| func ptrTo[T any](v T) *T { return &v } |
There was a problem hiding this comment.
github.com/coder/coder/v2/coderd/util/ptr.Ref
There was a problem hiding this comment.
changed to use ptr.Ref
| toolName string, | ||
| ) (toolCallID string) { | ||
| t.Helper() | ||
| sysCtx := dbauthz.AsSystemRestricted(ctx) //nolint:gocritic // Test fixture composes chatstate transitions outside a request. |
There was a problem hiding this comment.
This is a great opportunity to exercise the permission set of dbauthz.AsChatd (and everywhere else we reach for it in this file)
| // MaxQueueSize is the maximum number of queued user messages per chat. | ||
| const MaxQueueSize = 20 | ||
|
|
There was a problem hiding this comment.
Would it make more sense to pass this into the state machine?
There was a problem hiding this comment.
It's already in the state machine. take a look at MaxQueueSize in coderd/x/chatd/chatstate/transitions.go.
| if cfg.Pubsub == nil { | ||
| panic("chatd: Pubsub is nil") | ||
| } | ||
| ps := cfg.Pubsub |
There was a problem hiding this comment.
Making pubsub a required parameter would make this situation less panic-able.
There was a problem hiding this comment.
I agree, let's avoid runtime panics where we can and not hide required inputs in config. New(ps, config) seems like the right signature.
f20c569 to
dc8fd23
Compare
723e393 to
779754a
Compare
dc8fd23 to
ef9bcfc
Compare
779754a to
f2c32c2
Compare
Documentation CheckUpdates Needed
Automated review via Coder Agents |
mafredri
left a comment
There was a problem hiding this comment.
Nothing major to flag here, a nice improvement and LOC reduction 👍
| if opts.WorkspaceID.Valid { | ||
| workspaceAwareness = workspaceAttachedAwareness | ||
| } else { | ||
| workspaceAwareness = workspaceDetachedAwareness |
There was a problem hiding this comment.
Nit: This change seems kind of pointless?
|
|
||
| if !chat.RootChatID.Valid && !chat.ParentChatID.Valid { | ||
| chat.RootChatID = uuid.NullUUID{UUID: chat.ID, Valid: true} | ||
| initialMessages := make([]chatstate.Message, 0, 4) |
There was a problem hiding this comment.
Is this optimization truly worth introducing a value that has to be kept in sync with initial messages?
| } | ||
|
|
||
| // Publish the sidebar watch event explicitly after chatstate has | ||
| // committed and emitted its own state-machine notifications. The | ||
| // watch endpoint is intentionally outside the RFC refactor scope. |
There was a problem hiding this comment.
Nit: Self-referential "RFC refactor" does not stand the test of time. If this is a TODO we could link to a ticket with context.
| var result SendMessageResult | ||
| machine := p.newChatMachine(opts.ChatID) | ||
| updateErr := machine.Update(ctx, func(tx *chatstate.Tx) error { | ||
| store := tx.Store() |
There was a problem hiding this comment.
Seeing this used, the structure feels a bit confusing. How about turning the Update signature into func(tx *chatstate.Tx, store database.Store) error or something like that?
The danger here is using a db defined outside the function apssed to Update, especially when it's hidden behind tx which piggy-backs on similar naming.
There was a problem hiding this comment.
Changed the signature of the callback function.
| txErr := p.db.InTx(func(tx database.Store) error { | ||
| lockedChat, err := tx.GetChatByIDForUpdate(ctx, opts.ChatID) | ||
| machine := p.newChatMachine(opts.ChatID) | ||
| updateErr := machine.Update(ctx, func(tx *chatstate.Tx) error { |
There was a problem hiding this comment.
Nit: Similar pattern as in PR 1, using updateErr instead of err. Prefer err when you don't need to juggle error values.
| } | ||
| return err |
| } | ||
|
|
||
| // Wake the chatd run loop so it processes the chat immediately. | ||
| p.signalWake() | ||
| return nil | ||
| return err |
There was a problem hiding this comment.
| } | |
| // Wake the chatd run loop so it processes the chat immediately. | |
| p.signalWake() | |
| return nil | |
| return err | |
| case default: | |
| return err | |
| } |
Suggestion: Same as in PR 1, structure gives compile-time protection against case statements that don't return.
| // Capture the post-reconcile chat inside the transaction so | ||
| // the returned chat and the watch event reflect the snapshot | ||
| // bump and status change produced by the transition itself. | ||
| latest, err := tx.Store().GetChatByID(ctx, chat.ID) |
There was a problem hiding this comment.
I think this pattern reinforces why we should restructure the callback function, somestimes we store := tx.Store(), other times tx.Store().Func, eventually in the future a similar code path will call p.db (or similar).
There was a problem hiding this comment.
Changed the signature of the callback function.
| if cfg.Pubsub == nil { | ||
| panic("chatd: Pubsub is nil") | ||
| } | ||
| ps := cfg.Pubsub |
There was a problem hiding this comment.
I agree, let's avoid runtime panics where we can and not hide required inputs in config. New(ps, config) seems like the right signature.
| case xerrors.Is(err, chatd.ErrChatArchived): | ||
| httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
| Message: "Cannot interrupt an archived chat.", | ||
| }) | ||
| case writeChatInvalidState(ctx, rw, err): | ||
| // response already written | ||
| case errors.Is(err, chatstate.ErrTransitionNotAllowed): | ||
| httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ | ||
| Message: "Chat is not in an interruptible state.", | ||
| Detail: err.Error(), | ||
| }) |
There was a problem hiding this comment.
Does there exist a cleaner implementation where we don't need to handle multiple cases in multiple locations? Maybe not the right naming but this whole switch could essentially be if handleChatError(ctx, rw, err) { return }.
ef9bcfc to
7dc8235
Compare
f2c32c2 to
17e40aa
Compare
7dc8235 to
d01e533
Compare
c5fb860 to
c41e5d6
Compare
d01e533 to
2c55f38
Compare
2c55f38 to
96fdf6f
Compare
21cf5c4 to
c61bc2e
Compare
b504e1d to
7e3810e
Compare
c61bc2e to
3cb1fc6
Compare
7e3810e to
61bb879
Compare
93e4119 to
bb090c5
Compare
61bb879 to
5b6d034
Compare
5b6d034 to
85a3bab
Compare
bb090c5 to
0bc334f
Compare
PR 2 of the chatd refactor. Reimplements the chatd http endpoints to use the state machine from #25908.
Adds a new
/chats/$id/reconcile-invalidendpoint to allow admins to recover chats that end up in an invalid state from the perspective of the state machine.Removes many chatd tests because the state machine PR introduced tests that replace them.
The CI doesn't pass on purpose. Only the tip of the chatd refactor PR stack will pass CI.