SPB Git

Repository Public

ARCHITECTURE.md 5.8 KB

# SPIDER architecture

Version 0.1 — matches the code in this repository. Decisions are in docs/DECISIONS/.

flowchart TD
  C[CLI / SDK / Dashboard] -->|/v1, API key or session| G[Gateway  src/spider/api]
  G --> O[Orchestrator  orchestrator.py + scheduler.py]
  O <--> PG[(PostgreSQL: jobs, frontier, tasks, leases, evidence, search)]
  G <--> ST[(Object store: content-addressed blobs)]
  W1[HTTP worker] -->|/internal/v1 leases, uploads, acks| G
  W2[Browser worker + egress proxy] --> G
  W3[macOS worker: Apple Vision OCR] --> G
  W4[Apple Silicon worker: MLX vision] --> G
  W1 -->|SSRF guard| WEB((Web))
  W2 -->|egress proxy = SSRF guard| WEB

# Planes

Plane Process Holds Talks to
Control spider serve (gateway + orchestrator + maintenance loop) DB credentials, signing secret, blob store PostgreSQL, clients, workers
Data spider worker --capabilities … a worker key only gateway /internal/v1, the Web through the guard

Workers never see the database. The gateway never fetches a source page (the only outbound calls of the control plane are signed webhooks, through the same SSRF guard).

# Modules (src/spider)

Module Role
contracts.py v1 request models and worker payloads (pydantic, unknown fields rejected)
netguard.py SSRF rules, pinned-address resolver, loopback egress proxy for browsers
canonical.py URL canonicalisation, scope allowlist, trap heuristics, SimHash
robots.py RFC 9309 parser and matcher
engine/http.py guarded HTTP engine: per-hop validation, bounded streaming, conditional requests, MIME sniffing, sitemaps
refusal.py, browser/refusal.py access-denial recognition (never circumvention)
browser/ browser pool, stability, snapshot, capture (viewport, full page, tiles, mosaic, elements), state exploration, replay
extract/ HTML, Markdown, structured data, tables, PDF, media, label/value pairs, schema candidates
ocr/ + services/ocr-worker Apple Vision worker (Swift) and its client, tile geometry, CER/WER
vision/ local MLX model registry, warm runner, OCR-attestation guard
fusion.py evidence reconciliation by dependency group, conflicts, calibration measures
pipeline.py worker-side page processing: artifacts, links, text, record
orchestrator.py jobs, frontier admission, leases with fencing tokens, ingestion, finalisation
scheduler.py lease reaper, deadlines, monitors, change events, webhooks, garbage collection
diffing.py text / layout / field / pixel comparison
store/objects.py content-addressed blob store
api/ gateway: public /v1, admin, internal worker API
worker.py, cli.py worker runtime and the spider command

# Life of a crawl

  1. POST /v1/crawl → request validated (mandatory policies cannot be disabled, budgets bounded), seeds canonicalised and checked by the SSRF guard, source kill switches and suspensions checked, job stored (idempotency key unique per tenant). Seeds are admitted to the frontier; a sitemap task is queued for robots.txt → sitemaps.
  2. A worker heartbeats and asks for leases. In one transaction the orchestrator picks runnable tasks (SKIP LOCKED), keeps at most one per host, checks the host is free (delay elapsed, nothing in flight, not suspended), marks it busy and issues a lease with a new fencing token.
  3. The worker fetches through the guard: scheme, port and address rules, robots.txt, scope re-checked on every redirect hop, body capped after decompression. For a browser task it then renders the page in a fresh context whose only route out is the egress proxy, captures, explores read-only states, and runs extraction, OCR, optional vision and fusion locally.
  4. Artifacts are uploaded by hash (the gateway verifies the hash), then the task is completed. The completion transaction checks the fencing token, stores snapshot / artifacts / states / record / evidence / search document, updates the URL, admits the links found (scope, depth, trap and pattern budgets, page budget), applies host feedback (delay, 429 back-off, denial counters, suspension) and finalises the job when nothing is left.
  5. Clients read progress (GET /v1/jobs/{id}, SSE events), partial results at any time, the graph and the coverage report.

# Delivery guarantees

At-least-once. A lost worker's lease expires; the task is re-queued with a higher fencing token, so a late result from the first worker is refused (409) and cannot overwrite the second. Ingestion is idempotent (unique (job_id, url_id, engine, profile) on snapshots). Attempts are bounded; exhausted tasks are dead-lettered and the job ends partial with the reason. No exactly-once claim is made.

# Data model

Tables: tenants, api_keys, sessions, sites, policies, host_state, jobs, urls, crawl_edges, tasks, job_attempts, workers, snapshots, page_states, blobs, artifacts, extracted_records, field_evidence, schemas, connectors, schedules, change_events, audit_events, usage_counters, job_events, webhook_deliveries, search_documents (migrations/0001_init.sql). Every tenant-owned table carries tenant_id.

# Modes

  • Single node: spider serve + spider worker --capabilities http,browser,ocr,vision on one machine, PostgreSQL local (infra/compose provides PostgreSQL for development).
  • Distributed: one gateway; workers on any node that reaches it over the private network. Routing is by capability: OCR only where the Swift binary exists (macOS), vision only where the model is cached and memory suffices; a worker that cannot provide a capability drops it at start-up and says so in its log.

# What is deliberately absent

Stealth, fingerprint evasion, identity rotation, CAPTCHA solving, form submission, logged-in sessions, private endpoints. See THREAT_MODEL.md and ADR-0005.

Files main
Menu

Theme