llm-ingestion-okf/tests/test_segmented_profile.py
Kjell Tore Guttormsen ea64b9915d feat(profiles): SEGMENTED_OKF_V0_2 so a segmented bundle can declare its spec
Neither shipped profile could do both: SEGMENTED_V1 names `bundle_id` and not
`okf_version`, OKF_V0_2 names `okf_version` and has no segmentation at all. The
sixth profile is where the two intersect. Additive, as upstream support always
is here -- a new profile, never a migration.

THE INDEX POLICY IS DECIDED, NOT INHERITED, and that is the part that was easy
to get wrong. Measured: OKF_V0_2.index has facets=None and per_directory=False,
while SEGMENTED_V1.index has both. Building on OKF_V0_2's index would have
produced a segmented bundle with no faceted index -- structurally valid, SPEC
conformant, and missing the surface a consumer reads. So the index comes from
SEGMENTED_V1 with both root keys named, and the spec declaration from OKF_V0_2.
The premise is asserted in the suite rather than trusted to stay true.

`SegmentationPolicy.adjudication_key` is the discriminator BETWEEN segmented
profiles. It has to be a field with a value: every 1-to-N branch keys on
`profile.segmentation is not None`, which both segmented profiles satisfy, so a
later step surfacing the adjudication marker on that check would write it into
SEGMENTED_V1 too and move a byte-pinned golden. `None` means the profile does
not surface adjudication state at all.

`okf_version`'s VALUE is not in this module and must never be: a profile names
a key, the caller owns its value (decision E1). A constant here would claim a
decision this library does not own and would be the one thing to chase on every
upstream release. Asserted.

Five existing profiles untouched, each still equal to a freshly constructed
copy, root keys pinned literally, and OKF_LATEST still DEFAULT -- adding a
profile is not a GA event.

Suite 926 -> 931.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-02 14:21:15 +02:00

206 lines
7.9 KiB
Python

"""SEGMENTED_V1: the capability that lets one document become many concepts.
Additive in the strict sense the upstream-version policy demands. `DEFAULT`,
`STRICT_V1`, `STRUCTURED_V1` and `OKF_V0_2` keep the bytes they had, because
the new field is defaulted and every branch downstream is keyed on
`profile.segmentation is not None`.
That toggle is deliberately NOT `IndexPolicy.per_directory`. `STRICT_V1`
already sets that field True while Door B ignores it, so keying the capability
there would change `STRICT_V1`'s Door B output and break a byte-stability pin
that four consumers depend on. The measured trap is the reason the assertion
below is written as an identity check on all four profiles rather than on one.
"""
from __future__ import annotations
from dataclasses import replace
from llm_ingestion_okf.profiles import (
DEFAULT,
OKF_LATEST,
OKF_V0_2,
SEGMENTED_V1,
STRICT_V1,
STRUCTURED_V1,
BundleProfile,
SegmentationPolicy,
)
EXISTING = (DEFAULT, STRICT_V1, STRUCTURED_V1, OKF_V0_2)
# --- the capability -------------------------------------------------------
def test_only_the_new_profile_declares_the_segmentation_capability() -> None:
assert SEGMENTED_V1.segmentation is not None
for profile in EXISTING:
assert profile.segmentation is None
def test_the_capability_is_not_keyed_on_per_directory() -> None:
# STRICT_V1 already sets per_directory True and Door B ignores it. Keying
# the 1-to-N branch there would silently change STRICT_V1's output.
assert STRICT_V1.index.per_directory is True
assert STRICT_V1.segmentation is None
def test_the_new_profile_writes_an_index_per_directory_and_names_bundle_id() -> None:
assert SEGMENTED_V1.index.per_directory is True
assert SEGMENTED_V1.index.root_frontmatter == ("bundle_id",)
def test_bundle_id_is_permitted_at_the_root_rather_than_demanded_by_the_policy() -> None:
# The policy NAMES the key and fixes its position; the caller owns the
# value (D5). Demanding it here would claim a decision this library does
# not own -- Door B enforces the plan-requires-an-id rule instead.
assert "bundle_id" not in SEGMENTED_V1.index.root_frontmatter_required
def test_the_new_profile_keeps_the_structured_facets_and_keys() -> None:
assert SEGMENTED_V1.index.facets == STRUCTURED_V1.index.facets
assert SEGMENTED_V1.frontmatter.order == STRUCTURED_V1.frontmatter.order
assert SEGMENTED_V1.types is STRUCTURED_V1.types
assert SEGMENTED_V1.paths is STRUCTURED_V1.paths
# --- the additivity proof -------------------------------------------------
def test_the_four_existing_profiles_equal_freshly_constructed_copies() -> None:
# A defaulted field must not move any of them. `replace()` with no changes
# reconstructs each profile through the dataclass constructor, so a field
# that had become required -- or had picked up a non-default value -- shows
# up here as inequality rather than as golden churn six steps later.
for profile in EXISTING:
assert replace(profile) == profile
def test_okf_latest_is_still_default() -> None:
assert OKF_LATEST is DEFAULT
def test_a_profile_constructs_without_naming_the_new_field() -> None:
minimal = BundleProfile(
types=DEFAULT.types,
frontmatter=DEFAULT.frontmatter,
paths=DEFAULT.paths,
index=DEFAULT.index,
)
assert minimal.segmentation is None
# --- the policy's own defaults -------------------------------------------
def test_the_policy_names_the_keys_it_governs() -> None:
policy = SEGMENTED_V1.segmentation
assert policy is not None
assert policy.hierarchical_paths is True
assert policy.bundle_id_key == "bundle_id"
assert policy.segment_id_key == "segment_id"
assert policy.offset_key == "source_offset"
assert policy.nav_label == "index"
def test_the_policy_is_frozen() -> None:
policy = SegmentationPolicy()
try:
policy.bundle_id_key = "other" # type: ignore[misc]
except Exception:
return
raise AssertionError("SegmentationPolicy must be frozen")
# --- the sixth profile: SEGMENTED_OKF_V0_2 ---
def test_the_sixth_profile_exists_and_carries_both_capabilities() -> None:
"""A segmented bundle that can also declare which upstream spec it targets.
Neither shipped profile could: `SEGMENTED_V1` names `bundle_id` and not
`okf_version`, `OKF_V0_2` names `okf_version` and has no segmentation at
all. The sixth is what makes the two intersect.
"""
from llm_ingestion_okf.profiles import SEGMENTED_OKF_V0_2
assert SEGMENTED_OKF_V0_2.segmentation is not None
assert SEGMENTED_OKF_V0_2.index.facets is not None
assert SEGMENTED_OKF_V0_2.index.per_directory is True
assert SEGMENTED_OKF_V0_2.index.root_frontmatter == ("okf_version", "bundle_id")
def test_the_index_policy_was_decided_rather_than_inherited() -> None:
"""The premise this profile's construction rests on, asserted.
A profile built purely on `OKF_V0_2`'s index would have NO faceted index --
measured: `OKF_V0_2.index.facets is None` and `per_directory is False` --
and the adjudication marker would have nothing to project into. Inheriting
would have looked correct and produced a bundle missing half its point.
"""
from llm_ingestion_okf.profiles import OKF_V0_2, SEGMENTED_V1
assert OKF_V0_2.index.facets is None
assert OKF_V0_2.index.per_directory is False
assert SEGMENTED_V1.index.facets is not None
assert SEGMENTED_V1.index.per_directory is True
def test_the_two_segmented_profiles_are_distinguishable_by_a_policy_field() -> None:
"""`profile.segmentation is not None` CANNOT be the discriminator.
Both segmented profiles satisfy it, so a later step keying the adjudication
marker on it would write the marker into `SEGMENTED_V1` too and move a
byte-pinned golden. The discriminator has to be a field with a value.
"""
from llm_ingestion_okf.profiles import SEGMENTED_OKF_V0_2, SEGMENTED_V1
assert SEGMENTED_V1.segmentation is not None
assert SEGMENTED_OKF_V0_2.segmentation is not None
assert SEGMENTED_V1.segmentation.adjudication_key is None
assert SEGMENTED_OKF_V0_2.segmentation.adjudication_key == "adjudication"
# And the discriminator is the ONLY thing that differs: the sixth profile's
# segmentation policy is otherwise a copy, asserted rather than assumed so
# a field added to one and not the other cannot pass unnoticed.
from dataclasses import replace
assert replace(SEGMENTED_OKF_V0_2.segmentation, adjudication_key=None) == (
SEGMENTED_V1.segmentation
)
def test_the_five_existing_profiles_are_untouched() -> None:
"""Additive means additive: every prior profile still equals a freshly
constructed copy of itself, and its root keys are pinned literally."""
from dataclasses import replace
from llm_ingestion_okf.profiles import (
DEFAULT,
OKF_LATEST,
OKF_V0_2,
SEGMENTED_V1,
STRICT_V1,
STRUCTURED_V1,
)
assert DEFAULT.index.root_frontmatter == ()
assert OKF_V0_2.index.root_frontmatter == ("okf_version",)
assert SEGMENTED_V1.index.root_frontmatter == ("bundle_id",)
for profile in (DEFAULT, STRICT_V1, STRUCTURED_V1, OKF_V0_2, SEGMENTED_V1):
assert replace(profile) == profile
assert profile.renderers is None
assert OKF_LATEST is DEFAULT, "the GA alias is not flipped by adding a profile"
def test_the_sixth_profile_hard_codes_no_upstream_version_value() -> None:
"""A profile NAMES a key; the caller owns its value (decision E1).
`okf_version`'s value tracks the upstream Google version and belongs to
catalog, so a constant here would claim a decision this library does not
own -- and would be the one thing to chase on every upstream release.
"""
from llm_ingestion_okf.profiles import SEGMENTED_OKF_V0_2
assert "okf_version" in SEGMENTED_OKF_V0_2.index.root_frontmatter
assert "0.2" not in repr(SEGMENTED_OKF_V0_2.index.root_frontmatter)