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

427 lines (426 loc) • 21 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 daily usage report: at most once per surface (`cli`, `desktop`) per UTC day, to akm's privately hosted * reporting service. Each surface sends one `<surface>_daily` event (what is set up, how big the library is, and its * totals for the days counted), then the CLI one event per command that ran (`akm task run`) and Desktop one per page * opened and API route called (`desktop page library/search`), each with its counts. * * What may be sent is decided here and nowhere else. Every prop value is a number. Every event name and prop key is * built from akm's own definitions: its command tree, its declared flags and their closed value lists, which of * them it marks Deprecated, its harness ids, sources, outcome categories, error codes and asset types. A name akm did not define (an asset's input, a custom type or * harness) is counted as `other` or `[unknown]`, or left out; it is never spelled. Nothing is read from an argument * value, a ref, a path or a bundle. The one identifier is the usage store's random instance id, sent as the * session id. The fields are listed in docs/reference/data-and-telemetry.md. Every failure is swallowed; the report * never changes a command's output or exit code. */ import os from "node:os"; import { loadConfig } from "../core/config/config.js"; import { AKM_ERROR_CODES } from "../core/errors.js"; import { getDbPath } from "../core/paths.js"; import { isKnownType, KNOWN_TYPES } from "../core/recognition-util.js"; import { HARNESS_REGISTRY } from "../integrations/harnesses/index.js"; import { DETAIL_VALUES, OUTPUT_FORMATS } from "../output/context.js"; import { openDatabase } from "../storage/database.js"; import { getEntryCountByType } from "../storage/repositories/index-entries-repository.js"; import { pkgVersion } from "../version.js"; import { flushDesktopUse, readDesktopUse } from "./desktop-usage.js"; import { openUsage, readUsage } from "./usage.js"; import { CLI_USAGE_SOURCES, declaredFlags, deprecatedUse, EXIT_CATEGORIES, isFailureCategory, USAGE_NOTES, } from "./usage-kinds.js"; // The app key is write-only and public by design. The host is fixed: no environment variable or config key can // redirect it. const ENDPOINT = "https://reporting.fwdslsh.dev/api/v0/events"; const APP_KEY = "A-SH-4111202318"; const SEND_TIMEOUT_MS = 1500; const DAY_MS = 86_400_000; // The service refuses a request with more events, and drops an event that has a longer prop key. const BATCH_MAX = 25; const KEY_MAX = 40; const HARNESS_IDS = new Set(HARNESS_REGISTRY.map((harness) => harness.id)); const FAILURE_CATEGORIES = new Set([...Object.values(EXIT_CATEGORIES), "cancelled", "child"]); /** The string flags whose value is named, from akm's own closed lists. Any other value is counted, never named. */ const NAMED_VALUES = { format: OUTPUT_FORMATS, detail: DETAIL_VALUES, type: [...KNOWN_TYPES, "any"], }; /** Add to a count. A key over the service's limit is left out, since it would drop the whole event for it. */ function add(counts, key, n) { if (n && key.length <= KEY_MAX) counts[key] = (counts[key] ?? 0) + n; } /** No test run and no CI run reaches the network: `CI`, `bun test` (`NODE_ENV=test`, `BUN_TEST`). */ function underTestOrCi(env) { return Boolean(env.CI) || env.NODE_ENV === "test" || env.BUN_TEST === "1"; } /** * Whether the report is on, from the effective config (`extends` layers included): off when any layer sets * `usage.report: false` and no nearer layer overrides it. A config that cannot be loaded is "not enabled": akm * cannot confirm the person has not opted out, so nothing is sent. */ export function usageReportEnabled() { try { return loadConfig({ warn: false }).usage?.report !== false; } catch { return false; } } function day(at) { return at.toISOString().slice(0, 10); } function addDays(utcDay, days) { return day(new Date(Date.parse(`${utcDay}T00:00:00Z`) + days * DAY_MS)); } /** * The complete UTC days a report counts: after `through` (the last day the server accepted), else from `started`, up to * yesterday. `end` is the last of them as a number (20261129): the service files an event under the time it was sent. */ function period(through, started, today) { const since = through === undefined ? started : addDays(through, 1); const until = addDays(today, -1); if (since > until) return undefined; const days = (Date.parse(`${until}T00:00:00Z`) - Date.parse(`${since}T00:00:00Z`)) / DAY_MS + 1; return { since, until, days, end: Number(until.replaceAll("-", "")) }; } /** Set a meta row only forward: a day that is not later than the stored one changes nothing. */ const SET_LATER = "INSERT INTO cli_usage_meta(key,value) VALUES (?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value WHERE cli_usage_meta.value < excluded.value"; /** * Take today's attempt for `surface` (`report.<surface>.last_day`): true for exactly one caller per UTC day, across * concurrent processes (one atomic upsert). A store created today claims nothing, so a fresh install or sandbox * reports from its next UTC day. Returns the last day whose counts the server accepted (`report.<surface>.through`; * undefined before the first accepted report), the day collection started, and the install id: the store's * `instance`, a random UUID made with the store and derived from nothing on the machine. */ function claimDay(surface, today) { const db = openUsage(); try { const meta = (key) => db.prepare("SELECT value FROM cli_usage_meta WHERE key=?").get(key)?.value; const started = meta("collectionStart"); if (started === undefined || started.slice(0, 10) >= today) return undefined; const key = `report.${surface}.last_day`; const last = meta(key); // Already tried today (every command after the first): a read only, no write lock on the shared store. if (last !== undefined && last >= today) return undefined; if (!db.prepare(SET_LATER).run(key, today).changes) return undefined; return { through: meta(`report.${surface}.through`), started: started.slice(0, 10), install: meta("instance"), }; } finally { db.close(); } } /** * Keep what this attempt came to, for `akm doctor metrics usage`, and on an accepted report the last day its counts * reach, so the next report starts after it. A failed send leaves that day where it was: the next day's * report counts the days this one missed. */ function record(surface, outcome, through) { const db = openUsage(); try { const set = db.prepare("INSERT INTO cli_usage_meta(key,value) VALUES (?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value"); set.run(`report.${surface}.last_outcome`, outcome.outcome); set.run(`report.${surface}.last_detail`, outcome.detail); if (through !== undefined) db.prepare(SET_LATER).run(`report.${surface}.through`, through); } finally { db.close(); } } /** * A recorded command path as akm defines it: its event name (`akm task run`) and the flags declared along it. A * token the command tree does not hold ends the name with `[unknown]`. */ function definedCommand(root, path) { const flags = new Map(Object.entries(root.args ?? {})); const names = ["akm"]; let node = root; for (const token of path) { const next = Object.hasOwn(node.subCommands ?? {}, token) ? node.subCommands?.[token] : undefined; if (!next) { names.push("[unknown]"); break; } names.push(token); node = next; for (const [name, definition] of Object.entries(node.args ?? {})) flags.set(name, definition); } return { name: names.join(" "), flags }; } /** * The CLI's counts for the UTC days after `through` (the last day the server accepted) up to yesterday, from the * durable aggregates: the totals for the daily event, and the counts of each command as it was typed. Before the * first accepted report they start on the day collection started (`started`), so the install day is counted. * Without the command tree (`root`) no command can be checked against it, so only the totals are counted. */ function cliCounts(root, through, started, today) { const totals = { period_days: 0, commands: 0, failures: 0 }; for (const source of CLI_USAGE_SOURCES) totals[`src_${source}`] = 0; const events = new Map(); const span = period(through, started, today); if (!span) return { totals, events }; totals.period_days = span.days; totals.period_end = span.end; for (const row of readUsage({ since: span.since, until: span.until, aggregatesOnly: true }).aggregates) { const dimensions = row.dimensions; const attempts = Number(row.attempts); const outcomes = Object.entries(row.outcomes).map(([outcome, n]) => { // An outcome is `<category>:<exit code>:<asset type, or "unresolved">`, then `:<error code>` when there was one, // then `:<notes>` joined by `+` (see outcomeKey). const [category = "", , type = "unresolved", code, notes = ""] = outcome.split(":"); return { category, type, code, notes: notes.split("+"), n }; }); // From the outcomes, not the stored `failures` column: rows written by earlier releases counted warnings and // cancellations there. const failures = outcomes.reduce((sum, o) => sum + (isFailureCategory(o.category) ? o.n : 0), 0); const counted = {}; add(counted, `src_${CLI_USAGE_SOURCES.has(dimensions.source ?? "") ? dimensions.source : "unknown"}`, attempts); if (dimensions.harness) add(counted, `h_${HARNESS_IDS.has(dimensions.harness) ? dimensions.harness : "other"}`, attempts); add(totals, "commands", attempts); add(totals, "failures", failures); for (const [key, n] of Object.entries(counted)) add(totals, key, n); if (!root) continue; const typed = dimensions.invokedCommand ?? dimensions.command ?? []; const { name, flags } = definedCommand(root, typed); // A flag given twice in one run is one use of it. const used = new Set(); for (const argument of dimensions.arguments ?? []) { // A flag this command does not declare, named only when akm declares it on another command. const elsewhere = argument.name === "[unknown]" ? argument.spelling?.replace(/^--/, "") : undefined; if (elsewhere && declaredFlags(root).has(elsewhere)) used.add(`uflag_${elsewhere}`); const definition = flags.get(argument.name); if (!argument.explicit || !definition || definition.type === "positional") continue; used.add(`flag_${argument.name}`); const named = definition.type === "enum" ? definition.options : NAMED_VALUES[argument.name]; if (typeof argument.value === "string" && named?.includes(argument.value)) used.add(`flag_${argument.name}_${argument.value}`); } for (const key of used) add(counted, key, attempts); const deprecated = deprecatedUse(root, typed, dimensions.arguments ?? []); if (deprecated.command) add(counted, "deprecated", attempts); for (const flag of deprecated.flags) add(counted, `dflag_${flag}`, attempts); for (const { category, type, code, notes, n } of outcomes) { for (const note of notes) if (USAGE_NOTES.has(note)) add(counted, note, n); if (category !== "success") add(counted, `out_${FAILURE_CATEGORIES.has(category) ? category : "failure"}`, n); if (type !== "unresolved") add(counted, `type_${isKnownType(type) ? type : "other"}`, n); if (code !== undefined && AKM_ERROR_CODES.has(code)) add(counted, `err_${code}`, n); } const props = events.get(name) ?? { period_days: span.days, period_end: span.end, count: 0, failures: 0, duration_ms: 0, }; events.set(name, props); add(props, "count", attempts); add(props, "failures", failures); add(props, "duration_ms", Number(row.duration_ms)); for (const [key, n] of Object.entries(counted)) add(props, key, n); } return { totals, events }; } /** * Desktop's counts for the same days as the CLI's: totals for the daily event, and one event for each of its pages * opened (`desktop page library/search`) and API routes called (`desktop GET search`), as Desktop named them. A name * longer than the service accepts is in the totals only. */ function desktopCounts(through, started, today) { flushDesktopUse(); const totals = { period_days: 0, page_views: 0, api_calls: 0, failures: 0 }; const events = new Map(); const span = period(through, started, today); if (!span) return { totals, events }; totals.period_days = span.days; totals.period_end = span.end; for (const { name, count, failures } of readDesktopUse(span.since, span.until)) { add(totals, name.startsWith("page ") ? "page_views" : "api_calls", count); add(totals, "failures", failures); const eventName = `desktop ${name}`; if (eventName.length <= KEY_MAX) events.set(eventName, { period_days: span.days, period_end: span.end, count, failures }); } return { totals, events }; } /** What is set up, as counts and 0/1 switches from the effective config: never a name, a path or a URL. */ function setupSnapshot() { const config = loadConfig(); const bundles = Object.values(config.bundles ?? {}).filter((bundle) => bundle.enabled !== false); return { bundles: bundles.length, bundles_git: bundles.filter((bundle) => bundle.git !== undefined).length, bundles_website: bundles.filter((bundle) => bundle.website !== undefined).length, bundles_npm: bundles.filter((bundle) => bundle.npm !== undefined).length, registries: config.registries?.length ?? 0, connections: Object.keys(config.connections ?? {}).length, harnesses: Object.keys(config.harnesses ?? {}).length, ...harnessPlatforms(config.harnesses ?? {}), projects: Object.keys(config.projects ?? {}).length, tasks_enabled: config.scheduler?.enabled?.length ?? 0, semantic_search: config.semanticSearchMode === "auto" ? 1 : 0, improve: config.improve?.enabled === false ? 0 : 1, exec: config.experimental?.exec === true ? 1 : 0, }; } /** The configured harnesses by platform (`harness_claude`), a platform akm does not know as `harness_other`. */ function harnessPlatforms(harnesses) { const counts = {}; for (const [name, harness] of Object.entries(harnesses)) { const platform = harness.platform ?? name; add(counts, `harness_${HARNESS_IDS.has(platform) ? platform : "other"}`, 1); } return counts; } /** The indexed asset counts, by akm's own types, from the read-only index. Absent when there is none to read. */ function librarySnapshot() { const props = {}; try { const db = openDatabase(getDbPath(), { readonly: true, create: false }); try { db.exec("PRAGMA busy_timeout=250; PRAGMA query_only=ON;"); const byType = getEntryCountByType(db); props.assets = Object.values(byType).reduce((sum, n) => sum + n, 0); // A type outside akm's own set is named by a bundle's adapter or the user: it is counted, never named. for (const [type, n] of Object.entries(byType)) { const key = isKnownType(type) ? `assets_${type}` : "assets_other"; props[key] = (props[key] ?? 0) + n; } } finally { db.close(); } } catch { // No index, or one that cannot be read: the snapshot omits the asset counts. } return props; } function systemProps(version) { const platform = process.platform; return { isDebug: false, // Only the language, script and region of the locale, and only the numbers of the OS release: a kernel build // can carry a name its builder chose. locale: /^[a-z]{2,3}(-[A-Z][a-z]{3})?(-[A-Z]{2})?/.exec(Intl.DateTimeFormat().resolvedOptions().locale)?.[0] ?? "", osName: platform === "linux" ? "Linux" : platform === "darwin" ? "macOS" : platform === "win32" ? "Windows" : platform, osVersion: /^\d+(\.\d+){0,2}/.exec(os.release())?.[0] ?? "", engineName: process.versions.bun ? "bun" : "node", engineVersion: process.versions.bun ?? process.versions.node, appVersion: version, sdkVersion: `akm@${version}`, }; } /** * POST the events, at most 25 a request, all inside one time limit. The service answers a request it refuses with a * 4xx, and an App-Key it does not know with a 200 whose body carries `error`: both are rejections. A report over 25 * events goes in several requests; when only some arrive the report is not accepted, and the next one sends those * commands' counts again. */ async function send(events, deps) { const timeoutMs = deps.timeoutMs ?? SEND_TIMEOUT_MS; const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), timeoutMs); const post = async (batch) => { try { const response = await (deps.fetch ?? fetch)(ENDPOINT, { method: "POST", headers: { "App-Key": APP_KEY, "Content-Type": "application/json" }, body: JSON.stringify(batch), signal: controller.signal, }); const text = await response.text().catch(() => ""); if (!response.ok) return { outcome: "rejected", detail: `HTTP ${response.status}: ${text}`.slice(0, 300) }; let error; try { error = JSON.parse(text)?.error; } catch { // A 200 with a body that is not JSON carries no error. } return error ? { outcome: "rejected", detail: String(error).slice(0, 300) } : { outcome: "sent", detail: "" }; } catch (error) { const detail = controller.signal.aborted ? `no answer within ${timeoutMs} ms` : error instanceof Error ? error.message : String(error); return { outcome: "failed", detail: detail.slice(0, 300) }; } }; try { const batches = []; for (let at = 0; at < events.length; at += BATCH_MAX) batches.push(events.slice(at, at + BATCH_MAX)); const outcomes = await Promise.all(batches.map(post)); return outcomes.find((outcome) => outcome.outcome !== "sent") ?? { outcome: "sent", detail: "" }; } finally { clearTimeout(timer); } } /** * Send the surface's daily report when it is due. `commands` is the CLI's command tree: the CLI's per-command events * are checked against it, and are not sent without it. Resolves true when the server accepted the report; never * throws. */ export async function reportUsageIfDue({ surface, version = pkgVersion, commands }, deps = {}) { try { const env = deps.env ?? process.env; if (underTestOrCi(env) || !usageReportEnabled()) return false; const now = deps.now ?? new Date(); const today = day(now); const claim = claimDay(surface, today); if (!claim) return false; const counts = surface === "cli" ? cliCounts(commands, claim.through, claim.started, today) : desktopCounts(claim.through, claim.started, today); const system = systemProps(version); const event = (eventName, props) => ({ timestamp: now.toISOString(), sessionId: claim.install, eventName, systemProps: system, props, }); const outcome = await send([ event(`${surface}_daily`, { ...counts.totals, ...setupSnapshot(), ...librarySnapshot() }), ...[...counts.events].map(([name, props]) => event(name, props)), ], deps); record(surface, outcome, outcome.outcome === "sent" ? addDays(today, -1) : undefined); return outcome.outcome === "sent"; } catch { return false; } }