import { CommandId, type OrchestrationV2DomainEvent, type OrchestrationV2ExecutionNode, type OrchestrationV2ProviderThread, type OrchestrationV2Run, type OrchestrationV2RunAttempt, type OrchestrationV2TurnItem, RunId, ThreadId, } from "@t3tools/contracts"; import * as Context from "effect/Context"; import * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import { GitWorkflowService } from "../git/GitWorkflowService.ts"; import { ProjectService } from "../project/ProjectService.ts"; import { ProviderAuthService } from "../provider/Services/ProviderAuthService.ts"; import { EventSinkV2 } from "./EventSink.ts"; import { ContextHandoffServiceV2, providerMessageWithContextHandoffs, } from "./ContextHandoffService.ts"; import { IdAllocatorV2 } from "./IdAllocator.ts"; import { ProjectionStoreV2 } from "./ProjectionStore.ts"; import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts"; import { makeProviderFailure } from "./ProviderFailure.ts"; import { canRouteRelatedSubagent, RunExecutionServiceV2, selectInheritedBackgroundTurnItems, } from "./RunExecutionService.ts"; import { RuntimePolicyV2 } from "./RuntimePolicy.ts"; export class ProviderTurnStartError extends Schema.TaggedErrorClass()( "ProviderTurnStartError", { runId: RunId, cause: Schema.optional(Schema.Defect()), }, ) {} const isProviderTurnStartError = Schema.is(ProviderTurnStartError); export interface ProviderTurnStartServiceV2Shape { readonly start: (input: { readonly threadId: ThreadId; readonly runId: RunId; }) => Effect.Effect; } export class ProviderTurnStartServiceV2 extends Context.Service< ProviderTurnStartServiceV2, ProviderTurnStartServiceV2Shape >()("t3/orchestration-v2/ProviderTurnStartService/ProviderTurnStartServiceV2") {} export const layer: Layer.Layer< ProviderTurnStartServiceV2, never, | EventSinkV2 | ContextHandoffServiceV2 | IdAllocatorV2 | FileSystem.FileSystem | GitWorkflowService | ProjectService | ProviderAuthService | ProjectionStoreV2 | ProviderSessionManagerV2 | RunExecutionServiceV2 | RuntimePolicyV2 > = Layer.effect( ProviderTurnStartServiceV2, Effect.gen(function* () { const eventSink = yield* EventSinkV2; const contextHandoffService = yield* ContextHandoffServiceV2; const idAllocator = yield* IdAllocatorV2; const fileSystem = yield* FileSystem.FileSystem; const gitWorkflow = yield* GitWorkflowService; const projects = yield* ProjectService; const providerAuth = yield* ProviderAuthService; const projectionStore = yield* ProjectionStoreV2; const providerSessions = yield* ProviderSessionManagerV2; const runExecution = yield* RunExecutionServiceV2; const runtimePolicy = yield* RuntimePolicyV2; const start = Effect.fn("orchestrationV2.providerTurnStart.start")(function* (input: { readonly threadId: ThreadId; readonly runId: RunId; }) { const { runId } = input; const projection = yield* projectionStore.getThreadProjection(input.threadId); const run = projection.runs.find((candidate) => candidate.id === runId); if (run === undefined) { return yield* new ProviderTurnStartError({ runId, cause: `Run ${runId} was not found.` }); } if (run.status !== "starting") { // The effect is idempotent once the run has advanced or terminalized. return; } const rootNode = projection.nodes.find((candidate) => candidate.id === run.rootNodeId); const attempt = projection.attempts.find((candidate) => candidate.id === run.activeAttemptId); const providerThread = projection.providerThreads.find( (candidate) => candidate.id === run.providerThreadId, ); const message = projection.messages.find((candidate) => candidate.id === run.userMessageId); const checkpointScope = projection.checkpointScopes.find( (candidate) => candidate.id === rootNode?.checkpointScopeId, ); const handoffs = projection.contextHandoffs.filter( (handoff) => handoff.targetRunId === run.id && handoff.status === "ready", ); const nativeForkTransfer = projection.contextTransfers.find( (transfer) => transfer.type === "fork" && transfer.targetThreadId === input.threadId && transfer.targetRunId === run.id && transfer.status === "pending" && transfer.resolution === null, ); const existingResumeFallback = projection.contextTransfers.find( (transfer) => transfer.type === "provider_handoff" && transfer.sourceThreadId === projection.thread.id && transfer.targetThreadId === projection.thread.id && transfer.targetRunId === run.id && transfer.status === "resolved_portable" && transfer.resolution?.strategy === "portable_context", ); if ( rootNode === undefined || attempt === undefined || providerThread === undefined || providerThread.providerSessionId === null || message === undefined || checkpointScope === undefined ) { return yield* new ProviderTurnStartError({ runId, cause: `Run ${runId} is missing its execution projection state.`, }); } if (message.attachments.length === 0 && message.text.trimStart().startsWith("/")) { const isEmptyCompaction = message.text.trim().toLowerCase() === "/compact" && !projection.messages.some( (candidate) => candidate.role === "user" && (candidate.text.trim().toLowerCase() !== "/compact" || candidate.attachments.length > 0), ); // Preparing a run may already point the thread at a newly selected // provider. Account commands still belong to its last native session. const nativeThreads = new Map( projection.providerThreads .filter( (candidate) => candidate.ownerNodeId === null && candidate.nativeThreadRef !== null, ) .map((candidate) => [candidate.id, candidate]), ); const previousNativeRun = projection.runs.reduce( (previous, candidate) => candidate.ordinal < run.ordinal && candidate.providerThreadId !== null && nativeThreads.has(candidate.providerThreadId) && (previous === undefined || candidate.ordinal > previous.ordinal) ? candidate : previous, undefined, ); const nativeThread = nativeThreads.get( previousNativeRun?.providerThreadId ?? projection.thread.activeProviderThreadId ?? providerThread.id, ); const authInstanceId = nativeThread?.providerInstanceId ?? run.providerInstanceId; const authResult = isEmptyCompaction ? null : yield* Effect.result( providerAuth.tryHandlePromptCommand({ instanceId: authInstanceId, text: message.text, hasAttachments: false, }), ); if (isEmptyCompaction || authResult?._tag === "Failure" || authResult?.success) { const now = yield* DateTime.now; const failure = isEmptyCompaction ? makeProviderFailure({ class: "validation_error", message: "Start a conversation before compacting this thread.", }) : authResult?._tag === "Failure" ? makeProviderFailure({ class: "permission_error", message: authResult.failure.detail, }) : undefined; const status = failure === undefined ? "completed" : "failed"; const itemBase = { id: idAllocator.derive.runSignalTurnItem({ runId, signal: isEmptyCompaction ? "empty-compaction" : "provider-sign-out", }), threadId: projection.thread.id, runId, nodeId: rootNode.id, providerThreadId: nativeThread?.id ?? providerThread.id, providerTurnId: null, nativeItemRef: null, parentItemId: null, ordinal: Math.max( 0, ...projection.turnItems .filter((item) => item.runId === runId) .map((item) => item.ordinal), ) + 1, status, startedAt: now, completedAt: now, updatedAt: now, } as const; const item: OrchestrationV2TurnItem = failure !== undefined ? { ...itemBase, type: "error", title: isEmptyCompaction ? "Cannot compact an empty thread" : "Provider sign-out failed", failure, } : { ...itemBase, type: "command_execution", title: "Provider signed out", input: message.text.trim(), output: "Provider signed out", exitCode: 0, }; const eventPayloads = [ { type: "turn-item.updated", payload: item }, { type: "run.updated", payload: { ...run, status, startedAt: now, completedAt: now } }, { type: "run-attempt.updated", payload: { ...attempt, status, startedAt: now, completedAt: now }, }, { type: "node.updated", payload: { ...rootNode, status, startedAt: now, completedAt: now }, }, { type: "provider-thread.updated", payload: { ...providerThread, status: providerThread.nativeThreadRef === null ? "not_loaded" : "idle", updatedAt: now, }, }, ] as const; const events = yield* Effect.forEach(eventPayloads, (event) => Effect.gen(function* () { return { ...event, id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), threadId: projection.thread.id, runId, nodeId: rootNode.id, providerInstanceId: authInstanceId, occurredAt: now, } satisfies OrchestrationV2DomainEvent; }), ); yield* eventSink.writeIfRunCurrent({ threadId: projection.thread.id, runId, activeAttemptId: attempt.id, expectedStatus: "starting", events, }); return; } } const { worktreePath, branch } = projection.thread; if (worktreePath !== null && branch !== null) { const exists = yield* fileSystem .exists(worktreePath) .pipe(Effect.orElseSucceed(() => true)); if (!exists) { const project = yield* projects.getById(projection.thread.projectId).pipe( Effect.map(Option.getOrUndefined), Effect.orElseSucceed(() => undefined), ); if (project !== undefined) { yield* Effect.logWarning("provider turn start recreating missing worktree", { threadId: projection.thread.id, worktreePath, branch, }); yield* gitWorkflow.pruneWorktrees({ cwd: project.workspaceRoot }).pipe( Effect.andThen( gitWorkflow.createWorktree({ cwd: project.workspaceRoot, refName: branch, path: worktreePath, }), ), Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause) ? Effect.failCause(cause) : Effect.logWarning("provider turn start failed to recreate worktree", { threadId: projection.thread.id, worktreePath, cause: Cause.pretty(cause), }), ), ); } } } const selectInheritedBackgroundItems = ( current: typeof projection, ): ReturnType => selectInheritedBackgroundTurnItems({ threadId: current.thread.id, currentProviderThreadId: providerThread.id, currentRunOrdinal: run.ordinal, runs: current.runs, turnItems: current.turnItems, }); const inheritedBackgroundTurnItems = yield* projectionStore .getThreadProjection(projection.thread.id) .pipe(Effect.map(selectInheritedBackgroundItems)); const providerSessionId = providerThread.providerSessionId; const isCurrentAttemptInStatus = ( expectedStatus: OrchestrationV2Run["status"], ): Effect.Effect => projectionStore.getThreadProjection(projection.thread.id).pipe( Effect.map((current) => { const currentRun = current.runs.find((candidate) => candidate.id === run.id); return ( currentRun?.activeAttemptId === attempt.id && currentRun.status === expectedStatus ); }), Effect.catchCause(() => Effect.succeed(false)), ); const resolvedRuntimePolicy = yield* runtimePolicy.resolve({ thread: projection.thread, modelSelection: run.modelSelection, }); const existingSessionProjection = projection.providerSessions.find( (candidate) => candidate.id === providerSessionId, ); const session = yield* providerSessions.open({ threadId: projection.thread.id, providerSessionId, modelSelection: run.modelSelection, runtimePolicy: resolvedRuntimePolicy, ...(existingSessionProjection === undefined ? {} : { resumeFromSession: existingSessionProjection }), }); let effectiveHandoffs = handoffs; const loadedProviderThread = yield* Effect.gen(function* () { if (nativeForkTransfer !== undefined) { const sourceProjection = yield* projectionStore.getThreadProjection( nativeForkTransfer.sourceThreadId, ); const sourceRun = sourceProjection.runs.find( (candidate) => candidate.id === nativeForkTransfer.sourcePoint.runId, ); const sourceProviderThread = sourceProjection.providerThreads.find( (candidate) => candidate.id === sourceRun?.providerThreadId, ); const sourceAttempt = sourceProjection.attempts.find( (candidate) => candidate.id === sourceRun?.activeAttemptId, ); const sourceProviderTurn = sourceProjection.providerTurns.find( (candidate) => candidate.id === sourceAttempt?.providerTurnId || candidate.runAttemptId === sourceAttempt?.id, ); if (sourceRun === undefined || sourceProviderThread === undefined) { return yield* new ProviderTurnStartError({ runId, cause: `Native fork transfer ${nativeForkTransfer.id} has no source provider execution.`, }); } return yield* session.forkThread({ sourceProviderThread, sourceProviderTurns: sourceProjection.providerTurns, targetThreadId: projection.thread.id, modelSelection: run.modelSelection, runtimePolicy: resolvedRuntimePolicy, ...(sourceProviderTurn === undefined ? {} : { providerTurnId: sourceProviderTurn.id }), }); } if (providerThread.nativeThreadRef === null) { return yield* session.ensureThread({ threadId: projection.thread.id, modelSelection: run.modelSelection, runtimePolicy: resolvedRuntimePolicy, providerSessionId, }); } const resumed = yield* Effect.result( session.resumeThread({ providerThread, threadId: projection.thread.id, modelSelection: run.modelSelection, runtimePolicy: resolvedRuntimePolicy, }), ); if (resumed._tag === "Success") { return resumed.success; } const replacement = yield* session.ensureThread({ threadId: projection.thread.id, modelSelection: run.modelSelection, runtimePolicy: resolvedRuntimePolicy, providerSessionId, }); if (existingResumeFallback !== undefined) { return replacement; } const transferId = yield* idAllocator.allocate.contextTransfer({ sourceThreadId: projection.thread.id, targetThreadId: projection.thread.id, type: "provider_resume_fallback", }); const createdAt = yield* DateTime.now; const handoff = yield* contextHandoffService.prepareProviderHandoff({ threadId: projection.thread.id, targetRunId: run.id, transferId, fromProviderThreadIds: [providerThread.id], toProviderThreadId: providerThread.id, fromProviderInstanceId: providerThread.providerInstanceId, toProviderInstanceId: run.providerInstanceId, coveredRunOrdinals: { from: 1, to: Math.max(1, run.ordinal - 1) }, strategy: "full_thread_summary", items: projection.turnItems, createdAt, }); effectiveHandoffs = [...handoffs, handoff]; yield* eventSink.write({ events: [ { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "context-handoff.updated", threadId: projection.thread.id, runId: run.id, providerInstanceId: run.providerInstanceId, occurredAt: createdAt, payload: handoff, }, { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "context-transfer.updated", threadId: projection.thread.id, runId: run.id, providerInstanceId: run.providerInstanceId, occurredAt: createdAt, payload: { id: transferId, type: "provider_handoff", sourceThreadId: projection.thread.id, targetThreadId: projection.thread.id, sourcePoint: { threadId: projection.thread.id }, basePoint: null, sourceProviderInstanceId: providerThread.providerInstanceId, targetProviderInstanceId: run.providerInstanceId, targetRunId: run.id, status: "resolved_portable", resolution: { strategy: "portable_context", contextHandoffId: handoff.id }, createdBy: "system", error: null, createdAt, updatedAt: createdAt, consumedAt: null, }, }, ], }); return replacement; }); if (!(yield* isCurrentAttemptInStatus("starting"))) { return; } const now = yield* DateTime.now; const runningProviderThread: OrchestrationV2ProviderThread = { ...loadedProviderThread, id: providerThread.id, driver: session.driver, providerInstanceId: run.providerInstanceId, providerSessionId, appThreadId: projection.thread.id, ownerNodeId: providerThread.ownerNodeId, firstRunOrdinal: providerThread.firstRunOrdinal ?? run.ordinal, lastRunOrdinal: run.ordinal, handoffIds: providerThread.handoffIds, forkedFrom: providerThread.forkedFrom, status: "active", createdAt: providerThread.createdAt, updatedAt: now, }; const runningRun: OrchestrationV2Run = { ...run, status: "running", startedAt: now, }; const runningAttempt: OrchestrationV2RunAttempt = { ...attempt, status: "running", startedAt: now, }; const runningRootNode: OrchestrationV2ExecutionNode = { ...rootNode, status: "running", startedAt: now, }; const events: Array = [ { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id, providerSessionId, }), type: "provider-session.updated", threadId: projection.thread.id, driver: session.driver, providerInstanceId: run.providerInstanceId, occurredAt: now, payload: session.providerSession, }, { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "provider-thread.updated", threadId: projection.thread.id, driver: session.driver, providerInstanceId: run.providerInstanceId, occurredAt: now, payload: runningProviderThread, }, ...(nativeForkTransfer === undefined || runningProviderThread.nativeThreadRef === null ? [] : [ { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "context-transfer.updated" as const, threadId: projection.thread.id, runId: run.id, driver: session.driver, providerInstanceId: run.providerInstanceId, occurredAt: now, payload: { ...nativeForkTransfer, targetProviderInstanceId: run.providerInstanceId, targetRunId: run.id, status: "consumed" as const, resolution: { strategy: "native_fork" as const, providerThreadRef: runningProviderThread.nativeThreadRef, }, error: null, updatedAt: now, consumedAt: now, }, }, ]), { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "run.updated", threadId: projection.thread.id, runId: run.id, nodeId: rootNode.id, providerInstanceId: run.providerInstanceId, occurredAt: now, payload: runningRun, }, { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "run-attempt.updated", threadId: projection.thread.id, runId: run.id, nodeId: rootNode.id, providerInstanceId: run.providerInstanceId, occurredAt: now, payload: runningAttempt, }, { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "node.updated", threadId: projection.thread.id, runId: run.id, nodeId: rootNode.id, providerInstanceId: run.providerInstanceId, occurredAt: now, payload: runningRootNode, }, ]; const runningWrite = yield* eventSink.writeIfRunCurrent({ threadId: projection.thread.id, runId: run.id, activeAttemptId: attempt.id, expectedStatus: "starting", events, }); if (!runningWrite.committed) { return; } const routableSubagents = projection.subagents.filter((subagent) => canRouteRelatedSubagent(subagent.status), ); yield* runExecution.startRootRun({ commandId: CommandId.make(`command:effect:provider-turn.start:${run.id}`), appThread: projection.thread, providerSessionId, session, run: runningRun, rootNode: runningRootNode, checkpointScope, providerThread: runningProviderThread, attempt: runningAttempt, attemptId: attempt.id, loadInheritedBackgroundTurnItems: () => projectionStore.getThreadProjection(projection.thread.id).pipe( Effect.map(selectInheritedBackgroundItems), Effect.catchCause(() => Effect.succeed(inheritedBackgroundTurnItems)), ), relatedThreadIds: routableSubagents.flatMap((subagent) => subagent.childThreadId === null ? [] : [subagent.childThreadId], ), relatedProviderThreadIds: routableSubagents.flatMap((subagent) => subagent.providerThreadId === null ? [] : [subagent.providerThreadId], ), providerTurnOrdinal: Math.max( 0, ...projection.providerTurns .filter((turn) => turn.providerThreadId === providerThread.id) .map((turn) => turn.ordinal), ) + 1, shouldStartProviderTurn: () => isCurrentAttemptInStatus("running"), shouldFinalizeRun: () => projectionStore.getThreadProjection(projection.thread.id).pipe( Effect.map((current) => { const currentRun = current.runs.find((candidate) => candidate.id === run.id); return ( currentRun?.activeAttemptId === attempt.id && (currentRun.status === "starting" || currentRun.status === "running") ); }), Effect.catchCause(() => Effect.succeed(false)), ), hasUnpairedRunInterruptRequest: () => projectionStore.getThreadProjection(projection.thread.id).pipe( Effect.map((current) => { const requestId = idAllocator.derive.runSignalTurnItem({ runId: run.id, signal: "interrupt-request", }); const resultId = idAllocator.derive.runSignalTurnItem({ runId: run.id, signal: "interrupt-result", }); const hasRequest = current.turnItems.some((item) => item.id === requestId); const hasResult = current.turnItems.some((item) => item.id === resultId); return hasRequest && !hasResult; }), Effect.catchCause(() => Effect.succeed(false)), ), message: { messageId: message.id, text: effectiveHandoffs.length === 0 ? message.text : providerMessageWithContextHandoffs({ handoffs: effectiveHandoffs, userText: message.text, }), attachments: message.attachments, createdBy: message.createdBy, creationSource: message.creationSource, }, modelSelection: run.modelSelection, runtimePolicy: resolvedRuntimePolicy, }); }); return ProviderTurnStartServiceV2.of({ start: (input) => start(input).pipe( Effect.mapError((cause) => isProviderTurnStartError(cause) ? cause : new ProviderTurnStartError({ runId: input.runId, cause }), ), ), }); }), );