Skip to content

Commit de0b3ef

Browse files
t3dotggclaude
andcommitted
fix(server): cancelling setup stops its script without a worktree too
The setup terminal was only closed when a cancelled launch had created a worktree, so cancelling during an empty-repo fallback (or a retry that reuses a worktree) left the script running. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent ea99e37 commit de0b3ef

2 files changed

Lines changed: 59 additions & 8 deletions

File tree

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ function makeHarness(options: HarnessOptions = {}) {
137137
const runSetup = vi.fn(
138138
options.runSetup ?? (() => Effect.succeed({ status: "no-script" as const })),
139139
);
140+
const closeTerminal = vi.fn(
141+
(_input: Parameters<TerminalManager.TerminalManager["Service"]["close"]>[0]) => Effect.void,
142+
);
140143
const generateBranchName = vi.fn(
141144
options.generateBranchName ?? (() => Effect.succeed({ branch: "generated-branch" })),
142145
);
@@ -146,7 +149,7 @@ function makeHarness(options: HarnessOptions = {}) {
146149
const externalServices = Layer.mergeAll(
147150
WorktreeSetupTracker.layer,
148151
Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({ get: () => Effect.succeed(null) }),
149-
Layer.mock(TerminalManager.TerminalManager)({ close: () => Effect.void }),
152+
Layer.mock(TerminalManager.TerminalManager)({ close: closeTerminal }),
150153
Layer.succeed(ProjectService.ProjectService, {
151154
create: () => Effect.die("unused"),
152155
bootstrap: () => Effect.die("unused"),
@@ -233,6 +236,7 @@ function makeHarness(options: HarnessOptions = {}) {
233236
generateBranchName,
234237
generateThreadTitle,
235238
runSetup,
239+
closeTerminal,
236240
};
237241
}
238242

@@ -2237,6 +2241,52 @@ it.effect("cancels tracked setup before provider work is released", () =>
22372241
}),
22382242
);
22392243

2244+
it.effect("cancelling setup in an empty repository stops its script in the project folder", () =>
2245+
Effect.gen(function* () {
2246+
const entered = yield* Deferred.make<void>();
2247+
const harness = makeHarness({
2248+
hasCommit: () => Effect.succeed(false),
2249+
runSetup: (input) =>
2250+
Deferred.succeed(entered, undefined).pipe(
2251+
Effect.as({
2252+
status: "started" as const,
2253+
async: false,
2254+
scriptId: "setup",
2255+
scriptName: "Setup",
2256+
scriptCommand: "vp install",
2257+
terminalId: "setup",
2258+
cwd: input.worktreePath,
2259+
completion: Effect.never,
2260+
}),
2261+
),
2262+
});
2263+
yield* Effect.gen(function* () {
2264+
const launches = yield* ThreadLaunch.ThreadLaunchService;
2265+
const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
2266+
const launched = yield* launches.launch(
2267+
launchInput({
2268+
command: "launch:cancel-empty-repo",
2269+
thread: "thread:cancel-empty-repo",
2270+
message: "Start",
2271+
workspace: { type: "worktree", baseRef: "main" },
2272+
}),
2273+
);
2274+
yield* Deferred.await(entered);
2275+
yield* waitUntil(() =>
2276+
tracker
2277+
.get(launched.threadId)
2278+
.pipe(Effect.map((setup) => setup?.setupScript?.terminalId === "setup")),
2279+
);
2280+
assert.isTrue(yield* tracker.cancel(launched.threadId));
2281+
assert.deepInclude(harness.closeTerminal.mock.calls[0]?.[0], {
2282+
threadId: launched.threadId,
2283+
terminalId: "setup",
2284+
});
2285+
assert.equal(harness.removeWorktree.mock.calls.length, 0);
2286+
}).pipe(Effect.provide(harness.layer));
2287+
}),
2288+
);
2289+
22402290
it.effect.each([0, 1])("releases an async setup before its completion with exit %s", (exitCode) =>
22412291
Effect.gen(function* () {
22422292
const completion = yield* Deferred.make<{ exitCode: number | null; durationMs: number }>();

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,15 @@ const make = Effect.gen(function* () {
574574
cancelled ? "cancelled" : "failed",
575575
cancelled ? null : failureDetail(Cause.squash(cause)),
576576
);
577-
// A cancelled setup leaves nothing behind. A failed one keeps a worktree
578-
// the thread recorded, so a retry reuses it, and removes one it never
579-
// recorded, which a retry would otherwise duplicate.
577+
// A cancelled setup leaves nothing behind, including a script still
578+
// running in a worktree or the project folder. A failed one keeps a
579+
// worktree the thread recorded, so a retry reuses it, and removes one
580+
// it never recorded, which a retry would otherwise duplicate.
581+
if (tracked && cancelled && setupTerminalId)
582+
yield* terminals
583+
.close({ threadId, terminalId: setupTerminalId, deleteHistory: true })
584+
.pipe(Effect.ignore);
580585
if (tracked && createdWorktreePath && (cancelled || !workspaceRecorded)) {
581-
if (setupTerminalId)
582-
yield* terminals
583-
.close({ threadId, terminalId: setupTerminalId, deleteHistory: true })
584-
.pipe(Effect.ignore);
585586
const removedPath = createdWorktreePath;
586587
// The thread forgets the worktree only once it is gone; a failed
587588
// removal leaves the directory for the user to clean up rather than

0 commit comments

Comments
 (0)