verify automation interruption recovery and manual debug lifecycle

This commit is contained in:
2026-09-29 12:06:48 +01:00
parent 4f148a50de
commit e6a83c94e0
10 changed files with 300 additions and 17 deletions
@@ -76,6 +76,10 @@ 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 {
connection = null;
var initialize = request("initialize");
var params = initialize.putObject("params").put("protocolVersion", "2025-06-18");
params.putObject("capabilities"); params.putObject("clientInfo").put("name", "real-loop-test").put("version", "1");
@@ -92,6 +96,179 @@ class HeadlessMcpDesktopTest {
assertTrue(owned.stream().noneMatch(ProcessHandle::isAlive), "No managed runtime may survive the test");
}
void acquire() throws Exception {
fx(() -> ((Button) dev.rootNode().lookup("#dev-grant")).fire());
task = value(call("studio_control_acquire", args())).path("taskId").asText();
}
void prepareLongRun() throws Exception {
// Real guest work below the watchdog budget makes a 600-frame call observable in flight.
Files.writeString(source, Files.readString(source).replace("fn frame() -> void {",
"fn frame() -> void { let spin: int = 0; while (spin < 10000) { spin = spin + 1; }"));
acquire(); build("long-build"); boot();
}
JsonNode startRunning(String requestId) throws Exception {
var receipt = value(call("studio_headless_advance", args().put("clientRequestId", requestId).put("frames", 600)));
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
do {
var status = value(call("studio_operation_get", args().put("clientRequestId", requestId)));
assertFalse(status.path("workerTerminated").asBoolean(), status.toString());
if (status.path("progress").path("completedFrames").asInt() > 0) return receipt;
Thread.sleep(5);
} while (System.nanoTime() < deadline);
throw new AssertionError("No runtime progress");
}
@Test void disconnectAndDuplicateReceiptDoNotReplayAndCancellationRetainsTask() throws Exception {
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();
assertEquals(200, client.send(delete, HttpResponse.BodyHandlers.discarding()).statusCode());
client.close(); client = HttpClient.newHttpClient(); connect();
assertEquals(ControlState.AGENT_WORKING, project.automation().snapshot().state());
assertEquals(receipt.path("operationId"), value(call("studio_headless_advance",
args().put("clientRequestId", "cancel-me").put("frames", 600))).path("operationId"));
value(call("studio_operation_cancel", args().put("operationId", receipt.path("operationId").asText())));
var result = value(completed("cancel-me"));
assertEquals("CANCELLED", result.path("result").path("status").asText(), result.toString());
assertTrue(result.path("progress").path("completedFrames").asInt() < 600);
assertEquals(ControlState.AGENT_WORKING, project.automation().snapshot().state());
assertEquals(session, value(call("studio_headless_state", args())).path("sessionId").asText());
assertArrayEquals(png(call("studio_operation_get", args().put("clientRequestId", "cancel-me"))), project.automation().snapshot().capture().png());
}
@Test void revokeWaitsForRuntimeAndDocumentRefreshAndRejectsLateCalls() throws Exception {
prepareLongRun();
value(call("studio_headless_advance", args().put("clientRequestId", "before-revoke").put("frames", 1)));
completed("before-revoke");
var lastCapture = project.automation().snapshot().capture();
var refreshEntered = new CompletableFuture<Void>(); var refreshGate = new CompletableFuture<Void>();
var refreshed = new java.util.concurrent.atomic.AtomicReference<String>();
var handoff = project.automation().registerDocuments(new DocumentHandoff() {
public CompletionStage<Boolean> hasUnsavedChanges() { return CompletableFuture.completedFuture(false); }
public CompletionStage<Void> saveAll() { throw new AssertionError("Do not write old buffers"); }
public CompletionStage<Void> refreshFromDisk() {
assertTrue(owned.stream().noneMatch(ProcessHandle::isAlive), "Runtime must stop before refresh");
refreshEntered.complete(null);
return refreshGate.thenRun(() -> { try { refreshed.set(Files.readString(source)); } catch (Exception failure) { throw new RuntimeException(failure); } });
}
});
try {
String edited = Files.readString(source) + "\n// external edit retained on revoke\n"; Files.writeString(source, edited);
startRunning("revoked");
fx(() -> ((Button) dev.rootNode().lookup("#dev-reclaim")).fire());
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());
assertSame(lastCapture, project.automation().snapshot().capture(), "Revoked work must not publish a late frame");
refreshGate.complete(null);
assertEquals(ControlState.MANUAL, project.automation().snapshot().state());
assertEquals(edited, refreshed.get()); assertEquals(edited, Files.readString(source));
assertStale(true);
} finally { refreshGate.complete(null); handoff.close(); }
}
@Test void waitingRevokeProjectCloseAndReopenNeverRestoreAuthority() throws Exception {
fx(() -> ((Button) dev.rootNode().lookup("#dev-grant")).fire());
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());
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());
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());
}
@Test void applicationContextShutdownClosesActiveProjectAndRuntime() throws Exception {
prepareLongRun(); startRunning("close-app");
app.close();
assertTrue(project.automation().snapshot().closed());
assertTrue(owned.stream().noneMatch(ProcessHandle::isAlive));
assertFalse(server.isRunning());
}
@Test void nonresponsiveOwnedHostIsKilledAndHumanCanRetryReclaim() throws Exception {
prepareLongRun(); startRunning("frozen");
var child = owned.stream().filter(p -> p.info().command().orElse("").endsWith("prometeu-headless")).findFirst().orElseThrow();
// POSIX desktop gate: freeze only this test's observed child, never a discovered user process.
assertEquals(0, new ProcessBuilder("/bin/kill", "-STOP", Long.toString(child.pid())).start().waitFor());
var outcome = project.automation().reclaim().toCompletableFuture().get(12, TimeUnit.SECONDS);
assertEquals(ControlResult.Status.CLEANUP_FAILED, outcome.status());
assertFalse(project.automation().snapshot().manualAllowed());
assertTrue(owned.stream().noneMatch(ProcessHandle::isAlive));
assertTrue(project.automation().reclaim().toCompletableFuture().get(5, TimeUnit.SECONDS).success());
assertTrue(project.automation().snapshot().manualAllowed());
}
@Test void realManualDebugStillRunsAfterAgentReleaseAndBlocksNewGrant() throws Exception {
// The existing manual protocol owns port 7777. Do not connect to someone else's Debug.
try (var probe = new java.net.ServerSocket()) { probe.bind(new java.net.InetSocketAddress("127.0.0.1", 7777)); }
acquire(); build("before-debug"); boot();
value(call("studio_control_release", args()));
try (var debug = app.getBean(p.studio.api.execution.ExecutionControllerFactory.class)
.create(project.projectReference(), project.projectLocalStudioSetup(), project.executionSession())) {
var running = debug.play().completion().toCompletableFuture().get(15, TimeUnit.SECONDS);
assertEquals(p.studio.api.execution.ExecutionState.RUNNING, running.state(), running.toString());
ProcessHandle.current().descendants().filter(p -> p.info().command().orElse("").startsWith(installation + "/")).forEach(owned::add);
assertEquals(ControlResult.Status.BUSY, project.automation().grant(false).toCompletableFuture().get(2, TimeUnit.SECONDS).status());
debug.stop();
assertEquals(p.studio.api.execution.ExecutionState.STOPPED, project.executionSession().snapshot().state());
}
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(7);
while ((!project.automation().snapshot().canGrant() || owned.stream().anyMatch(ProcessHandle::isAlive)) && System.nanoTime() < deadline) Thread.sleep(10);
assertTrue(owned.stream().noneMatch(ProcessHandle::isAlive));
assertTrue(project.automation().grant(false).toCompletableFuture().get(3, TimeUnit.SECONDS).success());
assertTrue(project.automation().reclaim().toCompletableFuture().get(3, TimeUnit.SECONDS).success());
}
@Test void forcedHostLossPreservesKnownProgressAndRequiresExplicitNewSession() throws Exception {
prepareLongRun(); startRunning("crash");
var child = owned.stream().filter(p -> p.info().command().orElse("").endsWith("prometeu-headless")).findFirst().orElseThrow();
assertTrue(child.destroyForcibly()); child.onExit().get(5, TimeUnit.SECONDS);
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
JsonNode result;
do { result = value(call("studio_operation_get", args().put("clientRequestId", "crash"))); if (result.path("workerTerminated").asBoolean()) break; Thread.sleep(5); }
while (System.nanoTime() < deadline);
assertTrue(result.path("workerTerminated").asBoolean());
assertEquals("INFRASTRUCTURE_FAILURE", result.path("failure").path("code").asText());
assertTrue(result.path("progress").path("completedFrames").asInt() < 600);
assertEquals(ControlState.AGENT_WORKING, project.automation().snapshot().state());
assertEquals(result.path("operationId"), value(call("studio_headless_advance", args().put("clientRequestId", "crash").put("frames", 600))).path("operationId"));
value(call("studio_headless_shutdown", args())); String previous = session; boot(); assertNotEquals(previous, session);
value(call("studio_headless_advance", args().put("clientRequestId", "after-crash").put("frames", 1)));
assertEquals("SUCCEEDED", value(completed("after-crash")).path("result").path("status").asText());
}
@Test void compileErrorAndGuestFaultCanBeCorrectedThroughHttpWithinTheSameTask() throws Exception {
acquire(); String identity = task; String original = Files.readString(source);
build("good"); boot();
Files.writeString(source, "fn frame( {");
var query = args().put("operation", "diagnostics"); query.putObject("parameters").put("uri", source.toUri().toString());
assertFalse(value(call("studio_language_query", query)).path("result").isEmpty());
value(call("studio_build_start", args().put("clientRequestId", "invalid")));
var failed = value(completed("invalid")).path("result");
assertEquals("FAILED", failed.path("status").asText()); assertFalse(failed.path("diagnostics").isEmpty());
assertEquals(identity, project.automation().snapshot().task().value());
Files.writeString(source, original.replace("x = x + 1;", "x = x / (x - x);"));
build("guest-fault"); boot();
value(call("studio_headless_advance", args().put("clientRequestId", "faulted").put("frames", 3)));
var fault = value(completed("faulted")).path("result");
assertEquals("GUEST_FAULT", fault.path("status").asText()); assertFalse(fault.path("diagnostics").isEmpty());
assertFalse(value(call("studio_headless_fault", args())).path("fault").isNull());
assertEquals(identity, project.automation().snapshot().task().value());
Files.writeString(source, original); assertTrue(value(call("studio_language_query", query)).path("result").isEmpty());
build("corrected"); boot();
value(call("studio_headless_advance", args().put("clientRequestId", "corrected-run").put("frames", 3)));
assertEquals("SUCCEEDED", value(completed("corrected-run")).path("result").path("status").asText());
assertEquals(identity, project.automation().snapshot().task().value());
value(call("studio_control_release", args())); assertTrue(project.automation().snapshot().manualAllowed());
}
@Test void fullHttpLoopMatchesRawRuntimePixelsAndDisplayedDevAcrossResetAndRebuild() throws Exception {
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"));