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
+37 -37
View File
@@ -10,7 +10,7 @@ describe("session-notification", () => {
function createMockPluginInput() {
return {
$: async (cmd: TemplateStringsArray | string, ...values: any[]) => {
// #given - track notification commands (osascript, notify-send, powershell)
// given - track notification commands (osascript, notify-send, powershell)
const cmdStr = typeof cmd === "string"
? cmd
: cmd.reduce((acc, part, i) => acc + part + (values[i] ?? ""), "")
@@ -43,13 +43,13 @@ describe("session-notification", () => {
})
afterEach(() => {
// #given - cleanup after each test
// given - cleanup after each test
subagentSessions.clear()
_resetForTesting()
})
test("should not trigger notification for subagent session", async () => {
// #given - a subagent session exists
// given - a subagent session exists
const subagentSessionID = "subagent-123"
subagentSessions.add(subagentSessionID)
@@ -57,7 +57,7 @@ describe("session-notification", () => {
idleConfirmationDelay: 0,
})
// #when - subagent session goes idle
// when - subagent session goes idle
await hook({
event: {
type: "session.idle",
@@ -68,12 +68,12 @@ describe("session-notification", () => {
// Wait for any pending timers
await new Promise((resolve) => setTimeout(resolve, 50))
// #then - notification should NOT be sent
// then - notification should NOT be sent
expect(notificationCalls).toHaveLength(0)
})
test("should not trigger notification when mainSessionID is set and session is not main", async () => {
// #given - main session is set, but a different session goes idle
// given - main session is set, but a different session goes idle
const mainSessionID = "main-123"
const otherSessionID = "other-456"
setMainSession(mainSessionID)
@@ -82,7 +82,7 @@ describe("session-notification", () => {
idleConfirmationDelay: 0,
})
// #when - non-main session goes idle
// when - non-main session goes idle
await hook({
event: {
type: "session.idle",
@@ -93,12 +93,12 @@ describe("session-notification", () => {
// Wait for any pending timers
await new Promise((resolve) => setTimeout(resolve, 50))
// #then - notification should NOT be sent
// then - notification should NOT be sent
expect(notificationCalls).toHaveLength(0)
})
test("should trigger notification for main session when idle", async () => {
// #given - main session is set
// given - main session is set
const mainSessionID = "main-789"
setMainSession(mainSessionID)
@@ -107,7 +107,7 @@ describe("session-notification", () => {
skipIfIncompleteTodos: false,
})
// #when - main session goes idle
// when - main session goes idle
await hook({
event: {
type: "session.idle",
@@ -118,12 +118,12 @@ describe("session-notification", () => {
// Wait for idle confirmation delay + buffer
await new Promise((resolve) => setTimeout(resolve, 100))
// #then - notification should be sent
// then - notification should be sent
expect(notificationCalls.length).toBeGreaterThanOrEqual(1)
})
test("should skip notification for subagent even when mainSessionID is set", async () => {
// #given - both mainSessionID and subagent session exist
// given - both mainSessionID and subagent session exist
const mainSessionID = "main-999"
const subagentSessionID = "subagent-888"
setMainSession(mainSessionID)
@@ -133,7 +133,7 @@ describe("session-notification", () => {
idleConfirmationDelay: 0,
})
// #when - subagent session goes idle
// when - subagent session goes idle
await hook({
event: {
type: "session.idle",
@@ -144,12 +144,12 @@ describe("session-notification", () => {
// Wait for any pending timers
await new Promise((resolve) => setTimeout(resolve, 50))
// #then - notification should NOT be sent (subagent check takes priority)
// then - notification should NOT be sent (subagent check takes priority)
expect(notificationCalls).toHaveLength(0)
})
test("should handle subagentSessions and mainSessionID checks in correct order", async () => {
// #given - main session and subagent session exist
// given - main session and subagent session exist
const mainSessionID = "main-111"
const subagentSessionID = "subagent-222"
const unknownSessionID = "unknown-333"
@@ -160,7 +160,7 @@ describe("session-notification", () => {
idleConfirmationDelay: 0,
})
// #when - subagent session goes idle
// when - subagent session goes idle
await hook({
event: {
type: "session.idle",
@@ -168,7 +168,7 @@ describe("session-notification", () => {
},
})
// #when - unknown session goes idle (not main, not in subagentSessions)
// when - unknown session goes idle (not main, not in subagentSessions)
await hook({
event: {
type: "session.idle",
@@ -179,12 +179,12 @@ describe("session-notification", () => {
// Wait for any pending timers
await new Promise((resolve) => setTimeout(resolve, 50))
// #then - no notifications (subagent blocked by subagentSessions, unknown blocked by mainSessionID check)
// then - no notifications (subagent blocked by subagentSessions, unknown blocked by mainSessionID check)
expect(notificationCalls).toHaveLength(0)
})
test("should cancel pending notification on session activity", async () => {
// #given - main session is set
// given - main session is set
const mainSessionID = "main-cancel"
setMainSession(mainSessionID)
@@ -193,7 +193,7 @@ describe("session-notification", () => {
skipIfIncompleteTodos: false,
})
// #when - session goes idle
// when - session goes idle
await hook({
event: {
type: "session.idle",
@@ -201,7 +201,7 @@ describe("session-notification", () => {
},
})
// #when - activity happens before delay completes
// when - activity happens before delay completes
await hook({
event: {
type: "tool.execute.before",
@@ -212,15 +212,15 @@ describe("session-notification", () => {
// Wait for original delay to pass
await new Promise((resolve) => setTimeout(resolve, 150))
// #then - notification should NOT be sent (cancelled by activity)
// then - notification should NOT be sent (cancelled by activity)
expect(notificationCalls).toHaveLength(0)
})
test("should handle session.created event without notification", async () => {
// #given - a new session is created
// given - a new session is created
const hook = createSessionNotification(createMockPluginInput(), {})
// #when - session.created event fires
// when - session.created event fires
await hook({
event: {
type: "session.created",
@@ -233,15 +233,15 @@ describe("session-notification", () => {
// Wait for any pending timers
await new Promise((resolve) => setTimeout(resolve, 50))
// #then - no notification should be triggered
// then - no notification should be triggered
expect(notificationCalls).toHaveLength(0)
})
test("should handle session.deleted event and cleanup state", async () => {
// #given - a session exists
// given - a session exists
const hook = createSessionNotification(createMockPluginInput(), {})
// #when - session.deleted event fires
// when - session.deleted event fires
await hook({
event: {
type: "session.deleted",
@@ -254,12 +254,12 @@ describe("session-notification", () => {
// Wait for any pending timers
await new Promise((resolve) => setTimeout(resolve, 50))
// #then - no notification should be triggered
// then - no notification should be triggered
expect(notificationCalls).toHaveLength(0)
})
test("should mark session activity on message.updated event", async () => {
// #given - main session is set
// given - main session is set
const mainSessionID = "main-message"
setMainSession(mainSessionID)
@@ -268,7 +268,7 @@ describe("session-notification", () => {
skipIfIncompleteTodos: false,
})
// #when - session goes idle, then message.updated fires
// when - session goes idle, then message.updated fires
await hook({
event: {
type: "session.idle",
@@ -288,12 +288,12 @@ describe("session-notification", () => {
// Wait for idle delay to pass
await new Promise((resolve) => setTimeout(resolve, 100))
// #then - notification should NOT be sent (activity cancelled it)
// then - notification should NOT be sent (activity cancelled it)
expect(notificationCalls).toHaveLength(0)
})
test("should mark session activity on tool.execute.before event", async () => {
// #given - main session is set
// given - main session is set
const mainSessionID = "main-tool"
setMainSession(mainSessionID)
@@ -302,7 +302,7 @@ describe("session-notification", () => {
skipIfIncompleteTodos: false,
})
// #when - session goes idle, then tool.execute.before fires
// when - session goes idle, then tool.execute.before fires
await hook({
event: {
type: "session.idle",
@@ -320,12 +320,12 @@ describe("session-notification", () => {
// Wait for idle delay to pass
await new Promise((resolve) => setTimeout(resolve, 100))
// #then - notification should NOT be sent (activity cancelled it)
// then - notification should NOT be sent (activity cancelled it)
expect(notificationCalls).toHaveLength(0)
})
test("should not send duplicate notification for same session", async () => {
// #given - main session is set
// given - main session is set
const mainSessionID = "main-dup"
setMainSession(mainSessionID)
@@ -334,7 +334,7 @@ describe("session-notification", () => {
skipIfIncompleteTodos: false,
})
// #when - session goes idle twice
// when - session goes idle twice
await hook({
event: {
type: "session.idle",
@@ -355,7 +355,7 @@ describe("session-notification", () => {
// Wait for second potential notification
await new Promise((resolve) => setTimeout(resolve, 50))
// #then - only one notification should be sent
// then - only one notification should be sent
expect(notificationCalls).toHaveLength(1)
})
})