Skip to content

Commit 2958431

Browse files
committed
fix(server): preserve live project state on probe errors
1 parent 20363c3 commit 2958431

3 files changed

Lines changed: 150 additions & 0 deletions

File tree

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,22 @@ it.layer(NodeServices.layer)("project lookup with unavailable workspaces", (it)
357357
);
358358
});
359359

360+
const persistCliRuntimeState = (baseDir: string, origin: string) =>
361+
Effect.gen(function* () {
362+
const config = yield* makeCliTestServerConfig(baseDir);
363+
const state = yield* makePersistedServerRuntimeState({ config, port: 0 });
364+
yield* persistServerRuntimeState({
365+
path: config.serverRuntimeStatePath,
366+
state: { ...state, origin },
367+
});
368+
return config;
369+
});
370+
371+
const reserveLoopbackPort = Effect.gen(function* () {
372+
const net = yield* NetService.NetService;
373+
return yield* net.reserveLoopbackPort();
374+
}).pipe(Effect.provide(NetService.layer));
375+
360376
const withLiveProjectCliServer = <A, E, R>(baseDir: string, run: () => Effect.Effect<A, E, R>) =>
361377
Effect.gen(function* () {
362378
const config = yield* makeCliTestServerConfig(baseDir);
@@ -825,6 +841,73 @@ it.layer(NodeServices.layer)("bin cli parsing", (it) => {
825841
}),
826842
);
827843

844+
it.effect(
845+
"preserves runtime state and avoids offline mutation for non-refused probe failures",
846+
() =>
847+
Effect.gen(function* () {
848+
const baseDir = NodeFS.mkdtempSync(
849+
NodePath.join(NodeOS.tmpdir(), "t3-cli-projects-live-failure-test-"),
850+
);
851+
const workspaceRoot = NodeFS.mkdtempSync(
852+
NodePath.join(NodeOS.tmpdir(), "t3-cli-projects-live-failure-workspace-"),
853+
);
854+
const config = yield* persistCliRuntimeState(baseDir, "http://[invalid");
855+
856+
const error = yield* runCliWithRuntime([
857+
"project",
858+
"add",
859+
workspaceRoot,
860+
"--title",
861+
"Should Not Be Added",
862+
"--base-dir",
863+
baseDir,
864+
]).pipe(Effect.flip);
865+
866+
if (typeof error !== "object" || error === null || !("_tag" in error)) {
867+
assert.fail(`Expected a tagged project command error, got ${String(error)}`);
868+
}
869+
assert.equal(error._tag, "ProjectLiveServerRequestError");
870+
assert.isTrue(NodeFS.existsSync(config.serverRuntimeStatePath));
871+
const snapshot = yield* readPersistedSnapshot(baseDir);
872+
assert.isFalse(
873+
snapshot.projects.some(
874+
(project) => project.workspaceRoot === workspaceRoot && project.deletedAt === null,
875+
),
876+
);
877+
}),
878+
);
879+
880+
it.effect("clears a refused runtime endpoint before falling back to offline mutation", () =>
881+
Effect.gen(function* () {
882+
const baseDir = NodeFS.mkdtempSync(
883+
NodePath.join(NodeOS.tmpdir(), "t3-cli-projects-refused-runtime-test-"),
884+
);
885+
const workspaceRoot = NodeFS.mkdtempSync(
886+
NodePath.join(NodeOS.tmpdir(), "t3-cli-projects-refused-runtime-workspace-"),
887+
);
888+
const refusedPort = yield* reserveLoopbackPort;
889+
const config = yield* persistCliRuntimeState(baseDir, `http://127.0.0.1:${refusedPort}`);
890+
891+
yield* runCliWithRuntime([
892+
"project",
893+
"add",
894+
workspaceRoot,
895+
"--title",
896+
"Offline After Refusal",
897+
"--base-dir",
898+
baseDir,
899+
]);
900+
901+
assert.isFalse(NodeFS.existsSync(config.serverRuntimeStatePath));
902+
const snapshot = yield* readPersistedSnapshot(baseDir);
903+
assert.isTrue(
904+
snapshot.projects.some(
905+
(project) => project.workspaceRoot === workspaceRoot && project.deletedAt === null,
906+
),
907+
);
908+
}),
909+
);
910+
828911
it.effect("rejects dev-url on project commands", () =>
829912
Effect.gen(function* () {
830913
const workspaceRoot = NodeFS.mkdtempSync(

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { assert, it } from "@effect/vitest";
2+
import * as HttpClientError from "effect/unstable/http/HttpClientError";
3+
import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
24

35
import { EnvironmentInternalError } from "@t3tools/contracts";
46

57
import {
68
ProjectLiveServerDeclaredResponseError,
79
ProjectLiveServerRequestError,
10+
isProjectLiveServerConnectionRefusedError,
811
projectCommandErrorFromLiveServerRequest,
912
} from "./project.ts";
1013

@@ -23,6 +26,7 @@ it("maps declared server failures into structural project command errors", () =>
2326
assert.strictEqual(error.traceId, "trace-123");
2427
assert.strictEqual(error.message, "Server request failed (internal_error, trace trace-123).");
2528
assert.strictEqual(error.cause, cause);
29+
assert.isFalse(isProjectLiveServerConnectionRefusedError(error));
2630
});
2731

2832
it("preserves unexpected server failures without deriving the message from them", () => {
@@ -35,3 +39,30 @@ it("preserves unexpected server failures without deriving the message from them"
3539
assert.strictEqual(error.message, "Failed to call the running server.");
3640
assert.strictEqual(error.cause, cause);
3741
});
42+
43+
it("classifies only transport failures with an exact ECONNREFUSED code", () => {
44+
const request = HttpClientRequest.get("http://127.0.0.1:1/");
45+
const makeRequestError = (cause: unknown) =>
46+
projectCommandErrorFromLiveServerRequest(
47+
new HttpClientError.HttpClientError({
48+
reason: new HttpClientError.TransportError({ request, cause }),
49+
}),
50+
);
51+
52+
const refused = Object.assign(new Error("connection failed"), { code: "ECONNREFUSED" });
53+
const nestedRefused = Object.assign(new Error("wrapped connection failure"), { cause: refused });
54+
55+
assert.isTrue(isProjectLiveServerConnectionRefusedError(makeRequestError(refused)));
56+
assert.isTrue(isProjectLiveServerConnectionRefusedError(makeRequestError(nestedRefused)));
57+
assert.isFalse(
58+
isProjectLiveServerConnectionRefusedError(
59+
makeRequestError(new Error("connect ECONNREFUSED 127.0.0.1:1")),
60+
),
61+
);
62+
assert.isFalse(
63+
isProjectLiveServerConnectionRefusedError(
64+
makeRequestError(Object.assign(new Error("connection reset"), { code: "ECONNRESET" })),
65+
),
66+
);
67+
assert.isFalse(isProjectLiveServerConnectionRefusedError(new Error("connection failed")));
68+
});

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as FileSystem from "effect/FileSystem";
1616
import * as Layer from "effect/Layer";
1717
import * as Option from "effect/Option";
1818
import * as Path from "effect/Path";
19+
import * as Predicate from "effect/Predicate";
1920
import * as References from "effect/References";
2021
import * as Schema from "effect/Schema";
2122
import { Argument, Command, Flag, GlobalFlag } from "effect/unstable/cli";
@@ -102,6 +103,8 @@ export class ProjectLiveServerRequestError extends Schema.TaggedError<ProjectLiv
102103
}
103104
}
104105

106+
const isProjectLiveServerRequestError = Schema.is(ProjectLiveServerRequestError);
107+
105108
export class ProjectTitleEmptyError extends Schema.TaggedError<ProjectTitleEmptyError>()(
106109
"ProjectTitleEmptyError",
107110
{
@@ -186,6 +189,34 @@ export function projectCommandErrorFromLiveServerRequest(cause: unknown): Projec
186189
return new ProjectLiveServerRequestError({ operation: "callLiveServer", cause });
187190
}
188191

192+
const hasConnectionRefusedCode = (cause: unknown): boolean => {
193+
const seen = new Set<object>();
194+
let current = cause;
195+
196+
while (Predicate.isObject(current) && !seen.has(current)) {
197+
seen.add(current);
198+
if (Predicate.hasProperty(current, "code") && current.code === "ECONNREFUSED") {
199+
return true;
200+
}
201+
current = Predicate.hasProperty(current, "cause") ? current.cause : undefined;
202+
}
203+
204+
return false;
205+
};
206+
207+
export function isProjectLiveServerConnectionRefusedError(cause: unknown): boolean {
208+
if (!isProjectLiveServerRequestError(cause)) {
209+
return false;
210+
}
211+
212+
const transportError = cause.cause;
213+
return (
214+
HttpClientError.isHttpClientError(transportError) &&
215+
transportError.reason._tag === "TransportError" &&
216+
hasConnectionRefusedCode(transportError.reason.cause)
217+
);
218+
}
219+
189220
const projectCommandUuid = Crypto.Crypto.pipe(
190221
Effect.flatMap((crypto) => crypto.randomUUIDv4),
191222
Effect.mapError(
@@ -368,6 +399,11 @@ const tryResolveLiveProjectExecutionMode = Effect.fn("tryResolveLiveProjectExecu
368399
origin: runtimeState.value.origin,
369400
cause: attempted.failure,
370401
});
402+
403+
if (!isProjectLiveServerConnectionRefusedError(attempted.failure)) {
404+
return yield* attempted.failure;
405+
}
406+
371407
yield* clearPersistedServerRuntimeState(config.serverRuntimeStatePath);
372408
return Option.none<{ readonly origin: string }>();
373409
},

0 commit comments

Comments
 (0)