# Developer reference: contracts, deterministic execution, and operations

This guide describes the implementation in this repository, not a supported external SDK. LifePot's supported interface is the web game. `/api/judge` is an internal application endpoint; its request shapes are implementation contracts, not a public service promise.

Jev's adaptive decisions and reproducible execution are separate responsibilities. Runtime decisions can select supported environmental pressures and scheduled rule changes, plus species-level strategies, mutation targets and mutation tempos. `birth` in `src/game/world.ts` applies strategy and mutation directives to offspring; the model has no direct cell-edit or individual-action interface. A fresh adaptive run obtains new decisions at bounded triggers, while replay executes the recorded ledger without inference. Fixed-rule worlds do not request runtime decisions, and runtime fallback abstains from new intervention.

## Quick local verification

With Node/npm installed, run the real project checks:

```sh
npm ci
npm test
npm run typecheck
npm run lint
```

A deterministic setup example can run without inference or credentials. From the repository root, use `npx tsx` only if your environment already provides it; it is not a project dependency or promised interface. The project tests exercise the public-to-the-app TypeScript functions directly and are the copyable, dependency-safe contract examples:

```sh
npm test -- src/game/setup.test.ts src/game/rules.test.ts src/game/replay.test.ts
```

For deployment build requirements, see [Deployment](/learn/deployment). Do not send arbitrary requests to production `/api/judge`: it is an internal, quota-protected application route.

## Setup schemas and normalization

Source of truth: `src/game/setup.ts`, `src/game/world.ts`.

`setupAnswersSchema` is a strict object with exactly `world`, `threat`, and `reward`; each is a trimmed, nonempty string of at most 140 characters. Unknown keys fail. `canonicalAnswers` trims and revalidates. `hashSetupRequest` hashes the canonical ordered answer tuple and prefixes it with `setup_`.

`LifeConfig` is strict at each schema level:

| Field | Accepted values / constraint |
| --- | --- |
| `environment.abundance` | `scarce`, `balanced`, `rich` |
| `environment.distribution` | `clustered`, `scattered`, `seasonal` |
| `environment.hazard` | `drought`, `toxin`, `heat`, `crowding` |
| `environment.volatility` | `stable`, `pulsing`, `chaotic` |
| `fitness` | exactly `survive`, `replicate`, `cooperate`, `explore`, `adapt`; each 0–1 and total within 1e-9 of 1 |
| `founders.balance` | `prey_heavy`, `balanced`, `predator_heavy` |
| `founders.diversity` | `focused`, `varied` |
| `founders.preyStrategy` | `efficient_grazing`, `early_brood`, `armored`, `swarming`, `dispersal` |
| `founders.predatorStrategy` | `ambush`, `pursuit`, `pack_hunting`, `efficient_kill`, `brood_hunting` |
| `rules` | optional `WorldRuleGraph`; if omitted the default graph is used by the engine |

`normalizeFitness` clamps negative/non-finite inputs to zero and normalizes; an all-zero input becomes equal weights. It is not a substitute for validating external config. `defaultConfig()` uses balanced/scattered/drought/stable, equal weights, balanced/varied founders, efficient grazing/pursuit, and `defaultRuleGraph()`.

Setup prose maps by fixed keyword rules in `deterministicSetup`; it does not generate executable logic. The route may request typed setup interpretation from Jev, validates the response, and returns deterministic setup as an explicitly labeled fallback when model access or validation fails.

### Fidelity correction and review

Live setup first establishes a model-authored organism-to-slot contract from finite spans of the original world and threat, then selects roles and directed relationships for those named organisms. The initial environment/fitness call no longer independently selects another species count, role set or feeding graph. A duplicate identity can receive one corrective mapping call: valid earlier assignments remain fixed, and the remaining palette excludes already assigned or overlapping earlier mentions. A still-inconsistent correction is rejected. This is mechanical source grounding and bounded model interpretation, not keyword-based semantic acceptance.

The browser shows the named intent, concrete contract conflicts and finite failure-stage/code metadata. A corrected graph must retain the established species, roles and directed links even if a fresh fidelity reviewer says `approve`. The original answers are preserved, and diagnostics contain no raw provider payload or credentials. Named-intent and failure metadata remain outside the deterministic replay contract. Setup costs six calls directly and at most thirteen with identity/food-web correction and the council protocol correction; all calls are reserved and their successful response usage is accounted for.

Council selection preserves the returned Choice protocol: the selected label must have maximal probability. A reproduced nonmaximal selection with otherwise valid data receives one budgeted corrective request; a second inconsistent response, malformed distribution, invalid metadata or denied reservation remains a failure. The original probability data are never changed or renormalized, and successful correction accounts for both requests. This does not add runtime-council retries or alter simulation/replay validation.

The fidelity request includes the complete interpreted configuration and a readable, engine-derived food web alongside the original answers and encoded graph. Pair directions are decoded using each pair's own endpoints; review checks supported food-web intent, not biological realism or guaranteed success of the player's objective. A `reselect` or `reject` fidelity verdict triggers at most one correction; the corrected proposal must independently receive `approve`, and persistent rejection stays blocked. Roles/count are selected first; the dependent pair request includes the fixed species/roles. The complete corrected graph is validated and reviewed against the unchanged original answers. An approved correction receives a council selected for that graph. Every provider call reserves the existing call budget, and successful response usage is accumulated. A persistent nonapproval gets a finite, model-selected review focus; that focus is not independent proof of a semantic mismatch. Failure inside correction preserves the nonapproval instead of silently returning a seedable fallback. Additional review metadata stays outside deterministic engine/replay contracts.

Run `LIFEPOT_LIVE_SETUP_TEST=1 npm test -- src/app/api/judge/setup-live.test.ts` with a server-side `TYPESAFE_API_KEY` already configured to check real Jev acceptance of ordinary inputs. This explicitly opted-in test makes paid inference requests; without both settings it is skipped. Mocked approval tests prove the correction/control flow, not model fidelity. For an approved Cloudflare preview that already holds the server-side key, run `PLAYWRIGHT_BASE_URL=http://localhost:9239 LIFEPOT_LIVE_GAMEPLAY_BASE_URL=http://localhost:9239 npm run test:e2e -- e2e/live-jev-gameplay.spec.ts --workers=1 --retries=0`. This separate opt-in browser test exercises ordinary setup, adaptive play, reflection, a mobile follow-up and inference-free replay; it is skipped during normal fixture runs. Do not point it at production by default.

## Rule graph reference

Source: `src/game/rules.ts`; tests: `src/game/rules.test.ts`.

A graph has integer `version >= 1`, 2–4 species in canonical prefix order (`A`, `B`, `C`, `D`), one interaction for every canonical species pair exactly once and in order, an environment object, and optional validated council metadata. Species roles: `producer`, `grazer`, `hunter`, `scavenger`, `omnivore`. Self-interactions: `cooperative`, `territorial`, `cannibalistic`, `neutral`. Pair modes: `a_consumes_b`, `b_consumes_a`, `competition`, `mutualism`, `avoidance`, `neutral`.

The graph must have a viable basal-feeding role (`producer`, `grazer`, or `omnivore`). Roles alone do not authorize predation: directional pair edges control inter-species consumption. A strict invalid shape throws `Invalid ecosystem rule graph`; canonical-order, complete-pair, and basal-path invariant violations throw descriptive errors. Validation returns a cloned graph.

Environment vocabularies are regeneration (`steady`, `pulsed`, `depletion_feedback`), pressure (`stability`, `drought`, `toxin_wave`, `heat_wave`, `fragmentation`, `nutrient_bloom`), volatility (`stable`, `pulsing`, `chaotic`), intensity (`low`, `medium`, `high`), and duration (`short`, `medium`, `long`, `persistent`). Scheduled activations are `immediate`, `after_6`, `after_12`, `after_24`, `resources_low`, `population_boom`, `population_crash`, `species_emergence`, `species_extinction`, `predation_high`; transitions are `abrupt`, `ramp`, `pulse`.

A `RulePatch` is exactly one of: `{kind:"pair", pair, mode}`, `{kind:"self", species, value}`, or `{kind:"environment", field, value}`. It is strict; unknown keys/enum values reject. Pair and species targets must exist. A changed patch increments graph version once; an unchanged patch does not. Scheduled changes bind a generation 0–180, activation, duration, transition, and patch. They only take effect when their trigger is due and the accepted decision's graph-version evidence is valid. Temporary changes revert according to duration; persistent changes do not automatically revert.

### Validation example (actual functions)

Run the focused tests above to execute these cases using the repository's installed Vitest. This valid default graph is the actual `defaultRuleGraph()` contract; the second input is deliberately invalid because it adds an undeclared graph property and strict validation rejects it.

```ts
import { defaultRuleGraph, validateRuleGraph } from "./src/game/rules";

const graph = validateRuleGraph(defaultRuleGraph());
console.log(graph.species.length, graph.interactions[0].mode); // 2, "b_consumes_a"
try {
  validateRuleGraph({ ...graph, surprise: true });
} catch (error) {
  console.log((error as Error).message); // "Invalid ecosystem rule graph"
}
```

This snippet assumes the project TypeScript/Vitest environment and repository-root import path. It is a source-level example, not a standalone JavaScript script.

## Engine ownership and deterministic loop

Source: `src/game/world.ts`, `src/game/decisions.ts`, `src/game/runtime.ts`.

`createSimulation({seed, config, initialPopulation?, initialResources?})` clones and validates the rule graph. The default world is a 50×50 torus (2,500 cells), uses an unsigned 32-bit seed, has five byte-range traits per organism, energy capped at 255, and runs at most 180 generations. Initial resources and organisms can be injected for deterministic tests. `ENGINE_VERSION` is currently `lifepot-ca-8`; recorded ca7 runs select the explicit legacy engine.

The simulation's seeded PRNG drives resource initialization, placement, movement and reproduction choices, and mutation. Each generation calculates resource growth/hazards and organism actions from the previous state, then derives new aggregates/history and outcome. Organisms feed on basal resources only when their rule role allows it. Predation requires a directional graph relation (or an allowed cannibalistic self-relation); population counts do not create food-web edges. Reproduction transfers 38% of basal-parent energy (34% for hunter parents) to the child, rather than creating energy. In `lifepot-ca-8`, a selected mutation target chooses its corresponding heritable trait; without a target, selection remains seeded-random. Mutation is bounded, may fail at trait bounds, and may change variant identity; strategy and traits remain finite. Extinction-conditioned policies require a configured species that was actually populated and then disappeared; never-populated slots do not trigger them. New runs use `lifepot-ca-8`. Recorded `lifepot-ca-7` runs retain their original mechanics, mutation selection and activation semantics, including through replay restart and sharing. Applying old proposals under ca8 is a separately labeled counterfactual, not an exact replay.

The engine owns movement, feeding, resource growth, hazards, reproduction, mutation, death, scheduled rule activation, and outcome. These are deliberately toy-model mechanisms, not calibrated biological quantities. Basal feeders and consumers pay positive minimum upkeep after bounded social cost reductions; cooperation/mutualism reduces expenditure, never creates energy. Predation yields a fraction of the captured prey's available energy: efficient_kill 0.80, ordinary hunting 0.69, and pack_hunting 0.61, so strategy effects remain distinct while feeding-event energy equals the credited gain and cannot exceed prey energy. Competition remains non-feeding pressure. `fitness` influences mechanics; it does not specify a guaranteed result. The final `extinct` outcome is zero population; after the generation limit, a surviving world is `thriving` only when prey count is at least 50 and predators remain, otherwise `surviving`.

## Bounded decisions, frozen observations, and ledger

Decision schemas and allowed triggers/strategies live in `src/game/decisions.ts`. `src/game/runtime.ts` advances deterministically until a trigger, freezes and hashes the observation, and binds the accepted decision to that exact observation, generation, trigger and graph version. A decision must cover the declared probability choices, have valid probabilities and select a maximum-probability option. Species directives are checked against configured species. The model cannot send state mutations, arbitrary code, or undeclared enum values.

A trigger cannot occur before generation 12, within 12 generations of the previous accepted decision, or after eight ledger entries. Accepted entries remain ordered. Missing/invalid model output takes the labeled deterministic fallback path; no invalid proposal is treated as an accepted model decision. Cooldown, trigger, observation, generation, and graph-version checks are enforced in the runtime, not trusted from prose.

## Replay and tamper/version compatibility

Source: `src/game/replay.ts`; tests: `src/game/replay.test.ts`.

Replay v3 is a strict object containing `version: 3`, current `engineVersion`, canonical answers, validated config, integer seed 0–4,294,967,295, `setup_…` request hash, ledger of at most eight entries, and a `replay_…` contract hash. `createReplay` canonicalizes and validates; `encodeReplay` serializes validated replay data; `decodeReplay` validates the decoded object; `playReplay` runs the recorded ledger through the deterministic engine and returns replay/state/snapshot without inference. The optional interpreter parameter is intentionally unused.

Any malformed, tampered, unsupported, or semantically unreachable input is rejected (`Invalid, tampered, or unsupported LifePot replay v3` or `Invalid or unsupported LifePot replay`). The contract hash is a compact FNV-1a integrity checksum, not a cryptographic signature or proof against a determined attacker. A future engine behavior change requires an engine version change and compatibility review; do not claim cross-version support unless explicitly implemented and tested.

## HTTP and deployment boundaries

The `/api/judge` route is server-only for interpretation, not a public API. It checks strict payload schemas, limits request bodies to 16 KiB, applies `Cache-Control: no-store`, and uses the Cloudflare `JUDGE_RATE_LIMIT` binding for production call protection. Binding denial, absence, or errors fail closed to deterministic fallback rather than an unmetered model request. The Workers limiter is per client key and Cloudflare location, eventually consistent—not a global billing quota. Provider-side limits remain the spend control.

`TYPESAFE_API_KEY` is optional, server-only, and must never be exposed under `NEXT_PUBLIC_` or bundled to the browser. Do not add provider keys to docs/examples. Local development and tests inject deterministic in-process limiting; that local behavior is not production protection. See [deployment guide](/learn/deployment) and [architecture](/learn/architecture).

## Source map

- Setup vocabulary and hashes: `src/game/setup.ts`, `src/game/setup.test.ts`
- Rule schemas and patches: `src/game/rules.ts`, `src/game/rules.test.ts`
- Mechanics and world state: `src/game/world.ts`, `src/game/mechanics.test.ts`, `src/game/world.test.ts`
- Decisions, triggers and ledger: `src/game/decisions.ts`, `src/game/runtime.ts`, `src/game/decision-continuity.test.ts`
- Replay: `src/game/replay.ts`, `src/game/replay.test.ts`
- Server schemas, fallback, quota: `src/app/api/judge/`
- Interface: `src/components/GameCanvas.tsx`, `WorldObservatory.tsx`, `CreatureInspector.tsx`
- [Player guide](/learn/player-guide) · [Architecture](/learn/architecture) · [Simulation contract](/learn/simulation-contract) · [Deployment](/learn/deployment)
