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.
This commit is contained in:
+29
-1
@@ -1,11 +1,39 @@
|
||||
package p.studio.compiler.exceptions;
|
||||
|
||||
import p.studio.compiler.messages.BuildingIssue;
|
||||
import p.studio.compiler.source.diagnostics.SourceReference;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BuildException extends RuntimeException {
|
||||
private final boolean diagnosticPayloadAvailable;
|
||||
private final List<BuildingIssue> diagnostics;
|
||||
private final String failedStage;
|
||||
private final Map<Integer, SourceReference> sourceReferences;
|
||||
|
||||
public BuildException(String message) {
|
||||
super(message);
|
||||
this(message, null);
|
||||
}
|
||||
|
||||
public BuildException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
diagnosticPayloadAvailable = false;
|
||||
diagnostics = List.of();
|
||||
failedStage = null;
|
||||
sourceReferences = Map.of();
|
||||
}
|
||||
|
||||
public BuildException(String message, Throwable cause, List<BuildingIssue> diagnostics,
|
||||
String failedStage, Map<Integer, SourceReference> sourceReferences) {
|
||||
super(message, cause);
|
||||
this.diagnosticPayloadAvailable = true;
|
||||
this.diagnostics = List.copyOf(diagnostics);
|
||||
this.failedStage = failedStage;
|
||||
this.sourceReferences = Map.copyOf(sourceReferences);
|
||||
}
|
||||
|
||||
public boolean hasDiagnosticPayload() { return diagnosticPayloadAvailable; }
|
||||
public List<BuildingIssue> getDiagnostics() { return diagnostics; }
|
||||
public String getFailedStage() { return failedStage; }
|
||||
public Map<Integer, SourceReference> getSourceReferences() { return sourceReferences; }
|
||||
}
|
||||
|
||||
+62
-1
@@ -3,8 +3,13 @@ package p.studio.compiler.messages;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.diagnostics.*;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Builder
|
||||
@Builder(toBuilder = true)
|
||||
@Getter
|
||||
public class BuildingIssue {
|
||||
private final boolean error;
|
||||
@@ -17,6 +22,62 @@ public class BuildingIssue {
|
||||
private final Integer end;
|
||||
private final Integer functionIndex;
|
||||
private final Integer pc;
|
||||
private final Span sourceSpan;
|
||||
@Builder.Default private final DiagnosticContext context = DiagnosticContext.unknown();
|
||||
@Builder.Default private final List<RelatedSpan> related = List.of();
|
||||
private final String templateId;
|
||||
@Builder.Default private final Map<String, String> placeholders = Map.of();
|
||||
|
||||
BuildingIssue(boolean error, String message, Throwable exception, String phase, String code,
|
||||
Integer fileId, Integer start, Integer end, Integer functionIndex, Integer pc,
|
||||
Span sourceSpan, DiagnosticContext context, List<RelatedSpan> related,
|
||||
String templateId, Map<String, String> placeholders) {
|
||||
this.error = error;
|
||||
this.message = message;
|
||||
this.exception = exception;
|
||||
this.phase = phase;
|
||||
this.code = code;
|
||||
this.fileId = fileId;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.functionIndex = functionIndex;
|
||||
this.pc = pc;
|
||||
this.sourceSpan = sourceSpan;
|
||||
this.context = context == null ? DiagnosticContext.unknown() : context;
|
||||
this.related = List.copyOf(related);
|
||||
this.templateId = templateId;
|
||||
this.placeholders = Map.copyOf(placeholders);
|
||||
}
|
||||
|
||||
BuildingIssue(boolean error, String message, Throwable exception, String phase, String code,
|
||||
Integer fileId, Integer start, Integer end, Integer functionIndex, Integer pc) {
|
||||
this(error, message, exception, phase, code, fileId, start, end, functionIndex, pc,
|
||||
null, DiagnosticContext.unknown(), List.of(), null, Map.of());
|
||||
}
|
||||
|
||||
public Severity getSeverity() { return error ? Severity.Error : Severity.Warning; }
|
||||
|
||||
public static BuildingIssue fromDiagnostic(Diagnostic diagnostic) {
|
||||
var span = diagnostic.getSpan();
|
||||
return builder().error(diagnostic.getSeverity().isError()).code(diagnostic.getCode())
|
||||
.phase(diagnostic.getReportedPhase()).message(diagnostic.getMessage())
|
||||
.sourceSpan(span).fileId(span.isNone() ? null : span.getFileId().getId())
|
||||
.start(span.isNone() ? null : safeInt(span.getStart()))
|
||||
.end(span.isNone() ? null : safeInt(span.getEnd()))
|
||||
.templateId(diagnostic.getTemplateId()).placeholders(Map.copyOf(diagnostic.getPlaceholders()))
|
||||
.related(List.copyOf(diagnostic.getRelated().asCollection())).context(diagnostic.getContext()).build();
|
||||
}
|
||||
|
||||
public Diagnostic toDiagnostic() {
|
||||
var span = sourceSpan != null ? sourceSpan : fileId == null || fileId < 0 || start == null || end == null ? Span.none()
|
||||
: new Span(new FileId(fileId), start, end);
|
||||
var diagnosticPhase = java.util.Arrays.stream(DiagnosticPhase.values())
|
||||
.filter(value -> value.id().equals(phase)).findFirst().orElse(DiagnosticPhase.fromCode(code));
|
||||
return new Diagnostic(getSeverity(), code, diagnosticPhase, templateId == null ? code : templateId,
|
||||
placeholders, message, span, related, context, phase == null ? diagnosticPhase.id() : phase);
|
||||
}
|
||||
|
||||
private static int safeInt(long value) { return (int) Math.min(Integer.MAX_VALUE, Math.max(0, value)); }
|
||||
|
||||
public void print(LogAggregator logs) {
|
||||
final var composed = composeMessage();
|
||||
|
||||
+4
@@ -26,6 +26,10 @@ public class BuildingIssueSink implements ReadOnlyCollection<BuildingIssue> {
|
||||
final var builder = BuildingIssue.builder();
|
||||
consumer.accept(builder);
|
||||
final var issue = builder.build();
|
||||
return add(issue);
|
||||
}
|
||||
|
||||
public BuildingIssueSink add(BuildingIssue issue) {
|
||||
hasErrors |= issue.isError();
|
||||
issues.add(issue);
|
||||
return this;
|
||||
|
||||
+26
-2
@@ -33,6 +33,9 @@ public class SourceHandle {
|
||||
@Getter
|
||||
private final long lastModified;
|
||||
private final SourceProvider provider;
|
||||
private volatile String observedSha256;
|
||||
private volatile boolean mixedRevisions;
|
||||
private volatile byte[] observedBytes;
|
||||
|
||||
public SourceHandle(
|
||||
final ProjectId projectId,
|
||||
@@ -53,12 +56,33 @@ public class SourceHandle {
|
||||
|
||||
public Optional<byte[]> readBytes() {
|
||||
try {
|
||||
return Optional.ofNullable(provider.read());
|
||||
var bytes = provider.read();
|
||||
if (bytes != null) observe(bytes);
|
||||
return Optional.ofNullable(bytes);
|
||||
} catch (IOException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void observe(byte[] bytes) {
|
||||
final String digest;
|
||||
try { digest = java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(bytes)); }
|
||||
catch (java.security.NoSuchAlgorithmException impossible) { throw new IllegalStateException(impossible); }
|
||||
if (observedSha256 != null && !observedSha256.equals(digest)) mixedRevisions = true;
|
||||
observedSha256 = digest;
|
||||
observedBytes = bytes.clone();
|
||||
}
|
||||
|
||||
public String observedSha256() { return observedSha256; }
|
||||
public boolean mixedRevisions() { return mixedRevisions; }
|
||||
/** Already consumed bytes only; this accessor never invokes the provider. */
|
||||
public Optional<String> observedText() {
|
||||
var bytes = observedBytes;
|
||||
if (bytes == null) return Optional.empty();
|
||||
try { return Optional.of(StandardCharsets.UTF_8.newDecoder().decode(java.nio.ByteBuffer.wrap(bytes)).toString()); }
|
||||
catch (java.nio.charset.CharacterCodingException invalidUtf8) { return Optional.empty(); }
|
||||
}
|
||||
|
||||
public Optional<String> readUtf8() {
|
||||
return readBytes().map(bytes -> new String(bytes, StandardCharsets.UTF_8));
|
||||
}
|
||||
@@ -70,4 +94,4 @@ public class SourceHandle {
|
||||
size / 1024,
|
||||
LocalDateTime.ofInstant(Instant.ofEpochMilli(lastModified), ZoneId.systemDefault()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import p.studio.compiler.messages.BuildingIssue;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.tables.FileTableReader;
|
||||
import p.studio.utilities.json.DiagnosticPayloads;
|
||||
import p.studio.utilities.json.ImmutableJson;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
/** Projects recorded compiler facts. No live source reads, language inference or transport DTOs. */
|
||||
public final class CompilerDiagnosticProjection {
|
||||
private CompilerDiagnosticProjection() { }
|
||||
|
||||
public static List<Map<String, Object>> issues(List<BuildingIssue> issues, Map<Integer, SourceReference> sources,
|
||||
Map<Integer, String> text, Path root, String analysisId) {
|
||||
var result = new ArrayList<Map<String, Object>>();
|
||||
for (int i = 0; i < issues.size(); i++) result.add(issue(issues.get(i), sources, text, root, analysisId + ":" + i));
|
||||
return List.copyOf(result);
|
||||
}
|
||||
|
||||
public static Map<Integer, String> capturedText(FileTableReader files) {
|
||||
var result = new LinkedHashMap<Integer, String>();
|
||||
if (files != null) for (var id : files.identifiers()) files.get(id).observedText().ifPresent(value -> result.put(id.getId(), value));
|
||||
return Map.copyOf(result);
|
||||
}
|
||||
|
||||
public static List<Map<String, Object>> inputs(Map<Integer, SourceReference> sources, Path root) {
|
||||
return sources.values().stream().sorted(Comparator.comparingInt(SourceReference::fileId))
|
||||
.filter(source -> source.sha256() != null).map(source -> source(source, root)).toList();
|
||||
}
|
||||
|
||||
public static Map<String, Object> issue(BuildingIssue issue, Map<Integer, SourceReference> sources,
|
||||
Map<Integer, String> texts, Path root, String id) {
|
||||
var result = new LinkedHashMap<>(DiagnosticPayloads.issue(
|
||||
issue.getCode() == null ? "COMPILER_DIAGNOSTIC" : issue.getCode(), issue.getSeverity().name().toUpperCase(Locale.ROOT),
|
||||
Objects.requireNonNullElse(issue.getMessage(), "Compiler diagnostic")));
|
||||
var context = issue.getContext();
|
||||
result.put("issueId", id); result.put("codeOrigin", issue.getCode() == null ? "studio-adapter" : "producer");
|
||||
result.put("templateId", issue.getTemplateId()); result.put("templateParameters", issue.getPlaceholders());
|
||||
result.put("phase", issue.getPhase()); result.put("stageId", context.stageId());
|
||||
result.put("failureKind", context.failureKind().id()); result.put("facts", context.parameters());
|
||||
result.putAll(context.enrichment());
|
||||
if (!context.enrichment().containsKey("producer")) result.put("producer", ImmutableJson.fields("component", "compiler",
|
||||
"owner", "compiler", "languageId", null));
|
||||
Span primary = issue.getSourceSpan();
|
||||
if ((primary == null || primary.isNone()) && issue.getFileId() != null && issue.getFileId() >= 0
|
||||
&& issue.getStart() != null && issue.getEnd() != null) {
|
||||
primary = new Span(new FileId(issue.getFileId()), issue.getStart(), issue.getEnd());
|
||||
}
|
||||
var origin = new LinkedHashMap<>(origin(primary, sources, texts, root));
|
||||
var attribution = context.attribution();
|
||||
origin.put("attribution", ImmutableJson.fields("originKind", attribution.originKind(), "locationKind", attribution.locationKind(),
|
||||
"role", attribution.role(), "absenceReason", attribution.absenceReason()));
|
||||
var v = context.verification();
|
||||
origin.put("verification", v == null ? null : ImmutableJson.fields("functionIndex", v.functionIndex(), "pc", v.pc(),
|
||||
"functionName", v.functionName(), "moduleName", v.moduleName(), "pcSpace", v.pcSpace(), "pcUnit", v.pcUnit(),
|
||||
"validationPoint", v.validationPoint(), "passName", v.passName(), "stageId", context.stageId()));
|
||||
result.put("origin", ImmutableJson.object(origin));
|
||||
result.put("related", issue.getRelated().stream().map(related -> ImmutableJson.fields("relation", related.getRelation(),
|
||||
"role", related.getRole(), "message", related.getMessage(), "origin", origin(related.getSpan(), sources, texts, root))).toList());
|
||||
return ImmutableJson.object(result);
|
||||
}
|
||||
|
||||
public static Map<String, Object> source(SourceReference source, Path root) {
|
||||
var path = Path.of(source.canonicalPath()).toAbsolutePath().normalize();
|
||||
boolean local = root != null && path.startsWith(root.toAbsolutePath().normalize());
|
||||
return ImmutableJson.fields("fileId", source.fileId(), "projectId", source.projectId(), "inputKind", "source", "observationScope", "compiler",
|
||||
"uri", local ? path.toUri().toString() : null, "relativePath", source.relativePath(),
|
||||
"originScope", local ? "project" : "external", "sha256", source.sha256(),
|
||||
"revisionAvailability", source.sha256() == null ? "not-recorded" : source.mixedRevisions() ? "mixed" : "available",
|
||||
"readOnly", !local);
|
||||
}
|
||||
|
||||
private static Map<String, Object> origin(Span span, Map<Integer, SourceReference> sources,
|
||||
Map<Integer, String> texts, Path root) {
|
||||
if (span == null || span.isNone()) return ImmutableJson.fields("uri", null, "fileId", null, "span", null,
|
||||
"range", null, "revision", null, "availability", "unavailable");
|
||||
int file = span.getFileId().getId();
|
||||
var source = sources.get(file);
|
||||
var reference = source == null ? Map.<String, Object>of() : source(source, root);
|
||||
var text = texts.get(file);
|
||||
Map<String, Object> range = null;
|
||||
if (source != null && !source.mixedRevisions() && text != null && span.getStart() >= 0 && span.getEnd() >= span.getStart()
|
||||
&& span.getEnd() <= text.getBytes(java.nio.charset.StandardCharsets.UTF_8).length) {
|
||||
var start = position(text, span.getStart()); var end = position(text, span.getEnd());
|
||||
if (start != null && end != null) range = ImmutableJson.fields("start", start, "end", end, "unit", "utf16");
|
||||
}
|
||||
return ImmutableJson.fields("uri", reference.get("uri"), "fileId", file,
|
||||
"span", ImmutableJson.fields("start", span.getStart(), "end", span.getEnd(), "unit", "utf8-byte"),
|
||||
"range", range, "revision", source == null ? null : source.sha256(), "source", reference,
|
||||
"availability", source == null ? "partial" : "available");
|
||||
}
|
||||
|
||||
private static Map<String, Object> position(String text, long target) {
|
||||
long bytes = 0; int line = 0, character = 0;
|
||||
for (int i = 0; i < text.length() && bytes < target;) {
|
||||
int cp = text.codePointAt(i), width = Character.charCount(cp);
|
||||
int encoded = new String(Character.toChars(cp)).getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
|
||||
if (bytes + encoded > target) break;
|
||||
bytes += encoded; i += width;
|
||||
if (cp == '\n') { line++; character = 0; } else character += width;
|
||||
}
|
||||
return bytes == target ? ImmutableJson.fields("line", line, "character", character) : null;
|
||||
}
|
||||
}
|
||||
+18
@@ -18,6 +18,8 @@ public class Diagnostic {
|
||||
private final String message;
|
||||
private final Span span;
|
||||
private final ReadOnlyList<RelatedSpan> related;
|
||||
private final DiagnosticContext context;
|
||||
private final String reportedPhase;
|
||||
|
||||
public Diagnostic(
|
||||
final Severity severity,
|
||||
@@ -45,6 +47,20 @@ public class Diagnostic {
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
this(severity, code, phase, templateId, placeholders, message, span, related, DiagnosticContext.unknown());
|
||||
}
|
||||
|
||||
public Diagnostic(
|
||||
final Severity severity, final String code, final DiagnosticPhase phase,
|
||||
final String templateId, final Map<String, String> placeholders, final String message,
|
||||
final Span span, final List<RelatedSpan> related, final DiagnosticContext context) {
|
||||
this(severity, code, phase, templateId, placeholders, message, span, related, context, phase.id());
|
||||
}
|
||||
|
||||
public Diagnostic(
|
||||
final Severity severity, final String code, final DiagnosticPhase phase,
|
||||
final String templateId, final Map<String, String> placeholders, final String message,
|
||||
final Span span, final List<RelatedSpan> related, final DiagnosticContext context, final String reportedPhase) {
|
||||
this.severity = Objects.requireNonNull(severity);
|
||||
this.code = Objects.requireNonNull(code);
|
||||
this.phase = Objects.requireNonNull(phase);
|
||||
@@ -53,6 +69,8 @@ public class Diagnostic {
|
||||
this.message = Objects.requireNonNull(message);
|
||||
this.span = Objects.requireNonNull(span);
|
||||
this.related = ReadOnlyList.wrap(List.copyOf(Objects.requireNonNull(related)));
|
||||
this.context = Objects.requireNonNull(context);
|
||||
this.reportedPhase = Objects.requireNonNull(reportedPhase);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Small shared diagnostic value, independent of Studio and transport DTOs. */
|
||||
public record DiagnosticContext(
|
||||
String stageId, VerificationContext verification, SourceAttribution attribution,
|
||||
FailureKind failureKind, Map<String, Object> parameters, Map<String, Object> enrichment) {
|
||||
public DiagnosticContext(String stageId, VerificationContext verification, SourceAttribution attribution,
|
||||
FailureKind failureKind, Map<String, Object> parameters) {
|
||||
this(stageId, verification, attribution, failureKind, parameters, Map.of());
|
||||
}
|
||||
public DiagnosticContext {
|
||||
attribution = attribution == null ? SourceAttribution.unknown("unknown") : attribution;
|
||||
failureKind = failureKind == null ? FailureKind.UNKNOWN : failureKind;
|
||||
parameters = freeze(parameters == null ? Map.of() : parameters);
|
||||
enrichment = p.studio.utilities.json.ImmutableJson.object(enrichment);
|
||||
}
|
||||
|
||||
public static DiagnosticContext unknown() {
|
||||
return new DiagnosticContext(null, null, null, FailureKind.UNKNOWN, Map.of());
|
||||
}
|
||||
|
||||
public DiagnosticContext withStage(String stage) {
|
||||
return new DiagnosticContext(stage, verification, attribution, failureKind, parameters, enrichment);
|
||||
}
|
||||
|
||||
public DiagnosticContext withFailureKind(FailureKind kind) {
|
||||
return new DiagnosticContext(stageId, verification, attribution, kind, parameters, enrichment);
|
||||
}
|
||||
|
||||
public DiagnosticContext withAttribution(SourceAttribution origin) {
|
||||
return new DiagnosticContext(stageId, verification, origin, failureKind, parameters, enrichment);
|
||||
}
|
||||
|
||||
public DiagnosticContext withEnrichment(Map<String, Object> details) {
|
||||
return new DiagnosticContext(stageId, verification, attribution, failureKind, parameters, details);
|
||||
}
|
||||
|
||||
public DiagnosticContext withParameters(Map<String, Object> facts) {
|
||||
return new DiagnosticContext(stageId, verification, attribution, failureKind, facts, enrichment);
|
||||
}
|
||||
|
||||
private static Map<String, Object> freeze(Map<String, ?> input) {
|
||||
var output = new LinkedHashMap<String, Object>();
|
||||
input.forEach((key, value) -> output.put(key, freezeValue(value)));
|
||||
return Map.copyOf(output);
|
||||
}
|
||||
|
||||
private static Object freezeValue(Object value) {
|
||||
if (value instanceof String || value instanceof Boolean || value instanceof Byte || value instanceof Short
|
||||
|| value instanceof Integer || value instanceof Long || value instanceof Float || value instanceof Double
|
||||
|| value instanceof java.math.BigInteger || value instanceof java.math.BigDecimal) return value;
|
||||
if (value instanceof List<?> values) return values.stream().map(DiagnosticContext::freezeValue).toList();
|
||||
if (value instanceof Map<?, ?> values) {
|
||||
var output = new LinkedHashMap<String, Object>();
|
||||
values.forEach((key, item) -> output.put((String) key, freezeValue(item)));
|
||||
return Map.copyOf(output);
|
||||
}
|
||||
throw new IllegalArgumentException("diagnostic parameters must be structured scalar/list/map values");
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import p.studio.utilities.json.DiagnosticPayloads;
|
||||
import p.studio.utilities.json.ImmutableJson;
|
||||
import java.util.*;
|
||||
|
||||
/** Owner-maintained descriptions of detector behavior. They do not infer root cause. */
|
||||
public final class DiagnosticDescriptions {
|
||||
private DiagnosticDescriptions() { }
|
||||
public static Map<String, Object> describe(String check, Map<String, Object> semanticFacts) {
|
||||
boolean frontend = check.startsWith("pbs.");
|
||||
String rule = switch (check) {
|
||||
case "compiler.verify.ret-stack-height" -> "The stack at RET must match the function's declared result slot count.";
|
||||
case "compiler.verify.join-stack-height" -> "Incoming control-flow paths must agree on stack height at the join destination.";
|
||||
case "compiler.verify.resource-limit" -> "Producer verification must remain within its configured resource budget.";
|
||||
case "pbs.lowering.call-resolution" -> "Executable call lowering must identify an admitted callable using the actual resolution constraints.";
|
||||
case "pbs.lowering.structured-overload" -> "A structured call requires an exact signature in the owner index consulted by lowering.";
|
||||
case "pbs.flow.call-target" -> "An applied expression must resolve to a callable target in its current semantic scope.";
|
||||
case "pbs.flow.assignment-target" -> "An assignment must resolve its target binding in the current semantic scope.";
|
||||
case "pbs.lowering.stack-shape" -> "The executable body produced by frontend lowering must have a consistent stack shape.";
|
||||
default -> null;
|
||||
};
|
||||
var facts = new LinkedHashMap<String, Object>();
|
||||
semanticFacts.forEach((name, value) -> facts.put(name, DiagnosticPayloads.fact(value)));
|
||||
if (frontend) for (var name : List.of("expressionTypes", "moduleIdentity", "dependencyIdentity"))
|
||||
facts.putIfAbsent(name, DiagnosticPayloads.missingFact("Not recorded by this detector; source identity is carried separately"));
|
||||
var semantics = frontend || !facts.isEmpty()
|
||||
? ImmutableJson.fields("availability", "partial", "facts", facts)
|
||||
: ImmutableJson.fields("availability", "not-applicable", "facts", Map.of());
|
||||
var explanation = rule == null ? DiagnosticPayloads.unavailableExplanation()
|
||||
: ImmutableJson.fields("id", check, "summary", rule, "rule", rule, "availability", "available");
|
||||
var suggested = rule == null ? List.of() : List.of(ImmutableJson.fields("id", check + ".inspect-context",
|
||||
"description", frontend ? "Inspect the recorded declaration, expression types and actual resolution constraints."
|
||||
: "Inspect the produced IR and its recorded origin at the detection point; the location does not establish cause.",
|
||||
"evidenceFields", frontend ? List.of("semanticContext", "origin", "facts") : List.of("facts", "origin", "related")));
|
||||
return ImmutableJson.fields("checkId", check, "producer", ImmutableJson.fields("component",
|
||||
frontend ? "compiler-frontend" : "compiler-backend", "owner", frontend ? "compiler/pbs" : "compiler",
|
||||
"languageId", frontend ? "pbs" : null), "explanation", explanation,
|
||||
"semanticContext", semantics, "suggestedChecks", suggested);
|
||||
}
|
||||
|
||||
public static Diagnostic attach(Diagnostic diagnostic, String check, Map<String, Object> semantics, FailureKind kind) {
|
||||
return new Diagnostic(diagnostic.getSeverity(), diagnostic.getCode(), diagnostic.getPhase(),
|
||||
diagnostic.getTemplateId(), diagnostic.getPlaceholders(), diagnostic.getMessage(), diagnostic.getSpan(),
|
||||
diagnostic.getRelated().asList(), diagnostic.getContext().withEnrichment(describe(check, semantics))
|
||||
.withFailureKind(kind), diagnostic.getReportedPhase());
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
/** Evidence supplied by the detecting boundary, never inferred from message text. */
|
||||
public enum FailureKind {
|
||||
PROGRAM_REJECTION("program-rejection"),
|
||||
INTERNAL_FAILURE("internal-failure"),
|
||||
VERIFICATION_LIMIT("verification-limit"),
|
||||
INFRASTRUCTURE_FAILURE("infrastructure-failure"),
|
||||
UNKNOWN("unknown");
|
||||
|
||||
private final String id;
|
||||
|
||||
FailureKind(String id) { this.id = id; }
|
||||
|
||||
public String id() { return id; }
|
||||
}
|
||||
+9
@@ -7,9 +7,18 @@ import p.studio.compiler.source.Span;
|
||||
public class RelatedSpan {
|
||||
private final String message;
|
||||
private final Span span;
|
||||
private final String relation;
|
||||
private final String role;
|
||||
|
||||
public RelatedSpan(String message, Span span) {
|
||||
this(message, span, "unspecified", "context");
|
||||
}
|
||||
|
||||
public RelatedSpan(String message, Span span, String relation, String role) {
|
||||
this.message = message;
|
||||
this.span = span;
|
||||
this.relation = java.util.Objects.requireNonNull(relation);
|
||||
if (!"context".equals(role)) throw new IllegalArgumentException("related locations are context only");
|
||||
this.role = role;
|
||||
}
|
||||
}
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import p.studio.compiler.source.Span;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/** Provenance and location precision are independent. An anchor never proves cause. */
|
||||
public record SourceAttribution(
|
||||
String originKind, String locationKind, String role, Span span, String absenceReason) {
|
||||
public SourceAttribution {
|
||||
if (!Set.of("source", "generated", "unknown").contains(originKind)
|
||||
|| !Set.of("instruction", "function", "none").contains(locationKind)
|
||||
|| !Set.of("detection", "context").contains(role)) {
|
||||
throw new IllegalArgumentException("invalid source attribution kind or role");
|
||||
}
|
||||
if (span != null && span.isNone()) span = null;
|
||||
if ("none".equals(locationKind)) {
|
||||
if (span != null || !Set.of("no-emission-plan", "no-source-span", "generated-without-anchor", "unknown")
|
||||
.contains(absenceReason)) throw new IllegalArgumentException("missing attribution requires an absence reason");
|
||||
} else if (span == null || absenceReason != null) {
|
||||
throw new IllegalArgumentException("located attribution requires a real span and no absence reason");
|
||||
}
|
||||
if (("function".equals(locationKind) || "generated".equals(originKind))
|
||||
&& span != null && !"context".equals(role)) {
|
||||
throw new IllegalArgumentException("function and generated anchors are context only");
|
||||
}
|
||||
}
|
||||
|
||||
public static SourceAttribution source(Span span) {
|
||||
return span == null || span.isNone() ? unknown("no-source-span")
|
||||
: new SourceAttribution("source", "instruction", "detection", span, null);
|
||||
}
|
||||
|
||||
public static SourceAttribution function(Span span) {
|
||||
return span == null || span.isNone() ? unknown("no-source-span")
|
||||
: new SourceAttribution("source", "function", "context", span, null);
|
||||
}
|
||||
|
||||
public static SourceAttribution generated(Span anchor) {
|
||||
return anchor == null || anchor.isNone()
|
||||
? new SourceAttribution("generated", "none", "context", null, "generated-without-anchor")
|
||||
: new SourceAttribution("generated", "instruction", "context", anchor, null);
|
||||
}
|
||||
|
||||
public static SourceAttribution unknown(String reason) {
|
||||
return new SourceAttribution("unknown", "none", "detection", null, reason);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof SourceAttribution that)) return false;
|
||||
return originKind.equals(that.originKind) && locationKind.equals(that.locationKind) && role.equals(that.role)
|
||||
&& java.util.Objects.equals(absenceReason, that.absenceReason)
|
||||
&& (span == null ? that.span == null : that.span != null
|
||||
&& span.getFileId().getId() == that.span.getFileId().getId()
|
||||
&& span.getStart() == that.span.getStart() && span.getEnd() == that.span.getEnd());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return java.util.Objects.hash(originKind, locationKind, role, absenceReason,
|
||||
span == null ? null : span.getFileId().getId(), span == null ? null : span.getStart(),
|
||||
span == null ? null : span.getEnd());
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import p.studio.compiler.source.tables.FileTableReader;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** Compilation-local identity only. Paths and timestamps do not establish a content revision. */
|
||||
public record SourceReference(int fileId, int projectId, String canonicalPath, String relativePath,
|
||||
String sha256, boolean mixedRevisions) {
|
||||
public SourceReference(int fileId, int projectId, String canonicalPath, String relativePath) {
|
||||
this(fileId, projectId, canonicalPath, relativePath, null, false);
|
||||
}
|
||||
public static Map<Integer, SourceReference> snapshot(FileTableReader files) {
|
||||
if (files == null) return Map.of();
|
||||
var result = new LinkedHashMap<Integer, SourceReference>();
|
||||
for (var id : files.identifiers()) {
|
||||
var file = files.get(id);
|
||||
if (file == null) continue;
|
||||
result.put(id.getId(), new SourceReference(id.getId(), file.getProjectId().getIndex(),
|
||||
file.getCanonPath().toString(), file.getRelativePath().toString(), file.observedSha256(), file.mixedRevisions()));
|
||||
}
|
||||
return Map.copyOf(result);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
/** Coordinates belong to one checked program, not to an executed instruction. */
|
||||
public record VerificationContext(
|
||||
String validationPoint, String passName, String pcSpace, String pcUnit,
|
||||
int functionIndex, int pc, String functionName, String moduleName) {
|
||||
public VerificationContext at(String point, String pass) {
|
||||
return new VerificationContext(point, pass, pcSpace, pcUnit, functionIndex, pc, functionName, moduleName);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.models.SourceHandle;
|
||||
import p.studio.compiler.messages.BuildingIssue;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.identifiers.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ConsumedSourceProjectionTest {
|
||||
@Test void observationAccessNeverRereadsProviderAndDetectsMixedBytes() {
|
||||
var reads = new AtomicInteger();
|
||||
var handle = new SourceHandle(new ProjectId(0), Path.of("main.pbs"), Path.of("/fixture/main.pbs"), 0, 0,
|
||||
path -> () -> (reads.incrementAndGet() == 1 ? "first" : "changed").getBytes(StandardCharsets.UTF_8));
|
||||
assertNull(handle.observedSha256()); assertTrue(handle.observedText().isEmpty()); assertEquals(0, reads.get());
|
||||
handle.readBytes(); String first = handle.observedSha256();
|
||||
assertEquals("first", handle.observedText().orElseThrow()); assertEquals(1, reads.get());
|
||||
handle.readBytes(); assertNotEquals(first, handle.observedSha256()); assertTrue(handle.mixedRevisions());
|
||||
assertEquals("changed", handle.observedText().orElseThrow()); assertEquals(2, reads.get());
|
||||
}
|
||||
@Test void malformedObservedUtf8DoesNotSupplyInventedTextForRangeConversion() {
|
||||
var handle = new SourceHandle(new ProjectId(0), Path.of("main.pbs"), Path.of("/fixture/main.pbs"), 1, 0,
|
||||
path -> () -> new byte[]{(byte)0xff});
|
||||
handle.readBytes(); assertNotNull(handle.observedSha256()); assertTrue(handle.observedText().isEmpty());
|
||||
}
|
||||
@Test void unicodeRangesRequireMatchingObservedRevisionAndFileIdIsAnalysisLocal() {
|
||||
var root = Path.of("/fixture"); var id = new FileId(7); String text = "// á 😀\r\nfn x() {}\n";
|
||||
int start = text.substring(0, text.indexOf("x")).getBytes(StandardCharsets.UTF_8).length;
|
||||
var issue = BuildingIssue.fromDiagnostic(new Diagnostic(Severity.Error, "E_SEM_TEST", "observed", new Span(id, start, start+1), List.of()));
|
||||
var first = new SourceReference(7, 0, "/fixture/main.pbs", "main.pbs", "first-digest", false);
|
||||
var value = CompilerDiagnosticProjection.issues(List.of(issue), Map.of(7, first), Map.of(7, text), root, "analysis-a").getFirst();
|
||||
var origin = (Map<?, ?>)value.get("origin"); var range = (Map<?, ?>)origin.get("range");
|
||||
assertEquals(Map.of("line", 1, "character", 3), range.get("start"));
|
||||
assertEquals("first-digest", origin.get("revision"));
|
||||
var second = new SourceReference(7, 1, "/fixture/other.pbs", "other.pbs", "second-digest", true);
|
||||
var other = CompilerDiagnosticProjection.issues(List.of(issue), Map.of(7, second), Map.of(7, text), root, "analysis-b").getFirst();
|
||||
assertNotEquals(value.get("issueId"), other.get("issueId"));
|
||||
assertEquals("file:///fixture/other.pbs", ((Map<?, ?>)other.get("origin")).get("uri"));
|
||||
assertNull(((Map<?, ?>)other.get("origin")).get("range"));
|
||||
var missing = BuildingIssue.fromDiagnostic(new Diagnostic(Severity.Error, "E_SEM_TEST", "locationless", Span.none(), List.of()));
|
||||
assertNull(((Map<?, ?>)CompilerDiagnosticProjection.issues(List.of(missing), Map.of(), Map.of(), root, "c").getFirst().get("origin")).get("range"));
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.compiler.messages.BuildingIssue;
|
||||
import p.studio.compiler.models.SourceHandle;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.compiler.source.tables.FileTable;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StructuredDiagnosticTest {
|
||||
@Test
|
||||
void backendPhaseMustRoundTripWithoutInventingASemanticPhase() {
|
||||
var issue = BuildingIssue.builder().error(true).code("MARSHAL_VERIFY_PRECHECK_BAD_RET_STACK_HEIGHT")
|
||||
.phase("BACKEND_LOWER_TO_IRVM").message("ret mismatch")
|
||||
.context(DiagnosticContext.unknown().withStage("BACKEND_LOWER_TO_IRVM")
|
||||
.withFailureKind(FailureKind.INTERNAL_FAILURE)).build();
|
||||
var diagnostic = issue.toDiagnostic();
|
||||
assertEquals(DiagnosticPhase.UNKNOWN, diagnostic.getPhase());
|
||||
assertEquals("BACKEND_LOWER_TO_IRVM", diagnostic.getReportedPhase());
|
||||
assertEquals(issue.getPhase(), BuildingIssue.fromDiagnostic(diagnostic).getPhase());
|
||||
assertEquals(issue.getContext(), BuildingIssue.fromDiagnostic(diagnostic).getContext());
|
||||
}
|
||||
@Test
|
||||
void projectionMustPreservePhaseParametersRelatedAndLongSpan() {
|
||||
var span = new Span(new FileId(2), 0, (long) Integer.MAX_VALUE + 12);
|
||||
var related = new RelatedSpan("declared here", new Span(new FileId(3), 20, 25), "declaration", "context");
|
||||
var context = new DiagnosticContext("FrontendPhasePipelineStage", null, null,
|
||||
FailureKind.PROGRAM_REJECTION, Map.of("argument", 7));
|
||||
var original = new Diagnostic(Severity.Error, "E_SEM_TEST", DiagnosticPhase.STATIC_SEMANTICS,
|
||||
"semantic.test", Map.of("name", "value"), "Original message", span, List.of(related), context);
|
||||
var issue = BuildingIssue.fromDiagnostic(original);
|
||||
var projected = issue.toDiagnostic();
|
||||
assertEquals(0, issue.getStart());
|
||||
assertSame(span, projected.getSpan());
|
||||
assertEquals(original.getPhase(), projected.getPhase());
|
||||
assertEquals(original.getCode(), projected.getCode());
|
||||
assertEquals(original.getMessage(), projected.getMessage());
|
||||
assertEquals(original.getTemplateId(), projected.getTemplateId());
|
||||
assertEquals(original.getPlaceholders(), projected.getPlaceholders());
|
||||
assertEquals(context, projected.getContext());
|
||||
assertEquals("declaration", projected.getRelated().getFirst().getRelation());
|
||||
assertEquals("context", projected.getRelated().getFirst().getRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void provenanceMustRemainIndependentFromAvailability() {
|
||||
var zero = new Span(new FileId(0), 0, 1);
|
||||
assertEquals("detection", SourceAttribution.source(zero).role());
|
||||
assertEquals("context", SourceAttribution.generated(zero).role());
|
||||
assertEquals("generated-without-anchor", SourceAttribution.generated(null).absenceReason());
|
||||
assertEquals("unknown", SourceAttribution.source(Span.none()).originKind());
|
||||
assertEquals("function", SourceAttribution.function(zero).locationKind());
|
||||
assertEquals("context", SourceAttribution.function(zero).role());
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new SourceAttribution("generated", "instruction", "detection", zero, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void structuredParametersMustBeCopiedRecursively() {
|
||||
var list = new ArrayList<>(List.of("before"));
|
||||
var parameters = new HashMap<String, Object>();
|
||||
parameters.put("values", list);
|
||||
var context = new DiagnosticContext(null, null, null, null, parameters);
|
||||
list.clear(); parameters.clear();
|
||||
assertEquals(List.of("before"), context.parameters().get("values"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> context.parameters().put("x", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failuresMustKeepIndependentFileIdsAfterTablesAreCleared() {
|
||||
var left = table("left");
|
||||
var right = table("right");
|
||||
var issues = new ArrayList<>(List.of(BuildingIssue.builder().error(true).code("E_SEM_TEST").build()));
|
||||
var a = new BuildException("rejected", null, issues, "analyze", SourceReference.snapshot(left));
|
||||
var b = new BuildException("rejected", null, issues, "analyze", SourceReference.snapshot(right));
|
||||
left.clear(); right.clear(); issues.clear();
|
||||
assertEquals("/left/main.pbs", a.getSourceReferences().get(0).canonicalPath());
|
||||
assertEquals("/right/main.pbs", b.getSourceReferences().get(0).canonicalPath());
|
||||
assertEquals(1, a.getDiagnostics().size());
|
||||
assertTrue(a.hasDiagnosticPayload());
|
||||
assertThrows(UnsupportedOperationException.class, () -> a.getSourceReferences().clear());
|
||||
assertFalse(new BuildException("legacy").hasDiagnosticPayload());
|
||||
assertFalse(new BuildException("legacy", new IllegalStateException()).hasDiagnosticPayload());
|
||||
}
|
||||
|
||||
private FileTable table(String root) {
|
||||
var files = new FileTable(1);
|
||||
files.register(new SourceHandle(new ProjectId(0), Path.of("main.pbs"), Path.of("/" + root + "/main.pbs"),
|
||||
0, 0, path -> () -> { throw new AssertionError("identity snapshots must not read live source"); }));
|
||||
return files;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user