Files
coder/coderd/x/chatd/message_conversion.go
T
Michael Suchacz b2c8798e0e fix: replay stateless OpenAI reasoning across chat turns (#30298)
OpenAI Responses chats default to `store=false`, and chatd only replayed
reasoning as an `item_reference`, which needs provider-side storage.
Reasoning was therefore dropped between tool steps and turns, so
reasoning models lost their chain of thought mid-task.

This pins coder/fantasy `coder_2_33` at 6ba8681, which includes fantasy
#63 (and #64, #65, and #66). #63 captures each completed reasoning item
(ID, `encrypted_content`, summary) and replays it in full regardless of
`store`. In chatd:

- Every Responses call requests `reasoning.encrypted_content`. The
`store` default and configured includes are unchanged, and Chat
Completions requests are untouched.
- Prompt rebuilds keep reasoning parts without visible text when they
carry finalized encrypted state, so reasoning survives durable history
and coderd restarts.
- The provider-switch sanitizer also strips OpenAI reasoning from
history produced by a different or unknown provider instance, since
reasoning IDs and encrypted content only resolve where they were issued.
- A compaction model override on a different provider instance, or with
a different model, gets a compaction prompt without the chat's OpenAI
reasoning.
- Each saved reasoning part records the provider instance, model, and
credential (the deployment's or the user's personal API key) that
produced it, and the sanitizer keeps it only when all three match the
current turn, because another model, or the organization behind another
credential, can reject the encrypted content. Switching models, adding
or removing a personal key, or editing a config's model or provider
drops the earlier reasoning; switching back restores it. The record is
never returned by the API. Reasoning saved before this change has no
record and follows its model config.
- If OpenAI rejects replayed reasoning it cannot decrypt
(`invalid_encrypted_content`), for example after the deployment's key is
replaced with one from another organization, the request is retried once
without reasoning, and the reasoning that step produces is saved with
the provider's next rejection epoch. Epochs are kept separately for each
provider, model, and credential. Later steps and turns leave out
reasoning from earlier epochs, on any replica and after restarts, so
only the first request after the change is rejected and newer reasoning
keeps replaying. A retried step that returns no reasoning saves its
epoch on its first part. The advisor's prompt never includes OpenAI
reasoning, as on `main`.

## Testing

Rebased onto main at 98332c739b as 5735acb36ed (same tree as merging
the pre-rebase head a378071862a with main; older SHAs below refer to the
pre-rebase history). 3c17d807a33 then adds the model to the reasoning
record, 21156b2b32e pins Fantasy d55a867, which treats a response that
echoes `store: false` as unstored, bf795daa923 adds the retry and the
advisor change, 7eb8b1d029a moves the pin to 88cffb5 (`coder_2_33`
merged into #63; its OpenAI provider code is identical to d55a867), and
f4f40734a41 pins `coder_2_33` at 6ba8681 once #63 merged. That adds
fantasy #66, which replays the Responses message phase on assistant
text. abccede81f7 makes the retry remember rejected reasoning in the
process. The branch was then rebased onto main at abc2faed60, so every
SHA above refers to the pre-rebase history. 5fb3ce1689 replaces that
memory with the credential and a rejection epoch saved on each reasoning
part, and 62b5cb532a keeps epochs per provider, model, and credential
and saves the epoch when the retried step returns no reasoning.
1057beb327 hides the four internal fields from the Swagger spec and API
reference, and 29a705e099 merges main at d68b2c8b25. Its only
conflicts were the fantasy pin (kept at 6ba8681, whose `coder_2_33`
history includes main's 3510552) and test imports.

- Tests in `chatprompt`, `chatprovider`, `chatsanitize`, the
provider-switch sanitizer, and `integration_responses_test.go` cover
tool-step and durable replay, legacy and incomplete metadata, provider
switches, and a web search chat switching from `store=false` to
`store=true`. Integration tests also cover the retry, a later tool step
after it, the next turn on a fresh server against the same database
(with and without reasoning in the retried step), a personal key added
mid-chat, and the advisor prompt. Red-green verified, including a config
whose model is edited in place. The compaction override's model check
has unit tests and UAT coverage but no integration test. `go vet`,
targeted race tests, and the pre-commit hook (gen, fmt, lint, build)
pass. Local pre-push hooks were skipped; CI covers them.
- Remote UAT on dogfood with real gpt-5-mini and gpt-5.4 behind a
logging proxy that checks every request body. Rounds 1 and 2 failed on
dangling item references (both fixed). Rounds 3 to 5 passed
(ee3c0bf3d60, 0580870b5ff, a378071862a): tool-step and durable replay,
restarts, interrupts, provider switches and config repoints, web search
history, and compaction overrides. Both test providers shared one
upstream account, so cross-provider checks cover request shape, not a
real rejection. Round 6 at 21156b2b32e (model record) with gpt-5-mini
and gpt-5-nano: PASS. 151 Responses requests, all 200; no request
carried reasoning from a different model except an older, unrecorded
part that follows its config by design. Same-model config switches,
switching back, edits to a config's model, a mid-turn switch, restarts,
and a same-provider compaction override with a different model all
behaved as described. Round 7 at bf795daa923 (retry and advisor): PASS.
Through AI Gateway, a corrupted saved blob is rejected with HTTP 400
`invalid_encrypted_content`. All 14 rejected requests were retried once
without reasoning and returned 200, a different 400 was not retried, and
advisor requests on the chat model, a same-provider override, and
another provider carried no reasoning.
- f4f40734a41 changes only the pin. Its new OpenAI code is fantasy #66,
which UAT rounds 1 to 7 did not run. A live check of fantasy 6ba8681
through AI Gateway, two turns with tool calls on gpt-5.5 and gpt-5-mini
with `store` false and true: all 18 requests returned 200, and the
gpt-5.5 requests replayed encrypted reasoning followed by assistant
messages with the `commentary` and `final_answer` phases. `go test
./coderd/x/chatd/... ./codersdk/` passes (21 packages) on the same
fantasy tree.
- UAT round 8 at abccede81f7 (remembered rejection, and fantasy #66
through chatd) with gpt-5.5, gpt-5-mini, and gpt-5-nano: PASS. 110
requests; the 6 rejections were 5 corrupted saved blobs, each retried
once without reasoning and answered 200, and one unrelated `temperature`
error that was not retried. After the first rejection, later steps and
turns in that chat were never rejected again and replayed newer
reasoning. A coderd restart cost one more rejection, which also dropped
reasoning saved after the first one, and a second chat was unaffected.
gpt-5.5 requests replayed assistant messages with the phases the model
returned, with `store` false and true.
- UAT round 9 at 62b5cb532a (saved rejection epoch, personal key
switch) with gpt-5.5, gpt-5-mini, and gpt-5-nano: PASS. 158 Responses
and 7 Chat Completions requests; the 6 rejections were corrupted saved
blobs, each retried once without reasoning and answered 200, and an
unrelated `temperature` error was not retried. After a coderd restart,
and on a second coderd against the same database, the chat was not
rejected again and kept replaying the reasoning saved after the
rejection. Adding a personal key left out the deployment key's reasoning
before sending, and removing it brought that reasoning back. A retried
step without reasoning, two chats rejected at once, compaction, a model
switch after a rejection, and a switch to Chat Completions and back all
behaved as described.

## Review record

- Independent full-branch test audit at bbe1a1b27a: PASS.
- Codex through a378071862: compaction overrides kept foreign reasoning
and configs could be repointed to another provider (both fixed); a
request for documentation TODOs and two behaviors that already exist on
`main` (declined).
- Codex on 5735acb36e: two findings. Reasoning replayed after a
same-provider model switch: fixed in 3c17d807a33. A provider's base URL
edited in place keeps its reasoning: covered by the retry in
bf795daa923.
- Codex on 21156b2b32, bf795daa92, and 7eb8b1d029: no major issues.
Codex on f4f40734a4: rejected reasoning stayed in the chat's history, so
each later request in that chat was rejected and retried again without
any reasoning. Fixed in abccede81f7. Codex on abccede81f: that memory
was lost on a restart or another replica, where the next rejection also
dropped newer reasoning, and it grew without bound. Fixed in
5fb3ce1689. Codex on 5fb3ce1689: a rejection advanced the epoch for
every model and credential on the provider, and a retried step without
reasoning lost the new epoch. Both fixed in 62b5cb532a. Codex on
62b5cb532a: keep reasoning out of a config switched from Responses to
Chat Completions (declined: the Chat Completions converter already skips
reasoning parts, confirmed with a probe test). Codex on 29a705e099: no
major issues.
- ethanndickson: retry without reasoning on `invalid_encrypted_content`,
and keep OpenAI reasoning out of the advisor prompt. Both fixed in
bf795daa923. Then: in-process memory is not enough because dogfood
restarts on every merge and runs several replicas. Fixed in 5fb3ce1689
with his proposed rejection epoch in chat history, plus the credential
record, and tested with the next turn on a fresh server. Then: hide the
internal fields from Swagger, fixed in 1057beb327. Approved on
62b5cb532a.
- coder/fantasy#63 review: honoring an echoed `store: false` is fixed in
d55a867; replaying web searches that had no preceding reasoning is a
pre-existing gap, tracked in #30528.
- CI on 5735acb36ed: green after rerunning two failures outside this
change (a `cli` `TestSSH` data race, and a `test-go-pg` job timeout that
also fails on `main`). CI on 21156b2b32e: all checks pass. CI on
bf795daa923: all checks pass. CI on 7eb8b1d029a: all checks pass. CI on
f4f40734a41: all checks pass. CI on abccede81f7 did not run, because the
branch conflicted with `main` (#30492 changed the provider-switch
sanitizer to return database errors). CI on 62b5cb532a: all checks
pass. CI on 29a705e099: all checks pass.

> Xum, an AI coding agent, implemented, tested, and opened this PR on
behalf of @ibetitsmike.
2026-10-13 13:49:12 +02:00

1140 lines
38 KiB
Go

package chatd
import (
"cmp"
"context"
"database/sql"
"encoding/json"
"slices"
"strings"
"time"
"charm.land/fantasy"
"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/x/chatd/chatloop"
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
"github.com/coder/coder/v2/coderd/x/chatd/messagepartbuffer"
"github.com/coder/coder/v2/codersdk"
)
const interruptedToolResultErrorMessage = "tool call was interrupted before it produced a result"
type buildCommitStepMessagesInput struct {
modelConfigID uuid.UUID
step stepData
toolNameToConfigID map[string]uuid.UUID
logger slog.Logger
contentVersion int16
hookRewrittenToolCalls map[string]json.RawMessage
reasoningProvenance reasoningProvenance
}
func stampReasoningProvenance(part *codersdk.ChatMessagePart, provenance reasoningProvenance) {
part.ProviderIdentity = provenance.ProviderIdentity
part.ProviderModel = provenance.Model
part.ProviderCredential = provenance.Credential
part.ReasoningEpoch = provenance.Epoch
}
type stepMessagesForCommit struct {
Messages []chatstate.Message
// ConsumeCompactionRequest clears the manual compaction marker
// atomically with the commit. Set on compaction commits.
ConsumeCompactionRequest bool
}
func buildCommitStepMessages(input buildCommitStepMessagesInput) (stepMessagesForCommit, error) {
contentVersion := input.contentVersion
if contentVersion == 0 {
contentVersion = chatprompt.CurrentContentVersion
}
assistantBlocks, toolResults := splitStepContent(input.step.Content)
assistantParts := buildAssistantParts(input.logger, assistantBlocks, toolResults, input.step, input.toolNameToConfigID, input.hookRewrittenToolCalls)
stamped := false
for i := range assistantParts {
if assistantParts[i].Type == codersdk.ChatMessagePartTypeReasoning {
stampReasoningProvenance(&assistantParts[i], input.reasoningProvenance)
stamped = true
}
}
// Without a stamp, a step that produced no reasoning would lose the
// rejection epoch, and the next step would replay the rejected
// reasoning again.
if !stamped && input.reasoningProvenance.Epoch > 0 && len(assistantParts) > 0 {
stampReasoningProvenance(&assistantParts[0], input.reasoningProvenance)
}
messages := make([]chatstate.Message, 0, 1+len(toolResults))
if len(assistantParts) > 0 {
assistantContent, err := chatprompt.MarshalParts(assistantParts)
if err != nil {
return stepMessagesForCommit{}, xerrors.Errorf("marshal assistant content: %w", err)
}
messages = append(messages, assistantMessage(input.modelConfigID, contentVersion, assistantContent, input.step))
}
for _, toolResult := range toolResults {
part := chatprompt.PartFromContentWithLogger(context.Background(), input.logger, toolResult)
applyToolMetadata(&part, input.toolNameToConfigID)
if part.ToolCallID != "" && input.step.ToolResultCreatedAt != nil {
if ts, ok := input.step.ToolResultCreatedAt[part.ToolCallID]; ok {
part.CreatedAt = &ts
}
}
content, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{part})
if err != nil {
return stepMessagesForCommit{}, xerrors.Errorf("marshal tool result: %w", err)
}
messages = append(messages, baseMessage(database.ChatMessageRoleTool, database.ChatMessageVisibilityBoth, input.modelConfigID, contentVersion, content))
}
// Usage sums runtime_ms across rows, so the batch window is billed
// once on a dedicated record instead of an arbitrary member row.
stamp, ok, err := batchUsageMessage(input.modelConfigID, contentVersion, input.step.BatchRuntime, input.step.BatchBilledCalls)
if err != nil {
return stepMessagesForCommit{}, err
}
if ok {
messages = append(messages, stamp)
}
return stepMessagesForCommit{
Messages: messages,
}, nil
}
func splitStepContent(content []fantasy.Content) ([]fantasy.Content, []fantasy.ToolResultContent) {
assistantBlocks := make([]fantasy.Content, 0, len(content))
toolResults := make([]fantasy.ToolResultContent, 0)
for _, block := range content {
if tr, ok := asToolResultContent(block); ok && !tr.ProviderExecuted {
toolResults = append(toolResults, tr)
continue
}
assistantBlocks = append(assistantBlocks, block)
}
return assistantBlocks, toolResults
}
func asToolResultContent(block fantasy.Content) (fantasy.ToolResultContent, bool) {
if tr, ok := fantasy.AsContentType[fantasy.ToolResultContent](block); ok {
return tr, true
}
if tr, ok := fantasy.AsContentType[*fantasy.ToolResultContent](block); ok && tr != nil {
return *tr, true
}
return fantasy.ToolResultContent{}, false
}
func buildAssistantParts(
logger slog.Logger,
assistantBlocks []fantasy.Content,
toolResults []fantasy.ToolResultContent,
step stepData,
toolNameToConfigID map[string]uuid.UUID,
hookRewrittenToolCalls map[string]json.RawMessage,
) []codersdk.ChatMessagePart {
parts := make([]codersdk.ChatMessagePart, 0, len(assistantBlocks)+len(toolResults))
reasoningIdx := 0
for _, block := range assistantBlocks {
part := chatprompt.PartFromContentWithLogger(context.Background(), logger, block)
applyToolMetadata(&part, toolNameToConfigID)
switch part.Type {
case codersdk.ChatMessagePartTypeToolCall:
if part.ToolCallID != "" && step.ToolCallCreatedAt != nil {
if ts, ok := step.ToolCallCreatedAt[part.ToolCallID]; ok {
part.CreatedAt = &ts
}
}
// Hooks never see provider-executed calls, so such a call must not
// inherit attribution from an ordinary call that reused its ID.
if part.ToolCallID != "" && !part.ProviderExecuted {
_, part.HookRewritten = hookRewrittenToolCalls[part.ToolCallID]
}
case codersdk.ChatMessagePartTypeToolResult:
if part.ToolCallID != "" && step.ToolResultCreatedAt != nil {
if ts, ok := step.ToolResultCreatedAt[part.ToolCallID]; ok {
part.CreatedAt = &ts
}
}
case codersdk.ChatMessagePartTypeReasoning:
if reasoningIdx < len(step.ReasoningStartedAt) {
if ts := step.ReasoningStartedAt[reasoningIdx]; !ts.IsZero() {
part.CreatedAt = &ts
}
}
if reasoningIdx < len(step.ReasoningCompletedAt) {
if ts := step.ReasoningCompletedAt[reasoningIdx]; !ts.IsZero() {
part.CompletedAt = &ts
}
}
reasoningIdx++
}
if part.Type != "" {
parts = append(parts, part)
}
}
for _, tr := range toolResults {
attachments, err := chattool.AttachmentsFromMetadata(tr.ClientMetadata)
if err != nil {
logger.Warn(context.Background(), "skipping malformed tool attachment metadata",
slog.F("tool_name", tr.ToolName),
slog.F("tool_call_id", tr.ToolCallID),
slog.Error(err),
)
continue
}
for _, attachment := range attachments {
parts = append(parts, codersdk.ChatMessageFile(attachment.FileID, attachment.MediaType, attachment.Name))
}
}
return parts
}
func applyToolMetadata(part *codersdk.ChatMessagePart, toolNameToConfigID map[string]uuid.UUID) {
if part.ToolName == "" || len(toolNameToConfigID) == 0 {
return
}
if configID, ok := toolNameToConfigID[part.ToolName]; ok {
part.MCPServerConfigID = uuid.NullUUID{UUID: configID, Valid: true}
}
}
func assistantMessage(
modelConfigID uuid.UUID,
contentVersion int16,
content pqtype.NullRawMessage,
step stepData,
) chatstate.Message {
msg := baseMessage(database.ChatMessageRoleAssistant, database.ChatMessageVisibilityBoth, modelConfigID, contentVersion, content)
if step.Usage != (fantasy.Usage{}) {
msg.InputTokens = nullInt64IfNonZero(step.Usage.InputTokens)
msg.OutputTokens = nullInt64IfNonZero(step.Usage.OutputTokens)
msg.TotalTokens = nullInt64IfNonZero(step.Usage.TotalTokens)
msg.ReasoningTokens = nullInt64IfNonZero(step.Usage.ReasoningTokens)
msg.CacheCreationTokens = nullInt64IfNonZero(step.Usage.CacheCreationTokens)
msg.CacheReadTokens = nullInt64IfNonZero(step.Usage.CacheReadTokens)
}
msg.ContextLimit = step.ContextLimit
// InsertChatMessages maps a zero runtime to NULL, so a model
// invocation shorter than a millisecond persists the same way an
// unmeasured one does.
msg.RuntimeMs = nullInt64IfNonZero(step.Runtime.Milliseconds())
msg.ProviderResponseID = sql.NullString{String: step.ProviderResponseID, Valid: step.ProviderResponseID != ""}
return msg
}
func baseMessage(
role database.ChatMessageRole,
visibility database.ChatMessageVisibility,
modelConfigID uuid.UUID,
contentVersion int16,
content pqtype.NullRawMessage,
) chatstate.Message {
return chatstate.Message{
Role: role,
Content: content,
Visibility: visibility,
ModelConfigID: uuid.NullUUID{UUID: modelConfigID, Valid: modelConfigID != uuid.Nil},
ContentVersion: contentVersion,
}
}
func nullInt64IfNonZero(value int64) sql.NullInt64 {
if value == 0 {
return sql.NullInt64{}
}
return sql.NullInt64{Int64: value, Valid: true}
}
// toolBatchUsagePartType marks the dedicated billing record for a local
// tool batch. Internal to chatd: the row is persisted with model
// visibility so it never reaches the API or SSE, and prompt replay drops
// it because the part converts to no provider content.
const toolBatchUsagePartType codersdk.ChatMessagePartType = "tool-batch-usage"
// toolBatchUsagePayload is the audit payload stored on the usage record.
// It duplicates the row's runtime_ms so the billed window survives in
// content for debugging, alongside how many call intervals produced it.
type toolBatchUsagePayload struct {
BilledMs int64 `json:"billed_ms"`
BilledCalls int `json:"billed_calls"`
}
// batchUsageMessage builds the single model-invisible row that carries a
// local tool batch's billed runtime. Usage sums runtime_ms across rows,
// so a dedicated record keeps real tool results free of batch-level
// runtime. Completed and interrupted batches share this helper. Returns
// false when the batch bills no whole millisecond.
func batchUsageMessage(
modelConfigID uuid.UUID,
contentVersion int16,
runtime time.Duration,
billedCalls int,
) (chatstate.Message, bool, error) {
runtimeMs := runtime.Milliseconds()
if runtimeMs <= 0 {
return chatstate.Message{}, false, nil
}
payload, err := json.Marshal(toolBatchUsagePayload{
BilledMs: runtimeMs,
BilledCalls: billedCalls,
})
if err != nil {
return chatstate.Message{}, false, xerrors.Errorf("marshal tool batch usage payload: %w", err)
}
content, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{{
Type: toolBatchUsagePartType,
Result: payload,
}})
if err != nil {
return chatstate.Message{}, false, xerrors.Errorf("marshal tool batch usage part: %w", err)
}
msg := baseMessage(database.ChatMessageRoleTool, database.ChatMessageVisibilityModel, modelConfigID, contentVersion, content)
msg.RuntimeMs = sql.NullInt64{Int64: runtimeMs, Valid: true}
return msg, true, nil
}
func textFromParts(parts []codersdk.ChatMessagePart) string {
var builder strings.Builder
for _, part := range parts {
if part.Type == codersdk.ChatMessagePartTypeText {
_, _ = builder.WriteString(part.Text)
}
}
return builder.String()
}
type buildCompactionMessagesInput struct {
modelConfigID uuid.UUID
toolCallID string
toolName string
compaction compactionOutcome
contentVersion int16
pendingUserMessages []database.ChatMessage
}
type compactionMessagesForCommit struct {
Messages []chatstate.Message
}
func buildCompactionMessages(input buildCompactionMessagesInput) (compactionMessagesForCommit, error) {
contentVersion := input.contentVersion
if contentVersion == 0 {
contentVersion = chatprompt.CurrentContentVersion
}
toolName := input.toolName
if toolName == "" {
toolName = "chat_summarized"
}
systemContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{codersdk.ChatMessageText(input.compaction.SystemSummary)})
if err != nil {
return compactionMessagesForCommit{}, xerrors.Errorf("marshal compaction system summary: %w", err)
}
source := input.compaction.Source
if source == "" {
source = chatloop.CompactionSourceAutomatic
}
args, err := json.Marshal(map[string]any{
"source": source,
"threshold_percent": input.compaction.ThresholdPercent,
})
if err != nil {
return compactionMessagesForCommit{}, xerrors.Errorf("marshal compaction args: %w", err)
}
assistantContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
codersdk.ChatMessageToolCall(input.toolCallID, toolName, args),
})
if err != nil {
return compactionMessagesForCommit{}, xerrors.Errorf("marshal compaction tool call: %w", err)
}
summaryResult, err := json.Marshal(chatloop.CompactionToolResult{
Summary: input.compaction.SummaryReport,
Source: source,
ThresholdPercent: input.compaction.ThresholdPercent,
UsagePercent: input.compaction.UsagePercent,
ContextTokens: input.compaction.ContextTokens,
ContextLimitTokens: input.compaction.ChatContextLimit,
EstimatedContextTokens: input.compaction.EstimatedContextTokens,
TriggerContextLimitTokens: input.compaction.TriggerContextLimit,
})
if err != nil {
return compactionMessagesForCommit{}, xerrors.Errorf("marshal compaction result: %w", err)
}
toolContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
codersdk.ChatMessageToolResult(input.toolCallID, toolName, summaryResult, false, false),
})
if err != nil {
return compactionMessagesForCommit{}, xerrors.Errorf("marshal compaction tool result: %w", err)
}
assistantMsg := baseMessage(database.ChatMessageRoleAssistant, database.ChatMessageVisibilityUser, input.modelConfigID, contentVersion, assistantContent)
assistantMsg.RuntimeMs = nullInt64IfNonZero(input.compaction.Runtime.Milliseconds())
assistantMsg.ProviderResponseID = sql.NullString{String: input.compaction.ProviderResponseID, Valid: input.compaction.ProviderResponseID != ""}
messages := []chatstate.Message{
{
Role: database.ChatMessageRoleUser,
Content: systemContent,
Visibility: database.ChatMessageVisibilityModel,
ModelConfigID: uuid.NullUUID{UUID: input.modelConfigID, Valid: input.modelConfigID != uuid.Nil},
ContentVersion: contentVersion,
},
assistantMsg,
baseMessage(database.ChatMessageRoleTool, database.ChatMessageVisibilityBoth, input.modelConfigID, contentVersion, toolContent),
}
for i := range messages {
messages[i].Compressed = true
}
for _, row := range input.pendingUserMessages {
messages = append(messages, chatstate.Message{
Role: database.ChatMessageRoleUser,
Content: row.Content,
Visibility: database.ChatMessageVisibilityModel,
ModelConfigID: uuid.NullUUID{UUID: input.modelConfigID, Valid: input.modelConfigID != uuid.Nil},
ContentVersion: row.ContentVersion,
})
}
return compactionMessagesForCommit{Messages: messages}, nil
}
type buildClearMessagesInput struct {
modelConfigID uuid.UUID
toolCallID string
contentVersion int16
}
// buildClearMessages produces the manual context-clear boundary
// triplet, mirroring the compaction triplet shape: a hidden
// model-only user-role row (the boundary anchor the prompt query keys
// on), a user-visible synthetic chat_cleared tool call, and its tool
// result. The hidden row carries a short sentinel rather than empty
// content so the next prompt never sends an empty user message.
func buildClearMessages(input buildClearMessagesInput) ([]chatstate.Message, error) {
contentVersion := input.contentVersion
if contentVersion == 0 {
contentVersion = chatprompt.CurrentContentVersion
}
sentinelContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
codersdk.ChatMessageText("Previous conversation context was cleared by the user."),
})
if err != nil {
return nil, xerrors.Errorf("marshal clear sentinel: %w", err)
}
payload := json.RawMessage(`{"source":"manual"}`)
assistantContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
codersdk.ChatMessageToolCall(input.toolCallID, "chat_cleared", payload),
})
if err != nil {
return nil, xerrors.Errorf("marshal clear tool call: %w", err)
}
toolContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
codersdk.ChatMessageToolResult(input.toolCallID, "chat_cleared", payload, false, false),
})
if err != nil {
return nil, xerrors.Errorf("marshal clear tool result: %w", err)
}
messages := []chatstate.Message{
{
Role: database.ChatMessageRoleUser,
Content: sentinelContent,
Visibility: database.ChatMessageVisibilityModel,
ModelConfigID: uuid.NullUUID{UUID: input.modelConfigID, Valid: input.modelConfigID != uuid.Nil},
ContentVersion: contentVersion,
},
baseMessage(database.ChatMessageRoleAssistant, database.ChatMessageVisibilityUser, input.modelConfigID, contentVersion, assistantContent),
baseMessage(database.ChatMessageRoleTool, database.ChatMessageVisibilityBoth, input.modelConfigID, contentVersion, toolContent),
}
for i := range messages {
messages[i].Compressed = true
}
return messages, nil
}
// hasClearableMessageAfter reports whether any active, uncompressed
// model-visible conversation message follows the boundary index.
// System prompts and user-only rows do not make a chat clearable.
func hasClearableMessageAfter(messages []database.ChatMessage, index int) bool {
for i := index + 1; i < len(messages); i++ {
msg := messages[i]
if msg.Deleted || msg.Compressed {
continue
}
if msg.Role == database.ChatMessageRoleSystem {
continue
}
if msg.Visibility == database.ChatMessageVisibilityModel || msg.Visibility == database.ChatMessageVisibilityBoth {
return true
}
}
return false
}
// Hook model-context messages use the user role but must not reset
// per-turn guards.
func lastUserPromptIndex(messages []database.ChatMessage) int {
index := -1
for i, msg := range messages {
if msg.Deleted || msg.Compressed {
continue
}
if msg.Role == database.ChatMessageRoleUser && msg.Visibility != database.ChatMessageVisibilityModel {
index = i
}
}
return index
}
func currentTurnStartIndex(messages []database.ChatMessage) int {
return lastUserPromptIndex(messages) + 1
}
func currentTurnStepCount(messages []database.ChatMessage) int {
count := 0
for _, msg := range messages[currentTurnStartIndex(messages):] {
if msg.Deleted || msg.Compressed {
continue
}
if msg.Role == database.ChatMessageRoleAssistant {
count++
}
}
return count
}
type compactionRequirement int
const (
compactionRequirementNotNeeded compactionRequirement = iota
compactionRequirementNeeded
)
func compactionStatusFromHistory(
messages []database.ChatMessage,
requirement compactionRequirement,
thresholdPercent int32,
triggerContextLimit int64,
) compactionStatus {
boundaryIndex := latestContextBoundaryIndex(messages)
if requirement == compactionRequirementNeeded {
if boundaryIndex == -1 {
return compactionStatusNeeded
}
// The first assistant response after the previously compacted summary.
// Messages with role ChatMessageRoleAssistant carry context usage.
// Looking at ChatMessageRoleAssistant is enough - ChatMessageRoleTool
// does not carry context usage, and is always preceded by an assistant
// message.
if assistant, ok := firstUncompressedAssistantAfter(messages, boundaryIndex); ok &&
postCompactionAssistantOverLimit(assistant, thresholdPercent, triggerContextLimit) &&
!summaryRecordedUnderOtherTrigger(messages[boundaryIndex], thresholdPercent, triggerContextLimit) {
return compactionStatusStillOverLimit
}
if hasUncompressedMessageAfter(messages, boundaryIndex) {
return compactionStatusNeeded
}
return compactionStatusAfterCompaction
}
if boundaryIndex != -1 && !hasUncompressedMessageAfter(messages, boundaryIndex) {
return compactionStatusAfterCompaction
}
return compactionStatusNotNeeded
}
// summaryRecordedUnderOtherTrigger reports whether a chat_summarized
// boundary recorded a trigger other than the current one. A summary made
// under an older trigger is not a failed compaction under the current one.
func summaryRecordedUnderOtherTrigger(boundary database.ChatMessage, thresholdPercent int32, triggerContextLimit int64) bool {
parts, err := chatprompt.ParseContent(boundary)
if err != nil {
return false
}
for _, part := range parts {
if part.ToolName != "chat_summarized" || part.Type != codersdk.ChatMessagePartTypeToolResult {
continue
}
// Results written before trigger_context_limit_tokens stored the
// trigger's window in context_limit_tokens. A result without these
// keys compares equal to the current trigger.
const noTriggerContextLimit = -1
recorded := chatloop.CompactionToolResult{
ThresholdPercent: thresholdPercent,
ContextLimitTokens: triggerContextLimit,
TriggerContextLimitTokens: noTriggerContextLimit,
}
if err := json.Unmarshal(part.Result, &recorded); err != nil {
return false
}
recordedLimit := recorded.TriggerContextLimitTokens
if recordedLimit == noTriggerContextLimit {
recordedLimit = recorded.ContextLimitTokens
}
return recorded.ThresholdPercent != thresholdPercent || recordedLimit != triggerContextLimit
}
return false
}
// latestContextBoundaryIndex finds the latest compressed
// chat_summarized or chat_cleared boundary. Compaction and clear
// eligibility both stop here so neither reaches across the other.
func latestContextBoundaryIndex(messages []database.ChatMessage) int {
for i := len(messages) - 1; i >= 0; i-- {
if isContextBoundaryMessage(messages[i]) {
return i
}
}
return -1
}
func isContextBoundaryMessage(msg database.ChatMessage) bool {
if msg.Deleted || !msg.Compressed {
return false
}
parts, err := chatprompt.ParseContent(msg)
if err != nil {
return false
}
for _, part := range parts {
if (part.ToolName == "chat_summarized" || part.ToolName == "chat_cleared") &&
(part.Type == codersdk.ChatMessagePartTypeToolCall || part.Type == codersdk.ChatMessagePartTypeToolResult) {
return true
}
}
return false
}
// pendingUserSegmentStart returns the index of the first row of the trailing run of unanswered user-role rows (len(promptRows) when there is none or when no assistant row precedes it); scanning persisted rows means assistant rows terminate the run even when prompt conversion or sanitization drops them.
func pendingUserSegmentStart(promptRows []database.ChatMessage) int {
start := len(promptRows)
for start > 0 {
row := promptRows[start-1]
if row.Deleted || row.Compressed || row.Role != database.ChatMessageRoleUser {
break
}
start--
}
for _, row := range promptRows[:start] {
if !row.Deleted && row.Role == database.ChatMessageRoleAssistant {
return start
}
}
return len(promptRows)
}
func firstUncompressedAssistantAfter(messages []database.ChatMessage, index int) (database.ChatMessage, bool) {
for i := index + 1; i < len(messages); i++ {
msg := messages[i]
if msg.Deleted || msg.Compressed {
continue
}
if msg.Role == database.ChatMessageRoleAssistant {
return msg, true
}
}
return database.ChatMessage{}, false
}
func hasUncompressedMessageAfter(messages []database.ChatMessage, index int) bool {
for i := index + 1; i < len(messages); i++ {
msg := messages[i]
if !msg.Deleted && !msg.Compressed {
return true
}
}
return false
}
// postCompactionOverLimit reports whether the first assistant usage after the
// latest boundary reaches trigger's point. A disabled trigger never does.
func postCompactionOverLimit(messages []database.ChatMessage, trigger compactionTrigger) bool {
assistant, ok := firstUncompressedAssistantAfter(messages, latestContextBoundaryIndex(messages))
return ok && postCompactionAssistantOverLimit(assistant, trigger.thresholdPercent, trigger.contextLimit)
}
func postCompactionAssistantOverLimit(msg database.ChatMessage, thresholdPercent int32, triggerContextLimit int64) bool {
// Zero means "always compact." Excluding it from this check prevents a
// terminal over-limit error after every compaction.
if thresholdPercent <= 0 {
return false
}
return shouldCompactPromptUsage(usageFromMessage(msg), triggerContextLimit, thresholdPercent)
}
func usageFromMessage(msg database.ChatMessage) fantasy.Usage {
var usage fantasy.Usage
if msg.InputTokens.Valid {
usage.InputTokens = msg.InputTokens.Int64
}
if msg.OutputTokens.Valid {
usage.OutputTokens = msg.OutputTokens.Int64
}
if msg.TotalTokens.Valid {
usage.TotalTokens = msg.TotalTokens.Int64
}
if msg.ReasoningTokens.Valid {
usage.ReasoningTokens = msg.ReasoningTokens.Int64
}
if msg.CacheCreationTokens.Valid {
usage.CacheCreationTokens = msg.CacheCreationTokens.Int64
}
if msg.CacheReadTokens.Valid {
usage.CacheReadTokens = msg.CacheReadTokens.Int64
}
return usage
}
func historyHasStopAfterToolResult(messages []database.ChatMessage, stopAfterTools map[string]struct{}) (bool, error) {
if len(stopAfterTools) == 0 {
return false, nil
}
for _, msg := range messages[currentTurnStartIndex(messages):] {
if msg.Deleted || msg.Compressed || msg.Role != database.ChatMessageRoleTool {
continue
}
parts, err := chatprompt.ParseContent(msg)
if err != nil {
return false, xerrors.Errorf("parse tool message: %w", err)
}
for _, part := range parts {
if part.Type != codersdk.ChatMessagePartTypeToolResult || part.IsError {
continue
}
if _, ok := stopAfterTools[part.ToolName]; ok {
return true, nil
}
}
}
return false, nil
}
func currentHistoryComplete(messages []database.ChatMessage) (bool, error) {
idx := lastMessageIndex(messages, func(database.ChatMessage) bool { return true })
if idx == -1 || messages[idx].Role != database.ChatMessageRoleAssistant {
return false, nil
}
parts, err := chatprompt.ParseContent(messages[idx])
if err != nil {
return false, xerrors.Errorf("parse latest assistant message: %w", err)
}
for _, part := range parts {
if part.Type == codersdk.ChatMessagePartTypeToolCall && !part.ProviderExecuted {
return false, nil
}
}
return true, nil
}
func lastMessageIndex(messages []database.ChatMessage, accept func(database.ChatMessage) bool) int {
for i := len(messages) - 1; i >= 0; i-- {
if messages[i].Deleted || messages[i].Compressed {
continue
}
if accept(messages[i]) {
return i
}
}
return -1
}
func handledToolCallIDs(messages []database.ChatMessage) (map[string]bool, error) {
handled := make(map[string]bool)
for _, msg := range messages {
if msg.Deleted || msg.Compressed || msg.Role != database.ChatMessageRoleTool {
continue
}
parts, err := chatprompt.ParseContent(msg)
if err != nil {
return nil, xerrors.Errorf("parse tool message: %w", err)
}
for _, part := range parts {
if part.Type == codersdk.ChatMessagePartTypeToolResult && part.ToolCallID != "" {
handled[part.ToolCallID] = true
}
}
}
return handled, nil
}
type bufferedPartsToPartialMessagesInput struct {
parts []messagepartbuffer.Part
modelConfigID uuid.UUID
contentVersion int16
logger slog.Logger
interruptedAt time.Time
// attemptRuntime is the interrupted attempt's billable model
// invocation window: the span from the provider stream opening to
// the interrupt closing its buffer episode. It is persisted as
// runtime_ms on the first partial assistant message when the
// attempt streamed model-generated assistant content.
attemptRuntime time.Duration
}
type partialToolCall struct {
part codersdk.ChatMessagePart
index int
argsDelta strings.Builder
valid bool
durable bool
}
type partialToolResult struct {
part codersdk.ChatMessagePart
// streamed records that live result or reasoning deltas arrived
// without a durable result; they are stream-only and never persisted.
streamed bool
completed bool
}
func bufferedPartsToPartialMessages(input bufferedPartsToPartialMessagesInput) ([]chatstate.Message, error) {
contentVersion := input.contentVersion
if contentVersion == 0 {
contentVersion = chatprompt.CurrentContentVersion
}
parts := slices.Clone(input.parts)
slices.SortFunc(parts, func(a, b messagepartbuffer.Part) int {
return cmp.Compare(a.Seq, b.Seq)
})
state := partialMessageConversionState{
input: input,
contentVersion: contentVersion,
toolCalls: make(map[string]*partialToolCall),
toolResults: make(map[string]*partialToolResult),
answered: make(map[string]bool),
}
for _, buffered := range parts {
if err := state.consume(buffered); err != nil {
return nil, err
}
}
if err := state.finalizeToolCallPlaceholders(); err != nil {
return nil, err
}
if err := state.flushAssistant(); err != nil {
return nil, err
}
if err := state.flushAccumulatedToolResults(); err != nil {
return nil, err
}
if err := state.appendSyntheticInterruptionResults(); err != nil {
return nil, err
}
if input.attemptRuntime > 0 && state.modelStreamedAssistant {
// Usage reporting sums runtime_ms across rows, so placing the
// whole span on the first assistant message is sufficient.
for i := range state.messages {
if state.messages[i].Role != database.ChatMessageRoleAssistant {
continue
}
state.messages[i].RuntimeMs = nullInt64IfNonZero(input.attemptRuntime.Milliseconds())
break
}
}
return state.messages, nil
}
type partialMessageConversionState struct {
input bufferedPartsToPartialMessagesInput
contentVersion int16
messages []chatstate.Message
assistantParts []codersdk.ChatMessagePart
toolCalls map[string]*partialToolCall
toolCallOrder []string
toolResults map[string]*partialToolResult
toolResultOrder []string
answered map[string]bool
// modelStreamedAssistant distinguishes streamed content from tool
// attachment parts, which must not carry model runtime.
modelStreamedAssistant bool
}
func (s *partialMessageConversionState) consume(buffered messagepartbuffer.Part) error {
switch buffered.Role {
case codersdk.ChatMessageRoleAssistant:
s.consumeAssistantPart(buffered)
case codersdk.ChatMessageRoleTool:
return s.consumeToolPart(buffered)
default:
s.logSkippedPart(buffered, "unsupported buffered part role")
}
return nil
}
func (s *partialMessageConversionState) consumeAssistantPart(buffered messagepartbuffer.Part) {
part := buffered.MessagePart
if part.Type == "" {
s.logSkippedPart(buffered, "empty buffered assistant part type")
return
}
if part.Type != codersdk.ChatMessagePartTypeFile {
s.modelStreamedAssistant = true
}
if part.Type != codersdk.ChatMessagePartTypeToolCall {
if part.Type == codersdk.ChatMessagePartTypeReasoning &&
!s.input.interruptedAt.IsZero() {
interruptedAt := s.input.interruptedAt
if part.CreatedAt == nil {
part.CreatedAt = &interruptedAt
}
if part.CompletedAt == nil {
part.CompletedAt = &interruptedAt
}
}
s.assistantParts = append(s.assistantParts, part)
return
}
if part.ToolCallID == "" {
s.logSkippedPart(buffered, "tool call part missing tool call ID")
return
}
call := s.toolCall(part.ToolCallID)
call.part.Type = codersdk.ChatMessagePartTypeToolCall
call.part.ToolCallID = part.ToolCallID
if part.ToolName != "" {
call.part.ToolName = part.ToolName
}
if part.MCPServerConfigID.Valid {
call.part.MCPServerConfigID = part.MCPServerConfigID
}
if part.CreatedAt != nil {
call.part.CreatedAt = part.CreatedAt
}
call.part.ProviderExecuted = call.part.ProviderExecuted || part.ProviderExecuted
if part.ArgsDelta != "" {
if call.durable {
s.logSkippedPart(buffered, "tool call args delta arrived after full tool call")
return
}
_, _ = call.argsDelta.WriteString(part.ArgsDelta)
return
}
durable := part
durable.ArgsDelta = ""
if len(durable.Args) > 0 && !json.Valid(durable.Args) {
call.valid = false
s.assistantParts[call.index] = codersdk.ChatMessagePart{}
s.logSkippedPart(buffered, "tool call part has invalid durable args")
return
}
if call.durable {
s.logSkippedPart(buffered, "duplicate durable tool call part")
}
call.part = durable
call.valid = true
call.durable = true
s.assistantParts[call.index] = durable
}
func (s *partialMessageConversionState) consumeToolPart(buffered messagepartbuffer.Part) error {
part := buffered.MessagePart
if part.Type != codersdk.ChatMessagePartTypeToolResult {
s.logSkippedPart(buffered, "non tool-result part with tool role")
return nil
}
if part.ToolCallID == "" {
s.logSkippedPart(buffered, "tool result part missing tool call ID")
return nil
}
if part.ResultReset {
result := s.toolResult(part.ToolCallID)
result.part.ToolCallID = part.ToolCallID
result.part.ToolName = part.ToolName
result.streamed = false
s.logSkippedPart(buffered, "streaming tool result reset is not durable")
return nil
}
if part.ResultDelta != "" || part.ReasoningDelta != "" {
result := s.toolResult(part.ToolCallID)
result.part.ToolCallID = part.ToolCallID
if part.ToolName != "" {
result.part.ToolName = part.ToolName
}
if part.MCPServerConfigID.Valid {
result.part.MCPServerConfigID = part.MCPServerConfigID
}
if part.CreatedAt != nil {
result.part.CreatedAt = part.CreatedAt
}
result.part.ProviderExecuted = result.part.ProviderExecuted || part.ProviderExecuted
result.streamed = true
return nil
}
if err := s.finalizeToolCallPlaceholders(); err != nil {
return err
}
if !s.toolCallDurable(part.ToolCallID) {
s.logSkippedPart(buffered, "tool result has no matching durable tool call")
return nil
}
if len(part.Result) == 0 || !json.Valid(part.Result) {
s.logSkippedPart(buffered, "tool result part has invalid durable result")
return nil
}
if s.answered[part.ToolCallID] {
s.logSkippedPart(buffered, "duplicate durable tool result part")
return nil
}
part.ResultDelta = ""
part.ReasoningDelta = ""
part.ResultReset = false
if err := s.flushAssistant(); err != nil {
return err
}
if err := s.appendToolResult(part); err != nil {
return err
}
s.answered[part.ToolCallID] = true
return nil
}
func (s *partialMessageConversionState) toolCall(id string) *partialToolCall {
call := s.toolCalls[id]
if call != nil {
return call
}
call = &partialToolCall{index: len(s.assistantParts), valid: true}
s.toolCalls[id] = call
s.toolCallOrder = append(s.toolCallOrder, id)
s.assistantParts = append(s.assistantParts, codersdk.ChatMessagePart{})
return call
}
func (s *partialMessageConversionState) toolResult(id string) *partialToolResult {
result := s.toolResults[id]
if result != nil {
return result
}
result = &partialToolResult{}
s.toolResults[id] = result
s.toolResultOrder = append(s.toolResultOrder, id)
return result
}
func (s *partialMessageConversionState) finalizeToolCallPlaceholders() error {
for _, id := range s.toolCallOrder {
call := s.toolCalls[id]
if call == nil || call.durable || !call.valid {
continue
}
args := json.RawMessage(call.argsDelta.String())
if len(args) == 0 || !json.Valid(args) {
s.assistantParts[call.index] = codersdk.ChatMessagePart{}
call.valid = false
s.logSkippedPart(messagepartbuffer.Part{
Role: codersdk.ChatMessageRoleAssistant,
MessagePart: call.part,
}, "tool call args delta did not form durable JSON")
continue
}
call.part.Args = args
call.part.ArgsDelta = ""
call.durable = true
s.assistantParts[call.index] = call.part
}
return nil
}
func (s *partialMessageConversionState) flushAssistant() error {
if len(s.assistantParts) == 0 {
return nil
}
durable := make([]codersdk.ChatMessagePart, 0, len(s.assistantParts))
for _, part := range s.assistantParts {
if part.Type == "" {
continue
}
part.ArgsDelta = ""
part.ResultDelta = ""
part.ReasoningDelta = ""
part.ResultReset = false
durable = append(durable, part)
}
s.assistantParts = nil
if len(durable) == 0 {
return nil
}
content, err := chatprompt.MarshalParts(durable)
if err != nil {
return xerrors.Errorf("marshal partial assistant: %w", err)
}
s.messages = append(s.messages, baseMessage(database.ChatMessageRoleAssistant, database.ChatMessageVisibilityBoth, s.input.modelConfigID, s.contentVersion, content))
return nil
}
func (s *partialMessageConversionState) flushAccumulatedToolResults() error {
for _, id := range s.toolResultOrder {
if s.answered[id] {
continue
}
result := s.toolResults[id]
if result == nil || result.completed || !result.streamed {
continue
}
s.logSkippedPart(messagepartbuffer.Part{Role: codersdk.ChatMessageRoleTool, MessagePart: result.part}, "streaming tool result delta is not durable")
}
return nil
}
func (s *partialMessageConversionState) appendToolResult(part codersdk.ChatMessagePart) error {
content, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{part})
if err != nil {
return xerrors.Errorf("marshal partial tool result: %w", err)
}
s.messages = append(s.messages, baseMessage(database.ChatMessageRoleTool, database.ChatMessageVisibilityBoth, s.input.modelConfigID, s.contentVersion, content))
return nil
}
func (s *partialMessageConversionState) appendSyntheticInterruptionResults() error {
for _, id := range s.toolCallOrder {
if s.answered[id] {
continue
}
call := s.toolCalls[id]
if call == nil || !call.valid || !call.durable || call.part.ProviderExecuted {
continue
}
result, err := json.Marshal(map[string]string{"error": interruptedToolResultErrorMessage})
if err != nil {
return xerrors.Errorf("marshal synthetic interruption result: %w", err)
}
part := codersdk.ChatMessageToolResult(call.part.ToolCallID, call.part.ToolName, result, true, false)
part.MCPServerConfigID = call.part.MCPServerConfigID
if !s.input.interruptedAt.IsZero() {
part.CreatedAt = &s.input.interruptedAt
}
if err := s.appendToolResult(part); err != nil {
return xerrors.Errorf("marshal synthetic interruption message: %w", err)
}
s.answered[id] = true
}
return nil
}
func (s *partialMessageConversionState) toolCallDurable(id string) bool {
call := s.toolCalls[id]
return call != nil && call.valid && call.durable
}
func (s *partialMessageConversionState) logSkippedPart(buffered messagepartbuffer.Part, reason string) {
s.input.logger.Warn(context.Background(), "skipping buffered chat message part",
slog.F("reason", reason),
slog.F("role", buffered.Role),
slog.F("part_type", buffered.MessagePart.Type),
slog.F("tool_call_id", buffered.MessagePart.ToolCallID),
slog.F("tool_name", buffered.MessagePart.ToolName),
)
}