Skip to content

fix(agents): render codex TOML with multi-line literal strings - #754

Merged
jeff-r2026 merged 3 commits into
Tencent:mainfrom
ydflow:fix/codex-toml-multiline-literal
Sep 23, 2026
Merged

jeff-r2026 merged 3 commits into
Tencent:mainfrom
ydflow:fix/codex-toml-multiline-literal

Conversation

@ydflow

@ydflow ydflow commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #749

Summary

renderTomlAgent() in src/resources/agent-format.ts handed the whole spec to smol-toml's stringify with no options. That always emits basic strings, so every newline in a prompt became a literal \n escape:

developer_instructions = "line one\nline two\n"

A 16-line prompt (headings + bullet lists + a numbered output contract) collapses into one ~700-character line. Consequences, all from the issue:

  • any edit to the prompt reflows that entire line, so git diff on the rendered file is one huge -/+ pair
  • reading "what will the agent actually be told?" means mentally un-escaping the string, or piping through sed 's/\n/\n/g'
  • editors soft-wrap it into a blob, which discourages keeping prompts in the team repo at all

Fix

Values containing newlines now render as multi-line literal strings:

developer_instructions = '''
line one
line two
'''

The literal form rather than the basic multi-line form (""") because literals do no escape processing — content containing \ or " round-trips byte-for-byte and the renderer needs no escaping logic at all.

One detail worth flagging: the newline after the opening delimiter is the delimiter's own (TOML trims it), so '''\nvalue\n''' appends a newline the value never had. '''\nvalue''' round-trips exactly. I verified this against smol-toml for values with and without a trailing newline rather than assuming it.

Fallback

The basic form is kept when the value cannot be represented as a literal — a body containing ''', a body ending in ' (either closes the delimiter early), control characters such as \r, or U+007F DEL (TOML 1.0 bans it from literal strings along with C0, and it sits outside the \x00-\x1f block the predicate already covered). Empty and single-line values are unchanged, so existing single-line agents render byte-identically to before.

The same treatment covers strings arriving from tool_extras.codex, which are copied verbatim into the TOML root.

Dollar signs stay verbatim

The substitution passes a function replacement rather than a string. The value is agent-authored content, and a string replacement would let JavaScript expand $&, $$, $' and the backtick form inside it — a multi-line shell instruction carrying $$ would silently lose a dollar and no longer round-trip.

Tests

src/__tests__/agent-format.test.ts asserts the round trip (parseToml(renderForCodex(spec)) equals the original instructions) rather than exact formatting:

  • multi-line instructions become a literal and round-trip
  • a prompt carrying $$ and $& round-trips byte-for-byte
  • the fallback shapes (ends with a quote', contains ''' inside, has a CR) round-trip on the basic form
  • a value carrying U+007F DEL renders a document that still parses
  • single-line and empty instructions keep the basic form
  • tool_extras.codex strings get the same treatment
src/__tests__/agent-format.test.ts (70 tests) 190ms
 Test Files  1 passed (1)
      Tests  70 passed (70)

npx tsc --noEmit exit 0. npm run build exit 0.

The RED is real: reverting only the replacement callback to a string replacement fails the $$ case, because the prompt no longer round-trips.

End-to-end, real CLI

npm run build, then the compiled dist/index.js against a local git team repo (provider: git) holding .teamai/agents/shell-helper.md with a fenced bash block containing echo "$$HOME", toolPaths.codex declared, and the .codex root created so the tool counts as installed:

$ teamai pull --force
✔ [project] Synced 1 agents

Not verified

I did not exercise a live teamai pull against a real remote, and I did not run the full npm run test:e2e suite — it needs TEAMAI_TEST_TOKEN and TEAMAI_TEST_REPO_URL, which I do not have.

The one e2e file this branch touches, src/__tests__/e2e/doctor-delivery-cli.test.ts, was run directly and its expected codex fixture updated to the literal form. It reports 3 failed / 9 passed, but the same three failures occur on an unmodified checkout of origin/main (verified by building and running that file in a clean worktree) — they are Windows shell/env-dependent and predate this change. This branch adds no new failures.

The full agent/provider matrix from AGENTS.md (Claude, Codex, CodeBuddy, OpenCode × git, gitlab, github) is therefore not covered by this record.

Scope

Only the codex renderer. renderTomlAgent is shared by renderForCodex and renderForCodexInternal, so both get the fix; no other tool format is touched. The unrelated hook-handlers change that had drifted into this branch was removed, so the diff is codex rendering plus its tests only.

Assisted-by: Claude Code:claude-sonnet-4-5

`renderTomlAgent()` handed the whole spec to smol-toml's `stringify`
with no options, and that always emits basic strings — so every newline
in a prompt became a literal `\n` escape and a 16-line prompt collapsed
into one ~700-character line. `git diff` on a rendered prompt was one
huge `-`/`+` pair, and reading "what will the agent actually be told?"
meant mentally un-escaping the string.

Values containing newlines now render as multi-line literal strings
(`'''`), which keep real newlines and do no escape processing — so the
content round-trips byte-for-byte and the renderer needs no escaping
logic. The newline after the opening delimiter is the delimiter's own
(TOML trims it), so it must not be doubled; verified against smol-toml
for values with and without a trailing newline.

Falls back to the basic form when the value cannot be a literal: a body
containing `'''`, a body ending in `'` (either would close the
delimiter early), or control characters such as `\r`. Empty and
single-line values keep the existing output. The same treatment covers
strings arriving from `tool_extras.codex`, which are copied verbatim
into the TOML root.

Tests assert the round trip (`parseToml(renderForCodex(spec))` equals
the original instructions) rather than the exact formatting, plus the
fallback and single-line cases.

Full suite: 4140 passed / 98 failed against a 4134 / 100 baseline — the
98 failures are pre-existing environment-dependent ones (hooks, doctor,
init, push-pending-pr), unchanged by this diff. `tsc --noEmit` clean.

Closes Tencent#749

Assisted-by: Claude Code:claude-sonnet-4-5
@jeff-r2026 jeff-r2026 self-assigned this Sep 23, 2026
@github-actions

Copy link
Copy Markdown

Findings

  • [P1 blocking] src/resources/agent-format.ts:484 passes generated TOML as a replacement string, so JavaScript expands $&, $$, $', and $` sequences inside prompts. Multiline shell instructions containing $$, for example, silently lose a dollar and no longer round-trip. Use a replacement callback such as text.replace(escaped, () => replacement).
  • [P1 blocking] src/resources/agent-format.ts:440 permits U+007F DEL in literal strings, although TOML forbids that control character. A multiline value containing it produces invalid TOML; include \x7f in the fallback predicate and test it.
  • [P1 blocking] src/hook-handlers.ts:467 and its accompanying test are an unrelated votes-sync logging change. They do not trace to the stated Codex TOML goal and are absent from the PR description, violating the surgical-change rule; move them to a separate PR.
  • [P1 blocking] The PR description lacks the required real-CLI/end-to-end verification record. It reports unit Vitest, type checking, and suite totals, but does not document npm run build followed by an actual CLI workflow and its result.
  • [P2 non-blocking] src/resources/agent-format.ts:482 assumes every serialized key is emitted bare. tool_extras.codex keys requiring TOML quoting, such as keys containing spaces, will not match and their multiline values remain escaped despite the stated extras coverage.
  • [P2 non-blocking] src/hook-handlers.ts:473 can itself throw when the caught value is null or undefined, defeating the handler’s non-fatal guarantee. Format unknown thrown values safely instead of asserting Error.

The delivery check compares the delivered copy byte for byte with what
pullItem renders, so the fixture has to be the render. Since Tencent#749 a
multi-line `instructions` renders as a TOML literal (```'''```) that keeps
the real newline, rather than smol-toml's basic string with a ```\n```
escape — the codex agent check therefore reported a stale copy where the
file was exactly what the CLI had just written.
The substitution passed the generated TOML as a *string* replacement, so
JavaScript expanded `$&`, `$$`, `$'` and the backtick form inside the
agent-authored value. A multi-line shell instruction carrying `$$` lost a
dollar and no longer round-tripped. It now uses a function replacement,
whose return value is used verbatim.

`canUseTomlLiteral` also now refuses U+007F DEL. TOML 1.0 bans it from
literal strings along with C0, and it sits outside the `\x00-\x1f` block
the predicate already covered, so it needed its own alternative.

Tests: a `$$`/`$&` prompt round-trips byte-for-byte, and a DEL-carrying
value still renders a document that parses. The `$$` case is RED without
the replacement callback.
@ydflow
ydflow force-pushed the fix/codex-toml-multiline-literal branch from 5a1dfb1 to 8596077 Compare September 23, 2026 13:42
@github-actions

Copy link
Copy Markdown

Findings

  • [P1 blocking] The PR description still lacks the required real-CLI/end-to-end verification record. Updating an e2e fixture and reporting unit/full-suite totals does not document npm run build followed by an actual CLI workflow and its result.
  • [P2 non-blocking] src/resources/agent-format.ts:486 constructs the match using the raw key, but smol-toml quotes non-bare keys. Valid extras such as "custom prompt" or "foo.bar" therefore remain escaped instead of using the promised multiline literal form.
  • [P2 non-blocking] src/resources/agent-format.ts:483 only examines top-level string values. Multiline strings inside valid tool_extras.codex tables, preserved by reverseFromCodex, remain escaped, so the stated extras coverage is incomplete.

Resolved

  • Dollar-sign replacement expansion is fixed with a replacement callback.
  • U+007F DEL now correctly falls back to a basic string.
  • The unrelated votes-sync logging changes and unsafe caught-value handling are absent from the current PR diff.

@jeff-r2026
jeff-r2026 merged commit dad371c into Tencent:main Sep 23, 2026
10 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
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.

fix(agents): render codex TOML with multi-line strings so developer_instructions stays readable

2 participants