M9: a campaign you can actually get back

A campaign could already be exported and imported. What could not survive the
trip was everything that explains it: the state events behind the authoritative
document, the prompt each turn was actually given, the passages it was shown,
the summaries that carry long-story continuity, and which take belonged to which
turn. An imported campaign could be read and could no longer say why it was what
it was — and a manual correction, the one state change no narration explains,
was indistinguishable from something the story had established.

The bundle is now `ai-dnd-adventure-v3`, and the version is the design rather
than a side effect. Everything added here could have been another optional key,
the way persona, Save Points, narrative state and imported knowledge each were.
That mechanism stops working at exactly this addition: a v2 file with no prompt
provenance is ambiguous between "written before M9" and "written by M9 from a
campaign that has none", and those are different facts about a campaign. A
version number is how a recovery file states what it was capable of recording.
v1 and v2 still import, and every seam from pre-active-head onward is tested for
the rule that an older file is never reinterpreted under a newer assumption.

Two categories became three. "Chosen travels, derived is recomputed" was enough
until stored prompts had to be decided: they are derived, and they must travel
anyway. The test that separates evidence from cache is not "could this be
recomputed" but "would a recomputation answer the same question" — a rebuilt
search index answers the same question, a rebuilt prompt says what the turn
would be told *now*, which is the opposite of what the inspector is for.

Also here: a real SQLite backup, through the online backup API rather than a
file copy, taken while the application is running and verified before it is
kept; story cards settled as compatibility-only legacy data and taken out of the
narrator's prompt, because they were the untracked path around knowledge
authority that IMPORTED-KNOWLEDGE-DESIGN §73 already forbade; and no schema
change at all, proved against a database M8's own code wrote.

Three defects, found by running the milestone's own tests rather than by reading
them. Deleting a campaign leaked its FTS index rows, and SQLite then handed the
freed ids to the next source imported into any campaign, which failed with an
integrity error that Reindex could not repair — both ends are closed, and a
database already carrying the damage now repairs itself. An imported node with
no state snapshot was being stamped with the campaign's head state, so an Undo
to turn 2 showed what the story knew at turn 20. And the snapshot relink did not
persist at all, because it mutated a dict in place on a column SQLAlchemy tracks
by assignment: it looked correct in memory and wrote the wrong ids to disk.

Carrying per-turn prompts looked like it would halve the length of campaign that
can be restored. Measured — and after compressing them inside the file —
everything M9 added costs 12% of it: the import ceiling moves from about 318
turns to about 279, against a 100-turn certification target. The dominant cost
is not M9's at all. The per-position narrative state document is 74% of a
bundle, and v2 already carried it.

Backend 1,102 passed / 14 skipped / 0 failed. Frontend 145 passed. Lint,
production build and Docker build clean. Verified across two server processes
with two data directories, and in a real browser against a real narrator.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qyn3oRd4D6pi72nKBG725B
This commit is contained in:
JesseMarkowitz
2026-09-07 01:55:45 -04:00
co-authored by Claude Opus 5
parent 1ce9972760
commit 44edece67e
46 changed files with 9227 additions and 178 deletions
+43 -24
View File
@@ -29,7 +29,9 @@ from ..knowledge import records as knowledge_records
from . import encoding, history
AUTHORS_NOTE_DEPTH = 3 # actions from the end of history
CARD_BUDGET_SHARE = 0.4 # max share of non-reserved budget that story cards may take
# `CARD_BUDGET_SHARE = 0.4` was here, and is gone with the injection it bounded
# (M9). It is named rather than deleted silently because two other places
# reasoned about their own share against it.
NPC_WINDOW = 6 # actions of story searched for NPC trigger words ("in scene")
SEPARATOR = "\n\n"
@@ -508,30 +510,47 @@ def build_context(
adventure, available_after_knowledge, count_tokens, exclude_action_id
)
# ----- Story cards: triggered by recent story text (the window history could fill) -----
trigger_window = truncate_to_last_tokens(
SEPARATOR.join(a.text for a in actions), available_after_knowledge
)
triggered = match_cards(adventure.story_cards, trigger_window)
card_budget = int(available_after_knowledge * CARD_BUDGET_SHARE)
card_records = []
lore_lines: list[str] = []
# ----- Story cards: legacy, and no longer part of the narrator's prompt (M9)
#
# Until M9 a keyword-triggered story card was injected here as
# `World Lore: <entry>`, taking up to 40% of what was left after the
# imported knowledge had been placed.
#
# `IMPORTED-KNOWLEDGE-DESIGN.md` §73 settles that Story Cards are not the
# production imported-knowledge store and, in as many words, that they "must
# not become an alternate untracked path around the new knowledge
# authority/provenance rules". That is exactly what this was. A card entry
# arrived in front of the narrator as a world fact with:
#
# * no class — nothing said whether it was Canon, Reference or Inspiration,
# so nothing framed how far the narrator could rely on it;
# * no visibility — no narrator-only distinction at all;
# * no source, no hash, no lifecycle, nothing to disable it with;
# * no browser surface, since M8 removed the editor — so a reader could
# neither see it nor switch it off;
# * and no row in the context inspector, which renders `knowledge` and
# never rendered `cards`.
#
# It also competed with imported Canon for one budget, which is the
# arrangement M7 spent a milestone separating.
#
# M9's decision, recorded in the milestone report: story cards are
# **compatibility-only legacy data**. Nothing is deleted. The rows stay, the
# `/api/story-cards` endpoints stay, the bundle carries them out and back so
# a round trip destroys nothing, and `memorybank.cast_brief` still reads them
# as the summariser's character roster — a roster names who is on stage so a
# memory says "Aldric" rather than "he", it never reaches the narrator, and
# every memory written from it is authority-classified by the application
# afterwards. What stops is the one path that asserted campaign facts to the
# narrator without any of the controls §73 requires.
#
# `cards` stays in the report and is now always empty for a new turn.
# Removing the key would break the historical snapshots that have one, which
# M9 has just made portable: an old turn's evidence says story cards were
# included, and it must go on saying so.
card_records: list[dict] = []
lore_section = None
used = 0
for match in triggered:
line = f"World Lore: {match['entry'].strip()}"
tokens = count_tokens(line)
included = used + tokens <= card_budget
if included:
lore_lines.append(line)
used += tokens
card_records.append(
{"id": match["id"], "name": match["name"], "keyword": match["keyword"],
"included": included}
)
lore_section = (
Section("world_lore", "\n".join(lore_lines)) if lore_lines else None
)
# ----- Story history: newest first until the remaining budget is spent -----
history_budget = available_after_knowledge - used