test_ast_fold_cache.py

tests/test_ast_fold_cache.py

gh#34 — the AST call-edge stage re-resolves every file on every refresh.

test_a_new_function_invalidates_every_cached_fold

None test_ast_fold_cache.test_a_new_function_invalidates_every_cached_fold(Path tmp_path, monkeypatch)

A changed resolution input re-folds everything, not just the changed file.

tests/test_ast_fold_cache.py:212

Show on the map →

Called by none

Calls _run_stage, _two_file_db

Checks

  • assert second == 2, (f"a changed name->rowid map invalidates every file's fold, not just the edited " f"one — folded {second}" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
REQ-DDB-PIPE-003measuredtest_a_new_function_invalidates_every_cached_fold → _run_stage → import_ast_call_edges → run_harvest
REQ-DDB-PIPE-011measuredtest_a_new_function_invalidates_every_cached_fold → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_a_new_function_invalidates_every_cached_fold → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-WIKI-054inferredtest_a_new_function_invalidates_every_cached_fold → _run_stage → import_ast_call_edges → _fold_all_files → _fold_call_payload → _ast_record_call_edge → _narrow_by_receiver → _one_class → all
Source · 33 lines
def test_a_new_function_invalidates_every_cached_fold(tmp_path: Path, monkeypatch) -> None:
    """THE GUARD THAT MATTERS MOST, and the reason the payload sha alone is not the key. A
    new function joins `name_to_rowids`, so a call site in an UNCHANGED file may now resolve
    to a different rowid — or resolve at all where it did not. Serving that file's old edges
    would be a stale edge presented as a measurement, which is this project's most expensive
    recorded defect class.

    @brief A changed resolution input re-folds everything, not just the changed file.
    @return None.
    @version 1
    """
    repo = tmp_path / "repo"
    db = _two_file_db(repo, {"a.py": _A, "b.py": _B})
    cache = tmp_path / "clew.db.idxcache"
    _run_stage(db, repo, cache, monkeypatch)

    ## A NEW index, with an extra function — the resolution inputs have moved.
    widened = _two_file_db(
        repo,
        {
            "a.py": _A,
            "b.py": _B + '\n\ndef third(z):\n    """@brief Third."""\n    return other(z)\n',
        },
        db_name="widened.db",
    )
    second, _rows = _run_stage(widened, repo, cache, monkeypatch)

    assert second == 2, (
        f"a changed name->rowid map invalidates every file's fold, not just the edited "
        f"one — folded {second}"
    )

Activity · 7 steps
lines 223–223repo = tmp_path / "repo"lines 224–224db = _two_file_db(repo, {"a.py": _A, "b.py": _B})lines 225–225cache = tmp_path / "clew.db.idxcache"lines 226–226_run_stage(db, repo, cache, monkeypatch)lines 229–236widened = _two_file_db( repo, { "a.py": _A, "b.py": _B + '\n\ndef third(z):\n """@brief Third."""\n return other(z)\n', }, db_name="widened.db", )lines 237–237second, _rows = _run_stage(widened, repo, cache, monkeypatch)lines 239–242assert second == 2, ( f"a changed name->rowid map invalidates every file's fold, not just the edited " f"one — folded {second}" )

test_an_unchanged_file_is_not_refolded_on_a_second_run

None test_ast_fold_cache.test_an_unchanged_file_is_not_refolded_on_a_second_run(Path tmp_path, monkeypatch)

A second run over an unchanged tree folds nothing.

tests/test_ast_fold_cache.py:171

Show on the map →

Called by none

Calls _run_stage, _two_file_db

Checks

  • assert first == 2, f"the cold run must fold both files, folded {first}"
  • assert second == 0, f"an unchanged tree needs no folding, folded {second}"
  • assert second_rows == first_rows, "the cached run must write exactly the same edges"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
REQ-DDB-PIPE-003measuredtest_an_unchanged_file_is_not_refolded_on_a_second_run → _run_stage → import_ast_call_edges → run_harvest
REQ-DDB-PIPE-011measuredtest_an_unchanged_file_is_not_refolded_on_a_second_run → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_an_unchanged_file_is_not_refolded_on_a_second_run → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-WIKI-054inferredtest_an_unchanged_file_is_not_refolded_on_a_second_run → _run_stage → import_ast_call_edges → _fold_all_files → _fold_call_payload → _ast_record_call_edge → _narrow_by_receiver → _one_class → all
Source · 20 lines
def test_an_unchanged_file_is_not_refolded_on_a_second_run(tmp_path: Path, monkeypatch) -> None:
    """THE 59%. Nothing about the tree or the index moved between the two runs, so the second
    run has nothing to resolve — and resolved it all again anyway.

    @brief A second run over an unchanged tree folds nothing.
    @return None.
    @version 1
    """
    repo = tmp_path / "repo"
    db = _two_file_db(repo, {"a.py": _A, "b.py": _B})
    cache = tmp_path / "clew.db.idxcache"

    first, first_rows = _run_stage(db, repo, cache, monkeypatch)
    second, second_rows = _run_stage(db, repo, cache, monkeypatch)

    assert first == 2, f"the cold run must fold both files, folded {first}"
    assert second == 0, f"an unchanged tree needs no folding, folded {second}"
    assert second_rows == first_rows, "the cached run must write exactly the same edges"

Activity · 8 steps
lines 179–179repo = tmp_path / "repo"lines 180–180db = _two_file_db(repo, {"a.py": _A, "b.py": _B})lines 181–181cache = tmp_path / "clew.db.idxcache"lines 183–183first, first_rows = _run_stage(db, repo, cache, monkeypatch)lines 184–184second, second_rows = _run_stage(db, repo, cache, monkeypatch)lines 186–186assert first == 2, f"the cold run must fold both files, folded {first}"lines 187–187assert second == 0, f"an unchanged tree needs no folding, folded {second}"lines 188–188assert second_rows == first_rows, "the cached run must write exactly the same edges"

test_only_the_edited_file_is_refolded

None test_ast_fold_cache.test_only_the_edited_file_is_refolded(Path tmp_path, monkeypatch)

Editing one body re-folds one file.

tests/test_ast_fold_cache.py:191

Show on the map →

Called by none

Calls _run_stage, _two_file_db

Checks

  • assert second == 1, f"only the edited file needed folding, folded {second}"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
REQ-DDB-PIPE-003measuredtest_only_the_edited_file_is_refolded → _run_stage → import_ast_call_edges → run_harvest
REQ-DDB-PIPE-011measuredtest_only_the_edited_file_is_refolded → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_only_the_edited_file_is_refolded → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-WIKI-054inferredtest_only_the_edited_file_is_refolded → _run_stage → import_ast_call_edges → _fold_all_files → _fold_call_payload → _ast_record_call_edge → _narrow_by_receiver → _one_class → all
Source · 21 lines
def test_only_the_edited_file_is_refolded(tmp_path: Path, monkeypatch) -> None:
    """THE COMMON INCREMENTAL EDIT: a body changes, no signature does, so the resolution
    inputs are identical and every other file's fold is still valid. This is the case the
    field report was made of — seven edited files out of 913.

    @brief Editing one body re-folds one file.
    @return None.
    @version 1
    """
    repo = tmp_path / "repo"
    db = _two_file_db(repo, {"a.py": _A, "b.py": _B})
    cache = tmp_path / "clew.db.idxcache"
    _run_stage(db, repo, cache, monkeypatch)

    ## A body edit only: same functions, same lines, different statement.
    (repo / "b.py").write_text(_B.replace("return y * 2", "return y * 3"), encoding="utf-8")
    second, _rows = _run_stage(db, repo, cache, monkeypatch)

    assert second == 1, f"only the edited file needed folding, folded {second}"

Activity · 7 steps
lines 200–200repo = tmp_path / "repo"lines 201–201db = _two_file_db(repo, {"a.py": _A, "b.py": _B})lines 202–202cache = tmp_path / "clew.db.idxcache"lines 203–203_run_stage(db, repo, cache, monkeypatch)lines 206–206(repo / "b.py").write_text(_B.replace("return y * 2", "return y * 3"), encoding="utf-8")lines 207–207second, _rows = _run_stage(db, repo, cache, monkeypatch)lines 209–209assert second == 1, f"only the edited file needed folding, folded {second}"

test_the_cached_run_writes_exactly_what_a_cold_run_writes

None test_ast_fold_cache.test_the_cached_run_writes_exactly_what_a_cold_run_writes(Path tmp_path, monkeypatch)

A cache-served run and a cache-free run write identical rows.

tests/test_ast_fold_cache.py:245

Show on the map →

Called by none

Calls _run_stage, _two_file_db

Checks

  • assert folded_warm == 0 and folded_cold == 2, "the two runs must differ in HOW they got there"
  • assert warm_rows == cold_rows, "...and not in what they wrote"
  • assert warm_rows, "the fixture must produce edges at all, or this asserts nothing"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
REQ-DDB-PIPE-003measuredtest_the_cached_run_writes_exactly_what_a_cold_run_writes → _run_stage → import_ast_call_edges → run_harvest
REQ-DDB-PIPE-011measuredtest_the_cached_run_writes_exactly_what_a_cold_run_writes → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _parseable_file
REQ-DDB-PIPE-012inferredtest_the_cached_run_writes_exactly_what_a_cold_run_writes → _run_stage → import_ast_call_edges → run_harvest → _harvest_one_file → _ts_language_for → _grammar_available → has_language
REQ-DDB-WIKI-054inferredtest_the_cached_run_writes_exactly_what_a_cold_run_writes → _run_stage → import_ast_call_edges → _fold_all_files → _fold_call_payload → _ast_record_call_edge → _narrow_by_receiver → _one_class → all
Source · 21 lines
def test_the_cached_run_writes_exactly_what_a_cold_run_writes(tmp_path: Path, monkeypatch) -> None:
    """PARITY, asserted rather than argued. A cache that produces almost the right edges is
    worse than no cache: the rows it gets wrong are indistinguishable from measurements.

    @brief A cache-served run and a cache-free run write identical rows.
    @return None.
    @version 1
    """
    repo = tmp_path / "repo"
    db = _two_file_db(repo, {"a.py": _A, "b.py": _B})

    warm_cache = tmp_path / "warm.idxcache"
    _run_stage(db, repo, warm_cache, monkeypatch)
    folded_warm, warm_rows = _run_stage(db, repo, warm_cache, monkeypatch)

    cold_cache = tmp_path / "cold.idxcache"
    folded_cold, cold_rows = _run_stage(db, repo, cold_cache, monkeypatch)

    assert folded_warm == 0 and folded_cold == 2, "the two runs must differ in HOW they got there"
    assert warm_rows == cold_rows, "...and not in what they wrote"
    assert warm_rows, "the fixture must produce edges at all, or this asserts nothing"

1 more lines, not shown

Activity · 10 steps
lines 253–253repo = tmp_path / "repo"lines 254–254db = _two_file_db(repo, {"a.py": _A, "b.py": _B})lines 256–256warm_cache = tmp_path / "warm.idxcache"lines 257–257_run_stage(db, repo, warm_cache, monkeypatch)lines 258–258folded_warm, warm_rows = _run_stage(db, repo, warm_cache, monkeypatch)lines 260–260cold_cache = tmp_path / "cold.idxcache"lines 261–261folded_cold, cold_rows = _run_stage(db, repo, cold_cache, monkeypatch)lines 263–263assert folded_warm == 0 and folded_cold == 2, "the two runs must differ in HOW they got there"lines 264–264assert warm_rows == cold_rows, "...and not in what they wrote"lines 265–265assert warm_rows, "the fixture must produce edges at all, or this asserts nothing"