feat(robot): shrink endgame think time when both sides pass
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Has been skipped
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m17s

In a dead-drawn endgame — the two most recent journal moves are both
passes, so the board and the robot's rack are frozen and the robot is
bound to pass again — the robot still waited out its long late-game think
time (up to 90 min) before passing, needlessly dragging out a decided game.

Shorten that delay to a [0.8, 1.5]x band around the human's last-move think
time (the gap between the last two journal entries), clamped to [30s, 8min]
and taken as a min with the normal schedule, so the robot never moves
slower. A slow human collapses to the 8-min cap; a fast human is tracked,
with the floor keeping the robot from passing suspiciously instantly. The
anchor reads the move journal only (no schema change), stays deterministic
from the seed, and still defers to the sleep window.

RobotTurns now carries EndgamePass + OppLastMove, filled by one batched
journal query on the scan; the honest-AI single-game trigger keeps the
normal path (it moves at once). NextMoveAt (admin ETA) is left as the
normal-schedule upper bound.
This commit is contained in:
Ilia Denisov
2026-06-19 12:48:39 +02:00
parent 0eb1e0ef47
commit f3768d20f2
10 changed files with 342 additions and 7 deletions
+121
View File
@@ -249,3 +249,124 @@ func playHuman(t *testing.T, ctx context.Context, svc *game.Service, gameID, hum
t.Fatalf("human pass: %v", err)
}
}
// countMoves returns the number of committed moves in a game's journal.
func countMoves(t *testing.T, gameID uuid.UUID) int {
t.Helper()
var n int
if err := testDB.QueryRowContext(context.Background(),
`SELECT count(*) FROM backend.game_moves WHERE game_id = $1`, gameID).Scan(&n); err != nil {
t.Fatalf("count moves: %v", err)
}
return n
}
// setLastTwoMoveTimes rewrites the created_at of a game's two most recent journal entries
// so the endgame think-time anchor (their gap, the human's last-move think time) is a known
// value, independent of the millisecond spacing of the manufactured passes.
func setLastTwoMoveTimes(t *testing.T, gameID uuid.UUID, prevAt, lastAt time.Time) {
t.Helper()
ctx := context.Background()
if _, err := testDB.ExecContext(ctx,
`UPDATE backend.game_moves SET created_at = $2
WHERE game_id = $1 AND seq = (SELECT max(seq) FROM backend.game_moves WHERE game_id = $1)`,
gameID, lastAt); err != nil {
t.Fatalf("set last move time: %v", err)
}
if _, err := testDB.ExecContext(ctx,
`UPDATE backend.game_moves SET created_at = $2
WHERE game_id = $1 AND seq = (SELECT max(seq) - 1 FROM backend.game_moves WHERE game_id = $1)`,
gameID, prevAt); err != nil {
t.Fatalf("set prev move time: %v", err)
}
}
// TestRobotEndgamePassShrinksThinkTime checks the dead-drawn-endgame shrink end to end: when
// the two most recent moves are both passes, RobotTurns reports EndgamePass with the human's
// last-move think time (OppLastMove), and the driver answers on the shortened schedule — well
// before the normal late-game delay — rather than dragging out the decided game. The journal
// state is manufactured with direct passes so the test isolates the timing mechanism (the
// SQL anchor and the driver gate) from a full game to an empty bag.
func TestRobotEndgamePassShrinksThinkTime(t *testing.T) {
ctx := context.Background()
svc := newGameService()
robots := newRobotService(t, svc)
if err := robots.EnsurePool(ctx); err != nil {
t.Fatalf("ensure pool: %v", err)
}
robotID, err := robots.Pick(engine.VariantEnglish)
if err != nil {
t.Fatalf("pick: %v", err)
}
human := provisionAccount(t)
seed := openingSeed(t)
g, err := svc.Create(ctx, game.CreateParams{
Variant: engine.VariantEnglish, Seats: []uuid.UUID{human, robotID},
TurnTimeout: 24 * time.Hour, Seed: seed,
})
if err != nil {
t.Fatalf("create: %v", err)
}
const robotSeat = 1
store := game.NewStore(testDB)
turnOf := func(id uuid.UUID) game.RobotTurn {
t.Helper()
turns, err := store.RobotTurns(ctx, []uuid.UUID{robotID})
if err != nil {
t.Fatalf("robot turns: %v", err)
}
for _, rt := range turns {
if rt.GameID == id {
return rt
}
}
t.Fatalf("no robot turn for game %s", id)
return game.RobotTurn{}
}
// One pass so far (the human): not yet a double-pass endgame.
if _, err := svc.Pass(ctx, g.ID, human); err != nil {
t.Fatalf("human pass 1: %v", err)
}
if rt := turnOf(g.ID); rt.EndgamePass {
t.Fatalf("EndgamePass after a single pass = true, want false")
}
// Robot then human pass: the two most recent moves are now both passes and it is the
// robot's turn — the guaranteed-pass endgame state.
if _, err := svc.Pass(ctx, g.ID, robotID); err != nil {
t.Fatalf("robot pass: %v", err)
}
if _, err := svc.Pass(ctx, g.ID, human); err != nil {
t.Fatalf("human pass 2: %v", err)
}
if _, toMove, _, err := svc.Participants(ctx, g.ID); err != nil || toMove != robotSeat {
t.Fatalf("after two passes: toMove %d err %v, want robot seat %d", toMove, err, robotSeat)
}
// Anchor the human's last-move think time to 60s and start the robot's turn at daytime.
setLastTwoMoveTimes(t, g.ID, daytime.Add(-60*time.Second), daytime)
setTurnStarted(t, g.ID, daytime)
rt := turnOf(g.ID)
if !rt.EndgamePass {
t.Fatalf("EndgamePass after a double pass = false, want true")
}
if d := rt.OppLastMove; d < 59*time.Second || d > 61*time.Second {
t.Fatalf("OppLastMove = %s, want ~60s", d)
}
// The normal schedule for this move is at least the early band floor (~3.75 min); the
// 60s-anchored endgame delay is at most 90s for every seed. Driving at +150s is past the
// shrunk delay but well before the normal one, so the robot acts only because of the shrink.
before := countMoves(t, g.ID)
robots.Drive(ctx, daytime.Add(150*time.Second))
if after := countMoves(t, g.ID); after != before+1 {
t.Fatalf("robot did not act on the shrunk endgame schedule: moves %d → %d (want +1)", before, after)
}
if _, toMove, _, err := svc.Participants(ctx, g.ID); err != nil || toMove == robotSeat {
t.Fatalf("after the shrunk move: still the robot's turn (toMove %d, err %v)", toMove, err)
}
}