Group a turn's takes by their parent, not by where they sit

SP7 shipped a tree nobody could use. Driving it by hand found three things,
and the schema is what the third one needs.

A take forked onto its own branch leaves the coordinate its siblings are still
at. `attempts.group` filtered on (branch_id, depth), so that take read as the
only one of its turn -- 1/1 where the player is owed 1/3, with the other takes
unreachable from the line they were taken on. Nesting has the same shape from
the other side: takes under C1 and takes under C2 share a depth, and only the
parent says a pager under C2 reads 2/2 rather than counting C1's three too.

So `actions.parent_id`, and `group` keys on it. The alternative -- pointing a
branch's fork at a node instead of a depth, so a promoted take never moves --
was rejected: `lineage` exists so a read is an OR-clause per branch rather
than a walk up parent pointers, and moving the fork point changes path
resolution itself, which drags in cursors, memory depths and both bundle
formats. This column is read to group takes and for nothing else. One indexed
lookup, never a walk, and no read of the story changes.

Two writers had to learn it. `add_attempt` places a sibling by hand rather
than through `place_action`, and copies the parent, because a take belongs to
the turn it is a take *of*. `place_action` resolves it from the path for a
genuinely new node, which is the honest default.

And one reader had to be kept out of it. `delete_turn` meant "every attempt at
this coordinate"; the group spans branches now, so undoing a turn would have
deleted a take that another branch is telling. `attempts.on_branch` scopes it
back.

409 tests, unchanged from main and green -- a linear story has one take per
parent, so none of this is reachable until a second one exists. The migration
adds one column, backfills the linear case, and leaves NULL where it cannot
honestly place a row; `group` falls back to the coordinate there, which is the
rule those rows were written under. One `VACUUM FULL actions;` owed after
deploy.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Dvvqn9ZDR4ixeFPHNbww7
This commit is contained in:
parththakkar106
2026-08-18 21:34:12 +05:30
committed by Parth
co-authored by Claude Opus 5
parent e692780f08
commit 6fa213f6db
6 changed files with 224 additions and 5 deletions
+52 -4
View File
@@ -50,23 +50,66 @@ ATTEMPT_KEYS = ("world_state", "script", "raw_output")
def group(db: Session, action: models.Action) -> list[models.Action]:
"""Every attempt at `action`'s turn, oldest first.
A node with no branch is a pre-tree row that no path contains; it is its
own only attempt, and saying so here saves every caller a special case.
Keyed on the **parent**, not on the coordinate (SP9). The two agree right up
until a take is forked onto its own branch: it keeps its parent but leaves
the (branch, depth) its siblings are still at, so a coordinate would report
it as the only take of its turn — `1/1` where the player is owed `1/3`.
The parent also gets the nesting right without being asked. Takes under C1
and takes under C2 share a depth and, until one of them forks, a branch;
only the parent separates them, which is what makes a pager under C2 read
`2/2` instead of counting C1's three as well.
Two fallbacks, both meaning "this row predates the key being asked about":
a node with no branch is a pre-tree row no path contains, and a node with no
parent is a pre-SP9 row the backfill could not place. Both are their own
only attempt under the rule they were written with.
"""
if action.branch_id is None or action.depth is None:
return [action]
if action.parent_id is None:
# Pre-SP9, and the coordinate is the key those rows were written under.
# Root nodes land here too and are genuinely alone: nothing is a take of
# the opening of a story.
return (
db.query(models.Action)
.filter(
models.Action.adventure_id == action.adventure_id,
models.Action.branch_id == action.branch_id,
models.Action.depth == action.depth,
models.Action.parent_id.is_(None),
)
.order_by(models.Action.variant_index, models.Action.id)
.all()
)
return (
db.query(models.Action)
.filter(
models.Action.adventure_id == action.adventure_id,
models.Action.branch_id == action.branch_id,
models.Action.depth == action.depth,
models.Action.parent_id == action.parent_id,
)
.order_by(models.Action.variant_index, models.Action.id)
.all()
)
def on_branch(rows: list[models.Action], node: models.Action) -> list[models.Action]:
"""The takes in `rows` that sit on `node`'s own branch.
`group` answers "which takes are of this turn", and since SP9 that spans
branches — a take forked onto its own line is still a take of the same turn,
which is the whole point of keying on the parent.
Deleting is the one caller that must not follow it there. A take on another
branch is reachable through that branch and belongs to the story somebody is
telling on it; removing it because a turn was undone over here would delete
a line nobody asked about. Same parent *and* same branch is the coordinate,
which is what "every attempt at this turn" meant before a fork could move
one out of it.
"""
return [row for row in rows if row.branch_id == node.branch_id]
def live_in(rows: list[models.Action]) -> models.Action | None:
for row in rows:
if row.live:
@@ -149,6 +192,11 @@ def add_attempt(
"""
replacement.branch_id = previous.branch_id
replacement.depth = previous.depth
# Copied, never resolved from the path: a take belongs to the turn it is a
# take *of*, and that is what `group` keys on. Resolving it here would ask
# what is live one depth back, which is the same node right now and stops
# being once this turn is forked away from.
replacement.parent_id = previous.parent_id
replacement.live = True
# The end of the group, not one past `previous` — which is only the same
# thing when `previous` is the newest take. Switch a three-take turn back to
+54
View File
@@ -266,6 +266,16 @@ MIGRATIONS: list[tuple[int, str | dict[str, str]]] = [
# visible from today. Anchoring them at the tip instead would have emptied
# them out of every branch forked earlier than they were typed.
(62, "UPDATE memories SET depth = 0 WHERE depth IS NULL"),
# Phase 14, SP9 — the node a node was played after, so a turn's takes can be
# grouped by parent instead of by coordinate. A coordinate stops answering
# "which takes belong together" the moment one of them is forked onto its
# own branch: it leaves its siblings behind and reads 1/1 beside their 1/3.
#
# Nullable, and left NULL wherever the backfill cannot honestly place a row
# (see _backfill_parents). `attempts.group` falls back to the coordinate for
# those, which is the rule they were written under.
(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)"),
]
LATEST_VERSION = max((v for v, _ in MIGRATIONS), default=1)
@@ -278,6 +288,7 @@ SNAPSHOT_COMPRESS_VERSION = 43
TREE_BACKFILL_VERSION = 52
CURSOR_ANCHOR_VERSION = 56
SIBLING_SPLIT_VERSION = 60
PARENT_BACKFILL_VERSION = 64
# An adventure with no actions has no tip. -1 keeps "the next node goes at
# head_depth + 1" true without a special case (mirrors tree.NO_DEPTH).
@@ -324,6 +335,45 @@ def _backfill_world_delta(conn) -> None:
conn.execute(text(sql))
def _backfill_parents(conn) -> None:
"""Point every node at the take it was played after.
One pass, and deliberately only one: the live node one depth back on the
same branch. That is the whole of a linear story, which is the whole of
every adventure written before SP9 — forking reached the screen in SP7 and
the tree was found unusable before anyone forked with it.
The rows left NULL are the first node of a forked branch, whose parent sits
on an ancestor branch and cannot be found without walking `lineage` per row.
`attempts.group` falls back to the coordinate for a NULL parent, which is
exactly the rule those rows were written under, so the fallback is not a
degraded answer for them — it is the original one. Every fork made from SP9
on sets `parent_id` at write time and never relies on this.
Correlated to the row being updated rather than numbering the table, so the
planner drives it off ix_actions_branch_depth. That is the lesson of
_backfill_cursor_anchors, which numbered every action once per adventure.
"""
conn.execute(
text(
"""
UPDATE actions SET parent_id = (
SELECT prev.id FROM actions AS prev
WHERE prev.adventure_id = actions.adventure_id
AND prev.branch_id = actions.branch_id
AND prev.depth = actions.depth - 1
AND prev.live = TRUE
ORDER BY prev.id
LIMIT 1
)
WHERE parent_id IS NULL
AND branch_id IS NOT NULL
AND depth IS NOT NULL
"""
)
)
def _backfill_variant_count(conn) -> None:
"""Populate actions.variant_count from the existing variants list.
@@ -890,6 +940,10 @@ def bootstrap(engine: Engine) -> None:
if version == SIBLING_SPLIT_VERSION:
_backfill_state_after(conn)
_split_variants_into_siblings(conn)
# After 63 adds the column and 64 indexes it: the UPDATE is
# what the index is for, so it runs once both are in place.
if version == PARENT_BACKFILL_VERSION:
_backfill_parents(conn)
current = version
_set_version(conn, current)
_encrypt_plaintext_api_keys(conn)
+18
View File
@@ -333,6 +333,24 @@ class Action(Base):
ForeignKey("branches.id", ondelete="CASCADE"), nullable=True
)
depth: Mapped[int | None] = mapped_column(Integer, nullable=True)
# Phase 14, SP9: the node this one was played after — the take that was
# live when it was written, not merely whatever sits at depth - 1 now.
#
# It exists for one question: which takes belong to the same turn. A
# coordinate cannot answer it, because a take that gets forked onto its own
# branch leaves the coordinate its siblings are still at and would read
# `1/1` next to their `1/3`. A parent does not move when a branch does.
#
# Read only to group takes — one indexed lookup, never a walk. Paths still
# resolve through `lineage`, which is why this column can be added without
# touching a single read of the story.
#
# NULL on a root node, and on every pre-SP9 row the migration could not
# place: `attempts.group` falls back to the coordinate there, which is what
# those rows were written under.
parent_id: Mapped[int | None] = mapped_column(
ForeignKey("actions.id", ondelete="SET NULL"), nullable=True, index=True
)
# Phase 14, SP4: whether this node is the one the story tells at its
# coordinate. Retry no longer rewrites a row — it writes a *sibling* at the
# same (branch, depth), so a coordinate can hold several attempts and
+4 -1
View File
@@ -1047,7 +1047,10 @@ def delete_turn(
rather than off one of its attempts.
"""
memorybank.forget_node(db, adventure, node)
for attempt in attempts.group(db, node):
# Scoped to this node's branch (SP9). The group spans branches now, and a
# take that was forked onto its own line is another branch's story — see
# `attempts.on_branch`.
for attempt in attempts.on_branch(attempts.group(db, node), node):
db.delete(attempt)
+36
View File
@@ -182,6 +182,7 @@ def place_action(
adventure: models.Adventure,
action: models.Action,
branch: models.Branch | None = None,
parent: models.Action | None = None,
) -> models.Branch:
"""Put `action` on the head branch and move the head to it.
@@ -191,17 +192,52 @@ def place_action(
`branch` is the head, already resolved, for a caller placing several nodes
at once — see `place_new_nodes` for why that is worth a parameter.
`parent` is the take this node was played after (SP9), and is what groups a
turn's takes. Resolved from the path when the caller does not say, which is
the honest default: a node written now follows whatever the player is
reading now. A caller placing several nodes in one flush should chain it —
the second node's parent is the first, and the database has not seen either.
"""
branch = branch or head_branch(db, adventure)
action.branch_id = branch.id
if action.depth is None:
action.depth = action.index
if parent is not None:
action.parent_id = parent.id
elif action.parent_id is None and action.depth:
action.parent_id = _preceding_id(db, adventure, branch, action.depth)
adventure.head_branch_id = branch.id
if action.depth is not None and action.depth > adventure.head_depth:
adventure.head_depth = action.depth
return branch
def _preceding_id(
db: Session, adventure: models.Adventure, branch: models.Branch, depth: int
) -> int | None:
"""The id of the live node one step back along `branch`'s path.
Asked of the whole lineage rather than of `branch` alone, because a branch
borrows the story before its fork point: the node in front of a forked
branch's first turn lives on an ancestor, and that is exactly the parent a
pager needs to find its siblings through.
"""
path = lineage.Path(lineage.entries_of(branch))
return (
db.query(models.Action.id)
.filter(
models.Action.adventure_id == adventure.id,
models.Action.depth == depth - 1,
models.Action.live.is_(True),
path.clause(),
)
.order_by(models.Action.id)
.limit(1)
.scalar()
)
def place_memory(
db: Session,
adventure: models.Adventure,