Skip to content

fix(opencode): instruct all chat agents to use KaTeX-compatible math syntax#15054

Open
alberti42 wants to merge 1 commit intoanomalyco:devfrom
alberti42:fix-math-formatting-rules
Open

fix(opencode): instruct all chat agents to use KaTeX-compatible math syntax#15054
alberti42 wants to merge 1 commit intoanomalyco:devfrom
alberti42:fix-math-formatting-rules

Conversation

@alberti42
Copy link

Issue for this PR

Closes #15053

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The opencode frontend renders math with KaTeX, which requires $...$ for
inline equations and $$...$$ (flush-left, surrounded by blank lines) for
display equations. Without explicit instructions, LLMs default to \(...\)
and \[...\] or wrap display equations in fenced code blocks — none of which
KaTeX can render. The result is raw LaTeX source appearing in the chat instead
of rendered formulas.

This PR adds a shared math_formatting_rules.txt file and injects it into
every primary-agent system prompt via a {MATH_FORMATTING_RULES} placeholder.
The placeholder is inserted inside the existing formatting/style section of
each prompt rather than appended at the end. This matters: LLMs follow
instructions better when related rules are grouped together. Appending at the
end (e.g. via AGENTS.md) would scatter formatting guidance across the
prompt, with general style rules at the top and math rules at the bottom.
Placing them together gives the agent a coherent picture of all formatting
expectations in one place.

beast.txt (used for sub-agents that never produce chat output) is excluded
intentionally. The token overhead is negligible (~13 lines).

One implementation note worth calling out: the injection uses
prompt.replace("{MATH_FORMATTING_RULES}", () => rules) with a function
rather than a plain string. JavaScript's replace() treats $ specially in
replacement strings (e.g. $` inserts the portion before the match), so
a plain string would silently corrupt the LaTeX delimiters. Passing a function
bypasses this entirely.

How did you verify your code works?

Tested manually with OpenAI (GPT-4o / GPT-5), Claude (claude-sonnet), and
Gemini (gemini-2.5-pro) by asking each agent to write a response containing
both inline and display equations. All three correctly produced KaTeX-renderable
output after the fix.

Screenshots / recordings

Before — raw LaTeX source dumped as plain text, unreadable:

before-this-fix

After — equations rendered by KaTeX, properly formatted:

after-this-fix

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.

bug: math equations rendered as raw LaTeX source — agents not instructed to use KaTeX-compatible syntax

1 participant