debug(tmux-panes): log serverUrl and wrap attach cmd to expose errors

On attach failure the pane stays open for 30s showing the error.
All events logged to /tmp/opencode-tmux-debug.log.
This commit is contained in:
alex
2026-03-11 12:23:36 +00:00
parent 7bd2308b53
commit 1145ce2be4

View File

@@ -1,5 +1,6 @@
import type { Plugin } from "@opencode-ai/plugin"
import { spawn } from "bun"
import { appendFileSync } from "fs"
/**
* tmux-panes plugin
@@ -13,8 +14,14 @@ import { spawn } from "bun"
* - 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
@@ -23,7 +30,8 @@ const plugin: Plugin = async (ctx) => {
const sessions = new Map<string, string>() // sessionId → tmux paneId
const sourcePaneId = getCurrentPaneId()
const serverUrl = ctx.serverUrl.toString()
const serverUrl = ctx.serverUrl?.toString() ?? ""
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.
@@ -39,7 +47,11 @@ const plugin: Plugin = async (ctx) => {
const sessionId: string = info.id
if (sessions.has(sessionId)) return
const cmd = `opencode attach ${serverUrl} --session ${sessionId}`
// 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}; _exit=$?; echo "--- exit: $_exit ---" >> ${DEBUG_LOG}; [ $_exit -ne 0 ] && sleep 30'`
log(`spawning pane — cmd: ${attachCmd}`)
let args: string[]
if (rightColumnPanes.length === 0) {
@@ -76,10 +88,12 @@ const plugin: Plugin = async (ctx) => {
const proc = spawn(args, { stdout: "pipe", stderr: "pipe" })
const paneId = (await new Response(proc.stdout).text()).trim()
if ((await proc.exited) === 0 && paneId) {
sessions.set(sessionId, paneId)
rightColumnPanes.push(paneId)
}
const exitCode = await proc.exited
log(`split-window exit=${exitCode} paneId=${paneId}`)
if (exitCode === 0 && paneId) {
sessions.set(sessionId, paneId)
rightColumnPanes.push(paneId)
}
}
// Kill the pane when the subagent session ends