Skip to content

Commit 6b8a516

Browse files
juliusmarmingecodex
andcommitted
fix(observability): close lifecycle resource gaps
Co-authored-by: codex <[email protected]>
1 parent 1d7c529 commit 6b8a516

6 files changed

Lines changed: 195 additions & 9 deletions

File tree

‎apps/desktop/src/backend/DesktopBackendManager.test.ts‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ interface MakeInstanceInput {
124124
failure: DesktopBackendManager.PreflightFailure,
125125
) => Effect.Effect<boolean>;
126126
readonly config?: DesktopBackendManager.DesktopBackendStartConfig;
127-
readonly configResolve?: Effect.Effect<DesktopBackendManager.DesktopBackendStartConfig>;
127+
readonly configResolve?: Effect.Effect<
128+
DesktopBackendManager.DesktopBackendStartConfig,
129+
PlatformError.PlatformError
130+
>;
128131
readonly desktopTelemetryStream?: Stream.Stream<Uint8Array>;
129132
readonly desktopTelemetryPublisher?: Partial<
130133
DesktopTelemetryPublisher.DesktopTelemetryPublisher["Service"]
@@ -868,6 +871,83 @@ describe("DesktopBackendManager", () => {
868871
),
869872
);
870873

874+
it.effect("retries config resolution after a start request during stop teardown", () =>
875+
Effect.scoped(
876+
Effect.gen(function* () {
877+
const starts = yield* Queue.unbounded<number>();
878+
const teardownStarted = yield* Deferred.make<void>();
879+
const finishTeardown = yield* Deferred.make<void>();
880+
const configAttempts = yield* Ref.make(0);
881+
let startCount = 0;
882+
883+
const configFailure = PlatformError.systemError({
884+
_tag: "Unknown",
885+
module: "DesktopBackendManager",
886+
method: "configResolve",
887+
description: "transient configuration failure",
888+
});
889+
const spawnerLayer = Layer.succeed(
890+
ChildProcessSpawner.ChildProcessSpawner,
891+
ChildProcessSpawner.make(() =>
892+
Effect.gen(function* () {
893+
const scope = yield* Scope.Scope;
894+
const closed = yield* Deferred.make<void>();
895+
startCount += 1;
896+
yield* Queue.offer(starts, startCount);
897+
if (startCount === 1) {
898+
yield* Scope.addFinalizer(
899+
scope,
900+
Deferred.succeed(teardownStarted, undefined).pipe(
901+
Effect.andThen(Deferred.await(finishTeardown)),
902+
Effect.andThen(Deferred.succeed(closed, undefined)),
903+
Effect.asVoid,
904+
),
905+
);
906+
} else {
907+
yield* Scope.addFinalizer(
908+
scope,
909+
Deferred.succeed(closed, undefined).pipe(Effect.asVoid),
910+
);
911+
}
912+
return makeProcess({
913+
exitCode: Deferred.await(closed).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
914+
});
915+
}),
916+
),
917+
);
918+
const configResolve = Ref.updateAndGet(configAttempts, (attempt) => attempt + 1).pipe(
919+
Effect.flatMap((attempt) =>
920+
attempt === 2 ? Effect.fail(configFailure) : Effect.succeed(baseConfig),
921+
),
922+
);
923+
const instance = yield* makeTestInstance({
924+
spawnerLayer,
925+
configResolve,
926+
httpClientLayer: httpClientLayer(() => Effect.never),
927+
});
928+
929+
yield* instance.start;
930+
assert.equal(yield* Queue.take(starts), 1);
931+
932+
const stopFiber = yield* instance.stop().pipe(Effect.forkChild);
933+
yield* Deferred.await(teardownStarted).pipe(Effect.timeout("1 second"));
934+
yield* instance.start;
935+
yield* Deferred.succeed(finishTeardown, undefined);
936+
yield* Fiber.join(stopFiber).pipe(Effect.timeout("1 second"));
937+
938+
const pendingRestart = yield* instance.snapshot;
939+
assert.equal(pendingRestart.desiredRunning, true);
940+
assert.equal(pendingRestart.restartScheduled, true);
941+
942+
yield* TestClock.adjust(Duration.seconds(2));
943+
944+
assert.equal(yield* Queue.take(starts).pipe(Effect.timeout("1 second")), 2);
945+
assert.equal(yield* Ref.get(configAttempts), 3);
946+
assert.equal((yield* instance.snapshot).desiredRunning, true);
947+
}).pipe(Effect.provide(TestClock.layer())),
948+
),
949+
);
950+
871951
it.effect("keeps a timed-out run active until its process exits", () =>
872952
Effect.scoped(
873953
Effect.gen(function* () {

‎apps/desktop/src/backend/DesktopBackendManager.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,9 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
699699
Effect.option,
700700
);
701701
if (Option.isNone(config)) {
702+
if (current.desiredRunning) {
703+
yield* scheduleRestart("failed to generate desktop backend configuration");
704+
}
702705
return;
703706
}
704707
const entryExists = yield* fileSystem

‎apps/server/src/background/BackgroundPolicy.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,38 @@ describe("BackgroundPolicy", () => {
192192
}),
193193
);
194194

195+
it.effect("bounds client-id churn for one websocket connection", () =>
196+
Effect.gen(function* () {
197+
const policy = yield* BackgroundPolicy.BackgroundPolicy;
198+
const sessionId = AuthSessionId.make("session-1");
199+
const rpcClientId = RpcClientId.make(1);
200+
for (
201+
let index = 0;
202+
index <= BackgroundPolicy.MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT;
203+
index += 1
204+
) {
205+
yield* policy.reportClientActivity(
206+
sessionId,
207+
rpcClientId,
208+
makeReport({ clientId: `client-${index}` }),
209+
);
210+
}
211+
212+
const snapshot = yield* policy.snapshot;
213+
assert.equal(
214+
snapshot.leases.length,
215+
BackgroundPolicy.MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT,
216+
);
217+
assert.isTrue(
218+
snapshot.leases.some(
219+
(lease) =>
220+
lease.clientId ===
221+
`client-${BackgroundPolicy.MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT}`,
222+
),
223+
);
224+
}).pipe(Effect.provide(makeLayer(nominalHostPower))),
225+
);
226+
195227
it.effect("host low power mode disables opportunistic work without dropping scoped demand", () =>
196228
Effect.gen(function* () {
197229
const policy = yield* BackgroundPolicy.BackgroundPolicy;

‎apps/server/src/background/BackgroundPolicy.ts‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class BackgroundPolicy extends Context.Service<
5757

5858
const DEFAULT_LEASE_TTL_MS = 45_000;
5959
const MAX_LEASE_TTL_MS = 120_000;
60+
export const MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT = 16;
6061

6162
function scopeKey(scope: BackgroundScope): string {
6263
switch (scope.type) {
@@ -77,6 +78,53 @@ function isLeaseActive(lease: ClientActivityLease, now: DateTime.Utc): boolean {
7778
return DateTime.isGreaterThan(lease.expiresAt, now);
7879
}
7980

81+
function leaseKey(lease: Pick<ClientActivityLease, "sessionId" | "rpcClientId" | "clientId">) {
82+
return JSON.stringify([lease.sessionId, lease.rpcClientId, lease.clientId]);
83+
}
84+
85+
export function upsertClientActivityLease(
86+
leases: ReadonlyMap<string, ClientActivityLease>,
87+
lease: ClientActivityLease,
88+
now: DateTime.Utc,
89+
): Map<string, ClientActivityLease> {
90+
const next = new Map(leases);
91+
for (const [key, current] of next) {
92+
if (!isLeaseActive(current, now)) {
93+
next.delete(key);
94+
}
95+
}
96+
97+
const key = leaseKey(lease);
98+
if (!next.has(key)) {
99+
let connectionLeaseCount = 0;
100+
let oldestConnectionLease:
101+
| {
102+
readonly key: string;
103+
readonly updatedAtMs: number;
104+
}
105+
| undefined;
106+
for (const [currentKey, current] of next) {
107+
if (current.sessionId !== lease.sessionId || current.rpcClientId !== lease.rpcClientId) {
108+
continue;
109+
}
110+
connectionLeaseCount += 1;
111+
const updatedAtMs = DateTime.toEpochMillis(current.updatedAt);
112+
if (oldestConnectionLease === undefined || updatedAtMs < oldestConnectionLease.updatedAtMs) {
113+
oldestConnectionLease = { key: currentKey, updatedAtMs };
114+
}
115+
}
116+
if (
117+
connectionLeaseCount >= MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT &&
118+
oldestConnectionLease !== undefined
119+
) {
120+
next.delete(oldestConnectionLease.key);
121+
}
122+
}
123+
124+
next.set(key, lease);
125+
return next;
126+
}
127+
80128
function isForegroundLease(lease: ClientActivityLease, now: DateTime.Utc): boolean {
81129
return isLeaseActive(lease, now) && lease.visible && (lease.focused || lease.recentlyInteracted);
82130
}
@@ -216,11 +264,7 @@ export const make = Effect.fn("background.policy.make")(function* () {
216264
updatedAt: now,
217265
expiresAt,
218266
};
219-
yield* Ref.update(leasesRef, (leases) => {
220-
const next = new Map(leases);
221-
next.set(JSON.stringify([sessionId, rpcClientId, input.clientId]), lease);
222-
return next;
223-
});
267+
yield* Ref.update(leasesRef, (leases) => upsertClientActivityLease(leases, lease, now));
224268
yield* publishSnapshotUnlocked;
225269
}),
226270
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as Schema from "effect/Schema";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { ClientActivityClientId } from "./background.ts";
5+
6+
const decodeClientActivityClientId = Schema.decodeUnknownSync(ClientActivityClientId);
7+
8+
describe("ClientActivityClientId", () => {
9+
it("trims and accepts bounded client identifiers", () => {
10+
expect(decodeClientActivityClientId(" client-1 ")).toBe("client-1");
11+
expect(decodeClientActivityClientId("x".repeat(128))).toBe("x".repeat(128));
12+
});
13+
14+
it("rejects empty and oversized client identifiers", () => {
15+
expect(() => decodeClientActivityClientId(" ")).toThrow();
16+
expect(() => decodeClientActivityClientId("x".repeat(129))).toThrow();
17+
});
18+
});

‎packages/contracts/src/background.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as Schema from "effect/Schema";
22

3-
import { AuthSessionId, EnvironmentId, RpcClientId, ThreadId } from "./baseSchemas.ts";
3+
import {
4+
AuthSessionId,
5+
EnvironmentId,
6+
RpcClientId,
7+
ThreadId,
8+
TrimmedNonEmptyString,
9+
} from "./baseSchemas.ts";
410
import { ProviderInstanceId } from "./providerInstance.ts";
511

612
export const BackgroundBooleanState = Schema.Literals(["true", "false", "unknown"]);
@@ -55,9 +61,12 @@ export type BackgroundScope = typeof BackgroundScope.Type;
5561
export const ClientKind = Schema.Literals(["web", "desktop-renderer", "mobile", "unknown"]);
5662
export type ClientKind = typeof ClientKind.Type;
5763

64+
export const ClientActivityClientId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
65+
export type ClientActivityClientId = typeof ClientActivityClientId.Type;
66+
5867
export const ClientActivityReportInput = Schema.Struct({
5968
environmentId: Schema.optionalKey(EnvironmentId),
60-
clientId: Schema.String,
69+
clientId: ClientActivityClientId,
6170
clientKind: ClientKind,
6271
visible: Schema.Boolean,
6372
focused: Schema.Boolean,
@@ -75,7 +84,7 @@ export type ClientActivityReportInput = typeof ClientActivityReportInput.Type;
7584
export const ClientActivityLease = Schema.Struct({
7685
sessionId: AuthSessionId,
7786
rpcClientId: RpcClientId,
78-
clientId: Schema.String,
87+
clientId: ClientActivityClientId,
7988
clientKind: ClientKind,
8089
visible: Schema.Boolean,
8190
focused: Schema.Boolean,

0 commit comments

Comments
 (0)