Chat queue: handle usage refusals only after start-up recovery; fix shell share test ordering

TestAutoSwitchReplacesTheAutoContinue flaked because a usage refusal (delivered
on its own goroutine) was acted on while start-up recovery was still running.
resumeQueued read the durable auto-continue row, the server's account switch
then removed it, and resumeQueued put it back into the room; as the queue head
scheduled an hour out it held the switch's continue prompt behind it. The
refusal handler (and the schedule_followup tool) now wait for the same
recovery gate chat_* requests already wait for, and the refusal watcher is
registered only once the hub is fully wired.

TestChatShellShareQueuesAnOrdinaryPrompt flaked because the turn runner writes
the sender's reply straight to its connection while the echoed user turn goes
through the subscription writer, so the reply can arrive first and the test
discarded it while reading for the echo. The test now accepts either order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:24:22 +00:00
parent accb9970b6
commit 026942b9b7
7 changed files with 101 additions and 12 deletions
@@ -2,6 +2,7 @@ package server
import (
"context"
"path/filepath"
"slices"
"strings"
"testing"
@@ -161,3 +162,52 @@ func TestRestartedServerDoesNotScheduleTheRefusalTwice(t *testing.T) {
t.Fatalf("queued after the restart = %v", ops)
}
}
// A refusal reported while start-up recovery is still restoring the durable
// queue waits for it. Acted on earlier, recovery could read the queued
// auto-continue and put it back after the server's switch had removed it, so
// the switch's continue prompt waited behind it for the hour.
func TestUsageRefusalWaitsForStartupRecovery(t *testing.T) {
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
if err := store.RecordOperatorSession(context.Background(), "ses_1", "/work", "Fix ses_1", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
broker := &refusingChat{channelChat: newChannelChat(), quiet: true}
broker.switch_ = func(claude.UsageRefusal) (claude.AutoSwitch, error) {
return claude.AutoSwitch{}, claude.ErrNoAccountLeft
}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
hub := newChatHub()
hub.baseCtx = ctx
hub.registry = store
hub.settings = store
hub.queueStore = store
hub.liveStore = store
ready := make(chan struct{})
hub.recoveryReady = ready
hub.watchUsageRefusals(broker)
handled := make(chan struct{})
go func() {
defer close(handled)
broker.refuse(claude.UsageRefusal{SessionID: "ses_1", TurnID: "claude-1", MessageID: "refusal-1", ResetsAt: time.Now().Add(time.Hour)})
}()
select {
case <-handled:
t.Fatal("the refusal was handled before start-up recovery finished")
case <-time.After(100 * time.Millisecond):
}
if ops := queuedOps(t, store); len(ops) != 0 {
t.Fatalf("queued during recovery = %v", ops)
}
close(ready)
select {
case <-handled:
case <-time.After(5 * time.Second):
t.Fatal("the refusal was not handled after recovery")
}
if ops := queuedOps(t, store); !slices.Equal(ops, []string{"auto-continue-refusal-1"}) {
t.Fatalf("queued = %v", ops)
}
}