🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

feat: make workspaceAgentAddChatContext use the chatd state machine - #26112

Merged
hugodutka merged 1 commit into
hugodutka/chatd-refactorfrom
hugodutka/hugodutka/chatd-refactor-6
Jun 11, 2026
Merged

feat: make workspaceAgentAddChatContext use the chatd state machine#26112
hugodutka merged 1 commit into
hugodutka/chatd-refactorfrom
hugodutka/hugodutka/chatd-refactor-6

Conversation

@hugodutka

@hugodutka hugodutka commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

PR 6 of the chatd refactor. Originally not included in the RFC - I wasn't aware that workspaceAgentAddChatContext existed. It seems to be an internal endpoint that the workspace agent can call to inject additional context into the chat. I don't think it's used in production today, but I refactored it either way.

Previous PR in the stack: #26111

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 8fa644f to aef0c15 Compare June 5, 2026 18:05
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from efdf24f to 1627cf8 Compare June 5, 2026 18:05
@hugodutka hugodutka changed the title pr 6 feat: make workspaceAgentAddChatContext use the chatd state machine Jun 8, 2026
@hugodutka
hugodutka marked this pull request as ready for review June 8, 2026 12:38
@hugodutka
hugodutka requested review from johnstcn and mafredri and removed request for mafredri June 8, 2026 12:38
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from 1627cf8 to d0f134b Compare June 8, 2026 13:07
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from aef0c15 to 6c84888 Compare June 8, 2026 13:07
Comment thread coderd/workspaceagents.go Outdated
Comment on lines +2606 to +2634
if errors.Is(err, chatstate.ErrMessageQueueFull) {
var queueFull *chatstate.MessageQueueFullError
detail := ""
if errors.As(err, &queueFull) {
detail = fmt.Sprintf("Maximum %d messages can be queued.", queueFull.Max)
}
httpapi.Write(ctx, rw, http.StatusTooManyRequests, codersdk.Response{
Message: "Message queue is full.",
Detail: detail,
})
return
}
if errors.Is(err, chatstate.ErrInvalidState) {
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
Message: "Chat is in an invalid state.",
})
return
}
if errors.Is(err, chatstate.ErrTransitionNotAllowed) {
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
Message: "Chat is not in a state that accepts new context.",
Detail: err.Error(),
})
return
}
if errors.Is(err, chatstate.ErrChatNotFound) {
writeAgentChatError(ctx, rw, errChatNotFound)
return
}

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.

suggestion, non-blocking: maybe rewrite this as a switch?

Copy link
Copy Markdown
Contributor 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/workspaceagents.go Outdated
Comment on lines +2856 to +2872
func agentChatContextStateMessage(
content pqtype.NullRawMessage,
modelConfigID uuid.UUID,
ownerID uuid.UUID,
apiKeyID string,
) chatstate.Message {
return chatstate.Message{
Role: database.ChatMessageRoleUser,
Content: content,
Visibility: database.ChatMessageVisibilityBoth,
ModelConfigID: uuid.NullUUID{UUID: modelConfigID, Valid: modelConfigID != uuid.Nil},
CreatedBy: uuid.NullUUID{UUID: ownerID, Valid: ownerID != uuid.Nil},
ContentVersion: chatprompt.CurrentContentVersion,
APIKeyID: sql.NullString{String: apiKeyID, Valid: apiKeyID != ""},
}
}

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.

suggestion, non-blocking: this is only used in one place, just inline it for clarity.

Copy link
Copy Markdown
Contributor 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/workspaceagents.go
Comment on lines +2587 to +2589
if len(sendResult.InsertedMessages) == 0 {
return nil
}

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 seems like an error case? We're trying to send a message, but the result was that nothing was sent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From what I was able to gather, It's not an error. SendMessage will return no InsertedMessages if it queued up a message because it had to interrupt the chat and the chat is now in the interrupting state. So the last injected context won't be updated. But updating the last injected context column is described as "best effort" in other parts of the codebase, and its value doesn't have to be up to date. Looking at how it's used across the codebase, I think the "last updated context" column is a shoddy design and should be removed altogether.

@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from d0f134b to b17003c Compare June 9, 2026 12:03
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 6c84888 to 8641dd3 Compare June 9, 2026 12:03
Comment thread coderd/workspaceagents.go
}
if errors.Is(err, chatstate.ErrInvalidState) {
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
Message: "Chat is in an invalid state.",

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.

Do we need some type of debug logging or detail here? How may someone go about resolving this issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My current solution is to instruct admins in docs to use the /api/experimental/chats/{chat}/reconcile-invalid endpoint to get the chats out of an invalid state, but that's an awkward solution. I was thinking the archive/unarchive endpoints and the auto-archive loop could auto-apply invalid state reconciliation too.

Regarding logging, I think it would be best to add it in machine.Update whenever a transaction fails because of an invalid state error.

I would rather not do it now and address that in a follow up PR though - I added that as a Linear issue: https://linear.app/codercom/issue/CODAGT-584/automatic-invalid-state-reconciliation

@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 8641dd3 to d58c5ca Compare June 9, 2026 16:32
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch 2 times, most recently from a0c779b to 636b780 Compare June 10, 2026 11:50
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from 636b780 to 6e2d222 Compare June 11, 2026 11:43
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from d58c5ca to 1e945c4 Compare June 11, 2026 11:43
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from 6e2d222 to 4e54b39 Compare June 11, 2026 12:41
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 1e945c4 to 218f6eb Compare June 11, 2026 12:41
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from 4e54b39 to e0343d2 Compare June 11, 2026 16:27
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 218f6eb to 99da06d Compare June 11, 2026 16:27
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from e0343d2 to 1eeba2f Compare June 11, 2026 17:03
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 99da06d to 19ee932 Compare June 11, 2026 17:03
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from 1eeba2f to f8b7421 Compare June 11, 2026 17:04
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 19ee932 to 5cd93b7 Compare June 11, 2026 17:04
@hugodutka hugodutka mentioned this pull request Jun 11, 2026
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from f8b7421 to 4a27a74 Compare June 11, 2026 17:11
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch 2 times, most recently from c00a9ca to 323dc9b Compare June 11, 2026 17:13
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch 2 times, most recently from 3839cc9 to a174ac4 Compare June 11, 2026 17:14
@hugodutka
hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 323dc9b to 344ef41 Compare June 11, 2026 17:14
Base automatically changed from hugodutka/chatd-refactor-5 to hugodutka/chatd-refactor June 11, 2026 17:14
@hugodutka
hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from a174ac4 to f8e235d Compare June 11, 2026 17:15
@hugodutka
hugodutka merged commit fc2d742 into hugodutka/chatd-refactor Jun 11, 2026
@hugodutka
hugodutka deleted the hugodutka/hugodutka/chatd-refactor-6 branch June 11, 2026 17:15
@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