Skip to content

Commit d477ece

Browse files
committed
fix(web): resolve slashed file-chip paths through the workspace index
1 parent b0a0281 commit d477ece

3 files changed

Lines changed: 50 additions & 24 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ import { projectEnvironment } from "../state/projects";
115115
import { threadEnvironment } from "../state/threads";
116116
import {
117117
claimWorkspaceBasenameLookup,
118-
needsWorkspaceBasenameLookup,
119118
pickWorkspaceBasenameMatch,
120119
WORKSPACE_BASENAME_LOOKUP_LIMIT,
121120
} from "../workspaceBasenameLookup";
@@ -1836,7 +1835,7 @@ function ChatMarkdown({
18361835
);
18371836
const findWorkspaceBasenameMatch = useCallback(
18381837
async (workspaceRelativePath: string) => {
1839-
if (!cwd || environmentId === null || !needsWorkspaceBasenameLookup(workspaceRelativePath)) {
1838+
if (!cwd || environmentId === null) {
18401839
return null;
18411840
}
18421841
const result = await searchProjectEntries({
@@ -1854,8 +1853,9 @@ function ChatMarkdown({
18541853
},
18551854
[cwd, environmentId, searchProjectEntries],
18561855
);
1857-
// A bare filename resolves to the workspace root, which is rarely where the
1858-
// file is, so ask the index before opening.
1856+
// The message's path is relative to wherever the agent was working, which is
1857+
// not always the workspace root the panel reads against, so every open goes
1858+
// through the index first; a miss falls back to the literal path.
18591859
const openFileInPanel = useCallback(
18601860
(workspaceRelativePath: string, line: number | undefined) => {
18611861
if (!threadRef) return;
@@ -1864,7 +1864,7 @@ function ChatMarkdown({
18641864
const isLatestLookup = claimWorkspaceBasenameLookup();
18651865
const openAt = (path: string) =>
18661866
useRightPanelStore.getState().openFile(threadRef, path, line);
1867-
if (!cwd || !needsWorkspaceBasenameLookup(workspaceRelativePath)) {
1867+
if (!cwd || environmentId === null) {
18681868
openAt(workspaceRelativePath);
18691869
return;
18701870
}
@@ -1874,7 +1874,7 @@ function ChatMarkdown({
18741874
openAt(match ?? workspaceRelativePath);
18751875
})();
18761876
},
1877-
[cwd, findWorkspaceBasenameMatch, threadRef],
1877+
[cwd, environmentId, findWorkspaceBasenameMatch, threadRef],
18781878
);
18791879
const revealMarkdownFileInFileManager = useCallback(
18801880
async (fileLinkMeta: MarkdownFileLinkMeta) => {

‎apps/web/src/workspaceBasenameLookup.test.ts‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,9 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import {
44
claimWorkspaceBasenameLookup,
5-
needsWorkspaceBasenameLookup,
65
pickWorkspaceBasenameMatch,
76
} from "./workspaceBasenameLookup";
87

9-
describe("needsWorkspaceBasenameLookup", () => {
10-
it("flags bare filenames", () => {
11-
expect(needsWorkspaceBasenameLookup("ChatView.tsx")).toBe(true);
12-
expect(needsWorkspaceBasenameLookup("Makefile")).toBe(true);
13-
});
14-
15-
it("leaves anything with a directory alone", () => {
16-
expect(needsWorkspaceBasenameLookup("apps/web/src/components/ChatView.tsx")).toBe(false);
17-
expect(needsWorkspaceBasenameLookup("apps\\web\\ChatView.tsx")).toBe(false);
18-
expect(needsWorkspaceBasenameLookup(" ")).toBe(false);
19-
});
20-
});
21-
228
describe("pickWorkspaceBasenameMatch", () => {
239
const entries = [
2410
{ path: "apps/web/src/components/ChatView.test.tsx", kind: "file" as const },
@@ -31,6 +17,39 @@ describe("pickWorkspaceBasenameMatch", () => {
3117
);
3218
});
3319

20+
it("takes an exact workspace-relative path", () => {
21+
expect(pickWorkspaceBasenameMatch("apps/web/src/components/ChatView.tsx", entries)).toBe(
22+
"apps/web/src/components/ChatView.tsx",
23+
);
24+
});
25+
26+
it("matches a slashed path as a suffix wherever it actually lives", () => {
27+
expect(
28+
pickWorkspaceBasenameMatch("docs/GROUPS_PLAN.md", [
29+
{ path: "BudgetLens/docs/GROUPS_PLAN.md", kind: "file" },
30+
{ path: "docs/other.md", kind: "file" },
31+
]),
32+
).toBe("BudgetLens/docs/GROUPS_PLAN.md");
33+
});
34+
35+
it("does not let a suffix match straddle a segment boundary", () => {
36+
expect(
37+
pickWorkspaceBasenameMatch("docs/plan.md", [{ path: "src/mydocs/plan.md", kind: "file" }]),
38+
).toBeNull();
39+
expect(
40+
pickWorkspaceBasenameMatch("plan.md", [{ path: "src/plan.md.bak", kind: "file" }]),
41+
).toBeNull();
42+
});
43+
44+
it("prefers the exact suffix over a basename-only twin", () => {
45+
expect(
46+
pickWorkspaceBasenameMatch("docs/plan.md", [
47+
{ path: "other/plan.md", kind: "file" },
48+
{ path: "BudgetLens/docs/plan.md", kind: "file" },
49+
]),
50+
).toBe("BudgetLens/docs/plan.md");
51+
});
52+
3453
it("ignores directories", () => {
3554
expect(
3655
pickWorkspaceBasenameMatch("components", [

‎apps/web/src/workspaceBasenameLookup.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,25 @@ function basenameOfPath(path: string): string {
2323
return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
2424
}
2525

26-
export function needsWorkspaceBasenameLookup(relativePath: string): boolean {
27-
const trimmed = relativePath.trim();
28-
return trimmed.length > 0 && !trimmed.includes("/") && !trimmed.includes("\\");
26+
/** `a/b/c` matches `c` and `b/c` at a segment boundary, but not `abc`. */
27+
function hasSegmentSuffix(path: string, suffix: string): boolean {
28+
return path === suffix || path.endsWith(`/${suffix}`);
2929
}
3030

3131
export function pickWorkspaceBasenameMatch(
3232
basename: string,
3333
entries: ReadonlyArray<WorkspaceEntryCandidate>,
3434
): string | null {
35-
const target = basename.trim();
35+
const target = basename.trim().replaceAll("\\", "/");
3636
if (!target) return null;
3737
const files = entries.filter((entry) => entry.kind === "file");
38+
const exactPath = files.find((entry) => entry.path === target);
39+
if (exactPath) return exactPath.path;
40+
// Agents reference files relative to their own cwd, which is not always the
41+
// workspace root, so a `docs/plan.md` chip has to match wherever that suffix
42+
// actually lives. Index order (frecency) breaks ties.
43+
const suffixMatches = files.filter((entry) => hasSegmentSuffix(entry.path, target));
44+
if (suffixMatches.length > 0) return suffixMatches[0]?.path ?? null;
3845
const exact = files.find((entry) => basenameOfPath(entry.path) === target);
3946
if (exact) return exact.path;
4047
// Folded matching covers casing that drifted from disk, but `FOO.ts` against

0 commit comments

Comments
 (0)