fix: align split targeting with configured pane width
Use the configured agent pane width consistently in split target selection and avoid close+spawn churn by replacing the oldest pane when eviction is required. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
parent
b02721463e
commit
da3f24b8b1
@ -228,7 +228,7 @@ describe("decideSpawnActions", () => {
|
|||||||
expect(result.actions[0].type).toBe("spawn")
|
expect(result.actions[0].type).toBe("spawn")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("closes oldest pane when existing panes are too small to split", () => {
|
it("replaces oldest pane when existing panes are too small to split", () => {
|
||||||
// given - existing pane is below minimum splittable size
|
// given - existing pane is below minimum splittable size
|
||||||
const state = createWindowState(220, 30, [
|
const state = createWindowState(220, 30, [
|
||||||
{ paneId: "%1", width: 50, height: 15, left: 110, top: 0 },
|
{ paneId: "%1", width: 50, height: 15, left: 110, top: 0 },
|
||||||
@ -242,9 +242,8 @@ describe("decideSpawnActions", () => {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
expect(result.canSpawn).toBe(true)
|
expect(result.canSpawn).toBe(true)
|
||||||
expect(result.actions.length).toBe(2)
|
expect(result.actions.length).toBe(1)
|
||||||
expect(result.actions[0].type).toBe("close")
|
expect(result.actions[0].type).toBe("replace")
|
||||||
expect(result.actions[1].type).toBe("spawn")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("can spawn when existing pane is large enough to split", () => {
|
it("can spawn when existing pane is large enough to split", () => {
|
||||||
@ -394,4 +393,27 @@ describe("decideSpawnActions with custom agentPaneWidth", () => {
|
|||||||
expect(defaultResult.canSpawn).toBe(false)
|
expect(defaultResult.canSpawn).toBe(false)
|
||||||
expect(customResult.canSpawn).toBe(true)
|
expect(customResult.canSpawn).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("#given custom agentPaneWidth and splittable existing pane #when deciding spawn #then uses spawn without eviction", () => {
|
||||||
|
//#given
|
||||||
|
const customConfig: CapacityConfig = { mainPaneMinWidth: 120, agentPaneWidth: 40 }
|
||||||
|
const state = createWindowState(220, 44, [
|
||||||
|
{ paneId: "%1", width: 90, height: 30, left: 110, top: 0 },
|
||||||
|
])
|
||||||
|
const mappings: SessionMapping[] = [
|
||||||
|
{ sessionId: "old-ses", paneId: "%1", createdAt: new Date("2024-01-01") },
|
||||||
|
]
|
||||||
|
|
||||||
|
//#when
|
||||||
|
const result = decideSpawnActions(state, "ses1", "test", customConfig, mappings)
|
||||||
|
|
||||||
|
//#then
|
||||||
|
expect(result.canSpawn).toBe(true)
|
||||||
|
expect(result.actions.length).toBe(1)
|
||||||
|
expect(result.actions[0].type).toBe("spawn")
|
||||||
|
if (result.actions[0].type === "spawn") {
|
||||||
|
expect(result.actions[0].targetPaneId).toBe("%1")
|
||||||
|
expect(result.actions[0].splitDirection).toBe("-h")
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -57,11 +57,21 @@ export function canSplitPane(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function canSplitPaneAnyDirection(pane: TmuxPaneInfo): boolean {
|
export function canSplitPaneAnyDirection(pane: TmuxPaneInfo): boolean {
|
||||||
return pane.width >= MIN_SPLIT_WIDTH || pane.height >= MIN_SPLIT_HEIGHT
|
return canSplitPaneAnyDirectionWithMinWidth(pane, MIN_PANE_WIDTH)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBestSplitDirection(pane: TmuxPaneInfo): SplitDirection | null {
|
export function canSplitPaneAnyDirectionWithMinWidth(
|
||||||
const canH = pane.width >= MIN_SPLIT_WIDTH
|
pane: TmuxPaneInfo,
|
||||||
|
minPaneWidth: number = MIN_PANE_WIDTH,
|
||||||
|
): boolean {
|
||||||
|
return pane.width >= minSplitWidthFor(minPaneWidth) || pane.height >= MIN_SPLIT_HEIGHT
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getBestSplitDirection(
|
||||||
|
pane: TmuxPaneInfo,
|
||||||
|
minPaneWidth: number = MIN_PANE_WIDTH,
|
||||||
|
): SplitDirection | null {
|
||||||
|
const canH = pane.width >= minSplitWidthFor(minPaneWidth)
|
||||||
const canV = pane.height >= MIN_SPLIT_HEIGHT
|
const canV = pane.height >= MIN_SPLIT_HEIGHT
|
||||||
|
|
||||||
if (!canH && !canV) return null
|
if (!canH && !canV) return null
|
||||||
|
|||||||
@ -62,7 +62,7 @@ export function decideSpawnActions(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isSplittableAtCount(agentAreaWidth, currentCount, minPaneWidth)) {
|
if (isSplittableAtCount(agentAreaWidth, currentCount, minPaneWidth)) {
|
||||||
const spawnTarget = findSpawnTarget(state)
|
const spawnTarget = findSpawnTarget(state, minPaneWidth)
|
||||||
if (spawnTarget) {
|
if (spawnTarget) {
|
||||||
return {
|
return {
|
||||||
canSpawn: true,
|
canSpawn: true,
|
||||||
@ -85,19 +85,14 @@ export function decideSpawnActions(
|
|||||||
canSpawn: true,
|
canSpawn: true,
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
type: "close",
|
type: "replace",
|
||||||
paneId: oldestPane.paneId,
|
paneId: oldestPane.paneId,
|
||||||
sessionId: oldestMapping?.sessionId || "",
|
oldSessionId: oldestMapping?.sessionId || "",
|
||||||
},
|
newSessionId: sessionId,
|
||||||
{
|
|
||||||
type: "spawn",
|
|
||||||
sessionId,
|
|
||||||
description,
|
description,
|
||||||
targetPaneId: state.mainPane.paneId,
|
|
||||||
splitDirection: "-h",
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
reason: "closed 1 pane to make room for split",
|
reason: "replaced oldest pane to avoid split churn",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import type { SplitDirection, TmuxPaneInfo, WindowState } from "./types"
|
|||||||
import { MAIN_PANE_RATIO } from "./tmux-grid-constants"
|
import { MAIN_PANE_RATIO } from "./tmux-grid-constants"
|
||||||
import { computeGridPlan, mapPaneToSlot } from "./grid-planning"
|
import { computeGridPlan, mapPaneToSlot } from "./grid-planning"
|
||||||
import { canSplitPane, getBestSplitDirection } from "./pane-split-availability"
|
import { canSplitPane, getBestSplitDirection } from "./pane-split-availability"
|
||||||
|
import { MIN_PANE_WIDTH } from "./types"
|
||||||
|
|
||||||
export interface SpawnTarget {
|
export interface SpawnTarget {
|
||||||
targetPaneId: string
|
targetPaneId: string
|
||||||
@ -37,6 +38,7 @@ function findFirstEmptySlot(
|
|||||||
|
|
||||||
function findSplittableTarget(
|
function findSplittableTarget(
|
||||||
state: WindowState,
|
state: WindowState,
|
||||||
|
minPaneWidth: number,
|
||||||
_preferredDirection?: SplitDirection,
|
_preferredDirection?: SplitDirection,
|
||||||
): SpawnTarget | null {
|
): SpawnTarget | null {
|
||||||
if (!state.mainPane) return null
|
if (!state.mainPane) return null
|
||||||
@ -44,7 +46,7 @@ function findSplittableTarget(
|
|||||||
|
|
||||||
if (existingCount === 0) {
|
if (existingCount === 0) {
|
||||||
const virtualMainPane: TmuxPaneInfo = { ...state.mainPane, width: state.windowWidth }
|
const virtualMainPane: TmuxPaneInfo = { ...state.mainPane, width: state.windowWidth }
|
||||||
if (canSplitPane(virtualMainPane, "-h")) {
|
if (canSplitPane(virtualMainPane, "-h", minPaneWidth)) {
|
||||||
return { targetPaneId: state.mainPane.paneId, splitDirection: "-h" }
|
return { targetPaneId: state.mainPane.paneId, splitDirection: "-h" }
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
@ -56,17 +58,17 @@ function findSplittableTarget(
|
|||||||
const targetSlot = findFirstEmptySlot(occupancy, plan)
|
const targetSlot = findFirstEmptySlot(occupancy, plan)
|
||||||
|
|
||||||
const leftPane = occupancy.get(`${targetSlot.row}:${targetSlot.col - 1}`)
|
const leftPane = occupancy.get(`${targetSlot.row}:${targetSlot.col - 1}`)
|
||||||
if (leftPane && canSplitPane(leftPane, "-h")) {
|
if (leftPane && canSplitPane(leftPane, "-h", minPaneWidth)) {
|
||||||
return { targetPaneId: leftPane.paneId, splitDirection: "-h" }
|
return { targetPaneId: leftPane.paneId, splitDirection: "-h" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const abovePane = occupancy.get(`${targetSlot.row - 1}:${targetSlot.col}`)
|
const abovePane = occupancy.get(`${targetSlot.row - 1}:${targetSlot.col}`)
|
||||||
if (abovePane && canSplitPane(abovePane, "-v")) {
|
if (abovePane && canSplitPane(abovePane, "-v", minPaneWidth)) {
|
||||||
return { targetPaneId: abovePane.paneId, splitDirection: "-v" }
|
return { targetPaneId: abovePane.paneId, splitDirection: "-v" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const splittablePanes = state.agentPanes
|
const splittablePanes = state.agentPanes
|
||||||
.map((pane) => ({ pane, direction: getBestSplitDirection(pane) }))
|
.map((pane) => ({ pane, direction: getBestSplitDirection(pane, minPaneWidth) }))
|
||||||
.filter(
|
.filter(
|
||||||
(item): item is { pane: TmuxPaneInfo; direction: SplitDirection } =>
|
(item): item is { pane: TmuxPaneInfo; direction: SplitDirection } =>
|
||||||
item.direction !== null,
|
item.direction !== null,
|
||||||
@ -81,6 +83,9 @@ function findSplittableTarget(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findSpawnTarget(state: WindowState): SpawnTarget | null {
|
export function findSpawnTarget(
|
||||||
return findSplittableTarget(state)
|
state: WindowState,
|
||||||
|
minPaneWidth: number = MIN_PANE_WIDTH,
|
||||||
|
): SpawnTarget | null {
|
||||||
|
return findSplittableTarget(state, minPaneWidth)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user