Skip to content
Merged
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
6 changes: 6 additions & 0 deletions coderd/database/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func LockIDChatOrganizationSystemPrompt(organizationID uuid.UUID) int64 {
return GenLockID("chat_organization_system_prompt:" + organizationID.String())
}

// LockIDChatDebugRunSteps returns the advisory lock ID that serializes
// step numbering within one chat debug run.
func LockIDChatDebugRunSteps(runID uuid.UUID) int64 {
return GenLockID("chat_debug_run_steps:" + runID.String())
}

// GenLockID generates a unique and consistent lock ID from a given string.
func GenLockID(name string) int64 {
hash := fnv.New64()
Expand Down
5 changes: 5 additions & 0 deletions coderd/database/querier.go

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

10 changes: 9 additions & 1 deletion coderd/database/queries.sql.go

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

10 changes: 9 additions & 1 deletion coderd/database/queries/chatdebug.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ RETURNING *;
-- finished, the UPDATE returns zero rows, the INSERT gets no source
-- rows, and sql.ErrNoRows is returned. The UPDATE also serializes
-- with a concurrent run finalization under READ COMMITTED isolation.
-- A zero @step_number numbers the step after the run's highest step.
-- The row lock does not make that number unique, because the statement
-- snapshot predates the lock wait. Callers hold LockIDChatDebugRunSteps
-- from an earlier statement of the same transaction, so the snapshot
-- includes every committed step.
WITH locked_run AS (
UPDATE chat_debug_runs
SET updated_at = COALESCE(sqlc.narg('updated_at')::timestamptz, NOW())
Expand Down Expand Up @@ -102,7 +107,10 @@ INSERT INTO chat_debug_steps (
SELECT
@run_id::uuid,
locked_run.chat_id,
@step_number::int,
COALESCE(
NULLIF(@step_number::int, 0),
(SELECT COALESCE(MAX(s.step_number), 0) + 1 FROM chat_debug_steps s WHERE s.run_id = @run_id::uuid)
),
Comment thread
ibetitsmike marked this conversation as resolved.
Resolved
@operation::text,
@status::text,
sqlc.narg('history_tip_message_id')::bigint,
Expand Down
12 changes: 6 additions & 6 deletions coderd/x/chatd/chatdebug/bench_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func (noopStore) UpdateChatDebugStep(
}, nil
}

// GetChatDebugStepsByRunID isn't on the hot path these benchmarks
// exercise (step retry-collision handling), but overriding it avoids
// a nil-dereference panic if a future benchmark configuration change
// reaches it through the embedded nil database.Store.
func (noopStore) GetChatDebugStepsByRunID(context.Context, uuid.UUID) ([]database.ChatDebugStep, error) {
return nil, nil
func (s noopStore) InTx(fn func(database.Store) error, _ *database.TxOptions) error {
return fn(s)
}

func (noopStore) AcquireLock(context.Context, int64) error {
return nil
}

var _ database.Store = noopStore{}
Expand Down
42 changes: 11 additions & 31 deletions coderd/x/chatd/chatdebug/model_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,43 @@ func expectDebugLoggingEnabled(
db.EXPECT().GetUserChatDebugLoggingEnabled(gomock.Any(), ownerID).Return(true, nil)
}

func expectCreateStepNumberWithRequestValidity(
func expectCreateStepWithRequestValidity(
t *testing.T,
db *dbmock.MockStore,
runID uuid.UUID,
chatID uuid.UUID,
stepNumber int32,
op Operation,
normalizedRequestValid bool,
) uuid.UUID {
t.Helper()

stepID := uuid.New()

db.EXPECT().InTx(gomock.Any(), gomock.Any()).DoAndReturn(
func(f func(database.Store) error, _ *database.TxOptions) error { return f(db) })
db.EXPECT().AcquireLock(gomock.Any(), database.LockIDChatDebugRunSteps(runID)).Return(nil)
db.EXPECT().
InsertChatDebugStep(gomock.Any(), gomock.AssignableToTypeOf(database.InsertChatDebugStepParams{})).
DoAndReturn(func(_ context.Context, params database.InsertChatDebugStepParams) (database.ChatDebugStep, error) {
require.Equal(t, runID, params.RunID)
require.Equal(t, chatID, params.ChatID)
require.Equal(t, stepNumber, params.StepNumber)
require.Zero(t, params.StepNumber, "the database numbers the step")
require.Equal(t, string(op), params.Operation)
require.Equal(t, string(StatusInProgress), params.Status)
require.Equal(t, normalizedRequestValid, params.NormalizedRequest.Valid)

return database.ChatDebugStep{
ID: stepID,
RunID: runID,
ChatID: chatID,
StepNumber: params.StepNumber,
Operation: params.Operation,
Status: params.Status,
ID: stepID,
RunID: runID,
ChatID: chatID,
Operation: params.Operation,
Status: params.Status,
}, nil
})

return stepID
}

func expectCreateStepNumber(
t *testing.T,
db *dbmock.MockStore,
runID uuid.UUID,
chatID uuid.UUID,
stepNumber int32,
op Operation,
) uuid.UUID {
t.Helper()

return expectCreateStepNumberWithRequestValidity(
t,
db,
runID,
chatID,
stepNumber,
op,
true,
)
}

func expectCreateStep(
t *testing.T,
db *dbmock.MockStore,
Expand All @@ -106,7 +86,7 @@ func expectCreateStep(
) uuid.UUID {
t.Helper()

return expectCreateStepNumber(t, db, runID, chatID, 1, op)
return expectCreateStepWithRequestValidity(t, db, runID, chatID, op, true)
}

func expectUpdateStep(
Expand Down
4 changes: 0 additions & 4 deletions coderd/x/chatd/chatdebug/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,9 @@ func beginStep(
}
}

stepNum := nextStepNumber(rc.RunID)
step, err := svc.CreateStep(ctx, CreateStepParams{
RunID: rc.RunID,
ChatID: chatID,
StepNumber: stepNum,
Operation: op,
Status: StatusInProgress,
HistoryTipMessageID: rc.HistoryTipMessageID,
Expand All @@ -257,8 +255,6 @@ func beginStep(
return nil, ctx
}

syncStepCounter(rc.RunID, step.StepNumber)

sc := &StepContext{
StepID: step.ID,
RunID: rc.RunID,
Expand Down
2 changes: 1 addition & 1 deletion coderd/x/chatd/chatdebug/recorder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestBeginStep_FallsBackToRunChatID(t *testing.T) {
runChatID := uuid.New()
ownerID := uuid.New()
expectDebugLoggingEnabled(t, db, ownerID)
expectCreateStepNumberWithRequestValidity(t, db, runID, runChatID, 1, OperationGenerate, false)
expectCreateStepWithRequestValidity(t, db, runID, runChatID, OperationGenerate, false)

ctx := ContextWithRun(context.Background(), &RunContext{RunID: runID, ChatID: runChatID})
svc := NewService(db, testutil.Logger(t), nil)
Expand Down
30 changes: 3 additions & 27 deletions coderd/x/chatd/chatdebug/reuse_step_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ func TestBeginStepReuseStep(t *testing.T) {
ctrl := gomock.NewController(t)
db := dbmock.NewMockStore(ctrl)
expectDebugLoggingEnabled(t, db, ownerID)
expectCreateStepNumberWithRequestValidity(
t,
db,
runID,
chatID,
1,
OperationStream,
false,
)
expectCreateStepWithRequestValidity(t, db, runID, chatID, OperationStream, false)
expectDebugLoggingEnabled(t, db, ownerID)

svc := NewService(db, testutil.Logger(t), nil)
Expand Down Expand Up @@ -67,25 +59,9 @@ func TestBeginStepReuseStep(t *testing.T) {
ctrl := gomock.NewController(t)
db := dbmock.NewMockStore(ctrl)
expectDebugLoggingEnabled(t, db, ownerID)
expectCreateStepNumberWithRequestValidity(
t,
db,
runID,
chatID,
1,
OperationStream,
false,
)
expectCreateStepWithRequestValidity(t, db, runID, chatID, OperationStream, false)
expectDebugLoggingEnabled(t, db, ownerID)
expectCreateStepNumberWithRequestValidity(
t,
db,
runID,
chatID,
2,
OperationStream,
false,
)
expectCreateStepWithRequestValidity(t, db, runID, chatID, OperationStream, false)

svc := NewService(db, testutil.Logger(t), nil)
ctx := ContextWithRun(context.Background(), &RunContext{RunID: runID, ChatID: chatID})
Expand Down
73 changes: 27 additions & 46 deletions coderd/x/chatd/chatdebug/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ type UpdateRunParams struct {
type CreateStepParams struct {
RunID uuid.UUID
ChatID uuid.UUID
StepNumber int32
Operation Operation
Status Status
HistoryTipMessageID int64
Expand Down Expand Up @@ -341,7 +340,7 @@ func (s *Service) CreateStep(
now := s.clock.Now()
insert := database.InsertChatDebugStepParams{
RunID: params.RunID,
StepNumber: params.StepNumber,
StepNumber: 0, // Zero lets the database number the step.
Operation: string(params.Operation),
Status: string(params.Status),
HistoryTipMessageID: nullInt64(params.HistoryTipMessageID),
Expand All @@ -358,52 +357,34 @@ func (s *Service) CreateStep(
ChatID: params.ChatID,
}

// Cap retry attempts to prevent infinite loops under
// pathological concurrency. Each iteration performs two DB
// round-trips (insert + list), so 10 retries is generous.
const maxCreateStepRetries = 10

for range maxCreateStepRetries {
if err := ctx.Err(); err != nil {
return database.ChatDebugStep{}, err
}

step, err := s.db.InsertChatDebugStep(chatdContext(ctx), insert)
if err == nil {
// The INSERT CTE atomically bumps the parent run's
// updated_at, so no separate touch call is needed.
s.publishEvent(ctx, step.ChatID, EventKindStepUpdate, step.RunID, step.ID)
return step, nil
}
// The INSERT's locked_run CTE filters on id, chat_id, and
// finished_at IS NULL, so sql.ErrNoRows can mean "run not
// found", "chat_id mismatch", or "already finalized." Look
// the run up to disambiguate instead of conflating
// caller-side data bugs with the legitimate terminal case.
if errors.Is(err, sql.ErrNoRows) {
return database.ChatDebugStep{}, s.classifyMissingRun(ctx, params)
}
if !database.IsUniqueViolation(err, database.UniqueIndexChatDebugStepsRunStep) {
return database.ChatDebugStep{}, err
}

steps, listErr := s.db.GetChatDebugStepsByRunID(chatdContext(ctx), params.RunID)
if listErr != nil {
return database.ChatDebugStep{}, listErr
}
nextStepNumber := insert.StepNumber + 1
for _, existing := range steps {
if existing.StepNumber >= nextStepNumber {
nextStepNumber = existing.StepNumber + 1
}
// The insert numbers the step from its statement snapshot, which it
// takes before it waits on the run row. Holding the run's step lock
// from an earlier statement makes that snapshot include every step
// that the previous holder committed.
var step database.ChatDebugStep
err := s.db.InTx(func(tx database.Store) error {
if err := tx.AcquireLock(chatdContext(ctx), database.LockIDChatDebugRunSteps(params.RunID)); err != nil {
return xerrors.Errorf("lock debug run steps: %w", err)
}
insert.StepNumber = nextStepNumber
var err error
step, err = tx.InsertChatDebugStep(chatdContext(ctx), insert)
return err
}, nil)
// The INSERT's locked_run CTE filters on id, chat_id, and
// finished_at IS NULL, so sql.ErrNoRows can mean "run not
// found", "chat_id mismatch", or "already finalized." Look
// the run up to disambiguate instead of conflating
// caller-side data bugs with the legitimate terminal case.
if errors.Is(err, sql.ErrNoRows) {
return database.ChatDebugStep{}, s.classifyMissingRun(ctx, params)
}

return database.ChatDebugStep{}, xerrors.Errorf(
"chatdebug: failed to create step after %d retries (run %s)",
maxCreateStepRetries, params.RunID,
)
if err != nil {
return database.ChatDebugStep{}, err
}
// The INSERT CTE atomically bumps the parent run's
// updated_at, so no separate touch call is needed.
s.publishEvent(ctx, step.ChatID, EventKindStepUpdate, step.RunID, step.ID)
return step, nil
}

// classifyMissingRun disambiguates the sql.ErrNoRows returned by
Expand Down
Loading
Loading