fix(opencode): instruct all chat agents to use KaTeX-compatible math syntax#15054
Open
alberti42 wants to merge 1 commit intoanomalyco:devfrom
Open
fix(opencode): instruct all chat agents to use KaTeX-compatible math syntax#15054alberti42 wants to merge 1 commit intoanomalyco:devfrom
alberti42 wants to merge 1 commit intoanomalyco:devfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #15053
Type of change
What does this PR do?
The opencode frontend renders math with KaTeX, which requires
$...$forinline equations and
$$...$$(flush-left, surrounded by blank lines) fordisplay equations. Without explicit instructions, LLMs default to
\(...\)and
\[...\]or wrap display equations in fenced code blocks — none of whichKaTeX can render. The result is raw LaTeX source appearing in the chat instead
of rendered formulas.
This PR adds a shared
math_formatting_rules.txtfile and injects it intoevery 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 theprompt, 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 excludedintentionally. The token overhead is negligible (~13 lines).
One implementation note worth calling out: the injection uses
prompt.replace("{MATH_FORMATTING_RULES}", () => rules)with a functionrather than a plain string. JavaScript's
replace()treats$specially inreplacement strings (e.g.
$`inserts the portion before the match), soa 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:
After — equations rendered by KaTeX, properly formatted:
Checklist