Merge pull request #2931 from sjawhar/feat/tmux-session-isolation

feat(tmux): add session isolation mode for subagent panes
This commit is contained in:
YeonGyu-Kim
2026-03-29 18:44:29 -07:00
committed by GitHub
8 changed files with 342 additions and 10 deletions
+81 -9
View File
@@ -8,6 +8,8 @@ import {
POLL_INTERVAL_BACKGROUND_MS,
SESSION_READY_POLL_INTERVAL_MS,
SESSION_READY_TIMEOUT_MS,
spawnTmuxWindow,
spawnTmuxSession,
} from "../../shared/tmux"
import { queryWindowState } from "./pane-state-querier"
import { decideSpawnActions, decideCloseAction, type SessionMapping } from "./decision-engine"
@@ -68,6 +70,7 @@ export class TmuxSessionManager {
private nullStateCount = 0
private deps: TmuxUtilDeps
private pollingManager: TmuxPollingManager
private isolatedWindowPaneId: string | undefined
constructor(ctx: PluginInput, tmuxConfig: TmuxConfig, deps: TmuxUtilDeps = defaultTmuxDeps) {
this.client = ctx.client
this.tmuxConfig = tmuxConfig
@@ -103,6 +106,47 @@ export class TmuxSessionManager {
return this.tmuxConfig.enabled && this.deps.isInsideTmux()
}
private isIsolated(): boolean {
return this.tmuxConfig.isolation === "window" || this.tmuxConfig.isolation === "session"
}
private getEffectiveSourcePaneId(): string | undefined {
if (this.isIsolated() && this.isolatedWindowPaneId) {
return this.isolatedWindowPaneId
}
return this.sourcePaneId
}
private async spawnInIsolatedContainer(
sessionId: string,
title: string,
): Promise<string | null> {
if (!this.isIsolated()) return null
if (this.isolatedWindowPaneId) {
const state = await queryWindowState(this.isolatedWindowPaneId).catch(() => null)
if (state) return null
this.isolatedWindowPaneId = undefined
}
const isolation = this.tmuxConfig.isolation
log("[tmux-session-manager] creating isolated tmux container", { isolation, sessionId, title })
const result = isolation === "session"
? await spawnTmuxSession(sessionId, title, this.tmuxConfig, this.serverUrl, this.sourcePaneId)
: await spawnTmuxWindow(sessionId, title, this.tmuxConfig, this.serverUrl)
if (result.success && result.paneId) {
this.isolatedWindowPaneId = result.paneId
log("[tmux-session-manager] isolated container created", {
isolation,
paneId: result.paneId,
})
return result.paneId
}
log("[tmux-session-manager] failed to create isolated container", { isolation, sessionId })
return null
}
private getCapacityConfig(): CapacityConfig {
return {
layout: this.tmuxConfig.layout,
@@ -141,10 +185,11 @@ export class TmuxSessionManager {
}
private async queryWindowStateSafely(): Promise<WindowState | null> {
if (!this.sourcePaneId) return null
const paneId = this.getEffectiveSourcePaneId()
if (!paneId) return null
try {
return await queryWindowState(this.sourcePaneId)
return await queryWindowState(paneId)
} catch (error) {
log("[tmux-session-manager] failed to query window state for close", {
error: String(error),
@@ -164,7 +209,7 @@ export class TmuxSessionManager {
config: this.tmuxConfig,
serverUrl: this.serverUrl,
windowState: state,
sourcePaneId: this.sourcePaneId,
sourcePaneId: this.getEffectiveSourcePaneId(),
}
)
@@ -301,7 +346,8 @@ export class TmuxSessionManager {
}
private async tryAttachDeferredSession(): Promise<void> {
if (!this.sourcePaneId) return
const effectiveSourcePaneId = this.getEffectiveSourcePaneId()
if (!effectiveSourcePaneId) return
const sessionId = this.deferredQueue[0]
if (!sessionId) {
this.stopDeferredAttachLoop()
@@ -329,7 +375,7 @@ export class TmuxSessionManager {
return
}
const state = await queryWindowState(this.sourcePaneId)
const state = await queryWindowState(effectiveSourcePaneId)
if (!state) {
this.nullStateCount += 1
log("[tmux-session-manager] deferred attach window state is null", {
@@ -365,7 +411,7 @@ export class TmuxSessionManager {
config: this.tmuxConfig,
serverUrl: this.serverUrl,
windowState: state,
sourcePaneId: this.sourcePaneId,
sourcePaneId: effectiveSourcePaneId,
})
if (!result.success || !result.spawnedPaneId) {
@@ -470,12 +516,37 @@ export class TmuxSessionManager {
log("[tmux-session-manager] session already tracked or pending", { sessionId })
return
}
const sourcePaneId = this.sourcePaneId
this.pendingSessions.add(sessionId)
await this.enqueueSpawn(async () => {
try {
const isolatedPaneId = await this.spawnInIsolatedContainer(sessionId, title)
if (isolatedPaneId) {
const sessionReady = await this.waitForSessionReady(sessionId)
this.sessions.set(
sessionId,
createTrackedSession({ sessionId, paneId: isolatedPaneId, description: title }),
)
this.pollingManager.startPolling()
log("[tmux-session-manager] first subagent spawned in isolated window", {
sessionId,
paneId: isolatedPaneId,
sessionReady,
})
return
}
if (this.isIsolated()) {
log("[tmux-session-manager] isolated container failed, skipping inline fallback to preserve isolation", { sessionId })
return
}
const sourcePaneId = this.getEffectiveSourcePaneId()
if (!sourcePaneId) {
log("[tmux-session-manager] no effective source pane id")
return
}
const state = await queryWindowState(sourcePaneId)
if (!state) {
log("[tmux-session-manager] failed to query window state, deferring session")
@@ -609,7 +680,7 @@ export class TmuxSessionManager {
async onSessionDeleted(event: { sessionID: string }): Promise<void> {
if (!this.isEnabled()) return
if (!this.sourcePaneId) return
if (!this.getEffectiveSourcePaneId()) return
this.removeDeferredSession(event.sessionID)
@@ -635,7 +706,7 @@ export class TmuxSessionManager {
config: this.tmuxConfig,
serverUrl: this.serverUrl,
windowState: state,
sourcePaneId: this.sourcePaneId,
sourcePaneId: this.getEffectiveSourcePaneId(),
})
if (!result.success) {
@@ -712,6 +783,7 @@ export class TmuxSessionManager {
}
await this.retryPendingCloses()
this.isolatedWindowPaneId = undefined
log("[tmux-session-manager] cleanup complete")
}