#!/usr/bin/env node import { join, resolve } from "node:path"; import { applyCliOverrides, envOverrides, loadRepoConfig, } from "../config/load.js"; import { loadEnvLocal } from "../config/env.js"; import { describeConfig } from "../config/resolve.js"; import { initRepo } from "../init/copy.js"; import { approveStage, amendStage, rejectStage, runStage } from "../orchestrator/index.js"; import { writeKitJsonSchemas } from "../schemas/write-json.js"; import { ciReviewStage } from "../stages/ci-review.js"; import { fixStage } from "../stages/fix.js"; import { implementStage } from "../stages/implement.js"; import { intakeStage } from "../stages/intake.js"; import { prStage } from "../stages/pr.js"; import { reviewStage } from "../stages/review.js"; import { reviseFromLatestReview } from "../stages/revise.js"; import { specifyStage } from "../stages/specify.js"; import type { Span } from "@opentelemetry/api"; import type { AcdConfig, CliOverrides } from "../types/config.js"; import { isStageName, STAGE_NAMES, V1_STUBS, type StageName } from "../types/stages.js"; import { runTests } from "../hooks/mechanical.js"; import { listReviewRuns } from "../work/reviews.js"; import { listArtifacts, listWorkIds, readEvents, readState, resolveWorkId, saveWorkArtifact } from "../work/store.js"; import { emit } from "../work/events.js"; import { readFileSync } from "node:fs"; import { startUi } from "./ui.js"; import { decisionFromCommand, decisionFromStageResult, initTelemetry, noteValue, shutdownTelemetry, withSpan, } from "../telemetry/index.js"; function usage(): string { return `acd — Agentic Continuous Delivery runner (deterministic orchestrator) Usage: acd help | --help | -h acd-kit init [dir] [--cursor] [--github] acd-kit write-schemas acd-kit config acd intake --from "" acd specify [--artifact intent|behavior|feature|acceptance|all] acd implement [--scenario ] # id is the heading label (1 or E10a); omit: review follow-ups, else remaining scenarios acd review acd revise # fold the latest blocking review into the spec acd pr acd ci-review # alias: ci_review acd test acd fix acd approve acd reject --reason "..." acd amend [--note "..."] [--continue] acd status [work-id] acd list acd artifacts [work-id] acd reviews [work-id] acd events [work-id] acd artifact-save [--work-id ] [--contents "..."] acd ui [--port N] [--repo-root ] acd deploy|canary|rollback|hypothesis (stubs) HitL / guidance: approve — accept stage output and continue the pipeline reject — discard the pause with a reason (re-run the stage later) amend — keep artifacts, optionally add guidance, re-run (--continue) Global flags: --work-id --harness --tier classify|low|med|high --approve-policy all|none --require-approval | --no-require-approval --repo-root init: --cursor (Cursor adapter at repo root) --github (CI workflow) `; } function parseArgs(argv: string[]): { cmd: string; args: string[]; flags: Record; } { const [cmd = "help", ...rest] = argv; const args: string[] = []; const flags: Record = {}; for (let i = 0; i < rest.length; i++) { const token = rest[i] ?? ""; if (token === "--require-approval") flags.requireApproval = true; else if (token === "--no-require-approval") flags.requireApproval = false; else if (token.startsWith("--")) { const key = token.slice(2); const next = rest[i + 1]; if (next && !next.startsWith("--")) { flags[key] = next; i++; } else { flags[key] = true; } } else { args.push(token); } } return { cmd, args, flags }; } function overridesFromFlags(flags: Record): CliOverrides { const env = envOverrides(); const overrides: CliOverrides = { ...env }; if (typeof flags.harness === "string") overrides.harness = flags.harness; if (typeof flags.tier === "string") { overrides.tier = flags.tier as CliOverrides["tier"]; } if (typeof flags["approve-policy"] === "string") { const p = flags["approve-policy"]; if (p === "all" || p === "none") overrides.approvePolicy = p; } if (typeof flags.requireApproval === "boolean") { overrides.requireApproval = flags.requireApproval; } if (typeof flags["work-id"] === "string") overrides.workId = flags["work-id"]; if (typeof flags["repo-root"] === "string") overrides.repoRoot = flags["repo-root"]; return overrides; } async function main(): Promise { const { cmd, args, flags } = parseArgs(process.argv.slice(2)); const repoRoot = resolve( typeof flags["repo-root"] === "string" ? flags["repo-root"] : process.cwd(), ); loadEnvLocal(repoRoot); if (cmd === "help" || cmd === "--help" || cmd === "-h") { process.stdout.write(usage()); return; } const overrides = overridesFromFlags(flags); if (!process.env.ACD_REPO_ROOT) process.env.ACD_REPO_ROOT = repoRoot; if (cmd === "init") { initTelemetry(); try { await withSpan( "acd.command.init", { "acd.command": "init", "acd.repo_root": repoRoot }, async (span) => { const target = resolve(args[0] ?? repoRoot); initRepo(target, { cursorIde: flags.cursor === true, github: flags.github === true, }); noteValue("initialized .acd", { dir: join(target, ".acd") }, span); process.stdout.write( `Initialized ACD in ${join(target, ".acd")}\n\n` + `Next:\n` + ` npx acd-kit ui\n` + ` npx acd-kit intake --from ""\n`, ); }, ); } finally { await shutdownTelemetry(); } return; } if (cmd === "ui") { const port = typeof flags.port === "string" ? Number(flags.port) : undefined; await startUi({ repoRoot, port }); return; } if (cmd === "write-schemas") { writeKitJsonSchemas(); process.stdout.write("Wrote acd/schemas\n"); return; } const config = applyCliOverrides(loadRepoConfig(repoRoot), overrides); const ctx = { repoRoot, config, overrides }; initTelemetry(config.telemetry); try { await withSpan( `acd.command.${cmd}`, { "acd.command": cmd, "acd.repo_root": repoRoot }, async (span) => { await dispatchCommand(cmd, args, flags, ctx, span); }, ); } finally { await shutdownTelemetry(); } } async function dispatchCommand( cmd: string, args: string[], flags: Record, ctx: { repoRoot: string; config: AcdConfig; overrides: CliOverrides }, span: Span, ): Promise { const { repoRoot, config, overrides } = ctx; if (cmd === "config") { const described = describeConfig(config, overrides); noteValue(decisionFromCommand("config", described), described, span); process.stdout.write(JSON.stringify(described, null, 2) + "\n"); return; } if (cmd === "intake") { const from = flags.from; if (typeof from !== "string" || !from.trim()) { throw new Error(`acd intake requires --from ""`); } const result = await runStage(intakeStage, { sourceText: from }, ctx); noteValue(decisionFromStageResult("intake", result), stagePayload(result), span); printJson(result); return; } if (cmd === "specify") { const artifact = (flags.artifact as string) ?? "all"; const workId = resolveWorkId(repoRoot, overrides.workId); const result = await runStage( specifyStage, { workId, artifact }, { ...ctx, workId }, ); noteValue(decisionFromStageResult("specify", result), stagePayload(result), span); printJson(result); if (result.awaitingGuidance) process.exitCode = 2; return; } if (cmd === "implement") { const workId = resolveWorkId(repoRoot, overrides.workId); const scenario = typeof flags.scenario === "string" ? flags.scenario : undefined; const result = await runStage( implementStage, { workId, scenario }, { ...ctx, workId }, ); noteValue(decisionFromStageResult("implement", result), stagePayload(result), span); printJson(result); return; } if (cmd === "review") { const workId = resolveWorkId(repoRoot, overrides.workId); const result = await runStage(reviewStage, { workId }, { ...ctx, workId }); noteValue(decisionFromStageResult("review", result), stagePayload(result), span); printJson(result); if (result.output?.decision === "block") process.exitCode = 1; return; } if (cmd === "revise") { const workId = resolveWorkId(repoRoot, overrides.workId); const result = await reviseFromLatestReview(repoRoot, workId, config, overrides); const summary = { reopened: result.reopened.length, addedScenarios: result.addedScenarios.length, acceptanceRows: result.acceptanceRows, notes: result.notes.length, unmapped: result.unmapped.length, queue: result.implementQueue.map((job) => ({ index: job.index, mode: job.mode, })), clearedApproval: result.clearedApproval, }; noteValue(decisionFromCommand("revise", summary), summary, span); printJson(result); return; } if (cmd === "pr") { const workId = resolveWorkId(repoRoot, overrides.workId); const result = await runStage(prStage, { workId }, { ...ctx, workId }); noteValue(decisionFromStageResult("pr", result), stagePayload(result), span); printJson(result); return; } if (cmd === "ci-review" || cmd === "ci_review") { const ids = listWorkIds(repoRoot); if (ids.length === 0) { const tests = runTests(config, repoRoot); const payload = { decision: tests.ok ? "pass" : "block", findings: [], testsOk: tests.ok, note: "No ACD work items; ran tests only.", }; noteValue(decisionFromCommand("ci-review", payload), payload, span); printJson(payload); if (!tests.ok) process.exitCode = 1; return; } const workId = overrides.workId ? resolveWorkId(repoRoot, overrides.workId) : undefined; const result = await runStage( ciReviewStage, { workId }, { ...ctx, workId }, ); noteValue(decisionFromStageResult("ci_review", result), stagePayload(result), span); printJson(result); if (result.output?.decision === "block") process.exitCode = 1; return; } if (cmd === "test") { const result = runTests(config, repoRoot); noteValue(decisionFromCommand("test", result), result, span); printJson(result); if (!result.ok) process.exitCode = 1; return; } if (cmd === "fix") { const workId = resolveWorkId(repoRoot, overrides.workId); const result = await runStage(fixStage, { workId }, { ...ctx, workId }); noteValue(decisionFromStageResult("fix", result), stagePayload(result), span); printJson(result); if (result.output?.pipelineStatus === "red") process.exitCode = 1; return; } if (cmd === "approve") { const stage = args[0]; if (!stage || !isStageName(stage)) { throw new Error(`acd approve requires a stage (${STAGE_NAMES.join(", ")})`); } const workId = resolveWorkId(repoRoot, overrides.workId); const state = approveStage( repoRoot, workId, stage, process.env.USER ?? "human", ); noteValue(`approved ${stage}`, state, span); printJson(state); return; } if (cmd === "reject") { const stage = args[0]; if (!stage || !isStageName(stage)) { throw new Error(`acd reject requires a stage`); } const reason = typeof flags.reason === "string" ? flags.reason : "rejected"; const workId = resolveWorkId(repoRoot, overrides.workId); const state = rejectStage( repoRoot, workId, stage, reason, process.env.USER ?? "human", ); noteValue(`rejected ${stage}: ${reason}`, { ...state, reason }, span); printJson(state); return; } if (cmd === "amend") { const stage = args[0]; if (!stage || !isStageName(stage)) { throw new Error(`acd amend requires a stage (${STAGE_NAMES.join(", ")})`); } const note = typeof flags.note === "string" ? flags.note : null; const workId = resolveWorkId(repoRoot, overrides.workId); const state = amendStage( repoRoot, workId, stage, note, process.env.USER ?? "human", ); if (flags.continue === true || flags.continue === "true") { if (stage === "specify") { const result = await runStage( specifyStage, { workId, artifact: "all" }, { ...ctx, workId, overrides: { ...overrides, workId } }, ); noteValue( `amended ${stage} and continued: ${decisionFromStageResult("specify", result)}`, { amended: state, continued: stagePayload(result) }, span, ); printJson({ amended: state, continued: result }); if (result.awaitingGuidance) process.exitCode = 2; return; } throw new Error( `acd amend --continue is implemented for specify in v1; re-run \`acd ${stage}\` manually.`, ); } noteValue(`amended ${stage}`, state, span); printJson(state); process.stderr.write( `acd: amended '${stage}'. Re-run: npx acd ${stage}` + (note ? ` (guidance note saved)` : "") + `\n`, ); return; } if (cmd === "status") { const workId = resolveWorkId(repoRoot, args[0] ?? overrides.workId); const state = readState(repoRoot, workId); const events = readEvents(repoRoot, workId); const payload = { state, lastEvent: events.at(-1) ?? null, eventCount: events.length, }; noteValue(decisionFromCommand("status", payload), payload, span); printJson(payload); return; } if (cmd === "list") { const ids = listWorkIds(repoRoot); const payload = { workIds: ids, latest: ids.at(-1) ?? null }; noteValue(decisionFromCommand("list", payload), payload, span); printJson(payload); return; } if (cmd === "artifacts") { const workId = resolveWorkId(repoRoot, args[0] ?? overrides.workId); const payload = { workId, artifacts: listArtifacts(repoRoot, workId), }; noteValue(decisionFromCommand("artifacts", payload), payload, span); printJson(payload); return; } if (cmd === "reviews") { const workId = resolveWorkId(repoRoot, args[0] ?? overrides.workId); const reviews = listReviewRuns(readEvents(repoRoot, workId)); const latest = reviews.at(-1); const payload = { workId, reviews }; noteValue( decisionFromCommand("reviews", { reviewCount: reviews.length, latestDecision: latest?.decision ?? null, latestFindings: latest?.findings.length ?? 0, }), { reviewCount: reviews.length, latestDecision: latest?.decision ?? null, latestFindings: latest?.findings.length ?? 0, }, span, ); printJson(payload); return; } if (cmd === "events") { const workId = resolveWorkId(repoRoot, args[0] ?? overrides.workId); const events = readEvents(repoRoot, workId); const latest = events.at(-1); const summary = { eventCount: events.length, latestType: latest?.type ?? null, }; noteValue(decisionFromCommand("events", summary), summary, span); printJson({ workId, events }); return; } if (cmd === "artifact-save") { const name = args[0]; if (!name) { throw new Error(`acd artifact-save requires a file name (e.g. intent.md)`); } const workId = resolveWorkId(repoRoot, overrides.workId ?? args[1]); const contents = readArtifactSaveContents(flags); const path = saveWorkArtifact(repoRoot, workId, name, contents); emit(repoRoot, workId, "artifact.saved", undefined, { name, bytes: Buffer.byteLength(contents, "utf8"), }); noteValue(`saved artifact ${name}`, { workId, name, path, bytes: Buffer.byteLength(contents, "utf8") }, span); printJson({ workId, name, path, bytes: Buffer.byteLength(contents, "utf8") }); return; } if (V1_STUBS.includes(cmd as StageName) || cmd === "deploy") { throw new Error( `Stage '${cmd}' is named in the ACD state machine but not implemented in v1.`, ); } throw new Error(`Unknown command '${cmd}'.\n${usage()}`); } function printJson(value: unknown): void { process.stdout.write(JSON.stringify(value, null, 2) + "\n"); } function stagePayload(result: { output: unknown; awaitingApproval: boolean; awaitingGuidance: boolean; }): unknown { const output = result.output && typeof result.output === "object" ? (result.output as Record) : { value: result.output }; return { ...output, awaitingApproval: result.awaitingApproval, awaitingGuidance: result.awaitingGuidance, }; } function readArtifactSaveContents(flags: Record): string { if (typeof flags.contents === "string") return flags.contents; if (process.stdin.isTTY) { throw new Error( `acd artifact-save requires --contents "..." or file contents on stdin`, ); } return readFileSync(0, "utf8"); } main().catch(async (err) => { process.stderr.write(`${err instanceof Error ? err.message : err}\n`); await shutdownTelemetry(); process.exit(1); });