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());
|
||||
|
||||
@@ -367,7 +367,7 @@
|
||||
{
|
||||
"name": "studio_language_query",
|
||||
"authority": "task",
|
||||
"description": "Query supported project LSP capabilities after synchronizing saved files. 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.",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@@ -54,6 +54,36 @@ class AutomationLspQueriesTest {
|
||||
Map<String, Object> at(int line, int character) {
|
||||
return Map.of("uri", source.toUri().toString(), "position", Map.of("line", line, "character", character));
|
||||
}
|
||||
@Test void bundledSourcesAreReadableAcrossConnectionsAndOwnedByTheTask() throws Exception {
|
||||
Files.writeString(source, "import { Gfx } from @sdk:gfx;\nfn main() -> void { Gfx.clear(0); }\n");
|
||||
Files.writeString(source.resolveSibling("mod.barrel"), "pub fn main() -> void;\n");
|
||||
acquire();
|
||||
final var definition = query("definition", at(1, 24)).path("result");
|
||||
assertFalse(definition.isEmpty(), definition.toString());
|
||||
final String uri = definition.get(0).path("uri").asText();
|
||||
assertTrue(uri.startsWith("prometeu-source:"), uri);
|
||||
final var content = query("readExternalSource", Map.of("uri", uri)).path("result");
|
||||
assertTrue(content.path("descriptor").path("readOnly").asBoolean());
|
||||
assertEquals("bundled", content.path("descriptor").path("origin").asText());
|
||||
final String text = content.path("text").asText();
|
||||
final int offset = text.indexOf("LowGfx.clear") + "LowGfx.".length();
|
||||
assertTrue(offset > "LowGfx.".length());
|
||||
final String prefix = text.substring(0, offset);
|
||||
final var position = Map.of("line", (int) prefix.chars().filter(c -> c == '\n').count(),
|
||||
"character", offset - prefix.lastIndexOf('\n') - 1);
|
||||
final var request = Map.<String, Object>of("uri", uri, "position", position);
|
||||
assertTrue(query("hover", request).path("result").toString().contains("clear"));
|
||||
final var host = query("definition", request).path("result");
|
||||
assertFalse(host.isEmpty());
|
||||
assertTrue(query("readExternalSource", Map.of("uri", host.get(0).path("uri").asText()))
|
||||
.path("result").path("text").asText().contains("declare host LowGfx"));
|
||||
assertEquals(AutomationFailure.Code.READ_ONLY_SCOPE, assertThrows(AutomationException.class,
|
||||
() -> query("formatting", request)).failure().code());
|
||||
session.automation().reclaim().toCompletableFuture().join();
|
||||
acquire();
|
||||
assertEquals(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, assertThrows(AutomationException.class,
|
||||
() -> query("readExternalSource", Map.of("uri", uri))).failure().code());
|
||||
}
|
||||
@Test void diagnosticsAndNavigationSeeSavedChangesOverTheActualServer() throws Exception {
|
||||
acquire();
|
||||
assertTrue(query("diagnostics", at(0, 0)).path("result").isArray());
|
||||
@@ -186,26 +216,43 @@ class AutomationLspQueriesTest {
|
||||
assertTrue(cleaned.get());
|
||||
assertEquals(ControlState.MANUAL, session.automation().snapshot().state());
|
||||
}
|
||||
@Test void externalDependencyNavigationIsReadableButRefactoringIsReadOnly() throws Exception {
|
||||
Path lib = Files.createDirectories(temporary.resolve("lib")).toRealPath();
|
||||
@org.junit.jupiter.params.ParameterizedTest
|
||||
@org.junit.jupiter.params.provider.ValueSource(booleans = {false, true})
|
||||
void externalDependencyNavigationIsReadableButRefactoringIsReadOnly(boolean inWorkspace) throws Exception {
|
||||
Path lib = Files.createDirectories(inWorkspace ? root.resolve(".workspace/repos/lib") : 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(declaration, "// á 😀\r\nfn answer() -> int { return helper(); }\r\nfn helper() -> int { return 42; }\r\n");
|
||||
Files.writeString(module.resolve("mod.barrel"), "pub fn answer() -> int;\n");
|
||||
manifest(root, "app", List.of(Map.of("path", "../lib")));
|
||||
manifest(root, "app", List.of(Map.of("path", inWorkspace ? ".workspace/repos/lib" : "../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());
|
||||
assertTrue(locations.get(0).path("uri").asText().startsWith("prometeu-source:"));
|
||||
assertEquals(Files.readString(declaration), query("readExternalSource", Map.of("uri", locations.get(0).path("uri").asText())).path("result").path("text").asText());
|
||||
final String externalUri = locations.get(0).path("uri").asText();
|
||||
assertEquals(1, locations.get(0).path("range").path("start").path("line").asInt());
|
||||
final var nested = query("definition", Map.of("uri", externalUri, "position", Map.of("line", 1, "character", 28))).path("result");
|
||||
assertFalse(nested.isEmpty(), nested.toString());
|
||||
assertEquals(2, nested.get(0).path("range").path("start").path("line").asInt());
|
||||
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());
|
||||
final Path alias = Files.createSymbolicLink(root.resolve("src/dependency-alias.pbs"), declaration);
|
||||
assertEquals(AutomationFailure.Code.READ_ONLY_SCOPE, assertThrows(AutomationException.class,
|
||||
() -> query("formatting", Map.of("uri", alias.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());
|
||||
final String oldText = Files.readString(declaration);
|
||||
Files.writeString(declaration, "// changed revision\n" + oldText);
|
||||
assertThrows(AutomationException.class, () -> query("readExternalSource", Map.of("uri", externalUri)));
|
||||
final var renewed = query("definition", at(1, 26)).path("result");
|
||||
assertNotEquals(externalUri, renewed.get(0).path("uri").asText());
|
||||
assertEquals(2, renewed.get(0).path("range").path("start").path("line").asInt());
|
||||
}
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user