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
225 lines (224 loc) • 10.6 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/.
/**
* `index.db` connection lifecycle for the storage layer.
*
* Opens/closes the index database, running `ensureSchema` on the managed
* (writable) open path. This module lives BELOW the
* indexer, so the storage loan helpers (`index-db.ts`, `registry-index-cache-repository.ts`)
* import their opener from a sibling here instead of reaching up into the
* indexer (no storage→indexer dependency).
*/
import fs from "node:fs";
import path from "node:path";
import { ConfigError, UsageError } from "../../core/errors.js";
import { classifyPathAccess, describeInaccessiblePath } from "../../core/path-access.js";
import { getDbPath } from "../../core/paths.js";
import { warnOnce } from "../../core/warn.js";
import { openDatabase } from "../database.js";
import { openManagedDatabase } from "../managed-db.js";
import { SQLITE_BUSY_TIMEOUT_MS } from "../sqlite-pragmas.js";
import { openSqliteReadSnapshot, SqliteReadSnapshotUnavailableError } from "../sqlite-read-snapshot.js";
import { CANONICAL_INDEX_DB_VERSION } from "./index-entry-schema.js";
import { ensureSchema, newerIndexLayoutError } from "./index-schema.js";
/**
* Whether `error` is SQLite reporting that the file is not a usable database
* (`SQLITE_CORRUPT*` "database disk image is malformed", `SQLITE_NOTADB` "file
* is not a database") rather than a permission, lock, or schema problem.
* Matched on both `code` (bun:sqlite, better-sqlite3) and message text, since
* driver error shapes are not perfectly uniform.
*/
export function isCorruptionError(error) {
const code = error?.code;
if (typeof code === "string" && (code.startsWith("SQLITE_CORRUPT") || code === "SQLITE_NOTADB"))
return true;
const message = error instanceof Error ? error.message : String(error);
return (message.includes("database disk image is malformed") ||
message.includes("file is not a database") ||
message.includes("SQLITE_CORRUPT") ||
message.includes("SQLITE_NOTADB"));
}
/**
* Whether `index.db` itself is damaged: it cannot be opened, or `PRAGMA quick_check` reports a problem.
* A corruption error from a build or a read may come from another database (state.db), so callers ask
* this before deleting or blaming the index. An absent or unreadable-by-permission file is not damaged.
*/
export function indexDbIsDamaged(dbPath = getDbPath()) {
let db;
try {
db = openDatabase(dbPath, { readonly: true, create: false });
const row = db?.prepare("PRAGMA quick_check(1)").get();
return row !== undefined && row.quick_check !== "ok";
}
catch (error) {
return isCorruptionError(error);
}
finally {
db?.close();
}
}
/** Delete `index.db` and its sidecars; the build rebuilds it from the library. Best-effort per file. */
export function deleteIndexDatabase(dbPath) {
for (const suffix of ["", "-wal", "-shm"]) {
try {
fs.rmSync(`${dbPath}${suffix}`, { force: true });
}
catch {
// Best-effort cleanup; the next open still fails loudly if the file could not actually be removed.
}
}
}
/** Open `index.db` for writing, creating and migrating it. Corruption raises; only the build, which holds the rebuild lock, deletes and rebuilds. */
export function openIndexDatabase(dbPath, options) {
const resolvedPath = dbPath ?? getDbPath();
return openManagedDatabase({
path: resolvedPath,
pragmas: { dataDir: path.dirname(resolvedPath), busyTimeoutMs: options?.busyTimeoutMs },
init: (db) => {
// Source update uses this narrow lifecycle seam to ATTACH state.db and
// open its coordinator-owned outer transaction before ensureSchema or
// any indexer write can mutate the live generation.
options?.beforeSchema?.(db);
ensureSchema(db);
},
});
}
/** Said when the library has no usable index and no build is running. */
export const NO_INDEX_MESSAGE = "There is no search index for this library yet; run `akm index` to build one.";
/** The refusal of a command that needs the index when there is none. */
export function noIndexError() {
return new UsageError(NO_INDEX_MESSAGE, "MISSING_REQUIRED_ARGUMENT", "akm index");
}
export function openExistingDatabase(dbPath) {
// Existing-DB callers do not mutate schema or embedding metadata on open;
// they serve an older layout as-is and refuse a newer one (see checkIndexLayout).
//
// "Existing" is load-bearing: a missing file throws instead of being
// created. Create-on-open would leave a schema-less index.db behind (a
// fire-and-forget telemetry read would be enough), which every later opener then
// saw as an existing-but-broken index ("no such table: entries") — the
// curate→proposal file-order failure pinned by
// tests/storage/open-existing-database-no-create.test.ts. `create: false`
// below is the race-free backstop for this pre-check.
const resolvedPath = dbPath ?? getDbPath();
assertIndexPathReadable(resolvedPath);
if (classifyPathAccess(resolvedPath).access === "absent") {
throw noIndexError();
}
const db = openManagedDatabase({ path: resolvedPath, create: false });
try {
checkIndexLayout(db, resolvedPath);
return db;
}
catch (error) {
db.close();
throw error;
}
}
/**
* A reader serves an older layout as-is (the FTS readers understand both
* layouts, and a missing table degrades at the caller — keyword-only search,
* an inline rebuild, or a "run akm index" notice) and names it once per
* process; the next writable open migrates it in place. A newer layout is
* refused, naming the upgrade ({@link newerIndexLayoutError}).
*/
function checkIndexLayout(db, resolvedPath) {
let stored;
try {
const row = db.prepare("SELECT value FROM index_meta WHERE key = 'version'").get();
if (!row)
return;
stored = Number(row.value);
}
catch {
return;
}
if (!Number.isFinite(stored) || stored === CANONICAL_INDEX_DB_VERSION)
return;
if (stored > CANONICAL_INDEX_DB_VERSION)
throw newerIndexLayoutError(stored, resolvedPath);
warnOnce(`index-db-layout:${resolvedPath}`, `Index database at ${resolvedPath} uses an older layout (${stored}; this akm writes ${CANONICAL_INDEX_DB_VERSION}). ` +
"Serving it as-is; the next 'akm index' migrates it in place.");
}
/**
* Refuse to treat an UNREADABLE index as a missing one (#791).
*
* `fs.existsSync()` — which every one of these gates used to call — returns
* `false` for `EACCES` exactly as for `ENOENT`, so an index this process cannot
* read looked identical to one that had never been built. Callers then took
* their "no index yet" branch: `search`/`curate` returned no hits at exit 0 and
* told the user to run `akm index`, which would not have helped and which they
* may not have permission to do either.
*
* A `ConfigError` here exits 78 through the standard `{ok:false, error, code}`
* envelope, so both a human and a machine caller can tell "nothing indexed"
* from "I cannot see the index".
*/
export function assertIndexPathReadable(resolvedPath) {
const { access, code } = classifyPathAccess(resolvedPath);
if (access !== "inaccessible")
return;
throw new ConfigError(`Index database exists but is not readable: ${describeInaccessiblePath(resolvedPath, code)}.`, "DATA_DIR_UNREADABLE");
}
function openPlainReadonly(resolvedPath) {
return openDatabase(resolvedPath, { readonly: true, create: false });
}
function openIsolatedSnapshotOrFallBack(resolvedPath) {
try {
return openSqliteReadSnapshot(resolvedPath);
}
catch (error) {
if (!(error instanceof SqliteReadSnapshotUnavailableError))
throw error;
warnOnce(`index-read-snapshot-unavailable:${resolvedPath}`, `Could not take a non-mutating snapshot of ${resolvedPath} (${error.message}) — falling back to a plain ` +
"read-only open of the index database.");
return openPlainReadonly(resolvedPath);
}
}
/** {@link openReadonlyExistingDatabase} for a command that needs the index: a missing one is {@link noIndexError}. */
export function openReadonlyIndexOrRefuse(dbPath, options) {
const db = openReadonlyExistingDatabase(dbPath, options);
if (!db)
throw noIndexError();
return db;
}
/**
* Open an existing index for queries without changing the source database or
* running schema initialization. The default path attaches read-only to the
* source. `isolatedSnapshot` instead opens a disposable main/WAL copy so even
* SQLite's read-lock bookkeeping cannot touch the source SHM file.
*/
export function openReadonlyExistingDatabase(dbPath, options) {
const resolvedPath = dbPath ?? getDbPath();
// `undefined` means "no index" — reserve it for a genuinely absent one, and
// let an unreadable index raise instead of masquerading as absent (#791).
assertIndexPathReadable(resolvedPath);
if (classifyPathAccess(resolvedPath).access === "absent")
return undefined;
const db = options?.isolatedSnapshot ? openIsolatedSnapshotOrFallBack(resolvedPath) : openPlainReadonly(resolvedPath);
if (!db)
return undefined;
// This opener bypasses openManagedDatabase/applyStandardPragmas by design (no
// journal or schema work on a read-only handle), but that also left
// busy_timeout at SQLite's default of 0. In WAL that is harmless — readers
// never block — but in the DELETE/TRUNCATE modes the network-FS fallback and
// AKM_SQLITE_JOURNAL_MODE can select, a concurrent writer makes every read
// fail instantly with SQLITE_BUSY. busy_timeout is legal on a read-only
// connection, so apply just that one. `busyTimeoutMs` defaults to the
// shared 30s constant; a caller that must never sit behind another akm
// process's write lock for long (e.g. `akm info`) can pass a much shorter
// bound instead.
try {
db.exec(`PRAGMA busy_timeout = ${options?.busyTimeoutMs ?? SQLITE_BUSY_TIMEOUT_MS}`);
checkIndexLayout(db, resolvedPath);
return db;
}
catch (error) {
db.close();
throw error;
}
}
export function closeDatabase(db) {
db.close();
}