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, 04:45 UTC
| 1 | import * as AssetSigner from "../../assets/AssetSigner.ts"; |
| 2 | import * as DocumentText from "../../provider/DocumentText.ts"; |
| 3 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import { expect, it } from "@effect/vitest"; |
| 6 | import { |
| 7 | DEFAULT_SERVER_SETTINGS, |
| 8 | ChatImageAttachment, |
| 9 | CommandId, |
| 10 | EnvironmentId, |
| 11 | NodeId, |
| 12 | OrchestratorMcpThreadReadResult, |
| 13 | ProjectId, |
| 14 | ProviderDriverKind, |
| 15 | ProviderInstanceId, |
| 16 | RunId, |
| 17 | ScheduledTaskError, |
| 18 | ScheduledTask, |
| 19 | RuntimeRequestId, |
| 20 | ThreadId, |
| 21 | TurnItemId, |
| 22 | type OrchestrationV2ServerCommand, |
| 23 | type OrchestrationV2ProjectedTurnItem, |
| 24 | type OrchestrationV2ThreadShell, |
| 25 | } from "@t3tools/contracts"; |
| 26 | import { pinOrderKeyBetween } from "@t3tools/shared/pinOrderKey"; |
| 27 | import * as DateTime from "effect/DateTime"; |
| 28 | import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
| 29 | import * as Deferred from "effect/Deferred"; |
| 30 | import * as Effect from "effect/Effect"; |
| 31 | import * as TestClock from "effect/testing/TestClock"; |
| 32 | import * as Layer from "effect/Layer"; |
| 33 | import * as Schema from "effect/Schema"; |
| 34 | import { McpAttachmentInput } from "./attachment/input.ts"; |
| 35 | import { McpSchema, McpServer } from "effect/ai"; |
| 36 | import { FetchHttpClient } from "effect/http"; |
| 37 | |
| 38 | import { |
| 39 | OrchestratorCommandRejectedError, |
| 40 | OrchestratorDispatchError, |
| 41 | OrchestratorProjectionError, |
| 42 | OrchestratorProviderSelectionBusyError, |
| 43 | } from "../../orchestration-v2/Orchestrator.ts"; |
| 44 | |
| 45 | import * as ServerSecretStore from "../../auth/ServerSecretStore.ts"; |
| 46 | import * as ServerConfig from "../../config.ts"; |
| 47 | import * as ProviderAdapterRegistry from "../../orchestration-v2/ProviderAdapterRegistry.ts"; |
| 48 | import * as ThreadLaunchService from "../../orchestration-v2/ThreadLaunchService.ts"; |
| 49 | import * as GitVcsDriver from "../../vcs/GitVcsDriver.ts"; |
| 50 | import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts"; |
| 51 | import type * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts"; |
| 52 | import * as ThreadSearch from "../../orchestration-v2/ThreadSearch.ts"; |
| 53 | import * as PreviewBrowser from "../../preview/PreviewBrowser.ts"; |
| 54 | import * as ProjectService from "../../project/ProjectService.ts"; |
| 55 | import * as ProviderRegistry from "../../provider/ProviderRegistry.ts"; |
| 56 | import * as SecretRequests from "../../secrets/SecretRequests.ts"; |
| 57 | import * as RemoteDelegation from "../../peer/RemoteDelegation.ts"; |
| 58 | import * as ScheduledTaskService from "../../scheduledTasks/ScheduledTaskService.ts"; |
| 59 | import * as ServerSettings from "../../serverSettings.ts"; |
| 60 | import * as McpHttpServer from "../McpHttpServer.ts"; |
| 61 | import * as McpInvocationContext from "../McpInvocationContext.ts"; |
| 62 | import * as McpToolAccessTestkit from "../McpToolAccess.testkit.ts"; |
| 63 | import * as AnalyticsService from "../../telemetry/AnalyticsService.ts"; |
| 64 | import { dispatchFailure } from "../threadAccess.ts"; |
| 65 | import * as EnvironmentHandlers from "./environment/handlers.ts"; |
| 66 | import * as AttachmentHandlers from "./attachment/handlers.ts"; |
| 67 | import { ThreadToolkit } from "./thread/tools.ts"; |
| 68 | import * as PeerForwarding from "../../peer/PeerForwarding.ts"; |
| 69 | import * as PeerLinkRequests from "../../peer/PeerLinkRequests.ts"; |
| 70 | import * as PeerLinks from "../../peer/PeerLinks.ts"; |
| 71 | |
| 72 | const layerOrchestratorToolkit = McpHttpServer.layerOrchestratorToolkit.pipe( |
| 73 | Layer.provide(Layer.mock(ThreadLaunchService.ThreadLaunchService)({})), |
| 74 | Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})), |
| 75 | ); |
| 76 | |
| 77 | const declaredFailure = (result: McpSchema.CallToolResult) => { |
| 78 | const text = result.content[0]; |
| 79 | return result.isError === true && text?.type === "text" ? JSON.parse(text.text) : undefined; |
| 80 | }; |
| 81 | import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput"; |
| 82 | |
| 83 | const decodeMcpAttachmentInput = Schema.decodeUnknownEffect(McpAttachmentInput); |
| 84 | const decodeThreadReadResult = Schema.decodeUnknownEffect(OrchestratorMcpThreadReadResult); |
| 85 | const decodeThreadSearchResult = Schema.decodeUnknownEffect( |
| 86 | ThreadToolkit.tools.t3_thread_search.successSchema, |
| 87 | ); |
| 88 | |
| 89 | const layerThreadToolkit = McpHttpServer.layerThreadToolkit.pipe( |
| 90 | Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})), |
| 91 | Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), |
| 92 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 93 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 94 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 95 | ); |
| 96 | |
| 97 | const threadId = ThreadId.make("mcp-core-thread"); |
| 98 | const scope: McpInvocationContext.McpInvocationScope = { |
| 99 | environmentId: EnvironmentId.make("mcp-core-environment"), |
| 100 | requestNamespace: "mcp-core-session", |
| 101 | thread: { |
| 102 | threadId, |
| 103 | providerSessionId: "mcp-core-session", |
| 104 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 105 | }, |
| 106 | client: undefined, |
| 107 | issuedAt: 0, |
| 108 | capabilities: new Set(["orchestration"]), |
| 109 | }; |
| 110 | const client = McpSchema.McpServerClient.of({ |
| 111 | clientId: 1, |
| 112 | protocolVersion: "2025-06-18", |
| 113 | clientCapabilities: {}, |
| 114 | clientInfo: { name: "mcp-core", version: "1" }, |
| 115 | initializePayload: { |
| 116 | protocolVersion: "2025-06-18", |
| 117 | capabilities: {}, |
| 118 | clientInfo: { name: "mcp-core", version: "1" }, |
| 119 | }, |
| 120 | getClient: Effect.die("unused"), |
| 121 | }); |
| 122 | |
| 123 | it.effect("checks capability through the production registration", () => |
| 124 | Effect.gen(function* () { |
| 125 | const server = yield* McpServer.McpServer; |
| 126 | expect(server.tools.some(({ tool }) => tool.name === "t3_thread_organize")).toBe(true); |
| 127 | const result = yield* server |
| 128 | .callTool({ name: "t3_thread_organize", arguments: { action: "pin" } }) |
| 129 | .pipe( |
| 130 | Effect.provideService(McpInvocationContext.McpInvocationContext, { |
| 131 | ...scope, |
| 132 | capabilities: new Set<never>(), |
| 133 | }), |
| 134 | Effect.provideService(McpSchema.McpServerClient, client), |
| 135 | ); |
| 136 | expect(declaredFailure(result)).toMatchObject({ code: "capability_denied" }); |
| 137 | expect(result.isError).toBe(true); |
| 138 | expect(result.structuredContent).toBeUndefined(); |
| 139 | }).pipe( |
| 140 | Effect.provide( |
| 141 | layerThreadToolkit.pipe( |
| 142 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 143 | Layer.provide(NodeCrypto.layer), |
| 144 | Layer.provide(McpToolAccessTestkit.liveThreadsLayer), |
| 145 | ), |
| 146 | ), |
| 147 | ), |
| 148 | ); |
| 149 | |
| 150 | it.effect("an answered question reports already answered instead of not found", () => { |
| 151 | const shell = McpToolAccessTestkit.liveThreadShell(threadId); |
| 152 | const requestId = RuntimeRequestId.make("answered-question"); |
| 153 | const projection = McpToolAccessTestkit.idleThreadProjection(shell); |
| 154 | return Effect.gen(function* () { |
| 155 | const server = yield* McpServer.McpServer; |
| 156 | const respond = (id: string) => |
| 157 | server |
| 158 | .callTool({ |
| 159 | name: "t3_pending_request", |
| 160 | arguments: { action: "respond", requestId: id, answers: { size: "large" } }, |
| 161 | }) |
| 162 | .pipe( |
| 163 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 164 | Effect.provideService(McpSchema.McpServerClient, client), |
| 165 | ); |
| 166 | expect(declaredFailure(yield* respond(requestId))).toMatchObject({ |
| 167 | code: "invalid_request", |
| 168 | message: "The user-input request was already answered.", |
| 169 | }); |
| 170 | expect(declaredFailure(yield* respond("missing-question"))).toMatchObject({ |
| 171 | code: "invalid_request", |
| 172 | message: "The pending user-input request was not found.", |
| 173 | }); |
| 174 | }).pipe( |
| 175 | Effect.provide( |
| 176 | layerThreadToolkit.pipe( |
| 177 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 178 | Layer.provide(NodeCrypto.layer), |
| 179 | Layer.provide( |
| 180 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 181 | getThreadShell: () => Effect.succeed(shell), |
| 182 | getProjectThreadRecords: () => |
| 183 | Effect.succeed({ |
| 184 | ...projection, |
| 185 | runtimeRequests: [ |
| 186 | { |
| 187 | id: requestId, |
| 188 | nodeId: NodeId.make("root"), |
| 189 | providerTurnId: null, |
| 190 | nativeRequestRef: null, |
| 191 | kind: "user_input", |
| 192 | status: "resolved", |
| 193 | responseCapability: { type: "message" }, |
| 194 | createdAt: shell.createdAt, |
| 195 | resolvedAt: shell.updatedAt, |
| 196 | answers: { size: "large" }, |
| 197 | }, |
| 198 | ], |
| 199 | turnItems: [ |
| 200 | { |
| 201 | id: TurnItemId.make("answered-question-item"), |
| 202 | threadId, |
| 203 | runId: null, |
| 204 | nodeId: null, |
| 205 | providerThreadId: null, |
| 206 | providerTurnId: null, |
| 207 | nativeItemRef: null, |
| 208 | parentItemId: null, |
| 209 | ordinal: 0, |
| 210 | status: "completed", |
| 211 | title: null, |
| 212 | startedAt: shell.createdAt, |
| 213 | completedAt: shell.updatedAt, |
| 214 | updatedAt: shell.updatedAt, |
| 215 | type: "user_input_request", |
| 216 | requestId, |
| 217 | questions: [], |
| 218 | }, |
| 219 | ], |
| 220 | }), |
| 221 | dispatch: () => Effect.die("an answered question must not dispatch again"), |
| 222 | }), |
| 223 | ), |
| 224 | ), |
| 225 | ), |
| 226 | ); |
| 227 | }); |
| 228 | |
| 229 | it.effect("a pending question read keeps option previews", () => { |
| 230 | const shell = McpToolAccessTestkit.liveThreadShell(threadId); |
| 231 | const requestId = RuntimeRequestId.make("previewed-question"); |
| 232 | const projection = McpToolAccessTestkit.idleThreadProjection(shell); |
| 233 | const questions = [ |
| 234 | { |
| 235 | id: "post", |
| 236 | header: "Review", |
| 237 | question: "Post it?", |
| 238 | options: [{ label: "Post", description: "Publish the draft", preview: "Full proposed text" }], |
| 239 | }, |
| 240 | ]; |
| 241 | return Effect.gen(function* () { |
| 242 | const server = yield* McpServer.McpServer; |
| 243 | const result = yield* server |
| 244 | .callTool({ name: "t3_pending_request", arguments: { action: "read", requestId } }) |
| 245 | .pipe( |
| 246 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 247 | Effect.provideService(McpSchema.McpServerClient, client), |
| 248 | ); |
| 249 | expect(result.structuredContent).toEqual({ requestId, questions }); |
| 250 | }).pipe( |
| 251 | Effect.provide( |
| 252 | layerThreadToolkit.pipe( |
| 253 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 254 | Layer.provide(NodeCrypto.layer), |
| 255 | Layer.provide( |
| 256 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 257 | getThreadShell: () => Effect.succeed(shell), |
| 258 | getProjectThreadRecords: () => |
| 259 | Effect.succeed({ |
| 260 | ...projection, |
| 261 | runtimeRequests: [ |
| 262 | { |
| 263 | id: requestId, |
| 264 | nodeId: NodeId.make("root"), |
| 265 | providerTurnId: null, |
| 266 | nativeRequestRef: null, |
| 267 | kind: "user_input", |
| 268 | status: "pending", |
| 269 | responseCapability: { type: "message" }, |
| 270 | createdAt: shell.createdAt, |
| 271 | resolvedAt: null, |
| 272 | }, |
| 273 | ], |
| 274 | turnItems: [ |
| 275 | { |
| 276 | id: TurnItemId.make("previewed-question-item"), |
| 277 | threadId, |
| 278 | runId: null, |
| 279 | nodeId: null, |
| 280 | providerThreadId: null, |
| 281 | providerTurnId: null, |
| 282 | nativeItemRef: null, |
| 283 | parentItemId: null, |
| 284 | ordinal: 0, |
| 285 | status: "running", |
| 286 | title: null, |
| 287 | startedAt: shell.createdAt, |
| 288 | completedAt: null, |
| 289 | updatedAt: shell.updatedAt, |
| 290 | type: "user_input_request", |
| 291 | requestId, |
| 292 | questions, |
| 293 | }, |
| 294 | ], |
| 295 | }), |
| 296 | }), |
| 297 | ), |
| 298 | ), |
| 299 | ), |
| 300 | ); |
| 301 | }); |
| 302 | |
| 303 | it("bounds public command rejections and redacts internal dispatch causes", () => { |
| 304 | const command = { commandId: CommandId.make("mcp-core-command"), commandType: "thread.settle" }; |
| 305 | expect( |
| 306 | dispatchFailure(new OrchestratorDispatchError({ ...command, cause: "🙂".repeat(1001) })) |
| 307 | .message, |
| 308 | ).toBe("🙂".repeat(1000)); |
| 309 | expect( |
| 310 | dispatchFailure( |
| 311 | new OrchestratorCommandRejectedError({ ...command, cause: "Run is not queued." }), |
| 312 | ).message, |
| 313 | ).toBe("Run is not queued."); |
| 314 | const busy = new OrchestratorProviderSelectionBusyError({ |
| 315 | commandId: command.commandId, |
| 316 | threadId, |
| 317 | }); |
| 318 | expect(dispatchFailure(busy)).toMatchObject({ code: "invalid_request", message: busy.message }); |
| 319 | for (const cause of [ |
| 320 | undefined, |
| 321 | "", |
| 322 | new Error("private-storage-path"), |
| 323 | { message: "private-storage-path" }, |
| 324 | ]) { |
| 325 | expect(dispatchFailure(new OrchestratorDispatchError({ ...command, cause }))).toMatchObject({ |
| 326 | code: "orchestration_error", |
| 327 | message: "The operation could not be completed.", |
| 328 | }); |
| 329 | expect( |
| 330 | dispatchFailure(new OrchestratorCommandRejectedError({ ...command, cause })), |
| 331 | ).toMatchObject({ |
| 332 | code: "orchestration_error", |
| 333 | message: "The operation could not be completed.", |
| 334 | }); |
| 335 | } |
| 336 | expect( |
| 337 | dispatchFailure(new OrchestratorProjectionError({ threadId, cause: "private-storage-path" })), |
| 338 | ).toMatchObject({ |
| 339 | code: "orchestration_error", |
| 340 | message: "The operation could not be completed.", |
| 341 | }); |
| 342 | }); |
| 343 | |
| 344 | const fullAccessThreadId = ThreadId.make("mcp-core-full-access-thread"); |
| 345 | |
| 346 | it.effect("returns an HTML render reference that Codex and Claude tool rows both carry", () => |
| 347 | Effect.gen(function* () { |
| 348 | const server = yield* McpServer.McpServer; |
| 349 | const result = yield* server |
| 350 | .callTool({ |
| 351 | name: "html_render", |
| 352 | arguments: { html: "<p>Revenue</p>", title: "Revenue", height: 240 }, |
| 353 | }) |
| 354 | .pipe( |
| 355 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 356 | Effect.provideService(McpSchema.McpServerClient, client), |
| 357 | ); |
| 358 | const reference = htmlRenderFromToolItem({ |
| 359 | toolName: "t3-code.html_render", |
| 360 | output: result.structuredContent, |
| 361 | }); |
| 362 | expect(reference).toMatchObject({ title: "Revenue", height: 240 }); |
| 363 | expect( |
| 364 | htmlRenderFromToolItem({ toolName: "mcp__t3-code__html_render", output: result.content }), |
| 365 | ).toEqual(reference); |
| 366 | // Only a full-access thread may have the server read a file for it. |
| 367 | const byPath = (name: string, caller: McpInvocationContext.McpInvocationScope) => |
| 368 | server |
| 369 | .callTool({ |
| 370 | name, |
| 371 | arguments: { |
| 372 | path: "/t3-missing-dir/revenue.html", |
| 373 | ...(name === "html_render" ? { title: "Revenue", height: 240 } : {}), |
| 374 | }, |
| 375 | }) |
| 376 | .pipe( |
| 377 | Effect.provideService(McpInvocationContext.McpInvocationContext, caller), |
| 378 | Effect.provideService(McpSchema.McpServerClient, client), |
| 379 | // html_preview reports failures as plain text, html_render as a declared failure. |
| 380 | Effect.map((result) => (result.isError ? JSON.stringify(result.content) : undefined)), |
| 381 | ); |
| 382 | const fullAccess = { ...scope, thread: { ...scope.thread!, threadId: fullAccessThreadId } }; |
| 383 | for (const name of ["html_preview", "html_render"]) { |
| 384 | expect(yield* byPath(name, scope), name).toContain("only full-access threads"); |
| 385 | expect(yield* byPath(name, fullAccess), name).toContain("not a readable HTML document"); |
| 386 | } |
| 387 | }).pipe( |
| 388 | Effect.provide( |
| 389 | McpHttpServer.layerHtmlToolkit.pipe( |
| 390 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 391 | Layer.provide(McpProviderSessions.layer), |
| 392 | Layer.provide(PreviewBrowser.layer), |
| 393 | Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-html-render-" })), |
| 394 | Layer.provide( |
| 395 | DocumentText.layer.pipe( |
| 396 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 397 | ), |
| 398 | ), |
| 399 | Layer.provide(FetchHttpClient.layer), |
| 400 | Layer.provide( |
| 401 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 402 | getThreadShell: (id) => |
| 403 | Effect.succeed({ |
| 404 | id, |
| 405 | deletedAt: null, |
| 406 | archivedAt: null, |
| 407 | activeRunId: RunId.make("mcp-core-run"), |
| 408 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 409 | runtimeMode: id === fullAccessThreadId ? "full-access" : "approval-required", |
| 410 | interactionMode: "default", |
| 411 | } as OrchestrationV2ThreadShell), |
| 412 | }), |
| 413 | ), |
| 414 | ), |
| 415 | ), |
| 416 | ), |
| 417 | ); |
| 418 | |
| 419 | it.effect("records only failed calls of tools that hand off no work", () => { |
| 420 | // Recording runs detached from the tool response, so the first event is awaited. |
| 421 | const recorded = Deferred.makeUnsafe<{ event: string; properties: object }>(); |
| 422 | return Effect.gen(function* () { |
| 423 | const server = yield* McpServer.McpServer; |
| 424 | const organize = (capabilities: McpInvocationContext.McpInvocationScope["capabilities"]) => |
| 425 | server.callTool({ name: "t3_thread_organize", arguments: { action: "pin" } }).pipe( |
| 426 | Effect.provideService(McpInvocationContext.McpInvocationContext, { |
| 427 | ...scope, |
| 428 | capabilities, |
| 429 | }), |
| 430 | Effect.provideService(McpSchema.McpServerClient, client), |
| 431 | ); |
| 432 | expect((yield* organize(scope.capabilities)).isError).not.toBe(true); |
| 433 | expect(declaredFailure(yield* organize(new Set<never>()))).toMatchObject({ |
| 434 | code: "capability_denied", |
| 435 | }); |
| 436 | // The successful call ran first, so it would be the first event if it were recorded. |
| 437 | expect(yield* Deferred.await(recorded)).toMatchObject({ |
| 438 | event: "mcp.tool.invoked", |
| 439 | properties: { tool: "t3_thread_organize", outcome: "error" }, |
| 440 | }); |
| 441 | }).pipe( |
| 442 | Effect.provide( |
| 443 | layerThreadToolkit.pipe( |
| 444 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 445 | Layer.provide(NodeCrypto.layer), |
| 446 | Layer.provide( |
| 447 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 448 | getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)), |
| 449 | getProjectThreadRecords: ({ threadId: id }) => |
| 450 | Effect.succeed( |
| 451 | McpToolAccessTestkit.idleThreadProjection(McpToolAccessTestkit.liveThreadShell(id)), |
| 452 | ), |
| 453 | listActiveShellIndex: () => Effect.succeed([]), |
| 454 | dispatch: () => Effect.succeed({ sequence: 1 } as never), |
| 455 | }), |
| 456 | ), |
| 457 | Layer.provide( |
| 458 | Layer.succeed( |
| 459 | AnalyticsService.AnalyticsService, |
| 460 | AnalyticsService.AnalyticsService.of({ |
| 461 | record: (event, properties) => |
| 462 | Deferred.succeed(recorded, { event, properties: properties ?? {} }).pipe( |
| 463 | Effect.asVoid, |
| 464 | ), |
| 465 | flush: Effect.void, |
| 466 | recordOwners: () => Effect.void, |
| 467 | }), |
| 468 | ), |
| 469 | ), |
| 470 | ), |
| 471 | ), |
| 472 | ); |
| 473 | }); |
| 474 | |
| 475 | // A thread read whose runs or messages are bounded by nothing but the thread. |
| 476 | const historyReads = ( |
| 477 | reads: Array<string>, |
| 478 | queued?: { readonly attachments: ReadonlyArray<unknown>; readonly dispatched: Array<unknown> }, |
| 479 | ) => { |
| 480 | const record = ( |
| 481 | fields: ReadonlyArray<string>, |
| 482 | filter: ProjectionStore.ProjectionRecordFilter | undefined, |
| 483 | ) => { |
| 484 | if ( |
| 485 | fields.includes("runs") && |
| 486 | filter?.runIds === undefined && |
| 487 | filter?.runStatuses === undefined |
| 488 | ) |
| 489 | reads.push("every run"); |
| 490 | if ( |
| 491 | fields.includes("messages") && |
| 492 | filter?.messageIds === undefined && |
| 493 | filter?.messageRunIds === undefined |
| 494 | ) |
| 495 | reads.push("every message"); |
| 496 | }; |
| 497 | const projection = McpToolAccessTestkit.idleThreadProjection( |
| 498 | McpToolAccessTestkit.liveThreadShell(threadId), |
| 499 | ); |
| 500 | return Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 501 | getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)), |
| 502 | getProjectThreadRecords: (_target, fields, filter) => |
| 503 | Effect.sync(() => record(fields, filter)).pipe(Effect.as(projection as never)), |
| 504 | getThreadRecords: (_id, fields, filter) => |
| 505 | Effect.sync(() => record(fields, filter)).pipe( |
| 506 | Effect.as({ |
| 507 | ...projection, |
| 508 | runs: |
| 509 | queued === undefined |
| 510 | ? projection.runs |
| 511 | : [{ id: "run:queued", status: "queued", userMessageId: "message:queued" }], |
| 512 | } as never), |
| 513 | ), |
| 514 | getMessagesById: () => |
| 515 | Effect.succeed(new Map(queued === undefined ? [] : [["message:queued", queued]]) as never), |
| 516 | hasReceipt: () => Effect.succeed(false), |
| 517 | dispatch: (command) => |
| 518 | Effect.sync(() => queued?.dispatched.push(command)).pipe(Effect.as({ sequence: 1 } as never)), |
| 519 | }); |
| 520 | }; |
| 521 | |
| 522 | it.effect("t3_queue list and read load only the queued runs and their messages", () => { |
| 523 | const reads: Array<string> = []; |
| 524 | return Effect.gen(function* () { |
| 525 | const server = yield* McpServer.McpServer; |
| 526 | const call = (args: Record<string, unknown>) => |
| 527 | server |
| 528 | .callTool({ name: "t3_queue", arguments: args }) |
| 529 | .pipe( |
| 530 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 531 | Effect.provideService(McpSchema.McpServerClient, client), |
| 532 | ); |
| 533 | yield* call({ action: "list" }); |
| 534 | yield* call({ action: "read", queuedRunId: "run:queued" }); |
| 535 | expect(reads).toEqual([]); |
| 536 | }).pipe( |
| 537 | Effect.provide( |
| 538 | layerThreadToolkit.pipe( |
| 539 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 540 | Layer.provide(NodeCrypto.layer), |
| 541 | Layer.provide(historyReads(reads)), |
| 542 | ), |
| 543 | ), |
| 544 | ); |
| 545 | }); |
| 546 | |
| 547 | it.effect("handoff analytics read only the caller's active run and its message", () => { |
| 548 | const reads: Array<string> = []; |
| 549 | const attachments = [{ type: "image", id: "shot", name: "shot.png" }]; |
| 550 | const dispatched: Array<unknown> = []; |
| 551 | const recorded = Deferred.makeUnsafe<void>(); |
| 552 | return Effect.gen(function* () { |
| 553 | const server = yield* McpServer.McpServer; |
| 554 | yield* server |
| 555 | .callTool({ |
| 556 | name: "t3_queue", |
| 557 | arguments: { action: "edit", queuedRunId: "run:queued", text: "Edited" }, |
| 558 | }) |
| 559 | .pipe( |
| 560 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 561 | Effect.provideService(McpSchema.McpServerClient, client), |
| 562 | ); |
| 563 | yield* Deferred.await(recorded); |
| 564 | expect(reads).toEqual([]); |
| 565 | // The edit keeps the queued image, so `/logout` stays an ordinary prompt and is not refused. |
| 566 | const attached = yield* server |
| 567 | .callTool({ |
| 568 | name: "t3_queue", |
| 569 | arguments: { action: "edit", queuedRunId: "run:queued", text: " /LOGOUT " }, |
| 570 | }) |
| 571 | .pipe( |
| 572 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 573 | Effect.provideService(McpSchema.McpServerClient, client), |
| 574 | ); |
| 575 | expect(attached.isError).not.toBe(true); |
| 576 | expect(dispatched).toMatchObject([{ text: "Edited" }, { text: " /LOGOUT ", attachments }]); |
| 577 | expect(dispatched[0]).not.toHaveProperty("attachments"); |
| 578 | }).pipe( |
| 579 | Effect.provide( |
| 580 | layerThreadToolkit.pipe( |
| 581 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 582 | Layer.provide(NodeCrypto.layer), |
| 583 | Layer.provide(historyReads(reads, { attachments, dispatched })), |
| 584 | Layer.provide( |
| 585 | Layer.succeed( |
| 586 | AnalyticsService.AnalyticsService, |
| 587 | AnalyticsService.AnalyticsService.of({ |
| 588 | record: () => Deferred.succeed(recorded, undefined).pipe(Effect.asVoid), |
| 589 | flush: Effect.void, |
| 590 | recordOwners: () => Effect.void, |
| 591 | }), |
| 592 | ), |
| 593 | ), |
| 594 | ), |
| 595 | ), |
| 596 | ); |
| 597 | }); |
| 598 | |
| 599 | it.effect("rejects an unknown organize target instead of archiving the calling thread", () => { |
| 600 | const dispatched: Array<unknown> = []; |
| 601 | return Effect.gen(function* () { |
| 602 | const server = yield* McpServer.McpServer; |
| 603 | const outcome = yield* server |
| 604 | .callTool({ |
| 605 | name: "t3_thread_organize", |
| 606 | arguments: { action: "archive", threadIds: ["other-thread"] }, |
| 607 | }) |
| 608 | .pipe( |
| 609 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 610 | Effect.provideService(McpSchema.McpServerClient, client), |
| 611 | Effect.result, |
| 612 | ); |
| 613 | expect(dispatched).toEqual([]); |
| 614 | expect(outcome).toMatchObject({ |
| 615 | _tag: "Failure", |
| 616 | failure: { message: expect.stringContaining('Unknown argument "threadIds"') }, |
| 617 | }); |
| 618 | }).pipe( |
| 619 | Effect.provide( |
| 620 | layerThreadToolkit.pipe( |
| 621 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 622 | Layer.provide(NodeCrypto.layer), |
| 623 | Layer.provide( |
| 624 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 625 | getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)), |
| 626 | getProjectThreadRecords: ({ threadId: id }) => |
| 627 | Effect.succeed( |
| 628 | McpToolAccessTestkit.idleThreadProjection(McpToolAccessTestkit.liveThreadShell(id)), |
| 629 | ), |
| 630 | dispatch: (command) => { |
| 631 | dispatched.push(command); |
| 632 | return Effect.succeed({ sequence: 1 } as never); |
| 633 | }, |
| 634 | }), |
| 635 | ), |
| 636 | ), |
| 637 | ), |
| 638 | ); |
| 639 | }); |
| 640 | |
| 641 | it.effect("keeps unexpected handler defects private through the production registration", () => |
| 642 | Effect.gen(function* () { |
| 643 | const server = yield* McpServer.McpServer; |
| 644 | const result = yield* server |
| 645 | .callTool({ name: "t3_thread_organize", arguments: { action: "pin" } }) |
| 646 | .pipe( |
| 647 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 648 | Effect.provideService(McpSchema.McpServerClient, client), |
| 649 | ); |
| 650 | expect(result.isError).toBe(true); |
| 651 | expect(result.structuredContent).toBeUndefined(); |
| 652 | expect(result.content).toEqual([ |
| 653 | { type: "text", text: "Tool execution failed due to an internal server error." }, |
| 654 | ]); |
| 655 | }).pipe( |
| 656 | Effect.provide( |
| 657 | layerThreadToolkit.pipe( |
| 658 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 659 | Layer.provide(NodeCrypto.layer), |
| 660 | Layer.provide( |
| 661 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 662 | getThreadShell: () => Effect.die(new Error("private-storage-path")), |
| 663 | }), |
| 664 | ), |
| 665 | ), |
| 666 | ), |
| 667 | ), |
| 668 | ); |
| 669 | |
| 670 | it("keeps MCP preference output allowlisted and Unicode-bounded", () => { |
| 671 | const settings = { |
| 672 | ...DEFAULT_SERVER_SETTINGS, |
| 673 | privateCredential: "must-not-escape", |
| 674 | sourceControlWritingStyle: { |
| 675 | ...DEFAULT_SERVER_SETTINGS.sourceControlWritingStyle, |
| 676 | customInstructions: "🙂".repeat(4001), |
| 677 | }, |
| 678 | }; |
| 679 | const result = EnvironmentHandlers.preferences(settings); |
| 680 | expect(result).not.toHaveProperty("privateCredential"); |
| 681 | expect(result).not.toHaveProperty("providers"); |
| 682 | expect(result.sourceControlWritingStyle).toMatchObject({ |
| 683 | customInstructions: "🙂".repeat(4000), |
| 684 | truncated: true, |
| 685 | }); |
| 686 | }); |
| 687 | |
| 688 | it.effect("resolves reused attachment references from stored metadata", () => |
| 689 | Effect.gen(function* () { |
| 690 | const stored = ChatImageAttachment.make({ |
| 691 | type: "image", |
| 692 | id: "owned-image", |
| 693 | name: "original.png", |
| 694 | mimeType: "image/png", |
| 695 | sizeBytes: 12, |
| 696 | source: { |
| 697 | kind: "snap-shot", |
| 698 | capturedAt: "2026-09-10T00:00:00.000Z", |
| 699 | appName: "Terminal", |
| 700 | windowTitle: "Test", |
| 701 | accessibility: { format: "flat-text", text: "Stored context", truncated: false }, |
| 702 | }, |
| 703 | }); |
| 704 | const forged = yield* decodeMcpAttachmentInput({ |
| 705 | type: "image", |
| 706 | id: stored.id, |
| 707 | name: "changed.jpg", |
| 708 | mimeType: "image/jpeg", |
| 709 | sizeBytes: 99, |
| 710 | }); |
| 711 | const result = yield* AttachmentHandlers.resolveAttachmentReferences([forged], [stored]); |
| 712 | expect(result).toEqual([stored]); |
| 713 | const failure = yield* AttachmentHandlers.resolveAttachmentReferences( |
| 714 | [{ ...forged, id: "other-image" }], |
| 715 | [stored], |
| 716 | ).pipe(Effect.flip); |
| 717 | expect(failure.code).toBe("invalid_request"); |
| 718 | }), |
| 719 | ); |
| 720 | |
| 721 | const clientScope = ( |
| 722 | access: McpInvocationContext.McpClientCaller["access"], |
| 723 | ): McpInvocationContext.McpInvocationScope => ({ |
| 724 | environmentId: EnvironmentId.make("mcp-core-environment"), |
| 725 | requestNamespace: "client:session-1", |
| 726 | thread: undefined, |
| 727 | client: { sessionId: "session-1", label: "Claude Code", access }, |
| 728 | issuedAt: 0, |
| 729 | capabilities: new Set(["orchestration", "worktree", "pull-requests", "issues"]), |
| 730 | }); |
| 731 | |
| 732 | it.effect("a client caller targets any thread within its ceiling and cannot act as a thread", () => |
| 733 | Effect.gen(function* () { |
| 734 | const server = yield* McpServer.McpServer; |
| 735 | const call = ( |
| 736 | name: string, |
| 737 | args: Record<string, unknown>, |
| 738 | invocation: McpInvocationContext.McpInvocationScope, |
| 739 | ) => |
| 740 | server |
| 741 | .callTool({ name, arguments: args }) |
| 742 | .pipe( |
| 743 | Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), |
| 744 | Effect.provideService(McpSchema.McpServerClient, client), |
| 745 | ); |
| 746 | |
| 747 | const untargeted = yield* call("t3_thread_organize", { action: "pin" }, clientScope("auto")); |
| 748 | expect(declaredFailure(untargeted)).toMatchObject({ code: "target_required" }); |
| 749 | |
| 750 | const pinned = yield* call( |
| 751 | "t3_thread_organize", |
| 752 | { action: "pin", threadId: "other-project-thread" }, |
| 753 | clientScope("auto"), |
| 754 | ); |
| 755 | expect(pinned.isError).toBe(false); |
| 756 | expect(pinned.structuredContent).toMatchObject({ sequence: 7 }); |
| 757 | |
| 758 | const aboveCeiling = yield* call( |
| 759 | "t3_thread_organize", |
| 760 | { action: "pin", threadId: "other-project-thread" }, |
| 761 | clientScope("approval-required"), |
| 762 | ); |
| 763 | expect(declaredFailure(aboveCeiling)).toMatchObject({ |
| 764 | code: "runtime_mode_escalation_denied", |
| 765 | }); |
| 766 | |
| 767 | const forked = yield* call( |
| 768 | "t3_thread_transfer", |
| 769 | { action: "fork", sourcePoint: { type: "latest_stable" } }, |
| 770 | clientScope("auto"), |
| 771 | ); |
| 772 | expect(declaredFailure(forked)).toMatchObject({ code: "target_required" }); |
| 773 | }).pipe( |
| 774 | Effect.provide( |
| 775 | layerThreadToolkit.pipe( |
| 776 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 777 | Layer.provide(NodeCrypto.layer), |
| 778 | Layer.provide( |
| 779 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 780 | getThreadShell: (id) => |
| 781 | Effect.succeed(McpToolAccessTestkit.liveThreadShell(id, { runtimeMode: "auto" })), |
| 782 | getProjectThreadRecords: () => |
| 783 | Effect.succeed({ |
| 784 | thread: { |
| 785 | id: ThreadId.make("other-project-thread"), |
| 786 | projectId: "other-project", |
| 787 | runtimeMode: "auto", |
| 788 | interactionMode: "default", |
| 789 | deletedAt: null, |
| 790 | }, |
| 791 | } as never), |
| 792 | listActiveShellIndex: () => Effect.succeed([]), |
| 793 | dispatch: () => Effect.succeed({ sequence: 7, storedEvents: [] }), |
| 794 | }), |
| 795 | ), |
| 796 | ), |
| 797 | ), |
| 798 | ), |
| 799 | ); |
| 800 | |
| 801 | it.effect("an MCP pin lands above every existing pin, as a client pin does", () => { |
| 802 | const dispatched: Array<OrchestrationV2ServerCommand> = []; |
| 803 | let targetPinnedAt: DateTime.Utc | null = null; |
| 804 | const pinnedAt = DateTime.makeUnsafe("2026-11-10T00:00:00.000Z"); |
| 805 | const indexEntry = (id: string, pinned: boolean, pinOrderKey: string | null) => ({ |
| 806 | ...McpToolAccessTestkit.liveThreadShell(ThreadId.make(id)), |
| 807 | pinnedAt: pinned ? pinnedAt : null, |
| 808 | pinOrderKey, |
| 809 | mayBeLive: false, |
| 810 | hasRemoteTasks: false, |
| 811 | }); |
| 812 | return Effect.gen(function* () { |
| 813 | const server = yield* McpServer.McpServer; |
| 814 | const pin = server |
| 815 | .callTool({ |
| 816 | name: "t3_thread_organize", |
| 817 | arguments: { action: "pin", threadId: "pin-target" }, |
| 818 | }) |
| 819 | .pipe( |
| 820 | Effect.provideService(McpInvocationContext.McpInvocationContext, clientScope("auto")), |
| 821 | Effect.provideService(McpSchema.McpServerClient, client), |
| 822 | ); |
| 823 | |
| 824 | const first = yield* pin; |
| 825 | expect(first.isError).toBe(false); |
| 826 | const orderKey = (dispatched[0] as { readonly orderKey?: string }).orderKey; |
| 827 | expect(dispatched[0]).toMatchObject({ type: "thread.pin", threadId: "pin-target" }); |
| 828 | // The smallest key among pinned threads is "g"; the unpinned "b" and keyless pin are ignored. |
| 829 | expect(orderKey).toBe(pinOrderKeyBetween(null, "g")); |
| 830 | |
| 831 | // A thread the handler last saw pinned still gets a key: an unpin can commit |
| 832 | // before this command, and the reducer ignores the key if the pin holds. |
| 833 | targetPinnedAt = pinnedAt; |
| 834 | const again = yield* pin; |
| 835 | expect(again.isError).toBe(false); |
| 836 | expect(dispatched[1]).toMatchObject({ type: "thread.pin", orderKey }); |
| 837 | }).pipe( |
| 838 | Effect.provide( |
| 839 | layerThreadToolkit.pipe( |
| 840 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 841 | Layer.provide(NodeCrypto.layer), |
| 842 | Layer.provide( |
| 843 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 844 | getThreadShell: (id) => |
| 845 | Effect.succeed(McpToolAccessTestkit.liveThreadShell(id, { runtimeMode: "auto" })), |
| 846 | getProjectThreadRecords: ({ threadId: id }) => { |
| 847 | const projection = McpToolAccessTestkit.idleThreadProjection( |
| 848 | McpToolAccessTestkit.liveThreadShell(id, { runtimeMode: "auto" }), |
| 849 | ); |
| 850 | return Effect.succeed({ |
| 851 | ...projection, |
| 852 | thread: { ...projection.thread, pinnedAt: targetPinnedAt }, |
| 853 | }); |
| 854 | }, |
| 855 | listActiveShellIndex: () => |
| 856 | Effect.succeed([ |
| 857 | indexEntry("pinned-late", true, "t"), |
| 858 | indexEntry("pinned-top", true, "g"), |
| 859 | indexEntry("pinned-keyless", true, null), |
| 860 | indexEntry("unpinned-stale-key", false, "b"), |
| 861 | ]), |
| 862 | dispatch: (command) => { |
| 863 | dispatched.push(command); |
| 864 | return Effect.succeed({ sequence: dispatched.length, storedEvents: [] }); |
| 865 | }, |
| 866 | }), |
| 867 | ), |
| 868 | ), |
| 869 | ), |
| 870 | ); |
| 871 | }); |
| 872 | |
| 873 | it.effect("search names the calling thread, so an agent can tell its own match apart", () => |
| 874 | Effect.gen(function* () { |
| 875 | const server = yield* McpServer.McpServer; |
| 876 | const search = (invocation: McpInvocationContext.McpInvocationScope) => |
| 877 | server |
| 878 | .callTool({ name: "t3_thread_search", arguments: { query: "release notes" } }) |
| 879 | .pipe( |
| 880 | Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), |
| 881 | Effect.provideService(McpSchema.McpServerClient, client), |
| 882 | ); |
| 883 | |
| 884 | const fromThread = yield* search(scope); |
| 885 | expect(fromThread.structuredContent).toMatchObject({ |
| 886 | currentThreadId: threadId, |
| 887 | matches: [{ threadId }, { threadId: "earlier-thread" }], |
| 888 | }); |
| 889 | |
| 890 | const fromClient = yield* search(clientScope("auto")); |
| 891 | expect(fromClient.structuredContent).toMatchObject({ currentThreadId: null }); |
| 892 | |
| 893 | // A linked environment on an older version answers without the field. |
| 894 | const fromOlderPeer = yield* decodeThreadSearchResult({ matches: [] }); |
| 895 | expect(fromOlderPeer).toEqual({ matches: [] }); |
| 896 | }).pipe( |
| 897 | Effect.provide( |
| 898 | McpHttpServer.layerThreadToolkit.pipe( |
| 899 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 900 | Layer.provide(NodeCrypto.layer), |
| 901 | Layer.provide(McpToolAccessTestkit.liveThreadsLayer), |
| 902 | Layer.provide( |
| 903 | Layer.mock(ThreadSearch.ThreadSearch)({ |
| 904 | searchThreads: () => |
| 905 | Effect.succeed({ |
| 906 | matches: [threadId, ThreadId.make("earlier-thread")].map((id) => ({ |
| 907 | threadId: id, |
| 908 | projectId: ProjectId.make("project:mcp-test"), |
| 909 | title: "Release notes", |
| 910 | source: "title" as const, |
| 911 | snippet: "", |
| 912 | messageCreatedAt: null, |
| 913 | })), |
| 914 | }), |
| 915 | }), |
| 916 | ), |
| 917 | Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), |
| 918 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 919 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 920 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 921 | ), |
| 922 | ), |
| 923 | ), |
| 924 | ); |
| 925 | |
| 926 | it.effect("a read-only client reads threads and is refused every write before it runs", () => |
| 927 | Effect.gen(function* () { |
| 928 | const server = yield* McpServer.McpServer; |
| 929 | const call = (name: string, args: Record<string, unknown>) => |
| 930 | server |
| 931 | .callTool({ name, arguments: args }) |
| 932 | .pipe( |
| 933 | Effect.provideService( |
| 934 | McpInvocationContext.McpInvocationContext, |
| 935 | clientScope("read-only"), |
| 936 | ), |
| 937 | Effect.provideService(McpSchema.McpServerClient, client), |
| 938 | ); |
| 939 | |
| 940 | const configuration = yield* call("t3_thread_configure", { |
| 941 | threadId: "other-project-thread", |
| 942 | }); |
| 943 | expect(configuration.isError).toBe(false); |
| 944 | expect(configuration.structuredContent).toMatchObject({ runtimeMode: "auto" }); |
| 945 | |
| 946 | const pinned = yield* call("t3_thread_organize", { |
| 947 | action: "pin", |
| 948 | threadId: "other-project-thread", |
| 949 | }); |
| 950 | expect(declaredFailure(pinned)).toMatchObject({ code: "capability_denied" }); |
| 951 | expect(dispatched).toEqual([]); |
| 952 | |
| 953 | const configure = yield* call("t3_thread_configure", { |
| 954 | threadId: "other-project-thread", |
| 955 | modelSelection: { instanceId: "codex", model: "gpt-5" }, |
| 956 | }); |
| 957 | expect(declaredFailure(configure)).toMatchObject({ code: "capability_denied" }); |
| 958 | expect(dispatched).toEqual([]); |
| 959 | }).pipe( |
| 960 | Effect.provide( |
| 961 | layerThreadToolkit.pipe( |
| 962 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 963 | Layer.provide(NodeCrypto.layer), |
| 964 | Layer.provide( |
| 965 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 966 | getThreadShell: () => |
| 967 | Effect.succeed({ |
| 968 | id: ThreadId.make("other-project-thread"), |
| 969 | projectId: "other-project", |
| 970 | deletedAt: null, |
| 971 | } as never), |
| 972 | getProjectThreadRecords: () => |
| 973 | Effect.succeed({ |
| 974 | thread: { |
| 975 | id: ThreadId.make("other-project-thread"), |
| 976 | projectId: "other-project", |
| 977 | modelSelection: { instanceId: "codex", model: "gpt-5" }, |
| 978 | runtimeMode: "auto", |
| 979 | interactionMode: "default", |
| 980 | deletedAt: null, |
| 981 | }, |
| 982 | } as never), |
| 983 | dispatch: () => |
| 984 | Effect.sync(() => { |
| 985 | dispatched.push("dispatch"); |
| 986 | return { sequence: 7 } as never; |
| 987 | }), |
| 988 | }), |
| 989 | ), |
| 990 | ), |
| 991 | ), |
| 992 | ), |
| 993 | ); |
| 994 | const dispatched: Array<string> = []; |
| 995 | |
| 996 | it.effect("hides act-as-caller tools from a client caller", () => |
| 997 | Effect.gen(function* () { |
| 998 | const server = yield* McpServer.McpServer; |
| 999 | const error = yield* server |
| 1000 | .callTool({ |
| 1001 | name: "delegate_task", |
| 1002 | arguments: { task: "Review", mode: "async" }, |
| 1003 | }) |
| 1004 | .pipe( |
| 1005 | Effect.flip, |
| 1006 | Effect.provideService( |
| 1007 | McpInvocationContext.McpInvocationContext, |
| 1008 | clientScope("full-access"), |
| 1009 | ), |
| 1010 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1011 | ); |
| 1012 | expect(error.message).toContain("Tool 'delegate_task' not found"); |
| 1013 | }).pipe( |
| 1014 | Effect.provide( |
| 1015 | layerOrchestratorToolkit.pipe( |
| 1016 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1017 | Layer.provide(NodeCrypto.layer), |
| 1018 | Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), |
| 1019 | Layer.provide(ServerSettings.layerTest()), |
| 1020 | Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})), |
| 1021 | Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})), |
| 1022 | Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), |
| 1023 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1024 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1025 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1026 | Layer.provide(Layer.mock(ProjectService.ProjectService)({})), |
| 1027 | Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})), |
| 1028 | Layer.provide(Layer.mock(RemoteDelegation.RemoteDelegation)({})), |
| 1029 | ), |
| 1030 | ), |
| 1031 | ), |
| 1032 | ); |
| 1033 | |
| 1034 | it.effect("a caller cannot rewrite a scheduled task that runs above its own modes", () => |
| 1035 | Effect.gen(function* () { |
| 1036 | const server = yield* McpServer.McpServer; |
| 1037 | const call = (name: string, args: Record<string, unknown>) => |
| 1038 | server |
| 1039 | .callTool({ name, arguments: args }) |
| 1040 | .pipe( |
| 1041 | Effect.provideService(McpInvocationContext.McpInvocationContext, clientScope("auto")), |
| 1042 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1043 | ); |
| 1044 | const update = yield* call("schedule_task", { |
| 1045 | scheduledTaskId: "task-full-access", |
| 1046 | prompt: "Run something else", |
| 1047 | }); |
| 1048 | expect(declaredFailure(update)).toMatchObject({ code: "runtime_mode_escalation_denied" }); |
| 1049 | const remove = yield* call("delete_scheduled_task", { scheduledTaskId: "task-full-access" }); |
| 1050 | expect(declaredFailure(remove)).toMatchObject({ code: "runtime_mode_escalation_denied" }); |
| 1051 | const allowed = yield* call("schedule_task", { |
| 1052 | scheduledTaskId: "task-auto", |
| 1053 | enabled: false, |
| 1054 | }); |
| 1055 | expect(allowed.isError).toBe(false); |
| 1056 | const fresh = yield* call("schedule_task", { |
| 1057 | scheduledTaskId: "task-auto", |
| 1058 | freshSession: true, |
| 1059 | }); |
| 1060 | expect(fresh.structuredContent).toMatchObject({ freshSession: true }); |
| 1061 | const incomplete = yield* call("schedule_task", { prompt: "Run nightly" }); |
| 1062 | expect(declaredFailure(incomplete)).toMatchObject({ code: "invalid_request" }); |
| 1063 | }).pipe( |
| 1064 | Effect.provide( |
| 1065 | layerOrchestratorToolkit.pipe( |
| 1066 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1067 | Layer.provide(NodeCrypto.layer), |
| 1068 | Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), |
| 1069 | Layer.provide(ServerSettings.layerTest()), |
| 1070 | Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})), |
| 1071 | Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})), |
| 1072 | Layer.provide( |
| 1073 | Layer.mock(ScheduledTaskService.ScheduledTaskService)({ |
| 1074 | list: () => |
| 1075 | Effect.succeed({ |
| 1076 | tasks: [ |
| 1077 | scheduledTask("task-full-access", "full-access"), |
| 1078 | scheduledTask("task-auto", "auto"), |
| 1079 | ], |
| 1080 | }), |
| 1081 | upsert: (input) => |
| 1082 | Effect.succeed({ |
| 1083 | task: { ...(scheduledTask(input.id ?? "task-auto", "auto") as object), ...input }, |
| 1084 | } as never), |
| 1085 | }), |
| 1086 | ), |
| 1087 | Layer.provide(Layer.mock(ProjectService.ProjectService)({})), |
| 1088 | Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})), |
| 1089 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1090 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1091 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1092 | Layer.provide(Layer.mock(RemoteDelegation.RemoteDelegation)({})), |
| 1093 | ), |
| 1094 | ), |
| 1095 | ), |
| 1096 | ); |
| 1097 | |
| 1098 | it.effect.each([ |
| 1099 | { |
| 1100 | scenario: "explains why a webhook task cannot run manually", |
| 1101 | tasks: [ |
| 1102 | { |
| 1103 | ...Schema.decodeSync(ScheduledTask)(scheduledTask("task-webhook", "full-access")), |
| 1104 | schedule: { type: "webhook" as const, signature: null }, |
| 1105 | }, |
| 1106 | ], |
| 1107 | error: new ScheduledTaskError({ |
| 1108 | message: "Webhook tasks run when their URL receives a request.", |
| 1109 | cause: new Error("private-webhook-cause"), |
| 1110 | }), |
| 1111 | code: "orchestration_error", |
| 1112 | message: "Webhook tasks run when their URL receives a request.", |
| 1113 | }, |
| 1114 | { |
| 1115 | scenario: "reports a missing task before trying to run it", |
| 1116 | tasks: [], |
| 1117 | error: new ScheduledTaskError({ message: "must not run a missing task" }), |
| 1118 | code: "invalid_request", |
| 1119 | message: "The scheduled task was not found.", |
| 1120 | }, |
| 1121 | ])("run_scheduled_task_now $scenario", ({ tasks, error, code, message }) => |
| 1122 | Effect.gen(function* () { |
| 1123 | const server = yield* McpServer.McpServer; |
| 1124 | const result = yield* server |
| 1125 | .callTool({ name: "run_scheduled_task_now", arguments: { taskId: "task-webhook" } }) |
| 1126 | .pipe( |
| 1127 | Effect.provideService( |
| 1128 | McpInvocationContext.McpInvocationContext, |
| 1129 | clientScope("full-access"), |
| 1130 | ), |
| 1131 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1132 | ); |
| 1133 | expect(result.isError).toBe(true); |
| 1134 | expect(result.structuredContent).toBeUndefined(); |
| 1135 | expect(declaredFailure(result)).toEqual({ _tag: "OrchestratorMcpFailure", code, message }); |
| 1136 | expect(JSON.stringify(result)).not.toContain("private-"); |
| 1137 | }).pipe( |
| 1138 | Effect.provide( |
| 1139 | McpHttpServer.layerThreadToolkit.pipe( |
| 1140 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1141 | Layer.provide(NodeCrypto.layer), |
| 1142 | Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), |
| 1143 | Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})), |
| 1144 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1145 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1146 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1147 | Layer.provide( |
| 1148 | Layer.mock(ScheduledTaskService.ScheduledTaskService)({ |
| 1149 | list: () => Effect.succeed({ tasks }), |
| 1150 | runNow: () => Effect.fail(error), |
| 1151 | }), |
| 1152 | ), |
| 1153 | ), |
| 1154 | ), |
| 1155 | ), |
| 1156 | ); |
| 1157 | |
| 1158 | it.effect("run_scheduled_task_now reports why a started run failed to dispatch", () => |
| 1159 | Effect.gen(function* () { |
| 1160 | const server = yield* McpServer.McpServer; |
| 1161 | const result = yield* server |
| 1162 | .callTool({ name: "run_scheduled_task_now", arguments: { taskId: "task-full-access" } }) |
| 1163 | .pipe( |
| 1164 | Effect.provideService( |
| 1165 | McpInvocationContext.McpInvocationContext, |
| 1166 | clientScope("full-access"), |
| 1167 | ), |
| 1168 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1169 | ); |
| 1170 | expect(result.isError).toBe(false); |
| 1171 | expect(result.structuredContent).toMatchObject({ |
| 1172 | threadId: null, |
| 1173 | lastRunStatus: "failed", |
| 1174 | lastRunError: "the provider is disabled", |
| 1175 | }); |
| 1176 | }).pipe( |
| 1177 | Effect.provide( |
| 1178 | McpHttpServer.layerThreadToolkit.pipe( |
| 1179 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1180 | Layer.provide(NodeCrypto.layer), |
| 1181 | Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), |
| 1182 | Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})), |
| 1183 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1184 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1185 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1186 | Layer.provide( |
| 1187 | Layer.mock(ScheduledTaskService.ScheduledTaskService)({ |
| 1188 | list: () => |
| 1189 | Effect.succeed({ tasks: [scheduledTask("task-full-access", "full-access")] }), |
| 1190 | runNow: () => |
| 1191 | Effect.succeed({ |
| 1192 | threadId: null, |
| 1193 | task: { |
| 1194 | ...(scheduledTask("task-full-access", "full-access") as object), |
| 1195 | lastRunStatus: "failed" as const, |
| 1196 | lastRunError: "the provider is disabled", |
| 1197 | }, |
| 1198 | } as never), |
| 1199 | }), |
| 1200 | ), |
| 1201 | ), |
| 1202 | ), |
| 1203 | ), |
| 1204 | ); |
| 1205 | |
| 1206 | function scheduledTask(id: string, runtimeMode: "auto" | "full-access"): never { |
| 1207 | return { |
| 1208 | id, |
| 1209 | title: id, |
| 1210 | prompt: "Check the build", |
| 1211 | enabled: true, |
| 1212 | projectId: "project-a", |
| 1213 | threadId: null, |
| 1214 | schedule: { type: "interval", everyMs: 3_600_000 }, |
| 1215 | workspaceStrategy: { type: "worktree", baseRef: "main", startFromOrigin: true }, |
| 1216 | modelSelection: { instanceId: "codex", model: "gpt-5" }, |
| 1217 | runtimeMode, |
| 1218 | interactionMode: "default", |
| 1219 | createdBy: "user", |
| 1220 | creationSource: "web", |
| 1221 | nextRunAt: null, |
| 1222 | lastRunStatus: "never", |
| 1223 | lastRunAt: null, |
| 1224 | lastRunThreadId: null, |
| 1225 | lastRunError: null, |
| 1226 | runCount: 0, |
| 1227 | createdAt: "2026-10-01T00:00:00.000Z", |
| 1228 | updatedAt: "2026-10-01T00:00:00.000Z", |
| 1229 | } as never; |
| 1230 | } |
| 1231 | |
| 1232 | it.effect.each(["t3_thread_interrupt", "t3_workflow_stop"])( |
| 1233 | "a caller cannot use %s on a thread that runs above its own modes", |
| 1234 | (toolName) => |
| 1235 | Effect.gen(function* () { |
| 1236 | const server = yield* McpServer.McpServer; |
| 1237 | const result = yield* server |
| 1238 | .callTool({ |
| 1239 | name: toolName, |
| 1240 | arguments: { |
| 1241 | threadId: "full-access-thread", |
| 1242 | ...(toolName === "t3_workflow_stop" ? { subagentId: "workflow-coordinator" } : {}), |
| 1243 | }, |
| 1244 | }) |
| 1245 | .pipe( |
| 1246 | Effect.provideService(McpInvocationContext.McpInvocationContext, clientScope("auto")), |
| 1247 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1248 | ); |
| 1249 | expect(declaredFailure(result)).toMatchObject({ code: "runtime_mode_escalation_denied" }); |
| 1250 | }).pipe( |
| 1251 | Effect.provide( |
| 1252 | layerOrchestratorToolkit.pipe( |
| 1253 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1254 | Layer.provide(NodeCrypto.layer), |
| 1255 | Layer.provide( |
| 1256 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 1257 | getThreadShell: () => |
| 1258 | Effect.succeed({ |
| 1259 | projectId: "project-a", |
| 1260 | deletedAt: null, |
| 1261 | runtimeMode: "full-access", |
| 1262 | interactionMode: "default", |
| 1263 | } as never), |
| 1264 | getProjectThreadRecords: () => |
| 1265 | Effect.succeed({ |
| 1266 | thread: { |
| 1267 | id: ThreadId.make("full-access-thread"), |
| 1268 | projectId: "project-a", |
| 1269 | runtimeMode: "full-access", |
| 1270 | interactionMode: "default", |
| 1271 | deletedAt: null, |
| 1272 | }, |
| 1273 | runs: [], |
| 1274 | } as never), |
| 1275 | stopWorkflow: () => Effect.die("workflow stop must not dispatch above the ceiling"), |
| 1276 | interruptThread: () => Effect.die("interrupt must not dispatch above the ceiling"), |
| 1277 | }), |
| 1278 | ), |
| 1279 | Layer.provide(ServerSettings.layerTest()), |
| 1280 | Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})), |
| 1281 | Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})), |
| 1282 | Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), |
| 1283 | Layer.provide(Layer.mock(ProjectService.ProjectService)({})), |
| 1284 | Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})), |
| 1285 | Layer.provide(Layer.mock(RemoteDelegation.RemoteDelegation)({})), |
| 1286 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1287 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1288 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1289 | ), |
| 1290 | ), |
| 1291 | ), |
| 1292 | ); |
| 1293 | |
| 1294 | it.effect( |
| 1295 | "t3_background_wait marks work from a turn Claude started before T3 started its run", |
| 1296 | () => |
| 1297 | Effect.gen(function* () { |
| 1298 | const server = yield* McpServer.McpServer; |
| 1299 | const result = yield* server |
| 1300 | .callTool({ name: "t3_background_wait", arguments: { taskIds: ["checks"] } }) |
| 1301 | .pipe( |
| 1302 | Effect.provideService(McpInvocationContext.McpInvocationContext, { |
| 1303 | ...scope, |
| 1304 | capabilities: new Set<McpInvocationContext.McpCapability>([ |
| 1305 | "orchestration", |
| 1306 | "background-wait", |
| 1307 | ]), |
| 1308 | }), |
| 1309 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1310 | ); |
| 1311 | expect(result.structuredContent).toEqual({ matched: ["checks"], unmatched: [] }); |
| 1312 | }).pipe( |
| 1313 | Effect.provide( |
| 1314 | layerOrchestratorToolkit.pipe( |
| 1315 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1316 | Layer.provide(NodeCrypto.layer), |
| 1317 | Layer.provide( |
| 1318 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 1319 | getThreadShell: (id) => |
| 1320 | Effect.succeed(McpToolAccessTestkit.liveThreadShell(id, { activeRunId: null })), |
| 1321 | awaitBackgroundTasks: ({ taskIds }) => |
| 1322 | Effect.succeed({ matched: taskIds, unmatched: [] }), |
| 1323 | }), |
| 1324 | ), |
| 1325 | Layer.provide(ServerSettings.layerTest()), |
| 1326 | Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})), |
| 1327 | Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})), |
| 1328 | Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), |
| 1329 | Layer.provide(Layer.mock(ProjectService.ProjectService)({})), |
| 1330 | Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})), |
| 1331 | Layer.provide(Layer.mock(RemoteDelegation.RemoteDelegation)({})), |
| 1332 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1333 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1334 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1335 | ), |
| 1336 | ), |
| 1337 | ), |
| 1338 | ); |
| 1339 | |
| 1340 | it.effect("discovers workflow stop targets and requires orchestration capability", () => { |
| 1341 | const threadId = ThreadId.make("workflow-parent"); |
| 1342 | const shell = McpToolAccessTestkit.liveThreadShell(threadId); |
| 1343 | const subagentId = NodeId.make("workflow-coordinator"); |
| 1344 | const itemId = TurnItemId.make("workflow-coordinator-activity"); |
| 1345 | const row: OrchestrationV2ProjectedTurnItem = { |
| 1346 | position: 0, |
| 1347 | visibility: "local", |
| 1348 | sourceThreadId: threadId, |
| 1349 | sourceItemId: itemId, |
| 1350 | item: { |
| 1351 | id: itemId, |
| 1352 | threadId, |
| 1353 | runId: shell.activeRunId, |
| 1354 | nodeId: subagentId, |
| 1355 | providerThreadId: null, |
| 1356 | providerTurnId: null, |
| 1357 | nativeItemRef: null, |
| 1358 | parentItemId: null, |
| 1359 | ordinal: 0, |
| 1360 | status: "running", |
| 1361 | title: "Workflow: checkout review", |
| 1362 | startedAt: shell.createdAt, |
| 1363 | completedAt: null, |
| 1364 | updatedAt: shell.updatedAt, |
| 1365 | type: "subagent", |
| 1366 | subagentId, |
| 1367 | origin: "provider_native", |
| 1368 | driver: ProviderDriverKind.make("claudeAgent"), |
| 1369 | providerInstanceId: ProviderInstanceId.make("claudeAgent"), |
| 1370 | childThreadId: ThreadId.make("workflow-child"), |
| 1371 | prompt: "Review checkout", |
| 1372 | result: null, |
| 1373 | }, |
| 1374 | }; |
| 1375 | const stopped: Array<{ readonly threadId: ThreadId; readonly subagentId: string }> = []; |
| 1376 | return Effect.gen(function* () { |
| 1377 | const server = yield* McpServer.McpServer; |
| 1378 | const scope = clientScope("full-access"); |
| 1379 | const read = yield* server |
| 1380 | .callTool({ name: "t3_thread_read", arguments: { threadId, view: "activity" } }) |
| 1381 | .pipe( |
| 1382 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 1383 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1384 | ); |
| 1385 | expect(read.isError).toBe(false); |
| 1386 | const result = yield* decodeThreadReadResult(read.structuredContent); |
| 1387 | expect(result.items[0]?.subagentId).toBe(subagentId); |
| 1388 | const call = (invocation: McpInvocationContext.McpInvocationScope) => |
| 1389 | server |
| 1390 | .callTool({ |
| 1391 | name: "t3_workflow_stop", |
| 1392 | arguments: { threadId, subagentId: result.items[0]?.subagentId }, |
| 1393 | }) |
| 1394 | .pipe( |
| 1395 | Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), |
| 1396 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1397 | ); |
| 1398 | const denied = yield* call({ ...scope, capabilities: new Set(["worktree"]) }); |
| 1399 | expect(declaredFailure(denied)).toMatchObject({ code: "capability_denied" }); |
| 1400 | expect(stopped).toEqual([]); |
| 1401 | const allowed = yield* call(scope); |
| 1402 | expect(allowed.isError).toBe(false); |
| 1403 | expect(stopped).toEqual([{ threadId: "workflow-parent", subagentId: "workflow-coordinator" }]); |
| 1404 | }).pipe( |
| 1405 | Effect.provide( |
| 1406 | layerOrchestratorToolkit.pipe( |
| 1407 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1408 | Layer.provide(NodeCrypto.layer), |
| 1409 | Layer.provide( |
| 1410 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 1411 | getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)), |
| 1412 | getProjectThreadRecords: () => |
| 1413 | Effect.succeed(McpToolAccessTestkit.idleThreadProjection(shell)), |
| 1414 | getRunCount: () => Effect.succeed(1), |
| 1415 | getTimelinePage: () => |
| 1416 | Effect.succeed({ items: [row], totalItems: 1, hasMore: false, hasEarlier: false }), |
| 1417 | stopWorkflow: (input) => |
| 1418 | Effect.sync(() => { |
| 1419 | stopped.push(input); |
| 1420 | }), |
| 1421 | }), |
| 1422 | ), |
| 1423 | Layer.provide(ServerSettings.layerTest()), |
| 1424 | Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})), |
| 1425 | Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})), |
| 1426 | Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), |
| 1427 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1428 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1429 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1430 | Layer.provide(Layer.mock(ProjectService.ProjectService)({})), |
| 1431 | Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})), |
| 1432 | Layer.provide(Layer.mock(RemoteDelegation.RemoteDelegation)({})), |
| 1433 | ), |
| 1434 | ), |
| 1435 | ); |
| 1436 | }); |
| 1437 | |
| 1438 | it.effect("only the caller that prepared a pending upload can discard it", () => |
| 1439 | Effect.gen(function* () { |
| 1440 | const server = yield* McpServer.McpServer; |
| 1441 | const call = ( |
| 1442 | name: string, |
| 1443 | args: Record<string, unknown>, |
| 1444 | invocation: McpInvocationContext.McpInvocationScope, |
| 1445 | ) => |
| 1446 | server |
| 1447 | .callTool({ name, arguments: args }) |
| 1448 | .pipe( |
| 1449 | Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), |
| 1450 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1451 | ); |
| 1452 | const prepared = yield* call( |
| 1453 | "t3_attachment_prepare_upload", |
| 1454 | { upload: { name: "shot.png", mimeType: "image/png", sizeBytes: 4 } }, |
| 1455 | scope, |
| 1456 | ); |
| 1457 | const { attachmentId } = prepared.structuredContent as { readonly attachmentId: string }; |
| 1458 | const otherThread = { |
| 1459 | ...scope, |
| 1460 | requestNamespace: "other-session", |
| 1461 | thread: { ...scope.thread!, threadId: ThreadId.make("other-thread") }, |
| 1462 | }; |
| 1463 | |
| 1464 | const refused = yield* call("t3_attachment_discard", { attachmentId }, otherThread); |
| 1465 | expect(declaredFailure(refused)).toMatchObject({ code: "invalid_request" }); |
| 1466 | |
| 1467 | yield* TestClock.adjust(24 * 60 * 60 * 1000 + 5 * 60_000); |
| 1468 | yield* call( |
| 1469 | "t3_attachment_prepare_upload", |
| 1470 | { upload: { name: "later.png", mimeType: "image/png", sizeBytes: 4 } }, |
| 1471 | scope, |
| 1472 | ); |
| 1473 | |
| 1474 | const discarded = yield* call( |
| 1475 | "t3_attachment_discard", |
| 1476 | { attachmentId }, |
| 1477 | { ...scope, requestNamespace: "mcp-core-session-2" }, |
| 1478 | ); |
| 1479 | expect(discarded.isError).toBe(false); |
| 1480 | }).pipe( |
| 1481 | Effect.provide( |
| 1482 | McpHttpServer.layerAttachmentToolkit.pipe( |
| 1483 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1484 | Layer.provide(McpToolAccessTestkit.liveThreadsLayer), |
| 1485 | Layer.provide(AssetSigner.layer.pipe(Layer.provideMerge(ServerSecretStore.layer))), |
| 1486 | Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-attachment-" })), |
| 1487 | Layer.provide( |
| 1488 | DocumentText.layer.pipe( |
| 1489 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 1490 | ), |
| 1491 | ), |
| 1492 | Layer.provide(NodeCrypto.layer), |
| 1493 | ), |
| 1494 | ), |
| 1495 | ), |
| 1496 | ); |
| 1497 | |
| 1498 | const sendTargetId = ThreadId.make("send-target-thread"); |
| 1499 | const sendCommand = { |
| 1500 | commandId: CommandId.make("mcp-core-send"), |
| 1501 | commandType: "message.dispatch", |
| 1502 | }; |
| 1503 | |
| 1504 | it.effect.each([ |
| 1505 | { |
| 1506 | outcome: "says that the missing workspace could not be recreated", |
| 1507 | refusal: new ThreadManagement.ThreadManagementWorktreeRestoreError({ |
| 1508 | threadId: sendTargetId, |
| 1509 | cause: new Error("The branch no longer exists."), |
| 1510 | }), |
| 1511 | reply: { |
| 1512 | _tag: "OrchestratorMcpFailure", |
| 1513 | code: "thread_not_sendable", |
| 1514 | message: |
| 1515 | "Couldn't recreate the missing workspace of thread send-target-thread, so the message was not sent. The branch no longer exists.", |
| 1516 | }, |
| 1517 | }, |
| 1518 | { |
| 1519 | outcome: "passes on the text reason of an orchestrator rejection", |
| 1520 | refusal: new OrchestratorCommandRejectedError({ |
| 1521 | ...sendCommand, |
| 1522 | cause: "Thread send-target-thread is deleted.", |
| 1523 | }), |
| 1524 | reply: { |
| 1525 | _tag: "OrchestratorMcpFailure", |
| 1526 | code: "orchestration_error", |
| 1527 | message: "Thread send-target-thread is deleted.", |
| 1528 | }, |
| 1529 | }, |
| 1530 | { |
| 1531 | outcome: "keeps an internal dispatch cause private", |
| 1532 | refusal: new OrchestratorDispatchError({ |
| 1533 | ...sendCommand, |
| 1534 | cause: new Error("private-storage-path"), |
| 1535 | }), |
| 1536 | reply: { |
| 1537 | _tag: "OrchestratorMcpFailure", |
| 1538 | code: "orchestration_error", |
| 1539 | message: "The operation could not be completed.", |
| 1540 | }, |
| 1541 | }, |
| 1542 | ])("t3_thread_send_attachments $outcome", ({ refusal, reply }) => { |
| 1543 | const stored = ChatImageAttachment.make({ |
| 1544 | type: "image", |
| 1545 | id: "owned-image", |
| 1546 | name: "original.png", |
| 1547 | mimeType: "image/png", |
| 1548 | sizeBytes: 12, |
| 1549 | }); |
| 1550 | return Effect.gen(function* () { |
| 1551 | const server = yield* McpServer.McpServer; |
| 1552 | const result = yield* server |
| 1553 | .callTool({ |
| 1554 | name: "t3_thread_send_attachments", |
| 1555 | arguments: { threadId: sendTargetId, message: "See the file.", attachments: [stored] }, |
| 1556 | }) |
| 1557 | .pipe( |
| 1558 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
| 1559 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1560 | ); |
| 1561 | expect(declaredFailure(result)).toEqual(reply); |
| 1562 | }).pipe( |
| 1563 | Effect.provide( |
| 1564 | McpHttpServer.layerAttachmentToolkit.pipe( |
| 1565 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1566 | Layer.provide( |
| 1567 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 1568 | getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)), |
| 1569 | getProjectThreadRecords: () => |
| 1570 | Effect.succeed({ |
| 1571 | ...McpToolAccessTestkit.idleThreadProjection( |
| 1572 | McpToolAccessTestkit.liveThreadShell(sendTargetId), |
| 1573 | ), |
| 1574 | messages: [{ attachments: [stored] }], |
| 1575 | } as never), |
| 1576 | sendToThread: () => Effect.fail(refusal), |
| 1577 | }), |
| 1578 | ), |
| 1579 | Layer.provide(AssetSigner.layer.pipe(Layer.provideMerge(ServerSecretStore.layer))), |
| 1580 | Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-attachment-" })), |
| 1581 | Layer.provide( |
| 1582 | DocumentText.layer.pipe( |
| 1583 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 1584 | ), |
| 1585 | ), |
| 1586 | Layer.provide(NodeCrypto.layer), |
| 1587 | ), |
| 1588 | ), |
| 1589 | ); |
| 1590 | }); |
| 1591 | |
| 1592 | it.effect( |
| 1593 | "t3_thread_list settled:false with an empty statuses list lists the unsettled threads", |
| 1594 | () => { |
| 1595 | const unsettled = McpToolAccessTestkit.liveThreadShell(ThreadId.make("unsettled-thread")); |
| 1596 | const settled = { |
| 1597 | ...McpToolAccessTestkit.liveThreadShell(ThreadId.make("settled-thread")), |
| 1598 | settledOverride: "settled", |
| 1599 | } as const; |
| 1600 | return Effect.gen(function* () { |
| 1601 | const server = yield* McpServer.McpServer; |
| 1602 | const result = yield* server |
| 1603 | .callTool({ |
| 1604 | name: "t3_thread_list", |
| 1605 | arguments: { projectId: unsettled.projectId, settled: false, statuses: [] }, |
| 1606 | }) |
| 1607 | .pipe( |
| 1608 | Effect.provideService( |
| 1609 | McpInvocationContext.McpInvocationContext, |
| 1610 | clientScope("full-access"), |
| 1611 | ), |
| 1612 | Effect.provideService(McpSchema.McpServerClient, client), |
| 1613 | ); |
| 1614 | expect(result.isError).toBe(false); |
| 1615 | expect(result.structuredContent).toMatchObject({ |
| 1616 | threads: [{ threadId: unsettled.id, settled: false }], |
| 1617 | total: 1, |
| 1618 | }); |
| 1619 | }).pipe( |
| 1620 | Effect.provide( |
| 1621 | layerOrchestratorToolkit.pipe( |
| 1622 | Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), |
| 1623 | Layer.provide(NodeCrypto.layer), |
| 1624 | Layer.provide( |
| 1625 | Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 1626 | listProjectThreads: () => Effect.succeed([unsettled, settled]), |
| 1627 | getShellSnapshot: () => Effect.succeed({ threads: [unsettled, settled] } as never), |
| 1628 | }), |
| 1629 | ), |
| 1630 | Layer.provide(ServerSettings.layerTest()), |
| 1631 | Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})), |
| 1632 | Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})), |
| 1633 | Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), |
| 1634 | Layer.provide( |
| 1635 | Layer.mock(ProjectService.ProjectService)({ |
| 1636 | getById: (projectId) => |
| 1637 | Effect.succeedSome({ |
| 1638 | id: projectId, |
| 1639 | title: "MCP test", |
| 1640 | workspaceRoot: "/repo", |
| 1641 | defaultModelSelection: null, |
| 1642 | scripts: [], |
| 1643 | createdAt: "2026-10-18T00:00:00.000Z", |
| 1644 | updatedAt: "2026-10-18T00:00:00.000Z", |
| 1645 | deletedAt: null, |
| 1646 | }), |
| 1647 | }), |
| 1648 | ), |
| 1649 | Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})), |
| 1650 | Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), |
| 1651 | Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), |
| 1652 | Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), |
| 1653 | Layer.provide(Layer.mock(RemoteDelegation.RemoteDelegation)({})), |
| 1654 | ), |
| 1655 | ), |
| 1656 | ); |
| 1657 | }, |
| 1658 | ); |