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
272 lines (271 loc) • 13.9 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/.
/**
* Writing assets: the one write side of the asset facade. {@link writeAssets} runs a session against a write
* target's bundle under the asset-mutation lease; every change the session makes passes the same gates here, in the
* same order, whichever adapter holds the bundle, and its files are recorded for the target's boundary commit.
*/
import path from "node:path";
import { stashDirFor } from "../asset/asset-placement.js";
import { makeBundleRef, parseBundleRef } from "../asset/asset-ref.js";
import { parseFrontmatter, readFrontmatterDate, replaceFrontmatterLine, spliceFrontmatterLine, } from "../asset/frontmatter.js";
import { parsePlacementRef } from "../asset/resolve-ref.js";
import { localDateStamp, toPosix } from "../common.js";
import { assetNotFound, ConfigError, UsageError } from "../errors.js";
import { withAssetMutationLeaseSync } from "../index-writer-lock.js";
import { warn } from "../warn.js";
import { recordWrittenPath } from "../write-provenance.js";
import { commitWriteTargetBoundary, ensureWritable, isWriteCapableSourceKind, recordWriteTargetPath, } from "../write-source.js";
import { assetOf, readFrom } from "./asset.js";
import { assetBundleOf, bundleFor, bundleNameOf } from "./bundle.js";
import { forgetIndexedAsset, refreshIndexedAsset } from "./index-rows.js";
import { assetRevision } from "./read.js";
import { assetType } from "./type.js";
/**
* The gates every write passes: the bundle takes writes, and it is a kind akm writes (a directory, a git checkout, or a
* store its adapter keeps). A source built by hand of another kind is refused, as the loader would refuse it.
*/
function gate(source, config) {
ensureWritable(source, config);
if (source.location !== undefined || isWriteCapableSourceKind(source.kind))
return;
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.');
}
/** Where a write to `ref` goes, refusing nothing a write would refuse. Only an invalid ref throws. */
function addressIn(held, ref) {
const readIn = (id) => readFrom(held, id);
const { conceptId: written, fragment } = parseBundleRef(ref);
const holding = (found, placement) => {
const { conceptId } = found;
const type = assetType(found);
const typeDir = stashDirFor(type);
const name = typeDir !== undefined && conceptId.startsWith(`${typeDir}/`) ? conceptId.slice(typeDir.length + 1) : conceptId;
return {
conceptId,
type,
name,
exists: true,
...(found.entry.file !== undefined ? { file: found.entry.file } : {}),
// The asset is kept under a key other than the one a create of the ref writes.
...(placement?.key !== undefined && placement.key !== found.entry.key ? { heldElsewhere: true } : {}),
};
};
/** Where a create of the ref goes; undefined for a name that cannot be placed. */
const placementOf = () => {
try {
return held.place(parsePlacementRef(ref));
}
catch {
return undefined;
}
};
const found = fragment === undefined ? readIn(written) : undefined;
// Where a create of the ref would write, for a ref a create can place; the ref of any other asset has none.
if (found)
return holding(found, placementOf());
const placed = parsePlacementRef(ref);
const named = { type: placed.type, name: placed.name, exists: false };
const placement = placementOf();
const shown = placement?.file !== undefined ? { file: placement.file } : {};
// Not a write: the asset is named as written; the file is only the display path the adapter would give it.
if (!held.writes(placed.type))
return { conceptId: written, ...named, ...shown };
// A name that cannot be placed is named as written; a write refuses it where it is made.
const conceptId = placement?.conceptId ?? written;
// A create replaces the asset at the concept it places (`env/default` is placed as the concept `env/.env` holds).
const there = conceptId !== written && fragment === undefined ? readIn(conceptId) : undefined;
if (there)
return holding(there, placement);
// A directory, or any other entry no adapter reads as an asset, where a create writes holds the ref too.
return { conceptId, ...named, exists: placement?.occupied === true, ...shown };
}
/** Where a write to `ref` goes, with the refusals a write makes: a type its adapter does not write, a name it cannot place. */
function gatedAddressIn(held, ref) {
const address = addressIn(held, ref);
if (address.exists)
return address;
const placed = parsePlacementRef(ref);
held.assertWrites(placed.type);
held.place(placed);
return address;
}
/**
* Where a write to `ref` goes in the bundle `bundle`, without a write session. Unlike {@link AssetWriter.address} it
* refuses nothing a write would refuse (a type the bundle's adapter does not write, a name it cannot place): nothing is
* written, so the write refuses where it is made. Only an invalid ref throws.
*/
export function addressOf(bundle, ref) {
return addressIn(bundleFor(bundle), ref);
}
/**
* The file `ref` is kept in (the existing asset's, else the one a create would write) in a bundle that keeps assets as
* files; undefined for a store, which keeps none and is not asked. For checks on where a name lands, never to find it.
*/
export function fileOfRef(bundle, ref) {
const held = bundleFor(bundle);
return held.isStore ? undefined : addressIn(held, ref).file;
}
/**
* The ref of the asset an old stored record named by a bundle-relative path (`changes[].path`), for converters only
* (§8.4). Valid when no file holds the asset any more; undefined when the path is no asset's.
*/
export function legacyRefOf(bundle, relPath) {
const conceptId = bundleFor(bundle).identifyKey(relPath);
return conceptId === null ? undefined : makeBundleRef(bundleNameOf(bundle), conceptId);
}
/**
* The concept id of the asset a file of the directory bundle `bundle` would hold, from the path alone (the adapter's rule);
* undefined when the file is outside the bundle or no asset's. For a caller that holds a file it read and writes the asset.
*/
export function conceptIdAtFile(bundle, file) {
const held = bundleFor(bundle);
if (held.root === undefined)
return undefined;
const key = toPosix(path.relative(held.root, file));
return key === "" || key.startsWith("..") ? undefined : (held.identifyKey(key) ?? undefined);
}
/**
* Markdown with its `updated` set to today by akm, whatever date the content carried (a model's, an older one); a new
* memory with no `observed` also gets today's. Content without a frontmatter block, and anything not Markdown (an env
* file, a script), is returned as it is.
*/
function stampDates(content, file, newMemory = false) {
if (typeof content !== "string" || !file?.toLowerCase().endsWith(".md"))
return content;
const today = localDateStamp(new Date());
const line = `updated: ${today}`;
// Replaced where it sits, so key order and comments are kept; added last when absent.
const stamped = replaceFrontmatterLine(content, "updated", line) ?? spliceFrontmatterLine(content, line);
if (stamped === null)
return content;
if (!newMemory || readFrontmatterDate(parseFrontmatter(stamped).data, "observed") !== undefined)
return stamped;
return spliceFrontmatterLine(stamped, `observed: ${today}`) ?? stamped;
}
function writerFor(target) {
const { source, config } = target;
const held = assetBundleOf(source);
/** Record the files a change touched for the boundary commit and the run's provenance; answer with its result. */
const record = (change) => {
for (const file of change.changed) {
recordWriteTargetPath(source, file);
// Run-scoped write provenance (#652).
recordWrittenPath(file);
}
};
/** What the index holds for the asset a change touched: its row is written again, or dropped with the asset. */
const reindex = (change, kept) => {
// The entry the write handed back (a keyring's item), else what the bundle reads for the concept now.
const asset = !kept
? undefined
: change.entry
? assetOf(held, { conceptId: change.conceptId, entry: change.entry })
: readFrom(held, change.conceptId);
const updated = asset ? refreshIndexedAsset(asset) : forgetIndexedAsset(held, change.conceptId);
// A store's asset is found through its index row alone: a row not updated is a secret later commands miss or still list.
if (!updated && held.isStore) {
warn(`The index was not updated for "${makeBundleRef(source.name, change.conceptId)}" (no index, or another akm is writing it): run \`akm index\` so other commands see the change.`);
}
};
const result = (change, kept = true) => {
record(change);
reindex(change, kept);
return {
ref: makeBundleRef(source.name, change.conceptId),
...(change.content !== undefined ? { content: change.content } : {}),
};
};
/** The asset a change applies to, whatever spelling named it, after the gates every change to an existing asset passes. */
const existing = (conceptId, options) => {
gate(source, config);
const asset = readFrom(held, conceptId);
if (!asset)
throw assetNotFound(`${source.name}//${conceptId}`);
const type = assetType(asset);
held.assertWrites(type);
if (options?.retire)
held.assertRetires(type);
if (options?.revision !== undefined && assetRevision(asset.entry.bytes()) !== options.revision) {
throw new UsageError(`Asset "${asset.ref}" changed since it was read; the change was not applied.`, "ASSET_CONTENT_CHANGED");
}
return asset;
};
return {
read: (conceptId) => readFrom(held, conceptId),
address: (ref) => gatedAddressIn(held, ref),
create(asset, options) {
gate(source, config);
held.assertWrites(asset.type);
if (options?.exclusive) {
const { conceptId } = held.place(asset);
if (held.read(conceptId)) {
throw new UsageError(`Asset "${source.name}//${conceptId}" already exists.`, "RESOURCE_ALREADY_EXISTS");
}
}
// Without `exclusive` a create replaces the asset, with the bytes a create writes.
const stamped = options?.verbatim
? asset
: { ...asset, content: stampDates(asset.content, held.place(asset).file, asset.type === "memory") };
return result(held.create(stamped, {
replace: !options?.exclusive,
verbatim: options?.verbatim,
}));
},
update(conceptId, content, options) {
const asset = existing(conceptId, options);
return result(held.update(asset.conceptId, stampDates(content, asset.entry.file)));
},
delete(conceptId, options) {
// A package's delete is one change per asset it held, the entry's first: every row of them leaves the index.
const [entry, ...members] = held.delete(existing(conceptId, options).conceptId);
for (const member of members)
result(member, false);
return result(entry, false);
},
mirrorPackage(asset, files, options) {
gate(source, config);
held.assertWrites(asset.type);
const [entry, ...others] = held.mirrorPackage(asset, files, { replace: options?.replace });
for (const change of others)
result(change);
return result(entry);
},
retire(conceptId, note, options) {
return held
.retire(existing(conceptId, { ...options, retire: true }).conceptId, note)
.map((change) => ({ ref: result(change, false).ref, handle: change.handle }));
},
retired: () => held.retired(),
restore(handle, fallback) {
gate(source, config);
held.assertRetiring();
return result(held.restore(handle, fallback));
},
forget(handle) {
gate(source, config);
held.assertRetiring();
record(held.forget(handle));
},
expireRetired(before) {
gate(source, config);
held.assertRetiring();
record(held.expireRetired(before));
},
};
}
/**
* Run `run` with a writer for `target`'s bundle, under the asset-mutation lease (re-entrant, so an async caller
* holding `withAssetMutationLease` may call it). Every change's files are recorded for the target's boundary commit
* and the run's write provenance. With `message`, the boundary commit runs when `run` returns; without, the
* caller's own boundary commits them.
*/
export function writeAssets(target, options, run) {
return withAssetMutationLeaseSync(options.purpose, () => {
const result = run(writerFor(target));
if (options.message !== undefined)
commitWriteTargetBoundary(target, options.message);
return result;
});
}