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

437 lines (436 loc) • 23.8 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/. /** * write-source — resolve a write target and publish writes to it. * * The only module that branches on `source.kind` for writes. It does two * things: * * 1. Resolve the destination: `--bundle` → `defaultWriteTarget` → working * stash (`defaultBundle`). `writable` defaults to true on `filesystem` and * `location`, and false on `git`; `website` / `npm` are never writable (the * config loader rejects `writable: true` on them). * 2. Publish: write the file atomically inside the bundle root, and for a * git-backed target commit exactly the operation's paths once at the * boundary ({@link commitWriteTargetBoundary} → `saveGitStash`), pushing * with `--force-with-lease` when the target is writable and has an * upstream. * * Nothing commits per asset (issue #507). Callers write assets through * `writeAssets` (`src/core/assets/write.ts`) and plain files through * {@link writeSourceFile}, then fire {@link commitWriteTargetBoundary} once. An * accepted proposal commits its own paths ({@link commitAcceptedPaths}), on a * filesystem stash that is a git repository too. A process that accepts many * proposals collects them and commits once at its boundary (#1185). */ import fs from "node:fs"; import path from "node:path"; import { lockContentRootFor } from "../integrations/lockfile.js"; import { GitStashPushError, getCachePaths, inspectGitUpstream, isGitBackedStash, parseGitRepoUrl, runGit, saveGitStash, } from "../sources/providers/git.js"; import { listIgnoredExactPaths } from "../sources/providers/git-stash.js"; import { conceptIdFromTypeName } from "./asset/resolve-ref.js"; import { assertLinksLeadInside } from "./assets/files.js"; import { assertAdapterWrites, detectBundleAdapter } from "./assets/gate.js"; import { bundleIdForDir } from "./bundle-id.js"; import { existingFileMode, isWithin, resolveStashDir, writeFileAtomic } from "./common.js"; import { bundleAtDir, loadConfig, resolveActiveConfiguredSources, resolveConfiguredSources } from "./config/config.js"; import { ConfigError, UsageError } from "./errors.js"; import { sanitizeCommitMessage } from "./git-message.js"; import { warn, warnOnce } from "./warn.js"; import { recordWrittenPath } from "./write-provenance.js"; // ── Write-target resolution ───────────────────────────────────────────────── /** `writable` defaults to true on `filesystem` and `location`, and false on every other kind. */ export function resolveWritable(entry) { if (typeof entry.writable === "boolean") return entry.writable; return entry.type === "filesystem" || entry.type === "location"; } /** * Whether a resolved source is write-activated. Only the primary stash and * sources explicitly marked `writable: true` are writable; registry-cached * (installed, read-only) sources are never written in place because * `akm update` overwrites them. */ export function isSourceWriteActivated(source) { return source.writable === true; } /** The two kinds writes are defined for; scheduler state binds only to these. */ export function isWriteCapableSourceKind(kind) { return kind === "filesystem" || kind === "git"; } /** * Throw when `name` is a configured bundle that is disabled. Callers run it * after a name failed to resolve against the active set, so a disabled bundle * is reported as disabled rather than as not configured. */ export function assertBundleNotDisabled(akmConfig, name) { if (resolveConfiguredSources(akmConfig).some((source) => source.name === name)) { throw new UsageError(`Bundle "${name}" is disabled.`, "INVALID_FLAG_VALUE"); } } /** * Resolve the destination for a write: explicit `--bundle`, then * `defaultWriteTarget`, then the working stash (`defaultBundle`). There is no * fallback to the first writable source. */ export function resolveWriteTarget(akmConfig, explicitTarget, options = {}) { const configuredSources = resolveActiveConfiguredSources(akmConfig); const requireWritable = options.requireWritable !== false; const flag = options.flag ?? "--bundle"; if (explicitTarget) { const match = configuredSources.find((s) => s.name === explicitTarget); if (!match) { assertBundleNotDisabled(akmConfig, explicitTarget); throw new UsageError(`${flag} must reference a source name from your config. No source named "${explicitTarget}" is configured. Run \`akm bundle list\` to see available sources.`, "INVALID_FLAG_VALUE"); } if (requireWritable && !resolveWritable({ type: match.type, writable: match.writable })) { throw new ConfigError(`source ${explicitTarget} is not writable`, "INVALID_CONFIG_FILE", `Set \`writable: true\` on the "${explicitTarget}" source in your config, or pass ${flag} to a different source.`); } return adaptConfiguredSource(match); } if (akmConfig.defaultWriteTarget) { const match = configuredSources.find((s) => s.name === akmConfig.defaultWriteTarget); if (!match) { throw new ConfigError(`defaultWriteTarget "${akmConfig.defaultWriteTarget}" does not match any configured source.`, "INVALID_CONFIG_FILE", "Update `defaultWriteTarget` in your config (run `akm config get defaultWriteTarget`) or run `akm bundle list` to see configured sources."); } if (requireWritable && !resolveWritable({ type: match.type, writable: match.writable })) { throw new ConfigError(`defaultWriteTarget "${akmConfig.defaultWriteTarget}" is not writable`, "INVALID_CONFIG_FILE", `Set \`writable: true\` on the "${akmConfig.defaultWriteTarget}" source in your config, or change \`defaultWriteTarget\` to a writable source.`); } return adaptConfiguredSource(match); } return resolveWorkingStashTarget(akmConfig, options); } /** Resolve the implicit working stash without consulting `defaultWriteTarget`. */ export function resolveWorkingStashTarget(akmConfig, options = {}) { const configuredSources = resolveActiveConfiguredSources(akmConfig); const requireWritable = options.requireWritable !== false; if (process.env.AKM_BUNDLE_DIR?.trim()) { const stashDir = resolveStashDir(); const configuredBundleId = bundleAtDir(akmConfig, stashDir); if (configuredBundleId !== undefined && !configuredSources.some((source) => source.name === configuredBundleId)) { throw new ConfigError("The AKM_BUNDLE_DIR source is disabled in config.", "INVALID_CONFIG_FILE"); } const target = writeTargetAt(stashDir, akmConfig); if (requireWritable && !resolveWritable(target.config)) { throw new ConfigError(`Bundle "${target.source.name}" is not writable.`, "INVALID_CONFIG_FILE"); } return { ...target, selector: undefined }; } const defaultBundleSource = akmConfig.defaultBundle ? configuredSources.find((source) => source.name === akmConfig.defaultBundle) : undefined; if (!defaultBundleSource || !akmConfig.defaultBundle) { throw new ConfigError("No default bundle is configured.", "INVALID_CONFIG_FILE"); } const target = adaptConfiguredSource(defaultBundleSource); if (requireWritable && !resolveWritable(target.config)) { throw new ConfigError(`defaultBundle "${akmConfig.defaultBundle}" is not writable`, "INVALID_CONFIG_FILE", `Set \`writable: true\` on the "${akmConfig.defaultBundle}" bundle, or set \`defaultWriteTarget\` to a writable source.`); } return { ...target, selector: undefined }; } /** * The write target of the directory `dir` a command holds (the working stash, a `--dir`, a root a stored record names). * A directory a configured bundle holds is that bundle's target, as {@link resolveWriteTarget} resolves it: its * permission, its kind and its publication are config's, and a disabled one is refused. Any other directory is * a plain filesystem bundle that takes writes, with the adapter its root's probe finds. */ export function writeTargetAt(dir, config = loadConfig()) { const configured = bundleAtDir(config, dir); if (configured !== undefined) return resolveWriteTarget(config, configured, { requireWritable: false }); const adapterId = detectBundleAdapter(dir); const name = bundleIdForDir(dir, new Set(Object.keys(config.bundles ?? {}))); return { source: { kind: "filesystem", name, path: dir, adapterId }, config: { type: "filesystem", name, path: dir, writable: true }, }; } /** * Map a runtime {@link ConfiguredSource} onto a write target. A managed git * bundle's content root comes from the lock (`localRoot`) first — the same * resolver the indexer's read path uses, so a write lands exactly where a read * walks. Before the first lock row it falls back to the cache checkout and its * `content/` convention. */ function adaptConfiguredSource(runtime) { if (runtime.source.type === "location") { // Its adapter reads and writes it; the adapter allowlists decide which writes it takes. const { location } = runtime.source; return { selector: runtime.name, source: { kind: "location", name: runtime.name, ...(runtime.adapterId ? { adapterId: runtime.adapterId } : {}), location, }, config: { type: "location", name: runtime.name, location, ...(runtime.writable !== undefined ? { writable: runtime.writable } : {}), }, }; } if (!isWriteCapableSourceKind(runtime.type)) { throw new ConfigError(`write-source: source "${runtime.name}" has unsupported kind "${runtime.type}" for writes. ` + "Writes are only defined for `filesystem` and `git` sources.", "INVALID_CONFIG_FILE", 'Use `kind: "filesystem"` or `kind: "git"` for writable sources.'); } const kind = runtime.type; const lockRoot = kind === "git" ? lockContentRootFor(runtime.name, runtime.type) : undefined; const repoPath = lockRoot ?? pathFromConfiguredSource(runtime); if (!repoPath) { throw new ConfigError(`Source "${runtime.name}" has no resolvable on-disk path; writes are unsupported for this entry.`, "INVALID_CONFIG_FILE"); } const contentRoot = kind === "git" ? (lockRoot ?? resolveGitContentRoot(repoPath)) : repoPath; const root = path.resolve(contentRoot, runtime.root ?? "."); if (!isWithin(root, contentRoot)) { throw new ConfigError(`Root "${runtime.root}" escapes bundle "${runtime.name}".`, "INVALID_CONFIG_FILE"); } const adapterId = runtime.adapterId ?? detectBundleAdapter(root); const config = { type: runtime.type, name: runtime.name, path: root, ...(runtime.writable !== undefined ? { writable: runtime.writable } : {}), ...(runtime.options ? { options: runtime.options } : {}), }; return { selector: runtime.name, source: { kind, name: runtime.name, path: root, adapterId, ...(kind === "git" ? { repoPath } : {}) }, config, }; } /** Resolve the asset root inside a git checkout while preserving root-layout repos. */ export function resolveGitContentRoot(repoPath) { const contentPath = path.join(repoPath, "content"); // asset-io: bundle-root git checkout layout return fs.existsSync(contentPath) && fs.statSync(contentPath).isDirectory() ? contentPath : repoPath; } function pathFromConfiguredSource(runtime) { const spec = runtime.source; if (spec.type === "filesystem") return spec.path; if (spec.type === "git") { try { return getCachePaths(parseGitRepoUrl(spec.url).canonicalUrl).repoDir; } catch { return undefined; } } return undefined; } /** * The read-only checks a write target must pass: the adapter takes AKM asset writes, and a git target is a * materialized checkout on a branch. Its repository root is resolved from the content root (which may be a * subdirectory) so the boundary commit uses repository-relative paths. Nothing is changed and nothing is fetched. */ function checkWriteTarget(target, kind = {}) { if (!kind.plain) assertAdapterWrites(target.source, kind.assetType); if (target.source.kind !== "git") return { target }; const contentRoot = path.resolve(requireWriteTargetPath(target.source)); const rootResult = runGit(["-C", contentRoot, "rev-parse", "--show-toplevel"]); if (rootResult.status !== 0 || !rootResult.stdout.trim()) { throw new ConfigError(`Writable Git target "${target.source.name}" is not materialized as a Git checkout at ${contentRoot}; refusing to write without a commit boundary.`, "INVALID_CONFIG_FILE", `Run \`akm bundle update ${target.source.name}\` to materialize it, or point the bundle at a writable Git checkout.`); } const repoPath = path.resolve(rootResult.stdout.trim()); const branchResult = runGit(["-C", repoPath, "symbolic-ref", "--quiet", "HEAD"]); if (branchResult.status !== 0 || !branchResult.stdout.trim()) { throw new UsageError(`Writable Git target "${target.source.name}" is detached from a branch.`, "INVALID_FLAG_VALUE"); } return { target, repoPath }; } /** * Whether a write to `explicitTarget` (or, with none, to where a write goes by default) would be accepted, and the * sentence why not when it would not. It runs the checks the write path runs, so it never disagrees with a save. * `name` is the bundle the write resolves to, or null when none resolves. */ export function inspectWriteTarget(akmConfig, explicitTarget) { try { const resolved = resolveWriteTarget(akmConfig, explicitTarget); checkWriteTarget(resolved); return { name: resolved.source.name, blocker: null }; } catch (error) { let name = null; try { name = resolveWriteTarget(akmConfig, explicitTarget, { requireWritable: false }).source.name; } catch { // Nothing resolves: the blocker above says why. } return { name, blocker: error instanceof Error ? error.message : String(error) }; } } /** Validate a write target before a command mutates it: the read-only checks, then an advisory upstream check. */ export function prepareWriteTargetForMutation(target, kind = {}) { const { repoPath } = checkWriteTarget(target, kind); if (repoPath === undefined) return target; const contentRoot = path.resolve(requireWriteTargetPath(target.source)); try { const upstream = inspectGitUpstream(repoPath); if (upstream.behind > 0) { warnOnce(`write-source:git-behind:${repoPath}`, `Writable Git target "${target.source.name}" is ${upstream.behind} commit(s) behind ${upstream.upstream}; writing anyway. Run \`akm bundle update ${target.source.name}\` to catch up.`); } if (upstream.ahead > 0) { warnOnce(`write-source:git-ahead:${repoPath}`, `Writable Git target "${target.source.name}" has ${upstream.ahead} unpushed commit(s); writing another on top. Push or reconcile them when convenient.`); } } catch (error) { // The upstream check is advisory (it fetches). Being offline must not stop // a local write; the push reports its own failure at the boundary. warnOnce(`write-source:git-upstream:${repoPath}`, `Could not check upstream for Git target "${target.source.name}" (${error instanceof Error ? error.message : String(error)}); writing anyway.`); } return { ...target, source: { ...target.source, path: contentRoot, repoPath } }; } // ── Writing assets ────────────────────────────────────────────────────────── /** The gates an asset write passes: {@link assertPlainWritable}, and an adapter that writes assets of `type`. */ export function assertWritableSource(source, config, type) { assertPlainWritable(source, config); assertAdapterWrites(source, type); } /** The gates of a plain file write: the source takes writes and is a kind akm writes, whatever its adapter. */ export function assertPlainWritable(source, config) { ensureWritable(source, config); requireWriteTargetPath(source); } /** * Write a plain text file (a file of no asset type: the Desktop Files view's) into `source` at `filePath` (already resolved * inside it, after {@link assertPlainWritable}), atomically: sibling temp file, fdatasync, rename, so a crash or a full disk * never leaves a half-written file where a good one was. Written as given; the Markdown policy of an asset (normalized * document, trailing line break, home-path advisory) is the adapter's, in `serialize` (`akm-format.ts`). */ export function writeSourceFile(source, filePath, content) { fs.mkdirSync(path.dirname(filePath), { recursive: true }); // asset-io: files-view a plain file, not an asset writeFileAtomic(filePath, content, existingFileMode(filePath)); recordWriteTargetPath(source, filePath); // Run-scoped write provenance (#652). recordWrittenPath(filePath); } /** Refuse a write to a source that is not write-activated. */ export function ensureWritable(source, config) { if (resolveWritable(config)) return; throw new UsageError(`Source "${source.name}" is not writable. Set \`writable: true\` on the source config entry to enable writes.`, "INVALID_FLAG_VALUE"); } /** Require a path for a directory-only operation. */ export function requireWriteTargetPath(source) { if (source.path !== undefined && isWriteCapableSourceKind(source.kind)) return source.path; throw new ConfigError(`write-source: unsupported kind "${source.kind}" for source "${source.name}". ` + "Writes are only defined for `filesystem` and `git` sources.", "INVALID_CONFIG_FILE", 'Set `kind: "filesystem"` (or `kind: "git"`) on the source, or add a parallel filesystem entry.'); } /** `[origin//]conceptId` for a commit subject, each component sanitized against CR/LF/NUL smuggling. */ export function formatRefForMessage(ref) { const origin = ref.origin ? sanitizeCommitMessage(ref.origin) : ""; const conceptId = conceptIdFromTypeName(sanitizeCommitMessage(ref.type), sanitizeCommitMessage(ref.name)); return origin ? `${origin}//${conceptId}` : conceptId; } // ── Boundary commit ───────────────────────────────────────────────────────── /** * Absolute paths written to a git target since its last boundary commit, keyed * by repository root. This is what lets the boundary `git add` exactly the * operation's files and nothing else in the checkout. */ const pendingGitPaths = new Map(); export function repoDirFor(source) { return path.resolve(source.repoPath ?? requireWriteTargetPath(source)); } /** Record a path the git target's next boundary commit must include. */ export function recordWriteTargetPath(source, filePath) { if (source.kind !== "git") return; const repoDir = repoDirFor(source); const pending = pendingGitPaths.get(repoDir) ?? new Set(); pending.add(path.resolve(filePath)); pendingGitPaths.set(repoDir, pending); } /** * Refuse a write path outside the source root. A symbolic link below it must * lead to an existing file or folder inside the bundle; a link leaving the * bundle or leading nowhere is refused. The source root itself may be a symlink. */ export function assertWritePathsInsideSource(source, filePaths) { for (const filePath of filePaths) assertLinksLeadInside(requireWriteTargetPath(source), source.name, filePath); } /** * Commit a git target's recorded paths plus `options.paths` (absolute or * repository-relative) as one commit, and push it with `--force-with-lease` * when the target is writable, has an upstream, and `push !== false`. A no-op * for filesystem targets unless `filesystem` is set. Ignored paths stay local: * they are dropped from the commit with a warning rather than failing a write * that already landed. */ export function commitWriteTargetBoundary(target, message, options) { commitBoundary(target, message, options); } /** {@link commitWriteTargetBoundary}, reporting what the commit came to. */ function commitBoundary(target, message, options) { if (target.source.path === undefined || (target.source.kind !== "git" && !options?.filesystem)) { return { committed: false, reason: "not-git" }; } const repoDir = repoDirFor(target.source); const recorded = pendingGitPaths.get(repoDir) ?? new Set(); pendingGitPaths.delete(repoDir); if (!isGitBackedStash(repoDir)) return { committed: false, reason: "not-git" }; const toRepoRelative = (filePath) => (path.isAbsolute(filePath) ? path.relative(repoDir, filePath) : filePath).replaceAll(path.sep, "/"); const paths = [...new Set([...(options?.paths ?? []), ...recorded].map(toRepoRelative))]; if (paths.length === 0) return { committed: false, reason: "nothing-to-commit" }; const ignored = new Set(listIgnoredExactPaths(repoDir, paths)); if (ignored.size > 0) { warn(`warning: ${ignored.size} path(s) in "${target.source.name}" are ignored by .gitignore and stay local (not committed): ${[...ignored].join(", ")}`); } const committable = paths.filter((filePath) => !ignored.has(filePath)); if (committable.length === 0) return { committed: false, reason: "ignored" }; try { const saved = saveGitStash(undefined, message, resolveWritable(target.config), { repoDir, paths: committable, ...(options?.body ? { body: options.body } : {}), ...(options?.push === undefined ? {} : { push: options.push }), }); if (saved.reason) warn(`warning: "${target.source.name}" ${saved.reason}`); return saved.commit ? { committed: true, sha: saved.commit } : { committed: false, reason: "nothing-to-commit" }; } catch (error) { if (error instanceof GitStashPushError) { const what = error.committedNow ? `Changes were committed as ${error.commit}` : `Nothing new to commit; earlier commits up to ${error.commit} are unpushed`; throw new Error(`${what}, but publication failed: ${error.message}`, { cause: error }); } throw error; } } /** * Commit exactly the paths an accepted proposal wrote or removed, when the * target is a git repository: a filesystem stash with a `.git` too, which the * boundary commit skips, locally (the end-of-run sync pushes). A failed commit * only warns, since the accept already landed. Returns what the commit came to. */ export function commitAcceptedPaths(target, message, paths, body) { try { return commitBoundary(target, message, { paths, ...(body ? { body } : {}), filesystem: true, ...(target.source.kind === "git" ? {} : { push: false }), }); } catch (error) { const detail = error instanceof Error ? error.message : String(error); warn(`warning: could not commit the accept (${message}): ${detail}`); return { committed: false, reason: "failed", error: detail }; } }