Skip to content

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

Merged
mnriem merged 1 commit into
github:mainfrom
jawwad-ali:fix/toml-command-backslash
Jun 24, 2026
Merged

fix: render valid TOML when a command body contains backslashes#3135
mnriem merged 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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes CommandRegistrar.render_toml_command() so it always emits parseable TOML when the command body contains backslashes (notably Windows paths like C:\Users\...) by routing those bodies away from multiline basic TOML strings (which interpret backslash escapes) and toward multiline literal strings (or an escaped basic-string fallback when needed).

Changes:

  • Update TOML rendering logic to avoid multiline basic strings when body contains \, preferring multiline literal strings instead.
  • Preserve the existing “fallback to escaped basic string” behavior when the body contains both triple-quote styles.
  • Add regression tests asserting tomllib.loads() succeeds and the prompt value round-trips for Windows-path, trailing-backslash, and “both triple quotes + backslash” bodies.
Show a summary per file
File Description
src/specify_cli/agents.py Adjusts TOML prompt rendering selection to avoid backslash-escape parsing issues in multiline basic strings.
tests/test_extensions.py Adds tests that reproduce the TOML parse failure/corruption on backslash-containing bodies and validate the fix via tomllib.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@mnriem mnriem merged commit 034fbfc into github:main Jun 24, 2026
11 checks passed
@mnriem

mnriem commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

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.

3 participants