Skip to content

Commit 2ab5a2a

Browse files
committed
fix(web): keep existing literal chip paths ahead of divergent index hits
1 parent 03e7f34 commit 2ab5a2a

3 files changed

Lines changed: 72 additions & 4 deletions

File tree

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ import { useAtomQueryRunner } from "../state/use-atom-query-runner";
163163
import { projectEnvironment } from "../state/projects";
164164
import {
165165
claimWorkspaceBasenameLookup,
166+
needsLiteralWorkspaceFileCheck,
166167
normalizeWorkspaceLookupPath,
167168
pickWorkspaceBasenameMatch,
168169
WORKSPACE_BASENAME_LOOKUP_LIMIT,
@@ -2209,6 +2210,9 @@ function useChatMarkdownState({
22092210
const searchProjectEntries = useAtomQueryRunner(projectEnvironment.searchEntries, {
22102211
reportFailure: false,
22112212
});
2213+
const readWorkspaceFile = useAtomQueryRunner(projectEnvironment.readFile, {
2214+
reportFailure: false,
2215+
});
22122216
const openPreview = useAtomCommand(previewEnvironment.open, {
22132217
reportFailure: false,
22142218
});
@@ -2437,6 +2441,20 @@ function useChatMarkdownState({
24372441
},
24382442
[createAssetUrl, cwd, openPreview, preparedConnection, threadRef],
24392443
);
2444+
const literalWorkspaceFileExists = useCallback(
2445+
async (workspaceRelativePath: string) => {
2446+
if (!cwd || environmentId === null) return false;
2447+
const result = await readWorkspaceFile({
2448+
environmentId,
2449+
input: { cwd, relativePath: workspaceRelativePath },
2450+
});
2451+
// readFile reads from disk rather than the search index, so gitignored
2452+
// files still report present. Binary files fail the read and count as
2453+
// missing here; a stat RPC would close that gap.
2454+
return result._tag === "Success";
2455+
},
2456+
[cwd, environmentId, readWorkspaceFile],
2457+
);
24402458
const findWorkspaceBasenameMatch = useCallback(
24412459
async (workspaceRelativePath: string) => {
24422460
const lookupPath = normalizeWorkspaceLookupPath(workspaceRelativePath);
@@ -2452,11 +2470,19 @@ function useChatMarkdownState({
24522470
kind: "file",
24532471
},
24542472
});
2455-
return result._tag === "Success"
2456-
? pickWorkspaceBasenameMatch(lookupPath, result.value.entries)
2457-
: null;
2473+
if (result._tag !== "Success") return null;
2474+
const match = pickWorkspaceBasenameMatch(lookupPath, result.value.entries);
2475+
if (
2476+
needsLiteralWorkspaceFileCheck(lookupPath, match) &&
2477+
(await literalWorkspaceFileExists(lookupPath))
2478+
) {
2479+
// The literal chip path names a real file, so keep it instead of the
2480+
// indexed twin. Callers fall back to the literal path on null.
2481+
return null;
2482+
}
2483+
return match;
24582484
},
2459-
[cwd, environmentId, searchProjectEntries],
2485+
[cwd, environmentId, literalWorkspaceFileExists, searchProjectEntries],
24602486
);
24612487
// Chip paths are relative to the agent's cwd, so every workspace open goes
24622488
// through the index first. Absolute host paths open as-is.

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

Lines changed: 28 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+
needsLiteralWorkspaceFileCheck,
56
pickWorkspaceBasenameMatch,
67
} from "./workspaceBasenameLookup";
78

@@ -90,6 +91,33 @@ describe("pickWorkspaceBasenameMatch", () => {
9091
});
9192
});
9293

94+
describe("needsLiteralWorkspaceFileCheck", () => {
95+
it("checks the disk when a slashed hit names a different file than the chip", () => {
96+
expect(
97+
needsLiteralWorkspaceFileCheck("docs/GROUPS_PLAN.md", "BudgetLens/docs/GROUPS_PLAN.md"),
98+
).toBe(true);
99+
});
100+
101+
it("skips the disk when the hit is the literal path", () => {
102+
expect(
103+
needsLiteralWorkspaceFileCheck(
104+
"apps/web/src/components/ChatView.tsx",
105+
"apps/web/src/components/ChatView.tsx",
106+
),
107+
).toBe(false);
108+
});
109+
110+
it("skips the disk when there is no hit", () => {
111+
expect(needsLiteralWorkspaceFileCheck("docs/plan.md", null)).toBe(false);
112+
});
113+
114+
it("skips the disk for bare names, which keep the ranked-first hit", () => {
115+
expect(needsLiteralWorkspaceFileCheck("ChatView.tsx", "apps/web/src/ChatView.tsx")).toBe(
116+
false,
117+
);
118+
});
119+
});
120+
93121
describe("claimWorkspaceBasenameLookup", () => {
94122
it("keeps only the newest claim, whatever order the lookups settle in", () => {
95123
const first = claimWorkspaceBasenameLookup("thread-a");

‎apps/web/src/workspaceBasenameLookup.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ function hasSegmentSuffix(path: string, suffix: string): boolean {
3434
return posix === suffix || posix.endsWith(`/${suffix}`);
3535
}
3636

37+
/**
38+
* Whether a divergent index hit may override the literal chip path only after
39+
* a disk check. The search index skips gitignored files, so its absence there
40+
* is not proof a file does not exist: for slashed paths an index hit that
41+
* names a different file must not hijack a real file at the literal path.
42+
* Bare names keep the ranked-first hit with no disk check.
43+
*/
44+
export function needsLiteralWorkspaceFileCheck(
45+
lookupPath: string,
46+
match: string | null,
47+
): boolean {
48+
return match !== null && match !== lookupPath && lookupPath.includes("/");
49+
}
50+
3751
export function pickWorkspaceBasenameMatch(
3852
basename: string,
3953
entries: ReadonlyArray<WorkspaceEntryCandidate>,

0 commit comments

Comments
 (0)