test_named_targets.py

tests/test_named_targets.py

One repository root may hold several named indexes.

test_a_first_party_target_adds_nested_excludes

None test_named_targets.test_a_first_party_target_adds_nested_excludes(Path tmp_path)

A first-party target adds the nested trees to the caller's exclusions.

tests/test_named_targets.py:234

Checks

  • assert "build" in got_exclude, "the operator's own exclusion was dropped"
  • assert "extern/dep" in got_exclude, "the nested tree was not excluded from first-party"
  • assert got_options is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_a_first_party_target_adds_nested_excludes → _sub_index_scope → declared_sub_index_excludes
REQ-DDB-GUARD-012inferredtest_a_first_party_target_adds_nested_excludes → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node
REQ-DDB-INDEX-002measuredtest_a_first_party_target_adds_nested_excludes → _sub_index_scope → derive_sub_indexes
REQ-DDB-MCP-001measuredtest_a_first_party_target_adds_nested_excludes → target_for
REQ-DDB-WIKI-054inferredtest_a_first_party_target_adds_nested_excludes → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all
Source · 25 lines
def test_a_first_party_target_adds_nested_excludes(tmp_path: Path) -> None:
    """ADDS, never replaces. The operator's own exclusions and the derived ones answer different
    questions — "what I do not want indexed" and "what belongs to another index" — and dropping
    either produces a build that is quietly wrong in a different direction.

    @brief First-party excludes the nested trees on top of the caller's list.
    @version 1
    """
    from clew.mcp_server.server import _sub_index_scope
    from clew.mcp_server.state import target_for
    from clew.scope import FIRST_PARTY_INDEX

    repo = tmp_path / "vendored"
    (repo / "app").mkdir(parents=True)
    nested = repo / "extern" / "dep"
    nested.mkdir(parents=True)
    (nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")

    first = target_for(repo, tmp_path / "state", name=FIRST_PARTY_INDEX)
    got_exclude, got_options = _sub_index_scope(first, repo.resolve(), ["build"], None)
    assert "build" in got_exclude, "the operator's own exclusion was dropped"
    assert "extern/dep" in got_exclude, "the nested tree was not excluded from first-party"
    assert got_options is None

Activity · 13 steps
lines 242–242from clew.mcp_server.server import _sub_index_scopelines 243–243from clew.mcp_server.state import target_forlines 244–244from clew.scope import FIRST_PARTY_INDEXlines 246–246repo = tmp_path / "vendored"lines 247–247(repo / "app").mkdir(parents=True)lines 248–248nested = repo / "extern" / "dep"lines 249–249nested.mkdir(parents=True)lines 250–250(nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")lines 252–252first = target_for(repo, tmp_path / "state", name=FIRST_PARTY_INDEX)lines 253–253got_exclude, got_options = _sub_index_scope(first, repo.resolve(), ["build"], None)lines 254–254assert "build" in got_exclude, "the operator's own exclusion was dropped"lines 255–255assert "extern/dep" in got_exclude, "the nested tree was not excluded from first-party"lines 256–256assert got_options is None

test_a_hostile_name_cannot_escape_the_state_directory

None test_named_targets.test_a_hostile_name_cannot_escape_the_state_directory(Path tmp_path)

A name that would escape the state directory is refused or neutralised.

tests/test_named_targets.py:117

Show on the map →

Called by none

Calls target_for

Checks

  • assert resolved.is_relative_to(state), (f"name {hostile!r} placed a database outside the state root: {resolved}" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measuredtest_a_hostile_name_cannot_escape_the_state_directory → target_for
Source · 26 lines
def test_a_hostile_name_cannot_escape_the_state_directory(tmp_path: Path) -> None:
    """THE NAME REACHES A FILESYSTEM PATH, so it is untrusted input to a path join. A name of
    `../../etc` would otherwise place a database outside the state root — and sub-index names
    come from a repository's own `.clew.yaml`, which is exactly the "target repo influences the
    build" surface this project keeps hardening.

    Asserted as containment rather than as a specific rejection, so either refusing or
    sanitising satisfies it — what must not happen is a path outside the root.

    @brief A sub-index database stays under the state root.
    @version 1
    """
    repo = tmp_path / "myproj"
    repo.mkdir()
    state = (tmp_path / "state").resolve()
    for hostile in ("../../etc", "..", "a/b", "with space", "sub\x00idx"):
        try:
            got = target_for(repo, state, name=hostile)
        except (ValueError, OSError):
            continue
        resolved = Path(got.db_path).resolve()
        assert resolved.is_relative_to(state), (
            f"name {hostile!r} placed a database outside the state root: {resolved}"
        )

Activity · 10 steps
lines 129–129repo = tmp_path / "myproj"lines 130–130repo.mkdir()lines 131–131state = (tmp_path / "state").resolve()lines 132–132◇ for hostile in ("../../etc", "..", "a/b", "with space", "sub\x00idx")lines 133–133trylines 134–134got = target_for(repo, state, name=hostile)lines 135–135except (ValueError, OSError)lines 136–136continuelines 137–137resolved = Path(got.db_path).resolve()lines 138–140assert resolved.is_relative_to(state), ( f"name {hostile!r} placed a database outside the state root: {resolved}" )

test_a_legacy_record_without_repo_path_still_reloads

None test_named_targets.test_a_legacy_record_without_repo_path_still_reloads(Path tmp_path)

A registry written before sub-indexes existed still reloads.

tests/test_named_targets.py:178

Show on the map →

Called by none

Calls none

Checks

  • assert len(got) == 1
  • assert got[0].repo_path == "/some/repo", "a legacy record lost its repo path"
  • assert got[0].name is None, "a legacy record must read as the whole repository"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measured
Source · 23 lines
def test_a_legacy_record_without_repo_path_still_reloads(tmp_path: Path) -> None:
    """Every registry on disk today stores `{slug, db_path}` and nothing else, keyed by the repo
    path. The reader must keep falling back to the KEY for those, or an upgrade silently forgets
    every target the user already has.

    @brief Pre-sub-index records keep working.
    @version 1
    """
    import json

    from clew.mcp_server.state import TargetRegistry

    home = tmp_path / "state"
    home.mkdir(parents=True)
    legacy = {"/some/repo": {"slug": "repo-abc123", "db_path": "/some/state/repo-abc123/clew.db"}}
    (home / "targets.json").write_text(json.dumps(legacy), encoding="utf-8")

    got = TargetRegistry(home=home).targets()
    assert len(got) == 1
    assert got[0].repo_path == "/some/repo", "a legacy record lost its repo path"
    assert got[0].name is None, "a legacy record must read as the whole repository"

Activity · 10 steps
lines 186–186import jsonlines 188–188from clew.mcp_server.state import TargetRegistrylines 190–190home = tmp_path / "state"lines 191–191home.mkdir(parents=True)lines 192–192legacy = {"/some/repo": {"slug": "repo-abc123", "db_path": "/some/state/repo-abc123/clew.db"}}lines 193–193(home / "targets.json").write_text(json.dumps(legacy), encoding="utf-8")lines 195–195got = TargetRegistry(home=home).targets()lines 196–196assert len(got) == 1lines 197–197assert got[0].repo_path == "/some/repo", "a legacy record lost its repo path"lines 198–198assert got[0].name is None, "a legacy record must read as the whole repository"

test_a_named_target_is_distinct

None test_named_targets.test_a_named_target_is_distinct(Path tmp_path)

A named target gets its own slug and its own database.

tests/test_named_targets.py:73

Show on the map →

Called by none

Calls target_for

Checks

  • assert len({plain.slug, first.slug, vendor.slug}) == 3, (f"slugs collided: {plain.slug}, {first.slug}, {vendor.slug}" )
  • assert len({plain.db_path, first.db_path, vendor.db_path}) == 3, "db paths collided"
  • assert first.repo_path == vendor.repo_path == plain.repo_path

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measuredtest_a_named_target_is_distinct → target_for
Source · 26 lines
def test_a_named_target_is_distinct(tmp_path: Path) -> None:
    """Two sub-indexes of ONE root must not share a database — that is the whole feature. The
    assertion is on both the slug and the db path, because the slug is what a caller addresses
    and the path is what the build writes; a change that made them disagree would route reads
    and writes to different files.

    @brief Named targets differ from the unnamed one and from each other.
    @version 1
    """
    repo = tmp_path / "myproj"
    repo.mkdir()
    state = tmp_path / "state"

    plain = target_for(repo, state)
    first = target_for(repo, state, name="first-party")
    vendor = target_for(repo, state, name="llama-cpp")

    assert len({plain.slug, first.slug, vendor.slug}) == 3, (
        f"slugs collided: {plain.slug}, {first.slug}, {vendor.slug}"
    )
    assert len({plain.db_path, first.db_path, vendor.db_path}) == 3, "db paths collided"
    ## Every sub-index still names the SAME repository — that is what makes them siblings, and
    ## what Phase 2's cross-index lookup will use to find them.
    assert first.repo_path == vendor.repo_path == plain.repo_path

Activity · 9 steps
lines 82–82repo = tmp_path / "myproj"lines 83–83repo.mkdir()lines 84–84state = tmp_path / "state"lines 86–86plain = target_for(repo, state)lines 87–87first = target_for(repo, state, name="first-party")lines 88–88vendor = target_for(repo, state, name="llama-cpp")lines 90–92assert len({plain.slug, first.slug, vendor.slug}) == 3, ( f"slugs collided: {plain.slug}, {first.slug}, {vendor.slug}" )lines 93–93assert len({plain.db_path, first.db_path, vendor.db_path}) == 3, "db paths collided"lines 96–96assert first.repo_path == vendor.repo_path == plain.repo_path

test_a_vendored_target_is_scoped_to_its_tree

None test_named_targets.test_a_vendored_target_is_scoped_to_its_tree(Path tmp_path)

A vendored target is scoped to its own tree.

tests/test_named_targets.py:261

Checks

  • assert got_options["index_scope"] == {"roots": ["extern/dep"]}

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → declared_sub_index_excludes
REQ-DDB-GUARD-012inferredtest_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node
REQ-DDB-INDEX-002measuredtest_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → derive_sub_indexes
REQ-DDB-MCP-001measuredtest_a_vendored_target_is_scoped_to_its_tree → target_for
REQ-DDB-WIKI-054inferredtest_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all
Source · 18 lines
def test_a_vendored_target_is_scoped_to_its_tree(tmp_path: Path) -> None:
    """@brief A named vendored sub-index declares roots covering only itself.
    @version 1
    """
    from clew.mcp_server.server import _sub_index_scope
    from clew.mcp_server.state import target_for

    repo = tmp_path / "vendored"
    (repo / "app").mkdir(parents=True)
    nested = repo / "extern" / "dep"
    nested.mkdir(parents=True)
    (nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")

    vendored = target_for(repo, tmp_path / "state", name="extern-dep")
    _got_exclude, got_options = _sub_index_scope(vendored, repo.resolve(), None, None)
    assert got_options["index_scope"] == {"roots": ["extern/dep"]}

Activity · 10 steps
lines 265–265from clew.mcp_server.server import _sub_index_scopelines 266–266from clew.mcp_server.state import target_forlines 268–268repo = tmp_path / "vendored"lines 269–269(repo / "app").mkdir(parents=True)lines 270–270nested = repo / "extern" / "dep"lines 271–271nested.mkdir(parents=True)lines 272–272(nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")lines 274–274vendored = target_for(repo, tmp_path / "state", name="extern-dep")lines 275–275_got_exclude, got_options = _sub_index_scope(vendored, repo.resolve(), None, None)lines 276–276assert got_options["index_scope"] == {"roots": ["extern/dep"]}

test_a_whole_repo_target_keeps_the_callers_scope

None test_named_targets.test_a_whole_repo_target_keeps_the_callers_scope(Path tmp_path)

A whole-repo target's build scope is left exactly as the caller stated it.

tests/test_named_targets.py:203

Checks

  • assert _sub_index_scope(whole, repo.resolve(), None, None) == (None, None)
  • assert _sub_index_scope(whole, repo.resolve(), ["build"], {"kconfig": {}}) == (["build"], {"kconfig": {}}, )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001inferredtest_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → declared_sub_index_excludes
REQ-DDB-GUARD-012inferredtest_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node
REQ-DDB-INDEX-002inferredtest_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → derive_sub_indexes
REQ-DDB-MCP-001measuredtest_a_whole_repo_target_keeps_the_callers_scope → target_for
REQ-DDB-WIKI-054inferredtest_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all
Source · 29 lines
def test_a_whole_repo_target_keeps_the_callers_scope(tmp_path: Path) -> None:
    """THE #511 GUARD. A repository that COULD be split must keep building whole until someone
    builds its named targets. Narrowing it here would shrink an index nobody asked to shrink and
    report success — a smaller index that reads as healthy, which is the silent-narrowing failure
    that task records.

    `exclude=None` must survive as None, not become []: None means "inherit what an earlier
    session stated", and flattening it would silently withdraw the operator's own decision.

    @brief An unnamed target's scope passes through untouched.
    @version 1
    """
    from clew.mcp_server.server import _sub_index_scope
    from clew.mcp_server.state import target_for

    repo = tmp_path / "vendored"
    (repo / "app").mkdir(parents=True)
    nested = repo / "extern" / "dep"
    nested.mkdir(parents=True)
    (nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")

    whole = target_for(repo, tmp_path / "state")
    assert _sub_index_scope(whole, repo.resolve(), None, None) == (None, None)
    assert _sub_index_scope(whole, repo.resolve(), ["build"], {"kconfig": {}}) == (
        ["build"],
        {"kconfig": {}},
    )

Activity · 10 steps
lines 215–215from clew.mcp_server.server import _sub_index_scopelines 216–216from clew.mcp_server.state import target_forlines 218–218repo = tmp_path / "vendored"lines 219–219(repo / "app").mkdir(parents=True)lines 220–220nested = repo / "extern" / "dep"lines 221–221nested.mkdir(parents=True)lines 222–222(nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")lines 224–224whole = target_for(repo, tmp_path / "state")lines 225–225assert _sub_index_scope(whole, repo.resolve(), None, None) == (None, None)lines 226–229assert _sub_index_scope(whole, repo.resolve(), ["build"], {"kconfig": {}}) == ( ["build"], {"kconfig": {}}, )

test_an_unnamed_target_is_unchanged

None test_named_targets.test_an_unnamed_target_is_unchanged(Path tmp_path)

An unnamed target keeps exactly the slug and path it has always had.

tests/test_named_targets.py:54

Show on the map →

Called by none

Calls _legacy_slug, target_for

Checks

  • assert got.slug == _legacy_slug(repo.resolve()), ("the unnamed slug rule changed; every index already built is now unreachable" )
  • assert got.db_path.endswith(f"targets/{got.slug}/clew.db")

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measuredtest_an_unnamed_target_is_unchanged → target_for
Source · 17 lines
def test_an_unnamed_target_is_unchanged(tmp_path: Path) -> None:
    """THE COMPATIBILITY GATE. Every index already on disk lives at a path derived from this
    rule, and nothing records where it came from — so if this moves, those indexes are not
    migrated, they are lost, and the failure reads as "never built" on a repo that has one.

    @brief The historical slug derivation is preserved.
    @version 1
    """
    repo = tmp_path / "myproj"
    repo.mkdir()
    got = target_for(repo, tmp_path / "state")
    assert got.slug == _legacy_slug(repo.resolve()), (
        "the unnamed slug rule changed; every index already built is now unreachable"
    )
    assert got.db_path.endswith(f"targets/{got.slug}/clew.db")

Activity · 5 steps
lines 62–62repo = tmp_path / "myproj"lines 63–63repo.mkdir()lines 64–64got = target_for(repo, tmp_path / "state")lines 65–67assert got.slug == _legacy_slug(repo.resolve()), ( "the unnamed slug rule changed; every index already built is now unreachable" )lines 68–68assert got.db_path.endswith(f"targets/{got.slug}/clew.db")

test_named_allocation_is_deterministic

None test_named_targets.test_named_allocation_is_deterministic(Path tmp_path)

The same name yields the same target, every time.

tests/test_named_targets.py:101

Show on the map →

Called by none

Calls target_for

Checks

  • assert target_for(repo, state, name="vendor") == target_for(repo, state, name="vendor")

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measuredtest_named_allocation_is_deterministic → target_for
Source · 14 lines
def test_named_allocation_is_deterministic(tmp_path: Path) -> None:
    """A build and a later query must land on the same database. Nothing persists the mapping
    from name to path, so determinism IS the mapping — exactly as it already is for the unnamed
    form.

    @brief Allocation is a pure function of (path, name).
    @version 1
    """
    repo = tmp_path / "myproj"
    repo.mkdir()
    state = tmp_path / "state"
    assert target_for(repo, state, name="vendor") == target_for(repo, state, name="vendor")

Activity · 4 steps
lines 109–109repo = tmp_path / "myproj"lines 110–110repo.mkdir()lines 111–111state = tmp_path / "state"lines 112–112assert target_for(repo, state, name="vendor") == target_for(repo, state, name="vendor")

test_the_registry_holds_sub_indexes_of_one_root

None test_named_targets.test_the_registry_holds_sub_indexes_of_one_root(Path tmp_path)

A registry holds several sub-indexes of one root, each with its own database.

tests/test_named_targets.py:145

Show on the map →

Called by none

Calls none

Checks

  • assert len(loaded) == 3, f"expected three records, got {sorted(loaded)}"
  • assert back.repo_path == str(repo.resolve()), (f"{allocated.slug} reloaded with repo_path {back.repo_path!r} — a composite key " "leaked into a field that is used as a filesystem path" )
  • assert back.name == allocated.name
  • assert Path(back.db_path).parent.is_dir(), "the db directory was not created"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measured
Source · 31 lines
def test_the_registry_holds_sub_indexes_of_one_root(tmp_path: Path) -> None:
    """THE FIELD THAT MAKES THE COMPOSITE KEY SAFE is `repo_path` inside the record. `targets()`
    used to reconstruct a Target's repo_path FROM THE KEY, which is correct while the key is a
    bare path and wrong the moment it carries a name — the result would be a path that does not
    exist, handed to `Path()` and stamped into replies as the repository that answered.

    @brief Sub-indexes register and reload with the true repo path.
    @version 1
    """
    from clew.mcp_server.state import TargetRegistry

    repo = tmp_path / "myproj"
    repo.mkdir()
    reg = TargetRegistry(home=tmp_path / "state")

    whole = reg.register(repo)
    first = reg.register(repo, name="first-party")
    vendor = reg.register(repo, name="llama-cpp")

    loaded = {t.slug: t for t in reg.targets()}
    assert len(loaded) == 3, f"expected three records, got {sorted(loaded)}"
    for allocated in (whole, first, vendor):
        back = loaded[allocated.slug]
        assert back.repo_path == str(repo.resolve()), (
            f"{allocated.slug} reloaded with repo_path {back.repo_path!r} — a composite key "
            "leaked into a field that is used as a filesystem path"
        )
        assert back.name == allocated.name
        assert Path(back.db_path).parent.is_dir(), "the db directory was not created"

Activity · 14 steps
lines 154–154from clew.mcp_server.state import TargetRegistrylines 156–156repo = tmp_path / "myproj"lines 157–157repo.mkdir()lines 158–158reg = TargetRegistry(home=tmp_path / "state")lines 160–160whole = reg.register(repo)lines 161–161first = reg.register(repo, name="first-party")lines 162–162vendor = reg.register(repo, name="llama-cpp")lines 164–164loaded = {t.slug: t for t in reg.targets()}lines 165–165assert len(loaded) == 3, f"expected three records, got {sorted(loaded)}"lines 166–166◇ for allocated in (whole, first, vendor)lines 167–167back = loaded[allocated.slug]lines 168–171assert back.repo_path == str(repo.resolve()), ( f"{allocated.slug} reloaded with repo_path {back.repo_path!r} — a composite key " "leaked into a field that is used as a filesystem path" )lines 172–172assert back.name == allocated.namelines 173–173assert Path(back.db_path).parent.is_dir(), "the db directory was not created"