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 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 attachBytes(root, workId, bytes, purpose, origin == null ? source.getFileName().toString() : origin, author); } /** Framework-generated receipts use the same canonical artifact format and integrity checks. */ public Artifact attachBytes(Path root, String workId, byte[] content, String purpose, String origin, String author) { byte[] bytes = content.clone(); if (bytes.length > MAX_BYTES) throw ProtocolException.invalid("Evidence exceeds 1 MiB"); return Filespace.locked(root, () -> { var work = works.editable(root, workId); String id = works.nextReference(work.artifacts(), "ART"); Path folder = works.folder(root, workId); var artifact = writePrepared(folder, workId, id, bytes, purpose, origin, author); works.addReference(root, workId, "artifacts", id); return artifact; }); } /** Normal evidence format in an unpublished Work folder; caller publishes its Work references. */ public Artifact writePrepared(Path folder, String workId, String id, byte[] content, String purpose, String origin, String author) { Work.identifier(workId, "WRK"); Work.identifier(id, "ART"); byte[] bytes = content.clone(); if (bytes.length > MAX_BYTES) throw ProtocolException.invalid("Evidence exceeds 1 MiB"); 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, 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)); 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); } } }