mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
Wires chat lifecycle hooks into chatd, gated by the `agent-lifecycle-hooks` experiment. Part of the lifecycle hooks stack (#27401, #27428, #27430). See `docs/admin/setup/chat-lifecycle-hooks.md` for the consumer-facing contract. ## Summary When a hook URL is configured, chatd dispatches `session_start`, `user_prompt_submit`, `pre_tool_use`, `post_tool_use`, `pre_compact`, `post_compact`, and `stop` events to the consumer and applies its responses. ## Design - **Stateless**: Coder stores no hook dispatch or decision state. Delivery is at least once; consumers deduplicate on stable payload identifiers (chat ID, event type, tool-use ID) and answer duplicates with the same decision. - **Admission-time prompt effects**: `user_prompt_submit` dispatches exactly once per submission (create, send, queue, edit, subagent spawn) and folds its effects into the stored prompt as typed message parts: original-or-overridden user parts, then model-only `hook-context`, then a user-visible `hook-notice`. Hook context is stripped from every client-facing conversion; hook notices are excluded from model prompts. The server rejects hook parts in client-submitted content. - **Tool gating**: `pre_tool_use` allow can override tool input; deny becomes a synthetic denied tool result, with any returned model context persisted as a model-only transcript row so it never reaches clients. The denial text identifies an external policy (the deployment's lifecycle hook) as the source and marks the decision as persistent, so the model explains the denial instead of retrying it or misreporting it as an infrastructure failure. - **Fail closed**: a dispatch failure rejects the triggering request or moves the chat to the error state in the same transaction as the affected step, so a runnable state is never published with unapproved content. - **Admission before persistence**: `pre_tool_use` is dispatched for the calls the model produced, before the assistant message is stored. See "Staged tool admission" below. - **Fresh dispatch per tool call**: every non-provider-executed tool call is decided by its own `pre_tool_use` dispatch; Coder never reuses an earlier decision on the consumer's behalf. Retries re-dispatch the same logical event. ## Structure All hook dispatch flows through one seam: entry points build a `chathooks.Chat` (chat identity) and a `chathooks.Message` (event details) and call `Trigger.Trigger`, the only component that talks to the dispatcher. The integration lives in the `coderd/x/chatd/chathooks` subpackage, split by responsibility: - `trigger.go`: the trigger seam; builds the wire envelope per event, normalizes deny into a typed error, and holds the package's single enabled-check. - `effects.go`: pure conversion of hook results into transcript rows and prompt parts. - `errors.go`: failure classification (dispatch error messages, denial mapping, tool-result dispatch-failure scanning). - `tooluse.go`: the tool-call gate (`pre_tool_use` preflight, `post_tool_use` payloads, applying admitted input to the step). Server-bound glue stays in `coderd/x/chatd/hook_server.go`: the chat-parking dispatch error handlers, the step-commit row insertion wrappers, and the dynamic post-tool-use state loader, which depends on chatd validation types. This PR adopts the `codersdk/x/agenthooks` and `coderd/x/agenthooks/dispatch` import paths introduced at the tip of #27401; intermediate commits still reference the pre-move paths and are not individually buildable. ## Staged tool admission `pre_tool_use` originally ran at tool execution time, which is after the assistant message carrying the tool call was already committed. An `input_override` therefore had to rewrite stored message content in place. @hugodutka pointed out that chatd treats message content as immutable, and that the rewrite was a shortcut rather than a requirement. It was also a correctness problem in its own right: the rewrite only updated the database, so the transcript could show one input while a different one had executed. The hook now runs before the step is persisted: ```text provider stream ends (tool calls complete, in memory) -> pre_tool_use dispatch per call -> ONE transaction: assistant row with admitted inputs, synthetic denials, hook rows -> execute ``` The step is inserted once, carrying the input the tool runs with. `UpdateChatMessageContentByID` and `Tx.UpdateMessageContent` are deleted from #27428, so message content stays immutable. Two consequences, both intentional: - **Clients converge rather than wait.** Tool-call parts still stream live, so a rewritten call briefly shows the model's proposed input before the committed message replaces it. The chat store already clears stream state when an assistant message arrives, so the stored input wins with no frontend change and no added latency before tool cards appear. - **A call already in history was already admitted.** Execution consumes the stored input instead of dispatching a second decision, which keeps one dispatch and one set of hook effects per call. A consumer policy change between admission and execution applies to later calls, not to calls already admitted. The per-chat debug endpoint still records the provider's original tool input. Its purpose is to report provider behavior, and it requires an explicit per-chat debug flag; the invariant here covers the transcript. ## Configuration Adds `chat-hook-url`, `chat-hook-secret`, `chat-hook-timeout`, and `chat-hook-enabled` deployment options with startup validation. The flags are hidden from `coder server --help` while the feature is experimental; the setup guide documents them. ## Tool input validation Built-in tool arguments reach a consumer as raw JSON with key spelling preserved, but the tools decode those bytes with Go, which matches struct fields case-insensitively and keeps the last match. A policy reading `path` could therefore authorize one value while the tool executed another, and a lone case variant such as `{"PATH":"/secret"}` was invisible to a policy checking for `path`. Coder now rejects a built-in tool call whose input repeats a key or spells a schema property with different capitalization, before the `pre_tool_use` dispatch, so a consumer is never asked to authorize bytes whose meaning depends on the reader. Rejected calls produce an error result the model can retry; unambiguous calls in the same batch still run. A consumer-authored `input_override` is rechecked after the dispatch and fails the turn closed, because the model cannot correct it. Dynamic and MCP inputs are excluded because the client and the workspace agent execute those calls rather than coderd. Two paths needed more than a schema check. Execution resolves a deprecated tool name to its canonical tool, so validation resolves aliases first. The `edit_files` decoder also reads `search` and `replace`, which its schema does not advertise, so those aliases are now matched exactly and their case variants ignored. A hook denial now returns a structured 403 carrying `kind: "hook_denied"`, mirroring the dispatch-failure response that already carries its own kind. Without it a client cannot tell a policy decision apart from a generic failure, and the chat UI titled a denial "Request failed". Adding a kind needs no migration: `ChatErrorKind` is persisted only inside the JSONB `chats.last_error` column, whose decoder accepts unknown kinds. The hook docs also correct the tool-input convergence window. A batch dispatches sequentially before the assistant row commits, so the original input stays visible for a span that scales with the number of tool calls in the step rather than a single hook timeout. > This PR was written by Mux, an AI coding agent, on Mike's behalf.
780 lines
25 KiB
Go
780 lines
25 KiB
Go
package codersdk_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/shopspring/decimal"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func TestChatModelProviderOptions_MarshalJSON_UsesPlainProviderPayload(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sendReasoning := true
|
|
thinkingDisplay := "summarized"
|
|
|
|
raw, err := json.Marshal(codersdk.ChatModelProviderOptions{
|
|
Anthropic: &codersdk.ChatModelAnthropicProviderOptions{
|
|
SendReasoning: &sendReasoning,
|
|
ThinkingDisplay: &thinkingDisplay,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
require.NotContains(t, string(raw), `"type":"anthropic.options"`)
|
|
require.NotContains(t, string(raw), `"data":`)
|
|
require.Contains(t, string(raw), `"send_reasoning":true`)
|
|
require.Contains(t, string(raw), `"thinking_display":"summarized"`)
|
|
}
|
|
|
|
func TestChatModelProviderOptions_UnmarshalJSON_ParsesPlainProviderPayloads(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
raw := []byte(`{
|
|
"anthropic": {
|
|
"send_reasoning": true,
|
|
"thinking_display": "summarized"
|
|
}
|
|
}`)
|
|
|
|
var decoded codersdk.ChatModelProviderOptions
|
|
err := json.Unmarshal(raw, &decoded)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.Anthropic)
|
|
require.NotNil(t, decoded.Anthropic.SendReasoning)
|
|
require.True(t, *decoded.Anthropic.SendReasoning)
|
|
require.NotNil(t, decoded.Anthropic.ThinkingDisplay)
|
|
require.Equal(t, "summarized", *decoded.Anthropic.ThinkingDisplay)
|
|
}
|
|
|
|
func TestChatUsageLimitExceededFrom(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("ExtractsTyped409", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
want := codersdk.ChatUsageLimitExceededResponse{
|
|
Response: codersdk.Response{Message: "Chat usage limit exceeded."},
|
|
SpentMicros: 123,
|
|
LimitMicros: 456,
|
|
ResetsAt: time.Date(2026, time.March, 16, 12, 0, 0, 0, time.UTC),
|
|
}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
require.Equal(t, http.MethodPost, r.Method)
|
|
require.Equal(t, "/api/experimental/chats", r.URL.Path)
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
rw.WriteHeader(http.StatusConflict)
|
|
require.NoError(t, json.NewEncoder(rw).Encode(want))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
serverURL, err := url.Parse(srv.URL)
|
|
require.NoError(t, err)
|
|
|
|
client := codersdk.NewExperimentalClient(codersdk.New(serverURL))
|
|
_, err = client.CreateChat(context.Background(), codersdk.CreateChatRequest{
|
|
Content: []codersdk.ChatInputPart{{
|
|
Type: codersdk.ChatInputPartTypeText,
|
|
Text: "hello",
|
|
}},
|
|
})
|
|
require.Error(t, err)
|
|
|
|
sdkErr, ok := codersdk.AsError(err)
|
|
require.True(t, ok)
|
|
require.Equal(t, http.StatusConflict, sdkErr.StatusCode())
|
|
require.Equal(t, want.Message, sdkErr.Message)
|
|
|
|
limitErr := codersdk.ChatUsageLimitExceededFrom(err)
|
|
require.NotNil(t, limitErr)
|
|
require.Equal(t, want, *limitErr)
|
|
})
|
|
|
|
t.Run("ReturnsNilForNonLimitErrors", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require.Nil(t, codersdk.ChatUsageLimitExceededFrom(codersdk.NewError(http.StatusConflict, codersdk.Response{Message: "plain conflict"})))
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
require.NoError(t, json.NewEncoder(rw).Encode(codersdk.Response{Message: "Invalid request."}))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
serverURL, err := url.Parse(srv.URL)
|
|
require.NoError(t, err)
|
|
|
|
client := codersdk.NewExperimentalClient(codersdk.New(serverURL))
|
|
_, err = client.CreateChat(context.Background(), codersdk.CreateChatRequest{
|
|
Content: []codersdk.ChatInputPart{{
|
|
Type: codersdk.ChatInputPartTypeText,
|
|
Text: "hello",
|
|
}},
|
|
})
|
|
require.Error(t, err)
|
|
|
|
sdkErr, ok := codersdk.AsError(err)
|
|
require.True(t, ok)
|
|
require.Equal(t, http.StatusBadRequest, sdkErr.StatusCode())
|
|
require.Nil(t, codersdk.ChatUsageLimitExceededFrom(err))
|
|
})
|
|
}
|
|
|
|
func TestChatErrorKind_JSONRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
terminal := codersdk.ChatError{
|
|
Message: "limit reached",
|
|
Kind: codersdk.ChatErrorKindUsageLimit,
|
|
}
|
|
data, err := json.Marshal(terminal)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(data), `"kind":"usage_limit"`)
|
|
|
|
var decodedTerminal codersdk.ChatError
|
|
require.NoError(t, json.Unmarshal(data, &decodedTerminal))
|
|
require.Equal(t, codersdk.ChatErrorKindUsageLimit, decodedTerminal.Kind)
|
|
|
|
retry := codersdk.ChatStreamRetry{
|
|
Attempt: 1,
|
|
Error: "retrying",
|
|
Kind: codersdk.ChatErrorKindUsageLimit,
|
|
}
|
|
data, err = json.Marshal(retry)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(data), `"kind":"usage_limit"`)
|
|
|
|
var decodedRetry codersdk.ChatStreamRetry
|
|
require.NoError(t, json.Unmarshal(data, &decodedRetry))
|
|
require.Equal(t, codersdk.ChatErrorKindUsageLimit, decodedRetry.Kind)
|
|
}
|
|
|
|
func TestChatStreamEvent_JSONRoundTripIncludesResetTypesAndPartMetadata(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chatID := uuid.New()
|
|
events := []codersdk.ChatStreamEvent{
|
|
{Type: codersdk.ChatStreamEventTypePreviewReset, ChatID: chatID},
|
|
{Type: codersdk.ChatStreamEventTypeHistoryReset, ChatID: chatID},
|
|
{
|
|
Type: codersdk.ChatStreamEventTypeMessagePart,
|
|
ChatID: chatID,
|
|
MessagePart: &codersdk.ChatStreamMessagePart{
|
|
Role: codersdk.ChatMessageRoleAssistant,
|
|
Part: codersdk.ChatMessageText("partial"),
|
|
HistoryVersion: 12,
|
|
GenerationAttempt: 3,
|
|
Seq: 4,
|
|
},
|
|
},
|
|
}
|
|
data, err := json.Marshal(events)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(data), `"type":"preview_reset"`)
|
|
require.Contains(t, string(data), `"type":"history_reset"`)
|
|
require.Contains(t, string(data), `"history_version":12`)
|
|
require.Contains(t, string(data), `"generation_attempt":3`)
|
|
require.Contains(t, string(data), `"seq":4`)
|
|
|
|
var decoded []codersdk.ChatStreamEvent
|
|
require.NoError(t, json.Unmarshal(data, &decoded))
|
|
require.Equal(t, codersdk.ChatStreamEventTypePreviewReset, decoded[0].Type)
|
|
require.Equal(t, codersdk.ChatStreamEventTypeHistoryReset, decoded[1].Type)
|
|
require.Equal(t, int64(12), decoded[2].MessagePart.HistoryVersion)
|
|
require.Equal(t, int64(3), decoded[2].MessagePart.GenerationAttempt)
|
|
require.Equal(t, int64(4), decoded[2].MessagePart.Seq)
|
|
}
|
|
|
|
func TestChatMessagePart_StripInternal(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("StripsProviderMetadata", func(t *testing.T) {
|
|
t.Parallel()
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeToolCall,
|
|
ToolCallID: "call-1",
|
|
ToolName: "some_tool",
|
|
Args: json.RawMessage(`{"key":"value"}`),
|
|
ProviderMetadata: json.RawMessage(`{"type":"ephemeral"}`),
|
|
}
|
|
part.StripInternal()
|
|
assert.Nil(t, part.ProviderMetadata)
|
|
// Public fields preserved.
|
|
assert.Equal(t, codersdk.ChatMessagePartTypeToolCall, part.Type)
|
|
assert.Equal(t, "call-1", part.ToolCallID)
|
|
assert.Equal(t, "some_tool", part.ToolName)
|
|
assert.JSONEq(t, `{"key":"value"}`, string(part.Args))
|
|
})
|
|
|
|
t.Run("StripsFileDataWhenFileIDSet", func(t *testing.T) {
|
|
t.Parallel()
|
|
id := uuid.New()
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeFile,
|
|
FileID: uuid.NullUUID{UUID: id, Valid: true},
|
|
MediaType: "image/png",
|
|
Data: []byte("binary-payload"),
|
|
}
|
|
part.StripInternal()
|
|
assert.Nil(t, part.Data)
|
|
assert.Equal(t, id, part.FileID.UUID)
|
|
assert.Equal(t, "image/png", part.MediaType)
|
|
})
|
|
|
|
t.Run("PreservesDataWhenNoFileID", func(t *testing.T) {
|
|
t.Parallel()
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeFile,
|
|
MediaType: "image/png",
|
|
Data: []byte("inline-data"),
|
|
}
|
|
part.StripInternal()
|
|
assert.Equal(t, []byte("inline-data"), part.Data)
|
|
})
|
|
|
|
t.Run("StripsContextFileContent", func(t *testing.T) {
|
|
t.Parallel()
|
|
agentID := uuid.New()
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeContextFile,
|
|
ContextFilePath: "/home/coder/AGENTS.md",
|
|
ContextFileContent: "large content",
|
|
ContextFileAgentID: uuid.NullUUID{UUID: agentID, Valid: true},
|
|
ContextFileOS: "linux",
|
|
ContextFileDirectory: "/home/coder/project",
|
|
ContextFileSkillMetaFile: "CUSTOM.md",
|
|
}
|
|
part.StripInternal()
|
|
// Internal fields stripped.
|
|
assert.Empty(t, part.ContextFileContent)
|
|
assert.Empty(t, part.ContextFileOS)
|
|
assert.Empty(t, part.ContextFileDirectory)
|
|
assert.Empty(t, part.ContextFileSkillMetaFile)
|
|
// Public fields preserved.
|
|
assert.Equal(t, "/home/coder/AGENTS.md", part.ContextFilePath)
|
|
assert.Equal(t, agentID, part.ContextFileAgentID.UUID)
|
|
assert.True(t, part.ContextFileAgentID.Valid)
|
|
})
|
|
|
|
t.Run("NoopOnCleanPart", func(t *testing.T) {
|
|
t.Parallel()
|
|
part := codersdk.ChatMessageText("hello")
|
|
part.StripInternal()
|
|
assert.Equal(t, "hello", part.Text)
|
|
assert.Equal(t, codersdk.ChatMessagePartTypeText, part.Type)
|
|
})
|
|
}
|
|
|
|
func TestChatModelReasoningEffortConfigEnumTags(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
want := strings.Join(codersdk.ChatModelReasoningEffortValues(), ",")
|
|
typ := reflect.TypeOf(codersdk.ChatModelReasoningEffortConfig{})
|
|
for _, fieldName := range []string{"Default", "Max"} {
|
|
field, ok := typ.FieldByName(fieldName)
|
|
require.True(t, ok)
|
|
require.Equal(t, want, field.Tag.Get("enum"))
|
|
}
|
|
}
|
|
|
|
// TestChatMessagePartVariantTags validates the `variants` struct tags
|
|
// on ChatMessagePart fields. Every field must either declare variant
|
|
// membership or be explicitly excluded, and every known part type
|
|
// must appear in at least one tag.
|
|
//
|
|
// If this test fails, edit the variants struct tags on ChatMessagePart
|
|
// in codersdk/chats.go.
|
|
func TestChatMessagePartVariantTags(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const editHint = "edit the variants struct tags on ChatMessagePart in codersdk/chats.go"
|
|
|
|
// Fields intentionally excluded from all generated variants.
|
|
// If you add a new field to ChatMessagePart, either add a
|
|
// variants tag or add it here with a comment explaining why.
|
|
excludedFields := map[string]string{
|
|
"type": "discriminant, added automatically by codegen",
|
|
"provider_metadata": "internal only, stripped by db2sdk before API responses",
|
|
"context_file_content": "internal only, stripped before API responses (typescript:\"-\")",
|
|
"context_file_os": "internal only, used during prompt expansion (typescript:\"-\")",
|
|
"context_file_directory": "internal only, used during prompt expansion (typescript:\"-\")",
|
|
"skill_dir": "internal only, used by read_skill tools (typescript:\"-\")",
|
|
"context_file_skill_meta_file": "internal only, restored on subsequent turns (typescript:\"-\")",
|
|
}
|
|
// Part types intentionally excluded from all generated variants.
|
|
// If you add a new part type, either reference it in a variants
|
|
// tag or add it here with a reason.
|
|
excludedTypes := map[codersdk.ChatMessagePartType]string{
|
|
codersdk.ChatMessagePartTypeHookContext: "internal only, stripped from client-facing conversions by db2sdk",
|
|
}
|
|
knownTypes := make(map[codersdk.ChatMessagePartType]bool)
|
|
for _, pt := range codersdk.AllChatMessagePartTypes() {
|
|
knownTypes[pt] = true
|
|
}
|
|
|
|
// Parse all variants tags from the struct and validate them.
|
|
typ := reflect.TypeOf(codersdk.ChatMessagePart{})
|
|
coveredTypes := make(map[codersdk.ChatMessagePartType]bool)
|
|
|
|
for i := range typ.NumField() {
|
|
f := typ.Field(i)
|
|
jsonTag := f.Tag.Get("json")
|
|
if jsonTag == "" || jsonTag == "-" {
|
|
continue
|
|
}
|
|
jsonName, _, _ := strings.Cut(jsonTag, ",")
|
|
|
|
varTag := f.Tag.Get("variants")
|
|
if varTag == "" {
|
|
assert.Contains(t, excludedFields, jsonName,
|
|
"field %s (json:%q) has no variants tag and is not in excludedFields; %s",
|
|
f.Name, jsonName, editHint)
|
|
continue
|
|
}
|
|
|
|
assert.NotEqual(t, "type", jsonName,
|
|
"the discriminant field must not have a variants tag; %s", editHint)
|
|
|
|
for _, entry := range strings.Split(varTag, ",") {
|
|
typeLit := codersdk.ChatMessagePartType(strings.TrimSuffix(entry, "?"))
|
|
|
|
assert.True(t, knownTypes[typeLit],
|
|
"field %s variants tag references unknown type %q; %s",
|
|
f.Name, typeLit, editHint)
|
|
|
|
coveredTypes[typeLit] = true
|
|
}
|
|
}
|
|
|
|
// Every known type must appear in at least one variants tag
|
|
// unless it is intentionally excluded from client codegen.
|
|
for pt := range knownTypes {
|
|
if _, excluded := excludedTypes[pt]; excluded {
|
|
assert.False(t, coveredTypes[pt],
|
|
"ChatMessagePartType %q is in excludedTypes but referenced by a variants tag; %s", pt, editHint)
|
|
continue
|
|
}
|
|
assert.True(t, coveredTypes[pt],
|
|
"ChatMessagePartType %q is not referenced by any variants tag; %s", pt, editHint)
|
|
}
|
|
|
|
// Enforce the omitempty <-> variants invariant:
|
|
// required in any variant => must NOT have omitempty
|
|
// optional in all variants => MUST have omitempty
|
|
// See the struct comment on ChatMessagePart for rationale.
|
|
t.Run("omitempty must match variant optionality", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
typ := reflect.TypeOf(codersdk.ChatMessagePart{})
|
|
for i := range typ.NumField() {
|
|
f := typ.Field(i)
|
|
varTag := f.Tag.Get("variants")
|
|
if varTag == "" {
|
|
continue
|
|
}
|
|
|
|
allOptional := true
|
|
for _, entry := range strings.Split(varTag, ",") {
|
|
if !strings.HasSuffix(entry, "?") {
|
|
allOptional = false
|
|
break
|
|
}
|
|
}
|
|
|
|
jsonTag := f.Tag.Get("json")
|
|
hasOmitEmpty := strings.Contains(jsonTag, "omitempty")
|
|
|
|
if !allOptional {
|
|
assert.False(t, hasOmitEmpty,
|
|
"field %s is required in at least one variant but has omitempty in its json tag; "+
|
|
"remove omitempty so Go does not silently drop the zero value that TypeScript expects to always be present",
|
|
f.Name)
|
|
} else {
|
|
assert.True(t, hasOmitEmpty,
|
|
"field %s is optional in all variants but is missing omitempty in its json tag; "+
|
|
"add omitempty to avoid sending zero values for fields the frontend does not expect",
|
|
f.Name)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestChatMessagePart_CreatedAt_JSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("RoundTrips", func(t *testing.T) {
|
|
t.Parallel()
|
|
ts := time.Date(2025, 6, 15, 12, 30, 0, 0, time.UTC)
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeToolCall,
|
|
ToolCallID: "tc-1",
|
|
ToolName: "execute",
|
|
CreatedAt: &ts,
|
|
}
|
|
data, err := json.Marshal(part)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(data), `"created_at"`)
|
|
|
|
var decoded codersdk.ChatMessagePart
|
|
err = json.Unmarshal(data, &decoded)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.CreatedAt)
|
|
require.True(t, ts.Equal(*decoded.CreatedAt))
|
|
})
|
|
|
|
t.Run("OmittedWhenNil", func(t *testing.T) {
|
|
t.Parallel()
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeToolCall,
|
|
ToolCallID: "tc-1",
|
|
ToolName: "execute",
|
|
}
|
|
data, err := json.Marshal(part)
|
|
require.NoError(t, err)
|
|
require.NotContains(t, string(data), `"created_at"`)
|
|
})
|
|
}
|
|
|
|
func TestChatMessagePart_ReasoningTimestamps_JSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("RoundTrips", func(t *testing.T) {
|
|
t.Parallel()
|
|
startedAt := time.Date(2025, 6, 15, 12, 30, 0, 0, time.UTC)
|
|
completedAt := startedAt.Add(2 * time.Second)
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeReasoning,
|
|
Text: "thinking out loud",
|
|
CreatedAt: &startedAt,
|
|
CompletedAt: &completedAt,
|
|
}
|
|
data, err := json.Marshal(part)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(data), `"created_at"`)
|
|
require.Contains(t, string(data), `"completed_at"`)
|
|
|
|
var decoded codersdk.ChatMessagePart
|
|
err = json.Unmarshal(data, &decoded)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.CreatedAt)
|
|
require.NotNil(t, decoded.CompletedAt)
|
|
require.True(t, startedAt.Equal(*decoded.CreatedAt))
|
|
require.True(t, completedAt.Equal(*decoded.CompletedAt))
|
|
})
|
|
|
|
t.Run("OmittedWhenNil", func(t *testing.T) {
|
|
t.Parallel()
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeReasoning,
|
|
Text: "thinking out loud",
|
|
}
|
|
data, err := json.Marshal(part)
|
|
require.NoError(t, err)
|
|
require.NotContains(t, string(data), `"created_at"`)
|
|
require.NotContains(t, string(data), `"completed_at"`)
|
|
})
|
|
|
|
t.Run("LegacyCreatedAtWithoutCompletedAt", func(t *testing.T) {
|
|
t.Parallel()
|
|
// CompletedAt is omitted on messages persisted before this
|
|
// feature shipped. Confirm round-trip leaves CompletedAt nil
|
|
// while preserving CreatedAt so legacy data does not break
|
|
// API consumers.
|
|
startedAt := time.Date(2025, 6, 15, 12, 30, 0, 0, time.UTC)
|
|
part := codersdk.ChatMessagePart{
|
|
Type: codersdk.ChatMessagePartTypeReasoning,
|
|
Text: "legacy reasoning",
|
|
CreatedAt: &startedAt,
|
|
}
|
|
data, err := json.Marshal(part)
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(data), `"created_at"`)
|
|
require.NotContains(t, string(data), `"completed_at"`)
|
|
|
|
var decoded codersdk.ChatMessagePart
|
|
err = json.Unmarshal(data, &decoded)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.CreatedAt)
|
|
require.Nil(t, decoded.CompletedAt)
|
|
})
|
|
}
|
|
|
|
func TestModelCostConfig_LegacyNumericJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var decoded codersdk.ModelCostConfig
|
|
err := json.Unmarshal([]byte("{\"input_price_per_million_tokens\": 1.5}"), &decoded)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.InputPricePerMillionTokens)
|
|
require.True(t, decoded.InputPricePerMillionTokens.Equal(decimal.RequireFromString("1.5")))
|
|
}
|
|
|
|
func TestModelCostConfig_QuotedDecimalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var decoded codersdk.ModelCostConfig
|
|
err := json.Unmarshal([]byte("{\"input_price_per_million_tokens\": \"1.5\"}"), &decoded)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.InputPricePerMillionTokens)
|
|
require.True(t, decoded.InputPricePerMillionTokens.Equal(decimal.RequireFromString("1.5")))
|
|
}
|
|
|
|
func TestModelCostConfig_NilVsZero(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zero := decimal.Zero
|
|
raw, err := json.Marshal(struct {
|
|
Nil codersdk.ModelCostConfig `json:"nil"`
|
|
Zero codersdk.ModelCostConfig `json:"zero"`
|
|
}{
|
|
Nil: codersdk.ModelCostConfig{},
|
|
Zero: codersdk.ModelCostConfig{InputPricePerMillionTokens: &zero},
|
|
})
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(raw), "\"zero\":{\"input_price_per_million_tokens\":\"0\"}")
|
|
require.Contains(t, string(raw), "\"nil\":{}")
|
|
}
|
|
|
|
func TestChatModelCallConfig_UnmarshalLegacyPricing(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var decoded codersdk.ChatModelCallConfig
|
|
err := json.Unmarshal([]byte("{\"input_price_per_million_tokens\": 1.5}"), &decoded)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.Cost)
|
|
require.NotNil(t, decoded.Cost.InputPricePerMillionTokens)
|
|
require.True(t, decoded.Cost.InputPricePerMillionTokens.Equal(decimal.RequireFromString("1.5")))
|
|
}
|
|
|
|
func TestChatModelCallConfig_UnmarshalStrict(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var decoded codersdk.ChatModelCallConfig
|
|
err := decoded.UnmarshalStrict([]byte(`{
|
|
"temperature": 0.5,
|
|
"cost": {"input_price_per_million_tokens": "5"},
|
|
"input_price_per_million_tokens": 1.5,
|
|
"provider_options": {"anthropic": {"thinking": {"budget_tokens": 1024}}}
|
|
}`))
|
|
require.NoError(t, err)
|
|
require.NotNil(t, decoded.Temperature)
|
|
require.True(t, decoded.Cost.InputPricePerMillionTokens.Equal(decimal.RequireFromString("5")))
|
|
|
|
err = decoded.UnmarshalStrict([]byte(`{"provider_options": {"anthropic": {"bogus_setting": true}}}`))
|
|
require.ErrorContains(t, err, `unknown field "bogus_setting"`)
|
|
|
|
// Trailing data after the first value is rejected, matching json.Unmarshal.
|
|
err = decoded.UnmarshalStrict([]byte(`{"temperature": 0.5} {"bogus_setting": true}`))
|
|
require.ErrorContains(t, err, "trailing data")
|
|
|
|
// UnmarshalJSON stays lenient.
|
|
require.NoError(t, json.Unmarshal([]byte(`{"bogus_setting": true}`), &decoded))
|
|
}
|
|
|
|
func TestChatCostSummary_JSONRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
original := codersdk.ChatCostSummary{
|
|
TotalCostMicros: 123,
|
|
}
|
|
raw, err := json.Marshal(original)
|
|
require.NoError(t, err)
|
|
|
|
var decoded codersdk.ChatCostSummary
|
|
err = json.Unmarshal(raw, &decoded)
|
|
require.NoError(t, err)
|
|
require.Equal(t, original.TotalCostMicros, decoded.TotalCostMicros)
|
|
}
|
|
|
|
// TestChat_JSONRoundTrip verifies that every field of codersdk.Chat
|
|
// survives a JSON marshal/unmarshal cycle. This catches omitempty
|
|
// silently eating zero-ish values, struct tag typos, and similar
|
|
// serialization bugs in the pubsub path.
|
|
func TestChat_JSONRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := time.Now().UTC().Truncate(time.Microsecond)
|
|
prState := "open"
|
|
prTitle := "test PR"
|
|
authorLogin := "testuser"
|
|
avatarURL := "https://example.com/avatar.png"
|
|
baseBranch := "main"
|
|
headBranch := "feature/test"
|
|
prNumber := int32(42)
|
|
commits := int32(3)
|
|
approved := true
|
|
reviewerCount := int32(2)
|
|
refreshedAt := now
|
|
staleAt := now.Add(time.Hour)
|
|
lastError := &codersdk.ChatError{
|
|
Message: "boom",
|
|
Detail: "provider detail",
|
|
Kind: codersdk.ChatErrorKindGeneric,
|
|
Provider: "openai",
|
|
Retryable: true,
|
|
StatusCode: 503,
|
|
}
|
|
prURL := "https://github.com/coder/coder/pull/42"
|
|
workspaceID := uuid.New()
|
|
buildID := uuid.New()
|
|
agentID := uuid.New()
|
|
parentChatID := uuid.New()
|
|
rootChatID := uuid.New()
|
|
|
|
original := codersdk.Chat{
|
|
ID: uuid.New(),
|
|
OwnerID: uuid.New(),
|
|
WorkspaceID: &workspaceID,
|
|
BuildID: &buildID,
|
|
AgentID: &agentID,
|
|
ParentChatID: &parentChatID,
|
|
RootChatID: &rootChatID,
|
|
LastModelConfigID: uuid.New(),
|
|
Title: "round-trip-test",
|
|
Status: codersdk.ChatStatusRunning,
|
|
LastError: lastError,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
Archived: true,
|
|
MCPServerIDs: []uuid.UUID{uuid.New()},
|
|
Labels: map[string]string{"env": "prod"},
|
|
DiffStatus: &codersdk.ChatDiffStatus{
|
|
ChatID: uuid.New(),
|
|
URL: &prURL,
|
|
PullRequestState: &prState,
|
|
PullRequestTitle: prTitle,
|
|
PullRequestDraft: true,
|
|
ChangesRequested: true,
|
|
Additions: 10,
|
|
Deletions: 5,
|
|
ChangedFiles: 3,
|
|
AuthorLogin: &authorLogin,
|
|
AuthorAvatarURL: &avatarURL,
|
|
BaseBranch: &baseBranch,
|
|
HeadBranch: &headBranch,
|
|
PRNumber: &prNumber,
|
|
Commits: &commits,
|
|
Approved: &approved,
|
|
ReviewerCount: &reviewerCount,
|
|
RefreshedAt: &refreshedAt,
|
|
StaleAt: &staleAt,
|
|
},
|
|
}
|
|
|
|
data, err := json.Marshal(original)
|
|
require.NoError(t, err)
|
|
|
|
var decoded codersdk.Chat
|
|
err = json.Unmarshal(data, &decoded)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, original, decoded)
|
|
}
|
|
|
|
func TestNewDynamicTool(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
type testArgs struct {
|
|
Query string `json:"query"`
|
|
}
|
|
|
|
t.Run("CorrectSchema", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tool := codersdk.NewDynamicTool(
|
|
"search", "search things",
|
|
func(_ context.Context, args testArgs, _ codersdk.DynamicToolCall) (codersdk.DynamicToolResponse, error) {
|
|
return codersdk.DynamicToolResponse{Content: args.Query}, nil
|
|
},
|
|
)
|
|
|
|
require.Equal(t, "search", tool.Name)
|
|
require.Equal(t, "search things", tool.Description)
|
|
require.Contains(t, string(tool.InputSchema), `"query"`)
|
|
require.Contains(t, string(tool.InputSchema), `"string"`)
|
|
})
|
|
|
|
t.Run("HandlerReceivesArgs", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var received testArgs
|
|
tool := codersdk.NewDynamicTool(
|
|
"search", "search things",
|
|
func(_ context.Context, args testArgs, _ codersdk.DynamicToolCall) (codersdk.DynamicToolResponse, error) {
|
|
received = args
|
|
return codersdk.DynamicToolResponse{Content: "ok"}, nil
|
|
},
|
|
)
|
|
|
|
resp, err := tool.Handler(context.Background(), codersdk.DynamicToolCall{
|
|
Args: `{"query":"hello"}`,
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, "ok", resp.Content)
|
|
require.Equal(t, "hello", received.Query)
|
|
})
|
|
|
|
t.Run("InvalidJSONArgs", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tool := codersdk.NewDynamicTool(
|
|
"search", "search things",
|
|
func(_ context.Context, args testArgs, _ codersdk.DynamicToolCall) (codersdk.DynamicToolResponse, error) {
|
|
return codersdk.DynamicToolResponse{Content: "should not reach"}, nil
|
|
},
|
|
)
|
|
|
|
resp, err := tool.Handler(context.Background(), codersdk.DynamicToolCall{
|
|
Args: "not-json",
|
|
})
|
|
require.NoError(t, err)
|
|
require.True(t, resp.IsError)
|
|
require.Contains(t, resp.Content, "invalid parameters")
|
|
})
|
|
}
|
|
|
|
//nolint:tparallel,paralleltest
|
|
func TestParseChatWorkspaceTTL(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want time.Duration
|
|
wantErr bool
|
|
}{
|
|
{"Empty_ReturnsDefault", "", 0, false},
|
|
{"ValidDuration_Hours", "2h", 2 * time.Hour, false},
|
|
{"ValidDuration_HoursAndMinutes", "2h30m", 2*time.Hour + 30*time.Minute, false},
|
|
{"ValidDuration_Minutes", "90m", 90 * time.Minute, false},
|
|
{"Zero", "0s", 0, false},
|
|
{"Negative", "-1h", 0, true},
|
|
{"Invalid", "not-a-duration", 0, true},
|
|
{"LargeDuration", "720h", 720 * time.Hour, false},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := codersdk.ParseChatWorkspaceTTL(tc.input)
|
|
if tc.wantErr {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.want, got)
|
|
})
|
|
}
|
|
}
|