From ac0e4ce4c0cc2336deb4f40b99215d5e0dca58aa Mon Sep 17 00:00:00 2001 From: "Edgar Y. Walker" Date: Thu, 26 Nov 2026 05:32:53 -0800 Subject: [PATCH 01/17] fix(hashing): use a cached file hash only for the algorithm that made it (ITL-1488) FileHashKey carries the algorithm's hash label, and the in-memory, SQLite and PostgreSQL cachers return an entry only for the same label. warm-cache counts another algorithm's entry as not cached. Stored cache tables keep their layout: the stored hash already starts with its label. Co-Authored-By: Claude Opus 5.5 --- changelog.d/ITL-1488.md | 12 ++ docs/concepts/file-hash-caching.md | 14 +- docs/concepts/file-types.md | 3 +- ...1-26-itl-1488-file-hash-cache-algorithm.md | 37 ++++ src/orcapod/hashing/cache_population.py | 22 ++- src/orcapod/hashing/file_hashers.py | 32 +++- src/orcapod/hashing/hash_cachers.py | 33 ++-- src/orcapod/hashing/postgres_hash_cacher.py | 29 ++- src/orcapod/protocols/hashing_protocols.py | 9 + tests/test_hashing/test_cache_population.py | 7 +- .../test_directory_hash_caching.py | 4 + .../test_file_hash_cache_algorithm.py | 178 ++++++++++++++++++ tests/test_hashing/test_file_hashers.py | 28 +-- .../test_hash_cacher_match_mtime.py | 2 +- .../test_hash_cacher_write_guards.py | 8 +- tests/test_hashing/test_hash_cachers.py | 4 +- .../test_hashing/test_postgres_hash_cacher.py | 50 ++++- 17 files changed, 408 insertions(+), 64 deletions(-) create mode 100644 changelog.d/ITL-1488.md create mode 100644 docs/dev/plans/2026-11-26-itl-1488-file-hash-cache-algorithm.md create mode 100644 tests/test_hashing/test_file_hash_cache_algorithm.py diff --git a/changelog.d/ITL-1488.md b/changelog.d/ITL-1488.md new file mode 100644 index 000000000..8325f13e7 --- /dev/null +++ b/changelog.d/ITL-1488.md @@ -0,0 +1,12 @@ +### Fixed + +- The file hash cache no longer returns a hash made with another algorithm. A cache entry was + keyed by the file's path, modification time and size only, so after + `orcapod warm-cache --algorithm md5` the default (SHA-256) file hash returned the MD5 + value whenever caching was on, changing the identities and stored values a file enters. + `FileHashKey` now carries the algorithm's hash label (`label`), and the in-memory, SQLite + and PostgreSQL cachers return an entry only for the same label; an entry of another + algorithm is a miss, and `warm-cache` counts it as not cached. Stored cache tables keep + their layout (the stored hash already starts with its label); a file hashed with a second + algorithm replaces the first algorithm's entry. A file content hasher now declares the + label of its hashes (`hash_label`). diff --git a/docs/concepts/file-hash-caching.md b/docs/concepts/file-hash-caching.md index b738a7d2b..1aa9b0a5a 100644 --- a/docs/concepts/file-hash-caching.md +++ b/docs/concepts/file-hash-caching.md @@ -16,10 +16,10 @@ default, configurable via the `algorithm` argument). It does this every time, with no memory of previous results. `HashCacher` is a protocol for any object that maps a `FileHashKey` to a -`ContentHash`. The key is a three-field struct: +`ContentHash`. The key is a four-field struct: ``` -FileHashKey(path, mtime_ns, size) +FileHashKey(path, mtime_ns, size, label) ``` The `mtime_ns` and `size` fields encode the file's identity at a specific @@ -28,6 +28,16 @@ changes — the key is different, so the old entry is never returned. You do not need to explicitly invalidate entries; the key change handles it automatically. +The `label` field names the hash algorithm (`sha256`, `md5`, ...): it is the +label every hash of that algorithm carries. A cached hash is returned only for +the algorithm that made it. A cache filled with another algorithm, for example +by `orcapod warm-cache --algorithm md5`, is a miss for the default SHA-256 +hasher, which reads the file and computes its own hash; it never returns the +MD5 value. The stored tables hold one hash per path, modification time and +size, so hashing the same file with a second algorithm replaces the first +algorithm's entry. Fill the cache with the algorithm your pipeline uses (the +default, `sha256`, unless you configured another). + `CachedFileHasher` wraps a `FileHasher` with any `HashCacher`. When you call `hash_file(path)` on it, it first constructs the key from the file's current metadata and queries the cacher. On a cache hit, it returns the stored digest diff --git a/docs/concepts/file-types.md b/docs/concepts/file-types.md index 73de2c2f3..558f80c9f 100644 --- a/docs/concepts/file-types.md +++ b/docs/concepts/file-types.md @@ -188,7 +188,8 @@ for _, data in total_lines.pod(everything): ## Large files Hashing a file means reading it. For large files Orcapod can cache the hash, keyed by the -file's path, size and modification time, so that an unchanged file is not read again: +file's path, size and modification time and the hash algorithm, so that an unchanged file is +not read again (a hash cached by another algorithm is never used): ```python op.enable_file_hash_caching() diff --git a/docs/dev/plans/2026-11-26-itl-1488-file-hash-cache-algorithm.md b/docs/dev/plans/2026-11-26-itl-1488-file-hash-cache-algorithm.md new file mode 100644 index 000000000..6faaf9cea --- /dev/null +++ b/docs/dev/plans/2026-11-26-itl-1488-file-hash-cache-algorithm.md @@ -0,0 +1,37 @@ +# ITL-1488: the file hash cache returns a hash made with another algorithm + +## Problem + +`CachedFileHasher` keyed a cache entry by `(path, mtime_ns, size)` only and returned a hit +without checking which algorithm made it. After `orcapod warm-cache --force --algorithm md5`, +the default (SHA-256) file handler returned the MD5 value with caching on. + +## Fix + +- `FileHashKey` gains `label`, the hash label of the algorithm (`ContentHash.method`). +- `FileContentHasherProtocol` gains `hash_label`; `FileHasher` returns its algorithm, + `CachedFileHasher` its inner hasher's label and builds the key with it. +- The cachers return an entry only for the key's label: + - `InMemoryHashCacher`: the label is part of the dict key (entries of two algorithms + coexist). + - `SqliteHashCacher`, `PostgresHashCacher`: the stored BLOB is already + `to_prefixed_digest()` (`