Drawer/closet/triple IDs built by concatenating strings without a delimiter before hashing can collide: hash((s1 + str(i1))) == hash((s2 + str(i2))) whenever s1+str(i1) == s2+str(i2). Concretely, source_file="foo" + chunk_index=12 produces the same hash input as source_file="foo1" + chunk_index=2 — both yield "foo12". Under ChromaDB's primary-key constraint the second write silently overwrites the first; one chunk's content is lost without surfacing an error (the surrounding broad `except Exception:` blocks swallow any backend complaint). The defect class is "string + string concat fed to a hash without a delimiter." Per the styleguide's partial-scope-key-migration rule, every site sharing the pattern is a candidate and must be triaged — not just the ones the original issue named. FIX — 6 sites - mempalace/miner.py:1253 drawer_id, add_drawer() back-compat - mempalace/miner.py:1386 drawer_id, batched mine loop - mempalace/miner.py:1416 drawer_ids, closet-build re-derivation - mempalace/format_miner.py:643 drawer_id, format_miner batched loop - mempalace/mcp_server.py:1136 drawer_id, tool_add_drawer - mempalace/knowledge_graph.py:305 triple_id, KG triple insertion MIGRATED FOR CONSISTENCY — 2 sites - mempalace/convo_miner.py:87 sentinel_key — was `:`, now `|` - mempalace/convo_miner.py:422 drawer_key — was `:`, now `|` Pre-existing delimiter was `:`. Migrated because (a) source_file can contain literal `:` on Windows paths (`C:\Users\…`) and URL-like sources (`https://host:8080`), weakening `:` as a separator; (b) `|` is reserved in Windows filenames so source_file cannot contain it, making it strictly safer. DELIMITER CHOICE - `|` chosen over `:` (Windows / URL source_file edge cases) - `|` chosen over `\x00` (NUL breaks log greppability + print() debug + downstream string handling for marginal extra safety) - Matches the established precedent in mempalace/diary_ingest.py (lines 52, 76, 91, 98 — all already on `|`) EXEMPT — audited and correct as-is Single-input hashes (nothing to delimit): - mempalace/miner.py:1432 closet_id (source_file only) - mempalace/format_miner.py:559 sentinel_id (source_file only) - mempalace/palace.py:433 lock filename (source_file only) - mempalace/palace.py:629 palace_key (lock_key_source only) - mempalace/diary_ingest.py:158 content_hash (text only) - mempalace/hooks_cli.py:329 pidfile digest (joined cmd only) - mempalace/sources/context.py:141 record digest (source_file only) Already correctly delimited: - mempalace/hallways.py:157 `f"{wing}::{a}::{b}"` (`::`) - mempalace/palace_graph.py:454 `f"{a}↔{b}"` (`↔`) - mempalace/diary_ingest.py:52,76,91,98 (`|` precedent) Protected by composition (uniqueness guaranteed by the ID prefix, not by the hash slice): - mempalace/mcp_server.py:1635 entry_id is `diary_{wing}_{strftime('%Y%m%d_%H%M%S%f')}_{sha[:12]}`. Microsecond-resolution timestamp prefix supplies uniqueness; the trailing hash is a content-discriminator, not the write-time uniqueness guarantor. NEW MODULES - mempalace/ids.py — single source of truth for ID construction. Five helpers (make_drawer_id_from_chunk, make_drawer_id_from_content, make_convo_drawer_id, make_convo_sentinel_id, make_triple_id) plus an ID_RECIPE = "v2" constant. - mempalace/collision_scan.py — pre-mining defense. Runs immediately before each batched ChromaDB upsert; raises CollisionError naming the colliding (source_file, chunk_index) pairs if any proposed drawer_id appears more than once with conflicting metadata across the union of incoming and existing rows. DETECTION ChromaDB's primary-key constraint means past collisions were silently resolved at upsert time (last-write-wins), erasing evidence. A post-hoc audit cannot reconstruct what was lost from palace state alone. Therefore: - Pre-mining risk scan. Before each batched upsert, compute the proposed drawer_ids for the incoming chunk set AND query existing drawer_ids from the collection. If any proposed id appears more than once in the union (incoming-vs-incoming or incoming-vs- existing) with conflicting (source_file, chunk_index), abort the mine with an actionable error naming the colliding pairs. Collision is caught BEFORE it destroys data, which is the only point at which palace state still carries the evidence. - New metadata key: `"id_recipe": "v2"` on every drawer written under the delimited recipe. Audits compare like-for-like; drawers without `id_recipe` are treated as v1 legacy (undelimited or `:`-delimited), not as collisions. - Honest disclosure: palaces mined under any pre-v2 mempalace may carry silent past collisions whose original content is unrecoverable from palace state. Future library tier work will give users a per-drawer audit + opt-in archival path. TESTS - tests/test_ids.py: 17 unit tests covering all 5 helpers, the ID_RECIPE constant, the private `_delimited_sha256` helper, and the four defect-class collision shapes (chunk_index boundary, content boundary, extract_mode boundary, ISO datetime boundary). RED collision tests confirm the v2 recipe breaks the defect class. - tests/test_collision_scan.py: 10 tests covering clean batches, idempotent re-mines, incoming-vs-incoming collisions, incoming-vs- existing collisions, error-message quality, empty batches, metadata without chunk_index, and ChromaDB backend errors propagating cleanly. - tests/test_convo_miner_unit.py: FakeCol gains a stub `get()` so the pre-mining scan can probe an empty in-test collection. BACKWARDS COMPATIBILITY - Existing v1 drawers remain queryable; no rewrite of stored IDs. - New mining passes write v2 drawers alongside v1 via PR #1628's additive-mining model. - No user action required; opt-in cleanup ships separately. VERIFICATION - macOS Python 3.12 (local): 2304 passed, 3 skipped, 0 failed, coverage 85.44% (above 80% threshold). - Linux Python 3.9/3.11/3.13 (OrbStack with full `pip install -e '.[dev]'` flow): see PR body for per-version pass counts. - `ruff check .` + `ruff format --check .`: clean. - pylint score: ids.py 10.00/10, collision_scan.py 10.00/10; all modified files >= 8.92 (no regression). - bandit: clean on all new files; the one MEDIUM finding in knowledge_graph.py is on lines 385/407 (pre-existing SQL string construction in timeline queries), not in code touched by this PR. - pre_push_check.sh: ALL CHECKS PASSED. Refs: deferred from PR #1572 (Copilot finding #13). Styleguide audit documented above: 8 fix/migration + 11 enumerated exempt sites.
187 lines
8.0 KiB
Python
187 lines
8.0 KiB
Python
"""Tests for mempalace.ids — collision-safe ID construction.
|
|
|
|
The RED test that pins this whole PR is
|
|
``test_make_drawer_id_from_chunk_does_not_collide_across_boundary`` — it
|
|
constructs the classic ``"/path/a1" + "23" == "/path/a" + "123"`` collision
|
|
shape and asserts the new delimiter-based recipe produces distinct IDs.
|
|
Against the pre-v2 recipe (no delimiter), this test FAILS. Against v2,
|
|
it PASSES.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
|
|
from mempalace import ids
|
|
|
|
|
|
# ── ID_RECIPE constant ─────────────────────────────────────────────────
|
|
|
|
|
|
def test_id_recipe_constant_is_v2():
|
|
"""Audit code reads ids.ID_RECIPE to tag new drawers. The constant
|
|
must be the literal "v2" string; a typo here silently re-introduces
|
|
the ambiguity v2 was meant to fix."""
|
|
assert ids.ID_RECIPE == "v2"
|
|
|
|
|
|
# ── make_drawer_id_from_chunk ─────────────────────────────────────────
|
|
|
|
|
|
def test_make_drawer_id_from_chunk_returns_expected_prefix():
|
|
"""Drawer IDs are namespaced by wing and room so cross-wing
|
|
collisions are impossible regardless of the hash slice."""
|
|
result = ids.make_drawer_id_from_chunk("proj", "log", "/a", 0)
|
|
assert result.startswith("drawer_proj_log_")
|
|
|
|
|
|
def test_make_drawer_id_from_chunk_hash_length_is_24_hex():
|
|
"""The hash slice must be 24 hex chars to keep drawer IDs storable
|
|
in fixed-width metadata columns and to match the historical recipe
|
|
length."""
|
|
result = ids.make_drawer_id_from_chunk("w", "r", "/file.md", 5)
|
|
hash_part = result.removeprefix("drawer_w_r_")
|
|
assert len(hash_part) == 24
|
|
assert all(c in "0123456789abcdef" for c in hash_part)
|
|
|
|
|
|
def test_make_drawer_id_from_chunk_does_not_collide_across_boundary():
|
|
"""RED test pinning the whole PR.
|
|
|
|
Classic collision: source_file="/path/a1" + chunk_index=23 produces
|
|
hash input "/path/a123" under the pre-v2 recipe. source_file="/path/a"
|
|
+ chunk_index=123 produces the SAME "/path/a123" — same hash, same
|
|
drawer_id, second ChromaDB upsert overwrites the first.
|
|
|
|
Under the v2 recipe (delimiter '|'), the two inputs become
|
|
"/path/a1|23" and "/path/a|123" — distinct strings, distinct
|
|
hashes, distinct drawer IDs. No collision.
|
|
"""
|
|
a = ids.make_drawer_id_from_chunk("w", "r", "/path/a1", 23)
|
|
b = ids.make_drawer_id_from_chunk("w", "r", "/path/a", 123)
|
|
assert a != b, f"Collision survived v2 recipe: {a!r} == {b!r}"
|
|
|
|
|
|
def test_make_drawer_id_from_chunk_is_deterministic():
|
|
"""Same inputs must always produce the same ID — re-mining a file
|
|
that hasn't changed must hit the same drawer slot, or
|
|
file_already_mined() loses its idempotency."""
|
|
a = ids.make_drawer_id_from_chunk("w", "r", "/file.md", 7)
|
|
b = ids.make_drawer_id_from_chunk("w", "r", "/file.md", 7)
|
|
assert a == b
|
|
|
|
|
|
def test_make_drawer_id_from_chunk_windows_path_with_colon_does_not_collide():
|
|
"""Windows paths contain ':' in drive letters (C:\\Users\\...). The
|
|
v2 recipe uses '|' precisely so paths that contain ':' can never
|
|
align with a chunk index to collide. This test would FAIL on a
|
|
':'-delimited recipe because Windows paths and URL-like paths
|
|
(https://host:8080) commonly end in ':digits'."""
|
|
a = ids.make_drawer_id_from_chunk("w", "r", "C:\\Users\\foo", 5)
|
|
b = ids.make_drawer_id_from_chunk("w", "r", "C:\\Users\\foo:", 5)
|
|
assert a != b
|
|
|
|
|
|
# ── make_drawer_id_from_content ───────────────────────────────────────
|
|
|
|
|
|
def test_make_drawer_id_from_content_does_not_collide_across_boundary():
|
|
"""mcp_server.py:1136 hashes wing+room+content with no delimiter.
|
|
Architecturally identical defect to the chunk-index sites:
|
|
wing="foo"+room="bar" hashes the same as wing="fooba"+room="r".
|
|
v2 delimiter breaks this."""
|
|
a = ids.make_drawer_id_from_content("foo", "bar", "x")
|
|
b = ids.make_drawer_id_from_content("fooba", "r", "x")
|
|
assert a != b
|
|
|
|
|
|
def test_make_drawer_id_from_content_returns_expected_prefix():
|
|
"""Same namespacing pattern as the chunk-index helper."""
|
|
result = ids.make_drawer_id_from_content("proj", "scratch", "hello")
|
|
assert result.startswith("drawer_proj_scratch_")
|
|
|
|
|
|
# ── make_convo_drawer_id ──────────────────────────────────────────────
|
|
|
|
|
|
def test_make_convo_drawer_id_does_not_collide_across_extract_mode_boundary():
|
|
"""convo_miner.py:422 hashes source_file+extract_mode+chunk_index.
|
|
Pre-v2 used ':' as delimiter — this test would still PASS on the ':'
|
|
recipe for clean inputs, but the migration to '|' is for
|
|
consistency with the chunk-index helpers and to remove the
|
|
Windows-path / URL-source edge case where ':' can appear in the
|
|
source_file itself."""
|
|
a = ids.make_convo_drawer_id("w", "r", "/log.jsonl", "general", 5)
|
|
b = ids.make_convo_drawer_id("w", "r", "/log.jsonl", "extract", 5)
|
|
assert a != b
|
|
|
|
|
|
def test_make_convo_drawer_id_returns_expected_prefix():
|
|
result = ids.make_convo_drawer_id("claude", "diary", "/c.jsonl", "general", 0)
|
|
assert result.startswith("drawer_claude_diary_")
|
|
|
|
|
|
# ── make_convo_sentinel_id ────────────────────────────────────────────
|
|
|
|
|
|
def test_make_convo_sentinel_id_returns_expected_prefix():
|
|
"""Sentinel IDs are namespaced under '_reg_' so they can be
|
|
filtered out of normal drawer queries."""
|
|
result = ids.make_convo_sentinel_id("/c.jsonl", "general")
|
|
assert result.startswith("_reg_")
|
|
|
|
|
|
def test_make_convo_sentinel_id_distinguishes_extract_modes():
|
|
a = ids.make_convo_sentinel_id("/c.jsonl", "general")
|
|
b = ids.make_convo_sentinel_id("/c.jsonl", "extract")
|
|
assert a != b
|
|
|
|
|
|
# ── make_triple_id ────────────────────────────────────────────────────
|
|
|
|
|
|
def test_make_triple_id_returns_expected_prefix():
|
|
"""Triple IDs prefix with 't_' and embed the subject/predicate/object
|
|
triple in the ID for grep-ability in SQLite."""
|
|
result = ids.make_triple_id("sub1", "loves", "obj1", "2026-01-01", "2026-05-30T10:00:00")
|
|
assert result.startswith("t_sub1_loves_obj1_")
|
|
|
|
|
|
def test_make_triple_id_hash_length_is_12_hex():
|
|
"""Triple IDs historically truncate at 12 hex chars (vs 24 for
|
|
drawers) because the subject/predicate/object prefix already
|
|
supplies the bulk of the namespace."""
|
|
result = ids.make_triple_id("s", "p", "o", "2026-01-01", "2026-05-30T10:00:00")
|
|
hash_part = result.removeprefix("t_s_p_o_")
|
|
assert len(hash_part) == 12
|
|
|
|
|
|
def test_make_triple_id_does_not_collide_across_iso_datetime_boundary():
|
|
"""Pre-v2 hash input was f'{valid_from}{recorded_at}' with no
|
|
delimiter — two ISO datetimes concatenated could in principle
|
|
collide (valid_from='2026-01-01' + recorded_at='T12:00:00' ==
|
|
valid_from='2026-01-01T12' + recorded_at=':00:00' for hash
|
|
purposes). v2 delimiter prevents this."""
|
|
a = ids.make_triple_id("s", "p", "o", "2026-01-01", "T12:00:00")
|
|
b = ids.make_triple_id("s", "p", "o", "2026-01-01T12", ":00:00")
|
|
assert a != b
|
|
|
|
|
|
# ── _delimited_sha256 (private helper, smoke test only) ───────────────
|
|
|
|
|
|
def test_private_delimited_sha256_uses_pipe_delimiter():
|
|
"""Confirms the implementation actually uses '|' and not ':' — a
|
|
subtle copy-paste from the diary_ingest precedent or a stale
|
|
':' precedent from convo_miner could regress the delimiter without
|
|
breaking the higher-level tests."""
|
|
result = ids._delimited_sha256(("a", "b"), 64)
|
|
expected = hashlib.sha256(b"a|b").hexdigest()
|
|
assert result == expected
|
|
|
|
|
|
def test_private_delimited_sha256_truncation_honoured():
|
|
"""Truncation argument actually shortens the hex output."""
|
|
result = ids._delimited_sha256(("a", "b"), 8)
|
|
assert len(result) == 8
|