Files
t3code/apps/server/src/orchestration-v2/ProviderTurnControlService.ts
T
Julius Marminge 27897acd26 Map orchestration turns to provider instances
- Separate provider driver identity from configured provider instances
- Add durable event, effect, checkpoint, and turn-position persistence
- Route turn lifecycle and recovery through shared orchestration services
2026-08-03 11:45:02 -04:00

237 lines
8.7 KiB
TypeScript

import {
MessageId,
ProviderSessionId,
ProviderThreadId,
ProviderTurnId,
RunAttemptId,
ThreadId,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import { ProjectionStoreV2 } from "./ProjectionStore.ts";
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
const yieldToRuntime = Effect.yieldNow.pipe(
Effect.andThen(
Effect.promise(
() =>
new Promise<void>((resolve) => {
setImmediate(resolve);
}),
),
),
);
export class ProviderTurnControlError extends Schema.TaggedErrorClass<ProviderTurnControlError>()(
"ProviderTurnControlError",
{
threadId: ThreadId,
operation: Schema.Literals(["interrupt", "restart", "steer"]),
providerTurnId: ProviderTurnId,
cause: Schema.optional(Schema.Defect()),
},
) {}
export interface ProviderTurnControlServiceV2Shape {
readonly interrupt: (input: {
readonly threadId: ThreadId;
readonly providerSessionId: ProviderSessionId;
readonly providerThreadId: ProviderThreadId;
readonly providerTurnId: ProviderTurnId;
}) => Effect.Effect<void, ProviderTurnControlError>;
readonly steer: (input: {
readonly threadId: ThreadId;
readonly providerSessionId: ProviderSessionId;
readonly providerThreadId: ProviderThreadId;
readonly providerTurnId: ProviderTurnId;
readonly messageId: MessageId;
}) => Effect.Effect<void, ProviderTurnControlError>;
readonly interruptAndAwaitTerminal: (input: {
readonly threadId: ThreadId;
readonly providerSessionId: ProviderSessionId;
readonly providerThreadId: ProviderThreadId;
readonly providerTurnId: ProviderTurnId;
readonly interruptedAttemptId: RunAttemptId;
}) => Effect.Effect<void, ProviderTurnControlError>;
}
export class ProviderTurnControlServiceV2 extends Context.Service<
ProviderTurnControlServiceV2,
ProviderTurnControlServiceV2Shape
>()("t3/orchestration-v2/ProviderTurnControlService/ProviderTurnControlServiceV2") {}
export const layer: Layer.Layer<
ProviderTurnControlServiceV2,
never,
ProjectionStoreV2 | ProviderSessionManagerV2
> = Layer.effect(
ProviderTurnControlServiceV2,
Effect.gen(function* () {
const projections = yield* ProjectionStoreV2;
const sessions = yield* ProviderSessionManagerV2;
const load = (input: {
readonly threadId: ThreadId;
readonly providerSessionId: ProviderSessionId;
readonly providerThreadId: ProviderThreadId;
readonly providerTurnId: ProviderTurnId;
readonly operation: "interrupt" | "restart" | "steer";
}) =>
Effect.gen(function* () {
const projection = yield* projections.getThreadProjection(input.threadId);
const providerThread = projection.providerThreads.find(
(candidate) => candidate.id === input.providerThreadId,
);
const providerTurn = projection.providerTurns.find(
(candidate) => candidate.id === input.providerTurnId,
);
if (
providerThread === undefined ||
providerTurn === undefined ||
providerThread.providerSessionId !== input.providerSessionId ||
providerTurn.providerThreadId !== providerThread.id
) {
return yield* new ProviderTurnControlError({
threadId: input.threadId,
operation: input.operation,
providerTurnId: input.providerTurnId,
cause: "The recorded provider execution target is no longer valid.",
});
}
if (providerTurn.status !== "running") {
return { projection, providerThread, providerTurn, session: Option.none() };
}
const session = yield* sessions.get(input.providerSessionId);
if (Option.isNone(session)) {
return yield* new ProviderTurnControlError({
threadId: input.threadId,
operation: input.operation,
providerTurnId: input.providerTurnId,
cause: `Provider session ${input.providerSessionId} is not active.`,
});
}
return { projection, providerThread, providerTurn, session };
});
return ProviderTurnControlServiceV2.of({
interrupt: (input) =>
Effect.gen(function* () {
const loaded = yield* load({ ...input, operation: "interrupt" });
if (Option.isNone(loaded.session)) return;
yield* loaded.session.value.interruptTurn({
providerThread: loaded.providerThread,
providerTurnId: loaded.providerTurn.id,
});
}).pipe(
Effect.mapError((cause) =>
Schema.is(ProviderTurnControlError)(cause)
? cause
: new ProviderTurnControlError({
threadId: input.threadId,
operation: "interrupt",
providerTurnId: input.providerTurnId,
cause,
}),
),
),
interruptAndAwaitTerminal: (input) =>
Effect.gen(function* () {
const loaded = yield* load({ ...input, operation: "restart" });
if (Option.isSome(loaded.session)) {
yield* loaded.session.value.interruptTurn({
providerThread: loaded.providerThread,
providerTurnId: loaded.providerTurn.id,
});
}
for (let remaining = 1_000; remaining > 0; remaining -= 1) {
const projection = yield* projections.getThreadProjection(input.threadId);
const providerTurn = projection.providerTurns.find(
(candidate) => candidate.id === input.providerTurnId,
);
const attempt = projection.attempts.find(
(candidate) => candidate.id === input.interruptedAttemptId,
);
if (
providerTurn !== undefined &&
providerTurn.status !== "running" &&
attempt !== undefined &&
attempt.status !== "running"
) {
return;
}
// Provider terminal events are projected on a detached ingestion
// fiber. Yield through the Node event loop instead of sleeping on
// Effect's clock so deterministic runtimes cannot deadlock a
// command that is waiting for that projection.
yield* yieldToRuntime;
}
return yield* new ProviderTurnControlError({
threadId: input.threadId,
operation: "restart",
providerTurnId: input.providerTurnId,
cause: `Provider turn ${input.providerTurnId} did not terminalize before restart.`,
});
}).pipe(
Effect.mapError((cause) =>
Schema.is(ProviderTurnControlError)(cause)
? cause
: new ProviderTurnControlError({
threadId: input.threadId,
operation: "restart",
providerTurnId: input.providerTurnId,
cause,
}),
),
),
steer: (input) =>
Effect.gen(function* () {
const loaded = yield* load({ ...input, operation: "steer" });
if (Option.isNone(loaded.session)) return;
const message = loaded.projection.messages.find(
(candidate) => candidate.id === input.messageId,
);
const run = loaded.projection.runs.find(
(candidate) => candidate.activeAttemptId === loaded.providerTurn.runAttemptId,
);
if (message === undefined || run === undefined) {
return yield* new ProviderTurnControlError({
threadId: input.threadId,
operation: "steer",
providerTurnId: input.providerTurnId,
cause: "The persisted steering message or target run is missing.",
});
}
yield* loaded.session.value.steerTurn({
threadId: input.threadId,
runId: run.id,
providerThread: loaded.providerThread,
providerTurnId: loaded.providerTurn.id,
message: {
messageId: message.id,
text: message.text,
attachments: message.attachments,
createdBy: message.createdBy,
creationSource: message.creationSource,
},
});
}).pipe(
Effect.mapError((cause) =>
Schema.is(ProviderTurnControlError)(cause)
? cause
: new ProviderTurnControlError({
threadId: input.threadId,
operation: "steer",
providerTurnId: input.providerTurnId,
cause,
}),
),
),
});
}),
);