Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(shared): bound the file-link label so bracket runs stop rescanning (#5782)
Tyler062b4618c229
diff --git a/packages/shared/src/composerInlineTokens.test.ts b/packages/shared/src/composerInlineTokens.test.tsindex 5a7c14f17..81fd6add2 100644--- a/packages/shared/src/composerInlineTokens.test.ts+++ b/packages/shared/src/composerInlineTokens.test.ts@@ -129,4 +129,25 @@ describe("collectComposerInlineTokens", () => {},]);});++ it("still collects a file link whose label is at the length cap", () => {+ const label = `${"a".repeat(508)}.tsx`;+ const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);++ expect(tokens).toHaveLength(1);+ expect(tokens[0]?.value).toBe(`src/${label}`);+ });++ it("leaves a file link past the label cap as plain text", () => {+ const label = `${"a".repeat(509)}.tsx`;+ expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);+ });++ it("stays fast on unterminated bracket runs", () => {+ // Unbounded, the label body rescanned the rest of the text from every+ // whitespace: this input took seconds.+ const started = performance.now();+ expect(collectComposerInlineTokens(" [[".repeat(40_000))).toEqual([]);+ expect(performance.now() - started).toBeLessThan(1_000);+ });});diff --git a/packages/shared/src/composerInlineTokens.ts b/packages/shared/src/composerInlineTokens.tsindex dda548059..11a5accf3 100644--- a/packages/shared/src/composerInlineTokens.ts+++ b/packages/shared/src/composerInlineTokens.ts@@ -20,7 +20,21 @@ export interface CollectComposerInlineTokensOptions {const SKILL_TOKEN_REGEX = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s)/g;const MENTION_TOKEN_REGEX = /(^|\s)@(?:"((?:\\.|[^"\\])*)"|([^\s@"]+))(?=\s)/g;-const FILE_LINK_TOKEN_REGEX = /(^|\s)\[((?:\\.|[^\]\\])*)\]\(([^)\s]+)\)(?=\s)/g;+/**+ * The label body is bounded rather than `*`. Unbounded, every whitespace in+ * the composer is a candidate start: the engine scans the rest of the text for+ * a closing `]`, fails, and rescans from the next whitespace — quadratic on+ * input like " [[[[[…". A cap makes each attempt constant-bounded.+ *+ * Only a basename ever survives the `label !== basename` check below, so this+ * cannot reject a link a user could meaningfully write; the longest filename+ * any common filesystem allows is 255.+ */+const MAX_FILE_LINK_LABEL_LENGTH = 512;+const FILE_LINK_TOKEN_REGEX = new RegExp(+ `(^|\\s)\\[((?:\\\\.|[^\\]\\\\]){0,${MAX_FILE_LINK_LABEL_LENGTH}})\\]\\(([^)\\s]+)\\)(?=\\s)`,+ "g",+);const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;// Autocomplete emits canonical file links, so ambiguous bare @scope/package text stays a package.