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
+60 -60
View File
@@ -4,7 +4,7 @@ import { parseModelSuggestion, promptWithModelSuggestionRetry } from "./model-su
describe("parseModelSuggestion", () => {
describe("structured NamedError format", () => {
it("should extract suggestion from ProviderModelNotFoundError", () => {
//#given a structured NamedError with suggestions
// given a structured NamedError with suggestions
const error = {
name: "ProviderModelNotFoundError",
data: {
@@ -14,10 +14,10 @@ describe("parseModelSuggestion", () => {
},
}
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should return the first suggestion
// then should return the first suggestion
expect(result).toEqual({
providerID: "anthropic",
modelID: "claude-sonet-4",
@@ -26,7 +26,7 @@ describe("parseModelSuggestion", () => {
})
it("should return null when suggestions array is empty", () => {
//#given a NamedError with empty suggestions
// given a NamedError with empty suggestions
const error = {
name: "ProviderModelNotFoundError",
data: {
@@ -36,15 +36,15 @@ describe("parseModelSuggestion", () => {
},
}
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should return null
// then should return null
expect(result).toBeNull()
})
it("should return null when suggestions field is missing", () => {
//#given a NamedError without suggestions
// given a NamedError without suggestions
const error = {
name: "ProviderModelNotFoundError",
data: {
@@ -53,17 +53,17 @@ describe("parseModelSuggestion", () => {
},
}
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should return null
// then should return null
expect(result).toBeNull()
})
})
describe("nested error format", () => {
it("should extract suggestion from nested data.error", () => {
//#given an error with nested NamedError in data field
// given an error with nested NamedError in data field
const error = {
data: {
name: "ProviderModelNotFoundError",
@@ -75,10 +75,10 @@ describe("parseModelSuggestion", () => {
},
}
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should extract from nested structure
// then should extract from nested structure
expect(result).toEqual({
providerID: "openai",
modelID: "gpt-5",
@@ -87,7 +87,7 @@ describe("parseModelSuggestion", () => {
})
it("should extract suggestion from nested error field", () => {
//#given an error with nested NamedError in error field
// given an error with nested NamedError in error field
const error = {
error: {
name: "ProviderModelNotFoundError",
@@ -99,10 +99,10 @@ describe("parseModelSuggestion", () => {
},
}
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should extract from nested error field
// then should extract from nested error field
expect(result).toEqual({
providerID: "google",
modelID: "gemini-3-flsh",
@@ -113,15 +113,15 @@ describe("parseModelSuggestion", () => {
describe("string message format", () => {
it("should parse suggestion from error message string", () => {
//#given an Error with model-not-found message and suggestion
// given an Error with model-not-found message and suggestion
const error = new Error(
"Model not found: anthropic/claude-sonet-4. Did you mean: claude-sonnet-4, claude-sonnet-4-5?"
)
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should extract from message string
// then should extract from message string
expect(result).toEqual({
providerID: "anthropic",
modelID: "claude-sonet-4",
@@ -130,14 +130,14 @@ describe("parseModelSuggestion", () => {
})
it("should parse from plain string error", () => {
//#given a plain string error message
// given a plain string error message
const error =
"Model not found: openai/gtp-5. Did you mean: gpt-5?"
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should extract from string
// then should extract from string
expect(result).toEqual({
providerID: "openai",
modelID: "gtp-5",
@@ -146,15 +146,15 @@ describe("parseModelSuggestion", () => {
})
it("should parse from object with message property", () => {
//#given an object with message property
// given an object with message property
const error = {
message: "Model not found: google/gemini-3-flsh. Did you mean: gemini-3-flash?",
}
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should extract from message property
// then should extract from message property
expect(result).toEqual({
providerID: "google",
modelID: "gemini-3-flsh",
@@ -163,48 +163,48 @@ describe("parseModelSuggestion", () => {
})
it("should return null when message has no suggestion", () => {
//#given an error without Did you mean
// given an error without Did you mean
const error = new Error("Model not found: anthropic/nonexistent.")
//#when parsing the error
// when parsing the error
const result = parseModelSuggestion(error)
//#then should return null
// then should return null
expect(result).toBeNull()
})
})
describe("edge cases", () => {
it("should return null for null error", () => {
//#given null
//#when parsing
// given null
// when parsing
const result = parseModelSuggestion(null)
//#then should return null
// then should return null
expect(result).toBeNull()
})
it("should return null for undefined error", () => {
//#given undefined
//#when parsing
// given undefined
// when parsing
const result = parseModelSuggestion(undefined)
//#then should return null
// then should return null
expect(result).toBeNull()
})
it("should return null for unrelated error", () => {
//#given an unrelated error
// given an unrelated error
const error = new Error("Connection timeout")
//#when parsing
// when parsing
const result = parseModelSuggestion(error)
//#then should return null
// then should return null
expect(result).toBeNull()
})
it("should return null for empty object", () => {
//#given empty object
//#when parsing
// given empty object
// when parsing
const result = parseModelSuggestion({})
//#then should return null
// then should return null
expect(result).toBeNull()
})
})
@@ -212,11 +212,11 @@ describe("parseModelSuggestion", () => {
describe("promptWithModelSuggestionRetry", () => {
it("should succeed on first try without retry", async () => {
//#given a client where prompt succeeds
// given a client where prompt succeeds
const promptMock = mock(() => Promise.resolve())
const client = { session: { prompt: promptMock } }
//#when calling promptWithModelSuggestionRetry
// when calling promptWithModelSuggestionRetry
await promptWithModelSuggestionRetry(client as any, {
path: { id: "session-1" },
body: {
@@ -225,12 +225,12 @@ describe("promptWithModelSuggestionRetry", () => {
},
})
//#then should call prompt exactly once
// then should call prompt exactly once
expect(promptMock).toHaveBeenCalledTimes(1)
})
it("should retry with suggestion on model-not-found error", async () => {
//#given a client that fails first with model-not-found, then succeeds
// given a client that fails first with model-not-found, then succeeds
const promptMock = mock()
.mockRejectedValueOnce({
name: "ProviderModelNotFoundError",
@@ -243,7 +243,7 @@ describe("promptWithModelSuggestionRetry", () => {
.mockResolvedValueOnce(undefined)
const client = { session: { prompt: promptMock } }
//#when calling promptWithModelSuggestionRetry
// when calling promptWithModelSuggestionRetry
await promptWithModelSuggestionRetry(client as any, {
path: { id: "session-1" },
body: {
@@ -253,7 +253,7 @@ describe("promptWithModelSuggestionRetry", () => {
},
})
//#then should call prompt twice - first with original, then with suggestion
// then should call prompt twice - first with original, then with suggestion
expect(promptMock).toHaveBeenCalledTimes(2)
const retryCall = promptMock.mock.calls[1][0]
expect(retryCall.body.model).toEqual({
@@ -263,13 +263,13 @@ describe("promptWithModelSuggestionRetry", () => {
})
it("should throw original error when no suggestion available", async () => {
//#given a client that fails with a non-model-not-found error
// given a client that fails with a non-model-not-found error
const originalError = new Error("Connection refused")
const promptMock = mock().mockRejectedValueOnce(originalError)
const client = { session: { prompt: promptMock } }
//#when calling promptWithModelSuggestionRetry
//#then should throw the original error
// when calling promptWithModelSuggestionRetry
// then should throw the original error
await expect(
promptWithModelSuggestionRetry(client as any, {
path: { id: "session-1" },
@@ -284,7 +284,7 @@ describe("promptWithModelSuggestionRetry", () => {
})
it("should throw original error when retry also fails", async () => {
//#given a client that fails with model-not-found, retry also fails
// given a client that fails with model-not-found, retry also fails
const modelNotFoundError = {
name: "ProviderModelNotFoundError",
data: {
@@ -299,8 +299,8 @@ describe("promptWithModelSuggestionRetry", () => {
.mockRejectedValueOnce(retryError)
const client = { session: { prompt: promptMock } }
//#when calling promptWithModelSuggestionRetry
//#then should throw the retry error (not the original)
// when calling promptWithModelSuggestionRetry
// then should throw the retry error (not the original)
await expect(
promptWithModelSuggestionRetry(client as any, {
path: { id: "session-1" },
@@ -315,7 +315,7 @@ describe("promptWithModelSuggestionRetry", () => {
})
it("should preserve other body fields during retry", async () => {
//#given a client that fails first with model-not-found
// given a client that fails first with model-not-found
const promptMock = mock()
.mockRejectedValueOnce({
name: "ProviderModelNotFoundError",
@@ -328,7 +328,7 @@ describe("promptWithModelSuggestionRetry", () => {
.mockResolvedValueOnce(undefined)
const client = { session: { prompt: promptMock } }
//#when calling with additional body fields
// when calling with additional body fields
await promptWithModelSuggestionRetry(client as any, {
path: { id: "session-1" },
body: {
@@ -341,7 +341,7 @@ describe("promptWithModelSuggestionRetry", () => {
},
})
//#then retry call should preserve all fields except corrected model
// then retry call should preserve all fields except corrected model
const retryCall = promptMock.mock.calls[1][0]
expect(retryCall.body.agent).toBe("explore")
expect(retryCall.body.system).toBe("You are a helpful agent")
@@ -354,7 +354,7 @@ describe("promptWithModelSuggestionRetry", () => {
})
it("should handle string error message with suggestion", async () => {
//#given a client that fails with a string error containing suggestion
// given a client that fails with a string error containing suggestion
const promptMock = mock()
.mockRejectedValueOnce(
new Error("Model not found: anthropic/claude-sonet-4. Did you mean: claude-sonnet-4?")
@@ -362,7 +362,7 @@ describe("promptWithModelSuggestionRetry", () => {
.mockResolvedValueOnce(undefined)
const client = { session: { prompt: promptMock } }
//#when calling promptWithModelSuggestionRetry
// when calling promptWithModelSuggestionRetry
await promptWithModelSuggestionRetry(client as any, {
path: { id: "session-1" },
body: {
@@ -371,22 +371,22 @@ describe("promptWithModelSuggestionRetry", () => {
},
})
//#then should retry with suggested model
// then should retry with suggested model
expect(promptMock).toHaveBeenCalledTimes(2)
const retryCall = promptMock.mock.calls[1][0]
expect(retryCall.body.model.modelID).toBe("claude-sonnet-4")
})
it("should not retry when no model in original request", async () => {
//#given a client that fails with model-not-found but original has no model param
// given a client that fails with model-not-found but original has no model param
const modelNotFoundError = new Error(
"Model not found: anthropic/claude-sonet-4. Did you mean: claude-sonnet-4?"
)
const promptMock = mock().mockRejectedValueOnce(modelNotFoundError)
const client = { session: { prompt: promptMock } }
//#when calling without model in body
//#then should throw without retrying
// when calling without model in body
// then should throw without retrying
await expect(
promptWithModelSuggestionRetry(client as any, {
path: { id: "session-1" },