f146aeff0f
* 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>
271 lines
7.4 KiB
TypeScript
271 lines
7.4 KiB
TypeScript
import { describe, it, expect } from "bun:test"
|
|
import { createEventState, serializeError, type EventState } from "./events"
|
|
import type { RunContext, EventPayload } from "./types"
|
|
|
|
const createMockContext = (sessionID: string = "test-session"): RunContext => ({
|
|
client: {} as RunContext["client"],
|
|
sessionID,
|
|
directory: "/test",
|
|
abortController: new AbortController(),
|
|
})
|
|
|
|
async function* toAsyncIterable<T>(items: T[]): AsyncIterable<T> {
|
|
for (const item of items) {
|
|
yield item
|
|
}
|
|
}
|
|
|
|
describe("serializeError", () => {
|
|
it("returns 'Unknown error' for null/undefined", () => {
|
|
// given / when / then
|
|
expect(serializeError(null)).toBe("Unknown error")
|
|
expect(serializeError(undefined)).toBe("Unknown error")
|
|
})
|
|
|
|
it("returns message from Error instance", () => {
|
|
// given
|
|
const error = new Error("Something went wrong")
|
|
|
|
// when / then
|
|
expect(serializeError(error)).toBe("Something went wrong")
|
|
})
|
|
|
|
it("returns string as-is", () => {
|
|
// given / when / then
|
|
expect(serializeError("Direct error message")).toBe("Direct error message")
|
|
})
|
|
|
|
it("extracts message from plain object", () => {
|
|
// given
|
|
const errorObj = { message: "Object error message", code: "ERR_001" }
|
|
|
|
// when / then
|
|
expect(serializeError(errorObj)).toBe("Object error message")
|
|
})
|
|
|
|
it("extracts message from nested error object", () => {
|
|
// given
|
|
const errorObj = { error: { message: "Nested error message" } }
|
|
|
|
// when / then
|
|
expect(serializeError(errorObj)).toBe("Nested error message")
|
|
})
|
|
|
|
it("extracts message from data.message path", () => {
|
|
// given
|
|
const errorObj = { data: { message: "Data error message" } }
|
|
|
|
// when / then
|
|
expect(serializeError(errorObj)).toBe("Data error message")
|
|
})
|
|
|
|
it("JSON stringifies object without message property", () => {
|
|
// given
|
|
const errorObj = { code: "ERR_001", status: 500 }
|
|
|
|
// when
|
|
const result = serializeError(errorObj)
|
|
|
|
// then
|
|
expect(result).toContain("ERR_001")
|
|
expect(result).toContain("500")
|
|
})
|
|
})
|
|
|
|
describe("createEventState", () => {
|
|
it("creates initial state with correct defaults", () => {
|
|
// given / when
|
|
const state = createEventState()
|
|
|
|
// then
|
|
expect(state.mainSessionIdle).toBe(false)
|
|
expect(state.lastOutput).toBe("")
|
|
expect(state.lastPartText).toBe("")
|
|
expect(state.currentTool).toBe(null)
|
|
expect(state.hasReceivedMeaningfulWork).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe("event handling", () => {
|
|
it("session.idle sets mainSessionIdle to true for matching session", async () => {
|
|
// given
|
|
const ctx = createMockContext("my-session")
|
|
const state = createEventState()
|
|
|
|
const payload: EventPayload = {
|
|
type: "session.idle",
|
|
properties: { sessionID: "my-session" },
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then
|
|
expect(state.mainSessionIdle).toBe(true)
|
|
})
|
|
|
|
it("session.idle does not affect state for different session", async () => {
|
|
// given
|
|
const ctx = createMockContext("my-session")
|
|
const state = createEventState()
|
|
|
|
const payload: EventPayload = {
|
|
type: "session.idle",
|
|
properties: { sessionID: "other-session" },
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then
|
|
expect(state.mainSessionIdle).toBe(false)
|
|
})
|
|
|
|
it("hasReceivedMeaningfulWork is false initially after session.idle", async () => {
|
|
// given - session goes idle without any assistant output (race condition scenario)
|
|
const ctx = createMockContext("my-session")
|
|
const state = createEventState()
|
|
|
|
const payload: EventPayload = {
|
|
type: "session.idle",
|
|
properties: { sessionID: "my-session" },
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then - idle but no meaningful work yet
|
|
expect(state.mainSessionIdle).toBe(true)
|
|
expect(state.hasReceivedMeaningfulWork).toBe(false)
|
|
})
|
|
|
|
it("message.updated with assistant role sets hasReceivedMeaningfulWork", async () => {
|
|
// given
|
|
const ctx = createMockContext("my-session")
|
|
const state = createEventState()
|
|
|
|
const payload: EventPayload = {
|
|
type: "message.updated",
|
|
properties: {
|
|
info: { sessionID: "my-session", role: "assistant" },
|
|
},
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then
|
|
expect(state.hasReceivedMeaningfulWork).toBe(true)
|
|
})
|
|
|
|
it("message.updated with user role does not set hasReceivedMeaningfulWork", async () => {
|
|
// given - user message should not count as meaningful work
|
|
const ctx = createMockContext("my-session")
|
|
const state = createEventState()
|
|
|
|
const payload: EventPayload = {
|
|
type: "message.updated",
|
|
properties: {
|
|
info: { sessionID: "my-session", role: "user" },
|
|
},
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then - user role should not count as meaningful work
|
|
expect(state.hasReceivedMeaningfulWork).toBe(false)
|
|
})
|
|
|
|
it("tool.execute sets hasReceivedMeaningfulWork", async () => {
|
|
// given
|
|
const ctx = createMockContext("my-session")
|
|
const state = createEventState()
|
|
|
|
const payload: EventPayload = {
|
|
type: "tool.execute",
|
|
properties: {
|
|
sessionID: "my-session",
|
|
name: "read_file",
|
|
input: { filePath: "/src/index.ts" },
|
|
},
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then
|
|
expect(state.hasReceivedMeaningfulWork).toBe(true)
|
|
})
|
|
|
|
it("tool.execute from different session does not set hasReceivedMeaningfulWork", async () => {
|
|
// given
|
|
const ctx = createMockContext("my-session")
|
|
const state = createEventState()
|
|
|
|
const payload: EventPayload = {
|
|
type: "tool.execute",
|
|
properties: {
|
|
sessionID: "other-session",
|
|
name: "read_file",
|
|
input: { filePath: "/src/index.ts" },
|
|
},
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then - different session's tool call shouldn't count
|
|
expect(state.hasReceivedMeaningfulWork).toBe(false)
|
|
})
|
|
|
|
it("session.status with busy type sets mainSessionIdle to false", async () => {
|
|
// given
|
|
const ctx = createMockContext("my-session")
|
|
const state: EventState = {
|
|
mainSessionIdle: true,
|
|
mainSessionError: false,
|
|
lastError: null,
|
|
lastOutput: "",
|
|
lastPartText: "",
|
|
currentTool: null,
|
|
hasReceivedMeaningfulWork: false,
|
|
}
|
|
|
|
const payload: EventPayload = {
|
|
type: "session.status",
|
|
properties: { sessionID: "my-session", status: { type: "busy" } },
|
|
}
|
|
|
|
const events = toAsyncIterable([payload])
|
|
const { processEvents } = await import("./events")
|
|
|
|
// when
|
|
await processEvents(ctx, events, state)
|
|
|
|
// then
|
|
expect(state.mainSessionIdle).toBe(false)
|
|
})
|
|
})
|