Skip to content
TypeScript

fix(orchestration): gate prepared provider starts (#23669)

parmartejas28c7d3e76780
diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
index ea1669f4b7d..852c6e81b01 100644
--- a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
@@ -1159,6 +1159,100 @@ it.effect("shows the fetch diagnosis when preparing a worktree from origin fails
}).pipe(Effect.provide(harness.layer));
});
+it.effect("rejects a preparation retry after the project disables its provider", () => {
+ let fetchAttempts = 0;
+ const harness = makeHarness({
+ fetchWorktreeBaseCommit: () =>
+ fetchAttempts++ === 0
+ ? Effect.fail(
+ new GitCommandError({
+ operation: "GitVcsDriver.fetchRemote",
+ command: "git",
+ cwd: project.workspaceRoot,
+ detail: "Git could not reach the remote.",
+ exitCode: 128,
+ }),
+ )
+ : Effect.succeed({ commitSha: "remote-main-sha", remoteRefName: "origin/main" }),
+ });
+ return Effect.gen(function* () {
+ const launches = yield* ThreadLaunch.ThreadLaunchService;
+ const settings = yield* ServerSettings.ServerSettingsService;
+ const threads = yield* ThreadManagement.ThreadManagementService;
+ const launched = yield* launches.launch(
+ launchInput({
+ command: "command:launch:disabled-preparation-retry",
+ thread: "thread:launch:disabled-preparation-retry",
+ message: "Start from origin",
+ workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
+ }),
+ );
+ yield* preparationSettled(launched.threadId);
+ yield* settings.updateSettings({
+ projectSettingsOverrides: {
+ [projectId]: { providerInstances: { [modelSelection.instanceId]: { enabled: false } } },
+ },
+ });
+
+ const error = yield* launches
+ .retryPreparation({
+ commandId: CommandId.make("command:launch:disabled-preparation-retry:retry"),
+ threadId: launched.threadId,
+ runId: launched.projection.runs[0]!.id,
+ })
+ .pipe(Effect.flip);
+
+ assert.equal(error._tag, "ThreadManagementProviderInstanceDisabledError");
+ assert.equal((yield* threads.getThreadProjection(launched.threadId)).runs[0]?.status, "failed");
+ assert.equal(fetchAttempts, 1);
+ }).pipe(Effect.provide(harness.layer));
+});
+
+it.effect("does not release a prepared run after the project disables its provider", () => {
+ return Effect.gen(function* () {
+ const setupEntered = yield* Deferred.make<void>();
+ const allowSetup = yield* Deferred.make<void>();
+ const harness = makeHarness({
+ runSetup: () =>
+ Deferred.succeed(setupEntered, undefined).pipe(
+ Effect.andThen(Deferred.await(allowSetup)),
+ Effect.as({ status: "no-script" as const }),
+ ),
+ });
+ yield* Effect.gen(function* () {
+ const launches = yield* ThreadLaunch.ThreadLaunchService;
+ const settings = yield* ServerSettings.ServerSettingsService;
+ const launched = yield* launches.launch(
+ launchInput({
+ command: "command:launch:disable-during-preparation",
+ thread: "thread:launch:disable-during-preparation",
+ message: "Prepare a worktree",
+ workspace: { type: "worktree", baseRef: "main" },
+ }),
+ );
+ yield* Deferred.await(setupEntered);
+ yield* settings.updateSettings({
+ projectSettingsOverrides: {
+ [projectId]: { providerInstances: { [modelSelection.instanceId]: { enabled: false } } },
+ },
+ });
+ yield* Deferred.succeed(allowSetup, undefined);
+ const projection = yield* preparationSettled(launched.threadId);
+ assert.equal(projection.runs[0]?.status, "failed");
+ const effects = yield* (yield* EffectOutbox.EffectOutboxV2).listByCommandId(
+ CommandId.make("command:launch:disable-during-preparation:release"),
+ );
+ assert.isFalse(
+ effects.some(
+ (effect) =>
+ effect.request.type === "provider-turn.start" &&
+ effect.request.runId === launched.projection.runs[0]?.id,
+ ),
+ );
+ }).pipe(Effect.provide(harness.layer));
+ });
+});
+
it.effect.each(["fails", "is interrupted"] as const)(
"holds a message queued during setup when the workspace preparation %s",
(ending) =>
diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.ts b/apps/server/src/orchestration-v2/ThreadManagementService.ts
index 192ed4007fc..15417783cd7 100644
--- a/apps/server/src/orchestration-v2/ThreadManagementService.ts
+++ b/apps/server/src/orchestration-v2/ThreadManagementService.ts
@@ -724,6 +724,22 @@ const make = Effect.gen(function* () {
}),
),
);
+ case "prepared-run.release":
+ case "prepared-run.retry":
+ return orchestrator
+ .getThreadRecords(command.threadId, ["runs"], { runIds: [command.runId] })
+ .pipe(
+ Effect.flatMap((projection) => {
+ const run = projection.runs.find((candidate) => candidate.id === command.runId);
+ return run === undefined
+ ? Effect.void
+ : assertProviderInstanceEnabled({
+ projectId: projection.thread.projectId,
+ commandId: command.commandId,
+ instanceId: run.providerInstanceId,
+ });
+ }),
+ );
default:
return Effect.void;
}