Files
coder/coderd/x/chatd/memory_index_test.go
T
Michael Suchacz 0968f97a43 feat: move a chat family between projects in chatd (#30893)
chatd can now move a root chat, together with its subagent chats, into a
project, to another project, or out of its project. Nothing calls this
yet. #30894 adds the API.

**Stack** (moving chats between projects, bottom first):
1. **This PR**: the move in chatd
2. #30894: `PATCH /api/v2/chats/{chat}` takes `project_id`
3. #30895: **Move to project** in the chat menu
4. #30795: drag a chat onto a project folder in the sidebar

**Why:** today a chat keeps the project it was created with. A move must
follow the family rules of archiving, because a step that runs during a
move would still have the memory tools of the old project.

- `chatstate.Tx.SetProject` runs only from W, E0 and E1, like
`SetArchived`. `SetFamilyProject()` locks the root row before its
subagents, in the same order as archiving, so a busy family gets
`ErrTransitionNotAllowed` and nothing changes.
- A move to the project the chat is already in changes nothing, even
while the chat runs. The check reads the locked root row, so it waits
for a move that is still in flight.
- Only the root row stores `project_id`, and subagents use the project
of the root. After the commit, chatd publishes a new `project_change`
watch event for each chat of the family. The move keeps `updated_at`, so
lists keep their order.
- A root chat outside a project now has an empty memory index. When a
chat leaves its project, or its project is deleted, its next turn start
commits one memory index update that removes the memories the model saw
there.

**What to scrutinize:** the lock order and the same-project check under
the lock in `SetFamilyProject()` (`chatstate/family.go`), the turn-start
condition in `memoryIndexMessage()` (`memory.go`), and whether the
`ARCHITECTURE.md` text matches them.

**Generated** (`make gen`): `queries.sql.go`, `querier.go`, `dbmock` and
`dbmetrics` come from `queries/chats.sql`. `coderd/apidoc`,
`docs/reference/api/schemas.md` and `typesGenerated.ts` come from the
new watch event kind, which is also why two `chats.test.ts` fixtures
gain `project_change: false`.

**Architecture doc:** `coderd/x/chatd/ARCHITECTURE.md` lists
`SetProject` among the transitions and in the execution state diagram
(W, E0 and E1 back to themselves). It also says that a root chat's
project can change after creation, and that a moved chat's next turn
starts with a memory index update from the old project to the new one.

## Testing

- `make pre-commit` passes. `go test` passes for
`coderd/x/chatd/chatstate`, `coderd/database/dbauthz`
(`TestMethodTestSuite`) and the chatd memory tests.
- `TestSetFamilyProjectLocksRootFirst` fails with 55P03 when a subagent
row is locked first. `TestSetFamilyProjectKeepsTheCurrentProject` covers
the same-project move during a run. The transition matrix test gains
`SetProject` cases for W, E0 and E1.
- `TestMovedChatUsesNewProject` runs two real turns around a move: the
second turn uses the instructions and memories of the new project, and
`OutOfProject` fails without the memory index removal.

> Xum opened this PR on behalf of @ibetitsmike.
2026-10-19 12:38:17 +00:00

237 lines
8.7 KiB
Go

package chatd_test
import (
"strings"
"sync"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/x/chatd"
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
// TestProjectMemoryIndexKeepsPromptPrefixStable drives a project chat through
// a memory write and a second turn, and checks that the memory index reaches
// the model as appended history while tool definitions and earlier messages,
// the provider's cached prefix, stay byte-identical.
func TestProjectMemoryIndexKeepsPromptPrefixStable(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
db, ps := dbtestutil.NewDB(t)
var (
mu sync.Mutex
requests []*chattest.OpenAIRequest
)
openAIURL := chattest.NewOpenAI(t, func(req *chattest.OpenAIRequest) chattest.OpenAIResponse {
if !req.Stream {
return chattest.OpenAINonStreamingResponse("title")
}
mu.Lock()
requests = append(requests, req)
n := len(requests)
mu.Unlock()
if n == 1 {
chunk := chattest.OpenAIToolCallChunk(chattool.SaveMemoryToolName, `{"name":"deploy-day","description":"Deploys happen on Tuesdays","body":"The team deploys every Tuesday."}`)
chunk.Choices[0].ToolCalls[0].ID = "call_save"
return chattest.OpenAIStreamingResponse(chunk)
}
return chattest.OpenAIStreamingResponse(chattest.OpenAITextChunks("ok")...)
})
user, org, model := seedChatDependenciesWithProvider(t, db, "openai-compat", openAIURL)
project := dbgen.ChatProject(t, db, database.ChatProject{OrganizationID: org.ID, OwnerID: user.ID, Name: "platform"})
dbgen.ChatProjectMemory(t, db, database.ChatProjectMemory{
ProjectID: project.ID, OrganizationID: org.ID, CreatedBy: user.ID,
Name: "release-owner", Description: "Alice owns releases", Body: "Alice owns releases.",
})
server := newActiveTestServer(t, db, ps, func(cfg *chatd.Config) {
cfg.AIBridgeTransportFactory = chatAIGatewayTransportFactoryPointer(chattest.NewMockAIBridgeTransport(t, openAIURL))
})
chat, err := server.CreateChat(ctx, chatd.CreateOptions{
OrganizationID: org.ID,
OwnerID: user.ID,
ProjectID: uuid.NullUUID{UUID: project.ID, Valid: true},
Title: "memory-index",
ModelConfigID: model.ID,
InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("remember deploy day")},
})
require.NoError(t, err)
waitForChatStatus(ctx, t, db, chat.ID, database.ChatStatusWaiting)
_, err = server.SendMessage(ctx, chatd.SendMessageOptions{
ChatID: chat.ID,
CreatedBy: user.ID,
Content: []codersdk.ChatMessagePart{codersdk.ChatMessageText("what do you know?")},
})
require.NoError(t, err)
waitForChatStatus(ctx, t, db, chat.ID, database.ChatStatusWaiting)
mu.Lock()
defer mu.Unlock()
require.Len(t, requests, 3, "save step, answer step, second turn")
snapshot := chattool.FormatMemoryIndexSnapshot([]chattool.MemoryIndexEntry{{Name: "release-owner", Description: "Alice owns releases"}})
update := chattool.FormatMemoryIndexUpdate([]chattool.MemoryIndexEntry{{Name: "deploy-day", Description: "Deploys happen on Tuesdays"}}, nil)
count := func(req *chattest.OpenAIRequest, text string) int {
n := 0
for _, msg := range req.Messages {
if strings.Contains(msg.Content, text) {
n++
}
}
return n
}
for i, req := range requests {
require.Equal(t, 1, count(req, snapshot), "request %d carries the turn-one snapshot once", i)
// Tool definitions lead the cached prefix and must not change when a
// memory is written.
require.Equal(t, requests[0].Tools, req.Tools, "request %d tools", i)
// Every earlier message is replayed unchanged.
prev := requests[max(i-1, 0)]
require.GreaterOrEqual(t, len(req.Messages), len(prev.Messages))
require.Equal(t, prev.Messages, req.Messages[:len(prev.Messages)], "request %d history prefix", i)
}
// The write in turn one reaches the model at the start of turn two, not
// mid-turn.
require.Zero(t, count(requests[1], update))
require.Equal(t, 1, count(requests[2], update))
promptRows, err := db.GetChatMessagesForPromptByChatID(ctx, chat.ID)
require.NoError(t, err)
var indexRows int
for _, msg := range promptRows {
if msg.Visibility == database.ChatMessageVisibilityModel && msg.Role == database.ChatMessageRoleUser {
indexRows++
}
}
require.Equal(t, 2, indexRows, "one snapshot and one update")
}
// TestMovedChatUsesNewProject moves a chat between two turns and checks that
// the next turn uses the instructions and the memory of the chat's new
// project, or of no project once the chat leaves its project.
func TestMovedChatUsesNewProject(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
// toBilling moves the chat to Billing instead of out of any project.
toBilling bool
// want are texts that appear once in the second turn's request.
want []string
}{
{
name: "ToProject",
toBilling: true,
// The model saw the Platform index in turn one, so turn two adds
// the Billing memories and removes the Platform ones.
want: []string{
`in the project "Billing"`,
"Write all dates in ISO 8601.",
chattool.FormatMemoryIndexUpdate(
[]chattool.MemoryIndexEntry{{Name: "invoice-format", Description: "Invoices use ISO dates"}},
[]string{"deploy-day"},
),
},
},
{
name: "OutOfProject",
want: []string{chattool.FormatMemoryIndexUpdate(nil, []string{"deploy-day"})},
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
db, ps := dbtestutil.NewDB(t)
var (
mu sync.Mutex
requests []*chattest.OpenAIRequest
)
openAIURL := chattest.NewOpenAI(t, func(req *chattest.OpenAIRequest) chattest.OpenAIResponse {
if !req.Stream {
return chattest.OpenAINonStreamingResponse("title")
}
mu.Lock()
requests = append(requests, req)
mu.Unlock()
return chattest.OpenAIStreamingResponse(chattest.OpenAITextChunks("ok")...)
})
user, org, model := seedChatDependenciesWithProvider(t, db, "openai-compat", openAIURL)
platform := dbgen.ChatProject(t, db, database.ChatProject{OrganizationID: org.ID, OwnerID: user.ID, Name: "Platform"})
billing := dbgen.ChatProject(t, db, database.ChatProject{OrganizationID: org.ID, OwnerID: user.ID, Name: "Billing"})
_, err := db.UpsertChatProjectInstructions(ctx, database.UpsertChatProjectInstructionsParams{
ProjectID: billing.ID,
Instructions: "Write all dates in ISO 8601.",
UpdatedBy: user.ID,
})
require.NoError(t, err)
dbgen.ChatProjectMemory(t, db, database.ChatProjectMemory{
ProjectID: platform.ID, OrganizationID: org.ID, CreatedBy: user.ID,
Name: "deploy-day", Description: "Deploys happen on Tuesdays", Body: "The team deploys every Tuesday.",
})
dbgen.ChatProjectMemory(t, db, database.ChatProjectMemory{
ProjectID: billing.ID, OrganizationID: org.ID, CreatedBy: user.ID,
Name: "invoice-format", Description: "Invoices use ISO dates", Body: "Invoices use ISO 8601 dates.",
})
server := newActiveTestServer(t, db, ps, func(cfg *chatd.Config) {
cfg.AIBridgeTransportFactory = chatAIGatewayTransportFactoryPointer(chattest.NewMockAIBridgeTransport(t, openAIURL))
})
chat, err := server.CreateChat(ctx, chatd.CreateOptions{
OrganizationID: org.ID,
OwnerID: user.ID,
ProjectID: uuid.NullUUID{UUID: platform.ID, Valid: true},
Title: "moved",
ModelConfigID: model.ID,
InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("hi")},
})
require.NoError(t, err)
waitForChatStatus(ctx, t, db, chat.ID, database.ChatStatusWaiting)
target := uuid.NullUUID{}
if tc.toBilling {
target = uuid.NullUUID{UUID: billing.ID, Valid: true}
}
require.NoError(t, server.MoveChatToProject(ctx, chat, target))
_, err = server.SendMessage(ctx, chatd.SendMessageOptions{
ChatID: chat.ID,
CreatedBy: user.ID,
Content: []codersdk.ChatMessagePart{codersdk.ChatMessageText("what do you know?")},
})
require.NoError(t, err)
waitForChatStatus(ctx, t, db, chat.ID, database.ChatStatusWaiting)
mu.Lock()
defer mu.Unlock()
require.Len(t, requests, 2, "one request per turn")
count := func(req *chattest.OpenAIRequest, text string) int {
n := 0
for _, msg := range req.Messages {
if strings.Contains(msg.Content, text) {
n++
}
}
return n
}
last := requests[1]
require.Zero(t, count(last, `in the project "Platform"`))
for _, text := range tc.want {
require.Equal(t, 1, count(last, text), text)
}
})
}
}