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
+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')
})