fix: 통일 완료 뒤 턴 실행을 중단

Ref처럼 isunited 2/3에서 예약 스케줄을 멈추고 processor 직접 호출도 no-op 처리한다. 이미 기본값으로 내려간 종료 게임의 refreshLimit만 식별해 복구하는 마이그레이션과 회귀 테스트를 추가한다.
This commit is contained in:
2026-08-20 06:13:28 +00:00
parent 107b433d85
commit 92ad49db45
9 changed files with 83 additions and 2 deletions
@@ -152,6 +152,12 @@ export class TurnDaemonLifecycle {
this.status.state = 'idle';
}
if ((await this.stateStore.shouldHaltScheduledRuns?.()) ?? false) {
this.status.nextTurnTime = undefined;
await this.clock.sleepMs(500);
continue;
}
if (this.pendingRun) {
await this.runOnce(this.pendingRun);
this.pendingRun = null;
+1
View File
@@ -52,6 +52,7 @@ export interface TurnStateStore {
saveLastTurnTime(turnTime: Date): Promise<void>;
loadCheckpoint(): Promise<TurnCheckpoint | undefined>;
saveCheckpoint(checkpoint?: TurnCheckpoint): Promise<void>;
shouldHaltScheduledRuns?(): Promise<boolean>;
loadGameClock?(wallNow?: Date): Promise<{ mode: GameClockMode; now: Date }>;
advanceGameClockTo?(target: Date, wallNow: Date): Promise<void>;
}
@@ -1,4 +1,5 @@
import type { TurnCheckpoint, TurnStateStore } from '../lifecycle/types.js';
import { asNumber, asRecord } from '@sammo-ts/common';
import type { InMemoryTurnWorld } from './inMemoryWorld.js';
export class InMemoryTurnStateStore implements TurnStateStore {
@@ -29,6 +30,11 @@ export class InMemoryTurnStateStore implements TurnStateStore {
this.world.setCheckpoint(checkpoint);
}
async shouldHaltScheduledRuns(): Promise<boolean> {
const meta = asRecord(this.world.getState().meta);
return asNumber(meta.isunited ?? meta.isUnited, 0) >= 2;
}
async loadGameClock(wallNow = new Date(Date.now())): Promise<{ mode: 'realtime' | 'manual'; now: Date }> {
return {
mode: this.world.getGameClockState().mode,
@@ -51,6 +51,17 @@ export class InMemoryTurnProcessor implements TurnProcessor {
const deadlineMs = startMs + Math.max(0, budget.budgetMs);
const isBudgetExpired = () => Date.now() >= deadlineMs;
if (isWorldUnited(this.world)) {
return {
lastTurnTime: this.world.getState().lastTurnTime.toISOString(),
processedGenerals: 0,
processedTurns: 0,
durationMs: Math.max(0, Date.now() - startMs),
partial: false,
checkpoint,
};
}
this.world.setCheckpoint(checkpoint);
this.world.updateWorldMeta({
refreshLimit: calculateAccessRefreshLimit(this.world.getState().tickSeconds),
@@ -14,6 +14,40 @@ import {
const addMinutes = (time: Date, minutes: number): Date => new Date(time.getTime() + minutes * 60_000);
describe('TurnDaemonLifecycle', () => {
it('does not schedule another processor run after the world reaches a terminal united state', async () => {
const clock = new ManualClock(new Date('2026-01-01T00:00:00.000Z').getTime());
const controlQueue = new InMemoryControlQueue();
const processor = { run: vi.fn() };
const lifecycle = new TurnDaemonLifecycle(
{
clock,
controlQueue,
getNextTickTime: (value) => addMinutes(value, 60),
stateStore: {
loadLastTurnTime: async () => new Date('2042-01-01T00:00:00.000Z'),
loadNextGeneralTurnTime: async () => new Date('2042-01-01T00:30:00.000Z'),
saveLastTurnTime: async () => {},
loadCheckpoint: async () => undefined,
saveCheckpoint: async () => {},
shouldHaltScheduledRuns: async () => {
controlQueue.enqueue({ type: 'shutdown', reason: 'terminal world verified' });
return true;
},
},
processor,
},
{
profile: 'terminal-united',
defaultBudget: { budgetMs: 100, maxGenerals: 10, catchUpCap: 1 },
}
);
await lifecycle.start();
expect(processor.run).not.toHaveBeenCalled();
expect(lifecycle.getStatus().nextTurnTime).toBeUndefined();
});
it('runs manual game time to each monthly snapshot without waiting for wall time', async () => {
const wallNow = new Date('2026-01-01T00:00:00.000Z');
const operationalClock = new ManualClock(wallNow.getTime());
+9
View File
@@ -286,5 +286,14 @@ describe('InMemoryTurnProcessor ordering', () => {
expect(result.processedTurns).toBe(1);
expect(world.getState()).toMatchObject({ currentYear: 189, currentMonth: 2, meta: { isUnited: 2 } });
world.updateWorldMeta({ refreshLimit: 12_000 });
const haltedResult = await processor.run(addMinutes(baseTime, 60), {
budgetMs: 1_000,
maxGenerals: 10,
catchUpCap: 10,
});
expect(haltedResult).toMatchObject({ processedGenerals: 0, processedTurns: 0 });
expect(world.getState().meta).toMatchObject({ isUnited: 2, refreshLimit: 12_000 });
});
});
+1 -1
View File
@@ -39,7 +39,7 @@ describe('readReleaseManifest', () => {
await expect(readReleaseManifest(workspaceRoot)).resolves.toMatchObject({
controllerProtocol: RELEASE_CONTROLLER_PROTOCOL,
gatewaySchemaHead: '20260819000000_backfill_profile_release_source',
gameSchemaHead: '20260820000000_widen_emperor_officer_pictures',
gameSchemaHead: '20260820001000_restore_united_turn_halt',
});
});
@@ -0,0 +1,14 @@
-- isunited 2/3에서 Ref 턴 실행은 중단되고, 통일 시 늘린 refreshLimit가 유지된다.
-- 기존 core daemon pass가 기본값으로 되돌린 행만 식별해 100배 값을 복구한다.
UPDATE "world_state"
SET "meta" = jsonb_set(
"meta",
'{refreshLimit}',
to_jsonb((("meta" ->> 'refreshLimit')::integer * 100)),
true
)
WHERE COALESCE("meta" ->> 'isunited', "meta" ->> 'isUnited', '0') ~ '^[0-9]+$'
AND COALESCE("meta" ->> 'isunited', "meta" ->> 'isUnited', '0')::integer >= 2
AND COALESCE("meta" ->> 'refreshLimit', '') ~ '^[0-9]+$'
AND ("meta" ->> 'refreshLimit')::integer =
ROUND(POWER("tick_seconds"::numeric / 60, 0.6) * 3)::integer * 10;
+1 -1
View File
@@ -2,6 +2,6 @@
"formatVersion": 1,
"controllerProtocol": 2,
"gatewaySchemaHead": "20260819000000_backfill_profile_release_source",
"gameSchemaHead": "20260820000000_widen_emperor_officer_pictures",
"gameSchemaHead": "20260820001000_restore_united_turn_halt",
"components": ["gateway-api", "gateway-frontend", "release-controller", "game-api", "game-engine", "game-frontend"]
}