Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { DEFAULT_MODEL, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
3import { assert, it } from "@effect/vitest";
4import { Deferred, Effect, Fiber, Option, Ref, Stream } from "effect";
5
6import { ServerConfig } from "./config.ts";
7import {
8 OrchestrationEngineService,
9 type OrchestrationEngineShape,
10} from "./orchestration/Services/OrchestrationEngine.ts";
11import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery.ts";
12import { AnalyticsService } from "./telemetry/Services/AnalyticsService.ts";
13import {
14 getAutoBootstrapDefaultModelSelection,
15 launchStartupHeartbeat,
16 makeCommandGate,
17 resolveAutoBootstrapWelcomeTargets,
18 resolveWelcomeBase,
19 ServerRuntimeStartupError,
20} from "./serverRuntimeStartup.ts";
21
22it("uses the canonical Codex default for auto-bootstrapped model selection", () => {
23 assert.deepStrictEqual(getAutoBootstrapDefaultModelSelection(), {
24 instanceId: ProviderInstanceId.make("codex"),
25 model: DEFAULT_MODEL,
26 });
27});
28
29it.effect("enqueueCommand waits for readiness and then drains queued work", () =>
30 Effect.scoped(
31 Effect.gen(function* () {
32 const executionCount = yield* Ref.make(0);
33 const commandGate = yield* makeCommandGate;
34
35 const queuedCommandFiber = yield* commandGate
36 .enqueueCommand(Ref.updateAndGet(executionCount, (count) => count + 1))
37 .pipe(Effect.forkScoped);
38
39 yield* Effect.yieldNow;
40 assert.equal(yield* Ref.get(executionCount), 0);
41
42 yield* commandGate.signalCommandReady;
43
44 const result = yield* Fiber.join(queuedCommandFiber);
45 assert.equal(result, 1);
46 assert.equal(yield* Ref.get(executionCount), 1);
47 }),
48 ),
49);
50
51it.effect("enqueueCommand fails queued work when readiness fails", () =>
52 Effect.scoped(
53 Effect.gen(function* () {
54 const commandGate = yield* makeCommandGate;
55 const failure = yield* Deferred.make<void, never>();
56
57 const queuedCommandFiber = yield* commandGate
58 .enqueueCommand(Deferred.await(failure).pipe(Effect.as("should-not-run")))
59 .pipe(Effect.forkScoped);
60
61 yield* commandGate.failCommandReady(
62 new ServerRuntimeStartupError({
63 message: "startup failed",
64 }),
65 );
66
67 const error = yield* Effect.flip(Fiber.join(queuedCommandFiber));
68 assert.equal(error.message, "startup failed");
69 }),
70 ),
71);
72
73it.effect("launchStartupHeartbeat does not block the caller while counts are loading", () =>
74 Effect.scoped(
75 Effect.gen(function* () {
76 const releaseCounts = yield* Deferred.make<void, never>();
77
78 yield* launchStartupHeartbeat.pipe(
79 Effect.provideService(ProjectionSnapshotQuery, {
80 getCommandReadModel: () => Effect.die("unused"),
81 getSnapshot: () => Effect.die("unused"),
82 getShellSnapshot: () => Effect.die("unused"),
83 getSnapshotSequence: () => Effect.die("unused"),
84 getCounts: () =>
85 Deferred.await(releaseCounts).pipe(
86 Effect.as({
87 projectCount: 2,
88 threadCount: 3,
89 }),
90 ),
91 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
92 getProjectShellById: () => Effect.succeed(Option.none()),
93 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
94 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
95 getThreadShellById: () => Effect.succeed(Option.none()),
96 getThreadDetailById: () => Effect.succeed(Option.none()),
97 }),
98 Effect.provideService(AnalyticsService, {
99 record: () => Effect.void,
100 flush: Effect.void,
101 }),
102 );
103 }),
104 ),
105);
106
107it.effect("resolveWelcomeBase derives cwd and project name from server config", () =>
108 Effect.gen(function* () {
109 const welcome = yield* resolveWelcomeBase.pipe(
110 Effect.provideService(ServerConfig, {
111 cwd: "/tmp/startup-project",
112 } as never),
113 );
114
115 assert.deepStrictEqual(welcome, {
116 cwd: "/tmp/startup-project",
117 projectName: "startup-project",
118 });
119 }),
120);
121
122it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and thread ids", () => {
123 const bootstrapProjectId = ProjectId.make("project-startup-bootstrap");
124 const bootstrapThreadId = ThreadId.make("thread-startup-bootstrap");
125
126 return Effect.gen(function* () {
127 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
128 const targets = yield* resolveAutoBootstrapWelcomeTargets.pipe(
129 Effect.provideService(ServerConfig, {
130 cwd: "/tmp/startup-project",
131 autoBootstrapProjectFromCwd: true,
132 } as never),
133 Effect.provideService(ProjectionSnapshotQuery, {
134 getCommandReadModel: () => Effect.die("unused"),
135 getSnapshot: () => Effect.die("unused"),
136 getShellSnapshot: () => Effect.die("unused"),
137 getSnapshotSequence: () => Effect.die("unused"),
138 getCounts: () => Effect.die("unused"),
139 getActiveProjectByWorkspaceRoot: () =>
140 Effect.succeed(
141 Option.some({
142 id: bootstrapProjectId,
143 title: "Startup Project",
144 workspaceRoot: "/tmp/startup-project",
145 defaultModelSelection: getAutoBootstrapDefaultModelSelection(),
146 scripts: [],
147 createdAt: "2026-01-01T00:00:00.000Z",
148 updatedAt: "2026-01-01T00:00:00.000Z",
149 deletedAt: null,
150 }),
151 ),
152 getProjectShellById: () => Effect.die("unused"),
153 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.some(bootstrapThreadId)),
154 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
155 getThreadShellById: () => Effect.die("unused"),
156 getThreadDetailById: () => Effect.die("unused"),
157 }),
158 Effect.provideService(OrchestrationEngineService, {
159 readEvents: () => Stream.empty,
160 dispatch: (command) =>
161 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
162 Effect.as({ sequence: 1 }),
163 ),
164 streamDomainEvents: Stream.empty,
165 } satisfies OrchestrationEngineShape),
166 Effect.provide(NodeServices.layer),
167 );
168
169 assert.deepStrictEqual(targets, {
170 bootstrapProjectId,
171 bootstrapThreadId,
172 });
173 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
174 });
175});
176
177it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when missing", () =>
178 Effect.gen(function* () {
179 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
180 const targets = yield* resolveAutoBootstrapWelcomeTargets.pipe(
181 Effect.provideService(ServerConfig, {
182 cwd: "/tmp/startup-project",
183 autoBootstrapProjectFromCwd: true,
184 } as never),
185 Effect.provideService(ProjectionSnapshotQuery, {
186 getCommandReadModel: () => Effect.die("unused"),
187 getSnapshot: () => Effect.die("unused"),
188 getShellSnapshot: () => Effect.die("unused"),
189 getSnapshotSequence: () => Effect.die("unused"),
190 getCounts: () => Effect.die("unused"),
191 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
192 getProjectShellById: () => Effect.die("unused"),
193 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
194 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
195 getThreadShellById: () => Effect.die("unused"),
196 getThreadDetailById: () => Effect.die("unused"),
197 }),
198 Effect.provideService(OrchestrationEngineService, {
199 readEvents: () => Stream.empty,
200 dispatch: (command) =>
201 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
202 Effect.as({ sequence: 1 }),
203 ),
204 streamDomainEvents: Stream.empty,
205 } satisfies OrchestrationEngineShape),
206 Effect.provide(NodeServices.layer),
207 );
208
209 assert.equal(typeof targets.bootstrapProjectId, "string");
210 assert.equal(typeof targets.bootstrapThreadId, "string");
211 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), ["project.create", "thread.create"]);
212 }),
213);