iosm-cli 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +6 -6
- package/dist/core/background-processes.d.ts.map +1 -1
- package/dist/core/background-processes.js +3 -2
- package/dist/core/background-processes.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +3 -2
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +5 -2
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/utils/shell.d.ts +8 -0
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +45 -0
- package/dist/utils/shell.js.map +1 -1
- package/docs/configuration.md +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.3.4] - 2026-04-06
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- **Windows shell command compatibility** — added runtime auto-adaptation for Windows command syntax, routing cmd-style commands (`%VAR%`, `dir`, `C:\...`) to `cmd.exe` and PowerShell-style commands (`$env:...`, `Get-*`) to `powershell.exe`
|
|
15
|
+
- **Telegram bridge command execution on Windows** — fixed failures when users sent native Windows shell commands through Telegram-driven runs while IOSM executed under bash
|
|
16
|
+
- **Execution path consistency** — applied shell adaptation uniformly across foreground bash execution, bash tool execution, and detached background processes
|
|
17
|
+
|
|
18
|
+
### Tests
|
|
19
|
+
|
|
20
|
+
- Added regression suite for Windows command adaptation and explicit-shell passthrough behavior
|
|
21
|
+
|
|
10
22
|
## [0.3.3] - 2026-04-05
|
|
11
23
|
|
|
12
24
|
### Fixed
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
|
-
<h1>IOSM CLI 0.3.
|
|
3
|
+
<h1>IOSM CLI 0.3.4</h1>
|
|
4
4
|
|
|
5
5
|
<p><strong>Terminal-native AI runtime for controlled, measurable engineering work on real codebases.</strong></p>
|
|
6
6
|
|
|
@@ -32,12 +32,12 @@ It is not a chat interface. It is a runtime.
|
|
|
32
32
|
|
|
33
33
|
---
|
|
34
34
|
|
|
35
|
-
## ✦ What's New in 0.3.
|
|
35
|
+
## ✦ What's New in 0.3.4
|
|
36
36
|
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
- Added regression tests for
|
|
37
|
+
- Added Windows shell command auto-adapter in runtime execution paths: cmd-style commands (`%VAR%`, `dir`, `C:\...`) and PowerShell-style commands (`$env:...`, `Get-*`) are now auto-routed to the correct interpreter even when IOSM runs via bash
|
|
38
|
+
- Fixed Telegram/Windows remote execution friction where native Windows command syntax failed in bridge-driven shell calls
|
|
39
|
+
- Applied adapter behavior consistently for foreground bash execution, bash tool execution, and detached background processes
|
|
40
|
+
- Added regression tests for Windows command adaptation behavior
|
|
41
41
|
|
|
42
42
|
## ✦ Major Additions in 0.2.16
|
|
43
43
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"background-processes.d.ts","sourceRoot":"","sources":["../../src/core/background-processes.ts"],"names":[],"mappings":"AAkBA,MAAM,MAAM,uBAAuB,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,YAAY,GAAG,SAAS,CAAC;AAiB9F,MAAM,WAAW,uBAAuB;IACvC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IAChC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,uBAAuB,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,2BAA2B;IAC3C,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;CAChC;AAED,MAAM,WAAW,8BAA8B;IAC9C,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;CACvB;AA2KD,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,2BAA2B,GAAG,uBAAuB,
|
|
1
|
+
{"version":3,"file":"background-processes.d.ts","sourceRoot":"","sources":["../../src/core/background-processes.ts"],"names":[],"mappings":"AAkBA,MAAM,MAAM,uBAAuB,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,YAAY,GAAG,SAAS,CAAC;AAiB9F,MAAM,WAAW,uBAAuB;IACvC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IAChC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAA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AA8B,CAuDhC"}
|
|
@@ -2,7 +2,7 @@ import { randomBytes } from "node:crypto";
|
|
|
2
2
|
import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
3
3
|
import { join, resolve } from "node:path";
|
|
4
4
|
import { spawn, spawnSync } from "child_process";
|
|
5
|
-
import { getShellConfig, getShellEnv, killProcessTree } from "../utils/shell.js";
|
|
5
|
+
import { adaptCommandForShell, getShellConfig, getShellEnv, killProcessTree } from "../utils/shell.js";
|
|
6
6
|
const BACKGROUND_DIR_SEGMENTS = [".iosm", "background", "processes"];
|
|
7
7
|
const MAX_LOG_TAIL_BYTES = 256 * 1024;
|
|
8
8
|
const BACKGROUND_STOP_FORCE_KILL_DELAY_MS = 1500;
|
|
@@ -187,9 +187,10 @@ export function startBackgroundProcess(input) {
|
|
|
187
187
|
const metaPath = join(processesDir, `${id}.json`);
|
|
188
188
|
const now = new Date().toISOString();
|
|
189
189
|
const { shell, args } = getShellConfig();
|
|
190
|
+
const adaptedCommand = adaptCommandForShell(input.command);
|
|
190
191
|
const wrappedCommand = [
|
|
191
192
|
"(",
|
|
192
|
-
|
|
193
|
+
adaptedCommand,
|
|
193
194
|
")",
|
|
194
195
|
"__iosm_bg_exit_code=$?",
|
|
195
196
|
`__iosm_bg_finished_at=$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || node -e 'console.log(new Date().toISOString())')`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"background-processes.js","sourceRoot":"","sources":["../../src/core/background-processes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACN,SAAS,EACT,UAAU,EACV,SAAS,EACT,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,MAAM,EACN,QAAQ,EACR,aAAa,GACb,MAAM,SAAS,CAAC;AACjB,OAAO,EA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V,CAAC;QAED,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACjD,IAAI,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,GAAG,GAAG,WAAW,GAAG,WAAW,EAAE,CAAC;YACrE,MAAM,CAAC,aAAa,IAAI,CAAC,CAAC;YAC1B,SAAS;QACV,CAAC;QAED,MAAM,aAAa,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,cAAc,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC;QACpG,KAAK,MAAM,QAAQ,IAAI,aAAa,EAAE,CAAC;YACtC,IAAI,CAAC;gBACJ,MAAM,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YACnC,CAAC;YAAC,MAAM,CAAC;gBACR,iDAAiD;YAClD,CAAC;QACF,CAAC;QACD,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;QACpB,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport {\n\tcloseSync,\n\texistsSync,\n\tfstatSync,\n\tmkdirSync,\n\topenSync,\n\treadFileSync,\n\treadSync,\n\treaddirSync,\n\trmSync,\n\tstatSync,\n\twriteFileSync,\n} from \"node:fs\";\nimport { join, resolve } from \"node:path\";\nimport { spawn, spawnSync } from \"child_process\";\nimport { getShellConfig, getShellEnv, killProcessTree } from \"../utils/shell.js\";\n\nexport type BackgroundProcessStatus = \"running\" | \"done\" | \"error\" | \"terminated\" | \"unknown\";\n\ninterface BackgroundProcessMeta {\n\tid: string;\n\tcommand: string;\n\tcwd: string;\n\trootCwd: string;\n\tpid: number;\n\tcreatedAt: string;\n\tstartedAt: string;\n\tsource?: \"interactive\" | \"tool\";\n\trequestedStopAt?: string;\n\tlogPath: string;\n\tfinishedAtPath: string;\n\texitCodePath: string;\n}\n\nexport interface BackgroundProcessRecord {\n\tid: string;\n\tcommand: string;\n\tcwd: string;\n\trootCwd: string;\n\tpid: number;\n\tcreatedAt: string;\n\tstartedAt: string;\n\tsource?: \"interactive\" | \"tool\";\n\trequestedStopAt?: string;\n\tlogPath: string;\n\tfinishedAtPath: string;\n\texitCodePath: string;\n\tmetaPath: string;\n\tstatus: BackgroundProcessStatus;\n\tfinishedAt?: string;\n\texitCode?: number;\n}\n\nexport interface StartBackgroundProcessInput {\n\trootCwd: string;\n\tcommand: string;\n\tcwd?: string;\n\tsource?: \"interactive\" | \"tool\";\n}\n\nexport interface PruneBackgroundProcessesResult {\n\tremoved: number;\n\tremovedIds: string[];\n\tskippedRunning: number;\n\tskippedRecent: number;\n\tthresholdHours: number;\n}\n\nconst BACKGROUND_DIR_SEGMENTS = [\".iosm\", \"background\", \"processes\"] as const;\nconst MAX_LOG_TAIL_BYTES = 256 * 1024;\nconst BACKGROUND_STOP_FORCE_KILL_DELAY_MS = 1500;\nconst BACKGROUND_STOP_FALLBACK_EXIT_CODE = 143;\n\nfunction getProcessesDir(rootCwd: string): string {\n\treturn join(rootCwd, ...BACKGROUND_DIR_SEGMENTS);\n}\n\nfunction shellQuote(value: string): string {\n\treturn `'${value.replace(/'/g, `'\\\"'\\\"'`)}'`;\n}\n\nfunction isProcessAlive(pid: number): boolean {\n\ttry {\n\t\tprocess.kill(pid, 0);\n\t\treturn true;\n\t} catch (error) {\n\t\tif ((error as NodeJS.ErrnoException | undefined)?.code === \"EPERM\") return true;\n\t\treturn false;\n\t}\n}\n\nfunction parseIntegerFile(path: string): number | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\tconst raw = readFileSync(path, \"utf8\").trim();\n\t\tif (!raw) return undefined;\n\t\tconst value = Number.parseInt(raw, 10);\n\t\treturn Number.isFinite(value) ? value : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction parseTextFile(path: string): string | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\tconst raw = readFileSync(path, \"utf8\").trim();\n\t\treturn raw || undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction writeCompletionArtifacts(meta: BackgroundProcessMeta, finishedAt: string, exitCode: number): void {\n\twriteFileSync(meta.finishedAtPath, `${finishedAt}\\n`, \"utf8\");\n\twriteFileSync(meta.exitCodePath, `${exitCode}\\n`, \"utf8\");\n}\n\nfunction signalProcessTree(pid: number, signal: \"SIGTERM\" | \"SIGKILL\"): void {\n\tif (process.platform === \"win32\") {\n\t\t// /F is forced kill; omit it for soft terminate attempt.\n\t\tconst args = signal === \"SIGKILL\" ? [\"/F\", \"/T\", \"/PID\", String(pid)] : [\"/T\", \"/PID\", String(pid)];\n\t\ttry {\n\t\t\tspawnSync(\"taskkill\", args, { stdio: \"ignore\", timeout: 5000 });\n\t\t} catch {\n\t\t\t// Ignore termination errors (process may already be gone).\n\t\t}\n\t\treturn;\n\t}\n\n\ttry {\n\t\tprocess.kill(-pid, signal);\n\t} catch {\n\t\ttry {\n\t\t\tprocess.kill(pid, signal);\n\t\t} catch {\n\t\t\t// Process already dead.\n\t\t}\n\t}\n}\n\nfunction scheduleForceKill(pid: number): void {\n\tconst timer = setTimeout(() => {\n\t\tif (!isProcessAlive(pid)) return;\n\t\tkillProcessTree(pid);\n\t}, BACKGROUND_STOP_FORCE_KILL_DELAY_MS);\n\ttimer.unref();\n}\n\nfunction toRecord(meta: BackgroundProcessMeta, metaPath: string): BackgroundProcessRecord {\n\tconst finishedAt = parseTextFile(meta.finishedAtPath);\n\tconst exitCode = parseIntegerFile(meta.exitCodePath);\n\tconst alive = isProcessAlive(meta.pid);\n\tlet status: BackgroundProcessStatus = \"unknown\";\n\tif (!alive && meta.requestedStopAt) {\n\t\tstatus = \"terminated\";\n\t} else if (alive) {\n\t\tstatus = \"running\";\n\t} else if (finishedAt && typeof exitCode === \"number\") {\n\t\tstatus = exitCode === 0 ? \"done\" : \"error\";\n\t}\n\n\treturn {\n\t\tid: meta.id,\n\t\tcommand: meta.command,\n\t\tcwd: meta.cwd,\n\t\trootCwd: meta.rootCwd,\n\t\tpid: meta.pid,\n\t\tcreatedAt: meta.createdAt,\n\t\tstartedAt: meta.startedAt,\n\t\tsource: meta.source,\n\t\trequestedStopAt: meta.requestedStopAt,\n\t\tlogPath: meta.logPath,\n\t\tfinishedAtPath: meta.finishedAtPath,\n\t\texitCodePath: meta.exitCodePath,\n\t\tmetaPath,\n\t\tstatus,\n\t\tfinishedAt,\n\t\texitCode,\n\t};\n}\n\nfunction loadMeta(metaPath: string): BackgroundProcessMeta | undefined {\n\ttry {\n\t\tconst parsed = JSON.parse(readFileSync(metaPath, \"utf8\")) as Partial<BackgroundProcessMeta>;\n\t\tif (\n\t\t\ttypeof parsed.id !== \"string\" ||\n\t\t\ttypeof parsed.command !== \"string\" ||\n\t\t\ttypeof parsed.cwd !== \"string\" ||\n\t\t\ttypeof parsed.rootCwd !== \"string\" ||\n\t\t\ttypeof parsed.pid !== \"number\" ||\n\t\t\ttypeof parsed.createdAt !== \"string\" ||\n\t\t\ttypeof parsed.startedAt !== \"string\" ||\n\t\t\ttypeof parsed.logPath !== \"string\" ||\n\t\t\ttypeof parsed.finishedAtPath !== \"string\" ||\n\t\t\ttypeof parsed.exitCodePath !== \"string\"\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn {\n\t\t\tid: parsed.id,\n\t\t\tcommand: parsed.command,\n\t\t\tcwd: parsed.cwd,\n\t\t\trootCwd: parsed.rootCwd,\n\t\t\tpid: parsed.pid,\n\t\t\tcreatedAt: parsed.createdAt,\n\t\t\tstartedAt: parsed.startedAt,\n\t\t\tsource: parsed.source,\n\t\t\trequestedStopAt: parsed.requestedStopAt,\n\t\t\tlogPath: parsed.logPath,\n\t\t\tfinishedAtPath: parsed.finishedAtPath,\n\t\t\texitCodePath: parsed.exitCodePath,\n\t\t};\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction saveMeta(metaPath: string, meta: BackgroundProcessMeta): void {\n\twriteFileSync(metaPath, `${JSON.stringify(meta, null, 2)}\\n`, \"utf8\");\n}\n\nfunction readFileTailUtf8(path: string, maxBytes: number): string {\n\tif (!existsSync(path)) return \"\";\n\tconst fd = openSync(path, \"r\");\n\ttry {\n\t\tconst size = fstatSync(fd).size;\n\t\tif (size <= 0) return \"\";\n\t\tconst readSize = Math.min(size, maxBytes);\n\t\tconst buffer = Buffer.alloc(readSize);\n\t\treadSync(fd, buffer, 0, readSize, size - readSize);\n\t\treturn buffer.toString(\"utf8\");\n\t} finally {\n\t\tcloseSync(fd);\n\t}\n}\n\nexport function startBackgroundProcess(input: StartBackgroundProcessInput): BackgroundProcessRecord {\n\tconst rootCwd = resolve(input.rootCwd);\n\tconst processCwd = resolve(input.cwd ?? rootCwd);\n\tif (!existsSync(processCwd) || !statSync(processCwd).isDirectory()) {\n\t\tthrow new Error(`Background process cwd does not exist: ${processCwd}`);\n\t}\n\n\tconst processesDir = getProcessesDir(rootCwd);\n\tmkdirSync(processesDir, { recursive: true });\n\n\tconst id = `bg_${Date.now()}_${randomBytes(4).toString(\"hex\")}`;\n\tconst logPath = join(processesDir, `${id}.log`);\n\tconst finishedAtPath = join(processesDir, `${id}.finished`);\n\tconst exitCodePath = join(processesDir, `${id}.exitcode`);\n\tconst metaPath = join(processesDir, `${id}.json`);\n\tconst now = new Date().toISOString();\n\n\tconst { shell, args } = getShellConfig();\n\tconst wrappedCommand = [\n\t\t\"(\",\n\t\tinput.command,\n\t\t\")\",\n\t\t\"__iosm_bg_exit_code=$?\",\n\t\t`__iosm_bg_finished_at=$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || node -e 'console.log(new Date().toISOString())')`,\n\t\t`printf \"%s\\\\n\" \"$__iosm_bg_finished_at\" > ${shellQuote(finishedAtPath)}`,\n\t\t`printf \"%s\\\\n\" \"$__iosm_bg_exit_code\" > ${shellQuote(exitCodePath)}`,\n\t\t'exit \"$__iosm_bg_exit_code\"',\n\t].join(\"\\n\");\n\n\tconst logFd = openSync(logPath, \"a\");\n\tconst child = spawn(shell, [...args, wrappedCommand], {\n\t\tcwd: processCwd,\n\t\tdetached: true,\n\t\tenv: getShellEnv(),\n\t\tstdio: [\"ignore\", logFd, logFd],\n\t});\n\tcloseSync(logFd);\n\n\tif (!child.pid) {\n\t\tthrow new Error(\"Failed to start background process: missing pid.\");\n\t}\n\n\tchild.unref();\n\n\tconst meta: BackgroundProcessMeta = {\n\t\tid,\n\t\tcommand: input.command,\n\t\tcwd: processCwd,\n\t\trootCwd,\n\t\tpid: child.pid,\n\t\tcreatedAt: now,\n\t\tstartedAt: now,\n\t\tsource: input.source,\n\t\tlogPath,\n\t\tfinishedAtPath,\n\t\texitCodePath,\n\t};\n\tsaveMeta(metaPath, meta);\n\treturn toRecord(meta, metaPath);\n}\n\nexport function listBackgroundProcesses(rootCwd: string, limit = 20): BackgroundProcessRecord[] {\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tif (!existsSync(dir)) return [];\n\tconst maxItems = Math.max(1, Math.min(200, Math.floor(limit)));\n\tconst files = readdirSync(dir)\n\t\t.filter((name) => name.endsWith(\".json\"))\n\t\t.map((name) => join(dir, name))\n\t\t.sort((a, b) => b.localeCompare(a))\n\t\t.slice(0, maxItems);\n\n\tconst records: BackgroundProcessRecord[] = [];\n\tfor (const metaPath of files) {\n\t\tconst meta = loadMeta(metaPath);\n\t\tif (!meta) continue;\n\t\trecords.push(toRecord(meta, metaPath));\n\t}\n\treturn records;\n}\n\nexport function getBackgroundProcess(rootCwd: string, id: string): BackgroundProcessRecord | undefined {\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tconst metaPath = join(dir, `${id}.json`);\n\tif (!existsSync(metaPath)) return undefined;\n\tconst meta = loadMeta(metaPath);\n\tif (!meta) return undefined;\n\treturn toRecord(meta, metaPath);\n}\n\nexport function stopBackgroundProcess(rootCwd: string, id: string): BackgroundProcessRecord | undefined {\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tconst metaPath = join(dir, `${id}.json`);\n\tconst meta = loadMeta(metaPath);\n\tif (!meta) return undefined;\n\n\tconst wasAlive = isProcessAlive(meta.pid);\n\tif (!wasAlive) {\n\t\treturn toRecord(meta, metaPath);\n\t}\n\n\tmeta.requestedStopAt = new Date().toISOString();\n\tsaveMeta(metaPath, meta);\n\n\t// Try graceful shutdown first and then enforce a hard kill in the background if needed.\n\tsignalProcessTree(meta.pid, \"SIGTERM\");\n\tscheduleForceKill(meta.pid);\n\n\tif (isProcessAlive(meta.pid)) {\n\t\treturn toRecord(meta, metaPath);\n\t}\n\n\t// Process already stopped synchronously; write fallback completion markers if shell did not flush them.\n\tconst hasFinishedAt = existsSync(meta.finishedAtPath);\n\tconst hasExitCode = existsSync(meta.exitCodePath);\n\tif (!hasFinishedAt || !hasExitCode) {\n\t\twriteCompletionArtifacts(meta, new Date().toISOString(), BACKGROUND_STOP_FALLBACK_EXIT_CODE);\n\t}\n\n\treturn toRecord(meta, metaPath);\n}\n\nexport function readBackgroundProcessLogTail(rootCwd: string, id: string, lines = 80): string | undefined {\n\tconst record = getBackgroundProcess(rootCwd, id);\n\tif (!record) return undefined;\n\tconst maxLines = Math.max(1, Math.min(1000, Math.floor(lines)));\n\tconst tail = readFileTailUtf8(record.logPath, MAX_LOG_TAIL_BYTES);\n\tif (!tail) return \"\";\n\tconst chunks = tail.split(/\\r?\\n/);\n\tconst sliced = chunks.slice(-maxLines);\n\treturn sliced.join(\"\\n\").trimEnd();\n}\n\nexport function pruneBackgroundProcesses(\n\trootCwd: string,\n\toptions?: {\n\t\tmaxAgeHours?: number;\n\t\tlimit?: number;\n\t},\n): PruneBackgroundProcessesResult {\n\tconst thresholdHoursRaw = options?.maxAgeHours;\n\tconst thresholdHours =\n\t\ttypeof thresholdHoursRaw === \"number\" && Number.isFinite(thresholdHoursRaw)\n\t\t\t? Math.max(1, Math.floor(thresholdHoursRaw))\n\t\t\t: 168;\n\tconst limitRaw = options?.limit;\n\tconst limit = typeof limitRaw === \"number\" && Number.isFinite(limitRaw) ? Math.max(1, Math.floor(limitRaw)) : 1000;\n\tconst thresholdMs = thresholdHours * 60 * 60 * 1000;\n\tconst now = Date.now();\n\tconst result: PruneBackgroundProcessesResult = {\n\t\tremoved: 0,\n\t\tremovedIds: [],\n\t\tskippedRunning: 0,\n\t\tskippedRecent: 0,\n\t\tthresholdHours,\n\t};\n\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tif (!existsSync(dir)) return result;\n\n\tconst metaPaths = readdirSync(dir)\n\t\t.filter((name) => name.endsWith(\".json\"))\n\t\t.map((name) => join(dir, name))\n\t\t.sort((a, b) => b.localeCompare(a))\n\t\t.slice(0, limit);\n\n\tfor (const metaPath of metaPaths) {\n\t\tconst meta = loadMeta(metaPath);\n\t\tif (!meta) continue;\n\t\tconst record = toRecord(meta, metaPath);\n\t\tif (record.status === \"running\") {\n\t\t\tresult.skippedRunning += 1;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst createdAtMs = Date.parse(record.createdAt);\n\t\tif (Number.isFinite(createdAtMs) && now - createdAtMs < thresholdMs) {\n\t\t\tresult.skippedRecent += 1;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst filesToDelete = [record.metaPath, record.logPath, record.finishedAtPath, record.exitCodePath];\n\t\tfor (const filePath of filesToDelete) {\n\t\t\ttry {\n\t\t\t\trmSync(filePath, { force: true });\n\t\t\t} catch {\n\t\t\t\t// Ignore cleanup errors for best-effort pruning.\n\t\t\t}\n\t\t}\n\t\tresult.removed += 1;\n\t\tresult.removedIds.push(record.id);\n\t}\n\n\treturn result;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"background-processes.js","sourceRoot":"","sources":["../../src/core/background-processes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACN,SAAS,EACT,UAAU,EACV,SAAS,EAC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IAAI;YAAE,SAAS;QACpB,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACxC,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACjC,MAAM,CAAC,cAAc,IAAI,CAAC,CAAC;YAC3B,SAAS;QACV,CAAC;QAED,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACjD,IAAI,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,GAAG,GAAG,WAAW,GAAG,WAAW,EAAE,CAAC;YACrE,MAAM,CAAC,aAAa,IAAI,CAAC,CAAC;YAC1B,SAAS;QACV,CAAC;QAED,MAAM,aAAa,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,cAAc,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC;QACpG,KAAK,MAAM,QAAQ,IAAI,aAAa,EAAE,CAAC;YACtC,IAAI,CAAC;gBACJ,MAAM,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YACnC,CAAC;YAAC,MAAM,CAAC;gBACR,iDAAiD;YAClD,CAAC;QACF,CAAC;QACD,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;QACpB,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport {\n\tcloseSync,\n\texistsSync,\n\tfstatSync,\n\tmkdirSync,\n\topenSync,\n\treadFileSync,\n\treadSync,\n\treaddirSync,\n\trmSync,\n\tstatSync,\n\twriteFileSync,\n} from \"node:fs\";\nimport { join, resolve } from \"node:path\";\nimport { spawn, spawnSync } from \"child_process\";\nimport { adaptCommandForShell, getShellConfig, getShellEnv, killProcessTree } from \"../utils/shell.js\";\n\nexport type BackgroundProcessStatus = \"running\" | \"done\" | \"error\" | \"terminated\" | \"unknown\";\n\ninterface BackgroundProcessMeta {\n\tid: string;\n\tcommand: string;\n\tcwd: string;\n\trootCwd: string;\n\tpid: number;\n\tcreatedAt: string;\n\tstartedAt: string;\n\tsource?: \"interactive\" | \"tool\";\n\trequestedStopAt?: string;\n\tlogPath: string;\n\tfinishedAtPath: string;\n\texitCodePath: string;\n}\n\nexport interface BackgroundProcessRecord {\n\tid: string;\n\tcommand: string;\n\tcwd: string;\n\trootCwd: string;\n\tpid: number;\n\tcreatedAt: string;\n\tstartedAt: string;\n\tsource?: \"interactive\" | \"tool\";\n\trequestedStopAt?: string;\n\tlogPath: string;\n\tfinishedAtPath: string;\n\texitCodePath: string;\n\tmetaPath: string;\n\tstatus: BackgroundProcessStatus;\n\tfinishedAt?: string;\n\texitCode?: number;\n}\n\nexport interface StartBackgroundProcessInput {\n\trootCwd: string;\n\tcommand: string;\n\tcwd?: string;\n\tsource?: \"interactive\" | \"tool\";\n}\n\nexport interface PruneBackgroundProcessesResult {\n\tremoved: number;\n\tremovedIds: string[];\n\tskippedRunning: number;\n\tskippedRecent: number;\n\tthresholdHours: number;\n}\n\nconst BACKGROUND_DIR_SEGMENTS = [\".iosm\", \"background\", \"processes\"] as const;\nconst MAX_LOG_TAIL_BYTES = 256 * 1024;\nconst BACKGROUND_STOP_FORCE_KILL_DELAY_MS = 1500;\nconst BACKGROUND_STOP_FALLBACK_EXIT_CODE = 143;\n\nfunction getProcessesDir(rootCwd: string): string {\n\treturn join(rootCwd, ...BACKGROUND_DIR_SEGMENTS);\n}\n\nfunction shellQuote(value: string): string {\n\treturn `'${value.replace(/'/g, `'\\\"'\\\"'`)}'`;\n}\n\nfunction isProcessAlive(pid: number): boolean {\n\ttry {\n\t\tprocess.kill(pid, 0);\n\t\treturn true;\n\t} catch (error) {\n\t\tif ((error as NodeJS.ErrnoException | undefined)?.code === \"EPERM\") return true;\n\t\treturn false;\n\t}\n}\n\nfunction parseIntegerFile(path: string): number | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\tconst raw = readFileSync(path, \"utf8\").trim();\n\t\tif (!raw) return undefined;\n\t\tconst value = Number.parseInt(raw, 10);\n\t\treturn Number.isFinite(value) ? value : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction parseTextFile(path: string): string | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\tconst raw = readFileSync(path, \"utf8\").trim();\n\t\treturn raw || undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction writeCompletionArtifacts(meta: BackgroundProcessMeta, finishedAt: string, exitCode: number): void {\n\twriteFileSync(meta.finishedAtPath, `${finishedAt}\\n`, \"utf8\");\n\twriteFileSync(meta.exitCodePath, `${exitCode}\\n`, \"utf8\");\n}\n\nfunction signalProcessTree(pid: number, signal: \"SIGTERM\" | \"SIGKILL\"): void {\n\tif (process.platform === \"win32\") {\n\t\t// /F is forced kill; omit it for soft terminate attempt.\n\t\tconst args = signal === \"SIGKILL\" ? [\"/F\", \"/T\", \"/PID\", String(pid)] : [\"/T\", \"/PID\", String(pid)];\n\t\ttry {\n\t\t\tspawnSync(\"taskkill\", args, { stdio: \"ignore\", timeout: 5000 });\n\t\t} catch {\n\t\t\t// Ignore termination errors (process may already be gone).\n\t\t}\n\t\treturn;\n\t}\n\n\ttry {\n\t\tprocess.kill(-pid, signal);\n\t} catch {\n\t\ttry {\n\t\t\tprocess.kill(pid, signal);\n\t\t} catch {\n\t\t\t// Process already dead.\n\t\t}\n\t}\n}\n\nfunction scheduleForceKill(pid: number): void {\n\tconst timer = setTimeout(() => {\n\t\tif (!isProcessAlive(pid)) return;\n\t\tkillProcessTree(pid);\n\t}, BACKGROUND_STOP_FORCE_KILL_DELAY_MS);\n\ttimer.unref();\n}\n\nfunction toRecord(meta: BackgroundProcessMeta, metaPath: string): BackgroundProcessRecord {\n\tconst finishedAt = parseTextFile(meta.finishedAtPath);\n\tconst exitCode = parseIntegerFile(meta.exitCodePath);\n\tconst alive = isProcessAlive(meta.pid);\n\tlet status: BackgroundProcessStatus = \"unknown\";\n\tif (!alive && meta.requestedStopAt) {\n\t\tstatus = \"terminated\";\n\t} else if (alive) {\n\t\tstatus = \"running\";\n\t} else if (finishedAt && typeof exitCode === \"number\") {\n\t\tstatus = exitCode === 0 ? \"done\" : \"error\";\n\t}\n\n\treturn {\n\t\tid: meta.id,\n\t\tcommand: meta.command,\n\t\tcwd: meta.cwd,\n\t\trootCwd: meta.rootCwd,\n\t\tpid: meta.pid,\n\t\tcreatedAt: meta.createdAt,\n\t\tstartedAt: meta.startedAt,\n\t\tsource: meta.source,\n\t\trequestedStopAt: meta.requestedStopAt,\n\t\tlogPath: meta.logPath,\n\t\tfinishedAtPath: meta.finishedAtPath,\n\t\texitCodePath: meta.exitCodePath,\n\t\tmetaPath,\n\t\tstatus,\n\t\tfinishedAt,\n\t\texitCode,\n\t};\n}\n\nfunction loadMeta(metaPath: string): BackgroundProcessMeta | undefined {\n\ttry {\n\t\tconst parsed = JSON.parse(readFileSync(metaPath, \"utf8\")) as Partial<BackgroundProcessMeta>;\n\t\tif (\n\t\t\ttypeof parsed.id !== \"string\" ||\n\t\t\ttypeof parsed.command !== \"string\" ||\n\t\t\ttypeof parsed.cwd !== \"string\" ||\n\t\t\ttypeof parsed.rootCwd !== \"string\" ||\n\t\t\ttypeof parsed.pid !== \"number\" ||\n\t\t\ttypeof parsed.createdAt !== \"string\" ||\n\t\t\ttypeof parsed.startedAt !== \"string\" ||\n\t\t\ttypeof parsed.logPath !== \"string\" ||\n\t\t\ttypeof parsed.finishedAtPath !== \"string\" ||\n\t\t\ttypeof parsed.exitCodePath !== \"string\"\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn {\n\t\t\tid: parsed.id,\n\t\t\tcommand: parsed.command,\n\t\t\tcwd: parsed.cwd,\n\t\t\trootCwd: parsed.rootCwd,\n\t\t\tpid: parsed.pid,\n\t\t\tcreatedAt: parsed.createdAt,\n\t\t\tstartedAt: parsed.startedAt,\n\t\t\tsource: parsed.source,\n\t\t\trequestedStopAt: parsed.requestedStopAt,\n\t\t\tlogPath: parsed.logPath,\n\t\t\tfinishedAtPath: parsed.finishedAtPath,\n\t\t\texitCodePath: parsed.exitCodePath,\n\t\t};\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction saveMeta(metaPath: string, meta: BackgroundProcessMeta): void {\n\twriteFileSync(metaPath, `${JSON.stringify(meta, null, 2)}\\n`, \"utf8\");\n}\n\nfunction readFileTailUtf8(path: string, maxBytes: number): string {\n\tif (!existsSync(path)) return \"\";\n\tconst fd = openSync(path, \"r\");\n\ttry {\n\t\tconst size = fstatSync(fd).size;\n\t\tif (size <= 0) return \"\";\n\t\tconst readSize = Math.min(size, maxBytes);\n\t\tconst buffer = Buffer.alloc(readSize);\n\t\treadSync(fd, buffer, 0, readSize, size - readSize);\n\t\treturn buffer.toString(\"utf8\");\n\t} finally {\n\t\tcloseSync(fd);\n\t}\n}\n\nexport function startBackgroundProcess(input: StartBackgroundProcessInput): BackgroundProcessRecord {\n\tconst rootCwd = resolve(input.rootCwd);\n\tconst processCwd = resolve(input.cwd ?? rootCwd);\n\tif (!existsSync(processCwd) || !statSync(processCwd).isDirectory()) {\n\t\tthrow new Error(`Background process cwd does not exist: ${processCwd}`);\n\t}\n\n\tconst processesDir = getProcessesDir(rootCwd);\n\tmkdirSync(processesDir, { recursive: true });\n\n\tconst id = `bg_${Date.now()}_${randomBytes(4).toString(\"hex\")}`;\n\tconst logPath = join(processesDir, `${id}.log`);\n\tconst finishedAtPath = join(processesDir, `${id}.finished`);\n\tconst exitCodePath = join(processesDir, `${id}.exitcode`);\n\tconst metaPath = join(processesDir, `${id}.json`);\n\tconst now = new Date().toISOString();\n\n\tconst { shell, args } = getShellConfig();\n\tconst adaptedCommand = adaptCommandForShell(input.command);\n\tconst wrappedCommand = [\n\t\t\"(\",\n\t\tadaptedCommand,\n\t\t\")\",\n\t\t\"__iosm_bg_exit_code=$?\",\n\t\t`__iosm_bg_finished_at=$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || node -e 'console.log(new Date().toISOString())')`,\n\t\t`printf \"%s\\\\n\" \"$__iosm_bg_finished_at\" > ${shellQuote(finishedAtPath)}`,\n\t\t`printf \"%s\\\\n\" \"$__iosm_bg_exit_code\" > ${shellQuote(exitCodePath)}`,\n\t\t'exit \"$__iosm_bg_exit_code\"',\n\t].join(\"\\n\");\n\n\tconst logFd = openSync(logPath, \"a\");\n\tconst child = spawn(shell, [...args, wrappedCommand], {\n\t\tcwd: processCwd,\n\t\tdetached: true,\n\t\tenv: getShellEnv(),\n\t\tstdio: [\"ignore\", logFd, logFd],\n\t});\n\tcloseSync(logFd);\n\n\tif (!child.pid) {\n\t\tthrow new Error(\"Failed to start background process: missing pid.\");\n\t}\n\n\tchild.unref();\n\n\tconst meta: BackgroundProcessMeta = {\n\t\tid,\n\t\tcommand: input.command,\n\t\tcwd: processCwd,\n\t\trootCwd,\n\t\tpid: child.pid,\n\t\tcreatedAt: now,\n\t\tstartedAt: now,\n\t\tsource: input.source,\n\t\tlogPath,\n\t\tfinishedAtPath,\n\t\texitCodePath,\n\t};\n\tsaveMeta(metaPath, meta);\n\treturn toRecord(meta, metaPath);\n}\n\nexport function listBackgroundProcesses(rootCwd: string, limit = 20): BackgroundProcessRecord[] {\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tif (!existsSync(dir)) return [];\n\tconst maxItems = Math.max(1, Math.min(200, Math.floor(limit)));\n\tconst files = readdirSync(dir)\n\t\t.filter((name) => name.endsWith(\".json\"))\n\t\t.map((name) => join(dir, name))\n\t\t.sort((a, b) => b.localeCompare(a))\n\t\t.slice(0, maxItems);\n\n\tconst records: BackgroundProcessRecord[] = [];\n\tfor (const metaPath of files) {\n\t\tconst meta = loadMeta(metaPath);\n\t\tif (!meta) continue;\n\t\trecords.push(toRecord(meta, metaPath));\n\t}\n\treturn records;\n}\n\nexport function getBackgroundProcess(rootCwd: string, id: string): BackgroundProcessRecord | undefined {\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tconst metaPath = join(dir, `${id}.json`);\n\tif (!existsSync(metaPath)) return undefined;\n\tconst meta = loadMeta(metaPath);\n\tif (!meta) return undefined;\n\treturn toRecord(meta, metaPath);\n}\n\nexport function stopBackgroundProcess(rootCwd: string, id: string): BackgroundProcessRecord | undefined {\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tconst metaPath = join(dir, `${id}.json`);\n\tconst meta = loadMeta(metaPath);\n\tif (!meta) return undefined;\n\n\tconst wasAlive = isProcessAlive(meta.pid);\n\tif (!wasAlive) {\n\t\treturn toRecord(meta, metaPath);\n\t}\n\n\tmeta.requestedStopAt = new Date().toISOString();\n\tsaveMeta(metaPath, meta);\n\n\t// Try graceful shutdown first and then enforce a hard kill in the background if needed.\n\tsignalProcessTree(meta.pid, \"SIGTERM\");\n\tscheduleForceKill(meta.pid);\n\n\tif (isProcessAlive(meta.pid)) {\n\t\treturn toRecord(meta, metaPath);\n\t}\n\n\t// Process already stopped synchronously; write fallback completion markers if shell did not flush them.\n\tconst hasFinishedAt = existsSync(meta.finishedAtPath);\n\tconst hasExitCode = existsSync(meta.exitCodePath);\n\tif (!hasFinishedAt || !hasExitCode) {\n\t\twriteCompletionArtifacts(meta, new Date().toISOString(), BACKGROUND_STOP_FALLBACK_EXIT_CODE);\n\t}\n\n\treturn toRecord(meta, metaPath);\n}\n\nexport function readBackgroundProcessLogTail(rootCwd: string, id: string, lines = 80): string | undefined {\n\tconst record = getBackgroundProcess(rootCwd, id);\n\tif (!record) return undefined;\n\tconst maxLines = Math.max(1, Math.min(1000, Math.floor(lines)));\n\tconst tail = readFileTailUtf8(record.logPath, MAX_LOG_TAIL_BYTES);\n\tif (!tail) return \"\";\n\tconst chunks = tail.split(/\\r?\\n/);\n\tconst sliced = chunks.slice(-maxLines);\n\treturn sliced.join(\"\\n\").trimEnd();\n}\n\nexport function pruneBackgroundProcesses(\n\trootCwd: string,\n\toptions?: {\n\t\tmaxAgeHours?: number;\n\t\tlimit?: number;\n\t},\n): PruneBackgroundProcessesResult {\n\tconst thresholdHoursRaw = options?.maxAgeHours;\n\tconst thresholdHours =\n\t\ttypeof thresholdHoursRaw === \"number\" && Number.isFinite(thresholdHoursRaw)\n\t\t\t? Math.max(1, Math.floor(thresholdHoursRaw))\n\t\t\t: 168;\n\tconst limitRaw = options?.limit;\n\tconst limit = typeof limitRaw === \"number\" && Number.isFinite(limitRaw) ? Math.max(1, Math.floor(limitRaw)) : 1000;\n\tconst thresholdMs = thresholdHours * 60 * 60 * 1000;\n\tconst now = Date.now();\n\tconst result: PruneBackgroundProcessesResult = {\n\t\tremoved: 0,\n\t\tremovedIds: [],\n\t\tskippedRunning: 0,\n\t\tskippedRecent: 0,\n\t\tthresholdHours,\n\t};\n\n\tconst dir = getProcessesDir(resolve(rootCwd));\n\tif (!existsSync(dir)) return result;\n\n\tconst metaPaths = readdirSync(dir)\n\t\t.filter((name) => name.endsWith(\".json\"))\n\t\t.map((name) => join(dir, name))\n\t\t.sort((a, b) => b.localeCompare(a))\n\t\t.slice(0, limit);\n\n\tfor (const metaPath of metaPaths) {\n\t\tconst meta = loadMeta(metaPath);\n\t\tif (!meta) continue;\n\t\tconst record = toRecord(meta, metaPath);\n\t\tif (record.status === \"running\") {\n\t\t\tresult.skippedRunning += 1;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst createdAtMs = Date.parse(record.createdAt);\n\t\tif (Number.isFinite(createdAtMs) && now - createdAtMs < thresholdMs) {\n\t\t\tresult.skippedRecent += 1;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst filesToDelete = [record.metaPath, record.logPath, record.finishedAtPath, record.exitCodePath];\n\t\tfor (const filePath of filesToDelete) {\n\t\t\ttry {\n\t\t\t\trmSync(filePath, { force: true });\n\t\t\t} catch {\n\t\t\t\t// Ignore cleanup errors for best-effort pruning.\n\t\t\t}\n\t\t}\n\t\tresult.removed += 1;\n\t\tresult.removedIds.push(record.id);\n\t}\n\n\treturn result;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bash-executor.d.ts","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AASH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAOtD,MAAM,WAAW,mBAAmB;IACnC,+DAA+D;IAC/D,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,mCAAmC;IACnC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,yEAAyE;IACzE,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,UAAU;IAC1B,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,mDAAmD;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,uCAAuC;IACvC,SAAS,EAAE,OAAO,CAAC;IACnB,yFAAyF;IACzF,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,8EAA8E;IAC9E,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,6DAA6D;IAC7D,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iDAAiD;IACjD,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,UAAU,CAAC,
|
|
1
|
+
{"version":3,"file":"bash-executor.d.ts","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AASH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAOtD,MAAM,WAAW,mBAAmB;IACnC,+DAA+D;IAC/D,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,mCAAmC;IACnC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,yEAAyE;IACzE,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,UAAU;IAC1B,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,mDAAmD;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,uCAAuC;IACvC,SAAS,EAAE,OAAO,CAAC;IACnB,yFAAyF;IACzF,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,8EAA8E;IAC9E,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,6DAA6D;IAC7D,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iDAAiD;IACjD,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,UAAU,CAAC,CA0H/F;AAED;;;GAGG;AACH,wBAAsB,yBAAyB,CAC9C,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,cAAc,EAC1B,OAAO,CAAC,EAAE,mBAAmB,GAC3B,OAAO,CAAC,UAAU,CAAC,CAsFrB"}
|
|
@@ -11,7 +11,7 @@ import { tmpdir } from "node:os";
|
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { spawn } from "child_process";
|
|
13
13
|
import stripAnsi from "strip-ansi";
|
|
14
|
-
import { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from "../utils/shell.js";
|
|
14
|
+
import { adaptCommandForShell, getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from "../utils/shell.js";
|
|
15
15
|
import { DEFAULT_MAX_BYTES, truncateTail } from "./tools/truncate.js";
|
|
16
16
|
// ============================================================================
|
|
17
17
|
// Implementation
|
|
@@ -33,7 +33,8 @@ import { DEFAULT_MAX_BYTES, truncateTail } from "./tools/truncate.js";
|
|
|
33
33
|
export function executeBash(command, options) {
|
|
34
34
|
return new Promise((resolve, reject) => {
|
|
35
35
|
const { shell, args } = getShellConfig();
|
|
36
|
-
const
|
|
36
|
+
const adaptedCommand = adaptCommandForShell(command);
|
|
37
|
+
const child = spawn(shell, [...args, adaptedCommand], {
|
|
37
38
|
cwd: options?.cwd,
|
|
38
39
|
detached: true,
|
|
39
40
|
env: getShellEnv(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bash-executor.js","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAoB,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAqB,KAAK,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEvG,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAkCtE,+EAA+E;AAC/E,iBAAiB;AACjB,+EAA+E;AAE/E;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,WAAW,CAAC,OAAe,EAAE,OAA6B;IACzE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,cAAc,EAAE,CAAC;QACz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CAAC;QACtB,CAAC;QAED,2BAA2B;QAC3B,IAAI,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;YAC9B,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACzC,MAAM,gBAAgB,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC;YAClD,OAAO;gBACN,MAAM,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU;gBAC1E,QAAQ,EAAE,SAAS;gBACnB,SAAS,EAAE,IAAI;gBACf,SAAS,EAAE,gBAAgB,CAAC,SAAS;gBACrC,cAAc,EAAE,YAAY;aAC5B,CAAC;QACH,CAAC;QAED,MAAM,GAAG,CAAC;IACX,CAAC;AACF,CAAC","sourcesContent":["/**\n * Bash command execution with streaming support and cancellation.\n *\n * This module provides a unified bash execution implementation used by:\n * - AgentSession.executeBash() for interactive and RPC modes\n * - Direct calls from modes that need bash execution\n */\n\nimport { randomBytes } from \"node:crypto\";\nimport { createWriteStream, type WriteStream } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { type ChildProcess, spawn } from \"child_process\";\nimport stripAnsi from \"strip-ansi\";\nimport { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from \"../utils/shell.js\";\nimport type { BashOperations } from \"./tools/bash.js\";\nimport { DEFAULT_MAX_BYTES, truncateTail } from \"./tools/truncate.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface BashExecutorOptions {\n\t/** Callback for streaming output chunks (already sanitized) */\n\tonChunk?: (chunk: string) => void;\n\t/** AbortSignal for cancellation */\n\tsignal?: AbortSignal;\n\t/** Working directory for command execution. Defaults to process.cwd() */\n\tcwd?: string;\n}\n\nexport interface BashResult {\n\t/** Combined stdout + stderr output (sanitized, possibly truncated) */\n\toutput: string;\n\t/** Process exit code (undefined if killed/cancelled) */\n\texitCode: number | undefined;\n\t/** Whether the command was cancelled via signal */\n\tcancelled: boolean;\n\t/** Whether the output was truncated */\n\ttruncated: boolean;\n\t/** Path to temp file containing full output (if output exceeded truncation threshold) */\n\tfullOutputPath?: string;\n\t/** Background process id (present only for detached background execution). */\n\tbackgroundTaskId?: string;\n\t/** Path to JSON status metadata for a background process. */\n\tbackgroundStatusPath?: string;\n\t/** Path to log file for a background process. */\n\tbackgroundLogPath?: string;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Execute a bash command with optional streaming and cancellation support.\n *\n * Features:\n * - Streams sanitized output via onChunk callback\n * - Writes large output to temp file for later retrieval\n * - Supports cancellation via AbortSignal\n * - Sanitizes output (strips ANSI, removes binary garbage, normalizes newlines)\n * - Truncates output if it exceeds the default max bytes\n *\n * @param command - The bash command to execute\n * @param options - Optional streaming callback and abort signal\n * @returns Promise resolving to execution result\n */\nexport function executeBash(command: string, options?: BashExecutorOptions): Promise<BashResult> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst { shell, args } = getShellConfig();\n\t\tconst child: ChildProcess = spawn(shell, [...args, command], {\n\t\t\tcwd: options?.cwd,\n\t\t\tdetached: true,\n\t\t\tenv: getShellEnv(),\n\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t});\n\n\t\t// Track sanitized output for truncation\n\t\tconst outputChunks: string[] = [];\n\t\tlet outputBytes = 0;\n\t\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t// Temp file for large output\n\t\tlet tempFilePath: string | undefined;\n\t\tlet tempFileStream: WriteStream | undefined;\n\t\tlet totalBytes = 0;\n\n\t\t// Handle abort signal\n\t\tconst abortHandler = () => {\n\t\t\tif (child.pid) {\n\t\t\t\tkillProcessTree(child.pid);\n\t\t\t}\n\t\t};\n\n\t\tif (options?.signal) {\n\t\t\tif (options.signal.aborted) {\n\t\t\t\t// Already aborted, don't even start\n\t\t\t\tchild.kill();\n\t\t\t\tresolve({\n\t\t\t\t\toutput: \"\",\n\t\t\t\t\texitCode: undefined,\n\t\t\t\t\tcancelled: true,\n\t\t\t\t\ttruncated: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\toptions.signal.addEventListener(\"abort\", abortHandler, { once: true });\n\t\t}\n\n\t\tconst decoder = new TextDecoder();\n\n\t\tconst handleData = (data: Buffer) => {\n\t\t\ttotalBytes += data.length;\n\n\t\t\t// Sanitize once at the source: strip ANSI, replace binary garbage, normalize newlines\n\t\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t\t// Start writing to temp file if exceeds threshold\n\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\t\ttempFilePath = join(tmpdir(), `iosm-bash-${id}.log`);\n\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t// Write already-buffered chunks to temp file\n\t\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\t\ttempFileStream.write(chunk);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (tempFileStream) {\n\t\t\t\ttempFileStream.write(text);\n\t\t\t}\n\n\t\t\t// Keep rolling buffer of sanitized text\n\t\t\toutputChunks.push(text);\n\t\t\toutputBytes += text.length;\n\t\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\t\tconst removed = outputChunks.shift()!;\n\t\t\t\toutputBytes -= removed.length;\n\t\t\t}\n\n\t\t\t// Stream to callback if provided\n\t\t\tif (options?.onChunk) {\n\t\t\t\toptions.onChunk(text);\n\t\t\t}\n\t\t};\n\n\t\tchild.stdout?.on(\"data\", handleData);\n\t\tchild.stderr?.on(\"data\", handleData);\n\n\t\tchild.on(\"close\", (code) => {\n\t\t\t// Clean up abort listener\n\t\t\tif (options?.signal) {\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\n\t\t\t}\n\n\t\t\tif (tempFileStream) {\n\t\t\t\ttempFileStream.end();\n\t\t\t}\n\n\t\t\t// Combine buffered chunks for truncation (already sanitized)\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\n\t\t\t// code === null means killed (cancelled)\n\t\t\tconst cancelled = code === null;\n\n\t\t\tresolve({\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: cancelled ? undefined : code,\n\t\t\t\tcancelled,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t});\n\t\t});\n\n\t\tchild.on(\"error\", (err) => {\n\t\t\t// Clean up abort listener\n\t\t\tif (options?.signal) {\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\n\t\t\t}\n\n\t\t\tif (tempFileStream) {\n\t\t\t\ttempFileStream.end();\n\t\t\t}\n\n\t\t\treject(err);\n\t\t});\n\t});\n}\n\n/**\n * Execute a bash command using custom BashOperations.\n * Used for remote execution (SSH, containers, etc.).\n */\nexport async function executeBashWithOperations(\n\tcommand: string,\n\tcwd: string,\n\toperations: BashOperations,\n\toptions?: BashExecutorOptions,\n): Promise<BashResult> {\n\tconst outputChunks: string[] = [];\n\tlet outputBytes = 0;\n\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\tlet tempFilePath: string | undefined;\n\tlet tempFileStream: WriteStream | undefined;\n\tlet totalBytes = 0;\n\n\tconst decoder = new TextDecoder();\n\n\tconst onData = (data: Buffer) => {\n\t\ttotalBytes += data.length;\n\n\t\t// Sanitize: strip ANSI, replace binary garbage, normalize newlines\n\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t// Start writing to temp file if exceeds threshold\n\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `iosm-bash-${id}.log`);\n\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\ttempFileStream.write(chunk);\n\t\t\t}\n\t\t}\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.write(text);\n\t\t}\n\n\t\t// Keep rolling buffer\n\t\toutputChunks.push(text);\n\t\toutputBytes += text.length;\n\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\tconst removed = outputChunks.shift()!;\n\t\t\toutputBytes -= removed.length;\n\t\t}\n\n\t\t// Stream to callback\n\t\tif (options?.onChunk) {\n\t\t\toptions.onChunk(text);\n\t\t}\n\t};\n\n\ttry {\n\t\tconst result = await operations.exec(command, cwd, {\n\t\t\tonData,\n\t\t\tsignal: options?.signal,\n\t\t});\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\tconst fullOutput = outputChunks.join(\"\");\n\t\tconst truncationResult = truncateTail(fullOutput);\n\t\tconst cancelled = options?.signal?.aborted ?? false;\n\n\t\treturn {\n\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\texitCode: cancelled ? undefined : (result.exitCode ?? undefined),\n\t\t\tcancelled,\n\t\t\ttruncated: truncationResult.truncated,\n\t\t\tfullOutputPath: tempFilePath,\n\t\t};\n\t} catch (err) {\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\t// Check if it was an abort\n\t\tif (options?.signal?.aborted) {\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\t\t\treturn {\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: undefined,\n\t\t\t\tcancelled: true,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t};\n\t\t}\n\n\t\tthrow err;\n\t}\n}\n"]}
|
|
1
|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AI;gBACf,SAAS,EAAE,gBAAgB,CAAC,SAAS;gBACrC,cAAc,EAAE,YAAY;aAC5B,CAAC;QACH,CAAC;QAED,MAAM,GAAG,CAAC;IACX,CAAC;AACF,CAAC","sourcesContent":["/**\n * Bash command execution with streaming support and cancellation.\n *\n * This module provides a unified bash execution implementation used by:\n * - AgentSession.executeBash() for interactive and RPC modes\n * - Direct calls from modes that need bash execution\n */\n\nimport { randomBytes } from \"node:crypto\";\nimport { createWriteStream, type WriteStream } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { type ChildProcess, spawn } from \"child_process\";\nimport stripAnsi from \"strip-ansi\";\nimport { adaptCommandForShell, getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from \"../utils/shell.js\";\nimport type { BashOperations } from \"./tools/bash.js\";\nimport { DEFAULT_MAX_BYTES, truncateTail } from \"./tools/truncate.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface BashExecutorOptions {\n\t/** Callback for streaming output chunks (already sanitized) */\n\tonChunk?: (chunk: string) => void;\n\t/** AbortSignal for cancellation */\n\tsignal?: AbortSignal;\n\t/** Working directory for command execution. Defaults to process.cwd() */\n\tcwd?: string;\n}\n\nexport interface BashResult {\n\t/** Combined stdout + stderr output (sanitized, possibly truncated) */\n\toutput: string;\n\t/** Process exit code (undefined if killed/cancelled) */\n\texitCode: number | undefined;\n\t/** Whether the command was cancelled via signal */\n\tcancelled: boolean;\n\t/** Whether the output was truncated */\n\ttruncated: boolean;\n\t/** Path to temp file containing full output (if output exceeded truncation threshold) */\n\tfullOutputPath?: string;\n\t/** Background process id (present only for detached background execution). */\n\tbackgroundTaskId?: string;\n\t/** Path to JSON status metadata for a background process. */\n\tbackgroundStatusPath?: string;\n\t/** Path to log file for a background process. */\n\tbackgroundLogPath?: string;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Execute a bash command with optional streaming and cancellation support.\n *\n * Features:\n * - Streams sanitized output via onChunk callback\n * - Writes large output to temp file for later retrieval\n * - Supports cancellation via AbortSignal\n * - Sanitizes output (strips ANSI, removes binary garbage, normalizes newlines)\n * - Truncates output if it exceeds the default max bytes\n *\n * @param command - The bash command to execute\n * @param options - Optional streaming callback and abort signal\n * @returns Promise resolving to execution result\n */\nexport function executeBash(command: string, options?: BashExecutorOptions): Promise<BashResult> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst { shell, args } = getShellConfig();\n\t\tconst adaptedCommand = adaptCommandForShell(command);\n\t\tconst child: ChildProcess = spawn(shell, [...args, adaptedCommand], {\n\t\t\tcwd: options?.cwd,\n\t\t\tdetached: true,\n\t\t\tenv: getShellEnv(),\n\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t});\n\n\t\t// Track sanitized output for truncation\n\t\tconst outputChunks: string[] = [];\n\t\tlet outputBytes = 0;\n\t\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t// Temp file for large output\n\t\tlet tempFilePath: string | undefined;\n\t\tlet tempFileStream: WriteStream | undefined;\n\t\tlet totalBytes = 0;\n\n\t\t// Handle abort signal\n\t\tconst abortHandler = () => {\n\t\t\tif (child.pid) {\n\t\t\t\tkillProcessTree(child.pid);\n\t\t\t}\n\t\t};\n\n\t\tif (options?.signal) {\n\t\t\tif (options.signal.aborted) {\n\t\t\t\t// Already aborted, don't even start\n\t\t\t\tchild.kill();\n\t\t\t\tresolve({\n\t\t\t\t\toutput: \"\",\n\t\t\t\t\texitCode: undefined,\n\t\t\t\t\tcancelled: true,\n\t\t\t\t\ttruncated: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\toptions.signal.addEventListener(\"abort\", abortHandler, { once: true });\n\t\t}\n\n\t\tconst decoder = new TextDecoder();\n\n\t\tconst handleData = (data: Buffer) => {\n\t\t\ttotalBytes += data.length;\n\n\t\t\t// Sanitize once at the source: strip ANSI, replace binary garbage, normalize newlines\n\t\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t\t// Start writing to temp file if exceeds threshold\n\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\t\ttempFilePath = join(tmpdir(), `iosm-bash-${id}.log`);\n\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t// Write already-buffered chunks to temp file\n\t\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\t\ttempFileStream.write(chunk);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (tempFileStream) {\n\t\t\t\ttempFileStream.write(text);\n\t\t\t}\n\n\t\t\t// Keep rolling buffer of sanitized text\n\t\t\toutputChunks.push(text);\n\t\t\toutputBytes += text.length;\n\t\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\t\tconst removed = outputChunks.shift()!;\n\t\t\t\toutputBytes -= removed.length;\n\t\t\t}\n\n\t\t\t// Stream to callback if provided\n\t\t\tif (options?.onChunk) {\n\t\t\t\toptions.onChunk(text);\n\t\t\t}\n\t\t};\n\n\t\tchild.stdout?.on(\"data\", handleData);\n\t\tchild.stderr?.on(\"data\", handleData);\n\n\t\tchild.on(\"close\", (code) => {\n\t\t\t// Clean up abort listener\n\t\t\tif (options?.signal) {\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\n\t\t\t}\n\n\t\t\tif (tempFileStream) {\n\t\t\t\ttempFileStream.end();\n\t\t\t}\n\n\t\t\t// Combine buffered chunks for truncation (already sanitized)\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\n\t\t\t// code === null means killed (cancelled)\n\t\t\tconst cancelled = code === null;\n\n\t\t\tresolve({\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: cancelled ? undefined : code,\n\t\t\t\tcancelled,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t});\n\t\t});\n\n\t\tchild.on(\"error\", (err) => {\n\t\t\t// Clean up abort listener\n\t\t\tif (options?.signal) {\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\n\t\t\t}\n\n\t\t\tif (tempFileStream) {\n\t\t\t\ttempFileStream.end();\n\t\t\t}\n\n\t\t\treject(err);\n\t\t});\n\t});\n}\n\n/**\n * Execute a bash command using custom BashOperations.\n * Used for remote execution (SSH, containers, etc.).\n */\nexport async function executeBashWithOperations(\n\tcommand: string,\n\tcwd: string,\n\toperations: BashOperations,\n\toptions?: BashExecutorOptions,\n): Promise<BashResult> {\n\tconst outputChunks: string[] = [];\n\tlet outputBytes = 0;\n\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\tlet tempFilePath: string | undefined;\n\tlet tempFileStream: WriteStream | undefined;\n\tlet totalBytes = 0;\n\n\tconst decoder = new TextDecoder();\n\n\tconst onData = (data: Buffer) => {\n\t\ttotalBytes += data.length;\n\n\t\t// Sanitize: strip ANSI, replace binary garbage, normalize newlines\n\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t// Start writing to temp file if exceeds threshold\n\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `iosm-bash-${id}.log`);\n\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\ttempFileStream.write(chunk);\n\t\t\t}\n\t\t}\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.write(text);\n\t\t}\n\n\t\t// Keep rolling buffer\n\t\toutputChunks.push(text);\n\t\toutputBytes += text.length;\n\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\tconst removed = outputChunks.shift()!;\n\t\t\toutputBytes -= removed.length;\n\t\t}\n\n\t\t// Stream to callback\n\t\tif (options?.onChunk) {\n\t\t\toptions.onChunk(text);\n\t\t}\n\t};\n\n\ttry {\n\t\tconst result = await operations.exec(command, cwd, {\n\t\t\tonData,\n\t\t\tsignal: options?.signal,\n\t\t});\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\tconst fullOutput = outputChunks.join(\"\");\n\t\tconst truncationResult = truncateTail(fullOutput);\n\t\tconst cancelled = options?.signal?.aborted ?? false;\n\n\t\treturn {\n\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\texitCode: cancelled ? undefined : (result.exitCode ?? undefined),\n\t\t\tcancelled,\n\t\t\ttruncated: truncationResult.truncated,\n\t\t\tfullOutputPath: tempFilePath,\n\t\t};\n\t} catch (err) {\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\t// Check if it was an abort\n\t\tif (options?.signal?.aborted) {\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\t\t\treturn {\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: undefined,\n\t\t\t\tcancelled: true,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t};\n\t\t}\n\n\t\tthrow err;\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bash.d.ts","sourceRoot":"","sources":["../../../src/core/tools/bash.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC7D,OAAO,EAAE,KAAK,MAAM,EAAQ,MAAM,mBAAmB,CAAC;AAMtD,OAAO,EAAoD,KAAK,gBAAgB,EAAgB,MAAM,eAAe,CAAC;AACtH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBA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B,CAAC;CACtC;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,SAAS,CAAC,OAAO,UAAU,CAAC,CA0MnG;AAED,0EAA0E;AAC1E,eAAO,MAAM,QAAQ;;;;QAAgC,CAAC"}
|
package/dist/core/tools/bash.js
CHANGED
|
@@ -5,7 +5,7 @@ import { join } from "node:path";
|
|
|
5
5
|
import { Type } from "@sinclair/typebox";
|
|
6
6
|
import { spawn } from "child_process";
|
|
7
7
|
import stripAnsi from "strip-ansi";
|
|
8
|
-
import { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from "../../utils/shell.js";
|
|
8
|
+
import { adaptCommandForShell, getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from "../../utils/shell.js";
|
|
9
9
|
import { startBackgroundProcess } from "../background-processes.js";
|
|
10
10
|
import { isSandboxEnabledFromEnv, wrapCommandWithSandbox } from "../sandbox/executor.js";
|
|
11
11
|
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, truncateTail } from "./truncate.js";
|
|
@@ -128,7 +128,10 @@ function resolveSpawnContext(command, cwd, spawnHook) {
|
|
|
128
128
|
if (modified.cwd !== baseContext.cwd) {
|
|
129
129
|
process.stderr.write(`[iosm] Extension modified bash cwd: ${baseContext.cwd} → ${modified.cwd}\n`);
|
|
130
130
|
}
|
|
131
|
-
return
|
|
131
|
+
return {
|
|
132
|
+
...modified,
|
|
133
|
+
command: adaptCommandForShell(modified.command),
|
|
134
|
+
};
|
|
132
135
|
}
|
|
133
136
|
export function createBashTool(cwd, options) {
|
|
134
137
|
const ops = options?.operations ?? defaultBashOperations;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bash.js","sourceRoot":"","sources":["../../../src/core/tools/bash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,U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c,EAAE,CAAC;wBACpB,cAAc,CAAC,GAAG,EAAE,CAAC;oBACtB,CAAC;oBAED,IAAI,MAAM,GAAG,aAAa,CAAC;oBAE3B,IAAI,GAAG,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;wBAC/B,IAAI,MAAM;4BAAE,MAAM,IAAI,MAAM,CAAC;wBAC7B,MAAM,IAAI,iBAAiB,CAAC;wBAC5B,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;oBAC3B,CAAC;yBAAM,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;wBAC/C,MAAM,WAAW,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;wBAC9C,IAAI,MAAM;4BAAE,MAAM,IAAI,MAAM,CAAC;wBAC7B,MAAM,IAAI,2BAA2B,WAAW,UAAU,CAAC;wBAC3D,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;oBAC3B,CAAC;yBAAM,CAAC;wBACP,MAAM,CAAC,GAAG,CAAC,CAAC;oBACb,CAAC;gBACF,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACJ,CAAC;KACD,CAAC;AACH,CAAC;AAED,0EAA0E;AAC1E,MAAM,CAAC,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC","sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport { createWriteStream, existsSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { AgentTool } from \"@mariozechner/pi-agent-core\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { spawn } from \"child_process\";\nimport stripAnsi from \"strip-ansi\";\nimport { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from \"../../utils/shell.js\";\nimport { startBackgroundProcess } from \"../background-processes.js\";\nimport { isSandboxEnabledFromEnv, wrapCommandWithSandbox } from \"../sandbox/executor.js\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateTail } from \"./truncate.js\";\nimport type { ToolPermissionGuard } from \"./permissions.js\";\n\n/**\n * Generate a unique temp file path for bash output\n */\nfunction getTempFilePath(): string {\n\tconst id = randomBytes(8).toString(\"hex\");\n\treturn join(tmpdir(), `iosm-bash-${id}.log`);\n}\n\nconst bashSchema = Type.Object({\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n\trun_in_background: Type.Optional(\n\t\tType.Boolean({ description: \"Run command in detached background mode and return task id immediately\" }),\n\t),\n});\n\nexport type BashToolInput = Static<typeof bashSchema>;\n\nexport interface BashToolDetails {\n\ttruncation?: TruncationResult;\n\tfullOutputPath?: string;\n\tbackgroundTaskId?: string;\n\tbackgroundStatusPath?: string;\n\tbackgroundLogPath?: string;\n}\n\n/**\n * Pluggable operations for the bash tool.\n * Override these to delegate command execution to remote systems (e.g., SSH).\n */\nexport interface BashOperations {\n\t/**\n\t * Execute a command and stream output.\n\t * @param command - The command to execute\n\t * @param cwd - Working directory\n\t * @param options - Execution options\n\t * @returns Promise resolving to exit code (null if killed)\n\t */\n\texec: (\n\t\tcommand: string,\n\t\tcwd: string,\n\t\toptions: {\n\t\t\tonData: (data: Buffer) => void;\n\t\t\tsignal?: AbortSignal;\n\t\t\ttimeout?: number;\n\t\t\tenv?: NodeJS.ProcessEnv;\n\t\t},\n\t) => Promise<{ exitCode: number | null }>;\n}\n\n/**\n * Default bash operations using local shell\n */\nconst defaultBashOperations: BashOperations = {\n\texec: (command, cwd, { onData, signal, timeout, env }) => {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst { shell, args } = getShellConfig();\n\n\t\t\tif (!existsSync(cwd)) {\n\t\t\t\treject(new Error(`Working directory does not exist: ${cwd}\\nCannot execute bash commands.`));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet wrapped;\n\t\t\ttry {\n\t\t\t\twrapped = wrapCommandWithSandbox({\n\t\t\t\t\tcommand: shell,\n\t\t\t\t\targs: [...args, command],\n\t\t\t\t\tcwd,\n\t\t\t\t\tenabled: isSandboxEnabledFromEnv(),\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\treject(error instanceof Error ? error : new Error(String(error)));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst child = spawn(wrapped.command, wrapped.args, {\n\t\t\t\tcwd,\n\t\t\t\tdetached: true,\n\t\t\t\tenv: env ?? getShellEnv(),\n\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t});\n\n\t\t\tlet timedOut = false;\n\n\t\t\t// Set timeout if provided\n\t\t\tlet timeoutHandle: NodeJS.Timeout | undefined;\n\t\t\tif (timeout !== undefined && timeout > 0) {\n\t\t\t\ttimeoutHandle = setTimeout(() => {\n\t\t\t\t\ttimedOut = true;\n\t\t\t\t\tif (child.pid) {\n\t\t\t\t\t\tkillProcessTree(child.pid);\n\t\t\t\t\t}\n\t\t\t\t}, timeout * 1000);\n\t\t\t}\n\n\t\t\t// Stream stdout and stderr\n\t\t\tif (child.stdout) {\n\t\t\t\tchild.stdout.on(\"data\", onData);\n\t\t\t}\n\t\t\tif (child.stderr) {\n\t\t\t\tchild.stderr.on(\"data\", onData);\n\t\t\t}\n\n\t\t\t// Handle shell spawn errors\n\t\t\tchild.on(\"error\", (err) => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\treject(err);\n\t\t\t});\n\n\t\t\t// Handle abort signal - kill entire process tree\n\t\t\tconst onAbort = () => {\n\t\t\t\tif (child.pid) {\n\t\t\t\t\tkillProcessTree(child.pid);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tif (signal) {\n\t\t\t\tif (signal.aborted) {\n\t\t\t\t\tonAbort();\n\t\t\t\t} else {\n\t\t\t\t\tsignal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Handle process exit\n\t\t\tchild.on(\"close\", (code) => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\n\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\treject(new Error(\"aborted\"));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (timedOut) {\n\t\t\t\t\treject(new Error(`timeout:${timeout}`));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresolve({ exitCode: code });\n\t\t\t});\n\t\t});\n\t},\n};\n\nexport interface BashSpawnContext {\n\tcommand: string;\n\tcwd: string;\n\tenv: NodeJS.ProcessEnv;\n}\n\nexport type BashSpawnHook = (context: BashSpawnContext) => BashSpawnContext;\n\nfunction sanitizeBashChunk(text: string): string {\n\treturn sanitizeBinaryOutput(stripAnsi(text)).replace(/\\r/g, \"\");\n}\n\nfunction resolveSpawnContext(command: string, cwd: string, spawnHook?: BashSpawnHook): BashSpawnContext {\n\tconst baseContext: BashSpawnContext = {\n\t\tcommand,\n\t\tcwd,\n\t\tenv: { ...getShellEnv() },\n\t};\n\n\tif (!spawnHook) return baseContext;\n\n\tconst modified = spawnHook(baseContext);\n\n\tif (modified.command !== baseContext.command) {\n\t\tprocess.stderr.write(\n\t\t\t`[iosm] Extension modified bash command:\\n before: ${baseContext.command}\\n after: ${modified.command}\\n`,\n\t\t);\n\t}\n\tif (modified.cwd !== baseContext.cwd) {\n\t\tprocess.stderr.write(`[iosm] Extension modified bash cwd: ${baseContext.cwd} → ${modified.cwd}\\n`);\n\t}\n\n\treturn modified;\n}\n\nexport interface BashToolOptions {\n\t/** Custom operations for command execution. Default: local shell */\n\toperations?: BashOperations;\n\t/** Command prefix prepended to every command (e.g., \"shopt -s expand_aliases\" for alias support) */\n\tcommandPrefix?: string;\n\t/** Hook to adjust command, cwd, or env before execution */\n\tspawnHook?: BashSpawnHook;\n\t/** Optional permission guard executed before command starts */\n\tpermissionGuard?: ToolPermissionGuard;\n}\n\nexport function createBashTool(cwd: string, options?: BashToolOptions): AgentTool<typeof bashSchema> {\n\tconst ops = options?.operations ?? defaultBashOperations;\n\tconst commandPrefix = options?.commandPrefix;\n\tconst spawnHook = options?.spawnHook;\n\tconst permissionGuard = options?.permissionGuard;\n\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,\n\t\tparameters: bashSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ command, timeout, run_in_background }: { command: string; timeout?: number; run_in_background?: boolean },\n\t\t\tsignal?: AbortSignal,\n\t\t\tonUpdate?,\n\t\t) => {\n\t\t\tif (permissionGuard) {\n\t\t\t\tconst allowed = await permissionGuard({\n\t\t\t\t\ttoolName: \"bash\",\n\t\t\t\t\tcwd,\n\t\t\t\t\tinput: { command, timeout, run_in_background },\n\t\t\t\t\tsummary: command.trim().slice(0, 200),\n\t\t\t\t});\n\t\t\t\tif (!allowed) {\n\t\t\t\t\tthrow new Error(\"Permission denied for bash command.\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Apply command prefix if configured (e.g., \"shopt -s expand_aliases\" for alias support)\n\t\t\tconst resolvedCommand = commandPrefix ? `${commandPrefix}\\n${command}` : command;\n\t\t\tconst spawnContext = resolveSpawnContext(resolvedCommand, cwd, spawnHook);\n\t\t\tif (run_in_background) {\n\t\t\t\tif (ops !== defaultBashOperations) {\n\t\t\t\t\tthrow new Error(\"Background bash is not supported with custom execution operations.\");\n\t\t\t\t}\n\t\t\t\tconst background = startBackgroundProcess({\n\t\t\t\t\trootCwd: cwd,\n\t\t\t\t\tcwd: spawnContext.cwd,\n\t\t\t\t\tcommand: spawnContext.command,\n\t\t\t\t\tsource: \"tool\",\n\t\t\t\t});\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: `Started background process ${background.id} (pid ${background.pid}). Use /bg status ${background.id}, /bg logs ${background.id}, or /bg stop ${background.id}.`,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\tbackgroundTaskId: background.id,\n\t\t\t\t\t\tbackgroundStatusPath: background.metaPath,\n\t\t\t\t\t\tbackgroundLogPath: background.logPath,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\t// We'll stream to a temp file if output gets large\n\t\t\t\tlet tempFilePath: string | undefined;\n\t\t\t\tlet tempFileStream: ReturnType<typeof createWriteStream> | undefined;\n\t\t\t\tlet totalBytes = 0;\n\n\t\t\t\tconst decoder = new TextDecoder();\n\t\t\t\t// Keep a rolling buffer of recent sanitized output\n\t\t\t\tconst chunks: string[] = [];\n\t\t\t\tlet rollingOutput = \"\";\n\t\t\t\tlet chunksBytes = 0;\n\t\t\t\t// Keep more than we need so we have enough context for tail truncation\n\t\t\t\tconst maxChunksBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t\t\tconst pushText = (text: string) => {\n\t\t\t\t\tif (!text) {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Start writing to temp file once we exceed the threshold\n\t\t\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t\t\t// Write all buffered chunks to the file\n\t\t\t\t\t\tfor (const chunk of chunks) {\n\t\t\t\t\t\t\ttempFileStream.write(chunk);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Write to temp file if we have one\n\t\t\t\t\tif (tempFileStream) {\n\t\t\t\t\t\ttempFileStream.write(text);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Keep rolling buffer of recent data\n\t\t\t\t\tchunks.push(text);\n\t\t\t\t\trollingOutput += text;\n\t\t\t\t\tchunksBytes += Buffer.byteLength(text, \"utf-8\");\n\n\t\t\t\t\t// Trim old chunks if buffer is too large\n\t\t\t\t\twhile (chunksBytes > maxChunksBytes && chunks.length > 1) {\n\t\t\t\t\t\tconst removed = chunks.shift()!;\n\t\t\t\t\t\tchunksBytes -= Buffer.byteLength(removed, \"utf-8\");\n\t\t\t\t\t\trollingOutput = rollingOutput.slice(removed.length);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Stream partial output to callback (truncated rolling buffer)\n\t\t\t\t\tif (onUpdate) {\n\t\t\t\t\t\tconst truncation = truncateTail(rollingOutput);\n\t\t\t\t\t\tonUpdate({\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: truncation.content || \"\" }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\ttruncation: truncation.truncated ? truncation : undefined,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tconst handleData = (data: Buffer) => {\n\t\t\t\t\ttotalBytes += data.length;\n\t\t\t\t\tpushText(sanitizeBashChunk(decoder.decode(data, { stream: true })));\n\t\t\t\t};\n\n\t\t\t\tconst flushDecoder = () => {\n\t\t\t\t\tpushText(sanitizeBashChunk(decoder.decode()));\n\t\t\t\t};\n\n\t\t\t\tops.exec(spawnContext.command, spawnContext.cwd, {\n\t\t\t\t\tonData: handleData,\n\t\t\t\t\tsignal,\n\t\t\t\t\ttimeout,\n\t\t\t\t\tenv: spawnContext.env,\n\t\t\t\t})\n\t\t\t\t\t.then(({ exitCode }) => {\n\t\t\t\t\t\tflushDecoder();\n\n\t\t\t\t\t\t// Close temp file stream\n\t\t\t\t\t\tif (tempFileStream) {\n\t\t\t\t\t\t\ttempFileStream.end();\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Apply tail truncation\n\t\t\t\t\t\tconst truncation = truncateTail(rollingOutput);\n\t\t\t\t\t\tlet outputText = truncation.content || \"(no output)\";\n\n\t\t\t\t\t\t// Build details with truncation info\n\t\t\t\t\t\tlet details: BashToolDetails | undefined;\n\n\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\tdetails = {\n\t\t\t\t\t\t\t\ttruncation,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\t// Build actionable notice\n\t\t\t\t\t\t\tconst startLine = truncation.totalLines - truncation.outputLines + 1;\n\t\t\t\t\t\t\tconst endLine = truncation.totalLines;\n\n\t\t\t\t\t\t\tif (truncation.lastLinePartial) {\n\t\t\t\t\t\t\t\t// Edge case: last line alone > 30KB\n\t\t\t\t\t\t\t\tconst lastLineSize = formatSize(\n\t\t\t\t\t\t\t\t\tBuffer.byteLength(rollingOutput.split(\"\\n\").pop() || \"\", \"utf-8\"),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else if (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (exitCode !== 0 && exitCode !== null) {\n\t\t\t\t\t\t\toutputText += `\\n\\nCommand exited with code ${exitCode}`;\n\t\t\t\t\t\t\treject(new Error(outputText));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: outputText }], details });\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err: Error) => {\n\t\t\t\t\t\tflushDecoder();\n\n\t\t\t\t\t\t// Close temp file stream\n\t\t\t\t\t\tif (tempFileStream) {\n\t\t\t\t\t\t\ttempFileStream.end();\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet output = rollingOutput;\n\n\t\t\t\t\t\tif (err.message === \"aborted\") {\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += \"Command aborted\";\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else if (err.message.startsWith(\"timeout:\")) {\n\t\t\t\t\t\t\tconst timeoutSecs = err.message.split(\":\")[1];\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += `Command timed out after ${timeoutSecs} seconds`;\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t};\n}\n\n/** Default bash tool using process.cwd() - for backwards compatibility */\nexport const bashTool = createBashTool(process.cwd());\n"]}
|
|
1
|
+
{"version":3,"file":"bash.js","sourceRoot":"","sources":["../../../src/core/tools/bash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAA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E,CAAC;wBACpB,cAAc,CAAC,GAAG,EAAE,CAAC;oBACtB,CAAC;oBAED,IAAI,MAAM,GAAG,aAAa,CAAC;oBAE3B,IAAI,GAAG,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;wBAC/B,IAAI,MAAM;4BAAE,MAAM,IAAI,MAAM,CAAC;wBAC7B,MAAM,IAAI,iBAAiB,CAAC;wBAC5B,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;oBAC3B,CAAC;yBAAM,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;wBAC/C,MAAM,WAAW,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;wBAC9C,IAAI,MAAM;4BAAE,MAAM,IAAI,MAAM,CAAC;wBAC7B,MAAM,IAAI,2BAA2B,WAAW,UAAU,CAAC;wBAC3D,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;oBAC3B,CAAC;yBAAM,CAAC;wBACP,MAAM,CAAC,GAAG,CAAC,CAAC;oBACb,CAAC;gBACF,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACJ,CAAC;KACD,CAAC;AACH,CAAC;AAED,0EAA0E;AAC1E,MAAM,CAAC,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC","sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport { createWriteStream, existsSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { AgentTool } from \"@mariozechner/pi-agent-core\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { spawn } from \"child_process\";\nimport stripAnsi from \"strip-ansi\";\nimport { adaptCommandForShell, getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from \"../../utils/shell.js\";\nimport { startBackgroundProcess } from \"../background-processes.js\";\nimport { isSandboxEnabledFromEnv, wrapCommandWithSandbox } from \"../sandbox/executor.js\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateTail } from \"./truncate.js\";\nimport type { ToolPermissionGuard } from \"./permissions.js\";\n\n/**\n * Generate a unique temp file path for bash output\n */\nfunction getTempFilePath(): string {\n\tconst id = randomBytes(8).toString(\"hex\");\n\treturn join(tmpdir(), `iosm-bash-${id}.log`);\n}\n\nconst bashSchema = Type.Object({\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n\trun_in_background: Type.Optional(\n\t\tType.Boolean({ description: \"Run command in detached background mode and return task id immediately\" }),\n\t),\n});\n\nexport type BashToolInput = Static<typeof bashSchema>;\n\nexport interface BashToolDetails {\n\ttruncation?: TruncationResult;\n\tfullOutputPath?: string;\n\tbackgroundTaskId?: string;\n\tbackgroundStatusPath?: string;\n\tbackgroundLogPath?: string;\n}\n\n/**\n * Pluggable operations for the bash tool.\n * Override these to delegate command execution to remote systems (e.g., SSH).\n */\nexport interface BashOperations {\n\t/**\n\t * Execute a command and stream output.\n\t * @param command - The command to execute\n\t * @param cwd - Working directory\n\t * @param options - Execution options\n\t * @returns Promise resolving to exit code (null if killed)\n\t */\n\texec: (\n\t\tcommand: string,\n\t\tcwd: string,\n\t\toptions: {\n\t\t\tonData: (data: Buffer) => void;\n\t\t\tsignal?: AbortSignal;\n\t\t\ttimeout?: number;\n\t\t\tenv?: NodeJS.ProcessEnv;\n\t\t},\n\t) => Promise<{ exitCode: number | null }>;\n}\n\n/**\n * Default bash operations using local shell\n */\nconst defaultBashOperations: BashOperations = {\n\texec: (command, cwd, { onData, signal, timeout, env }) => {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst { shell, args } = getShellConfig();\n\n\t\t\tif (!existsSync(cwd)) {\n\t\t\t\treject(new Error(`Working directory does not exist: ${cwd}\\nCannot execute bash commands.`));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet wrapped;\n\t\t\ttry {\n\t\t\t\twrapped = wrapCommandWithSandbox({\n\t\t\t\t\tcommand: shell,\n\t\t\t\t\targs: [...args, command],\n\t\t\t\t\tcwd,\n\t\t\t\t\tenabled: isSandboxEnabledFromEnv(),\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\treject(error instanceof Error ? error : new Error(String(error)));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst child = spawn(wrapped.command, wrapped.args, {\n\t\t\t\tcwd,\n\t\t\t\tdetached: true,\n\t\t\t\tenv: env ?? getShellEnv(),\n\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t});\n\n\t\t\tlet timedOut = false;\n\n\t\t\t// Set timeout if provided\n\t\t\tlet timeoutHandle: NodeJS.Timeout | undefined;\n\t\t\tif (timeout !== undefined && timeout > 0) {\n\t\t\t\ttimeoutHandle = setTimeout(() => {\n\t\t\t\t\ttimedOut = true;\n\t\t\t\t\tif (child.pid) {\n\t\t\t\t\t\tkillProcessTree(child.pid);\n\t\t\t\t\t}\n\t\t\t\t}, timeout * 1000);\n\t\t\t}\n\n\t\t\t// Stream stdout and stderr\n\t\t\tif (child.stdout) {\n\t\t\t\tchild.stdout.on(\"data\", onData);\n\t\t\t}\n\t\t\tif (child.stderr) {\n\t\t\t\tchild.stderr.on(\"data\", onData);\n\t\t\t}\n\n\t\t\t// Handle shell spawn errors\n\t\t\tchild.on(\"error\", (err) => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\treject(err);\n\t\t\t});\n\n\t\t\t// Handle abort signal - kill entire process tree\n\t\t\tconst onAbort = () => {\n\t\t\t\tif (child.pid) {\n\t\t\t\t\tkillProcessTree(child.pid);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tif (signal) {\n\t\t\t\tif (signal.aborted) {\n\t\t\t\t\tonAbort();\n\t\t\t\t} else {\n\t\t\t\t\tsignal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Handle process exit\n\t\t\tchild.on(\"close\", (code) => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\n\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\treject(new Error(\"aborted\"));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (timedOut) {\n\t\t\t\t\treject(new Error(`timeout:${timeout}`));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresolve({ exitCode: code });\n\t\t\t});\n\t\t});\n\t},\n};\n\nexport interface BashSpawnContext {\n\tcommand: string;\n\tcwd: string;\n\tenv: NodeJS.ProcessEnv;\n}\n\nexport type BashSpawnHook = (context: BashSpawnContext) => BashSpawnContext;\n\nfunction sanitizeBashChunk(text: string): string {\n\treturn sanitizeBinaryOutput(stripAnsi(text)).replace(/\\r/g, \"\");\n}\n\nfunction resolveSpawnContext(command: string, cwd: string, spawnHook?: BashSpawnHook): BashSpawnContext {\n\tconst baseContext: BashSpawnContext = {\n\t\tcommand,\n\t\tcwd,\n\t\tenv: { ...getShellEnv() },\n\t};\n\n\tif (!spawnHook) return baseContext;\n\n\tconst modified = spawnHook(baseContext);\n\n\tif (modified.command !== baseContext.command) {\n\t\tprocess.stderr.write(\n\t\t\t`[iosm] Extension modified bash command:\\n before: ${baseContext.command}\\n after: ${modified.command}\\n`,\n\t\t);\n\t}\n\tif (modified.cwd !== baseContext.cwd) {\n\t\tprocess.stderr.write(`[iosm] Extension modified bash cwd: ${baseContext.cwd} → ${modified.cwd}\\n`);\n\t}\n\n\treturn {\n\t\t...modified,\n\t\tcommand: adaptCommandForShell(modified.command),\n\t};\n}\n\nexport interface BashToolOptions {\n\t/** Custom operations for command execution. Default: local shell */\n\toperations?: BashOperations;\n\t/** Command prefix prepended to every command (e.g., \"shopt -s expand_aliases\" for alias support) */\n\tcommandPrefix?: string;\n\t/** Hook to adjust command, cwd, or env before execution */\n\tspawnHook?: BashSpawnHook;\n\t/** Optional permission guard executed before command starts */\n\tpermissionGuard?: ToolPermissionGuard;\n}\n\nexport function createBashTool(cwd: string, options?: BashToolOptions): AgentTool<typeof bashSchema> {\n\tconst ops = options?.operations ?? defaultBashOperations;\n\tconst commandPrefix = options?.commandPrefix;\n\tconst spawnHook = options?.spawnHook;\n\tconst permissionGuard = options?.permissionGuard;\n\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,\n\t\tparameters: bashSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ command, timeout, run_in_background }: { command: string; timeout?: number; run_in_background?: boolean },\n\t\t\tsignal?: AbortSignal,\n\t\t\tonUpdate?,\n\t\t) => {\n\t\t\tif (permissionGuard) {\n\t\t\t\tconst allowed = await permissionGuard({\n\t\t\t\t\ttoolName: \"bash\",\n\t\t\t\t\tcwd,\n\t\t\t\t\tinput: { command, timeout, run_in_background },\n\t\t\t\t\tsummary: command.trim().slice(0, 200),\n\t\t\t\t});\n\t\t\t\tif (!allowed) {\n\t\t\t\t\tthrow new Error(\"Permission denied for bash command.\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Apply command prefix if configured (e.g., \"shopt -s expand_aliases\" for alias support)\n\t\t\tconst resolvedCommand = commandPrefix ? `${commandPrefix}\\n${command}` : command;\n\t\t\tconst spawnContext = resolveSpawnContext(resolvedCommand, cwd, spawnHook);\n\t\t\tif (run_in_background) {\n\t\t\t\tif (ops !== defaultBashOperations) {\n\t\t\t\t\tthrow new Error(\"Background bash is not supported with custom execution operations.\");\n\t\t\t\t}\n\t\t\t\tconst background = startBackgroundProcess({\n\t\t\t\t\trootCwd: cwd,\n\t\t\t\t\tcwd: spawnContext.cwd,\n\t\t\t\t\tcommand: spawnContext.command,\n\t\t\t\t\tsource: \"tool\",\n\t\t\t\t});\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: `Started background process ${background.id} (pid ${background.pid}). Use /bg status ${background.id}, /bg logs ${background.id}, or /bg stop ${background.id}.`,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\tbackgroundTaskId: background.id,\n\t\t\t\t\t\tbackgroundStatusPath: background.metaPath,\n\t\t\t\t\t\tbackgroundLogPath: background.logPath,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\t// We'll stream to a temp file if output gets large\n\t\t\t\tlet tempFilePath: string | undefined;\n\t\t\t\tlet tempFileStream: ReturnType<typeof createWriteStream> | undefined;\n\t\t\t\tlet totalBytes = 0;\n\n\t\t\t\tconst decoder = new TextDecoder();\n\t\t\t\t// Keep a rolling buffer of recent sanitized output\n\t\t\t\tconst chunks: string[] = [];\n\t\t\t\tlet rollingOutput = \"\";\n\t\t\t\tlet chunksBytes = 0;\n\t\t\t\t// Keep more than we need so we have enough context for tail truncation\n\t\t\t\tconst maxChunksBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t\t\tconst pushText = (text: string) => {\n\t\t\t\t\tif (!text) {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Start writing to temp file once we exceed the threshold\n\t\t\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t\t\t// Write all buffered chunks to the file\n\t\t\t\t\t\tfor (const chunk of chunks) {\n\t\t\t\t\t\t\ttempFileStream.write(chunk);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Write to temp file if we have one\n\t\t\t\t\tif (tempFileStream) {\n\t\t\t\t\t\ttempFileStream.write(text);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Keep rolling buffer of recent data\n\t\t\t\t\tchunks.push(text);\n\t\t\t\t\trollingOutput += text;\n\t\t\t\t\tchunksBytes += Buffer.byteLength(text, \"utf-8\");\n\n\t\t\t\t\t// Trim old chunks if buffer is too large\n\t\t\t\t\twhile (chunksBytes > maxChunksBytes && chunks.length > 1) {\n\t\t\t\t\t\tconst removed = chunks.shift()!;\n\t\t\t\t\t\tchunksBytes -= Buffer.byteLength(removed, \"utf-8\");\n\t\t\t\t\t\trollingOutput = rollingOutput.slice(removed.length);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Stream partial output to callback (truncated rolling buffer)\n\t\t\t\t\tif (onUpdate) {\n\t\t\t\t\t\tconst truncation = truncateTail(rollingOutput);\n\t\t\t\t\t\tonUpdate({\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: truncation.content || \"\" }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\ttruncation: truncation.truncated ? truncation : undefined,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tconst handleData = (data: Buffer) => {\n\t\t\t\t\ttotalBytes += data.length;\n\t\t\t\t\tpushText(sanitizeBashChunk(decoder.decode(data, { stream: true })));\n\t\t\t\t};\n\n\t\t\t\tconst flushDecoder = () => {\n\t\t\t\t\tpushText(sanitizeBashChunk(decoder.decode()));\n\t\t\t\t};\n\n\t\t\t\tops.exec(spawnContext.command, spawnContext.cwd, {\n\t\t\t\t\tonData: handleData,\n\t\t\t\t\tsignal,\n\t\t\t\t\ttimeout,\n\t\t\t\t\tenv: spawnContext.env,\n\t\t\t\t})\n\t\t\t\t\t.then(({ exitCode }) => {\n\t\t\t\t\t\tflushDecoder();\n\n\t\t\t\t\t\t// Close temp file stream\n\t\t\t\t\t\tif (tempFileStream) {\n\t\t\t\t\t\t\ttempFileStream.end();\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Apply tail truncation\n\t\t\t\t\t\tconst truncation = truncateTail(rollingOutput);\n\t\t\t\t\t\tlet outputText = truncation.content || \"(no output)\";\n\n\t\t\t\t\t\t// Build details with truncation info\n\t\t\t\t\t\tlet details: BashToolDetails | undefined;\n\n\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\tdetails = {\n\t\t\t\t\t\t\t\ttruncation,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\t// Build actionable notice\n\t\t\t\t\t\t\tconst startLine = truncation.totalLines - truncation.outputLines + 1;\n\t\t\t\t\t\t\tconst endLine = truncation.totalLines;\n\n\t\t\t\t\t\t\tif (truncation.lastLinePartial) {\n\t\t\t\t\t\t\t\t// Edge case: last line alone > 30KB\n\t\t\t\t\t\t\t\tconst lastLineSize = formatSize(\n\t\t\t\t\t\t\t\t\tBuffer.byteLength(rollingOutput.split(\"\\n\").pop() || \"\", \"utf-8\"),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else if (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (exitCode !== 0 && exitCode !== null) {\n\t\t\t\t\t\t\toutputText += `\\n\\nCommand exited with code ${exitCode}`;\n\t\t\t\t\t\t\treject(new Error(outputText));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: outputText }], details });\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err: Error) => {\n\t\t\t\t\t\tflushDecoder();\n\n\t\t\t\t\t\t// Close temp file stream\n\t\t\t\t\t\tif (tempFileStream) {\n\t\t\t\t\t\t\ttempFileStream.end();\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet output = rollingOutput;\n\n\t\t\t\t\t\tif (err.message === \"aborted\") {\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += \"Command aborted\";\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else if (err.message.startsWith(\"timeout:\")) {\n\t\t\t\t\t\t\tconst timeoutSecs = err.message.split(\":\")[1];\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += `Command timed out after ${timeoutSecs} seconds`;\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t};\n}\n\n/** Default bash tool using process.cwd() - for backwards compatibility */\nexport const bashTool = createBashTool(process.cwd());\n"]}
|
package/dist/utils/shell.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export type WindowsCommandAdapter = "none" | "cmd" | "powershell";
|
|
2
|
+
export declare function resolveWindowsCommandAdapter(command: string, platform?: NodeJS.Platform): WindowsCommandAdapter;
|
|
3
|
+
/**
|
|
4
|
+
* On Windows we execute through bash by default. If the incoming command is
|
|
5
|
+
* clearly written for cmd.exe or PowerShell syntax, wrap it in the matching
|
|
6
|
+
* interpreter so users can paste native commands without manual rewriting.
|
|
7
|
+
*/
|
|
8
|
+
export declare function adaptCommandForShell(command: string, platform?: NodeJS.Platform): string;
|
|
1
9
|
/**
|
|
2
10
|
* Get shell configuration based on platform.
|
|
3
11
|
* Resolution order:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["../../src/utils/shell.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["../../src/utils/shell.ts"],"names":[],"mappings":"AAiBA,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,KAAK,GAAG,YAAY,CAAC;AAMlE,wBAAgB,4BAA4B,CAC3C,OAAO,EAAE,MAAM,EACf,QAAQ,GAAE,MAAM,CAAC,QAA2B,GAC1C,qBAAqB,CAoBvB;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAM,CAAC,QAA2B,GAAG,MAAM,CAS1G;AAqCD;;;;;;GAMG;AACH,wBAAgB,cAAc,IAAI;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAA;CAAE,CAoElE;AAED,wBAAgB,WAAW,IAAI,MAAM,CAAC,UAAU,CAY/C;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CA8BxD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAwBjD"}
|
package/dist/utils/shell.js
CHANGED
|
@@ -4,6 +4,51 @@ import { spawn, spawnSync } from "child_process";
|
|
|
4
4
|
import { getBinDir, getSettingsPath } from "../config.js";
|
|
5
5
|
import { SettingsManager } from "../core/settings-manager.js";
|
|
6
6
|
let cachedShellConfig = null;
|
|
7
|
+
const WINDOWS_CMD_ENV_PATTERN = /%[A-Za-z_][A-Za-z0-9_]*%/;
|
|
8
|
+
const WINDOWS_CMD_BUILTIN_PATTERN = /^\s*@?\s*(?:dir|copy|xcopy|move|ren|rename|del|erase|type|set|cls|mkdir|md|rmdir|rd|where)\b/i;
|
|
9
|
+
const WINDOWS_DRIVE_PATH_PATTERN = /(?:^|[\s"'`])(?:[A-Za-z]:\\)/;
|
|
10
|
+
const WINDOWS_POWERSHELL_ENV_PATTERN = /\$env:[A-Za-z_][A-Za-z0-9_]*/i;
|
|
11
|
+
const WINDOWS_POWERSHELL_CMDLET_PATTERN = /^\s*(?:Get|Set|New|Remove|Invoke|Test|Write|Select|Where|ForEach)-[A-Za-z]/i;
|
|
12
|
+
const WINDOWS_POWERSHELL_VAR_ASSIGNMENT_PATTERN = /^\s*\$[A-Za-z_][A-Za-z0-9_]*\s*=/;
|
|
13
|
+
const WINDOWS_EXPLICIT_SHELL_PATTERN = /^\s*(?:cmd(?:\.exe)?|powershell(?:\.exe)?|pwsh(?:\.exe)?)\b/i;
|
|
14
|
+
function quotePosixShellArg(value) {
|
|
15
|
+
return `'${value.replace(/'/g, `'\"'\"'`)}'`;
|
|
16
|
+
}
|
|
17
|
+
export function resolveWindowsCommandAdapter(command, platform = process.platform) {
|
|
18
|
+
if (platform !== "win32")
|
|
19
|
+
return "none";
|
|
20
|
+
const trimmed = command.trim();
|
|
21
|
+
if (!trimmed)
|
|
22
|
+
return "none";
|
|
23
|
+
if (WINDOWS_EXPLICIT_SHELL_PATTERN.test(trimmed))
|
|
24
|
+
return "none";
|
|
25
|
+
const looksLikePowerShell = WINDOWS_POWERSHELL_ENV_PATTERN.test(trimmed) ||
|
|
26
|
+
WINDOWS_POWERSHELL_CMDLET_PATTERN.test(trimmed) ||
|
|
27
|
+
WINDOWS_POWERSHELL_VAR_ASSIGNMENT_PATTERN.test(trimmed);
|
|
28
|
+
if (looksLikePowerShell)
|
|
29
|
+
return "powershell";
|
|
30
|
+
const looksLikeCmd = WINDOWS_CMD_ENV_PATTERN.test(trimmed) ||
|
|
31
|
+
WINDOWS_CMD_BUILTIN_PATTERN.test(trimmed) ||
|
|
32
|
+
WINDOWS_DRIVE_PATH_PATTERN.test(trimmed);
|
|
33
|
+
if (looksLikeCmd)
|
|
34
|
+
return "cmd";
|
|
35
|
+
return "none";
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* On Windows we execute through bash by default. If the incoming command is
|
|
39
|
+
* clearly written for cmd.exe or PowerShell syntax, wrap it in the matching
|
|
40
|
+
* interpreter so users can paste native commands without manual rewriting.
|
|
41
|
+
*/
|
|
42
|
+
export function adaptCommandForShell(command, platform = process.platform) {
|
|
43
|
+
const adapter = resolveWindowsCommandAdapter(command, platform);
|
|
44
|
+
if (adapter === "cmd") {
|
|
45
|
+
return `cmd.exe /d /s /c ${quotePosixShellArg(command)}`;
|
|
46
|
+
}
|
|
47
|
+
if (adapter === "powershell") {
|
|
48
|
+
return `powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command ${quotePosixShellArg(command)}`;
|
|
49
|
+
}
|
|
50
|
+
return command;
|
|
51
|
+
}
|
|
7
52
|
/**
|
|
8
53
|
* Find bash executable on PATH (cross-platform)
|
|
9
54
|
*/
|
package/dist/utils/shell.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shell.js","sourceRoot":"","sources":["../../src/utils/shell.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,IAAI,iBAAiB,GAA6C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,IAAI,CAAC,EAAE,CAAC,CAAC;AACZ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW;IAC1C,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QAClC,+CAA+C;QAC/C,IAAI,CAAC;YACJ,KAAK,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE;gBACpD,KAAK,EAAE,QAAQ;gBACf,QAAQ,EAAE,IAAI;aACd,CAAC,CAAC;QACJ,CAAC;QAAC,MAAM,CAAC;YACR,kCAAkC;QACnC,CAAC;IACF,CAAC;SAAM,CAAC;QACP,gCAAgC;QAChC,IAAI,CAAC;YACJ,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,iEAAiE;YACjE,IAAI,CAAC;gBACJ,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC;gBACR,uBAAuB;YACxB,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { existsSync } from \"node:fs\";\nimport { delimiter } from \"node:path\";\nimport { spawn, spawnSync } from \"child_process\";\nimport { getBinDir, getSettingsPath } from \"../config.js\";\nimport { SettingsManager } from \"../core/settings-manager.js\";\n\nlet cachedShellConfig: { shell: string; args: string[] } | null = null;\n\n/**\n * Find bash executable on PATH (cross-platform)\n */\nfunction findBashOnPath(): string | null {\n\tif (process.platform === \"win32\") {\n\t\t// Windows: Use 'where' and verify file exists (where can return non-existent paths)\n\t\ttry {\n\t\t\tconst result = spawnSync(\"where\", [\"bash.exe\"], { encoding: \"utf-8\", timeout: 5000 });\n\t\t\tif (result.status === 0 && result.stdout) {\n\t\t\t\tconst firstMatch = result.stdout.trim().split(/\\r?\\n/)[0];\n\t\t\t\tif (firstMatch && existsSync(firstMatch)) {\n\t\t\t\t\treturn firstMatch;\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\t\treturn null;\n\t}\n\n\t// Unix: Use 'which' and trust its output (handles Termux and special filesystems)\n\ttry {\n\t\tconst result = spawnSync(\"which\", [\"bash\"], { encoding: \"utf-8\", timeout: 5000 });\n\t\tif (result.status === 0 && result.stdout) {\n\t\t\tconst firstMatch = result.stdout.trim().split(/\\r?\\n/)[0];\n\t\t\tif (firstMatch) {\n\t\t\t\treturn firstMatch;\n\t\t\t}\n\t\t}\n\t} catch {\n\t\t// Ignore errors\n\t}\n\treturn null;\n}\n\n/**\n * Get shell configuration based on platform.\n * Resolution order:\n * 1. User-specified shellPath in settings.json\n * 2. On Windows: Git Bash in known locations, then bash on PATH\n * 3. On Unix: /bin/bash, then bash on PATH, then fallback to sh\n */\nexport function getShellConfig(): { shell: string; args: string[] } {\n\tif (cachedShellConfig) {\n\t\treturn cachedShellConfig;\n\t}\n\n\tconst settings = SettingsManager.create();\n\tconst customShellPath = settings.getShellPath();\n\n\t// 1. Check user-specified shell path\n\tif (customShellPath) {\n\t\tif (existsSync(customShellPath)) {\n\t\t\tcachedShellConfig = { shell: customShellPath, args: [\"-c\"] };\n\t\t\treturn cachedShellConfig;\n\t\t}\n\t\tthrow new Error(\n\t\t\t`Custom shell path not found: ${customShellPath}\\nPlease update shellPath in ${getSettingsPath()}`,\n\t\t);\n\t}\n\n\tif (process.platform === \"win32\") {\n\t\t// 2. Try Git Bash in known locations\n\t\tconst paths: string[] = [];\n\t\tconst programFiles = process.env.ProgramFiles;\n\t\tif (programFiles) {\n\t\t\tpaths.push(`${programFiles}\\\\Git\\\\bin\\\\bash.exe`);\n\t\t}\n\t\tconst programFilesX86 = process.env[\"ProgramFiles(x86)\"];\n\t\tif (programFilesX86) {\n\t\t\tpaths.push(`${programFilesX86}\\\\Git\\\\bin\\\\bash.exe`);\n\t\t}\n\n\t\tfor (const path of paths) {\n\t\t\tif (existsSync(path)) {\n\t\t\t\tcachedShellConfig = { shell: path, args: [\"-c\"] };\n\t\t\t\treturn cachedShellConfig;\n\t\t\t}\n\t\t}\n\n\t\t// 3. Fallback: search bash.exe on PATH (Cygwin, MSYS2, WSL, etc.)\n\t\tconst bashOnPath = findBashOnPath();\n\t\tif (bashOnPath) {\n\t\t\tcachedShellConfig = { shell: bashOnPath, args: [\"-c\"] };\n\t\t\treturn cachedShellConfig;\n\t\t}\n\n\t\tthrow new Error(\n\t\t\t`No bash shell found. Options:\\n` +\n\t\t\t\t` 1. Install Git for Windows: https://git-scm.com/download/win\\n` +\n\t\t\t\t` 2. Add your bash to PATH (Cygwin, MSYS2, etc.)\\n` +\n\t\t\t\t` 3. Set shellPath in ${getSettingsPath()}\\n\\n` +\n\t\t\t\t`Searched Git Bash in:\\n${paths.map((p) => ` ${p}`).join(\"\\n\")}`,\n\t\t);\n\t}\n\n\t// Unix: try /bin/bash, then bash on PATH, then fallback to sh\n\tif (existsSync(\"/bin/bash\")) {\n\t\tcachedShellConfig = { shell: \"/bin/bash\", args: [\"-c\"] };\n\t\treturn cachedShellConfig;\n\t}\n\n\tconst bashOnPath = findBashOnPath();\n\tif (bashOnPath) {\n\t\tcachedShellConfig = { shell: bashOnPath, args: [\"-c\"] };\n\t\treturn cachedShellConfig;\n\t}\n\n\tcachedShellConfig = { shell: \"sh\", args: [\"-c\"] };\n\treturn cachedShellConfig;\n}\n\nexport function getShellEnv(): NodeJS.ProcessEnv {\n\tconst binDir = getBinDir();\n\tconst pathKey = Object.keys(process.env).find((key) => key.toLowerCase() === \"path\") ?? \"PATH\";\n\tconst currentPath = process.env[pathKey] ?? \"\";\n\tconst pathEntries = currentPath.split(delimiter).filter(Boolean);\n\tconst hasBinDir = pathEntries.includes(binDir);\n\tconst updatedPath = hasBinDir ? currentPath : [binDir, currentPath].filter(Boolean).join(delimiter);\n\n\treturn {\n\t\t...process.env,\n\t\t[pathKey]: updatedPath,\n\t};\n}\n\n/**\n * Sanitize binary output for display/storage.\n * Removes characters that crash string-width or cause display issues:\n * - Control characters (except tab, newline, carriage return)\n * - Lone surrogates\n * - Unicode Format characters (crash string-width due to a bug)\n * - Characters with undefined code points\n */\nexport function sanitizeBinaryOutput(str: string): string {\n\t// Use Array.from to properly iterate over code points (not code units)\n\t// This handles surrogate pairs correctly and catches edge cases where\n\t// codePointAt() might return undefined\n\treturn Array.from(str)\n\t\t.filter((char) => {\n\t\t\t// Filter out characters that cause string-width to crash\n\t\t\t// This includes:\n\t\t\t// - Unicode format characters\n\t\t\t// - Lone surrogates (already filtered by Array.from)\n\t\t\t// - Control chars except \\t \\n \\r\n\t\t\t// - Characters with undefined code points\n\n\t\t\tconst code = char.codePointAt(0);\n\n\t\t\t// Skip if code point is undefined (edge case with invalid strings)\n\t\t\tif (code === undefined) return false;\n\n\t\t\t// Allow tab, newline, carriage return\n\t\t\tif (code === 0x09 || code === 0x0a || code === 0x0d) return true;\n\n\t\t\t// Filter out control characters (0x00-0x1F, except 0x09, 0x0a, 0x0x0d)\n\t\t\tif (code <= 0x1f) return false;\n\n\t\t\t// Filter out Unicode format characters\n\t\t\tif (code >= 0xfff9 && code <= 0xfffb) return false;\n\n\t\t\treturn true;\n\t\t})\n\t\t.join(\"\");\n}\n\n/**\n * Kill a process and all its children (cross-platform)\n */\nexport function killProcessTree(pid: number): void {\n\tif (process.platform === \"win32\") {\n\t\t// Use taskkill on Windows to kill process tree\n\t\ttry {\n\t\t\tspawn(\"taskkill\", [\"/F\", \"/T\", \"/PID\", String(pid)], {\n\t\t\t\tstdio: \"ignore\",\n\t\t\t\tdetached: true,\n\t\t\t});\n\t\t} catch {\n\t\t\t// Ignore errors if taskkill fails\n\t\t}\n\t} else {\n\t\t// Use SIGKILL on Unix/Linux/Mac\n\t\ttry {\n\t\t\tprocess.kill(-pid, \"SIGKILL\");\n\t\t} catch {\n\t\t\t// Fallback to killing just the child if process group kill fails\n\t\t\ttry {\n\t\t\t\tprocess.kill(pid, \"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C;QACP,gCAAgC;QAChC,IAAI,CAAC;YACJ,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,iEAAiE;YACjE,IAAI,CAAC;gBACJ,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC;gBACR,uBAAuB;YACxB,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { existsSync } from \"node:fs\";\nimport { delimiter } from \"node:path\";\nimport { spawn, spawnSync } from \"child_process\";\nimport { getBinDir, getSettingsPath } from \"../config.js\";\nimport { SettingsManager } from \"../core/settings-manager.js\";\n\nlet cachedShellConfig: { shell: string; args: string[] } | null = null;\n\nconst WINDOWS_CMD_ENV_PATTERN = /%[A-Za-z_][A-Za-z0-9_]*%/;\nconst WINDOWS_CMD_BUILTIN_PATTERN =\n\t/^\\s*@?\\s*(?:dir|copy|xcopy|move|ren|rename|del|erase|type|set|cls|mkdir|md|rmdir|rd|where)\\b/i;\nconst WINDOWS_DRIVE_PATH_PATTERN = /(?:^|[\\s\"'`])(?:[A-Za-z]:\\\\)/;\nconst WINDOWS_POWERSHELL_ENV_PATTERN = /\\$env:[A-Za-z_][A-Za-z0-9_]*/i;\nconst WINDOWS_POWERSHELL_CMDLET_PATTERN = /^\\s*(?:Get|Set|New|Remove|Invoke|Test|Write|Select|Where|ForEach)-[A-Za-z]/i;\nconst WINDOWS_POWERSHELL_VAR_ASSIGNMENT_PATTERN = /^\\s*\\$[A-Za-z_][A-Za-z0-9_]*\\s*=/;\nconst WINDOWS_EXPLICIT_SHELL_PATTERN = /^\\s*(?:cmd(?:\\.exe)?|powershell(?:\\.exe)?|pwsh(?:\\.exe)?)\\b/i;\n\nexport type WindowsCommandAdapter = \"none\" | \"cmd\" | \"powershell\";\n\nfunction quotePosixShellArg(value: string): string {\n\treturn `'${value.replace(/'/g, `'\\\"'\\\"'`)}'`;\n}\n\nexport function resolveWindowsCommandAdapter(\n\tcommand: string,\n\tplatform: NodeJS.Platform = process.platform,\n): WindowsCommandAdapter {\n\tif (platform !== \"win32\") return \"none\";\n\n\tconst trimmed = command.trim();\n\tif (!trimmed) return \"none\";\n\tif (WINDOWS_EXPLICIT_SHELL_PATTERN.test(trimmed)) return \"none\";\n\n\tconst looksLikePowerShell =\n\t\tWINDOWS_POWERSHELL_ENV_PATTERN.test(trimmed) ||\n\t\tWINDOWS_POWERSHELL_CMDLET_PATTERN.test(trimmed) ||\n\t\tWINDOWS_POWERSHELL_VAR_ASSIGNMENT_PATTERN.test(trimmed);\n\tif (looksLikePowerShell) return \"powershell\";\n\n\tconst looksLikeCmd =\n\t\tWINDOWS_CMD_ENV_PATTERN.test(trimmed) ||\n\t\tWINDOWS_CMD_BUILTIN_PATTERN.test(trimmed) ||\n\t\tWINDOWS_DRIVE_PATH_PATTERN.test(trimmed);\n\tif (looksLikeCmd) return \"cmd\";\n\n\treturn \"none\";\n}\n\n/**\n * On Windows we execute through bash by default. If the incoming command is\n * clearly written for cmd.exe or PowerShell syntax, wrap it in the matching\n * interpreter so users can paste native commands without manual rewriting.\n */\nexport function adaptCommandForShell(command: string, platform: NodeJS.Platform = process.platform): string {\n\tconst adapter = resolveWindowsCommandAdapter(command, platform);\n\tif (adapter === \"cmd\") {\n\t\treturn `cmd.exe /d /s /c ${quotePosixShellArg(command)}`;\n\t}\n\tif (adapter === \"powershell\") {\n\t\treturn `powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command ${quotePosixShellArg(command)}`;\n\t}\n\treturn command;\n}\n\n/**\n * Find bash executable on PATH (cross-platform)\n */\nfunction findBashOnPath(): string | null {\n\tif (process.platform === \"win32\") {\n\t\t// Windows: Use 'where' and verify file exists (where can return non-existent paths)\n\t\ttry {\n\t\t\tconst result = spawnSync(\"where\", [\"bash.exe\"], { encoding: \"utf-8\", timeout: 5000 });\n\t\t\tif (result.status === 0 && result.stdout) {\n\t\t\t\tconst firstMatch = result.stdout.trim().split(/\\r?\\n/)[0];\n\t\t\t\tif (firstMatch && existsSync(firstMatch)) {\n\t\t\t\t\treturn firstMatch;\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\t\treturn null;\n\t}\n\n\t// Unix: Use 'which' and trust its output (handles Termux and special filesystems)\n\ttry {\n\t\tconst result = spawnSync(\"which\", [\"bash\"], { encoding: \"utf-8\", timeout: 5000 });\n\t\tif (result.status === 0 && result.stdout) {\n\t\t\tconst firstMatch = result.stdout.trim().split(/\\r?\\n/)[0];\n\t\t\tif (firstMatch) {\n\t\t\t\treturn firstMatch;\n\t\t\t}\n\t\t}\n\t} catch {\n\t\t// Ignore errors\n\t}\n\treturn null;\n}\n\n/**\n * Get shell configuration based on platform.\n * Resolution order:\n * 1. User-specified shellPath in settings.json\n * 2. On Windows: Git Bash in known locations, then bash on PATH\n * 3. On Unix: /bin/bash, then bash on PATH, then fallback to sh\n */\nexport function getShellConfig(): { shell: string; args: string[] } {\n\tif (cachedShellConfig) {\n\t\treturn cachedShellConfig;\n\t}\n\n\tconst settings = SettingsManager.create();\n\tconst customShellPath = settings.getShellPath();\n\n\t// 1. Check user-specified shell path\n\tif (customShellPath) {\n\t\tif (existsSync(customShellPath)) {\n\t\t\tcachedShellConfig = { shell: customShellPath, args: [\"-c\"] };\n\t\t\treturn cachedShellConfig;\n\t\t}\n\t\tthrow new Error(\n\t\t\t`Custom shell path not found: ${customShellPath}\\nPlease update shellPath in ${getSettingsPath()}`,\n\t\t);\n\t}\n\n\tif (process.platform === \"win32\") {\n\t\t// 2. Try Git Bash in known locations\n\t\tconst paths: string[] = [];\n\t\tconst programFiles = process.env.ProgramFiles;\n\t\tif (programFiles) {\n\t\t\tpaths.push(`${programFiles}\\\\Git\\\\bin\\\\bash.exe`);\n\t\t}\n\t\tconst programFilesX86 = process.env[\"ProgramFiles(x86)\"];\n\t\tif (programFilesX86) {\n\t\t\tpaths.push(`${programFilesX86}\\\\Git\\\\bin\\\\bash.exe`);\n\t\t}\n\n\t\tfor (const path of paths) {\n\t\t\tif (existsSync(path)) {\n\t\t\t\tcachedShellConfig = { shell: path, args: [\"-c\"] };\n\t\t\t\treturn cachedShellConfig;\n\t\t\t}\n\t\t}\n\n\t\t// 3. Fallback: search bash.exe on PATH (Cygwin, MSYS2, WSL, etc.)\n\t\tconst bashOnPath = findBashOnPath();\n\t\tif (bashOnPath) {\n\t\t\tcachedShellConfig = { shell: bashOnPath, args: [\"-c\"] };\n\t\t\treturn cachedShellConfig;\n\t\t}\n\n\t\tthrow new Error(\n\t\t\t`No bash shell found. Options:\\n` +\n\t\t\t\t` 1. Install Git for Windows: https://git-scm.com/download/win\\n` +\n\t\t\t\t` 2. Add your bash to PATH (Cygwin, MSYS2, etc.)\\n` +\n\t\t\t\t` 3. Set shellPath in ${getSettingsPath()}\\n\\n` +\n\t\t\t\t`Searched Git Bash in:\\n${paths.map((p) => ` ${p}`).join(\"\\n\")}`,\n\t\t);\n\t}\n\n\t// Unix: try /bin/bash, then bash on PATH, then fallback to sh\n\tif (existsSync(\"/bin/bash\")) {\n\t\tcachedShellConfig = { shell: \"/bin/bash\", args: [\"-c\"] };\n\t\treturn cachedShellConfig;\n\t}\n\n\tconst bashOnPath = findBashOnPath();\n\tif (bashOnPath) {\n\t\tcachedShellConfig = { shell: bashOnPath, args: [\"-c\"] };\n\t\treturn cachedShellConfig;\n\t}\n\n\tcachedShellConfig = { shell: \"sh\", args: [\"-c\"] };\n\treturn cachedShellConfig;\n}\n\nexport function getShellEnv(): NodeJS.ProcessEnv {\n\tconst binDir = getBinDir();\n\tconst pathKey = Object.keys(process.env).find((key) => key.toLowerCase() === \"path\") ?? \"PATH\";\n\tconst currentPath = process.env[pathKey] ?? \"\";\n\tconst pathEntries = currentPath.split(delimiter).filter(Boolean);\n\tconst hasBinDir = pathEntries.includes(binDir);\n\tconst updatedPath = hasBinDir ? currentPath : [binDir, currentPath].filter(Boolean).join(delimiter);\n\n\treturn {\n\t\t...process.env,\n\t\t[pathKey]: updatedPath,\n\t};\n}\n\n/**\n * Sanitize binary output for display/storage.\n * Removes characters that crash string-width or cause display issues:\n * - Control characters (except tab, newline, carriage return)\n * - Lone surrogates\n * - Unicode Format characters (crash string-width due to a bug)\n * - Characters with undefined code points\n */\nexport function sanitizeBinaryOutput(str: string): string {\n\t// Use Array.from to properly iterate over code points (not code units)\n\t// This handles surrogate pairs correctly and catches edge cases where\n\t// codePointAt() might return undefined\n\treturn Array.from(str)\n\t\t.filter((char) => {\n\t\t\t// Filter out characters that cause string-width to crash\n\t\t\t// This includes:\n\t\t\t// - Unicode format characters\n\t\t\t// - Lone surrogates (already filtered by Array.from)\n\t\t\t// - Control chars except \\t \\n \\r\n\t\t\t// - Characters with undefined code points\n\n\t\t\tconst code = char.codePointAt(0);\n\n\t\t\t// Skip if code point is undefined (edge case with invalid strings)\n\t\t\tif (code === undefined) return false;\n\n\t\t\t// Allow tab, newline, carriage return\n\t\t\tif (code === 0x09 || code === 0x0a || code === 0x0d) return true;\n\n\t\t\t// Filter out control characters (0x00-0x1F, except 0x09, 0x0a, 0x0x0d)\n\t\t\tif (code <= 0x1f) return false;\n\n\t\t\t// Filter out Unicode format characters\n\t\t\tif (code >= 0xfff9 && code <= 0xfffb) return false;\n\n\t\t\treturn true;\n\t\t})\n\t\t.join(\"\");\n}\n\n/**\n * Kill a process and all its children (cross-platform)\n */\nexport function killProcessTree(pid: number): void {\n\tif (process.platform === \"win32\") {\n\t\t// Use taskkill on Windows to kill process tree\n\t\ttry {\n\t\t\tspawn(\"taskkill\", [\"/F\", \"/T\", \"/PID\", String(pid)], {\n\t\t\t\tstdio: \"ignore\",\n\t\t\t\tdetached: true,\n\t\t\t});\n\t\t} catch {\n\t\t\t// Ignore errors if taskkill fails\n\t\t}\n\t} else {\n\t\t// Use SIGKILL on Unix/Linux/Mac\n\t\ttry {\n\t\t\tprocess.kill(-pid, \"SIGKILL\");\n\t\t} catch {\n\t\t\t// Fallback to killing just the child if process group kill fails\n\t\t\ttry {\n\t\t\t\tprocess.kill(pid, \"SIGKILL\");\n\t\t\t} catch {\n\t\t\t\t// Process already dead\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
|
package/docs/configuration.md
CHANGED
|
@@ -354,6 +354,8 @@ export IOSM_SESSION_TRACE_DIR="$HOME/.iosm/traces"
|
|
|
354
354
|
# export IOSM_OFFLINE=1
|
|
355
355
|
```
|
|
356
356
|
|
|
357
|
+
Windows note: when IOSM runs through Git Bash, bash tool execution now auto-adapts native Windows command syntax (`%LOCALAPPDATA%`, `dir`, `C:\...`, `$env:...`, `Get-*`) to the correct interpreter (`cmd.exe` or `powershell.exe`) automatically.
|
|
358
|
+
|
|
357
359
|
---
|
|
358
360
|
|
|
359
361
|
## Profiles
|