Skip to content

Commit 7379933

Browse files
feat: retry a failed workspace preparation (#15326)
1 parent 1826fb5 commit 7379933

13 files changed

Lines changed: 752 additions & 70 deletions

File tree

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@ import {
4141
View,
4242
} from "react-native";
4343
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
44-
import type { EnvironmentId, ToolActivityIcon } from "@t3tools/contracts";
44+
import {
45+
ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
46+
type EnvironmentId,
47+
type RunId,
48+
type ThreadId,
49+
type ToolActivityIcon,
50+
} from "@t3tools/contracts";
51+
import { useAtomValue } from "@effect/atom-react";
52+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
53+
import { environmentThreadDetails, threadEnvironment } from "../../state/threads";
54+
import { useAtomCommand } from "../../state/use-atom-command";
4555
import { toolActivityFaviconUrl } from "@t3tools/shared/favicon";
4656

4757
import { AppText as Text } from "../../components/AppText";
@@ -770,6 +780,44 @@ function workLogRowKey(row: ThreadFeedActivity): string {
770780
return row.id;
771781
}
772782

783+
/** Shown while the thread's run still ends in this failed preparation. */
784+
function WorkspacePreparationRetryButton(props: {
785+
readonly environmentId: EnvironmentId;
786+
readonly threadId: ThreadId;
787+
readonly runId: RunId;
788+
}) {
789+
const retryable = useAtomValue(
790+
environmentThreadDetails.threadAtom(scopeThreadRef(props.environmentId, props.threadId)),
791+
(thread) => {
792+
const run = thread?.projection.runs.find((candidate) => candidate.id === props.runId);
793+
return run?.status === "failed" && run.workspacePreparation !== undefined;
794+
},
795+
);
796+
const retry = useAtomCommand(threadEnvironment.retryWorkspacePreparation, "retry setup");
797+
const [busy, setBusy] = useState(false);
798+
if (!retryable) return null;
799+
return (
800+
<Pressable
801+
accessibilityRole="button"
802+
accessibilityLabel="Retry workspace preparation"
803+
disabled={busy}
804+
onPress={() => {
805+
setBusy(true);
806+
void Haptics.selectionAsync();
807+
void retry({
808+
environmentId: props.environmentId,
809+
input: { threadId: props.threadId, runId: props.runId },
810+
}).finally(() => setBusy(false));
811+
}}
812+
className="ml-7 mt-2 min-h-11 flex-row items-center gap-1.5 self-start rounded-full border border-border px-4"
813+
style={{ opacity: busy ? 0.5 : 1 }}
814+
>
815+
<SymbolView name="arrow.clockwise" size={13} tintColorClassName="accent-icon" />
816+
<Text className="font-t3-medium text-sm text-foreground">Retry</Text>
817+
</Pressable>
818+
);
819+
}
820+
773821
const ThreadWorkLogRow = memo(function ThreadWorkLogRow(
774822
props: Omit<
775823
ThreadWorkLogProps,
@@ -850,6 +898,14 @@ const ThreadWorkLogRow = memo(function ThreadWorkLogRow(
850898
{failureItem.failure.message}
851899
</Text>
852900
) : null}
901+
{failureItem.failure.code === ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE &&
902+
failureItem.runId !== null ? (
903+
<WorkspacePreparationRetryButton
904+
environmentId={props.environmentId}
905+
threadId={failureItem.threadId}
906+
runId={failureItem.runId}
907+
/>
908+
) : null}
853909
</View>
854910
</WorkLogPressable>
855911
);

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
legacyThreadPullRequestKey,
1313
} from "@t3tools/shared/threadPullRequests";
1414
import {
15+
ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
1516
type ChatAttachment,
1617
CommandId,
1718
isProviderNativeSubagentThread,
@@ -400,6 +401,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
400401
case "prepared-run.release":
401402
case "prepared-run.progress":
402403
case "prepared-run.fail":
404+
case "prepared-run.retry":
403405
case "run.interrupt":
404406
case "queued-message.promote-to-steer":
405407
case "queue.resume":
@@ -442,6 +444,14 @@ function pendingThreadTitleGenerationEffect(
442444

443445
const WORKSPACE_PREPARATION_INPUT = "Preparing workspace";
444446

447+
/** A reopened preparation item drops the output and exit code of the attempt it replaces. */
448+
function withoutPreparationResult(
449+
item: Extract<OrchestrationV2TurnItem, { readonly type: "command_execution" }>,
450+
) {
451+
const { output: _output, exitCode: _exitCode, outputIndicatesFailure: _failure, ...rest } = item;
452+
return rest;
453+
}
454+
445455
function isBlockingRun(run: OrchestrationV2Run): boolean {
446456
return (
447457
run.status === "preparing" ||
@@ -5150,6 +5160,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
51505160
...(command.restartContinuationOfRunId === undefined
51515161
? {}
51525162
: { restartContinuationOfRunId: command.restartContinuationOfRunId }),
5163+
...(dispatchMode.type === "defer_start" && dispatchMode.workspaceStrategy !== undefined
5164+
? { workspacePreparation: dispatchMode.workspaceStrategy }
5165+
: {}),
51535166
...wakeWorkStartedAt(projection.runs, command),
51545167
};
51555168
const attempt: OrchestrationV2RunAttempt = {
@@ -7489,7 +7502,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
74897502
command: Extract<
74907503
OrchestrationV2Command,
74917504
{
7492-
readonly type: "prepared-run.release" | "prepared-run.progress" | "prepared-run.fail";
7505+
readonly type:
7506+
| "prepared-run.release"
7507+
| "prepared-run.progress"
7508+
| "prepared-run.fail"
7509+
| "prepared-run.retry";
74937510
}
74947511
>,
74957512
projection: Pick<
@@ -7512,7 +7529,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
75127529
candidate.input === WORKSPACE_PREPARATION_INPUT,
75137530
);
75147531
if (
7515-
run?.status !== "preparing" ||
7532+
run?.status !== (command.type === "prepared-run.retry" ? "failed" : "preparing") ||
75167533
attempt === undefined ||
75177534
rootNode === undefined ||
75187535
providerThread === undefined ||
@@ -7730,7 +7747,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
77307747
completedAt: now,
77317748
updatedAt: now,
77327749
type: "error",
7733-
failure: command.failure,
7750+
failure: {
7751+
...command.failure,
7752+
code: ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
7753+
},
77347754
},
77357755
});
77367756
yield* emitEvent({
@@ -7744,6 +7764,90 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
77447764
});
77457765
});
77467766

7767+
/**
7768+
* Returns a run whose workspace preparation failed to preparing. The failure
7769+
* item turns cancelled so clients stop offering the retry; ThreadLaunchService
7770+
* runs the recorded preparation again once this commits.
7771+
*/
7772+
const dispatchPreparedRunRetry = (
7773+
command: Extract<OrchestrationV2Command, { readonly type: "prepared-run.retry" }>,
7774+
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
7775+
) =>
7776+
Effect.gen(function* () {
7777+
const projection = yield* loadProjectionForCommand(
7778+
command,
7779+
["runs", "attempts", "nodes", "providerThreads", "turnItems"],
7780+
{ turnItemTypes: ["command_execution", "error"], turnItemRunId: command.runId },
7781+
);
7782+
const state = preparedRunState(command, projection);
7783+
const failureItem = projection.turnItems.find(
7784+
(candidate): candidate is Extract<OrchestrationV2TurnItem, { readonly type: "error" }> =>
7785+
candidate.type === "error" &&
7786+
candidate.status === "failed" &&
7787+
candidate.failure.code === ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
7788+
);
7789+
if (
7790+
state === null ||
7791+
state.run.workspacePreparation === undefined ||
7792+
failureItem === undefined ||
7793+
projection.thread.archivedAt !== null ||
7794+
projection.thread.deletedAt !== null
7795+
) {
7796+
return yield* new OrchestratorDispatchError({
7797+
commandId: command.commandId,
7798+
commandType: command.type,
7799+
cause: `Run ${command.runId} has no failed workspace preparation to retry.`,
7800+
});
7801+
}
7802+
if (projection.runs.some((run) => run.id !== state.run.id && isBlockingRun(run))) {
7803+
return yield* new OrchestratorDispatchError({
7804+
commandId: command.commandId,
7805+
commandType: command.type,
7806+
cause: "Another run is active on this thread.",
7807+
});
7808+
}
7809+
const now = yield* DateTime.now;
7810+
const emitEvent = emit(events, command);
7811+
const scope = {
7812+
threadId: command.threadId,
7813+
runId: state.run.id,
7814+
nodeId: state.rootNode.id,
7815+
providerInstanceId: state.run.providerInstanceId,
7816+
occurredAt: now,
7817+
};
7818+
yield* emitEvent({
7819+
...scope,
7820+
type: "turn-item.updated",
7821+
payload: { ...failureItem, status: "cancelled", updatedAt: now },
7822+
});
7823+
yield* emitEvent({
7824+
...scope,
7825+
type: "turn-item.updated",
7826+
payload: {
7827+
...withoutPreparationResult(state.preparationItem),
7828+
status: "running",
7829+
title: WORKSPACE_PREPARATION_INPUT,
7830+
completedAt: null,
7831+
updatedAt: now,
7832+
},
7833+
});
7834+
yield* emitEvent({
7835+
...scope,
7836+
type: "run-attempt.updated",
7837+
payload: { ...state.attempt, status: "pending", completedAt: null },
7838+
});
7839+
yield* emitEvent({
7840+
...scope,
7841+
type: "node.updated",
7842+
payload: { ...state.rootNode, status: "pending", completedAt: null },
7843+
});
7844+
yield* emitEvent({
7845+
...scope,
7846+
type: "run.updated",
7847+
payload: { ...state.run, status: "preparing", completedAt: null },
7848+
});
7849+
});
7850+
77477851
/**
77487852
* Ends the background work a settled thread still shows that no provider
77497853
* process will report on: work on the provider thread whose interrupt just
@@ -9450,6 +9554,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
94509554
case "prepared-run.fail":
94519555
yield* dispatchPreparedRunFail(command, events);
94529556
break;
9557+
case "prepared-run.retry":
9558+
yield* dispatchPreparedRunRetry(command, events);
9559+
break;
94539560
case "runtime-request.respond":
94549561
yield* dispatchRuntimeRequestRespond(command, events, effects);
94559562
break;

0 commit comments

Comments
 (0)