oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

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