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.
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.
docQueries (find, outline, summary, toSvg) from @linorahq/doc. This app adds only HTTP shaping, auth, caching, freshness, the changes log and PNG encoding.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
--mode=worker)LINORA_DOCWORKER_HOST, default 127.0.0.1): regular workers and an optional large-memory pool for big pages (ADR 0706). The API picks the worker by rendezvous hash of pageId within a pool (ADR 0304), so a page stays warm in one worker.svc === 'api' whose fid equals the path’s fileId, checked before any body read or DB access: 401 for missing/forged/expired/user tokens, 403 for another service or file.Doc with the change feed on.X-Linora-Seq.svc: 'docworker'.minSeq: wait for the follower (<= 500 ms), then a journal catch-up, then wait out the rest of 2 s. Still short -> 503 unavailable + Retry-After: 1.doc.bytes() + change-ring bytes + reservations of loads in flight. They never exceed LINORA_DOCWORKER_MAX_BYTES (default 2 GiB).503 with the internal header x-linora-page-too-large. The API sends such pages to the large-memory pool (LINORA_DOCWORKER_LARGE_URLS): by estimated size up front, or by retrying once after this 503 (ADR 0706). The estimate formula (estimatePageBytes, server-common) is shared with the API.LINORA_DOCWORKER_RSS_LIMIT (default 1.5x cap + 256 MiB; fragmentation guard).since older than the kept journal -> 400 fields.since.changes has no minSeq, so each call reads the durable journal head (one PK index lookup) and catches up to it: every acknowledged batch is included.propsFromJson round-trips), f32 values as the shortest round-tripping decimal, non-applicable fields null, unknown ids as [id, null...].nodes/get pages at 10,000 rows with an opaque pre-order cursor.<indent><id> <type> "<name>" x,y wxh [+N: ...]; on the first page a root with hidden children gets a trailing ` [+N: …]` line.nameRegex runs only on V8’s linear engine and sees the first 256 chars of each name. checkRegex delegates to packages/doc’s checkNameRegex (the same rule find() applies); anything else is 400 (ADR 0704, SR-01).LINORA_DOCWORKER_FIND_BUDGET_MS, 100 ms). When it runs out the answer is truncated: true with a nextCursor that resumes the scan (total is then a lower bound).vm timeout of LINORA_DOCWORKER_QUERY_TIMEOUT_MS (2 s). V8 preempts it, the worker answers 503 + Retry-After: 5 and counts linora_docworker_query_timeouts_total{route}. changes checks the same deadline between journal batches.toSvg (WP-A culls off-region and sub-pixel nodes, caps at 200K elements) -> resvg on a libuv thread.
maxSize (default 768), upscaling <= 4x.--mode=compactor)compaction_leases row for the qualifying page with the largest tail_bytes.tail_ops >= 20,000, tail_bytes >= 4 MiB, or the first journal row after snapshot_seq older than 10 minutes. The age rule also catches closed rooms.tail_deletes > 0 (accepted DELETE ops in the tail), the snapshot has >= LINORA_COMPACTOR_DELETE_MIN_NODES nodes, last_op_at is older than LINORA_COMPACTOR_DELETE_QUIET_S and the newest snapshot is older than max(LINORA_COMPACTOR_DELETE_INTERVAL_S, node count x LINORA_COMPACTOR_DELETE_INTERVAL_US_PER_NODE µs). That keeps delete-driven jobs to about 10% of one compactor per page. A DELETE removes whole subtrees: 6 ops (26 KB) once emptied a 660K-node page, which otherwise waited 10 minutes while every open decoded the old snapshot.SKIP LOCKED). The lease is renewed every leaseS/3 while the job runs.loadTombstones, ADR 0101 carry-forward) + journal rows up to the head at start, in seq order.page_snapshots row (or PG chunks when LINORA_SNAPSHOT_STORE=pg) + pages.snapshot_seq/node_count + tail_ops/tail_bytes/tail_deletes -= compacted (exact, signed), applied only if snapshot_seq still equals the job’s base. Otherwise the job lost a race: rollback, and its blob files are removed. Quiet pages (nothing after the snapshot, newest row > 60 s old) get their counters set to 0 (ADR 0705).(LINORA_COMPACTOR_MAX_BYTES - 128 MiB) / 400 B nodes are never claimed. A large-memory compactor (bigger cap, LINORA_COMPACTOR_MIN_NODES set) takes them (ADR 0706).LINORA_COMPACTOR_MAX_JOBS jobs (default 500) or when RSS passes the cap.LINORA_COMPACTOR_CLEANUP_MS):
npm run dev:docworker: port 8790, metrics 9466.npm run dev:compactor: metrics 9467.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 |
npm run bench -w @linorahq/docworker; generateSite(4000) = 662,191 nodes, raw snapshot 35.6 MiB, gzip 5.9 MiB)doc.bytes() is 52 MiB (82.8 B/node incl. 17 MB text).test/follow.test.ts).test/compaction.test.ts).