refactor: major codebase cleanup - BDD comments, file splitting, bug fixes (#1350)

* style(tests): normalize BDD comments from '// #given' to '// given'

- Replace 4,668 Python-style BDD comments across 107 test files
- Patterns changed: // #given -> // given, // #when -> // when, // #then -> // then
- Also handles no-space variants: //#given -> // given

* fix(rules-injector): prefer output.metadata.filePath over output.title

- Extract file path resolution to dedicated output-path.ts module
- Prefer metadata.filePath which contains actual file path
- Fall back to output.title only when metadata unavailable
- Fixes issue where rules weren't injected when tool output title was a label

* feat(slashcommand): add optional user_message parameter

- Add user_message optional parameter for command arguments
- Model can now call: command='publish' user_message='patch'
- Improves error messages with clearer format guidance
- Helps LLMs understand correct parameter usage

* feat(hooks): restore compaction-context-injector hook

- Restore hook deleted in cbbc7bd0 for session compaction context
- Injects 7 mandatory sections: User Requests, Final Goal, Work Completed,
  Remaining Tasks, Active Working Context, MUST NOT Do, Agent Verification State
- Re-register in hooks/index.ts and main plugin entry

* refactor(background-agent): split manager.ts into focused modules

- Extract constants.ts for TTL values and internal types (52 lines)
- Extract state.ts for TaskStateManager class (204 lines)
- Extract spawner.ts for task creation logic (244 lines)
- Extract result-handler.ts for completion handling (265 lines)
- Reduce manager.ts from 1377 to 755 lines (45% reduction)
- Maintain backward compatible exports

* refactor(agents): split prometheus-prompt.ts into subdirectory

- Move 1196-line prometheus-prompt.ts to prometheus/ subdirectory
- Organize prompt sections into separate files for maintainability
- Update agents/index.ts exports

* refactor(delegate-task): split tools.ts into focused modules

- Extract categories.ts for category definitions and routing
- Extract executor.ts for task execution logic
- Extract helpers.ts for utility functions
- Extract prompt-builder.ts for prompt construction
- Reduce tools.ts complexity with cleaner separation of concerns

* refactor(builtin-skills): split skills.ts into individual skill files

- Move each skill to dedicated file in skills/ subdirectory
- Create barrel export for backward compatibility
- Improve maintainability with focused skill modules

* chore: update import paths and lockfile

- Update prometheus import path after refactor
- Update bun.lock

* fix(tests): complete BDD comment normalization

- Fix remaining #when/#then patterns missed by initial sed
- Affected: state.test.ts, events.test.ts

---------

Co-authored-by: justsisyphus <justsisyphus@users.noreply.github.com>
This commit is contained in:
YeonGyu-Kim
2026-02-01 16:47:50 +09:00
committed by GitHub
parent c83150d9ea
commit f146aeff0f
145 changed files with 10307 additions and 9562 deletions
@@ -25,46 +25,46 @@ describe("canSplitPane", () => {
})
it("returns true for horizontal split when width >= 2*MIN+1", () => {
//#given - pane with exactly minimum splittable width (107)
// given - pane with exactly minimum splittable width (107)
const pane = createPane(MIN_SPLIT_WIDTH, 20)
//#when
// when
const result = canSplitPane(pane, "-h")
//#then
// then
expect(result).toBe(true)
})
it("returns false for horizontal split when width < 2*MIN+1", () => {
//#given - pane just below minimum splittable width
// given - pane just below minimum splittable width
const pane = createPane(MIN_SPLIT_WIDTH - 1, 20)
//#when
// when
const result = canSplitPane(pane, "-h")
//#then
// then
expect(result).toBe(false)
})
it("returns true for vertical split when height >= 2*MIN+1", () => {
//#given - pane with exactly minimum splittable height (23)
// given - pane with exactly minimum splittable height (23)
const pane = createPane(50, MIN_SPLIT_HEIGHT)
//#when
// when
const result = canSplitPane(pane, "-v")
//#then
// then
expect(result).toBe(true)
})
it("returns false for vertical split when height < 2*MIN+1", () => {
//#given - pane just below minimum splittable height
// given - pane just below minimum splittable height
const pane = createPane(50, MIN_SPLIT_HEIGHT - 1)
//#when
// when
const result = canSplitPane(pane, "-v")
//#then
// then
expect(result).toBe(false)
})
})
@@ -81,35 +81,35 @@ describe("canSplitPaneAnyDirection", () => {
})
it("returns true when can split horizontally but not vertically", () => {
//#given
// given
const pane = createPane(MIN_SPLIT_WIDTH, MIN_SPLIT_HEIGHT - 1)
//#when
// when
const result = canSplitPaneAnyDirection(pane)
//#then
// then
expect(result).toBe(true)
})
it("returns true when can split vertically but not horizontally", () => {
//#given
// given
const pane = createPane(MIN_SPLIT_WIDTH - 1, MIN_SPLIT_HEIGHT)
//#when
// when
const result = canSplitPaneAnyDirection(pane)
//#then
// then
expect(result).toBe(true)
})
it("returns false when cannot split in any direction", () => {
//#given - pane too small in both dimensions
// given - pane too small in both dimensions
const pane = createPane(MIN_SPLIT_WIDTH - 1, MIN_SPLIT_HEIGHT - 1)
//#when
// when
const result = canSplitPaneAnyDirection(pane)
//#then
// then
expect(result).toBe(false)
})
})
@@ -126,57 +126,57 @@ describe("getBestSplitDirection", () => {
})
it("returns -h when only horizontal split possible", () => {
//#given
// given
const pane = createPane(MIN_SPLIT_WIDTH, MIN_SPLIT_HEIGHT - 1)
//#when
// when
const result = getBestSplitDirection(pane)
//#then
// then
expect(result).toBe("-h")
})
it("returns -v when only vertical split possible", () => {
//#given
// given
const pane = createPane(MIN_SPLIT_WIDTH - 1, MIN_SPLIT_HEIGHT)
//#when
// when
const result = getBestSplitDirection(pane)
//#then
// then
expect(result).toBe("-v")
})
it("returns null when no split possible", () => {
//#given
// given
const pane = createPane(MIN_SPLIT_WIDTH - 1, MIN_SPLIT_HEIGHT - 1)
//#when
// when
const result = getBestSplitDirection(pane)
//#then
// then
expect(result).toBe(null)
})
it("returns -h when width >= height and both splits possible", () => {
//#given - wider than tall
// given - wider than tall
const pane = createPane(MIN_SPLIT_WIDTH + 10, MIN_SPLIT_HEIGHT)
//#when
// when
const result = getBestSplitDirection(pane)
//#then
// then
expect(result).toBe("-h")
})
it("returns -v when height > width and both splits possible", () => {
//#given - taller than wide (height needs to be > width for -v)
// given - taller than wide (height needs to be > width for -v)
const pane = createPane(MIN_SPLIT_WIDTH, MIN_SPLIT_WIDTH + 10)
//#when
// when
const result = getBestSplitDirection(pane)
//#then
// then
expect(result).toBe("-v")
})
})
@@ -204,32 +204,32 @@ describe("decideSpawnActions", () => {
describe("minimum size enforcement", () => {
it("returns canSpawn=false when window too small", () => {
//#given - window smaller than minimum pane size
// given - window smaller than minimum pane size
const state = createWindowState(50, 5)
//#when
// when
const result = decideSpawnActions(state, "ses1", "test", defaultConfig, [])
//#then
// then
expect(result.canSpawn).toBe(false)
expect(result.reason).toContain("too small")
})
it("returns canSpawn=true when main pane can be split", () => {
//#given - main pane width >= 2*MIN_PANE_WIDTH+1 = 107
// given - main pane width >= 2*MIN_PANE_WIDTH+1 = 107
const state = createWindowState(220, 44)
//#when
// when
const result = decideSpawnActions(state, "ses1", "test", defaultConfig, [])
//#then
// then
expect(result.canSpawn).toBe(true)
expect(result.actions.length).toBe(1)
expect(result.actions[0].type).toBe("spawn")
})
it("closes 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, [
{ paneId: "%1", width: 50, height: 15, left: 110, top: 0 },
])
@@ -237,10 +237,10 @@ describe("decideSpawnActions", () => {
{ sessionId: "old-ses", paneId: "%1", createdAt: new Date("2024-01-01") },
]
//#when
// when
const result = decideSpawnActions(state, "ses1", "test", defaultConfig, mappings)
//#then
// then
expect(result.canSpawn).toBe(true)
expect(result.actions.length).toBe(2)
expect(result.actions[0].type).toBe("close")
@@ -248,15 +248,15 @@ describe("decideSpawnActions", () => {
})
it("can spawn when existing pane is large enough to split", () => {
//#given - existing pane is above minimum splittable size
// given - existing pane is above minimum splittable size
const state = createWindowState(320, 50, [
{ paneId: "%1", width: MIN_SPLIT_WIDTH + 10, height: MIN_SPLIT_HEIGHT + 10, left: 160, top: 0 },
])
//#when
// when
const result = decideSpawnActions(state, "ses1", "test", defaultConfig, [])
//#then
// then
expect(result.canSpawn).toBe(true)
expect(result.actions.length).toBe(1)
expect(result.actions[0].type).toBe("spawn")
@@ -265,28 +265,28 @@ describe("decideSpawnActions", () => {
describe("basic spawn decisions", () => {
it("returns canSpawn=true when capacity allows new pane", () => {
//#given - 220x44 window, mainPane width=110 >= MIN_SPLIT_WIDTH(107)
// given - 220x44 window, mainPane width=110 >= MIN_SPLIT_WIDTH(107)
const state = createWindowState(220, 44)
//#when
// when
const result = decideSpawnActions(state, "ses1", "test", defaultConfig, [])
//#then
// then
expect(result.canSpawn).toBe(true)
expect(result.actions.length).toBe(1)
expect(result.actions[0].type).toBe("spawn")
})
it("spawns with splitDirection", () => {
//#given
// given
const state = createWindowState(212, 44, [
{ paneId: "%1", width: MIN_SPLIT_WIDTH, height: MIN_SPLIT_HEIGHT, left: 106, top: 0 },
])
//#when
// when
const result = decideSpawnActions(state, "ses1", "test", defaultConfig, [])
//#then
// then
expect(result.canSpawn).toBe(true)
expect(result.actions[0].type).toBe("spawn")
if (result.actions[0].type === "spawn") {
@@ -296,13 +296,13 @@ describe("decideSpawnActions", () => {
})
it("returns canSpawn=false when no main pane", () => {
//#given
// given
const state: WindowState = { windowWidth: 212, windowHeight: 44, mainPane: null, agentPanes: [] }
//#when
// when
const result = decideSpawnActions(state, "ses1", "test", defaultConfig, [])
//#then
// then
expect(result.canSpawn).toBe(false)
expect(result.reason).toBe("no main pane found")
})
@@ -311,42 +311,42 @@ describe("decideSpawnActions", () => {
describe("calculateCapacity", () => {
it("calculates 2D grid capacity (cols x rows)", () => {
//#given - 212x44 window (user's actual screen)
//#when
// given - 212x44 window (user's actual screen)
// when
const capacity = calculateCapacity(212, 44)
//#then - availableWidth=106, cols=(106+1)/(52+1)=2, rows=(44+1)/(11+1)=3 (accounting for dividers)
// then - availableWidth=106, cols=(106+1)/(52+1)=2, rows=(44+1)/(11+1)=3 (accounting for dividers)
expect(capacity.cols).toBe(2)
expect(capacity.rows).toBe(3)
expect(capacity.total).toBe(6)
})
it("returns 0 cols when agent area too narrow", () => {
//#given - window too narrow for even 1 agent pane
//#when
// given - window too narrow for even 1 agent pane
// when
const capacity = calculateCapacity(100, 44)
//#then - availableWidth=50, cols=50/53=0
// then - availableWidth=50, cols=50/53=0
expect(capacity.cols).toBe(0)
expect(capacity.total).toBe(0)
})
it("returns 0 rows when window too short", () => {
//#given - window too short
//#when
// given - window too short
// when
const capacity = calculateCapacity(212, 10)
//#then - rows=10/11=0
// then - rows=10/11=0
expect(capacity.rows).toBe(0)
expect(capacity.total).toBe(0)
})
it("scales with larger screens but caps at MAX_GRID_SIZE=4", () => {
//#given - larger 4K-like screen (400x100)
//#when
// given - larger 4K-like screen (400x100)
// when
const capacity = calculateCapacity(400, 100)
//#then - cols capped at 4, rows capped at 4 (MAX_GRID_SIZE)
// then - cols capped at 4, rows capped at 4 (MAX_GRID_SIZE)
expect(capacity.cols).toBe(3)
expect(capacity.rows).toBe(4)
expect(capacity.total).toBe(12)
+52 -52
View File
@@ -145,7 +145,7 @@ describe('TmuxSessionManager', () => {
describe('constructor', () => {
test('enabled when config.enabled=true and isInsideTmux=true', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = createMockContext()
@@ -157,15 +157,15 @@ describe('TmuxSessionManager', () => {
agent_pane_min_width: 40,
}
//#when
// when
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
//#then
// then
expect(manager).toBeDefined()
})
test('disabled when config.enabled=true but isInsideTmux=false', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(false)
const { TmuxSessionManager } = await import('./manager')
const ctx = createMockContext()
@@ -177,15 +177,15 @@ describe('TmuxSessionManager', () => {
agent_pane_min_width: 40,
}
//#when
// when
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
//#then
// then
expect(manager).toBeDefined()
})
test('disabled when config.enabled=false', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = createMockContext()
@@ -197,17 +197,17 @@ describe('TmuxSessionManager', () => {
agent_pane_min_width: 40,
}
//#when
// when
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
//#then
// then
expect(manager).toBeDefined()
})
})
describe('onSessionCreated', () => {
test('first agent spawns from source pane via decision engine', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
mockQueryWindowState.mockImplementation(async () => createWindowState())
@@ -227,10 +227,10 @@ describe('TmuxSessionManager', () => {
'Background: Test Task'
)
//#when
// when
await manager.onSessionCreated(event)
//#then
// then
expect(mockQueryWindowState).toHaveBeenCalledTimes(1)
expect(mockExecuteActions).toHaveBeenCalledTimes(1)
@@ -248,7 +248,7 @@ describe('TmuxSessionManager', () => {
})
test('second agent spawns with correct split direction', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
let callCount = 0
@@ -283,18 +283,18 @@ describe('TmuxSessionManager', () => {
}
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
//#when - first agent
// when - first agent
await manager.onSessionCreated(
createSessionCreatedEvent('ses_1', 'ses_parent', 'Task 1')
)
mockExecuteActions.mockClear()
//#when - second agent
// when - second agent
await manager.onSessionCreated(
createSessionCreatedEvent('ses_2', 'ses_parent', 'Task 2')
)
//#then
// then
expect(mockExecuteActions).toHaveBeenCalledTimes(1)
const call = mockExecuteActions.mock.calls[0]
expect(call).toBeDefined()
@@ -304,7 +304,7 @@ describe('TmuxSessionManager', () => {
})
test('does NOT spawn pane when session has no parentID', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = createMockContext()
@@ -318,15 +318,15 @@ describe('TmuxSessionManager', () => {
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
const event = createSessionCreatedEvent('ses_root', undefined, 'Root Session')
//#when
// when
await manager.onSessionCreated(event)
//#then
// then
expect(mockExecuteActions).toHaveBeenCalledTimes(0)
})
test('does NOT spawn pane when disabled', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = createMockContext()
@@ -344,15 +344,15 @@ describe('TmuxSessionManager', () => {
'Background: Test Task'
)
//#when
// when
await manager.onSessionCreated(event)
//#then
// then
expect(mockExecuteActions).toHaveBeenCalledTimes(0)
})
test('does NOT spawn pane for non session.created event type', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = createMockContext()
@@ -371,15 +371,15 @@ describe('TmuxSessionManager', () => {
},
}
//#when
// when
await manager.onSessionCreated(event)
//#then
// then
expect(mockExecuteActions).toHaveBeenCalledTimes(0)
})
test('replaces oldest agent when unsplittable (small window)', async () => {
//#given - small window where split is not possible
// given - small window where split is not possible
mockIsInsideTmux.mockReturnValue(true)
mockQueryWindowState.mockImplementation(async () =>
createWindowState({
@@ -410,12 +410,12 @@ describe('TmuxSessionManager', () => {
}
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
//#when
// when
await manager.onSessionCreated(
createSessionCreatedEvent('ses_new', 'ses_parent', 'New Task')
)
//#then - with small window, replace action is used instead of close+spawn
// then - with small window, replace action is used instead of close+spawn
expect(mockExecuteActions).toHaveBeenCalledTimes(1)
const call = mockExecuteActions.mock.calls[0]
expect(call).toBeDefined()
@@ -427,7 +427,7 @@ describe('TmuxSessionManager', () => {
describe('onSessionDeleted', () => {
test('closes pane when tracked session is deleted', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
let stateCallCount = 0
@@ -471,10 +471,10 @@ describe('TmuxSessionManager', () => {
)
mockExecuteAction.mockClear()
//#when
// when
await manager.onSessionDeleted({ sessionID: 'ses_child' })
//#then
// then
expect(mockExecuteAction).toHaveBeenCalledTimes(1)
const call = mockExecuteAction.mock.calls[0]
expect(call).toBeDefined()
@@ -486,7 +486,7 @@ describe('TmuxSessionManager', () => {
})
test('does nothing when untracked session is deleted', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = createMockContext()
@@ -499,17 +499,17 @@ describe('TmuxSessionManager', () => {
}
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
//#when
// when
await manager.onSessionDeleted({ sessionID: 'ses_unknown' })
//#then
// then
expect(mockExecuteAction).toHaveBeenCalledTimes(0)
})
})
describe('cleanup', () => {
test('closes all tracked panes', async () => {
//#given
// given
mockIsInsideTmux.mockReturnValue(true)
let callCount = 0
@@ -542,10 +542,10 @@ describe('TmuxSessionManager', () => {
mockExecuteAction.mockClear()
//#when
// when
await manager.cleanup()
//#then
// then
expect(mockExecuteAction).toHaveBeenCalledTimes(2)
})
})
@@ -554,26 +554,26 @@ describe('TmuxSessionManager', () => {
describe('DecisionEngine', () => {
describe('calculateCapacity', () => {
test('calculates correct 2D grid capacity', async () => {
//#given
// given
const { calculateCapacity } = await import('./decision-engine')
//#when
// when
const result = calculateCapacity(212, 44)
//#then - availableWidth=106, cols=(106+1)/(52+1)=2, rows=(44+1)/(11+1)=3 (accounting for dividers)
// then - availableWidth=106, cols=(106+1)/(52+1)=2, rows=(44+1)/(11+1)=3 (accounting for dividers)
expect(result.cols).toBe(2)
expect(result.rows).toBe(3)
expect(result.total).toBe(6)
})
test('returns 0 cols when agent area too narrow', async () => {
//#given
// given
const { calculateCapacity } = await import('./decision-engine')
//#when
// when
const result = calculateCapacity(100, 44)
//#then - availableWidth=50, cols=50/53=0
// then - availableWidth=50, cols=50/53=0
expect(result.cols).toBe(0)
expect(result.total).toBe(0)
})
@@ -581,7 +581,7 @@ describe('DecisionEngine', () => {
describe('decideSpawnActions', () => {
test('returns spawn action with splitDirection when under capacity', async () => {
//#given
// given
const { decideSpawnActions } = await import('./decision-engine')
const state: WindowState = {
windowWidth: 212,
@@ -598,7 +598,7 @@ describe('DecisionEngine', () => {
agentPanes: [],
}
//#when
// when
const decision = decideSpawnActions(
state,
'ses_1',
@@ -607,7 +607,7 @@ describe('DecisionEngine', () => {
[]
)
//#then
// then
expect(decision.canSpawn).toBe(true)
expect(decision.actions).toHaveLength(1)
expect(decision.actions[0].type).toBe('spawn')
@@ -620,7 +620,7 @@ describe('DecisionEngine', () => {
})
test('returns replace when split not possible', async () => {
//#given - small window where split is never possible
// given - small window where split is never possible
const { decideSpawnActions } = await import('./decision-engine')
const state: WindowState = {
windowWidth: 160,
@@ -650,7 +650,7 @@ describe('DecisionEngine', () => {
{ sessionId: 'ses_old', paneId: '%1', createdAt: new Date('2024-01-01') },
]
//#when
// when
const decision = decideSpawnActions(
state,
'ses_new',
@@ -659,14 +659,14 @@ describe('DecisionEngine', () => {
sessionMappings
)
//#then - agent area (80) < MIN_SPLIT_WIDTH (105), so replace is used
// then - agent area (80) < MIN_SPLIT_WIDTH (105), so replace is used
expect(decision.canSpawn).toBe(true)
expect(decision.actions).toHaveLength(1)
expect(decision.actions[0].type).toBe('replace')
})
test('returns canSpawn=false when window too small', async () => {
//#given
// given
const { decideSpawnActions } = await import('./decision-engine')
const state: WindowState = {
windowWidth: 60,
@@ -683,7 +683,7 @@ describe('DecisionEngine', () => {
agentPanes: [],
}
//#when
// when
const decision = decideSpawnActions(
state,
'ses_1',
@@ -692,7 +692,7 @@ describe('DecisionEngine', () => {
[]
)
//#then
// then
expect(decision.canSpawn).toBe(false)
expect(decision.reason).toContain('too small')
})