dsh-math-tex: Fix KaTeX error (red) formulas before persistence

Introduction

When doing mathematical derivations in DeepSeek Harness (DSH), the interface renders formulas in Markdown with KaTeX. Once the model outputs delimiters, macros, or structures that don’t match the parser’s expectations, KaTeX doesn’t error out and exit—instead, it draws the raw source code directly on the screen in the error color #cc0000. The entire answer may therefore turn “a sea of red,” and the conversation history will retain the same unreadable text. The frontend bundle lives inside app.asar with empty KaTeX options, so errorColor or maxExpand cannot be changed via configuration afterwards.

dsh-math-tex is a host-side DSH plugin published by maintainer EnhydraLutristus (category: networking tool). Before text is written to persistence and pushed to the frontend, it validates and repairs math regions that would render in red, using the same version of KaTeX as the client, while also using system prompt rules to reduce the probability of the model making mistakes. Below we describe its positioning, two lines of defense, installation and configuration, and usage boundaries.

What Is This

  • Name: dsh-math-tex (npm package name dsh-math-tex, version 1.2.11 in current materials)
  • Repository: EnhydraLutristus/dsh-math-tex
  • Positioning: A pure host plugin, with no client-side component. The renderer cannot be modified; repairs happen on the llm/stream waterfall while streaming output is still under host control.
  • Problem solved: Keep DSH math answers readable—after streaming chunks are rewritten by the guard, they enter the persisted message body, real-time frontend frames, and context compression all at once, so the screen and conversation output stay consistent.

The plugin declares peer-dependency compatibility with @deepseek-ai/dsh ^0.2.0-rc.2 and others in package.json; the runtime requires Node ^22.19 || >=24. The license is MIT.

Two Lines of Defense

Prevention: System Prompt Rules

Typography rules are injected via systemPrompt.section (display equations on their own lines, only formulas inside $$, no formulas glued to headings, etc.), so the model produces fewer structures that break KaTeX.

The promptRules config controls injection scope:

Value Behavior
auto (default) Only inject into sessions where math has already appeared
full Inject all nine rules into every session
minimal Keep only the two red-text-prevention rules
off Do not inject

Fallback: llm/stream Guard

The outer waterfall for each streaming model call will:

  1. Split math into regions (recognizing $…$ / $$…$$, as well as \(…\) / \[…\]—four kinds of delimiters);
  2. Render each region with the same version of KaTeX as the client;
  3. If error color appears in the output (judgment cannot rely solely on “whether an exception was thrown”—for example, \htmlClass{red}{x} can succeed on a strict call yet still display in red), then attempt repairs in a fixed order before letting it pass.

Repair order (no guessing at math, no deleting formula content):

  1. Re-format—when the formula itself is correct but the surrounding structure is wrong, change it into canonical forms like $$\n…\n$$;
  2. Targeted rescue—only when there is definite semantics: strip the backslash before unknown commands, or regroup duplicate subscripts; after modification it must be re-rendered and strictly pass with no red before being accepted;
  3. Degrade—block regions that cannot be repaired get wrapped in code fences; inline ones lose their delimiters while the source is preserved, so readers won’t see red text.

Text without $ and \ passes through byte-for-byte; non-math body text is not rewritten.

Installation and Activation

Run the official installation script in the plugin source directory (copies to profile and registers the bundle):

node tools/install.mjs              # copy to profile and register bundle
node tools/install.mjs --dry-run    # only report what would change
node tools/install.mjs --uninstall  # remove from bundle list

After installation you must start a new session: the bundle stack is read when the session’s runner process starts, and the current session will not reload it.

Manual mounting: place this package into <profile>/node_modules/ and add the package name to dsh.profile.bundles in the profile’s package.json.

Community directory page (no official affiliation with DeepSeek / High-Flyer): dsh-math-tex on skillhub.cn.

Configuration

Configure by id in the profile’s cordis.patch.yml, for example:

- id: dsh-math-tex
  name: dsh-math-tex
  config:
    repair: auto
    logFile: C:/tmp/math-tex.log
Option Default Description
enabled true When false, neither line of defense is mounted
promptRules auto See above
repair auto auto rewrites / check-only only logs without rewriting / off lets everything pass
tailBuffer 15 Minimum number of characters held back, to avoid completion markers leaking character by character
maxHold 600 Maximum number of characters to wait for a single unclosed region; beyond this limit it is passed through unchanged and that block is no longer repaired
logFile '' Diagnostic log path; if empty, only uses the host logger
katexPath '' Specify the KaTeX path; by default resolved per profile
macros {} Macros passed to the renderer, consistent with the client

Unknown configuration keys will raise an error rather than being silently ignored. When enabled is false or repair is off, behavior is identical to the plugin not being installed; the pass-through policy when maxHold is exceeded is consistent with the “rather not interfere than risk stalling” design.

Typical Usage and Diagnostics

Confirm the Plugin Is Hooked Up

Similar lines may appear in the host log (the last line indicates real turns are processed by the guard):

oracle: katex 0.16.47
prompt section "math-output-format" registered (auto)
llm/stream hooked model=deepseek-flash mode=auto
prompt rules now apply to session session-8c6a99cc-…
guarded stream: 214 text delta(s) in, 231 out

in should be much greater than 1 (the self-test fake stream is 1).

Locally Inspect How a Text Is Judged and Repaired

node tools/inspect-fixture.mjs                    # default: checks test/fixtures/real-broken.txt
node tools/inspect-fixture.mjs path/to/text.md    # any Markdown/text

This prints the region count, reasons for red-flagging, repair actions, and whether there are still red-rendering regions after repair.

Troubleshooting Real Sessions

node tools/find-red-session.mjs                   # scan local sessions, list answers that would still render in red
node tools/strip-sentinel.mjs --list
node tools/strip-sentinel.mjs --id <prefix> --yes
node tools/inspect-session.mjs <session.jsonl>
node tools/probe-region.mjs 'd\alpha=0'

Note: what is stored in the persisted records is the model’s original text; what the guard modifies is the outward streaming output. Having “bad formulas” in the records does not mean the current interface is still red—take the output of the same stream as authoritative.

Run regression tests in the plugin directory:

npm test

Use Cases and Cautions

For whom: developers who frequently ask the model in DSH to write derivations, proofs, or technical notes containing LaTeX, and who have encountered KaTeX red text polluting entire answers.

Design boundaries (explicitly stated in the README):

  • The frontend KaTeX bundle cannot be changed; only host-side interception is possible;
  • Both waiting and holding have upper bounds to avoid stalling the stream;
  • Math content will not be deleted; the worst case is display in source form.

Security and permissions: the plugin runs with the current DSH process’s permissions; it hooks llm/stream and may write logFile. Read the repository source and MIT license before installing, and only enable it in trusted profiles.

The DSH ecosystem emphasizes “everything is a plugin”; the community directory is an independent site, and this article does not describe it as an official app store.

Conclusion

dsh-math-tex uses prompt rules plus real KaTeX rendering validation to intercept and repair formula regions that would display in red before they are written to disk, keeping streaming math answers readable on both the interface and the output. For details, test descriptions, and design documents, see the repository README and docs/, STATUS.md.

  • Directory page: https://www.skillhub.cn/plugins/EnhydraLutristus/dsh-math-tex
  • GitHub: https://github.com/EnhydraLutristus/dsh-math-tex
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye