feat(pbs): support quoted project imports and installed authoring contracts
This commit is contained in:
@@ -12,11 +12,17 @@ import p.studio.utilities.logs.LogAggregator;
|
||||
/** Runs only with installed product JARs, outside the checkout. No runtime checkout is read. */
|
||||
public final class AnimationInstalledProbe {
|
||||
public static void verify(Path root,Path corpus)throws Exception{
|
||||
verify(root,corpus,"app");
|
||||
}
|
||||
public static void verify(Path root,Path corpus,String projectName)throws Exception{
|
||||
var f=new AnimationProjectFixture();f.root=root;f.setup();
|
||||
try{
|
||||
var manifest=f.mapper.readTree(root.resolve("prometeu.json").toFile());
|
||||
((com.fasterxml.jackson.databind.node.ObjectNode)manifest).put("name",projectName);
|
||||
f.mapper.writeValue(root.resolve("prometeu.json").toFile(),manifest);
|
||||
f.manual.execute(AnimationService.Request.save(f.project,f.uuid,null,f.document()));
|
||||
Files.writeString(root.resolve("src/mod.barrel"),"pub fn frame()->void;pub fn animationProbe()->int;");
|
||||
Files.writeString(root.resolve("src/main.pbs"),"import { create_hero } from @app:generated/animations/b_"+f.uuid.toString().replace("-","")+";\n"+"""
|
||||
Files.writeString(root.resolve("src/main.pbs"),"import { create_hero } from @"+f.mapper.writeValueAsString(projectName)+":generated/animations/b_"+f.uuid.toString().replace("-","")+";\n"+"""
|
||||
import { AnimationError } from @core:animation;
|
||||
declare global trace:int=0;
|
||||
fn run()->result<AnimationError> {
|
||||
@@ -35,11 +41,11 @@ public final class AnimationInstalledProbe {
|
||||
for(boolean optimized:new boolean[]{false,true}){
|
||||
var program=optimized?context.optimizedIrvm:context.irvm;
|
||||
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
|
||||
String prefix="pbs-animation-installed"+(optimized?"-optimized":"");Files.write(corpus.resolve(prefix+".pbx"),module.serialize());
|
||||
String prefix="pbs-animation-installed"+(projectName.equals("app")?"":"-hyphenated")+(optimized?"-optimized":"");Files.write(corpus.resolve(prefix+".pbx"),module.serialize());
|
||||
for(int i=0;i<program.module().functions().size();i++)if(program.module().functions().get(i).name().contains("animationProbe"))Files.writeString(corpus.resolve(prefix+".probe"),Integer.toString(i));
|
||||
}
|
||||
var bridge=new p.studio.lsp.services.compiler.CompilerLanguageServiceBridge(f.layouts);
|
||||
var lsp=new p.studio.lsp.messages.LspProjectContext("app","pbs",root);
|
||||
var lsp=new p.studio.lsp.messages.LspProjectContext(projectName,"pbs",root);
|
||||
for(var id:context.fileTable.identifiers()){
|
||||
var path=context.fileTable.get(id).getCanonPath();if(path.startsWith(Path.of("/virtual/generated/animations"))&&path.endsWith("main.pbs")){
|
||||
var source=bridge.currentSource(lsp,path).orElseThrow();
|
||||
@@ -50,6 +56,7 @@ public final class AnimationInstalledProbe {
|
||||
if(schema==null||!schema.getProtocol().equals("jar"))throw new AssertionError("Installed MCP schema missing");
|
||||
try(var input=schema.openStream()){String text=new String(input.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8);for(var name:new String[]{"studio_animation_query","studio_animation_update"})if(!text.contains(name))throw new AssertionError(name);}
|
||||
System.out.println("Installed generated animation verified: shared save, factories, read-only origins, MCP schemas, ordinary/optimized corpus");
|
||||
System.out.println("Installed generated animation project: "+projectName);
|
||||
}finally{f.close();}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +131,9 @@ public final class InstalledKnowledgeProbe {
|
||||
Files.write(fixtures.resolve("pbs-"+example.getKey()+"-installed.pbx"),exampleResult.bytecodeBytes());
|
||||
System.out.println("Installed "+example.getKey()+" execution fixture generated");
|
||||
}
|
||||
ProductionContractsInstalledProbe.verify(root, fixtures.resolve("production-contracts"));
|
||||
p.studio.app.animation.AnimationInstalledProbe.verify(Files.createDirectory(Path.of("animation-authored")).toRealPath(),fixtures);
|
||||
p.studio.app.animation.AnimationInstalledProbe.verify(Files.createDirectory(Path.of("animation-hyphenated")).toRealPath(),fixtures,"zelda-1");
|
||||
verifyExternalSources(provider.getClass().getClassLoader());
|
||||
registration.unsubscribe();
|
||||
System.out.println("Installed knowledge verified: " + docs.size() + " references; example compiled");
|
||||
|
||||
@@ -23,6 +23,8 @@ class InstalledKnowledgeTest {
|
||||
assertTrue(log.contains("Installed memcard execution fixture generated"), log);
|
||||
assertTrue(log.contains("Installed animation execution fixture generated"), log);
|
||||
assertTrue(log.contains("Installed generated animation verified"), log);
|
||||
assertTrue(log.contains("Installed production contracts verified: 21 compile classifications"), log);
|
||||
assertTrue(log.contains("Installed generated animation project: zelda-1"), log);
|
||||
}
|
||||
|
||||
@Test void installedCompilerAndSdkCarryTheSameVerifiedCatalog() throws Exception {
|
||||
@@ -46,7 +48,7 @@ class InstalledKnowledgeTest {
|
||||
var process = new ProcessBuilder(command)
|
||||
.directory(outsideRepository.toFile()).redirectErrorStream(true).redirectOutput(output.toFile()).start();
|
||||
try {
|
||||
assertTrue(process.waitFor(45, TimeUnit.SECONDS), "Installed knowledge probe timed out");
|
||||
assertTrue(process.waitFor(150, TimeUnit.SECONDS), "Installed knowledge probe timed out");
|
||||
String log = Files.readString(output);
|
||||
assertEquals(0, process.exitValue(), log);
|
||||
return log;
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package p.studio.app.compiler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.compiler.messages.BuilderPipelineConfig;
|
||||
import p.studio.compiler.models.BuilderPipelineContext;
|
||||
import p.studio.compiler.source.diagnostics.CompilerDiagnosticProjection;
|
||||
import p.studio.compiler.source.diagnostics.SourceReference;
|
||||
import p.studio.compiler.workspaces.BuilderPipelineService;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
import java.nio.file.*;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
|
||||
/** Compiles only the examples actually extracted from the installed reference provider. */
|
||||
public final class ProductionContractsInstalledProbe {
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
public static void verify(Path examples, Path artifacts) throws Exception {
|
||||
Files.createDirectories(artifacts);
|
||||
var corpus = JSON.readTree(examples.resolve("production/cases.json").toFile());
|
||||
var reports = new ArrayList<Map<String, Object>>();
|
||||
var failures = new ArrayList<String>();
|
||||
var provider = FrontendRegistryService.require("pbs");
|
||||
var references = provider.documentation().orElseThrow().references(1, p.studio.AppMode.Game);
|
||||
var origin = provider.getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
if (!origin.getPath().endsWith(".jar")) throw new AssertionError("Installed frontend required");
|
||||
for (var spec : corpus.get("cases")) {
|
||||
String id = spec.get("id").asText();
|
||||
Path root = examples.resolve("production").resolve(spec.get("project").asText()).toRealPath();
|
||||
var report = new LinkedHashMap<String, Object>();
|
||||
report.put("id", id);
|
||||
report.put("sourceDiscovered", true);
|
||||
report.put("expectation", spec);
|
||||
for (var reference : spec.get("references")) {
|
||||
if (references.stream().noneMatch(r -> r.id().equals(reference.asText())))
|
||||
throw new AssertionError("Missing normative reference " + reference);
|
||||
}
|
||||
report.put("sourceSha256", sourceHashes(root));
|
||||
var context = BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false, root.toString()));
|
||||
String status;
|
||||
List<Map<String, Object>> issues = List.of();
|
||||
try {
|
||||
var result = BuilderPipelineService.INSTANCE.compile(context, LogAggregator.with(line -> {}));
|
||||
if (result.diagnostics().stream().anyMatch(issue -> issue.isError())) throw new AssertionError(result.diagnostics());
|
||||
status = "compiled";
|
||||
Path cartridge = Files.createDirectories(artifacts.resolve(id));
|
||||
Files.write(cartridge.resolve("program.pbx"), result.bytecodeBytes());
|
||||
JSON.writerWithDefaultPrettyPrinter().writeValue(cartridge.resolve("manifest.json").toFile(), Map.of(
|
||||
"magic", "PMTU", "cartridge_version", 1, "app_id", 169, "title", "PBS production contracts",
|
||||
"app_version", "1.0.0", "app_mode", "Game", "capabilities", List.of(), "asset_table", List.of(), "preload", List.of()));
|
||||
report.put("cartridge", cartridge.toAbsolutePath().toString());
|
||||
report.put("bytecodeSha256", sha(cartridge.resolve("program.pbx")));
|
||||
report.put("manifestSha256", sha(cartridge.resolve("manifest.json")));
|
||||
if (spec.get("scenarios").toString().contains("addressing")) {
|
||||
var bridge = new p.studio.lsp.services.compiler.CompilerLanguageServiceBridge();
|
||||
var name = JSON.readTree(root.resolve("prometeu.json").toFile()).get("name").asText();
|
||||
var lsp = new p.studio.lsp.messages.LspProjectContext(name, "pbs", root);
|
||||
var main = root.resolve("src/main.pbs");
|
||||
var text = Files.readString(main);
|
||||
var analysis = bridge.analyzeDocument(lsp, main.toUri().toString(), text);
|
||||
report.put("lspAnalysis", analysis.details());
|
||||
if (analysis.issues().stream().anyMatch(i -> i.severity() == p.studio.lsp.messages.BaselineIssueSeverity.ERROR))
|
||||
throw new IllegalStateException("Installed LSP disagrees with compilation: " + analysis.issues());
|
||||
if (id.equals("dependency")) {
|
||||
var position = position(text, text.lastIndexOf("answer()"));
|
||||
try (var registry = new p.studio.lsp.services.compiler.ExternalSourceRegistry()) {
|
||||
var consumer = registry.connect(null, false);
|
||||
var definition = bridge.navigation(lsp, main.toUri().toString(), text, position.line(), position.character(), registry, consumer);
|
||||
if (definition.locations().stream().noneMatch(location -> registry.source(consumer, location.uri()).path()
|
||||
.equals(root.resolve("dependency/src/api/main.pbs"))))
|
||||
throw new IllegalStateException("Quoted dependency navigation lost its defining module: " + definition);
|
||||
report.put("lspDefinition", definition.locations());
|
||||
}
|
||||
var at = position(text, text.indexOf('@') + 1);
|
||||
var completion = bridge.completion(lsp, main.toUri().toString(), text, at.line(), at.character());
|
||||
if (completion.items().stream().noneMatch(item -> item.label().equals("@\"physics-2d\":api")))
|
||||
throw new IllegalStateException("Quoted dependency completion is absent: " + completion);
|
||||
report.put("lspCompletion", completion.items());
|
||||
}
|
||||
}
|
||||
} catch (BuildException rejection) {
|
||||
var sources = SourceReference.snapshot(context.fileTable);
|
||||
issues = rejection.getDiagnostics().stream().map(issue -> CompilerDiagnosticProjection.issue(issue,
|
||||
sources, CompilerDiagnosticProjection.capturedText(context.fileTable), root, id)).toList();
|
||||
status = "rejected";
|
||||
} catch (Exception failure) {
|
||||
status = "infrastructure-failure";
|
||||
report.put("failure", failure.toString());
|
||||
}
|
||||
boolean matches = status.equals(spec.get("expectedCompile").asText());
|
||||
if (spec.has("diagnosticCode")) matches &= issues.stream().anyMatch(i -> spec.get("diagnosticCode").asText().equals(i.get("code"))
|
||||
&& spec.get("diagnosticPhase").asText().equals(i.get("phase")))
|
||||
&& issues.stream().noneMatch(i -> Set.of("internal-failure", "infrastructure-failure").contains(i.get("failureKind")));
|
||||
matches &= sourceHashes(root).equals(report.get("sourceSha256"));
|
||||
report.put("diagnostics", issues);
|
||||
report.put("compileStatus", status);
|
||||
report.put("matchesExpectation", matches);
|
||||
report.put("sourceUnchanged", sourceHashes(root).equals(report.get("sourceSha256")));
|
||||
if (!matches) failures.add(id + ": " + status + " " + issues + " " + report.get("failure"));
|
||||
reports.add(report);
|
||||
}
|
||||
var provenance = new LinkedHashMap<String, Object>();
|
||||
provenance.put("frontendOrigin", origin.toString());
|
||||
provenance.put("pipelineOrigin", BuilderPipelineService.class.getProtectionDomain().getCodeSource().getLocation().toString());
|
||||
provenance.put("javaVersion", System.getProperty("java.version"));
|
||||
provenance.put("stdlibMajor", 1);
|
||||
provenance.put("target", "Game");
|
||||
var jars = new TreeMap<String, String>();
|
||||
try (var paths = Files.list(Path.of(origin.toURI()).getParent())) {
|
||||
for (var jar : paths.filter(p -> p.toString().endsWith(".jar")).sorted().toList()) jars.put(jar.getFileName().toString(), sha(jar));
|
||||
}
|
||||
provenance.put("installedJarSha256", jars);
|
||||
try (var catalog = provider.getClass().getClassLoader().getResourceAsStream("runtime-catalog/pbs-sdk/runtime-catalog.properties")) {
|
||||
if (catalog == null) throw new AssertionError("Missing installed SDK identity");
|
||||
var properties = new Properties(); properties.load(catalog);
|
||||
provenance.put("sdkCatalog", properties);
|
||||
}
|
||||
provenance.put("expectationsSha256", sha(examples.resolve("production/cases.json")));
|
||||
provenance.put("gfxExecution", "not-run; installed API compilation only");
|
||||
provenance.put("diagnosticNavigation", "Existing explanation schema has no reference-ID link field; installed guide supplies explicit mappings");
|
||||
JSON.writerWithDefaultPrettyPrinter().writeValue(artifacts.resolve("compilation.json").toFile(), Map.of(
|
||||
"schemaVersion", 1, "provenance", provenance, "cases", reports, "passed", failures.isEmpty()));
|
||||
if (!failures.isEmpty()) throw new AssertionError(String.join("\n", failures));
|
||||
System.out.println("Installed production contracts verified: " + reports.size() + " compile classifications; execution artifacts generated");
|
||||
}
|
||||
|
||||
private static Map<String, String> sourceHashes(Path root) throws Exception {
|
||||
var hashes = new TreeMap<String, String>();
|
||||
try (var files = Files.walk(root)) {
|
||||
for (var file : files.filter(Files::isRegularFile).sorted().toList()) hashes.put(root.relativize(file).toString(), sha(file));
|
||||
}
|
||||
return hashes;
|
||||
}
|
||||
|
||||
private static p.studio.lsp.messages.DocumentPosition position(String text, int utf16Index) {
|
||||
String prefix = text.substring(0, utf16Index);
|
||||
return new p.studio.lsp.messages.DocumentPosition((int) prefix.chars().filter(c -> c == '\n').count(),
|
||||
utf16Index - prefix.lastIndexOf('\n') - 1);
|
||||
}
|
||||
|
||||
private static String sha(Path file) throws Exception {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(file)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user