implements PR-10f bank composition apply through packer and snapshot refresh

This commit is contained in:
2026-03-24 13:42:50 +00:00
parent 401f27a5bb
commit 2fa604e308
11 changed files with 288 additions and 17 deletions
@@ -599,6 +599,13 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
return writeCoordinator.execute(project, () -> updateAssetContractInWriteLane(safeRequest));
}
@Override
public ApplyBankCompositionResponse applyBankComposition(final ApplyBankCompositionRequest request) {
final ApplyBankCompositionRequest safeRequest = Objects.requireNonNull(request, "request");
final PackerProjectContext project = safeRequest.project();
return writeCoordinator.execute(project, () -> applyBankCompositionInWriteLane(safeRequest));
}
private UpdateAssetContractResponse updateAssetContractInWriteLane(UpdateAssetContractRequest request) {
final PackerProjectContext project = request.project();
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
@@ -656,6 +663,52 @@ 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 PackerDeleteAssetEvaluation evaluation = actionReadService.evaluateDelete(snapshot, project, request.assetReference());
if (!evaluation.canDelete()) {
return new ApplyBankCompositionResponse(
false,
Objects.requireNonNullElse(evaluation.reason(), "Asset bank composition cannot be updated."));
}
final Path assetRoot = evaluation.resolved().assetRoot();
final Path manifestPath = assetRoot.resolve("asset.json");
if (!Files.isRegularFile(manifestPath)) {
return new ApplyBankCompositionResponse(false, "asset.json was not found for the requested asset root.");
}
final ObjectNode manifest;
try {
final JsonNode rawManifest = mapper.readTree(manifestPath.toFile());
if (!(rawManifest instanceof ObjectNode objectNode)) {
return new ApplyBankCompositionResponse(false, "asset.json must contain a JSON object at the root.");
}
manifest = objectNode;
} catch (IOException exception) {
return new ApplyBankCompositionResponse(false, "Unable to read asset manifest: " + exception.getMessage());
}
try {
patchManifestArtifacts(manifest, request);
mapper.writerWithDefaultPrettyPrinter().writeValue(manifestPath.toFile(), manifest);
final var runtime = runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterUpdateAssetContract(
currentSnapshot,
generation,
assetRoot,
manifestPath,
evaluation.resolved().registryEntry()));
saveRuntimeCache(project, runtime.snapshot());
return new ApplyBankCompositionResponse(true, null);
} catch (IOException exception) {
return new ApplyBankCompositionResponse(false, "Unable to update asset bank composition: " + exception.getMessage());
} catch (RuntimeException exception) {
return new ApplyBankCompositionResponse(false, "Unable to update runtime snapshot: " + exception.getMessage());
}
}
private OutputFormatCatalog resolveManifestOutputFormat(ObjectNode manifest) {
final JsonNode outputNode = manifest.get("output");
if (!(outputNode instanceof ObjectNode outputObject)) {
@@ -701,6 +754,27 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
});
}
private void patchManifestArtifacts(ObjectNode manifest, ApplyBankCompositionRequest request) {
final ObjectNode inputsNode = mutableObject(manifest, "inputs");
inputsNode.removeAll();
final var artifactsNode = manifest.putArray("artifacts");
for (int index = 0; index < request.selectedFiles().size(); index += 1) {
final String file = Objects.requireNonNullElse(request.selectedFiles().get(index), "").trim();
if (file.isBlank() || !isTrustedRelativePath(file)) {
throw new IllegalArgumentException("Selected artifact file paths must stay inside the asset root.");
}
final var artifactNode = artifactsNode.addObject();
artifactNode.put("file", file);
artifactNode.put("index", index);
}
}
private boolean isTrustedRelativePath(String value) {
final Path path = Path.of(value).normalize();
return !path.isAbsolute() && !path.startsWith("..");
}
private ObjectNode mutableObject(ObjectNode parent, String fieldName) {
final JsonNode current = parent.get(fieldName);
if (current instanceof ObjectNode objectNode) {
@@ -7,6 +7,7 @@ import p.packer.messages.assets.OutputCodecCatalog;
import p.packer.messages.assets.OutputFormatCatalog;
import p.packer.messages.diagnostics.PackerDiagnosticCategory;
import p.packer.messages.diagnostics.PackerDiagnosticSeverity;
import p.packer.models.PackerAssetArtifactSelection;
import p.packer.models.PackerAssetDeclaration;
import p.packer.models.PackerAssetDeclarationParseResult;
import p.packer.models.PackerDiagnostic;
@@ -45,6 +46,7 @@ public final class PackerAssetDeclarationParser {
final var name = requiredText(root, "name", diagnostics, manifestPath);
final var assetFamily = requiredAssetFamily(root, diagnostics, manifestPath);
final var inputsByRole = requiredInputs(root.path("inputs"), diagnostics, manifestPath);
final var artifacts = optionalArtifacts(root.path("artifacts"), diagnostics, manifestPath);
final var outputFormat = requiredOutputFormat(root.path("output"), diagnostics, manifestPath);
final var outputCodec = requiredOutputCodec(root.path("output"), diagnostics, manifestPath);
final var outputMetadata = optionalOutputMetadata(root.path("output"), diagnostics, manifestPath);
@@ -70,6 +72,7 @@ public final class PackerAssetDeclarationParser {
name,
assetFamily,
inputsByRole,
artifacts,
outputFormat,
outputCodec,
outputMetadata,
@@ -274,6 +277,61 @@ public final class PackerAssetDeclarationParser {
return Map.copyOf(result);
}
private List<PackerAssetArtifactSelection> optionalArtifacts(
final JsonNode node,
final List<PackerDiagnostic> diagnostics,
final Path manifestPath) {
if (node.isMissingNode() || node.isNull()) {
return List.of();
}
if (!node.isArray()) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Field 'artifacts' must be an array.",
manifestPath,
true));
return List.of();
}
final List<PackerAssetArtifactSelection> result = new ArrayList<>();
for (JsonNode entry : node) {
if (!entry.isObject()) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Each artifact entry must be an object with 'file' and 'index'.",
manifestPath,
true));
continue;
}
final JsonNode fileNode = entry.path("file");
final JsonNode indexNode = entry.path("index");
if (!fileNode.isTextual() || fileNode.asText().isBlank() || !indexNode.isInt() || indexNode.asInt() < 0) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Each artifact entry must define a non-blank 'file' and a non-negative integer 'index'.",
manifestPath,
true));
continue;
}
final String relativePath = fileNode.asText().trim();
if (!isTrustedRelativePath(relativePath)) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Artifact file path must stay inside the asset root.",
manifestPath,
true));
continue;
}
result.add(new PackerAssetArtifactSelection(relativePath, indexNode.asInt()));
}
result.sort(Comparator.comparingInt(PackerAssetArtifactSelection::index));
return List.copyOf(result);
}
private boolean isTrustedRelativePath(final String value) {
final Path path = Path.of(value).normalize();
return !path.isAbsolute() && !path.startsWith("..");
@@ -150,7 +150,7 @@ public final class PackerAssetDetailsService {
.map(this::toBankCompositionFile)
.toList();
final List<PackerBankCompositionFile> selectedFiles = flattenSelectedInputPaths(declaration.inputsByRole()).stream()
final List<PackerBankCompositionFile> selectedFiles = selectedPathsFor(declaration).stream()
.map(path -> resolveSelectedBankFile(runtimeAsset.assetRoot(), path, walkFilesByPath))
.flatMap(Optional::stream)
.toList();
@@ -161,9 +161,15 @@ public final class PackerAssetDetailsService {
walkProjection.measuredBankSizeBytes());
}
private List<String> flattenSelectedInputPaths(Map<String, List<String>> inputsByRole) {
private List<String> selectedPathsFor(PackerAssetDeclaration declaration) {
if (!declaration.artifacts().isEmpty()) {
return declaration.artifacts().stream()
.sorted(Comparator.comparingInt(PackerAssetArtifactSelection::index))
.map(PackerAssetArtifactSelection::file)
.toList();
}
final List<String> selected = new ArrayList<>();
inputsByRole.values().forEach(selected::addAll);
declaration.inputsByRole().values().forEach(selected::addAll);
return List.copyOf(selected);
}