From d0227e9f2b28e9e289b2c663ecc38227c3876a71 Mon Sep 17 00:00:00 2001 From: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:52:54 -0800 Subject: [PATCH] fix(extensions): keep TOML command prompts identical to the body render_toml_command put the closing """ or ''' on its own line, so the parsed prompt of every extension or preset command rendered for a TOML agent (gemini, tabnine) gained a trailing newline, and an empty body became "\n". The escaped basic string branches of the same function already round-trip the body exactly, and the core TomlIntegration renderer does not force a trailing newline either. Close the multiline strings right after the body, so an LF body round-trips exactly. A body ending in a double quote gets a line-ending backslash before the closing delimiter; a literal body ending in a single quote falls back to the escaped basic string, matching TomlIntegration._render_toml_string. Assisted-by: OpenAI Codex CLI (model: gpt-5.6-sol, autonomous) Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) --- src/specify_cli/agents.py | 14 +++++++----- tests/test_extensions.py | 47 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/specify_cli/agents.py b/src/specify_cli/agents.py index 7538e04579..c0401f37ee 100644 --- a/src/specify_cli/agents.py +++ b/src/specify_cli/agents.py @@ -347,13 +347,15 @@ def render_toml_command(self, frontmatter: dict, body: str, source_id: str) -> s if self._has_illegal_toml_control(body): toml_lines.append(f"prompt = {self._render_basic_toml_string(body)}") elif '"""' not in body and "\\" not in body: - toml_lines.append('prompt = """') - toml_lines.append(body) - toml_lines.append('"""') + if body.endswith('"'): + toml_lines.append('prompt = """\n' + body + '\\\n"""') + else: + toml_lines.append('prompt = """\n' + body + '"""') elif "'''" not in body: - toml_lines.append("prompt = '''") - toml_lines.append(body) - toml_lines.append("'''") + if body.endswith("'"): + toml_lines.append(f"prompt = {self._render_basic_toml_string(body)}") + else: + toml_lines.append("prompt = '''\n" + body + "'''") else: toml_lines.append(f"prompt = {self._render_basic_toml_string(body)}") diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 530ad0bf7e..77a2a0525e 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -3885,6 +3885,49 @@ def test_render_toml_command_preserves_multiline_description(self): assert parsed["description"] == "first line\nsecond line\n" + @pytest.mark.parametrize( + ("body", "expected_form"), + [ + pytest.param("Do work", 'prompt = """\n', id="single-line"), + pytest.param("", 'prompt = """\n', id="empty"), + pytest.param("Do work\n", 'prompt = """\n', id="trailing-newline"), + pytest.param( + "\nleading newline", 'prompt = """\n', id="leading-newline" + ), + pytest.param('ends with quote"', '\\\n"""', id="trailing-quote"), + pytest.param( + 'ends with two quotes""', '\\\n"""', id="trailing-two-quotes" + ), + pytest.param("first line\nsecond line", 'prompt = """\n', id="multiline"), + pytest.param( + r"a body with a \ backslash", "prompt = '''\n", id="literal" + ), + pytest.param( + "a \\ body ending with '", + 'prompt = "', + id="literal-trailing-quote", + ), + pytest.param( + 'contains """ and ends with \'', + 'prompt = "', + id="triple-double-trailing-quote", + ), + ], + ) + def test_render_toml_command_round_trips_prompt_exactly( + self, body, expected_form + ): + """Every TOML prompt form must round-trip an LF body exactly.""" + from specify_cli.agents import CommandRegistrar as AgentCommandRegistrar + + registrar = AgentCommandRegistrar() + output = registrar.render_toml_command( + {"description": "x"}, body, "extension:test-ext" + ) + + assert expected_form in output + assert tomllib.loads(output)["prompt"] == body + @pytest.mark.parametrize( ("description", "expected"), [ @@ -3950,7 +3993,7 @@ def test_render_toml_command_preserves_backslashes_in_body(self): "extension:test-ext", ) parsed = tomllib.loads(output) # must not raise - assert parsed["prompt"].strip() == r"Run C:\Users\dev\tool.exe then report." + assert parsed["prompt"] == r"Run C:\Users\dev\tool.exe then report." def test_render_toml_command_handles_trailing_backslash(self): """A body ending in a backslash must round-trip without corruption.""" @@ -3963,7 +4006,7 @@ def test_render_toml_command_handles_trailing_backslash(self): "extension:test-ext", ) parsed = tomllib.loads(output) - assert parsed["prompt"].strip() == "path ends with sep\\" + assert parsed["prompt"] == "path ends with sep\\" def test_render_toml_command_backslash_with_both_triple_quotes_escapes(self): """Body with a backslash and both triple-quote styles → escaped basic string."""