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:
@@ -1,5 +1,6 @@
|
|||||||
import type { Plugin } from "@opencode-ai/plugin"
|
import type { Plugin } from "@opencode-ai/plugin"
|
||||||
import { spawn } from "bun"
|
import { spawn } from "bun"
|
||||||
|
import { appendFileSync } from "fs"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tmux-panes plugin
|
* tmux-panes plugin
|
||||||
@@ -13,8 +14,14 @@ import { spawn } from "bun"
|
|||||||
* - Panes close automatically when subagent sessions end
|
* - Panes close automatically when subagent sessions end
|
||||||
*
|
*
|
||||||
* Only activates when running inside a tmux session (TMUX env var is set).
|
* 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 isInsideTmux = () => Boolean(process.env.TMUX)
|
||||||
const getCurrentPaneId = () => process.env.TMUX_PANE
|
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 sessions = new Map<string, string>() // sessionId → tmux paneId
|
||||||
const sourcePaneId = getCurrentPaneId()
|
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.
|
// Ordered list of pane IDs in the right column.
|
||||||
// Empty = no right column yet; length > 0 = right column exists.
|
// Empty = no right column yet; length > 0 = right column exists.
|
||||||
@@ -39,7 +47,11 @@ const plugin: Plugin = async (ctx) => {
|
|||||||
const sessionId: string = info.id
|
const sessionId: string = info.id
|
||||||
if (sessions.has(sessionId)) return
|
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[]
|
let args: string[]
|
||||||
if (rightColumnPanes.length === 0) {
|
if (rightColumnPanes.length === 0) {
|
||||||
@@ -76,7 +88,9 @@ const plugin: Plugin = async (ctx) => {
|
|||||||
|
|
||||||
const proc = spawn(args, { stdout: "pipe", stderr: "pipe" })
|
const proc = spawn(args, { stdout: "pipe", stderr: "pipe" })
|
||||||
const paneId = (await new Response(proc.stdout).text()).trim()
|
const paneId = (await new Response(proc.stdout).text()).trim()
|
||||||
if ((await proc.exited) === 0 && paneId) {
|
const exitCode = await proc.exited
|
||||||
|
log(`split-window exit=${exitCode} paneId=${paneId}`)
|
||||||
|
if (exitCode === 0 && paneId) {
|
||||||
sessions.set(sessionId, paneId)
|
sessions.set(sessionId, paneId)
|
||||||
rightColumnPanes.push(paneId)
|
rightColumnPanes.push(paneId)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user