Skip to content

fix(cache): keep Windows cache keys case-preserving (#4925) - #4937

Closed
Deepjyoti-Sarmah wants to merge 4 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4925-windows-case-sensitive-path
Closed

Deepjyoti-Sarmah wants to merge 4 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4925-windows-case-sensitive-path

Conversation

@Deepjyoti-Sarmah

@Deepjyoti-Sarmah Deepjyoti-Sarmah commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4925.

On Windows, file_hash() ran the caller path through _normalize_path(), which called os.path.normcase(). That lowercased the path used for is_file(), stat() and read_bytes(). On a case-sensitive WSL share, README.md became readme.md, so the rebuild failed.

The same value was also the cache key. Lowercasing made README.md and readme.md share a digest and a semantic cache entry. When both files exist, that turns the crash into a wrong cache hit.

This change separates the path used for I/O from the path used for the cache key:

- p = _normalize_path(path)
- p.is_file(); p.stat(); p.read_bytes()
+ src = os_long_path(path)
+ src.is_file(); src.stat(); src.read_bytes()

  key = _normalize_path(path)
- return Path(os.path.normcase(s))
+ return Path(os.path.normpath(s))
- salt = walked_rel.as_posix().lower()
+ salt = walked_rel.as_posix()

_normalize_path() now keeps case. file_hash() and cached_word_count() use os_long_path() for the filesystem path. The stat-index key is case-preserving.

On POSIX, _normalize_path() still returns the path unchanged. On Windows, a case-only spelling change may now miss the cache. That is safer than returning another file's cache entry.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Evidence

Before: on v8 at 0.9.100.

tests/test_cache_windows_case_4925.py
8 failed in 3.35s

The reported failure:

file_hash requires a file, got: ...\readme.md

The quieter failure, with both README.md and readme.md present:

file_hash(README.md) == file_hash(readme.md)
load_cached(readme.md) returned the entry saved for README.md

After:

tests/test_cache_windows_case_4925.py
8 passed

cache-focused suites
146 passed, 1 skipped

full suite
9396 passed, 18 skipped, 1 xfailed in 1092.49s

The full suite was run with PYTHONPATH=. so subprocess CLI tests imported this worktree instead of the installed package.

Verification & Invariants

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

The invariant: a cache key may miss, but it must not name a different file.

The tests pin:

  • Uppercase files and uppercase roots hash and cache correctly.
  • save_cached() and load_cached() round trip an uppercase path.
  • Two case variants with the same content get different hashes.
  • Two case variants do not share a semantic cache entry.
  • Hardlink and symlink aliases keep distinct hashes.
  • cached_word_count() writes and uses the stat index for an uppercase path.

Known limitations:

  • On a case-insensitive volume, alternate case spellings may miss the cache. That costs a rebuild, not correctness.
  • Path.relative_to() on Windows compares case-insensitively. Two WSL directories differing only by case can still be conflated. That is separate hardening and is not required for this issue.
  • Old lowercased cache entries on Windows become unreachable and are rebuilt. No graph data is lost.
  • No cache schema bump and no graph format change.

How was this tested?

PYTHONPATH=. .venv/bin/python -m pytest tests/test_cache_windows_case_4925.py -q
PYTHONPATH=. .venv/bin/python -m pytest -q -p no:cacheprovider --basetemp=/var/tmp/gf4925-full6
.venv/bin/python -m ruff check graphify/cache.py tests/test_cache_windows_case_4925.py
.venv/bin/python -m graphify update .

Branch fix/4925-windows-case-sensitive-path is based on upstream/v8 (32e5efe9, v0.9.100). It has four commits:

bcd81af5 test(cache): pin Windows case-sensitive path handling (#4925)
78448a04 fix(cache): keep Windows cache keys case-preserving (#4925)
54e6ba23 fix(cache): open hashed files through os_long_path (#4925)
002d31e3 fix(cache): open word-count files through os_long_path (#4925)

Merge Danger

Door: Two-way.

The default path is unchanged. No cache schema bump and no graph format change. Reverting restores the old cache keys.

Blast Radius: Cache.

After merge, old lowercased cache entries on Windows become unreachable and are rebuilt. A user who spells the same file with different case may see a cache miss. That costs time and returns the correct result. Ignore rules, secret filtering, extraction and graph writing are unchanged.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. No source fragments changed.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

Deepjyoti-Sarmah added 4 commits November 2, 2026 00:22
…4925)

The regression needs Windows normalization and a case-sensitive filesystem. The fixture takes the win32 branch on a case-sensitive temp dir, then checks hashing, semantic cache round trips, case-variant siblings, aliases and the word-count stat index.
os.path.normcase lowercases a path on Windows, but case sensitivity belongs to the volume. A Windows process can reach a case-sensitive WSL share, where README.md and readme.md are two files. Normalize separators and strip the extended-length prefix, then keep the caller case in the key and salt.
A cache key is not a file handle. Keep the raw caller path for is_file(), stat() and read_bytes(), and add the Windows long-path prefix only on that I/O path. The stat-index key comes from the normalized identity.
…#4925)

cached_word_count() had the same key-path-as-file-path mistake. Stat the raw long-path-safe file, key the index by the case-preserving identity, and pass the caller path to compute().
@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Thanks for the pull request, @Deepjyoti-Sarmah. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 3 changed function(s) (1 sampled, not proven, 2 not verified).

Formal verification. PR-changed functions: 1/3 verified (0 proven, 1 may-equivalent, 0 distinguished) · 2 not verified (2 unsupported).

Not verified on this run: cached\_word\_count (unsupported), file\_hash (unsupported).


Graphify review — findings

Stops lowercasing paths on Windows when computing cache keys in file_hash and cached_word_count, so files with uppercase names or roots open correctly on case-sensitive volumes (#4925). _normalize_path now only normalizes separators and the extended-length prefix (normpath instead of normcase), and the digest salt no longer calls .lower(). Filesystem access goes through _os_long_path using the caller's original spelling. The tradeoff is that case-variant spellings of the same file on a case-insensitive Windows volume now get distinct cache keys instead of sharing one, which can cost a cache miss. Adds a Linux-runnable test file that fakes the win32 branch on a case-sensitive temp dir. It covers uppercase file and root hashing, semantic cache round-trips, no cross-hits between case variants, hardlink and symlink aliases keeping distinct hashes, and the word-count stat fastpath.

No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 2942 functions depend on the 105 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: load_cached() — 66 callers, 10 callees
  • worse: save_cached() — 60 callers, 10 callees
  • worse: file_hash() — 59 callers, 7 callees
  • worse: cached_word_count() — 6 callers, 6 callees

Verification — 2942 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1495 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

49 of 490 test file(s) selected (10%) via static blast radius.

  • tests/test_binary_media_words.py — impact
  • tests/test_cache.py — impact
  • tests/test_cache_long_paths.py — impact
  • tests/test_cache_stale_import_target.py — impact
  • tests/test_cache_windows_case_4925.py — impact, changed-test
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_grammar_cache_key.py — impact
  • tests/test_ignore_file_encoding.py — impact
  • tests/test_incremental_mtime_collision.py — impact
  • tests/test_java_member_calls.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_language_override_lifecycle.py — impact
  • tests/test_language_overrides.py — impact
  • tests/test_llm_backends.py — impact
  • tests/test_manifest_ingest.py — impact
  • tests/test_msbuild_dir_evidence.py — impact
  • tests/test_objc_field_table_remap.py — impact
  • tests/test_out_dir_evidence.py — impact
  • tests/test_partial_cache.py — impact
  • tests/test_pipeline.py — impact
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_python_injected_factory_calls.py — impact
  • tests/test_python_property_reads.py — impact
  • tests/test_python_pytest_fixture_receivers.py — impact
  • tests/test_rust_self_member_calls.py — impact
  • tests/test_rust_use_trees.py — impact
  • tests/test_semantic_cache_basename_recovery.py — impact
  • tests/test_semantic_cache_out_root.py — impact
  • tests/test_skill_persisted_scan_options.py — impact
  • tests/test_skill_semantic_all_cached.py — impact
  • tests/test_skillgen.py — impact
  • tests/test_stale_prune.py — impact
  • tests/test_stat_index_husk.py — impact
  • tests/test_stat_index_multi_root.py — impact
  • tests/test_stat_index_portability.py — impact
  • tests/test_terraform_modules.py — impact
  • tests/test_update_no_change_gate.py — impact
  • tests/test_vbnet_extractor.py — impact
  • tests/test_watch.py — impact
  • tests/test_watch_manifest_location.py — impact
  • tests/test_word_count_cache.py — impact
  • tests/test_zero_node_no_cache.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

No difference found (not proven): No behavior difference found in \_normalize\_path (not a proof).

The verifier ran both versions of \_normalize\_path on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

Could not verify: Could not verify cached\_word\_count.

The verifier did not have enough to check cached\_word\_count, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `cache_root` is annotated `'Path | None'` — outside the synthesizable primitive/collection set

Could not verify: Could not verify file\_hash.

The verifier did not have enough to check file\_hash, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `cache_root` is annotated `'Path | None'` — outside the synthesizable primitive/collection set

· 1 grounded finding(s) anchored inline below; 3 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/cache.py
return Path(os.path.normpath(s))


def file_hash(path: Path, root: Path = Path("."), cache_root: "Path | None" = None) -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Health regression — file_hash()

fans out to 7 callees (efferent coupling); 59 callers depend on it (afferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

safishamsi added a commit that referenced this pull request Nov 2, 2026
…ase-preserving cache fixes

#4944 resolves Rust module-qualified calls, #4945 links calls inside
expression-macro arguments, and #4937 keeps cache keys case-preserving
(which also shifts the POSIX salt digest for uppercase-bearing paths).
All three change cached output, so warm caches must re-run.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
safishamsi added a commit that referenced this pull request Nov 2, 2026
…keys

#4937 made the file_hash salt case-preserving (dropped the .lower() on the
absolute-path fallback), but the existing order-independence test still
asserted the lowercased salt. Update its exp_abs to match the written-case
salt so it pins the new behavior instead of the retired one (#4925, #4937).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.101 (published to PyPI), cherry-picked with your authorship preserved. Follow-up: updated an existing order-independence test (test_word_count_cache) to expect the case-preserving salt, since the digest now changes for any relative path carrying an uppercase char on POSIX too. Fixes #4925. Thanks @Deepjyoti-Sarmah.

@safishamsi safishamsi closed this Nov 2, 2026
safishamsi pushed a commit that referenced this pull request Nov 8, 2026
99b8da7 and ff8ddf5 (#4925, #4937) open files through os_long_path(), but
on Windows its \\?\ spelling also leaked into cache identity.

file_hash() and cached_word_count() bound the stat index to the prefixed
root, so the plain cache_root that extract() passes read as a second root.
Every Windows `graphify update` that re-extracts warned "stat index
switched" (#3989) and flushed mid-run, and since no key was relative to the
prefixed anchor, stat-index.json was written with absolute keys and a moved
checkout re-hashed every file (#2199). The walked-parent match compared a
plain path with the prefixed root and never matched, so under a junctioned
or symlinked scan root an in-root alias took its target's salt and digest
(#2832), and the out-of-root salt carried a //?/ prefix. _normalize_path()
also stripped only \\?\, so the UNC form \\?\UNC\server\share became a
relative key that the flush pruned, and a corpus on a network or WSL share
always wrote an empty stat index.

Bind the index to the normalized root as before #4925, normalize the
resolved path and root before they become keys, salts or comparisons, and
map \\?\UNC\ back to \\. File I/O still goes through os_long_path(). CI
runs on Linux, where both helpers are the identity, so this only showed on
Windows.

Co-Authored-By: Claude <[email protected]>
(cherry picked from commit 49616eb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

file_hash opens the lowercased path on Windows, so files with uppercase names fail on case-sensitive dirs (\wsl.localhost)

2 participants