Skip to content

feat(crawler): write a crawl lease (owner pid, host, heartbeat) for the whole command #946 - #1017

Merged
nc9 merged 2 commits into
mainfrom
feat/946-crawl-lease-write
Oct 22, 2026
Merged

nc9 merged 2 commits into
mainfrom
feat/946-crawl-lease-write

Conversation

@nc9

@nc9 nc9 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Refs #946 (PR 1 of 2: lease WRITE only; no change to retirement or self disk --prune).

What

  • Migration 35: nullable owner_pid, owner_host, heartbeat_at on crawls, also on the crawls column reconcile list and in the fresh-db schema. Old rows and old CLIs are unaffected (NULL lease).
  • startCrawlLease (packages/crawler): beats every 15 s on a SECOND connection with busy_timeout 1 s, skips a beat when busy, unref'd timer, re-asserts ownership each beat, release clears owner (only if still ours) and keeps the heartbeat. Polls every 1 s until the crawl row exists, so the first beat lands quickly.
  • audit, crawl and analyze hold it for the whole command (crawl, --resume, analyzed/building window) and release in finally. The signal handler (stopInterruptedCrawls) clears it too, since finally does not run there. kill -9 leaves it, by design.
  • Constants (15 s heartbeat, 5 min stale, 24 h pid trust, 1 s busy) in storage/types.ts; stale and pid-trust are unused until PR 2.

Tests

  • packages/crawler/tests/crawl-lease.test.ts (13): fresh db, v34-era db opens with no manual step and keeps rows, reconcile repairs a db stamped current without the columns, rollback (old-CLI-style insert/update on a db with the columns), lease written, heartbeat refreshes, waits for the crawl row, release clears/idempotent, cleared on the error path, release leaves another process's lease, signal-path clear, second-connection busy skip (about 1 s, no throw), no-op on :memory:/missing file (creates nothing).
  • apps/cli/tests/audit/stop-interrupted-crawl.test.ts: lease dropped on the signal path, only this pid's.
  • Existing migration/column tests and make validate in docs/ pass. tsgo clean in packages/crawler and apps/cli.

Perf

1,000-page synthetic crawl written through writeCrawlToStorage, bun 1.4.3, isolated dir, 7 runs each, median: no lease 2924 ms, 15 s heartbeat 2731 ms, 5 ms heartbeat (3,000x the design rate) 2960 ms. All within run-to-run noise (about 2.5 to 3.4 s). A real squirrel crawl of 1,000 synthetic pages (isolated HOME) took 5.3 s, wrote the heartbeat and cleared the owner on exit.

Not covered

Overlapping-audit, live/dead predicate and self disk listing tests are PR 2 (nothing reads the lease here). Skill (squirrelscan/skills): no change needed.

@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Review of #1017 (crawl lease writer, schema 35). I read the diff and did not run the tests.

The change is additive, nullable and write-only. The heartbeat runs on its own connection, the timer is unref'd, and release only clears rows this process still owns. The migration is mirrored in CRAWLS_ALTER_COLUMNS and the base schema. I found nothing that should block merge.

  • (low) packages/crawler/src/storage/crawl-lease.ts: clearCrawlLease is exported and tested, but the signal path in stop-interrupted-crawl.ts runs its own inline UPDATE instead. Either call the helper there or drop the export. Two copies of the clear logic can drift. The inline one also matches on pid only, not host.
  • (low) crawl-lease.ts: every beat is a synchronous bun:sqlite call with a 1s busy timeout. Under a long foreign write it can block the event loop for up to 1s every 15s. The comment says it avoids stalling the loop; it caps the stall rather than removing it. Worth rewording the comment.
  • (low) crawl-lease.ts beat: it re-asserts ownership on every beat without checking the current owner. If another process resumes the same crawl, the two will overwrite each other's pid. This is harmless while nothing reads the lease, but the reader PR should account for it.
  • (nit) CRAWL_LEASE_STALE_MS and CRAWL_LEASE_PID_TRUST_MS in types.ts are unused for now. Fine as groundwork, but the reader PR should be the one to add them.
  • (nit) startLeaseForStorage reads dbPath through an as { dbPath?: unknown } cast. An optional dbPath on the CrawlStorage interface would be cleaner.

Test coverage looks good: the writer, the migration, and the pid-scoped clear on the interrupt path are all tested.

@nc9
nc9 force-pushed the feat/946-crawl-lease-write branch from ff133bc to 1de729d Compare October 22, 2026 06:23
@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Reviewed the diff. The lease writer is write-only, additive and best-effort, and I found no blocking problems.

  • (low) packages/crawler/src/storage/types.ts: CRAWL_LEASE_PID_TRUST_MS is exported but nothing in this PR uses it. Either drop it until the reader PR lands or say in a comment that it is reserved.
  • (low) crawl-lease.ts beat: heartbeat_at comes from each host's local Date.now(). The stale check (heartbeat_at < now - 5min) therefore assumes clocks are roughly in sync when two hosts share a DB, for example over a network filesystem. That is probably fine for local SQLite. The reader PR should handle it, or note it as an assumption.
  • (nit) startLeaseForStorage finds the DB path with (storage as { dbPath?: unknown }).dbPath. Adding dbPath? as an optional member of the CrawlStorage interface would avoid the cast.
  • (nit) In release(), a busy-skipped initial beat means held stays empty and nothing is cleared. That is harmless, because the heartbeat later takes the lease and the stale window covers the rest. A one-line comment would save the next reader from wondering.
  • (nit) The tests cover the migration, the compare-and-set, and the signal path. A test that release() followed by a late timer tick does not re-assert the lease would also pin the released guard directly. The existing signal-path test covers most of that already.

Nothing here needs to block merge.

@nc9
nc9 merged commit 807b356 into main Oct 22, 2026
15 checks passed
@nc9
nc9 deleted the feat/946-crawl-lease-write branch October 22, 2026 06:37
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