Deletes the runner's secondary task index. `spawnTaskIfNeeded` skipped a
spawn when the active task had the same kind, history version, and
status, but that guard cannot fire. The PR removes the guard, the
`tasksByIndex` map that only it read, the `taskIndexKey` type, the
`taskRecord` fields that fed the index, and a redundant check before
`cancelActiveTask`. It renames `spawnTaskIfNeeded` to `spawnTask` and
deletes `TestRunner_CleansUpOnOwnershipTakeover`, whose steps all repeat
in `TestWorker_CleanupStopsRoutingAndCancelsTasks`.
## Why it is safe
- Only `spawnTaskIfNeeded` sets `activeTaskSet`, and `processState`
calls `acceptState` (which sets `hasAcceptedState`) right after every
spawn. On each later event, `processState` cancels the active task,
which clears `activeTaskSet`, before it spawns. So the guard's first
operand is always false.
- For the same reason, `activeTaskSet` implies `hasAcceptedState`, so
`processState` drops the `r.hasAcceptedState && r.activeTaskSet` check
and calls `cancelActiveTask`, which already returns when no task is
active.
- With the guard replaced by `if r.activeTaskSet { panic(...) }`, `go
test ./coderd/x/chatd/` and `go test ./coderd/ -run Chat` pass.
- `localLocks` still serializes a canceled task with its same-key
replacement
(`TestRunner_SerializesReplacementTasksForSameHistoryAndStatus`), and
the task-ID map stays.
- `git grep` finds no other use of `taskIndexKey`, `tasksByIndex`, or
`spawnTaskIfNeeded`.
Each deleted check, its copy in the remaining test, and a mutant that
fails both tests on `main` and the remaining test here:
| Deleted check | Remaining check | Mutant |
|---|---|---|
| first generation task starts | same `waitCall` | `running` spawns an
interrupt task |
| takeover cancels the task | same `requireTaskCanceled` | no ownership
check; no `requestCleanup` |
| no task starts after takeover | `assertNoCall` after one more publish
| takeover cancels the task and spawns an abandon task |
## Review focus
The dead-guard argument depends on the order in `processState`: cancel
the active task, spawn, then `acceptState`. The `newRunner` and
`taskRecord` hunks otherwise only realign fields after gofmt.
## Validation
- `go test ./coderd/x/chatd/ -race -count=1 -run
'^(TestRunner_|TestWorker)'`: 67 tests pass on `main`, 66 on this
branch.
- `go test ./coderd/x/chatd/ -count=1`: pass.
- Mutants (`go test -overlay`): each fails as listed. A copy with a
syntax error fails to build, so the overlay applies.
- The pre-commit hook (`make pre-commit`) passed.
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
156 lines
5.5 KiB
Go
156 lines
5.5 KiB
Go
package chatd //nolint:testpackage // Uses unexported chatworker helpers.
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/testutil"
|
|
"github.com/coder/quartz"
|
|
)
|
|
|
|
func TestRunner_IgnoresDuplicateStateNotifications(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
starter := newBlockingTaskStarter(false)
|
|
startWorker(t, testOptions(t, f, starter))
|
|
starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
latest, err := f.db.GetChatByID(testutil.Context(t, testutil.WaitShort), chat.ID)
|
|
require.NoError(t, err)
|
|
|
|
publishChatUpdate(t, f, latest)
|
|
publishChatUpdate(t, f, latest)
|
|
starter.assertNoCall(t)
|
|
}
|
|
|
|
func TestRunner_CancelsActiveTaskWhenHistoryChanges(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
starter := newBlockingTaskStarter(false)
|
|
startWorker(t, testOptions(t, f, starter))
|
|
first := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
|
|
updated := commitAssistantStep(t, f, chat.ID, "first step")
|
|
require.Greater(t, updated.HistoryVersion, first.input.HistoryVersion)
|
|
requireTaskCanceled(t, first)
|
|
require.NotErrorIs(t, context.Cause(first.ctx), errTaskTimeout)
|
|
second := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
require.Equal(t, updated.HistoryVersion, second.input.HistoryVersion)
|
|
require.Same(t, first.input.SessionStart, second.input.SessionStart)
|
|
}
|
|
|
|
func TestRunner_CancelsActiveTaskWhenStatusChanges(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
starter := newBlockingTaskStarter(false)
|
|
startWorker(t, testOptions(t, f, starter))
|
|
first := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
|
|
updated := interruptChat(t, f, chat.ID)
|
|
require.Equal(t, database.ChatStatusInterrupting, updated.Status)
|
|
requireTaskCanceled(t, first)
|
|
second := starter.waitCall(t, taskKindInterrupt, chat.ID)
|
|
require.Equal(t, updated.HistoryVersion, second.input.HistoryVersion)
|
|
}
|
|
|
|
func TestRunner_SerializesReplacementTasksForSameHistoryAndStatus(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
starter := newBlockingTaskStarter(true)
|
|
defer starter.releaseAll()
|
|
startWorker(t, testOptions(t, f, starter))
|
|
first := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
|
|
forceExecutionStateAndPublish(t, f, chat.ID, database.ChatStatusInterrupting, false)
|
|
starter.waitCall(t, taskKindInterrupt, chat.ID)
|
|
forceExecutionStateAndPublish(t, f, chat.ID, database.ChatStatusRunning, false)
|
|
starter.assertNoCall(t)
|
|
|
|
starter.release(t, 0)
|
|
replacement := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
require.Equal(t, first.input.HistoryVersion, replacement.input.HistoryVersion)
|
|
}
|
|
|
|
func TestRunner_AllowsReplacementForDifferentHistoryOrStatus(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
starter := newBlockingTaskStarter(true)
|
|
defer starter.releaseAll()
|
|
startWorker(t, testOptions(t, f, starter))
|
|
first := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
|
|
updated := commitAssistantStep(t, f, chat.ID, "different history")
|
|
second := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
require.Greater(t, second.input.HistoryVersion, first.input.HistoryVersion)
|
|
require.Equal(t, updated.HistoryVersion, second.input.HistoryVersion)
|
|
}
|
|
|
|
func TestRunner_TaskTimeoutRetries(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
clock := quartz.NewMock(t).WithLogger(quartz.NoOpLogger)
|
|
timeoutTrap := clock.Trap().AfterFunc("chatworker", "task-timeout-generation")
|
|
starter := newBlockingTaskStarter(false)
|
|
opts := testOptions(t, f, starter)
|
|
opts.Clock = clock
|
|
opts.TaskRetryInitialBackoff = time.Minute
|
|
opts.TaskRetryMaxBackoff = time.Minute
|
|
startWorker(t, opts)
|
|
|
|
timeoutTrap.MustWait(testutil.Context(t, testutil.WaitLong)).MustRelease(testutil.Context(t, testutil.WaitLong))
|
|
timeoutTrap.Close()
|
|
first := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
retryTrap := clock.Trap().NewTimer("chatworker", "task-retry-generation")
|
|
defer retryTrap.Close()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
clock.Advance(defaultTaskTimeout).MustWait(ctx)
|
|
retryTrap.MustWait(ctx).MustRelease(ctx)
|
|
require.ErrorIs(t, context.Cause(first.ctx), errTaskTimeout)
|
|
clock.Advance(time.Minute).MustWait(ctx)
|
|
second := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
require.Equal(t, first.input.HistoryVersion, second.input.HistoryVersion)
|
|
}
|
|
|
|
func TestWorker_RoutesDatabaseSyncStateToActiveRunner(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
clock := quartz.NewMock(t).WithLogger(quartz.NoOpLogger)
|
|
starter := newBlockingTaskStarter(false)
|
|
opts := testOptions(t, f, starter)
|
|
opts.Clock = clock
|
|
opts.RunnerSyncInterval = time.Minute
|
|
startWorker(t, opts)
|
|
first := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
|
|
forceExecutionState(t, f, chat.ID, database.ChatStatusInterrupting, false)
|
|
clock.Advance(time.Minute).MustWait(testutil.Context(t, testutil.WaitLong))
|
|
requireTaskCanceled(t, first)
|
|
starter.waitCall(t, taskKindInterrupt, chat.ID)
|
|
}
|
|
|
|
func TestWorker_CleanupStopsRoutingAndCancelsTasks(t *testing.T) {
|
|
t.Parallel()
|
|
f := newWorkerTestFixture(t)
|
|
chat := f.createRunningChat(t)
|
|
starter := newBlockingTaskStarter(false)
|
|
startWorker(t, testOptions(t, f, starter))
|
|
first := starter.waitCall(t, taskKindGeneration, chat.ID)
|
|
|
|
latest := acquireChat(t, f, chat.ID, uuid.New(), uuid.New())
|
|
requireTaskCanceled(t, first)
|
|
publishChatUpdate(t, f, latest)
|
|
starter.assertNoCall(t)
|
|
}
|