feat(tmux): add session-spawn utility with tests

This commit is contained in:
YeonGyu-Kim
2026-04-28 10:45:10 +09:00
parent ad315987c7
commit 13b3bd9e72
2 changed files with 190 additions and 40 deletions
+26 -40
View File
@@ -1,10 +1,10 @@
import { spawn } from "../../bun-spawn-shim"
import type { TmuxConfig } from "../../../config/schema"
import { getTmuxPath } from "../../../tools/interactive-bash/tmux-path-resolver"
import type { SpawnPaneResult } from "../types"
import type { runTmuxCommand as RunTmuxCommand } from "../runner"
import { isInsideTmux } from "./environment"
import { isServerRunning } from "./server-health"
import { shellEscapeForDoubleQuotedCommand } from "../../shell-env"
import { shellSingleQuote } from "../../shell-env"
const ISOLATED_SESSION_NAME_PREFIX = "omo-agents"
@@ -15,28 +15,21 @@ export function getIsolatedSessionName(pid: number = process.pid): string {
async function getWindowDimensions(
tmux: string,
sourcePaneId: string,
runTmuxCommand: typeof RunTmuxCommand,
): Promise<{ width: number; height: number } | null> {
const proc = spawn(
[tmux, "display", "-p", "-t", sourcePaneId, "#{window_width},#{window_height}"],
{ stdout: "pipe", stderr: "pipe" },
)
const exitCode = await proc.exited
const stdout = await new Response(proc.stdout).text()
const result = await runTmuxCommand(tmux, ["display", "-p", "-t", sourcePaneId, "#{window_width},#{window_height}"])
if (exitCode !== 0) return null
if (result.exitCode !== 0) return null
const [width, height] = stdout.trim().split(",").map(Number)
const [width, height] = result.output.trim().split(",").map(Number)
if (Number.isNaN(width) || Number.isNaN(height)) return null
return { width, height }
}
async function sessionExists(tmux: string, sessionName: string): Promise<boolean> {
const proc = spawn([tmux, "has-session", "-t", sessionName], {
stdout: "ignore",
stderr: "ignore",
})
return (await proc.exited) === 0
async function sessionExists(tmux: string, sessionName: string, runTmuxCommand: typeof RunTmuxCommand): Promise<boolean> {
const result = await runTmuxCommand(tmux, ["has-session", "-t", sessionName])
return result.exitCode === 0
}
export async function spawnTmuxSession(
@@ -44,9 +37,13 @@ export async function spawnTmuxSession(
description: string,
config: TmuxConfig,
serverUrl: string,
directory: string,
sourcePaneId?: string,
): Promise<SpawnPaneResult> {
const { log } = await import("../../logger")
const [{ log }, { runTmuxCommand }] = await Promise.all([
import("../../logger"),
import("../runner"),
])
log("[spawnTmuxSession] called", {
sessionId,
@@ -78,21 +75,19 @@ export async function spawnTmuxSession(
log("[spawnTmuxSession] all checks passed, creating isolated session...")
const shell = process.env.SHELL || "/bin/sh"
const escapedUrl = shellEscapeForDoubleQuotedCommand(serverUrl)
const escapedSessionId = shellEscapeForDoubleQuotedCommand(sessionId)
const opencodeCmd = `${shell} -c "opencode attach ${escapedUrl} --session ${escapedSessionId}"`
const effectiveDirectory = directory || process.cwd()
const opencodeCmd = `opencode attach ${shellSingleQuote(serverUrl)} --session ${shellSingleQuote(sessionId)} --dir ${shellSingleQuote(effectiveDirectory)}`
const sizeArgs: string[] = []
if (sourcePaneId) {
const dims = await getWindowDimensions(tmux, sourcePaneId)
const dims = await getWindowDimensions(tmux, sourcePaneId, runTmuxCommand)
if (dims) {
sizeArgs.push("-x", String(dims.width), "-y", String(dims.height))
}
}
const isolatedSessionName = getIsolatedSessionName()
const sessionAlreadyExists = await sessionExists(tmux, isolatedSessionName)
const sessionAlreadyExists = await sessionExists(tmux, isolatedSessionName, runTmuxCommand)
const args = sessionAlreadyExists
? [
@@ -117,31 +112,22 @@ export async function spawnTmuxSession(
sessionName: isolatedSessionName,
})
const proc = spawn([tmux, ...args], { stdout: "pipe", stderr: "pipe" })
const exitCode = await proc.exited
const stdout = await new Response(proc.stdout).text()
const paneId = stdout.trim()
const result = await runTmuxCommand(tmux, args)
const paneId = result.output
if (exitCode !== 0 || !paneId) {
const stderr = await new Response(proc.stderr).text()
log("[spawnTmuxSession] FAILED", { exitCode, stderr: stderr.trim() })
if (result.exitCode !== 0 || !paneId) {
log("[spawnTmuxSession] FAILED", { exitCode: result.exitCode, stderr: result.stderr.trim() })
return { success: false }
}
const title = `omo-subagent-${description.slice(0, 20)}`
const titleProc = spawn([tmux, "select-pane", "-t", paneId, "-T", title], {
stdout: "ignore",
stderr: "pipe",
})
const stderrPromise = new Response(titleProc.stderr).text().catch(() => "")
const titleExitCode = await titleProc.exited
if (titleExitCode !== 0) {
const titleStderr = await stderrPromise
const titleResult = await runTmuxCommand(tmux, ["select-pane", "-t", paneId, "-T", title])
if (titleResult.exitCode !== 0) {
log("[spawnTmuxSession] WARNING: failed to set pane title", {
paneId,
title,
exitCode: titleExitCode,
stderr: titleStderr.trim(),
exitCode: titleResult.exitCode,
stderr: titleResult.stderr.trim(),
})
}