feat(studio): improve compiler reliability and AI diagnostics
Preserve structured diagnostics, source attribution, phase outcomes and revision context through compiler, Studio, LSP, MCP and JavaFX. Add project service discovery and resilient VS Code reconnection. Correct all eight reproduced PBS production failures in expression context, sibling executable bindings and nominal owner identity. Consolidate specs, lessons, immutable baseline evidence and the maintained source-to-guest qualification runner. Validation: required Gradle gate recorded 724 tests, with 711 passed and 13 skipped. Full corpus passed 168 compilation observations and 108 guest runs (104 successes and four deliberate faults); supplemental controls passed 42 compilations and 16 guest runs. TypeScript compilation, two Node discovery tests and discussion validation passed.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package p.studio.core.language;
|
||||
|
||||
import p.studio.api.automation.*;
|
||||
import p.studio.utilities.json.ImmutableJson;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/** Per-admitted-call budget/resources. Session and operation handle retain authority and admission. */
|
||||
public final class LanguageQueryExecution {
|
||||
private final String queryId = UUID.randomUUID().toString();
|
||||
private final long executionMillis, cleanupMillis, deadline;
|
||||
private final String operation;
|
||||
private final List<Runnable> cancellations = new CopyOnWriteArrayList<>();
|
||||
private final AtomicReference<AutomationFailure> primary = new AtomicReference<>();
|
||||
private volatile String cleanup = "not-started", method;
|
||||
private volatile boolean cancelled, cleanupFailed;
|
||||
private volatile Map<String, Object> context = Map.of();
|
||||
public void correlate(Map<String, Object> additions) {
|
||||
var merged = new LinkedHashMap<>(context); merged.putAll(additions); context = ImmutableJson.object(merged);
|
||||
}
|
||||
|
||||
public LanguageQueryExecution(String operation, long executionMillis, long cleanupMillis) {
|
||||
this.operation = operation; this.method = operation; this.executionMillis = executionMillis; this.cleanupMillis = cleanupMillis;
|
||||
this.deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(executionMillis);
|
||||
}
|
||||
public String queryId() { return queryId; }
|
||||
public long deadlineNanos() { return deadline; }
|
||||
public long responseDeadlineNanos() { return deadline + TimeUnit.MILLISECONDS.toNanos(cleanupMillis); }
|
||||
public void method(String method) { this.method = method; }
|
||||
public void own(Runnable cancellation) {
|
||||
cancellations.add(cancellation);
|
||||
if (cancelled) cancellation.run();
|
||||
}
|
||||
public void cancel() {
|
||||
cancelled = true;
|
||||
for (var action : cancellations) try { action.run(); } catch (RuntimeException failure) {
|
||||
cleanupFailed = true; cleanup = "failed";
|
||||
record(AutomationFailure.Code.LANGUAGE_QUERY_CLEANUP_FAILED, "Language query resource cleanup failed", Map.of());
|
||||
}
|
||||
}
|
||||
public void record(AutomationFailure failure) { primary.compareAndSet(null, failure); }
|
||||
public boolean hasFailure() { return primary.get() != null; }
|
||||
public void record(AutomationFailure.Code code, String message, Map<String, Object> details) {
|
||||
record(new AutomationFailure(code, message, Map.of(), details));
|
||||
}
|
||||
|
||||
/** Keep the managed worker alive until its owned executor really stops, even after the caller's response budget. */
|
||||
public void finishWorkers(ExecutorService workers) {
|
||||
cleanup = "running"; workers.shutdownNow();
|
||||
boolean interrupted = Thread.interrupted();
|
||||
try {
|
||||
while (!workers.isTerminated()) {
|
||||
try { workers.awaitTermination(50, TimeUnit.MILLISECONDS); }
|
||||
catch (InterruptedException stop) { interrupted = true; }
|
||||
if (!workers.isTerminated() && System.nanoTime() >= responseDeadlineNanos()) cleanup = "pending";
|
||||
}
|
||||
cleanup = "terminated";
|
||||
} finally { if (interrupted) Thread.currentThread().interrupt(); }
|
||||
}
|
||||
|
||||
public AutomationException exception(AutomationFailure.Code fallback, String message, boolean workerTerminated) {
|
||||
var failure = primary.get();
|
||||
if (failure == null) { record(fallback, message, Map.of()); failure = primary.get(); }
|
||||
var details = new LinkedHashMap<>(failure.details()); details.putAll(context);
|
||||
details.putAll(ImmutableJson.fields("component", "lsp", "operation", operation, "method", method,
|
||||
"queryId", queryId, "executionBudgetMs", executionMillis, "cleanupBudgetMs", cleanupMillis,
|
||||
"workerTerminated", workerTerminated, "cleanup", cleanupFailed ? "failed" : workerTerminated ? "terminated" : cleanup));
|
||||
return new AutomationException(new AutomationFailure(failure.code(), failure.message(), failure.runtimeError(), details));
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,9 @@ import java.util.Map;
|
||||
/** Application adapter to the existing language server; never a replacement compiler. */
|
||||
public interface LanguageQueryPort {
|
||||
Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> parameters) throws Exception;
|
||||
default Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> parameters,
|
||||
LanguageQueryExecution execution) throws Exception {
|
||||
return query(task, operation, parameters);
|
||||
}
|
||||
void endTask(TaskIdentity task);
|
||||
}
|
||||
|
||||
+51
-13
@@ -12,9 +12,14 @@ public final class TaskLanguageQueries implements LanguageQueries {
|
||||
private final ProjectAccessRegistry projects;
|
||||
private final LanguageQueryPort language;
|
||||
private final Set<TaskIdentity> registered = ConcurrentHashMap.newKeySet();
|
||||
private final long executionMillis, cleanupMillis;
|
||||
|
||||
public TaskLanguageQueries(ProjectAccessRegistry projects, LanguageQueryPort language) {
|
||||
this(projects, language, 10_000, 2_000);
|
||||
}
|
||||
public TaskLanguageQueries(ProjectAccessRegistry projects, LanguageQueryPort language, long executionMillis, long cleanupMillis) {
|
||||
this.projects = Objects.requireNonNull(projects); this.language = Objects.requireNonNull(language);
|
||||
this.executionMillis = executionMillis; this.cleanupMillis = cleanupMillis;
|
||||
}
|
||||
|
||||
@Override public Map<String, Object> query(Path root, String taskId, String operation, Map<String, Object> parameters) {
|
||||
@@ -25,30 +30,63 @@ public final class TaskLanguageQueries implements LanguageQueries {
|
||||
catch (RuntimeException failure) { registered.remove(task); throw failure; }
|
||||
}
|
||||
var result = new AtomicReference<Map<String, Object>>();
|
||||
var execution = new LanguageQueryExecution(operation, executionMillis, cleanupMillis);
|
||||
execution.correlate(p.studio.utilities.json.ImmutableJson.fields("projectRoot", task.projectRoot().toString(), "taskId", task.value()));
|
||||
var handle = session.submit(task, "LSP " + operation, () -> {
|
||||
result.set(language.query(task, operation, parameters));
|
||||
return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, List.of(), null);
|
||||
var reply = language.query(task, operation, parameters, execution);
|
||||
if (execution.hasFailure()) throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_INTERNAL_FAILURE, "Language query failed", false);
|
||||
result.set(reply);
|
||||
Map<String, Object> view = Map.of();
|
||||
if (reply.get("analysis") instanceof Map<?, ?> analysis && analysis.get("summary") != null) {
|
||||
view = p.studio.utilities.json.ImmutableJson.fields("issues", analysis.getOrDefault("issues", null),
|
||||
"summary", analysis.get("summary"), "context", reply.getOrDefault("context", Map.of()));
|
||||
}
|
||||
return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, List.of(), null, view);
|
||||
});
|
||||
execution.correlate(Map.of("operationId", handle.id().value()));
|
||||
try {
|
||||
// A failed result must also wait for worker termination and admission
|
||||
// cleanup before the caller can issue its next query.
|
||||
handle.completion().handle((outcome, failure) -> null)
|
||||
.thenCompose(ignored -> handle.termination()).toCompletableFuture().get(12, TimeUnit.SECONDS);
|
||||
handle.completion().toCompletableFuture().get();
|
||||
handle.completion().toCompletableFuture().get(Math.max(1, execution.deadlineNanos() - System.nanoTime()), TimeUnit.NANOSECONDS);
|
||||
handle.termination().toCompletableFuture().get(Math.max(1, execution.responseDeadlineNanos() - System.nanoTime()), TimeUnit.NANOSECONDS);
|
||||
requireTask(session, taskId);
|
||||
return Objects.requireNonNull(result.get());
|
||||
var reply = new LinkedHashMap<>(Objects.requireNonNull(result.get()));
|
||||
if (reply.get("context") instanceof Map<?, ?> context) {
|
||||
var correlated = new LinkedHashMap<String, Object>(); context.forEach((key, value) -> correlated.put((String) key, value));
|
||||
correlated.put("operationId", handle.id().value()); reply.put("context", correlated);
|
||||
}
|
||||
return p.studio.utilities.json.ImmutableJson.object(reply);
|
||||
} catch (ExecutionException failure) {
|
||||
if (failure.getCause() instanceof RuntimeException cause) throw cause;
|
||||
throw new AutomationException(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "LSP query failed");
|
||||
awaitCleanup(handle, execution);
|
||||
if (failure.getCause() instanceof AutomationException domain) {
|
||||
execution.record(domain.failure());
|
||||
throw execution.exception(domain.failure().code(), domain.getMessage(), handle.termination().toCompletableFuture().isDone());
|
||||
}
|
||||
if (failure.getCause() instanceof ControlDeniedException denied) throw denied;
|
||||
if (failure.getCause() instanceof IllegalArgumentException parametersFailure) throw parametersFailure;
|
||||
throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_INTERNAL_FAILURE, "Language query failed",
|
||||
handle.termination().toCompletableFuture().isDone());
|
||||
} catch (InterruptedException failure) {
|
||||
handle.cancel(); Thread.currentThread().interrupt();
|
||||
throw new AutomationException(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "LSP query interrupted");
|
||||
} catch (TimeoutException | CancellationException failure) {
|
||||
handle.cancel();
|
||||
throw new AutomationException(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "LSP query cancelled or timed out");
|
||||
execution.record(AutomationFailure.Code.LANGUAGE_QUERY_CANCELLED, "Language query interrupted", Map.of());
|
||||
execution.cancel(); handle.cancel(); Thread.currentThread().interrupt();
|
||||
throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_CANCELLED, "Language query interrupted", handle.termination().toCompletableFuture().isDone());
|
||||
} catch (TimeoutException failure) {
|
||||
execution.record(AutomationFailure.Code.LANGUAGE_QUERY_TIMEOUT, "Language query timed out", Map.of());
|
||||
execution.cancel(); handle.cancel(); awaitCleanup(handle, execution);
|
||||
throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_TIMEOUT, "Language query timed out", handle.termination().toCompletableFuture().isDone());
|
||||
} catch (CancellationException failure) {
|
||||
execution.record(AutomationFailure.Code.LANGUAGE_QUERY_CANCELLED, "Language query cancelled", Map.of());
|
||||
execution.cancel(); handle.cancel(); awaitCleanup(handle, execution);
|
||||
throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_CANCELLED, "Language query cancelled", handle.termination().toCompletableFuture().isDone());
|
||||
}
|
||||
}
|
||||
|
||||
private static void awaitCleanup(p.studio.api.operation.OperationHandle<?> handle, LanguageQueryExecution execution) {
|
||||
try { handle.termination().toCompletableFuture().get(Math.max(1, execution.responseDeadlineNanos() - System.nanoTime()), TimeUnit.NANOSECONDS); }
|
||||
catch (InterruptedException stopped) { Thread.currentThread().interrupt(); }
|
||||
catch (ExecutionException | TimeoutException ignored) { /* The response reports actual termination separately. */ }
|
||||
}
|
||||
|
||||
private static TaskIdentity requireTask(AutomationSession session, String id) {
|
||||
var snapshot = session.snapshot();
|
||||
if (snapshot.closed() || snapshot.state() != ControlState.AGENT_WORKING || snapshot.task() == null || !snapshot.task().value().equals(id))
|
||||
|
||||
+26
-4
@@ -26,6 +26,7 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
private ManagedHeadlessSession runtime;
|
||||
private CanonicalCapture capture;
|
||||
private List<String> diagnostics = List.of();
|
||||
private Map<String, Object> diagnosticResult = Map.of();
|
||||
private CompletableFuture<ControlResult> returning;
|
||||
private final List<Runnable> manualCancellation = new ArrayList<>();
|
||||
private final List<Runnable> taskCleanup = new ArrayList<>();
|
||||
@@ -79,7 +80,7 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
|
||||
@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));
|
||||
active == null ? null : active.snapshot(), capture, stale, diagnostics, List.copyOf(calls), diagnosticResult);
|
||||
}
|
||||
|
||||
@Override public OperationSubscription subscribe(Consumer<AutomationSnapshot> listener) {
|
||||
@@ -166,6 +167,7 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
if (closed) return ControlResult.of(ControlResult.Status.CLOSED, "Project closed");
|
||||
if (state != ControlState.WAITING_FOR_AGENT || handoffPending) return ControlResult.of(ControlResult.Status.NOT_GRANTED, "Human grant required");
|
||||
task = new TaskIdentity(root, UUID.randomUUID().toString());
|
||||
diagnosticResult = Map.of(); diagnostics = List.of();
|
||||
state = ControlState.AGENT_WORKING; record("acquire"); publish();
|
||||
return new ControlResult(ControlResult.Status.ACQUIRED, task, "");
|
||||
}
|
||||
@@ -212,7 +214,7 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
operation = active; ownedRuntime = runtime; idle = manualIdle; publisherIdle = publicationIdle;
|
||||
cancellations = closing ? List.copyOf(manualCancellation) : List.of();
|
||||
participants = closing ? List.of() : List.copyOf(documents);
|
||||
cleanups = List.copyOf(taskCleanup); taskCleanup.clear();
|
||||
cleanups = List.copyOf(taskCleanup);
|
||||
publication = connection; connection = null;
|
||||
if (publication != null) publication.invalidate();
|
||||
publish();
|
||||
@@ -239,8 +241,9 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
cleanup.whenComplete((v, failure) -> {
|
||||
synchronized (this) {
|
||||
if (failure == null) {
|
||||
taskCleanup.removeAll(cleanups);
|
||||
runtime = null; busy = false; state = ControlState.MANUAL;
|
||||
diagnostics = List.of(); publish();
|
||||
diagnostics = List.of(); diagnosticResult = Map.of(); publish();
|
||||
result.complete(ControlResult.of(ControlResult.Status.RETURNED, ""));
|
||||
} else {
|
||||
diagnostics = List.of(message(failure)); publish();
|
||||
@@ -256,8 +259,11 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
requireTask(identity);
|
||||
if (busy) throw new ControlDeniedException(ControlResult.Status.BUSY);
|
||||
busy = true; record(summary);
|
||||
diagnosticResult = Map.of();
|
||||
var finalResult = new java.util.concurrent.atomic.AtomicReference<AutomationOutcome>();
|
||||
try { active = executor.submit(summary, () -> {
|
||||
var result = work.call();
|
||||
finalResult.set(result);
|
||||
// Cancellation acknowledgement can precede a cooperative final result. Publish
|
||||
// its known frame before worker termination, but never after task revocation.
|
||||
synchronized (this) {
|
||||
@@ -287,7 +293,23 @@ public final class DefaultAutomationSession implements AutomationSession {
|
||||
});
|
||||
var settled = operation.termination().whenComplete((v, f) -> {
|
||||
subscription.unsubscribe();
|
||||
synchronized (this) { if (active == operation) { busy = false; publish(); } }
|
||||
synchronized (this) { if (active == operation) {
|
||||
busy = false;
|
||||
var result = finalResult.get();
|
||||
if (valid(identity) && result != null && !result.diagnosticResult().isEmpty()) {
|
||||
var view = new LinkedHashMap<>(result.diagnosticResult());
|
||||
if (operation.snapshot().state() == OperationState.CANCELLED) {
|
||||
var resultSummary = new LinkedHashMap<>((Map<String, Object>)view.getOrDefault("summary", Map.of()));
|
||||
resultSummary.put("validationStatus", "cancelled"); view.put("summary", resultSummary);
|
||||
}
|
||||
var context = new LinkedHashMap<>((Map<String, Object>) view.getOrDefault("context", Map.of()));
|
||||
context.put("operationId", operation.id().value());
|
||||
if (operation.snapshot().state() == OperationState.CANCELLED && context.containsKey("buildId")) context.put("buildId", null);
|
||||
view.put("context", context);
|
||||
diagnosticResult = p.studio.utilities.json.ImmutableJson.object(view);
|
||||
}
|
||||
publish();
|
||||
} }
|
||||
});
|
||||
// Waiting on the raw worker future does not wait for its dependent
|
||||
// callbacks. Expose the stage that also releases session admission.
|
||||
|
||||
@@ -38,12 +38,16 @@ public final class DefaultTaskTools implements TaskTools {
|
||||
// Artifacts are mutable in place. Old IDs must never boot newly overwritten files.
|
||||
data.builds.clear();
|
||||
var result = shipper.prepareNow(catalog.openProject(data.task.projectRoot()), ignored -> {});
|
||||
record.build = result;
|
||||
if (Thread.currentThread().isInterrupted() || record.handle != null && record.handle.snapshot().state() == OperationState.CANCELLED) result = result.asCancelled();
|
||||
if (result.success() && !Thread.currentThread().isInterrupted()) {
|
||||
record.buildId = UUID.randomUUID().toString();
|
||||
data.builds.put(record.buildId, result);
|
||||
}
|
||||
return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, result.diagnostics(), null);
|
||||
record.build = result.withContext(p.studio.utilities.json.ImmutableJson.fields("projectRoot", data.task.projectRoot().toString(),
|
||||
"taskId", data.task.value(), "clientRequestId", record.request, "buildId", record.buildId));
|
||||
if (record.buildId != null) data.builds.put(record.buildId, record.build);
|
||||
return new AutomationOutcome(result.status() == p.studio.api.shipper.ShipperPreparationStatus.CANCELLED
|
||||
? AutomationOutcome.Kind.CANCELLED : AutomationOutcome.Kind.SUCCEEDED, 0, 0, result.diagnostics(), null, record.build.diagnosticView());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -215,13 +219,18 @@ public final class DefaultTaskTools implements TaskTools {
|
||||
var snapshot = record.handle.snapshot();
|
||||
if (!done && (snapshot.state() == OperationState.SUCCEEDED || snapshot.state() == OperationState.FAILED))
|
||||
snapshot = new OperationSnapshot(snapshot.id(), OperationState.RUNNING, snapshot.updatedAt(), snapshot.summary());
|
||||
if (done && snapshot.state() == OperationState.CANCELLED && record.build != null) {
|
||||
if (record.buildId != null) data.builds.remove(record.buildId);
|
||||
record.buildId = null;
|
||||
record.build = record.build.asCancelled().withContext(p.studio.utilities.json.ImmutableJson.fields("buildId", null));
|
||||
}
|
||||
var current = data.session.snapshot();
|
||||
var captured = record.runtime == null ? null : record.runtime.capture();
|
||||
boolean stale = captured != null && (current.stale() || current.capture() == null
|
||||
|| !captured.sessionId().equals(current.capture().sessionId()) || !captured.buildId().equals(current.capture().buildId())
|
||||
|| !captured.logicalFrameIndex().equals(current.capture().logicalFrameIndex()) || !captured.frameId().equals(current.capture().frameId()));
|
||||
return new TrackedOperation(snapshot, record.request, record.progress,
|
||||
done ? record.buildId : null, done ? record.build : null, done ? record.runtime : null, done ? record.failure : null,
|
||||
done ? record.buildId : null, done && record.build != null ? record.build.withContext(Map.of("operationId", record.handle.id().value())) : null, done ? record.runtime : null, done ? record.failure : null,
|
||||
done, stale);
|
||||
}
|
||||
private static AutomationException failure(AutomationFailure.Code code, String message) { return new AutomationException(code, message); }
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package p.studio.core.shipper;
|
||||
|
||||
import p.studio.api.diagnostic.StudioDiagnostic;
|
||||
import p.studio.utilities.json.ImmutableJson;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Application-adapted compiler rejection; Core never imports compiler exception classes. */
|
||||
public final class CompilerBuildFailure extends RuntimeException {
|
||||
private final List<StudioDiagnostic> issues;
|
||||
private final Map<String, Object> context;
|
||||
private final boolean payloadAvailable;
|
||||
public CompilerBuildFailure(String message, List<StudioDiagnostic> issues, Map<String, Object> context, boolean payloadAvailable) {
|
||||
super(message); this.issues = List.copyOf(issues); this.context = ImmutableJson.object(context); this.payloadAvailable = payloadAvailable;
|
||||
}
|
||||
public List<StudioDiagnostic> issues() { return issues; }
|
||||
public Map<String, Object> context() { return context; }
|
||||
public boolean payloadAvailable() { return payloadAvailable; }
|
||||
}
|
||||
@@ -4,15 +4,22 @@ import p.studio.api.project.ProjectReference;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import p.studio.api.diagnostic.StudioDiagnostic;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface CompilerBuildGateway {
|
||||
CompilerBuildOutput compile(ProjectReference project, Consumer<String> logSink);
|
||||
|
||||
record CompilerBuildOutput(Path bytecodeArtifact, List<String> requiredCapabilities) {
|
||||
record CompilerBuildOutput(Path bytecodeArtifact, List<String> requiredCapabilities,
|
||||
List<StudioDiagnostic> issues, Map<String, Object> context) {
|
||||
public CompilerBuildOutput(Path bytecodeArtifact, List<String> requiredCapabilities) {
|
||||
this(bytecodeArtifact, requiredCapabilities, List.of(), Map.of());
|
||||
}
|
||||
public CompilerBuildOutput {
|
||||
bytecodeArtifact = bytecodeArtifact == null ? null : bytecodeArtifact.toAbsolutePath().normalize();
|
||||
requiredCapabilities = List.copyOf(requiredCapabilities);
|
||||
issues = List.copyOf(issues); context = p.studio.utilities.json.ImmutableJson.object(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+111
-32
@@ -55,51 +55,130 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
|
||||
}
|
||||
|
||||
@Override public ShipperPrepareResult prepareNow(ProjectReference project, Consumer<ShipperLogEntry> sink) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(sink, "sink");
|
||||
final List<ShipperLogEntry> logs = new ArrayList<>();
|
||||
final List<String> diagnostics = new ArrayList<>();
|
||||
Objects.requireNonNull(project); Objects.requireNonNull(sink);
|
||||
var logs = new ArrayList<ShipperLogEntry>();
|
||||
var issues = new ArrayList<p.studio.api.diagnostic.StudioDiagnostic>();
|
||||
var stages = new java.util.LinkedHashMap<String, String>();
|
||||
for (var stage : List.of("compiler-build", "pack-validation", "pack", "manifest")) stages.put(stage, "not-run");
|
||||
java.util.Map<String, Object> inputs = java.util.Map.of();
|
||||
String current = "compiler-build";
|
||||
try {
|
||||
checkCancelled();
|
||||
final CompilerBuildGateway.CompilerBuildOutput build = compiler.compile(project,
|
||||
checkCancelled(); stages.put(current, "running");
|
||||
var build = compiler.compile(project,
|
||||
message -> emit(logs, sink, ShipperLogEntry.info(ShipperLogSource.BUILD, normalize(message))));
|
||||
checkCancelled();
|
||||
final var cartridge = p.studio.core.project.ProjectCartridgeConfiguration.adopt(project, mapper);
|
||||
if ((cartridge.memcardSlots() != null) != build.requiredCapabilities().contains("memcard")) {
|
||||
inputs = build.context().isEmpty() ? java.util.Map.of("projectRoot", project.rootPath().toString(), "compilerDiagnosticCoverage", "legacy") : build.context();
|
||||
issues.addAll(build.issues());
|
||||
var compilerEvaluation = p.studio.utilities.json.DiagnosticPayloads.evaluate(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList());
|
||||
if (!"passed".equals(compilerEvaluation)) {
|
||||
stages.put(current, compilerEvaluation);
|
||||
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, compilerEvaluation);
|
||||
}
|
||||
stages.put(current, "passed");
|
||||
checkCancelled(); current = "pack-validation"; stages.put(current, "running");
|
||||
var cartridge = p.studio.core.project.ProjectCartridgeConfiguration.adopt(project, mapper);
|
||||
if ((cartridge.memcardSlots() != null) != build.requiredCapabilities().contains("memcard"))
|
||||
throw new IllegalArgumentException("memcard_slots and compiled memcard capability must be declared together");
|
||||
}
|
||||
if (build.bytecodeArtifact() != null) {
|
||||
emit(logs, sink, ShipperLogEntry.info(ShipperLogSource.BUILD,
|
||||
"Prepared program bytecode at " + build.bytecodeArtifact()));
|
||||
}
|
||||
final PackerProjectContext context = new PackerProjectContext(project.name(), project.rootPath());
|
||||
final var validation = packer.validatePackWorkspace(new ValidatePackWorkspaceRequest(context));
|
||||
if (build.bytecodeArtifact() != null) emit(logs, sink, ShipperLogEntry.info(ShipperLogSource.BUILD,
|
||||
"Prepared program bytecode at " + build.bytecodeArtifact()));
|
||||
var context = new PackerProjectContext(project.name(), project.rootPath());
|
||||
var validation = packer.validatePackWorkspace(new ValidatePackWorkspaceRequest(context));
|
||||
emit(logs, sink, logFor(validation.status(), ShipperLogSource.PACK_VALIDATION, validation.summary()));
|
||||
validation.assets().forEach(asset -> asset.diagnostics().forEach(value -> diagnostics.add(value.message())));
|
||||
if (!validation.canPack()) {
|
||||
return ShipperPrepareResult.failed(logs, diagnostics);
|
||||
int validationIssueStart = issues.size();
|
||||
for (var asset : validation.assets()) for (var diagnostic : asset.diagnostics()) {
|
||||
issues.add(packerIssue("PACKER_" + diagnostic.category().name(), diagnostic.severity().name(), diagnostic.message(),
|
||||
p.studio.utilities.json.ImmutableJson.fields("category", diagnostic.category().name(), "blocking", diagnostic.blocking(),
|
||||
"assetId", asset.assetId(), "assetName", asset.assetName(), "assetPath", asset.assetPath().toString(),
|
||||
"evidencePath", diagnostic.evidencePath() == null ? null : diagnostic.evidencePath().toString(),
|
||||
"bankAvailability", "not-recorded"), project, asset.assetPath()));
|
||||
}
|
||||
checkCancelled();
|
||||
final var packed = packer.packWorkspace(new PackWorkspaceRequest(context));
|
||||
checkCancelled();
|
||||
emit(logs, sink, logFor(packed.status(), ShipperLogSource.PACK, packed.summary()));
|
||||
if (validation.status() != PackerOperationStatus.SUCCESS || !validation.canPack()) {
|
||||
if (issues.size() == validationIssueStart) issues.add(packerIssue("PACKER_BUILD", "ERROR", validation.summary(),
|
||||
java.util.Map.of("blocking", true, "category", "BUILD"), project, null));
|
||||
stages.put(current, "rejected");
|
||||
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, "rejected");
|
||||
}
|
||||
stages.put(current, "passed"); checkCancelled(); current = "pack"; stages.put(current, "running");
|
||||
var packed = packer.packWorkspace(new PackWorkspaceRequest(context));
|
||||
checkCancelled(); emit(logs, sink, logFor(packed.status(), ShipperLogSource.PACK, packed.summary()));
|
||||
if (packed.status() != PackerOperationStatus.SUCCESS) {
|
||||
diagnostics.add(packed.summary());
|
||||
return ShipperPrepareResult.failed(logs, diagnostics);
|
||||
issues.add(packerIssue("PACKER_BUILD", "ERROR", packed.summary(), java.util.Map.of("category", "BUILD", "blocking", true), project, null));
|
||||
stages.put(current, "rejected");
|
||||
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, "rejected");
|
||||
}
|
||||
final List<Path> artifacts = packed.result().emittedArtifacts().stream()
|
||||
.map(value -> value.path().toAbsolutePath().normalize()).toList();
|
||||
stages.put(current, "passed");
|
||||
var artifacts = packed.result().emittedArtifacts().stream().map(value -> value.path().toAbsolutePath().normalize()).toList();
|
||||
artifacts.forEach(path -> emit(logs, sink, ShipperLogEntry.info(ShipperLogSource.PACK, "Emitted " + path)));
|
||||
final Path manifest = writeManifest(project, build, logs, sink);
|
||||
return new ShipperPrepareResult(ShipperPreparationStatus.SUCCESS, logs, manifest, artifacts, diagnostics);
|
||||
} catch (IOException | RuntimeException exception) {
|
||||
final String message = exception.getMessage() == null ? exception.getClass().getSimpleName() : exception.getMessage();
|
||||
diagnostics.add(message);
|
||||
current = "manifest"; stages.put(current, "running"); checkCancelled();
|
||||
var manifest = writeManifest(project, build, logs, sink); checkCancelled(); stages.put(current, "passed");
|
||||
return finish(ShipperPreparationStatus.SUCCESS, logs, manifest, artifacts, issues, stages, inputs, "passed");
|
||||
} catch (java.util.concurrent.CancellationException cancelled) {
|
||||
stages.put(current, "cancelled");
|
||||
return finish(ShipperPreparationStatus.CANCELLED, logs, null, List.of(), issues, stages, inputs, "cancelled");
|
||||
} catch (CompilerBuildFailure failure) {
|
||||
issues.addAll(failure.issues()); inputs = failure.context();
|
||||
if (Thread.currentThread().isInterrupted()) {
|
||||
stages.put(current, "cancelled");
|
||||
return finish(ShipperPreparationStatus.CANCELLED, logs, null, List.of(), issues, stages, inputs, "cancelled");
|
||||
}
|
||||
var evaluation = failure.payloadAvailable() ? p.studio.utilities.json.DiagnosticPayloads.evaluate(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList()) : "failed";
|
||||
if ("passed".equals(evaluation) || "failed".equals(inputs.get("validationStatus"))) evaluation = "failed";
|
||||
if (issues.isEmpty()) {
|
||||
var unknown = new java.util.LinkedHashMap<>(p.studio.utilities.json.DiagnosticPayloads.issue("STUDIO_COMPILER_FAILURE", "ERROR", failure.getMessage()));
|
||||
unknown.put("limitations", List.of(p.studio.utilities.json.DiagnosticPayloads.limitation("DIAGNOSTICS_UNAVAILABLE", "Compiler did not provide structured issues")));
|
||||
issues.add(p.studio.api.diagnostic.StudioDiagnostic.fromMap(unknown));
|
||||
}
|
||||
stages.put(current, "rejected".equals(evaluation) ? "rejected" : "failed");
|
||||
emit(logs, sink, ShipperLogEntry.error(ShipperLogSource.BUILD, failure.getMessage()));
|
||||
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, evaluation);
|
||||
} catch (IOException | RuntimeException failure) {
|
||||
if (Thread.currentThread().isInterrupted()) {
|
||||
stages.put(current, "cancelled");
|
||||
return finish(ShipperPreparationStatus.CANCELLED, logs, null, List.of(), issues, stages, inputs, "cancelled");
|
||||
}
|
||||
String message = Objects.requireNonNullElse(failure.getMessage(), failure.getClass().getSimpleName());
|
||||
var value = new java.util.LinkedHashMap<>(p.studio.utilities.json.DiagnosticPayloads.issue("STUDIO_BUILD_FAILED", "ERROR", message));
|
||||
value.put("producer", p.studio.utilities.json.ImmutableJson.fields("component", "studio", "owner", "studio", "languageId", null));
|
||||
value.put("stageId", current); value.put("phase", current);
|
||||
value.put("failureKind", failure instanceof IOException || failure.getCause() instanceof IOException ? "infrastructure-failure" : "unknown");
|
||||
issues.add(p.studio.api.diagnostic.StudioDiagnostic.fromMap(value)); stages.put(current, "failed");
|
||||
emit(logs, sink, ShipperLogEntry.error(ShipperLogSource.BUILD, message));
|
||||
return ShipperPrepareResult.failed(logs, diagnostics);
|
||||
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, "failed");
|
||||
}
|
||||
}
|
||||
|
||||
private ShipperPrepareResult finish(ShipperPreparationStatus status, List<ShipperLogEntry> logs, Path manifest,
|
||||
List<Path> artifacts, List<p.studio.api.diagnostic.StudioDiagnostic> issues,
|
||||
java.util.Map<String, String> stages, java.util.Map<String, Object> context, String evaluation) {
|
||||
var view = p.studio.utilities.json.DiagnosticPayloads.view(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList(),
|
||||
"build", evaluation, stages.entrySet().stream().map(entry -> p.studio.utilities.json.DiagnosticPayloads.stage(entry.getKey(), entry.getValue())).toList(), "partial");
|
||||
var summary = new java.util.LinkedHashMap<>(view.summary());
|
||||
var coverage = new java.util.LinkedHashMap<>((java.util.Map<String, Object>)summary.get("coverage"));
|
||||
var limitations = new ArrayList<>((List<java.util.Map<String, Object>>)coverage.get("limitations"));
|
||||
if ("legacy".equals(context.get("compilerDiagnosticCoverage"))) {
|
||||
summary.put("counts", p.studio.utilities.json.ImmutableJson.fields("errors", null, "warnings", null, "infos", null));
|
||||
coverage.put("details", issues.isEmpty() ? "unavailable" : "partial");
|
||||
limitations.add(p.studio.utilities.json.DiagnosticPayloads.limitation("LEGACY_COMPILER_DIAGNOSTICS", "Compiler gateway did not supply a structured diagnostic collection"));
|
||||
}
|
||||
if (status == ShipperPreparationStatus.CANCELLED) {
|
||||
coverage.put("details", "partial");
|
||||
limitations.add(p.studio.utilities.json.DiagnosticPayloads.limitation("CANCELLED_EVALUATION", "Only observations collected before cancellation are available"));
|
||||
}
|
||||
coverage.put("limitations", limitations); summary.put("coverage", coverage);
|
||||
return new ShipperPrepareResult(status, logs, manifest, artifacts, issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::message).toList(),
|
||||
view.issues().stream().map(p.studio.api.diagnostic.StudioDiagnostic::fromMap).toList(), summary, context);
|
||||
}
|
||||
|
||||
private p.studio.api.diagnostic.StudioDiagnostic packerIssue(String code, String severity, String message,
|
||||
java.util.Map<String, Object> facts, ProjectReference project, Path path) {
|
||||
var value = new java.util.LinkedHashMap<>(p.studio.utilities.json.DiagnosticPayloads.issue(code, severity, message));
|
||||
value.put("producer", p.studio.utilities.json.ImmutableJson.fields("component", "packer", "owner", "packer", "languageId", null));
|
||||
value.put("phase", "pack"); value.put("facts", facts); value.put("failureKind", "program-rejection");
|
||||
value.put("origin", p.studio.utilities.json.ImmutableJson.fields("uri", path != null && path.startsWith(project.rootPath()) ? path.toUri().toString() : null,
|
||||
"span", null, "range", null, "availability", path == null ? "unavailable" : "partial"));
|
||||
return p.studio.api.diagnostic.StudioDiagnostic.fromMap(value);
|
||||
}
|
||||
|
||||
private static void checkCancelled() {
|
||||
if (Thread.currentThread().isInterrupted()) throw new java.util.concurrent.CancellationException("Build cancelled");
|
||||
}
|
||||
|
||||
+4
-1
@@ -49,7 +49,10 @@ class TaskLanguageQueriesTest {
|
||||
assertEquals(ControlResult.Status.BUSY, assertThrows(ControlDeniedException.class,
|
||||
() -> session.submit(task, "overlap", () -> null)).status());
|
||||
} finally { worker.stopped.complete(null); }
|
||||
assertSame(failure, assertThrows(ExecutionException.class, () -> failed.get(5, TimeUnit.SECONDS)).getCause());
|
||||
var returned = assertThrows(ExecutionException.class, () -> failed.get(5, TimeUnit.SECONDS)).getCause();
|
||||
assertInstanceOf(AutomationException.class, returned);
|
||||
assertEquals(failure.failure().code(), ((AutomationException)returned).failure().code());
|
||||
assertEquals(true, ((AutomationException)returned).failure().details().get("workerTerminated"));
|
||||
var next = caller.submit(() -> queries.query(root, task.value(), "definition", Map.of()));
|
||||
var nextWorker = submitted.poll(5, TimeUnit.SECONDS);
|
||||
assertNotNull(nextWorker);
|
||||
|
||||
+9
@@ -254,6 +254,15 @@ class DefaultAutomationSessionTest {
|
||||
assertEquals(CLOSED, await(grant).status()); assertTrue(s.snapshot().closed());
|
||||
}
|
||||
|
||||
@Test void failedTaskResourceCleanupIsRetainedForExplicitRetry() {
|
||||
var s = session(); s.grant(false).toCompletableFuture().join(); var identity = s.acquire().task();
|
||||
var attempts = new java.util.concurrent.atomic.AtomicInteger();
|
||||
s.onTaskEnd(identity, () -> { if (attempts.incrementAndGet() == 1) throw new IllegalStateException("release failed"); });
|
||||
assertEquals(CLEANUP_FAILED, await(s.reclaim()).status());
|
||||
assertEquals(ControlState.RETURNING_CONTROL, s.snapshot().state()); assertFalse(s.snapshot().manualAllowed());
|
||||
assertEquals(RETURNED, await(s.reclaim()).status()); assertEquals(2, attempts.get()); assertTrue(s.snapshot().manualAllowed());
|
||||
}
|
||||
|
||||
static class Documents implements DocumentHandoff {
|
||||
boolean dirty, failSave; int saves, refreshes;
|
||||
CompletableFuture<Void> save = CompletableFuture.completedFuture(null), refresh = CompletableFuture.completedFuture(null);
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package p.studio.core.shipper;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.project.*;
|
||||
import p.studio.api.operation.*;
|
||||
import p.studio.api.shipper.*;
|
||||
import p.studio.api.diagnostic.StudioDiagnostic;
|
||||
import p.studio.utilities.json.*;
|
||||
import p.packer.*;
|
||||
import p.packer.messages.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.lang.reflect.Proxy;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StructuredShipperTest {
|
||||
@TempDir Path root;
|
||||
StudioShipperService shipper(CompilerBuildGateway compiler, boolean cancelPack) {
|
||||
var packer = (PackerWorkspaceService)Proxy.newProxyInstance(PackerWorkspaceService.class.getClassLoader(), new Class[]{PackerWorkspaceService.class}, (proxy, method, args) -> {
|
||||
if (method.getName().equals("validatePackWorkspace")) {
|
||||
if (cancelPack) throw new CancellationException("pack validation cancelled");
|
||||
return new ValidatePackWorkspaceResult(PackerOperationStatus.FAILED, "failed without assets", List.of());
|
||||
}
|
||||
throw new AssertionError("No subsequent stage is allowed: " + method.getName());
|
||||
});
|
||||
var executor = new OperationExecutor() { public <R> OperationHandle<R> submit(String summary, Callable<R> work) { throw new AssertionError(); } };
|
||||
return new StudioShipperService(compiler, packer, new ObjectMapper(), executor);
|
||||
}
|
||||
ProjectReference project() { return new ProjectReference("case", "1", "pbs", 1, ProjectTarget.Game, root); }
|
||||
@Test void failedPackerWithEmptyAssetsCannotApproveAndKeepsCompilerWarning() throws Exception {
|
||||
Files.writeString(root.resolve("prometeu.json"), "{\"app_id\":1}");
|
||||
var warning = StudioDiagnostic.fromMap(DiagnosticPayloads.issue("ORIGINAL_WARNING", "WARNING", "warning"));
|
||||
var service = shipper((project, sink) -> new CompilerBuildGateway.CompilerBuildOutput(null, List.of(), List.of(warning), Map.of("analysisId", "original")), false);
|
||||
var result = service.prepareNow(project(), ignored -> {});
|
||||
assertEquals(ShipperPreparationStatus.FAILED, result.status());
|
||||
assertEquals("rejected", result.summary().get("validationStatus"));
|
||||
assertEquals(List.of("ORIGINAL_WARNING", "PACKER_BUILD"), result.issues().stream().map(StudioDiagnostic::code).toList());
|
||||
assertTrue(result.summary().toString().contains("not-run"));
|
||||
assertFalse(Files.exists(root.resolve("build/manifest.json")));
|
||||
}
|
||||
@Test void packerDiagnosticPreservesAssetEvidenceAndNeverInventsBankIdentity() throws Exception {
|
||||
Files.writeString(root.resolve("prometeu.json"), "{\"app_id\":1}");
|
||||
var diagnostic = new p.packer.dtos.PackerDiagnosticDTO(p.packer.messages.diagnostics.PackerDiagnosticSeverity.ERROR,
|
||||
p.packer.messages.diagnostics.PackerDiagnosticCategory.STRUCTURAL, "invalid asset", root.resolve("evidence.json"), true);
|
||||
var asset = new p.packer.dtos.PackerPackValidationAssetDTO(42, "fixture", root.resolve("assets/fixture.json"), 1, List.of(diagnostic));
|
||||
var packer = (PackerWorkspaceService)Proxy.newProxyInstance(PackerWorkspaceService.class.getClassLoader(), new Class[]{PackerWorkspaceService.class}, (proxy, method, args) -> {
|
||||
if (method.getName().equals("validatePackWorkspace")) return new ValidatePackWorkspaceResult(PackerOperationStatus.FAILED, "blocked", List.of(asset));
|
||||
throw new AssertionError(method.getName());
|
||||
});
|
||||
var executor = new OperationExecutor() { public <R> OperationHandle<R> submit(String summary, Callable<R> work) { throw new AssertionError(); } };
|
||||
var service = new StudioShipperService((project, sink) -> new CompilerBuildGateway.CompilerBuildOutput(null,List.of()), packer, new ObjectMapper(), executor);
|
||||
var result = service.prepareNow(project(), ignored -> {}); var issue = result.issues().getFirst().toMap();
|
||||
assertEquals("PACKER_STRUCTURAL", issue.get("code")); assertEquals("studio-adapter", issue.get("codeOrigin"));
|
||||
var facts = (Map<?, ?>)issue.get("facts"); assertEquals(true, facts.get("blocking")); assertEquals(42, facts.get("assetId"));
|
||||
assertEquals(root.resolve("evidence.json").toString(), facts.get("evidencePath"));
|
||||
assertEquals("not-recorded", facts.get("bankAvailability")); assertFalse(facts.containsKey("bankId"));
|
||||
}
|
||||
@Test void neutralCompilerFailureKeepsPayloadAndDoesNotRunPacker() {
|
||||
var value = new LinkedHashMap<>(DiagnosticPayloads.issue("PBC_INVALID_IR", "ERROR", "invalid RET"));
|
||||
value.put("checkId", "compiler.verify.ret-stack-height"); value.put("failureKind", "program-rejection");
|
||||
value.put("facts", Map.of("expectedHeight", 0, "actualHeight", 1));
|
||||
var issue = StudioDiagnostic.fromMap(value);
|
||||
var service = shipper((project, sink) -> { throw new CompilerBuildFailure("rejected", List.of(issue), Map.of("analysisId", "original"), true); }, false);
|
||||
var result = service.prepareNow(project(), ignored -> {});
|
||||
assertEquals(issue.toMap(), result.issues().getFirst().toMap()); assertEquals("original", result.context().get("analysisId"));
|
||||
assertEquals("rejected", result.summary().get("validationStatus"));
|
||||
}
|
||||
@Test void cancellationDuringCompileOrPackRemainsCancellation() throws Exception {
|
||||
Files.writeString(root.resolve("prometeu.json"), "{\"app_id\":1}");
|
||||
for (boolean duringPack : List.of(false, true)) {
|
||||
var result = shipper((project, sink) -> {
|
||||
if (!duringPack) throw new CancellationException();
|
||||
return new CompilerBuildGateway.CompilerBuildOutput(null, List.of());
|
||||
}, duringPack).prepareNow(project(), ignored -> {});
|
||||
assertEquals(ShipperPreparationStatus.CANCELLED, result.status());
|
||||
assertEquals("cancelled", result.summary().get("validationStatus")); assertNull(result.manifestPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user