Skip to content

Commit ff774e4

Browse files
ScottN-PVclaude
andcommitted
fix(server): t3_preview_close reports the server browser's refusal
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 43f8a8d commit ff774e4

3 files changed

Lines changed: 88 additions & 5 deletions

File tree

‎apps/server/src/mcp/toolkits/previewControls/handlers.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { expect, it } from "@effect/vitest";
44
import {
55
DEFAULT_SERVER_SETTINGS,
66
EnvironmentId,
7+
PreviewAutomationControlInterruptedError,
78
ProjectId,
89
ProviderInstanceId,
10+
SERVER_BROWSER_AUTOMATION_CLIENT_ID,
911
ThreadId,
1012
} from "@t3tools/contracts";
1113
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
@@ -101,3 +103,86 @@ it.effect.each([
101103
}),
102104
),
103105
);
106+
107+
it.effect("close reports why the server browser refused to close a tab", () =>
108+
Effect.scoped(
109+
Effect.gen(function* () {
110+
const threadId = ThreadId.make("preview-controls-thread");
111+
const scope: McpInvocationContext.McpInvocationScope = {
112+
environmentId: EnvironmentId.make("preview-controls-environment"),
113+
requestNamespace: "preview-controls-provider-session",
114+
thread: {
115+
threadId,
116+
providerSessionId: "preview-controls-provider-session",
117+
providerInstanceId: ProviderInstanceId.make("codex"),
118+
},
119+
client: undefined,
120+
capabilities: new Set(["preview"]),
121+
issuedAt: 0,
122+
};
123+
const manager = yield* Preview.make.pipe(
124+
Effect.provide(
125+
Layer.merge(
126+
ServerConfig.layerTest(process.cwd(), { prefix: "t3-preview-controls-" }).pipe(
127+
Layer.provide(NodeServices.layer),
128+
),
129+
NodeCrypto.layer,
130+
),
131+
),
132+
);
133+
const tab = yield* manager.open({
134+
threadId,
135+
url: "http://localhost:3000",
136+
runtime: "server",
137+
});
138+
const layerDependencies = Layer.mergeAll(
139+
Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({
140+
invoke: () =>
141+
Effect.fail(
142+
new PreviewAutomationControlInterruptedError({
143+
operation: "close",
144+
environmentId: scope.environmentId,
145+
threadId,
146+
providerSessionId: "preview-controls-provider-session",
147+
providerInstanceId: ProviderInstanceId.make("codex"),
148+
clientId: SERVER_BROWSER_AUTOMATION_CLIENT_ID,
149+
connectionId: "server-browser-connection",
150+
requestId: "close-request",
151+
tabId: tab.tabId,
152+
timeoutMs: 1_000,
153+
remoteTag: "PreviewAutomationControlInterruptedError",
154+
remoteMessageLength: 0,
155+
cause: null,
156+
reason: "agentMismatch",
157+
}),
158+
),
159+
}),
160+
Layer.succeed(Preview.PreviewManager, manager),
161+
Layer.succeed(McpInvocationContext.McpInvocationContext, scope),
162+
McpToolAccessTestkit.liveThreadsLayer,
163+
Layer.mock(ServerSettings.ServerSettingsService)({
164+
getSettings: Effect.succeed({
165+
...DEFAULT_SERVER_SETTINGS,
166+
enableAgentBrowserAccess: true,
167+
}),
168+
}),
169+
);
170+
const toolkit = yield* PreviewControlsToolkit.pipe(
171+
Effect.provide(
172+
McpToolAccess.HandlersLayer.layer(PreviewControlsHandlers.layer).pipe(
173+
Layer.provide(layerDependencies),
174+
),
175+
),
176+
);
177+
178+
const closed = yield* toolkit
179+
.handle("t3_preview_close", { tabId: tab.tabId })
180+
.pipe(Stream.unwrap, Stream.runCollect, Effect.provide(layerDependencies));
181+
182+
expect(closed.at(-1)?.result).toMatchObject({
183+
_tag: "PreviewAutomationControlInterruptedError",
184+
reason: "agentMismatch",
185+
});
186+
}),
187+
),
188+
);

‎apps/server/src/mcp/toolkits/previewControls/handlers.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ export const layer = McpToolAccess.toLayer(PreviewControlsToolkit, {
3333
sessions.some((session) => session.tabId === input.tabId && session.runtime === "server")
3434
) {
3535
const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker;
36-
yield* broker
37-
.invoke({ scope, operation: "close", input: {}, tabId: input.tabId })
38-
.pipe(Effect.mapError(unavailable));
36+
yield* broker.invoke({ scope, operation: "close", input: {}, tabId: input.tabId });
3937
return {};
4038
}
4139
yield* manager

‎apps/server/src/mcp/toolkits/previewControls/tools.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
NonNegativeInt,
33
OrchestratorMcpFailure,
4-
PreviewAutomationUnavailableError,
4+
PreviewAutomationError,
55
PreviewListResult,
66
PreviewTabId,
77
} from "@t3tools/contracts";
@@ -13,7 +13,7 @@ import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
1313
import * as McpInvocationContext from "../../McpInvocationContext.ts";
1414

1515
const shared = {
16-
failure: Schema.Union([OrchestratorMcpFailure, PreviewAutomationUnavailableError]),
16+
failure: Schema.Union([PreviewAutomationError, OrchestratorMcpFailure]),
1717
failureMode: "return" as const,
1818
dependencies: [
1919
McpInvocationContext.McpInvocationContext,

0 commit comments

Comments
 (0)