@devries-cameron20 / t3code-1 / commits / 3d6ece2b29
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): file links the agent cited from another project open that project's file (#35036)

Julius Marminge committed Nov 12, 2026 at 02:47 UTC 3d6ece2b29f2c5b71a0b226af31d28d39440eaad
4 files changed +201 -69
apps/mobile/src/features/threads/ThreadFeed.tsx
+115 -61
@@ -233,9 +233,10 @@ import { ThreadMcpApp } from "./McpAppWebView";
233 import { assetEnvironment, useAssetUrl, useRefreshAssetUrl } from "../../state/assets";
234 import { useAtomQueryRunner } from "../../state/use-atom-query-runner";
235 import { readEnvironmentScope, usePreparedConnection } from "../../state/session";
236 -import { projectEnvironment } from "../../state/projects";
236 +import { environmentProjects, projectEnvironment } from "../../state/projects";
237 import {
238 needsWorkspaceBasenameLookup,
239 + pickProjectLinkMatch,
240 pickWorkspaceLinkMatch,
241 WORKSPACE_BASENAME_LOOKUP_LIMIT,
242 workspaceLinkLookupQuery,
@@ -260,6 +261,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
261 import { useV2ItemSupport } from "../../state/v2-item-support";
262 import {
263 fileRoutePathSegments,
264 + isAbsolutePath,
265 resolveFileLinkPath,
266 resolveWorkspaceFilePath,
267 resolveWorkspaceRelativeFilePath,
@@ -2838,6 +2840,85 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
2840 // A v1 link names no environment, so it falls back to this message's.
2841 return scopeThreadRef(props.environmentId, link.threadId);
2842 };
2843 + // The file a chat link names: in this workspace, else in the one other project that has it.
2844 + const findFileLinkTarget = async (
2845 + linkPath: string,
2846 + path: string,
2847 + signal: AbortSignal,
2848 + ): Promise<string> => {
2849 + const relativePath = resolveWorkspaceRelativeFilePath(props.workspaceRoot, path);
2850 + if (
2851 + !props.workspaceRoot ||
2852 + !relativePath ||
2853 + /[\\/]$/.test(linkPath) ||
2854 + workspaceLinkLookupQuery(linkPath, props.workspaceRoot) === null ||
2855 + !readEnvironmentScope(props.environmentId, AuthFilesystemReadScope)
2856 + ) {
2857 + return relativePath ?? path;
2858 + }
2859 + const workspaceRoot = props.workspaceRoot;
2860 + const originalPath = resolveWorkspaceFilePath(workspaceRoot, relativePath);
2861 + const queryOptions = { signal, reportFailure: false, reportDefect: false };
2862 + const searchRoot = (root: string) =>
2863 + executeAtomQuery(
2864 + appAtomRegistry,
2865 + projectEnvironment.searchEntries({
2866 + environmentId: props.environmentId,
2867 + input: {
2868 + cwd: root,
2869 + query: workspaceLinkLookupQuery(relativePath, root) ?? relativePath,
2870 + kind: "file",
2871 + limit: WORKSPACE_BASENAME_LOOKUP_LIMIT,
2872 + },
2873 + }),
2874 + queryOptions,
2875 + ).then((result) => ({
2876 + root,
2877 + entries: result._tag === "Success" ? result.value.entries : [],
2878 + }));
2879 + const lookup = async () => {
2880 + const own = await searchRoot(workspaceRoot);
2881 + let match = pickWorkspaceLinkMatch(relativePath, workspaceRoot, own.entries);
2882 + const otherProject = match === null;
2883 + if (otherProject) {
2884 + const threadProjectId = appAtomRegistry.get(
2885 + environmentThreadShells.threadShellAtom(
2886 + scopeThreadRef(props.environmentId, props.threadId),
2887 + ),
2888 + )?.projectId;
2889 + const otherRoots = appAtomRegistry
2890 + .get(environmentProjects.projectsAtom)
2891 + .flatMap((project) =>
2892 + project.environmentId === props.environmentId &&
2893 + project.id !== threadProjectId &&
2894 + project.workspaceRoot !== workspaceRoot
2895 + ? [project.workspaceRoot]
2896 + : [],
2897 + );
2898 + match =
2899 + otherRoots.length === 0
2900 + ? null
2901 + : pickProjectLinkMatch(relativePath, await Promise.all(otherRoots.map(searchRoot)));
2902 + }
2903 + if (!match || match === relativePath || signal.aborted) return relativePath;
2904 + const metadata = await executeAtomQuery(
2905 + appAtomRegistry,
2906 + projectEnvironment.fileMetadata({
2907 + environmentId: props.environmentId,
2908 + input: { path: originalPath },
2909 + }),
2910 + queryOptions,
2911 + );
2912 + // Another project's file never replaces a thread-local file the search missed.
2913 + return metadata._tag === "Success" &&
2914 + (needsWorkspaceBasenameLookup(relativePath) && !otherProject
2915 + ? metadata.value?.kind !== "directory"
2916 + : metadata.value === null)
2917 + ? match
2918 + : relativePath;
2919 + };
2920 + return lookup().catch(() => relativePath);
2921 + };
2922 const onMarkdownLinkPress = (href: string, imageEmbed = false) => {
2923 markdownLinkRequest.current?.abort();
2924 const request = new AbortController();
@@ -2905,8 +2986,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
2986 if (presentation.kind === "file") {
2987 const path = resolveFileLinkPath(props.workspaceRoot, presentation.path);
2988 if (path === null) return;
2908 - const relativePath = resolveWorkspaceRelativeFilePath(props.workspaceRoot, path);
2909 - const openAt = (target: string) => {
2989 + void findFileLinkTarget(presentation.path, path, request.signal).then((target) => {
2990 if (request.signal.aborted) return;
2991 void Haptics.selectionAsync();
2992 navigation.navigate("ThreadFile", {
@@ -2915,62 +2995,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
2995 path: fileRoutePathSegments(target),
2996 ...(presentation.line ? { line: String(presentation.line) } : {}),
2997 });
2918 - };
2919 - if (
2920 - props.workspaceRoot &&
2921 - relativePath &&
2922 - !/[\\/]$/.test(presentation.path) &&
2923 - workspaceLinkLookupQuery(presentation.path, props.workspaceRoot) !== null &&
2924 - readEnvironmentScope(props.environmentId, AuthFilesystemReadScope)
2925 - ) {
2926 - const workspaceRoot = props.workspaceRoot;
2927 - const originalPath = resolveWorkspaceFilePath(props.workspaceRoot, relativePath);
2928 - void executeAtomQuery(
2929 - appAtomRegistry,
2930 - projectEnvironment.searchEntries({
2931 - environmentId: props.environmentId,
2932 - input: {
2933 - cwd: workspaceRoot,
2934 - query: workspaceLinkLookupQuery(relativePath, workspaceRoot) ?? relativePath,
2935 - kind: "file",
2936 - limit: WORKSPACE_BASENAME_LOOKUP_LIMIT,
2937 - },
2938 - }),
2939 - { signal: request.signal, reportFailure: false, reportDefect: false },
2940 - ).then(
2941 - (result) => {
2942 - const match =
2943 - result._tag === "Success"
2944 - ? pickWorkspaceLinkMatch(relativePath, workspaceRoot, result.value.entries)
2945 - : null;
2946 - if (!match || match === relativePath) {
2947 - openAt(relativePath);
2948 - return;
2949 - }
2950 - if (request.signal.aborted) return;
2951 - void executeAtomQuery(
2952 - appAtomRegistry,
2953 - projectEnvironment.fileMetadata({
2954 - environmentId: props.environmentId,
2955 - input: { path: originalPath },
2956 - }),
2957 - { signal: request.signal, reportFailure: false, reportDefect: false },
2958 - ).then(
2959 - (metadata) =>
2960 - openAt(
2961 - metadata._tag === "Success" &&
2962 - (needsWorkspaceBasenameLookup(relativePath)
2963 - ? metadata.value?.kind !== "directory"
2964 - : metadata.value === null)
2965 - ? match
2966 - : relativePath,
2967 - ),
2968 - () => openAt(relativePath),
2969 - );
2970 - },
2971 - () => openAt(relativePath),
2972 - );
2973 - } else openAt(relativePath ?? path);
2998 + });
2999 return;
3000 }
3001
@@ -3052,8 +3077,37 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
3077 switch (actionId) {
3078 case "copy-full-path":
3079 case "copy-relative-path": {
3055 - const clipboardPath = fileChipClipboardPath(target, actionId);
3056 - if (clipboardPath !== undefined) copyTextWithHaptic(clipboardPath);
3080 + const presentation = resolveMarkdownLinkPresentation(href);
3081 + const path =
3082 + presentation.kind === "file"
3083 + ? resolveFileLinkPath(props.workspaceRoot, presentation.path)
3084 + : null;
3085 + if (presentation.kind !== "file" || path === null) return;
3086 + markdownLinkRequest.current?.abort();
3087 + const request = new AbortController();
3088 + markdownLinkRequest.current = request;
3089 + void findFileLinkTarget(presentation.path, path, request.signal).then((found) => {
3090 + if (request.signal.aborted) return;
3091 + const fullPath =
3092 + props.workspaceRoot && !isAbsolutePath(found)
3093 + ? resolveWorkspaceFilePath(props.workspaceRoot, found)
3094 + : found;
3095 + const clipboardPath = fileChipClipboardPath(
3096 + {
3097 + ...target,
3098 + ...(target.fullPath ? { fullPath } : {}),
3099 + ...(target.relativePath
3100 + ? {
3101 + relativePath:
3102 + resolveWorkspaceRelativeFilePath(props.workspaceRoot, fullPath) ??
3103 + target.relativePath,
3104 + }
3105 + : {}),
3106 + },
3107 + actionId,
3108 + );
3109 + if (clipboardPath !== undefined) copyTextWithHaptic(clipboardPath);
3110 + });
3111 return;
3112 }
3113 case "open-file":
apps/web/src/components/ChatMarkdown.tsx
+39 -7
@@ -174,6 +174,7 @@ import { filesystemEnvironment } from "../state/filesystem";
174 import {
175 claimWorkspaceBasenameLookup,
176 needsWorkspaceBasenameLookup,
177 + pickProjectLinkMatch,
178 pickWorkspaceLinkMatch,
179 WORKSPACE_BASENAME_LOOKUP_LIMIT,
180 workspaceLinkLookupQuery,
@@ -792,13 +793,44 @@ function useChatMarkdownState({
793 });
794 if (metadata._tag !== "Success" || metadata.value.entries[0] !== null) return null;
795 }
795 - const result = await searchProjectEntries({
796 - environmentId,
797 - input: { cwd, query, limit: WORKSPACE_BASENAME_LOOKUP_LIMIT, kind: "file" },
798 - });
799 - return result._tag === "Success"
800 - ? pickWorkspaceLinkMatch(workspaceRelativePath, cwd, result.value.entries)
801 - : null;
796 + const searchRoot = async (root: string) => {
797 + const rootQuery = workspaceLinkLookupQuery(workspaceRelativePath, root);
798 + if (rootQuery === null) return { root, entries: [] };
799 + const result = await searchProjectEntries({
800 + environmentId,
801 + input: {
802 + cwd: root,
803 + query: rootQuery,
804 + limit: WORKSPACE_BASENAME_LOOKUP_LIMIT,
805 + kind: "file",
806 + },
807 + });
808 + return { root, entries: result._tag === "Success" ? result.value.entries : [] };
809 + };
810 + const own = await searchRoot(cwd);
811 + const ownMatch = pickWorkspaceLinkMatch(workspaceRelativePath, cwd, own.entries);
812 + if (ownMatch !== null) return ownMatch;
813 + const threadProjectId = threadRef ? readThreadShell(threadRef)?.projectId : undefined;
814 + const otherRoots = projects.flatMap((project) =>
815 + project.environmentId === environmentId &&
816 + project.id !== threadProjectId &&
817 + project.workspaceRoot !== cwd
818 + ? [project.workspaceRoot]
819 + : [],
820 + );
821 + if (otherRoots.length === 0) return null;
822 + // Another project's file never replaces a thread-local file the search missed.
823 + if (needsWorkspaceBasenameLookup(workspaceRelativePath)) {
824 + const metadata = await getFilesystemMetadata({
825 + environmentId,
826 + input: { paths: [resolveWorkspaceFilePath(workspaceRelativePath, cwd)] },
827 + });
828 + if (metadata._tag !== "Success" || metadata.value.entries[0] !== null) return null;
829 + }
830 + return pickProjectLinkMatch(
831 + workspaceRelativePath,
832 + await Promise.all(otherRoots.map(searchRoot)),
833 + );
834 };
835 const openFileInPanel = (fileLinkMeta: MarkdownFileLinkMeta, panelPath: string) => {
836 if (!threadRef) return;
apps/web/src/workspaceBasenameLookup.test.ts
+24
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
2
3 import {
4 claimWorkspaceBasenameLookup,
5 + pickProjectLinkMatch,
6 pickWorkspaceBasenameMatch,
7 pickWorkspaceLinkMatch,
8 workspaceLinkLookupQuery,
@@ -89,6 +90,29 @@ describe("chat file links an agent shortened", () => {
90 });
91 });
92
93 +describe("chat file links into another project", () => {
94 + const appB = {
95 + root: "/home/user/app-b",
96 + entries: [{ path: "src/api/rules/categories.ts", kind: "file" as const }],
97 + };
98 +
99 + // regression: #10553 a path the agent cited inside another registered project
100 + it("resolves to the absolute path in the one project that has the file", () => {
101 + expect(
102 + pickProjectLinkMatch("rules/categories.ts", [
103 + { root: "/home/user/app-c", entries: [] },
104 + appB,
105 + ]),
106 + ).toBe("/home/user/app-b/src/api/rules/categories.ts");
107 + });
108 +
109 + it("opens nothing when several projects have the file", () => {
110 + expect(
111 + pickProjectLinkMatch("rules/categories.ts", [appB, { ...appB, root: "/home/user/app-c" }]),
112 + ).toBeNull();
113 + });
114 +});
115 +
116 describe("claimWorkspaceBasenameLookup", () => {
117 it("keeps only the newest claim, whatever order the lookups settle in", () => {
118 const first = claimWorkspaceBasenameLookup();
packages/client-runtime/src/workspaceBasenameLookup.ts
+23 -1
@@ -1,4 +1,8 @@
1 -import { isAbsolutePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
1 +import {
2 + isAbsolutePath,
3 + isWindowsAbsolutePath,
4 + resolveWorkspaceFilePath,
5 +} from "@t3tools/shared/path";
6
7 export const WORKSPACE_BASENAME_LOOKUP_LIMIT = 25;
8
@@ -113,3 +117,21 @@ function pickStrippedWorkspaceMatch(
117 const files = entries.filter((entry) => entry.kind === "file");
118 return files.find((entry) => key(entry.path) === key(strippedPath))?.path ?? null;
119 }
120 +
121 +/**
122 + * The absolute path of the one file a chat link names across other project roots, or null when
123 + * no root or more than one has it. Agents cite paths relative to whichever repo they worked in.
124 + */
125 +export function pickProjectLinkMatch(
126 + relativePath: string,
127 + searches: ReadonlyArray<{
128 + readonly root: string;
129 + readonly entries: ReadonlyArray<WorkspaceEntryCandidate>;
130 + }>,
131 +): string | null {
132 + const matches = searches.flatMap(({ root, entries }) => {
133 + const match = pickWorkspaceLinkMatch(relativePath, root, entries);
134 + return match === null ? [] : [resolveWorkspaceFilePath(match, root)];
135 + });
136 + return matches.length === 1 ? (matches[0] ?? null) : null;
137 +}