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."""