feat: bootstrap portable engineering protocol CLI
Implement milestones 0 and 1 with project and repository configuration, Work persistence, discussions, draft decisions, evidence integrity, and deterministic context exports. Add Java 25, Spring Boot and Modulith, native distributions, CI workflows, protocol documentation, and the pinned SDKMAN GraalVM CE toolchain. Validated with 20 passing tests and the macOS ARM64 native multi-repository continuity smoke.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package net.intrepid.aiengineering.artifact;
|
||||
|
||||
import net.intrepid.aiengineering.filesystem.*;
|
||||
import net.intrepid.aiengineering.work.*;
|
||||
import java.nio.file.*;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
public final class ArtifactService {
|
||||
public static final long MAX_BYTES = 1024 * 1024;
|
||||
private final WorkService works = new WorkService();
|
||||
public List<Artifact> list(Path root, String id) {
|
||||
var work = works.load(root, id); Path folder = works.folder(root, id);
|
||||
return work.artifacts().stream().map(ref -> {
|
||||
var artifact = Documents.read(Filespace.resolve(folder, "artifacts/evidence/" + ref + "/artifact.yaml"), Artifact.class);
|
||||
if (!artifact.id().equals(ref) || !artifact.workId().equals(id)) throw ProtocolException.validation("Artifact identity mismatch: " + ref);
|
||||
String expected = "artifacts/evidence/" + ref + "/payload";
|
||||
if (!artifact.path().equals(expected)) throw ProtocolException.validation("Invalid artifact payload reference: " + artifact.path());
|
||||
return artifact;
|
||||
}).toList();
|
||||
}
|
||||
public Artifact attach(Path root, String workId, Path source, String purpose, String origin, String author) {
|
||||
if (!Files.isRegularFile(source, LinkOption.NOFOLLOW_LINKS)) throw ProtocolException.missing("Evidence source is not a regular file: " + source);
|
||||
if (Filespace.io(() -> Files.size(source)) > MAX_BYTES) throw ProtocolException.invalid("Evidence exceeds 1 MiB; reference large resources instead");
|
||||
byte[] bytes = Filespace.io(() -> {
|
||||
try (var input = Files.newInputStream(source)) { return input.readNBytes((int) MAX_BYTES + 1); }
|
||||
});
|
||||
if (bytes.length > MAX_BYTES) throw ProtocolException.invalid("Evidence exceeds 1 MiB");
|
||||
return Filespace.locked(root, () -> {
|
||||
var work = works.load(root, workId); String id = works.nextReference(work.artifacts(), "ART");
|
||||
Path folder = works.folder(root, workId);
|
||||
Path parent = Filespace.resolve(folder, "artifacts/evidence");
|
||||
Path stage = Filespace.resolve(parent, "." + id + "-" + UUID.randomUUID() + ".tmp"); Filespace.directory(stage);
|
||||
var artifact = new Artifact(1, id, workId, origin == null ? source.getFileName().toString() : origin, purpose,
|
||||
"artifacts/evidence/" + id + "/payload", hash(bytes), bytes.length, author, Instant.now().toString());
|
||||
Filespace.writeBytes(stage.resolve("payload"), bytes, false);
|
||||
Filespace.write(stage.resolve("artifact.yaml"), Documents.yaml(artifact), false);
|
||||
Filespace.moveDirectory(stage, Filespace.resolve(parent, id));
|
||||
works.addReference(root, workId, "artifacts", id); return artifact;
|
||||
});
|
||||
}
|
||||
public void validate(Path root, String id) {
|
||||
for (var artifact : list(root, id)) {
|
||||
Path path = Filespace.resolve(works.folder(root, id), artifact.path());
|
||||
if (!Files.isRegularFile(path)) throw ProtocolException.validation("Missing artifact payload: " + artifact.path());
|
||||
if (Filespace.io(() -> Files.size(path)) != artifact.size() || artifact.size() > MAX_BYTES)
|
||||
throw ProtocolException.validation("Artifact size mismatch: " + artifact.id());
|
||||
if (!hash(Filespace.io(() -> Files.readAllBytes(path))).equals(artifact.sha256()))
|
||||
throw ProtocolException.validation("Artifact SHA-256 mismatch: " + artifact.id());
|
||||
}
|
||||
}
|
||||
private static String hash(byte[] bytes) {
|
||||
try { return HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(bytes)); }
|
||||
catch (java.security.NoSuchAlgorithmException e) { throw new IllegalStateException(e); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user