Linora

Doc workers and compactor (apps/docworker)

Owner: WP-G. ADRs 0021 (topology), 0010 (persistence), 0019 (sequenced apply), 0101 (tombstones), 0304 (query proxy), 0701-0703 (this tier). Contracts: DocCore, DocQueries in shared/src/doc-api.ts; query routes and schemas in shared/src/api.ts; ServiceTokenClaims in shared/src/auth-token.ts.

Purpose

Every server-side read that needs a full document runs here: API queries (summary, outline, nodes, search, changes), MCP reads, PNG/SVG render, and compaction. The realtime tier never loads a document.

Current (wave 2)

apps/docworker/src/
  main.ts              --mode=worker|compactor, config, metrics/health port, RSS guard, drain (exit 75 = restart me)
  config.ts            LINORA_DOCWORKER_* / LINORA_COMPACTOR_* env
  store.ts, blob.ts    snapshot reads (blob store or PG chunks, inflated into one exactly sized buffer), journal cursor, snapshot blob keys
  materialize.ts       snapshot + journal -> Doc, shared by worker and compactor (origin 'sequenced', doc.seq = row seq)
  changes.ts           per-page change ring + journal fallback
  worker/              server.ts (routes, svc-token auth, freshness), cache.ts (LRU), page.ts (cached page), follow.ts (realtime)
  queries/             handlers.ts, format.ts (rows, outline text), render.ts (resvg), regex.ts (ReDoS guard), provider.ts
  compactor/           compactor.ts (loop), compact.ts (one page), writes.ts (snapshots, leases, retention), cleanup.ts

Doc worker (--mode=worker)

Compactor (--mode=compactor)

Running

Both need DATABASE_URL and LINORA_BLOB_DIR (shared with the API and realtime). In production each connects as its own tier role (linora_docworker read-only, linora_compactor; ADR 0310). The worker also needs token keys for svc:api (verify) and svc:docworker (sign): LINORA_TOKEN_KEYS_SVC_API + LINORA_TOKEN_KEYS_SVC_DOCWORKER, or the dev master LINORA_TOKEN_KEYS. It never holds the user-token key. The API needs LINORA_DOCWORKER_URLS=http://127.0.0.1:8790.

Env Default Meaning
LINORA_DOCWORKER_HOST / LINORA_DOCWORKER_PORT / LINORA_DOCWORKER_METRICS_PORT 127.0.0.1 / 8790 / 9466 private bind; 0 = ephemeral, metrics -1 = off
LINORA_DOCWORKER_MAX_BYTES 2 GiB LRU cap
LINORA_DOCWORKER_RSS_LIMIT 1.5 x cap + 256 MiB exit-75 guard
LINORA_DOCWORKER_DB_POOL 4 Postgres connections
LINORA_DOCWORKER_FIND_BUDGET_MS 100 time budget of one search walk (then truncated + cursor)
LINORA_DOCWORKER_QUERY_TIMEOUT_MS 2000 wall-clock guard per query (503); 0 = off; must exceed the find budget
LINORA_REALTIME_INTERNAL_URL (else LINORA_REALTIME_URL) unset live follow; unset = journal catch-up only
LINORA_COMPACTOR_MAX_BYTES / LINORA_COMPACTOR_MAX_JOBS 2 GiB / 500 memory cap, jobs per process
LINORA_COMPACTOR_MIN_NODES 0 claim only pages with at least this many nodes (large-memory compactors)
LINORA_DOCWORKER_RASTER_RSS_LIMIT 512 MiB RSS cap of the PNG rasterizer child (SR-08)
LINORA_DOCWORKER_METRICS_HOST / LINORA_COMPACTOR_METRICS_HOST 127.0.0.1 metrics bind address (SR-18)
LINORA_SNAPSHOT_STORE blob blob (the blob store) or pg (chunks)
LINORA_BLOB_STORE + LINORA_S3_* local local (LINORA_BLOB_DIR) or s3 (S3-compatible bucket; ADR 0308)
LINORA_COMPACTOR_TAIL_OPS / _TAIL_BYTES / _TAIL_AGE_S 20000 / 4 MiB / 600 thresholds
LINORA_COMPACTOR_DELETE_MIN_NODES / _DELETE_QUIET_S / _DELETE_INTERVAL_S / _DELETE_INTERVAL_US_PER_NODE 20000 / 2 / 5 / 15 delete rule (ADR 0312): snapshot node floor, seconds without ops, minimum age of the newest snapshot = max(interval, nodes x µs/node)
LINORA_COMPACTOR_POLL_MS / _LEASE_S / _CLEANUP_MS / _ID 2000 / 120 / 600000 / random loop knobs
LINORA_COMPACTOR_METRICS_PORT 9467  

Measured (M4 Pro; npm run bench -w @linorahq/docworker; generateSite(4000) = 662,191 nodes, raw snapshot 35.6 MiB, gzip 5.9 MiB)

Acceptance (WP-G)