refactor(utils): give fast_hash an exact replacement and restore helper test cases

parser.compute_hash is not a drop-in for fast_hash: it takes str only and always returns 16
characters. The warning and docstring now name hashlib.md5(data).hexdigest()[:length], with
str encoded as UTF-8, and note compute_hash only as the str, default-length equivalent.

Restore the cases the first commit dropped from the deprecated helpers' tests: valid JSON,
several markers in one string, a dropped-context marker without a count, and bytes input to
fast_hash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tejas Chopra
2026-10-09 18:31:46 -07:00
co-authored by Claude Opus 5.5
parent 346ec05dca
commit f49ee37743
2 changed files with 19 additions and 5 deletions
+4 -3
View File
@@ -46,12 +46,13 @@ def fast_hash(data: str | bytes, length: int = 16) -> str:
content-addressable lookups in compression caches, prefix tracking, etc.
.. deprecated::
Use :func:`headroom.parser.compute_hash`, which is the same MD5
truncated to 16 hex characters.
Use ``hashlib.md5(data).hexdigest()[:length]``, encoding ``str``
input as UTF-8 first. For ``str`` input and the default length,
:func:`headroom.parser.compute_hash` returns the same value.
"""
warnings.warn(
"fast_hash is deprecated and will be removed in a future release; "
"use headroom.parser.compute_hash instead.",
"use hashlib.md5(data).hexdigest()[:length], encoding str input as UTF-8 first.",
DeprecationWarning,
stacklevel=2,
)
+15 -2
View File
@@ -131,6 +131,12 @@ def test_format_timestamp_normalizes_aware_datetimes() -> None:
("name", "args", "expected"),
[
("fast_hash", ("hello", 8), "5d41402a"),
("fast_hash", (b"hello",), "5d41402abc4b2a76"),
(
"create_dropped_context_marker",
("budget",),
'<headroom:dropped_context reason="budget">',
),
(
"create_dropped_context_marker",
("budget", 4),
@@ -143,9 +149,16 @@ def test_format_timestamp_normalizes_aware_datetimes() -> None:
),
(
"extract_markers",
('x <headroom:tool_digest sha256="abc">',),
[{"type": "tool_digest", "attributes": {"sha256": "abc"}}],
(
'x <headroom:tool_digest sha256="abc"> y '
'<headroom:dropped_context reason="budget" count="2">',
),
[
{"type": "tool_digest", "attributes": {"sha256": "abc"}},
{"type": "dropped_context", "attributes": {"reason": "budget", "count": "2"}},
],
),
("safe_json_loads", ('{"ok": true}',), ({"ok": True}, True)),
("safe_json_loads", ("{bad",), (None, False)),
("safe_json_dumps", ({"emoji": "café"},), '{"emoji":"café"}'),
],