Skip to content

Commit dc272e6

Browse files
CallumAlderclaude
andcommitted
fix(web): open chat file links written relative to a subfolder
A link like `builders/raw.ts`, written relative to the package or submodule the agent was working in, resolved against the workspace root and opened "The file could not be accessed or read". The lookup fallback only ran for bare filenames and repeated-workspace-prefix paths. When such a sub-path is missing at the root, search the workspace and open the file whose path ends with it at a segment boundary, only when exactly one search result matches. Sub-paths that exist as written still open directly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 47f2c8d commit dc272e6

3 files changed

Lines changed: 87 additions & 13 deletions

File tree

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ import {
170170
needsWorkspaceBasenameLookup,
171171
pickStrippedWorkspaceMatch,
172172
pickWorkspaceBasenameMatch,
173+
pickWorkspaceSuffixMatch,
173174
stripRepeatedWorkspacePrefix,
174175
WORKSPACE_BASENAME_LOOKUP_LIMIT,
175176
} from "@t3tools/client-runtime/workspace-basename-lookup";
@@ -771,7 +772,16 @@ function useChatMarkdownState({
771772
const strippedPath = isBasename
772773
? null
773774
: stripRepeatedWorkspacePrefix(workspaceRelativePath, cwd);
774-
if (!isBasename && strippedPath === null) return null;
775+
const literalPathIsMissing = async () => {
776+
const metadata = await getFilesystemMetadata({
777+
environmentId,
778+
input: { paths: [resolveWorkspaceFilePath(workspaceRelativePath, cwd)] },
779+
});
780+
return metadata._tag === "Success" && metadata.value.entries[0] === null;
781+
};
782+
const isSubPath = !isBasename && strippedPath === null;
783+
// A sub-path that resolves as written needs no search.
784+
if (isSubPath && !(await literalPathIsMissing())) return null;
775785
const result = await searchProjectEntries({
776786
environmentId,
777787
input: {
@@ -782,31 +792,25 @@ function useChatMarkdownState({
782792
},
783793
});
784794
if (result._tag !== "Success") return null;
785-
if (strippedPath === null) {
795+
if (isBasename) {
786796
return pickWorkspaceBasenameMatch(workspaceRelativePath, result.value.entries);
787797
}
798+
if (strippedPath === null) {
799+
return pickWorkspaceSuffixMatch(workspaceRelativePath, cwd, result.value.entries);
800+
}
788801
const match = pickStrippedWorkspaceMatch(strippedPath, cwd, result.value.entries);
789802
if (match === null) return null;
790803
// Search can be truncated or still scanning, so it cannot rule out the
791804
// literal path. Check the host before redirecting to the stripped match.
792-
const metadata = await getFilesystemMetadata({
793-
environmentId,
794-
input: { paths: [resolveWorkspaceFilePath(workspaceRelativePath, cwd)] },
795-
});
796-
return metadata._tag === "Success" && metadata.value.entries[0] === null ? match : null;
805+
return (await literalPathIsMissing()) ? match : null;
797806
};
798807
const openFileInPanel = (fileLinkMeta: MarkdownFileLinkMeta, panelPath: string) => {
799808
if (!threadRef) return;
800809
// Every open supersedes pending lookups, including synchronous opens.
801810
const isLatestLookup = claimWorkspaceBasenameLookup();
802811
const openAt = (path: string) =>
803812
useRightPanelStore.getState().openFile(threadRef, path, fileLinkMeta.line);
804-
if (
805-
fileLinkMeta.workspaceLookupPath === null ||
806-
!cwd ||
807-
(!needsWorkspaceBasenameLookup(panelPath) &&
808-
stripRepeatedWorkspacePrefix(panelPath, cwd) === null)
809-
) {
813+
if (fileLinkMeta.workspaceLookupPath === null || !cwd) {
810814
openAt(panelPath);
811815
return;
812816
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
33
import {
44
claimWorkspaceBasenameLookup,
55
pickWorkspaceBasenameMatch,
6+
pickWorkspaceSuffixMatch,
67
} from "@t3tools/client-runtime/workspace-basename-lookup";
78

89
describe("pickWorkspaceBasenameMatch", () => {
@@ -52,6 +53,51 @@ describe("pickWorkspaceBasenameMatch", () => {
5253
});
5354
});
5455

56+
describe("pickWorkspaceSuffixMatch", () => {
57+
const entries = [
58+
{ path: "packages/convex/builders/raw.ts", kind: "file" as const },
59+
{ path: "packages/convex/builders/user.ts", kind: "file" as const },
60+
{ path: "apps/web/lib/utils.ts", kind: "file" as const },
61+
{ path: "packages/convex/lib/utils.ts", kind: "file" as const },
62+
];
63+
64+
it("resolves a path written relative to a subfolder", () => {
65+
expect(pickWorkspaceSuffixMatch("builders/raw.ts", "/repo", entries)).toBe(
66+
"packages/convex/builders/raw.ts",
67+
);
68+
});
69+
70+
it("matches whole segments only", () => {
71+
expect(pickWorkspaceSuffixMatch("ilders/raw.ts", "/repo", entries)).toBeNull();
72+
});
73+
74+
it("returns null when the suffix is ambiguous", () => {
75+
expect(pickWorkspaceSuffixMatch("lib/utils.ts", "/repo", entries)).toBeNull();
76+
});
77+
78+
it("leaves bare filenames, dot segments and absolute paths to other lookups", () => {
79+
expect(pickWorkspaceSuffixMatch("raw.ts", "/repo", entries)).toBeNull();
80+
expect(pickWorkspaceSuffixMatch("./builders/raw.ts", "/repo", entries)).toBeNull();
81+
expect(pickWorkspaceSuffixMatch("/repo/builders/raw.ts", "/repo", entries)).toBeNull();
82+
});
83+
84+
it("ignores directories", () => {
85+
expect(
86+
pickWorkspaceSuffixMatch("src/components", "/repo", [
87+
{ path: "apps/web/src/components", kind: "directory" },
88+
]),
89+
).toBeNull();
90+
});
91+
92+
it("folds case on Windows workspaces", () => {
93+
expect(
94+
pickWorkspaceSuffixMatch("Builders\\Raw.ts", "C:\\repo", [
95+
{ path: "packages\\convex\\builders\\raw.ts", kind: "file" },
96+
]),
97+
).toBe("packages\\convex\\builders\\raw.ts");
98+
});
99+
});
100+
55101
describe("claimWorkspaceBasenameLookup", () => {
56102
it("keeps only the newest claim, whatever order the lookups settle in", () => {
57103
const first = claimWorkspaceBasenameLookup();

‎packages/client-runtime/src/workspaceBasenameLookup.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ export function stripRepeatedWorkspacePrefix(relativePath: string, cwd: string):
6565
return null;
6666
}
6767

68+
// Agents often write paths relative to the subfolder they work in (a package, a submodule).
69+
// Only a match that is unique across the searched files is safe to open.
70+
export function pickWorkspaceSuffixMatch(
71+
relativePath: string,
72+
cwd: string,
73+
entries: ReadonlyArray<WorkspaceEntryCandidate>,
74+
): string | null {
75+
if (isAbsolutePath(relativePath)) return null;
76+
const segments = relativePath.split(/[\\/]+/);
77+
if (segments.length < 2 || segments.some((segment) => !segment || /^\.{1,2}$/.test(segment))) {
78+
return null;
79+
}
80+
const caseInsensitive = isWindowsAbsolutePath(cwd);
81+
const key = (path: string) => {
82+
const normalized = path.replaceAll("\\", "/");
83+
return caseInsensitive ? normalized.toLowerCase() : normalized;
84+
};
85+
const suffix = `/${key(segments.join("/"))}`;
86+
const matches = entries.filter(
87+
(entry) => entry.kind === "file" && key(entry.path).endsWith(suffix),
88+
);
89+
return matches.length === 1 ? (matches[0]?.path ?? null) : null;
90+
}
91+
6892
export function pickStrippedWorkspaceMatch(
6993
strippedPath: string,
7094
cwd: string,

0 commit comments

Comments
 (0)