Skip to content

feat(cli): audit --max-duration stops at a wall-clock budget and writes a partial report #1271 - #1324

Closed
nc9 wants to merge 5 commits into
mainfrom
feat/max-duration-1271
Closed

nc9 wants to merge 5 commits into
mainfrom
feat/max-duration-1271

Conversation

@nc9

@nc9 nc9 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1271

squirrel audit --max-duration <seconds> (and [crawler] max_duration): a wall-clock budget for the whole audit. Contract from the decision on the issue: status partial, reason code max_duration, count of unfetched pages; stop launching new work at the budget, finish in-flight pages, exit within max(10% of the budget, 15 s); unset means no limit.

What happens at the limit

  • Crawl: stopped at the budget through the existing crawler.stop() (idle workers exit, in-flight fetches get the crawler's 5 s grace). A budget reached with zero pages stored fails with a clear message (exit 1).
  • Link and resource checks: given a budget that ends inside the margin (20% of it): resourceCheckBudgetMs and the external-link budgetMs, both existing engine knobs. The signed-in cloud prefetch is skipped when no budget is left.
  • Page rules: new deadlineAt on streamPageRules/runStreamingRules (40% of the margin): no further page starts its rules, the pass ends cleanly, pagesNotEvaluated is returned.
  • Whole rules phase: hard-cut at 60% of the margin (Effect.timeoutOption); the page results already went to the store batch by batch, so the report is written from the store.
  • Report: status: "partial", statusReasonCode: "max_duration", maxDuration: { seconds, unfetchedPages, pagesNotEvaluated, siteChecksCut }. Only when the budget actually cut something and the run was not already failed/blocked/partial. Persisted in scanScope.maxDuration, so squirrel report and report --input restore it. scanScope.stopReason = "time" is set too (the existing partial-scan hint fires).
  • Exit code: a partial report exits 0 (a --fail-on gate still decides). No new code, so nothing collides with fix(cli): offline level snapshot reports what ran; unreachable start URL writes a report, exit 5 #1275 #1269 #1297 (unreachable, exit 5) or feat(cli): audit where a start URL redirects, with consent for another site (#1281) #1282 (exit 4 redirected); partial already exists in AuditStatus. statusReasonCode widened to AuditStatusReasonCode = AuditFailureReasonCode | "max_duration"; max_duration is deliberately not a failure class, so classifyAuditFailureReasonText and the failure notices are untouched.

Known limit (said in the docs)

If a request made BEFORE the page-rule loop (the site-fetch stage: soft-404 and internal-link probes) is hanging when the hard stop comes, the whole rules phase is cut: site-wide checks and, in that case, the page checks too are missing (siteChecksCut: true). The e2e fixture hits exactly this (one URL never answers). Making that stage deadline-aware is a deeper engine change; happy to file a follow-up.

Tests

  • apps/cli/tests/cli/audit-max-duration-e2e.test.ts: the real CLI against a local site whose pages are slow and one URL never answers: partial report in JSON and text, exit 0, elapsed inside budget + 15 s; a budget that is never reached changes nothing; 0, -5, abc, 1e3, empty are rejected with exit 1.
  • apps/cli/tests/audit/max-duration.test.ts: margin math, deadline order, flag parser, reason sentence, saved-JSON round trip (and a malformed maxDuration dropped).
  • packages/audit-engine/tests/streaming-page-rules-deadline-1271.test.ts: no deadline, past deadline, deadline passing mid-run.
  • packages/report/tests/max-duration-partial-1271.test.ts: JSON, text, markdown and llm.
  • Also run: completion, audit-options, scan-scope-unfetched, classifier-parity, failure-reason renderers, waf-blocked, streaming golden and canonical tests; cli lint, format:check and tsgo; tsgo on core-contracts, report, audit-engine and config; make validate in docs/.
  • Not run: the full cli and rules suites (scripts/test-suites.ts), a signed-in or cloud-prefetch run with a budget, a large-site run where the report build itself is slow. Local Bun is 1.4.3, not the pinned 1.4.0.

Docs: docs/cli/audit.mdx (flag row, "Time limit" section with the margin, exit codes), docs/configuration/crawler.mdx, docs/configuration/output.mdx. Completions: bash, zsh, fish. Skill: needs a short note (agents should read status and maxDuration of a partial report); private issue filed.

Review round (fa39fec)

  • Slow robots/sitemaps: a new discoveryDeadlineAt crawler option (30% of the budget) cuts the root-probe preamble and the sitemap walk; the reviewer fixture with hung robots, sitemaps and llms.txt now completes 40 pages in 14 s at a 10 s budget (was 67 s and exit 1).
  • Hung seed: the budget now counts from the command's start (maxDurationStartedAt) and the reachability probe is cut to what is left; a black-hole seed at a 10 s budget fails at 11 s with "the time limit (--max-duration) was reached before the site answered" (was 31 s). Docs sentence fixed.
  • 30 MB page: profiled with the reviewer's huge fixture at 15 s: crawl 13.5 s, rules 37.6 s, 53 s wall. The time is one synchronous parse and rule run on the huge page, which timeoutOption cannot preempt. Not fixed; documented under "Limits" (the margin covers waiting, not computation). A size cap for budgeted runs would silently skip real pages, so I left it as a decision for you.
  • Timer overflow: the flag and [crawler] max_duration are capped at 604800 s (7 days) with a clear error; createDurationBudget clamps defensively.
  • MCP: audit_website takes maxDuration (same cap), documented in docs/cli/mcp.mdx, with a test.
  • A --max-duration run that cuts the resource checks stores the engine's crawl/asset-fetch-degraded info note; the CLI no longer prints "Rule not found in registry" for it.
  • Real-CLI e2e tests added for slow discovery, black-hole seed and overflow (bounded runtime). Private API: published partial reports lose statusReasonCode/maxDuration to zod stripping; needs the private change at the gitlink bump (not filed yet, say if you want an issue).

Delta review round (rebased onto v0.0.143)

  • A budget spent in the crawl preamble no longer lets the crawl start: stop() called before the crawl loop existed was lost when the run set isRunning = true. A seed that sends headers and then a never-ending body now exits at the budget plus the seed probes (budget 8: exit 1 at 10 s, "no pages collected"; it hung to the watchdog before).
  • The seed-redirect probe is inside the budget (detectRedirects(url, { deadlineAt })): budget 3 exits at 4 s (was 10 s).
  • Budget-unreachable reason classifies as timeout; both unreachable paths use redactUrl / redactUrlsInText (the merge kept chore(release): v0.0.143 #1341's redaction); a real-CLI test with user:pass@host/?token=#frag asserts none of it appears in output for the budget and the non-budget case.
  • Resource checks that run out of the budget are named: maxDuration.resourceChecksCut and the reason sentence (real-CLI test with 60 slow stylesheets). XML and HTML carry the coverage notice only; the docs say so.
  • Merge notes: audit-tools keeps main's auditableUrl gate plus startedAt; completion keeps --no-store-scripts and adds --max-duration to bash, zsh and fish. --include-info (feat(report): infoChecks in json and llm; rule-timed-out in skippedChecks #669 #518 #1344) is not on main yet, so the union is yours at that merge.

@nc9

nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up for the known limit (hung request in the pre-page-rule stage): #1327

@nc9

nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up for the single-large-page overrun (parse blocks the event loop in the crawl and in the rules phase): #1332

@nc9
nc9 force-pushed the feat/max-duration-1271 branch 2 times, most recently from 8bf17de to 4a133bd Compare October 30, 2026 07:18
nc9 added 4 commits October 30, 2026 20:26
…overy, 7-day ceiling, MCP maxDuration #1271

Signed-off-by: Nik Cubrilovic <[email protected]>
…nfig and degraded-note tests #1271

Signed-off-by: Nik Cubrilovic <[email protected]>
…probe in budget, timeout class, resource checks named, redacted unreachable #1271

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/max-duration-1271 branch 2 times, most recently from 7d8a457 to 27dc982 Compare October 30, 2026 10:03
@nc9

nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Landed in v0.0.144 as 7e54d41d cee27809 9988f023 6f0715e8 58aa1138 via #1356. Release: https://github.com/squirrelscan/squirrelscan/releases/tag/v0.0.144. Closing this original.

nik

@nc9 nc9 closed this Oct 30, 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.

audit --max-duration: a whole-audit time budget that writes a partial report instead of being killed

1 participant