@devries-cameron20 / t3code Mirror · read-only / commits / 3595b9e038
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 18:30 UTC

fix(server): access list keeps changes made while its stream opens (#35640)

Julius Marminge committed Nov 12, 2026 at 14:48 UTC 3595b9e0384784cd31db46c58e8b7feca09eecbd
2 files changed +133 -4
apps/server/src/ws.test.ts
+114
@@ -3,6 +3,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3 import * as NodeServices from "@effect/platform-node/NodeServices";
4 import { assert, it } from "@effect/vitest";
5 import {
6 + AuthAccessReadScope,
7 AuthOrchestrationOperateScope,
8 AuthOrchestrationReadScope,
9 AuthSessionId,
@@ -22,6 +23,7 @@ import {
23 WsRpcGroup,
24 WsServerRefreshProvidersRpc,
25 WsSubscribeServerConfigRpc,
26 + type AuthAccessStreamEvent,
27 type OrchestrationV2AppThread,
28 type OrchestrationV2DomainEvent,
29 type OrchestrationV2StoredEvent,
@@ -1142,6 +1144,118 @@ it.effect.each([
1144 ),
1145 );
1146
1147 +it.effect("access changes made while the snapshot loads reach the client after it", () =>
1148 + Effect.gen(function* () {
1149 + const pairingChanges = yield* PubSub.unbounded<PairingGrantStore.BootstrapCredentialChange>();
1150 + const sessionChanges = yield* PubSub.unbounded<SessionStore.SessionCredentialChange>();
1151 + const subscribed = yield* Queue.unbounded<void>();
1152 + const subscribe = <A>(changes: PubSub.PubSub<A>) =>
1153 + Stream.unwrap(
1154 + PubSub.subscribe(changes).pipe(
1155 + Effect.tap(() => Queue.offer(subscribed, undefined)),
1156 + Effect.map(Stream.fromSubscription),
1157 + ),
1158 + );
1159 + const revokedElsewhere = AuthSessionId.make("revoked-elsewhere");
1160 + const responses = yield* Queue.unbounded<AuthAccessStreamEvent>();
1161 + const server = yield* RpcServer.makeNoSerialization(WsRpcGroup, {
1162 + ...WS_RPC_SERVER_OPTIONS,
1163 + onFromServer: (response) =>
1164 + response._tag === "Chunk"
1165 + ? Effect.forEach(
1166 + response.values,
1167 + (event) => Queue.offer(responses, event as AuthAccessStreamEvent),
1168 + {
1169 + discard: true,
1170 + },
1171 + )
1172 + : Effect.void,
1173 + }).pipe(
1174 + Effect.provide(
1175 + layerWsRpc(
1176 + {
1177 + sessionId: AuthSessionId.make("current"),
1178 + subject: "current",
1179 + method: "browser-session-cookie",
1180 + scopes: [],
1181 + },
1182 + {},
1183 + yield* ServerBrowser.ServerBrowser,
1184 + () => Effect.void,
1185 + { secretReferences: true },
1186 + ).pipe(
1187 + Layer.merge(RpcAuthorization.layer([AuthAccessReadScope])),
1188 + Layer.provide(
1189 + Layer.mergeAll(
1190 + layerUnusedConfigRpcServices,
1191 + Layer.mock(ExternalLauncher.ExternalLauncher)({}),
1192 + Layer.mock(ProviderRegistry.ProviderRegistry)({}),
1193 + Layer.mock(UsageLimitSources.UsageLimitSources)({}),
1194 + Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({}),
1195 + Layer.mock(DirectEndpoints.DirectEndpoints)({}),
1196 + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
1197 + namedProjectsRoot: "/unused",
1198 + }),
1199 + Layer.mock(ScratchThreadPromotion.ScratchThreadPromotion)({}),
1200 + Layer.mock(ServerEnvironment.ServerEnvironment)({}),
1201 + Layer.mock(EnvironmentAuth.EnvironmentAuth)({
1202 + listPairingLinks: () => Effect.succeed([]),
1203 + // Another client revokes a session after the snapshot read it.
1204 + listClientSessions: () =>
1205 + PubSub.publish(sessionChanges, {
1206 + type: "clientRemoved",
1207 + sessionId: revokedElsewhere,
1208 + }).pipe(Effect.as([])),
1209 + }),
1210 + Layer.mock(PairingGrantStore.PairingGrantStore)({
1211 + streamChanges: subscribe(pairingChanges),
1212 + }),
1213 + Layer.mock(SessionStore.SessionStore)({
1214 + cookieName: "auth-access-test",
1215 + legacyCookieName: undefined,
1216 + streamChanges: subscribe(sessionChanges),
1217 + }),
1218 + ),
1219 + ),
1220 + ),
1221 + ),
1222 + );
1223 + const requestId = RpcMessage.RequestId("auth-access");
1224 + yield* server.write(0, {
1225 + _tag: "Request",
1226 + id: requestId,
1227 + tag: WS_METHODS.subscribeAuthAccess,
1228 + payload: {},
1229 + headers: Headers.empty,
1230 + });
1231 + const snapshot = yield* Queue.take(responses);
1232 + assert.equal(snapshot.type, "snapshot");
1233 + yield* server.write(0, { _tag: "Ack", requestId });
1234 + yield* Queue.take(subscribed);
1235 + yield* Queue.take(subscribed);
1236 +
1237 + // A later change arrives either way, so a dropped revocation fails as a mismatch.
1238 + yield* PubSub.publish(pairingChanges, { type: "pairingLinkRemoved", id: "later" });
1239 + const next = yield* Queue.take(responses);
1240 + assert.deepEqual(next, {
1241 + version: 1,
1242 + revision: 2,
1243 + type: "clientRemoved",
1244 + payload: { sessionId: revokedElsewhere },
1245 + });
1246 + yield* server.write(0, { _tag: "Interrupt", requestId, interruptors: [] });
1247 + }).pipe(
1248 + Effect.provide(
1249 + Layer.mergeAll(
1250 + Layer.fresh(layerConfigSources),
1251 + Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({}),
1252 + Layer.mock(ServerBrowser.ServerBrowser)({}),
1253 + ),
1254 + ),
1255 + Effect.scoped,
1256 + ),
1257 +);
1258 +
1259 it.effect("on-demand clients get provider config without workspace snapshots", () =>
1260 Effect.gen(function* () {
1261 const instanceId = ProviderInstanceId.make("claudeAgent");
apps/server/src/ws.ts
+19 -4
@@ -3443,13 +3443,28 @@ export const layerWsRpc = (
3443 [WS_METHODS.subscribeAuthAccess]: (_input) =>
3444 Stream.unwrap(
3445 Effect.gen(function* () {
3446 + const liveBuffer = yield* Queue.unbounded<
3447 + PairingGrantStore.BootstrapCredentialChange | SessionStore.SessionCredentialChange
3448 + >();
3449 + // Each store subscribes only when consumed, so start both before the snapshot read.
3450 + yield* Effect.forkScoped(
3451 + Stream.runForEach(bootstrapCredentials.streamChanges, (change) =>
3452 + Queue.offer(liveBuffer, change),
3453 + ),
3454 + { startImmediately: true },
3455 + );
3456 + yield* Effect.forkScoped(
3457 + Stream.runForEach(sessions.streamChanges, (change) =>
3458 + Queue.offer(liveBuffer, change),
3459 + ),
3460 + { startImmediately: true },
3461 + );
3462 const initialSnapshot = yield* loadAuthAccessSnapshot();
3463 const revisionRef = yield* Ref.make(1);
3448 - const accessChanges: Stream.Stream<
3449 - PairingGrantStore.BootstrapCredentialChange | SessionStore.SessionCredentialChange
3450 - > = Stream.merge(bootstrapCredentials.streamChanges, sessions.streamChanges);
3464
3452 - const liveEvents: Stream.Stream<AuthAccessStreamEvent> = accessChanges.pipe(
3465 + const liveEvents: Stream.Stream<AuthAccessStreamEvent> = Stream.fromQueue(
3466 + liveBuffer,
3467 + ).pipe(
3468 Stream.mapEffect((change) =>
3469 Ref.updateAndGet(revisionRef, (revision) => revision + 1).pipe(
3470 Effect.map((revision) =>