fix(web): Woke dismiss syncs, preview mute is back, and subscription retries back off (#15034)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
798e945aa7
commit
c5a929e1ac
11 files changed
+306
-193
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
EnvironmentAuthorizationError,
|
||||
EnvironmentId,
|
||||
PreviewTabId,
|
||||
ThreadId,
|
||||
@@ -618,6 +619,114 @@ describe("environment RPC", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("doubles the retry delay for repeated failures and resets it after a value", () =>
|
||||
Effect.gen(function* () {
|
||||
const domainError = new Error("thread not hydrated yet");
|
||||
const subscriptions = yield* Queue.unbounded<number>();
|
||||
const failures = yield* Queue.unbounded<void>();
|
||||
let attempts = 0;
|
||||
const client = {
|
||||
[WS_METHODS.subscribeTerminalEvents]: () =>
|
||||
Stream.unwrap(
|
||||
Effect.sync(() => {
|
||||
attempts += 1;
|
||||
return attempts;
|
||||
}).pipe(
|
||||
Effect.tap((attempt) => Queue.offer(subscriptions, attempt)),
|
||||
Effect.map((attempt) => {
|
||||
if (attempt <= 2) return Stream.fail(domainError);
|
||||
if (attempt === 3)
|
||||
return Stream.concat(Stream.make("event"), Stream.fail(domainError));
|
||||
return Stream.never;
|
||||
}),
|
||||
),
|
||||
),
|
||||
} as unknown as WsRpcProtocolClient;
|
||||
const { activeSession, supervisor } = yield* makeHarness();
|
||||
|
||||
yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
|
||||
const subscriptionFiber = yield* subscribe(
|
||||
WS_METHODS.subscribeTerminalEvents,
|
||||
{},
|
||||
{
|
||||
onExpectedFailure: () => Queue.offer(failures, undefined),
|
||||
retryExpectedFailureAfter: "100 millis",
|
||||
},
|
||||
).pipe(
|
||||
Stream.runDrain,
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
Effect.forkChild,
|
||||
);
|
||||
|
||||
expect(yield* Queue.take(subscriptions)).toBe(1);
|
||||
yield* Queue.take(failures);
|
||||
yield* TestClock.adjust("100 millis");
|
||||
expect(yield* Queue.take(subscriptions)).toBe(2);
|
||||
yield* Queue.take(failures);
|
||||
// The second retry waits 200ms, so 100ms is not enough.
|
||||
yield* TestClock.adjust("100 millis");
|
||||
expect(Option.isNone(yield* Queue.poll(subscriptions))).toBe(true);
|
||||
yield* TestClock.adjust("100 millis");
|
||||
expect(yield* Queue.take(subscriptions)).toBe(3);
|
||||
// Attempt 3 delivered a value before failing, so the delay is back to 100ms.
|
||||
yield* Queue.take(failures);
|
||||
yield* TestClock.adjust("100 millis");
|
||||
expect(yield* Queue.take(subscriptions)).toBe(4);
|
||||
yield* Fiber.interrupt(subscriptionFiber);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("waits for the next session after an authorization failure", () =>
|
||||
Effect.gen(function* () {
|
||||
const subscriptions = yield* Queue.unbounded<void>();
|
||||
const failed = yield* Deferred.make<void>();
|
||||
let attempts = 0;
|
||||
const client = {
|
||||
[WS_METHODS.subscribeTerminalEvents]: () =>
|
||||
Stream.unwrap(
|
||||
Queue.offer(subscriptions, undefined).pipe(
|
||||
Effect.map(() => {
|
||||
attempts += 1;
|
||||
return attempts === 1
|
||||
? Stream.fail(
|
||||
new EnvironmentAuthorizationError({
|
||||
message: "Missing scope",
|
||||
requiredScope: "orchestration:read",
|
||||
}),
|
||||
)
|
||||
: Stream.never;
|
||||
}),
|
||||
),
|
||||
),
|
||||
} as unknown as WsRpcProtocolClient;
|
||||
const { activeSession, supervisor } = yield* makeHarness();
|
||||
|
||||
yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
|
||||
const subscriptionFiber = yield* subscribe(
|
||||
WS_METHODS.subscribeTerminalEvents,
|
||||
{},
|
||||
{
|
||||
onExpectedFailure: () => Deferred.succeed(failed, undefined).pipe(Effect.asVoid),
|
||||
retryExpectedFailureAfter: "100 millis",
|
||||
},
|
||||
).pipe(
|
||||
Stream.runDrain,
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
Effect.forkChild,
|
||||
);
|
||||
|
||||
yield* Queue.take(subscriptions);
|
||||
yield* Deferred.await(failed);
|
||||
yield* TestClock.adjust("1 minute");
|
||||
expect(Option.isNone(yield* Queue.poll(subscriptions))).toBe(true);
|
||||
|
||||
yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
|
||||
yield* Queue.take(subscriptions);
|
||||
yield* Fiber.interrupt(subscriptionFiber);
|
||||
expect(attempts).toBe(2);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect.each(["input", "stream"] as const)(
|
||||
"does not classify %s subscription defects as expected failures",
|
||||
(where) =>
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { ORCHESTRATION_V2_WS_METHODS, WS_METHODS } from "@t3tools/contracts";
|
||||
import {
|
||||
EnvironmentAuthorizationError,
|
||||
ORCHESTRATION_V2_WS_METHODS,
|
||||
WS_METHODS,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Context from "effect/Context";
|
||||
import type * as Duration from "effect/Duration";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
@@ -92,6 +96,10 @@ export class EnvironmentRpcSubscriptionObserver extends Context.Reference<{
|
||||
}) {}
|
||||
|
||||
export const isRpcClientError = Schema.is(RpcClientError.RpcClientError);
|
||||
const isEnvironmentAuthorizationError = Schema.is(EnvironmentAuthorizationError);
|
||||
|
||||
/** Ceiling for the doubling delay between same-session expected-failure retries. */
|
||||
const MAX_EXPECTED_FAILURE_RETRY_DELAY_MS = 30_000;
|
||||
|
||||
export type EnvironmentRpcInput<TTag extends EnvironmentRpcTag> = Parameters<RpcMethod<TTag>>[0];
|
||||
|
||||
@@ -192,6 +200,12 @@ interface SubscriptionOptions<TTag extends EnvironmentSubscriptionRpcTag> {
|
||||
readonly onExpectedFailure?: (
|
||||
cause: Cause.Cause<EnvironmentRpcStreamFailure<TTag>>,
|
||||
) => Effect.Effect<void, never, never>;
|
||||
/**
|
||||
* First delay before resubscribing on the same session after an expected
|
||||
* failure. Each consecutive failure doubles it up to 30 seconds, and the
|
||||
* first value from a healthy stream resets it. Authorization failures are
|
||||
* not retried; they wait for the next session or `resubscribe` signal.
|
||||
*/
|
||||
readonly retryExpectedFailureAfter?: Duration.Input;
|
||||
readonly resubscribe?: Stream.Stream<unknown, never, never>;
|
||||
}
|
||||
@@ -238,6 +252,9 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(
|
||||
EnvironmentRpcStreamValue<TTag>,
|
||||
EnvironmentRpcStreamFailure<TTag>
|
||||
>;
|
||||
// Consecutive expected-failure retries on this session. Reset by
|
||||
// the first value a resubscribed stream delivers.
|
||||
let expectedFailureRetries = 0;
|
||||
const subscribeToSession = (): Stream.Stream<A, EnvironmentRpcStreamFailure<TTag>> =>
|
||||
Stream.suspend(() =>
|
||||
Stream.unwrap(
|
||||
@@ -248,7 +265,13 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(
|
||||
method: tag,
|
||||
input,
|
||||
});
|
||||
const stream = mapStream(session, method(input));
|
||||
const stream = mapStream(session, method(input)).pipe(
|
||||
Stream.onFirst(() =>
|
||||
Effect.sync(() => {
|
||||
expectedFailureRetries = 0;
|
||||
}),
|
||||
),
|
||||
);
|
||||
// An evicted preview host completes its registration stream.
|
||||
// Re-register only after completion; failures still follow the
|
||||
// session recovery policy and browser actions are never replayed.
|
||||
@@ -296,14 +319,27 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(
|
||||
const handled = Stream.fromEffect(options.onExpectedFailure(cause)).pipe(
|
||||
Stream.drain,
|
||||
);
|
||||
if (options.retryExpectedFailureAfter === undefined) {
|
||||
const isAuthorizationFailure = cause.reasons.some(
|
||||
(reason) =>
|
||||
reason._tag === "Fail" && isEnvironmentAuthorizationError(reason.error),
|
||||
);
|
||||
if (
|
||||
options.retryExpectedFailureAfter === undefined ||
|
||||
isAuthorizationFailure
|
||||
) {
|
||||
return handled;
|
||||
}
|
||||
const retryDelay = Duration.millis(
|
||||
Math.min(
|
||||
Duration.toMillis(options.retryExpectedFailureAfter) *
|
||||
2 ** expectedFailureRetries,
|
||||
MAX_EXPECTED_FAILURE_RETRY_DELAY_MS,
|
||||
),
|
||||
);
|
||||
expectedFailureRetries += 1;
|
||||
return handled.pipe(
|
||||
Stream.concat(
|
||||
Stream.fromEffect(Effect.sleep(options.retryExpectedFailureAfter)).pipe(
|
||||
Stream.drain,
|
||||
),
|
||||
Stream.fromEffect(Effect.sleep(retryDelay)).pipe(Stream.drain),
|
||||
),
|
||||
Stream.concat(subscribeToSession()),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user