Files
t3code/apps/server/src/orchestration-v2/ProviderContinuationService.test.ts
T
Mike Olsonandcodex 686806f72a fix(orchestrator): Wake settled parents when delegated children finish
An app-owned delegated child wrote its result into the parent projection but
never started a parent run, so an async delegation's result sat unread until the
user's next message. Offer a provider continuation request when a delegated
child terminalizes, with a per-task completionWake policy separating async
delegations from blocking waits.

The wake must also reach the model. ProviderContinuationRequests is a
provider-native mechanism whose dispatch only triggers ingestion of output the
adapter already buffered, and both ClaudeAdapterV2 and AcpAdapterV2 discard the
message text when it is marked creationSource provider. An app-owned child
buffers nothing, so a wake marked that way settled instantly having prompted
nothing. App-owned wakes now dispatch as message_text via a shared
delegatedTaskWakeRequest helper used by both producers.

Co-authored-by: codex <[email protected]>
2026-08-14 11:23:27 +02:00

278 lines
10 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest";
import {
ProviderDriverKind,
ProviderThreadId,
ThreadId,
type OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
import * as Deferred from "effect/Deferred";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Semaphore from "effect/Semaphore";
import { layer as idAllocatorLayer } from "./IdAllocator.ts";
import {
type ProviderContinuationRequest,
ProviderContinuationRequests,
layer as continuationRequestsLayer,
} from "./ProviderContinuationRequests.ts";
import { workerLive } from "./ProviderContinuationService.ts";
import { ThreadManagementService } from "./ThreadManagementService.ts";
const threadId = ThreadId.make("thread-provider-continuation");
const providerThreadId = ProviderThreadId.make("provider-thread-continuation");
const driver = ProviderDriverKind.make("continuation-test");
const projection = {
thread: { archivedAt: null },
messages: [],
} as unknown as OrchestrationV2ThreadProjection;
const request = (
dispatchIfCurrent?: ProviderContinuationRequest["dispatchIfCurrent"],
detail: string | null = null,
): ProviderContinuationRequest => ({
threadId,
providerThreadId,
driver,
detail,
...(dispatchIfCurrent === undefined ? {} : { dispatchIfCurrent }),
});
const makeGuard = Effect.fnUntraced(function* (completed?: Deferred.Deferred<void>) {
const generation = yield* Ref.make(0);
const permit = yield* Semaphore.make(1);
const capture = Effect.gen(function* () {
const captured = yield* Ref.updateAndGet(generation, (value) => value + 1);
return <A, E, R>(effect: Effect.Effect<A, E, R>) =>
permit
.withPermit(
Effect.gen(function* () {
if ((yield* Ref.get(generation)) !== captured) return Option.none();
return Option.some(yield* effect);
}),
)
.pipe(
completed === undefined
? (effect) => effect
: Effect.ensuring(Deferred.succeed(completed, undefined)),
);
});
return {
capture,
invalidate: permit.withPermit(Ref.update(generation, (value) => value + 1)),
};
});
function testLayer(input: {
readonly dispatched: Queue.Queue<unknown>;
readonly getThreadProjection: () => Effect.Effect<OrchestrationV2ThreadProjection>;
}) {
const threads = Layer.mock(ThreadManagementService)({
getThreadProjection: input.getThreadProjection,
dispatch: (command) => Queue.offer(input.dispatched, command).pipe(Effect.as({} as never)),
});
const worker = workerLive.pipe(
Layer.provide(Layer.mergeAll(idAllocatorLayer, continuationRequestsLayer, threads)),
);
return Layer.merge(continuationRequestsLayer, worker);
}
describe("ProviderContinuationService", () => {
it.effect("marks an adapter-buffered wake with the provider creation source", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
yield* requests.offer(request());
const command = (yield* Queue.take(dispatched)) as { readonly creationSource: string };
// ClaudeAdapterV2 keys on this to attach buffered CLI output.
assert.equal(command.creationSource, "provider");
}).pipe(
Effect.provide(
testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
),
Effect.scoped,
);
});
});
it.effect("delivers a message_text wake as a real prompt, not a buffered wake", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
yield* requests.offer({
threadId,
providerThreadId,
driver,
detail: "Delegated task completed.",
delivery: "message_text",
});
const command = (yield* Queue.take(dispatched)) as {
readonly creationSource: string;
readonly text: string;
};
// An app-owned child buffers nothing in the adapter, so this text is
// the whole wake. Marking it "provider" would make ClaudeAdapterV2
// drop it and settle the turn having prompted nothing.
assert.notEqual(command.creationSource, "provider");
assert.equal(command.creationSource, "server");
assert.equal(command.text, "Delegated task completed.");
}).pipe(
Effect.provide(
testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
),
Effect.scoped,
);
});
});
it.effect("dispatches a current request exactly once", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
yield* requests.offer(request());
yield* Queue.take(dispatched);
yield* Effect.yieldNow;
assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
}).pipe(
Effect.provide(
testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
),
Effect.scoped,
);
});
});
it.effect("drops a request invalidated before dispatch", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
const guard = yield* makeGuard();
const dispatchIfCurrent = yield* guard.capture;
yield* guard.invalidate;
yield* requests.offer(request(dispatchIfCurrent));
yield* Effect.yieldNow;
yield* Effect.yieldNow;
assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
}).pipe(
Effect.provide(
testLayer({ dispatched, getThreadProjection: () => Effect.succeed(projection) }),
),
Effect.scoped,
);
});
});
it.effect("drops a request invalidated while projection is blocked", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
const projectionEntered = yield* Deferred.make<void>();
const releaseProjection = yield* Deferred.make<void>();
const guardCompleted = yield* Deferred.make<void>();
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
const guard = yield* makeGuard(guardCompleted);
const dispatchIfCurrent = yield* guard.capture;
yield* requests.offer(request(dispatchIfCurrent));
yield* Deferred.await(projectionEntered);
yield* guard.invalidate;
yield* Deferred.succeed(releaseProjection, undefined);
yield* Deferred.await(guardCompleted);
assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
}).pipe(
Effect.provide(
testLayer({
dispatched,
getThreadProjection: () =>
Deferred.succeed(projectionEntered, undefined).pipe(
Effect.andThen(Deferred.await(releaseProjection)),
Effect.as(projection),
),
}),
),
Effect.scoped,
);
});
});
it.effect("does not revive an old request when a later generation is current", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
const firstProjectionEntered = yield* Deferred.make<void>();
const releaseFirstProjection = yield* Deferred.make<void>();
let projectionCalls = 0;
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
const guard = yield* makeGuard();
const first = yield* guard.capture;
yield* requests.offer(request(first, "A"));
yield* Deferred.await(firstProjectionEntered);
const second = yield* guard.capture;
yield* requests.offer(request(second, "B"));
yield* Deferred.succeed(releaseFirstProjection, undefined);
const command = yield* Queue.take(dispatched);
assert.equal((command as { readonly text?: unknown }).text, "B");
yield* Effect.yieldNow;
assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
}).pipe(
Effect.provide(
testLayer({
dispatched,
getThreadProjection: () => {
projectionCalls += 1;
return projectionCalls === 1
? Deferred.succeed(firstProjectionEntered, undefined).pipe(
Effect.andThen(Deferred.await(releaseFirstProjection)),
Effect.as(projection),
)
: Effect.succeed(projection);
},
}),
),
Effect.scoped,
);
});
});
it.effect("clears a sticky continuation offer when its thread is archived", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
const cleared = yield* Ref.make(false);
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
yield* requests.offer({
...request(),
clearIfCurrent: () => Ref.set(cleared, true),
});
for (let attempt = 0; attempt < 20 && !(yield* Ref.get(cleared)); attempt += 1) {
yield* Effect.yieldNow;
}
assert.isTrue(yield* Ref.get(cleared));
assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
}).pipe(
Effect.provide(
testLayer({
dispatched,
getThreadProjection: () =>
Effect.succeed({
...projection,
thread: {
...projection.thread,
archivedAt: DateTime.makeUnsafe("2026-07-20T00:00:00.000Z"),
},
}),
}),
),
Effect.scoped,
);
});
});
});