Skip to content

feat: use the state machine in chatd http endpoints - #25923

Merged
hugodutka merged 6 commits into
hugodutka/chatd-refactorfrom
hugodutka/chatd-refactor-2
Jun 11, 2026
Merged

hugodutka merged 6 commits into
hugodutka/chatd-refactorfrom
hugodutka/chatd-refactor-2

Conversation

@hugodutka

@hugodutka hugodutka commented Jun 1, 2026 •

Copy link
Copy Markdown
Collaborator

PR 2 of the chatd refactor. Reimplements the chatd http endpoints to use the state machine from #25908.

Adds a new /chats/$id/reconcile-invalid endpoint 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.

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Docs preview

📖 View docs preview for docs/reference/api/chats.md

@hugodutka
hugodutka requested review from johnstcn and mafredri June 1, 2026 19:10
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from ffbb99e to f20c569 Compare June 3, 2026 12:45
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch from 5d6ca0a to 723e393 Compare June 3, 2026 12:45
Comment on lines +43 to +50
func TestNewRequiresPubsub(t *testing.T) {
t.Parallel()

require.PanicsWithValue(t, "chatd: Pubsub is nil", func() {
_ = New(Config{})
})
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

changed to New(pb database.Pubsub, chatd.Config{})

Comment thread coderd/exp_chats_chatstate_test.go Outdated

// ptrTo returns a pointer to the given value. Helper for the
// pointer-shaped UpdateChatRequest fields.
func ptrTo[T any](v T) *T { return &v }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

github.com/coder/coder/v2/coderd/util/ptr.Ref

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

changed to use ptr.Ref

Comment thread coderd/exp_chats_chatstate_test.go Outdated
toolName string,
) (toolCallID string) {
t.Helper()
sysCtx := dbauthz.AsSystemRestricted(ctx) //nolint:gocritic // Test fixture composes chatstate transitions outside a request.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a great opportunity to exercise the permission set of dbauthz.AsChatd (and everywhere else we reach for it in this file)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Comment thread coderd/x/chatd/chatd.go
Comment on lines -1377 to -1379
// MaxQueueSize is the maximum number of queued user messages per chat.
const MaxQueueSize = 20

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it make more sense to pass this into the state machine?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It's already in the state machine. take a look at MaxQueueSize in coderd/x/chatd/chatstate/transitions.go.

Comment thread coderd/x/chatd/chatd.go
Comment on lines +3641 to +3644
if cfg.Pubsub == nil {
panic("chatd: Pubsub is nil")
}
ps := cfg.Pubsub

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Making pubsub a required parameter would make this situation less panic-able.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from f20c569 to dc8fd23 Compare June 4, 2026 14:42
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch from 723e393 to 779754a Compare June 4, 2026 14:42
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from dc8fd23 to ef9bcfc Compare June 4, 2026 18:51
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch from 779754a to f2c32c2 Compare June 4, 2026 18:52
@coderagents

coderagents Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Documentation Check

Updates Needed

  • docs/ai-coder/agents/tasks-to-chats-migration.md (line ~191-207) - The chat status mapping table lists all codersdk.ChatStatus values but is missing the new interrupting status added by this PR. Add a row for interrupting with an appropriate Tasks API mapping (n/a) and description (e.g., "Agent is being interrupted; transitions to waiting when complete."). The paragraph below the table (lines 202-207) describing which statuses are/aren't set in production should also be updated.
  • docs/ai-coder/agents/platform-controls/chat-auto-archive.md (line 22-23) - Lists active statuses that block auto-archiving (running, pending, paused, requires_action). The new interrupting status likely indicates ongoing work and should be added to this list so chats in that state are not auto-archived.

Automated review via Coder Agents

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nothing major to flag here, a nice improvement and LOC reduction 👍

Comment thread coderd/x/chatd/chatd.go
if opts.WorkspaceID.Valid {
workspaceAwareness = workspaceAttachedAwareness
} else {
workspaceAwareness = workspaceDetachedAwareness

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: This change seems kind of pointless?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, fixed.

Comment thread coderd/x/chatd/chatd.go

if !chat.RootChatID.Valid && !chat.ParentChatID.Valid {
chat.RootChatID = uuid.NullUUID{UUID: chat.ID, Valid: true}
initialMessages := make([]chatstate.Message, 0, 4)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this optimization truly worth introducing a value that has to be kept in sync with initial messages?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No. Removed it.

Comment thread coderd/x/chatd/chatd.go
}

// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

Comment thread coderd/x/chatd/chatd.go
var result SendMessageResult
machine := p.newChatMachine(opts.ChatID)
updateErr := machine.Update(ctx, func(tx *chatstate.Tx) error {
store := tx.Store()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changed the signature of the callback function.

Comment thread coderd/x/chatd/chatd.go
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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Similar pattern as in PR 1, using updateErr instead of err. Prefer err when you don't need to juggle error values.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Comment thread coderd/x/chatd/chatd.go
}
return err

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we wrap err?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Comment thread coderd/x/chatd/chatd.go
Comment on lines 2359 to +2360
}

// Wake the chatd run loop so it processes the chat immediately.
p.signalWake()
return nil
return err

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
}
// 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Comment thread coderd/x/chatd/chatd.go
// 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changed the signature of the callback function.

Comment thread coderd/x/chatd/chatd.go
Comment on lines +3641 to +3644
if cfg.Pubsub == nil {
panic("chatd: Pubsub is nil")
}
ps := cfg.Pubsub

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread coderd/exp_chats.go Outdated
Comment on lines +3573 to +3583
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(),
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 }.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added a helper.

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from ef9bcfc to 7dc8235 Compare June 5, 2026 15:08
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch from f2c32c2 to 17e40aa Compare June 5, 2026 15:08
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 7dc8235 to d01e533 Compare June 5, 2026 17:24
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch 2 times, most recently from c5fb860 to c41e5d6 Compare June 5, 2026 18:05
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from d01e533 to 2c55f38 Compare June 5, 2026 18:05
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 2c55f38 to 96fdf6f Compare June 9, 2026 12:03
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch 2 times, most recently from 21cf5c4 to c61bc2e Compare June 9, 2026 16:32
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch 2 times, most recently from b504e1d to 7e3810e Compare June 11, 2026 11:43
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch from c61bc2e to 3cb1fc6 Compare June 11, 2026 11:43
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 7e3810e to 61bb879 Compare June 11, 2026 16:27
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch 2 times, most recently from 93e4119 to bb090c5 Compare June 11, 2026 17:03
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 61bb879 to 5b6d034 Compare June 11, 2026 17:03
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-2 branch from 5b6d034 to 85a3bab Compare June 11, 2026 17:04
@hugodutka
hugodutka force-pushed the chatd-pr1-state-machine branch from bb090c5 to 0bc334f Compare June 11, 2026 17:04
@hugodutka hugodutka mentioned this pull request Jun 11, 2026
Base automatically changed from chatd-pr1-state-machine to hugodutka/chatd-refactor June 11, 2026 17:08
@hugodutka
hugodutka merged commit 8f24257 into hugodutka/chatd-refactor Jun 11, 2026
7 of 9 checks passed
@hugodutka
hugodutka deleted the hugodutka/chatd-refactor-2 branch June 11, 2026 17:08
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants