test_shared_parse.py¶
tests/test_shared_parse.py
gh#358 — one parse per file, ten stages, ten independent cache keys.
test_every_harvester_subclass_is_reachable_from_the_plan
None test_shared_parse.test_every_harvester_subclass_is_reachable_from_the_plan()
Every harvester the codebase defines is one the shared pass warms.
tests/test_shared_parse.py:360
Checks
assert not missing, (f"harvester stage(s) {sorted(missing)} run outside the shared parse, so each one " "re-parses every indexed file — the defect that cost 29.5 s of a 130 s cold build " "when macro_refs did it. Add the harvester to HarvestPlan and to active()." )assert {"dispatch", "subscribe"} <= fields
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-006 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → shared_key_harvester → resolve_shared_key_patterns |
| REQ-DDB-INDEX-002 | inferred | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → lock_harvester → manifest_key → hash_file |
| REQ-DDB-INDEX-010 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → includes_harvester |
| REQ-DDB-INDEX-011 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → property_key_harvester |
| REQ-DDB-PIPE-003 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan |
| REQ-DDB-PIPE-007 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → gate_harvester |
| REQ-DDB-PIPE-012 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → js_symbol_harvester |
| REQ-DDB-PIPE-013 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → docstring_harvester |
| REQ-DDB-PIPE-015 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → defaults_harvester |
| REQ-DDB-SCHEMA-001 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → spawn_harvester |
| REQ-DDB-SCHEMA-004 | inferred | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → dispatch_harvester |
| REQ-DDB-SCHEMA-005 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → shared_key_harvester |
| REQ-DDB-SCHEMA-011 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → lock_harvester |
| REQ-DDB-SCHEMA-012 | measured | test_every_harvester_subclass_is_reachable_from_the_plan → build_harvest_plan → lock_harvester → load_lock_patterns → declaration_origin |
| REQ-DDB-WIKI-034 | measured |
Source · 48 lines
def test_every_harvester_subclass_is_reachable_from_the_plan() -> None:
"""THE GUARD THAT WOULD HAVE CAUGHT `macro_refs`, AND THE REASON IT IS STRUCTURAL.
`test_stages_after_shared_pass_do_not_parse` iterates a hand-built list, so a stage absent
from that list is invisible to it — it passed for as long as `macro_refs` drove its own
full-tree parse, costing 29.5 s of a 130 s cold build. A test that enumerates the thing it
is checking cannot detect an omission FROM that thing.
So this enumerates `Harvester.__subclasses__()` after importing the whole package: the
codebase's own answer to "what harvesters exist", which no plan edit can shrink. A new
harvester that forgets the plan fails here on the day it is written, with a message naming
the cost of not fixing it.
@brief No harvester runs outside the shared parse.
@version 1
"""
import importlib
import pkgutil
import clew
from clew.harvest_plan import HarvestPlan, build_harvest_plan
## Subclasses only register once their module is imported, and the pipeline imports them
## lazily — so without this walk the set is whatever earlier tests happened to touch, and
## the assertion would silently weaken as import order changed.
for mod in pkgutil.iter_modules(clew.__path__):
importlib.import_module(f"clew.{mod.name}")
## SCOPED TO THE PACKAGE. `__subclasses__()` sees every subclass alive in the interpreter,
## including this module's own `_CountingHarvester` test double — which carries the base
## class's empty `stage` and made the first run of this guard fail on `['']`. Filtering by
## `__module__` asks the question that was meant: what does *clew* define?
defined = {
cls.stage for cls in Harvester.__subclasses__() if cls.__module__.startswith("clew.")
}
warmed = {h.stage for h in build_harvest_plan().active()}
fields = set(HarvestPlan.__dataclass_fields__)
missing = defined - warmed - _CONDITIONAL_STAGES
assert not missing, (
f"harvester stage(s) {sorted(missing)} run outside the shared parse, so each one "
"re-parses every indexed file — the defect that cost 29.5 s of a 130 s cold build "
"when macro_refs did it. Add the harvester to HarvestPlan and to active()."
)
## The conditional half: absent from a plan built with no manifests, but they must still be
## the PLAN's to construct. Named fields rather than instances, because that is the property
## that survives a build which declares nothing.
assert {"dispatch", "subscribe"} <= fields1 more lines, not shown
Activity · 12 steps
test_one_stage_version_bump_recomputes_only_that_stage
None test_shared_parse.test_one_stage_version_bump_recomputes_only_that_stage(tuple[Path, Path, Path] indexed_repo, pytest.MonkeyPatch monkeypatch)
Bumping ONE stage's version recomputes ONE stage.
tests/test_shared_parse.py:211
Checks
assert len(counter.paths) == len(_FILES)assert tally.computed == len(_FILES)assert tally.cached == len(_FILES) * (len(stages) - 1)assert (stages[1].stage, stages[1].stage_version, len(_FILES)) in rowsassert (stages[1].stage, stages[1].stage_version - 1, len(_FILES)) in rows
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | |
| REQ-DDB-PIPE-003 | measured | test_one_stage_version_bump_recomputes_only_that_stage → run_shared_parse |
| REQ-DDB-PIPE-011 | measured | test_one_stage_version_bump_recomputes_only_that_stage → run_shared_parse → _shared_parse_one_file → _ast_parse_one_file → _parseable_file |
| REQ-DDB-PIPE-012 | inferred | test_one_stage_version_bump_recomputes_only_that_stage → run_shared_parse → _shared_parse_one_file → _ast_parse_one_file → _ts_language_for → _grammar_available → has_language |
Source · 36 lines
def test_one_stage_version_bump_recomputes_only_that_stage(
indexed_repo: tuple[Path, Path, Path],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""THE CONTROL THAT FORBIDS A COMBINED CACHE KEY. Sharing the parse is only safe
while the KEYS stay separate: a merged key would either invalidate all four
stages here (all-or-nothing) or serve a stale slice for the bumped one (lossy).
Either shows up as a `computed` count that is not exactly one stage's worth.
@brief One invalidated stage costs one stage's recomputation.
@version 1
"""
repo, db, cache_path = indexed_repo
cache, stages = _fixture_stages(repo, cache_path)
conn = sqlite3.connect(str(db))
run_shared_parse(conn, repo, stages, cache)
stages[1].stage_version += 1
counter = _ParseCounter()
monkeypatch.setattr(harvest, "_ast_parse_one_file", counter)
tally = run_shared_parse(conn, repo, stages, cache)
conn.close()
# Each file is still parsed at most once, and only the bumped stage recomputes.
assert len(counter.paths) == len(_FILES)
assert tally.computed == len(_FILES)
assert tally.cached == len(_FILES) * (len(stages) - 1)
# The superseded rows are left alone: invalidation is by KEY, not by deletion.
rows = cache.conn.execute(
"SELECT stage, stage_version, COUNT(*) FROM extract_cache "
"GROUP BY stage, stage_version ORDER BY stage, stage_version",
).fetchall()
assert (stages[1].stage, stages[1].stage_version, len(_FILES)) in rows
assert (stages[1].stage, stages[1].stage_version - 1, len(_FILES)) in rows
Activity · 15 steps
test_pair_accounting_is_not_double_counted
None test_shared_parse.test_pair_accounting_is_not_double_counted(tuple[Path, Path, Path] indexed_repo)
One (file, stage) payload is counted once, not once per consumer.
tests/test_shared_parse.py:323
Checks
assert (cache.hits, cache.misses) == (0, len(_FILES) * len(stages))assert (warm.hits, warm.misses) == (len(_FILES) * len(stages), 0)
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | |
| REQ-DDB-PIPE-003 | measured | test_pair_accounting_is_not_double_counted → run_harvest |
| REQ-DDB-PIPE-011 | measured | test_pair_accounting_is_not_double_counted → run_harvest → _harvest_one_file → _parseable_file |
| REQ-DDB-PIPE-012 | inferred | test_pair_accounting_is_not_double_counted → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language |
Source · 28 lines
def test_pair_accounting_is_not_double_counted(
indexed_repo: tuple[Path, Path, Path],
) -> None:
"""`misses` is published as the index's `payloads_recomputed` and `status` shows it
to an agent deciding whether to trust an answer. Since the shared pass COMPUTES
what the stages then READ, naive counting would report a fully cold build as
fully missed AND fully hit at the same time.
@brief A cold build reports misses only; a warm one reports hits only.
@version 1
"""
repo, db, cache_path = indexed_repo
cache, stages = _fixture_stages(repo, cache_path)
conn = sqlite3.connect(str(db))
run_shared_parse(conn, repo, stages, cache)
for stage in stages:
run_harvest(conn, repo, stage, cache)
assert (cache.hits, cache.misses) == (0, len(_FILES) * len(stages))
cache.conn.commit()
warm = IndexCache(cache_path, repo)
run_shared_parse(conn, repo, stages, warm)
for stage in stages:
run_harvest(conn, repo, stage, warm)
conn.close()
assert (warm.hits, warm.misses) == (len(_FILES) * len(stages), 0)
Activity · 14 steps
test_rebuild_still_parses_each_file_once
None test_shared_parse.test_rebuild_still_parses_each_file_once(tuple[Path, Path, Path] indexed_repo, pytest.MonkeyPatch monkeypatch)
rebuild recomputes everything, but each file only once.
tests/test_shared_parse.py:285
Checks
assert len(run_harvest(conn, repo, stage, rebuild)) == len(_FILES)assert tally.computed == len(_FILES) * len(fresh)assert tally.cached == 0assert len(counter.paths) == len(_FILES)
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | |
| REQ-DDB-PIPE-003 | measured | test_rebuild_still_parses_each_file_once → run_harvest |
| REQ-DDB-PIPE-011 | measured | test_rebuild_still_parses_each_file_once → run_harvest → _harvest_one_file → _parseable_file |
| REQ-DDB-PIPE-012 | inferred | test_rebuild_still_parses_each_file_once → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language |
Source · 34 lines
def test_rebuild_still_parses_each_file_once(
indexed_repo: tuple[Path, Path, Path],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""`--rebuild` sets `read_enabled=False` so nothing on disk is trusted. Without
the this-run exemption in `IndexCache._readable`, the shared pass would compute
every payload and every stage would then MISS and parse again — `--rebuild`
would be the one mode gh#358 did not fix, and nothing would say so.
@brief A forced rebuild reuses its own recomputation.
@version 1
"""
repo, db, cache_path = indexed_repo
warm, stages = _fixture_stages(repo, cache_path)
conn = sqlite3.connect(str(db))
run_shared_parse(conn, repo, stages, warm)
warm.conn.commit()
rebuild = IndexCache(cache_path, repo, read_enabled=False)
fresh = [_CountingHarvester(f"probe_{n}") for n in range(4)]
counter = _ParseCounter()
monkeypatch.setattr(harvest, "_ast_parse_one_file", counter)
tally = run_shared_parse(conn, repo, fresh, rebuild)
for stage in fresh:
assert len(run_harvest(conn, repo, stage, rebuild)) == len(_FILES)
conn.close()
# Everything was recomputed (nothing on disk was trusted) …
assert tally.computed == len(_FILES) * len(fresh)
assert tally.cached == 0
# … and yet each file was parsed exactly once, not once per stage.
assert len(counter.paths) == len(_FILES)
Activity · 16 steps
test_shared_pass_parses_each_file_once
None test_shared_parse.test_shared_pass_parses_each_file_once(tuple[Path, Path, Path] indexed_repo, pytest.MonkeyPatch monkeypatch)
The shared pass parses each file once, whatever the stage count.
tests/test_shared_parse.py:145
Checks
assert len(counter.paths) == len(_FILES)assert sorted(counter.paths) == sorted(_FILES)assert tally.parsed == len(_FILES)assert tally.computed == len(_FILES) * len(stages)assert tally.cached == 0assert [len(s.harvested) for s in stages] == [len(_FILES)] * len(stages)
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | |
| REQ-DDB-PIPE-003 | measured | test_shared_pass_parses_each_file_once → run_shared_parse |
| REQ-DDB-PIPE-011 | measured | test_shared_pass_parses_each_file_once → run_shared_parse → _shared_parse_one_file → _ast_parse_one_file → _parseable_file |
| REQ-DDB-PIPE-012 | inferred | test_shared_pass_parses_each_file_once → run_shared_parse → _shared_parse_one_file → _ast_parse_one_file → _ts_language_for → _grammar_available → has_language |
Source · 29 lines
def test_shared_pass_parses_each_file_once(
indexed_repo: tuple[Path, Path, Path],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""THE gh#358 ASSERTION. Four stages over three files: three parses, twelve
payloads. Before the shared pass the same work cost twelve parses, and the
ratio is the whole saving — so this counts BOTH, because "3 parses" alone would
also be satisfied by a pass that quietly dropped a stage.
@brief One parse per file, one payload per (file, stage).
@version 1
"""
repo, db, cache_path = indexed_repo
counter = _ParseCounter()
monkeypatch.setattr(harvest, "_ast_parse_one_file", counter)
cache, stages = _fixture_stages(repo, cache_path)
conn = sqlite3.connect(str(db))
tally = run_shared_parse(conn, repo, stages, cache)
conn.close()
assert len(counter.paths) == len(_FILES)
assert sorted(counter.paths) == sorted(_FILES)
assert tally.parsed == len(_FILES)
assert tally.computed == len(_FILES) * len(stages)
assert tally.cached == 0
assert [len(s.harvested) for s in stages] == [len(_FILES)] * len(stages)
Activity · 13 steps
test_stages_after_shared_pass_do_not_parse
None test_shared_parse.test_stages_after_shared_pass_do_not_parse(tuple[Path, Path, Path] indexed_repo, pytest.MonkeyPatch monkeypatch)
After the shared pass, every stage's own harvest parses nothing.
tests/test_shared_parse.py:179
Checks
assert len(results) == len(_FILES)assert counter.paths == []
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | |
| REQ-DDB-PIPE-003 | measured | test_stages_after_shared_pass_do_not_parse → run_harvest |
| REQ-DDB-PIPE-011 | measured | test_stages_after_shared_pass_do_not_parse → run_harvest → _harvest_one_file → _parseable_file |
| REQ-DDB-PIPE-012 | inferred | test_stages_after_shared_pass_do_not_parse → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language |
Source · 27 lines
def test_stages_after_shared_pass_do_not_parse(
indexed_repo: tuple[Path, Path, Path],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The saving only lands if the stages then find their rows. A stage whose key
disagreed with the warmed one would re-parse and still produce correct rows —
silently costing exactly what gh#358 set out to remove — so this asserts the
ZERO, and asserts the payloads still arrive.
@brief Warmed stages serve from cache and parse nothing.
@version 1
"""
repo, db, cache_path = indexed_repo
cache, stages = _fixture_stages(repo, cache_path)
conn = sqlite3.connect(str(db))
run_shared_parse(conn, repo, stages, cache)
counter = _ParseCounter()
monkeypatch.setattr(harvest, "_ast_parse_one_file", counter)
for stage in stages:
results = run_harvest(conn, repo, stage, cache)
assert len(results) == len(_FILES)
conn.close()
assert counter.paths == []
Activity · 11 steps
test_unchanged_tree_parses_nothing
None test_shared_parse.test_unchanged_tree_parses_nothing(tuple[Path, Path, Path] indexed_repo, pytest.MonkeyPatch monkeypatch)
An unchanged tree parses nothing at all on the next build.
tests/test_shared_parse.py:252
Checks
assert counter.paths == []assert tally.parsed == 0assert tally.computed == 0assert tally.cached == len(_FILES) * len(stages)
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | |
| REQ-DDB-PIPE-003 | measured | test_unchanged_tree_parses_nothing → run_shared_parse |
| REQ-DDB-PIPE-011 | measured | test_unchanged_tree_parses_nothing → run_shared_parse → _shared_parse_one_file → _ast_parse_one_file → _parseable_file |
| REQ-DDB-PIPE-012 | inferred | test_unchanged_tree_parses_nothing → run_shared_parse → _shared_parse_one_file → _ast_parse_one_file → _ts_language_for → _grammar_available → has_language |
Source · 28 lines
def test_unchanged_tree_parses_nothing(
indexed_repo: tuple[Path, Path, Path],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The shared pass must not make an incremental refresh worse. It asks
`extract_has` per (file, stage) and parses only when at least one misses, so a
fully-cached tree costs existence checks and no parse — and no payload DECODE
either, which is why `extract_has` exists rather than reusing `extract_get`.
@brief A warm refresh does no parsing.
@version 1
"""
repo, db, cache_path = indexed_repo
cache, stages = _fixture_stages(repo, cache_path)
conn = sqlite3.connect(str(db))
run_shared_parse(conn, repo, stages, cache)
counter = _ParseCounter()
monkeypatch.setattr(harvest, "_ast_parse_one_file", counter)
tally = run_shared_parse(conn, repo, stages, cache)
conn.close()
assert counter.paths == []
assert tally.parsed == 0
assert tally.computed == 0
assert tally.cached == len(_FILES) * len(stages)