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(); 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 revisions(Path root, List 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(); } }