fix: chat file links open the file agents cite #33943
Mergedjuliusmarmingemerged 11 commits into
main fromrefs/pull/33943/headmerged by juliusmarminge View on GitHubjuliusmarminge added labelsagent-context-increase
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/mobile/src/features/threads/ThreadFeed.tsx line 2897OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2893,4 +2894,4 @@28932894 props.workspaceRoot &&28942895 relativePath &&28952896 !/[\\/]$/.test(presentation.path) &&2896- needsWorkspaceBasenameLookup(relativePath) &&2897+ workspaceLinkLookupQuery(relativePath, props.workspaceRoot) !== null &&evals/mcp-tool-use/file-link-grading.ts line 40OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +20,21 @@20+ * Chat turns every file-shaped markdown link and inline code span into a pill resolved against21+ * the thread's cwd. A pill is broken when it lands on no world file or directory and the client's22+ * on-click lookup (basename, repeated prefix, unique suffix) finds no one file under the cwd.23+ */24+export const fileLinkPills = (25+ finalText: string,26+ cwd: string,27+ worldFiles: ReadonlyArray<string>,28+) => {29+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));30+ const sources = [31+ ...extractMarkdownLinkHrefs(finalText),32+ ...extractInlineCodeSpans(finalText).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []),33+ ];34+ return [...new Set(sources)].flatMap((source) => {35+ const target = resolveMarkdownFileLinkTarget(source, cwd);36+ if (target === null) return [];37+ const path = splitFilePathPosition(target).path.replace(/\/+$/, "");38+ // System paths such as /usr/bin/node are not what these cases test.39+ if (!/^\/(?:home|Users)\//.test(path)) return [];40+ const relative = splitFilePathPosition(source).path;
- evals/mcp-tool-use/file-link-grading.ts line 40OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +20,21 @@20+ * Chat turns every file-shaped markdown link and inline code span into a pill resolved against21+ * the thread's cwd. A pill is broken when it lands on no world file or directory and the client's22+ * on-click lookup (basename, repeated prefix, unique suffix) finds no one file under the cwd.23+ */24+export const fileLinkPills = (25+ finalText: string,26+ cwd: string,27+ worldFiles: ReadonlyArray<string>,28+) => {29+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));30+ const sources = [31+ ...extractMarkdownLinkHrefs(finalText),32+ ...extractInlineCodeSpans(finalText).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []),33+ ];34+ return [...new Set(sources)].flatMap((source) => {35+ const target = resolveMarkdownFileLinkTarget(source, cwd);36+ if (target === null) return [];37+ const path = splitFilePathPosition(target).path.replace(/\/+$/, "");38+ // System paths such as /usr/bin/node are not what these cases test.39+ if (!/^\/(?:home|Users)\//.test(path)) return [];40+ const relative = splitFilePathPosition(source).path;
- evals/mcp-tool-use/file-link-grading.ts line 33OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +13,21 @@13+} from "../../packages/client-runtime/src/workspaceBasenameLookup.ts";14+15+export const FileLinksExpect = Schema.Struct({16+ /** The final answer has at least this many file pills that open a world file. */17+ minWorking: Schema.Number,18+});19+20+/**21+ * Chat turns every file-shaped markdown link and inline code span into a pill resolved against22+ * the thread's cwd. A pill is broken when it lands on no world file or directory and the client's23+ * on-click lookup (basename, repeated prefix, unique suffix) finds no one file under the cwd.24+ */25+export const fileLinkPills = (26+ finalText: string,27+ cwd: string,28+ worldFiles: ReadonlyArray<string>,29+) => {30+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));31+ const sources = [32+ ...extractMarkdownLinkHrefs(finalText),33+ ...extractInlineCodeSpans(finalText).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []),
- apps/mobile/src/features/threads/ThreadFeed.tsx line 2897Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2893,4 +2894,4 @@28932894 props.workspaceRoot &&28942895 relativePath &&28952896 !/[\\/]$/.test(presentation.path) &&2896- needsWorkspaceBasenameLookup(relativePath) &&2897+ workspaceLinkLookupQuery(presentation.path, props.workspaceRoot) !== null &&
- evals/mcp-tool-use/file-link-grading.ts lines 64 to 64OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +44,21 @@44+ (span) => inlineCodeFilePathCandidate(span) ?? [],45+ ),46+ ];47+ return [...new Set(sources)].flatMap((source) => {48+ const target = resolveMarkdownFileLinkTarget(source, cwd);49+ if (target === null) return [];50+ const path = splitFilePathPosition(target).path.replace(/\/+$/, "");51+ // System paths such as /usr/bin/node are not what these cases test.52+ if (!/^\/(?:home|Users)\//.test(path)) return [];53+ const relative = parseMarkdownFileLink(source)?.path ?? path;54+ const entries = files.flatMap((file) =>55+ file.startsWith(`${cwd}/`)56+ ? [{ path: file.slice(cwd.length + 1), kind: "file" as const }]57+ : [],58+ );59+ const works =60+ files.some((file) => file === path || file.startsWith(`${path}/`)) ||61+ (!relative.startsWith("/") &&62+ // The client opens the first of several same-named files, which may be the wrong one.63+ (needsWorkspaceBasenameLookup(relative)64+ ? entries.filter((entry) => entry.path.split("/").at(-1) === relative).length === 1evals/mcp-tool-use/file-link-grading.ts line 50OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +30,21 @@30+ worldFiles: ReadonlyArray<string>,31+) => {32+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));33+ // A link whose label names its own file renders as one pill, so code in that label is no pill.34+ const withoutFileLabels = finalText.replace(35+ /\[([^\]\n]*)\]\(([^)\s]+)\)/g,36+ (link, label: string, href: string) =>37+ isMarkdownFileLinkLabel(label.replaceAll("`", ""), normalizeMarkdownLinkDestination(href))38+ ? `[](${href})`39+ : link,40+ );41+ const sources = [42+ ...extractMarkdownLinkHrefs(finalText),43+ ...extractInlineCodeSpans(withoutFileLabels).flatMap(44+ (span) => inlineCodeFilePathCandidate(span) ?? [],45+ ),46+ ];47+ return [...new Set(sources)].flatMap((source) => {48+ const target = resolveMarkdownFileLinkTarget(source, cwd);49+ if (target === null) return [];50+ const path = splitFilePathPosition(target).path.replace(/\/+$/, "");
- evals/mcp-tool-use/file-link-grading.ts lines 55 to 55OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +35,21 @@35+/**36+ * Chat turns every file-shaped markdown link and inline code span into a pill resolved against37+ * the thread's cwd. A pill is broken when it lands on no world file or directory and the client's38+ * on-click lookup (basename, repeated prefix, unique suffix) finds no one file under the cwd.39+ */40+export const fileLinkPills = (41+ finalText: string,42+ cwd: string,43+ worldFiles: ReadonlyArray<string>,44+) => {45+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));46+ // A link whose label names its own file renders as one pill, so code in that label is no pill.47+ const withoutFileLabels = finalText.replace(48+ /\[([^\]\n]*)\]\(([^)\s]+)\)/g,49+ (link, label: string, href: string) =>50+ isMarkdownFileLinkLabel(label.replaceAll("`", ""), normalizeMarkdownLinkDestination(href))51+ ? `[](${href})`52+ : link,53+ );54+ const sources = [55+ ...extractMarkdownLinkHrefs(finalText),
- evals/mcp-tool-use/file-link-grading.ts line 63OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +43,21 @@43+ worldFiles: ReadonlyArray<string>,44+) => {45+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));46+ // A link whose label names its own file renders as one pill, so code in that label is no pill.47+ const withoutFileLabels = finalText.replace(48+ /\[([^\]\n]*)\]\(([^)\s]+)\)/g,49+ (link, label: string, href: string) =>50+ isMarkdownFileLinkLabel(label.replaceAll("`", ""), normalizeMarkdownLinkDestination(href))51+ ? `[](${href})`52+ : link,53+ );54+ const sources = [55+ ...extractMarkdownLinkHrefs(finalText),56+ ...extractInlineCodeSpans(withoutFileLabels).flatMap(57+ (span) => inlineCodeFilePathCandidate(span) ?? [],58+ ),59+ ];60+ return [...new Set(sources)].flatMap((source) => {61+ const target = resolveMarkdownFileLinkTarget(source, cwd);62+ if (target === null) return [];63+ const path = NodePath.posix.normalize(splitFilePathPosition(target).path).replace(/\/+$/, "");
- evals/mcp-tool-use/file-link-grading.ts line 65OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +45,21 @@45+ * the thread's cwd. A pill is broken when it lands on no world file or directory and the client's46+ * on-click lookup (basename, repeated prefix, unique suffix) finds no one file under the cwd.47+ */48+export const fileLinkPills = (49+ finalText: string,50+ cwd: string,51+ worldFiles: ReadonlyArray<string>,52+) => {53+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));54+ // A link whose label names its own file renders as one pill, so code in that label is no pill.55+ // Fenced blocks render as code, never as pills.56+ const prose = finalText.replace(/```[\s\S]*?(?:```|$)/g, "");57+ const withoutFileLabels = prose.replace(58+ /\[([^\]\n]*)\]\(([^)\s]+)\)/g,59+ (link, label: string, href: string) =>60+ isMarkdownFileLinkLabel(label.replaceAll("`", ""), normalizeMarkdownLinkDestination(href))61+ ? `[](${href})`62+ : link,63+ );64+ const sources = [65+ ...extractMarkdownLinkHrefs(prose),
- @@ -0,0 +46,21 @@46+ * on-click lookup (basename, repeated prefix, unique suffix) finds no one file under the cwd.47+ */48+export const fileLinkPills = (49+ finalText: string,50+ cwd: string,51+ worldFiles: ReadonlyArray<string>,52+) => {53+ const files = worldFiles.map((file) => (file.startsWith("/") ? file : `${cwd}/${file}`));54+ // A link whose label names its own file renders as one pill, so code in that label is no pill.55+ // Fenced blocks render as code, never as pills.56+ const prose = finalText.replace(/```[\s\S]*?(?:```|$)/g, "");57+ const withoutFileLabels = prose.replace(58+ /\[([^\]\n]*)\]\(([^)\s]+)\)/g,59+ (link, label: string, href: string) =>60+ isMarkdownFileLinkLabel(label.replaceAll("`", ""), normalizeMarkdownLinkDestination(href))61+ ? `[](${href})`62+ : link,63+ );64+ const sources = [65+ // A link written inside inline code renders as code.66+ ...extractMarkdownLinkHrefs(prose.replace(/`[^`\n]+`/g, "")),evals/mcp-tool-use/file-link-grading.ts lines 86 to 93Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +59,21 @@59+ (link, label: string, href: string) =>60+ isMarkdownFileLinkLabel(label.replaceAll("`", ""), normalizeMarkdownLinkDestination(href))61+ ? `[](${href})`62+ : link,63+ );64+ const sources = [65+ // A link written inside inline code renders as code.66+ ...extractMarkdownLinkHrefs(prose.replace(/`[^`\n]+`/g, "")),67+ ...extractInlineCodeSpans(withoutFileLabels).flatMap(68+ (span) => inlineCodeFilePathCandidate(span) ?? [],69+ ),70+ ];71+ return [...new Set(sources)].flatMap((source) => {72+ const target = resolveMarkdownFileLinkTarget(source, cwd);73+ if (target === null) return [];74+ const path = normalizeAbsolutePath(splitFilePathPosition(target).path);75+ // System paths such as /usr/bin/node are not what these cases test; the workspace always is.76+ if (!path.startsWith(`${cwd}/`) && !/^\/(?:home|Users)\//.test(path)) return [];77+ const relative = parseMarkdownFileLink(source)?.path ?? path;78+ const entries = files.flatMap((file) =>79+ file.startsWith(`${cwd}/`)
juliusmarminge merged this3051b32
juliusmarminge closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed