From 96192c19ffe837005ecc3e2a3574ffad5b5d36d7 Mon Sep 17 00:00:00 2001 From: Filip Hejmowski Date: Mon, 26 Oct 2026 12:17:29 +0100 Subject: [PATCH] fix(core): run the idle hook after a move or reload at the idle boundary --- packages/core/src/session/execution.ts | 7 +++-- packages/core/src/session/runner/index.ts | 6 ++-- packages/core/src/session/runner/llm.ts | 15 ++++++--- packages/core/test/session-runner.test.ts | 38 ++++++++++++++++++++++- 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/packages/core/src/session/execution.ts b/packages/core/src/session/execution.ts index fb7a9d3a644..ce11a7a0f97 100644 --- a/packages/core/src/session/execution.ts +++ b/packages/core/src/session/execution.ts @@ -86,11 +86,12 @@ export const layer = Layer.effect( force: boolean, continuation?: SessionRunner.Continuation, promotable: SessionInbox.Promotable = "input", + stepped = false, ): Effect.fn.Return { const session = yield* store.get(sessionID) if (!session) return yield* Effect.die(new Error(`Session not found: ${sessionID}`)) const result = yield* SessionRunner.Service.use((runner) => - runner.drain({ sessionID, force, continuation, promotable }), + runner.drain({ sessionID, force, continuation, promotable, stepped }), ).pipe( instances.provide(session), Effect.tapCause((cause) => @@ -101,8 +102,8 @@ export const layer = Layer.effect( ) return yield* SessionRunner.DrainResult.$match(result, { Complete: () => Effect.void, - Moved: (result) => drain(sessionID, false, result.continuation, promotable), - Reloaded: (result) => drain(sessionID, result.force, result.continuation, promotable), + Moved: (result) => drain(sessionID, false, result.continuation, promotable, result.stepped), + Reloaded: (result) => drain(sessionID, result.force, result.continuation, promotable, result.stepped), }) }) const coordinator = yield* SessionRunCoordinator.make({ diff --git a/packages/core/src/session/runner/index.ts b/packages/core/src/session/runner/index.ts index c646d493867..9f32886248b 100644 --- a/packages/core/src/session/runner/index.ts +++ b/packages/core/src/session/runner/index.ts @@ -22,8 +22,8 @@ export type Continuation = { readonly step: number } export type DrainResult = Data.TaggedEnum<{ Complete: {} - Moved: { readonly continuation?: Continuation } - Reloaded: { readonly force: boolean; readonly continuation?: Continuation } + Moved: { readonly continuation?: Continuation; readonly stepped?: true } + Reloaded: { readonly force: boolean; readonly continuation?: Continuation; readonly stepped?: true } }> export const DrainResult = Data.taggedEnum() @@ -36,6 +36,8 @@ export interface Interface { readonly continuation?: Continuation /** "steer" settles the active intent without promoting queued next-turn work. */ readonly promotable?: Promotable + /** The previous drain ran a step before handing off, so reaching idle still runs the idle hook. */ + readonly stepped?: boolean }) => Effect.Effect } diff --git a/packages/core/src/session/runner/llm.ts b/packages/core/src/session/runner/llm.ts index 543f60ee1b0..3e1eedc24fd 100644 --- a/packages/core/src/session/runner/llm.ts +++ b/packages/core/src/session/runner/llm.ts @@ -64,9 +64,9 @@ const layer = Layer.effect( let continuing = input.continuation !== undefined let step = input.continuation?.step ?? 1 let entering = true - let stepped = false + let stepped = input.stepped ?? false const promotable = input.promotable ?? "input" - if (!force && !continuing) { + if (!force && !continuing && !stepped) { const pending = yield* SessionInbox.nextPromotable(db, sessionID, "input") if (!pending) return DrainResult.Complete() const control = pending.type === "compaction" || pending.type === "move" @@ -82,7 +82,11 @@ const layer = Layer.effect( while (true) { if (lifecycle.isClosed()) { yield* restore(modelTransport.close(sessionID)) - return DrainResult.Reloaded({ force, continuation: continuing ? { step } : undefined }) + return DrainResult.Reloaded({ + force, + continuation: continuing ? { step } : undefined, + stepped: !continuing && stepped ? true : undefined, + }) } // Location entry and idle boundaries allow queued controls, not necessarily queued prompts. const pending = yield* SessionInbox.serialized( @@ -116,7 +120,10 @@ const layer = Layer.effect( step = 1 } if (pending?.type === "move") - return DrainResult.Moved({ continuation: continuing ? { step } : undefined }) + return DrainResult.Moved({ + continuation: continuing ? { step } : undefined, + stepped: !continuing && stepped ? true : undefined, + }) if (pending?.type === "compaction") { const compacted = yield* restore( Effect.gen(function* () { diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index 1018e5cc076..8f954ca8494 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -1256,6 +1256,42 @@ describe("SessionRunnerLLM", () => { ]) }) + scenario("runs the idle hook after a move delivered at the idle boundary", function* (s) { + const runner = yield* SessionRunner.Service + const hooks = yield* PluginHooks.Service + const idles: Session.ID[] = [] + yield* hooks.register("session", "idle", (event) => Effect.sync(() => idles.push(event.sessionID))) + yield* s.admit("Before moving") + yield* s.llm.push(TestLLM.text("Done", "text-done")) + const stream = yield* s.llm.gate + const run = yield* runner.drain({ sessionID, force: false }).pipe(Effect.forkChild) + yield* stream.started + yield* s.sessionInbox.admit({ + id: SessionMessage.ID.create(), + sessionID, + item: { + type: "move", + payload: { + location: Location.Ref.make({ directory: AbsolutePath.make("/project") }), + projectID: Project.ID.global, + }, + delivery: "queue", + }, + }) + yield* stream.release + const moved = yield* Fiber.join(run) + + expect(moved).toEqual(SessionRunner.DrainResult.Moved({ stepped: true })) + expect(idles).toEqual([]) + if (moved._tag !== "Moved") throw new Error("Expected a Location handoff") + + // The destination drain has nothing to promote but still owes the idle hook for the step before the move. + yield* runner.drain({ sessionID, force: false, stepped: moved.stepped }) + + expect(idles).toEqual([sessionID]) + expect(s.requests).toHaveLength(1) + }) + scenario("passes no background work to the idle hook when none is running", function* (s) { const hooks = yield* PluginHooks.Service const events: SessionHooks["idle"][] = [] @@ -4426,7 +4462,7 @@ describe("SessionRunnerLLM", () => { yield* stream.release expect({ result: yield* Fiber.join(run), location: (yield* s.session.get(sessionID)).location }).toEqual({ - result: SessionRunner.DrainResult.Moved({}), + result: SessionRunner.DrainResult.Moved({ stepped: true }), location, }) expect(s.requests.map(userTexts)).toEqual([["A"], ["A"]])