Skip to content

Commit 89e1bae

Browse files
mleungdevclaude
andcommitted
fix(web): repository-relative file links open in parent-folder projects
When a project is rooted above a repository (e.g. a workspace folder holding several repos), agents write links relative to the repository they work in, such as docs/STATUS.md for repo/docs/STATUS.md. The chat link fell back to a workspace search only for bare filenames and repeated-prefix paths, so these opened a missing file. Search for them too when the literal path is missing, and take the shallowest file ending in that path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c5332bf commit 89e1bae

3 files changed

Lines changed: 96 additions & 15 deletions

File tree

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ import { filesystemEnvironment } from "../state/filesystem";
165165
import {
166166
claimWorkspaceBasenameLookup,
167167
needsWorkspaceBasenameLookup,
168+
pickNestedWorkspaceMatch,
168169
pickStrippedWorkspaceMatch,
169170
pickWorkspaceBasenameMatch,
170171
stripRepeatedWorkspacePrefix,
@@ -750,7 +751,17 @@ function useChatMarkdownState({
750751
const strippedPath = isBasename
751752
? null
752753
: stripRepeatedWorkspacePrefix(workspaceRelativePath, cwd);
753-
if (!isBasename && strippedPath === null) return null;
754+
// Search can be truncated or still scanning, so it cannot rule out the
755+
// literal path. Check the host before redirecting to another match.
756+
const literalPathMissing = async () => {
757+
const metadata = await getFilesystemMetadata({
758+
environmentId,
759+
input: { paths: [resolveWorkspaceFilePath(workspaceRelativePath, cwd)] },
760+
});
761+
return metadata._tag === "Success" && metadata.value.entries[0] === null;
762+
};
763+
const isNested = !isBasename && strippedPath === null;
764+
if (isNested && !(await literalPathMissing())) return null;
754765
const result = await searchProjectEntries({
755766
environmentId,
756767
input: {
@@ -761,31 +772,23 @@ function useChatMarkdownState({
761772
},
762773
});
763774
if (result._tag !== "Success") return null;
764-
if (strippedPath === null) {
775+
if (isBasename) {
765776
return pickWorkspaceBasenameMatch(workspaceRelativePath, result.value.entries);
766777
}
778+
if (strippedPath === null) {
779+
return pickNestedWorkspaceMatch(workspaceRelativePath, cwd, result.value.entries);
780+
}
767781
const match = pickStrippedWorkspaceMatch(strippedPath, cwd, result.value.entries);
768782
if (match === null) return null;
769-
// Search can be truncated or still scanning, so it cannot rule out the
770-
// literal path. Check the host before redirecting to the stripped match.
771-
const metadata = await getFilesystemMetadata({
772-
environmentId,
773-
input: { paths: [resolveWorkspaceFilePath(workspaceRelativePath, cwd)] },
774-
});
775-
return metadata._tag === "Success" && metadata.value.entries[0] === null ? match : null;
783+
return (await literalPathMissing()) ? match : null;
776784
};
777785
const openFileInPanel = (fileLinkMeta: MarkdownFileLinkMeta, panelPath: string) => {
778786
if (!threadRef) return;
779787
// Every open supersedes pending lookups, including synchronous opens.
780788
const isLatestLookup = claimWorkspaceBasenameLookup();
781789
const openAt = (path: string) =>
782790
useRightPanelStore.getState().openFile(threadRef, path, fileLinkMeta.line);
783-
if (
784-
fileLinkMeta.workspaceLookupPath === null ||
785-
!cwd ||
786-
(!needsWorkspaceBasenameLookup(panelPath) &&
787-
stripRepeatedWorkspacePrefix(panelPath, cwd) === null)
788-
) {
791+
if (fileLinkMeta.workspaceLookupPath === null || !cwd) {
789792
openAt(panelPath);
790793
return;
791794
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import {
44
claimWorkspaceBasenameLookup,
5+
pickNestedWorkspaceMatch,
56
pickWorkspaceBasenameMatch,
67
} from "@t3tools/client-runtime/workspace-basename-lookup";
78

@@ -61,3 +62,57 @@ describe("claimWorkspaceBasenameLookup", () => {
6162
expect(first()).toBe(false);
6263
});
6364
});
65+
66+
describe("pickNestedWorkspaceMatch", () => {
67+
const cwd = "/Users/dev/workspace";
68+
69+
it("finds a repository-relative path inside a nested repository", () => {
70+
expect(
71+
pickNestedWorkspaceMatch("docs/STATUS.md", cwd, [
72+
{ path: "repo/docs/STATUS-old.md", kind: "file" },
73+
{ path: "repo/docs/STATUS.md", kind: "file" },
74+
]),
75+
).toBe("repo/docs/STATUS.md");
76+
});
77+
78+
it("prefers the shallowest copy over worktree copies deeper down", () => {
79+
expect(
80+
pickNestedWorkspaceMatch("docs/STATUS.md", cwd, [
81+
{ path: "worktrees/lane-a/docs/STATUS.md", kind: "file" },
82+
{ path: "repo/docs/STATUS.md", kind: "file" },
83+
{ path: "worktrees/lane-b/docs/STATUS.md", kind: "file" },
84+
]),
85+
).toBe("repo/docs/STATUS.md");
86+
});
87+
88+
it("keeps search rank between equally shallow copies", () => {
89+
expect(
90+
pickNestedWorkspaceMatch("docs/STATUS.md", cwd, [
91+
{ path: "worktrees/lane-a/docs/STATUS.md", kind: "file" },
92+
{ path: "repo-a/docs/STATUS.md", kind: "file" },
93+
{ path: "repo-b/docs/STATUS.md", kind: "file" },
94+
]),
95+
).toBe("repo-a/docs/STATUS.md");
96+
});
97+
98+
it("matches whole path segments only, and never directories", () => {
99+
expect(
100+
pickNestedWorkspaceMatch("docs/STATUS.md", cwd, [
101+
{ path: "repo/mydocs/STATUS.md", kind: "file" },
102+
{ path: "repo/docs/STATUS.md", kind: "directory" },
103+
]),
104+
).toBeNull();
105+
});
106+
107+
it("folds case only for Windows workspaces", () => {
108+
const entries = [{ path: "repo\\Docs\\Status.md", kind: "file" as const }];
109+
expect(pickNestedWorkspaceMatch("docs/status.md", "C:\\work", entries)).toBe(
110+
"repo\\Docs\\Status.md",
111+
);
112+
expect(pickNestedWorkspaceMatch("docs/status.md", cwd, entries)).toBeNull();
113+
});
114+
115+
it("returns null when nothing matches", () => {
116+
expect(pickNestedWorkspaceMatch("docs/STATUS.md", cwd, [])).toBeNull();
117+
});
118+
});

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,26 @@ export function pickStrippedWorkspaceMatch(
7878
const files = entries.filter((entry) => entry.kind === "file");
7979
return files.find((entry) => key(entry.path) === key(strippedPath))?.path ?? null;
8080
}
81+
82+
// A project rooted above a repository gets repository-relative links ("docs/x.md"
83+
// for "repo/docs/x.md"). Take the shallowest file ending in that path; ties keep
84+
// search rank, like the basename lookup.
85+
export function pickNestedWorkspaceMatch(
86+
relativePath: string,
87+
cwd: string,
88+
entries: ReadonlyArray<WorkspaceEntryCandidate>,
89+
): string | null {
90+
if (isAbsolutePath(relativePath)) return null;
91+
const caseInsensitive = isWindowsAbsolutePath(cwd);
92+
const key = (path: string) => {
93+
const normalized = path.replaceAll("\\", "/");
94+
return caseInsensitive ? normalized.toLowerCase() : normalized;
95+
};
96+
const suffix = `/${key(relativePath).replace(/^\.\//, "")}`;
97+
const depth = (path: string) => path.split(/[\\/]+/).length;
98+
const matches = entries.filter(
99+
(entry) => entry.kind === "file" && key(entry.path).endsWith(suffix),
100+
);
101+
const shallowest = Math.min(...matches.map((entry) => depth(entry.path)));
102+
return matches.find((entry) => depth(entry.path) === shallowest)?.path ?? null;
103+
}

0 commit comments

Comments
 (0)