diff --git a/packages/core/src/session/background.ts b/packages/core/src/session/background.ts index 822974790c6..1a247a7afa6 100644 --- a/packages/core/src/session/background.ts +++ b/packages/core/src/session/background.ts @@ -7,8 +7,8 @@ import type { SessionStore } from "./store.js" /** * Background jobs still running for a Session or any of its descendants. A child's own - * background shell counts even after the child reported to its parent: its completion - * notice wakes the child, which can still change what the parent hears. + * background shell counts even after the child reported to its parent: its notice wakes + * the child again, so the child's work is not finished. */ export const outstanding = Effect.fnUntraced(function* ( jobs: Pick, diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index 8f954ca8494..7a3093600ea 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -1256,6 +1256,35 @@ describe("SessionRunnerLLM", () => { ]) }) + scenario("runs the idle hook again when a background notice wakes the session", function* (s) { + const jobs = yield* Job.Service + const hooks = yield* PluginHooks.Service + const done = yield* Deferred.make() + yield* jobs.start({ + id: "shell-own", + type: "shell", + recovery: { kind: "shell", sessionID, shellID: "shell-own", command: "bun test" }, + run: Deferred.await(done), + }) + yield* jobs.background("shell-own") + const background: number[] = [] + yield* hooks.register("session", "idle", (event) => Effect.sync(() => background.push(event.background.length))) + yield* s.llm.push( + TestLLM.text("Waiting for the tests", "text-waiting"), + TestLLM.text("Tests passed", "text-passed"), + ) + + yield* s.runPrompt("Start") + yield* Deferred.succeed(done, "ok") + yield* jobs.wait({ id: "shell-own" }) + // Admitted the way the shell tool delivers a background notice: the default resume wakes the Session. + yield* s.session.synthetic({ sessionID, text: "Command finished" }).pipe(Effect.orDie) + yield* s.session.wait(sessionID) + + expect(background).toEqual([1, 0]) + expect(s.requests).toHaveLength(2) + }) + 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 diff --git a/packages/plugin/src/README.md b/packages/plugin/src/README.md index 50ae448ff93..0ab7a863084 100644 --- a/packages/plugin/src/README.md +++ b/packages/plugin/src/README.md @@ -128,7 +128,8 @@ run after interruption, failure, or control-only work such as a manual compactio plugin that always admits input must bound its own loop. `event.background` lists background shells and subagents still running for the Session or its descendants. When one -finishes, its notice wakes the Session and the hook runs again, so a plugin that waits for background work can return: +finishes, its notice wakes the Session and the hook runs again, so a plugin that waits for background work can return. +Notices admitted with `resume: false`, such as those recovered after a restart, wait for the next wake instead: ```ts await ctx.session.hook("idle", async (event) => { @@ -137,6 +138,11 @@ await ctx.session.hook("idle", async (event) => { }) ``` +A continuation runs inside the same execution, so a loop that is still going when the server restarts is resumed by +restart recovery and the hook keeps running. Persist your own state if the loop should come back paused. A hook that +fails, fails the execution like any other session hook, so handle errors from slow or remote work such as a judge model +call. + Promise tools use complete executable tool values with async executors: ```ts diff --git a/packages/plugin/src/effect/README.md b/packages/plugin/src/effect/README.md index 9028e80c47e..504c1a6965f 100644 --- a/packages/plugin/src/effect/README.md +++ b/packages/plugin/src/effect/README.md @@ -130,7 +130,8 @@ run after interruption, failure, or control-only work such as a manual compactio plugin that always admits input must bound its own loop. `event.background` lists background shells and subagents still running for the Session or its descendants. When one -finishes, its notice wakes the Session and the hook runs again, so a plugin that waits for background work can return: +finishes, its notice wakes the Session and the hook runs again, so a plugin that waits for background work can return. +Notices admitted with `resume: false`, such as those recovered after a restart, wait for the next wake instead: ```ts yield * @@ -143,6 +144,11 @@ yield * ) ``` +A continuation runs inside the same execution, so a loop that is still going when the server restarts is resumed by +restart recovery and the hook keeps running. Persist your own state if the loop should come back paused. A hook that +fails, fails the execution like any other session hook, so handle errors from slow or remote work such as a judge model +call. + ## Reloading A Domain When data captured by a transform changes, reload the affected domain: diff --git a/packages/plugin/src/effect/session.ts b/packages/plugin/src/effect/session.ts index fee22f5163f..00ca29b8f19 100644 --- a/packages/plugin/src/effect/session.ts +++ b/packages/plugin/src/effect/session.ts @@ -162,7 +162,8 @@ export type SessionIdleBackground = * of the Session going idle. * * `background` lists work that is still running. When it finishes, its notice wakes the Session - * and the hook runs again, so a plugin that waits for background work can simply return. + * and the hook runs again, so a plugin that waits for background work can simply return. Notices + * admitted with `resume: false`, such as those recovered after a restart, wait for the next wake. */ export interface SessionIdle { readonly sessionID: Session.ID diff --git a/packages/plugin/src/promise/session.ts b/packages/plugin/src/promise/session.ts index ed6b5d9f2c1..2932d575a0c 100644 --- a/packages/plugin/src/promise/session.ts +++ b/packages/plugin/src/promise/session.ts @@ -162,7 +162,8 @@ export type SessionIdleBackground = * of the Session going idle. * * `background` lists work that is still running. When it finishes, its notice wakes the Session - * and the hook runs again, so a plugin that waits for background work can simply return. + * and the hook runs again, so a plugin that waits for background work can simply return. Notices + * admitted with `resume: false`, such as those recovered after a restart, wait for the next wake. */ export interface SessionIdle { readonly sessionID: Session.ID