Repository navigation
fix(client): retry the permissions check after a reconnect #27008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juliusmarminge
merged 2 commits into
main
from
t3/session-permissions-refetch-on-connect
Oct 29, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { EnvironmentId, type AuthSessionState } from "@t3tools/contracts"; | ||
| import * as Clock from "effect/Clock"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Queue from "effect/Queue"; | ||
| import * as Stream from "effect/Stream"; | ||
| import * as SubscriptionRef from "effect/SubscriptionRef"; | ||
| import * as TestClock from "effect/testing/TestClock"; | ||
| import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; | ||
|
|
||
| import { | ||
| AVAILABLE_CONNECTION_STATE, | ||
| PrimaryConnectionTarget, | ||
| type PreparedConnection, | ||
| } from "../connection/model.ts"; | ||
| import * as EnvironmentRegistry from "../connection/registry.ts"; | ||
| import * as EnvironmentSupervisor from "../connection/supervisor.ts"; | ||
| import * as RpcHttp from "../rpc/http.ts"; | ||
| import type { RpcSession } from "../rpc/session.ts"; | ||
| import { createEnvironmentSessionAtoms } from "./session.ts"; | ||
|
|
||
| const TARGET = new PrimaryConnectionTarget({ | ||
| environmentId: EnvironmentId.make("environment-1"), | ||
| label: "Test environment", | ||
| httpBaseUrl: "https://environment.example.test", | ||
| wsBaseUrl: "wss://environment.example.test", | ||
| }); | ||
| const PREPARED: PreparedConnection = { | ||
| environmentId: TARGET.environmentId, | ||
| label: TARGET.label, | ||
| httpBaseUrl: "https://environment.example.test", | ||
| socketUrl: "wss://environment.example.test/ws", | ||
| httpAuthorization: { _tag: "Bearer", token: "bearer-token" }, | ||
| target: TARGET, | ||
| }; | ||
| const SESSION = { | ||
| authenticated: true, | ||
| auth: { | ||
| policy: "remote-reachable", | ||
| bootstrapMethods: [], | ||
| sessionMethods: [], | ||
| sessionCookieName: "t3_session", | ||
| }, | ||
| scopes: ["orchestration:read"], | ||
| } satisfies AuthSessionState; | ||
|
|
||
| const rpcSession = (): RpcSession => ({ closed: Effect.never }) as unknown as RpcSession; | ||
|
|
||
| const makeHarness = Effect.fn("TestSessionState.makeHarness")(function* ( | ||
| reply: (requestNumber: number) => Promise<Response>, | ||
| ) { | ||
| let requestCount = 0; | ||
| const transport = yield* SubscriptionRef.make(Option.some(rpcSession())); | ||
| const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ | ||
| target: TARGET, | ||
| state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), | ||
| session: transport, | ||
| prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), | ||
| connect: Effect.void, | ||
| disconnect: Effect.void, | ||
| retryNow: Effect.void, | ||
| } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); | ||
| const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = ( | ||
| _environmentId, | ||
| stream, | ||
| ) => Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor); | ||
| const runtime = Atom.runtime( | ||
| Layer.mergeAll( | ||
| Layer.succeed( | ||
| EnvironmentRegistry.EnvironmentRegistry, | ||
| EnvironmentRegistry.EnvironmentRegistry.of({ | ||
| followStream, | ||
| } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]), | ||
| ), | ||
| RpcHttp.layerRemoteHttpClient(async () => reply(++requestCount)), | ||
| // The test's clock, so the retry wait moves with `TestClock.adjust`. | ||
| Layer.succeed(Clock.Clock, yield* Clock.Clock), | ||
| ), | ||
| ); | ||
| const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) => | ||
| Effect.sync(() => registry.dispose()), | ||
| ); | ||
| const results = yield* Queue.unbounded<AsyncResult.AsyncResult<AuthSessionState, unknown>>(); | ||
| const atom = createEnvironmentSessionAtoms(runtime).sessionStateAtom(TARGET.environmentId); | ||
| const unsubscribe = registry.subscribe(atom, (result) => Queue.offerUnsafe(results, result), { | ||
| immediate: true, | ||
| }); | ||
| yield* Effect.addFinalizer(() => Effect.sync(unsubscribe)); | ||
| const nextSettled = Effect.fn("TestSessionState.nextSettled")(function* () { | ||
| for (;;) { | ||
| const result = yield* Queue.take(results); | ||
| if (!result.waiting && result._tag !== "Initial") return result; | ||
| } | ||
| }); | ||
| return { transport, nextSettled }; | ||
| }); | ||
|
|
||
| const unreachable = () => Promise.reject(new TypeError("Network request failed")); | ||
|
|
||
| describe("environment session state", () => { | ||
| it.effect("fetches permissions again when the connection is re-established", () => | ||
| Effect.gen(function* () { | ||
| const harness = yield* makeHarness((requestNumber) => | ||
| requestNumber === 1 ? unreachable() : Promise.resolve(Response.json(SESSION)), | ||
| ); | ||
| expect((yield* harness.nextSettled())._tag).toBe("Failure"); | ||
|
|
||
| yield* SubscriptionRef.set(harness.transport, Option.some(rpcSession())); | ||
|
|
||
| expect(yield* harness.nextSettled()).toMatchObject({ _tag: "Success", value: SESSION }); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("retries a failed permission check with backoff that restarts per connection", () => | ||
| Effect.gen(function* () { | ||
| const harness = yield* makeHarness((requestNumber) => | ||
| requestNumber <= 3 ? unreachable() : Promise.resolve(Response.json(SESSION)), | ||
| ); | ||
| expect((yield* harness.nextSettled())._tag).toBe("Failure"); | ||
|
|
||
| yield* TestClock.adjust("2 seconds"); | ||
| expect((yield* harness.nextSettled())._tag).toBe("Failure"); | ||
|
|
||
| yield* SubscriptionRef.set(harness.transport, Option.some(rpcSession())); | ||
| expect((yield* harness.nextSettled())._tag).toBe("Failure"); | ||
|
|
||
| yield* TestClock.adjust("2 seconds"); | ||
| expect(yield* harness.nextSettled()).toMatchObject({ _tag: "Success", value: SESSION }); | ||
| }), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.