Files
t3code/apps/server/src/orchestration-v2/ProviderContinuationService.ts
T
Mike Olsonandcodex 4fc73fd290 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-15 16:50:12 +02:00

88 lines
3.4 KiB
TypeScript

import { CommandId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { IdAllocatorV2 } from "./IdAllocator.ts";
import {
type ProviderContinuationRequest,
ProviderContinuationRequests,
} from "./ProviderContinuationRequests.ts";
import { ThreadManagementService } from "./ThreadManagementService.ts";
const CONTINUATION_MESSAGE_TEXT = "Background task completed.";
/**
* Drains ProviderContinuationRequests and dispatches an internal
* message.dispatch per request so the wake turn buffered by the adapter is
* ingested as a normal run. Dispatches queue_after_active, so a continuation
* racing a user run simply queues behind it and drains the wake buffer once
* that run finishes.
*/
export const workerLive = Layer.effectDiscard(
Effect.gen(function* () {
const ids = yield* IdAllocatorV2;
const requests = yield* ProviderContinuationRequests;
const threads = yield* ThreadManagementService;
const dispatchContinuation = Effect.fn("ProviderContinuationService.dispatchContinuation")(
function* (request: ProviderContinuationRequest) {
const projection = yield* threads.getThreadProjection(request.threadId);
if (projection.thread.archivedAt !== null) {
yield* Effect.logInfo("orchestration-v2.provider-continuation.thread-archived", {
threadId: request.threadId,
providerThreadId: request.providerThreadId,
});
// Still run the guard so adapters can clear sticky continuationRequested.
if (request.dispatchIfCurrent !== undefined) {
yield* request.dispatchIfCurrent(Effect.void);
}
return;
}
// The ordinal is display metadata only: allocate.message appends a
// random UUID, so a stale projection read here cannot collide ids.
const messageId = yield* ids.allocate.message({
threadId: request.threadId,
ordinal: projection.messages.length + 1,
});
const commandId = CommandId.make(`provider-continuation:${messageId}`);
const dispatch = threads.dispatch({
type: "message.dispatch",
commandId,
threadId: request.threadId,
messageId,
text: request.detail ?? CONTINUATION_MESSAGE_TEXT,
attachments: [],
dispatchMode: { type: "queue_after_active" },
createdBy: "agent",
// "provider" marks an adapter-buffered wake, which ClaudeAdapterV2
// detects to attach the buffered CLI output and drop this text. A
// message_text wake has no buffered output, so it must not carry that
// marker or the turn settles immediately having prompted nothing.
creationSource: request.delivery === "message_text" ? "server" : "provider",
});
if (request.dispatchIfCurrent === undefined) {
yield* dispatch;
return;
}
yield* request.dispatchIfCurrent(dispatch);
},
);
yield* requests.take.pipe(
Effect.flatMap((request) =>
dispatchContinuation(request).pipe(
Effect.catchCause((cause) =>
Effect.logWarning("orchestration-v2.provider-continuation.dispatch-failed", {
threadId: request.threadId,
providerThreadId: request.providerThreadId,
cause,
}),
),
),
),
Effect.forever,
Effect.forkScoped,
);
}),
);