fix(core): skip the idle hook for control-only drains

This commit is contained in:
Filip Hejmowski 2026-10-23 19:53:24 +02:00
parent eee91dfad9
commit 5cac47adce
6 changed files with 57 additions and 13 deletions

View file

@ -61,6 +61,7 @@ const layer = Layer.effect(
let continuing = input.continuation !== undefined
let step = input.continuation?.step ?? 1
let entering = true
let stepped = false
const promotable = input.promotable ?? "input"
if (!force && !continuing) {
const pending = yield* SessionInbox.nextPromotable(db, sessionID, "input")
@ -158,9 +159,9 @@ const layer = Layer.effect(
force = false
continue
}
if (!force && !continuing && pending?.delivery === "queue" && promotable === "steer")
return DrainResult.Complete()
if (!force && !continuing && !pending) {
if (!force && !continuing && (!pending || (pending.delivery === "queue" && promotable === "steer"))) {
// Control-only drains such as a manual compaction never ran the model, so they skip the hook.
if (pending || !stepped) return DrainResult.Complete()
// The Session would go idle; a plugin may admit input to keep this execution working.
yield* restore(hooks.trigger("session", "idle", { sessionID }))
if (!(yield* SessionInbox.has(db, sessionID, "input"))) return DrainResult.Complete()
@ -195,6 +196,7 @@ const layer = Layer.effect(
const next = yield* advanceToStep()
if (next._tag !== "Ready") return next
continuing = yield* runStep(next.context, step)
stepped = true
step++
force = false
entering = false

View file

@ -1208,6 +1208,46 @@ describe("SessionRunnerLLM", () => {
expect(idles).toEqual([2])
})
scenario("keeps idle hook input pending across a compaction admitted with it", function* (s) {
const hooks = yield* PluginHooks.Service
const idles: Session.ID[] = []
yield* hooks.register("session", "idle", (event) =>
Effect.gen(function* () {
idles.push(event.sessionID)
if (idles.length > 1) return
yield* s.session.synthetic({ sessionID, text: "Keep going", resume: false }).pipe(Effect.orDie)
yield* s.session.compact({ sessionID }).pipe(Effect.orDie)
}),
)
yield* s.llm.push(
TestLLM.text("First answer", "text-first"),
TestLLM.text("## Objective\n- summary", "hook-summary"),
TestLLM.text("Second answer", "text-second"),
)
yield* s.runPrompt("Start")
expect(idles).toEqual([sessionID, sessionID])
expect(s.requests).toHaveLength(3)
expect(userTexts(s.requests[2])).toContain("Keep going")
expect((yield* s.context).filter((message) => message.type === "synthetic")).toHaveLength(1)
expect(yield* s.inbox).toEqual([])
})
scenario("does not run the session idle hook after manual compaction of an idle session", function* (s) {
yield* s.runPrompt("First")
const hooks = yield* PluginHooks.Service
const idles: Session.ID[] = []
yield* hooks.register("session", "idle", (event) => Effect.sync(() => idles.push(event.sessionID)))
yield* s.llm.push(TestLLM.text("## Objective\n- summary", "idle-summary"))
yield* s.session.compact({ sessionID })
yield* s.resume
expect(s.requests).toHaveLength(2)
expect(idles).toEqual([])
})
scenario("executes the tool advertised before a registry reload", function* (s) {
const registry = yield* Tool.Service
const scope = yield* Scope.make()

View file

@ -123,8 +123,8 @@ await ctx.session.hook("retry", (event) => {
})
```
When a Session finishes its work with no pending input, the `idle` hook runs just before it goes idle. It does not run
after interruption or failure. Input the hook admits runs in the same execution instead of the Session going idle, so a
When a Session finishes model work with no pending input, the `idle` hook runs just before it goes idle. It does not
run after interruption, failure, or control-only work such as a manual compaction. Input the hook admits runs in the same execution instead of the Session going idle, so a
plugin that always admits input must bound its own loop:
```ts

View file

@ -125,8 +125,8 @@ yield *
)
```
When a Session finishes its work with no pending input, the `idle` hook runs just before it goes idle. It does not run
after interruption or failure. Input the hook admits runs in the same execution instead of the Session going idle, so a
When a Session finishes model work with no pending input, the `idle` hook runs just before it goes idle. It does not
run after interruption, failure, or control-only work such as a manual compaction. Input the hook admits runs in the same execution instead of the Session going idle, so a
plugin that always admits input must bound its own loop:
```ts

View file

@ -137,9 +137,10 @@ export interface SessionRetry {
}
/**
* Runs when a Session finishes its work and has no pending input, just before it goes idle.
* It does not run after interruption or failure. Input admitted by the hook, such as a
* synthetic message, runs in the same execution instead of the Session going idle.
* Runs when a Session finishes model work and has no pending input, just before it goes idle.
* It does not run after interruption, failure, or control-only work such as a manual compaction.
* Input admitted by the hook, such as a synthetic message, runs in the same execution instead
* of the Session going idle.
*/
export interface SessionIdle {
readonly sessionID: Session.ID

View file

@ -137,9 +137,10 @@ export interface SessionRetry {
}
/**
* Runs when a Session finishes its work and has no pending input, just before it goes idle.
* It does not run after interruption or failure. Input admitted by the hook, such as a
* synthetic message, runs in the same execution instead of the Session going idle.
* Runs when a Session finishes model work and has no pending input, just before it goes idle.
* It does not run after interruption, failure, or control-only work such as a manual compaction.
* Input admitted by the hook, such as a synthetic message, runs in the same execution instead
* of the Session going idle.
*/
export interface SessionIdle {
readonly sessionID: Session.ID