Skip to content

Commit 7c3e343

Browse files
authored
Merge branch 'main' into t3code/631bed8d
2 parents 106de31 + b38aa18 commit 7c3e343

9 files changed

Lines changed: 211 additions & 14 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ jobs:
330330
# No Electron setup here: `t3` (apps/server) has no Electron dependency
331331
# and none of its tests touch the runtime. Only the non-server `test`
332332
# job, which covers @t3tools/desktop, needs the download.
333+
- name: Install Chromium for server browser integration tests
334+
run: node apps/server/node_modules/playwright-core/cli.js install --with-deps --only-shell chromium
335+
333336
- name: Test
334337
run: vp run --filter t3 test --shard ${{ matrix.shard }}/${{ strategy.job-total }}
335338

‎apps/server/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
},
5252
"devDependencies": {
5353
"@effect/vitest": "catalog:",
54+
"@modelcontextprotocol/sdk": "1.29.0",
5455
"@t3tools/contracts": "workspace:*",
5556
"@t3tools/shared": "workspace:*",
5657
"@t3tools/ssh": "workspace:*",

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { describe, expect, it } from "@effect/vitest";
3+
import { AjvJsonSchemaValidator } from "@modelcontextprotocol/sdk/validation/ajv";
4+
import type { JsonSchemaType } from "@modelcontextprotocol/sdk/validation";
35
import {
46
CommandId,
57
EnvironmentId,
@@ -765,7 +767,22 @@ describe("orchestrator MCP toolkit", () => {
765767
const invoke = (name: string, args: Record<string, unknown>) =>
766768
invokeAs(invocation, name, args);
767769

770+
const refusedSettle = yield* invoke("t3_thread_organize", { action: "settle" });
771+
expect(refusedSettle.isError).toBe(true);
772+
expect(refusedSettle.structuredContent).toBeUndefined();
773+
expect(declaredFailure(refusedSettle)).toEqual({
774+
_tag: "OrchestratorMcpFailure",
775+
code: "orchestration_error",
776+
message: `Thread ${parentThreadId} has active or blocked work and cannot be settled.`,
777+
});
778+
const afterRefusedSettle = yield* orchestrator.getThreadProjection(parentThreadId);
779+
expect(afterRefusedSettle.thread.settledOverride).not.toBe("settled");
780+
expect(afterRefusedSettle.runs.find((run) => run.id === parentRun?.id)?.status).toBe(
781+
"running",
782+
);
783+
768784
const pinned = yield* invoke("t3_thread_organize", { action: "pin" });
785+
expect(pinned.isError).toBe(false);
769786
expect(pinned.structuredContent).toHaveProperty("sequence");
770787
expect((yield* orchestrator.getThreadShell(parentThreadId))?.pinnedAt).not.toBeNull();
771788
yield* invoke("t3_thread_organize", { action: "unpin" });
@@ -774,6 +791,20 @@ describe("orchestrator MCP toolkit", () => {
774791
if (parentRun === undefined || parentRun.rootNodeId === null) {
775792
return yield* Effect.die(new Error("Parent run missing."));
776793
}
794+
for (const name of ["t3_queue_edit", "t3_queue_cancel"]) {
795+
const refusedQueueMutation = yield* invoke(name, {
796+
queuedRunId: parentRun.id,
797+
...(name === "t3_queue_edit" ? { text: "Keep the active turn." } : {}),
798+
});
799+
expect(refusedQueueMutation.isError).toBe(true);
800+
expect(refusedQueueMutation.structuredContent).toBeUndefined();
801+
expect(declaredFailure(refusedQueueMutation)).toEqual({
802+
_tag: "OrchestratorMcpFailure",
803+
code: "orchestration_error",
804+
message: `Run ${parentRun.id} is not queued.`,
805+
});
806+
}
807+
777808
let parentRootNodeId = parentRun.rootNodeId;
778809
const queueAutomaticCompletion = (suffix: string, taskText: string) =>
779810
Effect.gen(function* () {
@@ -1211,10 +1242,36 @@ describe("orchestrator MCP toolkit", () => {
12111242
return yield* Effect.die(new Error("Queued user follow-up missing."));
12121243
}
12131244
const queueFirstPage = yield* invoke("t3_queue_list", { limit: 1 });
1245+
expect(queueFirstPage.isError).toBe(false);
12141246
expect(queueFirstPage.structuredContent).toMatchObject({
12151247
items: [{ queuedRunId: queueRace.queuedRun.id }],
12161248
nextCursor: 1,
12171249
});
1250+
const queueDefinition = server.tools.find(({ tool }) => tool.name === "t3_queue_list");
1251+
const validateQueue = new AjvJsonSchemaValidator().getValidator(
1252+
queueDefinition!.tool.outputSchema! as JsonSchemaType,
1253+
);
1254+
expect(validateQueue(queueFirstPage.structuredContent).valid).toBe(true);
1255+
const missingThreadId = ThreadId.make("00000000-0000-4000-8000-000000000000");
1256+
const missingThreadQueue = yield* invoke("t3_queue_list", {
1257+
threadId: missingThreadId,
1258+
limit: 1,
1259+
});
1260+
expect(missingThreadQueue.isError).toBe(true);
1261+
expect(declaredFailure(missingThreadQueue)).toMatchObject({
1262+
_tag: "OrchestratorMcpFailure",
1263+
code: "thread_not_found",
1264+
message: "The thread was not found.",
1265+
});
1266+
expect(missingThreadQueue.structuredContent).toBeUndefined();
1267+
expect(validateQueue({ items: "invalid", nextCursor: null }).valid).toBe(false);
1268+
const missingThreadRead = yield* invoke("t3_thread_read", {
1269+
threadId: missingThreadId,
1270+
});
1271+
expect(missingThreadRead.isError).toBe(true);
1272+
expect(declaredFailure(missingThreadRead)).toMatchObject({
1273+
_tag: "OrchestratorMcpFailure",
1274+
});
12181275
const queueSecondPage = yield* invoke("t3_queue_list", { cursor: 1, limit: 1 });
12191276
expect(queueSecondPage.structuredContent).toEqual({
12201277
items: [{ queuedRunId: queuedUserRun.id, text: "🙂".repeat(1000), truncated: true }],

‎apps/server/src/mcp/threadAccess.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import * as Crypto from "effect/Crypto";
1212
import * as Effect from "effect/Effect";
1313

14+
import type { OrchestratorV2Error } from "../orchestration-v2/Orchestrator.ts";
1415
import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
1516
import * as OrchestrationMcp from "./OrchestratorMcpService.ts";
1617
import * as McpInvocationContext from "./McpInvocationContext.ts";
@@ -21,6 +22,18 @@ export const unavailable = () =>
2122
message: "The operation could not be completed.",
2223
});
2324

25+
/** Decider string rejections are public; wrapped storage and hydration causes are not. */
26+
export const dispatchFailure = (error: OrchestratorV2Error) =>
27+
(error._tag === "OrchestratorDispatchError" ||
28+
error._tag === "OrchestratorCommandRejectedError") &&
29+
typeof error.cause === "string" &&
30+
error.cause.length > 0
31+
? new OrchestratorMcpFailure({
32+
code: "orchestration_error",
33+
message: Array.from(error.cause).slice(0, 1000).join(""),
34+
})
35+
: unavailable();
36+
2437
/**
2538
* The most a caller may hand to the threads it targets. A thread caller is
2639
* capped by its own thread's modes; an OAuth client by the ceiling chosen

‎apps/server/src/mcp/toolkits/core.test.ts‎

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { expect, it } from "@effect/vitest";
4+
import { AjvJsonSchemaValidator } from "@modelcontextprotocol/sdk/validation/ajv";
5+
import type { JsonSchemaType } from "@modelcontextprotocol/sdk/validation";
46
import {
57
DEFAULT_SERVER_SETTINGS,
68
ChatImageAttachment,
9+
CommandId,
710
EnvironmentId,
811
ProviderInstanceId,
912
RunId,
@@ -17,8 +20,13 @@ import { McpAttachmentInput } from "./attachment/input.ts";
1720
import { McpSchema, McpServer, Tool } from "effect/ai";
1821
import { FetchHttpClient } from "effect/http";
1922

23+
import {
24+
OrchestratorCommandRejectedError,
25+
OrchestratorDispatchError,
26+
OrchestratorProjectionError,
27+
} from "../../orchestration-v2/Orchestrator.ts";
28+
2029
import * as ServerConfig from "../../config.ts";
21-
import { OrchestratorProjectionError } from "../../orchestration-v2/Orchestrator.ts";
2230
import * as ProviderAdapterRegistry from "../../orchestration-v2/ProviderAdapterRegistry.ts";
2331
import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts";
2432
import * as PreviewBrowser from "../../preview/PreviewBrowser.ts";
@@ -28,6 +36,7 @@ import * as SecretRequests from "../../secrets/SecretRequests.ts";
2836
import * as ScheduledTaskService from "../../scheduledTasks/ScheduledTaskService.ts";
2937
import * as McpHttpServer from "../McpHttpServer.ts";
3038
import * as McpInvocationContext from "../McpInvocationContext.ts";
39+
import { dispatchFailure } from "../threadAccess.ts";
3140
import { OrchestratorToolkit } from "./orchestrator/tools.ts";
3241
import { PreviewToolkit } from "./preview/tools.ts";
3342
import { PreviewControlsToolkit } from "./previewControls/tools.ts";
@@ -136,6 +145,8 @@ it.effect("checks capability before accessing services through the production re
136145
Effect.provideService(McpSchema.McpServerClient, client),
137146
);
138147
expect(declaredFailure(result)).toMatchObject({ code: "capability_denied" });
148+
expect(result.isError).toBe(true);
149+
expect(result.structuredContent).toBeUndefined();
139150
}).pipe(
140151
Effect.provide(
141152
McpHttpServer.layerThreadToolkit.pipe(
@@ -161,6 +172,22 @@ it.effect("returns a bounded public failure without serializing storage causes",
161172
code: "orchestration_error",
162173
message: "The operation could not be completed.",
163174
});
175+
expect(result.isError).toBe(true);
176+
expect(result.content).toEqual([
177+
{
178+
type: "text",
179+
text: '{"_tag":"OrchestratorMcpFailure","code":"orchestration_error","message":"The operation could not be completed."}',
180+
},
181+
]);
182+
const definition = server.tools.find(({ tool }) => tool.name === "t3_thread_organize");
183+
expect(definition?.tool.outputSchema).toBeDefined();
184+
const validate = new AjvJsonSchemaValidator().getValidator(
185+
definition!.tool.outputSchema! as JsonSchemaType,
186+
);
187+
expect(result.structuredContent).toBeUndefined();
188+
expect(validate({ sequence: 1 }).valid).toBe(true);
189+
expect(validate({ code: "orchestration_error" }).valid).toBe(false);
190+
expect(validate({ sequence: "invalid" }).valid).toBe(false);
164191
}).pipe(
165192
Effect.provide(
166193
McpHttpServer.layerThreadToolkit.pipe(
@@ -182,6 +209,42 @@ it.effect("returns a bounded public failure without serializing storage causes",
182209
),
183210
);
184211

212+
it("bounds public command rejections and redacts internal dispatch causes", () => {
213+
const command = { commandId: CommandId.make("mcp-core-command"), commandType: "thread.settle" };
214+
expect(
215+
dispatchFailure(new OrchestratorDispatchError({ ...command, cause: "🙂".repeat(1001) }))
216+
.message,
217+
).toBe("🙂".repeat(1000));
218+
expect(
219+
dispatchFailure(
220+
new OrchestratorCommandRejectedError({ ...command, cause: "Run is not queued." }),
221+
).message,
222+
).toBe("Run is not queued.");
223+
for (const cause of [
224+
undefined,
225+
"",
226+
new Error("private-storage-path"),
227+
{ message: "private-storage-path" },
228+
]) {
229+
expect(dispatchFailure(new OrchestratorDispatchError({ ...command, cause }))).toMatchObject({
230+
code: "orchestration_error",
231+
message: "The operation could not be completed.",
232+
});
233+
expect(
234+
dispatchFailure(new OrchestratorCommandRejectedError({ ...command, cause })),
235+
).toMatchObject({
236+
code: "orchestration_error",
237+
message: "The operation could not be completed.",
238+
});
239+
}
240+
expect(
241+
dispatchFailure(new OrchestratorProjectionError({ threadId, cause: "private-storage-path" })),
242+
).toMatchObject({
243+
code: "orchestration_error",
244+
message: "The operation could not be completed.",
245+
});
246+
});
247+
185248
it.effect("returns an HTML render reference that Codex and Claude tool rows both carry", () =>
186249
Effect.gen(function* () {
187250
const server = yield* McpServer.McpServer;
@@ -229,6 +292,57 @@ it.effect("returns an HTML render reference that Codex and Claude tool rows both
229292
),
230293
);
231294

295+
it.effect("returns invalid parameter errors through the production registration", () =>
296+
Effect.gen(function* () {
297+
const server = yield* McpServer.McpServer;
298+
const error = yield* server
299+
.callTool({ name: "t3_thread_organize", arguments: { action: "invalid" } })
300+
.pipe(
301+
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
302+
Effect.provideService(McpSchema.McpServerClient, client),
303+
Effect.flip,
304+
);
305+
expect(error._tag).toBe("InvalidParams");
306+
}).pipe(
307+
Effect.provide(
308+
McpHttpServer.layerThreadToolkit.pipe(
309+
Layer.provideMerge(McpServer.McpServer.layer),
310+
Layer.provide(NodeCrypto.layer),
311+
Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})),
312+
),
313+
),
314+
),
315+
);
316+
317+
it.effect("keeps unexpected handler defects private through the production registration", () =>
318+
Effect.gen(function* () {
319+
const server = yield* McpServer.McpServer;
320+
const result = yield* server
321+
.callTool({ name: "t3_thread_organize", arguments: { action: "pin" } })
322+
.pipe(
323+
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
324+
Effect.provideService(McpSchema.McpServerClient, client),
325+
);
326+
expect(result.isError).toBe(true);
327+
expect(result.structuredContent).toBeUndefined();
328+
expect(result.content).toEqual([
329+
{ type: "text", text: "Tool execution failed due to an internal server error." },
330+
]);
331+
}).pipe(
332+
Effect.provide(
333+
McpHttpServer.layerThreadToolkit.pipe(
334+
Layer.provideMerge(McpServer.McpServer.layer),
335+
Layer.provide(NodeCrypto.layer),
336+
Layer.provide(
337+
Layer.mock(ThreadManagement.ThreadManagementService)({
338+
getThreadShell: () => Effect.die(new Error("private-storage-path")),
339+
}),
340+
),
341+
),
342+
),
343+
),
344+
);
345+
232346
it("keeps MCP preference output allowlisted and Unicode-bounded", () => {
233347
const settings = {
234348
...DEFAULT_SERVER_SETTINGS,

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Effect from "effect/Effect";
1111
import { modelSelectionCommandType } from "@t3tools/shared/model";
1212

1313
import {
14+
dispatchFailure,
1415
newCommandId,
1516
readCaller,
1617
readFullAccessCaller,
@@ -45,7 +46,7 @@ const dispatch = Effect.fn("mcp.dispatchThreadCommand")(function* (
4546
const { threads, projection } = yield* readWritableThread(threadId);
4647
const result = yield* threads
4748
.dispatch(command({ commandId: yield* newCommandId(), threadId: projection.thread.id }))
48-
.pipe(Effect.mapError(unavailable));
49+
.pipe(Effect.mapError(dispatchFailure));
4950
return { sequence: result.sequence };
5051
});
5152

@@ -131,7 +132,7 @@ export const layer = ThreadToolkit.toLayer({
131132
createdBy: "agent",
132133
creationSource: "mcp",
133134
})
134-
.pipe(Effect.mapError(unavailable));
135+
.pipe(Effect.mapError(dispatchFailure));
135136
return { sequence: result.sequence, targetThreadId };
136137
}),
137138
t3_thread_merge_back: (input) =>
@@ -148,7 +149,7 @@ export const layer = ThreadToolkit.toLayer({
148149
createdBy: "agent",
149150
creationSource: "mcp",
150151
})
151-
.pipe(Effect.mapError(unavailable));
152+
.pipe(Effect.mapError(dispatchFailure));
152153
return { sequence: result.sequence, targetThreadId: input.targetThreadId };
153154
}),
154155
t3_thread_transfers: (input) =>
@@ -191,7 +192,7 @@ export const layer = ThreadToolkit.toLayer({
191192
commandId: yield* newCommandId(),
192193
modelSelection: input.modelSelection,
193194
})
194-
.pipe(Effect.mapError(unavailable));
195+
.pipe(Effect.mapError(dispatchFailure));
195196
return { sequence: result.sequence };
196197
}),
197198
t3_pending_request_list: (input) =>
@@ -219,7 +220,7 @@ export const layer = ThreadToolkit.toLayer({
219220
requestId: input.requestId,
220221
answers: input.answers,
221222
})
222-
.pipe(Effect.mapError(unavailable));
223+
.pipe(Effect.mapError(dispatchFailure));
223224
return { sequence: result.sequence };
224225
}),
225226
t3_queue_list: (input) =>
@@ -300,7 +301,7 @@ export const layer = ThreadToolkit.toLayer({
300301
default:
301302
command = { ...common, type: `thread.${input.action}` };
302303
}
303-
const result = yield* threads.dispatch(command).pipe(Effect.mapError(unavailable));
304+
const result = yield* threads.dispatch(command).pipe(Effect.mapError(dispatchFailure));
304305
return { sequence: result.sequence };
305306
}),
306307
});

‎apps/server/src/preview/ServerBrowserContexts.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as NodeEvents from "node:events";
22

33
import { INCOGNITO_BROWSER_PROFILE_ID } from "@t3tools/contracts";
4-
import type { Browser, BrowserContext, BrowserType } from "playwright-core";
4+
import { chromium, type Browser, type BrowserContext, type BrowserType } from "playwright-core";
55
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
66

77
import { ServerBrowserContexts } from "./ServerBrowserContexts.ts";
@@ -12,9 +12,8 @@ const launches = vi.hoisted(() => ({
1212
mkdir: vi.fn().mockResolvedValue(undefined),
1313
rm: vi.fn().mockResolvedValue(undefined),
1414
}));
15-
vi.mock("playwright-core", () => ({
16-
chromium: { launch: launches.launch, launchPersistentContext: launches.persistent },
17-
}));
15+
vi.spyOn(chromium, "launch").mockImplementation(launches.launch);
16+
vi.spyOn(chromium, "launchPersistentContext").mockImplementation(launches.persistent);
1817
vi.mock("node:fs/promises", () => ({ mkdir: launches.mkdir, rm: launches.rm }));
1918

2019
const makeContext = () => {

0 commit comments

Comments
 (0)