Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions apps/server/src/orchestration-v2/EffectWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,30 @@ export const layerExecutor: Layer.Layer<
: { answers: effect.request.answers }),
})
.pipe(
// Dispatch resolves the request before delivery, so the last
// failed attempt records the failure where the user sees it
// instead of leaving the run to spin with no question.
Effect.tapCause((cause) =>
willRetry ||
Cause.hasInterruptsOnly(cause) ||
effect.request.type !== "runtime-request.respond"
? Effect.void
: threads
.dispatch({
type: "runtime-request.delivery.fail",
commandId: CommandId.make(`${effect.commandId}:delivery-failed`),
threadId: effect.threadId,
requestId: effect.request.requestId,
})
.pipe(
Effect.catchCause(() =>
Effect.logWarning(
"Failed to record undelivered runtime request answer",
{ effectId: effect.id },
),
),
),
),
Effect.mapError(
(cause) =>
new OrchestrationEffectExecutionError({
Expand Down
74 changes: 73 additions & 1 deletion apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
RunId,
ThreadLinkedPullRequest,
ThreadId,
type TurnItemId,
TurnItemId,
} from "@t3tools/contracts";
import { modelSelectionsEqual } from "@t3tools/shared/model";
import {
Expand Down Expand Up @@ -424,6 +424,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
case "queued-run.cancel":
case "queued-run.edit":
case "runtime-request.respond":
case "runtime-request.delivery.fail":
case "thread.user-input.dismiss":
case "checkpoint.rollback":
case "checkpoint.rollback.fail":
Expand Down Expand Up @@ -461,6 +462,8 @@ function pendingThreadTitleGenerationEffect(
}

const WORKSPACE_PREPARATION_INPUT = "Preparing workspace";
const RUNTIME_REQUEST_DELIVERY_FAILED_MESSAGE =
"T3 Code could not confirm that your answer reached the agent. If the run stays stuck, stop it and try again.";

/** A reopened preparation item drops the output and exit code of the attempt it replaces. */
function withoutPreparationResult(
Expand Down Expand Up @@ -7211,6 +7214,72 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
]);
});

/**
* Records an answer whose delivery to the provider failed. Dispatch resolved the
* request before delivery, and a failed delivery does not show whether the
* provider is still waiting, so the answer stays saved and the timeline shows
* the failure instead of a run that spins with nothing to answer.
*/
const dispatchRuntimeRequestDeliveryFail = (
command: Extract<
OrchestrationV2InternalCommand,
{ readonly type: "runtime-request.delivery.fail" }
>,
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
) =>
Effect.gen(function* () {
const context = yield* projectionStore
.getRuntimeResponseContext(command.threadId, command.requestId)
.pipe(mapDispatchError(command));
const requestNode = context.node;
if (context.request === undefined || requestNode === undefined) return;
// The session may already be detached; the failure is recorded either way.
const providerSession = context.session;
const thread = yield* projectionStore
.getThread(command.threadId)
.pipe(mapDispatchError(command));
const now = yield* DateTime.now;
yield* emit(
events,
command,
)({
type: "turn-item.updated",
threadId: command.threadId,
...(requestNode.runId === null ? {} : { runId: requestNode.runId }),
nodeId: requestNode.id,
...(providerSession === undefined
? {}
: {
driver: providerSession.driver,
providerInstanceId: providerSession.providerInstanceId,
}),
occurredAt: now,
payload: {
id: TurnItemId.make(`turn-item:runtime-request-delivery-failure:${command.commandId}`),
threadId: command.threadId,
runId: requestNode.runId,
nodeId: requestNode.id,
providerThreadId: requestNode.providerThreadId,
providerTurnId: requestNode.providerTurnId,
nativeItemRef: null,
parentItemId: null,
ordinal: yield* nextTurnItemOrdinal({ thread }),
status: "failed",
title: "Answer delivery not confirmed",
startedAt: now,
completedAt: now,
updatedAt: now,
type: "error",
failure: {
class: "transport_error",
message: RUNTIME_REQUEST_DELIVERY_FAILED_MESSAGE,
code: null,
retryable: null,
},
},
});
});

const dispatchThreadUserInputDismiss = (
command: Extract<OrchestrationV2Command, { readonly type: "thread.user-input.dismiss" }>,
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
Expand Down Expand Up @@ -10208,6 +10277,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
case "runtime-request.respond":
yield* dispatchRuntimeRequestRespond(command, events, effects);
break;
case "runtime-request.delivery.fail":
yield* dispatchRuntimeRequestDeliveryFail(command, events);
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
break;
case "thread.user-input.dismiss":
yield* dispatchThreadUserInputDismiss(command, events, effects);
break;
Expand Down
Loading
Loading