Skip to content

Commit 79b178b

Browse files
fix(client): reset permission retry backoff per connection and name rejected sessions
A reconnect now restarts the retry backoff, and an environment that answers authenticated:false gets its own message instead of promising a retry that never runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a266d28 commit 79b178b

6 files changed

Lines changed: 27 additions & 8 deletions

File tree

‎apps/mobile/src/features/settings/EnvironmentRoutesSection.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ export function EnvironmentRoutesSection({
168168
<Text className="text-sm text-foreground-muted">
169169
{sessionPermissionsUnavailableMessage({
170170
connected: activeRouteId !== null,
171-
failed:
172-
sessionResult._tag === "Failure" ||
173-
(!sessionResult.waiting && session?.authenticated === false),
171+
failed: sessionResult._tag === "Failure",
172+
rejected:
173+
sessionResult._tag !== "Failure" &&
174+
!sessionResult.waiting &&
175+
session?.authenticated === false,
174176
})}
175177
</Text>
176178
)}

‎apps/mobile/src/features/settings/SettingsEnvironmentDetailRouteScreen.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,12 @@ function EnvironmentDetail({ environmentId }: { readonly environmentId: Environm
209209
</Text>
210210
) : !allowed ? (
211211
<Text className="px-2 text-sm text-foreground-muted">
212-
{AsyncResult.isFailure(sessionResult) || session === null
212+
{AsyncResult.isFailure(sessionResult) || session === null || !session.authenticated
213213
? sessionPermissionsUnavailableMessage({
214214
connected,
215215
failed: AsyncResult.isFailure(sessionResult),
216+
rejected:
217+
!AsyncResult.isFailure(sessionResult) && session?.authenticated === false,
216218
})
217219
: "This connection does not have permission to manage the environment."}
218220
</Text>

‎apps/web/src/components/settings/SessionPermissions.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export function SessionPermissions({
3737
<p className="py-3 text-xs text-muted-foreground">
3838
{sessionPermissionsUnavailableMessage({
3939
connected,
40-
failed: session.hasError || (!session.isPending && session.data?.authenticated === false),
40+
failed: session.hasError,
41+
rejected: !session.hasError && !session.isPending && session.data?.authenticated === false,
4142
})}
4243
</p>
4344
);

‎packages/client-runtime/src/state/session.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,20 @@ describe("environment session state", () => {
113113
}),
114114
);
115115

116-
it.effect("retries a failed permission check while the connection stays up", () =>
116+
it.effect("retries a failed permission check with backoff that restarts per connection", () =>
117117
Effect.gen(function* () {
118118
const harness = yield* makeHarness((requestNumber) =>
119-
requestNumber <= 2 ? unreachable() : Promise.resolve(Response.json(SESSION)),
119+
requestNumber <= 3 ? unreachable() : Promise.resolve(Response.json(SESSION)),
120120
);
121121
expect((yield* harness.nextSettled())._tag).toBe("Failure");
122122

123123
yield* TestClock.adjust("2 seconds");
124124
expect((yield* harness.nextSettled())._tag).toBe("Failure");
125125

126-
yield* TestClock.adjust("4 seconds");
126+
yield* SubscriptionRef.set(harness.transport, Option.some(rpcSession()));
127+
expect((yield* harness.nextSettled())._tag).toBe("Failure");
128+
129+
yield* TestClock.adjust("2 seconds");
127130
expect(yield* harness.nextSettled()).toMatchObject({ _tag: "Success", value: SESSION });
128131
}),
129132
);

‎packages/client-runtime/src/state/session.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { PreparedConnection } from "../connection/model.ts";
1414
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
1515
import * as ManagedRelay from "../relay/managedRelay.ts";
1616
import type { RemoteEnvironmentRequestError } from "../rpc/http.ts";
17+
import type { RpcSession } from "../rpc/session.ts";
1718
import { safeErrorLogAttributes } from "../errors/safeLog.ts";
1819
import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
1920
import { followStreamInEnvironment } from "./environmentStreams.ts";
@@ -154,6 +155,7 @@ function makeEnvironmentSessionAtoms<R, E>(
154155

155156
const sessionStateAtom = Atom.family((environmentId: EnvironmentId) => {
156157
let consecutiveFailures = 0;
158+
let backoffConnection: RpcSession | null = null;
157159
return runtime
158160
.atom((get) => {
159161
const prepared = Option.getOrNull(get(preparedConnectionValueAtom(environmentId)));
@@ -165,6 +167,10 @@ function makeEnvironmentSessionAtoms<R, E>(
165167
if (prepared === null || Option.isNone(connection)) {
166168
return Effect.never;
167169
}
170+
if (connection.value !== backoffConnection) {
171+
backoffConnection = connection.value;
172+
consecutiveFailures = 0;
173+
}
168174
return Effect.gen(function* () {
169175
const signer = yield* Effect.serviceOption(ManagedRelay.ManagedRelayDpopSigner);
170176
const remoteAuthorization = yield* Effect.serviceOption(

‎packages/shared/src/authScopeOptions.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,14 @@ export const AUTH_SCOPE_OPTIONS: ReadonlyArray<{
108108
/** Shown instead of the granted scopes while they are unknown, so every surface says the same thing. */
109109
export function sessionPermissionsUnavailableMessage(input: {
110110
readonly connected: boolean;
111+
/** The check errored; the client retries it while connected. */
111112
readonly failed: boolean;
113+
/** The environment answered that this session is not signed in; retrying will not change that. */
114+
readonly rejected: boolean;
112115
}): string {
113116
if (!input.connected) return "Connect to this environment to view this session’s permissions.";
117+
if (input.rejected)
118+
return "This environment did not accept your session. Reconnect to try again.";
114119
return input.failed
115120
? "Could not verify your permissions. Retrying automatically."
116121
: "Checking permissions…";

0 commit comments

Comments
 (0)