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

61 lines (60 loc) • 2.41 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/. import { warn } from "../core/warn.js"; import { warnOnBundleRenameDrift } from "./bundle-identity-guard.js"; import { describeLockHolder, releaseIndexRebuildLock, tryAcquireIndexRebuildLock } from "./index-rebuild-lock.js"; import { akmIndex } from "./indexer.js"; /** Whether a build outcome says it was skipped. Takes `unknown` so improve's loosely typed `reindexFn` test stubs can be passed straight in. */ export function isSkippedIndexBuild(result) { return typeof result === "object" && result !== null && "skipped" in result; } export function skippedIndexReport(result) { return { reason: "lock-held", pid: result.skipped.pid, launcherPid: result.skipped.launcherPid, startedAt: result.skipped.startedAt, }; } export async function buildIndex(options) { const lock = acquireBuildLock(); if ("skipped" in lock) return lock; try { return await buildUnderLock(options); } finally { releaseIndexRebuildLock(lock.ownership); } } /** * Take the rebuild lock, then run `run` with a `build` that builds under that lock; release it when `run` settles. * `build` must not be called after `run` returns. When a live process holds the lock `run` still runs, and its * `build` skips instead of building, so what `run` publishes is indexed by the next build. */ export async function withIndexBuildLock(run) { const lock = acquireBuildLock(); if ("skipped" in lock) return run(async () => lock); try { return await run(buildUnderLock); } finally { releaseIndexRebuildLock(lock.ownership); } } function buildUnderLock(options) { // §11.5: warn (once) if the configured bundle ids drifted from the persisted // index prefixes (hand-renamed bundle key) BEFORE the build could re-mint. warnOnBundleRenameDrift(); return akmIndex(options); } function acquireBuildLock() { const lock = tryAcquireIndexRebuildLock(); if (lock.state === "held") { warn(`[index] another index run holds the lock (${describeLockHolder(lock.holder)}); not building.`); return { skipped: lock.holder }; } return { ownership: lock.ownership }; }