expose manifest-driven project knowledge through MCP

This commit is contained in:
2026-09-29 08:30:38 +01:00
parent 9f61f9c07d
commit 58f11a8123
14 changed files with 665 additions and 13 deletions
@@ -8,15 +8,39 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.function.BooleanSupplier;
import p.studio.api.automation.AutomationException;
import p.studio.api.automation.AutomationFailure;
/** Application-composed routing only; authority is owned by each project session. */
public final class ProjectAccessRegistry implements ProjectAccess {
private final Map<Path, Supplier<ManualLease>> projects = new HashMap<>();
private record Entry(Supplier<ManualLease> authority, BooleanSupplier available) {}
private final Map<Path, Entry> projects = new HashMap<>();
public synchronized OperationSubscription register(Path root, Supplier<ManualLease> authority) {
public OperationSubscription register(Path root, Supplier<ManualLease> authority) {
return register(root, authority, () -> false);
}
public synchronized OperationSubscription register(Path root, Supplier<ManualLease> authority, BooleanSupplier available) {
Path key = canonical(root);
if (projects.putIfAbsent(key, authority) != null) throw new IllegalStateException("Project already open: " + key);
return () -> { synchronized (this) { projects.remove(key, authority); } };
var entry = new Entry(java.util.Objects.requireNonNull(authority), java.util.Objects.requireNonNull(available));
if (projects.putIfAbsent(key, entry) != null) throw new IllegalStateException("Project already open: " + key);
return () -> { synchronized (this) { projects.remove(key, entry); } };
}
/** Exact root lookup, never focus-based or a descendant-path admission. No grant is needed. */
public Path requireOpenRoot(Path root) {
Path key = canonical(root);
Entry entry;
synchronized (this) { entry = projects.get(key); }
// Never call session code while holding the routing lock.
if (!root.isAbsolute() || entry == null || !entry.available().getAsBoolean())
throw new AutomationException(AutomationFailure.Code.PROJECT_NOT_OPEN, "The explicit project root is not open");
synchronized (this) {
if (projects.get(key) != entry)
throw new AutomationException(AutomationFailure.Code.PROJECT_NOT_OPEN, "The project was closed");
}
return key;
}
@Override public ManualLease enterManual(Path path) {
@@ -25,7 +49,7 @@ public final class ProjectAccessRegistry implements ProjectAccess {
Path key = canonical(path);
authority = projects.entrySet().stream().filter(e -> key.startsWith(e.getKey()))
.max(java.util.Comparator.comparingInt(e -> e.getKey().getNameCount()))
.map(Map.Entry::getValue).orElse(null);
.map(e -> e.getValue().authority()).orElse(null);
}
// Project creation/setup before opening a session is still a manual workflow.
return authority == null ? () -> {} : authority.get();
@@ -32,9 +32,12 @@ public final class DefaultProjectSessionFactory implements ProjectSessionFactory
final var setup = setupStore.load(project);
final var state = stateStore.load(project);
var automation = new DefaultAutomationSession(project.rootPath(), operations);
var registration = access.register(project.rootPath(), automation::enterManual);
var ready = new java.util.concurrent.atomic.AtomicBoolean();
var registration = access.register(project.rootPath(), automation::enterManual,
() -> ready.get() && !automation.snapshot().closed());
try { lsp.boot(project); }
catch (RuntimeException failure) { registration.unsubscribe(); throw failure; }
ready.set(true);
return new DefaultProjectSession(project, stateStore, setup, new DefaultExecutionSession(), lsp, state, automation, registration);
}
}
@@ -0,0 +1,47 @@
package p.studio.core.project;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.api.automation.AutomationException;
import p.studio.api.automation.AutomationFailure;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.*;
class ProjectAccessRegistryTest {
@TempDir Path root;
@Test void exactOpenRootsIgnoreFocusAndDoNotAcquireManualAuthority() throws Exception {
var registry = new ProjectAccessRegistry();
Path first = Files.createDirectory(root.resolve("first")).toRealPath();
Path second = Files.createDirectory(root.resolve("second")).toRealPath();
var available = new AtomicBoolean(true);
var registration = registry.register(first, () -> { throw new AssertionError("Context does not acquire control"); }, available::get);
registry.register(second, () -> () -> {}, () -> true);
assertEquals(first, registry.requireOpenRoot(first));
assertEquals(second, registry.requireOpenRoot(second));
assertDenied(registry, first.resolve("src"));
assertDenied(registry, Path.of("relative"));
var alias = Files.createSymbolicLink(root.resolve("alias"), first);
assertEquals(first, registry.requireOpenRoot(alias));
available.set(false);
assertDenied(registry, first);
available.set(true);
registration.unsubscribe();
assertDenied(registry, first);
assertEquals(second, registry.requireOpenRoot(second));
}
@Test void guardOnlyRegistrationIsNotAnOpenProject() {
var registry = new ProjectAccessRegistry();
registry.register(root, () -> () -> {});
assertDenied(registry, root);
assertNotNull(registry.enterManual(root.resolve("src")));
}
private void assertDenied(ProjectAccessRegistry registry, Path path) {
assertEquals(AutomationFailure.Code.PROJECT_NOT_OPEN,
assertThrows(AutomationException.class, () -> registry.requireOpenRoot(path)).failure().code());
}
}
@@ -17,21 +17,24 @@ final class DefaultProjectSessionTest {
@TempDir Path temporaryDirectory;
@Test
void factoryLoadsStateBootsLspAndCloseIsOrderedAndIdempotent() {
void factoryLoadsStateBootsLspAndCloseIsOrderedAndIdempotent() throws Exception {
final var mapper = new ObjectMapper();
final var stateStore = new ProjectLocalStudioStateStore(mapper);
final var setupStore = new ProjectLocalStudioSetupStore(mapper);
final var lsp = new RecordingLsp();
final var access = new p.studio.core.project.ProjectAccessRegistry();
final var factory = new DefaultProjectSessionFactory(stateStore, setupStore, lsp,
new p.studio.core.project.ProjectAccessRegistry(), new p.studio.core.support.ControlledOperations());
access, new p.studio.core.support.ControlledOperations());
final var project = project();
final var session = factory.open(project);
assertEquals(project.rootPath().toRealPath(), access.requireOpenRoot(project.rootPath()));
final var next = ProjectLocalStudioState.defaults()
.withOpenShellState(new ProjectLocalStudioState.OpenShellState("DEBUG"));
session.replaceProjectLocalStudioState(next);
session.close();
session.close();
assertThrows(p.studio.api.automation.AutomationException.class, () -> access.requireOpenRoot(project.rootPath()));
assertEquals(1, lsp.boots);
assertEquals(1, lsp.shutdowns);
@@ -55,6 +58,25 @@ final class DefaultProjectSessionTest {
return new ProjectReference("Example", "1.0.0", "pbs", 1, ProjectTarget.Game, temporaryDirectory);
}
@Test
void failedBootNeverPublishesAnOpenProjectAndRemovesItsRegistration() {
var mapper = new ObjectMapper();
var access = new p.studio.core.project.ProjectAccessRegistry();
var lsp = new LspLifecyclePort() {
@Override public void boot(ProjectReference project) {
assertThrows(p.studio.api.automation.AutomationException.class, () -> access.requireOpenRoot(project.rootPath()));
throw new IllegalStateException("boot failed");
}
@Override public void shutdown(ProjectReference project) {}
};
var factory = new DefaultProjectSessionFactory(new ProjectLocalStudioStateStore(mapper),
new ProjectLocalStudioSetupStore(mapper), lsp, access, new p.studio.core.support.ControlledOperations());
assertThrows(IllegalStateException.class, () -> factory.open(project()));
assertThrows(p.studio.api.automation.AutomationException.class, () -> access.requireOpenRoot(project().rootPath()));
var registration = access.register(project().rootPath(), () -> () -> {}, () -> true);
registration.unsubscribe();
}
private static final class RecordingLsp implements LspLifecyclePort {
int boots;
int shutdowns;