@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
99 lines • 5.73 kB
TypeScript
import { type WindowsProcessRow } from "@earendil-works/pi-agent-core/node";
/** Family of a resolved shell executable, used to pick invocation arguments. */
export type ShellKind = "bash" | "sh" | "cmd" | "powershell";
export interface ShellConfig {
shell: string;
args: string[];
commandTransport?: "argv" | "stdin";
/** Detected shell family. Lets PTY callers pass the right command transport per shell. */
kind?: ShellKind;
}
/** Environment variable that overrides shell resolution with an explicit bash path (Windows-first). */
export declare const GIT_BASH_PATH_ENV = "SENPI_GIT_BASH_PATH";
/** Classify a shell executable by its basename so non-bash shells get correct args. */
export declare function resolveShellKind(shellPath: string): ShellKind;
/**
* Resolve shell configuration based on platform and an optional explicit shell path.
* Resolution order:
* 1. User-specified shellPath
* 2. On Windows: Git Bash in known locations, then bash on PATH
* 3. On Unix: /bin/bash, then bash on PATH, then fallback to sh
*/
export declare function getShellConfig(customShellPath?: string): ShellConfig;
export declare const POWERSHELL_ARGS: readonly ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command"];
/** Resolve PowerShell on Windows, preferring PowerShell 7 when available. */
export declare function getPowerShellConfig(): ShellConfig;
/**
* The environment agent-spawned shells get: the user's shell environment with declared credentials withheld
* (senpi#6820), plus the kill guard's runtime layer.
*/
export declare function getShellEnv(): NodeJS.ProcessEnv;
/**
* The environment for a command the user typed (`!cmd`): no kill guard runtime layer, which judges only what the agent
* runs (senpi#5349).
*/
export declare function getUserShellEnv(): NodeJS.ProcessEnv;
/**
* Sanitize binary output for display/storage.
* Removes characters that crash string-width or cause display issues:
* - Control characters (except tab, newline, carriage return)
* - Unicode interlinear annotation characters U+FFF9..U+FFFB (crash string-width due to a bug)
*/
export declare function sanitizeBinaryOutput(str: string): string;
/** A detached child we own until its whole process group is gone. */
export interface TrackedDetachedChild {
/** Pid of the process we spawned; on unix it is also its own process-group leader. */
readonly pid: number;
/** Process group shutdown must kill. Equal to `pid`, because we spawn detached. */
readonly pgid: number;
/** The leader exited but its group still has members, so `pid` must not be signalled. */
readonly leaderExited: boolean;
}
export declare function trackDetachedChildPid(pid: number): void;
export declare function untrackDetachedChildPid(pid: number): void;
/** Read-only snapshot of the tracked groups (diagnostics and tests; never module state). */
export declare function listTrackedDetachedChildren(): readonly TrackedDetachedChild[];
/**
* Record that a tracked child exited. Ownership is released only when its process group
* is empty; while descendants survive there, shutdown must still be able to kill them.
* Windows has no such group, so the entry is dropped on exit as before.
*/
export declare function noteDetachedChildExited(pid: number): void;
/** Drop tracked groups that have no members left, so a long session cannot accumulate entries. */
export declare function pruneTrackedDetachedChildren(): void;
export declare function killTrackedDetachedChildren(): void;
/**
* Ordered `taskkill` launchers to try, most reliable first.
*
* `spawn("taskkill", ...)` relies on a PATH lookup, so any session whose PATH lost
* `%SystemRoot%\System32` (a POSIX-style PATH inherited from a Git Bash/MSYS launcher,
* a truncated user PATH, a locked-down service account) fails to resolve it. A broken PATH
* must not cost us the process-tree kill, so every absolute System32 location that actually
* exists is tried before the bare PATH-resolved name.
*/
export declare function windowsTaskkillCandidates(env?: NodeJS.ProcessEnv): string[];
/**
* Kill a process and its descendants on Windows: one process listing (bounded by `TASKKILL_TIMEOUT_MS`)
* decides the tree, a process counting as a child only when it started at or after the parent it names,
* and `taskkill /F` ends each pid by name. `/T` would also adopt an unrelated older process through a
* recycled parent pid (senpi#2999); it is used only when no listing can be read.
*
* Synchronous on purpose. Shutdown paths call `killTrackedDetachedChildren()` and then
* `process.exit()` in the same tick (`emergencyTerminalExit()`), so neither an
* asynchronous killer nor a fallback wired to a child's `error` event would ever run and
* the tracked child would survive. `spawnSync` also reports a failed executable lookup on
* its returned `error` field instead of emitting it, so a PATH without
* `%SystemRoot%\System32` can no longer surface as an uncaught `spawn taskkill ENOENT`.
*
* The direct `process.kill` at the end is a degraded last resort reached only when no
* `taskkill.exe` can be launched at all. It maps to `TerminateProcess`, which does not
* touch descendants — the same limitation `packages/pty/src/pipe-fallback.ts` documents.
* Nothing in-process can walk a Windows process tree without an external tool, so this
* still beats leaving the whole tree running.
*/
export declare function killWindowsProcessTree(pid: number, taskkillPaths?: string[], listProcesses?: () => readonly WindowsProcessRow[] | undefined): void;
/**
* Kill a process and all its children (cross-platform)
*/
export declare function killProcessTree(pid: number): void;
//# sourceMappingURL=shell.d.ts.map