Skip to content

Hash a first knot's stitches by their full path - #177

Merged
JBenda merged 2 commits into
JBenda:masterfrom
choosatron:fix/knot-hash-full-path
Nov 27, 2026
Merged

JBenda merged 2 commits into
JBenda:masterfrom
choosatron:fix/knot-hash-full-path

Conversation

@jerrytron

Copy link
Copy Markdown
Contributor

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.

jerrytron and others added 2 commits November 15, 2026 23:53
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.
@JBenda

JBenda commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Good catch, thank you so much for the effort.
I pushed into https://github.com/JBenda/inkcpp/tree/fix/knot-hash-full-path a small addition from my side, that we only even list elements with an index ≠ -1 inside the numbered children. It should not change much, but it feels cleaner to me.

If you have no objection, would you merge it into this PR?

@jerrytron

Copy link
Copy Markdown
Contributor Author

No objection, thank you. Keeping the named children out of the numbered map is cleaner. Merged into this PR.

@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 DISABLED
Windows x64 130/130 passed

@JBenda
JBenda merged commit 21533cd into JBenda:master Nov 27, 2026
7 of 9 checks passed
@jerrytron
jerrytron deleted the fix/knot-hash-full-path 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