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

121 lines (120 loc) • 6.75 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/. /** * The last seven days against the eight weeks before them (three tiles), the four-week improve * line, and what needs attention. Every figure is derived from the same stores `akm metrics` and `akm health` read, and a * range is only claimed when there is enough data to have a usual. */ import { metricsWindow, readLlmRows, readUsageRows } from "../commands/metrics/metrics-cli.js"; import { proposalWaitingSince } from "../commands/proposal/proposal-types.js"; import { stateBundleId } from "../core/bundle-at-dir.js"; import { resolveStashDir } from "../core/common.js"; import { defaultConnection, loadConfig } from "../core/config/config.js"; import { learningStatus } from "../setup/learning.js"; import { queryImproveRuns } from "../storage/repositories/improve-runs-repository.js"; import { listStateProposals } from "../storage/repositories/proposals-repository.js"; import { queryTaskHistory } from "../storage/repositories/task-history-repository.js"; import { againstUsual, classifyOutcome, MIN_USUAL_WEEKS } from "./derive.js"; import { isUserSearch, missesFromRows } from "./misses.js"; import { stateDbUnavailable, withReadonlyStateDb } from "./readers.js"; import { nearestRank } from "./stats.js"; const DAY = 86_400_000; const iso = (ms) => new Date(ms).toISOString(); export function overviewPulse() { const now = Date.now(); const weekAgo = iso(now - 7 * DAY); const baselineStart = iso(now - 63 * DAY); const window = metricsWindow("70d", now); const state = withReadonlyStateDb((db) => ({ usage: readUsageRows(db, window), llm: readLlmRows(db, window), runs: queryImproveRuns(db, weekAgo), tasks: queryTaskHistory(db, { since: weekAgo }), proposals: listStateProposals(db, { bundleId: stateBundleId(resolveStashDir()), withContent: false }), }), null); if (!state) return stateDbUnavailable(); const rows = state.usage; // Found nothing: this week's share against the eight weeks before. const searches = rows.filter(isUserSearch); const current = searches.filter((row) => row.at >= weekAgo); const baseline = searches.filter((row) => row.at >= baselineStart && row.at < weekAgo); const miss = (list) => list.filter((row) => row.resultCount === 0).length; const missedNow = miss(current); const foundNothing = { x: missedNow, n: current.length, rate: current.length ? missedNow / current.length : null, usual: againstUsual(missedNow, current.length, miss(baseline), baseline.length), }; // Negative feedback: a count this week against the weekly counts before it. const negative = rows.filter((row) => row.eventType === "feedback" && row.signal === "negative"); const negativeNow = negative.filter((row) => row.at >= weekAgo); const perWeek = Array.from({ length: 8 }, (_, i) => { const from = iso(now - 63 * DAY + i * 7 * DAY); const to = iso(now - 63 * DAY + (i + 1) * 7 * DAY); return negative.filter((row) => row.at >= from && row.at < to).length; }); const weeksWithData = perWeek.filter((count) => count > 0).length; const sortedWeeks = [...perWeek].sort((a, b) => a - b); const latest = negativeNow.sort((a, b) => b.at.localeCompare(a.at))[0]; const negativeFeedback = { count: negativeNow.length, assets: new Set(negativeNow.map((row) => row.ref).filter(Boolean)).size, usual: weeksWithData >= MIN_USUAL_WEEKS ? { low: nearestRank(sortedWeeks, 0.1), high: nearestRank(sortedWeeks, 0.9), state: "ok" } : { low: null, high: null, state: "low-data" }, latest: latest ? { ref: latest.ref ?? null, at: latest.at } : null, }; const failedTasks = state.tasks.filter((row) => row.status === "failed"); const runs = { improve: { ok: state.runs.filter((run) => run.ok === 1).length, total: state.runs.length }, tasks: { ok: state.tasks.filter((row) => row.status === "completed").length, total: state.tasks.length }, failedTasks: failedTasks .sort((a, b) => b.started_at.localeCompare(a.started_at)) .slice(0, 3) .map((row) => ({ taskId: row.task_id, at: row.started_at })), }; // The improve line: what happened to the proposals made in the last four weeks. const fourWeeks = iso(now - 28 * DAY); const recent = state.proposals.filter((p) => p.createdAt >= fourWeeks); const classes = recent.map((p) => ({ p, outcome: classifyOutcome(p) })); const llmCalls = state.llm .filter((row) => row.day >= fourWeeks.slice(0, 10)) .reduce((sum, row) => sum + row.calls, 0); const improveLine = { weeks: 4, proposed: recent.length, // A revert writes a rejected review, so count by outcome class: a reverted proposal was accepted first. accepted: classes.filter((c) => ["judge", "you", "unknown", "reverted"].includes(c.outcome)).length, acceptedThenReverted: classes.filter((c) => c.outcome === "reverted").length, rejected: classes.filter((c) => c.outcome === "rejected").length, rejectedByYou: classes.filter((c) => c.outcome === "rejected" && c.p.review?.decidedBy === "human").length, waiting: classes.filter((c) => c.outcome === "pending").length, calls: llmCalls, }; // Needs attention. const pending = state.proposals.filter((p) => p.status === "pending"); const oldest = pending.map((p) => proposalWaitingSince(p)).sort()[0] ?? null; const repeated = missesFromRows(current).filter((m) => m.repeated); const revertedThisWeek = state.proposals.filter((p) => p.status === "reverted" && p.updatedAt >= weekAgo).length; const failedImprove = state.runs.filter((run) => run.ok !== 1).length; // Improve needs a connection only when learning is on and runs on no harness: Setup reads the same two answers. const config = loadConfig(); const learning = learningStatus(config); const needsConnection = learning.on && learning.harness === null && !defaultConnection(config); const attention = { pending: { count: pending.length, oldest }, misses: { queries: repeated.length }, failedRuns: { improve: failedImprove, tasks: failedTasks.length }, harm: { reverted: revertedThisWeek }, setup: needsConnection ? { needs: "a model connection" } : null, }; return { available: true, tiles: { foundNothing, negativeFeedback, runs }, improveLine, attention, }; }