(Adaptive Loop) Pass 1

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).
This commit is contained in:
AlexKucera committed 2026-08-12 22:06:09 +02:00
1 parent 7d3ca6b236
commit f4b2b76653
31 files changed
+7113

No files matched your search

+211
View File
@@ -0,0 +1,211 @@
// Adaptive Loop — immutable declarative scenarios. SPEC §9.
// Sparse start notation order: nutrient / producer / grazer / phage / heat / toxin.
import type { LineageId, PressureId } from "../domain/types";
export type FixtureKind =
| "nutrientInlet"
| "heatSource"
| "toxinInlet"
| "drain"
| "immuneNode"
| "objectiveMarker";
export interface FixtureDef {
slot: number;
kind: FixtureKind;
/** For objectiveMarker: a label like "sanctuary" / "assay". */
marker?: string;
}
/** Start tuple in notation order: [nutrient, producer, grazer, phage, heat, toxin]. */
export type StartTuple = [number, number, number, number, number, number];
export interface SustainedSpec {
assaySlots: number[];
assayPopMin: number;
producerRange: [number, number];
grazerRange: [number, number];
phageMax: number;
toxinMax: number;
}
export type WinSpec =
| { kind: "producerAt"; slot: number; producerMin: number; totalProducerMin: number }
| {
kind: "endurance";
atTurn: number;
totalProducerMin: number;
totalGrazerMin: number;
producerChambersMin: number;
grazerChambersMin: number;
}
| { kind: "cleanse"; totalToxinMax: number; totalPopMin: number; fromTurn: number; toTurn: number }
| { kind: "escort"; slot: number; grazerMin: number; phageMin: number; totalProducerMin: number }
| { kind: "sustained"; required: number; sustained: SustainedSpec };
export interface ScenarioDef {
id: string;
index: number;
title: string;
pressure: PressureId;
subtitle: string;
teaches: string;
turnLimit: number;
fixtures: FixtureDef[];
start: Record<number, StartTuple>;
win: WinSpec;
winDescription: string;
}
export const SCENARIOS: ScenarioDef[] = [
{
id: "s1-heat",
index: 0,
title: "Cultivation Quota",
pressure: "heat",
subtitle: "Heat",
teaches: "rotation, ownership, flow, growth, preview, first visible pressure",
turnLimit: 8,
fixtures: [
{ slot: 1, kind: "nutrientInlet" },
{ slot: 7, kind: "nutrientInlet" },
{ slot: 3, kind: "heatSource" },
{ slot: 9, kind: "heatSource" },
],
start: {
0: [3, 2, 0, 0, 0, 0],
6: [3, 2, 0, 0, 0, 0],
10: [2, 1, 0, 0, 0, 0],
4: [1, 0, 0, 0, 0, 0],
},
win: { kind: "producerAt", slot: 11, producerMin: 5, totalProducerMin: 12 },
winDescription: "producer at slot 11 ≥ 5 and total producer ≥ 12",
},
{
id: "s2-hunger",
index: 1,
title: "Endurance",
pressure: "hunger",
subtitle: "Hunger",
teaches: "competition, migration, dormancy, deliberate denial of predator food",
turnLimit: 10,
fixtures: [{ slot: 0, kind: "nutrientInlet" }],
start: {
1: [2, 3, 0, 0, 0, 0],
5: [1, 2, 1, 0, 0, 0],
7: [2, 3, 1, 0, 0, 0],
10: [0, 2, 2, 0, 0, 0],
},
win: {
kind: "endurance",
atTurn: 10,
totalProducerMin: 8,
totalGrazerMin: 4,
producerChambersMin: 3,
grazerChambersMin: 2,
},
winDescription:
"at turn 10: total producer ≥ 8, total grazer ≥ 4, producer in ≥ 3 chambers, grazer in ≥ 2 chambers",
},
{
id: "s3-toxin",
index: 2,
title: "Cleansing",
pressure: "toxin",
subtitle: "Toxin",
teaches: "routing contaminants, resistance payment, shared detoxification spillover",
turnLimit: 9,
fixtures: [
{ slot: 1, kind: "nutrientInlet" },
{ slot: 3, kind: "toxinInlet" },
{ slot: 5, kind: "drain" },
{ slot: 11, kind: "drain" },
],
start: {
0: [3, 3, 0, 0, 0, 0],
3: [0, 0, 0, 0, 0, 2],
6: [2, 3, 0, 0, 0, 0],
9: [0, 0, 0, 0, 0, 2],
10: [2, 2, 1, 0, 0, 0],
},
win: { kind: "cleanse", totalToxinMax: 2, totalPopMin: 8, fromTurn: 6, toTurn: 9 },
winDescription: "turn 6–9: total toxin ≤ 2 and total producer + grazer ≥ 8",
},
{
id: "s4-immunity",
index: 3,
title: "Escort",
pressure: "immunity",
subtitle: "Immunity",
teaches: "immune targeting, cloak, retargeting spillover, predator vulnerability",
turnLimit: 8,
fixtures: [
{ slot: 0, kind: "nutrientInlet" },
{ slot: 8, kind: "nutrientInlet" },
{ slot: 2, kind: "immuneNode" },
{ slot: 4, kind: "immuneNode" },
{ slot: 6, kind: "immuneNode" },
{ slot: 11, kind: "objectiveMarker", marker: "sanctuary" },
],
start: {
1: [2, 3, 0, 0, 0, 0],
5: [2, 3, 2, 0, 0, 0],
7: [0, 0, 0, 1, 0, 0],
9: [1, 0, 2, 1, 0, 0],
},
win: { kind: "escort", slot: 11, grazerMin: 3, phageMin: 0, totalProducerMin: 6 },
winDescription: "grazer at slot 11 ≥ 3 and total producer ≥ 6",
},
{
id: "s5-convergence",
index: 4,
title: "Stable Equilibrium",
pressure: "immunity",
subtitle: "Convergence",
teaches: "all systems plus inherited gift-and-burden consequences",
turnLimit: 12,
fixtures: [
{ slot: 0, kind: "nutrientInlet" },
{ slot: 6, kind: "nutrientInlet" },
{ slot: 2, kind: "heatSource" },
{ slot: 8, kind: "heatSource" },
{ slot: 3, kind: "toxinInlet" },
{ slot: 9, kind: "drain" },
{ slot: 4, kind: "immuneNode" },
{ slot: 10, kind: "immuneNode" },
{ slot: 1, kind: "objectiveMarker", marker: "assay" },
{ slot: 7, kind: "objectiveMarker", marker: "assay" },
{ slot: 11, kind: "objectiveMarker", marker: "assay" },
],
start: {
0: [3, 3, 0, 0, 0, 0],
3: [0, 0, 0, 0, 0, 2],
5: [2, 2, 1, 0, 0, 0],
6: [3, 2, 0, 0, 0, 0],
8: [0, 0, 0, 0, 2, 0],
9: [0, 0, 0, 0, 0, 1],
10: [2, 2, 2, 1, 0, 0],
},
win: {
kind: "sustained",
required: 2,
sustained: {
assaySlots: [1, 7, 11],
assayPopMin: 2,
producerRange: [8, 14],
grazerRange: [3, 7],
phageMax: 3,
toxinMax: 6,
},
},
winDescription: "hold the equilibrium for 2 consecutive turns",
},
];
export const SCENARIO_BY_ID: Record<string, ScenarioDef> = Object.fromEntries(
SCENARIOS.map((s) => [s.id, s]),
);
export type InheritanceSet = Partial<Record<LineageId, PressureId[]>>;
export const STORAGE_KEY = "adaptive-loop-save-v1";