import { spawn } from "node:child_process" import { existsSync } from "node:fs" import { join } from "node:path" import { runtimeSlug } from "./sg-manifest" export const AST_GREP_BIN_DIR_ENV_KEY = "OMO_AST_GREP_BIN_DIR" export const KILL_GRACE_MS = 1_000 const AST_GREP_INSTALL_TIMEOUT_MS = 30_000 export type AstGrepInstallSpawnOutcome = | { readonly kind: "exit"; readonly code: number | null; readonly signal: NodeJS.Signals | null } | { readonly kind: "spawn-error"; readonly error: Error; readonly missingExecutable: boolean } export interface AstGrepInstallSpawnOptions { readonly cwd: string readonly env: Record } export interface AstGrepInstallSpawnedProcess { readonly kill: () => void readonly outcome: Promise } export type AstGrepInstallSpawn = ( command: string, args: readonly string[], options: AstGrepInstallSpawnOptions, ) => AstGrepInstallSpawnedProcess export type AstGrepSkillInstallResult = | { readonly kind: "succeeded" } | { readonly kind: "skipped"; readonly reason: string } | { readonly kind: "failed"; readonly reason: string } | { readonly kind: "timed-out" } export interface AstGrepSkillInstallOptions { readonly env?: Record readonly fileExists?: (filePath: string) => boolean readonly platform?: NodeJS.Platform readonly skillDir: string readonly spawnProcess?: AstGrepInstallSpawn readonly targetDir: string readonly timeoutMs?: number } export type RunAstGrepSkillInstall = (options: AstGrepSkillInstallOptions) => Promise interface InstallInvocation { readonly command: string readonly args: readonly string[] } type TimedInvocationOutcome = AstGrepInstallSpawnOutcome | { readonly kind: "timed-out" } export function astGrepRuntimeDir( baseDir: string, platform: NodeJS.Platform = process.platform, arch: string = process.arch, ): string { return join(baseDir, "runtime", "ast-grep", runtimeSlug(platform, arch)) } function isMissingExecutable(error: Error): boolean { if (!("code" in error)) return false return error.code === "ENOENT" } function defaultSpawnProcess( command: string, args: readonly string[], options: AstGrepInstallSpawnOptions, ): AstGrepInstallSpawnedProcess { const child = spawn(command, args, { cwd: options.cwd, env: options.env, stdio: "ignore", windowsHide: true, }) let settled = false const outcome = new Promise((resolve) => { const settle = (result: AstGrepInstallSpawnOutcome): void => { if (settled) return settled = true resolve(result) } child.once("error", (error: Error) => { settle({ kind: "spawn-error", error, missingExecutable: isMissingExecutable(error) }) }) child.once("exit", (code: number | null, signal: NodeJS.Signals | null) => { settle({ kind: "exit", code, signal }) }) }) return { kill: () => { if (!child.killed) child.kill() }, outcome, } } function scriptPathForPlatform(skillDir: string, platform: NodeJS.Platform): string { return join(skillDir, platform === "win32" ? "install.ps1" : "install.sh") } function invocationsForPlatform(scriptPath: string, platform: NodeJS.Platform): readonly InstallInvocation[] { if (platform !== "win32") return [{ command: "bash", args: [scriptPath] }] const args = ["-NoProfile", "-ExecutionPolicy", "Bypass", "-File", scriptPath] return [ { command: "pwsh", args }, { command: "powershell.exe", args }, ] } async function runInvocation(input: { readonly invocation: InstallInvocation readonly env: Record readonly skillDir: string readonly spawnProcess: AstGrepInstallSpawn readonly timeoutMs: number }): Promise { const child = input.spawnProcess(input.invocation.command, input.invocation.args, { cwd: input.skillDir, env: input.env, }) let timedOut = false // The timeout MUST stay ref'd: it is the only producer of progress when the // child never exits. An unref'd timer can be starved by the runtime's event // loop (observed hanging the Windows test runner for the full job ceiling), // and clearTimeout in the finally block already prevents leaks. let terminateDeadline: (() => void) | undefined let terminateDeadlineGrace: ReturnType | undefined const killIgnored = new Promise((_, reject) => { terminateDeadline = () => reject(new Error("ast-grep install child ignored termination")) }) const timeout = setTimeout(() => { timedOut = true child.kill() // Grace period for kill() to settle the outcome; a child that ignores // termination must fail loudly instead of hanging the caller forever. terminateDeadlineGrace = setTimeout(() => terminateDeadline?.(), KILL_GRACE_MS) }, input.timeoutMs) try { const outcome = await Promise.race([child.outcome, killIgnored]) if (timedOut) return { kind: "timed-out" } return outcome } catch (error) { // The child ignored termination past the grace deadline: fail loudly with // a named reason instead of hanging the caller forever. Spawn-error shape // carries the reason through the caller's failedReason mapping. if (error instanceof Error && error.message.includes("ignored termination")) { return { kind: "spawn-error", error, missingExecutable: false } } throw error } finally { clearTimeout(timeout) if (terminateDeadlineGrace !== undefined) clearTimeout(terminateDeadlineGrace) } } function failedReason( outcome: Exclude, ): string { return outcome.error.message } export async function runAstGrepSkillInstall(options: AstGrepSkillInstallOptions): Promise { const platform = options.platform ?? process.platform const fileExists = options.fileExists ?? existsSync const scriptPath = scriptPathForPlatform(options.skillDir, platform) if (!fileExists(scriptPath)) return { kind: "skipped", reason: `missing ${scriptPath}` } const env = { ...(options.env ?? process.env), [AST_GREP_BIN_DIR_ENV_KEY]: options.targetDir } const spawnProcess = options.spawnProcess ?? defaultSpawnProcess const timeoutMs = options.timeoutMs ?? AST_GREP_INSTALL_TIMEOUT_MS const invocations = invocationsForPlatform(scriptPath, platform) try { for (const invocation of invocations) { const outcome = await runInvocation({ env, invocation, skillDir: options.skillDir, spawnProcess, timeoutMs }) if (outcome.kind === "timed-out") return { kind: "timed-out" } if (outcome.kind === "exit") { if (outcome.code === 0) return { kind: "succeeded" } return { kind: "failed", reason: `${invocation.command} exited ${outcome.code ?? outcome.signal ?? "without status"}`, } } if (platform === "win32" && outcome.missingExecutable && invocation.command === "pwsh") continue return { kind: "failed", reason: failedReason(outcome) } } return { kind: "failed", reason: "no ast-grep install shell was available" } } catch (error) { if (error instanceof Error) return { kind: "failed", reason: error.message } return { kind: "failed", reason: String(error) } } }