@devries-cameron20 / t3code-1 / commits / 9ce9b1fd41
Fork 0

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

fix(server): provider launches and text generation wait for provider updates (#31186)

Julius Marminge committed Nov 11, 2026 at 15:26 UTC 9ce9b1fd419538fa32cd251e9e490a360877259a
16 files changed +545 -46
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+98
@@ -106,6 +106,7 @@ import type * as ProviderContinuationRequests from "@t3tools/provider-core/serve
106 import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
107 import * as ClaudeAdapterV2 from "./ClaudeAdapterV2.ts";
108 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
109 +import * as ProviderInstallationGate from "../../provider/ProviderInstallationGate.ts";
110 import { ClaudeOrchestratorReplayHarness } from "./ClaudeAdapterV2.testkit.ts";
111 import { provideDeterministicTestRuntime } from "../testkit/DeterministicRuntime.ts";
112 import { runOrchestratorV2Scenario } from "../testkit/OrchestratorScenario.ts";
@@ -1301,6 +1302,102 @@ describe("ClaudeAdapterV2 append-system-prompt-file launch argument", () => {
1302 );
1303 });
1304
1305 +describe("ClaudeAdapterV2 provider installation gate", () => {
1306 + it.effect("opens each turn's Claude process inside the installation gate", () =>
1307 + Effect.scoped(
1308 + Effect.gen(function* () {
1309 + const fileSystem = yield* FileSystem.FileSystem;
1310 + const log: Array<string> = [];
1311 + const instanceId = ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID;
1312 + const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
1313 + instanceId,
1314 + settings: DEFAULT_CLAUDE_SETTINGS,
1315 + environment: {},
1316 + attachmentsDir: yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-gate-" }),
1317 + fileSystem,
1318 + path: yield* Path.Path,
1319 + random: yield* SecureRandom.SecureRandom,
1320 + idAllocator: yield* IdAllocator.IdAllocatorV2,
1321 + installationGate: ProviderInstallationGate.ProviderInstallationGate.of({
1322 + execute: (gatedInstanceId, effect) =>
1323 + Effect.sync(() => log.push(`enter ${gatedInstanceId}`)).pipe(
1324 + Effect.andThen(effect),
1325 + Effect.ensuring(Effect.sync(() => log.push("exit"))),
1326 + ),
1327 + update: () => Effect.die("unused"),
1328 + }),
1329 + queryRunner: {
1330 + allocateSessionId: Effect.succeed("native-thread-claude-gate"),
1331 + open: () =>
1332 + Effect.sync(() => {
1333 + log.push("open query");
1334 + return {
1335 + messages: Stream.never,
1336 + offer: () => Effect.void,
1337 + setModel: () => Effect.void,
1338 + applyFlagSettings: () => Effect.void,
1339 + setPermissionMode: () => Effect.void,
1340 + backgroundTasks: () => Effect.succeed(false),
1341 + mcpServerStatus: Effect.succeed([]),
1342 + readMcpResource: () => Effect.die("unexpected MCP resource read"),
1343 + callMcpTool: () => Effect.die("unexpected MCP tool call"),
1344 + listPermissionRules: Effect.die("unexpected permission rules read"),
1345 + stopTask: () => Effect.void,
1346 + interrupt: Effect.void,
1347 + close: Effect.void,
1348 + };
1349 + }),
1350 + forkSession: () => Effect.die("unused"),
1351 + subagentLaunchToolUseId: () => Effect.succeed(null),
1352 + assertComplete: Effect.void,
1353 + },
1354 + });
1355 + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1356 + runtimeMode: "full-access",
1357 + interactionMode: "default",
1358 + cwd: yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-gate-cwd-" }),
1359 + });
1360 + const threadId = ThreadId.make("thread-claude-gate");
1361 + const runtime = yield* adapter.openSession({
1362 + threadId,
1363 + providerSessionId: ProviderSessionId.make("provider-session-claude-gate"),
1364 + modelSelection: CLAUDE_TEST_MODEL_SELECTION,
1365 + runtimePolicy,
1366 + });
1367 + const providerThread = yield* runtime.ensureThread({
1368 + threadId,
1369 + modelSelection: CLAUDE_TEST_MODEL_SELECTION,
1370 + runtimePolicy,
1371 + });
1372 + assert.deepStrictEqual(log, []);
1373 +
1374 + yield* runtime.startTurn(
1375 + makeClaudeTestTurnInput({
1376 + threadId,
1377 + providerThread,
1378 + now: yield* DateTime.now,
1379 + attemptId: RunAttemptId.make("attempt-claude-gate"),
1380 + text: "Hello",
1381 + attachments: [],
1382 + runtimePolicy,
1383 + }),
1384 + );
1385 +
1386 + assert.deepStrictEqual(log, [`enter ${instanceId}`, "open query", "exit"]);
1387 + }).pipe(
1388 + Effect.provide(
1389 + Layer.mergeAll(
1390 + IdAllocator.layer,
1391 + McpProviderSessions.layer,
1392 + NodeServices.layer,
1393 + NodeCrypto.layer,
1394 + ),
1395 + ),
1396 + ),
1397 + ),
1398 + );
1399 +});
1400 +
1401 describe("ClaudeAdapterV2 approval cancellation", () => {
1402 it.effect("observes an approval signal that was already aborted", () =>
1403 Effect.gen(function* () {
@@ -1486,6 +1583,7 @@ describe("ClaudeAdapterV2 executable path", () => {
1583 Layer.mergeAll(
1584 IdAllocator.layer,
1585 McpProviderSessions.layer,
1586 + ProviderInstallationGate.layer,
1587 NodeServices.layer,
1588 NodeCrypto.layer,
1589 ),
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts
+2
@@ -37,6 +37,7 @@ import {
37 import * as ClaudeAdapterV2 from "./ClaudeAdapterV2.ts";
38 import { claudeTaskOutputRoot } from "./ClaudeTaskOutputTail.ts";
39 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
40 +import * as ProviderInstallationGate from "../../provider/ProviderInstallationGate.ts";
41 import { ProviderAdapterDriverCreateError } from "@t3tools/provider-core/server/adapterDriver";
42 import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
43 import { randomUuidV4 } from "@t3tools/provider-core/server/randomUuid";
@@ -1054,6 +1055,7 @@ function layerClaudeProviderAdapterRegistryReplay(
1055 Layer.mergeAll(
1056 layerClaudeAgentSdkReplay(transcript, options),
1057 IdAllocator.layer,
1058 + ProviderInstallationGate.layer,
1059 NodeServices.layer,
1060 NodeCrypto.layer,
1061 layerServerConfig,
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+27 -18
@@ -161,6 +161,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
161 import { resolveAttachmentPath } from "../../attachmentStore.ts";
162 import { snapshotMcpApp } from "../../mcpApps/McpAppSnapshot.ts";
163 import { resolveClaudeSdkExecutablePath } from "../../provider/Drivers/ClaudeExecutable.ts";
164 +import * as ProviderInstallationGate from "../../provider/ProviderInstallationGate.ts";
165 import {
166 planClaudeSkillDispatch,
167 type ClaudeSkillTarget,
@@ -4953,6 +4954,7 @@ export interface ClaudeAdapterV2Options {
4954 request: ProviderContinuationRequests.ProviderContinuationRequest,
4955 ) => Effect.Effect<void>;
4956 };
4957 + readonly installationGate?: ProviderInstallationGate.ProviderInstallationGate["Service"];
4958 }
4959
4960 export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
@@ -11374,24 +11376,28 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
11376 const resolvedTurnCwd = yield* fileSystem
11377 .realPath(turnInput.runtimePolicy.cwd ?? ".")
11378 .pipe(Effect.orElseSucceed(() => turnInput.runtimePolicy.cwd));
11377 - const querySession = yield* queryRunner
11378 - .open({
11379 - threadId: turnInput.threadId,
11380 - providerSessionId: input.providerSessionId,
11381 - options: queryOptions,
11382 - })
11383 - .pipe(
11384 - Effect.onError(() =>
11385 - closedExistingNativeThreadId === nativeThreadId
11386 - ? Effect.gen(function* () {
11387 - yield* clearWakeStateForNativeThread(nativeThreadId);
11388 - yield* resetBackgroundTaskStateForNativeThreadProcess(nativeThreadId, {
11389 - status: "idle",
11390 - });
11391 - })
11392 - : Effect.void,
11393 - ),
11394 - );
11379 + const openQueryRuntime = queryRunner.open({
11380 + threadId: turnInput.threadId,
11381 + providerSessionId: input.providerSessionId,
11382 + options: queryOptions,
11383 + });
11384 + // The SDK spawns the CLI per turn, so only the open waits for a provider update.
11385 + const querySession = yield* (
11386 + adapterOptions.installationGate === undefined
11387 + ? openQueryRuntime
11388 + : adapterOptions.installationGate.execute(adapterOptions.instanceId, openQueryRuntime)
11389 + ).pipe(
11390 + Effect.onError(() =>
11391 + closedExistingNativeThreadId === nativeThreadId
11392 + ? Effect.gen(function* () {
11393 + yield* clearWakeStateForNativeThread(nativeThreadId);
11394 + yield* resetBackgroundTaskStateForNativeThreadProcess(nativeThreadId, {
11395 + status: "idle",
11396 + });
11397 + })
11398 + : Effect.void,
11399 + ),
11400 + );
11401 // Mark only successful opens; retries must not resume sessions that failed to create.
11402 yield* Ref.update(openedNativeThreads, (current) => {
11403 if (current.has(nativeThreadId)) {
@@ -12852,6 +12858,7 @@ export type ClaudeAdapterV2DriverEnv =
12858 | IdAllocator.IdAllocatorV2
12859 | McpProviderSessions.McpProviderSessions
12860 | Path.Path
12861 + | ProviderInstallationGate.ProviderInstallationGate
12862 | ServerConfig.ServerConfig;
12863
12864 export const createClaudeAdapterV2 = Effect.fn("ClaudeAdapterV2Driver.create")(
@@ -12866,6 +12873,7 @@ export const createClaudeAdapterV2 = Effect.fn("ClaudeAdapterV2Driver.create")(
12873 const queryRunner = yield* ClaudeAgentSdkQueryRunner;
12874 const serverConfig = yield* ServerConfig.ServerConfig;
12875 const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
12876 + const installationGate = yield* ProviderInstallationGate.ProviderInstallationGate;
12877 const baseEnvironment = yield* mergeClaudeProcessEnvironment(
12878 config,
12879 environment,
@@ -12889,6 +12897,7 @@ export const createClaudeAdapterV2 = Effect.fn("ClaudeAdapterV2Driver.create")(
12897 idAllocator,
12898 queryRunner,
12899 continuationRequests,
12900 + installationGate,
12901 ...hooks,
12902 });
12903 },
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+50
@@ -52,6 +52,7 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
52 import * as ProjectionStore from "./ProjectionStore.ts";
53 import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
54 import * as ProviderEventIngestor from "./ProviderEventIngestor.ts";
55 +import * as ProviderInstallationGate from "../provider/ProviderInstallationGate.ts";
56 import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
57 import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
58 import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
@@ -617,6 +618,7 @@ function layerTest(input: {
618 readonly scopeCloseReached?: Deferred.Deferred<void>;
619 readonly serverSettingsLayer?: ReturnType<typeof ServerSettings.layerTest>;
620 readonly projectServiceLayer?: Layer.Layer<ProjectService.ProjectService>;
621 + readonly installationGateLayer?: Layer.Layer<ProviderInstallationGate.ProviderInstallationGate>;
622 }) {
623 const layerConfiguredEventSink =
624 input.flakyReleaseWrites !== undefined
@@ -702,6 +704,7 @@ function layerTest(input: {
704 layerProviderEventIngestorTest,
705 layerConfiguredMcpRegistry,
706 layerTestStores,
707 + input.installationGateLayer ?? ProviderInstallationGate.layer,
708 ...(input.serverSettingsLayer === undefined ? [] : [input.serverSettingsLayer]),
709 ...(input.projectServiceLayer === undefined ? [] : [input.projectServiceLayer]),
710 ),
@@ -1104,6 +1107,53 @@ it.effect("ProviderSessionManagerV2 closes every live session for a provider ins
1107 }),
1108 );
1109
1110 +it.effect("ProviderSessionManagerV2 opens provider sessions inside the installation gate", () =>
1111 + Effect.gen(function* () {
1112 + const state = yield* Ref.make(emptyState);
1113 + const log: Array<string> = [];
1114 + const effect = Effect.gen(function* () {
1115 + const eventSink = yield* EventSink.EventSinkV2;
1116 + const idAllocator = yield* IdAllocator.IdAllocatorV2;
1117 + const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
1118 + const threadId = ThreadId.make("thread-provider-session-manager-installation-gate");
1119 + const providerSessionId = yield* idAllocator.allocate.providerSession({
1120 + providerInstanceId: modelSelection.instanceId,
1121 + threadId,
1122 + });
1123 + yield* eventSink.write({
1124 + events: [
1125 + yield* makeThreadCreatedEvent({ idAllocator, threadId, now: yield* DateTime.now }),
1126 + ],
1127 + });
1128 +
1129 + yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
1130 +
1131 + assert.deepStrictEqual(log, [`enter ${modelSelection.instanceId}`, "openSession", "exit"]);
1132 + });
1133 +
1134 + yield* effect.pipe(
1135 + Effect.provide(
1136 + layerTest({
1137 + state,
1138 + idleTimeoutMs: 60_000,
1139 + beforeOpen: () => Effect.sync(() => log.push("openSession")),
1140 + installationGateLayer: Layer.succeed(
1141 + ProviderInstallationGate.ProviderInstallationGate,
1142 + ProviderInstallationGate.ProviderInstallationGate.of({
1143 + execute: (instanceId, gated) =>
1144 + Effect.sync(() => log.push(`enter ${instanceId}`)).pipe(
1145 + Effect.andThen(gated),
1146 + Effect.ensuring(Effect.sync(() => log.push("exit"))),
1147 + ),
1148 + update: () => Effect.die("unused"),
1149 + }),
1150 + ),
1151 + }),
1152 + ),
1153 + );
1154 + }),
1155 +);
1156 +
1157 it.effect("ProviderSessionManagerV2 opens a duplicate session only once", () =>
1158 Effect.gen(function* () {
1159 const state = yield* Ref.make(emptyState);
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+26 -20
@@ -45,6 +45,7 @@ import {
45 withMetrics,
46 } from "../observability/Metrics.ts";
47 import { ProviderWorkspaceMissingError } from "../provider/Errors.ts";
48 +import * as ProviderInstallationGate from "../provider/ProviderInstallationGate.ts";
49 import * as ProjectService from "../project/ProjectService.ts";
50 import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
51 import * as ServerSettings from "../serverSettings.ts";
@@ -362,6 +363,7 @@ export const layerWithOptions = (
363 | ProjectionStore.ProjectionStoreV2
364 | ProviderEventIngestor.ProviderEventIngestorV2
365 | ProviderAdapterRegistry.ProviderAdapterRegistryV2
366 + | ProviderInstallationGate.ProviderInstallationGate
367 > =>
368 Layer.effect(
369 ProviderSessionManagerV2,
@@ -382,6 +384,7 @@ export const layerWithOptions = (
384 const providerEventIngestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
385 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
386 const agentScope = yield* AgentScope;
387 + const installationGate = yield* ProviderInstallationGate.ProviderInstallationGate;
388 // Labs flag, read live: Codex per turn (its shared session outlives a toggle), Claude per query.
389 // Unreadable settings keep it off.
390 const promptSuggestionsEnabled = Option.isNone(serverSettings)
@@ -2668,26 +2671,29 @@ export const layerWithOptions = (
2671 });
2672 const sessionScope = yield* Scope.fork(sessionScopes);
2673 yield* agentScope.clear(input.threadId);
2671 - const runtime = yield* adapter
2672 - .openSession({
2673 - threadId: input.threadId,
2674 - providerSessionId: input.providerSessionId,
2675 - modelSelection: input.modelSelection,
2676 - runtimePolicy: input.runtimePolicy,
2677 - promptSuggestions: promptSuggestionsEnabled,
2678 - ...(input.resumeFromSession === undefined
2679 - ? {}
2680 - : { resumeFromSession: input.resumeFromSession }),
2681 - ...(input.initialNativeThreadId === undefined
2682 - ? {}
2683 - : { initialNativeThreadId: input.initialNativeThreadId }),
2684 - ...(input.initialProviderItemIdentityVersion === undefined
2685 - ? {}
2686 - : {
2687 - initialProviderItemIdentityVersion:
2688 - input.initialProviderItemIdentityVersion,
2689 - }),
2690 - })
2674 + const runtime = yield* installationGate
2675 + .execute(
2676 + input.modelSelection.instanceId,
2677 + adapter.openSession({
2678 + threadId: input.threadId,
2679 + providerSessionId: input.providerSessionId,
2680 + modelSelection: input.modelSelection,
2681 + runtimePolicy: input.runtimePolicy,
2682 + promptSuggestions: promptSuggestionsEnabled,
2683 + ...(input.resumeFromSession === undefined
2684 + ? {}
2685 + : { resumeFromSession: input.resumeFromSession }),
2686 + ...(input.initialNativeThreadId === undefined
2687 + ? {}
2688 + : { initialNativeThreadId: input.initialNativeThreadId }),
2689 + ...(input.initialProviderItemIdentityVersion === undefined
2690 + ? {}
2691 + : {
2692 + initialProviderItemIdentityVersion:
2693 + input.initialProviderItemIdentityVersion,
2694 + }),
2695 + }),
2696 + )
2697 .pipe(
2698 Effect.provideService(Scope.Scope, sessionScope),
2699 // Bound scope close after cleanup; a wedged finalizer must not hold the open lock or interrupter.
apps/server/src/orchestration-v2/runtimeLayer.ts
+7 -1
@@ -12,6 +12,7 @@ import * as McpAppRequests from "../mcpApps/McpAppRequests.ts";
12 import * as ProviderSessionRuntime from "../persistence/ProviderSessionRuntime.ts";
13 import * as TextGeneration from "../textGeneration/TextGeneration.ts";
14 import * as ProviderAuthService from "../provider/ProviderAuthService.ts";
15 +import * as ProviderInstallationGate from "../provider/ProviderInstallationGate.ts";
16 import * as AgentSessionImporter from "../project/AgentSessionImporter.ts";
17 import * as AgentSessionScanner from "../project/AgentSessionScanner.ts";
18 import * as ProjectService from "../project/ProjectService.ts";
@@ -140,6 +141,7 @@ const layerProviderSessionManagerProvided = ProviderSessionManager.layer.pipe(
141 IdAllocator.layer,
142 layerProviderEventIngestorProvided,
143 ProjectionStore.layer,
144 + ProviderInstallationGate.layer,
145 ),
146 ),
147 );
@@ -319,7 +321,11 @@ const layerProviderContinuationWorkerProvided = ProviderContinuationService.laye
321 );
322 const layerThreadTitleRegenerationProvided = ThreadTitleRegenerationService.layer.pipe(
323 Layer.provide(
322 - Layer.mergeAll(layerThreadManagementProvided, ProjectStore.layer, TextGeneration.layer),
324 + Layer.mergeAll(
325 + layerThreadManagementProvided,
326 + ProjectStore.layer,
327 + TextGeneration.layer.pipe(Layer.provide(ProviderInstallationGate.layer)),
328 + ),
329 ),
330 );
331 const layerEffectExecutorProvided = EffectWorker.layerExecutor.pipe(
apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts
+2
@@ -47,6 +47,7 @@ import * as ProviderAuthService from "../../provider/ProviderAuthService.ts";
47 import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
48 import * as ProviderContinuationService from "../ProviderContinuationService.ts";
49 import * as ProviderEventIngestor from "../ProviderEventIngestor.ts";
50 +import * as ProviderInstallationGate from "../../provider/ProviderInstallationGate.ts";
51 import * as ProviderInstanceRegistry from "../../provider/ProviderInstanceRegistry.ts";
52 import * as ProviderRuntimeRecoveryService from "../ProviderRuntimeRecoveryService.ts";
53 import * as ProviderSessionManager from "../ProviderSessionManager.ts";
@@ -408,6 +409,7 @@ export function layerWithRegistry<Error>(
409 McpSessionRegistryTestkit.layer,
410 layerProviderEventIngestorProvided,
411 layerStores,
412 + ProviderInstallationGate.layer,
413 ),
414 ),
415 );
apps/server/src/provider/ProviderInstallationGate.test.ts new
+49
@@ -0,0 +1,49 @@
1 +import { ProviderInstanceId } from "@t3tools/contracts";
2 +import { assert, it } from "@effect/vitest";
3 +import * as Deferred from "effect/Deferred";
4 +import * as Effect from "effect/Effect";
5 +import * as Fiber from "effect/Fiber";
6 +
7 +import * as ProviderInstallationGate from "./ProviderInstallationGate.ts";
8 +
9 +const INSTANCE_ID = ProviderInstanceId.make("codex");
10 +
11 +it.effect("queues work that starts while an update waits for in-flight work", () =>
12 + Effect.gen(function* () {
13 + const gate = yield* ProviderInstallationGate.ProviderInstallationGate;
14 + const inFlightStarted = yield* Deferred.make<void>();
15 + const releaseInFlight = yield* Deferred.make<void>();
16 + const log: Array<string> = [];
17 +
18 + const inFlight = yield* gate
19 + .execute(
20 + INSTANCE_ID,
21 + Deferred.succeed(inFlightStarted, undefined).pipe(
22 + Effect.andThen(Deferred.await(releaseInFlight)),
23 + Effect.andThen(Effect.sync(() => log.push("in-flight work finished"))),
24 + ),
25 + )
26 + .pipe(Effect.forkScoped);
27 + yield* Deferred.await(inFlightStarted);
28 + // Starting immediately runs each fiber until it blocks: the update while draining in-flight
29 + // work, by which point it holds the turnstile, and later work at the gate.
30 + const update = yield* gate
31 + .update(
32 + [INSTANCE_ID],
33 + Effect.sync(() => log.push("update ran")),
34 + )
35 + .pipe(Effect.forkScoped({ startImmediately: true }));
36 + const later = yield* gate
37 + .execute(
38 + INSTANCE_ID,
39 + Effect.sync(() => log.push("later work ran")),
40 + )
41 + .pipe(Effect.forkScoped({ startImmediately: true }));
42 + yield* Deferred.succeed(releaseInFlight, undefined);
43 + yield* Fiber.join(inFlight);
44 + yield* Fiber.join(update);
45 + yield* Fiber.join(later);
46 +
47 + assert.deepStrictEqual(log, ["in-flight work finished", "update ran", "later work ran"]);
48 + }).pipe(Effect.provide(ProviderInstallationGate.layer)),
49 +);
apps/server/src/provider/ProviderInstallationGate.ts new
+73
@@ -0,0 +1,73 @@
1 +import type { ProviderInstanceId } from "@t3tools/contracts";
2 +import * as Context from "effect/Context";
3 +import * as Effect from "effect/Effect";
4 +import * as Layer from "effect/Layer";
5 +import * as Semaphore from "effect/Semaphore";
6 +
7 +// Executions share `room` and an update drains it. A Semaphore lets a smaller request overtake a
8 +// waiting larger one, so an update first holds `turnstile` and new executions queue behind it.
9 +interface InstanceGate {
10 + readonly turnstile: Semaphore.Semaphore;
11 + readonly room: Semaphore.Semaphore;
12 +}
13 +
14 +const ROOM_PERMITS = Number.MAX_SAFE_INTEGER;
15 +
16 +export class ProviderInstallationGate extends Context.Service<
17 + ProviderInstallationGate,
18 + {
19 + /** Runs provider work once no update of the instance's installation is running or waiting. */
20 + readonly execute: <A, E, R>(
21 + instanceId: ProviderInstanceId,
22 + effect: Effect.Effect<A, E, R>,
23 + ) => Effect.Effect<A, E, R>;
24 + /** Runs an installation update after the instances' in-flight work, excluding new work. */
25 + readonly update: <A, E, R>(
26 + instanceIds: ReadonlyArray<ProviderInstanceId>,
27 + effect: Effect.Effect<A, E, R>,
28 + ) => Effect.Effect<A, E, R>;
29 + }
30 +>()("t3/provider/ProviderInstallationGate") {}
31 +
32 +export const layer = Layer.sync(ProviderInstallationGate, () => {
33 + // Instances are few and configured, so their gates are kept for the server's lifetime.
34 + const gates = new Map<ProviderInstanceId, InstanceGate>();
35 + const gateFor = (instanceId: ProviderInstanceId) =>
36 + Effect.sync(() => {
37 + const existing = gates.get(instanceId);
38 + if (existing) return existing;
39 + const gate = {
40 + turnstile: Semaphore.makeUnsafe(1),
41 + room: Semaphore.makeUnsafe(ROOM_PERMITS),
42 + };
43 + gates.set(instanceId, gate);
44 + return gate;
45 + });
46 +
47 + const execute = Effect.fn("ProviderInstallationGate.execute")(function* <A, E, R>(
48 + instanceId: ProviderInstanceId,
49 + effect: Effect.Effect<A, E, R>,
50 + ) {
51 + const gate = yield* gateFor(instanceId);
52 + yield* gate.turnstile.withPermits(1)(Effect.void);
53 + return yield* gate.room.withPermits(1)(effect);
54 + });
55 +
56 + const update = Effect.fn("ProviderInstallationGate.update")(function* <A, E, R>(
57 + instanceIds: ReadonlyArray<ProviderInstanceId>,
58 + effect: Effect.Effect<A, E, R>,
59 + ) {
60 + // A stable order keeps two updates that share instances from deadlocking.
61 + const instanceGates = yield* Effect.forEach(Array.from(new Set(instanceIds)).sort(), gateFor);
62 + const drained = instanceGates.reduceRight(
63 + (inner, gate) => gate.room.withPermits(ROOM_PERMITS)(inner),
64 + effect,
65 + );
66 + return yield* instanceGates.reduceRight(
67 + (inner, gate) => gate.turnstile.withPermits(1)(inner),
68 + drained,
69 + );
70 + });
71 +
72 + return ProviderInstallationGate.of({ execute, update });
73 +});
apps/server/src/provider/ProviderInstanceRegistry.test.ts
+2
@@ -48,6 +48,7 @@ import * as ResetCreditCoordinator from "./resetCreditCoordinator.ts";
48 import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
49 import { makeProviderInstanceRegistry } from "./ProviderInstanceRegistry.ts";
50 import { ProviderDriverError } from "./Errors.ts";
51 +import * as ProviderInstallationGate from "./ProviderInstallationGate.ts";
52 import * as ProviderOrchestrationAdapterInfrastructure from "./ProviderOrchestrationAdapterInfrastructure.ts";
53 import * as PortScanner from "../preview/PortScanner.ts";
54 import * as ProviderHostLive from "./ProviderHostLive.ts";
@@ -204,6 +205,7 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => {
205 ),
206 Layer.provideMerge(ProviderLatestVersions.layer),
207 Layer.provideMerge(McpProviderSessions.layer),
208 + Layer.provideMerge(ProviderInstallationGate.layer),
209 Layer.provideMerge(ModelManifest.layerTest),
210 Layer.provideMerge(ResetCreditCoordinator.layerTest),
211 );
apps/server/src/provider/ProviderRegistry.test.ts
+10 -3
@@ -57,6 +57,7 @@ import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderS
57 import * as OpenCodeRuntime from "@t3tools/provider-opencode/server/OpenCodeRuntime";
58 import * as OpenCodeServerLedger from "@t3tools/provider-opencode/server/OpenCodeServerLedger";
59 import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
60 +import * as ProviderInstallationGate from "./ProviderInstallationGate.ts";
61 import * as ProviderInstanceRegistryHydration from "./ProviderInstanceRegistryHydration.ts";
62 import * as PortScanner from "../preview/PortScanner.ts";
63 import * as ServerConfig from "../config.ts";
@@ -2881,7 +2882,9 @@ it.layer(
2882 yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
2883 const layerProviderRegistry = ProviderRegistry.layer.pipe(
2884 Layer.provideMerge(
2884 - ProviderInstanceRegistryHydration.layer.pipe(Layer.provide(PortScanner.layerTest)),
2885 + ProviderInstanceRegistryHydration.layer.pipe(
2886 + Layer.provide(Layer.mergeAll(PortScanner.layerTest, ProviderInstallationGate.layer)),
2887 + ),
2888 ),
2889 Layer.provideMerge(AntigravityInstallation.AntigravityInstallation.layer),
2890 Layer.provideMerge(
@@ -2999,7 +3002,9 @@ it.layer(
3002 yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
3003 const layerProviderRegistry = ProviderRegistry.layer.pipe(
3004 Layer.provideMerge(
3002 - ProviderInstanceRegistryHydration.layer.pipe(Layer.provide(PortScanner.layerTest)),
3005 + ProviderInstanceRegistryHydration.layer.pipe(
3006 + Layer.provide(Layer.mergeAll(PortScanner.layerTest, ProviderInstallationGate.layer)),
3007 + ),
3008 ),
3009 Layer.provideMerge(AntigravityInstallation.AntigravityInstallation.layer),
3010 Layer.provideMerge(
@@ -3134,7 +3139,9 @@ it.layer(
3139 );
3140 readSeed = false;
3141 const services = yield* Layer.build(
3137 - ProviderInstanceRegistryHydration.layer.pipe(Layer.provide(PortScanner.layerTest)),
3142 + ProviderInstanceRegistryHydration.layer.pipe(
3143 + Layer.provide(Layer.mergeAll(PortScanner.layerTest, ProviderInstallationGate.layer)),
3144 + ),
3145 ).pipe(Effect.provide(dependencies));
3146 failCurrentRead = input.failCurrentRead ?? false;
3147 yield* PubSub.publish(changes, undefined);
apps/server/src/provider/providerMaintenanceRunner.test.ts
+149
@@ -23,6 +23,14 @@ import { ChildProcessSpawner } from "effect/process";
23 import * as HostProcess from "@t3tools/shared/HostProcess";
24 import { SpawnExecutableResolution } from "@t3tools/shared/shell";
25
26 +import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
27 +
28 +import * as ServerSettings from "../serverSettings.ts";
29 +import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
30 +import * as TextGeneration from "../textGeneration/TextGeneration.ts";
31 +import * as TextGenerators from "../textGeneration/TextGenerators.ts";
32 +import * as ProviderInstallationGate from "./ProviderInstallationGate.ts";
33 +import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
34 import * as ProviderRegistry from "./ProviderRegistry.ts";
35 import * as ModelManifest from "./ModelManifest.ts";
36 import * as ProviderMaintenanceRunner from "./providerMaintenanceRunner.ts";
@@ -255,6 +263,8 @@ const makeTestRunner = (
263 refreshInBackground: Effect.void,
264 }),
265 latestVersions,
266 + // Layers memoize by reference, so a test that provides the gate shares it.
267 + ProviderInstallationGate.layer,
268 ),
269 ),
270 ),
@@ -297,6 +307,7 @@ describe("providerMaintenanceRunner", () => {
307 [
308 ["queued", "Waiting for another provider update to finish."],
309 ["running", "Checking for the latest version"],
310 + ["running", "Waiting for running provider work to finish."],
311 ["running", "Running native-agent update"],
312 ["running", "Verifying the installed version"],
313 ["succeeded", "Provider updated."],
@@ -1000,6 +1011,144 @@ describe("providerMaintenanceRunner", () => {
1011 );
1012 });
1013
1014 + it.effect("holds text generation for an instance until its update finishes", () => {
1015 + const updateStarted = Deferred.makeUnsafe<void>();
1016 + const log: Array<string> = [];
1017 + return Effect.gen(function* () {
1018 + const { registry } = yield* makeRegistry(baseProvider, {
1019 + installedVersionAfterUpdate: "1.0.0",
1020 + });
1021 + const updater = yield* makeTestRunner(registry);
1022 + const instance: ProviderInstance = {
1023 + instanceId: CODEX_INSTANCE_ID,
1024 + driverKind: CODEX_DRIVER,
1025 + continuationIdentity: { driverKind: CODEX_DRIVER, continuationKey: CODEX_INSTANCE_ID },
1026 + displayName: undefined,
1027 + enabled: true,
1028 + snapshot: { getSnapshot: Effect.succeed(baseProvider) } as ProviderInstance["snapshot"],
1029 + orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
1030 + textGeneration: TextGeneration.TextGeneration.of({
1031 + generateCommitMessage: () => Effect.die("unused"),
1032 + generatePrContent: () => Effect.die("unused"),
1033 + generateBranchName: () => Effect.die("unused"),
1034 + generateReviewGuide: () => Effect.die("unused"),
1035 + generateTranscriptionPostProcessing: () => Effect.die("unused"),
1036 + generateThreadTitle: () =>
1037 + Effect.sync(() => {
1038 + log.push("text generation spawned provider");
1039 + return { title: "Generated title" };
1040 + }),
1041 + }),
1042 + };
1043 + const textGeneration = yield* TextGeneration.make.pipe(
1044 + Effect.provide(
1045 + Layer.mergeAll(
1046 + Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
1047 + listInstances: Effect.succeed([instance]),
1048 + getInstance: (id) => Effect.succeed(id === CODEX_INSTANCE_ID ? instance : undefined),
1049 + }),
1050 + ServerSettings.layerTest(),
1051 + Layer.mock(TextGenerators.TextGenerators)({ runner: () => Effect.undefined }),
1052 + Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
1053 + resolveLink: () => Effect.die("unused"),
1054 + }),
1055 + ),
1056 + ),
1057 + );
1058 +
1059 + const update = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
1060 + yield* Deferred.await(updateStarted);
1061 + const generation = yield* textGeneration
1062 + .generateThreadTitle({
1063 + cwd: "/tmp",
1064 + message: "hello",
1065 + linkedContext: "",
1066 + modelSelection: { instanceId: CODEX_INSTANCE_ID, model: "gpt-5" },
1067 + })
1068 + .pipe(Effect.forkScoped);
1069 + yield* TestClock.adjust("1 minute");
1070 + const result = yield* Fiber.join(update);
1071 + yield* Fiber.join(generation);
1072 +
1073 + assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
1074 + assert.deepStrictEqual(log, ["update command exited", "text generation spawned provider"]);
1075 + }).pipe(
1076 + Effect.provide(
1077 + Layer.mergeAll(
1078 + ProviderInstallationGate.layer,
1079 + layerNonWindowsPlatform,
1080 + layerLatestVersionHttpClient("1.0.0"),
1081 + layerMockSpawner(() => ({
1082 + exitCode: Deferred.succeed(updateStarted, undefined).pipe(
1083 + Effect.andThen(Effect.sleep("1 minute")),
1084 + Effect.andThen(Effect.sync(() => log.push("update command exited"))),
1085 + Effect.as(ChildProcessSpawner.ExitCode(0)),
1086 + ),
1087 + })),
1088 + ),
1089 + ),
1090 + );
1091 + });
1092 +
1093 + it.effect("holds every instance that shares the updated installation", () => {
1094 + const CODEX_WORK_INSTANCE_ID = ProviderInstanceId.make("codex_work");
1095 + const updateStarted = Deferred.makeUnsafe<void>();
1096 + const log: Array<string> = [];
1097 + return Effect.gen(function* () {
1098 + const { registry } = yield* makeRegistry(
1099 + [
1100 + baseProvider,
1101 + { ...baseProvider, instanceId: CODEX_WORK_INSTANCE_ID },
1102 + baseOpenCodeProvider,
1103 + ],
1104 + { installedVersionAfterUpdate: "1.0.0" },
1105 + );
1106 + // OpenCode installs into the same npm prefix, so it shares the lock key but not the package.
1107 + const updater = yield* makeTestRunner(registry);
1108 + const gate = yield* ProviderInstallationGate.ProviderInstallationGate;
1109 +
1110 + const update = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
1111 + yield* Deferred.await(updateStarted);
1112 + const shared = yield* gate
1113 + .execute(
1114 + CODEX_WORK_INSTANCE_ID,
1115 + Effect.sync(() => log.push("shared installation ran")),
1116 + )
1117 + .pipe(Effect.forkScoped);
1118 + const other = yield* gate
1119 + .execute(
1120 + OPENCODE_INSTANCE_ID,
1121 + Effect.sync(() => log.push("other driver ran")),
1122 + )
1123 + .pipe(Effect.forkScoped);
1124 + yield* TestClock.adjust("1 minute");
1125 + yield* Fiber.join(update);
1126 + yield* Fiber.join(shared);
1127 + yield* Fiber.join(other);
1128 +
1129 + assert.deepStrictEqual(log, [
1130 + "other driver ran",
1131 + "update command exited",
1132 + "shared installation ran",
1133 + ]);
1134 + }).pipe(
1135 + Effect.provide(
1136 + Layer.mergeAll(
1137 + ProviderInstallationGate.layer,
1138 + layerNonWindowsPlatform,
1139 + layerLatestVersionHttpClient("1.0.0"),
1140 + layerMockSpawner(() => ({
1141 + exitCode: Deferred.succeed(updateStarted, undefined).pipe(
1142 + Effect.andThen(Effect.sleep("1 minute")),
1143 + Effect.andThen(Effect.sync(() => log.push("update command exited"))),
1144 + Effect.as(ChildProcessSpawner.ExitCode(0)),
1145 + ),
1146 + })),
1147 + ),
1148 + ),
1149 + );
1150 + });
1151 +
1152 it.effect(
1153 "releases the running-provider marker when interrupted after queuing but before the lock run starts",
1154 () =>
apps/server/src/provider/providerMaintenanceRunner.ts
+36 -2
@@ -25,6 +25,7 @@ import * as NodeUtil from "node:util";
25
26 import * as ModelManifest from "./ModelManifest.ts";
27 import { resolveProviderCompatibility } from "./providerCompatibility.ts";
28 +import * as ProviderInstallationGate from "./ProviderInstallationGate.ts";
29 import * as ProviderRegistry from "./ProviderRegistry.ts";
30 import { makeProviderMaintenanceCommandCoordinator } from "./providerMaintenanceCommandCoordinator.ts";
31 import {
@@ -323,6 +324,7 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () {
324 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
325 const httpClient = yield* HttpClient.HttpClient;
326 const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions;
327 + const installationGate = yield* ProviderInstallationGate.ProviderInstallationGate;
328 const runMaintenanceCommand = (
329 update: ProviderMaintenanceCommandAction,
330 onProgress: (line: string) => Effect.Effect<void>,
@@ -334,6 +336,28 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () {
336 onProgress,
337 ...(update.env ? { env: update.env } : {}),
338 });
339 + // Instances with other homes or accounts can still run the binary this update replaces. The
340 + // lock key names the package manager location, so other drivers there keep their own package.
341 + const instancesSharingInstallation = Effect.fn(
342 + "ProviderMaintenanceRunner.instancesSharingInstallation",
343 + )(function* (instanceId: ProviderInstanceId, provider: ProviderDriverKind, lockKey: string) {
344 + const providers = yield* providerRegistry.getProviders;
345 + const shared = yield* Effect.forEach(
346 + providers.filter(
347 + (candidate) => candidate.instanceId !== instanceId && candidate.driver === provider,
348 + ),
349 + (candidate) =>
350 + providerRegistry
351 + .getProviderMaintenanceCapabilitiesForInstance(candidate.instanceId, candidate.driver)
352 + .pipe(
353 + Effect.map((capabilities) =>
354 + capabilities.update?.lockKey === lockKey ? [candidate.instanceId] : [],
355 + ),
356 + ),
357 + { concurrency: "unbounded" },
358 + );
359 + return [instanceId, ...shared.flat()];
360 + });
361 const commandCoordinator = yield* makeProviderMaintenanceCommandCoordinator({
362 makeAlreadyRunningError: () =>
363 new ServerProviderUpdateError({
@@ -557,8 +581,18 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () {
581 }
582 }
583
560 - yield* setRunningMessage(`Running ${describeCommand(command)}`);
561 - const result = yield* runMaintenanceCommand(command, setRunningMessage);
584 + const sharingInstances = yield* instancesSharingInstallation(
585 + instanceId,
586 + provider,
587 + update.lockKey,
588 + );
589 + yield* setRunningMessage("Waiting for running provider work to finish.");
590 + const result = yield* installationGate.update(
591 + sharingInstances,
592 + setRunningMessage(`Running ${describeCommand(command)}`).pipe(
593 + Effect.andThen(runMaintenanceCommand(command, setRunningMessage)),
594 + ),
595 + );
596 const finishedAt = yield* nowIso;
597 if (result.timedOut || result.exitCode !== 0) {
598 yield* Effect.logWarning("Provider update command failed", {
apps/server/src/server.ts
+4
@@ -110,6 +110,7 @@ import * as NativeAppIconResolver from "./assets/NativeAppIconResolver.ts";
110 import * as AntigravityInstallation from "./provider/AntigravityInstallation.ts";
111 import * as ProviderInstallation from "./provider/providerInstallation.ts";
112 import * as CodexInstallation from "./provider/CodexInstallation.ts";
113 +import * as ProviderInstallationGate from "./provider/ProviderInstallationGate.ts";
114 import * as ProviderInstanceRegistry from "./provider/ProviderInstanceRegistry.ts";
115 import * as ProviderAdapterRegistry from "./orchestration-v2/ProviderAdapterRegistry.ts";
116 import * as ProviderRegistry from "./provider/ProviderRegistry.ts";
@@ -700,6 +701,9 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll(
701 ),
702 Layer.provideMerge(ProviderRegistry.layer),
703 Layer.provideMerge(ProviderInstanceRegistryHydration.layer),
704 + // One gate for the server, below hydration so the Claude adapter sees it: layers memoize by
705 + // reference, so the runtime layer shares it.
706 + Layer.provideMerge(ProviderInstallationGate.layer),
707 // Provider hydration builds the Claude runner, which reports agents to the same scanner the
708 // terminal and preview layers share.
709 Layer.provideMerge(layerPortScanner),
apps/server/src/textGeneration/CodexTextGeneration.test.ts
+3
@@ -6,6 +6,7 @@ import {
6 type ServerProvider,
7 } from "@t3tools/contracts";
8 import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
9 +import * as ProviderInstallationGate from "../provider/ProviderInstallationGate.ts";
10 import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
11 import * as ServerSettings from "../serverSettings.ts";
12 import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
@@ -640,6 +641,7 @@ it.effect.each(PREFLIGHT_PRIMARY_STATES)("text generation preflight: %s", (prima
641 Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
642 resolveLink: () => Effect.die("unused"),
643 }),
644 + ProviderInstallationGate.layer,
645 ),
646 ),
647 );
@@ -730,6 +732,7 @@ it.effect(
732 Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
733 resolveLink: () => Effect.die("unused"),
734 }),
735 + ProviderInstallationGate.layer,
736 ),
737 ),
738 );
apps/server/src/textGeneration/TextGeneration.ts
+7 -2
@@ -17,6 +17,7 @@ import * as ServerSettings from "../serverSettings.ts";
17 import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration";
18 import { TextGenerationError } from "@t3tools/contracts";
19
20 +import * as ProviderInstallationGate from "../provider/ProviderInstallationGate.ts";
21 import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
22 import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
23 import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
@@ -77,6 +78,7 @@ export const make = Effect.gen(function* () {
78 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
79 const sourceControl = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
80 const serverSettings = yield* ServerSettings.ServerSettingsService;
81 + const installationGate = yield* ProviderInstallationGate.ProviderInstallationGate;
82 const textGenerators = yield* TextGenerators.TextGenerators;
83 const generate = Effect.fn("TextGeneration.generate")(function* <A>(
84 operation: TextGenerationOp,
@@ -146,8 +148,11 @@ export const make = Effect.gen(function* () {
148 "Review guides need a coding agent that can read the checkout. None is ready. Pick Codex or Claude as the review guide model in Settings → Source control.",
149 });
150 }
149 - return yield* resolveInstance(registry, operation, selection.instanceId).pipe(
150 - Effect.flatMap((service) => invoke(service, selection)),
151 + return yield* installationGate.execute(
152 + selection.instanceId,
153 + resolveInstance(registry, operation, selection.instanceId).pipe(
154 + Effect.flatMap((service) => invoke(service, selection)),
155 + ),
156 );
157 });
158 return TextGeneration.of({