test_external_provenance.py

tests/test_external_provenance.py

Per-file EXTERNAL provenance (gh#335) — the tag, and what must NOT move because of it.

test_a_nested_tree_with_no_indexed_file_is_not_reported

None test_external_provenance.test_a_nested_tree_with_no_indexed_file_is_not_reported(Path tmp_path)

A nested tree that owns no indexed row is never named in the stamp.

tests/test_external_provenance.py:368

Checks

  • assert external_roots(root) == ["excluded_area/someone_elses"], "detection still sees it"
  • assert tagged == 0
  • assert reported == [], "a root that owns no row must not reach build_meta"
  • assert measure_index_coverage(db_path, root).external_roots == ()

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_a_nested_tree_with_no_indexed_file_is_not_reported → external_roots → dependency_roots
REQ-DDB-INDEX-002measuredtest_a_nested_tree_with_no_indexed_file_is_not_reported → measure_index_coverage → _file_yields → memoized
REQ-DDB-INDEX-003measuredtest_a_nested_tree_with_no_indexed_file_is_not_reported → measure_index_coverage
REQ-DDB-INDEX-004measured
REQ-DDB-INDEX-005measuredtest_a_nested_tree_with_no_indexed_file_is_not_reported → stamp_external_provenance
REQ-DDB-PIPE-014catalog
Source · 31 lines
def test_a_nested_tree_with_no_indexed_file_is_not_reported(tmp_path: Path) -> None:
    """TWO REASONS, EITHER SUFFICIENT, and the second is the one that made this
    urgent. A nested tree some other rule already excluded contributes no row, so:

    - naming it is misleading — "0 files belong to 1 nested tree" reads like a broken
      detector, and that exact line cost a real measurement here before it was traced
      to an unrelated cause;
    - ANYTHING STAMPED IS PUBLISHED OVER MCP. The walk sees every directory on disk,
      including trees the index deliberately does not cover, and emitting one of those
      names into `build_meta` discloses a path this build was configured to leave out.
      Caught on this repo's own self-index, which holds an excluded target directory.

    @brief A detected-but-empty external root is counted, not named.
    @version 1
    """
    ## A DECLARED dependency with NO indexed file — the shape an exclusion leaves behind. It has
    ## to be a real gitlink, not just a directory holding `.git`: since gh#352 the latter is a
    ## stray developer clone, which is first party and would make this test pass for the wrong
    ## reason (`external_roots` empty because nothing was DETECTED, rather than because the
    ## detected root owns no row).
    db_path, root = _indexed(tmp_path, _ours(), nested=("excluded_area/someone_elses",))

    assert external_roots(root) == ["excluded_area/someone_elses"], "detection still sees it"

    tagged, reported = stamp_external_provenance(db_path, root)

    assert tagged == 0
    assert reported == [], "a root that owns no row must not reach build_meta"
    assert measure_index_coverage(db_path, root).external_roots == ()

Activity · 6 steps
lines 388–388db_path, root = _indexed(tmp_path, _ours(), nested=("excluded_area/someone_elses",))lines 390–390assert external_roots(root) == ["excluded_area/someone_elses"], "detection still sees it"lines 392–392tagged, reported = stamp_external_provenance(db_path, root)lines 394–394assert tagged == 0lines 395–395assert reported == [], "a root that owns no row must not reach build_meta"lines 396–396assert measure_index_coverage(db_path, root).external_roots == ()

test_a_shared_path_prefix_does_not_misattribute_a_sibling_tree

None test_external_provenance.test_a_shared_path_prefix_does_not_misattribute_a_sibling_tree(Path tmp_path)

Two sibling trees sharing a name prefix are attributed separately.

tests/test_external_provenance.py:431

Checks

  • assert owners["vendor/lib/a.c"] == "vendor/lib"
  • assert owners["vendor/lib-utils/b.c"] == "vendor/lib-utils"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_a_shared_path_prefix_does_not_misattribute_a_sibling_tree → stamp_external_provenance → external_roots → dependency_roots
REQ-DDB-INDEX-005measuredtest_a_shared_path_prefix_does_not_misattribute_a_sibling_tree → stamp_external_provenance
REQ-DDB-PIPE-014catalog
REQ-DDB-QUERY-001measuredtest_a_shared_path_prefix_does_not_misattribute_a_sibling_tree → list_files
REQ-DDB-QUERY-003measuredtest_a_shared_path_prefix_does_not_misattribute_a_sibling_tree → list_files → table_exists
Source · 22 lines
def test_a_shared_path_prefix_does_not_misattribute_a_sibling_tree(tmp_path: Path) -> None:
    """`vendor/lib` and `vendor/lib-utils` share a prefix, and a bare `startswith`
    would tag the second repo's files as belonging to the first — a WRONG attribution,
    which is worse than an absent one because it reads as a confident measurement
    rather than as missing knowledge.

    @brief Prefix matching respects the path separator.
    @version 1
    """
    db_path, root = _indexed(
        tmp_path,
        [("vendor/lib/a.c", 3), ("vendor/lib-utils/b.c", 3)],
        nested=("vendor/lib", "vendor/lib-utils"),
    )
    stamp_external_provenance(db_path, root)

    owners = {entry.path: entry.external_root for entry in q.list_files(db_path)}

    assert owners["vendor/lib/a.c"] == "vendor/lib"
    assert owners["vendor/lib-utils/b.c"] == "vendor/lib-utils"

Activity · 5 steps
lines 440–444db_path, root = _indexed( tmp_path, [("vendor/lib/a.c", 3), ("vendor/lib-utils/b.c", 3)], nested=("vendor/lib", "vendor/lib-utils"), )lines 445–445stamp_external_provenance(db_path, root)lines 447–447owners = {entry.path: entry.external_root for entry in q.list_files(db_path)}lines 449–449assert owners["vendor/lib/a.c"] == "vendor/lib"lines 450–450assert owners["vendor/lib-utils/b.c"] == "vendor/lib-utils"

test_a_stray_developer_clone_the_parent_tracks_is_first_party

None test_external_provenance.test_a_stray_developer_clone_the_parent_tracks_is_first_party(Path tmp_path)

A stray clone inside a directory the parent TRACKS is first party.

tests/test_external_provenance.py:170

Show on the map →

Called by none

Calls _indexed, _ours, _theirs, external_roots, git_run, stamp_external_provenance

Checks

  • assert (make_gitlink_free_clone / ".git").is_dir(), "the fixture must hold a REAL git tree"
  • assert external_roots(root) == ["vendor/theirs"], ("a stray clone in a tracked directory is NOT a declared dependency — reporting it " "makes the tag depend on who built the index rather than on what the repo committed" )
  • assert rows["evidence/checkout/probe.c"] is None, "the repo's own tracked file stays ours"
  • assert rows["vendor/theirs/lib_0.c"] == "vendor/theirs"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_a_stray_developer_clone_the_parent_tracks_is_first_party → external_roots → dependency_roots
REQ-DDB-INDEX-005measuredtest_a_stray_developer_clone_the_parent_tracks_is_first_party → stamp_external_provenance
REQ-DDB-PIPE-014catalog
Source · 46 lines
def test_a_stray_developer_clone_the_parent_tracks_is_first_party(tmp_path: Path) -> None:
    """THE NEGATIVE HALF OF gh#352, and the case that forced the predicate split. Both
    directories here hold a real `.git`; only one is a gitlink in the parent's index. The other
    is a repository somebody CLONED into a directory whose files this repo tracks and owns.

    WHY THAT MUST BE FIRST PARTY. The external tag would otherwise be a property of the WORKING
    COPY rather than of the repository: the same commit indexes differently depending on who
    built it, and one of those indexes reports the repo's own code as somebody else's. Measured
    on [tvanfossen/entropic](https://github.com/tvanfossen/entropic): three roots tagged
    external against the ONE `.gitmodules` declares, because two of them held entropic's own
    committed blobs — `git ls-files -s` returns mode 100644 there, not 160000.

    A `.git`-only detector cannot express this test at all: it answers "somebody has a
    repository here", and the question is "did this repository declare it as a dependency".

    @brief A `.git` with no gitlink in the parent is first party.
    @version 1
    """
    db_path, root = _indexed(
        tmp_path,
        [*_ours(), *_theirs(), ("evidence/checkout/probe.c", 4)],
        nested=("vendor/theirs",),
    )
    ## A real repository, and the parent TRACKS its file rather than recording a gitlink — the
    ## shape `git clone` leaves behind inside a tracked directory.
    make_gitlink_free_clone = root / "evidence" / "checkout"
    git_run(make_gitlink_free_clone, "init", "-q", ".")
    git_run(root, "add", "-f", "evidence/checkout/probe.c")

    assert (make_gitlink_free_clone / ".git").is_dir(), "the fixture must hold a REAL git tree"
    assert external_roots(root) == ["vendor/theirs"], (
        "a stray clone in a tracked directory is NOT a declared dependency — reporting it "
        "makes the tag depend on who built the index rather than on what the repo committed"
    )

    stamp_external_provenance(db_path, root)
    conn = sqlite3.connect(str(db_path))
    try:
        rows = dict(conn.execute(f"SELECT name, {EXTERNAL_ROOT_COLUMN} FROM path").fetchall())
    finally:
        conn.close()

    assert rows["evidence/checkout/probe.c"] is None, "the repo's own tracked file stays ours"
    assert rows["vendor/theirs/lib_0.c"] == "vendor/theirs"

Activity · 12 steps
lines 188–192db_path, root = _indexed( tmp_path, [*_ours(), *_theirs(), ("evidence/checkout/probe.c", 4)], nested=("vendor/theirs",), )lines 195–195make_gitlink_free_clone = root / "evidence" / "checkout"lines 196–196git_run(make_gitlink_free_clone, "init", "-q", ".")lines 197–197git_run(root, "add", "-f", "evidence/checkout/probe.c")lines 199–199assert (make_gitlink_free_clone / ".git").is_dir(), "the fixture must hold a REAL git tree"lines 200–203assert external_roots(root) == ["vendor/theirs"], ( "a stray clone in a tracked directory is NOT a declared dependency — reporting it " "makes the tag depend on who built the index rather than on what the repo committed" )lines 205–205stamp_external_provenance(db_path, root)lines 206–206conn = sqlite3.connect(str(db_path))lines 207–207trylines 208–208rows = dict(conn.execute(f"SELECT name, {EXTERNAL_ROOT_COLUMN} FROM path").fetchall())lines 212–212assert rows["evidence/checkout/probe.c"] is None, "the repo's own tracked file stays ours"lines 213–213assert rows["vendor/theirs/lib_0.c"] == "vendor/theirs"

test_a_submodule_whose_git_is_a_file_is_detected

None test_external_provenance.test_a_submodule_whose_git_is_a_file_is_detected(Path tmp_path)

A SUBMODULE, whose .git is a FILE, is detected like a clone.

tests/test_external_provenance.py:401

Show on the map →

Called by none

Calls external_roots, make_gitlink, repo_with_submodules

Checks

  • assert not (root / "extern" / "theirs" / ".git").is_dir(), ("the fixture's whole point is that this `.git` is a FILE — if some helper turned it " "back into a directory the test would pass while covering the case that already worked" )
  • assert external_roots(root) == ["extern/theirs"], ("a submodule's .git is a FILE; a detector that only looks for a directory " "is blind to every submodule while still reporting a plausible count" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_a_submodule_whose_git_is_a_file_is_detected → external_roots → dependency_roots
REQ-DDB-INDEX-005measuredtest_a_submodule_whose_git_is_a_file_is_detected → external_roots
REQ-DDB-PIPE-014catalog
Source · 28 lines
def test_a_submodule_whose_git_is_a_file_is_detected(tmp_path: Path) -> None:
    """THE CASE THE WHOLE FEATURE WAS BUILT FOR, and the one every other fixture in
    this file gets wrong. A `git submodule` checkout writes a 44-byte
    `gitdir: ../../.git/modules/<name>` POINTER FILE where a standalone clone has a
    `.git` DIRECTORY, and the detector tested `".git" in dirnames`.

    Found on [tvanfossen/entropic](https://github.com/tvanfossen/entropic), which
    wraps llama.cpp as a submodule: the build reported 28 nested trees — a number
    that made the detector look like it was working — and `extern/llama.cpp` was not
    one of them. No unit fixture could have caught it, because every fixture built a
    `.git` directory. This one does not.

    @brief A `.git` file marks a nested tree just as a `.git` directory does.
    @version 1
    """
    root = repo_with_submodules(tmp_path / "repo")
    make_gitlink(root, "extern/theirs", git_as_file=True)

    assert not (root / "extern" / "theirs" / ".git").is_dir(), (
        "the fixture's whole point is that this `.git` is a FILE — if some helper turned it "
        "back into a directory the test would pass while covering the case that already worked"
    )
    assert external_roots(root) == ["extern/theirs"], (
        "a submodule's .git is a FILE; a detector that only looks for a directory "
        "is blind to every submodule while still reporting a plausible count"
    )

Activity · 4 steps
lines 416–416root = repo_with_submodules(tmp_path / "repo")lines 417–417make_gitlink(root, "extern/theirs", git_as_file=True)lines 419–422assert not (root / "extern" / "theirs" / ".git").is_dir(), ( "the fixture's whole point is that this `.git` is a FILE — if some helper turned it " "back into a directory the test would pass while covering the case that already worked" )lines 423–426assert external_roots(root) == ["extern/theirs"], ( "a submodule's .git is a FILE; a detector that only looks for a directory " "is blind to every submodule while still reporting a plausible count" )

test_a_symlinked_doc_outside_the_repository_is_not_ingested

None test_external_provenance.test_a_symlinked_doc_outside_the_repository_is_not_ingested(Path tmp_path)

A committed symlink pointing outside the repo is not ingested as repo prose.

tests/test_external_provenance.py:521

Checks

  • assert not any("SENTINEL-PROSE-LEAK" in content for _, content in rows), (f"a file outside the repository was ingested as repo prose: {rows}" )
  • assert any(path == "docs/real.md" for path, _ in rows), ("the containment check must not cost the ordinary in-repo document" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measuredtest_a_symlinked_doc_outside_the_repository_is_not_ingested → ingest_supplementary_docs → _ingest_one_doc_file → memoized
REQ-DDB-PIPE-001measuredtest_a_symlinked_doc_outside_the_repository_is_not_ingested → ingest_supplementary_docs
REQ-DDB-PIPE-014measuredtest_a_symlinked_doc_outside_the_repository_is_not_ingested → ingest_supplementary_docs → within_repo
Source · 41 lines
def test_a_symlinked_doc_outside_the_repository_is_not_ingested(tmp_path: Path) -> None:
    """H2, 1.1.0 SECURITY AUDIT. `glob` FOLLOWS SYMLINKS, and git tracks symlinks, so a
    repository could commit `README.md -> /etc/passwd` (or `/proc/self/environ`, which carries
    the indexing user's `ANTHROPIC_API_KEY`) and clew would read it, chunk it into
    `supplementary_docs`, and serve it to a model labelled with an in-repo path — the worst
    combination: exfiltration plus a false provenance an agent has no way to doubt.

    `clew/query/source.py:_read_body` already resolves-then-refuses on the read side; the
    INGEST side did not. A missing or escaping file is skipped exactly as an unreadable one is.

    @brief An escaping symlink contributes no prose row.
    @return None.
    @version 1
    """
    from clew.prose import ingest_supplementary_docs

    secret = tmp_path / "outside" / "secret.md"
    secret.parent.mkdir(parents=True)
    secret.write_text("# Secret\n\nSENTINEL-PROSE-LEAK\n", encoding="utf-8")

    repo = tmp_path / "repo"
    (repo / "docs").mkdir(parents=True)
    (repo / "README.md").symlink_to(secret)
    (repo / "docs" / "guide.md").symlink_to(secret)
    (repo / "docs" / "real.md").write_text("# Real\n\nin-repo content\n", encoding="utf-8")

    db = tmp_path / "clew.db"
    ingest_supplementary_docs(db, repo)

    conn = sqlite3.connect(str(db))
    rows = conn.execute("SELECT file_path, content FROM supplementary_docs").fetchall()
    conn.close()

    assert not any("SENTINEL-PROSE-LEAK" in content for _, content in rows), (
        f"a file outside the repository was ingested as repo prose: {rows}"
    )
    assert any(path == "docs/real.md" for path, _ in rows), (
        "the containment check must not cost the ordinary in-repo document"
    )

Activity · 16 steps
lines 535–535from clew.prose import ingest_supplementary_docslines 537–537secret = tmp_path / "outside" / "secret.md"lines 538–538secret.parent.mkdir(parents=True)lines 539–539secret.write_text("# Secret\n\nSENTINEL-PROSE-LEAK\n", encoding="utf-8")lines 541–541repo = tmp_path / "repo"lines 542–542(repo / "docs").mkdir(parents=True)lines 543–543(repo / "README.md").symlink_to(secret)lines 544–544(repo / "docs" / "guide.md").symlink_to(secret)lines 545–545(repo / "docs" / "real.md").write_text("# Real\n\nin-repo content\n", encoding="utf-8")lines 547–547db = tmp_path / "clew.db"lines 548–548ingest_supplementary_docs(db, repo)lines 550–550conn = sqlite3.connect(str(db))lines 551–551rows = conn.execute("SELECT file_path, content FROM supplementary_docs").fetchall()lines 552–552conn.close()lines 554–556assert not any("SENTINEL-PROSE-LEAK" in content for _, content in rows), ( f"a file outside the repository was ingested as repo prose: {rows}" )lines 557–559assert any(path == "docs/real.md" for path, _ in rows), ( "the containment check must not cost the ordinary in-repo document" )

test_a_symlinked_source_file_contributes_no_file_doc

None test_external_provenance.test_a_symlinked_source_file_contributes_no_file_doc(Path tmp_path)

A symlinked source file does not leak an outside file as its file-level doc.

tests/test_external_provenance.py:566

Checks

  • assert not any("SENTINEL-FILEDOC-LEAK" in doc for _, doc in docs), (f"an outside file was read as an in-repo file's documentation: {docs}" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002inferredtest_a_symlinked_source_file_contributes_no_file_doc → ingest_file_docs → _collect_file_docs → memoized
REQ-DDB-PIPE-008measuredtest_a_symlinked_source_file_contributes_no_file_doc → ingest_file_docs
REQ-DDB-PIPE-014measuredtest_a_symlinked_source_file_contributes_no_file_doc → ingest_file_docs → _collect_file_docs → within_repo
REQ-DDB-SCHEMA-014measuredtest_a_symlinked_source_file_contributes_no_file_doc → ingest_file_docs → ensure_file_docs_table
Source · 36 lines
def test_a_symlinked_source_file_contributes_no_file_doc(tmp_path: Path) -> None:
    """The second half of H2. `filedocs` reads `repo_root / rel` for every INDEXED file, so a
    symlinked `.c` whose target lives outside the tree has its docblock extracted and served
    as that path's file documentation. The path list comes from the index, so this one is
    reachable even where the prose globs are not.

    @brief An escaping path is not read for file-level documentation.
    @return None.
    @version 1
    """
    from clew.filedocs import ingest_file_docs

    outside = tmp_path / "outside" / "leak.c"
    outside.parent.mkdir(parents=True)
    outside.write_text("/** @file\n * SENTINEL-FILEDOC-LEAK\n */\n", encoding="utf-8")

    repo = tmp_path / "repo"
    repo.mkdir()
    (repo / "leaked.c").symlink_to(outside)

    db = tmp_path / "clew.db"
    conn = sqlite3.connect(str(db))
    conn.execute("CREATE TABLE path (name TEXT PRIMARY KEY, type INTEGER NOT NULL)")
    conn.execute("INSERT INTO path (name, type) VALUES ('leaked.c', 1)")
    conn.commit()
    conn.close()

    ingest_file_docs(db, repo)

    conn = sqlite3.connect(str(db))
    docs = conn.execute("SELECT file_path, doc FROM file_docs").fetchall()
    conn.close()

    assert not any("SENTINEL-FILEDOC-LEAK" in doc for _, doc in docs), (
        f"an outside file was read as an in-repo file's documentation: {docs}"
    )

1 more lines, not shown

Activity · 18 steps
lines 576–576from clew.filedocs import ingest_file_docslines 578–578outside = tmp_path / "outside" / "leak.c"lines 579–579outside.parent.mkdir(parents=True)lines 580–580outside.write_text("/** @file\n * SENTINEL-FILEDOC-LEAK\n */\n", encoding="utf-8")lines 582–582repo = tmp_path / "repo"lines 583–583repo.mkdir()lines 584–584(repo / "leaked.c").symlink_to(outside)lines 586–586db = tmp_path / "clew.db"lines 587–587conn = sqlite3.connect(str(db))lines 588–588conn.execute("CREATE TABLE path (name TEXT PRIMARY KEY, type INTEGER NOT NULL)")lines 589–589conn.execute("INSERT INTO path (name, type) VALUES ('leaked.c', 1)")lines 590–590conn.commit()lines 591–591conn.close()lines 593–593ingest_file_docs(db, repo)lines 595–595conn = sqlite3.connect(str(db))lines 596–596docs = conn.execute("SELECT file_path, doc FROM file_docs").fetchall()lines 597–597conn.close()lines 599–601assert not any("SENTINEL-FILEDOC-LEAK" in doc for _, doc in docs), ( f"an outside file was read as an in-repo file's documentation: {docs}" )

test_admitting_a_submodule_leaves_first_party_coverage_identical

None test_external_provenance.test_admitting_a_submodule_leaves_first_party_coverage_identical(Path tmp_path)

Admitting a submodule must not move a single first-party coverage figure.

tests/test_external_provenance.py:218

Show on the map →

Called by none

Calls _indexed, _ours, _theirs, measure_index_coverage, stamp_external_provenance

Checks

  • assert both.indexed_files == alone.indexed_files
  • assert both.substantive_files == alone.substantive_files
  • assert both.barren_files == alone.barren_files
  • assert both.barren_ratio == alone.barren_ratio
  • assert both.undocumented_files == alone.undocumented_files
  • assert both.external_files == 12
  • assert both.external_roots == ("vendor/theirs",)
  • assert alone.external_files == 0

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_admitting_a_submodule_leaves_first_party_coverage_identical → stamp_external_provenance → external_roots → dependency_roots
REQ-DDB-INDEX-002measuredtest_admitting_a_submodule_leaves_first_party_coverage_identical → measure_index_coverage → _file_yields → memoized
REQ-DDB-INDEX-003measuredtest_admitting_a_submodule_leaves_first_party_coverage_identical → measure_index_coverage
REQ-DDB-INDEX-004measured
REQ-DDB-INDEX-005measuredtest_admitting_a_submodule_leaves_first_party_coverage_identical → stamp_external_provenance
REQ-DDB-PIPE-014catalog
Source · 36 lines
def test_admitting_a_submodule_leaves_first_party_coverage_identical(tmp_path: Path) -> None:
    """THE LOAD-BEARING CONTROL, and the reason it is phrased as a comparison of two
    builds rather than as an expected number: it holds whatever the matcher does. If
    a first-party figure moves when a nested tree is admitted, the tagging is wrong,
    and no assertion about the matcher's internals would have caught it.

    The external files are barren, so an untagged build would drag `barren_ratio`
    from 0.0 to 12/37 — a visible, wrong measurement rather than a marginal one.

    @brief First-party coverage is invariant under admitting an external tree.
    @version 1
    """
    alone_db, alone_root = _indexed(tmp_path / "alone", _ours())
    stamp_external_provenance(alone_db, alone_root)
    alone = measure_index_coverage(alone_db, alone_root)

    both_db, both_root = _indexed(
        tmp_path / "both", [*_ours(), *_theirs()], nested=("vendor/theirs",)
    )
    stamp_external_provenance(both_db, both_root)
    both = measure_index_coverage(both_db, both_root)

    assert both.indexed_files == alone.indexed_files
    assert both.substantive_files == alone.substantive_files
    assert both.barren_files == alone.barren_files
    assert both.barren_ratio == alone.barren_ratio
    assert both.undocumented_files == alone.undocumented_files

    ## And the TOTAL really did rise — without this the test above would pass just as
    ## well against a build that never indexed the submodule at all, which is the
    ## behaviour gh#333 removed.
    assert both.external_files == 12
    assert both.external_roots == ("vendor/theirs",)
    assert alone.external_files == 0

Activity · 14 steps
lines 230–230alone_db, alone_root = _indexed(tmp_path / "alone", _ours())lines 231–231stamp_external_provenance(alone_db, alone_root)lines 232–232alone = measure_index_coverage(alone_db, alone_root)lines 234–236both_db, both_root = _indexed( tmp_path / "both", [*_ours(), *_theirs()], nested=("vendor/theirs",) )lines 237–237stamp_external_provenance(both_db, both_root)lines 238–238both = measure_index_coverage(both_db, both_root)lines 240–240assert both.indexed_files == alone.indexed_fileslines 241–241assert both.substantive_files == alone.substantive_fileslines 242–242assert both.barren_files == alone.barren_fileslines 243–243assert both.barren_ratio == alone.barren_ratiolines 244–244assert both.undocumented_files == alone.undocumented_fileslines 249–249assert both.external_files == 12lines 250–250assert both.external_roots == ("vendor/theirs",)lines 251–251assert alone.external_files == 0

test_an_unresolved_include_is_neither_first_party_nor_external

None test_external_provenance.test_an_unresolved_include_is_neither_first_party_nor_external(Path tmp_path)

An unresolved #include row must not be counted as this repo's.

tests/test_external_provenance.py:328

Checks

  • assert coverage.indexed_files == 25, "an unresolvable include is not one of this repo's files"
  • assert coverage.external_files == 0, "nor is it attributable to a named foreign tree"
  • assert coverage.unresolved_files == 1, "and the exclusion is reported, not silent"
  • assert stats.files.indexed_files == 25
  • assert stats.files.unresolved_files == 1

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_an_unresolved_include_is_neither_first_party_nor_external → stamp_external_provenance → external_roots → dependency_roots
REQ-DDB-INDEX-002measuredtest_an_unresolved_include_is_neither_first_party_nor_external → measure_index_coverage → _file_yields → memoized
REQ-DDB-INDEX-003measuredtest_an_unresolved_include_is_neither_first_party_nor_external → measure_index_coverage
REQ-DDB-INDEX-004measuredtest_an_unresolved_include_is_neither_first_party_nor_external → graph_stats → _symbol_provenance → records_provenance
REQ-DDB-INDEX-005measuredtest_an_unresolved_include_is_neither_first_party_nor_external → stamp_external_provenance
REQ-DDB-MCP-001measuredtest_an_unresolved_include_is_neither_first_party_nor_external → graph_stats → file_counts → meta_section
REQ-DDB-PIPE-005measuredtest_an_unresolved_include_is_neither_first_party_nor_external → graph_stats → read_build_signature
REQ-DDB-PIPE-014catalog
REQ-DDB-QUERY-001measuredtest_an_unresolved_include_is_neither_first_party_nor_external → graph_stats → connect
REQ-DDB-QUERY-003measuredtest_an_unresolved_include_is_neither_first_party_nor_external → graph_stats → call_edge_counts → table_exists
REQ-DDB-QUERY-008measuredtest_an_unresolved_include_is_neither_first_party_nor_external → graph_stats
Source · 38 lines
def test_an_unresolved_include_is_neither_first_party_nor_external(tmp_path: Path) -> None:
    """THE DEFECT THAT MADE THE INVARIANCE ABOVE FALSE, and it was found on the real
    target rather than here. Doxygen registers a `path` row for every `#include` it
    cannot resolve, spelled as a BARE FILENAME with no directory. A bare filename
    matches no external root, so every one of them counted as first party.

    Measured on [entropic](https://github.com/tvanfossen/entropic): admitting the
    llama.cpp submodule added 324 such rows from its Ascend and Hexagon backends and
    moved `indexed_files` from 488 to 804 — on a change whose entire contract is that
    no first-party figure moves. The RATIOS held throughout, because those files do
    not exist on disk and the line count had already dropped them from the
    substantive set. An invariant preserved by an accident one layer down is not
    preserved, and the accident is exactly why no ratio assertion caught it.

    @brief A bare-filename row is counted apart from both populations.
    @version 1
    """
    db_path, root = _indexed(tmp_path, _ours())
    conn = sqlite3.connect(str(db_path))
    try:
        ## Exactly what doxygen writes for an unresolvable include: a name with no
        ## directory and no file behind it.
        conn.execute("INSERT INTO path (type, name) VALUES (1, 'aclnn_add.h')")
        conn.commit()
    finally:
        conn.close()
    stamp_external_provenance(db_path, root)

    coverage = measure_index_coverage(db_path, root)
    assert coverage.indexed_files == 25, "an unresolvable include is not one of this repo's files"
    assert coverage.external_files == 0, "nor is it attributable to a named foreign tree"
    assert coverage.unresolved_files == 1, "and the exclusion is reported, not silent"

    stats = q.graph_stats(db_path)
    assert stats.files.indexed_files == 25
    assert stats.files.unresolved_files == 1

Activity · 13 steps
lines 345–345db_path, root = _indexed(tmp_path, _ours())lines 346–346conn = sqlite3.connect(str(db_path))lines 347–347trylines 350–350conn.execute("INSERT INTO path (type, name) VALUES (1, 'aclnn_add.h')")lines 351–351conn.commit()lines 354–354stamp_external_provenance(db_path, root)lines 356–356coverage = measure_index_coverage(db_path, root)lines 357–357assert coverage.indexed_files == 25, "an unresolvable include is not one of this repo's files"lines 358–358assert coverage.external_files == 0, "nor is it attributable to a named foreign tree"lines 359–359assert coverage.unresolved_files == 1, "and the exclusion is reported, not silent"lines 361–361stats = q.graph_stats(db_path)lines 362–362assert stats.files.indexed_files == 25lines 363–363assert stats.files.unresolved_files == 1

test_an_untagged_index_reports_no_external_recording

None test_external_provenance.test_an_untagged_index_reports_no_external_recording(Path tmp_path)

An index predating the tag reports every file as first party, and says so.

tests/test_external_provenance.py:280

Show on the map →

Called by none

Calls _indexed, _ours, graph_stats

Checks

  • assert stats.files.indexed_files == 25
  • assert stats.files.external_files == 0
  • assert stats.files.external_recorded is False

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-004measuredtest_an_untagged_index_reports_no_external_recording → graph_stats → _symbol_provenance → records_provenance
REQ-DDB-MCP-001measuredtest_an_untagged_index_reports_no_external_recording → graph_stats → file_counts → meta_section
REQ-DDB-PIPE-005measuredtest_an_untagged_index_reports_no_external_recording → graph_stats → read_build_signature
REQ-DDB-PIPE-014catalog
REQ-DDB-QUERY-001measuredtest_an_untagged_index_reports_no_external_recording → graph_stats → connect
REQ-DDB-QUERY-003measuredtest_an_untagged_index_reports_no_external_recording → graph_stats → call_edge_counts → table_exists
REQ-DDB-QUERY-008measuredtest_an_untagged_index_reports_no_external_recording → graph_stats
Source · 22 lines
def test_an_untagged_index_reports_no_external_recording(tmp_path: Path) -> None:
    """`external_files == 0` is TRUE both for a repo that vendors nothing and for an
    index built before gh#335 — `external_recorded` is what separates them, for the
    same reason `coverage_recorded` exists. An unmeasured zero that reads as a
    measured one is how a reader concludes "checked and fine" from "not checked".

    Counting everything as first party on such an index is correct rather than a
    fallback: a build below build version 32 EXCLUDED nested git trees outright, so
    everything it holds really is first party.

    @brief An index with no external column reports first-party counts and not-recorded.
    @version 1
    """
    db_path, _ = _indexed(tmp_path, _ours())

    stats = q.graph_stats(db_path)

    assert stats.files.indexed_files == 25
    assert stats.files.external_files == 0
    assert stats.files.external_recorded is False

Activity · 5 steps
lines 293–293db_path, _ = _indexed(tmp_path, _ours())lines 295–295stats = q.graph_stats(db_path)lines 297–297assert stats.files.indexed_files == 25lines 298–298assert stats.files.external_files == 0lines 299–299assert stats.files.external_recorded is False

test_directory_rollup_says_what_it_counts_and_lists_external_files

None test_external_provenance.test_directory_rollup_says_what_it_counts_and_lists_external_files(Path tmp_path)

The directory rollup lists what is INDEXED and never claims to be a tree census.

tests/test_external_provenance.py:456

Show on the map →

Called by none

Calls _indexed, _ours, _theirs, directory_rollup, rollup_meaning, stamp_external_provenance

Checks

  • assert by_dir, "the rollup must produce rows for an index that holds files"
  • assert roll.indexed_files == sum(e.indexed_files for e in roll.directories)
  • assert counts == sorted(counts, reverse=True)
  • assert sum(e.external_files for e in roll.directories) > 0, ("an external file must be COUNTED and annotated, never filtered out of the inventory" )
  • assert "INDEXED" in note and "not a census of the tree" in note
  • assert "largest directory in the repository" in note, ("the reply must separate 'largest here' from 'largest in the repository'" )
  • assert "not one subtraction" in note, ("it must NOT claim the coverage difference is a single arithmetic — two attempts at that " "sum both failed to reconcile" )
  • assert rollup_meaning(0, 0, 0) == ""
  • assert "not shown" in rollup_meaning(100, 5, 9)
  • assert "not shown" not in rollup_meaning(100, 9, 9)

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_directory_rollup_says_what_it_counts_and_lists_external_files → stamp_external_provenance → external_roots → dependency_roots
REQ-DDB-INDEX-005measuredtest_directory_rollup_says_what_it_counts_and_lists_external_files → stamp_external_provenance
REQ-DDB-PIPE-014catalog
REQ-DDB-QUERY-001measuredtest_directory_rollup_says_what_it_counts_and_lists_external_files → directory_rollup → connect
REQ-DDB-QUERY-002measuredtest_directory_rollup_says_what_it_counts_and_lists_external_files → rollup_meaning
REQ-DDB-QUERY-003measuredtest_directory_rollup_says_what_it_counts_and_lists_external_files → directory_rollup → has_columns
Source · 61 lines
def test_directory_rollup_says_what_it_counts_and_lists_external_files(tmp_path: Path) -> None:
    """SIX SHELL CALLS TO REPLACE A DELETED TOOL. `list_files` has always inventoried the indexed
    files, and the `list_files` TOOL was removed in the four-tool consolidation — so on mbedtls
    2026-08-14 Q4 ran six `find … | wc -l` commands, 6 of that cell's 11 fallbacks, and still
    missed the four marks it was computing them for.

    THE ROLLUP MUST NOT READ AS A TREE CENSUS, and on this target that distinction inverts the
    answer: mbedtls `tests/` holds 310 tracked files and 44 indexed ones, because most are
    `.function` / `.data` fixtures no grammar handles. So the largest directory in the INDEX is
    `library` and in the REPOSITORY is `tests`, and a reply offering the first as the second would
    argue for a wrong answer — the same class as the two false-count rosters and the config
    payload.

    IT ALSO MUST NOT ASSERT AN ARITHMETIC IT DOES NOT HAVE. `coverage.indexed_files` is 443 where
    the rollup is 527; I wrote the reconciliation as 527-41 and then as 527-41-259 and NEITHER
    added up, because coverage also drops prose and documentation and those categories overlap. So
    the note states the two DEFINITIONS instead. A stated sum that does not add up is worse than
    none, and this test pins the definitions rather than the numbers for the same reason.

    NON-FILTERING HOLDS HERE TOO: an external file is counted and annotated, never omitted, or the
    rollup would report a smaller repository than the index holds.
    """
    from clew.query import directory_rollup
    from clew.query.corpus import rollup_meaning

    db_path, root = _indexed(tmp_path, [*_ours(), *_theirs()], nested=("vendor/theirs",))
    stamp_external_provenance(db_path, root)
    roll = directory_rollup(db_path)

    by_dir = {e.directory: e for e in roll.directories}
    assert by_dir, "the rollup must produce rows for an index that holds files"
    assert roll.indexed_files == sum(e.indexed_files for e in roll.directories)
    ## Largest first, so "which directory is biggest HERE" is the first row rather than a scan.
    counts = [e.indexed_files for e in roll.directories]
    assert counts == sorted(counts, reverse=True)

    ## THE EXTERNAL FILES ARE PRESENT AND ANNOTATED. The fixture indexes a vendored git tree, so
    ## some directory must carry a non-zero external count — omitting them would be the
    ## filtered-answer failure applied to the inventory.
    assert sum(e.external_files for e in roll.directories) > 0, (
        "an external file must be COUNTED and annotated, never filtered out of the inventory"
    )

    ## THE SENTENCE IS THE LOAD-BEARING PART, and it must make both misreadings impossible.
    note = roll.rollup_meaning
    assert "INDEXED" in note and "not a census of the tree" in note
    assert "largest directory in the repository" in note, (
        "the reply must separate 'largest here' from 'largest in the repository'"
    )
    assert "not one subtraction" in note, (
        "it must NOT claim the coverage difference is a single arithmetic — two attempts at that "
        "sum both failed to reconcile"
    )

    ## AND IT STAYS SILENT ON AN EMPTY INDEX rather than describing an absent file set.
    assert rollup_meaning(0, 0, 0) == ""
    ## The cap is reported when it applies, never silently.
    assert "not shown" in rollup_meaning(100, 5, 9)
    assert "not shown" not in rollup_meaning(100, 9, 9)

Activity · 18 steps
lines 478–478from clew.query import directory_rolluplines 479–479from clew.query.corpus import rollup_meaninglines 481–481db_path, root = _indexed(tmp_path, [*_ours(), *_theirs()], nested=("vendor/theirs",))lines 482–482stamp_external_provenance(db_path, root)lines 483–483roll = directory_rollup(db_path)lines 485–485by_dir = {e.directory: e for e in roll.directories}lines 486–486assert by_dir, "the rollup must produce rows for an index that holds files"lines 487–487assert roll.indexed_files == sum(e.indexed_files for e in roll.directories)lines 489–489counts = [e.indexed_files for e in roll.directories]lines 490–490assert counts == sorted(counts, reverse=True)lines 495–497assert sum(e.external_files for e in roll.directories) > 0, ( "an external file must be COUNTED and annotated, never filtered out of the inventory" )lines 500–500note = roll.rollup_meaninglines 501–501assert "INDEXED" in note and "not a census of the tree" in notelines 502–504assert "largest directory in the repository" in note, ( "the reply must separate 'largest here' from 'largest in the repository'" )lines 505–508assert "not one subtraction" in note, ( "it must NOT claim the coverage difference is a single arithmetic — two attempts at that " "sum both failed to reconcile" )lines 511–511assert rollup_meaning(0, 0, 0) == ""lines 513–513assert "not shown" in rollup_meaning(100, 5, 9)lines 514–514assert "not shown" not in rollup_meaning(100, 9, 9)

test_graph_stats_counts_first_party_and_reports_external_beside_it

None test_external_provenance.test_graph_stats_counts_first_party_and_reports_external_beside_it(Path tmp_path)

graph_stats reports first party by default and names the external population.

tests/test_external_provenance.py:256

Show on the map →

Called by none

Calls _indexed, _ours, _theirs, graph_stats, stamp_external_provenance

Checks

  • assert stats.files.indexed_files == 25, "the 12 vendored files are not this repo's"
  • assert stats.files.files_with_symbols == 25
  • assert stats.files.files_with_bodies == 25
  • assert stats.files.external_files == 12
  • assert stats.files.external_roots == ("vendor/theirs",)
  • assert stats.files.external_recorded is True

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → stamp_external_provenance → external_roots → dependency_roots
REQ-DDB-INDEX-004measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → graph_stats → _symbol_provenance → records_provenance
REQ-DDB-INDEX-005measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → stamp_external_provenance
REQ-DDB-MCP-001measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → graph_stats → file_counts → meta_section
REQ-DDB-PIPE-005measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → graph_stats → read_build_signature
REQ-DDB-PIPE-014catalog
REQ-DDB-QUERY-001measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → graph_stats → connect
REQ-DDB-QUERY-003measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → graph_stats → call_edge_counts → table_exists
REQ-DDB-QUERY-008measuredtest_graph_stats_counts_first_party_and_reports_external_beside_it → graph_stats
Source · 22 lines
def test_graph_stats_counts_first_party_and_reports_external_beside_it(tmp_path: Path) -> None:
    """The same invariance one layer up, where a consumer actually reads it. Asserted
    against the file counts `graph_stats` COMPUTES rather than the coverage figures it
    merely reads back, because those are two different mechanisms and only one of them
    is exercised by the coverage test above.

    @brief graph_stats file counts exclude external files and count them separately.
    @version 1
    """
    db_path, root = _indexed(tmp_path, [*_ours(), *_theirs()], nested=("vendor/theirs",))
    stamp_external_provenance(db_path, root)

    stats = q.graph_stats(db_path)

    assert stats.files.indexed_files == 25, "the 12 vendored files are not this repo's"
    assert stats.files.files_with_symbols == 25
    assert stats.files.files_with_bodies == 25
    assert stats.files.external_files == 12
    assert stats.files.external_roots == ("vendor/theirs",)
    assert stats.files.external_recorded is True

Activity · 9 steps
lines 265–265db_path, root = _indexed(tmp_path, [*_ours(), *_theirs()], nested=("vendor/theirs",))lines 266–266stamp_external_provenance(db_path, root)lines 268–268stats = q.graph_stats(db_path)lines 270–270assert stats.files.indexed_files == 25, "the 12 vendored files are not this repo's"lines 271–271assert stats.files.files_with_symbols == 25lines 272–272assert stats.files.files_with_bodies == 25lines 273–273assert stats.files.external_files == 12lines 274–274assert stats.files.external_roots == ("vendor/theirs",)lines 275–275assert stats.files.external_recorded is True

test_list_files_returns_external_files_annotated_not_filtered

None test_external_provenance.test_list_files_returns_external_files_annotated_not_filtered(Path tmp_path)

list_files must LIST external files, annotated, never filtered out.

tests/test_external_provenance.py:304

Show on the map →

Called by none

Calls _indexed, _ours, _theirs, list_files, stamp_external_provenance

Checks

  • assert len(listed) == 37, "an external file is annotated, never withheld"
  • assert owners["vendor/theirs/lib_0.c"] == "vendor/theirs"
  • assert owners["src/mod_0.c"] == ""
  • assert q.list_files(db_path, "vendor/*"), "a glob over the submodule still answers"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_list_files_returns_external_files_annotated_not_filtered → stamp_external_provenance → external_roots → dependency_roots
REQ-DDB-INDEX-005measuredtest_list_files_returns_external_files_annotated_not_filtered → stamp_external_provenance
REQ-DDB-PIPE-014catalog
REQ-DDB-QUERY-001measuredtest_list_files_returns_external_files_annotated_not_filtered → list_files
REQ-DDB-QUERY-003measuredtest_list_files_returns_external_files_annotated_not_filtered → list_files → table_exists
Source · 22 lines
def test_list_files_returns_external_files_annotated_not_filtered(tmp_path: Path) -> None:
    """THE NON-FILTERING GUARANTEE. Every aggregate in this file reports first party
    by default; a query must not. An inventory that said "this repo contains 25 files"
    while the index holds 37 is precisely the failure the emptiness notes exist to
    prevent, and it is worse here than elsewhere because `list_files` is the tool a
    reader uses to find out what the index covers.

    @brief list_files includes external files and tags them.
    @version 1
    """
    db_path, root = _indexed(tmp_path, [*_ours(), *_theirs()], nested=("vendor/theirs",))
    stamp_external_provenance(db_path, root)

    listed = q.list_files(db_path)
    owners = {entry.path: entry.external_root for entry in listed}

    assert len(listed) == 37, "an external file is annotated, never withheld"
    assert owners["vendor/theirs/lib_0.c"] == "vendor/theirs"
    assert owners["src/mod_0.c"] == ""
    assert q.list_files(db_path, "vendor/*"), "a glob over the submodule still answers"

Activity · 8 steps
lines 314–314db_path, root = _indexed(tmp_path, [*_ours(), *_theirs()], nested=("vendor/theirs",))lines 315–315stamp_external_provenance(db_path, root)lines 317–317listed = q.list_files(db_path)lines 318–318owners = {entry.path: entry.external_root for entry in listed}lines 320–320assert len(listed) == 37, "an external file is annotated, never withheld"lines 321–321assert owners["vendor/theirs/lib_0.c"] == "vendor/theirs"lines 322–322assert owners["src/mod_0.c"] == ""lines 323–323assert q.list_files(db_path, "vendor/*"), "a glob over the submodule still answers"

test_only_a_directory_with_its_own_git_tree_is_external

None test_external_provenance.test_only_a_directory_with_its_own_git_tree_is_external(Path tmp_path)

A directory the repo declares as a submodule is detected; a copied-in one is not.

tests/test_external_provenance.py:139

Show on the map →

Called by none

Calls _indexed, _ours, _theirs, external_roots, stamp_external_provenance

Checks

  • assert external_roots(root) == ["vendor/theirs"]
  • assert rows["vendor/theirs/lib_0.c"] == "vendor/theirs"
  • assert rows["vendor/copied/header_only.h"] is None, ("a copied-in third-party file with no git tree of its own IS first party — " "this repo committed it and owns it, and no name matching may say otherwise" )
  • assert rows["src/mod_0.c"] is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_only_a_directory_with_its_own_git_tree_is_external → external_roots → dependency_roots
REQ-DDB-INDEX-005measuredtest_only_a_directory_with_its_own_git_tree_is_external → stamp_external_provenance
REQ-DDB-PIPE-014catalog
Source · 29 lines
def test_only_a_directory_with_its_own_git_tree_is_external(tmp_path: Path) -> None:
    """THE RULE, stated as a discrimination rather than as a match. Both directories
    below are named `vendor/...`; only one is a declared dependency.

    @brief External detection keys on the parent's gitlink, never on a directory name.
    @version 2
    """
    db_path, root = _indexed(
        tmp_path,
        [*_ours(), *_theirs(), ("vendor/copied/header_only.h", 4)],
        nested=("vendor/theirs",),
    )
    assert external_roots(root) == ["vendor/theirs"]

    stamp_external_provenance(db_path, root)
    conn = sqlite3.connect(str(db_path))
    try:
        rows = dict(conn.execute(f"SELECT name, {EXTERNAL_ROOT_COLUMN} FROM path").fetchall())
    finally:
        conn.close()

    assert rows["vendor/theirs/lib_0.c"] == "vendor/theirs"
    assert rows["vendor/copied/header_only.h"] is None, (
        "a copied-in third-party file with no git tree of its own IS first party — "
        "this repo committed it and owns it, and no name matching may say otherwise"
    )
    assert rows["src/mod_0.c"] is None

Activity · 9 steps
lines 146–150db_path, root = _indexed( tmp_path, [*_ours(), *_theirs(), ("vendor/copied/header_only.h", 4)], nested=("vendor/theirs",), )lines 151–151assert external_roots(root) == ["vendor/theirs"]lines 153–153stamp_external_provenance(db_path, root)lines 154–154conn = sqlite3.connect(str(db_path))lines 155–155trylines 156–156rows = dict(conn.execute(f"SELECT name, {EXTERNAL_ROOT_COLUMN} FROM path").fetchall())lines 160–160assert rows["vendor/theirs/lib_0.c"] == "vendor/theirs"lines 161–164assert rows["vendor/copied/header_only.h"] is None, ( "a copied-in third-party file with no git tree of its own IS first party — " "this repo committed it and owns it, and no name matching may say otherwise" )lines 165–165assert rows["src/mod_0.c"] is None