wigolo icon indicating copy to clipboard operation
wigolo copied to clipboard

fix(fetch): unify SPA settle into one bounded shared path + content_completeness signal

Open KnockOutEZ opened this issue 4 months ago • 1 comments

Why

The browser tier's "is this SPA done rendering?" logic had been point-patched for ~3 months and kept regressing. The causes were structural, not a single bug:

  1. Two duplicate settle implementations — playwright-tier.ts and browser-pool.ts each hand-rolled goto → networkidle → hydration-probe waits independently, and had drifted apart. Every fix landed in one path; the other resurfaced the bug later.
  2. Two opposing failure modes — shell-as-content (probe fires early, nav shell leaks as content) vs never-settles (networkidle never fires, waits burn the full budget → hang). Tightening one regressed the other, so fixes oscillated.
  3. Selector-registry drift — hydration detection was a hardcoded framework-selector list that the web changes underneath.
  4. No standing regression corpus — fixes were validated against whatever sites were in a given benchmark round, so regressions only surfaced months later by hand.

What changed

  • One shared settlePage() (src/fetch/settle.ts) consumed by both browser paths. Single bounded deadline (POST_GOTO_CAP_MS), so total post-goto time can't blow up. Hybrid gate: the existing hydration probe is the zero-latency fast-path on recognized content; a new content-stability poller (content-outside-chrome stopped growing for N ticks) is the universal fallback that generalizes past the selector registry. The old app-shell escalation re-poll is deleted — content still growing is the escalation signal.
  • content_completeness signal on every browser capture: { level: full | partial | shell, reason, settled_by }. Dissolves the strict-vs-loose tension — a nav shell comes back labeled shell/nav_shell instead of either silently leaking as content or hanging. Surfaced on the fetch and crawl MCP responses.
  • Cache honors it (migration 009): shell captures are treated stale on the next read → one live refetch, never a loop; cache-only mode still serves the labeled shell.
  • Research excludes shell sources — a captured shell never becomes a cited research source.
  • A permanent regression corpus (tests/integration/fetch/spa-settle-corpus/) — a local server serving seven synthetic failure classes (delayed-mount SPA, nav-shell-forever, never-networkidle, instant static, challenge shell, code-heavy docs, ticker), driving the real browser and asserting exact completeness labels + wall-clock bounds. This is the standing guard: a fix for one failure mode that regresses the other now fails immediately.

Notes

  • Keyless-local behavior unchanged; no new config knobs (cadence/epsilon are constants).
  • The hydration render gate learned to count data-table rows as content, but only alongside co-present prose — so a nav-as-table or skeleton-table shell still reads as a shell (guarded by must-not-fire tests + a TS/injected-string lockstep test).
  • Test baseline on the dev machine: 4 LLM-provider suites fail due to a stale local API key (pre-existing, unrelated); everything else green.

Summary by CodeRabbit

  • New Features

    • Added content completeness reporting for fetched, cached, and crawled pages, identifying full, partial, or shell content.
    • Improved browser rendering waits to capture dynamically loaded content, including data tables.
    • Research results now exclude shell-only captures and record them as rejected sources.
  • Bug Fixes

    • Shell pages in the cache are refreshed when live fetching is available, while cache-only mode remains unchanged.
    • Added safeguards for challenge pages and pages that never finish loading.
  • Documentation

    • Updated fetch and crawl tool output descriptions with content completeness details.

KnockOutEZ avatar Jul 18 '26 12:07 KnockOutEZ

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 09b957b1-2fae-4e0b-8f74-0f98c02d6859

📥 Commits

Reviewing files that changed from the base of the PR and between 50757ee6b15926221026435ed34ec0d09c57d532 and 9aba9bb50fcb9f8265d036a9d782d751a2a23c39.

📒 Files selected for processing (10)
  • src/fetch/hydration-probe.ts
  • src/fetch/settle.ts
  • src/types.ts
  • tests/integration/fetch-cache-completeness-gate.test.ts
  • tests/integration/fetch/spa-settle-corpus.test.ts
  • tests/integration/fetch/spa-settle-corpus/fixtures.ts
  • tests/integration/fetch/spa-settle-corpus/server.ts
  • tests/unit/fetch/hydration-probe.test.ts
  • tests/unit/fetch/settle.test.ts
  • tests/unit/research/pipeline-shell-completeness.test.ts
🚧 Files skipped from review as they are similar to previous changes (8)
  • tests/unit/research/pipeline-shell-completeness.test.ts
  • tests/integration/fetch/spa-settle-corpus/server.ts
  • tests/integration/fetch/spa-settle-corpus/fixtures.ts
  • tests/integration/fetch/spa-settle-corpus.test.ts
  • src/fetch/settle.ts
  • src/types.ts
  • src/fetch/hydration-probe.ts
  • tests/unit/fetch/settle.test.ts

📝 Walkthrough

Walkthrough

The change adds browser content-completeness classification, shared page settling, cache persistence, shell-aware refetching, and propagation through fetch, crawl, and research outputs. Tests cover settlement behavior, browser flows, cache migration, cache gating, and downstream filtering.

Changes

Content completeness

Layer / File(s) Summary
DOM classification and settling contracts
src/types.ts, src/fetch/hydration-probe.ts, src/fetch/settle.ts, tests/unit/fetch/*
Adds DOM verdicts, table-row hydration detection, bounded settling with stability and abort handling, and completeness mapping.
Browser fetch integration
src/fetch/browser-pool.ts, src/fetch/playwright-tier.ts, src/fetch/router.ts, tests/integration/fetch/spa-settle-corpus*, tests/unit/fetch/*
Routes browser captures through shared settling, labels challenge shells, returns completeness, and validates SPA scenarios with browser integration tests.
Cache schema and fetch behavior
src/cache/*, src/tools/fetch.ts, tests/integration/fetch-cache-completeness-gate.test.ts, tests/unit/cache/*, tests/unit/tools/fetch.test.ts
Adds idempotent cache columns, persists completeness labels, preserves legacy behavior, and refetches shell entries outside cache-only mode.
Crawl and research result propagation
src/crawl/crawler.ts, src/research/pipeline.ts, src/instructions.ts, tests/unit/crawl/*, tests/unit/research/*
Propagates completeness to crawl results, rejects shell research sources, records shell-content, and documents the new output field.

Estimated code review effort: 5 (Critical) | ~90+ minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant SmartRouter
  participant BrowserPool
  participant Cache
  participant ResearchPipeline
  Client->>SmartRouter: fetch URL
  SmartRouter->>BrowserPool: browser capture
  BrowserPool-->>SmartRouter: content and completeness
  SmartRouter->>Cache: persist content completeness
  SmartRouter-->>Client: fetch result with content_completeness
  ResearchPipeline->>SmartRouter: fetch candidate sources
  SmartRouter-->>ResearchPipeline: sources with completeness
  ResearchPipeline-->>Client: exclude shell sources and record rejection
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 46.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fetch change: shared bounded SPA settling plus the new content_completeness signal.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • [ ] Create stacked PR
  • [ ] Commit on current branch
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Commit unit tests in branch feat/spa-settle-overhaul

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 18 '26 12:07 coderabbitai[bot]