feat(studio): ship JavaFX 2 Play and delegate integration tooling
Launch the JavaFX 2 project shell on Java 25 with an interactive viewport, managed Play lifecycle and project mutation guards. Preserve headless/MCP and save compatibility, and qualify exact runtime executable bundles. Separate unit JaCoCo gates from opt-in integration suites and use the internal Pong fixture for the headless visual pilot. Move maintenance and native conformance tools into tooling; retain the VS Code extension and update current navigation links. Validation: tooling make verify passed, including 1064 Studio unit tests, JaCoCo thresholds and 22 headless integration tests.
This commit is contained in:
+6
@@ -27,6 +27,12 @@ public final class DefaultExecutionControllerFactory implements ExecutionControl
|
||||
|
||||
@Override
|
||||
public ExecutionController create(ProjectReference project, ProjectLocalStudioSetup setup, ExecutionSession session) {
|
||||
return create(project,setup,session,p.studio.api.execution.PlayOptions.PERSISTENT);
|
||||
}
|
||||
|
||||
@Override public ExecutionController create(ProjectReference project,ProjectLocalStudioSetup setup,ExecutionSession session,p.studio.api.execution.PlayOptions options) {
|
||||
if(runtime.interactive())return new InteractiveExecutionController(project,setup,session,shipper,runtime,operations,access,options);
|
||||
if(options.disposableSave())throw new UnsupportedOperationException("Disposable Play requires interactive host");
|
||||
return new PlayStopExecutionController(project, setup, session, shipper, runtime, operations, access);
|
||||
}
|
||||
}
|
||||
|
||||
+17
-4
@@ -50,6 +50,11 @@ public final class DefaultExecutionSession implements ExecutionSession {
|
||||
@Override
|
||||
public void transitionToRunning() { publish(transitionLocked(ExecutionState.RUNNING, false)); }
|
||||
|
||||
@Override public void transitionToPausing() {publish(transitionLocked(ExecutionState.PAUSING,false));}
|
||||
@Override public void transitionToPaused() {publish(transitionLocked(ExecutionState.PAUSED,false));}
|
||||
@Override public void transitionToResuming() {publish(transitionLocked(ExecutionState.RESUMING,false));}
|
||||
@Override public void transitionToStopping() {publish(transitionLocked(ExecutionState.STOPPING,false));}
|
||||
|
||||
@Override
|
||||
public void transitionToRuntimeFailed() { publish(transitionLocked(ExecutionState.RUNTIME_FAILED, false)); }
|
||||
|
||||
@@ -92,7 +97,8 @@ public final class DefaultExecutionSession implements ExecutionSession {
|
||||
final ExecutionState current = snapshot().state();
|
||||
if (current == ExecutionState.PREPARING
|
||||
|| current == ExecutionState.CONNECTING
|
||||
|| current == ExecutionState.RUNNING) {
|
||||
|| current == ExecutionState.RUNNING || current == ExecutionState.PAUSING
|
||||
|| current == ExecutionState.PAUSED || current == ExecutionState.RESUMING || current == ExecutionState.STOPPING) {
|
||||
transitionToStopped();
|
||||
}
|
||||
listeners.clear();
|
||||
@@ -101,7 +107,9 @@ public final class DefaultExecutionSession implements ExecutionSession {
|
||||
private void appendLog(ExecutionLogSource source, ExecutionLogSeverity severity, String message, Instant timestamp) {
|
||||
final ExecutionSnapshot snapshot;
|
||||
synchronized (monitor) {
|
||||
logs.add(new ExecutionLogEntry(nextSequence++, timestamp, source, severity, message));
|
||||
if(logs.size()==2048)logs.remove(0);
|
||||
String bounded=message.length()>8192?message.substring(0,8192)+"…":message;
|
||||
logs.add(new ExecutionLogEntry(nextSequence++, timestamp, source, severity, bounded));
|
||||
snapshot = snapshotLocked();
|
||||
}
|
||||
publish(snapshot);
|
||||
@@ -131,10 +139,15 @@ public final class DefaultExecutionSession implements ExecutionSession {
|
||||
case IDLE -> next == ExecutionState.PREPARING;
|
||||
case PREPARING -> next == ExecutionState.PREPARE_FAILED
|
||||
|| next == ExecutionState.CONNECTING
|
||||
|| next == ExecutionState.STOPPING
|
||||
|| next == ExecutionState.STOPPED;
|
||||
case PREPARE_FAILED -> next == ExecutionState.IDLE || next == ExecutionState.PREPARING;
|
||||
case CONNECTING -> next == ExecutionState.RUNNING || next == ExecutionState.RUNTIME_FAILED || next == ExecutionState.STOPPED;
|
||||
case RUNNING -> next == ExecutionState.STOPPED || next == ExecutionState.RUNTIME_FAILED;
|
||||
case CONNECTING -> next == ExecutionState.RUNNING || next == ExecutionState.PAUSED || next == ExecutionState.STOPPING || next == ExecutionState.RUNTIME_FAILED || next == ExecutionState.STOPPED;
|
||||
case RUNNING -> next == ExecutionState.PAUSING || next == ExecutionState.STOPPING || next == ExecutionState.STOPPED || next == ExecutionState.RUNTIME_FAILED;
|
||||
case PAUSING -> next == ExecutionState.PAUSED || next == ExecutionState.RUNNING || next == ExecutionState.STOPPING || next == ExecutionState.STOPPED || next == ExecutionState.RUNTIME_FAILED;
|
||||
case PAUSED -> next == ExecutionState.RESUMING || next == ExecutionState.STOPPING || next == ExecutionState.STOPPED || next == ExecutionState.RUNTIME_FAILED;
|
||||
case RESUMING -> next == ExecutionState.RUNNING || next == ExecutionState.PAUSED || next == ExecutionState.STOPPING || next == ExecutionState.STOPPED || next == ExecutionState.RUNTIME_FAILED;
|
||||
case STOPPING -> next == ExecutionState.STOPPED || next == ExecutionState.PAUSED || next == ExecutionState.RUNTIME_FAILED;
|
||||
case RUNTIME_FAILED, STOPPED -> next == ExecutionState.IDLE || next == ExecutionState.PREPARING;
|
||||
};
|
||||
if (!allowed) throw new IllegalStateException("illegal execution transition: " + current + " -> " + next);
|
||||
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
package p.studio.core.execution;
|
||||
|
||||
import p.studio.api.execution.*;
|
||||
import p.studio.api.project.*;
|
||||
import p.studio.api.operation.*;
|
||||
import p.studio.api.automation.*;
|
||||
import p.studio.api.shipper.*;
|
||||
import p.studio.core.runtime.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/** Interactive session lifecycle. Process completion, rather than a click, releases authority. */
|
||||
final class InteractiveExecutionController implements ExecutionController {
|
||||
private final Object monitor=new Object();
|
||||
private final ProjectReference project;
|
||||
private final ProjectLocalStudioSetup setup;
|
||||
private final ExecutionSession session;
|
||||
private final Shipper shipper;
|
||||
private final RuntimeGateway runtime;
|
||||
private final OperationExecutor operations;
|
||||
private final ProjectAccess access;
|
||||
private final PlayOptions options;
|
||||
private Attempt attempt;
|
||||
private boolean closed;
|
||||
private static final class Attempt {
|
||||
final ManualLease lease;
|
||||
volatile RuntimeConnection connection;
|
||||
volatile OperationHandle<ExecutionSnapshot> play;
|
||||
volatile OperationHandle<ShipperPrepareResult> preparation;
|
||||
volatile boolean stopping,pauseOnReady;
|
||||
boolean released;
|
||||
CompletableFuture<Void> stop;
|
||||
Attempt(ManualLease lease){this.lease=lease;}
|
||||
}
|
||||
InteractiveExecutionController(ProjectReference project,ProjectLocalStudioSetup setup,ExecutionSession session,
|
||||
Shipper shipper,RuntimeGateway runtime,OperationExecutor operations,ProjectAccess access,PlayOptions options) {
|
||||
this.project=project;this.setup=setup;this.session=session;this.shipper=shipper;
|
||||
this.runtime=runtime;this.operations=operations;this.access=access;this.options=options;
|
||||
}
|
||||
@Override public OperationHandle<ExecutionSnapshot> play() {
|
||||
synchronized(monitor){
|
||||
if(closed)throw new IllegalStateException("Execution controller is closed");
|
||||
if(attempt!=null&&!attempt.released){
|
||||
if(attempt.play!=null)return attempt.play;
|
||||
throw new ControlDeniedException(ControlResult.Status.BUSY);
|
||||
}
|
||||
Attempt a=new Attempt(access.enterExecution(project.rootPath()));attempt=a;
|
||||
a.lease.onProjectClose(()->stopAttempt(a));session.beginPreparationCycle();
|
||||
try{a.play=operations.submit("Play "+project.name(),()->start(a));
|
||||
a.play.termination().whenComplete((v,f)->{synchronized(monitor){
|
||||
if(a.connection==null){if(a.stopping)stoppedWithoutProcess(a);release(a);}
|
||||
}});
|
||||
return a.play;}
|
||||
catch(RuntimeException failure){release(a);session.transitionToPrepareFailed();throw failure;}
|
||||
}
|
||||
}
|
||||
private ExecutionSnapshot start(Attempt a) {
|
||||
try {
|
||||
if(a.stopping)return session.snapshot();
|
||||
a.preparation=access.prepareExecution(project.rootPath(),()->shipper.prepare(project,session::appendShipperLog));
|
||||
if(a.stopping)a.preparation.cancel();
|
||||
ShipperPrepareResult prepared=a.preparation.completion().toCompletableFuture().join();
|
||||
a.preparation.termination().toCompletableFuture().join();
|
||||
if(a.stopping)return session.snapshot();
|
||||
if(!prepared.success()){session.transitionToPrepareFailed();return session.snapshot();}
|
||||
session.transitionToConnecting();
|
||||
RuntimeConnection connection=runtime.start(project,setup,
|
||||
log->session.appendRuntimeLog(log.severity(),log.message(),log.timestamp()),options);
|
||||
synchronized(monitor){a.connection=connection;}
|
||||
connection.completion().whenComplete((code,failure)->exited(a,connection,code,failure));
|
||||
if(a.stopping){connection.stopAsync().join();return session.snapshot();}
|
||||
boolean paused=a.pauseOnReady;
|
||||
if(paused)connection.pause().join();
|
||||
connection.startExecution().join();
|
||||
synchronized(monitor){
|
||||
if(attempt==a&&a.connection==connection&&!a.stopping){
|
||||
if(paused)session.transitionToPaused();else session.transitionToRunning();
|
||||
}
|
||||
}
|
||||
if(a.pauseOnReady&&!paused&&!a.stopping)pause();
|
||||
} catch(Throwable failure){
|
||||
Throwable cause=cause(failure);
|
||||
synchronized(monitor){
|
||||
if(attempt==a&&a.connection==null){
|
||||
if(a.stopping){/* Actual worker termination owns the final state and lease. */}
|
||||
else {log("Play failed: "+message(cause));
|
||||
if(session.snapshot().state()==ExecutionState.PREPARING)session.transitionToPrepareFailed();
|
||||
else session.transitionToRuntimeFailed();}
|
||||
} else if(attempt==a&&!a.stopping){log("Runtime startup failed: "+message(cause));session.transitionToRuntimeFailed();}
|
||||
}
|
||||
} finally {
|
||||
OperationHandle<ShipperPrepareResult> preparation=a.preparation;
|
||||
if(preparation!=null)try{preparation.termination().toCompletableFuture().join();}catch(CompletionException|CancellationException ignored){}
|
||||
a.preparation=null;
|
||||
}
|
||||
return session.snapshot();
|
||||
}
|
||||
@Override public OperationHandle<ExecutionSnapshot> pause(){return changePause(true);}
|
||||
@Override public OperationHandle<ExecutionSnapshot> resume(){return changePause(false);}
|
||||
private OperationHandle<ExecutionSnapshot> changePause(boolean pause) {
|
||||
synchronized(monitor){
|
||||
Attempt a=attempt;
|
||||
if(a==null||a.released||a.stopping)throw new IllegalStateException("No controllable runtime");
|
||||
if(pause&&(session.snapshot().state()==ExecutionState.PREPARING||session.snapshot().state()==ExecutionState.CONNECTING)){
|
||||
a.pauseOnReady=true;return operations.submit("Pause on ready",session::snapshot);
|
||||
}
|
||||
ExecutionState expected=pause?ExecutionState.RUNNING:ExecutionState.PAUSED;
|
||||
if(session.snapshot().state()!=expected)throw new IllegalStateException("Invalid pause/resume state");
|
||||
RuntimeConnection connection=a.connection;
|
||||
if(pause)session.transitionToPausing();else session.transitionToResuming();
|
||||
return operations.submit(pause?"Pause":"Resume",()->{
|
||||
try{(pause?connection.pause():connection.resume()).join();
|
||||
synchronized(monitor){if(attempt==a&&a.connection==connection&&!a.stopping){
|
||||
if(pause)session.transitionToPaused();else session.transitionToRunning();}}
|
||||
}catch(Throwable failure){synchronized(monitor){
|
||||
if(attempt==a&&a.connection==connection&&!a.stopping){
|
||||
log("Runtime control failed: "+message(cause(failure)));
|
||||
if(pause)session.transitionToRunning();else session.transitionToPaused();}}
|
||||
throw new CompletionException(cause(failure));}
|
||||
return session.snapshot();
|
||||
});
|
||||
}
|
||||
}
|
||||
@Override public void stop(){stopAsync();}
|
||||
@Override public CompletionStage<Void> stopAsync(){synchronized(monitor){return attempt==null?CompletableFuture.completedFuture(null):stopAttempt(attempt);}}
|
||||
private CompletableFuture<Void> stopAttempt(Attempt a) {
|
||||
synchronized(monitor){
|
||||
if(a.released)return CompletableFuture.completedFuture(null);
|
||||
if(a.stop!=null)return a.stop;
|
||||
a.stopping=true;a.stop=new CompletableFuture<>();CompletableFuture<Void> result=a.stop;
|
||||
if(a.preparation!=null)a.preparation.cancel();
|
||||
if(a.play!=null&&!a.play.termination().toCompletableFuture().isDone())a.play.cancel();
|
||||
if(attempt==a&&active(session.snapshot().state()))session.transitionToStopping();
|
||||
try {operations.submit("Stop runtime",()->{
|
||||
try {
|
||||
if(a.play!=null)try{a.play.termination().toCompletableFuture().join();}catch(CompletionException|CancellationException ignored){}
|
||||
RuntimeConnection connection=a.connection;
|
||||
if(connection!=null){connection.stopAsync().join();exited(a,connection,connection.completion().join(),null);}
|
||||
synchronized(monitor){if(a.connection==null){stoppedWithoutProcess(a);release(a);} }
|
||||
result.complete(null);
|
||||
}catch(Throwable failure){Throwable why=cause(failure);synchronized(monitor){
|
||||
if(attempt==a&&why instanceof RuntimeStopRefusedException){
|
||||
a.stopping=false;a.stop=null;session.transitionToPaused();
|
||||
}else if(attempt==a&&!a.released){session.transitionToRuntimeFailed();}
|
||||
log("Stop failed: "+message(why));}
|
||||
result.completeExceptionally(why);
|
||||
}return null;
|
||||
});}catch(RuntimeException failure){result.completeExceptionally(failure);}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
private void exited(Attempt a,RuntimeConnection connection,Integer code,Throwable failure){
|
||||
synchronized(monitor){
|
||||
if(a.connection!=connection)return;a.connection=null;
|
||||
if(attempt==a){
|
||||
if(failure==null&&code!=null&&code==0)session.transitionToStopped();
|
||||
else {log("Runtime ended: "+(failure==null?"exit "+code:message(cause(failure))));session.transitionToRuntimeFailed();}
|
||||
}
|
||||
if(a.play==null||a.play.termination().toCompletableFuture().isDone())release(a);
|
||||
else a.play.termination().whenComplete((v,f)->release(a));
|
||||
}
|
||||
}
|
||||
private void stoppedWithoutProcess(Attempt a){synchronized(monitor){
|
||||
if(attempt==a&&active(session.snapshot().state()))session.transitionToStopped();}}
|
||||
private void release(Attempt a){synchronized(monitor){if(!a.released){a.released=true;a.lease.close();}}}
|
||||
@Override public ExecutionFrame acquireLatestFrame(){synchronized(monitor){return attempt==null||attempt.connection==null?null:attempt.connection.acquireLatestFrame();}}
|
||||
@Override public boolean ownsExecution(){synchronized(monitor){return attempt!=null&&!attempt.released;}}
|
||||
@Override public void input(ExecutionInput input){synchronized(monitor){
|
||||
if(attempt!=null&&attempt.connection!=null&&session.snapshot().state()==ExecutionState.RUNNING)attempt.connection.input(input);}}
|
||||
@Override public void close(){synchronized(monitor){closed=true;}stop();}
|
||||
private void log(String text){session.appendRuntimeLog(ExecutionLogSeverity.ERROR,text);}
|
||||
private static boolean active(ExecutionState state){return switch(state){case PREPARING,CONNECTING,RUNNING,PAUSING,PAUSED,RESUMING,STOPPING->true;default->false;};}
|
||||
private static Throwable cause(Throwable failure){while((failure instanceof CompletionException||failure instanceof ExecutionException)&&failure.getCause()!=null)failure=failure.getCause();return failure;}
|
||||
private static String message(Throwable failure){return failure.getMessage()==null?failure.getClass().getSimpleName():failure.getMessage();}
|
||||
}
|
||||
+2
-1
@@ -100,7 +100,8 @@ final class PlayStopExecutionController implements ExecutionController {
|
||||
}
|
||||
|
||||
private ExecutionSnapshot runPlay() {
|
||||
final OperationHandle<ShipperPrepareResult> preparation = shipper.prepare(project, session::appendShipperLog);
|
||||
final OperationHandle<ShipperPrepareResult> preparation = access.prepareExecution(project.rootPath(),
|
||||
() -> shipper.prepare(project, session::appendShipperLog));
|
||||
synchronized (monitor) {
|
||||
preparationOperation = preparation;
|
||||
if (stopRequested) {
|
||||
|
||||
@@ -82,6 +82,7 @@ public final class ProjectAccessRegistry implements ProjectAccess {
|
||||
}
|
||||
if(automation.isEmpty())return enterManual(path);
|
||||
var session=automation.get();
|
||||
if(session instanceof p.studio.api.automation.ExecutionAuthority owner)return owner.enterExecution();
|
||||
synchronized(session) {
|
||||
if(session.snapshot().manualOperations()!=0||session.runtimeAttached())
|
||||
throw new p.studio.api.automation.ControlDeniedException(p.studio.api.automation.ControlResult.Status.BUSY);
|
||||
@@ -89,6 +90,28 @@ public final class ProjectAccessRegistry implements ProjectAccess {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public <T> T prepareExecution(Path path, java.util.function.Supplier<T> work) {
|
||||
java.util.Optional<p.studio.api.automation.AutomationSession> automation;
|
||||
try {automation=findAutomation(path);}
|
||||
catch(AutomationException failure){
|
||||
if(failure.failure().code()!=AutomationFailure.Code.PROJECT_NOT_OPEN)throw failure;
|
||||
return work.get();
|
||||
}
|
||||
if(automation.orElse(null) instanceof p.studio.api.automation.ExecutionAuthority owner)
|
||||
return owner.prepareExecution(work);
|
||||
return work.get();
|
||||
}
|
||||
@Override public <T> p.studio.api.operation.OperationHandle<T> submitManual(Path path,
|
||||
p.studio.api.operation.OperationExecutor executor,String summary,java.util.concurrent.Callable<T> work) {
|
||||
p.studio.api.automation.ExecutionAuthority owner=null;
|
||||
try {if(findAutomation(path).orElse(null) instanceof p.studio.api.automation.ExecutionAuthority found)owner=found;}
|
||||
catch(AutomationException failure){if(failure.failure().code()!=AutomationFailure.Code.PROJECT_NOT_OPEN)throw failure;}
|
||||
var preparationOwner=owner;
|
||||
java.util.concurrent.Callable<T> admitted = owner!=null && owner.preparingExecution()
|
||||
? () -> preparationOwner.callExecutionPreparation(work) : work;
|
||||
return ProjectAccess.super.submitManual(path,executor,summary,admitted);
|
||||
}
|
||||
|
||||
private static Path canonical(Path path) {
|
||||
Path normalized = path.toAbsolutePath().normalize();
|
||||
try { return normalized.toRealPath(); }
|
||||
|
||||
@@ -5,6 +5,16 @@ import java.util.concurrent.CompletableFuture;
|
||||
public interface RuntimeConnection extends AutoCloseable {
|
||||
CompletableFuture<Integer> completion();
|
||||
|
||||
default CompletableFuture<Void> startExecution() { return CompletableFuture.completedFuture(null); }
|
||||
default CompletableFuture<Void> pause() { return CompletableFuture.failedFuture(new UnsupportedOperationException("Pause unavailable")); }
|
||||
default CompletableFuture<Void> resume() { return CompletableFuture.failedFuture(new UnsupportedOperationException("Resume unavailable")); }
|
||||
default CompletableFuture<Void> stopAsync() {
|
||||
try {close();return CompletableFuture.completedFuture(null);}
|
||||
catch (RuntimeException failure) {return CompletableFuture.failedFuture(failure);}
|
||||
}
|
||||
default p.studio.api.execution.ExecutionFrame acquireLatestFrame() {return null;}
|
||||
default void input(p.studio.api.execution.ExecutionInput input) { }
|
||||
|
||||
@Override
|
||||
void close();
|
||||
}
|
||||
|
||||
@@ -9,4 +9,11 @@ import java.util.function.Consumer;
|
||||
public interface RuntimeGateway {
|
||||
RuntimeConnection start(ProjectReference project, ProjectLocalStudioSetup setup,
|
||||
Consumer<RuntimeLogEntry> logs) throws Exception;
|
||||
|
||||
default boolean interactive() {return false;}
|
||||
default RuntimeConnection start(ProjectReference project, ProjectLocalStudioSetup setup,
|
||||
Consumer<RuntimeLogEntry> logs,p.studio.api.execution.PlayOptions options) throws Exception {
|
||||
if(options.disposableSave())throw new UnsupportedOperationException("Disposable Play unavailable");
|
||||
return start(project,setup,logs);
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package p.studio.core.runtime;
|
||||
|
||||
/** Normal Stop was refused to preserve pending/unsaved game data. The connection stays alive. */
|
||||
public final class RuntimeStopRefusedException extends RuntimeException {
|
||||
public RuntimeStopRefusedException(String message) {super(message);}
|
||||
}
|
||||
+36
-1
@@ -8,7 +8,7 @@ import java.util.concurrent.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/** Project authority is independent of any UI, transport connection or operation result. */
|
||||
public final class DefaultAutomationSession implements AutomationSession {
|
||||
public final class DefaultAutomationSession implements AutomationSession,ExecutionAuthority {
|
||||
private final Path root;
|
||||
private final OperationExecutor executor;
|
||||
private final AutomationConnectionLifecycle connections;
|
||||
@@ -20,6 +20,8 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
private ControlState state = ControlState.MANUAL;
|
||||
private TaskIdentity task;
|
||||
private int manualOperations;
|
||||
private boolean executionOwned;
|
||||
private final ThreadLocal<Boolean> executionPreparation = ThreadLocal.withInitial(() -> false);
|
||||
private boolean handoffPending, closed, busy, stale;
|
||||
private long generation;
|
||||
private OperationHandle<AutomationOutcome> active;
|
||||
@@ -52,8 +54,12 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
}
|
||||
|
||||
public synchronized ManualLease enterManual() {
|
||||
return enterManual(false);
|
||||
}
|
||||
private ManualLease enterManual(boolean executionAdmission) {
|
||||
if (closed) throw new ControlDeniedException(ControlResult.Status.CLOSED);
|
||||
if (state != ControlState.MANUAL) throw new ControlDeniedException(ControlResult.Status.BUSY);
|
||||
if (executionOwned && !executionAdmission && !executionPreparation.get()) throw new ControlDeniedException(ControlResult.Status.BUSY);
|
||||
if (manualOperations++ == 0) manualIdle = new CompletableFuture<>();
|
||||
stale = capture != null;
|
||||
publish();
|
||||
@@ -82,6 +88,35 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
};
|
||||
}
|
||||
|
||||
/** One exclusive interactive owner; its preparation may temporarily enter the manual lane. */
|
||||
public synchronized ManualLease enterExecution() {
|
||||
if (executionOwned || manualOperations!=0 || runtime!=null)
|
||||
throw new ControlDeniedException(ControlResult.Status.BUSY);
|
||||
executionOwned=true;
|
||||
final ManualLease lease;
|
||||
try {lease=enterManual(true);}catch(RuntimeException failure){executionOwned=false;throw failure;}
|
||||
return new ManualLease() {
|
||||
private boolean released;
|
||||
@Override public void onProjectClose(Runnable action){lease.onProjectClose(action);}
|
||||
@Override public void close(){synchronized(DefaultAutomationSession.this){
|
||||
if(!released){released=true;executionOwned=false;lease.close();}
|
||||
}}
|
||||
};
|
||||
}
|
||||
public boolean preparingExecution(){return executionPreparation.get();}
|
||||
public <T> T prepareExecution(java.util.function.Supplier<T> work) {
|
||||
boolean prior=executionPreparation.get();
|
||||
synchronized(this){if(!executionOwned || closed)throw new ControlDeniedException(ControlResult.Status.BUSY);}
|
||||
executionPreparation.set(true);
|
||||
try{return work.get();}finally{if(prior)executionPreparation.set(true);else executionPreparation.remove();}
|
||||
}
|
||||
public <T> T callExecutionPreparation(java.util.concurrent.Callable<T> work) throws Exception {
|
||||
boolean prior=executionPreparation.get();
|
||||
synchronized(this){if(!executionOwned || closed)throw new ControlDeniedException(ControlResult.Status.BUSY);}
|
||||
executionPreparation.set(true);
|
||||
try{return work.call();}finally{if(prior)executionPreparation.set(true);else executionPreparation.remove();}
|
||||
}
|
||||
|
||||
@Override public synchronized AutomationSnapshot snapshot() {
|
||||
return new AutomationSnapshot(root, state, handoffPending, closed, manualOperations, task,
|
||||
active == null ? null : active.snapshot(), capture, stale, diagnostics, List.copyOf(calls), diagnosticResult,
|
||||
|
||||
+11
-3
@@ -56,20 +56,28 @@ public final class DefaultProjectSessionFactory implements ProjectSessionFactory
|
||||
|
||||
@Override
|
||||
public synchronized ProjectSession open(ProjectReference projectReference) {
|
||||
return open(projectReference,true);
|
||||
}
|
||||
@Override public synchronized ProjectSession openForPlay(ProjectReference projectReference){return open(projectReference,false);}
|
||||
|
||||
private ProjectSession open(ProjectReference projectReference,boolean editorInfrastructure) {
|
||||
if (closed) throw new IllegalStateException("Project sessions are closing");
|
||||
final ProjectReference project = Objects.requireNonNull(projectReference, "projectReference");
|
||||
final var setup = setupStore.load(project);
|
||||
// Preparation reports readiness independently; opening never grants agent authority.
|
||||
if (setup.agentIntegration().enabled()) preparation.prepare(project.rootPath());
|
||||
if (editorInfrastructure && setup.agentIntegration().enabled()) preparation.prepare(project.rootPath());
|
||||
LspLifecyclePort lifecycle=editorInfrastructure?lsp:new LspLifecyclePort(){
|
||||
public void boot(ProjectReference ignored){}public void shutdown(ProjectReference ignored){}
|
||||
};
|
||||
final var state = stateStore.load(project);
|
||||
var automation = new DefaultAutomationSession(project.rootPath(), operations, connections);
|
||||
var ready = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
var registration = access.register(project.rootPath(), automation::enterManual,
|
||||
() -> ready.get() && !automation.snapshot().closed(), automation);
|
||||
try { lsp.boot(project); }
|
||||
try { lifecycle.boot(project); }
|
||||
catch (RuntimeException failure) { registration.unsubscribe(); throw failure; }
|
||||
ready.set(true);
|
||||
var session = new DefaultProjectSession(project, stateStore, setup, new DefaultExecutionSession(), lsp, state, automation, () -> {
|
||||
var session = new DefaultProjectSession(project, stateStore, setup, new DefaultExecutionSession(), lifecycle, state, automation, () -> {
|
||||
registration.unsubscribe();
|
||||
synchronized (DefaultProjectSessionFactory.this) { sessions.removeIf(s -> s.automation() == automation); }
|
||||
});
|
||||
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
package p.studio.core.execution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.execution.*;
|
||||
import p.studio.api.project.*;
|
||||
import p.studio.api.operation.*;
|
||||
import p.studio.api.shipper.*;
|
||||
import p.studio.core.runtime.*;
|
||||
import p.studio.core.project.ProjectAccessRegistry;
|
||||
import p.studio.core.session.DefaultAutomationSession;
|
||||
import p.studio.core.support.ControlledOperations;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class InteractiveExecutionControllerTest {
|
||||
@TempDir Path root;
|
||||
static final class Connection implements RuntimeConnection {
|
||||
final CompletableFuture<Integer> exit=new CompletableFuture<>();
|
||||
final List<String> commands=new CopyOnWriteArrayList<>();boolean refuse;
|
||||
public CompletableFuture<Integer> completion(){return exit;}
|
||||
public CompletableFuture<Void> startExecution(){commands.add("start");return CompletableFuture.completedFuture(null);}
|
||||
public CompletableFuture<Void> pause(){commands.add("pause");return CompletableFuture.completedFuture(null);}
|
||||
public CompletableFuture<Void> resume(){commands.add("resume");return CompletableFuture.completedFuture(null);}
|
||||
public CompletableFuture<Void> stopAsync(){
|
||||
if(refuse)return CompletableFuture.failedFuture(new RuntimeStopRefusedException("Save first"));
|
||||
exit.complete(0);return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
public void close(){stopAsync().join();}
|
||||
}
|
||||
private static <T> OperationHandle<T> done(T value){
|
||||
var operations=new ControlledOperations();var h=operations.submit("done",()->value);operations.last.finish();return h;
|
||||
}
|
||||
private Shipper shipper(){return (p,l)->done(new ShipperPrepareResult(ShipperPreparationStatus.SUCCESS,List.of(),root.resolve("manifest.json"),List.of(),List.of()));}
|
||||
private RuntimeGateway gateway(Connection connection){return (p,s,l)->connection;}
|
||||
private ProjectReference project(){return new ProjectReference("Game","1","pbs",1,ProjectTarget.Game,root);}
|
||||
|
||||
@Test void failedConnectionReleasesAuthorityAfterCompletionAndAllowsAnotherAttempt(){
|
||||
var ops=new ControlledOperations();var authority=new DefaultAutomationSession(root,ops);
|
||||
var access=new ProjectAccessRegistry();access.register(root,authority::enterManual,()->true,authority);
|
||||
var state=new DefaultExecutionSession();var first=new Connection();var second=new Connection();
|
||||
var calls=new java.util.concurrent.atomic.AtomicInteger();RuntimeGateway runtime=(p,s,l)->calls.getAndIncrement()==0?first:second;
|
||||
var controller=new InteractiveExecutionController(project(),ProjectLocalStudioSetup.defaults(),state,shipper(),runtime,ops,access,PlayOptions.PERSISTENT);
|
||||
controller.play();ops.last.finish();assertTrue(controller.ownsExecution());
|
||||
first.exit.completeExceptionally(new java.io.IOException("video disconnected"));
|
||||
assertEquals(ExecutionState.RUNTIME_FAILED,state.snapshot().state());assertFalse(controller.ownsExecution());
|
||||
try(var lease=access.enterManual(root)){}
|
||||
assertTrue(state.snapshot().logs().stream().anyMatch(log->log.message().contains("video disconnected")));
|
||||
controller.play();ops.last.finish();assertEquals(ExecutionState.RUNNING,state.snapshot().state());
|
||||
var stopped=controller.stopAsync();ops.last.finish();stopped.toCompletableFuture().join();assertFalse(controller.ownsExecution());
|
||||
}
|
||||
|
||||
@Test void pauseAndRefusedStopRetainOwnershipUntilProcessActuallyEnds(){
|
||||
var ops=new ControlledOperations();var authority=new DefaultAutomationSession(root,ops);
|
||||
var access=new ProjectAccessRegistry();access.register(root,authority::enterManual,()->true,authority);
|
||||
var state=new DefaultExecutionSession();var connection=new Connection();
|
||||
var controller=new InteractiveExecutionController(project(),ProjectLocalStudioSetup.defaults(),state,shipper(),gateway(connection),ops,access,PlayOptions.PERSISTENT);
|
||||
controller.play();ops.last.finish();assertEquals(ExecutionState.RUNNING,state.snapshot().state());
|
||||
controller.pause();ops.last.finish();assertEquals(ExecutionState.PAUSED,state.snapshot().state());
|
||||
assertThrows(p.studio.api.automation.ControlDeniedException.class,()->access.enterManual(root));
|
||||
controller.resume();ops.last.finish();assertEquals(ExecutionState.RUNNING,state.snapshot().state());
|
||||
connection.refuse=true;var refused=controller.stopAsync();ops.last.finish();
|
||||
assertThrows(CompletionException.class,()->refused.toCompletableFuture().join());
|
||||
assertEquals(ExecutionState.PAUSED,state.snapshot().state());assertFalse(connection.exit.isDone());
|
||||
assertThrows(p.studio.api.automation.ControlDeniedException.class,()->access.enterManual(root));
|
||||
connection.refuse=false;var stop=controller.stopAsync();ops.last.finish();stop.toCompletableFuture().join();
|
||||
assertEquals(ExecutionState.STOPPED,state.snapshot().state());
|
||||
try(var edit=access.enterManual(root)){assertEquals(1,authority.snapshot().manualOperations());}
|
||||
}
|
||||
@Test void focusLostDuringStartupPausesBeforeArmingTheGame()throws Exception {
|
||||
var ops=new ControlledOperations();var state=new DefaultExecutionSession();var connection=new Connection();
|
||||
var entered=new CountDownLatch(1);var proceed=new CountDownLatch(1);
|
||||
RuntimeGateway gateway=(p,s,l)->{entered.countDown();assertTrue(proceed.await(2,TimeUnit.SECONDS));return connection;};
|
||||
var controller=new InteractiveExecutionController(project(),ProjectLocalStudioSetup.defaults(),state,shipper(),gateway,ops,new ProjectAccessRegistry(),PlayOptions.PERSISTENT);
|
||||
controller.play();var play=ops.last;var running=CompletableFuture.runAsync(play::finish);
|
||||
assertTrue(entered.await(2,TimeUnit.SECONDS));controller.pause();var pause=ops.last;
|
||||
proceed.countDown();running.get(2,TimeUnit.SECONDS);pause.finish();
|
||||
assertEquals(List.of("pause","start"),connection.commands);assertEquals(ExecutionState.PAUSED,state.snapshot().state());
|
||||
var stopped=controller.stopAsync();ops.last.finish();stopped.toCompletableFuture().join();
|
||||
}
|
||||
@Test void cancelledPreparationKeepsOwnershipUntilWriterActuallyTerminates()throws Exception {
|
||||
var ops=new ControlledOperations();var prepOps=new ControlledOperations();var authority=new DefaultAutomationSession(root,ops);
|
||||
var access=new ProjectAccessRegistry();access.register(root,authority::enterManual,()->true,authority);
|
||||
var entered=new CountDownLatch(1);var state=new DefaultExecutionSession();
|
||||
Shipper preparation=(p,l)->{
|
||||
var h=access.submitManual(root,prepOps,"prepare",()->new ShipperPrepareResult(
|
||||
ShipperPreparationStatus.SUCCESS,List.of(),root.resolve("manifest.json"),List.of(),List.of()));
|
||||
entered.countDown();return h;
|
||||
};
|
||||
RuntimeGateway runtime=(p,s,l)->{throw new AssertionError("Cancelled preparation must not start runtime");};
|
||||
var controller=new InteractiveExecutionController(project(),ProjectLocalStudioSetup.defaults(),state,preparation,runtime,ops,access,PlayOptions.PERSISTENT);
|
||||
controller.play();var play=ops.last;var running=CompletableFuture.runAsync(play::finish);
|
||||
assertTrue(entered.await(2,TimeUnit.SECONDS));var stop=controller.stopAsync();var stopper=ops.last;
|
||||
prepOps.last.completed.completeExceptionally(new CancellationException());
|
||||
assertThrows(p.studio.api.automation.ControlDeniedException.class,()->access.enterManual(root));
|
||||
assertFalse(running.isDone());assertEquals(ExecutionState.STOPPING,state.snapshot().state());
|
||||
prepOps.last.finish();running.get(2,TimeUnit.SECONDS);stopper.finish();stop.toCompletableFuture().join();
|
||||
assertEquals(ExecutionState.STOPPED,state.snapshot().state());
|
||||
try(var editing=access.enterManual(root)){assertEquals(1,authority.snapshot().manualOperations());}
|
||||
}
|
||||
}
|
||||
+17
@@ -17,6 +17,23 @@ class DefaultAutomationSessionTest {
|
||||
DefaultAutomationSession session() { return new DefaultAutomationSession(root, operations); }
|
||||
ControlResult await(CompletionStage<ControlResult> result) { return result.toCompletableFuture().join(); }
|
||||
TaskIdentity start(DefaultAutomationSession s) { assertEquals(GRANTED, await(s.grant(false)).status()); return s.acquire().task(); }
|
||||
@Test void interactiveOwnerBlocksOtherWritersButAdmitsItsPreparationAcrossThreads() throws Exception {
|
||||
var s=session();var access=new ProjectAccessRegistry();access.register(root,s::enterManual,()->true,s);
|
||||
try(var execution=access.enterExecution(root)){
|
||||
assertThrows(ControlDeniedException.class,s::enterManual);
|
||||
assertThrows(ControlDeniedException.class,()->access.enterManual(root));
|
||||
assertEquals(BUSY,await(s.grant(false)).status());
|
||||
var prepared=access.prepareExecution(root,()->access.submitManual(root,operations,"prepare",()->{
|
||||
try(var nested=access.enterManual(root)){return "prepared";}
|
||||
}));
|
||||
assertThrows(ControlDeniedException.class,()->access.enterManual(root));
|
||||
CompletableFuture.runAsync(operations.last::finish).get(2,TimeUnit.SECONDS);
|
||||
assertEquals("prepared",prepared.completion().toCompletableFuture().join());
|
||||
assertEquals(1,s.snapshot().manualOperations());
|
||||
}
|
||||
try(var edit=access.enterManual(root)){assertEquals(1,s.snapshot().manualOperations());}
|
||||
assertEquals(0,s.snapshot().manualOperations());
|
||||
}
|
||||
@Test void manualTestRuntimeUsesExistingLeaseAndPreventsGrantUntilExplicitCleanup() {
|
||||
var s=session();var runtime=new ManagedHeadlessSession(){public String sessionId(){return "manual-test";}public void cancel(){}public CompletionStage<Void> shutdown(){return CompletableFuture.completedFuture(null);}};
|
||||
assertThrows(ControlDeniedException.class,()->s.attachRuntime(null,runtime));
|
||||
|
||||
Reference in New Issue
Block a user