Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/specify_cli/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")

Expand Down
47 changes: 45 additions & 2 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down Expand Up @@ -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."""
Expand All @@ -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."""
Expand Down
Loading