Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(trail): find programs, scratch folders and Trail's files on Windows
Shrish Dwivedi3b4e6d70deaf
diff --git a/src/skills/anchors.ts b/src/skills/anchors.tsindex ba2dcba0..d6bc9f81 100644--- a/src/skills/anchors.ts+++ b/src/skills/anchors.ts@@ -25,7 +25,7 @@ import { flatLine } from '../notes/flatten.js';import { leadingCds, simpleCommands } from '../trace/shell.js';import { programWords } from '../trace/shell-words.js';import { unquote } from '../trace/shell-scan.js';-import { FIXED_DIRS } from '../trace/guard-fast.js';+import { FIXED_DIRS, pathDirs, programFiles } from '../trace/guard-fast.js';import { wideGone, type WideKind } from './anchors-wide.js';export type AnchorKind = 'program' | 'script' | 'make' | 'file' | 'branch' | WideKind;@@ -283,7 +283,7 @@ const VERSION_DIRS = ['.volta/bin', '.bun/bin', '.cargo/bin', 'go/bin', '.pyenv//** The hook's PATH, 4.11's fixed folders and the version managers' folders (R35). */function programDirs(env: NodeJS.ProcessEnv): string[] {const home = env.HOME || homedir();- const dirs = String(env.PATH ?? '').split(':').filter((d) => d.startsWith('/'));+ const dirs = pathDirs(env);for (const d of FIXED_DIRS) {if (d.startsWith('$HOME')) dirs.push(join(home, d.slice('$HOME'.length)));else if (!d.includes('$USER')) dirs.push(d);@@ -296,15 +296,18 @@ function programDirs(env: NodeJS.ProcessEnv): string[] {return [...new Set(dirs)];}-function onPath(name: string, dirs: string[]): boolean {- return dirs.some((d) => {+/** On Windows a program is git.exe or npm.cmd, and no file has an exec bit: any regular file of those names counts. */+function onPath(name: string, dirs: string[], env: NodeJS.ProcessEnv): boolean {+ const names = programFiles(name, env);+ const win = process.platform === 'win32';+ return dirs.some((d) => names.some((n) => {try {- const st = statSync(join(d, name));- return st.isFile() && (st.mode & 0o111) !== 0;+ const st = statSync(join(d, n));+ return st.isFile() && (win || (st.mode & 0o111) !== 0);} catch {return false;}- });+ }));}/** Scripts in the nearest package.json from `dir` up to the checkout; null when none can be read. */@@ -381,7 +384,7 @@ export function goneAnchors(skill: ParsedSkill, cwd: string, env: NodeJS.Process// No PATH, no check: the fixed folders alone miss /usr/bin.if (!String(env.PATH ?? '').trim()) continue;dirs ??= programDirs(env);- if (!onPath(a.name, dirs)) out.push(a);+ if (!onPath(a.name, dirs, env)) out.push(a);} else if (!cwd) {continue;} else if (a.kind === 'script') {diff --git a/src/trace/guard-fast.ts b/src/trace/guard-fast.tsindex a6335bcc..fb3e3b3a 100644--- a/src/trace/guard-fast.ts+++ b/src/trace/guard-fast.ts@@ -13,7 +13,7 @@* `process.getBuiltinModule`. TRAIL_HOME is resolved as gate-file.ts does.*/import { homedir, userInfo } from 'node:os';-import { dirname, join } from 'node:path';+import { delimiter, dirname, isAbsolute, join } from 'node:path';import type * as Fs from 'node:fs';import type * as Crypto from 'node:crypto';import { sessionPath, trailHomeFast } from './gate-file.js';@@ -120,18 +120,32 @@ function onlyRunsTimeout(command: string): boolean {return runs > 0 && (command.match(/\btimeout\b/g) ?? []).length === runs;}-/** An executable regular file at `dir/timeout`. */-function executableIn(dir: string): boolean {+/** The PATH's folders, split on the platform's delimiter (';' on Windows), absolute ones only. */+export function pathDirs(env: NodeJS.ProcessEnv): string[] {+ return String(env.PATH ?? '').split(delimiter).filter((d) => d !== '' && isAbsolute(d));+}++/** The file names a program can have: itself, and on Windows with each PATHEXT ending too (git.exe, npm.cmd). */+export function programFiles(name: string, env: NodeJS.ProcessEnv): string[] {+ if (process.platform !== 'win32') return [name];+ const exts = String(env.PATHEXT || '.COM;.EXE;.BAT;.CMD').split(';').map((e) => e.trim().toLowerCase()).filter((e) => /^\.[a-z0-9]{1,8}$/.test(e));+ return [name, ...exts.map((e) => name + e)];+}++/** An executable regular file at `dir/timeout` (on Windows, which has no exec bit, any timeout.exe and the like). */+function executableIn(dir: string, env: NodeJS.ProcessEnv): boolean {if (!dir) return false;- try {- const f = fs();- const p = join(dir, 'timeout');- if (!f.statSync(p).isFile()) return false;- f.accessSync(p, f.constants.X_OK);- return true;- } catch {- return false;- }+ const f = fs();+ return programFiles('timeout', env).some((name) => {+ try {+ const p = join(dir, name);+ if (!f.statSync(p).isFile()) return false;+ f.accessSync(p, f.constants.X_OK);+ return true;+ } catch {+ return false;+ }+ });}/** PATH dirs from env, then the fixed folders of 4.11. */@@ -139,13 +153,13 @@ export function timeoutOnPath(env: NodeJS.ProcessEnv): boolean {const home = env.HOME || homedir();let user = env.USER || env.LOGNAME || '';if (!user) try { user = userInfo().username; } catch { /* none */ }- const dirs = String(env.PATH ?? '').split(':').filter((d) => d.startsWith('/'));+ const dirs = pathDirs(env);for (const d of FIXED_DIRS) {if (d.startsWith('$HOME')) dirs.push(join(home, d.slice('$HOME'.length)));else if (d.includes('$USER')) { if (/^[\w.-]{1,64}$/.test(user)) dirs.push(d.replace('$USER', user)); }else dirs.push(d);}- return [...new Set(dirs)].some(executableIn);+ return [...new Set(dirs)].some((d) => executableIn(d, env));}/** Reads <trail home>/learnings/no-timeout.md (under 1 KB) when it exists. Deny only when sure (4.11). */diff --git a/src/trace/run-calls.ts b/src/trace/run-calls.tsindex f66747a0..e368a364 100644--- a/src/trace/run-calls.ts+++ b/src/trace/run-calls.ts@@ -285,7 +285,17 @@ function scratchRoots(): string[] {return [tmpdir(), process.env.TMPDIR ?? '', '/tmp', '/private/tmp', '/var/folders'].filter(Boolean).map((p) => resolve(p));}-const inScratch = (p: string) => /\/scratchpad\//.test(p) || scratchRoots().some((r) => p === r || p.startsWith(r + sep));+const POSIX_SCRATCH = ['/tmp', '/private/tmp', '/var/folders'];++function inScratch(p: string): boolean {+ if (/\/scratchpad\//.test(p) || scratchRoots().some((r) => p === r || p.startsWith(r + sep))) return true;+ if (process.platform !== 'win32') return false;+ // Windows: a path may use either slash and any case, and Git Bash's /tmp is the person's temp folder.+ const fwd = p.replace(/\\/g, '/').toLowerCase();+ if (fwd.includes('/scratchpad/') || POSIX_SCRATCH.some((r) => fwd === r || fwd.startsWith(`${r}/`))) return true;+ const q = resolve(p).toLowerCase();+ return scratchRoots().some((r) => q === r.toLowerCase() || q.startsWith(r.toLowerCase() + sep));+}/** Absolute paths of helper scripts the run executed from scratch folders (4.5 step 7), from its ok step calls. */export function runHelpers(run: Pick<RunState, 'calls'>): string[] {diff --git a/src/trace/skill-session.ts b/src/trace/skill-session.tsindex 18031716..77a687e3 100644--- a/src/trace/skill-session.ts+++ b/src/trace/skill-session.ts@@ -63,9 +63,11 @@ export interface GateCtx {const set = (v: string | undefined) => typeof v === 'string' && v !== '' && v !== '0' && v.toLowerCase() !== 'false';function scratchFolder(cwd: string): boolean {- const p = resolve(cwd);- if (`${p}/`.includes('/scratchpad/')) return true;- const roots = [tmpdir(), '/tmp', '/private/tmp', '/var/folders', '/private/var/folders'].map((r) => resolve(r));+ // Windows paths ignore case (c:\ and C:\ are one folder) and resolve() gives them '\'.+ const fold = (x: string) => (process.platform === 'win32' ? x.toLowerCase() : x);+ const p = fold(resolve(cwd));+ if (`${p}${sep}`.includes(`${sep}scratchpad${sep}`)) return true;+ const roots = [tmpdir(), '/tmp', '/private/tmp', '/var/folders', '/private/var/folders'].map((r) => fold(resolve(r)));return roots.some((r) => p === r || p.startsWith(r + sep));}diff --git a/src/trace/tamper.ts b/src/trace/tamper.tsindex b42c3aa2..2df7f3f8 100644--- a/src/trace/tamper.ts+++ b/src/trace/tamper.ts@@ -48,7 +48,9 @@ interface Place { trail: string; projects: string; transcript: string | null }/** The protected places as written, and again with every symlink on the way resolved. */interface Zones { as: Place; real: Place; home: string }-const under = (p: string, dir: string) => p === dir || p.startsWith(dir + sep);+/** Windows paths ignore case: C:\Users\dev\.trail and c:\users\dev\.trail are one folder. */+const fold = (p: string) => (process.platform === 'win32' ? p.toLowerCase() : p);+const under = (p: string, dir: string) => { const a = fold(p), b = fold(dir); return a === b || a.startsWith(b + sep); };/** `p` with its deepest existing folder resolved through symlinks: where a write to `p` really lands. */function realOf(p: string): string {@@ -83,7 +85,7 @@ function zonesOf(ctx: TamperCtx): Zones {}function guardedIn(p: string, z: Place, wipe: boolean): boolean {- if (under(p, z.trail) || (z.transcript !== null && p === z.transcript)) return true;+ if (under(p, z.trail) || (z.transcript !== null && fold(p) === fold(z.transcript))) return true;if (wipe && (under(z.trail, p) || under(z.projects, p))) return true;if (!under(p, z.projects)) return false;return relative(z.projects, p).split(sep)[1] !== 'memory';@@ -298,7 +300,8 @@ function simpleWrites(w: string[], folder: string | null, z: Zones, vars?: Vars)/** A `cd` from `folder`; null when only the shell could know where it lands. */function cdFrom(folder: string | null, simple: string, home: string): string | null {const next = effectiveFolder(folder ?? '/\0', simple, home);- return next && !next.startsWith('/\0') ? next : null;+ // No real path holds a NUL; on Windows resolve() puts a drive before the '/\0' this starts from.+ return next && !next.includes('\0') ? next : null;}/** Whether a Bash command writes a guarded path; `depth` bounds `bash -c` inside `bash -c`. */