Skip to content

fix: render valid TOML when a command body contains backslashes#3135

Open
jawwad-ali wants to merge 1 commit into
github:mainfrom
jawwad-ali:fix/toml-command-backslash
Open

fix: render valid TOML when a command body contains backslashes#3135
jawwad-ali wants to merge 1 commit into
github:mainfrom
jawwad-ali:fix/toml-command-backslash

Conversation

@jawwad-ali

Copy link
Copy Markdown
Contributor

Description

CommandRegistrar.render_toml_command() renders a command body into a multiline basic TOML string ("""..."""). Basic strings process backslash escape sequences, but the body was emitted raw — so any backslash in the body breaks the output:

  • A Windows path like C:\Users\... contains \U, which TOML reads as an 8-digit unicode escape → the agent's TOML loader fails with Invalid hex value and the generated Gemini / Tabnine command file is completely unparseable.
  • A body ending in a single \ becomes a TOML line-continuation that silently swallows the closing newline.

Reproduced with tomllib:

render_toml_command({}, r"Run C:\Users\dev\tool.exe", "src")
# -> prompt = """ ... C:\Users\dev\tool.exe ... """
# tomllib.loads(...) -> TOMLDecodeError: Invalid hex value

Fix

Route bodies containing a backslash away from the basic-string branch: prefer the multiline literal form ('''...'''), which does not process escapes, and fall back to the escaped basic string (_render_basic_toml_string) when both triple-quote styles are present. This mirrors the backslash escaping already done by base.py's TomlIntegration. One-line condition change; behavior is unchanged for backslash-free bodies.

Testing

  • Tested locally with uv run specify --help (exit 0)

  • Ran existing tests with uv sync && uv run pytest

  • uvx ruff check src/ — All checks passed

  • uv run pytest tests/test_extensions.py tests/integrations/test_integration_gemini.py tests/integrations/test_integration_tabnine.py381 passed, 3 skipped (no regressions; the existing 3 render_toml_command tests still pass).

  • New tests in tests/test_extensions.py: a Windows-path body, a trailing-backslash body, and a backslash + both-triple-quote-styles fallback — each asserts tomllib.loads() succeeds and the body round-trips. The first two fail on main (TOMLDecodeError / dropped backslash) and pass with this fix.

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (describe below)

Found and fixed with Claude Code (Claude Opus 4.8) under my direction. AI located the unescaped basic-string branch, confirmed the existing TomlIntegration escaping precedent, implemented the routing fix, and wrote the tests; I reproduced the tomllib parse failure on main and verified the fix + no regressions in the Gemini/Tabnine suites before submitting. I'll disclose if any review responses are AI-assisted as well.

render_toml_command() emitted the body inside a multiline *basic* TOML
string ("""..."""), which processes backslash escape sequences. A command
body containing a backslash — e.g. a Windows path like C:\Users\... whose
\U reads as an invalid unicode escape — therefore produced unparseable TOML
("Invalid hex value"), so the generated Gemini/Tabnine command file failed
to load. A body ending in a backslash also silently ate the closing newline
via TOML line-continuation.

Route bodies containing a backslash to the multiline *literal* form
('''...'''), which does not process escapes, or to the escaped basic string
when both triple-quote styles are present. Mirrors the escaping already done
by base.py's TomlIntegration.

Add tests covering a Windows path, a trailing backslash, and the
backslash + both-triple-quote-styles fallback.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jawwad-ali jawwad-ali requested a review from mnriem as a code owner June 23, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant