Skip to content

feat(cli): squirrel self disk --prune, to reclaim what old audits are holding - #259

Merged
nc9 merged 3 commits into
mainfrom
feat/self-disk-prune
Sep 8, 2026
Merged

nc9 merged 3 commits into
mainfrom
feat/self-disk-prune

Conversation

@nc9

@nc9 nc9 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

The reclaim half of squirrelscan/repo#1912, on top of #256. Explicit, user-triggered, and it decides nothing about retention.

The problem

A project keeps every audit it has ever run. Nothing retires a prior crawl's rows, so project.db grows by about one audit each time: 95 MB after one audit of a 1,000-page site, 189 MB after two, 215 MB after three. #256 made that visible; this makes it fixable.

What --prune does

squirrel self disk --prune --keep 3 --dry-run
squirrel self disk --prune --keep 3

Retires the audits beyond the newest --keep: their rule results, links, images, sitemaps and frontier, plus the page rows a newer audit already supersedes. Then rebuilds the file so the space returns to the filesystem.

Measured on two audits of a 60-page site, keeping one:

measurement before after
project.db 11.5 MB 5.7 MB
page rows for the retired crawl 60 0
next audit's pages fetched 0 of 60, all unchanged

That last row is the one that matters.

What it deliberately keeps

The failure worth avoiding is not "reclaims too little", it is taking something the next crawl needs and turning every re-audit into a cold crawl, which would cost users far more than the disk it saved. So:

  • The newest page record per url. The superseded-page predicate is getCachedPage's own ordering inverted, so what it deletes is exactly the rows that read can never return. A url whose only row belongs to a retired crawl keeps that row: it is still the freshest thing known about that page.
  • resource_sizes. A retired crawl's rows may still be the most recent sub-resource record for a (type, url) via the rules(crawl): sitemap lastmod drifts from the page's own dateModified #107 anti-join.
  • published_reports, which is not recomputable, and the crawls row, so the audit stays listed rather than the history silently shrinking.

Smart audits are unaffected either way: carried findings live in page_findings and site_pages, which are site-keyed rather than crawl-keyed.

Reclaimed audits say so (Nik's decision on the issue)

A nullable retired_at on crawls, migration 25. Without it the prune left the crawl row saying completed while its rule results were gone, so the report path rebuilt a confident empty report from the pages that survive. That reads as a clean audit: a wrong answer where the old behaviour was merely a large file, and the reason this PR stayed a draft.

retireCrawls stamps it inside the same transaction as the deletes, so a crash cannot leave data gone but the audit still renderable. The refusal is structural rather than a check per call site: the three CLI gates that tested isReportReadyStatus(crawl.status) now test isReportRenderable(crawl), covering report <id>, --diff and --regression-since together, and reconstructReport refuses independently underneath all of them. One definition of the sentence, so it reads the same wherever you meet it:

Audit data was reclaimed on 2026-09-08: 06148d76-6c48-465a-bc16-deb44144350d

report --list still shows the audit, marked retired, with the date its data went. Keeping the crawl row is what makes the history visible rather than silently shrinking.

The column goes on the reconcile list at the same time it goes in the migration. That is not belt-and-braces: five tables have been broken by a database stamped at or past a version whose migration a differently-numbered build skipped. Here the failure would be quieter and worse than those, because reads are SELECT * mapped by key — a missing column yields undefined rather than throwing, so every retired audit would silently read as not retired.

Verified end to end: pruned a real project, then report <id>, --diff and --regression-since each refused with that line while the audit that was kept rendered normally.

Second review round

Two more real defects, both fixed:

  • analyze re-scored reclaimed data and reported "Analysis complete". It keeps its own status gate, and retirement does not change the status, so analyze <id> re-ran the rules over the surviving pages; its automatic selector picked retired crawls too.
  • A prune committing mid-read escaped the refusal. The stamp was checked once, outside any read transaction, so another process could retire between that check and the page reads. It re-reads the stamp after every read now.

And two tests that were weaker than they looked: the migration assertion passed without the repair (a missing column does not throw on read), and the "same transaction" assertion held whether or not the two were separable. The first now asserts through the write that does fail loudly; the second rolls the transaction back.

Why it is awkward on purpose

--keep is required and has no default. Retiring a crawl makes its report unrenderable, and report --list, --diff and --regression-since <audit-id> all reach into that history, so the window is a decision for the person whose reports they are. Merging this must not make it for them.

The plan is printed and confirmed before anything is deleted, --dry-run stops after printing, and an audit never prunes anything. Automatic retention stays undecided and out of this PR: --keep is still required, and an audit still never prunes anything. The keep-last-3 recommendation and the numbers behind it are a decision block on the issue.

One trap, recorded

VACUUM alone made the file bigger: 189 MB to 239 MB. In WAL mode the rewrite lands in the write-ahead log, so the main file shrinks while the -wal beside it grows by more than was saved. PRAGMA wal_checkpoint(TRUNCATE) after the vacuum, and measuring after the connection closes, is what makes the saving real. In the benchmarks file so the next person does not rediscover it.

Tests

Storage: preview equals what the delete removes; the conditional-GET cache survives with the right etag; only superseded page rows go; a url seen by only the retired crawl keeps its row; the crawl row stays; retiring nothing is a no-op; vacuum leaves the data intact.

CLI: nothing is planned inside the window; the plan is ordered oldest first; planning deletes nothing; a missing database is not an error; the reclaim returns space and the count confirmed on is the count deleted.

End to end, outside the suite: two audits of the same 60-page site, prune, then a third audit that reported 60 unchanged and 0 fetched, with the kept report still rendering at score 47.

Docs updated. Benchmarks row added.

@nc9
nc9 marked this pull request as draft September 7, 2026 21:31
@nc9
nc9 force-pushed the feat/self-disk-prune branch from 386ece0 to 15dc16e Compare September 7, 2026 21:44
nc9 added a commit that referenced this pull request Sep 8, 2026
…scan skill, bump to 1.3 (#265)

Adds the disk usage and reclaim commands from #256 and #259 to the agent skill so agents know where ~/.squirrel growth comes from and how to reclaim it, and that --keep is the user's call. Skill version 1.2 -> 1.3.

Signed-off-by: Nik Cubrilovic <[email protected]>
Co-authored-by: Nik Cubrilovic <[email protected]>
nc9 pushed a commit that referenced this pull request Sep 8, 2026
…port #1912

Nik's decision on the issue: option 1, a nullable `retired_at` on `crawls`.

Without it, `--prune` left the crawl row saying `completed` while its rule
results were gone, so `reconstructReport` read zero checks and assembled a
confident report with no findings from the pages that survive. That reads as a
clean audit. It is a wrong answer where the old behaviour was merely a large
file, which is why #259 stayed a draft until this landed.

Migration 25 adds the column, and it goes on a reconcile list at the same time.
That guard is not belt-and-braces: `pages`, `sitemaps`, `robots_txt`, `links` and
`sitemap_url_statuses` have each been broken by a DB stamped at or past a version
whose migration a differently-numbered build had skipped, and here the casualty
would be every `listCrawls` SELECT — `report`, `report --list` and the prune
itself, not just the new field. Tested by seeding a database already stamped at
the current version and asserting the column appears on open.

`retireCrawls` stamps it inside the same transaction as the deletes, so a crash
cannot produce the one state worse than either end: data gone, audit still
renderable.

Refusal is structural rather than a check per call site. The three CLI gates that
tested `isReportReadyStatus(crawl.status)` now test `isReportRenderable(crawl)`,
which covers `report <id>`, `--diff` and `--regression-since` together and cannot
be missed by a fourth. `reconstructReport` refuses independently, underneath all
of them. Both say the same sentence, from one definition:

  Audit data was reclaimed on 2026-09-08: 06148d76-...

`report --list` still shows the audit, marked `retired`, with the date its data
went — keeping the crawl row is what makes the history visible instead of
silently shrinking.

Verified end to end: pruned a real project, then `report <id>`, `--diff` and
`--regression-since` each refused with that line while the audit that was kept
rendered normally.

Retention is still manual and `--keep` still required; the automatic window is
undecided and stays that way.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/self-disk-prune branch from 15dc16e to f6fa8f1 Compare September 8, 2026 00:52
@nc9
nc9 marked this pull request as ready for review September 8, 2026 00:57
Nik Cubrilovic added 3 commits September 8, 2026 11:05
… holding #1912

A project keeps every audit it has ever run: 95 MB after one audit of a
1,000-page site, 189 MB after two, 215 MB after three. #256 made that visible;
this makes it fixable, explicitly and only when asked.

`--prune --keep N` retires the audits beyond the newest N — their rule results,
sitemaps, page features and frontier, plus the page rows a newer audit already
supersedes — then rebuilds the file so the space returns to the filesystem.
Measured on two audits of a 60-page site, keeping one: 11.5 MB to 5.7 MB, and
the next audit reported all 60 pages unchanged with 0 fetched.

What it keeps is the part worth getting right, and an adversarial review moved
two of these from "kept by luck" to "kept on purpose":

- the newest page record per url, so an incremental re-crawl still gets its
  ETag instead of refetching the site. The predicate is `getCachedPage`'s own
  ordering inverted, so what goes is exactly what that read can never return.
- links, images and their appearances. `getLinksByPage` and `getImagesByPage`
  have no crawl_id filter and `reuseCachedPage` copies their result into the
  next crawl, so retiring them would leave a reused page with no links in the
  NEXT audit's report.
- `resource_sizes`, since a retired crawl's rows may still be the freshest
  sub-resource record anyone has, and `published_reports`, and the `crawls` row.

Guards, each with a test: a crawl that is not finished is refused inside
`retireCrawls` rather than only in the caller, so a prune racing a live audit
cannot delete its frontier; `--keep` is matched whole-string, because
`parseInt("1e3")` is 1 and `--keep 1e3 --yes` would otherwise have kept one
audit and deleted the rest; the plan is printed and confirmed first; `--dry-run`
stops after printing; and an audit never prunes anything.

VACUUM alone made the file BIGGER, 189 MB to 239 MB: in WAL mode the rewrite
lands in the write-ahead log, so the main file shrinks while the `-wal` beside
it grows by more than was saved. `PRAGMA wal_checkpoint(TRUNCATE)` after the
vacuum, and running `close()` rather than merely constructing its Effect, is
what makes the saving real.

Draft until one thing is decided: a retired crawl still says `completed`, so
`reconstructReport` will build a confident empty report from it. Making an audit
unrenderable needs retirement state the report path can see, which is a schema
change on a user's project.db. Options are on the issue.

Signed-off-by: Nik Cubrilovic <[email protected]>
…port #1912

Nik's decision on the issue: option 1, a nullable `retired_at` on `crawls`.

Without it, `--prune` left the crawl row saying `completed` while its rule
results were gone, so `reconstructReport` read zero checks and assembled a
confident report with no findings from the pages that survive. That reads as a
clean audit. It is a wrong answer where the old behaviour was merely a large
file, which is why #259 stayed a draft until this landed.

Migration 25 adds the column, and it goes on a reconcile list at the same time.
That guard is not belt-and-braces: `pages`, `sitemaps`, `robots_txt`, `links` and
`sitemap_url_statuses` have each been broken by a DB stamped at or past a version
whose migration a differently-numbered build had skipped, and here the casualty
would be every `listCrawls` SELECT — `report`, `report --list` and the prune
itself, not just the new field. Tested by seeding a database already stamped at
the current version and asserting the column appears on open.

`retireCrawls` stamps it inside the same transaction as the deletes, so a crash
cannot produce the one state worse than either end: data gone, audit still
renderable.

Refusal is structural rather than a check per call site. The three CLI gates that
tested `isReportReadyStatus(crawl.status)` now test `isReportRenderable(crawl)`,
which covers `report <id>`, `--diff` and `--regression-since` together and cannot
be missed by a fourth. `reconstructReport` refuses independently, underneath all
of them. Both say the same sentence, from one definition:

  Audit data was reclaimed on 2026-09-08: 06148d76-...

`report --list` still shows the audit, marked `retired`, with the date its data
went — keeping the crawl row is what makes the history visible instead of
silently shrinking.

Verified end to end: pruned a real project, then `report <id>`, `--diff` and
`--regression-since` each refused with that line while the audit that was kept
rendered normally.

Retention is still manual and `--keep` still required; the automatic window is
undecided and stays that way.

Signed-off-by: Nik Cubrilovic <[email protected]>
Review of the commit above found two, both real, both the case it was written to
prevent reaching:

- **`analyze` re-scored reclaimed data and called it a success.** Retirement
  leaves the status `completed`, and `analyze` has its own status-only gate, so
  `analyze <id>` re-ran the rules over whatever pages survived the prune and
  reported "Analysis complete" for a crawl most of whose data is gone. Its
  automatic selector picked retired crawls too. Both now test the stamp.

- **A prune committing mid-read escaped the refusal.** `reconstructReport`
  checked the stamp once, outside any read transaction, so another process could
  retire between that check and the page and rule-result reads — and the
  function would return a confident empty report assembled from pre-retirement
  metadata. It re-reads the stamp after every read now, before returning.

Two tests were weaker than they looked, and both are fixed rather than argued
with. The migration test's `listCrawls` assertion passed WITHOUT the repair,
because reads are `SELECT *` mapped by key: a missing column yields undefined
rather than throwing. It now asserts through the prune's UPDATE, which is the
part that fails loudly. And the "same transaction" test asserted only the
successful end state, which holds whether or not the two are separable; it now
rolls the transaction back and asserts neither the stamp nor the deletes
survived.

The comment on the reconcile list said a missing column makes every listCrawls
SELECT throw. It does not, and the truth is worse: every retired audit silently
reads as not retired and renders the empty report the column exists to prevent.
Corrected in place.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/self-disk-prune branch from ae16164 to 8b295ce Compare September 8, 2026 01:05
@nc9
nc9 merged commit bbb6cc1 into main Sep 8, 2026
13 checks passed
@nc9
nc9 deleted the feat/self-disk-prune branch September 8, 2026 01:12
nc9 added a commit that referenced this pull request Sep 8, 2026
…1912)

Re-auditing a project wrote a whole new crawl and retired nothing, so
project.db grew by about one audit every time: 95 MB per audit of a
1,000-page site, unbounded, with nothing saying so. #259 shipped the
manual reclaim; this is the automatic half Nik signed off on.

After a SUCCESSFUL audit, retire the audits outside `[storage]
keep_audits` (default 3, `0` or `false` disables) through the existing
retireCrawls path. Retired audits stay listed and refuse to open, as
#259 made them. One line on stderr when something goes, never stdout,
so `-f json` cannot be polluted.

Only a successful audit retires, and a failed one does not hold a place
in the window either. `crawls.status` reads `analyzed` for a run whose
report came out `failed` or `blocked`, so without migration 27 recording
what the report actually said, three good audits plus two days of a site
being down plus one recovery run would keep the two blocked runs and
delete all three audits from before the outage. That column also carries
a `building` sentinel between the analyzed transition and the report
being reconstructed, so a second audit of the same project cannot retire
a crawl whose own process is still reading it.

The freed space stays inside the file on purpose. At a window of three
an audit retires one audit's worth of rows every time, about a quarter
of the file, so a share-based VACUUM threshold would rewrite the whole
database after every audit forever. Left alone those pages are reused
and the file plateaus; the rewrite is reserved for more than one audit
going at once, which means a backlog or a lowered window.

Bounded per criterion 5, which needed three plan fixes:

- `pages` is keyed (crawl_id, normalized_url), so the superseded-pages
  predicate had no index for its lookup by url and re-read the whole
  table per candidate row: 11,779 ms at 40,000 rows, 61 ms with
  migration 26's idx_pages_url_recency. getCachedPage was scanning the
  same way on every incremental re-audit.
- The recency test `a > b OR (a = b AND c > d)` seeks to the url then
  walks every version of it; as a row-value comparison it range-seeks.
  Equivalence to the old spelling is asserted, ties included.
- The sweep of page rows an earlier retirement kept reads every row in
  `pages` when written as a join, and costs a probe per audit ever
  retired when driven by a list of retired crawl ids. Driven by the urls
  this crawl just wrote it is bounded by the audit, which is also the
  only set that can have changed. Two partial indexes on `crawls` keep
  the candidate query and the retired lookup off a table that only
  grows; both are created after the migrations rather than in SCHEMA,
  which runs first and would throw on a pre-25 database.

Measured, 40-page site, five audits, after a WAL checkpoint: 14.71 MB
unbounded against 11.85 MB at keep 3, with a sixth audit leaving it at
11.86. Every audit after the first fetched 0 of 40 pages, including the
ones that ran after data was retired, so retiring never costs a cold
re-crawl. The pass itself is 0.35 ms when there is nothing to retire
and 59 ms when one audit goes, at 1,000 pages an audit.

`self disk --prune` also had to change: with retention on it found
nothing to retire and would have left the file large, so it now offers
the rebuild on its own, counts kept audits as ones that can still be
opened, and asks "Retire them?" on the number of audits going rather
than the number of rows.

Two adversarial review rounds, thirteen findings, eleven fixed. The two
left are recorded in the PR.

Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 added a commit that referenced this pull request Sep 8, 2026
…1912) (#285)

* feat(cli): keep the last 3 audits of a project and retire the rest (#1912)

Re-auditing a project wrote a whole new crawl and retired nothing, so
project.db grew by about one audit every time: 95 MB per audit of a
1,000-page site, unbounded, with nothing saying so. #259 shipped the
manual reclaim; this is the automatic half Nik signed off on.

After a SUCCESSFUL audit, retire the audits outside `[storage]
keep_audits` (default 3, `0` or `false` disables) through the existing
retireCrawls path. Retired audits stay listed and refuse to open, as
#259 made them. One line on stderr when something goes, never stdout,
so `-f json` cannot be polluted.

Only a successful audit retires, and a failed one does not hold a place
in the window either. `crawls.status` reads `analyzed` for a run whose
report came out `failed` or `blocked`, so without migration 27 recording
what the report actually said, three good audits plus two days of a site
being down plus one recovery run would keep the two blocked runs and
delete all three audits from before the outage. That column also carries
a `building` sentinel between the analyzed transition and the report
being reconstructed, so a second audit of the same project cannot retire
a crawl whose own process is still reading it.

The freed space stays inside the file on purpose. At a window of three
an audit retires one audit's worth of rows every time, about a quarter
of the file, so a share-based VACUUM threshold would rewrite the whole
database after every audit forever. Left alone those pages are reused
and the file plateaus; the rewrite is reserved for more than one audit
going at once, which means a backlog or a lowered window.

Bounded per criterion 5, which needed three plan fixes:

- `pages` is keyed (crawl_id, normalized_url), so the superseded-pages
  predicate had no index for its lookup by url and re-read the whole
  table per candidate row: 11,779 ms at 40,000 rows, 61 ms with
  migration 26's idx_pages_url_recency. getCachedPage was scanning the
  same way on every incremental re-audit.
- The recency test `a > b OR (a = b AND c > d)` seeks to the url then
  walks every version of it; as a row-value comparison it range-seeks.
  Equivalence to the old spelling is asserted, ties included.
- The sweep of page rows an earlier retirement kept reads every row in
  `pages` when written as a join, and costs a probe per audit ever
  retired when driven by a list of retired crawl ids. Driven by the urls
  this crawl just wrote it is bounded by the audit, which is also the
  only set that can have changed. Two partial indexes on `crawls` keep
  the candidate query and the retired lookup off a table that only
  grows; both are created after the migrations rather than in SCHEMA,
  which runs first and would throw on a pre-25 database.

Measured, 40-page site, five audits, after a WAL checkpoint: 14.71 MB
unbounded against 11.85 MB at keep 3, with a sixth audit leaving it at
11.86. Every audit after the first fetched 0 of 40 pages, including the
ones that ran after data was retired, so retiring never costs a cold
re-crawl. The pass itself is 0.35 ms when there is nothing to retire
and 59 ms when one audit goes, at 1,000 pages an audit.

`self disk --prune` also had to change: with retention on it found
nothing to retire and would have left the file large, so it now offers
the rebuild on its own, counts kept audits as ones that can still be
opened, and asks "Retire them?" on the number of audits going rather
than the number of rows.

Two adversarial review rounds, thirteen findings, eleven fixed. The two
left are recorded in the PR.

Signed-off-by: Nik Cubrilovic <[email protected]>

* chore(cli): say when an audit's outcome could not be recorded on its crawl

A swallowed setReportStatus leaves the crawl reading `building`, which
retention treats as neither retirable nor countable, so it holds its
rows until someone prunes. Not worth failing a finished audit over, but
worth a line.

Signed-off-by: Nik Cubrilovic <[email protected]>

* docs(cli): the retention notice prints every time, only its hint is first-time

Signed-off-by: Nik Cubrilovic <[email protected]>

* docs(benchmarks): what migrations 26 and 27 cost a real 100 MB project.db

Signed-off-by: Nik Cubrilovic <[email protected]>

* docs(benchmarks): what the new pages index costs on the write side

Signed-off-by: Nik Cubrilovic <[email protected]>

* docs(changelog): a project keeps its last 3 audits

Under [Unreleased], not a new v0.0.92 heading: the release workflow
extracts the section matching the version being cut, so a v0.0.92
section holding only this line would ship as the whole release notes
and drop everything already sitting under [Unreleased].

Signed-off-by: Nik Cubrilovic <[email protected]>

* docs(changelog): note automatic audit retention in v0.0.92

Signed-off-by: Nik Cubrilovic <[email protected]>

---------

Signed-off-by: Nik Cubrilovic <[email protected]>
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.

1 participant