Skip to content

Commit 4772822

Browse files
authored
fix: worktree threads keep their worktree when the agent starts, and messages sent during setup queue (#17654)
1 parent 5f7294d commit 4772822

8 files changed

Lines changed: 312 additions & 45 deletions

File tree

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,15 @@ export interface ThreadDetailScreenProps {
173173
* The server has not created this thread yet. "preparing" runs while the
174174
* queued creation is delivered (a worktree may be checking out); "failed"
175175
* is a rejected creation whose content went back to the project draft.
176+
* `serverOwned` marks a thread the server already created that is still
177+
* setting up its worktree; follow-ups queue on the server behind that setup.
176178
*/
177179
readonly creationState:
178-
| { readonly kind: "preparing"; readonly preparingWorktree: boolean }
180+
| {
181+
readonly kind: "preparing";
182+
readonly preparingWorktree: boolean;
183+
readonly serverOwned: boolean;
184+
}
179185
| { readonly kind: "failed"; readonly reason: string; readonly onEditTask: () => void }
180186
| null;
181187
readonly activePendingApproval: PendingApproval | null;
@@ -1439,11 +1445,14 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
14391445
canStopThread={props.canStopThread}
14401446
environmentId={props.environmentId}
14411447
projectCwd={props.threadCwd ?? props.projectWorkspaceRoot}
1442-
// Follow-ups typed during setup wait in the draft: queueing
1443-
// them against a thread id the server may still reject
1444-
// would strand them in the outbox.
1448+
// Follow-ups typed before the server creates the thread wait in
1449+
// the draft: queueing them against a thread id the server may
1450+
// still reject would strand them in the outbox.
14451451
sendBlockedReason={
1446-
props.creationState?.kind === "preparing" ? "Starting the task…" : null
1452+
props.creationState?.kind === "preparing" &&
1453+
!props.creationState.serverOwned
1454+
? "Starting the task…"
1455+
: null
14471456
}
14481457
draftKey={props.composerDraftKey ?? undefined}
14491458
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/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,20 @@ export function useThreadComposerState() {
677677
alternateModifier: followUpOverride !== undefined && followUpOverride !== followUpBehavior,
678678
activeTurnDefault: followUpBehavior,
679679
});
680-
const followUpDispatchMode =
681-
followUpAction === "auto" ? null : followUpAction === "queue" ? "queue" : "auto";
680+
// A send while the worktree is still being set up waits behind that setup,
681+
// so the server can hold it if the setup ends without a worktree.
682+
// Before detail loads, the shell's latest run can be a queued follow-up
683+
// while its runtime still reports the setup.
684+
const setupRunning = [selectedThreadActivityRun?.status, selectedThreadRuntime?.status].some(
685+
(status) => status === "preparing" || status === "starting",
686+
);
687+
const followUpDispatchMode = setupRunning
688+
? "queue"
689+
: followUpAction === "auto"
690+
? null
691+
: followUpAction === "queue"
692+
? "queue"
693+
: "auto";
682694

683695
const metadata = makeQueuedMessageMetadata();
684696
const messageId = MessageId.make(metadata.messageId);
@@ -735,7 +747,9 @@ export function useThreadComposerState() {
735747
saveQueuedRunEdit,
736748
selectedEnvironmentRuntime?.connectionState,
737749
selectedEnvironmentRuntime?.serverConfig,
750+
selectedThreadActivityRun?.status,
738751
selectedThreadCreation,
752+
selectedThreadRuntime?.status,
739753
selectedThreadShell,
740754
uploadThreadFeedback,
741755
],

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

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12691269
]);
12701270
});
12711271

1272-
const startNextQueuedRun = (threadId: ThreadId, options?: { readonly failedRunId?: RunId }) =>
1272+
const startNextQueuedRun = (
1273+
threadId: ThreadId,
1274+
options?: { readonly failedRunId?: RunId; readonly endedRunId?: RunId },
1275+
) =>
12731276
Effect.gen(function* () {
12741277
// Every terminal run checks the queue. Only a deliverable queued run
12751278
// needs the transcript for provider handoff and legacy import context.
@@ -1304,15 +1307,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13041307
// Hold the queue so the user decides when to resume it. Validation
13051308
// failures (setup, unsupported handoff) belong to that message alone,
13061309
// and a message queued for another provider is how users recover.
1310+
// A message queued behind a worktree that was cancelled or never created
1311+
// would run in the project checkout instead, so it waits for the user.
13071312
const failedRun = latestExecutedRun(projection.runs);
13081313
const failureClass =
13091314
failedRun?.id === options?.failedRunId
13101315
? latestRootProviderFailure(failedRun, projection.turnItems)?.class
13111316
: undefined;
1317+
const worktreeMissing =
1318+
failedRun?.id === options?.endedRunId &&
1319+
failedRun?.workspacePreparation?.type === "worktree" &&
1320+
projection.thread.worktreePath === null;
13121321
if (
1313-
failureClass !== undefined &&
1314-
failureClass !== "validation_error" &&
1315-
failedRun?.providerInstanceId === queuedRun.providerInstanceId
1322+
worktreeMissing ||
1323+
(failureClass !== undefined &&
1324+
failureClass !== "validation_error" &&
1325+
failedRun?.providerInstanceId === queuedRun.providerInstanceId)
13161326
) {
13171327
const now = yield* DateTime.now;
13181328
yield* writeSystemEvents(
@@ -4871,9 +4881,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
48714881
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
48724882
);
48734883
const activeRun = projection.runs.find(isBlockingRun);
4884+
// A message queued during worktree setup can arrive after that setup failed
4885+
// without a worktree. It waits, held, like the messages queued before the
4886+
// failure, instead of starting in the project checkout.
4887+
const latestRun = latestExecutedRun(projection.runs);
4888+
const worktreeMissingRun =
4889+
activeRun === undefined &&
4890+
dispatchMode.type === "queue_after_active" &&
4891+
(latestRun?.status === "failed" || latestRun?.status === "interrupted") &&
4892+
latestRun.workspacePreparation?.type === "worktree" &&
4893+
projection.thread.worktreePath === null
4894+
? latestRun
4895+
: undefined;
4896+
const queueBehindRun = activeRun ?? worktreeMissingRun;
48744897
const pendingMergeBackTransfers = pendingMergeBackTransfersForThread(projection);
48754898
const shouldQueue =
4876-
activeRun !== undefined &&
4899+
queueBehindRun !== undefined &&
48774900
(dispatchMode.type === "defer_start" ||
48784901
dispatchMode.type === "start_immediately" ||
48794902
dispatchMode.type === "queue_after_active");
@@ -4888,13 +4911,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
48884911
const queueProviderThread =
48894912
activeProviderThread ??
48904913
projection.providerThreads.find(
4891-
(candidate) => candidate.id === activeRun.providerThreadId,
4914+
(candidate) => candidate.id === queueBehindRun.providerThreadId,
48924915
);
48934916
if (queueProviderThread === undefined) {
48944917
return yield* new OrchestratorDispatchError({
48954918
commandId: command.commandId,
48964919
commandType: command.type,
4897-
cause: `Active run ${activeRun.id} has no provider thread for queued dispatch.`,
4920+
cause: `Active run ${queueBehindRun.id} has no provider thread for queued dispatch.`,
48984921
});
48994922
}
49004923
const now = yield* DateTime.now;
@@ -4948,7 +4971,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49484971
const attemptId = idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 });
49494972
const rootNodeId = idAllocator.derive.rootNode({ runId });
49504973
const checkpointScope =
4951-
activeRun.status === "preparing"
4974+
queueBehindRun.status === "preparing" || worktreeMissingRun !== undefined
49524975
? null
49534976
: yield* runtimePolicy.resolve({ thread: projection.thread, modelSelection }).pipe(
49544977
Effect.flatMap((resolvedRuntimePolicy) =>
@@ -4985,7 +5008,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49855008
rootNodeId,
49865009
activeAttemptId: attemptId,
49875010
status: "queued",
4988-
...(projection.runs.some(
5011+
...(worktreeMissingRun !== undefined ||
5012+
projection.runs.some(
49895013
(candidate) => candidate.status === "queued" && candidate.queueHeld === true,
49905014
)
49915015
? { queueHeld: true }
@@ -8292,6 +8316,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
82928316
type: "run.updated",
82938317
payload: { ...state.run, status: "preparing", completedAt: null },
82948318
});
8319+
// Messages held when this preparation failed follow the run again. A hold
8320+
// from a later run (a stop, say) is not this failure's to release.
8321+
const failureOwnsQueue = latestExecutedRun(projection.runs)?.id === state.run.id;
8322+
for (const run of projection.runs) {
8323+
if (!failureOwnsQueue || run.status !== "queued" || run.queueHeld !== true) continue;
8324+
yield* emitEvent({
8325+
type: "run.updated",
8326+
threadId: command.threadId,
8327+
runId: run.id,
8328+
providerInstanceId: run.providerInstanceId,
8329+
occurredAt: now,
8330+
payload: { ...run, queueHeld: false },
8331+
});
8332+
}
82958333
});
82968334

82978335
/**
@@ -11056,8 +11094,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1105611094
threadId,
1105711095
startNextQueuedRun(
1105811096
threadId,
11059-
stored.event.type === "run.updated" && stored.event.payload.status === "failed"
11060-
? { failedRunId: stored.event.payload.id }
11097+
stored.event.type === "run.updated"
11098+
? {
11099+
endedRunId: stored.event.payload.id,
11100+
...(stored.event.payload.status === "failed"
11101+
? { failedRunId: stored.event.payload.id }
11102+
: {}),
11103+
}
1106111104
: undefined,
1106211105
),
1106311106
)

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

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

1427+
it.effect.each(["fails", "is interrupted"] as const)(
1428+
"holds a message queued during setup when the workspace preparation %s",
1429+
(ending) =>
1430+
Effect.gen(function* () {
1431+
const fetchEntered = yield* Deferred.make<void>();
1432+
const allowFetch = yield* Deferred.make<void>();
1433+
let fetchFailures = 1;
1434+
const harness = makeHarness({
1435+
fetchRemote: () =>
1436+
fetchFailures-- <= 0
1437+
? Effect.void
1438+
: Deferred.succeed(fetchEntered, undefined).pipe(
1439+
Effect.andThen(Deferred.await(allowFetch)),
1440+
Effect.andThen(
1441+
Effect.fail(
1442+
new GitCommandError({
1443+
operation: "GitVcsDriver.fetchRemote",
1444+
command: "git",
1445+
cwd: project.workspaceRoot,
1446+
detail: "Git could not reach the remote.",
1447+
exitCode: 128,
1448+
}),
1449+
),
1450+
),
1451+
),
1452+
});
1453+
yield* Effect.gen(function* () {
1454+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1455+
const threads = yield* ThreadManagement.ThreadManagementService;
1456+
const launched = yield* launches.launch(
1457+
launchInput({
1458+
command: `command:launch:queued-setup-${ending}`,
1459+
thread: `thread:launch:queued-setup-${ending}`,
1460+
message: "First message",
1461+
workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
1462+
}),
1463+
);
1464+
yield* Deferred.await(fetchEntered);
1465+
const queued = yield* threads.sendToThread({
1466+
projectId,
1467+
commandId: CommandId.make(`command:launch:queued-setup-${ending}:follow-up`),
1468+
threadId: launched.threadId,
1469+
messageId: MessageId.make(`message:launch:queued-setup-${ending}:follow-up`),
1470+
text: "Sent during setup",
1471+
attachments: [],
1472+
mode: "queue",
1473+
createdBy: "user",
1474+
creationSource: "web",
1475+
});
1476+
assert.equal(queued.delivery, "queued");
1477+
if (ending === "fails") {
1478+
yield* Deferred.succeed(allowFetch, undefined);
1479+
} else {
1480+
// An agent stop does not ask to hold the queue.
1481+
yield* threads.dispatch({
1482+
type: "run.interrupt",
1483+
commandId: CommandId.make("command:launch:queued-setup-interrupt"),
1484+
threadId: launched.threadId,
1485+
runId: launched.projection.runs[0]!.id,
1486+
});
1487+
}
1488+
1489+
// Without its worktree the follow-up would start in the project checkout.
1490+
yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe(
1491+
Stream.filter(
1492+
(stored) =>
1493+
stored.event.type === "run.updated" && stored.event.payload.queueHeld === true,
1494+
),
1495+
Stream.runHead,
1496+
);
1497+
const projection = yield* threads.getThreadProjection(launched.threadId);
1498+
assert.equal(projection.runs.at(-1)?.status, "queued");
1499+
assert.equal(projection.thread.worktreePath, null);
1500+
1501+
if (ending === "fails") {
1502+
// A successful retry lets the held message follow the first turn again.
1503+
yield* launches.retryPreparation({
1504+
commandId: CommandId.make("command:launch:queued-setup-retry"),
1505+
threadId: launched.threadId,
1506+
runId: launched.projection.runs[0]!.id,
1507+
});
1508+
const retried = yield* threads.getThreadProjection(launched.threadId);
1509+
assert.equal(retried.runs.at(-1)?.queueHeld, false);
1510+
}
1511+
}).pipe(Effect.provide(harness.layer));
1512+
}),
1513+
);
1514+
1515+
it.effect.each(["failed", "interrupted"] as const)(
1516+
"holds a message queued during setup that arrives after the setup %s",
1517+
(ending) => {
1518+
let fetchEntered: Deferred.Deferred<void> | null = null;
1519+
const harness = makeHarness({
1520+
fetchRemote: () =>
1521+
ending === "interrupted"
1522+
? Deferred.succeed(fetchEntered!, undefined).pipe(Effect.andThen(Effect.never))
1523+
: Effect.fail(
1524+
new GitCommandError({
1525+
operation: "GitVcsDriver.fetchRemote",
1526+
command: "git",
1527+
cwd: project.workspaceRoot,
1528+
detail: "Git could not reach the remote.",
1529+
exitCode: 128,
1530+
}),
1531+
),
1532+
});
1533+
return Effect.gen(function* () {
1534+
fetchEntered = yield* Deferred.make<void>();
1535+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1536+
const threads = yield* ThreadManagement.ThreadManagementService;
1537+
const launched = yield* launches.launch(
1538+
launchInput({
1539+
command: `command:launch:late-queued-follow-up-${ending}`,
1540+
thread: `thread:launch:late-queued-follow-up-${ending}`,
1541+
message: "First message",
1542+
workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
1543+
}),
1544+
);
1545+
if (ending === "interrupted") {
1546+
yield* Deferred.await(fetchEntered);
1547+
// An agent stop does not ask to hold the queue.
1548+
yield* threads.dispatch({
1549+
type: "run.interrupt",
1550+
commandId: CommandId.make("command:launch:late-queued-follow-up:interrupt"),
1551+
threadId: launched.threadId,
1552+
runId: launched.projection.runs[0]!.id,
1553+
});
1554+
}
1555+
yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe(
1556+
Stream.filter(
1557+
(stored) => stored.event.type === "run.updated" && stored.event.payload.status === ending,
1558+
),
1559+
Stream.runHead,
1560+
);
1561+
1562+
// Queued while setup ran, delivered only after it failed: it must not start
1563+
// in the project checkout.
1564+
const queued = yield* threads.sendToThread({
1565+
projectId,
1566+
commandId: CommandId.make(`command:launch:late-queued-follow-up-${ending}:send`),
1567+
threadId: launched.threadId,
1568+
messageId: MessageId.make(`message:launch:late-queued-follow-up-${ending}:send`),
1569+
text: "Sent during setup",
1570+
attachments: [],
1571+
mode: "queue",
1572+
createdBy: "user",
1573+
creationSource: "web",
1574+
});
1575+
assert.equal(queued.delivery, "queued");
1576+
const projection = yield* threads.getThreadProjection(launched.threadId);
1577+
assert.equal(projection.runs.at(-1)?.status, "queued");
1578+
assert.equal(projection.runs.at(-1)?.queueHeld, true);
1579+
}).pipe(Effect.provide(harness.layer));
1580+
},
1581+
);
1582+
14271583
it.effect("a retry reuses a recorded worktree without undoing its branch rename", () => {
14281584
let setupFailures = 1;
14291585
const harness = makeHarness({

0 commit comments

Comments
 (0)