Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62a997f364 | ||
|
|
279a871a77 | ||
|
|
e08d49c3eb | ||
|
|
3c8e91f644 | ||
|
|
d27ee34901 | ||
|
|
c8755c21c2 | ||
|
|
7f082b61d8 | ||
|
|
903fa7a74f |
@@ -91,15 +91,16 @@ decision that this project's threat model does not cover
|
|||||||
|
|
||||||
## Pointing the storyteller at Ollama
|
## Pointing the storyteller at Ollama
|
||||||
|
|
||||||
The endpoint, model and (unused) API key are **runtime settings stored in the
|
The endpoint, the model and the generation parameters are **runtime settings
|
||||||
database**, not environment variables. Set them on the app's Settings page, or
|
stored in the database**, not environment variables. There is no API key field:
|
||||||
with one request:
|
M2 removed it along with the cloud providers, and Ollama does not use one. Set
|
||||||
|
them on the app's Settings page, or with one request:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -X PUT http://127.0.0.1:8000/api/settings \
|
curl -X PUT http://127.0.0.1:8000/api/settings \
|
||||||
-H 'Content-Type: application/json' \
|
-H 'Content-Type: application/json' \
|
||||||
-d '{"endpoint_url":"http://127.0.0.1:11434/v1","model":"qwen2.5:3b-instruct",
|
-d '{"endpoint_url":"http://127.0.0.1:11434/v1","model":"qwen2.5:3b-instruct",
|
||||||
"api_mode":"chat","api_key":"","max_output_tokens":200,
|
"api_mode":"chat","max_output_tokens":200,
|
||||||
"context_token_budget":4096}'
|
"context_token_budget":4096}'
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -208,7 +209,7 @@ visible from within.
|
|||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd backend && .venv/bin/python -m pytest tests/ -q # 604 tests
|
cd backend && .venv/bin/python -m pytest tests/ -q # 698 tests
|
||||||
cd frontend && npm run lint && npm run build
|
cd frontend && npm run lint && npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -224,6 +225,17 @@ suite as complete evidence.
|
|||||||
is lost from the union, or if a new HTTP client is added without the shared
|
is lost from the union, or if a new HTTP client is added without the shared
|
||||||
verification context.
|
verification context.
|
||||||
|
|
||||||
|
M4 added `test_save_points.py`, which fails if restoring a Save Point starts
|
||||||
|
deleting history, stops going through the active head, forks on its own, lets a
|
||||||
|
Save Point on one campaign be restored through another, or lets deleting a branch
|
||||||
|
take a Save Point with it. It also fails if listing Save Points goes back to one
|
||||||
|
query per Save Point, or starts fetching narration to render the list.
|
||||||
|
|
||||||
|
`test_process_restart.py` is the durability guard: it starts the application as a
|
||||||
|
real subprocess, kills it, and starts a second one against the same database. A
|
||||||
|
Save Point that survived only because a Python object was still alive would pass
|
||||||
|
an in-process test and fail a user's restart.
|
||||||
|
|
||||||
M2 added two more. `test_endpoint_policy.py` fails if the set of reachable
|
M2 added two more. `test_endpoint_policy.py` fails if the set of reachable
|
||||||
addresses widens, or if either place the rule is applied stops applying it —
|
addresses widens, or if either place the rule is applied stops applying it —
|
||||||
it resolves hostnames through a stub, so it tests the policy rather than
|
it resolves hostnames through a stub, so it tests the policy rather than
|
||||||
@@ -266,21 +278,24 @@ docker exec app python -c "import socket; socket.create_connection(('1.1.1.1',44
|
|||||||
# -> OSError: Network is unreachable, and story turns still work
|
# -> OSError: Network is unreachable, and story turns still work
|
||||||
```
|
```
|
||||||
|
|
||||||
`planning/reports/M1-BASELINE-REPORT.md` records the run this procedure is
|
`planning/archive/milestone-reports/M1-BASELINE-REPORT.md` records the run this procedure is
|
||||||
taken from, including the packet captures.
|
taken from, including the packet captures.
|
||||||
|
|
||||||
## Things inherited from upstream that M1 deliberately did not touch
|
## Things still inherited from upstream
|
||||||
|
|
||||||
These are M2's scope (`planning/BUILD-MILESTONES.md`), listed here so nobody
|
M2 removed the hosted, cloud, account, analytics, Postgres/Render and QuickJS
|
||||||
reports them as new:
|
scripting surfaces outright — `PROVENANCE.md` lists exactly what went. What is
|
||||||
|
left of upstream that a newcomer might report as a defect:
|
||||||
|
|
||||||
- hosted/multi-user/account/demo-key code, analytics tables, Postgres and
|
- **Inert legacy tables and columns.** Five tables and four columns M2 emptied
|
||||||
Render deployment paths, and the OpenRouter default endpoint constant all
|
of meaning are still in the schema, unmapped, so an M1-era campaign database
|
||||||
still exist in the tree. None of them is reachable from a default local run,
|
opens unchanged. Nothing reads or writes them. A cleanup migration waits for
|
||||||
and none requires a cloud service.
|
the schema to settle after M5 (`planning/BUILD-MILESTONES.md`).
|
||||||
- `docs/*.html` is upstream's GitHub Pages project site and still links Google
|
- **Dual-dialect migration code.** `backend/app/migrations.py` still carries
|
||||||
Fonts. It is not served by the application and is not part of any build.
|
SQLite/Postgres branches from upstream, although Postgres support itself is
|
||||||
- `.github/workflows/ci.yml` is upstream's GitHub Actions pipeline. This
|
gone and SQLite is the only store. Same cleanup, same milestone.
|
||||||
|
- **`.github/workflows/ci.yml`** is upstream's GitHub Actions pipeline. This
|
||||||
repository lives on a self-hosted Gitea; the workflow is kept for provenance
|
repository lives on a self-hosted Gitea; the workflow is kept for provenance
|
||||||
and is not what runs the tests here.
|
and is not what runs the tests here.
|
||||||
- QuickJS campaign scripting is still present and still tested.
|
- **No frontend tests.** `npm run lint && npm run build` is the whole frontend
|
||||||
|
check. A test runner is M8's job.
|
||||||
|
|||||||
@@ -26,9 +26,11 @@ import is a merge of the pinned commit with `--allow-unrelated-histories`, so:
|
|||||||
|
|
||||||
- `git log d72f7c1bda0f34fccd84afb7a25c34eb01c901de` shows the real upstream
|
- `git log d72f7c1bda0f34fccd84afb7a25c34eb01c901de` shows the real upstream
|
||||||
history, not a squashed snapshot;
|
history, not a squashed snapshot;
|
||||||
- upstream paths are unchanged (`backend/`, `frontend/`, `docs/`, …), so a
|
- upstream code paths are unchanged (`backend/`, `frontend/`, …), so a later
|
||||||
later upstream commit can still be fetched and cherry-picked against
|
upstream commit can still be fetched and cherry-picked against matching
|
||||||
matching files;
|
files. Upstream's own documentation trees, `plan/` and `docs/`, were removed
|
||||||
|
on 2026-09-03: they described the hosted, scripted, multi-user product this
|
||||||
|
fork is not. They remain in this repository's history and in upstream;
|
||||||
- the planning package that predates the fork keeps its own history on the
|
- the planning package that predates the fork keeps its own history on the
|
||||||
other parent of the merge.
|
other parent of the merge.
|
||||||
|
|
||||||
@@ -104,7 +106,7 @@ unchanged. They are not product functionality and nothing reads or writes them.
|
|||||||
## What this fork changed in Milestone M1
|
## What this fork changed in Milestone M1
|
||||||
|
|
||||||
Nothing was removed from upstream. The changes are the offline/locality
|
Nothing was removed from upstream. The changes are the offline/locality
|
||||||
hardening M1 called for; see `planning/reports/M1-BASELINE-REPORT.md` for the
|
hardening M1 called for; see `planning/archive/milestone-reports/M1-BASELINE-REPORT.md` for the
|
||||||
evidence.
|
evidence.
|
||||||
|
|
||||||
- `backend/app/context/encoding.py` (new) and `backend/app/context/builder.py` —
|
- `backend/app/context/encoding.py` (new) and `backend/app/context/builder.py` —
|
||||||
|
|||||||
@@ -1,39 +1,38 @@
|
|||||||
# AI D&D
|
# Adventure Storyteller
|
||||||
|
|
||||||
[](https://github.com/parththakkar106/AI-DnD/actions/workflows/ci.yml)
|
|
||||||
[](LICENSE)
|
[](LICENSE)
|
||||||
|
|
||||||
An AI Dungeon-style interactive storytelling app that runs entirely on your own machine, with
|
An interactive storytelling app that runs entirely on your own machine, with your own model.
|
||||||
your own AI model. Create scenarios, play open-ended adventures where an LLM narrates the
|
Create scenarios and play open-ended adventures where a local LLM narrates the world, keeps
|
||||||
world, and extend the engine with **JavaScript scripts compatible with real AI Dungeon
|
track of what is true, and remembers what happened.
|
||||||
scripting**.
|
|
||||||
|
|
||||||
> ### ▶️ Try it live: **[parththakkar106.github.io/AI-DnD](https://parththakkar106.github.io/AI-DnD/)**
|
This is the **Adventure Storyteller** fork of [AI-DnD](https://github.com/parththakkar106/AI-DnD).
|
||||||
> The project page loads instantly and launches the hosted demo in one tap. Play a scenario as
|
It is deliberately narrower than its upstream: single-user, local-only, and pointed at a model
|
||||||
> a guest: no sign-up and no API key needed. The demo runs on a free tier that sleeps, so the
|
you run yourself. The hosted deployment, the accounts and sessions, the cloud provider support,
|
||||||
> first load after it's been idle takes about 30 to 60 seconds to wake up.
|
the Postgres path, and the JavaScript scripting engine have all been removed rather than
|
||||||
|
disabled. What is left is a storyteller you can run offline.
|
||||||
|
|
||||||
|
> **Local-only, by design.** The app talks to one place — an Ollama-compatible endpoint on this
|
||||||
|
> machine or on a machine you control on your own network — and it refuses to be pointed at a
|
||||||
|
> public address. There is no telemetry, no account, no cloud inference, and nothing is fetched
|
||||||
|
> at runtime from the Internet.
|
||||||
>
|
>
|
||||||
> For the internals, read the **[design notes](https://parththakkar106.github.io/AI-DnD/guide.html)**.
|
> For the internals, read [`planning/TECHNICAL-DESIGN.md`](planning/TECHNICAL-DESIGN.md) and
|
||||||
> They walk through the context budgeting, the world-state referee, and the memory bank, and
|
> [`planning/CONTEXT-AND-MEMORY.md`](planning/CONTEXT-AND-MEMORY.md), which cover the context
|
||||||
> state the reasoning behind each one ([Markdown version](docs/GUIDE.md)).
|
> budgeting, the state model and the memory bank as this fork builds them.
|
||||||
|
|
||||||
Built with FastAPI and SQLAlchemy on the backend and React (Vite) on the frontend, running on
|
Built with FastAPI and SQLAlchemy on the backend and React (Vite) on the frontend, storing
|
||||||
SQLite locally and Postgres in the cloud. It works with **any OpenAI-compatible endpoint**:
|
everything in one SQLite file.
|
||||||
Ollama and LM Studio locally, or OpenRouter, OpenAI, Groq, or vLLM in the cloud. Endpoint, key,
|
|
||||||
and model are all runtime settings, and OpenRouter's free-tier models make the whole experience
|
|
||||||
cost nothing.
|
|
||||||
|
|
||||||

|
On the play screen, the left rail carries live world state. The AI proposes changes each turn
|
||||||
|
and a Python engine decides what actually sticks; the chip under the narration reports what
|
||||||
*The play screen. The left rail shows live world state. The AI proposes changes each turn, and
|
changed. The `‹ 2/2 ›` under a turn steps between the takes it has, and writing below a take
|
||||||
a Python engine decides what actually sticks. The chip under the narration reports what
|
that isn't the live one starts a new branch.
|
||||||
changed. The `‹ 2/2 ›` under a turn steps between the takes it has. Writing below a take that
|
|
||||||
isn't the live one starts a new branch.*
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **The full play loop.** Do / Say / Story / Continue actions, streamed AI responses (SSE),
|
- **The full play loop.** Do / Say / Story / Continue actions, streamed AI responses (SSE),
|
||||||
retry, undo, and edit. Reasoning models are supported: "thinking" streams into a collapsible
|
retry, undo, redo, and edit. Reasoning models are supported: "thinking" streams into a collapsible
|
||||||
💭 panel with its own token budget.
|
💭 panel with its own token budget.
|
||||||
- **A branching story tree.** The story is a tree, not a list. Any turn can hold more than one
|
- **A branching story tree.** The story is a tree, not a list. Any turn can hold more than one
|
||||||
**take**, and `‹ 2/4 ›` steps between them. Stepping is free: the story below simply empties,
|
**take**, and `‹ 2/4 ›` steps between them. Stepping is free: the story below simply empties,
|
||||||
@@ -47,7 +46,8 @@ isn't the live one starts a new branch.*
|
|||||||
cast; the adventure carries their live values. The AI proposes deltas, and a Python engine
|
cast; the adventure carries their live values. The AI proposes deltas, and a Python engine
|
||||||
referees them: it clamps values to range, enforces per-turn caps and cooldowns, keeps counters
|
referees them: it clamps values to range, enforces per-turn caps and cooldowns, keeps counters
|
||||||
monotonic and milestones sticky, then strips the machine-readable block out of the prose
|
monotonic and milestones sticky, then strips the machine-readable block out of the prose
|
||||||
(`backend/app/worldstate/engine.py`). Word-labeled bands (`40–60: minor damage`) make the
|
(`backend/app/worldstate/`: `apply.py` clamps, `parse.py` reads the block back).
|
||||||
|
Word-labeled bands (`40–60: minor damage`) make the
|
||||||
model reliable at it. No dice and no scripting are required.
|
model reliable at it. No dice and no scripting are required.
|
||||||
- **AI Dungeon-compatible context engine.** Memory, author's note, and story cards (world
|
- **AI Dungeon-compatible context engine.** Memory, author's note, and story cards (world
|
||||||
info) are triggered by keywords in recent story text, then assembled under a token budget
|
info) are triggered by keywords in recent story text, then assembled under a token budget
|
||||||
@@ -55,41 +55,54 @@ isn't the live one starts a new branch.*
|
|||||||
- **Insights: total prompt transparency.** Every turn stores the exact prompt sent to the
|
- **Insights: total prompt transparency.** Every turn stores the exact prompt sent to the
|
||||||
model. Open 🔍 on any AI action to see each context component, its token cost, and why it was
|
model. Open 🔍 on any AI action to see each context component, its token cost, and why it was
|
||||||
included.
|
included.
|
||||||
- **JavaScript scripting, AI Dungeon-compatible.** `onInput` / `onModelContext` / `onOutput`
|
|
||||||
modifiers share `state` and a `worldEntries` API, and run in an embedded quickjs sandbox
|
|
||||||
(`backend/app/scripting/`). Real AI Dungeon scripts import and run as is. An in-app CodeMirror
|
|
||||||
editor is included.
|
|
||||||
- **Auto-summarization and Memory Bank.** The modern AI Dungeon memory system: AI-generated
|
- **Auto-summarization and Memory Bank.** The modern AI Dungeon memory system: AI-generated
|
||||||
memories every few actions, a running story summary, and embedding-based retrieval that
|
memories every few actions, a running story summary, and embedding-based retrieval that
|
||||||
pulls old-but-relevant facts back into context, with similarity scores visible in Insights
|
pulls old-but-relevant facts back into context, with similarity scores visible in Insights
|
||||||
(`backend/app/memorybank.py`).
|
(`backend/app/memorybank.py`).
|
||||||
- **Undo and retry that actually roll back state.** Undo and retry roll back the world state
|
- **Undo, Redo, and retry that roll back state and delete nothing.** Undo moves where the story
|
||||||
and script state to a per-node snapshot, not just the text, and prune the memories that
|
is being read; it removes no accepted turn, so Redo can walk forward into the turns it stepped
|
||||||
covered the removed turns. Nothing a retry replaces is discarded: the old attempt stays as
|
over. Both restore the world state from a per-node snapshot rather than just the text, and a
|
||||||
another take of that turn, one keystroke and one click from becoming a branch of its own.
|
memory derived from a turn now behind the head stops being retrieved without being deleted or
|
||||||
- **Import and export.** AI Dungeon-compatible formats for scripts and scenarios; JSON for
|
re-embedded. Writing a new turn below a moved-back head is the moment the story forks: the
|
||||||
everything else. An adventure exports as `ai-dnd-adventure-v2`, which carries the whole tree:
|
displaced future stays on the line it was written for, and ordinary Redo stops offering it.
|
||||||
every branch, every take, and the fork points, since those were chosen rather than computed.
|
Nothing a retry replaces is discarded either — the old attempt stays as another take of that
|
||||||
Files saved in the old single-line format still import.
|
turn, one keystroke and one click from becoming a branch of its own.
|
||||||
- **Optional accounts for hosted deployments.** By default the app is single-user with zero
|
- **Save Points.** Name a moment — "Before entering the abbey" — keep playing,
|
||||||
auth friction. Set `AIDND_MULTI_USER=1` and visitors play instantly as guests (signed
|
restart the app, and come back to it. Restoring one moves the story back to
|
||||||
session cookie), can register (email and password) at any point to keep their data, and each
|
that moment and deletes nothing: the turns you wrote after it stay, Redo still
|
||||||
user gets isolated data plus their own encrypted-at-rest API key. A server-funded **shared
|
walks forward into them, and writing something different from the Save Point
|
||||||
demo key** with a daily turn cap lets people try it without bringing a key
|
is what starts a new line while the old one is kept. A Save Point is a name for
|
||||||
(`backend/app/auth.py`). Each new guest is also given a copy of a short pre-played
|
a position and holds no copy of the story, so restoring it is the same
|
||||||
adventure, so the first screen shows real turns and their world-state changes without
|
movement Undo makes (`backend/app/routers/adventures/checkpoints.py`,
|
||||||
spending a demo turn (`backend/app/starter.py`).
|
`backend/app/head.py`). They last until *you* delete them: deleting one deletes
|
||||||
|
no story, and deleting a branch a Save Point is kept on is refused until you
|
||||||
|
remove the Save Point yourself, so nothing takes a named moment away behind
|
||||||
|
your back.
|
||||||
|
- **Import and export.** AI Dungeon-compatible scenario format; JSON for everything else. An adventure exports as `ai-dnd-adventure-v2`, which carries the whole tree:
|
||||||
|
every branch, every take, the fork points, which branches the story has left behind, the Save
|
||||||
|
Points and the position it is being read at — all of them chosen rather than computed, which is
|
||||||
|
the rule for what a bundle carries. A campaign opens where its head says, never at a Save Point
|
||||||
|
merely because it has one. A campaign exported after two Undos imports still undone, with its
|
||||||
|
retained future intact, instead of silently reopening at its newest turn. Files that predate
|
||||||
|
the head position, and files saved in the old single-line format, still import.
|
||||||
|
- **Single user, no accounts.** There is no sign-up, no login, no session and no API key
|
||||||
|
anywhere in the product. The storyteller API binds to loopback and is unauthenticated by
|
||||||
|
design, because the only person who can reach it is the person running it. A new install
|
||||||
|
starts with a short pre-played adventure, so the first screen shows real turns and their
|
||||||
|
world-state changes rather than an empty page (`backend/app/starter.py`).
|
||||||
|
- **A refusal you can rely on.** The inference endpoint is checked against an address
|
||||||
|
allowlist when you save it and again before every request, so a public endpoint is refused
|
||||||
|
even if the setting is edited in the database directly. TLS verification is never traded
|
||||||
|
against reachability: a privately issued certificate is verified against your machine's own
|
||||||
|
trust store, and there is no bypass switch.
|
||||||
|
|
||||||
## Screenshots
|
## Screenshots
|
||||||
|
|
||||||
| | |
|
None yet. The inherited screenshots showed upstream's UI — a Scripts tab, Log in and Sign up,
|
||||||
|---|---|
|
a guest banner, scripting demo scenarios — none of which this fork has since M2, so they were
|
||||||
|  |  |
|
removed rather than left standing as a picture of a product that no longer exists. The M4
|
||||||
| **Insights**: the exact prompt for the next turn, broken into components with token counts and the trigger word that pulled each story card in. | **Authoring**: stats with ranges, per-turn caps, cooldowns, and word-labeled bands; NPCs the AI addresses by id. |
|
closeout drove the real application in a real browser, so the screens exist and work; taking
|
||||||
|  |  |
|
presentable screenshots of them is a job for the UI pass in M8.
|
||||||
| **Scripting**: the three AI Dungeon hooks with shared persistent `state`, run in a quickjs sandbox. | **Home**: continue a story in progress or start from a scenario. |
|
|
||||||
|  |  |
|
|
||||||
| **The tree**: one lane per line, from the moment it left its parent to the moment it ends. The horizontal axis is the story's own clock, so a short branch reads as short. | **Branches**: every line the story has taken, and the three things you can do to one. A line the one you're reading was forked from can't be deleted, and says so. |
|
|
||||||
|
|
||||||
## Quick start
|
## Quick start
|
||||||
|
|
||||||
@@ -121,58 +134,67 @@ Open http://localhost:5173.
|
|||||||
|
|
||||||
## Connect a model
|
## Connect a model
|
||||||
|
|
||||||
Open **Settings** in the app and point it at any OpenAI-compatible endpoint:
|
Ollama is the inference backend v1 supports. Open **Settings** in the app and point it at one:
|
||||||
|
|
||||||
| Provider | Endpoint URL | Notes |
|
| Where Ollama runs | Endpoint URL | Notes |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| Ollama (local) | `http://localhost:11434/v1` | free, private; also serves embedding models for the Memory Bank (e.g. `nomic-embed-text`) |
|
| The same machine | `http://localhost:11434/v1` | the default, and the simplest thing that works |
|
||||||
| LM Studio (local) | `http://localhost:1234/v1` | free, private |
|
| A machine on your own network | `http://<host>:11434/v1` or `https://<host>/v1` | explicitly configured; see below |
|
||||||
| OpenRouter | `https://openrouter.ai/api/v1` | `:free` models cost nothing (no embeddings on the free tier) |
|
|
||||||
| OpenAI / Groq / vLLM / … | provider's `/v1` URL | anything speaking `/v1/chat/completions` |
|
|
||||||
| Claude Code CLI (local) | `http://127.0.0.1:8787/v1` | your Claude subscription instead of an API key; see [Playing against Claude locally](#playing-against-claude-locally) |
|
|
||||||
|
|
||||||
Model name, API key, generation parameters, and (optionally) summary and embedding models for
|
Model name, generation parameters, and (optionally) summary and embedding models for the
|
||||||
the Memory Bank are all configured there too. No config files and no rebuild are needed.
|
Memory Bank are configured there too. No config files and no rebuild are needed. There is no
|
||||||
|
API key field, because there is nothing to authenticate to.
|
||||||
|
|
||||||
### Playing against Claude locally
|
The adapter underneath speaks the OpenAI-compatible protocol, because that is what Ollama
|
||||||
|
serves. That is an implementation detail, not a promise of support for arbitrary local
|
||||||
|
servers that happen to speak the same protocol. Public and cloud inference endpoints are
|
||||||
|
prohibited outright — see `planning/DECISIONS/002-ollama-only-v1.md` and
|
||||||
|
`planning/DECISIONS/011-local-inference-endpoint-policy.md`.
|
||||||
|
|
||||||
`backend/tools/claude_shim.py` serves an OpenAI-compatible endpoint backed by the
|
### What the endpoint policy allows
|
||||||
`claude` command line tool, so you can play the demos against a real model without an
|
|
||||||
API key. Each request spawns one `claude --print` process, which suits the turn engine:
|
The address is checked when you save it and again before every request. Only loopback and
|
||||||
the app assembles the whole prompt every turn and expects a stateless endpoint.
|
private-network addresses are accepted; every public address is refused, by address rather than
|
||||||
|
by hostname, so a name that resolves outward is refused too. A well-known cloud inference host
|
||||||
|
is named in the error message only so the refusal says *why*.
|
||||||
|
|
||||||
|
Running the model on a second machine you control is supported and expected — that machine
|
||||||
|
does the inference while the storyteller itself stays bound to loopback on yours. If that
|
||||||
|
machine serves HTTPS with a certificate from a CA you installed, it works: certificates are
|
||||||
|
verified against your operating system's trust store as well as the bundled one. Verification
|
||||||
|
itself is never relaxed, and there is no option to turn it off.
|
||||||
|
|
||||||
|
### Playing against a local shim (development only)
|
||||||
|
|
||||||
|
`backend/tools/claude_shim.py` serves an OpenAI-compatible endpoint on `127.0.0.1:8787`
|
||||||
|
backed by a command-line tool, which is useful for testing the turn engine against a stronger
|
||||||
|
model. Each request spawns one process, which suits the engine: the app assembles the whole
|
||||||
|
prompt every turn and expects a stateless endpoint.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd backend
|
cd backend
|
||||||
.venv/Scripts/python.exe tools/claude_shim.py # listens on 127.0.0.1:8787
|
.venv/bin/python tools/claude_shim.py # listens on 127.0.0.1:8787
|
||||||
```
|
```
|
||||||
|
|
||||||
In Settings, choose the OpenAI-compatible provider, set the base URL to
|
Set the base URL to `http://127.0.0.1:8787/v1` and pick a model the tool offers. Set the
|
||||||
`http://127.0.0.1:8787/v1`, put any non-empty string in the API key field, and pick
|
reasoning budget to `0` or `-1`: a positive budget sends a `reasoning.max_tokens` field that
|
||||||
`sonnet`. The shim ignores the key and authenticates as you, through the CLI. Set the
|
some models reject. Embeddings are not served — leave the embedding model blank, or point the
|
||||||
reasoning budget to `0` or `-1`: a positive budget sends a `reasoning.max_tokens` field
|
Memory Bank at an endpoint that serves one.
|
||||||
that Claude 5 models reject.
|
|
||||||
|
|
||||||
Embeddings are not served. Leave the embedding model blank, or point the Memory Bank at
|
The shim has no authentication and spends whatever quota backs it, so run it on loopback and
|
||||||
a real endpoint.
|
leave it there.
|
||||||
|
|
||||||
Run it against a local backend only. The endpoint has no authentication, and anything
|
|
||||||
reaching it spends your Claude quota. `app/netguard.py` blocks localhost endpoints when
|
|
||||||
`AIDND_MULTI_USER` is set, so a deployed instance cannot be pointed at it.
|
|
||||||
|
|
||||||
## How a turn works
|
## How a turn works
|
||||||
|
|
||||||
```
|
```
|
||||||
player input
|
player input
|
||||||
→ onInput script modifier
|
|
||||||
→ assemble context: [narrator prompt] + [world state + stat guide] + [AI instructions]
|
→ assemble context: [narrator prompt] + [world state + stat guide] + [AI instructions]
|
||||||
+ [plot essentials] + [story summary] + [retrieved memories]
|
+ [plot essentials] + [story summary] + [retrieved memories]
|
||||||
+ [triggered story cards] + [history along this branch, token-budgeted]
|
+ [triggered story cards] + [history along this branch, token-budgeted]
|
||||||
+ [author's note] + [player action]
|
+ [author's note] + [player action]
|
||||||
→ onModelContext script modifier
|
|
||||||
→ snapshot context (Insights)
|
→ snapshot context (Insights)
|
||||||
→ provider adapter → AI (streamed)
|
→ provider adapter → AI (streamed)
|
||||||
→ extract + referee the world-state delta block, strip it from the prose
|
→ extract + referee the world-state delta block, strip it from the prose
|
||||||
→ onOutput script modifier
|
|
||||||
→ store & render
|
→ store & render
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -180,18 +202,18 @@ player input
|
|||||||
|
|
||||||
```
|
```
|
||||||
frontend/ React + Vite SPA ──HTTP/SSE──► backend/ FastAPI
|
frontend/ React + Vite SPA ──HTTP/SSE──► backend/ FastAPI
|
||||||
├─ routers/ auth, scenarios, adventures, story cards, scripts, chat, settings, analytics, debug
|
├─ routers/ scenarios, adventures, story cards, chat, settings, debug
|
||||||
├─ models.py SQLAlchemy: User, Scenario, Adventure, Branch, Action, StoryCard, Script, Settings, Memory
|
├─ models.py SQLAlchemy: Scenario, Adventure, Branch, Action, StoryCard, Settings, Memory
|
||||||
├─ migrations.py hand-rolled, versioned via PRAGMA user_version (64 and counting)
|
├─ migrations.py hand-rolled, versioned via PRAGMA user_version (79 and counting)
|
||||||
├─ auth.py guest/registered users, sessions, shared demo key
|
├─ endpoints.py the inference-endpoint address policy
|
||||||
├─ security.py password hashing, cookie signing, API-key encryption
|
├─ tlstrust.py one TLS context: the OS trust store unioned with certifi's
|
||||||
├─ tree.py forking, promotion, and where a node is placed
|
├─ tree.py forking, promotion, and where a node is placed
|
||||||
|
├─ head.py the active head: where the story is read, and what moving it costs
|
||||||
|
├─ checkpoints Save Points: durable names for positions, in routers/adventures/
|
||||||
├─ attempts.py the takes of one turn, grouped by parent
|
├─ attempts.py the takes of one turn, grouped by parent
|
||||||
├─ context/ prompt assembly under a token budget + lineage/history windowing
|
├─ context/ prompt assembly under a token budget + lineage/history windowing
|
||||||
├─ worldstate/ the stat engine: clamps, cooldowns, bands, milestones
|
├─ worldstate/ the stat engine: clamps, cooldowns, bands, milestones
|
||||||
├─ scripting/ quickjs sandbox + AI Dungeon API surface
|
|
||||||
├─ memorybank.py auto-summarization + embedding retrieval
|
├─ memorybank.py auto-summarization + embedding retrieval
|
||||||
├─ analytics.py buffered visit counters + the owner's dashboard query
|
|
||||||
├─ bundle.py the export/import formats, v2 (tree) and a v1 reader
|
├─ bundle.py the export/import formats, v2 (tree) and a v1 reader
|
||||||
├─ providers/ OpenAI-compatible adapter, streaming
|
├─ providers/ OpenAI-compatible adapter, streaming
|
||||||
└─ data.db SQLite (path overridable via AIDND_DB_PATH)
|
└─ data.db SQLite (path overridable via AIDND_DB_PATH)
|
||||||
@@ -202,9 +224,10 @@ development, Vite proxies `/api` to FastAPI.
|
|||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
549 backend tests: unit tests plus full HTTP integration through the real quickjs scripting
|
698 backend tests: unit tests plus full HTTP integration through the real turn engine, with
|
||||||
engine, with the LLM provider mocked. CI runs them on every push, alongside the frontend
|
the model provider mocked. They run with no route to the Internet, which is a requirement
|
||||||
lint/build and a Docker image build.
|
rather than a convenience — an offline claim proved on a machine that has been online once
|
||||||
|
proves nothing.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd backend && pip install -r requirements.txt -r requirements-dev.txt
|
cd backend && pip install -r requirements.txt -r requirements-dev.txt
|
||||||
@@ -233,59 +256,20 @@ most interesting engineering in the repo.
|
|||||||
the number of SQL clauses is bounded by the context window rather than by the number of
|
the number of SQL clauses is bounded by the context window rather than by the number of
|
||||||
forks.
|
forks.
|
||||||
|
|
||||||
## Visit analytics
|
|
||||||
|
|
||||||
The hosted demo keeps its own analytics: an owner-only dashboard at `/analytics` shows
|
|
||||||
traffic, which shared scenarios get played, turns and demo-key spend, errors, and a funnel
|
|
||||||
from *visited* to *played a turn* to *signed up*. It is visible only to the emails listed in
|
|
||||||
`AIDND_ANALYTICS_EMAILS`, and the route returns 404 for everyone else.
|
|
||||||
|
|
||||||
This is built into the app rather than added with a third-party script, for reasons specific
|
|
||||||
to this project: the CSP allows only `script-src 'self'`, ad blockers block the popular
|
|
||||||
trackers, and none of those trackers can see the measurement that matters here, a turn. Counts
|
|
||||||
are aggregated in memory and flushed as UPSERTs, so a visit is a write and never a read, and
|
|
||||||
every dashboard query is a `GROUP BY` that returns tens of rows regardless of traffic volume.
|
|
||||||
That matters: see the egress note above for what reading rows per request costs on this stack.
|
|
||||||
|
|
||||||
## Deploy (Render)
|
|
||||||
|
|
||||||
The repo ships a [`render.yaml`](render.yaml) blueprint: one Docker web service that serves
|
|
||||||
the SPA and API same-origin, backed by external [Neon](https://neon.tech) Postgres. The free
|
|
||||||
Render tier has no persistent disk, so the database lives off-box.
|
|
||||||
|
|
||||||
1. Create a **Neon** project and copy its pooled connection string.
|
|
||||||
2. In Render, choose **New → Blueprint** and point it at this repo. Render reads
|
|
||||||
`render.yaml`.
|
|
||||||
3. Fill in the secrets it prompts for (`sync: false` vars): `AIDND_DATABASE_URL` (the Neon
|
|
||||||
string); `AIDND_DEMO_API_KEY` and `AIDND_DEMO_MODELS` to offer a no-signup demo; and
|
|
||||||
`AIDND_ANALYTICS_EMAILS` (your own account's email) to see the Visitors dashboard.
|
|
||||||
`AIDND_SECRET_KEY` is generated automatically and stays stable across deploys.
|
|
||||||
4. Deploy. Pushes to `main` auto-deploy after this. The health check is `/api/health`.
|
|
||||||
|
|
||||||
On the free tier the service sleeps after about 15 minutes idle, and the first request after
|
|
||||||
that takes about 30 to 60 seconds to wake it. Point any keep-warm pinger at `/api/health`,
|
|
||||||
which deliberately doesn't touch the database: waking the database around the clock costs far
|
|
||||||
more than the cold start saves.
|
|
||||||
|
|
||||||
If you put another proxy or CDN in front of Render, set `AIDND_TRUSTED_PROXY_HOPS` to the
|
|
||||||
number of proxies in the chain. It defaults to 1. The rate limiter reads the client IP that
|
|
||||||
many entries from the right of `X-Forwarded-For`, because the trusted edge appends the real
|
|
||||||
one last. Leave it at 1 behind two proxies and the limiter reads an entry the caller
|
|
||||||
supplied, so anyone can rotate the header for a fresh rate-limit bucket per request.
|
|
||||||
|
|
||||||
## Repo notes
|
## Repo notes
|
||||||
|
|
||||||
- `plan/` holds the phased implementation plan this project was built from, kept as a build
|
- `planning/` is this fork's own package: the product specification, the architecture
|
||||||
log. All fourteen phases are complete. The later files (11, 12, 14) also serve as design
|
decisions, the milestone plan, the acceptance contract, and a review report for every
|
||||||
notes for the state-revert, world-state, and story-tree work.
|
milestone shipped. Start at [`planning/README.md`](planning/README.md).
|
||||||
[`plan/STATUS.md`](plan/STATUS.md) is the running thread: what shipped, what was measured,
|
- [`planning/archive/`](planning/archive/README.md) holds the Phase 0 research that chose this
|
||||||
and what is owed next.
|
base and the completed milestone reports. It is history, not instruction.
|
||||||
- [`docs/GUIDE.md`](docs/GUIDE.md) holds design notes: how each subsystem works and why it was
|
- [`DEVELOPMENT.md`](DEVELOPMENT.md) is how to set the project up, point it at a model, and run
|
||||||
built that way, with the measurements behind the decisions. It is also rendered as a
|
the tests. [`PROVENANCE.md`](PROVENANCE.md) records what came from upstream and what changed.
|
||||||
[reading page](https://parththakkar106.github.io/AI-DnD/guide.html).
|
- `backend/.env.example` lists the two environment variables the backend reads. Everything
|
||||||
- `backend/.env.example` lists the few environment variables the backend reads.
|
about the model is a runtime setting on the Settings page instead.
|
||||||
- [`docs/self-review.md`](docs/self-review.md) records a full-codebase self-review pass and
|
- Upstream's own `plan/` build log and `docs/` project site were removed in the 2026-09-03
|
||||||
what came out of it. All correctness findings are resolved.
|
documentation pass: they described AI-DnD's hosted, scripted, multi-user product. Both are
|
||||||
|
still in Git history, and in upstream.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,19 @@ the import recomputes them:
|
|||||||
* `lineage` is a cache of `parent` plus `fork_depth`. Shipping it as well would
|
* `lineage` is a cache of `parent` plus `fork_depth`. Shipping it as well would
|
||||||
put a second source of truth for one fact into a file anyone can hand-edit,
|
put a second source of truth for one fact into a file anyone can hand-edit,
|
||||||
and the two could then disagree without any read reporting it.
|
and the two could then disagree without any read reporting it.
|
||||||
* The head depth is the tip of the head branch, which is a fact about the nodes
|
* The head depth used to be derived the same way — the tip of the head branch,
|
||||||
that arrived with it.
|
a fact about the nodes that arrived with it. M3 moved it to the other side of
|
||||||
|
the rule. Undo no longer deletes, so a story can be read at a position behind
|
||||||
|
its retained tip, and where the reader stopped is a decision nobody can
|
||||||
|
recompute from the rows: the same tree exports identically whether the user
|
||||||
|
undid three turns or none. `headDepth` is therefore written, and an import
|
||||||
|
that ignored it would silently Redo the story to its newest retained turn,
|
||||||
|
which is the Phase 0B export finding this milestone exists to close.
|
||||||
|
|
||||||
|
A bundle written before M3 has no `headDepth` key, and there is nothing lost in
|
||||||
|
that: at the time it was written the head could not sit behind the tip, so the
|
||||||
|
derived answer *was* the recorded one. Such a file imports by deriving, exactly
|
||||||
|
as it always did.
|
||||||
|
|
||||||
Every hand-editable coordinate is therefore checked before a row is written, in
|
Every hand-editable coordinate is therefore checked before a row is written, in
|
||||||
`plan`, rather than repaired afterwards. An import that fails partway leaves an
|
`plan`, rather than repaired afterwards. An import that fails partway leaves an
|
||||||
@@ -117,9 +128,22 @@ def export(db: Session, adventure: models.Adventure) -> dict:
|
|||||||
# belongs to is the root. `_local` places its nodes there.
|
# belongs to is the root. `_local` places its nodes there.
|
||||||
"branches": [_exported_branch(b, local) for b in branches] or [_ROOT],
|
"branches": [_exported_branch(b, local) for b in branches] or [_ROOT],
|
||||||
"headBranch": local.get(adventure.head_branch_id, 0),
|
"headBranch": local.get(adventure.head_branch_id, 0),
|
||||||
|
# M3. Where the story is being read, which is not always where it
|
||||||
|
# ends. See the head-depth rule at the top of this module.
|
||||||
|
"headDepth": adventure.head_depth,
|
||||||
"memoryCursor": _exported_anchor(adventure, cursors.MEMORY, local),
|
"memoryCursor": _exported_anchor(adventure, cursors.MEMORY, local),
|
||||||
"summaryCursor": _exported_anchor(adventure, cursors.SUMMARY, local),
|
"summaryCursor": _exported_anchor(adventure, cursors.SUMMARY, local),
|
||||||
"memories": [_exported_memory(m, local) for m in adventure.memories],
|
"memories": [_exported_memory(m, local) for m in adventure.memories],
|
||||||
|
# M4. A Save Point is a decision — someone chose this position and gave
|
||||||
|
# it a name — so it goes in the file by the rule at the top of this
|
||||||
|
# module. Nothing here is derived: the coordinate is the one stored, not
|
||||||
|
# one recomputed from the rows, because the whole point of the pointer
|
||||||
|
# is that no amount of reading the turns can tell you which one somebody
|
||||||
|
# named. A bundle written before M4 has no key here and imports with no
|
||||||
|
# Save Points, which is what such a campaign had.
|
||||||
|
"checkpoints": [
|
||||||
|
_exported_checkpoint(c, local) for c in _checkpoints_of(db, adventure)
|
||||||
|
],
|
||||||
"storyCards": [
|
"storyCards": [
|
||||||
{"type": c.type, "name": c.name, "keys": c.keys,
|
{"type": c.type, "name": c.name, "keys": c.keys,
|
||||||
"entry": c.entry, "notes": c.notes}
|
"entry": c.entry, "notes": c.notes}
|
||||||
@@ -151,6 +175,15 @@ def _exported_branch(branch: models.Branch, local: dict[int, int]) -> dict:
|
|||||||
# unnamed tree byte-identical to the one SP6 wrote.
|
# unnamed tree byte-identical to the one SP6 wrote.
|
||||||
if branch.name:
|
if branch.name:
|
||||||
out["name"] = branch.name
|
out["name"] = branch.name
|
||||||
|
# M3. A branch the story left behind is a decision too — the depth a
|
||||||
|
# divergent write departed at — so it goes in the file by the same rule that
|
||||||
|
# puts the fork points in it. A restored backup that could not tell abandoned
|
||||||
|
# history from active history would have lost the only thing distinguishing
|
||||||
|
# them, since every row of both arrives either way. Omitted when the branch
|
||||||
|
# is active, which keeps a file for a never-undone tree as it was.
|
||||||
|
if branch.superseded_at is not None:
|
||||||
|
out["supersededAt"] = branch.superseded_at.isoformat()
|
||||||
|
out["supersededDepth"] = branch.superseded_depth
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
@@ -192,6 +225,35 @@ def _exported_memory(memory: models.Memory, local: dict[int, int]) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _checkpoints_of(db: Session, adventure: models.Adventure) -> list[models.Checkpoint]:
|
||||||
|
"""Returns the campaign's Save Points in creation order.
|
||||||
|
|
||||||
|
Read with a query rather than through a relationship, for the reason
|
||||||
|
`models.Branch` declares none: a relationship on `Adventure` would be loaded
|
||||||
|
by anything that touches an adventure, and the export is the only thing in
|
||||||
|
the application that wants every Save Point at once.
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
db.query(models.Checkpoint)
|
||||||
|
.filter(models.Checkpoint.adventure_id == adventure.id)
|
||||||
|
.order_by(models.Checkpoint.id)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _exported_checkpoint(checkpoint: models.Checkpoint, local: dict[int, int]) -> dict:
|
||||||
|
return {
|
||||||
|
"name": checkpoint.name,
|
||||||
|
"note": checkpoint.note,
|
||||||
|
# The branch as a position in this file's list, like every other branch
|
||||||
|
# reference in the bundle. The depth is a coordinate along it and needs
|
||||||
|
# no translation.
|
||||||
|
"branch": _local(checkpoint.branch_id, local),
|
||||||
|
"depth": checkpoint.depth,
|
||||||
|
"createdAt": checkpoint.created_at.isoformat() if checkpoint.created_at else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def _imported_persona(persona) -> dict:
|
def _imported_persona(persona) -> dict:
|
||||||
"""Reads a bundle's `persona` block into `Adventure` keyword arguments.
|
"""Reads a bundle's `persona` block into `Adventure` keyword arguments.
|
||||||
|
|
||||||
@@ -252,11 +314,23 @@ def plan(bundle: dict, version: str) -> dict:
|
|||||||
_planned_nodes(bundle, len(branches)) if version == FORMAT
|
_planned_nodes(bundle, len(branches)) if version == FORMAT
|
||||||
else _planned_v1_nodes(bundle)
|
else _planned_v1_nodes(bundle)
|
||||||
)
|
)
|
||||||
|
head = _as_index(bundle.get("headBranch"), len(branches), default=0)
|
||||||
return {
|
return {
|
||||||
"branches": branches,
|
"branches": branches,
|
||||||
"nodes": nodes,
|
"nodes": nodes,
|
||||||
"memories": _planned_memories(bundle, len(branches)),
|
"memories": _planned_memories(bundle, len(branches)),
|
||||||
"head": _as_index(bundle.get("headBranch"), len(branches), default=0),
|
# M4. Empty for a version 1 bundle and for any version 2 bundle written
|
||||||
|
# before Save Points existed, which is the same answer: no one had named
|
||||||
|
# a position in those campaigns.
|
||||||
|
"checkpoints": (
|
||||||
|
_planned_checkpoints(bundle, len(branches), nodes)
|
||||||
|
if version == FORMAT else []
|
||||||
|
),
|
||||||
|
"head": head,
|
||||||
|
# None means the file does not say, which is every version 1 bundle and
|
||||||
|
# every version 2 bundle written before M3. `_point_the_head` derives it
|
||||||
|
# then, which is what those files were written expecting.
|
||||||
|
"headDepth": _planned_head_depth(bundle, branches, nodes, head),
|
||||||
# Version 2 records where the derived work reached. Version 1 counted
|
# Version 2 records where the derived work reached. Version 1 counted
|
||||||
# it, and a count cannot become a node until the nodes exist. See
|
# it, and a count cannot become a node until the nodes exist. See
|
||||||
# `settle`.
|
# `settle`.
|
||||||
@@ -268,6 +342,58 @@ def plan(bundle: dict, version: str) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _derived_tip(branches: list[dict], nodes: list[dict], head: int) -> int:
|
||||||
|
"""Returns where the head branch's story ends, which is where a file that
|
||||||
|
does not state a head depth is opened.
|
||||||
|
|
||||||
|
This is the rule `tree.refresh_head` applies and the one every bundle
|
||||||
|
written before M3 was exported under: the deepest node that arrived on the
|
||||||
|
head branch, or, for a branch that carries none of its own, the fork point
|
||||||
|
it inherits its story up to.
|
||||||
|
"""
|
||||||
|
depths = [n["depth"] for n in nodes if n["branch"] == head]
|
||||||
|
if depths:
|
||||||
|
return max(depths)
|
||||||
|
fork_depth = branches[head].get("forkDepth")
|
||||||
|
return fork_depth if fork_depth is not None else lineage.NO_DEPTH
|
||||||
|
|
||||||
|
|
||||||
|
def _planned_head_depth(
|
||||||
|
bundle: dict, branches: list[dict], nodes: list[dict], head: int
|
||||||
|
) -> int | None:
|
||||||
|
"""Returns the active head depth the file states, or None if it states none.
|
||||||
|
|
||||||
|
Checked here rather than repaired later, for the reason the module docstring
|
||||||
|
gives: a coordinate that disagrees with the rows is not a value any read can
|
||||||
|
be given, and an import that discovers it afterwards has already written
|
||||||
|
half a tree.
|
||||||
|
|
||||||
|
Two bounds. A head past the retained tip is a file claiming the story is
|
||||||
|
read somewhere it does not reach — the shape a truncated or hand-edited
|
||||||
|
export takes, and the one that would silently move the reader forward.
|
||||||
|
Below, `NO_DEPTH` is the floor, because an adventure with no story at all
|
||||||
|
sits one step in front of its first node.
|
||||||
|
|
||||||
|
A head *behind* the tip is not an error. It is the whole point: it is a
|
||||||
|
story the user undid and did not redo, and it must import undone.
|
||||||
|
"""
|
||||||
|
stated = bundle.get("headDepth")
|
||||||
|
if stated is None:
|
||||||
|
return None
|
||||||
|
tip = _derived_tip(branches, nodes, head)
|
||||||
|
if not _is_int(stated) or stated < lineage.NO_DEPTH:
|
||||||
|
raise HTTPException(
|
||||||
|
400, f"The file gives the active head depth as {stated!r}."
|
||||||
|
)
|
||||||
|
if stated > tip:
|
||||||
|
raise HTTPException(
|
||||||
|
400,
|
||||||
|
f"The file reads its story at depth {stated}, but branch {head} "
|
||||||
|
f"ends at {tip}.",
|
||||||
|
)
|
||||||
|
return stated
|
||||||
|
|
||||||
|
|
||||||
def _planned_branches(bundle: dict) -> list[dict]:
|
def _planned_branches(bundle: dict) -> list[dict]:
|
||||||
raw = bundle.get("branches")
|
raw = bundle.get("branches")
|
||||||
entries = [b for b in raw if isinstance(b, dict)] if isinstance(raw, list) else []
|
entries = [b for b in raw if isinstance(b, dict)] if isinstance(raw, list) else []
|
||||||
@@ -277,8 +403,9 @@ def _planned_branches(bundle: dict) -> list[dict]:
|
|||||||
for i, entry in enumerate(entries):
|
for i, entry in enumerate(entries):
|
||||||
parent = entry.get("parent")
|
parent = entry.get("parent")
|
||||||
name = _planned_branch_name(entry, i)
|
name = _planned_branch_name(entry, i)
|
||||||
|
left = _planned_supersession(entry)
|
||||||
if parent is None:
|
if parent is None:
|
||||||
specs.append(dict(_ROOT, **({"name": name} if name else {})))
|
specs.append(dict(_ROOT, **({"name": name} if name else {}), **left))
|
||||||
continue
|
continue
|
||||||
# A branch may fork only from a branch listed before it. The export
|
# A branch may fork only from a branch listed before it. The export
|
||||||
# writes them that way, because branches are numbered in creation order
|
# writes them that way, because branches are numbered in creation order
|
||||||
@@ -302,10 +429,27 @@ def _planned_branches(bundle: dict) -> list[dict]:
|
|||||||
specs.append({
|
specs.append({
|
||||||
"parent": parent, "forkDepth": fork_depth,
|
"parent": parent, "forkDepth": fork_depth,
|
||||||
**({"name": name} if name else {}),
|
**({"name": name} if name else {}),
|
||||||
|
**left,
|
||||||
})
|
})
|
||||||
return specs
|
return specs
|
||||||
|
|
||||||
|
|
||||||
|
def _planned_supersession(entry: dict) -> dict:
|
||||||
|
"""Returns the branch's disposition as the file gives it, or nothing.
|
||||||
|
|
||||||
|
Both keys or neither. A time with no depth cannot say what was displaced and
|
||||||
|
a depth with no time is not a record of anything having happened, so a file
|
||||||
|
carrying one of them is treated as carrying neither rather than half of a
|
||||||
|
fact — no read depends on these columns, and inventing the missing half
|
||||||
|
would be the only way to get a wrong answer out of them.
|
||||||
|
"""
|
||||||
|
at = _as_time(entry.get("supersededAt"))
|
||||||
|
depth = entry.get("supersededDepth")
|
||||||
|
if at is None or not _is_int(depth):
|
||||||
|
return {}
|
||||||
|
return {"supersededAt": at, "supersededDepth": depth}
|
||||||
|
|
||||||
|
|
||||||
def _planned_branch_name(entry: dict, i: int) -> str | None:
|
def _planned_branch_name(entry: dict, i: int) -> str | None:
|
||||||
"""Returns the name a branch entry carries, or `None` if nobody named it.
|
"""Returns the name a branch entry carries, or `None` if nobody named it.
|
||||||
|
|
||||||
@@ -427,6 +571,57 @@ def _planned_memories(bundle: dict, branches: int) -> list[dict]:
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _planned_checkpoints(
|
||||||
|
bundle: dict, branches: int, nodes: list[dict]
|
||||||
|
) -> list[dict]:
|
||||||
|
"""Returns the file's Save Points, checked against the tree it also carries.
|
||||||
|
|
||||||
|
A Save Point whose coordinate names no turn in the file is dropped rather
|
||||||
|
than imported, and dropped rather than raising. The two halves of that are
|
||||||
|
each deliberate:
|
||||||
|
|
||||||
|
* Dropped, because an imported pointer to a position the imported story does
|
||||||
|
not contain is a Save Point that can only ever refuse to restore. It would
|
||||||
|
be a row that exists to disappoint.
|
||||||
|
* Not a 400, unlike the head depth. The head is a position the campaign is
|
||||||
|
read at, so a file that misplaces it opens the story in the wrong place
|
||||||
|
and every read is affected. A Save Point is a bookmark, and a bad one
|
||||||
|
spoils nothing else in the file — refusing to import a whole campaign
|
||||||
|
because one bookmark is wrong would lose the story to save the bookmark.
|
||||||
|
|
||||||
|
A name that is blank once trimmed is dropped for the same reason the create
|
||||||
|
endpoint refuses one: an unnamed Save Point is not identifiable in a list.
|
||||||
|
"""
|
||||||
|
# Every coordinate the file writes, not only the ones it marks live.
|
||||||
|
# `_write_nodes` makes exactly one attempt at each coordinate live whatever
|
||||||
|
# the file says, so a coordinate that exists is a coordinate that will
|
||||||
|
# resolve — and reading the flags here would drop a Save Point over a
|
||||||
|
# question the writer has already settled.
|
||||||
|
written = {(n["branch"], n["depth"]) for n in nodes}
|
||||||
|
raw = bundle.get("checkpoints")
|
||||||
|
out: list[dict] = []
|
||||||
|
for entry in raw if isinstance(raw, list) else []:
|
||||||
|
if not isinstance(entry, dict):
|
||||||
|
continue
|
||||||
|
name = str(entry.get("name") or "").strip()[:schemas.CHECKPOINT_NAME_MAX]
|
||||||
|
if not name:
|
||||||
|
continue
|
||||||
|
depth = entry.get("depth")
|
||||||
|
if not _is_int(depth):
|
||||||
|
continue
|
||||||
|
branch = _as_index(entry.get("branch"), branches, default=None)
|
||||||
|
if branch is None or (branch, depth) not in written:
|
||||||
|
continue
|
||||||
|
out.append({
|
||||||
|
"name": name,
|
||||||
|
"note": str(entry.get("note") or ""),
|
||||||
|
"branch": branch,
|
||||||
|
"depth": depth,
|
||||||
|
"createdAt": _as_time(entry.get("createdAt")),
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def _planned_anchors(bundle: dict, branches: int) -> dict:
|
def _planned_anchors(bundle: dict, branches: int) -> dict:
|
||||||
anchors = {}
|
anchors = {}
|
||||||
for name in ("memory", "summary"):
|
for name in ("memory", "summary"):
|
||||||
@@ -452,6 +647,7 @@ def write(db: Session, adventure: models.Adventure, story: dict) -> None:
|
|||||||
_write_nodes(db, adventure, story["nodes"], ids)
|
_write_nodes(db, adventure, story["nodes"], ids)
|
||||||
_write_memories(db, adventure, story["memories"], ids)
|
_write_memories(db, adventure, story["memories"], ids)
|
||||||
_point_the_head(adventure, story, ids)
|
_point_the_head(adventure, story, ids)
|
||||||
|
_write_checkpoints(db, adventure, story["checkpoints"], ids)
|
||||||
_write_anchors(adventure, story, ids)
|
_write_anchors(adventure, story, ids)
|
||||||
|
|
||||||
|
|
||||||
@@ -479,6 +675,8 @@ def _write_branches(
|
|||||||
lineage=[],
|
lineage=[],
|
||||||
name=spec.get("name"),
|
name=spec.get("name"),
|
||||||
created_at=models.utcnow(),
|
created_at=models.utcnow(),
|
||||||
|
superseded_at=spec.get("supersededAt"),
|
||||||
|
superseded_depth=spec.get("supersededDepth"),
|
||||||
)
|
)
|
||||||
).inserted_primary_key[0]
|
).inserted_primary_key[0]
|
||||||
entries = [[new_id, None]]
|
entries = [[new_id, None]]
|
||||||
@@ -565,25 +763,53 @@ def _write_memories(
|
|||||||
db.add(memory)
|
db.add(memory)
|
||||||
|
|
||||||
|
|
||||||
|
def _write_checkpoints(
|
||||||
|
db: Session, adventure: models.Adventure, specs: list[dict], ids: list[int]
|
||||||
|
) -> None:
|
||||||
|
"""Writes the Save Points, and moves nothing.
|
||||||
|
|
||||||
|
Note what this function does not touch. The head is pointed by
|
||||||
|
`_point_the_head` from the file's own `headBranch`/`headDepth`, and importing
|
||||||
|
a Save Point must not disturb it — a campaign exported at turn 30 with a
|
||||||
|
Save Point at turn 12 opens at turn 30. The bundle records where the story
|
||||||
|
was being read and, separately, which positions someone named; restoring one
|
||||||
|
of them is a thing the user does afterwards, not a thing an import does for
|
||||||
|
them.
|
||||||
|
"""
|
||||||
|
for spec in specs:
|
||||||
|
checkpoint = models.Checkpoint(
|
||||||
|
adventure_id=adventure.id,
|
||||||
|
name=spec["name"],
|
||||||
|
note=spec["note"],
|
||||||
|
branch_id=ids[spec["branch"]],
|
||||||
|
depth=spec["depth"],
|
||||||
|
)
|
||||||
|
if spec["createdAt"] is not None:
|
||||||
|
checkpoint.created_at = spec["createdAt"]
|
||||||
|
db.add(checkpoint)
|
||||||
|
|
||||||
|
|
||||||
def _point_the_head(
|
def _point_the_head(
|
||||||
adventure: models.Adventure, story: dict, ids: list[int]
|
adventure: models.Adventure, story: dict, ids: list[int]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Sets which branch the story is played on, and how deep it goes.
|
"""Sets which branch the story is played on, and where it is being read.
|
||||||
|
|
||||||
The branch comes from the file and the depth does not. The tip of a branch is
|
Both come from the file now (M3). A bundle that states its head depth is
|
||||||
whatever arrived on it, and a branch with no nodes of its own sits at its
|
opened exactly where its owner left it, undone turns and all, and the turns
|
||||||
fork point, which is the last node its story contains. That node is borrowed
|
past that point arrive as the retained future they were exported as.
|
||||||
but it is still the tip. This is the rule `tree.refresh_head` applies, run
|
|
||||||
here before a flush.
|
A file that states none is opened at the tip of its head branch — whatever
|
||||||
|
arrived on it, or, for a branch carrying no nodes of its own, the fork point
|
||||||
|
that is the last node its story contains. That is the rule
|
||||||
|
`tree.refresh_head` applies and the only answer a pre-M3 bundle can be given,
|
||||||
|
because at the time it was written the head could not be anywhere else.
|
||||||
"""
|
"""
|
||||||
head = story["head"]
|
head = story["head"]
|
||||||
adventure.head_branch_id = ids[head]
|
adventure.head_branch_id = ids[head]
|
||||||
depths = [n["depth"] for n in story["nodes"] if n["branch"] == head]
|
if story["headDepth"] is not None:
|
||||||
if depths:
|
adventure.head_depth = story["headDepth"]
|
||||||
adventure.head_depth = max(depths)
|
|
||||||
return
|
return
|
||||||
fork_depth = story["branches"][head].get("forkDepth")
|
adventure.head_depth = _derived_tip(story["branches"], story["nodes"], head)
|
||||||
adventure.head_depth = fork_depth if fork_depth is not None else lineage.NO_DEPTH
|
|
||||||
|
|
||||||
|
|
||||||
def _write_anchors(
|
def _write_anchors(
|
||||||
|
|||||||
@@ -78,7 +78,16 @@ class Path:
|
|||||||
"""One story, expressed as a SQL clause and as a Python predicate.
|
"""One story, expressed as a SQL clause and as a Python predicate.
|
||||||
|
|
||||||
The object holds the lineage entries newest first, plus the depth of the
|
The object holds the lineage entries newest first, plus the depth of the
|
||||||
tip. The tip is used only to estimate how much story each entry covers.
|
head. Every entry is read as capped at the head, which is what makes the
|
||||||
|
active head a position the whole application honours (M3).
|
||||||
|
|
||||||
|
Before M3 the head was always the deepest node, so the cap never bit and the
|
||||||
|
tip was used only to estimate how much story each entry covers. Undo now
|
||||||
|
moves the head backward without deleting anything, so a path can have live
|
||||||
|
nodes past its head, and those nodes are not part of the story being told.
|
||||||
|
Capping here is what hides them, and it hides them from every read at once:
|
||||||
|
the transcript, the context builder, `attempts.preceding`, and memory
|
||||||
|
retrieval all funnel through `path_of`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, entries: list[tuple[int, int | None]], tip: int | None = None):
|
def __init__(self, entries: list[tuple[int, int | None]], tip: int | None = None):
|
||||||
@@ -91,6 +100,40 @@ class Path:
|
|||||||
def __len__(self) -> int:
|
def __len__(self) -> int:
|
||||||
return len(self.entries)
|
return len(self.entries)
|
||||||
|
|
||||||
|
# ------------------------------------------------------------- the head
|
||||||
|
|
||||||
|
def _cap(self, max_depth: int | None) -> int | None:
|
||||||
|
"""Returns `max_depth` limited by the head, which no read may pass.
|
||||||
|
|
||||||
|
Three cases, and the third is the one M3 added:
|
||||||
|
|
||||||
|
* No head recorded (`tip is None`). The caller asked for the lineage
|
||||||
|
without a position, so the entry's own cap stands. `tree` builds such
|
||||||
|
a path when it resolves the node in front of a depth.
|
||||||
|
* An uncapped entry, which means "this branch through to its tip". The
|
||||||
|
head is the cap.
|
||||||
|
* A capped entry, which is an ancestor capped at the fork depth. The
|
||||||
|
head still wins when it sits behind that fork, because undoing below
|
||||||
|
a fork point is undoing into the shared prefix. Taking the smaller of
|
||||||
|
the two is what lets Undo walk back past a fork instead of stopping
|
||||||
|
there — safe now that it deletes nothing.
|
||||||
|
"""
|
||||||
|
if self.tip is None:
|
||||||
|
return max_depth
|
||||||
|
if max_depth is None:
|
||||||
|
return self.tip
|
||||||
|
return min(max_depth, self.tip)
|
||||||
|
|
||||||
|
def uncapped(self) -> "Path":
|
||||||
|
"""Returns the same lineage read through to its retained tip.
|
||||||
|
|
||||||
|
This is the retained history, head or no head: what Redo can still walk
|
||||||
|
forward into, and what a write below the head has to fork away from.
|
||||||
|
Only those two callers should use it. Every read of *the story* wants
|
||||||
|
the capped path.
|
||||||
|
"""
|
||||||
|
return Path(self.entries, None)
|
||||||
|
|
||||||
# ---------------------------------------------------------------- SQL
|
# ---------------------------------------------------------------- SQL
|
||||||
|
|
||||||
def clause(
|
def clause(
|
||||||
@@ -125,7 +168,9 @@ class Path:
|
|||||||
entries = self.entries if count is None else self.entries[:count]
|
entries = self.entries if count is None else self.entries[:count]
|
||||||
if not entries:
|
if not entries:
|
||||||
return false()
|
return false()
|
||||||
on_path = or_(*[self._entry_clause(model, b, d) for b, d in entries])
|
on_path = or_(
|
||||||
|
*[self._entry_clause(model, b, self._cap(d)) for b, d in entries]
|
||||||
|
)
|
||||||
if model is models.Action:
|
if model is models.Action:
|
||||||
return and_(on_path, models.Action.live.is_(True))
|
return and_(on_path, models.Action.live.is_(True))
|
||||||
return on_path
|
return on_path
|
||||||
@@ -155,9 +200,10 @@ class Path:
|
|||||||
for branch_id, max_depth in self.entries:
|
for branch_id, max_depth in self.entries:
|
||||||
if node.branch_id != branch_id:
|
if node.branch_id != branch_id:
|
||||||
continue
|
continue
|
||||||
if max_depth is None:
|
cap = self._cap(max_depth)
|
||||||
|
if cap is None:
|
||||||
return True
|
return True
|
||||||
if node.depth is not None and node.depth <= max_depth:
|
if node.depth is not None and node.depth <= cap:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -187,7 +233,7 @@ class Path:
|
|||||||
return total
|
return total
|
||||||
covered = 0
|
covered = 0
|
||||||
for i, (_, max_depth) in enumerate(self.entries):
|
for i, (_, max_depth) in enumerate(self.entries):
|
||||||
top = self.tip if max_depth is None else max_depth
|
top = self._cap(max_depth)
|
||||||
below = self.entries[i + 1][1] if i + 1 < total else NO_DEPTH
|
below = self.entries[i + 1][1] if i + 1 < total else NO_DEPTH
|
||||||
if top is None or below is None:
|
if top is None or below is None:
|
||||||
# Either no tip was recorded, or a hand-written row is missing a
|
# Either no tip was recorded, or a hand-written row is missing a
|
||||||
@@ -211,7 +257,8 @@ class Path:
|
|||||||
story has forked.
|
story has forked.
|
||||||
"""
|
"""
|
||||||
for i, (_, max_depth) in enumerate(self.entries):
|
for i, (_, max_depth) in enumerate(self.entries):
|
||||||
if max_depth is not None and max_depth <= depth:
|
cap = self._cap(max_depth)
|
||||||
|
if cap is not None and cap <= depth:
|
||||||
return i
|
return i
|
||||||
return len(self.entries)
|
return len(self.entries)
|
||||||
|
|
||||||
@@ -241,7 +288,8 @@ class Path:
|
|||||||
return depth
|
return depth
|
||||||
for entry_branch, max_depth in self.entries:
|
for entry_branch, max_depth in self.entries:
|
||||||
if entry_branch == branch_id:
|
if entry_branch == branch_id:
|
||||||
return depth if max_depth is None else min(depth, max_depth)
|
cap = self._cap(max_depth)
|
||||||
|
return depth if cap is None else min(depth, cap)
|
||||||
return NO_DEPTH
|
return NO_DEPTH
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,405 @@
|
|||||||
|
"""M3: where the story is being read, and what moving that point costs.
|
||||||
|
|
||||||
|
Undo used to delete. It removed the trailing nodes, let `tree.refresh_head`
|
||||||
|
recompute the tip from what survived, and the story was wherever the rows ended.
|
||||||
|
That made the head a derived value and made Redo impossible, because the turns it
|
||||||
|
would have moved forward into were gone.
|
||||||
|
|
||||||
|
The head is now a stored position that can sit behind the retained tip. Nothing
|
||||||
|
is deleted, so three things that used to be the same question are now three
|
||||||
|
different ones:
|
||||||
|
|
||||||
|
* **the active head** — `adventure.head_branch_id` and `adventure.head_depth`,
|
||||||
|
the end of the story being told. Every read of the story stops here, because
|
||||||
|
`lineage.Path` caps every entry at it.
|
||||||
|
* **the retained tip** — the deepest live node still on the lineage. Redo walks
|
||||||
|
toward it. It is read through `Path.uncapped()`, and only this module and the
|
||||||
|
divergence check may look at it.
|
||||||
|
* **the opening** — the shallowest node on the story, which is the floor Undo
|
||||||
|
may not pass.
|
||||||
|
|
||||||
|
Everything that moves the head or asks a question about it lives here, so the
|
||||||
|
turn engine, Retry, Add-take, Undo, Redo and Edit share one set of rules rather
|
||||||
|
than four similar ones. The Phase 0B spike put the fork check in the write path
|
||||||
|
and left Retry and Add-take on the old one, which is exactly the divergence this
|
||||||
|
module exists to prevent.
|
||||||
|
|
||||||
|
The state that belongs to a position is not recomputed. Every node carries the
|
||||||
|
world state it left behind (`attempts.snapshot_outcome`), so moving the head is a
|
||||||
|
row lookup plus `attempts.restore_state`, at any distance, in either direction.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from sqlalchemy.orm import Session, undefer
|
||||||
|
|
||||||
|
from . import attempts, models, tree
|
||||||
|
from .context import lineage
|
||||||
|
|
||||||
|
# The kinds of node a player writes. An undo or a redo steps over a whole turn,
|
||||||
|
# which is one of these followed by the reply to it, so both ends need to agree
|
||||||
|
# on what "a player's half of a turn" is.
|
||||||
|
PLAYER_TYPES = ("do", "say", "story", "continue")
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ reading
|
||||||
|
|
||||||
|
def opening_depth(db: Session, adventure: models.Adventure) -> int | None:
|
||||||
|
"""Returns the depth of the first node of the story, or None if there is none.
|
||||||
|
|
||||||
|
This is Undo's floor. The Phase 0B spike moved the head to -1 and rendered an
|
||||||
|
empty transcript, because its guard tested for a node of type `start` and an
|
||||||
|
adventure opened with a player-written `story` action has none. Asking the
|
||||||
|
path for its shallowest node needs no such special case: whatever the opening
|
||||||
|
is called, it is the node with the smallest depth, and the story keeps it.
|
||||||
|
|
||||||
|
The read is uncapped. The opening does not move when the head does, and
|
||||||
|
capping would make the floor depend on where the head already is.
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
db.query(models.Action.depth)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
lineage.path_of(db, adventure).uncapped().clause(models.Action),
|
||||||
|
)
|
||||||
|
.order_by(models.Action.depth.asc(), models.Action.id.asc())
|
||||||
|
.limit(1)
|
||||||
|
.scalar()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def retained_tip(db: Session, adventure: models.Adventure) -> int | None:
|
||||||
|
"""Returns the depth of the deepest live node still retained on this lineage.
|
||||||
|
|
||||||
|
This is what the head would be if the story had never been undone, and it is
|
||||||
|
what Redo can reach. It is not the head, and no read of the story may use it.
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
db.query(models.Action.depth)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
lineage.path_of(db, adventure).uncapped().clause(models.Action),
|
||||||
|
)
|
||||||
|
.order_by(models.Action.depth.desc(), models.Action.id.desc())
|
||||||
|
.limit(1)
|
||||||
|
.scalar()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def behind_tip(db: Session, adventure: models.Adventure) -> bool:
|
||||||
|
"""Returns whether retained story sits past the head.
|
||||||
|
|
||||||
|
This one predicate answers every "does this write need to fork?" question in
|
||||||
|
the application. It is true exactly when the user has undone and not redone,
|
||||||
|
which is the only situation in which writing can displace an accepted future.
|
||||||
|
|
||||||
|
It is also what makes "is this turn the tip?" answerable again. Retry,
|
||||||
|
Add-take and `stand_on` each decide between amending a turn in place and
|
||||||
|
giving it a branch, and each used to ask `last_action`, which reads the
|
||||||
|
*capped* path and therefore reports the node at the head as the newest one.
|
||||||
|
Under a moved-back head that answer is wrong in the dangerous direction: it
|
||||||
|
says a turn with an accepted future is a leaf, and amending it in place would
|
||||||
|
leave that future descending from a take that is no longer live.
|
||||||
|
"""
|
||||||
|
tip = retained_tip(db, adventure)
|
||||||
|
return tip is not None and tip > adventure.head_depth
|
||||||
|
|
||||||
|
|
||||||
|
def node_at(
|
||||||
|
db: Session, adventure: models.Adventure, depth: int
|
||||||
|
) -> models.Action | None:
|
||||||
|
"""Returns the live node at `depth` on the retained lineage, outcome loaded.
|
||||||
|
|
||||||
|
The read is uncapped on purpose: Redo asks for a node it is about to move the
|
||||||
|
head onto, which is by definition past the head at the time of asking. The
|
||||||
|
outcome columns are undeferred because the only reason to fetch this row is
|
||||||
|
to restore the state it left behind.
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
lineage.path_of(db, adventure).uncapped().clause(models.Action),
|
||||||
|
models.Action.depth == depth,
|
||||||
|
)
|
||||||
|
.options(
|
||||||
|
undefer(models.Action.state_after),
|
||||||
|
undefer(models.Action.world_state_after),
|
||||||
|
)
|
||||||
|
.order_by(models.Action.id)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def redo_target(db: Session, adventure: models.Adventure) -> int | None:
|
||||||
|
"""Returns the depth the head moves to on Redo, or None if there is nowhere.
|
||||||
|
|
||||||
|
Redo steps over a whole turn, the same unit Undo steps back over, so a
|
||||||
|
player's action and the reply to it move together. Landing between them would
|
||||||
|
show the story an input with no answer and would leave the next Undo undoing
|
||||||
|
half a turn.
|
||||||
|
|
||||||
|
The walk is along the retained lineage, which is what makes Redo follow the
|
||||||
|
continuation that was active rather than choosing among branches. After a
|
||||||
|
divergence the new branch *is* the lineage, and the displaced future is no
|
||||||
|
longer on it, so this returns None without having to know that a divergence
|
||||||
|
happened. That is `STORY-BRANCH-SEMANTICS.md` §8 falling out of the lineage
|
||||||
|
rather than being enforced by a flag.
|
||||||
|
"""
|
||||||
|
ahead = (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
lineage.path_of(db, adventure).uncapped().clause(models.Action),
|
||||||
|
models.Action.depth > adventure.head_depth,
|
||||||
|
)
|
||||||
|
.order_by(models.Action.depth.asc(), models.Action.id.asc())
|
||||||
|
.limit(2)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
if not ahead:
|
||||||
|
return None
|
||||||
|
first = ahead[0]
|
||||||
|
if (
|
||||||
|
first.type in PLAYER_TYPES
|
||||||
|
and len(ahead) > 1
|
||||||
|
and ahead[1].type == "ai"
|
||||||
|
and ahead[1].depth == (first.depth or 0) + 1
|
||||||
|
):
|
||||||
|
return ahead[1].depth
|
||||||
|
return first.depth
|
||||||
|
|
||||||
|
|
||||||
|
def can_redo(db: Session, adventure: models.Adventure) -> bool:
|
||||||
|
"""Returns whether an ordinary Redo is available from where the head is."""
|
||||||
|
return redo_target(db, adventure) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def undo_target(
|
||||||
|
db: Session, adventure: models.Adventure
|
||||||
|
) -> tuple[int, models.Action] | None:
|
||||||
|
"""Returns where Undo moves the head, and the first node it steps back over.
|
||||||
|
|
||||||
|
None means there is nothing to undo, which is either an empty story or a head
|
||||||
|
already resting on the opening. The caller turns that into a 400; this
|
||||||
|
function does not raise, so that the same question can be asked without
|
||||||
|
committing to undoing.
|
||||||
|
|
||||||
|
A turn is the player's node plus the reply to it, and both move together for
|
||||||
|
the reason given in `redo_target`. The player half is only claimed when it is
|
||||||
|
directly in front of the reply, so a bare `continue`, which writes no player
|
||||||
|
node, steps back over the reply alone.
|
||||||
|
"""
|
||||||
|
newest = (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
lineage.path_of(db, adventure).clause(models.Action),
|
||||||
|
)
|
||||||
|
.order_by(models.Action.depth.desc(), models.Action.id.desc())
|
||||||
|
.limit(2)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
if not newest:
|
||||||
|
return None
|
||||||
|
last = newest[0]
|
||||||
|
first_stepped = last
|
||||||
|
before = newest[1] if len(newest) > 1 else None
|
||||||
|
if (
|
||||||
|
last.type == "ai"
|
||||||
|
and before is not None
|
||||||
|
and before.type in PLAYER_TYPES
|
||||||
|
and before.depth == (last.depth or 0) - 1
|
||||||
|
):
|
||||||
|
first_stepped = before
|
||||||
|
floor = opening_depth(db, adventure)
|
||||||
|
if first_stepped.depth is None or floor is None:
|
||||||
|
return None
|
||||||
|
if first_stepped.depth <= floor:
|
||||||
|
# Stepping back over this turn would hide the opening of the campaign,
|
||||||
|
# which is the pre-campaign state `STORY-BRANCH-SEMANTICS.md` §4 stops
|
||||||
|
# at. The floor is the opening node itself rather than depth -1, so an
|
||||||
|
# adventure that opens on a player-written `story` action stops in the
|
||||||
|
# same place as one that opens on a `start` node.
|
||||||
|
return None
|
||||||
|
return first_stepped.depth - 1, first_stepped
|
||||||
|
|
||||||
|
|
||||||
|
def can_undo(db: Session, adventure: models.Adventure) -> bool:
|
||||||
|
"""Returns whether an ordinary Undo is available from where the head is."""
|
||||||
|
return undo_target(db, adventure) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def displaced_history_under(
|
||||||
|
db: Session, adventure: models.Adventure, node: models.Action
|
||||||
|
) -> bool:
|
||||||
|
"""Returns whether story the reader cannot see descends from `node`.
|
||||||
|
|
||||||
|
This is the question an in-place edit has to ask. Editing rewrites one row
|
||||||
|
and re-evaluates nothing, which is what makes it a correction rather than a
|
||||||
|
new continuation. That is harmless while everything descending from the row
|
||||||
|
is on screen: the reader can see what their correction has to stay
|
||||||
|
consistent with. It stops being harmless the moment a continuation descends
|
||||||
|
from the row and is *not* on screen, because the edit then silently changes
|
||||||
|
the words an invisible stretch of story was written from. That is the one
|
||||||
|
way M3's retained history can be made to contradict itself.
|
||||||
|
|
||||||
|
Refusing is deliberately the whole of the fix. Making such an edit fork, so
|
||||||
|
the original text and its future stay whole, is
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` §14-15 — and §15 requires re-evaluating the
|
||||||
|
state the edited prose implies, which is M5's extraction pass. Neither is
|
||||||
|
started here.
|
||||||
|
|
||||||
|
The question is asked as one shape rather than two, because the two ways a
|
||||||
|
descendant becomes invisible turn out to be the same fact. An undone future
|
||||||
|
sits past the head on this very lineage; a displaced line sits past a fork
|
||||||
|
on a branch the story left. In both cases there is a live node, deeper than
|
||||||
|
this one, that descends from it and is not on the path being read — and the
|
||||||
|
departed branch is usually an *ancestor* of the branch now being read, which
|
||||||
|
is why "branches other than the active one" is the wrong set to look at.
|
||||||
|
|
||||||
|
Only the deepest live node on each descending branch is examined. Whether a
|
||||||
|
node is on the read path is monotone in depth: a branch is on the path with
|
||||||
|
a cap, and a node is visible when its depth is at or under that cap. So if
|
||||||
|
the deepest one is visible, every shallower one is too, and if it is not,
|
||||||
|
the answer is already yes.
|
||||||
|
|
||||||
|
A node that is not live has no descendants of its own — a take the story
|
||||||
|
moved past keeps a continuation only by being forked, and that fork is a
|
||||||
|
branch this loop asks about anyway — so editing one is always safe.
|
||||||
|
"""
|
||||||
|
if not node.live or node.depth is None:
|
||||||
|
return False
|
||||||
|
read = lineage.path_of(db, adventure)
|
||||||
|
branches = (
|
||||||
|
db.query(models.Branch)
|
||||||
|
.filter(models.Branch.adventure_id == adventure.id)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
for branch in branches:
|
||||||
|
# Uncapped: the question is what this branch's story descends from, not
|
||||||
|
# how much of it the reader is currently being shown.
|
||||||
|
if not lineage.Path(lineage.entries_of(branch)).contains(node):
|
||||||
|
continue
|
||||||
|
deepest = (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
models.Action.branch_id == branch.id,
|
||||||
|
models.Action.live.is_(True),
|
||||||
|
models.Action.depth > node.depth,
|
||||||
|
)
|
||||||
|
.order_by(models.Action.depth.desc(), models.Action.id.desc())
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if deepest is not None and not read.contains(deepest):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ writing
|
||||||
|
|
||||||
|
def move_to(db: Session, adventure: models.Adventure, depth: int) -> None:
|
||||||
|
"""Moves the active head to `depth` and restores the state recorded there.
|
||||||
|
|
||||||
|
This is the whole of Undo and Redo. Nothing is deleted, nothing is
|
||||||
|
recomputed, and the direction of travel does not matter: the node at the
|
||||||
|
destination carries the world state it left behind, so arriving from in front
|
||||||
|
of it and arriving from behind it restore the same value.
|
||||||
|
|
||||||
|
A destination with no node — the head resting one step in front of the
|
||||||
|
opening — leaves the live state alone, which is `attempts.restore_state`'s
|
||||||
|
rule for a missing snapshot and the reason it is not this function's job to
|
||||||
|
invent an empty one.
|
||||||
|
"""
|
||||||
|
adventure.head_depth = depth
|
||||||
|
attempts.restore_state(adventure, node_at(db, adventure, depth))
|
||||||
|
|
||||||
|
|
||||||
|
def move_to_node(db: Session, adventure: models.Adventure, node: models.Action) -> bool:
|
||||||
|
"""Moves the head onto `node`, changing line only if it is not on this one.
|
||||||
|
|
||||||
|
M4 restores a Save Point through this, and it adds no restoring of its own:
|
||||||
|
the depth half is `move_to` unchanged, so the state, the transcript, the
|
||||||
|
assembled context and memory eligibility all arrive exactly as they do for
|
||||||
|
Undo and Redo. Returns whether the line had to change as well as the depth,
|
||||||
|
which is the one thing about a restore a caller cannot work out afterwards.
|
||||||
|
|
||||||
|
The head is two values, and the two halves move for different reasons. A
|
||||||
|
Save Point almost always names a position on the story being read — its own
|
||||||
|
line, or the shared prefix that line inherits — and then only the depth
|
||||||
|
moves. Leaving the branch alone is what makes the restored position keep the
|
||||||
|
continuation it has: after a divergence, restoring to the shared prefix must
|
||||||
|
put the reader back on the *new* line, where Redo walks into the turns they
|
||||||
|
are still writing, not into the future they left. Reaching for the Save
|
||||||
|
Point's own branch there would quietly hand back the abandoned story.
|
||||||
|
|
||||||
|
The other case is real and has to work. A Save Point survives divergence
|
||||||
|
(`STORY-BRANCH-SEMANTICS.md` §19), so one can name a position on a line the
|
||||||
|
story has since left, and no amount of depth movement reaches a branch this
|
||||||
|
path does not contain. The line then moves as well — one assignment, the
|
||||||
|
same one `switch_branch` makes — and the depth still moves through
|
||||||
|
`move_to`. Nothing is created: a restore never forks, whichever case it
|
||||||
|
takes. The first write below the restored head does, through
|
||||||
|
`fork_if_behind_head`, like every other write.
|
||||||
|
"""
|
||||||
|
switched = not lineage.path_of(db, adventure).uncapped().contains(node)
|
||||||
|
if switched:
|
||||||
|
adventure.head_branch_id = node.branch_id
|
||||||
|
move_to(db, adventure, node.depth)
|
||||||
|
return switched
|
||||||
|
|
||||||
|
|
||||||
|
def fork_if_behind_head(db: Session, adventure: models.Adventure) -> bool:
|
||||||
|
"""Gives the story a new branch when a write would displace a retained future.
|
||||||
|
|
||||||
|
Returns whether a branch was created, which is what a caller reports as a
|
||||||
|
divergence.
|
||||||
|
|
||||||
|
Called before every write that continues the story, and it does nothing on
|
||||||
|
the ordinary path where the head is already at the tip. That is the property
|
||||||
|
worth keeping: a story that is never undone forks exactly as often as it did
|
||||||
|
before M3, so the branch table does not fill up with one branch per turn.
|
||||||
|
|
||||||
|
Undo alone must not fork. Moving the head is not a decision to abandon
|
||||||
|
anything — the user may be reading, or about to Redo. Only the first write
|
||||||
|
below the head states which continuation they mean, which is
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` §8 and §20 and what makes Redo survive an Undo.
|
||||||
|
|
||||||
|
`tree.branch_at` leaves the departed branch exactly as it is: its nodes stay
|
||||||
|
live, at their depths, on their branch. The new branch inherits the story up
|
||||||
|
to the head and owns everything written from here, so the displaced future
|
||||||
|
remains reachable through the branch it was written on.
|
||||||
|
"""
|
||||||
|
if not behind_tip(db, adventure):
|
||||||
|
return False
|
||||||
|
departed = lineage.branch_of(db, adventure)
|
||||||
|
at_depth = adventure.head_depth
|
||||||
|
tree.branch_at(db, adventure, at_depth)
|
||||||
|
if departed is not None:
|
||||||
|
mark_superseded(departed, at_depth)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def mark_superseded(branch: models.Branch, depth: int) -> None:
|
||||||
|
"""Records that this branch's story past `depth` was displaced.
|
||||||
|
|
||||||
|
`DATA-MODEL.md` §5 gives a branch a disposition of active, retained or
|
||||||
|
disposable. This is that disposition, stored as the fact that produced it
|
||||||
|
rather than as a word: the depth the story left at, and when. A branch with
|
||||||
|
no `superseded_at` is active; one with a value has retained history past that
|
||||||
|
depth which no active head is reading.
|
||||||
|
|
||||||
|
Nothing in the application reads these columns to make a decision, and that
|
||||||
|
is deliberate. Redo is decided by the lineage, not by a flag, so a stale or
|
||||||
|
hand-edited value here cannot make the story wrong. They exist so that the
|
||||||
|
cleanup and discarded-history features `STORY-BRANCH-SEMANTICS.md` §28 and
|
||||||
|
§29 leave to a later version have something to select on, and so that a
|
||||||
|
divergence is observable in a test.
|
||||||
|
|
||||||
|
The shallowest departure wins. A branch left at depth 9 and later left again
|
||||||
|
at depth 4 has retained history from 4 onward, and recording the later, deeper
|
||||||
|
value would understate what was displaced.
|
||||||
|
"""
|
||||||
|
if branch.superseded_depth is None or depth < branch.superseded_depth:
|
||||||
|
branch.superseded_depth = depth
|
||||||
|
if branch.superseded_at is None:
|
||||||
|
branch.superseded_at = models.utcnow()
|
||||||
@@ -301,7 +301,8 @@ MIGRATIONS: list[tuple[int, str | dict[str, str]]] = [
|
|||||||
(63, "ALTER TABLE actions ADD COLUMN parent_id INTEGER REFERENCES actions(id) ON DELETE SET NULL"),
|
(63, "ALTER TABLE actions ADD COLUMN parent_id INTEGER REFERENCES actions(id) ON DELETE SET NULL"),
|
||||||
(64, "CREATE INDEX IF NOT EXISTS ix_actions_parent ON actions (parent_id)"),
|
(64, "CREATE INDEX IF NOT EXISTS ix_actions_parent ON actions (parent_id)"),
|
||||||
# Phase 17: `Settings.stream` was dead state. Nothing ever read it, and every
|
# Phase 17: `Settings.stream` was dead state. Nothing ever read it, and every
|
||||||
# turn streams. This is item S1 in `docs/self-review.md`. The table holds one
|
# turn streams; upstream's self-review log flagged it as dead state. The
|
||||||
|
# table holds one
|
||||||
# row per user, so the rewrite is small and needs no VACUUM FULL.
|
# row per user, so the rewrite is small and needs no VACUUM FULL.
|
||||||
(65, "ALTER TABLE settings DROP COLUMN stream"),
|
(65, "ALTER TABLE settings DROP COLUMN stream"),
|
||||||
# Phase 17, SP8: drop the eight columns the story tree replaced. Each one was
|
# Phase 17, SP8: drop the eight columns the story tree replaced. Each one was
|
||||||
@@ -342,6 +343,24 @@ MIGRATIONS: list[tuple[int, str | dict[str, str]]] = [
|
|||||||
# client, which a cold model load on a CPU-only machine can exceed. The
|
# client, which a cold model load on a CPU-only machine can exceed. The
|
||||||
# default matches `providers.openai_compatible.DEFAULT_READ_TIMEOUT`.
|
# default matches `providers.openai_compatible.DEFAULT_READ_TIMEOUT`.
|
||||||
(77, "ALTER TABLE settings ADD COLUMN model_timeout_seconds INTEGER NOT NULL DEFAULT 300"),
|
(77, "ALTER TABLE settings ADD COLUMN model_timeout_seconds INTEGER NOT NULL DEFAULT 300"),
|
||||||
|
|
||||||
|
# M3. A branch left behind by a divergent write records where the story left
|
||||||
|
# it. NULL means active, which is what every existing branch is: before M3
|
||||||
|
# the head could not sit behind the tip, so no branch had been superseded.
|
||||||
|
# No backfill.
|
||||||
|
(78, "ALTER TABLE branches ADD COLUMN superseded_at TIMESTAMP"),
|
||||||
|
(79, "ALTER TABLE branches ADD COLUMN superseded_depth INTEGER"),
|
||||||
|
# M4: Save Points. `create_all` creates the `checkpoints` table itself, on
|
||||||
|
# existing databases as well as fresh ones, exactly as it did for
|
||||||
|
# `memories` at version 2 and `branches` at version 46. What it does not
|
||||||
|
# create is the index every list and every cascade reads, so that is what
|
||||||
|
# this version is.
|
||||||
|
#
|
||||||
|
# No backfill. A Save Point records a decision someone made, and nobody has
|
||||||
|
# made one yet: an M3 database has no position a user chose to name, and
|
||||||
|
# inventing one would be inventing the decision.
|
||||||
|
(80, "CREATE INDEX IF NOT EXISTS ix_checkpoints_adventure "
|
||||||
|
"ON checkpoints (adventure_id)"),
|
||||||
]
|
]
|
||||||
|
|
||||||
LATEST_VERSION = max((v for v, _ in MIGRATIONS), default=1)
|
LATEST_VERSION = max((v for v, _ in MIGRATIONS), default=1)
|
||||||
|
|||||||
@@ -228,6 +228,78 @@ class Branch(Base):
|
|||||||
# branch by its fork depth, which deleting a branch does not change.
|
# branch by its fork depth, which deleting a branch does not change.
|
||||||
name: Mapped[str | None] = mapped_column(String(80), nullable=True)
|
name: Mapped[str | None] = mapped_column(String(80), nullable=True)
|
||||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)
|
||||||
|
# M3: the disposition `DATA-MODEL.md` §5 gives a branch, stored as the fact
|
||||||
|
# that produced it. NULL means active. A value means a divergent write left
|
||||||
|
# this branch at `superseded_depth`, so its nodes past that depth are
|
||||||
|
# retained history that no active head is reading.
|
||||||
|
#
|
||||||
|
# Nothing reads these to decide behaviour. Redo follows the lineage, so a
|
||||||
|
# wrong value here cannot make the story wrong; they exist for the cleanup
|
||||||
|
# and discarded-history features that `STORY-BRANCH-SEMANTICS.md` §28-29
|
||||||
|
# leave to a later version. See `head.mark_superseded`.
|
||||||
|
superseded_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
||||||
|
superseded_depth: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Checkpoint(Base):
|
||||||
|
"""M4: a Save Point — a durable named pointer to a story position.
|
||||||
|
|
||||||
|
"Save Point" is what the user reads; `checkpoint` is what the code calls it
|
||||||
|
(`BROWSER-UX-SPEC.md` §23).
|
||||||
|
|
||||||
|
The row holds a name and a coordinate, and no story. `DATA-MODEL.md` §8
|
||||||
|
describes the pointer as naming a turn; the coordinate here is
|
||||||
|
`(branch_id, depth)`, which is what M3 made the head and what
|
||||||
|
`head.node_at` resolves. Restoring one is therefore head movement with a
|
||||||
|
bounds check rather than a restore system of its own — see ADR 012 and
|
||||||
|
`head.move_to_node`.
|
||||||
|
|
||||||
|
A coordinate rather than an action id, deliberately. One coordinate can
|
||||||
|
hold several attempts at a turn and exactly one of them is live, so a
|
||||||
|
retry replaces the row a Save Point would have pinned. "Turn 42 of this
|
||||||
|
line" survives a retry; "action 918" would point at a take the story no
|
||||||
|
longer tells.
|
||||||
|
|
||||||
|
`branch_id` is the branch the node itself sits on, not the branch that was
|
||||||
|
being read when the Save Point was made. Those differ whenever the head is
|
||||||
|
resting in a shared prefix, and the node's own branch is the one that still
|
||||||
|
names the position after the reader has moved elsewhere.
|
||||||
|
|
||||||
|
**Nothing removes a Save Point but the user.** They are not cleaned up for
|
||||||
|
going stale, for being behind the head, or for pointing into a future the
|
||||||
|
story has left (`STORY-BRANCH-SEMANTICS.md` §19).
|
||||||
|
|
||||||
|
That includes deleting a branch. `branch_id` carries `ON DELETE CASCADE` as
|
||||||
|
referential integrity — a Save Point must never point at a branch that is
|
||||||
|
gone — but the branch endpoint refuses to delete a branch any Save Point
|
||||||
|
names, so the cascade does not fire through the application
|
||||||
|
(`routers/adventures/branches.py`, `STORY-BRANCH-SEMANTICS.md` §19.1). The
|
||||||
|
user deletes the Save Point first, which deletes no story, and then the
|
||||||
|
branch. Deleting the whole campaign does cascade, and should: that is what
|
||||||
|
the user asked for.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__tablename__ = "checkpoints"
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
|
adventure_id: Mapped[int] = mapped_column(
|
||||||
|
ForeignKey("adventures.id", ondelete="CASCADE")
|
||||||
|
)
|
||||||
|
name: Mapped[str] = mapped_column(String(120), default="")
|
||||||
|
# `DATA-MODEL.md` §8's optional notes, and `BROWSER-UX-SPEC.md` §24's
|
||||||
|
# optional second field. Empty is the ordinary case.
|
||||||
|
note: Mapped[str] = mapped_column(Text, default="")
|
||||||
|
branch_id: Mapped[int] = mapped_column(
|
||||||
|
ForeignKey("branches.id", ondelete="CASCADE")
|
||||||
|
)
|
||||||
|
depth: Mapped[int] = mapped_column(Integer)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)
|
||||||
|
# Bumped by a rename, which is the only edit a Save Point allows. The
|
||||||
|
# coordinate is never rewritten: `STORY-BRANCH-SEMANTICS.md` §24 keeps a
|
||||||
|
# Save Point's meaning auditable by making "move it" delete-and-recreate.
|
||||||
|
updated_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime, default=utcnow, onupdate=utcnow
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Memory(Base):
|
class Memory(Base):
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ Read the modules in this order to follow a turn from end to end:
|
|||||||
turns playing a turn, and the lock that allows only one at a time
|
turns playing a turn, and the lock that allows only one at a time
|
||||||
takes retries and the attempts that collect at one coordinate
|
takes retries and the attempts that collect at one coordinate
|
||||||
branches where a story splits
|
branches where a story splits
|
||||||
|
checkpoints Save Points: durable names for positions the head can return to
|
||||||
|
|
||||||
What this package re-exports, and what it deliberately does not:
|
What this package re-exports, and what it deliberately does not:
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ from . import ( # noqa: F401
|
|||||||
turns,
|
turns,
|
||||||
takes,
|
takes,
|
||||||
branches,
|
branches,
|
||||||
|
checkpoints,
|
||||||
bundle_io,
|
bundle_io,
|
||||||
refresh,
|
refresh,
|
||||||
insights,
|
insights,
|
||||||
@@ -40,7 +42,7 @@ from . import ( # noqa: F401
|
|||||||
from ... import limits # noqa: F401 `adventures.limits` is patched by tests.
|
from ... import limits # noqa: F401 `adventures.limits` is patched by tests.
|
||||||
from .crud import SNIPPET_MAX, _snippet
|
from .crud import SNIPPET_MAX, _snippet
|
||||||
from .paging import ACTION_PAGE
|
from .paging import ACTION_PAGE
|
||||||
from .takes import retry_action, undo_turn
|
from .takes import redo_turn, retry_action, undo_turn
|
||||||
from .turns import world_delta_of
|
from .turns import world_delta_of
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
@@ -48,6 +50,7 @@ __all__ = [
|
|||||||
"SNIPPET_MAX",
|
"SNIPPET_MAX",
|
||||||
"_snippet",
|
"_snippet",
|
||||||
"limits",
|
"limits",
|
||||||
|
"redo_turn",
|
||||||
"retry_action",
|
"retry_action",
|
||||||
"router",
|
"router",
|
||||||
"undo_turn",
|
"undo_turn",
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ coordinate through `nodes.delete_turn`.
|
|||||||
from fastapi import Depends, HTTPException
|
from fastapi import Depends, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ... import attempts, models, schemas, tree
|
from ... import attempts, head, models, schemas, tree
|
||||||
from ...database import get_db
|
from ...database import get_db
|
||||||
|
|
||||||
from . import turns
|
from . import turns
|
||||||
@@ -41,6 +41,11 @@ def list_actions(
|
|||||||
],
|
],
|
||||||
total=total,
|
total=total,
|
||||||
has_more=has_more,
|
has_more=has_more,
|
||||||
|
# Every page carries them, not just the newest window: the client reads
|
||||||
|
# the flags off whichever page arrived last, and scrolling up must not
|
||||||
|
# be able to grey out a Redo that is still available (M3).
|
||||||
|
can_undo=head.can_undo(db, adventure),
|
||||||
|
can_redo=head.can_redo(db, adventure),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -55,6 +60,27 @@ def update_action(
|
|||||||
action = db.get(models.Action, action_id)
|
action = db.get(models.Action, action_id)
|
||||||
if action is None or action.adventure_id != adventure_id:
|
if action is None or action.adventure_id != adventure_id:
|
||||||
raise HTTPException(404, "Action not found")
|
raise HTTPException(404, "Action not found")
|
||||||
|
# An edit rewrites this row and re-evaluates nothing after it, which is what
|
||||||
|
# makes it a correction rather than a new continuation. That is safe while
|
||||||
|
# everything descending from the row is on screen, and unsafe the moment
|
||||||
|
# something descends from it that is not — an undone future, or a line a
|
||||||
|
# divergence left behind. The reader cannot see that story, so they cannot
|
||||||
|
# see what their correction has just contradicted (M3).
|
||||||
|
#
|
||||||
|
# Refusing is the whole of the fix, deliberately. Making the edit fork, so
|
||||||
|
# that the original text and its future stay whole, is
|
||||||
|
# `STORY-BRANCH-SEMANTICS.md` §14-15 — and §15 requires re-evaluating the
|
||||||
|
# state the edited prose implies, which is M5's extraction pass. Neither is
|
||||||
|
# started here. What is closed is the one case where the application could
|
||||||
|
# produce retained history that silently disagrees with itself.
|
||||||
|
if head.displaced_history_under(db, adventure, action):
|
||||||
|
raise HTTPException(
|
||||||
|
400,
|
||||||
|
"This turn has a later story that is not on screen — undone, or "
|
||||||
|
"left behind by a new continuation. Editing it here would change "
|
||||||
|
"the words that story was written from. Redo to bring it back "
|
||||||
|
"first, or play the turn again to start a new line from here.",
|
||||||
|
)
|
||||||
# One row holds one text. Nothing mirrors it now, so nothing else has to be
|
# One row holds one text. Nothing mirrors it now, so nothing else has to be
|
||||||
# updated. The edit used to have to be written into the live variant entry
|
# updated. The edit used to have to be written into the live variant entry
|
||||||
# as well, or paging away and back reverted it.
|
# as well, or paging away and back reverted it.
|
||||||
@@ -81,6 +107,7 @@ def delete_action(
|
|||||||
# This works like undo. The turn is deleted with all of its attempts,
|
# This works like undo. The turn is deleted with all of its attempts,
|
||||||
# and whatever it produced is withdrawn. The marks are depths, and a
|
# and whatever it produced is withdrawn. The marks are depths, and a
|
||||||
# depth does not move when an action before it is deleted.
|
# depth does not move when an action before it is deleted.
|
||||||
|
was_at = adventure.head_depth
|
||||||
delete_turn(db, adventure, action)
|
delete_turn(db, adventure, action)
|
||||||
db.flush()
|
db.flush()
|
||||||
db.expire(adventure, ["actions"])
|
db.expire(adventure, ["actions"])
|
||||||
@@ -88,6 +115,13 @@ def delete_action(
|
|||||||
# middle leaves a gap in the depths, which is intended. See
|
# middle leaves a gap in the depths, which is intended. See
|
||||||
# `_backfill_tree`.
|
# `_backfill_tree`.
|
||||||
tree.refresh_head(db, adventure)
|
tree.refresh_head(db, adventure)
|
||||||
|
# `refresh_head` recomputes the tip, which since M3 is not the head. A
|
||||||
|
# story sitting behind its retained tip must not be dragged forward to
|
||||||
|
# the tip by an unrelated delete — that would silently Redo it. Keep the
|
||||||
|
# head where the reader left it, unless the delete took the ground out
|
||||||
|
# from under it, in which case the new tip is as far as it can stay.
|
||||||
|
if was_at < adventure.head_depth:
|
||||||
|
adventure.head_depth = was_at
|
||||||
# The script state and the world state belong to the adventure, not to
|
# The script state and the world state belong to the adventure, not to
|
||||||
# the node, so deleting the node does not take back what it did to
|
# the node, so deleting the node does not take back what it did to
|
||||||
# them. Put them back to what the story now ends with, which is the
|
# them. Put them back to what the story now ends with, which is the
|
||||||
|
|||||||
@@ -56,6 +56,19 @@ def list_branches(
|
|||||||
.group_by(models.Action.branch_id)
|
.group_by(models.Action.branch_id)
|
||||||
.all()
|
.all()
|
||||||
}
|
}
|
||||||
|
# M4 closeout: how many Save Points name a position on each line. Deleting a
|
||||||
|
# branch deletes them along with its story, and the panel has to be able to
|
||||||
|
# say so before the button is pressed (review §R B-2). One grouped query for
|
||||||
|
# the whole tree, like the one above it — never one per branch.
|
||||||
|
save_points = {
|
||||||
|
branch_id: count
|
||||||
|
for branch_id, count in db.query(
|
||||||
|
models.Checkpoint.branch_id, func.count(models.Checkpoint.id)
|
||||||
|
)
|
||||||
|
.filter(models.Checkpoint.adventure_id == adventure.id)
|
||||||
|
.group_by(models.Checkpoint.branch_id)
|
||||||
|
.all()
|
||||||
|
}
|
||||||
out = []
|
out = []
|
||||||
for branch in branches:
|
for branch in branches:
|
||||||
count, tip = owned.get(branch.id, (0, None))
|
count, tip = owned.get(branch.id, (0, None))
|
||||||
@@ -70,6 +83,7 @@ def list_branches(
|
|||||||
branch.fork_depth if branch.fork_depth is not None else tree.NO_DEPTH
|
branch.fork_depth if branch.fork_depth is not None else tree.NO_DEPTH
|
||||||
),
|
),
|
||||||
own_actions=count,
|
own_actions=count,
|
||||||
|
save_points=save_points.get(branch.id, 0),
|
||||||
is_head=(branch.id == adventure.head_branch_id),
|
is_head=(branch.id == adventure.head_branch_id),
|
||||||
name=branch.name,
|
name=branch.name,
|
||||||
created_at=branch.created_at,
|
created_at=branch.created_at,
|
||||||
@@ -151,15 +165,31 @@ def delete_branch(
|
|||||||
heavily retried adventure from growing without bound. That is why it ships
|
heavily retried adventure from growing without bound. That is why it ships
|
||||||
with the view that first lets anyone create a fork rather than after it.
|
with the view that first lets anyone create a fork rather than after it.
|
||||||
|
|
||||||
Two kinds of branch cannot be deleted. The root cannot, because it holds the
|
Three kinds of branch cannot be deleted. The root cannot, because it holds
|
||||||
turns every other branch borrows, so deleting it deletes the whole story. The
|
the turns every other branch borrows, so deleting it deletes the whole story.
|
||||||
branch currently being read cannot, and neither can any branch it was forked
|
The branch currently being read cannot, and neither can any branch it was
|
||||||
from, because the cascade would remove the head under the player and leave
|
forked from, because the cascade would remove the head under the player and
|
||||||
`head_branch_id` dangling. Switch branches first.
|
leave `head_branch_id` dangling. Switch branches first.
|
||||||
|
|
||||||
|
The third is M4's: **a branch a Save Point names cannot be deleted while that
|
||||||
|
Save Point exists.** `STORY-BRANCH-SEMANTICS.md` §19 says a named checkpoint
|
||||||
|
remains until explicitly deleted, and §28 says a future cleanup feature must
|
||||||
|
retain paths referenced by checkpoints. A cascade that removed Save Points
|
||||||
|
along with a branch would break both, and would break them silently: the
|
||||||
|
story the user asked to delete is the visible thing, and the named moments
|
||||||
|
would go without ever being named in the request. So the deletion is refused,
|
||||||
|
the Save Points are listed, and the user decides — delete the Save Point
|
||||||
|
first, then the branch. Deleting a Save Point still deletes no story (§25),
|
||||||
|
so the recovery costs nothing but a click.
|
||||||
|
|
||||||
|
The check covers the whole doomed subtree, not just this branch, because
|
||||||
|
deleting a branch takes everything forked from it.
|
||||||
|
|
||||||
Nodes and memories are deleted by `ON DELETE CASCADE`, and descendants by the
|
Nodes and memories are deleted by `ON DELETE CASCADE`, and descendants by the
|
||||||
cascade on `branches.parent_branch_id`, so the delete is a single statement
|
cascade on `branches.parent_branch_id`, so the delete is a single statement
|
||||||
however deep the subtree is.
|
however deep the subtree is. `checkpoints.branch_id` also carries a cascade,
|
||||||
|
as referential integrity — a Save Point must never point at a branch that is
|
||||||
|
gone — but the guard above means it does not fire through this endpoint.
|
||||||
"""
|
"""
|
||||||
branch = get_branch_or_404(adventure, branch_id, db)
|
branch = get_branch_or_404(adventure, branch_id, db)
|
||||||
if branch.parent_branch_id is None:
|
if branch.parent_branch_id is None:
|
||||||
@@ -178,6 +208,11 @@ def delete_branch(
|
|||||||
400, "You are reading this branch, or one forked from it. Switch to "
|
400, "You are reading this branch, or one forked from it. Switch to "
|
||||||
"another branch first.",
|
"another branch first.",
|
||||||
)
|
)
|
||||||
|
# Refused before the lock is taken: this is a decision about the request, not
|
||||||
|
# a race with a turn.
|
||||||
|
protecting = _save_points_protecting(db, adventure, branch)
|
||||||
|
if protecting:
|
||||||
|
raise HTTPException(409, _protected_message(protecting))
|
||||||
turns.acquire_turn_lock(adventure_id)
|
turns.acquire_turn_lock(adventure_id)
|
||||||
try:
|
try:
|
||||||
# Collect the subtree before the delete, because afterwards there is no
|
# Collect the subtree before the delete, because afterwards there is no
|
||||||
@@ -199,6 +234,55 @@ def delete_branch(
|
|||||||
finally:
|
finally:
|
||||||
turns._active_turns.discard(adventure_id)
|
turns._active_turns.discard(adventure_id)
|
||||||
|
|
||||||
|
# How many Save Point names to spell out before the message starts summarising.
|
||||||
|
# Enough to be actionable, few enough to stay a sentence.
|
||||||
|
NAMED_IN_REFUSAL = 3
|
||||||
|
|
||||||
|
|
||||||
|
def _save_points_protecting(
|
||||||
|
db: Session, adventure: models.Adventure, branch: models.Branch
|
||||||
|
) -> list[models.Checkpoint]:
|
||||||
|
"""Returns the Save Points that deleting `branch` would destroy.
|
||||||
|
|
||||||
|
The whole subtree, because deleting a branch takes everything forked from
|
||||||
|
it, and a check that looked only at this branch would let a Save Point on a
|
||||||
|
child be deleted without a word.
|
||||||
|
"""
|
||||||
|
doomed = _branch_subtree(db, adventure, branch)
|
||||||
|
return (
|
||||||
|
db.query(models.Checkpoint)
|
||||||
|
.filter(
|
||||||
|
models.Checkpoint.adventure_id == adventure.id,
|
||||||
|
models.Checkpoint.branch_id.in_(doomed),
|
||||||
|
)
|
||||||
|
.order_by(models.Checkpoint.created_at, models.Checkpoint.id)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _protected_message(protecting: list[models.Checkpoint]) -> str:
|
||||||
|
"""Says which Save Points stand in the way, and what to do about it.
|
||||||
|
|
||||||
|
Named rather than counted, because "2 Save Points" leaves the user hunting
|
||||||
|
for which ones. A long list is truncated so the message stays readable; the
|
||||||
|
Save Points panel shows the rest.
|
||||||
|
"""
|
||||||
|
names = [f"“{c.name}”" for c in protecting[:NAMED_IN_REFUSAL]]
|
||||||
|
listed = ", ".join(names)
|
||||||
|
extra = len(protecting) - len(names)
|
||||||
|
if extra > 0:
|
||||||
|
listed += f" and {extra} more"
|
||||||
|
subject = "a Save Point" if len(protecting) == 1 else "Save Points"
|
||||||
|
return (
|
||||||
|
f"This branch, or a branch forked from it, is where {subject} "
|
||||||
|
f"{listed} {'is' if len(protecting) == 1 else 'are'} saved. Delete "
|
||||||
|
f"{'that Save Point' if len(protecting) == 1 else 'those Save Points'} "
|
||||||
|
f"first if you no longer need "
|
||||||
|
f"{'it' if len(protecting) == 1 else 'them'}, then delete the branch. "
|
||||||
|
f"Deleting a Save Point does not delete any story."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _branch_subtree(
|
def _branch_subtree(
|
||||||
db: Session, adventure: models.Adventure, root: models.Branch
|
db: Session, adventure: models.Adventure, root: models.Branch
|
||||||
) -> set[int]:
|
) -> set[int]:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ only check ownership and hand the work over.
|
|||||||
from fastapi import Body, Depends, Request
|
from fastapi import Body, Depends, Request
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ... import bundle, limits, models, schemas
|
from ... import bundle, head, limits, models, schemas
|
||||||
from ...database import get_db
|
from ...database import get_db
|
||||||
|
|
||||||
from .deps import CurrentUser, current_adventure, router
|
from .deps import CurrentUser, current_adventure, router
|
||||||
@@ -62,7 +62,14 @@ def import_adventure(
|
|||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(adventure)
|
db.refresh(adventure)
|
||||||
|
# A campaign exported while undone imports undone (M3), so the history
|
||||||
|
# controls have to be right on the response that opens it — otherwise the
|
||||||
|
# first thing the reader sees about a story with a retained future is a
|
||||||
|
# greyed-out Redo.
|
||||||
|
out = schemas.AdventureOut.model_validate(adventure)
|
||||||
|
out.can_undo = head.can_undo(db, adventure)
|
||||||
|
out.can_redo = head.can_redo(db, adventure)
|
||||||
# This is not a funnel step. A returning player imports a bundle, so it
|
# This is not a funnel step. A returning player imports a bundle, so it
|
||||||
# says nothing about how far a first-time visitor got. It is counted anyway,
|
# says nothing about how far a first-time visitor got. It is counted anyway,
|
||||||
# because it is the clearest evidence that anyone uses the export format.
|
# because it is the clearest evidence that anyone uses the export format.
|
||||||
return adventure
|
return out
|
||||||
|
|||||||
@@ -0,0 +1,351 @@
|
|||||||
|
"""M4: Save Points — create, list, rename, delete, and restore.
|
||||||
|
|
||||||
|
A Save Point is a durable named pointer to a story position and nothing else.
|
||||||
|
It stores a coordinate, never a copy of any story, and restoring one moves the
|
||||||
|
active head to that coordinate. That is the whole design, and it is what
|
||||||
|
`BUILD-MILESTONES.md`'s note on M4 and ADR 012 ask for: M3 made the head a
|
||||||
|
stored `(branch, depth)` and made arriving at one a row lookup plus a state
|
||||||
|
restore, so a Save Point needs no restore machinery of its own.
|
||||||
|
|
||||||
|
What is deliberately absent from this module, because a second copy of any of it
|
||||||
|
would be the failure M4 is warned about:
|
||||||
|
|
||||||
|
* no head fields are assigned here — `head.move_to_node` moves the head, and
|
||||||
|
`head.move_to` under it restores the state, exactly as Undo and Redo do;
|
||||||
|
* nothing reconstructs state, prunes a memory, copies a turn, or deletes one;
|
||||||
|
* nothing forks. Restore is not a decision to abandon anything, so it creates no
|
||||||
|
branch. The first write below the restored head forks, through the same
|
||||||
|
`fork_if_behind_head` every other write goes through, and the displaced future
|
||||||
|
stays retained (`STORY-BRANCH-SEMANTICS.md` §20).
|
||||||
|
|
||||||
|
The user-facing word is "Save Point" and the internal one is `checkpoint`
|
||||||
|
(`BROWSER-UX-SPEC.md` §23). Error strings here are read by a player, so they say
|
||||||
|
Save Point.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from fastapi import Depends, HTTPException
|
||||||
|
from sqlalchemy import and_, or_
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from ... import head, models, schemas
|
||||||
|
from ...context import lineage
|
||||||
|
from ...database import get_db
|
||||||
|
|
||||||
|
from . import turns
|
||||||
|
from .deps import current_adventure, router
|
||||||
|
from .paging import current_window
|
||||||
|
|
||||||
|
|
||||||
|
def _node_at(
|
||||||
|
db: Session, adventure: models.Adventure, branch_id: int, depth: int
|
||||||
|
) -> models.Action | None:
|
||||||
|
"""Returns the live turn a Save Point's coordinate names, or None.
|
||||||
|
|
||||||
|
The lookup is by coordinate and is not scoped to any path. That is the
|
||||||
|
point of it: a Save Point outlives the reader moving away, so the question
|
||||||
|
it has to answer is "is this position still in this campaign's retained
|
||||||
|
history", not "is it on the story being read now". Whether it is on the
|
||||||
|
current path is a separate question, and `head.move_to_node` is what acts on
|
||||||
|
the answer.
|
||||||
|
|
||||||
|
`live` is what makes the coordinate follow a retry. One coordinate can hold
|
||||||
|
several attempts at a turn, and a Save Point names the turn rather than the
|
||||||
|
attempt, so it lands on whichever take the story currently tells.
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
models.Action.branch_id == branch_id,
|
||||||
|
models.Action.depth == depth,
|
||||||
|
models.Action.live.is_(True),
|
||||||
|
)
|
||||||
|
.order_by(models.Action.id)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class _Coordinate:
|
||||||
|
"""The shape `lineage.Path` reads, without loading a story row.
|
||||||
|
|
||||||
|
`Path.contains` asks three things of a node: its branch, its depth, and
|
||||||
|
whether it is live. A coordinate already known to resolve has all three, so
|
||||||
|
the membership question can be put to the coordinate itself. That keeps the
|
||||||
|
single implementation of "is this on the path being read" in `lineage`,
|
||||||
|
where M3 put it, while costing no query and no prose.
|
||||||
|
"""
|
||||||
|
|
||||||
|
branch_id: int
|
||||||
|
depth: int
|
||||||
|
live: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
def _live_coordinates(
|
||||||
|
db: Session, adventure: models.Adventure, checkpoints: list[models.Checkpoint]
|
||||||
|
) -> set[tuple[int, int]]:
|
||||||
|
"""Returns which of these Save Points' coordinates still name a live turn.
|
||||||
|
|
||||||
|
One query for the whole list, selecting two integer columns.
|
||||||
|
|
||||||
|
This replaces a resolution per Save Point (M4 review §R B-1), which cost one
|
||||||
|
query each and loaded whole `Action` entities — narration included — to
|
||||||
|
answer a question that is only ever "does a row exist here". `paging.py`
|
||||||
|
states the rule this now follows: a bulk read names the columns it needs, so
|
||||||
|
a new column costs nothing until someone adds it to the list.
|
||||||
|
|
||||||
|
The clause is an OR of exact `(branch, depth)` pairs rather than
|
||||||
|
`branch IN (…) AND depth IN (…)`, which would match the cross product and
|
||||||
|
report a Save Point as resolved because *some other* Save Point's depth
|
||||||
|
exists on *this* one's branch.
|
||||||
|
"""
|
||||||
|
coordinates = {(c.branch_id, c.depth) for c in checkpoints}
|
||||||
|
if not coordinates:
|
||||||
|
return set()
|
||||||
|
rows = (
|
||||||
|
db.query(models.Action.branch_id, models.Action.depth)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adventure.id,
|
||||||
|
models.Action.live.is_(True),
|
||||||
|
or_(*[
|
||||||
|
and_(models.Action.branch_id == branch, models.Action.depth == depth)
|
||||||
|
for branch, depth in coordinates
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
return {(branch, depth) for branch, depth in rows}
|
||||||
|
|
||||||
|
|
||||||
|
def _render_all(
|
||||||
|
db: Session, adventure: models.Adventure, checkpoints: list[models.Checkpoint]
|
||||||
|
) -> list[schemas.CheckpointOut]:
|
||||||
|
"""Reads Save Points out with the three facts the panel needs about them.
|
||||||
|
|
||||||
|
Bounded work whatever the length of the list: one query for the coordinates
|
||||||
|
and one lineage for the campaign, both computed before the loop. Rendering
|
||||||
|
one Save Point and rendering fifty differ in Python, not in round trips.
|
||||||
|
"""
|
||||||
|
live = _live_coordinates(db, adventure, checkpoints)
|
||||||
|
# The path is a property of the campaign, not of any Save Point, so it is
|
||||||
|
# read once. Reading it per row was the other half of the N+1.
|
||||||
|
path = lineage.path_of(db, adventure).uncapped()
|
||||||
|
out = []
|
||||||
|
for checkpoint in checkpoints:
|
||||||
|
coordinate = (checkpoint.branch_id, checkpoint.depth)
|
||||||
|
resolved = coordinate in live
|
||||||
|
rendered = schemas.CheckpointOut.model_validate(checkpoint)
|
||||||
|
# The same `depth + 1` the branch list counts with, so a moment number
|
||||||
|
# means the same thing in both places.
|
||||||
|
rendered.turn = checkpoint.depth + 1
|
||||||
|
rendered.resolved = resolved
|
||||||
|
rendered.on_path = resolved and path.contains(
|
||||||
|
_Coordinate(checkpoint.branch_id, checkpoint.depth)
|
||||||
|
)
|
||||||
|
out.append(rendered)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _rendered(
|
||||||
|
db: Session, adventure: models.Adventure, checkpoint: models.Checkpoint
|
||||||
|
) -> schemas.CheckpointOut:
|
||||||
|
"""Reads one Save Point out, through the same path the list uses."""
|
||||||
|
return _render_all(db, adventure, [checkpoint])[0]
|
||||||
|
|
||||||
|
|
||||||
|
def _get_or_404(
|
||||||
|
db: Session, adventure: models.Adventure, checkpoint_id: int
|
||||||
|
) -> models.Checkpoint:
|
||||||
|
"""Resolves a Save Point id, refusing one that belongs to another campaign.
|
||||||
|
|
||||||
|
The ownership check is the reason this is a function rather than a `db.get`
|
||||||
|
at each call site. A Save Point names a position in one campaign's history,
|
||||||
|
and a coordinate from another campaign would name a different story's turn —
|
||||||
|
or, worse, resolve against this one by arithmetic coincidence. So the id is
|
||||||
|
matched against this adventure, and a Save Point belonging to another is a
|
||||||
|
404 rather than a restore of the wrong story.
|
||||||
|
"""
|
||||||
|
checkpoint = db.get(models.Checkpoint, checkpoint_id)
|
||||||
|
if checkpoint is None or checkpoint.adventure_id != adventure.id:
|
||||||
|
raise HTTPException(404, "Save Point not found")
|
||||||
|
return checkpoint
|
||||||
|
|
||||||
|
|
||||||
|
def _clean_name(raw: str) -> str:
|
||||||
|
"""Returns the trimmed name, refusing one that is blank once trimmed."""
|
||||||
|
name = (raw or "").strip()
|
||||||
|
if not name:
|
||||||
|
raise HTTPException(400, "A Save Point needs a name.")
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{adventure_id}/checkpoints", response_model=list[schemas.CheckpointOut])
|
||||||
|
def list_checkpoints(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
adventure: models.Adventure = Depends(current_adventure),
|
||||||
|
):
|
||||||
|
"""Returns the campaign's Save Points, newest first.
|
||||||
|
|
||||||
|
Newest first rather than in story order, because story order is not
|
||||||
|
something this list can honestly claim. Depths are positions along a path,
|
||||||
|
and two Save Points on lines that parted company are not comparable by depth
|
||||||
|
at all — ordering by it would draw a sequence that no reading of the story
|
||||||
|
passes through. When they were made is a fact about all of them.
|
||||||
|
"""
|
||||||
|
rows = (
|
||||||
|
db.query(models.Checkpoint)
|
||||||
|
.filter(models.Checkpoint.adventure_id == adventure.id)
|
||||||
|
.order_by(models.Checkpoint.created_at.desc(), models.Checkpoint.id.desc())
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
return _render_all(db, adventure, rows)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/{adventure_id}/checkpoints",
|
||||||
|
response_model=schemas.CheckpointOut,
|
||||||
|
status_code=201,
|
||||||
|
)
|
||||||
|
def create_checkpoint(
|
||||||
|
adventure_id: int,
|
||||||
|
payload: schemas.CheckpointCreate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
adventure: models.Adventure = Depends(current_adventure),
|
||||||
|
):
|
||||||
|
"""Names the position the story is currently being read at.
|
||||||
|
|
||||||
|
The active head, not the retained tip. Creating a Save Point after two Undos
|
||||||
|
saves the undone position, because that is where the reader is and the
|
||||||
|
position they are looking at is the one they mean. The distinction only
|
||||||
|
exists at all because M3 stopped Undo from deleting.
|
||||||
|
|
||||||
|
The node at the head is resolved before the row is written, and its own
|
||||||
|
branch is what gets stored — which is not always the branch being read. A
|
||||||
|
head resting in a shared prefix sits on an ancestor's node, and the
|
||||||
|
ancestor is the branch that still names that position after the reader has
|
||||||
|
forked away from it.
|
||||||
|
|
||||||
|
**Held under the campaign's turn lock** (M4 closeout, review §S C-5). "Save
|
||||||
|
where I am" has to name one committed position, and the head is exactly what
|
||||||
|
a turn in flight is about to move. Without the lock this endpoint could read
|
||||||
|
`head_depth` while a turn was mid-commit and store a coordinate for a
|
||||||
|
position the story had already left — a Save Point silently naming the wrong
|
||||||
|
moment, which no later operation could detect. It is the same lock Undo,
|
||||||
|
Redo and Restore take, for the same reason, and not a new mechanism.
|
||||||
|
|
||||||
|
Rename and Delete deliberately do **not** take it: neither reads nor moves a
|
||||||
|
story position, so there is nothing for a turn in flight to race them over.
|
||||||
|
"""
|
||||||
|
name = _clean_name(payload.name)
|
||||||
|
turns.acquire_turn_lock(adventure_id)
|
||||||
|
try:
|
||||||
|
node = head.node_at(db, adventure, adventure.head_depth)
|
||||||
|
if node is None:
|
||||||
|
raise HTTPException(400, "There is no turn here to save yet.")
|
||||||
|
checkpoint = models.Checkpoint(
|
||||||
|
adventure_id=adventure.id,
|
||||||
|
name=name,
|
||||||
|
note=payload.note or "",
|
||||||
|
branch_id=node.branch_id,
|
||||||
|
depth=node.depth,
|
||||||
|
)
|
||||||
|
db.add(checkpoint)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(checkpoint)
|
||||||
|
return _rendered(db, adventure, checkpoint)
|
||||||
|
finally:
|
||||||
|
turns._active_turns.discard(adventure_id)
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch(
|
||||||
|
"/{adventure_id}/checkpoints/{checkpoint_id}",
|
||||||
|
response_model=schemas.CheckpointOut,
|
||||||
|
)
|
||||||
|
def rename_checkpoint(
|
||||||
|
checkpoint_id: int,
|
||||||
|
payload: schemas.CheckpointRename,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
adventure: models.Adventure = Depends(current_adventure),
|
||||||
|
):
|
||||||
|
"""Changes a Save Point's label. Nothing else about it moves.
|
||||||
|
|
||||||
|
Not the coordinate, not the head, not a row of story. A Save Point that has
|
||||||
|
been renamed restores to exactly the position it did before, which is
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` §23.
|
||||||
|
"""
|
||||||
|
checkpoint = _get_or_404(db, adventure, checkpoint_id)
|
||||||
|
if payload.name is not None:
|
||||||
|
checkpoint.name = _clean_name(payload.name)
|
||||||
|
if payload.note is not None:
|
||||||
|
checkpoint.note = payload.note
|
||||||
|
db.commit()
|
||||||
|
db.refresh(checkpoint)
|
||||||
|
return _rendered(db, adventure, checkpoint)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{adventure_id}/checkpoints/{checkpoint_id}", status_code=204)
|
||||||
|
def delete_checkpoint(
|
||||||
|
checkpoint_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
adventure: models.Adventure = Depends(current_adventure),
|
||||||
|
):
|
||||||
|
"""Removes the named pointer, and only the pointer.
|
||||||
|
|
||||||
|
The turn it named stays, its branch stays, the future past it stays, and the
|
||||||
|
head does not move. This endpoint deletes one row of the `checkpoints`
|
||||||
|
table. `STORY-BRANCH-SEMANTICS.md` §25.
|
||||||
|
"""
|
||||||
|
checkpoint = _get_or_404(db, adventure, checkpoint_id)
|
||||||
|
db.delete(checkpoint)
|
||||||
|
db.commit()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/{adventure_id}/checkpoints/{checkpoint_id}/restore",
|
||||||
|
response_model=schemas.ActionPage,
|
||||||
|
)
|
||||||
|
def restore_checkpoint(
|
||||||
|
adventure_id: int,
|
||||||
|
checkpoint_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
adventure: models.Adventure = Depends(current_adventure),
|
||||||
|
):
|
||||||
|
"""Returns the story to a Save Point, deleting nothing.
|
||||||
|
|
||||||
|
Four steps, and the last one is not this module's code: resolve the
|
||||||
|
coordinate, refuse it if it no longer names a live turn, hand it to
|
||||||
|
`head.move_to_node`, and answer with the window the head now caps. The
|
||||||
|
transcript, the world state, the assembled context and which memories can be
|
||||||
|
retrieved all move together, because all four already read through the one
|
||||||
|
path object the head caps — the same reason Undo needed no memory pruning.
|
||||||
|
|
||||||
|
The turns past the restored position are retained, exactly as they are after
|
||||||
|
an Undo, and ordinary Redo can still walk forward into them until the user
|
||||||
|
writes something different. Restore does not fork; the first write below the
|
||||||
|
head does.
|
||||||
|
|
||||||
|
A coordinate that no longer resolves is refused rather than approximated.
|
||||||
|
Moving the head to the nearest surviving turn would be the one outcome worse
|
||||||
|
than doing nothing: a Save Point that silently means somewhere else.
|
||||||
|
"""
|
||||||
|
checkpoint = _get_or_404(db, adventure, checkpoint_id)
|
||||||
|
turns.acquire_turn_lock(adventure_id)
|
||||||
|
try:
|
||||||
|
node = _node_at(db, adventure, checkpoint.branch_id, checkpoint.depth)
|
||||||
|
if node is None:
|
||||||
|
raise HTTPException(
|
||||||
|
409,
|
||||||
|
"That Save Point's position is no longer part of this story.",
|
||||||
|
)
|
||||||
|
head.move_to_node(db, adventure, node)
|
||||||
|
adventure.updated_at = models.utcnow()
|
||||||
|
db.commit()
|
||||||
|
db.refresh(adventure)
|
||||||
|
# A window, not the whole story, for the reason Undo gives: the client
|
||||||
|
# replaces its transcript with this, and the transcript is a window.
|
||||||
|
return current_window(db, adventure)
|
||||||
|
finally:
|
||||||
|
turns._active_turns.discard(adventure_id)
|
||||||
@@ -9,7 +9,9 @@ from sqlalchemy import func
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy.orm.attributes import set_committed_value
|
from sqlalchemy.orm.attributes import set_committed_value
|
||||||
|
|
||||||
from ... import attempts, images, limits, memorybank, models, schemas, tree, worldstate
|
from ... import (
|
||||||
|
attempts, head, images, limits, memorybank, models, schemas, tree, worldstate,
|
||||||
|
)
|
||||||
from ...database import get_db
|
from ...database import get_db
|
||||||
|
|
||||||
from .deps import CurrentUser, current_adventure, router
|
from .deps import CurrentUser, current_adventure, router
|
||||||
@@ -239,6 +241,11 @@ def get_adventure(
|
|||||||
set_committed_value(adventure, "actions", actions)
|
set_committed_value(adventure, "actions", actions)
|
||||||
out = schemas.AdventureOut.model_validate(adventure)
|
out = schemas.AdventureOut.model_validate(adventure)
|
||||||
out.action_count = total
|
out.action_count = total
|
||||||
|
# M3. Opening a story has to render its history controls correctly, and a
|
||||||
|
# campaign whose head sits behind the retained tip — undone and then closed —
|
||||||
|
# must come back with Redo available.
|
||||||
|
out.can_undo = head.can_undo(db, adventure)
|
||||||
|
out.can_redo = head.can_redo(db, adventure)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ module in the package can import them.
|
|||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from sqlalchemy.orm import Session, undefer
|
from sqlalchemy.orm import Session, undefer
|
||||||
|
|
||||||
from ... import attempts, memorybank, models, tree
|
from ... import attempts, head, memorybank, models, tree
|
||||||
from ...context import cursors
|
from ...context import cursors
|
||||||
from ...context import lineage
|
from ...context import lineage
|
||||||
|
|
||||||
@@ -125,6 +125,10 @@ def stand_on(
|
|||||||
newest is not None
|
newest is not None
|
||||||
and newest.branch_id == action.branch_id
|
and newest.branch_id == action.branch_id
|
||||||
and newest.depth == action.depth
|
and newest.depth == action.depth
|
||||||
|
# A turn the head rests on is still not a leaf while a retained future
|
||||||
|
# descends from it. `last_action` reads the capped path and cannot see
|
||||||
|
# that future, so switching in place here would strand it (M3).
|
||||||
|
and not head.behind_tip(db, adventure)
|
||||||
)
|
)
|
||||||
if at_the_tip:
|
if at_the_tip:
|
||||||
# The story at this coordinate is about to change, so withdraw whatever
|
# The story at this coordinate is about to change, so withdraw whatever
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ columns and apply the same numbering, so both live here.
|
|||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
from sqlalchemy.orm import Session, load_only
|
from sqlalchemy.orm import Session, load_only
|
||||||
|
|
||||||
from ... import models, schemas
|
from ... import head, models, schemas
|
||||||
from ...context import lineage
|
from ...context import lineage
|
||||||
|
|
||||||
|
|
||||||
@@ -165,4 +165,6 @@ def current_window(db: Session, adventure: models.Adventure) -> schemas.ActionPa
|
|||||||
],
|
],
|
||||||
total=total,
|
total=total,
|
||||||
has_more=has_more,
|
has_more=has_more,
|
||||||
|
can_undo=head.can_undo(db, adventure),
|
||||||
|
can_redo=head.can_redo(db, adventure),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from fastapi import Depends, HTTPException, Request
|
|||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ... import attempts, limits, memorybank, models, schemas, tree
|
from ... import attempts, head, limits, memorybank, models, schemas, tree
|
||||||
from ...context import cursors
|
from ...context import cursors
|
||||||
from ...context import lineage
|
from ...context import lineage
|
||||||
from ...database import get_db
|
from ...database import get_db
|
||||||
@@ -19,8 +19,8 @@ from ...sse import SSE_HEADERS
|
|||||||
|
|
||||||
from . import turns
|
from . import turns
|
||||||
from .deps import CurrentUser, current_adventure, router
|
from .deps import CurrentUser, current_adventure, router
|
||||||
from .nodes import delete_turn, last_action, stand_on
|
from .nodes import last_action, stand_on
|
||||||
from .paging import action_window, annotate_takes, current_window
|
from .paging import current_window
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{adventure_id}/retry")
|
@router.post("/{adventure_id}/retry")
|
||||||
@@ -33,24 +33,44 @@ def retry_action(
|
|||||||
):
|
):
|
||||||
"""Regenerates the last AI action and keeps the discarded attempt.
|
"""Regenerates the last AI action and keeps the discarded attempt.
|
||||||
|
|
||||||
The attempt on screen stays as it was written. The shared script state and
|
The attempt on screen stays as it was written. The world state rolls back to
|
||||||
world state roll back to what the node before it left behind, and the new
|
what the node before it left behind, and the new attempt is stored as a
|
||||||
attempt is stored as a sibling at the same coordinate. No text the AI wrote
|
sibling at the same coordinate. No text the AI wrote is rewritten or deleted.
|
||||||
is rewritten or deleted.
|
|
||||||
|
M3 added the one case that cannot be a sibling. Retrying the turn the head
|
||||||
|
rests on while a retained future still descends from it would leave that
|
||||||
|
future hanging off a take that is no longer live — the story after it was
|
||||||
|
written to continue the old text. So a retry from behind the tip takes a
|
||||||
|
branch instead, exactly as `add_take` does for a turn the story has moved
|
||||||
|
past. It is the same operation reached from a different button.
|
||||||
"""
|
"""
|
||||||
turns.acquire_turn_lock(adventure_id)
|
turns.acquire_turn_lock(adventure_id)
|
||||||
last_ai = None
|
last_ai = None
|
||||||
try:
|
try:
|
||||||
newest = last_action(adventure, db)
|
newest = last_action(adventure, db)
|
||||||
if newest is not None and newest.type == "ai":
|
if newest is not None and newest.type == "ai":
|
||||||
last_ai = newest
|
# Read this before anything moves, and note it is *not*
|
||||||
|
# `fork_if_behind_head`: this fork leaves the path just in front of
|
||||||
|
# the turn being retried rather than at the head, so the new take
|
||||||
|
# lands at the same depth under the same parent.
|
||||||
|
diverging = head.behind_tip(db, adventure)
|
||||||
|
if diverging:
|
||||||
|
departed = lineage.branch_of(db, adventure)
|
||||||
|
tree.branch_at(db, adventure, (newest.depth or 0) - 1)
|
||||||
|
if departed is not None:
|
||||||
|
head.mark_superseded(departed, (newest.depth or 0) - 1)
|
||||||
|
else:
|
||||||
|
# Only a sibling attempt names the node it replaces. A branched
|
||||||
|
# take is a fresh node at the same coordinate, so `generate_turn`
|
||||||
|
# places it through the tree rather than through `add_attempt`.
|
||||||
|
last_ai = newest
|
||||||
# Roll the state back to before this AI turn's hooks ran, so that
|
# Roll the state back to before this AI turn's hooks ran, so that
|
||||||
# regenerating starts from a clean state rather than applying output
|
# regenerating starts from a clean state rather than applying output
|
||||||
# mutations on top of the attempt being replaced. If the preceding
|
# mutations on top of the attempt being replaced. If the preceding
|
||||||
# node has no snapshot, which happens for a pre-SP4 row that the
|
# node has no snapshot, which happens for a pre-SP4 row that the
|
||||||
# migration could not derive one for, this call does nothing and
|
# migration could not derive one for, this call does nothing and
|
||||||
# leaves the state as it is.
|
# leaves the state as it is.
|
||||||
attempts.roll_back_before(db, adventure, last_ai)
|
attempts.roll_back_before(db, adventure, newest)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(adventure)
|
db.refresh(adventure)
|
||||||
except BaseException:
|
except BaseException:
|
||||||
@@ -137,6 +157,17 @@ def select_variant(
|
|||||||
"Only the latest message can be switched — the story has already "
|
"Only the latest message can be switched — the story has already "
|
||||||
"continued from this one.",
|
"continued from this one.",
|
||||||
)
|
)
|
||||||
|
if head.behind_tip(db, adventure):
|
||||||
|
# The head is behind the retained tip, so this turn reads as the newest
|
||||||
|
# one but still has an accepted future descending from it. Switching the
|
||||||
|
# live take in place would leave that future continuing text the story
|
||||||
|
# no longer tells. Forking is the operation that does this safely, and
|
||||||
|
# `/fork` is where it lives.
|
||||||
|
raise HTTPException(
|
||||||
|
400,
|
||||||
|
"This turn has a later story that was undone but kept. Redo first, "
|
||||||
|
"or use another take to start a new line from here.",
|
||||||
|
)
|
||||||
turns.acquire_turn_lock(adventure_id)
|
turns.acquire_turn_lock(adventure_id)
|
||||||
try:
|
try:
|
||||||
chosen = rows[payload.index]
|
chosen = rows[payload.index]
|
||||||
@@ -263,7 +294,15 @@ def add_take(
|
|||||||
retry_of = None
|
retry_of = None
|
||||||
try:
|
try:
|
||||||
newest = last_action(adventure, db)
|
newest = last_action(adventure, db)
|
||||||
at_the_tip = newest is not None and newest.id == action.id
|
# `last_action` reads the capped path, so under a moved-back head it
|
||||||
|
# reports the node at the head as the newest one. A turn with a retained
|
||||||
|
# future is not a leaf, whatever the capped read says, so ask the head
|
||||||
|
# module rather than trusting the depth comparison alone (M3).
|
||||||
|
at_the_tip = (
|
||||||
|
newest is not None
|
||||||
|
and newest.id == action.id
|
||||||
|
and not head.behind_tip(db, adventure)
|
||||||
|
)
|
||||||
if at_the_tip and action.type == "ai":
|
if at_the_tip and action.type == "ai":
|
||||||
# Nothing was played after it, so its attempts are still leaves and
|
# Nothing was played after it, so its attempts are still leaves and
|
||||||
# a branch would serve no purpose. This is the `retry` path.
|
# a branch would serve no purpose. This is the `retry` path.
|
||||||
@@ -274,7 +313,10 @@ def add_take(
|
|||||||
# text that is there now. The new attempt leaves the path just
|
# text that is there now. The new attempt leaves the path just
|
||||||
# before the turn, so that story keeps the attempt it was written
|
# before the turn, so that story keeps the attempt it was written
|
||||||
# for.
|
# for.
|
||||||
|
departed = lineage.branch_of(db, adventure)
|
||||||
tree.branch_at(db, adventure, action.depth - 1)
|
tree.branch_at(db, adventure, action.depth - 1)
|
||||||
|
if departed is not None:
|
||||||
|
head.mark_superseded(departed, action.depth - 1)
|
||||||
attempts.roll_back_before(db, adventure, action)
|
attempts.roll_back_before(db, adventure, action)
|
||||||
adventure.updated_at = models.utcnow()
|
adventure.updated_at = models.utcnow()
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -309,71 +351,78 @@ def undo_turn(
|
|||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
adventure: models.Adventure = Depends(current_adventure),
|
adventure: models.Adventure = Depends(current_adventure),
|
||||||
):
|
):
|
||||||
"""Deletes the last turn: the trailing AI action and its player action, if any.
|
"""Moves the story back one turn. Deletes nothing (M3).
|
||||||
|
|
||||||
The endpoint also rolls the shared `script_state` back to before that turn
|
This endpoint used to remove the trailing AI action and the player action in
|
||||||
ran, and it prunes any memory that summarized the removed actions. The turn
|
front of it, prune the memories that covered them, and let the tip fall back
|
||||||
lock prevents an undo while a turn is still generating.
|
to whatever survived. Undoing was therefore the one operation in the
|
||||||
|
application that destroyed accepted story, and it was why there was no Redo:
|
||||||
|
the turns to move forward into no longer existed.
|
||||||
|
|
||||||
|
Now it moves `adventure.head_depth`. The rows stay exactly where they are,
|
||||||
|
still live, still on their branch, and `lineage.Path` stops every read at the
|
||||||
|
head instead. The transcript, the assembled context, `attempts.preceding` and
|
||||||
|
memory retrieval all narrow together, because all four already funnelled
|
||||||
|
through the same path object.
|
||||||
|
|
||||||
|
The memory bank needs no pruning for the same reason. A memory carries the
|
||||||
|
coordinate of the node its block ends on, so a memory derived from a turn
|
||||||
|
that is now past the head falls outside the capped clause and stops being
|
||||||
|
retrievable — and becomes eligible again on Redo, without having been deleted
|
||||||
|
and re-embedded. That is `STORY-BRANCH-SEMANTICS.md` §33 for free.
|
||||||
|
|
||||||
|
The state comes back from the node the story now ends on, which recorded what
|
||||||
|
it left behind when it played. See `head.move_to`.
|
||||||
"""
|
"""
|
||||||
turns.acquire_turn_lock(adventure_id)
|
turns.acquire_turn_lock(adventure_id)
|
||||||
try:
|
try:
|
||||||
# Only the last turn is removed, so fetch the two actions it can
|
target = head.undo_target(db, adventure)
|
||||||
# consist of rather than the whole story.
|
if target is None:
|
||||||
newest = (
|
|
||||||
db.query(models.Action)
|
|
||||||
.filter(
|
|
||||||
models.Action.adventure_id == adventure.id,
|
|
||||||
lineage.path_of(db, adventure).clause(models.Action),
|
|
||||||
)
|
|
||||||
.order_by(models.Action.depth.desc(), models.Action.id.desc())
|
|
||||||
.limit(2)
|
|
||||||
.all()
|
|
||||||
)
|
|
||||||
if not newest or newest[0].type == "start":
|
|
||||||
raise HTTPException(400, "Nothing to undo")
|
raise HTTPException(400, "Nothing to undo")
|
||||||
last = newest[0]
|
depth, _first_stepped = target
|
||||||
before_that = newest[1] if len(newest) > 1 else None
|
head.move_to(db, adventure, depth)
|
||||||
# Undo only what this branch owns. Everything before the fork is
|
adventure.updated_at = models.utcnow()
|
||||||
# borrowed from an ancestor and is part of that ancestor's story too, so
|
|
||||||
# an undo here must never delete a turn out of another branch. The test
|
|
||||||
# reads the row's own branch rather than the fork depth, because the
|
|
||||||
# branch is what decides the case.
|
|
||||||
if last.branch_id != adventure.head_branch_id:
|
|
||||||
raise HTTPException(
|
|
||||||
400, "Nothing to undo on this branch — the turns before it "
|
|
||||||
"belong to the branch it was forked from.",
|
|
||||||
)
|
|
||||||
first_removed = last
|
|
||||||
if (last.type == "ai" and before_that is not None
|
|
||||||
and before_that.type in ("do", "say", "story")
|
|
||||||
and before_that.branch_id == adventure.head_branch_id):
|
|
||||||
first_removed = before_that
|
|
||||||
# The state the story returns to once the turn is gone, which is what
|
|
||||||
# the node before the earliest removed one left behind. Read it before
|
|
||||||
# the deletes, while those rows are still in the story.
|
|
||||||
restore_to = attempts.preceding(db, adventure, first_removed)
|
|
||||||
delete_turn(db, adventure, last)
|
|
||||||
if first_removed is not last:
|
|
||||||
delete_turn(db, adventure, first_removed)
|
|
||||||
attempts.restore_state(adventure, restore_to)
|
|
||||||
db.flush() # Apply the deletes before anything reads the story back.
|
|
||||||
db.expire(adventure, ["actions"])
|
|
||||||
# The tip moves back with the deleted rows.
|
|
||||||
tree.refresh_head(db, adventure)
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(adventure)
|
db.refresh(adventure)
|
||||||
# Return the newest window rather than the whole story. The client
|
# Return the newest window rather than the whole story. The client
|
||||||
# replaces its transcript with this response, and the transcript is a
|
# replaces its transcript with this response, and the transcript is a
|
||||||
# window. Returning everything would defeat the paging on the action a
|
# window. Returning everything would defeat the paging on the action a
|
||||||
# player is most likely to repeat several times in a row.
|
# player is most likely to repeat several times in a row.
|
||||||
actions, total, has_more = action_window(db, adventure)
|
return current_window(db, adventure)
|
||||||
return schemas.ActionPage(
|
finally:
|
||||||
actions=[
|
turns._active_turns.discard(adventure_id)
|
||||||
schemas.ActionOut.model_validate(a)
|
|
||||||
for a in annotate_takes(db, adventure.id, actions)
|
|
||||||
],
|
@router.post("/{adventure_id}/redo", response_model=schemas.ActionPage)
|
||||||
total=total,
|
def redo_turn(
|
||||||
has_more=has_more,
|
adventure_id: int,
|
||||||
)
|
db: Session = Depends(get_db),
|
||||||
|
adventure: models.Adventure = Depends(current_adventure),
|
||||||
|
):
|
||||||
|
"""Moves the story forward again into the continuation Undo stepped out of.
|
||||||
|
|
||||||
|
Redo exists because Undo stopped deleting. It walks the head forward over one
|
||||||
|
whole turn along the retained lineage, and restores the state that turn left
|
||||||
|
behind.
|
||||||
|
|
||||||
|
It follows the lineage rather than choosing among branches, which is what
|
||||||
|
makes it invalidate itself correctly. Writing below a moved-back head forks,
|
||||||
|
and from the new branch the displaced future is no longer on the lineage at
|
||||||
|
all — so there is nothing ahead to walk into and this returns 400 without any
|
||||||
|
flag having to be set or cleared. `STORY-BRANCH-SEMANTICS.md` §8.
|
||||||
|
|
||||||
|
400 is also what a head already at the tip gets, which is the ordinary case
|
||||||
|
for a story that has never been undone.
|
||||||
|
"""
|
||||||
|
turns.acquire_turn_lock(adventure_id)
|
||||||
|
try:
|
||||||
|
depth = head.redo_target(db, adventure)
|
||||||
|
if depth is None:
|
||||||
|
raise HTTPException(400, "Nothing to redo")
|
||||||
|
head.move_to(db, adventure, depth)
|
||||||
|
adventure.updated_at = models.utcnow()
|
||||||
|
db.commit()
|
||||||
|
db.refresh(adventure)
|
||||||
|
return current_window(db, adventure)
|
||||||
finally:
|
finally:
|
||||||
turns._active_turns.discard(adventure_id)
|
turns._active_turns.discard(adventure_id)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from fastapi.responses import StreamingResponse
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ... import (
|
from ... import (
|
||||||
attempts, limits, memorybank, models, schemas, tree, worldstate,
|
attempts, head, limits, memorybank, models, schemas, tree, worldstate,
|
||||||
)
|
)
|
||||||
from ...context import build_context, cursors
|
from ...context import build_context, cursors
|
||||||
from ...database import get_db
|
from ...database import get_db
|
||||||
@@ -333,6 +333,14 @@ def create_action(
|
|||||||
acquire_turn_lock(adventure_id)
|
acquire_turn_lock(adventure_id)
|
||||||
try:
|
try:
|
||||||
_move_to_after(db, adventure, payload.after_id)
|
_move_to_after(db, adventure, payload.after_id)
|
||||||
|
# The first write below a moved-back head is where a divergence happens
|
||||||
|
# (M3). Undo alone does not fork — the user may be reading, or about to
|
||||||
|
# Redo — so this is the moment the story states which continuation it
|
||||||
|
# means. The displaced future keeps its rows on the branch being left.
|
||||||
|
# A head already at the tip, which is every ordinary turn, forks nothing.
|
||||||
|
if head.fork_if_behind_head(db, adventure):
|
||||||
|
db.commit()
|
||||||
|
db.refresh(adventure)
|
||||||
except BaseException:
|
except BaseException:
|
||||||
_active_turns.discard(adventure_id)
|
_active_turns.discard(adventure_id)
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ ICON_MAX = 16 # One emoji or glyph. VARCHAR(16).
|
|||||||
BRANCH_NAME_MAX = 80 # What a player called one line of the story. VARCHAR(80).
|
BRANCH_NAME_MAX = 80 # What a player called one line of the story. VARCHAR(80).
|
||||||
PERSONA_NAME_MAX = 80 # The protagonist's name. VARCHAR(80).
|
PERSONA_NAME_MAX = 80 # The protagonist's name. VARCHAR(80).
|
||||||
PERSONA_PRONOUNS_MAX = 40 # "they/them" and the like. VARCHAR(40).
|
PERSONA_PRONOUNS_MAX = 40 # "they/them" and the like. VARCHAR(40).
|
||||||
|
# M4: what a player called a Save Point. VARCHAR(120). Wider than a branch name
|
||||||
|
# because these are sentences rather than labels — "Before entering the abbey"
|
||||||
|
# is the example the specification uses throughout.
|
||||||
|
CHECKPOINT_NAME_MAX = 120
|
||||||
|
|
||||||
Name = Annotated[str, Field(max_length=NAME_MAX)]
|
Name = Annotated[str, Field(max_length=NAME_MAX)]
|
||||||
Tags = Annotated[str, Field(max_length=TAGS_MAX)]
|
Tags = Annotated[str, Field(max_length=TAGS_MAX)]
|
||||||
@@ -35,6 +39,7 @@ Image = Annotated[str, Field(max_length=IMAGE_MAX)]
|
|||||||
Icon = Annotated[str, Field(max_length=ICON_MAX)]
|
Icon = Annotated[str, Field(max_length=ICON_MAX)]
|
||||||
PersonaName = Annotated[str, Field(max_length=PERSONA_NAME_MAX)]
|
PersonaName = Annotated[str, Field(max_length=PERSONA_NAME_MAX)]
|
||||||
PersonaPronouns = Annotated[str, Field(max_length=PERSONA_PRONOUNS_MAX)]
|
PersonaPronouns = Annotated[str, Field(max_length=PERSONA_PRONOUNS_MAX)]
|
||||||
|
CheckpointName = Annotated[str, Field(max_length=CHECKPOINT_NAME_MAX)]
|
||||||
|
|
||||||
|
|
||||||
class ORMModel(BaseModel):
|
class ORMModel(BaseModel):
|
||||||
@@ -254,6 +259,10 @@ class BranchOut(ORMModel):
|
|||||||
fork_depth: int | None = None
|
fork_depth: int | None = None
|
||||||
depth: int
|
depth: int
|
||||||
own_actions: int = 0
|
own_actions: int = 0
|
||||||
|
# M4: how many Save Points name a position on this line. Deleting the branch
|
||||||
|
# deletes them with its story, so the panel warns with a number rather than
|
||||||
|
# a vague caution. Zero for a line nobody has bookmarked, which is most.
|
||||||
|
save_points: int = 0
|
||||||
is_head: bool = False
|
is_head: bool = False
|
||||||
# NULL for a branch nobody has named. The client labels those from the fork
|
# NULL for a branch nobody has named. The client labels those from the fork
|
||||||
# depth rather than the server inventing a name. See the column comment.
|
# depth rather than the server inventing a name. See the column comment.
|
||||||
@@ -267,6 +276,67 @@ class BranchRename(BaseModel):
|
|||||||
name: Annotated[str, Field(max_length=BRANCH_NAME_MAX)] | None = None
|
name: Annotated[str, Field(max_length=BRANCH_NAME_MAX)] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- Save Points (M4) ----------
|
||||||
|
#
|
||||||
|
# "Save Point" is the user-facing term and `checkpoint` is the internal one
|
||||||
|
# (`BROWSER-UX-SPEC.md` §23). The wire format uses the internal name, as the
|
||||||
|
# rest of this module does.
|
||||||
|
|
||||||
|
|
||||||
|
class CheckpointOut(ORMModel):
|
||||||
|
"""One Save Point: a name and the position it names.
|
||||||
|
|
||||||
|
The position is reported three ways because the panel needs three different
|
||||||
|
things from it. `turn` is what a reader counts — the same `depth + 1` the
|
||||||
|
branch list shows. `depth` and `branch_id` are the coordinate itself.
|
||||||
|
`on_path` says whether the position lies on the story being read, which is
|
||||||
|
how the panel can tell a Save Point on this line from one naming a line the
|
||||||
|
story has left; restoring either works, but they are not the same offer.
|
||||||
|
|
||||||
|
`resolved` is false when the coordinate no longer names a live turn, which
|
||||||
|
an action deleted out of the middle of a story can do. Restore refuses such
|
||||||
|
a Save Point rather than moving the head somewhere approximate, so the list
|
||||||
|
says so before the button is pressed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
id: int
|
||||||
|
adventure_id: int
|
||||||
|
name: str
|
||||||
|
note: str = ""
|
||||||
|
branch_id: int
|
||||||
|
depth: int
|
||||||
|
turn: int = 0
|
||||||
|
on_path: bool = True
|
||||||
|
resolved: bool = True
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class CheckpointCreate(BaseModel):
|
||||||
|
"""A Save Point at wherever the story is being read.
|
||||||
|
|
||||||
|
The position is not a field. A Save Point is made at the campaign's active
|
||||||
|
head, which the server already knows, and accepting a coordinate from the
|
||||||
|
client would be the second way to name a position — the thing this milestone
|
||||||
|
exists not to build.
|
||||||
|
"""
|
||||||
|
|
||||||
|
name: CheckpointName
|
||||||
|
note: Prose = ""
|
||||||
|
|
||||||
|
|
||||||
|
class CheckpointRename(BaseModel):
|
||||||
|
"""A new label, and nothing else.
|
||||||
|
|
||||||
|
There is deliberately no coordinate here. `STORY-BRANCH-SEMANTICS.md` §24
|
||||||
|
keeps a Save Point's meaning auditable by refusing to move one: rename it,
|
||||||
|
or delete it and make another where you are.
|
||||||
|
"""
|
||||||
|
|
||||||
|
name: CheckpointName | None = None
|
||||||
|
note: Prose | None = None
|
||||||
|
|
||||||
|
|
||||||
class ActionUpdate(BaseModel):
|
class ActionUpdate(BaseModel):
|
||||||
text: ActionText
|
text: ActionText
|
||||||
|
|
||||||
@@ -317,6 +387,12 @@ class AdventureOut(ORMModel):
|
|||||||
# story's length, which is how the client knows more actions exist above.
|
# story's length, which is how the client knows more actions exist above.
|
||||||
actions: list[ActionOut] = []
|
actions: list[ActionOut] = []
|
||||||
action_count: int = 0
|
action_count: int = 0
|
||||||
|
# M3. Whether the history controls have anywhere to go from where the story
|
||||||
|
# is. The client cannot work either out for itself: `can_undo` needs the
|
||||||
|
# campaign opening, which may be off the top of the loaded window, and
|
||||||
|
# `can_redo` needs the retained future, which the client is never sent.
|
||||||
|
can_undo: bool = False
|
||||||
|
can_redo: bool = False
|
||||||
|
|
||||||
|
|
||||||
class ActionPage(BaseModel):
|
class ActionPage(BaseModel):
|
||||||
@@ -327,6 +403,10 @@ class ActionPage(BaseModel):
|
|||||||
# Whether anything older than this slice exists. The server computes it, so
|
# Whether anything older than this slice exists. The server computes it, so
|
||||||
# the client never has to do arithmetic on positions to find the end.
|
# the client never has to do arithmetic on positions to find the end.
|
||||||
has_more: bool = False
|
has_more: bool = False
|
||||||
|
# The same two flags `AdventureOut` carries, so that the response to Undo,
|
||||||
|
# Redo or a turn updates the controls without a second request.
|
||||||
|
can_undo: bool = False
|
||||||
|
can_redo: bool = False
|
||||||
|
|
||||||
|
|
||||||
# ---------- Memory bank (Phase 6) ----------
|
# ---------- Memory bank (Phase 6) ----------
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
"""The storyteller, run as a real OS process for `test_process_restart.py`.
|
||||||
|
|
||||||
|
Not a test module, and named so pytest does not collect it: it is the program
|
||||||
|
the test starts, twice, against one database file.
|
||||||
|
|
||||||
|
The model is replaced with a deterministic fake before the app is imported, so
|
||||||
|
the process needs no Ollama, no network and no configuration. Everything else —
|
||||||
|
the engine, the migrations, the routers, the session lifecycle — is the real
|
||||||
|
application, which is the whole point of spawning a process at all.
|
||||||
|
|
||||||
|
python _restart_server.py <db_path> <port>
|
||||||
|
"""
|
||||||
|
import itertools
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
HERE = Path(__file__).resolve().parent
|
||||||
|
sys.path.insert(0, str(HERE.parent)) # backend/, so `app` imports
|
||||||
|
sys.path.insert(0, str(HERE)) # tests/, so `fakes` imports
|
||||||
|
|
||||||
|
db_path, port = sys.argv[1], int(sys.argv[2])
|
||||||
|
os.environ["AIDND_DB_PATH"] = db_path
|
||||||
|
# A developer's shell may point these at Postgres, and `app.database` prefers
|
||||||
|
# either over the SQLite path. The suite's conftest clears them for the same
|
||||||
|
# reason; a spawned process does not inherit that, so clear them here too.
|
||||||
|
os.environ.pop("AIDND_DATABASE_URL", None)
|
||||||
|
os.environ.pop("DATABASE_URL", None)
|
||||||
|
|
||||||
|
from fakes import GOLD_PER_TURN, gold_reply # noqa: E402
|
||||||
|
|
||||||
|
_turn = itertools.count(1)
|
||||||
|
|
||||||
|
|
||||||
|
class DeterministicProvider:
|
||||||
|
"""Banks one turn's worth of gold per reply, numbered so text is checkable.
|
||||||
|
|
||||||
|
The same instrumentation `test_head_cursor.py` and `test_save_points.py`
|
||||||
|
use, for the same reason: it makes "the state at this position" a number the
|
||||||
|
test can assert rather than a paragraph it has to interpret. M5 replaces the
|
||||||
|
machinery underneath; what this measures is where the story is being read.
|
||||||
|
"""
|
||||||
|
|
||||||
|
last_usage = None
|
||||||
|
|
||||||
|
def __init__(self, *a, **k):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def generate(self, parts, *, temperature, max_tokens):
|
||||||
|
yield ("text", gold_reply(f"Beat {next(_turn)}."))
|
||||||
|
|
||||||
|
|
||||||
|
from app.routers.adventures import turns # noqa: E402
|
||||||
|
|
||||||
|
turns.OpenAICompatibleProvider = DeterministicProvider
|
||||||
|
|
||||||
|
from app.main import app # noqa: E402
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
# Loopback only, as every supported start path does.
|
||||||
|
uvicorn.run(app, host="127.0.0.1", port=port, log_level="warning")
|
||||||
@@ -200,18 +200,40 @@ def test_the_assembled_prompt_is_stored_once_per_turn(client):
|
|||||||
assert len(moved) == 1 and moved != live_holder, "the prompt follows the story"
|
assert len(moved) == 1 and moved != live_holder, "the prompt follows the story"
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------- removing the turn
|
# ------------------------------------------------- stepping behind the turn
|
||||||
|
|
||||||
def test_undo_takes_every_attempt_with_it(client):
|
def test_undo_hides_every_attempt_and_keeps_them_all(client):
|
||||||
|
"""M3 rewrote this test. Undo used to delete the turn, and the assertion was
|
||||||
|
that it took the whole sibling group with it rather than leaving orphaned
|
||||||
|
attempts at a coordinate the story no longer reached.
|
||||||
|
|
||||||
|
The group still moves as one, but it moves out of the story rather than out
|
||||||
|
of the database: one Undo steps behind the turn, so none of its three
|
||||||
|
attempts is in what the story tells, and all three are still on disk for the
|
||||||
|
Redo that walks back into them. The old assertion is kept as the second half
|
||||||
|
— what the story reads — and the row count is the new first half.
|
||||||
|
"""
|
||||||
ScriptedProvider.replies = ["One.", "Two.", "Three."]
|
ScriptedProvider.replies = ["One.", "Two.", "Three."]
|
||||||
_play(client)
|
_play(client)
|
||||||
_retry(client)
|
_retry(client)
|
||||||
_retry(client)
|
_retry(client)
|
||||||
assert len([a for a in _rows(client.adv_id) if a.type == "ai"]) == 3
|
before = _rows(client.adv_id)
|
||||||
|
assert len([a for a in before if a.type == "ai"]) == 3
|
||||||
|
|
||||||
r = client.post(f"/api/adventures/{client.adv_id}/undo")
|
r = client.post(f"/api/adventures/{client.adv_id}/undo")
|
||||||
assert r.status_code == 200, r.text
|
assert r.status_code == 200, r.text
|
||||||
assert [a.type for a in _rows(client.adv_id)] == ["start"]
|
# What the story tells: the opening, and none of the turn's attempts.
|
||||||
|
assert [a["type"] for a in r.json()["actions"]] == ["start"]
|
||||||
|
# What it holds: every row that was there before, attempts included.
|
||||||
|
after = _rows(client.adv_id)
|
||||||
|
assert len(after) == len(before)
|
||||||
|
assert {a.id for a in after} == {a.id for a in before}
|
||||||
|
assert len([a for a in after if a.type == "ai"]) == 3
|
||||||
|
# And the group is reachable again, whole, with the same take live.
|
||||||
|
live_before = [a.id for a in before if a.type == "ai" and a.live]
|
||||||
|
r = client.post(f"/api/adventures/{client.adv_id}/redo")
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
assert [a.id for a in _rows(client.adv_id) if a.type == "ai" and a.live] == live_before
|
||||||
|
|
||||||
|
|
||||||
def test_deleting_a_retried_turn_deletes_its_attempts(client):
|
def test_deleting_a_retried_turn_deletes_its_attempts(client):
|
||||||
|
|||||||
@@ -432,26 +432,41 @@ def test_a_memory_on_the_line_left_behind_is_out_of_range_on_the_fork(client):
|
|||||||
|
|
||||||
# ------------------------------------------------------------------- undo
|
# ------------------------------------------------------------------- undo
|
||||||
|
|
||||||
def test_undo_stops_at_the_fork(client):
|
def test_undo_walks_off_a_fork_into_the_story_it_inherits(client):
|
||||||
"""Undoing a turn on a fork must never reach into the branch it forked
|
"""M3 rewrote this test, and reversed half of it.
|
||||||
from. Those turns belong to that branch's story too."""
|
|
||||||
|
Undo used to refuse at a fork point, and it had to: it deleted the turns it
|
||||||
|
stepped over, and the turns before the fork belong to the parent branch's
|
||||||
|
story as well. Refusing was the only way to stop one branch's Undo from
|
||||||
|
removing rows another branch was reading.
|
||||||
|
|
||||||
|
Nothing is deleted now, so there is nothing to protect the parent from. A
|
||||||
|
forked branch inherits the story up to its fork, that inherited story is
|
||||||
|
part of what this branch tells, and Undo walks back through it like any
|
||||||
|
other retained history. The floor is the campaign opening, not the fork.
|
||||||
|
"""
|
||||||
discarded = _divergent_story(client)
|
discarded = _divergent_story(client)
|
||||||
_fork(client, discarded)
|
_fork(client, discarded)
|
||||||
rows_before = len(_rows(client.adv_id))
|
rows_before = len(_rows(client.adv_id))
|
||||||
|
|
||||||
r = client.post(f"/api/adventures/{client.adv_id}/undo")
|
r = client.post(f"/api/adventures/{client.adv_id}/undo")
|
||||||
assert r.status_code == 200, r.text
|
assert r.status_code == 200, r.text
|
||||||
# The promoted attempt is removed, and the player action before it
|
# One Undo steps over a whole turn, so it takes the player's action with the
|
||||||
# stays, because that action belongs to the parent and the parent
|
# reply to it — and that player action is the parent's row, sitting in front
|
||||||
# still has it.
|
# of the fork. Stepping behind it is a read moving backwards, not a branch
|
||||||
assert len(_rows(client.adv_id)) == rows_before - 1
|
# reaching into another branch's rows: nothing moved either way.
|
||||||
assert _texts(client) == ["You enter a cave.", "> You look around."]
|
assert len(_rows(client.adv_id)) == rows_before
|
||||||
|
assert _texts(client) == ["You enter a cave."]
|
||||||
|
|
||||||
# Nothing is left of this branch's own turns, so undo must refuse
|
# The opening is the floor, and it is the parent's node too.
|
||||||
# instead of removing the parent's turns.
|
|
||||||
r = client.post(f"/api/adventures/{client.adv_id}/undo")
|
r = client.post(f"/api/adventures/{client.adv_id}/undo")
|
||||||
assert r.status_code == 400
|
assert r.status_code == 400
|
||||||
assert "forked from" in r.json()["detail"]
|
assert "Nothing to undo" in r.json()["detail"]
|
||||||
|
|
||||||
|
# Redo walks back out to where the fork was left, taking the turn whole.
|
||||||
|
assert client.post(f"/api/adventures/{client.adv_id}/redo").status_code == 200
|
||||||
|
assert _texts(client) == ["You enter a cave.", "> You look around.", "Attempt one."]
|
||||||
|
assert len(_rows(client.adv_id)) == rows_before
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------- the tree view
|
# ----------------------------------------------------------- the tree view
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ Most of this file used to be about the shared demo key: an access gate on a
|
|||||||
"power user" email allowlist, and a pinning rule that stopped a public visitor
|
"power user" email allowlist, and a pinning rule that stopped a public visitor
|
||||||
reaching paid models on a server-funded key. M2 removed the hosted deployment
|
reaching paid models on a server-funded key. M2 removed the hosted deployment
|
||||||
those defended, so the rules they tested no longer exist to be tested. See
|
those defended, so the rules they tested no longer exist to be tested. See
|
||||||
`planning/reports/M2-*` for the accounting.
|
`planning/archive/milestone-reports/M2-*` for the accounting.
|
||||||
|
|
||||||
What remains is what the page still does: stream a reply from the configured
|
What remains is what the page still does: stream a reply from the configured
|
||||||
model, honour a system prompt and a per-request model override, and refuse a
|
model, honour a system prompt and a per-request model override, and refuse a
|
||||||
|
|||||||
@@ -0,0 +1,873 @@
|
|||||||
|
"""M3: the active head, and what moving it costs.
|
||||||
|
|
||||||
|
This file is the acceptance contract for the milestone that stopped Undo from
|
||||||
|
deleting. Its subject is one invariant and the behaviour that follows from it:
|
||||||
|
|
||||||
|
Undo deletes zero accepted turns.
|
||||||
|
|
||||||
|
Everything else here is a consequence. Redo exists because the turns are still
|
||||||
|
there. Divergence retires a future rather than removing it. Memory and summary
|
||||||
|
coverage narrow and widen again as the head moves, without anything being
|
||||||
|
re-embedded. Export carries where the reader stopped, because that is now a
|
||||||
|
decision rather than a fact about the newest row.
|
||||||
|
|
||||||
|
The tests are named for the acceptance items they discharge — D01-D10, E01-E04,
|
||||||
|
I01-I03, I07, L01-L02 in `planning/V1-ACCEPTANCE-TESTS.md` — so a reader can go
|
||||||
|
from a failing test to the requirement it belongs to without a map.
|
||||||
|
|
||||||
|
The world state is instrumentation here, not the subject. Each scripted reply
|
||||||
|
banks ten gold, which makes "the state at this position" a number a test can
|
||||||
|
assert instead of a paragraph it has to interpret. M5 replaces that machinery
|
||||||
|
with genre-neutral narrative state; these tests then need the *instrumentation*
|
||||||
|
moved, not the assertions removed, because what they measure is where the story
|
||||||
|
is being read.
|
||||||
|
|
||||||
|
python -m pytest tests/test_head_cursor.py -v
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
from fastapi import Depends
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from app import limits, models
|
||||||
|
from app.providers import ProviderError
|
||||||
|
from app.context import cursors, lineage
|
||||||
|
from app.database import Base, SessionLocal, engine, get_db
|
||||||
|
from app.main import app
|
||||||
|
from app import auth, tree
|
||||||
|
from app.routers import adventures
|
||||||
|
|
||||||
|
from fakes import GOLD_PER_TURN, GOLD_SCHEMA, ScriptedProvider, gold_replies
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def client(monkeypatch):
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
setup = SessionLocal()
|
||||||
|
user = models.User(is_guest=False, email="head@example.com")
|
||||||
|
setup.add(user)
|
||||||
|
setup.flush()
|
||||||
|
setup.add(models.Settings(user_id=user.id, api_key="enc:dummy", model="test-model"))
|
||||||
|
scenario = models.Scenario(user_id=user.id, title="S", stat_schema=GOLD_SCHEMA)
|
||||||
|
setup.add(scenario)
|
||||||
|
setup.flush()
|
||||||
|
adv = models.Adventure(
|
||||||
|
user_id=user.id, title="Tavern", scenario_id=scenario.id,
|
||||||
|
world_state={"player": {"hp": 100, "gold": 0}},
|
||||||
|
)
|
||||||
|
setup.add(adv)
|
||||||
|
setup.flush()
|
||||||
|
setup.add(models.Action(adventure_id=adv.id, type="start", text="You enter the tavern."))
|
||||||
|
setup.commit()
|
||||||
|
adv_id, user_id = adv.id, user.id
|
||||||
|
setup.close()
|
||||||
|
|
||||||
|
ScriptedProvider.replies = gold_replies()
|
||||||
|
monkeypatch.setattr(adventures.turns, "OpenAICompatibleProvider", ScriptedProvider)
|
||||||
|
monkeypatch.setattr(limits, "check_row_cap", lambda *a, **k: None)
|
||||||
|
|
||||||
|
def _current_user(db=Depends(get_db)):
|
||||||
|
return db.get(models.User, user_id)
|
||||||
|
|
||||||
|
app.dependency_overrides[auth.get_current_user] = _current_user
|
||||||
|
c = TestClient(app)
|
||||||
|
c.adv_id = adv_id
|
||||||
|
try:
|
||||||
|
yield c
|
||||||
|
finally:
|
||||||
|
app.dependency_overrides.clear()
|
||||||
|
adventures.turns._active_turns.clear()
|
||||||
|
Base.metadata.drop_all(bind=engine)
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ helpers
|
||||||
|
|
||||||
|
def _play(client, text="look around", type="do", after_id=None):
|
||||||
|
payload = {"type": type, "text": text}
|
||||||
|
if after_id is not None:
|
||||||
|
payload["after_id"] = after_id
|
||||||
|
r = client.post(f"/api/adventures/{client.adv_id}/actions", json=payload)
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def _turns(client, count):
|
||||||
|
for n in range(count):
|
||||||
|
_play(client, f"turn {n}")
|
||||||
|
|
||||||
|
|
||||||
|
def _undo(client):
|
||||||
|
return client.post(f"/api/adventures/{client.adv_id}/undo")
|
||||||
|
|
||||||
|
|
||||||
|
def _redo(client):
|
||||||
|
return client.post(f"/api/adventures/{client.adv_id}/redo")
|
||||||
|
|
||||||
|
|
||||||
|
def _adventure(client) -> dict:
|
||||||
|
r = client.get(f"/api/adventures/{client.adv_id}")
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
|
||||||
|
def _texts(client) -> list[str]:
|
||||||
|
return [a["text"] for a in _adventure(client)["actions"]]
|
||||||
|
|
||||||
|
|
||||||
|
def _rows(adv_id) -> list[models.Action]:
|
||||||
|
"""Every action row, story or not, live or not, head or no head."""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
return (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(models.Action.adventure_id == adv_id)
|
||||||
|
.order_by(models.Action.branch_id, models.Action.depth, models.Action.id)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _gold(adv_id) -> int:
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
adv = db.get(models.Adventure, adv_id)
|
||||||
|
return (adv.world_state or {}).get("player", {}).get("gold", 0)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _head(adv_id) -> tuple[int, int]:
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
adv = db.get(models.Adventure, adv_id)
|
||||||
|
return adv.head_branch_id, adv.head_depth
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------- D01, D02, D03: undo
|
||||||
|
|
||||||
|
def test_d01_one_undo_returns_the_transcript_and_the_state(client):
|
||||||
|
_turns(client, 2)
|
||||||
|
assert _gold(client.adv_id) == 2 * GOLD_PER_TURN
|
||||||
|
told = _texts(client)
|
||||||
|
|
||||||
|
page = _undo(client)
|
||||||
|
assert page.status_code == 200, page.text
|
||||||
|
|
||||||
|
# Both halves of the turn step back together: a player's action and the
|
||||||
|
# reply to it are one accepted story step.
|
||||||
|
assert [a["text"] for a in page.json()["actions"]] == told[:-2]
|
||||||
|
assert _gold(client.adv_id) == 1 * GOLD_PER_TURN
|
||||||
|
|
||||||
|
|
||||||
|
def test_d02_five_consecutive_undos_each_land_where_they_should(client):
|
||||||
|
_turns(client, 7)
|
||||||
|
assert _gold(client.adv_id) == 7 * GOLD_PER_TURN
|
||||||
|
|
||||||
|
for step in range(1, 6):
|
||||||
|
assert _undo(client).status_code == 200
|
||||||
|
assert _gold(client.adv_id) == (7 - step) * GOLD_PER_TURN
|
||||||
|
# Two rows per turn, plus the opening.
|
||||||
|
assert len(_texts(client)) == 1 + 2 * (7 - step)
|
||||||
|
|
||||||
|
|
||||||
|
def test_d03_undo_walks_back_to_the_campaign_opening_and_stops(client):
|
||||||
|
_turns(client, 7)
|
||||||
|
|
||||||
|
for _ in range(7):
|
||||||
|
assert _undo(client).status_code == 200
|
||||||
|
|
||||||
|
assert _texts(client) == ["You enter the tavern."]
|
||||||
|
assert _gold(client.adv_id) == 0
|
||||||
|
# The opening is the floor. There is no pre-campaign position to reach.
|
||||||
|
refused = _undo(client)
|
||||||
|
assert refused.status_code == 400
|
||||||
|
assert "Nothing to undo" in refused.json()["detail"]
|
||||||
|
assert _adventure(client)["can_undo"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_m3_invariant_undo_deletes_zero_accepted_turns(client):
|
||||||
|
"""The row count is the milestone. Everything else in this file is a
|
||||||
|
consequence of it, so it is asserted on its own, over the whole retained
|
||||||
|
tree rather than over the story being told."""
|
||||||
|
_turns(client, 5)
|
||||||
|
before = [a.id for a in _rows(client.adv_id)]
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
assert _undo(client).status_code == 200
|
||||||
|
|
||||||
|
after = [a.id for a in _rows(client.adv_id)]
|
||||||
|
assert after == before
|
||||||
|
assert len(_texts(client)) == 1 # and yet the story is back at its opening
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------------------------------------------- D04, L02: redo
|
||||||
|
|
||||||
|
def test_d04_redo_restores_the_continuation_and_its_state(client):
|
||||||
|
_turns(client, 4)
|
||||||
|
whole = _texts(client)
|
||||||
|
|
||||||
|
_undo(client)
|
||||||
|
_undo(client)
|
||||||
|
assert _gold(client.adv_id) == 2 * GOLD_PER_TURN
|
||||||
|
|
||||||
|
assert _redo(client).status_code == 200
|
||||||
|
assert _gold(client.adv_id) == 3 * GOLD_PER_TURN
|
||||||
|
assert _redo(client).status_code == 200
|
||||||
|
assert _gold(client.adv_id) == 4 * GOLD_PER_TURN
|
||||||
|
assert _texts(client) == whole
|
||||||
|
# Nowhere further forward to go, and the control says so.
|
||||||
|
assert _adventure(client)["can_redo"] is False
|
||||||
|
assert _redo(client).status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_l02_state_matches_the_position_in_both_directions(client):
|
||||||
|
"""Each position's state is the snapshot the node left behind, so arriving
|
||||||
|
from in front of it and arriving from behind it must agree."""
|
||||||
|
_turns(client, 5)
|
||||||
|
going_back = []
|
||||||
|
for _ in range(5):
|
||||||
|
_undo(client)
|
||||||
|
going_back.append(_gold(client.adv_id))
|
||||||
|
|
||||||
|
coming_forward = []
|
||||||
|
for _ in range(5):
|
||||||
|
_redo(client)
|
||||||
|
coming_forward.append(_gold(client.adv_id))
|
||||||
|
|
||||||
|
assert going_back == [40, 30, 20, 10, 0]
|
||||||
|
assert coming_forward == [10, 20, 30, 40, 50]
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------- D05, E01, E04: divergence
|
||||||
|
|
||||||
|
def test_d05_a_new_turn_below_the_head_retires_redo_and_keeps_the_future(client):
|
||||||
|
_turns(client, 4)
|
||||||
|
abandoned = {a.id for a in _rows(client.adv_id)}
|
||||||
|
|
||||||
|
_undo(client)
|
||||||
|
_undo(client)
|
||||||
|
assert _adventure(client)["can_redo"] is True
|
||||||
|
|
||||||
|
ScriptedProvider.replies = ["A different road.\n```state\n{\"player.gold\": 1}\n```"]
|
||||||
|
_play(client, "go the other way")
|
||||||
|
|
||||||
|
# Ordinary Redo cannot walk into the old future any more...
|
||||||
|
assert _adventure(client)["can_redo"] is False
|
||||||
|
assert _redo(client).status_code == 400
|
||||||
|
# ...and not one row of it was deleted to achieve that.
|
||||||
|
kept = {a.id for a in _rows(client.adv_id)}
|
||||||
|
assert abandoned <= kept
|
||||||
|
# The story now tells the new continuation.
|
||||||
|
assert _texts(client)[-1].startswith("A different road.")
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_departed_branch_records_where_the_story_left_it(client):
|
||||||
|
"""`DATA-MODEL.md` §5 gives a branch a disposition. It is stored as the fact
|
||||||
|
that produced it — the depth the story left at — and nothing reads it to
|
||||||
|
decide behaviour, so this test is what makes a divergence observable."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_undo(client)
|
||||||
|
left_at = _head(client.adv_id)[1]
|
||||||
|
|
||||||
|
_play(client, "elsewhere")
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
superseded = (
|
||||||
|
db.query(models.Branch)
|
||||||
|
.filter(
|
||||||
|
models.Branch.adventure_id == client.adv_id,
|
||||||
|
models.Branch.superseded_at.isnot(None),
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
assert len(superseded) == 1
|
||||||
|
assert superseded[0].superseded_depth == left_at
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_e01_e04_a_fact_from_the_abandoned_future_is_not_current(client):
|
||||||
|
"""E01 and E04 are the same mechanism measured twice: the state that is
|
||||||
|
current is the one belonging to the position the story is read at, so a
|
||||||
|
number only the abandoned future ever reached cannot survive a divergence."""
|
||||||
|
ScriptedProvider.replies = [
|
||||||
|
"You find a purse.\n```state\n{\"player.gold\": 10}\n```",
|
||||||
|
"You find the hoard.\n```state\n{\"player.gold\": 500}\n```",
|
||||||
|
]
|
||||||
|
_play(client, "search")
|
||||||
|
_play(client, "keep searching")
|
||||||
|
assert _gold(client.adv_id) == 510
|
||||||
|
|
||||||
|
_undo(client)
|
||||||
|
assert _gold(client.adv_id) == 10
|
||||||
|
|
||||||
|
ScriptedProvider.replies = ["You leave empty-handed.\n```state\n{\"player.gold\": 1}\n```"]
|
||||||
|
_play(client, "go home")
|
||||||
|
|
||||||
|
assert _gold(client.adv_id) == 11, "the hoard belonged to a story this one is not"
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------ E02, E03: derived work
|
||||||
|
|
||||||
|
def _attach_memory(adv_id, text, at_depth):
|
||||||
|
"""Writes a memory ending on the live node at `at_depth`, the way the
|
||||||
|
summarizer would, and anchors the memory cursor there."""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
adventure = db.get(models.Adventure, adv_id)
|
||||||
|
node = (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adv_id,
|
||||||
|
models.Action.depth == at_depth,
|
||||||
|
models.Action.live.is_(True),
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
memory = models.Memory(
|
||||||
|
adventure_id=adv_id, text=text, source_start=0, source_end=node.depth,
|
||||||
|
)
|
||||||
|
tree.attach_memory(memory, node)
|
||||||
|
db.add(memory)
|
||||||
|
cursors.MEMORY.anchor_at(adventure, node)
|
||||||
|
cursors.SUMMARY.anchor_at(adventure, node)
|
||||||
|
db.commit()
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _retrievable(adv_id) -> set[str]:
|
||||||
|
"""The memories the story can reach, through the clause `memorybank`
|
||||||
|
retrieves with."""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
adventure = db.get(models.Adventure, adv_id)
|
||||||
|
rows = (
|
||||||
|
db.query(models.Memory)
|
||||||
|
.filter(
|
||||||
|
models.Memory.adventure_id == adv_id,
|
||||||
|
lineage.path_of(db, adventure).clause(models.Memory),
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
return {m.text for m in rows}
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_e02_an_abandoned_memory_is_unreachable_and_still_on_disk(client):
|
||||||
|
_turns(client, 3)
|
||||||
|
tip_depth = _head(client.adv_id)[1]
|
||||||
|
_attach_memory(client.adv_id, "Mara reveals she is a spy.", tip_depth)
|
||||||
|
assert _retrievable(client.adv_id) == {"Mara reveals she is a spy."}
|
||||||
|
|
||||||
|
_undo(client)
|
||||||
|
# Negative control: undone, the revelation is not retrievable...
|
||||||
|
assert _retrievable(client.adv_id) == set()
|
||||||
|
_play(client, "talk about the weather")
|
||||||
|
# ...and after diverging it stays unreachable, on a line the story left.
|
||||||
|
assert _retrievable(client.adv_id) == set()
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
assert db.query(models.Memory).count() == 1, "unreachable, not deleted"
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_e02_positive_control_the_memory_returns_on_the_line_it_belongs_to(client):
|
||||||
|
"""The negative control alone would pass if memories were simply broken.
|
||||||
|
Redo puts the head back on the lineage the memory was written for, and it
|
||||||
|
must be retrievable again — without having been re-embedded."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_attach_memory(client.adv_id, "Mara reveals she is a spy.", _head(client.adv_id)[1])
|
||||||
|
|
||||||
|
_undo(client)
|
||||||
|
assert _retrievable(client.adv_id) == set()
|
||||||
|
_redo(client)
|
||||||
|
assert _retrievable(client.adv_id) == {"Mara reveals she is a spy."}
|
||||||
|
|
||||||
|
|
||||||
|
def test_e03_a_summary_anchor_cannot_claim_coverage_past_the_head(client):
|
||||||
|
"""E03 without redesigning the summarizer. The anchor says how far the
|
||||||
|
derived work has read; resolved against a head-capped path it can never
|
||||||
|
report a stretch in the abandoned future as already covered, so that content
|
||||||
|
is re-derived for the new line rather than carried into it."""
|
||||||
|
_turns(client, 3)
|
||||||
|
tip_depth = _head(client.adv_id)[1]
|
||||||
|
_attach_memory(client.adv_id, "Everything up to the reveal.", tip_depth)
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
adventure = db.get(models.Adventure, client.adv_id)
|
||||||
|
assert cursors.SUMMARY.depth(db, adventure) == tip_depth
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
_undo(client)
|
||||||
|
_play(client, "a different question")
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
adventure = db.get(models.Adventure, client.adv_id)
|
||||||
|
covered = cursors.SUMMARY.depth(db, adventure)
|
||||||
|
assert covered < tip_depth, "coverage from the abandoned line was claimed"
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------ D06-D08: retry and takes
|
||||||
|
|
||||||
|
def test_d06_d08_retry_keeps_the_earlier_take_and_reuses_the_parent_state(client):
|
||||||
|
ScriptedProvider.replies = gold_replies("Take")
|
||||||
|
_play(client, "knock")
|
||||||
|
first = [a for a in _rows(client.adv_id) if a.type == "ai"][0]
|
||||||
|
|
||||||
|
r = client.post(f"/api/adventures/{client.adv_id}/retry")
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
|
||||||
|
ai_rows = [a for a in _rows(client.adv_id) if a.type == "ai"]
|
||||||
|
assert len(ai_rows) == 2, "the earlier take is retained"
|
||||||
|
assert first.id in {a.id for a in ai_rows}
|
||||||
|
# Both takes sit at the same coordinate, which is what makes them takes
|
||||||
|
# rather than turns, and the state is one turn's worth either way.
|
||||||
|
assert {a.depth for a in ai_rows} == {first.depth}
|
||||||
|
assert _gold(client.adv_id) == GOLD_PER_TURN
|
||||||
|
|
||||||
|
|
||||||
|
def test_d07_a_retry_from_behind_the_tip_branches_instead_of_amending(client):
|
||||||
|
"""A turn with an accepted future is not a leaf, whatever the capped read
|
||||||
|
says. Retrying it has to leave that future on the line it was written for,
|
||||||
|
which is a branch — the same operation `add_take` performs for a turn the
|
||||||
|
story has already moved past."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_undo(client)
|
||||||
|
before = {a.id for a in _rows(client.adv_id)}
|
||||||
|
|
||||||
|
r = client.post(f"/api/adventures/{client.adv_id}/retry")
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
|
||||||
|
after = {a.id for a in _rows(client.adv_id)}
|
||||||
|
assert before <= after, "the retained future kept every row"
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
assert db.query(models.Branch).filter_by(adventure_id=client.adv_id).count() == 2
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_switching_a_take_is_refused_while_a_kept_future_hangs_off_it(client):
|
||||||
|
"""The one operation that cannot be made safe by branching, because it
|
||||||
|
changes which take is live in place. It reports rather than guesses."""
|
||||||
|
ScriptedProvider.replies = gold_replies()
|
||||||
|
_play(client, "knock")
|
||||||
|
client.post(f"/api/adventures/{client.adv_id}/retry")
|
||||||
|
_play(client, "go in")
|
||||||
|
_undo(client)
|
||||||
|
|
||||||
|
newest = [a for a in _rows(client.adv_id) if a.type == "ai" and a.live][0]
|
||||||
|
r = client.post(
|
||||||
|
f"/api/adventures/{client.adv_id}/actions/{newest.id}/variant", json={"index": 0}
|
||||||
|
)
|
||||||
|
assert r.status_code == 400
|
||||||
|
assert "undone but kept" in r.json()["detail"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------- D09, D10: editing
|
||||||
|
|
||||||
|
def test_d09_d10_replaying_a_turn_forks_and_keeps_the_old_line(client):
|
||||||
|
"""D09 and D10 through the operation the product actually offers. Editing
|
||||||
|
an earlier turn and replaying it is one endpoint — `takes` — and it is
|
||||||
|
already the "return to the parent state, then continue differently" shape
|
||||||
|
the semantics ask for. The plain text edit beside it is a correction to
|
||||||
|
prose that creates no continuation, and M3 does not change it.
|
||||||
|
"""
|
||||||
|
ScriptedProvider.replies = [
|
||||||
|
"You accuse her.\n```state\n{\"player.gold\": 10}\n```",
|
||||||
|
"She draws a knife.\n```state\n{\"player.gold\": 20}\n```",
|
||||||
|
]
|
||||||
|
_play(client, "I accuse Mara of stealing the key.")
|
||||||
|
_play(client, "wait")
|
||||||
|
accusation = [a for a in _rows(client.adv_id) if a.type == "do"][0]
|
||||||
|
old_future = {a.id for a in _rows(client.adv_id)}
|
||||||
|
|
||||||
|
ScriptedProvider.replies = ["She shakes her head.\n```state\n{\"player.gold\": 1}\n```"]
|
||||||
|
r = client.post(
|
||||||
|
f"/api/adventures/{client.adv_id}/actions/{accusation.id}/takes",
|
||||||
|
json={"text": "I quietly ask Mara whether she has seen the key."},
|
||||||
|
)
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
|
||||||
|
# The old future is retained whole...
|
||||||
|
assert old_future <= {a.id for a in _rows(client.adv_id)}
|
||||||
|
# ...the edited input is what the story now tells...
|
||||||
|
told = _texts(client)
|
||||||
|
assert any("quietly ask Mara" in t for t in told)
|
||||||
|
assert not any("accuse her" in t for t in told)
|
||||||
|
# ...and no state from the abandoned line leaked into it.
|
||||||
|
assert _gold(client.adv_id) == 1
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ L01
|
||||||
|
|
||||||
|
def test_l01_a_failed_turn_accepts_no_narration_and_strands_no_state(client):
|
||||||
|
"""A provider that fails must not leave the story half-advanced.
|
||||||
|
|
||||||
|
The head does move, by exactly one node, and that is deliberate rather than
|
||||||
|
a gap in the guarantee: A05 keeps the player's submitted text so it can be
|
||||||
|
tried again, and the head sits on it. What L01 forbids is an *accepted*
|
||||||
|
narration with half-written state behind it, a head that has moved past a
|
||||||
|
turn that did not happen, or earlier story becoming unreachable — so those
|
||||||
|
are what this asserts.
|
||||||
|
"""
|
||||||
|
_turns(client, 2)
|
||||||
|
branch, depth = _head(client.adv_id)
|
||||||
|
told = _texts(client)
|
||||||
|
banked = _gold(client.adv_id)
|
||||||
|
ai_before = [a.id for a in _rows(client.adv_id) if a.type == "ai"]
|
||||||
|
|
||||||
|
ScriptedProvider.replies = [ProviderError("the model refused")]
|
||||||
|
client.post(f"/api/adventures/{client.adv_id}/actions",
|
||||||
|
json={"type": "do", "text": "the turn that fails"})
|
||||||
|
|
||||||
|
# One step, onto the retained player action, on the same branch.
|
||||||
|
assert _head(client.adv_id) == (branch, depth + 1)
|
||||||
|
# No narration was accepted, and no state moved with a turn that did not
|
||||||
|
# finish.
|
||||||
|
assert [a.id for a in _rows(client.adv_id) if a.type == "ai"] == ai_before
|
||||||
|
assert _gold(client.adv_id) == banked
|
||||||
|
# Every earlier turn is still readable, in order, unchanged.
|
||||||
|
assert _texts(client)[:len(told)] == told
|
||||||
|
|
||||||
|
# And the story still moves: one Undo steps back over the stranded input
|
||||||
|
# and the position is a complete turn again.
|
||||||
|
ScriptedProvider.replies = gold_replies()
|
||||||
|
assert _undo(client).status_code == 200
|
||||||
|
assert _texts(client) == told
|
||||||
|
assert _gold(client.adv_id) == banked
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------- I01, I02, I03, I07: bundles
|
||||||
|
|
||||||
|
def _export(client) -> dict:
|
||||||
|
r = client.get(f"/api/adventures/{client.adv_id}/export")
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
|
||||||
|
def _import(client, bundle) -> dict:
|
||||||
|
"""Imports a bundle and returns the new adventure.
|
||||||
|
|
||||||
|
"A fresh data directory" is an adventure this file has never touched: the
|
||||||
|
import allocates its own branch rows and its own nodes, and resolves the
|
||||||
|
file's local branch numbers against them, which is the whole of what the
|
||||||
|
round trip has to get right. Sharing a database with the original does not
|
||||||
|
weaken that — the two adventures share no row.
|
||||||
|
"""
|
||||||
|
r = client.post("/api/adventures/import", json=bundle)
|
||||||
|
assert r.status_code == 201, r.text
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
|
||||||
|
def _story_of(adv_id) -> list[str]:
|
||||||
|
"""The story an imported adventure tells, read through its own head."""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
adventure = db.get(models.Adventure, adv_id)
|
||||||
|
rows = (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter(
|
||||||
|
models.Action.adventure_id == adv_id,
|
||||||
|
lineage.path_of(db, adventure).clause(models.Action),
|
||||||
|
)
|
||||||
|
.order_by(models.Action.depth, models.Action.id)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
return [a.text for a in rows]
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_i01_i02_a_campaign_round_trips(client):
|
||||||
|
_turns(client, 3)
|
||||||
|
told = _texts(client)
|
||||||
|
|
||||||
|
imported = _import(client, _export(client))
|
||||||
|
|
||||||
|
assert _story_of(imported["id"]) == told
|
||||||
|
assert _gold(imported["id"]) == 3 * GOLD_PER_TURN
|
||||||
|
|
||||||
|
|
||||||
|
def test_i03_the_bundle_carries_the_history_the_story_no_longer_tells(client):
|
||||||
|
"""A backup that keeps only the active line is not a backup: the retained
|
||||||
|
future is what Redo and every later recovery feature read."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_undo(client)
|
||||||
|
_play(client, "another way")
|
||||||
|
active = _texts(client)
|
||||||
|
|
||||||
|
bundle = _export(client)
|
||||||
|
assert len(bundle["branches"]) == 2
|
||||||
|
imported = _import(client, bundle)
|
||||||
|
|
||||||
|
assert _story_of(imported["id"]) == active
|
||||||
|
# Every row of the abandoned line arrived too, on its own branch.
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
assert (
|
||||||
|
db.query(models.Action).filter_by(adventure_id=imported["id"]).count()
|
||||||
|
== len(_rows(client.adv_id))
|
||||||
|
)
|
||||||
|
assert db.query(models.Branch).filter_by(adventure_id=imported["id"]).count() == 2
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_i07_an_undone_head_survives_export_and_import(client):
|
||||||
|
"""The Phase 0B finding this milestone exists to close. Before M3 the head
|
||||||
|
depth was derived on import from the newest retained row, so a campaign
|
||||||
|
exported after two Undos came back silently redone to its tip."""
|
||||||
|
_turns(client, 5)
|
||||||
|
whole = _texts(client)
|
||||||
|
_undo(client)
|
||||||
|
_undo(client)
|
||||||
|
undone = _texts(client)
|
||||||
|
assert len(undone) == len(whole) - 4
|
||||||
|
|
||||||
|
bundle = _export(client)
|
||||||
|
assert bundle["headDepth"] == _head(client.adv_id)[1]
|
||||||
|
|
||||||
|
imported = _import(client, bundle)
|
||||||
|
|
||||||
|
# It opens exactly where it was left...
|
||||||
|
assert _story_of(imported["id"]) == undone
|
||||||
|
assert imported["can_redo"] is True
|
||||||
|
# ...the later turns arrived as retained history...
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
assert (
|
||||||
|
db.query(models.Action).filter_by(adventure_id=imported["id"]).count()
|
||||||
|
== len(_rows(client.adv_id))
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
# ...and Redo still walks forward into them, twice, to the same story.
|
||||||
|
for _ in range(2):
|
||||||
|
r = client.post(f"/api/adventures/{imported['id']}/redo")
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
assert _story_of(imported["id"]) == whole
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_bundle_written_before_m3_opens_at_its_tip(client):
|
||||||
|
"""Backward compatibility. A file with no `headDepth` is one written when
|
||||||
|
the head could not be anywhere but the tip, so deriving it is not a
|
||||||
|
fallback — it is the position that file recorded."""
|
||||||
|
_turns(client, 3)
|
||||||
|
told = _texts(client)
|
||||||
|
bundle = _export(client)
|
||||||
|
del bundle["headDepth"]
|
||||||
|
|
||||||
|
imported = _import(client, bundle)
|
||||||
|
|
||||||
|
assert _story_of(imported["id"]) == told
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_bundle_that_reads_past_its_own_story_is_refused(client):
|
||||||
|
"""Checked in `plan`, before a row is written, for the reason the module
|
||||||
|
docstring gives: an import that discovers a bad coordinate afterwards has
|
||||||
|
already written half a tree."""
|
||||||
|
_turns(client, 2)
|
||||||
|
bundle = _export(client)
|
||||||
|
bundle["headDepth"] = 999
|
||||||
|
|
||||||
|
r = client.post("/api/adventures/import", json=bundle)
|
||||||
|
assert r.status_code == 400
|
||||||
|
assert "ends at" in r.json()["detail"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_bundle_carries_which_branches_the_story_left(client):
|
||||||
|
"""Every row of an abandoned line arrives on an import either way, so the
|
||||||
|
disposition is the only thing that distinguishes it from an active one.
|
||||||
|
Losing it on a round trip would leave a restored backup unable to tell them
|
||||||
|
apart — which is what the later cleanup and recovery features select on."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_undo(client)
|
||||||
|
_play(client, "another way")
|
||||||
|
|
||||||
|
imported = _import(client, _export(client))
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
left = (
|
||||||
|
db.query(models.Branch)
|
||||||
|
.filter(
|
||||||
|
models.Branch.adventure_id == imported["id"],
|
||||||
|
models.Branch.superseded_at.isnot(None),
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
origin = (
|
||||||
|
db.query(models.Branch)
|
||||||
|
.filter(
|
||||||
|
models.Branch.adventure_id == client.adv_id,
|
||||||
|
models.Branch.superseded_at.isnot(None),
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
assert len(left) == len(origin) == 1
|
||||||
|
assert left[0].superseded_depth == origin[0].superseded_depth
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_bundle_with_half_a_disposition_imports_as_active(client):
|
||||||
|
"""Neither column is read to decide anything, so a file carrying one of them
|
||||||
|
is taken as carrying neither rather than having its missing half invented."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_undo(client)
|
||||||
|
_play(client, "another way")
|
||||||
|
bundle = _export(client)
|
||||||
|
for branch in bundle["branches"]:
|
||||||
|
branch.pop("supersededDepth", None)
|
||||||
|
|
||||||
|
imported = _import(client, bundle)
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
assert (
|
||||||
|
db.query(models.Branch)
|
||||||
|
.filter(
|
||||||
|
models.Branch.adventure_id == imported["id"],
|
||||||
|
models.Branch.superseded_at.isnot(None),
|
||||||
|
)
|
||||||
|
.count()
|
||||||
|
== 0
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------- the narrator-edit guard (M3 closeout)
|
||||||
|
|
||||||
|
def _edit(client, action_id, text):
|
||||||
|
return client.patch(
|
||||||
|
f"/api/adventures/{client.adv_id}/actions/{action_id}", json={"text": text}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _live_ai_at(adv_id, depth):
|
||||||
|
return [
|
||||||
|
a for a in _rows(adv_id)
|
||||||
|
if a.type == "ai" and a.live and a.depth == depth
|
||||||
|
][0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_editing_a_turn_with_nothing_after_it_is_still_allowed(client):
|
||||||
|
"""The guard has to stay out of the way of the operation it protects. A
|
||||||
|
correction to the newest turn contradicts nothing, and that is the case the
|
||||||
|
edit control exists for."""
|
||||||
|
_turns(client, 2)
|
||||||
|
newest = [a for a in _rows(client.adv_id) if a.type == "ai"][-1]
|
||||||
|
|
||||||
|
r = _edit(client, newest.id, "Corrected.")
|
||||||
|
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
assert _texts(client)[-1] == "Corrected."
|
||||||
|
|
||||||
|
|
||||||
|
def test_editing_a_turn_on_the_visible_story_is_still_allowed(client):
|
||||||
|
"""Mid-story is not by itself unsafe: everything descending from the turn is
|
||||||
|
on screen, so the reader can see what their correction has to agree with.
|
||||||
|
Making that case fork is `STORY-BRANCH-SEMANTICS.md` §14-15 and belongs to
|
||||||
|
M5 with the state re-evaluation it needs."""
|
||||||
|
_turns(client, 3)
|
||||||
|
older = [a for a in _rows(client.adv_id) if a.type == "ai"][0]
|
||||||
|
|
||||||
|
r = _edit(client, older.id, "Mara wears a green cloak.")
|
||||||
|
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
assert "Mara wears a green cloak." in _texts(client)
|
||||||
|
|
||||||
|
|
||||||
|
def test_editing_a_turn_with_an_undone_future_is_refused(client):
|
||||||
|
"""The first unsafe case: the story past the head is not on screen, so an
|
||||||
|
edit here would silently change the words it was written from."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_undo(client)
|
||||||
|
at_head = [a for a in _rows(client.adv_id) if a.type == "ai" and a.live]
|
||||||
|
target = sorted(at_head, key=lambda a: a.depth)[-2]
|
||||||
|
before = target.text
|
||||||
|
|
||||||
|
r = _edit(client, target.id, "Something else entirely.")
|
||||||
|
|
||||||
|
assert r.status_code == 400
|
||||||
|
assert "not on screen" in r.json()["detail"]
|
||||||
|
# Refused, not partially applied.
|
||||||
|
assert _rows(client.adv_id)[0].adventure_id == client.adv_id
|
||||||
|
assert [a.text for a in _rows(client.adv_id) if a.id == target.id] == [before]
|
||||||
|
|
||||||
|
|
||||||
|
def test_editing_a_turn_a_divergence_left_behind_is_refused(client):
|
||||||
|
"""The second unsafe case, and the one a head check alone would miss: after
|
||||||
|
a divergence the head is back at a tip, but a displaced line still runs on
|
||||||
|
past the shared turn."""
|
||||||
|
_turns(client, 3)
|
||||||
|
shared = [a for a in _rows(client.adv_id) if a.type == "ai"][0]
|
||||||
|
_undo(client)
|
||||||
|
_undo(client)
|
||||||
|
_play(client, "a different road")
|
||||||
|
assert _adventure(client)["can_redo"] is False, "the head is at a tip again"
|
||||||
|
|
||||||
|
r = _edit(client, shared.id, "Rewritten under both lines.")
|
||||||
|
|
||||||
|
assert r.status_code == 400
|
||||||
|
assert "left behind by a new continuation" in r.json()["detail"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_turn_the_displaced_line_does_not_descend_from_is_still_editable(client):
|
||||||
|
"""The guard must be narrow. A branch that forked *before* a turn does not
|
||||||
|
descend from it, so a correction there contradicts nothing on that line."""
|
||||||
|
_turns(client, 2)
|
||||||
|
_undo(client)
|
||||||
|
_play(client, "a different road") # forks below turn 1
|
||||||
|
_turns(client, 2) # and continues past the fork
|
||||||
|
newest = [a for a in _rows(client.adv_id) if a.type == "ai" and a.live]
|
||||||
|
target = sorted(newest, key=lambda a: a.depth)[-1]
|
||||||
|
|
||||||
|
r = _edit(client, target.id, "Corrected on the live line.")
|
||||||
|
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_take_that_is_not_live_stays_editable(client):
|
||||||
|
"""A take the story is not telling has no continuation of its own — keeping
|
||||||
|
one is what forking is for — so correcting its text cannot contradict
|
||||||
|
anything."""
|
||||||
|
ScriptedProvider.replies = gold_replies()
|
||||||
|
_play(client, "knock")
|
||||||
|
client.post(f"/api/adventures/{client.adv_id}/retry")
|
||||||
|
discarded = [a for a in _rows(client.adv_id) if a.type == "ai" and not a.live][0]
|
||||||
|
|
||||||
|
r = _edit(client, discarded.id, "The take nobody chose, corrected.")
|
||||||
|
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_guard_lifts_when_the_story_is_brought_back(client):
|
||||||
|
"""Refusal is a redirection, not a dead end: the error names Redo, so Redo
|
||||||
|
has to make the edit possible again."""
|
||||||
|
_turns(client, 3)
|
||||||
|
_undo(client)
|
||||||
|
live = sorted(
|
||||||
|
[a for a in _rows(client.adv_id) if a.type == "ai" and a.live],
|
||||||
|
key=lambda a: a.depth,
|
||||||
|
)
|
||||||
|
target = live[-2]
|
||||||
|
assert _edit(client, target.id, "x").status_code == 400
|
||||||
|
|
||||||
|
_redo(client)
|
||||||
|
|
||||||
|
r = _edit(client, target.id, "Corrected once the story was whole again.")
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
@@ -0,0 +1,325 @@
|
|||||||
|
"""D11 and L03 across a genuine OS process boundary.
|
||||||
|
|
||||||
|
The rest of the suite runs the app in-process through `TestClient`, which is the
|
||||||
|
right tool for almost everything and the wrong one for exactly one claim:
|
||||||
|
*durability*. A Save Point that survived only because a Python object was still
|
||||||
|
alive would pass a same-process test and fail a user's restart. `TestClient`
|
||||||
|
cannot tell those apart, so the M4 review recorded the shipped D11/L03 tests as
|
||||||
|
weaker than the acceptance items they were named for (§R B-3).
|
||||||
|
|
||||||
|
This module closes that. It starts the real application as a **subprocess**,
|
||||||
|
plays a story over HTTP, kills the process, starts a **second** process against
|
||||||
|
the same database file, and only then asks whether the Save Point is still
|
||||||
|
there. Everything crossing the boundary crosses it as bytes on disk.
|
||||||
|
|
||||||
|
Deterministic and local: the spawned server replaces the model with a scripted
|
||||||
|
provider (`_restart_server.py`), so there is no Ollama, no network and no
|
||||||
|
sleep-and-hope — readiness is probed, not waited for.
|
||||||
|
|
||||||
|
python -m pytest tests/test_process_restart.py -v
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from fakes import GOLD_PER_TURN
|
||||||
|
|
||||||
|
HERE = Path(__file__).resolve().parent
|
||||||
|
SERVER = HERE / "_restart_server.py"
|
||||||
|
|
||||||
|
# How long a spawned server may take to answer before the test gives up. The
|
||||||
|
# process imports the app and runs migrations on a fresh file, which is well
|
||||||
|
# under a second on this project; the ceiling is for a loaded machine.
|
||||||
|
STARTUP_TIMEOUT = 60.0
|
||||||
|
|
||||||
|
|
||||||
|
def _free_port() -> int:
|
||||||
|
"""Returns a port nothing is listening on.
|
||||||
|
|
||||||
|
Bind, read, release. There is a race between releasing and the server
|
||||||
|
claiming it, which is why the caller probes for readiness rather than
|
||||||
|
assuming success — a lost race shows up as a startup timeout, not as a
|
||||||
|
silent pass.
|
||||||
|
"""
|
||||||
|
with socket.socket() as s:
|
||||||
|
s.bind(("127.0.0.1", 0))
|
||||||
|
return s.getsockname()[1]
|
||||||
|
|
||||||
|
|
||||||
|
class Server:
|
||||||
|
"""One storyteller process, and the HTTP calls the test makes against it."""
|
||||||
|
|
||||||
|
def __init__(self, db_path: str, port: int):
|
||||||
|
self.port = port
|
||||||
|
self.proc = subprocess.Popen(
|
||||||
|
[sys.executable, str(SERVER), db_path, str(port)],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
# Never inherit the parent's database redirection; the child is told
|
||||||
|
# which file to open on its command line.
|
||||||
|
env={**os.environ, "AIDND_DB_PATH": db_path},
|
||||||
|
)
|
||||||
|
|
||||||
|
# ------------------------------------------------------------ lifecycle
|
||||||
|
|
||||||
|
def wait_until_ready(self) -> None:
|
||||||
|
deadline = time.monotonic() + STARTUP_TIMEOUT
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
if self.proc.poll() is not None:
|
||||||
|
raise AssertionError(
|
||||||
|
f"server exited early ({self.proc.returncode}):\n{self._output()}"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
self.call("GET", "/settings")
|
||||||
|
return
|
||||||
|
except (urllib.error.URLError, ConnectionError, OSError):
|
||||||
|
time.sleep(0.05)
|
||||||
|
raise AssertionError(f"server never became ready:\n{self._output()}")
|
||||||
|
|
||||||
|
def stop(self) -> None:
|
||||||
|
"""Ends the process, and does not return until it is actually gone."""
|
||||||
|
if self.proc.poll() is None:
|
||||||
|
self.proc.terminate()
|
||||||
|
try:
|
||||||
|
self.proc.wait(timeout=15)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
self.proc.kill()
|
||||||
|
self.proc.wait(timeout=15)
|
||||||
|
if self.proc.stdout is not None:
|
||||||
|
self.proc.stdout.close()
|
||||||
|
|
||||||
|
def _output(self) -> str:
|
||||||
|
if self.proc.stdout is None:
|
||||||
|
return "(no output captured)"
|
||||||
|
try:
|
||||||
|
return self.proc.stdout.read().decode(errors="replace")[-2000:]
|
||||||
|
except Exception:
|
||||||
|
return "(output unreadable)"
|
||||||
|
|
||||||
|
def is_listening(self) -> bool:
|
||||||
|
try:
|
||||||
|
self.call("GET", "/settings")
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- HTTP
|
||||||
|
|
||||||
|
def call(self, method: str, path: str, payload=None, expect: int | None = None):
|
||||||
|
data = json.dumps(payload).encode() if payload is not None else None
|
||||||
|
request = urllib.request.Request(
|
||||||
|
f"http://127.0.0.1:{self.port}/api{path}",
|
||||||
|
data=data,
|
||||||
|
method=method,
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(request, timeout=60) as response:
|
||||||
|
body, status = response.read(), response.status
|
||||||
|
except urllib.error.HTTPError as exc: # a real answer, not a failure
|
||||||
|
body, status = exc.read(), exc.code
|
||||||
|
if expect is not None and status != expect:
|
||||||
|
raise AssertionError(f"{method} {path} -> {status}: {body[:400]!r}")
|
||||||
|
return json.loads(body) if body and status != 204 else None
|
||||||
|
|
||||||
|
def play(self, adventure_id: int, text: str) -> None:
|
||||||
|
"""Plays one turn through the streaming endpoint, to completion."""
|
||||||
|
request = urllib.request.Request(
|
||||||
|
f"http://127.0.0.1:{self.port}/api/adventures/{adventure_id}/actions",
|
||||||
|
data=json.dumps({"type": "do", "text": text}).encode(),
|
||||||
|
method="POST",
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
with urllib.request.urlopen(request, timeout=120) as response:
|
||||||
|
response.read()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------- reading
|
||||||
|
|
||||||
|
def transcript(self, adventure_id: int) -> list[str]:
|
||||||
|
page = self.call("GET", f"/adventures/{adventure_id}", expect=200)
|
||||||
|
return [a["text"] for a in page["actions"]]
|
||||||
|
|
||||||
|
def gold(self, adventure_id: int) -> int:
|
||||||
|
state = self.call("GET", f"/adventures/{adventure_id}/world-state", expect=200)
|
||||||
|
return state["state"]["player"]["gold"]
|
||||||
|
|
||||||
|
def total_rows(self, adventure_id: int) -> int:
|
||||||
|
"""Every row of the whole tree, head or no head.
|
||||||
|
|
||||||
|
`action_count` is scoped to the path being read, so it falls when the
|
||||||
|
head moves back — which is the feature, not a deletion. The export
|
||||||
|
carries the entire tree whatever the head is doing, so it is what
|
||||||
|
"nothing was deleted" has to be measured against.
|
||||||
|
"""
|
||||||
|
bundle = self.call("GET", f"/adventures/{adventure_id}/export", expect=200)
|
||||||
|
return len(bundle["actions"])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def workspace():
|
||||||
|
"""A database file, and whichever servers a test starts against it."""
|
||||||
|
directory = tempfile.mkdtemp(prefix="m4-restart-")
|
||||||
|
db_path = os.path.join(directory, "campaign.db")
|
||||||
|
started: list[Server] = []
|
||||||
|
|
||||||
|
def start() -> Server:
|
||||||
|
server = Server(db_path, _free_port())
|
||||||
|
started.append(server)
|
||||||
|
server.wait_until_ready()
|
||||||
|
return server
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield start
|
||||||
|
finally:
|
||||||
|
# Every child dies even if the test failed part way through, and each
|
||||||
|
# stop() waits, so a later test cannot inherit a live listener.
|
||||||
|
for server in started:
|
||||||
|
server.stop()
|
||||||
|
|
||||||
|
|
||||||
|
def _campaign_with_a_save_point(server: Server):
|
||||||
|
"""Three turns, a Save Point on the third, then four more turns.
|
||||||
|
|
||||||
|
Returns everything the second process has to be able to reproduce.
|
||||||
|
"""
|
||||||
|
scenario = server.call("POST", "/scenarios", {
|
||||||
|
"title": "Abbey",
|
||||||
|
"stat_schema": {"player": {"gold": {"initial": 0, "min": 0, "max": 9999}}},
|
||||||
|
}, expect=201)
|
||||||
|
adventure = server.call("POST", "/adventures", {
|
||||||
|
"title": "The abbey", "scenario_id": scenario["id"],
|
||||||
|
}, expect=201)
|
||||||
|
adventure_id = adventure["id"]
|
||||||
|
|
||||||
|
for n in range(3):
|
||||||
|
server.play(adventure_id, f"turn {n}")
|
||||||
|
at_save = {
|
||||||
|
"transcript": server.transcript(adventure_id),
|
||||||
|
"gold": server.gold(adventure_id),
|
||||||
|
}
|
||||||
|
save_point = server.call("POST", f"/adventures/{adventure_id}/checkpoints",
|
||||||
|
{"name": "Before entering the abbey"}, expect=201)
|
||||||
|
|
||||||
|
for n in range(4):
|
||||||
|
server.play(adventure_id, f"later {n}")
|
||||||
|
at_tip = {
|
||||||
|
"transcript": server.transcript(adventure_id),
|
||||||
|
"gold": server.gold(adventure_id),
|
||||||
|
"rows": server.total_rows(adventure_id),
|
||||||
|
}
|
||||||
|
return adventure_id, save_point, at_save, at_tip
|
||||||
|
|
||||||
|
|
||||||
|
def test_d11_l03_a_save_point_survives_a_real_process_restart(workspace):
|
||||||
|
"""D11 and L03 together, across a boundary a same-process test cannot cross.
|
||||||
|
|
||||||
|
The first process writes the campaign and exits. The second process is a
|
||||||
|
different interpreter with an empty session, an empty identity map and no
|
||||||
|
memory of anything — everything it knows, it reads off the disk.
|
||||||
|
"""
|
||||||
|
first = workspace()
|
||||||
|
adventure_id, save_point, at_save, at_tip = _campaign_with_a_save_point(first)
|
||||||
|
|
||||||
|
assert at_tip["gold"] == at_save["gold"] + 4 * GOLD_PER_TURN
|
||||||
|
assert len(at_tip["transcript"]) == len(at_save["transcript"]) + 8
|
||||||
|
|
||||||
|
# --- the boundary -----------------------------------------------------
|
||||||
|
first.stop()
|
||||||
|
assert first.proc.poll() is not None, "the first server did not actually exit"
|
||||||
|
assert not first.is_listening(), "the first server is still answering"
|
||||||
|
|
||||||
|
second = workspace()
|
||||||
|
assert second.proc.pid != first.proc.pid
|
||||||
|
|
||||||
|
# --- D11: the Save Point is still there -------------------------------
|
||||||
|
listed = second.call("GET", f"/adventures/{adventure_id}/checkpoints", expect=200)
|
||||||
|
assert [c["id"] for c in listed] == [save_point["id"]]
|
||||||
|
assert listed[0]["name"] == "Before entering the abbey"
|
||||||
|
assert (listed[0]["branch_id"], listed[0]["depth"]) == (
|
||||||
|
save_point["branch_id"], save_point["depth"]
|
||||||
|
)
|
||||||
|
assert listed[0]["resolved"] is True
|
||||||
|
# The story came back too, at the position the first process left it.
|
||||||
|
assert second.transcript(adventure_id) == at_tip["transcript"]
|
||||||
|
|
||||||
|
rows_before_restore = second.total_rows(adventure_id)
|
||||||
|
assert rows_before_restore == at_tip["rows"]
|
||||||
|
|
||||||
|
# --- L03: restoring reconstructs the historical position --------------
|
||||||
|
page = second.call(
|
||||||
|
"POST", f"/adventures/{adventure_id}/checkpoints/{save_point['id']}/restore",
|
||||||
|
expect=200,
|
||||||
|
)
|
||||||
|
assert [a["text"] for a in page["actions"]] == at_save["transcript"]
|
||||||
|
assert second.transcript(adventure_id) == at_save["transcript"]
|
||||||
|
assert second.gold(adventure_id) == at_save["gold"]
|
||||||
|
|
||||||
|
# --- restore deleted nothing, and Redo is still available -------------
|
||||||
|
assert second.total_rows(adventure_id) == rows_before_restore
|
||||||
|
assert page["can_redo"] is True
|
||||||
|
assert second.call("GET", f"/adventures/{adventure_id}", expect=200)["can_redo"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_retained_continuation_is_reachable_after_a_restart(workspace):
|
||||||
|
"""The later history is not merely present in the database after a restart —
|
||||||
|
it is still the continuation the story tells, walkable by ordinary Redo."""
|
||||||
|
first = workspace()
|
||||||
|
adventure_id, save_point, at_save, at_tip = _campaign_with_a_save_point(first)
|
||||||
|
first.stop()
|
||||||
|
|
||||||
|
second = workspace()
|
||||||
|
second.call("POST",
|
||||||
|
f"/adventures/{adventure_id}/checkpoints/{save_point['id']}/restore",
|
||||||
|
expect=200)
|
||||||
|
assert second.transcript(adventure_id) == at_save["transcript"]
|
||||||
|
|
||||||
|
steps = 0
|
||||||
|
while second.call("GET", f"/adventures/{adventure_id}", expect=200)["can_redo"]:
|
||||||
|
second.call("POST", f"/adventures/{adventure_id}/redo", expect=200)
|
||||||
|
steps += 1
|
||||||
|
assert steps <= 10, "Redo never stopped"
|
||||||
|
|
||||||
|
assert steps == 4
|
||||||
|
assert second.transcript(adventure_id) == at_tip["transcript"]
|
||||||
|
assert second.gold(adventure_id) == at_tip["gold"]
|
||||||
|
assert second.total_rows(adventure_id) == at_tip["rows"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_divergent_write_after_a_restart_keeps_the_displaced_future(workspace):
|
||||||
|
"""D13's second half, across the boundary: the fork still happens on the
|
||||||
|
write rather than on the restore, and the displaced turns keep their rows."""
|
||||||
|
first = workspace()
|
||||||
|
adventure_id, save_point, at_save, at_tip = _campaign_with_a_save_point(first)
|
||||||
|
first.stop()
|
||||||
|
|
||||||
|
second = workspace()
|
||||||
|
second.call("POST",
|
||||||
|
f"/adventures/{adventure_id}/checkpoints/{save_point['id']}/restore",
|
||||||
|
expect=200)
|
||||||
|
branches_before = second.call("GET", f"/adventures/{adventure_id}/branches",
|
||||||
|
expect=200)
|
||||||
|
assert len(branches_before) == 1, "restore must not fork"
|
||||||
|
|
||||||
|
second.play(adventure_id, "go around the back")
|
||||||
|
|
||||||
|
branches_after = second.call("GET", f"/adventures/{adventure_id}/branches",
|
||||||
|
expect=200)
|
||||||
|
assert len(branches_after) == 2, "the write should have forked"
|
||||||
|
# Nothing was deleted to achieve it: the tree grew by the new turn only.
|
||||||
|
assert second.total_rows(adventure_id) == at_tip["rows"] + 2
|
||||||
|
# Ordinary Redo no longer offers the displaced future.
|
||||||
|
assert second.call("GET", f"/adventures/{adventure_id}", expect=200)["can_redo"] is False
|
||||||
|
# And the Save Point still names the position it always did.
|
||||||
|
listed = second.call("GET", f"/adventures/{adventure_id}/checkpoints", expect=200)
|
||||||
|
assert (listed[0]["branch_id"], listed[0]["depth"]) == (
|
||||||
|
save_point["branch_id"], save_point["depth"]
|
||||||
|
)
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
"""Tests for undo and retry rolling back the shared `world_state`
|
"""Tests for undo and retry rolling back the shared `world_state`.
|
||||||
(plan/11-state-revert-and-retry-fix.md).
|
|
||||||
|
|
||||||
The state being rolled back was the scripting engine's `script_state` until
|
The state being rolled back was the scripting engine's `script_state` until
|
||||||
M2 removed campaign scripting. The machinery under test — `attempts.restore_state`,
|
M2 removed campaign scripting. The machinery under test — `attempts.restore_state`,
|
||||||
@@ -62,6 +61,39 @@ def _add(db, adv, index, type_, text="x", state_after=None):
|
|||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
|
def _row_count(db, adv):
|
||||||
|
"""Every action row the adventure holds, live or not, head or no head."""
|
||||||
|
return db.query(models.Action).filter_by(adventure_id=adv.id).count()
|
||||||
|
|
||||||
|
|
||||||
|
def _all_types(db, adv):
|
||||||
|
"""The retained story in depth order, which is not the same as the story
|
||||||
|
being told once the head has moved back behind the tip."""
|
||||||
|
rows = (
|
||||||
|
db.query(models.Action)
|
||||||
|
.filter_by(adventure_id=adv.id)
|
||||||
|
.order_by(models.Action.depth, models.Action.id)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
return [a.type for a in rows]
|
||||||
|
|
||||||
|
|
||||||
|
def _retrievable(db, adv):
|
||||||
|
"""The memories the story can currently reach, read through the same clause
|
||||||
|
`memorybank` retrieves with — which is capped at the active head."""
|
||||||
|
from app.context import lineage
|
||||||
|
|
||||||
|
rows = (
|
||||||
|
db.query(models.Memory)
|
||||||
|
.filter(
|
||||||
|
models.Memory.adventure_id == adv.id,
|
||||||
|
lineage.path_of(db, adv).clause(models.Memory),
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
return {m.text for m in rows}
|
||||||
|
|
||||||
|
|
||||||
def _forget_snapshots(db, adv):
|
def _forget_snapshots(db, adv):
|
||||||
"""Blank every outcome, the way a row written before SP4 looks.
|
"""Blank every outcome, the way a row written before SP4 looks.
|
||||||
|
|
||||||
@@ -81,16 +113,31 @@ def test_undo_reverts_state_to_before_the_turn(db):
|
|||||||
# A turn moved world_state from {gold:0} to {gold:10}. The node in
|
# A turn moved world_state from {gold:0} to {gold:10}. The node in
|
||||||
# front of the turn records where it started. The current state is
|
# front of the turn records where it started. The current state is
|
||||||
# the mutated one.
|
# the mutated one.
|
||||||
|
#
|
||||||
|
# M3 rewrote what the second half of this test asserts. Undo used to delete
|
||||||
|
# the turn, so the story was short afterwards because the rows were gone.
|
||||||
|
# It now moves the head, so the story is short because it is being read
|
||||||
|
# from somewhere earlier — and the rows are all still there. The state
|
||||||
|
# assertion is unchanged, because `attempts.restore_state` is unchanged:
|
||||||
|
# the state still comes off the node the story now ends on.
|
||||||
user, adv = _make_adventure(db, {"gold": 10})
|
user, adv = _make_adventure(db, {"gold": 10})
|
||||||
_add(db, adv, 0, "start", state_after={"gold": 0})
|
_add(db, adv, 0, "start", state_after={"gold": 0})
|
||||||
_add(db, adv, 1, "do", state_after={"gold": 0})
|
_add(db, adv, 1, "do", state_after={"gold": 0})
|
||||||
_add(db, adv, 2, "ai", state_after={"gold": 10})
|
_add(db, adv, 2, "ai", state_after={"gold": 10})
|
||||||
db.commit()
|
db.commit()
|
||||||
|
before = _row_count(db, adv)
|
||||||
|
|
||||||
adventures.undo_turn(adv.id, db=db, adventure=adv)
|
page = adventures.undo_turn(adv.id, db=db, adventure=adv)
|
||||||
|
|
||||||
assert adv.world_state == {"gold": 0}
|
assert adv.world_state == {"gold": 0}
|
||||||
assert [a.type for a in adv.actions] == ["start"]
|
# What the story now tells.
|
||||||
|
assert [a.type for a in page.actions] == ["start"]
|
||||||
|
assert adv.head_depth == 0
|
||||||
|
# What it still holds. Zero accepted turns deleted, which is the M3
|
||||||
|
# invariant this file is the closest test to.
|
||||||
|
assert _row_count(db, adv) == before
|
||||||
|
assert _all_types(db, adv) == ["start", "do", "ai"]
|
||||||
|
assert page.can_redo is True
|
||||||
|
|
||||||
|
|
||||||
def test_undo_of_bare_continue_uses_the_node_in_front(db):
|
def test_undo_of_bare_continue_uses_the_node_in_front(db):
|
||||||
@@ -101,10 +148,30 @@ def test_undo_of_bare_continue_uses_the_node_in_front(db):
|
|||||||
_add(db, adv, 1, "ai", state_after={"gold": 5})
|
_add(db, adv, 1, "ai", state_after={"gold": 5})
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
adventures.undo_turn(adv.id, db=db, adventure=adv)
|
page = adventures.undo_turn(adv.id, db=db, adventure=adv)
|
||||||
|
|
||||||
assert adv.world_state == {"gold": 0}
|
assert adv.world_state == {"gold": 0}
|
||||||
assert [a.type for a in adv.actions] == ["start"]
|
assert [a.type for a in page.actions] == ["start"]
|
||||||
|
assert _all_types(db, adv) == ["start", "ai"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_redo_puts_back_the_state_the_turn_left_behind(db):
|
||||||
|
"""The other half of the same mechanism: undo and redo restore the same
|
||||||
|
snapshot from opposite directions, because it belongs to the node rather
|
||||||
|
than to the direction of travel."""
|
||||||
|
user, adv = _make_adventure(db, {"gold": 10})
|
||||||
|
_add(db, adv, 0, "start", state_after={"gold": 0})
|
||||||
|
_add(db, adv, 1, "do", state_after={"gold": 0})
|
||||||
|
_add(db, adv, 2, "ai", state_after={"gold": 10})
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
adventures.undo_turn(adv.id, db=db, adventure=adv)
|
||||||
|
page = adventures.redo_turn(adv.id, db=db, adventure=adv)
|
||||||
|
|
||||||
|
assert adv.world_state == {"gold": 10}
|
||||||
|
assert [a.type for a in page.actions] == ["start", "do", "ai"]
|
||||||
|
assert adv.head_depth == 2
|
||||||
|
assert page.can_redo is False
|
||||||
|
|
||||||
|
|
||||||
def test_undo_leaves_state_untouched_when_snapshot_missing(db):
|
def test_undo_leaves_state_untouched_when_snapshot_missing(db):
|
||||||
@@ -147,20 +214,45 @@ def test_undo_blocked_by_active_turn_lock(db):
|
|||||||
adventures.turns._active_turns.discard(adv.id)
|
adventures.turns._active_turns.discard(adv.id)
|
||||||
|
|
||||||
|
|
||||||
def test_undo_prunes_memory_covering_removed_actions(db):
|
def test_undo_stops_retrieving_a_memory_without_deleting_it(db):
|
||||||
|
"""M3 rewrote this test. Undo used to prune the memories covering the turns
|
||||||
|
it deleted, because those turns were gone and a summary of them described
|
||||||
|
story the adventure no longer had.
|
||||||
|
|
||||||
|
Nothing is deleted now, and nothing needs pruning either. A memory carries
|
||||||
|
the coordinate of the node its block ends on, so one derived from a turn
|
||||||
|
that is now past the head falls outside the head-capped path clause and
|
||||||
|
stops being retrievable — and becomes eligible again on Redo, without having
|
||||||
|
been deleted and re-embedded. That is `STORY-BRANCH-SEMANTICS.md` §33
|
||||||
|
holding as a consequence of the head rather than as its own mechanism.
|
||||||
|
"""
|
||||||
user, adv = _make_adventure(db, {})
|
user, adv = _make_adventure(db, {})
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
_add(db, adv, i, "ai" if i % 2 else "do", state_after={})
|
_add(db, adv, i, "ai" if i % 2 else "do", state_after={})
|
||||||
# A memory summarizing actions up to index 3, which undo will delete.
|
db.commit()
|
||||||
covering = models.Memory(adventure_id=adv.id, text="m", source_start=0, source_end=3)
|
# A memory ending on the turn undo will step behind, and one ending before
|
||||||
keep = models.Memory(adventure_id=adv.id, text="k", source_start=0, source_end=1)
|
# it. The coordinate is what the clause reads; `source_*` only says which
|
||||||
|
# stretch the summarizer covered.
|
||||||
|
covering = models.Memory(
|
||||||
|
adventure_id=adv.id, text="m", source_start=0, source_end=3,
|
||||||
|
branch_id=adv.head_branch_id, depth=3,
|
||||||
|
)
|
||||||
|
keep = models.Memory(
|
||||||
|
adventure_id=adv.id, text="k", source_start=0, source_end=1,
|
||||||
|
branch_id=adv.head_branch_id, depth=1,
|
||||||
|
)
|
||||||
db.add_all([covering, keep])
|
db.add_all([covering, keep])
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
adventures.undo_turn(adv.id, db=db, adventure=adv) # removes indexes 2 & 3
|
adventures.undo_turn(adv.id, db=db, adventure=adv) # head moves to depth 1
|
||||||
|
|
||||||
texts = {m.text for m in adv.memories}
|
assert _retrievable(db, adv) == {"k"}
|
||||||
assert texts == {"k"}
|
# Still on disk, still embedded, still attached to the adventure.
|
||||||
|
assert {m.text for m in adv.memories} == {"k", "m"}
|
||||||
|
|
||||||
|
adventures.redo_turn(adv.id, db=db, adventure=adv)
|
||||||
|
|
||||||
|
assert _retrievable(db, adv) == {"k", "m"}
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------------------------------- withdrawing a node
|
# -------------------------------------------------------- withdrawing a node
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ This file must pass unmodified through SP1 (schema), SP2 (branch clause),
|
|||||||
and SP3 (memories on nodes). If a change here looks necessary in one of
|
and SP3 (memories on nodes). If a change here looks necessary in one of
|
||||||
those subphases, the change is wrong, not the test. SP4 is the first
|
those subphases, the change is wrong, not the test. SP4 is the first
|
||||||
subphase allowed to move it, and only for the variant-count semantics
|
subphase allowed to move it, and only for the variant-count semantics
|
||||||
called out in plan/14.
|
the story-tree migration changed.
|
||||||
"""
|
"""
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
|
|||||||
@@ -31,8 +31,7 @@ from app.main import app
|
|||||||
# The three tables as they stood at schema 45, frozen. This is a snapshot
|
# The three tables as they stood at schema 45, frozen. This is a snapshot
|
||||||
# of a past schema. It must not be updated to track `models.py`, because
|
# of a past schema. It must not be updated to track `models.py`, because
|
||||||
# the whole point is that it lacks what SP1 adds. This DDL uses SQLite
|
# the whole point is that it lacks what SP1 adds. This DDL uses SQLite
|
||||||
# syntax only. The migration's Postgres half is exercised against a real
|
# syntax only.
|
||||||
# server at deploy time (see plan/14).
|
|
||||||
PRE_TREE_DDL = (
|
PRE_TREE_DDL = (
|
||||||
"""
|
"""
|
||||||
CREATE TABLE adventures (
|
CREATE TABLE adventures (
|
||||||
@@ -120,7 +119,12 @@ def pre_tree():
|
|||||||
# Dropping the tables is the only way to remove the columns.
|
# Dropping the tables is the only way to remove the columns.
|
||||||
# SQLite refuses to drop a column that a foreign key references,
|
# SQLite refuses to drop a column that a foreign key references,
|
||||||
# and that is exactly the case for `branch_id`.
|
# and that is exactly the case for `branch_id`.
|
||||||
for table in ("actions", "memories", "branches", "adventures"):
|
#
|
||||||
|
# `checkpoints` (M4) is dropped first and for a different reason: it
|
||||||
|
# references both `branches` and `adventures`, and SQLite refuses to
|
||||||
|
# drop a table another table still points at. Any future table that
|
||||||
|
# references these four has to be added to the front of this list.
|
||||||
|
for table in ("checkpoints", "actions", "memories", "branches", "adventures"):
|
||||||
conn.execute(text(f"DROP TABLE IF EXISTS {table}"))
|
conn.execute(text(f"DROP TABLE IF EXISTS {table}"))
|
||||||
for ddl in PRE_TREE_DDL:
|
for ddl in PRE_TREE_DDL:
|
||||||
conn.execute(text(ddl))
|
conn.execute(text(ddl))
|
||||||
@@ -593,7 +597,7 @@ def pre_split():
|
|||||||
Base.metadata.drop_all(bind=engine)
|
Base.metadata.drop_all(bind=engine)
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
with engine.begin() as conn:
|
with engine.begin() as conn:
|
||||||
for table in ("actions", "memories", "branches", "adventures"):
|
for table in ("checkpoints", "actions", "memories", "branches", "adventures"):
|
||||||
conn.execute(text(f"DROP TABLE IF EXISTS {table}"))
|
conn.execute(text(f"DROP TABLE IF EXISTS {table}"))
|
||||||
for ddl in PRE_TREE_DDL:
|
for ddl in PRE_TREE_DDL:
|
||||||
conn.execute(text(ddl))
|
conn.execute(text(ddl))
|
||||||
|
|||||||
@@ -4,10 +4,9 @@ Reviewing a prompt tells you what it asks for. It does not tell you what a model
|
|||||||
does with it. This runs the real pipeline twice over the *same* blocks of the
|
does with it. This runs the real pipeline twice over the *same* blocks of the
|
||||||
*same* story, changing only the prompt, and prints the memories side by side.
|
*same* story, changing only the prompt, and prints the memories side by side.
|
||||||
|
|
||||||
What it showed the first time it was run, and why `MEMORY_MAX_WORDS` exists, is
|
Why `MEMORY_MAX_WORDS` exists is what it showed the first time it was run: two
|
||||||
written up in `plan/18-persona-and-memory-quality.md`. Two consecutive memories
|
consecutive memories from one story came back in two different persons, and the
|
||||||
from one story came back in two different persons, and the same model wrote 34
|
same model wrote 34 words for one block and 105 for the next.
|
||||||
words for one block and 105 for the next.
|
|
||||||
|
|
||||||
Three things make it a fair test rather than a demonstration:
|
Three things make it a fair test rather than a demonstration:
|
||||||
|
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 103 KiB |
|
Before Width: | Height: | Size: 66 KiB |
|
Before Width: | Height: | Size: 157 KiB |
|
Before Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 84 KiB |
|
Before Width: | Height: | Size: 59 KiB |
@@ -1,267 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<title>AI D&D — an AI Dungeon-style storytelling engine</title>
|
|
||||||
<meta name="description" content="An AI Dungeon-style interactive storytelling app. FastAPI + React, any OpenAI-compatible model, an RPG world-state engine the AI proposes and Python referees, and a quickjs sandbox that runs real AI Dungeon scripts.">
|
|
||||||
<meta property="og:title" content="AI D&D — an AI Dungeon-style storytelling engine">
|
|
||||||
<meta property="og:description" content="Play open-ended adventures narrated by an LLM, with a world-state engine that keeps the numbers honest.">
|
|
||||||
<meta property="og:image" content="https://parththakkar106.github.io/AI-DnD/images/play-world-state.jpg">
|
|
||||||
<meta property="og:type" content="website">
|
|
||||||
<meta name="twitter:card" content="summary_large_image">
|
|
||||||
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='.9em' font-size='90'%3E%E2%9A%94%3C/text%3E%3C/svg%3E">
|
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@500;700&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
|
|
||||||
<style>
|
|
||||||
:root {
|
|
||||||
--bg: #0a0a0f;
|
|
||||||
--panel: #131320;
|
|
||||||
--border: #2b2b3d;
|
|
||||||
--border-bright: #3d3d55;
|
|
||||||
--text: #e2ddd0;
|
|
||||||
--dim: #918c7d;
|
|
||||||
--accent: #d4a94e;
|
|
||||||
--accent-bright: #e8c476;
|
|
||||||
--accent-dim: #96773a;
|
|
||||||
--display: 'Cinzel', Georgia, serif;
|
|
||||||
--ui: 'Inter', 'Segoe UI', system-ui, sans-serif;
|
|
||||||
color-scheme: dark;
|
|
||||||
}
|
|
||||||
* { box-sizing: border-box; }
|
|
||||||
body {
|
|
||||||
margin: 0;
|
|
||||||
background:
|
|
||||||
radial-gradient(1200px 700px at 15% -10%, rgba(212,169,78,.06), transparent 60%),
|
|
||||||
radial-gradient(1000px 600px at 90% 110%, rgba(88,76,140,.08), transparent 55%),
|
|
||||||
var(--bg);
|
|
||||||
background-attachment: fixed;
|
|
||||||
color: var(--text);
|
|
||||||
font-family: var(--ui);
|
|
||||||
line-height: 1.65;
|
|
||||||
-webkit-font-smoothing: antialiased;
|
|
||||||
}
|
|
||||||
.wrap { max-width: 1080px; margin: 0 auto; padding: 0 24px; }
|
|
||||||
a { color: var(--accent-bright); }
|
|
||||||
|
|
||||||
header { padding: 72px 0 40px; text-align: center; }
|
|
||||||
.mark { font-family: var(--display); font-size: 14px; letter-spacing: .28em; color: var(--accent); text-transform: uppercase; }
|
|
||||||
h1 {
|
|
||||||
font-family: var(--display); font-weight: 700;
|
|
||||||
font-size: clamp(2.4rem, 6vw, 4rem); margin: .2em 0 .1em; letter-spacing: .02em;
|
|
||||||
background: linear-gradient(180deg, var(--accent-bright), var(--accent));
|
|
||||||
-webkit-background-clip: text; background-clip: text; color: transparent;
|
|
||||||
}
|
|
||||||
.tagline { font-size: clamp(1.05rem, 2.2vw, 1.3rem); color: var(--text); max-width: 46ch; margin: .6em auto 0; }
|
|
||||||
.sub { color: var(--dim); max-width: 60ch; margin: 1em auto 0; font-size: .97rem; }
|
|
||||||
|
|
||||||
.cta { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; margin: 32px 0 10px; }
|
|
||||||
.btn {
|
|
||||||
display: inline-block; padding: 13px 26px; border-radius: 8px; text-decoration: none;
|
|
||||||
font-weight: 600; font-size: 1rem; border: 1px solid var(--border-bright); transition: .18s;
|
|
||||||
}
|
|
||||||
.btn-primary { background: linear-gradient(180deg, var(--accent-bright), var(--accent)); color: #17130a; border-color: var(--accent); }
|
|
||||||
.btn-primary:hover { filter: brightness(1.08); transform: translateY(-1px); }
|
|
||||||
.btn-ghost { background: var(--panel); color: var(--text); }
|
|
||||||
.btn-ghost:hover { border-color: var(--accent); color: var(--accent-bright); }
|
|
||||||
.wake { color: var(--dim); font-size: .85rem; text-align: center; margin-top: 4px; }
|
|
||||||
|
|
||||||
figure { margin: 0; }
|
|
||||||
figure img {
|
|
||||||
width: 100%; height: auto; display: block; border-radius: 10px;
|
|
||||||
border: 1px solid var(--border); box-shadow: 0 24px 60px rgba(0,0,0,.55);
|
|
||||||
}
|
|
||||||
figcaption { color: var(--dim); font-size: .88rem; margin-top: 12px; }
|
|
||||||
.hero-shot { margin: 44px 0 8px; }
|
|
||||||
|
|
||||||
section { padding: 56px 0; border-top: 1px solid var(--border); margin-top: 56px; }
|
|
||||||
h2 {
|
|
||||||
font-family: var(--display); font-size: 1.6rem; font-weight: 700;
|
|
||||||
color: var(--accent); margin: 0 0 8px; letter-spacing: .02em;
|
|
||||||
}
|
|
||||||
.lede { color: var(--dim); margin: 0 0 32px; max-width: 68ch; }
|
|
||||||
|
|
||||||
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 32px; }
|
|
||||||
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 22px; }
|
|
||||||
.card h3 { font-family: var(--display); font-size: 1.12rem; margin: 0 0 8px; color: var(--text); }
|
|
||||||
.card p { margin: 0 0 16px; color: var(--dim); font-size: .95rem; }
|
|
||||||
.card p:last-child { margin-bottom: 0; }
|
|
||||||
.card img { border-radius: 8px; border: 1px solid var(--border); width: 100%; height: auto; display: block; }
|
|
||||||
|
|
||||||
pre {
|
|
||||||
background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
|
|
||||||
padding: 20px; overflow-x: auto; font-size: .86rem; line-height: 1.7; color: var(--text);
|
|
||||||
}
|
|
||||||
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
|
||||||
|
|
||||||
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 20px; margin-top: 8px; }
|
|
||||||
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 20px; }
|
|
||||||
.stat .n { font-family: var(--display); font-size: 1.9rem; color: var(--accent-bright); line-height: 1.1; }
|
|
||||||
.stat .l { color: var(--dim); font-size: .88rem; margin-top: 6px; }
|
|
||||||
|
|
||||||
ul.notes { padding-left: 0; list-style: none; margin: 0; }
|
|
||||||
ul.notes li { border-left: 2px solid var(--accent-dim, #96773a); padding: 2px 0 2px 18px; margin-bottom: 22px; color: var(--dim); }
|
|
||||||
ul.notes strong { color: var(--text); }
|
|
||||||
|
|
||||||
.stack { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; }
|
|
||||||
.chip { background: var(--panel); border: 1px solid var(--border); border-radius: 999px; padding: 5px 14px; font-size: .85rem; color: var(--dim); }
|
|
||||||
|
|
||||||
footer { border-top: 1px solid var(--border); margin-top: 56px; padding: 36px 0 64px; color: var(--dim); font-size: .9rem; text-align: center; }
|
|
||||||
|
|
||||||
@media (max-width: 720px) {
|
|
||||||
header { padding: 48px 0 24px; }
|
|
||||||
section { padding: 40px 0; margin-top: 40px; }
|
|
||||||
.btn { width: 100%; }
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div class="wrap">
|
|
||||||
|
|
||||||
<header>
|
|
||||||
<div class="mark">⚔ Interactive fiction, refereed</div>
|
|
||||||
<h1>AI D&D</h1>
|
|
||||||
<p class="tagline">Open-ended adventures narrated by an LLM — with an engine that keeps the numbers honest.</p>
|
|
||||||
<p class="sub">Create a world, play it in second person, and let the model improvise the story while a Python
|
|
||||||
referee enforces what's actually true: hit points, an ally's trust, a raised alarm, a quest milestone.
|
|
||||||
Bring your own model, or play the demo with none.</p>
|
|
||||||
|
|
||||||
<div class="cta">
|
|
||||||
<a class="btn btn-primary" href="https://ai-dnd-1gmp.onrender.com">Launch the live demo →</a>
|
|
||||||
<a class="btn btn-ghost" href="guide.html">Read the design notes</a>
|
|
||||||
<a class="btn btn-ghost" href="architecture.html">HLD & LLD walkthrough</a>
|
|
||||||
<a class="btn btn-ghost" href="https://github.com/parththakkar106/AI-DnD">View the source</a>
|
|
||||||
</div>
|
|
||||||
<p class="wake">No sign-up, no API key. Hosted on a free tier that sleeps — the first load takes ~30–60s to wake.</p>
|
|
||||||
|
|
||||||
<figure class="hero-shot">
|
|
||||||
<img src="images/play-world-state.jpg" alt="The play screen with the world-state rail open, showing HP, mana, an NPC's trust and a raised alarm flag">
|
|
||||||
<figcaption>The left rail is live world state. The model proposes what changed this turn; the engine decides
|
|
||||||
what sticks, and the chip under the narration reports the result. The <code>‹ 2/2 ›</code>
|
|
||||||
under a turn steps between the takes it has.</figcaption>
|
|
||||||
</figure>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<section>
|
|
||||||
<h2>What makes it more than a chat wrapper</h2>
|
|
||||||
<p class="lede">Four things a plain "talk to a model" app doesn't do.</p>
|
|
||||||
|
|
||||||
<div class="grid">
|
|
||||||
<div class="card">
|
|
||||||
<h3>The story is a tree</h3>
|
|
||||||
<p>Any turn can hold more than one <em>take</em>. Stepping between them is free — the story below simply
|
|
||||||
empties, and the server is told nothing. Writing below a take that isn't the live one is what makes a
|
|
||||||
branch, and a branch stores no turns of its own: it records where it left its parent and borrows
|
|
||||||
everything above that. Twenty forks cost 1.007× the page load of the same story flat. Switch lines and
|
|
||||||
the world state, the script scoreboard and the cooldown clocks all come back to what that line left.</p>
|
|
||||||
<img src="images/branch-map.jpg" alt="The branch map: one horizontal lane per line of the story, each joined to its parent by an elbow at the moment it forked">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="card">
|
|
||||||
<h3>The AI proposes, Python referees</h3>
|
|
||||||
<p>A scenario declares stats, flags, milestones and a named cast. Each turn the model appends the changes
|
|
||||||
it thinks happened — and the engine clamps them to range, enforces per-turn caps and cooldowns, keeps
|
|
||||||
counters monotonic and milestones sticky, then strips the machine-readable block out of the prose.
|
|
||||||
Word-labelled bands (<code>40–60: minor damage</code>) are what make the model reliable at it.
|
|
||||||
No dice, no scripting required.</p>
|
|
||||||
<img src="images/scenario-editor-npcs.jpg" alt="The scenario editor showing NPC stats with ranges, per-turn caps, cooldowns and labelled bands">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="card">
|
|
||||||
<h3>You can see the entire prompt</h3>
|
|
||||||
<p>Every turn stores exactly what was sent to the model. Open Insights on any action to see each context
|
|
||||||
component, what it cost in tokens, and why it was there — including which trigger word pulled in each
|
|
||||||
story card and the similarity score behind each retrieved memory.</p>
|
|
||||||
<img src="images/insights.jpg" alt="The Insights panel showing the assembled prompt broken into components with token counts">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="card">
|
|
||||||
<h3>Real AI Dungeon scripts run</h3>
|
|
||||||
<p>The three familiar hooks — <code>onInput</code>, <code>onModelContext</code>, <code>onOutput</code> —
|
|
||||||
with shared persistent <code>state</code> and a <code>worldEntries</code> API, executed in an embedded
|
|
||||||
quickjs sandbox. Scripts written for AI Dungeon import and work, and there's a CodeMirror editor in the app.</p>
|
|
||||||
<img src="images/script-editor.jpg" alt="The in-app script editor showing an input hook written in JavaScript">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="card">
|
|
||||||
<h3>Memory that survives a long story</h3>
|
|
||||||
<p>The modern AI Dungeon memory system: AI-generated memories every few actions, a running story summary,
|
|
||||||
and embedding-based retrieval that pulls an old-but-relevant fact back into context when it matters.
|
|
||||||
Undo and retry roll the world state back to a per-action snapshot rather than only rewriting the text.
|
|
||||||
Every story stays where you left it, and the home page opens on its most recent line.</p>
|
|
||||||
<img src="images/home.jpg" alt="The home page, showing stories in progress alongside scenarios to start from">
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section>
|
|
||||||
<h2>How a turn works</h2>
|
|
||||||
<p class="lede">Player input goes through the script pipeline, into a token-budgeted context, out to whichever
|
|
||||||
model you configured, and back through the referee.</p>
|
|
||||||
<pre><code>player input
|
|
||||||
→ onInput script modifier
|
|
||||||
→ assemble context: [narrator prompt] + [world state + stat guide] + [AI instructions]
|
|
||||||
+ [plot essentials] + [story summary] + [retrieved memories]
|
|
||||||
+ [triggered story cards] + [history along this branch, token-budgeted]
|
|
||||||
+ [author's note] + [player action]
|
|
||||||
→ onModelContext script modifier
|
|
||||||
→ snapshot context (Insights)
|
|
||||||
→ provider adapter → AI (streamed)
|
|
||||||
→ extract + referee the world-state delta block, strip it from the prose
|
|
||||||
→ onOutput script modifier
|
|
||||||
→ store & render</code></pre>
|
|
||||||
|
|
||||||
<div class="stack">
|
|
||||||
<span class="chip">FastAPI</span>
|
|
||||||
<span class="chip">SQLAlchemy</span>
|
|
||||||
<span class="chip">React + Vite</span>
|
|
||||||
<span class="chip">Postgres / SQLite</span>
|
|
||||||
<span class="chip">quickjs sandbox</span>
|
|
||||||
<span class="chip">Server-sent events</span>
|
|
||||||
<span class="chip">Docker</span>
|
|
||||||
<span class="chip">Any OpenAI-compatible endpoint</span>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section>
|
|
||||||
<h2>Engineering notes</h2>
|
|
||||||
<p class="lede">The parts that were measured rather than guessed at.</p>
|
|
||||||
|
|
||||||
<div class="stats">
|
|
||||||
<div class="stat"><div class="n">189×</div><div class="l">less database egress per adventure load</div></div>
|
|
||||||
<div class="stat"><div class="n">440</div><div class="l">backend tests, run by CI on every push</div></div>
|
|
||||||
<div class="stat"><div class="n">64</div><div class="l">schema migrations, applied in order on boot</div></div>
|
|
||||||
<div class="stat"><div class="n">$0</div><div class="l">to run it locally against Ollama</div></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<ul class="notes" style="margin-top:34px">
|
|
||||||
<li><strong>Database egress, cut ~189×.</strong> Every adventure load was pulling the entire assembled
|
|
||||||
prompt — about 74 KB per turn — just to read two small fields off it. Moving those into their own columns
|
|
||||||
and deferring the heavy ones took one load from 38.5 MB to 0.20 MB. A test hooks into SQLAlchemy's cursor
|
|
||||||
events and fails if a bulk load ever names those columns again.</li>
|
|
||||||
<li><strong>Turn cost, made flat.</strong> Assembling a turn walked the whole story, so it grew with story
|
|
||||||
length — 839 KB of reads by turn 200. History is now served as tails and slices from SQL: the same turn
|
|
||||||
costs 129 KB and stops growing at around turn 50.</li>
|
|
||||||
<li><strong>Branching that costs 103 bytes.</strong> A branch stores where it left its parent and borrows
|
|
||||||
every turn above that, so nothing is copied on a fork. A 40-turn story forked twenty times loads in
|
|
||||||
31,652 B against 31,433 B for the same story flat — 1.007×. Reads stay cheap because the ancestry is
|
|
||||||
windowed the way the history is: the number of SQL clauses is bounded by the context window, not by how
|
|
||||||
many times the story has forked.</li>
|
|
||||||
<li><strong>A shared demo key that can't be drained.</strong> The hosted demo funds a model for visitors, so
|
|
||||||
model selection is pinned server-side with a structural backstop that raises if any code path tries to
|
|
||||||
resolve a model outside the allowed set — plus a daily per-visitor turn cap.</li>
|
|
||||||
</ul>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<footer>
|
|
||||||
<p>Built by <a href="https://github.com/parththakkar106">Parth Thakkar</a> ·
|
|
||||||
<a href="https://github.com/parththakkar106/AI-DnD">Source on GitHub</a> ·
|
|
||||||
<a href="https://github.com/parththakkar106/AI-DnD/blob/main/LICENSE">MIT</a></p>
|
|
||||||
<p>Run it yourself with one command: <code>docker compose up --build</code></p>
|
|
||||||
</footer>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,156 +0,0 @@
|
|||||||
# Self-review log
|
|
||||||
|
|
||||||
**Every correctness bug on this page is resolved.** 20 are fixed, and 1 is intentionally
|
|
||||||
skipped, with the reasoning recorded below. The record is kept because the reasoning outlives
|
|
||||||
the verdicts, and several of these are traps worth remembering. The *cleanup backlog* at the
|
|
||||||
bottom is a deliberately open list of non-bugs (reuse, simplification, efficiency), not
|
|
||||||
outstanding defects.
|
|
||||||
|
|
||||||
Original review: 2026-07-05, whole project in scope (no git history at the time).
|
|
||||||
Status key: `fixed` means applied, `skipped` means intentionally not fixed, and `pending` means
|
|
||||||
outstanding (none remain).
|
|
||||||
|
|
||||||
**2026-07-06 update (branch `bugfix-code-review`):** every finding was re-verified against
|
|
||||||
current code. #1, #2, #3, #4, #6, and #12 had already been fixed in earlier sessions, so their
|
|
||||||
statuses were stale. #5, #7, #8, #9, #10, #13, #14 (backend) and #15 through #21 (frontend)
|
|
||||||
were fixed in this pass. #11 is skipped: real AI Dungeon's `addStoryCard` also returns the new
|
|
||||||
card's index (0-falsy included) per the official scripting guidebook, so changing it would
|
|
||||||
break compatibility. This is documented in `engine.py`'s prelude instead. The cleanup backlog
|
|
||||||
(R/S/E/A items) below remains open by design.
|
|
||||||
|
|
||||||
## Correctness bugs
|
|
||||||
|
|
||||||
### 1. [fixed] seed_demo.py doesn't stamp schema version → server crashes on next start
|
|
||||||
- `backend/seed_demo.py:10`
|
|
||||||
- A fresh DB created via `Base.metadata.create_all` leaves `PRAGMA user_version` at 0. The next server start sees the tables exist and replays every ALTER TABLE migration, causing a `duplicate column name` crash.
|
|
||||||
- Fix: stamp user_version to latest after create_all (reuse migrations.bootstrap logic).
|
|
||||||
|
|
||||||
### 2. [fixed] Turn-lock race: two simultaneous turns can run on the same adventure
|
|
||||||
- `backend/app/routers/adventures.py:302`
|
|
||||||
- `ensure_not_generating()` runs in the route handler, but `_active_turns.add()` only happens when the StreamingResponse generator is first iterated. Double-clicking Continue sends both requests past the 409 check, producing duplicate Action.index rows and interleaved generations.
|
|
||||||
- Fix: atomically test-and-set the lock in the request phase, release in the stream's `finally`.
|
|
||||||
|
|
||||||
### 3. [fixed] Migration 10 renumbers indexes with a correlated subquery on the table being updated
|
|
||||||
- `backend/app/migrations.py:34`
|
|
||||||
- SQLite may evaluate the subquery against partially updated rows, so duplicate indexes can survive the "repair".
|
|
||||||
- Fix: compute new indexes in Python (SELECT ordered, then UPDATE per row).
|
|
||||||
|
|
||||||
### 4. [fixed] SQLite foreign keys never enabled → CASCADE/SET NULL clauses are dead
|
|
||||||
- `backend/app/database.py:8`
|
|
||||||
- Deleting a Script leaves orphaned `scenario_scripts` rows. SQLite rowid reuse can then attach a future script to an old scenario.
|
|
||||||
- Fix: `PRAGMA foreign_keys=ON` via engine connect event.
|
|
||||||
|
|
||||||
### 5. [fixed] Provider generate() silently yields nothing for non-SSE 200 responses
|
|
||||||
- `backend/app/providers/openai_compatible.py:84`
|
|
||||||
- A server that ignores `stream=true` and returns plain JSON produces no `data:` lines, so the result is an empty AI action with no error.
|
|
||||||
- Fix: buffer the non-SSE body and fall back to parsing it as a single JSON completion.
|
|
||||||
|
|
||||||
### 6. [fixed] Fire-and-forget asyncio task can be GC'd mid-run and wedge the memory bank
|
|
||||||
- `backend/app/memorybank.py:146`
|
|
||||||
- The result of `asyncio.create_task` is not referenced, so the task can vanish silently and leave an adventure ID stuck in `_running`.
|
|
||||||
- Fix: keep strong refs in a set, discard in done-callback.
|
|
||||||
|
|
||||||
### 7. [fixed] Memory cursors are list positions but Memory.source_start/end are Action.index values
|
|
||||||
- `backend/app/memorybank.py:213`
|
|
||||||
- After any action deletion, indexes keep gaps and positions shift, so summarization skips or duplicates blocks and `_update_story_summary` folds the wrong memories.
|
|
||||||
- Fix: use one space consistently. Track cursors by Action.index (position-independent), or renumber on delete.
|
|
||||||
|
|
||||||
### 8. [fixed] Pinned memories don't count toward memory_top_k cap
|
|
||||||
- `backend/app/memorybank.py:120`
|
|
||||||
- 6 pinned plus top_k=5 injects 11 memories, blowing the token budget.
|
|
||||||
- Fix: fill with unpinned only up to `top_k - len(pinned)` (min 0).
|
|
||||||
|
|
||||||
### 9. [fixed] Embedding-model change → cosine() zips different-dimension vectors silently
|
|
||||||
- `backend/app/memorybank.py:69`
|
|
||||||
- Old 768-dimension embeddings scored against a new 1536-dimension query produce garbage similarity with no error, and are never re-embedded.
|
|
||||||
- Fix: return 0.0 on length mismatch (and ideally clear stale embeddings so _embed_pending redoes them).
|
|
||||||
|
|
||||||
### 10. [fixed] MAX_STORY_CARDS cap is a no-op for cards created in one hook
|
|
||||||
- `backend/app/scripting/pipeline.py:59`
|
|
||||||
- `len(existing) + len(seen_ids) < MAX...` never counts newly added cards, since seen_ids is a subset of existing. A script can insert an unbounded number of cards in one turn.
|
|
||||||
- Fix: count inserts made during the loop.
|
|
||||||
|
|
||||||
### 11. [skipped] addStoryCard returns 0 (falsy) for the first card, indistinguishable from `false` rejection
|
|
||||||
- `backend/app/scripting/engine.py:39`
|
|
||||||
- `if (!addStoryCard(...))` misfires when the card list was empty.
|
|
||||||
- Fix: return `storyCards.length` (1-based, always truthy) or `true`; document.
|
|
||||||
|
|
||||||
### 12. [fixed] scenario_id=0 truthiness bug in create_story_card
|
|
||||||
- `backend/app/routers/story_cards.py:28`
|
|
||||||
- `scenario_id or ...` picks the wrong owner when id is 0. Use `is not None`.
|
|
||||||
|
|
||||||
### 13. [fixed] test_connection 500s on non-dict JSON from /models
|
|
||||||
- `backend/app/routers/settings.py:54`
|
|
||||||
- Only ValueError is caught. `data.get`/`m.get` on non-dict input raises AttributeError, producing a 500 instead of `{ok:false}`.
|
|
||||||
- Fix: catch (ValueError, AttributeError, TypeError) or validate shapes.
|
|
||||||
|
|
||||||
### 14. [fixed] AI Dungeon exports with `worldInformation` key lose all story cards silently
|
|
||||||
- `backend/app/routers/scenarios.py:133`
|
|
||||||
- Import reads only `storyCards`/`worldInfo`. `worldInformation` is in _IGNORED_KEYS, so it is dropped without being reported.
|
|
||||||
- Fix: accept `worldInformation` as a card source too.
|
|
||||||
|
|
||||||
### 15. [fixed] Shared debounce timer loses edits (Play PlotPanel)
|
|
||||||
- `frontend/src/pages/Play.jsx:28`
|
|
||||||
- One `saveTimer` is shared by all plot fields and story-card saves. Editing a second field within 600ms cancels the first pending PATCH, causing silent data loss.
|
|
||||||
- Fix: per-key timers (e.g. a Map keyed by field/card id).
|
|
||||||
|
|
||||||
### 16. [fixed] Same shared-debounce data loss in ScenarioEditor
|
|
||||||
- `frontend/src/pages/ScenarioEditor.jsx:22`
|
|
||||||
- Same fix as #15.
|
|
||||||
|
|
||||||
### 17. [fixed] Continue button silently discards typed input text
|
|
||||||
- `frontend/src/pages/Play.jsx:469`
|
|
||||||
- Clicking Continue with text in the box sends type 'continue' (the backend ignores the text) and clears the input.
|
|
||||||
- Fix: don't clear input on continue (or treat non-empty input as a normal send).
|
|
||||||
|
|
||||||
### 18. [fixed] retry() optimistically deletes last AI action with no rollback on failure
|
|
||||||
- `frontend/src/pages/Play.jsx:475`
|
|
||||||
- A failed retry (409 or network error) leaves the UI missing an action that still exists server-side.
|
|
||||||
- Fix: restore the removed action in the catch path (or only remove on first stream event).
|
|
||||||
|
|
||||||
### 19. [fixed] Settings test()/save() have no error handling → stuck on "Testing…"
|
|
||||||
- `frontend/src/pages/Settings.jsx:66`
|
|
||||||
- A rejection leaves `{pending:true}` forever and produces an unhandled rejection.
|
|
||||||
- Fix: try/catch → setTestResult({ok:false, error:msg}).
|
|
||||||
|
|
||||||
### 20. [fixed] InsightsPanel race: slow earlier request overwrites newer report
|
|
||||||
- `frontend/src/pages/Play.jsx:302`
|
|
||||||
- There is no staleness guard, so a slow getAdventureContext call can overwrite a newer action snapshot.
|
|
||||||
- Fix: track a request id or cancelled flag in the effect.
|
|
||||||
|
|
||||||
### 21. [fixed] extractPlaceholders ignores ${...} in story-card trigger keys
|
|
||||||
- `frontend/src/pages/Scenarios.jsx:50`
|
|
||||||
- The backend fills placeholders in card.keys, but the modal never prompts for those names, so a literal `${hero}` key never matches.
|
|
||||||
- Fix: also scan card.keys when collecting placeholder names.
|
|
||||||
|
|
||||||
## Cleanup backlog (reuse / simplification / efficiency / altitude — not bugs, apply later)
|
|
||||||
|
|
||||||
- **R1** ~~three copies of the debounced-autosave handler.~~
|
|
||||||
**Half applied in phase 17 (2026-08).** All three use
|
|
||||||
`frontend/src/hooks/useDebouncedSave.js`. A shared StoryCardList component is
|
|
||||||
still open.
|
|
||||||
- **R2** `backend/seed_demo.py:228`: re-implements create_adventure. Call the router logic instead.
|
|
||||||
- **R3** `backend/app/providers/openai_compatible.py:122`: complete() duplicates _request()'s body building. Add a `stream` param to _request().
|
|
||||||
- **R4** ~~six copies of child-resource get, owner-check, and 404.~~
|
|
||||||
**Applied in phase 17 (2026-08).** All 32 handlers take the `current_adventure`
|
|
||||||
dependency from `routers/adventures/deps.py`.
|
|
||||||
- **R5** `frontend/src/api.js:26`: streamSSE duplicates request()'s error extraction. Extract `throwIfNotOk(resp)`.
|
|
||||||
- **S1** ~~`Settings.stream` is dead state (never read).~~
|
|
||||||
**Applied in phase 17 (2026-08).** The column, both schema fields, and migration 65
|
|
||||||
drop it. `R4` went with it: the ownership check is the `current_adventure` dependency.
|
|
||||||
- **S2** `frontend/src/pages/Play.jsx:6`: MODES and PLAYER_TYPES are identical constants; lastIsAi/canUndo are computed twice.
|
|
||||||
- **E1** `backend/app/context/builder.py:119`: joins and tokenizes the entire adventure history every turn for the trigger window. Walk reversed(actions) until budget instead.
|
|
||||||
- **E2** `backend/app/scripting/pipeline.py:77`: rebuilds full history dicts, JSON, and a blocking commit per script per hook. Build once per hook, slice to HISTORY_WINDOW first, and commit once.
|
|
||||||
- **E3** `frontend/src/pages/Play.jsx:561`: every SSE chunk re-renders all action rows. Isolate streaming text in a child component or React.memo rows.
|
|
||||||
- **E4** `backend/app/context/builder.py:48`: Section.tokens is uncached, so the whole context gets tokenized 2 to 3 times per turn. Cache counts and sum sections.
|
|
||||||
- **E5** `frontend/src/pages/Play.jsx:490`: the keydown effect has no dependency array, so the listener is re-registered every render.
|
|
||||||
- **E6** `backend/app/memorybank.py:182`: catch-up summarization awaits blocks sequentially. Gather independent blocks instead.
|
|
||||||
- **A1** ~~no UniqueConstraint('adventure_id','index'); index allocation is ad-hoc per writer.~~
|
|
||||||
**Overtaken by phase 14 (2026-08).** `index` is a legacy column that nothing reads: ordering
|
|
||||||
is `(branch_id, depth)` now, allocated in one place (`tree.place_action`). The column is kept
|
|
||||||
unread for one release and then dropped, so a constraint on it would apply to a column that
|
|
||||||
no longer does anything.
|
|
||||||
- **A2** `backend/app/providers/openai_compatible.py:45`: CHAT_CONTINUE_HINT is appended below the budgeting layer. Assemble prompts in the context builder instead.
|
|
||||||
- **A3** `backend/app/routers/adventures.py:390`: import endpoints hand-coerce raw dicts. Use a Pydantic bundle schema.
|
|
||||||
- **A4** `backend/app/routers/adventures.py:207`: onModelContext flattens (system, story) and ships everything as user content if modified. Pass structure through the hook instead.
|
|
||||||
- **A5** `frontend/src/pages/Home.jsx:87`: the client appends 'Z' to naive datetimes. Emit ISO-8601 with an offset from the API instead.
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
# React + Vite
|
|
||||||
|
|
||||||
This template provides a minimal setup to get React working in Vite with HMR and some Oxlint rules.
|
|
||||||
|
|
||||||
Currently, two official plugins are available:
|
|
||||||
|
|
||||||
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Oxc](https://oxc.rs)
|
|
||||||
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/)
|
|
||||||
|
|
||||||
## React Compiler
|
|
||||||
|
|
||||||
The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
|
|
||||||
|
|
||||||
## Expanding the Oxlint configuration
|
|
||||||
|
|
||||||
If you are developing a production application, use TypeScript with type-aware lint rules enabled. See the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for how to integrate TypeScript and Oxlint's TypeScript-related rules in your project.
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { createPortal } from 'react-dom'
|
import { createPortal } from 'react-dom'
|
||||||
import { CORNER, PAD, ROW_H, branchLabel, headLineage, layoutTree, momentTicks } from './branches'
|
import { CORNER, PAD, ROW_H, branchLabel, headLineage, layoutTree, momentTicks, savePointsUnder } from './branches'
|
||||||
|
|
||||||
// The story tree, drawn.
|
// The story tree, drawn.
|
||||||
//
|
//
|
||||||
@@ -108,6 +108,9 @@ export function BranchMap({ branches, busyId, onSwitch, onRename, onDelete, onCl
|
|||||||
// would throw away the only copy of it.
|
// would throw away the only copy of it.
|
||||||
const save = async () => { if (await onRename(selected, renameText)) setRenameText(null) }
|
const save = async () => { if (await onRename(selected, renameText)) setRenameText(null) }
|
||||||
const drop = async () => { if (await onDelete(selected)) setConfirming(false) }
|
const drop = async () => { if (await onDelete(selected)) setConfirming(false) }
|
||||||
|
// Save Points naming a moment on the selected branch, or on anything forked
|
||||||
|
// from it, keep it alive — the server refuses to delete them along with it.
|
||||||
|
const protecting = selected ? savePointsUnder(branches, selected.id) : 0
|
||||||
|
|
||||||
return createPortal(
|
return createPortal(
|
||||||
<div className="modal-overlay branch-map-overlay" onClick={onClose}>
|
<div className="modal-overlay branch-map-overlay" onClick={onClose}>
|
||||||
@@ -239,7 +242,13 @@ export function BranchMap({ branches, busyId, onSwitch, onRename, onDelete, onCl
|
|||||||
|
|
||||||
{confirming ? (
|
{confirming ? (
|
||||||
<div className="branch-confirm">
|
<div className="branch-confirm">
|
||||||
<span>Delete this branch and everything forked from it?</span>
|
{/* The same wording the list gives, because the same deletion
|
||||||
|
is reachable from both views and a rule that held in one of
|
||||||
|
them would not be a rule. */}
|
||||||
|
<span>
|
||||||
|
Delete this branch and everything forked from it? The story on
|
||||||
|
other paths, and every Save Point, is unaffected.
|
||||||
|
</span>
|
||||||
<button type="button" className="danger" disabled={busy} onClick={drop}>
|
<button type="button" className="danger" disabled={busy} onClick={drop}>
|
||||||
Delete
|
Delete
|
||||||
</button>
|
</button>
|
||||||
@@ -265,10 +274,14 @@ export function BranchMap({ branches, busyId, onSwitch, onRename, onDelete, onCl
|
|||||||
the head stands on is offered, and says why it cannot go. */}
|
the head stands on is offered, and says why it cannot go. */}
|
||||||
{!isRoot && (
|
{!isRoot && (
|
||||||
<button type="button" className="danger"
|
<button type="button" className="danger"
|
||||||
disabled={busy || isLoadBearing}
|
disabled={busy || isLoadBearing || protecting > 0}
|
||||||
title={isLoadBearing
|
title={
|
||||||
? 'The line you are reading is built on this one. Switch away first.'
|
isLoadBearing
|
||||||
: undefined}
|
? 'The line you are reading is built on this one. Switch away first.'
|
||||||
|
: protecting > 0
|
||||||
|
? `${protecting === 1 ? 'A Save Point is' : `${protecting} Save Points are`} saved on this line or one forked from it. Delete ${protecting === 1 ? 'it' : 'them'} first — deleting a Save Point deletes no story.`
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
onClick={() => setConfirming(true)}>Delete</button>
|
onClick={() => setConfirming(true)}>Delete</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -106,6 +106,25 @@ export const api = {
|
|||||||
}),
|
}),
|
||||||
deleteBranch: (advId, branchId) =>
|
deleteBranch: (advId, branchId) =>
|
||||||
request(`/adventures/${advId}/branches/${branchId}`, { method: 'DELETE' }),
|
request(`/adventures/${advId}/branches/${branchId}`, { method: 'DELETE' }),
|
||||||
|
// Save Points (M4). A Save Point is a durable name for a story position; the
|
||||||
|
// server stores the coordinate and nothing else. Create takes no position —
|
||||||
|
// it is always made at the campaign's active head, which is where the reader
|
||||||
|
// is. Restore answers with the story as it now stands, like a branch switch,
|
||||||
|
// so the caller replaces its window rather than reloading everything.
|
||||||
|
listCheckpoints: (advId) => request(`/adventures/${advId}/checkpoints`),
|
||||||
|
createCheckpoint: (advId, name, note = '') =>
|
||||||
|
request(`/adventures/${advId}/checkpoints`, {
|
||||||
|
method: 'POST', body: JSON.stringify({ name, note }),
|
||||||
|
}),
|
||||||
|
renameCheckpoint: (advId, checkpointId, name) =>
|
||||||
|
request(`/adventures/${advId}/checkpoints/${checkpointId}`, {
|
||||||
|
method: 'PATCH', body: JSON.stringify({ name }),
|
||||||
|
}),
|
||||||
|
deleteCheckpoint: (advId, checkpointId) =>
|
||||||
|
request(`/adventures/${advId}/checkpoints/${checkpointId}`, { method: 'DELETE' }),
|
||||||
|
restoreCheckpoint: (advId, checkpointId) =>
|
||||||
|
request(`/adventures/${advId}/checkpoints/${checkpointId}/restore`, { method: 'POST' }),
|
||||||
|
|
||||||
// Play a turn again, differently (SP9). An AI turn regenerates; a player's
|
// Play a turn again, differently (SP9). An AI turn regenerates; a player's
|
||||||
// own takes the text given. Streams, because it is a turn like any other.
|
// own takes the text given. Streams, because it is a turn like any other.
|
||||||
//
|
//
|
||||||
@@ -124,6 +143,9 @@ export const api = {
|
|||||||
exportAdventure: (id) => request(`/adventures/${id}/export`),
|
exportAdventure: (id) => request(`/adventures/${id}/export`),
|
||||||
importAdventure: (bundle) => request('/adventures/import', { method: 'POST', body: JSON.stringify(bundle) }),
|
importAdventure: (bundle) => request('/adventures/import', { method: 'POST', body: JSON.stringify(bundle) }),
|
||||||
undo: (advId) => request(`/adventures/${advId}/undo`, { method: 'POST' }),
|
undo: (advId) => request(`/adventures/${advId}/undo`, { method: 'POST' }),
|
||||||
|
// Undo moves the story back without deleting it, so there is somewhere to
|
||||||
|
// move forward to again (M3). Both answer with the newest window.
|
||||||
|
redo: (advId) => request(`/adventures/${advId}/redo`, { method: 'POST' }),
|
||||||
getAdventureContext: (advId) => request(`/adventures/${advId}/context`),
|
getAdventureContext: (advId) => request(`/adventures/${advId}/context`),
|
||||||
// "Update from scenario": GET describes what would change, POST applies it.
|
// "Update from scenario": GET describes what would change, POST applies it.
|
||||||
previewRefresh: (advId) => request(`/adventures/${advId}/refresh`),
|
previewRefresh: (advId) => request(`/adventures/${advId}/refresh`),
|
||||||
|
|||||||
@@ -61,6 +61,41 @@ export function headLineage(branches) {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// How many Save Points would go if this branch were deleted.
|
||||||
|
//
|
||||||
|
// Deleting a branch deletes everything forked from it, and a Save Point names a
|
||||||
|
// position on a line, so the Save Points on the whole doomed subtree go too.
|
||||||
|
// The count is summed over that subtree rather than over the one branch — a
|
||||||
|
// warning that said "1 Save Point" while three disappeared would be worse than
|
||||||
|
// no warning at all.
|
||||||
|
//
|
||||||
|
// Computed here because the panel already holds every branch and its parent,
|
||||||
|
// so the answer costs a walk rather than an endpoint. The server remains the
|
||||||
|
// authority on what is actually deleted; this only lets the button say so
|
||||||
|
// before it is pressed, the same division `headLineage` already uses.
|
||||||
|
export function savePointsUnder(branches, rootId) {
|
||||||
|
const children = new Map()
|
||||||
|
for (const b of branches) {
|
||||||
|
const key = b.parent_branch_id
|
||||||
|
if (!children.has(key)) children.set(key, [])
|
||||||
|
children.get(key).push(b)
|
||||||
|
}
|
||||||
|
const byId = new Map(branches.map((b) => [b.id, b]))
|
||||||
|
const seen = new Set()
|
||||||
|
const stack = [rootId]
|
||||||
|
let total = 0
|
||||||
|
while (stack.length) {
|
||||||
|
const id = stack.pop()
|
||||||
|
// The guard is against a cycle, which the schema forbids and a walk should
|
||||||
|
// still never hang on.
|
||||||
|
if (seen.has(id)) continue
|
||||||
|
seen.add(id)
|
||||||
|
total += byId.get(id)?.save_points ?? 0
|
||||||
|
for (const child of children.get(id) ?? []) stack.push(child.id)
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- Map geometry ----------
|
// ---------- Map geometry ----------
|
||||||
|
|
||||||
export const ROW_H = 64 // one branch, name above the lane and meta below
|
export const ROW_H = 64 // one branch, name above the lane and meta below
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
* imports below.
|
* imports below.
|
||||||
*
|
*
|
||||||
* The page component still owns all of the session state. Lifting it into a
|
* The page component still owns all of the session state. Lifting it into a
|
||||||
* `usePlaySession` hook waits for Stage 5 of `plan/17-refactor.md`, which adds
|
* `usePlaySession` hook waits for the frontend test runner M8 adds
|
||||||
* a frontend test runner. Moving eighteen `useState` calls and seven
|
* (`planning/BUILD-MILESTONES.md`). Moving eighteen `useState` calls and seven
|
||||||
* `useEffect` calls is a rewrite rather than a move, and nothing would catch a
|
* `useEffect` calls is a rewrite rather than a move, and nothing would catch a
|
||||||
* mistake in it today.
|
* mistake in it today.
|
||||||
*/
|
*/
|
||||||
@@ -22,6 +22,7 @@ import { BranchPanel } from './panels/BranchPanel'
|
|||||||
import { InsightsPanel } from './panels/InsightsPanel'
|
import { InsightsPanel } from './panels/InsightsPanel'
|
||||||
import { MemoryPanel } from './panels/MemoryPanel'
|
import { MemoryPanel } from './panels/MemoryPanel'
|
||||||
import { PlotPanel } from './panels/PlotPanel'
|
import { PlotPanel } from './panels/PlotPanel'
|
||||||
|
import { SavePointPanel } from './panels/SavePointPanel'
|
||||||
|
|
||||||
const MODES = ['do', 'say', 'story']
|
const MODES = ['do', 'say', 'story']
|
||||||
const PLAYER_TYPES = ['do', 'say', 'story']
|
const PLAYER_TYPES = ['do', 'say', 'story']
|
||||||
@@ -68,7 +69,7 @@ export default function Play() {
|
|||||||
const [busy, setBusy] = useState(false)
|
const [busy, setBusy] = useState(false)
|
||||||
const [toast, setToast] = useState(null)
|
const [toast, setToast] = useState(null)
|
||||||
const [editing, setEditing] = useState(null)
|
const [editing, setEditing] = useState(null)
|
||||||
const [panel, setPanel] = useState(null) // null | 'plot' | 'insights'
|
const [panel, setPanel] = useState(null) // null | 'plot' | 'memory' | 'branches' | 'savepoints' | 'insights'
|
||||||
// Bumped when something outside the turn loop changes the drawers' state
|
// Bumped when something outside the turn loop changes the drawers' state
|
||||||
// (currently "Update from scenario"), which no action count would reflect.
|
// (currently "Update from scenario"), which no action count would reflect.
|
||||||
const [stateKey, setStateKey] = useState(0)
|
const [stateKey, setStateKey] = useState(0)
|
||||||
@@ -104,6 +105,12 @@ export default function Play() {
|
|||||||
const [total, setTotal] = useState(0)
|
const [total, setTotal] = useState(0)
|
||||||
const [hasMore, setHasMore] = useState(false)
|
const [hasMore, setHasMore] = useState(false)
|
||||||
const [loadingOlder, setLoadingOlder] = useState(false)
|
const [loadingOlder, setLoadingOlder] = useState(false)
|
||||||
|
// Whether Undo and Redo have anywhere to go, as the server last reported it
|
||||||
|
// (M3). Neither is derivable here: Undo stops at the campaign opening, which
|
||||||
|
// may be off the top of the loaded window, and Redo depends on the retained
|
||||||
|
// future, which the transcript is never sent. So the flags ride on every
|
||||||
|
// window the server hands back and this holds the newest answer.
|
||||||
|
const [history, setHistory] = useState({ undo: false, redo: false })
|
||||||
const storyEndRef = useRef(null)
|
const storyEndRef = useRef(null)
|
||||||
const abortRef = useRef(null)
|
const abortRef = useRef(null)
|
||||||
const pinnedRef = useRef(true) // autoscroll only while the reader is at the bottom
|
const pinnedRef = useRef(true) // autoscroll only while the reader is at the bottom
|
||||||
@@ -155,10 +162,19 @@ export default function Play() {
|
|||||||
setActions(adv.actions)
|
setActions(adv.actions)
|
||||||
setTotal(adv.action_count ?? adv.actions.length)
|
setTotal(adv.action_count ?? adv.actions.length)
|
||||||
setHasMore(adv.actions.length < (adv.action_count ?? adv.actions.length))
|
setHasMore(adv.actions.length < (adv.action_count ?? adv.actions.length))
|
||||||
|
// A campaign closed while undone reopens undone, so the controls have
|
||||||
|
// to be told before the reader can press one.
|
||||||
|
setHistory({ undo: !!adv.can_undo, redo: !!adv.can_redo })
|
||||||
})
|
})
|
||||||
.catch(() => navigate('/'))
|
.catch(() => navigate('/'))
|
||||||
}, [id, navigate])
|
}, [id, navigate])
|
||||||
|
|
||||||
|
// Record what the server says the history controls can do. Every response
|
||||||
|
// that carries a window carries them, so this is called wherever one lands.
|
||||||
|
const noteHistory = useCallback((page) => {
|
||||||
|
setHistory({ undo: !!page.can_undo, redo: !!page.can_redo })
|
||||||
|
}, [])
|
||||||
|
|
||||||
// Take the story the server just handed back, whole.
|
// Take the story the server just handed back, whole.
|
||||||
//
|
//
|
||||||
// Switching a branch and forking one both answer with the newest window of
|
// Switching a branch and forking one both answer with the newest window of
|
||||||
@@ -173,11 +189,12 @@ export default function Play() {
|
|||||||
setActions(page.actions)
|
setActions(page.actions)
|
||||||
setTotal(page.total)
|
setTotal(page.total)
|
||||||
setHasMore(page.has_more)
|
setHasMore(page.has_more)
|
||||||
|
noteHistory(page)
|
||||||
// The world state comes back to what that branch's tip left
|
// The world state comes back to what that branch's tip left
|
||||||
// behind, so anything drawn from them is now showing another line's
|
// behind, so anything drawn from them is now showing another line's
|
||||||
// numbers until it re-reads.
|
// numbers until it re-reads.
|
||||||
setStateKey((k) => k + 1)
|
setStateKey((k) => k + 1)
|
||||||
}, [])
|
}, [noteHistory])
|
||||||
|
|
||||||
// Fetch the page above the one on screen and prepend it.
|
// Fetch the page above the one on screen and prepend it.
|
||||||
//
|
//
|
||||||
@@ -216,6 +233,7 @@ export default function Play() {
|
|||||||
}
|
}
|
||||||
setTotal(page.total)
|
setTotal(page.total)
|
||||||
setHasMore(page.has_more)
|
setHasMore(page.has_more)
|
||||||
|
noteHistory(page)
|
||||||
} catch {
|
} catch {
|
||||||
// Leave hasMore alone: a failed fetch should let the reader try again
|
// Leave hasMore alone: a failed fetch should let the reader try again
|
||||||
// by scrolling, not permanently hide the rest of their story. But hold
|
// by scrolling, not permanently hide the rest of their story. But hold
|
||||||
@@ -227,7 +245,7 @@ export default function Play() {
|
|||||||
loadingOlderRef.current = false
|
loadingOlderRef.current = false
|
||||||
setLoadingOlder(false)
|
setLoadingOlder(false)
|
||||||
}
|
}
|
||||||
}, [actions, hasMore, id])
|
}, [actions, hasMore, id, noteHistory])
|
||||||
|
|
||||||
// Put the viewport back after a prepend. useLayoutEffect, not useEffect:
|
// Put the viewport back after a prepend. useLayoutEffect, not useEffect:
|
||||||
// this has to run before the browser paints, or the reader sees the story
|
// this has to run before the browser paints, or the reader sees the story
|
||||||
@@ -277,6 +295,12 @@ export default function Play() {
|
|||||||
setReasoningStream(null)
|
setReasoningStream(null)
|
||||||
setActions((prev) => [...prev, event.action])
|
setActions((prev) => [...prev, event.action])
|
||||||
setTotal((n) => n + 1)
|
setTotal((n) => n + 1)
|
||||||
|
// A turn was accepted, so there is something to undo and nothing left to
|
||||||
|
// redo: writing below a moved-back head is the divergence that retires
|
||||||
|
// the old future, and writing at the tip never had a future in front of
|
||||||
|
// it. Set here rather than fetched, because the turn arrives over SSE and
|
||||||
|
// this is the same answer the server would give.
|
||||||
|
setHistory({ undo: true, redo: false })
|
||||||
} else if (event.type === 'error') {
|
} else if (event.type === 'error') {
|
||||||
setStreaming(null)
|
setStreaming(null)
|
||||||
setReasoningStream(null)
|
setReasoningStream(null)
|
||||||
@@ -363,30 +387,42 @@ export default function Play() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function undo() {
|
// Undo and Redo differ only in which way the head moves, so they share
|
||||||
|
// everything else: the same window shape comes back, the same reader state is
|
||||||
|
// dropped, and the same world state has been restored underneath.
|
||||||
|
async function moveHead(call) {
|
||||||
setToast(null)
|
setToast(null)
|
||||||
setPreview(null)
|
setPreview(null)
|
||||||
setPinned(null)
|
setPinned(null)
|
||||||
try {
|
try {
|
||||||
// A window, not the whole story — undo is the action most likely to be
|
// A window, not the whole story — these are the actions most likely to be
|
||||||
// repeated several times running, so it must not re-fetch everything.
|
// repeated several times running, so they must not re-fetch everything.
|
||||||
const page = await api.undo(id)
|
const page = await call(id)
|
||||||
setActions(page.actions)
|
setActions(page.actions)
|
||||||
setTotal(page.total)
|
setTotal(page.total)
|
||||||
setHasMore(page.has_more)
|
setHasMore(page.has_more)
|
||||||
|
noteHistory(page)
|
||||||
|
// The story is at a different position, so the state panels are showing
|
||||||
|
// the numbers of a turn that is no longer where the story ends.
|
||||||
|
setStateKey((k) => k + 1)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setToast({ text: err.message, isError: true })
|
setToast({ text: err.message, isError: true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ctrl+Z undo / Ctrl+R retry, ignored while typing in a field.
|
const undo = () => moveHead(api.undo)
|
||||||
|
const redo = () => moveHead(api.redo)
|
||||||
|
|
||||||
|
// Ctrl+Z undo / Ctrl+Shift+Z redo / Ctrl+R retry, ignored while typing.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const lastIsAi = actions.length > 0 && actions[actions.length - 1].type === 'ai'
|
const lastIsAi = actions.length > 0 && actions[actions.length - 1].type === 'ai'
|
||||||
const canUndo = actions.length > 0 && actions[actions.length - 1].type !== 'start'
|
|
||||||
const onKey = (e) => {
|
const onKey = (e) => {
|
||||||
if (!(e.ctrlKey || e.metaKey) || busy) return
|
if (!(e.ctrlKey || e.metaKey) || busy) return
|
||||||
if (e.target.closest?.('input, textarea, select, [contenteditable]')) return
|
if (e.target.closest?.('input, textarea, select, [contenteditable]')) return
|
||||||
if (e.key.toLowerCase() === 'z' && canUndo) {
|
if (e.key.toLowerCase() === 'z' && e.shiftKey && history.redo) {
|
||||||
|
e.preventDefault()
|
||||||
|
redo()
|
||||||
|
} else if (e.key.toLowerCase() === 'z' && !e.shiftKey && history.undo) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
undo()
|
undo()
|
||||||
} else if (e.key.toLowerCase() === 'r' && lastIsAi) {
|
} else if (e.key.toLowerCase() === 'r' && lastIsAi) {
|
||||||
@@ -467,6 +503,7 @@ export default function Play() {
|
|||||||
setActions(adv.actions)
|
setActions(adv.actions)
|
||||||
setTotal(adv.action_count ?? adv.actions.length)
|
setTotal(adv.action_count ?? adv.actions.length)
|
||||||
setHasMore(adv.actions.length < (adv.action_count ?? adv.actions.length))
|
setHasMore(adv.actions.length < (adv.action_count ?? adv.actions.length))
|
||||||
|
setHistory({ undo: !!adv.can_undo, redo: !!adv.can_redo })
|
||||||
// The branch and the world state can both have moved.
|
// The branch and the world state can both have moved.
|
||||||
setStateKey((k) => k + 1)
|
setStateKey((k) => k + 1)
|
||||||
} catch { /* stale beats wrong */ }
|
} catch { /* stale beats wrong */ }
|
||||||
@@ -490,7 +527,6 @@ export default function Play() {
|
|||||||
if (!adventure) return null
|
if (!adventure) return null
|
||||||
|
|
||||||
const lastIsAi = actions.length > 0 && actions[actions.length - 1].type === 'ai'
|
const lastIsAi = actions.length > 0 && actions[actions.length - 1].type === 'ai'
|
||||||
const canUndo = actions.length > 0 && actions[actions.length - 1].type !== 'start'
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`play-layout ${panel ? 'with-panel' : ''}`}>
|
<div className={`play-layout ${panel ? 'with-panel' : ''}`}>
|
||||||
@@ -510,6 +546,8 @@ export default function Play() {
|
|||||||
onClick={() => setPanel(panel === 'memory' ? null : 'memory')}>Memory</button>
|
onClick={() => setPanel(panel === 'memory' ? null : 'memory')}>Memory</button>
|
||||||
<button className={panel === 'branches' ? 'active' : ''}
|
<button className={panel === 'branches' ? 'active' : ''}
|
||||||
onClick={() => setPanel(panel === 'branches' ? null : 'branches')}>Branches</button>
|
onClick={() => setPanel(panel === 'branches' ? null : 'branches')}>Branches</button>
|
||||||
|
<button className={panel === 'savepoints' ? 'active' : ''}
|
||||||
|
onClick={() => setPanel(panel === 'savepoints' ? null : 'savepoints')}>Save Points</button>
|
||||||
<button className={panel === 'insights' ? 'active' : ''}
|
<button className={panel === 'insights' ? 'active' : ''}
|
||||||
onClick={() => { setInspectActionId(null); setPanel(panel === 'insights' ? null : 'insights') }}>
|
onClick={() => { setInspectActionId(null); setPanel(panel === 'insights' ? null : 'insights') }}>
|
||||||
Insights
|
Insights
|
||||||
@@ -673,7 +711,19 @@ export default function Play() {
|
|||||||
<div className="turn-buttons">
|
<div className="turn-buttons">
|
||||||
<button onClick={() => send('continue')} disabled={busy}>Continue ▸</button>
|
<button onClick={() => send('continue')} disabled={busy}>Continue ▸</button>
|
||||||
<button onClick={retry} disabled={busy || !lastIsAi} title="Ctrl+R">↻ Retry</button>
|
<button onClick={retry} disabled={busy || !lastIsAi} title="Ctrl+R">↻ Retry</button>
|
||||||
<button onClick={undo} disabled={busy || !canUndo} title="Ctrl+Z">↶ Undo</button>
|
<button onClick={undo} disabled={busy || !history.undo} title="Ctrl+Z">↶ Undo</button>
|
||||||
|
{/* Undo keeps what it steps back over, so this walks the story
|
||||||
|
forward into it again. Disabled once a new turn has been written
|
||||||
|
from here: that future is retained, but it is no longer the
|
||||||
|
continuation this story tells. */}
|
||||||
|
<button onClick={redo} disabled={busy || !history.redo} title="Ctrl+Shift+Z">↷ Redo</button>
|
||||||
|
{/* Save Point sits with the history controls because that is what
|
||||||
|
it is: a name for a position Undo and Redo move between. The
|
||||||
|
button opens the panel, where the name is typed — the moment it
|
||||||
|
saves is wherever the story is being read, so there is nothing
|
||||||
|
to choose first. */}
|
||||||
|
<button onClick={() => setPanel('savepoints')} disabled={busy}
|
||||||
|
title="Name this moment so you can come back to it">⚑ Save Point</button>
|
||||||
</div>
|
</div>
|
||||||
<div className="input-bar">
|
<div className="input-bar">
|
||||||
<div className="mode-select">
|
<div className="mode-select">
|
||||||
@@ -712,7 +762,10 @@ export default function Play() {
|
|||||||
{panel && (
|
{panel && (
|
||||||
<div className="side-panel">
|
<div className="side-panel">
|
||||||
<div className="side-panel-header">
|
<div className="side-panel-header">
|
||||||
<h2>{{ plot: 'Plot Components', memory: 'Memory Bank', branches: 'Branches', insights: 'Insights' }[panel]}</h2>
|
<h2>{{
|
||||||
|
plot: 'Plot Components', memory: 'Memory Bank', branches: 'Branches',
|
||||||
|
savepoints: 'Save Points', insights: 'Insights',
|
||||||
|
}[panel]}</h2>
|
||||||
<button onClick={() => setPanel(null)}>✕</button>
|
<button onClick={() => setPanel(null)}>✕</button>
|
||||||
</div>
|
</div>
|
||||||
{panel === 'plot' ? (
|
{panel === 'plot' ? (
|
||||||
@@ -724,6 +777,17 @@ export default function Play() {
|
|||||||
// but deleting a branch deletes the memories that hung off it,
|
// but deleting a branch deletes the memories that hung off it,
|
||||||
// and that happens without a turn being played.
|
// and that happens without a turn being played.
|
||||||
refreshKey={`${actions.length}:${stateKey}`} />
|
refreshKey={`${actions.length}:${stateKey}`} />
|
||||||
|
) : panel === 'savepoints' ? (
|
||||||
|
// Restoring one moves the story exactly as Undo and Redo do, so it
|
||||||
|
// adopts the returned window the same way a branch switch does —
|
||||||
|
// the state panels are showing another position's numbers until
|
||||||
|
// they re-read.
|
||||||
|
<SavePointPanel
|
||||||
|
advId={id}
|
||||||
|
refreshKey={`${actions.length}:${stateKey}`}
|
||||||
|
onRestored={adoptWindow}
|
||||||
|
onError={(message) => setToast({ text: message, isError: true })}
|
||||||
|
/>
|
||||||
) : panel === 'branches' ? (
|
) : panel === 'branches' ? (
|
||||||
<BranchPanel
|
<BranchPanel
|
||||||
advId={id}
|
advId={id}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { api } from '../../../api'
|
import { api } from '../../../api'
|
||||||
import { BranchMap } from '../../../BranchMap'
|
import { BranchMap } from '../../../BranchMap'
|
||||||
import { branchLabel, headLineage, orderBranches } from '../../../branches'
|
import { branchLabel, headLineage, orderBranches, savePointsUnder } from '../../../branches'
|
||||||
|
|
||||||
function BranchPanel({ advId, refreshKey, onSwitched, onTreeChanged, onError }) {
|
function BranchPanel({ advId, refreshKey, onSwitched, onTreeChanged, onError }) {
|
||||||
const [branches, setBranches] = useState(null)
|
const [branches, setBranches] = useState(null)
|
||||||
@@ -88,6 +88,12 @@ function BranchPanel({ advId, refreshKey, onSwitched, onTreeChanged, onError })
|
|||||||
// was forked from. The button said nothing about that and answered
|
// was forked from. The button said nothing about that and answered
|
||||||
// with a toast; it now says so before it is pressed.
|
// with a toast; it now says so before it is pressed.
|
||||||
const loadBearing = lineage.has(branch.id)
|
const loadBearing = lineage.has(branch.id)
|
||||||
|
// A Save Point naming a moment on this branch, or on anything forked
|
||||||
|
// from it, keeps the branch alive: named moments last until the user
|
||||||
|
// deletes them, so the server refuses the deletion rather than taking
|
||||||
|
// them with it. Counted over the subtree, because deleting a branch
|
||||||
|
// takes its descendants.
|
||||||
|
const protecting = savePointsUnder(branches, branch.id)
|
||||||
return (
|
return (
|
||||||
<div key={branch.id} className={`branch-row ${branch.is_head ? 'here' : ''}`}
|
<div key={branch.id} className={`branch-row ${branch.is_head ? 'here' : ''}`}
|
||||||
style={{ marginLeft: indent * 12 }}>
|
style={{ marginLeft: indent * 12 }}>
|
||||||
@@ -117,11 +123,20 @@ function BranchPanel({ advId, refreshKey, onSwitched, onTreeChanged, onError })
|
|||||||
{branch.own_actions} of its own
|
{branch.own_actions} of its own
|
||||||
{branch.parent_branch_id !== null && ` · forked at moment ${branch.fork_depth + 1}`}
|
{branch.parent_branch_id !== null && ` · forked at moment ${branch.fork_depth + 1}`}
|
||||||
{` · ends at ${branch.depth + 1}`}
|
{` · ends at ${branch.depth + 1}`}
|
||||||
|
{protecting > 0 &&
|
||||||
|
` · ${protecting === 1 ? '1 Save Point' : `${protecting} Save Points`} kept here`}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{isConfirming ? (
|
{isConfirming ? (
|
||||||
<div className="branch-confirm">
|
<div className="branch-confirm">
|
||||||
<span>Delete this branch and everything forked from it?</span>
|
{/* Only reachable when no Save Point points into this
|
||||||
|
subtree — the button is disabled otherwise, and the server
|
||||||
|
refuses regardless. So this says what the deletion costs
|
||||||
|
without hedging about Save Points that cannot be at risk. */}
|
||||||
|
<span>
|
||||||
|
Delete this branch and everything forked from it? The story
|
||||||
|
on other paths, and every Save Point, is unaffected.
|
||||||
|
</span>
|
||||||
<button type="button" className="danger" disabled={busy}
|
<button type="button" className="danger" disabled={busy}
|
||||||
onClick={() => remove(branch)}>Delete</button>
|
onClick={() => remove(branch)}>Delete</button>
|
||||||
<button type="button" onClick={() => setConfirming(null)}>Keep</button>
|
<button type="button" onClick={() => setConfirming(null)}>Keep</button>
|
||||||
@@ -145,10 +160,15 @@ function BranchPanel({ advId, refreshKey, onSwitched, onTreeChanged, onError })
|
|||||||
{/* The root holds the turns every other branch borrows, and
|
{/* The root holds the turns every other branch borrows, and
|
||||||
the server refuses it — so it is not offered. */}
|
the server refuses it — so it is not offered. */}
|
||||||
{branch.parent_branch_id !== null && (
|
{branch.parent_branch_id !== null && (
|
||||||
<button type="button" className="danger" disabled={busy || loadBearing}
|
<button type="button" className="danger"
|
||||||
title={loadBearing
|
disabled={busy || loadBearing || protecting > 0}
|
||||||
? 'The line you are reading is built on this one. Switch away first.'
|
title={
|
||||||
: undefined}
|
loadBearing
|
||||||
|
? 'The line you are reading is built on this one. Switch away first.'
|
||||||
|
: protecting > 0
|
||||||
|
? `${protecting === 1 ? 'A Save Point is' : `${protecting} Save Points are`} saved on this line or one forked from it. Delete ${protecting === 1 ? 'it' : 'them'} first — deleting a Save Point deletes no story.`
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
onClick={() => setConfirming(branch.id)}>Delete</button>
|
onClick={() => setConfirming(branch.id)}>Delete</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,209 @@
|
|||||||
|
// Save Points: naming a place in the story, and going back to one.
|
||||||
|
//
|
||||||
|
// "Save Point" is the word throughout, and the words branch, head, node and
|
||||||
|
// fork appear nowhere a player can read (`BROWSER-UX-SPEC.md` §23). The panel
|
||||||
|
// is deliberately small — a form, a list, and two confirmations. It is not a
|
||||||
|
// branch explorer, and the tree it sits over stays out of sight.
|
||||||
|
//
|
||||||
|
// Both confirmations exist to say what does *not* happen, because that is the
|
||||||
|
// part a player cannot see and would otherwise assume the worst about: restore
|
||||||
|
// keeps the later story, and deleting a Save Point deletes no story at all.
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
import { api } from '../../../api'
|
||||||
|
|
||||||
|
function SavePointPanel({ advId, refreshKey, onRestored, onError }) {
|
||||||
|
const [points, setPoints] = useState(null)
|
||||||
|
const [failed, setFailed] = useState(null)
|
||||||
|
const [name, setName] = useState('')
|
||||||
|
const [note, setNote] = useState('')
|
||||||
|
const [saving, setSaving] = useState(false)
|
||||||
|
const [busyId, setBusyId] = useState(null)
|
||||||
|
const [renaming, setRenaming] = useState(null) // { id, text }
|
||||||
|
const [confirming, setConfirming] = useState(null) // { id, kind }
|
||||||
|
const [tick, setTick] = useState(0)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false
|
||||||
|
setFailed(null)
|
||||||
|
api.listCheckpoints(advId)
|
||||||
|
.then((list) => { if (!cancelled) setPoints(list) })
|
||||||
|
.catch((err) => { if (!cancelled) setFailed(err.message) })
|
||||||
|
return () => { cancelled = true }
|
||||||
|
}, [advId, refreshKey, tick])
|
||||||
|
|
||||||
|
// Answers whether it worked, so a caller can keep its editor open on a
|
||||||
|
// refusal — a rename the server turned down must not take the typed name
|
||||||
|
// with it.
|
||||||
|
async function run(id, work) {
|
||||||
|
setBusyId(id)
|
||||||
|
try {
|
||||||
|
await work()
|
||||||
|
setTick((t) => t + 1)
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
onError(err.message)
|
||||||
|
return false
|
||||||
|
} finally {
|
||||||
|
setBusyId(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function create(e) {
|
||||||
|
e.preventDefault()
|
||||||
|
if (!name.trim() || saving) return
|
||||||
|
setSaving(true)
|
||||||
|
try {
|
||||||
|
await api.createCheckpoint(advId, name.trim(), note.trim())
|
||||||
|
setName('')
|
||||||
|
setNote('')
|
||||||
|
setTick((t) => t + 1)
|
||||||
|
} catch (err) {
|
||||||
|
onError(err.message)
|
||||||
|
} finally {
|
||||||
|
setSaving(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const restore = (p) => run(p.id, async () => {
|
||||||
|
onRestored(await api.restoreCheckpoint(advId, p.id))
|
||||||
|
setConfirming(null)
|
||||||
|
})
|
||||||
|
const rename = async (p) => {
|
||||||
|
if (await run(p.id, () => api.renameCheckpoint(advId, p.id, renaming.text))) {
|
||||||
|
setRenaming(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const remove = async (p) => {
|
||||||
|
if (await run(p.id, () => api.deleteCheckpoint(advId, p.id))) setConfirming(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="save-point-panel">
|
||||||
|
<form className="save-point-new" onSubmit={create}>
|
||||||
|
<label htmlFor="save-point-name">Save this moment</label>
|
||||||
|
<input
|
||||||
|
id="save-point-name"
|
||||||
|
autoFocus
|
||||||
|
maxLength={120}
|
||||||
|
placeholder="Before entering the abbey"
|
||||||
|
value={name}
|
||||||
|
disabled={saving}
|
||||||
|
onChange={(e) => setName(e.target.value)}
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
className="save-point-note"
|
||||||
|
maxLength={500}
|
||||||
|
placeholder="A note, if you want one (optional)"
|
||||||
|
value={note}
|
||||||
|
disabled={saving}
|
||||||
|
onChange={(e) => setNote(e.target.value)}
|
||||||
|
/>
|
||||||
|
<button type="submit" className="primary" disabled={saving || !name.trim()}>
|
||||||
|
Save Point
|
||||||
|
</button>
|
||||||
|
<p className="save-point-hint">
|
||||||
|
Saves the moment you are reading now. If you have stepped back, that
|
||||||
|
is the moment it saves.
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
{failed && <div className="panel-empty">Couldn’t read the Save Points — {failed}</div>}
|
||||||
|
{!failed && !points && <div className="panel-empty">Reading your Save Points…</div>}
|
||||||
|
{!failed && points?.length === 0 && (
|
||||||
|
<p className="save-point-intro">
|
||||||
|
No Save Points yet. Name a moment you might want to come back to, then
|
||||||
|
keep playing — going back to it later leaves everything you wrote
|
||||||
|
after it in place.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="save-point-list">
|
||||||
|
{(points || []).map((p) => {
|
||||||
|
const isRenaming = renaming?.id === p.id
|
||||||
|
const busy = busyId === p.id
|
||||||
|
const confirm = confirming?.id === p.id ? confirming.kind : null
|
||||||
|
return (
|
||||||
|
<div key={p.id} className={`save-point-row ${p.on_path ? '' : 'elsewhere'}`}>
|
||||||
|
<div className="save-point-head">
|
||||||
|
{isRenaming ? (
|
||||||
|
<input
|
||||||
|
className="save-point-rename"
|
||||||
|
autoFocus
|
||||||
|
maxLength={120}
|
||||||
|
value={renaming.text}
|
||||||
|
onChange={(e) => setRenaming({ ...renaming, text: e.target.value })}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === 'Enter') rename(p)
|
||||||
|
if (e.key === 'Escape') setRenaming(null)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<span className="save-point-name">{p.name}</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="save-point-meta">
|
||||||
|
Moment {p.turn}
|
||||||
|
{/* Said plainly, without naming a branch: the story took a
|
||||||
|
different turning after this point, and going back to it
|
||||||
|
returns to the telling it was saved in. */}
|
||||||
|
{!p.on_path && p.resolved && ' · on a path you left'}
|
||||||
|
{!p.resolved && ' · this moment is no longer in the story'}
|
||||||
|
</div>
|
||||||
|
{p.note && <div className="save-point-note-text">{p.note}</div>}
|
||||||
|
|
||||||
|
{confirm === 'restore' ? (
|
||||||
|
<div className="save-point-confirm">
|
||||||
|
<span>
|
||||||
|
The story will return to this Save Point. Everything you
|
||||||
|
wrote after it is kept — it just stops being where you are.
|
||||||
|
</span>
|
||||||
|
<button type="button" className="primary" disabled={busy}
|
||||||
|
onClick={() => restore(p)}>Restore</button>
|
||||||
|
<button type="button" onClick={() => setConfirming(null)}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
) : confirm === 'delete' ? (
|
||||||
|
<div className="save-point-confirm">
|
||||||
|
<span>
|
||||||
|
Delete this Save Point? Deleting it does not delete any of
|
||||||
|
the story — only the name you gave this moment.
|
||||||
|
</span>
|
||||||
|
<button type="button" className="danger" disabled={busy}
|
||||||
|
onClick={() => remove(p)}>Delete</button>
|
||||||
|
<button type="button" onClick={() => setConfirming(null)}>Keep</button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="save-point-tools">
|
||||||
|
<button type="button" disabled={busy || !p.resolved}
|
||||||
|
title={p.resolved ? undefined
|
||||||
|
: 'The moment this Save Point named is no longer in the story.'}
|
||||||
|
onClick={() => setConfirming({ id: p.id, kind: 'restore' })}>
|
||||||
|
Restore
|
||||||
|
</button>
|
||||||
|
{isRenaming ? (
|
||||||
|
<>
|
||||||
|
<button type="button" disabled={busy} onClick={() => rename(p)}>Save</button>
|
||||||
|
<button type="button" onClick={() => setRenaming(null)}>Cancel</button>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<button type="button" disabled={busy}
|
||||||
|
onClick={() => setRenaming({ id: p.id, text: p.name })}>
|
||||||
|
Rename
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<button type="button" className="danger" disabled={busy}
|
||||||
|
onClick={() => setConfirming({ id: p.id, kind: 'delete' })}>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export { SavePointPanel }
|
||||||
@@ -255,6 +255,102 @@
|
|||||||
flex-basis: 100%;
|
flex-basis: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Save Points (M4). Deliberately the same furniture as the branch panel: a Save
|
||||||
|
Point is a name for a position, and it should not look like a different
|
||||||
|
species of thing from the lines it names positions on. The confirmations
|
||||||
|
borrow the branch panel's shape but not its danger colouring — restoring
|
||||||
|
destroys nothing, and only deletion is red. */
|
||||||
|
.save-point-panel { display: flex; flex-direction: column; gap: 14px; }
|
||||||
|
.save-point-new { display: flex; flex-direction: column; gap: 7px; }
|
||||||
|
.save-point-new label {
|
||||||
|
font-size: 0.64rem;
|
||||||
|
letter-spacing: 0.14em;
|
||||||
|
text-transform: uppercase;
|
||||||
|
color: var(--text-dim);
|
||||||
|
}
|
||||||
|
.save-point-new input {
|
||||||
|
padding: 5px 9px;
|
||||||
|
font-family: var(--font-story);
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
.save-point-new input.save-point-note {
|
||||||
|
font-size: 0.82rem;
|
||||||
|
}
|
||||||
|
.save-point-hint, .save-point-intro {
|
||||||
|
margin: 0;
|
||||||
|
color: var(--text-dim);
|
||||||
|
font-size: 0.78rem;
|
||||||
|
line-height: 1.55;
|
||||||
|
}
|
||||||
|
.save-point-list { display: flex; flex-direction: column; gap: 8px; }
|
||||||
|
.save-point-row {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 5px;
|
||||||
|
padding: 9px 11px;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 5px;
|
||||||
|
background: var(--bg-input);
|
||||||
|
}
|
||||||
|
/* A Save Point naming a moment on a telling the story has left. Dimmed rather
|
||||||
|
than hidden: it still restores, and hiding it would be the automatic cleanup
|
||||||
|
this milestone deliberately does not do. */
|
||||||
|
.save-point-row.elsewhere { opacity: 0.72; }
|
||||||
|
.save-point-head { display: flex; align-items: center; gap: 7px; }
|
||||||
|
.save-point-name {
|
||||||
|
font-family: var(--font-story);
|
||||||
|
font-size: 0.98rem;
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
.save-point-rename {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
padding: 3px 7px;
|
||||||
|
font-family: var(--font-story);
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
.save-point-meta {
|
||||||
|
font-size: 0.72rem;
|
||||||
|
color: var(--text-dim);
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
}
|
||||||
|
.save-point-note-text {
|
||||||
|
font-size: 0.78rem;
|
||||||
|
color: var(--text-dim);
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
.save-point-tools, .save-point-confirm {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 5px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
.save-point-tools button, .save-point-confirm button {
|
||||||
|
padding: 3px 9px;
|
||||||
|
font-size: 0.72rem;
|
||||||
|
color: var(--text-dim);
|
||||||
|
background: transparent;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
}
|
||||||
|
.save-point-tools button:hover:not(:disabled),
|
||||||
|
.save-point-confirm button:hover:not(:disabled) {
|
||||||
|
color: var(--accent-bright);
|
||||||
|
border-color: var(--border-bright);
|
||||||
|
}
|
||||||
|
.save-point-tools button.danger:hover:not(:disabled),
|
||||||
|
.save-point-confirm button.danger:hover:not(:disabled) {
|
||||||
|
color: var(--danger);
|
||||||
|
border-color: var(--danger);
|
||||||
|
}
|
||||||
|
.save-point-tools button:disabled,
|
||||||
|
.save-point-confirm button:disabled { opacity: 0.4; cursor: default; }
|
||||||
|
.save-point-confirm span {
|
||||||
|
font-size: 0.74rem;
|
||||||
|
color: var(--text-dim);
|
||||||
|
line-height: 1.5;
|
||||||
|
flex-basis: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
.action-edit { margin-bottom: 14px; }
|
.action-edit { margin-bottom: 14px; }
|
||||||
/* Sized by AutoTextarea to fit the text being edited — an AI beat is usually
|
/* Sized by AutoTextarea to fit the text being edited — an AI beat is usually
|
||||||
several paragraphs, and the old fixed 110px turned that into a keyhole.
|
several paragraphs, and the old fixed 110px turned that into a keyhole.
|
||||||
|
|||||||
@@ -1,122 +0,0 @@
|
|||||||
# AI D&D — Local AI Dungeon Clone: Plan Overview
|
|
||||||
|
|
||||||
> **[STATUS.md](STATUS.md) — where things stand and what to pick up next.** Read that
|
|
||||||
> first; this file is the shape of the project, not its current state.
|
|
||||||
|
|
||||||
A locally hosted web app replicating AI Dungeon's core experience: scenarios, adventures,
|
|
||||||
AI-driven storytelling, AI Dungeon-style memory/context management, JavaScript scripting
|
|
||||||
(compatible with real AI Dungeon scripts), and full transparency into what is sent to the AI.
|
|
||||||
|
|
||||||
## Confirmed decisions
|
|
||||||
|
|
||||||
| Area | Decision |
|
|
||||||
|---|---|
|
|
||||||
| Backend | Python — FastAPI + SQLAlchemy + SQLite (single-user, local) |
|
|
||||||
| Frontend | React SPA (Vite), dark AI Dungeon-like theme |
|
|
||||||
| AI provider | Provider-agnostic adapter layer; first adapter: **OpenAI-compatible** (`/v1/chat/completions`) — covers Ollama, LM Studio, OpenAI, OpenRouter, vLLM, Groq. Endpoint URL, API key, model name all configurable at runtime. |
|
|
||||||
| Scripting | **JavaScript, AI Dungeon-compatible** (`onInput` / `onModelContext` / `onOutput` modifiers, shared `state`, `worldEntries` API) via an embedded JS engine (quickjs / py-mini-racer). Real AI Dungeon scripts should import and run. |
|
|
||||||
| Import/export | AI Dungeon-compatible formats for scripts and scenarios; JSON export/import for everything. |
|
|
||||||
|
|
||||||
## Architecture at a glance
|
|
||||||
|
|
||||||
```
|
|
||||||
frontend/ React + Vite SPA ──HTTP/SSE──► backend/ FastAPI
|
|
||||||
├─ routers/ (scenarios, adventures, actions, scripts, settings, insights)
|
|
||||||
├─ models/ (SQLAlchemy: Scenario, Adventure, Action, StoryCard, Script, Settings)
|
|
||||||
├─ context/ (prompt assembly: memory, author's note, world info, history budget)
|
|
||||||
├─ scripting/ (JS sandbox, AI Dungeon API surface, per-adventure state)
|
|
||||||
├─ providers/ (base adapter + openai_compatible.py; streaming)
|
|
||||||
└─ data.db (SQLite)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Core domain model
|
|
||||||
|
|
||||||
- **Scenario** — template: title, description, opening prompt (with `${placeholders}`), memory,
|
|
||||||
author's note, story cards (world info), attached scripts, tags.
|
|
||||||
- **Adventure** — a playthrough created from a scenario (or blank). Owns its own copy of memory,
|
|
||||||
author's note, story cards, script state, and the action list.
|
|
||||||
- **Action** — one entry in the story: type (`do` / `say` / `story` / `continue` / AI output),
|
|
||||||
text, timestamp, plus the **context snapshot** (exact prompt sent to the AI) for Insights.
|
|
||||||
- **Story Card / World Info** — keys (comma-separated keywords), entry text, optional type/notes.
|
|
||||||
Injected into context only when a key matches recent story text.
|
|
||||||
- **Script** — JS source per hook (input / context / output modifier), attachable to scenarios;
|
|
||||||
copied into adventures with persistent `state`.
|
|
||||||
|
|
||||||
## The turn pipeline (heart of the app)
|
|
||||||
|
|
||||||
```
|
|
||||||
player input
|
|
||||||
→ onInput script modifier
|
|
||||||
→ store player action
|
|
||||||
→ assemble context: [AI instructions] + [plot essentials] + [story summary]
|
|
||||||
+ [triggered story cards ("World Lore:")] + [story history, token-budgeted]
|
|
||||||
+ [author's note inserted N lines from the end] + [player action]
|
|
||||||
→ onModelContext script modifier
|
|
||||||
→ snapshot context (Insights)
|
|
||||||
→ provider adapter → AI (streamed)
|
|
||||||
→ onOutput script modifier
|
|
||||||
→ store AI action → render
|
|
||||||
```
|
|
||||||
|
|
||||||
## Phases
|
|
||||||
|
|
||||||
1. **[Phase 1 — Foundation](01-phase-foundation.md)**: repo scaffold, FastAPI + SQLite models,
|
|
||||||
React shell, scenario/adventure CRUD, settings (endpoint config).
|
|
||||||
2. **[Phase 2 — Play loop + AI](02-phase-play-loop.md)**: provider adapter with streaming,
|
|
||||||
Do/Say/Story/Continue, Retry/Undo/Edit, the adventure play screen.
|
|
||||||
3. **[Phase 3 — Context engine + Insights](03-phase-context-insights.md)**: memory, author's note,
|
|
||||||
story cards with keyword triggering, token budgeting, per-turn prompt snapshots + Insights UI.
|
|
||||||
4. **[Phase 4 — Scripting](04-phase-scripting.md)**: embedded JS sandbox, AI Dungeon scripting API,
|
|
||||||
script editor, script + scenario import/export (AI Dungeon-compatible).
|
|
||||||
5. **[Phase 5 — Polish](05-phase-polish.md)**: AI Dungeon-like theming pass, placeholders on
|
|
||||||
scenario start, adventure export/import, quality-of-life and hardening.
|
|
||||||
6. **[Phase 6 — Auto Summarization + Memory Bank](06-phase-memory-bank.md)** *(optional)*:
|
|
||||||
modern AI Dungeon memory system — AI-generated memories every 6 actions, Story Summary every
|
|
||||||
15, embedding-based retrieval of relevant memories into context.
|
|
||||||
|
|
||||||
Each phase ends with the app runnable and testable end-to-end.
|
|
||||||
|
|
||||||
## Public release (phases 7–10)
|
|
||||||
|
|
||||||
Goal: public GitHub repo + hosted multi-user deployment, linkable from resume/website.
|
|
||||||
|
|
||||||
| Area | Decision |
|
|
||||||
|---|---|
|
|
||||||
| License | MIT |
|
|
||||||
| Auth | Email + password, **optional** — guest sessions play instantly, register to keep data |
|
|
||||||
| Signup | Open (rate-limited) |
|
|
||||||
| LLM keys | BYOK per user + shared server-funded demo key (capped, details TBD in Phase 8) |
|
|
||||||
| Database (hosted) | **TBD — ask at start of Phase 9** (SQLite-on-disk vs Postgres) |
|
|
||||||
| Hosting | Render (tier TBD in Phase 10) |
|
|
||||||
| Domain | Platform URL (custom domain later, optional) |
|
|
||||||
| README media | Deferred — text-only first, screenshots/GIF in a later pass |
|
|
||||||
|
|
||||||
Open questions are recorded at the top of each phase file under "Ask before implementing".
|
|
||||||
|
|
||||||
7. **[Phase 7 — Public repo & portability](07-phase-public-repo.md)**: MIT license, portfolio
|
|
||||||
README, Dockerfile + compose, cross-platform run instructions, publish to GitHub.
|
|
||||||
8. **[Phase 8 — Optional accounts & multi-user](08-phase-accounts.md)** *(the big one)*:
|
|
||||||
guest-first sessions, optional email+password upgrade, per-user data scoping across all
|
|
||||||
routers/tables, per-user encrypted BYOK settings, shared demo key with caps.
|
|
||||||
9. **[Phase 9 — Production hardening](09-phase-hardening.md)**: env-var config, quickjs
|
|
||||||
time/memory limits, rate limiting, size/row caps, locked-down debug surface, production
|
|
||||||
serving, database decision.
|
|
||||||
10. **[Phase 10 — Deploy & publish](10-phase-deploy.md)**: Render blueprint + deploy, seeded
|
|
||||||
demo scenarios, live smoke test, resume/website links and blurb.
|
|
||||||
|
|
||||||
## Post-launch
|
|
||||||
|
|
||||||
- **[State revert + retry fix](11-state-revert-and-retry-fix.md)**: undo/retry roll the shared
|
|
||||||
`script_state` back; per-action `state_before` snapshots; undo concurrency lock.
|
|
||||||
- **[Phase 12 — RPG world state](12-phase-rpg-world-state.md)**: structured world/player/NPC stats
|
|
||||||
+ milestones per scenario (`stat_schema`); the AI proposes deltas, a Python engine clamps them
|
|
||||||
(min/max, per-turn cap, cooldown, sticky milestones); band descriptions keep the model honest;
|
|
||||||
World State drawer + Insights delta report; reuses the Phase 11 undo/retry snapshot.
|
|
||||||
- **[Memory-bank embedding cost](13-memory-embedding-cost.md)** *(round three of the egress
|
|
||||||
work)*: a turn fetched the whole memory bank's vectors to pick five — 96% of everything it
|
|
||||||
read. Packed float32 + an in-process cache + SQL-side filtering; a played turn went 6.4 MB
|
|
||||||
to 123 kB. Includes the byte-meter harness (`backend/tools/`). One step left, see STATUS.
|
|
||||||
- **[Phase 14 — Story tree](14-phase-story-tree.md)** *(designed, not started)*: the linear
|
|
||||||
action list becomes a branching tree, so a retry is a sibling rather than a rewrite and both
|
|
||||||
paths survive. The real argument is bug elimination — seven bug classes trace to "the story
|
|
||||||
is a mutable list" and all disappear when nothing is rewritten in place.
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
# Phase 1 — Foundation
|
|
||||||
|
|
||||||
**Goal:** runnable skeleton — backend serving a database-backed API, frontend shell with
|
|
||||||
navigation, scenario & adventure CRUD, and a settings page for the AI endpoint. No AI calls yet.
|
|
||||||
|
|
||||||
## Backend
|
|
||||||
|
|
||||||
- [x] Project scaffold: `backend/` with FastAPI app, uvicorn entrypoint, `requirements.txt`
|
|
||||||
(fastapi, uvicorn, sqlalchemy, pydantic, httpx, tiktoken; quickjs deferred to phase 4).
|
|
||||||
- [x] SQLite via SQLAlchemy; auto-create `data.db` on first run.
|
|
||||||
- [x] Models:
|
|
||||||
- `Scenario`: id, title, description, prompt, memory, authors_note, tags, created/updated.
|
|
||||||
- `StoryCard`: id, owner (scenario or adventure), keys, entry, type, title, description.
|
|
||||||
- `Adventure`: id, scenario_id (nullable), title, memory, authors_note, script_state (JSON),
|
|
||||||
created/updated.
|
|
||||||
- `Action`: id, adventure_id, index, type (`do|say|story|continue|ai|start`), text,
|
|
||||||
context_snapshot (JSON, nullable), created.
|
|
||||||
- `Script`: id, name, description, input_js, context_js, output_js, library_js.
|
|
||||||
- `Settings`: single row — endpoint_url, api_key, model, temperature, max_output_tokens,
|
|
||||||
context_token_budget, api_mode (`chat|completion`).
|
|
||||||
- [x] Routers: CRUD for scenarios, adventures (+ create-from-scenario copying memory/AN/cards),
|
|
||||||
story cards, settings. Consistent JSON errors.
|
|
||||||
- [x] CORS for the Vite dev server; production mode serves built frontend as static files.
|
|
||||||
|
|
||||||
## Frontend
|
|
||||||
|
|
||||||
- [x] Vite + React scaffold in `frontend/`; router with pages: Home (adventure list),
|
|
||||||
Scenarios (list + editor), Play (placeholder), Settings.
|
|
||||||
- [x] Dark base theme (AI Dungeon-like: near-black background, serif story font, gold/teal accent).
|
|
||||||
- [x] Scenario editor: title, description, prompt, memory, author's note, story card list editor.
|
|
||||||
- [x] Settings page: endpoint URL, API key, model name, sampling params; "Test connection" button
|
|
||||||
(backend proxies a trivial request — wired for real in Phase 2, stub now).
|
|
||||||
- [x] "New adventure" flow: pick scenario (or blank) → creates adventure → navigates to Play page.
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
Run `uvicorn` + `npm run dev`, create/edit/delete scenarios with story cards, start an adventure
|
|
||||||
from one, see it listed on Home, and save endpoint settings — all persisted across restarts.
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
# Phase 2 — Play loop + AI provider
|
|
||||||
|
|
||||||
**Goal:** the core game is playable. Player acts, AI continues the story, streamed live.
|
|
||||||
|
|
||||||
## Provider adapter layer
|
|
||||||
|
|
||||||
- [x] `providers/base.py`: abstract `Provider` — `generate(prompt_parts, params) -> async stream of text`.
|
|
||||||
Takes an assembled context object (system text + story text), so providers decide how to
|
|
||||||
map it to their wire format.
|
|
||||||
- [x] `providers/openai_compatible.py`:
|
|
||||||
- Chat mode: system message carries instructions/memory; story history flows in as
|
|
||||||
user/assistant text continuation framing suited to a chat endpoint. A configurable
|
|
||||||
"narrator" system prompt frames the AI as a second-person storyteller continuing the text.
|
|
||||||
- Streaming via SSE from the endpoint, re-streamed to the browser.
|
|
||||||
- Optional raw completion mode (`/v1/completions`) for pure-continuation models.
|
|
||||||
- [x] Errors surfaced cleanly (bad key, connection refused, model not found) with retry affordance.
|
|
||||||
- [x] "Test connection" on Settings now real.
|
|
||||||
|
|
||||||
## Turn engine (`POST /adventures/{id}/actions`)
|
|
||||||
|
|
||||||
- [x] Input formatting per AI Dungeon conventions:
|
|
||||||
- **Do** → `> You <text>` (normalized to second person, stripped punctuation as needed)
|
|
||||||
- **Say** → `> You say "<text>"`
|
|
||||||
- **Story** → raw text appended
|
|
||||||
- **Continue** → no player text; AI just continues
|
|
||||||
- [x] Simple context for this phase: opening prompt + full history, truncated from the top to the
|
|
||||||
token budget (tiktoken count). Real context engine lands in Phase 3.
|
|
||||||
- [x] Response streamed to the client via SSE; final text stored as an `ai` action.
|
|
||||||
- [x] **Retry**: delete last AI action, regenerate with same input.
|
|
||||||
- [x] **Undo/Erase**: delete last action pair (player + AI) or single action.
|
|
||||||
- [x] **Edit**: PATCH any action's text in place.
|
|
||||||
|
|
||||||
## Play UI
|
|
||||||
|
|
||||||
- [x] Story view: continuous prose (not chat bubbles), player actions styled distinctly
|
|
||||||
(`>` prefix, accent color), auto-scroll, streaming text renders token-by-token.
|
|
||||||
- [x] Input bar with mode selector (Do / Say / Story) + Continue button; Enter to send.
|
|
||||||
- [x] Per-turn controls: Retry, Undo, Edit (inline contenteditable or textarea swap).
|
|
||||||
- [x] Loading/streaming state, error toast with retry.
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
Point Settings at any OpenAI-compatible endpoint (e.g. Ollama or LM Studio locally), start an
|
|
||||||
adventure, and play a multi-turn story with all four input modes plus retry/undo/edit, with
|
|
||||||
streaming output.
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
# Phase 3 — Context engine + Insights
|
|
||||||
|
|
||||||
**Goal:** AI Dungeon-grade context management, and full visibility into every prompt.
|
|
||||||
|
|
||||||
## Context assembly (`context/builder.py`)
|
|
||||||
|
|
||||||
Assembles the prompt each turn from AI Dungeon's **plot components**
|
|
||||||
(per help.aidungeon.com/faq/the-memory-system):
|
|
||||||
|
|
||||||
```
|
|
||||||
[AI Instructions] ↠behavioral guidance for the model (always included)
|
|
||||||
[Plot Essentials] ↠key facts for constant recall — the classic "Memory" (always)
|
|
||||||
[Story Summary] ↠running summary slot; manual in this phase, auto in Phase 6
|
|
||||||
[Triggered Story Cards] ↠"World Lore: <entry>" for each triggered card (conditional)
|
|
||||||
[Story history] ↠as many recent actions as fit the token budget
|
|
||||||
[Author's Note] ↠injected N lines (default 3) before the end of history
|
|
||||||
[Latest player action] ↠(+ script frontMemory right after it, Phase 4)
|
|
||||||
```
|
|
||||||
|
|
||||||
- [x] **AI Instructions / Plot Essentials / Story Summary / Author's Note**: adventure-level
|
|
||||||
free-text fields, always included, editable mid-adventure from the side panel.
|
|
||||||
- [x] **Story cards** — five fields per official docs: **Type** (organizational, not sent to AI),
|
|
||||||
**Name** (not sent to AI), **Entry** (sent when triggered), **Triggers**, **Notes** (not sent).
|
|
||||||
- Triggers: comma-separated words/phrases; **case-insensitive but space-sensitive**;
|
|
||||||
**partial-word matching** (`boat` triggers on `boats`); matched against both player input
|
|
||||||
and AI output in the recent-story window.
|
|
||||||
- Not instant: a card triggered mid-response only enters context on the *next* turn; once
|
|
||||||
triggered, stays active while the triggering text remains in the context window.
|
|
||||||
- Triggered entries injected once each, prefixed `World Lore:`; story cards are the **first
|
|
||||||
component dropped** when context is full.
|
|
||||||
- Editable per-adventure (copied from scenario at creation). Soft-cap sanity limit (AI Dungeon
|
|
||||||
allows 5,000/adventure).
|
|
||||||
- [x] **Author's Note**: inserted near the end (strongest steering position), formatted
|
|
||||||
`[Author's note: <text>]`.
|
|
||||||
- [x] **Token budgeting** with tiktoken: always-included components (AI Instructions, Plot
|
|
||||||
Essentials, Story Summary, Author's Note) reserved first; story cards get a capped share
|
|
||||||
and are dropped first when over budget; remainder goes to story history (newest first).
|
|
||||||
Budget = `context_token_budget` setting.
|
|
||||||
- [x] Slots for script-provided memory overrides (Phase 4): `state.memory.context` (prepended),
|
|
||||||
`state.memory.authorsNote` (replaces/augments author's note), `state.memory.frontMemory`
|
|
||||||
(inserted immediately after the latest player action).
|
|
||||||
- [x] Builder returns a structured `ContextReport`: ordered sections, each with source label,
|
|
||||||
text, token count; plus totals and a list of triggered cards (and which keyword fired).
|
|
||||||
|
|
||||||
## Insights
|
|
||||||
|
|
||||||
- [x] Every AI turn stores its `ContextReport` on the `Action` row (`context_snapshot`).
|
|
||||||
- [x] `GET /adventures/{id}/actions/{id}/context` returns it.
|
|
||||||
- [x] **Insights panel** in Play UI (drawer/tab):
|
|
||||||
- Exact final prompt text as sent, sectioned and color-coded (memory / world info / history /
|
|
||||||
author's note / input), with per-section token counts and total vs budget.
|
|
||||||
- Which story cards triggered and on which keyword; which history got cut off.
|
|
||||||
- Viewable for the *upcoming* turn (dry-run endpoint: "what would be sent now") and for any
|
|
||||||
past AI action.
|
|
||||||
- [x] Adventure side panel: edit memory, author's note, story cards mid-game (AI Dungeon's
|
|
||||||
right-hand panel equivalent).
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
Create a card with key `dragon`; mention a dragon in play and see the card enter the context in
|
|
||||||
the Insights panel (and influence the AI); verify memory and author's note appear in the snapshot
|
|
||||||
in the right positions; long adventures visibly trim oldest history within budget.
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
# Phase 4 — Scripting (AI Dungeon-compatible JavaScript)
|
|
||||||
|
|
||||||
**Goal:** real AI Dungeon scripts import and run: the three modifier hooks, persistent `state`,
|
|
||||||
and the scripting API surface.
|
|
||||||
|
|
||||||
## JS runtime
|
|
||||||
|
|
||||||
- [x] Embed a JS engine in Python: **quickjs** (preferred; check Windows wheel availability at
|
|
||||||
implementation time; fallback: py-mini-racer, or Node subprocess as last resort).
|
|
||||||
- [x] Sandbox matching AI Dungeon's documented limits: each hook runs **isolated**, **16 MB
|
|
||||||
memory cap**, **2-second timeout**; no filesystem/network/process access; script errors
|
|
||||||
captured and surfaced in the UI, never crash a turn.
|
|
||||||
|
|
||||||
## AI Dungeon scripting model (compatibility target)
|
|
||||||
|
|
||||||
*(Per official docs: help.aidungeon.com/faq/how-do-i-write-scripts-and-use-scripting)*
|
|
||||||
|
|
||||||
Three lifecycle hooks — `onInput`, `onModelContext`, `onOutput`. Each script defines a modifier
|
|
||||||
and **must call it as its last line**:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const modifier = (text) => {
|
|
||||||
// script logic
|
|
||||||
return { text, stop }
|
|
||||||
}
|
|
||||||
modifier(text)
|
|
||||||
```
|
|
||||||
|
|
||||||
- **onInput** — modifies player input before context construction.
|
|
||||||
- **onModelContext** — modifies the assembled text sent to the model.
|
|
||||||
- **onOutput** — modifies the model output before it is shown/stored.
|
|
||||||
- **Shared Library** — code prepended to all three slots.
|
|
||||||
|
|
||||||
Return contract:
|
|
||||||
- [x] `{ text, stop }`; `stop: true` from onInput prevents the AI call.
|
|
||||||
- [x] Empty-string `text` from onInput/onOutput → user-facing error (replicate this behavior).
|
|
||||||
|
|
||||||
Globals provided (exact names from docs):
|
|
||||||
|
|
||||||
- [x] `text` — hook input (player input / context / AI response respectively).
|
|
||||||
- [x] `state` — persisted per adventure across turns (`Adventure.script_state`); includes
|
|
||||||
`state.memory`, `state.message` (shown as a UI notice), `state.placeholders`.
|
|
||||||
- [x] `state.memory` slots: `context` (prepended to context), `authorsNote` (near end, before
|
|
||||||
latest response), `frontMemory` (inserted right after the player's input).
|
|
||||||
- [x] `history` — array of recent actions: `{ text, rawText, type }`.
|
|
||||||
- [x] `storyCards` — array of `{ id, keys, entry, type }`, backed by the adventure's story cards.
|
|
||||||
- [x] Story card functions: `addStoryCard(keys, entry, type)` → index (or `false` on duplicate),
|
|
||||||
`updateStoryCard(index, keys, entry, type)` and `removeStoryCard(index)` → throw if absent.
|
|
||||||
- [x] Legacy aliases for older scripts: `worldInfo` / `worldEntries`, `addWorldEntry`,
|
|
||||||
`updateWorldEntry`, `removeWorldEntry` mapped onto the storyCards implementation.
|
|
||||||
- [x] `info` — `{ actionCount, characterNames, memoryLength, maxChars }`.
|
|
||||||
- [x] `log(message)` / `console.log` — captured per turn, shown in a script log panel.
|
|
||||||
|
|
||||||
## Pipeline integration
|
|
||||||
|
|
||||||
```
|
|
||||||
player input → INPUT modifier → format & store
|
|
||||||
context build → CONTEXT modifier → (snapshot includes pre- and post-script versions in Insights)
|
|
||||||
AI response → OUTPUT modifier → store & render
|
|
||||||
```
|
|
||||||
|
|
||||||
- [x] Insights (Phase 3) extended: show context before vs after the context modifier (diff view),
|
|
||||||
and script log output per turn.
|
|
||||||
|
|
||||||
## Script management UI
|
|
||||||
|
|
||||||
- [x] Scripts page: create/edit scripts with a code editor (CodeMirror), one tab per slot
|
|
||||||
(Library / Input / Context / Output), description field.
|
|
||||||
- [x] Attach scripts to scenarios; adventures inherit at creation. Enable/disable per adventure.
|
|
||||||
- [x] Test-run a script against sample text without an AI call.
|
|
||||||
|
|
||||||
## Import / Export
|
|
||||||
|
|
||||||
- [x] **Scripts**: export/import as JSON bundle `{ name, library, input, context, output }` and
|
|
||||||
as raw `.js` files per slot (matching how AI Dungeon scripts circulate — paste or file).
|
|
||||||
- [x] **Scenarios**: export/import JSON including prompt, memory, author's note, story cards,
|
|
||||||
and attached scripts. Accept AI Dungeon scenario export JSON where format is known;
|
|
||||||
map fields best-effort and report anything unmapped.
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
Paste a real AI Dungeon script (e.g. a simple input modifier + state counter + world entry
|
|
||||||
manipulation) and it runs unmodified across turns; state persists; export a scenario with scripts,
|
|
||||||
re-import it into a fresh database, and play it.
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
# Phase 5 — Polish & quality of life
|
|
||||||
|
|
||||||
**Goal:** the app feels like AI Dungeon — cohesive dark UI, smooth flows, safe data handling.
|
|
||||||
|
|
||||||
## UI/UX pass
|
|
||||||
|
|
||||||
- [x] Theming: refined dark palette, serif story typography, subtle textures/gradients à la
|
|
||||||
AI Dungeon; consistent buttons, panels, modals; responsive layout.
|
|
||||||
- [x] Home: adventure cards with scenario name, last-played time, action count; search/filter;
|
|
||||||
scenario gallery with tags.
|
|
||||||
- [x] Play screen: collapsible right side panel (Memory / Cards / Scripts / Insights tabs),
|
|
||||||
keyboard shortcuts (Enter send, Ctrl+Z undo, Ctrl+R retry), smooth streaming autoscroll
|
|
||||||
that pauses when the user scrolls up.
|
|
||||||
- [x] Scenario **placeholders**: `${Character name}` style variables in prompt/memory prompt the
|
|
||||||
player for values when starting an adventure (AI Dungeon behavior).
|
|
||||||
|
|
||||||
## Data & robustness
|
|
||||||
|
|
||||||
- [x] Adventure export/import (full JSON: actions, memory, cards, script state) — backup/share.
|
|
||||||
- [x] Delete confirmations (trash/soft-delete skipped — plain confirm dialogs).
|
|
||||||
- [x] SQLite migrations story (versioned schema bootstrap via PRAGMA user_version —
|
|
||||||
`backend/app/migrations.py`).
|
|
||||||
- [x] Request logging + a debug page tailing recent provider requests/responses (bodies redacted
|
|
||||||
of API key) — "Recent AI requests" on the Settings page.
|
|
||||||
- [x] Graceful handling: provider timeout/cancel (stop generation button), concurrent turn lock
|
|
||||||
per adventure.
|
|
||||||
|
|
||||||
## Nice-to-haves (only if time/interest)
|
|
||||||
|
|
||||||
- [ ] Multiple provider profiles with quick switching (e.g. local Ollama vs OpenRouter).
|
|
||||||
- [ ] Per-scenario generation params overriding global settings.
|
|
||||||
- [ ] Retry with "give me something different" (temperature bump / anti-repeat nudge).
|
|
||||||
- [ ] Basic light theme toggle.
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
A friend could sit down at `localhost`, start a scenario with placeholders, play comfortably,
|
|
||||||
peek at Insights, and you can back up / restore everything via export files.
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
# Phase 6 — Auto Summarization + Memory Bank (optional)
|
|
||||||
|
|
||||||
**Goal:** replicate modern AI Dungeon's Memory System (per
|
|
||||||
help.aidungeon.com/faq/the-memory-system): AI-generated memories, a running Story Summary, and
|
|
||||||
embedding-based retrieval. This phase makes extra AI calls (summarization + embeddings), so it is
|
|
||||||
opt-in per adventure and gated on the endpoint supporting it.
|
|
||||||
|
|
||||||
## Auto Summarization
|
|
||||||
|
|
||||||
- [x] **Memories**: every 6 actions (starting at action 12), summarize that block of
|
|
||||||
player actions + AI responses into a short "memory" via a background AI call
|
|
||||||
(same provider, cheap/configurable model override).
|
|
||||||
- [x] **Story Summary**: every 15 actions, update the Story Summary plot component — a running
|
|
||||||
overview of the plot — folding in recent memories; compress it when it grows too long.
|
|
||||||
- [x] Story Summary stays **manually editable**; user edits inform future updates
|
|
||||||
(they are the base text for the next summarization pass) but are never overwritten silently.
|
|
||||||
- [x] Summarization failures are non-fatal: log, retry next interval.
|
|
||||||
(Failed calls appear on the debug page; cursors only advance on success, so the
|
|
||||||
next turn retries. Implementation: `backend/app/memorybank.py`.)
|
|
||||||
|
|
||||||
## Memory Bank
|
|
||||||
|
|
||||||
- [x] Store each memory with an **embedding vector** (OpenAI-compatible `/v1/embeddings`;
|
|
||||||
embedding model configurable in Settings; feature disabled if unavailable).
|
|
||||||
- [x] Each turn, embed the recent story text and rank memories by cosine similarity;
|
|
||||||
inject the top-K "Used Memories" into context as their own component
|
|
||||||
(between Story Summary and story cards in the layout). Pinned memories are always
|
|
||||||
included; top-K is a setting (default 5).
|
|
||||||
- [x] Configurable bank capacity (AI Dungeon tiers: 25–400; ours: a setting, default 200);
|
|
||||||
when full, evict least-recently-used/least-retrieved memories ("Forgotten Memories").
|
|
||||||
- [x] SQLite storage for vectors (JSON blob + in-memory cosine ranking — pure Python,
|
|
||||||
no numpy needed at this scale).
|
|
||||||
|
|
||||||
## UI
|
|
||||||
|
|
||||||
- [x] Memory Bank panel: list memories (used / idle / forgotten), edit or delete, pin favorites,
|
|
||||||
see which memories were retrieved for a given turn (🔍 on an AI action → Insights snapshot).
|
|
||||||
- [x] Insights integration: retrieved memories shown as a context section with similarity scores.
|
|
||||||
- [x] Adventure settings: toggle auto-summarization / memory bank (per adventure, in the Memory
|
|
||||||
panel); summary + embedding models are chosen globally in Settings (deliberate
|
|
||||||
simplification — one endpoint config for the whole app).
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
Play a 40+ action adventure: memories appear every 6 actions, the Story Summary updates every 15,
|
|
||||||
an early-game fact that scrolled out of the raw history gets retrieved via the Memory Bank when it
|
|
||||||
becomes relevant again, and Insights shows exactly which memories were injected and why.
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
# Phase 7 — Public repo & portability
|
|
||||||
|
|
||||||
**Goal:** make the repo public-worthy and runnable by anyone on any OS, so the GitHub link is
|
|
||||||
immediately usable on a resume — before any hosted-deployment work.
|
|
||||||
|
|
||||||
## Decisions (confirmed)
|
|
||||||
|
|
||||||
| Question | Answer |
|
|
||||||
|---|---|
|
|
||||||
| License | **MIT** |
|
|
||||||
| README media (screenshots/GIF) | **Skip for now** — text-only README; visuals in a later pass |
|
|
||||||
|
|
||||||
**Repo name (decided): `AI-DnD`.** Ask before publishing: whether the existing commit
|
|
||||||
history/messages are fine to publish as-is.
|
|
||||||
|
|
||||||
## Repo hygiene
|
|
||||||
|
|
||||||
- [x] Add `LICENSE` (MIT, current year, Parth Thakkar).
|
|
||||||
- [x] Verify no secrets or user data are tracked (`openrouter_key.env`, `data.db` — already
|
|
||||||
gitignored and never committed; re-verify before push).
|
|
||||||
- [x] Add `backend/.env.example` documenting every env var the app reads (grows in Phase 9).
|
|
||||||
(Currently just `AIDND_DB_PATH`, added to `database.py` for Docker/hosted volumes.)
|
|
||||||
- [x] Decide what to do with `CODE_REVIEW_FINDINGS.md` and `plan/` — keep (shows process, good
|
|
||||||
for a portfolio) — just give them a one-line mention in the README.
|
|
||||||
|
|
||||||
## README rewrite (portfolio-grade, text-only)
|
|
||||||
|
|
||||||
- [x] Pitch paragraph: what it is, what makes it interesting (AI Dungeon-compatible scripting,
|
|
||||||
memory bank with embeddings, full prompt transparency/Insights, provider-agnostic).
|
|
||||||
- [x] Feature list with pointers into the code (scripting engine, context builder, memory bank).
|
|
||||||
- [x] Architecture diagram (reuse/refresh the one in `plan/00-OVERVIEW.md`).
|
|
||||||
- [x] Setup instructions for **Windows (start.ps1), macOS/Linux (manual), and Docker**.
|
|
||||||
- [x] "Bring your own model" section: Ollama / LM Studio / OpenRouter free models — emphasize it
|
|
||||||
runs fully free.
|
|
||||||
- [x] Placeholder section for screenshots/GIF (added in a later pass).
|
|
||||||
|
|
||||||
## Docker (one-command run for non-Windows users)
|
|
||||||
|
|
||||||
- [x] `Dockerfile`: multi-stage — build frontend (`npm run build`), then Python image serving
|
|
||||||
FastAPI with the built SPA mounted (SPA fallback already exists in `app/main.py`).
|
|
||||||
(3 stages: node build → pip wheel build with gcc for quickjs → slim runtime.)
|
|
||||||
- [x] `docker-compose.yml`: single service, named volume mounted at `/data`
|
|
||||||
(`AIDND_DB_PATH=/data/data.db`), port mapping.
|
|
||||||
- [x] `start.sh` for macOS/Linux dev parity with `start.ps1` (optional, nice-to-have).
|
|
||||||
- [x] Test: `docker compose up` from a clean clone → app works at `http://localhost:8000`.
|
|
||||||
Done 2026-07-06 in WSL2 Ubuntu (Docker Engine installed there for this): health + SPA +
|
|
||||||
deep links 200, scenario created via API survives a full `compose down`/`up` (named
|
|
||||||
volume). Two real bugs found and fixed: package-lock.json was missing top-level
|
|
||||||
`@emnapi/*` entries (regenerated with npm 11.18) and the build image needed node 24 to
|
|
||||||
match the npm-11 lockfile. Note: from Windows, reach a WSL-hosted container via the WSL
|
|
||||||
IP (`hostname -I`) — localhost forwarding didn't apply; irrelevant on normal hosts.
|
|
||||||
|
|
||||||
## Publish
|
|
||||||
|
|
||||||
- [ ] Create public GitHub repo (`gh repo create`), push `main`.
|
|
||||||
- [ ] Add repo description, topics (`ai-dungeon`, `fastapi`, `react`, `llm`, `interactive-fiction`).
|
|
||||||
- [ ] Confirm the GitHub rendering of README looks right.
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
A stranger on macOS with Docker installed can clone the repo, run one command, open the app,
|
|
||||||
paste an OpenRouter free-tier key, and play an adventure — without asking you anything.
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
# Phase 8 — Optional accounts & multi-user ✅ (implemented 2026-07-06, branch `phase-8-accounts`)
|
|
||||||
|
|
||||||
**Goal:** turn the single-user app into a multi-user one where **accounts are optional**:
|
|
||||||
a visitor can start playing instantly as a guest, and can register (email + password) at any
|
|
||||||
point to keep their adventures across devices/browsers. This is the largest phase — it touches
|
|
||||||
every router and most tables.
|
|
||||||
|
|
||||||
## Decisions (confirmed)
|
|
||||||
|
|
||||||
| Question | Answer |
|
|
||||||
|---|---|
|
|
||||||
| Auth method | **Email + password, optional** — guest sessions work without an account |
|
|
||||||
| Signup policy | **Open signup** (rate-limited) |
|
|
||||||
| LLM API keys | **BYOK + shared demo key** — users can paste their own key; users without one get limited turns on a server-funded key |
|
|
||||||
| Demo key funding | **OpenRouter free models** (owner's key, `:free` whitelist; default `google/gemma-4-26b-a4b-it:free`) |
|
|
||||||
| Demo turn cap | **20 successful turns/user/day** (failed provider calls don't count) |
|
|
||||||
| Guest data retention | **Never delete** for v1 (no cleanup job; revisit if the DB grows) |
|
|
||||||
| Password reset | **Skipped for v1** (no email provider; forgotten password = lost account) |
|
|
||||||
| Login with an active guest session | Guest is **abandoned**, not merged (its data stays under the guest user) |
|
|
||||||
| Memory bank on demo key | **Disabled** (no background AI calls on the server-funded key; visible note in the Memory panel/Insights) |
|
|
||||||
|
|
||||||
## Data model
|
|
||||||
|
|
||||||
- [x] `User` table: id, email (nullable — null means guest), password_hash (nullable),
|
|
||||||
created_at, last_seen_at, is_guest flag, demo_turns_used + demo_turns_date.
|
|
||||||
- [x] `user_id` FK on `Adventure`, `Scenario`, `Script`, `Settings`. Story cards/actions/
|
|
||||||
memories inherit scope via their parent (ownership checks resolve the parent).
|
|
||||||
- [x] Settings **per-user** (row per user_id, unique index). API key **encrypted at rest**
|
|
||||||
(Fernet; key derived from `AIDND_SECRET_KEY` or auto-generated `secret.key` next to the
|
|
||||||
DB). Key is write-only through the API (`has_api_key` instead of echoing it).
|
|
||||||
- [x] Migrations 13–23: create "local user" id=1, assign all existing rows to it, unique
|
|
||||||
index on settings.user_id; plus a Python bootstrap step that encrypts any plaintext
|
|
||||||
api_key (`enc:` prefix marks encrypted values).
|
|
||||||
- [x] Demo/starter scenarios: `user_id NULL` + `is_public` — everyone sees them read-only;
|
|
||||||
`seed_demo.py` seeds the Sunken Crypt scenario as public (its scripts are unowned and
|
|
||||||
ship with it; the sample adventure belongs to the local user).
|
|
||||||
|
|
||||||
## Auth & sessions
|
|
||||||
|
|
||||||
- [x] Guest flow: `GET /api/auth/me` with no/invalid cookie → creates guest User + signed
|
|
||||||
long-lived httpOnly cookie (HMAC, `security.py`). Other endpoints 401 without a session;
|
|
||||||
the frontend re-establishes via /me and retries once. No signup wall anywhere.
|
|
||||||
- [x] Register upgrades the guest **in place** (same user_id — data kept). scrypt password
|
|
||||||
hashing (stdlib, no extra dep).
|
|
||||||
- [x] Login switches the session cookie to the account (guest abandoned). Logout clears it.
|
|
||||||
- [x] Every router handler resolves `current_user`; every query filtered by user_id
|
|
||||||
(scenarios/adventures/scripts/story-cards/settings; debug log is local-mode only since
|
|
||||||
it's a global buffer).
|
|
||||||
- [x] Rate limit on register/login: 10 attempts / 5 min per IP (in-memory).
|
|
||||||
- [x] Local/self-hosted mode stays frictionless: auto-created local user, no login UI unless
|
|
||||||
`AIDND_MULTI_USER=1`. Local installs and docker compose behave exactly as before.
|
|
||||||
|
|
||||||
## Shared demo key (BYOK fallback)
|
|
||||||
|
|
||||||
- [x] Env vars: `AIDND_DEMO_API_KEY`, `AIDND_DEMO_ENDPOINT_URL` (default OpenRouter),
|
|
||||||
`AIDND_DEMO_MODELS` (comma whitelist), `AIDND_DEMO_TURNS_PER_DAY` (default 20).
|
|
||||||
Demo only activates in multi-user mode.
|
|
||||||
- [x] No API key configured → demo endpoint/key/whitelisted model; per-user per-day counter;
|
|
||||||
429 with a friendly "add your own key in Settings" message when capped (checked before
|
|
||||||
the turn starts so no orphaned player action).
|
|
||||||
- [x] Memory bank + auto-summarization disabled on demo turns (decided: disable, not count).
|
|
||||||
|
|
||||||
## Frontend
|
|
||||||
|
|
||||||
- [x] Auth UI: Sign up / Log in modal (register default, toggle to login), "Playing as guest —
|
|
||||||
sign up to keep your adventures" nudge in the header, account email + logout when
|
|
||||||
registered. All hidden in local mode (`multi_user:false` from /me).
|
|
||||||
- [x] `api.js`: 401 → GET /auth/me (new guest session) → retry once, for both JSON and SSE.
|
|
||||||
- [x] Settings: demo banner ("Using the shared demo key — N of M free turns left today"),
|
|
||||||
write-only API key field with Remove button, debug log hidden in multi-user mode.
|
|
||||||
- [x] Public scenarios: "demo ✦" badge in the list; read-only editor (fieldset-disabled) with
|
|
||||||
an explainer banner; Play/Export still available.
|
|
||||||
|
|
||||||
## Exit criteria — verified 2026-07-06
|
|
||||||
|
|
||||||
Two sessions (curl cookie jars + Chrome UI): each guest gets an isolated world; register
|
|
||||||
mid-session keeps all data (same user id); logging in from the second session shows the same
|
|
||||||
account data; duplicate email → 409; wrong password → 401; rate limiter kicks in. Demo cap
|
|
||||||
returns 429 at 0 turns left. Migration tested on a copy of the real data.db (rows adopted by
|
|
||||||
local user, api_key Fernet-encrypted and decrypts back to the original). Live OpenRouter turn
|
|
||||||
through the encrypted-key path works in local mode. `vite build` + oxlint clean.
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
# Phase 9 — Production hardening
|
|
||||||
|
|
||||||
**Goal:** make the app safe and stable to expose to strangers on the internet: config via
|
|
||||||
environment, resource limits on everything user-controlled, and a single-service production
|
|
||||||
build.
|
|
||||||
|
|
||||||
## Decisions
|
|
||||||
|
|
||||||
| Question | Answer |
|
|
||||||
|---|---|
|
|
||||||
| Database | **Decide at start of this phase.** SQLite on a persistent disk (zero code change, but Render disks require the ~$7/mo starter tier) vs Postgres (free/cheap managed options, better resume talking point, needs SQLAlchemy URL + migration tweaks). Revisit with current Render pricing. |
|
|
||||||
|
|
||||||
**Ask before implementing:** the database choice above, and target monthly budget (drives
|
|
||||||
Render tier: free tier sleeps after idle + has no persistent disk).
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
- [ ] All config via env vars with sane local defaults: `DATABASE_URL`, `SECRET_KEY`
|
|
||||||
(sessions + API-key encryption), `MULTI_USER`, `CORS_ORIGINS`, demo-key vars (Phase 8),
|
|
||||||
port/host. Document each in `backend/.env.example`.
|
|
||||||
- [ ] Fail fast on missing `SECRET_KEY` when `MULTI_USER=true`.
|
|
||||||
|
|
||||||
## Abuse & resource limits
|
|
||||||
|
|
||||||
- [ ] **quickjs limits**: per-execution time limit and memory limit on the scripting engine
|
|
||||||
(`scripting/engine.py`) — user-submitted JS must not be able to hang or OOM the server.
|
|
||||||
- [ ] Rate limiting on expensive endpoints (turn generation, script run, auth) — per-user and
|
|
||||||
per-IP (e.g. `slowapi`).
|
|
||||||
- [ ] Request size limits (script source length, memory/story-card text lengths, action text).
|
|
||||||
- [ ] Cap per-user row counts (adventures, scenarios, scripts, story cards) with friendly errors.
|
|
||||||
- [ ] Audit debug router (`routers/debug.py`) and `/docs`: admin-only or disabled when
|
|
||||||
`MULTI_USER=true` — debug log may contain other users' prompts.
|
|
||||||
|
|
||||||
## Production serving
|
|
||||||
|
|
||||||
- [ ] Single service: FastAPI serves the built SPA (fallback already exists) — verify the Docker
|
|
||||||
image from Phase 7 is production-ready (no `--reload`, multiple workers or async-safe
|
|
||||||
single worker; check SQLite + multiple workers interaction before choosing).
|
|
||||||
- [ ] CORS locked to the deployed origin (moot if same-origin single service — verify).
|
|
||||||
- [ ] Security headers middleware; cookies `Secure` + `SameSite`.
|
|
||||||
- [ ] Streaming (SSE) works behind Render's proxy — verify no buffering issues.
|
|
||||||
- [ ] Structured logging; scrub API keys from all logs and the debug page.
|
|
||||||
|
|
||||||
## Database (after decision)
|
|
||||||
|
|
||||||
- [ ] If Postgres: swap `DATABASE_URL`, verify JSON-blob columns (embeddings) and
|
|
||||||
`migrations.py` work; test full play loop.
|
|
||||||
- [ ] If SQLite-on-disk: confirm WAL mode + single-worker (or serialized writes) is acceptable.
|
|
||||||
- [ ] Backup story: platform DB backups (Postgres) or a scheduled dump of the disk (SQLite).
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
Running the production Docker image locally with `MULTI_USER=true`: a hostile user cannot hang
|
|
||||||
the server with a `while(true)` script, cannot see another user's data or the debug log, gets
|
|
||||||
rate-limited instead of burning the demo key, and the app streams turns normally the whole time.
|
|
||||||
|
|
||||||
### Verified 2026-07-07 (uvicorn, `MULTI_USER=1`, fresh SQLite DB, curl)
|
|
||||||
|
|
||||||
- **Fail-fast secret:** `import app.main` with `MULTI_USER=1` and no `AIDND_SECRET_KEY` raises
|
|
||||||
the RuntimeError as designed (won't boot).
|
|
||||||
- **`while(true)` script:** `POST /api/scripts/{id}/test` on an `input_js` infinite loop returns
|
|
||||||
`InternalError: interrupted` (engine time limit) — server stays responsive afterward.
|
|
||||||
- **Cross-user isolation:** guest B sees `[]` for scripts, gets 404 on guest A's script id;
|
|
||||||
guest A keeps its own row. No leakage.
|
|
||||||
- **Debug log:** `GET /api/debug/requests` → 403 in multi-user mode.
|
|
||||||
- **Rate limiting:** 12 rapid `POST /api/auth/register` → 429 after the 10th (auth scope, 10/300s).
|
|
||||||
- **Body size:** 3 MB body to `POST /api/scenarios` → 413 (limit 2 MB) via BodySizeLimitMiddleware.
|
|
||||||
- **Security headers:** CSP, `x-frame-options: DENY`, `x-content-type-options: nosniff`,
|
|
||||||
`referrer-policy: same-origin` on every response — including the SSE stream.
|
|
||||||
- **Docs disabled:** Swagger UI and OpenAPI schema not served (`/docs`, `/openapi.json` fall
|
|
||||||
through to the SPA `index.html`; no `swagger-ui`, no API schema exposed).
|
|
||||||
- **SSE streaming:** `POST /api/adventures/{id}/actions` streams `text/event-stream` with
|
|
||||||
`x-accel-buffering: no`, chunked, incremental events — the pure-ASGI middlewares don't buffer.
|
|
||||||
(No LLM key configured here, so it streams the "No model configured" error event; a *live*
|
|
||||||
provider turn through this path was verified end-to-end in Phase 8.)
|
|
||||||
|
|
||||||
All Phase 9 exit criteria met.
|
|
||||||
|
|
||||||
### Postgres path verified 2026-07-07 (real Neon, PostgreSQL 18.4)
|
|
||||||
|
|
||||||
Pointed the backend at a live Neon `DATABASE_URL` (pooled, `sslmode=require&channel_binding=require`):
|
|
||||||
|
|
||||||
- **Driver/URL:** `postgres://`/`postgresql://` normalized to `postgresql+psycopg://`; raw
|
|
||||||
psycopg3 connect succeeds.
|
|
||||||
- **Bootstrap:** fresh DB → `create_all` builds all 11 tables and stamps `schema_version = 23`
|
|
||||||
(= `LATEST_VERSION`); the SQLite-only migrations 2–23 are correctly skipped on a fresh DB.
|
|
||||||
- **Embedding column:** `memories.embedding` is Postgres `json`; a float list round-trips
|
|
||||||
intact (`[0.1, 0.2, 0.3, -0.4]` back as a `list`).
|
|
||||||
- **ORM CRUD:** user → scenario → adventure → memory create/read/delete with FK cascades works.
|
|
||||||
- **HTTP round-trip (multi-user, TestClient):** guest bootstrap → register → me → create
|
|
||||||
scenario → list → settings → delete, all 200/201/204 against Neon.
|
|
||||||
|
|
||||||
Remaining Phase 10 step: the Docker production image specifically (build + run the container).
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
# Phase 10 — Deploy & publish
|
|
||||||
|
|
||||||
**Goal:** the app live on Render at a public URL, linked from resume/website alongside the
|
|
||||||
GitHub repo.
|
|
||||||
|
|
||||||
## Decisions (confirmed)
|
|
||||||
|
|
||||||
| Question | Answer |
|
|
||||||
|---|---|
|
|
||||||
| Platform | **Render** |
|
|
||||||
| Domain | **Platform URL is fine** (e.g. `ai-dnd.onrender.com`); custom domain can be added later anytime |
|
|
||||||
|
|
||||||
**Ask before implementing:** Render tier (free-with-sleep vs ~$7/mo always-on — depends on the
|
|
||||||
Phase 9 database decision), and the exact service name (it becomes the public URL).
|
|
||||||
|
|
||||||
### Decisions (confirmed 2026-07-07)
|
|
||||||
|
|
||||||
| Question | Answer |
|
|
||||||
|---|---|
|
|
||||||
| Database | **Neon Postgres** (external managed; free tier has no persistent disk). Path fully verified against real Neon — see `plan/09-phase-hardening.md`. |
|
|
||||||
| Tier | **Free** (sleeps after ~15 min idle; ~30–60s first-wake). |
|
|
||||||
| Service name | `ai-dnd` (→ `ai-dnd.onrender.com`, adjustable in dashboard). |
|
|
||||||
| Region | `virginia` (us-east, matches Neon us-east-1). |
|
|
||||||
| Local Docker preflight | Skipped by choice — Render builds the same Dockerfile in the cloud; its build logs are the image test. |
|
|
||||||
|
|
||||||
## Deploy
|
|
||||||
|
|
||||||
- [x] `render.yaml` blueprint: web service from the Dockerfile, env vars (SECRET_KEY generated,
|
|
||||||
demo-key vars, `MULTI_USER=true`), `/api/health` health check, external Neon Postgres.
|
|
||||||
README gained a "Deploy (Render)" section.
|
|
||||||
- [ ] Set up the Render service, connect the GitHub repo, auto-deploy on push to `main`.
|
|
||||||
- [ ] Seed production with 2–3 good demo scenarios (public/starter scenarios from Phase 8) so
|
|
||||||
first-time visitors have something great to click immediately.
|
|
||||||
- [ ] Smoke test the live URL: guest play on demo key, register, BYOK flow, scripting, memory
|
|
||||||
bank, Insights — from a device/network that isn't yours.
|
|
||||||
- [ ] Free-tier note: if on free tier, first request after idle takes ~30–60s to wake — add a
|
|
||||||
friendly loading state or accept it (revisit tier if it feels bad).
|
|
||||||
|
|
||||||
## Post-launch guardrails
|
|
||||||
|
|
||||||
- [ ] Watch demo-key spend/usage for the first days (OpenRouter dashboard); confirm caps hold.
|
|
||||||
- [ ] Set up uptime monitoring (free: UptimeRobot or similar) — optional.
|
|
||||||
- [ ] Error visibility: Render logs are enough for v1; note how to pull them.
|
|
||||||
|
|
||||||
## Resume / website
|
|
||||||
|
|
||||||
- [ ] README: add the live-demo link + "Try it" section at the top.
|
|
||||||
- [ ] 2–3 sentence project blurb for resume/website (stack, the interesting hard parts:
|
|
||||||
AI Dungeon-compatible JS scripting sandbox, embedding-based memory bank, prompt
|
|
||||||
transparency, guest-first optional auth).
|
|
||||||
- [ ] Later pass (deferred from Phase 7): screenshots/demo GIF for README and website card.
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
A recruiter clicks one link on your resume, lands on the live app, plays three turns of a demo
|
|
||||||
scenario as a guest without configuring anything, and can find the GitHub repo from the page.
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
# Plan: undo/retry state revert (+ concurrency lock)
|
|
||||||
|
|
||||||
Fixes three linked issues around `script_state` (the shared per-adventure
|
|
||||||
"scoreboard" scripts write to) and undo/retry.
|
|
||||||
|
|
||||||
## Background
|
|
||||||
|
|
||||||
- `script_state` is one shared dict on `Adventure` (`models.py:90`), mutated in
|
|
||||||
exactly ONE place: `pipeline.py:89` (`self.adventure.script_state = state`).
|
|
||||||
- Today, undo (`adventures.py:445`) and retry (`:422`) delete *actions* but never
|
|
||||||
touch `script_state`, so state never rolls back.
|
|
||||||
|
|
||||||
## Issue 1 — retry double-applies state (pre-existing bug)
|
|
||||||
|
|
||||||
Retry deletes the last AI action and regenerates. The output hook already mutated
|
|
||||||
`script_state` on the first attempt; regenerating runs it again, stacking the change
|
|
||||||
(e.g. "add 10 gold" → 20 gold after one retry). Same root cause as undo not
|
|
||||||
reverting.
|
|
||||||
|
|
||||||
## Issue 4 — undo has no concurrency guard
|
|
||||||
|
|
||||||
Turns take `acquire_turn_lock` (`:189`); undo does not, so undo can race a turn
|
|
||||||
that is still streaming.
|
|
||||||
|
|
||||||
## Issue 2 — Memory Bank leftovers (smaller than expected)
|
|
||||||
|
|
||||||
`run_post_turn` already clamps `memory_cursor`/`summary_cursor` down to the current
|
|
||||||
action count (`memorybank.py:178-182`), so there is NO cursor stall. The only
|
|
||||||
remainder: a `Memory` created from a turn that was later undone stays behind, its
|
|
||||||
`source_start/source_end` now pointing past the end of the story.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## The fix
|
|
||||||
|
|
||||||
### 1. Snapshot state per turn (Issue 1 + enables undo revert)
|
|
||||||
|
|
||||||
- Add column `state_before: JSON nullable` to `Action` (`models.py`).
|
|
||||||
- Migration: append `(25, "ALTER TABLE actions ADD COLUMN state_before JSON")`
|
|
||||||
to `migrations.py`. `JSON` is valid on both SQLite and Postgres.
|
|
||||||
- In `run_player_turn` / `generate_turn`, when the FIRST action of a turn is
|
|
||||||
created, stash `copy.deepcopy(adventure.script_state)` onto it — captured
|
|
||||||
*before* any hook runs. (Player action for do/say/story; the AI action for a
|
|
||||||
bare `continue`.)
|
|
||||||
- Fix retry directly: before regenerating, restore
|
|
||||||
`adventure.script_state` from the deleted AI action's `state_before` so the
|
|
||||||
output hook starts from the pre-turn scoreboard instead of the mutated one.
|
|
||||||
|
|
||||||
### 2. Revert on undo (Issue depends on #1)
|
|
||||||
|
|
||||||
- In `undo_turn`, after deleting the popped actions, set
|
|
||||||
`adventure.script_state = <deleted player/first action>.state_before` (fall back
|
|
||||||
to `{}` if null, i.e. pre-migration turns), then commit.
|
|
||||||
- Only wire this into undo + retry — NOT the arbitrary
|
|
||||||
`delete_action` endpoint (`:830`); mid-history state revert is undefined.
|
|
||||||
|
|
||||||
### 3. Lock undo (Issue 4)
|
|
||||||
|
|
||||||
- Wrap `undo_turn` body in `acquire_turn_lock(adventure_id)` /
|
|
||||||
`_active_turns.discard(...)` in a `finally`. It's synchronous (not SSE), so no
|
|
||||||
`with_turn_lock` wrapper needed — just acquire and discard.
|
|
||||||
|
|
||||||
### 4. Clean up dangling memories (Issue 2, optional)
|
|
||||||
|
|
||||||
- In `undo_turn` after deleting actions, delete any `Memory` whose `source_start`
|
|
||||||
is >= the new story-action count (i.e. summarized a turn that no longer exists).
|
|
||||||
Cursors already self-heal, so this is polish, not correctness.
|
|
||||||
|
|
||||||
## Known limits (document, don't fix)
|
|
||||||
|
|
||||||
- Story cards a script created (`_apply_cards`, `pipeline.py:87`) are NOT reverted —
|
|
||||||
only text + script_state roll back.
|
|
||||||
- Pre-migration turns have `state_before = NULL` → undo falls back to `{}`.
|
|
||||||
- Demo turn cap is not refunded on undo (intentional).
|
|
||||||
|
|
||||||
## Test checklist
|
|
||||||
|
|
||||||
- Script that increments a counter: play → undo → counter back to prior value.
|
|
||||||
- Same script: play → retry → counter changes once, not twice.
|
|
||||||
- Undo during an active stream returns 409, doesn't corrupt state.
|
|
||||||
- Undo a turn old enough to have been summarized: no orphaned memory left.
|
|
||||||
@@ -1,269 +0,0 @@
|
|||||||
# Phase 12 — RPG world state (AI-authored, engine-clamped)
|
|
||||||
|
|
||||||
**Goal:** each turn carries a structured **world state** — world stats (e.g. `day`),
|
|
||||||
player stats (`hp`, `mana`), per-NPC stats (`health`, `trust`, …) for the NPCs currently
|
|
||||||
in scene, and **milestones** (story-progress flags / quest objectives). After the player
|
|
||||||
acts, the AI reads the current state, narrates, and **proposes** state changes; the
|
|
||||||
engine **validates and clamps** them against a schema before storing. Stat meanings are
|
|
||||||
described in words (bands) so the model reasons semantically, not arithmetically.
|
|
||||||
|
|
||||||
This is deliberately the "AI owns mechanics, engine enforces limits" design — NOT a
|
|
||||||
deterministic dice engine. The AI proposes; Python is the referee.
|
|
||||||
|
|
||||||
## Design decisions (settled)
|
|
||||||
|
|
||||||
- **AI proposes, engine clamps.** The model never owns the numbers directly. It emits
|
|
||||||
a delta; the engine applies min/max, per-turn caps, and cooldowns server-side. The
|
|
||||||
AI cannot be trusted to obey its own frequency rules — the engine must.
|
|
||||||
- **Band descriptions are the reliability trick.** Stats carry word ranges
|
|
||||||
(`0–20: very weak`, `20–40: hurt`, …). The model reads "he's badly hurt" and adjusts
|
|
||||||
down, instead of doing math it's bad at.
|
|
||||||
- **Dedicated NPCs, each with its own stats.** NPCs are defined in a `npcs` section of
|
|
||||||
the schema, keyed by a stable id (`gwen`). Each has a `name`, `desc`, trigger `keys`,
|
|
||||||
and its **own** `stats` block (a dragon can have `ferocity`, a merchant `prices`) — no
|
|
||||||
forced shared template. On adventure creation each NPC auto-creates a story card (from
|
|
||||||
its name/keys/desc) so lore injection + in-scene detection keep working. Live values
|
|
||||||
live in `world_state["npc"]` keyed by the NPC id; only NPCs **in scene this turn** get
|
|
||||||
their stats injected into context. The AI addresses them as `npc.<id>.<stat>`.
|
|
||||||
- **Schema on the scenario, live values on the adventure.** The scenario is the
|
|
||||||
template (what stats exist, their bands + rules); the adventure holds current values.
|
|
||||||
- **Milestones are sticky story flags.** Predefined objectives the AI marks reached via
|
|
||||||
the same delta channel. Once reached they stay reached (revert only through the undo
|
|
||||||
snapshot). Injected as "Goals" (pending) so the AI drives toward them and "Achieved"
|
|
||||||
so it doesn't re-do them. Emergent/AI-invented milestones are out of scope for v1.
|
|
||||||
- **Flags are two-way booleans.** Separate from milestones: named on/off world/character
|
|
||||||
state (`has_key`, `disguised`, `alarm_raised`) the AI can flip either direction via
|
|
||||||
`"flags.<name>": true|false`. Have an `initial` value and a `desc`; no clamp/cooldown.
|
|
||||||
- **Stat guide (descriptions + band ranges).** A fixed, per-scenario legend injected each
|
|
||||||
turn, describing each stat's `desc` and its full band ladder (`0–20 very weak, …`) —
|
|
||||||
handled independently, so a stat may have a description, a range, both, or neither. This
|
|
||||||
is separate from the live values block (which still shows only the *current* band label),
|
|
||||||
giving the model the whole scale to reason across without bloating the per-turn line.
|
|
||||||
- **One-call turn.** The AI narrates AND appends a fenced state-delta block; the engine
|
|
||||||
parses it and strips it from the visible text. No second LLM call — matters on the
|
|
||||||
rate-limited free-tier demo (20 req/min). Parser is forgiving of messy JSON from
|
|
||||||
weaker free models.
|
|
||||||
- **Memory bank / multi-memory is untouched.** (Confirmed.)
|
|
||||||
- **Separate from `script_state`.** World state gets its own column so it never collides
|
|
||||||
with the scripting scoreboard, and reuses the same undo/retry snapshot pattern
|
|
||||||
(`Action.state_before`, see `plan/11`).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Data model
|
|
||||||
|
|
||||||
### Schema definition — `Scenario.stat_schema` (JSON, nullable)
|
|
||||||
|
|
||||||
Migration `(26, "ALTER TABLE scenarios ADD COLUMN stat_schema JSON")`.
|
|
||||||
|
|
||||||
```jsonc
|
|
||||||
{
|
|
||||||
"world": {
|
|
||||||
"day": { "type": "counter", "min": 1, "initial": 1, "desc": "In-game day",
|
|
||||||
"max_delta_per_turn": 1, "cooldown": 0 }
|
|
||||||
},
|
|
||||||
"player": {
|
|
||||||
"hp": { "min": 0, "max": 100, "initial": 100, "max_delta_per_turn": 30,
|
|
||||||
"cooldown": 0, "bands": [[0,20,"very weak"],[20,40,"hurt"],
|
|
||||||
[40,60,"minor damage"],[60,90,"healthy"],[90,100,"full health"]] },
|
|
||||||
"mana": { "min": 0, "max": 50, "initial": 20, "max_delta_per_turn": 15 }
|
|
||||||
},
|
|
||||||
"npcs": { // each NPC has its OWN stats
|
|
||||||
"gwen": {
|
|
||||||
"name": "Gwen", "keys": "Gwen, ranger, her",
|
|
||||||
"desc": "A loyal ranger and the player's ally.",
|
|
||||||
"stats": {
|
|
||||||
"health": { "min": 0, "max": 100, "initial": 100, "bands": [...] },
|
|
||||||
"trust": { "min": -100, "max": 100, "initial": 20, "max_delta_per_turn": 20,
|
|
||||||
"bands": [[-100,-30,"hostile"],[-30,30,"wary"],[30,100,"ally"]] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"flags": { // two-way on/off booleans
|
|
||||||
"has_key": { "desc": "Player holds the dungeon key", "initial": false },
|
|
||||||
"alarm_raised": { "desc": "The enemy is alerted", "initial": false }
|
|
||||||
},
|
|
||||||
"milestones": { // sticky story-progress flags
|
|
||||||
"rescue_gwen": { "desc": "Rescue Gwen from the bandits" },
|
|
||||||
"reach_capital": { "desc": "Arrive at the capital city" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Per-stat rule fields (all optional, engine enforces):
|
|
||||||
- `min` / `max` — hard clamp.
|
|
||||||
- `initial` — value when first instantiated.
|
|
||||||
- `max_delta_per_turn` — largest absolute change allowed in one turn (extra is clamped).
|
|
||||||
- `cooldown` — minimum player actions between changes to this stat (0 = every turn).
|
|
||||||
- `bands` — `[lo, hi, label]` triples, used only to describe the value to the model.
|
|
||||||
- `type` — `"counter"` (monotonic, e.g. day) vs default numeric; counters reject
|
|
||||||
negative deltas.
|
|
||||||
|
|
||||||
Milestones carry only `desc` (the objective text). They are boolean and sticky — the
|
|
||||||
engine accepts a delta of `true` only, records the action index reached, and ignores
|
|
||||||
attempts to re-set or un-set (undo is the only way back).
|
|
||||||
|
|
||||||
Each NPC in `npcs` carries `name`, `desc`, trigger `keys`, and its own `stats` block
|
|
||||||
(same per-stat fields as above). All defined NPCs are instantiated up front; a story card
|
|
||||||
is auto-created per NPC on adventure creation (skipped if a card with that name already
|
|
||||||
exists) so descriptions inject as lore and in-scene detection works.
|
|
||||||
|
|
||||||
### Live values — `Adventure.world_state` (JSON, default `{}`)
|
|
||||||
|
|
||||||
Migration `(27, "ALTER TABLE adventures ADD COLUMN world_state JSON")`.
|
|
||||||
|
|
||||||
```jsonc
|
|
||||||
{
|
|
||||||
"world": { "day": 3 },
|
|
||||||
"player": { "hp": 55, "mana": 10 },
|
|
||||||
"npc": { "gwen": { "health": 80, "trust": 20 } }, // keyed by NPC id
|
|
||||||
"milestones": { "rescue_gwen": { "reached": true, "at": 7 } },
|
|
||||||
"_meta": { "last_changed": { "player.hp": 7, "npc.gwen.trust": 6 } } // action index
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
`_meta.last_changed` backs the `cooldown` rule. NPC blocks are instantiated up front from
|
|
||||||
each NPC's own `stats`.
|
|
||||||
|
|
||||||
### Undo/retry snapshot — `Action.world_state_before` (JSON, nullable)
|
|
||||||
|
|
||||||
Migration `(28, "ALTER TABLE actions ADD COLUMN world_state_before JSON")`.
|
|
||||||
Snapshotted and reverted exactly like `state_before` (Phase 11) — same call sites.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Turn flow
|
|
||||||
|
|
||||||
```
|
|
||||||
player input → INPUT hook (existing)
|
|
||||||
context build → inject [World State] section (current values + band scale + emit-rule)
|
|
||||||
AI response → narration + trailing ```state { ...delta... } ``` block
|
|
||||||
→ parse delta → validate/clamp against schema → apply → strip block
|
|
||||||
→ snapshot world_state onto the action (undo)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 1. Context injection (`context/builder.py`)
|
|
||||||
|
|
||||||
New always-on section `world_state`, placed with the system sections. Keep it **terse**
|
|
||||||
(competes with story history under the default context budget, raised to 16384 in Phase 12):
|
|
||||||
|
|
||||||
```
|
|
||||||
World state — day 3.
|
|
||||||
You: HP 55/100 (minor damage), Mana 10/50.
|
|
||||||
Gwen: health 80 (healthy), trust 20 (neutral).
|
|
||||||
Goals: Arrive at the capital city.
|
|
||||||
Achieved: Rescued Gwen from the bandits.
|
|
||||||
```
|
|
||||||
|
|
||||||
- Only inject NPC lines for cards **triggered this turn** — reuse `triggered` /
|
|
||||||
`card_records` already computed in `build_context` (`builder.py:120`). No extra work.
|
|
||||||
- Milestones: list unreached ones under `Goals` and reached ones under `Achieved`
|
|
||||||
(omit either line when empty). These are cheap and always included.
|
|
||||||
- Append the value's band label in parentheses so the model reads meaning, not just a
|
|
||||||
number.
|
|
||||||
- Append a compact **emit rule** (once, in the narrator/system text). Instruct the model
|
|
||||||
explicitly to:
|
|
||||||
- end its reply with a fenced `state` block **only when something actually changed**;
|
|
||||||
- **omit the block entirely** when nothing changed this turn (no empty `{}`);
|
|
||||||
- include **only the stats that changed** as deltas — never restate unchanged stats,
|
|
||||||
never send full/absolute values, e.g. `{"player.hp": -15, "npc.gwen.trust": +5}`.
|
|
||||||
This keeps the emitted block tiny (saves output tokens on the free tier) and means the
|
|
||||||
engine's clamp/cooldown logic only ever sees real changes.
|
|
||||||
|
|
||||||
### 2. Delta parse + validate (new `worldstate/` module)
|
|
||||||
|
|
||||||
New module `backend/app/worldstate/engine.py` (mirrors `scripting/` layout):
|
|
||||||
|
|
||||||
- `extract_delta(text) -> (clean_text, delta_dict)` — pull the trailing ```` ```state ````
|
|
||||||
block, tolerate missing/extra fences, trailing commas, `+N` numbers; return `{}` on
|
|
||||||
parse failure (never break the turn — same philosophy as a broken script).
|
|
||||||
- `apply_delta(adventure, delta, action_index) -> report` — for each `path: change`:
|
|
||||||
1. resolve `path` (`player.hp`, `world.day`, `npc.<npcId>.trust`,
|
|
||||||
`milestones.<id>`) against the schema; unknown paths ignored (logged).
|
|
||||||
2. **milestone path** → accept only `true`, set `{reached: true, at: action_index}`,
|
|
||||||
ignore if already reached; skip the numeric steps below.
|
|
||||||
3. lazily instantiate NPC stat block from template if missing.
|
|
||||||
4. reject if `cooldown` not elapsed (`action_index - _meta.last_changed[path] < cooldown`).
|
|
||||||
5. clamp change to `max_delta_per_turn`; counters reject negative.
|
|
||||||
6. apply, then clamp result to `[min, max]`.
|
|
||||||
7. record `_meta.last_changed[path] = action_index`.
|
|
||||||
- Returns a report (applied / clamped / rejected) for the Insights panel, like the
|
|
||||||
script report.
|
|
||||||
|
|
||||||
### 3. Wire into `generate_turn` (`routers/adventures.py:249`)
|
|
||||||
|
|
||||||
- Snapshot: `world_state_before = snapshot_world_state(adventure)` alongside the existing
|
|
||||||
`state_before` (`:271`).
|
|
||||||
- After the `output` hook and empty-text check (`:340`): `clean, delta =
|
|
||||||
extract_delta(text)`, `report = apply_delta(...)`, store `text = clean`, stash the
|
|
||||||
report into `snapshot["world_state"]`.
|
|
||||||
- Persist `world_state_before` onto the AI `Action` (`:341` block) and commit
|
|
||||||
`adventure.world_state`.
|
|
||||||
- Do this only when `scenario.stat_schema` is non-empty — zero overhead for plain
|
|
||||||
narrative adventures.
|
|
||||||
|
|
||||||
### 4. Undo / retry (`routers/adventures.py`)
|
|
||||||
|
|
||||||
Reuse the Phase 11 wiring verbatim, in parallel:
|
|
||||||
- retry (`:449`): also restore `adventure.world_state` from the deleted AI action's
|
|
||||||
`world_state_before`.
|
|
||||||
- undo (`:489`): also restore from the first removed action's `world_state_before`
|
|
||||||
(fall back to `{}`).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## UI
|
|
||||||
|
|
||||||
- **World State panel** (play view): render current `world_state` as a readable sheet —
|
|
||||||
world / player / per-NPC, with band label and a bar for `min..max` stats, plus a
|
|
||||||
**milestones checklist** (reached vs pending). Reuse the collapsible-tree styling from
|
|
||||||
the existing Play State drawer.
|
|
||||||
- **Insights**: show the parsed delta + apply/clamp/reject report per turn (next to the
|
|
||||||
script report already there).
|
|
||||||
- **Scenario editor**: a `stat_schema` editor. **v1 is a raw JSON editor** (CodeMirror,
|
|
||||||
reusing the script-slot editor setup) — settled, no form builder for now. A form-based
|
|
||||||
stat builder is a possible later nicety.
|
|
||||||
- Graceful when `stat_schema` is empty: panel + editor hidden, app behaves exactly as
|
|
||||||
today.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Seed / demo
|
|
||||||
|
|
||||||
- New seeded demo scenario `seed_data/*.json` with a small `stat_schema` (player hp/mana,
|
|
||||||
`day`, one or two NPC story cards with health/trust) so the feature is visible on the
|
|
||||||
live demo without the player configuring anything. Keep it `:free`-model friendly.
|
|
||||||
- Extend `seed.py` idempotently (matches existing seeder contract).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Exit criteria
|
|
||||||
|
|
||||||
Play the demo RPG scenario: the World State panel shows hp/mana/day, an NPC's trust, and
|
|
||||||
a milestones checklist; taking a fight action drops hp and the narration matches the new
|
|
||||||
band; a friendly action raises an NPC's trust; completing an objective marks its
|
|
||||||
milestone reached (and it stays reached); a change larger than `max_delta_per_turn` is
|
|
||||||
clamped; undo rolls every stat and milestone back to the prior turn; a plain (no-schema)
|
|
||||||
adventure is completely unaffected.
|
|
||||||
|
|
||||||
## Known limits (document, don't fix in v1)
|
|
||||||
|
|
||||||
- The AI can still narrate against the numbers occasionally; injected state + firm emit
|
|
||||||
rule reduces but won't eliminate it. No post-narration consistency check in v1.
|
|
||||||
- No dice / skill checks / combat resolution — this phase is stat tracking only. A
|
|
||||||
deterministic resolver is a possible Phase 13.
|
|
||||||
- NPC stats are keyed by the schema NPC id; editing a scenario's `npcs` between play
|
|
||||||
sessions can orphan a live `npc` entry (harmless, ignored on read).
|
|
||||||
- Cooldown/`max_delta_per_turn` are per-turn heuristics, not a full rules engine.
|
|
||||||
|
|
||||||
## Test checklist
|
|
||||||
|
|
||||||
- Schema with `max_delta_per_turn: 30`: a delta of `-50` applies as `-30`, clamps at `min`.
|
|
||||||
- `cooldown: 2` on a stat: two consecutive changes → second is rejected until 2 actions pass.
|
|
||||||
- Counter (`day`): a negative delta is rejected; `+1` advances.
|
|
||||||
- Milestone: `true` marks it reached with `at`; a second set is a no-op; `false` ignored.
|
|
||||||
- Each NPC instantiates its own stats at `initial`; `npc.<id>.<stat>` resolves per-NPC.
|
|
||||||
- Malformed / missing `state` block → turn still completes, delta `{}`, no crash.
|
|
||||||
- A turn where nothing changes emits no `state` block (and an empty `{}` is a no-op).
|
|
||||||
- Undo after a stat change restores the prior value; retry doesn't double-apply.
|
|
||||||
- Empty `stat_schema`: no World State section injected, no `world_state` writes.
|
|
||||||
@@ -1,317 +0,0 @@
|
|||||||
# 13 — Memory-bank embedding cost (round three of the egress work)
|
|
||||||
|
|
||||||
**Goal:** stop every turn fetching the entire memory bank's embeddings. Measured at
|
|
||||||
**3,024 KB per turn** on adventure 25 against **129 KB** for everything else a turn
|
|
||||||
reads — the memory bank is ~96% of a turn's database traffic, and it is fetched fresh
|
|
||||||
every single turn to pick `memory_top_k = 5` memories.
|
|
||||||
|
|
||||||
Found 2026-08-16 while designing the story tree (see `14-phase-story-tree.md`), because
|
|
||||||
memory retrieval is the one read a tree **cannot** window — it is long-range recall by
|
|
||||||
design, so it always spans the full path. That makes this the cost floor of a turn under
|
|
||||||
the tree, which is why it lands first.
|
|
||||||
|
|
||||||
## The measurement (production, Neon SQL editor)
|
|
||||||
|
|
||||||
```sql
|
|
||||||
SELECT count(*), avg(json_array_length(embedding))::int,
|
|
||||||
avg(length(embedding::text))::int,
|
|
||||||
pg_size_pretty(sum(length(embedding::text))::bigint)
|
|
||||||
FROM memories WHERE embedding IS NOT NULL;
|
|
||||||
-- 134 memories | 1536 dims | 30,971 bytes each | 4,053 kB total
|
|
||||||
```
|
|
||||||
|
|
||||||
| adventure | active | fetched | per turn |
|
|
||||||
|---|---|---|---|
|
|
||||||
| 25 | 100 | 100 | **3,024 kB** |
|
|
||||||
| 21 | 18 | 18 | 545 kB |
|
|
||||||
| 12 | 12 | 12 | 363 kB |
|
|
||||||
| 20 | 4 | 4 | 121 kB |
|
|
||||||
|
|
||||||
Break-even against the rest of a turn is **4.2 memories**, i.e. about action 25. Every
|
|
||||||
adventure past that is dominated by this.
|
|
||||||
|
|
||||||
`active == fetched` everywhere: nothing has been evicted yet, so the Python-side
|
|
||||||
`forgotten` filter currently costs nothing. It becomes a real leak the moment eviction
|
|
||||||
starts.
|
|
||||||
|
|
||||||
## Why round two missed it
|
|
||||||
|
|
||||||
`retrieve_memories` needs `settings.embedding_model`, and embedding providers are
|
|
||||||
**BYOK-only by construction** — they never touch the demo key. So the public demo never
|
|
||||||
embeds anything, and the round-two stress harness (which had no embedding model
|
|
||||||
configured) measured the turn loop with its heaviest read switched off. The 23.0 MB
|
|
||||||
figure for a 200-action playthrough is the memory-bank-**off** number; with it on,
|
|
||||||
adventure 25 is closer to 300 MB.
|
|
||||||
|
|
||||||
**Rule going forward: any egress measurement must run with an embedding model set.**
|
|
||||||
|
|
||||||
## Root cause
|
|
||||||
|
|
||||||
`memorybank.py:208`
|
|
||||||
|
|
||||||
```python
|
|
||||||
candidates = [m for m in adventure.memories if not m.forgotten and m.embedding]
|
|
||||||
```
|
|
||||||
|
|
||||||
Walks the relationship, so every memory row for the adventure loads with its embedding.
|
|
||||||
`embedding` is `Mapped[list]` on a `JSON` column — 1536 floats serialised as text is
|
|
||||||
~31 KB. Cosine ranking happens in Python (a deliberate choice, documented at
|
|
||||||
`models.py:144`), so all of it must cross the wire. The comment sized it by **count**
|
|
||||||
("fine at a few hundred") rather than by **bytes**.
|
|
||||||
|
|
||||||
Not the same bug as migration 36/37 — the column is not a repeating group and there is
|
|
||||||
no denormalisation to fix. It is a *format* problem plus a *fetch-frequency* problem.
|
|
||||||
|
|
||||||
## Decisions (settled 2026-08-16)
|
|
||||||
|
|
||||||
- **Packed float32, keep 1536 dimensions.** 31 KB → 6 KB, a straight 5x, with **zero
|
|
||||||
retrieval-quality risk**. Explicitly rejected dropping to 512/768 dims: the size fix
|
|
||||||
plus the cache makes the extra 3x unnecessary, and it would have meant re-embedding.
|
|
||||||
- **No re-embedding.** Dimensions are unchanged, so the migration is a pure format
|
|
||||||
conversion of the 134 existing rows — read the JSON, write packed bytes, no API calls.
|
|
||||||
One-time 4 MB read.
|
|
||||||
- **In-process cache alongside the size fix**, not sequenced after it. Turns for one
|
|
||||||
adventure arrive back-to-back, so a dict keyed by adventure id takes steady-state cost
|
|
||||||
to ~0. 100 vectors as float32 is 600 KB of RAM — negligible.
|
|
||||||
- **`memory_bank_capacity` 200 → ~80.** Taken on *quality* grounds as much as cost:
|
|
||||||
ranking 200 memories to pick 5 dilutes retrieval. Note this will start evicting on
|
|
||||||
adventure 25 immediately (it sits at 100).
|
|
||||||
- **Not pgvector.** It is the structural answer and would keep vectors in the database
|
|
||||||
entirely, but it breaks SQLite dev parity — which the codebase protects deliberately
|
|
||||||
(`context/history.py:42`, the `replace()`/`trim()` dialect dance). Revisit only if the
|
|
||||||
bank grows past what Python cosine can handle.
|
|
||||||
|
|
||||||
## Work, in order
|
|
||||||
|
|
||||||
1. **Rebuild the byte-meter harness — in the repo this time.** `backend/tools/dbmeter.py`
|
|
||||||
+ `stress_session.py`. The originals lived outside the repo and are gone. **Default it
|
|
||||||
to running with an embedding model configured**, since that omission is precisely what
|
|
||||||
hid this finding. Do this first so every item below is measured, not assumed.
|
|
||||||
**Done 2026-08-16** — see the baseline below.
|
|
||||||
2. **Migration 38 — `memories.embedding_blob` (`LargeBinary`).** **Done.** Backfill in Python
|
|
||||||
(`struct.pack(f"<{n}f", *vec)`); the conversion cannot be expressed in portable SQL, so
|
|
||||||
unlike migration 36/37 this one does pay a one-time 4 MB read. Drop the old JSON column
|
|
||||||
in a follow-up migration once verified, not in the same one.
|
|
||||||
3. **Read path.** **Done.** `retrieve_memories` queries `memories` directly with
|
|
||||||
`forgotten = false AND embedding_blob IS NOT NULL` in **SQL**, not Python. Unpack with
|
|
||||||
`struct`/`numpy`. Same for `_evict_over_capacity` and `_embed_pending`, which walk the
|
|
||||||
same relationship for a count and for the unembedded rows (see the baseline above).
|
|
||||||
4. **Vector cache.** **Done.** Keyed by `adventure_id`, bounded to 8 adventures. It
|
|
||||||
turned out to need no invalidation callbacks at all — see below.
|
|
||||||
5. **Capacity default 200 → 80.** **Done** (migration 41, only rows still on the old
|
|
||||||
default). Eviction checked at scale first: trimming a 100-memory bank to 80 costs
|
|
||||||
0.8 kB and eight statements, and reads no vectors at all.
|
|
||||||
6. **Infinite scroll upward in `Play.jsx`** for the remaining 423 KB page load of a
|
|
||||||
finished adventure — the last open item from round two. Load the newest turns, fetch
|
|
||||||
older ones as the reader scrolls up.
|
|
||||||
|
|
||||||
## Baseline from the harness (2026-08-16)
|
|
||||||
|
|
||||||
`python -m tools.stress_session`, 200 actions × 4 KB, 100 memories × 1536 dims:
|
|
||||||
|
|
||||||
| shape | fetched | memories' share |
|
|
||||||
|---|---|---|
|
|
||||||
| `GET /adventures` (index) | 0.7 kB | — |
|
|
||||||
| `GET /adventures/{id}` (page load) | 426.7 kB | — |
|
|
||||||
| `POST /adventures/{id}/actions` (one turn) | **3,258.7 kB** | 96% |
|
|
||||||
| `GET /adventures/{id}/context` (Insights) | 3,223.7 kB | 97% |
|
|
||||||
| `run_post_turn` (background) | **3,139.1 kB** | 100% |
|
|
||||||
|
|
||||||
It reproduces both production figures independently — 426.7 kB against the measured
|
|
||||||
423 KB page load, 3,258.7 kB against 3,024 + 129 kB for a turn, and 31.4 KB per
|
|
||||||
embedding against 31.0 KB. `--no-embeddings` reports 112.0 kB for the same turn, so
|
|
||||||
the round-two blind spot is now a **29x** gap anyone can see in one flag.
|
|
||||||
|
|
||||||
**Two findings the SQL measurement could not have shown**, both the same root cause
|
|
||||||
in a different caller:
|
|
||||||
|
|
||||||
- **`run_post_turn` fetches the whole bank again**, every turn. `_evict_over_capacity`
|
|
||||||
walks `adventure.memories` to count the active ones, and `_embed_pending` walks it to
|
|
||||||
find the unembedded ones. So a played turn actually costs ~6.4 MB, not 3.2 — the
|
|
||||||
original estimate was half the real number.
|
|
||||||
- **Insights pays it a third time**, on a page the player can open repeatedly without
|
|
||||||
spending a turn.
|
|
||||||
|
|
||||||
So step 3 below is not just `retrieve_memories`: **every walk of
|
|
||||||
`adventure.memories` has to go**. `_evict_over_capacity` wants a count and an ordering,
|
|
||||||
`_embed_pending` wants rows where `embedding IS NULL` — neither needs a single vector,
|
|
||||||
and both are pure SQL.
|
|
||||||
|
|
||||||
## After (2026-08-16, same harness, same fixture)
|
|
||||||
|
|
||||||
| shape | before | after (cold) | after (warm) |
|
|
||||||
|---|---|---|---|
|
|
||||||
| `POST .../actions` (one turn) | 3,258.7 kB | 723.4 kB | **122.3 kB** |
|
|
||||||
| `run_post_turn` | 3,139.1 kB | 0.7 kB | 0.7 kB |
|
|
||||||
| `GET .../context` (Insights) | 3,223.7 kB | 117.9 kB | 117.9 kB |
|
|
||||||
| `GET .../memories` (drawer) | ~3,138 kB | 23.6 kB | 23.6 kB |
|
|
||||||
|
|
||||||
A played turn is turn + post_turn: **6,398 kB → 123 kB** once warm, a 52x cut. The
|
|
||||||
targets above were ~700 kB cold and ~130 kB warm, so both were met.
|
|
||||||
|
|
||||||
Steps 2–5 landed together, because they are one deployable unit: the columns are no
|
|
||||||
use unless something reads them, and deferring them breaks the old readers. Two
|
|
||||||
additions the plan did not anticipate:
|
|
||||||
|
|
||||||
- **`memories.embedded`, a boolean beside the blob** (migration 39/40). Once the
|
|
||||||
vectors are deferred, every "does this have an embedding?" check becomes a lazy
|
|
||||||
load — an N+1 of 6 KB reads down the Memories drawer. Same shape as
|
|
||||||
`actions.variant_count` beside `actions.variants`, and the same reason.
|
|
||||||
- **The cache needs no invalidation callbacks.** Vectors only ever change through
|
|
||||||
`set_vector`, which drops the one entry; everything that *removes* a memory from
|
|
||||||
play leaves the catalogue query, and entries missing from the catalogue are dropped
|
|
||||||
on the next read. So eviction, deletion and pruning need no hooks and cannot be
|
|
||||||
forgotten. Vectors are held as `array("f")` — 6 KB each, matching the column;
|
|
||||||
a list of Python floats would have been eight times the plan's RAM estimate.
|
|
||||||
|
|
||||||
**Step 6 landed on 2026-08-17**, along with everything else this plan left open — see
|
|
||||||
"Closing the plan" at the end. The page load is a window of 60 actions now: 62.6 kB on
|
|
||||||
a 600-action fixture, down from 606.0 kB, and no longer a function of the story's
|
|
||||||
length.
|
|
||||||
|
|
||||||
## Guardrails to add with this work
|
|
||||||
|
|
||||||
- **Query-count / byte assertions per endpoint**, extending the `test_egress.py` idea:
|
|
||||||
assert an endpoint issues at most N queries and fetches under X KB against
|
|
||||||
production-sized fixtures. This class of bug is invisible at ten rows.
|
|
||||||
- **Explicit column projections on read paths.** List endpoints name the fields they
|
|
||||||
need rather than loading whole entities, so the next heavy column is opt-**in**. This
|
|
||||||
is the structural version of what `deferred=True` does by hand.
|
|
||||||
- **Row-width review rule.** Any new large column justifies itself or goes out-of-line.
|
|
||||||
`actions` now carries five JSON columns.
|
|
||||||
|
|
||||||
Deliberately **not** taken: moving `context_snapshot` out of the database. It costs
|
|
||||||
nothing on reads now that it is deferred, and storage is ~$0.02/mo. Revisit only if
|
|
||||||
backups or storage start to hurt.
|
|
||||||
|
|
||||||
> **Revisit it.** That call weighed egress and got egress right, but it never weighed
|
|
||||||
> the free tier's *storage* ceiling — see "Storage, which this plan did not cost" below.
|
|
||||||
|
|
||||||
## Verification
|
|
||||||
|
|
||||||
- Harness: `python -m tools.stress_session`, memory bank **on**, before and after,
|
|
||||||
against the baseline table above. Target for the turn shape is 3,258 kB → ~700 kB
|
|
||||||
cold, ~130 kB warm (the cache leaves only what a turn reads besides the bank).
|
|
||||||
`run_post_turn` should fall to roughly nothing: neither of its two walks needs a
|
|
||||||
vector at all.
|
|
||||||
- Re-run the round-two shapes with an embedding model configured, so the 200-action
|
|
||||||
playthrough number is finally honest.
|
|
||||||
- The existing `test_egress.py` guard must still pass — nothing here should touch the
|
|
||||||
deferred action columns.
|
|
||||||
|
|
||||||
## Verified on production, 2026-08-17
|
|
||||||
|
|
||||||
Two things were still taken on trust when this shipped: every measurement had run on
|
|
||||||
SQLite, and every number came from a synthetic fixture. Both are now checked.
|
|
||||||
|
|
||||||
### The migration landed on real Postgres
|
|
||||||
|
|
||||||
`schema_version` reads **41**, matching the repo's `LATEST_VERSION`. The live schema has
|
|
||||||
`embedding_blob bytea` and `embedded boolean`, so the `{dialect: sql}` map in migration
|
|
||||||
38 spells BYTEA correctly against a real server — the one thing tests could not prove,
|
|
||||||
since `test_migration_38_is_spelled_for_both_dialects` only inspects the SQL string.
|
|
||||||
The backfill is complete: 134 memories, `embedded = 134`, `embedding_blob = 134`, no
|
|
||||||
stragglers and no rows skipped as malformed.
|
|
||||||
|
|
||||||
### The 5x is real, on real vectors
|
|
||||||
|
|
||||||
| | bytes | per memory |
|
|
||||||
|---|---|---|
|
|
||||||
| `embedding` (JSON) | 4,150,121 | 30,971 |
|
|
||||||
| `embedding_blob` (float32) | 823,296 | 6,144 |
|
|
||||||
|
|
||||||
**5.04x**, against the plan's predicted ~31 KB → 6,144 B. The largest real bank is 100
|
|
||||||
memories = 614,400 B of vectors, so the old code fetched **~3.10 MB per retrieval** on
|
|
||||||
that adventure — which is where the 3,153 kB measured on production came from. That
|
|
||||||
figure is now fully accounted for.
|
|
||||||
|
|
||||||
### SQLite and Postgres agree
|
|
||||||
|
|
||||||
`tools.stress_session` gained an `AIDND_STRESS_DATABASE_URL` escape hatch and was run
|
|
||||||
against a throwaway Neon database at the default fixture (200 actions, 100 memories):
|
|
||||||
|
|
||||||
| shape | SQLite | Postgres |
|
|
||||||
|---|---|---|
|
|
||||||
| index | 4.1 kB | 4.1 kB |
|
|
||||||
| page load | 426.7 kB | 425.0 kB |
|
|
||||||
| one turn, cold | 723.4 kB | 722.3 kB |
|
|
||||||
| one turn, warm | 122.3 kB | **121.1 kB** |
|
|
||||||
| Insights | 117.9 kB | 116.7 kB |
|
|
||||||
| Memories drawer | 23.7 kB | 21.7 kB |
|
|
||||||
| `run_post_turn` | 0.7 kB | 0.6 kB |
|
|
||||||
|
|
||||||
Within 0.5% everywhere. The dialect caveat in the harness docstring is real but small:
|
|
||||||
what dominates is which columns get asked for, and the ORM decides that identically.
|
|
||||||
The warm turn spends **1.7 kB on `memories`, 1% of the read** — the cache behaves on
|
|
||||||
psycopg exactly as it does on SQLite.
|
|
||||||
|
|
||||||
### The page load is worse than modelled, for a different reason
|
|
||||||
|
|
||||||
The synthetic fixture is **~2x heavier per action than production**: 994 B/action real
|
|
||||||
against ~2,133 B/action synthetic, so a real 200-action adventure is ~194 kB, not 427.
|
|
||||||
But the largest real adventure is **607 actions**, not 200, and costs **589.5 kB** in
|
|
||||||
one response. Step 6 is more urgent than this plan assumed, and for the opposite
|
|
||||||
reason to the one modelled — stories get *longer* than the fixture, not heavier.
|
|
||||||
|
|
||||||
Worth fixing the fixture's narration size when step 6 lands, so the harness stops
|
|
||||||
flattering the per-action figure while understating the length.
|
|
||||||
|
|
||||||
### Storage, which this plan did not cost
|
|
||||||
|
|
||||||
`context_snapshot` is **150.8 MB of uncompressed JSON across 944 actions** — ~163 kB a
|
|
||||||
row on average, and ~232 kB a row in the largest adventure, against the ~74 KB/row the
|
|
||||||
comment in `models.py` claims. TOAST compresses it to ~89 MB on disk, but
|
|
||||||
`octet_length` is what would cross the wire, because Postgres decompresses before
|
|
||||||
sending. Deferral is the only thing standing between a bulk read and a 137 MB query.
|
|
||||||
|
|
||||||
The database is **99.6 MB total**, of which `actions` is **88.9 MB**. Neon's free tier
|
|
||||||
is 512 MB. At ~94 kB of disk per action that ceiling arrives at roughly **5,400
|
|
||||||
actions**, and 944 are already stored. So the "~$0.02/mo, leave it in the database"
|
|
||||||
call above is wrong for the tier this actually runs on — not because reads cost
|
|
||||||
anything, but because the free tier meters *storage*, and that is the constraint with
|
|
||||||
a cliff. Dropping the dead `memories.embedding` column reclaims 4.05 MB (4%), which
|
|
||||||
helps and does not solve it.
|
|
||||||
|
|
||||||
None of the numbers above required reading a single row of anyone's content: counts,
|
|
||||||
`octet_length` sums and catalog sizes only.
|
|
||||||
|
|
||||||
## Closing the plan, 2026-08-17
|
|
||||||
|
|
||||||
Everything above landed the same day the verification did.
|
|
||||||
|
|
||||||
| | before | after |
|
|
||||||
|---|---|---|
|
|
||||||
| page load, 600 actions | 606.0 kB | **62.6 kB**, and flat in story length |
|
|
||||||
| adventures index, 6 adventures | 469.7 kB | **0.3 kB** |
|
|
||||||
| `context_snapshot` stored | ~89 MB | ~43 MB (after a VACUUM) |
|
|
||||||
| a played turn | 6.4 MB (2026-08-16) | 123 kB |
|
|
||||||
|
|
||||||
**Step 6, the window.** `GET /adventures/{id}` returns the newest `ACTION_PAGE`
|
|
||||||
actions and the story's length; `GET /{id}/actions?before_id=` walks back from there.
|
|
||||||
Anchored on an action rather than an offset — the offset version breaks precisely when
|
|
||||||
a turn lands mid-scroll, handing the reader one action twice and hiding another — and
|
|
||||||
that choice is also what makes it survive the story tree, since it compares indices to
|
|
||||||
order a branch rather than treating them as positions.
|
|
||||||
|
|
||||||
**Byte assertions.** `tests/test_egress.py` now carries per-action budgets as well as
|
|
||||||
column guards, plus one test whose only job is to fail if the fixture ever gets too
|
|
||||||
small for the budgets to catch anything.
|
|
||||||
|
|
||||||
**Column projections.** `ACTION_LIST_COLUMNS` and `MEMORY_LIST_COLUMNS` name what a
|
|
||||||
list response renders, and the adventures index selects four columns instead of the
|
|
||||||
entity. That one was not just future-proofing: an Adventure carries seven text and JSON
|
|
||||||
columns the index never shows.
|
|
||||||
|
|
||||||
**The JSON vector column is gone**, and dropping it exposed a live bug — changing your
|
|
||||||
embedding model had stopped re-embedding the bank the day migration 38 shipped. See
|
|
||||||
`tests/test_embedding_model_switch.py`.
|
|
||||||
|
|
||||||
**Storage.** `context_snapshot` is zlib-compressed through a TypeDecorator
|
|
||||||
(`app/compression.py`, migrations 43–45), so the call sites never learned about it.
|
|
||||||
3.5x on real Postgres. **This does not shrink anything until `VACUUM FULL actions`
|
|
||||||
runs** — Postgres marks dropped columns rather than reclaiming them, and the backfill
|
|
||||||
leaves a dead tuple per row.
|
|
||||||
|
|
||||||
Not done: nothing exercises the scroll behaviour in a browser. The frontend has no test
|
|
||||||
runner, and prepend-and-restore-scroll is the part most likely to feel wrong even when
|
|
||||||
it is correct.
|
|
||||||
@@ -1,914 +0,0 @@
|
|||||||
# Phase 14 — Story tree (branching adventures)
|
|
||||||
|
|
||||||
**Goal:** replace the linear action list with a **tree**. A retry becomes a sibling
|
|
||||||
rather than a rewrite; continuing from one makes it a branch. Players can go back to any
|
|
||||||
turn, take a different path, and keep both — switching between them freely.
|
|
||||||
|
|
||||||
Depends on `13-memory-embedding-cost.md` shipping first: memory retrieval is the one read
|
|
||||||
a tree cannot window, so it is the cost floor of a turn under this design. Fix the floor
|
|
||||||
before building on it.
|
|
||||||
|
|
||||||
## Why (beyond the feature)
|
|
||||||
|
|
||||||
Seven bug classes stop existing, and every one traces to the same root — **the story is a
|
|
||||||
mutable list**:
|
|
||||||
|
|
||||||
| Bug | Why it goes away |
|
|
||||||
|---|---|
|
|
||||||
| Deleting a middle action skips a later one forever (`note_action_removed`) | Cursors become node ids, not positions in a shifting list |
|
|
||||||
| `prune_dangling_memories` orphaning actions behind the cursor | Nothing is ever removed |
|
|
||||||
| Editing a summarised action leaves its memory stale forever — **currently unfixed** | Editing makes a new node; the old memory stays correct for the old path |
|
|
||||||
| The one-turn memory holdback (`settled_story_actions`) | Nothing is mutated, so nothing goes stale — the concept is unnecessary |
|
|
||||||
| The legacy-cursor no-rewind trap found while fixing that | Same |
|
|
||||||
| "Anything reading `adventure.actions` during generation must exclude the retried action" — leaked into 4 call sites | The replaced turn is not on your path; it cannot leak |
|
|
||||||
| `variant_count` / mirrored `text` drifting from `variants` | The denormalisation disappears entirely |
|
|
||||||
|
|
||||||
It also resolves the 1NF violation: `variants` as a JSON repeating group becomes rows.
|
|
||||||
|
|
||||||
**Scored, 2026-08-18, with SP1–SP5 shipped.** Six of the seven are gone as described.
|
|
||||||
The seventh — the one-turn holdback — is gone too, but the reasoning above was wrong
|
|
||||||
about *why* it could go: siblings share a coordinate, so replacing what a turn says still
|
|
||||||
invalidates the memory covering it. What made it deletable is that the repair already
|
|
||||||
existed for undo and delete; see the trap note below. Two rows want a footnote:
|
|
||||||
|
|
||||||
- **Editing a summarised action** is still unfixed. The table says editing makes a new
|
|
||||||
node; nothing in SP1–SP5 makes it do that, and no subphase is scheduled to. `PATCH
|
|
||||||
/actions/{id}` still writes over the text in place, and the memory covering it goes
|
|
||||||
stale exactly as before. What *has* changed is that the machinery to fix it now exists
|
|
||||||
— an edit could write a sibling and switch to it, which is a retry the player typed —
|
|
||||||
so it is a small change whenever it is wanted.
|
|
||||||
- **The 1NF violation** is resolved. Nothing writes a `variants` array any more, in the
|
|
||||||
database or out of it — SP6 replaced the bundle that was its last producer, and the
|
|
||||||
array survives only in the v1 *reader*, which exists so files already saved still
|
|
||||||
import.
|
|
||||||
|
|
||||||
## Design decisions (settled 2026-08-16)
|
|
||||||
|
|
||||||
- **Full branching, with UI.** Not the "tree schema, no branch picker" middle option —
|
|
||||||
branching ships as a feature people use.
|
|
||||||
- **`branch_id` + `depth` on every node, not parent pointers alone.** Parent pointers
|
|
||||||
alone mean walking N links to read a story, which throws away the round-two windowing
|
|
||||||
work. `depth` replaces `index` as the ordering key.
|
|
||||||
- **A `branches` table with `parent_branch_id` and `fork_depth`.** The fork point is
|
|
||||||
**stored at fork time, never inferred**. Nothing is copied on a fork — a branch borrows
|
|
||||||
its ancestors' turns.
|
|
||||||
- **Lineage cached on the branch row.** `lineage = [(D,∞), (C,45), (B,30), (A,10)]`,
|
|
||||||
computed once at fork (parent's lineage + one entry). Reads never walk to reconstruct
|
|
||||||
it. Each ancestor is capped at the `fork_depth` of the branch beneath it.
|
|
||||||
- **Read the lineage lazily, windowed.** Query the newest few lineage entries, measure,
|
|
||||||
fetch more only if the context budget is not covered — the exact shape of
|
|
||||||
`history.window_covering()`. **Clause count is bounded by the context window, not by
|
|
||||||
fork count**, so a 200-fork story reads as cheaply as a 2-fork one.
|
|
||||||
- **Promote to a branch only on continue.** Attempts at the tip stay as sibling leaves;
|
|
||||||
one becomes a branch the moment a turn is played past it. Keeps the lineage chain to
|
|
||||||
"divergences I built a story on", not "every retry ever" — the difference between a
|
|
||||||
handful of entries and fifty.
|
|
||||||
- **Memories and the summary attach to the node that produced them**, found by walking
|
|
||||||
up. Shared ancestors are shared automatically, so a fork costs nothing and **nothing
|
|
||||||
needs recreating**. A memory covering depths 37–42 hangs off that branch's node 42 and
|
|
||||||
is invisible to any path not through it. Generalise the rule: *anything derived attaches
|
|
||||||
to the node that produced it* — the summary included, so stop storing it per turn.
|
|
||||||
- **Full lineage for memories, windowed lineage for the story.** Memory retrieval is
|
|
||||||
long-range recall and cannot be windowed, but memories are sparse (~1 per 6 actions), so
|
|
||||||
a long OR-clause returning ~33 small rows is fine. Two queries, one lineage.
|
|
||||||
- **Never auto-prune.** Nothing is deleted without an explicit user action. **This makes
|
|
||||||
a branch-management UI a hard dependency, not a nice-to-have** — storage grows without
|
|
||||||
limit otherwise.
|
|
||||||
- **Story cards stay adventure-wide.** A card invented on branch B shows on branch A.
|
|
||||||
Already true for undo today (script card mutations are not reverted), so this is a
|
|
||||||
documented limit, not a regression. Explicitly rejected event-sourcing card changes onto
|
|
||||||
nodes.
|
|
||||||
- **State carries over almost free.** `state_before` / `world_state_before` already
|
|
||||||
snapshot the script scoreboard and RPG world state per action, and `apply_variant()`
|
|
||||||
already restores them on a switch — a branch switch is the same move. Wrinkle: those are
|
|
||||||
*before* pictures; a node wants the *after*. And they are NULL on pre-column rows.
|
|
||||||
|
|
||||||
## Schema sketch
|
|
||||||
|
|
||||||
```
|
|
||||||
branches(id, adventure_id, parent_branch_id, fork_depth, lineage JSON, created_at)
|
|
||||||
actions(id, adventure_id, branch_id, depth, type, text, reasoning,
|
|
||||||
world_delta, state_after, world_state_after, context_snapshot, created_at)
|
|
||||||
memories(..., branch_id, depth) -- attached to the node that produced it
|
|
||||||
adventures(..., head_branch_id, head_depth)
|
|
||||||
```
|
|
||||||
|
|
||||||
Reading branch C, tip at depth 7, lineage `[(C,7), (B,5), (A,3)]`:
|
|
||||||
|
|
||||||
```sql
|
|
||||||
SELECT * FROM actions
|
|
||||||
WHERE (branch_id='C' AND depth <= 7)
|
|
||||||
OR (branch_id='B' AND depth <= 5)
|
|
||||||
OR (branch_id='A' AND depth <= 3)
|
|
||||||
ORDER BY depth DESC LIMIT 32
|
|
||||||
```
|
|
||||||
|
|
||||||
→ `A0 A1 A2 A3 B4 B5 C6 C7`. Depth is a position along *a* path, not a global turn
|
|
||||||
number — `A4` and `B4` are alternatives, not duplicates.
|
|
||||||
|
|
||||||
## Work
|
|
||||||
|
|
||||||
1. `branches` table, `branch_id`/`depth` on `actions`, `head_*` on `adventures`.
|
|
||||||
2. Lineage computation + a **single module that owns the branch clause** — same role
|
|
||||||
`context/history.py` plays today. Every query must go through it; one forgotten clause
|
|
||||||
shows the wrong story, quietly.
|
|
||||||
3. `history.py` rewritten against lineage windowing. `window_covering` keeps its shape.
|
|
||||||
4. Memories/summary attached to nodes; delete the cursor-position machinery
|
|
||||||
(`position_of_index`, `settled_*`, `note_action_removed`, `_rewind_cursors_to_index`).
|
|
||||||
5. Sibling storage for un-promoted tip attempts + the promotion step.
|
|
||||||
6. Node state moves from *before* to *after* snapshots.
|
|
||||||
7. Migration: every existing adventure becomes branch A; `index` → `depth`; `variants`
|
|
||||||
entries become sibling nodes; `variant_index` becomes the head pointer.
|
|
||||||
8. Frontend: branch picker replacing `VariantPager`, plus branch management (rename,
|
|
||||||
delete, switch) — required, given no auto-pruning.
|
|
||||||
|
|
||||||
## Open
|
|
||||||
|
|
||||||
Nothing. The last item — **`retry_of.index` reuse**, which stopped the world-state
|
|
||||||
cooldown clock advancing on a re-run of the same turn — was closed in SP4 by reusing the
|
|
||||||
retried node's *depth*, and pinned by a test in SP5. Everything else that stood open here
|
|
||||||
was decided on 2026-08-17; see the next section.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Implementation plan (decided 2026-08-17)
|
|
||||||
|
|
||||||
Four decisions, taken before any code was written:
|
|
||||||
|
|
||||||
| Question | Decision |
|
|
||||||
|---|---|
|
|
||||||
| Phased or single migration? | **Structural-first, no runtime flag.** SP1–SP4 migrate to the tree *representation* with behaviour identical to today. Branching features layer on after. |
|
|
||||||
| How destructive is the migration? | **Keep the legacy columns for one release.** `index`, `variants`, `variant_index`, `variant_count` stay, unread, until the tree is proven live (SP8 drops them). |
|
|
||||||
| Branch-management UI scope | **Full tree visualisation** — a spatial view of forks, not just a picker. |
|
|
||||||
| Export bundle | **`ai-dnd-adventure-v2`**, with a v1 legacy reader so existing bundles keep importing. |
|
|
||||||
|
|
||||||
**Why no feature flag.** A linear story *is* a tree with one branch, so the intermediate
|
|
||||||
states are not half-migrated — they are the same product with a superset schema
|
|
||||||
underneath. That makes "current adventures are unaffected" a literal, testable pass
|
|
||||||
condition for every subphase up to SP4, which a flag would have replaced with two live
|
|
||||||
code paths through the context builder, the memory bank, undo and retry at once.
|
|
||||||
|
|
||||||
## The regression contract
|
|
||||||
|
|
||||||
`tests/test_story_tree_baseline.py` (built in SP0) drives the whole product over HTTP —
|
|
||||||
create, play, retry, switch, undo, page with `before_id`, memories, summary, export — and
|
|
||||||
asserts only on API responses, never internals.
|
|
||||||
|
|
||||||
**It must pass unmodified through SP1, SP2 and SP3.** That is the contract those
|
|
||||||
subphases are verified against. SP4 is the first subphase permitted to change it, and
|
|
||||||
even there only where the change is deliberate and named below.
|
|
||||||
|
|
||||||
## Traps found while reading (these are the ones that bite quietly)
|
|
||||||
|
|
||||||
- **`history._from_memory()` slices `adventure.actions`.** The "never load twice"
|
|
||||||
shortcut returns whatever is already in the session — which under a tree is *every
|
|
||||||
branch's* actions, not the path. It would silently assemble context from siblings.
|
|
||||||
This is the single highest-risk line in the change; SP2 must make the shortcut
|
|
||||||
branch-aware or delete it.
|
|
||||||
- **`scripting/pipeline.py::_history()` and `_info()` read `adventure.actions` directly**,
|
|
||||||
and hand it to user scripts as the documented history API. Same trap, user-visible.
|
|
||||||
- **`Adventure.actions` is `order_by="Action.index"`.** Ordering it by `depth` is not
|
|
||||||
enough — the collection is still every branch. Anything iterating it needs the path.
|
|
||||||
- **`limits.check_row_cap("actions")` counts every action in the adventure.** With no
|
|
||||||
auto-pruning, a branched adventure hits `MAX_ACTIONS_PER_ADVENTURE` while its *story*
|
|
||||||
is far shorter. The cap has to count the tree but be explained as the tree, or move.
|
|
||||||
- **The holdback cannot die in SP3.** `settled_story_actions` exists because retry
|
|
||||||
mutates a row in place. Retry stops mutating in SP4, so the holdback is only safe to
|
|
||||||
delete there — deleting it in SP3 reopens the exact bug it was written for. It survived
|
|
||||||
SP3 as `memorybank.settled_after`, which is the `- 1` in "how much story is past the
|
|
||||||
mark"; that subtraction is the whole of it.
|
|
||||||
|
|
||||||
*Closed in SP4, but not for the stated reason.* Siblings share a coordinate and the
|
|
||||||
mark names the coordinate, so replacing what a turn says still invalidates the memory
|
|
||||||
covering it — retry mutating a row was never the whole of the problem. What let the
|
|
||||||
holdback go is that the right repair (`forget_node` plus a rewind) already existed for
|
|
||||||
undo and delete, and retry and a sibling switch now make it too. **If a mark still
|
|
||||||
needs correcting when the story changes, correct it; do not decline to make the mark.**
|
|
||||||
|
|
||||||
## Subphases
|
|
||||||
|
|
||||||
Each ships independently, on its own branch, green before the next starts.
|
|
||||||
|
|
||||||
### SP0 — Baseline and regression net *(no product change)*
|
|
||||||
|
|
||||||
- `tests/test_story_tree_baseline.py` — the contract above. 24 tests covering opening a
|
|
||||||
windowed adventure, the turn engine (do/say/story/continue), script effects, retry and
|
|
||||||
variant switching, undo, paging up to the start, edit/delete, memories, export/import
|
|
||||||
round-trip, and world state.
|
|
||||||
|
|
||||||
**Done, 2026-08-17.** 259 existing tests green (17.8s), then **283 green** with the
|
|
||||||
baseline added, against unmodified `main`. That is the number every subphase below is
|
|
||||||
measured against.
|
|
||||||
|
|
||||||
A pre-migration (**schema 45**) fixture is needed too, built by a script rather than
|
|
||||||
committed as a binary — but its only consumer is the SP1 migration test, so it lands
|
|
||||||
there.
|
|
||||||
|
|
||||||
#### `--rich`: a correctness fixture beside the scale one
|
|
||||||
|
|
||||||
The measuring fixture is sized from production and leaves every column it does not weigh
|
|
||||||
at its default. Checked against a freshly built one, that is exactly the set of columns a
|
|
||||||
tree has to migrate: `state_before`/`world_state_before` NULL on all 600 rows, no
|
|
||||||
scenario and so no RPG layer, no adventure scripts, both cursors 0, and 100 retry
|
|
||||||
histories whose two attempts carry **byte-identical text with `variant_index` always 0** —
|
|
||||||
so "which attempt is live?", the one question SP4's migration answers, had no observable
|
|
||||||
answer.
|
|
||||||
|
|
||||||
`tools.stress_session --rich` fills in precisely those and nothing else:
|
|
||||||
|
|
||||||
```
|
|
||||||
cd backend
|
|
||||||
.venv/Scripts/python.exe -m tools.stress_session --rich --actions 30 --memories 12
|
|
||||||
```
|
|
||||||
|
|
||||||
An RPG scenario built from the real seed schema with a world state played forward (hp
|
|
||||||
100 → 71, flags flipping partway); per-action `state_before`/`world_state_before`
|
|
||||||
snapshots that are monotonic, so a bad rollback reads as a wrong number rather than as
|
|
||||||
nothing; a gold script on the adventure; story cards; non-zero memory and summary
|
|
||||||
cursors with a real `story_summary`; pinned and forgotten memories; retry attempts with
|
|
||||||
distinct texts, counts of 2 *and* 3, and a live attempt that is **often not the last one
|
|
||||||
written**; and a second adventure, so "does this leak across adventures?" is answerable —
|
|
||||||
a branch clause that forgot its adventure would still look correct on a database holding
|
|
||||||
exactly one.
|
|
||||||
|
|
||||||
It is a correctness fixture, so prefer it small: what it is for is variety per row, not
|
|
||||||
rows. **Its byte figures are not comparable to a plain run** and it does not replace the
|
|
||||||
scale fixture — the plain one still holds the egress ceilings, and was re-measured
|
|
||||||
unchanged (1.8 kB, actions 1.7 kB) after `--rich` was added.
|
|
||||||
|
|
||||||
The invariant `text == variants[variant_index]["text"]` holds on every retried row, and
|
|
||||||
is asserted when the fixture is built. SP4's migration reads exactly that to decide which
|
|
||||||
sibling becomes the head.
|
|
||||||
|
|
||||||
### SP1 — Schema and migration *(no behaviour change)*
|
|
||||||
|
|
||||||
| File | Change |
|
|
||||||
|---|---|
|
|
||||||
| `app/models.py` | New `Branch` model. `Action.branch_id`, `Action.depth`. `Adventure.head_branch_id`, `head_depth`. `Memory.branch_id`, `Memory.depth`. Legacy columns untouched. |
|
|
||||||
| `app/migrations.py` | Migrations 46+: create `branches`; add columns; index `(branch_id, depth)`; backfill. Dialect map wherever BLOB/BYTEA-style spellings diverge. |
|
|
||||||
|
|
||||||
Backfill: one branch per adventure, `lineage = [(A, ∞)]`; `actions.branch_id = A`,
|
|
||||||
`depth = index`; `adventures.head_*` from `max(index)`; memories take branch A and a
|
|
||||||
depth derived from `source_end`.
|
|
||||||
|
|
||||||
**Verify:** baseline test unmodified. New `tests/test_tree_migration.py` — every action
|
|
||||||
carries a branch and `depth == old index`, no row lost, head pointers correct, memories
|
|
||||||
mapped. Bootstrap run twice is a no-op. `tests/test_egress.py` ceilings unmoved (two
|
|
||||||
integers a row). **Post-deploy `VACUUM FULL actions;` is mandatory** — this rewrites
|
|
||||||
every row, which is the 144 MB lesson at the top of `STATUS.md`.
|
|
||||||
|
|
||||||
**Done, 2026-08-17** (branch `sp1-tree-schema`). **297 tests green**, the 283 from SP0
|
|
||||||
plus 14 in `test_tree_migration.py`. The baseline contract passes **unmodified**, which
|
|
||||||
was the pass condition. Five things the plan did not anticipate, all of them found by
|
|
||||||
building it:
|
|
||||||
|
|
||||||
- **The writes could not wait for SP2.** The file table above lists only `models.py` and
|
|
||||||
`migrations.py`, but a migration never visits a row written *after* it runs — so
|
|
||||||
shipping the columns without a writer would leave every turn played between the two
|
|
||||||
deploys with no branch, and from SP2 on a row with no branch is a row no read can see.
|
|
||||||
`app/tree.py` is that writer: `root_branch` / `head_branch` (get-or-create),
|
|
||||||
`place_action`, `place_memory`, `refresh_head`. One module for the same reason SP2 gets
|
|
||||||
one — a node written without a branch fails by *disappearing*, not by raising. Wired
|
|
||||||
into create/turn/import/undo/delete/memory, plus `seed_demo.py` and
|
|
||||||
`tools.stress_session` (a fixture built by `create_all` is stamped LATEST, so no
|
|
||||||
migration ever runs against it).
|
|
||||||
- **`adventures.head_branch_id` cannot be a foreign key.** `branches.adventure_id`
|
|
||||||
already points the other way, and the pair is then a cycle `create_all` refuses to
|
|
||||||
order; the fix for that is `use_alter`, which SQLite has no ALTER for. It is a plain
|
|
||||||
integer, documented as a cache, and `head_branch` recovers onto the root if it ever
|
|
||||||
names a branch that is gone.
|
|
||||||
- **`lineage` is NOT NULL, because `branches` comes from `create_all`.** The backfill
|
|
||||||
cannot insert a row and fill the lineage afterwards via a NULL marker, so it inserts
|
|
||||||
`'[]'` and guards step two on `json_array_length(lineage) = 0` — not `= '[]'`, because
|
|
||||||
Postgres `json` has no equality operator.
|
|
||||||
- **SQLite cannot drop a column a foreign key names.** So a current-schema database
|
|
||||||
cannot be rewound past `branch_id` at all, which broke the two existing tests that
|
|
||||||
simulate an old database by rewinding only the *stamp*. Fixed properly:
|
|
||||||
`migrations._column_already_there` makes every `ADD COLUMN` idempotent (the
|
|
||||||
`IF NOT EXISTS` the module docstring asks for and SQLite has no syntax for), and
|
|
||||||
`tests/schema_rewind.py` holds the inverse of the migrations that *can* be undone.
|
|
||||||
The SP1 fixture therefore builds a **genuine schema 45** by dropping the three tables
|
|
||||||
and recreating them from frozen pre-tree DDL, so the real ALTERs run — including the
|
|
||||||
one that adds a foreign key.
|
|
||||||
- **Deleting a branch takes its nodes with it**, and deleting an adventure takes its
|
|
||||||
branch — both verified, both at the database level via `ON DELETE CASCADE` on the two
|
|
||||||
`branch_id` columns. SP7's delete-a-branch needs no code of its own for the nodes.
|
|
||||||
|
|
||||||
Measured: `branches` costs **0.1 kB of a 733.5 kB turn (0 %)**, and the page load
|
|
||||||
(62.6 kB) and index (1.8 kB) shapes are byte-identical to the figures in `STATUS.md`.
|
|
||||||
One cost that is *not* free: the new table and its index add ~47 ms to every
|
|
||||||
`create_all`/`drop_all` cycle on SQLite, and the suite does one per test — 20 s → 38 s.
|
|
||||||
Test-only (DDL fsync), so no model change; if it ever matters, the fix is the test
|
|
||||||
harness, not the schema.
|
|
||||||
|
|
||||||
### SP2 — The branch clause *(reads move to lineage; still one branch)*
|
|
||||||
|
|
||||||
One module owns the clause; every action read goes through it. A forgotten clause shows
|
|
||||||
the wrong story, quietly, which is why it is one module and not a convention.
|
|
||||||
|
|
||||||
| File | Change |
|
|
||||||
|---|---|
|
|
||||||
| `app/context/lineage.py` *(new)* | Lineage computation and the branch clause. The only place that knows how a path is selected. |
|
|
||||||
| `app/context/history.py` | `_filters` takes the clause; order by `depth`; `window_covering` keeps its shape. **Fix `_from_memory`.** |
|
|
||||||
| `app/routers/adventures.py` | `action_window` anchors on the anchor's `depth`; `last_action`, `next_index`→`next_depth`, `_latest_narration`. |
|
|
||||||
| `app/scripting/pipeline.py` | `_history()`/`_info()` read the path, not the collection. |
|
|
||||||
|
|
||||||
**Verify:** baseline test unmodified. `test_history_window.py`, `test_action_paging.py`
|
|
||||||
green. New test builds a **two-branch fixture directly in the DB** and asserts the design
|
|
||||||
doc's own example reads back as `A0 A1 A2 A3 B4 B5 C6 C7`, and that a sibling's nodes are
|
|
||||||
invisible. Egress: a 20-fork fixture costs within a small factor of a 1-fork one —
|
|
||||||
clause count is bounded by the context window, not by fork count.
|
|
||||||
|
|
||||||
**Done, 2026-08-17** (branch `sp2-branch-clause`). **317 tests green**, the 297 from SP1
|
|
||||||
plus 20 in `test_branch_clause.py`. The baseline contract passes **unmodified**, which
|
|
||||||
was the pass condition. Six things worth not rediscovering:
|
|
||||||
|
|
||||||
- **The contract forced the write side, not the read side.** The SP0 baseline writes its
|
|
||||||
actions straight to the database and must pass unmodified — so "every writer calls
|
|
||||||
`place_action`" could not be the invariant, because the baseline is a writer and does
|
|
||||||
not. Neither did any of the eleven other test fixtures. The alternative was a read
|
|
||||||
tolerant of a NULL branch, which is the quiet-wrong-story failure this subphase exists
|
|
||||||
to make impossible. So the session enforces it instead: `tree.place_new_nodes` runs
|
|
||||||
from `Session.before_flush` and places anything unplaced, registered in `models.py` so
|
|
||||||
that importing the models arms it. The call sites keep their explicit calls — a node
|
|
||||||
placed at the call site is placed *before* the code around it reads the row back.
|
|
||||||
- **A branch could no longer be created with a flush.** `root_branch` did
|
|
||||||
`db.add(); db.flush()` to get the id its lineage names, and a nested flush inside
|
|
||||||
`before_flush` raises. It inserts through Core and reads the row back — same
|
|
||||||
transaction, three statements, once per adventure ever.
|
|
||||||
- **The identity map holds weak references, and it cost 25 % of the suite.** Resolving
|
|
||||||
the head branch per node re-read the `branches` row for every node in the flush, because
|
|
||||||
nothing held a strong reference between two calls: **201 SELECTs to write 200 actions**,
|
|
||||||
and 36 s → 45 s on the same 297 tests measured back to back. The head is now resolved
|
|
||||||
once per adventure per flush — **2 SELECTs**, and the same 297 tests then time within
|
|
||||||
noise of SP1 (44.2 s each; this machine's load drifts by ~20 % between runs, so trust
|
|
||||||
the statement count, not the stopwatch). Pinned by a test that counts reads of
|
|
||||||
`branches`, because the stopwatch is all the symptom there ever was.
|
|
||||||
- **The index screen is the one read scoped by head branch rather than by lineage.**
|
|
||||||
`_latest_narration` picks one row per adventure for a hundred adventures at once, and a
|
|
||||||
lineage clause each would put hundreds of OR-terms on that query. The two answers differ
|
|
||||||
only for a branch with no nodes of its own, which cannot exist — a branch is created by
|
|
||||||
playing a turn onto it.
|
|
||||||
- **Two reads are deliberately left un-pathed**, both documented where they live.
|
|
||||||
`max_action_index` allocates the legacy `index`, which must stay adventure-wide or two
|
|
||||||
branches issue the same number; and export is a flat v1 bundle whose reader has no idea
|
|
||||||
branches exist, which is why SP6 replaces the format rather than widening the query.
|
|
||||||
A third is a known divergence, not a decision: the index screen's `action_count` counts
|
|
||||||
the tree, and will overstate a branched story until SP5.
|
|
||||||
- **`Adventure.actions` was left ordered by `index` on purpose.** Ordering the collection
|
|
||||||
by depth would not make it a story — it is every branch's actions, and a path is a
|
|
||||||
selection out of it. What the relationship is still for is ownership and the
|
|
||||||
delete-orphan cascade.
|
|
||||||
|
|
||||||
Measured: a story forked **20 times reads its newest 32-action window for 3,178 B against
|
|
||||||
the 2,961 B an unforked story of the same length costs (1.07×)**, naming one branch of its
|
|
||||||
22 lineage entries. The pre-tree 600-action `--keep` fixture was migrated and then driven
|
|
||||||
over HTTP end to end: index **1,840 B**, page load **64,149 B** — the same shapes as
|
|
||||||
before the phase — and scrolling to the start took 9 pages and saw all 600 actions exactly
|
|
||||||
once.
|
|
||||||
|
|
||||||
### SP3 — Memories and summary attach to nodes
|
|
||||||
|
|
||||||
Cursors stop being positions in a shifting list. `memory_cursor`/`summary_cursor` become
|
|
||||||
node-anchored `(branch_id, depth)`.
|
|
||||||
|
|
||||||
Deleted: `position_of_index`, `note_action_removed`, `_rewind_cursors_to_index`,
|
|
||||||
`prune_dangling_memories`, and their call sites in `undo_turn` and `delete_action`.
|
|
||||||
**Kept until SP4:** `settled_story_actions` and the holdback (see traps).
|
|
||||||
|
|
||||||
**Verify:** baseline test unmodified. `test_memory_settling.py` and
|
|
||||||
`test_memory_retrieval.py` updated, plus a new branch-isolation test — a memory created
|
|
||||||
on branch B is invisible from branch A, and shared ancestors are visible from both.
|
|
||||||
Memory retrieval reads the *full* lineage (it cannot be windowed) but stays sparse:
|
|
||||||
assert the byte cost on a deep fork.
|
|
||||||
|
|
||||||
**Done, 2026-08-18** (branch `sp3-node-cursors`). **330 tests green**, the 318 the branch
|
|
||||||
started from plus 12 — 11 in `test_branch_clause`'s new sibling `test_memory_nodes.py`
|
|
||||||
and one on migration 56. The baseline contract passes **unmodified**, which was the pass
|
|
||||||
condition. `app/context/cursors.py` is the new module; migrations 53–56 add
|
|
||||||
`memory_cursor_branch_id/_depth` and `summary_cursor_branch_id/_depth` and translate the
|
|
||||||
old counts into them.
|
|
||||||
|
|
||||||
Seven things worth not rediscovering:
|
|
||||||
|
|
||||||
- **An anchor is a coordinate, not a pointer, and that is what deleted the machinery.**
|
|
||||||
Half of this subphase was expected to be rewriting the cursor bookkeeping in depth
|
|
||||||
terms. None of it needed rewriting: `count_after(41)` is well defined with node 41
|
|
||||||
deleted, and deleting node 12 does not change what "past node 41" means. So
|
|
||||||
`note_action_removed`, `_rewind_cursors_to_index`, `position_of_index` and the
|
|
||||||
post-turn clamp did not become depth-shaped versions of themselves — they became
|
|
||||||
nothing. **If a mark still needs correcting when the story changes, it is still a
|
|
||||||
position.**
|
|
||||||
- **The clamp had its own trap and it also goes.** `run_post_turn` clamped both cursors
|
|
||||||
to the story length every pass, deliberately against the *full* count, because
|
|
||||||
clamping to the settled count rewound a caught-up adventure a step and re-covered an
|
|
||||||
action. An anchor past the tip is not a broken value: `settled_after` reports nothing
|
|
||||||
to do, and the story growing back past it resumes exactly where it left off.
|
|
||||||
- **`prune_dangling_memories` became a lookup, and got stricter by accident.** A memory
|
|
||||||
hangs off the node its block ends on, so "what did this node produce?" is
|
|
||||||
`(branch_id, depth)` — `memorybank.forget_node`. The scan it replaces could only ever
|
|
||||||
notice damage *after* the fact (a covered range past `max(index)`), and could not
|
|
||||||
notice at all when the node was deleted from the middle of a story that still had
|
|
||||||
later actions. Withdrawing the memory is half the job: the ground it covered is still
|
|
||||||
behind the mark, so the mark goes back to `source_start - 1` — a depth, whether or not
|
|
||||||
a row still sits there.
|
|
||||||
- **A memory with no node had to be spelled out in the clause.** A hand-written memory
|
|
||||||
summarises nothing, so it carries a branch and a NULL depth. Every ancestor entry in a
|
|
||||||
lineage clause is capped `depth <= fork`, and NULL fails that — so a typed memory would
|
|
||||||
have become invisible at the first fork after it was written, with nothing to see but a
|
|
||||||
prompt that stopped mentioning it. `Path.clause(unanchored=True)` is that case, and
|
|
||||||
actions never pass it: an action with no depth is a pre-tree row no read should see.
|
|
||||||
- **Retrieval reads the whole lineage, and it is free.** Measured on two stories of 84
|
|
||||||
actions and 14 memories each, one flat and one forked twenty times: **1,807 B against
|
|
||||||
1,823 B**. The clause carries 22 branch terms instead of one, and the clause is not what
|
|
||||||
crosses the wire. The egress shapes are otherwise byte-identical to SP1's — index
|
|
||||||
1.8 kB, page load 62.7 kB, turn 733.8 kB.
|
|
||||||
- **Three reads stay adventure-wide, deliberately.** Embedding and eviction are facts
|
|
||||||
about the row and about the bank, not about the path — skipping a sibling's memories
|
|
||||||
would only mean embedding them at the moment somebody switched to them, and evicting the
|
|
||||||
memories of a story nobody is reading is the right thing to evict first. The Memories
|
|
||||||
drawer is management rather than retrieval, and hiding a branch's memories there would
|
|
||||||
make them unfindable in a phase whose rule is that nothing is removed automatically.
|
|
||||||
- **The v1 bundle still speaks positions, in exactly two places.** Export counts the
|
|
||||||
anchor back into a position; import translates the other way, but only after the
|
|
||||||
actions exist, because that is the one moment the two coordinate systems can be lined
|
|
||||||
up. `cursors.position_of` and `cursors.anchor_at_position` are the whole of what still
|
|
||||||
knows about positions, and SP6's v2 format retires them.
|
|
||||||
|
|
||||||
Migrations 53–56 rewrite `adventures`, not `actions` — a few hundred rows against a few
|
|
||||||
hundred thousand — so **this deploy needs no `VACUUM FULL` of its own**. The one SP1 owes
|
|
||||||
is still owed.
|
|
||||||
|
|
||||||
### SP4 — Variants become sibling nodes
|
|
||||||
|
|
||||||
Retry stops mutating a row. It writes a sibling leaf at the same depth.
|
|
||||||
|
|
||||||
Deleted: `set_variants`, `variant_of`, `apply_variant`, `VARIANT_SNAPSHOT_KEYS`, and the
|
|
||||||
holdback. Node state moves from *before* to *after* snapshots (`state_after`,
|
|
||||||
`world_state_after`) — a sibling needs its own outcome, so this belongs here rather than
|
|
||||||
in its own subphase. Pre-column rows are NULL and must stay tolerated.
|
|
||||||
|
|
||||||
A migration converts existing `variants` JSON into sibling rows — reading the legacy
|
|
||||||
column that decision 2 kept, which is the whole reason it was kept.
|
|
||||||
|
|
||||||
`/variants` and `/variant` keep their URLs and response shapes here, re-implemented over
|
|
||||||
sibling rows, so the frontend keeps working until SP7 replaces it.
|
|
||||||
|
|
||||||
**Verify:** the first subphase allowed to move the baseline test, and only for
|
|
||||||
`variant_count`/`variant_index` semantics — the retry *outcomes* must not move.
|
|
||||||
`test_retry_variants.py` rewritten against siblings but asserting the same observable
|
|
||||||
results, including that the gold script is not double-applied. `test_state_revert.py`
|
|
||||||
against after-snapshots. Migration test: attempt count preserved, the active attempt
|
|
||||||
becomes the head.
|
|
||||||
|
|
||||||
**Done, 2026-08-18** (branch `sp4-sibling-nodes`). **347 tests green**, the 330 SP3
|
|
||||||
finished with plus 13 in the new `test_attempt_siblings.py`, 8 in `test_tree_migration
|
|
||||||
.py`, and three holdback tests deleted. `app/attempts.py` is the new module; migrations
|
|
||||||
57–60 add `live`, `state_after`, `world_state_after`, derive the after-snapshots, and
|
|
||||||
split every `variants` list into rows.
|
|
||||||
|
|
||||||
**The baseline test did not have to move, and neither did `test_retry_variants.py`.**
|
|
||||||
Both pass unmodified. SP4 was *permitted* to change the variant-count semantics and it
|
|
||||||
turned out nothing observable needed changing — which is the strongest form the pass
|
|
||||||
condition could have taken, and worth knowing before SP6 asks for the same licence.
|
|
||||||
|
|
||||||
Seven things worth not rediscovering:
|
|
||||||
|
|
||||||
- **A coordinate needs a `live` flag, and the branch clause is where it belongs.**
|
|
||||||
Siblings share `(branch_id, depth)`, so the lineage clause alone returns all of them
|
|
||||||
and the story tells itself twice. `Path.clause` adds `Action.live` for actions (and
|
|
||||||
only for actions — memories have no siblings to lose to), which means no read of the
|
|
||||||
story had to learn that retries exist. `app/attempts.py` is the only code that looks
|
|
||||||
past it.
|
|
||||||
- **The prompt has to move with the flag or retry becomes a storage multiplier.** A
|
|
||||||
`context_snapshot` is ~163 kB of prompt that every attempt at a turn shares, and the
|
|
||||||
old JSON list existed precisely to store it once. Giving each sibling row a copy would
|
|
||||||
have undone that. So the invariant is: the assembled prompt lives on the attempt the
|
|
||||||
story tells, and a superseded one keeps only its own slices (`ATTEMPT_KEYS` — the
|
|
||||||
world-state delta, the script report, the raw reply). Measured on the pre-tree
|
|
||||||
600-action fixture, migrated: **700 rows for the same 600-turn story, and the prompt
|
|
||||||
archive byte-identical at 0.50 MB.**
|
|
||||||
- **The holdback was not quite unnecessary — it was the wrong repair.** The plan said
|
|
||||||
retry stops mutating rows so nothing goes stale. Not so: siblings share a coordinate,
|
|
||||||
and the mark and the memory both name the *coordinate*, so replacing what a turn says
|
|
||||||
still invalidates them. What made the holdback deletable is that the right repair
|
|
||||||
already existed — `forget_node` plus a rewind, which undo and delete have called since
|
|
||||||
SP3. Retry and a sibling switch now call it too, and the holdback (`settled_count`,
|
|
||||||
`settled_after`, `settled_story_actions`, `newest_settled`) is gone. **A memory can
|
|
||||||
now cover the newest action**, which it never could before.
|
|
||||||
- **`state_after` needed the same flush guard `place_action` has.** The migration
|
|
||||||
derives every existing row's outcome from the next row's `state_before`, and the tip's
|
|
||||||
from the adventure's live state — but a row written by a fixture or a script after
|
|
||||||
that has nobody to derive from, and the failure mode is undo silently leaving the
|
|
||||||
scoreboard where it was. `tree.stamp_outcome` runs from `before_flush` beside
|
|
||||||
`place_new_nodes`. It writes the truth as of the flush: a writer that changes no state
|
|
||||||
between two nodes leaves the same state behind both.
|
|
||||||
- **Switching attempts hands the caller a different row id**, because that is what an
|
|
||||||
attempt being a node *means*. The endpoints are addressed by any attempt at the turn
|
|
||||||
rather than by the live one, so a client holding a stale id still asks about the right
|
|
||||||
turn — but `Play.jsx` matched the reply against `updated.id` and had to be changed to
|
|
||||||
match against the action it asked about. One line, and SP7 removes the pager anyway.
|
|
||||||
- **Deleting a turn deletes its attempts.** A discarded attempt is only reachable
|
|
||||||
*through* its coordinate, so leaving it behind would leave a row nothing can name and
|
|
||||||
no read can see. `delete_turn` is that rule in one place, called by undo and by
|
|
||||||
delete-an-action, and it works whichever attempt's id the caller happens to hold.
|
|
||||||
- **Siblings share the legacy `index`.** They are takes on one turn, so two rows now
|
|
||||||
carry one index — which `max_action_index` (a maximum, not a count) survives, and
|
|
||||||
which is what lets the v1 export fold a group back into a `variants` array. Export is
|
|
||||||
now the only producer of that shape anywhere; nothing in the database holds one.
|
|
||||||
|
|
||||||
Measured: index **1.8 kB**, page load **62.7 kB**, one turn **734.8 kB** — the first two
|
|
||||||
byte-identical to SP1's and SP3's, the turn up 1.0 kB (0.14 %) for the `live` column
|
|
||||||
across a 346-row read. Migrations 57–59 each rewrite every row of `actions` and 60
|
|
||||||
inserts one per discarded attempt, so **this deploy owes a `VACUUM FULL actions;`** —
|
|
||||||
the SP1 one is still owed too, and one vacuum after this deploy settles both.
|
|
||||||
|
|
||||||
### SP5 — Fork on continue
|
|
||||||
|
|
||||||
Playing past a non-head sibling promotes it: a `branches` row with `parent_branch_id`,
|
|
||||||
`fork_depth`, and a `lineage` computed once from the parent's. Nothing is copied.
|
|
||||||
`adventures.head_*` moves. **The cooldown clock must not advance on a re-run of the same
|
|
||||||
turn** — the one item still open above.
|
|
||||||
|
|
||||||
**Verify:** a fork creates exactly one branch row and copies no actions; lineage is
|
|
||||||
correct and capped at each `fork_depth`; both branches read independently; switching
|
|
||||||
restores the right script/world state; the cooldown test from `test_worldstate.py`
|
|
||||||
still holds across a retry.
|
|
||||||
|
|
||||||
**Done, 2026-08-18** (branch `sp5-fork-on-continue`). **365 tests green**, the 347 from
|
|
||||||
SP4 plus 18 in `test_branch_forking.py`. `tree.fork` is the whole of it; three endpoints
|
|
||||||
(`GET /branches`, `POST /branches/{id}/switch`, `POST /actions/{id}/fork`) are what SP7's
|
|
||||||
tree view will be drawn on.
|
|
||||||
|
|
||||||
**Where the promotion happens, and why not where the plan said.** The plan put it on the
|
|
||||||
*next turn*: attempts stay leaves, and one becomes a branch when a turn is played past
|
|
||||||
it. Promoting the winner then means moving a row off the branch the reader is standing
|
|
||||||
on and leaving that branch to pick a new node for the depth — it disturbs a story nobody
|
|
||||||
asked to change. The same divergence, seen from the other side, promotes the attempt you
|
|
||||||
are *leaving for*: `POST /actions/{id}/fork` gives the discarded attempt a branch and
|
|
||||||
moves the head to it, and the line it leaves is untouched. Branch count is identical
|
|
||||||
either way — one per divergence somebody actually built on — and one of the two never
|
|
||||||
rewrites a story in place. Without it, the losing attempts would also be unreachable
|
|
||||||
forever, since only the tip can be switched: fork-on-continue alone is a one-way door.
|
|
||||||
|
|
||||||
Six things worth not rediscovering:
|
|
||||||
|
|
||||||
- **A fork must not move the derived work, and it was about to.** The first cut moved the
|
|
||||||
memories at the forked coordinate onto the new branch and re-anchored the cursors that
|
|
||||||
named it. Both are wrong, and for the same reason: a memory describes whichever attempt
|
|
||||||
was *live* at that coordinate, which is the one staying on the parent. The right answer
|
|
||||||
needs no code at all — the lineage caps the parent at `fork_depth`, one depth short of
|
|
||||||
it, so the memory is simply out of range from the fork, invisible to both the retrieval
|
|
||||||
clause and `Path.depth_on`. The block is summarized again, from the text this branch
|
|
||||||
actually tells.
|
|
||||||
- **Depth had to stop following `index`.** They agreed until now. `index` is
|
|
||||||
adventure-wide (the v1 bundle is keyed on it) so on a story forked at depth 6 after
|
|
||||||
twenty turns it hands the next node depth 21 and leaves a fourteen-deep hole in the
|
|
||||||
middle of a path — which every windowing estimate then has to work around.
|
|
||||||
`next_depth` is `head_depth + 1`; `place_action` still derives depth from index for
|
|
||||||
fixtures and imports, which is what that default is for.
|
|
||||||
- **Undo has to stop at the fork.** It reads the newest two nodes on the path and deletes
|
|
||||||
the turn they make up — and on a fresh branch the second of them is borrowed from the
|
|
||||||
parent, whose story also contains it. The guard is on the row's own `branch_id`, not on
|
|
||||||
the fork depth, because that is the fact that decides it.
|
|
||||||
- **The cooldown clock came out right for free.** It lives in `_meta.last_changed` inside
|
|
||||||
the world state, and the world state is restored from the tip's `world_state_after` on
|
|
||||||
every switch — so each branch carries its own clock without anything knowing there is
|
|
||||||
one. The carried-over open item (a retry must not advance it) is SP4's reused depth,
|
|
||||||
and both are pinned by tests.
|
|
||||||
- **The session does not autoflush**, and `fork` read the sibling group after moving the
|
|
||||||
node out of it — so the move had not been written and the node was renumbered straight
|
|
||||||
back into the group it had just left. Read the group first. (`autoflush=False` is
|
|
||||||
deliberate, in `database.py`; anything in this phase that mutates then queries the same
|
|
||||||
rows has to order itself by hand.)
|
|
||||||
- **`/fork` has to be idempotent before it is anything else**, because a fork leaves the
|
|
||||||
promoted attempt alone on its branch: a repeated call — a double click, a retried
|
|
||||||
request — would otherwise be told the turn it just forked has nothing to fork to. The
|
|
||||||
"already the story" case is answered before the shape of the turn is looked at.
|
|
||||||
|
|
||||||
Measured, on a 40-turn story forked **twenty** times against the same story flat:
|
|
||||||
21 branches, 140 rows, an 80-action story, and a page load of **31,652 B against
|
|
||||||
31,433 B (1.007×)**. A branch costs **103 B** of id, parent, fork depth and cached
|
|
||||||
ancestry. No migration, no vacuum.
|
|
||||||
|
|
||||||
**One gap, deliberately left for SP6.** A forked adventure has no honest `v1` export —
|
|
||||||
the format has one story and there are two — so export emits every branch's turns
|
|
||||||
interleaved by index, which reads as a mangled story rather than as lost data. SP6's v2
|
|
||||||
bundle fixes it, and SP7 is where a player first gets any way to fork at all, so the
|
|
||||||
order those two ship in is the order that matters.
|
|
||||||
|
|
||||||
**Also known, and not fixed here:** the two cursors are one pair on the adventure, so
|
|
||||||
switching branches makes the mark on the branch being left unreadable from the new one
|
|
||||||
(`Path.depth_on` answers "nothing covered", which is the safe direction — redo the work,
|
|
||||||
never skip it). Switching back and forth therefore re-summarizes. Per-branch cursors are
|
|
||||||
the fix if it ever matters; it costs AI calls, not correctness.
|
|
||||||
|
|
||||||
### SP6 — Export/import v2
|
|
||||||
|
|
||||||
`ai-dnd-adventure-v2` carries branches and nodes; import accepts v1 and v2, mapping a v1
|
|
||||||
bundle's linear actions plus `variants` onto one branch with siblings.
|
|
||||||
`limits.check_bundle_lists` learns about branches.
|
|
||||||
|
|
||||||
**Verify:** v2 round-trip of a branched adventure is lossless; a v1 bundle still imports;
|
|
||||||
a bundle claiming more branches than rows is rejected rather than half-applied.
|
|
||||||
|
|
||||||
**Done, 2026-08-18** (branch `sp6-bundle-v2`). **381 tests green**, the 365 SP5 finished
|
|
||||||
with plus 16 in the new `test_bundle_v2.py`. `app/bundle.py` owns both formats; the two
|
|
||||||
endpoints in `routers/adventures.py` are a delegation and the shared plumbing, and the
|
|
||||||
`variants` array now exists nowhere but the v1 *reader*.
|
|
||||||
|
|
||||||
**The rule the module is built on: a bundle carries what was *chosen*, never what is
|
|
||||||
*derived*.** The head branch, the fork points, the live flags and the anchors are
|
|
||||||
decisions somebody made, and they are in the file. `lineage`, the head *depth*, `index`
|
|
||||||
and the variant ordinals are computed from those and are rebuilt on the way in. That is
|
|
||||||
not tidiness — a bundle is a text file anybody can edit, and every derived field shipped
|
|
||||||
beside its source is a chance for the file to disagree with itself in a way no read would
|
|
||||||
report. It is also the answer to "is the round trip lossless?": everything omitted is
|
|
||||||
reconstructed, and the tests assert the reconstruction rather than the bytes.
|
|
||||||
|
|
||||||
Six things worth not rediscovering:
|
|
||||||
|
|
||||||
- **`depth` cannot be the legacy `index`, and this is where that stops being academic.**
|
|
||||||
They agreed until SP5, and a bundle is the first writer that has to fill `index` for a
|
|
||||||
*forked* story — where two branches both have a node at depth 4. `index`'s one
|
|
||||||
remaining job is handing the next row a number nothing else holds, which is a fact
|
|
||||||
about the adventure rather than about a path, so the import allocates one per turn in
|
|
||||||
bundle order: siblings share it, the way SP4 leaves them, and no two coordinates do.
|
|
||||||
- **Validation happens before the adventure row exists.** Everything a hand-edited file
|
|
||||||
can get wrong about the shape of a tree — a node naming a branch that is not listed, a
|
|
||||||
fork with no depth, a branch forking from one listed after it — is a 400 raised by
|
|
||||||
`plan`, which touches no session. The alternative is an adventure holding a story with
|
|
||||||
a hole in it, and this whole phase exists because a story with a hole in it fails by
|
|
||||||
going quiet.
|
|
||||||
- **A branch may only fork from one listed before it.** That is how the export writes
|
|
||||||
them, and requiring it buys acyclicity for the price of a comparison — a cycle in the
|
|
||||||
parent chain would be an import that never returns rather than one that fails.
|
|
||||||
- **`{}` and absent are different snapshots.** An empty `state_after` means "this node
|
|
||||||
left an empty scoreboard behind"; a missing one means "nobody knows, leave the live
|
|
||||||
state alone" (`attempts.restore_state`). Trimming empty dicts on the way out would have
|
|
||||||
saved eighteen bytes a row and turned an undo that clears a score into one that leaves
|
|
||||||
it standing. Only `worldDelta`, which is display, is dropped when empty.
|
|
||||||
- **A file is allowed to be wrong about which attempt is live, and the import corrects it
|
|
||||||
rather than refusing.** "Exactly one sibling in a group is live" is an invariant of the
|
|
||||||
*database*, not of the format; a coordinate with none is a turn no read can see, so the
|
|
||||||
first attempt is made live. That is a different class from a missing branch, which is
|
|
||||||
structure, and is refused.
|
|
||||||
- **`limits.MAX_BRANCHES_PER_ADVENTURE` (1000) has no live counterpart.** Forking is a
|
|
||||||
POST that adds one row and has no cap of its own, so this is the one bundle cap that
|
|
||||||
does not mirror something creation enforces. Worth closing if branch management ever
|
|
||||||
makes forking cheap to repeat.
|
|
||||||
|
|
||||||
**The verify line above was slightly wrong, and the code does the honest version.** "More
|
|
||||||
branches than rows" fails on an adventure with no actions, which legitimately has one
|
|
||||||
branch and no rows. It became two rules instead: a cap on the branch list, and *every
|
|
||||||
branch a node names must exist*.
|
|
||||||
|
|
||||||
Measured with `tools/measure_bundle.py` on the 600-action `--rich` fixture — 600 turns,
|
|
||||||
750 nodes, because 150 of them were retried:
|
|
||||||
|
|
||||||
| | bytes | vs v1 |
|
|
||||||
|---|---|---|
|
|
||||||
| v1 shape | 587,475 | — |
|
|
||||||
| **v2** | **911,229** | **1.551×** |
|
|
||||||
| v2 without the outcomes | 544,318 | 0.927× |
|
|
||||||
|
|
||||||
**The tree is free; the outcomes are what cost.** Coordinates *save* 57.5 B a node
|
|
||||||
against v1's turn-and-variants shape, and the entire 1.55× is `state_after` /
|
|
||||||
`world_state_after` at 489 B a node — which are there because a bundle without them
|
|
||||||
imports a tree nobody can switch inside. Twenty forks add 660 B to the same file, **33 B
|
|
||||||
a branch**, so the format is as indifferent to fork count as the reads are. The longest
|
|
||||||
adventure production holds exports at 4.3 % of `MAX_IMPORT_BODY_BYTES`.
|
|
||||||
|
|
||||||
**Two lines of the SP0 baseline changed, not the one it predicted.** The note in
|
|
||||||
`test_story_tree_baseline.py` allowed for the `format` assertion; the `variants` array in
|
|
||||||
`test_export_keeps_retry_attempts` is the same fact from the other side — a bundle with
|
|
||||||
coordinates has no use for a repeating group. Everything else in that file still passes
|
|
||||||
unmodified.
|
|
||||||
|
|
||||||
**No migration, no vacuum.** SP6 adds no column and rewrites no row.
|
|
||||||
|
|
||||||
**And one trap, paid for once.** `tools/measure_bundle.py` imported `app` before
|
|
||||||
`tools.stress_session`, which is what points `AIDND_DB_PATH` at a throwaway file —
|
|
||||||
`app.database` reads it at module scope. It fails by *working*: the first run seeded a
|
|
||||||
synthetic user and adventure into the local `backend/data.db` and printed perfectly good
|
|
||||||
numbers, and only the second run tripped over the unique email. Anything importing that
|
|
||||||
harness must import it first, and the file now says so where the imports are.
|
|
||||||
|
|
||||||
### SP7 — Frontend: the tree becomes reachable
|
|
||||||
|
|
||||||
`VariantPager` is removed. A branch view replaces it, plus switch, rename and
|
|
||||||
delete-with-confirm. `api.js` gains the branch endpoints.
|
|
||||||
|
|
||||||
**Verify:** this is where the standing open gap gets closed — **drive the 600-action
|
|
||||||
`--keep` fixture in a browser by hand**, on the same scroll path that has never been
|
|
||||||
driven and already hid one bug. A vitest + jsdom harness covers the prepend arithmetic;
|
|
||||||
jsdom has no layout, so scroll position still needs eyes.
|
|
||||||
|
|
||||||
**Done, 2026-08-18** (branch `sp7-tree-ui`). **396 tests green**, the 381 SP6 finished
|
|
||||||
with plus 15 in the new `test_branch_management.py`. Driven by hand against the `--keep`
|
|
||||||
fixture in Chrome, which is how the one bug below was found.
|
|
||||||
|
|
||||||
**Shape chosen: a branch rail, not a spatial node map.** Three mockups were built and
|
|
||||||
compared before any of it was written, and the deciding argument was not aesthetic. A
|
|
||||||
node map is a second windowing problem — the fixture this subphase must be verified
|
|
||||||
against is 600 actions, which is 600 nodes — so building one would have spent SP7 on the
|
|
||||||
thing that delays the verification SP7 exists to do. The rail ships now; the map is a
|
|
||||||
later feature and costs nothing extra to add, because both draw from the same
|
|
||||||
`GET /branches`. The panel sits beside Plot/Memory/Scripts/Insights, which is this app's
|
|
||||||
existing idiom for a right-hand rail rather than a new one.
|
|
||||||
|
|
||||||
**SP7 was not a frontend-only subphase, and the spec above did not say so.** Of the three
|
|
||||||
operations it names, SP5 had built exactly one. `switch` existed; `rename` had no column
|
|
||||||
and no route, `delete` had no route at all. So it opens with migration 61
|
|
||||||
(`branches.name`), a `PATCH` and a `DELETE` — worth remembering for any future subphase
|
|
||||||
whose one-line spec says "plus the UI for X".
|
|
||||||
|
|
||||||
Five things worth not rediscovering:
|
|
||||||
|
|
||||||
- **A name is stored; a label is derived.** `branches.name` is NULL until somebody
|
|
||||||
chooses one, and the client draws an unnamed branch from its fork depth
|
|
||||||
(`Fork at moment 547`). A generated "branch 4" in the column would be a lie the moment
|
|
||||||
branch 3 is deleted and the ordinals shift under it; a fork depth is a coordinate, and
|
|
||||||
nothing can shift it. The v2 bundle carries the name for exactly the reason SP6 gives
|
|
||||||
for carrying the fork points — it is a decision, not something computed from one.
|
|
||||||
- **Refusing to delete the head is only half of it.** The other half is refusing any
|
|
||||||
branch the head was *forked from*: `parent_branch_id` cascades, so deleting an ancestor
|
|
||||||
takes the head with it and leaves `head_branch_id` pointing at a row that is gone. One
|
|
||||||
membership test against the head's own `lineage` covers both, because a lineage already
|
|
||||||
names itself and every branch it borrows from.
|
|
||||||
- **A deleted branch's cursor has to be cleared, and the reason is SQLite.** On Postgres
|
|
||||||
a stale branch id simply never resolves. SQLite hands the freed id to the next fork, at
|
|
||||||
which point the anchor resolves onto a branch it has never seen and reports a stretch
|
|
||||||
of story as already summarized — losing it from the memories for good. Same class as
|
|
||||||
the width-mismatch `cosine` returning 0.0: it reports nothing.
|
|
||||||
- **`VariantOut` had to grow an `id`.** A fork is addressed by the node being taken. The
|
|
||||||
group renumbers whenever an attempt is added, so an ordinal held across that points at
|
|
||||||
a different take — the same reason SP4's note called the pager's index match "one line,
|
|
||||||
and SP7 removes the pager anyway".
|
|
||||||
- **Four panels reloaded on the wrong thing, and only a browser could say so.**
|
|
||||||
`actions.length` was the refresh key for the Branches panel, the Status drawer
|
|
||||||
(script state), Insights and the Memory Bank. **A branch switch does not change the
|
|
||||||
length of the story** — it changes which story it is. So the Branches panel drew a
|
|
||||||
one-branch tree while the reader was already on a second, Insights showed the prompt
|
|
||||||
for the path just left, and the scoreboard kept the other line's numbers. The server
|
|
||||||
was correct throughout, and nothing in 396 tests could see any of it. All four now key
|
|
||||||
on `${actions.length}:${stateKey}`, and `stateKey` is bumped by `adoptWindow` — the two
|
|
||||||
operations that move the head — plus branch deletion, which removes that branch's
|
|
||||||
memories without a turn being played.
|
|
||||||
|
|
||||||
`tools/branch_fixture.py` exists because of this: it builds two branches of **equal
|
|
||||||
path length**, which is the case `actions.length` cannot distinguish at all. The
|
|
||||||
`--keep` fixture could not have found it, and neither could a fixture whose branches
|
|
||||||
happened to differ in length.
|
|
||||||
|
|
||||||
**What a switch puts back was checked end to end, not just server-side.** SP5 already
|
|
||||||
proved `restore_state` in `test_switching_restores_the_script_and_world_state`; what had
|
|
||||||
never been looked at is whether the *screen* re-reads it. On `tools/branch_fixture.py`,
|
|
||||||
switching between the two branches moves the World State drawer from **hp 60 to hp 95**
|
|
||||||
live, redraws the bar, swaps the story to the other take (`hp -5`, not `hp -40`) and
|
|
||||||
repoints Insights at the other path — "History: 5 of 5 actions", carrying the scratch and
|
|
||||||
not the beating.
|
|
||||||
|
|
||||||
**Every memory is now a memory of a story, not of an adventure — migration 62.** A
|
|
||||||
hand-written memory used to carry a NULL depth, described as "belongs to the adventure
|
|
||||||
rather than to a path". That reads as harmless and is not: a NULL is a coordinate no fork
|
|
||||||
can cap, so a note typed on one line followed the reader onto branches whose events it
|
|
||||||
never described. `tree.place_memory` anchors it at the head instead — *the story you were
|
|
||||||
reading when you wrote it* — and it then obeys exactly the rule a summarised memory obeys.
|
|
||||||
|
|
||||||
The whole `unanchored` escape clause in `lineage.Path.clause` existed for that one case
|
|
||||||
and is **deleted**, not merely unused. Its docstring argued a capped `depth <= fork` would
|
|
||||||
drop a typed memory "the moment its branch stopped being the newest entry"; anchoring is
|
|
||||||
the better answer to the same worry, because the memory is not exempt from the path, it is
|
|
||||||
*on* one.
|
|
||||||
|
|
||||||
**The drawer shows the path being read, and nothing else.** Same clause as retrieval, so
|
|
||||||
the bank you can see is the bank the model can see — one question, one answer. The earlier
|
|
||||||
attempt at this shipped an adventure-wide list with an `on_path` flag and an *another
|
|
||||||
branch* badge; anchoring makes that redundant, and the field, the badge and its CSS are
|
|
||||||
gone. Nothing is stranded by hiding: a memory lives on a branch, switching to that branch
|
|
||||||
shows it, and deleting the branch deletes it
|
|
||||||
(`test_deleting_a_branch_deletes_the_memories_written_on_it`).
|
|
||||||
|
|
||||||
Pinning is unchanged and still path-scoped: it decides *order*, the path decides
|
|
||||||
*existence*. A pinned memory on a branch you are not reading is not sent, because the path
|
|
||||||
clause runs before pinning is considered.
|
|
||||||
|
|
||||||
**Migration 62 anchors existing NULL-depth memories at depth 0 of their branch**, not at
|
|
||||||
the tip. 0 is at or before every fork point, so every memory stays visible from exactly
|
|
||||||
the paths it is visible from today — the anchor takes nothing out of anybody's bank on
|
|
||||||
deploy. Anchoring at the tip would have emptied them out of every branch forked earlier
|
|
||||||
than they were typed, on a database with real users on it.
|
|
||||||
|
|
||||||
**The scroll path was driven, and it holds.** Three prepends on the 602-action fixture,
|
|
||||||
60 actions and ~16,200 px each. The same DOM node stayed at viewport top 792 → 787 — a
|
|
||||||
**5 px drift** across the prepend — and the view stayed 48,174 px from the bottom, so PR
|
|
||||||
#2's throw-to-the-end does not reproduce. Console clean. One note for anyone measuring it
|
|
||||||
again: the fixture's prose repeats, so an anchor found by matching *text* lands on an
|
|
||||||
older copy of the same sentence and reads as a huge jump. Hold the DOM node.
|
|
||||||
|
|
||||||
**Still open, deliberately:** no vitest + jsdom harness. The verify line offers
|
|
||||||
hand-driving *or* the harness and this took the first. The harness remains the thing that
|
|
||||||
would catch a prepend regression without a person in the loop, and jsdom's lack of layout
|
|
||||||
means it would not have settled the 5 px question either way.
|
|
||||||
|
|
||||||
**One user-visible change nobody asked for, recorded here because a script
|
|
||||||
author would otherwise find it by being surprised.** Moving `_history` onto
|
|
||||||
`context_history.story_actions` fixed the branch bug it was there to fix, and
|
|
||||||
carried a second change with it: `story_actions` drops blank-text rows, which
|
|
||||||
`adventure.actions` did not. So a user script's `history` array and
|
|
||||||
`info.actionCount` both got shorter for any adventure holding one. It is the
|
|
||||||
right shape — a textless row is bookkeeping with no AI Dungeon counterpart, and
|
|
||||||
the prompt never included one — but a script that fires "every N actions" now
|
|
||||||
fires on different turns. Not compatible with both readings; this is the one
|
|
||||||
chosen.
|
|
||||||
|
|
||||||
### SP8 — Drop the legacy columns
|
|
||||||
|
|
||||||
Only once the tree is proven live. Migration drops `index`, `variants`, `variant_index`,
|
|
||||||
`variant_count`, followed by `VACUUM FULL actions;`. **Also `adventures.memory_cursor`
|
|
||||||
and `summary_cursor`** — unread since SP3, kept only so a rolled-back build resumes from
|
|
||||||
a real number. They are on `adventures`, so dropping them costs no vacuum.
|
|
||||||
|
|
||||||
**And `actions.state_before` / `world_state_before`**, unwritten and unread since SP4 for
|
|
||||||
the same reason: a rolled-back build still finds a real snapshot on every row it wrote
|
|
||||||
itself. They are deferred JSON on `actions`, so they go in the same rewrite as `index`
|
|
||||||
and cost nothing extra.
|
|
||||||
|
|
||||||
`variant_count` and `variant_index` are the two to check before dropping: SP4 left them
|
|
||||||
as a maintained cache of the sibling group's shape, because the pager reads both for
|
|
||||||
every row of a page and must not pay a query per turn to get them. They are dead only
|
|
||||||
once SP7's tree view has replaced the pager.
|
|
||||||
|
|
||||||
**Verify:** full suite; egress ceilings; a measured before/after size, aggregates only.
|
|
||||||
|
|
||||||
### SP9 — Takes, not chips: one pager, and a fork that makes a new take
|
|
||||||
|
|
||||||
**Why this exists.** SP7 was driven by hand and the tree was unusable. Three things were
|
|
||||||
wrong, and only the third is a bug:
|
|
||||||
|
|
||||||
* **A chip did two different things.** At the tip it switched; above the tip it only
|
|
||||||
*previewed*, and taking it needed a second button. One control, two meanings, and the
|
|
||||||
meaning depended on where the player was standing.
|
|
||||||
* **Nothing could fork but an AI turn that already had a second take.**
|
|
||||||
`POST /actions/{id}/fork` answers 400 when the turn has one take, so a player's own
|
|
||||||
message had no way to become anything else and "branch from here" did not exist.
|
|
||||||
* **Retry only worked on the newest action.** Mid-story there was no retry at all.
|
|
||||||
|
|
||||||
**The model, in the player's words.** Every action can gain another *take*. On an AI node
|
|
||||||
that means regenerate; on the player's own it means type something else. Stepping between
|
|
||||||
takes is free — `3/3` to `1/3` is navigation, and the story below simply empties, because
|
|
||||||
that take has no children yet. **A branch is created when you write below a take that is
|
|
||||||
not the live one**, never before.
|
|
||||||
|
|
||||||
That collapses SP5's fork and SP4's retry into one operation and deletes the
|
|
||||||
tip-versus-past distinction from the UI entirely. It survives only in the implementation,
|
|
||||||
where it decides whether a write needs a branch at all.
|
|
||||||
|
|
||||||
**Takes are grouped by parent, not by coordinate.** This is the load-bearing change.
|
|
||||||
`attempts.group()` filters `branch_id == … AND depth == …`, and the player's own example
|
|
||||||
breaks it:
|
|
||||||
|
|
||||||
```
|
|
||||||
B ── C C1 C2 <- three takes, one parent (B)
|
|
||||||
│ └── D1' D2' <- two takes, parent C2
|
|
||||||
└── D1 D2 D3 <- three takes, parent C1
|
|
||||||
```
|
|
||||||
|
|
||||||
Standing on the C2 path at that depth must read `2/2`, not `5`. Coordinate grouping gets
|
|
||||||
that right by accident — writing under a non-live take forks, so the two sets land on
|
|
||||||
different branches. It gets `C` wrong: once C is forked onto its own branch it is alone
|
|
||||||
at its coordinate and reads `1/1`, losing C1 and C2 from a pager that must still say
|
|
||||||
`1/3`.
|
|
||||||
|
|
||||||
**Decision: add `actions.parent_id`.** The alternative — making a branch's fork point a
|
|
||||||
*node* instead of a depth, so a promoted take never moves — was rejected. `lineage` exists
|
|
||||||
precisely so a read is an OR-clause per branch rather than a walk up parent pointers, and
|
|
||||||
re-pointing the fork at a node changes path resolution itself, which drags in `cursors`,
|
|
||||||
memory depths and both bundle formats. `parent_id` is read only to group a turn's takes:
|
|
||||||
one indexed lookup, never a walk, and nothing about how a path resolves changes. Per SP2's
|
|
||||||
sizing note an integer beside `depth` is cheap, and the backfill rewrites a heap measured
|
|
||||||
at 1.7 MB on 2026-08-18.
|
|
||||||
|
|
||||||
**`variant_count` / `variant_index` are reprieved, not revived.** SP8 was going to drop
|
|
||||||
both; the pager needs the group's shape again. It needs it *per parent*, which is not what
|
|
||||||
either column caches, so SP8 still drops them and SP9 computes the shape from `parent_id`.
|
|
||||||
|
|
||||||
**Verify:** the SP0 baseline passes unmodified — a linear story has one take per parent,
|
|
||||||
and none of this is reachable without a second one. Plus: `2/2` under one take while its
|
|
||||||
sibling holds `3/3`; a pager that still reads `1/3` after its take has been forked onto its
|
|
||||||
own branch; a write below a non-live take forks exactly once; a fork on a player's own
|
|
||||||
message generates a reply.
|
|
||||||
|
|
||||||
**Owed:** a migration adding one column, and a `VACUUM FULL actions;` after it.
|
|
||||||
|
|
||||||
## Standing constraints
|
|
||||||
|
|
||||||
- **No production data is read at any point in this phase.** Migrations, the e2e
|
|
||||||
baseline and every egress measurement run on local SQLite and the synthetic `--keep`
|
|
||||||
fixture. If a real Postgres is ever needed for a write path, it is a throwaway
|
|
||||||
database whose name contains `stress`/`scratch`, and it is asked for first.
|
|
||||||
- **Any migration that rewrites `actions` is followed by `VACUUM FULL actions;`** on the
|
|
||||||
direct endpoint, not `-pooler`. SP1, SP4 and SP8 each rewrite every row. **As of
|
|
||||||
2026-08-18 two are owed** (SP1's and SP4's) and neither has been deployed; one vacuum
|
|
||||||
after the SP4 deploy settles both.
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
# Pokémon League Championship demo: playtest handover
|
|
||||||
|
|
||||||
Read this before you resume work on the demo scenario. It covers the current
|
|
||||||
state of `05-league-championship.json`, what a live 10-turn playtest on
|
|
||||||
production confirmed, and two bugs the playtest found.
|
|
||||||
|
|
||||||
**Last updated: 2026-08-28.**
|
|
||||||
|
|
||||||
> **Superseded in part on 2026-08-28.** Both bugs below were investigated against the
|
|
||||||
> real data and **both root causes named here are wrong**. See
|
|
||||||
> `plan/16-world-state-refusals.md` for what was actually happening and what was
|
|
||||||
> changed. The playtest record and the "Confirmed working" section still stand.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Where things stand
|
|
||||||
|
|
||||||
The scenario is live at `https://ai-dnd-1gmp.onrender.com` as
|
|
||||||
**"[Demo] League Championship: Round One"**, seeded from
|
|
||||||
`backend/app/seed_data/05-league-championship.json`. It replaced an earlier,
|
|
||||||
weaker draft titled "Road to the Champion" — that old scenario and its stale
|
|
||||||
test adventure (adventure id 42) are still in the production database. Delete
|
|
||||||
them by hand from `/scenarios` and `/adventures` when convenient; a delete
|
|
||||||
click froze the browser tab during this session behind what looked like a
|
|
||||||
native confirm dialog, so budget time for that if you try again.
|
|
||||||
|
|
||||||
The schema nests all five of the player's Pokémon and Milo's Pokémon under
|
|
||||||
`npcs`, not flattened into `player.<name>_hp` fields. Each npc entry carries
|
|
||||||
its own `stats` map (`hp`, `status`, and for Milo, `active_pokemon`,
|
|
||||||
`active_hp`, `active_status`, `pokemon_left`). `player.active_pokemon` is a
|
|
||||||
text stat that names whichever of the player's Pokémon is currently out. This
|
|
||||||
design survived a real playtest: see "Confirmed working" below.
|
|
||||||
|
|
||||||
Settings on the test account now point at the user's own OpenRouter key,
|
|
||||||
endpoint `https://openrouter.ai/api/v1`, model `deepseek/deepseek-v4-flash-0731`,
|
|
||||||
reasoning budget `-1`. The shared demo key's model
|
|
||||||
(`google/gemma-4-26b-a4b-it:free`) was hitting persistent 429s from OpenRouter
|
|
||||||
capacity, not from the app's own rate limiter — switch back to it only after
|
|
||||||
confirming that model isn't still rate-limited.
|
|
||||||
|
|
||||||
## Confirmed working: a live 10-turn playtest
|
|
||||||
|
|
||||||
Adventure id 43, played turn by turn against DeepSeek V4 Flash on production.
|
|
||||||
Milo's Graveler and Onix both fainted; Kabutops came out third. Across ten
|
|
||||||
turns:
|
|
||||||
|
|
||||||
- HP tracked correctly on both sides, including sandstorm chip damage each
|
|
||||||
turn once `sandstorm_active` flipped on.
|
|
||||||
- `status` stayed correctly independent per Pokémon (all `none` throughout
|
|
||||||
this run — no status move was tried).
|
|
||||||
- `player.active_pokemon` and `npc.milo.active_pokemon` both switched
|
|
||||||
correctly as Pokémon were sent out or fainted (Pidgeotto → Wartortle →
|
|
||||||
Machoke; Graveler → Onix → Kabutops).
|
|
||||||
- `player.potions` decremented correctly on use (3 → 2) and the healed
|
|
||||||
Pokémon's HP rose by the expected amount.
|
|
||||||
- The World State sidebar reflected every one of these changes live, without
|
|
||||||
a manual refresh, after the model's reply finished streaming.
|
|
||||||
|
|
||||||
This confirms the nested-`npcs` redesign from earlier in the session was the
|
|
||||||
right fix for "why did you flatten then" — parallel entities with independent
|
|
||||||
stats work as npcs, not as flattened player fields.
|
|
||||||
|
|
||||||
## Two bugs the playtest found
|
|
||||||
|
|
||||||
**1. The model sometimes skips the trailing `state` block entirely.**
|
|
||||||
|
|
||||||
> **Unconfirmed.** Truncation at `max_output_tokens` removes the block too, and it
|
|
||||||
> was never ruled out here. See `plan/16`.
|
|
||||||
On the very first turn of this run, DeepSeek V4 Flash narrated a Wartortle HP
|
|
||||||
drop but never appended the ` ```state ` block the engine parses. The engine
|
|
||||||
correctly left the state untouched — this is model non-compliance, not an
|
|
||||||
engine bug — but the drop was silent: no error, no visible sign in the UI
|
|
||||||
beyond "the numbers didn't move." A `Retry` on that same turn produced the
|
|
||||||
delta correctly. Confirmed by reading the raw action row over
|
|
||||||
`/api/adventures/{id}/actions` — `hasState` came back `false` on the first
|
|
||||||
attempt and `true` on the retry. If this happens often during your own play,
|
|
||||||
it is worth an authors'-note reminder or a stronger trailing-instruction
|
|
||||||
nudge in `engine.py`'s prompt scaffolding, not a schema change.
|
|
||||||
|
|
||||||
**2. The model reliably forgets `npc.milo.pokemon_left` and milestones on a
|
|
||||||
faint, despite an explicit instruction to update both.**
|
|
||||||
|
|
||||||
> **Wrong on both halves.** The model emitted `pokemon_left` at *both* faints; the
|
|
||||||
> engine clamped it to nothing and reported it as applied. And it never emitted a
|
|
||||||
> milestone because the milestone ids were absent from the prompt entirely. Neither
|
|
||||||
> was an attention problem. See `plan/16`. `ai_instructions` in
|
|
||||||
the scenario file already says: *"decrement `npc.milo.pokemon_left` when one
|
|
||||||
of his faints"* and *"Mark milestones as they happen."* Across two separate
|
|
||||||
faints in this session (Graveler, then Onix), the model correctly reset
|
|
||||||
`active_pokemon`/`active_hp`/`active_status` for the incoming Pokémon every
|
|
||||||
time, but never once touched `pokemon_left` (stuck at `3/3` through both
|
|
||||||
faints) and never checked off "Knock out Milo's lead Graveler," even though
|
|
||||||
that milestone was unambiguously satisfied. This looks like the faint
|
|
||||||
instruction is buried inside a longer bulleted list the model is only
|
|
||||||
partially attending to. Worth trying: pull the faint-handling instructions
|
|
||||||
into their own short paragraph, or add a stat-guide line for `pokemon_left`
|
|
||||||
and the milestones that makes them as visually prominent as `hp`/`status`.
|
|
||||||
|
|
||||||
**Separately, not necessarily a bug:** `world.turn` (a `counter` stat defined
|
|
||||||
in the schema) stayed at `0` for all ten turns. `ai_instructions` never tells
|
|
||||||
the model to increment it — the instructions cover HP, status, potions,
|
|
||||||
active_pokemon, and pokemon_left, but not turn. If you want the counter to
|
|
||||||
mean something, add an explicit line telling the model to bump
|
|
||||||
`world.turn` by 1 every reply.
|
|
||||||
|
|
||||||
## Suggested next steps
|
|
||||||
|
|
||||||
1. Decide whether to patch `ai_instructions` for the two gaps above, then
|
|
||||||
redeploy and play a few more turns to confirm faints correctly decrement
|
|
||||||
`pokemon_left` and flip milestones.
|
|
||||||
2. Clean up the stale "Road to the Champion" scenario and adventure 42.
|
|
||||||
3. Try a status-condition move (Ivysaur's Poison Powder or similar) — this
|
|
||||||
playtest never exercised the `status` stat changing away from `none`, so
|
|
||||||
it is unverified in practice even though the schema supports it.
|
|
||||||
4. If DeepSeek keeps skipping state blocks more than rarely, consider the
|
|
||||||
trailing-reminder wording in `engine.py` (`build_state_reminder` or
|
|
||||||
equivalent) rather than switching models — the schema itself is sound.
|
|
||||||
@@ -1,343 +0,0 @@
|
|||||||
# World-state refusals: what the engine throws away, and who gets told
|
|
||||||
|
|
||||||
Read this before you play the Pokémon demo again. It records why two bugs in
|
|
||||||
`plan/15-pokemon-demo-handover.md` were diagnosed wrongly, what the engine was
|
|
||||||
actually doing, and what changed. Everything here is merged and green. It **has** now been driven in a browser;
|
|
||||||
read "Driven in a browser" at the end first, because three of the five changes
|
|
||||||
did not work and one bug explains all three.
|
|
||||||
|
|
||||||
**Last updated: 2026-08-28.**
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## The one sentence version
|
|
||||||
|
|
||||||
`apply_delta` records three outcomes for every change the model sends —
|
|
||||||
`applied`, `clamped`, `rejected` — and everything downstream read only
|
|
||||||
`applied`. A refused change therefore reached the player as an ordinary chip and
|
|
||||||
reached the model, on the next turn, as a change that had succeeded.
|
|
||||||
|
|
||||||
## What was actually wrong
|
|
||||||
|
|
||||||
`plan/15` recorded two bugs and named a cause for each. Both causes were wrong,
|
|
||||||
and the investigation is worth keeping because the same reasoning trap is easy
|
|
||||||
to repeat: **the visible evidence was "the number did not move", and the natural
|
|
||||||
reading of that is that the model never tried.**
|
|
||||||
|
|
||||||
### `pokemon_left` was emitted every time
|
|
||||||
|
|
||||||
Adventure 43's action rows carry `milo pokemon_left` in `world_changes` at both
|
|
||||||
faints, each with `"delta": 0, "value": 3`. The model saw the faint and wrote
|
|
||||||
the path. It was not forgetting anything.
|
|
||||||
|
|
||||||
`old == new == 3` is reachable only from a **positive** value. `pokemon_left`
|
|
||||||
was `min 0, max 3, initial 3, max_delta_per_turn 1`, so `+2` capped to `+1`,
|
|
||||||
reached 4, and clamped back to the ceiling of 3. Net zero.
|
|
||||||
|
|
||||||
So the model sent the remaining count as an absolute — "two left" — instead of
|
|
||||||
a delta of `-1`. Milo's four stats alternate between the two conventions:
|
|
||||||
|
|
||||||
| stat | convention |
|
|
||||||
|---|---|
|
|
||||||
| `active_pokemon` | text, absolute |
|
|
||||||
| `active_hp` | number, delta |
|
|
||||||
| `active_status` | text, absolute |
|
|
||||||
| `pokemon_left` | number, delta |
|
|
||||||
|
|
||||||
HP survives because damage is naturally phrased as a change. A count is
|
|
||||||
naturally phrased as a state, so it got text semantics.
|
|
||||||
|
|
||||||
**The general rule this produces:** a numeric stat whose `initial` equals the
|
|
||||||
boundary it moves away from turns every wrong-signed change into a silent
|
|
||||||
no-op. Every `hp` in the scenario has that shape (`initial == max`). It has
|
|
||||||
never fired only because damage is phrased as a decrease by luck of language.
|
|
||||||
|
|
||||||
### Milestones were never emitted at all
|
|
||||||
|
|
||||||
Zero milestone changes across nine AI turns. `EMIT_RULE` asks for
|
|
||||||
`"milestones.<id>": true` and `apply_delta` matches `<id>` against the schema
|
|
||||||
key, but `render_state_section` printed only the description, and
|
|
||||||
`render_reference` skipped the milestones section entirely
|
|
||||||
(`STAT_SECTIONS = ("world", "player")`). The string `graveler_defeated` was
|
|
||||||
nowhere in the prompt.
|
|
||||||
|
|
||||||
The same playtest is its own control: `sandstorm_active` is a flag, flags
|
|
||||||
*are* printed by name, and it worked.
|
|
||||||
|
|
||||||
### The replay was teaching the model to repeat itself
|
|
||||||
|
|
||||||
Found while deciding whether to feed refusals forward, and the most damaging of
|
|
||||||
the three. `_history_text` re-attached each past turn's state block from
|
|
||||||
`world_delta["delta"]` — **what the model sent**, not what was applied. So the
|
|
||||||
turn after the faint contained the model's own block claiming
|
|
||||||
`"npc.milo.pokemon_left": 2`, directly above a live values line reading
|
|
||||||
`pokemon_left 3/3`, with nothing to say which was true.
|
|
||||||
|
|
||||||
That is a per-turn lesson that sending `2` is correct. The identical mistake at
|
|
||||||
the second faint is what that lesson predicts.
|
|
||||||
|
|
||||||
## What changed
|
|
||||||
|
|
||||||
Five changes, on `fix-silent-clamps-and-milestone-ids`.
|
|
||||||
|
|
||||||
1. **`Action.world_changes` reports refusals** (`models.py`). Reads `clamped`
|
|
||||||
and `rejected` beside `applied`. Accepted stats carry `clamped`; refusals
|
|
||||||
become `kind: "rejected"` entries. The `fix` key is present only when the
|
|
||||||
engine wrote one — it is empty for every accepted change, and this property
|
|
||||||
runs for every action of every list response.
|
|
||||||
2. **The UI distinguishes three outcomes** (`Play.jsx`, `index.css`). Clamped to
|
|
||||||
a standstill reads `no change — at its limit` on a dashed chip; a partial
|
|
||||||
clamp is marked `(limited)`; a rejection carries its reason. Dashed and
|
|
||||||
dimmed rather than red: the rules refusing a change is them working.
|
|
||||||
3. **Milestones are named to the model** (`engine.py`). The goals line is now
|
|
||||||
`Goals (mark with milestones.<id>): graveler_defeated — Knock out Milo's lead
|
|
||||||
Graveler; …`, the same treatment NPCs get with `(npc.milo)`.
|
|
||||||
4. **Refusals carry a generated correction** (`engine.py`). Each rejection, and
|
|
||||||
each clamp that moved nothing, builds a `fix` string from the stat definition
|
|
||||||
at the point of refusal, so it quotes the real limits and lists the real
|
|
||||||
names. `render_refusals()` renders them into the prompt directly above
|
|
||||||
`EMIT_REMINDER`, for the previous AI turn only.
|
|
||||||
5. **History replays what was accepted** (`builder.py`). `applied_delta()`
|
|
||||||
rebuilds the block from `report["applied"]`, dropping any numeric entry where
|
|
||||||
`new == old` so a change that moved nothing cannot be copied as a zero.
|
|
||||||
|
|
||||||
Plus the demo scenario: `pokemon_left` became `pokemon_fainted`
|
|
||||||
(`type: counter`, `initial: 0`), which puts a wrong sign on the counter rule
|
|
||||||
where it is rejected out loud instead of absorbed. The faint instruction moved
|
|
||||||
into its own paragraph, and a `world.turn` line was added — it sat at 0 for the
|
|
||||||
whole playtest because nothing ever told the model to move it.
|
|
||||||
|
|
||||||
### The design call worth not re-litigating
|
|
||||||
|
|
||||||
**A clamp that reduced a change but still moved the value says nothing.** Only
|
|
||||||
total losses are reported. If you tell a model its 80 damage became 30, it can
|
|
||||||
treat the shortfall as a debt and send the remaining 50 next turn — which is the
|
|
||||||
swing `max_delta_per_turn` exists to prevent. A rejection has no partial credit
|
|
||||||
to chase. `test_a_clamp_that_still_moved_the_value_says_nothing` pins this.
|
|
||||||
|
|
||||||
## How to test it
|
|
||||||
|
|
||||||
532 backend tests pass and the frontend builds. **The UI work has no automated
|
|
||||||
cover** — this project has no frontend test runner, which is the standing
|
|
||||||
reason its UI bugs are found by hand.
|
|
||||||
|
|
||||||
Run the backend from `backend/` with
|
|
||||||
`.venv/Scripts/python.exe -m pytest tests/`. The new file is
|
|
||||||
`tests/test_change_visibility.py` (23 tests). Each of the three mechanisms fails
|
|
||||||
its own test when disabled; that was checked by sabotage, not assumed.
|
|
||||||
|
|
||||||
To drive it, re-seed the scenario and play the demo:
|
|
||||||
|
|
||||||
1. **A refusal chip.** Open the World State drawer, use its ✎ edit mode to put
|
|
||||||
Milo's `active_hp` at full, then play a turn where he takes no damage but the
|
|
||||||
model tries to heal him. Easier and more reliable: send a deliberately wrong
|
|
||||||
block by editing an AI turn. What you are looking for is a dashed chip
|
|
||||||
reading `no change — at its limit`, not a `+0`.
|
|
||||||
2. **The milestone.** Knock out Graveler. `graveler_defeated` should tick, and
|
|
||||||
a `✓ graveler defeated` chip should appear. This is the single clearest
|
|
||||||
pass/fail in the whole change — it never once happened before.
|
|
||||||
3. **The faint counter.** At the same faint, `pokemon_fainted` should go 0 → 1.
|
|
||||||
If the model sends an absolute again, it is now refused rather than absorbed,
|
|
||||||
and the refusal note should appear in the *next* turn's prompt. Read it under
|
|
||||||
Insights → the turn's context snapshot, section `world_state_refusals`.
|
|
||||||
4. **The replay.** In the same snapshot, check the replayed history: a past
|
|
||||||
turn's `state` block should carry only the changes that were accepted.
|
|
||||||
5. **`world.turn`** should now advance by 1 per reply.
|
|
||||||
|
|
||||||
Check the narrow layout too. The chips grew longer text, and `.chg` is inside
|
|
||||||
the story column with nothing to scroll sideways — `overflow-wrap: anywhere` is
|
|
||||||
doing the work, and it was not re-checked at 390 px. Chrome clamps its minimum
|
|
||||||
window width to ~500 px, so relaunch with `--window-size=` rather than trying to
|
|
||||||
resize a maximized window.
|
|
||||||
|
|
||||||
## Still open
|
|
||||||
|
|
||||||
- **The missing `state` block from `plan/15` bug 1 is unexplained.** Truncation
|
|
||||||
at `max_output_tokens` removes the block, which `LENGTH_HEADROOM` exists to
|
|
||||||
prevent, and it was never ruled out. The distinguishing evidence is whether
|
|
||||||
the narration ends mid-sentence with `finish_reason: length`.
|
|
||||||
- **Every `hp` stat still has the `initial == max` shape.** Now visible when it
|
|
||||||
bites, rather than silent, but not designed out.
|
|
||||||
- ~~**The stale "Road to the Champion" scenario and adventure 42** are still on
|
|
||||||
production. Deleting them is hand-work and was deliberately not automated.~~
|
|
||||||
Automated on 2026-08-28: `seed.py` now deletes seeded scenarios no seed file
|
|
||||||
claims. Adventure 42 survives with a NULL `scenario_id`, and losing the cover
|
|
||||||
art is the whole cost.
|
|
||||||
- **The Bandit Camp demo (`04-rpg-world-state.json`) was not checked** for the
|
|
||||||
same milestone problem. Its milestones were equally unnamed to the model
|
|
||||||
before this change, so it is worth asking whether one has ever fired there.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Driven in a browser, 2026-08-28
|
|
||||||
|
|
||||||
Adventure 45, four turns, on production against the demo model. Two of the five
|
|
||||||
changes worked. Three did not reach anyone, for one reason.
|
|
||||||
|
|
||||||
### The bug: the stored column dropped two of the three lists
|
|
||||||
|
|
||||||
`world_delta_of` in `routers/adventures.py` wrote `delta` and `applied` only.
|
|
||||||
Every consumer that distinguishes outcomes reads the other two:
|
|
||||||
|
|
||||||
- `Action.world_changes` builds `clamped_paths` from `world_delta["clamped"]`,
|
|
||||||
so every chip carried `clamped: false`. `Play.jsx`'s `blocked = c.clamped &&
|
|
||||||
d === 0` could never be true, and `(limited)` could never render.
|
|
||||||
- With no `rejected` list, a `kind: "rejected"` chip was unreachable.
|
|
||||||
- `worldstate.refusals` reads both lists, so `render_refusals` always returned
|
|
||||||
an empty string and no correction ever reached the next prompt.
|
|
||||||
|
|
||||||
The `fix` string survived only because the engine stores it inside the `applied`
|
|
||||||
entry. Turn 3 is the record: the snapshot's `report.clamped` held
|
|
||||||
`npc.ivysaur.hp` and `npc.milo.active_hp` with correct `fix` text, both chips
|
|
||||||
came back `clamped: false`, turn 4's prompt contained no correction, and the
|
|
||||||
model repeated the same mistake.
|
|
||||||
|
|
||||||
`world_delta_of` now carries all three lists.
|
|
||||||
|
|
||||||
**Why the 21 tests passed.** The `action()` helper in
|
|
||||||
`test_change_visibility.py` built the column by hand as `{"delta": delta,
|
|
||||||
**report}`, with every list present. The write path was never exercised. The
|
|
||||||
helper now fills the column through `world_delta_of`. Removing the two lines
|
|
||||||
again fails 9 of the 23 tests; that was checked, not assumed.
|
|
||||||
|
|
||||||
### The two that were not code faults
|
|
||||||
|
|
||||||
`graveler_defeated` never fired and `world.turn` never moved. Both instructions
|
|
||||||
are present and correct in the assembled prompt: the goals line reads `Goals
|
|
||||||
(mark with milestones.<id>): graveler_defeated — …`, and the scenario says to
|
|
||||||
add 1 to `world.turn` every reply. The demo model ignores both. Change 3
|
|
||||||
landed; the model is the limit.
|
|
||||||
|
|
||||||
### The absolutes were coming from the scenario's own wording
|
|
||||||
|
|
||||||
The model sent a total, not a change, for almost every number:
|
|
||||||
`npc.ivysaur.hp: 96`, `npc.milo.active_hp: 65` then `88`, `player.potions: 2`.
|
|
||||||
Because every `hp` has `initial == max`, each one clamped back to where it
|
|
||||||
started. `potions` went **up** when the player spent one.
|
|
||||||
|
|
||||||
`EMIT_RULE` does say "CHANGES ONLY, as deltas (not new totals)", and
|
|
||||||
`EMIT_REMINDER` repeats "deltas only". The scenario contradicted both at closer
|
|
||||||
range. `milo.active_hp.desc` said "**Reset this to** the newcomer's full HP",
|
|
||||||
which is an instruction to send an absolute, and that desc is injected every
|
|
||||||
turn. The bullets said "**Drop** the HP … and **raise** it", naming a direction
|
|
||||||
but never a sign. The only line that said "(not a delta)" was
|
|
||||||
`player.active_pokemon`, so naming the exception made the rule look optional.
|
|
||||||
|
|
||||||
The one stat whose desc used delta wording, `pokemon_fainted` ("Add 1 each
|
|
||||||
time"), is the one that worked.
|
|
||||||
|
|
||||||
Fixed in `05-league-championship.json`: every `hp` desc and the potion desc now
|
|
||||||
state the sign, the bullets do too, and the lead-in says plainly that every
|
|
||||||
number is a change with a worked example. `milo.active_hp.max_delta_per_turn`
|
|
||||||
went 65 → 98, because a switch legitimately moves that stat a full bar and the
|
|
||||||
old cap made the reset unreachable in one turn.
|
|
||||||
|
|
||||||
**Not fixed:** the `initial == max` shape itself. It is now loud rather than
|
|
||||||
silent, and the wording removes the usual cause, but the shape is still there.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Driven against Claude, 2026-08-28
|
|
||||||
|
|
||||||
The four turns above ran on the free demo model, which ignored two instructions
|
|
||||||
that were present and correct in the prompt. To separate model behavior from
|
|
||||||
code, the same demo was played again through `backend/tools/claude_shim.py`, an
|
|
||||||
OpenAI-compatible endpoint backed by the local `claude` command line tool. The
|
|
||||||
model was `sonnet`. See the README for how to run it.
|
|
||||||
|
|
||||||
Every one of the five changes worked:
|
|
||||||
|
|
||||||
| Turn | Chips |
|
|
||||||
|---|---|
|
|
||||||
| 1 | `turn +1`, `active pokemon → Wartortle`, `wartortle hp -8`, `milo active hp -46`, dashed `type advantage used refused — no such flag` |
|
|
||||||
| 2 | `turn +1`, `milo active hp -52`, `milo pokemon fainted +1`, `milo active pokemon → Onix`, `✓ graveler defeated` |
|
|
||||||
| 3 | `turn +1 (limited)`, dashed `milo active hp no change — at its limit` |
|
|
||||||
| 4 | `turn +1`, `milo active hp +32`, `✓ type advantage used` |
|
|
||||||
|
|
||||||
`graveler_defeated` fired for the first time in five playtests, `world.turn`
|
|
||||||
moved every turn, `pokemon_fainted` went 0 to 1 at the faint, and every HP number
|
|
||||||
was a signed change rather than a total. The two failures left open above were
|
|
||||||
the demo model, not the instructions.
|
|
||||||
|
|
||||||
**The refusal loop is verified end to end.** Turn 3 clamped to nothing. Turn 4's
|
|
||||||
assembled prompt, read from `GET /api/adventures/1/actions/9/context`, carried
|
|
||||||
the correction verbatim:
|
|
||||||
|
|
||||||
```
|
|
||||||
[Part of your last state block was not applied. Correct it in this turn's block:
|
|
||||||
- `npc.milo.active_hp` did not move. It is already at its minimum of 0 (it runs
|
|
||||||
from 0 to 98; it moves at most 98 per turn).]
|
|
||||||
```
|
|
||||||
|
|
||||||
The model's next delta was `{"world.turn": 1, "npc.milo.active_hp": 32,
|
|
||||||
"milestones.type_advantage_used": true}`. That is the loop the whole change
|
|
||||||
exists for, and it had never been observed running.
|
|
||||||
|
|
||||||
### The one bug that is still a schema fault
|
|
||||||
|
|
||||||
At the faint the model set `npc.milo.active_pokemon` to `Onix` and sent no
|
|
||||||
positive `active_hp` change, so Onix arrived on the field at 0 of 98 and every
|
|
||||||
later hit was refused. Sonnet followed the rest of the scenario closely, so this
|
|
||||||
is the schema rather than the model: `active_hp` is one stat with one `max` of
|
|
||||||
98, shared by Graveler at 98, Onix at 90, and Kabutops at 88. A switch has to
|
|
||||||
raise it, and nothing in the schema can raise it on the referee's side.
|
|
||||||
|
|
||||||
The fix is to give Milo's three Pokemon their own NPC entries, which also removes
|
|
||||||
the `initial == max` shape from this demo. Not done: the session's remaining time
|
|
||||||
went to the guest starter instead.
|
|
||||||
|
|
||||||
### Cost
|
|
||||||
|
|
||||||
About $0.04 a turn, billed against the Claude subscription rather than a card.
|
|
||||||
Roughly 13k of each request's 20k prompt tokens is the command line tool's own
|
|
||||||
overhead; the app's prompt is about 7k.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## What shipped alongside it, 2026-08-28
|
|
||||||
|
|
||||||
**A permanent local test rig.** `backend/tools/claude_shim.py` is now in the
|
|
||||||
repo. It finds the `claude` binary through `AIDND_CLAUDE_BIN`, then `PATH`, then
|
|
||||||
the per-user install, and takes `--port` and `--claude`.
|
|
||||||
|
|
||||||
**The demo says Pokemon in its title, and has a Pokeball for cover art.**
|
|
||||||
`05-league-championship.json` is now `[Demo] Pokemon League Championship: Round
|
|
||||||
One`. Renaming a seed exposed a fault: `seed.py` matches a seed to its row by
|
|
||||||
title, so a rename inserted a second public scenario and stranded the first,
|
|
||||||
which is the same failure this file records for "Road to the Champion". Seeds
|
|
||||||
now carry `previous_titles`, and `_find_renamed` lands the rename on the
|
|
||||||
existing row. The scenario kept its id, and no orphan appeared.
|
|
||||||
|
|
||||||
The cover art is a 3.2 kB PNG data URI. An SVG one was tried first and stored as
|
|
||||||
an empty `image_url`: `app/images.py` accepts raster formats only, on purpose,
|
|
||||||
because SVG can carry script and these bytes are served from the app's own
|
|
||||||
origin. `backend/tools/make_pokeball.py` draws the ball with `zlib` alone, so
|
|
||||||
regenerating it needs no image library.
|
|
||||||
|
|
||||||
**Seeded scenarios no seed file claims are deleted.** `previous_titles` stops a
|
|
||||||
rename stranding a row, but the rows already stranded still needed deleting by
|
|
||||||
hand on every deployment. `_sweep_unclaimed` removes them on the next boot. Only
|
|
||||||
a NULL owner with `is_public` is reachable, so nothing a player made can be
|
|
||||||
touched, and `adventures.scenario_id` is `ON DELETE SET NULL`, so an adventure
|
|
||||||
started from a deleted demo keeps its story and loses only the artwork. The
|
|
||||||
sweep is skipped when a seed file fails to parse, and an empty seed directory
|
|
||||||
never reaches it: neither reads as an instruction to delete live content.
|
|
||||||
|
|
||||||
**Every new guest gets the played adventure.** `app/starter.py` copies a shipped
|
|
||||||
export bundle into each new guest account, from the guest mint in
|
|
||||||
`routers/auth.py`. The bundle is this session's adventure trimmed to its first
|
|
||||||
two exchanges, which ends on the knockout and shows an applied change, a refused
|
|
||||||
one, and a milestone. It stops before the Onix bug above, and the state it
|
|
||||||
leaves has Onix at its own 90 HP so a guest can play on from it.
|
|
||||||
|
|
||||||
An adventure has no cover art of its own and inherits its scenario's, and a
|
|
||||||
bundle carries no scenario id, because an id is local to one database. The
|
|
||||||
starter file names its source under `scenarioTitle` instead, and
|
|
||||||
`starter._link_scenario` looks it up, so the copy shows the Pokeball rather than
|
|
||||||
a monogram tile.
|
|
||||||
|
|
||||||
The row building that `POST /adventures/import` did inline moved into
|
|
||||||
`bundle.materialize`, which both callers now use. The limit and rate checks
|
|
||||||
stayed in the endpoint: the starter writes a file the server ships, so it has no
|
|
||||||
untrusted list to cap. `tests/test_starter_adventure.py` covers the file, the
|
|
||||||
copy, the chips, the playable end state, and the two failure paths.
|
|
||||||
@@ -1,582 +0,0 @@
|
|||||||
# Phase 17: refactor for readability
|
|
||||||
|
|
||||||
The code works and it is tested. It is also hard to read, because four files hold
|
|
||||||
most of it, one schema migration is half finished, and the published guide is a
|
|
||||||
hand-maintained copy of another file. This phase fixes those three things without
|
|
||||||
changing what the app does.
|
|
||||||
|
|
||||||
## How to use this file
|
|
||||||
|
|
||||||
This file is both the plan and the running status. Update the progress table at
|
|
||||||
the end of every stage. Read the table first when you pick the work back up.
|
|
||||||
|
|
||||||
Each stage lands as its own commit on `refactor-17`. No stage starts until the
|
|
||||||
stage before it is green.
|
|
||||||
|
|
||||||
## Progress
|
|
||||||
|
|
||||||
| Stage | Work | Status | Landed |
|
|
||||||
|---|---|---|---|
|
|
||||||
| 0 | Hygiene: worktrees, branches, undocumented settings | done, except the branch deletion | 2026-08-29 |
|
|
||||||
| 1 | Split the four largest files | done: one test setup, and all four files split | 2026-08-29 |
|
|
||||||
| 2 | Remove duplication | done: all seven items | 2026-08-29 |
|
|
||||||
| 3 | SP8: drop the legacy columns | code done, all eight columns gone; the deploy and `VACUUM FULL` remain | 2026-08-29 |
|
|
||||||
| 4 | Documentation | not started | |
|
|
||||||
| 5 | A frontend test runner | not started | |
|
|
||||||
|
|
||||||
## Baseline
|
|
||||||
|
|
||||||
Record these numbers before you start. They are how you tell a refactor from a
|
|
||||||
rewrite.
|
|
||||||
|
|
||||||
- 549 backend tests pass in 103 seconds.
|
|
||||||
- `backend/app` holds 13,486 lines across 45 files.
|
|
||||||
- `backend/tests` holds 12,221 lines across 38 files.
|
|
||||||
- `frontend/src` holds 9,171 lines across 21 files.
|
|
||||||
- The app exposes 70 endpoints and applies 64 migrations on boot.
|
|
||||||
|
|
||||||
Run the suite with `cd backend && .venv/Scripts/python.exe -m pytest tests/ -q`.
|
|
||||||
|
|
||||||
## What the review found
|
|
||||||
|
|
||||||
### The code is clean at the statement level
|
|
||||||
|
|
||||||
A scan for unreferenced functions across `backend/app` returned only FastAPI route
|
|
||||||
handlers, which the decorator references rather than the name. A scan of
|
|
||||||
`index.css` found 5 unused class names out of 373, and every one of the 5 is either
|
|
||||||
CodeMirror's or built from a template string. There is no dead code to delete and
|
|
||||||
no `TODO` or `FIXME` anywhere in the tree.
|
|
||||||
|
|
||||||
Read that as a constraint. The gains in this phase come from moving code, not from
|
|
||||||
finding rot.
|
|
||||||
|
|
||||||
### Four files hold most of the complexity
|
|
||||||
|
|
||||||
| File | Lines | What it holds |
|
|
||||||
|---|---|---|
|
|
||||||
| `backend/app/routers/adventures.py` | 2353 | Nine concerns: the turn engine, branches, takes, import and export, adventure scripts, refresh from scenario, insights, memory CRUD, and action CRUD |
|
|
||||||
| `frontend/src/pages/Play.jsx` | 2280 | 27 components, plus a 722-line `Play()` holding 18 `useState` calls and 7 `useEffect` calls |
|
|
||||||
| `frontend/src/index.css` | 2865 | One stylesheet. The `max-width: 720px` block at line 2655 overrides rules written 2000 lines above it |
|
|
||||||
| `backend/app/worldstate/engine.py` | 918 | Four jobs: parse a delta, apply a delta, render the context sections, and instantiate a schema |
|
|
||||||
|
|
||||||
### The schema is half migrated
|
|
||||||
|
|
||||||
`plan/14-phase-story-tree.md` defines SP8, which drops the columns the tree
|
|
||||||
replaced. SP8 was gated on the tree running in production. It now does. Eight
|
|
||||||
columns are still written on every turn and read by nothing:
|
|
||||||
|
|
||||||
- `actions.index`, `variants`, `variant_index`, `variant_count`
|
|
||||||
- `actions.state_before`, `world_state_before`
|
|
||||||
- `adventures.memory_cursor`, `summary_cursor`
|
|
||||||
|
|
||||||
Until they go, `models.py` carries two vocabularies for one idea, and every reader
|
|
||||||
has to be told which one is live.
|
|
||||||
|
|
||||||
### The published guide is a hand-written copy
|
|
||||||
|
|
||||||
`docs/guide.html` is 84 KB of hand-written HTML covering the same material as the
|
|
||||||
63 KB `docs/GUIDE.md`. Nothing generates one from the other. They have already
|
|
||||||
drifted: the HTML last changed on 2026-08-20 and the Markdown on 2026-08-28, and
|
|
||||||
section 3.6, "Counting visits", exists only in the Markdown. GitHub Pages publishes
|
|
||||||
the HTML, so readers get the stale copy. `docs/architecture.html` is 87 KB of
|
|
||||||
hand-written HTML with no Markdown source at all.
|
|
||||||
|
|
||||||
### Duplication the tests already protect
|
|
||||||
|
|
||||||
- `worldstate.apply_delta` and `apply_override` each re-implement the same path
|
|
||||||
routing for `flags.`, `milestones.`, and `npc.<id>.`. That is six parallel
|
|
||||||
branches at `engine.py:573`, `587`, `617`, `675`, `697`, and `740`.
|
|
||||||
- 35 of the 38 test files repeat the same 8-line temporary-database prologue. There
|
|
||||||
is no `conftest.py`.
|
|
||||||
- `ScriptedProvider` or `FakeProvider` is defined 12 times, once per test file that
|
|
||||||
needs a fake model.
|
|
||||||
- The debounced autosave helper is copied into `Play.jsx:204`,
|
|
||||||
`ScenarioEditor.jsx:23`, and `ScriptEditor.jsx:22`.
|
|
||||||
- `get_adventure_or_404` is called by hand in about 20 handlers. It is a function,
|
|
||||||
not a dependency, so every handler repeats the call.
|
|
||||||
- `routers/chat.py:22` imports `SSE_HEADERS` and `sse` from `routers/adventures.py`.
|
|
||||||
One router reaches into another for shared plumbing.
|
|
||||||
|
|
||||||
### One setting is documented nowhere
|
|
||||||
|
|
||||||
`AIDND_TRUSTED_PROXY_HOPS` is read at `limits.py:55`. It does not appear in
|
|
||||||
`backend/.env.example`, `README.md`, or `render.yaml`. It sets how many proxy hops
|
|
||||||
`client_ip()` trusts, which is what stops a rotated `X-Forwarded-For` header from
|
|
||||||
buying a fresh rate-limit bucket. If a deployment adds a proxy hop and nobody sets
|
|
||||||
this variable, the bypass comes back silently.
|
|
||||||
|
|
||||||
The unprefixed `DATABASE_URL` that `database.py:24` also accepts turned out to be
|
|
||||||
documented already, inside the `AIDND_DATABASE_URL` entry in
|
|
||||||
`backend/.env.example`. No change needed there.
|
|
||||||
|
|
||||||
### `Settings.stream` is dead
|
|
||||||
|
|
||||||
`models.py:622` defines the column, and `schemas.py:452` and `499` expose it.
|
|
||||||
Nothing reads it in the backend or the frontend. This is item S1 in
|
|
||||||
`docs/self-review.md`.
|
|
||||||
|
|
||||||
### Local clutter
|
|
||||||
|
|
||||||
Everything here is gitignored, so it costs repository weight nothing. It costs disk
|
|
||||||
and attention.
|
|
||||||
|
|
||||||
- Two abandoned worktrees, each a full copy of the tree:
|
|
||||||
`.claude/worktrees/simplify-comments` and `.claude/worktrees/sp7-tree-ui`. Both
|
|
||||||
branches landed on `main` as squashes.
|
|
||||||
- 21 local branches. 13 read as unmerged to `git branch --no-merged`, and
|
|
||||||
`plan/STATUS.md` already records that they landed as squashes.
|
|
||||||
- Three stale SQLite files in `backend/`: `data.backup-2026-07-06.db`,
|
|
||||||
`data.backup-pre-phase8.db`, and `scroll_fixture.db`, 2.9 MB together. The live
|
|
||||||
local database, `backend/data.db`, is not one of them and is not touched.
|
|
||||||
|
|
||||||
## Stage 0: hygiene
|
|
||||||
|
|
||||||
No source changes. Do this first, so later stages run against a quiet tree.
|
|
||||||
|
|
||||||
1. Remove both worktrees with `git worktree remove`, then delete the branches that
|
|
||||||
landed as squashes. Verify each one first: a branch is safe to delete when
|
|
||||||
`git log main..<branch>` names only commits whose message already appears in
|
|
||||||
`git log main`.
|
|
||||||
2. Delete the three stale `.db` files. Leave `backend/data.db` alone.
|
|
||||||
3. Add `AIDND_TRUSTED_PROXY_HOPS` to `backend/.env.example`, with the reason it
|
|
||||||
exists. Add it to the deploy section of `README.md` too, because that is where
|
|
||||||
somebody putting the app behind another proxy looks.
|
|
||||||
|
|
||||||
**Check:** the suite still passes, and `git status` is clean.
|
|
||||||
|
|
||||||
### What Stage 0 actually did, 2026-08-29
|
|
||||||
|
|
||||||
Both worktrees are gone, which freed about 104 MB. Removing `sp7-tree-ui` needed one
|
|
||||||
extra step: a Vite dev server had been running out of that worktree since
|
|
||||||
2026-08-18, holding `frontend/.vite` open and owning port 5173. It was serving a tree
|
|
||||||
54 commits behind `main`. Stopping it freed the directory. Restart the real one with
|
|
||||||
`start.ps1`.
|
|
||||||
|
|
||||||
The three stale `.db` files are deleted. `backend/data.db` is untouched.
|
|
||||||
|
|
||||||
`AIDND_TRUSTED_PROXY_HOPS` is now in `backend/.env.example` and in the README deploy
|
|
||||||
section.
|
|
||||||
|
|
||||||
**Still owed.** The 19 squash-landed branches are still there. `git branch -D` is
|
|
||||||
blocked by the permission classifier, which is a reasonable guard on a destructive
|
|
||||||
command. All 19 are verified safe by the rule above. Run this to clear them:
|
|
||||||
|
|
||||||
```
|
|
||||||
git branch -D bugfix-code-review docs-story-tree fix-prepend-autoscroll \
|
|
||||||
fix-silent-clamps-and-milestone-ids fix-store-refusals-and-delta-wording \
|
|
||||||
handover-2026-08-17 measure-post-vacuum-sizes phase-14-story-tree \
|
|
||||||
phase-7-public-repo phase-8-accounts sp1-tree-schema sp10-memory-bank-eviction \
|
|
||||||
sp2-branch-clause sp3-node-cursors sp4-sibling-nodes sp5-fork-on-continue \
|
|
||||||
sp6-bundle-v2 sp7-tree-ui sp7b-take-pager worktree-keep-fixture-and-status \
|
|
||||||
worktree-simplify-comments
|
|
||||||
```
|
|
||||||
|
|
||||||
`worktree-keep-fixture-and-status` is the one that needed checking by hand. Its
|
|
||||||
commit subject appears nowhere in `main`, but the `--keep` flag it adds is on `main`
|
|
||||||
at `tools/stress_session.py:764`, along with both gotchas the message describes, at
|
|
||||||
`:686` and `:692`. Only the message differs.
|
|
||||||
|
|
||||||
The remote branches are left alone. Deleting those is a separate decision.
|
|
||||||
|
|
||||||
## Stage 1: split the four largest files
|
|
||||||
|
|
||||||
Every change in this stage moves code. None of it changes behavior. The 549 tests
|
|
||||||
are the check, and they must pass without being edited, except where this section
|
|
||||||
says otherwise.
|
|
||||||
|
|
||||||
### `routers/adventures.py` becomes a package
|
|
||||||
|
|
||||||
Split it into `backend/app/routers/adventures/`:
|
|
||||||
|
|
||||||
| Module | Holds | Source lines |
|
|
||||||
|---|---|---|
|
|
||||||
| `__init__.py` | The `APIRouter`, the shared dependencies, and re-exports | |
|
|
||||||
| `paging.py` | `ACTION_LIST_COLUMNS`, `ACTION_PAGE`, `action_window`, `annotate_takes`, `current_window` | 27-146, 1203-1261 |
|
|
||||||
| `crud.py` | List, create, get, patch, delete, plus the script-state and world-state readers | 225-580 |
|
|
||||||
| `turns.py` | The turn engine: `generate_turn`, `_generate_turn`, `run_player_turn`, the turn lock, and the SSE helpers | 581-1004 |
|
|
||||||
| `takes.py` | Retry, variants, takes, forking, and undo | 1005-1192, 1503-1778 |
|
|
||||||
| `branches.py` | The branch endpoints | 1193-1502 |
|
|
||||||
| `bundle_io.py` | Export and import | 1779-1837 |
|
|
||||||
| `scripts.py` | The per-adventure script endpoints | 1838-1941 |
|
|
||||||
| `refresh.py` | Refresh from scenario | 1942-2152 |
|
|
||||||
| `insights.py` | The context dry run and the per-action context | 2153-2189 |
|
|
||||||
| `memories.py` | Memory bank CRUD | 2190-2279 |
|
|
||||||
|
|
||||||
Target no file above 450 lines.
|
|
||||||
|
|
||||||
**The tests couple to the module, so read this before you start.** Twelve test
|
|
||||||
files call `monkeypatch.setattr(adventures, "OpenAICompatibleProvider", ...)`.
|
|
||||||
`monkeypatch` replaces a name in the module where the calling code looks it up, so
|
|
||||||
re-exporting from `__init__.py` does not keep those patches working. Once
|
|
||||||
`_generate_turn` lives in `turns.py`, the target becomes
|
|
||||||
`adventures.turns.OpenAICompatibleProvider`.
|
|
||||||
|
|
||||||
Do Stage 2's `conftest.py` work first if you want that to be a one-line change
|
|
||||||
instead of twelve. Otherwise retarget all twelve here. The other patched names are
|
|
||||||
`adventures.limits`, `adventures.check_demo_cap`, `adventures.generate_turn`, and
|
|
||||||
`adventures._active_turns`, each used once.
|
|
||||||
|
|
||||||
Keep these importable from the package root, because tests import them by name:
|
|
||||||
`ACTION_PAGE`, `SNIPPET_MAX`, `_snippet`, `world_delta_of`, `acquire_turn_lock`,
|
|
||||||
`retry_action`, `undo_turn`, and `_active_turns`.
|
|
||||||
|
|
||||||
`_active_turns` is module-level mutable state guarded by a lock. It must live in
|
|
||||||
exactly one module, `turns.py`, and every other module must import the module and
|
|
||||||
reach through it. If two modules import the set by value, the lock guards two
|
|
||||||
different sets and the turn lock stops working.
|
|
||||||
|
|
||||||
### What the router split actually did, 2026-08-29
|
|
||||||
|
|
||||||
`backend/app/routers/adventures.py` is now a package of 14 modules. The largest
|
|
||||||
is `turns.py` at 443 lines. Four modules exist that the table above does not
|
|
||||||
list, because the plan's eleven still mixed unrelated work:
|
|
||||||
|
|
||||||
| Extra module | Why it exists |
|
|
||||||
|---|---|
|
|
||||||
| `deps.py` | Holds the `APIRouter` and the ownership check. It imports nothing else in the package, so every endpoint module can import the router without importing its siblings. |
|
|
||||||
| `scenario_text.py` | Copying a scenario's text and cards has two callers, `crud.create_adventure` and `refresh`. Leaving it in either one made the other import an endpoint module. |
|
|
||||||
| `nodes.py` | Story-tree navigation that four modules use: `last_action`, `next_index`, `next_depth`, `stand_on`, `db_tip`, `delete_turn`. |
|
|
||||||
| `actions.py` | The three action endpoints. They page and delete rather than play a turn, so they do not belong in `crud.py`. |
|
|
||||||
|
|
||||||
Two decisions differ from the plan above.
|
|
||||||
|
|
||||||
**The package root does not re-export `acquire_turn_lock` or `_active_turns`.**
|
|
||||||
The plan said to keep them importable, but that makes a broken patch look like a
|
|
||||||
working one. Rebinding `adventures.generate_turn` changes the alias and leaves
|
|
||||||
every caller reading the original, and the test still passes. Leaving those names
|
|
||||||
off the package root raises `AttributeError` instead. Eighteen test call sites and
|
|
||||||
four in `backend/tools/` now say `adventures.turns.<name>`. The package root still
|
|
||||||
re-exports the pure helpers, so `adventures.ACTION_PAGE`, `adventures.undo_turn`,
|
|
||||||
and `chat.py`'s `from .adventures import SSE_HEADERS, sse` are unchanged.
|
|
||||||
|
|
||||||
**`world_delta_of` lives in `turns.py`.** It reads as a world-state helper and sat
|
|
||||||
beside the world-state endpoints, but `_generate_turn` is its only caller.
|
|
||||||
|
|
||||||
`_active_turns` behaved as the plan warned. Every module reaches it as
|
|
||||||
`turns._active_turns`, and a check confirms the four modules see one set object
|
|
||||||
and one lock.
|
|
||||||
|
|
||||||
One test coupled to the router for an unrelated module: it called
|
|
||||||
`adventures.worldstate.instantiate`. It imports `app.worldstate` directly now.
|
|
||||||
|
|
||||||
The split moved text rather than retyping it. An AST comparison against the
|
|
||||||
pre-split file confirms all 86 definitions are identical, once the `turns.`
|
|
||||||
prefix is normalized away. The 549 tests pass, and the OpenAPI schema still lists
|
|
||||||
the same 35 operations.
|
|
||||||
|
|
||||||
### `worldstate/engine.py` becomes a package
|
|
||||||
|
|
||||||
Split `backend/app/worldstate/engine.py` into four modules under
|
|
||||||
`backend/app/worldstate/`:
|
|
||||||
|
|
||||||
- `schema.py`: `has_schema`, `instantiate`, `reconcile`, `band_label`, `npc_name`,
|
|
||||||
`npc_triggers`, `_initials`.
|
|
||||||
- `parse.py`: `extract_delta`, `_tolerant_load`, `render_delta_block`,
|
|
||||||
`applied_delta`, `refusals`, `render_refusals`.
|
|
||||||
- `apply.py`: `apply_delta`, `apply_override`, and the shared path resolver Stage 2
|
|
||||||
introduces.
|
|
||||||
- `render.py`: `render_state_section`, `render_reference`, `_stat_line`,
|
|
||||||
`_describe_stat`.
|
|
||||||
|
|
||||||
`worldstate/__init__.py` re-exports every public name it exports today, so no call
|
|
||||||
site changes. The engine is imported as a module, not monkeypatched, so this split
|
|
||||||
carries none of the router's test coupling.
|
|
||||||
|
|
||||||
### `pages/Play.jsx` becomes a directory
|
|
||||||
|
|
||||||
Split it into `frontend/src/pages/Play/`:
|
|
||||||
|
|
||||||
- `index.jsx`: the page component.
|
|
||||||
- `usePlaySession.js`: the 18 `useState` calls and 7 `useEffect` calls that drive
|
|
||||||
one adventure, behind one hook.
|
|
||||||
- `panels/`: `PlotPanel`, `MemoryPanel`, `ScriptsPanel`, `InsightsPanel`,
|
|
||||||
`BranchPanel`.
|
|
||||||
- `drawers/`: `StatusDrawer`, `WorldStateDrawer`, and the `StatRow`, `StatGroup`,
|
|
||||||
`StateTree`, and `StateValue` parts they use.
|
|
||||||
- `reports/`: `StateChangeChips`, `WorldStateReport`, `ScriptReport`,
|
|
||||||
`CacheReport`, `TokenBreakdown`.
|
|
||||||
- `TakePager.jsx`, `RefreshModal.jsx`.
|
|
||||||
|
|
||||||
There is no frontend test runner until Stage 5, so this split is verified by
|
|
||||||
`npm run lint`, `npm run build`, and by driving the Play screen in a browser. Drive
|
|
||||||
it. `plan/STATUS.md` records that the last two Play bugs were both found by hand and
|
|
||||||
were unreachable from any test that existed.
|
|
||||||
|
|
||||||
### `index.css` becomes a directory
|
|
||||||
|
|
||||||
Split it into `frontend/src/styles/`, imported in order from `index.css`:
|
|
||||||
`tokens.css`, `base.css`, `nav.css`, `cards.css`, `play.css`, `drawers.css`,
|
|
||||||
`schema-editor.css`, `insights.css`, `modals.css`, and `theme.css`.
|
|
||||||
|
|
||||||
Move each `@media` block next to the rules it overrides, rather than leaving one
|
|
||||||
`max-width: 720px` block at the end. Keep the source order identical when you move
|
|
||||||
rules, because CSS resolves ties by order and the file relies on that in at least
|
|
||||||
two known places, recorded in `plan/STATUS.md` and in the comments at
|
|
||||||
`index.css:1011` and `1082`.
|
|
||||||
|
|
||||||
**Check:** 549 tests pass, `npm run lint` and `npm run build` are clean, and the
|
|
||||||
Play screen works in a browser at desktop and at 500 px wide.
|
|
||||||
|
|
||||||
### What Stage 1 actually did to the frontend, 2026-08-29
|
|
||||||
|
|
||||||
`Play.jsx` is now `frontend/src/pages/Play/`, twelve files. The largest is
|
|
||||||
`index.jsx` at 781 lines. `index.css` is now an `@import` list over
|
|
||||||
`frontend/src/styles/`, eighteen files.
|
|
||||||
|
|
||||||
Two decisions differ from the plan above.
|
|
||||||
|
|
||||||
**`usePlaySession.js` does not exist yet.** The page component still owns all of
|
|
||||||
the session state. Moving eighteen `useState` calls and seven `useEffect` calls
|
|
||||||
is a rewrite, not a move, and no frontend test would catch a mistake in it
|
|
||||||
today. It waits for Stage 5.
|
|
||||||
|
|
||||||
**The `@media` blocks stayed where they were.** `responsive.css` still holds one
|
|
||||||
`max-width: 720px` block, at the end of the import order. Moving a `@media` block
|
|
||||||
next to the rules it overrides moves it earlier in the cascade, which changes
|
|
||||||
which of two equal-specificity rules wins. Nothing in the test suite would catch
|
|
||||||
that. Do this after Stage 5.
|
|
||||||
|
|
||||||
Each split is verified by a different proof, because neither one has a test:
|
|
||||||
|
|
||||||
- CSS: the parts rebuild `index.css` byte for byte, and the built bundle is
|
|
||||||
identical before and after at 56686 bytes.
|
|
||||||
- JSX: every non-blank line of the original appears exactly once, in order, across
|
|
||||||
the twelve files. A name-resolution check confirms every identifier each file
|
|
||||||
references is defined or imported there, with no unused imports.
|
|
||||||
|
|
||||||
The line split stranded a comment at six of the boundaries. A leading comment
|
|
||||||
sits above the section it describes, so each boundary cut one loose and left it
|
|
||||||
at the end of the file before it. All six moved to the section they describe.
|
|
||||||
|
|
||||||
`npm run lint` and `npm run build` are clean, and 549 tests pass. Driving the
|
|
||||||
Play screen covered the story view, all five panels, both drawers including the
|
|
||||||
world-state edit form, the branch map, the refresh dialog, and the take pager,
|
|
||||||
which stepped onto a take that lives on another branch and switched to it. The
|
|
||||||
console reported no errors. The extension cannot resize the render viewport and
|
|
||||||
the app sends `X-Frame-Options: DENY`, so the narrow-width check ran by setting
|
|
||||||
the `max-width` media queries to `all` in the live stylesheet. All 71 narrow
|
|
||||||
rules found their elements: the nav collapses to one button, the panel tabs move
|
|
||||||
onto the title row, a panel fills the screen, the composer stacks, and both
|
|
||||||
drawers become edge tabs.
|
|
||||||
|
|
||||||
## Stage 2: remove duplication
|
|
||||||
|
|
||||||
Each item here is small and is covered by tests that already exist.
|
|
||||||
|
|
||||||
1. **One path resolver in `worldstate`.** `apply_delta` and `apply_override` route
|
|
||||||
`flags.<name>`, `milestones.<id>`, `npc.<id>.<stat>`, `world.<stat>`, and
|
|
||||||
`player.<stat>` with parallel code. Extract a resolver that returns the
|
|
||||||
container, the stat definition, and the kind, then let the two functions differ
|
|
||||||
only in the write rule. Their rules genuinely differ, so do not merge the
|
|
||||||
functions themselves. `apply_override` ignores `cooldown`,
|
|
||||||
`max_delta_per_turn`, and the rule that a counter cannot decrease, and it lets a
|
|
||||||
milestone be un-set. `tests/test_worldstate.py` covers both.
|
|
||||||
2. **Add `backend/tests/conftest.py`.** Move the temporary-database prologue there,
|
|
||||||
so the other 35 files drop 8 lines each. The prologue has to run before
|
|
||||||
`from app.main import app`, because `main.py` calls `bootstrap(engine)` at import
|
|
||||||
time. A `conftest.py` runs before any test module, which satisfies that.
|
|
||||||
3. **One fake provider.** Move `ScriptedProvider` and `FakeProvider` to `conftest.py`
|
|
||||||
as one fixture, replacing 12 copies.
|
|
||||||
4. **Move the SSE helpers out of the router.** Put `sse`, `SSE_HEADERS`, and
|
|
||||||
`turn_error` in `backend/app/sse.py`, so `chat.py` stops importing from
|
|
||||||
`adventures`.
|
|
||||||
5. **Make `get_adventure_or_404` a dependency.** About 20 handlers repeat the call.
|
|
||||||
A `Depends` removes the line from each one and puts the ownership check in the
|
|
||||||
signature, where a reader looking for it expects it.
|
|
||||||
6. **One `useDebouncedSave` hook** in `frontend/src/hooks/`, replacing the three
|
|
||||||
copies.
|
|
||||||
7. **Delete `Settings.stream`.** Remove the column, the two schema fields, and add
|
|
||||||
the migration that drops it. This is item S1 in `docs/self-review.md`. Mark it
|
|
||||||
applied there.
|
|
||||||
|
|
||||||
**Check:** 549 tests pass. Test count may drop if consolidating fixtures removes a
|
|
||||||
duplicate case. If it does, say which case and why in the commit message.
|
|
||||||
|
|
||||||
### What Stage 2 actually did, 2026-08-29
|
|
||||||
|
|
||||||
All seven items landed. Items 2 and 3 went in early, with the test setup in
|
|
||||||
`32cd7c1`, because the conftest had to exist before the router split could move
|
|
||||||
any test. Items 1, 4, and 5 are `2c57b1c`. Items 6 and 7 are `e0bf2b6`.
|
|
||||||
|
|
||||||
**Item 1, the path resolver.** `_resolve` returns a `_Target` naming the kind,
|
|
||||||
the section, the key, the stat definition, and the character, or a rejection.
|
|
||||||
The two callers now differ only in the write rule, which is what the item asked
|
|
||||||
for. Building the container is a method on `_Target` rather than part of
|
|
||||||
resolving, because a rejected path must not leave an empty section behind.
|
|
||||||
|
|
||||||
A refactor here is hard to check by reading, so it was checked by running. A
|
|
||||||
differential harness fed 3960 generated payloads through the old and the new
|
|
||||||
implementation and compared the state and the report. Ignoring `fix`, there are
|
|
||||||
zero differences. 674 rejections gained a `fix` string and none lost one, all of
|
|
||||||
them in `apply_override`, which had been the terser of the two. That is an
|
|
||||||
improvement rather than a regression: `apply_delta` already worded those
|
|
||||||
strings, and the world-state editor renders them.
|
|
||||||
|
|
||||||
**Item 5, the ownership dependency.** All 32 handlers converted, not the 20 the
|
|
||||||
review estimated. Two proofs, because tests alone would not catch a change in
|
|
||||||
the order of the checks or in the public HTTP surface:
|
|
||||||
|
|
||||||
- An AST pass confirmed the `get_adventure_or_404` call was the first statement
|
|
||||||
in every one of the 32 handlers. If it were not, hoisting it into a dependency
|
|
||||||
would move work that used to run after something else.
|
|
||||||
- The generated OpenAPI document was diffed against one built from a `git clone`
|
|
||||||
at `HEAD`. The only difference is that `rename_branch` now lists `branch_id`
|
|
||||||
before `adventure_id`, which is parameter order in the spec and not a route
|
|
||||||
change.
|
|
||||||
|
|
||||||
Seven tests in `test_state_revert.py` needed updating. They call handlers as
|
|
||||||
plain functions rather than over HTTP, so they have to pass `adventure=` now.
|
|
||||||
That file is the only one that does this.
|
|
||||||
|
|
||||||
**Item 7 needed a migration guard.** Migration 65 drops `settings.stream`, and
|
|
||||||
it is the first migration that drops a column. `_column_already_there` already
|
|
||||||
existed for the `ADD COLUMN` case. Dropping needs the mirror, `_column_already_gone`,
|
|
||||||
because `create_all` builds the current schema, which is already missing the
|
|
||||||
column, and fixtures like `test_tree_migration.pre_tree` stamp an old version
|
|
||||||
against a database built that way and replay. Verified by running migration 65
|
|
||||||
twice, once against a database that still had the column and once against one
|
|
||||||
that did not.
|
|
||||||
|
|
||||||
**One duplicate was left alone.** `FakeProvider` in `test_chat.py` is not a copy
|
|
||||||
of `ScriptedProvider`. It records the key, model, and endpoint it was
|
|
||||||
constructed with, which is how the chat tests assert on what would have gone
|
|
||||||
over the wire. `fakes.ScriptedProvider` streams replies and records prompts.
|
|
||||||
Merging them would give one class two unrelated jobs.
|
|
||||||
|
|
||||||
## Stage 3: SP8, drop the legacy columns
|
|
||||||
|
|
||||||
This is the only stage that touches the production database. Follow
|
|
||||||
`plan/14-phase-story-tree.md`, which specifies it.
|
|
||||||
|
|
||||||
1. Confirm nothing reads the eight columns. `ACTION_LIST_COLUMNS` in the adventures
|
|
||||||
package lists `index`, `variant_count`, and `variant_index` today, so that tuple
|
|
||||||
changes here. `bundle.py:37` and `context/history.py:424` both describe `index`
|
|
||||||
as unread; verify that rather than trusting the comment.
|
|
||||||
2. Add the migration that drops them. Remove the columns from `models.py`, and
|
|
||||||
remove the fields from `schemas.py` and from `ActionOut`.
|
|
||||||
3. Deploy, then run `VACUUM FULL actions;` on the direct Neon endpoint, not the
|
|
||||||
`-pooler` one. Dropping a column rewrites toasted values, and nothing reclaims
|
|
||||||
that space on its own. `plan/STATUS.md` records what happened the last time
|
|
||||||
nobody ran it: the database reached 144.2 MB against a 512 MB tier.
|
|
||||||
4. `VACUUM FULL` takes an `ACCESS EXCLUSIVE` lock, so the app blocks on `actions`
|
|
||||||
for the duration. It took 5.5 seconds at 144 MB.
|
|
||||||
|
|
||||||
**Check:** 549 tests pass, `/api/health` answers `{"ok":true}` after the deploy, and
|
|
||||||
one existing adventure opens, takes a turn, retries it, and pages back through the
|
|
||||||
transcript.
|
|
||||||
|
|
||||||
### What Stage 3 actually did, 2026-08-29
|
|
||||||
|
|
||||||
Steps 1 and 2 landed. Step 3, the deploy and the `VACUUM FULL`, is still open,
|
|
||||||
because it runs against production and belongs with the release, not with the
|
|
||||||
branch.
|
|
||||||
|
|
||||||
**Step 1, the read audit.** Nothing outside the migrations reads the eight
|
|
||||||
columns. `TakePager.jsx` reads `action.take_count` and `action.take_index` only,
|
|
||||||
which is what makes dropping the three payload fields safe. The comments in
|
|
||||||
`bundle.py` and `context/history.py` were accurate.
|
|
||||||
|
|
||||||
**Step 2, the drop.** Migrations 66 to 73 drop one column each. `index` is a
|
|
||||||
keyword in SQLite, so migration 71 quotes it. `models.py`, `schemas.py`, and
|
|
||||||
`ACTION_LIST_COLUMNS` lost the same eight, `Adventure.actions` now orders by
|
|
||||||
`id`, and `attempts.renumber`, `context/history.max_action_index`, and
|
|
||||||
`nodes.next_index` are deleted.
|
|
||||||
|
|
||||||
Two problems came out of the migration passes rather than the DDL:
|
|
||||||
|
|
||||||
- `_split_variants_into_siblings` wrote through `Base.metadata.tables["actions"]`,
|
|
||||||
the live ORM table, so it stopped compiling the moment migration 66 removed
|
|
||||||
five of its columns. It now writes through `_ACTIONS_AT_60`, a frozen `Table`
|
|
||||||
carrying its own `MetaData`. That declaration is a snapshot of a past schema
|
|
||||||
and must not be updated to track `models.py`.
|
|
||||||
- Five passes read columns that migrations 66 to 73 drop. A `create_all`
|
|
||||||
database replays every migration against the current schema, so each pass now
|
|
||||||
calls `_has_columns` and returns early when the columns are absent. This is
|
|
||||||
the rule `_column_already_there` applies to DDL, applied to the passes.
|
|
||||||
|
|
||||||
`bootstrap` gained a `through` argument. A migration test that asserts on
|
|
||||||
something a later migration removes stops at the version it is about, rather
|
|
||||||
than reading a schema several versions newer.
|
|
||||||
|
|
||||||
**One pre-existing bug found and not fixed.** `bundle._write_nodes` never sets
|
|
||||||
`Action.parent_id`, and `paging.annotate_takes` groups on `parent_id`, so an
|
|
||||||
imported adventure's take pager reads 1 of 1. The frontend has read only
|
|
||||||
`take_count` since SP9, so this predates Stage 3 and is not a regression from
|
|
||||||
it. `test_retry_variants.py` documents it.
|
|
||||||
|
|
||||||
**Check:** 555 backend tests pass, up from 549. `test_tree_migration.py` gained
|
|
||||||
eight parametrized cases asserting each column is gone after a real schema-45
|
|
||||||
database migrates all the way. A `create_all` database would pass those without
|
|
||||||
running the migration, which is why the fixture is a frozen pre-tree one.
|
|
||||||
|
|
||||||
## Stage 4: documentation
|
|
||||||
|
|
||||||
1. **Generate `docs/guide.html` from `docs/GUIDE.md`.** Write a small build script
|
|
||||||
that renders the Markdown into the existing hand-written HTML shell, keeping the
|
|
||||||
current styles and metadata. After that, one edit updates both. Note in
|
|
||||||
`README.md` that the HTML is generated and that you edit the Markdown.
|
|
||||||
2. **Decide what `docs/architecture.html` is.** It has no Markdown source. Either
|
|
||||||
give it one and generate it the same way, or state at the top of the file that
|
|
||||||
it is hand-written, so the next person does not look for a source that does not
|
|
||||||
exist.
|
|
||||||
3. **Split `plan/STATUS.md`.** It is 63 KB, and most of it is dated session logs.
|
|
||||||
Keep "Pick up here", "Things worth remembering", and "Running things" in
|
|
||||||
`STATUS.md`. Move the dated entries to `plan/history/`, newest file first.
|
|
||||||
4. **Add `docs/DEVELOPING.md`.** Record how to run the app, how to run the tests,
|
|
||||||
where each subsystem lives, and the invariants a newcomer breaks first:
|
|
||||||
- Anything that reads `adventure.actions` during generation takes
|
|
||||||
`exclude_action_id`. It leaks into four places, not one.
|
|
||||||
- `memory_cursor` and `summary_cursor` are positions into `story_actions()`, and
|
|
||||||
`Memory.source_start` and `source_end` are `Action.index` values. The two
|
|
||||||
spaces diverge as soon as anything is deleted.
|
|
||||||
- `/auth/me` must not raise. It is the SPA's bootstrap call, so anything it
|
|
||||||
touches that can raise takes the whole frontend down.
|
|
||||||
- The global field rule in the stylesheet keys off `input[type=...]`, so a bare
|
|
||||||
`<input>` and any new input type get browser default styling until you add
|
|
||||||
them.
|
|
||||||
- A modal opened from `.side-panel` needs `createPortal`, because the panel's
|
|
||||||
filling transform animation makes it the containing block for
|
|
||||||
`position: fixed`.
|
|
||||||
|
|
||||||
**Check:** the generated HTML matches the Markdown section for section, including
|
|
||||||
section 3.6. Every link in `README.md` and `docs/index.html` resolves.
|
|
||||||
|
|
||||||
## Stage 5: a frontend test runner
|
|
||||||
|
|
||||||
`plan/STATUS.md` names the missing runner as the reason this project keeps finding
|
|
||||||
UI bugs by hand. Two shipped bugs were unreachable from any test that existed.
|
|
||||||
|
|
||||||
1. Add Vitest, React Testing Library, and jsdom. Add a `test` script to
|
|
||||||
`frontend/package.json`.
|
|
||||||
2. Add the step to the existing `frontend` job in `.github/workflows/ci.yml`,
|
|
||||||
between lint and build.
|
|
||||||
3. Write the first tests against the two bug classes that already recurred:
|
|
||||||
- The take pager renders when a retry's reply arrives over SSE. The stream builds
|
|
||||||
its own `ActionOut`, so it was the one payload that never carried the
|
|
||||||
annotation.
|
|
||||||
- Retaking a player turn does not produce `> You > You`. The editor is seeded
|
|
||||||
with stored text that is already formatted.
|
|
||||||
4. Add a test for `usePlaySession` from Stage 1, since that hook now holds the state
|
|
||||||
the page used to hold inline.
|
|
||||||
|
|
||||||
**Check:** `npm test` passes locally and in CI.
|
|
||||||
|
|
||||||
## Risks
|
|
||||||
|
|
||||||
| Risk | Where | How you catch it |
|
|
||||||
|---|---|---|
|
|
||||||
| A monkeypatch silently stops patching, and a test passes while calling a real provider | Stage 1, `routers/adventures` split | After retargeting, break the fake on purpose and confirm the tests that use it fail |
|
|
||||||
| `_active_turns` ends up imported by value into two modules, so the turn lock guards two sets | Stage 1 | `tests/test_state_revert.py` reaches for `adventures._active_turns`. Keep that import path working, and confirm a second concurrent turn still returns 409 |
|
|
||||||
| A CSS rule changes meaning because the split reorders it | Stage 1, stylesheet split | Compare the built CSS before and after. Order within each section must not change |
|
|
||||||
| The column drop rewrites the table and nobody reclaims the space | Stage 3 | Run `VACUUM FULL actions;` on the direct endpoint, and read sizes from `sum(octet_length(col))` rather than `n_live_tup` |
|
|
||||||
| Splitting `Play.jsx` breaks something no test covers | Stage 1 | Drive the Play screen by hand at both widths. Stage 5 exists to shrink this risk for next time |
|
|
||||||
|
|
||||||
## What this phase does not do
|
|
||||||
|
|
||||||
- It does not replace the hand-rolled migration runner with Alembic. 64 migrations
|
|
||||||
run correctly, and `docs/GUIDE.md` section 2.6 already explains the choice.
|
|
||||||
- It does not move `bootstrap(engine)` out of import time in `main.py`. The current
|
|
||||||
behavior means a failed migration means no service, which is deliberate. Stage 2's
|
|
||||||
`conftest.py` removes the boilerplate that import-time bootstrapping forces on
|
|
||||||
tests, which is the part that actually hurts.
|
|
||||||
- It does not change any API shape, any database content, or any prompt.
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
# Appendix: the memory-prompt A/B, run 2 — a fresh story
|
|
||||||
|
|
||||||
An independent replication of `plan/18-appendix-memory-ab-run.md`, produced by
|
|
||||||
`backend/tools/memory_ab.py` through `tools/claude_shim.py` after
|
|
||||||
`MEMORY_MAX_WORDS` was added. The story is **newly generated**, not the one run 1
|
|
||||||
used, so this tests the prompts against different prose rather than re-scoring
|
|
||||||
the same text.
|
|
||||||
|
|
||||||
## What replicated, and what did not
|
|
||||||
|
|
||||||
| | run 1 before | run 2 before | run 1 after | run 2 after |
|
|
||||||
|---|---|---|---|---|
|
|
||||||
| memory 1 | 34 w, "you" | 89 w, "the player" | 72 w, named | 54 w, named |
|
|
||||||
| memory 2 | 105 w, "the player" | 107 w, "the player" | 68 w, named | 55 w, named |
|
|
||||||
|
|
||||||
**Naming replicated cleanly.** Four control memories across two runs, and not one
|
|
||||||
of them names the protagonist. Four treatment memories, and all four do. That is
|
|
||||||
the change working, and it is the finding to rely on.
|
|
||||||
|
|
||||||
**Length replicated, and the ceiling holds.** The control ran 34, 89, 105, 107
|
|
||||||
words — a four-fold spread with no stated budget. The treatment after
|
|
||||||
`MEMORY_MAX_WORDS` ran 54 and 55.
|
|
||||||
|
|
||||||
**The person-drift did not recur, and the claim about it should be read
|
|
||||||
narrowly.** Run 1 produced two control memories in two different persons — "You
|
|
||||||
crept low" and "The player asked Gwen" — which is exactly the reported
|
|
||||||
complaint. Run 2's controls were both "the player", consistently. So drifting
|
|
||||||
between *second* and *third* person is a real thing a model does, observed once,
|
|
||||||
not something it does every time. What is consistent across both runs is that
|
|
||||||
the control never reaches for the character's name, because it has never been
|
|
||||||
told one.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
*Everything below is the raw run, unedited.*
|
|
||||||
|
|
||||||
One story, generated through `build_context` against `sonnet` at `http://127.0.0.1:8787/v1`. Both prompts then summarize the same blocks, so the prompt is the only variable. The control is `MEMORY_SYSTEM_PROMPT` as of `9cdcb55`.
|
|
||||||
|
|
||||||
| memory | arm | words | framing |
|
|
||||||
|---|---|---|---|
|
|
||||||
| 1 | before | 89 | "the player" |
|
|
||||||
| 1 | after | 54 | named |
|
|
||||||
| 2 | before | 107 | "the player" |
|
|
||||||
| 2 | after | 55 | named |
|
|
||||||
|
|
||||||
## Memory 1
|
|
||||||
|
|
||||||
**Before:** The player and Gwen scouted a bandit camp at dawn, agreeing on a quiet approach with Gwen circling the right flank (targeting the spear-carrying guard) while the player advanced from the left; the strongbox sat hidden under an oiled tarp in the camp's center. The player searched an empty bedroll undetected, taking a belt knife, a handful of copper and silver coins, and a charcoal-marked parchment map of the camp, then began moving low toward the strongbox while the guard remained oblivious and Gwen stayed ready with her bow.
|
|
||||||
|
|
||||||
**After:** Kaelen and Gwen infiltrated the bandit camp at dawn, splitting flanks with Gwen covering right from the tree line, bow ready, while Kaelen approached left. Kaelen looted a bedroll near an overturned cart, taking a belt knife, coins, and a charcoal map of the camp, then began moving low toward the tarp-covered strongbox undetected.
|
|
||||||
|
|
||||||
## Memory 2
|
|
||||||
|
|
||||||
**Before:** The player crept through the bandit camp toward a tarp-covered iron strongbox, reflexively reached to pull Gwen down beside him despite her being forty feet away at the tree line, and the resulting half-second lapse nearly caused a second bandit (a wood-gatherer) to spot him at the woodpile; Gwen almost loosed an arrow but held on his signal, and the bandit left unaware. The player then waited for the spearman to turn toward the fire and moved undetected to crouch beside the strongbox, where Gwen signaled approval and pointed to the simple hasp-and-padlock lock before he asked her whether she still trusted his read on the situation.
|
|
||||||
|
|
||||||
**After:** Kaelen infiltrated the bandit camp toward the tarp-covered strongbox while Gwen covered him from the tree line; he instinctively reached to pull her down beside him but she was forty feet away, nearly exposing him to an unnoticed second bandit gathering wood, which frayed Gwen's trust before he safely reached the strongbox's simple hasp-and-padlock lock.
|
|
||||||
|
|
||||||
## The story both arms summarized
|
|
||||||
|
|
||||||
**start:** Dawn breaks grey over the treeline as you and Gwen crouch at the edge of the bandit camp. Smoke curls from a dying fire; three bedrolls lie empty. Somewhere ahead, the stolen caravan strongbox waits.
|
|
||||||
|
|
||||||
Gwen checks her bowstring and looks to you. "Quiet, or loud?"
|
|
||||||
|
|
||||||
**do:** go quiet and signal Gwen to circle around the right flank
|
|
||||||
|
|
||||||
**ai:** You lean close, voice barely a breath. "Quiet. Take the right flank, stay in the tree line — I'll come at them from the left when you're set. Don't loose until I move first, unless someone spots you."
|
|
||||||
|
|
||||||
Gwen's mouth quirks, half approval, half habit. "Try not to trip over anything this time." She melts sideways into the brush without a sound, her passage barely stirring a fern, and angles wide around the camp's edge, bow already loose in her hand.
|
|
||||||
|
|
||||||
You give her a slow count — enough time to get into position — watching the camp in the meantime. A lone bandit paces near the fire, spear resting on his shoulder, yawning like the night watch had been long and dull. Beyond him, tents sag in a loose ring, and past those, half-hidden under an oiled tarp, a squat iron-banded shape sits guarded by nothing more than habit and complacency. The strongbox.
|
|
||||||
|
|
||||||
You catch a flicker of movement at the tree line opposite — Gwen, settling into a crouch behind a fallen log, bow half-drawn, patient as stone. She lifts two fingers, then points toward the spear-carrier: hers if it goes loud.
|
|
||||||
|
|
||||||
You ease forward through the underbrush, testing each footfall before committing your weight, keeping the dying fire's smoke between you and the pacing guard's sightline. Your pulse is steady, senses sharpened — the cool mana still humming faintly under your skin from old habit, ready if you need it. No alarm yet. No eyes turned your way. The camp sleeps in its false security, and for now, the quiet is yours to keep or break.
|
|
||||||
|
|
||||||
You reach the shadow of an overturned cart at the camp's rim, close enough now to hear the guard mutter something to himself. Gwen is in position, arrow nocked, waiting on your signal.
|
|
||||||
|
|
||||||
**do:** search the nearest bedroll for anything useful
|
|
||||||
|
|
||||||
**ai:** You glance toward the nearest bedroll — one of the three empty ones, rumpled blankets still holding the shape of whoever slept there, maybe ten feet from the cart's shadow. The guard's back is turned, spear-shoulder rotating slowly as he paces his short loop. You judge the distance, the timing, and decide it's worth the risk.
|
|
||||||
|
|
||||||
You slip low across the open ground, boots finding bare dirt instead of dry leaves, and drop to a crouch beside the bedroll. Your fingers move quick and quiet through the folded blanket, a stiff leather pack shoved half-underneath. Coins clink faintly — you cup your palm over the pouch before it can rattle again — and your hand closes on a stubby belt knife, a handful of copper and silver, and a folded scrap of parchment that might be a rough map of the camp's layout, marks scratched in charcoal.
|
|
||||||
|
|
||||||
The guard's pacing falters. He half-turns, spear dipping, some animal instinct prickling at the edge of his awareness. You freeze mid-motion, breath held, willing yourself into the cart's shadow. Across the clearing, you catch the barest shift from Gwen — her draw hand tightening, ready to loose if this goes wrong.
|
|
||||||
|
|
||||||
The moment stretches. Then the guard grunts, spits into the dying fire, and resumes his slow circuit, oblivious. You let your breath go slow and silent, tucking the coin pouch and parchment into your jerkin, the knife into your belt beside your own.
|
|
||||||
|
|
||||||
It's not much, but it's something — coin for later, and maybe a map worth reading twice before you commit to a route through this camp. You ease back from the bedroll, retreating to the cart's cover, eyes flicking to Gwen. She gives the faintest nod: *still clear, still yours to call.*
|
|
||||||
|
|
||||||
The guard yawns again, spear drooping, utterly unaware how close he came to dying loud. The strongbox waits under its tarp, patient as ever. The quiet is still yours — for now.
|
|
||||||
|
|
||||||
**do:** move toward the strongbox, keeping low
|
|
||||||
|
|
||||||
**ai:** You keep to the cart's shadow a moment longer, judging the guard's rhythm — three steps out, a pause, three steps back, spear dipping low on the turn. When his back rotates fully away, you move.
|
|
||||||
|
|
||||||
Low and fast, you cross the gap between the cart and the nearest tent, one hand brushing the canvas to steady yourself rather than grip it — no rustle, no tug on the guy-lines. You thread between two more tents, close enough now to smell tallow and old woodsmoke, and drop into a crouch behind a stack of crated supplies not ten feet from the tarp-covered strongbox.
|
|
||||||
|
|
||||||
From here you can see it properly: iron bands dark with rust, a heavy lock through a hasp, the oiled tarp thrown over it more to keep off dew than to hide it. Careless. Whoever's running this camp trusts their numbers more than their diligence.
|
|
||||||
|
|
||||||
You risk a glance back toward the tree line. Gwen hasn't moved, but her head has tracked your progress the whole way, bow still half-drawn, patient. She catches your eye and gives the barest tilt of her chin toward the strongbox — *go on, I've got you* — before her gaze slides back to the pacing guard, who is still oblivious, still yawning his way through a boring watch.
|
|
||||||
|
|
||||||
Closer now, you can make out voices from one of the tents — low, unhurried, two men talking about nothing in particular. Not alarmed. Not aware. The camp's quiet holds, fragile as it is, and the strongbox sits within reach, close enough that you could touch the tarp if you stretched out a hand.
|
|
||||||
|
|
||||||
Your fingers itch to check the lock, to see if it needs force or finesse — but that's a decision for the next breath, not this one. For now you crouch in the crate-shadow, heartbeat steady, senses stretched wide across the camp: the pacing guard, the murmuring tent, Gwen's stillness at your back. Nothing has broken yet. The strongbox is yours to reach for.
|
|
||||||
|
|
||||||
**do:** grab her wrist and pull her down behind the woodpile
|
|
||||||
|
|
||||||
**ai:** Your hand moves on instinct before thought catches up — reaching sideways for Gwen's wrist to haul her down out of sight — but your fingers close on nothing but cool air. She isn't beside you. She's forty feet off across the clearing, still nested behind her log at the tree line, exactly where you left her.
|
|
||||||
|
|
||||||
The mistake costs you half a second, and half a second is enough. Boots crunch on the far side of the crates — close, closer than the pacing guard should be. A second bandit, one you hadn't accounted for, rounds the supply stack with a coil of rope over one shoulder, heading for the woodpile stacked just past your cover.
|
|
||||||
|
|
||||||
You drop flat behind the crates instead, breath locked in your chest, willing your travel-stained leathers to melt into the shadow. The bandit hums tunelessly, kneels at the woodpile — near enough you could count the notches in his boot heel — and starts loading split logs into his arms, utterly unaware he's sharing six feet of dirt with an exiled ranger and a very recent, very foolish reflex.
|
|
||||||
|
|
||||||
Across the camp, Gwen has gone rigid, bow lifting a fraction — she saw the second man appear, saw you freeze, and for one taut moment she's clearly bracing to put an arrow in his throat if he so much as glances your way. You catch her eye through the crate-gap and give the smallest shake of your head. *Not yet. Hold.*
|
|
||||||
|
|
||||||
The wood-gatherer straightens, arms full, and ambles off toward the fire without ever knowing how close he came. Your heart hammers against your ribs. Gwen eases her draw, but her jaw stays tight, and even at this distance you can feel the flicker of irritation under her patience — not at the bandit, at you, for a half-second of carelessness that nearly bought you both a fight you hadn't chosen.
|
|
||||||
|
|
||||||
The strongbox still waits behind its tarp, closer now than ever. But the quiet feels thinner than it did a moment ago.
|
|
||||||
|
|
||||||
**do:** wait for the bandit to turn, then move
|
|
||||||
|
|
||||||
**ai:** You settle back against the crates, still as the wood itself, and let your breathing fall into the same slow rhythm as the camp around you. The wood-gatherer's footsteps fade toward the fire pit, and a moment later you hear the clatter of logs dropped beside the flames, a muttered word to the pacing spearman, then silence again but for the crackle of embers catching new fuel.
|
|
||||||
|
|
||||||
You count another ten heartbeats before you risk a look. The spearman has stopped to warm his hands, back fully to you now, attention on the fire instead of his rounds. No one else stirs between you and the tarp.
|
|
||||||
|
|
||||||
This is the gap.
|
|
||||||
|
|
||||||
You rise from the crate-shadow in one smooth motion, low and quick, closing the last stretch of open ground between the crates and the strongbox in a handful of silent strides. The tarp is close enough to touch now — you drop to a crouch beside it, back pressed to the iron-banded corner, and finally let yourself breathe.
|
|
||||||
|
|
||||||
Across the clearing, Gwen's stance has eased a fraction. She tracked every step of that crossing, and though her jaw is still set from the wood-gatherer's near miss, there's the faintest approval in how she holds herself now — the tension of someone watching a mistake corrected rather than repeated. She lifts two fingers again, then points toward the lock hasp: *your move.*
|
|
||||||
|
|
||||||
The lock itself is heavy, old iron, a simple hasp-and-padlock rather than anything clever — the kind of thing built to deter opportunists, not a trained hand. You could pick it quiet, or put a boot to it loud. The voices still murmur from the nearby tent, unconcerned. The spearman still warms his hands at the fire. The quiet holds, thinner than before, but yours.
|
|
||||||
|
|
||||||
**do:** ask her whether she still trusts my read on this
|
|
||||||
|
|
||||||
**ai:** You crouch there a moment longer, one hand still braced on the strongbox's cold iron corner, and risk a glance across the clearing. Gwen's eyes are already on you — they usually are — and you mouth more than whisper the question, tilting your head just enough to ask it: *still with me on this?*
|
|
||||||
|
|
||||||
For a beat she doesn't answer, just watches the spearman warming his hands, the murmuring tent, the whole fragile balance of the camp's false calm. Then she gives a small, economical shrug, the kind that means *mostly*, and taps two fingers against her bow stave — a gesture you've learned means *your instincts, not your luck.* It isn't a full yes. The wood-gatherer's near miss still sits behind her eyes, and you can read it plainly enough: she'll follow your read, but she's watching it closer than she was an hour ago.
|
|
||||||
|
|
||||||
Her hand shifts, index finger drawing a small circle in the air, then pointing at the lock — *pick it, don't break it. Loud is still your last resort, not your first.* It's about as much reassurance as you're going to get from a ranger who's had one scare too many this morning, and honestly, it's fair. You nod once, slow, so she can see you take it.
|
|
||||||
|
|
||||||
The spearman shifts his weight at the fire but doesn't turn. The tent's murmur continues, unbroken. Whatever trust frayed at the woodpile hasn't snapped — it's just drawn tighter, watchful, waiting to see what you do with the lock in front of you. The strongbox sits solid and patient at your back, and beyond the thin canvas walls, the camp sleeps on in its dangerous, borrowed quiet.
|
|
||||||
|
|
||||||
You turn back to the hasp, letting your fingers find the mechanism, senses still split between the metal under your hands and the fire-lit shape of the spearman thirty feet off. The moment is yours to use well — or not.
|
|
||||||
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
# Appendix: the memory-prompt A/B, run 2026-08-31
|
|
||||||
|
|
||||||
The evidence behind `plan/18-persona-and-memory-quality.md`. Reproduce it with:
|
|
||||||
|
|
||||||
python tools/claude_shim.py --port 8787 &
|
|
||||||
python tools/memory_ab.py --out ab.md
|
|
||||||
|
|
||||||
One story, generated a turn at a time through the app's own `build_context`.
|
|
||||||
Both prompts then summarize the **same** blocks, so the story is held constant
|
|
||||||
and the prompt is the only variable. Every call is a separate request, so
|
|
||||||
neither arm sees the other's output, and the model is never told what is being
|
|
||||||
measured. The control is `MEMORY_SYSTEM_PROMPT` as of commit `9cdcb55`, read
|
|
||||||
from git rather than pasted, so it cannot drift from what shipped.
|
|
||||||
|
|
||||||
The model was a Claude model, reached through `tools/claude_shim.py`. See
|
|
||||||
"What this does not show" in plan/18 before generalising from it.
|
|
||||||
|
|
||||||
## What it showed
|
|
||||||
|
|
||||||
| memory | arm | words | how it names the protagonist |
|
|
||||||
|---|---|---|---|
|
|
||||||
| 1 | before | 34 | second person — "**You** crept low…" |
|
|
||||||
| 1 | after | 72 | "**Kaelen** and Gwen crouched…" |
|
|
||||||
| 2 | before | 105 | "**The player** asked Gwen to…" |
|
|
||||||
| 2 | after | 68 | "**Kaelen** and Gwen infiltrated…" |
|
|
||||||
|
|
||||||
Two things came out of this, and only one of them was the thing being tested.
|
|
||||||
|
|
||||||
**The reported fault reproduced.** Two consecutive memories, from one story,
|
|
||||||
written minutes apart, in two different persons. That is the complaint, observed
|
|
||||||
rather than argued from the prompt text. Both after-memories name Kaelen.
|
|
||||||
|
|
||||||
**A fault nobody had noticed.** "1-2 plain sentences" is not a length: 34 words
|
|
||||||
for one block, 105 for the next. `memory_top_k` injects five memories every
|
|
||||||
turn, so the bank's running cost was set by a number never stated.
|
|
||||||
`MEMORY_MAX_WORDS = 50` now states it. Re-run over this same story, the two
|
|
||||||
blocks came back at **32 and 58 words**, still named, still third person, still
|
|
||||||
carrying the camp map, the strongbox behind the second tent, and the strap
|
|
||||||
frayed near through:
|
|
||||||
|
|
||||||
> Kaelen crept low toward the strongbox, now known from the map to be behind
|
|
||||||
> the second tent, with the dozing watchman still unaware and Gwen holding her
|
|
||||||
> covering position on the flank.
|
|
||||||
|
|
||||||
**A likely accuracy win, at n=1.** The player's move was `grab her wrist and
|
|
||||||
pull her down behind the woodpile`. The before-memory recorded "The player asked
|
|
||||||
Gwen to grab her wrist and pull her down", inverting who acted. This is the
|
|
||||||
failure the brief predicts — with no cast there is nothing to say whose wrist
|
|
||||||
"her wrist" is — but one sample is an observation, not a result.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
*Everything below is the raw run, unedited.*
|
|
||||||
|
|
||||||
## Memory 1
|
|
||||||
|
|
||||||
**Before:** You crept low through the mist toward the second tent, following the map's mark toward the strongbox while Gwen held her flanking position, the sleeping watchman still dozing on his spear near the lean-to.
|
|
||||||
|
|
||||||
**After:** Kaelen and Gwen crouched at dawn outside the bandit camp; Kaelen signaled Gwen to circle the right flank while a drowsy watchman kept post near the lean-to, and Gwen took position unseen near the strongbox tent. Kaelen searched an abandoned bedroll, finding a whetstone, a few copper coins, dried meat, and a charcoal-scrawled camp map revealing the stolen strongbox was hidden behind the second tent, then began moving low toward that location.
|
|
||||||
|
|
||||||
## Memory 2
|
|
||||||
|
|
||||||
**Before:** The player asked Gwen to grab her wrist and pull her down behind the woodpile just before a second bandit—half-dressed, carrying a piss-bucket—stumbled out, relieved himself, and returned to bed in the first tent; Gwen lingered a beat before freeing her wrist and murmured "Good eyes." After waiting five heartbeats to confirm the bandit was asleep, the player and Gwen returned to the strongbox tent, where they found the old iron caravan lock too sturdy to pick and instead began testing the straps, discovering one frayed near through at the fold; the player then asked Gwen whether she still trusted their read on the situation.
|
|
||||||
|
|
||||||
**After:** Kaelen and Gwen infiltrated the bandit camp to reclaim a stolen strongbox, sheltering behind a woodpile while a second bandit relieved himself nearby before returning to sleep; afterward Kaelen returned to the strongbox tent, found its lock too sturdy to force by hand but noticed one strap frayed near through, and paused there with Gwen covering him to ask whether she still trusted his read on the situation.
|
|
||||||
|
|
||||||
## Summary
|
|
||||||
|
|
||||||
**Before:** Under cover of mist, the player and Gwen scouted a bandit camp, aiming to reach a strongbox marked on their map while a watchman dozed near a lean-to. Approaching the second tent, the player pulled Gwen down behind a woodpile just in time to avoid a half-dressed bandit who stepped out to relieve himself before returning to sleep in the first tent; Gwen lingered a moment before pulling free, murmuring "Good eyes." After waiting to confirm the camp was settled, the two returned to the strongbox tent, finding its old iron caravan lock too sturdy to pick. They shifted to testing the straps securing it instead, discovering one frayed nearly through at the fold—a possible way in. The player then turned to Gwen to ask whether she still trusted their read on the situation, leaving the moment of decision unresolved.
|
|
||||||
|
|
||||||
**After:** At dawn, Kaelen and Gwen scouted a bandit camp holding a stolen strongbox. Kaelen sent Gwen to circle the right flank near the strongbox tent while a drowsy watchman kept post, then searched an abandoned bedroll himself, finding a whetstone, a few copper coins, dried meat, and a charcoal map marking the strongbox's location behind the second tent. Moving low toward it, the pair sheltered behind a woodpile while a second bandit briefly woke and relieved himself before returning to sleep. Reaching the strongbox tent, Kaelen found its lock too sturdy to force by hand, but noticed one securing strap frayed nearly through. With Gwen covering him, he paused to ask whether she still trusted his read on the situation before attempting to exploit the weak strap.
|
|
||||||
|
|
||||||
## The story that was summarized
|
|
||||||
|
|
||||||
**start:** Dawn breaks grey over the treeline as you and Gwen crouch at the edge of the bandit camp. Smoke curls from a dying fire; three bedrolls lie empty. Somewhere ahead, the stolen caravan strongbox waits.
|
|
||||||
|
|
||||||
Gwen checks her bowstring and looks to you. "Quiet, or loud?"
|
|
||||||
|
|
||||||
**do:** go quiet and signal Gwen to circle around the right flank
|
|
||||||
|
|
||||||
**ai:** You melt back from the ridge line without a sound, boots finding moss instead of twigs, and catch Gwen's eye. Two fingers point right, then curl in a half-circle — *swing wide, come at the flank*. She reads it instantly, dips her chin, and peels off through the bracken, bow already half-raised, moving low and quiet along the tree line until the mist swallows her.
|
|
||||||
|
|
||||||
You hold position at the camp's edge, breath slow, watching the dying fire spit its last embers. A bandit shifts on watch near the lean-to, spear butt planted in the dirt, more bored than alert. No one else stirs. Somewhere off to the right you catch the faintest whisper of disturbed leaves — Gwen, further along than you'd expect, patient as ever. She's given herself a clean angle on the strongbox tent from the flank, out of the watchman's sightline entirely.
|
|
||||||
|
|
||||||
The plan is good. Quiet holds. All you need now is the first move — and the choice of who falls first, and how.
|
|
||||||
|
|
||||||
The watchman yawns, oblivious, close enough that a single silent step would put you in reach of his throat before he ever draws breath to shout.
|
|
||||||
|
|
||||||
**do:** search the nearest bedroll for anything useful
|
|
||||||
|
|
||||||
**ai:** You crouch by the nearest bedroll — a heap of stitched furs still holding the sleeper's warmth, though its owner is long since up and yawning by the fire. You keep one eye on the watchman as your fingers work through the folds. A whetstone, a handful of copper coins that clink softly against your palm before you still them with a fist, a scrap of oilcloth wrapped around dried meat gone hard at the edges. Nothing worth the risk so far.
|
|
||||||
|
|
||||||
Then your fingers catch on something stiffer, tucked beneath the bedroll's edge — a folded scrap of parchment, half-torn, charcoal-scrawled. A rough map of the camp, crude but legible: the lean-to marked with an X, a second mark deeper in camp labeled in shorthand you recognize as bandit cant for *strongbox*. It's tucked behind the second tent, not the first — further from the watchman than you'd guessed, closer to where Gwen is now waiting in the mist.
|
|
||||||
|
|
||||||
You fold it away inside your jerkin, pulse ticking a little faster. Small find, real value — it just turned guesswork into a plan. The watchman still hasn't stirred, spear planted, chin dropping toward his chest. Gwen's silhouette is a smudge of shadow at the flank, patient, waiting on your signal.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The camp holds still around you — watchman drowsing, fire dying, Gwen coiled and ready on the flank. The map sits warm against your ribs, the strongbox's true location now known. Your move.
|
|
||||||
|
|
||||||
**do:** move toward the strongbox, keeping low
|
|
||||||
|
|
||||||
**ai:** You drop low, weight rolling from heel to ball of each foot, threading between the empty bedroll and a stack of split firewood. The watchman's chin has sunk near to his chest, spear listing in his slack grip. You give him a wide berth anyway — no need to gamble breath on a man already halfway to sleep.
|
|
||||||
|
|
||||||
The second tent is smaller than the first, its flap laced shut with a crude leather cord. Just as the parchment promised, its bulk sits wrong for a tent meant to sleep anyone — square-cornered, iron-banded, too heavy for the ground it rests on. You crouch beside it, easing the lacing open one loop at a time, and the flap parts on a dull gleam of banded oak and iron: the strongbox, lock intact, straps still buckled from the caravan raid.
|
|
||||||
|
|
||||||
A shift of cloth behind you — not the watchman, too far behind you for that. You go still, hand dropping to your knife hilt, eyes cutting sideways through the tent's shadow toward the sound. Nothing resolves yet, just a settling, maybe wind through canvas, maybe not. Across the clearing, Gwen has gone motionless too, bow half-drawn, watching the same dark gap between tents that's caught your attention.
|
|
||||||
|
|
||||||
The strongbox is here, within reach, real weight under your palm instead of a rumor on a scrap of parchment. But something in that gap between the tents hasn't declared itself yet, and the watchman is still only a few careless steps away from waking to find you crouched over his camp's whole reason for being.
|
|
||||||
|
|
||||||
**do:** grab her wrist and pull her down behind the woodpile
|
|
||||||
|
|
||||||
**ai:** Your hand shoots out and closes around Gwen's wrist before she's fully turned toward the gap between the tents. She goes rigid for half a heartbeat — a killer's instinct, ready to break the grip — then reads your face and lets herself be pulled. You drag her down behind the woodpile together, shoulders pressed to split logs, breath shallow.
|
|
||||||
|
|
||||||
The shape that had been shifting in the shadow resolves itself: a second bandit, roused early, stumbling out from behind the tents with his breeches half-laced and a piss-bucket in hand, utterly uninterested in anything but the treeline. He doesn't look toward the strongbox tent. Doesn't look toward the woodpile. He shuffles past, empties the bucket against a stump, and turns back the way he came, already yawning.
|
|
||||||
|
|
||||||
Gwen's breath eases out slow beside you. She doesn't pull her wrist away immediately — a beat longer than she needs to — before easing it free herself. "Good eyes," she breathes, barely sound at all. Her gaze flicks to your hand, then back to your face, something unreadable and warm passing behind it. Watching your back instead of charging in — that's the kind of thing she remembers.
|
|
||||||
|
|
||||||
Through the gap in the logs you track the second bandit's path: he ducks into the first tent, not the watch line, and doesn't reemerge. Three accounted for now — the drowsing watchman, this one bedding back down, and whoever else the camp is hiding. The strongbox tent is still open behind you, lock waiting, straps waiting. Gwen settles back into her crouch, bow across her knees, eyes on you for the next signal.
|
|
||||||
|
|
||||||
**do:** wait for the bandit to turn, then move
|
|
||||||
|
|
||||||
**ai:** You go still as stone, eyes fixed on the tent flap the second bandit vanished into. Seconds crawl. Then the canvas stirs one last time — a grunt, a shifting weight settling onto a bedroll, the particular silence of a man who's decided the day can wait a while longer. You count five heartbeats to be sure, watching for any second shadow, any sign he's only feigning sleep. Nothing. Even the watchman by the fire hasn't stirred, spear still planted, chin still low.
|
|
||||||
|
|
||||||
You ease up from the woodpile, moving on the balls of your feet, and slip back to the strongbox tent. Gwen catches the motion and rises with you, silent as a drawn breath, bow angled to cover the gap between tents while you work. The lock is old iron, caravan-make, not bandit work — it'll need proper tools or proper force, not fingers.
|
|
||||||
|
|
||||||
You crouch and test the straps first, feeling for a weak buckle, a give point. One strap is frayed near through at the fold. With steady pressure it might part without the sharp crack of metal — quieter than forcing the lock, if slower. Gwen shifts her weight beside you, patient, trusting you to read the moment right, her eyes flicking once toward the dozing watchman and back.
|
|
||||||
|
|
||||||
The camp holds its breath around you both. The strongbox waits under your hands, and the next move is yours to make.
|
|
||||||
|
|
||||||
**do:** ask her whether she still trusts my read on this
|
|
||||||
|
|
||||||
**ai:**
|
|
||||||
|
|
||||||
@@ -1,655 +0,0 @@
|
|||||||
# The protagonist has no name, and the summarizer is told nothing
|
|
||||||
|
|
||||||
Two changes, in order. Phase 1 gives the adventure a persona. Phase 2 uses it,
|
|
||||||
along with the cast, to fix the memories. Phase 1 is worth shipping on its own;
|
|
||||||
Phase 2 depends on it and is much smaller once it lands.
|
|
||||||
|
|
||||||
**Both phases are built and green (631 backend tests). Phase 1 was driven in a
|
|
||||||
browser (21/21 checks). Phase 2 was run end to end against a real model, as a
|
|
||||||
controlled A/B on one story — see "Run with a real model". A bank written under
|
|
||||||
the old prompt can be rewritten in place — see the last section.**
|
|
||||||
|
|
||||||
**Last updated: 2026-08-31.**
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## The one sentence version
|
|
||||||
|
|
||||||
Memories come back inconsistent and vague because the summarizer is handed six
|
|
||||||
actions of second-person prose and nothing else — no protagonist, no cast, no
|
|
||||||
setting, no instruction about what person to write in — so it cannot say who
|
|
||||||
"you" is or who "she" is, and neither can any memory it writes.
|
|
||||||
|
|
||||||
## The evidence
|
|
||||||
|
|
||||||
This is the entire prompt that writes a memory (`memorybank.py:391`):
|
|
||||||
|
|
||||||
```
|
|
||||||
system: You compress interactive-fiction story excerpts into memories. Respond
|
|
||||||
with 1-2 plain sentences in past tense stating the concrete facts and
|
|
||||||
events (names, places, items, promises, injuries). No preamble, no
|
|
||||||
commentary.
|
|
||||||
|
|
||||||
user: Story excerpt:
|
|
||||||
|
|
||||||
<6 actions, joined by blank lines, truncated to 2000 tokens>
|
|
||||||
|
|
||||||
Memory:
|
|
||||||
```
|
|
||||||
|
|
||||||
Nothing else is passed. Not `adventure.memory` (the plot essentials), not the
|
|
||||||
NPC names and descriptions the scenario already defines, not the world state,
|
|
||||||
and not the protagonist — because until Phase 1 there is no protagonist to pass.
|
|
||||||
|
|
||||||
Two failures follow from that, and they are the two complaints:
|
|
||||||
|
|
||||||
**Inconsistent framing.** The prompt never says what person to write in. The
|
|
||||||
model picks one per call. Across a single bank you get "You entered the crypt",
|
|
||||||
"The player entered the crypt", and "He entered the crypt" describing the same
|
|
||||||
kind of event.
|
|
||||||
|
|
||||||
**No idea who anyone is.** Given `You push the door open. She grabs your arm.`
|
|
||||||
the only honest memory is *"You entered a room and she stopped you."* Retrieved
|
|
||||||
forty turns later into a scene with three women in it, that memory is worse than
|
|
||||||
nothing.
|
|
||||||
|
|
||||||
The summary inherits both problems, because `_update_story_summary` builds from
|
|
||||||
the memory bullets.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Phase 1 — the persona
|
|
||||||
|
|
||||||
## What already exists, and what does not
|
|
||||||
|
|
||||||
`stat_schema` already gives the player a stat block, and it is already
|
|
||||||
namespaced beside the NPCs:
|
|
||||||
|
|
||||||
```
|
|
||||||
world.day
|
|
||||||
player.hp <- the player's stats, already sectioned
|
|
||||||
npc.gwen.trust
|
|
||||||
flags.alarm_raised
|
|
||||||
milestones.escaped
|
|
||||||
```
|
|
||||||
|
|
||||||
NPCs carry `name`, `keys`, and `desc` (`schema.py`, `render_reference`). The
|
|
||||||
`player` section carries none of those. That asymmetry is the whole gap: the
|
|
||||||
player has stats but no identity, so the block renders as `You: hp 100/100` and
|
|
||||||
reads to the model as a floating global rather than a character.
|
|
||||||
|
|
||||||
## The paths do not change
|
|
||||||
|
|
||||||
`player.hp` stays `player.hp`. It does not become `kaelen.hp` or `user.hp`.
|
|
||||||
|
|
||||||
The reason is `_history_text()` in `context/builder.py`. It replays every past
|
|
||||||
turn's stored delta back into the prompt, and those stored blobs contain literal
|
|
||||||
strings like `{"player.hp": -15}`. A path that carries the persona's name breaks
|
|
||||||
the moment a player renames their character: every replayed block in history
|
|
||||||
then names a path the schema no longer defines, `applied_delta` renders it
|
|
||||||
anyway, and the model copies the broken form. A rename would also require
|
|
||||||
migrating every stored `world_state`, every seed scenario, and the `EMIT_RULE`
|
|
||||||
example text, for no functional gain.
|
|
||||||
|
|
||||||
What changes is the **label**, using the trick NPCs already use — print the
|
|
||||||
display name and the path together:
|
|
||||||
|
|
||||||
```
|
|
||||||
before: You: hp 100/100, mana 30/50.
|
|
||||||
after: Kaelen (player): hp 100/100, mana 30/50.
|
|
||||||
```
|
|
||||||
|
|
||||||
The name is visible to the reader, the path stays copyable by the model.
|
|
||||||
|
|
||||||
## Where the persona lives
|
|
||||||
|
|
||||||
Three plain columns on `adventures`, alongside `memory` and `authors_note`:
|
|
||||||
|
|
||||||
```python
|
|
||||||
persona_name: Mapped[str] = mapped_column(String(80), default="")
|
|
||||||
persona_pronouns: Mapped[str] = mapped_column(String(40), default="")
|
|
||||||
persona_desc: Mapped[str] = mapped_column(Text, default="")
|
|
||||||
```
|
|
||||||
|
|
||||||
**Not in `stat_schema`.** Two reasons. It has to work for an adventure with no
|
|
||||||
RPG layer at all, which is most of them, and `_initials()` in
|
|
||||||
`worldstate/schema.py` treats every dict inside a section as a stat definition —
|
|
||||||
a `persona` key dropped into `stat_schema.player` would be instantiated as a
|
|
||||||
stat, rendered as a stat line in the guide, and handed an `initial` value.
|
|
||||||
|
|
||||||
**Adventure-level, not scenario-level.** Two people playing the same scenario
|
|
||||||
are different characters. A scenario steers the protagonist through its plot
|
|
||||||
essentials, which it already can. Keeping personas off `scenarios` also avoids
|
|
||||||
having to decide what "Update from scenario" does to a persona the player has
|
|
||||||
edited: the answer is nothing, because the scenario never had one.
|
|
||||||
|
|
||||||
**Not per-branch.** Branches are alternative futures within one adventure; the
|
|
||||||
protagonist is the same person down all of them.
|
|
||||||
|
|
||||||
Empty `persona_name` means the feature is off and behavior is exactly what it is
|
|
||||||
today. That is the entire backward-compatibility story — no backfill.
|
|
||||||
|
|
||||||
## Pronouns get their own field
|
|
||||||
|
|
||||||
One short string: `he/him`, `she/her`, `they/them`. It exists because Phase 2
|
|
||||||
will tell the summarizer to write in third person. Without a stated pronoun the
|
|
||||||
model infers one from the name, and once it infers wrong that error is baked
|
|
||||||
into every memory it writes from then on and into the summary built from them.
|
|
||||||
A field is cheaper than a wrong guess repeated forever.
|
|
||||||
|
|
||||||
Blank is a valid value. When it is blank nothing is rendered, and the Phase 2
|
|
||||||
prompt tells the summarizer to use the name rather than a pronoun.
|
|
||||||
|
|
||||||
## Where it enters the prompt
|
|
||||||
|
|
||||||
A new static section in `build_context`, between `ai_instructions` and
|
|
||||||
`plot_essentials`:
|
|
||||||
|
|
||||||
```
|
|
||||||
Player character:
|
|
||||||
You are Kaelen (he/him). A half-elf ranger, exiled from the northern holds.
|
|
||||||
```
|
|
||||||
|
|
||||||
**It goes in the system block on purpose.** The description is user-only and
|
|
||||||
never changes during a turn, so it sits inside the cached prefix and costs
|
|
||||||
nothing after the first turn. This is why "only the user can change it" is not
|
|
||||||
just a product choice — it is what keeps the section free. Anything the AI could
|
|
||||||
rewrite would have to move below the history with the other live sections, and
|
|
||||||
would re-price the prompt on every change.
|
|
||||||
|
|
||||||
It is emitted whether or not the adventure has an RPG layer. It is not gated
|
|
||||||
behind `has_ws`.
|
|
||||||
|
|
||||||
## Where it enters the world state
|
|
||||||
|
|
||||||
`worldstate/render.py`, two edits:
|
|
||||||
|
|
||||||
- `render_state_section` takes the display name and prints
|
|
||||||
`Kaelen (player): …` in place of the hardcoded `You: …`. Falls back to `You:`
|
|
||||||
when no persona is set.
|
|
||||||
- `render_reference` gains one line tying the name to the path, mirroring the
|
|
||||||
NPC header it already writes:
|
|
||||||
`- Protagonist Kaelen — stats are addressed as player.<stat>.`
|
|
||||||
Without this the model reads "Kaelen" in the prose and invents `kaelen.hp`.
|
|
||||||
|
|
||||||
The persona description is **not** repeated in the stat guide. It already has
|
|
||||||
its own section, and the guide is about paths.
|
|
||||||
|
|
||||||
## The AI cannot edit it
|
|
||||||
|
|
||||||
Nothing to build. `_resolve()` in `worldstate/apply.py` rejects any path it does
|
|
||||||
not recognize, so a delta of `{"persona.name": "Bob"}` already falls through to
|
|
||||||
"`persona.name` is not a tracked value", and that refusal already reaches the
|
|
||||||
model through `render_refusals`. Worth one test to hold the behavior in place.
|
|
||||||
|
|
||||||
## Setting it, and changing it
|
|
||||||
|
|
||||||
**At the start.** `PlaceholderModal` (`components.jsx`) already exists and
|
|
||||||
already opens before an adventure begins — but only when the scenario contains
|
|
||||||
`${...}` tokens. It becomes a "Begin adventure" modal that always opens, with
|
|
||||||
the three persona fields at the top and any placeholder fields below.
|
|
||||||
|
|
||||||
**Placeholders stay independent of the persona.** A scenario using `${Name}`
|
|
||||||
will ask for a name twice. That is accepted: no scenario in the repo uses
|
|
||||||
placeholders at all (grepped across `seed_data/` and `starter_data/` — zero
|
|
||||||
occurrences), so the collision is hypothetical. If it ever shows up in practice,
|
|
||||||
pre-filling the `Name` field from the persona is a few lines in the modal with
|
|
||||||
no backend change.
|
|
||||||
|
|
||||||
**Later.** `AdventureUpdate` gains the three fields, and `PlotPanel` gets a
|
|
||||||
Protagonist block at the top. The debounced-save wiring is already there.
|
|
||||||
`WorldStateDrawer` shows the name as the character-sheet heading but does not
|
|
||||||
edit it — one edit surface, not two.
|
|
||||||
|
|
||||||
## Files
|
|
||||||
|
|
||||||
| File | Change |
|
|
||||||
|---|---|
|
|
||||||
| `models.py` | three columns on `Adventure` |
|
|
||||||
| `migrations.py` | 74, 75, 76 |
|
|
||||||
| `schemas.py` | `AdventureCreate`, `AdventureUpdate`, `AdventureOut` |
|
|
||||||
| `context/builder.py` | `persona` section after `plot_essentials` |
|
|
||||||
| `worldstate/render.py` | state-section label, reference line |
|
|
||||||
| `worldstate/__init__.py` | export whatever `render.py` newly exposes |
|
|
||||||
| `routers/adventures/crud.py` | store persona at creation |
|
|
||||||
| `bundle.py` | export key + import read |
|
|
||||||
| `frontend/components.jsx` | modal gains persona fields |
|
|
||||||
| `frontend/pages/Scenarios.jsx`, `Home.jsx` | always open the modal |
|
|
||||||
| `frontend/pages/Play/panels/PlotPanel.jsx` | Protagonist block |
|
|
||||||
| `frontend/pages/Play/drawers/WorldStateDrawer.jsx` | heading |
|
|
||||||
| `frontend/api.js` | pass the fields through |
|
|
||||||
|
|
||||||
## Tests
|
|
||||||
|
|
||||||
| File | What |
|
|
||||||
|---|---|
|
|
||||||
| new `test_persona.py` | create with persona; update; renders in the system block; works with no `stat_schema`; empty name is a no-op; AI delta at `persona.*` refused |
|
|
||||||
| `test_worldstate.py` | state line reads `Kaelen (player):`; `player.hp` still resolves |
|
|
||||||
| `test_prompt_caching.py` | persona is in the static prefix, above history |
|
|
||||||
| `test_bundle_v2.py` | round-trips; a bundle without the key still imports |
|
|
||||||
|
|
||||||
## Bundle format
|
|
||||||
|
|
||||||
No `FORMAT` bump. The export gains a `persona` key and the import reads it with
|
|
||||||
`.get()`, so a v2 bundle written before this change imports with an empty
|
|
||||||
persona — which is the same as not having one.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Phase 2 — what the summarizer is told
|
|
||||||
|
|
||||||
Depends on Phase 1 only for the protagonist's name. Everything else it needs is
|
|
||||||
already in the database and simply never passed.
|
|
||||||
|
|
||||||
## The cast brief
|
|
||||||
|
|
||||||
Build one short block and prepend it to both the memory prompt and the summary
|
|
||||||
prompt:
|
|
||||||
|
|
||||||
```
|
|
||||||
Cast:
|
|
||||||
- Kaelen (he/him) — the protagonist. A half-elf ranger, exiled from the
|
|
||||||
northern holds...
|
|
||||||
- Gwen — a loyal ranger and Kaelen's ally. Quick with a bow, dry-humoured.
|
|
||||||
|
|
||||||
Setting:
|
|
||||||
<adventure.memory, the plot essentials>
|
|
||||||
```
|
|
||||||
|
|
||||||
Sources, in order:
|
|
||||||
|
|
||||||
1. **Protagonist** — the Phase 1 persona. Falls back to "the player" when unset,
|
|
||||||
so the change still helps adventures with no persona.
|
|
||||||
2. **NPCs** — `stat_schema.npcs`: `name` + `desc`. Already there, never used
|
|
||||||
outside the turn prompt.
|
|
||||||
3. **Story cards** — for adventures with no RPG layer, run the existing
|
|
||||||
`_match_cards()` over the block being summarized and take the matched cards'
|
|
||||||
names and entries. This reuses the trigger code and yields exactly the
|
|
||||||
entities that appear in *that block*, not the whole world.
|
|
||||||
|
|
||||||
## Two rules for the brief
|
|
||||||
|
|
||||||
**Fixed descriptions only. No live stats.** It is tempting to include
|
|
||||||
`Gwen: trust 40 (wary)`. Do not. It makes every memory prompt different, which
|
|
||||||
loses prompt caching, and worse, it makes the same event summarized at two
|
|
||||||
different times come out framed differently — which is the problem being fixed.
|
|
||||||
|
|
||||||
**The summary gets it for free.** `_update_story_summary` builds from the memory
|
|
||||||
bullets, so better memories produce a better summary with no further change.
|
|
||||||
Pass the brief there too, because that function falls back to raw story text
|
|
||||||
when memory-writing has fallen behind.
|
|
||||||
|
|
||||||
## The prompt changes
|
|
||||||
|
|
||||||
`MEMORY_SYSTEM_PROMPT` gains an explicit framing rule:
|
|
||||||
|
|
||||||
- write in third person, never "you"
|
|
||||||
- refer to the protagonist by name
|
|
||||||
- name characters rather than using bare pronouns
|
|
||||||
|
|
||||||
Expected effect:
|
|
||||||
|
|
||||||
```
|
|
||||||
before: You entered a room and she stopped you.
|
|
||||||
after: Kaelen bribed Gwen with fifty silver to hold the north door while
|
|
||||||
he went down alone.
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Phase 1 as built
|
|
||||||
|
|
||||||
Everything above describes what shipped. Three notes on where it differs from
|
|
||||||
the sketch, and what has not been verified.
|
|
||||||
|
|
||||||
## Decisions taken during the build
|
|
||||||
|
|
||||||
**The section reads as one paragraph.** `render_persona` joins the sentence and
|
|
||||||
the description with a space rather than `SEPARATOR`. A blank line inside a
|
|
||||||
two-sentence section about one character reads as two unrelated notes.
|
|
||||||
|
|
||||||
**`Field` gained a `maxLength` prop.** The persona name is `VARCHAR(80)` and the
|
|
||||||
schema rejects 81 characters with a 422. Without the attribute the player only
|
|
||||||
learns that from a toast after typing, so the cap is now enforced in the input
|
|
||||||
as well. Every other `Field` is unaffected — the prop is optional.
|
|
||||||
|
|
||||||
**The blank-adventure button opens the modal too.** It used to create the
|
|
||||||
adventure immediately. It now collects a persona first, and passes
|
|
||||||
`title: 'Blank Adventure'` explicitly, because there is no scenario to take a
|
|
||||||
title from.
|
|
||||||
|
|
||||||
**`test_prompt_caching.py`'s fixture now sets a persona**, so every test in the
|
|
||||||
file that guards the static block runs with one present.
|
|
||||||
|
|
||||||
## Verified
|
|
||||||
|
|
||||||
- Migration 73 → 76 on a real pre-existing SQLite database: columns added, the
|
|
||||||
existing row preserved, persona empty, `render_persona` returns `""`.
|
|
||||||
- 593 backend tests pass, including 26 new ones in `test_persona.py` and 3 in
|
|
||||||
`test_bundle_v2.py`.
|
|
||||||
- `npm run build` is clean.
|
|
||||||
|
|
||||||
## Driven in a browser
|
|
||||||
|
|
||||||
Chromium via Playwright, against a fresh database with the demo scenarios
|
|
||||||
seeded. No API key needed: Insights assembles the prompt without calling a
|
|
||||||
model, so every check below runs on the real assembled context rather than on
|
|
||||||
a unit-test stub. 21/21 checks passed.
|
|
||||||
|
|
||||||
What was confirmed on screen and in the live `/context` payload:
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| The modal opens for a scenario with **no** `${...}` placeholders | it is now the only way to name a character, so it can no longer be conditional |
|
|
||||||
| The section order is real | `narrator, world_state_guide, world_state_rule, ai_instructions, **persona**, plot_essentials, …` |
|
|
||||||
| The persona is in the system half | `You are Kaelen (he/him). A half-elf ranger…` present in `prompt.system`, absent from `prompt.story` |
|
|
||||||
| The live stat line carries the name and the path | `Kaelen (player): hp 100/100 (full health), mana 30/50 (brimming)…` |
|
|
||||||
| The stat guide ties the name to the path | `Protagonist Kaelen … player.<stat>` |
|
|
||||||
| The drawer heading follows the name | rail read `KAELEN`, not `You` |
|
|
||||||
| A rename propagates | renamed to Aria in the Plot panel → drawer read `ARIA` after a reload, and the prompt read `You are Aria (he/him).` / `Aria (player):` |
|
|
||||||
| Clearing every field restores the old behavior exactly | no `persona` section, stat line back to `You:`, no `Protagonist` line in the guide |
|
|
||||||
| The blank-adventure button collects a persona and keeps its title | `title='Blank Adventure' persona='Wren'` |
|
|
||||||
| **A persona works with no RPG layer at all** | a blank adventure's sections were `['narrator', 'persona', 'length_hint']` — the case this feature was added for |
|
|
||||||
|
|
||||||
Two request failures in the run were the sandbox rather than the app: Google
|
|
||||||
Fonts is blocked by the egress policy, and the analytics beacon is aborted when
|
|
||||||
the page unloads. Neither appears with normal network access.
|
|
||||||
|
|
||||||
The driver script is not in the repo. There is no frontend test runner yet
|
|
||||||
(plan/17 stage 5), and one Playwright script is not the place to start one.
|
|
||||||
|
|
||||||
## A note for whoever runs the tests
|
|
||||||
|
|
||||||
`tiktoken` downloads `cl100k_base` from `openaipublic.blob.core.windows.net` on
|
|
||||||
first use, and 182 tests fail with a proxy error where that host is blocked.
|
|
||||||
The encoding is reconstructable offline from the npm package `js-tiktoken`,
|
|
||||||
whose `dist/ranks/cl100k_base.cjs` holds the same ranks in a compressed form —
|
|
||||||
decode it, write `<base64> <rank>` per line sorted by rank, and the result
|
|
||||||
matches the SHA-256 that `tiktoken_ext/openai_public.py` hardcodes, so the
|
|
||||||
reconstruction is verifiable rather than trusted. Drop it at
|
|
||||||
`$TIKTOKEN_CACHE_DIR/<sha1 of the URL>`.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Phase 2 as built
|
|
||||||
|
|
||||||
## The cast comes from the story cards, not from `stat_schema`
|
|
||||||
|
|
||||||
This is the one thing the sketch above got wrong, and it made the change much
|
|
||||||
smaller. `scenario_text.scenario_card_specs` already turns **every schema NPC
|
|
||||||
into a story card** on the adventure at creation, deduplicated against the
|
|
||||||
hand-written cards by name. So the cards are a single unified cast source that
|
|
||||||
covers schema NPCs, an author's own cards, and an adventure with no RPG layer,
|
|
||||||
through one path instead of three. `memorybank` never reads `stat_schema`.
|
|
||||||
|
|
||||||
## Keyword matching alone was not enough
|
|
||||||
|
|
||||||
The sketch said to run `_match_cards` over the block. Built that way first, and
|
|
||||||
it failed the exact case the change exists for.
|
|
||||||
|
|
||||||
The block `You push the door open. She grabs your arm.` matches **no** card
|
|
||||||
keyword, so the brief listed the protagonist and nobody else — leaving the
|
|
||||||
summarizer guessing at precisely the moment it was handed a brief to stop
|
|
||||||
guessing. Seed 04 gives Gwen the trigger keys `"Gwen, ranger, her"`, and even
|
|
||||||
that does not save it: the text says "she", not "her".
|
|
||||||
|
|
||||||
So the roster is **matched cards first, then topped up with the other
|
|
||||||
`character` cards** to `MAX_CAST_MEMBERS`. Places and items are not topped up —
|
|
||||||
an unmentioned tavern is not who "she" was — but a place that *is* mentioned
|
|
||||||
still matches normally.
|
|
||||||
|
|
||||||
That asymmetry with the turn prompt is deliberate. Including an untriggered card
|
|
||||||
as lore would be wrong: it is not relevant to the next sentence. Including an
|
|
||||||
untriggered character in a roster is right: the question the roster answers is
|
|
||||||
"who could these pronouns be", not "what is on stage".
|
|
||||||
|
|
||||||
## `_match_cards` became `match_cards`
|
|
||||||
|
|
||||||
Two callers now run the same rule, so it is public and exported from
|
|
||||||
`app.context`. One rule, one implementation.
|
|
||||||
|
|
||||||
## What actually gets sent
|
|
||||||
|
|
||||||
Verified against the seeded Bandit Camp scenario, with the real
|
|
||||||
`_create_due_memories` path and a stub provider:
|
|
||||||
|
|
||||||
```
|
|
||||||
Cast:
|
|
||||||
- Kaelen (he/him) — the protagonist. A half-elf ranger, exiled from the
|
|
||||||
northern holds for a killing he still won't explain.
|
|
||||||
- Bandit Camp — A rough camp of bandits in a forest clearing, holding a
|
|
||||||
stolen caravan strongbox.
|
|
||||||
- Gwen — A loyal ranger and the player's ally. Quick with a bow,
|
|
||||||
dry-humoured, fiercely protective. …
|
|
||||||
- Bandit Leader — The scarred leader of the bandit camp, guarding the
|
|
||||||
strongbox. …
|
|
||||||
|
|
||||||
Setting:
|
|
||||||
The player and Gwen, a loyal ranger ally, are raiding a bandit camp to
|
|
||||||
recover a stolen strongbox. …
|
|
||||||
|
|
||||||
Story excerpt:
|
|
||||||
|
|
||||||
… You are six paces from the strongbox when she hisses a warning. …
|
|
||||||
|
|
||||||
Memory:
|
|
||||||
```
|
|
||||||
|
|
||||||
That last line is the case in miniature: "she" is now resolvable.
|
|
||||||
|
|
||||||
**With no persona set**, the roster still lists the NPCs and the setting, and
|
|
||||||
the system prompt tells the model to call the protagonist "the player". Phase 2
|
|
||||||
therefore improves adventures that never set a persona at all.
|
|
||||||
|
|
||||||
**With no persona, no cards and no plot essentials**, the user message is byte
|
|
||||||
for byte what it was before this change — `Story excerpt:` first, no stray blank
|
|
||||||
lines. A test holds that.
|
|
||||||
|
|
||||||
## Decided, from the sketch's open questions
|
|
||||||
|
|
||||||
**Existing memories: left alone at first, and now rewritable on demand.** The
|
|
||||||
original answer was to let eviction age them out at `memory_bank_capacity` (80),
|
|
||||||
on the grounds that re-summarizing would duplicate whatever was still in the
|
|
||||||
bank, because nothing deletes the old rows. That reasoning was wrong about the
|
|
||||||
only option: a memory can be rewritten in place rather than written again.
|
|
||||||
See "Rewriting a bank written under the old prompt", at the end of this file.
|
|
||||||
|
|
||||||
**Retrieval framing mismatch: watched, not fixed.** `retrieve_memories` still
|
|
||||||
embeds the last 4 actions raw, in second person, while new memories are third
|
|
||||||
person and named. Embeddings handle paraphrase well, so this is speculative.
|
|
||||||
If retrieval quality visibly dips, prepending the same brief to the query text
|
|
||||||
is the first thing to try.
|
|
||||||
|
|
||||||
## Run with a real model, and what it changed
|
|
||||||
|
|
||||||
Run end to end against a real model. One story generated through the app's own
|
|
||||||
`build_context` a turn at a time, then **both** memory prompts run over the
|
|
||||||
**same** blocks, so the story is held constant and the prompt is the only
|
|
||||||
variable. Neither arm sees the other's output, and the model is never told what
|
|
||||||
is being measured. The control is `MEMORY_SYSTEM_PROMPT` as of commit `9cdcb55`,
|
|
||||||
read out of git rather than pasted, so it cannot drift from what shipped.
|
|
||||||
|
|
||||||
The harness is `backend/tools/memory_ab.py`, and it goes through
|
|
||||||
`OpenAICompatibleProvider` rather than calling a model directly, so the run
|
|
||||||
exercises the provider, the streaming path and `complete()`. Pointed at
|
|
||||||
`tools/claude_shim.py` it spends a Claude subscription instead of API credit:
|
|
||||||
|
|
||||||
python tools/claude_shim.py --port 8787 &
|
|
||||||
python tools/memory_ab.py --out ab.md
|
|
||||||
|
|
||||||
**The full transcript, with every memory, both summaries and the story they were
|
|
||||||
written from, is in `plan/18-appendix-memory-ab-run.md`.**
|
|
||||||
|
|
||||||
### The reported fault reproduced, and the fix held
|
|
||||||
|
|
||||||
| | words | framing |
|
|
||||||
|---|---|---|
|
|
||||||
| memory 1, before | 34 | second person — "**You** crept low through the mist…" |
|
|
||||||
| memory 2, before | 105 | third person — "**The player** asked Gwen to…" |
|
|
||||||
| memory 1, after | 72 | "**Kaelen** and Gwen crouched at dawn…" |
|
|
||||||
| memory 2, after | 68 | "**Kaelen** and Gwen infiltrated the bandit camp…" |
|
|
||||||
|
|
||||||
Two consecutive memories in one bank, written from the same story minutes apart,
|
|
||||||
in two different persons. That is the complaint, reproduced under controlled
|
|
||||||
conditions rather than argued from the prompt text.
|
|
||||||
|
|
||||||
**A second run, on a freshly generated story, qualifies that.** Its two control
|
|
||||||
memories were both "the player", consistently — the second-to-third person drift
|
|
||||||
did not recur. So the drift is a real thing a model does, seen once, not
|
|
||||||
something it does every time. Read the person-drift row as one observation.
|
|
||||||
|
|
||||||
What holds across both runs is the thing the change is actually for: **four
|
|
||||||
control memories, and not one names the protagonist. Four treatment memories,
|
|
||||||
and all four do.** The control has never been told a name. See
|
|
||||||
`plan/18-appendix-memory-ab-run-2.md`.
|
|
||||||
|
|
||||||
The control also got a fact wrong that the treatment did not. The player's move
|
|
||||||
was `grab her wrist and pull her down behind the woodpile`; the before-memory
|
|
||||||
recorded "The player asked Gwen to grab her wrist and pull her down behind the
|
|
||||||
woodpile", inverting who acted. One sample, so this is an observation rather
|
|
||||||
than a claim — but it is the failure mode the brief predicts, since without a
|
|
||||||
cast there is no way to tell whose wrist "her wrist" is.
|
|
||||||
|
|
||||||
### It also found a real problem, which is now fixed
|
|
||||||
|
|
||||||
"1-2 plain sentences" is not a length. The same model wrote 34 words for one
|
|
||||||
block and 105 for the next. A 105-word memory is a paragraph, and
|
|
||||||
`memory_top_k` injects five of them every turn, so the bank's running cost is
|
|
||||||
set by a number nobody had ever stated.
|
|
||||||
|
|
||||||
`MEMORY_MAX_WORDS = 50` now states it, and the prompt says which details to keep
|
|
||||||
when trimming: the ones a later scene could turn on. Re-run over the identical
|
|
||||||
story, the same two blocks came back at **32 and 58 words**, still naming
|
|
||||||
Kaelen, still third person, and still carrying every load-bearing fact — the
|
|
||||||
camp map, the strongbox behind the second tent, the strap frayed near through.
|
|
||||||
The second run, on different prose, came back at **54 and 55** against controls
|
|
||||||
of 89 and 107. Overshooting 50 slightly is expected: models exceed word budgets,
|
|
||||||
which is why `builder.length_hint` carries a `LENGTH_BUFFER` for the same reason.
|
|
||||||
|
|
||||||
Across both runs the control ran 34, 89, 105 and 107 words — a four-fold spread
|
|
||||||
with no budget stated anywhere. That is the number this found.
|
|
||||||
|
|
||||||
The variance is the real gain. Before, across both runs: 34 to 107. After: 32
|
|
||||||
to 58.
|
|
||||||
|
|
||||||
### What this does not show
|
|
||||||
|
|
||||||
The model behind the run is a Claude model. The app talks to an
|
|
||||||
OpenAI-compatible endpoint, and the parser in `worldstate/parse.py` exists
|
|
||||||
because weaker free models emit trailing commas and leading `+`. So this shows
|
|
||||||
the prompt is followable and that the brief supplies the missing information.
|
|
||||||
It does not show that a weaker production model complies as well. An explicit
|
|
||||||
framing rule is usually worth *more* on a weaker model, but that is an
|
|
||||||
expectation, not a measurement.
|
|
||||||
|
|
||||||
Two side observations from the same run, both pre-existing behavior working
|
|
||||||
correctly: the model sent `{"milestones.strongbox_found": false}`, `apply_delta`
|
|
||||||
refused it ("a milestone is sticky, so only true is accepted"), the refusal
|
|
||||||
reached the model through `render_refusals`, and the next turn sent `true`. The
|
|
||||||
Phase 1 persona also held across all six generated turns.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Rewriting a bank written under the old prompt
|
|
||||||
|
|
||||||
An adventure played before this change keeps a bank of unnamed, second-person
|
|
||||||
memories, and those are exactly the rows `memory_top_k` injects into every turn
|
|
||||||
from now on. Ageing them out only works for an adventure that is still being
|
|
||||||
played, and only after another 80 memories have been written. So there is a
|
|
||||||
backfill: `backend/tools/rewrite_memories.py`.
|
|
||||||
|
|
||||||
```
|
|
||||||
cd backend
|
|
||||||
python -m tools.rewrite_memories # what would change
|
|
||||||
python -m tools.rewrite_memories --write --limit 3 # try three of them
|
|
||||||
python -m tools.rewrite_memories --write --embed # the whole backfill
|
|
||||||
```
|
|
||||||
|
|
||||||
Without `--write` it makes no model calls and spends nothing. It reads whichever
|
|
||||||
database the app reads — `AIDND_DB_PATH`, or `DATABASE_URL` on a hosted deploy.
|
|
||||||
|
|
||||||
**In place, not delete-and-regenerate.** The obvious alternative is to drop the
|
|
||||||
bank and rewind `cursors.MEMORY`, letting the post-turn pass write it again.
|
|
||||||
That loses everything the row carries besides its text: whether it is pinned,
|
|
||||||
how often it has been retrieved, and the node it hangs off, which is what makes
|
|
||||||
a fork inherit the right memories and no others. It would also trickle the bank
|
|
||||||
back at `MAX_MEMORIES_PER_RUN` per turn, so an adventure nobody is playing would
|
|
||||||
never recover. Rewriting `text` keeps the row and costs one call per memory.
|
|
||||||
|
|
||||||
**One prompt assembly, not two.** `memorybank.summarize_block` is now the single
|
|
||||||
place a memory prompt is built, and both the post-turn pass and the tool call
|
|
||||||
it. A backfill that assembled its own prompt would be writing memories with a
|
|
||||||
prompt that never shipped, and nothing would report the difference.
|
|
||||||
|
|
||||||
**Reading the block back is the one genuinely new part.** A memory records
|
|
||||||
`source_start`, `source_end` and `branch_id`, and `memorybank.source_block`
|
|
||||||
turns those back into actions. The subtlety is the branch: the read has to use
|
|
||||||
the lineage of *the branch the memory was written on*, not the branch the
|
|
||||||
adventure is playing now. After a fork, the same depths hold different actions
|
|
||||||
on each side, so a read through the adventure's current path would summarize the
|
|
||||||
wrong story and say nothing about it. `test_memory_rewrite.py` builds that fork
|
|
||||||
and holds the rule.
|
|
||||||
|
|
||||||
**What it will not touch:**
|
|
||||||
|
|
||||||
- A memory with no source range — hand-written, or migrated by 62 from before
|
|
||||||
memories had coordinates. There is no block to rewrite it from, and the player
|
|
||||||
may have typed it.
|
|
||||||
- A memory whose actions have since been deleted.
|
|
||||||
- An adventure whose owner has no API key in Settings, because summarization
|
|
||||||
spends the user's own key by construction and never the shared demo key.
|
|
||||||
`--api-key`, `--model` and `--endpoint` override that — the last of these
|
|
||||||
points a run at `tools/claude_shim.py`, so a backfill can spend a Claude
|
|
||||||
subscription instead of API credit.
|
|
||||||
|
|
||||||
**The vector is cleared for every memory it rewrites**, because the stored one
|
|
||||||
describes wording that no longer exists. That takes the memory out of the ranked
|
|
||||||
bank until something embeds the new text: the app's own post-turn pass does it
|
|
||||||
`MAX_EMBED_BATCH` at a time, or `--embed` does it in the run. Re-embedding always
|
|
||||||
uses the owner's own embedding model and endpoint, never `--endpoint`, because a
|
|
||||||
vector is only meaningful against the vectors it is ranked beside.
|
|
||||||
|
|
||||||
**Stop the app before running with `--embed`.** A running process caches vectors
|
|
||||||
by memory id and expects to be the only writer (`memorybank._vector_cache`), so
|
|
||||||
a vector written from outside it can sit behind a stale cached copy until it
|
|
||||||
restarts. Clearing alone is safe at any time: an unembedded memory leaves the
|
|
||||||
catalogue, which is what the cache invalidates on.
|
|
||||||
|
|
||||||
## Running it against the hosted deploy
|
|
||||||
|
|
||||||
Two things make production different from a local database, and both are easy
|
|
||||||
to get wrong quietly.
|
|
||||||
|
|
||||||
**It holds other people's stories, and each adventure is summarized with its
|
|
||||||
owner's key.** An unfiltered `--write` would spend other people's money on
|
|
||||||
memories they did not ask to have rewritten. `--email` restricts a run to named
|
|
||||||
accounts and `--adventure` to single adventures; the dry run costs nothing and
|
|
||||||
prints the owner of each. Guests have no email and are reachable only by id,
|
|
||||||
which is the right amount of friction for rewriting a stranger's bank.
|
|
||||||
|
|
||||||
**The stored API keys are encrypted with `AIDND_SECRET_KEY`.** Render generates
|
|
||||||
that value and holds it for the web service, so a run from a checkout has to
|
|
||||||
carry the same one. With a different secret, `decrypt_secret` returns "" rather
|
|
||||||
than failing, and every adventure is reported as having no key — a run that
|
|
||||||
looks like it worked and did nothing.
|
|
||||||
|
|
||||||
```
|
|
||||||
AIDND_DATABASE_URL=<the Neon URL from the Render dashboard> \
|
|
||||||
AIDND_SECRET_KEY=<the value the web service has> \
|
|
||||||
python -m tools.rewrite_memories --email you@example.com
|
|
||||||
```
|
|
||||||
|
|
||||||
Run it from a checkout rather than from a shell on Render. The image copies
|
|
||||||
`backend/app` alone, so `tools/` is not on the box, and the free plan has no
|
|
||||||
shell anyway. The database is the same one either way.
|
|
||||||
|
|
||||||
Two smaller notes for that environment. The Neon URL to use is the direct
|
|
||||||
endpoint, not `-pooler`, for the same reason the sizing queries in STATUS use
|
|
||||||
it. And an adventure owned by a visitor playing on the shared demo key is
|
|
||||||
skipped, because summarization has never spent that key.
|
|
||||||
|
|
||||||
**The story summary is not rewritten.** It is one text per adventure rather than
|
|
||||||
a bank, and `_update_story_summary` hands the model the whole of it and asks for
|
|
||||||
an updated version under the new framing rule, so the next scheduled update
|
|
||||||
should carry it over to third person by itself. If it does not, that is a
|
|
||||||
separate and much smaller fix than this one.
|
|
||||||
@@ -358,10 +358,16 @@ Each entry:
|
|||||||
|
|
||||||
```text
|
```text
|
||||||
Before entering the abbey
|
Before entering the abbey
|
||||||
Turn 42
|
Moment 42
|
||||||
[Restore] [Rename] [Delete]
|
[Restore] [Rename] [Delete]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Moment, not Turn.** M4 closeout ruled in favour of the implemented product:
|
||||||
|
the branch panel and the tree overlay already count in moments (`forked at
|
||||||
|
moment 9`, `ends at moment 40`), so a Save Point list saying "Turn 42" would
|
||||||
|
make one screen use two words for one thing. This is a vocabulary alignment and
|
||||||
|
changes no behaviour; the number is unchanged.
|
||||||
|
|
||||||
## 26. Restore Confirmation
|
## 26. Restore Confirmation
|
||||||
|
|
||||||
Restoring is non-destructive.
|
Restoring is non-destructive.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Adventure Storyteller — Production Build Milestones
|
# Adventure Storyteller — Production Build Milestones
|
||||||
|
|
||||||
**Status:** In implementation. M1 and M2 complete (2026-09-02); M3 next
|
**Status:** In implementation. M1-M4 complete and accepted (M1 and M2: 2026-09-02; M3 and M4: 2026-09-03); M5 — Genre-Neutral Authoritative Narrative State — next to brief
|
||||||
**Base:** AI-DnD `d72f7c1bda0f34fccd84afb7a25c34eb01c901de`
|
**Base:** AI-DnD `d72f7c1bda0f34fccd84afb7a25c34eb01c901de`
|
||||||
|
|
||||||
## 1. Purpose
|
## 1. Purpose
|
||||||
@@ -97,8 +97,8 @@ A clean production build can start, open the browser UI, generate and persist st
|
|||||||
|
|
||||||
## Status: COMPLETE
|
## Status: COMPLETE
|
||||||
|
|
||||||
Accepted 2026-09-02. Evidence: `planning/reports/M1-BASELINE-REPORT.md` (run
|
Accepted 2026-09-02. Evidence: `planning/archive/milestone-reports/M1-BASELINE-REPORT.md` (run
|
||||||
logs and packet captures) and `planning/reports/M1-IMPLEMENTATION-REPORT.md`
|
logs and packet captures) and `planning/archive/milestone-reports/M1-IMPLEMENTATION-REPORT.md`
|
||||||
(review report). A01-A06, H01-H03 and H11 all pass on runtime evidence; 648
|
(review report). A01-A06, H01-H03 and H11 all pass on runtime evidence; 648
|
||||||
backend tests pass, including with no route to the Internet.
|
backend tests pass, including with no route to the Internet.
|
||||||
|
|
||||||
@@ -168,8 +168,8 @@ The codebase has a narrow single-user/local-only surface and the inherited story
|
|||||||
|
|
||||||
## Status: COMPLETE
|
## Status: COMPLETE
|
||||||
|
|
||||||
Accepted 2026-09-02. Evidence: `planning/reports/M2-BASELINE-REPORT.md`
|
Accepted 2026-09-02. Evidence: `planning/archive/milestone-reports/M2-BASELINE-REPORT.md`
|
||||||
(measurements) and `planning/reports/M2-IMPLEMENTATION-REPORT.md` (review);
|
(measurements) and `planning/archive/milestone-reports/M2-IMPLEMENTATION-REPORT.md` (review);
|
||||||
verdict *accept with non-blocking debt, proceed to M3*. Implementation is
|
verdict *accept with non-blocking debt, proceed to M3*. Implementation is
|
||||||
commit `8c65ae9`, and the three defects the review found are commit `8652fe7`
|
commit `8c65ae9`, and the three defects the review found are commit `8652fe7`
|
||||||
— see the closeout note appended to both reports.
|
— see the closeout note appended to both reports.
|
||||||
@@ -193,9 +193,10 @@ commit `8c65ae9`, and the three defects the review found are commit `8652fe7`
|
|||||||
**Debt carried forward, none of it blocking M3:** `Settings.model` still
|
**Debt carried forward, none of it blocking M3:** `Settings.model` still
|
||||||
defaults to `""` with nothing prompting for it (M8); inert legacy tables and
|
defaults to `""` with nothing prompting for it (M8); inert legacy tables and
|
||||||
columns await a cleanup migration once the schema settles, after M3/M5; there
|
columns await a cleanup migration once the schema settles, after M3/M5; there
|
||||||
are still no frontend tests (M8); `docs/*.html`, upstream's project site and not
|
are still no frontend tests (M8). `docs/*.html`, upstream's project site, was
|
||||||
served by the app, still links Google Fonts. Full table in the implementation
|
listed here as still linking Google Fonts; the whole inherited `docs/` tree was
|
||||||
report §P.
|
deleted in the 2026-09-03 documentation pass, which closes that item. Full table
|
||||||
|
in the implementation report §P.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -247,6 +248,51 @@ Must cover:
|
|||||||
|
|
||||||
History operations are non-destructive, Redo works, divergence preserves old futures, and export/import reopens at the exact active head.
|
History operations are non-destructive, Redo works, divergence preserves old futures, and export/import reopens at the exact active head.
|
||||||
|
|
||||||
|
## Status: COMPLETE
|
||||||
|
|
||||||
|
Accepted 2026-09-03. Evidence: `planning/archive/milestone-reports/M3-IMPLEMENTATION-REPORT.md`,
|
||||||
|
which is M3's primary evidence record — no separate baseline report was produced,
|
||||||
|
so that document carries the raw counts and runtime observations as well as the
|
||||||
|
review. The architecture is recorded in **ADR 012**.
|
||||||
|
|
||||||
|
**Capabilities M3 delivered, which later milestones inherit rather than build:**
|
||||||
|
|
||||||
|
- **Undo that deletes zero accepted turns** — measured directly on row identity:
|
||||||
|
15 rows before five Undos, 15 after,
|
||||||
|
- **Redo**, round-tripping exactly (head 14 → 4 → 14) in transcript and in state,
|
||||||
|
- a **single head-movement mechanism** every position change goes through, and a
|
||||||
|
**single capped lineage** that bounds every read of the story,
|
||||||
|
- **divergence decided by the lineage** rather than by a flag: the first write
|
||||||
|
below a moved-back head forks, the displaced future keeps its rows, and
|
||||||
|
ordinary Redo stops offering it with nothing to invalidate,
|
||||||
|
- **branch-scoped memory isolation preserved for free** — a memory past the head
|
||||||
|
is unretrievable and becomes eligible again on Redo, with no pruning and no
|
||||||
|
re-embedding,
|
||||||
|
- **an export that carries the reader's position**, so a campaign exported after
|
||||||
|
two Undos imports still undone, with pre-M3 bundles opening at their tip,
|
||||||
|
- **Undo across fork points** to the campaign opening, and refusal to change what
|
||||||
|
a turn says while story descends from it off screen — both ratified in
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` (§5, §10, §14A).
|
||||||
|
|
||||||
|
**Closeout condition — CLOSED at M4 closeout (2026-09-03).** M3 was accepted with
|
||||||
|
one condition outstanding: the required **browser smoke test had not been
|
||||||
|
performed**, because no session in which M3 was implemented or reviewed had a
|
||||||
|
browser available, leaving the DOM-level behaviour of the Redo button and its
|
||||||
|
disabled states unverified by observation.
|
||||||
|
|
||||||
|
That condition is now discharged. A real Firefox 154.0.1, driven through
|
||||||
|
geckodriver, exercised M3's controls in the rendered application: Undo enabled
|
||||||
|
and Redo disabled at the tip, two Undos moving the transcript back, Redo becoming
|
||||||
|
enabled and returning the original tip exactly, Retry and the take pager, and a
|
||||||
|
divergent write retiring Redo with no stale old-future text on screen. It passed.
|
||||||
|
Evidence: `planning/reports/M4-IMPLEMENTATION-REPORT.md` §W.7.
|
||||||
|
|
||||||
|
**Debt carried forward, none of it blocking M4:** full narrator-edit state
|
||||||
|
re-evaluation is deferred to M5 (`STORY-BRANCH-SEMANTICS.md` §14A records the
|
||||||
|
interim refusal); `POST /adventures/import` returns every branch's rows rather
|
||||||
|
than a head-capped window (inherited, harmless in the UI); `ActionPage` is
|
||||||
|
constructed in two places. Full table in the implementation report §S.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
# M4 — Named Save Points / Checkpoints
|
# M4 — Named Save Points / Checkpoints
|
||||||
@@ -283,6 +329,147 @@ Add durable user-facing Save Points on top of the active-head model.
|
|||||||
|
|
||||||
The user can create a named Save Point, continue, restart, restore it, and continue differently without losing later history.
|
The user can create a named Save Point, continue, restart, restore it, and continue differently without losing later history.
|
||||||
|
|
||||||
|
## Note from M3 — reuse the head machinery, do not build a second one
|
||||||
|
|
||||||
|
A Save Point is **a durable named pointer to a recoverable story position**, and
|
||||||
|
nothing more. M3 made that position a stored coordinate and made moving to one a
|
||||||
|
row lookup plus a state restore, so restoring a Save Point is head movement with
|
||||||
|
a bounds check — not a restore system of its own.
|
||||||
|
|
||||||
|
Concretely, M4 should:
|
||||||
|
|
||||||
|
- store the coordinate, the name, and the metadata around them, and no copy of
|
||||||
|
any story;
|
||||||
|
- restore by calling M3's head-movement mechanism, so that state, transcript,
|
||||||
|
context and memory eligibility all move together exactly as they do for Undo
|
||||||
|
and Redo, and so that later history is retained rather than deleted (D13 is
|
||||||
|
already satisfied by the mechanism);
|
||||||
|
- let the existing fork-on-first-write-below-the-head rule handle divergence
|
||||||
|
after a restore, rather than forking at restore time;
|
||||||
|
- validate that a Save Point's coordinate is still on the lineage being read
|
||||||
|
before moving to it.
|
||||||
|
|
||||||
|
A second restore path is the specific failure to avoid. The Phase 0B spike put
|
||||||
|
the fork check in the write path and left Retry and add-take on the old one, and
|
||||||
|
M3's cost was reconciling them; a parallel checkpoint mover would recreate that
|
||||||
|
divergence in a place where the two paths would silently disagree about what
|
||||||
|
"restore" means. See ADR 012.
|
||||||
|
|
||||||
|
## Status: COMPLETE
|
||||||
|
|
||||||
|
Accepted 2026-09-03. Evidence: `planning/reports/M4-IMPLEMENTATION-REPORT.md`,
|
||||||
|
including its §W closeout addendum. The Definition of Done is met, and — for the
|
||||||
|
first time in this project — **verified in a real browser**.
|
||||||
|
|
||||||
|
**What M4 delivered:**
|
||||||
|
|
||||||
|
- **`checkpoints`**, a table holding a name, an optional note, and a
|
||||||
|
`(branch, depth)` coordinate — and no copy of any story. `create_all` builds
|
||||||
|
it, as it did `memories` and `branches`; migration 80 adds the index. No
|
||||||
|
backfill: nobody had named a position before M4, and inventing one would be
|
||||||
|
inventing the decision.
|
||||||
|
- **Create / list / rename / delete / restore** under
|
||||||
|
`/api/adventures/{id}/checkpoints`, campaign-scoped, with a Save Point from
|
||||||
|
another campaign a 404 rather than a restore of the wrong story.
|
||||||
|
- **Create at the active head, not the retained tip**, so a Save Point made after
|
||||||
|
two Undos names the undone position.
|
||||||
|
- **Restore that delegates**, and is the whole of the milestone's architecture:
|
||||||
|
resolve the coordinate, refuse it if it names no live turn, then
|
||||||
|
`head.move_to_node` — one function whose depth half is M3's `head.move_to`
|
||||||
|
unchanged, and whose branch half is the single assignment `switch_branch`
|
||||||
|
makes. Restore forks nothing.
|
||||||
|
- **A browser Save Point panel** — create form, list, Restore, Rename, Delete,
|
||||||
|
with both confirmations saying what is *not* destroyed — plus a Save Point
|
||||||
|
button beside Undo and Redo, where it belongs. No branch explorer, no
|
||||||
|
discarded-history browser, no merge UI.
|
||||||
|
- **Export/import of Save Points** with no format version bump, and a pre-M4
|
||||||
|
bundle importing with none.
|
||||||
|
|
||||||
|
**The one architectural decision M4 had to make**, which ADR 012 does not settle:
|
||||||
|
a Save Point can name a position on a line the story has since left, so restore
|
||||||
|
moves the branch half of the head as well — but *only* when the coordinate is not
|
||||||
|
on the path being read. Doing it unconditionally would quietly hand back an
|
||||||
|
abandoned continuation whenever a Save Point in a shared prefix was restored. No
|
||||||
|
new ADR: this is ADR 012's mechanism applied to both halves of a coordinate ADR
|
||||||
|
012 already defines, not a new architecture. `TECHNICAL-DESIGN.md` §8.8 records
|
||||||
|
it.
|
||||||
|
|
||||||
|
**Tests:** 42 in `backend/tests/test_save_points.py`, covering D11-D14, I04, L03,
|
||||||
|
E-series lineage and memory isolation after restore and divergence, the edge
|
||||||
|
cases in the brief, and the M3-database migration.
|
||||||
|
|
||||||
|
**Facts M5 inherits, and must not redesign:**
|
||||||
|
|
||||||
|
- a Save Point is a **durable story coordinate** — `(branch, depth)` — carrying
|
||||||
|
no copy of transcript, state, prompt, memory or summary;
|
||||||
|
- **restore is M3 head movement**, through the same `head.move_to` Undo and Redo
|
||||||
|
use; there is no second restore path and M5 must not add one;
|
||||||
|
- **state recovery stays snapshot/cached-position based**, never a replay of the
|
||||||
|
campaign (`TECHNICAL-DESIGN.md` §10.4). This is now load-bearing for Save
|
||||||
|
Points as well as Undo/Redo;
|
||||||
|
- **the first divergent write** after a restore creates the continuation;
|
||||||
|
restore itself never forks;
|
||||||
|
- **history a Save Point names cannot disappear** through an unrelated deletion
|
||||||
|
(§19.1);
|
||||||
|
- **M3/M4 history and Save Point semantics are infrastructure now.** M5 replaces
|
||||||
|
the state *model*; it does not revisit how the story is positioned.
|
||||||
|
|
||||||
|
**Acceptance evidence:** D11-D14, I04 and L03 all pass. D11 and L03 are
|
||||||
|
discharged by automation that crosses a **genuine OS process boundary** — one
|
||||||
|
server process writes the campaign, is killed, and a second process reads it back
|
||||||
|
— rather than by recreating a client in one process.
|
||||||
|
|
||||||
|
**The browser condition is closed, for M4 and retrospectively for M3.** A real
|
||||||
|
Firefox 154.0.1, driven through geckodriver over the W3C WebDriver protocol,
|
||||||
|
exercised the rendered DOM end to end: **44/44 checks passed**, covering M3's
|
||||||
|
Undo/Redo enable states and transcript movement, Retry and the take pager, M3
|
||||||
|
divergence and the disappearance of Redo, and every M4 Save Point operation
|
||||||
|
including both confirmations and the branch-delete warning. No console errors.
|
||||||
|
This closes the outstanding M3 condition recorded above and the equivalent M4
|
||||||
|
one.
|
||||||
|
|
||||||
|
**The three review findings were fixed during closeout:**
|
||||||
|
|
||||||
|
- **B-1** — the Save Point list was an N+1 that loaded whole `Action` rows,
|
||||||
|
narration included. It is now one bulk two-column coordinate query plus one
|
||||||
|
lineage: **53 SELECTs for 25 Save Points became 5**, and the query count no
|
||||||
|
longer moves with the length of the list.
|
||||||
|
- **B-2** — deleting a branch silently deleted the Save Points naming it. Fixed
|
||||||
|
as a **behaviour** defect, not a wording one: a branch a Save Point names can
|
||||||
|
no longer be deleted at all. The request is refused with the offending Save
|
||||||
|
Points named, the user deletes them explicitly (which deletes no story), and
|
||||||
|
the branch then goes. Both delete controls, in the branch list and in the tree
|
||||||
|
overlay, disable and explain rather than warning about a loss that no longer
|
||||||
|
happens. `STORY-BRANCH-SEMANTICS.md` **§19.1** records the rule; §28 already
|
||||||
|
required a future cleanup feature to retain checkpoint-referenced paths, and
|
||||||
|
this is that requirement applied to the deletion path that exists today.
|
||||||
|
- **B-3** — the D11/L03 automation now spawns real server processes.
|
||||||
|
|
||||||
|
**Also fixed:** creating a Save Point takes the campaign's turn lock (review §S
|
||||||
|
C-5), so "save where I am" cannot read a head that a turn in flight is about to
|
||||||
|
move. Rename and Delete deliberately do not take it — neither reads nor moves a
|
||||||
|
story position.
|
||||||
|
|
||||||
|
**Debt M4 carries forward:** the Save Point panel has no frontend test, because
|
||||||
|
the project still has no frontend test runner at all (M8) — the browser smoke
|
||||||
|
test above is a closeout procedure, not a suite. `POST /adventures/import` still
|
||||||
|
returns every branch's rows rather than a head-capped window (inherited, M3).
|
||||||
|
|
||||||
|
## Note to M5 — the instrumentation is now larger than M3 estimated
|
||||||
|
|
||||||
|
M4 added **60 tests that use inherited RPG world-state values as deterministic
|
||||||
|
instrumentation**, on top of the ~20 M3 flagged. M5 replaces that state model,
|
||||||
|
and must **move the instrumentation while preserving the behavioural
|
||||||
|
assertions**: what those tests measure is where the story is being read and what
|
||||||
|
state belongs to that position, which is exactly as true after M5 as before it.
|
||||||
|
Deleting them would delete the evidence for D11-D14, I04, L03 and the E-series.
|
||||||
|
|
||||||
|
The existing constraint stands and is now load-bearing for Save Points as well as
|
||||||
|
Undo/Redo: **historical state must remain efficiently snapshot/cache
|
||||||
|
recoverable**, so that moving to a position never becomes proportional to
|
||||||
|
campaign length (`TECHNICAL-DESIGN.md` §10.4). Restore, Undo and Redo all pay
|
||||||
|
whatever that costs.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
# M5 — Genre-Neutral Authoritative Narrative State
|
# M5 — Genre-Neutral Authoritative Narrative State
|
||||||
@@ -367,7 +554,42 @@ undo semantics and are expected to be rewritten by M3; that is separate from
|
|||||||
this instrumentation point, and they should likewise be rewritten rather than
|
this instrumentation point, and they should likewise be rewritten rather than
|
||||||
dropped.
|
dropped.
|
||||||
|
|
||||||
Evidence: `planning/reports/M2-IMPLEMENTATION-REPORT.md` §K.2, §Q.
|
Evidence: `planning/archive/milestone-reports/M2-IMPLEMENTATION-REPORT.md` §K.2, §Q.
|
||||||
|
|
||||||
|
## Note from M3 — three constraints this milestone must satisfy
|
||||||
|
|
||||||
|
**1. Keep state efficiently recoverable at a retained position.**
|
||||||
|
M3's head movement is a row lookup plus a state restore, which is why Undo, Redo
|
||||||
|
and — from M4 — Save Point restore all cost the same regardless of how far into a
|
||||||
|
campaign the position is. A state model recoverable only by replaying events from
|
||||||
|
the campaign opening would make every one of those operations proportional to
|
||||||
|
campaign length, on exactly the long campaigns this product exists for.
|
||||||
|
|
||||||
|
`TECHNICAL-DESIGN.md` §10.4 already selects a hybrid of validated events plus
|
||||||
|
snapshots/cache. M3 turns the snapshot half from a preference into a requirement:
|
||||||
|
keep per-node snapshots, or an equivalent cache with the same property, while
|
||||||
|
adding the typed event model. See ADR 012.
|
||||||
|
|
||||||
|
**2. Move the instrumentation, keep the assertions.**
|
||||||
|
The M2 note above applies with more force after M3: `test_head_cursor.py` adds
|
||||||
|
roughly a dozen more tests that express position and rollback through the
|
||||||
|
inherited gold counter. What they measure is *positional* — that the state
|
||||||
|
belonging to a story position is restored when the head moves to it, in either
|
||||||
|
direction, and that an abandoned line's state does not survive a divergence.
|
||||||
|
Those properties must still hold over whatever carries state after M5. The file's
|
||||||
|
own docstring says so.
|
||||||
|
|
||||||
|
**3. Complete the narrator edit.**
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` §14-15 requires that a narrator edit become
|
||||||
|
authoritative and that the state it implies be re-evaluated. That requirement is
|
||||||
|
intact and unimplemented: re-evaluating state from prose a user typed needs this
|
||||||
|
milestone's extraction pass. M3 shipped the safe interim behavior only — an
|
||||||
|
in-place edit is refused when story descends from the turn and is off screen
|
||||||
|
(§14A), so retained history cannot be made to disagree with itself unseen.
|
||||||
|
M5 is where §14-15 is finished: return to the state before the edited narration,
|
||||||
|
treat the edited text as accepted output, re-evaluate the implied state, create a
|
||||||
|
new continuation, and retain the original. The refusal in §14A is then replaced
|
||||||
|
by that behavior rather than kept alongside it.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -427,7 +649,7 @@ M6 therefore additionally requires:
|
|||||||
degraded the storyteller quietly and left the transcript correct, which is the
|
degraded the storyteller quietly and left the transcript correct, which is the
|
||||||
right failure direction — but it must also be a *visible* one.
|
right failure direction — but it must also be a *visible* one.
|
||||||
|
|
||||||
Evidence: `planning/reports/M2-IMPLEMENTATION-REPORT.md` §A.1, §9.1.
|
Evidence: `planning/archive/milestone-reports/M2-IMPLEMENTATION-REPORT.md` §A.1, §9.1.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
# Codex Handoff Note
|
|
||||||
|
|
||||||
**Status:** Phase 0B handoff complete; historical only.
|
|
||||||
|
|
||||||
The earlier Phase 0B execution prompts are retained for audit/history:
|
|
||||||
|
|
||||||
- `PHASE-0B-CODEX-BRIEF.md`
|
|
||||||
- `PHASE-0B-CODEX-HANDOFF.md`
|
|
||||||
|
|
||||||
Do **not** execute either as the next production task.
|
|
||||||
|
|
||||||
Phase 0B has been completed and the resulting recommendation reviewed. AI-DnD is now the selected production base, and the planning package has been revised accordingly.
|
|
||||||
|
|
||||||
The next production Codex prompt has **not** been prepared. It should be created only after the current planning-package revision is reviewed and approved. When authorized, the first implementation prompt should be derived from Production Milestone M1 in `BUILD-MILESTONES.md`, not from the Phase 0B validation briefs.
|
|
||||||
@@ -74,6 +74,14 @@ story_profile:
|
|||||||
tense: optional string
|
tense: optional string
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The active head is **stored on the campaign, not derived** from its newest turn.
|
||||||
|
This is the concept M3 implemented; the current implementation carries it as a
|
||||||
|
branch reference plus a depth on that branch rather than as a turn id, which is
|
||||||
|
an equivalent coordinate and is what the export format records. What matters
|
||||||
|
conceptually is that the position is a decision the campaign remembers: two
|
||||||
|
campaigns holding identical turns can be being read at different places, and
|
||||||
|
nothing about the turns themselves can tell them apart.
|
||||||
|
|
||||||
Campaigns also store durable narrator rules and model configuration.
|
Campaigns also store durable narrator rules and model configuration.
|
||||||
|
|
||||||
Potential model roles:
|
Potential model roles:
|
||||||
@@ -109,7 +117,26 @@ Rules:
|
|||||||
- Redo moves the active head forward while the prior continuation remains selected,
|
- Redo moves the active head forward while the prior continuation remains selected,
|
||||||
- a new write below the retained tip creates a new continuation and leaves the old future retained/disposable.
|
- a new write below the retained tip creates a new continuation and leaves the old future retained/disposable.
|
||||||
|
|
||||||
Detailed behavior will be defined separately in `STORY-BRANCH-SEMANTICS.md`.
|
`disposition` above is conceptual. As implemented in M3 it is stored as **the
|
||||||
|
fact that produced it** rather than as a word: a branch records the depth a
|
||||||
|
divergent write left it at, and when. No value means active; a value means the
|
||||||
|
story past that depth is retained history no active head is reading. The
|
||||||
|
shallowest departure wins if a branch is left more than once.
|
||||||
|
|
||||||
|
Two properties of that representation are deliberate and worth carrying in this
|
||||||
|
document:
|
||||||
|
|
||||||
|
- **Nothing reads it to decide behavior.** Whether Redo is available, what the
|
||||||
|
transcript shows, and which continuation a write belongs to are all decided by
|
||||||
|
the lineage. A stale or hand-edited disposition therefore cannot make the story
|
||||||
|
wrong; it can only mislead a cleanup or recovery feature about what is
|
||||||
|
abandoned.
|
||||||
|
- **It survives export and import.** Every row of an abandoned line is exported
|
||||||
|
either way, so the disposition is the only thing distinguishing it from an
|
||||||
|
active one in a restored campaign.
|
||||||
|
|
||||||
|
Detailed behavior will be defined separately in `STORY-BRANCH-SEMANTICS.md`;
|
||||||
|
the architecture is recorded in ADR 012.
|
||||||
|
|
||||||
## 6. Turn
|
## 6. Turn
|
||||||
|
|
||||||
@@ -183,6 +210,66 @@ checkpoint:
|
|||||||
|
|
||||||
A checkpoint is a named pointer to a recoverable story position. It should normally remain tied to the turn where it was created. Restoring it moves the campaign active head; it does not delete later retained history. A new branch is created on the first divergent write after restore, not merely because the checkpoint was opened.
|
A checkpoint is a named pointer to a recoverable story position. It should normally remain tied to the turn where it was created. Restoring it moves the campaign active head; it does not delete later retained history. A new branch is created on the first divergent write after restore, not merely because the checkpoint was opened.
|
||||||
|
|
||||||
|
As implemented in M4, the pointer is a **coordinate rather than a turn id**:
|
||||||
|
`(branch_id, depth)`, which is the same pair §4 records as the campaign's active
|
||||||
|
head and which `head.node_at` resolves. This is the equivalence §4 already draws
|
||||||
|
between a turn reference and a branch-plus-depth, applied to the same position
|
||||||
|
from the other end, and it is not a shortcut — it is the more correct pointer of
|
||||||
|
the two for this data model:
|
||||||
|
|
||||||
|
- **A coordinate follows a retry; a row id does not.** One coordinate holds
|
||||||
|
every attempt at a turn and exactly one of them is live (§7). A Save Point
|
||||||
|
names the turn, so it must land on whichever take the story currently tells.
|
||||||
|
Pinning the row would leave the pointer on a superseded attempt the reader
|
||||||
|
cannot see.
|
||||||
|
- **The user-facing term is Save Point**; `checkpoint` remains the internal name
|
||||||
|
(`BROWSER-UX-SPEC.md` §23).
|
||||||
|
|
||||||
|
The row carries the name, an optional note, the coordinate, and its timestamps.
|
||||||
|
It carries **no** copy of the transcript, the state, the prompt, a memory, a
|
||||||
|
summary, or a branch's contents. Everything a restore produces comes from the
|
||||||
|
retained history the coordinate points into.
|
||||||
|
|
||||||
|
The retry case is what settles the coordinate-versus-turn-id question, and M4
|
||||||
|
closeout measured it rather than arguing it. A Save Point named a turn whose
|
||||||
|
live row was id 17; retrying that turn made id 17 dead and id 18 live at the
|
||||||
|
same coordinate; the Save Point resolved to id 18 and restored correctly. A row
|
||||||
|
id would have pinned a take the story no longer tells. **A Save Point names a
|
||||||
|
story position, not a particular take of it.**
|
||||||
|
|
||||||
|
Three further properties of the implemented model, recorded so they are decided
|
||||||
|
rather than incidental:
|
||||||
|
|
||||||
|
- **Names are not unique**, and nothing requires them to be. No product
|
||||||
|
requirement asks for uniqueness, and two names for one moment is a reasonable
|
||||||
|
thing for a player to want.
|
||||||
|
- **Several Save Points may name the same position.** Same reason.
|
||||||
|
- **Ordinary list presentation is newest-created first.** Story order is not
|
||||||
|
something the list can honestly claim: depths on lines that have parted
|
||||||
|
company are not comparable, so ordering by depth would draw a sequence that no
|
||||||
|
reading of the story passes through. When each was made is a fact about all of
|
||||||
|
them.
|
||||||
|
|
||||||
|
None of this is genre-specific. A coordinate is a position in a story; what the
|
||||||
|
state at that position *contains* is M5's question, and changing it does not
|
||||||
|
change what a Save Point is.
|
||||||
|
|
||||||
|
Two consequences worth recording here:
|
||||||
|
|
||||||
|
- **Restore reuses the campaign's one head-movement mechanism.** It resolves the
|
||||||
|
coordinate and moves the head; nothing is reconstructed and nothing is
|
||||||
|
deleted. The branch half of the head moves only when the coordinate is not on
|
||||||
|
the path being read, which is what makes a Save Point on a departed line
|
||||||
|
restorable at all — and what keeps a Save Point in a shared prefix from
|
||||||
|
dragging the reader off the line they chose. See `TECHNICAL-DESIGN.md` §8.8.
|
||||||
|
- **A checkpoint is durable against everything but its own explicit deletion.**
|
||||||
|
No pass removes one for going stale, sitting behind the head, or naming a line
|
||||||
|
the story left (`STORY-BRANCH-SEMANTICS.md` §19). Deleting a *branch* does not
|
||||||
|
remove one either: the deletion is refused while a checkpoint names any
|
||||||
|
position in the subtree, and the user deletes the checkpoint first
|
||||||
|
(§19.1). Deleting the whole campaign removes them, which is what deleting a
|
||||||
|
campaign means.
|
||||||
|
|
||||||
## 9. Narrative Entity
|
## 9. Narrative Entity
|
||||||
|
|
||||||
An entity is a persistent thing or concept in the fictional world.
|
An entity is a persistent thing or concept in the fictional world.
|
||||||
@@ -655,6 +742,19 @@ A campaign export should be capable of preserving:
|
|||||||
|
|
||||||
The physical container format remains an implementation choice, but the export must preserve the exact active branch **and active head position**, even when the head is behind a retained tip after Undo. A ZIP containing a database plus manifest remains a strong candidate.
|
The physical container format remains an implementation choice, but the export must preserve the exact active branch **and active head position**, even when the head is behind a retained tip after Undo. A ZIP containing a database plus manifest remains a strong candidate.
|
||||||
|
|
||||||
|
As implemented in M3, the export carries the active branch, the active head
|
||||||
|
position on it, and each branch's disposition, alongside the whole retained turn
|
||||||
|
graph. **M4 added the checkpoints**, by the same rule: a position someone chose
|
||||||
|
to name cannot be recomputed from the turns, because nothing about a turn records
|
||||||
|
that it was bookmarked. The head and the checkpoints stay independent on import —
|
||||||
|
a campaign opens where its head says, never at a checkpoint merely because one is
|
||||||
|
in the file. The governing rule for this package is that an export carries what was
|
||||||
|
*chosen* and recomputes what is *derived* — and the active head moved from the
|
||||||
|
second category to the first, because once Undo stops deleting, two campaigns
|
||||||
|
with identical turns can be being read at different positions and no import can
|
||||||
|
tell which. An export written before the field existed is opened at its retained
|
||||||
|
tip, which is the position such a file recorded.
|
||||||
|
|
||||||
## 30. Deletion vs Archival
|
## 30. Deletion vs Archival
|
||||||
|
|
||||||
The system must distinguish:
|
The system must distinguish:
|
||||||
|
|||||||
@@ -57,4 +57,4 @@ Therefore:
|
|||||||
`http://…:11434`.
|
`http://…:11434`.
|
||||||
|
|
||||||
M1 implemented this (`backend/app/tlstrust.py`); see
|
M1 implemented this (`backend/app/tlstrust.py`); see
|
||||||
`planning/reports/M1-IMPLEMENTATION-REPORT.md` §G.
|
`planning/archive/milestone-reports/M1-IMPLEMENTATION-REPORT.md` §G.
|
||||||
|
|||||||
@@ -115,4 +115,4 @@ itself beyond loopback, which remains out of scope for v1.
|
|||||||
- `SECURITY-THREAT-MODEL.md` §10A, §71A item 5, §77
|
- `SECURITY-THREAT-MODEL.md` §10A, §71A item 5, §77
|
||||||
- `TECHNICAL-DESIGN.md` §5.1 item 4, §5.2
|
- `TECHNICAL-DESIGN.md` §5.1 item 4, §5.2
|
||||||
- ADR 002 (Ollama-only, and the TLS consequence), ADR 004 (local-only production)
|
- ADR 002 (Ollama-only, and the TLS consequence), ADR 004 (local-only production)
|
||||||
- `planning/reports/M2-IMPLEMENTATION-REPORT.md` §F, §K.1
|
- `planning/archive/milestone-reports/M2-IMPLEMENTATION-REPORT.md` §F, §K.1
|
||||||
|
|||||||
@@ -0,0 +1,116 @@
|
|||||||
|
# ADR 012 — Active-Head Non-Destructive History
|
||||||
|
|
||||||
|
**Status:** Accepted; implemented in M3
|
||||||
|
**Date:** 2026-09-03
|
||||||
|
|
||||||
|
## Decision
|
||||||
|
|
||||||
|
Where the story is being read and how much story is retained are **two separate
|
||||||
|
facts**, stored separately and answered by different code.
|
||||||
|
|
||||||
|
- The **active head** is a stored position — a branch and a depth on it. It is
|
||||||
|
where the story currently ends as far as the reader, the narrator prompt, and
|
||||||
|
every feature built on them are concerned.
|
||||||
|
- The **retained tip** is the deepest node still kept on the same lineage. It may
|
||||||
|
be ahead of the head.
|
||||||
|
|
||||||
|
Undo and Redo move the active head. They delete nothing, restore nothing from a
|
||||||
|
log, and recompute nothing. Every ordinary read of the story is bounded by the
|
||||||
|
head; retained story beyond it stays live in the database, reachable by Redo,
|
||||||
|
and available to a divergence.
|
||||||
|
|
||||||
|
Concretely, and as implemented:
|
||||||
|
|
||||||
|
1. The active head is persisted on the campaign, not derived from the newest
|
||||||
|
row. It is a decision, and no read may re-derive it.
|
||||||
|
2. Lineage resolution caps every entry at the head, in one place, so the
|
||||||
|
transcript, the assembled context, take/parent resolution and memory
|
||||||
|
retrieval narrow together and cannot disagree.
|
||||||
|
3. Reading past the head is possible through one narrow, named exception, and
|
||||||
|
only two callers may use it: Redo, and the check that decides whether a write
|
||||||
|
must fork.
|
||||||
|
4. The state belonging to a position is recorded on the node that produced it, so
|
||||||
|
moving the head is a row lookup plus a restore — the same cost at any
|
||||||
|
distance, in either direction.
|
||||||
|
5. Undo alone never forks. The **first write below a moved-back head** is the
|
||||||
|
divergence: it creates a new continuation, and the displaced future stays
|
||||||
|
where it was written, on the line it was written on.
|
||||||
|
6. Whether an ordinary Redo exists is decided by the lineage, not by a flag. After
|
||||||
|
a divergence the displaced future is no longer on the lineage, so there is
|
||||||
|
nothing ahead to walk into and no state to invalidate.
|
||||||
|
7. A branch the story has left records the depth it was left at and when, as
|
||||||
|
metadata that **nothing reads to decide behavior**. It exists so a divergence
|
||||||
|
is observable and so later cleanup and recovery features have something to
|
||||||
|
select on.
|
||||||
|
8. Derived work — memories, summary coverage — is anchored to the node it came
|
||||||
|
from and is therefore filtered by the same capped lineage. Undo prunes
|
||||||
|
nothing; Redo re-derives nothing.
|
||||||
|
9. Export carries the active head, because it is a chosen position rather than a
|
||||||
|
fact about the newest row. Import honors it. A file that predates the field is
|
||||||
|
opened at its tip, which is the position such a file recorded.
|
||||||
|
|
||||||
|
## Context
|
||||||
|
|
||||||
|
ADR 005 states the **product requirement**: returning to an earlier point
|
||||||
|
preserves abandoned future history rather than erasing it, and the user sees
|
||||||
|
Undo/Redo/Retry/Save Point rather than branch management. It names a movable
|
||||||
|
active head as the implementation direction and stops there.
|
||||||
|
|
||||||
|
This ADR records the **architecture selected to implement it**, as built and
|
||||||
|
demonstrated in M3. It does not restate or revise ADR 005.
|
||||||
|
|
||||||
|
The production base shipped a destructive Undo: it deleted the trailing turns,
|
||||||
|
pruned the memories covering them, and let the tip fall back to whatever
|
||||||
|
survived. That made the head a derived value, made Redo impossible, and — as
|
||||||
|
Phase 0B found — let an export silently reopen an undone campaign at its newest
|
||||||
|
retained turn.
|
||||||
|
|
||||||
|
## Alternatives Considered
|
||||||
|
|
||||||
|
- **Keep the head derived and mark rows inactive.** Rejected: every read would
|
||||||
|
need its own filter, and the filters would drift. Capping the lineage once is
|
||||||
|
what makes the whole application agree about where the story ends.
|
||||||
|
- **Rebuild state by replaying events from the opening.** Rejected: it makes the
|
||||||
|
cost of Undo proportional to campaign length, and long campaigns are the case
|
||||||
|
this product exists for.
|
||||||
|
- **Fork on Undo rather than on the first write below the head.** Rejected:
|
||||||
|
moving the head is not a decision to abandon anything — the user may be
|
||||||
|
reading, or about to Redo — and forking on every Undo fills the branch table
|
||||||
|
with branches nobody chose. Redo could not survive it.
|
||||||
|
- **Decide Redo from a stored flag.** Rejected: a flag can be stale or
|
||||||
|
hand-edited, and a wrong value would produce a wrong story. Deriving it from
|
||||||
|
the lineage cannot.
|
||||||
|
|
||||||
|
## Reason
|
||||||
|
|
||||||
|
The head is the smallest thing that can move. Making it a stored position rather
|
||||||
|
than a derived one turns Undo from an operation that destroys accepted story
|
||||||
|
into one that changes a coordinate, and everything else — Redo, divergence
|
||||||
|
preserving the old future, memory isolation, an export that reopens where the
|
||||||
|
user left it — follows from that single change rather than needing machinery of
|
||||||
|
its own.
|
||||||
|
|
||||||
|
## Consequences
|
||||||
|
|
||||||
|
- **Undo deletes zero accepted rows.** This is the invariant the architecture
|
||||||
|
exists to hold, and it is asserted directly on row identity.
|
||||||
|
- **Retained history accumulates.** v1 requires no automatic cleanup; the
|
||||||
|
disposition metadata is what a later cleanup or recovery feature will select
|
||||||
|
on.
|
||||||
|
- **Any operation that changes what the story says at a position must ask
|
||||||
|
whether story descends from that position and is off screen.** Switching the
|
||||||
|
selected take and editing a turn's text in place both must refuse in that
|
||||||
|
situation rather than act silently. See `STORY-BRANCH-SEMANTICS.md` §10 and
|
||||||
|
§14A.
|
||||||
|
- **Undo crosses fork points**, because a forked story includes the story it was
|
||||||
|
forked out of and nothing is being deleted. The floor is the campaign opening.
|
||||||
|
- **Save Points must reuse this mechanism.** A named save point is a durable
|
||||||
|
coordinate; restoring one is head movement with a bounds check. Introducing a
|
||||||
|
second restore path would reintroduce exactly the divergence this ADR removes.
|
||||||
|
- **The narrative-state model must keep state efficiently recoverable at a
|
||||||
|
position** — a per-node snapshot or an equivalent cache — or Undo, Redo and
|
||||||
|
Save Point restore all become proportional to campaign length. This is a
|
||||||
|
constraint on ADR 010's engine, not a reversal of it.
|
||||||
|
- **Every feature that reads story must read it through the capped lineage.**
|
||||||
|
Anything that queries rows directly will see retained history the story is not
|
||||||
|
telling.
|
||||||
@@ -1,272 +0,0 @@
|
|||||||
# Phase 0B — Codex Initial Validation Brief
|
|
||||||
|
|
||||||
**Status:** COMPLETE / HISTORICAL — do not execute as a production prompt
|
|
||||||
**Purpose:** Run a focused first round of local validation on the three finalist repositories and return a recommendation based on what was actually learned.
|
|
||||||
|
|
||||||
## 1. Goal
|
|
||||||
|
|
||||||
We are **not** asking you to build the production application yet.
|
|
||||||
|
|
||||||
The goal of this round is to answer one question:
|
|
||||||
|
|
||||||
> Which existing project is the best starting point for the local interactive-story application, and what important technical facts did we learn that should affect the next design step?
|
|
||||||
|
|
||||||
The three finalists are:
|
|
||||||
|
|
||||||
1. AI-DnD
|
|
||||||
https://github.com/parththakkar106/AI-DnD
|
|
||||||
|
|
||||||
2. Open Dungeon
|
|
||||||
https://github.com/newideas99/open-dungeon
|
|
||||||
|
|
||||||
3. ai-adventure
|
|
||||||
https://github.com/CaoRuiming/ai-adventure
|
|
||||||
|
|
||||||
## 2. Important Product Requirements
|
|
||||||
|
|
||||||
Use these as the main evaluation criteria.
|
|
||||||
|
|
||||||
The eventual application should be:
|
|
||||||
|
|
||||||
- browser-first,
|
|
||||||
- local-only in v1,
|
|
||||||
- based on local Ollama inference,
|
|
||||||
- single-user,
|
|
||||||
- genre-agnostic,
|
|
||||||
- persistent across restarts,
|
|
||||||
- able to retain authoritative story state separately from model prose,
|
|
||||||
- able to Undo/Redo/Retry safely,
|
|
||||||
- able to preserve named save points/checkpoints,
|
|
||||||
- able to retain abandoned history without immediately deleting it,
|
|
||||||
- able to prevent abandoned-history facts/memories from leaking into the active story,
|
|
||||||
- able to support long-term context/memory,
|
|
||||||
- able to import local knowledge,
|
|
||||||
- able to inspect what context was sent to the model,
|
|
||||||
- architecturally compatible with future local image/video/TTS/STT support.
|
|
||||||
|
|
||||||
Do not try to implement all of these now.
|
|
||||||
|
|
||||||
This round is about determining which candidate already gives us the strongest foundation.
|
|
||||||
|
|
||||||
## 3. Read Only What You Need
|
|
||||||
|
|
||||||
Start with:
|
|
||||||
|
|
||||||
1. `README.md`
|
|
||||||
2. `SPECIFICATION.md`
|
|
||||||
3. `reports/PRELIMINARY-RECOMMENDATION.md`
|
|
||||||
4. `reports/REUSE-MATRIX.md`
|
|
||||||
|
|
||||||
Then use these only when relevant to a specific experiment:
|
|
||||||
|
|
||||||
- `STORY-BRANCH-SEMANTICS.md`
|
|
||||||
- `CONTEXT-AND-MEMORY.md`
|
|
||||||
- `SECURITY-THREAT-MODEL.md`
|
|
||||||
- `TEST-CAMPAIGN-FIXTURE.md`
|
|
||||||
|
|
||||||
Do **not** read every planning document up front unless needed.
|
|
||||||
|
|
||||||
## 4. Baseline Work for Each Candidate
|
|
||||||
|
|
||||||
For each repository:
|
|
||||||
|
|
||||||
1. Clone it cleanly.
|
|
||||||
2. Record the exact commit SHA.
|
|
||||||
3. Follow the documented install instructions.
|
|
||||||
4. Run the existing tests.
|
|
||||||
5. Build/start the application.
|
|
||||||
6. Confirm the basic local story flow.
|
|
||||||
7. Record:
|
|
||||||
- test results,
|
|
||||||
- storage/database technology,
|
|
||||||
- model/provider assumptions,
|
|
||||||
- local ports,
|
|
||||||
- major runtime failures,
|
|
||||||
- obvious cloud/hosted dependencies.
|
|
||||||
|
|
||||||
Do not spend excessive time fixing unrelated upstream problems.
|
|
||||||
|
|
||||||
If a project does not run cleanly, document why and continue.
|
|
||||||
|
|
||||||
## 5. Focused Experiment A — AI-DnD
|
|
||||||
|
|
||||||
We want to know whether AI-DnD can realistically serve as the production base.
|
|
||||||
|
|
||||||
Test:
|
|
||||||
|
|
||||||
- Can it run with Ollama locally?
|
|
||||||
- Can its story-tree/history system support simple user-facing Undo/Redo/Retry behavior?
|
|
||||||
- Does state rollback work independently of heavy RPG/stat mechanics?
|
|
||||||
- Can RPG-specific state be left empty/minimal without breaking the useful history/state architecture?
|
|
||||||
- Can QuickJS/scripting and hosted/cloud-oriented features be disabled without breaking local story operation?
|
|
||||||
- Can local memory/embedding behavior work without cloud services?
|
|
||||||
- Do memories/state respect the active history path?
|
|
||||||
- Is its context/Insights system useful for showing what was sent to the model?
|
|
||||||
|
|
||||||
Use a small disposable experiment if necessary.
|
|
||||||
|
|
||||||
Do **not** start stripping the whole application down.
|
|
||||||
|
|
||||||
## 6. Focused Experiment B — Open Dungeon
|
|
||||||
|
|
||||||
We want to know how expensive it would be to fix its history model.
|
|
||||||
|
|
||||||
Test:
|
|
||||||
|
|
||||||
- Confirm how Retry/Edit/Erase affect stored history.
|
|
||||||
- Identify whether old future turns are deleted.
|
|
||||||
- Trace which parts of the application depend on that linear/destructive behavior.
|
|
||||||
- Estimate how invasive it would be to change to:
|
|
||||||
- parent-linked turns,
|
|
||||||
- active head,
|
|
||||||
- retained abandoned history,
|
|
||||||
- named checkpoints,
|
|
||||||
- lineage-safe summaries/state.
|
|
||||||
|
|
||||||
Do not implement the complete branch system.
|
|
||||||
|
|
||||||
Also record useful existing pieces:
|
|
||||||
- browser UX,
|
|
||||||
- local image generation,
|
|
||||||
- character visual continuity,
|
|
||||||
- any scene/media architecture worth reusing.
|
|
||||||
|
|
||||||
## 7. Focused Experiment C — ai-adventure
|
|
||||||
|
|
||||||
We want to know whether its strong state/privacy architecture can realistically become a browser-based Ollama application.
|
|
||||||
|
|
||||||
Test:
|
|
||||||
|
|
||||||
- Run the existing tests.
|
|
||||||
- Confirm Undo/branch/checkpoint/replay behavior.
|
|
||||||
- Identify the provider abstraction.
|
|
||||||
- Prove one local Ollama-backed story turn using the smallest practical adapter.
|
|
||||||
- Determine how tightly the core application logic is coupled to the CLI.
|
|
||||||
- Assess whether the core could sit behind a browser/API layer without moving authoritative state logic.
|
|
||||||
- Review its local lore/FTS approach for possible reuse.
|
|
||||||
|
|
||||||
Do not build a browser frontend.
|
|
||||||
|
|
||||||
## 8. Offline / Privacy Check
|
|
||||||
|
|
||||||
For each candidate, once dependencies/models are installed:
|
|
||||||
|
|
||||||
- run it with outbound Internet unavailable or blocked where practical,
|
|
||||||
- exercise basic story generation,
|
|
||||||
- note any unexpected network attempts.
|
|
||||||
|
|
||||||
We do not need a full penetration test in this round.
|
|
||||||
|
|
||||||
We do need to know:
|
|
||||||
|
|
||||||
- whether local story use truly works offline,
|
|
||||||
- whether cloud services are required,
|
|
||||||
- whether analytics/telemetry/remote assets are present,
|
|
||||||
- how difficult those paths would be to remove.
|
|
||||||
|
|
||||||
## 9. Use the Standard Fixture Selectively
|
|
||||||
|
|
||||||
Use `TEST-CAMPAIGN-FIXTURE.md` where it helps answer continuity questions.
|
|
||||||
|
|
||||||
You do not need to execute the entire fixture against every candidate.
|
|
||||||
|
|
||||||
The most important checks are:
|
|
||||||
|
|
||||||
- possession/state consistency,
|
|
||||||
- restore/undo behavior,
|
|
||||||
- abandoned-path isolation,
|
|
||||||
- whether an old discarded fact can leak into current memory/context.
|
|
||||||
|
|
||||||
## 10. What Not to Do
|
|
||||||
|
|
||||||
Do not:
|
|
||||||
|
|
||||||
- build the production fork,
|
|
||||||
- merge repositories,
|
|
||||||
- redesign the full UI,
|
|
||||||
- implement full RAG,
|
|
||||||
- implement complete branching in Open Dungeon,
|
|
||||||
- remove all RPG code from AI-DnD,
|
|
||||||
- build a browser frontend for ai-adventure,
|
|
||||||
- add image/video/TTS/STT features,
|
|
||||||
- write the final production milestone plan.
|
|
||||||
|
|
||||||
Small disposable code changes are allowed only when needed to answer the evaluation questions.
|
|
||||||
|
|
||||||
## 11. Final Deliverable
|
|
||||||
|
|
||||||
The main output from this round should be a single recommendation document:
|
|
||||||
|
|
||||||
```text
|
|
||||||
PHASE-0B-RECOMMENDATION.md
|
|
||||||
```
|
|
||||||
|
|
||||||
It should summarize what was learned, not just list test logs.
|
|
||||||
|
|
||||||
Include:
|
|
||||||
|
|
||||||
### A. Executive Recommendation
|
|
||||||
|
|
||||||
- Which repository should be the production base?
|
|
||||||
- Confidence level: high / medium / low.
|
|
||||||
- Did the initial AI-DnD recommendation hold up?
|
|
||||||
|
|
||||||
### B. What We Learned About Each Candidate
|
|
||||||
|
|
||||||
For each:
|
|
||||||
- what worked,
|
|
||||||
- what failed,
|
|
||||||
- strongest reusable pieces,
|
|
||||||
- major architectural problems,
|
|
||||||
- likely amount/type of adaptation required.
|
|
||||||
|
|
||||||
### C. Key Technical Findings
|
|
||||||
|
|
||||||
Especially:
|
|
||||||
- history/undo model,
|
|
||||||
- state rollback,
|
|
||||||
- memory isolation,
|
|
||||||
- local Ollama support,
|
|
||||||
- offline/privacy behavior,
|
|
||||||
- browser suitability,
|
|
||||||
- imported-knowledge potential,
|
|
||||||
- future media extension potential.
|
|
||||||
|
|
||||||
### D. Important Surprises
|
|
||||||
|
|
||||||
Anything that contradicts the current planning assumptions.
|
|
||||||
|
|
||||||
### E. Recommendation for Next Step
|
|
||||||
|
|
||||||
Do **not** perform the next step.
|
|
||||||
|
|
||||||
Instead recommend what should happen next, such as:
|
|
||||||
- fork AI-DnD and begin a controlled strip-down,
|
|
||||||
- perform one additional experiment first,
|
|
||||||
- reconsider Open Dungeon,
|
|
||||||
- use ai-adventure as the base instead,
|
|
||||||
- revise one of the product assumptions.
|
|
||||||
|
|
||||||
### F. Open Questions
|
|
||||||
|
|
||||||
List anything that could not be resolved in this round.
|
|
||||||
|
|
||||||
## 12. Supporting Evidence
|
|
||||||
|
|
||||||
You may also create concise supporting notes/logs for:
|
|
||||||
|
|
||||||
- baseline results,
|
|
||||||
- AI-DnD experiment,
|
|
||||||
- Open Dungeon history analysis,
|
|
||||||
- ai-adventure Ollama adapter,
|
|
||||||
- offline/network observations.
|
|
||||||
|
|
||||||
Keep them concise.
|
|
||||||
|
|
||||||
The recommendation document is the primary deliverable.
|
|
||||||
|
|
||||||
## 13. Stop Condition
|
|
||||||
|
|
||||||
When `PHASE-0B-RECOMMENDATION.md` is complete, stop.
|
|
||||||
|
|
||||||
We will take the findings back into the design discussion, re-examine the assumptions, and decide the next step before any production implementation begins.
|
|
||||||
@@ -1,277 +0,0 @@
|
|||||||
# Phase 0B — Codex Local Validation Handoff
|
|
||||||
|
|
||||||
**Status:** COMPLETE / HISTORICAL — do not execute as a production prompt
|
|
||||||
**Purpose:** Validate the Phase 0A recommendation using local builds, tests, offline runtime observation, and tightly scoped experiments.
|
|
||||||
**Stop rule:** Do not begin production implementation.
|
|
||||||
|
|
||||||
## 1. Read Before Starting
|
|
||||||
|
|
||||||
Read the package in the order listed in `README.md`.
|
|
||||||
|
|
||||||
At minimum, before modifying any finalist, read:
|
|
||||||
|
|
||||||
1. `SPECIFICATION.md`
|
|
||||||
2. `DATA-MODEL.md`
|
|
||||||
3. `STORY-BRANCH-SEMANTICS.md`
|
|
||||||
4. `CONTEXT-AND-MEMORY.md`
|
|
||||||
5. `IMPORTED-KNOWLEDGE-DESIGN.md`
|
|
||||||
6. `SECURITY-THREAT-MODEL.md`
|
|
||||||
7. `MEDIA-EXTENSION-CONTRACT.md`
|
|
||||||
8. `BROWSER-UX-SPEC.md`
|
|
||||||
9. `TEST-CAMPAIGN-FIXTURE.md`
|
|
||||||
10. `V1-ACCEPTANCE-TESTS.md`
|
|
||||||
11. `reports/PRELIMINARY-RECOMMENDATION.md`
|
|
||||||
12. `reports/REUSE-MATRIX.md`
|
|
||||||
|
|
||||||
Treat the detailed behavioral documents and acceptance tests as the target behavior. Treat `TECHNICAL-DESIGN.md` as provisional.
|
|
||||||
|
|
||||||
## 2. Finalists to Clone
|
|
||||||
|
|
||||||
Clone only these three primary finalists for Phase 0B:
|
|
||||||
|
|
||||||
1. https://github.com/parththakkar106/AI-DnD
|
|
||||||
2. https://github.com/newideas99/open-dungeon
|
|
||||||
3. https://github.com/CaoRuiming/ai-adventure
|
|
||||||
|
|
||||||
At clone time record exact commit SHA, branch/tag, date, license, dependency lockfiles, required runtimes, and documented local model/provider assumptions.
|
|
||||||
|
|
||||||
Keep each upstream clone clean. Use separate experiment branches/worktrees for disposable changes. Do not merge candidate repositories together.
|
|
||||||
|
|
||||||
## 3. Result Codes
|
|
||||||
|
|
||||||
Use consistently:
|
|
||||||
|
|
||||||
```text
|
|
||||||
PASS
|
|
||||||
PARTIAL
|
|
||||||
FAIL
|
|
||||||
NOT IMPLEMENTED
|
|
||||||
NOT APPLICABLE
|
|
||||||
```
|
|
||||||
|
|
||||||
Do not convert an untested requirement into a PASS.
|
|
||||||
|
|
||||||
## 4. Validation V0 — Environment and Baseline
|
|
||||||
|
|
||||||
For all three:
|
|
||||||
|
|
||||||
- install from documented instructions,
|
|
||||||
- run existing test suite,
|
|
||||||
- run build/lint/typecheck where applicable,
|
|
||||||
- record failures,
|
|
||||||
- record actual current test count,
|
|
||||||
- record local data paths,
|
|
||||||
- record listening ports,
|
|
||||||
- record child processes/services,
|
|
||||||
- record model/provider configuration,
|
|
||||||
- record database/storage technology.
|
|
||||||
|
|
||||||
Deliver one baseline report per project. Do not rely on README claims for test counts or feature behavior.
|
|
||||||
|
|
||||||
## 5. Validation V1 — Offline and Network Behavior
|
|
||||||
|
|
||||||
After dependencies and local models are already installed, block outbound Internet and exercise launch, story creation, 5+ turns, restart/resume, summaries, memory/embeddings if present, Retry, Undo/rewind, checkpoint/branch features if present, `.txt`/`.md` import if present, and Open Dungeon local image generation if configured.
|
|
||||||
|
|
||||||
Capture open sockets, DNS attempts, HTTP(S)/WebSocket destinations, and which feature caused each request.
|
|
||||||
|
|
||||||
Use `SECURITY-THREAT-MODEL.md` and acceptance groups A, G, and H.
|
|
||||||
|
|
||||||
Pass condition for target v1 operation:
|
|
||||||
|
|
||||||
> Story content, imported knowledge, prompt/context data, and media prompts do not leave loopback or explicitly approved local endpoints.
|
|
||||||
|
|
||||||
## 6. Standard Fixture Use
|
|
||||||
|
|
||||||
Use `TEST-CAMPAIGN-FIXTURE.md` as the standard narrative test bed.
|
|
||||||
|
|
||||||
Where a finalist cannot represent the fixture directly, map it as closely as possible, document the mismatch, and do not silently change expected truth/state to suit the candidate.
|
|
||||||
|
|
||||||
Important checks:
|
|
||||||
|
|
||||||
- Mara's knowledge boundaries,
|
|
||||||
- Silver Key ownership,
|
|
||||||
- resurrection canon,
|
|
||||||
- Canon vs Reference vs Inspiration authority,
|
|
||||||
- Path A secret followed by restore/divergence into Path B,
|
|
||||||
- abandoned-history memory isolation,
|
|
||||||
- long-term memory plant,
|
|
||||||
- checkpoint persistence,
|
|
||||||
- science-fiction variant.
|
|
||||||
|
|
||||||
## 7. Acceptance-Test Mapping
|
|
||||||
|
|
||||||
Use `V1-ACCEPTANCE-TESTS.md` as the common comparison contract. Produce a gap matrix rather than forcing each candidate to fully pass v1.
|
|
||||||
|
|
||||||
Use these interpretations:
|
|
||||||
|
|
||||||
```text
|
|
||||||
Already passes
|
|
||||||
Passes with configuration
|
|
||||||
Small adaptation
|
|
||||||
Foundational redesign
|
|
||||||
Not present
|
|
||||||
```
|
|
||||||
|
|
||||||
Prioritize high-risk groups:
|
|
||||||
|
|
||||||
- A01-A05 — local operation/persistence
|
|
||||||
- C01-C05 — canon/state
|
|
||||||
- D01-D14 — Undo/Redo/Retry/checkpoints
|
|
||||||
- E01-E04 — lineage safety
|
|
||||||
- F01-F08 — memory/context
|
|
||||||
- G01-G10 — imported knowledge where supported
|
|
||||||
- H01-H10 — security/privacy
|
|
||||||
- J01-J03 — genre independence
|
|
||||||
- K01-K04 — media readiness
|
|
||||||
- L01-L04 — data integrity
|
|
||||||
|
|
||||||
Long-run M01-M04 need not be fully executed against every candidate if disproportionate; identify production risk and existing test coverage instead.
|
|
||||||
|
|
||||||
## 8. Experiment V2 — AI-DnD Strip-Down Feasibility
|
|
||||||
|
|
||||||
Do not redesign the application.
|
|
||||||
|
|
||||||
Answer:
|
|
||||||
|
|
||||||
1. Can a scenario run with RPG stats absent, empty, or minimal?
|
|
||||||
2. Do branch/retry/undo/tree tests operate independently of RPG mechanics?
|
|
||||||
3. Can user-facing tree complexity be hidden behind `STORY-BRANCH-SEMANTICS.md`?
|
|
||||||
4. Disable QuickJS scripting. What breaks?
|
|
||||||
5. Disable/remove hosted multi-user/auth/demo/analytics paths. What breaks locally?
|
|
||||||
6. Configure only local Ollama generation.
|
|
||||||
7. Configure only local embeddings, preferably Ollama/local.
|
|
||||||
8. Verify branch switching restores correct generic state.
|
|
||||||
9. Verify memory retrieval respects active lineage.
|
|
||||||
10. Test whether abandoned Path A facts leak into Path B.
|
|
||||||
11. Map Story Cards/world-info to Canon / Reference / Inspiration.
|
|
||||||
12. Determine whether prompt/context snapshots satisfy Context Inspector requirements.
|
|
||||||
13. Determine whether visual/scene snapshot data can be added without RPG coupling.
|
|
||||||
14. Inventory code coupled to RPG worldstate, scripting, hosted auth, analytics, remote providers, and AI Dungeon compatibility.
|
|
||||||
|
|
||||||
Estimate invasiveness by affected files/modules, not hours. Do not merge the experiment.
|
|
||||||
|
|
||||||
## 9. Experiment V3 — Open Dungeon Branch Retrofit Impact
|
|
||||||
|
|
||||||
Do not implement full branching.
|
|
||||||
|
|
||||||
Trace message CRUD, Retry, Erase, Edit, Continue, summary generation, state/character persistence, image association, and visual continuity. Confirm destructive-tail assumptions.
|
|
||||||
|
|
||||||
Design a minimal hypothetical persistence change supporting:
|
|
||||||
|
|
||||||
```text
|
|
||||||
turn/node ID
|
|
||||||
parent turn ID
|
|
||||||
active head
|
|
||||||
alternate narrator takes
|
|
||||||
retained disposable history
|
|
||||||
checkpoint pointer
|
|
||||||
lineage-safe summaries/memories
|
|
||||||
```
|
|
||||||
|
|
||||||
Use the standard fixture to reason through Undo/restore, Path A -> Path B divergence, stale summary/state risks, and image attachment after divergence.
|
|
||||||
|
|
||||||
Also inspect local image/provider patterns for reuse. Measure invasiveness; do not build the branch system.
|
|
||||||
|
|
||||||
## 10. Experiment V4 — ai-adventure Ollama / Service Boundary
|
|
||||||
|
|
||||||
1. Run existing tests unchanged.
|
|
||||||
2. Identify provider interface.
|
|
||||||
3. Prove one Ollama-backed turn using the smallest disposable adapter possible.
|
|
||||||
4. Identify modules that know about the CLI.
|
|
||||||
5. Determine whether the app/state layer can be wrapped by a browser/API service without moving authoritative logic.
|
|
||||||
6. Verify undo, branch, checkpoint, restore, replay.
|
|
||||||
7. Evaluate event/commit discipline for reuse.
|
|
||||||
8. Evaluate its FTS/lore system against `IMPORTED-KNOWLEDGE-DESIGN.md`.
|
|
||||||
9. Determine difficulty of adding semantic local retrieval while preserving lexical retrieval.
|
|
||||||
10. Check privacy boundary with the Ollama adapter.
|
|
||||||
|
|
||||||
Do not build a browser UI.
|
|
||||||
|
|
||||||
## 11. Validation V5 — Test Quality
|
|
||||||
|
|
||||||
For each finalist report actual test count, categories, branch/rollback coverage, state reconstruction, migrations, summary/memory coverage, provider mocks, offline/network tests, browser tests, security tests, flaky/failing tests, and tests requiring Internet.
|
|
||||||
|
|
||||||
Highlight which high-risk acceptance requirements already have regression coverage.
|
|
||||||
|
|
||||||
## 12. Validation V6 — Imported Knowledge Gap Analysis
|
|
||||||
|
|
||||||
Against `IMPORTED-KNOWLEDGE-DESIGN.md`, report local `.txt`/`.md` ingestion, classifications, campaign isolation, provenance, lexical/semantic search, embedding provider, enable/disable, deletion, export/import, hidden canon, prompt-injection framing, and remote URL/image behavior.
|
|
||||||
|
|
||||||
Do not implement a full new RAG subsystem during Phase 0B.
|
|
||||||
|
|
||||||
## 13. Validation V7 — Browser UX Gap Analysis
|
|
||||||
|
|
||||||
Against `BROWSER-UX-SPEC.md`, report story reading/input quality, streaming, Undo/Redo/Retry UI, alternate-take selection, edit behavior, Save Points, state inspection, knowledge management, prompt/context inspection, local-model status, and advanced complexity exposed to the user.
|
|
||||||
|
|
||||||
Explicitly identify AI-DnD components worth retaining and Open Dungeon components worth borrowing/reimplementing. Do not redesign the frontend.
|
|
||||||
|
|
||||||
## 14. Validation V8 — Future Media and Speech Readiness
|
|
||||||
|
|
||||||
Against `MEDIA-EXTENSION-CONTRACT.md`, determine whether the architecture can support future local image generation, video generation, audio/ambience, text-to-speech, and speech-to-text.
|
|
||||||
|
|
||||||
For STT, verify the architecture can support:
|
|
||||||
|
|
||||||
```text
|
|
||||||
local microphone/audio
|
|
||||||
->
|
|
||||||
local STT provider
|
|
||||||
->
|
|
||||||
editable draft text
|
|
||||||
->
|
|
||||||
normal user submission
|
|
||||||
```
|
|
||||||
|
|
||||||
STT output must not bypass the normal story commit path.
|
|
||||||
|
|
||||||
Do not implement STT/TTS/video during Phase 0B. Open Dungeon local image behavior may be exercised because it already exists.
|
|
||||||
|
|
||||||
## 15. Final Acceptance Gap Matrix
|
|
||||||
|
|
||||||
Produce a matrix organized by acceptance-test group covering A, C, D, E, F, G, H, I, J, K, L, plus UX fit. Include production impact for each gap.
|
|
||||||
|
|
||||||
## 16. Final Decision Matrix
|
|
||||||
|
|
||||||
Return:
|
|
||||||
|
|
||||||
| Question | AI-DnD | Open Dungeon | ai-adventure |
|
|
||||||
|---|---|---|---|
|
|
||||||
| Baseline builds | | | |
|
|
||||||
| Existing tests pass | | | |
|
|
||||||
| Runs offline after setup | | | |
|
|
||||||
| Ollama works | | | |
|
|
||||||
| History semantics fit | | | |
|
|
||||||
| State authority fits | | | |
|
|
||||||
| Memory/lineage fits | | | |
|
|
||||||
| Imported knowledge fit | | | |
|
|
||||||
| Prompt inspection fit | | | |
|
|
||||||
| Security/local-only hardening | | | |
|
|
||||||
| Unwanted-code removal scope | | | |
|
|
||||||
| Browser UX fit | | | |
|
|
||||||
| Media extension fit | | | |
|
|
||||||
| Future TTS/STT fit | | | |
|
|
||||||
| Major blockers | | | |
|
|
||||||
|
|
||||||
## 17. Recommendation Report
|
|
||||||
|
|
||||||
The final recommendation should answer:
|
|
||||||
|
|
||||||
1. Which single repository should be the production base?
|
|
||||||
2. Why?
|
|
||||||
3. What are the top architectural risks?
|
|
||||||
4. What must be removed?
|
|
||||||
5. What must be generalized?
|
|
||||||
6. Which concepts/components should be reimplemented from other candidates?
|
|
||||||
7. Does Phase 0B change the preliminary AI-DnD recommendation?
|
|
||||||
8. Which open questions remain before `TECHNICAL-DESIGN.md` v1.0?
|
|
||||||
9. Are any v1 requirements likely to need reconsideration because of real technical constraints?
|
|
||||||
10. Is unlimited Undo straightforward? If not, what practical limit exists and why?
|
|
||||||
|
|
||||||
Use evidence, not repository popularity or feature count.
|
|
||||||
|
|
||||||
## 18. Stop Condition
|
|
||||||
|
|
||||||
Stop after baseline reports, offline/network evidence, three scoped experiments, test-quality report, acceptance-gap matrix, decision matrix, and final recommendation.
|
|
||||||
|
|
||||||
Do not start the production fork conversion, implement the complete branch system, build the final browser UI, implement full RAG, add video/TTS/STT, rewrite the production technical design, or write production milestones.
|
|
||||||
|
|
||||||
Return reports and experiment diffs/results for review. The fork/architecture decision will be made from those results.
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
# Planning Update Summary — Post Phase 0B Review
|
|
||||||
|
|
||||||
**Date:** 2026-09-01
|
|
||||||
**Purpose:** Review aid. This file summarizes planning changes made after accepting the ten architecture decisions from the Phase 0B review.
|
|
||||||
|
|
||||||
## New Decisions Recorded
|
|
||||||
|
|
||||||
1. AI-DnD is the production base at pinned Phase 0B commit `d72f7c1b...`.
|
|
||||||
2. AI-DnD's browser/service/story-tree/memory/context foundation is retained as the ownership center.
|
|
||||||
3. The non-destructive head-cursor Undo/Redo spike is promoted into the production design, but the disposable spike is not treated as merge-ready production code.
|
|
||||||
4. Narrative state will use explicit typed events/absolute assignments inspired by ai-adventure, not AI-DnD's relative-delta protocol.
|
|
||||||
5. ai-adventure is an implementation reference, not the product specification.
|
|
||||||
6. Open Dungeon is a UX/media reference only.
|
|
||||||
7. Abandoned history is retained and marked disposable; no automatic cleanup is required in v1.
|
|
||||||
8. Export/import must preserve active head position as part of the history work.
|
|
||||||
9. Imported knowledge will be a separate first-class subsystem rather than an extension of Story Cards.
|
|
||||||
10. Future image/video/audio/TTS/STT extension contracts remain, but no media generation is required for v1.
|
|
||||||
11. Local-only inference may span user-controlled machines: same-host Ollama is the default, but an explicitly configured trusted-LAN Ollama host is supported in v1 without exposing the storyteller UI/API to the LAN.
|
|
||||||
|
|
||||||
## Documents Materially Revised
|
|
||||||
|
|
||||||
- `README.md`
|
|
||||||
- `SPECIFICATION.md`
|
|
||||||
- `TECHNICAL-DESIGN.md`
|
|
||||||
- `DATA-MODEL.md`
|
|
||||||
- `STORY-BRANCH-SEMANTICS.md`
|
|
||||||
- `CONTEXT-AND-MEMORY.md`
|
|
||||||
- `IMPORTED-KNOWLEDGE-DESIGN.md`
|
|
||||||
- `SECURITY-THREAT-MODEL.md`
|
|
||||||
- `V1-ACCEPTANCE-TESTS.md`
|
|
||||||
- `BUILD-MILESTONES.md`
|
|
||||||
- `RESEARCH-PLAN.md`
|
|
||||||
- `CODEX-HANDOFF-NOTE.md`
|
|
||||||
- `004-local-only-production.md`
|
|
||||||
- `005-branch-preserving-history.md`
|
|
||||||
- `008-phase0-before-build-plan.md`
|
|
||||||
|
|
||||||
## New ADRs
|
|
||||||
|
|
||||||
- `009-ai-dnd-production-base.md`
|
|
||||||
- `010-explicit-typed-narrative-state-events.md`
|
|
||||||
|
|
||||||
## Historical Evidence Intentionally Preserved
|
|
||||||
|
|
||||||
The Phase 0A candidate reports and the coding agent's `PHASE-0B-RECOMMENDATION.md` remain research evidence. They may contain assumptions that are superseded by the current planning documents. They are not silently rewritten to make the historical analysis appear as if it had reached later conclusions originally.
|
|
||||||
|
|
||||||
## Coding Prompt Status
|
|
||||||
|
|
||||||
No next Codex implementation prompt is included in this revision.
|
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
# ChatGPT Project Sources — what to upload
|
||||||
|
|
||||||
|
This file exists for one purpose: to tell the repository owner which files
|
||||||
|
belong in the **InteractiveStory** ChatGPT project's Sources. It is not a
|
||||||
|
documentation index — `planning/README.md` is that.
|
||||||
|
|
||||||
|
`planning/project-sources.txt` is the same list as bare paths, one per line, for
|
||||||
|
gathering the files.
|
||||||
|
|
||||||
|
The rule behind the list: **upload what is authoritative now.** A project source
|
||||||
|
is treated as current fact by whatever reads it, so a superseded document
|
||||||
|
uploaded alongside a current one does not add context — it manufactures a
|
||||||
|
conflict.
|
||||||
|
|
||||||
|
Refresh the Sources whenever the planning package is revised (`VERSION.md`
|
||||||
|
records each revision) and whenever a milestone completes.
|
||||||
|
|
||||||
|
## REQUIRED PROJECT SOURCES
|
||||||
|
|
||||||
|
Twenty-eight files.
|
||||||
|
|
||||||
|
**Repository orientation**
|
||||||
|
|
||||||
|
```text
|
||||||
|
README.md
|
||||||
|
DEVELOPMENT.md
|
||||||
|
PROVENANCE.md
|
||||||
|
```
|
||||||
|
|
||||||
|
**The planning index and its version**
|
||||||
|
|
||||||
|
```text
|
||||||
|
planning/README.md
|
||||||
|
planning/VERSION.md
|
||||||
|
```
|
||||||
|
|
||||||
|
**Product requirements and design**
|
||||||
|
|
||||||
|
```text
|
||||||
|
planning/SPECIFICATION.md
|
||||||
|
planning/TECHNICAL-DESIGN.md
|
||||||
|
planning/DATA-MODEL.md
|
||||||
|
planning/STORY-BRANCH-SEMANTICS.md
|
||||||
|
planning/CONTEXT-AND-MEMORY.md
|
||||||
|
planning/IMPORTED-KNOWLEDGE-DESIGN.md
|
||||||
|
planning/SECURITY-THREAT-MODEL.md
|
||||||
|
planning/MEDIA-EXTENSION-CONTRACT.md
|
||||||
|
planning/BROWSER-UX-SPEC.md
|
||||||
|
```
|
||||||
|
|
||||||
|
**Implementation plan and acceptance contract**
|
||||||
|
|
||||||
|
```text
|
||||||
|
planning/BUILD-MILESTONES.md
|
||||||
|
planning/V1-ACCEPTANCE-TESTS.md
|
||||||
|
planning/TEST-CAMPAIGN-FIXTURE.md
|
||||||
|
```
|
||||||
|
|
||||||
|
**The active ADRs** — all eleven. They are short, and each one closes a question
|
||||||
|
that will otherwise be reopened.
|
||||||
|
|
||||||
|
```text
|
||||||
|
planning/DECISIONS/001-browser-first.md
|
||||||
|
planning/DECISIONS/002-ollama-only-v1.md
|
||||||
|
planning/DECISIONS/003-authoritative-local-state.md
|
||||||
|
planning/DECISIONS/004-local-only-production.md
|
||||||
|
planning/DECISIONS/005-branch-preserving-history.md
|
||||||
|
planning/DECISIONS/006-genre-agnostic-core.md
|
||||||
|
planning/DECISIONS/007-future-media-extension.md
|
||||||
|
planning/DECISIONS/009-ai-dnd-production-base.md
|
||||||
|
planning/DECISIONS/010-explicit-typed-narrative-state-events.md
|
||||||
|
planning/DECISIONS/011-local-inference-endpoint-policy.md
|
||||||
|
planning/DECISIONS/012-active-head-non-destructive-history.md
|
||||||
|
```
|
||||||
|
|
||||||
|
ADR **008** is deliberately absent: it was the process gate requiring Phase 0 to
|
||||||
|
close before production work began, Phase 0 closed on 2026-09-01, and it is now
|
||||||
|
in `planning/archive/decisions/`.
|
||||||
|
|
||||||
|
## CURRENT-MILESTONE SOURCE
|
||||||
|
|
||||||
|
One file, and it changes as development progresses:
|
||||||
|
|
||||||
|
```text
|
||||||
|
planning/reports/M4-IMPLEMENTATION-REPORT.md
|
||||||
|
```
|
||||||
|
|
||||||
|
M4 is the most recently completed milestone, and M5 is the next to be briefed.
|
||||||
|
This report is M4's review *and* its closeout record: its §W holds the corrective
|
||||||
|
work, the process-boundary automation, and the real-browser verification that
|
||||||
|
closed the condition M3 and M4 both carried.
|
||||||
|
|
||||||
|
**Replace it, do not accumulate.** When M5's report lands, remove this one from
|
||||||
|
the project Sources and upload M5's instead. The repository does the same thing:
|
||||||
|
`planning/reports/` holds the current milestone's report and
|
||||||
|
`planning/archive/milestone-reports/` holds the rest.
|
||||||
|
|
||||||
|
## DO NOT UPLOAD AS PROJECT SOURCES
|
||||||
|
|
||||||
|
Not because these are worthless — because a source is read as current fact, and
|
||||||
|
these are not current.
|
||||||
|
|
||||||
|
- **Everything under `planning/archive/`.** The Phase 0 research reports, the
|
||||||
|
Phase 0B recommendation and spikes, the completed M1 and M2 milestone reports,
|
||||||
|
and archived ADR 008. Every conclusion they reached that still matters has
|
||||||
|
already been applied to the active documents; what is left is superseded
|
||||||
|
reasoning that will contradict the current package if uploaded beside it.
|
||||||
|
- **Older milestone reports**, once their successor exists. Uploading M1, M2 and
|
||||||
|
M3 together produces three descriptions of the same subsystem at three
|
||||||
|
different stages.
|
||||||
|
- **Source code.** `backend/`, `frontend/`, tests, migrations. The planning
|
||||||
|
package describes the system; the code is read in the repository, where it can
|
||||||
|
be searched and run.
|
||||||
|
- **Inherited upstream documentation.** Upstream AI-DnD's `plan/` build log and
|
||||||
|
`docs/` guides, generated HTML and screenshots were deleted from this
|
||||||
|
repository on 2026-09-03 for exactly this reason: they describe a hosted,
|
||||||
|
scripted, multi-user product this fork removed. Do not re-upload them from
|
||||||
|
upstream or from Git history.
|
||||||
|
- **Generated HTML, images and screenshots.** They add tokens, not facts.
|
||||||
|
- **`repo-inventory.txt` or any other local scratch output.** Not tracked, not
|
||||||
|
authoritative, stale the moment it is written.
|
||||||
@@ -1,21 +1,151 @@
|
|||||||
# Adventure Storyteller Planning Package
|
# Adventure Storyteller — Planning Package
|
||||||
|
|
||||||
**Status:** Phase 0 complete; architecture selected; **Milestones M1 and M2 implemented and accepted (2026-09-02)**.
|
**This file is the index. Start here.**
|
||||||
**Production coding:** Underway, milestone by milestone. M1 and M2 are done; M3 is the next milestone to brief.
|
|
||||||
|
|
||||||
This package contains the current product requirements, final Phase 0 architecture decisions, detailed subsystem designs, acceptance tests, research evidence, and the production milestone plan for the local-only interactive-story project.
|
**Current state:** Phase 0 complete; AI-DnD forked as the production base;
|
||||||
|
milestones **M1, M2, M3 and M4 implemented and accepted** (M3 and M4:
|
||||||
|
2026-09-03).
|
||||||
|
**Next:** **M5 — Genre-Neutral Authoritative Narrative State.** Its brief has not
|
||||||
|
been written yet, and writing it is the current action.
|
||||||
|
|
||||||
## Current Decision
|
**Package version:** see `VERSION.md`, which records what each revision changed
|
||||||
|
and why.
|
||||||
|
|
||||||
Phase 0A static research and Phase 0B local validation are complete.
|
## Where documentation lives
|
||||||
|
|
||||||
The production starting point is:
|
```text
|
||||||
|
planning/
|
||||||
|
active specifications and implementation planning — authoritative
|
||||||
|
|
||||||
|
planning/DECISIONS/
|
||||||
|
active architectural decisions (ADRs) — authoritative
|
||||||
|
|
||||||
|
planning/reports/
|
||||||
|
the current milestone's implementation report — evidence, not instruction
|
||||||
|
|
||||||
|
planning/archive/
|
||||||
|
historical evidence and completed planning material;
|
||||||
|
not authoritative for current implementation
|
||||||
|
```
|
||||||
|
|
||||||
|
You do not need to open `planning/archive/` to do ordinary milestone work. Go
|
||||||
|
there only when an active document sends you for a specific piece of historical
|
||||||
|
evidence. If an archived document contradicts an active one, the active one is
|
||||||
|
right.
|
||||||
|
|
||||||
|
Outside `planning/`: `README.md` describes the application, `DEVELOPMENT.md` is
|
||||||
|
setup and local operation, and `PROVENANCE.md` records what came from upstream
|
||||||
|
AI-DnD and what each milestone changed.
|
||||||
|
|
||||||
|
## Which document controls
|
||||||
|
|
||||||
|
When two documents appear to conflict, the one higher in this list wins:
|
||||||
|
|
||||||
|
1. `SPECIFICATION.md` — product requirements and required behavior.
|
||||||
|
2. The detailed behavior/design documents:
|
||||||
|
`STORY-BRANCH-SEMANTICS.md`, `CONTEXT-AND-MEMORY.md`,
|
||||||
|
`IMPORTED-KNOWLEDGE-DESIGN.md`, `SECURITY-THREAT-MODEL.md`,
|
||||||
|
`MEDIA-EXTENSION-CONTRACT.md`, `BROWSER-UX-SPEC.md`, `DATA-MODEL.md`.
|
||||||
|
3. `V1-ACCEPTANCE-TESTS.md` — the observable pass/fail contract.
|
||||||
|
4. `TECHNICAL-DESIGN.md` — the selected implementation architecture.
|
||||||
|
5. The ADRs in `DECISIONS/`.
|
||||||
|
6. `BUILD-MILESTONES.md` — implementation sequence. It sequences work; it does
|
||||||
|
not override product behavior.
|
||||||
|
7. The current milestone report in `reports/`, then `planning/archive/` —
|
||||||
|
evidence and historical findings, authoritative over nothing.
|
||||||
|
|
||||||
|
Two standing qualifications:
|
||||||
|
|
||||||
|
- Candidate repositories and research reports are **not** specifications. In
|
||||||
|
particular, ai-adventure is an implementation reference for selected patterns;
|
||||||
|
its behavior does not override this package.
|
||||||
|
- Where an ADR records the architecture chosen to implement a requirement stated
|
||||||
|
elsewhere, both stand: ADR 005 states the history requirement and ADR 012
|
||||||
|
states the architecture that implements it.
|
||||||
|
|
||||||
|
## The active documents
|
||||||
|
|
||||||
|
| Document | What it is for |
|
||||||
|
| --- | --- |
|
||||||
|
| `SPECIFICATION.md` | What the product must do. The top of the authority order. |
|
||||||
|
| `TECHNICAL-DESIGN.md` | The selected architecture, including what M1-M3 built, recorded as fact. |
|
||||||
|
| `DATA-MODEL.md` | Entities, the stored head, branch disposition, and the export shape. |
|
||||||
|
| `STORY-BRANCH-SEMANTICS.md` | Undo/Redo/Retry/branch/take behavior, including the M3 ratifications. |
|
||||||
|
| `CONTEXT-AND-MEMORY.md` | Prompt assembly, summarization, branch-safe memory. |
|
||||||
|
| `IMPORTED-KNOWLEDGE-DESIGN.md` | Canon / Reference / Inspiration knowledge as a first-class subsystem. |
|
||||||
|
| `SECURITY-THREAT-MODEL.md` | The trust boundary, and the inference endpoint policy as implemented. |
|
||||||
|
| `MEDIA-EXTENSION-CONTRACT.md` | The contract future image/video/audio/TTS/STT work must fit. |
|
||||||
|
| `BROWSER-UX-SPEC.md` | The browser surface, and what is deliberately not in it. |
|
||||||
|
| `BUILD-MILESTONES.md` | M1-M11, what each delivers, what is done, and the notes each milestone leaves its successors. |
|
||||||
|
| `V1-ACCEPTANCE-TESTS.md` | The pass/fail contract v1 is measured against. |
|
||||||
|
| `TEST-CAMPAIGN-FIXTURE.md` | The standard campaign the acceptance tests are run on. |
|
||||||
|
| `VERSION.md` | Package revision history: what each closeout changed. |
|
||||||
|
| `PROJECT-SOURCES.md` | Which files to upload as ChatGPT Project Sources. |
|
||||||
|
|
||||||
|
## Reading order for a new coding agent
|
||||||
|
|
||||||
|
1. This file.
|
||||||
|
2. `SPECIFICATION.md`
|
||||||
|
3. `TECHNICAL-DESIGN.md`
|
||||||
|
4. `BUILD-MILESTONES.md` — find the milestone you are being asked to do.
|
||||||
|
5. `STORY-BRANCH-SEMANTICS.md`
|
||||||
|
6. `DATA-MODEL.md`
|
||||||
|
7. `CONTEXT-AND-MEMORY.md`
|
||||||
|
8. `IMPORTED-KNOWLEDGE-DESIGN.md`
|
||||||
|
9. `SECURITY-THREAT-MODEL.md`
|
||||||
|
10. `BROWSER-UX-SPEC.md`
|
||||||
|
11. `V1-ACCEPTANCE-TESTS.md`
|
||||||
|
12. `DECISIONS/` — all of them; they are short.
|
||||||
|
13. `reports/M4-IMPLEMENTATION-REPORT.md`, for what the last milestone actually
|
||||||
|
left behind. Nothing in `planning/archive/` unless sent there.
|
||||||
|
|
||||||
|
## Architectural decisions
|
||||||
|
|
||||||
|
Active ADRs, all of which still constrain current or future work:
|
||||||
|
|
||||||
|
| ADR | Decision |
|
||||||
|
| --- | --- |
|
||||||
|
| `001-browser-first.md` | The UI is a browser application. |
|
||||||
|
| `002-ollama-only-v1.md` | Ollama-compatible local inference only; trusted-LAN HTTPS with a private CA is verified, never bypassed. |
|
||||||
|
| `003-authoritative-local-state.md` | The application owns authoritative state; the model does not. |
|
||||||
|
| `004-local-only-production.md` | Local-only runtime; no cloud, telemetry or runtime remote assets. |
|
||||||
|
| `005-branch-preserving-history.md` | History is preserved, not overwritten. **The requirement.** |
|
||||||
|
| `006-genre-agnostic-core.md` | The core state model is genre-agnostic. |
|
||||||
|
| `007-future-media-extension.md` | Media generation stays optional and decoupled. |
|
||||||
|
| `009-ai-dnd-production-base.md` | AI-DnD at `d72f7c1b…` is the production base. |
|
||||||
|
| `010-explicit-typed-narrative-state-events.md` | Explicit typed events / absolute assignments, not relative deltas. |
|
||||||
|
| `011-local-inference-endpoint-policy.md` | Address allowlist, deny by default, checked twice, TLS mandatory. |
|
||||||
|
| `012-active-head-non-destructive-history.md` | The stored active head. **The architecture implementing ADR 005.** |
|
||||||
|
|
||||||
|
`008-phase0-before-build-plan.md` was a process gate — do not begin production
|
||||||
|
work until Phase 0 closes — which Phase 0 satisfied on 2026-09-01. It is in
|
||||||
|
`archive/decisions/` and constrains nothing now. ADR numbering continues from
|
||||||
|
012; 008 is not reused.
|
||||||
|
|
||||||
|
## Milestone reports
|
||||||
|
|
||||||
|
`reports/` holds the report for the milestone most recently completed, because
|
||||||
|
that is the one the next milestone's planning has to consult:
|
||||||
|
|
||||||
|
- `reports/M4-IMPLEMENTATION-REPORT.md` — M4's review and its evidence record.
|
||||||
|
|
||||||
|
Completed earlier milestones are in `archive/milestone-reports/`, which M3's
|
||||||
|
report joined when M4's landed: a milestone report is useful during the
|
||||||
|
immediate next milestone and historical afterwards. M3's is now at
|
||||||
|
`archive/milestone-reports/M3-IMPLEMENTATION-REPORT.md`, unedited.
|
||||||
|
|
||||||
|
## The decision this package rests on
|
||||||
|
|
||||||
|
Phase 0A static research and Phase 0B local validation closed on 2026-09-01 with
|
||||||
|
one decision:
|
||||||
|
|
||||||
> **Fork AI-DnD at upstream commit `d72f7c1bda0f34fccd84afb7a25c34eb01c901de`.**
|
> **Fork AI-DnD at upstream commit `d72f7c1bda0f34fccd84afb7a25c34eb01c901de`.**
|
||||||
|
|
||||||
The selection is based on measured Phase 0B behavior, not feature count. AI-DnD already contains the highest-value structural machinery: browser UI, FastAPI service boundary, SQLite persistence, parent-linked story history, alternate takes, branch-aware state snapshots, local Ollama operation, branch-scoped memory, prompt/context inspection, streaming, export/import, and a substantial automated test suite.
|
It was chosen on measured behavior, not feature count: AI-DnD already had the
|
||||||
|
browser UI, the FastAPI service boundary, SQLite persistence, parent-linked
|
||||||
The selected composition of ideas is:
|
story history, alternate takes, branch-aware state snapshots, local Ollama
|
||||||
|
operation, branch-scoped memory, prompt inspection, streaming, export/import and
|
||||||
|
a substantial test suite. The composition of ideas selected around it:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
AI-DnD production base
|
AI-DnD production base
|
||||||
@@ -26,45 +156,20 @@ AI-DnD production base
|
|||||||
+ Gamentic provider-neutral media concepts
|
+ Gamentic provider-neutral media concepts
|
||||||
```
|
```
|
||||||
|
|
||||||
This is **not** a repository merge. AI-DnD is the ownership center. Other projects are implementation references only unless a later milestone explicitly reimplements a compatible idea.
|
This is **not** a repository merge. AI-DnD is the ownership center; the others
|
||||||
|
are implementation references only.
|
||||||
|
|
||||||
## Phase 0B Findings That Changed the Plan
|
Phase 0B also corrected several Phase 0A assumptions, and those corrections are
|
||||||
|
now built into the active documents rather than needing to be read from the
|
||||||
|
research: shipped Undo was destructive with no Redo (ADR 012 replaced it);
|
||||||
|
relative-delta world state can be semantically wrong while syntactically valid
|
||||||
|
(ADR 010); `tiktoken` and Google Fonts broke offline operation (fixed in M1);
|
||||||
|
export had to carry the head position (M3); Story Cards were not a sufficient
|
||||||
|
imported-knowledge store (`IMPORTED-KNOWLEDGE-DESIGN.md`).
|
||||||
|
|
||||||
Phase 0B confirmed the fork choice while correcting several Phase 0A assumptions:
|
The evidence is in `archive/phase0/`, and `archive/phase0/PHASE-0B-RECOMMENDATION.md`
|
||||||
|
is the strongest single document there. It is research evidence, not a
|
||||||
- AI-DnD's shipped Undo was destructive and had no Redo.
|
specification, and it was not rewritten to match later conclusions.
|
||||||
- A disposable spike proved non-destructive head-cursor Undo/Redo in three backend files while preserving branch-scoped memory isolation.
|
|
||||||
- A new continuation written after Undo can fork from the moved-back head while retaining the abandoned future.
|
|
||||||
- AI-DnD's current relative-delta world-state protocol can produce semantically wrong state under realistic context even when the proposal is syntactically valid.
|
|
||||||
- Production narrative state will therefore use explicit typed events/absolute assignments inspired by ai-adventure rather than AI-DnD's relative-delta protocol.
|
|
||||||
- AI-DnD requires offline hardening: `tiktoken` attempts a first-use CDN fetch and the SPA requests Google Fonts at runtime.
|
|
||||||
- AI-DnD's export format must preserve the active head position; otherwise export/import can silently redo an undone story.
|
|
||||||
- AI-DnD Story Cards are not a sufficient imported-knowledge store because they are not designed for the required classification, provenance, chunking, and lineage semantics.
|
|
||||||
- Open Dungeon remains useful for UX/media ideas but is no longer a serious production-fork candidate.
|
|
||||||
- ai-adventure is not the production base but is the strongest implementation reference for authoritative typed state events, head movement, checkpoints, replay, and narrow local-only behavior.
|
|
||||||
|
|
||||||
See `PHASE-0B-RECOMMENDATION.md` for the coding agent's evidence. That report is retained as research evidence; the planning documents in this package record the decisions made after reviewing it.
|
|
||||||
|
|
||||||
## Document Authority
|
|
||||||
|
|
||||||
Use the documents in this order when requirements appear to conflict:
|
|
||||||
|
|
||||||
1. `SPECIFICATION.md` — product requirements and required behavior.
|
|
||||||
2. Detailed behavior/design documents:
|
|
||||||
- `STORY-BRANCH-SEMANTICS.md`
|
|
||||||
- `CONTEXT-AND-MEMORY.md`
|
|
||||||
- `IMPORTED-KNOWLEDGE-DESIGN.md`
|
|
||||||
- `SECURITY-THREAT-MODEL.md`
|
|
||||||
- `MEDIA-EXTENSION-CONTRACT.md`
|
|
||||||
- `BROWSER-UX-SPEC.md`
|
|
||||||
- `DATA-MODEL.md`
|
|
||||||
3. `V1-ACCEPTANCE-TESTS.md` — observable pass/fail contract.
|
|
||||||
4. `TECHNICAL-DESIGN.md` — selected implementation architecture.
|
|
||||||
5. Foundational ADRs (`001-...md` through the current ADR set).
|
|
||||||
6. `BUILD-MILESTONES.md` — implementation sequence; it does not override product behavior.
|
|
||||||
7. Phase 0 research reports — evidence and historical findings.
|
|
||||||
|
|
||||||
Candidate repositories and research reports are **not** specifications. In particular, ai-adventure is an implementation reference for selected patterns; its behavior does not override this package.
|
|
||||||
|
|
||||||
## Foundational Decisions
|
## Foundational Decisions
|
||||||
|
|
||||||
@@ -95,29 +200,10 @@ The following are settled for v1:
|
|||||||
- LAN inference is distinct from LAN exposure of the storyteller UI/API; the latter is not required for v1,
|
- LAN inference is distinct from LAN exposure of the storyteller UI/API; the latter is not required for v1,
|
||||||
- future local image/video/audio/TTS/STT support remains optional and decoupled from the story engine.
|
- future local image/video/audio/TTS/STT support remains optional and decoupled from the story engine.
|
||||||
|
|
||||||
## Phase 0 Status
|
|
||||||
|
|
||||||
### Complete
|
## Still open, and deliberately so
|
||||||
|
|
||||||
- candidate discovery and triage,
|
These do not block the architecture and are not v1 requirements:
|
||||||
- static architecture/privacy/licensing review,
|
|
||||||
- local clone/build/test validation,
|
|
||||||
- real Ollama testing,
|
|
||||||
- offline/network observation,
|
|
||||||
- AI-DnD strip-down/entanglement checks,
|
|
||||||
- Open Dungeon history-retrofit analysis,
|
|
||||||
- ai-adventure Ollama/service-boundary checks,
|
|
||||||
- AI-DnD non-destructive Undo/Redo spike,
|
|
||||||
- referee/state-protocol follow-up,
|
|
||||||
- export/import head-position follow-up,
|
|
||||||
- Story Card lineage review,
|
|
||||||
- Postgres removability review,
|
|
||||||
- production fork decision,
|
|
||||||
- production architecture decision.
|
|
||||||
|
|
||||||
### Deferred to implementation/release validation
|
|
||||||
|
|
||||||
These do not block the architecture decision:
|
|
||||||
|
|
||||||
- comparative recommendation of narrator/state models for real users,
|
- comparative recommendation of narrator/state models for real users,
|
||||||
- multi-hour/100-turn long-run behavior,
|
- multi-hour/100-turn long-run behavior,
|
||||||
@@ -125,66 +211,82 @@ These do not block the architecture decision:
|
|||||||
- actual future image/video/TTS/STT provider integration,
|
- actual future image/video/TTS/STT provider integration,
|
||||||
- abandoned-history cleanup UI/policy (not required in v1).
|
- abandoned-history cleanup UI/policy (not required in v1).
|
||||||
|
|
||||||
## Recommended Reading Order for the Next Implementation Stage
|
|
||||||
|
|
||||||
Do not convert this into a coding prompt until the package review is approved.
|
## Where the work stands
|
||||||
|
|
||||||
When implementation planning resumes, read:
|
|
||||||
|
|
||||||
1. `SPECIFICATION.md`
|
|
||||||
2. `TECHNICAL-DESIGN.md`
|
|
||||||
3. `BUILD-MILESTONES.md`
|
|
||||||
4. `STORY-BRANCH-SEMANTICS.md`
|
|
||||||
5. `DATA-MODEL.md`
|
|
||||||
6. `CONTEXT-AND-MEMORY.md`
|
|
||||||
7. `IMPORTED-KNOWLEDGE-DESIGN.md`
|
|
||||||
8. `SECURITY-THREAT-MODEL.md`
|
|
||||||
9. `BROWSER-UX-SPEC.md`
|
|
||||||
10. `V1-ACCEPTANCE-TESTS.md`
|
|
||||||
11. ADRs, especially the production-base and narrative-state-event decisions
|
|
||||||
12. Phase 0B reports only as supporting evidence
|
|
||||||
|
|
||||||
## Workflow From Here
|
|
||||||
|
|
||||||
```text
|
```text
|
||||||
Phase 0 research and spikes COMPLETE
|
Phase 0 research and spikes COMPLETE
|
||||||
|
Architecture/fork decision COMPLETE
|
||||||
|
Planning package approved COMPLETE
|
||||||
|
|
|
|
||||||
v
|
v
|
||||||
Architecture/fork decision COMPLETE
|
Milestone M1 COMPLETE (2026-09-02)
|
||||||
|
fork + offline baseline archive/milestone-reports/M1-*.md
|
||||||
|
|
|
|
||||||
v
|
v
|
||||||
Planning package revision COMPLETE
|
Milestone M2 COMPLETE (2026-09-02)
|
||||||
|
|
local-only surface + endpoint archive/milestone-reports/M2-*.md
|
||||||
v
|
|
||||||
Approve planning package COMPLETE
|
|
||||||
|
|
|
||||||
v
|
|
||||||
Milestone M1 COMPLETE (2026-09-02)
|
|
||||||
fork + offline baseline see planning/reports/M1-*.md
|
|
||||||
|
|
|
||||||
v
|
|
||||||
Milestone M2 COMPLETE (2026-09-02)
|
|
||||||
local-only surface + endpoint see planning/reports/M2-*.md
|
|
||||||
policy
|
policy
|
||||||
|
|
|
|
||||||
v
|
v
|
||||||
Milestone M3 NEXT — brief not yet prepared
|
Milestone M3 COMPLETE (2026-09-03)
|
||||||
non-destructive undo/redo
|
non-destructive undo/redo, archive/milestone-reports/M3-*.md
|
||||||
|
active-head export and ADR 012
|
||||||
|
|
|
|
||||||
v
|
v
|
||||||
Implement and review milestone-by-milestone
|
Milestone M4 COMPLETE (2026-09-03)
|
||||||
|
named Save Points reports/M4-IMPLEMENTATION-REPORT.md
|
||||||
|
| browser verification: PASS (M3 + M4)
|
||||||
|
v
|
||||||
|
M5-M11, one at a time see BUILD-MILESTONES.md
|
||||||
```
|
```
|
||||||
|
|
||||||
## Stop Rule
|
## Stop Rule
|
||||||
|
|
||||||
**One milestone at a time. Do not begin a milestone before its brief exists.**
|
**One milestone at a time. Do not begin a milestone before its brief exists.**
|
||||||
|
|
||||||
M1 and M2 are complete and accepted; the evidence is in `reports/M1-*.md` and
|
**No M5 brief has been prepared.** Writing one is the current action, informed by
|
||||||
`reports/M2-*.md`. **No M3 brief has been prepared.** The current action is to
|
the M4 report's §U readiness assessment and by the note `BUILD-MILESTONES.md`
|
||||||
write one, informed by the post-M2 corrections below and by
|
attaches to M5 — in particular that M4 added 55 tests using the inherited
|
||||||
`reports/M2-IMPLEMENTATION-REPORT.md` §Q, which records that M3's chokepoints
|
world-state values as deterministic instrumentation, which M5 must **move rather
|
||||||
were left untouched or simplified by M2 and that the Phase 0B undo/redo spike
|
than delete**.
|
||||||
still applies.
|
|
||||||
|
**No conditions remain open on M1-M4.** The browser smoke condition that M3 and
|
||||||
|
M4 both carried was satisfied at M4 closeout: a real Firefox exercised the
|
||||||
|
rendered DOM for both milestones' controls, 44/44 checks passing. The M3 report's
|
||||||
|
§M.2 and the M4 report's §M record the condition as it stood; the M4 report's §W
|
||||||
|
records it closed.
|
||||||
|
|
||||||
|
## What each milestone closeout corrected
|
||||||
|
|
||||||
|
These tables are the audit trail: what implementation evidence forced back into
|
||||||
|
the planning package, milestone by milestone. `VERSION.md` narrates the same
|
||||||
|
changes.
|
||||||
|
|
||||||
|
### Post-M3 corrections applied (2026-09-03)
|
||||||
|
|
||||||
|
M3's review recommended planning changes and, following the M2 pattern, reported
|
||||||
|
rather than applied them. All are now applied, together with the closeout work
|
||||||
|
the milestone itself required:
|
||||||
|
|
||||||
|
| Document | Correction |
|
||||||
|
| --- | --- |
|
||||||
|
| `DECISIONS/012-active-head-non-destructive-history.md` | **New ADR.** The architecture selected to implement ADR 005: head stored not derived, one capped read path, one movement mechanism, state from the node, divergence on first write below the head, Redo decided by the lineage, advisory disposition metadata, and the head as an exported decision. |
|
||||||
|
| `STORY-BRANCH-SEMANTICS.md` §5 | Undo crosses fork points and continues to the campaign opening. The old refusal was a consequence of destructive deletion, not a product decision. |
|
||||||
|
| `STORY-BRANCH-SEMANTICS.md` §10 | Switching which take is live is refused while a later story is off screen, with the two resolutions the user has. |
|
||||||
|
| `STORY-BRANCH-SEMANTICS.md` §14A | **New.** In-place editing before §14-15 exist: refuse when story descends from the turn and is not on screen. States explicitly that the full narrator-edit requirement stands and is completed in M5. |
|
||||||
|
| `TECHNICAL-DESIGN.md` §8.7, §9.1 | **New.** The implemented active-head model and bundle behaviour, recorded as fact. |
|
||||||
|
| `TECHNICAL-DESIGN.md` §10.4 | Constraint from M3: the snapshot half of the hybrid state model is a requirement, or head movement becomes proportional to campaign length. |
|
||||||
|
| `DATA-MODEL.md` §4, §5, §29 | The head as campaign-stored rather than derived; the branch disposition as implemented and deliberately advisory; the export as carrying a chosen position. |
|
||||||
|
| `BUILD-MILESTONES.md` M3 | Marked COMPLETE, with inherited capabilities, the open browser condition, and carried debt. |
|
||||||
|
| `BUILD-MILESTONES.md` M4 | Note: a Save Point is a durable pointer; restore by reusing M3's head movement rather than building a second restore path. |
|
||||||
|
| `BUILD-MILESTONES.md` M5 | Note: keep state efficiently recoverable at a position; move the test instrumentation rather than the assertions; complete the narrator edit. |
|
||||||
|
| `V1-ACCEPTANCE-TESTS.md` D03, D10, I07, L01 | D03's result recorded as a full pass; **D10's milestone ownership stated without weakening any pass condition**; I07's pre-M3 bundle clause added; the apparent L01/A05 conflict resolved. |
|
||||||
|
| `README.md` | Corrected to describe the current local-only single-user application. The scripting, accounts, analytics, hosted-demo, cloud-provider, Postgres and Render material described subsystems M2 removed. |
|
||||||
|
|
||||||
|
`SPECIFICATION.md` and `SECURITY-THREAT-MODEL.md` were deliberately **not**
|
||||||
|
changed. M3 altered no product requirement and touched no path in the threat
|
||||||
|
model.
|
||||||
|
|
||||||
### Post-M2 corrections applied (2026-09-03)
|
### Post-M2 corrections applied (2026-09-03)
|
||||||
|
|
||||||
|
|||||||
@@ -123,6 +123,29 @@ If the selected base architecture makes unlimited Undo substantially harder or u
|
|||||||
|
|
||||||
Phase 0B demonstrated repeated non-destructive Undo well beyond the minimum five-step requirement. The selected head-cursor design should therefore support Undo across retained active-lineage history up to the root unless a later implementation defect forces a documented exception.
|
Phase 0B demonstrated repeated non-destructive Undo well beyond the minimum five-step requirement. The selected head-cursor design should therefore support Undo across retained active-lineage history up to the root unless a later implementation defect forces a documented exception.
|
||||||
|
|
||||||
|
### Undo crosses fork points (settled in M3)
|
||||||
|
|
||||||
|
Undo continues backward through story a branch **inherited** from the line it
|
||||||
|
forked from, up to the campaign opening. It does not stop at the fork.
|
||||||
|
|
||||||
|
This reverses the behavior of the pre-M3 base, and the reversal follows from the
|
||||||
|
history model rather than from a change of mind about the product. Undo used to
|
||||||
|
delete the turns it stepped over, and the turns before a fork belong to the
|
||||||
|
parent line's story as well, so refusing at the fork was the only way to stop
|
||||||
|
one line's Undo from destroying story another line was still telling. Undo now
|
||||||
|
moves the reading position and deletes nothing, so there is nothing to protect
|
||||||
|
the parent from: a forked story includes the story it was forked out of, and
|
||||||
|
walking back through it is a reader moving backward, not a branch reaching into
|
||||||
|
another branch's history.
|
||||||
|
|
||||||
|
The only floor is the campaign opening. There is no pre-campaign position to
|
||||||
|
reach, and Undo at the opening reports that there is nothing to undo.
|
||||||
|
|
||||||
|
One consequence is worth stating plainly for anyone reading a transcript: a
|
||||||
|
single Undo on a forked story can step back over a turn that was originally
|
||||||
|
written on the line it forked from. Nothing about that turn changes; it simply
|
||||||
|
stops being part of what is currently being told.
|
||||||
|
|
||||||
## 6. State Restoration on Undo
|
## 6. State Restoration on Undo
|
||||||
|
|
||||||
Undo must restore more than visible transcript text.
|
Undo must restore more than visible transcript text.
|
||||||
@@ -242,6 +265,28 @@ User action
|
|||||||
|
|
||||||
Inactive takes should remain retained initially.
|
Inactive takes should remain retained initially.
|
||||||
|
|
||||||
|
### Selecting a take while a later story is off screen (settled in M3)
|
||||||
|
|
||||||
|
Selecting a different take is a change to what the story says at a position that
|
||||||
|
already has a story after it. While that later story is on screen, the choice is
|
||||||
|
plainly visible and the user can see what they are changing.
|
||||||
|
|
||||||
|
It is not, once the later story has been moved out of view — undone and not yet
|
||||||
|
redone, or left behind by a new continuation. Silently switching the take
|
||||||
|
underneath it would leave retained history continuing from words the story no
|
||||||
|
longer says, and the user would have no way to see that it had happened.
|
||||||
|
|
||||||
|
The system must therefore refuse to switch the selected take in that situation
|
||||||
|
and say why, rather than switching it quietly. The user resolves it by deciding
|
||||||
|
what they mean:
|
||||||
|
|
||||||
|
- **Redo**, bringing the later story back into view, and then choose freely; or
|
||||||
|
- **play the turn again from here**, which starts a new continuation and keeps
|
||||||
|
the old one as retained history.
|
||||||
|
|
||||||
|
The same rule and the same two resolutions apply to editing a turn's text in
|
||||||
|
place; see §14A.
|
||||||
|
|
||||||
## 11. Retry vs Branch
|
## 11. Retry vs Branch
|
||||||
|
|
||||||
Retry should not be presented to the user as “creating a branch.”
|
Retry should not be presented to the user as “creating a branch.”
|
||||||
@@ -332,6 +377,38 @@ Therefore the system must:
|
|||||||
|
|
||||||
The system must not simply replace visible text while leaving stale state behind.
|
The system must not simply replace visible text while leaving stale state behind.
|
||||||
|
|
||||||
|
## 14A. Editing In Place, Before §14-15 Are Implemented
|
||||||
|
|
||||||
|
§14 and §15 describe the finished behavior: a narrator edit becomes
|
||||||
|
authoritative, the state it implies is re-evaluated, a new continuation is
|
||||||
|
created, and the original narration and its future are retained. That
|
||||||
|
requirement stands in full and is **not** weakened by this section.
|
||||||
|
|
||||||
|
It is not yet built. Re-evaluating the state implied by prose a user typed
|
||||||
|
requires the authoritative narrative-state extraction that the genre-neutral
|
||||||
|
state milestone introduces, so the finished behavior is completed there. What
|
||||||
|
exists in the meantime is a plain correction: it changes the words of one turn
|
||||||
|
and re-evaluates nothing.
|
||||||
|
|
||||||
|
That correction is safe while everything descending from the turn is on screen,
|
||||||
|
because the user can see what their change has to stay consistent with. It is
|
||||||
|
not safe when a continuation descends from the turn and is **off screen** —
|
||||||
|
undone and not yet redone, or left behind by a divergence — because the edit
|
||||||
|
would then silently change the words that retained story was written from, and
|
||||||
|
nothing on screen would show it. Retained history is not permitted to be made to
|
||||||
|
disagree with itself in a way the user cannot see.
|
||||||
|
|
||||||
|
Until §14-15 are implemented, the system must therefore **refuse** an in-place
|
||||||
|
edit of a turn that has story descending from it which is not currently being
|
||||||
|
shown, and say why. The user resolves it the same two ways as §10:
|
||||||
|
|
||||||
|
- **Redo**, bringing the later story back into view; or
|
||||||
|
- **play the turn again from here**, which is the §13 shape — return to the
|
||||||
|
parent position, continue differently, and keep the old line as retained
|
||||||
|
history.
|
||||||
|
|
||||||
|
Refusing is the minimum that keeps the invariant. It is not the destination.
|
||||||
|
|
||||||
## 16. Manual State / Canon Correction
|
## 16. Manual State / Canon Correction
|
||||||
|
|
||||||
The user should be able to correct authoritative story state without rewriting prose.
|
The user should be able to correct authoritative story state without rewriting prose.
|
||||||
@@ -390,6 +467,39 @@ They should not be removed by:
|
|||||||
- branch divergence,
|
- branch divergence,
|
||||||
- ordinary history cleanup.
|
- ordinary history cleanup.
|
||||||
|
|
||||||
|
### 19.1 A checkpoint protects the history it names
|
||||||
|
|
||||||
|
Added at M4 closeout, from implementation evidence.
|
||||||
|
|
||||||
|
§19's durability rule is only meaningful if something enforces it against the
|
||||||
|
operations that delete history. Deleting a branch deletes that branch and
|
||||||
|
everything forked from it, so a checkpoint naming a position anywhere in that
|
||||||
|
subtree would go with it — and go silently, because the story is what the user
|
||||||
|
asked to delete and the named moments are not mentioned in the request.
|
||||||
|
|
||||||
|
Therefore:
|
||||||
|
|
||||||
|
- **A branch cannot be deleted while a checkpoint names a position on it, or on
|
||||||
|
any branch forked from it.** The deletion is refused, not amended: nothing is
|
||||||
|
half-deleted, and no checkpoint is quietly relocated or dropped.
|
||||||
|
- **The refusal names the checkpoints standing in the way**, so the user knows
|
||||||
|
what to act on rather than being told only that something is in the way.
|
||||||
|
- **The user resolves it by deleting the checkpoint explicitly**, which is
|
||||||
|
§19's "until explicitly deleted" being honoured rather than worked around.
|
||||||
|
Deleting a checkpoint still deletes no story (§25), so the recovery costs the
|
||||||
|
user nothing they wanted to keep.
|
||||||
|
- **The scope is the subtree**, not the named branch alone.
|
||||||
|
|
||||||
|
This is the same rule §28 already anticipates for a future cleanup feature —
|
||||||
|
*retain paths referenced by checkpoints* — applied to the one deletion path that
|
||||||
|
exists today. A cleanup or discarded-history feature added later must honour it
|
||||||
|
too, and must not acquire a way to delete a checkpoint as a side effect of
|
||||||
|
deleting history.
|
||||||
|
|
||||||
|
Campaign deletion is not an exception to this and needs no rule: deleting a
|
||||||
|
campaign deletes everything in it, checkpoints included, and that is what the
|
||||||
|
user asked for.
|
||||||
|
|
||||||
## 20. Checkpoint Restore
|
## 20. Checkpoint Restore
|
||||||
|
|
||||||
Restoring a checkpoint:
|
Restoring a checkpoint:
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ controls on a network they trust — and why the endpoint is always explicitly
|
|||||||
configured, never discovered.
|
configured, never discovered.
|
||||||
|
|
||||||
See ADR 002 (*Transport for a Trusted-LAN Endpoint*) and, for the demonstrated
|
See ADR 002 (*Transport for a Trusted-LAN Endpoint*) and, for the demonstrated
|
||||||
deployment, `planning/reports/M1-IMPLEMENTATION-REPORT.md` §F.
|
deployment, `planning/archive/milestone-reports/M1-IMPLEMENTATION-REPORT.md` §F.
|
||||||
|
|
||||||
Allowed future local paths may also include explicitly configured local media services.
|
Allowed future local paths may also include explicitly configured local media services.
|
||||||
|
|
||||||
@@ -361,6 +361,109 @@ Abandoned history must:
|
|||||||
- stop influencing current state/context/memory/summary,
|
- stop influencing current state/context/memory/summary,
|
||||||
- remain available for future recovery/cleanup features.
|
- remain available for future recovery/cleanup features.
|
||||||
|
|
||||||
|
### 8.7 As implemented in M3
|
||||||
|
|
||||||
|
M3 built this model. The following is fact rather than direction, and ADR 012
|
||||||
|
records it as the architectural decision. Sections 8.1-8.6 stand; this says how
|
||||||
|
they were realised.
|
||||||
|
|
||||||
|
**The head is stored, not derived.** A campaign carries a branch and a depth,
|
||||||
|
and that pair is the active head. No read may recompute it from the newest row —
|
||||||
|
that was the pre-M3 behavior, and it is what made Redo impossible and made an
|
||||||
|
export reopen an undone campaign at its tip.
|
||||||
|
|
||||||
|
**Lineage reads are capped at the head, in one place.** The path abstraction that
|
||||||
|
already resolved a branch's ancestry now also limits every entry to the head, so
|
||||||
|
the transcript, the assembled narrator context, take/parent resolution and memory
|
||||||
|
retrieval narrow together. There is exactly one way to read past the head — a
|
||||||
|
named, uncapped view of the same lineage — and only two callers may use it: Redo,
|
||||||
|
and the check that decides whether a write must fork. Any new feature that reads
|
||||||
|
story rows directly, rather than through the capped lineage, will see retained
|
||||||
|
history the story is not telling.
|
||||||
|
|
||||||
|
**Head movement is one mechanism.** Undo, Redo, and anything later that restores
|
||||||
|
a position resolve a target depth and then call a single move operation, which
|
||||||
|
sets the coordinate and restores the state recorded at it. Undo and Redo differ
|
||||||
|
only in which way they resolve the target. Both step over a whole turn — a
|
||||||
|
player's action and the reply to it — so the head never rests between the two
|
||||||
|
halves of one turn.
|
||||||
|
|
||||||
|
**State comes from the node, not from a replay.** Each node records the state it
|
||||||
|
left behind, so moving the head is a row lookup plus a restore: the same cost at
|
||||||
|
any distance, in either direction, and identical whether the position is reached
|
||||||
|
from in front of it or from behind. This is the property §10.4's hybrid storage
|
||||||
|
must preserve.
|
||||||
|
|
||||||
|
**Divergence is a property of the lineage, not a flag.** The first write below a
|
||||||
|
moved-back head forks; Undo alone never does. After the fork, the displaced
|
||||||
|
future is no longer on the lineage being read, so ordinary Redo finds nothing
|
||||||
|
ahead and reports that it has nowhere to go. Nothing has to be invalidated,
|
||||||
|
cleared, or kept in step.
|
||||||
|
|
||||||
|
**A branch the story leaves records the depth and time it was left**, as metadata
|
||||||
|
nothing reads to decide behavior (§8.6's "implementation-appropriate metadata").
|
||||||
|
It makes a divergence observable and gives later cleanup and recovery features
|
||||||
|
something to select on; because no decision depends on it, a stale or hand-edited
|
||||||
|
value cannot make the story wrong.
|
||||||
|
|
||||||
|
**Operations that change what the story says at a position must ask whether
|
||||||
|
story descends from that position and is off screen.** Switching which take is
|
||||||
|
live, and editing a turn's text in place, both refuse in that situation rather
|
||||||
|
than act silently, because retained history must not be made to disagree with
|
||||||
|
itself in a way the user cannot see. See `STORY-BRANCH-SEMANTICS.md` §10 and
|
||||||
|
§14A.
|
||||||
|
|
||||||
|
### 8.8 Save Points, as implemented in M4
|
||||||
|
|
||||||
|
M4 added durable named Save Points and built nothing in §8 that was not already
|
||||||
|
there. This records what the milestone establishes as fact.
|
||||||
|
|
||||||
|
**A Save Point is a name and a coordinate.** The stored row holds the name, an
|
||||||
|
optional note, and `(branch, depth)` — the same pair §8.7 calls the head. It
|
||||||
|
holds no transcript, no state, no summary, no memory, and no branch contents.
|
||||||
|
`DATA-MODEL.md` §8 describes the pointer as naming a turn; the coordinate is
|
||||||
|
that turn's address, and `DATA-MODEL.md` §8's implementation note records why
|
||||||
|
this project uses the address rather than a row id: one coordinate can hold
|
||||||
|
several attempts at a turn, and a retry replaces the live one. "Turn 42 of this
|
||||||
|
line" survives a retry; a row id would pin a take the story no longer tells.
|
||||||
|
|
||||||
|
**Restore is head movement, and nothing else.** It resolves the coordinate,
|
||||||
|
refuses it if it no longer names a live turn, and then moves the head — the
|
||||||
|
depth through §8.7's single move operation, unchanged. The transcript, the
|
||||||
|
assembled context, the state and memory eligibility all arrive together because
|
||||||
|
they already read through the one capped lineage. There is no second restore
|
||||||
|
path, no state reconstruction, no memory pruning and no separate redo stack:
|
||||||
|
D13 is satisfied by the mechanism rather than by code written to satisfy it.
|
||||||
|
|
||||||
|
**A Save Point may name a position on a line the story has left.** Save Points
|
||||||
|
survive divergence, so this is reachable in ordinary use, and the depth half of
|
||||||
|
the head cannot reach a branch the current path does not contain. Restore
|
||||||
|
therefore moves the branch half as well when, and only when, the coordinate is
|
||||||
|
not on the path being read — the same single assignment a branch switch makes.
|
||||||
|
The distinction matters in the other direction too: a Save Point in a shared
|
||||||
|
prefix must *not* drag the reader onto the ancestor, because which continuation
|
||||||
|
follows that turn is exactly what the reader has already chosen.
|
||||||
|
|
||||||
|
**Restore never forks.** Moving the head is not a decision to abandon anything.
|
||||||
|
The first write below the restored head forks, through §8.7's existing check,
|
||||||
|
and the displaced future stays retained — so Redo still walks the original
|
||||||
|
continuation until the user writes something different, and stops offering it
|
||||||
|
once they have.
|
||||||
|
|
||||||
|
**Nothing removes a Save Point but the user.** There is no cleanup pass, and none
|
||||||
|
is wanted: a Save Point pointing behind the head, or into a line the story left,
|
||||||
|
is doing its job.
|
||||||
|
|
||||||
|
That rule is enforced against the one operation that could break it. Deleting a
|
||||||
|
branch deletes everything forked from it, so a Save Point naming a position in
|
||||||
|
that subtree would go too — silently, since the story is what the user asked to
|
||||||
|
delete. **The deletion is therefore refused while any Save Point names that
|
||||||
|
subtree**, and the refusal names them. The user deletes the Save Point
|
||||||
|
explicitly, which deletes no story, and then the branch. `STORY-BRANCH-SEMANTICS.md`
|
||||||
|
§19.1 states the rule; §28 already required a future cleanup feature to retain
|
||||||
|
paths a checkpoint references, and this is that requirement applied to the
|
||||||
|
deletion path that exists today.
|
||||||
|
|
||||||
## 9. Export / Import and Head Position
|
## 9. Export / Import and Head Position
|
||||||
|
|
||||||
AI-DnD's current export carries branch information but reconstructs the imported head at the branch tip.
|
AI-DnD's current export carries branch information but reconstructs the imported head at the branch tip.
|
||||||
@@ -383,6 +486,55 @@ For compatibility with earlier bundles, import may fall back to the retained tip
|
|||||||
|
|
||||||
Export/import regression tests must include an undone campaign and verify the imported story reopens at the exact exported head rather than silently redoing later turns.
|
Export/import regression tests must include an undone campaign and verify the imported story reopens at the exact exported head rather than silently redoing later turns.
|
||||||
|
|
||||||
|
### 9.1 As implemented in M3
|
||||||
|
|
||||||
|
The bundle carries the active head depth beside the active branch, and the import
|
||||||
|
honors it. This moved the head depth across the format's own rule about what a
|
||||||
|
bundle carries: a bundle carries what was *chosen* and recomputes what is
|
||||||
|
*derived*, and before M3 the head depth was genuinely derived — the newest row was
|
||||||
|
the only place a story could be read. It is a decision now, because the same tree
|
||||||
|
exports identically whether the user undid three turns or none, so the file has to
|
||||||
|
say.
|
||||||
|
|
||||||
|
A file that does not state a head is opened at the tip of its active branch. That
|
||||||
|
is a fallback only in form: such a file was written when the head could not be
|
||||||
|
anywhere else, so deriving the tip reproduces the position it actually recorded.
|
||||||
|
Pre-tree bundles take the same path. No format version bump was required, because
|
||||||
|
an absent field is unambiguous.
|
||||||
|
|
||||||
|
The head depth is validated before any row is written — a depth past the branch's
|
||||||
|
own retained story is a file disagreeing with itself and is refused, while a depth
|
||||||
|
*behind* it is the feature.
|
||||||
|
|
||||||
|
The bundle also carries which branches the story has left, and at what depth.
|
||||||
|
Every row of an abandoned line is exported either way, so without that metadata a
|
||||||
|
restored campaign could not distinguish abandoned history from active history —
|
||||||
|
which is precisely what a later cleanup or recovery feature has to select on.
|
||||||
|
|
||||||
|
### 9.2 Save Points in the bundle, as implemented in M4
|
||||||
|
|
||||||
|
Save Points are exported and imported with the campaign, which is `I04`. They
|
||||||
|
fall on the "chosen" side of §9.1's rule without argument: a position someone
|
||||||
|
named is not recoverable from the rows, since nothing about a turn records that
|
||||||
|
a player once bookmarked it.
|
||||||
|
|
||||||
|
No format version bump. A bundle written before M4 has no `checkpoints` key and
|
||||||
|
imports with none, which is what such a campaign had — the same unambiguous
|
||||||
|
absence §9.1 relies on for the head depth, and the same treatment the persona
|
||||||
|
block and the branch disposition received.
|
||||||
|
|
||||||
|
The head and the Save Points are independent, deliberately. An import opens the
|
||||||
|
campaign where `headDepth` says, never at a Save Point merely because the file
|
||||||
|
carries one: the bundle records where the story was being read and, separately,
|
||||||
|
which positions were named, and choosing between them is the user's to make
|
||||||
|
after the file is open.
|
||||||
|
|
||||||
|
A Save Point whose coordinate names no turn in the file is dropped rather than
|
||||||
|
refusing the import — the opposite of the head depth's treatment, and for a
|
||||||
|
stated reason. A misplaced head affects every read in the file; a bookmark
|
||||||
|
pointing outside the story affects only itself, and rejecting a whole campaign
|
||||||
|
to protect one bookmark would lose the story to save the pointer.
|
||||||
|
|
||||||
## 10. Authoritative Narrative State
|
## 10. Authoritative Narrative State
|
||||||
|
|
||||||
### 10.1 Do not retain the RPG state protocol as the product model
|
### 10.1 Do not retain the RPG state protocol as the product model
|
||||||
@@ -457,6 +609,17 @@ Selected direction:
|
|||||||
|
|
||||||
Events provide audit/reconstruction value. Snapshots/cache make normal reads, Undo/Redo, and context construction fast.
|
Events provide audit/reconstruction value. Snapshots/cache make normal reads, Undo/Redo, and context construction fast.
|
||||||
|
|
||||||
|
**Constraint added by M3 (see ADR 012).** The snapshot half is not an
|
||||||
|
optimization to be traded away. M3's head movement is a row lookup plus a
|
||||||
|
restore, which is why Undo, Redo and — later — Save Point restore cost the same
|
||||||
|
at any distance into a campaign's history. A state model that could only be
|
||||||
|
reconstructed by replaying events from the campaign opening would make every one
|
||||||
|
of those operations proportional to campaign length, on exactly the long
|
||||||
|
campaigns this product exists for. Whatever M5 introduces must keep the
|
||||||
|
authoritative state at a retained position efficiently recoverable — a per-node
|
||||||
|
snapshot, or an equivalent cache with the same property — while adding the typed
|
||||||
|
event model.
|
||||||
|
|
||||||
## 11. Context and Memory
|
## 11. Context and Memory
|
||||||
|
|
||||||
Retain AI-DnD's useful lineage-aware memory foundation, but align it with the product authority model.
|
Retain AI-DnD's useful lineage-aware memory foundation, but align it with the product authority model.
|
||||||
|
|||||||
@@ -1,7 +1,28 @@
|
|||||||
# Adventure Storyteller — V1 Acceptance Tests
|
# Adventure Storyteller — V1 Acceptance Tests
|
||||||
|
|
||||||
**Status:** v1.1 planning/release contract — updated after Phase 0B, and after M2 for
|
**Status:** v1.3 planning/release contract — updated after Phase 0B, after M2 for
|
||||||
the security contract (H10 strengthened, H12 added)
|
the security contract (H10 strengthened, H12 added), after M3 for history
|
||||||
|
ownership and results (D03, D10, I07, L01), and after M4 for Save Point results
|
||||||
|
(D11-D14, I04, L03, E-series) and the browser condition below
|
||||||
|
|
||||||
|
> **Browser-level verification (M4 closeout, 2026-09-03).** The browser smoke
|
||||||
|
> condition that M3 and M4 both carried is **satisfied**. A real Firefox 154.0.1,
|
||||||
|
> driven through geckodriver over the W3C WebDriver protocol, exercised the
|
||||||
|
> rendered DOM: M3's Undo/Redo enable states, transcript movement, Retry and the
|
||||||
|
> take pager, divergence and the loss of Redo; and M4's full Save Point
|
||||||
|
> lifecycle including both confirmations and the branch-delete warning. 44/44
|
||||||
|
> checks passed with no console errors, on two independent runs. No pass
|
||||||
|
> condition anywhere in this document was changed to achieve it. See
|
||||||
|
> `reports/M4-IMPLEMENTATION-REPORT.md` §W.
|
||||||
|
>
|
||||||
|
> **Three kinds of evidence are recorded separately below, and are not
|
||||||
|
> interchangeable.** *Automated* means a test in the repository's suite, which
|
||||||
|
> runs on every future change. *Live runtime* means a real server exercised over
|
||||||
|
> HTTP — stronger than a unit test about process boundaries, weaker than a
|
||||||
|
> browser about anything a user sees. *Browser* means the rendered DOM driven by
|
||||||
|
> a real browser, which is the only evidence that a control is visible, enabled
|
||||||
|
> and wired. Where a result cites more than one, the strongest is named last.
|
||||||
|
|
||||||
**Purpose:** Define black-box acceptance tests for finalist evaluation during Phase 0B and for the eventual v1 release.
|
**Purpose:** Define black-box acceptance tests for finalist evaluation during Phase 0B and for the eventual v1 release.
|
||||||
|
|
||||||
## 1. Test Philosophy
|
## 1. Test Philosophy
|
||||||
@@ -575,6 +596,17 @@ All retained turns can be traversed backward safely.
|
|||||||
### Partial
|
### Partial
|
||||||
System supports at least five but has a documented technical limit.
|
System supports at least five but has a documented technical limit.
|
||||||
|
|
||||||
|
### Result (M3)
|
||||||
|
**Pass, not partial.** Undo traverses to the campaign opening and then reports
|
||||||
|
that there is nothing to undo. No technical limit applies: each step is one
|
||||||
|
indexed query regardless of story length, because the position is a stored
|
||||||
|
coordinate rather than a replay. The floor is the campaign opening — there is no
|
||||||
|
pre-campaign position to reach.
|
||||||
|
|
||||||
|
Note also that Undo continues backward through story a branch inherited from the
|
||||||
|
line it forked from; it does not stop at a fork. See
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` §5.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## D04 — Redo
|
## D04 — Redo
|
||||||
@@ -689,6 +721,26 @@ Mara wears a green cloak.
|
|||||||
- downstream state is re-evaluated,
|
- downstream state is re-evaluated,
|
||||||
- old version/future remains retained/disposable.
|
- old version/future remains retained/disposable.
|
||||||
|
|
||||||
|
### Milestone ownership
|
||||||
|
|
||||||
|
All three pass conditions stand for v1. They are delivered across three
|
||||||
|
milestones, and this note records which is which rather than reducing the
|
||||||
|
requirement:
|
||||||
|
|
||||||
|
- **M3 — safe history behavior.** Replaying a narrator turn with different text
|
||||||
|
forks, keeps the original take and its future, and starts the new continuation
|
||||||
|
from the correct earlier state. In-place editing of a turn is **refused** while
|
||||||
|
story descends from it off screen, so retained history cannot be made to
|
||||||
|
contradict itself unseen (`STORY-BRANCH-SEMANTICS.md` §14A). Delivered.
|
||||||
|
- **M5 — authoritative state re-evaluation.** The second pass condition. Making a
|
||||||
|
hand-typed narrator correction authoritative and re-evaluating the state it
|
||||||
|
implies requires the narrative-state extraction pass, so it is completed there,
|
||||||
|
and the §14A refusal is replaced by it. Outstanding.
|
||||||
|
- **Later browser UX work — the finished editing workflow.** How the user reaches
|
||||||
|
and confirms the operation. Outstanding.
|
||||||
|
|
||||||
|
D10 is therefore **not** satisfied at the end of M3, and is not scored as such.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## D11 — Named Checkpoint
|
## D11 — Named Checkpoint
|
||||||
@@ -705,6 +757,14 @@ Before entering the abbey
|
|||||||
### Pass
|
### Pass
|
||||||
Checkpoint persists across application restart.
|
Checkpoint persists across application restart.
|
||||||
|
|
||||||
|
### Result — PASS (M4 closeout, 2026-09-04)
|
||||||
|
*Automated:* `backend/tests/test_process_restart.py` starts the application as a
|
||||||
|
subprocess, writes the campaign, **terminates the process**, and starts a second
|
||||||
|
process against the same database — the Save Point, its name and its
|
||||||
|
`(branch, depth)` coordinate all survive.
|
||||||
|
*Browser:* the Save Point is still listed after a full page reload
|
||||||
|
(`reports/M4-IMPLEMENTATION-REPORT.md` §W.7 section E).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## D12 — Restore Checkpoint
|
## D12 — Restore Checkpoint
|
||||||
@@ -719,6 +779,11 @@ Checkpoint persists across application restart.
|
|||||||
### Pass
|
### Pass
|
||||||
Transcript/state return to checkpoint position.
|
Transcript/state return to checkpoint position.
|
||||||
|
|
||||||
|
### Result — PASS (M4 closeout, 2026-09-04)
|
||||||
|
*Automated:* `test_d12_restore_returns_the_transcript_and_the_state`.
|
||||||
|
*Browser:* the visible transcript and the state both move back, and the view
|
||||||
|
refreshes without a manual reload (§W.7 section F).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## D13 — Restore Does Not Delete Later History
|
## D13 — Restore Does Not Delete Later History
|
||||||
@@ -728,6 +793,13 @@ Transcript/state return to checkpoint position.
|
|||||||
### Pass
|
### Pass
|
||||||
Later story is retained as abandoned/disposable history.
|
Later story is retained as abandoned/disposable history.
|
||||||
|
|
||||||
|
### Result — PASS (M4 closeout, 2026-09-04)
|
||||||
|
*Automated:* measured on **row identity**, not on counts — the set of action row
|
||||||
|
ids before a restore equals the set after it. Ordinary Redo still walks the retained
|
||||||
|
continuation until a divergent write, and after that write the displaced rows are
|
||||||
|
still present while Redo reports nothing ahead. Four `test_d13_*` tests, and
|
||||||
|
confirmed in the browser with a database check behind it.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## D14 — Delete Checkpoint
|
## D14 — Delete Checkpoint
|
||||||
@@ -741,10 +813,31 @@ Delete named checkpoint.
|
|||||||
- checkpoint pointer disappears,
|
- checkpoint pointer disappears,
|
||||||
- referenced story turn/history remains intact.
|
- referenced story turn/history remains intact.
|
||||||
|
|
||||||
|
### Result — PASS (M4 closeout, 2026-09-04)
|
||||||
|
*Automated:* the pointer row goes; the referenced turn, the later history and the
|
||||||
|
active head are all unchanged (`test_d14_delete_removes_the_pointer_and_no_story`).
|
||||||
|
*Browser:* the confirmation states that deleting the Save Point does not delete
|
||||||
|
the story, and the story remains afterwards (§W.7 section I).
|
||||||
|
|
||||||
|
A Save Point is also the **only** thing that can remove itself: deleting a branch
|
||||||
|
whose history a Save Point names is refused rather than cascading
|
||||||
|
(`STORY-BRANCH-SEMANTICS.md` §19.1), verified automatically and in the browser.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
# E. Branch and Lineage Safety
|
# E. Branch and Lineage Safety
|
||||||
|
|
||||||
|
> **M4 result (2026-09-03).** E01 and E04 were re-exercised through a Save Point
|
||||||
|
> restore rather than only through Undo, and pass: a memory derived past a
|
||||||
|
> restored head stops being retrievable and becomes eligible again on Redo,
|
||||||
|
> without being deleted or re-embedded; after restore-plus-divergence the old
|
||||||
|
> future's memory stays ineligible even as the new line grows past its depth; and
|
||||||
|
> the transcript after a restore holds only the active lineage while the
|
||||||
|
> displaced rows remain in the tree. **E03** (summary lineage over a long story)
|
||||||
|
> remains **NOT PERFORMED** — it needs a long-run campaign and is owned by
|
||||||
|
> M6/M11, unchanged from M3.
|
||||||
|
|
||||||
|
|
||||||
## E01 — Abandoned Future Cannot Affect Active State
|
## E01 — Abandoned Future Cannot Affect Active State
|
||||||
|
|
||||||
**Priority:** REQUIRED FOR V1
|
**Priority:** REQUIRED FOR V1
|
||||||
@@ -1321,6 +1414,15 @@ Export preserves retained alternate/disposable history needed for recovery, unle
|
|||||||
### Pass
|
### Pass
|
||||||
Named checkpoints survive export/import.
|
Named checkpoints survive export/import.
|
||||||
|
|
||||||
|
### Result — PASS (M4 closeout, 2026-09-04)
|
||||||
|
*Automated and live runtime.* A real round trip with three Save Points across two
|
||||||
|
branches: names, notes and
|
||||||
|
coordinates survive, branch references are remapped to the imported rows
|
||||||
|
(1→3, 2→4), and each restores to a distinct position and state in the new
|
||||||
|
campaign. Importing Save Points does **not** move the active head — the head
|
||||||
|
still comes from the bundle's `headDepth`. Bundles written before M4 carry no
|
||||||
|
`checkpoints` key, import cleanly, and create none.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## I05 — Knowledge Provenance Export
|
## I05 — Knowledge Provenance Export
|
||||||
@@ -1358,6 +1460,21 @@ No external API credentials are embedded in campaign export.
|
|||||||
- import does not silently Redo to the newest retained turn,
|
- import does not silently Redo to the newest retained turn,
|
||||||
- Redo/recovery behavior remains coherent after import.
|
- Redo/recovery behavior remains coherent after import.
|
||||||
|
|
||||||
|
### Also required — an export written before the head was carried
|
||||||
|
|
||||||
|
Import an export produced by a build that recorded no active head, and confirm it
|
||||||
|
opens at the retained tip of its active branch.
|
||||||
|
|
||||||
|
This is compatibility, not a degraded path, and the distinction matters when
|
||||||
|
reading a result: such a file was written when the head could not be anywhere but
|
||||||
|
the tip, so opening it there reproduces the position it actually recorded. An
|
||||||
|
import that refused it, or that guessed some other position for it, would be the
|
||||||
|
failure.
|
||||||
|
|
||||||
|
An export whose stated head lies beyond the story it contains is a file
|
||||||
|
disagreeing with itself and must be refused rather than opened at a guessed
|
||||||
|
position.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
# J. Genre Independence
|
# J. Genre Independence
|
||||||
@@ -1490,6 +1607,20 @@ No condition exists where:
|
|||||||
- branch head advances incorrectly,
|
- branch head advances incorrectly,
|
||||||
- previous story becomes inaccessible.
|
- previous story becomes inaccessible.
|
||||||
|
|
||||||
|
### Note on the head, and on A05
|
||||||
|
|
||||||
|
"The head advances incorrectly" must be read together with A05, or the two appear
|
||||||
|
to contradict each other. A failed turn **does** move the active head forward by
|
||||||
|
one, onto the player's submitted text, because A05 deliberately retains that text
|
||||||
|
so the player can try again. That is correct behavior, not a half-advanced head.
|
||||||
|
|
||||||
|
What this test forbids is the head moving past a turn that did not happen: an
|
||||||
|
accepted narration with state written only partway, or a position that implies a
|
||||||
|
reply the story never received. Assert on the accepted narration and the
|
||||||
|
authoritative state, not on whether the head moved at all. One Undo from that
|
||||||
|
position steps back over the stranded input and leaves the story on a complete
|
||||||
|
turn.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## L02 — State Reconstruction
|
## L02 — State Reconstruction
|
||||||
@@ -1520,6 +1651,14 @@ State at each position matches original accepted state.
|
|||||||
### Pass
|
### Pass
|
||||||
Correct historical state is reconstructed.
|
Correct historical state is reconstructed.
|
||||||
|
|
||||||
|
### Result — PASS (M4 closeout, 2026-09-04)
|
||||||
|
*Automated, across a genuine OS process boundary:* the state at the Save Point
|
||||||
|
was recorded before the first process exited, and a second process restored
|
||||||
|
exactly that value after the campaign had been advanced past it
|
||||||
|
(`backend/tests/test_process_restart.py`). This replaced a same-process
|
||||||
|
`TestClient` restart, which could not distinguish durable state from a live
|
||||||
|
object.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## L04 — Derived Data Can Be Rebuilt
|
## L04 — Derived Data Can Be Rebuilt
|
||||||
|
|||||||
@@ -1,8 +1,204 @@
|
|||||||
# Planning Package Version
|
# Planning Package Version
|
||||||
|
|
||||||
**Package:** Adventure Storyteller Planning Package v2.2
|
- **Package:** Adventure Storyteller Planning Package v2.6
|
||||||
**Revision date:** 2026-09-03
|
- **Revision date:** 2026-09-03
|
||||||
**Status:** Phase 0 complete; architecture selected; **Milestones M1 and M2 implemented and accepted**; M3 not yet briefed.
|
- **Status:** Phase 0 complete; architecture selected; **Milestones M1-M4 implemented and accepted**; M5 is next to brief.
|
||||||
|
|
||||||
|
## v2.6 — M4 Closeout (2026-09-03)
|
||||||
|
|
||||||
|
M4 is **accepted**. Its review returned *PASS WITH CORRECTIVE WORK REQUIRED*; the
|
||||||
|
corrective work is done, and the browser condition that M3 and M4 both carried is
|
||||||
|
closed.
|
||||||
|
|
||||||
|
**The three review findings, fixed:**
|
||||||
|
|
||||||
|
- **B-1 — the Save Point list was an N+1.** It resolved each Save Point with its
|
||||||
|
own query and loaded whole `Action` rows, narration included, to answer "does a
|
||||||
|
row exist here". It is now one bulk two-column coordinate query plus one
|
||||||
|
lineage computation: **53 SELECTs for 25 Save Points became 5**, and the count
|
||||||
|
no longer grows with the list. Guarded by three tests, including one proving
|
||||||
|
the coordinate is matched as a *pair* — an `IN`-list version would report a
|
||||||
|
Save Point resolved because another branch has a live row at the same depth.
|
||||||
|
- **B-2 — deleting a branch silently deleted its Save Points.** Fixed as a
|
||||||
|
**behaviour** defect rather than a missing warning, because
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` §19 says a named checkpoint remains until
|
||||||
|
explicitly deleted and §28 already required future cleanup to retain
|
||||||
|
checkpoint-referenced paths. **A branch a Save Point names can no longer be
|
||||||
|
deleted.** The request is refused with the offending Save Points named; the
|
||||||
|
user deletes them explicitly, which deletes no story, and the branch then
|
||||||
|
goes. Both delete controls disable and explain. Recorded as a new
|
||||||
|
**§19.1**.
|
||||||
|
- **B-3 — the D11/L03 automation never left one process.** A new module spawns
|
||||||
|
real server processes, kills the first, and reads the campaign back with the
|
||||||
|
second.
|
||||||
|
|
||||||
|
**Also fixed (review §S C-5):** creating a Save Point now takes the campaign's
|
||||||
|
turn lock, so "save where I am" cannot read a head a turn in flight is about to
|
||||||
|
move. Rename and Delete deliberately do not take it, and a test pins that
|
||||||
|
decision.
|
||||||
|
|
||||||
|
**Real-browser verification — the first in this project.** A Firefox 154.0.1
|
||||||
|
driven through geckodriver over the W3C WebDriver protocol exercised the rendered
|
||||||
|
DOM for **both** milestones: **44/44 checks passed**, no console errors. It
|
||||||
|
covered M3's Undo/Redo enable states, transcript movement, Retry and the take
|
||||||
|
pager, and divergence retiring Redo; and M4's whole Save Point lifecycle
|
||||||
|
including both confirmations and the new branch-delete warning. **The outstanding
|
||||||
|
M3 browser condition is therefore closed as well.** No dependency was added to
|
||||||
|
the repository: the WebDriver client for the run was written against stdlib HTTP.
|
||||||
|
|
||||||
|
**Also corrected, found while fixing B-2:** `models.py`, `TECHNICAL-DESIGN.md`
|
||||||
|
§8.8 and `DATA-MODEL.md` §8 all described the cascade as the durability rule.
|
||||||
|
They now describe the refusal, and record that `checkpoints.branch_id`'s cascade
|
||||||
|
survives as referential integrity that the application no longer reaches.
|
||||||
|
|
||||||
|
**Documents corrected by this closeout:**
|
||||||
|
|
||||||
|
- `V1-ACCEPTANCE-TESTS.md` records results for **D11-D14, I04, L03** and the
|
||||||
|
E-series, and states that the browser-level condition is satisfied. **No pass
|
||||||
|
condition was weakened** — and D11/L03 now note that the automation crosses a
|
||||||
|
genuine OS process boundary, which is the standard later milestones should
|
||||||
|
meet.
|
||||||
|
- `DATA-MODEL.md` §8 records the coordinate as implemented, with the retry
|
||||||
|
measurement that settles coordinate-versus-turn-id, and three decisions that
|
||||||
|
were previously implicit: names are not unique, several Save Points may name
|
||||||
|
one position, and the list is newest-created first.
|
||||||
|
- `STORY-BRANCH-SEMANTICS.md` gains **§19.1** — a checkpoint protects the
|
||||||
|
history it names. This is the only behavioural specification change in the
|
||||||
|
closeout, and it strengthens §19 rather than weakening anything.
|
||||||
|
- `BROWSER-UX-SPEC.md` §25 rules for the implemented vocabulary: **Moment N**,
|
||||||
|
not *Turn N*, because the branch panel and tree overlay already count in
|
||||||
|
moments. Vocabulary only; no behaviour changes.
|
||||||
|
- `BUILD-MILESTONES.md` marks **M4 COMPLETE**, records the fixes and the browser
|
||||||
|
result, and warns M5 that the instrumentation to move is now **55 tests**.
|
||||||
|
- `README.md` records M1-M4 accepted and M5 as next to brief.
|
||||||
|
- `PROJECT-SOURCES.md` and `project-sources.txt` point at the current report;
|
||||||
|
`project-sources.txt` still named the archived M3 report and was corrected.
|
||||||
|
|
||||||
|
**Report rotation** happened in the reporting pass that preceded this closeout:
|
||||||
|
`M3-IMPLEMENTATION-REPORT.md` moved to `archive/milestone-reports/` as a pure
|
||||||
|
rename, and `reports/` now holds M4's report, whose **§W** is this closeout's
|
||||||
|
evidence record.
|
||||||
|
|
||||||
|
**No new ADR.** ADR 012 already decides the architecture, and the corrective work
|
||||||
|
forced no new architectural decision. `SPECIFICATION.md`,
|
||||||
|
`STORY-BRANCH-SEMANTICS.md`, `SECURITY-THREAT-MODEL.md`, `CONTEXT-AND-MEMORY.md`,
|
||||||
|
`IMPORTED-KNOWLEDGE-DESIGN.md` and ADRs 003, 005 and 012 are unchanged.
|
||||||
|
|
||||||
|
**M5 readiness:** ready. Save Points store no state and no checkpoint code reads
|
||||||
|
any, so M5 can change what a snapshot contains without touching what a Save Point
|
||||||
|
is — provided it keeps state recoverable at a position without replay
|
||||||
|
(`TECHNICAL-DESIGN.md` §10.4).
|
||||||
|
|
||||||
|
## v2.5 — M4 Implementation (2026-09-03)
|
||||||
|
|
||||||
|
M4 added durable named Save Points. This revision records only what the
|
||||||
|
implementation established as fact; **no product requirement changed**, and the
|
||||||
|
milestone is **not** marked accepted — its review has not been written.
|
||||||
|
|
||||||
|
- `TECHNICAL-DESIGN.md` gains **§8.8** and **§9.2**: the Save Point as a name
|
||||||
|
plus a coordinate holding no story, restore as head movement with a bounds
|
||||||
|
check, the rule that the branch half of the head moves only when the
|
||||||
|
coordinate is off the path being read, restore never forking, and the bundle
|
||||||
|
carrying Save Points independently of the head.
|
||||||
|
- `DATA-MODEL.md` **§8** records the pointer as implemented — `(branch, depth)`
|
||||||
|
rather than a turn id, with the reason: one coordinate holds every attempt at
|
||||||
|
a turn and exactly one is live, so a coordinate follows a retry where a row id
|
||||||
|
would pin a superseded take. **§29** records the checkpoints in the export.
|
||||||
|
- `BUILD-MILESTONES.md` **M4** gains a status block: what shipped, the one
|
||||||
|
architectural decision the milestone had to make and why it needed no new ADR,
|
||||||
|
the test count, and the outstanding browser condition.
|
||||||
|
- `README.md` describes Save Points as a user-facing capability.
|
||||||
|
|
||||||
|
**No ADR was created.** ADR 012 already decides the architecture M4 needed —
|
||||||
|
restore reuses active-head movement — and a table is not a decision. The one
|
||||||
|
question ADR 012 does not answer, whether restore moves the branch half of the
|
||||||
|
head, is that same mechanism applied to a coordinate ADR 012 already defines;
|
||||||
|
`TECHNICAL-DESIGN.md` §8.8 records the answer rather than a new ADR asserting it.
|
||||||
|
|
||||||
|
`SPECIFICATION.md`, `SECURITY-THREAT-MODEL.md`, `STORY-BRANCH-SEMANTICS.md` and
|
||||||
|
`V1-ACCEPTANCE-TESTS.md` are unchanged. M4 altered no product requirement, added
|
||||||
|
no outbound path, and implemented the checkpoint semantics
|
||||||
|
`STORY-BRANCH-SEMANTICS.md` §18-25 already specified rather than amending them.
|
||||||
|
|
||||||
|
**The browser smoke test remains unperformed, now for both M3 and M4.** No
|
||||||
|
session has had a usable browser. See `BUILD-MILESTONES.md` M3 and M4.
|
||||||
|
|
||||||
|
## v2.4 — Documentation Consolidation (2026-09-03)
|
||||||
|
|
||||||
|
No product requirement, architecture decision or milestone status changed in
|
||||||
|
this revision. It reorganises the documentation so that a new coding agent can
|
||||||
|
tell authoritative material from evidence at a glance.
|
||||||
|
|
||||||
|
- `planning/archive/` is created and is **non-authoritative by declaration**
|
||||||
|
(`archive/README.md`). It holds `phase0/` — the research that chose AI-DnD —
|
||||||
|
`milestone-reports/` — the completed M1 and M2 reports — and `decisions/`,
|
||||||
|
which now holds ADR **008**, the Phase-0-before-build process gate that Phase 0
|
||||||
|
satisfied. ADR numbering continues from 012; 008 is not reused.
|
||||||
|
- `planning/reports/` now holds **only the current milestone's report**,
|
||||||
|
`M3-IMPLEMENTATION-REPORT.md`, because M4 planning has to consult it. It moves
|
||||||
|
to the archive when M4's report replaces it.
|
||||||
|
- The Phase 0B execution prompts and handoff/status/summary documents
|
||||||
|
(`CODEX-HANDOFF-NOTE.md`, `PHASE-0B-CODEX-BRIEF.md`,
|
||||||
|
`PHASE-0B-CODEX-HANDOFF.md`, `PLANNING-UPDATE-SUMMARY.md`) and the Phase 0A
|
||||||
|
discovery and triage reports were **deleted**: intermediate working documents
|
||||||
|
whose conclusions all reached the two recommendation reports, and which remain
|
||||||
|
in Git history.
|
||||||
|
- Upstream AI-DnD's inherited `plan/` build log and `docs/` project site
|
||||||
|
(guides, generated HTML, screenshots) were **deleted**. They documented a
|
||||||
|
hosted, scripted, multi-user product with accounts — every screenshot showed a
|
||||||
|
Scripts tab and a Sign up button — which M2 removed. Both trees remain in Git
|
||||||
|
history and in upstream.
|
||||||
|
- `README.md`, `DEVELOPMENT.md` and `PROVENANCE.md` are corrected where they
|
||||||
|
pointed at the removed trees or described removed capability as present.
|
||||||
|
`DEVELOPMENT.md`'s "things M1 did not touch" section had gone stale at M2 and
|
||||||
|
now says what is actually still inherited.
|
||||||
|
- `planning/README.md` is rewritten as **the documentation index**: current
|
||||||
|
milestone, the three-way active/ADR/archive split, the authority order,
|
||||||
|
reading order, where reports live, and what comes next. The milestone
|
||||||
|
correction tables are preserved unchanged.
|
||||||
|
- New: `planning/PROJECT-SOURCES.md` and `planning/project-sources.txt`, the
|
||||||
|
manifest of files to upload as ChatGPT Project Sources.
|
||||||
|
|
||||||
|
## v2.3 — Post-M3 Closeout (2026-09-03)
|
||||||
|
|
||||||
|
M3 replaced destructive Undo with a stored active head. Its review is
|
||||||
|
`archive/milestone-reports/M3-IMPLEMENTATION-REPORT.md`, which is also M3's primary evidence
|
||||||
|
record — no separate baseline report was produced — and whose §W records this
|
||||||
|
closeout.
|
||||||
|
|
||||||
|
In summary:
|
||||||
|
|
||||||
|
- the architecture is recorded as **ADR 012 — Active-Head Non-Destructive
|
||||||
|
History**: the head is stored rather than derived, every read of the story is
|
||||||
|
capped at it in one place, one mechanism moves it, state comes from the node
|
||||||
|
rather than from a replay, the first write below a moved-back head is the
|
||||||
|
divergence, and Redo is decided by the lineage rather than by a flag. ADR 005
|
||||||
|
is unchanged: it states the product requirement, and ADR 012 states the
|
||||||
|
architecture chosen to implement it,
|
||||||
|
- two history semantics are **ratified** in `STORY-BRANCH-SEMANTICS.md`: Undo
|
||||||
|
crosses fork points to the campaign opening (§5), and the system refuses to
|
||||||
|
switch which take is live while a later story is off screen (§10),
|
||||||
|
- a **new §14A** records the interim in-place-editing rule — refuse when story
|
||||||
|
descends from the turn and is not on screen — and states explicitly that
|
||||||
|
§14-15's full narrator-edit requirement stands and is completed in M5,
|
||||||
|
- `TECHNICAL-DESIGN.md` gains **§8.7** and **§9.1** recording the implemented
|
||||||
|
model and bundle behaviour as fact, and a constraint on §10.4: the snapshot
|
||||||
|
half of the hybrid state model is a requirement, because head movement must
|
||||||
|
not become proportional to campaign length,
|
||||||
|
- `DATA-MODEL.md` records the head as campaign-stored, the branch disposition as
|
||||||
|
implemented and deliberately advisory, and the export as carrying a chosen
|
||||||
|
position rather than a derived one,
|
||||||
|
- `BUILD-MILESTONES.md` marks **M3 complete**, states the one outstanding
|
||||||
|
condition (the browser smoke test), tells **M4** to reuse M3's head movement
|
||||||
|
rather than build a second restore path, and gives **M5** three constraints,
|
||||||
|
- `V1-ACCEPTANCE-TESTS.md` records D03's full pass, states **D10's milestone
|
||||||
|
ownership without weakening any pass condition**, adds I07's pre-M3 bundle
|
||||||
|
clause, and resolves the apparent L01/A05 conflict,
|
||||||
|
- `README.md` is corrected to describe the current local-only single-user
|
||||||
|
application rather than the upstream hosted one.
|
||||||
|
|
||||||
|
`SPECIFICATION.md` and `SECURITY-THREAT-MODEL.md` are unchanged: M3 altered no
|
||||||
|
product requirement and touched no path in the threat model.
|
||||||
|
|
||||||
## v2.2 — Post-M2 Closeout (2026-09-03)
|
## v2.2 — Post-M2 Closeout (2026-09-03)
|
||||||
|
|
||||||
@@ -10,7 +206,7 @@ M2 removed the hosted, cloud, account and scripting surface and added the
|
|||||||
inference endpoint policy. Its review recommended six planning changes and
|
inference endpoint policy. Its review recommended six planning changes and
|
||||||
reported rather than applied them; all six are applied in this revision, listed
|
reported rather than applied them; all six are applied in this revision, listed
|
||||||
in `README.md` § *Post-M2 corrections applied*, with the evidence in
|
in `README.md` § *Post-M2 corrections applied*, with the evidence in
|
||||||
`reports/M2-BASELINE-REPORT.md` and `reports/M2-IMPLEMENTATION-REPORT.md`.
|
`archive/milestone-reports/M2-BASELINE-REPORT.md` and `archive/milestone-reports/M2-IMPLEMENTATION-REPORT.md`.
|
||||||
|
|
||||||
In summary:
|
In summary:
|
||||||
|
|
||||||
@@ -43,7 +239,7 @@ the architecture selected in v2 was reversed.
|
|||||||
M1 implementation evidence contradicted or under-specified parts of v2. The
|
M1 implementation evidence contradicted or under-specified parts of v2. The
|
||||||
corrections are recorded in the documents themselves and listed in
|
corrections are recorded in the documents themselves and listed in
|
||||||
`README.md` § *Post-M1 corrections applied*; the evidence behind them is in
|
`README.md` § *Post-M1 corrections applied*; the evidence behind them is in
|
||||||
`reports/M1-BASELINE-REPORT.md` and `reports/M1-IMPLEMENTATION-REPORT.md`.
|
`archive/milestone-reports/M1-BASELINE-REPORT.md` and `archive/milestone-reports/M1-IMPLEMENTATION-REPORT.md`.
|
||||||
|
|
||||||
In summary:
|
In summary:
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
# Archive — historical material, not authoritative
|
||||||
|
|
||||||
|
Everything under `planning/archive/` is **evidence and history**. None of it
|
||||||
|
governs current implementation. If an archived document and an active planning
|
||||||
|
document disagree, the active document is right and the archived one records
|
||||||
|
what was believed or measured at the time.
|
||||||
|
|
||||||
|
Do not consult this directory during ordinary milestone work unless an active
|
||||||
|
document sends you here for a specific piece of historical evidence.
|
||||||
|
|
||||||
|
## What is here
|
||||||
|
|
||||||
|
### `phase0/` — why AI-DnD was selected
|
||||||
|
|
||||||
|
Phase 0A static research and Phase 0B local validation, closed 2026-09-01.
|
||||||
|
|
||||||
|
| File | What it is |
|
||||||
|
| --- | --- |
|
||||||
|
| `RESEARCH-PLAN.md` | The question Phase 0 existed to answer, and its final dispositions. |
|
||||||
|
| `PRELIMINARY-RECOMMENDATION.md` | The Phase 0A conclusion, from static review alone. |
|
||||||
|
| `REUSE-MATRIX.md` | What each candidate offered against the required subsystems. |
|
||||||
|
| `AI-DND-ANALYSIS.md` | The selected base, analysed before the fork. |
|
||||||
|
| `AI-ADVENTURE-ANALYSIS.md` | The rejected finalist still cited as the implementation reference for typed state events. |
|
||||||
|
| `OPEN-DUNGEON-ANALYSIS.md` | The rejected finalist still cited as the UX/future-media reference. |
|
||||||
|
| `PHASE-0B-RECOMMENDATION.md` | **The strongest single document.** The measured Phase 0B findings and the fork decision. |
|
||||||
|
| `PHASE-0B-BASELINE.md` | Clean clones, builds and test runs for the three finalists. |
|
||||||
|
| `PHASE-0B-AI-DND-EXPERIMENT.md` | AI-DnD driven against a real local Ollama. |
|
||||||
|
| `PHASE-0B-AI-ADVENTURE-OLLAMA.md` | ai-adventure's Ollama and service-boundary behaviour. |
|
||||||
|
| `PHASE-0B-OPEN-DUNGEON-HISTORY.md` | Whether Open Dungeon could be retrofitted with history. |
|
||||||
|
| `PHASE-0B-OFFLINE-NETWORK.md` | What each candidate reached for with no route to the Internet. |
|
||||||
|
| `PHASE-0B-UNDO-SPIKE.md` | The disposable spike that proved non-destructive undo/redo, and became ADR 012's architecture. |
|
||||||
|
| `PHASE-0B-FOLLOWUP-CHECKS.md` | The world-state protocol, export/head, story-card lineage and Postgres checks. |
|
||||||
|
|
||||||
|
Phase 0A discovery and triage material (the candidate inventory, source index,
|
||||||
|
reference-project list, licensing and static-privacy reviews, the Phase 0A
|
||||||
|
status page) and the Phase 0B execution prompts were deleted in the 2026-09-03
|
||||||
|
documentation cleanup. They are intermediate working documents whose
|
||||||
|
conclusions all reached the two recommendation reports above, and they remain in
|
||||||
|
Git history.
|
||||||
|
|
||||||
|
### `milestone-reports/` — completed milestone evidence
|
||||||
|
|
||||||
|
`M1-BASELINE-REPORT.md`, `M1-IMPLEMENTATION-REPORT.md`,
|
||||||
|
`M2-BASELINE-REPORT.md`, `M2-IMPLEMENTATION-REPORT.md`,
|
||||||
|
`M3-IMPLEMENTATION-REPORT.md`.
|
||||||
|
|
||||||
|
M3's report is both its review and its primary evidence record; no separate M3
|
||||||
|
baseline report was produced. It arrived here when M4's report landed.
|
||||||
|
|
||||||
|
Every architectural conclusion these reports reached has already been applied to
|
||||||
|
the active planning documents and the ADRs — see `planning/VERSION.md`, which
|
||||||
|
lists the corrections each milestone produced. The reports are kept for their
|
||||||
|
measurements and their reasoning, not as instructions.
|
||||||
|
|
||||||
|
The **current** milestone's report stays in `planning/reports/` while it is
|
||||||
|
still useful for reviewing the next milestone, and moves here when it is not.
|
||||||
|
|
||||||
|
### `decisions/` — superseded or completed ADRs
|
||||||
|
|
||||||
|
`008-phase0-before-build-plan.md` — a process gate ("do not start production
|
||||||
|
work before Phase 0 closes") that Phase 0 satisfied on 2026-09-01. It
|
||||||
|
constrains nothing now. ADR numbering continues from 012 in
|
||||||
|
`planning/DECISIONS/`; 008 is not reused.
|
||||||
|
|
||||||
|
## A note on paths inside these files
|
||||||
|
|
||||||
|
Archived documents are kept **verbatim**. File paths written inside them refer
|
||||||
|
to where those files lived when the document was written — before this archive
|
||||||
|
existed, and in some cases before files were deleted. That is deliberate: an
|
||||||
|
evidence record that has been quietly edited is no longer evidence. Resolve any
|
||||||
|
such path against Git history, not against the current tree.
|
||||||