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.
268 lines
6.9 KiB
Go
268 lines
6.9 KiB
Go
package chatdebug
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"charm.land/fantasy"
|
|
"github.com/google/uuid"
|
|
|
|
"cdr.dev/slog/v3"
|
|
)
|
|
|
|
// RecorderOptions identifies the chat/model context for debug recording.
|
|
type RecorderOptions struct {
|
|
ChatID uuid.UUID
|
|
OwnerID uuid.UUID
|
|
}
|
|
|
|
// WrapModel returns model unchanged when debug recording is disabled, or a
|
|
// debug wrapper when a service is available.
|
|
func WrapModel(
|
|
model fantasy.LanguageModel,
|
|
svc *Service,
|
|
opts RecorderOptions,
|
|
) fantasy.LanguageModel {
|
|
if model == nil {
|
|
panic("chatdebug: nil LanguageModel")
|
|
}
|
|
if svc == nil {
|
|
return model
|
|
}
|
|
return &debugModel{inner: model, svc: svc, opts: opts}
|
|
}
|
|
|
|
type attemptSink struct {
|
|
mu sync.Mutex
|
|
attempts []Attempt
|
|
attemptCounter atomic.Int32
|
|
}
|
|
|
|
func (s *attemptSink) nextAttemptNumber() int {
|
|
if s == nil {
|
|
panic("chatdebug: nil attemptSink")
|
|
}
|
|
return int(s.attemptCounter.Add(1))
|
|
}
|
|
|
|
func (s *attemptSink) record(a Attempt) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
s.attempts = append(s.attempts, a)
|
|
}
|
|
|
|
// replaceByNumber overwrites a previously recorded attempt whose Number
|
|
// matches. If no match is found, the attempt is appended. This supports
|
|
// the provisional-then-upgrade flow used for SSE bodies where Read()
|
|
// records a completed attempt on EOF and Close() later needs to replace
|
|
// it with a failed attempt when inner.Close() surfaces an error.
|
|
func (s *attemptSink) replaceByNumber(number int, a Attempt) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
for i := range s.attempts {
|
|
if s.attempts[i].Number == number {
|
|
s.attempts[i] = a
|
|
return
|
|
}
|
|
}
|
|
s.attempts = append(s.attempts, a)
|
|
}
|
|
|
|
func (s *attemptSink) snapshot() []Attempt {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
attempts := make([]Attempt, len(s.attempts))
|
|
copy(attempts, s.attempts)
|
|
return attempts
|
|
}
|
|
|
|
type attemptSinkKey struct{}
|
|
|
|
func withAttemptSink(ctx context.Context, sink *attemptSink) context.Context {
|
|
if sink == nil {
|
|
panic("chatdebug: nil attemptSink")
|
|
}
|
|
return context.WithValue(ctx, attemptSinkKey{}, sink)
|
|
}
|
|
|
|
func attemptSinkFromContext(ctx context.Context) *attemptSink {
|
|
sink, _ := ctx.Value(attemptSinkKey{}).(*attemptSink)
|
|
return sink
|
|
}
|
|
|
|
const stepFinalizeTimeout = 5 * time.Second
|
|
|
|
func stepFinalizeContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
|
if ctx == nil {
|
|
panic("chatdebug: nil context")
|
|
}
|
|
return context.WithTimeout(context.WithoutCancel(ctx), stepFinalizeTimeout)
|
|
}
|
|
|
|
type stepHandle struct {
|
|
stepCtx *StepContext
|
|
sink *attemptSink
|
|
svc *Service
|
|
mu sync.Mutex
|
|
status Status
|
|
response any
|
|
metadata any
|
|
// hadError tracks whether a prior finalization wrote an error
|
|
// payload. Used to decide whether a successful retry needs to
|
|
// explicitly clear the error field via jsonClear.
|
|
hadError bool
|
|
}
|
|
|
|
// beginStep validates preconditions, creates a debug step, and returns a
|
|
// handle plus an enriched context carrying the attempt sink.
|
|
// Returns (nil, original ctx) when debug recording should be skipped.
|
|
func beginStep(
|
|
ctx context.Context,
|
|
svc *Service,
|
|
opts RecorderOptions,
|
|
op Operation,
|
|
normalizedReq any,
|
|
) (*stepHandle, context.Context) {
|
|
if svc == nil {
|
|
return nil, ctx
|
|
}
|
|
|
|
rc, ok := RunFromContext(ctx)
|
|
if !ok || rc.RunID == uuid.Nil {
|
|
return nil, ctx
|
|
}
|
|
|
|
chatID := opts.ChatID
|
|
if chatID == uuid.Nil {
|
|
chatID = rc.ChatID
|
|
}
|
|
if !svc.IsEnabled(ctx, chatID, opts.OwnerID) {
|
|
return nil, ctx
|
|
}
|
|
|
|
holder, reuseStep := reuseHolderFromContext(ctx)
|
|
if reuseStep {
|
|
holder.mu.Lock()
|
|
defer holder.mu.Unlock()
|
|
// Only reuse the cached handle if it belongs to the same run.
|
|
// A different RunContext means a new logical run, so we must
|
|
// create a fresh step to avoid cross-run attribution.
|
|
if holder.handle != nil && holder.handle.stepCtx.RunID == rc.RunID {
|
|
enriched := withAttemptSink(ctx, holder.handle.sink)
|
|
return holder.handle, enriched
|
|
}
|
|
}
|
|
|
|
step, err := svc.CreateStep(ctx, CreateStepParams{
|
|
RunID: rc.RunID,
|
|
ChatID: chatID,
|
|
Operation: op,
|
|
Status: StatusInProgress,
|
|
HistoryTipMessageID: rc.HistoryTipMessageID,
|
|
NormalizedRequest: normalizedReq,
|
|
})
|
|
if err != nil {
|
|
svc.log.Warn(ctx, "failed to create chat debug step",
|
|
slog.Error(err),
|
|
slog.F("chat_id", chatID),
|
|
slog.F("run_id", rc.RunID),
|
|
slog.F("operation", op),
|
|
)
|
|
return nil, ctx
|
|
}
|
|
|
|
sc := &StepContext{
|
|
StepID: step.ID,
|
|
RunID: rc.RunID,
|
|
ChatID: chatID,
|
|
}
|
|
handle := &stepHandle{stepCtx: sc, sink: &attemptSink{}, svc: svc}
|
|
enriched := withAttemptSink(ctx, handle.sink)
|
|
if reuseStep {
|
|
holder.handle = handle
|
|
}
|
|
|
|
return handle, enriched
|
|
}
|
|
|
|
// finish updates the debug step with final status and data. A mutex
|
|
// guards the write so concurrent callers (e.g. retried stream wrappers
|
|
// sharing a reuse handle) don't race. Later retries are allowed to
|
|
// overwrite earlier failure results so the step reflects the final
|
|
// outcome, but stale callbacks cannot regress a terminal state.
|
|
func (h *stepHandle) finish(
|
|
ctx context.Context,
|
|
status Status,
|
|
response any,
|
|
usage any,
|
|
errPayload any,
|
|
metadata any,
|
|
) {
|
|
if h == nil || h.stepCtx == nil {
|
|
return
|
|
}
|
|
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
|
|
// Reject stale callbacks that would regress a terminal state.
|
|
// Status priority: in_progress < interrupted < error < completed.
|
|
// A tardy safety-net writing "interrupted" cannot clobber a step
|
|
// that already reached "completed" or "error" from a real retry.
|
|
// Equal-priority updates are allowed so that retries ending in the
|
|
// same terminal class (e.g. error → error under ReuseStep) can
|
|
// still update the step with newer attempt data.
|
|
if h.status.IsTerminal() && status.Priority() < h.status.Priority() {
|
|
return
|
|
}
|
|
|
|
h.status = status
|
|
h.response = response
|
|
h.metadata = metadata
|
|
if errPayload != nil {
|
|
h.hadError = true
|
|
}
|
|
if h.svc == nil {
|
|
return
|
|
}
|
|
|
|
updateCtx, cancel := stepFinalizeContext(ctx)
|
|
defer cancel()
|
|
|
|
// When the step completes successfully after a prior failed
|
|
// attempt, the error field must be explicitly cleared. A plain
|
|
// nil would leave the COALESCE-based SQL untouched, so we send
|
|
// jsonClear{} which serializes as a valid JSONB null. Only do
|
|
// this when a prior error was actually recorded; otherwise
|
|
// clean successes would get a spurious JSONB null that downstream
|
|
// aggregation could misread as an error.
|
|
errValue := errPayload
|
|
if errValue == nil && status == StatusCompleted && h.hadError {
|
|
errValue = jsonClear{}
|
|
}
|
|
|
|
if _, updateErr := h.svc.UpdateStep(updateCtx, UpdateStepParams{
|
|
ID: h.stepCtx.StepID,
|
|
ChatID: h.stepCtx.ChatID,
|
|
Status: status,
|
|
NormalizedResponse: response,
|
|
Usage: usage,
|
|
Attempts: h.sink.snapshot(),
|
|
Error: errValue,
|
|
Metadata: metadata,
|
|
FinishedAt: h.svc.clock.Now(),
|
|
}); updateErr != nil {
|
|
h.svc.log.Warn(updateCtx, "failed to finalize chat debug step",
|
|
slog.Error(updateErr),
|
|
slog.F("step_id", h.stepCtx.StepID),
|
|
slog.F("chat_id", h.stepCtx.ChatID),
|
|
slog.F("status", status),
|
|
)
|
|
}
|
|
}
|