Skip to content

feat(cli): opt-in storage of fetched first-party scripts in the content store #1255 - #1267

Merged
nc9 merged 4 commits into
mainfrom
feat/store-scripts-1255
Oct 29, 2026
Merged

nc9 merged 4 commits into
mainfrom
feat/store-scripts-1255

Conversation

@nc9

@nc9 nc9 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1255

What

Opt-in [storage] store_scripts = true (and squirrel audit --store-scripts for one run) keeps the first-party scripts an audit fetched in the content store, for corpus and research runs that re-analyse a crawl later.

  • Retrievable by URL, linked from the audit: new crawl_scripts table in the project DB (created with the schema, no migration): script URL, content hash, size, status, skip reason, and the page URLs that load it (getCrawlScripts(crawl, { pageUrl }) rebuilds "page + its bundles"; getCrawlScriptBody(crawl, url) reads a body).
  • Deduplicated by SHA-256: identical bundles across pages, audits and sites are one content-store row.
  • Caps: store_scripts_max_bytes (default 2 MB per file; over it the script is recorded as skipped / too-large with its size and its body is never hashed or compressed) and store_scripts_total_bytes (default a quarter of content_store_max_bytes; distinct bytes per audit, a shared bundle counts once; past it skipped / total-cap). Both take numbers or size strings.
  • Retention: bodies are normal content-store rows (LRU under content_store_max_bytes); retainPageBodies touches them with the HTML; crawl_scripts is in RETIREABLE_TABLES, so keep_audits retention and self disk --prune retire the index with the audit.
  • Off by default: no index rows, no JS in the content store, no extra work. Uses only the scripts the resource phase already fetched (plus preloaded chunks): no extra request. Failure never fails the audit (warns).
  • Completions (bash, zsh, fish) updated for --store-scripts.

Evidence

  • packages/crawler/tests/store-scripts-1255.test.ts (11): retrieval by URL per crawl, dedupe (one put, one row, across crawls too), page link, per-file cap (size kept, body never written), total cap with shared-bundle accounting, failed or bodiless scripts leave no row, store error skips one script, no store = nothing, retention touches the hashes, and 20 distinct 5 MB bundles plus 20 copies (200 MB of input) stop at the cap with 3 writes, no copies written, heap growth under 64 MB.
  • apps/cli/tests/audit/store-scripts-1255.test.ts (7): real squirrel audit --offline --http against a fixture site: two pages, a shared bundle under two URLs, an own bundle, an oversized one. Asserts the index, one content-store row for the shared bundle, gunzipped bodies equal the served bytes, the oversized one skipped with its size, the total cap, the config key without the flag, and OFF by default (no rows, no JS in the store).
  • packages/config/tests/store-scripts-config-1255.test.ts (3).
  • tsgo --noEmit clean for config, utils, crawler, cli; CLI lint and format:check clean; scripts/test-suites.ts --check; docs make validate; detect-secrets; content-store, retention, disk and home-guard tests still pass.

Docs

docs/configuration/storage.mdx (option, the three keys, disk cost, how it interplays with keep_audits and content_store_max_bytes, privacy: stays local), docs/cli/audit.mdx (flag), CHANGELOG under Unreleased.

Skill

The squirrelscan agent skill (squirrelscan/skills) needs no change.

Known, deferred

  • No per-run --no-store-scripts (config on, flag off).
  • store_scripts_total_bytes default is a quarter of the compressed store cap but counts decoded text (documented).
  • content-store.db and project DBs are 0644, like the existing HTML store; bundles can contain keys.
  • The empty crawl_scripts tables are created in every project DB even with the option off.
  • A chunked script over 5 MB is recorded at 5 MB plus one, not its true size (documented as a lower bound).
  • With the option on the fetch is bounded to 256 MB of held script text and 120 s; scripts left out are recorded memory-cap / time-budget.

@nc9
nc9 force-pushed the feat/store-scripts-1255 branch from eebe8a2 to 781bdbd Compare October 29, 2026 07:54
@nc9
nc9 marked this pull request as ready for review October 29, 2026 08:15
@claude

claude Bot commented Oct 29, 2026

Copy link
Copy Markdown

Review of #1267 (opt-in store_scripts). I read the diff for the CLI, engine, fetcher and storage changes. I did not run the tests or CI.

  • (medium) packages/audit-engine/src/script-fetcher.ts: heldBytes += result.content?.length ?? 0 counts UTF-16 code units, not bytes. The storage side uses Buffer.byteLength. For non-ASCII bundles the memory cap undercounts by up to about 3x, so use Buffer.byteLength or document the approximation.
  • (medium) sqlite.ts storeCrawlScripts: store.put (gzip plus write) and the SHA-256 hashing run synchronously in a loop over up to 500 scripts of up to 2 MB each, inside one transaction. This blocks the event loop, which AGENTS.md warns starves setTimeout deadlines. Yield on a time budget between scripts. Keeping the transaction open across yields also needs a look.
  • (low) sqlite.ts: the store.put failure path assigns let stored = hash before the try, which is dead. Also catch {} swallows the error with no log, so the store-error reason can't be diagnosed.
  • (low) sqlite.ts: getCrawlScripts calls JSON.parse(row.source_pages) with no guard, and the existence check runs once per row. That is fine for the data this PR writes, but one corrupt row would fail the whole read. Please wrap it.
  • (low) getCrawlScriptBody matches url = ? OR final_url = ? with .get. Two scripts that redirect to the same final URL give a nondeterministic row, though the content hash would likely be identical. The crawl_scripts table also has no index on final_url or content_hash.
  • (low) adapter.ts has the clamp expression Math.min(ceiling, Math.max(1, ...)) duplicated from storedScriptFetchLimit in the CLI. The two could drift. Consider sharing one helper in utils. The new lines in adapter.ts and sqlite.ts also look unformatted (long lines), which may not matter for these packages per AGENTS.md.
  • (low) controllers/audit.ts: storing is wired only at one point in runAudit. Please confirm that the resume and incremental paths either reach it or are intentionally excluded, and say so in the docs.
  • (nit) crawl_scripts has a FOREIGN KEY to crawls. Please confirm every crawl-deletion path outside RETIREABLE_TABLES also clears it (I could not verify this).
  • Test coverage is good: crawler, engine bounds, config and CLI tests, plus docs and completions. Please confirm that the isolateSquirrelHome guard test and docs-sync/scripts/test-suites.ts --check pass.

I found no security problems. Script bodies are only stored and hashed, never executed or rendered. Skipped scripts and sizes are bounded by config caps.

@nc9
nc9 merged commit 660dcfc into main Oct 29, 2026
16 checks passed
@nc9
nc9 deleted the feat/store-scripts-1255 branch October 29, 2026 10:27
@nc9 nc9 mentioned this pull request Oct 29, 2026
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.

Opt-in storage of fetched JS bundles in the content store for corpus and research runs

1 participant