Skip to content

feat(audit): search-first discovery for large repos (per-lexicon content search with walk fallback) - #1971

Merged
lex00 merged 1 commit into
mainfrom
feat/520-search-first-fetch
Aug 26, 2026
Merged

lex00 merged 1 commit into
mainfrom
feat/520-search-first-fetch

Conversation

@lex00

@lex00 lex00 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Summary

fetchRepoFiles used to walk the entire repo tree, filter by extension (isCandidatePath), and download every candidate — thousands of files on a large monorepo, most yielding nothing, while CI/IaC files at non-canonical paths still went undetected. This adds a search-first discovery strategy for large repos, used alongside the existing walk for small ones.

Heuristic: when to switch strategies

LARGE_REPO_TREE_ENTRIES = 500 — a repo switches from the walk to search-first once its tree is bigger than that, measured off data a call already has to make:

  • GitHub / Forgejo: their git/trees?recursive=1 API returns the whole tree in one call regardless of repo size, so the entry count from that call (or GitHub's truncated flag) is a free signal.
  • GitLab: the recursive tree API lists all directories before any blobs on a large repo (audit/fetch: GitLab large-monorepo tree walk returns 0 files (directories dominate the 2000-entry page cap) #518), so it can't safely be used to size the repo. Instead, the first page of the root-level, non-recursive listing (already the first call the walk makes) stands in: a full 100-entry root page is treated as "large." Below that, the walk always runs — even with a token present — so small repos never pay for a search call.

Per-forge search

One row per content-detected lexicon (k8s, aws, azure, gcp, docker, helm) in a single table (CONTENT_SEARCH_TERMS) drives all three hosts — adding a lexicon here is the only change needed to search for it:

  • GitHub: GET /search/code?q=<term>+repo:<owner>/<repo> — subject to GitHub's ~10 req/min unauthenticated rate limit.
  • GitLab: GET /projects/:id/search?scope=blobs&search=<term> — requires a token even for public projects; GitLab's basic (non-Elasticsearch) blob search returns at most ~20 results per query and doesn't paginate past them, which the existing "short page = last page" loop already handles correctly.
  • Forgejo: GET /repos/:owner/:repo/search?q=<term>, tolerating both a bare array and Gitea's {ok, data} envelope. Forgejo's code-search availability/shape isn't consistently documented across versions, so this is explicitly best-effort.

Known-path lexicons bypass search entirely. GitHub/Forgejo Actions must live under .github/.forgejo workflows (platform-enforced), so those are filtered straight out of the tree already in hand — zero search calls. GitLab's include: can pull a CI file in from anywhere, so its canonical file additionally gets a stages: search term (unchanged from #519) to catch non-canonical includes (#520's original under-detection symptom).

Fallback behavior

Search is additive, never load-bearing. Any error, rate limit, or unsupported/unavailable search API (a 403/429/500, or a 404 meaning "this endpoint doesn't exist" — distinguished from a legitimate empty result set) is caught and the code falls back to:

Tests

Added to packages/core/src/audit/fetch.test.ts:

  • Strategy selection: small repo (even with a token) → walk; large repo → search, for github/gitlab/forgejo
  • Per-forge search request shapes (GitHub /search/code, GitLab /search?scope=blobs, Forgejo /repos/.../search)
  • Pagination past 100 results until a short page, for github and forgejo
  • Rate-limit/error fallback to the walk for all three hosts, including a Forgejo 404 ("no code search here") distinct from a real empty result
  • Known-path lexicons (CI) bypass search entirely — verified by asserting no search call references the workflow path

packages/core/src/audit/discover.ts: exported ciLexiconForPath (previously module-private) so fetch.ts can filter known-path matches straight from an already-fetched tree.

Test results

  • npx vitest run packages/core/src/audit — 12 files, 124 tests passed
  • npx vitest run packages/core — 327 files, 5027 tests passed
  • npx tsc --noEmit -p packages/core/tsconfig.json — clean
  • Full monorepo npx vitest run — 1075 passed / 3 skipped test files, 14291 passed / 32 skipped tests, 0 failures

Fixes #520

🤖 Generated with Claude Code

Above a size threshold, fetch.ts switches from a full tree walk +
extension filter to per-lexicon content search: known-path lexicons
(github, forgejo workflows; gitlab's canonical file) are kept straight
from the tree already in hand, while content-detected lexicons (k8s,
aws, azure, gcp, docker, helm) are found via each host's search API
using a characteristic content term. Any search failure, rate limit,
or unsupported API degrades to the walk rather than failing the audit.

Closes #520.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@lex00
lex00 merged commit a85418e into main Aug 26, 2026
7 of 9 checks passed
@lex00
lex00 deleted the feat/520-search-first-fetch branch August 26, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

audit/fetch: search-first discovery for large repos (replace extension-based candidate filter with per-lexicon content search)

1 participant