Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(orchestration): hold no request-end hook where no notice can be h…
…eld (FIX-2123)

The send at the filing request's end registered a request-end hook at
every filing on a board that delivers. A context built outside a
request's run (testBlock's) never runs its hooks, and the engine keeps
owed hooks process-wide: every later host shutdown in the process then
waited out its 10 s drain budget. On CI's shared module cache that made
wait-on-task.test.ts time out test by test.

The hook is registered only where a notice can be held for the request:
a host that records which requests have ended (`requestHost.waits.ended`).
Anywhere else nothing is held, so nothing needs sending at its end.

Regression test: a row filed under testBlock, then a host's dispose
finishes in under 2 s (it took the 10 s budget before).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VaN4uTiYqiabCbsFLWM52A
  • Loading branch information
claude committed Oct 23, 2026
commit 0501d15228db0cbc4473d2182bb398c448f212ed
6 changes: 6 additions & 0 deletions packages/orchestration/src/tasks/notice/delivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ export function createDelivery(
};

function whenRequestEnds(send: () => Promise<void>): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reuse: whenRequestEnds mirrors the onRequestEnded + skip-suspended pattern already in wait-on-task / task-request. Not wrong to duplicate once, but a tiny shared registrar would keep park-timing rationale in one place if more features register end hooks.

// Only where a notice can be held for this request: a host that records
// which requests have ended. Anywhere else nothing is held for it, and a
// hook would be owed for nothing: a context built outside a request's
// run (a test harness's) never runs its hooks, and a host's shutdown
// waits out its drain budget for every hook still owed in the process.
if (ctx.requestHost?.waits?.ended === undefined) return;
onRequestEnded(ctx, async (ending) => {
if (ending.status === "suspended") return;
try {
Expand Down
37 changes: 37 additions & 0 deletions packages/orchestration/test/tasks/notice-claim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { afterEach, describe, expect, it } from "vitest";
import type { FlowInstance, GeneratorModelCallOptions } from "@flow-state-dev/core/types";
import { inMemoryStores, type StoreAdapter } from "@flow-state-dev/engine";
import { sqliteStores } from "@flow-state-dev/store-sqlite";
import { dispatcher, handler } from "@flow-state-dev/core";
import { testBlock } from "@flow-state-dev/testing";
import { z } from "zod";
import type { TaskSettledInput } from "../../src";
import { defineTaskCollection, type TaskCollectionRef, type TaskWorker } from "../../src/tasks";
import { taskBoard } from "../../src/task-board";
import {
fileCall,
finalAnswer,
Expand Down Expand Up @@ -173,3 +178,35 @@ describe.each(STORES)("a task's ending is heard once by its filer (%s)", { timeo
}
});
});

describe("a row filed by a block run outside a request (FIX-2123)", () => {
it("leaves nothing for a host's shutdown to wait on: its context records no ended requests, so no notice is held for it", async () => {
// `testBlock` builds a request's context but never ends the request. A
// request-end hook registered there stays owed in the process, and every
// host's shutdown in it waits out its drain budget for it.
const board = taskBoard({
name: "outside-run",
boardId: "outside-run",
mode: "delegation",
collection: defineTaskCollection({ id: "outside-run-ledger", scope: "user" }),
workers: { implement: dispatcher({ name: "outside-impl", type: "task", action: "implement", session: "per-task" }) as unknown as TaskWorker }
});
const file = handler({
name: "outside-file",
inputSchema: z.unknown(),
uses: [board.capability],
execute: async (_input, ctx) => {
const tasks = await (ctx.cap as Record<string, { tasks: () => Promise<TaskCollectionRef> }>)["outside-run"].tasks();
await tasks.addTask({ goal: "work", assignee: "implement" });
return {};
}
});
expect((await testBlock(file, { input: undefined })).error).toBeNull();

const state = runtimeFor(waitFlow(stepModel([]).model));
await state.getRouter();
const started = Date.now();
await state.dispose();
expect(Date.now() - started).toBeLessThan(2_000);
});
});
16 changes: 6 additions & 10 deletions packages/orchestration/test/tasks/notice-defer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,15 @@ describe("a notice whose block fails (FIX-2123)", () => {
it("stays owed, taken by the request that failed, and the next copy takes it over and is heard once", async () => {
const h = host(() => false, true);
try {
// The request that filed the task sends its notice as it ends; that copy's block fails.
await h.act("seed", { id: "t1" });
const takenBy = async () =>
((await h.act("read", { id: "t1" })) as Record<string, { by?: string } | null>)["noticeTaken:1:completed"]?.by;
for (let i = 0; i < 200 && (await takenBy()) === undefined; i += 1) await new Promise((r) => setTimeout(r, 10));
expect(await takenBy()).toEqual(expect.any(String));
await expect(h.act("onTaskSettled", notice, true)).rejects.toThrow(/execution_error/);
expect(h.heard).toEqual([]);
const failed = (await h.act("read", { id: "t1" })) as Record<string, { by?: string } | null>;
expect(failed["noticeOwed:1:completed"]).toMatchObject({ attempt: 1, ending: "completed" });
expect(failed["noticeTaken:1:completed"]?.by).toEqual(expect.any(String));

// The filer's pass sends it again: once the request that took it has ended, a copy takes it over.
for (let i = 0; i < 200 && h.heard.length === 0; i += 1) {
await h.act("onTaskSettled", notice, true);
if (h.heard.length === 0) await new Promise((r) => setTimeout(r, 10));
}
// The filer's pass sends it again: the request that took it has ended, so this copy takes it over.
await h.act("onTaskSettled", notice, true);
expect(h.heard).toEqual([expect.objectContaining({ taskId: "t1", ending: "completed" })]);
const metadata = (await h.act("read", { id: "t1" })) as Record<string, unknown>;
expect(metadata["noticeOwed:1:completed"] ?? null).toBeNull();
Expand Down
Loading