Repository navigation
Description
LINK_DEFINITION_RE in studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts excludes a newline from the label:
const LINK_DEFINITION_RE = /\[(?:\\.|[^\]\n\\]){1,999}\]:/;Marked's def rule does not:
/^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/
[^\]]+ admits a newline, so Marked registers a definition whose label spans lines. updateLinkDefinitionParity never sees one, parity.linkDefinition stays false, and the definition can be committed into an independently parsed block while a twin is still live. Marked emits no token for a label it has already seen, so the two lex apart — the same failure mode as #9540, reached through the character class rather than the bound.
Measured
Marked's own verdict, and the regex's:
| input | Marked registers | LINK_DEFINITION_RE |
|---|---|---|
[foo]: /url |
yes | yes |
[foo\nbar]: /url |
yes | no |
Reachable through the cache on the repeated-definition shape already used by MARKDOWN_CASES:
const source = `[foo\nbar]: /url\n\n${paragraphs(12)}[foo\nbar]: /url\n\nq\n\n`;streamed one character at a time, comparing render.parseMarkdownIntoBlocks(render.markdown) against parseMarkdownIntoBlocks(remend(input)) at every prefix. First mismatch at prefix 127:
prefix: "[foo\nbar]: /url\n\npart 0\n\n…part 11\n\n[foo\nbar]: /"
incremental: [ …, "part 11", "\n\n", "[foo\nbar]: /" ]
full doc : [ …, "part 11", "\n\n" ]
The incremental list carries a trailing block the full-document lex does not.
Measured against 1d8deefc4 (the #9540 fix, bound at 999). The bound is not what admits this: every candidate bound excludes \n, so it reproduces at 200 as well.
Why this is not a one-character fix
Dropping \n from the class is the obvious change and should not land without a measurement. The exclusion is what keeps the scan bounded by the line: every [ is a start position, and today one that never reaches ]: stops at the end of its line. Without it an unmatched [ scans forward through the whole tail, so the per-line O(n·B) becomes something the reply length drives.
That is the same cost axis measured for #9540, where removing the upper bound turned a linear scan quadratic — 100k characters on one [-dense line went from 6.51ms at bound 200 and 27.72ms at 999 to 1257.36ms unbounded. Whoever takes this should put the per-chunk cost next to it, on a reply that actually contains a multi-line definition label, before and after.
Worth confirming first how often a multi-line label is worth paying for. CommonMark allows it, and Marked accepts it, but a model emitting one is rarer than the >200-character label in #9540 — so the measurement may well argue for leaving this open deliberately, the way the >999 case is left open in the comment on that fix.
Found while fixing #9540. Filing separately so it does not ride on that PR.
Activity
HughScott2002 commentedon Aug 24, 2026
Measured it, and the cost argument I filed this under is wrong. Correcting that, since it was the whole reason for keeping this separate.
I wrote that dropping the
\nexclusion "lets an unmatched[scan forward through the whole tail, so the per-line O(n·B) becomes something the reply length drives." That is not what happens. The{1,999}bound stays in place and still caps every start position, so the scan remains O(n·B) and linear. The exclusion was buying an earlier stop, not the bound.Isolated, the regex does get more expensive — bracket-dense prose goes from 0.23–0.68ms to about 18ms per 50k, because a start position no longer stops at the end of its line. That cost does not reach the cache, for three reasons already in the file:
STALLED_TAIL_CHARACTERSupdateLinkDefinitionParitybefore the regex runsfindCommitBoundaryPer chunk through
IncrementalMarkdownCache, base against head:The balanced case is the one that counts: unbalanced brackets stall into the full-document path where the regex never runs, so only a balanced bracket-dense reply exercises it on every block. Both are 0%.
Also worth recording, since my original report inferred it from token shapes rather than reading it: ground truth is Marked's own definition map,
lexer.tokens.links. It registers[foo\nbar]: /urlasfoo bar— the label's whitespace is normalised, so a label that soft-wraps registers as though it never had.Given that, this is not a separate trade-off from #9540. Both are the same probe having drifted from Marked's
defrule, whose label is[^\]]+— no length cap, no exclusion of a line ending — and each is independently reachable through the same code path. They are also independent: with the bound already raised to 999 and\nstill excluded, this still diverges at prefix 127.So I have folded it into the #9540 PR rather than leaving it open. Both fixes, one regex, one rationale.
fix(studio): track Marked's def rule in the link definition probe
fix(studio): track Marked's def rule in the link definition probe (#9645
fix(studio): track Marked's def rule in the link definition probe (un…