: asapien/signet

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 
329 
330 
331 
332 
333 
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
358 
359 
360 
361 
362 
363 
364 
365 
366 
367 
368 
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386 
387 
# Rebuild plan: from the 2026-09-18 code to the 2026-09-19 model

**Historical plan, not a current implementation report.** Task completion was
recorded in `todo.txt`, but source inspection on 2026-11-10 found material
acceptance gaps. Use `HANDOFF.md` §4 for those findings. The steps, old line
references and test scenarios below preserve the original intent; they are not
proof that the current code meets it.

Written for whoever (or whichever model) does the code change. It assumes no
memory of the design session. Read, in this order: `CONTEXT.md` (vocabulary —
use those words in code, comments, UI and tests), `HANDOFF.md` §2 (decisions
D16–D29 and why), `docs/adr/0001-signers-sign-at-sign-off.md`, `AGENTS.md`
(hard constraints; they all still apply). Line numbers below refer to the
2026-09-18 code and will drift as you edit.

## 0. What changes, in one paragraph

A template names **parties** (ordered posts). The drafter binds the document to
one **organisation**, assigns one **signer** per party, reserves at most one
**signature zone** per party, and submits. The worker flattens the imprints
once into `proof.pdf` and copies it to `stamped.pdf`. Parties act strictly one
after another. A sign-off on a zone party carries a **placement** inside the
zone; the worker overlays that signer's **enrolled facsimile** onto
`stamped.pdf`. After the last party the worker overlays, appends the audit page
(the full event log) and writes `final.pdf`; only then does the status become
`RELEASED`. Signers without a facsimile enrol one in the browser before their
first sign-off. Nothing about the browser/worker text-fitting contract,
kcgi/kfcgi, qpdf, or the 60 MB budget changes.

## 1. Scope guard — do not build these

Parallel signing or `any`/`all`; more than one zone per party; per-page
initials; redaction; free-text imprints; `customer`; notifications of any kind;
PAdES; hand-picked DAM images on a document (all images come via PIM keys);
template versioning. If something seems to need one of these, stop and ask.

## 2. Schema (`sql/schema.sql`)

The DB is recreated on every dev run, so edit the schema in place; no
migration.

```sql
CREATE TYPE doc_status AS ENUM ('DRAFT','PENDING','CORRECTION_REQUIRED',
                                'DISAPPROVED','RELEASED');

-- series: + retired_at TIMESTAMPTZ
-- templates: + retired_at TIMESTAMPTZ; route_json becomes
--   {"parties":[{"name":"Partner","signer":"anil"}]}   (signer = default, may be null)
-- documents:
--   - customer_id                     (D21)
--   + org_code       VARCHAR(50)      (D23; NOT NULL at submit, nullable in DRAFT)
--   - current_level  → + current_party INTEGER NOT NULL DEFAULT 1  (1-based)
--   - rendered_revision → + render_seq INTEGER NOT NULL DEFAULT 0,
--                         + rendered_seq INTEGER NOT NULL DEFAULT 0,
--                         + render_kind VARCHAR(10)   -- 'stamp'|'sign'|'release'
--   - approved_at    → + released_at TIMESTAMPTZ
--   template_id stays nullable (unlink sets it NULL)
-- doc_decisions: level → party INTEGER; approver → signer;
--   + placement JSONB   -- {"page":2,"coords":[x1,y1,x2,y2],"ref_w":1000,"ref_h":1414}, NULL for check-only
--   decision IN ('SIGNOFF','CORRECTION','DISAPPROVE')
-- doc_events.action values: CREATED, SUBMITTED, RESUBMITTED, STAMPED,
--   STAMP_FAILED, SIGNED, SIGN_FAILED, CORRECTION_REQUIRED, DISAPPROVED,
--   RELEASED, RELEASE_FAILED, RETRY, UNLINKED, ENROLLED
-- pim_attributes: + attr_type VARCHAR(5) NOT NULL DEFAULT 'text' CHECK (attr_type IN ('text','image'))
--   image value = DAM-relative path validated by is_safe_relpath
-- NEW facsimiles (username VARCHAR(100) PRIMARY KEY, enrolled_at TIMESTAMPTZ,
--   enrolled_by VARCHAR(100) NOT NULL, width INTEGER, height INTEGER)
--   metadata only; the PNG lives in sig_dir. width/height (pixels) are set by
--   the worker at enrol so the client can auto-fit a placement without ever
--   seeing the image.
```

`render_seq`/`rendered_seq` replace `revision`/`rendered_revision` as the
"worker is busy" gate (D3's pattern, generalised): the API increments
`render_seq` and sets `render_kind` whenever it enqueues a job (submit,
sign-off, release, retry); the worker sets `rendered_seq = seq` on success.
`revision` still counts submits and scopes decisions.

Redis mirror (pim-api resync and writes): `HSET <org>:metadata <key> <value>`
for text, `HSET <org>:images <key> <path>` for images. Add `SADD pim:orgs <org>`
so the API can list organisations without touching PostgreSQL.

## 3. Configuration (`etc/signet.conf.example`, `src/conf.c` users)

* Roles: `role.drafter`, `role.signer`, `role.admin`, `role.pim_admin`
  (rename from `maker`/`approver`; group names stay config-only).
* `sig_inbox = /var/lib/signet/sig-inbox` — directory owned `signetd:signet`,
  mode `1730`: the API user can create files but not list the directory;
  `signetd` moves files out. Add it to `make install` and `test/dev-up.sh`.
* Dev groups in `test/dev-up.sh` / `lighttpd-dev.conf`: `clerks` → drafter,
  `management` → signer, `signet-admins` → admin + pim_admin. Keep the seeded
  facsimiles for `ceo` and `manager` **and** seed matching `facsimiles` rows.

## 4. Layout and route JSON (client ⇄ API ⇄ worker)

```json
{"reference_width":1000,"reference_height":1414,"imprints":[
 {"page":1,"type":"pim_text","ref":"gstin","prefix":"GSTIN: ","coords":[120,150,520,180],"font":"fonts/X.ttf","align":"left"},
 {"page":1,"type":"serial","coords":[120,200,620,228]},
 {"page":1,"type":"date","coords":[700,150,900,178],"align":"right"},
 {"page":2,"type":"pim_image","ref":"seal","shape":"circle","coords":[300,1100,90]},
 {"page":2,"type":"signature_zone","party":"Partner","coords":[600,1050,850,1150],"min_w":120,"min_h":40}]}
```

```json
{"parties":[{"name":"Manager","signer":"manager"},{"name":"Partner","signer":"ceo"}]}
```

* Imprint types: `pim_text`, `pim_image`, `serial`, `date`, `signature_zone`.
  `dam_image` and `signature` are gone. `ref` is a bare key; the organisation
  comes from `documents.org_code`. Shapes: `rect`, `circle` (`poly` gone).
* `min_w`/`min_h` are in the imprint's natural pixels (same frame as `coords`).
* Party names: `is_safe_name`, unique within a route, 1–20 parties.

## 5. API (`src/signet-api.c`)

Endpoint list at the top of the file becomes:

```
GET  /me                               → {user, roles:{drafter,signer,admin,pim_admin}, enrolled:bool}
GET  /docs?scope=mine|pending|involved|all
POST /docs                             file=<pdf> | template_id=<uuid>
GET  /docs/<id>
POST /docs/<id>/save                   json={title,org_code,series_code,layout,route}
POST /docs/<id>/submit
POST /docs/<id>/rerender               (retry: re-enqueue render_kind)
POST /docs/<id>/decide                 decision=signoff|correction|disapprove, comment, placement=<json>
POST /docs/<id>/delete                 (drafts only)
POST /docs/<id>/unlink                 (admin)
GET  /docs/<id>/file/<source|proof|stamped|final>
GET  /templates   POST /templates      doc_id, name
POST /templates/<id>/retire            (admin)   POST /templates/<id>/delete (admin; 409 if any document links)
GET  /series      POST /series         code, pattern (admin)   POST /series/<code>/retire (admin)
GET  /pim                              → {orgs:[…], attrs:{<org>:{text:{k:v},images:{k:path}}}}
GET  /dam                              → {fonts:[…], images:[…]}   (images for the PIM editor only)
POST /me/facsimile                     file=<png>  (enrol self)      POST /facsimiles  user, file=<png> (admin)
```

Behaviour, by endpoint (only what changes):

* **`/save`** ([:381](../src/signet-api.c:381)–[:398](../src/signet-api.c:398)):
  validate `org_code` with `is_safe_name`; `check_layout` accepts the new
  types, rejects `dam_image`/`signature`/`poly`, requires `party`, `min_w`,
  `min_h` on zones and checks `min ≤ zone size`; `check_route` validates
  parties (name, signer both `is_safe_name`); every zone's `party` must exist
  in the route; at most one zone per party. Drop `customer_id`.
* **`/submit`** ([:420](../src/signet-api.c:420)–[:475](../src/signet-api.c:475)):
  fail with a message when `title = ''` (D22), `org_code` is NULL, a
  `pim_text` key is not in `HGET <org>:metadata`, a `pim_image` key is not in
  `HGET <org>:images` or its file is missing under `dam_dir`, a party has no
  signer, or the series is retired. Do **not** require signers to be enrolled
  at submit (they enrol when they sign). Status → `PENDING`; `RESUBMITTED`
  event instead of `SUBMITTED` when coming from `CORRECTION_REQUIRED`;
  `current_party = 1`; `render_seq + 1`, `render_kind = 'stamp'`; enqueue
  `{"doc_id","seq","kind":"stamp"}`. Serial, `doc_date` allocation unchanged
  (invariant 8).
* **`/decide`** ([:580](../src/signet-api.c:580)–[:689](../src/signet-api.c:689)),
  replace `CAN_DECIDE`:
  `status = 'PENDING' AND rendered_seq = render_seq AND render_error IS NULL
  AND route_json->'parties'->(current_party-1)->>'signer' = $user`.
  Decisions are `signoff|correction|disapprove`; comment required unless
  signoff. For signoff on a party **with a zone**: `placement` is required,
  its page must equal the zone's page, its rectangle must lie inside the zone
  and be ≥ `min_w × min_h` (compare in the zone's natural frame), and
  `facsimiles.enrolled_at` for the signer must be non-NULL (else 409
  "enrol your signature first"). Insert the decision (with placement). Then:
  * not the last party → `current_party + 1`; if the party had a zone:
    `render_seq + 1`, `render_kind='sign'`, enqueue `kind:"sign"`; a
    check-only sign-off enqueues nothing.
  * last party → `render_seq + 1`, `render_kind='release'`, enqueue
    `kind:"release"`; status stays `PENDING` until the worker finishes
    (`RELEASED` is set by the worker).
  * correction → `CORRECTION_REQUIRED`; disapprove → `DISAPPROVED`; both
    unlink `stamped.pdf`, as today.
  Delete `release()` ([:520](../src/signet-api.c:520)–[:575](../src/signet-api.c:575))
  from the API entirely: the worker owns it now (D18).
* **`/rerender`**: allowed for the drafter (kind `stamp`), the current/last
  signer or admin (kinds `sign`/`release`): re-enqueue the stored
  `render_kind` with `render_seq + 1`; log `RETRY`.
* **`/file/<kind>`** ([:713](../src/signet-api.c:713)): unchanged rule
  (`admin || in_route || (is_drafter && kind != 'stamped')`), with `in_route`
  now testing `route_json->'parties' @> [{"signer": $user}]`. `final` only
  when `RELEASED`.
* **`/docs`** scope `pending` = documents whose current party's signer is me
  and `rendered_seq = render_seq`; add a `waiting_on` column
  (`current party name + signer`) for the dashboard instead of "Level".
* **`GET /docs/<id>`**: return `parties` as an array with each party's name,
  signer, zone (if any) and its decision for this revision; `can_edit`,
  `can_decide`, `needs_enrolment` (can_decide ∧ zone ∧ not enrolled),
  `rendering` (`rendered_seq < render_seq`), and the full event log
  (`doc_events` ordered by id) for the side panel.
* **Templates**: `POST /templates` copies `layout_json` and `route_json`
  (party names and signers as defaults). `retire`/`delete` per D28;
  `GET /templates` excludes retired unless admin and includes `linked` (count
  of documents). `POST /docs/<id>/unlink` sets `template_id = NULL`, logs
  `UNLINKED`.
* **Facsimiles**: `POST /me/facsimile` (any signer) and `POST /facsimiles`
  (admin, `user` field) accept one PNG ≤ 1 MB, verify the PNG signature
  bytes, write it to `<sig_inbox>/<user>.png.tmp` then rename to
  `<sig_inbox>/<user>.png`, upsert `facsimiles (username, enrolled_by)` with
  `enrolled_at = NULL`, and enqueue `{"kind":"enrol","user":…}`. The API
  never opens `sig_dir` (invariant 5) and never reads the inbox back.
  `GET /me.enrolled` = `enrolled_at IS NOT NULL`.

## 6. Worker (`src/signetd.c`, `src/render.[ch]`)

Job payload `{"doc_id","seq","kind"}` or `{"kind":"enrol","user"}`. A job is
stale (skip, return 0) unless `documents.render_seq = seq` — same idea as
today's revision check ([signetd.c:108](../src/signetd.c:108)).

* `stamp`: `render_stamp()` on `source.pdf` → `proof.pdf`; then copy
  `proof.pdf` → `stamped.pdf` (no facsimiles at this point). Drop
  `with_signatures` and the `signature` imprint type from `render.c`;
  `pim_text` resolves `HGET <org>:metadata`, `pim_image` resolves
  `HGET <org>:images` then `dam_dir/<path>`. Set `rendered_seq`, log `STAMPED`.
* `sign`: read the latest `SIGNOFF` decision with a placement for this
  revision; new `render_sign(ctx, stamped_in, out, png, placement)` draws one
  PNG (from `<sig_dir>/<signer>.png`) at the placement rectangle on an empty
  overlay and `qpdf --overlay`s it onto `stamped.pdf` (atomic `.tmp` + rename
  as now). Fit the image inside the placement keeping proportions, centred.
  Set `rendered_seq`, log `SIGNED`.
* `release`: do the `sign` step for the last decision if it has a placement,
  then build the audit lines **in the worker** from `documents` (id, serial,
  title, organisation, drafter) and `doc_events` (every row, all revisions,
  `to_char(at) | actor | action | detail`, comments included), call
  `render_release()`, compute the SHA-256 of the body as today, and in one
  statement set `status='RELEASED', final_sha256, released_at, rendered_seq`.
  Unlink `stamped.pdf`. Log `RELEASED`. On failure: `render_error`,
  `RELEASE_FAILED`; the last signer or admin can retry.
* `enrol`: copy `<sig_inbox>/<user>.png` → `<sig_dir>/<user>.png` (owner
  `signetd`, mode 0600, via a `.tmp` + rename), unlink the inbox file, set
  `facsimiles.enrolled_at = now(), width, height` (from the PNG header), log
  `ENROLLED` (`doc_id` NULL — make
  `doc_events.doc_id` nullable, or keep a separate small log line; prefer the
  nullable column). Validate with libpng before accepting (reuse the PNG
  reader in `pdfw.c`); on a bad file delete it and leave `enrolled_at` NULL.
* Startup drain and `BRPOPLPUSH`/`LREM` discipline unchanged (invariant 7).

`render_stamp` keeps clipping semantics for `circle` (HANDOFF §4.5 asks for
real clipping — do it now if cheap in `pdfw.c` with a clip path, otherwise
leave the bounding-box behaviour and say so).

## 7. PIM (`pim/pim-api.c`, `pim/www/index.html`)

* `attr_type` on write (`text` default, `image` requires a DAM-relative path
  that passes `is_safe_relpath`); resync mirrors both hashes and `pim:orgs`.
* Editor: organisation selector (list + "new"), a type toggle per attribute,
  and for images a picker fed by `GET /api/dam` images.
* Reads stay open to every authenticated user (D24).

## 8. Client (`www/app.js`, `www/index.html`, `www/style.css`)

Keep AGENTS.md invariants 1–3 exactly. Section by section
([app.js](../www/app.js) line refs from the 2026-09-18 file):

* `TYPES` ([:13](../www/app.js:13)): `pim_text`, `pim_image`, `serial`,
  `date`, `signature_zone`. Palette shows zones as "Signature zone"; the
  props panel ([:402](../www/app.js:402)) offers a party dropdown (from the
  route), `min_w`/`min_h` inputs, and for `pim_*` a key dropdown from
  `app.pim.attrs[doc.org_code]`. Remove the `dam_image` picker.
* `imprintText` ([:166](../www/app.js:166)): resolve `pim_text` against the
  document's organisation; when no organisation is chosen yet show the key in
  brackets.
* Side panel ([:481](../www/app.js:481)–[:595](../www/app.js:595)): title
  (required, submit button disabled while empty), organisation selector,
  series, **parties editor** replacing `routeEditor` ([:437](../www/app.js:437)):
  ordered rows of party name + signer username (prefilled from the template),
  add/remove/reorder. Remove the customer field and the mode dropdown.
* Preview mode for the current signer: show earlier facsimiles (they are in
  `stamped.pdf` already) and this party's zone as an outlined box. Decision
  bar: **Sign off / Request correction / Disapprove**. For a zone party:
  * `needs_enrolment` → open the enrolment dialog: a `<canvas>` signature pad
    (pointer events, black stroke on transparent, export `toBlob('image/png')`)
    or a PNG file input; `POST /me/facsimile`; poll `GET /me` until
    `enrolled`, then continue.
  * placement box inside the zone: starts at the auto fit — the largest
    rectangle with the facsimile's aspect (`width/height` from `GET /me`)
    centred in the zone — then draggable and resizable, constrained to the
    zone and to `min_w × min_h`; the box is an `<area>` in the page map like
    any other imprint (invariant 1). Submit `placement` with the sign-off.
    If the auto fit cannot satisfy `min_w × min_h` (a tall facsimile in a
    wide zone), show the box at the fit anyway and let the signer adjust; the
    API's check is the authority.
* Dashboard ([:599](../www/app.js:599)): columns Document, Status, Drafter,
  Waiting on, Created; scopes `mine | pending | involved | all` unchanged.
  Show "rendering…" while `rendering`; show the retry button on
  `render_error` to whoever may retry.
* History ([:471](../www/app.js:471)): render the event log from
  `GET /docs/<id>` (HANDOFF §4.11).
* Polling ([:503](../www/app.js:503)): poll while `rendering`, not only after
  submit (sign-offs and release render too).
* Series/templates admin views: retire buttons; template delete with the
  linked count and the 409 message.

## 9. Tests (`test/e2e.sh`)

Rewrite the flow; keep the harness (`as`, `expect`). Cover, in this order:

1. roles: `"drafter":true,"signer":false` for a clerk; series create needs
   admin; series retire.
2. PIM: org `SKGA` with `gstin` (text) and `seal` (image → `seals/agency_seal.png`);
   `GET /api/pim` lists orgs and both types.
3. draft from upload; save with title, org, parties `[Manager/manager,
   Partner/ceo]`, imprints as in §4; save rejects: unknown zone party, two
   zones for one party, `min` larger than the zone, `dam_image` type.
4. submit rejects: empty title; missing org; org without `gstin` (use a second
   org); party without signer. Then submit ok → poll `rendering=false` →
   serial `SKGA/SCNREPLY/<today>/01`; proof text has GSTIN, serial, date;
   drafter gets 403 on `stamped`; other drafter gets 404.
5. decisions: `ceo` cannot decide yet (409); `manager` (check-only) signs off
   with no placement → `current_party=2`, no render; `ceo` decide without
   enrolment → 409 "enrol"; `POST /me/facsimile` with the fixture PNG → poll
   `/me` enrolled; decide with placement outside the zone → 409; inside but
   below `min` → 409; valid → poll → `pdfimages -list` on `stamped.pdf`
   (fetched as `manager`, in route) shows one more image than the proof.
   Since ceo is the last party this is also the release: poll → `RELEASED`,
   `final_sha256` set, `final.pdf` text contains `SUBMITTED`, `SIGNED`,
   `RELEASED` lines and the serial; `stamped.pdf` gone.
6. correction round on a second document: manager requests correction with a
   comment → `CORRECTION_REQUIRED`; drafter saves and resubmits →
   `RESUBMITTED` event, `revision=2`, `current_party=1`, same serial and
   `doc_date`; manager and ceo sign again; final audit text contains the
   correction comment (D26).
7. disapprove on a third document → `DISAPPROVED`, delete → 409, serial kept,
   next document in the series gets `/03`.
8. self-service (D16): a user in both groups drafts a one-party document with
   themselves as signer and releases it.
9. templates: create from a document; new document from template carries
   parties and zones; retire hides it from the drafter; delete → 409 while
   linked; admin unlinks the documents; delete → ok.
10. retry: worker failures are now hard to provoke from the API (every
    reference is checked at submit and the DAM is read only then). Keep one
    authorisation check — `rerender` is 403 for a user who is neither the
    drafter, the current/last signer nor admin — and provoke a real failure
    once by hand (e.g. `chmod 000` the seal inside the container between
    save and submit) to see `render_error` + `RETRY` work end to end.

Then the visual check from AGENTS.md (edit vs proof vs stamped, both pages),
plus: placement box and rendered facsimile coincide; earlier facsimile visible
to the next party; circle seal clipped if you implemented clipping.

## 10. Documentation to update in the same change

* `AGENTS.md` invariants: **4** → "…signer refs are party names that exist in
  the route; placements are validated against the zone"; **5** → "Only
  `signetd` reads `sig_dir`. `signet-api` touches a facsimile only transiently
  in the enrolment request (writes it to `sig_inbox`, never reads it back) and
  never opens `sig_dir`. Drafters never receive `stamped.pdf` unless they are
  also a signer on that document"; **6** → "Imprints are flattened once, at
  submit. Each sign-off overlays one facsimile; release is a worker job.
  There is no `STAMPING` status; progress is `rendered_seq < render_seq`,
  failure is `render_error`". Rename `maker`/`approver` wording throughout;
  update the dev-groups note and the build/test commands if they change.
* `HANDOFF.md`: rewrite §3 (map, disk layout, request flow, JSON shapes) for
  the new code; strike the superseded rows' *why* only if it no longer helps;
  update §1 status and the e2e count; move §4 items 4, 5, 8, 11 to "done".
* `README.md`: user-facing flow (drafter → parties → release), enrolment, PIM
  organisations.
* `CONTEXT.md`: only if a term changed meaning while building — say so
  explicitly in the summary rather than silently editing.

## 11. Suggested order of work

Each step ends with a clean `-Wall -Wextra` build and a green (possibly
shorter) `e2e.sh`.

1. Schema, config roles, `sig_inbox`, dev seeding; rename identifiers
   (`maker_user` → `drafter_user`, statuses); API compiles with old logic
   mostly intact; e2e reduced to what still holds.
2. `/save` + `/submit` validation for org, parties, zones, title; worker
   `stamp` job with the new resolution; e2e steps 1–4.
3. `/decide` rewrite, worker `sign` and `release`, delete API `release()`;
   e2e steps 5–8 minus enrolment (seed `enrolled_at` for `ceo`/`manager`).
4. Enrolment: inbox, `/me/facsimile`, worker `enrol`; e2e enrolment checks.
5. Templates/series lifecycle, unlink; PIM types and orgs; e2e step 9.
6. Client, all sections; visual check.
7. Docs (§10), full e2e, RSS measurement against the 60 MB budget.

## 12. Acceptance

* `test/e2e.sh` ends with "all passed" and covers every item in §9.
* A signer at party *n* can never fetch a document whose `current_party < n`
  (404), and never sees a facsimile of party *> n* (there is none).
* Replacing a seal file on the DAM after submit does not change `stamped.pdf`
  or `final.pdf` of a pending document.
* `grep -rn 'customer\|current_level\|PENDING_APPROVAL\|"any"\|maker' src www sql test etc` returns nothing.
* RSS of all processes ≤ 60 MB with two API workers.