2026-02-08 15:01:42 +09:00
|
|
|
function delay(milliseconds: number): Promise<void> {
|
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, milliseconds))
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 19:31:04 +09:00
|
|
|
async function readStream(stream: ReadableStream<Uint8Array> | null | undefined): Promise<string> {
|
|
|
|
|
return stream ? new Response(stream).text() : ""
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
export async function closeTmuxPane(paneId: string): Promise<boolean> {
|
2026-04-18 19:31:04 +09:00
|
|
|
const [{ log }, { isInsideTmux }, { getTmuxPath }, { spawn }] = await Promise.all([
|
|
|
|
|
import("../../logger"),
|
|
|
|
|
import("./environment"),
|
|
|
|
|
import("../../../tools/interactive-bash/tmux-path-resolver"),
|
|
|
|
|
import("./spawn-process"),
|
|
|
|
|
])
|
2026-02-08 15:01:42 +09:00
|
|
|
|
|
|
|
|
if (!isInsideTmux()) {
|
|
|
|
|
log("[closeTmuxPane] SKIP: not inside tmux")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tmux = await getTmuxPath()
|
|
|
|
|
if (!tmux) {
|
|
|
|
|
log("[closeTmuxPane] SKIP: tmux not found")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log("[closeTmuxPane] sending Ctrl+C for graceful shutdown", { paneId })
|
|
|
|
|
const ctrlCProc = spawn([tmux, "send-keys", "-t", paneId, "C-c"], {
|
2026-04-18 19:31:04 +09:00
|
|
|
stdout: "ignore",
|
|
|
|
|
stderr: "ignore",
|
2026-02-08 15:01:42 +09:00
|
|
|
})
|
|
|
|
|
await ctrlCProc.exited
|
|
|
|
|
|
|
|
|
|
await delay(250)
|
|
|
|
|
|
|
|
|
|
log("[closeTmuxPane] killing pane", { paneId })
|
|
|
|
|
|
2026-04-18 19:31:04 +09:00
|
|
|
const killPaneProc = spawn([tmux, "kill-pane", "-t", paneId], {
|
2026-02-08 15:01:42 +09:00
|
|
|
stdout: "pipe",
|
|
|
|
|
stderr: "pipe",
|
|
|
|
|
})
|
2026-04-18 19:31:04 +09:00
|
|
|
const [, stderr, exitCode] = await Promise.all([
|
|
|
|
|
readStream(killPaneProc.stdout),
|
|
|
|
|
readStream(killPaneProc.stderr),
|
|
|
|
|
killPaneProc.exited,
|
|
|
|
|
])
|
2026-02-08 15:01:42 +09:00
|
|
|
|
2026-04-18 19:34:54 +09:00
|
|
|
const trimmedStderr = stderr.trim()
|
|
|
|
|
const paneAlreadyGone = exitCode !== 0 && /can't find pane/i.test(trimmedStderr)
|
|
|
|
|
|
|
|
|
|
if (paneAlreadyGone) {
|
|
|
|
|
log("[closeTmuxPane] SUCCESS (pane already closed by Ctrl+C)", { paneId })
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
if (exitCode !== 0) {
|
2026-04-18 19:34:54 +09:00
|
|
|
log("[closeTmuxPane] FAILED", { paneId, exitCode, stderr: trimmedStderr })
|
|
|
|
|
return false
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-18 19:34:54 +09:00
|
|
|
log("[closeTmuxPane] SUCCESS", { paneId })
|
|
|
|
|
return true
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|