Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions coderd/database/check_constraint.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions coderd/database/db2sdk/db2sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,7 @@ func Chat(c database.Chat, diffStatus *database.ChatDiffStatus, files []database
OwnerID: c.OwnerID,
OwnerUsername: c.OwnerUsername,
OwnerName: c.OwnerName,
Kind: codersdk.ChatKind(c.Kind),
LastModelConfigID: c.LastModelConfigID,
Title: c.Title,
Status: codersdk.ChatStatus(c.Status),
Expand Down Expand Up @@ -1897,18 +1898,13 @@ func Chat(c database.Chat, diffStatus *database.ChatDiffStatus, files []database
chat.ParentChatID = &parentChatID
}
// Always initialize Children to an empty slice so the JSON
// field serializes as [] rather than null. Root chats may
// later have children populated; child chats remain empty
// because nesting depth is capped at 1.
// field serializes as [] rather than null. Callers populate it
// with subagent children for non-subagent chats.
chat.Children = []codersdk.Chat{}
switch {
case c.RootChatID.Valid:
if c.RootChatID.Valid {
rootChatID := c.RootChatID.UUID
chat.RootChatID = &rootChatID
case c.ParentChatID.Valid:
rootChatID := c.ParentChatID.UUID
chat.RootChatID = &rootChatID
default:
} else {
rootChatID := c.ID
chat.RootChatID = &rootChatID
}
Expand Down
1 change: 1 addition & 0 deletions coderd/database/db2sdk/db2sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ func TestChat_AllFieldsPopulated(t *testing.T) {
OwnerID: uuid.New(),
OwnerUsername: "owner-username",
OwnerName: "Owner Name",
Kind: database.ChatKindChat,
OrganizationID: uuid.New(),
WorkspaceID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
BuildID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func TestUpdateChatACLByIDGuards(t *testing.T) {
db.EXPECT().Wrappers().Return([]string{}).AnyTimes()
db.EXPECT().GetChatByID(gomock.Any(), arg.ID).Return(database.Chat{
ID: arg.ID,
Kind: database.ChatKindSubagent,
RootChatID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
ParentChatID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
}, nil)
Expand Down Expand Up @@ -1289,6 +1290,7 @@ func (s *MethodTestSuite) TestChats() {
parentIDs := []uuid.UUID{parentA.ID, parentB.ID}
params := database.GetChildChatsByParentIDsParams{
ParentIds: parentIDs,
Kinds: []database.ChatKind{database.ChatKindSubagent},
Archived: sql.NullBool{Bool: false, Valid: true},
}
rows := []database.GetChildChatsByParentIDsRow{
Expand Down
10 changes: 10 additions & 0 deletions coderd/database/dbgen/dbgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func Chat(t testing.TB, db database.Store, seed database.Chat) database.Chat {
AgentID: seed.AgentID,
ParentChatID: seed.ParentChatID,
RootChatID: seed.RootChatID,
Kind: takeFirst(seed.Kind, defaultChatKind(seed)),
LastModelConfigID: takeFirst(seed.LastModelConfigID, uuid.New()),
Title: takeFirst(seed.Title, testutil.GetRandomName(t)),
Mode: seed.Mode,
Expand All @@ -114,6 +115,15 @@ func Chat(t testing.TB, db database.Store, seed database.Chat) database.Chat {
return chat
}

// defaultChatKind derives the kind of a seeded chat: a seed with a
// root_chat_id is a subagent, anything else is a user chat.
func defaultChatKind(seed database.Chat) database.ChatKind {
if seed.RootChatID.Valid {
return database.ChatKindSubagent
}
return database.ChatKindChat
}

func ChatMessage(t testing.TB, db database.Store, seed database.ChatMessage) database.ChatMessage {
t.Helper()

Expand Down
71 changes: 71 additions & 0 deletions coderd/database/dbpurge/dbpurge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,7 @@ func TestDeleteOldChatFiles(t *testing.T) {
OrganizationID: deps.org.ID,
OwnerID: deps.user.ID,
LastModelConfigID: deps.modelConfig.ID,
ParentChatID: uuid.NullUUID{UUID: parentChat.ID, Valid: true},
RootChatID: uuid.NullUUID{UUID: parentChat.ID, Valid: true},
Title: "child-chat",
})
Expand Down Expand Up @@ -3401,3 +3402,73 @@ func TestBackfillChatMessagesSearchTsv(t *testing.T) {
testutil.TryReceive(ctx, t, done)
})
}

// TestDeleteOldChatsSubagentFamily covers purging an archived chat whose
// archived subagents fall outside the LIMIT window: the subagents are
// deleted in the same statement, and a chat with an unarchived subagent
// is skipped.
func TestDeleteOldChatsSubagentFamily(t *testing.T) {
t.Parallel()

now := time.Date(2025, 6, 15, 12, 0, 0, 0, time.UTC)
ctx := testutil.Context(t, testutil.WaitLong)
db, _, rawDB := dbtestutil.NewDBWithSQLDB(t)

user := dbgen.User(t, db, database.User{})
org := dbgen.Organization(t, db, database.Organization{})
_ = dbgen.OrganizationMember(t, db, database.OrganizationMember{UserID: user.ID, OrganizationID: org.ID})
_ = dbgen.ChatProvider(t, db, database.ChatProvider{Provider: "openai", DisplayName: "OpenAI"})
modelConfig := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{
Model: "test-model",
ContextLimit: 8192,
OrganizationID: org.ID,
})

newChat := func(title string, root uuid.NullUUID, archived bool, updatedAt time.Time) database.Chat {
chat := dbgen.Chat(t, db, database.Chat{
OrganizationID: org.ID,
OwnerID: user.ID,
LastModelConfigID: modelConfig.ID,
Title: title,
ParentChatID: root,
RootChatID: root,
})
_, err := rawDB.ExecContext(ctx, "UPDATE chats SET archived = $1, updated_at = $2 WHERE id = $3", archived, updatedAt, chat.ID)
require.NoError(t, err)
return chat
}
ref := func(id uuid.UUID) uuid.NullUUID { return uuid.NullUUID{UUID: id, Valid: true} }
exists := func(id uuid.UUID) bool {
_, err := db.GetChatByID(ctx, id)
return err == nil
}

old := now.Add(-40 * 24 * time.Hour)
// The parent is the oldest row; its subagents are archived later, so a
// LIMIT of 1 selects only the parent.
parent := newChat("parent", uuid.NullUUID{}, true, old)
subagentA := newChat("subagent a", ref(parent.ID), true, old.Add(time.Hour))
subagentB := newChat("subagent b", ref(parent.ID), true, old.Add(2*time.Hour))
// An archived chat with an unarchived subagent must survive.
blocked := newChat("blocked", uuid.NullUUID{}, true, old.Add(3*time.Hour))
liveSubagent := newChat("live subagent", ref(blocked.ID), false, old.Add(4*time.Hour))

deleted, err := db.DeleteOldChats(ctx, database.DeleteOldChatsParams{
BeforeTime: now.Add(-30 * 24 * time.Hour),
LimitCount: 1,
})
require.NoError(t, err)
require.Equal(t, int64(3), deleted, "parent plus both subagents in one statement")
require.False(t, exists(parent.ID))
require.False(t, exists(subagentA.ID))
require.False(t, exists(subagentB.ID))

deleted, err = db.DeleteOldChats(ctx, database.DeleteOldChatsParams{
BeforeTime: now.Add(-30 * 24 * time.Hour),
LimitCount: 10,
})
require.NoError(t, err)
require.Zero(t, deleted)
require.True(t, exists(blocked.ID))
require.True(t, exists(liveSubagent.ID))
}
23 changes: 18 additions & 5 deletions coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading