Skip to content

Commit 02129cf

Browse files
committed
fix(server): stop re-detecting the repository when a launch replays
A replayed launch had to pass a fresh repository check before it could read the workspace its accepted run already recorded, so a detection failure failed a launch whose message was accepted and a changed answer could contradict the folder the thread bound. The message command id is derived from the launch command id, so the replay is known before any detection runs. Detection now only happens for a first attempt, and the recorded strategy still wins over anything derived. Commit made by Space Bunny Free in opencode on behalf of Kriday.
1 parent 4e307f8 commit 02129cf

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

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

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

1141+
it.effect("replays a degraded worktree request without detecting the repository again", () =>
1142+
Effect.gen(function* () {
1143+
// The second attempt would fail if it detected again. Its run already recorded
1144+
// the folder the first attempt claimed, so the replay must not depend on it.
1145+
let detections = 0;
1146+
const harness = makeHarness({
1147+
isGitRepository: () =>
1148+
Effect.sync(() => ++detections).pipe(
1149+
Effect.flatMap((attempt) =>
1150+
attempt === 1
1151+
? Effect.succeed(false)
1152+
: Effect.fail(
1153+
new GitCommandError({
1154+
operation: "isGitRepository",
1155+
command: "git-rev-parse",
1156+
cwd: "/scratch",
1157+
detail: "Git is unreachable right now.",
1158+
}),
1159+
),
1160+
),
1161+
),
1162+
managedFolders: Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
1163+
namedProjectsRoot: "/projects",
1164+
folderForThread: (input) =>
1165+
Effect.sync(() =>
1166+
input.projectId === projectId
1167+
? Option.some("/scratch/folder-replayed-worktree")
1168+
: Option.none(),
1169+
),
1170+
}),
1171+
});
1172+
yield* Effect.gen(function* () {
1173+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1174+
const input = launchInput({
1175+
command: "command:launch:scratch-worktree-replay",
1176+
thread: "thread:launch:scratch-worktree-replay",
1177+
message: "Review this",
1178+
workspace: { type: "worktree", baseRef: "main" },
1179+
});
1180+
const launched = yield* launches.launch(input);
1181+
assert.equal(launched.projection.thread.worktreePath, "/scratch/folder-replayed-worktree");
1182+
1183+
const replayed = yield* launches.launch(input);
1184+
assert.isTrue(replayed.resumed);
1185+
assert.equal(detections, 1);
1186+
assert.equal(replayed.projection.thread.worktreePath, "/scratch/folder-replayed-worktree");
1187+
assert.equal(harness.createWorktree.mock.calls.length, 0);
1188+
}).pipe(Effect.provide(harness.layer));
1189+
}),
1190+
);
1191+
11411192
it.effect(
11421193
"fails a worktree request against a non-Git repository instead of running it at the root",
11431194
() =>

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,14 +743,26 @@ const make = Effect.gen(function* () {
743743
yield* validateReusableThread(input, candidateThreadId);
744744
}
745745

746+
// Derived from the command id, so a replay of an accepted launch is known
747+
// before any detection runs.
748+
const messageCommandId = CommandId.make(`${input.commandId}:initial-message`);
749+
const messageWasAlreadyAccepted =
750+
input.initialMessage === undefined
751+
? false
752+
: Option.isSome(yield* readReceipt(input, messageCommandId));
753+
746754
// A worktree needs a Git repository. The Scratch project is a folder, not
747755
// a checkout, so a worktree request there becomes a root launch, which the
748756
// block below answers with a folder of its own. Resolved before the
749757
// strategy is recorded on the run, so a retry replays what happened. Only
750758
// "no VCS at all" degrades: a non-Git repository keeps its own
751759
// unsupported-VCS failure instead of being treated as a shared folder.
760+
// An accepted launch already recorded the answer its run will prepare, so
761+
// detecting again could only fail a replay or contradict what the thread
762+
// bound.
752763
const requestedStrategy: ThreadLaunchWorkspaceStrategy =
753764
input.workspaceStrategy.type === "worktree" &&
765+
!messageWasAlreadyAccepted &&
754766
!(yield* git
755767
.isGitRepository(project.workspaceRoot)
756768
.pipe(Effect.mapError(mapError(input, "provision-worktree", candidateThreadId))))
@@ -820,11 +832,7 @@ const make = Effect.gen(function* () {
820832
}
821833

822834
let runId: RunId | null = null;
823-
let messageWasAlreadyAccepted = false;
824835
if (input.initialMessage !== undefined) {
825-
const messageCommandId = CommandId.make(`${input.commandId}:initial-message`);
826-
const messageReceipt = yield* readReceipt(input, messageCommandId);
827-
messageWasAlreadyAccepted = Option.isSome(messageReceipt);
828836
const messageId =
829837
input.initialMessage.messageId ??
830838
(yield* ids.allocate

0 commit comments

Comments
 (0)