feat(studio): improve compiler reliability and AI diagnostics
Test / Tests and coverage occurred while executing withChecks step.
Intrepid/Prometeu/Studio/pipeline/pr-master There was a failure building this commit

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:
2026-11-10 12:36:25 +00:00
parent 38f201a9c9
commit ea1cc26fe3
1216 changed files with 168017 additions and 354 deletions
@@ -8,7 +8,15 @@ import p.studio.core.shipper.CompilerBuildGateway;
import p.studio.utilities.logs.LogAggregator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import p.studio.compiler.source.diagnostics.CompilerDiagnosticProjection;
import p.studio.compiler.source.diagnostics.SourceReference;
import p.studio.compiler.exceptions.BuildException;
import p.studio.api.diagnostic.StudioDiagnostic;
import p.studio.core.shipper.CompilerBuildFailure;
import p.studio.utilities.json.ImmutableJson;
public final class CompilerBuildGatewayAdapter implements CompilerBuildGateway {
private final p.animation.GlyphBankLayoutSource layouts;
@@ -18,12 +26,41 @@ public final class CompilerBuildGatewayAdapter implements CompilerBuildGateway {
public CompilerBuildOutput compile(ProjectReference project, Consumer<String> logSink) {
final var config = new BuilderPipelineConfig(false, project.rootPath().toString());
var context=BuilderPipelineContext.fromConfig(config);context.glyphBankLayoutSource=layouts;
var analysisId = UUID.randomUUID().toString();
try {
final var result = BuilderPipelineService.INSTANCE.build(
context,
LogAggregator.with(logSink));
final var capabilities = new LinkedHashSet<>(
result.compileResult().analysisSnapshot().irBackend()
.getReservedMetadata().requiredCapabilities().asList());
return new CompilerBuildOutput(result.bytecodeArtifactPath(), capabilities.stream().toList());
var sources = SourceReference.snapshot(result.compileResult().analysisSnapshot().fileTable());
var root = context.rootProjectPathCanon == null ? project.rootPath() : context.rootProjectPathCanon;
var issues = CompilerDiagnosticProjection.issues(result.diagnostics(), sources,
CompilerDiagnosticProjection.capturedText(context.fileTable), root, analysisId).stream().map(StudioDiagnostic::fromMap).toList();
return new CompilerBuildOutput(result.bytecodeArtifactPath(), capabilities.stream().toList(), issues,
inputContext(context, root, analysisId, sources));
} catch (BuildException failure) {
var root = context.rootProjectPathCanon == null ? project.rootPath() : context.rootProjectPathCanon;
var issues = CompilerDiagnosticProjection.issues(failure.getDiagnostics(), failure.getSourceReferences(),
CompilerDiagnosticProjection.capturedText(context.fileTable), root, analysisId).stream().map(StudioDiagnostic::fromMap).toList();
var inputs = new java.util.LinkedHashMap<>(inputContext(context, root, analysisId, failure.getSourceReferences()));
if (failure.getCause() != null) inputs.put("validationStatus", "failed");
throw new CompilerBuildFailure(failure.getMessage(), issues, inputs, failure.hasDiagnosticPayload());
}
}
private Map<String, Object> inputContext(BuilderPipelineContext context, java.nio.file.Path root, String analysisId,
Map<Integer, SourceReference> sources) {
var workspace = context.resolvedWorkspace;
return ImmutableJson.fields("projectRoot", root.toString(), "analysisId", analysisId,
"taskId", null, "operationId", null, "clientRequestId", null, "buildId", null, "queryId", null, "document", null,
"inputs", CompilerDiagnosticProjection.inputs(sources, root), "inputCoverage", "compiler-inputs",
"configuration", ImmutableJson.fields("languageId", workspace == null || workspace.frontendSpec() == null ? null : workspace.frontendSpec().getLanguageId(),
"sdkVersion", workspace == null ? null : workspace.stdlib(), "target", workspace == null ? null : workspace.target().name(),
"vmProfile", context.config.vmProfile(), "revisionAvailability", "not-recorded"),
"coverage", "partial", "limitations", java.util.List.of(ImmutableJson.fields("code", "PACK_INPUT_REVISIONS_NOT_CAPTURED",
"message", "Compiler source revisions do not identify all pack/assets inputs"),
ImmutableJson.fields("code", "CONFIGURATION_REVISION_NOT_RECORDED", "message", "Configuration values are recorded without a manifest-byte revision")));
}
}
@@ -9,6 +9,8 @@ import org.eclipse.lsp4j.services.*;
import p.studio.api.automation.*;
import p.studio.api.project.ProjectCatalog;
import p.studio.core.language.LanguageQueryPort;
import p.studio.core.language.LanguageQueryExecution;
import p.studio.utilities.json.*;
import p.studio.lsp.api.LspServerLifecycle;
import p.studio.lsp.services.protocol.ExternalSourceLanguageServer;
import org.eclipse.lsp4j.jsonrpc.Launcher;
@@ -41,7 +43,7 @@ public final class AutomationLspQueries implements LanguageQueryPort {
@Override public void endTask(TaskIdentity task) {
items.remove(task);
final var state = externalTasks.remove(task);
final var state = externalTasks.get(task);
if (state == null) return;
final var workers = Executors.newCachedThreadPool();
try (var socket = new Socket()) {
@@ -60,13 +62,15 @@ public final class AutomationLspQueries implements LanguageQueryPort {
init.setInitializationOptions(Map.of("prometeuExternalConsumerId", state.consumer()));
server.initialize(init).get(2, TimeUnit.SECONDS);
server.releaseExternalSource(new ExternalSourceLanguageServer.Request(state.consumer(), null)).get(2, TimeUnit.SECONDS);
externalTasks.remove(task, state);
} finally { listening.cancel(true); }
} catch (Exception failure) {
// A closed project host has already released its registry. Other cleanup
// failures are surfaced instead of silently reporting a successful task end.
if (lifecycle.endpoint(state.binding()).isPresent())
throw new IllegalStateException("Could not release task external sources", failure);
} finally { workers.shutdownNow(); }
externalTasks.remove(task, state);
} finally { new LanguageQueryExecution("endTask", 10_000, 2_000).finishWorkers(workers); }
}
private static class QuietClient implements LanguageClient {
@@ -78,6 +82,24 @@ public final class AutomationLspQueries implements LanguageQueryPort {
}
@Override public Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> parameters) throws Exception {
return query(task, operation, parameters, new LanguageQueryExecution(operation, 10_000, 2_000));
}
@Override public Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> parameters,
LanguageQueryExecution execution) throws Exception {
try {
var result = performQuery(task, operation, parameters, execution);
if (execution.hasFailure()) throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_CLEANUP_FAILED, "Language query cleanup failed", true);
return result;
} catch (IllegalArgumentException | ControlDeniedException failure) { throw failure; }
catch (Exception failure) {
recordFailure(execution, failure);
throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_INTERNAL_FAILURE, "Language query failed", true);
}
}
private Map<String, Object> performQuery(TaskIdentity task, String operation, Map<String, Object> parameters,
LanguageQueryExecution execution) throws Exception {
if (!OPERATIONS.contains(operation)) throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "Unsupported language operation: " + operation);
var context = knowledge.context(task.projectRoot());
var primary = context.languages().stream().filter(ProjectKnowledge.Language::primary).findFirst().orElseThrow();
@@ -85,7 +107,7 @@ public final class AutomationLspQueries implements LanguageQueryPort {
throw failure(AutomationFailure.Code.UNSUPPORTED_LANGUAGE, "Project language has no available LSP");
var project = projects.openProject(context.projectRoot());
var binding = LspLifecycleAdapter.context(project);
var endpoint = lifecycle.endpoint(binding).orElseThrow(() -> failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Project LSP is not running; reopen the project if its language changed"));
var endpoint = lifecycle.endpoint(binding).orElseThrow(() -> failure(AutomationFailure.Code.LANGUAGE_SERVER_UNAVAILABLE, "Project LSP is not running; reopen the project if its language changed"));
var params = mapper.valueToTree(parameters);
CallHierarchyItem item = null;
String uri = null;
@@ -116,11 +138,14 @@ public final class AutomationLspQueries implements LanguageQueryPort {
}
uri = document.toUri().toString();
}
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
long deadline = execution.deadlineNanos();
if (System.nanoTime() >= deadline) throw new TimeoutException("Language query admission budget expired");
var workers = Executors.newCachedThreadPool(r -> { var thread = new Thread(r, "studio-automation-lsp"); thread.setDaemon(true); return thread; });
try (var socket = new Socket()) {
if (!InetAddress.getByName(endpoint.host()).isLoopbackAddress()) throw failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Project LSP endpoint is not local");
socket.connect(new InetSocketAddress(endpoint.host(), endpoint.port()), 2000);
execution.own(() -> { try { socket.close(); } catch (java.io.IOException ignored) {} });
execution.own(workers::shutdownNow);
if (!InetAddress.getByName(endpoint.host()).isLoopbackAddress()) throw failure(AutomationFailure.Code.LANGUAGE_SERVER_UNAVAILABLE, "Project LSP endpoint is not local");
socket.connect(new InetSocketAddress(endpoint.host(), endpoint.port()), (int)Math.max(1, Math.min(2000, TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()))));
var diagnostics = new CompletableFuture<List<Diagnostic>>();
String expectedUri = uri;
LanguageClient client = new LanguageClient() {
@@ -137,6 +162,7 @@ public final class AutomationLspQueries implements LanguageQueryPort {
.setRemoteInterface(ExternalSourceLanguageServer.class).setInput(socket.getInputStream())
.setOutput(socket.getOutputStream()).setExecutorService(workers).create();
var listening = launcher.startListening();
execution.own(() -> listening.cancel(true));
final var server = launcher.getRemoteProxy();
final var acquired = new HashSet<String>();
boolean succeeded = false;
@@ -144,12 +170,14 @@ public final class AutomationLspQueries implements LanguageQueryPort {
var initialize = new InitializeParams();
initialize.setRootUri(binding.projectRoot().toUri().toString());
initialize.setCapabilities(new ClientCapabilities());
initialize.getCapabilities().setExperimental(Map.of("prometeuExternalSources", true));
initialize.getCapabilities().setExperimental(Map.of("prometeuExternalSources", true,
"prometeuDiagnosticAnalysis", Map.of("version", 1)));
final var previous = externalTasks.get(task);
initialize.setInitializationOptions(previous == null
? Map.of("prometeuExternalPersistentConsumer", true)
: Map.of("prometeuExternalConsumerId", previous.consumer(), "prometeuExternalPersistentConsumer", true));
var capabilities = await(server.initialize(initialize), deadline).getCapabilities();
execution.method("initialize");
var capabilities = await(server.initialize(initialize), deadline, execution).getCapabilities();
final var experimental = gson.toJsonTree(capabilities.getExperimental());
if (experimental.isJsonObject() && experimental.getAsJsonObject().has("prometeuExternalConsumerId")) {
final String consumer = experimental.getAsJsonObject().get("prometeuExternalConsumerId").getAsString();
@@ -158,52 +186,69 @@ public final class AutomationLspQueries implements LanguageQueryPort {
server.initialized(new InitializedParams());
requireCapability(operation, capabilities);
List<Diagnostic> currentDiagnostics = List.of();
Map<String, Object> analysis = Map.of();
var rich = experimental.isJsonObject() ? experimental.getAsJsonObject().get("prometeuDiagnosticAnalysis") : null;
boolean richAnalysis = rich != null && rich.isJsonObject() && rich.getAsJsonObject().has("version")
&& rich.getAsJsonObject().get("version").getAsInt() == 1;
String digest = source == null ? null : sha256(source);
if (document != null) {
// Fresh client overlay contains exactly the saved UTF-8 file. Wait for analysis before querying.
server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(new TextDocumentItem(uri, primary.id(), 1, source)));
currentDiagnostics = await(diagnostics, deadline);
if (richAnalysis) {
execution.method("prometeu/documentAnalysis");
var retained = await(server.documentAnalysis(new ExternalSourceLanguageServer.DiagnosticAnalysisRequest(
new VersionedTextDocumentIdentifier(uri, 1), digest)), deadline, execution);
analysis = mapper.convertValue(mapper.readTree(gson.toJson(retained)), Map.class);
}
currentDiagnostics = await(diagnostics, deadline, execution);
}
Map<String, Object> externalRevision = Map.of();
if (external) {
final var request = new ExternalSourceLanguageServer.Request(externalTasks.get(task).consumer(), uri);
source = await(server.revalidateExternalSource(request), deadline).text();
var content = await(server.revalidateExternalSource(request), deadline, execution);
source = content.text();
externalRevision = mapper.convertValue(mapper.readTree(gson.toJson(content)), Map.class);
externalRevision = new LinkedHashMap<>(externalRevision);
externalRevision.remove("text");
if (source.getBytes(java.nio.charset.StandardCharsets.UTF_8).length > 4 * 1024 * 1024)
throw failure(AutomationFailure.Code.LIMIT_EXCEEDED, "External source exceeds 4 MiB");
}
execution.method(operation);
var text = server.getTextDocumentService();
var id = uri == null ? null : new TextDocumentIdentifier(uri);
Object result = switch (operation) {
case "readExternalSource" -> await(server.readExternalSource(
new ExternalSourceLanguageServer.Request(externalTasks.get(task).consumer(), uri)), deadline);
new ExternalSourceLanguageServer.Request(externalTasks.get(task).consumer(), uri)), deadline, execution);
case "diagnostics" -> currentDiagnostics;
case "hover" -> await(text.hover(new HoverParams(id, position(params.path("position"), source))), deadline);
case "completion" -> await(text.completion(new CompletionParams(id, position(params.path("position"), source))), deadline);
case "signatureHelp" -> await(text.signatureHelp(new SignatureHelpParams(id, position(params.path("position"), source))), deadline);
case "definition" -> await(text.definition(new DefinitionParams(id, position(params.path("position"), source))), deadline);
case "references" -> await(text.references(new ReferenceParams(id, position(params.path("position"), source), new ReferenceContext(params.path("includeDeclaration").asBoolean(false)))), deadline);
case "documentSymbols" -> await(text.documentSymbol(new DocumentSymbolParams(id)), deadline);
case "workspaceSymbols" -> await(server.getWorkspaceService().symbol(new WorkspaceSymbolParams(params.path("query").asText(""))), deadline);
case "prepareRename" -> await(text.prepareRename(new PrepareRenameParams(id, position(params.path("position"), source))), deadline);
case "hover" -> await(text.hover(new HoverParams(id, position(params.path("position"), source))), deadline, execution);
case "completion" -> await(text.completion(new CompletionParams(id, position(params.path("position"), source))), deadline, execution);
case "signatureHelp" -> await(text.signatureHelp(new SignatureHelpParams(id, position(params.path("position"), source))), deadline, execution);
case "definition" -> await(text.definition(new DefinitionParams(id, position(params.path("position"), source))), deadline, execution);
case "references" -> await(text.references(new ReferenceParams(id, position(params.path("position"), source), new ReferenceContext(params.path("includeDeclaration").asBoolean(false)))), deadline, execution);
case "documentSymbols" -> await(text.documentSymbol(new DocumentSymbolParams(id)), deadline, execution);
case "workspaceSymbols" -> await(server.getWorkspaceService().symbol(new WorkspaceSymbolParams(params.path("query").asText(""))), deadline, execution);
case "prepareRename" -> await(text.prepareRename(new PrepareRenameParams(id, position(params.path("position"), source))), deadline, execution);
case "rename" -> {
String name = params.path("newName").asText("");
if (name.isBlank()) throw new IllegalArgumentException("newName is required");
yield await(text.rename(new RenameParams(id, position(params.path("position"), source), name)), deadline);
yield await(text.rename(new RenameParams(id, position(params.path("position"), source), name)), deadline, execution);
}
case "formatting" -> await(text.formatting(new DocumentFormattingParams(id, new FormattingOptions(4, true))), deadline);
case "formatting" -> await(text.formatting(new DocumentFormattingParams(id, new FormattingOptions(4, true))), deadline, execution);
case "codeActions" -> await(text.codeAction(new CodeActionParams(id,
range(params.path("range"), source),
new CodeActionContext(currentDiagnostics))), deadline);
case "documentLinks" -> await(text.documentLink(new DocumentLinkParams(id)), deadline);
case "foldingRanges" -> await(text.foldingRange(new FoldingRangeRequestParams(id)), deadline);
new CodeActionContext(currentDiagnostics))), deadline, execution);
case "documentLinks" -> await(text.documentLink(new DocumentLinkParams(id)), deadline, execution);
case "foldingRanges" -> await(text.foldingRange(new FoldingRangeRequestParams(id)), deadline, execution);
case "selectionRanges" -> {
if (!params.path("positions").isArray() || params.path("positions").isEmpty()) throw new IllegalArgumentException("positions are required");
var positions = new ArrayList<Position>();
for (var p : params.path("positions")) positions.add(position(p, source));
yield await(text.selectionRange(new SelectionRangeParams(id, positions)), deadline);
yield await(text.selectionRange(new SelectionRangeParams(id, positions)), deadline, execution);
}
case "prepareCallHierarchy" -> await(text.prepareCallHierarchy(new CallHierarchyPrepareParams(id, position(params.path("position"), source))), deadline);
case "incomingCalls" -> await(text.callHierarchyIncomingCalls(new CallHierarchyIncomingCallsParams(item)), deadline);
case "outgoingCalls" -> await(text.callHierarchyOutgoingCalls(new CallHierarchyOutgoingCallsParams(item)), deadline);
case "semanticTokens" -> await(text.semanticTokensFull(new SemanticTokensParams(id)), deadline);
case "prepareCallHierarchy" -> await(text.prepareCallHierarchy(new CallHierarchyPrepareParams(id, position(params.path("position"), source))), deadline, execution);
case "incomingCalls" -> await(text.callHierarchyIncomingCalls(new CallHierarchyIncomingCallsParams(item)), deadline, execution);
case "outgoingCalls" -> await(text.callHierarchyOutgoingCalls(new CallHierarchyOutgoingCallsParams(item)), deadline, execution);
case "semanticTokens" -> await(text.semanticTokensFull(new SemanticTokensParams(id)), deadline, execution);
default -> throw new IllegalArgumentException("Unsupported operation");
};
var wire = mapper.readTree(gson.toJson(result));
@@ -215,7 +260,7 @@ public final class AutomationLspQueries implements LanguageQueryPort {
acquired.removeAll(externalTasks.get(task).uris());
for (String externalUri : returned) {
await(server.retainExternalSource(new ExternalSourceLanguageServer.Request(
externalTasks.get(task).consumer(), externalUri)), deadline);
externalTasks.get(task).consumer(), externalUri)), deadline, execution);
externalTasks.get(task).uris().add(externalUri);
}
}
@@ -229,9 +274,23 @@ public final class AutomationLspQueries implements LanguageQueryPort {
if (operation.equals("prepareCallHierarchy") || operation.equals("incomingCalls") || operation.equals("outgoingCalls")) addItemIds(task, wire);
var output = new LinkedHashMap<String, Object>();
output.put("operation", operation); output.put("result", mapper.convertValue(wire, Object.class));
if (document != null && !richAnalysis) {
limitations.add("ANALYSIS_DETAILS_UNAVAILABLE");
analysis = ImmutableJson.fields("analysisId", null, "issues", List.of(), "summary",
DiagnosticPayloads.legacySummary("document"));
}
output.put("limitations", limitations);
output.put("analysis", analysis);
output.put("context", ImmutableJson.fields("projectRoot", context.projectRoot().toString(),
"taskId", task.value(), "clientRequestId", null, "buildId", null, "queryId", execution.queryId(), "analysisId", analysis.get("analysisId"),
"document", document == null ? null : ImmutableJson.fields("uri", uri, "version", 1, "sha256", digest),
"inputs", analysis.getOrDefault("inputs", List.of()), "externalRevision", externalRevision,
"inputCoverage", "partial", "configuration", analysis.getOrDefault("configuration", Map.of())));
succeeded = true;
return Collections.unmodifiableMap(output);
} catch (Exception failure) {
recordFailure(execution, failure);
throw failure;
} finally {
if (!succeeded && !acquired.isEmpty() && externalTasks.containsKey(task)) {
final var state = externalTasks.get(task);
@@ -242,11 +301,39 @@ public final class AutomationLspQueries implements LanguageQueryPort {
server.releaseExternalSource(new ExternalSourceLanguageServer.Request(state.consumer(), acquiredUri))
.get(500, TimeUnit.MILLISECONDS);
}
} catch (Exception cleanupFailure) {
execution.record(AutomationFailure.Code.LANGUAGE_QUERY_CLEANUP_FAILED, "Could not release query external sources", Map.of());
} finally { if (interrupted) Thread.currentThread().interrupt(); }
}
listening.cancel(true);
}
} finally { workers.shutdownNow(); }
} finally { execution.finishWorkers(workers); }
}
private static String sha256(String text) throws java.security.NoSuchAlgorithmException {
return HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256")
.digest(text.getBytes(java.nio.charset.StandardCharsets.UTF_8)));
}
private static void recordFailure(LanguageQueryExecution execution, Exception failure) {
if (failure instanceof AutomationException domain) { execution.record(domain.failure()); return; }
if (failure instanceof IllegalArgumentException || failure instanceof ControlDeniedException) return;
var cause = failure instanceof ExecutionException && failure.getCause() != null ? failure.getCause() : failure;
AutomationFailure.Code code;
Map<String, Object> details = Map.of();
if (cause instanceof TimeoutException || cause instanceof SocketTimeoutException) code = AutomationFailure.Code.LANGUAGE_QUERY_TIMEOUT;
else if (cause instanceof InterruptedException || cause instanceof CancellationException) code = AutomationFailure.Code.LANGUAGE_QUERY_CANCELLED;
else if (cause instanceof ConnectException || cause instanceof SocketException) code = AutomationFailure.Code.LANGUAGE_SERVER_UNAVAILABLE;
else if (cause instanceof org.eclipse.lsp4j.jsonrpc.ResponseErrorException response) {
int rpc = response.getResponseError().getCode();
details = ImmutableJson.fields("protocolCode", rpc, "reason", rpc == -32020 ? "ANALYSIS_REVISION_MISMATCH" : rpc == -32021 ? "ANALYSIS_UNAVAILABLE" : null);
code = rpc == -32020 ? AutomationFailure.Code.LANGUAGE_QUERY_REVISION_MISMATCH
: rpc == -32021 || rpc == -32603 ? AutomationFailure.Code.LANGUAGE_QUERY_INTERNAL_FAILURE
: AutomationFailure.Code.LANGUAGE_QUERY_PROTOCOL_FAILURE;
} else if (cause instanceof org.eclipse.lsp4j.jsonrpc.JsonRpcException || cause instanceof com.google.gson.JsonParseException
|| cause instanceof com.fasterxml.jackson.core.JsonProcessingException) code = AutomationFailure.Code.LANGUAGE_QUERY_PROTOCOL_FAILURE;
else code = AutomationFailure.Code.LANGUAGE_QUERY_INTERNAL_FAILURE;
execution.record(code, "Language query failed: " + code.name(), details);
}
private void rememberExternalUris(Set<String> uris, com.fasterxml.jackson.databind.JsonNode node) {
@@ -336,7 +423,8 @@ public final class AutomationLspQueries implements LanguageQueryPort {
if (node.isContainerNode()) for (var child : node) validateEdits(context, child);
}
private static <T> T await(CompletableFuture<T> future, long deadline) throws Exception {
private static <T> T await(CompletableFuture<T> future, long deadline, LanguageQueryExecution execution) throws Exception {
execution.own(() -> future.cancel(true));
try { return future.get(Math.max(1, deadline - System.nanoTime()), TimeUnit.NANOSECONDS); }
catch (ExecutionException failure) {
if (failure.getCause() instanceof org.eclipse.lsp4j.jsonrpc.ResponseErrorException response) {
@@ -10,7 +10,9 @@ import java.util.Objects;
public final class LspLifecycleAdapter implements LspLifecyclePort {
private final LspServerLifecycle lifecycle;
private final java.util.Map<java.nio.file.Path, LspProjectContext> bindings = new java.util.concurrent.ConcurrentHashMap<>();
private record Binding(LspProjectContext context, String sessionId) {}
private final com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
private final java.util.Map<java.nio.file.Path, Binding> bindings = new java.util.concurrent.ConcurrentHashMap<>();
public LspLifecycleAdapter(LspServerLifecycle lifecycle) {
this.lifecycle = Objects.requireNonNull(lifecycle, "lifecycle");
@@ -19,15 +21,41 @@ public final class LspLifecycleAdapter implements LspLifecyclePort {
@Override
public void boot(ProjectReference project) {
var binding = context(project);
String sessionId = java.util.UUID.randomUUID().toString();
lifecycle.bootServer(LspServerConfiguration.builder().endpoint(
new p.studio.lsp.messages.LspServerEndpoint("127.0.0.1", 0)).build(), binding);
bindings.put(project.rootPath().toAbsolutePath().normalize(), binding);
try {
var endpoint = lifecycle.endpoint(binding).orElseThrow(() -> new IllegalStateException("Project LSP endpoint unavailable"));
var directory = java.nio.file.Files.createDirectories(binding.projectRoot().resolve(".studio"));
var temporary = java.nio.file.Files.createTempFile(directory, "lsp-", ".tmp");
try {
mapper.writeValue(temporary.toFile(), java.util.Map.of(
"schemaVersion", 1, "projectRoot", binding.projectRoot().toString(),
"sessionId", sessionId, "processId", ProcessHandle.current().pid(),
"host", endpoint.host(), "port", endpoint.port()));
java.nio.file.Files.move(temporary, directory.resolve("lsp.json"),
java.nio.file.StandardCopyOption.ATOMIC_MOVE, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
} finally { java.nio.file.Files.deleteIfExists(temporary); }
bindings.put(project.rootPath().toAbsolutePath().normalize(), new Binding(binding, sessionId));
} catch (Exception failure) {
lifecycle.shutdownServer(binding);
throw new IllegalStateException("Failed to publish project LSP endpoint", failure);
}
}
@Override
public void shutdown(ProjectReference project) {
var binding = bindings.remove(project.rootPath().toAbsolutePath().normalize());
if (binding != null) lifecycle.shutdownServer(binding);
if (binding == null) return;
try {
var file = binding.context().projectRoot().resolve(".studio/lsp.json");
if (java.nio.file.Files.exists(file)
&& binding.sessionId().equals(mapper.readTree(file.toFile()).path("sessionId").asText())) {
java.nio.file.Files.deleteIfExists(file);
}
} catch (java.io.IOException failure) {
throw new IllegalStateException("Failed to remove project LSP discovery", failure);
} finally { lifecycle.shutdownServer(binding.context()); }
}
static LspProjectContext context(ProjectReference project) {
@@ -89,6 +89,7 @@ public final class McpTaskTools {
result.put("manifestPath", build.manifestPath() == null ? null : build.manifestPath().toString());
result.set("emittedArtifacts", mapper.valueToTree(build.emittedArtifacts().stream().map(Path::toString).toList()));
result.set("diagnostics", mapper.valueToTree(build.diagnostics()));
build.diagnosticView().forEach((key, item) -> result.set(key, mapper.valueToTree(item)));
var logs = result.putArray("logs");
build.logs().forEach(log -> logs.addObject().put("source", log.source().name()).put("severity", log.severity().name())
.put("message", log.message()).put("timestamp", log.timestamp().toString()));
@@ -56,7 +56,7 @@ public final class ExecutorOperationExecutor implements OperationExecutor {
final FutureTask<Void> submitted = new FutureTask<>(() -> {
try {
final R result = operation.call();
if (transitionTerminal(OperationState.SUCCEEDED, summary)) {
if (transitionTerminal(cancelledResult(result) ? OperationState.CANCELLED : OperationState.SUCCEEDED, summary)) {
completion.complete(result);
}
} catch (Throwable failure) {
@@ -84,6 +84,11 @@ public final class ExecutorOperationExecutor implements OperationExecutor {
}
}
private static boolean cancelledResult(Object result) {
return result instanceof p.studio.api.automation.AutomationOutcome outcome && outcome.kind() == p.studio.api.automation.AutomationOutcome.Kind.CANCELLED
|| result instanceof p.studio.api.shipper.ShipperPrepareResult build && build.status() == p.studio.api.shipper.ShipperPreparationStatus.CANCELLED;
}
@Override
public OperationId id() {
return id;
@@ -57,6 +57,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -169,6 +174,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -260,6 +270,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -351,6 +366,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -383,7 +403,7 @@
{
"name": "studio_language_query",
"authority": "task",
"description": "Query project LSP capabilities using saved project files or task-owned read-only external sources. readExternalSource reads an external URI returned by definition; hover and definition can continue inside it. Returns refactoring proposals, never applies edits.",
"description": "Query project LSP capabilities using saved project files or task-owned read-only external sources. readExternalSource reads an external URI returned by definition; hover and definition can continue inside it. Returns refactoring proposals, never applies edits. Adds analysis and context from the retained didOpen analysis negotiated with prometeuDiagnosticAnalysis v1. Includes saved-source SHA-256 and queryId. 10000 ms execution from admission plus 2000 ms cleanup; language failures use LANGUAGE_QUERY_* or LANGUAGE_SERVER_UNAVAILABLE. Capability fallback is limited with ANALYSIS_DETAILS_UNAVAILABLE. Never infer validity from an empty standard diagnostic array.",
"inputSchema": {
"type": "object",
"properties": {
@@ -530,7 +550,29 @@
"const": "ok"
},
"value": {
"type": "object"
"type": "object",
"properties": {
"analysis": {
"type": "object",
"properties": {
"issues": {
"type": "array",
"description": "Canonical immutable issues, at most 1024 whole issues and 1 MiB UTF-8 JSON. Explicit LIMIT_EXCEEDED with original counts when partial.",
"items": {
"type": "object",
"description": "issueId, producer, original code/codeOrigin, templateId/templateParameters, checkId, severity, phase/stageId/failureKind, message, facts, explanation, semanticContext, origin, related, suggestedChecks, limitations."
}
},
"summary": {
"type": "object",
"description": "scope=document|project|build; validationStatus=passed|rejected|failed|incomplete|not-evaluated|cancelled; nullable counts, stages and independent detail/revision coverage."
}
}
},
"context": {
"type": "object"
}
}
}
},
"required": [
@@ -558,6 +600,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -592,9 +639,110 @@
}
},
"exampleValue": {
"operation": "definition",
"operation": "diagnostics",
"result": [],
"limitations": []
"limitations": [],
"analysis": {
"analysisId": "analysis-example",
"issues": [
{
"issueId": "analysis-example:0",
"producer": {
"component": "compiler-frontend",
"owner": "compiler/pbs",
"languageId": "pbs"
},
"code": "E_SEM_UNSUPPORTED_LOWERING",
"codeOrigin": "producer",
"templateId": "E_SEM_UNSUPPORTED_LOWERING",
"templateParameters": {},
"checkId": "pbs.lowering.call-resolution",
"severity": "ERROR",
"phase": "semantics",
"stageId": "frontend",
"failureKind": "program-rejection",
"message": "Recorded callable could not be resolved",
"facts": {},
"explanation": {
"id": "pbs.lowering.call-resolution",
"summary": "Executable call lowering must identify an admitted callable using the actual resolution constraints.",
"rule": "Executable call lowering must identify an admitted callable using the actual resolution constraints.",
"availability": "available"
},
"semanticContext": {
"availability": "partial",
"facts": {
"requestedSymbol": {
"value": "data.value",
"availability": "available",
"reason": null
},
"resolutionConstraints": {
"value": "bool",
"availability": "available",
"reason": null
},
"declaredTypes": {
"value": [
"int"
],
"availability": "available",
"reason": null
},
"expressionTypes": {
"value": null,
"availability": "not-recorded",
"reason": "Not recorded by this detector"
}
}
},
"origin": {
"uri": null,
"span": null,
"range": null,
"availability": "unavailable"
},
"related": [],
"suggestedChecks": [],
"limitations": []
}
],
"summary": {
"scope": "project",
"validationStatus": "rejected",
"counts": {
"errors": 1,
"warnings": 0,
"infos": 0
},
"stages": [
{
"stageId": "analysis",
"status": "rejected"
}
],
"coverage": {
"details": "complete",
"revisions": "partial",
"limitations": []
}
},
"inputs": []
},
"context": {
"projectRoot": "/projects/game",
"taskId": "task-example",
"queryId": "query-example",
"analysisId": "analysis-example",
"clientRequestId": null,
"buildId": null,
"document": {
"uri": "file:///projects/game/src/main.pbs",
"version": 1,
"sha256": "0000000000000000000000000000000000000000000000000000000000000000"
},
"inputCoverage": "partial"
}
}
},
{
@@ -664,6 +812,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -797,6 +950,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -900,6 +1058,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -991,6 +1154,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1082,6 +1250,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1182,6 +1355,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1273,6 +1451,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1374,6 +1557,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1537,6 +1725,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1643,6 +1836,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1679,7 +1877,7 @@
{
"name": "studio_operation_get",
"authority": "task",
"description": "Read a task-local operation by exactly one of operationId or clientRequestId, including known progress, typed result and image.",
"description": "Read a task-local operation by exactly one of operationId or clientRequestId, including known progress, typed result and image. Worker completion (SUCCEEDED) is independent of build approval. Build result adds canonical issues, summary and context; usable buildId requires SUCCESS with all four stages passed. Cancellation remains CANCELLED; final results require workerTerminated.",
"inputSchema": {
"type": "object",
"properties": {
@@ -1740,7 +1938,37 @@
"const": "ok"
},
"value": {
"type": "object"
"type": "object",
"properties": {
"result": {
"oneOf": [
{
"type": "null"
},
{
"type": "object",
"properties": {
"issues": {
"type": "array",
"description": "Canonical immutable issues, at most 1024 whole issues and 1 MiB UTF-8 JSON. Explicit LIMIT_EXCEEDED with original counts when partial.",
"items": {
"type": "object",
"description": "issueId, producer, original code/codeOrigin, templateId/templateParameters, checkId, severity, phase/stageId/failureKind, message, facts, explanation, semanticContext, origin, related, suggestedChecks, limitations."
}
},
"summary": {
"type": "object",
"description": "scope=document|project|build; validationStatus=passed|rejected|failed|incomplete|not-evaluated|cancelled; nullable counts, stages and independent detail/revision coverage."
},
"context": {
"type": "object",
"description": "projectRoot/taskId/operationId/clientRequestId/analysisId/buildId and actual-read input revisions; pack/assets coverage is explicit."
}
}
}
]
}
}
}
},
"required": [
@@ -1768,6 +1996,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1795,14 +2028,133 @@
"operationId": "operation-advance"
},
"exampleValue": {
"operationId": "operation-advance",
"clientRequestId": "advance-example",
"state": "RUNNING",
"operationId": "operation-build",
"clientRequestId": "build-example",
"state": "SUCCEEDED",
"workerTerminated": true,
"progress": {
"requestedFrames": 60,
"completedFrames": 12
"requestedFrames": null,
"completedFrames": null
},
"result": null
"failure": null,
"result": {
"kind": "build",
"status": "FAILED",
"buildId": null,
"manifestPath": null,
"emittedArtifacts": [],
"diagnostics": [
"Recorded callable could not be resolved"
],
"logs": [],
"issues": [
{
"issueId": "analysis-example:0",
"producer": {
"component": "compiler-frontend",
"owner": "compiler/pbs",
"languageId": "pbs"
},
"code": "E_SEM_UNSUPPORTED_LOWERING",
"codeOrigin": "producer",
"templateId": "E_SEM_UNSUPPORTED_LOWERING",
"templateParameters": {},
"checkId": "pbs.lowering.call-resolution",
"severity": "ERROR",
"phase": "semantics",
"stageId": "frontend",
"failureKind": "program-rejection",
"message": "Recorded callable could not be resolved",
"facts": {},
"explanation": {
"id": "pbs.lowering.call-resolution",
"summary": "Executable call lowering must identify an admitted callable using the actual resolution constraints.",
"rule": "Executable call lowering must identify an admitted callable using the actual resolution constraints.",
"availability": "available"
},
"semanticContext": {
"availability": "partial",
"facts": {
"requestedSymbol": {
"value": "data.value",
"availability": "available",
"reason": null
},
"resolutionConstraints": {
"value": "bool",
"availability": "available",
"reason": null
},
"declaredTypes": {
"value": [
"int"
],
"availability": "available",
"reason": null
},
"expressionTypes": {
"value": null,
"availability": "not-recorded",
"reason": "Not recorded by this detector"
}
}
},
"origin": {
"uri": null,
"span": null,
"range": null,
"availability": "unavailable"
},
"related": [],
"suggestedChecks": [],
"limitations": []
}
],
"summary": {
"scope": "build",
"validationStatus": "rejected",
"counts": {
"errors": 1,
"warnings": 0,
"infos": 0
},
"stages": [
{
"stageId": "compiler-build",
"status": "rejected"
},
{
"stageId": "pack-validation",
"status": "not-run"
},
{
"stageId": "pack",
"status": "not-run"
},
{
"stageId": "manifest",
"status": "not-run"
}
],
"coverage": {
"details": "complete",
"revisions": "partial",
"limitations": []
}
},
"context": {
"projectRoot": "/projects/game",
"taskId": "task-example",
"operationId": "operation-build",
"clientRequestId": "build-example",
"analysisId": "analysis-example",
"buildId": null,
"queryId": null,
"document": null,
"inputs": [],
"coverage": "partial"
}
}
}
},
{
@@ -1872,6 +2224,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -1965,6 +2322,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -2075,6 +2437,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -2168,6 +2535,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -2259,6 +2631,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -2350,6 +2727,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -2801,6 +3183,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [
@@ -3279,6 +3666,11 @@
"runtimeError": {
"type": "object",
"additionalProperties": true
},
"details": {
"type": "object",
"additionalProperties": true,
"description": "Neutral component/operation/method/queryId, budgets, cleanup and workerTerminated; runtimeError remains native-runtime-only."
}
},
"required": [