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
575 lines (574 loc) • 28.2 kB
JavaScript
// 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/.
import { toErrorMessage } from "../../core/common.js";
import { EXTRACT_INFRASTRUCTURE_SKIP_REASONS } from "../../core/improve-types.js";
import { listPendingStateMigrations } from "../../core/state-db.js";
import { learningStatus } from "../../setup/learning.js";
import { STATE_DB_FREELIST_WARN_RATIO, } from "../../storage/state-db-integrity.js";
import { resolveImproveStrategy, resolveProcessEnabled } from "../improve/improve-strategies.js";
import { resolveProposalQueueTarget } from "../proposal/repository.js";
import { semanticSearchCheck } from "./semantic-search-check.js";
import { TASK_FAIL_RATE_WARN } from "./types.js";
/**
* Resolve the proposal queue owner through the resolver extract and every other
* proposal writer use. A strategy that does not resolve carries the resolver's error: extraction is then not known to
* be on or off.
*/
export function proposalQueueState(config, stashDir) {
let extractEnabled = false;
let strategy;
try {
const selected = resolveImproveStrategy(undefined, config);
extractEnabled = resolveProcessEnabled("extract", selected.config);
strategy = { strategy: selected.name };
}
catch (error) {
strategy = { strategyError: toErrorMessage(error) };
}
const base = { stashDir, learningOn: learningStatus(config).on, ...strategy, extractEnabled };
try {
resolveProposalQueueTarget(stashDir, config);
return base;
}
catch (error) {
return { ...base, problem: toErrorMessage(error) };
}
}
/** Rolling window the `session-extraction` check reads from the extract ledger (#914). */
export const SESSION_EXTRACTION_LEDGER_WINDOW_DAYS = 7;
/**
* `state-db-migrations` when the open itself failed: the migrations still
* pending after a failed apply, from a read-only listing that never applies
* anything. A `fail` row, not a crash, is what a bundler watches for instead
* of grepping akm's error text.
*/
export function runPendingStateMigrationsCheck(stateDbPath, deps = {}) {
const pending = (deps.listPendingStateMigrations ?? listPendingStateMigrations)(stateDbPath);
if (pending.length === 0) {
return {
name: "state-db-migrations",
kind: "deterministic",
status: "pass",
confidence: "high",
message: "state.db has no pending migrations.",
evidence: { path: stateDbPath, pending: [] },
};
}
const range = pending.length > 1 ? `${pending[0]} … ${pending[pending.length - 1]}` : pending[0];
return {
name: "state-db-migrations",
kind: "deterministic",
status: "fail",
confidence: "high",
message: `${pending.length} pending state.db migration(s) (${range}); run \`akm migrate apply\`.`,
evidence: { path: stateDbPath, pending },
};
}
/**
* Hard check: state.db's ledger is current. Every open applies the pending
* migrations (copying the file aside first when one drops schema), so after
* health's own open nothing is pending; this names what that open applied, so
* an upgrade that migrated state.db is visible in the report.
*/
export function appliedStateMigrationsCheck(stateDbPath, report) {
const { applied, backupPath } = report;
const range = applied.length > 1 ? `${applied[0]} … ${applied[applied.length - 1]}` : applied[0];
return {
name: "state-db-migrations",
kind: "deterministic",
status: "pass",
confidence: "high",
message: applied.length === 0
? "state.db has no pending migrations."
: `Applied ${applied.length} pending state.db migration(s) on open (${range})` +
(backupPath ? `; the pre-migration copy is ${backupPath}.` : "."),
evidence: { path: stateDbPath, pending: [], applied, ...(backupPath ? { backupPath } : {}) },
};
}
/**
* Name the dominant `detail.reason` behind `task-fail-rate`'s warning
* when one reason accounts for at least half of the window's agent dispatch
* failures — e.g. "(timeout-dominant: 9/12 agent dispatch failures)" — so an
* operator sees the shape of the failure from data instead of grepping task
* logs. Ties break on reason name for determinism. `undefined` when there are
* no counted failures or no single reason reaches the 50% floor.
*/
export function dominantAgentFailureReason(counts) {
const entries = Object.entries(counts);
const total = entries.reduce((sum, [, count]) => sum + count, 0);
if (total === 0)
return undefined;
const [reason, count] = entries.reduce((best, entry) => entry[1] > best[1] || (entry[1] === best[1] && entry[0] < best[0]) ? entry : best);
return count / total >= 0.5 ? { reason, count, total } : undefined;
}
/** "132 runs of 8 tasks (120 scheduled, 12 manual)": what `task-fail-rate` counted, by what started each run. */
function describeTaskRuns(counts) {
const noun = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
const byTrigger = [
counts.scheduled > 0 ? `${counts.scheduled} scheduled` : "",
counts.manual > 0 ? `${counts.manual} manual` : "",
counts.unrecorded > 0 ? `${counts.unrecorded} with no recorded trigger` : "",
counts.unreadable > 0 ? `${counts.unreadable} unreadable` : "",
].filter(Boolean);
return `${noun(counts.runs, "run")} of ${noun(counts.tasks, "task")}${byTrigger.length > 0 ? ` (${byTrigger.join(", ")})` : ""}`;
}
/**
* The ordered health-check registry. ORDER IS LOAD-BEARING: `akmHealth`
* iterates this array and appends to hardChecks/advisories in sequence, so the
* declaration order below is exactly the emission order (hard checks first in
* their array, advisories in theirs). Each `run` is a verbatim copy of the
* corresponding former inline block.
*/
export const HEALTH_CHECKS = [
{
name: "state-db-schema",
channel: "hard",
run: (ctx) => ({
name: "state-db-schema",
kind: "deterministic",
status: ctx.missingTables.length === 0 ? "pass" : "fail",
confidence: "high",
message: ctx.missingTables.length === 0
? "state.db opened and required tables are present."
: `state.db is missing required tables: ${ctx.missingTables.join(", ")}`,
evidence: { path: ctx.stateDbPath, tables: ctx.tableNames },
}),
},
{
name: "state-db-round-trip",
channel: "hard",
run: (ctx) => ({
name: "state-db-round-trip",
kind: "deterministic",
// A skipped probe proved nothing: it is unknown, not a pass.
...(ctx.probe.skipped
? { status: "unknown", reason: "not-probed" }
: { status: ctx.probe.ok ? "pass" : "fail" }),
confidence: "high",
message: ctx.probe.skipped
? "state.db append/read round-trip skipped: this run is read-only."
: ctx.probe.ok
? "state.db append/read round-trip succeeded."
: `state.db round-trip failed: ${ctx.probe.error}`,
evidence: { path: ctx.stateDbPath, durationMs: ctx.probe.durationMs },
}),
},
{
// R0: nothing looked at state.db's own SQLite-level integrity before
// this — the round-trip probe above only proves one row can be appended
// and read back, which stays true on a database that fails
// `PRAGMA integrity_check` elsewhere (corrupt indexes, out-of-order rowids).
// Also reports the freelist ratio (fraction of pages VACUUM could
// reclaim) so a bloated-but-uncorrupted file is visible as a warning
// rather than silence.
name: "state-db-integrity",
channel: "hard",
run: (ctx) => {
const { ok, lines, error } = ctx.stateDbIntegrity;
const { ratio: freelistRatio, error: freelistError } = ctx.stateDbFreelist;
if (!ok) {
const detail = error ?? lines.join("; ");
return {
name: "state-db-integrity",
kind: "deterministic",
status: "fail",
confidence: "high",
message: `state.db failed PRAGMA integrity_check: ${detail}. Repair: back up state.db, then run ` +
`sqlite3 -readonly state.db ".recover" | sqlite3 state.new.db, verify state.new.db passes integrity_check, stop ` +
"every akm process, then delete state.db-wal and state.db-shm before swapping state.new.db in as " +
"state.db — a leftover WAL from the OLD database is replayed onto the new one and corrupts it.",
evidence: { path: ctx.stateDbPath, lines, freelistRatio },
};
}
if (freelistError) {
return {
name: "state-db-integrity",
kind: "deterministic",
status: "fail",
confidence: "high",
message: `state.db passed PRAGMA integrity_check, but reading its freelist/page-count failed: ${freelistError}.`,
evidence: { path: ctx.stateDbPath, lines, freelistError },
};
}
const freelistWarn = freelistRatio > STATE_DB_FREELIST_WARN_RATIO;
return {
name: "state-db-integrity",
kind: "deterministic",
status: freelistWarn ? "warn" : "pass",
confidence: "high",
message: freelistWarn
? `state.db passed PRAGMA integrity_check, but ${(freelistRatio * 100).toFixed(1)}% of its pages are free (reclaimable by VACUUM).`
: "state.db passed PRAGMA integrity_check.",
evidence: {
path: ctx.stateDbPath,
freelistCount: ctx.stateDbFreelist.freelistCount,
pageCount: ctx.stateDbFreelist.pageCount,
freelistRatio,
},
};
},
},
{
name: "state-db-migrations",
channel: "hard",
run: (ctx) => appliedStateMigrationsCheck(ctx.stateDbPath, ctx.stateDbMigrations),
},
{
name: "task-history-read",
channel: "hard",
run: (ctx) => ({
name: "task-history-read",
kind: "deterministic",
status: "pass",
confidence: "high",
message: `Read ${ctx.taskRuns.runs} task-history row(s) since ${ctx.since}.`,
evidence: { rows: ctx.taskRuns.runs, since: ctx.since },
}),
},
{
// Whether an active run's akm is still there is `classifyAttempt`'s answer (owner pid, host, boot), never the
// run's age: a long run is not a stuck one.
name: "active-runs",
channel: "hard",
run: (ctx) => {
const name = (tasks) => [...tasks]
.sort((a, b) => b.ageMs - a.ageMs)
.map((t) => `${t.taskId} (started ${Math.round(t.ageMs / 60000)}m ago)`)
.join(", ");
const base = { name: "active-runs", kind: "deterministic", confidence: "high" };
const evidence = {
stuckActiveRuns: ctx.stuckActiveRuns,
stuckActiveTasks: ctx.stuckActiveTasks,
unverifiedActiveTasks: ctx.unverifiedActiveTasks,
};
if (ctx.stuckActiveRuns > 0) {
const unverified = ctx.unverifiedActiveTasks.length > 0
? ` Cannot tell whether ${name(ctx.unverifiedActiveTasks)} are still running.`
: "";
return {
...base,
status: "warn",
message: `${ctx.stuckActiveRuns} active task run(s) whose akm process is gone: ${name(ctx.stuckActiveTasks)}.${unverified}`,
evidence,
};
}
if (ctx.unverifiedActiveTasks.length > 0) {
return {
...base,
status: "unknown",
reason: "blocked",
message: `Cannot tell whether ${name(ctx.unverifiedActiveTasks)} are still running: the owner is on another host or recorded no liveness evidence.`,
evidence,
};
}
return {
...base,
status: "pass",
message: "Every active task run has a live owner, or there are none.",
evidence,
};
},
},
{
name: "connections-and-harnesses",
channel: "hard",
run: (ctx) => ctx.connectionsAndHarnesses,
},
{
name: "improve-failures",
channel: "advisory",
run: (ctx) => {
const { failedRunIds, interruptedRunIds } = ctx.improveOutcomes;
const interruption = interruptedRunIds.length > 0
? `${interruptedRunIds.length} improve run(s) were interrupted by a signal; cancellation versus failure is unknown.`
: "";
return {
name: "improve-failures",
kind: "deterministic",
...(failedRunIds.length > 0
? { status: "warn" }
: interruptedRunIds.length > 0
? { status: "unknown", reason: "blocked" }
: { status: "pass" }),
confidence: "high",
message: failedRunIds.length > 0 || interruptedRunIds.length > 0
? `${failedRunIds.length} improve run(s) recorded failures since ${ctx.since}.${interruption ? ` ${interruption}` : ""} Inspect \`akm doctor --since ${ctx.since} --group-by run\`.`
: "No improve runs recorded failures or signal interruptions in this window.",
evidence: {
failedRuns: failedRunIds.length,
runIds: failedRunIds,
interruptedRuns: interruptedRunIds.length,
interruptedRunIds,
},
};
},
},
{
name: "index-sources",
channel: "hard",
run: (ctx) => ctx.indexSources,
},
{
name: "bundle-adapters",
channel: "hard",
run: (ctx) => ctx.bundleAdapters,
},
{
name: "semantic-search",
channel: "hard",
run: (ctx) => semanticSearchCheck(ctx.semanticSearch),
},
{
// C2 (13-bus-factor): the cron task-failure rate was computed but never
// surfaced as an advisory, so a sustained 15–16% fail rate stayed
// invisible on `akm health`. Warn at/above TASK_FAIL_RATE_WARN (5%).
//
// Item 6: the aggregate can hide a single consistently-failing task inside
// a large, mostly-healthy population (e.g. 1 flaky task at 100 rows total
// reads as <5% aggregate). Also warn when the single worst task_id (with
// enough rows to be a real signal — see MIN_ROWS_FOR_WORST_TASK_FAIL_RATE)
// crosses the same threshold, naming it explicitly.
name: "task-fail-rate",
channel: "advisory",
run: (ctx) => {
const pctStr = ctx.taskFailRate === null ? "unknown" : `${(ctx.taskFailRate * 100).toFixed(1)}%`;
const thresholdPct = `${(TASK_FAIL_RATE_WARN * 100).toFixed(0)}%`;
const aggregateWarn = ctx.taskFailRate !== null && ctx.taskFailRate >= TASK_FAIL_RATE_WARN;
const worst = ctx.worstTaskFailRate;
const worstWarn = worst !== null && worst.rate >= TASK_FAIL_RATE_WARN;
const warn = aggregateWarn || worstWarn;
// Name the dominant agent dispatch failure reason (timeout,
// non_zero_exit, spawn_failed, …) when the check is already warning, so
// "timeout-dominant" is visible without grepping task logs.
const agentFailureReasonCounts = ctx.agentFailureReasonCounts ?? {};
const dominant = warn ? dominantAgentFailureReason(agentFailureReasonCounts) : undefined;
const dominantSuffix = dominant
? ` (${dominant.reason}-dominant: ${dominant.count}/${dominant.total} agent dispatch failures)`
: "";
const counted = describeTaskRuns(ctx.taskRuns);
let message;
if (ctx.taskRuns.runs === 0) {
message = `No task runs since ${ctx.since} — no task-fail-rate signal.`;
}
else if (warn) {
const parts = [];
if (aggregateWarn) {
parts.push(`aggregate ${pctStr} across ${counted} since ${ctx.since} ≥ ${thresholdPct}`);
}
if (worstWarn && worst) {
const worstPctStr = `${(worst.rate * 100).toFixed(1)}%`;
parts.push(`task "${worst.taskId}" fails ${worstPctStr} of its ${worst.rows} run(s) ≥ ${thresholdPct}`);
}
message = `Task fail rate warning: ${parts.join("; ")}${dominantSuffix} — inspect failed runs (ok=false) for early-exit/harness errors.`;
}
else {
message = `Task fail rate ${pctStr} across ${counted} since ${ctx.since} (below ${thresholdPct} threshold).`;
}
return {
name: "task-fail-rate",
kind: "deterministic",
// No run in the window is no signal, not a pass.
...(ctx.taskRuns.runs === 0
? { status: "unknown", reason: "idle" }
: { status: warn ? "warn" : "pass" }),
confidence: "high",
message,
evidence: {
taskFailRate: ctx.taskFailRate,
taskRuns: ctx.taskRuns,
threshold: TASK_FAIL_RATE_WARN,
worstTaskFailRate: worst,
// Always present (even pass/empty) so scripts reading evidence
// never need to branch on check status to find the breakdown.
agentFailureReasonCounts,
},
};
},
},
{
// #914: derived from the `extract_sessions_seen` LEDGER, not
// `improve_runs.result_json` — the hook-driven `akm proposal extract
// --session-id ...` (the standard Claude Code plugin setup) never writes
// an `improve_runs` row, so the old improve_runs-only source reported
// "not active" as `pass` unconditionally on a plugin-driven machine, no
// matter how healthy or broken extraction actually was.
name: "session-extraction",
channel: "advisory",
run: (ctx) => {
const { since: ledgerSince, rows } = ctx.sessionExtractionLedger;
const days = SESSION_EXTRACTION_LEDGER_WINDOW_DAYS;
const base = {
name: "session-extraction",
kind: "heuristic",
confidence: "medium",
evidence: { ledgerSince, ledgerRows: rows },
};
const totalRows = rows.reduce((sum, row) => sum + row.count, 0);
if (totalRows === 0) {
// Extraction is off when the strategy does not enable it; with the config unreadable that is not known.
const reason = !ctx.proposalQueue || ctx.proposalQueue.strategyError
? "blocked"
: ctx.proposalQueue.extractEnabled
? "idle"
: "off";
return { ...base, status: "unknown", reason, message: `No extraction recorded in the last ${days} days.` };
}
// `read_failed` / `exception` land in the ledger as `failed`, not `skipped`.
const nothingHarvested = rows.every((row) => ["skipped", "failed", "proposal_write_failed"].includes(row.outcome));
const reasonFor = (row) => row.outcome === "proposal_write_failed" ? row.outcome : row.skipReason;
const knownSkipReasons = [...new Set(rows.map(reasonFor).filter((reason) => reason !== null))].sort();
const allKnownReasonsAreInfra = knownSkipReasons.every((reason) => reason === "proposal_write_failed" ||
EXTRACT_INFRASTRUCTURE_SKIP_REASONS.includes(reason));
if (nothingHarvested && allKnownReasonsAreInfra) {
const reasonClauses = knownSkipReasons.map((reason) => {
const engines = [
...new Set(rows.flatMap((row) => (reasonFor(row) === reason && row.engine ? [row.engine] : []))),
].sort();
return engines.length > 0 ? `${reason} (engine ${engines.map((e) => `"${e}"`).join(", ")})` : reason;
});
if (rows.some((row) => reasonFor(row) === null))
reasonClauses.push("unknown reason");
const verb = rows.some((row) => row.outcome === "failed" || row.outcome === "proposal_write_failed")
? "skipped or failed"
: "skipped";
return {
...base,
status: "warn",
message: `${totalRows} of ${totalRows} sessions in the last ${days} days ${verb}: ${reasonClauses.join(", ")}.`,
};
}
// #1290: some sessions harvested while others had candidate writes fail. A failed write that is not a validator
// refusal keeps the session's hash null, so it is retried; a row stops counting once the session is extracted
// again, and a refusal (hash recorded) is no infrastructure failure. A session that queued some candidates and
// failed to write others is recorded as `candidates_queued`.
const unresolvedWrites = rows
.filter((row) => row.retryable && ["proposal_write_failed", "candidates_queued"].includes(row.outcome))
.reduce((sum, row) => sum + row.count, 0);
if (unresolvedWrites > 0) {
return {
...base,
evidence: { ...base.evidence, unresolvedWriteFailures: unresolvedWrites },
status: "warn",
message: `${unresolvedWrites} of ${totalRows} sessions in the last ${days} days had candidate writes fail (proposal_write_failed) and are not yet extracted again.`,
};
}
const outcomeCounts = new Map();
for (const row of rows)
outcomeCounts.set(row.outcome, (outcomeCounts.get(row.outcome) ?? 0) + row.count);
const outcomeSummary = [...outcomeCounts.entries()]
.sort(([a], [b]) => a.localeCompare(b))
.map(([outcome, count]) => `${outcome}: ${count}`)
.join(", ");
return {
...base,
status: "pass",
message: `Session extraction active in the last ${days} days (${outcomeSummary}).`,
};
},
},
{
// #1289: the same resolver extract calls before it spends model work. Only for learning that is on: the
// strategy enables extract, or the ledger shows extraction ran (a hook-driven machine has no strategy).
name: "proposal-queue",
channel: "advisory",
run: (ctx) => {
const base = { name: "proposal-queue", kind: "deterministic", confidence: "high" };
if (!ctx.proposalQueue) {
return {
...base,
status: "unknown",
reason: "blocked",
message: "The primary stash or config could not be read; the proposal queue owner was not checked.",
};
}
const { stashDir, learningOn, strategy, strategyError, extractEnabled, problem } = ctx.proposalQueue;
const ranRecently = ctx.sessionExtractionLedger.rows.some((row) => row.count > 0);
if (!learningOn) {
return {
...base,
status: "unknown",
reason: "off",
message: "Learning is off, so the proposal queue owner was not checked.",
};
}
if (strategyError && !ranRecently) {
return {
...base,
status: "unknown",
reason: "blocked",
message: `The configured improve strategy does not resolve, so the proposal queue owner was not checked: ${strategyError}`,
};
}
if (!extractEnabled && !ranRecently) {
return {
...base,
status: "unknown",
reason: "off",
message: `Session extraction is not enabled in improve strategy "${strategy}", so the proposal queue owner was not checked.`,
};
}
if (!problem)
return { ...base, status: "pass", message: `The proposal queue of ${stashDir} has a writable owner.` };
return {
...base,
status: "warn",
message: `Extraction cannot queue proposals: ${problem} Register ${stashDir} as a writable bundle (\`akm bundle add\`), or mark its owner writable.`,
evidence: { stashDir, problem },
};
},
},
{
name: "auto-accept-validation",
channel: "advisory",
run: (ctx) => {
const aa = ctx.autoAccept;
const base = {
name: "auto-accept-validation",
kind: "heuristic",
evidence: { promoted: aa.promoted, validationFailed: aa.validationFailed },
};
if (aa.validationFailed > 0) {
return {
...base,
status: "warn",
confidence: "high",
message: `${aa.validationFailed} auto-accept validation attempt(s) failed after passing the confidence threshold (truncated description, invalid frontmatter, etc.) — the affected proposals remain pending for manual review.`,
};
}
if (aa.promoted > 0) {
return {
...base,
status: "pass",
confidence: "high",
message: `Auto-accept healthy: ${aa.promoted} proposal(s) promoted, 0 validation failures.`,
};
}
return {
...base,
status: "unknown",
reason: "idle",
confidence: "low",
message: "Auto-accept did not promote or reject any proposal in this window (disabled, or none above the threshold).",
};
},
},
{
// #950: best-effort "installed vs latest release" advisory, gated behind
// the same --probe/--no-probe flag as endpoint reachability. Computed once
// in health.ts (network IO), projected here like connectionsAndHarnesses.
name: "cli-version",
channel: "advisory",
run: (ctx) => ctx.versionDrift,
},
{
// #953: registered last — order is load-bearing (see the HEALTH_CHECKS
// doc comment above). Best-effort scheduler-binary-drift advisory, gated
// behind the same --probe/--no-probe flag as endpoint reachability and
// cli-version. Computed once in health.ts (process-spawn IO), projected
// here like versionDrift/connectionsAndHarnesses.
name: "scheduler-binary",
channel: "advisory",
run: (ctx) => ctx.schedulerBinaryDrift,
},
];