feat(studio): show executed source diagnostics in UI2
Capture bounded compiler inputs by revision and PBX identity, consume Play v2 faults and retain static diagnostic excerpts across failed preparation. Integrate the UI2 panel, qualification evidence and regression tests without adding an editor.
This commit is contained in:
+12
@@ -75,6 +75,18 @@ public class SourceHandle {
|
||||
|
||||
public String observedSha256() { return observedSha256; }
|
||||
public boolean mixedRevisions() { return mixedRevisions; }
|
||||
/** A coherent copy of already consumed input; never invokes the provider. */
|
||||
public synchronized Optional<Observation> observation(int maxBytes) {
|
||||
if (observedBytes != null && observedBytes.length > maxBytes)
|
||||
throw new IllegalStateException("Execution source capture unavailable: source byte limit: " + relativePath);
|
||||
return observedBytes == null ? Optional.empty()
|
||||
: Optional.of(new Observation(observedSha256, mixedRevisions, observedBytes));
|
||||
}
|
||||
|
||||
public record Observation(String sha256, boolean mixedRevisions, byte[] bytes) {
|
||||
public Observation { bytes = bytes.clone(); }
|
||||
@Override public byte[] bytes() { return bytes.clone(); }
|
||||
}
|
||||
/** Already consumed bytes only; this accessor never invokes the provider. */
|
||||
public Optional<String> observedText() {
|
||||
var bytes = observedBytes;
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import p.studio.compiler.source.tables.FileTableReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Compilation-owned capture. Callers supply their retention policy, not live paths. */
|
||||
public final class ConsumedSources {
|
||||
private ConsumedSources() { }
|
||||
public record Source(int fileId, int projectId, String path, String logicalPath,
|
||||
String kind, String sha256, String text) { }
|
||||
|
||||
public static List<Source> capture(FileTableReader files, int maxFileBytes, int maxFiles, long maxBytes) {
|
||||
var result = new ArrayList<Source>();
|
||||
long total = 128; // Capture identity/header metadata, in addition to each source record.
|
||||
for (var id : files.identifiers()) {
|
||||
if (result.size() >= maxFiles) throw unavailable("source count limit");
|
||||
var file = files.get(id);
|
||||
var observation = file.observation(maxFileBytes).orElseThrow(() -> unavailable("input was not captured: " + file.getRelativePath()));
|
||||
if (observation.mixedRevisions()) throw unavailable("mixed input revisions: " + file.getRelativePath());
|
||||
byte[] bytes = observation.bytes();
|
||||
if (bytes.length > maxFileBytes) throw unavailable("source byte limit: " + file.getRelativePath());
|
||||
String path = file.getCanonPath().toString(), logical = file.getRelativePath().toString();
|
||||
String kind = logical.startsWith("@stdlib") ? "sdk" : logical.startsWith("@generated") ? "generated" : "source";
|
||||
// Fixed fields, both paths, hash and origin are included in the budget.
|
||||
total += bytes.length + path.getBytes(StandardCharsets.UTF_8).length
|
||||
+ logical.getBytes(StandardCharsets.UTF_8).length + 128L;
|
||||
if (total > maxBytes) throw unavailable("capture byte limit");
|
||||
final String text;
|
||||
try { text = StandardCharsets.UTF_8.newDecoder().decode(java.nio.ByteBuffer.wrap(bytes)).toString(); }
|
||||
catch (java.nio.charset.CharacterCodingException invalid) { throw unavailable("invalid UTF-8: " + logical); }
|
||||
result.add(new Source(id.getId(), file.getProjectId().getIndex(), path, logical, kind, observation.sha256(), text));
|
||||
}
|
||||
return List.copyOf(result);
|
||||
}
|
||||
|
||||
private static IllegalStateException unavailable(String reason) {
|
||||
return new IllegalStateException("Execution source capture unavailable: " + reason);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.models.SourceHandle;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.compiler.source.tables.FileTable;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ConsumedSourcesTest {
|
||||
@Test void capturesConsumedBytesWithoutRereadingAndPreservesVirtualOrigins() {
|
||||
var reads = new AtomicInteger(); var files = new FileTable(1);
|
||||
var source = new SourceHandle(new ProjectId(0), Path.of("@stdlib/core/sample/main.pbs"),
|
||||
Path.of("/virtual/stdlib/core/sample/main.pbs"), 0, 0,
|
||||
path -> () -> { reads.incrementAndGet(); return "// á 😀\r\nfn x(){}".getBytes(StandardCharsets.UTF_8); });
|
||||
files.register(source); source.readBytes();
|
||||
var capture = ConsumedSources.capture(files, 1024, 10, 4096);
|
||||
assertEquals(1, reads.get()); assertEquals("sdk", capture.getFirst().kind());
|
||||
assertEquals("// á 😀\r\nfn x(){}", capture.getFirst().text());
|
||||
assertThrows(UnsupportedOperationException.class, () -> capture.clear());
|
||||
}
|
||||
|
||||
@Test void refusesUncapturedMixedMalformedAndOverBudgetInputs() {
|
||||
var files = new FileTable(1); var read = new AtomicInteger();
|
||||
var source = new SourceHandle(new ProjectId(0), Path.of("main.pbs"), Path.of("/fixture/main.pbs"), 0, 0,
|
||||
path -> () -> (read.incrementAndGet() == 1 ? "first" : "other").getBytes(StandardCharsets.UTF_8));
|
||||
files.register(source);
|
||||
assertThrows(IllegalStateException.class, () -> ConsumedSources.capture(files, 100, 1, 1000));
|
||||
source.readBytes();
|
||||
assertThrows(IllegalStateException.class, () -> ConsumedSources.capture(files, 4, 1, 1000));
|
||||
assertThrows(IllegalStateException.class, () -> ConsumedSources.capture(files, 100, 0, 1000));
|
||||
assertThrows(IllegalStateException.class, () -> ConsumedSources.capture(files, 100, 1, 1));
|
||||
source.readBytes();
|
||||
assertThrows(IllegalStateException.class, () -> ConsumedSources.capture(files, 100, 1, 1000));
|
||||
var invalid = new FileTable(1);
|
||||
var malformed = new SourceHandle(new ProjectId(0), Path.of("bad.pbs"), Path.of("/fixture/bad.pbs"), 1, 0, path -> () -> new byte[]{(byte)255});
|
||||
invalid.register(malformed); malformed.readBytes();
|
||||
assertThrows(IllegalStateException.class, () -> ConsumedSources.capture(invalid, 100, 1, 1000));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user