test(server): cover the agent browser access gate

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) <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 5 committed 2026-08-15 17:21:57 +02:00
1 parent 4d200eca1d
commit d9388442be
2 files changed
+82 -5

No files matched your search

@@ -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<ThreadId> = [];
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)),
);
});
@@ -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<Name extends keyof ProviderService.ProviderService["Service"]> =
@@ -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<ProviderRuntimeEvent>();
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));
}