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
@@ -3,6 +3,16 @@ package p.packer;
import p.packer.messages.*;
public interface PackerWorkspaceService {
/** Shared readers/writers and recoverable publication use one reentrant project lane. */
default <T> T withProject(PackerProjectContext project, java.util.concurrent.Callable<T> work) {
try{return work.call();}catch(RuntimeException failure){throw failure;}catch(Exception failure){throw new IllegalStateException(failure);}
}
default void publishWorkspaceFiles(PublishWorkspaceFilesRequest request) {
throw new UnsupportedOperationException("Recoverable publication is not configured");
}
default p.packer.dtos.PackerPackPlanDTO planPackWorkspace(PackerProjectContext project) {
throw new UnsupportedOperationException("Final pack plan is not configured");
}
default p.packer.dtos.PackerGlyphBankLayoutDTO getGlyphBankLayout(GetGlyphBankLayoutRequest request) {
throw new UnsupportedOperationException("Glyph bank layout resolver is not configured");
}
@@ -11,9 +11,14 @@ public record PackerDiagnosticDTO(
PackerDiagnosticCategory category,
String message,
Path evidencePath,
boolean blocking) {
boolean blocking, String code, java.util.Map<String,Object> context) {
public PackerDiagnosticDTO(PackerDiagnosticSeverity severity,PackerDiagnosticCategory category,String message,Path evidencePath,boolean blocking) {
this(severity,category,message,evidencePath,blocking,null,java.util.Map.of());
}
public PackerDiagnosticDTO {
context=context==null?java.util.Map.of():java.util.Collections.unmodifiableMap(new java.util.LinkedHashMap<>(context));
Objects.requireNonNull(severity, "severity");
Objects.requireNonNull(category, "category");
message = Objects.requireNonNull(message, "message").trim();
@@ -0,0 +1,16 @@
package p.packer.dtos;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
/** Preload entries come from the very same emitter used to build assets.pa. */
public record PackerPackPlanDTO(List<Bank> banks,List<Preload> preload,List<PackerDiagnosticDTO> diagnostics) {
public PackerPackPlanDTO {banks=List.copyOf(banks);preload=List.copyOf(preload);diagnostics=List.copyOf(diagnostics);}
public record Bank(Integer assetId,String reference,Path root,String bankType,boolean buildSelected,boolean included,long contentCount,
Map<String,String> metadata,List<Integer> coldDependencies) {
public Bank(Integer assetId,String reference,Path root,String bankType,boolean included,long contentCount,Map<String,String> metadata,List<Integer> coldDependencies){this(assetId,reference,root,bankType,included,included,contentCount,metadata,coldDependencies);}
public Bank{metadata=Map.copyOf(metadata);coldDependencies=List.copyOf(coldDependencies);}
}
public record Preload(int assetId,int slot,String bankType){}
}
@@ -4,7 +4,9 @@ import java.util.Objects;
public record GetAssetDetailsRequest(
PackerProjectContext project,
AssetReference assetReference) {
AssetReference assetReference,boolean initializeIdentity) {
public GetAssetDetailsRequest(PackerProjectContext project,AssetReference reference){this(project,reference,true);}
public GetAssetDetailsRequest {
Objects.requireNonNull(project, "project");
@@ -4,12 +4,14 @@ import java.util.Objects;
public record ListAssetsRequest(
PackerProjectContext project,
boolean deepSync) {
boolean deepSync, boolean refreshReadOnly) {
public ListAssetsRequest {
Objects.requireNonNull(project, "project");
}
public ListAssetsRequest(PackerProjectContext project, boolean deepSync) { this(project,deepSync,false); }
public ListAssetsRequest(PackerProjectContext project) {
this(project, false);
}
@@ -0,0 +1,11 @@
package p.packer.messages;
import java.util.Map;
/** Consumer-qualified capacity facts, never inferred from a version family. */
public record PackCapacityContract(String profileId,String executableSha256,Map<String,Integer> limits) {
public PackCapacityContract {
if(profileId==null||profileId.isBlank()||executableSha256==null||!executableSha256.matches("[0-9a-f]{64}"))throw new IllegalArgumentException("Qualified profile identity required");
limits=Map.copyOf(limits);if(limits.isEmpty()||limits.values().stream().anyMatch(n->n<1))throw new IllegalArgumentException("Positive qualified capacities required");
}
}
@@ -3,9 +3,15 @@ package p.packer.messages;
import java.util.Objects;
public record PackWorkspaceRequest(
PackerProjectContext project) {
PackerProjectContext project,
java.nio.file.Path outputDirectory, PackCapacityContract capacity) {
public PackWorkspaceRequest(PackerProjectContext project) { this(project, null,null); }
public PackWorkspaceRequest(PackerProjectContext project,java.nio.file.Path outputDirectory){this(project,outputDirectory,null);}
public PackWorkspaceRequest {
Objects.requireNonNull(project, "project");
outputDirectory = outputDirectory == null ? null : outputDirectory.toAbsolutePath().normalize();
if (outputDirectory != null && !outputDirectory.startsWith(project.rootPath()))
throw new IllegalArgumentException("Pack output must be inside the project");
}
}
@@ -7,10 +7,13 @@ import java.util.Objects;
public record PackWorkspaceResult(
PackerOperationStatus status,
String summary,
PackerPackExecutionSummaryDTO result) {
PackerPackExecutionSummaryDTO result, java.util.List<p.packer.dtos.PackerDiagnosticDTO> diagnostics) {
public PackWorkspaceResult(PackerOperationStatus status,String summary,PackerPackExecutionSummaryDTO result){this(status,summary,result,java.util.List.of());}
public PackWorkspaceResult {
Objects.requireNonNull(status, "status");
diagnostics=java.util.List.copyOf(diagnostics);
summary = Objects.requireNonNull(summary, "summary").trim();
Objects.requireNonNull(result, "result");
if (summary.isBlank()) {
@@ -0,0 +1,23 @@
package p.packer.messages;
import java.util.List;
import java.util.Objects;
/** Validated candidate files published under the same project lane as asset readers. */
public record PublishWorkspaceFilesRequest(PackerProjectContext project, String transactionId, List<Edit> edits) {
public PublishWorkspaceFilesRequest {
Objects.requireNonNull(project); java.util.UUID.fromString(transactionId); edits=List.copyOf(edits);
if(edits.stream().map(Edit::path).distinct().count()!=edits.size())throw new IllegalArgumentException("Duplicate publication path");
}
public record Edit(String path, String beforeSha256, byte[] content) {
public Edit {
var relative=java.nio.file.Path.of(path);
if(relative.isAbsolute()||relative.normalize().startsWith("..")||!relative.normalize().toString().equals(path)||path.equals(".")||path.contains("\\"))
throw new IllegalArgumentException("Expected project-relative publication path");
if(beforeSha256!=null&&!beforeSha256.matches("[0-9a-f]{64}"))throw new IllegalArgumentException("Expected previous SHA-256");
if(path.startsWith(".studio/asset-transactions/"))throw new IllegalArgumentException("Cannot publish a journal as an asset");
content=content==null?null:content.clone();
}
@Override public byte[] content(){return content==null?null:content.clone();}
}
}
@@ -11,9 +11,14 @@ public record PackerDiagnostic(
PackerDiagnosticCategory category,
String message,
Path evidencePath,
boolean blocking) {
boolean blocking, String code, java.util.Map<String,Object> context) {
public PackerDiagnostic(PackerDiagnosticSeverity severity,PackerDiagnosticCategory category,String message,Path evidencePath,boolean blocking) {
this(severity,category,message,evidencePath,blocking,null,java.util.Map.of());
}
public PackerDiagnostic {
context=context==null?java.util.Map.of():java.util.Collections.unmodifiableMap(new java.util.LinkedHashMap<>(context));
Objects.requireNonNull(severity, "severity");
Objects.requireNonNull(category, "category");
message = Objects.requireNonNull(message, "message").trim();
@@ -120,7 +120,7 @@ public final class FileSystemPackerCacheRepository {
diagnostic.category,
diagnostic.message,
diagnostic.evidencePath == null || diagnostic.evidencePath.isBlank() ? null : Path.of(diagnostic.evidencePath),
diagnostic.blocking));
diagnostic.blocking,diagnostic.code,diagnostic.context));
}
return List.copyOf(loaded);
}
@@ -135,7 +135,7 @@ public final class FileSystemPackerCacheRepository {
diagnostic.category(),
diagnostic.message(),
diagnostic.evidencePath() == null ? null : diagnostic.evidencePath().toString(),
diagnostic.blocking()))
diagnostic.blocking(),diagnostic.code(),diagnostic.context()))
.toList();
}
@@ -210,6 +210,7 @@ public final class FileSystemPackerCacheRepository {
@JsonProperty("category") PackerDiagnosticCategory category,
@JsonProperty("message") String message,
@JsonProperty("evidence_path") String evidencePath,
@JsonProperty("blocking") boolean blocking) {
@JsonProperty("blocking") boolean blocking,
@JsonProperty("code") String code, @JsonProperty("context") Map<String,Object> context) {
}
}
@@ -146,7 +146,7 @@ public class PackerAssetWalker {
PackerDiagnosticCategory.STRUCTURAL,
"Accepted Scene Bank request artifact is missing: " + SCENE_PACK_REQUEST_FILE,
requestPath,
true));
true, "SCENE_REQUEST_MISSING", java.util.Map.of("assetRoot", assetRoot.toString(), "family", "SCENE_BANK")));
return new PackerWalkResult(List.of(), diagnostics);
}
try {
@@ -251,26 +251,16 @@ public class PackerAssetWalker {
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
final List<PackerAssetArtifactSelection> artifacts = declaration.artifacts();
final Set<Integer> seenIndices = new java.util.HashSet<>();
for (int expectedIndex = 0; expectedIndex < artifacts.size(); expectedIndex += 1) {
final PackerAssetArtifactSelection artifact = artifacts.get(expectedIndex);
if (!seenIndices.add(artifact.index())) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Tile bank artifacts contain duplicate index " + artifact.index() + ".",
assetRoot,
true));
}
if (artifact.index() != expectedIndex) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Tile bank artifacts must form a contiguous index range starting at 0.",
assetRoot,
true));
break;
}
}
final List<Integer> duplicateIndices=new ArrayList<>();
for(var artifact:artifacts)if(!seenIndices.add(artifact.index()))duplicateIndices.add(artifact.index());
final List<Integer> missingIndices=new ArrayList<>();
for(int index=0;index<artifacts.size();index++)if(!seenIndices.contains(index))missingIndices.add(index);
var indexFacts=new java.util.LinkedHashMap<String,Object>();indexFacts.put("assetRoot",assetRoot.toString());indexFacts.put("family","GLYPH_BANK");
indexFacts.put("missingIndices",List.copyOf(missingIndices));indexFacts.put("duplicateIndices",List.copyOf(duplicateIndices));
if(!duplicateIndices.isEmpty())diagnostics.add(new PackerDiagnostic(PackerDiagnosticSeverity.ERROR,PackerDiagnosticCategory.STRUCTURAL,
"Tile bank artifacts contain duplicate index "+duplicateIndices.getFirst()+".",assetRoot,true,"GLYPH_INDICES_DUPLICATE",indexFacts));
if(!missingIndices.isEmpty())diagnostics.add(new PackerDiagnostic(PackerDiagnosticSeverity.ERROR,PackerDiagnosticCategory.STRUCTURAL,
"Tile bank artifacts must form a contiguous index range starting at 0.",assetRoot,true,"GLYPH_INDICES_NONCONTIGUOUS",indexFacts));
final int tileCapacity = (GLYPH_BANK_EMITTED_SHEET_SIZE / requirements.tileSize())
* (GLYPH_BANK_EMITTED_SHEET_SIZE / requirements.tileSize());
@@ -282,7 +272,7 @@ public class PackerAssetWalker {
+ requirements.tileSize() + "x" + requirements.tileSize()
+ ": capacity is " + tileCapacity + " tiles.",
assetRoot,
true));
true,"GLYPH_CONTENT_CAPACITY_EXCEEDED",java.util.Map.of("assetRoot",assetRoot.toString(),"family","GLYPH_BANK","used",artifacts.size(),"required",artifacts.size(),"limit",tileCapacity)));
}
final List<DeclaredPalette> declaredPalettes = declaredPalettes(declaration);
@@ -41,8 +41,8 @@ public final class PackerRuntimeLoader implements PackerRuntimeSnapshotLoader {
@Override
public PackerRuntimeSnapshot load(PackerProjectContext project, long generation) {
final var safeProject = Objects.requireNonNull(project, "project");
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(safeProject));
// Snapshot reads must not normalize or create published registry bytes.
// Explicit workspace/mutation operations own initialization.
final var registry = workspaceFoundation.loadRegistry(safeProject);
final PackerWorkspaceCacheState priorCacheState = loadPriorCacheState(safeProject);
final var registryByRoot = registry
@@ -77,8 +77,51 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
this.glyphMaterialSupport = new PackerGlyphMaterialSupport(mapper);
}
private final java.util.Map<Path,Long> publicationEpochs=new java.util.concurrent.ConcurrentHashMap<>();
private PackerRuntimeSnapshot cachedSnapshot(PackerProjectContext project) {
long epoch=PackerFilePublication.epoch(project.rootPath());Long previous=publicationEpochs.put(project.rootPath(),epoch);
return previous!=null&&previous!=epoch?runtimeRegistry.refresh(project).snapshot():runtimeRegistry.getOrLoad(project).snapshot();
}
@Override public <T>T withProject(PackerProjectContext project,java.util.concurrent.Callable<T> work) {
return writeCoordinator.execute(project,work);
}
@Override public void publishWorkspaceFiles(PublishWorkspaceFilesRequest request) {
withProject(request.project(),()->{PackerFilePublication.publish(request);runtimeRegistry.refresh(request.project());return null;});
}
@Override public p.packer.dtos.PackerPackPlanDTO planPackWorkspace(PackerProjectContext project) {
return withProject(project,()->{
var snapshot=createFrozenPackingSnapshot(runtimeRegistry.refresh(project).snapshot());
var banks=new ArrayList<p.packer.dtos.PackerPackPlanDTO.Bank>();
var diagnostics=new ArrayList<PackerDiagnosticDTO>();
for(var asset:snapshot.assets()) {
var declaration=asset.parsedDeclaration().declaration();if(declaration==null){
if(asset.registryEntry().map(PackerRegistryEntry::includedInBuild).orElse(false))diagnostics.addAll(PackerReadMessageMapper.toDiagnosticDTOs(asset.parsedDeclaration().diagnostics()));
else diagnostics.add(new PackerDiagnosticDTO(PackerDiagnosticSeverity.WARNING,PackerDiagnosticCategory.STRUCTURAL,"Excluded asset has no valid bank type; typed residency counts omit it.",asset.manifestPath(),false,"ASSET_BUDGET_TYPE_UNAVAILABLE",Map.of("assetRoot",asset.assetRoot().toString(),"included",false)));
continue;
}
var dependencies=new java.util.TreeSet<Integer>();
for(var file:asset.walkProjection().buildCandidateFiles())if(file.metadata().get("scenePackRequest") instanceof ScenePackRequest scene)
scene.layers().stream().filter(ScenePackLayerRequest::active).map(ScenePackLayerRequest::glyphAssetId).forEach(dependencies::add);
var entry=asset.registryEntry();String type=switch(declaration.assetFamily()){case GLYPH_BANK->"GLYPH";case SCENE_BANK->"SCENE";case SOUND_BANK->"SOUNDS";default->"UNKNOWN";};
String reference=entry.map(e->AssetReference.forAssetId(e.assetId()).value()).orElseGet(()->AssetReference.forRelativeAssetRoot(PackerWorkspacePaths.relativeAssetRoot(project,asset.assetRoot())).value());
banks.add(new p.packer.dtos.PackerPackPlanDTO.Bank(entry.map(PackerRegistryEntry::assetId).orElse(null),reference,asset.assetRoot(),type,entry.map(PackerRegistryEntry::includedInBuild).orElse(false),isEffectivelyIncludedInPackSet(asset),declaration.artifacts().size(),declaration.outputMetadata(),List.copyOf(dependencies)));
if(isEffectivelyIncludedInPackSet(asset))diagnostics.addAll(toPackValidationAssetDTO(asset).diagnostics());
}
if(diagnostics.stream().anyMatch(PackerDiagnosticDTO::blocking))return new p.packer.dtos.PackerPackPlanDTO(banks,List.of(),diagnostics);
var packed=buildPackedWorkspaceArtifacts(snapshot);var types=new HashMap<Integer,String>();
packed.assetTable().forEach(a->types.put(((Number)a.get("asset_id")).intValue(),a.get("bank_type").toString()));
var preload=packed.preload().stream().map(p->new p.packer.dtos.PackerPackPlanDTO.Preload(((Number)p.get("asset_id")).intValue(),((Number)p.get("slot")).intValue(),types.get(((Number)p.get("asset_id")).intValue()))).toList();
return new p.packer.dtos.PackerPackPlanDTO(banks,preload,diagnostics);
});
}
@Override
public InitWorkspaceResult initWorkspace(InitWorkspaceRequest request) {
return withProject(request.project(),()->initWorkspaceInLane(request));
}
private InitWorkspaceResult initWorkspaceInLane(InitWorkspaceRequest request) {
final InitWorkspaceResult result = workspaceFoundation.initWorkspace(request);
runtimeRegistry.refresh(Objects.requireNonNull(request, "request").project());
return result;
@@ -86,10 +129,14 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
@Override
public ListAssetsResult listAssets(ListAssetsRequest request) {
return withProject(request.project(),()->listAssetsInLane(request));
}
private ListAssetsResult listAssetsInLane(ListAssetsRequest request) {
final var project = Objects.requireNonNull(request, "request").project();
final var snapshot = request.deepSync()
? writeCoordinator.execute(project, () -> refreshAfterArtifactIdentityBackfill(project))
: runtimeRegistry.getOrLoad(project).snapshot();
: request.refreshReadOnly()?runtimeRegistry.refresh(project).snapshot():cachedSnapshot(project);
final var events = new PackerOperationEventEmitter(project, eventSink);
final var registry = snapshot.registry();
final Map<Path, PackerRegistryEntry> registryByRoot = new HashMap<>();
@@ -204,20 +251,30 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
public GetAssetDetailsResult getAssetDetails(GetAssetDetailsRequest request) {
final GetAssetDetailsRequest safeRequest = Objects.requireNonNull(request, "request");
return writeCoordinator.execute(safeRequest.project(), () -> {
backfillLoadedArtifactIdentity(safeRequest.project());
if(safeRequest.initializeIdentity())backfillLoadedArtifactIdentity(safeRequest.project());
else runtimeRegistry.refresh(safeRequest.project());
return detailsService.getAssetDetails(safeRequest);
});
}
@Override
public GetAssetActionsResult getAssetActions(GetAssetActionsRequest request) {
return withProject(request.project(),()->getAssetActionsInLane(request));
}
private GetAssetActionsResult getAssetActionsInLane(GetAssetActionsRequest request) {
cachedSnapshot(request.project());
return actionReadService.getAssetActions(request);
}
@Override
public PackWorkspaceSummaryResult getPackWorkspaceSummary(PackWorkspaceSummaryRequest request) {
return withProject(request.project(),()->getPackWorkspaceSummaryInLane(request));
}
private PackWorkspaceSummaryResult getPackWorkspaceSummaryInLane(PackWorkspaceSummaryRequest request) {
final PackerProjectContext project = Objects.requireNonNull(request, "request").project();
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final List<PackerPackSummaryAssetDTO> assets = snapshot.assets().stream()
.filter(this::isEffectivelyIncludedInPackSet)
.sorted(Comparator.comparingInt(runtimeAsset -> runtimeAsset.registryEntry().get().assetId()))
@@ -396,7 +453,8 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
new PackerPackExecutionSummaryDTO("assets.pa", 0, System.currentTimeMillis() - startedAt, List.of()));
}
final Path buildRoot = project.rootPath().resolve("build").toAbsolutePath().normalize();
final Path buildRoot = request.outputDirectory() == null ? project.rootPath().resolve("build").toAbsolutePath().normalize()
: request.outputDirectory();
final String operationId = randomOperationId();
final Path stagingRoot = buildRoot.resolve(".staging").resolve(operationId);
final Path stagedAssetsPath = stagingRoot.resolve("assets.pa");
@@ -404,8 +462,17 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
final Path stagedPreloadPath = stagingRoot.resolve("preload.json");
final Path stagedAssetTableMetadataPath = stagingRoot.resolve("asset_table_metadata.json");
try {
var existingParent = buildRoot;
while (existingParent != null && !Files.exists(existingParent, java.nio.file.LinkOption.NOFOLLOW_LINKS)) existingParent = existingParent.getParent();
if (existingParent == null || !existingParent.toRealPath().startsWith(project.rootPath().toRealPath())) throw new IOException("Pack output escapes the project");
Files.createDirectories(buildRoot);
if (!buildRoot.toRealPath().startsWith(project.rootPath().toRealPath())) throw new IOException("Pack output escapes the project");
Files.createDirectories(stagingRoot);
final PackedWorkspaceArtifacts packedWorkspace = buildPackedWorkspaceArtifacts(frozenSnapshot);
var paths=new HashMap<Integer,String>();frozenSnapshot.assets().forEach(a->a.registryEntry().ifPresent(e->paths.put(e.assetId(),a.assetRoot().toString())));
var capacityDiagnostics=PackerCapacityValidation.validate(request.capacity(),packedWorkspace.assetTable(),packedWorkspace.preload(),paths);
if(!capacityDiagnostics.isEmpty())return new PackWorkspaceResult(PackerOperationStatus.PARTIAL,"Qualified preload capacity rejected.",
new PackerPackExecutionSummaryDTO("assets.pa",0,System.currentTimeMillis()-startedAt,List.of()),capacityDiagnostics);
writeJson(stagedAssetTablePath, packedWorkspace.assetTable());
writeJson(stagedPreloadPath, packedWorkspace.preload());
writeJson(stagedAssetTableMetadataPath, packedWorkspace.assetTableMetadata());
@@ -447,7 +514,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
PackerOperationEventEmitter events) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final PackerRegisterAssetEvaluation evaluation = actionReadService.evaluateRegister(snapshot, project, request.assetReference());
final Path assetRoot = evaluation.resolved().assetRoot();
final Path manifestPath = assetRoot.resolve("asset.json");
@@ -497,7 +564,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
PackerOperationEventEmitter events) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final PackerDeleteAssetEvaluation evaluation = actionReadService.evaluateDelete(snapshot, project, request.assetReference());
final Path assetRoot = evaluation.resolved().assetRoot();
final Path manifestPath = assetRoot.resolve("asset.json");
@@ -542,7 +609,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
PackerOperationEventEmitter events) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final PackerDeleteAssetEvaluation evaluation = actionReadService.evaluateDelete(snapshot, project, request.assetReference());
final Path assetRoot = evaluation.resolved().assetRoot();
final String relativeAssetRoot = relativeAssetRoot(project, assetRoot);
@@ -599,7 +666,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
PackerOperationEventEmitter events) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final PackerMoveAssetEvaluation evaluation = actionReadService.evaluateMove(snapshot, project, request.assetReference(), request.targetRoot());
if (!evaluation.canMove()) {
return moveFailureResult(
@@ -963,7 +1030,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
}
private PackerRuntimeSnapshot backfillLoadedArtifactIdentity(PackerProjectContext project) {
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
return backfillArtifactIdentity(snapshot)
? runtimeRegistry.refresh(project).snapshot()
: snapshot;
@@ -1587,7 +1654,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
private UpdateAssetContractResponse updateAssetContractInWriteLane(UpdateAssetContractRequest request) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final PackerDeleteAssetEvaluation evaluation = actionReadService.evaluateDelete(snapshot, project, request.assetReference());
if (!evaluation.canDelete()) {
return new UpdateAssetContractResponse(
@@ -1648,7 +1715,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
private ApplyBankCompositionResponse applyBankCompositionInWriteLane(ApplyBankCompositionRequest request) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final PackerDeleteAssetEvaluation evaluation = actionReadService.evaluateDelete(snapshot, project, request.assetReference());
if (!evaluation.canDelete()) {
return new ApplyBankCompositionResponse(
@@ -1694,7 +1761,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
private ApplyPaletteOverhaulingResponse applyPaletteOverhaulingInWriteLane(ApplyPaletteOverhaulingRequest request) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
final PackerRuntimeSnapshot snapshot = cachedSnapshot(project);
final PackerDeleteAssetEvaluation evaluation = actionReadService.evaluateDelete(snapshot, project, request.assetReference());
if (!evaluation.canDelete()) {
return new ApplyPaletteOverhaulingResponse(
@@ -1802,7 +1869,10 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
}
final ExistingArtifactEntry existing = existingArtifactsByFile.get(file);
final var artifactNode = artifactsNode.addObject();
artifactNode.put("artifact_id", existing != null && existing.artifactId() != null ? existing.artifactId() : nextArtifactId());
// Bank-local allocation is repeatable across amended previews. Published identities
// always win; retrying an uncommitted composition cannot invent another target.
artifactNode.put("artifact_id", existing != null && existing.artifactId() != null ? existing.artifactId()
: "art-" + UUID.nameUUIDFromBytes(file.getBytes(java.nio.charset.StandardCharsets.UTF_8)).toString().replace("-", ""));
artifactNode.put("file", file);
artifactNode.put("index", index);
final String fingerprint = Optional.ofNullable(walkFilesByPath.get(file))
@@ -0,0 +1,28 @@
package p.packer.services;
import p.packer.dtos.PackerDiagnosticDTO;
import p.packer.messages.PackCapacityContract;
import p.packer.messages.diagnostics.*;
import java.util.*;
final class PackerCapacityValidation {
static List<PackerDiagnosticDTO> validate(PackCapacityContract profile,List<Map<String,Object>> table,
List<Map<String,Object>> preload,Map<Integer,String> paths) {
if(profile==null)return List.of();
var types=new HashMap<Integer,String>();table.forEach(a->types.put(((Number)a.get("asset_id")).intValue(),a.get("bank_type").toString()));
var byType=new TreeMap<String,List<Map<String,Object>>>();preload.forEach(p->byType.computeIfAbsent(types.get(((Number)p.get("asset_id")).intValue()),ignored->new ArrayList<>()).add(p));
var diagnostics=new ArrayList<PackerDiagnosticDTO>();
byType.forEach((type,entries)->{
Integer limit=profile.limits().get(type);int required=entries.stream().mapToInt(p->((Number)p.get("slot")).intValue()+1).max().orElse(0);
if(limit==null||required>limit) {
var context=new LinkedHashMap<String,Object>();context.put("profileId",profile.profileId());context.put("executableSha256",profile.executableSha256());context.put("bankType",type);
context.put("used",entries.size());context.put("required",required);if(limit!=null)context.put("limit",limit);
context.put("slots",entries.stream().map(p->p.get("slot")).toList());
var ids=entries.stream().map(p->((Number)p.get("asset_id")).intValue()).toList();context.put("assetIds",ids);context.put("paths",ids.stream().map(paths::get).filter(Objects::nonNull).toList());
diagnostics.add(new PackerDiagnosticDTO(PackerDiagnosticSeverity.ERROR,PackerDiagnosticCategory.STRUCTURAL,
limit==null?"No qualified capacity for preload bank type "+type:"Preload requires "+required+" "+type+" slots; qualified limit is "+limit,
null,true,limit==null?"PRELOAD_CAPACITY_UNKNOWN":"PRELOAD_CAPACITY_EXCEEDED",context));
}
});return diagnostics;
}
}
@@ -0,0 +1,89 @@
package p.packer.services;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.packer.messages.PublishWorkspaceFilesRequest;
import java.nio.file.*;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.Callable;
/** Roll forward only after the durable commit marker. Every shared reader recovers first. */
final class PackerFilePublication {
private static final ObjectMapper JSON=new ObjectMapper();
private static final java.util.concurrent.ConcurrentMap<Path,java.util.concurrent.locks.ReentrantLock> LOCKS=new java.util.concurrent.ConcurrentHashMap<>();
private static final java.util.concurrent.ConcurrentMap<Path,Long> EPOCHS=new java.util.concurrent.ConcurrentHashMap<>();
static long epoch(Path root) {try{return EPOCHS.getOrDefault(root.toRealPath(),0L);}catch(Exception e){return 0L;}}
record File(String path,String before,String after,String object) {}
record Journal(List<File> files) {}
static <T>T guarded(Path project,Callable<T> work)throws Exception {
Files.createDirectories(project);Path root=project.toRealPath();
Path home=contained(root,".studio/asset-transactions");Files.createDirectories(home);
var monitor=LOCKS.computeIfAbsent(root,ignored->new java.util.concurrent.locks.ReentrantLock());monitor.lockInterruptibly();
try { try(var channel=FileChannel.open(contained(root,".studio/asset-transactions/.lock"),StandardOpenOption.CREATE,StandardOpenOption.WRITE);
var lock=channel.lock()) {
try(var dirs=Files.list(home)) {
for(Path dir:dirs.filter(Files::isDirectory).sorted().toList()) {
if(Files.isSymbolicLink(dir))throw new IllegalStateException("Journal cannot be a symlink");
if(Files.exists(dir.resolve("commit"))&&!Files.exists(dir.resolve("done"))){flush(dir.resolve("recovered"),new byte[]{1});apply(root,dir);}
}
}
return work.call();
} } finally {monitor.unlock();}
}
static void publish(PublishWorkspaceFilesRequest request)throws Exception {
Path root=request.project().rootPath().toRealPath();
Path dir=contained(root,".studio/asset-transactions/"+request.transactionId());
if(Files.exists(dir.resolve("commit"))) { if(!Files.exists(dir.resolve("done")))apply(root,dir);return; }
Files.createDirectories(dir);List<File> files=new ArrayList<>();int index=0;
for(var edit:request.edits()) {
Path destination=contained(root,edit.path());
String actual=Files.exists(destination)?hash(Files.readAllBytes(destination)):null;
if(!Objects.equals(actual,edit.beforeSha256()))throw new IllegalStateException("Publication input changed: "+edit.path());
byte[] content=edit.content();String object=content==null?null:"object-"+index++;
if(content!=null)flush(dir.resolve(object),content);
files.add(new File(edit.path(),actual,content==null?null:hash(content),object));
}
flush(dir.resolve("manifest.json"),JSON.writeValueAsBytes(new Journal(files)));
if(Thread.currentThread().isInterrupted())throw new java.util.concurrent.CancellationException("Cancelled before asset commit");
flush(dir.resolve("commit"),new byte[]{1});syncDirectory(dir);
// Publication is irrevocable here. Ignore interruption until the complete state is visible.
boolean interrupted=Thread.interrupted();
try{apply(root,dir);}finally{if(interrupted)Thread.currentThread().interrupt();}
}
private static void apply(Path root,Path dir)throws Exception {
var journal=JSON.readValue(dir.resolve("manifest.json").toFile(),Journal.class);
for(var file:journal.files()) {
Path destination=contained(root,file.path());
if(file.after()==null)Files.deleteIfExists(destination);
else {
byte[] bytes=Files.readAllBytes(dir.resolve(file.object()));
if(!hash(bytes).equals(file.after()))throw new IllegalStateException("Corrupt publication object: "+file.path());
Files.createDirectories(destination.getParent());
if(Files.exists(destination)&&hash(Files.readAllBytes(destination)).equals(file.after()))continue;
Path temporary=Files.createTempFile(destination.getParent(),".asset-publish-",".tmp");
try{flush(temporary,bytes);Files.move(temporary,destination,StandardCopyOption.ATOMIC_MOVE,StandardCopyOption.REPLACE_EXISTING);}
finally{Files.deleteIfExists(temporary);}
syncDirectory(destination.getParent());
}
}
flush(dir.resolve("done"),new byte[]{1});syncDirectory(dir);EPOCHS.merge(root,1L,Long::sum);
}
static Path contained(Path root,String relative)throws Exception {
Path candidate=root.resolve(relative).normalize();
if(!candidate.startsWith(root)||candidate.equals(root))throw new IllegalArgumentException("Path escapes project");
Path part=root;
for(Path element:root.relativize(candidate)) {
part=part.resolve(element);
if(Files.isSymbolicLink(part))throw new IllegalArgumentException("Symbolic link in publication path: "+part);
}
return candidate;
}
static String hash(byte[] bytes)throws Exception{return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));}
private static void flush(Path path,byte[] bytes)throws Exception {
try(var channel=FileChannel.open(path,StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING,StandardOpenOption.WRITE)) {
var buffer=java.nio.ByteBuffer.wrap(bytes);while(buffer.hasRemaining())channel.write(buffer);channel.force(true);
}
}
private static void syncDirectory(Path path)throws Exception {try(var channel=FileChannel.open(path,StandardOpenOption.READ)){channel.force(true);}}
}
@@ -11,26 +11,40 @@ import java.util.concurrent.atomic.AtomicInteger;
public final class PackerProjectWriteCoordinator implements Closeable {
private final ConcurrentMap<ProjectKey, ExecutorService> executors = new ConcurrentHashMap<>();
private final AtomicInteger nextThreadId = new AtomicInteger(1);
private final ThreadLocal<java.util.Set<Path>> active=ThreadLocal.withInitial(java.util.HashSet::new);
public <T> Future<T> submit(PackerProjectContext project, Callable<T> task) {
final PackerProjectContext safeProject = Objects.requireNonNull(project, "project");
final Callable<T> safeTask = Objects.requireNonNull(task, "task");
return executorFor(safeProject).submit(safeTask);
return executorFor(safeProject).submit(() -> guarded(safeProject,safeTask));
}
public <T> T execute(PackerProjectContext project, Callable<T> task) {
Path root=project.rootPath().toAbsolutePath().normalize();
if(active.get().contains(root)) {
try{return task.call();}catch(RuntimeException failure){throw failure;}catch(Exception failure){throw new IllegalStateException(failure);}
}
var worker=new java.util.concurrent.atomic.AtomicReference<Thread>();
var interrupted=new java.util.concurrent.atomic.AtomicBoolean();
Future<T> future=submit(project,()->{worker.set(Thread.currentThread());if(interrupted.get())Thread.currentThread().interrupt();return task.call();});
boolean restore=false;
try {
return submit(project, task).get();
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Write execution was interrupted for project: " + project.projectId(), exception);
while(true)try{return future.get();}
catch(InterruptedException interruption){restore=true;interrupted.set(true);Thread thread=worker.get();if(thread!=null)thread.interrupt();}
} catch (ExecutionException exception) {
final Throwable cause = exception.getCause();
if (cause instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException("Write execution failed for project: " + project.projectId(), cause);
}
} finally {if(restore)Thread.currentThread().interrupt();}
}
private <T>T guarded(PackerProjectContext project,Callable<T> work)throws Exception {
Path root=project.rootPath().toAbsolutePath().normalize();
if(active.get().contains(root))return work.call();
active.get().add(root);
try{return PackerFilePublication.guarded(root,work);}finally{active.get().remove(root);Thread.interrupted();}
}
@Override
@@ -210,7 +210,7 @@ public final class PackerReadMessageMapper {
diagnostic.category(),
diagnostic.message(),
diagnostic.evidencePath(),
diagnostic.blocking());
diagnostic.blocking(),diagnostic.code(),diagnostic.context());
}
private static PackerAssetActionAvailabilityDTO toAssetActionAvailabilityDTO(
@@ -39,7 +39,7 @@ public final class PackerWorkspaceFoundation {
Files.createDirectories(PackerWorkspacePaths.assetsRoot(project));
Files.createDirectories(PackerWorkspacePaths.registryDirectory(project));
final var registryState = registryRepository.load(project);
registryRepository.save(project, registryState);
if(!Files.exists(PackerWorkspacePaths.registryPath(project)))registryRepository.save(project, registryState);
return new InitWorkspaceResult(
PackerOperationStatus.SUCCESS,
"Workspace initialized for packer control.",
@@ -0,0 +1,52 @@
package p.packer.services;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import p.packer.messages.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
import static org.junit.jupiter.api.Assertions.*;
class PackerFilePublicationTest {
@TempDir Path root;
PackerProjectWriteCoordinator lane=new PackerProjectWriteCoordinator();
@AfterEach void close(){lane.close();}
PackerProjectContext project(){return new PackerProjectContext("publication",root);}
byte[] bytes(String value){return value.getBytes(java.nio.charset.StandardCharsets.UTF_8);}
@Test void committedCrashStateRecoversBeforeFirstReaderAndUncommittedStageDoesNotPublish()throws Exception {
Files.writeString(root.resolve("a"),"old");Files.writeString(root.resolve("b"),"old");
String id=UUID.randomUUID().toString();Path dir=Files.createDirectories(root.resolve(".studio/asset-transactions/"+id));
Files.write(dir.resolve("object-0"),bytes("new"));Files.write(dir.resolve("object-1"),bytes("new"));
var manifest=new PackerFilePublication.Journal(List.of(new PackerFilePublication.File("a",PackerFilePublication.hash(bytes("old")),PackerFilePublication.hash(bytes("new")),"object-0"),new PackerFilePublication.File("b",PackerFilePublication.hash(bytes("old")),PackerFilePublication.hash(bytes("new")),"object-1")));
new com.fasterxml.jackson.databind.ObjectMapper().writeValue(dir.resolve("manifest.json").toFile(),manifest);
assertEquals(List.of("old","old"),lane.execute(project(),()->List.of(Files.readString(root.resolve("a")),Files.readString(root.resolve("b")))));
Files.write(dir.resolve("commit"),new byte[]{1});Files.writeString(root.resolve("a"),"new"); // Simulated death halfway through a committed publication.
lane.close();lane=new PackerProjectWriteCoordinator();
assertEquals(List.of("new","new"),lane.execute(project(),()->List.of(Files.readString(root.resolve("a")),Files.readString(root.resolve("b")))));
assertTrue(Files.exists(dir.resolve("done")));assertTrue(Files.exists(dir.resolve("recovered")));
}
@Test void staleInputRejectsWholePublicationBeforeCommitAndValidPublicationFlushesAllFiles()throws Exception {
Files.writeString(root.resolve("a"),"old");Files.writeString(root.resolve("b"),"external");
var edits=List.of(new PublishWorkspaceFilesRequest.Edit("a",PackerFilePublication.hash(bytes("old")),bytes("new")),new PublishWorkspaceFilesRequest.Edit("b",PackerFilePublication.hash(bytes("old")),bytes("new")));
String failed=UUID.randomUUID().toString();
assertThrows(IllegalStateException.class,()->lane.execute(project(),()->{PackerFilePublication.publish(new PublishWorkspaceFilesRequest(project(),failed,edits));return null;}));
assertEquals("old",Files.readString(root.resolve("a")));assertEquals("external",Files.readString(root.resolve("b")));assertFalse(Files.exists(root.resolve(".studio/asset-transactions/"+failed+"/commit")));
Files.writeString(root.resolve("b"),"old");String applied=UUID.randomUUID().toString();
lane.execute(project(),()->{PackerFilePublication.publish(new PublishWorkspaceFilesRequest(project(),applied,edits));return null;});
assertEquals("new",Files.readString(root.resolve("a")));assertEquals("new",Files.readString(root.resolve("b")));assertTrue(Files.exists(root.resolve(".studio/asset-transactions/"+applied+"/done")));
}
@Test void sharedReadersCannotSeeAPublisherHalfwayThroughItsProjectLane()throws Exception {
Files.writeString(root.resolve("a"),"old");Files.writeString(root.resolve("b"),"old");
var halfway=new CountDownLatch(1);var finish=new CountDownLatch(1);
var publishing=lane.submit(project(),()->{Files.writeString(root.resolve("a"),"new");halfway.countDown();assertTrue(finish.await(5,TimeUnit.SECONDS));Files.writeString(root.resolve("b"),"new");return null;});
assertTrue(halfway.await(5,TimeUnit.SECONDS));var reading=lane.submit(project(),()->List.of(Files.readString(root.resolve("a")),Files.readString(root.resolve("b"))));
assertFalse(reading.isDone());finish.countDown();publishing.get(5,TimeUnit.SECONDS);assertEquals(List.of("new","new"),reading.get(5,TimeUnit.SECONDS));
}
@Test void reentrantLaneDoesNotDeadlockAndPublicationRejectsSymlinkEscape()throws Exception {
assertEquals("inside",lane.execute(project(),()->lane.execute(project(),()->"inside")));
Files.createSymbolicLink(root.resolve("escape"),root.getParent());
var edit=new PublishWorkspaceFilesRequest.Edit("escape/forbidden",null,bytes("unsafe"));
assertThrows(IllegalArgumentException.class,()->lane.execute(project(),()->{PackerFilePublication.publish(new PublishWorkspaceFilesRequest(project(),UUID.randomUUID().toString(),List.of(edit)));return null;}));
}
}