fix(hooks): settle idle prompt continuations

This commit is contained in:
YeonGyu-Kim
2026-05-10 12:55:36 +09:00
parent 806842981f
commit 1ea4dfe215
8 changed files with 118 additions and 1 deletions
@@ -113,6 +113,38 @@ afterEach(async () => {
})
describe("createTeamIdleWakeHint", () => {
test("settles idle before sending the wake hint", async () => {
// given
const baseDir = await createTemporaryBaseDir()
const config = createConfig(baseDir)
const teamRunId = randomUUID()
await seedRuntimeState(createRuntimeState(teamRunId), config)
await seedUnreadMessage(teamRunId, config, randomUUID(), "first message body", 100)
const promptAsyncSpy = mock(async (_input: WakeHintPromptInput) => ({}))
const handler = createTeamIdleWakeHint({
directory: "/tmp/project",
client: { session: { promptAsync: promptAsyncSpy } },
}, config, { idleSettleMs: 50 })
// when
const startedAt = Date.now()
const eventPromise = handler({
event: {
type: "session.idle",
properties: { sessionID: "member-session" },
},
})
await Promise.resolve()
// then
expect(promptAsyncSpy).not.toHaveBeenCalled()
await eventPromise
expect(Date.now() - startedAt).toBeGreaterThanOrEqual(45)
expect(promptAsyncSpy).toHaveBeenCalledTimes(1)
})
test("sends a trigger-only wake hint when new unread mail exists", async () => {
// given
const baseDir = await createTemporaryBaseDir()
@@ -8,6 +8,7 @@ import {
buildMemberPromptBody,
} from "../../features/team-mode/member-session-routing"
import { log } from "../../shared/logger"
import { settleAfterSessionIdle } from "../shared/session-idle-settle"
type PromptAsyncInput = {
path: { id: string }
@@ -31,6 +32,7 @@ type TeamIdleWakeHintContext = {
type HookInput = { event: { type: string; properties?: unknown } }
export type HookImpl = (input: HookInput) => Promise<void>
type TeamIdleWakeHintOptions = { idleSettleMs?: number }
function getIdleSessionID(properties: unknown): string | undefined {
const record = properties as { sessionID?: string } | undefined
@@ -41,7 +43,7 @@ function buildWakeHint(unreadCount: number): string {
return `You have ${unreadCount} new team messages. They will be injected on your next turn.`
}
export function createTeamIdleWakeHint(ctx: TeamIdleWakeHintContext, config: TeamModeConfig): HookImpl {
export function createTeamIdleWakeHint(ctx: TeamIdleWakeHintContext, config: TeamModeConfig, options?: TeamIdleWakeHintOptions): HookImpl {
return async ({ event }: HookInput): Promise<void> => {
if (event.type !== "session.idle") return
@@ -97,6 +99,7 @@ export function createTeamIdleWakeHint(ctx: TeamIdleWakeHintContext, config: Tea
}
applyMemberSessionRouting(sessionID, memberEntry)
await settleAfterSessionIdle(options?.idleSettleMs)
await ctx.client.session.promptAsync({
path: { id: sessionID },