feat(robot): occasional off-strategy deviation, strict in the endgame
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Has been skipped
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m9s

The robot followed its per-game playToWin/lose intent on every move, which made
the outcome too predictable. It now flips that intent for a single move on ~20%
of opening/midgame turns (a winning robot eases off, a losing one surges ahead),
so the chosen strategy may not pan out — which favours the human. The chance
tapers linearly to 0 over the last 14 tiles in the bag and is 0 once the bag is
empty, so the endgame follows the chosen strategy strictly.

The decision is deterministic from the seed (mix(seed,"deviate",moveCount)) and
applies to both robot paths via the shared selectMove; the per-game play-to-win
intent the admin card shows is unchanged. Adds deviateProb/deviates helpers and
unit tests (taper bounds + monotonicity, never-in-endgame, determinism, ~20%
distribution); bakes the behaviour into ARCHITECTURE §7, FUNCTIONAL (+_ru),
backend/README, PRERELEASE and PLAN Stage 5.
This commit is contained in:
Ilia Denisov
2026-06-15 21:37:23 +02:00
parent 9e6899bb7d
commit 3bceafbc12
9 changed files with 156 additions and 16 deletions
+12 -5
View File
@@ -67,7 +67,8 @@ func (s *Service) handle(ctx context.Context, rt game.RobotTurn, now time.Time)
// Honest-AI game: the robot moves the instant it is its turn — no sleep window and
// no proactive nudge (chat and nudge are disabled in these games, and the player
// chose an opponent that "moves at once"). It still plays to the same per-game
// strength (playToWin) and margin band as the human-mimicry path.
// strength (playToWin), margin band and occasional off-strategy deviation as the
// human-mimicry path.
if rt.VsAI {
if rt.ToMove == rt.RobotSeat {
return s.act(ctx, rt, now)
@@ -164,9 +165,11 @@ func (s *Service) maybeNudge(ctx context.Context, rt game.RobotTurn, now time.Ti
return nil
}
// act reads the live turn, chooses a move by margin and submits it. State that
// has moved on since the scan (a finished game, a turn that is no longer the
// robot's) surfaces as a benign error and is skipped.
// act reads the live turn, chooses a move by margin — usually toward the robot's
// per-game intent, but with an occasional off-strategy deviation that fades to none
// as the bag empties — and submits it. State that has moved on since the scan (a
// finished game, a turn that is no longer the robot's) surfaces as a benign error
// and is skipped.
func (s *Service) act(ctx context.Context, rt game.RobotTurn, now time.Time) error {
st, err := s.games.GameState(ctx, rt.GameID, rt.RobotID)
if err != nil {
@@ -179,7 +182,11 @@ func (s *Service) act(ctx context.Context, rt game.RobotTurn, now time.Time) err
myScore := st.Game.Seats[st.Seat].Score
oppScore := bestOpponentScore(st.Game.Seats, st.Seat)
d := selectMove(cands, myScore, oppScore, playToWin(rt.Seed), defaultBand, st.Rack, st.BagLen)
win := playToWin(rt.Seed)
if deviates(rt.Seed, rt.MoveCount, st.BagLen) {
win = !win // an occasional off-strategy move; never once the bag is empty
}
d := selectMove(cands, myScore, oppScore, win, defaultBand, st.Rack, st.BagLen)
var res game.MoveResult
switch d.kind {
+37
View File
@@ -23,6 +23,15 @@ const (
// human wins about 60% of games (docs/ARCHITECTURE.md §7).
playToWinPercent = 40
// The robot occasionally plays a single move against its per-game win/lose
// intent (an off-strategy "wobble"), so the chosen strategy may not pan out —
// which favours the human. deviateMaxProb is the peak probability of that, held
// through the opening and midgame; it tapers linearly to 0 over the last
// deviateTaperTiles tiles left in the bag, reaching 0 once the bag is empty, so
// the endgame follows the chosen strategy strictly (docs/ARCHITECTURE.md §7).
deviateMaxProb = 0.20
deviateTaperTiles = 14
// The robot's think time depends on how far the game has progressed: early moves
// are quick and late moves can be long (endgame deliberation). The delay is drawn
// from a band that interpolates with the move count from [delayEarlyLoMinutes,
@@ -129,6 +138,34 @@ func PlayToWin(seed int64) bool { return playToWin(seed) }
// win in any given game (the admin console shows it alongside the per-game decision).
const PlayToWinTargetPercent = playToWinPercent
// deviateProb is the probability that the robot plays a single move against its
// per-game win/lose intent, given the number of tiles left in the bag. It is
// deviateMaxProb through the opening and midgame, then tapers linearly to 0 over
// the last deviateTaperTiles tiles, reaching 0 once the bag is empty so the endgame
// follows the chosen strategy strictly.
func deviateProb(bagLen int) float64 {
switch {
case bagLen <= 0:
return 0
case bagLen >= deviateTaperTiles:
return deviateMaxProb
default:
return deviateMaxProb * float64(bagLen) / float64(deviateTaperTiles)
}
}
// deviates reports whether the robot deviates from its per-game win/lose intent on
// the move at moveCount: a deterministic per-turn draw (mix/unitFloat, like the
// think-time sampling) against deviateProb(bagLen), so it is reproducible across
// restarts and never fires once the bag is empty.
func deviates(seed int64, moveCount, bagLen int) bool {
p := deviateProb(bagLen)
if p <= 0 {
return false
}
return unitFloat(mix(seed, "deviate", moveCount)) < p
}
// NextMoveAt is the deterministic instant the robot is scheduled to play the move at
// moveCount, given when the turn started and the opponent's timezone (which anchors the
// robot's sleep window). It is the sampled think-time delay, deferred to the end of the
+72
View File
@@ -1,6 +1,7 @@
package robot
import (
"math"
"sort"
"testing"
"time"
@@ -238,6 +239,77 @@ func TestPlayToWinExport(t *testing.T) {
}
}
// TestDeviateProbTaper checks the deviation probability is deviateMaxProb while the
// bag holds at least deviateTaperTiles tiles, halves at the taper midpoint, is 0
// once the bag is empty, and stays within [0, deviateMaxProb] and non-decreasing.
func TestDeviateProbTaper(t *testing.T) {
if p := deviateProb(0); p != 0 {
t.Errorf("deviateProb(0) = %v, want 0 (strict endgame)", p)
}
if p := deviateProb(deviateTaperTiles); p != deviateMaxProb {
t.Errorf("deviateProb(%d) = %v, want %v", deviateTaperTiles, p, deviateMaxProb)
}
if p := deviateProb(deviateTaperTiles + 50); p != deviateMaxProb {
t.Errorf("deviateProb above the taper = %v, want %v (capped)", p, deviateMaxProb)
}
if p := deviateProb(deviateTaperTiles / 2); math.Abs(p-deviateMaxProb/2) > 1e-9 {
t.Errorf("deviateProb at half taper = %v, want ~%v", p, deviateMaxProb/2)
}
prev := -1.0
for bag := 0; bag <= deviateTaperTiles+5; bag++ {
p := deviateProb(bag)
if p < 0 || p > deviateMaxProb {
t.Fatalf("deviateProb(%d) = %v out of [0,%v]", bag, p, deviateMaxProb)
}
if p < prev {
t.Fatalf("deviateProb not non-decreasing: bag %d gives %v after %v", bag, p, prev)
}
prev = p
}
}
// TestDeviatesNeverInEndgame checks the robot never deviates once the bag is empty,
// for every seed and move count, so the endgame follows the chosen strategy strictly.
func TestDeviatesNeverInEndgame(t *testing.T) {
for seed := int64(1); seed <= 5000; seed++ {
for mc := 0; mc < 40; mc++ {
if deviates(seed, mc, 0) {
t.Fatalf("deviates with an empty bag for seed=%d mc=%d", seed, mc)
}
}
}
}
// TestDeviatesDeterministic checks the per-turn deviation draw is reproducible for a
// (seed, moveCount, bagLen), so the driver recomputes the same decision on every scan.
func TestDeviatesDeterministic(t *testing.T) {
for seed := int64(1); seed <= 500; seed++ {
for mc := 0; mc < 30; mc++ {
got := deviates(seed, mc, deviateTaperTiles)
if deviates(seed, mc, deviateTaperTiles) != got {
t.Fatalf("deviates not deterministic for seed=%d mc=%d", seed, mc)
}
}
}
}
// TestDeviatesDistribution checks the deviation rate over many games lands near
// deviateMaxProb while the bag is full (above the taper), at a fixed move count.
func TestDeviatesDistribution(t *testing.T) {
const n = 20000
hits := 0
for seed := int64(1); seed <= n; seed++ {
if deviates(seed, 3, deviateTaperTiles+20) {
hits++
}
}
pct := float64(hits) / float64(n) * 100
want := deviateMaxProb * 100
if pct < want-2 || pct > want+2 {
t.Errorf("deviation rate = %.1f%%, want ~%.0f%% (±2)", pct, want)
}
}
// TestProactiveNudgeGap checks the proactive-nudge schedule: the first gap (refIdle 0) is
// ~60-90 min, every gap stays within [60 min, 6 h] and is deterministic, and the gap lengthens
// as the idle grows (the median at 12 h idle exceeds the median at the start).