Files
bquarkz ea1cc26fe3
Test / Tests and coverage occurred while executing withChecks step.
Intrepid/Prometeu/Studio/pipeline/pr-master There was a failure building this commit
feat(studio): improve compiler reliability and AI diagnostics
Preserve structured diagnostics, source attribution, phase outcomes and revision context through compiler, Studio, LSP, MCP and JavaFX. Add project service discovery and resilient VS Code reconnection.

Correct all eight reproduced PBS production failures in expression context, sibling executable bindings and nominal owner identity. Consolidate specs, lessons, immutable baseline evidence and the maintained source-to-guest qualification runner.

Validation: required Gradle gate recorded 724 tests, with 711 passed and 13 skipped. Full corpus passed 168 compilation observations and 108 guest runs (104 successes and four deliberate faults); supplemental controls passed 42 compilations and 16 guest runs. TypeScript compilation, two Node discovery tests and discussion validation passed.
2026-11-10 12:36:25 +00:00

125 lines
7.9 KiB
Java

package p.studio.compiler.backend.irvm;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.studio.compiler.backend.bytecode.BytecodeEmitter;
import p.studio.compiler.backend.bytecode.BytecodeEmitter.Operation;
import p.studio.compiler.exceptions.BuildException;
import p.studio.compiler.messages.BuildingIssue;
import p.studio.compiler.messages.BuilderPipelineConfig;
import p.studio.compiler.models.BuilderPipelineContext;
import p.studio.compiler.source.Span;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.diagnostics.*;
import p.studio.compiler.workspaces.BuilderPipelineService;
import p.studio.utilities.logs.LogAggregator;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
/** Local qualification probe. Controlled IR failures are not historical reproductions. */
public final class CompilerDiagnosticProbe {
static final ObjectMapper JSON = new ObjectMapper();
static final Span SOURCE = new Span(new FileId(7), 0, 9);
public static void main(String[] args) throws Exception {
var rows = new ArrayList<Map<String, Object>>();
var source = program(1, List.of(Operation.ret().withAttribution(SourceAttribution.source(SOURCE))));
rows.add(check("ret-source", source));
rows.add(check("join-source", program(0, List.of(Operation.pushI32(1, null), Operation.jmpIfTrue(18, null),
Operation.pushI32(2, null), Operation.ret().withAttribution(SourceAttribution.source(SOURCE))))));
rows.add(check("generated-anchor", program(1, List.of(Operation.ret().withAttribution(SourceAttribution.generated(SOURCE))))));
rows.add(check("generated-no-anchor", program(1, List.of(Operation.ret().withAttribution(SourceAttribution.generated(null))))));
rows.add(check("unknown-origin", program(1, List.of(Operation.ret()))));
rows.add(check("no-plan", new IRVMProgram(source.module())));
var input = program(0, List.of(Operation.jmp(6, null), Operation.ret().withAttribution(SourceAttribution.source(SOURCE))));
var corrupt = new OptimizeIRVMService.IRVMPass() {
public String name() { return "ControlledRetMismatch"; }
public IRVMProgram apply(IRVMProgram checked) {
return program(1, checked.emissionPlan().functions().getFirst().operations().asCollection().stream().toList());
}
};
try {
new OptimizeIRVMService(new IRVMValidator(), List.of(OptimizeIRVMService.simplifyJumpToNextPcPass(), corrupt)).optimize(input);
throw new AssertionError("controlled pass must fail");
} catch (IRVMValidationException failure) {
rows.add(row("shifted-pass-output", failure));
}
var corpus = JSON.readTree(Path.of(args[0]).toFile()).get("cases");
var scratch = Path.of(args[1]);
for (var id : List.of("F1-direct", "F1-local")) {
var fixture = java.util.stream.StreamSupport.stream(corpus.spliterator(), false)
.filter(item -> item.get("id").asText().equals(id)).findFirst().orElseThrow();
var root = scratch.resolve(id);
var fields = fixture.get("files").fields();
while (fields.hasNext()) {
var field = fields.next();
var file = root.resolve(field.getKey()); Files.createDirectories(file.getParent());
Files.writeString(file, field.getValue().asText());
}
var logs = new ArrayList<String>();
var context = BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false, root.toString()));
var row = new LinkedHashMap<String, Object>(); row.put("case", id);
try {
var result = BuilderPipelineService.INSTANCE.compile(context, LogAggregator.with(logs::add));
row.put("observed", "compiled"); row.put("bytecodeBytes", result.bytecodeBytes().length);
row.put("diagnostics", result.diagnostics().stream().map(CompilerDiagnosticProbe::issue).toList());
} catch (BuildException failure) {
row.put("observed", "program-rejection"); row.put("hasDiagnosticPayload", failure.hasDiagnosticPayload());
row.put("failedStage", failure.getFailedStage());
row.put("diagnostics", failure.getDiagnostics().stream().map(CompilerDiagnosticProbe::issue).toList());
row.put("sources", failure.getSourceReferences());
}
row.put("logs", logs); rows.add(row);
}
var report = new LinkedHashMap<String, Object>(); report.put("schemaVersion", 1);
report.put("qualification", "checkout compiler; controlled failures and unchanged PBS fixtures; no runtime execution");
report.put("origins", Map.of("pipeline", origin(BuilderPipelineService.class), "core", origin(BuildException.class),
"validator", origin(IRVMValidator.class)));
report.put("cases", rows);
JSON.writerWithDefaultPrettyPrinter().writeValue(Path.of(args[2]).toFile(), report);
}
static Map<String, Object> check(String id, IRVMProgram program) {
try { new IRVMValidator().validate(program, false); throw new AssertionError("expected controlled failure " + id); }
catch (IRVMValidationException failure) { return row(id, failure); }
}
static Map<String, Object> row(String id, IRVMValidationException failure) {
var result = new LinkedHashMap<String, Object>(); result.put("case", id); result.put("observed", "internal-failure");
result.put("diagnostic", issue(failure.toIssue(failure.context().stageId() == null
? "probe-validator" : failure.context().stageId(), failure.getMessage()))); return result;
}
static Map<String, Object> issue(BuildingIssue issue) {
var result = new LinkedHashMap<String, Object>(); var context = issue.getContext();
result.put("code", issue.getCode()); result.put("error", issue.isError()); result.put("phase", issue.getPhase());
result.put("message", issue.getMessage()); result.put("stageId", context.stageId());
result.put("failureKind", context.failureKind().id()); result.put("verification", context.verification());
result.put("parameters", context.parameters());
var attribution = context.attribution(); var origin = new LinkedHashMap<String, Object>();
origin.put("originKind", attribution.originKind()); origin.put("locationKind", attribution.locationKind());
origin.put("role", attribution.role()); origin.put("absenceReason", attribution.absenceReason());
origin.put("span", span(attribution.span())); result.put("attribution", origin);
result.put("sourceSpan", span(issue.getSourceSpan()));
result.put("fileId", issue.getFileId()); result.put("start", issue.getStart()); result.put("end", issue.getEnd());
result.put("related", issue.getRelated().stream().map(related -> Map.of("relation", related.getRelation(),
"role", related.getRole(), "span", span(related.getSpan()))).toList());
return result;
}
static Object span(Span span) {
return span == null || span.isNone() ? null : Map.of("fileId", span.getFileId().getId(), "start", span.getStart(), "end", span.getEnd());
}
static IRVMProgram program(int results, List<Operation> operations) {
var fn = new BytecodeEmitter.FunctionPlan("probe", 0, 0, results, 2, ReadOnlyList.wrap(operations),
results == 0 ? p.studio.compiler.models.registry.IRRegistry.FunctionMetadata.empty() : null,
"fixture:main", SourceAttribution.function(SOURCE));
return new IRVMProgram(false, new BytecodeEmitter.EmissionPlan(0, ReadOnlyList.empty(), ReadOnlyList.empty(), ReadOnlyList.from(fn)));
}
static String origin(Class<?> type) { return type.getProtectionDomain().getCodeSource().getLocation().toString(); }
}