UNPKG

akm-cli

Version:

akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur

519 lines (518 loc) • 24.3 kB
// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. /** * `akm proposal` command family (#225). Leaf handlers are `defineJsonCommand`s, * which wrap the body in `runWithJsonErrors` and emit the JSON envelope * (stdout/stderr/exit-code). */ import { parsePositiveIntFlag } from "../../cli/parse-args.js"; import { defineGroupCommand, defineJsonCommand, EXIT_CODES, output } from "../../cli/shared.js"; import { setUsageErrorCode } from "../../cli/usage-kinds.js"; import { resolveStashDir } from "../../core/common.js"; import { loadConfig } from "../../core/config/config.js"; import { SHIPPED_ALIAS } from "../../core/config/model-aliases.js"; import { ConfigError, UsageError } from "../../core/errors.js"; import { stderr } from "../../core/stdio.js"; import { mergeNotices } from "../../execution/notices.js"; import { installLlmUsagePersistenceIfAbsent } from "../../llm/usage-persist.js"; import { withLlmStage } from "../../llm/usage-telemetry.js"; import { resolveImproveExecution } from "../improve/execution.js"; import { extractCommand } from "../improve/extract-cli.js"; import { resolveImproveStrategy } from "../improve/improve-strategies.js"; import { drainProposals } from "./drain.js"; import { akmProposalAccept, akmProposalAcceptMany, akmProposalDiff, akmProposalList, akmProposalReject, akmProposalReopen, akmProposalRevert, akmProposalShow, bulkAdjudicateProposals, } from "./proposal.js"; import { proposalWaitingSince } from "./proposal-types.js"; import { proposeCommand } from "./propose-cli.js"; /** * Parse + validate the shared bulk-adjudication filter flags * (`--max-diff-lines`, `--older-than`). Shared by the accept and reject bulk branches. */ function parseBulkFilterFlags(args) { const rawMaxDiff = args["max-diff-lines"] ? Number.parseInt(String(args["max-diff-lines"]), 10) : undefined; if (rawMaxDiff !== undefined && (Number.isNaN(rawMaxDiff) || rawMaxDiff < 0)) { throw new UsageError("--max-diff-lines must be a non-negative integer", "INVALID_FLAG_VALUE"); } const rawOlderThan = args["older-than"] ? Number.parseInt(String(args["older-than"]), 10) : undefined; if (rawOlderThan !== undefined && (Number.isNaN(rawOlderThan) || rawOlderThan < 0)) { throw new UsageError("--older-than must be a non-negative integer (days)", "INVALID_FLAG_VALUE"); } return { maxDiffLines: rawMaxDiff, olderThanMs: rawOlderThan !== undefined ? rawOlderThan * 86_400_000 : undefined, }; } function parseProposalStatus(raw) { if (raw === undefined) return undefined; const trimmed = raw.trim(); if (!trimmed) return undefined; if (trimmed === "pending" || trimmed === "accepted" || trimmed === "rejected" || trimmed === "reverted") { return trimmed; } throw new UsageError(`Invalid --status value: "${raw}". Expected one of: pending, accepted, rejected, reverted.`, "INVALID_FLAG_VALUE"); } const proposalListCommand = defineJsonCommand({ meta: { name: "list", description: "List proposal queue entries" }, args: { queue: { type: "string", description: "Select the proposal queue by source name" }, status: { type: "string", description: "Filter by status (pending|accepted|rejected|reverted)", }, ref: { type: "string", description: "Filter by asset ref ([bundle//]conceptId, e.g. knowledge/guide.md)" }, type: { type: "string", description: "Filter by asset type" }, generator: { type: "string", description: "Filter by generator (e.g. reflect, distill, consolidate-pair)", }, }, run({ args }) { const status = parseProposalStatus(args.status); const result = akmProposalList({ queue: args.queue, status, ref: args.ref, type: args.type, generator: args.generator, includeArchive: status === "accepted" || status === "rejected" || status === "reverted", }); output("proposal-list", result); }, }); const proposalAcceptCommand = defineJsonCommand({ meta: { name: "accept", description: "Accept proposals and promote them into the bundle. Several ids are accepted in one process and committed once.", }, args: { id: { type: "positional", description: "Proposal id (uuid or prefix) or asset ref; repeatable. Optional with --generator", required: false, }, queue: { type: "string", description: "Select the proposal queue by source name" }, bundle: { type: "string", description: "Write destination; must match the proposal's recorded target" }, // F-6 / #393: Batch accept by generator, diff size, or age. generator: { type: "string", description: "Bulk-accept pending proposals from this generator (e.g. reflect, distill)", }, "max-diff-lines": { type: "string", description: "Bulk accept only proposals of at most this many lines", }, "older-than": { type: "string", description: "Bulk accept only proposals older than this many days", }, "dry-run": { type: "boolean", description: "List proposals that would be bulk-accepted without accepting them.", default: false, }, yes: { type: "boolean", alias: "y", description: "Skip confirmation prompt (required in non-interactive mode for bulk accept)", default: false, }, }, async run({ args }) { const generator = args.generator; // F-6 / #393: Bulk-accept when --generator is provided without a positional id. if (generator && !args.id) { const { confirmDestructive } = await import("../../cli/confirm.js"); const confirmed = await confirmDestructive(`Bulk-accept all matching proposals from generator "${generator}"? This cannot be undone.`, { yes: args.yes === true || args["dry-run"] === true }); if (!confirmed) { stderr().write("Aborted.\n"); return; } const { maxDiffLines, olderThanMs } = parseBulkFilterFlags(args); const { count, results, skippedForContinuityRisk } = await bulkAdjudicateProposals({ action: "accept", generator, maxDiffLines, olderThanMs, dryRun: args["dry-run"], queue: args.queue, target: args.bundle, }); output("proposal-accept-batch", { accepted: count, results, // How many otherwise-matching proposals were skipped specifically // because they carry retirement.continuityRisk — visible under // --dry-run and on a real run alike, distinct from an ordinary // --max-diff-lines/--older-than miss. skippedForContinuityRisk, dryRun: args["dry-run"], }); return; } if (!args.id) { throw new UsageError("Usage: akm proposal accept <id> [<id> ...] OR akm proposal accept --generator <generator>", "MISSING_REQUIRED_ARGUMENT"); } // citty keeps every positional token in `_`, though it declares only `id`. const ids = (Array.isArray(args._) && args._.length > 0 ? args._ : [args.id]).map(String); if (ids.length > 1) { const results = await akmProposalAcceptMany({ ids, queue: args.queue, target: args.bundle, }); const failed = results.filter((result) => !result.ok).length; output("proposal-accept-batch", { accepted: results.length - failed, failed, results }); if (failed > 0) { process.exitCode = EXIT_CODES.GENERAL; setUsageErrorCode(results.flatMap((result) => (result.ok ? [] : [result.code]))[0]); } return; } const result = await akmProposalAccept({ id: ids[0], queue: args.queue, target: args.bundle, }); output("proposal-accept", result); }, }); const proposalRejectCommand = defineJsonCommand({ meta: { name: "reject", description: "Reject a proposal and record the reason" }, args: { id: { type: "positional", description: "Proposal id (uuid or prefix) or asset ref. Optional with --generator", required: false, }, reason: { type: "string", description: "Reason for rejection (required)" }, queue: { type: "string", description: "Select the proposal queue by source name" }, // F-6 / #393: Batch reject by generator, diff size, or age. generator: { type: "string", description: "Bulk-reject pending proposals from this generator (e.g. reflect, distill)", }, "max-diff-lines": { type: "string", description: "Bulk reject only proposals of at most this many lines", }, "older-than": { type: "string", description: "Bulk reject only proposals older than this many days", }, "dry-run": { type: "boolean", description: "List proposals that would be bulk-rejected without rejecting them.", default: false, }, // Deprecated no-op: reject no longer asks (a rejection is undone with `proposal reopen`). Removed in 0.11. yes: { type: "boolean", alias: "y", hidden: true, description: "Deprecated: reject no longer asks", default: false, }, }, async run({ args }) { const generator = args.generator; if (!args.reason || !String(args.reason).trim()) { throw new UsageError("Usage: akm proposal reject <id> --reason '<reason>' OR akm proposal reject --generator <generator> --reason '<reason>'", "MISSING_REQUIRED_ARGUMENT"); } // F-6 / #393: Bulk-reject when --generator is provided without a positional id. if (generator && !args.id) { const { maxDiffLines, olderThanMs } = parseBulkFilterFlags(args); const { count, results } = await bulkAdjudicateProposals({ action: "reject", generator, maxDiffLines, olderThanMs, dryRun: args["dry-run"], queue: args.queue, reason: String(args.reason), }); output("proposal-reject-batch", { rejected: count, results, dryRun: args["dry-run"] }); return; } if (!args.id) { throw new UsageError("Usage: akm proposal reject <id> --reason '<reason>' OR akm proposal reject --generator <generator> --reason '<reason>'", "MISSING_REQUIRED_ARGUMENT"); } const result = await akmProposalReject({ id: args.id, queue: args.queue, reason: String(args.reason), }); output("proposal-reject", result); }, }); // `proposal reopen` (#997): the undo a rejection lacked. Unlike `reject` it is // reversible (reject again), so it asks no confirmation. const proposalReopenCommand = defineJsonCommand({ meta: { name: "reopen", description: "Reopen rejected proposals: move them back to pending, keeping the rejection in their history. " + "Takes full proposal ids; refused (and none reopened) if any is not rejected or its target changed since it was created.", }, args: { id: { type: "positional", description: "Rejected proposal id (full uuid); repeat it to reopen several at once", required: true, }, reason: { type: "string", description: "Why the rejection is being undone (kept in the proposal's review history)", }, queue: { type: "string", description: "Select the proposal queue by source name" }, }, async run({ args }) { // citty keeps every positional token in `_`, though it declares only `id`. const ids = (Array.isArray(args._) && args._.length > 0 ? args._ : [args.id]).map(String); const reason = typeof args.reason === "string" && args.reason.trim() ? args.reason.trim() : undefined; const results = await akmProposalReopen({ ids, queue: args.queue, ...(reason !== undefined ? { reason } : {}), }); if (results.length === 1) output("proposal-reopen", results[0]); else output("proposal-reopen-batch", { reopened: results.length, results }); }, }); const proposalDiffCommand = defineJsonCommand({ meta: { name: "diff", description: "Show the diff for a proposal (accepts full UUID, UUID prefix of a pending proposal, or asset ref)", }, args: { id: { type: "positional", description: "Proposal id (uuid / prefix) or asset ref (e.g. skills/akm-dream)", required: true, }, queue: { type: "string", description: "Select the proposal queue by source name" }, bundle: { type: "string", description: "Diff destination; must match the proposal's recorded target" }, }, run({ args }) { const result = akmProposalDiff({ id: args.id, queue: args.queue, target: args.bundle }); output("proposal-diff", result); }, }); // Revert an accepted proposal. // // Exit codes (mapped by `runWithJsonErrors` from the typed errors thrown by // `akmProposalRevert` / `revertProposal`): // 0 — success; prior content restored. // 1 — generic error (also used by `UsageError("INVALID_FLAG_VALUE")` and // `UsageError("MISSING_REQUIRED_ARGUMENT")` when the proposal is not // accepted, or no backup is available). // 1 — `NotFoundError("FILE_NOT_FOUND")` when the proposal id does not resolve. const proposalRevertCommand = defineJsonCommand({ meta: { name: "revert", description: "Revert an accepted proposal: restore the prior asset content from the backup captured at promotion time. " + "Errors if the proposal is not accepted or has no backup (new-asset proposals leave no backup). " + "Accepts the full proposal UUID or the asset ref. UUID prefixes are not supported for archived proposals — use the full UUID.", }, args: { id: { type: "positional", description: "Proposal id (full uuid for archived proposals) or asset ref", required: true, }, queue: { type: "string", description: "Select the proposal queue by source name" }, bundle: { type: "string", description: "Write destination; must match the proposal's recorded target" }, }, async run({ args }) { const result = await akmProposalRevert({ id: args.id, queue: args.queue, target: args.bundle, }); output("proposal-revert", result); }, }); // `proposal show` (#225): show a single proposal with its validation findings. const proposalShowCommand = defineJsonCommand({ meta: { name: "show", description: "Show a single proposal and its validation findings" }, args: { id: { type: "positional", description: "Proposal id (uuid / prefix) or asset ref (e.g. skills/akm-dream)", required: true, }, queue: { type: "string", description: "Select the proposal queue by source name" }, }, run({ args }) { const result = akmProposalShow({ id: args.id, queue: args.queue }); output("proposal-show", result); }, }); const proposalDrainCommand = defineJsonCommand({ meta: { name: "drain", description: "Drain the pending proposal backlog: accept what a quality judge passed, reject empty diffs, leave the rest for judgment or review. The judgment tier accepts only consolidate promotions; every other kind waits for a person", }, args: { "dry-run": { type: "boolean", description: "List what would be accepted/rejected/deferred without writing.", default: false, }, yes: { type: "boolean", alias: "y", description: "Skip confirmation prompt (required in non-interactive mode for promotion).", default: false, }, "max-accepts": { type: "string", description: "Per-run accept ceiling; extra accepts are reported as skippedByCap", }, "older-than": { type: "string", description: "Only consider proposals created (or last reopened) more than this many days ago.", }, promote: { type: "boolean", description: "Promote (accept) judge-passed proposals; without it drain only queues decisions", default: false, }, judgment: { type: "boolean", description: "Enable the judgment tier for this drain (consolidate promotions only)", default: false, }, strategy: { type: "string", description: "Improve strategy whose triage block (applyMode, ceilings, judgment) to use", }, }, async run({ args }) { const stashDir = resolveStashDir(); const cfg = loadConfig(); // Read the triage block from the named improve strategy. CLI flags // always override config; config supplies defaults for any flag omitted, // except that a bare drain queues: the apply mode comes from `--promote` // or an explicitly named `--strategy`, never from the implicit default // strategy (#1274). const selectedStrategy = resolveImproveStrategy(args.strategy, cfg); const triageConfig = selectedStrategy.config.processes?.triage; const dryRun = args["dry-run"] === true; const applyMode = args.promote === true ? "promote" : args.strategy !== undefined ? (triageConfig?.applyMode ?? "queue") : "queue"; const maxAccepts = parsePositiveIntFlag(args["max-accepts"], "--max-accepts") ?? triageConfig?.maxAcceptsPerRun ?? 200; const rawOlderThan = parsePositiveIntFlag(args["older-than"], "--older-than"); const olderThanMs = rawOlderThan !== undefined ? rawOlderThan * 86_400_000 : undefined; // Promotion in promote mode is destructive (commits to git, no batch revert). if (applyMode === "promote" && !dryRun) { const { confirmDestructive } = await import("../../cli/confirm.js"); const confirmed = await confirmDestructive("Drain and promote judge-passed pending proposals? Promotions commit to git and cannot be batch-reverted.", { yes: args.yes === true }); if (!confirmed) { stderr().write("Aborted.\n"); return; } } // `--older-than` is applied here as a pre-filter on excludeIds: ids that // are too fresh are excluded so the drain never touches them. This reads // the pending set once here; drainProposals reads the pending set again // internally. let excludeIds; if (olderThanMs !== undefined) { const { listProposals } = await import("./repository.js"); const now = Date.now(); excludeIds = new Set(listProposals(stashDir, { status: "pending" }) // Fail SAFE: exclude a proposal when its age cannot be computed // (NaN date) OR it is too fresh. An unparseable date must never be // treated as old enough to drain/promote. Age counts from creation, // or from the last reopen (#997) — a proposal just reopened is fresh. .filter((proposal) => { const age = now - new Date(proposalWaitingSince(proposal)).getTime(); return Number.isNaN(age) || age < olderThanMs; }) .map((proposal) => proposal.id)); } // --judgment is an invocation-level opt-in that overrides a // disabled block. An explicitly selected strategy may opt in through its // judgment block; the implicit default strategy keeps standalone drain's // historical no-judge behavior. const judgmentWanted = args.judgment === true || (args.strategy !== undefined && triageConfig?.judgment?.enabled === true); const judgmentResolution = judgmentWanted ? resolveImproveExecution({ config: cfg, process: "triage", processName: "proposal-triage-judgment", alias: SHIPPED_ALIAS.judge, }) : null; if (judgmentWanted && !judgmentResolution) { throw new ConfigError("Proposal judgment was requested, but no connection or harness is configured to judge with.", "LLM_NOT_CONFIGURED", "Run `akm setup`, or set improve.connection or improve.processes.triage.connection."); } const judgment = judgmentResolution?.runner ?? null; // #576: persist + attribute per-call LLM usage for the standalone drain // path. `IfAbsent` keeps an enclosing `akm improve` sink in charge when // drain runs as a sub-step; the disposer clears only a sink we installed. const disposeDrainUsageSink = installLlmUsagePersistenceIfAbsent(); let result; try { result = await withLlmStage("drain", () => drainProposals({ stashDir, config: cfg, applyMode, maxAccepts, dryRun, ...(excludeIds ? { excludeIds } : {}), judgment, })); } finally { disposeDrainUsageSink(); } const dispatchNotices = result.notices; const notices = mergeNotices(judgmentResolution?.notices, dispatchNotices); output("proposal-drain", { schemaVersion: 1, ok: true, applyMode, dryRun, strategy: selectedStrategy.name, judgmentEngine: judgment?.name ?? null, judgmentKind: judgment?.kind ?? null, ...(notices ? { notices } : {}), promoted: result.promoted, rejected: result.rejected, deferred: result.deferred, skippedByCap: result.skippedByCap, staged: result.staged, failed: result.failed, }); }, }); // ── proposal noun group ─────────────────────────────────────────────────────── export const proposalCommand = defineGroupCommand({ meta: { name: "proposal", description: "Manage the proposal queue: list, show, diff, accept, reject, reopen, revert, extract, new, drain", }, // The group declared `--queue`/`--status`/`--ref`/`--type` only so the bare // form could act as `proposal list`. That form is gone (see below), and // `proposalListCommand` declares the identical set, so keeping them here // would just advertise flags in `akm proposal --help` that no group-level // body reads. subCommands: { list: proposalListCommand, show: proposalShowCommand, diff: proposalDiffCommand, accept: proposalAcceptCommand, reject: proposalRejectCommand, reopen: proposalReopenCommand, revert: proposalRevertCommand, drain: proposalDrainCommand, extract: extractCommand, new: proposeCommand, }, // Bare `akm proposal` runs `akm proposal list` (see insertBareListSubcommand in src/cli.ts). });