Replaces the last Postgres-backed run-summary subtests with mock tests
of the `AggregateRunSummary` boundary and one extended Postgres test.
This deletes `MultipleStepsSumTokens` and the helpers `createTestStep`,
`updateTestStepWithUsage` and `updateTestStepWithFullUsage`.
**Why it is safe:**
- `NilRunID` now runs against a mock with no expectations, so any query
fails it, and checks that the base map comes back unchanged.
- The new `QueryAsChatdReturnsError` checks that the read runs as the
chatd actor and that the query error is returned.
- `RecomputesHasErrorAndCompletedEndpointLabel` now inserts step 2
before step 1 and compares the whole summary read through the real
query: five token sums, `has_error`, the endpoint label and the seeded
`first_message`. With the table from #30972, it covers every assertion
of `MultipleStepsSumTokens`.
- Of the same 42 `summary.go` mutants, the tests now kill 34 (`main`
kills 22, #30972 kills 31). The new kills are the nil run ID return
value, a swallowed query error and a read without the chatd actor. The 8
survivors are equivalent or only change logging.
- Mutants of the `GetChatDebugStepsByRunID` SQL that order by
`started_at` alone or reverse the order fail the Postgres test. Dropping
the `ORDER BY` survives: Postgres reads these rows through the unique
`(run_id, step_number)` index, which already returns them in step order.
- No references to the removed helpers remain.
**Review focus:** the setup order in
`RecomputesHasErrorAndCompletedEndpointLabel`. Step 2 is inserted first,
so the expected label holds only if the query orders by `step_number`.
Since #31007, `CreateStep` numbers steps itself, so the test inserts
both rows through the store with explicit step numbers.
Validation:
- `go test -race -count=1 -p 4 -parallel 4
./coderd/x/chatd/chatdebug/...` passes.
- `golangci-lint run --fix=false ./coderd/x/chatd/chatdebug/...` reports
0 issues, and the pre-commit hook passed.
## Stack
This stack makes the debug run-summary reduction database-free and
replaces its Postgres fixtures with literal rows.
1. #30972 Replace run-summary Postgres subtests with literal rows
2. #30973 Check the run-summary query boundary
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
Moves the post-query reduction in
`chatdebug.(*Service).AggregateRunSummary` into a private
`summarizeRunSteps` method and tests it with table cases over literal
`database.ChatDebugStep` rows. This deletes the 11 Postgres-backed
subtests of `TestService_AggregateRunSummary` whose assertions the table
covers; each one built its own database.
**Why it is safe:**
- The moved body is unchanged apart from `return result`.
`AggregateRunSummary` still owns the nil run ID check, the
chatd-authorized read and the error return.
- Every assertion of the removed subtests maps to a table row that
compares the whole output map, so absent keys are checked too. Each row
also checks that the base map is not mutated.
- `NilRunID`, `RecomputesHasErrorAndCompletedEndpointLabel` and
`MultipleStepsSumTokens` stay on Postgres here; #30973 replaces them.
- I ran 42 mutants of `summary.go` with `go test -overlay`: `main`'s
tests kill 22, this PR's tests kill 31, including all 22. The new kills
cover JSON null and empty error payloads, error payloads on completed
steps, malformed usage and attempts, empty paths, first-match selection
and caller map mutation. Of the 11 survivors, 3 are query-boundary
mutants that `main` also misses (#30973 kills them) and 8 are equivalent
or only change logging.
**Review focus:** `summarizeRunSteps` in `summary.go`. The moved block
should match `main` apart from the return statement.
Validation:
- `go test -race -count=1 -p 4 -parallel 4
./coderd/x/chatd/chatdebug/...` passes.
- `golangci-lint run --fix=false ./coderd/x/chatd/chatdebug/...` reports
0 issues, and the pre-commit hook passed.
## Stack
This stack makes the debug run-summary reduction database-free and
replaces its Postgres fixtures with literal rows.
1. #30972 Replace run-summary Postgres subtests with literal rows
2. #30973 Check the run-summary query boundary
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
`InsertChatDebugStep` now numbers a step after the run's highest step
when the caller passes zero, so chatdebug stops numbering steps in
process memory. `CreateStepParams` loses `StepNumber`, `beginStep` no
longer calls `nextStepNumber` or `syncStepCounter`, and `CreateStep`
replaces its collision retry, which listed the run's steps and bumped
the number, with a per-run lock. #31008 deletes the counters, which no
production code reads after this PR.
**Why it is safe:** Postgres takes the insert's snapshot before the
statement waits on the run row, so creates that waited on the same run
read the same highest step. `CreateStep` therefore takes
`pg_advisory_xact_lock(LockIDChatDebugRunSteps(run))` in its own
statement and inserts in the same transaction, so each insert starts
after the previous holder committed. The new
`TestService_CreateStep_NumbersConcurrentSteps` holds the run row,
starts 20 creates, waits until all of them wait on a lock, then commits
and checks the numbers 1 to 20. It passes 10 of 10 runs, and fails 10 of
10 with a 10-attempt collision retry in place of the lock.
`TestService_CreateStep_RetriesDuplicateStepNumbers` becomes
`TestService_CreateStep_NumbersStepsPerRun`, and
`TestService_CreateStep_ListRetryErrorWins` goes with the list call it
covered. Mutants that fail the chatdebug tests: no lock, the lock
outside the transaction, the lock keyed by chat, a constant number,
storing the zero, numbering across runs, a nonzero number from
`CreateStep`, and an unclassified missing run. One mutant survives and
is equivalent: running the insert on `s.db` instead of `tx` inside the
same `InTx` callback, because the insert still commits before the
transaction releases the lock. Running the insert after the transaction
commits fails the new test with a duplicate key. A copy of `service.go`
with garbage in it fails to build.
**Review focus:** `@step_number` stays as an override. Production passes
zero, and the fixtures in `querier_test.go`, `dbauthz_test.go`,
`dbpurge_test.go` and `exp_chats_test.go` keep their explicit numbers.
No test depends on the override (a mutant that ignores it survives),
because every fixture numbers its steps in insert order. Dropping the
parameter would touch about 20 fixtures outside chatdebug.
The PR removes more than it adds: +179/-233 hand-written lines. The
generated `querier.go` and `queries.sql.go` change 15 more.
Validation:
- `make gen/db` changes only `querier.go` and `queries.sql.go`.
- `go test -race -count=1 -p 4 -parallel 4
./coderd/x/chatd/chatdebug/...` passes, and `go test -count=10 -run
TestService_CreateStep ./coderd/x/chatd/chatdebug/` passes all 6 tests
10 of 10 times.
- `go test -count=1 -run ChatDebug ./coderd/database/
./coderd/database/dbpurge/` and `go test -count=1 -run Debug
./coderd/x/chatd/` (19 tests) pass, and the 25 chatd debug-run tests
pass under `-race`.
- `golangci-lint run --fix=false ./coderd/x/chatd/chatdebug/...
./coderd/database/` 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. This PR: 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.