Skip to content

refactor: add chat_message_role enum and content_version column - #23042

Merged
mafredri merged 1 commit into
mainfrom
mafredri/feat-unify-chat-storage-follow-up
Mar 13, 2026
Merged

mafredri merged 1 commit into
mainfrom
mafredri/feat-unify-chat-storage-follow-up

Conversation

@mafredri

@mafredri mafredri commented Mar 13, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #22958. Adds a Postgres enum for chat_messages.role and a content_version column that replaces the structural heuristic for distinguishing content formats.

Migration 000434 creates chat_message_role enum, converts the role column from text, rebuilds the partial index, and adds content_version smallint NOT NULL. The column is added with DEFAULT 0 to backfill existing rows, then the default is dropped so future inserts must specify the version explicitly.

Content versioning: version 0 rows (all existing data) use the role-aware heuristic detection from #22958. Version 1 rows (all new inserts) store content as []ChatMessagePart JSON uniformly, including system messages. ParseContent takes database.ChatMessage directly and dispatches on version internally, callers never see version numbers. Unknown versions error instead of falling through.

Role enum: eliminates string(codersdk.ChatMessageRole*) casts at all 9 DB write sites and codersdk.ChatMessageRole(m.Role) unvalidated casts at read sites. Invalid roles are now caught at the DB layer.

System messages: now written as []ChatMessagePart via MarshalParts in V1, removing the special case that kept them as bare JSON strings. V0 system messages still parse through parseSystemRole as before.

Refs #22958

Base automatically changed from mafredri/feat-unify-chat-storage to main March 13, 2026 15:53
@mafredri mafredri changed the title refactor(coderd/database): add chat_message_role enum and content_version column refactor: add chat_message_role enum and content_version column Mar 13, 2026
@mafredri
mafredri force-pushed the mafredri/feat-unify-chat-storage-follow-up branch from 670f04b to d1a9408 Compare March 13, 2026 15:57
@mafredri
mafredri marked this pull request as ready for review March 13, 2026 16:03

@johnstcn johnstcn 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 blocking, thanks for the refactor!

// ContentVersionV0 is the legacy content format. Parsing uses
// role-aware heuristics to distinguish fantasy envelope format
// from SDK parts.
ContentVersionV0 int16 = 0

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.

Any particular reason to use this data type?

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.

Only that it matches the DB type (smallint). We could use a more regular type but that takes up a little more space in the DB.

Comment thread coderd/chatd/chatprompt/chatprompt.go Outdated
if err := json.Unmarshal(raw.RawMessage, &parts); err != nil {
return nil, xerrors.Errorf("parse %s content: %w", role, err)
}
if !hasNonEmptyType(parts) {

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.

Don't use no double negatives!

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.

Good catch. Removed the !hasNonEmptyType guard from parseContentV1 entirely. V1 content is always written by MarshalParts so parts will always have types. The guard was carried over from the legacy parsing functions where it made sense as a heuristic fallback.

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.

Double check the migration number before merge; Mikes pr has that number I think

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.

Will check before merge, thanks.

@mafredri
mafredri force-pushed the mafredri/feat-unify-chat-storage-follow-up branch 3 times, most recently from bd42887 to 3805d95 Compare March 13, 2026 16:29
…sion column

Adds a Postgres enum for chat_messages.role (previously text) and a
content_version column that replaces the structural heuristic used
to distinguish legacy fantasy envelope format from SDK parts.

Migration 000434 creates the chat_message_role enum, converts the
role column, rebuilds the partial index, and adds content_version
(smallint, default 0).

Version 0 rows use the existing role-aware heuristic detection.
Version 1 rows (all new inserts) store content as []ChatMessagePart
JSON uniformly across all roles, including system messages.

ParseContent now takes database.ChatMessage directly and dispatches
on content version internally. Callers never see or pass version
numbers. Unknown versions error instead of falling through.

All string(codersdk.ChatMessageRole*) casts at DB write sites are
replaced with database.ChatMessageRole* constants generated by sqlc.
System messages are now written as []ChatMessagePart via MarshalParts
instead of bare JSON strings, eliminating the V1 special case.
@mafredri
mafredri force-pushed the mafredri/feat-unify-chat-storage-follow-up branch from 3805d95 to b17402c Compare March 13, 2026 16:33
@mafredri
mafredri enabled auto-merge (squash) March 13, 2026 16:43
@mafredri
mafredri merged commit 4a79af1 into main Mar 13, 2026
26 checks passed
@mafredri
mafredri deleted the mafredri/feat-unify-chat-storage-follow-up branch March 13, 2026 16:47
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 13, 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