refactor(#74) P4: wire engine/stub/db through the ABILITIES registry

Behavior-preserving rewire: every per-ability code path now iterates the
ordered ABILITIES registry instead of hand-enumerating the eight fields.

- engine.ts: buildSystemPrompt (schema lines + constraint bullets),
  buildUserPrompt (payload keys at their historical positions),
  validateTurnResult (per-field validate), applyTurnResult (ordered apply
  block), isMeaningful, and the runTurn "unclear" fallback all drive off
  ABILITIES. The eight moved ability bodies (~1,249 lines) are deleted;
  the names the engine and its tests import are re-exported from
  ./abilities.
- abilities/types.ts: Ability.stub is a zero-arg method (the stub has no
  GameState/AbilityCtx to pass; it needs none) — all eight implementations
  were already arg-less.
- stub.ts: deterministicTurn fills the eight ability fields from the
  registry (each contributes its inert default), between the core fields
  and markers — JSON output stays byte-identical (verified against
  origin/master).
- db.ts: the legacy-row migration for ability-owned GameState fields
  (wars/trades/embargoes/industry) iterates the registry's stateFields,
  only-if-undefined, instead of a hardcoded field list.

No client changes, no new deps, no shared/ runtime deps. typecheck + lint
+ 511 unit tests green; stub JSON byte-identical to origin/master.

Fixes #74
This commit is contained in:
2026-10-16 07:23:11 +00:00
parent 2d17c743f6
commit 15a1642eec
4 changed files with 129 additions and 1612 deletions
+15 -11
View File
@@ -4,6 +4,7 @@ import path from 'node:path';
import Database from 'better-sqlite3';
import { migrateRegionStats } from '../shared/protocol';
import type { GameState } from '../shared/types';
import { ABILITIES } from './abilities';
export interface Db {
save(state: GameState): void;
@@ -49,17 +50,20 @@ export function openDb(filePath: string): Db {
if (state.pendingNationOps === undefined) state.pendingNationOps = [];
// (v0.27, #44) Same for map markers: a legacy game has none placed.
if (state.markers === undefined) state.markers = [];
// (v0.33.0, #56) Same for named wars: a legacy game has none declared
// (any pre-v0.33 `war` relation stays a bare/implicit relation — see
// syncWarRelations, which never force-creates a War entity).
if (state.wars === undefined) state.wars = [];
// (v0.35.0, #58) Same for the trade & embargo economy layer: a
// legacy game has no active trades or embargoes.
if (state.trades === undefined) state.trades = [];
if (state.embargoes === undefined) state.embargoes = [];
// (v0.36, #59) Same for the industry layer: a legacy game has no
// active industry projects.
if (state.industry === undefined) state.industry = [];
// (v0.33.0 #56, v0.35.0 #58, v0.36 #59, v0.40.0 #67) Same for every
// ability-owned GameState field (wars / trades / embargoes / industry):
// a legacy game has none of them. The ABILITIES registry is the single
// source of truth for which fields the LLM abilities own — each is a
// list, so the default is `[]`, applied only-if-undefined so a saved
// value is never wiped. (Any pre-v0.33 `war` relation stays a
// bare/implicit relation — see syncWarRelations, which never
// force-creates a War entity.)
for (const a of ABILITIES) {
const fields = a.stateFields;
if (!fields) continue;
const row = state as unknown as Record<string, unknown>;
for (const f of fields) if (row[f] === undefined) row[f] = [];
}
// (v0.28, #46) Rows saved before per-region stats existed are migrated
// ONCE at load time (and never re-migrated afterwards — the field is
// then present): each owned region is seeded with a copy of its owner's