fix(coding-agent,tui): stabilize windows shell/path handling

fixes #1775

fixes #1769
This commit is contained in:
badlogic
2026-03-14 05:36:04 +01:00
parent 1a4d153d7a
commit 4f81c3c28d
10 changed files with 213 additions and 76 deletions

View File

@@ -125,6 +125,10 @@ function toPosixPath(p: string): string {
return p.split(sep).join("/");
}
function getHomeDir(): string {
return process.env.HOME || homedir();
}
function prefixIgnorePattern(line: string, prefix: string): string | null {
const trimmed = line.trim();
if (!trimmed) return null;
@@ -512,44 +516,55 @@ function collectResourceFiles(dir: string, resourceType: ResourceType): string[]
}
function matchesAnyPattern(filePath: string, patterns: string[], baseDir: string): boolean {
const rel = relative(baseDir, filePath);
const rel = toPosixPath(relative(baseDir, filePath));
const name = basename(filePath);
const filePathPosix = toPosixPath(filePath);
const isSkillFile = name === "SKILL.md";
const parentDir = isSkillFile ? dirname(filePath) : undefined;
const parentRel = isSkillFile ? relative(baseDir, parentDir!) : undefined;
const parentRel = isSkillFile ? toPosixPath(relative(baseDir, parentDir!)) : undefined;
const parentName = isSkillFile ? basename(parentDir!) : undefined;
const parentDirPosix = isSkillFile ? toPosixPath(parentDir!) : undefined;
return patterns.some((pattern) => {
if (minimatch(rel, pattern) || minimatch(name, pattern) || minimatch(filePath, pattern)) {
const normalizedPattern = toPosixPath(pattern);
if (
minimatch(rel, normalizedPattern) ||
minimatch(name, normalizedPattern) ||
minimatch(filePathPosix, normalizedPattern)
) {
return true;
}
if (!isSkillFile) return false;
return minimatch(parentRel!, pattern) || minimatch(parentName!, pattern) || minimatch(parentDir!, pattern);
return (
minimatch(parentRel!, normalizedPattern) ||
minimatch(parentName!, normalizedPattern) ||
minimatch(parentDirPosix!, normalizedPattern)
);
});
}
function normalizeExactPattern(pattern: string): string {
if (pattern.startsWith("./") || pattern.startsWith(".\\")) {
return pattern.slice(2);
}
return pattern;
const normalized = pattern.startsWith("./") || pattern.startsWith(".\\") ? pattern.slice(2) : pattern;
return toPosixPath(normalized);
}
function matchesAnyExactPattern(filePath: string, patterns: string[], baseDir: string): boolean {
if (patterns.length === 0) return false;
const rel = relative(baseDir, filePath);
const rel = toPosixPath(relative(baseDir, filePath));
const name = basename(filePath);
const filePathPosix = toPosixPath(filePath);
const isSkillFile = name === "SKILL.md";
const parentDir = isSkillFile ? dirname(filePath) : undefined;
const parentRel = isSkillFile ? relative(baseDir, parentDir!) : undefined;
const parentRel = isSkillFile ? toPosixPath(relative(baseDir, parentDir!)) : undefined;
const parentDirPosix = isSkillFile ? toPosixPath(parentDir!) : undefined;
return patterns.some((pattern) => {
const normalized = normalizeExactPattern(pattern);
if (normalized === rel || normalized === filePath) {
if (normalized === rel || normalized === filePathPosix) {
return true;
}
if (!isSkillFile) return false;
return normalized === parentRel || normalized === parentDir;
return normalized === parentRel || normalized === parentDirPosix;
});
}
@@ -1385,17 +1400,17 @@ export class DefaultPackageManager implements PackageManager {
private resolvePath(input: string): string {
const trimmed = input.trim();
if (trimmed === "~") return homedir();
if (trimmed.startsWith("~/")) return join(homedir(), trimmed.slice(2));
if (trimmed.startsWith("~")) return join(homedir(), trimmed.slice(1));
if (trimmed === "~") return getHomeDir();
if (trimmed.startsWith("~/")) return join(getHomeDir(), trimmed.slice(2));
if (trimmed.startsWith("~")) return join(getHomeDir(), trimmed.slice(1));
return resolve(this.cwd, trimmed);
}
private resolvePathFromBase(input: string, baseDir: string): string {
const trimmed = input.trim();
if (trimmed === "~") return homedir();
if (trimmed.startsWith("~/")) return join(homedir(), trimmed.slice(2));
if (trimmed.startsWith("~")) return join(homedir(), trimmed.slice(1));
if (trimmed === "~") return getHomeDir();
if (trimmed.startsWith("~/")) return join(getHomeDir(), trimmed.slice(2));
if (trimmed.startsWith("~")) return join(getHomeDir(), trimmed.slice(1));
return resolve(baseDir, trimmed);
}
@@ -1632,7 +1647,7 @@ export class DefaultPackageManager implements PackageManager {
prompts: join(projectBaseDir, "prompts"),
themes: join(projectBaseDir, "themes"),
};
const userAgentsSkillsDir = join(homedir(), ".agents", "skills");
const userAgentsSkillsDir = join(getHomeDir(), ".agents", "skills");
const projectAgentsSkillDirs = collectAncestorAgentsSkillDirs(this.cwd).filter(
(dir) => resolve(dir) !== resolve(userAgentsSkillsDir),
);

View File

@@ -3,7 +3,8 @@
* Used by auth-storage.ts and model-registry.ts.
*/
import { execSync } from "child_process";
import { execSync, spawnSync } from "child_process";
import { getShellConfig } from "../utils/shell.js";
// Cache for shell command results (persists for process lifetime)
const commandResultCache = new Map<string, string | undefined>();
@@ -21,23 +22,62 @@ export function resolveConfigValue(config: string): string | undefined {
return envValue || config;
}
function executeCommand(commandConfig: string): string | undefined {
if (commandResultCache.has(commandConfig)) {
return commandResultCache.get(commandConfig);
}
function executeWithConfiguredShell(command: string): { executed: boolean; value: string | undefined } {
try {
const { shell, args } = getShellConfig();
const result = spawnSync(shell, [...args, command], {
encoding: "utf-8",
timeout: 10000,
stdio: ["ignore", "pipe", "ignore"],
shell: false,
windowsHide: true,
});
const command = commandConfig.slice(1);
let result: string | undefined;
if (result.error) {
const error = result.error as NodeJS.ErrnoException;
if (error.code === "ENOENT") {
return { executed: false, value: undefined };
}
return { executed: true, value: undefined };
}
if (result.status !== 0) {
return { executed: true, value: undefined };
}
const value = (result.stdout ?? "").trim();
return { executed: true, value: value || undefined };
} catch {
return { executed: false, value: undefined };
}
}
function executeWithDefaultShell(command: string): string | undefined {
try {
const output = execSync(command, {
encoding: "utf-8",
timeout: 10000,
stdio: ["ignore", "pipe", "ignore"],
});
result = output.trim() || undefined;
return output.trim() || undefined;
} catch {
result = undefined;
return undefined;
}
}
function executeCommand(commandConfig: string): string | undefined {
if (commandResultCache.has(commandConfig)) {
return commandResultCache.get(commandConfig);
}
const command = commandConfig.slice(1);
const result =
process.platform === "win32"
? (() => {
const configuredResult = executeWithConfiguredShell(command);
return configuredResult.executed ? configuredResult.value : executeWithDefaultShell(command);
})()
: executeWithDefaultShell(command);
commandResultCache.set(commandConfig, result);
return result;

View File

@@ -8,6 +8,10 @@ import { ensureTool } from "../../utils/tools-manager.js";
import { resolveToCwd } from "./path-utils.js";
import { DEFAULT_MAX_BYTES, formatSize, type TruncationResult, truncateHead } from "./truncate.js";
function toPosixPath(value: string): string {
return value.split(path.sep).join("/");
}
const findSchema = Type.Object({
pattern: Type.String({
description: "Glob pattern to match files, e.g. '*.ts', '**/*.json', or 'src/**/*.spec.ts'",
@@ -102,9 +106,9 @@ export function createFindTool(cwd: string, options?: FindToolOptions): AgentToo
// Relativize paths
const relativized = results.map((p) => {
if (p.startsWith(searchPath)) {
return p.slice(searchPath.length + 1);
return toPosixPath(p.slice(searchPath.length + 1));
}
return path.relative(searchPath, p);
return toPosixPath(path.relative(searchPath, p));
});
const resultLimitReached = relativized.length >= effectiveLimit;
@@ -228,7 +232,7 @@ export function createFindTool(cwd: string, options?: FindToolOptions): AgentToo
relativePath += "/";
}
relativized.push(relativePath);
relativized.push(toPosixPath(relativePath));
}
const resultLimitReached = relativized.length >= effectiveLimit;