Commit Graph
3 Commits
Author SHA1 Message Date
Michael Suchacz aef1917171 refactor(coderd/x/chatd): delete the write-only generatedChatTitle (#30926)
Deletes `generatedChatTitle`, a mutex-guarded cell in which
`maybeGenerateChatTitle` stored each generated title so that tests could
read it. Production passed a fresh one and never read it. The PR removes
the type, its `Store` and `Load` methods, the parameter, and the six
`Load` checks in the title tests, including the one that #30933 added to
`quickgen_debug_internal_test.go`.

## Why it is safe

`git grep generatedChatTitle` finds nothing. Each removed `Load` check
has a remaining check on the stored chat row or the published event:

| Removed check | Remaining check |
|---|---|
| `WritesGeneratedTitle`: title stored | stored title and source,
`title_change` event |
| `KeepsCurrentTitleWhenNoTitleIsWritten`, both cases: nothing stored |
stored title unchanged, no publish |
| `TestQuickgenFollowsConfiguredRetries/Title`: nothing stored | `model
call failed` and `TitleGenerationOverrideCallFailure` cover the same
return |
| `TitleGenerationOverrideSetUsable`: title stored | gomock expects the
exact `UpdateChatTitleByID` |
| `TitleGenerationOverrideCallFailure`: nothing stored | gomock fails on
any `UpdateChatTitleByID` |
| `TestQuickgenDebugRun/TitleGeneration`: whether a title was stored |
whether the owner's channel got one event |

One gap needed a test change. Without the `return` after a refused title
write, the code publishes the zero `updatedChat` on the nil owner's
channel, which only the observer caught on `main`
(`quickgen_internal_test.go:688`). The two no-write cases now use a
`psmock` pubsub with no expectations, so any publish fails them.

Mutants of `maybeGenerateChatTitle` (`go test -overlay`) and the tests
they fail:

| Mutant | Fails |
|---|---|
| no `return` after a refused write | `write refused by a rename during
the call` |
| no `return` after a failed model call | `model call failed`,
`TitleGenerationOverrideCallFailure`, `TitleGeneration/ModelFails` |
| writes `title + "!"` | `WritesGeneratedTitle`,
`TitleGenerationOverrideSetUsable`,
`TestMaybeGenerateChatTitleAppliesModelConfigReasoningEffort` |
| no `publishChatPubsubEvent` | `WritesGeneratedTitle`,
`TitleGeneration/Completed` and `/CreateFails` |

## Review focus

- The mock in the two no-write cases is stricter than the old owner
subscription: a publish on any channel fails.
- `TitleGeneration` checks the event because a recorded
`UpdateChatTitleByID` call stays true when the write fails.
`MemoryPubsub.Publish` waits for its subscribers, so `len(events)` is
final when the call returns.

## Validation

- `go test ./coderd/x/chatd/ -count=1 -race -run 'Title|Quickgen'`:
pass.
- Mutants: each fails as listed, and the first passes with only the
observer removed. A copy with a syntax error fails to build, so the
overlay applies.
- `TitleGeneration` fails when `generate` always returns true
(`ModelFails`) or when the title write fails (`Completed`,
`CreateFails`).
- The pre-commit hook (`make pre-commit`) passed.

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-27 10:14:11 +01:00
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
Michael Suchacz a1556f8e62 test(coderd/x/chatd): pin the debug runs that turns, titles and status labels record (#30933)
Adds tests that pin what four call sites record when they create and
finalize a debug run: chat turns, manual titles, automatic titles and
turn status labels. #30934 moves all four onto one helper. Automatic
titles and status labels had no debug-run test, and the chat turn and
manual title tests checked only some fields.

- `TestRunnerDebugTurnEnsureCreatesOnce` now checks every insert
parameter, the full `RunContext` the model call sees, and that the
insert runs on a context that ignores the caller's cancellation but has
a deadline.
- New `TestRunnerDebugTurnEnsureCreateFailureDisablesTurn`: a failed
insert disables debug for the turn, and the turn does not retry it.
- `TestProposeChatTitle_DebugRun` also checks that the run has no
trigger message, its model config and model, the seed label in its
summary, and that it recorded steps.
- New `TestQuickgenDebugRun` covers automatic titles and status labels
when the model succeeds, when it fails, and when the insert fails. It
checks the insert, the run context and the finalize call. Titles record
the configured provider and model; status labels record the resolved
ones.
- New `TestPrepareManualTitleDebugRunCreateFailure`: when the manual
title insert fails, the title call gets no run and finishing does
nothing.

**Why it is safe:** the PR changes only tests, and they pass on `main`'s
production code. To show they can fail, I ran 34 mutants of the four
call sites, and 31 fail the new tests. Two survivors change nothing
these tests can observe: the seed summary on the manual title insert
(finalize rewrites the summary) and the 10 second finalize timeout for
titles and labels. This layer does not pin the third: removing
`context.WithoutCancel` from the title and label insert in
`quickgen.go`. #30934 moves that detach into the shared `BeginRun`,
which the chat turn test pins.

Validation: `go test -race -count=1 -p 4 -parallel 4 ./coderd/x/chatd/
-run
'^(TestRunnerDebugTurn.*|TestProposeChatTitle_DebugRun|TestQuickgenDebugRun|TestPrepareManualTitleDebugRunCreateFailure)$'`
passes on this branch and with `main`'s production code, and the
pre-commit hook passed.

This layer now sits on #30995 instead of #30932. A merge commit moved it
there without a force push, and the layer's own diff is unchanged (same
patch-id). The same command and `go test -race
./coderd/x/chatd/chatdebug/...` pass at the new head.

## 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. This PR: 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. #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 21:43:20 +00:00