Skip to content

test(core): golden hash determinism guard across languages and versions#132

Merged
Connorrmcd6 merged 2 commits into
Connorrmcd6:mainfrom
laurigates:upstream-pr/test-golden-hash
Jun 15, 2026
Merged

test(core): golden hash determinism guard across languages and versions#132
Connorrmcd6 merged 2 commits into
Connorrmcd6:mainfrom
laurigates:upstream-pr/test-golden-hash

Conversation

@laurigates

Copy link
Copy Markdown
Contributor

Severity: MEDIUM — hash/normalization determinism

The stored anchor hash is the single value every Surface consumer compares against, yet no
test pins its exact output
. The canonical token stream can change for an unchanged symbol
without anyone touching hash.rs:

  • a tree-sitter grammar bump renames a node kind or reshapes the tree — grammars are
    caret-pinned in Cargo.toml (tree-sitter-rust = "0.24.2"^0.24.2) and only frozen by
    Cargo.lock, which Dependabot bumps on a schedule;
  • a tree-sitter core bump changes traversal;
  • a refactor of the canonicalization itself.

Any of these silently re-hashes every anchored span in every downstream repo: a wave of false
DIVERGED, or — worse — two spans that should differ start colliding. CI stays green because
nothing asserts the hash value.

What this adds

surf-core/tests/golden_hash.rs, public-API only:

  1. Goldens — exact 12-hex hash of a representative symbol in each family (Rust, TS, TSX,
    Python, Go). Snippets include comments + non-canonical whitespace, so the goldens also
    encode the "formatting/comments ignored" guarantee.
  2. Cosmetic invariants — reformatting and a consistent local rename keep the hash.
  3. Logic sensitivity — an operator flip and swapped operands move the hash, and b - a
    does not collide with a - b.

Because CI already runs the suite on ubuntu-latest and macos-14, this doubles as a
cross-platform drift guard between the three release target triples — directly addressing
"is the stored hash stable across target triples / rust-version / grammar bumps?".

A failure here is the intended signal: the canonical form moved. The fix is to revert the
bump, or to ship the change deliberately (update CHANGELOG, treat as a hash-format break, tell
consumers to re-verify).

Verified: cargo test -p surf-core --test golden_hash → 3 passed.

Applies equally upstream (Connorrmcd6/surface).

🤖 Generated with Claude Code

…ns (#4)

* test(core): golden hash determinism guard across languages and versions

The stored anchor hash is the only value every consumer compares against,
yet nothing pinned its exact output. A tree-sitter grammar bump (the
grammars are caret-pinned and only frozen by Cargo.lock, which Dependabot
bumps) can rename a node kind and silently change every stored hash —
a wave of false DIVERGED downstream, or worse, new collisions — with
green CI.

Add goldens for Rust/TS/TSX/Python/Go plus invariants: cosmetic edits
(reformatting, consistent rename) keep the hash; logic edits (operator
flip, swapped operands) move it. Runs on Linux and macOS in CI, so it
also catches cross-platform drift between the release target triples.

https://claude.ai/code/session_01T3Z35bgg6cvWzBDfyh6pQ8

* style: rustfmt golden_hash test

(cherry picked from commit 7bb7a59)

@Connorrmcd6 Connorrmcd6 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch on the golden hash pinning, I didn't think of that! One minor change just to keep things tidy: could you drop cosmetic_edits_do_not_change_the_hash since it duplicates rust_quiet_on_reformat_and_rename (surf-core/tests/hash.rs:126)?

The cosmetic_edits_do_not_change_the_hash test re-verified Rust hash
invariance under reformatting and consistent rename, which
rust_quiet_on_reformat_and_rename (surf-core/tests/hash.rs:126) already
covers. Per upstream review feedback, remove the duplicate; the unique
value of golden_hash.rs is the pinned per-language hash values.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@laurigates

Copy link
Copy Markdown
Contributor Author

Thanks! Dropped cosmetic_edits_do_not_change_the_hash in ffadf77 — you're right that it duplicated rust_quiet_on_reformat_and_rename (surf-core/tests/hash.rs:126). The pinned per-language goldens in golden_hashes_are_stable_per_language are the unique guard the new file adds. Remaining suite is green.

@Connorrmcd6 Connorrmcd6 merged commit 349297b into Connorrmcd6:main Jun 15, 2026
5 checks passed
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