From 2e0764e7b06524ea77876a97b2182dd02e9163d8 Mon Sep 17 00:00:00 2001 From: Jack Felke Date: Tue, 10 Mar 2026 11:47:34 -0700 Subject: [PATCH] fix: eliminate shell syntax in git.run() calls across 8 tools (#172) Replace shell-piped commands passed to execFileSync('git', args) with: - Array-arg run() calls for git commands - New shell() helper (src/lib/shell.ts) for non-git commands needing pipes - Native Node.js fs/path for file reads, stat, line counting - In-JS filtering/slicing instead of grep/head/tail/wc pipes Affected tools: verify-completion, token-audit, session-handoff, audit-workspace, sharpen-followup, scope-work, enrich-agent-task, sequence-tasks Closes #172 --- package-lock.json | 2 +- src/lib/shell.ts | 28 ++++++++++++++++++++++++++++ src/tools/audit-workspace.ts | 16 ++++++++++++++-- src/tools/enrich-agent-task.ts | 32 ++++++++++++++++++++++++-------- src/tools/scope-work.ts | 4 +++- src/tools/sequence-tasks.ts | 3 ++- src/tools/session-handoff.ts | 10 +++++++--- src/tools/sharpen-followup.ts | 2 +- src/tools/token-audit.ts | 21 ++++++++++++++------- src/tools/verify-completion.ts | 32 ++++++++++++++++++-------------- 10 files changed, 112 insertions(+), 38 deletions(-) create mode 100644 src/lib/shell.ts diff --git a/package-lock.json b/package-lock.json index 89ef280..4e5a169 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,7 +29,7 @@ "vitest": "^4.0.18" }, "engines": { - "node": ">=18" + "node": ">=20" } }, "node_modules/@esbuild/aix-ppc64": { diff --git a/src/lib/shell.ts b/src/lib/shell.ts new file mode 100644 index 0000000..394572d --- /dev/null +++ b/src/lib/shell.ts @@ -0,0 +1,28 @@ +import { execSync } from "child_process"; +import { PROJECT_DIR } from "./files.js"; + +/** + * Run a shell command string (with pipes, redirects, etc.). + * Use this for non-git commands or commands that need shell interpretation. + * Returns stdout+stderr on success, or a descriptive error string on failure. + */ +export function shell(cmd: string, opts: { timeout?: number } = {}): string { + try { + return execSync(cmd, { + cwd: PROJECT_DIR, + encoding: "utf-8", + timeout: opts.timeout || 30000, + maxBuffer: 2 * 1024 * 1024, + stdio: ["pipe", "pipe", "pipe"], + shell: "/bin/sh", + }).trim(); + } catch (e: any) { + if (e.killed === true || e.signal === "SIGTERM") { + return `[timed out after ${opts.timeout || 30000}ms]`; + } + // For commands like tsc that exit non-zero but produce useful output + const output = (e.stdout || "") + (e.stderr || ""); + if (output.trim()) return output.trim(); + return `[command failed: ${cmd} (exit ${e.status ?? "?"})]`; + } +} diff --git a/src/tools/audit-workspace.ts b/src/tools/audit-workspace.ts index d4306bd..6f59859 100644 --- a/src/tools/audit-workspace.ts +++ b/src/tools/audit-workspace.ts @@ -1,6 +1,10 @@ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { run } from "../lib/git.js"; +import { shell } from "../lib/shell.js"; import { readIfExists, findWorkspaceDocs } from "../lib/files.js"; +import { readdirSync, existsSync } from "fs"; +import { join } from "path"; +import { PROJECT_DIR } from "../lib/files.js"; /** Extract top-level work areas from file paths generically */ function detectWorkAreas(files: string[]): Set { @@ -36,7 +40,8 @@ export function registerAuditWorkspace(server: McpServer): void { {}, async () => { const docs = findWorkspaceDocs(); - const recentFiles = run("git diff --name-only HEAD~10 2>/dev/null || echo ''").split("\n").filter(Boolean); + const diffResult = run(["diff", "--name-only", "HEAD~10"]); + const recentFiles = diffResult.startsWith("[") ? [] : diffResult.split("\n").filter(Boolean); const sections: string[] = []; // Doc freshness @@ -75,7 +80,14 @@ export function registerAuditWorkspace(server: McpServer): void { // Check for gap trackers or similar tracking docs const trackingDocs = Object.entries(docs).filter(([n]) => /gap|track|progress/i.test(n)); if (trackingDocs.length > 0) { - const testFilesCount = parseInt(run("find tests -name '*.spec.ts' -o -name '*.test.ts' 2>/dev/null | wc -l").trim()) || 0; + let testFilesCount = 0; + const testsDir = join(PROJECT_DIR, "tests"); + if (existsSync(testsDir)) { + try { + const allFiles = readdirSync(testsDir, { recursive: true }) as string[]; + testFilesCount = allFiles.filter(f => /\.(spec|test)\.(ts|tsx|js|jsx)$/.test(String(f))).length; + } catch { /* skip */ } + } sections.push(`## Tracking Docs\n${trackingDocs.map(([n]) => { const age = docStatus.find(d => d.name === n)?.ageHours ?? "?"; return `- .claude/${n} — last updated ${age}h ago`; diff --git a/src/tools/enrich-agent-task.ts b/src/tools/enrich-agent-task.ts index 236edfa..fbfbfd2 100644 --- a/src/tools/enrich-agent-task.ts +++ b/src/tools/enrich-agent-task.ts @@ -29,12 +29,16 @@ function findAreaFiles(area: string): string { // If area looks like a path, search directly if (area.includes("/")) { - return run(`git ls-files -- '${safeArea}*' 2>/dev/null | head -20`); + const result = run(["ls-files", "--", `${safeArea}*`]); + if (!result.startsWith("[")) return result.split("\n").slice(0, 20).join("\n"); } // Search for area keyword in git-tracked file paths - const files = run(`git ls-files 2>/dev/null | grep -i '${safeArea}' | head -20`); - if (files && !files.startsWith("[command failed")) return files; + const allFiles = run(["ls-files"]); + if (allFiles.startsWith("[")) return getDiffFiles("HEAD~3"); + const re = new RegExp(safeArea, "i"); + const matched = allFiles.split("\n").filter(f => re.test(f)).slice(0, 20).join("\n"); + if (matched) return matched; // Fallback to recently changed files return getDiffFiles("HEAD~3"); @@ -42,18 +46,30 @@ function findAreaFiles(area: string): string { /** Find related test files for an area */ function findRelatedTests(area: string): string { - if (!area) return run("git ls-files 2>/dev/null | grep -E '\\.(spec|test)\\.(ts|tsx|js|jsx)$' | head -10"); + const allFiles = run(["ls-files"]); + if (allFiles.startsWith("[")) return ""; + const testFiles = allFiles.split("\n").filter(f => /\.(spec|test)\.(ts|tsx|js|jsx)$/.test(f)); + + if (area) { + const safeArea = shellEscape(area.split(/\s+/)[0]); + const re = new RegExp(safeArea, "i"); + const matched = testFiles.filter(f => re.test(f)).slice(0, 10).join("\n"); + if (matched) return matched; + } - const safeArea = shellEscape(area.split(/\s+/)[0]); - const tests = run(`git ls-files 2>/dev/null | grep -E '\\.(spec|test)\\.(ts|tsx|js|jsx)$' | grep -i '${safeArea}' | head -10`); - return tests || run("git ls-files 2>/dev/null | grep -E '\\.(spec|test)\\.(ts|tsx|js|jsx)$' | head -10"); + return testFiles.slice(0, 10).join("\n"); } /** Get an example pattern from the first matching file */ function getExamplePattern(files: string): string { const firstFile = files.split("\n").filter(Boolean)[0]; if (!firstFile) return "no pattern available"; - return run(`head -30 '${shellEscape(firstFile)}' 2>/dev/null || echo 'could not read file'`); + const filePath = join(PROJECT_DIR, firstFile); + try { + if (!existsSync(filePath)) return "could not read file"; + const content = readFileSync(filePath, "utf-8"); + return content.split("\n").slice(0, 30).join("\n"); + } catch { return "could not read file"; } } // --------------------------------------------------------------------------- diff --git a/src/tools/scope-work.ts b/src/tools/scope-work.ts index 9b5d971..0b2694c 100644 --- a/src/tools/scope-work.ts +++ b/src/tools/scope-work.ts @@ -128,7 +128,9 @@ export function registerScopeWork(server: McpServer): void { .slice(0, 5); if (grepTerms.length > 0) { const pattern = shellEscape(grepTerms.join("|")); - matchedFiles = run(`git ls-files | head -500 | grep -iE '${pattern}' | head -30`); + const allFiles = run(["ls-files"]); + const re = new RegExp(grepTerms.join("|"), "i"); + matchedFiles = allFiles.split("\n").filter(f => re.test(f)).slice(0, 30).join("\n"); } // Check which relevant dirs actually exist (with path traversal protection) diff --git a/src/tools/sequence-tasks.ts b/src/tools/sequence-tasks.ts index 22dea23..5f76840 100644 --- a/src/tools/sequence-tasks.ts +++ b/src/tools/sequence-tasks.ts @@ -90,7 +90,8 @@ export function registerSequenceTasks(server: McpServer): void { // For locality: infer directories from path-like tokens in task text if (strategy === "locality") { // Use git ls-files with a depth limit instead of find for performance - const gitFiles = run("git ls-files 2>/dev/null | head -1000"); + const allGitFiles = run(["ls-files"]); + const gitFiles = allGitFiles.startsWith("[") ? "" : allGitFiles.split("\n").slice(0, 1000).join("\n"); const knownDirs = new Set(); for (const f of gitFiles.split("\n").filter(Boolean)) { const parts = f.split("/"); diff --git a/src/tools/session-handoff.ts b/src/tools/session-handoff.ts index d199462..695c326 100644 --- a/src/tools/session-handoff.ts +++ b/src/tools/session-handoff.ts @@ -3,13 +3,17 @@ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { existsSync, readFileSync } from "fs"; import { join } from "path"; import { run, getBranch, getRecentCommits, getStatus } from "../lib/git.js"; +import { shell } from "../lib/shell.js"; import { readIfExists, findWorkspaceDocs } from "../lib/files.js"; import { STATE_DIR, now } from "../lib/state.js"; +import { execFileSync } from "child_process"; /** Check if a CLI tool is available */ function hasCommand(cmd: string): boolean { - const result = run(`command -v ${cmd} 2>/dev/null`); - return !!result && !result.startsWith("[command failed"); + try { + execFileSync("which", [cmd], { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }); + return true; + } catch { return false; } } export function registerSessionHandoff(server: McpServer): void { @@ -44,7 +48,7 @@ export function registerSessionHandoff(server: McpServer): void { // Only try gh if it exists if (hasCommand("gh")) { - const openPRs = run("gh pr list --state open --json number,title,headRefName 2>/dev/null || echo '[]'"); + const openPRs = shell("gh pr list --state open --json number,title,headRefName 2>/dev/null || echo '[]'"); if (openPRs && openPRs !== "[]") { sections.push(`## Open PRs\n\`\`\`json\n${openPRs}\n\`\`\``); } diff --git a/src/tools/sharpen-followup.ts b/src/tools/sharpen-followup.ts index db5acaa..0065458 100644 --- a/src/tools/sharpen-followup.ts +++ b/src/tools/sharpen-followup.ts @@ -87,7 +87,7 @@ export function registerSharpenFollowup(server: McpServer): void { // Gather context to resolve ambiguity const contextFiles: string[] = [...(previous_files ?? [])]; const recentChanged = getRecentChangedFiles(); - const porcelainOutput = run("git status --porcelain 2>/dev/null"); + const porcelainOutput = run(["status", "--porcelain"]); const untrackedOrModified = parsePortelainFiles(porcelainOutput); const allKnownFiles = [...new Set([...contextFiles, ...recentChanged, ...untrackedOrModified])].filter(Boolean); diff --git a/src/tools/token-audit.ts b/src/tools/token-audit.ts index b7aad2c..a2a908b 100644 --- a/src/tools/token-audit.ts +++ b/src/tools/token-audit.ts @@ -2,6 +2,7 @@ import { z } from "zod"; import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { run } from "../lib/git.js"; +import { shell } from "../lib/shell.js"; import { readIfExists, findWorkspaceDocs, PROJECT_DIR } from "../lib/files.js"; import { loadState, saveState, now, STATE_DIR } from "../lib/state.js"; import { readFileSync, existsSync, statSync } from "fs"; @@ -39,8 +40,8 @@ export function registerTokenAudit(server: McpServer): void { let wasteScore = 0; // 1. Git diff size & dirty file count - const diffStat = run("git diff --stat --no-color 2>/dev/null"); - const dirtyFiles = run("git diff --name-only 2>/dev/null"); + const diffStat = run(["diff", "--stat", "--no-color"]); + const dirtyFiles = run(["diff", "--name-only"]); const dirtyList = dirtyFiles.split("\n").filter(Boolean); const dirtyCount = dirtyList.length; @@ -63,8 +64,14 @@ export function registerTokenAudit(server: McpServer): void { for (const f of dirtyList.slice(0, 30)) { // Use shell-safe quoting instead of interpolation - const wc = run(`wc -l < '${shellEscape(f)}' 2>/dev/null`); - const lines = parseInt(wc) || 0; + const filePath = join(PROJECT_DIR, f); + let lines = 0; + try { + if (existsSync(filePath)) { + const content = readFileSync(filePath, "utf-8"); + lines = content.split("\n").length; + } + } catch { /* skip unreadable files */ } estimatedContextTokens += lines * AVG_LINE_BYTES * AVG_TOKENS_PER_BYTE; if (lines > 500) { largeFiles.push(`${f} (${lines} lines)`); @@ -80,8 +87,8 @@ export function registerTokenAudit(server: McpServer): void { // 3. CLAUDE.md bloat check const claudeMd = readIfExists("CLAUDE.md", 1); if (claudeMd !== null) { - const stat = run(`wc -c < '${shellEscape("CLAUDE.md")}' 2>/dev/null`); - const bytes = parseInt(stat) || 0; + const claudePath = join(PROJECT_DIR, "CLAUDE.md"); + const bytes = existsSync(claudePath) ? statSync(claudePath).size : 0; if (bytes > 5120) { patterns.push(`CLAUDE.md is ${(bytes / 1024).toFixed(1)}KB — injected every session, burns tokens on paste`); recommendations.push("Trim CLAUDE.md to essentials (<5KB). Move reference docs to files read on-demand"); @@ -139,7 +146,7 @@ export function registerTokenAudit(server: McpServer): void { // Read with size cap: take the tail if too large const raw = stat.size <= MAX_TOOL_LOG_BYTES ? readFileSync(toolLogPath, "utf-8") - : run(`tail -c ${MAX_TOOL_LOG_BYTES} '${shellEscape(toolLogPath)}'`); + : shell(`tail -c ${MAX_TOOL_LOG_BYTES} '${shellEscape(toolLogPath)}'`); const lines = raw.trim().split("\n").filter(Boolean); totalToolCalls = lines.length; diff --git a/src/tools/verify-completion.ts b/src/tools/verify-completion.ts index 732532f..25d5a98 100644 --- a/src/tools/verify-completion.ts +++ b/src/tools/verify-completion.ts @@ -2,7 +2,8 @@ import { z } from "zod"; import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { run, getStatus } from "../lib/git.js"; import { PROJECT_DIR } from "../lib/files.js"; -import { existsSync } from "fs"; +import { shell } from "../lib/shell.js"; +import { existsSync, readFileSync } from "fs"; import { join } from "path"; /** Detect package manager from lockfiles */ @@ -34,7 +35,9 @@ function detectTestRunner(): string | null { /** Check if a build script exists in package.json */ function hasBuildScript(): boolean { try { - const pkg = JSON.parse(run("cat package.json 2>/dev/null")); + const pkgPath = join(PROJECT_DIR, "package.json"); + if (!existsSync(pkgPath)) return false; + const pkg = JSON.parse(readFileSync(pkgPath, "utf-8")); return !!pkg?.scripts?.build; } catch { return false; } } @@ -55,7 +58,8 @@ export function registerVerifyCompletion(server: McpServer): void { const checks: { name: string; passed: boolean; detail: string }[] = []; // 1. Type check (single invocation, extract both result and count) - const tscOutput = run(`${pm === "npx" ? "npx" : pm} tsc --noEmit 2>&1 | tail -20`); + const tscCmd = pm === "npx" ? "npx tsc --noEmit" : `${pm} tsc --noEmit`; + const tscOutput = shell(tscCmd, { timeout: 60000 }); const errorLines = tscOutput.split("\n").filter(l => /error TS\d+/.test(l)); const typePassed = errorLines.length === 0; checks.push({ @@ -80,39 +84,38 @@ export function registerVerifyCompletion(server: McpServer): void { // 3. Tests if (!skip_tests) { const runner = detectTestRunner(); - const changedFiles = run("git diff --name-only HEAD~1 2>/dev/null").split("\n").filter(Boolean); + const changedFiles = run(["diff", "--name-only", "HEAD~1"]).split("\n").filter(Boolean); let testCmd = ""; if (runner === "playwright") { const runnerCmd = `${pm === "npx" ? "npx" : `${pm} exec`} playwright test`; if (test_scope && test_scope !== "all") { testCmd = test_scope.endsWith(".spec.ts") || test_scope.endsWith(".test.ts") - ? `${runnerCmd} ${test_scope} --reporter=line 2>&1 | tail -20` - : `${runnerCmd} --grep "${test_scope}" --reporter=line 2>&1 | tail -20`; + ? `${runnerCmd} ${test_scope} --reporter=line` + : `${runnerCmd} --grep "${test_scope}" --reporter=line`; } else { - // Auto-detect from changed files const changedTests = changedFiles.filter(f => /\.(spec|test)\.(ts|tsx|js)$/.test(f)).slice(0, 5); if (changedTests.length > 0) { - testCmd = `${runnerCmd} ${changedTests.join(" ")} --reporter=line 2>&1 | tail -20`; + testCmd = `${runnerCmd} ${changedTests.join(" ")} --reporter=line`; } } } else if (runner === "vitest" || runner === "jest") { const runnerCmd = `${pm === "npx" ? "npx" : `${pm} exec`} ${runner}`; if (test_scope && test_scope !== "all") { - testCmd = `${runnerCmd} --run ${test_scope} 2>&1 | tail -20`; + testCmd = `${runnerCmd} --run ${test_scope}`; } else { const changedTests = changedFiles.filter(f => /\.(spec|test)\.(ts|tsx|js)$/.test(f)).slice(0, 5); if (changedTests.length > 0) { - testCmd = `${runnerCmd} --run ${changedTests.join(" ")} 2>&1 | tail -20`; + testCmd = `${runnerCmd} --run ${changedTests.join(" ")}`; } } } else if (test_scope) { - // No recognized runner but scope given — try npm test - testCmd = `${pm} test 2>&1 | tail -20`; + testCmd = `${pm} test`; } if (testCmd) { - const testResult = run(testCmd, { timeout: 120000 }); + const fullOutput = shell(testCmd, { timeout: 120000 }); + const testResult = fullOutput.split("\n").slice(-20).join("\n"); const testPassed = /pass/i.test(testResult) && !/fail/i.test(testResult); checks.push({ name: "Tests", @@ -130,7 +133,8 @@ export function registerVerifyCompletion(server: McpServer): void { // 4. Build check (only if build script exists and not skipped) if (!skip_build && hasBuildScript()) { - const buildCheck = run(`${pm === "npx" ? "npm run" : pm} build 2>&1 | tail -10`, { timeout: 60000 }); + const fullBuild = shell(`${pm === "npx" ? "npm run" : pm} build`, { timeout: 60000 }); + const buildCheck = fullBuild.split("\n").slice(-10).join("\n"); const buildPassed = !/\b[Ee]rror\b/.test(buildCheck) || /Successfully compiled/.test(buildCheck); checks.push({ name: "Build",