Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions src/features/tmux-subagent/action-executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import type { TmuxConfig } from "../../config/schema"
import type { PaneAction, WindowState } from "./types"
import { spawnTmuxPane, closeTmuxPane, enforceMainPaneWidth, replaceTmuxPane } from "../../shared/tmux"
import {
applyLayout,
spawnTmuxPane,
closeTmuxPane,
enforceMainPaneWidth,
replaceTmuxPane,
} from "../../shared/tmux"
import { getTmuxPath } from "../../tools/interactive-bash/tmux-path-resolver"
import { queryWindowState } from "./pane-state-querier"
import { log } from "../../shared"

export interface ActionResult {
Expand All @@ -19,11 +27,40 @@ export interface ExecuteContext {
config: TmuxConfig
serverUrl: string
windowState: WindowState
sourcePaneId?: string
}

async function enforceMainPane(windowState: WindowState): Promise<void> {
async function enforceMainPane(
windowState: WindowState,
config: TmuxConfig,
): Promise<void> {
if (!windowState.mainPane) return
await enforceMainPaneWidth(windowState.mainPane.paneId, windowState.windowWidth)
await enforceMainPaneWidth(windowState.mainPane.paneId, windowState.windowWidth, {
mainPaneSize: config.main_pane_size,
mainPaneMinWidth: config.main_pane_min_width,
agentPaneMinWidth: config.agent_pane_min_width,
})
}

async function enforceLayoutAndMainPane(ctx: ExecuteContext): Promise<void> {
const sourcePaneId = ctx.sourcePaneId
if (!sourcePaneId) {
await enforceMainPane(ctx.windowState, ctx.config)
return
}

const latestState = await queryWindowState(sourcePaneId)
if (!latestState?.mainPane) {
await enforceMainPane(ctx.windowState, ctx.config)
return
}

const tmux = await getTmuxPath()
if (tmux) {
await applyLayout(tmux, ctx.config.layout, ctx.config.main_pane_size)
}

await enforceMainPane(latestState, ctx.config)
}

export async function executeAction(
Expand All @@ -33,7 +70,7 @@ export async function executeAction(
if (action.type === "close") {
const success = await closeTmuxPane(action.paneId)
if (success) {
await enforceMainPane(ctx.windowState)
await enforceLayoutAndMainPane(ctx)
}
return { success }
}
Expand All @@ -46,6 +83,9 @@ export async function executeAction(
ctx.config,
ctx.serverUrl
)
if (result.success) {
await enforceLayoutAndMainPane(ctx)
}
return {
success: result.success,
paneId: result.paneId,
Expand All @@ -62,7 +102,7 @@ export async function executeAction(
)

if (result.success) {
await enforceMainPane(ctx.windowState)
await enforceLayoutAndMainPane(ctx)
}

return {
Expand Down
82 changes: 82 additions & 0 deletions src/features/tmux-subagent/decision-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
canSplitPane,
canSplitPaneAnyDirection,
getBestSplitDirection,
findSpawnTarget,
type SessionMapping
} from "./decision-engine"
import type { WindowState, CapacityConfig, TmuxPaneInfo } from "./types"
Expand Down Expand Up @@ -228,6 +229,29 @@ describe("decideSpawnActions", () => {
expect(result.actions[0].type).toBe("spawn")
})

it("respects configured agent min width for split decisions", () => {
// given
const state = createWindowState(240, 44, [
{ paneId: "%1", width: 100, height: 44, left: 140, top: 0 },
])
const mappings: SessionMapping[] = [
{ sessionId: "old-ses", paneId: "%1", createdAt: new Date("2024-01-01") },
]
const strictConfig: CapacityConfig = {
mainPaneSize: 60,
mainPaneMinWidth: 120,
agentPaneWidth: 60,
}

// when
const result = decideSpawnActions(state, "ses1", "test", strictConfig, mappings)

// then
expect(result.canSpawn).toBe(false)
expect(result.actions).toHaveLength(0)
expect(result.reason).toContain("defer")
})

it("closes oldest pane when existing panes are too small to split", () => {
// given - existing pane is below minimum splittable size
const state = createWindowState(220, 30, [
Expand Down Expand Up @@ -306,6 +330,64 @@ describe("decideSpawnActions", () => {
expect(result.canSpawn).toBe(false)
expect(result.reason).toBe("no main pane found")
})

it("uses configured main pane size for split/defer decision", () => {
// given
const state = createWindowState(240, 44, [
{ paneId: "%1", width: 90, height: 44, left: 150, top: 0 },
])
const mappings: SessionMapping[] = [
{ sessionId: "old-ses", paneId: "%1", createdAt: new Date("2024-01-01") },
]
const wideMainConfig: CapacityConfig = {
mainPaneSize: 80,
mainPaneMinWidth: 120,
agentPaneWidth: 40,
}

// when
const result = decideSpawnActions(state, "ses1", "test", wideMainConfig, mappings)

// then
expect(result.canSpawn).toBe(false)
expect(result.actions).toHaveLength(0)
expect(result.reason).toContain("defer")
})
})
})

describe("findSpawnTarget", () => {
it("uses deterministic vertical fallback order", () => {
// given
const state: WindowState = {
windowWidth: 320,
windowHeight: 44,
mainPane: {
paneId: "%0",
width: 160,
height: 44,
left: 0,
top: 0,
title: "main",
isActive: true,
},
agentPanes: [
{ paneId: "%1", width: 70, height: 20, left: 170, top: 0, title: "a", isActive: false },
{ paneId: "%2", width: 120, height: 44, left: 240, top: 0, title: "b", isActive: false },
{ paneId: "%3", width: 120, height: 22, left: 240, top: 22, title: "c", isActive: false },
],
}
const config: CapacityConfig = {
mainPaneSize: 50,
mainPaneMinWidth: 120,
agentPaneWidth: 40,
}

// when
const target = findSpawnTarget(state, config)

// then
expect(target).toEqual({ targetPaneId: "%2", splitDirection: "-v" })
})
})

Expand Down
33 changes: 27 additions & 6 deletions src/features/tmux-subagent/grid-planning.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MIN_PANE_HEIGHT, MIN_PANE_WIDTH } from "./types"
import type { TmuxPaneInfo } from "./types"
import type { CapacityConfig, TmuxPaneInfo } from "./types"
import {
DIVIDER_SIZE,
MAIN_PANE_RATIO,
MAX_GRID_SIZE,
computeAgentAreaWidth,
} from "./tmux-grid-constants"

export interface GridCapacity {
Expand All @@ -24,12 +24,32 @@ export interface GridPlan {
slotHeight: number
}

type CapacityOptions = CapacityConfig | number | undefined

function resolveMinPaneWidth(options?: CapacityOptions): number {
if (typeof options === "number") {
return Math.max(1, options)
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolveMinPaneWidth() ignores CapacityConfig.agentPaneWidth when options is a config object, always falling back to MIN_PANE_WIDTH. This can overestimate grid capacity / slot widths under stricter agentPaneWidth settings and lead to inconsistent planning vs split checks. Consider deriving minPaneWidth from options.agentPaneWidth when options is a CapacityConfig (and only defaulting to MIN_PANE_WIDTH when config is absent).

Suggested change
}
}
if (options && typeof (options as CapacityConfig).agentPaneWidth === "number") {
return Math.max(1, (options as CapacityConfig).agentPaneWidth)
}

Copilot uses AI. Check for mistakes.
if (options && typeof options.agentPaneWidth === "number") {
return Math.max(1, options.agentPaneWidth)
}
return MIN_PANE_WIDTH
}

function resolveAgentAreaWidth(windowWidth: number, options?: CapacityOptions): number {
if (typeof options === "number") {
return computeAgentAreaWidth(windowWidth)
}
return computeAgentAreaWidth(windowWidth, options)
}

export function calculateCapacity(
windowWidth: number,
windowHeight: number,
minPaneWidth: number = MIN_PANE_WIDTH,
options?: CapacityOptions,
): GridCapacity {
const availableWidth = Math.floor(windowWidth * (1 - MAIN_PANE_RATIO))
const availableWidth = resolveAgentAreaWidth(windowWidth, options)
const minPaneWidth = resolveMinPaneWidth(options)
const cols = Math.min(
MAX_GRID_SIZE,
Math.max(
Expand All @@ -55,8 +75,9 @@ export function computeGridPlan(
windowWidth: number,
windowHeight: number,
paneCount: number,
options?: CapacityOptions,
): GridPlan {
const capacity = calculateCapacity(windowWidth, windowHeight)
const capacity = calculateCapacity(windowWidth, windowHeight, options)
const { cols: maxCols, rows: maxRows } = capacity

if (maxCols === 0 || maxRows === 0 || paneCount === 0) {
Expand All @@ -79,7 +100,7 @@ export function computeGridPlan(
}
}

const availableWidth = Math.floor(windowWidth * (1 - MAIN_PANE_RATIO))
const availableWidth = resolveAgentAreaWidth(windowWidth, options)
const slotWidth = Math.floor(availableWidth / bestCols)
const slotHeight = Math.floor(windowHeight / bestRows)

Expand Down
Loading