Skip to content

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

Open
jerrytron wants to merge 1 commit into
JBenda:masterfrom
choosatron:fix/knot-hash-full-path
Open

jerrytron wants to merge 1 commit 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.

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.
@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

This branch has not been deployed

No deployments
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.

1 participant