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

77 lines (76 loc) • 3.84 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/. /** * An asset that changed ref and kept its identity: its history is re-keyed to the new ref. * * Two things move an asset's ref without making it a new asset: a file that was renamed or moved on disk * (`pairMovedFiles`, which pairs the vanished entry with the new file of the same content), and the one-time carry-over * of refs that gained a different spelling (`legacy-refs.ts`). Both end in the same re-key of the rows * state.db keeps by ref, which is {@link rekeyStateRefs}. */ import fs from "node:fs"; import path from "node:path"; import { groupBy } from "../core/common.js"; import { withStateDb } from "../core/state-db.js"; /** state.db tables that store a full `bundle//conceptId` ref, with the column that holds it. */ const STATE_REF_COLUMNS = [ ["usage_events", "entry_ref"], ["proposals", "ref"], ["events", "ref"], ["task_history", "target_ref"], ["improve_ledger", "ref"], ["asset_salience", "asset_ref"], ["asset_outcome", "asset_ref"], ["workflow_runs", "workflow_ref"], ]; /** Re-key the state.db rows stored under the old refs. A row already at the new ref is an orphan and gives way. */ export function rekeyStateRefs(moves) { if (moves.length === 0) return; withStateDb((db) => { db.transaction(() => { for (const [table, column] of STATE_REF_COLUMNS) { const update = db.prepare(`UPDATE OR REPLACE ${table} SET ${column} = ? WHERE ${column} = ?`); for (const move of moves) update.run(move.newItemRef, move.oldItemRef); } })(); }); } const sameName = (entries, filePath) => entries.filter((entry) => path.basename(entry.filePath) === path.basename(filePath)); /** * Pair each stored entry whose file is gone with the new file of the same content. A hash held by one vanished and one * new file pairs them; a hash held by several pairs the ones whose file names match one to one, and anything still * ambiguous is left unpaired (indexed as new). An entry whose file still exists is not a move. */ export function pairMovedFiles(stored, fresh) { // asset-io: state the directory scan pairs moved files (principled exception 6) const vanished = stored.filter((entry) => entry.contentHash !== null && !fs.existsSync(entry.filePath)); const freshByHash = groupBy([...fresh], (entry) => entry.contentHash); const pairs = []; const unpaired = []; for (const [hash, candidates] of groupBy(vanished, (entry) => entry.contentHash)) { const targets = freshByHash.get(hash); if (!targets) continue; for (const from of candidates) { const named = sameName(targets, from.filePath); const matched = candidates.length === 1 && targets.length === 1 ? targets[0] : named.length === 1 && sameName(candidates, from.filePath).length === 1 ? named[0] : undefined; if (matched) pairs.push({ id: from.id, oldItemRef: from.itemRef, oldFilePath: from.filePath, entry: matched }); else unpaired.push(from); } } return { pairs, unpaired }; } /** An entry row moved to its new file in place: its id keeps its vector, links and scores. */ export function repointEntry(db, id, entry) { const { provenance } = entry; db.prepare("UPDATE entries SET item_ref = ?, bundle_id = ?, concept_id = ?, adapter_id = ?, file_path = ? WHERE id = ?").run(provenance.itemRef, provenance.bundleId, provenance.conceptId, provenance.adapterId, entry.filePath, id); }