Skip to content

Commit ecf5bf3

Browse files
fix(shared): keep command path evidence literal across shell wrappers
1 parent eac8fac commit ecf5bf3

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎packages/shared/src/markdownLinks.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ describe("thread-evidenced file candidates", () => {
505505
"cd ../meeting/raw && ls",
506506
'cat "/notes/meeting 42/transcript.md"',
507507
'type "C:\\Work\\Docs\\Guide.md"',
508+
'cd "\\\\server\\share" && cat docs\\guide.md',
509+
"/bin/bash -lc 'cd ../wrapped-app && cat src/wrapped.ts'",
510+
"/bin/bash -lc 'cat /repos/app-b/src/api/rules/categories.ts'",
511+
'git commit -m "fix: update /repos/app-b/src/api/rules/categories.ts"',
512+
"echo 'see /repos/app-b/src/api/rules/categories.ts'",
508513
"cd /other | cat pipe/file.ts",
509514
"cd /other & cat background/file.ts",
510515
"cd /other || cat failure/file.ts",
@@ -528,9 +533,15 @@ describe("thread-evidenced file candidates", () => {
528533
"/repos/meeting/raw",
529534
"/notes/meeting 42/transcript.md",
530535
"C:\\Work\\Docs\\Guide.md",
536+
"\\\\server\\share\\docs\\guide.md",
537+
"/repos/wrapped-app/src/wrapped.ts",
538+
"/repos/app-b/src/api/rules/categories.ts",
531539
]),
532540
);
533541
expect(paths).not.toContain("/repos/app b/src/local.ts");
542+
expect(
543+
resolveThreadFilePathCandidate("rules/categories.ts", cwd, createThreadFilePathIndex(paths)),
544+
).toBe("/repos/app-b/src/api/rules/categories.ts");
534545
expect(
535546
paths.filter(
536547
(path) =>

‎packages/shared/src/threadFilePaths.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,28 @@ function parentPath(path: string): string | null {
3737
function resolveOperand(path: string, cwd: string | null): string | null {
3838
if (!LITERAL_PATH_PATTERN.test(path) || path.startsWith("-")) return null;
3939
if (isAbsolutePath(path)) return path;
40+
if (path.split(/[/\\]/).some((segment) => segment.trim() !== segment)) return null;
4041
if (cwd === null || !isAbsolutePath(cwd)) return null;
4142
const windows = isWindowsAbsolutePath(cwd);
4243
if (!windows && path.includes("\\")) return null;
4344
const normalized = cwd.replaceAll("\\", "/");
44-
const root = normalized.match(windows ? /^(?:[A-Za-z]:\/|\/\/[^/]+\/[^/]+\/)/ : /^\//)?.[0];
45+
const root = normalized.match(windows ? /^(?:[A-Za-z]:\/|\/\/[^/]+\/[^/]+(?:\/|$))/ : /^\//)?.[0];
4546
if (!root) return null;
4647
const segments = normalized.slice(root.length).split("/").filter(Boolean);
4748
for (const segment of path.replaceAll("\\", "/").split("/")) {
4849
if (segment === "..") segments.pop();
4950
else if (segment !== "" && segment !== ".") segments.push(segment);
5051
}
51-
const resolved = `${root}${segments.join("/")}`;
52+
const resolved = `${root.endsWith("/") ? root : `${root}/`}${segments.join("/")}`;
5253
return windows ? resolved.replaceAll("/", "\\") : resolved;
5354
}
5455

5556
// Unsupported shell syntax discards command evidence instead of guessing its cwd.
5657
function commandPaths(input: string, cwd: string | null): string[] {
58+
const shell = input.match(
59+
/^(?:\/(?:usr\/)?bin\/)?(?:sh|bash|zsh)\s+-(?:c|lc)\s+(?:"([^"]*)"|'([^']*)')\s*$/,
60+
);
61+
if (shell) input = shell[1] ?? shell[2]!;
5762
if (/[|`$*?()<>#{}]/.test(input) || /(^|[^&])&([^&]|$)/.test(input)) return [];
5863
if (/\\[\s"']/.test(input)) return [];
5964
const tokens = /[ \t\r]+|&&|[;\n]|"[^"\n]*"|'[^'\n]*'|[^\s;&"']+/gy;

0 commit comments

Comments
 (0)