Files
coder/coderd/x/chatd/chatdebug/reuse_step_internal_test.go
T
Michael Suchacz e3657389bb refactor(coderd/x/chatd/chatdebug): delete the process-global step counters (#31008)
Deletes chatdebug's process-global step counters: the `stepCounters` and
`runRefCounts` maps, `refCountMu`, `trackRunRef`, `releaseRunRef`,
`nextStepNumber`, `syncStepCounter` and `CleanupStepCounter`, the
`runtime.AddCleanup` hook that `ContextWithRun` registered for every run
context, and the two `CleanupStepCounter` calls in `FinalizeRun`. Since
#31007 the database numbers steps, so no production code reads them.

**Why it is safe:** after #31007 no production code reads the counters.
Production code only maintained them: `ContextWithRun` registered a
reference for every run context and `FinalizeRun` called
`CleanupStepCounter`, and this PR removes both. A search of `coderd/`
and `enterprise/` for the deleted names finds nothing now. The deleted
tests covered only the deleted code: `TestNextStepNumber_Concurrent` and
`TestSyncStepCounter_AdvancesCounter` checked counter values, and the
three garbage-collection tests in `context_internal_test.go` checked
that a counter lives until the last `RunContext` for its run is
collected. The 20 `t.Cleanup(func() { CleanupStepCounter(runID) })`
lines go with them. `TestReuseStep_PreservesExistingHolder` and
`TestBeginStepReuseStep` stay. The `FinalizeRun` doc comment no longer
mentions the counter.

**Review focus:** `ContextWithRun` is now a plain `context.WithValue`.
Code that copies a `RunContext`, such as
`runnerDebugTurn.contextLocked`, no longer affects anything, because no
state is tied to the struct's lifetime.

Validation:
- `go test -race -count=1 -p 4 -parallel 4
./coderd/x/chatd/chatdebug/...` passes.
- `go test -count=1 -run Debug ./coderd/x/chatd/` (19 tests) passes, and
the 25 chatd debug-run tests pass under `-race` (`go test -race -run
'^(TestActiveServer_ChatTurnDebugRun.*|TestRunnerDebugTurn.*|TestProposeChatTitle_DebugRun|TestArchiveChatDeletesDebugRuns|TestEditMessageDebugCleanup.*|TestQuickgen.*|TestMaybeGenerateChatTitle.*|TestPrepareManualTitleDebugRunCreateFailure)$'
./coderd/x/chatd/`).
- `golangci-lint run --fix=false ./coderd/x/chatd/chatdebug/...` reports
0 issues, and the pre-commit hook passed.

## Stack

This stack deletes the stale debug-run sweep, then simplifies the
chatdebug debug-run, stream wrapper and step numbering code, drops the
indexes the sweep used, and last deletes a runner debug field that lost
its only reader with the heartbeats. The debug-run and stream wrapper
refactors each sit on a test-only layer that pins the behavior they
keep.

1. #30988 Delete the chat debug run and step heartbeats
2. #30989 Delete `FinalizeStale` and the stale threshold
3. #30995 Delete the `FinalizeStaleChatDebugRows` query
4. #30933 Pin the debug runs that turns, titles and status labels record
5. #30934 Create and finalize debug runs through one helper
6. #30980 Table the stream wrapper tests and cover their status rules
7. #30981 Share one finalize-once lifecycle between the stream wrappers
8. #31007 Number chat debug steps in the database
9. This PR: Delete the process-global step counters
10. #31009 Drop the chat debug stale-sweep indexes
11. #31010 Drop the unused `runnerCtx` from the runner debug turn

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-22 07:17:24 +00:00

78 lines
2.5 KiB
Go

package chatdebug
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/coder/coder/v2/coderd/database/dbmock"
"github.com/coder/coder/v2/testutil"
)
func TestBeginStepReuseStep(t *testing.T) {
t.Parallel()
t.Run("reuses handle under ReuseStep", func(t *testing.T) {
t.Parallel()
chatID := uuid.New()
ownerID := uuid.New()
runID := uuid.New()
ctrl := gomock.NewController(t)
db := dbmock.NewMockStore(ctrl)
expectDebugLoggingEnabled(t, db, ownerID)
expectCreateStepWithRequestValidity(t, db, runID, chatID, OperationStream, false)
expectDebugLoggingEnabled(t, db, ownerID)
svc := NewService(db, testutil.Logger(t), nil)
ctx := ContextWithRun(context.Background(), &RunContext{RunID: runID, ChatID: chatID})
ctx = ReuseStep(ctx)
opts := RecorderOptions{ChatID: chatID, OwnerID: ownerID}
firstHandle, firstEnriched := beginStep(ctx, svc, opts, OperationStream, nil)
secondHandle, secondEnriched := beginStep(ctx, svc, opts, OperationStream, nil)
require.NotNil(t, firstHandle)
require.Same(t, firstHandle, secondHandle)
require.Same(t, firstHandle.stepCtx, secondHandle.stepCtx)
require.Same(t, firstHandle.sink, secondHandle.sink)
require.Equal(t, runID, firstHandle.stepCtx.RunID)
require.Equal(t, chatID, firstHandle.stepCtx.ChatID)
require.NotEqual(t, uuid.Nil, firstHandle.stepCtx.StepID)
require.Same(t, attemptSinkFromContext(firstEnriched), attemptSinkFromContext(secondEnriched))
})
t.Run("creates new handles without ReuseStep", func(t *testing.T) {
t.Parallel()
chatID := uuid.New()
ownerID := uuid.New()
runID := uuid.New()
ctrl := gomock.NewController(t)
db := dbmock.NewMockStore(ctrl)
expectDebugLoggingEnabled(t, db, ownerID)
expectCreateStepWithRequestValidity(t, db, runID, chatID, OperationStream, false)
expectDebugLoggingEnabled(t, db, ownerID)
expectCreateStepWithRequestValidity(t, db, runID, chatID, OperationStream, false)
svc := NewService(db, testutil.Logger(t), nil)
ctx := ContextWithRun(context.Background(), &RunContext{RunID: runID, ChatID: chatID})
opts := RecorderOptions{ChatID: chatID, OwnerID: ownerID}
firstHandle, _ := beginStep(ctx, svc, opts, OperationStream, nil)
secondHandle, _ := beginStep(ctx, svc, opts, OperationStream, nil)
require.NotNil(t, firstHandle)
require.NotNil(t, secondHandle)
require.NotSame(t, firstHandle, secondHandle)
require.NotSame(t, firstHandle.sink, secondHandle.sink)
require.NotEqual(t, firstHandle.stepCtx.StepID, secondHandle.stepCtx.StepID)
})
}