test(structure): the index resolves a parent naming a segment of its own document

K3-21 C, red. `structure` read `parent` as a document number only; a
`segment_id` answers to no key its lookup builds, so every `--shell-parent`
pointer rendered unresolved (`parent: p1977?`, 675 of 675 on one standard)
and both declared parents in each segmented golden render `parent: s0?` --
while the concept each names stands in the bundle.

Held here: a segment pointer resolves among the concepts sharing the
pointer's `source_file`; the same id in another document is not the parent; a
pointer naming nothing still carries `UNRESOLVED_MARKER`; a parent that is a
document number resolves as before; the flagged fixture build's index renders
no parent unresolved.

3 of 5 red on 46e555d; the two green are the unchanged routes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-09-11 13:48:11 +02:00
commit c188a0c749

View file

@ -0,0 +1,94 @@
"""The index resolves a `parent` that names a segment of the same document.
K3-21 C. The index is a projection of each concept's frontmatter, and
`structure` read `parent` as a document NUMBER only: its lookup answers to a
concept's name, its `source_file` and that file's stem, and to document
numbers. A `segment_id` answers to none of them, so every pointer
`--shell-parent` writes rendered unresolved (`parent: p1977?`) while the
concept it names stands in the bundle -- 675 of 675 on one standard, and both
declared parents in each segmented golden (`parent: s0?`).
A segment id is unique only inside the plan of ONE document, so it is looked
up among the concepts sharing the pointing concept's `source_file`, never
across the bundle. `UNRESOLVED_MARKER` keeps its meaning: a pointer that names
nothing still says so. The same key carries a declared segment id and a
derived document number (`inbox.py`), and the number route is unchanged.
`sts-inherit.xml` is hand-written in an invented setting and carries no
sentence from any source.
"""
from __future__ import annotations
from pathlib import Path
from llm_ingestion_okf import cli, structure
FIXTURE = Path(__file__).parent / "fixtures" / "sts-inherit.xml"
def _doc(**values: str) -> structure.DocumentStructure:
return structure.structure_from_frontmatter(values)
def _parent(documents: dict[str, structure.DocumentStructure], name: str) -> str:
resolved = structure.resolve_structure(documents)
return structure.facet_values(name, resolved, ("parent",))["parent"]
def test_a_parent_naming_a_segment_of_the_same_document_resolves() -> None:
documents = {
"a/one.md": _doc(title="One", source_file="a.xml", segment_id="p1"),
"a/two.md": _doc(title="Two", source_file="a.xml", segment_id="p2", parent="p1"),
}
resolved = structure.resolve_structure(documents)
edge = next(edge for edge in resolved.edges if edge.kind == "parent")
assert edge.target == "a/one.md"
assert _parent(documents, "a/two.md") == "p1"
def test_the_same_segment_id_in_another_document_is_not_the_parent() -> None:
documents = {
"a/one.md": _doc(title="One", source_file="a.xml", segment_id="p1"),
"b/one.md": _doc(title="One", source_file="b.xml", segment_id="p1"),
"b/two.md": _doc(title="Two", source_file="b.xml", segment_id="p2", parent="p1"),
}
resolved = structure.resolve_structure(documents)
edge = next(edge for edge in resolved.edges if edge.kind == "parent")
assert edge.target == "b/one.md"
def test_a_pointer_naming_nothing_still_says_so() -> None:
documents = {
"a/two.md": _doc(title="Two", source_file="a.xml", segment_id="p2", parent="p9"),
}
assert _parent(documents, "a/two.md") == "p9" + structure.UNRESOLVED_MARKER
def test_a_parent_that_is_a_document_number_resolves_as_before() -> None:
documents = {
"n1.md": _doc(title="N1", source_file="n1.md", number="1"),
"n11.md": _doc(title="N11", source_file="n11.md", number="1.1", parent="1"),
}
resolved = structure.resolve_structure(documents)
edge = next(edge for edge in resolved.edges if edge.kind == "parent")
assert edge.target == "n1.md"
assert _parent(documents, "n11.md") == "1"
def test_the_flagged_build_s_index_renders_no_parent_unresolved(tmp_path: Path) -> None:
inbox = tmp_path / "inbox"
inbox.mkdir()
(inbox / FIXTURE.name).write_bytes(FIXTURE.read_bytes())
bundle = tmp_path / "bundle"
argv = ["build", str(inbox), "--bundle", str(bundle), "--bundle-id", "inherit-fixture"]
assert cli.main([*argv, "--okf-version", "0.2", "--shell-parent"]) == 0
facets = [
line
for index in bundle.rglob("index.md")
for line in index.read_text(encoding="utf-8").split("\n")
if "parent: " in line
]
# The four shells with an ancestor holding text (`tests/test_shell_parent.py`).
assert len(facets) == 4
assert not [line for line in facets if "?" in line.split("parent: ", 1)[1].split(";")[0]]