feat(pbs): resolve editorial chains and expose read-only external sources
This commit is contained in:
@@ -10,6 +10,8 @@ import p.studio.api.automation.*;
|
||||
import p.studio.api.project.ProjectCatalog;
|
||||
import p.studio.core.language.LanguageQueryPort;
|
||||
import p.studio.lsp.api.LspServerLifecycle;
|
||||
import p.studio.lsp.services.protocol.ExternalSourceLanguageServer;
|
||||
import org.eclipse.lsp4j.jsonrpc.Launcher;
|
||||
|
||||
import java.net.*;
|
||||
import java.nio.file.*;
|
||||
@@ -24,7 +26,7 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
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",
|
||||
private static final Set<String> OPERATIONS = Set.of("readExternalSource", "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");
|
||||
@@ -33,7 +35,47 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
this.lifecycle = lifecycle; this.projects = projects; this.knowledge = knowledge; this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override public void endTask(TaskIdentity task) { items.remove(task); }
|
||||
private record ExternalTask(String consumer, p.studio.lsp.messages.LspServerEndpoint endpoint,
|
||||
p.studio.lsp.messages.LspProjectContext binding, Set<String> uris) {}
|
||||
private final Map<TaskIdentity, ExternalTask> externalTasks = new ConcurrentHashMap<>();
|
||||
|
||||
@Override public void endTask(TaskIdentity task) {
|
||||
items.remove(task);
|
||||
final var state = externalTasks.remove(task);
|
||||
if (state == null) return;
|
||||
final var workers = Executors.newCachedThreadPool();
|
||||
try (var socket = new Socket()) {
|
||||
socket.connect(new InetSocketAddress(state.endpoint().host(), state.endpoint().port()), 2000);
|
||||
final var client = new QuietClient();
|
||||
final var launcher = new Launcher.Builder<ExternalSourceLanguageServer>().setLocalService(client)
|
||||
.setRemoteInterface(ExternalSourceLanguageServer.class).setInput(socket.getInputStream())
|
||||
.setOutput(socket.getOutputStream()).setExecutorService(workers).create();
|
||||
final var listening = launcher.startListening();
|
||||
try {
|
||||
final var server = launcher.getRemoteProxy();
|
||||
final var init = new InitializeParams();
|
||||
init.setRootUri(state.binding().projectRoot().toUri().toString());
|
||||
init.setCapabilities(new ClientCapabilities());
|
||||
init.getCapabilities().setExperimental(Map.of("prometeuExternalSources", true));
|
||||
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);
|
||||
} 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(); }
|
||||
}
|
||||
|
||||
private static class QuietClient implements LanguageClient {
|
||||
public void telemetryEvent(Object event) {}
|
||||
public void publishDiagnostics(PublishDiagnosticsParams event) {}
|
||||
public void showMessage(MessageParams message) {}
|
||||
public CompletableFuture<MessageActionItem> showMessageRequest(ShowMessageRequestParams request) { return CompletableFuture.completedFuture(null); }
|
||||
public void logMessage(MessageParams message) {}
|
||||
}
|
||||
|
||||
@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);
|
||||
@@ -55,7 +97,15 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
uri = params.path("uri").asText(null);
|
||||
if (uri == null) throw new IllegalArgumentException("uri is required");
|
||||
}
|
||||
Path document = uri == null ? null : sourcePath(context, uri);
|
||||
final boolean external = uri != null && uri.startsWith("prometeu-source:");
|
||||
if (external) {
|
||||
final var state = externalTasks.get(task);
|
||||
if (state == null || !state.uris().contains(uri)) throw failure(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, "Unknown task-owned external source");
|
||||
if (EDITS.contains(operation)) throw failure(AutomationFailure.Code.READ_ONLY_SCOPE, "External sources are read-only");
|
||||
if (!Set.of("readExternalSource", "hover", "definition").contains(operation))
|
||||
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "Operation is unavailable for external documents");
|
||||
} else if (operation.equals("readExternalSource")) throw new IllegalArgumentException("An external source URI is required");
|
||||
Path document = uri == null || external ? null : sourcePath(context, uri);
|
||||
if (document != null && EDITS.contains(operation)) requireEditable(context, document.toUri().toString());
|
||||
String source = null;
|
||||
if (document != null) {
|
||||
@@ -83,14 +133,28 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
return CompletableFuture.completedFuture(new ApplyWorkspaceEditResponse(false));
|
||||
}
|
||||
};
|
||||
var launcher = LSPLauncher.createClientLauncher(client, socket.getInputStream(), socket.getOutputStream(), workers, consumer -> consumer);
|
||||
var launcher = new Launcher.Builder<ExternalSourceLanguageServer>().setLocalService(client)
|
||||
.setRemoteInterface(ExternalSourceLanguageServer.class).setInput(socket.getInputStream())
|
||||
.setOutput(socket.getOutputStream()).setExecutorService(workers).create();
|
||||
var listening = launcher.startListening();
|
||||
final var server = launcher.getRemoteProxy();
|
||||
final var acquired = new HashSet<String>();
|
||||
boolean succeeded = false;
|
||||
try {
|
||||
var server = launcher.getRemoteProxy();
|
||||
var initialize = new InitializeParams();
|
||||
initialize.setRootUri(binding.projectRoot().toUri().toString());
|
||||
initialize.setCapabilities(new ClientCapabilities());
|
||||
initialize.getCapabilities().setExperimental(Map.of("prometeuExternalSources", true));
|
||||
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();
|
||||
final var experimental = gson.toJsonTree(capabilities.getExperimental());
|
||||
if (experimental.isJsonObject() && experimental.getAsJsonObject().has("prometeuExternalConsumerId")) {
|
||||
final String consumer = experimental.getAsJsonObject().get("prometeuExternalConsumerId").getAsString();
|
||||
externalTasks.putIfAbsent(task, new ExternalTask(consumer, endpoint, binding, ConcurrentHashMap.newKeySet()));
|
||||
}
|
||||
server.initialized(new InitializedParams());
|
||||
requireCapability(operation, capabilities);
|
||||
List<Diagnostic> currentDiagnostics = List.of();
|
||||
@@ -99,9 +163,17 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(new TextDocumentItem(uri, primary.id(), 1, source)));
|
||||
currentDiagnostics = await(diagnostics, deadline);
|
||||
}
|
||||
if (external) {
|
||||
final var request = new ExternalSourceLanguageServer.Request(externalTasks.get(task).consumer(), uri);
|
||||
source = await(server.revalidateExternalSource(request), deadline).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");
|
||||
}
|
||||
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);
|
||||
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);
|
||||
@@ -136,6 +208,17 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
};
|
||||
var wire = mapper.readTree(gson.toJson(result));
|
||||
validateEdits(context, wire);
|
||||
if (operation.equals("definition") && externalTasks.containsKey(task)) {
|
||||
final var returned = new HashSet<String>();
|
||||
rememberExternalUris(returned, wire);
|
||||
acquired.addAll(returned);
|
||||
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).uris().add(externalUri);
|
||||
}
|
||||
}
|
||||
var limitations = new ArrayList<String>();
|
||||
if (operation.equals("codeActions") && wire.isArray()) {
|
||||
for (var action : wire) if (action.has("command")) {
|
||||
@@ -147,11 +230,30 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
var output = new LinkedHashMap<String, Object>();
|
||||
output.put("operation", operation); output.put("result", mapper.convertValue(wire, Object.class));
|
||||
output.put("limitations", limitations);
|
||||
succeeded = true;
|
||||
return Collections.unmodifiableMap(output);
|
||||
} finally { listening.cancel(true); }
|
||||
} finally {
|
||||
if (!succeeded && !acquired.isEmpty() && externalTasks.containsKey(task)) {
|
||||
final var state = externalTasks.get(task);
|
||||
final boolean interrupted = Thread.interrupted();
|
||||
try {
|
||||
for (String acquiredUri : acquired) {
|
||||
state.uris().remove(acquiredUri);
|
||||
server.releaseExternalSource(new ExternalSourceLanguageServer.Request(state.consumer(), acquiredUri))
|
||||
.get(500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
} finally { if (interrupted) Thread.currentThread().interrupt(); }
|
||||
}
|
||||
listening.cancel(true);
|
||||
}
|
||||
} finally { workers.shutdownNow(); }
|
||||
}
|
||||
|
||||
private void rememberExternalUris(Set<String> uris, com.fasterxml.jackson.databind.JsonNode node) {
|
||||
if (node.isObject() && node.path("uri").asText().startsWith("prometeu-source:")) uris.add(node.path("uri").asText());
|
||||
if (node.isContainerNode()) node.forEach(child -> rememberExternalUris(uris, child));
|
||||
}
|
||||
|
||||
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<>());
|
||||
@@ -163,6 +265,12 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
}
|
||||
|
||||
private static void requireCapability(String operation, ServerCapabilities caps) {
|
||||
if (operation.equals("readExternalSource")) {
|
||||
final var raw = new com.google.gson.Gson().toJsonTree(caps.getExperimental());
|
||||
if (!raw.isJsonObject() || !raw.getAsJsonObject().has("prometeuExternalSources"))
|
||||
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "LSP does not advertise external sources");
|
||||
return;
|
||||
}
|
||||
Object capability = switch (operation) {
|
||||
case "diagnostics" -> caps.getTextDocumentSync();
|
||||
case "hover" -> caps.getHoverProvider(); case "completion" -> caps.getCompletionProvider();
|
||||
@@ -233,6 +341,7 @@ public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
catch (ExecutionException failure) {
|
||||
if (failure.getCause() instanceof org.eclipse.lsp4j.jsonrpc.ResponseErrorException response) {
|
||||
int code = response.getResponseError().getCode();
|
||||
if (code == -32010) throw failure(AutomationFailure.Code.READ_ONLY_SCOPE, response.getResponseError().getMessage());
|
||||
if (code == -32602) throw new IllegalArgumentException("LSP rejected query parameters");
|
||||
if (code == -32601 || code == -32600)
|
||||
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, response.getResponseError().getMessage());
|
||||
|
||||
Reference in New Issue
Block a user