From 026b6e172c47fa09246b89b6e1d414b8e15f1057 Mon Sep 17 00:00:00 2001 From: Younes Beriane Date: Mon, 12 Oct 2026 07:05:52 +0200 Subject: [PATCH] fix(design): keep icon generator from corrupting SVGs or truncating output (#526) * fix(design): keep icon generator from corrupting SVGs or truncating output apply_viewbox_size rewrote every width/height attribute in the document, including stroke-width and child elements, and only handled double quotes. Restrict it to the root tag and set width and height independently. generate_icon capped output at 4096 tokens, which truncates detailed icons. Raise it to 16384 and report finish_reason MAX_TOKENS separately from empty or malformed output. Fixes #525 * fix(design): apply the response guard and MAX_TOKENS diagnosis to icon batch generate_batch still indexed candidates[0].content.parts unguarded and printed a bare "No valid SVGs generated." Share the response-text extraction and the no-SVG diagnosis with generate_icon, treat text=None as an empty response, and warn when a batch is cut off at MAX_TOKENS after returning fewer icons than requested. Refs #525 * fix(design): match only real root attributes when sizing icon SVGs _set_root_attr searched the root tag with a regex, so width= or height= text inside another quoted attribute value (aria-label="height='2'") was rewritten and the output stopped being valid XML. Walk the tag's attributes instead, consuming each quoted value whole, and only replace an attribute whose name is exactly width or height. Refs #525 --- .../skills/design/scripts/icon/generate.py | 88 ++++++++--- .../scripts/tests/test_icon_generate.py | 142 ++++++++++++++++++ .../skills/design/scripts/icon/generate.py | 88 ++++++++--- .../scripts/tests/test_icon_generate.py | 142 ++++++++++++++++++ 4 files changed, 416 insertions(+), 44 deletions(-) create mode 100644 .claude/skills/design/scripts/tests/test_icon_generate.py create mode 100644 cli/assets/skills/design/scripts/tests/test_icon_generate.py diff --git a/.claude/skills/design/scripts/icon/generate.py b/.claude/skills/design/scripts/icon/generate.py index 5e00fa9..7d3a338 100644 --- a/.claude/skills/design/scripts/icon/generate.py +++ b/.claude/skills/design/scripts/icon/generate.py @@ -174,18 +174,68 @@ def apply_color(svg_code, color): return svg_code +# Root opening tag; quoted attribute values may contain ">". +_SVG_ROOT_TAG = re.compile(r""""']|"[^"]*"|'[^']*')*>""") + + +# One token of an opening tag: name[=value], or a stray quoted string. +# Quoted values are consumed whole, so text inside them is never a name. +_TAG_ATTR = re.compile( + r"""([^\s=/>"']+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"']+))?|"[^"]*"|'[^']*'""") + + +def _set_root_attr(tag, attr, value): + """Set attr on a single opening tag, whatever quote style it uses.""" + for m in _TAG_ATTR.finditer(tag, len(' tag only, leaving the viewBox alone""" if size: - # Update width/height attributes if present - svg_code = re.sub(r'width="[^"]*"', f'width="{size}"', svg_code) - svg_code = re.sub(r'height="[^"]*"', f'height="{size}"', svg_code) - # Add width/height if not present - if 'width=' not in svg_code: - svg_code = svg_code.replace('' in out + assert 'viewBox="0 0 24 24"' in out + root = out[: out.index(">") + 1] + assert 'width="48"' in root and 'height="48"' in root + + +def test_viewbox_size_handles_width_and_height_independently(gen): + out = gen.apply_viewbox_size("", 32) + root = out[: out.index(">") + 1] + assert "width=\"32\"" in root and "height=\"32\"" in root + assert "width='10'" not in root + + +@pytest.mark.parametrize("src, kept", [ + ('', + {"aria-label": "height='2'"}), + ("", + {"aria-label": 'width="2" height="3"'}), + ('', + {"data-note": "a > b width='1'"}), + ("", + {"data-note": 'x>y height="2"'}), +]) +def test_viewbox_size_ignores_attribute_text_inside_quoted_values(gen, src, kept): + out = gen.apply_viewbox_size(src, 24) + root = ET.fromstring(out) + assert root.get("width") == "24" and root.get("height") == "24" + for name, value in kept.items(): + assert root.get(name) == value + for child in root: + assert child.get("width") in (None, "1") + + +def test_generate_icon_uses_large_token_budget(gen, tmp_path): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '') + out = tmp_path / "a.svg" + assert gen.generate_icon("crane", output_path=str(out)) == str(out) + cfg = client.models.generate_content.call_args.kwargs["config"] + assert cfg.max_output_tokens >= 16384 + + +def test_generate_icon_reports_max_tokens(gen, capsys): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '```svg\n\n' + ' opening tag; quoted attribute values may contain ">". +_SVG_ROOT_TAG = re.compile(r""""']|"[^"]*"|'[^']*')*>""") + + +# One token of an opening tag: name[=value], or a stray quoted string. +# Quoted values are consumed whole, so text inside them is never a name. +_TAG_ATTR = re.compile( + r"""([^\s=/>"']+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"']+))?|"[^"]*"|'[^']*'""") + + +def _set_root_attr(tag, attr, value): + """Set attr on a single opening tag, whatever quote style it uses.""" + for m in _TAG_ATTR.finditer(tag, len(' tag only, leaving the viewBox alone""" if size: - # Update width/height attributes if present - svg_code = re.sub(r'width="[^"]*"', f'width="{size}"', svg_code) - svg_code = re.sub(r'height="[^"]*"', f'height="{size}"', svg_code) - # Add width/height if not present - if 'width=' not in svg_code: - svg_code = svg_code.replace('' in out + assert 'viewBox="0 0 24 24"' in out + root = out[: out.index(">") + 1] + assert 'width="48"' in root and 'height="48"' in root + + +def test_viewbox_size_handles_width_and_height_independently(gen): + out = gen.apply_viewbox_size("", 32) + root = out[: out.index(">") + 1] + assert "width=\"32\"" in root and "height=\"32\"" in root + assert "width='10'" not in root + + +@pytest.mark.parametrize("src, kept", [ + ('', + {"aria-label": "height='2'"}), + ("", + {"aria-label": 'width="2" height="3"'}), + ('', + {"data-note": "a > b width='1'"}), + ("", + {"data-note": 'x>y height="2"'}), +]) +def test_viewbox_size_ignores_attribute_text_inside_quoted_values(gen, src, kept): + out = gen.apply_viewbox_size(src, 24) + root = ET.fromstring(out) + assert root.get("width") == "24" and root.get("height") == "24" + for name, value in kept.items(): + assert root.get(name) == value + for child in root: + assert child.get("width") in (None, "1") + + +def test_generate_icon_uses_large_token_budget(gen, tmp_path): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '') + out = tmp_path / "a.svg" + assert gen.generate_icon("crane", output_path=str(out)) == str(out) + cfg = client.models.generate_content.call_args.kwargs["config"] + assert cfg.max_output_tokens >= 16384 + + +def test_generate_icon_reports_max_tokens(gen, capsys): + client = gen.genai.Client.return_value + client.models.generate_content.return_value = _response( + '```svg\n\n' + '