oficina / docs Public
Contract rendering foundation
Status: implemented on feat/contract-workflow, 2026-10-26; independently reviewed by GPT-6 / Codex. Repository merge gates remain separate. Accepted for implementation with its cumulative-indentation finding addressed by the fatal overflow diagnostic below. Consumes the 2026-10-25 contract workflow design §5 and §9. Paul asked to continue implementation on 2026-10-26. This is the first implementation unit; it is not the integrated Fichas preview or the signing workflow.
Scope
Add internal/contract, a pure, bounded renderer for a versioned template tree and explicitly supplied typed values. It resolves structural field nodes into text, validates that the entire tree is renderable, and renders an actual PDF with the existing Docs exporter. It has no HTTP route, credential, database, source fetch, or send/sign operation. App handlers must authorise and pin their source revisions before calling it later.
Keep this format separate from the live CRDT/editor schema until web and native clients can preserve field nodes. Adding a live schema node now would allow older clients to discard fields. This package's tests exercise real merge and PDF output but are not evidence of an integrated workflow.
Data contract
Template has version 1, a title, a document root and field definitions. Nodes use Docs' exported tree vocabulary plus an inline merge_field node whose attrs.key names a definition. A field definition has a stable key, display label, kind (text, date, money), optional flag, and a currency for money. No expressions or arbitrary HTML. Repeated field occurrences use the same value. Definition order must not affect output. Reject duplicate definitions, unused definitions and dangling field nodes.
Values are keyed by field key and typed. Missing map entries differ from explicit empty text. Required text rejects empty or whitespace-only input; optional missing/empty text produces no text. Zero money is a real value. Date values are strict calendar dates YYYY-MM-DD with no timezone. Money is signed int64 minor units plus an explicit ISO currency agreeing with its definition. No float conversion, including at int64 limits.
The first supported format is explicitly iso: dates are ISO dates and money is EUR 1234.56, with the currency's standard minor-unit exponent (JPY 0, EUR 2, KWD 3). Locale-specific formatting is a later, explicit format version. Refuse unsupported formats instead of guessing locale. This narrows the initial workflow proposal's locale option deliberately; it does not claim localised presentation that is not implemented.
The supported document subset is doc/paragraph/heading/bulletlist/ orderedlist/listitem/blockquote/codeblock/text/hard_break. Enforce the parent/child grammar, heading levels 1–6, indentation 0–8, and the existing alignment values. Only attributes the renderer actually supports may be used on each node. Known font/size marks and strong/em/code are permitted on text/fields/breaks; reject unknown or contradictory marks rather than quietly discarding their meaning. Link/image/table and unknown nodes are errors, not flattened content. Return diagnostics with field key/node path and stable code, not submitted field values or PDF/text content.
Bounds and failure semantics
Bounds are constants with reasons: encoded template 1 MiB; 10,000 tree nodes and depth 32; 128 definitions; 64 KiB per value and 1 MiB total resolved text. These fit ordinary contracts while bounding recursive and repeated-field expansion. Validate UTF-8 and refuse control characters other than supported text whitespace. Titles and labels are bounded too. Decode a single JSON value strictly, including unknown properties. Typed callers go through the same semantic validation as decoded callers.
Independent code review additionally identified quadratic shaping of long unbroken runs in the existing PDF exporter. Cap each word at 1,024 UTF-8 bytes, interactive PDF text at 128 KiB, and PDF output at 100 pages. Refuse excess before returning an artifact. Page exhaustion returns no truncated PDF; empty paragraphs count toward that budget. Bound expanded node count too, including hard breaks introduced for multiline values. These are explicit supported-subset limits, not silently truncated inputs.
Merge builds a fresh tree and never mutates the template or input map. Values are literal text even when they contain field syntax or markup. An error returns no resolved document or PDF. PDF font-substitution reports are fatal for this package even though ordinary Docs export is permissive. Errors expose codes/coordinates, not the offending characters.
The PDF exporter must also report layout overflow (including cumulative quote/list/paragraph indentation). The contract adapter treats that report as fatal and returns no preview. Test nested lists/quotes combined with maximum paragraph indentation, and single glyphs too wide for the remaining line. A successful glyph report alone is not evidence all text fits.
Preview returns the resolved document, exact PDF bytes, SHA-256 digest, template-format version and renderer-format identifier. It does not claim those bytes are persisted, approved, issued, cryptographically signed or authorised. A later preparation store must freeze the returned bytes, not re-render them on send. An accessible HTML view must escape the resolved text; this package does not mint trusted HTML.
Evidence
Tests cover repeated fields, labels independent from keys, input immutability, literal markup, missing/empty/zero, leap dates, currency scale/mismatch, int64 bounds, unsupported format/schema/node/mark, contradictory formatting, malformed grammar, resource caps, and missing glyph refusal. A PDF integration case extracts text from the real output and checks substituted contract terms and its digest; it must fail when substitution is disabled. Refusal cases must return no preview artifact.
After independent review: implement the types/strict parser, resolver and PDF adapter; run the focused tests and adversarial mutation; run repository checks and independent branch review before any merge. The next app-level unit is template authoring/persistence with client compatibility, followed by the authenticated Fichas transport already identified in suite #14.
Implementation and review record
internal/contract implements strict parsing, semantic validation, typed resolution and real PDF generation. export.PDFBounded adds a page budget without capping ordinary export; Report.LayoutOverflow lets the contract adapter refuse overflow without silently changing ordinary export policy. The PDF bytes and SHA-256 digest are returned together. No persistence, editor schema change, UI, Fichas source fetch or signing route is exposed.
Independent GPT-6 code review found the unbroken-word shaping issue above; limits and adversarial tests addressed it. Final review approved the isolated foundation subject to CI/merge gates and independently ran go test ./internal/contract ./internal/export successfully. The parent also ran race-enabled contract tests, extracted the fictional sample's PDF text with Poppler, and visually inspected its page. An overlay mutation disabling field substitution made the expected-value test fail for missing contract terms; no working-tree code was changed by that mutation.
Formatting v1 is ISO, explicitly not locale-specific. This foundation is not the completed integrated-preview milestone; the remaining app-level work in the workflow design still applies.