Files
coder/coderd/x/chatd/chatd_debug.go
T
Michael Suchacz e8882b6b51 refactor(coderd/x/chatd): create and finalize debug runs through one helper (#30934)
Chat turns, manual titles, automatic titles and turn status labels each
created a debug run on a detached, time-bounded context, rebuilt the run
context from the row, and finalized the run by hand. This PR adds
`chatdebug.Service.BeginRun` and `StartRun` and routes all four through
them. It deletes `prepareQuickgenDebugCandidate`, `shortTextCandidate`,
`chatdebugRunContext`, `debugCreateRunTimeout` and a literal 5 second
insert timeout.

- `BeginRun` inserts the run on a context that ignores the caller's
cancellation, with a 5 second limit, and builds the `RunContext` from
its parameters. Chat turns call it and still finalize in
`runnerDebugTurn`.
- `StartRun` wraps `BeginRun` for one model call and returns a context
that carries the run plus a function that finalizes it from the call's
error. Titles and status labels use it.
- New `TestService_StartRun` covers a finalized run, a failed insert and
a failed finalize with a mock store, including the seed summary that
each finalize keeps and the warning that each failure logs.

**Why it is safe:** #30933 pins the insert parameters, the run context,
the finalize status and summary, and the failed insert for all four call
sites. Those tests and the other debug-run, title and quickgen tests
pass before and after. `CreateRun` stores its parameters unchanged, so a
`RunContext` built from the parameters equals one built from the row. A
repo-wide search finds no reference to the deleted symbols. Of 14
mutants of `BeginRun` and `StartRun`, 13 fail `TestService_StartRun`.
They drop either failure warning, report a failed insert as success, let
the insert inherit the caller's cancellation or lose its deadline, drop
the parent chat or the history tip from the run context, drop the seed
summary from the insert or the finalize, ignore the call error or the
finalize timeout, leave the run out of the returned context, or finalize
the wrong run. The survivor shortens the insert limit from 5 seconds to
1.

**Review focus:** log output. Create and finalize failures now log
through the chatdebug logger as `failed to create debug run` and `failed
to finalize debug run`. Before, each call site used its own logger and
message. Compaction still creates its debug run by hand, because #30766
changes that code; it can move to `StartRun` after that PR merges.

**Moved onto the deletion stack:** a merge commit moved this layer onto
the updated #30933 without a force push. It carries one forced change:
#30989 removed the `database/sql` import from
`chatdebug/service_test.go` with the last test that used it, and
`TestService_StartRun` here still uses `sql.NullString`, so the merge
adds the import back. The rest of the diff is unchanged.

Validation:
- `go test -race -count=1 -p 4 -parallel 4 ./coderd/x/chatd/ -run
'^(TestActiveServer_ChatTurnDebugRun.*|TestRunnerDebugTurn.*|TestProposeChatTitle_DebugRun|TestArchiveChatDeletesDebugRuns|TestEditMessageDebugCleanup.*|TestQuickgen.*|TestMaybeGenerateChatTitle.*|TestPrepareManualTitleDebugRunCreateFailure)$'`:
the same 25 tests pass before and after, and again after the move.
- `go test -race -count=1 -p 4 -parallel 4
./coderd/x/chatd/chatdebug/...` passes.
- `golangci-lint run --fix=false ./coderd/x/chatd/chatdebug/...
./coderd/x/chatd/` 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. This PR: 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. #31008 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-20 22:11:25 +00:00

108 lines
2.9 KiB
Go

package chatd
import (
"context"
"time"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
)
const (
debugCleanupRetryDelay = 500 * time.Millisecond
debugCleanupAttempts = 3
debugCleanupTimeout = 5 * time.Second
// debugFinalizeTimeout caps best-effort debug run finalization
// outside the caller's canceled context.
debugFinalizeTimeout = 5 * time.Second
shortTextDebugFinalizeTimeout = 10 * time.Second
// debugCleanupClockSkew gives cleanup cutoffs tolerance for cross-
// replica clock drift. The cutoff is sampled from the DB
// (updated_at returned by the status transition), and
// chat_debug_runs.started_at is stamped by whatever replica
// processes the replacement turn. If that replica's clock lags
// the DB, its started_at can land behind a commit-time cutoff
// even though the insert physically happened after commit.
// Subtracting this buffer ensures the fast retry path cannot
// delete replacement rows when clocks drift by up to this
// amount; rows within the buffer survive the fast cleanup, and
// dbpurge deletes them once they age past the debug retention
// window.
debugCleanupClockSkew = 30 * time.Second
)
func (p *Server) debugService() *chatdebug.Service {
if p == nil {
return nil
}
if p.debugSvcFactory == nil {
return p.debugSvc
}
p.debugSvcInit.Do(func() {
p.debugSvc = p.debugSvcFactory()
p.debugSvcReady.Store(p.debugSvc != nil)
})
return p.debugSvc
}
func (p *Server) existingDebugService() *chatdebug.Service {
if p == nil {
return nil
}
if p.debugSvcFactory == nil {
return p.debugSvc
}
if !p.debugSvcReady.Load() {
return nil
}
return p.debugSvc
}
func (p *Server) scheduleDebugCleanup(
ctx context.Context,
logMessage string,
fields []slog.Field,
cleanup func(context.Context, *chatdebug.Service) error,
) {
debugSvc := p.debugService()
if debugSvc == nil {
return
}
cleanupCtx, stopCleanupCtx := p.inflightContext(ctx)
if err := p.goInflight(func() {
defer stopCleanupCtx()
for attempt := 0; attempt < debugCleanupAttempts; attempt++ {
if attempt > 0 {
timer := p.clock.NewTimer(debugCleanupRetryDelay, "chatd", "debug_cleanup")
defer timer.Stop()
select {
case <-timer.C:
case <-cleanupCtx.Done():
timer.Stop()
return
}
}
passCtx, cancel := context.WithTimeout(cleanupCtx, debugCleanupTimeout)
err := cleanup(passCtx, debugSvc)
cancel()
if err == nil {
return
}
logFields := append([]slog.Field{
slog.F("attempt", attempt+1),
slog.F("max_attempts", debugCleanupAttempts),
}, fields...)
logFields = append(logFields, slog.Error(err))
p.logger.Warn(cleanupCtx, logMessage, logFields...)
}
}); err != nil {
stopCleanupCtx()
logFields := append([]slog.Field{slog.F("cleanup", logMessage)}, fields...)
logFields = append(logFields, slog.Error(err))
p.logger.Error(context.WithoutCancel(ctx), "failed to schedule chat debug cleanup", logFields...)
}
}