fix(tmux): pick per-instance ports and restore pane cleanup
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import type { Plugin } from "@opencode-ai/plugin"
|
||||
import { spawn } from "bun"
|
||||
import { appendFileSync } from "fs"
|
||||
|
||||
/**
|
||||
* tmux-panes plugin
|
||||
@@ -14,14 +13,8 @@ import { appendFileSync } from "fs"
|
||||
* - Panes close automatically when subagent sessions end
|
||||
*
|
||||
* Only activates when running inside a tmux session (TMUX env var is set).
|
||||
*
|
||||
* Debug log: /tmp/opencode-tmux-debug.log
|
||||
*/
|
||||
|
||||
const DEBUG_LOG = "/tmp/opencode-tmux-debug.log"
|
||||
const log = (msg: string) =>
|
||||
appendFileSync(DEBUG_LOG, `[${new Date().toISOString()}] ${msg}\n`)
|
||||
|
||||
const isInsideTmux = () => Boolean(process.env.TMUX)
|
||||
const getCurrentPaneId = () => process.env.TMUX_PANE
|
||||
|
||||
@@ -31,7 +24,6 @@ const plugin: Plugin = async (ctx) => {
|
||||
const sessions = new Map<string, string>() // sessionId → tmux paneId
|
||||
const sourcePaneId = getCurrentPaneId()
|
||||
const serverUrl = (ctx.serverUrl?.toString() ?? "").replace(/\/$/, "")
|
||||
log(`plugin init — serverUrl=${serverUrl} sourcePaneId=${sourcePaneId}`)
|
||||
|
||||
// Ordered list of pane IDs in the right column.
|
||||
// Empty = no right column yet; length > 0 = right column exists.
|
||||
@@ -44,14 +36,10 @@ const plugin: Plugin = async (ctx) => {
|
||||
const info = (event as any).properties?.info
|
||||
// parentID presence distinguishes subagents from the root session
|
||||
if (!info?.id || !info?.parentID) return
|
||||
const sessionId: string = info.id
|
||||
if (sessions.has(sessionId)) return
|
||||
const sessionId: string = info.id
|
||||
if (sessions.has(sessionId)) return
|
||||
|
||||
// Wrap the attach command: on failure, show the error and keep the
|
||||
// pane open for 30 s so we can read it before it disappears.
|
||||
const attachCmd = `opencode attach ${serverUrl} --session ${sessionId}`
|
||||
const cmd = `bash -c '${attachCmd}; echo "--- exit: $? ---" >> ${DEBUG_LOG}; sleep 30'`
|
||||
log(`spawning pane — cmd: ${attachCmd}`)
|
||||
const cmd = `opencode attach ${serverUrl} --session ${sessionId}`
|
||||
|
||||
let args: string[]
|
||||
if (rightColumnPanes.length === 0) {
|
||||
@@ -88,12 +76,11 @@ const plugin: Plugin = async (ctx) => {
|
||||
|
||||
const proc = spawn(args, { stdout: "pipe", stderr: "pipe" })
|
||||
const paneId = (await new Response(proc.stdout).text()).trim()
|
||||
const exitCode = await proc.exited
|
||||
log(`split-window exit=${exitCode} paneId=${paneId}`)
|
||||
if (exitCode === 0 && paneId) {
|
||||
sessions.set(sessionId, paneId)
|
||||
rightColumnPanes.push(paneId)
|
||||
}
|
||||
const exitCode = await proc.exited
|
||||
if (exitCode === 0 && paneId) {
|
||||
sessions.set(sessionId, paneId)
|
||||
rightColumnPanes.push(paneId)
|
||||
}
|
||||
}
|
||||
|
||||
// Kill the pane when the subagent session ends
|
||||
|
||||
Reference in New Issue
Block a user