refactor(team-mode): use dedicated focus and grid windows for team layout
Replace in-window pane splitting with two purpose-built windows (focus: main-vertical, grid: tiled) created off the target session, so leader pane is never disturbed and layouts no longer collapse under teammate count. 🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -34,90 +34,60 @@ function buildAttachCommand(member: TeamLayoutMember, serverUrl: string): string
|
||||
return `opencode attach ${shellSingleQuote(serverUrl)} --session ${shellSingleQuote(member.sessionId)} --dir ${shellSingleQuote(getPaneWorkingDirectory(member))}`
|
||||
}
|
||||
|
||||
const PANE_SHELL_INIT_DELAY_MS = 200
|
||||
|
||||
let paneCreationLock: Promise<void> = Promise.resolve()
|
||||
|
||||
async function acquirePaneCreationLock(): Promise<() => void> {
|
||||
let release: () => void
|
||||
const newLock = new Promise<void>((resolve) => { release = resolve })
|
||||
const previousLock = paneCreationLock
|
||||
paneCreationLock = newLock
|
||||
await previousLock
|
||||
return release!
|
||||
}
|
||||
|
||||
async function resolveCurrentWindowTarget(tmuxPath: string, leaderPaneId: string): Promise<string | null> {
|
||||
const result = await runTmuxCommand(tmuxPath, ["display", "-p", "-t", leaderPaneId, "#{session_name}:#{window_index}"])
|
||||
if (!result.success || !result.output) return null
|
||||
return result.output.trim()
|
||||
}
|
||||
|
||||
async function resolveCurrentWindowId(tmuxPath: string, leaderPaneId: string): Promise<string | null> {
|
||||
const result = await runTmuxCommand(tmuxPath, ["display", "-p", "-t", leaderPaneId, "#{window_id}"])
|
||||
if (!result.success || !result.output) return null
|
||||
return result.output.trim()
|
||||
}
|
||||
|
||||
async function listPanesInWindow(tmuxPath: string, windowTarget: string): Promise<Array<string>> {
|
||||
const result = await runTmuxCommand(tmuxPath, ["list-panes", "-t", windowTarget, "-F", "#{pane_id}"])
|
||||
async function listPanesInWindow(tmuxPath: string, windowId: string): Promise<Array<string>> {
|
||||
const result = await runTmuxCommand(tmuxPath, ["list-panes", "-t", windowId, "-F", "#{pane_id}"])
|
||||
if (!result.success || !result.output) return []
|
||||
return result.output.trim().split("\n").filter(Boolean)
|
||||
}
|
||||
|
||||
async function rebalanceWithLeader(tmuxPath: string, windowTarget: string, leaderPaneId: string): Promise<void> {
|
||||
const panes = await listPanesInWindow(tmuxPath, windowTarget)
|
||||
if (panes.length <= 1) return
|
||||
// main-vertical with many teammates collapses each pane to ~10 rows, which the
|
||||
// opencode TUI cannot render. Switch to tiled once the column gets too tall.
|
||||
const teammateCount = panes.length - 1
|
||||
const layout = teammateCount >= 4 ? "tiled" : "main-vertical"
|
||||
await runTmuxCommand(tmuxPath, ["select-layout", "-t", windowTarget, layout])
|
||||
if (layout === "main-vertical") {
|
||||
await runTmuxCommand(tmuxPath, ["resize-pane", "-t", leaderPaneId, "-x", "30%"])
|
||||
}
|
||||
}
|
||||
|
||||
async function createTeammatePaneInCurrentWindow(
|
||||
async function createTeamWindow(
|
||||
tmuxPath: string,
|
||||
leaderPaneId: string,
|
||||
windowTarget: string,
|
||||
member: TeamLayoutMember,
|
||||
): Promise<string | null> {
|
||||
const releaseLock = await acquirePaneCreationLock()
|
||||
try {
|
||||
const panes = await listPanesInWindow(tmuxPath, windowTarget)
|
||||
const isFirstTeammate = panes.length === 1
|
||||
targetSessionId: string,
|
||||
windowName: string,
|
||||
layout: "main-vertical" | "tiled",
|
||||
members: Array<TeamLayoutMember>,
|
||||
serverUrl: string,
|
||||
): Promise<{ windowId: string; panesByMember: Record<string, string> } | null> {
|
||||
const [firstMember, ...restMembers] = members
|
||||
if (!firstMember) return null
|
||||
|
||||
let splitResult
|
||||
if (isFirstTeammate) {
|
||||
splitResult = await runTmuxCommand(tmuxPath, [
|
||||
"split-window", "-t", leaderPaneId, "-h", "-l", "70%", "-d",
|
||||
"-P", "-F", "#{pane_id}", "-c", getPaneWorkingDirectory(member),
|
||||
])
|
||||
} else {
|
||||
const teammatePanes = panes.filter((p) => p !== leaderPaneId)
|
||||
const teammateCount = teammatePanes.length
|
||||
const splitVertically = teammateCount % 2 === 1
|
||||
const targetIndex = Math.floor((teammateCount - 1) / 2)
|
||||
const targetPane = teammatePanes[targetIndex] ?? teammatePanes[teammatePanes.length - 1]
|
||||
const created = await runTmuxCommand(tmuxPath, [
|
||||
"new-window", "-d", "-P", "-F", "#{window_id}", "-t", targetSessionId, "-n", windowName,
|
||||
"-c", getPaneWorkingDirectory(firstMember),
|
||||
])
|
||||
if (!created.success || !created.output) return null
|
||||
|
||||
splitResult = await runTmuxCommand(tmuxPath, [
|
||||
"split-window", "-t", targetPane!, splitVertically ? "-v" : "-h", "-d",
|
||||
"-P", "-F", "#{pane_id}", "-c", getPaneWorkingDirectory(member),
|
||||
])
|
||||
}
|
||||
const windowId = created.output.trim()
|
||||
const initialPanes = await listPanesInWindow(tmuxPath, windowId)
|
||||
const firstPaneId = initialPanes[0]
|
||||
if (!firstPaneId) return null
|
||||
|
||||
if (!splitResult.success || !splitResult.output) return null
|
||||
const paneId = splitResult.output.trim()
|
||||
|
||||
await rebalanceWithLeader(tmuxPath, windowTarget, leaderPaneId)
|
||||
await new Promise((resolve) => setTimeout(resolve, PANE_SHELL_INIT_DELAY_MS))
|
||||
|
||||
return paneId
|
||||
} finally {
|
||||
releaseLock()
|
||||
const panesByMember: Record<string, string> = { [firstMember.name]: firstPaneId }
|
||||
for (const member of restMembers) {
|
||||
const split = await runTmuxCommand(tmuxPath, [
|
||||
"split-window", "-d", "-P", "-F", "#{pane_id}", "-t", firstPaneId,
|
||||
"-c", getPaneWorkingDirectory(member),
|
||||
])
|
||||
if (!split.success || !split.output) return null
|
||||
panesByMember[member.name] = split.output.trim()
|
||||
}
|
||||
|
||||
const layoutResult = await runTmuxCommand(tmuxPath, ["select-layout", "-t", windowId, layout])
|
||||
if (!layoutResult.success) return null
|
||||
|
||||
if (layout === "main-vertical") {
|
||||
await runTmuxCommand(tmuxPath, ["set-window-option", "-t", windowId, "main-pane-width", "60%"])
|
||||
await runTmuxCommand(tmuxPath, ["select-layout", "-t", windowId, layout])
|
||||
}
|
||||
|
||||
for (const member of members) {
|
||||
const paneId = panesByMember[member.name]
|
||||
if (!paneId) return null
|
||||
await runTmuxCommand(tmuxPath, ["select-pane", "-t", paneId, "-T", member.name])
|
||||
await runTmuxCommand(tmuxPath, ["send-keys", "-t", paneId, buildAttachCommand(member, serverUrl), "Enter"])
|
||||
}
|
||||
|
||||
return { windowId, panesByMember }
|
||||
}
|
||||
|
||||
export async function createTeamLayout(teamRunId: string, members: Array<TeamLayoutMember>, tmuxMgr: TmuxSessionManager): Promise<TeamLayoutResult | null> {
|
||||
@@ -141,9 +111,8 @@ export async function createTeamLayout(teamRunId: string, members: Array<TeamLay
|
||||
}
|
||||
|
||||
const callerSession = await resolveCallerTmuxSession(tmuxPath)
|
||||
const leaderPaneId = process.env.TMUX_PANE
|
||||
const fallbackSessionName = `omo-team-${teamRunId}`
|
||||
const ownedSession = callerSession === null || !leaderPaneId
|
||||
const ownedSession = callerSession === null
|
||||
const targetSessionId = callerSession?.sessionId ?? fallbackSessionName
|
||||
|
||||
if (ownedSession) {
|
||||
@@ -152,36 +121,15 @@ export async function createTeamLayout(teamRunId: string, members: Array<TeamLay
|
||||
if (!created.success || !created.output) return null
|
||||
}
|
||||
|
||||
if (!leaderPaneId || ownedSession) {
|
||||
log("no leader pane for split layout, skipping visualization", { teamRunId })
|
||||
return null
|
||||
}
|
||||
|
||||
const windowTarget = await resolveCurrentWindowTarget(tmuxPath, leaderPaneId)
|
||||
const windowId = await resolveCurrentWindowId(tmuxPath, leaderPaneId)
|
||||
if (!windowTarget || !windowId) return null
|
||||
|
||||
const panesByMember: Record<string, string> = {}
|
||||
|
||||
for (const member of members) {
|
||||
const paneId = await createTeammatePaneInCurrentWindow(tmuxPath, leaderPaneId, windowTarget, member)
|
||||
if (paneId) panesByMember[member.name] = paneId
|
||||
}
|
||||
|
||||
if (Object.keys(panesByMember).length === 0) return null
|
||||
|
||||
for (const member of members) {
|
||||
const paneId = panesByMember[member.name]
|
||||
if (!paneId) continue
|
||||
const cmd = buildAttachCommand(member, serverUrl)
|
||||
await runTmuxCommand(tmuxPath, ["send-keys", "-t", paneId, cmd, "Enter"])
|
||||
}
|
||||
const focus = await createTeamWindow(tmuxPath, targetSessionId, `team-${teamRunId}-focus`, "main-vertical", members, serverUrl)
|
||||
const grid = await createTeamWindow(tmuxPath, targetSessionId, `team-${teamRunId}-grid`, "tiled", members, serverUrl)
|
||||
if (!focus || !grid) return null
|
||||
|
||||
return {
|
||||
focusWindowId: windowId,
|
||||
gridWindowId: windowId,
|
||||
focusPanesByMember: panesByMember,
|
||||
gridPanesByMember: panesByMember,
|
||||
focusWindowId: focus.windowId,
|
||||
gridWindowId: grid.windowId,
|
||||
focusPanesByMember: focus.panesByMember,
|
||||
gridPanesByMember: grid.panesByMember,
|
||||
targetSessionId,
|
||||
ownedSession,
|
||||
}
|
||||
@@ -216,7 +164,7 @@ export async function removeTeamLayout(
|
||||
return
|
||||
}
|
||||
|
||||
if (cleanupTarget.paneIds && cleanupTarget.paneIds.length > 0) {
|
||||
if (cleanupTarget?.paneIds && cleanupTarget.paneIds.length > 0) {
|
||||
for (const paneId of cleanupTarget.paneIds) {
|
||||
try {
|
||||
await runTmuxCommand(tmuxPath, ["kill-pane", "-t", paneId])
|
||||
@@ -224,26 +172,11 @@ export async function removeTeamLayout(
|
||||
log("tmux team pane cleanup failed", { teamRunId, paneId })
|
||||
}
|
||||
}
|
||||
|
||||
const leaderPaneId = process.env.TMUX_PANE
|
||||
if (leaderPaneId) {
|
||||
const windowTarget = await resolveCurrentWindowTarget(tmuxPath, leaderPaneId)
|
||||
if (windowTarget) await rebalanceWithLeader(tmuxPath, windowTarget, leaderPaneId)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const leaderPaneId = process.env.TMUX_PANE
|
||||
const leaderWindowId = leaderPaneId
|
||||
? await resolveCurrentWindowId(tmuxPath, leaderPaneId)
|
||||
: null
|
||||
|
||||
for (const windowId of [cleanupTarget.focusWindowId, cleanupTarget.gridWindowId]) {
|
||||
if (!windowId) continue
|
||||
if (leaderWindowId && windowId === leaderWindowId) {
|
||||
log("tmux team layout skipping kill-window on leader window", { teamRunId, windowId })
|
||||
continue
|
||||
}
|
||||
try {
|
||||
await runTmuxCommand(tmuxPath, ["kill-window", "-t", windowId])
|
||||
} catch (windowError) {
|
||||
|
||||
Reference in New Issue
Block a user