`wrapStreamSeq` and `wrapObjectStreamSeq` each carried a copy of the
same finalize-once lifecycle. This PR moves it into a private
`streamStep` (the once-only gate and the context safety net) and a
`streamOutcome` (finish and error bookkeeping, and the early-stop and
missing-finish status rules), and rewrites both wrappers on them. Each
wrapper keeps its own part switch and payload type. It also deletes a
`stop != nil` check that is never false and a `seq != nil` check that
`Stream` and `StreamObject` make before they wrap a sequence.
**Why it is safe:** the PR changes no test. #30980 pins both wrappers'
status rules, and the chatdebug package and the chatd debug-run tests
pass before and after. All 39 mutants of the new lifecycle and of each
wrapper's wiring fail the package's tests, and a copy of `model.go` with
garbage in it fails to build. One more mutant drops the `finalized`
check from the safety net, so a step can be written twice; #30980's race
case kills it in 50 of 50 runs without `-race` and in 45 to 49 of 50
with `-race`. Two details keep their values: the response usage
normalizes a zero `fantasy.Usage` when no finish part arrived, which
gives the same zero value as before, and the summary's error count and
last error are copied from `streamOutcome` when the step finalizes
instead of being counted inline.
**Review focus:** the once-only gate stays per iterator (`streamStep`),
not on `stepHandle`, because `stepHandle.finish` lets a retry overwrite
an earlier result. `finish` builds the payload only when its call
finalizes the step, under the same lock as before.
**Moved onto the deletion stack:** #30988 deletes the stream heartbeats,
so the merge commit that moved this layer (no force push) also drops
them from the new lifecycle: `heartbeatDone`, `startHeartbeat`, the
channel close in `finalizeLocked`, both `step.startHeartbeat()` calls
and `launchHeartbeat`, and with them the heartbeat blocks in
`debugModel.Generate` and `GenerateObject`. The rest of the diff is
unchanged.
Validation:
- `go test -race -count=1 -p 4 -parallel 4
./coderd/x/chatd/chatdebug/...` passes before and after.
- `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.
- `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. This PR: 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.