Skip to content

Studio: a link reference definition whose label spans lines is not held in the live tail #9633

Closed

Description

@HughScott2002

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

  1. HughScott2002 commented on Aug 24, 2026

    @HughScott2002
    ContributorAuthor

    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 \n exclusion "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:

    • the live tail is capped at STALLED_TAIL_CHARACTERS
    • a fenced block returns from updateLinkDefinitionParity before the regex runs
    • the full-document path never reaches findCommitBoundary

    Per chunk through IncrementalMarkdownCache, base against head:

    case base +999 +999 and line ending
    bracket-heavy, unbalanced 1464.5us 1468.3us 1473.1us (0%)
    citations, balanced 1545.2us 1548.8us 1548.9us (0%)

    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]: /url as foo 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 def rule, 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 \n still 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.

  2. added a commit that references this issue on Sep 7, 2026
    72cfa2a
  3. added a commit that references this issue on Sep 20, 2026
    9cf76c1
  4. added a commit that references this issue on Sep 29, 2026
    8c69546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions