canicode 0.11.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -78,8 +78,189 @@ ${footer}`;
78
78
  }
79
79
  }
80
80
 
81
+ // src/core/roundtrip/annotation-payload.ts
82
+ var CANICODE_JSON_FENCE = "```canicode-json";
83
+ function formatIntentValueForDisplay(value) {
84
+ if (value === void 0) return "undefined";
85
+ if (value === null) return "null";
86
+ if (typeof value === "object") {
87
+ try {
88
+ return `\`${JSON.stringify(value)}\``;
89
+ } catch {
90
+ return String(value);
91
+ }
92
+ }
93
+ return `\`${String(value)}\``;
94
+ }
95
+ function buildCodegenDirective(args) {
96
+ const { sceneNodeId, intent } = args;
97
+ const val = intent.value === void 0 ? "undefined" : JSON.stringify(intent.value);
98
+ return `When generating code for node ${sceneNodeId}, set ${intent.field} to ${val} (user intent, scope: ${intent.scope}). Prefer this over the current Figma scene value when they disagree.`;
99
+ }
100
+ function sceneOutcomeToAck(result, reason) {
101
+ return reason !== void 0 ? { result, reason } : { result };
102
+ }
103
+ function buildOutcomeHumanLine(args) {
104
+ if (args.skippedDefinitionDueToAdr012) {
105
+ const adrHint = " Canicode skipped writing the source component without `allowDefinitionWrite: true` (ADR-012 safer default). The instance-level change did not apply as intended in the scene.";
106
+ if (args.reason === "silent-ignore") {
107
+ return "**Scene write outcome:** The write ran, but the property value did not change on this instance (silent-ignore)." + adrHint;
108
+ }
109
+ return "**Scene write outcome:** Figma rejected an instance-level change" + (args.errorMessage ? `: ${args.errorMessage}` : "") + "." + adrHint;
110
+ }
111
+ if (args.reason === "silent-ignore") {
112
+ return "**Scene write outcome:** The write ran, but the property value did not change on this instance (silent-ignore). No source definition was available to escalate.";
113
+ }
114
+ if (args.reason === "override-error") {
115
+ return "**Scene write outcome:** Figma rejected an instance-level change" + (args.errorMessage ? `: ${args.errorMessage}` : "") + ". No source definition was available to escalate.";
116
+ }
117
+ return "**Scene write outcome:** Could not apply automatically" + (args.errorMessage ? `: ${args.errorMessage}` : "") + ".";
118
+ }
119
+ function buildAdr012PropagationParagraph(args) {
120
+ const { componentName, replicaCount } = args;
121
+ const fanOutHint = typeof replicaCount === "number" && replicaCount >= 2 ? ` This batched question covers ${replicaCount} instance scenes \u2014 changing **${componentName}** at the definition still affects every inheriting instance, not just one row in the batch.` : "";
122
+ return `Canicode's safer default (ADR-012) is to skip writing the source component **${componentName}** without explicit opt-in, because that write propagates to every non-overridden instance of **${componentName}** in the file.${fanOutHint} Prefer a manual override on **this** instance when you only need a local fix. Use \`allowDefinitionWrite: true\` only when you intend to change **${componentName}** for all inheriting instances \u2014 it is not a neutral shortcut for a single-instance tweak.`;
123
+ }
124
+ function buildDefinitionWriteSkippedBody(args) {
125
+ const {
126
+ ruleId,
127
+ sceneNodeId,
128
+ componentName,
129
+ reason,
130
+ errorMessage,
131
+ replicaCount,
132
+ intent
133
+ } = args;
134
+ const ackIntent = intent ? {
135
+ field: intent.field,
136
+ value: intent.value,
137
+ scope: intent.scope
138
+ } : void 0;
139
+ const sceneWriteOutcome = sceneOutcomeToAck("user-declined-propagation", "adr-012-opt-in-disabled");
140
+ const codegenDirective = intent !== void 0 ? buildCodegenDirective({ sceneNodeId, intent }) : void 0;
141
+ const jsonBlock = {
142
+ v: 1,
143
+ ruleId,
144
+ nodeId: sceneNodeId,
145
+ ...ackIntent ? { intent: ackIntent } : {},
146
+ sceneWriteOutcome,
147
+ ...codegenDirective ? { codegenDirective } : {}
148
+ };
149
+ const userAnswerLine = intent !== void 0 ? `**User answered:** ${formatIntentValueForDisplay(intent.value)} for **${intent.field}** (scope: ${intent.scope}).` : null;
150
+ const outcomeLine = buildOutcomeHumanLine({
151
+ reason,
152
+ ...errorMessage !== void 0 ? { errorMessage } : {},
153
+ skippedDefinitionDueToAdr012: true
154
+ });
155
+ const adrBlock = buildAdr012PropagationParagraph({
156
+ componentName,
157
+ ...replicaCount !== void 0 ? { replicaCount } : {}
158
+ });
159
+ const proseParts = [userAnswerLine, outcomeLine, adrBlock].filter(
160
+ (p) => p !== null
161
+ );
162
+ const prose = proseParts.join("\n\n");
163
+ return appendJsonFenceAndFooter(prose, jsonBlock, ruleId);
164
+ }
165
+ function buildNoDefinitionFallbackBody(args) {
166
+ const { ruleId, sceneNodeId, reason, errorMessage, intent } = args;
167
+ const ackIntent = intent ? { field: intent.field, value: intent.value, scope: intent.scope } : void 0;
168
+ const outcomeResult = reason === "silent-ignore" ? "silent-ignored" : reason === "override-error" ? "api-rejected" : "api-rejected";
169
+ const sceneWriteOutcome = sceneOutcomeToAck(
170
+ outcomeResult,
171
+ reason === "silent-ignore" ? "silent-ignore-no-definition" : "no-definition-escalation"
172
+ );
173
+ const codegenDirective = intent !== void 0 ? buildCodegenDirective({ sceneNodeId, intent }) : void 0;
174
+ const jsonBlock = {
175
+ v: 1,
176
+ ruleId,
177
+ nodeId: sceneNodeId,
178
+ ...ackIntent ? { intent: ackIntent } : {},
179
+ sceneWriteOutcome,
180
+ ...codegenDirective ? { codegenDirective } : {}
181
+ };
182
+ const userAnswerLine = intent !== void 0 ? `**User answered:** ${formatIntentValueForDisplay(intent.value)} for **${intent.field}** (scope: ${intent.scope}).` : null;
183
+ const outcomeLine = buildOutcomeHumanLine({
184
+ reason,
185
+ ...errorMessage !== void 0 ? { errorMessage } : {},
186
+ skippedDefinitionDueToAdr012: false
187
+ });
188
+ const prose = [userAnswerLine, outcomeLine].filter((p) => p !== null).join("\n\n");
189
+ return appendJsonFenceAndFooter(prose, jsonBlock, ruleId);
190
+ }
191
+ function buildDefinitionTierFailureBody(args) {
192
+ const { ruleId, sceneNodeId, intent, kind, errorMessage } = args;
193
+ const sceneWriteOutcome = sceneOutcomeToAck(
194
+ kind === "read-only-library" ? "api-rejected" : "api-rejected",
195
+ kind === "read-only-library" ? "definition-read-only" : "definition-write-failed"
196
+ );
197
+ const codegenDirective = intent !== void 0 ? buildCodegenDirective({ sceneNodeId, intent }) : void 0;
198
+ const jsonBlock = {
199
+ v: 1,
200
+ ruleId,
201
+ nodeId: sceneNodeId,
202
+ ...intent ? {
203
+ intent: {
204
+ field: intent.field,
205
+ value: intent.value,
206
+ scope: intent.scope
207
+ }
208
+ } : {},
209
+ sceneWriteOutcome,
210
+ ...codegenDirective ? { codegenDirective } : {}
211
+ };
212
+ const human = kind === "read-only-library" ? "source component lives in an external library and is read-only from this file \u2014 apply the fix in the library file itself." : `could not apply at source definition: ${errorMessage}`;
213
+ const userAnswerLine = intent !== void 0 ? `**User answered:** ${formatIntentValueForDisplay(intent.value)} for **${intent.field}** (scope: ${intent.scope}).` : null;
214
+ const outcomeLine = `**Scene write outcome:** ${human}`;
215
+ const prose = [userAnswerLine, outcomeLine].filter((p) => p !== null).join("\n\n");
216
+ return appendJsonFenceAndFooter(prose, jsonBlock, ruleId);
217
+ }
218
+ function appendJsonFenceAndFooter(prose, jsonBlock, ruleId) {
219
+ const footer = `\u2014 *${ruleId}*`;
220
+ const hasIntent = jsonBlock.intent !== void 0;
221
+ if (!hasIntent) {
222
+ return `${prose}
223
+
224
+ ${footer}`;
225
+ }
226
+ const jsonText = JSON.stringify(jsonBlock, null, 0);
227
+ return `${prose}
228
+
229
+ ${CANICODE_JSON_FENCE}
230
+ ${jsonText}
231
+ \`\`\`
232
+
233
+ ${footer}`;
234
+ }
235
+ var FENCED_JSON_RE = new RegExp(
236
+ `${CANICODE_JSON_FENCE.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*([\\s\\S]*?)\\s*\`\`\``,
237
+ "m"
238
+ );
239
+ function parseCanicodeJsonPayloadFromMarkdown(text) {
240
+ const m = FENCED_JSON_RE.exec(text);
241
+ if (!m?.[1]) return void 0;
242
+ try {
243
+ const raw = JSON.parse(m[1].trim());
244
+ if (!raw || typeof raw !== "object") return void 0;
245
+ const o = raw;
246
+ if (o.v !== 1 || typeof o.ruleId !== "string") return void 0;
247
+ return raw;
248
+ } catch {
249
+ return void 0;
250
+ }
251
+ }
252
+
81
253
  // src/core/roundtrip/apply-with-instance-fallback.ts
82
254
  var DEFINITION_WRITE_SKIPPED_EVENT = "cic_roundtrip_definition_write_skipped";
255
+ function categoryIdForAnnotate(categories, kind, roundtripIntent) {
256
+ if (kind === "adr012-definition-skipped") {
257
+ return categories.fallback;
258
+ }
259
+ if (roundtripIntent !== void 0) {
260
+ return categories.gotcha;
261
+ }
262
+ return categories.flag;
263
+ }
83
264
  function resolveSourceComponentName(definition, question) {
84
265
  if (definition && typeof definition.name === "string" && definition.name) {
85
266
  return definition.name;
@@ -93,11 +274,24 @@ ${footer}`;
93
274
  async function routeToDefinitionOrAnnotate(definition, writeFn, ctx) {
94
275
  if (definition && !ctx.allowDefinitionWrite && ctx.reason !== "non-override-error") {
95
276
  const componentName = resolveSourceComponentName(definition, ctx.question);
277
+ const replicaCount = typeof ctx.question.replicas === "number" && Number.isInteger(ctx.question.replicas) ? ctx.question.replicas : void 0;
96
278
  if (ctx.categories) {
97
279
  upsertCanicodeAnnotation(ctx.scene, {
98
280
  ruleId: ctx.question.ruleId,
99
- markdown: `The fix below could not be applied on this instance child \u2014 the property silently ignored the write or the override was rejected. Apply it on the source component **${componentName}** so every instance picks it up. Re-run with \`allowDefinitionWrite: true\` to let canicode propagate automatically.`,
100
- categoryId: ctx.categories.fallback
281
+ markdown: buildDefinitionWriteSkippedBody({
282
+ ruleId: ctx.question.ruleId,
283
+ sceneNodeId: ctx.scene.id,
284
+ componentName,
285
+ reason: ctx.reason,
286
+ ...ctx.errorMessage !== void 0 ? { errorMessage: ctx.errorMessage } : {},
287
+ ...replicaCount !== void 0 ? { replicaCount } : {},
288
+ ...ctx.roundtripIntent !== void 0 ? { intent: ctx.roundtripIntent } : {}
289
+ }),
290
+ categoryId: categoryIdForAnnotate(
291
+ ctx.categories,
292
+ "adr012-definition-skipped",
293
+ ctx.roundtripIntent
294
+ )
101
295
  });
102
296
  }
103
297
  ctx.telemetry?.(DEFINITION_WRITE_SKIPPED_EVENT, {
@@ -111,11 +305,21 @@ ${footer}`;
111
305
  }
112
306
  if (!definition) {
113
307
  if (ctx.categories) {
114
- const markdown = ctx.reason === "silent-ignore" ? "write accepted but value unchanged; no definition available" : ctx.reason === "override-error" ? `could not apply automatically: ${ctx.errorMessage ?? ""}` : `could not apply automatically: ${ctx.errorMessage ?? ""}`;
308
+ const markdown = buildNoDefinitionFallbackBody({
309
+ ruleId: ctx.question.ruleId,
310
+ sceneNodeId: ctx.scene.id,
311
+ reason: ctx.reason,
312
+ ...ctx.errorMessage !== void 0 ? { errorMessage: ctx.errorMessage } : {},
313
+ ...ctx.roundtripIntent !== void 0 ? { intent: ctx.roundtripIntent } : {}
314
+ });
115
315
  upsertCanicodeAnnotation(ctx.scene, {
116
316
  ruleId: ctx.question.ruleId,
117
317
  markdown,
118
- categoryId: ctx.categories.fallback
318
+ categoryId: categoryIdForAnnotate(
319
+ ctx.categories,
320
+ "other-failure",
321
+ ctx.roundtripIntent
322
+ )
119
323
  });
120
324
  }
121
325
  return ctx.reason === "silent-ignore" ? { icon: "\u{1F4DD}", label: "silent-ignore, annotated" } : { icon: "\u{1F4DD}", label: `error: ${ctx.errorMessage ?? ""}` };
@@ -132,8 +336,18 @@ ${footer}`;
132
336
  if (ctx.categories) {
133
337
  upsertCanicodeAnnotation(ctx.scene, {
134
338
  ruleId: ctx.question.ruleId,
135
- markdown: isRemoteReadOnly ? "source component lives in an external library and is read-only from this file \u2014 apply the fix in the library file itself." : `could not apply at source definition: ${defMsg}`,
136
- categoryId: ctx.categories.fallback
339
+ markdown: buildDefinitionTierFailureBody({
340
+ ruleId: ctx.question.ruleId,
341
+ sceneNodeId: ctx.scene.id,
342
+ ...ctx.roundtripIntent !== void 0 ? { intent: ctx.roundtripIntent } : {},
343
+ kind: isRemoteReadOnly ? "read-only-library" : "definition-error",
344
+ errorMessage: defMsg
345
+ }),
346
+ categoryId: categoryIdForAnnotate(
347
+ ctx.categories,
348
+ "other-failure",
349
+ ctx.roundtripIntent
350
+ )
137
351
  });
138
352
  }
139
353
  return {
@@ -143,7 +357,7 @@ ${footer}`;
143
357
  }
144
358
  }
145
359
  async function applyWithInstanceFallback(question, writeFn, context = {}) {
146
- const { categories, allowDefinitionWrite = false, telemetry } = context;
360
+ const { categories, allowDefinitionWrite = false, telemetry, roundtripIntent } = context;
147
361
  const scene = await figma.getNodeByIdAsync(question.nodeId);
148
362
  if (!scene) return { icon: "\u{1F4DD}", label: "missing node" };
149
363
  const definition = question.sourceChildId ? await figma.getNodeByIdAsync(question.sourceChildId) : null;
@@ -156,7 +370,8 @@ ${footer}`;
156
370
  categories,
157
371
  reason: "silent-ignore",
158
372
  allowDefinitionWrite,
159
- telemetry
373
+ telemetry,
374
+ ...roundtripIntent !== void 0 ? { roundtripIntent } : {}
160
375
  });
161
376
  }
162
377
  return { icon: "\u2705", label: "instance/scene" };
@@ -171,7 +386,8 @@ ${footer}`;
171
386
  reason: "non-override-error",
172
387
  errorMessage: msg,
173
388
  allowDefinitionWrite,
174
- telemetry
389
+ telemetry,
390
+ ...roundtripIntent !== void 0 ? { roundtripIntent } : {}
175
391
  });
176
392
  }
177
393
  return routeToDefinitionOrAnnotate(definition, writeFn, {
@@ -181,7 +397,8 @@ ${footer}`;
181
397
  reason: "override-error",
182
398
  errorMessage: msg,
183
399
  allowDefinitionWrite,
184
- telemetry
400
+ telemetry,
401
+ ...roundtripIntent !== void 0 ? { roundtripIntent } : {}
185
402
  });
186
403
  }
187
404
  }
@@ -220,6 +437,39 @@ ${footer}`;
220
437
  target.setBoundVariable(prop, variable);
221
438
  return true;
222
439
  }
440
+ function buildRoundtripIntentFromPropertyAnswer(question, answerValue) {
441
+ const raw = question.targetProperty;
442
+ if (raw === void 0) return void 0;
443
+ const props = Array.isArray(raw) ? raw : [raw];
444
+ if (props.length === 0) return void 0;
445
+ if (props.length === 1) {
446
+ const prop = props[0];
447
+ const perProp = answerValue && typeof answerValue === "object" && !("variable" in answerValue) && !Array.isArray(answerValue) ? answerValue[prop] : answerValue;
448
+ const parsed = parseValueForIntent(perProp);
449
+ if (parsed === void 0) return void 0;
450
+ return { field: prop, value: parsed, scope: "instance" };
451
+ }
452
+ const obj = answerValue && typeof answerValue === "object" && !("variable" in answerValue) && !Array.isArray(answerValue) ? answerValue : void 0;
453
+ const picked = {};
454
+ for (const p of props) {
455
+ if (obj && p in obj && obj[p] !== void 0) picked[p] = obj[p];
456
+ }
457
+ if (Object.keys(picked).length === 0) return void 0;
458
+ return {
459
+ field: props.join(", "),
460
+ value: picked,
461
+ scope: "instance"
462
+ };
463
+ }
464
+ function parseValueForIntent(raw) {
465
+ if (raw && typeof raw === "object" && "variable" in raw) {
466
+ return { variable: raw.variable };
467
+ }
468
+ if (raw && typeof raw === "object" && "fallback" in raw) {
469
+ return raw.fallback;
470
+ }
471
+ return raw;
472
+ }
223
473
  function applyPropertyScalar(target, prop, scalar) {
224
474
  const rec = target;
225
475
  const before = rec[prop];
@@ -228,6 +478,10 @@ ${footer}`;
228
478
  return true;
229
479
  }
230
480
  async function applyPropertyMod(question, answerValue, context = {}) {
481
+ const roundtripIntent = buildRoundtripIntentFromPropertyAnswer(
482
+ question,
483
+ answerValue
484
+ );
231
485
  const props = Array.isArray(question.targetProperty) ? question.targetProperty : question.targetProperty !== void 0 ? [question.targetProperty] : [];
232
486
  return applyWithInstanceFallback(
233
487
  question,
@@ -258,7 +512,10 @@ ${footer}`;
258
512
  }
259
513
  return changed;
260
514
  },
261
- context
515
+ {
516
+ ...context,
517
+ ...roundtripIntent !== void 0 ? { roundtripIntent } : {}
518
+ }
262
519
  );
263
520
  }
264
521
 
@@ -334,7 +591,15 @@ ${footer}`;
334
591
  }
335
592
  const ruleId = extractRuleId(text);
336
593
  if (!ruleId) continue;
337
- out.push({ nodeId: node.id, ruleId });
594
+ const payload = parseCanicodeJsonPayloadFromMarkdown(text);
595
+ const payloadAligned = payload && payload.ruleId === ruleId;
596
+ out.push({
597
+ nodeId: node.id,
598
+ ruleId,
599
+ ...payloadAligned && payload.intent ? { intent: payload.intent } : {},
600
+ ...payloadAligned && payload.sceneWriteOutcome ? { sceneWriteOutcome: payload.sceneWriteOutcome } : {},
601
+ ...payloadAligned && payload.codegenDirective ? { codegenDirective: payload.codegenDirective } : {}
602
+ });
338
603
  }
339
604
  return out;
340
605
  }
@@ -360,17 +625,22 @@ ${footer}`;
360
625
  walk(root, canicodeCategoryIds, out);
361
626
  return out;
362
627
  }
628
+ function safeChildren(node) {
629
+ try {
630
+ const c = node.children;
631
+ return Array.isArray(c) ? c : [];
632
+ } catch {
633
+ return [];
634
+ }
635
+ }
363
636
  function walk(node, canicodeCategoryIds, out) {
364
637
  try {
365
638
  const local = extractAcknowledgmentsFromNode(node, canicodeCategoryIds);
366
639
  for (const a of local) out.push(a);
367
640
  } catch {
368
641
  }
369
- const children = node.children;
370
- if (Array.isArray(children)) {
371
- for (const child of children) {
372
- if (child && typeof child === "object") walk(child, canicodeCategoryIds, out);
373
- }
642
+ for (const child of safeChildren(node)) {
643
+ if (child && typeof child === "object") walk(child, canicodeCategoryIds, out);
374
644
  }
375
645
  }
376
646
 
@@ -13,6 +13,12 @@ This skill works with either channel — the CLI or the canicode MCP server. Bot
13
13
  - A **saved fixture** (from `canicode calibrate-save-fixture`)
14
14
  - A **FIGMA_TOKEN** for live Figma URLs
15
15
 
16
+ ### Step 0: Verify canicode MCP tools are loaded (optional fast path)
17
+
18
+ Before shelling out to `npx canicode analyze …`, check whether the **`analyze` MCP tool** is available in **this** session — not only whether `.mcp.json` lists `canicode`. New MCP registrations usually need a **restart or MCP reload** before tools appear.
19
+
20
+ If you must use the CLI fallback, say so out loud: the user may have added `claude mcp add canicode …` but not restarted yet (#433). After restart/reload, `analyze` via MCP avoids the `npx` spawn. The fallback is valid — silence makes users think the MCP install failed.
21
+
16
22
  ## How to Analyze
17
23
 
18
24
  ### From a Figma URL
@@ -5,6 +5,8 @@ description: Gotcha survey (Claude Code or Cursor) — Q&A workflow; answers acc
5
5
 
6
6
  # CanICode Gotchas — Design Gotcha Survey
7
7
 
8
+ **Channel contrast:** **`canicode-gotchas`** (**this skill**) persists answers **only** in **local** `.claude/skills/canicode-gotchas/SKILL.md` — **memo-only**, no Plugin write to Figma. **`canicode-roundtrip`** writes to the **canvas**. Use gotchas when you want Q&A captured for code-gen context without mutating the file.
9
+
8
10
  Run a gotcha survey on a Figma design to collect implementation context that Figma cannot encode natively, capture developer/designer answers, and upsert them into **`.claude/skills/canicode-gotchas/SKILL.md`** so downstream `figma-implement-design` runs have annotation-ready context. In this model, rules do rule-based best-practice detection, and gotcha is the annotation output from that detection. Some gotchas come from violation rules (what is wrong and how to resolve it); others come from info-collection rules (neutral context Figma cannot represent, like interaction intent/state).
9
11
 
10
12
  **Install location:** The workflow prose may live under `.claude/skills/canicode-gotchas/SKILL.md` (default `canicode init`) or be copied to `.cursor/skills/canicode-gotchas/SKILL.md` (`canicode init --cursor-skills`). The **authoritative gotcha store** is always **`.claude/skills/canicode-gotchas/SKILL.md`** — the CLI `upsert-gotcha-section` writes there only. In the `.claude` copy, this file has two regions: the **Workflow** below (installed by `canicode init`, never overwritten manually) and the **Collected Gotchas** region at the bottom (one numbered section per design, replaced in place on re-runs).
@@ -18,6 +20,12 @@ Run a gotcha survey on a Figma design to collect implementation context that Fig
18
20
 
19
21
  ## Workflow
20
22
 
23
+ ### Step 0: Verify canicode MCP tools are loaded (optional fast path)
24
+
25
+ Before Step 1, verify that `gotcha-survey` is callable in **this** session — not merely listed in `.mcp.json`. Newly registered MCP servers usually need a **host restart or MCP reload** before tools appear (same pattern as `/canicode-roundtrip` Step 0 for the Figma MCP).
26
+
27
+ When you fall back to `npx canicode gotcha-survey … --json`, tell the user explicitly: the canicode MCP may not be loaded yet. They should register it (`claude mcp add canicode -- npx --yes --package=canicode canicode-mcp`, or the Cursor/`mcp.json` equivalent in the Customization guide) and **restart the IDE or reload MCP** — then the next session can use the MCP tool without spawning `npx`. The CLI fallback is correct behavior; silence makes users think registration failed (#433).
28
+
21
29
  ### Step 1: Run the gotcha survey
22
30
 
23
31
  If the `gotcha-survey` MCP tool is available, call it with the user's Figma URL:
@@ -115,77 +123,48 @@ The `core/contracts/design-key.ts` helper (`computeDesignKey`) handles every sha
115
123
 
116
124
  File-state detection (4-way: missing / valid / missing-heading / clobbered) and section walking (find existing `## #NNN — ...` by `Design key` substring, otherwise compute the next monotonic zero-padded NNN) are deterministic markdown operations and live in `core/gotcha/upsert-gotcha-section.ts` with vitest coverage — do not re-implement them in prose (per ADR-016).
117
125
 
118
- Render the per-design section markdown using the **Output Template** below with the literal string `{{SECTION_NUMBER}}` in the header (the CLI substitutes the right NNN for you preserves it on replace, computes the next monotonic value on append). Then invoke:
126
+ Build **one JSON object** on stdin for `upsert-gotcha-section`. The CLI renders the section markdown from `survey` + `answers` via `renderGotchaSection` in TypeScript (#439) severity, rule text, node ids, and instance context come **verbatim** from `gotcha-survey --json`; the skill must not paste LLM-authored section prose.
127
+
128
+ Payload shape:
129
+
130
+ ```json
131
+ {
132
+ "survey": {
133
+ "designKey": "<same as Step 4a>",
134
+ "designGrade": "<from gotcha-survey>",
135
+ "questions": "<full questions[] array from gotcha-survey — preserve order>"
136
+ },
137
+ "answers": {
138
+ "<nodeId>": { "answer": "…" }
139
+ },
140
+ "designName": "<Figma file name or fixture label>",
141
+ "figmaUrl": "<the user's input URL or path>",
142
+ "analyzedAt": "<ISO 8601 timestamp when you upsert>",
143
+ "today": "<YYYY-MM-DD local date for the section title>"
144
+ }
145
+ ```
146
+
147
+ For skipped / n/a: use `{ "skipped": true }` for that `nodeId`, or omit the key. Skipped questions do **not** get per-question rows; `renderGotchaSection` appends a compact **`#### Skipped (N)`** block listing each `ruleId` with a count (`ruleId` lines sorted lexically — see `src/core/gotcha/render-gotcha-section.ts`).
148
+
149
+ Invoke (cac requires `--input=-`, not `--input -`, so the stdin sentinel survives parsing — #420):
119
150
 
120
151
  ```bash
121
152
  npx canicode upsert-gotcha-section \
122
153
  --file .claude/skills/canicode-gotchas/SKILL.md \
123
154
  --design-key "<designKey from Step 4a>" \
124
- --section - # then pipe the rendered section markdown through stdin
155
+ --input=-
125
156
  ```
126
157
 
127
- The CLI prints a JSON result `{ state, action, sectionNumber, wrote, userMessage }`:
158
+ Pipe the JSON object on stdin. `--design-key` must equal `survey.designKey` (the CLI validates the match).
159
+
160
+ The CLI prints JSON `{ state, action, sectionNumber, wrote, userMessage, designKey }`:
128
161
 
129
162
  - `wrote: true` → success. `action` is `"replace"` (preserved `sectionNumber`) or `"append"` (next monotonic `sectionNumber`).
130
163
  - `wrote: false` with `state: "missing"` → tell the user: *"Your gotchas SKILL.md is not installed yet. Run `canicode init` first, then re-invoke this skill."* Stop here.
131
164
  - `wrote: false` with `state: "clobbered"` → tell the user: *"Your gotchas SKILL.md is missing the canicode YAML frontmatter (pre-#340 single-design clobber). Run `canicode init --force` to restore the workflow, then re-run this survey — your answers will land in a clean numbered section."* Stop here.
132
165
  - `wrote: true` with `state: "missing-heading"` → silent recovery. The CLI injected the `# Collected Gotchas` heading and appended the section; the workflow region above is untouched.
133
166
 
134
- The Workflow region above must never be touched. Do NOT copy Workflow prose into the per-design section; the section only carries metadata + gotcha answers.
135
-
136
- ## Output Template
137
-
138
- Each per-design section in the `# Collected Gotchas` region has this exact shape:
139
-
140
- ````markdown
141
- ## #NNN — {designName} — {YYYY-MM-DD}
142
-
143
- - **Figma URL**: {figmaUrl}
144
- - **Design key**: {designKey}
145
- - **Grade**: {designGrade}
146
- - **Analyzed at**: {analyzedAt}
147
-
148
- ### Gotchas
149
-
150
- #### {ruleId} — {nodeName}
151
-
152
- - **Severity**: {severity}
153
- - **Node ID**: {nodeId}
154
- - **Instance context** (omit this bullet if `instanceContext` was not in the survey question): parent instance `parentInstanceNodeId`, source node `sourceNodeId`, component `sourceComponentName` / `sourceComponentId` when present — roundtrip apply uses this to write on the source definition when instance overrides fail.
155
- - **Question**: {question}
156
- - **Answer**: {userAnswer}
157
-
158
- (repeat for each question)
159
- ````
160
-
161
- ### Field mapping
162
-
163
- | Field | Source |
164
- |-------|--------|
165
- | `NNN` | `sectionNumber` — zero-padded three-digit index. Preserved on re-run, incremented on append. |
166
- | `designName` | Figma file name or fixture name from the input |
167
- | `YYYY-MM-DD` | Today's date (the day you are running the survey) |
168
- | `figmaUrl` | The input URL or fixture path provided by the user |
169
- | `designKey` | `survey.designKey` from the gotcha-survey response (see Step 4a) |
170
- | `designGrade` | `designGrade` from gotcha-survey response |
171
- | `analyzedAt` | Current timestamp (ISO 8601) |
172
- | `ruleId` | `ruleId` from each question |
173
- | `nodeName` | `nodeName` from each question |
174
- | `severity` | `severity` from each question (blocking / risk / missing-info — the last surfaces only for info-collection rules per #406) |
175
- | `nodeId` | `nodeId` from each question |
176
- | `instanceContext` | When present on the question, copy `parentInstanceNodeId`, `sourceNodeId`, `sourceComponentId`, `sourceComponentName` into the bullet above (roundtrip / Plugin apply) |
177
- | `question` | `question` from each question |
178
- | `userAnswer` | The answer collected from the user in Step 3 |
179
-
180
- ### Skipped questions
181
-
182
- If the user skipped a question or said "n/a", still include it in the section with:
183
-
184
- ```markdown
185
- - **Answer**: _(skipped)_
186
- ```
187
-
188
- This ensures the code generation agent knows the gotcha exists even if no answer was provided.
167
+ The Workflow region above must never be touched.
189
168
 
190
169
  ## Edge Cases
191
170
 
@@ -195,5 +174,5 @@ This ensures the code generation agent knows the gotcha exists even if no answer
195
174
  - **Workflow region**: Never modified. If you notice the Workflow region has been edited by the user, leave their edits alone — only the `# Collected Gotchas` region is under skill control.
196
175
  - **Pre-#340 clobbered file** (the YAML frontmatter was rewritten to a per-design variant, so the canonical `canicode-gotchas` frontmatter is missing): tell the user to run `canicode init --force` to restore the workflow, then re-run the survey. The prior single-design content cannot be automatically migrated into a `## #001` section — the user re-runs and gets a clean section.
197
176
  - **MCP tool not available**: Fall back to `npx canicode gotcha-survey <input> --json` — the CLI returns the same `GotchaSurvey` shape. If the CLI is also unavailable (e.g. no node runtime), tell the user to install the canicode MCP server or the `canicode` npm package (see Prerequisites).
198
- - **Partial answers**: If the user stops mid-survey, upsert the section with answers collected so far. Mark remaining questions as _(skipped)_.
177
+ - **Partial answers**: If the user stops mid-survey, upsert the section with answers collected so far. Remaining questions count toward **`#### Skipped (N)`** (omit keys or `{ "skipped": true }`).
199
178
  - **Manual section deletion**: If the user deletes a middle section by hand, do not renumber existing sections. The next new section still gets `(highest existing number) + 1`; numeric gaps are acceptable (same pattern as `.claude/docs/ADR.md`).