live.md

  1Interactive live variant mode: select elements in the browser, pick a design action, and get AI-generated HTML+CSS variants hot-swapped via the dev server's HMR.
  2
  3## Prerequisites
  4
  5A running dev server with hot module replacement (Vite, Next.js, Bun, etc.), OR a static HTML file open in the browser.
  6
  7## The contract (read once)
  8
  9Execute in order. No step skipped, no step reordered.
 10
 111. `live.mjs`: boot.
 122. Navigate to the URL that serves `pageFile` (infer from `package.json`, docs, terminal output, or an open tab). If you can't infer it confidently, tell the user once to open their dev/preview URL. Never use `serverPort` as that URL; it's the helper, not the app.
 133. Poll loop with the default long timeout (600000 ms). After every event or `--reply`, run `live-poll.mjs` again immediately. Never pass a short `--timeout=`.
 144. On `generate`: read screenshot if present; load the action's reference; plan three distinct directions; write all variants in one edit; `--reply done`; poll again.
 155. On `accept` / `discard`: the poll script runs `live-accept.mjs`, acknowledges the delivered event, and prints `_completionAck`. Plain accepts/discards are terminal immediately; carbonize accepts remain recoverable until you finish cleanup, run `live-complete.mjs --id EVENT_ID`, and only then poll again.
 166. If interrupted, run `live-status.mjs` or `live-resume.mjs` before guessing. The durable journal replays unacknowledged work after helper restart.
 177. On `exit`: run the cleanup at the bottom.
 18
 19Harness policy:
 20- **Claude Code**: run the poll as a **background task** (no short timeout). The harness notifies you when it completes, so the main conversation stays free. Do not block the shell.
 21- **Cursor**: run the poll in the **foreground** (blocking shell; not a background terminal, not a subagent). Cursor background terminals and subagents do not reliably resume the chat with poll stdout.
 22- **Codex**: run the poll in the **foreground** (blocking shell; not a background task, not a subagent). Codex background exec sessions do not reliably surface poll stdout back into the conversation at the moment events arrive, so a "fire-and-forget" background poll will stall live mode.
 23- **Other harnesses**: foreground unless you know stdout reliably returns to this session.
 24
 25Chat is overhead. No recap, no tutorial output, no pasting PRODUCT / DESIGN bodies. Spend tokens on tools and edits; on failure, one or two short sentences.
 26
 27## Start
 28
 29```bash
 30node .cursor/skills/impeccable/scripts/live.mjs
 31```
 32
 33Output JSON: `{ ok, serverPort, serverToken, pageFiles, hasProduct, product, productPath, hasDesign, design, designPath, migrated }`. `pageFiles` is the list of HTML entries the live script was injected into. Keep PRODUCT.md and DESIGN.md in mind for variant generation; **DESIGN.md wins on visual decisions; PRODUCT.md wins on strategic/voice decisions.** When DESIGN.md is missing, identity is **not** absent; extract it from CSS variables, computed styles, and sibling components on the page (see Step 4 Phase A). Identity preservation is the default; departure from existing identity requires an explicit trigger from PRODUCT.md anti-references or the user's freeform prompt. If `migrated: true`, the loader auto-renamed legacy `.impeccable.md` to `PRODUCT.md`; mention this once and suggest `/impeccable document` for the matching DESIGN.md.
 34
 35`serverPort` and `serverToken` belong to the small **Impeccable live helper** HTTP server (serves `/live.js`, SSE, and `/poll`). That port is **not** your dev server and is usually not the URL you open to view the app. The browser page is whatever origin serves one of the `pageFiles` entries (Vite / Next / Bun / tunnel / LAN hostname).
 36
 37If output is `{ ok: false, error: "config_missing" | "config_invalid", path }`, this project hasn't been configured for live mode (or its config is stale). See **First-time setup** at the bottom.
 38
 39## Poll loop
 40
 41```
 42LOOP:
 43  node .cursor/skills/impeccable/scripts/live-poll.mjs   # default long timeout; no --timeout=
 44  Read JSON; dispatch on "type"
 45
 46  "generate"  โ†’ Handle Generate; reply done; LOOP
 47  "accept"    โ†’ Handle Accept; complete carbonize cleanup if required; LOOP
 48  "discard"   โ†’ Handle Discard; LOOP
 49  "prefetch"  โ†’ Handle Prefetch; LOOP
 50  "timeout"   โ†’ LOOP
 51  "exit"      โ†’ break โ†’ Cleanup
 52```
 53
 54## Recovery commands
 55
 56The live helper persists an append-only journal under `.impeccable/live/sessions/`. Browser checkpoints are advisory but durable; the journal is canonical. This is local durable recovery state, not project source.
 57
 58Use these commands when the chat was interrupted, polling was missed, the helper restarted, or the browser reloaded:
 59
 60```bash
 61node .cursor/skills/impeccable/scripts/live-status.mjs
 62node .cursor/skills/impeccable/scripts/live-resume.mjs --id SESSION_ID
 63node .cursor/skills/impeccable/scripts/live-complete.mjs --id SESSION_ID
 64```
 65
 66- `live-status.mjs` prints connected helper state, active durable sessions, and queued pending events. It works even when the helper is down by reading the journal directly.
 67- `live-resume.mjs` prints the active snapshot, pending event, checkpoint phase, visible variant, parameter values, and the next safe agent action.
 68- `live-complete.mjs` is the canonical manual final acknowledgement. Use it after carbonize/manual cleanup is verified and no further poll acknowledgement will happen automatically.
 69
 70Server restart rule: start `live-server.mjs` again, then poll. Startup requeues unacknowledged pending events from the journal, so do not ask the user to click Go again unless `live-resume.mjs` says no active session exists.
 71
 72## Handle `generate`
 73
 74Event: `{id, action, freeformPrompt?, count, pageUrl, element, screenshotPath?, comments?, strokes?}`.
 75
 76Speed matters; the user is watching a spinner. Minimize tool calls by using the `wrap` helper and writing all variants in a single edit.
 77
 78### 1. Read the screenshot (if present)
 79
 80`event.screenshotPath` is **only sent when the user placed at least one comment or stroke before Go.** When present, it's an absolute path to a PNG of the element as rendered with the annotations baked in. **Read it before planning**: annotations encode user intent not recoverable from `element.outerHTML` alone.
 81
 82When `screenshotPath` is absent, don't ask for one and don't go looking for the current rendering. The omission is deliberate: without annotations, a screenshot would anchor the model on the existing design and fight the three-distinct-directions brief. Work from `element.outerHTML`, the computed styles in `event.element`, and the freeform prompt if present.
 83
 84`event.comments` and `event.strokes` carry structured metadata alongside the visual. Treat the screenshot as primary; use the structured data for specifics worth quoting (e.g. the exact text of a comment).
 85
 86Reading annotations precisely:
 87
 88- **Comment position carries meaning.** Its `{x, y}` is element-local CSS px (same coord space as `element.boundingRect`). Find the child under that point and apply the comment text LOCALLY to that sub-element. A comment near the title is about the title, not a global description.
 89- **Comments and strokes are independent annotations** unless clearly paired by overlap or tight proximity. Don't let the visual weight of a prominent stroke override the precise location of a textually-specific comment elsewhere.
 90- **Strokes are gestures; read them by shape.** Closed loop = "this thing" (emphasis / focus); arrow = direction (move / point to); cross or slash = delete; free scribble = emphasis or delete depending on context. A loop around region X means "pay attention to X," not "only change pixels inside X."
 91- **When a stroke's intent is ambiguous** (circle or arrow? emphasis or move?), state your reading in one sentence of rationale rather than silently guessing. If the uncertainty materially changes the brief, ask one short clarifying question before generating.
 92
 93### 2. Wrap the element
 94
 95```bash
 96node .cursor/skills/impeccable/scripts/live-wrap.mjs --id EVENT_ID --count EVENT_COUNT --element-id "ELEMENT_ID" --classes "class1,class2" --tag "div" --text "TEXT_SNIPPET"
 97```
 98
 99Flag mapping. Keep them separate, don't collapse into `--query`:
100
101- `--element-id` โ† `event.element.id`
102- `--classes` โ† `event.element.classes` joined with commas
103- `--tag` โ† `event.element.tagName`
104- `--text` โ† first ~80 chars of `event.element.textContent` (trim, single-line). **Pass this every call.** When the picked element shares classes + tag with sibling components (a list of `<Card>`s, repeating sections), this is what disambiguates which branch in source to wrap. Without it, wrap silently lands on the first match and may rewrite the wrong element.
105
106The helper searches ID first, then classes, then tag + class combo. If `event.pageUrl` implies the file (e.g. `/` is usually `index.html`), pass `--file PATH` to skip the search. `--query` is a fallback for raw text search only; do not use it for normal element lookups.
107
108If `--text` matches multiple candidates equally well, wrap exits with `{ error: "element_ambiguous", candidates: [...] }` and `fallback: "agent-driven"`: read the candidate line ranges, decide which one matches the picked element from page context, and write the wrapper manually per the fallback flow.
109
110Output on success: `{ file, insertLine, commentSyntax, styleMode, styleTag, cssSelectorPrefixExamples, cssAuthoring }`.
111
112`styleMode` controls how preview CSS must be authored. Treat it as a detected capability mode, not a framework guess:
113
114- `scoped`: use `@scope ([data-impeccable-variant="N"])` rules.
115- `astro-global-prefixed`: use explicit `[data-impeccable-variant="N"]` selector prefixes and the exact `styleTag` returned by the tool.
116
117Use `cssAuthoring` as the source of truth for the current file. It includes the exact `styleTag`, selector strategy, selector examples, requirements, and forbidden patterns. Do not apply a framework-specific exception unless the returned `styleMode` / `cssAuthoring.mode` says to.
118
119**Fallback errors.** Wrap only writes into files it judges to be source (tracked by git, not marked GENERATED, not listed in config's `generatedFiles`). If it can't land on a source file, it errors without writing; accepting a variant into a generated file is silent data loss. Three shapes:
120
121- `{ error: "file_is_generated", file, hint }`: user-supplied `--file` points at a generated file.
122- `{ error: "element_not_in_source", generatedMatch, hint }`: element exists only in a generated file (the next build would wipe any edits).
123- `{ error: "element_not_found", hint }`: element isn't in any project file; likely runtime-injected (JS component, dynamic render from data).
124
125All three carry `fallback: "agent-driven"`. Follow **Handle fallback** below.
126
127### 3. Load the action's reference
128
129If `event.action` is `impeccable` (the default freeform action), use SKILL.md's shared laws plus the loaded register reference (`brand.md` or `product.md`). Do not load a sub-command reference. **Freeform is not a pass to skip parameters:** you still follow the composition budget and the freeform bias in **ยง7 Parameters** below. Sub-command files list MUST-have signature knobs; freeform has no such file, so sizing knobs from surface weight and primary axes is entirely on you.
130
131Any other `event.action` (`bolder`, `quieter`, `distill`, `polish`, `typeset`, `colorize`, `layout`, `adapt`, `animate`, `delight`, `overdrive`): Read `reference/<action>.md` before planning. Each sub-command encodes a specific discipline; skipping its reference produces generic output. Those files may require specific params; layer them on top of the ยง7 budget, not instead of it.
132
133### 4. Plan three variants: identity first, then mode, then axes
134
135The wrong frame for live mode is "show three different design directions." Live runs on an existing surface; the brand has already been chosen. The job is variation **within identity**, not selection between identities. Failure mode: three editorial-typographic variants on a brief that wasn't editorial. Bigger failure mode: three off-brand variants the user can't accept because they don't look like their product.
136
137Four phases. Do them in order.
138
139#### Phase A: Extract the identity (non-skippable)
140
141The existing surface has an identity already. Read it before planning anything. Sources, in priority order:
142
1431. **DESIGN.md** if loaded: read the visual system fields (palette, type pairing, motion, components). This is the authoritative answer.
1442. **CSS custom properties** in the page's stylesheets (`:root { --color-...; --font-...; ... }`): these are de-facto tokens.
1453. **Computed styles** on the picked element and its parent: colors, fonts, spacing scales, corner radii.
1464. **Sibling components on the page**: what visual rhetoric do existing components use? (Asymmetric or centered? Dense or airy? Bold or quiet?)
147
148Write down what you see in **one sentence**. The sentence describes the surface that's actually on screen; it is not aspirational, not opinionated, not edited toward what the brand "should" be. Capture, in roughly this order:
149
150- The dominant surface color and accent color, by hex or token name (use the actual values, not categories like "warm" or "neutral").
151- The type pairing: the actual font names loaded, primary first.
152- The layout topology: how the dominant elements are arranged (stacked / side-by-side / grid / asymmetric / overlay).
153- The surface treatment: corners, borders, shadows, density of decoration.
154- The voice tone you read off the copy itself, not off the aesthetic feel.
155
156Be specific. "Modern" is not a color, "elegant" is not a type pairing, "clean" is not a layout. If you can't extract a real value for an axis, skip it rather than fabricate. The point is to record what is, not to describe what you wish it were.
157
158Do not include adjectives that name an aesthetic family ("editorial-leaning", "terminal-flavored", "brutalist"); those are conclusions, not data. They belong to Phase C lane selection in departure mode, not to identity description. Letting them sneak into Phase A is how the identity-lock collapses into a self-fulfilling prophecy.
159
160This sentence is the **identity lock**. Every variant must be readable as the same brand if rendered side by side. Skipping this phase is the primary cause of off-brand variants. Absence of DESIGN.md is never an excuse; extract from CSS and computed styles instead.
161
162#### Phase B: Pick mode (default vs departure)
163
164**Default mode**: the existing identity is preserved. Variants vary expression axes within it. *This is the right mode for ~90% of live sessions.* The user picked an element on a real product they're shipping; they expect variants of *their* hero, not three different brands' heroes.
165
166**Departure mode**: the existing identity is rejected. Variants propose alternatives consistent with PRODUCT.md voice. Trigger only when at least one is true:
167
168- PRODUCT.md anti-references explicitly call out the current surface ("the current `index.html` is itself an example"; "diffuse away from this"; "the page on screen is the failure"). Generic anti-references that describe what to avoid in general do **not** trigger departure mode; only ones that point at *this* surface specifically.
169- The user's freeform prompt explicitly asks for departure ("rebuild this from scratch", "what if it weren't editorial at all", "show me something completely different").
170
171If you're unsure, you're in default mode. The cost of being wrong about default is "three on-brand variants with similar feel": recoverable, the user picks none. The cost of being wrong about departure is "three off-brand variants": unrecoverable, the user is annoyed.
172
173#### Phase C: Plan three variants
174
175**Default mode.** Each variant commits to a different **primary axis** of difference, while preserving the identity sentence. The six axes:
176
1771. **Hierarchy**: which element commands the eye?
1782. **Layout topology**: stacked / side-by-side / grid / asymmetric / overlay
1793. **Typographic system**: pairing logic, scale ratio, case/weight strategy *within the available faces*
1804. **Color strategy**: which existing palette role carries the surface (Restrained / Committed / Full palette / Drenched). Use the brand's existing palette tokens, not new colors.
1815. **Density**: minimal / comfortable / dense
1826. **Structural decomposition**: merge, split, progressive disclosure
183
184Three variants โ†’ three DIFFERENT axes. The trio reads as *the same brand at three angles*. Do not introduce new fonts, new palette hues, or new aesthetic-family signals; those belong to departure mode.
185
186**While planning each variant, also name its 2โ€“3 parameter knobs** (per the ยง7 budget table). Parameters are part of the design, not a decoration added afterward. If the variant explores density, expose a density knob. If it explores color commitment, expose a color-amount range. Deciding "what's tunable" during planning produces better knobs than retrofitting them onto finished HTML.
187
188**Departure mode.** Each variant anchors to a different **aesthetic direction**, derived from the brand's stated voice and register in PRODUCT.md. Do NOT pick from a fixed catalog of lane categories. The right three directions for this brand are not the same as the right three for another brand, and picking from a list is itself the training-data reflex (the model selects "Swiss-grid, Terminal, Industrial-signage" every time because those are the furthest-from-editorial items in any enumerated list).
189
190Instead, work from the brand:
191
1921. Read PRODUCT.md's Brand Personality words. What physical, spatial, or material experiences would embody those words if design were not involved? (A personality described as "specific, earned, unmistakable" evokes a hand-stamped letter, a numbered print, a watchmaker's loupe. A personality described as "restless, loud, unfiltered" evokes a concert poster, a spray-painted wall, a megaphone.)
1932. From those physical experiences, derive three visual directions that are genuinely different from each other AND from the current surface you're departing.
1943. Avoid the **reflex-reject lanes** in [brand.md](brand.md). Don't trade one monoculture for another. If you find yourself reaching for "Swiss-grid" or "Terminal" or "Industrial-signage" by reflex, you are pattern-matching a catalog in your training data, not reading the brand. Start over from the personality words.
1954. Each direction must be expressible in one concrete sentence that names a real-world referent ("a museum exhibition label system for a contemporary art gallery" not "clean and minimal"). If your sentence contains only adjectives, it's not concrete enough.
1965. **While planning each direction, also name its 2โ€“3 parameter knobs** (per the ยง7 budget table). The same principle as default mode: decide "what's tunable" during planning, not after writing the HTML. A departure-mode hero with 0 parameters is not "bold creative vision," it's a missed opportunity for the user to fine-tune the direction they pick.
197
198#### Phase D: Squint test
199
200**Default mode squint.** Read each variant's identity sentence and compare to the locked identity from Phase A. If any variant has drifted to a different palette, type voice, or visual rhetoric, it has crossed into departure mode by accident; rework. Then check that each variant commits to a different primary axis. Three "tighter density" variants is failure.
201
202**Departure mode squint.** Two passes, family before sentence:
203
2041. **Family pass.** Label each variant with one design-family word of your own choosing (any concrete noun: *exhibition, storefront, cockpit, recipe-card, playbill, field-manual*). If any two variants share a label, or if the label could apply to the other variants equally well, rework. Do not use a fixed vocabulary list for the labels. *This pass is non-negotiable in departure mode and catches the monoculture failure that the sentence pass misses.*
2052. **Sentence pass.** Write three one-sentence descriptions side by side. If two of them rhyme ("both feature big type" / "both are stacks of sections" / "both center the CTA"), rework the offender.
206
207**When the primary axis is color or theme, forbid the trio from sharing theme + dominant hue.** Two dark-plus-one-dark is not distinct. Aim for three color worlds, not three shades of the same.
208
209**For action-specific invocations**, each variant must vary along the dimension the action names:
210
211- `bolder`: amplify a different dimension per variant (scale / saturation / structural change). Not three "slightly bigger" variants.
212- `quieter`: pull back a different dimension (color / ornament / spacing).
213- `distill`: remove a different class of excess (visual noise / redundant content / nested structure).
214- `polish`: target a different refinement axis (rhythm / hierarchy / micro-details like corner radii, focus states, optical kerning).
215- `typeset`: different type pairing AND different scale ratio each. Not three riffs on one pairing.
216- `colorize`: different hue family each (not shades of one hue). Vary chroma and contrast strategy.
217- `layout`: different structural arrangement (stacked / side-by-side / grid / asymmetric). Not spacing tweaks.
218- `adapt`: different target context per variant (mobile-first / tablet / desktop / print or low-data). Don't make three mobile layouts.
219- `animate`: different motion vocabulary (cascade stagger / clip wipe / scale-and-focus / morph / parallax). Not three staggered fades.
220- `delight`: different flavor of personality (unexpected micro-interaction / typographic surprise / illustrated accent / sonic-or-haptic moment / easter-egg interaction).
221- `overdrive`: different convention broken (scale / structure / motion / input model / state transitions). Skip `overdrive.md`'s "propose and ask" step; live mode is non-interactive.
222
223### 5. Apply the freeform prompt (if present)
224
225`event.freeformPrompt` is the user's ceiling on direction (all variants must honor it), but still explore meaningfully different *interpretations*. The interpretations stay within whichever mode you picked in Phase B.
226
227In **default mode**, the prompt narrows the axes you choose, not the identity. *"Make it feel more confident"* โ†’ variant 1 amplifies hierarchy (one element commands the eye), variant 2 commits the existing accent color (Committed strategy on the brand's hue), variant 3 tightens density and removes decorative slack. Three different axes, same brand.
228
229In **departure mode**, the prompt narrows the lanes you draw from, not the families. *"Make it feel like a newspaper front page"* would itself be a departure-mode prompt; honor it but pick three meaningfully different newspaper-adjacent lanes (broadsheet vs. tabloid vs. trade journal), and run the family pass to confirm they don't collapse into one.
230
231When the prompt and PRODUCT.md anti-references conflict (the prompt asks for X, the anti-references ban X), the anti-references win; they describe the brand's standing position, the prompt is one moment.
232
233### 6. Write all variants in a single edit
234
235Complete HTML replacement of the original element for each variant, not a CSS-only patch. Consider the element's context (computed styles, parent structure, CSS variables from `event.element`).
236
237Write CSS + all variants in ONE edit at the `insertLine` reported by `wrap`. Colocate CSS as a `<style>` tag inside the variant wrapper; `<style>` works anywhere in modern browsers and this ensures CSS and HTML arrive atomically (no FOUC).
238
239Use the `cssAuthoring` object returned by `live-wrap.mjs` to author the temporary preview CSS. The style opening tag shown below is the common case; replace it with `cssAuthoring.styleTag` when the tool returns a different one. The variant markup shape is otherwise stable:
240
241```html
242<!-- Variants: insert below this line -->
243<style data-impeccable-css="SESSION_ID">
244  /* rules matching cssAuthoring.rulePattern */
245</style>
246<div data-impeccable-variant="1">
247  <!-- variant 1: full element replacement (single top-level element) -->
248</div>
249<div data-impeccable-variant="2" style="display: none">
250  <!-- variant 2: full element replacement -->
251</div>
252<div data-impeccable-variant="3" style="display: none">
253  <!-- variant 3: full element replacement -->
254</div>
255```
256
257**Each variant div contains exactly one top-level element: the full replacement for the original.** Use the same tag as the original (e.g. `<section>` if the user picked a `<section>`). Loose siblings (heading + paragraph + div as direct children of the variant div) break the outline tracking and the accept flow, which both assume one child.
258
259The first variant has no `display: none` (visible by default). All others do. If variants use only inline styles and no preview CSS, omit the `<style>` tag entirely.
260
261One edit, all variants; the browser's MutationObserver picks everything up in one pass.
262
263For `styleMode: "scoped"`, author every `:scope` rule with a descendant combinator. The `@scope` boundary is the **variant wrapper `<div data-impeccable-variant="N">`**, not the element you're designing. A bare `:scope { background: cream; }` styles the wrapper, not the inner replacement, so the cream lands on a `display: contents` shell while the actual element keeps page defaults. Always step in: `:scope > .card`, `:scope > section`, `:scope .hero-title`, etc. The fake test agent's CSS in `tests/live-e2e/agent.mjs` is a faithful template; every scoped rule starts `:scope > ...`.
264
265**JSX / TSX target files.** Wrap `<style>` content in a template literal so the CSS `{` / `}` aren't parsed as JSX expressions, and use `className=` / `style={{โ€ฆ}}` on every variant element. Keep `data-impeccable-*` attributes as-is; they're plain strings:
266
267```tsx
268<style data-impeccable-css="SESSION_ID">{`
269  @scope ([data-impeccable-variant="1"]) { ... }
270  @scope ([data-impeccable-variant="2"]) { ... }
271`}</style>
272<div data-impeccable-variant="1">
273  {/* variant 1 */}
274</div>
275<div data-impeccable-variant="2" style={{ display: 'none' }}>
276  {/* variant 2 */}
277</div>
278```
279
280The wrap script already gives you a single-rooted JSX wrapper: a `<div data-impeccable-variants="โ€ฆ">` outer element with the marker comments tucked inside. Drop the variants block above into the "Variants: insert below this line" comment and the source stays valid TSX.
281
282### 7. Parameters (composition-sized, 0โ€“4 per variant)
283
284Each variant can expose **coarse** knobs alongside the full HTML/CSS replacement. The browser docks a small panel to the right of the outline with one control per parameter. The user drags/clicks and sees instant feedback: there is zero regeneration cost because the knob toggles a CSS variable or data attribute that the variant's scoped CSS is already authored against.
285
286**What โ€œoptionalโ€ does not mean.** Parameters are not nice-to-have decoration on large work. The word meant โ€œomit controls that are redundant or cosmetic,โ€ not โ€œdefault to zero because three variants were enough work.โ€
287
288**When to add.** As soon as the variantโ€™s scoped CSS has a meaningful continuous or stepped axis: density, color amount, type scale, motion intensity, column weight, and so on. If you can imagine the user muttering โ€œa bit tighterโ€ or โ€œa touch more accentโ€ **without** wanting a full regeneration, wire that axis. **Not** micro-margins or one-off nudges; those are not parameters.
289
290**Freeform (`action` is `impeccable`) bias.** You did not load a sub-command reference, so you must **choose** signature axes yourself. Match the budget table: for a hero or large composition, that means **2โ€“3 axes per variant**, not 1. Prefer knobs that sit on the dimensions where your three variants actually differ (if density varies, expose it as a `steps` knob; if color commitment varies, expose it as a `range`). A hero that ships with **0** params is almost always a mistake, not a judgment call. A hero with exactly **1** param is underweight unless the design is genuinely a fixed-point comparison. Start from the budget table, not from zero.
291
292**Budget scales with the element's visual weight, not token budget.** Knobs need real estate to read as tunable; three sliders on a single control are noise.
293
294- **Leaf / tiny**: a single button, icon, input, bare heading, solitary paragraph: **0 params.**
295- **Small composition**: labeled input, simple card, short callout (โ‰ค ~5 visual children): **0โ€“1** params when one dominant axis is obvious; otherwise **0.**
296- **Medium composition**: section component, nav cluster, dense card, short feature block (6โ€“15 visual children): **target 2**; **1** is acceptable if the block is simple; **0** only when variants are truly fixed points.
297- **Large composition**: hero section, full page region, spread layout, strong internal structure (16+ visual children or multiple sub-sections): **target 2โ€“3**; **up to 4** when several independent axes (e.g. structure `steps` + `density` + one accent) are all authored in scoped CSS.
298
299**When in doubt, ask whether a dial exists before defaulting to zero.** The user can always request more variants, but the point of live mode is instant tuning without another Go. Crowding the panel is bad; **under-shipping** knobs on a dense composition is the more common failure for freeform. Count by **visual** children, not DOM depth; a shallow-but-wide hero is still large.
300
301**Hard cap per variant**: at most **four** parameters so the panel stays legible; rare fifth only if the reference explicitly allows it.
302
303**How to declare.** Put a JSON manifest on the variant wrapper:
304
305```html
306<div data-impeccable-variant="1" data-impeccable-params='[
307  {"id":"color-amount","kind":"range","min":0,"max":1,"step":0.05,"default":0.5,"label":"Color amount"},
308  {"id":"density","kind":"steps","default":"snug","label":"Density","options":[
309    {"value":"airy","label":"Airy"},
310    {"value":"snug","label":"Snug"},
311    {"value":"packed","label":"Packed"}
312  ]},
313  {"id":"serif","kind":"toggle","default":false,"label":"Serif display"}
314]'>
315  ...variant content...
316</div>
317```
318
319**Three kinds:**
320
321- `range`: smooth slider. Drives a CSS custom property `--p-<id>` on the variant wrapper. Author CSS with `var(--p-color-amount, 0.5)`. Fields: `min`, `max`, `step`, `default` (number), `label`.
322- `steps`: segmented radio. Drives a data attribute `data-p-<id>` on the variant wrapper. Author CSS with `:scope[data-p-density="airy"] .grid { ... }`. Fields: `options` (array of `{value, label}`), `default` (string), `label`.
323- `toggle`: on/off switch. Drives BOTH a CSS var (`--p-<id>: 0|1`) and a data attribute (present when on, absent when off). Use whichever is more convenient. Fields: `default` (boolean), `label`.
324
325**Signature params per action.** For named sub-commands, read that actionโ€™s `reference/<action>.md` for one or two **MUST** params (e.g. `layout` โ†’ `density`). Those are non-negotiable when the design can express them. **Freeform has no file-level MUST**; the **Freeform (`impeccable`) bias** in this section is the stand-in. If the userโ€™s action is both stylized and sub-command (e.g. `colorize`), the sub-commandโ€™s MUST list takes precedence for its axes; still respect the **Hard cap** and add no redundant duplicate knobs.
326
327**Reset on variant switch.** User dials density on v1, flips to v2, v2 starts at v2's declared defaults. Known limitation; preservation across variants may land later.
328
329**On accept**, the browser sends the user's current values in the accept event. `live-accept.mjs` writes them as a sibling comment:
330
331```html
332<!-- impeccable-param-values SESSION_ID: {"color-amount":0.7,"density":"packed"} -->
333```
334
335The carbonize cleanup step (see below) reads that comment and bakes the chosen values into the final CSS. For `steps`/`toggle` attribute selectors: keep only the branch matching the chosen value, drop the others, collapse `:scope[data-p-density="packed"] .grid` to a semantic class rule. For `range` vars: either substitute the literal or keep the var with the chosen value as its new default.
336
337### 8. Signal done
338
339```bash
340node .cursor/skills/impeccable/scripts/live-poll.mjs --reply EVENT_ID done --file RELATIVE_PATH
341```
342
343`RELATIVE_PATH` is relative to project root (`public/index.html`, `src/App.tsx`, etc.); the browser fetches source directly if the dev server lacks HMR.
344
345Then run `live-poll.mjs` again immediately.
346
347### Aborting an in-flight session
348
349If wrap or generation fails after the browser has flipped to GENERATING (e.g. wrap landed on the wrong source branch and you've already reverted it, or generation hit an unrecoverable error), tell the **browser** so its bar resets to PICKING:
350
351```bash
352node .cursor/skills/impeccable/scripts/live-poll.mjs --reply EVENT_ID error "Short reason"
353```
354
355Don't run `live-accept --discard` for this; that's a pure file mutator, the browser doesn't see it, and the bar gets stuck on the GENERATING dots forever (the user has to refresh). `--discard` is only correct when the **browser** initiated the discard (user clicked โœ• during CYCLING) and the agent is just running source-side cleanup the browser already triggered.
356
357## Handle fallback
358
359When wrap returns `fallback: "agent-driven"`, the deterministic flow doesn't apply. Pick up here.
360
361The goal is the same: give the user three variants to choose from AND persist the accepted one in a place the next build won't wipe. The difference is that you have to pick the right source file yourself.
362
363### Step 1: Identify where the element actually lives
364
365Use the error payload:
366
367- `element_not_in_source` with `generatedMatch: "public/docs/foo.html"`: the served HTML is generated. Find the generator (grep for writers of that path, e.g. `scripts/build-sub-pages.js`, an Astro/Next template) and locate the template or partial that emits this element.
368- `element_not_found`: the element is runtime-injected. Look for the component that renders it (React/Vue/Svelte), the JS that assembles it, or the data source that feeds it.
369- `file_is_generated` with `file: "..."`: user pointed at a generated file explicitly. Same resolution as `element_not_in_source`.
370
371Read the candidate source until you're confident where a change to the element would belong. If the change is purely visual, that source might be a shared stylesheet, not the template.
372
373### Step 2: Show three variants in the DOM for preview
374
375The browser bar is waiting for variants. Even without a wrapper in source, you still need to show something:
376
3771. Manually write the wrapper scaffold into the **served** file (the one the browser actually loaded). Use the same structure `live-wrap.mjs` produces; `<!-- impeccable-variants-start ID --><div data-impeccable-variants="ID" data-impeccable-variant-count="3" style="display: contents">โ€ฆ</div><!-- end -->`.
3782. Insert your three variant divs inside it, same shape as the deterministic path.
3793. Signal done with `--reply EVENT_ID done --file <served file>`. The browser's no-HMR fallback will fetch and inject.
380
381This served-file edit is **temporary**: next regen wipes it, and that's fine. The real work happens on accept.
382
383### Step 3: On accept, write to true source
384
385When the accept event arrives (`_acceptResult.handled` will usually be `false` here because accept also refuses to persist into generated files; see Handle accept for the carbonize branch), extract the accepted variant's content and write it into the source you identified in Step 1:
386
387- Structural change โ†’ edit the template / component source.
388- Visual-only change โ†’ add or update rules in the appropriate stylesheet; remove the inline `<style>` scope.
389- Dynamic from data โ†’ update the data source or the render logic.
390
391Then remove the temporary wrapper from the served file if it's still there.
392
393### Step 4: On discard, clean up the served file
394
395Remove the wrapper you inserted in Step 2. Nothing else to do.
396
397## Handle `accept`
398
399Event: `{id, variantId, _acceptResult, _completionAck}`. The poll script already ran `live-accept.mjs` to handle the file operation deterministically, then acknowledged event delivery to the helper. The browser DOM is already updated.
400
401- `_completionAck.ok !== true`: do not poll yet. Run `live-status.mjs` / `live-resume.mjs`, complete the cleanup manually if needed, then run `live-complete.mjs --id EVENT_ID`.
402- `_acceptResult.handled: true` and `carbonize: false`: nothing to do. Poll again.
403- `_acceptResult.handled: true` and `carbonize: true`: **post-accept cleanup is required before the next poll.** See the "Required after accept (carbonize)" section below. The `event._acceptResult.todo` field, `_completionAck.requiresComplete`, and a stderr banner all point at this required follow-up; none are decorative. After cleanup, run `live-complete.mjs --id EVENT_ID`, then poll again.
404- `_acceptResult.handled: false, mode: "fallback"`: the session lived in a generated file and the script refused to persist there. You've already written the accepted variant into true source during Handle fallback Step 3; just clean up the temporary wrapper in the served file if any, and poll again.
405- `_acceptResult.handled: false` without `mode`: manual cleanup: read file, find markers, edit.
406
407### Required after accept (carbonize)
408
409When `_acceptResult.carbonize === true`, the accepted variant was stitched into source with helper markers and inline CSS so the browser can render it immediately with no visual gap. That stitch-in is **temporary**. The agent must rewrite it into permanent form before doing anything else. Skipping this leaves dead `@scope` rules for unaccepted variants, a pointless `data-impeccable-variant` wrapper, and `impeccable-carbonize-start/end` comment noise in the source file; all of which accumulate across sessions.
410
411Do these five steps in the current thread, synchronously, before the next poll. Do not poll again until the file is clean.
412
4131. **Locate the carbonize block** in the source file (`_acceptResult.file`). It's bracketed by `<!-- impeccable-carbonize-start SESSION_ID -->` and `<!-- impeccable-carbonize-end SESSION_ID -->` and contains a `<style data-impeccable-css="SESSION_ID">` element. If the variant declared parameters, an `<!-- impeccable-param-values SESSION_ID: {...} -->` comment sits alongside the style tag with the user's chosen values; read it first; it drives steps 3 and 4 below.
4142. **Move the CSS rules** into the project's real stylesheet. Which stylesheet depends on the project (e.g. `site/styles/workflow.css` for an Astro project, or the component's co-located CSS file for a Vite/Next project; pick whichever already owns styling for the surrounding element).
4153. **Bake in parameter values while rewriting selectors.** For `@scope ([data-impeccable-variant="N"])` wrappers: retarget to real, semantic classes on the accepted HTML (`.why-visual--v2 .v2-label { โ€ฆ }`). For `:scope[data-p-<id>="VALUE"]` selectors: keep only the branch matching the chosen value from the param-values comment; drop the others (they're dead after accept). For `var(--p-<id>, DEFAULT)` in the CSS: either substitute the literal value, or if the param is still useful as a knob going forward, leave the var and update its initial declaration to the chosen value.
4164. **Unwrap the accepted content.** Delete the `<div data-impeccable-variant="N" style="display: contents">` that wraps it. Drop `data-impeccable-params` and any `data-p-*` attributes from it; those are live-mode plumbing, not source.
4175. **Delete the inline `<style>` block, the `<!-- impeccable-param-values -->` comment if present, and both `<!-- impeccable-carbonize-start/end -->` markers.** Also drop any `@scope` rules for variants other than the accepted one; those are dead code now.
418
419After the file is clean, run `live-complete.mjs --id SESSION_ID`, verify it reports `phase: "completed"`, then poll again.
420
421A background agent may be used for the rewrite, but the current thread is responsible for verifying the five steps are complete before issuing the next poll. In practice, inline is usually faster and less error-prone.
422
423## Handle `discard`
424
425Event: `{id, _acceptResult, _completionAck}`. The poll script already restored the original, removed all variant markers, and acknowledged `discarded` durable completion. Nothing to do unless `_completionAck.ok !== true`; in that case run `live-complete.mjs --id EVENT_ID --discarded`, then poll again.
426
427## Handle `prefetch`
428
429Event: `{pageUrl}`. The browser fires this the first time the user selects an element on a given route, as a latency shortcut; it signals the user is likely about to Go on a page you haven't read yet.
430
431Resolve `pageUrl` to the underlying file:
432
433- Root `/` โ†’ the `pageFile` returned by `live.mjs` (usually `public/index.html` or equivalent).
434- Sub-routes (e.g. `/docs`, `/docs/live`) โ†’ the generated or source file for that route. Use your knowledge of the project layout (multi-page static sites often resolve `/foo` โ†’ `public/foo/index.html`; SPAs may map all routes to a single entry).
435
436Read the file into context, then poll again. No `--reply`: this is speculative pre-work; Go will come later. If you can't confidently resolve the route to a file, skip and poll again.
437
438Dedupe is the browser's job (one prefetch per unique pathname per session); trust it. If the same file shows up twice from different routes mapping to the same file, the second Read is cached anyway.
439
440## Exit
441
442The user can stop live mode by:
443- Saying "stop live mode" / "exit live" in chat
444- Closing the browser tab (SSE drops, poll returns `exit` after 8s)
445- The browser's exit button
446
447When the poll returns `exit`, proceed to cleanup. If the poll is still running as a background task, kill it first.
448
449## Cleanup
450
451```bash
452node .cursor/skills/impeccable/scripts/live-server.mjs stop
453```
454
455Stops the HTTP server and runs `live-inject.mjs --remove` to strip `localhost:โ€ฆ/live.js` from the HTML entry. To stop the server but keep the inject tag (for a quick restart), use `stop --keep-inject`. `.impeccable/live/config.json` persists as project config for future sessions.
456
457Then:
458- Remove any leftover variant wrappers (search for `impeccable-variants-start` markers).
459- Remove any leftover carbonize blocks (search for `impeccable-carbonize-start` markers).
460
461## First-time setup (config missing or invalid)
462
463If `live.mjs` outputs `{ ok: false, error: "config_missing" | "config_invalid", path }`, write the live config at the reported path. By default this is `.impeccable/live/config.json`.
464
465Schema:
466
467```json
468{
469  "files": ["<path-or-glob>", "<path-or-glob>", ...],
470  "exclude": ["<optional-glob>", ...],
471  "insertBefore": "</body>",
472  "commentSyntax": "html",
473  "cspChecked": true
474}
475```
476
477`files` is the inject target; **the HTML files the browser actually loads**, not necessarily source. Each entry is either a literal path (`"public/index.html"`) or a glob pattern (`"public/**/*.html"`). Tracked or generated doesn't matter here; wrap has its own generated-file guard and routes accepts through the fallback flow.
478
479`exclude` (optional) is a list of glob patterns matching files to skip, even if a `files` glob would have included them. Use for email templates, demo fixtures, or any HTML that isn't a live page.
480
481`cspChecked` tracks whether the CSP detection step below has already run. Absent on first setup; set to `true` after CSP is checked (whether patched, declined, or not needed).
482
483**Hard-excluded paths (cannot be overridden).** `**/node_modules/**` and `**/.git/**` are never matched regardless of what the user writes. These are vendor/metadata directories and injecting into them would silently instrument third-party code.
484
485**Glob syntax.** `**` matches any number of path segments (including zero), `*` matches any characters except `/`, `?` matches a single character except `/`. Paths are always relative to the project root with forward slashes.
486
487| Framework | `files` | `insertBefore` | `commentSyntax` |
488|-----------|---------|----------------|-----------------|
489| SPA with single shell (Vite / React / Plain HTML) | `["index.html"]` | `</body>` | `html` |
490| Next.js (App Router) | `["app/layout.tsx"]` | `</body>` | `jsx` |
491| Next.js (Pages) | `["pages/_document.tsx"]` | `</body>` | `jsx` |
492| Nuxt | `["app.vue"]` | `</body>` | `html` |
493| Svelte / SvelteKit | `["src/app.html"]` | `</body>` | `html` |
494| Astro | `[" <root layout .astro>"]` | `</body>` | `html` |
495| Multi-page (separate HTML per route) | `["public/**/*.html"]`: a glob covering the served directory | `</body>` | `html` |
496
497Pick an anchor that exists in every file (`</body>` almost always works). Use `insertAfter` if the anchor should match **after** a specific line.
498
499For multi-page sites, **prefer a glob over a literal file list**. New pages added later are picked up automatically on the next `live-inject.mjs` run; no config maintenance needed.
500
501For multi-page sites whose pages are *rebuilt* by a generator (Astro, static-site generators, custom scripts like `build-sub-pages.js`), the inject survives only until the next regeneration. Re-run `live.mjs` after each build. Accept is unaffected; it writes to true source via the fallback flow.
502
503### Drift-heal warning
504
505On every `live.mjs` boot, after inject, the project is scanned for HTML files under common page-source roots (`public/`, `src/`, `app/`, `pages/`). If any exist that aren't covered by the resolved `files` list, the output includes a `configDrift` field:
506
507```json
508{
509  "ok": true,
510  "serverPort": 8400,
511  "pageFiles": [ "..." ],
512  "configDrift": {
513    "orphans": ["public/new-section/index.html", "public/docs/new-command.html"],
514    "orphanCount": 2,
515    "hint": "2 HTML file(s) exist but aren't in config.files. Consider adding them, or use a glob pattern like \"public/**/*.html\"."
516  }
517}
518```
519
520When `configDrift` is present, surface it to the user once per session before entering the poll loop:
521
522> Noticed N HTML file(s) in the project that aren't in `config.files`:
523>
524> - `public/new-section/index.html`
525> - `public/docs/new-command.html`
526>
527> Add them, or switch `files` to a glob like `["public/**/*.html"]` and let it track new pages automatically?
528
529Don't auto-update the config; let the user decide. `configDrift` is `null` when there's no drift.
530
531### CSP detection (first-time only)
532
533If `config.cspChecked === true`, skip this entire section. You already asked this user once; the answer sticks.
534
535Otherwise, run the detection helper:
536
537```bash
538node .cursor/skills/impeccable/scripts/detect-csp.mjs
539```
540
541Output: `{ shape, signals }` where `shape` is one of `append-arrays`, `append-string`, `middleware`, `meta-tag`, or `null`. The shape is named by *patch mechanism*, so one template covers many frameworks.
542
543- **`null`**: no CSP; skip to writing `.impeccable/live/config.json` with `cspChecked: true`.
544- **`append-arrays`**: CSP defined as structured directive arrays. Auto-patchable. See *append-arrays* below. Covers:
545  - Monorepo helpers with `additionalScriptSrc` / `additionalConnectSrc` options (Next.js + shared config package)
546  - SvelteKit `kit.csp.directives`
547  - Nuxt `nuxt-security` module's `contentSecurityPolicy`
548- **`append-string`**: CSP written as a literal value string. Auto-patchable. See *append-string* below. Covers:
549  - Inline `next.config.*` `headers()` with a CSP literal
550  - Nuxt `routeRules` / `nitro.routeRules` headers
551- **`middleware`** or **`meta-tag`**: rarer. Detected but not auto-patched in v1. Show the user the detected files and ask them to add `http://localhost:8400` to `script-src` and `connect-src` manually, then mark `cspChecked: true` and proceed.
552
553#### Consent prompt template
554
555Use this phrasing so the experience is consistent across agents:
556
557> **CSP patch needed.** I detected a Content Security Policy in your project that blocks `http://localhost:8400`: the live picker won't load without an allowance. Here's the change I'd make:
558>
559> ```diff
560> [file: <patchTarget>]
561> [exact diff, 2โ€“5 lines]
562> ```
563>
564> It's guarded by `NODE_ENV === "development"` so the extra entry only appears in dev and never reaches production. You can remove it any time by reverting this file. Apply? [y/n]
565
566On "no": skip the patch, mention live won't work until the user adds the allowance manually, still write `cspChecked: true` (the question's been asked).
567
568On "yes": apply the Shape-specific patch below, then write `cspChecked: true`.
569
570#### append-arrays
571
572CSP expressed as structured directive arrays. Patch mechanism: declare a dev-only array, spread it into the script-src and connect-src arrays.
573
574**Declare near the top of the file that holds the CSP arrays:**
575
576```ts
577// Dev-only allowance so impeccable live mode can load. Guarded by NODE_ENV.
578const __impeccableLiveDev =
579  process.env.NODE_ENV === "development" ? ["http://localhost:8400"] : [];
580```
581
582**Append `...__impeccableLiveDev` to the script-src and connect-src directive arrays.** Per-framework specifics:
583
584- **Next.js + monorepo helper**: edit the *app's* `next.config.*` (not the shared helper), appending to `additionalScriptSrc` and `additionalConnectSrc` passed into `createBaseNextConfig` (or equivalent). Keeps the shared package clean.
585- **SvelteKit**: edit `svelte.config.js`, appending to `kit.csp.directives['script-src']` and `kit.csp.directives['connect-src']`.
586- **Nuxt + nuxt-security**: edit `nuxt.config.*`, appending to `security.headers.contentSecurityPolicy['script-src']` and `['connect-src']`.
587
588Reference outputs:
589- `tests/framework-fixtures/nextjs-turborepo/expected-after-patch.ts` (Next.js)
590- `tests/framework-fixtures/sveltekit-csp/expected-after-patch.js` (SvelteKit)
591
592Idempotency: if `__impeccableLiveDev` already exists in the file, the patch is already applied; skip asking and just mark `cspChecked: true`.
593
594#### append-string
595
596CSP built as a literal value string. Two-point patch: declare a dev-only string near the top, interpolate it into the CSP at the `script-src` and `connect-src` directives.
597
598```ts
599// Dev-only allowance so impeccable live mode can load.
600const __impeccableLiveDev =
601  process.env.NODE_ENV === "development" ? " http://localhost:8400" : "";
602```
603
604Then in the CSP value string:
605- `script-src 'self' 'unsafe-inline'` โ†’ `` `script-src 'self' 'unsafe-inline'${__impeccableLiveDev}` ``
606- `connect-src 'self'` โ†’ `` `connect-src 'self'${__impeccableLiveDev}` ``
607
608(Leading space on the dev string so it concatenates cleanly into the existing value. Convert the literal CSP directives into template strings as part of the edit if they aren't already.)
609
610Per-framework specifics:
611- **Next.js inline `headers()`**: edit `next.config.*`, splicing the variable into the CSP value.
612- **Nuxt `routeRules`**: edit `nuxt.config.*`, splicing into the CSP in `routeRules['/**'].headers['Content-Security-Policy']`.
613
614Reference outputs:
615- `tests/framework-fixtures/nextjs-inline-csp/expected-after-patch.js` (Next.js)
616- `tests/framework-fixtures/nuxt-csp/expected-after-patch.ts` (Nuxt)
617
618### Troubleshooting
619
620If a user says "no" to the CSP patch at setup time and later complains that live doesn't work: their dev CSP blocks `http://localhost:8400`. Fix: delete `cspChecked` from `.impeccable/live/config.json` and re-run `live.mjs`: setup will ask again.
621
622Then re-run `live.mjs`.