Skip to content

Commit c7e0fbf

Browse files
committed
fix: hold follow-ups when worktree setup fails and let mobile queue during setup
1 parent 2540f80 commit c7e0fbf

5 files changed

Lines changed: 97 additions & 10 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,15 @@ export interface ThreadDetailScreenProps {
169169
* The server has not created this thread yet. "preparing" runs while the
170170
* queued creation is delivered (a worktree may be checking out); "failed"
171171
* is a rejected creation whose content went back to the project draft.
172+
* `serverOwned` marks a thread the server already created that is still
173+
* setting up its worktree; follow-ups queue on the server behind that setup.
172174
*/
173175
readonly creationState:
174-
| { readonly kind: "preparing"; readonly preparingWorktree: boolean }
176+
| {
177+
readonly kind: "preparing";
178+
readonly preparingWorktree: boolean;
179+
readonly serverOwned: boolean;
180+
}
175181
| { readonly kind: "failed"; readonly reason: string; readonly onEditTask: () => void }
176182
| null;
177183
readonly activePendingApproval: PendingApproval | null;
@@ -1392,11 +1398,14 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
13921398
canStopThread={props.canStopThread}
13931399
environmentId={props.environmentId}
13941400
projectCwd={props.threadCwd ?? props.projectWorkspaceRoot}
1395-
// Follow-ups typed during setup wait in the draft: queueing
1396-
// them against a thread id the server may still reject
1397-
// would strand them in the outbox.
1401+
// Follow-ups typed before the server creates the thread wait in
1402+
// the draft: queueing them against a thread id the server may
1403+
// still reject would strand them in the outbox.
13981404
sendBlockedReason={
1399-
props.creationState?.kind === "preparing" ? "Starting the task…" : null
1405+
props.creationState?.kind === "preparing" &&
1406+
!props.creationState.serverOwned
1407+
? "Starting the task…"
1408+
: null
14001409
}
14011410
draftKey={props.composerDraftKey ?? undefined}
14021411
followUpBehavior={props.followUpBehavior}

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,9 @@ function ThreadRouteContent(
889889
]);
890890
const creationState = ((): ThreadDetailScreenProps["creationState"] => {
891891
if (selectedThreadCreation === null) {
892-
return awaitingBootstrapTurn ? { kind: "preparing", preparingWorktree: true } : null;
892+
return awaitingBootstrapTurn
893+
? { kind: "preparing", preparingWorktree: true, serverOwned: true }
894+
: null;
893895
}
894896
if (selectedThreadCreation.outcome?.kind === "failed") {
895897
return {
@@ -901,6 +903,7 @@ function ThreadRouteContent(
901903
return {
902904
kind: "preparing",
903905
preparingWorktree: selectedThreadCreation.message.creation?.workspaceMode === "worktree",
906+
serverOwned: false,
904907
};
905908
})();
906909
if (!environmentId || !threadId) {

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,15 +1298,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12981298
// Hold the queue so the user decides when to resume it. Validation
12991299
// failures (setup, unsupported handoff) belong to that message alone,
13001300
// and a message queued for another provider is how users recover.
1301+
// A message queued behind a worktree that was cancelled or never created
1302+
// would run in the project checkout instead, so it waits for the user.
13011303
const failedRun = latestExecutedRun(projection.runs);
13021304
const failureClass =
13031305
failedRun?.id === options?.failedRunId
13041306
? latestRootProviderFailure(failedRun, projection.turnItems)?.class
13051307
: undefined;
1308+
const worktreeMissing =
1309+
failedRun?.id === options?.failedRunId &&
1310+
failedRun?.workspacePreparation?.type === "worktree" &&
1311+
projection.thread.worktreePath === null;
13061312
if (
1307-
failureClass !== undefined &&
1308-
failureClass !== "validation_error" &&
1309-
failedRun?.providerInstanceId === queuedRun.providerInstanceId
1313+
worktreeMissing ||
1314+
(failureClass !== undefined &&
1315+
failureClass !== "validation_error" &&
1316+
failedRun?.providerInstanceId === queuedRun.providerInstanceId)
13101317
) {
13111318
const now = yield* DateTime.now;
13121319
yield* writeSystemEvents(

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,68 @@ it.effect("retries a failed workspace preparation on the same run", () => {
14221422
}).pipe(Effect.provide(harness.layer));
14231423
});
14241424

1425+
it.effect("holds a message queued during setup when the workspace preparation fails", () =>
1426+
Effect.gen(function* () {
1427+
const allowFetch = yield* Deferred.make<void>();
1428+
const harness = makeHarness({
1429+
fetchRemote: () =>
1430+
Deferred.await(allowFetch).pipe(
1431+
Effect.andThen(
1432+
Effect.fail(
1433+
new GitCommandError({
1434+
operation: "GitVcsDriver.fetchRemote",
1435+
command: "git",
1436+
cwd: project.workspaceRoot,
1437+
detail: "Git could not reach the remote.",
1438+
exitCode: 128,
1439+
}),
1440+
),
1441+
),
1442+
),
1443+
});
1444+
yield* Effect.gen(function* () {
1445+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1446+
const threads = yield* ThreadManagement.ThreadManagementService;
1447+
const launched = yield* launches.launch(
1448+
launchInput({
1449+
command: "command:launch:queued-failed-setup",
1450+
thread: "thread:launch:queued-failed-setup",
1451+
message: "First message",
1452+
workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
1453+
}),
1454+
);
1455+
const queued = yield* threads.sendToThread({
1456+
projectId,
1457+
commandId: CommandId.make("command:launch:queued-failed-setup:follow-up"),
1458+
threadId: launched.threadId,
1459+
messageId: MessageId.make("message:launch:queued-failed-setup:follow-up"),
1460+
text: "Sent during setup",
1461+
attachments: [],
1462+
mode: "queue",
1463+
createdBy: "user",
1464+
creationSource: "web",
1465+
});
1466+
assert.equal(queued.delivery, "queued");
1467+
yield* Deferred.succeed(allowFetch, undefined);
1468+
1469+
// Without its worktree the follow-up would start in the project checkout.
1470+
yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe(
1471+
Stream.filter(
1472+
(stored) =>
1473+
stored.event.type === "run.updated" && stored.event.payload.queueHeld === true,
1474+
),
1475+
Stream.runHead,
1476+
);
1477+
const projection = yield* threads.getThreadProjection(launched.threadId);
1478+
assert.deepEqual(
1479+
projection.runs.map((run) => run.status),
1480+
["failed", "queued"],
1481+
);
1482+
assert.equal(projection.thread.worktreePath, null);
1483+
}).pipe(Effect.provide(harness.layer));
1484+
}),
1485+
);
1486+
14251487
it.effect("a retry reuses a recorded worktree without undoing its branch rename", () => {
14261488
let setupFailures = 1;
14271489
const harness = makeHarness({

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,8 @@ export default function ChatView(props: ChatViewProps) {
36723672
// starts on its own instead of being refused.
36733673
const sendQueuesBehindSetup =
36743674
isServerThread && (activeRunPreparing || activeActivityRun?.status === "starting");
3675+
// Such a send skips the local dispatch, so this keeps the composer busy while it is in flight.
3676+
const [setupQueueSendInFlight, setSetupQueueSendInFlight] = useState(false);
36753677
useEffect(() => {
36763678
attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
36773679
}, [attachmentPreviewHandoffByMessageId]);
@@ -9432,7 +9434,9 @@ export default function ChatView(props: ChatViewProps) {
94329434
return;
94339435
}
94349436
// A send queued behind setup leaves the first send's dispatch and setup card alone.
9435-
if (!sendQueuesBehindSetup) {
9437+
if (sendQueuesBehindSetup) {
9438+
setSetupQueueSendInFlight(true);
9439+
} else {
94369440
beginLocalDispatch({
94379441
preparingWorktree: multipleModelSelections !== null || Boolean(baseBranchForWorktree),
94389442
// Only a draft has a background submission to hide behind its hero.
@@ -10117,6 +10121,7 @@ export default function ChatView(props: ChatViewProps) {
1011710121
}
1011810122
}
1011910123
sendInFlightRef.current = false;
10124+
setSetupQueueSendInFlight(false);
1012010125
if (!turnStartSucceeded) {
1012110126
setDockedDraftHeroThreadKey((currentThreadKey) =>
1012210127
currentThreadKey === activeThreadKey ? null : currentThreadKey,
@@ -11630,6 +11635,7 @@ export default function ChatView(props: ChatViewProps) {
1163011635
isConnecting={isConnecting}
1163111636
isSendBusy={
1163211637
(isSendBusy && !sendQueuesBehindSetup) ||
11638+
setupQueueSendInFlight ||
1163311639
isSavingQueuedEdit ||
1163411640
isResuming
1163511641
}

0 commit comments

Comments
 (0)