package p.studio.app.mcp; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.node.ObjectNode; import org.junit.jupiter.api.*; import org.junit.jupiter.api.io.TempDir; import p.studio.api.automation.*; import p.studio.api.operation.*; import p.studio.api.shipper.*; import p.studio.app.operation.ExecutorOperationExecutor; import p.studio.core.project.*; import p.studio.core.runtime.HeadlessGateway; import p.studio.core.session.*; import p.studio.core.shipper.SavedProjectPreparation; import java.nio.file.*; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.function.*; import static org.junit.jupiter.api.Assertions.*; class McpTaskToolsTest { String httpAuthorization; @TempDir Path root; final ObjectMapper mapper = new ObjectMapper(); final ExecutorService workers = Executors.newCachedThreadPool(); final ProjectAccessRegistry access = new ProjectAccessRegistry(); final AtomicInteger builds = new AtomicInteger(); final CountDownLatch buildEntered = new CountDownLatch(1), buildGate = new CountDownLatch(1); volatile boolean holdBuild, failBuild, infrastructureFailure; DefaultAutomationSession session; DefaultTaskTools service; ProjectCatalogService catalog; McpToolRegistry tools; FakeRuntime runtime; String task; @BeforeEach void setup() throws Exception { root = root.toRealPath(); Files.writeString(root.resolve("prometeu.json"), "{\"name\":\"game\",\"version\":\"1.0.0\",\"language\":\"pbs\",\"stdlib\":\"1\",\"target\":\"Game\",\"dependencies\":[]}"); catalog = new ProjectCatalogService(root.getParent(), mapper, new ProjectLocalStudioSetupStore(mapper)); session = new DefaultAutomationSession(root, new ExecutorOperationExecutor(workers)); access.register(root, session::enterManual, () -> !session.snapshot().closed(), session); compose((project, sink) -> { builds.incrementAndGet(); buildEntered.countDown(); if (holdBuild) awaitGate(buildGate); if (infrastructureFailure) throw new IllegalStateException("test failure"); return failBuild ? ShipperPrepareResult.failed(List.of(), List.of("compile error")) : new ShipperPrepareResult(ShipperPreparationStatus.SUCCESS, List.of(), root.resolve("build/manifest.json"), List.of(), List.of()); }); } void compose(SavedProjectPreparation preparation) { service = new DefaultTaskTools(access, catalog, preparation, Optional.of((project, buildId, build) -> { runtime = new FakeRuntime(buildId); return runtime; })); tools = new McpToolRegistry(new McpToolCatalog(mapper), mapper, McpTaskTools.handlers(service, mapper, true)); } @AfterEach void close() throws Exception { buildGate.countDown(); if (runtime != null) { runtime.gate.countDown(); runtime.shutdownGate.complete(null); } session.closeAsync().toCompletableFuture().get(5, TimeUnit.SECONDS); workers.shutdownNow(); } ObjectNode args() { var args = mapper.createObjectNode().put("projectRoot", root.toString()); if (task != null) args.put("taskId", task); return args; } ObjectNode rootOnly() { return mapper.createObjectNode().put("projectRoot", root.toString()); } JsonNode call(String name, ObjectNode args) { return tools.call(name, args).wire(); } JsonNode value(JsonNode response) { assertFalse(response.path("isError").asBoolean(), response.toString()); return response.path("structuredContent").path("value"); } void error(String code, JsonNode response) { assertEquals(code, response.path("structuredContent").path("error").path("code").asText(), response.toString()); } void acquire() { session.grant(false).toCompletableFuture().join(); task = value(call("studio_control_acquire", rootOnly())).path("taskId").asText(); } JsonNode poll(String request) { return value(call("studio_operation_get", args().put("clientRequestId", request))); } JsonNode completed(String request) throws Exception { long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); JsonNode result; do { result = poll(request); if (result.path("workerTerminated").asBoolean()) return result; Thread.sleep(5); } while (System.nanoTime() < deadline); fail("Operation did not finish: " + result); return null; } String buildAndBoot() throws Exception { call("studio_build_start", args().put("clientRequestId", "build")); String id = completed("build").path("result").path("buildId").asText(); value(call("studio_headless_boot", args().put("buildId", id))); return id; } @Test void grantIsHumanOnlyAndDuplicateBuildsShareOneOperationAcrossRacingClients() throws Exception { error("NOT_GRANTED", call("studio_control_acquire", rootOnly())); acquire(); holdBuild = true; var start = new CountDownLatch(1); var results = new ArrayList>(); for (int n = 0; n < 20; n++) results.add(workers.submit(() -> { start.await(); return value(call("studio_build_start", args().put("clientRequestId", "same"))).path("operationId").asText(); })); start.countDown(); var ids = new HashSet(); for (var result : results) ids.add(result.get(2, TimeUnit.SECONDS)); assertEquals(1, ids.size()); assertTrue(buildEntered.await(2, TimeUnit.SECONDS)); assertEquals(1, builds.get()); assertEquals(ids.iterator().next(), poll("same").path("operationId").asText()); // lost receipt lookup error("BUSY", call("studio_build_start", args().put("clientRequestId", "different"))); error("CLIENT_REQUEST_CONFLICT", call("studio_headless_advance", args().put("clientRequestId", "same").put("frames", 1))); assertThrows(ControlDeniedException.class, session::enterManual); buildGate.countDown(); assertEquals("SUCCESS", completed("same").path("result").path("status").asText()); value(call("studio_build_start", args().put("clientRequestId", "same"))); assertEquals(1, builds.get()); } @Test void failuresRetainAuthorityAndFreshTaskOrServiceDoesNotRecoverOldOperations() throws Exception { acquire(); failBuild = true; value(call("studio_build_start", args().put("clientRequestId", "bad"))); var result = completed("bad"); assertEquals("FAILED", result.path("result").path("status").asText()); assertTrue(result.path("result").path("diagnostics").toString().contains("compile error")); assertEquals(ControlState.AGENT_WORKING, session.snapshot().state()); failBuild = false; infrastructureFailure = true; value(call("studio_build_start", args().put("clientRequestId", "infra"))); assertEquals("INFRASTRUCTURE_FAILURE", completed("infra").path("failure").path("code").asText()); assertEquals(ControlState.AGENT_WORKING, session.snapshot().state()); String old = task; value(call("studio_control_release", args())); error("INVALID_TASK", call("studio_operation_get", args().put("clientRequestId", "bad"))); acquire(); assertNotEquals(old, task); error("OPERATION_NOT_FOUND", call("studio_operation_get", args().put("clientRequestId", "bad"))); var restarted = new DefaultTaskTools(access, catalog, (p, s) -> { throw new AssertionError("No replay"); }, Optional.empty()); assertEquals(AutomationFailure.Code.OPERATION_NOT_FOUND, assertThrows(AutomationException.class, () -> restarted.operation(root, task, new OperationLookup(null, "bad"))).failure().code()); } @Test void headlessHandlersShareCanonicalPixelsWithDevAndNeverRepeatAnAdvance() throws Exception { acquire(); buildAndBoot(); value(call("studio_headless_set_input", args().set("input", mapper.createObjectNode().put("a", true)))); assertEquals(true, runtime.input.get("a")); assertEquals(false, runtime.input.get("up")); assertEquals(0, runtime.input.get("touchY")); var receipt = value(call("studio_headless_advance", args().put("clientRequestId", "advance").put("frames", 5))); var result = completed("advance"); assertEquals(1, runtime.advances.get()); assertEquals(5, result.path("progress").path("completedFrames").asInt()); assertEquals(receipt.path("operationId"), value(call("studio_headless_advance", args().put("clientRequestId", "advance").put("frames", 5))).path("operationId")); error("CLIENT_REQUEST_CONFLICT", call("studio_headless_advance", args().put("clientRequestId", "advance").put("frames", 6))); var wire = call("studio_operation_get", args().put("clientRequestId", "advance")); assertEquals("image", wire.path("content").get(1).path("type").asText()); assertArrayEquals(session.snapshot().capture().png(), Base64.getDecoder().decode(wire.path("content").get(1).path("data").asText())); assertEquals(900, result.path("capture").path("frameId").asInt()); assertEquals(5, result.path("capture").path("logicalFrameIndex").asInt()); value(call("studio_headless_capture", args())); for (var operation : List.of("state", "fault", "clear_input", "reset")) value(call("studio_headless_" + operation, args())); assertTrue(session.snapshot().stale()); assertTrue(poll("advance").path("capture").path("stale").asBoolean()); value(call("studio_headless_shutdown", args())); assertTrue(runtime.stopped); error("SESSION_NOT_FOUND", call("studio_headless_state", args())); assertEquals(ControlState.AGENT_WORKING, session.snapshot().state()); } @Test void runtimeFaultAndBoundsDoNotRevokeTaskAndRebuildStopsRuntimeFirst() throws Exception { acquire(); String oldBuild = buildAndBoot(); runtime.kind = AutomationOutcome.Kind.GUEST_FAULT; value(call("studio_headless_advance", args().put("clientRequestId", "fault").put("frames", 2))); assertEquals("GUEST_FAULT", completed("fault").path("result").path("status").asText()); error("LIMIT_EXCEEDED", call("studio_headless_advance", args().put("clientRequestId", "large").put("frames", 601))); error("BUSY", call("studio_headless_boot", args().put("buildId", oldBuild))); value(call("studio_build_start", args().put("clientRequestId", "rebuild"))); String next = completed("rebuild").path("result").path("buildId").asText(); assertTrue(runtime.stopped); error("BUILD_NOT_FOUND", call("studio_headless_boot", args().put("buildId", oldBuild))); value(call("studio_headless_boot", args().put("buildId", next))); assertEquals(ControlState.AGENT_WORKING, session.snapshot().state()); } @Test void resetAllowsOnlyExplicitRebootOfTheSameKnownBuild() throws Exception { acquire(); String build = buildAndBoot(); var original = runtime; error("BUSY", call("studio_headless_boot", args().put("buildId", build))); value(call("studio_headless_reset", args())); error("BUILD_NOT_FOUND", call("studio_headless_boot", args().put("buildId", "unknown"))); value(call("studio_headless_boot", args().put("buildId", build))); assertSame(original, runtime); assertEquals(TaskTools.HeadlessAction.BOOT, runtime.lastAction); error("BUSY", call("studio_headless_boot", args().put("buildId", build))); } @Test void rebuildWaitsForShutdownAcknowledgementBeforeTouchingArtifacts() throws Exception { acquire(); String build = buildAndBoot(); runtime.shutdownGate = new CompletableFuture<>(); failBuild = true; value(call("studio_build_start", args().put("clientRequestId", "blocked-rebuild"))); assertTrue(runtime.shutdownEntered.await(2, TimeUnit.SECONDS)); assertEquals(1, builds.get()); assertFalse(poll("blocked-rebuild").path("workerTerminated").asBoolean()); error("BUSY", call("studio_headless_boot", args().put("buildId", build))); runtime.shutdownGate.complete(null); assertEquals("FAILED", completed("blocked-rebuild").path("result").path("status").asText()); assertEquals(2, builds.get()); error("BUILD_NOT_FOUND", call("studio_headless_boot", args().put("buildId", build))); assertEquals(ControlState.AGENT_WORKING, session.snapshot().state()); } @Test void cancellationPreservesKnownProgressAndReclaimWaitsForWorkerTermination() throws Exception { acquire(); buildAndBoot(); runtime.hold = true; String operation = value(call("studio_headless_advance", args().put("clientRequestId", "long").put("frames", 50))).path("operationId").asText(); assertTrue(runtime.entered.await(2, TimeUnit.SECONDS)); assertEquals(2, poll("long").path("progress").path("completedFrames").asInt()); assertTrue(value(call("studio_operation_cancel", args().put("operationId", operation))).path("cancellationRequested").asBoolean()); assertEquals(2, poll("long").path("progress").path("completedFrames").asInt()); assertEquals("CANCELLED", poll("long").path("state").asText()); assertFalse(poll("long").path("workerTerminated").asBoolean()); error("BUSY", call("studio_build_start", args().put("clientRequestId", "blocked"))); var returnControl = session.reclaim().toCompletableFuture(); assertFalse(returnControl.isDone()); error("INVALID_TASK", call("studio_headless_advance", args().put("clientRequestId", "long").put("frames", 50))); error("INVALID_TASK", call("studio_operation_cancel", args().put("operationId", operation))); runtime.gate.countDown(); assertTrue(returnControl.get(2, TimeUnit.SECONDS).success()); assertTrue(session.snapshot().manualAllowed()); assertTrue(runtime.stopped); } @Test void anotherProjectCannotReadCachedWorkAndNoGatewayIsAdvertisedAsAvailable() throws Exception { acquire(); buildAndBoot(); Path other = Files.createDirectory(root.resolve("other")); var another = new DefaultAutomationSession(other, new ExecutorOperationExecutor(workers)); access.register(other, another::enterManual, () -> true, another); another.grant(false).toCompletableFuture().join(); String otherTask = another.acquire().task().value(); error("INVALID_TASK", call("studio_operation_get", args().put("projectRoot", other.toString()).put("clientRequestId", "build"))); error("OPERATION_NOT_FOUND", call("studio_operation_get", args().put("projectRoot", other.toString()).put("taskId", otherTask).put("clientRequestId", "build"))); assertFalse(McpTaskTools.handlers(service, mapper, false).containsKey("studio_headless_boot")); another.closeAsync().toCompletableFuture().join(); } @Test void cooperativeCancellationPublishesTheSameFinalKnownCaptureWithoutReleasingAuthority() throws Exception { acquire(); buildAndBoot(); runtime.hold = true; String operation = value(call("studio_headless_advance", args().put("clientRequestId", "cancelled").put("frames", 50))).path("operationId").asText(); assertTrue(runtime.entered.await(2, TimeUnit.SECONDS)); value(call("studio_operation_cancel", args().put("operationId", operation))); runtime.gate.countDown(); long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(2); while (!poll("cancelled").path("workerTerminated").asBoolean() && System.nanoTime() < deadline) Thread.sleep(5); var result = poll("cancelled"); assertTrue(result.path("workerTerminated").asBoolean()); assertEquals("CANCELLED", result.path("result").path("status").asText()); assertEquals(2, result.path("progress").path("completedFrames").asInt()); assertFalse(result.path("capture").path("stale").asBoolean()); assertEquals(java.math.BigInteger.valueOf(2), session.snapshot().capture().logicalFrameIndex()); assertEquals(ControlState.AGENT_WORKING, session.snapshot().state()); assertFalse(value(call("studio_operation_cancel", args().put("operationId", operation))).path("cancellationRequested").asBoolean()); } @Test void realHttpBuildUsesExistingCompilerAndPackerAndRecoversReceiptByClientId() throws Exception { var docs = p.studio.compiler.FrontendRegistryService.require("pbs").documentation().orElseThrow().references(1, p.studio.AppMode.Game); for (var reference : docs) if (reference.id().startsWith("pbs:example:game:1:")) { Path file = root.resolve(reference.id().substring("pbs:example:game:1:".length())); Files.createDirectories(file.getParent()); Files.writeString(file, reference.text()); } try (var packer = p.packer.Packer.bootstrap(mapper, ignored -> {})) { var shipper = new p.studio.core.shipper.StudioShipperService(new p.studio.app.compiler.CompilerBuildGatewayAdapter(), packer.workspaceService(), mapper, new ExecutorOperationExecutor(workers), access); compose(shipper); try (var server = new LocalMcpServer(mapper, tools); var client = java.net.http.HttpClient.newHttpClient()) { server.start(); var credential = server.credentials().prepare(root.toRealPath()); credential.activate(); httpAuthorization = "Bearer " + credential.secret(); var initialize = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", 1).put("method", "initialize"); var params = initialize.putObject("params").put("protocolVersion", "2025-06-18"); params.putObject("capabilities"); params.putObject("clientInfo").put("name", "task-test").put("version", "1"); String connection = post(client, server, null, initialize).headers().firstValue("Mcp-Session-Id").orElseThrow(); post(client, server, connection, mapper.createObjectNode().put("jsonrpc", "2.0").put("method", "notifications/initialized")); error("NOT_GRANTED", httpCall(client, server, connection, "studio_control_acquire", rootOnly())); session.grant(false).toCompletableFuture().join(); task = value(httpCall(client, server, connection, "studio_control_acquire", rootOnly())).path("taskId").asText(); assertThrows(ControlDeniedException.class, () -> shipper.prepare(catalog.openProject(root), ignored -> {})); // Discard the initial response, as a client with a lost receipt would do. httpCall(client, server, connection, "studio_build_start", args().put("clientRequestId", "http-build")); var result = completed("http-build"); assertEquals("SUCCESS", result.path("result").path("status").asText(), result.toString()); assertTrue(Files.isRegularFile(Path.of(result.path("result").path("manifestPath").asText()))); var recovered = value(httpCall(client, server, connection, "studio_operation_get", args().put("clientRequestId", "http-build"))); assertEquals(result.path("operationId"), recovered.path("operationId")); assertEquals(result.path("operationId"), value(httpCall(client, server, connection, "studio_build_start", args().put("clientRequestId", "http-build"))).path("operationId")); value(httpCall(client, server, connection, "studio_control_release", args())); error("INVALID_TASK", httpCall(client, server, connection, "studio_operation_get", args().put("clientRequestId", "http-build"))); assertTrue(session.snapshot().manualAllowed()); } } } @Test void allToolGroupsConformOverHttpWithRealLspAndTestOnlyRuntime() throws Exception { Path source = Files.createDirectories(root.resolve("src")).resolve("main.pbs"); Files.writeString(source, "fn main() -> int { return 1; }\n"); var lifecycle = new p.studio.lsp.services.LspServerLifecycleImpl(); var lsp = new p.studio.app.lsp.LspLifecycleAdapter(lifecycle); var project = catalog.openProject(root); lsp.boot(project); var knowledge = new p.studio.app.compiler.CompilerProjectKnowledge(access, catalog, mapper); var language = new p.studio.core.language.TaskLanguageQueries(access, new p.studio.app.lsp.AutomationLspQueries(lifecycle, catalog, knowledge, mapper)); var handlers = new LinkedHashMap<>(McpTaskTools.handlers(service, mapper, true)); handlers.putAll(McpProjectKnowledgeTools.handlers(knowledge, mapper)); handlers.putAll(McpLanguageTools.handlers(language, mapper)); try (var server = new LocalMcpServer(mapper, new McpToolRegistry(new McpToolCatalog(mapper), mapper, handlers)); var client = java.net.http.HttpClient.newHttpClient()) { server.start(); var credential = server.credentials().prepare(root.toRealPath()); credential.activate(); httpAuthorization = "Bearer " + credential.secret(); var unauthorized = java.net.http.HttpRequest.newBuilder(server.connection().endpoint()).POST(java.net.http.HttpRequest.BodyPublishers.ofString("{}")) .timeout(java.time.Duration.ofSeconds(5)).build(); assertEquals(401, client.send(unauthorized, java.net.http.HttpResponse.BodyHandlers.discarding()).statusCode()); String first = connect(client, server), second = connect(client, server); var list = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", 1).put("method", "tools/list"); assertEquals(17, mapper.readTree(post(client, server, first, list).body()).path("result").path("tools").size()); var context = value(httpCall(client, server, first, "studio_project_context", rootOnly())); assertTrue(context.path("languages").toString().contains("pbs")); value(httpCall(client, server, first, "studio_reference_read", rootOnly().put("referenceId", "pbs:quickstart").put("limit", 100))); error("NOT_GRANTED", httpCall(client, server, first, "studio_control_acquire", rootOnly())); error("INVALID_TASK", httpCall(client, server, first, "studio_build_start", args().put("taskId", "missing").put("clientRequestId", "denied"))); session.grant(false).toCompletableFuture().join(); var a = workers.submit(() -> httpCall(client, server, first, "studio_control_acquire", rootOnly())); var b = workers.submit(() -> httpCall(client, server, second, "studio_control_acquire", rootOnly())); var results = List.of(a.get(5, TimeUnit.SECONDS), b.get(5, TimeUnit.SECONDS)); assertEquals(1, results.stream().filter(r -> !r.path("isError").asBoolean()).count()); task = value(results.stream().filter(r -> !r.path("isError").asBoolean()).findFirst().orElseThrow()).path("taskId").asText(); var query = args().put("operation", "diagnostics"); query.putObject("parameters").put("uri", source.toUri().toString()); value(httpCall(client, server, first, "studio_language_query", query)); Files.writeString(source, "fn main( {"); assertFalse(value(httpCall(client, server, first, "studio_language_query", query)).path("result").isEmpty()); Files.writeString(source, "fn main() -> int { return 2; }\n"); value(httpCall(client, server, first, "studio_language_query", query)); httpCall(client, server, first, "studio_build_start", args().put("clientRequestId", "conformance-build")); var build = httpCompleted(client, server, first, "conformance-build"); value(httpCall(client, server, first, "studio_headless_boot", args().put("buildId", build.path("result").path("buildId").asText()))); for (var op : List.of("reset", "clear_input", "state", "fault")) value(httpCall(client, server, first, "studio_headless_" + op, args())); value(httpCall(client, server, first, "studio_headless_set_input", args().set("input", mapper.createObjectNode().put("a", true)))); value(httpCall(client, server, first, "studio_headless_advance", args().put("clientRequestId", "conformance-advance").put("frames", 5))); var advanced = httpCompleted(client, server, first, "conformance-advance"); assertEquals("SUCCEEDED", advanced.path("result").path("status").asText()); var captured = httpCall(client, server, first, "studio_headless_capture", args()); value(captured); assertArrayEquals(session.snapshot().capture().png(), Base64.getDecoder().decode(captured.path("content").get(1).path("data").asText())); var deleted = java.net.http.HttpRequest.newBuilder(server.connection().endpoint()).DELETE() .header("Authorization", httpAuthorization).header("Mcp-Session-Id", first).build(); assertEquals(200, client.send(deleted, java.net.http.HttpResponse.BodyHandlers.discarding()).statusCode()); assertEquals(ControlState.AGENT_WORKING, session.snapshot().state()); String reconnected = connect(client, server); assertEquals(advanced.path("operationId"), value(httpCall(client, server, reconnected, "studio_operation_get", args().put("clientRequestId", "conformance-advance"))).path("operationId")); runtime.kind = AutomationOutcome.Kind.GUEST_FAULT; value(httpCall(client, server, reconnected, "studio_headless_advance", args().put("clientRequestId", "guest").put("frames", 2))); assertEquals("GUEST_FAULT", httpCompleted(client, server, reconnected, "guest").path("result").path("status").asText()); value(httpCall(client, server, reconnected, "studio_operation_cancel", args().put("operationId", advanced.path("operationId").asText()))); runtime.hold = true; String pending = value(httpCall(client, server, reconnected, "studio_headless_advance", args().put("clientRequestId", "cancel-over-http").put("frames", 50))).path("operationId").asText(); assertTrue(value(httpCall(client, server, reconnected, "studio_operation_cancel", args().put("operationId", pending))) .path("cancellationRequested").asBoolean()); runtime.gate.countDown(); assertEquals("CANCELLED", httpCompleted(client, server, reconnected, "cancel-over-http").path("state").asText()); value(httpCall(client, server, reconnected, "studio_headless_shutdown", args())); value(httpCall(client, server, reconnected, "studio_control_release", args())); error("INVALID_TASK", httpCall(client, server, reconnected, "studio_operation_get", args().put("clientRequestId", "conformance-advance"))); session.grant(false).toCompletableFuture().join(); task = value(httpCall(client, server, reconnected, "studio_control_acquire", rootOnly())).path("taskId").asText(); session.reclaim().toCompletableFuture().join(); error("INVALID_TASK", httpCall(client, server, reconnected, "studio_build_start", args().put("clientRequestId", "late"))); } finally { lsp.shutdown(project); } } String connect(java.net.http.HttpClient client, LocalMcpServer server) throws Exception { var initialize = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", 1).put("method", "initialize"); var params = initialize.putObject("params").put("protocolVersion", "2025-06-18"); params.putObject("capabilities"); params.putObject("clientInfo").put("name", "conformance-test").put("version", "1"); String connection = post(client, server, null, initialize).headers().firstValue("Mcp-Session-Id").orElseThrow(); assertEquals(202, post(client, server, connection, mapper.createObjectNode().put("jsonrpc", "2.0").put("method", "notifications/initialized")).statusCode()); return connection; } JsonNode httpCompleted(java.net.http.HttpClient client, LocalMcpServer server, String connection, String request) throws Exception { long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); do { var result = value(httpCall(client, server, connection, "studio_operation_get", args().put("clientRequestId", request))); if (result.path("workerTerminated").asBoolean()) return result; Thread.sleep(5); } while (System.nanoTime() < deadline); throw new AssertionError("Operation did not terminate"); } JsonNode httpCall(java.net.http.HttpClient client, LocalMcpServer server, String connection, String name, ObjectNode arguments) throws Exception { var request = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", UUID.randomUUID().toString()).put("method", "tools/call"); request.putObject("params").put("name", name).set("arguments", arguments); var response = post(client, server, connection, request); assertEquals(200, response.statusCode(), response.body()); var body = mapper.readTree(response.body()); assertFalse(body.has("error"), body.toString()); return body.path("result"); } java.net.http.HttpResponse post(java.net.http.HttpClient client, LocalMcpServer server, String connection, JsonNode body) throws Exception { var request = java.net.http.HttpRequest.newBuilder(server.connection().endpoint()).timeout(java.time.Duration.ofSeconds(10)) .header("Authorization", httpAuthorization).header("Content-Type", "application/json") .header("Accept", "application/json, text/event-stream").header("MCP-Protocol-Version", "2025-06-18"); if (connection != null) request.header("Mcp-Session-Id", connection); return client.send(request.POST(java.net.http.HttpRequest.BodyPublishers.ofString(body.toString())).build(), java.net.http.HttpResponse.BodyHandlers.ofString()); } static void awaitGate(CountDownLatch gate) { boolean interrupted = false; while (true) { try { gate.await(); break; } catch (InterruptedException ignored) { interrupted = true; } } if (interrupted) Thread.currentThread().interrupt(); } final class FakeRuntime implements HeadlessGateway.Session { final String id = UUID.randomUUID().toString(), build; final AtomicInteger advances = new AtomicInteger(); final CountDownLatch entered = new CountDownLatch(1), gate = new CountDownLatch(1); final CountDownLatch shutdownEntered = new CountDownLatch(1); CompletableFuture shutdownGate = CompletableFuture.completedFuture(null); TaskTools.HeadlessAction lastAction; volatile boolean stopped, hold, cancelled; volatile AutomationOutcome.Kind kind = AutomationOutcome.Kind.SUCCEEDED; Map input; FakeRuntime(String build) { this.build = build; } public String sessionId() { return id; } public void cancel() { cancelled = true; } public CompletionStage shutdown() { shutdownEntered.countDown(); return shutdownGate.thenRun(() -> stopped = true); } public long maxFramesPerAdvance() { return 600; } CanonicalCapture capture(long logical) { return new CanonicalCapture(id, build, logical, 900, 1, 1, 1, Base64.getDecoder().decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aK1kAAAAASUVORK5CYII=")); } public TaskTools.HeadlessReply call(TaskTools.HeadlessAction action, Map input) { this.input = input; lastAction = action; return new TaskTools.HeadlessReply(Map.of("sessionId", id), action == TaskTools.HeadlessAction.CAPTURE ? capture(5) : null); } public AutomationOutcome advance(long frames, Consumer progress) { advances.incrementAndGet(); progress.accept(new TrackedOperation.Progress(frames, Math.min(2, frames))); entered.countDown(); if (hold) awaitGate(gate); long completed = cancelled ? Math.min(2, frames) : frames; return new AutomationOutcome(cancelled ? AutomationOutcome.Kind.CANCELLED : kind, frames, completed, List.of(), capture(completed)); } } }