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
Checks
assert TargetRegistry().targets() == []
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured | test_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
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
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
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
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
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-006 | measured | test_a_non_positive_cull_age_leaves_every_current_index_alone → write_build_signature |
| REQ-DDB-CONFIG-007 | measured | test_a_non_positive_cull_age_leaves_every_current_index_alone → write_build_signature |
| REQ-DDB-INDEX-003 | measured | |
| REQ-DDB-MCP-001 | measured | test_a_non_positive_cull_age_leaves_every_current_index_alone → target_for |
| REQ-DDB-MCP-004 | measured | |
| REQ-DDB-PIPE-005 | measured | test_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
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
Checks
assert TargetRegistry(home).drop(str(tmp_path / "repo")) is Trueassert (victim / "keep.txt").exists(), ("a symlinked target directory was followed out of the state root" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured |
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
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
Checks
assert TargetRegistry(home).drop(str(tmp_path / "repo")) is Trueassert 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
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured |
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
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)], culledassert [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
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-006 | measured | test_cull_drops_a_never_built_row_without_touching_a_current_index → write_build_signature |
| REQ-DDB-CONFIG-007 | measured | test_cull_drops_a_never_built_row_without_touching_a_current_index → write_build_signature |
| REQ-DDB-INDEX-003 | measured | |
| REQ-DDB-MCP-001 | measured | |
| REQ-DDB-MCP-004 | measured | |
| REQ-DDB-PIPE-005 | measured | test_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
Checks
assert [t.slug for t in targets] == [target_for(repo, home).slug], targetsassert targets[0].repo_path == str(repo), "the surviving row must carry the repository path"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured | test_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
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
Checks
assert str(repo) not in stored, f"the legacy row survived: {sorted(stored)}"assert target_for(repo, home).slug in storedassert target_for(other, home).slug in stored, "another repository's row must be untouched"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured | test_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"