Skip to content

feat(crawler): retention and self disk --prune read the crawl lease #946 - #1023

Closed
nc9 wants to merge 2 commits into
mainfrom
feat/946-crawl-lease-use
Closed

nc9 wants to merge 2 commits into
mainfrom
feat/946-crawl-lease-use

Conversation

@nc9

@nc9 nc9 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes #946. PR 2 of 2: retirement and self disk --prune now READ the crawl lease that v0.0.127 (#1017) writes. This is code that deletes crawls, so it needs a data-safety review.

What changes

isCrawlAbandoned (new packages/crawler/src/storage/crawl-liveness.ts, pure apart from an injected clock, host name and pid probe) is the single verdict behind listAbandonedCrawls, listPossiblyLiveCrawls and the re-check INSIDE the retireCrawlsDetailed transaction (one shared read, unfinishedCrawlRows, so they cannot disagree). The pid probe is a synchronous syscall, legal in that transaction.

  1. No heartbeat, no owner (released lease or an older CLI), or an unreadable heartbeat: the fix(storage): retire crawls from killed audits; prune continues past a failing project #941 #945 rule, unchanged.
  2. Owner set, heartbeat under 5 min (including a negative age): LIVE, whatever fix(storage): retire crawls from killed audits; prune continues past a failing project #941 #945 says.
  3. Stale heartbeat, owner on THIS host with a positive-integer pid that is alive, heartbeat under 24 h: LIVE.
  4. Otherwise dead. A killed running crawl (what --resume continues) is abandoned after 24 h of silence, a crawled one after the stale window. stopped, paused and failed get the fix(storage): retire crawls from killed audits; prune continues past a failing project #941 #945 verdict only: a dead or unreleased lease never retires a resumable crawl sooner. fix(storage): retire crawls from killed audits; prune continues past a failing project #941 #945 still applies on top, so a dead lease is never kept longer than before.

Also: beat() takes over a same-host lease whose pid is gone at once (so --resume after a kill reclaims the lease immediately, not after 5 min); --prune --dry-run shows a held crawl's owner pid, host and heartbeat age; docs (self disk, keep_audits) and CHANGELOG updated.

Every way this could delete a live crawl

Failure mode Mitigation Test
Overlapping audits, later finishes first, #945 retires the earlier one fresh heartbeat or live pid beats #945 storage "overlapping audits" + e2e below
Event loop blocked or laptop asleep > 5 min (stale heartbeat) same-host live pid keeps it LIVE "stale heartbeat, same host, pid alive"
Pid reuse (dead owner's pid reassigned) reads alive pid trusted at most 24 h of heartbeat age; worst case kept longer, never deleted early "pid alive is trusted for at most 24 h"
Clock stepped back negative age counts as fresh; a heartbeat more than 24 h in the future is unreadable and falls back to #945 "negative age", "more than a day in the future", storage "clock step backwards"
Different host never pid-probed; live until the stale window "a different host is never pid-probed"
Unreadable owner (no pid, 0, negative, fractional, text) never probed (0 or a negative pid would signal process groups); live until the stale window "unreadable owner", isReadablePid
NULL lease (older CLI), released lease, garbage heartbeat exact #945 rule "no usable lease keeps the #945 rule", storage NULL-lease case
Resumed crawl re-claims the lease takeover on a dead same-host pid; verdict re-read, so a taken-over lease is live "takeover" tests, storage "resumed crawl", e2e resume
Heartbeat skipped on a busy db beat waits 1 s then skips; stale window is 300x that, same-host pid check covers the rest PR1 busy-skip test
Beat lands between listing and delete verdict recomputed inside the delete transaction, the crawl is skipped "re-checked inside the transaction"
Killed running crawl retired too early, breaking --resume 24 h of silence for running "killed RUNNING crawl waits 24 h"
stopped/paused/failed crawl with an unreleased lease (kill -9 right after the status write) retired after 5 min instead of 7 days those statuses ignore a dead lease, #945 verdict only truth table: 3 statuses x {dead same-host, other host, null owner}
Beat commits between the liveness read and the delete (SQLITE_BUSY_SNAPSHOT) retire runs as an IMMEDIATE transaction "retire transaction" test (fails with a deferred txn)
Sleeping laptop / SIGSTOP, stale heartbeat same-host live pid trusted up to 24 h; beat resumes within 15 s of wake pid-alive cases
Containers sharing a host name cannot see the host pid, read live as dead unsupported setup (WAL needs one kernel), noted in code and docs n/a
Older CLI retires a crawl cannot be prevented: it ignores leases; same as before this PR n/a

Verification

  • packages/crawler/tests/crawl-liveness.test.ts (36 tests): the verdict matrix plus storage cases above. Existing abandoned-crawls, retired-at-migration, retention, disk-prune, stop-interrupted-crawl tests pass unchanged. tsgo clean (crawler, apps/cli), make validate passes.
  • E2E in an isolated HOME (script not committed): audit A against a server that hangs every page after /, started first; its rows backdated 3 h so fix(storage): retire crawls from killed audits; prune continues past a failing project #941 #945 alone retires it; audit B (same project, other site) ran to completion with retention. A stayed running, not retired, owner kept; self disk --prune --dry-run listed it as skipped. After kill -9 of A and a 10 min old heartbeat, --dry-run lists it as abandoned (here fix(storage): retire crawls from killed audits; prune continues past a failing project #941 #945 also applies, since a newer audit finished).
  • E2E resume: crawl killed with kill -9 at 16 pages, audit --resume took the lease over within 3 s, finished all 80 pages, lease released. crawl then analyze on the newest crawl works.

Notes

@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Reviewed the diff. The logic is conservative where it matters, since any doubt resolves to "live". The liveness verdict is a pure function with an injectable environment and a thorough test matrix. The listing and the in-transaction re-check share one read (unfinishedCrawlRows), so they can't drift. I found no blocking issues.

  • (low) packages/crawler/src/storage/crawl-lease.ts, takeOverDeadOwner: it runs inside the try of the caller, so a throw from the extra SELECT is swallowed and returns false. That is the safe direction. A short comment saying so would help. The takeover is also only as good as the pid probe: a reused pid on this host reads as alive, so --resume falls back to waiting for the stale window. That is acceptable.
  • (low) packages/crawler/src/storage/sqlite.ts: retireCrawls now makes a process.kill(pid, 0) syscall per unfinished crawl inside the write transaction. The count is small in practice, so it's fine. If many abandoned crawls ever accumulate, consider probing before the transaction and only re-validating the heartbeat inside it.
  • (nit) sqlite.ts: the new UnfinishedCrawlRow interface sits between the "Schema version" comment block and export const SCHEMA_VERSION, which splits the comment from the constant it describes. Move the interface above that comment.
  • (nit) self.ts: the skipped-crawl message is built from two nested ternaries on heartbeatAt. Computing the lease and reason strings once above the console.log would be easier to read.
  • (nit) Test coverage looks good. A test that checks the takeOverDeadOwner CAS loses cleanly when another process takes the lease between the read and the update would round it out, if one isn't already there.

@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Review of #1023 (retention reads the crawl lease). I read the diff and did not run the tests. CI was still pending when I checked.

The design looks sound. Every doubtful case resolves to "live", the verdict is a pure function with an injectable environment, and the listing and the re-check inside the retire transaction share one read (unfinishedCrawlRows). run.immediate() is the right fix for the BUSY_SNAPSHOT race. The test matrix is thorough.

  • (medium) packages/crawler/src/storage/crawl-liveness.ts (isCrawlAbandoned, crawled branch): a crawled crawl with a stale heartbeat and a non-live owner is retired immediately, with no idle or newer-audit condition. The "other host counts as live until 5 minutes pass" rule means a crawl owned by a machine that shares the DB over a network filesystem, or by a container with a different hostname but the same mounted DB, loses protection after 5 minutes of silence. A crawled crawl is waiting for analyze, so this deletes user data. I know the docs call this out and that WAL over NFS is unsupported, but a check that this case is covered by a test would help. Consider also requiring the legacy idle window for non-same-host owners.
  • (low) crawl-lease.ts (takeOverDeadOwner): the SELECT and the UPDATE are not in one transaction. The UPDATE is a CAS on owner_pid and owner_host, so it is safe against a double takeover. It does not guard against the same pid being reused by a new process between the read and the write, but that window is negligible.
  • (low) self.ts: the new "held by a process that may still be running" text is shown whenever heartbeatAt is defined. A crawl can be skipped for other reasons, such as a stopped crawl that has not reached its idle time, while still carrying a stale lease. The message can then misattribute the reason.
  • (nit) sqlite.ts: the new UnfinishedCrawlRow interface sits between the "Schema version" comment and export const SCHEMA_VERSION, which splits the comment from its constant. Move it above the comment.
  • (nit) The CHANGELOG entry is a single very long paragraph. Splitting it into a short summary plus a few bullets would make it easier to read.

nc9 added a commit that referenced this pull request Oct 22, 2026
…nt, skip reasons, link phrases (#1034)

Batch of six reviewed PRs landed through one branch and one CI run:
- #1032 Bun 1.4.4 pin (CA blocklist for the eight compromised eMudhra intermediates, NSS 3.131.1 roots)
- #1030 nested CSS evidence for the style reader (repro from #1014 by @dbdefault)
- #1025 page-intrinsic skips use a not-applicable reason so priors resolve
- #1019 generic link phrases in ten more languages for link-text and anchor-text
- #1021 content/word-count skips noindex pages
- #1023 retirement of abandoned crawls uses the crawl lease
Plus a test fix for the residency probe on Bun 1.4.4 linux-x64.

Closes #840
Closes #946
Closes #1011
Closes #1013
Refs #841

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

nc9 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Landed through integration PR #1034 (squash merge 4dc22a9 on main), released in v0.0.128. Closing as superseded.

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.

Crawl lease (pid + heartbeat) so abandoned-crawl retirement knows liveness

1 participant