Skip to content

Commit 0c43d3e

Browse files
fix(web): a bare file name the agent cited from a checkout it cd'd into opens that file
The fallback to the folders the agent worked in only joined the cited text onto each folder, so a bare name such as config.test.yaml whose file sits in a subfolder still opened nothing. When no folder has the name directly, web and mobile now search the latest few folders by name, one at a time, and open the first match. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 779c28d commit 0c43d3e

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ import { readEnvironmentScope, usePreparedConnection } from "../../state/session
236236
import { environmentProjects, projectEnvironment } from "../../state/projects";
237237
import {
238238
agentWorkingDirectories,
239+
findAgentDirectoryMatch,
239240
needsWorkspaceBasenameLookup,
240241
pickProjectLinkMatch,
241242
pickWorkspaceLinkMatch,
@@ -2934,16 +2935,15 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
29342935
}
29352936
if (match === null && reply !== undefined && !signal.aborted) {
29362937
// No project has the file, so it opens where the agent last worked, as written.
2937-
const agentPaths = agentWorkingDirectories(
2938+
const agentRoots = agentWorkingDirectories(
29382939
appAtomRegistry.get(
29392940
environmentThreadDetails.quietThreadAtom(
29402941
scopeThreadRef(props.environmentId, props.threadId),
29412942
),
29422943
)?.projection.visibleTurnItems ?? [],
29432944
reply,
2944-
).flatMap((root) =>
2945-
root === workspaceRoot ? [] : [resolveWorkspaceFilePath(root, relativePath)],
2946-
);
2945+
).filter((root) => root !== workspaceRoot);
2946+
const agentPaths = agentRoots.map((root) => resolveWorkspaceFilePath(root, relativePath));
29472947
if (agentPaths.length === 0) return relativePath;
29482948
const [original, ...found] = await Promise.all(
29492949
[originalPath, ...agentPaths].map(metadataOf),
@@ -2952,7 +2952,13 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
29522952
const index = found.findIndex(
29532953
(result) => result._tag === "Success" && result.value?.kind === "file",
29542954
);
2955-
return agentPaths[index] ?? relativePath;
2955+
const literal = agentPaths[index];
2956+
if (literal !== undefined || !needsWorkspaceBasenameLookup(relativePath)) {
2957+
return literal ?? relativePath;
2958+
}
2959+
return (
2960+
(await findAgentDirectoryMatch(relativePath, agentRoots, searchRoot)) ?? relativePath
2961+
);
29562962
}
29572963
if (!match || match === relativePath || signal.aborted) return relativePath;
29582964
const metadata = await metadataOf(originalPath);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ import { projectEnvironment } from "../state/projects";
173173
import { filesystemEnvironment } from "../state/filesystem";
174174
import {
175175
claimWorkspaceBasenameLookup,
176+
findAgentDirectoryMatch,
176177
needsWorkspaceBasenameLookup,
177178
pickProjectLinkMatch,
178179
pickWorkspaceLinkMatch,
@@ -842,7 +843,11 @@ function useChatMarkdownState({
842843
);
843844
const metadata = await getFilesystemMetadata({ environmentId, input: { paths: agentPaths } });
844845
if (metadata._tag !== "Success") return null;
845-
return agentPaths.find((_, index) => metadata.value.entries[index]?.kind === "file") ?? null;
846+
const literal = agentPaths.find((_, index) => metadata.value.entries[index]?.kind === "file");
847+
if (literal !== undefined || !needsWorkspaceBasenameLookup(workspaceRelativePath)) {
848+
return literal ?? null;
849+
}
850+
return findAgentDirectoryMatch(workspaceRelativePath, agentRoots, searchRoot);
846851
};
847852
const openFileInPanel = (fileLinkMeta: MarkdownFileLinkMeta, panelPath: string) => {
848853
if (!threadRef) return;

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

Lines changed: 32 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
agentWorkingDirectories,
55
claimWorkspaceBasenameLookup,
6+
findAgentDirectoryMatch,
67
pickProjectLinkMatch,
78
pickWorkspaceBasenameMatch,
89
pickWorkspaceLinkMatch,
@@ -152,6 +153,37 @@ describe("agentWorkingDirectories", () => {
152153
});
153154
});
154155

156+
// regression: #35337 a bare file name the agent cited from a subfolder of an unregistered checkout
157+
describe("findAgentDirectoryMatch", () => {
158+
const search = (found: Record<string, ReadonlyArray<string>>) => (root: string) =>
159+
Promise.resolve({
160+
root,
161+
entries: (found[root] ?? []).map((path) => ({ path, kind: "file" as const })),
162+
});
163+
164+
it("opens the one file of that name in the latest folder the agent worked in", async () => {
165+
expect(
166+
await findAgentDirectoryMatch(
167+
"config.test.yaml",
168+
["/work/repo-b/server", "/work/repo-b", "/work/repo-c"],
169+
search({
170+
"/work/repo-b/server": ["config.test.yaml"],
171+
"/work/repo-b": ["server/config.test.yaml"],
172+
"/work/repo-c": ["config.test.yaml"],
173+
}),
174+
),
175+
).toBe("/work/repo-b/server/config.test.yaml");
176+
});
177+
178+
it("passes over a folder without the file, and opens nothing when none has it", async () => {
179+
const found = search({ "/work/repo-c": ["etc/config.test.yaml"] });
180+
expect(
181+
await findAgentDirectoryMatch("config.test.yaml", ["/work/repo-b", "/work/repo-c"], found),
182+
).toBe("/work/repo-c/etc/config.test.yaml");
183+
expect(await findAgentDirectoryMatch("config.test.yaml", ["/work/repo-b"], found)).toBeNull();
184+
});
185+
});
186+
155187
describe("claimWorkspaceBasenameLookup", () => {
156188
it("keeps only the newest claim, whatever order the lookups settle in", () => {
157189
const first = claimWorkspaceBasenameLookup();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ export function pickProjectLinkMatch(
137137
return matches.length === 1 ? (matches[0] ?? null) : null;
138138
}
139139

140+
const AGENT_DIRECTORY_SEARCH_LIMIT = 4;
141+
142+
/**
143+
* The file a bare name cites in the latest folder the agent worked in that has one of that name.
144+
* Folders are searched one at a time: the latest usually has it, and each search walks a tree.
145+
*/
146+
export async function findAgentDirectoryMatch(
147+
relativePath: string,
148+
directories: ReadonlyArray<string>,
149+
searchRoot: (root: string) => Promise<{
150+
readonly root: string;
151+
readonly entries: ReadonlyArray<WorkspaceEntryCandidate>;
152+
}>,
153+
): Promise<string | null> {
154+
for (const directory of directories.slice(0, AGENT_DIRECTORY_SEARCH_LIMIT)) {
155+
const match = pickProjectLinkMatch(relativePath, [await searchRoot(directory)]);
156+
if (match !== null) return match;
157+
}
158+
return null;
159+
}
160+
140161
// A quoted string or a comment is one token, so `cd` inside either is not read as a command.
141162
const SHELL_TOKEN = /(?<![^\s;&|()])#[^\n]*|"[^"]*"|'[^']*'|[;&|()\n]|[ \t]+|[^\s;&|()"']+/g;
142163
const SHELL_NAME = /(?:^|\/)(?:ba|z|da)?sh$/;

0 commit comments

Comments
 (0)