Skip to content

Commit 3dbeab4

Browse files
committed
fix: hold follow-ups when setup is interrupted and release them on retry
1 parent c7e0fbf commit 3dbeab4

3 files changed

Lines changed: 112 additions & 67 deletions

File tree

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12631263
]);
12641264
});
12651265

1266-
const startNextQueuedRun = (threadId: ThreadId, options?: { readonly failedRunId?: RunId }) =>
1266+
const startNextQueuedRun = (
1267+
threadId: ThreadId,
1268+
options?: { readonly failedRunId?: RunId; readonly endedRunId?: RunId },
1269+
) =>
12671270
Effect.gen(function* () {
12681271
// Every terminal run checks the queue. Only a deliverable queued run
12691272
// needs the transcript for provider handoff and legacy import context.
@@ -1306,7 +1309,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13061309
? latestRootProviderFailure(failedRun, projection.turnItems)?.class
13071310
: undefined;
13081311
const worktreeMissing =
1309-
failedRun?.id === options?.failedRunId &&
1312+
failedRun?.id === options?.endedRunId &&
13101313
failedRun?.workspacePreparation?.type === "worktree" &&
13111314
projection.thread.worktreePath === null;
13121315
if (
@@ -8111,6 +8114,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
81118114
type: "run.updated",
81128115
payload: { ...state.run, status: "preparing", completedAt: null },
81138116
});
8117+
// Messages held when this preparation failed follow the run again.
8118+
for (const run of projection.runs) {
8119+
if (run.status !== "queued" || run.queueHeld !== true) continue;
8120+
yield* emitEvent({
8121+
type: "run.updated",
8122+
threadId: command.threadId,
8123+
runId: run.id,
8124+
providerInstanceId: run.providerInstanceId,
8125+
occurredAt: now,
8126+
payload: { ...run, queueHeld: false },
8127+
});
8128+
}
81148129
});
81158130

81168131
/**
@@ -10656,8 +10671,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1065610671
threadId,
1065710672
startNextQueuedRun(
1065810673
threadId,
10659-
stored.event.type === "run.updated" && stored.event.payload.status === "failed"
10660-
? { failedRunId: stored.event.payload.id }
10674+
stored.event.type === "run.updated"
10675+
? {
10676+
endedRunId: stored.event.payload.id,
10677+
...(stored.event.payload.status === "failed"
10678+
? { failedRunId: stored.event.payload.id }
10679+
: {}),
10680+
}
1066110681
: undefined,
1066210682
),
1066310683
)

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

Lines changed: 81 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,66 +1422,89 @@ 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",
1425+
it.effect.each(["fails", "is interrupted"] as const)(
1426+
"holds a message queued during setup when the workspace preparation %s",
1427+
(ending) =>
1428+
Effect.gen(function* () {
1429+
const allowFetch = yield* Deferred.make<void>();
1430+
let fetchFailures = 1;
1431+
const harness = makeHarness({
1432+
fetchRemote: () =>
1433+
fetchFailures-- <= 0
1434+
? Effect.void
1435+
: Deferred.await(allowFetch).pipe(
1436+
Effect.andThen(
1437+
Effect.fail(
1438+
new GitCommandError({
1439+
operation: "GitVcsDriver.fetchRemote",
1440+
command: "git",
1441+
cwd: project.workspaceRoot,
1442+
detail: "Git could not reach the remote.",
1443+
exitCode: 128,
1444+
}),
1445+
),
1446+
),
1447+
),
14651448
});
1466-
assert.equal(queued.delivery, "queued");
1467-
yield* Deferred.succeed(allowFetch, undefined);
1449+
yield* Effect.gen(function* () {
1450+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1451+
const threads = yield* ThreadManagement.ThreadManagementService;
1452+
const launched = yield* launches.launch(
1453+
launchInput({
1454+
command: `command:launch:queued-setup-${ending}`,
1455+
thread: `thread:launch:queued-setup-${ending}`,
1456+
message: "First message",
1457+
workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
1458+
}),
1459+
);
1460+
const queued = yield* threads.sendToThread({
1461+
projectId,
1462+
commandId: CommandId.make(`command:launch:queued-setup-${ending}:follow-up`),
1463+
threadId: launched.threadId,
1464+
messageId: MessageId.make(`message:launch:queued-setup-${ending}:follow-up`),
1465+
text: "Sent during setup",
1466+
attachments: [],
1467+
mode: "queue",
1468+
createdBy: "user",
1469+
creationSource: "web",
1470+
});
1471+
assert.equal(queued.delivery, "queued");
1472+
if (ending === "fails") {
1473+
yield* Deferred.succeed(allowFetch, undefined);
1474+
} else {
1475+
// An agent stop does not ask to hold the queue.
1476+
yield* threads.dispatch({
1477+
type: "run.interrupt",
1478+
commandId: CommandId.make("command:launch:queued-setup-interrupt"),
1479+
threadId: launched.threadId,
1480+
runId: launched.projection.runs[0]!.id,
1481+
});
1482+
}
14681483

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-
}),
1484+
// Without its worktree the follow-up would start in the project checkout.
1485+
yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe(
1486+
Stream.filter(
1487+
(stored) =>
1488+
stored.event.type === "run.updated" && stored.event.payload.queueHeld === true,
1489+
),
1490+
Stream.runHead,
1491+
);
1492+
const projection = yield* threads.getThreadProjection(launched.threadId);
1493+
assert.equal(projection.runs.at(-1)?.status, "queued");
1494+
assert.equal(projection.thread.worktreePath, null);
1495+
1496+
if (ending === "fails") {
1497+
// A successful retry lets the held message follow the first turn again.
1498+
yield* launches.retryPreparation({
1499+
commandId: CommandId.make("command:launch:queued-setup-retry"),
1500+
threadId: launched.threadId,
1501+
runId: launched.projection.runs[0]!.id,
1502+
});
1503+
const retried = yield* threads.getThreadProjection(launched.threadId);
1504+
assert.equal(retried.runs.at(-1)?.queueHeld, false);
1505+
}
1506+
}).pipe(Effect.provide(harness.layer));
1507+
}),
14851508
);
14861509

14871510
it.effect("a retry reuses a recorded worktree without undoing its branch rename", () => {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,8 +3672,9 @@ 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);
3675+
// Such a send skips the local dispatch, so this keeps its thread's composer busy while it is
3676+
// in flight.
3677+
const [setupQueueSendThreadKey, setSetupQueueSendThreadKey] = useState<string | null>(null);
36773678
useEffect(() => {
36783679
attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
36793680
}, [attachmentPreviewHandoffByMessageId]);
@@ -9435,7 +9436,7 @@ export default function ChatView(props: ChatViewProps) {
94359436
}
94369437
// A send queued behind setup leaves the first send's dispatch and setup card alone.
94379438
if (sendQueuesBehindSetup) {
9438-
setSetupQueueSendInFlight(true);
9439+
setSetupQueueSendThreadKey(activeThreadKey);
94399440
} else {
94409441
beginLocalDispatch({
94419442
preparingWorktree: multipleModelSelections !== null || Boolean(baseBranchForWorktree),
@@ -10121,7 +10122,7 @@ export default function ChatView(props: ChatViewProps) {
1012110122
}
1012210123
}
1012310124
sendInFlightRef.current = false;
10124-
setSetupQueueSendInFlight(false);
10125+
setSetupQueueSendThreadKey((current) => (current === activeThreadKey ? null : current));
1012510126
if (!turnStartSucceeded) {
1012610127
setDockedDraftHeroThreadKey((currentThreadKey) =>
1012710128
currentThreadKey === activeThreadKey ? null : currentThreadKey,
@@ -11635,7 +11636,8 @@ export default function ChatView(props: ChatViewProps) {
1163511636
isConnecting={isConnecting}
1163611637
isSendBusy={
1163711638
(isSendBusy && !sendQueuesBehindSetup) ||
11638-
setupQueueSendInFlight ||
11639+
(setupQueueSendThreadKey !== null &&
11640+
setupQueueSendThreadKey === activeThreadKey) ||
1163911641
isSavingQueuedEdit ||
1164011642
isResuming
1164111643
}

0 commit comments

Comments
 (0)