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)