Skip to content
Open
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
11 changes: 8 additions & 3 deletions src/specify_cli/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,14 @@ def render_toml_command(self, frontmatter: dict, body: str, source_id: str) -> s
toml_lines.append(f"# Source: {source_id}")
toml_lines.append("")

# Keep TOML output valid even when body contains triple-quote delimiters.
# Prefer multiline forms, then fall back to escaped basic string.
if '"""' not in body:
# Keep TOML output valid even when body contains triple-quote delimiters
# or backslashes. Prefer multiline forms, then fall back to escaped basic
# string. A multiline *basic* string ("""...""") processes backslash escape
# sequences, so a body containing a backslash (e.g. a Windows path
# ``C:\\Users\\...`` whose ``\\U`` reads as an invalid unicode escape) would
# produce unparseable TOML — route those to the *literal* form ('''...'''),
# which does not process escapes, or to the escaped basic string.
if '"""' not in body and "\\" not in body:
toml_lines.append('prompt = """')
toml_lines.append(body)
toml_lines.append('"""')
Expand Down
41 changes: 41 additions & 0 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,47 @@ def test_render_toml_command_preserves_multiline_description(self):

assert parsed["description"] == "first line\nsecond line\n"

def test_render_toml_command_preserves_backslashes_in_body(self):
"""A backslash in the body (e.g. a Windows path) must not break TOML.

A multiline basic string ("\"\"\"") processes backslash escapes, so
``C:\\Users`` (``\\U``) would render as invalid TOML; the body must
round-trip with backslashes intact.
"""
from specify_cli.agents import CommandRegistrar as AgentCommandRegistrar

registrar = AgentCommandRegistrar()
output = registrar.render_toml_command(
{"description": "x"},
r"Run C:\Users\dev\tool.exe then report.",
"extension:test-ext",
)
parsed = tomllib.loads(output) # must not raise
assert parsed["prompt"].strip() == 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."""
from specify_cli.agents import CommandRegistrar as AgentCommandRegistrar

registrar = AgentCommandRegistrar()
output = registrar.render_toml_command(
{"description": "x"},
"path ends with sep\\",
"extension:test-ext",
)
parsed = tomllib.loads(output)
assert parsed["prompt"].strip() == "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."""
from specify_cli.agents import CommandRegistrar as AgentCommandRegistrar

registrar = AgentCommandRegistrar()
body = "a \\ b\nc \"\"\" d\ne ''' f"
output = registrar.render_toml_command({"description": "x"}, body, "extension:test-ext")
parsed = tomllib.loads(output)
assert parsed["prompt"] == body

def test_register_commands_for_claude(self, extension_dir, project_dir):
"""Test registering commands for Claude agent."""
# Create .claude directory
Expand Down