feat(hint): persist the vs_ai idle-hint wait (wall-clock + read sanitiser)
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Has been skipped
CI / integration (pull_request) Has been skipped
CI / ui (pull_request) Successful in 1m28s
CI / conformance (pull_request) Successful in 11s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m44s

Per the owner's call, the idle-hint gate now PERSISTS across leaving and reopening the
app, instead of the session-scoped monotonic clock: the unlock is a wall-clock instant
(hintUnlockAtMs) stamped from the robot's reply, stored on the local record, carried on
the game view + the opponent_moved move delta, and read through a sanitiser that caps it
at now + the window. So:
- the wait survives a relaunch (a stuck turn is not forgotten);
- a device clock set BACK cannot freeze the gate (the cap bounds the remaining to the
  window and self-heals on the next read);
- a clock set FORWARD just opens the hint early -- accepted as harmless for a solo game.

- lib/hints.ts hintGateRemainingMs now takes the unlock instant + clamps to the window.
- localgame: re-add hintUnlockAtMs to the record/meta; stamp it off the robot's reply;
  sanitise on read (a shared hintUnlock helper feeds stateView + the opponent_moved event).
- gamedelta + PushEvent: the move delta carries hintUnlockAtMs so the view stays fresh.
- Game.svelte: hintUnlockAt derives from the view; the tick + lock + toast unchanged.
- offline.spec.ts: also assert the lock survives a reload (the wait persisted).
- Docs FUNCTIONAL(+_ru)/ARCHITECTURE updated (persist + sanitiser, not monotonic-resets).
This commit is contained in:
Ilia Denisov
2026-07-06 22:34:50 +02:00
parent 42a6308261
commit ff486c80f8
12 changed files with 107 additions and 64 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ function makeGame(seed: bigint): LocalGame {
return new LocalGame({ variant: 'scrabble_en', version: 'sample', seed, players: 2, dawg, multipleWords: true });
}
const meta = { id: 'g1', seats, createdAtUnix: 1, updatedAtUnix: 2 };
const meta = { id: 'g1', seats, createdAtUnix: 1, updatedAtUnix: 2, hintUnlockAtMs: 0 };
describe('local game serialize + replay', () => {
it('reconstructs a mid-game position by replay', () => {
+7
View File
@@ -38,6 +38,11 @@ export interface LocalGameRecord {
status: 'active' | 'finished';
createdAtUnix: number;
updatedAtUnix: number;
/** Wall-clock ms at which the vs_ai idle hint unlocks (the robot's last move + the window), or 0
* while open (no robot move yet). Persisted so the wait survives leaving and reopening the app;
* read through a sanitiser (cap at now + window) so a device clock set back cannot freeze it —
* see lib/hints. */
hintUnlockAtMs: number;
}
/** The record fields the caller owns (the engine supplies the rest). */
@@ -46,6 +51,7 @@ export interface RecordMeta {
seats: Seat[];
createdAtUnix: number;
updatedAtUnix: number;
hintUnlockAtMs: number;
}
/**
@@ -67,6 +73,7 @@ export function serializeGame(game: LocalGame, meta: RecordMeta): LocalGameRecor
status: game.isOver ? 'finished' : 'active',
createdAtUnix: meta.createdAtUnix,
updatedAtUnix: meta.updatedAtUnix,
hintUnlockAtMs: meta.hintUnlockAtMs,
};
}
+25 -2
View File
@@ -13,6 +13,7 @@ import { serializeGame, replayGame, type LocalGameRecord, type Seat as LocalSeat
import { getLocalGame, saveLocalGame, listLocalGames, deleteLocalGame } from './store';
import { RULESETS } from './ruleset';
import { getDawg } from '../dict';
import { HINT_GATE_MS } from '../hints';
import { LOCAL_ID_PREFIX, isLocalGameId } from './id';
import { decide } from '../robot/strategy';
import { GatewayError, type PlacedTile } from '../client';
@@ -120,6 +121,7 @@ export class LocalSource implements GameLoopSource {
seats: opts.seats.map((s) => ({ kind: s.kind, name: s.name, accountId: s.accountId })),
createdAtUnix: now,
updatedAtUnix: now,
hintUnlockAtMs: 0, // no robot move yet: a human-first opening's hint is open at once
});
const entry: Live = { game, record };
this.live.set(opts.id, entry);
@@ -277,15 +279,20 @@ export class LocalSource implements GameLoopSource {
else if (dec.kind === 'exchange' && g.bagLength >= RULESETS[entry.record.variant].rackSize) out.push(g.exchange(dec.exchange));
else out.push(g.pass());
}
// Arm the idle hint gate from the robot's reply: a wall-clock unlock time, persisted so the wait
// survives a relaunch. Read back through a sanitiser (lib/hints / stateView) so a clock set back
// cannot freeze it.
if (out.length > 0) entry.record.hintUnlockAtMs = Date.now() + HINT_GATE_MS;
return out;
}
private emitRobot(entry: Live, moves: LocalMove[]): void {
const set = this.listeners.get(entry.record.id);
if (!set) return;
const hintUnlockAtMs = this.hintUnlock(entry);
for (const m of moves) {
const game = this.gameView(entry);
const ev: PushEvent = { kind: 'opponent_moved', gameId: entry.record.id, move: this.moveRecord(entry.record.variant, m), game, bagLen: entry.game.bagLength };
const ev: PushEvent = { kind: 'opponent_moved', gameId: entry.record.id, move: this.moveRecord(entry.record.variant, m), game, bagLen: entry.game.bagLength, hintUnlockAtMs };
for (const cb of set) cb(ev);
}
if (entry.game.isOver) {
@@ -301,6 +308,7 @@ export class LocalSource implements GameLoopSource {
seats: entry.record.seats,
createdAtUnix: entry.record.createdAtUnix,
updatedAtUnix: nowUnix(),
hintUnlockAtMs: entry.record.hintUnlockAtMs,
});
await saveLocalGame(entry.record);
}
@@ -333,10 +341,25 @@ export class LocalSource implements GameLoopSource {
// --- shape builders --------------------------------------------------------
// The vs_ai idle-hint unlock instant, sanitised on read: capped at now + the window so a device
// clock set back cannot push it far ahead and freeze the gate (the client counts down from here).
// 0 while open (a human-first opening, no robot move yet).
private hintUnlock(entry: Live): number {
return entry.record.hintUnlockAtMs ? Math.min(entry.record.hintUnlockAtMs, Date.now() + HINT_GATE_MS) : 0;
}
private stateView(entry: Live): StateView {
const seat = this.humanSeat(entry);
const rack = entry.game.handOf(seat).map((t) => indexToLetter(entry.record.variant, t));
return { game: this.gameView(entry), seat, rack, bagLen: entry.game.bagLength, hintsRemaining: 1, walletBalance: 0 };
return {
game: this.gameView(entry),
seat,
rack,
bagLen: entry.game.bagLength,
hintsRemaining: 1,
walletBalance: 0,
hintUnlockAtMs: this.hintUnlock(entry),
};
}
private gameView(entry: Live): GameView {