engine: reject turn buffers that continue past a turn boundary

The whole-turn buffer is replayed from the committed state; committing is
what grants the mover their per-turn time increment. In the degenerate
all-others-dropped case a buffer like [..., Pass, ...more] is a sequence of
individually legal moves spanning several turns — it would commit them all
for a single increment. Throw as soon as a committed move is followed by
more buffer elements.

Also log an error when moveAI's 500-iteration force-commit safety net
fires, so a would-be livelock is visible in server logs instead of being
silently masked.

Tests: malformed buffer rejection (nothing half-applied leaks) and
multi-turn buffer rejection.
This commit is contained in:
coyotte508 2026-08-16 21:26:05 +00:00
commit e36715fedc
2 changed files with 66 additions and 3 deletions

View file

@ -107,6 +107,56 @@ describe('wrapper (tentative turns)', () => {
expect(JSON.parse(JSON.stringify(truncated))).to.deep.equal(JSON.parse(JSON.stringify(shortened)));
});
it('should reject a malformed buffer without leaking anything half-applied', async () => {
const platform = new Platform();
const A = platform.saved.currentPlayers[0];
const before = JSON.parse(JSON.stringify(platform.saved));
// A third loan is illegal (two loans per player at most): the buffer must be
// rejected even though its first two moves are legal.
let error: Error | null = null;
try {
await platform.send([getLoan, getLoan, getLoan], A);
} catch (err) {
error = err as Error;
}
expect(error, 'an illegal move mid-buffer must reject the whole buffer').to.not.be.null;
// Nothing half-applied leaked into the platform's saved state...
expect(JSON.parse(JSON.stringify(platform.saved))).to.deep.equal(before);
// ...and a valid buffer from that same state still works
const ok = await platform.send([getLoan, payLoan, pass], A);
expect(ok.saved).to.be.true;
});
it('should reject a buffer that keeps playing past a turn boundary', async () => {
// Degenerate case: the only other player is dropped, so the same player is up
// again right after passing — [Pass, Pass] is a sequence of individually legal
// moves that spans TWO turns. It must not commit both for one time increment.
const platform = new Platform();
const A = platform.saved.currentPlayers[0];
const B = 1 - A;
platform.saved = await wrapper.dropPlayer(platform.saved, B);
const before = JSON.parse(JSON.stringify(platform.saved));
let error: Error | null = null;
try {
await platform.send([pass, pass], A);
} catch (err) {
error = err as Error;
}
expect(error).to.not.be.null;
expect(error!.message).to.match(/turn boundary/);
expect(JSON.parse(JSON.stringify(platform.saved))).to.deep.equal(before);
// A buffer ending exactly on the turn boundary is still fine
const ok = await platform.send([getLoan, pass], A);
expect(ok.saved).to.be.true;
expect(platform.saved.currentPlayers).to.deep.equal([A]);
});
it('should play a full auction with the right commit points and a never-shrinking log', async () => {
const platform = new Platform();
const A = platform.saved.currentPlayers[0];

View file

@ -38,8 +38,17 @@ export async function move(G: GameState, move: Move | Move[] | null | undefined,
return { ...G, newTurn: false };
}
for (const m of moves) {
G = engine.move(G, m, player);
for (let i = 0; i < moves.length; i++) {
G = engine.move(G, moves[i], player);
// The buffer must describe at most ONE turn: committing is what grants the
// mover their per-turn time increment. Without this guard a buffer like
// `[..., Pass, ...more]` — legal in the degenerate case where the same player
// is up again because everyone else dropped — would replay across the turn
// boundary and commit several turns for a single increment.
if (G.newTurn !== false && i < moves.length - 1) {
throw new Error('The turn buffer continues past a turn boundary: only one turn may be played per call');
}
}
return G;
@ -77,7 +86,11 @@ export function moveAI(G: GameState, player: number): GameState {
// Safety net — should be unreachable (Pass is always available in the move phase
// and commits the turn). The state is consistent (every atomic move was legal and
// logged), it just did not reach a turn boundary; persisting it keeps the game
// going instead of wedging it.
// going instead of wedging it. Shout so a would-be livelock is visible in the
// server logs instead of being silently masked.
console.error(
`moveAI: force-committing after 500 moves without reaching a turn boundary (player ${player}, phase ${G.phase}, round ${G.round})`
);
G.newTurn = true;
return G;