From d9388442be1ed197407d5aa65ce23e9dbe72bf54 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 15 Aug 2026 17:21:57 +0200 Subject: [PATCH] test(server): cover the agent browser access gate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The check flagged an untested backend behavior change, and it was right — withholding the MCP credential is the whole feature, and nothing asserted it. Credential issuance is the observable that matters: it is the only place a credential is minted and `/mcp` accepts nothing else, so withholding it is what actually denies every provider and any external MCP client. Testing it needed a seam. The real issuer reads a module-global registry that only a running MCP server installs, so from a unit test the enabled and disabled paths look identical — both leave no session behind. Reading the stored session back would have passed either way, which is worse than no test. `ProviderServiceLiveOptions` already carries a test override for the canonical event logger, so the issuer follows that existing pattern. Co-Authored-By: Claude Opus 5 (1M context) --- .../provider/Layers/ProviderService.test.ts | 73 ++++++++++++++++++- .../src/provider/Layers/ProviderService.ts | 14 +++- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/apps/server/src/provider/Layers/ProviderService.test.ts b/apps/server/src/provider/Layers/ProviderService.test.ts index 7334cd0197..87b5971009 100644 --- a/apps/server/src/provider/Layers/ProviderService.test.ts +++ b/apps/server/src/provider/Layers/ProviderService.test.ts @@ -12,6 +12,7 @@ import type { } from "@t3tools/contracts"; import { ApprovalRequestId, + EnvironmentId, EventId, ProviderDriverKind, ProviderInstanceId, @@ -20,7 +21,7 @@ import { TurnId, } from "@t3tools/contracts"; import { createModelSelection } from "@t3tools/shared/model"; -import { it, assert, vi } from "@effect/vitest"; +import { it, assert, describe, vi } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; @@ -1957,3 +1958,73 @@ validation.layer("ProviderServiceLive validation", (it) => { }), ); }); + +describe("agent browser access", () => { + const startSessionWith = (enableAgentBrowserAccess: boolean, threadId: ThreadId) => + Effect.gen(function* () { + const issued: Array = []; + const codex = makeFakeCodexAdapter(); + const providerAdapterLayer = Layer.succeed( + ProviderAdapterRegistry.ProviderAdapterRegistry, + makeAdapterRegistryMock({ [CODEX_DRIVER]: codex.adapter }), + ); + const runtimeRepositoryLayer = ProviderSessionRuntime.layer.pipe( + Layer.provide(SqlitePersistenceMemory), + ); + const directoryLayer = ProviderSessionDirectoryLive.pipe( + Layer.provide(runtimeRepositoryLayer), + ); + const providerLayer = makeProviderServiceLive({ + issueMcpCredential: (request) => + Effect.sync(() => { + issued.push(request.threadId); + return undefined; + }), + }).pipe( + Layer.provide(providerAdapterLayer), + Layer.provide(directoryLayer), + Layer.provide(ServerSettings.ServerSettingsService.layerTest({ enableAgentBrowserAccess })), + Layer.provide(serverConfigTestLayer), + Layer.provide(AnalyticsService.layerTest), + Layer.provide( + Layer.succeed( + ProviderEventLoggers.ProviderEventLoggers, + ProviderEventLoggers.NoOpProviderEventLoggers, + ), + ), + ); + + yield* Effect.gen(function* () { + const provider = yield* ProviderService.ProviderService; + return yield* provider.startSession(threadId, { + provider: CODEX_DRIVER, + providerInstanceId: codexInstanceId, + threadId, + runtimeMode: "full-access", + }); + }).pipe(Effect.provide(providerLayer)); + + return issued; + }); + + // Credential issuance is the observable that matters: it is the only place a + // credential is minted, and `/mcp` accepts nothing else, so withholding it is + // what actually denies every provider and external MCP client. + it.effect("requests no MCP credential when agent browser access is off", () => + Effect.gen(function* () { + const issued = yield* startSessionWith(false, asThreadId("thread-browser-off")); + + assert.deepEqual(issued, []); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.effect("requests an MCP credential when agent browser access is on", () => + Effect.gen(function* () { + const threadId = asThreadId("thread-browser-on"); + + const issued = yield* startSessionWith(true, threadId); + + assert.deepEqual(issued, [threadId]); + }).pipe(Effect.provide(NodeServices.layer)), + ); +}); diff --git a/apps/server/src/provider/Layers/ProviderService.ts b/apps/server/src/provider/Layers/ProviderService.ts index 4974505d67..6953a1f24d 100644 --- a/apps/server/src/provider/Layers/ProviderService.ts +++ b/apps/server/src/provider/Layers/ProviderService.ts @@ -67,6 +67,13 @@ const isModelSelection = Schema.is(ModelSelection); */ export interface ProviderServiceLiveOptions { readonly canonicalEventLogger?: EventNdjsonLogger; + /** + * Overrides MCP credential issuance. The real issuer reads a module-global + * registry that only a running MCP server installs, which makes the + * agent-browser-access gate unobservable from a unit test; this seam lets a + * test see whether a credential was requested at all. + */ + readonly issueMcpCredential?: typeof McpSessionRegistry.issueActiveMcpCredential; } type ProviderServiceMethod = @@ -217,6 +224,8 @@ const makeProviderService = Effect.fn("makeProviderService")(function* ( const registry = yield* ProviderAdapterRegistry.ProviderAdapterRegistry; const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory; const serverSettings = yield* ServerSettings.ServerSettingsService; + const issueMcpCredential = + options?.issueMcpCredential ?? McpSessionRegistry.issueActiveMcpCredential; const runtimeEventPubSub = yield* PubSub.unbounded(); const nowIso = Effect.map(DateTime.now, DateTime.formatIso); /** @@ -253,10 +262,7 @@ const makeProviderService = Effect.fn("makeProviderService")(function* ( yield* Effect.sync(() => McpProviderSession.clearMcpProviderSession(threadId)); return undefined; } - const credential = yield* McpSessionRegistry.issueActiveMcpCredential({ - threadId, - providerInstanceId, - }); + const credential = yield* issueMcpCredential({ threadId, providerInstanceId }); if (credential) { yield* Effect.sync(() => McpProviderSession.setMcpProviderSession(credential.config)); }