Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

feat: rules repairer mapping diagnostics to edits

momoyeyuf31163a605f1
diff --git a/benchmarks/repair-rounds/repairs.mjs b/benchmarks/repair-rounds/repairs.mjs
new file mode 100644
index 00000000..d04f545b
--- /dev/null
+++ b/benchmarks/repair-rounds/repairs.mjs
@@ -0,0 +1,161 @@
+// Rules repairer: turns diagnostics into document edits using only what the
+// receipt carries (subject paths, evidence, supportedFixes, px figures in
+// messages). Diagnostics that do not map to an edit are reported as
+// unactionable; the repairer never inspects the injection record.
+
+const MISSING_VALUE_HINTS = {
+ output: 'output.html',
+ title: 'Untitled diagram',
+};
+
+function unescapePointer(part) {
+ return part.replace(/~1/g, '/').replace(/~0/g, '~');
+}
+
+export function getByPointer(doc, pointer) {
+ let target = doc;
+ for (const part of pointer.split('/').filter(Boolean).map(unescapePointer)) {
+ if (target === undefined || target === null) return undefined;
+ target = target[part];
+ }
+ return target;
+}
+
+export function setByPointer(doc, pointer, value) {
+ const parts = pointer.split('/').filter(Boolean).map(unescapePointer);
+ let target = doc;
+ for (const part of parts.slice(0, -1)) {
+ if (target === undefined || target === null) return false;
+ target = target[part];
+ }
+ if (target === undefined || target === null) return false;
+ target[parts[parts.length - 1]] = value;
+ return true;
+}
+
+function uniqueId(doc, base, collection) {
+ const used = new Set((doc[collection] || []).map((node) => node.id));
+ let index = 2;
+ let candidate = `${base}-${index}`;
+ while (used.has(candidate)) {
+ index += 1;
+ candidate = `${base}-${index}`;
+ }
+ return candidate;
+}
+
+const RULES = [
+ {
+ match: (diagnostic) => diagnostic.code === 'schema/required'
+ && diagnostic.subject?.path && diagnostic.evidence?.missingProperty,
+ apply(diagnostic, doc) {
+ const property = diagnostic.evidence.missingProperty;
+ const value = MISSING_VALUE_HINTS[property];
+ if (value === undefined) return null;
+ const pointer = `${diagnostic.subject.path === '/' ? '' : diagnostic.subject.path}/${property}`;
+ return setByPointer(doc, pointer, value)
+ ? { detail: `set ${pointer} to ${JSON.stringify(value)}` }
+ : null;
+ },
+ },
+ {
+ match: (diagnostic) => ['schema/enum', 'schema/const'].includes(diagnostic.code)
+ && diagnostic.subject?.path
+ && (diagnostic.evidence?.allowedValues?.length || diagnostic.evidence?.allowedValue !== undefined),
+ apply(diagnostic, doc) {
+ const value = diagnostic.evidence.allowedValues?.[0] ?? diagnostic.evidence.allowedValue;
+ return setByPointer(doc, diagnostic.subject.path, value)
+ ? { detail: `set ${diagnostic.subject.path} to ${JSON.stringify(value)}` }
+ : null;
+ },
+ },
+ {
+ match: (diagnostic) => diagnostic.code === 'schema/additionalProperties'
+ && diagnostic.subject?.path && diagnostic.evidence?.additionalProperty,
+ apply(diagnostic, doc) {
+ const pointer = `${diagnostic.subject.path === '/' ? '' : diagnostic.subject.path}/${diagnostic.evidence.additionalProperty}`;
+ const parts = pointer.split('/').filter(Boolean).map(unescapePointer);
+ let target = doc;
+ for (const part of parts.slice(0, -1)) target = target?.[part];
+ if (target === undefined || target === null) return null;
+ delete target[parts[parts.length - 1]];
+ return { detail: `removed ${pointer}` };
+ },
+ },
+ {
+ // "Label "X" (~475px) is wider than component "users" (120px)"
+ match: (diagnostic) => diagnostic.code === 'layout/constraint'
+ && /Label "(.+)" \(~[\d.]+px\) is wider than component "([^"]+)" \(([\d.]+)px\)/.test(diagnostic.message || ''),
+ apply(diagnostic, doc, context) {
+ const match = diagnostic.message.match(/Label "(.+)" \(~([\d.]+)px\) is wider than component "([^"]+)" \(([\d.]+)px\)/);
+ const [, label, labelPx, nodeId, widthPx] = match;
+ const nodes = doc[context.nodes] || [];
+ const node = nodes.find((entry) => entry.id === nodeId || entry.label === label);
+ if (!node) return null;
+ const pxPerChar = Number(labelPx) / Math.max(label.length, 1);
+ const maxChars = Math.max(1, Math.floor(Number(widthPx) / pxPerChar));
+ if (label.length <= maxChars) return null;
+ node.label = label.slice(0, maxChars);
+ return { detail: `shortened label of "${node.id}" to ${maxChars} chars` };
+ },
+ },
+ {
+ match: (diagnostic) => diagnostic.code === 'workflow/duplicate-node-id'
+ && diagnostic.evidence?.duplicatePath && diagnostic.evidence?.duplicateNodeId,
+ apply(diagnostic, doc, context) {
+ const pointer = diagnostic.evidence.duplicatePath;
+ const id = uniqueId(doc, diagnostic.evidence.duplicateNodeId, context.nodes);
+ return setByPointer(doc, pointer, id)
+ ? { detail: `renamed duplicate node at ${pointer} to "${id}"` }
+ : null;
+ },
+ },
+ {
+ // "set /edges/0/to to verified node id "chat""
+ match: (diagnostic) => /\/unknown-(edge-)?endpoint$/.test(diagnostic.code || '')
+ && diagnostic.subject?.path
+ && diagnostic.supportedFixes?.some((fix) => /set \S+ to verified node id "/.test(fix)),
+ apply(diagnostic, doc) {
+ const fix = diagnostic.supportedFixes.find((entry) => /set \S+ to verified node id "/.test(entry));
+ const target = fix.match(/set (\S+) to verified node id "([^"]+)"/);
+ return setByPointer(doc, target[1], target[2])
+ ? { detail: `set ${target[1]} to "${target[2]}"` }
+ : null;
+ },
+ },
+ {
+ // "...reflow automatic spacing ... so the complete viewBox width is at most
+ // 1240px (current 2400px; ...)"
+ match: (diagnostic) => diagnostic.code === 'composition/desktop-readability'
+ && diagnostic.supportedFixes?.some((fix) => /at most (\d+(?:\.\d+)?)px \(current ([\d.]+)px/.test(fix)),
+ apply(diagnostic, doc) {
+ const fix = diagnostic.supportedFixes.find((entry) => /at most [\d.]+px \(current [\d.]+px/.test(entry));
+ const width = Number(fix.match(/at most (\d+(?:\.\d+)?)px \(current [\d.]+px/)[1]);
+ const viewBox = getByPointer(doc, '/meta/viewBox');
+ if (!Array.isArray(viewBox) || viewBox.length < 2) return null;
+ if (viewBox.length === 2) doc.meta.viewBox = [Math.floor(width), viewBox[1]];
+ else doc.meta.viewBox = [viewBox[0], viewBox[1], Math.floor(width), viewBox[3]];
+ return { detail: `set meta.viewBox width to ${Math.floor(width)}` };
+ },
+ },
+];
+
+export function repairFromDiagnostics(doc, diagnostics, { type, nodes }) {
+ const context = { type, nodes };
+ const applied = [];
+ const unactionable = [];
+ for (const diagnostic of diagnostics || []) {
+ const rule = RULES.find((candidate) => candidate.match(diagnostic));
+ let action = null;
+ if (rule) {
+ try {
+ action = rule.apply(diagnostic, doc, context);
+ } catch {
+ action = null;
+ }
+ }
+ if (action) applied.push({ code: diagnostic.code, ...action });
+ else unactionable.push({ code: diagnostic.code, message: diagnostic.message });
+ }
+ return { applied, unactionable };
+}