|
1 | 1 | import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; |
2 | 2 | import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; |
| 3 | +import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; |
3 | 4 | import { expect, it } from "@effect/vitest"; |
4 | 5 | import { NodeHttpServer } from "@effect/platform-node"; |
5 | 6 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
|
8 | 9 | OrchestratorMcpFailure, |
9 | 10 | PreviewTabId, |
10 | 11 | ProviderInstanceId, |
| 12 | + ProviderInteractionMode, |
| 13 | + RuntimeMode, |
11 | 14 | ThreadId, |
12 | 15 | } from "@t3tools/contracts"; |
13 | 16 | import * as Deferred from "effect/Deferred"; |
@@ -1067,3 +1070,145 @@ it.effect("admits provider and OAuth client credentials and points only clients |
1067 | 1070 | }), |
1068 | 1071 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
1069 | 1072 | ); |
| 1073 | + |
| 1074 | +it.effect( |
| 1075 | + "narrows an OAuth client's modes to the T3-Mode-Limit it sends, and never widens them", |
| 1076 | + () => |
| 1077 | + Effect.scoped( |
| 1078 | + Effect.gen(function* () { |
| 1079 | + const clientToken = "client-payload.client-signature"; |
| 1080 | + const clientScope: McpInvocationContext.McpInvocationScope = { |
| 1081 | + environmentId, |
| 1082 | + requestNamespace: "client:linked", |
| 1083 | + thread: undefined, |
| 1084 | + client: { sessionId: "linked", label: "T3 Code · laptop", access: "auto" }, |
| 1085 | + capabilities: new Set(["orchestration"]), |
| 1086 | + issuedAt: 1, |
| 1087 | + }; |
| 1088 | + // Starts a thread at the modes the gate hands it, so the outcome is those modes. |
| 1089 | + const StartsToolkit = Toolkit.make( |
| 1090 | + Tool.make("starts", { |
| 1091 | + description: "Reports the modes a started thread would run with.", |
| 1092 | + parameters: Schema.Struct({ |
| 1093 | + runtimeMode: Schema.optional(RuntimeMode), |
| 1094 | + interactionMode: Schema.optional(ProviderInteractionMode), |
| 1095 | + }), |
| 1096 | + success: Schema.Struct({ modes: Schema.String }), |
| 1097 | + failure: OrchestratorMcpFailure, |
| 1098 | + failureMode: "return", |
| 1099 | + dependencies: [ |
| 1100 | + McpInvocationContext.McpInvocationContext, |
| 1101 | + ThreadManagementService.ThreadManagementService, |
| 1102 | + ], |
| 1103 | + }), |
| 1104 | + ); |
| 1105 | + const serverLayer = McpHttpServer.toolkitRegistration( |
| 1106 | + StartsToolkit, |
| 1107 | + McpToolAccess.toLayer(StartsToolkit, { |
| 1108 | + starts: McpToolAccess.startsThreads( |
| 1109 | + (input) => input, |
| 1110 | + (_, modes) => |
| 1111 | + Effect.succeed({ modes: `${modes.runtimeMode}/${modes.interactionMode}` }), |
| 1112 | + ), |
| 1113 | + }), |
| 1114 | + ).pipe( |
| 1115 | + Layer.provideMerge(McpHttpServer.layerMcpTransport), |
| 1116 | + Layer.provide(McpToolAccessTestkit.liveThreadsLayer), |
| 1117 | + Layer.provide( |
| 1118 | + Layer.mock(McpSessionRegistry.McpSessionRegistry)({ |
| 1119 | + resolve: () => Effect.succeed(undefined), |
| 1120 | + }), |
| 1121 | + ), |
| 1122 | + Layer.provide( |
| 1123 | + Layer.succeed(McpHttpServer.McpClientAuthenticator, { |
| 1124 | + authenticate: (request) => |
| 1125 | + Effect.succeed( |
| 1126 | + request.headers.authorization === `Bearer ${clientToken}` |
| 1127 | + ? clientScope |
| 1128 | + : undefined, |
| 1129 | + ), |
| 1130 | + }), |
| 1131 | + ), |
| 1132 | + ); |
| 1133 | + yield* HttpRouter.serve(serverLayer, { disableListenLog: true, disableLogger: true }).pipe( |
| 1134 | + Layer.build, |
| 1135 | + ); |
| 1136 | + const httpClient = yield* HttpClient.HttpClient; |
| 1137 | + const decodeOutcome = Schema.decodeUnknownEffect( |
| 1138 | + Schema.fromJsonString( |
| 1139 | + Schema.Struct({ |
| 1140 | + result: Schema.Struct({ |
| 1141 | + content: Schema.Array(Schema.Struct({ text: Schema.String })), |
| 1142 | + }), |
| 1143 | + }), |
| 1144 | + ), |
| 1145 | + ); |
| 1146 | + const decodeText = Schema.decodeUnknownEffect( |
| 1147 | + Schema.fromJsonString( |
| 1148 | + Schema.Union([ |
| 1149 | + Schema.Struct({ modes: Schema.String }), |
| 1150 | + Schema.Struct({ code: Schema.String }), |
| 1151 | + ]), |
| 1152 | + ), |
| 1153 | + ); |
| 1154 | + /** Starts a thread as the client, with `limit` as T3-Mode-Limit; the HTTP status or the outcome. */ |
| 1155 | + const start = (limit: string | undefined, args: Record<string, string> = {}) => |
| 1156 | + Effect.gen(function* () { |
| 1157 | + const headers = { |
| 1158 | + accept: "application/json, text/event-stream", |
| 1159 | + authorization: `Bearer ${clientToken}`, |
| 1160 | + ...(limit === undefined ? {} : { "t3-mode-limit": limit }), |
| 1161 | + }; |
| 1162 | + const initialize = yield* httpClient.post("/mcp", { |
| 1163 | + headers, |
| 1164 | + body: HttpBody.text( |
| 1165 | + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1.0.0"}}}`, |
| 1166 | + "application/json", |
| 1167 | + ), |
| 1168 | + }); |
| 1169 | + if (initialize.status !== 200) return initialize.status; |
| 1170 | + const response = yield* httpClient.post("/mcp", { |
| 1171 | + headers: { |
| 1172 | + ...headers, |
| 1173 | + "mcp-session-id": initialize.headers["mcp-session-id"]!, |
| 1174 | + "mcp-protocol-version": "2025-06-18", |
| 1175 | + }, |
| 1176 | + body: HttpBody.text( |
| 1177 | + JSON.stringify({ |
| 1178 | + jsonrpc: "2.0", |
| 1179 | + id: 2, |
| 1180 | + method: "tools/call", |
| 1181 | + params: { name: "starts", arguments: args }, |
| 1182 | + }), |
| 1183 | + "application/json", |
| 1184 | + ), |
| 1185 | + }); |
| 1186 | + const body = yield* decodeOutcome(yield* response.text); |
| 1187 | + const outcome = yield* decodeText( |
| 1188 | + body.result.content.map((part) => part.text).join(""), |
| 1189 | + ); |
| 1190 | + return "code" in outcome ? outcome.code : outcome.modes; |
| 1191 | + }); |
| 1192 | + |
| 1193 | + // Approved for auto: without the header the client keeps its ceiling. |
| 1194 | + expect(yield* start(undefined)).toBe("auto/default"); |
| 1195 | + // The header narrows both modes, and asking above it is refused. |
| 1196 | + expect(yield* start("approval-required/plan")).toBe("approval-required/plan"); |
| 1197 | + expect(yield* start("approval-required/plan", { runtimeMode: "auto" })).toBe( |
| 1198 | + "runtime_mode_escalation_denied", |
| 1199 | + ); |
| 1200 | + expect(yield* start("auto-accept-edits/plan", { interactionMode: "default" })).toBe( |
| 1201 | + "interaction_mode_escalation_denied", |
| 1202 | + ); |
| 1203 | + // A broader header changes nothing: the approved ceiling still holds. |
| 1204 | + expect(yield* start("full-access/default")).toBe("auto/default"); |
| 1205 | + expect(yield* start("full-access/default", { runtimeMode: "full-access" })).toBe( |
| 1206 | + "runtime_mode_escalation_denied", |
| 1207 | + ); |
| 1208 | + // A malformed header is refused before anything runs. |
| 1209 | + expect(yield* start("auto")).toBe(400); |
| 1210 | + expect(yield* start("auto/default/extra")).toBe(400); |
| 1211 | + expect(yield* start("root/default")).toBe(400); |
| 1212 | + }), |
| 1213 | + ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 1214 | +); |
0 commit comments