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
38 lines (37 loc) • 1.53 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/.
/**
* The one way health reads `index.db`: read-only, closed after the read, and a failed read on a damaged file says so. A read that fails
* while a first build holds the rebuild lock reports `building` (the partial index is expected), not an error.
*/
import { toErrorMessage } from "../../core/common.js";
import { getDbPath } from "../../core/paths.js";
import { heldRebuildLock } from "../../indexer/index-rebuild-lock.js";
import { closeDatabase, indexDbIsDamaged, openReadonlyExistingDatabase, } from "../../storage/repositories/index-connection.js";
export function readHealthIndex(read, options) {
let dbPath;
try {
dbPath = options?.dbPath ?? getDbPath();
}
catch (error) {
return { error: toErrorMessage(error) };
}
try {
const db = openReadonlyExistingDatabase(dbPath, options);
if (!db)
return { noIndex: true };
try {
return { value: read(db) };
}
finally {
closeDatabase(db);
}
}
catch (error) {
if (heldRebuildLock())
return { building: true };
// The integrity check costs a pass over the file, so only a read that has failed pays for it.
return { error: indexDbIsDamaged(dbPath) ? "index.db is damaged" : toErrorMessage(error) };
}
}