diff --git a/packages/core/src/session/runner/llm.ts b/packages/core/src/session/runner/llm.ts index ca6cc08848c..c1551d25fdb 100644 --- a/packages/core/src/session/runner/llm.ts +++ b/packages/core/src/session/runner/llm.ts @@ -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 diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index ab600c6e9cf..7aac3564939 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -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() diff --git a/packages/plugin/src/README.md b/packages/plugin/src/README.md index aea1fe8642c..4188a4088e8 100644 --- a/packages/plugin/src/README.md +++ b/packages/plugin/src/README.md @@ -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 diff --git a/packages/plugin/src/effect/README.md b/packages/plugin/src/effect/README.md index be8e6a68cb8..6de593c1926 100644 --- a/packages/plugin/src/effect/README.md +++ b/packages/plugin/src/effect/README.md @@ -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 diff --git a/packages/plugin/src/effect/session.ts b/packages/plugin/src/effect/session.ts index 698dacef46b..140e2471575 100644 --- a/packages/plugin/src/effect/session.ts +++ b/packages/plugin/src/effect/session.ts @@ -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 diff --git a/packages/plugin/src/promise/session.ts b/packages/plugin/src/promise/session.ts index 02cff0815e5..caa7f73d94b 100644 --- a/packages/plugin/src/promise/session.ts +++ b/packages/plugin/src/promise/session.ts @@ -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