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
66 lines (65 loc) • 3.01 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 `stale-path` rule, shared by every lint surface: which absolute paths
* this machine can judge, and what an agent needs to find and correct one.
* Only the current user's home, a configured bundle or project root, and
* `/tmp` are judged; any other path belongs to some other host or user.
*/
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { bundleContentRoots } from "../config/config-sources.js";
import { expandProjectPath } from "../config/schema/projects.js";
import { findCurrentPathReferences } from "./lint-text.js";
/** The roots that make a path judgeable: the user's home, every configured bundle and project, and `extraRoots`. */
export function stalePathRoots(config, ...extraRoots) {
const projectPaths = Object.values(config.projects ?? {}).flatMap((project) => project.paths ?? []);
return [
os.homedir(),
...bundleContentRoots(config).map((bundle) => bundle.contentRoot),
...projectPaths.map(expandProjectPath),
...extraRoots,
];
}
function isUnder(candidate, root) {
return candidate === root || candidate.startsWith(root.endsWith("/") ? root : `${root}/`);
}
function nearestExistingDirectory(candidate) {
let dir = path.dirname(candidate);
// asset-io: host a path an asset's text names
while (dir !== path.dirname(dir) && !fs.statSync(dir, { throwIfNoEntry: false })?.isDirectory()) {
dir = path.dirname(dir);
}
return dir;
}
/**
* The stale paths in `text` (whose first line is file line `firstLine`): paths presented as current
* guidance that do not exist and that this machine can judge. `bundleRoot` is the bundle the text
* belongs to; a path under it also gets its portable `$AKM_BUNDLE_DIR` spelling in the detail.
*/
export function findStalePaths(text, firstLine, roots, bundleRoot) {
const findings = [];
for (const { path: candidate, line, lineText } of findCurrentPathReferences(text, firstLine)) {
const managed = roots.some((root) => isUnder(candidate, root));
const ephemeral = !managed && candidate.startsWith("/tmp/");
if (!managed && !ephemeral)
continue;
// asset-io: host a path an asset's text names
if (fs.existsSync(candidate))
continue;
const portable = isUnder(candidate, bundleRoot)
? ` (portable form: $AKM_BUNDLE_DIR${candidate.slice(bundleRoot.length)})`
: "";
findings.push({
path: candidate,
line,
lineText,
nearestExisting: nearestExistingDirectory(candidate),
reason: ephemeral ? "ephemeral" : "missing",
detail: `${ephemeral ? "ephemeral" : "nonexistent"} path: ${candidate}${portable}`,
});
}
return findings;
}