@devries-cameron20 / t3code-1 / commits / 39bd164d94
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): stop superseded provider startups (#15784)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Adamulek123 committed Oct 20, 2026 at 02:27 UTC 39bd164d94262720962f50c93d4638ffedd06cc4
2 files changed +122
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+120
@@ -3,6 +3,7 @@ import { expect, it, vi } from "vite-plus/test";
3 import { it as effectIt } from "@effect/vitest";
4 import {
5 CheckpointScopeId,
6 + ContextTransferId,
7 MessageId,
8 NodeId,
9 ProviderSessionId,
@@ -828,6 +829,125 @@ effectIt.effect("reads current startup state after waiting for the workspace lea
829 }),
830 );
831
832 +effectIt.effect.each([
833 + { boundary: "policy resolution", nativeMode: "ensure", loss: "replaced" },
834 + { boundary: "policy resolution", nativeMode: "ensure", loss: "interrupted" },
835 + { boundary: "session opening", nativeMode: "ensure", loss: "replaced" },
836 + { boundary: "session opening", nativeMode: "ensure", loss: "interrupted" },
837 + { boundary: "session opening", nativeMode: "resume", loss: "replaced" },
838 + { boundary: "session opening", nativeMode: "fork", loss: "replaced" },
839 +])(
840 + "does not continue $nativeMode startup when $loss during $boundary",
841 + ({ boundary, nativeMode, loss }) =>
842 + Effect.gen(function* () {
843 + const entered = yield* Deferred.make<void>();
844 + const release = yield* Deferred.make<void>();
845 + const gate = Deferred.succeed(entered, undefined).pipe(
846 + Effect.andThen(Deferred.await(release)),
847 + );
848 + const harness = makeLocalCommandHarness({ text: "Continue" });
849 + const projection = harness.projection();
850 + const run = projection.runs[0]!;
851 + const providerThread = projection.providerThreads[0]!;
852 + if (nativeMode === "resume") {
853 + harness.setProjection({
854 + ...projection,
855 + providerThreads: [
856 + {
857 + ...providerThread,
858 + nativeThreadRef: {
859 + driver: providerThread.driver,
860 + nativeId: "existing",
861 + strength: "strong",
862 + },
863 + },
864 + ],
865 + });
866 + } else if (nativeMode === "fork") {
867 + const sourceThreadId = ThreadId.make("fork-source");
868 + harness.getThreadRecords.mockReturnValue(
869 + Effect.succeed({
870 + ...projection,
871 + thread: { ...projection.thread, id: sourceThreadId },
872 + }),
873 + );
874 + harness.setProjection({
875 + ...projection,
876 + contextTransfers: [
877 + {
878 + id: ContextTransferId.make("pending-fork"),
879 + type: "fork",
880 + sourceThreadId,
881 + targetThreadId: projection.thread.id,
882 + sourcePoint: { threadId: sourceThreadId, runId: run.id },
883 + basePoint: null,
884 + sourceProviderInstanceId: run.providerInstanceId,
885 + targetProviderInstanceId: run.providerInstanceId,
886 + targetRunId: run.id,
887 + status: "pending",
888 + resolution: null,
889 + createdBy: "user",
890 + error: null,
891 + createdAt: projection.updatedAt,
892 + updatedAt: projection.updatedAt,
893 + consumedAt: null,
894 + },
895 + ],
896 + });
897 + }
898 + const ensureThread = vi.fn(() => Effect.succeed(providerThread));
899 + const resumeThread = vi.fn(() => Effect.succeed(providerThread));
900 + const forkThread = vi.fn(() => Effect.succeed(providerThread));
901 + harness.open.mockReturnValue(
902 + (boundary === "session opening" ? gate : Effect.void).pipe(
903 + Effect.as({
904 + driver: providerThread.driver,
905 + ensureThread,
906 + resumeThread,
907 + forkThread,
908 + } as never),
909 + ),
910 + );
911 + if (boundary === "policy resolution") {
912 + harness.resolveRuntimePolicy.mockReturnValue(
913 + gate.pipe(Effect.as({ cwd: "/tmp/native-account-command" } as never)),
914 + );
915 + }
916 +
917 + const startup = yield* harness.start.pipe(Effect.forkChild({ startImmediately: true }));
918 + yield* Deferred.await(entered);
919 + const current = harness.projection();
920 + const replacementId = RunAttemptId.make("replacement-attempt");
921 + const updated = {
922 + ...current,
923 + runs: current.runs.map((candidate) =>
924 + loss === "replaced"
925 + ? { ...candidate, activeAttemptId: replacementId }
926 + : { ...candidate, status: "interrupted" as const, completedAt: current.updatedAt },
927 + ),
928 + attempts:
929 + loss === "replaced"
930 + ? [
931 + ...current.attempts,
932 + { ...current.attempts[0]!, id: replacementId, attemptOrdinal: 2 },
933 + ]
934 + : current.attempts,
935 + };
936 + harness.setProjection(updated);
937 + yield* Deferred.succeed(release, undefined);
938 + yield* Fiber.join(startup);
939 +
940 + expect(harness.open).toHaveBeenCalledTimes(boundary === "session opening" ? 1 : 0);
941 + expect(ensureThread).not.toHaveBeenCalled();
942 + expect(resumeThread).not.toHaveBeenCalled();
943 + expect(forkThread).not.toHaveBeenCalled();
944 + expect(harness.startRootRun).not.toHaveBeenCalled();
945 + expect(harness.writeIfRunCurrent).not.toHaveBeenCalled();
946 + expect(harness.events).toEqual([]);
947 + expect(harness.projection()).toEqual(updated);
948 + }),
949 +);
950 +
951 effectIt.effect("starts another provider in the same checkout while a turn is pending", () =>
952 Effect.gen(function* () {
953 const worktreePath = "/tmp/provider-start-shared-checkout";
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+2
@@ -517,6 +517,7 @@ export const layer: Layer.Layer<
517 thread: projection.thread,
518 modelSelection: run.modelSelection,
519 });
520 + if (!(yield* isCurrentAttemptInStatus("starting"))) return;
521 const existingSessionProjection = projection.providerSessions.find(
522 (candidate) => candidate.id === providerSessionId,
523 );
@@ -579,6 +580,7 @@ export const layer: Layer.Layer<
580 return;
581 }
582 const session = sessionResult.success;
583 + if (!(yield* isCurrentAttemptInStatus("starting"))) return;
584 const loadFromProvider = (
585 load: Effect.Effect<OrchestrationV2ProviderThread, ProviderAdapter.ProviderAdapterV2Error>,
586 ) =>