Repository navigation
fix(runtime): snapshot of a story with a sequence fails to load - #176
Conversation
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.
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 !=)
Wrong argument order
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.
### 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>
|
This was a dig, not bad. Thanks a lot. I noticed that 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
|
Thanks, this looks good. I've merged |
Ink Proof ResultsThese results are obtained by running the Ink-Proof Testing Suite on the compiled binaries in this pull request.
|
…, 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)
The bug
Loading a snapshot back into the story it was taken from throws for many stories:
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: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:0andfind_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
inkcpp_test/Fixes.cppwith 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.")..pre-commit-config.yaml.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
0finds nothing) or put a count on the wrong container (shared hash). Fixing that means giving those containers unique names inbuild_container_hash_map(), which changes the file format, so it seemed better as a separate discussion. Happy to open an issue for it.