implement real headless process gateway and supervision
This commit is contained in:
@@ -198,7 +198,7 @@ class McpTaskToolsTest {
|
||||
assertEquals("CANCELLED", result.path("result").path("status").asText());
|
||||
assertEquals(2, result.path("progress").path("completedFrames").asInt());
|
||||
assertFalse(result.path("capture").path("stale").asBoolean());
|
||||
assertEquals(2, session.snapshot().capture().logicalFrameIndex());
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package p.studio.app.runtime;
|
||||
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import java.io.*;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
|
||||
/** Deliberately adversarial child process. Never packaged into production. */
|
||||
public final class HeadlessFixture {
|
||||
static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
static String mode;
|
||||
static void emit(JsonNode value) throws Exception {
|
||||
byte[] bytes = (value.toString() + "\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
for (int at = 0; at < bytes.length; at += 7) { System.out.write(bytes, at, Math.min(7, bytes.length - at)); System.out.flush(); }
|
||||
}
|
||||
static JsonNode state() { return MAPPER.createObjectNode().put("pc", 0).put("logicalFrameIndex", new BigInteger("18446744073709551615"))
|
||||
.put("tickIndex", new BigInteger("18446744073709551615")).put("appId", 1).put("status", "paused").set("stackTop", MAPPER.createArrayNode()); }
|
||||
static void response(String id, JsonNode result) throws Exception {
|
||||
emit(MAPPER.createObjectNode().put("type", "response").put("sessionId", mode.equals("wrong-session") ? "alien" : "fixture").put("requestId", id).set("result", result));
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
mode = args[0];
|
||||
if (mode.equals("child") || mode.equals("no-hello")) { Thread.sleep(60000); return; }
|
||||
if (mode.equals("malformed")) { System.out.println("not json"); Thread.sleep(60000); return; }
|
||||
if (mode.equals("oversized")) { System.out.print("x".repeat(5000)); System.out.flush(); Thread.sleep(60000); return; }
|
||||
if (mode.equals("utf8")) { System.out.write(new byte[]{(byte)0xff, 10}); System.out.flush(); Thread.sleep(60000); return; }
|
||||
var hello = MAPPER.createObjectNode().put("type", "hello").put("sessionId", "fixture").put("runtimeVersion", "fixture-only");
|
||||
hello.putObject("protocolVersion").put("major", mode.equals("major") ? 2 : 1).put("minor", 3);
|
||||
hello.set("capabilities", MAPPER.valueToTree(mode.equals("capabilities") ? Set.of("boot") : HeadlessWire.CAPABILITIES));
|
||||
hello.set("limits", MAPPER.valueToTree(new HeadlessLimits(600, 4096, 4096, 1000, 500, 150)));
|
||||
if (mode.equals("limits")) ((com.fasterxml.jackson.databind.node.ObjectNode) hello.path("limits")).put("maxFrames", 900);
|
||||
emit(hello);
|
||||
if (mode.equals("descendant")) {
|
||||
var child = new ProcessBuilder(java(), "-cp", System.getProperty("java.class.path"), HeadlessFixture.class.getName(), "child").start();
|
||||
Files.writeString(Path.of("descendant.pid"), Long.toString(child.pid()));
|
||||
}
|
||||
System.err.println("fixture stderr is not JSONL");
|
||||
String active = null; long frames = 0;
|
||||
var reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
for (String line; (line = reader.readLine()) != null;) {
|
||||
var request = MAPPER.readTree(line); String id = request.path("requestId").asText(), command = request.path("command").asText();
|
||||
switch (command) {
|
||||
case "initialize" -> response(id, MAPPER.createObjectNode().setAll(Map.of("protocolVersion", hello.path("protocolVersion"), "capabilities", hello.path("capabilities"))));
|
||||
case "boot", "reset" -> response(id, state());
|
||||
case "get_state" -> { if (!mode.equals("hang-call")) response(id, state()); }
|
||||
case "get_fault" -> response(id, MAPPER.nullNode());
|
||||
case "advance" -> {
|
||||
active = id; frames = request.path("params").path("frames").longValue();
|
||||
emit(MAPPER.createObjectNode().put("type", "progress").put("sessionId", "fixture").put("requestId", id)
|
||||
.put("requested", frames).put("completed", mode.equals("bad-progress") ? frames + 1 : 2));
|
||||
if (mode.equals("crash")) System.exit(23);
|
||||
if (mode.equals("hang-cancel") || mode.equals("cooperative") || mode.equals("descendant")) continue;
|
||||
response(id, MAPPER.createObjectNode().put("requested", frames).put("completed", frames).put("stop", "completed"));
|
||||
}
|
||||
case "cancel" -> {
|
||||
if (mode.equals("hang-cancel") || mode.equals("descendant")) continue;
|
||||
response(id, MAPPER.createObjectNode().put("requested", true).put("targetRequestId", active));
|
||||
response(active, MAPPER.createObjectNode().put("requested", frames).put("completed", 2).put("stop", "canceled"));
|
||||
}
|
||||
case "capture" -> {
|
||||
var image = new java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_ARGB);
|
||||
image.setRGB(0, 0, 0xff123456); var bytes = new ByteArrayOutputStream(); javax.imageio.ImageIO.write(image, "png", bytes);
|
||||
response(id, MAPPER.createObjectNode().put("mimeType", "image/png").put("encoding", "base64").put("pixelFormat", "RGBA8888")
|
||||
.put("width", 1).put("height", 1).put("stridePixels", 3).put("logicalFrameIndex", new BigInteger("18446744073709551615"))
|
||||
.put("frameId", new BigInteger("9223372036854775808")).put("data", mode.equals("bad-png") ? "broken" : Base64.getEncoder().encodeToString(bytes.toByteArray())));
|
||||
}
|
||||
case "shutdown" -> { if (mode.equals("descendant")) continue; response(id, MAPPER.createObjectNode().put("closed", true)); return; }
|
||||
default -> response(id, MAPPER.createObjectNode());
|
||||
}
|
||||
}
|
||||
}
|
||||
static String java() { return Path.of(System.getProperty("java.home"), "bin", "java").toString(); }
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package p.studio.app.runtime;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.automation.*;
|
||||
import p.studio.api.project.*;
|
||||
import p.studio.app.compiler.CompilerBuildGatewayAdapter;
|
||||
import p.studio.app.operation.ExecutorOperationExecutor;
|
||||
import p.studio.core.project.*;
|
||||
import p.studio.core.shipper.StudioShipperService;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Explicit packaged-process gate, not satisfied by protocol fixtures. */
|
||||
class HeadlessInstalledRuntimeTest {
|
||||
@TempDir Path root;
|
||||
@Test void configuredDistributionBootsSavedBuildAndReturnsDeterministicCanonicalPixels() throws Exception {
|
||||
String install = System.getProperty("studio.test.runtimeInstall");
|
||||
assertNotNull(install, "Supply -PstudioRuntimeInstall=<extracted runtime installation>");
|
||||
assertTrue(Files.isRegularFile(Path.of(install, "prometeu-headless")));
|
||||
var mapper = new ObjectMapper();
|
||||
for (var doc : p.studio.compiler.FrontendRegistryService.require("pbs").documentation().orElseThrow().references(1, p.studio.AppMode.Game))
|
||||
if (doc.id().startsWith("pbs:example:game:1:")) {
|
||||
Path file = root.resolve(doc.id().substring("pbs:example:game:1:".length()));
|
||||
Files.createDirectories(file.getParent()); Files.writeString(file, doc.text());
|
||||
}
|
||||
var setup = new ProjectLocalStudioSetupStore(mapper);
|
||||
var project = new ProjectCatalogService(root, mapper, setup).openProject(root);
|
||||
setup.save(project, new ProjectLocalStudioSetup(install, null));
|
||||
try (var workers = Executors.newSingleThreadExecutor(); var packer = p.packer.Packer.bootstrap(mapper, ignored -> {})) {
|
||||
var build = new StudioShipperService(new CompilerBuildGatewayAdapter(), packer.workspaceService(), mapper, new ExecutorOperationExecutor(workers))
|
||||
.prepare(project, ignored -> {}).completion().toCompletableFuture().get(15, TimeUnit.SECONDS);
|
||||
assertTrue(build.success(), build.diagnostics().toString());
|
||||
var gateway = new HeadlessProcessGateway(mapper, setup::load);
|
||||
var session = (HeadlessProcessSession) gateway.boot(project, "real-build", build);
|
||||
try {
|
||||
assertEquals(600, session.maxFramesPerAdvance());
|
||||
assertEquals(AutomationFailure.Code.NO_FRAME_AVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> session.call(TaskTools.HeadlessAction.CAPTURE, Map.of())).failure().code());
|
||||
var observed = new ArrayList<TrackedOperation.Progress>();
|
||||
var outcome = session.advance(3, observed::add);
|
||||
assertEquals(AutomationOutcome.Kind.SUCCEEDED, outcome.kind()); assertEquals(3, outcome.completedFrames());
|
||||
assertNotNull(outcome.capture()); assertEquals("real-build", outcome.capture().buildId());
|
||||
assertEquals(session.sessionId(), outcome.capture().sessionId());
|
||||
var image = javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(outcome.capture().png()));
|
||||
assertEquals(outcome.capture().width(), image.getWidth()); assertEquals(outcome.capture().height(), image.getHeight());
|
||||
assertEquals(3L, observed.getLast().completedFrames());
|
||||
var state = session.call(TaskTools.HeadlessAction.STATE, Map.of());
|
||||
assertEquals("0.1.0", state.values().get("runtimeVersion"));
|
||||
assertNull(session.call(TaskTools.HeadlessAction.FAULT, Map.of()).values().get("fault"));
|
||||
session.call(TaskTools.HeadlessAction.SET_INPUT, Map.of("a", true));
|
||||
session.call(TaskTools.HeadlessAction.CLEAR_INPUT, Map.of());
|
||||
session.call(TaskTools.HeadlessAction.RESET, Map.of());
|
||||
assertEquals(AutomationFailure.Code.NO_FRAME_AVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> session.call(TaskTools.HeadlessAction.CAPTURE, Map.of())).failure().code());
|
||||
session.call(TaskTools.HeadlessAction.BOOT, Map.of());
|
||||
var repeated = session.advance(3, ignored -> {});
|
||||
assertArrayEquals(outcome.capture().png(), repeated.capture().png());
|
||||
assertEquals(0, session.advance(0, ignored -> {}).completedFrames());
|
||||
assertEquals(AutomationFailure.Code.LIMIT_EXCEEDED, assertThrows(AutomationException.class,
|
||||
() -> session.advance(601, ignored -> {})).failure().code());
|
||||
} finally { session.shutdown().toCompletableFuture().get(7, TimeUnit.SECONDS); }
|
||||
assertFalse(session.alive());
|
||||
var cancellable = (HeadlessProcessSession) new HeadlessProcessGateway(mapper, setup::load,
|
||||
new HeadlessLimits(20000, 65536, 33554432, 5000, 60000, 5000)).boot(project, "cancel-build", build);
|
||||
try {
|
||||
assertEquals(20000, cancellable.maxFramesPerAdvance());
|
||||
var cancelled = cancellable.advance(20000, progress -> { if (progress.completedFrames() == 1) cancellable.cancel(); });
|
||||
assertEquals(AutomationOutcome.Kind.CANCELLED, cancelled.kind());
|
||||
assertTrue(cancelled.completedFrames() >= 1 && cancelled.completedFrames() < 20000);
|
||||
assertNotNull(cancelled.capture());
|
||||
} finally { cancellable.shutdown().toCompletableFuture().get(7, TimeUnit.SECONDS); }
|
||||
assertFalse(cancellable.alive());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package p.studio.app.runtime;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.automation.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class HeadlessProcessSessionTest {
|
||||
@TempDir Path root;
|
||||
static final HeadlessLimits LIMITS = new HeadlessLimits(600, 4096, 4096, 1000, 500, 150);
|
||||
final List<HeadlessProcessSession> sessions = new ArrayList<>();
|
||||
HeadlessProcessSession start(String mode) throws Exception {
|
||||
var process = new ProcessBuilder(HeadlessFixture.java(), "-cp", System.getProperty("java.class.path"), HeadlessFixture.class.getName(), mode).directory(root.toFile()).start();
|
||||
var session = new HeadlessProcessSession(process, new ObjectMapper(), LIMITS, "build"); sessions.add(session); return session;
|
||||
}
|
||||
@AfterEach void cleanup() { sessions.forEach(session -> session.abort("test cleanup")); }
|
||||
@Test void fragmentedProtocolPreservesUnsignedFrameIdentityLayoutAndSeparateStderr() throws Exception {
|
||||
var session = start("normal"); session.initializeAndBoot(root);
|
||||
var outcome = session.advance(3, ignored -> {});
|
||||
assertEquals(AutomationOutcome.Kind.SUCCEEDED, outcome.kind());
|
||||
assertEquals("18446744073709551615", outcome.capture().logicalFrameIndex().toString());
|
||||
assertEquals("9223372036854775808", outcome.capture().frameId().toString());
|
||||
assertEquals(3, outcome.capture().stridePixels());
|
||||
assertTrue(outcome.diagnostics().stream().anyMatch(s -> s.contains("fixture stderr")));
|
||||
var wire = p.studio.app.mcp.McpToolResult.capture(new ObjectMapper(), outcome.capture(), false).wire();
|
||||
assertEquals(outcome.capture().frameId(), wire.path("structuredContent").path("value").path("frameId").bigIntegerValue());
|
||||
session.shutdown().toCompletableFuture().get(3, TimeUnit.SECONDS); assertFalse(session.alive());
|
||||
}
|
||||
@Test void rejectsBadHandshakeMalformedOversizedAndNonUtf8OutputAndCleansUp() throws Exception {
|
||||
for (var mode : List.of("major", "capabilities", "limits", "malformed", "oversized", "utf8", "wrong-session", "no-hello")) {
|
||||
var session = start(mode);
|
||||
assertThrows(Exception.class, () -> session.initializeAndBoot(root), mode);
|
||||
session.abort("startup rejection"); assertFalse(session.alive(), mode);
|
||||
}
|
||||
}
|
||||
@Test void crashRetainsOnlyObservedProgressAndNeverRestartsOrReplays() throws Exception {
|
||||
var session = start("crash"); session.initializeAndBoot(root);
|
||||
var known = new AtomicReference<TrackedOperation.Progress>();
|
||||
assertThrows(AutomationException.class, () -> session.advance(30, known::set));
|
||||
assertEquals(2L, known.get().completedFrames()); assertFalse(session.alive());
|
||||
assertThrows(AutomationException.class, () -> session.call(TaskTools.HeadlessAction.STATE, Map.of()));
|
||||
}
|
||||
@Test void deadlinesStopHungCallsAndCancellationPreservesKnownPartialProgress() throws Exception {
|
||||
var call = start("hang-call"); call.initializeAndBoot(root);
|
||||
assertThrows(AutomationException.class, () -> call.call(TaskTools.HeadlessAction.STATE, Map.of())); assertFalse(call.alive());
|
||||
var session = start("hang-cancel"); session.initializeAndBoot(root);
|
||||
var known = new AtomicReference<TrackedOperation.Progress>();
|
||||
assertThrows(AutomationException.class, () -> session.advance(30, known::set));
|
||||
assertEquals(2L, known.get().completedFrames()); assertFalse(session.alive());
|
||||
}
|
||||
@Test void cooperativeCancelReturnsFinalImageAndRejectsConcurrentWork() throws Exception {
|
||||
var session = start("cooperative"); session.initializeAndBoot(root);
|
||||
var entered = new CountDownLatch(1);
|
||||
try (var workers = Executors.newSingleThreadExecutor()) {
|
||||
var result = workers.submit(() -> session.advance(30, ignored -> entered.countDown()));
|
||||
assertTrue(entered.await(2, TimeUnit.SECONDS));
|
||||
assertEquals(AutomationFailure.Code.BUSY, assertThrows(AutomationException.class,
|
||||
() -> session.call(TaskTools.HeadlessAction.STATE, Map.of())).failure().code());
|
||||
session.cancel();
|
||||
var outcome = result.get(2, TimeUnit.SECONDS);
|
||||
assertEquals(AutomationOutcome.Kind.CANCELLED, outcome.kind()); assertEquals(2, outcome.completedFrames()); assertNotNull(outcome.capture());
|
||||
}
|
||||
session.shutdown().toCompletableFuture().get(3, TimeUnit.SECONDS); assertFalse(session.alive());
|
||||
}
|
||||
@Test void malformedProgressAndCaptureInvalidateSession() throws Exception {
|
||||
for (var mode : List.of("bad-progress", "bad-png")) {
|
||||
var session = start(mode); session.initializeAndBoot(root);
|
||||
assertThrows(AutomationException.class, () -> session.advance(3, ignored -> {})); assertFalse(session.alive());
|
||||
}
|
||||
}
|
||||
@Test void shutdownEscalatesAndKillsOwnedDescendantsWithoutTouchingUnrelatedProcesses() throws Exception {
|
||||
var unrelated = new ProcessBuilder(HeadlessFixture.java(), "-cp", System.getProperty("java.class.path"), HeadlessFixture.class.getName(), "child").start();
|
||||
try {
|
||||
var session = start("descendant"); session.initializeAndBoot(root);
|
||||
long child = Long.parseLong(Files.readString(root.resolve("descendant.pid")));
|
||||
assertTrue(ProcessHandle.of(child).orElseThrow().isAlive());
|
||||
assertThrows(ExecutionException.class, () -> session.shutdown().toCompletableFuture().get(4, TimeUnit.SECONDS));
|
||||
assertFalse(session.alive()); assertFalse(ProcessHandle.of(child).map(ProcessHandle::isAlive).orElse(false));
|
||||
assertTrue(unrelated.isAlive());
|
||||
} finally { unrelated.destroyForcibly(); unrelated.waitFor(2, TimeUnit.SECONDS); }
|
||||
}
|
||||
@Test void missingConfiguredInstallationIsExplicitAndNeverDiscoversAnotherRuntime() throws Exception {
|
||||
var project = new p.studio.api.project.ProjectReference("game", "1", "pbs", 1, p.studio.api.project.ProjectTarget.Game, root);
|
||||
var build = p.studio.api.shipper.ShipperPrepareResult.failed(List.of(), List.of());
|
||||
var missing = new HeadlessProcessGateway(new ObjectMapper(), ignored -> p.studio.api.project.ProjectLocalStudioSetup.defaults());
|
||||
assertEquals(AutomationFailure.Code.RUNTIME_UNAVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> missing.boot(project, "build", build)).failure().code());
|
||||
var unavailable = new HeadlessProcessGateway(new ObjectMapper(), ignored -> new p.studio.api.project.ProjectLocalStudioSetup(root.toString(), null));
|
||||
assertEquals(AutomationFailure.Code.RUNTIME_UNAVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> unavailable.boot(project, "build", build)).failure().code());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user