Skip to content

Commit 2a8b97a

Browse files
yotamleoclaude
authored andcommitted
fix(dedup): say what the entity predicate proves and what it assumes (#296)
Absence of a `node_kind` stamp is not proof that a node is an entity: an extractor can mint a node standing for part of its file without stamping one (detect.py's per-sheet document nodes do exactly that), and the predicate read those as entities while its name and docstring claimed proof. Renames it `_reads_as_file_entity` and splits the docstring into what is PROVEN (not the file's own node — an `_id_prefixes` reconstruction that holds for every stored-path spelling) and what is ASSUMED (not a section, resting on the producer stamping `node_kind`). Adds the producer contract next to `_FILE_STRUCTURE_NODE_KINDS` and a test pinning the stamped/unstamped split, so the limit is enforced rather than described. No behaviour change: the same nodes merge as before. Co-Authored-By: Claude <[email protected]> (cherry picked from commit d123254)
1 parent 124ca10 commit 2a8b97a

2 files changed

Lines changed: 59 additions & 34 deletions

File tree

‎graphify/dedup.py‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -306,34 +306,43 @@ def _defines_id(node: dict) -> bool:
306306
# one of its sections. The markdown extractor stamps these precisely because
307307
# `file_type` cannot carry the distinction, and both are file-anchored in the
308308
# #1284 sense — two files' `## Decisions` sections are two sections (#3094).
309+
#
310+
# PRODUCER CONTRACT: an extractor that mints a node standing for a *part* of its
311+
# source file (a section, a sheet, a slide) must stamp one of these. The gate
312+
# below reads an unstamped node as an entity, so an unstamped structural node is
313+
# eligible to merge with its namesake in another file.
309314
_FILE_STRUCTURE_NODE_KINDS = frozenset({"page", "heading"})
310315

311316

312-
def _provably_not_file_structure(node: dict) -> bool:
313-
"""True only when the node is PROVABLY an entity found inside its file,
314-
rather than a structural part of the file itself (#296).
317+
def _reads_as_file_entity(node: dict) -> bool:
318+
"""True when the node reads as an entity found inside its file, rather than
319+
as the file itself or a stamped structural part of it (#296).
315320
316-
Two things disqualify a node, and either alone is enough:
321+
Be precise about what is proven and what is assumed, because the two halves
322+
differ in strength:
317323
318-
* It is its file's OWN node. ``_id_prefixes`` enumerates the ID a node
319-
standing for ``source_file`` itself would carry, in every spelling a
320-
stored path may take (absolute, repo-relative, or the pre-#1504 bare
324+
* PROVEN — it is not its file's OWN node. ``_id_prefixes`` enumerates the ID
325+
a node standing for ``source_file`` itself would carry, in every spelling
326+
a stored path may take (absolute, repo-relative, or the pre-#1504 bare
321327
stem). A file's own node IS one of those; an entity extracted from that
322-
file carries an ``_<entity>`` suffix, so it never equals one.
323-
* It is a section of the file — ``node_kind: "heading"`` — or the file node
324-
the extractor labelled outright, ``node_kind: "page"``. Repeated headings
325-
across sibling documents are distinct sections, not duplicates
326-
(#1284, re-verified on #3094), and stay blocked.
327-
328-
Fail-safe by construction: a node that cannot be checked (no ID, no
329-
provenance) answers False and stays blocked, so this can only ever *narrow*
330-
the set of nodes the cross-file gate treats as file-anchored — never widen
331-
it on a guess.
328+
file carries an ``_<entity>`` suffix, so it never equals one. This is a
329+
reconstruction, not a heuristic.
330+
* ASSUMED — it is not a section of the file. That rests on ``node_kind``,
331+
which only a producer that stamps it can attest. `heading`/`page` are
332+
honoured when present, but ABSENCE OF THE MARKER IS NOT PROOF OF
333+
ENTITY-NESS: a producer minting sub-file nodes without stamping
334+
`node_kind` (see the contract above) yields structural nodes that this
335+
returns True for, and two such nodes sharing a label in different files
336+
would merge. The conservative fix is on the producer side — stamp
337+
`node_kind` — not a guess here about what an unstamped node meant.
338+
339+
A node that cannot be checked at all (no ID, no provenance) answers False
340+
and stays blocked.
332341
"""
333342
nid = node.get("id") or ""
334343
source_file = node.get("source_file") or ""
335344
if not nid or not source_file:
336-
return False # unprovable — leave the file-anchored block in place
345+
return False # uncheckable — leave the file-anchored block in place
337346
if node.get("node_kind") in _FILE_STRUCTURE_NODE_KINDS:
338347
return False # the extractor says this node is part of the file's structure
339348
return nid not in _id_prefixes(source_file)
@@ -670,8 +679,8 @@ def deduplicate_entities(
670679
# it is provably safe (#2182). `concept` is the one file_type meant to
671680
# unify across files (#1284) — code is keyed by ID (#1205) and
672681
# image/paper labels are often shared basenames (logo.png), so both stay
673-
# blocked. rationale/document join `concept` here ONLY when the node is
674-
# provably not part of its file's own structure (#296): a file-anchored
682+
# blocked. rationale/document join `concept` here ONLY when the node
683+
# reads as an entity inside its file (#296): a file-anchored
675684
# *file_type* does not make an individual node file-anchored. An entity
676685
# extracted from a note — a person, a project — inherits `document` from
677686
# the file's extension, not from anything about itself, so in note-heavy
@@ -688,7 +697,7 @@ def deduplicate_entities(
688697
(n for n in group
689698
if (n.get("file_type") == "concept"
690699
or (n.get("file_type") in _FILE_ANCHORED_NONCODE
691-
and _provably_not_file_structure(n)))
700+
and _reads_as_file_entity(n)))
692701
and (n.get("source_file") or "")
693702
and _entropy(n.get("label", "")) >= _ENTROPY_THRESHOLD),
694703
key=lambda n: n["id"],

‎tests/test_dedup.py‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,31 +1219,32 @@ def test_same_word_variant_helper():
12191219
_CYRIL_VARIANTS = ["@cyrilXBT", "@cyrilxbt", "cyrilXBT"]
12201220

12211221

1222-
def test_provably_not_file_structure_helper():
1223-
"""The predicate proves entity-ness, and answers False whenever it cannot
1224-
(#296): a file's own node, a structural `page`/`heading` node, and any node
1225-
missing an id or provenance all stay treated as file-anchored."""
1226-
from graphify.dedup import _provably_not_file_structure
1222+
def test_reads_as_file_entity_helper():
1223+
"""A file's own node, a stamped `page`/`heading` node, and any node missing
1224+
an id or provenance all stay treated as file-anchored (#296). The own-node
1225+
half is a reconstruction and holds for every stored-path spelling; the
1226+
structural half rests on the producer stamping `node_kind`."""
1227+
from graphify.dedup import _reads_as_file_entity
12271228
# An entity extracted from a note: `<path>_<entity>` never equals the path.
1228-
assert _provably_not_file_structure(
1229+
assert _reads_as_file_entity(
12291230
{"id": "journal_2024_03_01_cyrilxbt", "label": "@cyrilXBT",
12301231
"source_file": "journal/2024-03-01.md"})
12311232
# The file's own node, in each spelling a stored source_file may take.
1232-
assert not _provably_not_file_structure(
1233+
assert not _reads_as_file_entity(
12331234
{"id": "journal_2024_03_01", "source_file": "journal/2024-03-01.md"})
1234-
assert not _provably_not_file_structure(
1235+
assert not _reads_as_file_entity(
12351236
{"id": "2024_03_01", "source_file": "journal/2024-03-01.md"}) # pre-#1504 bare stem
1236-
assert not _provably_not_file_structure(
1237+
assert not _reads_as_file_entity(
12371238
{"id": "vault_journal_2024_03_01", "source_file": 'C:\\vault\\journal\\2024-03-01.md'})
12381239
# The markdown extractor states it outright, for the file and its sections.
1239-
assert not _provably_not_file_structure(
1240+
assert not _reads_as_file_entity(
12401241
{"id": "anything", "node_kind": "page", "source_file": "docs/a.md"})
1241-
assert not _provably_not_file_structure(
1242+
assert not _reads_as_file_entity(
12421243
{"id": "docs_a_decisions", "node_kind": "heading",
12431244
"label": "Decisions", "source_file": "docs/a.md"})
12441245
# Unprovable -- no id, or no provenance.
1245-
assert not _provably_not_file_structure({"id": "", "source_file": "docs/a.md"})
1246-
assert not _provably_not_file_structure({"id": "docs_a_thing", "source_file": ""})
1246+
assert not _reads_as_file_entity({"id": "", "source_file": "docs/a.md"})
1247+
assert not _reads_as_file_entity({"id": "docs_a_thing", "source_file": ""})
12471248

12481249

12491250
def test_dedup_merges_crossfile_document_entity_variants():
@@ -1368,3 +1369,18 @@ def test_dedup_never_merges_repeated_headings_across_files():
13681369
)
13691370
assert {n["source_file"] for n in result_nodes} == {
13701371
"docs/a.md", "docs/b.md", "docs/c.md"}
1372+
1373+
1374+
def test_reads_as_file_entity_trusts_the_node_kind_stamp_only():
1375+
"""The documented limit of the structural half (#296): `node_kind` is what
1376+
marks a sub-file node, so an UNSTAMPED structural node reads as an entity.
1377+
Pinned deliberately — the fix for such a producer is to stamp `node_kind`,
1378+
and this test is what fails if the predicate is ever quietly changed to
1379+
guess instead."""
1380+
from graphify.dedup import _reads_as_file_entity
1381+
stamped = {"id": "book_xlsx_summary", "label": "Summary (sheet)",
1382+
"node_kind": "heading", "source_file": "book.xlsx"}
1383+
unstamped = dict(stamped)
1384+
del unstamped["node_kind"]
1385+
assert not _reads_as_file_entity(stamped)
1386+
assert _reads_as_file_entity(unstamped)

0 commit comments

Comments
 (0)