feat(studio): integrate project MCP setup and lifecycle
Intrepid/Prometeu/Studio/pipeline/pr-master This commit looks good
Test / Build skipped: 15, passed: 926
Intrepid/Prometeu/Studio/pipeline/head This commit looks good

This commit is contained in:
2026-09-29 19:24:28 +01:00
parent 97cf5e1919
commit d0737df0ac
48 changed files with 2048 additions and 225 deletions
@@ -39,6 +39,7 @@ class HeadlessMcpDesktopTest {
LocalMcpServer server;
HttpClient client;
String connection, task, build, session;
String authorization = "Bearer unavailable";
Path installation, source;
DevWorkspace dev;
Stage stage;
@@ -76,9 +77,9 @@ class HeadlessMcpDesktopTest {
stage = new Stage(); stage.setTitle("Real MCP / headless / DEV conformance");
stage.setScene(new Scene((javafx.scene.Parent) dev.rootNode(), 1000, 800)); stage.show(); dev.load();
});
connect();
}
void connect() throws Exception {
authorization = "Bearer " + p.studio.app.mcp.ProjectConnectionFiles.read(root, mapper).credential();
connection = null;
var initialize = request("initialize");
var params = initialize.putObject("params").put("protocolVersion", "2025-06-18");
@@ -96,8 +97,15 @@ class HeadlessMcpDesktopTest {
assertTrue(owned.stream().noneMatch(ProcessHandle::isAlive), "No managed runtime may survive the test");
}
void acquire() throws Exception {
void grant() throws Exception {
fx(() -> ((Button) dev.rootNode().lookup("#dev-grant")).fire());
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
while (project.automation().snapshot().state() != ControlState.WAITING_FOR_AGENT && System.nanoTime() < deadline) Thread.sleep(10);
assertEquals(ControlState.WAITING_FOR_AGENT, project.automation().snapshot().state());
connect();
}
void acquire() throws Exception {
grant();
task = value(call("studio_control_acquire", args())).path("taskId").asText();
}
void prepareLongRun() throws Exception {
@@ -121,7 +129,7 @@ class HeadlessMcpDesktopTest {
prepareLongRun();
var receipt = startRunning("cancel-me");
var delete = HttpRequest.newBuilder(server.connection().endpoint()).DELETE()
.header("Authorization", server.connection().authorizationHeader()).header("Mcp-Session-Id", connection).build();
.header("Authorization", authorization).header("Mcp-Session-Id", connection).build();
assertEquals(200, client.send(delete, HttpResponse.BodyHandlers.discarding()).statusCode());
client.close(); client = HttpClient.newHttpClient(); connect();
assertEquals(ControlState.AGENT_WORKING, project.automation().snapshot().state());
@@ -159,7 +167,7 @@ class HeadlessMcpDesktopTest {
refreshEntered.get(10, TimeUnit.SECONDS);
assertEquals(ControlState.RETURNING_CONTROL, project.automation().snapshot().state());
assertFalse(project.automation().snapshot().manualAllowed());
assertEquals("INVALID_TASK", call("studio_operation_get", args().put("clientRequestId", "revoked")).path("structuredContent").path("error").path("code").asText());
assertEquals(401, post(request("ping")).statusCode());
assertSame(lastCapture, project.automation().snapshot().capture(), "Revoked work must not publish a late frame");
refreshGate.complete(null);
assertEquals(ControlState.MANUAL, project.automation().snapshot().state());
@@ -169,19 +177,19 @@ class HeadlessMcpDesktopTest {
}
@Test void waitingRevokeProjectCloseAndReopenNeverRestoreAuthority() throws Exception {
fx(() -> ((Button) dev.rootNode().lookup("#dev-grant")).fire());
grant();
fx(() -> ((Button) dev.rootNode().lookup("#dev-reclaim")).fire());
assertEquals(ControlState.MANUAL, project.automation().snapshot().state());
assertEquals("NOT_GRANTED", call("studio_control_acquire", args()).path("structuredContent").path("error").path("code").asText());
assertEquals(401, post(request("ping")).statusCode());
prepareLongRun(); startRunning("close-project");
fx(dev::unLoad);
assertEquals(ControlState.AGENT_WORKING, project.automation().snapshot().state(), "DEV does not own task lifetime");
project.close();
assertTrue(owned.stream().noneMatch(ProcessHandle::isAlive));
assertEquals("PROJECT_NOT_OPEN", call("studio_operation_get", args().put("clientRequestId", "close-project")).path("structuredContent").path("error").path("code").asText());
assertEquals(401, post(request("ping")).statusCode());
project = app.getBean(ProjectSessionFactory.class).open(project.projectReference());
assertEquals(ControlState.MANUAL, project.automation().snapshot().state());
assertEquals("INVALID_TASK", call("studio_build_start", args().put("clientRequestId", "late")).path("structuredContent").path("error").path("code").asText());
assertEquals(401, post(request("ping")).statusCode());
}
@Test void applicationContextShutdownClosesActiveProjectAndRuntime() throws Exception {
@@ -270,13 +278,13 @@ class HeadlessMcpDesktopTest {
}
@Test void fullHttpLoopMatchesRawRuntimePixelsAndDisplayedDevAcrossResetAndRebuild() throws Exception {
assertEquals(401, post(request("ping")).statusCode());
grant();
assertEquals(17, mapper.readTree(post(request("tools/list")).body()).path("result").path("tools").size());
assertTrue(value(call("studio_project_context", args())).path("languages").toString().contains("pbs"));
value(call("studio_reference_read", args().put("referenceId", "pbs:quickstart").put("limit", 100)));
var sdk = value(call("studio_reference_read", args().put("referenceId", "pbs:sdk:game:1:sdk/input/main.pbs").put("limit", 1000)));
assertTrue(sdk.toString().contains("InputPad"), "Consult installed SDK source through the indexed read-only reference");
assertEquals("NOT_GRANTED", call("studio_control_acquire", args()).path("structuredContent").path("error").path("code").asText());
fx(() -> ((Button) dev.rootNode().lookup("#dev-grant")).fire());
assertEquals(ControlState.WAITING_FOR_AGENT, project.automation().snapshot().state());
task = value(call("studio_control_acquire", args())).path("taskId").asText();
assertFalse(task.isBlank());
@@ -442,7 +450,7 @@ class HeadlessMcpDesktopTest {
}
HttpResponse<String> post(JsonNode body) throws Exception {
var request = HttpRequest.newBuilder(server.connection().endpoint()).timeout(Duration.ofSeconds(15))
.header("Authorization", server.connection().authorizationHeader()).header("Content-Type", "application/json")
.header("Authorization", authorization).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(HttpRequest.BodyPublishers.ofString(body.toString())).build(), HttpResponse.BodyHandlers.ofString());