Skip to content

fix(runtime): snapshot of a story with a sequence fails to load - #176

Merged
JBenda merged 11 commits into
JBenda:masterfrom
choosatron:fix/snapshot-visit-counts
Nov 30, 2026
Merged

JBenda merged 11 commits into
JBenda:masterfrom
choosatron:fix/snapshot-visit-counts

Conversation

@jerrytron

Copy link
Copy Markdown
Contributor

The bug

Loading a snapshot back into the story it was taken from throws for many stories:

Invalid container id reference.
tracked containere are not allowed to move, expect we migrate

It happens for any story with a sequence ({a|b}, {stopping: ...}, {cycle: ...}, {shuffle: ...}), and for some with a labelled gather inside a choice. Seven lines are enough:

-> knot
=== knot ===
{First visit.|Later visit.}
* [Again]
  - - (inner) Inside.
  ** [Back] -> knot
* [Leave] -> END

Play to the first choice, create_snapshot(), new_runner_from_snapshot(): it throws.

Why

When not migrating, globals_impl::snap_load() looked every tracked container up again from the name hash stored with its visit count, and asserted that it found the same id. That lookup is not one-to-one:

  • A sequence's container is tracked but has no name of its own, so its hash is 0 and find_offset_for() finds nothing.
  • build_container_hash_map() names the named children of an indexed container without the index, so two containers can end up with the same name and hash, and the lookup returns the other one.

The fix

When not migrating, the visit counts are already loaded in container order and nothing needs looking up. The stored hash is now compared with the container's own hash at the same index, which still catches a snapshot from a different story. The lookup moves into the migration branch, which is otherwise unchanged.

Tests

  • New regression scenario in inkcpp_test/Fixes.cpp with the story above (inkcpp_test/ink/SnapshotVisitCounts.ink): it throws without the fix, and with it the restored runner carries on the sequence ("Later visit.").
  • ctest passes locally (70 cases). Formatted with clang-format 18.1.8, matching .pre-commit-config.yaml.
  • Outside the suite: we snapshotted at every choice of 70 real stories (5 different playthroughs each) and restored 1-4 choices back, 32,031 restores in all, comparing every replayed line, tag and choice list with the original run. 3 stories threw before the fix; after it, every restore matched.

Not in this PR

From reading the code (not tested), the migration path still resolves containers through the same lookup, so migrating a story with sequences would drop their visit counts (hash 0 finds nothing) or put a count on the wrong container (shared hash). Fixing that means giving those containers unique names in build_container_hash_map(), which changes the file format, so it seemed better as a separate discussion. Happy to open an issue for it.

Loading a snapshot into the story it was taken from threw "Invalid
container id reference." or "tracked containere are not allowed to
move" for many stories: any with a sequence ({a|b}, {stopping:},
{cycle:}, {shuffle:}), and some with a labelled gather inside a choice.

globals_impl::snap_load() re-derived every tracked container from the
name hash stored with its visit count, even when not migrating. That
lookup is not one-to-one: a sequence's container is tracked but has no
name of its own, so its hash is 0 and resolves to nothing, and named
containers below an indexed one are hashed without the index, so two
containers can share a hash and resolve to the other one.

When not migrating the counts are loaded in container order, so the
lookup is not needed: compare the stored hash with the container's own
instead, which still catches a snapshot from a different story. The
migration path is unchanged.

Found by snapshotting at every choice of 70 stories and restoring 1-4
choices back (32,031 restores): 3 stories threw before, none after,
and every replay matched the original run.
get_current_knot() returned hash_string("dead") rather than
hash_string("body.dead") for every stitch of a story's first-compiled
knot, and the full path for the stitches of every other knot. Which
form a caller got depended on where the knot fell in the alphabet.

start_container() files every child under indexed_children as well as
named_children, and every named child is filed at index -1. The map
keeps only the first, so each container's first named child is in both.
build_container_hash_map() walks named_children under the child's own
name, then walks indexed_children again under the parent's name, so
that one child's named children were hashed one level short and
overwrote the right _hash. At the root, that is the first knot: its
stitches came out as bare names, and the container hash table gained
an entry for each bare name as well.

The indexed walk now skips a child that is also named, since it was
hashed in the named walk.

Found by recording get_current_knot() at every ending of 70 stories:
three reported hashes that matched no knot or stitch path, all
stitches of the alphabetically first knot. With the fix, every ending
of all 70 matches its full path.
jerrytron and others added 5 commits November 27, 2026 13:52
Loading a snapshot into the story it was taken from threw "Invalid
container id reference." or "tracked containere are not allowed to
move" for many stories: any with a sequence ({a|b}, {stopping:},
{cycle:}, {shuffle:}), and some with a labelled gather inside a choice.

globals_impl::snap_load() re-derived every tracked container from the
name hash stored with its visit count, even when not migrating. That
lookup is not one-to-one: a sequence's container is tracked but has no
name of its own, so its hash is 0 and resolves to nothing, and named
containers below an indexed one are hashed without the index, so two
containers can share a hash and resolve to the other one.

When not migrating the counts are loaded in container order, so the
lookup is not needed: compare the stored hash with the container's own
instead, which still catches a snapshot from a different story. The
migration path is unchanged.

Found by snapshotting at every choice of 70 stories and restoring 1-4
choices back (32,031 restores): 3 stories threw before, none after,
and every replay matched the original run.
…of the same story

loader::migratable only indicates that the snapshot was taken in a migratable state
Added loader::migrate to indicate that the snapshot must be migrated (story hash !=)
Name them by their index like ink paths do ("knot.0.3.b.c-0"), and add the
tracked ones to the container hash so migrate_to() can find them.
JBenda added a commit that referenced this pull request Nov 27, 2026
### The bug

`get_current_knot()` names a stitch by its full path
(`hash_string("knot.stitch")`), except in a story's first-compiled knot,
where it returns the stitch's bare name (`hash_string("stitch")`).
Compiled JSON objects come out with their keys sorted, so which form a
caller gets depends on where the knot falls in the alphabet.
`MoveTo.ink` already shows it: after the story runs into `Dialog.core`,
`get_current_knot()` is `hash_string("core")`, and
`move_to(hash_string("core"))` succeeds.

### Why

`start_container()` files every child in `indexed_children` as well as
in `named_children`, and every named child goes in at index `-1`. The
map keeps only the first, so each container's first named child is in
both maps.

`build_container_hash_map()` walks `named_children` under each child's
own name, then walks `indexed_children` again under the *parent's* name.
For that one child, its named children are hashed one level short, and
because the indexed walk runs second, the short hash overwrites the
right `_hash`. At the root that child is the first knot: its stitches
get bare names, and the container hash table gains an extra entry for
each bare name.

### The fix

The indexed walk skips a child that is also named, since the named walk
has already hashed it under its own name. Unnamed indexed children are
walked as before.

### Tests

- New scenario in `inkcpp_test/MoveTo.cpp` using the existing
`MoveTo.ink`: `get_current_knot()` after reaching `Dialog.core` is
`hash_string("Dialog.core")`, and `move_to(hash_string("core"))` now
returns false. Both fail without the fix. (`Tags.cpp` already covers a
stitch in a later knot, `knot2.sub`.)
- ctest passes locally (70 cases). Formatted with clang-format 18.1.8,
matching `.pre-commit-config.yaml`.
- Outside the suite: we recorded `get_current_knot()` at every ending of
70 real stories. Before the fix, three stories reported hashes that
matched no knot or stitch path, all of them stitches of the
alphabetically first knot. After it, every ending matches its full path,
and every story's walk is otherwise unchanged.

### Note

This is separate from the issue mentioned in #176: named children of an
*unnamed* indexed container are still hashed without the index. That is
unchanged here.

---------

Co-authored-by: Julian Benda <julian.m.benda@gmail.com>
@JBenda JBenda mentioned this pull request Nov 27, 2026
@JBenda

JBenda commented Nov 27, 2026

Copy link
Copy Markdown
Owner

This was a dig, not bad. Thanks a lot. I noticed that migratable is a bit misleading, so I added a second member migrated for more confusion, but now at least both scenarios have a bool to check (1. if the snapshot could be migrated, 2. if the snapshot is migrated (story hash != old hash)).

I added a bit more to explicit label containers inside choices, which fixed #178. Please check if you are fine with https://github.com/JBenda/inkcpp/tree/pr-176. If yes, please merge it on top of your PR, and then I can merge it.

…t-counts

# Conflicts:
#	inkcpp/globals_impl.cpp
#	inkcpp_test/Fixes.cpp
@jerrytron

Copy link
Copy Markdown
Contributor Author

Thanks, this looks good. I've merged pr-176 into the PR branch. Beyond the suite, I replayed snapshot restores over our story corpus. Every restore matches, and get_current_knot() is unchanged at every step. Small thing: the new comment in shared/private/header.h has two typos, "soties" and "hashs".

@github-actions

Copy link
Copy Markdown

Ink Proof Results

These results are obtained by running the Ink-Proof Testing Suite on the compiled binaries in this pull request.

System Results
Linux x64 130/130 passed
MacOSX-ARM DISABLED
MacOSX DISABLED
Windows x64 130/130 passed

@JBenda
JBenda merged commit 2fd5ed4 into JBenda:master Nov 30, 2026
15 of 16 checks passed
@jerrytron
jerrytron deleted the fix/snapshot-visit-counts branch November 30, 2026 22:38
t0mg added a commit to t0mg/inkcpp that referenced this pull request Dec 2, 2026
…, JBenda#185) into eenk-patches

- Resolve conflicts in globals_impl.cpp, snapshot_impl.h, and inkcpp_test/Fixes.cpp
- Update basic_restorable_array::snap_to_writer() and globals_impl::stream_snapshot_to() for PR JBenda#185's trimmed snapshot format (story_seed/rng in globals, no _saved/_temp in visit_counts)
- Bump SNAPSHOT_VERSION from 2 to 3 due to PR JBenda#185 binary format change
- Remove obsolete shared/private/command_strings.cpp workaround (resolved upstream in JBenda#184)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants