|
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"; |
@@ -1178,3 +1181,145 @@ it.effect("admits provider and OAuth client credentials and points only clients |
1178 | 1181 | }), |
1179 | 1182 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
1180 | 1183 | ); |
| 1184 | + |
| 1185 | +it.effect( |
| 1186 | + "narrows an OAuth client's modes to the T3-Mode-Limit it sends, and never widens them", |
| 1187 | + () => |
| 1188 | + Effect.scoped( |
| 1189 | + Effect.gen(function* () { |
| 1190 | + const clientToken = "client-payload.client-signature"; |
| 1191 | + const clientScope: McpInvocationContext.McpInvocationScope = { |
| 1192 | + environmentId, |
| 1193 | + requestNamespace: "client:linked", |
| 1194 | + thread: undefined, |
| 1195 | + client: { sessionId: "linked", label: "T3 Code · laptop", access: "auto" }, |
| 1196 | + capabilities: new Set(["orchestration"]), |
| 1197 | + issuedAt: 1, |
| 1198 | + }; |
| 1199 | + // Starts a thread at the modes the gate hands it, so the outcome is those modes. |
| 1200 | + const StartsToolkit = Toolkit.make( |
| 1201 | + Tool.make("starts", { |
| 1202 | + description: "Reports the modes a started thread would run with.", |
| 1203 | + parameters: Schema.Struct({ |
| 1204 | + runtimeMode: Schema.optional(RuntimeMode), |
| 1205 | + interactionMode: Schema.optional(ProviderInteractionMode), |
| 1206 | + }), |
| 1207 | + success: Schema.Struct({ modes: Schema.String }), |
| 1208 | + failure: OrchestratorMcpFailure, |
| 1209 | + failureMode: "return", |
| 1210 | + dependencies: [ |
| 1211 | + McpInvocationContext.McpInvocationContext, |
| 1212 | + ThreadManagementService.ThreadManagementService, |
| 1213 | + ], |
| 1214 | + }), |
| 1215 | + ); |
| 1216 | + const serverLayer = McpHttpServer.toolkitRegistration( |
| 1217 | + StartsToolkit, |
| 1218 | + McpToolAccess.toLayer(StartsToolkit, { |
| 1219 | + starts: McpToolAccess.startsThreads( |
| 1220 | + (input) => input, |
| 1221 | + (_, modes) => |
| 1222 | + Effect.succeed({ modes: `${modes.runtimeMode}/${modes.interactionMode}` }), |
| 1223 | + ), |
| 1224 | + }), |
| 1225 | + ).pipe( |
| 1226 | + Layer.provideMerge(McpHttpServer.layerMcpTransport), |
| 1227 | + Layer.provide(McpToolAccessTestkit.liveThreadsLayer), |
| 1228 | + Layer.provide( |
| 1229 | + Layer.mock(McpSessionRegistry.McpSessionRegistry)({ |
| 1230 | + resolve: () => Effect.succeed(undefined), |
| 1231 | + }), |
| 1232 | + ), |
| 1233 | + Layer.provide( |
| 1234 | + Layer.succeed(McpHttpServer.McpClientAuthenticator, { |
| 1235 | + authenticate: (request) => |
| 1236 | + Effect.succeed( |
| 1237 | + request.headers.authorization === `Bearer ${clientToken}` |
| 1238 | + ? clientScope |
| 1239 | + : undefined, |
| 1240 | + ), |
| 1241 | + }), |
| 1242 | + ), |
| 1243 | + ); |
| 1244 | + yield* HttpRouter.serve(serverLayer, { disableListenLog: true, disableLogger: true }).pipe( |
| 1245 | + Layer.build, |
| 1246 | + ); |
| 1247 | + const httpClient = yield* HttpClient.HttpClient; |
| 1248 | + const decodeOutcome = Schema.decodeUnknownEffect( |
| 1249 | + Schema.fromJsonString( |
| 1250 | + Schema.Struct({ |
| 1251 | + result: Schema.Struct({ |
| 1252 | + content: Schema.Array(Schema.Struct({ text: Schema.String })), |
| 1253 | + }), |
| 1254 | + }), |
| 1255 | + ), |
| 1256 | + ); |
| 1257 | + const decodeText = Schema.decodeUnknownEffect( |
| 1258 | + Schema.fromJsonString( |
| 1259 | + Schema.Union([ |
| 1260 | + Schema.Struct({ modes: Schema.String }), |
| 1261 | + Schema.Struct({ code: Schema.String }), |
| 1262 | + ]), |
| 1263 | + ), |
| 1264 | + ); |
| 1265 | + /** Starts a thread as the client, with `limit` as T3-Mode-Limit; the HTTP status or the outcome. */ |
| 1266 | + const start = (limit: string | undefined, args: Record<string, string> = {}) => |
| 1267 | + Effect.gen(function* () { |
| 1268 | + const headers = { |
| 1269 | + accept: "application/json, text/event-stream", |
| 1270 | + authorization: `Bearer ${clientToken}`, |
| 1271 | + ...(limit === undefined ? {} : { "t3-mode-limit": limit }), |
| 1272 | + }; |
| 1273 | + const initialize = yield* httpClient.post("/mcp", { |
| 1274 | + headers, |
| 1275 | + body: HttpBody.text( |
| 1276 | + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1.0.0"}}}`, |
| 1277 | + "application/json", |
| 1278 | + ), |
| 1279 | + }); |
| 1280 | + if (initialize.status !== 200) return initialize.status; |
| 1281 | + const response = yield* httpClient.post("/mcp", { |
| 1282 | + headers: { |
| 1283 | + ...headers, |
| 1284 | + "mcp-session-id": initialize.headers["mcp-session-id"]!, |
| 1285 | + "mcp-protocol-version": "2025-06-18", |
| 1286 | + }, |
| 1287 | + body: HttpBody.text( |
| 1288 | + JSON.stringify({ |
| 1289 | + jsonrpc: "2.0", |
| 1290 | + id: 2, |
| 1291 | + method: "tools/call", |
| 1292 | + params: { name: "starts", arguments: args }, |
| 1293 | + }), |
| 1294 | + "application/json", |
| 1295 | + ), |
| 1296 | + }); |
| 1297 | + const body = yield* decodeOutcome(yield* response.text); |
| 1298 | + const outcome = yield* decodeText( |
| 1299 | + body.result.content.map((part) => part.text).join(""), |
| 1300 | + ); |
| 1301 | + return "code" in outcome ? outcome.code : outcome.modes; |
| 1302 | + }); |
| 1303 | + |
| 1304 | + // Approved for auto: without the header the client keeps its ceiling. |
| 1305 | + expect(yield* start(undefined)).toBe("auto/default"); |
| 1306 | + // The header narrows both modes, and asking above it is refused. |
| 1307 | + expect(yield* start("approval-required/plan")).toBe("approval-required/plan"); |
| 1308 | + expect(yield* start("approval-required/plan", { runtimeMode: "auto" })).toBe( |
| 1309 | + "runtime_mode_escalation_denied", |
| 1310 | + ); |
| 1311 | + expect(yield* start("auto-accept-edits/plan", { interactionMode: "default" })).toBe( |
| 1312 | + "interaction_mode_escalation_denied", |
| 1313 | + ); |
| 1314 | + // A broader header changes nothing: the approved ceiling still holds. |
| 1315 | + expect(yield* start("full-access/default")).toBe("auto/default"); |
| 1316 | + expect(yield* start("full-access/default", { runtimeMode: "full-access" })).toBe( |
| 1317 | + "runtime_mode_escalation_denied", |
| 1318 | + ); |
| 1319 | + // A malformed header is refused before anything runs. |
| 1320 | + expect(yield* start("auto")).toBe(400); |
| 1321 | + expect(yield* start("auto/default/extra")).toBe(400); |
| 1322 | + expect(yield* start("root/default")).toBe(400); |
| 1323 | + }), |
| 1324 | + ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 1325 | +); |
0 commit comments