Skip to content

Commit 04085eb

Browse files
committed
fix(server): skip disabled providers during startup bootstrap
1 parent ede3f2d commit 04085eb

2 files changed

Lines changed: 253 additions & 15 deletions

File tree

‎apps/server/src/serverRuntimeStartup.test.ts‎

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
11
import { assert, it } from "@effect/vitest";
2+
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
23
import {
34
DEFAULT_SERVER_SETTINGS,
45
DEFAULT_MODEL,
56
ProjectId,
67
ProviderInstanceId,
8+
ProviderDriverKind,
9+
CommandId,
10+
EnvironmentId,
11+
type ServerLifecycleStreamEvent,
712
} from "@t3tools/contracts";
13+
import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
14+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
15+
import { HttpServer } from "effect/http";
16+
import * as NetAddress from "effect/net/NetAddress";
817
import * as Deferred from "effect/Deferred";
918
import * as Effect from "effect/Effect";
1019
import * as Exit from "effect/Exit";
1120
import * as Fiber from "effect/Fiber";
1221
import * as Ref from "effect/Ref";
22+
import * as Layer from "effect/Layer";
23+
import * as Path from "effect/Path";
1324

1425
import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
1526

1627
import * as ServerConfig from "./config.ts";
1728
import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
29+
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
30+
import * as ServiceLauncherClient from "./cloud/serviceLauncherClient.ts";
31+
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
32+
import * as Keybindings from "./keybindings.ts";
33+
import * as EffectWorker from "./orchestration-v2/EffectWorker.ts";
34+
import * as LegacyV1ThreadImporter from "./orchestration-v2/legacy/LegacyV1ThreadImporter.ts";
35+
import * as Orchestrator from "./orchestration-v2/Orchestrator.ts";
36+
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
37+
import * as ProviderRuntimeRecovery from "./orchestration-v2/ProviderRuntimeRecoveryService.ts";
38+
import * as ProviderSessionManager from "./orchestration-v2/ProviderSessionManager.ts";
39+
import * as ThreadLaunch from "./orchestration-v2/ThreadLaunchService.ts";
40+
import * as ThreadManagement from "./orchestration-v2/ThreadManagementService.ts";
41+
import * as ExternalLauncher from "./process/externalLauncher.ts";
42+
import * as ProjectService from "./project/ProjectService.ts";
43+
import * as AgentAwarenessRelay from "./relay/AgentAwarenessRelay.ts";
44+
import { ServerActivation } from "./serverActivation.ts";
45+
import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";
46+
import * as ServerSettings from "./serverSettings.ts";
47+
import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
1848

1949
it("uses the canonical Codex model for auto-bootstrap", () => {
2050
assert.deepEqual(ServerRuntimeStartup.getAutoBootstrapThreadModelSelection(), {
@@ -23,6 +53,198 @@ it("uses the canonical Codex model for auto-bootstrap", () => {
2353
});
2454
});
2555

56+
it.effect.each([
57+
"disabled-codex",
58+
"disabled-default",
59+
"launch-failure",
60+
"project-failure",
61+
] as const)("keeps bootstrap launch failures separate from project failures: %s", (scenario) =>
62+
Effect.scoped(
63+
Effect.gen(function* () {
64+
const projectId = ProjectId.make("bootstrap-project");
65+
const instanceId = ProviderInstanceId.make(
66+
scenario === "disabled-default" ? "custom" : "codex",
67+
);
68+
const events: ServerLifecycleStreamEvent[] = [];
69+
const recovery = {
70+
terminalizedRuns: 0,
71+
stoppedSessions: 0,
72+
closedRequests: 0,
73+
retiredEffects: 0,
74+
requeuedEffects: 0,
75+
};
76+
const imports = { importedThreadCount: 0, importedMessageCount: 0 };
77+
const dependencies = Layer.mergeAll(
78+
NodeCrypto.layer,
79+
Path.layer,
80+
Layer.mock(ServerConfig.ServerConfig)({
81+
...(yield* ServerConfig.deriveServerPaths("/bootstrap-project", undefined).pipe(
82+
Effect.provide(Path.layer),
83+
)),
84+
baseDir: "/bootstrap-project",
85+
logLevel: "Error",
86+
traceMinLevel: "Info",
87+
traceTimingEnabled: false,
88+
traceBatchWindowMs: 200,
89+
traceMaxBytes: 1024,
90+
traceMaxFiles: 1,
91+
otlpTracesUrl: undefined,
92+
otlpMetricsUrl: undefined,
93+
otlpLogsUrl: undefined,
94+
otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
95+
otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
96+
otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
97+
otelEnvironment: OtelEnvironment.none,
98+
staticDir: undefined,
99+
devAllowedOrigins: [],
100+
desktopBootstrapToken: undefined,
101+
logWebSocketEvents: false,
102+
tailscaleServeEnabled: false,
103+
tailscaleServePort: 443,
104+
mode: "web",
105+
cwd: "/bootstrap-project",
106+
host: "localhost",
107+
port: 3773,
108+
devUrl: undefined,
109+
noBrowser: true,
110+
startupPresentation: "browser",
111+
autoBootstrapProjectFromCwd: true,
112+
}),
113+
Layer.mock(Keybindings.Keybindings)({ start: Effect.void }),
114+
Layer.mock(LegacyV1ThreadImporter.LegacyV1ThreadImporter)({
115+
pendingThreadCount: Effect.succeed(0),
116+
reconcileShells: Effect.succeed(imports),
117+
importPendingTranscripts: Effect.succeed(imports),
118+
}),
119+
Layer.mock(ProviderRuntimeRecovery.ProviderRuntimeRecoveryService)({
120+
recover: Effect.succeed(recovery),
121+
prepareForShutdown: Effect.void,
122+
reconcile: () => Effect.succeed(recovery),
123+
}),
124+
Layer.mock(Orchestrator.OrchestratorV2)({ recoverDelegatedTasks: Effect.void }),
125+
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({ shutdown: Effect.void }),
126+
Layer.mock(AgentAwarenessRelay.AgentAwarenessRelay)({ start: () => Effect.void }),
127+
Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({ runOnce: Effect.never }),
128+
Layer.mock(ServerLifecycleEvents.ServerLifecycleEvents)({
129+
publish: (event) =>
130+
Effect.sync(() => {
131+
const published = { ...event, sequence: events.length + 1 };
132+
events.push(published);
133+
return published;
134+
}),
135+
}),
136+
Layer.mock(ServerSettings.ServerSettingsService)({
137+
start: Effect.void,
138+
getSettings: Effect.succeed({
139+
...DEFAULT_SERVER_SETTINGS,
140+
defaultModelSelection:
141+
scenario === "disabled-default" ? { instanceId, model: "custom-model" } : null,
142+
providerInstances: {
143+
[instanceId]: {
144+
driver: ProviderDriverKind.make("codex"),
145+
enabled: false,
146+
config: {},
147+
},
148+
},
149+
}),
150+
}),
151+
Layer.mock(ServerEnvironment.ServerEnvironment)({
152+
getDescriptor: Effect.succeed({
153+
environmentId: EnvironmentId.make("bootstrap-environment"),
154+
label: "Test environment",
155+
platform: { os: "linux", arch: "x64" },
156+
serverVersion: "0.0.0-test",
157+
capabilities: { repositoryIdentity: true },
158+
}),
159+
}),
160+
Layer.mock(ProjectStore.ProjectStoreV2)({}),
161+
Layer.mock(ProjectService.ProjectService)({
162+
bootstrap: () =>
163+
scenario === "project-failure"
164+
? Effect.die("project bootstrap failed")
165+
: Effect.succeed({
166+
created: true,
167+
project: {
168+
id: projectId,
169+
title: "Project",
170+
workspaceRoot: "/bootstrap-project",
171+
repositoryIdentity: null,
172+
faviconPath: null,
173+
scripts: [],
174+
createdAt: "2026-01-01T00:00:00Z",
175+
updatedAt: "2026-01-01T00:00:00Z",
176+
deletedAt: null,
177+
defaultModelSelection:
178+
scenario === "disabled-default"
179+
? { instanceId, model: "custom-model" }
180+
: null,
181+
},
182+
}),
183+
}),
184+
Layer.mock(ThreadManagement.ThreadManagementService)({
185+
getShellSnapshot: () =>
186+
Effect.succeed({
187+
threads: [],
188+
archivedThreads: [],
189+
schemaVersion: 1,
190+
snapshotSequence: 0,
191+
}),
192+
}),
193+
Layer.mock(ThreadLaunch.ThreadLaunchService)({
194+
launch: (input) => {
195+
assert.equal(input.modelSelection.instanceId, instanceId);
196+
return scenario === "launch-failure"
197+
? Effect.fail(
198+
new ThreadLaunch.ThreadLaunchError({
199+
operation: "create-thread",
200+
commandId: CommandId.make("bootstrap-command"),
201+
projectId,
202+
cause: "test launch failure",
203+
}),
204+
)
205+
: Effect.fail(
206+
new ThreadManagement.ThreadManagementProviderInstanceDisabledError({
207+
projectId,
208+
instanceId,
209+
}),
210+
);
211+
},
212+
}),
213+
Layer.mock(ServiceLauncherClient.ServiceLauncherClient)({
214+
managed: false,
215+
prepareTrial: Effect.succeed(undefined),
216+
}),
217+
Layer.mock(GitVcsDriver.GitVcsDriver)({}),
218+
Layer.mock(AnalyticsService.AnalyticsService)({}),
219+
Layer.mock(EnvironmentAuth.EnvironmentAuth)({}),
220+
Layer.mock(ExternalLauncher.ExternalLauncher)({}),
221+
Layer.mock(HttpServer.HttpServer)({
222+
address: NetAddress.inetAddressFromIpStringUnsafe("127.0.0.1", 3773),
223+
}),
224+
);
225+
yield* Effect.gen(function* () {
226+
const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
227+
yield* startup.markHttpListening;
228+
const ready = yield* Effect.exit(startup.awaitCommandReady);
229+
assert.equal(Exit.isSuccess(ready), scenario !== "project-failure");
230+
const welcome = events.find((event) => event.type === "welcome");
231+
if (scenario === "project-failure") {
232+
assert.isUndefined(welcome);
233+
} else {
234+
assert.isDefined(welcome);
235+
if (welcome?.type === "welcome") {
236+
assert.equal(welcome.payload.bootstrapProjectId, projectId);
237+
assert.isUndefined(welcome.payload.bootstrapThreadId);
238+
}
239+
}
240+
}).pipe(
241+
Effect.provide(ServerRuntimeStartup.layerWithOptions().pipe(Layer.provide(dependencies))),
242+
Effect.provideService(ServerActivation, Effect.never),
243+
);
244+
}),
245+
),
246+
);
247+
26248
it.effect("starts without scanning or rebuilding projection history", () =>
27249
Effect.gen(function* () {
28250
const calls = yield* Ref.make<ReadonlyArray<string>>([]);

‎apps/server/src/serverRuntimeStartup.ts‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -285,22 +285,38 @@ const resolveAutoBootstrapWelcomeTargets = Effect.gen(function* () {
285285
if (existingThread === undefined) {
286286
const serverSettings = yield* ServerSettings.ServerSettingsService;
287287
const settings = yield* serverSettings.getSettings;
288-
const launched = yield* threadLaunch.launch({
289-
commandId: CommandId.make(yield* randomUUID),
290-
projectId: project.id,
291-
title: "New thread",
292-
modelSelection:
293-
resolveProjectSettings(settings, project.id, project).settings.defaultModelSelection ??
294-
threadModelSelection,
295-
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
296-
runtimeMode: resolveProjectSettings(settings, project.id, project).settings
297-
.defaultRuntimeMode,
298-
workspaceStrategy: { type: "root" },
299-
createdBy: "system",
300-
creationSource: "server",
301-
});
288+
const launched = yield* threadLaunch
289+
.launch({
290+
commandId: CommandId.make(yield* randomUUID),
291+
projectId: project.id,
292+
title: "New thread",
293+
modelSelection:
294+
resolveProjectSettings(settings, project.id, project).settings.defaultModelSelection ??
295+
threadModelSelection,
296+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
297+
runtimeMode: resolveProjectSettings(settings, project.id, project).settings
298+
.defaultRuntimeMode,
299+
workspaceStrategy: { type: "root" },
300+
createdBy: "system",
301+
creationSource: "server",
302+
})
303+
.pipe(
304+
// The bootstrap thread is optional; its launch must not prevent opening the app.
305+
Effect.catchTags({
306+
ThreadManagementProviderInstanceDisabledError: (error) =>
307+
Effect.logInfo("Skipped bootstrap thread because its provider instance is disabled", {
308+
projectId: project.id,
309+
instanceId: error.instanceId,
310+
}).pipe(Effect.as(undefined)),
311+
ThreadLaunchError: (error) =>
312+
Effect.logWarning("Failed to launch bootstrap thread", {
313+
projectId: project.id,
314+
operation: error.operation,
315+
}).pipe(Effect.as(undefined)),
316+
}),
317+
);
302318
bootstrapProjectId = project.id;
303-
bootstrapThreadId = launched.threadId;
319+
bootstrapThreadId = launched?.threadId;
304320
} else {
305321
bootstrapProjectId = project.id;
306322
bootstrapThreadId = existingThread.id;

0 commit comments

Comments
 (0)