connect MCP language queries to project LSP

This commit is contained in:
2026-09-29 09:21:59 +01:00
parent feb57c5298
commit c1834bad1f
24 changed files with 757 additions and 21 deletions
@@ -125,6 +125,68 @@ symbols reuse the existing project LSP. Other queries/refactoring proposals are
exposed only when supported; returned edits MUST respect project versus read-only
SDK/library scope. No query applies filesystem edits.
### 3.1 Language Query Operations
`studio_language_query` uses the existing project's LSP server via a real client,
not a parallel language implementation or a direct call into compiler semantics.
Its successful value is `{operation, result, limitations}`. `result` is the JSON
projection of the existing LSP response (including null or an empty list when
appropriate); LSP union wrappers are unwrapped. Provider classes remain inside
the application adapter. Studio API/Core use maps, lists and scalar values only.
| Operation | Required parameters beyond projectRoot/taskId |
| --- | --- |
| `diagnostics`, `documentSymbols`, `documentLinks`, `foldingRanges`, `formatting`, `semanticTokens` | `parameters.uri` |
| `hover`, `completion`, `signatureHelp`, `definition`, `references`, `prepareRename`, `prepareCallHierarchy` | `uri`, `position`; references optionally accepts `includeDeclaration` |
| `rename` | `uri`, `position`, `newName` |
| `workspaceSymbols` | optional `query`; no document URI is needed |
| `selectionRanges` | `uri`, nonempty `positions` |
| `codeActions` | `uri`, ordered `range`; diagnostics are obtained from the current saved file |
| `incomingCalls`, `outgoingCalls` | task-local `itemId` from a hierarchy result |
Support MUST be checked against the running server's initialize response before
dispatch. Unknown operations, including currently unsupported type hierarchy,
return `UNSUPPORTED_CAPABILITY`. Context publishes the installed provider's
capabilities, including semantic tokens; hierarchy and rename preparations use
their corresponding server capability. Positions are zero-based UTF-16 offsets,
must fit the saved file and must not split surrogate pairs. Reversed ranges and
missing operation-specific parameters are rejected.
The application lifecycle supplies each open project's bound loopback LSP
endpoint. Studio uses OS-assigned ports so different open projects do not compete
for one fixed port. The public LSP configuration default remains unchanged for
other hosts. A manifest language change requires reopening that project's server;
the adapter MUST NOT send queries to a server bound to another language/root.
Each query uses a short-lived client connection. For document requests, the
adapter opens exactly the saved UTF-8 text and waits for that connection's
published diagnostics before issuing the semantic query. Other dependency files
are read from disk by the existing compiler bridge. A fresh connection prevents
reuse of stale overlays from previous requests or the human editor. Unsaved or
concurrent external writes remain the developer's responsibility.
Queries require the current task, share the project's work admission/cancellation
and DEV operation observation, and do not silently queue behind conflicting work.
Return/reclaim invalidates the task immediately, waits for worker termination,
then clears task-local hierarchy items before manual control is restored. Item
IDs from another task or project cannot be used. Client sockets/listeners close
on success, failure, timeout or cancellation; project-server shutdown closes its
accepted sockets and executor. A lost client does not independently grant control.
The adapter bounds connect time to 2 seconds and the LSP exchange to 10 seconds;
Core bounds the request worker wait to 12 seconds. Documents are limited to 4 MiB
of UTF-8 and task-local hierarchy state to 1024 items. These are adapter resource
guards, unrelated to the runtime's deterministic frame/watchdog semantics.
Document URIs must resolve to known physical project/dependency source files.
Symlink targets are canonicalized before admission. External sources may be
queried, but edit-producing queries on read-only roots are rejected. Every URI in
returned workspace-edit proposals is checked against the same editable/read-only
scope; an unsafe proposal is rejected as a whole. File-operation edits and
executable commands are not offered; `workspace/applyEdit` is always refused.
No query writes a file. Virtual SDK declarations may not have physical definition
locations in the existing LSP; indexed SDK references remain available instead.
## 4. Result, Error and Operation Contracts
Successful tools return `isError:false` with structuredContent
@@ -294,8 +356,8 @@ authentication, Origin, endpoint/method/version handling, tool dispatch, malform
or oversized input, closure, restart and old-token rejection. Provider doubles
exist only in tests. Composition starts/closes transport with Studio.
The production registry composes `studio_project_context` and `studio_reference_read`
as of step 18; other
The production registry composes `studio_project_context`, `studio_reference_read`
and `studio_language_query` as of step 19; other
tools are advertised only when their functional handlers are connected.
DEV configuration-copy stays unavailable until step 21. Passing this transport
gate is not the phase 3 or final external-agent acceptance gate. API/Core and
+1
View File
@@ -17,6 +17,7 @@ dependencies {
implementation(project(":prometeu-studio-core"))
implementation(project(":prometeu-studio-ui-javafx"))
implementation(libs.spring.boot)
implementation(libs.lsp4j)
implementation(libs.javafx.controls)
implementation(libs.javafx.fxml)
}
@@ -301,12 +301,25 @@ public class StudioApplicationConfiguration {
return new p.studio.app.compiler.CompilerProjectKnowledge(projects, catalog, mapper);
}
@Bean
p.studio.core.language.LanguageQueryPort automationLanguagePort(LspServerLifecycle lifecycle, ProjectCatalog catalog,
p.studio.api.automation.ProjectKnowledge knowledge, ObjectMapper mapper) {
return new p.studio.app.lsp.AutomationLspQueries(lifecycle, catalog, knowledge, mapper);
}
@Bean
p.studio.api.automation.LanguageQueries languageQueries(p.studio.core.project.ProjectAccessRegistry projects,
p.studio.core.language.LanguageQueryPort language) {
return new p.studio.core.language.TaskLanguageQueries(projects, language);
}
@Bean
p.studio.app.mcp.McpToolRegistry mcpToolRegistry(p.studio.app.mcp.McpToolCatalog catalog, ObjectMapper mapper,
p.studio.api.automation.ProjectKnowledge knowledge) {
// Further providers are connected in PLN-0139 steps 19–20. Never advertise placeholders.
return new p.studio.app.mcp.McpToolRegistry(catalog, mapper,
p.studio.app.mcp.McpProjectKnowledgeTools.handlers(knowledge, mapper));
p.studio.api.automation.ProjectKnowledge knowledge, p.studio.api.automation.LanguageQueries language) {
// Control/build/runtime/operation providers are connected in PLN-0139 step 20.
var handlers = new java.util.LinkedHashMap<>(p.studio.app.mcp.McpProjectKnowledgeTools.handlers(knowledge, mapper));
handlers.putAll(p.studio.app.mcp.McpLanguageTools.handlers(language, mapper));
return new p.studio.app.mcp.McpToolRegistry(catalog, mapper, handlers);
}
@Bean(initMethod = "start", destroyMethod = "close")
@@ -153,6 +153,7 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
}
}
limitations.add("SDK resources are provider-owned, read-only resources, not editable project files");
limitations.add("Virtual SDK symbols may have no physical LSP definition location; consult indexed SDK references");
// Closing a project during discovery must not publish a successful context for that closed project.
openProjects.requireOpenRoot(root);
var descriptors = references.values().stream().map(owned -> {
@@ -232,6 +233,7 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
}
capabilities.add(capability("lsp", true));
capabilities.add(capability("diagnostics", true));
capabilities.add(capability("semanticTokens", true));
capabilities.add(capability("hover", server.hoverSupported()));
capabilities.add(capability("completion", server.completionSupported()));
capabilities.add(capability("signatureHelp", server.signatureHelpSupported()));
@@ -0,0 +1,245 @@
package p.studio.app.lsp;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.launch.LSPLauncher;
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.lsp.api.LspServerLifecycle;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
/** A real client of the already-running project LSP. Each query owns and closes its connection. */
public final class AutomationLspQueries implements LanguageQueryPort {
private final LspServerLifecycle lifecycle;
private final ProjectCatalog projects;
private final ProjectKnowledge knowledge;
private final ObjectMapper mapper;
private final com.google.gson.Gson gson = new MessageJsonHandler(Map.of()).getGson();
private final Map<TaskIdentity, Map<String, CallHierarchyItem>> items = new ConcurrentHashMap<>();
private static final Set<String> OPERATIONS = Set.of("diagnostics", "completion", "hover", "signatureHelp", "definition",
"references", "documentSymbols", "workspaceSymbols", "prepareRename", "rename", "codeActions", "formatting",
"documentLinks", "foldingRanges", "selectionRanges", "prepareCallHierarchy", "incomingCalls", "outgoingCalls", "semanticTokens");
private static final Set<String> EDITS = Set.of("completion", "prepareRename", "rename", "codeActions", "formatting");
public AutomationLspQueries(LspServerLifecycle lifecycle, ProjectCatalog projects, ProjectKnowledge knowledge, ObjectMapper mapper) {
this.lifecycle = lifecycle; this.projects = projects; this.knowledge = knowledge; this.mapper = mapper;
}
@Override public void endTask(TaskIdentity task) { items.remove(task); }
@Override public Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> parameters) 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();
if (primary.capabilities().stream().anyMatch(cap -> cap.name().equals("lsp") && cap.support() != ProjectKnowledge.Support.SUPPORTED))
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 params = mapper.valueToTree(parameters);
CallHierarchyItem item = null;
String uri = null;
if (operation.equals("incomingCalls") || operation.equals("outgoingCalls")) {
item = items.getOrDefault(task, Map.of()).get(params.path("itemId").asText());
if (item == null) throw new IllegalArgumentException("Unknown task-local hierarchy itemId");
uri = item.getUri();
} else if (!operation.equals("workspaceSymbols")) {
uri = params.path("uri").asText(null);
if (uri == null) throw new IllegalArgumentException("uri is required");
}
Path document = uri == null ? null : sourcePath(context, uri);
if (document != null && EDITS.contains(operation)) requireEditable(context, document.toUri().toString());
String source = null;
if (document != null) {
try (var input = Files.newInputStream(document)) {
var bytes = input.readNBytes(4 * 1024 * 1024 + 1);
if (bytes.length > 4 * 1024 * 1024) throw failure(AutomationFailure.Code.LIMIT_EXCEEDED, "Source document exceeds 4 MiB");
source = java.nio.charset.StandardCharsets.UTF_8.newDecoder().decode(java.nio.ByteBuffer.wrap(bytes)).toString();
}
uri = document.toUri().toString();
}
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
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);
var diagnostics = new CompletableFuture<List<Diagnostic>>();
String expectedUri = uri;
LanguageClient client = new LanguageClient() {
public void telemetryEvent(Object event) {}
public void publishDiagnostics(PublishDiagnosticsParams event) { if (Objects.equals(event.getUri(), expectedUri)) diagnostics.complete(List.copyOf(event.getDiagnostics())); }
public void showMessage(MessageParams message) {}
public CompletableFuture<MessageActionItem> showMessageRequest(ShowMessageRequestParams request) { return CompletableFuture.completedFuture(null); }
public void logMessage(MessageParams message) {}
@Override public CompletableFuture<ApplyWorkspaceEditResponse> applyEdit(ApplyWorkspaceEditParams edit) {
return CompletableFuture.completedFuture(new ApplyWorkspaceEditResponse(false));
}
};
var launcher = LSPLauncher.createClientLauncher(client, socket.getInputStream(), socket.getOutputStream(), workers, consumer -> consumer);
var listening = launcher.startListening();
try {
var server = launcher.getRemoteProxy();
var initialize = new InitializeParams();
initialize.setRootUri(binding.projectRoot().toUri().toString());
initialize.setCapabilities(new ClientCapabilities());
var capabilities = await(server.initialize(initialize), deadline).getCapabilities();
server.initialized(new InitializedParams());
requireCapability(operation, capabilities);
List<Diagnostic> currentDiagnostics = List.of();
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);
}
var text = server.getTextDocumentService();
var id = uri == null ? null : new TextDocumentIdentifier(uri);
Object result = switch (operation) {
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 "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);
}
case "formatting" -> await(text.formatting(new DocumentFormattingParams(id, new FormattingOptions(4, true))), deadline);
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);
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);
}
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);
default -> throw new IllegalArgumentException("Unsupported operation");
};
var wire = mapper.readTree(gson.toJson(result));
validateEdits(context, wire);
var limitations = new ArrayList<String>();
if (operation.equals("codeActions") && wire.isArray()) {
for (var action : wire) if (action.has("command")) {
((com.fasterxml.jackson.databind.node.ObjectNode) action).remove("command");
limitations.add("Executable LSP commands are not exposed; only edit proposals are returned");
}
}
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));
output.put("limitations", limitations);
return Collections.unmodifiableMap(output);
} finally { listening.cancel(true); }
} finally { workers.shutdownNow(); }
}
private void addItemIds(TaskIdentity task, com.fasterxml.jackson.databind.JsonNode node) {
if (node.isObject() && node.has("uri") && node.has("selectionRange") && node.has("name") && node.has("kind")) {
var stored = items.computeIfAbsent(task, ignored -> new ConcurrentHashMap<>());
if (stored.size() >= 1024) throw failure(AutomationFailure.Code.LIMIT_EXCEEDED, "Too many hierarchy items for this task");
String key = UUID.randomUUID().toString();
stored.put(key, gson.fromJson(node.toString(), CallHierarchyItem.class));
((com.fasterxml.jackson.databind.node.ObjectNode) node).put("itemId", key);
} else if (node.isContainerNode()) node.forEach(child -> addItemIds(task, child));
}
private static void requireCapability(String operation, ServerCapabilities caps) {
Object capability = switch (operation) {
case "diagnostics" -> caps.getTextDocumentSync();
case "hover" -> caps.getHoverProvider(); case "completion" -> caps.getCompletionProvider();
case "signatureHelp" -> caps.getSignatureHelpProvider(); case "definition" -> caps.getDefinitionProvider();
case "references" -> caps.getReferencesProvider(); case "documentSymbols" -> caps.getDocumentSymbolProvider();
case "workspaceSymbols" -> caps.getWorkspaceSymbolProvider(); case "rename", "prepareRename" -> caps.getRenameProvider();
case "codeActions" -> caps.getCodeActionProvider(); case "formatting" -> caps.getDocumentFormattingProvider();
case "documentLinks" -> caps.getDocumentLinkProvider(); case "foldingRanges" -> caps.getFoldingRangeProvider();
case "selectionRanges" -> caps.getSelectionRangeProvider(); case "prepareCallHierarchy", "incomingCalls", "outgoingCalls" -> caps.getCallHierarchyProvider();
case "semanticTokens" -> caps.getSemanticTokensProvider(); default -> null;
};
if (capability instanceof Either<?, ?> either) capability = either.isLeft() ? either.getLeft() : either.getRight();
if (operation.equals("prepareRename") && (!(capability instanceof RenameOptions options) || !Boolean.TRUE.equals(options.getPrepareProvider())))
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "LSP does not advertise prepareRename");
if (capability == null || Boolean.FALSE.equals(capability)) throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "LSP does not advertise " + operation);
}
private static Position position(com.fasterxml.jackson.databind.JsonNode node, String text) {
if (!node.path("line").isIntegralNumber() || !node.path("character").isIntegralNumber()
|| !node.path("line").canConvertToInt() || !node.path("character").canConvertToInt()) throw new IllegalArgumentException("UTF-16 position is required");
int line = node.path("line").intValue(), character = node.path("character").intValue();
String[] lines = text.split("\\r\\n|\\n|\\r", -1);
if (line < 0 || line >= lines.length || character < 0 || character > lines[line].length()
|| character > 0 && character < lines[line].length() && Character.isHighSurrogate(lines[line].charAt(character - 1)) && Character.isLowSurrogate(lines[line].charAt(character)))
throw new IllegalArgumentException("Position is outside the saved document or splits a UTF-16 pair");
return new Position(line, character);
}
private static Range range(com.fasterxml.jackson.databind.JsonNode node, String text) {
var start = position(node.path("start"), text); var end = position(node.path("end"), text);
if (start.getLine() > end.getLine() || start.getLine() == end.getLine() && start.getCharacter() > end.getCharacter())
throw new IllegalArgumentException("Range end precedes its start");
return new Range(start, end);
}
private static Path sourcePath(ProjectKnowledge.Context context, String uri) throws Exception {
final Path path;
try { path = Path.of(URI.create(uri)).toRealPath(); }
catch (RuntimeException | java.io.IOException invalid) { throw failure(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, "Source URI is not an accessible local project/dependency file"); }
boolean scope = path.startsWith(context.projectRoot()) || context.readOnlyRoots().stream().anyMatch(path::startsWith)
|| context.libraries().stream().flatMap(lib -> lib.roots().stream()).anyMatch(path::startsWith);
boolean source = context.languages().stream().flatMap(lang -> lang.extensions().stream()).anyMatch(extension -> path.getFileName().toString().endsWith("." + extension));
if (!scope || !source || !Files.isRegularFile(path)) throw failure(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, "URI is outside the known source scope");
return path;
}
private static void requireEditable(ProjectKnowledge.Context context, String uri) throws Exception {
Path path = sourcePath(context, uri);
if (!path.startsWith(context.projectRoot()) || context.readOnlyRoots().stream().anyMatch(path::startsWith))
throw failure(AutomationFailure.Code.READ_ONLY_SCOPE, "LSP edit proposal targets a read-only SDK/dependency");
}
private static void validateEdits(ProjectKnowledge.Context context, com.fasterxml.jackson.databind.JsonNode node) throws Exception {
if (node.isObject()) {
if (node.has("changes")) {
var fields = node.path("changes").fieldNames();
while (fields.hasNext()) requireEditable(context, fields.next());
}
if (node.has("textDocument") && node.has("edits")) requireEditable(context, node.path("textDocument").path("uri").asText());
if (node.has("kind") && Set.of("create", "rename", "delete").contains(node.path("kind").asText()))
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "LSP file operations are not exposed");
}
if (node.isContainerNode()) for (var child : node) validateEdits(context, child);
}
private static <T> T await(CompletableFuture<T> future, long deadline) throws Exception {
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) {
int code = response.getResponseError().getCode();
if (code == -32602) throw new IllegalArgumentException("LSP rejected query parameters");
if (code == -32601 || code == -32600)
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, response.getResponseError().getMessage());
}
throw failure;
}
catch (Exception failure) { future.cancel(true); throw failure; }
}
private static AutomationException failure(AutomationFailure.Code code, String message) { return new AutomationException(code, message); }
}
@@ -10,6 +10,7 @@ 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<>();
public LspLifecycleAdapter(LspServerLifecycle lifecycle) {
this.lifecycle = Objects.requireNonNull(lifecycle, "lifecycle");
@@ -17,19 +18,26 @@ public final class LspLifecycleAdapter implements LspLifecyclePort {
@Override
public void boot(ProjectReference project) {
lifecycle.bootServer(LspServerConfiguration.defaults(), context(project));
var binding = context(project);
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);
}
@Override
public void shutdown(ProjectReference project) {
lifecycle.shutdownServer(context(project));
var binding = bindings.remove(project.rootPath().toAbsolutePath().normalize());
if (binding != null) lifecycle.shutdownServer(binding);
}
private static LspProjectContext context(ProjectReference project) {
static LspProjectContext context(ProjectReference project) {
final var safeProject = Objects.requireNonNull(project, "project");
final java.nio.file.Path root;
try { root = safeProject.rootPath().toRealPath(); }
catch (java.io.IOException failure) { throw new IllegalStateException("Project root unavailable", failure); }
return new LspProjectContext(
safeProject.rootPath().toAbsolutePath().normalize().toString(),
root.toString(),
safeProject.languageId(),
safeProject.rootPath());
root);
}
}
@@ -0,0 +1,18 @@
package p.studio.app.mcp;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.studio.api.automation.LanguageQueries;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.Function;
public final class McpLanguageTools {
private McpLanguageTools() {}
public static Map<String, Function<JsonNode, McpToolResult>> handlers(LanguageQueries language, ObjectMapper mapper) {
return Map.of("studio_language_query", arguments -> McpToolResult.success(mapper, mapper.valueToTree(
language.query(Path.of(arguments.path("projectRoot").asText()), arguments.path("taskId").asText(),
arguments.path("operation").asText(), mapper.convertValue(arguments.path("parameters"),
new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>() {})))));
}
}
@@ -573,7 +573,7 @@
},
"exampleValue": {
"operation": "definition",
"locations": [],
"result": [],
"limitations": []
}
},
@@ -30,9 +30,10 @@ class StudioApplicationConfigurationTest {
assertFalse(executor.isShutdown());
assertTrue(mcp.isRunning());
var tools = context.getBean(p.studio.app.mcp.McpToolRegistry.class).descriptors();
org.junit.jupiter.api.Assertions.assertEquals(2, tools.size());
org.junit.jupiter.api.Assertions.assertEquals(3, tools.size());
org.junit.jupiter.api.Assertions.assertEquals("studio_project_context", tools.get(0).path("name").asText());
org.junit.jupiter.api.Assertions.assertEquals("studio_reference_read", tools.get(1).path("name").asText());
org.junit.jupiter.api.Assertions.assertEquals("studio_language_query", tools.get(2).path("name").asText());
}
assertTrue(executor.isShutdown());
assertFalse(mcp.isRunning());
@@ -0,0 +1,210 @@
package p.studio.app.lsp;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import p.studio.api.automation.*;
import p.studio.api.session.ProjectSession;
import p.studio.app.compiler.CompilerProjectKnowledge;
import p.studio.app.operation.ExecutorOperationExecutor;
import p.studio.core.language.TaskLanguageQueries;
import p.studio.core.project.*;
import p.studio.core.session.DefaultProjectSessionFactory;
import p.studio.lsp.services.LspServerLifecycleImpl;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
import static org.junit.jupiter.api.Assertions.*;
class AutomationLspQueriesTest {
@TempDir Path temporary;
final ObjectMapper mapper = new ObjectMapper();
final ExecutorService workers = Executors.newCachedThreadPool();
final LspServerLifecycleImpl lifecycle = new LspServerLifecycleImpl();
ProjectSession session;
TaskLanguageQueries queries;
final ProjectAccessRegistry access = new ProjectAccessRegistry();
Path root, source;
String task;
static final String CODE = "fn helper() -> int { return 1; }\nfn main() -> int { return helper(); }\n";
@BeforeEach void open() throws Exception {
root = Files.createDirectory(temporary.resolve("app")).toRealPath();
source = Files.createDirectories(root.resolve("src")).resolve("main.pbs");
manifest(root, "app", List.of());
Files.writeString(source, CODE);
Files.writeString(source.resolveSibling("mod.barrel"), "pub fn helper() -> int;\npub fn main() -> int;\n");
var setup = new ProjectLocalStudioSetupStore(mapper);
var catalog = new ProjectCatalogService(temporary, mapper, setup);
session = new DefaultProjectSessionFactory(new ProjectLocalStudioStateStore(mapper), setup,
new LspLifecycleAdapter(lifecycle), access, new ExecutorOperationExecutor(workers)).open(catalog.openProject(root));
var knowledge = new CompilerProjectKnowledge(access, catalog, mapper);
queries = new TaskLanguageQueries(access, new AutomationLspQueries(lifecycle, catalog, knowledge, mapper));
}
@AfterEach void close() { if (session != null) session.close(); workers.shutdownNow(); }
void acquire() {
assertTrue(session.automation().grant(false).toCompletableFuture().join().success());
task = session.automation().acquire().task().value();
}
JsonNode query(String operation, Map<String, Object> parameters) {
return mapper.valueToTree(queries.query(root, task, operation, parameters));
}
Map<String, Object> at(int line, int character) {
return Map.of("uri", source.toUri().toString(), "position", Map.of("line", line, "character", character));
}
@Test void diagnosticsAndNavigationSeeSavedChangesOverTheActualServer() throws Exception {
acquire();
assertTrue(query("diagnostics", at(0, 0)).path("result").isArray());
var definition = query("definition", at(1, 26)).path("result");
assertFalse(definition.isEmpty(), definition.toString());
assertEquals(source.toUri().toString(), definition.get(0).path("uri").asText());
Files.writeString(source, "fn main( {");
assertFalse(query("diagnostics", at(0, 0)).path("result").isEmpty());
Files.writeString(source, CODE.replace("helper", "renamed"));
Files.writeString(source.resolveSibling("mod.barrel"), "pub fn renamed() -> int;\npub fn main() -> int;\n");
var symbols = query("documentSymbols", at(0, 0)).path("result");
assertTrue(symbols.toString().contains("renamed"), symbols.toString());
assertFalse(symbols.toString().contains("helper"), symbols.toString());
}
@Test void proposalsDoNotWriteFilesAndQueriesCoverAdvertisedOperations() throws Exception {
acquire();
for (String operation : List.of("hover", "completion", "signatureHelp", "references", "prepareRename", "semanticTokens"))
assertEquals(operation, query(operation, at(1, 26)).path("operation").asText());
for (String operation : List.of("documentLinks", "foldingRanges", "formatting"))
assertEquals(operation, query(operation, at(0, 0)).path("operation").asText());
assertEquals("workspaceSymbols", query("workspaceSymbols", Map.of("query", "helper")).path("operation").asText());
var rename = new HashMap<>(at(1, 26)); rename.put("newName", "renamed");
assertTrue(query("rename", rename).path("result").has("changes"));
assertEquals(CODE, Files.readString(source));
var selections = Map.<String, Object>of("uri", source.toUri().toString(), "positions", List.of(Map.of("line", 0, "character", 4)));
assertEquals("selectionRanges", query("selectionRanges", selections).path("operation").asText());
var actions = Map.<String, Object>of("uri", source.toUri().toString(), "range", Map.of("start", Map.of("line", 0, "character", 0), "end", Map.of("line", 0, "character", 10)));
assertEquals("codeActions", query("codeActions", actions).path("operation").asText());
}
@Test void hierarchyIdsBelongToTheTaskAndDisappearOnReturn() {
acquire();
var prepared = query("prepareCallHierarchy", at(0, 4)).path("result");
assertFalse(prepared.isEmpty(), prepared.toString());
String id = prepared.get(0).path("itemId").asText();
assertFalse(id.isBlank());
assertFalse(query("incomingCalls", Map.of("itemId", id)).path("result").isEmpty());
assertEquals("outgoingCalls", query("outgoingCalls", Map.of("itemId", id)).path("operation").asText());
session.automation().reclaim().toCompletableFuture().join();
acquire();
assertThrows(IllegalArgumentException.class, () -> query("incomingCalls", Map.of("itemId", id)));
}
@Test void admissionUnsupportedOperationsAndSourceScopeAreExplicit() throws Exception {
assertThrows(ControlDeniedException.class, () -> queries.query(root, "ungranted", "diagnostics", at(0, 0)));
acquire();
assertThrows(ControlDeniedException.class, () -> queries.query(root, "wrong-task", "diagnostics", at(0, 0)));
assertEquals(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, assertThrows(AutomationException.class,
() -> query("typeHierarchy", at(0, 0))).failure().code());
Path outside = temporary.resolve("outside.pbs"); Files.writeString(outside, CODE);
assertEquals(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, assertThrows(AutomationException.class,
() -> query("diagnostics", Map.of("uri", outside.toUri().toString()))).failure().code());
assertThrows(IllegalArgumentException.class, () -> query("definition", at(999, 0)));
var context = LspLifecycleAdapter.context(session.projectReference());
var endpoint = lifecycle.endpoint(context).orElseThrow();
session.close(); session = null;
assertTrue(lifecycle.endpoint(context).isEmpty());
assertThrows(java.io.IOException.class, () -> new java.net.Socket(endpoint.host(), endpoint.port()));
assertEquals(AutomationFailure.Code.PROJECT_NOT_OPEN, assertThrows(AutomationException.class,
() -> query("diagnostics", at(0, 0))).failure().code());
}
@Test void mcpDispatchPreservesTaskFailuresAndReturnsNeutralLspResults() {
var tools = new p.studio.app.mcp.McpToolRegistry(new p.studio.app.mcp.McpToolCatalog(mapper), mapper,
p.studio.app.mcp.McpLanguageTools.handlers(queries, mapper));
var arguments = mapper.createObjectNode().put("projectRoot", root.toString()).put("taskId", "missing").put("operation", "diagnostics");
arguments.putObject("parameters").put("uri", source.toUri().toString());
var denied = tools.call("studio_language_query", arguments).wire();
assertEquals("INVALID_TASK", denied.path("structuredContent").path("error").path("code").asText());
acquire(); arguments.put("taskId", task);
var success = tools.call("studio_language_query", arguments).wire();
assertFalse(success.path("isError").asBoolean(), success.toString());
assertTrue(success.path("structuredContent").path("value").path("result").isArray());
session.automation().reclaim().toCompletableFuture().join();
assertEquals("INVALID_TASK", tools.call("studio_language_query", arguments).wire().path("structuredContent").path("error").path("code").asText());
}
@Test void authenticatedHttpToolUsesTheRealLspButDoesNotGrantTaskAuthority() throws Exception {
var tools = new p.studio.app.mcp.McpToolRegistry(new p.studio.app.mcp.McpToolCatalog(mapper), mapper,
p.studio.app.mcp.McpLanguageTools.handlers(queries, mapper));
try (var server = new p.studio.app.mcp.LocalMcpServer(mapper, tools);
var client = java.net.http.HttpClient.newHttpClient()) {
server.start();
var initialize = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", 1).put("method", "initialize");
var setup = initialize.putObject("params").put("protocolVersion", "2025-06-18");
setup.putObject("capabilities"); setup.putObject("clientInfo").put("name", "language-test").put("version", "1");
String connection = post(client, server, null, initialize).headers().firstValue("Mcp-Session-Id").orElseThrow();
post(client, server, connection, mapper.createObjectNode().put("jsonrpc", "2.0").put("method", "notifications/initialized"));
var request = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", 2).put("method", "tools/call");
var args = request.putObject("params").put("name", "studio_language_query").putObject("arguments")
.put("projectRoot", root.toString()).put("taskId", "ungranted").put("operation", "documentSymbols");
args.putObject("parameters").put("uri", source.toUri().toString());
var denied = mapper.readTree(post(client, server, connection, request).body()).path("result");
assertEquals("INVALID_TASK", denied.path("structuredContent").path("error").path("code").asText());
acquire(); args.put("taskId", task);
var result = mapper.readTree(post(client, server, connection, request).body()).path("result");
assertFalse(result.path("isError").asBoolean(), result.toString());
assertTrue(result.path("structuredContent").path("value").path("result").toString().contains("helper"));
}
}
private java.net.http.HttpResponse<String> post(java.net.http.HttpClient client, p.studio.app.mcp.LocalMcpServer server,
String connection, JsonNode body) throws Exception {
var request = java.net.http.HttpRequest.newBuilder(server.connection().endpoint()).timeout(java.time.Duration.ofSeconds(15))
.header("Authorization", server.connection().authorizationHeader()).header("Content-Type", "application/json")
.header("Accept", "application/json, text/event-stream").header("MCP-Protocol-Version", "2025-06-18");
if (connection != null) request.header("Mcp-Session-Id", connection);
return client.send(request.POST(java.net.http.HttpRequest.BodyPublishers.ofString(body.toString())).build(), java.net.http.HttpResponse.BodyHandlers.ofString());
}
@Test void reclaimCancelsTheQueryAndEndsTaskResourcesAfterTheWorkerStops() throws Exception {
var entered = new CountDownLatch(1);
var stopped = new CountDownLatch(1);
var cleaned = new java.util.concurrent.atomic.AtomicBoolean();
var slow = new TaskLanguageQueries(access, new p.studio.core.language.LanguageQueryPort() {
@Override public Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> params) throws Exception {
entered.countDown();
try { new CountDownLatch(1).await(); return Map.of(); }
finally { stopped.countDown(); }
}
@Override public void endTask(TaskIdentity task) { assertEquals(0, stopped.getCount()); cleaned.set(true); }
});
acquire();
var pending = workers.submit(() -> slow.query(root, task, "diagnostics", at(0, 0)));
assertTrue(entered.await(2, TimeUnit.SECONDS));
assertEquals(ControlResult.Status.BUSY, assertThrows(ControlDeniedException.class,
() -> queries.query(root, task, "diagnostics", at(0, 0))).status());
assertTrue(session.automation().reclaim().toCompletableFuture().get(2, TimeUnit.SECONDS).success());
assertThrows(ExecutionException.class, () -> pending.get(2, TimeUnit.SECONDS));
assertTrue(cleaned.get());
assertEquals(ControlState.MANUAL, session.automation().snapshot().state());
}
@Test void externalDependencyNavigationIsReadableButRefactoringIsReadOnly() throws Exception {
Path lib = Files.createDirectories(temporary.resolve("lib")).toRealPath();
manifest(lib, "lib", List.of());
Path module = Files.createDirectories(lib.resolve("src/shared"));
Path declaration = module.resolve("main.pbs");
Files.writeString(declaration, "fn answer() -> int { return 42; }\n");
Files.writeString(module.resolve("mod.barrel"), "pub fn answer() -> int;\n");
manifest(root, "app", List.of(Map.of("path", "../lib")));
Files.createDirectories(root.resolve(".workspace/repos")); Files.writeString(root.resolve(".workspace/repos/registry.json"), "{}");
Files.writeString(source, "import { answer } from @lib:shared;\nfn main() -> int { return answer(); }\n");
Files.writeString(source.resolveSibling("mod.barrel"), "pub fn main() -> int;\n");
acquire();
var locations = query("definition", at(1, 26)).path("result");
assertFalse(locations.isEmpty(), locations.toString());
assertEquals(declaration.toUri().toString(), locations.get(0).path("uri").asText());
assertEquals("documentSymbols", query("documentSymbols", Map.of("uri", declaration.toUri().toString())).path("operation").asText());
assertEquals(AutomationFailure.Code.READ_ONLY_SCOPE, assertThrows(AutomationException.class,
() -> query("formatting", Map.of("uri", declaration.toUri().toString()))).failure().code());
var rename = new HashMap<>(at(1, 26)); rename.put("newName", "renamed");
assertEquals(AutomationFailure.Code.READ_ONLY_SCOPE, assertThrows(AutomationException.class, () -> query("rename", rename)).failure().code());
}
private void manifest(Path path, String name, List<?> dependencies) throws Exception {
mapper.writeValue(path.resolve("prometeu.json").toFile(), Map.of("name", name, "version", "1.0.0", "language", "pbs", "stdlib", "1", "target", "Game", "dependencies", dependencies));
}
}
@@ -13,7 +13,7 @@ class StudioBoundaryArchTest {
static final ArchRule automationTransportStaysInTheApp = noClasses()
.that().resideInAnyPackage("p.studio.api..", "p.studio.core..", "p.studio.ui..")
.should().dependOnClassesThat().resideInAnyPackage(
"p.studio.app.mcp..", "com.sun.net.httpserver..", "io.modelcontextprotocol..")
"p.studio.app.mcp..", "p.studio.app.lsp..", "org.eclipse.lsp4j..", "com.sun.net.httpserver..", "io.modelcontextprotocol..")
.because("DEC-0060 confines MCP and HTTP transport to application adapters");
@ArchTest
@@ -7,4 +7,8 @@ public interface LspServerLifecycle {
void bootServer(LspServerConfiguration serverConfiguration, LspProjectContext context);
void shutdownServer(LspProjectContext context);
default java.util.Optional<p.studio.lsp.messages.LspServerEndpoint> endpoint(LspProjectContext context) {
return java.util.Optional.empty();
}
}
@@ -47,6 +47,12 @@ public final class LspServerLifecycleImpl implements LspServerLifecycle {
closeQuietly(host);
}
@Override
public java.util.Optional<p.studio.lsp.messages.LspServerEndpoint> endpoint(LspProjectContext context) {
var host = activeServers.get(context.projectKey());
return host == null || !host.matches(context) ? java.util.Optional.empty() : host.endpoint();
}
private static void closeQuietly(final TcpLspServerHost host) {
try {
host.close();
@@ -24,6 +24,7 @@ public final class TcpLspServerHost {
private volatile boolean closed;
private volatile ServerSocket serverSocket;
private volatile Thread acceptThread;
private final java.util.Set<Socket> clients = java.util.concurrent.ConcurrentHashMap.newKeySet();
public TcpLspServerHost(
final LspServerConfiguration serverConfiguration,
@@ -59,6 +60,8 @@ public final class TcpLspServerHost {
while (!closed) {
try {
final Socket socket = serverSocket.accept();
clients.add(socket);
if (closed) { clients.remove(socket); socket.close(); break; }
clientExecutor.submit(() -> handleClient(socket));
} catch (IOException exception) {
if (!closed) {
@@ -74,14 +77,21 @@ public final class TcpLspServerHost {
final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
server,
socket.getInputStream(),
socket.getOutputStream());
socket.getOutputStream(), clientExecutor, consumer -> consumer);
server.connect(launcher.getRemoteProxy());
launcher.startListening().get();
} catch (Exception exception) {
throw new IllegalStateException("LSP client failed", exception);
}
if (!closed && !socket.isClosed()) throw new IllegalStateException("LSP client failed", exception);
} finally { clients.remove(socket); }
}
public synchronized java.util.Optional<p.studio.lsp.messages.LspServerEndpoint> endpoint() {
return closed || serverSocket == null ? java.util.Optional.empty() : java.util.Optional.of(
new p.studio.lsp.messages.LspServerEndpoint(serverSocket.getInetAddress().getHostAddress(), serverSocket.getLocalPort()));
}
public boolean matches(LspProjectContext project) { return context.equals(project); }
public synchronized void close() throws Exception {
if (closed) {
return;
@@ -93,6 +103,8 @@ public final class TcpLspServerHost {
if (acceptThread != null) {
acceptThread.interrupt();
}
for (var socket : clients) { try { socket.close(); } catch (IOException ignored) {} }
clients.clear();
clientExecutor.shutdownNow();
}
}
@@ -0,0 +1,37 @@
package p.studio.lsp.services;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.lsp.messages.*;
import java.nio.file.Path;
import java.net.Socket;
import static org.junit.jupiter.api.Assertions.*;
class LspServerLifecycleTest {
@TempDir Path temporary;
@Test void projectEndpointsAreIndependentAndShutdownClosesConnectedSockets() throws Exception {
var lifecycle = new LspServerLifecycleImpl();
var first = new LspProjectContext("first", "pbs", temporary);
var second = new LspProjectContext("second", "pbs", temporary.resolve("second"));
var configuration = LspServerConfiguration.builder().endpoint(new LspServerEndpoint("127.0.0.1", 0)).build();
lifecycle.bootServer(configuration, first); lifecycle.bootServer(configuration, second);
try {
var endpoint = lifecycle.endpoint(first).orElseThrow();
assertNotEquals(endpoint.port(), lifecycle.endpoint(second).orElseThrow().port());
assertTrue(lifecycle.endpoint(new LspProjectContext("first", "other-language", temporary)).isEmpty());
try (var socket = new Socket(endpoint.host(), endpoint.port())) {
socket.setSoTimeout(2000);
String request = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"rootUri\":\"" + temporary.toUri() + "\",\"capabilities\":{}}}";
byte[] body = request.getBytes(java.nio.charset.StandardCharsets.UTF_8);
socket.getOutputStream().write(("Content-Length: " + body.length + "\r\n\r\n").getBytes(java.nio.charset.StandardCharsets.US_ASCII));
socket.getOutputStream().write(body); socket.getOutputStream().flush();
assertNotEquals(-1, socket.getInputStream().read());
lifecycle.shutdownServer(first);
socket.getInputStream().readAllBytes(); // Buffered reply drains, then EOF; no abandoned client connection.
assertTrue(lifecycle.endpoint(first).isEmpty());
assertTrue(lifecycle.endpoint(second).isPresent());
}
} finally { lifecycle.shutdownServer(first); lifecycle.shutdownServer(second); }
}
}
@@ -17,4 +17,6 @@ public interface AutomationSession {
OperationHandle<AutomationOutcome> submit(TaskIdentity task, String summary, Callable<AutomationOutcome> work);
void attachRuntime(TaskIdentity task, ManagedHeadlessSession runtime);
void markCaptureStale();
/** Cleanup runs after workers stop and before returning manual control. */
void onTaskEnd(TaskIdentity task, Runnable cleanup);
}
@@ -0,0 +1,9 @@
package p.studio.api.automation;
import java.nio.file.Path;
import java.util.Map;
/** Neutral language query boundary. Values contain only JSON-compatible maps, lists and scalars. */
public interface LanguageQueries {
Map<String, Object> query(Path projectRoot, String taskId, String operation, Map<String, Object> parameters);
}
@@ -0,0 +1,10 @@
package p.studio.core.language;
import p.studio.api.automation.TaskIdentity;
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;
void endTask(TaskIdentity task);
}
@@ -0,0 +1,55 @@
package p.studio.core.language;
import p.studio.api.automation.*;
import p.studio.core.project.ProjectAccessRegistry;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
/** Shares project admission, cancellation and DEV observation with other agent work. */
public final class TaskLanguageQueries implements LanguageQueries {
private final ProjectAccessRegistry projects;
private final LanguageQueryPort language;
private final Set<TaskIdentity> registered = ConcurrentHashMap.newKeySet();
public TaskLanguageQueries(ProjectAccessRegistry projects, LanguageQueryPort language) {
this.projects = Objects.requireNonNull(projects); this.language = Objects.requireNonNull(language);
}
@Override public Map<String, Object> query(Path root, String taskId, String operation, Map<String, Object> parameters) {
var session = projects.requireAutomation(root);
var task = requireTask(session, taskId);
if (registered.add(task)) {
try { session.onTaskEnd(task, () -> { try { language.endTask(task); } finally { registered.remove(task); } }); }
catch (RuntimeException failure) { registered.remove(task); throw failure; }
}
var result = new AtomicReference<Map<String, Object>>();
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);
});
try {
handle.completion().toCompletableFuture().get(12, TimeUnit.SECONDS);
handle.termination().toCompletableFuture().get(1, TimeUnit.SECONDS);
requireTask(session, taskId);
return Objects.requireNonNull(result.get());
} catch (ExecutionException failure) {
if (failure.getCause() instanceof RuntimeException cause) throw cause;
throw new AutomationException(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "LSP query failed");
} 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");
}
}
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))
throw new ControlDeniedException(ControlResult.Status.INVALID_TASK);
return snapshot.task();
}
}
@@ -14,7 +14,7 @@ import p.studio.api.automation.AutomationFailure;
/** Application-composed routing only; authority is owned by each project session. */
public final class ProjectAccessRegistry implements ProjectAccess {
private record Entry(Supplier<ManualLease> authority, BooleanSupplier available) {}
private record Entry(Supplier<ManualLease> authority, BooleanSupplier available, p.studio.api.automation.AutomationSession automation) {}
private final Map<Path, Entry> projects = new HashMap<>();
public OperationSubscription register(Path root, Supplier<ManualLease> authority) {
@@ -22,12 +22,27 @@ public final class ProjectAccessRegistry implements ProjectAccess {
}
public synchronized OperationSubscription register(Path root, Supplier<ManualLease> authority, BooleanSupplier available) {
return register(root, authority, available, null);
}
public synchronized OperationSubscription register(Path root, Supplier<ManualLease> authority, BooleanSupplier available,
p.studio.api.automation.AutomationSession automation) {
Path key = canonical(root);
var entry = new Entry(java.util.Objects.requireNonNull(authority), java.util.Objects.requireNonNull(available));
var entry = new Entry(java.util.Objects.requireNonNull(authority), java.util.Objects.requireNonNull(available), automation);
if (projects.putIfAbsent(key, entry) != null) throw new IllegalStateException("Project already open: " + key);
return () -> { synchronized (this) { projects.remove(key, entry); } };
}
public p.studio.api.automation.AutomationSession requireAutomation(Path root) {
Path key = requireOpenRoot(root);
synchronized (this) {
var entry = projects.get(key);
if (entry == null || entry.automation() == null)
throw new AutomationException(AutomationFailure.Code.PROJECT_NOT_OPEN, "No open project automation session");
return entry.automation();
}
}
/** Exact root lookup, never focus-based or a descendant-path admission. No grant is needed. */
public Path requireOpenRoot(Path root) {
Path key = canonical(root);
@@ -25,6 +25,12 @@ public final class DefaultAutomationSession implements AutomationSession {
private List<String> diagnostics = List.of();
private CompletableFuture<ControlResult> returning;
private final List<Runnable> manualCancellation = new ArrayList<>();
private final List<Runnable> taskCleanup = new ArrayList<>();
@Override public synchronized void onTaskEnd(TaskIdentity identity, Runnable cleanup) {
requireTask(identity);
taskCleanup.add(Objects.requireNonNull(cleanup));
}
private CompletableFuture<Void> manualIdle = CompletableFuture.completedFuture(null);
public DefaultAutomationSession(Path root, OperationExecutor executor) {
@@ -150,6 +156,7 @@ public final class DefaultAutomationSession implements AutomationSession {
final List<Runnable> cancellations;
final List<DocumentHandoff> participants;
final CompletableFuture<Void> idle;
final List<Runnable> cleanups;
synchronized (this) {
if (requireIdentity && !valid(identity)) return result(ControlResult.Status.INVALID_TASK, "Task identity is not current");
if (closing) closed = true;
@@ -161,6 +168,7 @@ public final class DefaultAutomationSession implements AutomationSession {
operation = active; ownedRuntime = runtime; idle = manualIdle;
cancellations = closing ? List.copyOf(manualCancellation) : List.of();
participants = closing ? List.of() : List.copyOf(documents);
cleanups = List.copyOf(taskCleanup); taskCleanup.clear();
publish();
}
// Never call another service while holding the authority monitor: Debug owns its own lock.
@@ -172,6 +180,7 @@ public final class DefaultAutomationSession implements AutomationSession {
CompletionStage<Void> shutdown = CompletableFuture.completedFuture(null);
if (ownedRuntime != null) { ownedRuntime.cancel(); shutdown = ownedRuntime.shutdown(); }
cleanup = CompletableFuture.allOf(stopped.toCompletableFuture(), shutdown.toCompletableFuture(), idle);
cleanup = cleanup.thenRun(() -> cleanups.forEach(Runnable::run));
// Closing disposes documents; an explicit return must refresh them before unlocking.
for (var p : participants) cleanup = cleanup.thenCompose(v -> {
synchronized (this) { if (closed) return CompletableFuture.completedFuture(null); }
@@ -34,7 +34,7 @@ public final class DefaultProjectSessionFactory implements ProjectSessionFactory
var automation = new DefaultAutomationSession(project.rootPath(), operations);
var ready = new java.util.concurrent.atomic.AtomicBoolean();
var registration = access.register(project.rootPath(), automation::enterManual,
() -> ready.get() && !automation.snapshot().closed());
() -> ready.get() && !automation.snapshot().closed(), automation);
try { lsp.boot(project); }
catch (RuntimeException failure) { registration.unsubscribe(); throw failure; }
ready.set(true);
@@ -15,7 +15,7 @@ class StudioModularityTest {
void verifiesStudioCoreApplicationModules() {
modules.verify();
assertEquals(
Set.of("project", "session", "assets", "shipper", "runtime", "execution"),
Set.of("project", "session", "assets", "shipper", "runtime", "execution", "language"),
modules.stream().map(module -> module.getIdentifier().toString()).collect(java.util.stream.Collectors.toSet()));
}
@@ -18,6 +18,23 @@ class DefaultAutomationSessionTest {
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 taskCleanupRunsAfterWorkerTerminationAndBeforeManualControl() {
var s = session(); var task = start(s);
var cleaned = new java.util.concurrent.atomic.AtomicInteger();
s.onTaskEnd(task, () -> {
assertTrue(operations.last.stopped.isDone());
assertFalse(s.snapshot().manualAllowed());
cleaned.incrementAndGet();
});
s.submit(task, "language query", () -> new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, List.of(), null));
var returning = s.reclaim().toCompletableFuture();
assertTrue(operations.last.cancelled); assertEquals(0, cleaned.get()); assertFalse(returning.isDone());
operations.last.finish();
assertEquals(RETURNED, returning.join().status()); assertEquals(1, cleaned.get());
s.closeAsync().toCompletableFuture().join(); assertEquals(1, cleaned.get());
assertThrows(ControlDeniedException.class, () -> s.onTaskEnd(task, () -> {}));
}
@Test void explicitGrantAndProjectScopedIdentityAreRequired() {
var s = session();
assertEquals(NOT_GRANTED, s.acquire().status());