Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
fix(studio): widen the link reference probe to match the definition probe (#11375)
Daniel Han949ce6999d96
diff --git a/studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts b/studio/frontend/src/components/assistant-ui/streaming-render-schedule.tsindex 4e4083ba89d..ae191413407 100644--- a/studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts+++ b/studio/frontend/src/components/assistant-ui/streaming-render-schedule.ts@@ -143,8 +143,74 @@ function isCodeBlock(block: string): boolean {const backtick = BACKTICK_OPENER_RE.exec(block);return backtick === null || !backtick[1].includes("`");}+// Must admit exactly what `LINK_DEFINITION_RE` admits: a label resolves only when BOTH ends+// carry it, so a narrower cap here made the wider one there unreachable (unslothai/unsloth#9540).const LINK_REFERENCE_RE =- /!?\[(?:\\.|[^\]\n\\]){1,200}\]\[(?:\\.|[^\]\n\\]){0,200}\]/;+ /!?\[(?:\\[\s\S]|[^\]\\]){1,999}\]\[(?:\\[\s\S]|[^\]\\]){0,999}\]/u;+// Label side as above, plus `[` and the optional `!`; the reference side needs no `!`.+const LINK_REFERENCE_WINDOW = 999 * 3 + 3;+// The `[` at the seam restarts escape parity, so the reference label is ONE candidate: the text+// up to the first unescaped `]`. Tested once here instead of from every `[` in the window.+const LINK_REFERENCE_LABEL_RE = /^(?:\\[\s\S]|[^\]\\]){0,999}$/u;++function unescapedClose(text: string, from: number): number {+ for (let i = text.indexOf("]", from); i >= 0; i = text.indexOf("]", i + 1)) {+ if (!isEscaped(text, i)) {+ return i;+ }+ }+ return -1;+}+// Same predicate as the regex over the whole reply, scanned from the rare `][` as+// `hasLinkDefinition` scans from `]:`. Neither label admits a bare `]`, which bounds a candidate.+function hasLinkReference(text: string): boolean {+ let bracket = text.indexOf("[");+ let nextBracket = bracket < 0 ? -1 : text.indexOf("[", bracket + 1);+ let close = -1;+ let nextClose = text.indexOf("]");+ let after = -1;+ for (let mid = text.indexOf("]["); mid >= 0; mid = text.indexOf("][", mid + 1)) {+ // No empty label, so the opener is at `mid - 2` or earlier. Forward: `lastIndexOf` is not.+ while (nextBracket >= 0 && nextBracket <= mid - 2) {+ bracket = nextBracket;+ nextBracket = text.indexOf("[", bracket + 1);+ }+ while (nextClose >= 0 && nextClose < mid) {+ if (!isEscaped(text, nextClose)) {+ close = nextClose;+ }+ nextClose = text.indexOf("]", nextClose + 1);+ }+ if (after < mid + 2) {+ after = unescapedClose(text, mid + 2);+ if (after < 0) {+ // Nothing closes from here on, so no later seam can either. Returning rather than+ // caching -1, which sits behind every later seam and rescans the tail: quadratic.+ return false;+ }+ }+ if (after - mid - 2 > LINK_REFERENCE_WINDOW) {+ continue;+ }+ let start = mid < LINK_REFERENCE_WINDOW ? 0 : mid - LINK_REFERENCE_WINDOW;+ if (close > start) {+ start = close + 1;+ }+ if (bracket < start || bracket > mid - 2) {+ continue;+ }+ // After the opener test, so a seam with no opener pays neither.+ if (!LINK_REFERENCE_LABEL_RE.test(text.slice(mid + 2, after))) {+ continue;+ }+ // `start`, not `bracket`: the class admits `[` inside a label, so an earlier one can+ // match where the last one fails. Skip test only, as in `hasLinkDefinition`.+ if (LINK_REFERENCE_RE.test(text.slice(start, after + 1))) {+ return true;+ }+ }+ return false;+}// Still the first line of a single block, for `updateLinkDefinitionParity` below.const FENCED_CODE_BLOCK_RE = /^ {0,3}(?:```|~~~)/;const WORD_CHARACTER_RE = /[\p{L}\p{N}_]/u;@@ -187,9 +253,12 @@ function blocksOf(markdown: string): readonly string[] {// not, so the scope would otherwise follow the reply's line ending. NOT for// `blocksOf`, whose one memo slot is shared with `parseMarkdownIntoRenderableBlocks`:// a normalised copy misses it and costs a CRLF reply two splits per render.+// Definition first is a cost decision: both are pure so the conjunction is unchanged, but only+// the one asked SECOND is skipped, and the reference scan is the dearer. `][` without a `]:` is+// the shape that separates them.function documentProse(markdown: string): string | null {const normalized = normalizeLineEndings(markdown);- if (!LINK_REFERENCE_RE.test(normalized) || !hasLinkDefinition(normalized)) {+ if (!hasLinkDefinition(normalized) || !hasLinkReference(normalized)) {return null;}const prose = normalizeLineEndings(@@ -197,7 +266,7 @@ function documentProse(markdown: string): string | null {.filter((block) => !isCodeBlock(block)).join("\n"),);- return LINK_DEFINITION_LINE_RE.test(prose) && LINK_REFERENCE_RE.test(prose)+ return LINK_DEFINITION_LINE_RE.test(prose) && hasLinkReference(prose)? prose: null;}diff --git a/studio/frontend/tests/link-definition-oracle.test.ts b/studio/frontend/tests/link-definition-oracle.test.tsindex 537c9ef3781..9a5289527c1 100644--- a/studio/frontend/tests/link-definition-oracle.test.ts+++ b/studio/frontend/tests/link-definition-oracle.test.ts@@ -151,6 +151,37 @@ test("a reply whose reference only resolves in one piece is never split into blo);});+test("a reference is never split into blocks because its label is long", () => {+ // Every case above uses a one-character label. Length is the one dimension the probes bound and+ // the only one this file never varied, which is why #9540 and #9645's half-fix were invisible.+ // In scope only when the two paths really differ, so past 999 drops out and this cannot pass+ // vacuously.+ const failures: string[] = [];+ let inScope = 0;+ for (const length of [1, 2, 199, 200, 201, 400, 998, 999, 1000, 1001, 2000]) {+ const label = "L".repeat(length);+ for (const reply of [+ `See [guide][${label}].\n\nplain prose between them\n\n[${label}]: /guide\n`,+ `[${label}]: /guide\n\nplain prose between them\n\nSee [guide][${label}].\n`,+ ]) {+ if (asOneDocument(reply) <= asBlocks(reply)) {+ continue;+ }+ inScope += 1;+ if (markdownRenderScope(reply) !== "document") {+ failures.push(`label length ${length}`);+ }+ }+ }+ assert.ok(inScope > 0, "no length lost an anchor when split, so this test proved nothing");+ assert.deepEqual(+ failures,+ [],+ "these labels resolve their reference only when the reply is rendered in one piece, but the " ++ `scan split them into blocks, so the reference renders as literal text: ${failures.join(", ")}`,+ );+});+test("line endings other than LF do not hide the definition", () => {for (const reply of ["~~~ts\rconst x = 1;\r~~~\r\rSee [guide][g].\r\r[g]: /guide\r",diff --git a/studio/frontend/tests/streaming-render-schedule.test.ts b/studio/frontend/tests/streaming-render-schedule.test.tsindex 5a77d4b6dfe..a267e152c74 100644--- a/studio/frontend/tests/streaming-render-schedule.test.ts+++ b/studio/frontend/tests/streaming-render-schedule.test.ts@@ -897,3 +897,70 @@ test("a reply dense with `]:` and no definition does not pay per occurrence", ()assert.ok(invalidMedian < 100,`500k of \`[]:\` cost ${invalidMedian.toFixed(1)}ms; invalid candidates are being rescanned`);});++test("a reference label past the old cap still resolves against its definition", () => {+ // A label resolves only when BOTH probes admit it, and the definition one moved to 999 while+ // this one stayed at 200, so 201..999 stayed on the blocks path (unslothai/unsloth#9540).+ for (const length of [200, 201, 400, 999]) {+ const label = "L".repeat(length);+ const reply = `See [guide][${label}].\n\n[${label}]: https://x.test/a\n`;+ assert.equal(markdownRenderScope(reply), "document",+ `a ${length}-character label did not reach the document path`);+ assert.equal(markdownRenderKey(reply), `document:[${label}]: https://x.test/a`);+ }+ // 999 is CommonMark's, not Marked's, whose `def` label is uncapped but only SPLITS here. The+ // render is remark, CommonMark-strict: 0 links past 999, so the blocks path loses nothing.+ const tooLong = "L".repeat(1000);+ assert.equal(markdownRenderScope(`See [guide][${tooLong}].\n\n[${tooLong}]: /u\n`), "blocks");+});++test("a run of `[` before a reference does not walk the widened label budget", () => {+ // A run of `[` is one start position per character, so the widened budget is paid at each:+ // 1743ms at 500k against 7.7ms bounding each seam. Loose, so a slow runner still separates them.+ const run = `${"[".repeat(500000)}][x]`;+ for (let i = 0; i < 3; i += 1) markdownRenderScope(run + " ");+ const runs: number[] = [];+ for (let i = 0; i < 5; i += 1) {+ const t0 = performance.now();+ markdownRenderScope(run + " ".repeat(i));+ runs.push(performance.now() - t0);+ }+ const median = runs.sort((a, b) => a - b)[2]!;+ assert.ok(median < 150,+ `500k of \`[\` cost ${median.toFixed(1)}ms; the reference probe is walking every start position`);+});++test("a reply whose every `]` is escaped does not rescan the tail per seam", () => {+ // `\][` leaves no unescaped `]` after the first seam, so the lookahead comes back empty and a+ // cached -1 would rescan the tail per seam: quadratic, 196s at 500k against 162ms narrow.+ const escaped = "\\][".repeat(166_666);+ for (let i = 0; i < 3; i += 1) markdownRenderScope(escaped + " ");+ const runs: number[] = [];+ for (let i = 0; i < 5; i += 1) {+ const t0 = performance.now();+ markdownRenderScope(escaped + " ".repeat(i));+ runs.push(performance.now() - t0);+ }+ const median = runs.sort((a, b) => a - b)[2]!;+ assert.ok(median < 150,+ `500k of \`\\][\` cost ${median.toFixed(1)}ms; an exhausted lookahead is being re-asked per seam`);+});++test("a seam whose reference label is past the cap is rejected once, not per `[`", () => {+ // The window bounds the slice, not the start positions in it: packed with `[` and a label past+ // the cap, it fails from every one and gives the window back nothing. Test that label once.+ const packed = `${"[".repeat(3000)}][${"y".repeat(3000)}]`.repeat(83);+ for (let i = 0; i < 3; i += 1) markdownRenderScope(packed + " ");+ const runs: number[] = [];+ for (let i = 0; i < 5; i += 1) {+ const t0 = performance.now();+ markdownRenderScope(packed + " ".repeat(i));+ runs.push(performance.now() - t0);+ }+ const median = runs.sort((a, b) => a - b)[2]!;+ assert.ok(median < 150,+ `500k of packed \`[\` cost ${median.toFixed(1)}ms; the window is re-tested from every \`[\``);+ // The rejection is a cost bound, not a change of answer: a label inside the cap still resolves.+ const label = "L".repeat(400);+ assert.equal(markdownRenderScope(`See [guide][${label}].\n\n[${label}]: /u\n`), "document");+});