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,32 @@
|
||||
package net.intrepid.aiengineering.project;
|
||||
|
||||
import net.intrepid.aiengineering.filesystem.ProtocolException;
|
||||
import java.util.List;
|
||||
|
||||
public record ProjectConfig(int schemaVersion, Identity project, List<Repository> repositories) {
|
||||
public ProjectConfig {
|
||||
ProtocolException.schema(schemaVersion);
|
||||
if (project == null || repositories == null) throw ProtocolException.validation("project and repositories are required");
|
||||
repositories = List.copyOf(repositories);
|
||||
if (repositories.stream().map(Repository::id).distinct().count() != repositories.size())
|
||||
throw ProtocolException.validation("Duplicate repository IDs");
|
||||
}
|
||||
public record Identity(String id, String name) {
|
||||
public Identity { identifier(id); ProtocolException.text(name, "project name"); }
|
||||
}
|
||||
public record Access(boolean read, boolean edit) {
|
||||
public Access { if (edit && !read) throw ProtocolException.invalid("edit access requires read access"); }
|
||||
}
|
||||
public record Repository(String id, String path, String type, String language, Access access, List<String> validationCommands) {
|
||||
public Repository {
|
||||
identifier(id); ProtocolException.text(path, "repository path");
|
||||
if (!List.of("linked-git", "git-submodule").contains(type == null ? "" : type))
|
||||
throw ProtocolException.invalid("Repository type must be linked-git or git-submodule");
|
||||
access = access == null ? new Access(true, false) : access;
|
||||
validationCommands = validationCommands == null ? List.of() : List.copyOf(validationCommands);
|
||||
}
|
||||
}
|
||||
public static void identifier(String id) {
|
||||
if (id == null || !id.matches("[a-z][a-z0-9-]{0,63}")) throw ProtocolException.invalid("Invalid identifier: " + id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package net.intrepid.aiengineering.project;
|
||||
|
||||
import net.intrepid.aiengineering.filesystem.*;
|
||||
import net.intrepid.aiengineering.project.internal.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
|
||||
public final class ProjectService {
|
||||
private final GitAdapter git = new ProcessGitAdapter();
|
||||
public Path discover(Path start, Path explicit) {
|
||||
if (explicit != null) {
|
||||
Path root = canonical(explicit);
|
||||
if (root.getFileName().toString().equals(".ai-engineering")) root = root.getParent();
|
||||
requireConfig(root); return root;
|
||||
}
|
||||
for (Path current = canonical(start); current != null; current = current.getParent()) {
|
||||
if (Files.exists(current.resolve(".ai-engineering"), LinkOption.NOFOLLOW_LINKS)) {
|
||||
requireConfig(current); return current;
|
||||
}
|
||||
}
|
||||
throw ProtocolException.missing("Engineering project not found; run ai-engineering init or use --project");
|
||||
}
|
||||
private static Path canonical(Path path) { return Filespace.io(() -> path.toAbsolutePath().normalize().toRealPath()); }
|
||||
private void requireConfig(Path root) {
|
||||
Path file = Filespace.resolve(root, ".ai-engineering/ai-engineering.yaml");
|
||||
if (!Files.isRegularFile(file)) throw ProtocolException.missing("Missing project configuration: " + file);
|
||||
}
|
||||
public ProjectConfig initialize(Path root, String name) {
|
||||
root = canonical(root);
|
||||
var identity = new ProjectConfig.Identity(slug(name), name);
|
||||
var config = new ProjectConfig(1, identity, List.of());
|
||||
Path target = Filespace.resolve(root, ".ai-engineering");
|
||||
if (Files.exists(target, LinkOption.NOFOLLOW_LINKS)) throw ProtocolException.invalid("Project already exists: " + target);
|
||||
Path staging = Filespace.resolve(root, ".ai-engineering-init-" + UUID.randomUUID());
|
||||
Filespace.directory(staging);
|
||||
for (String dir : List.of("works", "contracts", ".local")) Filespace.directory(staging.resolve(dir));
|
||||
Filespace.write(staging.resolve("ai-engineering.yaml"), Documents.yaml(config), false);
|
||||
Filespace.write(staging.resolve(".gitignore"), ".local/\nworks/.*.tmp\nworks/*/runs/\n", false);
|
||||
Filespace.moveDirectory(staging, target);
|
||||
return config;
|
||||
}
|
||||
public static String slug(String text) {
|
||||
ProtocolException.text(text, "name");
|
||||
String result = java.text.Normalizer.normalize(text, java.text.Normalizer.Form.NFD)
|
||||
.replaceAll("\\p{M}", "").toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]+", "-").replaceAll("^-|-$", "");
|
||||
if (result.isBlank()) result = "project";
|
||||
if (!Character.isLetter(result.charAt(0))) result = "p-" + result;
|
||||
return result.substring(0, Math.min(result.length(), 60)).replaceAll("-$", "");
|
||||
}
|
||||
public ProjectConfig load(Path root) { return Documents.read(Filespace.resolve(root, ".ai-engineering/ai-engineering.yaml"), ProjectConfig.class); }
|
||||
public ProjectConfig addRepository(Path root, ProjectConfig.Repository repository) {
|
||||
return Filespace.locked(root, () -> {
|
||||
var config = load(root);
|
||||
if (config.repositories().stream().anyMatch(r -> r.id().equals(repository.id()))) throw ProtocolException.invalid("Repository already registered: " + repository.id());
|
||||
Path path = repositoryPath(root, repository);
|
||||
if (config.repositories().stream().anyMatch(r -> repositoryPath(root, r).equals(path))) throw ProtocolException.invalid("Repository path already registered: " + path);
|
||||
git.inspect(root, path, repository.type());
|
||||
var repositories = new ArrayList<>(config.repositories()); repositories.add(repository);
|
||||
var updated = new ProjectConfig(1, config.project(), repositories);
|
||||
Filespace.write(Filespace.resolve(root, ".ai-engineering/ai-engineering.yaml"), Documents.yaml(updated), true);
|
||||
return updated;
|
||||
});
|
||||
}
|
||||
public Path repositoryPath(Path root, ProjectConfig.Repository repository) {
|
||||
Path path = Filespace.resolve(root, repository.path());
|
||||
if (path.equals(root) || path.startsWith(root.resolve(".ai-engineering"))) throw ProtocolException.invalid("Repository must be a source directory beneath project root");
|
||||
return path;
|
||||
}
|
||||
public void validate(Path root) {
|
||||
var config = load(root); var paths = new HashSet<Path>();
|
||||
for (var repo : config.repositories()) {
|
||||
Path path = repositoryPath(root, repo);
|
||||
if (!paths.add(path)) throw ProtocolException.validation("Duplicate repository path: " + repo.path());
|
||||
git.inspect(root, path, repo.type());
|
||||
}
|
||||
}
|
||||
public record Revision(String repositoryId, String path, String head, boolean dirty) {
|
||||
public Revision {
|
||||
ProjectConfig.identifier(repositoryId); ProtocolException.text(path, "repository snapshot path");
|
||||
if (head == null || !head.matches("UNBORN|[a-f0-9]{40}|[a-f0-9]{64}"))
|
||||
throw ProtocolException.validation("Invalid repository revision: " + head);
|
||||
}
|
||||
}
|
||||
public List<Revision> revisions(Path root, List<String> ids) {
|
||||
var config = load(root);
|
||||
return ids.stream().map(id -> {
|
||||
var repo = config.repositories().stream().filter(r -> r.id().equals(id)).findFirst()
|
||||
.orElseThrow(() -> ProtocolException.validation("Unknown repository: " + id));
|
||||
var snapshot = git.inspect(root, repositoryPath(root, repo), repo.type());
|
||||
return new Revision(id, repo.path(), snapshot.head(), snapshot.dirty());
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package net.intrepid.aiengineering.project.internal;
|
||||
|
||||
import java.nio.file.Path;
|
||||
public interface GitAdapter {
|
||||
record Snapshot(String head, boolean dirty) {}
|
||||
Snapshot inspect(Path root, Path repository, String type);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.intrepid.aiengineering.project.internal;
|
||||
|
||||
import net.intrepid.aiengineering.filesystem.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** Read-only Git adapter. No init, checkout, submodule update, commit or network operations. */
|
||||
public final class ProcessGitAdapter implements GitAdapter {
|
||||
private String git(Path path, String... args) {
|
||||
return Filespace.io(() -> {
|
||||
var command = new java.util.ArrayList<String>(java.util.List.of("git", "--no-optional-locks", "-C", path.toString()));
|
||||
command.addAll(java.util.List.of(args));
|
||||
var builder = new ProcessBuilder(command).redirectErrorStream(true);
|
||||
builder.environment().keySet().removeIf(k -> k.startsWith("GIT_"));
|
||||
var process = builder.start();
|
||||
String output;
|
||||
try (var executor = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) {
|
||||
var reader = executor.submit(() -> {
|
||||
try (var input = process.getInputStream()) {
|
||||
byte[] prefix = input.readNBytes(65536);
|
||||
input.transferTo(java.io.OutputStream.nullOutputStream());
|
||||
return new String(prefix, java.nio.charset.StandardCharsets.UTF_8).strip();
|
||||
}
|
||||
});
|
||||
try {
|
||||
if (!process.waitFor(10, TimeUnit.SECONDS)) { process.destroyForcibly(); throw ProtocolException.validation("Git inspection timed out: " + path); }
|
||||
output = reader.get(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
process.destroyForcibly(); Thread.currentThread().interrupt(); throw new java.io.IOException("Git inspection interrupted", e);
|
||||
} catch (java.util.concurrent.ExecutionException | java.util.concurrent.TimeoutException e) {
|
||||
process.destroyForcibly(); throw new java.io.IOException("Cannot read Git inspection output", e);
|
||||
}
|
||||
}
|
||||
if (process.exitValue() != 0) throw ProtocolException.validation("Git inspection failed: " + output);
|
||||
return output;
|
||||
});
|
||||
}
|
||||
@Override public Snapshot inspect(Path root, Path repository, String type) {
|
||||
if (!Files.isDirectory(repository)) throw ProtocolException.missing("Missing repository: " + repository);
|
||||
Path actual = Path.of(git(repository, "rev-parse", "--show-toplevel"));
|
||||
if (!Filespace.io(actual::toRealPath).equals(Filespace.io(repository::toRealPath)))
|
||||
throw ProtocolException.validation("Not an independent Git repository: " + repository);
|
||||
if (type.equals("git-submodule")) {
|
||||
String superproject = git(repository, "rev-parse", "--show-superproject-working-tree");
|
||||
if (superproject.isBlank() || !Filespace.io(() -> Path.of(superproject).toRealPath()).equals(Filespace.io(root::toRealPath)))
|
||||
throw ProtocolException.validation("Repository is not a submodule of this project: " + repository);
|
||||
}
|
||||
String head;
|
||||
try { head = git(repository, "rev-parse", "--verify", "HEAD"); }
|
||||
catch (ProtocolException e) { head = "UNBORN"; }
|
||||
return new Snapshot(head, !git(repository, "status", "--porcelain", "--untracked-files=normal").isBlank());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
@org.springframework.modulith.ApplicationModule(allowedDependencies = "filesystem")
|
||||
package net.intrepid.aiengineering.project;
|
||||
Reference in New Issue
Block a user