Adaptive Loop — a deterministic microfluidic strategy puzzle. Pure serializable nextState(state,action) owns every committed turn; previews call the same function on a clone. 5 handcrafted scenarios, bounded solver with verified win/lose routes + shared-ownership counterfactual, exact localStorage persistence/replay, accessible SVG interface, Vitest truth/solver/persistence tests, and Playwright black-box browser proof. Scenarios 3 and 4 retuned per SPEC §9 (provably unwinnable as originally specified); proofs and exact changes in docs/scenario-tuning.md. Invariants preserved. See CONTEXT-MANIFEST.md for the disclosure boundary and residual risks. Aggregate verified: tsc --noEmit, vitest (59), vite build, playwright (10).
116 lines
5.0 KiB
TypeScript
116 lines
5.0 KiB
TypeScript
// Adaptive Loop — solver/replay proof (SPEC §15 Gate 4). Replays the verified
|
|
// win/lose routes stored in tests/fixtures/solver-results.json (generated by the
|
|
// bounded solver) and asserts the exploit/avoid/counterfactual evidence.
|
|
import { describe, it, expect } from "vitest";
|
|
import { nextState } from "../src/domain/pipeline";
|
|
import { createScenarioState } from "../src/domain/scenario";
|
|
import { SCENARIOS, SCENARIO_BY_ID } from "../src/content/scenarios";
|
|
import type { RotationAction, LineageId, PressureId } from "../src/domain/types";
|
|
import results from "./fixtures/solver-results.json";
|
|
|
|
interface RouteEntry {
|
|
win: { loop: "A" | "B" | "C"; direction: "cw" | "ccw" }[];
|
|
lose: { loop: "A" | "B" | "C"; direction: "cw" | "ccw" }[];
|
|
winTurns: number;
|
|
loseTurns: number;
|
|
loseOutcome: "collapsed" | "expired";
|
|
winVerified: boolean;
|
|
loseVerified: boolean;
|
|
pressure: PressureId;
|
|
ownPressureUnlocked: boolean;
|
|
usesMultipleLoops: boolean;
|
|
}
|
|
const R = results as unknown as {
|
|
scenarios: Record<string, RouteEntry>;
|
|
adaptationEvidence: { exploit: { scenario: string; pressure: PressureId } | null; avoid: { scenario: string; pressure: PressureId } | null };
|
|
counterfactual: { scenario: string; solvableNormal: boolean; frozenBeamFoundWin: boolean; frozenExhaustiveFoundWin: boolean; conclusion: string };
|
|
};
|
|
|
|
function replay(scenarioId: string, route: { loop: "A" | "B" | "C"; direction: "cw" | "ccw" }[], inheritance: Partial<Record<LineageId, PressureId[]>> = {}) {
|
|
let s = createScenarioState(scenarioId, "standalone", inheritance);
|
|
for (const a of route) {
|
|
s = nextState(s, a as RotationAction).state;
|
|
if (s.outcome !== "playing") break;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
describe("Gate 4 — solver routes (replayed from fixtures)", () => {
|
|
for (const sc of SCENARIOS) {
|
|
const entry = R.scenarios[sc.id]!;
|
|
describe(sc.id, () => {
|
|
it("has a verified winning route", () => {
|
|
expect(entry, `fixture for ${sc.id}`).toBeDefined();
|
|
expect(entry.win.length).toBeGreaterThan(0);
|
|
expect(entry.win.length).toBeLessThanOrEqual(sc.turnLimit);
|
|
const final = replay(sc.id, entry.win);
|
|
expect(final.outcome).toBe("won");
|
|
});
|
|
it("has a verified losing route", () => {
|
|
expect(entry.lose.length).toBeGreaterThan(0);
|
|
const final = replay(sc.id, entry.lose);
|
|
expect(["collapsed", "expired"]).toContain(final.outcome);
|
|
});
|
|
it("uses rotations of multiple loops (ownership-relevant)", () => {
|
|
expect(entry.usesMultipleLoops).toBe(true);
|
|
});
|
|
});
|
|
}
|
|
|
|
it("at least one route exploits an adaptation (unlocks its scenario's pressure trait)", () => {
|
|
expect(R.adaptationEvidence.exploit, "exploit route").not.toBeNull();
|
|
const ex = R.adaptationEvidence.exploit!;
|
|
const entry = R.scenarios[ex.scenario]!;
|
|
expect(entry.ownPressureUnlocked).toBe(true);
|
|
// replay and confirm the trait actually unlocked during the winning route
|
|
let s = createScenarioState(ex.scenario, "standalone", {});
|
|
let unlocked = false;
|
|
for (const a of entry.win) {
|
|
s = nextState(s, a as RotationAction).state;
|
|
if (s.lineage.producer.unlocked[ex.pressure] || s.lineage.grazer.unlocked[ex.pressure]) unlocked = true;
|
|
if (s.outcome !== "playing") break;
|
|
}
|
|
expect(unlocked).toBe(true);
|
|
});
|
|
|
|
it("at least one route avoids an adaptation (its scenario's pressure trait stays locked)", () => {
|
|
expect(R.adaptationEvidence.avoid, "avoid route").not.toBeNull();
|
|
const av = R.adaptationEvidence.avoid!;
|
|
const entry = R.scenarios[av.scenario]!;
|
|
expect(entry.ownPressureUnlocked).toBe(false);
|
|
const final = replay(av.scenario, entry.win);
|
|
expect(final.outcome).toBe("won");
|
|
expect(final.lineage.producer.unlocked[av.pressure]).toBe(false);
|
|
expect(final.lineage.grazer.unlocked[av.pressure]).toBe(false);
|
|
});
|
|
|
|
it("counterfactual: ownership materially changes solvability", () => {
|
|
const cf = R.counterfactual;
|
|
expect(cf.solvableNormal, "normal game must be winnable").toBe(true);
|
|
const normalFinal = replay(cf.scenario, R.scenarios[cf.scenario]!.win);
|
|
expect(normalFinal.outcome).toBe("won");
|
|
// No winning route exists under frozen ownership (bounded-search evidence recorded).
|
|
expect(cf.frozenBeamFoundWin).toBe(false);
|
|
expect(cf.frozenExhaustiveFoundWin).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("scenario definitions are well-formed", () => {
|
|
it("all five scenarios present and unique", () => {
|
|
expect(SCENARIOS.length).toBe(5);
|
|
expect(new Set(SCENARIOS.map((s) => s.id)).size).toBe(5);
|
|
});
|
|
for (const sc of SCENARIOS) {
|
|
it(`${sc.id} fixtures reference valid slots`, () => {
|
|
for (const f of sc.fixtures) expect(f.slot).toBeGreaterThanOrEqual(0);
|
|
for (const slotStr of Object.keys(sc.start)) {
|
|
expect(Number(slotStr)).toBeGreaterThanOrEqual(0);
|
|
expect(Number(slotStr)).toBeLessThan(12);
|
|
}
|
|
});
|
|
}
|
|
it("scenario lookup works for all ids", () => {
|
|
for (const sc of SCENARIOS) expect(SCENARIO_BY_ID[sc.id]).toBeDefined();
|
|
});
|
|
});
|