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
521 lines (520 loc) • 29.8 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/.
/**
* Typed error classes for structured exit code classification.
*
* Each class maps to one process exit code through its `kind`; the numeric
* table lives in ONE place, `EXIT_CODES` in `src/cli/shared.ts`
* (`classifyExitCode` there does the mapping), and is not repeated here.
* ConfigError is configuration / environment, UsageError is bad CLI input,
* NotFoundError is a missing resource or a command-reported failure, and
* TransientError is ordinary contention or an unreachable service that a
* retry can fix.
*
* Each error carries a machine-readable `code` field. Codes are stable
* identifiers safe to consume from scripts and JSON output. Existing throw
* sites without an explicit code receive a default code per error class so
* older call sites continue to compile and behave unchanged.
*
* Each error also exposes a `hint()` method returning an actionable hint
* string (or `undefined`). Hints can be supplied at construction time or
* derived from the error `code` via the per-class default mapping below.
* The CLI surfaces this via `error.hint()` rather than message-regex parsing.
*/
/** Stable, machine-readable codes for ConfigError. */
export const CONFIG_ERROR_CODES = [
"CONFIG_DIR_UNRESOLVABLE",
"STASH_DIR_NOT_FOUND",
"STASH_DIR_NOT_A_DIRECTORY",
"STASH_DIR_UNREADABLE",
// The index/state database exists (or may exist) but this process cannot read
// it — a permission or ownership mismatch, not a missing index. Distinct from
// "not built yet" precisely so a read can fail loudly instead of returning an
// empty-but-successful result for an index that is sitting right there (#791).
"DATA_DIR_UNREADABLE",
// A write the command exists to make (e.g. `akm feedback`'s record) could not
// be stored because the data directory is read-only to this process, such as
// an agent sandbox that only allows writes inside the workspace.
"DATA_DIR_NOT_WRITABLE",
"INDEX_SCHEMA_INCOMPATIBLE",
"LLM_NOT_CONFIGURED",
"AGENT_NOT_CONFIGURED",
// A connection or harness was named (a flag, an asset, a config key) that is not configured: never replaced by a default.
"SELECTION_NOT_CONFIGURED",
"INVALID_CONFIG_FILE",
// A git bundle's checkout holds uncommitted paths or commits that are not on its upstream, and the
// command would replace or delete the checkout. The work is the person's: akm names it and stops.
"SOURCE_HAS_LOCAL_WORK",
"UNKNOWN_IMPROVE_STRATEGY",
// Refused stashDir that would clobber a sensitive system path or the user's
// home directory Triggered by `akm bundle create`/`akm setup` when the
// explicit `--dir` argument resolves to e.g. `/`, `$HOME`, `~/.config`,
// `/etc`, etc.
"UNSAFE_STASH_DIR",
// Defense-in-depth sentinel raised under `bun test` / NODE_ENV=test
// when a test sets AKM_BUNDLE_DIR but forgets to also point
// XDG_DATA_HOME / AKM_DATA_DIR (and XDG_STATE_HOME / AKM_STATE_DIR)
// at temp directories. See src/core/paths.ts.
"TEST_ISOLATION_MISSING",
// The host platform/architecture has no supported build for a requested
// binary operation (e.g. `akm upgrade` on an unreleased platform target).
"UNSUPPORTED_PLATFORM",
// `akm upgrade` refused: the environment blocks the upgrade (version
// contract, filesystem permissions, or leftover upgrade state). The error
// message carries the specific remediation.
"UPGRADE_BLOCKED",
// A `${ref}` apiKey reference did not resolve to a stored value —
// the named secret does not exist, or no store-backed resolver was wired at
// the call site.
"SECRET_REFERENCE_UNRESOLVED",
// A bundle that is not a directory (an OS keyring, a cloud vault) could not be reached: the client
// program its adapter runs is not installed, or it refused or failed the request.
"BUNDLE_UNREACHABLE",
// A registry URL akm was asked to fetch is unusable before any request is
// made: not http(s), carries userinfo, or `AKM_NPM_REGISTRY` does not parse.
"REGISTRY_URL_INVALID",
];
/** Stable, machine-readable codes for UsageError. */
export const USAGE_ERROR_CODES = [
"INVALID_FLAG_VALUE",
"INVALID_SOURCE_VALUE",
"INVALID_FORMAT_VALUE",
"INVALID_DETAIL_VALUE",
"INVALID_JSON_CONFIG_VALUE",
"INVALID_JSON_ARGUMENT",
"MISSING_REQUIRED_ARGUMENT",
"MISSING_OR_AMBIGUOUS_TARGET",
"TARGET_NOT_UPDATABLE",
"PATH_ESCAPE_VIOLATION",
"RESOURCE_ALREADY_EXISTS",
/** A conditional Markdown write was based on an older or removed source. */
"ASSET_CONTENT_CHANGED",
/** A proposal's target changed (or is gone) since the proposal was made. */
"PROPOSAL_STALE",
/** A decision was asked of a proposal that is no longer pending. */
"PROPOSAL_NOT_PENDING",
"TASK_SCHEMA_VERSION_UNSUPPORTED",
"INVALID_PROPOSAL",
"NON_INTERACTIVE_REQUIRES_YES",
// citty's own CLIError (unknown top-level command or subcommand), reclassified
// by src/cli.ts so it flows through the same JSON envelope as every other
// usage error instead of citty's raw usage-banner + console.error path.
"UNKNOWN_COMMAND",
// A flag the resolved command does not declare. citty (node:util parseArgs,
// strict: false) silently ignores these, so without this error a typo
// parses "successfully" and exits 0 — a `--fail-on-flaged`
// in CI would mean the gate never fires.
"UNKNOWN_FLAG",
// A workflow step authors with: on a target that cannot bind it: a
// tasks/<ref> target that declares no inputs: at all, or a commands/<ref> or
// scripts/<ref> target, which are never binding surfaces. The authored mapping
// is rejected rather than silently dropped at freeze (fail-closed). Thrown from
// src/workflows/freeze/targets/task.ts's
// taskDispatch and src/workflows/freeze/freeze.ts's resolveStep.
"COMPOSITION_INVALID",
// P1a: the sourceError funnel in src/tasks/source-v3.ts, re-coded from
// INVALID_FLAG_VALUE. Message text, field-path rendering (`$` for the
// empty path), and the file:line location string are unchanged.
"TASK_SOURCE_INVALID",
// P1a: src/execution/target-ref.ts's classifyTargetRef rejects any value
// that is not a canonical commands/, scripts/, tasks/, or workflows/ asset
// ref (fragments, malformed shapes, non-canonical spellings, other asset
// families, GitHub locators, etc).
"TARGET_REF_INVALID",
// P1a: declared only in this phase — wired to workflow source validation
// (e.g. `akm workflow validate`) in a later phase.
"WORKFLOW_SOURCE_INVALID",
// P1a: declared only in this phase — wired in P2b when with: bindings are
// validated against a target's declared inputs.
"INPUT_BINDING_INVALID",
// A recognized construct the target model does not model — distinct from
// INVALID_FLAG_VALUE, where the input is malformed. Thrown by
// `prepare/script-capture.ts`'s interpreter rejections.
"TASK_TARGET_UNSUPPORTED",
];
/**
* Stable, machine-readable codes for TransientError — a retryable-shortly
* signal distinct from every UsageError code (#948): schedulers classify exit 2 as "fix the command line",
* but these two conditions mean "try again in a few seconds", a different
* contract a cron wrapper or scheduler can branch on.
*/
export const TRANSIENT_ERROR_CODES = [
// Another live akm process holds this workflow run's lock file (`akm
// workflow run` racing an in-flight one). Not a bad command line: ordinary
// contention a caller can retry once that process finishes.
"RUN_LEASE_HELD",
// #948: `withImmediateTransaction`/`beginImmediateTransaction`
// (src/core/state-db.ts) exhausted every BEGIN IMMEDIATE retry attempt and
// the failure is still contention-shaped (`isSqliteContentionError`) —
// another akm process is writing state.db right now, not a genuine
// corruption/unrelated failure. The original driver error survives as
// `cause`.
"STATE_DB_CONTENDED",
// A
// concurrent `akm index` (or any other writer touching index.db — source
// add/update) can make a
// rebuild's write hit the same contention-shaped SQLite condition
// `isSqliteContentionError` already classifies for state.db, but index.db
// writes had no reclassification boundary of their own — the raw driver
// error ("database is locked") escaped `akmIndex` as exit 70
// (internal/unclassified) instead of this "retry shortly" contract.
// Thrown from `akmIndex`'s outer catch (src/indexer/indexer.ts), the one
// function every caller goes through, for a contention-shaped error
// escaping the walk, index, or embedding phase. The original driver error
// survives as `cause`.
"INDEX_DB_CONTENDED",
// The
// `akm improve` whole-run lock (`commands/improve/locks.ts`) is ordinary
// contention between two legitimate `improve` invocations — the same
// "config error is not the right label for a timing collision" shape, as
// for the index rebuild lock and the maintenance-start barrier. A config
// error would exit 78 and tell a supervisor to stop retrying a normal lock
// collision. Thrown from `tryAcquireImproveLockUnlocked` when the lock is
// held by a live PID and `--skip-if-locked` was not passed.
"IMPROVE_LOCK_HELD",
// Another live akm process held `akm.lock`'s write sentinel for the whole
// 30 s acquisition window (`src/integrations/lockfile.ts`). Previously a
// ConfigError (exit 78) for what is a timing collision, not a bad config.
"LOCKFILE_CONTENDED",
// The asset-mutation lease (`src/core/index-writer-lock.ts`) stayed held
// by a live process for the whole wait window. Previously a bare Error
// (exit 70) for the same ordinary contention.
"ASSET_MUTATION_LEASE_HELD",
// The one lock around native scheduler writes (`src/tasks/scheduler-lock.ts`)
// is held by another live akm process running `task sync|add|enable|
// disable|prune`. Ordinary contention: retry once it finishes.
"SCHEDULER_LOCK_HELD",
// A registry request failed for a reason a retry can fix: the connection,
// DNS lookup or TLS handshake failed, the request timed out, or the server
// answered 429/5xx after the boundary's own retries (`src/registry/network.ts`).
"REGISTRY_UNREACHABLE",
// A website bundle's page could not be fetched for a reason a retry can fix: the connection, DNS lookup or TLS
// handshake failed, the request timed out, or the server answered 429/5xx (`src/sources/snapshot-fetchers/`).
"WEBSITE_UNREACHABLE",
];
/** Stable, machine-readable codes for NotFoundError. */
export const NOT_FOUND_ERROR_CODES = [
"ASSET_NOT_FOUND",
"SOURCE_NOT_FOUND",
"WORKFLOW_NOT_FOUND",
"PROPOSAL_NOT_FOUND",
"FILE_NOT_FOUND",
"COMMAND_NOT_EXECUTABLE",
"IMPROVE_RUN_NOT_FOUND",
// The registry answered, but not with the thing asked for: HTTP 404/410, or
// metadata that names no such version, dist-tag, or default branch.
"REGISTRY_NOT_FOUND",
// The registry answered with something akm cannot use: a non-OK status that
// is neither not-found nor transient, a body that is not JSON or exceeds the
// byte cap, an index without a `stashes` array, or npm metadata whose
// tarball is missing or sits on a different origin than the registry.
"REGISTRY_RESPONSE_INVALID",
// A website answered, but not with a page: HTTP 4xx other than 404 and 429 (a 403 refusal, say), too many
// redirects, or a redirect with no Location (`src/sources/snapshot-fetchers/`).
"WEBSITE_REFUSED",
// A website answered, but akm found no page it could take: a 404, a page over the size cap, or a crawl that kept
// nothing (`src/sources/snapshot-fetchers/website-ingest.ts`).
"WEBSITE_NO_CONTENT",
// The codes below name why a command that reports a failed result (exit 1) instead of throwing failed. They are
// recorded for the usage report only (`failResult`, `outputWithExitCode` in src/cli/shared.ts); nothing prints them.
// An agent run that failed, one per `AgentFailureReason` (src/integrations/agent/spawn.ts): `agentFailureCode`.
"AGENT_TIMEOUT",
"AGENT_SPAWN_FAILED",
"AGENT_NON_ZERO_EXIT",
"AGENT_PARSE_ERROR",
"AGENT_COOLDOWN",
"AGENT_LLM_RATE_LIMIT",
"AGENT_LLM_CONTENT_FILTER",
"AGENT_LLM_INVALID_JSON",
"AGENT_CONTENT_POLICY_REJECT",
"AGENT_UNSUPPORTED_TYPE",
"AGENT_NO_CHANGE",
"AGENT_QUALITY_REJECTED",
"AGENT_ABORTED",
// `akm judge`: the call failed, or its reply was not a verdict.
"JUDGE_FAILED",
"JUDGE_UNPARSEABLE",
// `akm lint --fail-on-flagged` flagged something.
"LINT_FLAGGED",
// `akm migrate`: a migration step is blocked.
"MIGRATION_BLOCKED",
// `akm improve extract`: the harness is not installed, has no session data, or lacks the named session.
"HARNESS_SESSIONS_UNAVAILABLE",
// `akm doctor` (and the Deprecated `akm health`): a hard check failed.
"HEALTH_CHECK_FAILED",
// `akm upgrade` installed, but is not done: its migration is blocked or failed, a plugin step failed, or a
// channel switch did not happen. Its notes say which.
"UPGRADE_INCOMPLETE",
// `akm doctor scheduler sync` (and `akm task sync`): a task source could not be reconciled.
"SCHEDULER_SYNC_FAILED",
// A scheduler preview (`--dry-run`, or prune without --yes) lists removals it did not make.
"SCHEDULER_REMOVALS_PENDING",
// `akm task dispatch`: a task due this minute could not be started.
"TASK_DISPATCH_FAILED",
// `akm doctor explain`: the start check refuses one of the task's schedule entries.
"TASK_SCHEDULE_REFUSED",
// `akm workflow run`: the run failed, stopped blocked (a verification gate included), or was aborted.
"WORKFLOW_RUN_FAILED",
"WORKFLOW_RUN_BLOCKED",
"WORKFLOW_RUN_ABORTED",
];
/** The code of a failed agent run, from its `reason`: `AGENT_<REASON>`, or none for a reason akm does not define. */
export function agentFailureCode(reason) {
const code = `AGENT_${reason?.toUpperCase()}`;
return NOT_FOUND_ERROR_CODES.find((known) => known === code);
}
/** Every code an akm error class can carry: akm's own closed list, for a consumer that must not pass on any other string. */
export const AKM_ERROR_CODES = new Set([
...CONFIG_ERROR_CODES,
...USAGE_ERROR_CODES,
...TRANSIENT_ERROR_CODES,
...NOT_FOUND_ERROR_CODES,
]);
/**
* Default hint for each ConfigError code. Keep these short, actionable, and
* imperative. Returning undefined means "no canned hint".
*/
const CONFIG_HINTS = {
STASH_DIR_NOT_FOUND: "Run `akm setup` to create and configure your bundle, or configure a defaultBundle path.",
STASH_DIR_NOT_A_DIRECTORY: "The configured default bundle path exists but isn't a directory. Update it to point at a folder.",
STASH_DIR_UNREADABLE: "Check the path exists and your user has read permission, or update the default bundle path.",
DATA_DIR_UNREADABLE: "The data directory is not readable by the user running akm. Check its owner and mode, or point AKM_DATA_DIR / XDG_DATA_HOME somewhere this user owns.",
DATA_DIR_NOT_WRITABLE: "Nothing was recorded. Make the akm data directory writable for this process (for an agent sandbox, add it to the writable paths), or point AKM_DATA_DIR / XDG_DATA_HOME somewhere this user can write, then retry.",
INDEX_SCHEMA_INCOMPATIBLE: "Run `akm index --full` to rebuild the derived index from the currently materialized sources.",
LLM_NOT_CONFIGURED: "Run `akm setup` to add a connection (an OpenAI-compatible endpoint) or enable a harness; model features need one.",
AGENT_NOT_CONFIGURED: "Run `akm setup` to enable a harness or add a connection; agent work needs one.",
SELECTION_NOT_CONFIGURED: "Name one that is configured (`akm doctor` lists them), correct the name, or add it with `akm setup --name <name> --endpoint <url>` (a connection) or `akm setup --harness <name>`.",
TEST_ISOLATION_MISSING: "Under bun test, when AKM_BUNDLE_DIR is set you MUST also set XDG_DATA_HOME (or AKM_DATA_DIR) and XDG_STATE_HOME (or AKM_STATE_DIR) to temp directories so the test does not touch the developer's real ~/.local/share/akm or ~/.local/state/akm.",
UNSAFE_STASH_DIR: "Choose a path inside your home directory (e.g. ~/akm) or another empty workspace. The bundle directory cannot be the filesystem root, your home directory itself, or a sensitive system path like /etc, /var, ~/.config, or ~/.ssh.",
UNKNOWN_IMPROVE_STRATEGY: "Pass one of the listed strategy names to `--strategy`, or define it under `improve.strategies`. Names are case-sensitive.",
SECRET_REFERENCE_UNRESOLVED: "Check the secret exists (`akm secret list`) and the ref inside `${…}` matches it, or run `akm secret set secrets/<name>` to store it.",
REGISTRY_URL_INVALID: "Registry URLs must be credential-free http(s) URLs. Fix the entry with `akm registry list` / `akm registry add`, or the AKM_REGISTRY_URL / AKM_NPM_REGISTRY variable that supplied it.",
};
// COMPOSITION_INVALID covers several unrelated causes
// (a rejected with:, a multi-job source, a composition cycle/depth/size
// violation, an invalid child-output reference). USAGE_HINTS below carries
// only the with:-rejection text — accurate for every with:-rejection throw
// site (none of which pass an explicit constructor hint), but wrong for the
// others. Those throw sites pass their OWN explicit hint (the constructor's
// 3rd argument overrides USAGE_HINTS, per errors-usage-hints.test.ts's
// "explicit constructor hint still overrides the USAGE_HINTS default").
// Multi-job rejection is thrown from 5 separate call sites across
// src/tasks/prepare/prepare-support.ts and src/workflows/**, so its hint is
// centralized here as the one shared string all 5 import, rather than
// duplicated at each site and risking drift.
export const COMPOSITION_INVALID_MULTI_JOB_HINT = "AKM workflows support exactly one job per source, with no needs: between jobs. Split the extra job(s) into " +
"their own workflow file, and compose them with uses: workflows/<ref> instead.";
/** Default hint for each UsageError code. */
const USAGE_HINTS = {
INVALID_FLAG_VALUE: "Run `akm <command> --help` to see accepted values.",
INVALID_SOURCE_VALUE: "Pick one of: local, registry, all, or a configured source name.",
INVALID_FORMAT_VALUE: "Pick one of: json, jsonl, yaml, text, md.",
INVALID_DETAIL_VALUE: "Pick one of: brief, normal, full, agent.",
INVALID_JSON_CONFIG_VALUE: 'Array and object values take JSON, quoted for the shell: a JSON array like `akm config set projects.x.filters.project \'["x"]\'` or a JSON object like `akm config set connections.fast \'{"endpoint":"http://localhost:11434/v1"}\'`. `akm config unset <key>` removes a key.',
MISSING_OR_AMBIGUOUS_TARGET: "Use `akm bundle update --all` or pass a target like `akm bundle update npm:@scope/pkg` (not both).",
TARGET_NOT_UPDATABLE: "Run `akm bundle list` to view your sources, then retry with one of those values.",
MISSING_REQUIRED_ARGUMENT: "Refs use the form [bundle//]conceptId, e.g. `akm show knowledge/guide.md` or `akm show skills/deploy`.",
UNKNOWN_COMMAND: "Run `akm --help` to see available commands.",
UNKNOWN_FLAG: "Run the command with `--help` to see its accepted flags.",
// Names the two real rejection causes: (1) a task target that declares no
// inputs: at all, (2) commands/<ref> / scripts/<ref>, which are never binding
// surfaces.
//
// This default is reached ONLY by with:-rejection
// throw sites (the ones above, plus task.ts's noDeclaredInputsError and
// freeze.ts's resolveStep) — every other
// COMPOSITION_INVALID throw site (multi-job source, composition
// cycle/depth/size, invalid child-output reference, an env: on a
// composing step) passes its own explicit constructor hint instead of
// falling through to this text, so this stays scoped and accurate rather
// than generalized into something vaguer.
COMPOSITION_INVALID: "Remove the with: block, or target a tasks/<ref> whose source declares inputs: — commands/<ref> and scripts/<ref> steps are not binding surfaces.",
TASK_SOURCE_INVALID: "Fix the task source at the reported path and line, then re-run.",
TARGET_REF_INVALID: "Targets are canonical asset refs: `commands/review`, `scripts/build.sh`, `tasks/nightly`, `workflows/release`.",
// Points at the two verbs that inspect a workflow source without executing it.
WORKFLOW_SOURCE_INVALID: "Run `akm lint` to see the failing source location, or `akm doctor explain <ref>` to compile it without writing.",
INPUT_BINDING_INVALID: "Check the values against the task's declared inputs (`akm show tasks/<name>` lists them); for a step's with: keys, check them against the target's declared inputs.",
TASK_TARGET_UNSUPPORTED: "Task definitions support command, script, workflow, and shell (run:) targets; akm/command is layered by callers.",
};
/** Default hint for each TransientErrorCode. */
const TRANSIENT_HINTS = {
RUN_LEASE_HELD: "Wait for the akm process driving this run (the named pid) to finish, then retry.",
STATE_DB_CONTENDED: "Another akm process is writing state.db right now. Wait a few seconds and retry; commands that support --skip-if-locked can skip instead of failing.",
INDEX_DB_CONTENDED: "Another akm process is writing index.db; retry shortly.",
IMPROVE_LOCK_HELD: "Another akm improve run holds the whole-run lock right now. Wait for it to finish and retry, or pass --skip-if-locked on scheduled runs.",
LOCKFILE_CONTENDED: "Another akm process is updating its bundle lockfile right now. Wait a few seconds and retry.",
ASSET_MUTATION_LEASE_HELD: "Another akm process is writing bundle content right now. Wait for it to finish and retry.",
SCHEDULER_LOCK_HELD: "Another `akm doctor scheduler sync`, `akm import --type task`, `akm task enable` or `akm task disable` is writing the scheduler right now. Wait for it to finish and retry.",
REGISTRY_UNREACHABLE: "The registry did not answer in time. Check the network and the registry URL (`akm registry list`), then retry.",
WEBSITE_UNREACHABLE: "The website could not be reached or answered with a server error. Check the network, then retry.",
};
/** Default hint for each NotFoundError code. */
const NOT_FOUND_HINTS = {
ASSET_NOT_FOUND: "Run `akm search <query>` or `akm index` to refresh the index.",
SOURCE_NOT_FOUND: "Run `akm bundle list` to view your sources, then retry with one of those values.",
WORKFLOW_NOT_FOUND: "Run `akm workflow list --active` to see runs.",
// A proposal is addressed by id or ref, never by path — reusing
// FILE_NOT_FOUND here handed users "check the path exists and is readable"
// for a mistyped id, which points at the wrong thing entirely.
PROPOSAL_NOT_FOUND: "Run `akm proposal list` to see pending proposals and their ids.",
FILE_NOT_FOUND: "Check the path exists and is readable.",
COMMAND_NOT_EXECUTABLE: "Add execute permission (`chmod +x <program>`) or run it through an interpreter.",
IMPROVE_RUN_NOT_FOUND: "Run `akm improve` first, or `akm doctor metrics improve --since 30d` to see recent run ids in `runIds`.",
REGISTRY_NOT_FOUND: "Check the ref's spelling and version; `akm search <query> --from registry` lists installable refs.",
WEBSITE_REFUSED: "The website refused the request. Check the bundle's URL (`akm bundle list`) and that the site allows it.",
WEBSITE_NO_CONTENT: "The website had no page akm could import. Check the URL opens a page in a browser.",
};
/**
* Base class for all akm-thrown, classified errors. Carries the `kind`
* discriminant consumed by the CLI exit-code classifier. Errors that are NOT
* instances of `AkmError` are treated as genuinely unexpected (INTERNAL).
*/
export class AkmError extends Error {
}
/**
* Codes that mean "the world moved under this request; reload and decide again", whatever their kind. A host maps
* them to one conflict status (HTTP 409) instead of the status of the kind they are thrown as.
*/
export const CONFLICT_CODES = new Set([
"ASSET_CONTENT_CHANGED",
"PROPOSAL_STALE",
"PROPOSAL_NOT_PENDING",
"ASSET_MUTATION_LEASE_HELD",
]);
/** Raised when configuration or environment is invalid or missing. */
export class ConfigError extends AkmError {
kind = "config";
code;
_hint;
constructor(msg, code = "INVALID_CONFIG_FILE", hint) {
super(msg);
this.name = "ConfigError";
this.code = code;
this._hint = hint;
// Fixes `instanceof` checks under ES5 transpilation targets.
Object.setPrototypeOf(this, new.target.prototype);
}
hint() {
return this._hint ?? CONFIG_HINTS[this.code];
}
}
/** Raised when the user supplies invalid arguments or input. */
export class UsageError extends AkmError {
kind = "usage";
code;
_hint;
constructor(msg, code = "INVALID_FLAG_VALUE", hint) {
super(msg);
this.name = "UsageError";
this.code = code;
this._hint = hint;
// Fixes `instanceof` checks under ES5 transpilation targets.
Object.setPrototypeOf(this, new.target.prototype);
}
hint() {
return this._hint ?? USAGE_HINTS[this.code];
}
}
/**
* Raised when a condition is ordinary, retryable contention rather than a
* bad command line or a genuine failure — another akm process holds a lock
* or is writing state.db right now. Distinct from `UsageError` (#948):
* schedulers classify exit 2 as
* "fix the command line", so contention needs its own exit code (75,
* sysexits EX_TEMPFAIL) a cron wrapper can branch on to retry instead of
* alerting.
*/
export class TransientError extends AkmError {
kind = "transient";
code;
_hint;
constructor(msg, code, hint) {
super(msg);
this.name = "TransientError";
this.code = code;
this._hint = hint;
// Fixes `instanceof` checks under ES5 transpilation targets.
Object.setPrototypeOf(this, new.target.prototype);
}
hint() {
return this._hint ?? TRANSIENT_HINTS[this.code];
}
}
/** Raised when a requested resource (asset, entry, file) is not found. */
export class NotFoundError extends AkmError {
kind = "not-found";
code;
_hint;
constructor(msg, code = "ASSET_NOT_FOUND", hint) {
super(msg);
this.name = "NotFoundError";
this.code = code;
this._hint = hint;
// Fixes `instanceof` checks under ES5 transpilation targets.
Object.setPrototypeOf(this, new.target.prototype);
}
hint() {
return this._hint ?? NOT_FOUND_HINTS[this.code];
}
}
/** The refusal for an asset `ref` (`<bundle>//<conceptId>`) that no bundle holds: the one sentence every adapter and the asset facade use. */
export function assetNotFound(ref) {
return new NotFoundError(`Asset not found: ${ref}`, "ASSET_NOT_FOUND");
}
/**
* Test-isolation guard helper.
*
* `src/core/paths.ts` throws `ConfigError("TEST_ISOLATION_MISSING")` under
* `bun test` when `AKM_BUNDLE_DIR` is set without a paired data-dir or
* state-dir override. That throw must never be swallowed by best-effort
* catches around DB/data-dir operations — otherwise the guard's loud failure
* silently degrades into a "no result" outcome (cold cache, missing snapshot,
* etc.) and the underlying test leak goes undetected.
*
* Call `rethrowIfTestIsolationError(err)` from any catch block that returns
* a fallback value (null, [], empty result) after touching DB or data-dir
* paths. It re-throws when the caught error is the guard violation, otherwise
* does nothing so the existing benign-fallback path can proceed unchanged.
*
* Usage:
* try {
* const db = openDatabase();
* // ...
* } catch (err) {
* rethrowIfTestIsolationError(err);
* // existing benign-fallback handling
* }
*/
export function isTestIsolationError(err) {
return err instanceof ConfigError && err.code === "TEST_ISOLATION_MISSING";
}
export function rethrowIfTestIsolationError(err) {
if (isTestIsolationError(err)) {
throw err;
}
}
/**
* Unreadable-data-dir guard helper — the #791 sibling of the test-isolation
* pair above, and it exists for the same reason.
*
* `DATA_DIR_UNREADABLE` says "this path is there and I am not allowed to read
* it". It is raised by `assertIndexPathReadable` and friends precisely so a
* permission fault stops being indistinguishable from "nothing indexed yet".
* That distinction is destroyed again the moment a best-effort `catch` around
* the open collapses it into the same `null`/`[]`/`0` the absent case returns —
* which is how `akm search` came to answer "No search index available. Run
* 'akm index'" at exit 0 for a populated index sitting right there on disk.
*
* Call `rethrowIfDataDirUnreadable(err)` from any catch block that returns a
* fallback value after touching a data-dir path. Absent stays absent; a fault
* the operator has to fix keeps travelling.
*/
export function isDataDirUnreadableError(err) {
return err instanceof ConfigError && err.code === "DATA_DIR_UNREADABLE";
}
export function rethrowIfDataDirUnreadable(err) {
if (isDataDirUnreadableError(err)) {
throw err;
}
}