fix(game-engine): sync clock authority during pause

This commit is contained in:
2026-09-03 17:50:26 +00:00
parent b250e908f4
commit 1cb66f76fb
10 changed files with 766 additions and 172 deletions
@@ -0,0 +1,71 @@
import { describe, expect, it, vi } from 'vitest';
import type { GamePrismaClient } from '@sammo-ts/infra';
import {
reconcileClockSuspension,
startClockSuspension,
type ClockReconciliationResult,
type ClockSuspensionResult,
} from '../src/turn/clockReconciliation.js';
const authority = { kind: 'OFFLINE' as const, profileName: 'retry-test', reason: 'isolated unit test' };
describe('serializable clock operation retries', () => {
it('retries a PostgreSQL serialization conflict while starting suspension', async () => {
const result: ClockSuspensionResult = {
suspensionId: 'retry-start',
phase: 'SUSPENDED',
sourceRevision: 7,
targetRevision: 8,
cutTick: 123,
cutWallAt: new Date('2026-09-03T10:00:00.000Z'),
};
const transaction = vi
.fn()
.mockRejectedValueOnce(Object.assign(new Error('could not serialize access'), { code: 'P2034' }))
.mockResolvedValueOnce(result);
const db = { $transaction: transaction } as unknown as GamePrismaClient;
await expect(
startClockSuspension({ db, suspensionId: result.suspensionId, source: 'MAINTENANCE', authority })
).resolves.toEqual(result);
expect(transaction).toHaveBeenCalledTimes(2);
});
it('retries a raw 40001 conflict while reconciling suspension', async () => {
const result: ClockReconciliationResult = {
suspensionId: 'retry-resume',
phase: 'RECONCILING',
sourceRevision: 7,
targetRevision: 8,
deadlineGeneration: 8,
gapTicks: 100,
catchUpTicks: 0,
shiftTicks: 100,
alignedTick: 223,
resumeWallAt: new Date('2026-09-03T10:10:00.000Z'),
};
const transaction = vi
.fn()
.mockRejectedValueOnce(
Object.assign(new Error('SQLSTATE 40001'), { code: 'P2010', meta: { code: '40001' } })
)
.mockResolvedValueOnce(result);
const db = { $transaction: transaction } as unknown as GamePrismaClient;
await expect(reconcileClockSuspension({ db, suspensionId: result.suspensionId, authority })).resolves.toEqual(
result
);
expect(transaction).toHaveBeenCalledTimes(2);
});
it('does not retry a non-serialization failure', async () => {
const transaction = vi.fn().mockRejectedValue(new Error('authority denied'));
const db = { $transaction: transaction } as unknown as GamePrismaClient;
await expect(
startClockSuspension({ db, suspensionId: 'no-retry', source: 'MAINTENANCE', authority })
).rejects.toThrow('authority denied');
expect(transaction).toHaveBeenCalledTimes(1);
});
});
@@ -608,6 +608,12 @@ integration('database command queue', () => {
expectedUpdatedAt: null,
mutation: { kind: 'nationPriority', priority: ['develop'] },
},
{
type: 'shiftSchedule',
requestId: 'integration:engine:suspended-shift-schedule',
actionId: '00000000-0000-4000-8000-000000000023',
deltaMinutes: -15,
},
];
await db.inputEvent.createMany({
data: commands.map((command) => ({
@@ -637,14 +643,18 @@ integration('database command queue', () => {
const claimed = await new DatabaseTurnDaemonCommandQueue(db).drain();
expect(claimed.map(({ type }) => type)).toEqual(commands.map(({ type }) => type));
for (const command of commands) {
await expect(db.inputEvent.findUniqueOrThrow({ where: { requestId: command.requestId! } })).resolves.toMatchObject({
await expect(
db.inputEvent.findUniqueOrThrow({ where: { requestId: command.requestId! } })
).resolves.toMatchObject({
status: 'PROCESSING',
processingGameTick: 777n,
processingClockRevision: 23n,
processingDeadlineGeneration: 9n,
});
}
await expect(db.inputEvent.findUniqueOrThrow({ where: { requestId: blockedRequestId } })).resolves.toMatchObject({
await expect(
db.inputEvent.findUniqueOrThrow({ where: { requestId: blockedRequestId } })
).resolves.toMatchObject({
status: 'PENDING',
processingClockRevision: null,
});
@@ -0,0 +1,174 @@
import { describe, expect, it, vi } from 'vitest';
import type { GamePrisma } from '@sammo-ts/infra';
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
import { synchronizeRuntimeClockAuthorityUnderHeldLock } from '../src/turn/runtimeClockAuthoritySync.js';
import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
const baseTime = new Date('2026-09-03T10:00:00.000Z');
const buildWorld = (phase: 'RUNNING' | 'SUSPENDED' = 'RUNNING'): InMemoryTurnWorld => {
const general = {
id: 1,
name: 'clock-sync-general',
nationId: 1,
cityId: 1,
troopId: 0,
stats: { leadership: 50, strength: 50, intelligence: 50 },
turnTime: new Date('2026-09-03T10:10:00.000Z'),
turnTick: 36_000_000,
role: { items: { horse: null, weapon: null, book: null, item: null } },
triggerState: { flags: {}, counters: {}, modifiers: {}, meta: {} },
meta: { killturn: 24 },
officerLevel: 5,
experience: 0,
dedication: 0,
injury: 0,
gold: 1000,
rice: 1000,
crew: 0,
crewTypeId: 0,
train: 0,
atmos: 0,
age: 30,
npcState: 0,
} as TurnGeneral;
const state: TurnWorldState = {
id: 1,
currentYear: 190,
currentMonth: 1,
tickSeconds: 600,
lastTurnTime: baseTime,
clockBaseTime: baseTime,
clockTick: 0,
clockMode: 'realtime',
clockWallAnchor: baseTime,
lastTurnTick: 0,
clockPhase: phase,
clockRevision: 3,
deadlineGeneration: 5,
meta: { lastTurnTime: baseTime.toISOString() },
};
const snapshot: TurnWorldSnapshot = {
generals: [general],
cities: [],
nations: [],
troops: [],
diplomacy: [],
events: [],
initialEvents: [],
map: {
id: 'clock-sync',
name: 'clock-sync',
cities: [],
defaults: { trust: 50, trade: 100, supplyState: 1, frontState: 0 },
},
scenarioConfig: {
stat: { total: 300, min: 10, max: 100, npcTotal: 150, npcMax: 50, npcMin: 10, chiefMin: 70 },
iconPath: '',
map: {},
const: {},
environment: { mapName: 'test', unitSet: 'default' },
},
};
return new InMemoryTurnWorld(state, snapshot, {
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
});
};
const buildDb = (worldState: Record<string, unknown>, ledgers: unknown[] = []): GamePrisma.TransactionClient =>
({
worldState: { findFirst: vi.fn().mockResolvedValue(worldState) },
clockSuspension: { findMany: vi.fn().mockResolvedValue(ledgers) },
}) as unknown as GamePrisma.TransactionClient;
describe('runtime clock authority synchronization', () => {
it('adopts a maintenance suspension cut without advancing the game schedule', async () => {
const world = buildWorld('RUNNING');
const cutWallAt = new Date('2026-09-03T10:02:00.000Z');
const beforeTurnTick = world.getGeneralById(1)!.turnTick;
const db = buildDb({
id: 1,
clockBaseTime: baseTime,
clockTick: 7_200_000n,
clockMode: 'realtime',
clockWallAnchor: cutWallAt,
lastTurnTick: 0n,
clockPhase: 'SUSPENDED',
clockRevision: 3n,
deadlineGeneration: 5n,
});
await expect(synchronizeRuntimeClockAuthorityUnderHeldLock(db, world)).resolves.toBe(true);
expect(world.getGameClockState()).toMatchObject({
phase: 'SUSPENDED',
tick: 7_200_000,
revision: 3,
deadlineGeneration: 5,
wallAnchor: cutWallAt,
});
expect(world.getGeneralById(1)!.turnTick).toBe(beforeTurnTick);
});
it('replays the durable reconciliation shift before returning to RUNNING', async () => {
const world = buildWorld('SUSPENDED');
const resumeWallAt = new Date('2026-09-03T11:00:00.000Z');
const shiftTicks = 5_000;
const beforeTurnTick = world.getGeneralById(1)!.turnTick!;
const db = buildDb(
{
id: 1,
clockBaseTime: baseTime,
clockTick: 5_000n,
clockMode: 'realtime',
clockWallAnchor: resumeWallAt,
lastTurnTick: 5_000n,
clockPhase: 'RUNNING',
clockRevision: 4n,
deadlineGeneration: 6n,
},
[
{
id: 'maintenance-revision-3',
sourceRevision: 3n,
targetRevision: 4n,
shiftTicks: BigInt(shiftTicks),
alignedTick: 5_000n,
resumeWallAt,
},
]
);
await expect(synchronizeRuntimeClockAuthorityUnderHeldLock(db, world)).resolves.toBe(true);
expect(world.getGameClockState()).toMatchObject({
phase: 'RUNNING',
tick: 5_000,
lastTurnTick: 5_000,
revision: 4,
deadlineGeneration: 6,
wallAnchor: resumeWallAt,
});
expect(world.getGeneralById(1)!.turnTick).toBe(beforeTurnTick + shiftTicks);
});
it('rejects a revision jump when the durable ledger chain is incomplete', async () => {
const world = buildWorld('SUSPENDED');
const db = buildDb({
id: 1,
clockBaseTime: baseTime,
clockTick: 1n,
clockMode: 'realtime',
clockWallAnchor: baseTime,
lastTurnTick: 1n,
clockPhase: 'RUNNING',
clockRevision: 4n,
deadlineGeneration: 6n,
});
await expect(synchronizeRuntimeClockAuthorityUnderHeldLock(db, world)).rejects.toThrow(
/ledger chain ended at 3\/5/
);
});
});