test_registry_hygiene.py

tests/test_registry_hygiene.py

What the registry accumulates, found by reading a real one.

test_a_build_written_somewhere_else_registers_nothing

None test_registry_hygiene.test_a_build_written_somewhere_else_registers_nothing(Path tmp_path)

A build outside the served location leaves the registry alone.

tests/test_registry_hygiene.py:163

Show on the map →

Called by none

Calls _register_built_target

Checks

  • assert TargetRegistry().targets() == []

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measuredtest_a_build_written_somewhere_else_registers_nothing → _register_built_target → target_for
Source · 20 lines
def test_a_build_written_somewhere_else_registers_nothing(tmp_path: Path) -> None:
    """THE COUNTER-CASE. `--output /somewhere/mine.db` is a database the server does not read,
    so recording it as a target would list a repository whose index no query can reach — and the
    acceptance harness builds exactly that way.

    @brief A build outside the served location leaves the registry alone.
    @version 1
    """
    from clew.cli import _register_built_target

    repo = tmp_path / "repo"
    repo.mkdir()
    elsewhere = tmp_path / "mine.db"
    elsewhere.write_bytes(b"")

    _register_built_target(str(repo), elsewhere)

    assert TargetRegistry().targets() == []

Activity · 7 steps
lines 171–171from clew.cli import _register_built_targetlines 173–173repo = tmp_path / "repo"lines 174–174repo.mkdir()lines 175–175elsewhere = tmp_path / "mine.db"lines 176–176elsewhere.write_bytes(b"")lines 178–178_register_built_target(str(repo), elsewhere)lines 180–180assert TargetRegistry().targets() == []

test_a_cli_build_registers_the_target_it_wrote

None test_registry_hygiene.test_a_cli_build_registers_the_target_it_wrote(Path tmp_path)

A CLI build into the served location registers its repository.

tests/test_registry_hygiene.py:136

Show on the map →

Called by none

Calls target_for

Checks

  • assert Path(derived.db_path).is_file(), "premise: the build landed at the served path"
  • assert [t.repo_path for t in TargetRegistry(home).targets()] == [str(repo.resolve())]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CLI-001measured
REQ-DDB-CONFIG-001measured
REQ-DDB-CONFIG-004measured
REQ-DDB-CONFIG-005measured
REQ-DDB-CONFIG-006measured
REQ-DDB-CONFIG-007measured
REQ-DDB-CONFIG-009measured
REQ-DDB-INDEX-001measured
REQ-DDB-INDEX-002measured
REQ-DDB-INDEX-003measured
REQ-DDB-INDEX-004measured
REQ-DDB-INDEX-005measured
REQ-DDB-INDEX-006measured
REQ-DDB-INDEX-007measured
REQ-DDB-INDEX-008measured
REQ-DDB-INDEX-009measured
REQ-DDB-INDEX-010measured
REQ-DDB-INDEX-011measured
REQ-DDB-MCP-001measuredtest_a_cli_build_registers_the_target_it_wrote → target_for
REQ-DDB-MCP-004measured
REQ-DDB-PIPE-001measured
REQ-DDB-PIPE-003measured
REQ-DDB-PIPE-004measured
REQ-DDB-PIPE-005measured
REQ-DDB-PIPE-006measured
REQ-DDB-PIPE-007measured
REQ-DDB-PIPE-008measured
REQ-DDB-PIPE-009measured
REQ-DDB-PIPE-010measured
REQ-DDB-PIPE-011measured
REQ-DDB-PIPE-012measured
REQ-DDB-PIPE-013measured
REQ-DDB-PIPE-014measured
REQ-DDB-PIPE-015measured
REQ-DDB-QUERY-003measured
REQ-DDB-QUERY-011measured
REQ-DDB-SCHEMA-001measured
REQ-DDB-SCHEMA-002measured
REQ-DDB-SCHEMA-003measured
REQ-DDB-SCHEMA-004measured
REQ-DDB-SCHEMA-005measured
REQ-DDB-SCHEMA-006measured
REQ-DDB-SCHEMA-007measured
REQ-DDB-SCHEMA-009measured
REQ-DDB-SCHEMA-011measured
REQ-DDB-SCHEMA-012measured
REQ-DDB-SCHEMA-013measured
REQ-DDB-SCHEMA-014measured
REQ-DDB-SCHEMA-015measured
Source · 27 lines
def test_a_cli_build_registers_the_target_it_wrote(tmp_path: Path) -> None:
    """THE INVISIBLE INDEX. `clew --repo-root X` with no `--output` writes exactly where the
    server reads — `test_omitted_output_resolves_to_the_servers_own_path` pins that — and then
    registered nothing, so `index(action='targets')` did not list it and `cull` could not reach
    it. Two of this machine's six built indexes were in that state.

    REGISTERED AFTER THE BUILD, not at argument resolution: registration is the claim that a
    database is there, and a failed build must not make it.

    @brief A CLI build into the served location registers its repository.
    @version 1
    """
    from clew.cli import build_index

    repo = tmp_path / "repo"
    repo.mkdir()
    (repo / "unit.c").write_text("/** @brief unit. */\nint unit(void) { return 0; }\n")
    subprocess.run(["git", "init", "-q", "."], cwd=repo, check=True)
    home = TargetRegistry().home
    derived = target_for(repo, home)

    build_index(output=derived.db_path, repo_root=repo)

    assert Path(derived.db_path).is_file(), "premise: the build landed at the served path"
    assert [t.repo_path for t in TargetRegistry(home).targets()] == [str(repo.resolve())]

Activity · 10 steps
lines 148–148from clew.cli import build_indexlines 150–150repo = tmp_path / "repo"lines 151–151repo.mkdir()lines 152–152(repo / "unit.c").write_text("/** @brief unit. */\nint unit(void) { return 0; }\n")lines 153–153subprocess.run(["git", "init", "-q", "."], cwd=repo, check=True)lines 154–154home = TargetRegistry().homelines 155–155derived = target_for(repo, home)lines 157–157build_index(output=derived.db_path, repo_root=repo)lines 159–159assert Path(derived.db_path).is_file(), "premise: the build landed at the served path"lines 160–160assert [t.repo_path for t in TargetRegistry(home).targets()] == [str(repo.resolve())]

test_a_non_positive_cull_age_leaves_every_current_index_alone

None test_registry_hygiene.test_a_non_positive_cull_age_leaves_every_current_index_alone(Path tmp_path)

A non-positive cull age threshold does not destroy every index on the machine.

tests/test_registry_hygiene.py:273

Checks

  • assert culled == [], f"a zero age threshold culled current indexes: {culled}"
  • assert db.exists(), f"{db} was deleted by a zero age threshold"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-006measuredtest_a_non_positive_cull_age_leaves_every_current_index_alone → write_build_signature
REQ-DDB-CONFIG-007measuredtest_a_non_positive_cull_age_leaves_every_current_index_alone → write_build_signature
REQ-DDB-INDEX-003measured
REQ-DDB-MCP-001measuredtest_a_non_positive_cull_age_leaves_every_current_index_alone → target_for
REQ-DDB-MCP-004measured
REQ-DDB-PIPE-005measuredtest_a_non_positive_cull_age_leaves_every_current_index_alone → write_build_signature
Source · 36 lines
def test_a_non_positive_cull_age_leaves_every_current_index_alone(tmp_path: Path) -> None:
    """M4, 1.1.0 SECURITY AUDIT. `age > max_age_days` with `max_age_days=0` is true for every
    database that exists, so one call — from an agent reading "age threshold in days" and
    passing 0 to mean "no age limit", which is what 0 means in most such knobs — deleted every
    index on the machine, including repositories this session had never heard of. The blast
    radius is the whole state root because ONE server answers about every target.

    `None` is the spelling that disables the rule, and it already works. A non-positive number
    now means the same thing rather than the opposite, and the MCP boundary refuses it outright
    so a caller gets a validation error instead of a silent sweep.

    @brief Two fresh indexes survive `max_age_days=0`.
    @return None.
    @version 1
    """
    home = tmp_path / "state"
    kept = []
    for name in ("alpha", "beta"):
        repo = tmp_path / name
        repo.mkdir()
        target = target_for(repo, home)
        Path(target.db_path).parent.mkdir(parents=True, exist_ok=True)
        Path(target.db_path).write_text("", encoding="utf-8")
        write_build_signature(Path(target.db_path))
        TargetRegistry(home).register(repo)
        ## AGED BY AN HOUR, because `db_status` rounds `age_days` and a database written this
        ## instant rounds to 0.0 — which is not `> 0` and would make this test pass against the
        ## defect. An hour-old index is the ordinary case the sweep must not touch.
        os.utime(target.db_path, (time.time() - 3600, time.time() - 3600))
        kept.append(Path(target.db_path))

    culled = cull(TargetRegistry(home), max_age_days=0, include_stale=False)

    assert culled == [], f"a zero age threshold culled current indexes: {culled}"
    for db in kept:
        assert db.exists(), f"{db} was deleted by a zero age threshold"

1 more lines, not shown

Activity · 16 steps
lines 288–288home = tmp_path / "state"lines 289–289kept = []lines 290–290◇ for name in ("alpha", "beta")lines 291–291repo = tmp_path / namelines 292–292repo.mkdir()lines 293–293target = target_for(repo, home)lines 294–294Path(target.db_path).parent.mkdir(parents=True, exist_ok=True)lines 295–295Path(target.db_path).write_text("", encoding="utf-8")lines 296–296write_build_signature(Path(target.db_path))lines 297–297TargetRegistry(home).register(repo)lines 301–301os.utime(target.db_path, (time.time() - 3600, time.time() - 3600))lines 302–302kept.append(Path(target.db_path))lines 304–304culled = cull(TargetRegistry(home), max_age_days=0, include_stale=False)lines 306–306assert culled == [], f"a zero age threshold culled current indexes: {culled}"lines 307–307◇ for db in keptlines 308–308assert db.exists(), f"{db} was deleted by a zero age threshold"

test_a_symlinked_db_directory_is_not_followed_out_of_the_state_root

None test_registry_hygiene.test_a_symlinked_db_directory_is_not_followed_out_of_the_state_root(Path tmp_path)

A symlinked database directory is not followed out of the state root.

tests/test_registry_hygiene.py:231

Show on the map →

Called by none

Calls none

Checks

  • assert TargetRegistry(home).drop(str(tmp_path / "repo")) is True
  • assert (victim / "keep.txt").exists(), ("a symlinked target directory was followed out of the state root" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measured
Source · 38 lines
def test_a_symlinked_db_directory_is_not_followed_out_of_the_state_root(tmp_path: Path) -> None:
    """The other half of M3, and the one a lexical check cannot see at all: every component of
    `<home>/targets/<slug>` is textually inside `home` while `<slug>` is a symlink pointing
    anywhere. `shutil.rmtree` refuses to recurse through a symlinked directory itself, so what
    is at stake is the containment ORDER — resolving first is what makes the refusal explicit
    rather than incidental to rmtree's own behaviour.

    @brief A symlinked target directory leaves its destination intact.
    @return None.
    @version 1
    """
    home = tmp_path / "state"
    (home / "targets").mkdir(parents=True)
    victim = tmp_path / "victim"
    victim.mkdir()
    (victim / "keep.txt").write_text("precious", encoding="utf-8")
    (home / "targets" / "linked").symlink_to(victim)

    (home / REGISTRY_NAME).write_text(
        json.dumps(
            {
                "linked": {
                    "slug": "linked",
                    "db_path": str(home / "targets" / "linked" / "clew.db"),
                    "repo_path": str(tmp_path / "repo"),
                }
            }
        ),
        encoding="utf-8",
    )

    assert TargetRegistry(home).drop(str(tmp_path / "repo")) is True

    assert (victim / "keep.txt").exists(), (
        "a symlinked target directory was followed out of the state root"
    )

Activity · 9 steps
lines 242–242home = tmp_path / "state"lines 243–243(home / "targets").mkdir(parents=True)lines 244–244victim = tmp_path / "victim"lines 245–245victim.mkdir()lines 246–246(victim / "keep.txt").write_text("precious", encoding="utf-8")lines 247–247(home / "targets" / "linked").symlink_to(victim)lines 249–260(home / REGISTRY_NAME).write_text( json.dumps( { "linked": { "slug": "linked", "db_path": str(home / "targets" / "linked" / "clew.db"), "repo_path": str(tmp_path / "repo"), } } ), encoding="utf-8", )lines 262–262assert TargetRegistry(home).drop(str(tmp_path / "repo")) is Truelines 264–266assert (victim / "keep.txt").exists(), ( "a symlinked target directory was followed out of the state root" )

test_a_traversing_db_path_is_not_followed_into_an_rmtree

None test_registry_hygiene.test_a_traversing_db_path_is_not_followed_into_an_rmtree(Path tmp_path)

A registry row naming a path outside the state root never gets rmtree'd.

tests/test_registry_hygiene.py:187

Show on the map →

Called by none

Calls none

Checks

  • assert TargetRegistry(home).drop(str(tmp_path / "repo")) is True
  • assert victim.is_dir() and (victim / "keep.txt").exists(), ("a registry row walked `drop` out of the state root and deleted a directory outside it" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measured
Source · 40 lines
def test_a_traversing_db_path_is_not_followed_into_an_rmtree(tmp_path: Path) -> None:
    """M3, 1.1.0 SECURITY AUDIT — in code this session shipped, so the record is straight.
    `_discard` tested containment with `is_relative_to` on the UNRESOLVED path, which is a
    LEXICAL check: `<home>/targets/../../victim` is textually under `home` and resolves
    outside it. The registry is a JSON file on disk, so a row is editable by anything running
    as the user, and `drop`/`cull` then delete a directory of that row's choosing.

    Resolve-then-contain, and `ignore_errors=True` goes with it: it was swallowing exactly the
    refusals that would have made this visible.

    @brief A `..` traversal in db_path leaves the outside directory intact.
    @return None.
    @version 1
    """
    home = tmp_path / "state"
    (home / "targets").mkdir(parents=True)
    victim = tmp_path / "victim"
    victim.mkdir()
    (victim / "keep.txt").write_text("precious", encoding="utf-8")

    (home / REGISTRY_NAME).write_text(
        json.dumps(
            {
                "traversing": {
                    "slug": "traversing",
                    "db_path": str(home / "targets" / ".." / ".." / "victim" / "clew.db"),
                    "repo_path": str(tmp_path / "repo"),
                }
            }
        ),
        encoding="utf-8",
    )

    assert TargetRegistry(home).drop(str(tmp_path / "repo")) is True

    assert victim.is_dir() and (victim / "keep.txt").exists(), (
        "a registry row walked `drop` out of the state root and deleted a directory outside it"
    )

Activity · 8 steps
lines 201–201home = tmp_path / "state"lines 202–202(home / "targets").mkdir(parents=True)lines 203–203victim = tmp_path / "victim"lines 204–204victim.mkdir()lines 205–205(victim / "keep.txt").write_text("precious", encoding="utf-8")lines 207–218(home / REGISTRY_NAME).write_text( json.dumps( { "traversing": { "slug": "traversing", "db_path": str(home / "targets" / ".." / ".." / "victim" / "clew.db"), "repo_path": str(tmp_path / "repo"), } } ), encoding="utf-8", )lines 220–220assert TargetRegistry(home).drop(str(tmp_path / "repo")) is Truelines 222–224assert victim.is_dir() and (victim / "keep.txt").exists(), ( "a registry row walked `drop` out of the state root and deleted a directory outside it" )

test_cull_drops_a_never_built_row_without_touching_a_current_index

None test_registry_hygiene.test_cull_drops_a_never_built_row_without_touching_a_current_index(Path tmp_path)

cull(max_age_days=None, include_stale=False) removes only rows with no database.

tests/test_registry_hygiene.py:110

Checks

  • assert [row["repo_path"] for row in culled] == [str(ghost)], culled
  • assert [t.repo_path for t in registry.targets()] == [str(built)]
  • assert Path(target.db_path).is_file(), "the built index must survive"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-006measuredtest_cull_drops_a_never_built_row_without_touching_a_current_index → write_build_signature
REQ-DDB-CONFIG-007measuredtest_cull_drops_a_never_built_row_without_touching_a_current_index → write_build_signature
REQ-DDB-INDEX-003measured
REQ-DDB-MCP-001measured
REQ-DDB-MCP-004measured
REQ-DDB-PIPE-005measuredtest_cull_drops_a_never_built_row_without_touching_a_current_index → write_build_signature
Source · 26 lines
def test_cull_drops_a_never_built_row_without_touching_a_current_index(tmp_path: Path) -> None:
    """THE CONTROL, and the answer to "how do I get rid of the `$HOME` row". `cull` already
    drops a target whose database does not exist — but its DEFAULTS also delete every index
    built by an older pipeline, which on this machine was three healthy ones. So the remedy is
    the narrow call, and this pins both halves of it.

    @brief cull(max_age_days=None, include_stale=False) removes only rows with no database.
    @version 1
    """
    home = tmp_path / "state"
    ghost, built = tmp_path / "home_dir", tmp_path / "built"
    ghost.mkdir()
    built.mkdir()
    registry = TargetRegistry(home)
    registry.register(ghost)
    target = registry.register(built)
    write_build_signature(Path(target.db_path))

    culled = cull(registry, max_age_days=None, include_stale=False)

    assert [row["repo_path"] for row in culled] == [str(ghost)], culled
    assert [t.repo_path for t in registry.targets()] == [str(built)]
    assert Path(target.db_path).is_file(), "the built index must survive"


@pytest.mark.skipif(shutil.which("doxygen") is None, reason="asserted against a real build")

test_one_repository_registered_twice_is_listed_once

None test_registry_hygiene.test_one_repository_registered_twice_is_listed_once(Path tmp_path)

A legacy row and its slug row resolve to a single target.

tests/test_registry_hygiene.py:71

Show on the map →

Called by none

Calls _legacy_and_slug_rows, target_for

Checks

  • assert [t.slug for t in targets] == [target_for(repo, home).slug], targets
  • assert targets[0].repo_path == str(repo), "the surviving row must carry the repository path"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measuredtest_one_repository_registered_twice_is_listed_once → target_for
Source · 17 lines
def test_one_repository_registered_twice_is_listed_once(tmp_path: Path) -> None:
    """READ-SIDE, so an existing registry is fixed without waiting for a rebuild. Whoever is
    holding a duplicated registry today gets one row per repository on the next call.

    @brief A legacy row and its slug row resolve to a single target.
    @version 1
    """
    home, repo = tmp_path / "state", tmp_path / "repo"
    repo.mkdir()
    _legacy_and_slug_rows(home, repo)

    targets = TargetRegistry(home).targets()

    assert [t.slug for t in targets] == [target_for(repo, home).slug], targets
    assert targets[0].repo_path == str(repo), "the surviving row must carry the repository path"

Activity · 6 steps
lines 78–78home, repo = tmp_path / "state", tmp_path / "repo"lines 79–79repo.mkdir()lines 80–80_legacy_and_slug_rows(home, repo)lines 82–82targets = TargetRegistry(home).targets()lines 84–84assert [t.slug for t in targets] == [target_for(repo, home).slug], targetslines 85–85assert targets[0].repo_path == str(repo), "the surviving row must carry the repository path"

test_registering_removes_the_legacy_row_it_supersedes

None test_registry_hygiene.test_registering_removes_the_legacy_row_it_supersedes(Path tmp_path)

A whole-repo registration drops its own legacy key and nothing else.

tests/test_registry_hygiene.py:88

Show on the map →

Called by none

Calls _legacy_and_slug_rows, target_for

Checks

  • assert str(repo) not in stored, f"the legacy row survived: {sorted(stored)}"
  • assert target_for(repo, home).slug in stored
  • assert target_for(other, home).slug in stored, "another repository's row must be untouched"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-MCP-001measuredtest_registering_removes_the_legacy_row_it_supersedes → target_for
Source · 22 lines
def test_registering_removes_the_legacy_row_it_supersedes(tmp_path: Path) -> None:
    """WRITE-SIDE, so the duplicate does not come back. Only the row for the SAME slug is
    dropped — a registry holding several repositories keeps them all.

    @brief A whole-repo registration drops its own legacy key and nothing else.
    @version 1
    """
    home, repo, other = tmp_path / "state", tmp_path / "repo", tmp_path / "other"
    repo.mkdir()
    other.mkdir()
    _legacy_and_slug_rows(home, repo)
    registry = TargetRegistry(home)
    registry.register(other)

    registry.register(repo)

    stored = json.loads((home / REGISTRY_NAME).read_text())
    assert str(repo) not in stored, f"the legacy row survived: {sorted(stored)}"
    assert target_for(repo, home).slug in stored
    assert target_for(other, home).slug in stored, "another repository's row must be untouched"

Activity · 11 steps
lines 95–95home, repo, other = tmp_path / "state", tmp_path / "repo", tmp_path / "other"lines 96–96repo.mkdir()lines 97–97other.mkdir()lines 98–98_legacy_and_slug_rows(home, repo)lines 99–99registry = TargetRegistry(home)lines 100–100registry.register(other)lines 102–102registry.register(repo)lines 104–104stored = json.loads((home / REGISTRY_NAME).read_text())lines 105–105assert str(repo) not in stored, f"the legacy row survived: {sorted(stored)}"lines 106–106assert target_for(repo, home).slug in storedlines 107–107assert target_for(other, home).slug in stored, "another repository's row must be untouched"