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