Skip to content

Studio: a link reference definition with a label over 200 characters can be committed away, breaking the reference #9540

Closed

Description

@danielhanchen

LINK_DEFINITION_RE in studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts is:

const LINK_DEFINITION_RE = /\[(?:\\.|[^\]\n\\]){1,200}\]:/;

updateLinkDefinitionParity uses it to hold link reference definitions in the live tail, so Marked's document-wide definition map stays consistent. A definition whose label is longer than 200 characters is not recognised, so the incremental cache can commit it into an independently parsed block. A later reference in the live tail then has no definition to resolve against and renders as literal text.

CommonMark caps a link label at 999 characters, so every label from 201 to 999 is valid and missed.

Reproduced by streaming a definition, then enough prose to clear the rollback window, then the reference:

label length definition committed away reference still in the live tail link resolves
180 no yes yes
250 yes yes no
600 yes yes no

Raising the bound to 999 is the obvious fix, but it is not free and should not land without a measurement. parity.linkDefinition blocks commits, so a wider bound keeps more text in the live tail for longer, and the live tail is re-repaired and re-lexed on every token. Whoever takes this should put the per-chunk cost next to it, on a reply that actually contains a long definition label, before and after.

Found by Codex review on #9477, which touches that file but not those lines. Filing separately so it does not ride on a contributor's performance PR.

Activity

  1. HughScott2002 commented on Aug 24, 2026

    @HughScott2002
    Contributor

    Working on this — PR shortly.

    One finding worth surfacing before it lands, since it changes the shape of the fix. Raising the bound to 999 is right, but not because CommonMark caps the label there. Marked's own def rule has no cap at all:

    /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/
    

    so it registers labels past 999, and 999 leaves the divergence reachable — measured at 1000, 1200 and 2000.

    Unbounded is the exact match for the lexer and does close those, but I measured it before reaching for it and it is the wrong trade: the bound is what keeps the scan linear. Every [ on a line is a start position, so a bound of B costs O(n·B) per line while no bound costs O(n²). On one long line dense with [ that never reaches ]::

    line 200 999 unbounded
    50k 3.01ms 14.56ms 360.44ms
    100k 6.51ms 27.72ms 1257.36ms

    Ordinary multi-line text is identical across all three (0.53ms per 50k), since the class excludes \n and the scan is capped by the line.

    So the PR takes 999 and documents the residual past it as deliberate, rather than silently leaving it.

    On the per-chunk cost you asked for: the honest comparison is not before/after on the same label, since a 250-character label was cheap before precisely because it was committed away. Against the 200-character label that is already held today at 75.1us/chunk, a 250-character label after the fix costs 51.5us/chunk on the same reply. Replies with no long definition come out at +6% or below. Full numbers in the PR.

    Also filed #9633 for an adjacent gap found while measuring this: Marked's [^\]]+ admits a newline, so a label spanning lines is registered by Marked and missed by every bound here. It reproduces at 200 too, so it is not introduced by this fix, and it needs its own measurement — keeping it off this PR.

  2. HughScott2002 commented on Aug 24, 2026

    @HughScott2002
    Contributor

    Update: folding #9633 into this PR after measuring it.

    I said above I would keep it off this one. That was based on a cost argument I have since measured and found wrong — the {1,999} bound still caps the scan when the newline exclusion goes, so it stays linear, and per chunk through the cache the difference is 0%. Details on #9633.

    Both are the same probe having drifted from Marked's def rule ([^\]]+: no length cap, no exclusion of a line ending), each independently reachable through the same code path, and independent of each other — with the bound already at 999 and \n still excluded, the newline case still diverges at prefix 127. One regex, one rationale, so one PR.

  3. added a commit that references this issue on Sep 7, 2026
    72cfa2a
  4. added a commit that references this issue on Sep 20, 2026
    9cf76c1
  5. added 2 commits that reference this issue on Sep 29, 2026
    8c69546
    0c4a275
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