Skip to content

Commit 5622273

Browse files
fix(web): a cd inside quoted text no longer counts as a folder the agent worked in
A command such as echo "cd /work/example" was read as a directory change, so a file link could open in a checkout the agent never entered. Commands are now split into words with quoted strings kept whole, and only a cd that is the command itself, or one inside a script handed to sh -c, contributes. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 4ac7486 commit 5622273

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ describe("agentWorkingDirectories", () => {
142142
directories(
143143
"cd docs && cd ../other && cd ~/repo && cd $ROOT/x && cd /work/*/src",
144144
"abcd /work/a; echo cd /work/b; git -C /work/c status; cd /work/d",
145+
`echo "cd /work/g"; echo 'x; cd /work/h' && grep -c "cd /work/i" notes`,
145146
"cd /work/e\\ f && cd `pwd`/x",
146147
),
147148
).toEqual(["/work/d"]);

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

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

140-
// `cd` opening a command or a quoted script, then one operand; each character is read once.
141-
const CD_OPERAND =
142-
/(?:^|[;&|(\n"'])[ \t]*cd[ \t]+("[^"\n]*"|'[^'\n]*'|[^\s;&|()<>"']+)(?=$|[\s;&|)"'])/g;
140+
// A quoted string is one token, so `cd` inside quoted data is not read as a command.
141+
const SHELL_TOKEN = /"[^"]*"|'[^']*'|[;&|()\n]|[ \t]+|[^\s;&|()"']+/g;
142+
const SHELL_NAME = /(?:^|\/)(?:ba|z|da)?sh$/;
143+
144+
/** The operands of the `cd` commands in `script`, in order, through `sh -c "<script>"`. */
145+
function cdOperands(script: string): Array<string> {
146+
const operands: Array<string> = [];
147+
let words: Array<string> = [];
148+
let wordOpen = false;
149+
const endCommand = () => {
150+
const flag = words.findIndex((word) => /^-[a-z]*c$/.test(word));
151+
const inner = words[flag + 1];
152+
if (words[0] === "cd" && words[1] !== undefined) operands.push(words[1]);
153+
else if (flag > 0 && inner !== undefined && /^(["']).*\1$/s.test(inner)) {
154+
if (words.slice(0, flag).some((word) => SHELL_NAME.test(word))) {
155+
operands.push(...cdOperands(inner.slice(1, -1)));
156+
}
157+
}
158+
words = [];
159+
wordOpen = false;
160+
};
161+
for (const [token] of script.matchAll(SHELL_TOKEN)) {
162+
if (/^[;&|()\n]/.test(token)) endCommand();
163+
else if (/^[ \t]/.test(token)) wordOpen = false;
164+
else if (wordOpen) words[words.length - 1] += token;
165+
else {
166+
words.push(token);
167+
wordOpen = true;
168+
}
169+
}
170+
endCommand();
171+
return operands;
172+
}
143173

144174
function literalDirectory(operand: string): string | null {
145175
const path = /^["']/.test(operand) ? operand.slice(1, -1) : operand;
146176
// The shell would expand these, and only it knows to what.
147-
if (!isAbsolutePath(path) || /[$`*?[\]{}]/.test(path)) return null;
177+
if (!isAbsolutePath(path) || /[$`*?[\]{}"']/.test(path)) return null;
148178
return path.includes("\\") && !isWindowsAbsolutePath(path) ? null : path;
149179
}
150180

@@ -171,8 +201,8 @@ export function agentWorkingDirectories(
171201
for (let index = replyIndex - 1; index >= 0; index--) {
172202
const item = items[index]?.item;
173203
if (item?.type !== "command_execution" || typeof item.input !== "string") continue;
174-
for (const match of [...item.input.matchAll(CD_OPERAND)].reverse()) {
175-
const directory = literalDirectory(match[1] ?? "");
204+
for (const operand of cdOperands(item.input).reverse()) {
205+
const directory = literalDirectory(operand);
176206
if (directory !== null) directories.add(directory);
177207
if (directories.size === FILESYSTEM_METADATA_BATCH_LIMIT) return [...directories];
178208
}

0 commit comments

Comments
 (0)