Skip to content

Commit b889ec4

Browse files
t3dotggclaude
andcommitted
fix(mobile): sol/macroscope review fixes for resume hardening
- Blocked probe failures (auth, permissions) keep their classification: the supervisor now parks in blocked instead of churning immediate reconnects when a foreground probe fails with ConnectionBlockedError. - The environment-set dedup compares elements instead of join(","), which was ambiguous for ids containing commas. - switchMap uses its default concurrency so each environment-set change interrupts the previous subscription set instead of leaking one per change; mergeAll inside stays unbounded. - The backoff ladder test reads the scheduled retryAt per rung and asserts it lands in the jitter bounds [rung/2, rung], restoring real coverage of ladder progression and the 16s cap. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 2c8fe66 commit b889ec4

3 files changed

Lines changed: 61 additions & 17 deletions

File tree

‎apps/mobile/src/connection/background-activity.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ export const mobileBackgroundActivityReporterLayer = Layer.effectDiscard(
128128
SubscriptionRef.changes(registry.entries),
129129
).pipe(
130130
Stream.map((entries) => [...entries.keys()].sort()),
131-
Stream.changesWith((a, b) => a.join(",") === b.join(",")),
132-
Stream.switchMap(
133-
(environmentIds) =>
134-
Stream.mergeAll(environmentIds.map(connectedGenerations), {
135-
concurrency: "unbounded",
136-
}),
137-
{ concurrency: "unbounded" },
131+
Stream.changesWith((a, b) => a.length === b.length && a.every((id, i) => id === b[i])),
132+
// Default switchMap concurrency (1) interrupts the previous inner
133+
// stream on every environment-set change; unbounded here would leak a
134+
// subscription set per change. mergeAll inside stays unbounded so all
135+
// environments are observed concurrently.
136+
Stream.switchMap((environmentIds) =>
137+
Stream.mergeAll(environmentIds.map(connectedGenerations), {
138+
concurrency: "unbounded",
139+
}),
138140
),
139141
Stream.runForEach(() => Effect.sync(requestReport)),
140142
Effect.forkScoped,

‎packages/client-runtime/src/connection/supervisor.test.ts‎

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EnvironmentId } from "@t3tools/contracts";
22
import { RelayClientTracer } from "@t3tools/shared/relayTracing";
33
import { describe, expect, it } from "@effect/vitest";
4+
import * as Clock from "effect/Clock";
45
import * as Deferred from "effect/Deferred";
56
import * as Effect from "effect/Effect";
67
import * as Layer from "effect/Layer";
@@ -358,18 +359,24 @@ describe("EnvironmentSupervisor", () => {
358359
);
359360
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
360361

361-
// Jitter puts each delay in [rung/2, rung], so exact rung boundaries
362-
// are not observable; advancing by the 16s cap always covers at least
363-
// one more retry and proves the ladder keeps going past its last rung.
364-
for (const [index] of [1_000, 2_000, 4_000, 8_000, 16_000, 16_000].entries()) {
365-
yield* TestClock.adjust(16_000);
362+
// Jitter puts each delay in [rung/2, rung]. Read the scheduled retryAt
363+
// per rung, assert it falls inside the jitter bounds (proving ladder
364+
// progression and the 16s cap), then advance exactly to it.
365+
for (const [index, rung] of [1_000, 2_000, 4_000, 8_000, 16_000, 16_000].entries()) {
366+
const current = yield* SubscriptionRef.get(supervisor.state);
367+
const now = yield* Clock.currentTimeMillis;
368+
expect(current.retryAt).not.toBeNull();
369+
const delay = (current.retryAt ?? now) - now;
370+
expect(delay).toBeGreaterThanOrEqual(rung / 2);
371+
expect(delay).toBeLessThanOrEqual(rung);
372+
yield* TestClock.adjust(delay);
366373
yield* eventuallyState(
367374
supervisor.state,
368-
(state) => state.phase === "backoff" && state.attempt >= index + 2,
375+
(state) => state.phase === "backoff" && state.attempt === index + 2,
369376
);
370377
}
371378

372-
expect(yield* Ref.get(harness.prepareCount)).toBeGreaterThanOrEqual(7);
379+
expect(yield* Ref.get(harness.prepareCount)).toBe(7);
373380
}).pipe(Effect.provide(TestClock.layer())),
374381
);
375382

@@ -1029,6 +1036,34 @@ describe("EnvironmentSupervisor", () => {
10291036
}).pipe(Effect.provide(TestClock.layer())),
10301037
);
10311038

1039+
it.effect("parks in blocked when the foreground probe fails with a blocked error", () =>
1040+
Effect.gen(function* () {
1041+
const harness = yield* makeHarness({
1042+
probe: (attempt) => (attempt === 1 ? Effect.fail(blocked()) : Effect.void),
1043+
});
1044+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1045+
initiallyDesired: true,
1046+
}).pipe(Effect.provide(harness.dependencies));
1047+
1048+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1049+
yield* harness.wake("application-active");
1050+
// A blocked probe failure (auth revoked, permissions) must not churn
1051+
// immediate reconnects; the supervisor parks until an external signal.
1052+
yield* awaitState(
1053+
supervisor.state,
1054+
(state) => state.phase === "blocked" && state.lastFailure?.reason === "authentication",
1055+
);
1056+
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
1057+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1058+
1059+
yield* harness.wake("application-active");
1060+
yield* awaitState(
1061+
supervisor.state,
1062+
(state) => state.phase === "connected" && state.generation === 2,
1063+
);
1064+
}),
1065+
);
1066+
10321067
it.effect("quickly times out a stalled mobile foreground liveness probe", () =>
10331068
Effect.gen(function* () {
10341069
const harness = yield* makeHarness({

‎packages/client-runtime/src/connection/supervisor.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,16 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
450450
);
451451
if (probeEvent._tag === "ProbeCompleted") {
452452
if (Exit.isFailure(probeEvent.exit)) {
453-
// A failed health check already proves the session is dead;
454-
// replace it immediately instead of paying the retry ladder
455-
// for a failure the probe just diagnosed.
453+
const failure = Cause.findErrorOption(probeEvent.exit.cause);
454+
if (Option.isSome(failure) && failure.value._tag === "ConnectionBlockedError") {
455+
// Blocked failures (auth, permissions) must keep their
456+
// classification so the run loop parks in the blocked
457+
// state instead of churning immediate reconnects.
458+
yield* probeEvent.exit;
459+
}
460+
// A transiently failed health check already proves the
461+
// session is dead; replace it immediately instead of paying
462+
// the retry ladder for a failure the probe just diagnosed.
456463
return true;
457464
}
458465
break;

0 commit comments

Comments
 (0)