|
| 1 | +# Per-Element Workflow — How Every Page Gets Built |
| 2 | + |
| 3 | +> **This is the universal build process.** Every page, every feature Panel, every atom goes through this workflow — no exceptions. Deviations constitute a KB-worthy incident. |
| 4 | +> |
| 5 | +> The workflow was proven on the `full-stack-dashboard` example (converged 34/37 regions at 10% threshold across 30 iterations). It is now codified for all future references. |
| 6 | +
|
| 7 | +--- |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +1. [The Hard Rules](#1-the-hard-rules) |
| 12 | +2. [Order of Build](#2-order-of-build) |
| 13 | +3. [The Per-Element Loop](#3-the-per-element-loop) |
| 14 | +4. [Iteration Log Format](#4-iteration-log-format) |
| 15 | +5. [When an Element is "Done"](#5-when-an-element-is-done) |
| 16 | +6. [Escalation & Budget Exhaustion](#6-escalation--budget-exhaustion) |
| 17 | +7. [Enforcement](#7-enforcement) |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 1. The Hard Rules |
| 22 | + |
| 23 | +1. **Describe before name.** Before declaring an atom for a region, write the literal pixel description (shape count, fill vs outline, color roles, alignment, repetition count). Read at native pixel size. Per [`02-decomposition-process.md`](02-decomposition-process.md) §2 preamble. Violations are how KB §1 shipped. |
| 24 | + |
| 25 | +2. **Bottom-up build.** Atoms before Panels. Panels before features. Features before layout. Layout before whole-page verify. A feature Panel is never authored before its atoms exist and individually pass Stage C. |
| 26 | + |
| 27 | +3. **Verify after every create or modify.** `bun run scripts/verify.mts <example-id>` is non-optional. If the element is a single atom, hit its preview on `/atoms`. If it is a feature Panel, hit the dashboard route. Per [`03-visual-validation.md`](03-visual-validation.md) + KB §3. |
| 28 | + |
| 29 | +4. **10 % pixel-mismatch threshold.** Hard pass/fail line. Overrides only via per-region `tolerances.json` with a written reason. |
| 30 | + |
| 31 | +5. **30-iteration budget per element.** If the element does not pass Stage C within 30 iterations, stop. Either escalate (§6) or record the failure state and move on with documented reason. |
| 32 | + |
| 33 | +6. **Every iteration logged as an object.** Format in §4. Append to `visual-report/<example-id>/iterations.jsonl`. Empty iterations are not allowed — every fix gets a record. |
| 34 | + |
| 35 | +7. **No "verified" claim without a pass row.** Codified in KB §3, enforced by `docs/prompts/codegen.md` and `docs/prompts/visual-fidelity.md`. |
| 36 | + |
| 37 | +8. **KB first.** Before writing any code for an element, load `docs/knowledge-bases/panel-model-fidelity-corrections.md` and apply every Prevention hint relevant to the motif being built. |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## 2. Order of Build |
| 42 | + |
| 43 | +For any new reference / example, build in this order: |
| 44 | + |
| 45 | +``` |
| 46 | +1. reference.png committed |
| 47 | +2. decomposition.md authored (applies 02-decomposition-process.md) |
| 48 | +3. regions.json authored (applies 03-visual-validation.md §3.2) |
| 49 | +4. Stage A — bun run scripts/capture-before.mts <example-id> |
| 50 | + → visual-baselines/<example-id>/*.png committed |
| 51 | +5. Overlay diagnostic — bun run scripts/overview-strip.mts |
| 52 | + → visually confirm bboxes align with the reference |
| 53 | +6. Tokens + theme (pattern-level, reused across examples) |
| 54 | +7. Panel primitives (pattern-level, reused) |
| 55 | +8. Atoms — one at a time, each verified via /atoms preview route |
| 56 | +9. Feature Panels — one at a time, each verified via the dashboard route |
| 57 | +10. Layout (AppHeader, DashboardPage, routes) |
| 58 | +11. Whole-page verify — every region in regions.json, Stage C on all |
| 59 | +12. Per-failing-region fix loop — §3 per-element loop, budget 30 each |
| 60 | +``` |
| 61 | + |
| 62 | +**Invariant:** at every numbered step, the preceding step's artifacts exist and pass. Skipping ahead is a KB-worthy incident. |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## 3. The Per-Element Loop |
| 67 | + |
| 68 | +For each element (atom OR Panel OR feature OR whole page) the loop is: |
| 69 | + |
| 70 | +``` |
| 71 | +┌──────────────────────────────────────────────────────────────────┐ |
| 72 | +│ 1. LOAD — KB prevention hints for this element's motif │ |
| 73 | +│ │ |
| 74 | +│ 2. DESCRIBE — literal pixel description of the reference region │ |
| 75 | +│ (if atom: the per-atom baseline; if Panel: the panel bbox) │ |
| 76 | +│ │ |
| 77 | +│ 3. BUILD — write minimum viable .ts, .html, .scss │ |
| 78 | +│ │ |
| 79 | +│ 4. VERIFY — run scripts/verify.mts <example-id> │ |
| 80 | +│ read the pass/fail row for THIS element's region(s) │ |
| 81 | +│ │ |
| 82 | +│ 5. DIAGNOSE — if fail: │ |
| 83 | +│ a. examine visual-report/<id>/<region>.diff.png │ |
| 84 | +│ b. compare baseline + actual crops at native pixel size │ |
| 85 | +│ c. categorize drift: size, position, color, font, content │ |
| 86 | +│ │ |
| 87 | +│ 6. LOG — append iteration object to iterations.jsonl (§4) │ |
| 88 | +│ │ |
| 89 | +│ 7. FIX — one focused change per iteration │ |
| 90 | +│ (never bundle multiple hypotheses into a single iteration — │ |
| 91 | +│ you will not know which one moved the number) │ |
| 92 | +│ │ |
| 93 | +│ 8. REPEAT — from step 4 until pass OR budget exhausted │ |
| 94 | +│ │ |
| 95 | +│ 9. COMMIT — only after pass (or with escalation reason) │ |
| 96 | +└──────────────────────────────────────────────────────────────────┘ |
| 97 | +``` |
| 98 | + |
| 99 | +### 3.1 One hypothesis per iteration |
| 100 | + |
| 101 | +If you change `font-size` and `padding` in the same iteration and the number drops, you don't know which helped. The log entry will say "changed 2 things" and future iterations can't build on the knowledge. **Rule: one named change per iteration.** If the change requires coupled edits (e.g. Stat grid + label grid share columns), count the couple as one hypothesis. |
| 102 | + |
| 103 | +### 3.2 Verify is not a suggestion |
| 104 | + |
| 105 | +The workflow pauses after step 4. Do not proceed past step 4 without observing the pass/fail row for the current element. No "looks right, moving on." |
| 106 | + |
| 107 | +### 3.3 Diff image first |
| 108 | + |
| 109 | +When a region fails, open `visual-report/<id>/<region>.diff.png` before thinking about the fix. The diff shows *where* pixels differ (usually as red ghosting of both baseline and actual overlaid). That picture names the class of drift — size vs position vs color vs font — which directs the fix. |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## 4. Iteration Log Format |
| 114 | + |
| 115 | +Every iteration appends one object to `visual-report/<example-id>/iterations.jsonl`. JSONL so it's append-only and diff-friendly. |
| 116 | + |
| 117 | +Required fields: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "iteration": 17, |
| 122 | + "dateTried": "2026-04-18T18:05:00Z", |
| 123 | + "element": "alarm-stats.online", |
| 124 | + "issue": "diff ghosting shows baseline numerals larger than 33px render", |
| 125 | + "proposedFix": "font-size 33→36px", |
| 126 | + "plan": ["edit stat.component.scss", "verify"], |
| 127 | + "result": { |
| 128 | + "pass": false, |
| 129 | + "regionsPassing": 2, |
| 130 | + "regionsTotal": 4, |
| 131 | + "perRegion": { |
| 132 | + "alarm-stats": 8.93, |
| 133 | + "alarm-stats.online": 13.39, |
| 134 | + "alarm-stats.alarms": 9.18, |
| 135 | + "alarm-stats.sla": 11.31 |
| 136 | + }, |
| 137 | + "delta": "online regressed (11.67→13.39) — 36 is too big; return to 33" |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +Rules: |
| 143 | +- `iteration` — monotonic increasing, starting at 0 (initial state). |
| 144 | +- `dateTried` — ISO-8601, converted to absolute date. |
| 145 | +- `element` — the primary region id being worked on (for grouping). |
| 146 | +- `issue` — literal observation from the diff image or measurement. |
| 147 | +- `proposedFix` — single change, described concretely. |
| 148 | +- `plan` — ordered list of the exact operations. |
| 149 | +- `result.pass` — boolean, from Stage C. |
| 150 | +- `result.perRegion` — every failing region's mismatch %. |
| 151 | +- `result.delta` — one-sentence summary of what moved and why. |
| 152 | + |
| 153 | +Iteration 0 is the baseline measurement (no fix applied yet). Subsequent iterations reference the delta from the previous result. |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## 5. When an Element is "Done" |
| 158 | + |
| 159 | +Three-part gate: |
| 160 | + |
| 161 | +1. **Stage C pass** — the element's region(s) in `visual-report/<id>/summary.json` have `pass: true`. |
| 162 | +2. **KB check** — if the element matches a motif covered by a KB entry, the comment at the top of the `.ts` file cites the entry number. |
| 163 | +3. **ESLint + typecheck** — `bun run build` is clean. |
| 164 | + |
| 165 | +Only after all three can the element be marked complete or the change committed with "verified" / "passes" language. |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +## 6. Escalation & Budget Exhaustion |
| 170 | + |
| 171 | +After 30 iterations on a single element without pass, stop. Do not sneak a 31st. Instead: |
| 172 | + |
| 173 | +### 6.1 Categorize the residual drift |
| 174 | + |
| 175 | +- **Structural** — wrong atom, missing motif → open a KB entry, fix the pattern, retry. |
| 176 | +- **Sub-pixel text drift** — dominated by character x-offset in crops < 30 px tall → note in the iterations log that threshold is unachievable at this crop size with current fonts; document in KB. |
| 177 | +- **Baseline defect** — the `regions.json` bbox doesn't match the reference's actual content → fix the bbox and restart the budget (this is baseline authoring, not implementation). |
| 178 | + |
| 179 | +### 6.2 Document the stop |
| 180 | + |
| 181 | +Add a final iteration object with `"result.pass": false` and a `"budgetExhausted": true` field plus a `"residualCategory"` from the list above. The commit message must state "X of N regions pass; remaining failures are <category> with documented reason." |
| 182 | + |
| 183 | +### 6.3 Do not lower the threshold to force a pass |
| 184 | + |
| 185 | +10 % is the authoritative line. If budget exhausts and drift is legitimate, document it. If drift is structural, fix the pattern. **Do not raise the threshold to 15 % to ship.** |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## 7. Enforcement |
| 190 | + |
| 191 | +Multiple layers so skipping is hard: |
| 192 | + |
| 193 | +| Layer | What it enforces | |
| 194 | +|---|---| |
| 195 | +| `docs/prompts/codegen.md` | Forbids "verified" language without a Stage C pass row; cites KB entry numbers in component source | |
| 196 | +| `docs/prompts/visual-fidelity.md` | Review treats every KB Prevention hint as a hard check | |
| 197 | +| `scripts/verify.mts` | Non-zero exit on any region failure → CI gate | |
| 198 | +| `docs/knowledge-bases/panel-model-fidelity-corrections.md` | Growing catalogue of past failures; every agent loads it before codegen | |
| 199 | +| Persistent memory (user memory system) | `feedback_playwright_after_every_element`, `user_model_preference` carry the rules across sessions | |
| 200 | +| This doc (`04-per-element-workflow.md`) | Single source of truth for the build order and loop | |
| 201 | +| Git pre-commit (planned) | Rejects commits that mention "verify" / "pass" in messages when the most recent `summary.json` shows failures | |
| 202 | + |
| 203 | +If any of these layers is bypassed without a written reason, the workflow is broken — which is itself a KB entry. |
| 204 | + |
| 205 | +--- |
| 206 | + |
| 207 | +## 8. First-Run Preflight — KB in context before any code |
| 208 | + |
| 209 | +**Goal:** the first iteration on any new element should pass Stage C when its motifs are already covered by the KB. We do not re-discover known failures. |
| 210 | + |
| 211 | +Before writing any code for an element, execute this preflight: |
| 212 | + |
| 213 | +1. **Read the KB in full.** `docs/knowledge-bases/panel-model-fidelity-corrections.md` — every entry, not just the titles. |
| 214 | +2. **Enumerate applicable entries.** For the element you are about to build, list the KB entries whose motifs apply (ratio stats, segmented indicators, label strips, cyan pill tags, progress bars, etc.). |
| 215 | +3. **Inline the applicable Prevention hints.** Copy each hint verbatim into your working notes so they are in the same buffer as the code you are about to write. |
| 216 | +4. **Read §1 hard rules and §3 loop of this doc.** Confirm the build will follow them. |
| 217 | +5. **Cite KB entries in the `.ts` source.** A header comment like: |
| 218 | + ```ts |
| 219 | + // KB §1, §2 — applied: <short rationale> |
| 220 | + ``` |
| 221 | + |
| 222 | +The codegen prompt `docs/prompts/codegen.md` already encodes steps 1–5 as mandatory preflight; verify the prompt has not drifted. |
| 223 | + |
| 224 | +### Why this matters |
| 225 | + |
| 226 | +On the `full-stack-dashboard` first run, the AlarmStats sub-Panel took 20 iterations (0/4 → 4/4) because the `AccentRule` vs `ActivityIndicator` distinction and the Stat split-label grid constraint had not yet been codified. Once those became KB §1 and §2, the **same mistakes cost 0 iterations** on the next run — subsequent atoms converge in 1–3 iterations when their motif is covered. |
| 227 | + |
| 228 | +If a new element takes double-digit iterations to converge, that is itself evidence the KB is missing knowledge. Open a new entry before closing the work. |
| 229 | + |
| 230 | +--- |
| 231 | + |
| 232 | +## 9. Applying to a New Page |
| 233 | + |
| 234 | +When a new page is added (e.g., a second reference under `examples/<new-id>/`), run: |
| 235 | + |
| 236 | +``` |
| 237 | +# 1. Author artifacts |
| 238 | +docs/ui-plan/examples/<new-id>/ |
| 239 | + reference.png (committed) |
| 240 | + decomposition.md (applies 02-decomposition-process.md) |
| 241 | + regions.json (pixel-scan-verified bboxes) |
| 242 | + atoms-delta.md (proposals for new atoms) |
| 243 | + tuning-notes.md (observations) |
| 244 | +
|
| 245 | +# 2. Baselines |
| 246 | +bun run scripts/capture-before.mts <new-id> |
| 247 | +
|
| 248 | +# 3. Build each element following §3 loop |
| 249 | +# - atoms first, one at a time |
| 250 | +# - feature Panels next, one at a time |
| 251 | +# - layout last |
| 252 | +# each with its own iterations.jsonl entries |
| 253 | +
|
| 254 | +# 4. Whole-page verify |
| 255 | +bun run scripts/verify.mts <new-id> |
| 256 | +
|
| 257 | +# 5. Promote atoms / pattern updates from tuning-notes.md |
| 258 | +# (see 02-decomposition-process.md §8 feedback loop) |
| 259 | +``` |
| 260 | + |
| 261 | +No shortcuts. No bundled PRs that build multiple atoms without verifying each. No "tested the whole page at the end" as a substitute for per-element verification. |
0 commit comments