0702. Query semantics and guards in doc workers
Status: Accepted (2026-11-27). Agreed with WP-F (MCP) and WP-A (docQueries). nameRegex amended by 0704 (SR-01): find() runs nameRegex only on the linear engine (no backtracking fallback), with a time budget, and every query runs under a wall-clock guard.
Context
shared/src/api.ts fixes the request/response schemas but leaves value formats, paging and abuse limits open. The MCP feeds query rows back into ops (undo, duplicate), and the worker runs user-supplied patterns over 600K+ names.
Decision
- Values.
- Rows use the JSON-op form of
@linorahq/doc/json, so propsFromJson round-trips them: colors #RRGGBB[AA], enums and fontWeight as ints, points flat, parent + key exact.
- f32 values are printed as the shortest decimal that is the same f32. Computed bounds are rounded to 1/1000 px.
- Fields that do not apply to a node’s kind are null;
stroke is null when absent.
- getNodes.
- One row per requested id, in request order, each followed by its descendants to
childDepth in pre-order.
- Unknown ids give
[id, null, ...].
- Pages hold 10,000 rows. The opaque cursor binds to (ids, fields, childDepth) and resumes after the last returned node while it lives, else by count.
- search / outline.
docQueries cursors pass through. Unknown root/within ids and foreign cursors -> 400 with fields.
- Outline trailer. The outline text adds a trailing ` [+N: …]` line on the first page when the root has hidden children.
- changes.
- Net status per id over the range: deleted wins, a node created in the range is
created, and changed covers only nodes that existed before.
- The per-page ring answers exactly (whole deleted subtrees).
- Older ranges come from the journal and list DELETE roots only. Ranges below the kept journal -> 400.
- nameRegex (ReDoS).
- The worker sets V8’s
--enable-experimental-regexp-engine-on-excessive-backtracks at startup.
- It rejects patterns the linear engine cannot run, checked by test-compiling with the
l flag: backreferences, lookahead, inline flags, large counts.
- WP-A compiles
nameRegex without the i/u flags, which disable the fallback. So nameRegex is case-sensitive; name/text substrings stay case-insensitive.
- Render.
- toSvg culls off-region and sub-pixel nodes (<= 200K elements). resvg renders on a libuv thread with bundled fonts.
- Limits: maxSize longest side; upscaling <= 4x; one render plus a queue of 4, then 503; 5 s abort, then 503; SVG > 32 MB -> 400.
Alternatives
- Display-form values (degrees, enum names): the MCP needs op form for undo and converts for display itself.
- Worker threads with a timeout per regex: they would need a copy of the doc per thread.
- RE2: a new native dependency.
Consequences
- Agents can page any subtree and replay it exactly.
- A catastrophic regex costs about a millisecond per name scan instead of hanging the process.
- Case-insensitive regex needs character classes (
[Bb]utton).