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
684 lines (683 loc) • 36.2 kB
JavaScript
import { parseBundleRef } from "../../core/asset/asset-ref.js";
// 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/.
/**
* Knowledge-command helpers extracted from `src/cli.ts`.
*
* Covers the shared pipeline for reading, naming, and writing markdown assets
* (knowledge and memory) from the CLI. Extracted to keep the CLI entry point
* focused on command definitions and routing.
*/
import { createHash } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { parse as yamlParse } from "yaml";
import { normalizeAkmMarkdown } from "../../core/asset/akm-markdown.js";
import { assertFlatAssetName, combineCreatePath, normalizeCreateSubPath } from "../../core/asset/asset-create.js";
import { assetPathForName, stashDirFor } from "../../core/asset/asset-placement.js";
import { assembleAsset } from "../../core/asset/asset-serialize.js";
import { parseFrontmatter } from "../../core/asset/frontmatter.js";
import { conceptIdFromTypeName, displayRef, parsePlacementRef } from "../../core/asset/resolve-ref.js";
import { adapterWrites } from "../../core/assets/gate.js";
import { readAsset } from "../../core/assets/read.js";
import { embedPendingWrites } from "../../core/assets/type.js";
import { addressOf, writeAssets } from "../../core/assets/write.js";
import { bundleAt } from "../../core/bundle-at-dir.js";
import { bundleIdForDir } from "../../core/bundle-id.js";
import { isHttpUrl, isWithin, resolveStashDir, samePath, tryReadStdinText } from "../../core/common.js";
import { loadConfig } from "../../core/config/config.js";
import { UsageError } from "../../core/errors.js";
import { withAssetMutationLease } from "../../core/index-writer-lock.js";
import { refToRelPath, resolveRefPathInStash } from "../../core/lint/base-linter.js";
import { resolveBundleWriteTarget, resolveMutationTarget } from "../../core/mutation-target.js";
import { resolveStashStandards } from "../../core/standards/resolve-stash-standards.js";
import { warn } from "../../core/warn.js";
import { commitWriteTargetBoundary, formatRefForMessage, requireWriteTargetPath, resolveWriteTarget, writeTargetAt, } from "../../core/write-source.js";
import { resolveSourceEntries, sourceAtPath } from "../../indexer/search/search-source.js";
import { storeSecretResolver } from "../../sources/snapshot-fetchers/secret-seam.js";
import { fetchWebsiteMarkdownSnapshot, shouldAllowPrivateWebsiteUrlForTests, } from "../../sources/snapshot-fetchers/website-ingest.js";
import { writeSupersededEdge } from "../improve/memory/memory-belief.js";
const MAX_CAPTURED_ASSET_SLUG_LENGTH = 64;
// ── Asset-name normalisation ─────────────────────────────────────────────────
/**
* Validate and normalise a markdown asset name supplied by the user.
*
* Strips the `.md` extension, rejects empty names, and guards against path
* traversal (`..` segments). The `fallback` is used when `name` is undefined.
*/
export function normalizeMarkdownAssetName(name, fallback) {
const trimmed = (name ?? fallback)
.trim()
.replace(/\\/g, "/")
.replace(/^\/+|\/+$/g, "")
.replace(/\.md$/i, "");
if (!trimmed)
throw new UsageError("Asset name cannot be empty.");
const segments = trimmed.split("/");
if (segments.some((segment) => !segment || segment === "." || segment === "..")) {
throw new UsageError("Asset name must be a relative path without '.' or '..' segments.");
}
return trimmed;
}
// `--path`/`--name` create semantics are shared across all asset-creating
// commands; re-exported here so existing `./knowledge` importers keep working.
export { assertFlatAssetName, combineCreatePath, normalizeCreateSubPath };
function slugifyAssetName(value, fallbackPrefix) {
const slug = value
.toLowerCase()
.replace(/^[#>\-\s]+/, "")
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, MAX_CAPTURED_ASSET_SLUG_LENGTH);
return slug || `${fallbackPrefix}-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
}
/**
* Derive a slug-style asset name from `content` and an optional `preferred`
* hint (e.g. a URL-derived page title or the source filename stem).
*/
export function inferAssetName(content, fallbackPrefix, preferred) {
const firstNonEmptyLine = content
.split(/\r?\n/)
.map((line) => line.trim())
.find((line) => line.length > 0);
const basis = preferred?.trim() || firstNonEmptyLine || fallbackPrefix;
return slugifyAssetName(basis, fallbackPrefix);
}
// ── Content reading ──────────────────────────────────────────────────────────
/**
* Read knowledge content from a local file path or stdin (`"-"`).
*
* Returns the raw text and an optional `preferredName` derived from the
* source filename stem (used as a slug fallback when no `--name` flag was
* supplied).
*/
export function readKnowledgeContent(source) {
if (source === "-") {
const content = tryReadStdinText();
if (!content?.trim()) {
throw new UsageError("No stdin content received. Pipe a document into stdin or pass a file path.");
}
return { content };
}
const resolvedSource = path.resolve(source);
let stat;
try {
// asset-io: user-input source file or directory the user named
stat = fs.statSync(resolvedSource);
}
catch {
throw new UsageError(`Knowledge source not found: "${source}". Pass a readable file path or "-" for stdin.`);
}
if (!stat.isFile()) {
throw new UsageError(`Knowledge source must be a file: "${source}".`);
}
return {
// asset-io: user-input source file or directory the user named
content: fs.readFileSync(resolvedSource, "utf8"),
preferredName: path.basename(resolvedSource, path.extname(resolvedSource)),
};
}
/**
* Read knowledge content from a local path, stdin (`"-"`), or a remote URL.
*
* URLs are fetched via `fetchWebsiteMarkdownSnapshot`; local sources delegate
* to `readKnowledgeContent`.
*/
export async function readKnowledgeInput(source, options) {
if (!isHttpUrl(source))
return readKnowledgeContent(source);
const snapshot = await fetchWebsiteMarkdownSnapshot(source, {
stashDir: options?.stashDir,
resolveSecret: storeSecretResolver.resolveSecret,
allowPrivateHosts: options?.allowPrivateHosts ?? shouldAllowPrivateWebsiteUrlForTests(source),
});
return { content: snapshot.content, preferredName: snapshot.preferredName };
}
/**
* Parse a `--xref` / `--supersedes` value through the new-grammar input parser
* (`parsePlacementRef`, the `[bundle//]conceptId` grammar) so malformed values get a
* structured error instead of a misleading "did not resolve". Bundle qualifiers
* are retained for exact source membership checks.
*/
function parseWriteRef(raw, flag) {
let parsed;
try {
parsed = parsePlacementRef(raw);
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new UsageError(`${flag} "${raw}" is not a valid asset ref: ${message}`, "INVALID_FLAG_VALUE", `Refs use the conceptId form, e.g. ${flag} knowledge/auth-flow.`);
}
// Bundle qualifiers remain explicit so duplicate names resolve in the named
// source.
const conceptId = conceptIdFromTypeName(parsed.type, parsed.name);
const origin = parsed.origin;
return {
ref: origin ? `${origin}//${conceptId}` : conceptId,
type: parsed.type,
name: parsed.name,
...(origin ? { origin } : {}),
};
}
/**
* Trim, parse, and dedupe `--xref` / `--supersedes` flag values, in argv
* order. Parsing comes before deduplication so normalized spellings collapse
* into one canonical entry.
*/
function parseWriteRefs(rawRefs, flag) {
const parsedRefs = [];
for (const raw of rawRefs) {
const trimmed = raw.trim();
if (!trimmed)
continue;
const parsed = parseWriteRef(trimmed, flag);
if (!parsedRefs.some((p) => p.ref === parsed.ref))
parsedRefs.push(parsed);
}
return parsedRefs;
}
function resolveWriteRefRoots(target, targetFlag) {
const cfg = loadConfig();
let writeTarget;
try {
writeTarget = resolveWriteTarget(cfg, target, { flag: targetFlag });
}
catch (error) {
if (!target)
throw error;
try {
writeTarget = resolveBundleWriteTarget(cfg, target);
}
catch {
throw error;
}
}
const stashRoot = requireWriteTargetPath(writeTarget.source);
let workingStash;
try {
workingStash = resolveStashDir();
}
catch {
// No working stash configured — the write target alone.
}
const mutableRoots = [stashRoot];
if (workingStash && !samePath(workingStash, stashRoot))
mutableRoots.push(workingStash);
const namedSources = resolveSourceEntries(undefined, cfg);
const configuredNames = new Set(namedSources.map(({ bundleId }) => bundleId));
const roots = [
...mutableRoots
// asset-io: bundle-root root probe
.filter((p) => fs.existsSync(p))
.map((p) => {
const source = sourceAtPath(namedSources, p);
// The demotion of a superseded asset is an update of knowledge or a memory: its bundle's adapter writes them.
const writesAssets = (() => {
try {
return adapterWrites(bundleAt(p), "memory");
}
catch {
return false;
}
})();
const mutable = samePath(p, stashRoot) ? writesAssets : source?.writable === true && writesAssets;
return {
path: p,
source,
bundleId: source?.bundleId ?? bundleIdForDir(p, configuredNames),
mutable,
};
}),
...namedSources.flatMap((source) => {
const root = source.path;
// asset-io: bundle-root root probe
if (root === undefined || mutableRoots.some((m) => samePath(m, root)) || !fs.existsSync(root))
return [];
return [{ path: root, source, bundleId: source.bundleId, mutable: false }];
}),
];
return { roots };
}
function rootsForWriteRef(parsed, roots) {
if (!parsed.origin)
return roots;
return roots.filter((root) => root.bundleId === parsed.origin);
}
function canonicalWriteRef(parsed, bundleId) {
return `${bundleId}//${conceptIdFromTypeName(parsed.type, parsed.name)}`;
}
/**
* True when write-time validation must FAIL OPEN for this ref type — exactly
* lint's `checkMissingRefs` policy (`if (relPath === null) continue`,
* base-linter.ts): a type the slug resolver cannot map to a path (script refs
* are contract-pinned to return null) is accepted without an existence check
* rather than being unwinnable. Workflow refs never fail open — they resolve
* stash-rooted via {@link locateWriteRefInRoot}.
*/
function isFailOpenRefType(type, name) {
return type !== "workflow" && refToRelPath(type, name) === null;
}
/**
* Resolve a write-time ref to its primary on-disk file within a single stash
* root. Wraps lint's `resolveRefPathInStash` with one addition: workflow
* refs are probed against the ROOT's workflows/ dir first (every recognized
* workflow extension), because `workflowSpec.toAssetPath` inside
* `refToRelPath` probes the CWD — and write validation must not depend on the
* caller's cwd.
*/
function locateWriteRefInRoot(type, name, root) {
if (type === "workflow") {
const typeRoot = path.join(root, stashDirFor("workflow") ?? "workflows");
const candidate = assetPathForName("workflow", typeRoot, name);
// asset-io: deprecated workflow ref probe, removed with workflows
if (fs.existsSync(candidate))
return candidate;
}
const relPath = refToRelPath(type, name);
if (relPath === null)
return null;
return resolveRefPathInStash(relPath, type, name, root);
}
/** Build the shared exit-2 error for refs that resolved in no root. */
function unresolvedRefsError(flag, unresolved) {
const firstName = unresolved[0]?.name ?? "asset";
const firstType = unresolved[0]?.type ?? "knowledge";
return new UsageError(`${flag} ref${unresolved.length > 1 ? "s" : ""} did not resolve in the write target or any configured source: ${unresolved.map((u) => u.ref).join(", ")}`, "INVALID_FLAG_VALUE", `Find the intended asset with \`akm search "${firstName}" --type ${firstType}\`. Refs use the form [bundle//]conceptId (e.g. knowledge/guide.md).`);
}
// ── Cross-references (--xref) ────────────────────────────────────────────────
/**
* Soft cap on xrefs per asset, from the back-linking conventions' "~5" rule.
* Exceeding it warns on stderr but never blocks the write (the cap is a
* heuristic, not a validator bound).
*/
export const XREF_SOFT_CAP = 5;
/**
* Validate `--xref` flag values before ANY write happens.
*
* Each `[bundle//]conceptId` must resolve to a real asset in the write-ref root
* set (write target + working stash + configured sources — see
* {@link resolveWriteRefRoots}; cross-stash provenance refs into read-only
* sources are accepted). An unresolvable ref is input validation of an
* explicitly passed flag — it throws {@link UsageError} (exit 2) naming every
* bad ref, and the caller must invoke this before writing so a failed
* validation leaves the stash untouched. Resolution reuses the lint
* ref-resolver helpers (`refToRelPath` / `resolveRefPathInStash`) — do not
* fork a second resolver — and mirrors lint's fail-open policy: a type the
* resolver cannot map to a path (for example a script concept) is accepted
* without an existence check.
*
* Returns fully-qualified durable refs, deduplicated in argv order. More than
* {@link XREF_SOFT_CAP} refs emits a stderr warning (soft cap) but still
* returns them all.
*/
export function resolveXrefsForWrite(rawXrefs, target, targetFlag) {
const parsedRefs = parseWriteRefs(rawXrefs, "--xref");
if (parsedRefs.length === 0)
return [];
const { roots } = resolveWriteRefRoots(target, targetFlag);
const unresolved = [];
const xrefs = [];
for (const parsed of parsedRefs) {
const candidates = rootsForWriteRef(parsed, roots);
const resolvedRoot = isFailOpenRefType(parsed.type, parsed.name)
? candidates[0]
: candidates.find((root) => locateWriteRefInRoot(parsed.type, parsed.name, root.path) !== null);
if (!resolvedRoot) {
unresolved.push(parsed);
continue;
}
const ref = canonicalWriteRef(parsed, resolvedRoot.bundleId);
if (!xrefs.includes(ref))
xrefs.push(ref);
}
if (unresolved.length > 0) {
throw unresolvedRefsError("--xref", unresolved);
}
if (xrefs.length > XREF_SOFT_CAP) {
warn(`Warning: ${xrefs.length} xrefs exceeds the ~${XREF_SOFT_CAP} soft cap from the back-linking conventions. ` +
"Each xref folds into this asset's search hints, so extras blur its ranking signal. Writing anyway.");
}
return xrefs;
}
/**
* Merge validated xrefs into a markdown document's frontmatter `xrefs:` list.
*
* A document without frontmatter gains a single block; a document WITH
* frontmatter keeps every existing key and gets the refs dedupe-appended to
* its `xrefs:` list — never a nested second block. Runs BEFORE the asset is
* written so write-path indexing sees the final content. Returns `content`
* unchanged when `xrefs` is empty.
*
* The merge round-trips the frontmatter through the YAML parser, so it is
* only safe when the existing block parses as a YAML mapping. Malformed YAML
* would silently fall back to `parseFrontmatter`'s lenient scalar-only
* scanner and re-serializing that lossy result would destroy list/nested
* values (`tags: [a, b]` → `tags: ""`). Rather than corrupt data the caller
* asked to preserve, a block that is not a parseable YAML mapping throws
* {@link UsageError} (exit 2, before any write). The AKM write boundary also
* rejects malformed frontmatter without `--xref` because it cannot safely add
* required type metadata. Known
* cosmetic limitation: YAML comments and anchors in a VALID block do not
* survive the round-trip (values are preserved).
*/
export function mergeXrefsIntoContent(content, xrefs) {
if (xrefs.length === 0)
return content;
const parsed = parseFrontmatter(content);
if (parsed.frontmatter?.trim()) {
if (!isParseableYamlMapping(parsed.frontmatter)) {
throw new UsageError("--xref cannot merge into this document: its frontmatter is not a parseable YAML mapping, and rewriting it would drop the values the parser could not read.", "INVALID_FLAG_VALUE", "Fix the document's frontmatter (e.g. an unterminated quote) and retry.");
}
}
const existingValue = parsed.data.xrefs;
const existing = Array.isArray(existingValue)
? existingValue.filter((v) => typeof v === "string" && v.trim().length > 0)
: typeof existingValue === "string" && existingValue.trim()
? [existingValue.trim()]
: [];
const merged = [...existing];
for (const ref of xrefs) {
if (!merged.includes(ref))
merged.push(ref);
}
return assembleAsset({ ...parsed.data, xrefs: merged }, parsed.content);
}
/**
* True when a raw frontmatter block (the text between the `---` fences)
* parses as a YAML mapping — the precondition for any round-trip rewrite.
* Malformed YAML falls back to `parseFrontmatter`'s lossy lenient scanner
* (scalars only), and re-serializing that result destroys list/nested values
* (`tags: [a, b]` → `tags: ""`), so writers must refuse instead of rewriting.
*/
function isParseableYamlMapping(frontmatter) {
try {
const fmValue = yamlParse(frontmatter);
return fmValue !== null && typeof fmValue === "object" && !Array.isArray(fmValue);
}
catch {
return false;
}
}
/** Resolve any qualified supersedes ref as the mutation target for remember/import. */
export function resolveSupersedesWriteTarget(rawRefs, target, targetFlag) {
const config = loadConfig();
let effectiveTarget = target;
for (const parsed of parseWriteRefs(rawRefs, "--supersedes")) {
if (!parsed.origin)
continue;
const resolved = resolveMutationTarget(config, { type: parsed.type, name: parsed.name, origin: parsed.origin }, effectiveTarget, { flag: targetFlag }).target;
effectiveTarget = resolved.selector ?? resolved.source.name;
}
return effectiveTarget;
}
/**
* Asset types `--supersedes` must refuse to demote: the demotion writes a YAML
* frontmatter block onto the target file, and these types are RAW files whose
* bytes are the value (a secret's entire content is the credential; a task is
* pure YAML that a prepended second document breaks; scripts have arbitrary
* syntax). Prepending frontmatter corrupts them. `akm mv` excludes the same
* types as "not markdown assets" (plus `script`, unresolvable by design).
*/
const SUPERSEDE_REJECTED_TYPES = new Set(["secret", "env", "task", "script"]);
/**
* Validate `--supersedes` flag values before ANY write happens.
*
* The conventions' corrections pattern needs TWO writes: the new correction
* asset (with an xref to what it corrects) and a metadata edit demoting the
* old asset (`beliefState: superseded` + `supersededBy: [<new ref>]`). This
* helper performs the validation half: each ref must resolve to a real asset
* (same resolver + root set as {@link resolveXrefsForWrite}); an unresolvable
* ref throws {@link UsageError} (exit 2) naming every bad ref, so a failed
* validation leaves the stash untouched — no partial correction.
*
* Because the demotion PREPENDS a YAML frontmatter block when the target file
* has none, only markdown assets may be demoted: refs of a raw asset type
* ({@link SUPERSEDE_REJECTED_TYPES}) and refs resolving to any non-`.md` file
* (for example a task YAML file) are rejected with {@link UsageError} BEFORE
* any write — never silently corrupted.
*
* Demotion targets must live under the resolved write target's source path or
* the working stash — honoring the "only operate on writable sources"
* constraint (and never dirtying a non-target source outside its boundary
* commit). A ref that resolves only in another configured source — read-only
* OR writable-but-not-the-target — is returned with `writable: false` and a
* reason (naming the target-flag remedy when the source is writable); the
* caller writes the correction anyway and reports the demotion as not
* applied.
*
* Returns the deduplicated plan in argv order; empty input returns [].
*/
export function resolveSupersedesForWrite(rawRefs, target, targetFlag) {
const parsedRefs = parseWriteRefs(rawRefs, "--supersedes");
if (parsedRefs.length === 0)
return [];
const { roots } = resolveWriteRefRoots(target, targetFlag);
const plan = [];
const unresolved = [];
for (const parsed of parsedRefs) {
const orderedRoots = rootsForWriteRef(parsed, roots);
// Data-corruption gate (SPEC-5): demotion is a frontmatter write; a raw
// asset type must be rejected up front — resolving it and mutating the
// file would prepend a YAML block over its raw bytes.
if (SUPERSEDE_REJECTED_TYPES.has(parsed.type)) {
throw new UsageError(`--supersedes cannot demote ${parsed.ref}: asset type "${parsed.type}" uses raw files, and the demotion writes YAML frontmatter that would corrupt them.`, "INVALID_FLAG_VALUE", "Only markdown assets (e.g. memories/note, knowledge/guide, facts/team/tool-stack) can carry the beliefState/supersededBy demotion. Replace or delete the raw asset instead.");
}
let located = null;
for (const root of orderedRoots) {
const filePath = locateWriteRefInRoot(parsed.type, parsed.name, root.path);
if (filePath !== null) {
located = {
root: root.path,
source: root.source,
bundleId: root.bundleId,
mutable: root.mutable,
filePath,
};
break;
}
}
if (located === null) {
unresolved.push(parsed);
continue;
}
// Belt-and-suspenders for the same corruption class: whatever the type,
// the demotion may only touch a markdown file. Rejects, for example, a task
// YAML file reached through a malformed or stale ref.
if (!located.filePath.toLowerCase().endsWith(".md")) {
throw new UsageError(`--supersedes ${parsed.ref} resolves to a non-markdown file (${located.filePath}) — the demotion writes YAML frontmatter and would corrupt it.`, "INVALID_FLAG_VALUE", "Only markdown assets can carry the beliefState/supersededBy demotion. Replace or delete the file instead.");
}
const { root, source, bundleId, mutable: writable, filePath } = located;
// The eligibility rule is write-target-or-working-stash, NOT source
// writability: mutating a non-target writable source would leave it dirty
// outside any boundary commit. Name the remedy when one exists.
const namedWritableSource = source?.writable === true ? source.registryId : undefined;
const canonicalRef = canonicalWriteRef(parsed, bundleId);
if (plan.some((item) => item.ref === canonicalRef))
continue;
plan.push({
ref: canonicalRef,
filePath,
stashRoot: root,
writable,
...(writable
? {}
: {
reason: namedWritableSource
? `resolves outside the write target and the working stash, in writable source "${namedWritableSource}" at ${root}; ` +
`re-run with ${targetFlag ?? "--bundle"} ${namedWritableSource} to demote it there`
: `resolves outside the write target and the working stash, in a read-only source at ${root}; ` +
"demotion only applies to assets in the write target or the working stash",
}),
});
}
if (unresolved.length > 0) {
throw unresolvedRefsError("--supersedes", unresolved);
}
return plan;
}
export async function writeMarkdownAsset(options) {
if (options.expectedContentHash !== undefined && !/^[0-9a-f]{64}$/.test(options.expectedContentHash)) {
throw new UsageError("The expected content hash must be a lowercase hex SHA-256 digest (64 characters).", "INVALID_FLAG_VALUE");
}
const cfg = loadConfig();
// `--name` is the flat asset name; `--path` is the subdirectory under the
// type root. Combine them into the nested name the path resolver expects.
const subPath = normalizeCreateSubPath(options.path);
const baseName = normalizeMarkdownAssetName(options.name, inferAssetName(options.content, options.fallbackPrefix, options.preferredName));
const normalizedName = combineCreatePath(subPath, baseName);
const resolved = resolveMutationTarget(cfg, { type: options.type, name: normalizedName }, options.target, {
flag: options.targetFlag,
});
const { target } = resolved;
const { source, config } = target;
const stashDir = requireWriteTargetPath(source);
const typeRoot = path.join(stashDir, options.type === "knowledge" ? "knowledge" : "memories");
const assetPath = assetPathForName(options.type, typeRoot, normalizedName);
if (!isWithin(assetPath, typeRoot)) {
throw new UsageError(`Resolved ${options.type} path escapes the stash: "${normalizedName}"`);
}
// A correction cannot supersede ITSELF. Under `--force` the ref resolves to
// the very file this command is about to overwrite, and the demotion would
// immediately mark the fresh correction superseded (plus a self-xref) —
// silently hiding the fix from `--belief current` and capping its rank.
// Input validation: exit 2, before any write, nothing demoted.
for (const item of options.supersedes ?? []) {
if (path.resolve(item.filePath) === path.resolve(assetPath)) {
throw new UsageError(`--supersedes ${item.ref} resolves to the asset being written ("${resolved.displayRef}") — a correction cannot supersede itself.`, "INVALID_FLAG_VALUE", "Write the correction under a different --name, or drop --supersedes when overwriting an asset in place with --force.");
}
}
// The lease covers only what reads or changes authored files: the existence
// and revision checks, the write, the demotions and the boundary commit. The
// commit pushes inside the lease on purpose: a push from a writer that has
// released it would race the next writer's commit on the same branch (the
// conflicting-commit hazard the lease exists for). Indexing is a
// regenerable cache and runs after release.
const written = await withAssetMutationLease("markdown-write", async () => {
// Pre-flight: existence + force semantics. The helper itself overwrites
// unconditionally; the CLI surfaces a friendlier UsageError before any
// disk activity when --force is absent.
if (addressOf(source, conceptIdFromTypeName(options.type, normalizedName)).exists && !options.force) {
throw new UsageError(`${options.type === "knowledge" ? "Knowledge" : "Memory"} "${normalizedName}" already exists. Re-run with --force to overwrite it.`, "RESOURCE_ALREADY_EXISTS");
}
if (options.expectedContentHash !== undefined) {
const existing = readAsset(source, conceptIdFromTypeName(options.type, normalizedName))?.entry.bytes();
if (!existing || createHash("sha256").update(existing).digest("hex") !== options.expectedContentHash) {
throw new UsageError("The document changed or was removed after it was loaded. Reload its source before saving.", "ASSET_CONTENT_CHANGED");
}
// Unchanged when the live bytes are the submission itself (an untouched
// document keeps its bytes, missing metadata included) or exactly what
// the writer would write for it (e.g. the submission only lacks the
// final line break the writer adds).
const unchanged = existing.equals(Buffer.from(options.content, "utf8")) ||
existing.equals(Buffer.from(normalizeAkmMarkdown(options.content, resolved.ref.type), "utf8"));
if (unchanged && !options.supersedes?.length) {
return { ref: resolved.displayRef, path: assetPath, stashDir, unchanged: true };
}
}
// An overwrite rewrites the file the asset is read from (N19), so that is the path to report.
let writtenFile;
const change = writeAssets({ source, config }, { purpose: "markdown-write" }, (w) => {
writtenFile = w.address(conceptIdFromTypeName(resolved.ref.type, resolved.ref.name)).file;
return w.create({ type: resolved.ref.type, name: resolved.ref.name, content: options.content }, { exclusive: !options.force });
});
const { conceptId } = parseBundleRef(change.ref);
const durableRef = displayRef({ conceptId, bundleId: resolved.ref.origin });
const result = { path: writtenFile ?? assetPath, ref: resolved.displayRef };
// SPEC-5 (--supersedes): demote each superseded asset by mutating its
// frontmatter (`beliefState: superseded` + sorted-set-append `supersededBy`;
// every other key and the body are preserved). Ordered BEFORE
// commitWriteTargetBoundary so a git target batches the correction and the
// demoted incumbent into the single boundary commit instead of leaving the
// metadata edit as dirty residue after it.
const superseded = [];
for (const item of options.supersedes ?? []) {
if (!item.writable) {
const reason = item.reason ?? "target is not writable";
warn(`Warning: superseded asset ${item.ref} was NOT demoted (${reason}). ` +
"The correction was written and cites it in xrefs; demote the old asset where it is writable.");
superseded.push({ ref: item.ref, applied: false, reason });
continue;
}
// The demotion round-trips the old file's frontmatter through the YAML
// parser. A malformed block would silently fall back to the lossy lenient
// scanner and re-serializing that result destroys list/nested values —
// skip instead of rewriting, mirroring mergeXrefsIntoContent's
// abort-on-malformed policy (the correction itself still writes).
const oldConceptId = parseBundleRef(item.ref).conceptId;
const oldTarget = samePath(item.stashRoot, stashDir) ? target : writeTargetAt(item.stashRoot);
let oldFrontmatter = null;
try {
const old = readAsset(oldTarget.source, oldConceptId);
if (old)
oldFrontmatter = parseFrontmatter(old.entry.content()).frontmatter;
}
catch {
// Unreadable asset — let writeSupersededEdge surface the real error.
}
if (oldFrontmatter?.trim() && !isParseableYamlMapping(oldFrontmatter)) {
const reason = "its existing frontmatter is not a parseable YAML mapping — rewriting it would drop the values the parser could not read";
warn(`Warning: superseded asset ${item.ref} was NOT demoted (${reason}). ` +
"The correction was written and cites it in xrefs; fix the old asset's frontmatter and re-run the correction with --force.");
superseded.push({ ref: item.ref, applied: false, reason });
continue;
}
// A demotion failure (fs error, concurrent delete, malformed YAML the
// pre-check missed) must NOT abort the correction: the new asset is
// already on disk, and bailing out here would skip the boundary commit and
// the write-path indexing below — leaving the correction unindexed and,
// on a git target, uncommitted (and a re-run hits RESOURCE_ALREADY_EXISTS).
// Degrade to the same applied:false report the non-writable path uses.
try {
writeAssets(oldTarget, { purpose: "markdown-write" }, (w) => writeSupersededEdge(w, oldConceptId, durableRef));
}
catch (error) {
const reason = `demotion failed: ${error instanceof Error ? error.message : String(error)}`;
warn(`Warning: superseded asset ${item.ref} was NOT demoted (${reason}). ` +
"The correction was written and cites it in xrefs; demote the old asset manually or re-run the correction with --force.");
superseded.push({ ref: item.ref, applied: false, reason });
continue;
}
superseded.push({ ref: item.ref, applied: true });
}
// Single batch commit at the write boundary for git
// targets. No-op for filesystem/primary-stash targets.
commitWriteTargetBoundary(target, `Update ${formatRefForMessage(resolved.ref)}`);
return { result, superseded };
});
if (written.unchanged)
return written;
const { result, superseded } = written;
// Write-path indexing: the facade recorded the rows of the new asset and of each demoted one, so they are searchable
// immediately (`--belief current` takes effect without waiting for the next full index); their vectors are made now,
// fail-open, after the lease is released.
await embedPendingWrites();
// Placement hint (stash-organization conventions): CLI writers never receive
// the resolveStashStandards prompt injection LLM flows get, so a type-root
// write into a stash that carries convention/meta facts points the writer at
// the placement conventions. Additive output key, advisory only — parallel
// to search's `tip`. Fail-open: the hint must never break a completed write.
let hint;
if (!subPath && !normalizedName.includes("/")) {
try {
if (resolveStashStandards(stashDir).trim().length > 0) {
// Only point at the canonical organization fact when it actually
// exists — resolveStashStandards fires for ANY convention/meta fact,
// and a dead `akm show` pointer is worse than generic wording.
hint = readAsset(source, "facts/conventions/organization")
? `Wrote to the ${options.type} root. This stash has placement conventions — see \`akm show facts/conventions/organization\`.`
: `Wrote to the ${options.type} root. This stash has placement conventions — see the convention facts under its facts/ directory.`;
}
}
catch {
// Advisory only.
}
}
return {
ref: result.ref,
path: result.path,
stashDir,
...(hint ? { hint } : {}),
...(superseded.length > 0 ? { superseded } : {}),
};
}