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
388 lines (387 loc) • 17.4 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/.
/** The refs an improve run may consider, read from the index, and the small predicates over them. */
import path from "node:path";
import { makeBundleRef, parseBundleRef } from "../../core/asset/asset-ref.js";
import { conceptIdFromTypeName, parsePlacementRef, resolveRef } from "../../core/asset/resolve-ref.js";
import { findAsset } from "../../core/assets/read.js";
import { bundleAt } from "../../core/bundle-at-dir.js";
import { closestMatch } from "../../core/closest-match.js";
import { loadConfig } from "../../core/config/config.js";
import { ConfigError, NotFoundError, rethrowIfTestIsolationError, UsageError } from "../../core/errors.js";
import { isPathAbsent } from "../../core/path-access.js";
import { getDbPath } from "../../core/paths.js";
import { KNOWN_TYPES } from "../../core/recognition-util.js";
import { warn } from "../../core/warn.js";
import { deriveWritableBundleIds } from "../../indexer/installations.js";
import { findAssetOnDisk, resolveAssetPath } from "../../indexer/path-resolver.js";
import { resolveSourceEntries } from "../../indexer/search/search-source.js";
import { closeDatabase, openExistingDatabase, openReadonlyExistingDatabase, } from "../../storage/repositories/index-connection.js";
import { getAllEntries } from "../../storage/repositories/index-entries-repository.js";
import { hasCurrentEntriesTable } from "../../storage/repositories/index-entry-schema.js";
import { getUtilityScoresByIds } from "../../storage/repositories/index-utility-repository.js";
import { SqliteReadSnapshotUnavailableError } from "../../storage/sqlite-read-snapshot.js";
import { isDistillRefusedInputType } from "./distill.js";
import { isStrategyFilteredForAllPasses } from "./improve-strategies.js";
import { indexedAsset, plannedAsset } from "./indexed-asset.js";
import { parseMemoryRef } from "./memory/derived-ref.js";
/**
* Run `fn` against index.db — the live index, or a non-mutating snapshot for
* a dry run. `undefined` when there is none or the read fails (best-effort).
*/
export function withIndexDb(readOnly, fn) {
let db;
try {
db = readOnly ? openReadonlyExistingDatabase(undefined, { isolatedSnapshot: true }) : openExistingDatabase();
return db ? fn(db) : undefined;
}
catch (err) {
rethrowIfTestIsolationError(err);
return undefined;
}
finally {
if (db)
closeDatabase(db);
}
}
/**
* The index for candidate selection, or `undefined` when it is absent. An
* index without a readable `entries` table needs a rebuild and says so.
*/
function openEligibilityDb(readOnly) {
const db = readOnly
? openReadonlyExistingDatabase(undefined, { isolatedSnapshot: true })
: isPathAbsent(getDbPath())
? undefined
: openExistingDatabase();
if (db && !hasCurrentEntriesTable(db)) {
closeDatabase(db);
throw new ConfigError("index.db has no entries table this akm can read, so nothing can be selected for improvement.", "INDEX_SCHEMA_INCOMPATIBLE", "Run `akm index` to rebuild the derived index from the currently materialized sources.");
}
return db;
}
/**
* How a dry run describes an index it could not read. Only the typed
* failures map to an empty plan; any other error still throws.
*/
function unreadableSnapshot(readOnly, error) {
if (!readOnly)
return undefined;
if (error instanceof SqliteReadSnapshotUnavailableError) {
return {
status: "incompatible",
reason: `index.db cannot provide a stable non-mutating snapshot (${error.message}); dry-run uses an empty snapshot`,
};
}
if (error instanceof ConfigError && error.code === "INDEX_SCHEMA_INCOMPATIBLE") {
return {
status: "incompatible",
reason: `index.db is incompatible; ${error.hint() ?? error.message} Dry-run uses an empty snapshot and does not migrate it.`,
};
}
return undefined;
}
export function resolveImproveScope(scope, isIndexedRef) {
const trimmed = scope?.trim();
if (!trimmed)
return { mode: "all" };
try {
parsePlacementRef(trimmed);
return { mode: "ref", value: trimmed };
}
catch (err) {
// A bare word is a type scope (an unknown type matches
// nothing), unless it is no type and an indexed asset has it as its ref (a
// file at the bundle root): then it names that asset, the way a missed ref
// is retried. A ref-shaped value that fails to parse is a real error.
if (!/[/:#]/.test(trimmed)) {
const asRef = !KNOWN_TYPES.includes(trimmed) && isIndexedRef?.(trimmed) === true;
return { mode: asRef ? "ref" : "type", value: trimmed };
}
const message = err instanceof Error ? err.message : String(err);
throw new UsageError(`Invalid scope "${trimmed}": ${message}`, "INVALID_FLAG_VALUE");
}
}
/** Whether `word` is an indexed asset's ref and no indexed asset's type: a scope naming a file at the bundle root. */
export function namesIndexedRootAsset(word) {
let db;
try {
db = openReadonlyExistingDatabase();
if (!db)
return false;
const entries = getAllEntries(db);
return entries.some((e) => e.conceptId === word) && !entries.some((e) => e.entry.type === word);
}
catch (err) {
rethrowIfTestIsolationError(err);
return false;
}
finally {
if (db)
closeDatabase(db);
}
}
/** Dedupe by durable identity (item_ref, else ref), keeping the first. */
export function dedupeRefs(refs) {
const seen = new Set();
return refs.filter((r) => {
const key = r.itemRef ?? r.ref;
if (seen.has(key))
return false;
seen.add(key);
return true;
});
}
/**
* The bundle an improve run reads candidates from and files proposals into: its
* write target, the source rooted at `stashDir` (else the working bundle).
* Reflect reads a candidate from its owning bundle but the proposal lands in
* the write target, so a candidate owned by any other bundle would be read from
* one bundle and filed in another (#1000).
*/
function runBundleIdOf(sources, stashDir) {
const root = stashDir === undefined ? undefined : path.resolve(stashDir);
return sources.find((source) => root === undefined ? source.isDefault === true : source.path !== undefined && path.resolve(source.path) === root)?.bundleId;
}
export async function collectEligibleRefs(scope, stashDir, improveProfile, config) {
return collectEligibleRefsFromIndex(scope, stashDir, improveProfile, false, config);
}
/** Dry-run selector: query an existing index without creating or mutating it. */
export async function collectEligibleRefsReadOnly(scope, stashDir, improveProfile, config) {
return collectEligibleRefsFromIndex(scope, stashDir, improveProfile, true, config);
}
async function collectEligibleRefsFromIndex(scope, stashDir, improveProfile, readOnly, configOverride) {
const empty = (indexSnapshot) => ({
plannedRefs: [],
memorySummary: { eligible: 0, derived: 0 },
strategyFilteredRefs: [],
...(indexSnapshot ? { indexSnapshot } : {}),
});
const config = configOverride ?? loadConfig();
let sources;
try {
sources = resolveSourceEntries(stashDir, config);
}
catch {
return empty();
}
if (sources.length === 0)
return empty();
const writableBundleIds = deriveWritableBundleIds(sources);
const runBundleId = runBundleIdOf(sources, stashDir);
// Candidates come from the write target alone, and only while it is writable.
const isCandidateBundle = (bundleId) => bundleId !== undefined && bundleId === runBundleId && writableBundleIds.has(bundleId);
const ready = {
status: "ready",
reason: readOnly ? "loaded a non-mutating point-in-time copy of the existing index" : "loaded the prepared index",
};
const missing = {
status: "missing",
reason: readOnly
? "index.db is missing; dry-run uses an empty snapshot and does not create it"
: "index.db is missing after index preparation; the selector uses an empty snapshot",
};
let db;
try {
db = openEligibilityDb(readOnly);
if (!db)
return empty(missing);
if (scope.mode === "ref" && scope.value) {
const entriesByItemRef = new Map(getAllEntries(db).map((entry) => [entry.itemRef, entry]));
let resolved;
try {
resolved = resolveRef(scope.value, {
defaultBundle: config.defaultBundle,
bundles: sources.map(({ bundleId }) => ({
id: bundleId,
hasConcept: (conceptId) => entriesByItemRef.has(`${bundleId}//${conceptId}`),
})),
...(runBundleId ? { only: runBundleId } : {}),
});
}
catch (error) {
if (!(error instanceof NotFoundError) || !runBundleId)
throw error;
// Name the bundle that owns the ref; a typo or an unindexed ref keeps the default advice.
const { conceptId } = parseBundleRef(scope.value);
const owner = sources.find(({ bundleId }) => bundleId !== runBundleId && entriesByItemRef.has(`${bundleId}//${conceptId}`))?.bundleId;
if (!owner)
throw error;
throw new NotFoundError(error.message, error.code, `"${conceptId}" is in bundle "${owner}"; this run improves bundle "${runBundleId}" only. Run \`akm improve ${owner}//${conceptId}\` (or pass \`--bundle ${owner}\`).`);
}
const indexed = entriesByItemRef.get(`${resolved.bundle}//${resolved.conceptId}`);
if (!indexed?.bundleId || !indexed.conceptId || !indexedAsset(indexed.bundleId, indexed.conceptId)) {
if (findAssetOnDisk(scope.value, { stashDir }))
return empty(ready);
throw new NotFoundError(`Asset not found in the selected writable source: ${scope.value}`, "ASSET_NOT_FOUND");
}
if (!isCandidateBundle(indexed.bundleId))
return empty(ready);
const isMemory = indexed.entry.type === "memory";
return {
plannedRefs: [
{
ref: indexed.conceptId,
type: indexed.entry.type,
itemRef: indexed.itemRef,
reason: "scope-ref",
filePath: indexed.filePath,
},
],
memorySummary: {
eligible: isMemory ? 1 : 0,
derived: isMemory && indexed.entry.name.endsWith(".derived") ? 1 : 0,
},
strategyFilteredRefs: [],
indexSnapshot: ready,
};
}
const scoped = getAllEntries(db, scope.mode === "type" ? scope.value : undefined);
if (scope.mode === "type" && scope.value && scope.value !== "any" && scoped.length === 0) {
warnUnmatchedTypeScope(scope.value, getAllEntries(db));
}
const entries = scoped.filter((indexed) => isCandidateBundle(indexed.bundleId));
const planned = new Map();
const strategyFiltered = new Map();
let memoryEligible = 0;
let memoryDerived = 0;
for (const indexed of entries) {
const ref = indexed.conceptId;
// A ref that does not parse back to itself (a file named `a#b.md`) cannot be addressed.
try {
if (parseBundleRef(ref).conceptId !== ref)
continue;
}
catch (error) {
if (error instanceof UsageError || error instanceof NotFoundError)
continue;
throw error;
}
const isDerived = indexed.entry.name.endsWith(".derived");
// `.derived` memories skip reflect; cleanup inspects them on its own.
if (!isDerived && !planned.has(ref) && !strategyFiltered.has(ref)) {
const candidate = { ref, type: indexed.entry.type, filePath: indexed.filePath, itemRef: indexed.itemRef };
if (improveProfile && isStrategyFilteredForAllPasses(candidate, improveProfile)) {
strategyFiltered.set(ref, { ...candidate, reason: "strategy_filtered_all_passes" });
}
else {
const reason = scope.mode !== "type" && indexed.entry.type === "memory" ? "memory-cleanup" : "scope-type";
planned.set(ref, { ...candidate, reason });
}
}
if (indexed.entry.type === "memory") {
memoryEligible += 1;
if (isDerived)
memoryDerived += 1;
}
}
return {
plannedRefs: [...planned.values()],
memorySummary: { eligible: memoryEligible, derived: memoryDerived },
strategyFilteredRefs: [...strategyFiltered.values()],
indexSnapshot: ready,
};
}
catch (error) {
rethrowIfTestIsolationError(error);
const snapshot = unreadableSnapshot(readOnly, error);
if (snapshot)
return empty(snapshot);
throw error;
}
finally {
if (db)
closeDatabase(db);
}
}
/** A type scope that matches no indexed asset: warn on stderr, naming the nearest real type. */
function warnUnmatchedTypeScope(value, indexed) {
const types = [...new Set([...KNOWN_TYPES, ...indexed.map((entry) => entry.entry.type)])];
const suggestion = closestMatch(value, types, Math.max(1, Math.floor(value.length / 3)));
warn(`Scope "${value}" matches no asset in the stash.${suggestion ? ` Did you mean \`${suggestion}\`?` : ""}`);
}
/** The parent memory a memory-ref scope cleanup is restricted to. */
export function memoryCleanupParentRef(scope, stashDir) {
if (scope.mode !== "ref" || !scope.value)
return undefined;
const parsed = scopePlacement(scope.value);
if (parsed?.type !== "memory")
return undefined;
if (!parsed.name.endsWith(".derived"))
return conceptIdFromTypeName(parsed.type, parsed.name);
const stash = stashDir === undefined ? undefined : bundleAt(stashDir);
const sources = resolveSourceEntries(stashDir);
for (const { bundleId: id } of sources) {
const found = findAsset(makeBundleRef(id, conceptIdFromTypeName("memory", parsed.name)), stash);
if (!found)
continue;
const fm = found.entry.frontmatter() ?? {};
const parent = parseMemoryRef(typeof fm.source === "string" ? fm.source : undefined);
if (parent)
return parent;
}
return conceptIdFromTypeName("memory", parsed.name.slice(0, -".derived".length));
}
/** Should this ref enter the distill queue? Types distill refuses never do. */
export function isDistillCandidateRef(planned, stashDir) {
if (isDistillRefusedInputType(planned.type))
return false;
return shouldDistillMemoryRef(planned, stashDir);
}
/** A non-derived memory that is not marked `quality: proposed`. */
export function shouldDistillMemoryRef(planned, stashDir) {
if (planned.type !== "memory")
return false;
if (planned.filePath && plannedAsset(planned, stashDir)?.entry.frontmatter()?.quality === "proposed")
return false;
return !planned.ref.endsWith(".derived");
}
export function shouldAnalyzeMemoryCleanup(scope, eligibleMemories, primaryStashDir) {
if (!primaryStashDir || eligibleMemories === 0)
return false;
if (scope.mode === "all")
return true;
if (scope.mode === "type")
return scope.value === "memory";
return scope.value ? scopePlacement(scope.value)?.type === "memory" : false;
}
/** The placement a scope ref spells; undefined for a bundle-root ref, which no type folder heads. */
function scopePlacement(value) {
try {
return parsePlacementRef(value);
}
catch {
return undefined;
}
}
export function buildUtilityMap(refs, readOnly = false) {
const map = new Map();
if (refs.length === 0)
return map;
const refSet = new Set(refs.map((r) => r.ref));
withIndexDb(readOnly, (db) => {
const idToRef = new Map();
for (const indexed of getAllEntries(db)) {
const ref = indexed.conceptId;
if (refSet.has(ref))
idToRef.set(indexed.id, ref);
}
if (idToRef.size === 0)
return;
for (const [id, score] of getUtilityScoresByIds(db, [...idToRef.keys()])) {
const ref = idToRef.get(id);
if (ref)
map.set(ref, score.utility);
}
});
return map;
}
export async function findAssetFilePath(ref, stashDir, writableBundleIds) {
return resolveAssetPath(ref, {
stashDir,
mode: "disk-only",
writableBundleIds,
honorOrigin: true,
});
}