fix(core): run the idle hook after a move or reload at the idle boundary

This commit is contained in:
Filip Hejmowski 2026-10-26 12:17:29 +01:00
parent 8304ef1716
commit 96192c19ff
4 changed files with 56 additions and 10 deletions

View file

@ -86,11 +86,12 @@ export const layer = Layer.effect(
force: boolean,
continuation?: SessionRunner.Continuation,
promotable: SessionInbox.Promotable = "input",
stepped = false,
): Effect.fn.Return<void, SessionRunner.RunError> {
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<SessionSchema.ID, SessionRunner.RunError, InterruptReason>({

View file

@ -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<DrainResult>()
@ -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<DrainResult, RunError>
}

View file

@ -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* () {

View file

@ -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"]])