One of the main costs in terms of DB load from chats is extra round trips during transactions. This results in locks being held for longer periods of time -> an increase in lock waits for chats related queries. One of the opportunities for reducing some of the round trips is to eliminate extra chat row reads after we have just read the row in order to lock it. In this PR `ChatMachine.Update` now seeds the transaction struct with the row returned by `LockChatAndBumpSnapshotVersion`, plus a queue count taken right after it. We can then pass it into `Update` as an optional parameter validate against it, so neither re-reads the chat row from the database while the row lock is held. `updateChat` (send, edit, promote, tool results, interrupt, compact, clear, reconcile), hook failure, family archive, and the worker's generation and task transitions use it instead of reloading the chat under the lock. This PR relative to main: |measurement | main | this PR | percent change | |-- | -- | -- | -- | | Round trips under lock | 180.9 | 142.0 | -39% | | Statements under lock | 86.1 | 66.6 | -23% | | GetChatByID under lock | 54.1 | 35.8 | -34% | | GetChatByID total | 75.5 | 57.1 | -25% | | Statements | 235.8 | 216.2 | -8% | Generated by Coder Agents on behalf of @cstyan.
61 lines
2.5 KiB
Go
61 lines
2.5 KiB
Go
package chatd
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
|
|
)
|
|
|
|
// TestSubagentSpawnUnderArchivedRoot covers a spawn_agent call that is
|
|
// still in flight after its family was archived, for example from a
|
|
// runner that lost its lease and has not noticed yet. The spawn must
|
|
// fail instead of creating a running, unarchived child under the
|
|
// archived root, which would break the family archive invariant that
|
|
// ArchiveChat documents.
|
|
func TestSubagentSpawnUnderArchivedRoot(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db, ps := dbtestutil.NewDB(t)
|
|
server := newInternalTestServer(t, db, ps, chatprovider.ProviderAPIKeys{})
|
|
ctx := chatdTestContext(t)
|
|
user, org, model := seedInternalChatDeps(t, db)
|
|
|
|
// The root runs a turn; its spawn_agent call reads the parent now.
|
|
root := createInternalParentChat(ctx, t, server, db, org.ID, user.ID, model.ID, "root")
|
|
require.Equal(t, database.ChatStatusRunning, root.Status)
|
|
inFlightSnapshot := root
|
|
|
|
// The turn ends (another owner finished it) and the user archives
|
|
// the family.
|
|
_, err := chatstate.NewChatMachine(db, ps, root.ID).Update(ctx,
|
|
func(tx *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error {
|
|
_, err := tx.FinishTurn(chatstate.FinishTurnInput{})
|
|
return err
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, server.ArchiveChat(ctx, root))
|
|
|
|
// The in-flight spawn_agent call continues. Reloading the parent
|
|
// already refuses the spawn before any prompt hook runs.
|
|
_, err = server.loadSubagentSpawnParentChat(ctx, func() database.Chat { return inFlightSnapshot })
|
|
require.ErrorIs(t, err, chatstate.ErrChatFamilyArchived)
|
|
require.ErrorContains(t, err, "cannot create a child agent because the parent chat is archived")
|
|
|
|
// A spawn that reloaded the parent before the archive committed
|
|
// reaches child creation, where the locked root check refuses it.
|
|
_, err = server.createChildSubagentChatWithOptions(ctx, inFlightSnapshot, "late child", "", childSubagentChatOptions{})
|
|
require.ErrorIs(t, err, chatstate.ErrChatFamilyArchived)
|
|
// spawn_agent returns this text to the model as the tool error.
|
|
require.ErrorContains(t, err, "cannot create a child agent because the parent chat is archived")
|
|
|
|
familyIDs, err := db.GetChatFamilyIDsByRootID(ctx, root.ID)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []uuid.UUID{root.ID}, familyIDs, "no child may join the archived family")
|
|
}
|