The best way to control Claude Code, Codex and more from any computer or phone
fix(client-runtime): a chat file link opens a file that overlapping projects both list (#39142)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 19, 2026 at 15:52 UTC
7e004b80e2fea6a0a59e18cbb2cb21346a2bdd1d
3 files changed
+161
-18
apps/web/src/workspaceBasenameLookup.test.ts
+57
@@ -107,11 +107,68 @@ describe("chat file links into another project", () => {
107
).toBe("/home/user/app-b/src/api/rules/categories.ts");
108
});
109
110
+ it.each([
111
+ {
112
+ outer: "/home/user/reports",
113
+ inner: "/home/user/reports/docs",
114
+ found: "/home/user/reports/docs/handoff.txt",
115
+ },
116
+ { outer: "C:\\Reports", inner: "C:/Reports/docs/", found: "C:\\Reports\\docs\\handoff.txt" },
117
+ ])("resolves one file that a project nested in $outer also lists", ({ outer, inner, found }) => {
118
+ expect(
119
+ pickProjectLinkMatch("handoff.txt", [
120
+ { root: outer, entries: [{ path: "docs/handoff.txt", kind: "file" }] },
121
+ { root: inner, entries: [{ path: "handoff.txt", kind: "file" }] },
122
+ ]),
123
+ ).toBe(found);
124
+ });
125
+
126
it("opens nothing when several projects have the file", () => {
127
expect(
128
pickProjectLinkMatch("rules/categories.ts", [appB, { ...appB, root: "/home/user/app-c" }]),
129
).toBeNull();
130
});
131
+
132
+ it("opens nothing when overlapping projects also list a second suffix match", () => {
133
+ expect(
134
+ pickProjectLinkMatch("docs/handoff.txt", [
135
+ {
136
+ root: "/home/user/reports",
137
+ entries: [
138
+ { path: "quarter/docs/handoff.txt", kind: "file" },
139
+ { path: "archive/docs/handoff.txt", kind: "file" },
140
+ ],
141
+ },
142
+ {
143
+ root: "/home/user/reports/quarter",
144
+ entries: [{ path: "docs/handoff.txt", kind: "file" }],
145
+ },
146
+ {
147
+ root: "/home/user/reports/quarter/docs",
148
+ entries: [{ path: "handoff.txt", kind: "file" }],
149
+ },
150
+ ]),
151
+ ).toBeNull();
152
+ });
153
+
154
+ it.each([
155
+ { cited: "handoff.txt", other: "archive/handoff.txt" },
156
+ { cited: "HANDOFF.txt", other: "archive/Handoff.txt" },
157
+ ])("opens nothing when overlapping projects also list $other", ({ cited, other }) => {
158
+ expect(
159
+ pickProjectLinkMatch("handoff.txt", [
160
+ {
161
+ root: "/home/user/reports",
162
+ entries: [
163
+ { path: `team/docs/${cited}`, kind: "file" },
164
+ { path: other, kind: "file" },
165
+ ],
166
+ },
167
+ { root: "/home/user/reports/team", entries: [{ path: `docs/${cited}`, kind: "file" }] },
168
+ { root: "/home/user/reports/team/docs", entries: [{ path: cited, kind: "file" }] },
169
+ ]),
170
+ ).toBeNull();
171
+ });
172
});
173
174
// regression: #35337 a path the agent cited inside a checkout no project is rooted at
packages/client-runtime/src/workspaceBasenameLookup.ts
+55
-18
@@ -82,18 +82,25 @@ export function pickWorkspaceLinkMatch(
82
): string | null {
83
const trimmed = relativePath.trim();
84
if (needsWorkspaceBasenameLookup(trimmed)) return pickWorkspaceBasenameMatch(trimmed, entries);
85
- const stripped = stripRepeatedWorkspacePrefix(trimmed, cwd);
85
+ const matches = workspaceQualifiedPathMatches(trimmed, cwd, entries);
86
+ return matches.length === 1 ? (matches[0] ?? null) : null;
87
+}
88
+
89
+function workspaceQualifiedPathMatches(
90
+ relativePath: string,
91
+ cwd: string,
92
+ entries: ReadonlyArray<WorkspaceEntryCandidate>,
93
+): ReadonlyArray<string> {
94
+ const stripped = stripRepeatedWorkspacePrefix(relativePath, cwd);
95
const strippedMatch =
96
stripped === null ? null : pickStrippedWorkspaceMatch(stripped, cwd, entries);
88
- if (strippedMatch !== null) return strippedMatch;
89
- const suffix = trimmed.replaceAll("\\", "/");
90
- const matches = entries.filter(
91
- (entry) =>
92
- entry.kind === "file" &&
93
- (entry.path.replaceAll("\\", "/") === suffix ||
94
- entry.path.replaceAll("\\", "/").endsWith(`/${suffix}`)),
95
- );
96
- return matches.length === 1 ? (matches[0]?.path ?? null) : null;
97
+ if (strippedMatch !== null) return [strippedMatch];
98
+ const suffix = relativePath.replaceAll("\\", "/");
99
+ return entries.flatMap((entry) => {
100
+ if (entry.kind !== "file") return [];
101
+ const path = entry.path.replaceAll("\\", "/");
102
+ return path === suffix || path.endsWith(`/${suffix}`) ? [entry.path] : [];
103
+ });
104
}
105
106
function pickStrippedWorkspaceMatch(
@@ -112,7 +119,8 @@ function pickStrippedWorkspaceMatch(
119
120
/**
121
* The absolute path of the one file a chat link names across other project roots, or null when
115
- * no root or more than one has it. Agents cite paths relative to whichever repo they worked in.
122
+ * no root has it or roots name different files. Agents cite paths relative to their repo.
123
+ * Nested project roots list the same file more than once, which still names one file.
124
*/
125
export function pickProjectLinkMatch(
126
relativePath: string,
@@ -121,11 +129,39 @@ export function pickProjectLinkMatch(
129
readonly entries: ReadonlyArray<WorkspaceEntryCandidate>;
130
}>,
131
): string | null {
124
- const matches = searches.flatMap(({ root, entries }) => {
125
- const match = pickWorkspaceLinkMatch(relativePath, root, entries);
126
- return match === null ? [] : [resolveWorkspaceFilePath(match, root)];
127
- });
128
- return matches.length === 1 ? (matches[0] ?? null) : null;
132
+ const target = relativePath.trim();
133
+ const isBasename = needsWorkspaceBasenameLookup(target);
134
+ const matches = new Map<string, string>();
135
+ let rootHits = 0;
136
+ const addMatch = (path: string) => {
137
+ // Windows paths that differ only by case stay distinct: a directory can be case-sensitive.
138
+ const key = isWindowsAbsolutePath(path) ? path.replaceAll("/", "\\") : path;
139
+ if (!matches.has(key)) matches.set(key, path);
140
+ };
141
+ for (const { root, entries } of searches) {
142
+ if (isBasename) {
143
+ const match = pickWorkspaceBasenameMatch(target, entries);
144
+ if (match === null) continue;
145
+ rootHits += 1;
146
+ addMatch(resolveWorkspaceFilePath(match, root));
147
+ } else {
148
+ for (const match of workspaceQualifiedPathMatches(target, root, entries)) {
149
+ addMatch(resolveWorkspaceFilePath(match, root));
150
+ }
151
+ }
152
+ }
153
+ if (isBasename && rootHits > 1) {
154
+ // A root's pick, or a root left ambiguous by case, can hide another file elsewhere.
155
+ const folded = target.toLowerCase();
156
+ for (const { root, entries } of searches) {
157
+ for (const entry of entries) {
158
+ if (entry.kind === "file" && basenameOfPath(entry.path).toLowerCase() === folded) {
159
+ addMatch(resolveWorkspaceFilePath(entry.path, root));
160
+ }
161
+ }
162
+ }
163
+ }
164
+ return matches.size === 1 ? ([...matches.values()][0] ?? null) : null;
165
}
166
167
const AGENT_DIRECTORY_SEARCH_LIMIT = 4;
@@ -143,8 +179,9 @@ export async function findAgentDirectoryMatch(
179
}>,
180
): Promise<string | null> {
181
for (const directory of directories.slice(0, AGENT_DIRECTORY_SEARCH_LIMIT)) {
146
- const match = pickProjectLinkMatch(relativePath, [await searchRoot(directory)]);
147
- if (match !== null) return match;
182
+ const { root, entries } = await searchRoot(directory);
183
+ const match = pickWorkspaceLinkMatch(relativePath, root, entries);
184
+ if (match !== null) return resolveWorkspaceFilePath(match, root);
185
}
186
return null;
187
}
scripts/scenarios/citation-finds-file-in-overlapping-projects.e2e.ts
new
+49
@@ -0,0 +1,49 @@
1
+/// <reference lib="dom" />
2
+import { expect } from "e2e";
3
+
4
+import { T3_HOME } from "../e2e.config.ts";
5
+import { makeState, runPromise, test, WEB } from "./t3.ts";
6
+
7
+const FILE = "quarterly-handoff.txt";
8
+const CONTENT = "Handoff notes from the shared file";
9
+
10
+test(
11
+ "A citation finds one file listed by two overlapping projects",
12
+ WEB,
13
+ async ({ t3, app, browser, screen }) => {
14
+ const repository = await t3.project("reports", { [`docs/${FILE}`]: CONTENT });
15
+ const nested = `${repository}/docs`;
16
+ const project = await t3.project("planning");
17
+ const thread = await t3.thread({
18
+ project,
19
+ title: "Find the report",
20
+ messages: [`See :codex-file-citation{path="${FILE}" line_range_start="1"}`],
21
+ });
22
+ const { cli } = makeState(T3_HOME);
23
+ await runPromise(cli(["project", "add", nested, "--title", "Report docs"]));
24
+ try {
25
+ await t3.open(thread);
26
+ await screen.getByRole("button", /^Agent See/).tap();
27
+ await screen.getByRole("link", /^quarterly-handoff\.txt/).tap();
28
+
29
+ try {
30
+ await expect
31
+ .poll(() =>
32
+ browser.evaluate(() => {
33
+ const panel = document.querySelector("[data-right-panel-surface-content]");
34
+ for (const host of panel?.querySelectorAll("*") ?? []) {
35
+ const content = host.shadowRoot?.querySelector("[data-content]");
36
+ if (content) return content.textContent ?? "";
37
+ }
38
+ return panel?.textContent ?? "";
39
+ }),
40
+ )
41
+ .toContain(CONTENT);
42
+ } finally {
43
+ await app.screenshot("citation-in-overlapping-projects");
44
+ }
45
+ } finally {
46
+ await runPromise(cli(["project", "remove", nested]));
47
+ }
48
+ },
49
+);