Skip to content

Commit 38dbabb

Browse files
committed
fix(server): a partial launch retry prepares the folder it already bound
The recorded-strategy preference for replays also applied to a retry whose message dispatch never landed. That retry creates its run here, so the run only echoes the strategy chosen above, and preferring it over the bound-folder recovery sent preparation to the shared project root and cleared the folder the accepted create had already claimed. The recorded strategy now only wins when the message was already accepted, which is the replay it was written for. Commit made by Space Bunny Free in opencode on behalf of Kriday.
1 parent c403da8 commit 38dbabb

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,66 @@ it.effect("runs a worktree request against a non-repository project as a root la
11381138
}),
11391139
);
11401140

1141+
it.effect("keeps the claimed folder when a retried launch never dispatched its message", () =>
1142+
Effect.gen(function* () {
1143+
// The first attempt claims the folder and then fails to dispatch the message,
1144+
// so the retry has a launch receipt but no message receipt. The thread still
1145+
// owns that folder, and the retry must prepare it rather than the shared root.
1146+
const claimed: Array<string> = [];
1147+
const harness = makeHarness({
1148+
isGitRepository: () => Effect.succeed(false),
1149+
managedFolders: Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
1150+
namedProjectsRoot: "/projects",
1151+
folderForThread: (input) =>
1152+
Effect.sync(() => {
1153+
claimed.push(input.threadId);
1154+
return input.projectId === projectId
1155+
? Option.some("/scratch/folder-partial")
1156+
: Option.none();
1157+
}),
1158+
}),
1159+
});
1160+
yield* Effect.gen(function* () {
1161+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1162+
const threads = yield* ThreadManagement.ThreadManagementService;
1163+
const input = launchInput({
1164+
command: "command:launch:scratch-partial",
1165+
thread: "thread:launch:scratch-partial",
1166+
message: "Review this",
1167+
workspace: { type: "worktree", baseRef: "main" },
1168+
});
1169+
1170+
// The accepted create with no message behind it, which is what a failed
1171+
// dispatch leaves behind.
1172+
const threadId = ThreadId.make("thread:launch:scratch-partial");
1173+
yield* threads.dispatch({
1174+
type: "thread.create",
1175+
commandId: input.commandId,
1176+
threadId,
1177+
projectId,
1178+
title: input.title,
1179+
modelSelection,
1180+
runtimeMode: input.runtimeMode,
1181+
interactionMode: input.interactionMode,
1182+
branch: null,
1183+
worktreePath: "/scratch/folder-partial",
1184+
createdBy: "user",
1185+
creationSource: "web",
1186+
});
1187+
assert.lengthOf(claimed, 0);
1188+
1189+
const retried = yield* launches.launch(input);
1190+
assert.isTrue(retried.resumed);
1191+
// The retry claims no second folder; it prepares the one already bound.
1192+
assert.lengthOf(claimed, 0);
1193+
assert.equal(retried.projection.thread.worktreePath, "/scratch/folder-partial");
1194+
yield* waitUntil(() => Effect.sync(() => harness.runSetup.mock.calls.length === 1));
1195+
assert.equal(harness.runSetup.mock.calls[0]?.[0]?.worktreePath, "/scratch/folder-partial");
1196+
assert.equal(harness.createWorktree.mock.calls.length, 0);
1197+
}).pipe(Effect.provide(harness.layer));
1198+
}),
1199+
);
1200+
11411201
it.effect("replays a degraded worktree request without detecting the repository again", () =>
11421202
Effect.gen(function* () {
11431203
// The second attempt would fail if it detected again. Its run already recorded

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,13 @@ const make = Effect.gen(function* () {
883883
// A retried root launch prepares the folder its first attempt bound, so
884884
// a Scratch thread keeps its own. Other root launches bind no folder.
885885
const boundWorktreePath = projection.thread.worktreePath;
886-
// A replay prepares the workspace its accepted run recorded. Re-deriving
887-
// it here would let a changed answer replace what the thread already
888-
// bound, so the recorded strategy wins whenever there is one.
886+
// A replay prepares the workspace its accepted run recorded. A run this
887+
// attempt just created only echoes the strategy above, so reading it back
888+
// would shadow the bound-folder recovery a partial launch still needs.
889889
const recordedStrategy =
890-
runId === null
891-
? undefined
892-
: projection.runs.find((run) => run.id === runId)?.workspacePreparation;
890+
messageWasAlreadyAccepted && runId !== null
891+
? projection.runs.find((run) => run.id === runId)?.workspacePreparation
892+
: undefined;
893893
const preparationStrategy: ThreadLaunchWorkspaceStrategy =
894894
recordedStrategy ??
895895
(Option.isSome(launchReceipt) &&

0 commit comments

Comments
 (0)