# Make stories remember

Lesson 10 · Ship

Build deterministic narrative state first, then add optional runtime dialogue behind safe boundaries.

## Outcomes

- Represent story facts as explicit versioned game state.
- Generate dialogue from a bounded context without changing canonical rules.
- Preserve a complete offline experience when runtime AI is unavailable.

Prerequisites: Lesson 9 and an approved creative direction guide.

Deliverable: A narrative-state ledger with three deterministic reactions and one optional dialogue extension.


## Story memory begins with game state

A story remembers when facts persist and later reactions use them consistently. You do not need a model for that. Signal Garden already has meaningful facts: which relays are repaired, which route the player took, whether they returned early, and how many cells remain.

Store these as explicit values. Dialogue reads the ledger; it does not become the ledger. This boundary prevents a generated sentence from silently reviving a repaired tower or spending an item.

## Design the ledger

Pseudocode:

```text
NarrativeState v1 {
  repairedRelayIds: set of known relay IDs
  firstRelayId: known relay ID or null
  returnedEarlyCount: non-negative integer
  visitedRegions: set of known region IDs
  lastEventId: known event ID
}
```

Every update comes from a verified game event. After repair succeeds, gameplay code emits `relay_repaired:moss`; the state reducer adds `moss`; the UI and narrative layer react. Save stable IDs and values rather than engine objects. The [Godot saving guide](https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html) demonstrates the broader need to choose and serialize persistent state deliberately.

Version the ledger and test migration with an older fixture. If a field is renamed, translate it once on load and write the new format on the next save. Unknown IDs should produce a clear recovery path, not be silently discarded.

## Write deterministic reactions first

Create three authored lines for important states:

- first repair: “One clear tone answers from the valley.”
- early return: “The beacon is listening, but the network is incomplete.”
- all repaired: “Three signals hold. The path home is bright.”

These are the offline fallback and the reference behavior. They guarantee the player receives feedback even when no network or model is available. They also give an optional generator a tone and factual baseline.

Write reactions for absence as carefully as presence. Returning early is not an error, and the line should guide without pretending the player repaired something they did not.

## Add a bounded runtime extension

If you choose runtime dialogue, call it only after a game event has already resolved. Supply a small context packet: current ledger, one tone excerpt, allowed length, forbidden claims, and the fallback line. Require typed output such as `{ text, referencedFactIds }`, then validate that every referenced fact is present.

The [Structured Outputs documentation](https://developers.openai.com/api/docs/guides/structured-outputs) can help constrain shape, but semantic checking remains yours. A well-formed line can still contradict the ledger. On timeout, refusal, invalid fact, budget exhaustion, or offline play, show the authored fallback immediately.

## Failure modes and tradeoffs

**Transcript memory** treats previous prose as truth and accumulates contradictions. **Model-owned state** lets dialogue determine progress. **Context dumping** sends the entire design history for a ten-word reaction. **Fallback shame** makes the offline line noticeably worse, pressuring the player to remain connected.

Runtime variation can make a small world feel responsive, but it adds latency, moderation, privacy, cost, and test complexity. If those do not serve the experience, ship the deterministic lines. Optional means removable without breaking the loop.

## Build task: prove both paths

Implement or simulate the state reducer and three deterministic reactions. Save, close, reload, and verify the ledger. Then mock one runtime response using the exact contract and validate its fact references. Finally force a timeout and confirm the fallback appears without delaying progress.

```text
Write one optional Signal Garden reaction, maximum 18 words.
Use only facts in NARRATIVE STATE and the supplied tone rules.
Do not change game state, promise future mechanics, name unknown locations,
or address the player as a chosen hero. Return text plus referencedFactIds.
If no grounded reaction is possible, return the supplied fallback unchanged.
```

## Completion evidence

The same save must restore the same repaired relays and authored reaction offline. The optional line must reference only verified fact IDs, stay within length and tone constraints, and fail closed to the fallback. [Context engineering guidance](https://www.anthropic.com/research/effective-context-engineering-for-ai-agents) supports the small-packet approach: provide the facts needed for this reaction, not every fact the project has ever stored.



## Knowledge check

Which value should runtime dialogue be allowed to change directly?

1. Whether a relay is mechanically repaired.
2. The number of power cells in inventory.
3. A non-canonical line shown after an already verified repair event.

Answer: 3. Runtime dialogue may express authoritative state, but core progress remains under deterministic game logic.

## Further reading

- [Effective context engineering for AI agents](https://www.anthropic.com/research/effective-context-engineering-for-ai-agents) — Anthropic
- [Structured model outputs](https://developers.openai.com/api/docs/guides/structured-outputs) — OpenAI
- [Saving games](https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html) — Godot documentation

Sources checked 1 December 2026. Signal Garden is an original teaching scenario.
