Skip to content

Commit b7c7a78

Browse files
only21milclaudemaria-rcks
authored
fix(server): t3 project commands wait for a busy server's reply (pingdotgg#24090)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 04f3538 commit b7c7a78

2 files changed

Lines changed: 78 additions & 12 deletions

File tree

‎apps/server/src/cli/project.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import {
1818
import * as NetService from "@t3tools/shared/Net";
1919
import * as DateTime from "effect/DateTime";
2020
import * as Effect from "effect/Effect";
21+
import * as Fiber from "effect/Fiber";
2122
import * as FileSystem from "effect/FileSystem";
2223
import * as Layer from "effect/Layer";
2324
import * as References from "effect/References";
2425
import * as Stream from "effect/Stream";
2526
import { Command } from "effect/cli";
27+
import { FetchHttpClient } from "effect/http";
28+
import * as TestClock from "effect/testing/TestClock";
2629

2730
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
2831
import { cli } from "../binCli.ts";
@@ -37,6 +40,10 @@ import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
3740
import * as ProjectService from "../project/ProjectService.ts";
3841
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
3942
import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts";
43+
import {
44+
makePersistedServerRuntimeState,
45+
persistServerRuntimeState,
46+
} from "../serverRuntimeState.ts";
4047
import * as ServerSettings from "../serverSettings.ts";
4148
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
4249

@@ -121,6 +128,66 @@ it.effect("adds, renames, and removes projects through the V2 project CLI domain
121128
}).pipe(Effect.provide(NodeServices.layer)),
122129
);
123130

131+
it.effect.each(["snapshot", "mutation"] as const)(
132+
"waits for a busy live server's %s response",
133+
(slowRequest) =>
134+
Effect.gen(function* () {
135+
const fs = yield* FileSystem.FileSystem;
136+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-live-project-cli-" });
137+
const baseDir = NodePath.join(root, "state");
138+
const workspaceRoot = NodePath.join(root, "workspace");
139+
yield* fs.makeDirectory(workspaceRoot);
140+
yield* persistServerRuntimeState({
141+
path: NodePath.join(baseDir, "userdata", "server-runtime.json"),
142+
state: yield* makePersistedServerRuntimeState({
143+
config: { host: "127.0.0.1", devUrl: undefined },
144+
port: 3_773,
145+
}),
146+
});
147+
const now = "2026-01-01T00:00:00.000Z";
148+
const requestReceived = Promise.withResolvers<void>();
149+
const releaseResponse = Promise.withResolvers<void>();
150+
let snapshotRequests = 0;
151+
let mutations = 0;
152+
const fetch: typeof globalThis.fetch = async (_input, init) => {
153+
const isMutation = init?.method === "POST";
154+
if (isMutation) mutations++;
155+
else snapshotRequests++;
156+
if (slowRequest === "mutation" ? isMutation : !isMutation && snapshotRequests === 2) {
157+
requestReceived.resolve();
158+
await releaseResponse.promise;
159+
}
160+
return Response.json(
161+
isMutation
162+
? {
163+
id: "live-project",
164+
title: NodePath.basename(workspaceRoot),
165+
workspaceRoot,
166+
defaultModelSelection: null,
167+
scripts: [],
168+
createdAt: now,
169+
updatedAt: now,
170+
deletedAt: null,
171+
}
172+
: { projects: [], updatedAt: now },
173+
);
174+
};
175+
176+
const add = yield* runCli(["project", "add", workspaceRoot, "--base-dir", baseDir]).pipe(
177+
Effect.provideService(FetchHttpClient.Fetch, fetch),
178+
Effect.forkChild,
179+
);
180+
yield* Effect.promise(() => requestReceived.promise);
181+
yield* TestClock.adjust("2 seconds");
182+
releaseResponse.resolve();
183+
yield* Fiber.join(add);
184+
185+
assert.equal(snapshotRequests, 2);
186+
assert.equal(mutations, 1);
187+
assert.deepEqual((yield* readProjects(baseDir)).projects, []);
188+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
189+
);
190+
124191
const makeProjectLookupFixture = Effect.fn("ProjectCliTest.makeProjectLookupFixture")(function* () {
125192
const fs = yield* FileSystem.FileSystem;
126193
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-project-lookup-" });

‎apps/server/src/cli/project.ts‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ const layerProjectCliRuntime = RuntimeLayer.layerProjectService.pipe(
214214
Layer.provideMerge(SqlitePersistence.layerConfig),
215215
);
216216

217-
const PROJECT_CLI_LIVE_SERVER_TIMEOUT = Duration.seconds(1);
217+
const PROJECT_CLI_LIVE_SERVER_PROBE_TIMEOUT = Duration.seconds(1);
218+
const PROJECT_CLI_LIVE_SERVER_REQUEST_TIMEOUT = Duration.seconds(10);
218219
const withProjectCliSessionToken = <A, E, R>(
219220
environmentAuth: EnvironmentAuth.EnvironmentAuth["Service"],
220221
run: (token: string) => Effect.Effect<A, E, R>,
@@ -228,9 +229,6 @@ const withProjectCliSessionToken = <A, E, R>(
228229
(issued) => environmentAuth.revokeSession(issued.sessionId).pipe(Effect.ignore({ log: true })),
229230
);
230231

231-
const withProjectCliLiveServerTimeout = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
232-
effect.pipe(Effect.timeout(PROJECT_CLI_LIVE_SERVER_TIMEOUT));
233-
234232
const makeLiveServerClient = (origin: string) =>
235233
HttpApiClient.make(EnvironmentHttpApi, {
236234
baseUrl: origin,
@@ -317,17 +315,14 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* (
317315
});
318316

319317
const fetchLiveOrchestrationSnapshot = Effect.fn("project.fetchLiveOrchestrationSnapshot")(
320-
function* (origin: string, bearerToken: string) {
318+
function* (origin: string, bearerToken: string, _timeout?: Duration.Duration) {
321319
const client = yield* makeLiveServerClient(origin);
322320
return yield* client.projects.snapshot({
323321
headers: { authorization: `Bearer ${bearerToken}` },
324322
});
325323
},
326-
(effect, _origin, _bearerToken) =>
327-
effect.pipe(
328-
withProjectCliLiveServerTimeout,
329-
Effect.mapError(projectCommandErrorFromLiveServerRequest),
330-
),
324+
(effect, _origin, _bearerToken, timeout = PROJECT_CLI_LIVE_SERVER_REQUEST_TIMEOUT) =>
325+
effect.pipe(Effect.timeout(timeout), Effect.mapError(projectCommandErrorFromLiveServerRequest)),
331326
);
332327

333328
const dispatchLiveOrchestrationCommand = Effect.fn("project.dispatchLiveOrchestrationCommand")(
@@ -340,7 +335,7 @@ const dispatchLiveOrchestrationCommand = Effect.fn("project.dispatchLiveOrchestr
340335
},
341336
(effect, _origin, _bearerToken, _command) =>
342337
effect.pipe(
343-
withProjectCliLiveServerTimeout,
338+
Effect.timeout(PROJECT_CLI_LIVE_SERVER_REQUEST_TIMEOUT),
344339
Effect.mapError(projectCommandErrorFromLiveServerRequest),
345340
),
346341
);
@@ -361,7 +356,11 @@ const tryResolveLiveProjectExecutionMode = Effect.fn("tryResolveLiveProjectExecu
361356
}
362357

363358
const attempt = withProjectCliSessionToken(environmentAuth, (token) =>
364-
fetchLiveOrchestrationSnapshot(runtimeState.value.origin, token).pipe(
359+
fetchLiveOrchestrationSnapshot(
360+
runtimeState.value.origin,
361+
token,
362+
PROJECT_CLI_LIVE_SERVER_PROBE_TIMEOUT,
363+
).pipe(
365364
Effect.as({
366365
origin: runtimeState.value.origin,
367366
}),

0 commit comments

Comments
 (0)