feat(studio): deliver asset authoring and automation workflows
Test / Tests and coverage occurred while executing withChecks step.
Intrepid/Prometeu/Studio/pipeline/pr-master There was a failure building this commit

Add shared human/task asset batches with reviewed atomic publication, explicit
reference rebinds, ownership inspection and qualified glyph/scene budgets.
Expose the same authoring and capacity results through Assets and MCP.

Deliver isolated guest test targets, retained visual evidence and immutable
runtime lifecycle/control observations. Preserve provider qualification and
installed authoring examples alongside the completed discussion lessons.

Close AGD-106 coordination and the delivered discussion workflows through
housekeeping, updating lesson references and preserving qualification limits.
Sound remains outside the asset delivery wave.

Validation: 579 module tests and 31 additional desktop, process and installed
checks passed; native glyph/scene 16/17 boundaries qualified. Discussion
validation passed after housekeeping.
This commit is contained in:
2026-11-11 01:21:25 +00:00
parent b351f2e582
commit 1471b427f1
325 changed files with 61179 additions and 323 deletions
@@ -0,0 +1,22 @@
package p.studio.core.assets;
import p.studio.api.assets.*;
import p.studio.api.project.ProjectReference;
import java.nio.file.Path;
import java.util.List;
/** Application persistence/PNG boundary; all mutations are admitted by the shared Core service. */
public interface AssetBatchWorkspace {
List<AssetSummary> catalog(ProjectReference project);
List<AssetBatches.ArtifactInfo> artifacts(ProjectReference project,AssetReference bank);
List<AssetBatches.ArtifactInfo> previewArtifacts(ProjectReference project,String batch,AssetReference bank);
AssetDetailsSnapshot inspect(ProjectReference project,AssetReference bank);
AssetBatches.Revision revision(ProjectReference project);
List<AssetBatches.Ownership> dependencies(ProjectReference project,AssetReference asset);
AssetBudgets.Snapshot budget(ProjectReference project);
AssetBatches.AuthorizedPng authorize(ProjectReference project,Path source);
AssetBatches.Preview preview(ProjectReference project,String task,String request,AssetBatches.Request changes);
AssetBatches.Preview preview(ProjectReference project,String task,String batch);
AssetBatches.Result commit(ProjectReference project,String task,String request,String batch,AssetBatches.Revision revision);
AssetBatches.Result result(ProjectReference project,String task,String batch);
}
@@ -0,0 +1,49 @@
package p.studio.core.assets;
import p.packer.dtos.PackerPackPlanDTO;
import p.studio.api.assets.*;
import p.studio.api.assets.AssetBudgets.*;
import java.util.*;
/** Distinct bank-content and residency views over the final packer plan. */
public final class AssetBudgetPolicy {
public Snapshot evaluate(AssetBatches.Revision revision,ProfileSelection profile,PackerPackPlanDTO plan,
List<AssetBatches.Diagnostic> producerDiagnostics) {
var pools=new ArrayList<Pool>();var content=new ArrayList<Content>();var diagnostics=new ArrayList<>(producerDiagnostics);
var byId=new HashMap<Integer,PackerPackPlanDTO.Bank>();plan.banks().forEach(b->{if(b.assetId()!=null)byId.put(b.assetId(),b);});
for(var type:BankType.values()) {
var banks=plan.banks().stream().filter(b->b.bankType().equals(type.runtimeTag())).toList();
var included=banks.stream().filter(PackerPackPlanDTO.Bank::included).toList();
var preloads=new ArrayList<Preload>();var preloadedIds=new HashSet<Integer>();
for(var entry:plan.preload())if(entry.bankType().equals(type.runtimeTag())) {
var bank=byId.get(entry.assetId());if(bank==null)throw new IllegalArgumentException("Final preload refers to an unknown bank");
preloadedIds.add(entry.assetId());
var dependencies=bank.coldDependencies().stream().map(byId::get).filter(Objects::nonNull).map(b->AssetReference.of(b.reference())).toList();
preloads.add(new Preload(bank(bank),entry.slot(),dependencies));
}
Integer limit=profile.profile()==null?null:profile.profile().limits().get(type);
var pool=new Pool(type,banks.stream().map(AssetBudgetPolicy::bank).toList(),banks.stream().filter(PackerPackPlanDTO.Bank::buildSelected).map(AssetBudgetPolicy::bank).toList(),preloads,
included.stream().filter(b->!preloadedIds.contains(b.assetId())).map(AssetBudgetPolicy::bank).toList(),limit);pools.add(pool);
for(var b:banks) {
Long max=type==BankType.glyph?(long)new DefaultAssetCapacityPolicy().maxArtifactsForGlyphBank(b.metadata()):null;
content.add(new Content(AssetReference.of(b.reference()),type,type==BankType.glyph?"glyphs":"declared_artifacts",b.contentCount(),max));
}
if(pool.exceedsKnownLimit())diagnostics.add(diagnostic("PRELOAD_CAPACITY_EXCEEDED","ERROR",true,"Preload exceeds the qualified "+type+" pool",type,pool,profile));
if(pool.potentialExceedsKnownLimit())diagnostics.add(diagnostic("POTENTIAL_RESIDENCY_EXCEEDED","WARNING",false,"Potential dynamic loads exceed the pool; simultaneous residency is not established",type,pool,profile));
if(profile.state()==QualificationState.known&&limit==null&&pool.requiredSlots()>0)
diagnostics.add(diagnostic("PRELOAD_CAPACITY_UNKNOWN","ERROR",true,"This bank type is not qualified by the selected profile",type,pool,profile));
}
for(var bank:plan.banks())if(bank.included())for(int dependency:bank.coldDependencies()) {
var target=byId.get(dependency);
if(target==null||!target.included())diagnostics.add(new AssetBatches.Diagnostic("packer","SCENE_DEPENDENCY_NOT_IN_BUILD","ERROR","A scene's cold glyph dependency is absent from the pack set",true,
bank.root().toString(),bank.root().toString(),AssetFamily.SCENE_BANK,List.of(),List.of(),List.of(),null,null,null,Map.of("assetId",Objects.requireNonNullElse(bank.assetId(),0),"dependencyAssetId",dependency)));
}
return new Snapshot(revision,profile,content,pools,diagnostics);
}
private static Bank bank(PackerPackPlanDTO.Bank bank){return new Bank(AssetReference.of(bank.reference()),bank.root().toString());}
private static AssetBatches.Diagnostic diagnostic(String code,String severity,boolean blocking,String message,BankType type,Pool pool,ProfileSelection profile) {
var context=new LinkedHashMap<String,Object>();context.put("bankType",type.name());context.put("slots",pool.effectivePreload().stream().map(preload->Map.of("asset",preload.bank().asset().rawValue(),"path",preload.bank().path(),"slot",preload.slot(),"dependencies",preload.dependencies().stream().map(AssetReference::rawValue).toList())).toList());context.put("potentialDynamic",pool.potentialDynamic().stream().map(bank->Map.of("asset",bank.asset().rawValue(),"path",bank.path())).toList());
if(profile.profile()!=null)context.put("profileId",profile.profile().profileId());
return new AssetBatches.Diagnostic("studio",code,severity,message,blocking,null,null,null,List.of(),List.of(),List.of(),(long)pool.requiredSlots(),(long)pool.requiredSlots(),pool.limit()==null?null:pool.limit().longValue(),context);
}
}
@@ -0,0 +1,9 @@
package p.studio.core.assets;
import p.studio.api.assets.AssetBudgets;
import p.studio.api.project.ProjectReference;
/** The shipper and managed launch adapters consume the same qualified asset budget contract. */
public interface AssetBuildPreflight {
AssetBudgets.Snapshot inspect(ProjectReference project);
}
@@ -0,0 +1,83 @@
package p.studio.core.assets;
import p.studio.api.assets.*;
import p.studio.api.automation.*;
import p.studio.api.operation.*;
import p.studio.api.project.*;
import p.studio.core.project.ProjectAccessRegistry;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.*;
/** One admitted service for local human and current-task callers. Persistence owns durable dedup. */
public final class DefaultAssetAuthoringService implements AssetAuthoringService {
private final ProjectAccessRegistry projects;private final ProjectCatalog catalog;
private final AssetBatchWorkspace workspace;private final OperationExecutor executor;
private record Key(Path root,String task,String operation,String request){}
private record Pending(Object parameters,CompletableFuture<Object> result){}
private final Map<Key,Pending> requests=new ConcurrentHashMap<>();
public DefaultAssetAuthoringService(ProjectAccessRegistry projects,ProjectCatalog catalog,AssetBatchWorkspace workspace,OperationExecutor executor) {
this.projects=projects;this.catalog=catalog;this.workspace=workspace;this.executor=executor;
}
private AutomationSession authorize(Path root,String task) {
var session=projects.requireAutomation(projects.requireOpenRoot(root));var snapshot=session.snapshot();
if(snapshot.closed())throw new ControlDeniedException(ControlResult.Status.CLOSED);
if(task!=null&&(snapshot.state()!=ControlState.AGENT_WORKING||snapshot.task()==null||!task.equals(snapshot.task().value())))
throw new ControlDeniedException(ControlResult.Status.INVALID_TASK);
return session;
}
private ProjectReference project(Path root,String task){authorize(root,task);return catalog.openProject(root);}
@Override public List<AssetSummary> catalog(Path root,String task){return workspace.catalog(project(root,task));}
@Override public List<AssetBatches.ArtifactInfo> artifacts(Path root,String task,AssetReference bank){return workspace.artifacts(project(root,task),bank);}
@Override public List<AssetBatches.ArtifactInfo> previewArtifacts(Path root,String task,String batch,AssetReference bank){return workspace.previewArtifacts(project(root,task),batch,bank);}
@Override public AssetDetailsSnapshot inspect(Path root,String task,AssetReference bank){return workspace.inspect(project(root,task),bank);}
@Override public AssetBatches.Revision revision(Path root,String task){return workspace.revision(project(root,task));}
@Override public List<AssetBatches.Ownership> dependencies(Path root,String task,AssetReference asset){return workspace.dependencies(project(root,task),asset);}
@Override public AssetBudgets.Snapshot budget(Path root,String task){return workspace.budget(project(root,task));}
@Override public AssetBatches.Preview preview(Path root,String task,String batch){return workspace.preview(project(root,task),task,batch);}
@Override public AssetBatches.Result result(Path root,String task,String batch){return workspace.result(project(root,task),task,batch);}
@Override public AssetBatches.AuthorizedPng authorizePng(Path root,Path source){return mutate(root,null,()->workspace.authorize(catalog.openProject(root),source));}
@Override public AssetBatches.Preview preview(Path root,String task,String request,AssetBatches.Request changes) {
request(request);return deduplicate(root,task,"preview",request,changes,()->mutate(root,task,()->workspace.preview(catalog.openProject(root),task,request,changes)));
}
@Override public AssetBatches.Result commit(Path root,String task,String request,String batch,AssetBatches.Revision revision) {
request(request);return deduplicate(root,task,"commit",request,List.of(batch,revision),()->mutate(root,task,()->workspace.commit(catalog.openProject(root),task,request,batch,revision)));
}
@SuppressWarnings("unchecked") private <T>T deduplicate(Path root,String task,String operation,String request,Object parameters,Callable<T> work) {
authorize(root,task);var key=new Key(projects.requireOpenRoot(root),task,operation,request);var candidate=new Pending(parameters,new CompletableFuture<>());
var existing=requests.putIfAbsent(key,candidate);
if(existing!=null) {
if(!existing.parameters().equals(parameters))throw new AutomationException(AutomationFailure.Code.CLIENT_REQUEST_CONFLICT,"Asset request parameters changed");
return (T)existing.result().join();
}
try {T value=work.call();candidate.result().complete(value);return value;}
catch(RuntimeException failure){candidate.result().completeExceptionally(failure);requests.remove(key,candidate);throw failure;}
catch(Exception failure){candidate.result().completeExceptionally(failure);requests.remove(key,candidate);throw new IllegalStateException(failure);}
}
private <T>T mutate(Path root,String task,Callable<T> work) {
var session=authorize(root,task);var value=new java.util.concurrent.atomic.AtomicReference<T>();
OperationHandle<?> handle;ManualLease lease=null;
synchronized(session) {
authorize(root,task);
if(task==null)lease=projects.enterManual(root);
try {
Callable<AutomationOutcome> action=()->{value.set(work.call());return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED,0,0,List.of(),null);};
handle=task==null?executor.submit("Asset authoring",action):session.submit(session.snapshot().task(),"Asset authoring",action);
if(lease!=null)lease.onProjectClose(handle::cancel);
}catch(RuntimeException failure){if(lease!=null)lease.close();throw failure;}
}
boolean interrupted=false;
try {
var terminated=handle.termination().toCompletableFuture();
while(true)try{terminated.get();break;}catch(InterruptedException stop){interrupted=true;handle.cancel();}
handle.completion().toCompletableFuture().join();
if(value.get()==null)throw new CancellationException("Asset operation did not publish a result");
return value.get();
}catch(ExecutionException|CompletionException failure){
if(value.get() instanceof AssetBatches.Result result&&result.applied())return value.get();
Throwable cause=failure.getCause();while(cause instanceof CompletionException&&cause.getCause()!=null)cause=cause.getCause();
if(cause instanceof RuntimeException domain)throw domain;throw new IllegalStateException("Asset operation failed",cause);
}finally{if(lease!=null)lease.close();if(interrupted)Thread.currentThread().interrupt();}
}
private static void request(String request){if(request==null||request.isBlank()||request.length()>256)throw new IllegalArgumentException("Bounded requestId required");}
}
@@ -32,7 +32,7 @@ public final class PackerAssetCatalog implements AssetCatalog {
final var context = new p.packer.messages.PackerProjectContext(
projectReference.name(),
projectReference.rootPath());
return packer.listAssets(new ListAssetsRequest(context, deepSync)).assets().stream()
return packer.listAssets(new ListAssetsRequest(context, deepSync, true)).assets().stream()
.map(summary -> new AssetSummary(
AssetReference.of(summary.assetReference().value()),
summary.identity().assetName(),
@@ -19,16 +19,25 @@ import java.util.stream.Collectors;
public final class PackerAssetDetails implements AssetDetails {
private final PackerWorkspaceService packer;
private final boolean initializeIdentity;
public PackerAssetDetails(PackerWorkspaceService packer) {
this(packer,true);
}
public PackerAssetDetails(PackerWorkspaceService packer,boolean initializeIdentity) {
this.packer = Objects.requireNonNull(packer, "packer");
this.initializeIdentity=initializeIdentity;
}
@Override
public AssetDetailsSnapshot get(ProjectReference project, AssetReference assetReference) {
return packer.withProject(new PackerProjectContext(project.name(),project.rootPath()),()->getInLane(project,assetReference));
}
private AssetDetailsSnapshot getInLane(ProjectReference project,AssetReference assetReference) {
final var context = new PackerProjectContext(project.name(), project.rootPath());
final var reference = p.packer.messages.AssetReference.of(assetReference.value());
final var detailsResult = packer.getAssetDetails(new GetAssetDetailsRequest(context, reference));
final var detailsResult = packer.getAssetDetails(new GetAssetDetailsRequest(context, reference,initializeIdentity));
final var actionsResult = packer.getAssetActions(new GetAssetActionsRequest(context, reference));
final var details = detailsResult.details();
final var codecFields = details.codecConfigurationFieldsByCodec().entrySet().stream()
@@ -80,7 +89,7 @@ public final class PackerAssetDetails implements AssetDetails {
private static AssetDiagnostic diagnostic(PackerDiagnosticDTO diagnostic) {
return new AssetDiagnostic(
diagnostic.severity().name(), diagnostic.category().name(), diagnostic.message(),
diagnostic.evidencePath(), diagnostic.blocking());
diagnostic.evidencePath(), diagnostic.blocking(),"packer",diagnostic.code(),diagnostic.context());
}
private static AssetBankComposition bank(PackerBankCompositionDetailsDTO bank) {
@@ -16,6 +16,8 @@ public final class PackerAssetPacking implements AssetPacking {
this.access = java.util.Objects.requireNonNull(access);
}
private AssetBuildPreflight preflight;
public PackerAssetPacking withAssetPreflight(AssetBuildPreflight value){preflight=java.util.Objects.requireNonNull(value);return this;}
private final PackerWorkspaceService packer;
private final OperationExecutor operations;
@@ -62,7 +64,7 @@ public final class PackerAssetPacking implements AssetPacking {
asset.assetId(), asset.assetName(), asset.assetPath(), asset.lastModified(),
asset.diagnostics().stream()
.map(diagnostic -> new AssetPackDiagnostic(
diagnostic.severity().name(), diagnostic.message(), diagnostic.blocking()))
diagnostic.severity().name(), diagnostic.message(), diagnostic.blocking(),"packer",diagnostic.code(),diagnostic.evidencePath(),diagnostic.context()))
.toList()))
.toList());
}
@@ -73,16 +75,20 @@ public final class PackerAssetPacking implements AssetPacking {
}
private AssetPackExecution packNow(ProjectReference project) {
final var result = packer.packWorkspace(new PackWorkspaceRequest(context(project)));
var budget=preflight==null?null:preflight.inspect(project);
if(budget!=null&&!budget.buildAllowed())return new AssetPackExecution(AssetOperationStatus.FAILED,"Asset capacity validation rejected publication","assets.pa",0,0,java.util.List.of(),budget.diagnostics().stream().map(PackerAssetPacking::diagnostic).toList());
var contract=budget==null||budget.qualification().profile()==null?null:new PackCapacityContract(budget.qualification().profile().profileId(),budget.qualification().selectedExecutableSha256(),budget.qualification().profile().limits().entrySet().stream().collect(java.util.stream.Collectors.toMap(e->e.getKey().runtimeTag(),java.util.Map.Entry::getValue)));
final var result = packer.packWorkspace(new PackWorkspaceRequest(context(project),null,contract));
return new AssetPackExecution(
status(result.status()), result.summary(), result.result().canonicalArtifactName(),
result.result().packedAssetCount(), result.result().elapsedMillis(),
result.result().emittedArtifacts().stream()
.map(artifact -> new AssetEmittedArtifact(
artifact.label(), artifact.path(), artifact.canonical(), artifact.sizeBytes()))
.toList());
.toList(),result.diagnostics().stream().map(d->new AssetPackDiagnostic(d.severity().name(),d.message(),d.blocking(),"packer",d.code(),d.evidencePath(),d.context())).toList());
}
private static AssetPackDiagnostic diagnostic(AssetBatches.Diagnostic d){var facts=new java.util.LinkedHashMap<>(d.context());if(d.used()!=null)facts.put("used",d.used());if(d.required()!=null)facts.put("required",d.required());if(d.limit()!=null)facts.put("limit",d.limit());return new AssetPackDiagnostic(d.severity(),d.message(),d.blocking(),d.producer(),d.code(),d.path()==null?null:java.nio.file.Path.of(d.path()),facts);}
private static PackerProjectContext context(ProjectReference project) {
return new PackerProjectContext(project.name(), project.rootPath());
}
@@ -0,0 +1,17 @@
package p.studio.core.assets;
import p.packer.PackerWorkspaceService;
import p.packer.messages.PackerProjectContext;
import java.nio.file.*;
import java.util.concurrent.Callable;
/** Routes sidecar readers/writers through the same recoverable project boundary as packer readers. */
public final class ProjectAssetLane {
private ProjectAssetLane(){}
public static <T>T execute(PackerWorkspaceService packer,Path path,Callable<T> work) {
Path root=path.toAbsolutePath().normalize();
while(root!=null&&!Files.isRegularFile(root.resolve("prometeu.json")))root=root.getParent();
if(packer!=null&&root!=null)return packer.withProject(new PackerProjectContext(root.getFileName().toString(),root),work);
try{return work.call();}catch(RuntimeException domain){throw domain;}catch(Exception error){throw new IllegalStateException(error);}
}
}
@@ -18,6 +18,10 @@ import java.util.Objects;
import java.util.Set;
public final class AssetStudioMetadataService implements AssetMetadataService {
private p.packer.PackerWorkspaceService packer;
public AssetStudioMetadataService(ObjectMapper mapper,p.studio.api.automation.ProjectAccess access,p.packer.PackerWorkspaceService packer) {
this(mapper,access);this.packer=packer;
}
private p.studio.api.automation.ProjectAccess access = new p.studio.core.project.ProjectAccessRegistry();
public AssetStudioMetadataService(ObjectMapper mapper, p.studio.api.automation.ProjectAccess access) {
this(mapper);
@@ -40,6 +44,10 @@ public final class AssetStudioMetadataService implements AssetMetadataService {
@Override
public AssetStudioMetadataSnapshot read(Path assetRoot, AssetFamily assetFamily) {
return p.studio.core.assets.ProjectAssetLane.execute(packer,assetRoot,()->readInLane(assetRoot,assetFamily));
}
private AssetStudioMetadataSnapshot readInLane(Path assetRoot,AssetFamily assetFamily) {
final Path normalizedAssetRoot = Objects.requireNonNull(assetRoot, "assetRoot").toAbsolutePath().normalize();
return new AssetStudioMetadataSnapshot(
readGlyphSpecialization(normalizedAssetRoot, assetFamily),
@@ -49,6 +57,10 @@ public final class AssetStudioMetadataService implements AssetMetadataService {
@Override
public AssetMetadataWriteResult writeSceneBankMetadata(Path assetRoot, AssetStudioSceneBankMetadata metadata) {
return p.studio.core.assets.ProjectAssetLane.execute(packer,assetRoot,()->writeSceneBankMetadataInLane(assetRoot,metadata));
}
private AssetMetadataWriteResult writeSceneBankMetadataInLane(Path assetRoot,AssetStudioSceneBankMetadata metadata) {
try (p.studio.api.automation.ManualLease accessLease = access.enterManual(assetRoot)) {
try {
writeSceneBankMetadataFile(assetRoot, metadata);
@@ -99,6 +111,10 @@ public final class AssetStudioMetadataService implements AssetMetadataService {
@Override
public AssetMetadataWriteResult writeGlyphSpecialization(Path assetRoot, AssetStudioGlyphSpecialization specialization) {
return p.studio.core.assets.ProjectAssetLane.execute(packer,assetRoot,()->writeGlyphSpecializationInLane(assetRoot,specialization));
}
private AssetMetadataWriteResult writeGlyphSpecializationInLane(Path assetRoot,AssetStudioGlyphSpecialization specialization) {
try (p.studio.api.automation.ManualLease accessLease = access.enterManual(assetRoot)) {
try {
writeGlyphSpecializationFile(assetRoot, specialization);
@@ -129,6 +145,10 @@ public final class AssetStudioMetadataService implements AssetMetadataService {
@Override
public AssetMetadataWriteResult writeGlyphMaterialMetadata(Path assetRoot, AssetStudioGlyphMaterialMetadata metadata) {
return p.studio.core.assets.ProjectAssetLane.execute(packer,assetRoot,()->writeGlyphMaterialMetadataInLane(assetRoot,metadata));
}
private AssetMetadataWriteResult writeGlyphMaterialMetadataInLane(Path assetRoot,AssetStudioGlyphMaterialMetadata metadata) {
try (p.studio.api.automation.ManualLease accessLease = access.enterManual(assetRoot)) {
try {
writeGlyphMaterialMetadataFile(assetRoot, metadata);
@@ -164,7 +164,15 @@ public final class SceneBankWorkflowService implements SceneBankWorkflow {
}
private ScenePackRequest buildCanonicalRequest(ProjectReference projectReference, AssetWorkspaceAssetDetails details) throws IOException {
final AssetStudioSceneBankMetadata metadata = Objects.requireNonNull(details.sceneBankMetadata(), "sceneBankMetadata");
return buildCanonicalRequest(projectReference,Objects.requireNonNull(details.sceneBankMetadata(), "sceneBankMetadata"));
}
/** Shared canonical scene emitter; the caller owns authority and staged publication. */
public void writeComposition(ProjectReference project,Path root,AssetStudioSceneBankMetadata metadata)throws IOException {
writeAcceptedRequest(root.resolve(ACCEPTED_REQUEST_FILE),buildCanonicalRequest(project,metadata));
}
private ScenePackRequest buildCanonicalRequest(ProjectReference projectReference, AssetStudioSceneBankMetadata metadata) throws IOException {
final SceneLayerRuntimeDefaults sceneDefaults =
resolveSceneLayerRuntimeDefaults(projectReference, metadata.layerBindings());
final List<ScenePackLayerRequest> layers = new ArrayList<>();
@@ -58,7 +58,7 @@ final class PlayStopExecutionController implements ExecutionController {
}
if (manualLease != null) throw new p.studio.api.automation.ControlDeniedException(
p.studio.api.automation.ControlResult.Status.BUSY);
manualLease = access.enterManual(project.rootPath());
manualLease = access.enterExecution(project.rootPath());
manualLease.onProjectClose(this::stop);
stopRequested = false;
session.beginPreparationCycle();
@@ -1,7 +1,7 @@
package p.studio.core.language;
import p.studio.api.automation.*;
import p.studio.utilities.json.ImmutableJson;
import p.studio.api.values.ValueMaps;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
@@ -17,7 +17,7 @@ public final class LanguageQueryExecution {
private volatile boolean cancelled, cleanupFailed;
private volatile Map<String, Object> context = Map.of();
public void correlate(Map<String, Object> additions) {
var merged = new LinkedHashMap<>(context); merged.putAll(additions); context = ImmutableJson.object(merged);
var merged = new LinkedHashMap<>(context); merged.putAll(additions); context = ValueMaps.object(merged);
}
public LanguageQueryExecution(String operation, long executionMillis, long cleanupMillis) {
@@ -63,7 +63,7 @@ public final class LanguageQueryExecution {
var failure = primary.get();
if (failure == null) { record(fallback, message, Map.of()); failure = primary.get(); }
var details = new LinkedHashMap<>(failure.details()); details.putAll(context);
details.putAll(ImmutableJson.fields("component", "lsp", "operation", operation, "method", method,
details.putAll(ValueMaps.fields("component", "lsp", "operation", operation, "method", method,
"queryId", queryId, "executionBudgetMs", executionMillis, "cleanupBudgetMs", cleanupMillis,
"workerTerminated", workerTerminated, "cleanup", cleanupFailed ? "failed" : workerTerminated ? "terminated" : cleanup));
return new AutomationException(new AutomationFailure(failure.code(), failure.message(), failure.runtimeError(), details));
@@ -31,14 +31,14 @@ public final class TaskLanguageQueries implements LanguageQueries {
}
var result = new AtomicReference<Map<String, Object>>();
var execution = new LanguageQueryExecution(operation, executionMillis, cleanupMillis);
execution.correlate(p.studio.utilities.json.ImmutableJson.fields("projectRoot", task.projectRoot().toString(), "taskId", task.value()));
execution.correlate(p.studio.api.values.ValueMaps.fields("projectRoot", task.projectRoot().toString(), "taskId", task.value()));
var handle = session.submit(task, "LSP " + operation, () -> {
var reply = language.query(task, operation, parameters, execution);
if (execution.hasFailure()) throw execution.exception(AutomationFailure.Code.LANGUAGE_QUERY_INTERNAL_FAILURE, "Language query failed", false);
result.set(reply);
Map<String, Object> view = Map.of();
if (reply.get("analysis") instanceof Map<?, ?> analysis && analysis.get("summary") != null) {
view = p.studio.utilities.json.ImmutableJson.fields("issues", analysis.getOrDefault("issues", null),
view = p.studio.api.values.ValueMaps.fields("issues", analysis.getOrDefault("issues", null),
"summary", analysis.get("summary"), "context", reply.getOrDefault("context", Map.of()));
}
return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, List.of(), null, view);
@@ -55,7 +55,7 @@ public final class TaskLanguageQueries implements LanguageQueries {
var correlated = new LinkedHashMap<String, Object>(); context.forEach((key, value) -> correlated.put((String) key, value));
correlated.put("operationId", handle.id().value()); reply.put("context", correlated);
}
return p.studio.utilities.json.ImmutableJson.object(reply);
return p.studio.api.values.ValueMaps.object(reply);
} catch (ExecutionException failure) {
awaitCleanup(handle, execution);
if (failure.getCause() instanceof AutomationException domain) {
@@ -0,0 +1,40 @@
package p.studio.core.project;
import java.nio.file.*;
import java.net.URI;
import java.util.*;
import p.studio.api.diagnostic.DiagnosticSources.Source;
/** Read-only navigation: admission and revision checks precede any range selection. */
public final class DiagnosticNavigation {
private DiagnosticNavigation() {}
public static Source read(Path root, Map<String, Object> origin) {
try {
if (!(origin.get("uri") instanceof String uri) || !uri.startsWith("file:"))
return new Source("Source unavailable; no admitted physical URI", null, "", 0, 0);
var path = Path.of(URI.create(uri)).toRealPath();
if (!path.startsWith(root.toRealPath()) || !Files.isRegularFile(path))
return new Source("Source unavailable outside project admission", null, "", 0, 0);
byte[] bytes;
try (var input = Files.newInputStream(path)) { bytes = input.readNBytes(4 * 1024 * 1024 + 1); }
if (bytes.length > 4 * 1024 * 1024) return new Source("Source exceeds navigation limit", null, "", 0, 0);
var text = java.nio.charset.StandardCharsets.UTF_8.newDecoder().decode(java.nio.ByteBuffer.wrap(bytes)).toString();
var digest = HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(bytes));
if (origin.get("source") instanceof Map<?, ?> identity && "mixed".equals(identity.get("revisionAvailability")))
return new Source("Mixed analysis revisions; showing current source read-only", path, text, 0, 0);
if (!digest.equals(origin.get("revision")))
return new Source(origin.get("revision") == null ? "Revision unavailable; showing current source read-only" : "Stale diagnostic; showing changed source read-only", path, text, 0, 0);
if (!(origin.get("range") instanceof Map<?, ?> range)) return new Source("Current source; diagnostic has no range", path, text, 0, 0);
int start = offset(text, (Map<?, ?>) range.get("start")), end = offset(text, (Map<?, ?>) range.get("end"));
if (start < 0 || end < start) return new Source("Current source; range unavailable", path, text, 0, 0);
return new Source("Current source · read-only", path, text, start, end);
} catch (Exception unavailable) { return new Source("Source unavailable", null, "", 0, 0); }
}
private static int offset(String text, Map<?, ?> position) {
if (position == null || !(position.get("line") instanceof Number line) || !(position.get("character") instanceof Number character)) return -1;
int start = 0;
for (int i = 0; i < line.intValue(); i++) { start = text.indexOf('\n', start); if (start < 0) return -1; start++; }
int end = text.indexOf('\n', start); if (end < 0) end = text.length();
return character.intValue() < 0 || start + character.intValue() > end ? -1 : start + character.intValue();
}
}
@@ -34,12 +34,16 @@ public final class ProjectAccessRegistry implements ProjectAccess {
}
public p.studio.api.automation.AutomationSession requireAutomation(Path root) {
return findAutomation(root).orElseThrow(() -> new AutomationException(
AutomationFailure.Code.PROJECT_NOT_OPEN, "No open project automation session"));
}
/** Read-only project context can exist without an automation service in embedded consumers. */
public java.util.Optional<p.studio.api.automation.AutomationSession> findAutomation(Path root) {
Path key = requireOpenRoot(root);
synchronized (this) {
var entry = projects.get(key);
if (entry == null || entry.automation() == null)
throw new AutomationException(AutomationFailure.Code.PROJECT_NOT_OPEN, "No open project automation session");
return entry.automation();
return entry == null ? java.util.Optional.empty() : java.util.Optional.ofNullable(entry.automation());
}
}
@@ -69,6 +73,21 @@ public final class ProjectAccessRegistry implements ProjectAccess {
// Project creation/setup before opening a session is still a manual workflow.
return authority == null ? () -> {} : authority.get();
}
@Override public ManualLease enterExecution(Path path) {
java.util.Optional<p.studio.api.automation.AutomationSession> automation;
try { automation=findAutomation(path); }
catch(AutomationException unavailable) {
if(unavailable.failure().code()!=AutomationFailure.Code.PROJECT_NOT_OPEN)throw unavailable;
return enterManual(path);
}
if(automation.isEmpty())return enterManual(path);
var session=automation.get();
synchronized(session) {
if(session.snapshot().manualOperations()!=0||session.runtimeAttached())
throw new p.studio.api.automation.ControlDeniedException(p.studio.api.automation.ControlResult.Status.BUSY);
return enterManual(path);
}
}
private static Path canonical(Path path) {
Path normalized = path.toAbsolutePath().normalize();
@@ -0,0 +1,17 @@
package p.studio.core.runtime;
import p.studio.api.automation.*;
import p.studio.api.project.ProjectReference;
import java.nio.file.Path;
import java.util.Map;
/** Application IO over the common manifest parser, isolated run files and existing evidence store. */
public interface GuestTestSupport {
GuestTests.Catalog targets(ProjectReference project);
GuestTests.Target targetAtBuild(ProjectReference project, p.studio.api.shipper.ShipperPrepareResult build, String targetId) throws Exception;
long applicationId(p.studio.api.shipper.ShipperPrepareResult build) throws Exception;
Map<String,Object> media(ProjectReference project, String runId, GuestTestTools.Request request) throws Exception;
void completed(ProjectReference project, String runId, GuestTests.RunResult result) throws Exception;
boolean admitsCheckpoint(Path root, String artifactId);
String retainCapture(Path root, String runId, String operationId, String requestId, CanonicalCapture capture) throws Exception;
}
@@ -9,13 +9,30 @@ import java.util.function.Consumer;
/** Neutral runtime port. The process adapter owns protocol, effective limits and supervision. */
public interface HeadlessGateway {
Session boot(ProjectReference project, String buildId, ShipperPrepareResult build) throws Exception;
/** Must negotiate log_read before executing any test guest. Each call owns a fresh process. */
default Session bootTest(ProjectReference project, String buildId, ShipperPrepareResult build, Map<String, Object> options) throws Exception {
throw new AutomationException(AutomationFailure.Code.RUNTIME_INCOMPATIBLE, "Structured guest tests require negotiated log_read");
}
default Session boot(ProjectReference project, String buildId, ShipperPrepareResult build, Map<String, Object> options) throws Exception {
if (!options.isEmpty()) throw new AutomationException(AutomationFailure.Code.RUNTIME_INCOMPATIBLE, "Runtime boot options unavailable");
return boot(project, buildId, build);
}
interface Session extends ManagedHeadlessSession {
default GuestTests.LogPage logs(java.math.BigInteger afterSequence, int limit) throws Exception {
throw new AutomationException(AutomationFailure.Code.RUNTIME_INCOMPATIBLE, "Native log reads are unavailable");
}
long maxFramesPerAdvance();
/** The state already confirmed by boot; do not execute a second command to manufacture it. */
default TaskTools.HeadlessReply bootReply() {
var observed = observation();
var values = new java.util.LinkedHashMap<String, Object>();
values.put("sessionId", sessionId()); values.put("buildId", observed.buildId());
for (var key : java.util.List.of("state", "runtimeVersion", "limits", "capabilities"))
if (observed.nativeDetails().containsKey(key)) values.put(key, observed.nativeDetails().get(key));
values.put("observation", observed);
return new TaskTools.HeadlessReply(values, null);
}
TaskTools.HeadlessReply call(TaskTools.HeadlessAction action, Map<String, Object> input) throws Exception;
AutomationOutcome advance(long frames, Consumer<TrackedOperation.Progress> progress) throws Exception;
default java.util.concurrent.CompletionStage<TaskTools.HeadlessReply> stop(Map<String, Object> policy) {
@@ -0,0 +1,46 @@
package p.studio.core.session;
import p.studio.api.automation.Evidence;
import java.util.*;
/** Deterministic channel comparison in original top-left coordinates; no image toolkit. */
public final class CheckpointComparison {
public record Result(Evidence.Parameters parameters, Evidence.Verdict verdict, long comparedPixels,
long differentPixels, int maximumChannelError, Evidence.Rect boundingBox, Evidence.Pixels diff) {}
public Result compare(Evidence.Pixels expected, Evidence.Pixels actual, Evidence.Parameters p) {
Objects.requireNonNull(p);
var roi = p.roi() == null ? new Evidence.Rect(0,0,expected.width(),expected.height()) : p.roi();
roi.requireInside(expected.width(),expected.height());
p.masks().forEach(r -> r.requireInside(expected.width(),expected.height()));
var normalized = new Evidence.Parameters(roi,p.masks(),p.alpha(),p.channelLimit(),p.allowedDifferentPixels());
long eligible = 0;
for (int y=roi.y(); y<(long)roi.y()+roi.height(); y++) {
cancelled();
for (int x=roi.x(); x<(long)roi.x()+roi.width(); x++) if (!masked(p.masks(),x,y)) eligible++;
}
if (eligible == 0) throw new IllegalArgumentException("Comparison region contains no pixels");
byte[] a=expected.rgba(), b=actual.rgba(), diff=new byte[a.length];
if (expected.width()!=actual.width() || expected.height()!=actual.height())
return new Result(normalized,Evidence.Verdict.dimension_mismatch,0,0,0,null,new Evidence.Pixels(expected.width(),expected.height(),diff));
long compared=0,different=0; int max=0,minX=Integer.MAX_VALUE,minY=Integer.MAX_VALUE,maxX=-1,maxY=-1;
for (int y=0;y<expected.height();y++) for (int x=0;x<expected.width();x++) {
if (x==0) cancelled();
int index=(y*expected.width()+x)*4;
if (!roi.contains(x,y)||masked(p.masks(),x,y)) continue; // excluded: transparent black
compared++; int error=0;
for (int c=0;c<(p.alpha()==Evidence.Alpha.compare?4:3);c++)
error=Math.max(error,Math.abs(Byte.toUnsignedInt(a[index+c])-Byte.toUnsignedInt(b[index+c])));
max=Math.max(max,error);
diff[index+3]=(byte)255;
if (error>p.channelLimit()) {
different++; minX=Math.min(minX,x);minY=Math.min(minY,y);maxX=Math.max(maxX,x);maxY=Math.max(maxY,y);
diff[index]=(byte)255;diff[index+1]=(byte)error; // differing: opaque red/yellow
} else {diff[index+1]=(byte)96;} // compared within tolerance: opaque green
}
return new Result(normalized,different<=p.allowedDifferentPixels()?Evidence.Verdict.passed:Evidence.Verdict.rejected,
compared,different,max,different==0?null:new Evidence.Rect(minX,minY,maxX-minX+1,maxY-minY+1),
new Evidence.Pixels(expected.width(),expected.height(),diff));
}
private static void cancelled() { if (Thread.currentThread().isInterrupted()) throw new java.util.concurrent.CancellationException("Comparison cancelled before publication"); }
private static boolean masked(List<Evidence.Rect> masks,int x,int y) { return masks.stream().anyMatch(r->r.contains(x,y)); }
}
@@ -27,6 +27,10 @@ public final class DefaultAutomationSession implements AutomationSession {
private CanonicalCapture capture;
private List<String> diagnostics = List.of();
private Map<String, Object> diagnosticResult = Map.of();
private ControlObservation.Cleanup cleanupState = ControlObservation.Cleanup.NOT_REQUIRED;
private AutomationFailure cleanupFailure;
private RuntimeObservation lastRuntimeObservation = RuntimeObservation.absent();
private RuntimeObservation operationObservation;
private CompletableFuture<ControlResult> returning;
private final List<Runnable> manualCancellation = new ArrayList<>();
private final List<Runnable> taskCleanup = new ArrayList<>();
@@ -80,8 +84,13 @@ public final class DefaultAutomationSession implements AutomationSession {
@Override public synchronized AutomationSnapshot snapshot() {
return new AutomationSnapshot(root, state, handoffPending, closed, manualOperations, task,
active == null ? null : active.snapshot(), capture, stale, diagnostics, List.copyOf(calls), diagnosticResult);
active == null ? null : active.snapshot(), capture, stale, diagnostics, List.copyOf(calls), diagnosticResult,
(runtime == null ? lastRuntimeObservation : runtime.observation()).withContext(
p.studio.api.values.ValueMaps.fields("projectRoot", root.toString(), "taskId", task == null ? null : task.value())),
controlObservation(), operationObservation);
}
@Override public synchronized boolean runtimeAttached() { return runtime != null; }
private ControlObservation controlObservation() { return new ControlObservation(state, cleanupState, task == null, closed, cleanupFailure); }
@Override public OperationSubscription subscribe(Consumer<AutomationSnapshot> listener) {
listeners.add(listener); notifyListener(listener, snapshot());
@@ -98,7 +107,7 @@ public final class DefaultAutomationSession implements AutomationSession {
final List<DocumentHandoff> participants;
synchronized (this) {
if (closed) return result(ControlResult.Status.CLOSED, "Project closed");
if (state != ControlState.MANUAL || handoffPending || manualOperations != 0)
if (state != ControlState.MANUAL || handoffPending || manualOperations != 0 || runtime != null)
return result(ControlResult.Status.BUSY, "Manual work or another control transition is active");
handoffPending = true; attempt = ++generation;
participants = List.copyOf(documents); publish();
@@ -117,7 +126,7 @@ public final class DefaultAutomationSession implements AutomationSession {
if (generation != attempt) return ControlResult.of(ControlResult.Status.CLOSED, "Grant invalidated");
handoffPending = false;
diagnostics = outcome.success() ? List.of() : List.of(outcome.diagnostic());
publish(); return outcome;
publish(); return outcome.withObservation(controlObservation());
}
});
}
@@ -139,6 +148,7 @@ public final class DefaultAutomationSession implements AutomationSession {
prepared.activate();
connection = prepared; committed = true;
state = ControlState.WAITING_FOR_AGENT;
cleanupState = ControlObservation.Cleanup.NOT_REQUIRED; cleanupFailure = null;
record("grant");
return ControlResult.of(ControlResult.Status.GRANTED, "");
}
@@ -164,12 +174,12 @@ public final class DefaultAutomationSession implements AutomationSession {
}
@Override public synchronized ControlResult acquire() {
if (closed) return ControlResult.of(ControlResult.Status.CLOSED, "Project closed");
if (state != ControlState.WAITING_FOR_AGENT || handoffPending) return ControlResult.of(ControlResult.Status.NOT_GRANTED, "Human grant required");
if (closed) return ControlResult.of(ControlResult.Status.CLOSED, "Project closed").withObservation(controlObservation());
if (state != ControlState.WAITING_FOR_AGENT || handoffPending) return ControlResult.of(ControlResult.Status.NOT_GRANTED, "Human grant required").withObservation(controlObservation());
task = new TaskIdentity(root, UUID.randomUUID().toString());
diagnosticResult = Map.of(); diagnostics = List.of();
state = ControlState.AGENT_WORKING; record("acquire"); publish();
return new ControlResult(ControlResult.Status.ACQUIRED, task, "");
return new ControlResult(ControlResult.Status.ACQUIRED, task, "", controlObservation());
}
@Override public CompletionStage<ControlResult> release(TaskIdentity identity) {
@@ -209,6 +219,7 @@ public final class DefaultAutomationSession implements AutomationSession {
if (returning != null && !returning.isDone()) return returning;
if (!closing && state == ControlState.MANUAL && !handoffPending) return result(ControlResult.Status.RETURNED, "");
state = ControlState.RETURNING_CONTROL; task = null; generation++; handoffPending = false;
cleanupState = ControlObservation.Cleanup.PENDING; cleanupFailure = null;
record(closing ? "close" : "reclaim"); stale = capture != null;
result = returning = new CompletableFuture<>();
operation = active; ownedRuntime = runtime; idle = manualIdle; publisherIdle = publicationIdle;
@@ -242,12 +253,19 @@ public final class DefaultAutomationSession implements AutomationSession {
synchronized (this) {
if (failure == null) {
taskCleanup.removeAll(cleanups);
if (ownedRuntime != null) lastRuntimeObservation = ownedRuntime.observation();
runtime = null; busy = false; state = ControlState.MANUAL;
cleanupState = ControlObservation.Cleanup.COMPLETED;
diagnostics = List.of(); diagnosticResult = Map.of(); publish();
result.complete(ControlResult.of(ControlResult.Status.RETURNED, ""));
result.complete(ControlResult.of(ControlResult.Status.RETURNED, "").withObservation(controlObservation()));
} else {
cleanupState = ControlObservation.Cleanup.FAILED;
var cause = failure;
while (cause instanceof CompletionException && cause.getCause() != null) cause = cause.getCause();
cleanupFailure = cause instanceof AutomationException domain ? domain.failure()
: new AutomationFailure(AutomationFailure.Code.CLEANUP_FAILED, message(cause), Map.of(), Map.of("phase", "cleanup"));
diagnostics = List.of(message(failure)); publish();
result.complete(ControlResult.of(ControlResult.Status.CLEANUP_FAILED, message(failure)));
result.complete(ControlResult.of(ControlResult.Status.CLEANUP_FAILED, message(failure)).withObservation(controlObservation()));
}
}
});
@@ -261,8 +279,11 @@ public final class DefaultAutomationSession implements AutomationSession {
busy = true; record(summary);
diagnosticResult = Map.of();
var finalResult = new java.util.concurrent.atomic.AtomicReference<AutomationOutcome>();
var failedObservation = new java.util.concurrent.atomic.AtomicReference<RuntimeObservation>();
try { active = executor.submit(summary, () -> {
var result = work.call();
final AutomationOutcome result;
try { result = work.call(); }
catch (AutomationException failure) { failedObservation.set(failure.failure().observation()); throw failure; }
finalResult.set(result);
// Cancellation acknowledgement can precede a cooperative final result. Publish
// its known frame before worker termination, but never after task revocation.
@@ -270,7 +291,7 @@ public final class DefaultAutomationSession implements AutomationSession {
if (valid(identity)) {
diagnostics = result.diagnostics();
if (result.capture() != null && runtime != null && runtime.sessionId().equals(result.capture().sessionId())) {
capture = result.capture(); stale = false;
capture = result.capture(); stale = result.observation() != null && result.observation().captureStale();
}
publish();
}
@@ -286,7 +307,7 @@ public final class DefaultAutomationSession implements AutomationSession {
diagnostics = failure != null ? List.of(message(failure)) : outcome.diagnostics();
if (outcome != null && outcome.capture() != null && runtime != null
&& runtime.sessionId().equals(outcome.capture().sessionId())) {
capture = outcome.capture(); stale = false;
capture = outcome.capture(); stale = outcome.observation() != null && outcome.observation().captureStale();
}
publish();
}
@@ -296,6 +317,14 @@ public final class DefaultAutomationSession implements AutomationSession {
synchronized (this) { if (active == operation) {
busy = false;
var result = finalResult.get();
if (valid(identity)) {
var observed = result != null && result.observation() != null ? result.observation()
: failedObservation.get() != null ? failedObservation.get()
: runtime == null ? lastRuntimeObservation : runtime.observation();
if (runtime == null && failedObservation.get() != null) lastRuntimeObservation = observed;
operationObservation = observed.withContext(p.studio.api.values.ValueMaps.fields(
"projectRoot", root.toString(), "taskId", identity.value(), "operationId", operation.id().value()));
}
if (valid(identity) && result != null && !result.diagnosticResult().isEmpty()) {
var view = new LinkedHashMap<>(result.diagnosticResult());
if (operation.snapshot().state() == OperationState.CANCELLED) {
@@ -306,7 +335,7 @@ public final class DefaultAutomationSession implements AutomationSession {
context.put("operationId", operation.id().value());
if (operation.snapshot().state() == OperationState.CANCELLED && context.containsKey("buildId")) context.put("buildId", null);
view.put("context", context);
diagnosticResult = p.studio.utilities.json.ImmutableJson.object(view);
diagnosticResult = p.studio.api.values.ValueMaps.object(view);
}
publish();
} }
@@ -325,16 +354,23 @@ public final class DefaultAutomationSession implements AutomationSession {
}
@Override public synchronized void attachRuntime(TaskIdentity identity, ManagedHeadlessSession next) {
requireTask(identity);
requireRuntimeAuthority(identity);
if (runtime != null) throw new ControlDeniedException(ControlResult.Status.BUSY);
runtime = Objects.requireNonNull(next); stale = capture != null; record("runtime " + next.sessionId()); publish();
}
@Override public synchronized void markCaptureStale() { stale = capture != null; publish(); }
@Override public synchronized void runtimeObserved(TaskIdentity identity) { if (valid(identity)) publish(); }
@Override public synchronized void detachRuntime(TaskIdentity identity, ManagedHeadlessSession previous) {
requireTask(identity);
requireRuntimeAuthority(identity);
if (runtime != previous) throw new IllegalStateException("Runtime ownership changed");
lastRuntimeObservation = previous.observation();
runtime = null; stale = capture != null; publish();
}
private void requireRuntimeAuthority(TaskIdentity identity) {
if (identity != null) requireTask(identity);
else if (closed || state != ControlState.MANUAL || manualOperations == 0)
throw new ControlDeniedException(ControlResult.Status.BUSY);
}
private boolean valid(TaskIdentity identity) { return !closed && state == ControlState.AGENT_WORKING && task != null && task.equals(identity); }
private void requireTask(TaskIdentity identity) { if (!valid(identity)) throw new ControlDeniedException(ControlResult.Status.INVALID_TASK); }
private void record(String call) { calls.addLast(Objects.requireNonNullElse(call, "")); while (calls.size() > 50) calls.removeFirst(); }
@@ -342,8 +378,8 @@ public final class DefaultAutomationSession implements AutomationSession {
private static void notifyListener(Consumer<AutomationSnapshot> listener, AutomationSnapshot value) {
try { listener.accept(value); } catch (RuntimeException ignored) { /* Observation cannot change authority. */ }
}
private static CompletionStage<ControlResult> result(ControlResult.Status status, String text) {
return CompletableFuture.completedFuture(ControlResult.of(status, text));
private synchronized CompletionStage<ControlResult> result(ControlResult.Status status, String text) {
return CompletableFuture.completedFuture(ControlResult.of(status, text).withObservation(controlObservation()));
}
private static String message(Throwable failure) {
while (failure instanceof CompletionException && failure.getCause() != null) failure = failure.getCause();
@@ -0,0 +1,153 @@
package p.studio.core.session;
import p.studio.api.automation.*;
import p.studio.api.values.ValueMaps;
import p.studio.core.project.ProjectAccessRegistry;
import java.nio.file.Path;
import java.time.Instant;
import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
/** Shared checkpoint service. Mutations use the project's existing authority and worker. */
public final class DefaultEvidenceTools implements EvidenceTools {
private final ProjectAccessRegistry projects;
private final TaskTools tasks;
private final EvidenceStore store;
private final EvidenceStore.Codec codec;
private final CheckpointComparison comparison=new CheckpointComparison();
public DefaultEvidenceTools(ProjectAccessRegistry projects,TaskTools tasks,EvidenceStore store,EvidenceStore.Codec codec) {
this.projects=projects;this.tasks=tasks;this.store=store;this.codec=codec;
}
@Override public Evidence.Publication persist(Path root,String task,String request,OperationLookup lookup) {
Path project=authorize(root,task,false);String key="persist:"+lookup;
var prior=prior(project,request,key);if(prior!=null)return prior;
// Resolve the immutable original result before starting a new evidence operation.
var operation=tasks.operation(project,task,lookup);
if(!operation.workerTerminated())throw new AutomationException(AutomationFailure.Code.BUSY,"Original operation worker has not terminated");
var capture=operation.runtimeResult()==null?null:operation.runtimeResult().capture();
if(capture==null)throw new AutomationException(AutomationFailure.Code.NO_FRAME_AVAILABLE,"Original operation has no retained capture");
var provenance=ValueMaps.fields("origin","runtime","projectRoot",project.toString(),"taskId",task,
"operationId",operation.operation().id().value(),"clientRequestId",operation.clientRequestId(),
"buildId",capture.buildId(),"sessionId",capture.sessionId(),"logicalFrameIndex",capture.logicalFrameIndex(),"frameId",capture.frameId());
return mutate(project,task,()->publishImage(project,request,key,capture.png(),provenance,capture.width(),capture.height()));
}
@Override public Evidence.Publication importReference(Path root,String task,String request,Path source,String grant) {
Path project=authorize(root,task,task==null);
return mutate(project,task,()->{
var imported=store.importReference(project,source,grant);var bytes=imported.bytes();
String key="import:"+source.toAbsolutePath().normalize()+":"+imported.canonicalPath()+":"+grant+":"+sha(bytes);
var prior=prior(project,request,key);if(prior!=null)return prior;
return publishImage(project,request,key,bytes,ValueMaps.fields("origin","external","source",imported.canonicalPath(),
"selectedPath",source.toAbsolutePath().normalize().toString(),"projectRoot",project.toString(),"taskId",task,"humanSelected",imported.humanAuthorized()),null,null);
});
}
private Evidence.Publication publishImage(Path root,String request,String key,byte[] bytes,Map<String,Object> provenance,Integer width,Integer height) {
var pixels=codec.decode(bytes);
if(width!=null&&(pixels.width()!=width||pixels.height()!=height))throw new IllegalArgumentException("Capture PNG dimensions disagree with metadata");
String pub=id();var artifact=artifact(pub,bytes,pixels,provenance);
return store.publish(root,publication(root,pub,request,key,List.of(artifact),null,null),List.of(new Evidence.Blob(artifact,bytes)));
}
@Override public Evidence.Publication compare(Path root,String task,String request,String expectedId,String actualId,Evidence.Parameters parameters) {
Path project=authorize(root,task,task==null);String key="compare:"+expectedId+":"+actualId+":"+parameters;
var prior=prior(project,request,key);if(prior!=null)return prior;
return mutate(project,task,()->{
var expected=find(project,expectedId);var actual=find(project,actualId);
var result=comparison.compare(codec.decode(store.image(project,expectedId)),codec.decode(store.image(project,actualId)),parameters);
byte[] diff=codec.encode(result.diff());String pub=id();
var artifact=artifact(pub,diff,result.diff(),ValueMaps.fields("origin","comparison-diff","projectRoot",project.toString(),
"expectedId",expectedId,"actualId",actualId,"taskId",task));
var report=new Evidence.Report(id(),expectedId,actualId,artifact.id(),expected.sha256(),actual.sha256(),result.parameters(),
result.verdict(),result.comparedPixels(),result.differentPixels(),result.maximumChannelError(),result.boundingBox());
return store.publish(project,publication(project,pub,request,key,List.of(artifact),report,null),List.of(new Evidence.Blob(artifact,diff)));
});
}
@Override public List<Evidence.Publication> list(Path root,String task) {
return store.list(authorize(root,task,false)).stream().sorted(Comparator.comparing(Evidence.Publication::createdAt)).toList();
}
@Override public Evidence.View metadata(Path root,String task,String artifactId) {
var project=authorize(root,task,false);var artifact=find(project,artifactId);var current=projects.requireAutomation(project).snapshot();
var capture=current.capture();var p=artifact.provenance();
boolean stale="runtime".equals(p.get("origin"))&&(current.stale()||capture==null
||!Objects.equals(capture.buildId(),p.get("buildId"))||!Objects.equals(capture.sessionId(),p.get("sessionId"))
||!capture.logicalFrameIndex().toString().equals(String.valueOf(p.get("logicalFrameIndex")))
||!capture.frameId().toString().equals(String.valueOf(p.get("frameId"))));
return new Evidence.View(artifact,stale);
}
@Override public Evidence.Read read(Path root,String task,String artifactId,long offset,int length) {
return store.read(authorize(root,task,false),artifactId,offset,length);
}
@Override public byte[] image(Path root,String task,String artifactId) {return store.image(authorize(root,task,false),artifactId);}
@Override public Evidence.Pixels pixels(Path root,String task,String artifactId) {return codec.decode(image(root,task,artifactId));}
@Override public void delete(Path root,String task,String publicationId) {
var project=authorize(root,task,task==null);mutate(project,task,()->{store.delete(project,publicationId);return Boolean.TRUE;});
}
@Override public String authorizeReference(Path root,Path source) {
Path project=projects.requireOpenRoot(root);
try(var lease=projects.enterManual(project)){return store.authorizeReference(project,source);}
}
@Override public Evidence.Publication approveBaseline(Path root,String artifactId,String previousRevision) {
Path project=projects.requireOpenRoot(root);
try(var lease=projects.enterManual(project)) {
var artifact=find(project,artifactId);String pub=id();
var baseline=new Evidence.Baseline(id(),previousRevision,artifactId,artifact.provenance(),Instant.now().toString());
return store.publish(project,publication(project,pub,"human-review:"+id(),"baseline:"+artifactId,List.of(),null,baseline),List.of());
}
}
private Path authorize(Path root,String task,boolean mutation) {
Path project=projects.requireOpenRoot(root);var session=projects.requireAutomation(project);
synchronized(session) {
var snapshot=session.snapshot();
if(snapshot.closed())throw new ControlDeniedException(ControlResult.Status.CLOSED);
if(task!=null&&(snapshot.state()!=ControlState.AGENT_WORKING||snapshot.task()==null||!snapshot.task().value().equals(task)))
throw new ControlDeniedException(ControlResult.Status.INVALID_TASK);
// Local read-only human inspection remains available during an agent grant.
if(task==null&&mutation)try(var ignored=projects.enterManual(project)){}
}
return project;
}
private <T>T mutate(Path project,String task,Supplier<T> work) {
if(task==null)try(var lease=projects.enterManual(project)){return work.get();}
var session=projects.requireAutomation(project);var value=new AtomicReference<T>();
final p.studio.api.operation.OperationHandle<AutomationOutcome> handle;
synchronized(session) {
authorize(project,task,true);var identity=session.snapshot().task();
handle=session.submit(identity,"Visual evidence",()->{
value.set(work.get());return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED,0,0,List.of(),null);
});
}
try {
handle.completion().toCompletableFuture().get();handle.termination().toCompletableFuture().get();
authorize(project,task,false);return value.get();
}catch(InterruptedException e){handle.cancel();Thread.currentThread().interrupt();throw new CancellationException("Evidence request interrupted; query request identity before retrying");}
catch(ExecutionException e) {
handle.termination().toCompletableFuture().join();authorize(project,task,false);
if(e.getCause() instanceof RuntimeException cause)throw cause;
throw new AutomationException(AutomationFailure.Code.INFRASTRUCTURE_FAILURE,"Evidence operation failed");
}
}
private Evidence.Artifact find(Path root,String id) {
return store.list(root).stream().flatMap(p->p.artifacts().stream()).filter(a->a.id().equals(id)).findFirst()
.orElseThrow(()->new AutomationException(AutomationFailure.Code.REFERENCE_NOT_FOUND,"Evidence not found in this project"));
}
private Evidence.Publication prior(Path root,String request,String key) {
if(request==null||request.isBlank()||request.length()>256)throw new IllegalArgumentException("Bounded clientRequestId required");
var result=store.list(root).stream().filter(p->p.requestId().equals(request)).findFirst().orElse(null);
if(result!=null&&!result.parameterKey().equals(fingerprint(key)))throw new AutomationException(AutomationFailure.Code.CLIENT_REQUEST_CONFLICT,"Evidence request parameters changed");
return result;
}
private static Evidence.Publication publication(Path root,String pub,String request,String key,List<Evidence.Artifact> artifacts,Evidence.Report report,Evidence.Baseline baseline) {
return new Evidence.Publication(pub,request,fingerprint(key),root.toString(),Instant.now().toString(),artifacts,report,baseline);
}
private static Evidence.Artifact artifact(String pub,byte[] bytes,Evidence.Pixels pixels,Map<String,Object> provenance) {
return new Evidence.Artifact(id(),pub,sha(bytes),bytes.length,"image/png",pixels.width(),pixels.height(),provenance);
}
private static String id(){return UUID.randomUUID().toString();}
private static String fingerprint(String key){return sha(key.getBytes(java.nio.charset.StandardCharsets.UTF_8));}
private static String sha(byte[] bytes) {
try{return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));}
catch(java.security.NoSuchAlgorithmException e){throw new IllegalStateException(e);}
}
}
@@ -0,0 +1,228 @@
package p.studio.core.session;
import p.studio.api.automation.*;
import p.studio.api.operation.*;
import p.studio.api.project.*;
import p.studio.api.values.ValueMaps;
import p.studio.core.project.ProjectAccessRegistry;
import p.studio.core.runtime.*;
import p.studio.core.shipper.SavedProjectPreparation;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.*;
/** One bounded test runner, admitted by the existing manual lease or current-task worker. */
public final class DefaultGuestTestTools implements GuestTestTools, AutoCloseable {
private final ProjectAccessRegistry projects;
private final ProjectCatalog catalog;
private final SavedProjectPreparation shipper;
private final HeadlessGateway gateway;
private final GuestTestSupport support;
private final GuestTests.Decoder decoder;
private final OperationExecutor executor;
private final Map<Path,List<Run>> runs = new ConcurrentHashMap<>();
private final ScheduledExecutorService deadlines=Executors.newSingleThreadScheduledExecutor(Thread.ofPlatform().daemon().name("guest-test-deadlines").factory());
public DefaultGuestTestTools(ProjectAccessRegistry projects, ProjectCatalog catalog, SavedProjectPreparation shipper,
HeadlessGateway gateway, GuestTestSupport support, GuestTests.Decoder decoder, OperationExecutor executor) {
this.projects=projects;this.catalog=catalog;this.shipper=shipper;this.gateway=gateway;
this.support=support;this.decoder=decoder;this.executor=executor;
}
private AutomationSession authorize(Path root,String task) {
Path project=projects.requireOpenRoot(root);var session=projects.requireAutomation(project);
var snapshot=session.snapshot();
if(snapshot.closed())throw new ControlDeniedException(ControlResult.Status.CLOSED);
if(task!=null&&(snapshot.state()!=ControlState.AGENT_WORKING||snapshot.task()==null||!task.equals(snapshot.task().value())))
throw new ControlDeniedException(ControlResult.Status.INVALID_TASK);
return session;
}
@Override public GuestTests.Catalog targets(Path root,String task){authorize(root,task);return support.targets(catalog.openProject(root));}
@Override public View start(Path root,String task,String requestId,Request request) {
if(requestId==null||requestId.isBlank()||requestId.length()>256)throw new IllegalArgumentException("Bounded clientRequestId required");
var session=authorize(root,task);Path project=projects.requireOpenRoot(root);
synchronized(session) {
authorize(project,task);
var records=runs.computeIfAbsent(project,ignored->new CopyOnWriteArrayList<>());
var prior=records.stream().filter(r->Objects.equals(r.task,task)&&r.requestId.equals(requestId)).findFirst().orElse(null);
if(prior!=null) {
if(!prior.request.equals(request))throw new AutomationException(AutomationFailure.Code.CLIENT_REQUEST_CONFLICT,"Test request parameters changed");
return view(prior,session);
}
var target=targets(project,task).targets().stream().filter(t->t.id().equals(request.targetId())).findFirst()
.orElseThrow(()->new IllegalArgumentException("Undeclared test target"));
if(session.runtimeAttached())
throw new AutomationException(AutomationFailure.Code.BUSY,"Stop the current managed runtime before testing");
if(records.stream().anyMatch(r->!r.settled.isDone()))
throw new AutomationException(AutomationFailure.Code.BUSY,"Another project test worker is active");
var run=new Run(project,task,requestId,request,target,session,session.snapshot().task());
if(task==null)run.lease=projects.enterExecution(project);
try {
var ready=new CountDownLatch(1);
java.util.concurrent.Callable<AutomationOutcome> work=()->{ready.await();return execute(run);};
run.handle=task==null?executor.submit("Guest tests: "+target.id(),work):session.submit(run.identity,"Guest tests: "+target.id(),work);
records.add(run);
if(run.lease!=null)run.lease.onProjectClose(run.handle::cancel);
run.handle.termination().whenComplete((value,error)->{
try {
if(run.result==null) {
run.result=new GuestTranscript(run.target,0,decoder).result(run.runId,run.buildId,run.handle.id().value(),run.requestId,GuestTests.Completion.cancelled,null);
try{support.completed(catalog.openProject(run.root),run.runId,run.result);}catch(Exception retained){run.result=run.result.withContext(Map.of("resultStorageFailure",Objects.requireNonNullElse(retained.getMessage(),"Result storage unavailable")));}
}
} finally {if(run.lease!=null)run.lease.close();run.settled.complete(null);}
});
ready.countDown();return view(run,session);
}catch(RuntimeException error){if(run.lease!=null)run.lease.close();throw error;}
}
}
private AutomationOutcome execute(Run run) throws Exception {
var project=catalog.openProject(run.root);String buildId=UUID.randomUUID().toString();
var transcript=new GuestTranscript(run.target,Objects.requireNonNullElse(project.appId(),0L),decoder,16*1024*1024,id->support.admitsCheckpoint(run.root,id));
run.transcript=transcript;
GuestTests.Completion completion=GuestTests.Completion.running;AutomationFailure failure=null;CanonicalCapture capture=null;
var context=new LinkedHashMap<String,Object>();
context.put("projectRoot",run.root.toString());context.put("taskId",run.task);context.put("media",run.request.media().name());
context.put("reopenRunId",run.request.reopenRunId());context.put("discardUnsaved",run.request.discardUnsaved());
context.put("runnerLimits",Map.of("maxFrames",run.request.maxFrames(),"blockFrames",run.request.blockFrames(),"timeoutMs",run.request.timeoutMs()));
long deadline=System.nanoTime()+TimeUnit.MILLISECONDS.toNanos(run.request.timeoutMs());
var worker=Thread.currentThread();var alarm=deadlines.schedule(()->{run.timedOut=true;var runtime=run.runtime;if(runtime!=null)runtime.cancel();worker.interrupt();},run.request.timeoutMs(),TimeUnit.MILLISECONDS);
try {
checkInterrupted();
var build=shipper.prepareNow(project,new BuildSelection(run.target.id(),buildId),ignored->{});
context.put("buildProvenance",build.context());context.put("buildDiagnostics",build.diagnosticView());
if(!build.success())throw new AutomationException(new AutomationFailure(AutomationFailure.Code.BUILD_NOT_FOUND,"Test build did not complete",Map.of(),build.diagnosticView()));
run.buildId=buildId;
if(!run.target.equals(support.targetAtBuild(project,build,run.target.id())))throw new IllegalArgumentException("Test target definition changed after request admission");
transcript=new GuestTranscript(run.target,support.applicationId(build),decoder,16*1024*1024,id->support.admitsCheckpoint(run.root,id));
run.transcript=transcript;
checkInterrupted();
var media=support.media(project,run.runId,run.request);context.put("bootOptions",media);
run.runtime=gateway.bootTest(project,buildId,build,media);
try {run.session.attachRuntime(run.task==null?null:run.identity,run.runtime);run.attached=true;}
catch(RuntimeException denied){run.runtime.shutdown(run.request.discardUnsaved()).toCompletableFuture().join();throw denied;}
drain(run,transcript);
while(!transcript.finished()) {
checkInterrupted();
if(System.nanoTime()>=deadline || run.completedFrames>=run.request.maxFrames()){completion=GuestTests.Completion.timed_out;context.put("completionReason",run.completedFrames>=run.request.maxFrames()?"frame_limit":"wall_timeout");break;}
long block=Math.min(Math.min(run.request.blockFrames(),run.runtime.maxFramesPerAdvance()),run.request.maxFrames()-run.completedFrames);
if(block<1)throw new AutomationException(AutomationFailure.Code.LIMIT_EXCEEDED,"Runtime cannot advance a test block");
final long before=run.completedFrames;
var advanced=run.runtime.advance(block,p->{run.progress=new TrackedOperation.Progress(run.request.maxFrames(),before+Objects.requireNonNullElse(p.completedFrames(),0L));if(run.task!=null)run.session.runtimeObserved(run.identity);});
run.completedFrames+=advanced.completedFrames();capture=advanced.capture();
drain(run,transcript);
if(advanced.kind()==AutomationOutcome.Kind.CANCELLED){completion=run.timedOut?GuestTests.Completion.timed_out:GuestTests.Completion.cancelled;break;}
if(advanced.kind()!=AutomationOutcome.Kind.SUCCEEDED) {
failure=nativeFailure(advanced);
completion=GuestTests.Completion.infrastructure_failure;break;
}
}
if(completion==GuestTests.Completion.running)completion=transcript.failedInfrastructure()?GuestTests.Completion.infrastructure_failure:GuestTests.Completion.completed;
if(capture!=null)context.put("captureArtifactId",support.retainCapture(run.root,run.runId,run.handle.id().value(),run.requestId,capture));
} catch(InterruptedException|CancellationException cancelled) {
completion=run.timedOut?GuestTests.Completion.timed_out:GuestTests.Completion.cancelled;Thread.interrupted();
if(run.runtime!=null)run.runtime.cancel();
} catch(Exception problem) {
completion=run.timedOut?GuestTests.Completion.timed_out
:Thread.currentThread().isInterrupted()||run.handle.snapshot().state()==OperationState.CANCELLED?GuestTests.Completion.cancelled:GuestTests.Completion.infrastructure_failure;
Throwable cause=problem;while(cause instanceof CompletionException&&cause.getCause()!=null)cause=cause.getCause();
failure=cause instanceof AutomationException domain?domain.failure():new AutomationFailure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE,Objects.requireNonNullElse(cause.getMessage(),cause.getClass().getSimpleName()));
} finally {
alarm.cancel(false);
boolean interrupted=Thread.interrupted();
if(run.runtime!=null) {
try {drain(run,transcript);}catch(Exception unread){context.put("finalLogReadFailure",Objects.requireNonNullElse(unread.getMessage(),"Native logs unavailable"));if(failure==null&&completion==GuestTests.Completion.completed){failure=new AutomationFailure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE,"Final native logs unavailable");completion=GuestTests.Completion.infrastructure_failure;}}
context.put("runtimeObservation",observationValues(run.runtime.observation()));
try {closeRuntime(run,run.request.discardUnsaved());context.put("runtimeClosed",true);}
catch(RuntimeException refused) {
context.put("runtimeClosed",false);
var cause=refused;while(cause instanceof CompletionException&&cause.getCause() instanceof RuntimeException nested)cause=nested;
context.put("cleanupFailure",cause instanceof AutomationException domain?ValueMaps.fields("code",domain.failure().code().name(),"message",domain.failure().message(),"runtimeError",domain.failure().runtimeError()):Map.of("message",Objects.requireNonNullElse(cause.getMessage(),"Cleanup refused")));
if(failure==null)failure=cause instanceof AutomationException domain?domain.failure():new AutomationFailure(AutomationFailure.Code.CLEANUP_FAILED,"Test runtime cleanup refused");
if(completion==GuestTests.Completion.completed)completion=GuestTests.Completion.infrastructure_failure;
}
}
run.completedFrames=Math.max(run.completedFrames,Objects.requireNonNullElse(run.progress.completedFrames(),0L));
context.put("completedFrames",run.completedFrames);
run.result=transcript.result(run.runId,run.buildId,run.handle.id().value(),run.requestId,completion,failure).withContext(context);
try {support.completed(project,run.runId,run.result);}catch(Exception storage){
run.result=transcript.result(run.runId,run.buildId,run.handle.id().value(),run.requestId,GuestTests.Completion.infrastructure_failure,
new AutomationFailure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE,"Cannot retain test result: "+storage.getMessage())).withContext(context);
}
if(interrupted)Thread.currentThread().interrupt();
}
return new AutomationOutcome(completion==GuestTests.Completion.cancelled?AutomationOutcome.Kind.CANCELLED
:run.result.verdict()==GuestTests.CaseState.infrastructure_failure?AutomationOutcome.Kind.INFRASTRUCTURE_FAILURE:AutomationOutcome.Kind.SUCCEEDED,
run.request.maxFrames(),run.completedFrames,List.of("Guest test verdict: "+run.result.verdict()),capture,Map.of());
}
static AutomationFailure nativeFailure(AutomationOutcome outcome) {
Object original=outcome.observation()==null?null:outcome.observation().nativeDetails().get(outcome.kind()==AutomationOutcome.Kind.GUEST_FAULT?"fault":"error");
@SuppressWarnings("unchecked") var nativeCause=original instanceof Map<?,?> value?ValueMaps.object((Map<String,Object>)value):Map.<String,Object>of();
String message=String.valueOf(nativeCause.getOrDefault("message",nativeCause.getOrDefault("summary","Guest execution stopped: "+outcome.kind())));
return new AutomationFailure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE,message,nativeCause,Map.of("stop",outcome.kind().name()),outcome.observation());
}
private static Map<String,Object> observationValues(RuntimeObservation value) {
return ValueMaps.fields("sessionId",value.sessionId(),"buildId",value.buildId(),"lifecycle",value.lifecycle().name(),"nativeDetails",value.nativeDetails());
}
private static void checkInterrupted() throws InterruptedException {if(Thread.currentThread().isInterrupted())throw new InterruptedException("Guest test cancelled");}
private static void drain(Run run,GuestTranscript transcript) throws Exception {
while(true) {
var page=run.runtime.logs(transcript.cursor(),128);transcript.accept(page);
if(transcript.failedInfrastructure()||Objects.equals(transcript.cursor(),page.newestSequence()))return;
}
}
private static void closeRuntime(Run run,boolean discard) {
var runtime=run.runtime;if(runtime==null)return;
try {runtime.shutdown(discard).toCompletableFuture().join();}
catch(CompletionException error){if(error.getCause() instanceof RuntimeException domain)throw domain;throw error;}
var snapshot=run.session.snapshot();
if(run.attached&&run.session.runtimeAttached()&&Objects.equals(snapshot.runtimeObservation().sessionId(),runtime.sessionId())
&&!snapshot.closed()&&(run.task==null?snapshot.state()==ControlState.MANUAL:snapshot.task()!=null&&snapshot.task().equals(run.identity)))
run.session.detachRuntime(run.task==null?null:run.identity,runtime);
run.runtime=null;run.attached=false;
}
@Override public View get(Path root,String task,OperationLookup lookup){var session=authorize(root,task);synchronized(session){return view(find(root,task,lookup),session);}}
@Override public List<View> list(Path root,String task) {
var session=authorize(root,task);synchronized(session){return runs.getOrDefault(projects.requireOpenRoot(root),List.of()).stream().filter(r->task==null||task.equals(r.task)).map(run->view(run,session)).toList();}
}
@Override public boolean cancel(Path root,String task,String operation){authorize(root,task);return find(root,task,new OperationLookup(operation,null)).handle.cancel();}
@Override public TranscriptPage transcript(Path root,String task,OperationLookup lookup,int offset,int limit) {
if(offset<0||limit<1||limit>1024)throw new IllegalArgumentException("Invalid transcript page");
var result=get(root,task,lookup).result();var events=result==null?List.<GuestTests.TranscriptEvent>of():result.transcript();
if(offset>events.size())throw new IllegalArgumentException("Offset exceeds transcript");
int end=(int)Math.min((long)offset+limit,events.size());return new TranscriptPage(result==null?find(root,task,lookup).runId:result.runId(),offset,end,events.size(),events.subList(offset,end));
}
@Override public void shutdown(Path root,String task,String operation,boolean discard) {
var session=authorize(root,task);var run=find(root,task,new OperationLookup(operation,null));
if(!run.settled.isDone())throw new AutomationException(AutomationFailure.Code.BUSY,"Wait for test worker termination before recovery");
if(task==null)try(var lease=projects.enterManual(root)){closeRuntime(run,discard);}
else {
OperationHandle<AutomationOutcome> handle;
synchronized(session){authorize(root,task);handle=session.submit(session.snapshot().task(),"Guest test cleanup",()->{closeRuntime(run,discard);return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED,0,0,List.of(),null);});}
try{handle.completion().toCompletableFuture().join();}
catch(CompletionException error){if(error.getCause() instanceof RuntimeException domain)throw domain;throw error;}
finally{handle.termination().toCompletableFuture().join();authorize(root,task);}
}
}
@Override public void close(){deadlines.shutdownNow();}
private Run find(Path root,String task,OperationLookup lookup) {
authorize(root,task);
return runs.getOrDefault(projects.requireOpenRoot(root),List.of()).stream().filter(r->task==null||task.equals(r.task))
.filter(r->lookup.operationId()!=null?r.handle.id().value().equals(lookup.operationId()):r.requestId.equals(lookup.clientRequestId()))
.reduce((first,last)->last).orElseThrow(()->new AutomationException(AutomationFailure.Code.OPERATION_NOT_FOUND,"Test operation not found in current authority"));
}
private static View view(Run run,AutomationSession currentSession) {
boolean done=run.settled.isDone();var snapshot=run.handle.snapshot();
if(!done&&(snapshot.state()==OperationState.SUCCEEDED||snapshot.state()==OperationState.FAILED||snapshot.state()==OperationState.CANCELLED))
snapshot=new OperationSnapshot(snapshot.id(),OperationState.RUNNING,snapshot.updatedAt(),snapshot.summary());
var result=done?run.result:run.transcript==null?null:run.transcript.result(run.runId,run.buildId,run.handle.id().value(),run.requestId,GuestTests.Completion.running,null);
var current=currentSession.snapshot().runtimeObservation();
return new View(snapshot,run.requestId,run.runId,done,run.progress,result,current,
run.runtime!=null&&currentSession.runtimeAttached()&&Objects.equals(current.sessionId(),run.runtime.sessionId()));
}
private static final class Run {
final Path root;final String task,requestId,runId=UUID.randomUUID().toString();final Request request;final GuestTests.Target target;final AutomationSession session;final TaskIdentity identity;
OperationHandle<AutomationOutcome> handle;ManualLease lease;volatile HeadlessGateway.Session runtime;volatile GuestTranscript transcript;volatile GuestTests.RunResult result;volatile String buildId;
volatile long completedFrames;volatile TrackedOperation.Progress progress;
volatile boolean timedOut,attached;
final CompletableFuture<Void> settled=new CompletableFuture<>();
Run(Path root,String task,String requestId,Request request,GuestTests.Target target,AutomationSession session,TaskIdentity identity){this.root=root;this.task=task;this.requestId=requestId;this.request=request;this.target=target;this.session=session;this.identity=identity;progress=new TrackedOperation.Progress(request.maxFrames(),0L);}
}
}
@@ -17,6 +17,15 @@ public final class DefaultProjectSession implements ProjectSession {
private final LspLifecyclePort lsp;
private ProjectLocalStudioState state;
private boolean closed;
private p.studio.api.automation.EvidenceTools evidence;
void setEvidenceTools(p.studio.api.automation.EvidenceTools tools) { evidence = tools; }
@Override public java.util.Optional<p.studio.api.automation.EvidenceTools> evidenceTools() { return java.util.Optional.ofNullable(evidence); }
private p.studio.api.automation.GuestTestTools tests;
private p.studio.api.assets.AssetAuthoringService assetAuthoring;
void setAssetAuthoring(p.studio.api.assets.AssetAuthoringService service){assetAuthoring=service;}
@Override public java.util.Optional<p.studio.api.assets.AssetAuthoringService> assetAuthoring(){return java.util.Optional.ofNullable(assetAuthoring);}
void setGuestTestTools(p.studio.api.automation.GuestTestTools tools) { tests = tools; }
@Override public java.util.Optional<p.studio.api.automation.GuestTestTools> guestTestTools() { return java.util.Optional.ofNullable(tests); }
private final DefaultAutomationSession automation;
private final p.studio.api.operation.OperationSubscription accessRegistration;
@@ -39,6 +48,11 @@ public final class DefaultProjectSession implements ProjectSession {
@Override public ProjectLocalStudioSetup projectLocalStudioSetup() { return setup; }
@Override public ExecutionSession executionSession() { return execution; }
@Override public p.studio.api.automation.AutomationSession automation() { return automation; }
@Override public p.studio.api.diagnostic.DiagnosticSources diagnosticSources() {
return origin -> automation.snapshot().closed()
? new p.studio.api.diagnostic.DiagnosticSources.Source("Project closed", null, "", 0, 0)
: p.studio.core.project.DiagnosticNavigation.read(project.rootPath(), origin);
}
@Override public void replaceProjectLocalStudioState(ProjectLocalStudioState next) { state = Objects.requireNonNull(next, "state"); }
@Override public void saveProjectLocalStudioState() { stateStore.save(project, state); }
@@ -19,6 +19,12 @@ public final class DefaultProjectSessionFactory implements ProjectSessionFactory
private final p.studio.api.operation.OperationExecutor operations;
private final p.studio.api.automation.AutomationConnectionLifecycle connections;
private final p.studio.api.automation.AgentIntegrationPreparation preparation;
private p.studio.api.automation.EvidenceTools evidence;
public void setEvidenceTools(p.studio.api.automation.EvidenceTools tools) { evidence = tools; }
private p.studio.api.automation.GuestTestTools tests;
private p.studio.api.assets.AssetAuthoringService assetAuthoring;
public void setAssetAuthoring(p.studio.api.assets.AssetAuthoringService service){assetAuthoring=service;}
public void setGuestTestTools(p.studio.api.automation.GuestTestTools tools) { tests = tools; }
public DefaultProjectSessionFactory(ProjectLocalStudioStateStore stateStore,
ProjectLocalStudioSetupStore setupStore, LspLifecyclePort lsp,
@@ -67,6 +73,9 @@ public final class DefaultProjectSessionFactory implements ProjectSessionFactory
registration.unsubscribe();
synchronized (DefaultProjectSessionFactory.this) { sessions.removeIf(s -> s.automation() == automation); }
});
session.setEvidenceTools(evidence);
session.setGuestTestTools(tests);
session.setAssetAuthoring(assetAuthoring);
sessions.add(session);
return session;
}
@@ -32,18 +32,25 @@ public final class DefaultTaskTools implements TaskTools {
}
@Override public TrackedOperation build(Path root, String id, String request) {
return start(root, id, request, new Parameters("build", null), (data, record) -> {
return build(root, id, request, GuestTests.PRODUCTION);
}
@Override public TrackedOperation build(Path root, String id, String request, String targetId) {
String selected = new GuestTests.Selection(targetId).targetId();
return start(root, id, request, new Parameters("build", null, selected), (data, record) -> {
stopRuntime(data);
data.session.markCaptureStale();
// Artifacts are mutable in place. Old IDs must never boot newly overwritten files.
data.builds.clear();
var result = shipper.prepareNow(catalog.openProject(data.task.projectRoot()), ignored -> {});
if (GuestTests.PRODUCTION.equals(selected)) data.builds.entrySet().removeIf(entry ->
GuestTests.PRODUCTION.equals(entry.getValue().context().getOrDefault("targetId", GuestTests.PRODUCTION)));
String candidateId = UUID.randomUUID().toString();
var result = shipper.prepareNow(catalog.openProject(data.task.projectRoot()), new BuildSelection(selected, candidateId), ignored -> {})
.withContext(Map.of("targetId", selected));
if (Thread.currentThread().isInterrupted() || record.handle != null && record.handle.snapshot().state() == OperationState.CANCELLED) result = result.asCancelled();
if (result.success() && !Thread.currentThread().isInterrupted()) {
record.buildId = UUID.randomUUID().toString();
record.buildId = candidateId;
data.builds.put(record.buildId, result);
}
record.build = result.withContext(p.studio.utilities.json.ImmutableJson.fields("projectRoot", data.task.projectRoot().toString(),
record.build = result.withContext(p.studio.api.values.ValueMaps.fields("projectRoot", data.task.projectRoot().toString(),
"taskId", data.task.value(), "clientRequestId", record.request, "buildId", record.buildId));
if (record.buildId != null) data.builds.put(record.buildId, record.build);
return new AutomationOutcome(result.status() == p.studio.api.shipper.ShipperPreparationStatus.CANCELLED
@@ -55,13 +62,28 @@ public final class DefaultTaskTools implements TaskTools {
if (frames < 0) throw new IllegalArgumentException("frames must be nonnegative");
return start(root, id, request, new Parameters("advance", frames), (data, record) -> {
var runtime = requireRuntime(data);
data.session.markCaptureStale();
var result = runtime.advance(frames, progress -> {
if (!Objects.equals(progress.requestedFrames(), frames)) throw new IllegalArgumentException("Invalid runtime progress");
record.progress = progress;
data.session.runtimeObserved(data.task);
});
record.runtime = result;
var observed = result.observation();
if (observed == null) {
// Embedded gateways may not expose passive native state. Their explicit
// outcome still confirms progress/capture without proving a boot phase.
observed = runtime.observation();
if (observed.buildId() == null && result.capture() != null)
observed = RuntimeObservation.unknown(runtime.sessionId(), result.capture().buildId());
observed = observed.withProgress(new TrackedOperation.Progress(result.requestedFrames(), result.completedFrames()));
if (result.capture() != null) observed = observed.withCapture(result.capture());
}
record.observation = observed.withContext(
Map.of("projectRoot", data.task.projectRoot().toString(), "taskId", data.task.value(), "clientRequestId", record.request));
record.runtime = new AutomationOutcome(result.kind(), result.requestedFrames(), result.completedFrames(),
result.diagnostics(), result.capture(), result.diagnosticResult(), record.observation);
record.progress = new TrackedOperation.Progress(result.requestedFrames(), result.completedFrames());
return result;
return record.runtime;
});
}
@@ -85,6 +107,8 @@ public final class DefaultTaskTools implements TaskTools {
else if (!(error instanceof InterruptedException) && !(error instanceof CancellationException))
record.failure = new AutomationFailure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Managed operation failed");
throw error;
} finally {
if (record.observation == null && data.runtime != null) record.observation = data.runtime.observation();
}
});
// Rejected work never reserves a request ID. The authority monitor makes start/dedup atomic.
@@ -138,7 +162,7 @@ public final class DefaultTaskTools implements TaskTools {
catch (RuntimeException denied) { runtime.cancel(); runtime.shutdown().toCompletableFuture().join(); throw denied; }
data.runtime = runtime;
data.runtimeBuildId = buildId;
result = new HeadlessReply(Map.of("sessionId", runtime.sessionId(), "buildId", buildId), null);
result = runtime.bootReply();
}
data.reset = false;
} else if (action == HeadlessAction.SHUTDOWN) {
@@ -154,18 +178,37 @@ public final class DefaultTaskTools implements TaskTools {
if (action == HeadlessAction.RESET) { data.reset = true; session.markCaptureStale(); }
}
reply.set(result);
return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, List.of(), result.capture());
var observed = result.values().get("observation");
return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, List.of(), result.capture(), Map.of(),
observed instanceof RuntimeObservation observation ? observation : null);
});
}
try {
handle.completion().toCompletableFuture().get();
handle.termination().toCompletableFuture().get();
synchronized (session) { requireTask(session, id); return reply.get(); }
synchronized (session) {
var identity = requireTask(session, id);
var result = reply.get();
var values = new LinkedHashMap<>(result.values());
if (values.get("observation") instanceof RuntimeObservation observed)
values.put("observation", observed.withContext(Map.of("projectRoot", identity.projectRoot().toString(),
"taskId", id, "operationId", handle.id().value())));
return new HeadlessReply(values, result.capture());
}
} catch (InterruptedException error) {
handle.cancel(); Thread.currentThread().interrupt();
throw failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Headless call interrupted");
} catch (ExecutionException error) {
handle.termination().toCompletableFuture().join();
synchronized (session) { requireTask(session, id); }
if (error.getCause() instanceof AutomationException domain) {
var original = domain.failure();
var context = Map.<String, Object>of("projectRoot", session.snapshot().projectRoot().toString(),
"taskId", id, "operationId", handle.id().value());
var details = new LinkedHashMap<>(original.details()); details.put("context", context);
throw new AutomationException(new AutomationFailure(original.code(), original.message(), original.runtimeError(), details,
original.observation() == null ? null : original.observation().withContext(context)));
}
if (error.getCause() instanceof RuntimeException cause) throw cause;
throw failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Headless call failed");
} catch (CancellationException error) {
@@ -222,19 +265,24 @@ public final class DefaultTaskTools implements TaskTools {
if (done && snapshot.state() == OperationState.CANCELLED && record.build != null) {
if (record.buildId != null) data.builds.remove(record.buildId);
record.buildId = null;
record.build = record.build.asCancelled().withContext(p.studio.utilities.json.ImmutableJson.fields("buildId", null));
record.build = record.build.asCancelled().withContext(p.studio.api.values.ValueMaps.fields("buildId", null));
}
var current = data.session.snapshot();
var captured = record.runtime == null ? null : record.runtime.capture();
boolean stale = captured != null && (current.stale() || current.capture() == null
|| !captured.sessionId().equals(current.capture().sessionId()) || !captured.buildId().equals(current.capture().buildId())
|| !captured.logicalFrameIndex().equals(current.capture().logicalFrameIndex()) || !captured.frameId().equals(current.capture().frameId()));
var identity = p.studio.api.values.ValueMaps.fields("projectRoot", data.task.projectRoot().toString(),
"taskId", data.task.value(), "operationId", record.handle.id().value(), "clientRequestId", record.request);
return new TrackedOperation(snapshot, record.request, record.progress,
done ? record.buildId : null, done && record.build != null ? record.build.withContext(Map.of("operationId", record.handle.id().value())) : null, done ? record.runtime : null, done ? record.failure : null,
done, stale);
done, stale, done && record.observation != null ? record.observation.withContext(identity) : null,
current.runtimeObservation());
}
private static AutomationException failure(AutomationFailure.Code code, String message) { return new AutomationException(code, message); }
private record Parameters(String kind, Long frames) {}
private record Parameters(String kind, Long frames, String targetId) {
Parameters(String kind, Long frames) { this(kind, frames, GuestTests.PRODUCTION); }
}
@FunctionalInterface private interface Work { AutomationOutcome run(TaskData data, Record record) throws Exception; }
private static final class Record {
final String request;
@@ -245,6 +293,7 @@ public final class DefaultTaskTools implements TaskTools {
volatile ShipperPrepareResult build;
volatile AutomationOutcome runtime;
volatile AutomationFailure failure;
volatile RuntimeObservation observation;
Record(String request, Parameters parameters) {
this.request = request; this.parameters = parameters;
progress = new TrackedOperation.Progress(parameters.frames, null);
@@ -0,0 +1,150 @@
package p.studio.core.session;
import p.studio.api.automation.*;
import p.studio.api.values.ValueMaps;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.*;
/** Validates observed guest events only; never advances a guest or treats ordinary text as assertions. */
public final class GuestTranscript {
private final GuestTests.Target target;
private final long appId;
private final GuestTests.Decoder decoder;
private final int byteLimit;
private final java.util.function.Predicate<String> checkpointAdmitted;
private final Map<BigInteger,GuestTests.LogEvent> nativeEvents=new LinkedHashMap<>();
private final Map<String,GuestTests.Event> eventIds=new LinkedHashMap<>();
private final Map<String,Case> cases=new LinkedHashMap<>();
private final List<GuestTests.TranscriptEvent> transcript=new ArrayList<>();
private String sessionId,suiteId,problemCode,problem;
private BigInteger epoch,lastSequence;
private boolean suiteComplete,loss,suiteRejected,historicalOverflow;
private int bytes;
public GuestTranscript(GuestTests.Target target,long appId,GuestTests.Decoder decoder) {
this(target,appId,decoder,16*1024*1024);
}
public GuestTranscript(GuestTests.Target target,long appId,GuestTests.Decoder decoder,int byteLimit) {
this(target,appId,decoder,byteLimit,id->false);
}
public GuestTranscript(GuestTests.Target target,long appId,GuestTests.Decoder decoder,int byteLimit,java.util.function.Predicate<String> checkpointAdmitted) {
this.target=Objects.requireNonNull(target);this.appId=appId;this.decoder=Objects.requireNonNull(decoder);
if(appId<0||appId>0xffff_ffffL||byteLimit<1)throw new IllegalArgumentException("Invalid transcript limits/identity");
this.byteLimit=byteLimit;this.checkpointAdmitted=Objects.requireNonNull(checkpointAdmitted);target.cases().forEach(id->cases.put(id,new Case(id)));
}
public synchronized void accept(GuestTests.LogPage page) {
if(problem!=null)return;
if(sessionId==null){sessionId=page.sessionId();epoch=page.logEpoch();}
if(!sessionId.equals(page.sessionId())||!epoch.equals(page.logEpoch())) {fail("session_changed","Log cursor belongs to another session/log stream",true);return;}
historicalOverflow |= page.overflow();
if(page.gap()){fail("log_loss","Native log cursor reports unread history loss",true);return;}
if(page.events().isEmpty()&&page.newestSequence()!=null&&(lastSequence==null||page.newestSequence().compareTo(lastSequence)>0)) {
fail("empty_log_page","Provider returned no events before its available newest sequence",true);return;
}
for(var nativeEvent:page.events()) {
var previous=nativeEvents.get(nativeEvent.sequence());
if(previous!=null){if(!previous.equals(nativeEvent))fail("sequence_conflict","A previously observed native sequence changed",true);if(problem!=null)return;continue;}
BigInteger expected=lastSequence==null?BigInteger.ZERO:lastSequence.add(BigInteger.ONE);
if(!nativeEvent.sequence().equals(expected)){fail("sequence_gap","Native sequence is missing or reordered",true);return;}
int size=nativeEvent.message().getBytes(StandardCharsets.UTF_8).length+128;
if(size>byteLimit-bytes||nativeEvents.size()>=100_000){fail("transcript_limit","Host transcript limit exceeded before applying another event",true);return;}
bytes+=size;nativeEvents.put(nativeEvent.sequence(),nativeEvent);lastSequence=nativeEvent.sequence();
if(nativeEvent.tag()!=target.logTag()||!nativeEvent.source().equals("app"))continue;
if(nativeEvent.appId()==null||nativeEvent.appId()!=appId){fail("application_changed","Tagged record belongs to another application",true);return;}
final GuestTests.Event event;
try {event=decoder.decode(nativeEvent.message());}
catch(RuntimeException invalid){transcript.add(new GuestTests.TranscriptEvent(nativeEvent,null));fail("invalid_event","Tagged event is invalid: "+Objects.requireNonNullElse(invalid.getMessage(),invalid.getClass().getSimpleName()),false);return;}
if(event==null){fail("invalid_event","Decoder returned no event",false);return;}
transcript.add(new GuestTests.TranscriptEvent(nativeEvent,event));
var prior=eventIds.putIfAbsent(event.eventId(),event);
if(prior!=null){if(!prior.equals(event))fail("event_conflict","Duplicate eventId has conflicting content",false);if(problem!=null)return;continue;}
try {
if (integer(event.payload().get("ordinal")) != eventIds.size()-1) throw invalid("Missing or discontinuous producer event ordinal");
apply(event);
}catch(IllegalArgumentException invalid){fail("invalid_suite",invalid.getMessage(),false);return;}
}
}
private void apply(GuestTests.Event event) {
if(suiteComplete)throw invalid("Event follows suite completion");
if(event.kind()==GuestTests.EventKind.suite_start) {
if(suiteId!=null||event.caseId()!=null)throw invalid("Invalid duplicate suite start");
suiteId=event.suiteId();return;
}
if(suiteId==null||!suiteId.equals(event.suiteId()))throw invalid("Event has no matching suite start");
if(event.kind()==GuestTests.EventKind.suite_end) {
if(integer(event.payload().get("eventCount")) != eventIds.size()) throw invalid("Suite final event count disagrees with producer transcript");
if(event.caseId()!=null)throw invalid("Suite completion must not identify a case");
suiteComplete=true;
suiteRejected=explicitlyRejected(event);
if(cases.isEmpty())throw invalid("Empty suite cannot pass");
if(cases.values().stream().anyMatch(c->!c.ended))throw invalid("Expected cases did not complete");
if(cases.values().stream().mapToInt(c->c.assertions.size()).sum()==0)throw invalid("Suite has no assertions");
return;
}
var current=cases.get(event.caseId());
if(current==null)throw invalid("Event identifies an undeclared case");
switch(event.kind()) {
case case_start -> {if(current.started)throw invalid("Case starts more than once");current.started=true;}
case assertion -> {
requireActive(current);
if(!(event.payload().get("name") instanceof String name)||!(event.payload().get("message") instanceof String message)
||!(event.payload().get("passed") instanceof Boolean passed)||name.isBlank())throw invalid("Assertion requires name/message and boolean passed");
current.assertions.add(new GuestTests.Assertion(name,message,event.payload().get("expected"),event.payload().get("actual"),passed,
event.payload().containsKey("expected"),event.payload().containsKey("actual")));
}
case checkpoint -> {
requireActive(current);
if(!(event.payload().get("artifactId") instanceof String id)||id.isBlank())throw invalid("Checkpoint requires an original retained artifactId");
if(!checkpointAdmitted.test(id))throw invalid("Checkpoint artifact is not admitted in this project");
current.checkpoints.add(id);
}
case case_end -> {requireActive(current);current.rejected=explicitlyRejected(event);current.ended=true;}
default -> throw invalid("Invalid suite event order");
}
}
private static void requireActive(Case current){if(!current.started||current.ended)throw invalid("Case event outside its active lifetime");}
public synchronized boolean finished(){return suiteComplete||problem!=null;}
public synchronized boolean failedInfrastructure(){return problem!=null;}
public synchronized BigInteger cursor(){return lastSequence;}
public synchronized GuestTests.RunResult result(String runId,String buildId,String operationId,String requestId,
GuestTests.Completion completion,AutomationFailure nativeFailure) {
String issue=problem;
String code=problemCode;
if(issue==null&&completion!=GuestTests.Completion.running&&(!suiteComplete||completion!=GuestTests.Completion.completed)) {
issue="Run ended without a complete valid suite: "+completion;code="incomplete_run";
}
var results=cases.values().stream().map(c->new GuestTests.CaseResult(c.id,!c.started?GuestTests.CaseState.not_run
:!c.ended?completion==GuestTests.Completion.running?null:GuestTests.CaseState.infrastructure_failure
:c.rejected||c.assertions.stream().anyMatch(a->!a.passed())?GuestTests.CaseState.failed:GuestTests.CaseState.passed,
c.assertions,c.checkpoints,c.started,c.ended)).toList();
var failure=nativeFailure;
if(issue!=null) {
var details=new LinkedHashMap<String,Object>(nativeFailure==null?Map.of():nativeFailure.details());
details.put("transcriptFailure",ValueMaps.fields("code",code,"message",issue,"lastSequence",lastSequence,"logEpoch",epoch,
"retainedBytes",bytes,"byteLimit",byteLimit));
failure=nativeFailure==null?new AutomationFailure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE,issue,Map.of(),details)
:new AutomationFailure(nativeFailure.code(),nativeFailure.message(),nativeFailure.runtimeError(),details,nativeFailure.observation());
}
var verdict=failure!=null?GuestTests.CaseState.infrastructure_failure
:suiteRejected||results.stream().anyMatch(c->c.state()==GuestTests.CaseState.failed)?GuestTests.CaseState.failed
:!suiteComplete||completion==GuestTests.Completion.running?GuestTests.CaseState.not_run:GuestTests.CaseState.passed;
return new GuestTests.RunResult(runId,target.id(),buildId,sessionId,operationId,requestId,completion,verdict,suiteComplete,loss,results,transcript,failure)
.withContext(ValueMaps.fields("historicalOverflow", historicalOverflow, "lastNativeSequence", lastSequence, "logEpoch", epoch));
}
private static int integer(Object value) {
if (!(value instanceof Number) || !value.toString().matches("0|[1-9][0-9]*")) throw invalid("Producer count/ordinal must be a nonnegative integer");
try { return new BigInteger(value.toString()).intValueExact(); }
catch (ArithmeticException outside) { throw invalid("Producer count/ordinal exceeds host retention bounds"); }
}
private void fail(String code,String message,boolean lost){problemCode=code;problem=message;loss|=lost;}
private static boolean explicitlyRejected(GuestTests.Event event) {
var value=event.payload().get("passed");
if(event.payload().containsKey("passed")&&!(value instanceof Boolean))throw invalid("Completion passed flag must be boolean");
return Boolean.FALSE.equals(value);
}
private static IllegalArgumentException invalid(String reason){return new IllegalArgumentException(reason);}
private static final class Case {
final String id;boolean started,ended,rejected;final List<GuestTests.Assertion> assertions=new ArrayList<>();final List<String> checkpoints=new ArrayList<>();
Case(String id){this.id=id;}
}
}
@@ -0,0 +1,27 @@
package p.studio.core.shipper;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.packer.messages.PackCapacityContract;
import p.studio.api.assets.AssetBudgets;
import java.nio.file.*;
import java.util.*;
/** Capacity provenance records the actual packed bytes; managed launch verifies it again. */
final class AssetBuildQualification {
static PackCapacityContract contract(AssetBudgets.Snapshot snapshot) {
if(snapshot==null||snapshot.qualification().profile()==null)return null;
var profile=snapshot.qualification().profile();var limits=new TreeMap<String,Integer>();
profile.limits().forEach((type,count)->limits.put(type.runtimeTag(),count));
return new PackCapacityContract(profile.profileId(),profile.executableSha256(),limits);
}
static Path write(ObjectMapper json,Path output,AssetBudgets.Snapshot snapshot)throws java.io.IOException {
var files=new TreeMap<String,String>();
for(String name:List.of("manifest.json","program.pbx","assets.pa","asset_table.json","preload.json","asset_table_metadata.json")) {
Path file=output.resolve(name);if(Files.isRegularFile(file))files.put(name,digest(Files.readAllBytes(file)));
}
var record=new LinkedHashMap<String,Object>();record.put("contractVersion",1);record.put("snapshot",snapshot);
record.put("qualification",snapshot==null?"unknown":snapshot.qualification().state().name());record.put("files",files);
Path file=output.resolve("asset-capacity.json");json.writerWithDefaultPrettyPrinter().writeValue(file.toFile(),record);return file;
}
static String digest(byte[] bytes){try{return HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(bytes));}catch(java.security.NoSuchAlgorithmException impossible){throw new AssertionError(impossible);}}
}
@@ -1,7 +1,7 @@
package p.studio.core.shipper;
import p.studio.api.diagnostic.StudioDiagnostic;
import p.studio.utilities.json.ImmutableJson;
import p.studio.api.values.ValueMaps;
import java.util.List;
import java.util.Map;
@@ -11,7 +11,7 @@ public final class CompilerBuildFailure extends RuntimeException {
private final Map<String, Object> context;
private final boolean payloadAvailable;
public CompilerBuildFailure(String message, List<StudioDiagnostic> issues, Map<String, Object> context, boolean payloadAvailable) {
super(message); this.issues = List.copyOf(issues); this.context = ImmutableJson.object(context); this.payloadAvailable = payloadAvailable;
super(message); this.issues = List.copyOf(issues); this.context = ValueMaps.object(context); this.payloadAvailable = payloadAvailable;
}
public List<StudioDiagnostic> issues() { return issues; }
public Map<String, Object> context() { return context; }
@@ -10,6 +10,13 @@ import java.util.function.Consumer;
public interface CompilerBuildGateway {
CompilerBuildOutput compile(ProjectReference project, Consumer<String> logSink);
default CompilerBuildOutput compile(ProjectReference project, p.studio.api.automation.BuildSelection selection,
Path outputDirectory, Consumer<String> logSink) {
if (!selection.production()) throw new UnsupportedOperationException("Test target compilation is not composed");
if(!outputDirectory.toAbsolutePath().normalize().equals(project.rootPath().resolve("build")))
throw new UnsupportedOperationException("Exclusive production output is not supported by this compiler adapter");
return compile(project, logSink);
}
record CompilerBuildOutput(Path bytecodeArtifact, List<String> requiredCapabilities,
List<StudioDiagnostic> issues, Map<String, Object> context) {
@@ -19,7 +26,7 @@ public interface CompilerBuildGateway {
public CompilerBuildOutput {
bytecodeArtifact = bytecodeArtifact == null ? null : bytecodeArtifact.toAbsolutePath().normalize();
requiredCapabilities = List.copyOf(requiredCapabilities);
issues = List.copyOf(issues); context = p.studio.utilities.json.ImmutableJson.object(context);
issues = List.copyOf(issues); context = p.studio.api.values.ValueMaps.object(context);
}
}
}
@@ -0,0 +1,39 @@
package p.studio.core.shipper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.studio.api.values.ValueMaps;
import java.util.*;
/** Bounded Studio aggregation of already-produced issues; no language interpretation. */
public final class DiagnosticViews {
public static final int MAX_ISSUES = 1024, MAX_BYTES = 1024 * 1024;
private static final ObjectMapper JSON = new ObjectMapper();
private DiagnosticViews() { }
public record View(List<Map<String, Object>> issues, Map<String, Object> summary) {
public View { issues = issues.stream().map(ValueMaps::object).toList(); summary = ValueMaps.object(summary); }
}
public static View view(List<Map<String, Object>> input, String scope, String evaluation,
List<Map<String, Object>> stages, String revisionCoverage) {
long errors = input.stream().filter(issue -> "ERROR".equals(issue.get("severity"))).count();
long warnings = input.stream().filter(issue -> "WARNING".equals(issue.get("severity"))).count();
long infos = input.stream().filter(issue -> "INFO".equals(issue.get("severity"))).count();
var issues = new ArrayList<Map<String, Object>>();
int bytes = 2;
for (var issue : input) {
final int size;
try { size = JSON.writeValueAsBytes(issue).length + (issues.isEmpty() ? 0 : 1); }
catch (JsonProcessingException failure) { throw new IllegalArgumentException("Non-JSON diagnostic", failure); }
if (issues.size() == MAX_ISSUES || bytes + size > MAX_BYTES) break;
issues.add(issue); bytes += size;
}
boolean complete = issues.size() == input.size();
var limitations = complete ? List.of() : List.of(ValueMaps.fields("code", "LIMIT_EXCEEDED",
"message", "Diagnostic details exceed the public payload budget", "maxIssues", MAX_ISSUES,
"maxBytes", MAX_BYTES, "totalIssues", input.size()));
return new View(issues, ValueMaps.fields("scope", scope, "validationStatus", evaluation,
"counts", ValueMaps.fields("errors", errors, "warnings", warnings, "infos", infos),
"stages", stages, "coverage", ValueMaps.fields("details", complete ? "complete" : "partial",
"revisions", revisionCoverage, "limitations", limitations)));
}
}
@@ -9,4 +9,8 @@ import java.util.function.Consumer;
@FunctionalInterface
public interface SavedProjectPreparation {
ShipperPrepareResult prepareNow(ProjectReference project, Consumer<ShipperLogEntry> sink);
default ShipperPrepareResult prepareNow(ProjectReference project, p.studio.api.automation.BuildSelection selection, Consumer<ShipperLogEntry> sink) {
if (!selection.production()) throw new UnsupportedOperationException("Test target preparation is not composed");
return prepareNow(project, sink);
}
}
@@ -28,6 +28,8 @@ import java.util.Objects;
import java.util.function.Consumer;
public final class StudioShipperService implements Shipper, SavedProjectPreparation {
private p.studio.core.assets.AssetBuildPreflight assetPreflight;
public StudioShipperService withAssetPreflight(p.studio.core.assets.AssetBuildPreflight preflight){this.assetPreflight=Objects.requireNonNull(preflight);return this;}
private p.studio.api.automation.ProjectAccess access = new p.studio.core.project.ProjectAccessRegistry();
public StudioShipperService(CompilerBuildGateway compiler, PackerWorkspaceService packer, ObjectMapper mapper, OperationExecutor operations, p.studio.api.automation.ProjectAccess access) {
this(compiler, packer, mapper, operations);
@@ -55,6 +57,90 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
}
@Override public ShipperPrepareResult prepareNow(ProjectReference project, Consumer<ShipperLogEntry> sink) {
return prepareNow(project, p.studio.api.automation.BuildSelection.productionBuild(), sink);
}
@Override public ShipperPrepareResult prepareNow(ProjectReference project, p.studio.api.automation.BuildSelection selection,
Consumer<ShipperLogEntry> sink) {
if (selection.production()) return assetPreflight==null?prepareInto(project,selection,project.rootPath().resolve("build"),sink):prepareProduction(project,selection,sink);
Path stage = null;
try {
var targetRoot = project.rootPath().resolve(".studio/test-builds").resolve(selection.targetId());
admitOutput(project.rootPath(), targetRoot);
Files.createDirectories(targetRoot);
var destination = targetRoot.resolve(selection.buildId());
if (Files.exists(destination)) throw new IllegalStateException("Immutable test build already exists");
stage = Files.createTempDirectory(targetRoot, ".staging-" + selection.buildId() + "-");
// Adopt the existing project identity before freezing the effective configuration.
p.studio.core.project.ProjectCartridgeConfiguration.adopt(project, mapper);
packer.initWorkspace(new p.packer.messages.InitWorkspaceRequest(new PackerProjectContext(project.name(),project.rootPath())));
// Existing packer validation materializes legacy identity before the frozen input revision.
packer.validatePackWorkspace(new ValidatePackWorkspaceRequest(new PackerProjectContext(project.name(), project.rootPath())));
var inputs = inputHashes(project.rootPath());
var result = prepareInto(project, selection, stage, sink);
if (!result.success()) return result;
checkCancelled();
if (!inputs.equals(inputHashes(project.rootPath()))) throw new IllegalStateException("Test build inputs changed before publication; build is not qualified");
verifyConsumedSources(result.context());
var sourceHashes = result.context().getOrDefault("sourceSha256", java.util.Map.of());
var provenance = p.studio.api.values.ValueMaps.fields("targetId", selection.targetId(), "buildId", selection.buildId(),
"entrypoint", result.context().get("entrypoint"), "sources", sourceHashes,
"dependencyConfigurationSha256", result.context().getOrDefault("configurationSha256", java.util.Map.of()),
"projectInputSha256", inputs, "configuration", result.context().get("configuration"),
"revision", digest(mapper.writer().with(com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).writeValueAsBytes(java.util.Map.of("targetId", selection.targetId(), "inputs", inputs, "sources", sourceHashes))));
mapper.writeValue(stage.resolve("build-provenance.json").toFile(), provenance);
checkCancelled();
Files.move(stage, destination, java.nio.file.StandardCopyOption.ATOMIC_MOVE);
final Path stagedRoot = stage;
return new ShipperPrepareResult(result.status(), result.logs(), destination.resolve(stagedRoot.relativize(result.manifestPath())),
result.emittedArtifacts().stream().map(p -> destination.resolve(stagedRoot.relativize(p))).toList(),
result.diagnostics(), result.issues(), result.summary(), result.context()).withContext(java.util.Map.of("buildProvenance", provenance));
} catch (java.util.concurrent.CancellationException cancelled) {
return ShipperPrepareResult.failed(List.of(), List.of("Test build cancelled before publication")).asCancelled();
} catch (IOException | RuntimeException failure) {
if (Thread.currentThread().isInterrupted()) return ShipperPrepareResult.failed(List.of(), List.of("Test build cancelled before publication")).asCancelled();
String message = Objects.requireNonNullElse(failure.getMessage(), "Test build publication failed");
var issue = p.studio.api.diagnostic.StudioDiagnostic.fromMap(p.studio.api.diagnostic.DiagnosticValues.issue("TEST_BUILD_PUBLICATION_FAILED", "ERROR", message));
return finish(ShipperPreparationStatus.FAILED, List.of(), null, List.of(), List.of(issue),
java.util.Map.of("test-publication", "failed"), java.util.Map.of("targetId", selection.targetId(), "buildAttemptId", selection.buildId()), "failed");
} finally {
if (stage != null && Files.exists(stage)) {
try (var paths = Files.walk(stage)) {
for (var path : paths.sorted(java.util.Comparator.reverseOrder()).toList()) Files.deleteIfExists(path);
} catch (IOException ignored) { /* Hidden incomplete staging is never a published build. */ }
}
}
}
private ShipperPrepareResult prepareProduction(ProjectReference project,p.studio.api.automation.BuildSelection selection,Consumer<ShipperLogEntry> sink) {
Path stage=null;
try {
var home=project.rootPath().resolve(".studio/build-staging");admitOutput(project.rootPath(),home);Files.createDirectories(home);
p.studio.core.project.ProjectCartridgeConfiguration.adopt(project,mapper);
packer.validatePackWorkspace(new ValidatePackWorkspaceRequest(new PackerProjectContext(project.name(),project.rootPath())));
packer.initWorkspace(new p.packer.messages.InitWorkspaceRequest(new PackerProjectContext(project.name(),project.rootPath())));
var inputs=inputHashes(project.rootPath());stage=Files.createTempDirectory(home,"production-");
var result=prepareInto(project,selection,stage,sink);if(!result.success())return result;
verifyConsumedSources(result.context());checkCancelled();
final Path candidate=stage;return packer.withProject(new PackerProjectContext(project.name(),project.rootPath()),()->{
if(!inputs.equals(inputHashes(project.rootPath())))throw new IOException("Project assets changed before production publication");
var edits=new ArrayList<p.packer.messages.PublishWorkspaceFilesRequest.Edit>();
try(var files=Files.list(candidate)){for(Path file:files.filter(Files::isRegularFile).toList()) {
String relative="build/"+file.getFileName();Path prior=project.rootPath().resolve(relative);
edits.add(new p.packer.messages.PublishWorkspaceFilesRequest.Edit(relative,Files.exists(prior)?digest(Files.readAllBytes(prior)):null,Files.readAllBytes(file)));
}}
checkCancelled();packer.publishWorkspaceFiles(new p.packer.messages.PublishWorkspaceFilesRequest(new PackerProjectContext(project.name(),project.rootPath()),java.util.UUID.randomUUID().toString(),edits));
Path build=project.rootPath().resolve("build");return new ShipperPrepareResult(result.status(),result.logs(),build.resolve("manifest.json"),
result.emittedArtifacts().stream().map(p->build.resolve(candidate.relativize(p))).toList(),result.diagnostics(),result.issues(),result.summary(),result.context());
});
}catch(Exception failure) {
if(Thread.currentThread().isInterrupted()||failure instanceof java.util.concurrent.CancellationException)return ShipperPrepareResult.failed(List.of(),List.of("Production build cancelled before publication")).asCancelled();
return ShipperPrepareResult.failed(List.of(),List.of(Objects.requireNonNullElse(failure.getMessage(),"Production publication failed")));
}finally{if(stage!=null&&Files.exists(stage))try(var paths=Files.walk(stage)){for(Path path:paths.sorted(java.util.Comparator.reverseOrder()).toList())Files.deleteIfExists(path);}catch(IOException ignored){}}
}
private ShipperPrepareResult prepareInto(ProjectReference project, p.studio.api.automation.BuildSelection selection,
Path outputRoot, Consumer<ShipperLogEntry> sink) {
Objects.requireNonNull(project); Objects.requireNonNull(sink);
var logs = new ArrayList<ShipperLogEntry>();
var issues = new ArrayList<p.studio.api.diagnostic.StudioDiagnostic>();
@@ -64,11 +150,11 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
String current = "compiler-build";
try {
checkCancelled(); stages.put(current, "running");
var build = compiler.compile(project,
var build = compiler.compile(project, selection, outputRoot,
message -> emit(logs, sink, ShipperLogEntry.info(ShipperLogSource.BUILD, normalize(message))));
inputs = build.context().isEmpty() ? java.util.Map.of("projectRoot", project.rootPath().toString(), "compilerDiagnosticCoverage", "legacy") : build.context();
issues.addAll(build.issues());
var compilerEvaluation = p.studio.utilities.json.DiagnosticPayloads.evaluate(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList());
var compilerEvaluation = p.studio.api.diagnostic.DiagnosticValues.evaluate(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList());
if (!"passed".equals(compilerEvaluation)) {
stages.put(current, compilerEvaluation);
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, compilerEvaluation);
@@ -85,11 +171,11 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
emit(logs, sink, logFor(validation.status(), ShipperLogSource.PACK_VALIDATION, validation.summary()));
int validationIssueStart = issues.size();
for (var asset : validation.assets()) for (var diagnostic : asset.diagnostics()) {
issues.add(packerIssue("PACKER_" + diagnostic.category().name(), diagnostic.severity().name(), diagnostic.message(),
p.studio.utilities.json.ImmutableJson.fields("category", diagnostic.category().name(), "blocking", diagnostic.blocking(),
var facts=new java.util.LinkedHashMap<String,Object>(diagnostic.context());facts.putAll(p.studio.api.values.ValueMaps.fields("category", diagnostic.category().name(), "blocking", diagnostic.blocking(),
"assetId", asset.assetId(), "assetName", asset.assetName(), "assetPath", asset.assetPath().toString(),
"evidencePath", diagnostic.evidencePath() == null ? null : diagnostic.evidencePath().toString(),
"bankAvailability", "not-recorded"), project, asset.assetPath()));
"bankAvailability",diagnostic.context().containsKey("family")?"recorded":"not-recorded","producerCodeAvailability",diagnostic.code()==null?"not-recorded":"recorded"));
issues.add(packerIssue(Objects.requireNonNullElse(diagnostic.code(),"PACKER_"+diagnostic.category().name()),diagnostic.severity().name(),diagnostic.message(),facts,project,asset.assetPath()));
}
checkCancelled();
if (validation.status() != PackerOperationStatus.SUCCESS || !validation.canPack()) {
@@ -99,9 +185,18 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, "rejected");
}
stages.put(current, "passed"); checkCancelled(); current = "pack"; stages.put(current, "running");
var packed = packer.packWorkspace(new PackWorkspaceRequest(context));
var capacity=assetPreflight==null?null:assetPreflight.inspect(project);
if(capacity!=null) {
var expanded=new java.util.LinkedHashMap<String,Object>(inputs);expanded.put("assetCapacity",mapper.convertValue(capacity,java.util.Map.class));inputs=expanded;
for(var diagnostic:capacity.diagnostics())issues.add(assetIssue(diagnostic,project));
if(!capacity.buildAllowed()) {
stages.put(current,"rejected");return finish(ShipperPreparationStatus.FAILED,logs,null,List.of(),issues,stages,inputs,"rejected");
}
}
var packed = packer.packWorkspace(new PackWorkspaceRequest(context,outputRoot,AssetBuildQualification.contract(capacity)));
checkCancelled(); emit(logs, sink, logFor(packed.status(), ShipperLogSource.PACK, packed.summary()));
if (packed.status() != PackerOperationStatus.SUCCESS) {
for(var diagnostic:packed.diagnostics())issues.add(packerIssue(Objects.requireNonNullElse(diagnostic.code(),"PACKER_BUILD"),diagnostic.severity().name(),diagnostic.message(),diagnostic.context(),project,diagnostic.evidencePath()));
issues.add(packerIssue("PACKER_BUILD", "ERROR", packed.summary(), java.util.Map.of("category", "BUILD", "blocking", true), project, null));
stages.put(current, "rejected");
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, "rejected");
@@ -110,7 +205,8 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
var artifacts = packed.result().emittedArtifacts().stream().map(value -> value.path().toAbsolutePath().normalize()).toList();
artifacts.forEach(path -> emit(logs, sink, ShipperLogEntry.info(ShipperLogSource.PACK, "Emitted " + path)));
current = "manifest"; stages.put(current, "running"); checkCancelled();
var manifest = writeManifest(project, build, logs, sink); checkCancelled(); stages.put(current, "passed");
var manifest = writeManifest(project, build, outputRoot, logs, sink); checkCancelled(); stages.put(current, "passed");
AssetBuildQualification.write(mapper,outputRoot,capacity);
return finish(ShipperPreparationStatus.SUCCESS, logs, manifest, artifacts, issues, stages, inputs, "passed");
} catch (java.util.concurrent.CancellationException cancelled) {
stages.put(current, "cancelled");
@@ -121,11 +217,11 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
stages.put(current, "cancelled");
return finish(ShipperPreparationStatus.CANCELLED, logs, null, List.of(), issues, stages, inputs, "cancelled");
}
var evaluation = failure.payloadAvailable() ? p.studio.utilities.json.DiagnosticPayloads.evaluate(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList()) : "failed";
var evaluation = failure.payloadAvailable() ? p.studio.api.diagnostic.DiagnosticValues.evaluate(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList()) : "failed";
if ("passed".equals(evaluation) || "failed".equals(inputs.get("validationStatus"))) evaluation = "failed";
if (issues.isEmpty()) {
var unknown = new java.util.LinkedHashMap<>(p.studio.utilities.json.DiagnosticPayloads.issue("STUDIO_COMPILER_FAILURE", "ERROR", failure.getMessage()));
unknown.put("limitations", List.of(p.studio.utilities.json.DiagnosticPayloads.limitation("DIAGNOSTICS_UNAVAILABLE", "Compiler did not provide structured issues")));
var unknown = new java.util.LinkedHashMap<>(p.studio.api.diagnostic.DiagnosticValues.issue("STUDIO_COMPILER_FAILURE", "ERROR", failure.getMessage()));
unknown.put("limitations", List.of(p.studio.api.diagnostic.DiagnosticValues.limitation("DIAGNOSTICS_UNAVAILABLE", "Compiler did not provide structured issues")));
issues.add(p.studio.api.diagnostic.StudioDiagnostic.fromMap(unknown));
}
stages.put(current, "rejected".equals(evaluation) ? "rejected" : "failed");
@@ -137,8 +233,8 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
return finish(ShipperPreparationStatus.CANCELLED, logs, null, List.of(), issues, stages, inputs, "cancelled");
}
String message = Objects.requireNonNullElse(failure.getMessage(), failure.getClass().getSimpleName());
var value = new java.util.LinkedHashMap<>(p.studio.utilities.json.DiagnosticPayloads.issue("STUDIO_BUILD_FAILED", "ERROR", message));
value.put("producer", p.studio.utilities.json.ImmutableJson.fields("component", "studio", "owner", "studio", "languageId", null));
var value = new java.util.LinkedHashMap<>(p.studio.api.diagnostic.DiagnosticValues.issue("STUDIO_BUILD_FAILED", "ERROR", message));
value.put("producer", p.studio.api.values.ValueMaps.fields("component", "studio", "owner", "studio", "languageId", null));
value.put("stageId", current); value.put("phase", current);
value.put("failureKind", failure instanceof IOException || failure.getCause() instanceof IOException ? "infrastructure-failure" : "unknown");
issues.add(p.studio.api.diagnostic.StudioDiagnostic.fromMap(value)); stages.put(current, "failed");
@@ -150,19 +246,19 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
private ShipperPrepareResult finish(ShipperPreparationStatus status, List<ShipperLogEntry> logs, Path manifest,
List<Path> artifacts, List<p.studio.api.diagnostic.StudioDiagnostic> issues,
java.util.Map<String, String> stages, java.util.Map<String, Object> context, String evaluation) {
var view = p.studio.utilities.json.DiagnosticPayloads.view(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList(),
"build", evaluation, stages.entrySet().stream().map(entry -> p.studio.utilities.json.DiagnosticPayloads.stage(entry.getKey(), entry.getValue())).toList(), "partial");
var view = p.studio.core.shipper.DiagnosticViews.view(issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::toMap).toList(),
"build", evaluation, stages.entrySet().stream().map(entry -> p.studio.api.diagnostic.DiagnosticValues.stage(entry.getKey(), entry.getValue())).toList(), "partial");
var summary = new java.util.LinkedHashMap<>(view.summary());
var coverage = new java.util.LinkedHashMap<>((java.util.Map<String, Object>)summary.get("coverage"));
var limitations = new ArrayList<>((List<java.util.Map<String, Object>>)coverage.get("limitations"));
if ("legacy".equals(context.get("compilerDiagnosticCoverage"))) {
summary.put("counts", p.studio.utilities.json.ImmutableJson.fields("errors", null, "warnings", null, "infos", null));
summary.put("counts", p.studio.api.values.ValueMaps.fields("errors", null, "warnings", null, "infos", null));
coverage.put("details", issues.isEmpty() ? "unavailable" : "partial");
limitations.add(p.studio.utilities.json.DiagnosticPayloads.limitation("LEGACY_COMPILER_DIAGNOSTICS", "Compiler gateway did not supply a structured diagnostic collection"));
limitations.add(p.studio.api.diagnostic.DiagnosticValues.limitation("LEGACY_COMPILER_DIAGNOSTICS", "Compiler gateway did not supply a structured diagnostic collection"));
}
if (status == ShipperPreparationStatus.CANCELLED) {
coverage.put("details", "partial");
limitations.add(p.studio.utilities.json.DiagnosticPayloads.limitation("CANCELLED_EVALUATION", "Only observations collected before cancellation are available"));
limitations.add(p.studio.api.diagnostic.DiagnosticValues.limitation("CANCELLED_EVALUATION", "Only observations collected before cancellation are available"));
}
coverage.put("limitations", limitations); summary.put("coverage", coverage);
return new ShipperPrepareResult(status, logs, manifest, artifacts, issues.stream().map(p.studio.api.diagnostic.StudioDiagnostic::message).toList(),
@@ -171,21 +267,29 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
private p.studio.api.diagnostic.StudioDiagnostic packerIssue(String code, String severity, String message,
java.util.Map<String, Object> facts, ProjectReference project, Path path) {
var value = new java.util.LinkedHashMap<>(p.studio.utilities.json.DiagnosticPayloads.issue(code, severity, message));
value.put("producer", p.studio.utilities.json.ImmutableJson.fields("component", "packer", "owner", "packer", "languageId", null));
var value = new java.util.LinkedHashMap<>(p.studio.api.diagnostic.DiagnosticValues.issue(code, severity, message));
value.put("producer", p.studio.api.values.ValueMaps.fields("component", "packer", "owner", "packer", "languageId", null));
value.put("phase", "pack"); value.put("facts", facts); value.put("failureKind", "program-rejection");
value.put("origin", p.studio.utilities.json.ImmutableJson.fields("uri", path != null && path.startsWith(project.rootPath()) ? path.toUri().toString() : null,
value.put("origin", p.studio.api.values.ValueMaps.fields("uri", path != null && path.startsWith(project.rootPath()) ? path.toUri().toString() : null,
"span", null, "range", null, "availability", path == null ? "unavailable" : "partial"));
return p.studio.api.diagnostic.StudioDiagnostic.fromMap(value);
}
private p.studio.api.diagnostic.StudioDiagnostic assetIssue(p.studio.api.assets.AssetBatches.Diagnostic diagnostic,ProjectReference project) {
var facts=new java.util.LinkedHashMap<String,Object>(diagnostic.context());
if(diagnostic.used()!=null)facts.put("used",diagnostic.used());if(diagnostic.required()!=null)facts.put("required",diagnostic.required());if(diagnostic.limit()!=null)facts.put("limit",diagnostic.limit());
facts.put("blocking",diagnostic.blocking());
var issue=new java.util.LinkedHashMap<String,Object>(packerIssue(diagnostic.code(),diagnostic.severity(),diagnostic.message(),facts,project,diagnostic.path()==null?null:Path.of(diagnostic.path())).toMap());
issue.put("producer",p.studio.api.values.ValueMaps.fields("component",diagnostic.producer(),"owner",diagnostic.producer(),"languageId",null));
return p.studio.api.diagnostic.StudioDiagnostic.fromMap(issue);
}
private static void checkCancelled() {
if (Thread.currentThread().isInterrupted()) throw new java.util.concurrent.CancellationException("Build cancelled");
}
private Path writeManifest(ProjectReference project, CompilerBuildGateway.CompilerBuildOutput build,
private Path writeManifest(ProjectReference project, CompilerBuildGateway.CompilerBuildOutput build, Path buildRoot,
List<ShipperLogEntry> logs, Consumer<ShipperLogEntry> sink) {
final Path buildRoot = project.rootPath().resolve("build").toAbsolutePath().normalize();
final Path manifestPath = buildRoot.resolve("manifest.json");
try {
Files.createDirectories(buildRoot);
@@ -211,6 +315,45 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
}
}
private static void admitOutput(Path root, Path output) throws IOException {
Path parent = output;
while (parent != null && !Files.exists(parent, java.nio.file.LinkOption.NOFOLLOW_LINKS)) parent = parent.getParent();
if (parent == null || !parent.toRealPath().startsWith(root.toRealPath())) throw new IOException("Build output escapes the project");
}
private java.util.Map<String, String> inputHashes(Path root) throws IOException {
Path canonicalRoot=root.toRealPath();
var hashes = new java.util.TreeMap<String, String>();
hashes.put("prometeu.json", digest(Files.readAllBytes(root.resolve("prometeu.json"))));
var registry = root.resolve(".prometeu/index.json");
if (Files.isRegularFile(registry)) hashes.put(".prometeu/index.json", digest(Files.readAllBytes(registry)));
var assets = root.resolve("assets");
if (Files.isDirectory(assets)) try (var paths = Files.walk(assets)) {
for (var file : paths.filter(Files::isRegularFile).sorted().toList()) {
if (!file.toRealPath().startsWith(canonicalRoot)) throw new IOException("Asset input escapes project scope");
if(root.relativize(file).toString().equals("assets/.prometeu/cache.json"))continue;
hashes.put(root.relativize(file).toString(), digest(Files.readAllBytes(file)));
}
}
return java.util.Collections.unmodifiableMap(hashes);
}
private static void verifyConsumedSources(java.util.Map<String, Object> context) throws IOException {
if (!(context.get("diskSourceSha256") instanceof java.util.Map<?, ?> hashes) || hashes.isEmpty())
throw new IOException("Test compiler did not supply consumed-source hashes");
for (var entry : hashes.entrySet()) {
var path = Path.of((String) entry.getKey());
if (!path.equals(path.toRealPath()) || !digest(Files.readAllBytes(path)).equals(entry.getValue()))
throw new IOException("Consumed source changed before publication: " + path);
}
if (!(context.get("configurationSha256") instanceof java.util.Map<?, ?> configuration) || configuration.isEmpty())
throw new IOException("Test compiler did not supply consumed configuration hashes");
for (var entry : configuration.entrySet()) if (!digest(Files.readAllBytes(Path.of((String)entry.getKey()))).equals(entry.getValue()))
throw new IOException("Resolved dependency configuration changed before publication: " + entry.getKey());
}
private static String digest(byte[] bytes) {
try { return java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(bytes)); }
catch (java.security.NoSuchAlgorithmException impossible) { throw new IllegalStateException(impossible); }
}
private JsonNode readArrayOrEmpty(Path path) throws IOException {
if (!Files.isRegularFile(path)) {
return mapper.createArrayNode();
@@ -0,0 +1,22 @@
package p.studio.core.project;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class DiagnosticNavigationTest {
@TempDir Path root;
@Test void unicodeRangeIsSelectedOnlyForMatchingAdmittedRevision() throws Exception {
Path file = root.resolve("main.pbs"); byte[] bytes = "// á 😀\r\nfn x() {}\n".getBytes(java.nio.charset.StandardCharsets.UTF_8); Files.write(file, bytes);
String sha = HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(bytes));
var origin = Map.<String, Object>of("uri", file.toUri().toString(), "revision", sha, "range", Map.of("start", Map.of("line", 1, "character", 3), "end", Map.of("line", 1, "character", 4)));
var current = DiagnosticNavigation.read(root, origin); assertEquals("x", current.text().substring(current.start(), current.end()));
Files.writeString(file, "changed"); var stale = DiagnosticNavigation.read(root, origin);
assertTrue(stale.status().startsWith("Stale")); assertEquals(0, stale.start()); assertEquals(0, stale.end());
assertNull(DiagnosticNavigation.read(root.resolve("other"), origin).path());
assertNull(DiagnosticNavigation.read(root, Map.of("uri", "prometeu-source:unowned")).path());
assertNull(DiagnosticNavigation.read(root, Map.of()).path());
}
}
@@ -0,0 +1,47 @@
package p.studio.core.session;
import org.junit.jupiter.api.Test;
import p.studio.api.automation.Evidence;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CheckpointComparisonTest {
final CheckpointComparison service=new CheckpointComparison();
Evidence.Pixels image(byte... rgba){return new Evidence.Pixels(rgba.length/4,1,rgba);}
Evidence.Pixels a(){return image((byte)10,(byte)20,(byte)30,(byte)0,(byte)40,(byte)50,(byte)60,(byte)255);}
@Test void exactIndependentPixelsAndDeliberateDeltaRetainOriginalCoordinates() {
var pass=service.compare(a(),a(),Evidence.Parameters.exact(Evidence.Alpha.compare));
assertEquals(Evidence.Verdict.passed,pass.verdict());assertEquals(2,pass.comparedPixels());assertNull(pass.boundingBox());
var fail=service.compare(a(),image((byte)10,(byte)20,(byte)31,(byte)0,(byte)44,(byte)50,(byte)60,(byte)255),Evidence.Parameters.exact(Evidence.Alpha.compare));
assertEquals(Evidence.Verdict.rejected,fail.verdict());assertEquals(2,fail.differentPixels());assertEquals(4,fail.maximumChannelError());
assertEquals(new Evidence.Rect(0,0,2,1),fail.boundingBox());assertEquals(255,Byte.toUnsignedInt(fail.diff().rgba()[3]));
}
@Test void alphaPolicyIsExplicitAndTransparentRgbStillCounts() {
assertThrows(NullPointerException.class,()->Evidence.Parameters.exact(null));
var alpha=image((byte)10,(byte)20,(byte)30,(byte)1,(byte)40,(byte)50,(byte)60,(byte)255);
assertEquals(1,service.compare(a(),alpha,Evidence.Parameters.exact(Evidence.Alpha.compare)).differentPixels());
assertEquals(0,service.compare(a(),alpha,Evidence.Parameters.exact(Evidence.Alpha.ignore)).differentPixels());
var rgb=image((byte)11,(byte)20,(byte)30,(byte)0,(byte)40,(byte)50,(byte)60,(byte)255);
assertEquals(1,service.compare(a(),rgb,Evidence.Parameters.exact(Evidence.Alpha.ignore)).differentPixels());
}
@Test void halfOpenRoiAndOverlappingMaskUnionAreNotClipped() {
var expected=new Evidence.Pixels(3,2,new byte[24]);var actual=new byte[24];actual[0]=99;actual[20]=1;
var p=new Evidence.Parameters(new Evidence.Rect(1,0,2,2),List.of(new Evidence.Rect(1,0,1,1),new Evidence.Rect(1,0,2,1)),Evidence.Alpha.ignore,0,0);
var r=service.compare(expected,new Evidence.Pixels(3,2,actual),p);
assertEquals(2,r.comparedPixels());assertEquals(1,r.differentPixels());assertEquals(new Evidence.Rect(2,1,1,1),r.boundingBox());
assertEquals(0,r.diff().rgba()[3]);assertEquals(255,Byte.toUnsignedInt(r.diff().rgba()[19]));
assertThrows(IllegalArgumentException.class,()->service.compare(a(),a(),new Evidence.Parameters(new Evidence.Rect(1,0,2,1),List.of(),Evidence.Alpha.compare,0,0)));
}
@Test void toleranceIsInclusiveAndDifferentPixelAllowanceIsSeparate() {
var changed=image((byte)12,(byte)20,(byte)30,(byte)0,(byte)43,(byte)50,(byte)60,(byte)255);
var r=service.compare(a(),changed,new Evidence.Parameters(null,List.of(),Evidence.Alpha.compare,2,1));
assertEquals(Evidence.Verdict.passed,r.verdict());assertEquals(1,r.differentPixels());assertEquals(3,r.maximumChannelError());assertEquals(new Evidence.Rect(1,0,1,1),r.boundingBox());
assertEquals(Evidence.Verdict.rejected,service.compare(a(),changed,new Evidence.Parameters(null,List.of(),Evidence.Alpha.compare,2,0)).verdict());
}
@Test void emptyRegionsAndBadLimitsAreOperationalErrorsWhileDimensionMismatchIsARejection() {
assertThrows(IllegalArgumentException.class,()->service.compare(a(),a(),new Evidence.Parameters(null,List.of(new Evidence.Rect(0,0,2,1)),Evidence.Alpha.compare,0,0)));
assertThrows(IllegalArgumentException.class,()->new Evidence.Parameters(null,List.of(),Evidence.Alpha.compare,256,0));
var mismatch=service.compare(a(),image((byte)0,(byte)0,(byte)0,(byte)0),Evidence.Parameters.exact(Evidence.Alpha.compare));
assertEquals(Evidence.Verdict.dimension_mismatch,mismatch.verdict());assertEquals(0,mismatch.comparedPixels());
}
}
@@ -17,6 +17,14 @@ class DefaultAutomationSessionTest {
DefaultAutomationSession session() { return new DefaultAutomationSession(root, operations); }
ControlResult await(CompletionStage<ControlResult> result) { return result.toCompletableFuture().join(); }
TaskIdentity start(DefaultAutomationSession s) { assertEquals(GRANTED, await(s.grant(false)).status()); return s.acquire().task(); }
@Test void manualTestRuntimeUsesExistingLeaseAndPreventsGrantUntilExplicitCleanup() {
var s=session();var runtime=new ManagedHeadlessSession(){public String sessionId(){return "manual-test";}public void cancel(){}public CompletionStage<Void> shutdown(){return CompletableFuture.completedFuture(null);}};
assertThrows(ControlDeniedException.class,()->s.attachRuntime(null,runtime));
try(var lease=s.enterManual()){s.attachRuntime(null,runtime);assertTrue(s.runtimeAttached());}
assertEquals(BUSY,await(s.grant(false)).status());
try(var lease=s.enterManual()){s.detachRuntime(null,runtime);}
assertFalse(s.runtimeAttached());assertEquals(GRANTED,await(s.grant(false)).status());await(s.reclaim());
}
@Test void publicationIsRequiredAndCloseWaitsForAbortedPublicationWithoutHoldingMonitor() throws Exception {
var entered = new CountDownLatch(1); var proceed = new CountDownLatch(1);
@@ -162,9 +170,12 @@ class DefaultAutomationSessionTest {
docs.refresh = new CompletableFuture<>();
var returned = s.reclaim().toCompletableFuture();
assertEquals(ControlState.RETURNING_CONTROL, s.snapshot().state());
assertEquals(ControlObservation.Cleanup.PENDING, s.snapshot().controlObservation().cleanup());
assertTrue(s.snapshot().controlObservation().taskRevoked());
assertFalse(returned.isDone()); assertFalse(s.snapshot().manualAllowed());
docs.refresh.complete(null);
assertEquals(RETURNED, returned.join().status()); assertTrue(s.snapshot().manualAllowed());
assertEquals(ControlObservation.Cleanup.COMPLETED, returned.join().observation().cleanup());
}
@Test void revocationInvalidatesFirstAndWaitsForWorkerAndRuntimeBeforeRefresh() {
@@ -200,7 +211,11 @@ class DefaultAutomationSessionTest {
@Test void failedCleanupKeepsGuardAndHumanCanRetry() {
var s = session(); var docs = new Documents(); s.registerDocuments(docs); start(s);
docs.refresh = CompletableFuture.failedFuture(new IllegalStateException("disk unavailable"));
assertEquals(CLEANUP_FAILED, await(s.reclaim()).status());
var failed = await(s.reclaim());
assertEquals(CLEANUP_FAILED, failed.status());
assertEquals(ControlObservation.Cleanup.FAILED, failed.observation().cleanup());
assertTrue(failed.observation().taskRevoked());
assertEquals(AutomationFailure.Code.CLEANUP_FAILED, failed.observation().failure().code());
assertThrows(ControlDeniedException.class, s::enterManual);
docs.refresh = CompletableFuture.completedFuture(null);
assertEquals(RETURNED, await(s.reclaim()).status()); assertTrue(s.snapshot().manualAllowed());
@@ -28,12 +28,17 @@ final class DefaultProjectSessionTest {
final var project = project();
final var session = factory.open(project);
var source = temporaryDirectory.resolve("diagnostic.pbs"); java.nio.file.Files.writeString(source, "fn x() {}\n");
var origin = java.util.Map.<String, Object>of("uri", source.toUri().toString());
var navigation = session.diagnosticSources();
assertEquals("fn x() {}\n", navigation.read(origin).text());
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();
assertEquals("Project closed", navigation.read(origin).status()); assertNull(navigation.read(origin).path());
assertThrows(p.studio.api.automation.AutomationException.class, () -> access.requireOpenRoot(project.rootPath()));
assertEquals(1, lsp.boots);
@@ -0,0 +1,18 @@
package p.studio.core.session;
import org.junit.jupiter.api.Test;
import p.studio.api.automation.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class GuestFailureProjectionTest {
@Test void nativeWatchdogAndGuestFaultStayExactAndSeparateFromObservationMetadata() {
var error=Map.<String,Object>of("code","watchdog_expired","message","native message","details",Map.of("hostTickLimit",100000));
var observation=RuntimeObservation.unknown("session","build").transition(RuntimeObservation.Lifecycle.BOOTED,"advance",Map.of("error",error,"runtimeVersion","fixture"));
var failure=DefaultGuestTestTools.nativeFailure(new AutomationOutcome(AutomationOutcome.Kind.INFRASTRUCTURE_FAILURE,1,0,List.of(),null,Map.of(),observation));
assertEquals(error,failure.runtimeError());assertEquals("native message",failure.message());assertFalse(failure.runtimeError().containsKey("runtimeVersion"));assertSame(observation,failure.observation());
var fault=Map.<String,Object>of("kind","vm_panic","summary","native panic","pc",7);
var failed=DefaultGuestTestTools.nativeFailure(new AutomationOutcome(AutomationOutcome.Kind.GUEST_FAULT,1,0,List.of(),null,Map.of(),observation.transition(RuntimeObservation.Lifecycle.GUEST_OBSERVED,"fault",Map.of("fault",fault))));
assertEquals(fault,failed.runtimeError());assertEquals("native panic",failed.message());
}
}
@@ -0,0 +1,115 @@
package p.studio.core.session;
import org.junit.jupiter.api.Test;
import p.studio.api.automation.*;
import java.math.BigInteger;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class GuestTranscriptTest {
final Map<String,GuestTests.Event> messages=new HashMap<>();int messageId,eventOrdinal;
GuestTests.Target target(List<String> cases){return new GuestTests.Target("life","tests/main.pbs",List.of("tests"),List.of(),List.of(),cases,41000);}
GuestTranscript transcript(List<String> cases){return new GuestTranscript(target(cases),123,m->{var event=messages.get(m);if(event==null)throw new IllegalArgumentException("Malformed JSON");return event;});}
GuestTests.Event event(GuestTests.EventKind kind,String caseId,String id,Map<String,Object> payload){
if(kind==GuestTests.EventKind.suite_start)eventOrdinal=0;
var values=new HashMap<>(payload);values.put("ordinal",eventOrdinal++);
if(kind==GuestTests.EventKind.suite_end)values.put("eventCount",eventOrdinal);
return new GuestTests.Event(1,"suite",caseId,id,kind,values);
}
GuestTests.LogEvent nativeEvent(int seq,GuestTests.Event event){String key="event-"+messageId++;messages.put(key,event);return new GuestTests.LogEvent(BigInteger.valueOf(seq),BigInteger.ONE,BigInteger.valueOf(100+seq),"info","app",123L,41000,key);}
List<GuestTests.LogEvent> complete(boolean passed) {
var events=List.of(event(GuestTests.EventKind.suite_start,null,"suite-start",Map.of()),
event(GuestTests.EventKind.case_start,"a","a-start",Map.of()),
event(GuestTests.EventKind.assertion,"a","a-assert",Map.of("name","health","message","public state","expected",10,"actual",passed?10:9,"passed",passed)),
event(GuestTests.EventKind.case_end,"a","a-end",Map.of()),
event(GuestTests.EventKind.case_start,"b","b-start",Map.of()),
event(GuestTests.EventKind.assertion,"b","b-assert",Map.of("name","save","message","reopened","passed",true)),
event(GuestTests.EventKind.case_end,"b","b-end",Map.of()),event(GuestTests.EventKind.suite_end,null,"suite-end",Map.of()));
var nativeEvents=new ArrayList<GuestTests.LogEvent>();for(int i=0;i<events.size();i++)nativeEvents.add(nativeEvent(i,events.get(i)));return nativeEvents;
}
GuestTests.LogPage page(List<GuestTests.LogEvent> records,int newest){return new GuestTests.LogPage("session",BigInteger.ZERO,records,BigInteger.ZERO,BigInteger.valueOf(newest),records.isEmpty()?null:records.getLast().sequence(),false,false);}
GuestTests.RunResult result(GuestTranscript t,GuestTests.Completion completion){return t.result("run","build","operation","request",completion,null);}
@Test void paginatedSequenceZeroRequeryAndKnownFailureProduceCompleteDistinctVerdicts() {
var records=complete(true);var t=transcript(List.of("a","b"));
var first=page(records.subList(0,4),7);t.accept(first);assertEquals(BigInteger.valueOf(3),t.cursor());
assertEquals(GuestTests.CaseState.not_run,result(t,GuestTests.Completion.running).verdict());
t.accept(first);t.accept(page(records.subList(4,8),7));var passed=result(t,GuestTests.Completion.completed);
assertEquals(GuestTests.CaseState.passed,passed.verdict());assertEquals(8,passed.transcript().size());assertEquals(BigInteger.ZERO,passed.transcript().getFirst().nativeEvent().sequence());
assertTrue(passed.cases().getFirst().assertions().getFirst().expectedAvailable());assertFalse(passed.cases().get(1).assertions().getFirst().expectedAvailable());
var failed=transcript(List.of("a","b"));failed.accept(page(complete(false),7));var rejected=result(failed,GuestTests.Completion.completed);
assertEquals(GuestTests.CaseState.failed,rejected.verdict());assertEquals(GuestTests.CaseState.failed,rejected.cases().getFirst().state());assertNull(rejected.failure());
assertEquals(9,rejected.cases().getFirst().assertions().getFirst().actual());
var running=transcript(List.of("a","b"));running.accept(page(records.subList(0,3),7));
var active=result(running,GuestTests.Completion.running).cases().getFirst();assertTrue(active.started());assertFalse(active.completed());assertNull(active.state());
}
@Test void unreadGapsAndStreamOrSessionCursorChangesAreInfrastructureFailures() {
{
var t=transcript(List.of("a"));t.accept(new GuestTests.LogPage("session",BigInteger.ZERO,List.of(),null,null,null,true,true));
var r=result(t,GuestTests.Completion.infrastructure_failure);assertEquals(GuestTests.CaseState.infrastructure_failure,r.verdict());assertTrue(r.lossDetected());
}
for(String session:List.of("session","other")) {
var t=transcript(List.of("a"));var record=nativeEvent(0,event(GuestTests.EventKind.suite_start,null,"suite-start",Map.of()));t.accept(page(List.of(record),0));
t.accept(new GuestTests.LogPage(session,BigInteger.ONE,List.of(record),BigInteger.ZERO,BigInteger.ZERO,BigInteger.ZERO,false,false));
assertTrue(t.failedInfrastructure());assertTrue(result(t,GuestTests.Completion.infrastructure_failure).lossDetected());
}
}
@Test void historicalOverflowWithoutUnreadLossDoesNotRejectAPromptReader() {
var records=complete(true);var t=transcript(List.of("a","b"));
t.accept(page(records.subList(0,4),7));
t.accept(new GuestTests.LogPage("session",BigInteger.ZERO,records.subList(4,8),BigInteger.valueOf(4),BigInteger.valueOf(7),BigInteger.valueOf(7),false,true));
var result=result(t,GuestTests.Completion.completed);
assertEquals(GuestTests.CaseState.passed,result.verdict());assertFalse(result.lossDetected());
assertEquals(true,result.context().get("historicalOverflow"));
}
@Test void producerDropBeforeNativeSequenceAllocationCannotTurnIntoAPass() {
var records=complete(true);var t=transcript(List.of("a","b"));
var dropped=new ArrayList<GuestTests.LogEvent>();
for(int index=0;index<records.size();index++) {
if(index==2)continue;
var old=records.get(index);
dropped.add(new GuestTests.LogEvent(BigInteger.valueOf(dropped.size()),old.frame(),old.timestampMs(),old.level(),old.source(),old.appId(),old.tag(),old.message()));
}
t.accept(page(dropped,6));assertTrue(t.failedInfrastructure());
assertEquals(GuestTests.CaseState.infrastructure_failure,result(t,GuestTests.Completion.completed).verdict());
}
@Test void conflictingEventIdsAndChangedNativeSequencesDoNotReapplyResults() {
var t=transcript(List.of("a","b"));var records=complete(true);t.accept(page(records.subList(0,3),7));
var conflict=nativeEvent(3,event(GuestTests.EventKind.assertion,"a","a-assert",Map.of("name","health","message","conflict","passed",false)));
t.accept(page(List.of(conflict),7));assertTrue(t.failedInfrastructure());assertEquals(1,result(t,GuestTests.Completion.infrastructure_failure).cases().getFirst().assertions().size());
var changed=transcript(List.of("a","b"));changed.accept(page(records.subList(0,1),7));
changed.accept(page(List.of(nativeEvent(0,event(GuestTests.EventKind.suite_start,null,"different",Map.of()))),7));assertTrue(changed.failedInfrastructure());
}
@Test void emptyUnassertedMissingAndOutOfOrderCasesNeverPass() {
var empty=transcript(List.of());empty.accept(page(List.of(nativeEvent(0,event(GuestTests.EventKind.suite_start,null,"s",Map.of())),nativeEvent(1,event(GuestTests.EventKind.suite_end,null,"e",Map.of()))),1));
assertEquals(GuestTests.CaseState.infrastructure_failure,result(empty,GuestTests.Completion.completed).verdict());
var missing=transcript(List.of("a","b"));var records=complete(true);missing.accept(page(records.subList(0,4),7));
missing.accept(page(List.of(nativeEvent(4,event(GuestTests.EventKind.suite_end,null,"end-early",Map.of()))),7));
var r=result(missing,GuestTests.Completion.completed);assertEquals(GuestTests.CaseState.infrastructure_failure,r.verdict());assertEquals(GuestTests.CaseState.not_run,r.cases().get(1).state());
var none=transcript(List.of("a"));none.accept(page(List.of(nativeEvent(0,event(GuestTests.EventKind.suite_start,null,"s2",Map.of())),nativeEvent(1,event(GuestTests.EventKind.case_start,"a","as",Map.of())),nativeEvent(2,event(GuestTests.EventKind.case_end,"a","ae",Map.of())),nativeEvent(3,event(GuestTests.EventKind.suite_end,null,"e2",Map.of()))),3));
assertTrue(none.failedInfrastructure());
var order=transcript(List.of("a"));order.accept(page(List.of(nativeEvent(0,event(GuestTests.EventKind.case_start,"a","as2",Map.of()))),0));assertTrue(order.failedInfrastructure());
}
@Test void ordinaryTextAndWrongSourcesAreIgnoredButMalformedTaggedGuestEventsAreRetainedAsFailure() {
var t=transcript(List.of("a"));var ordinary=new GuestTests.LogEvent(BigInteger.ZERO,BigInteger.ZERO,BigInteger.ZERO,"info","pos",null,41000,"not JSON");
t.accept(page(List.of(ordinary),0));assertFalse(t.failedInfrastructure());
var malformed=new GuestTests.LogEvent(BigInteger.ONE,BigInteger.ZERO,BigInteger.ZERO,"info","app",123L,41000,"not JSON");
t.accept(page(List.of(malformed),1));var r=result(t,GuestTests.Completion.infrastructure_failure);
assertEquals(1,r.transcript().size());assertNull(r.transcript().getFirst().event());assertEquals("not JSON",r.transcript().getFirst().nativeEvent().message());assertTrue(t.failedInfrastructure());
}
@Test void nativeCauseSurvivesIncompleteSuiteAndTimeoutCancellationStaySeparate() {
var t=transcript(List.of("a"));var nativeFailure=new AutomationFailure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE,"Guest fault",Map.of("code","guest_fault","pc",7));
var fault=t.result("run","build","op","request",GuestTests.Completion.infrastructure_failure,nativeFailure);
assertEquals(nativeFailure.runtimeError(),fault.failure().runtimeError());assertEquals("Guest fault",fault.failure().message());assertEquals(GuestTests.CaseState.infrastructure_failure,fault.verdict());
for(var completion:List.of(GuestTests.Completion.cancelled,GuestTests.Completion.timed_out)) {
var r=result(transcript(List.of("a")),completion);assertEquals(completion,r.completion());assertEquals(GuestTests.CaseState.not_run,r.cases().getFirst().state());assertNotEquals(GuestTests.CaseState.passed,r.verdict());
}
}
@Test void checkpointsRequireProjectAdmissionAndTranscriptBudgetsFailExplicitly() {
var t=transcript(List.of("a"));t.accept(page(List.of(nativeEvent(0,event(GuestTests.EventKind.suite_start,null,"s",Map.of())),nativeEvent(1,event(GuestTests.EventKind.case_start,"a","a",Map.of())),nativeEvent(2,event(GuestTests.EventKind.checkpoint,"a","c",Map.of("artifactId","unknown")))),2));
assertTrue(t.failedInfrastructure());
var limited=new GuestTranscript(target(List.of("a")),123,messages::get,1);limited.accept(page(complete(true).subList(0,1),7));
var r=result(limited,GuestTests.Completion.infrastructure_failure);assertTrue(r.lossDetected());assertTrue(r.failure().message().contains("limit"));
var empty=transcript(List.of("a"));empty.accept(new GuestTests.LogPage("session",BigInteger.ZERO,List.of(),BigInteger.ZERO,BigInteger.TEN,null,false,false));
assertTrue(empty.failedInfrastructure());assertTrue(result(empty,GuestTests.Completion.infrastructure_failure).lossDetected());
}
}
@@ -0,0 +1,30 @@
package p.studio.core.shipper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import p.studio.api.diagnostic.DiagnosticValues;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class DiagnosticViewsTest {
@Test void wholeIssueCountLimitPreservesOriginalCountsAndEvaluation() {
var input = new ArrayList<Map<String, Object>>();
for (int at = 0; at < 1030; at++) input.add(DiagnosticValues.issue("FAIL", "ERROR", "failure " + at));
var bounded = DiagnosticViews.view(input, "build", "failed", List.of(), "partial");
assertEquals(1024, bounded.issues().size());
assertEquals(1030L, ((Map<?, ?>)bounded.summary().get("counts")).get("errors"));
assertEquals("failed", bounded.summary().get("validationStatus"));
assertTrue(bounded.summary().toString().contains("LIMIT_EXCEEDED"));
assertThrows(UnsupportedOperationException.class, () -> bounded.issues().getFirst().clear());
}
@Test void utf8BudgetNeverTruncatesOrSkipsAnOversizedIssue() throws Exception {
var first = DiagnosticValues.issue("FIRST", "ERROR", "small");
var large = DiagnosticValues.issue("LARGE", "ERROR", "😀".repeat(300_000));
var last = DiagnosticValues.issue("LAST", "WARNING", "small");
var bounded = DiagnosticViews.view(List.of(first, large, last), "build", "rejected", List.of(), "partial");
assertEquals(List.of(first), bounded.issues());
assertTrue(new ObjectMapper().writeValueAsBytes(bounded.issues()).length <= 1024 * 1024);
assertEquals(2L, ((Map<?, ?>)bounded.summary().get("counts")).get("errors"));
assertEquals(1L, ((Map<?, ?>)bounded.summary().get("counts")).get("warnings"));
}
}
@@ -6,7 +6,7 @@ import p.studio.api.project.*;
import p.studio.api.operation.*;
import p.studio.api.shipper.*;
import p.studio.api.diagnostic.StudioDiagnostic;
import p.studio.utilities.json.*;
import p.studio.api.diagnostic.DiagnosticValues;
import p.packer.*;
import p.packer.messages.*;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -32,7 +32,7 @@ class StructuredShipperTest {
ProjectReference project() { return new ProjectReference("case", "1", "pbs", 1, ProjectTarget.Game, root); }
@Test void failedPackerWithEmptyAssetsCannotApproveAndKeepsCompilerWarning() throws Exception {
Files.writeString(root.resolve("prometeu.json"), "{\"app_id\":1}");
var warning = StudioDiagnostic.fromMap(DiagnosticPayloads.issue("ORIGINAL_WARNING", "WARNING", "warning"));
var warning = StudioDiagnostic.fromMap(DiagnosticValues.issue("ORIGINAL_WARNING", "WARNING", "warning"));
var service = shipper((project, sink) -> new CompilerBuildGateway.CompilerBuildOutput(null, List.of(), List.of(warning), Map.of("analysisId", "original")), false);
var result = service.prepareNow(project(), ignored -> {});
assertEquals(ShipperPreparationStatus.FAILED, result.status());
@@ -59,7 +59,7 @@ class StructuredShipperTest {
assertEquals("not-recorded", facts.get("bankAvailability")); assertFalse(facts.containsKey("bankId"));
}
@Test void neutralCompilerFailureKeepsPayloadAndDoesNotRunPacker() {
var value = new LinkedHashMap<>(DiagnosticPayloads.issue("PBC_INVALID_IR", "ERROR", "invalid RET"));
var value = new LinkedHashMap<>(DiagnosticValues.issue("PBC_INVALID_IR", "ERROR", "invalid RET"));
value.put("checkId", "compiler.verify.ret-stack-height"); value.put("failureKind", "program-rejection");
value.put("facts", Map.of("expectedHeight", 0, "actualHeight", 1));
var issue = StudioDiagnostic.fromMap(value);