/*
RFC-0382: Command handlers for kernel.cache.status and kernel.cache.clear commands, delegating to the CacheLayer abstraction.
- Do not implement cache storage logic — delegates to CacheLayer.
- Handlers delegate to the cache layer port — no storage logic lives here.
- RFC-0382: initial implementation — runKernelCacheStatus and runKernelCacheClear handlers.
- ADR-0023: close CacheLayer connection after each command handler completes.
- RFC-1097: step 6 — compass.migrate codemod run
Mechanical v1 to v2 header migration across the workspace: 942 files rewritten — CHANGE_SUMMARY windows collapsed into history, forbidden v1 blocks stripped, KEY_DECISIONS seeded from @ai-invariant comments (5 files) or TODO placeholders (103 files), blocks reordered to canonical order.
- RFC-1097: sweep — werkstatt-engine clean
Sweep batch 4: 73 Compass headers on headerless engine files (certification, component-runtime, isolation, evolution, testing), real KEY_DECISIONS on 75 files (kernel, cache, dht, swim, gitmesh, runtime), ~80 purpose expansions (CONTRACT-02/PURPOSE-02), non-goals on 13 CONTRACT-03 files, CS-07 history literal fix repo-wide (253 files). Policy: .template.ts/.template.astro excludedPaths. werkstatt-engine now 0 diagnostics.
*/
import type {
KernelCommandInput,
KernelCommandResult,
KernelRuntimeContext,
} from "@warpgogol/werkstatt-shared/kernel";
import { createCacheLayer, type CacheStatus } from "./cache-layer.ts";
export async function runKernelCacheStatus(
_input: KernelCommandInput,
context: KernelRuntimeContext,
): Promise> {
const cache = await createCacheLayer(context.workspaceRoot);
try {
const status = await cache.status();
if (context.outputFormat === "pretty") {
if (status.available) {
context.logger.section("Kernel cache status");
context.logger.success(`Available — ${status.dbPath}`);
context.logger.info(`DB size: ${(status.dbSizeBytes / 1024).toFixed(1)} KB`);
for (const ns of status.namespaces) {
context.logger.info(
` ${ns.name}: ${ns.entries} entries, hit ratio ${(ns.hitRatio * 100).toFixed(1)}%`,
);
}
} else {
context.logger.warn(`Cache unavailable: ${status.unavailableReason ?? "unknown reason"}`);
}
}
return {
data: status,
exitCode: 0,
summary: status.available
? `[kernel.cache.status] Cache active — ${status.namespaces.length} namespace(s)`
: `[kernel.cache.status] Cache unavailable: ${status.unavailableReason ?? "unknown"}`,
};
} finally {
await cache.close();
}
}
export async function runKernelCacheClear(
input: KernelCommandInput,
context: KernelRuntimeContext,
): Promise> {
const cache = await createCacheLayer(context.workspaceRoot);
try {
const namespace = input.flags["namespace"] as string | undefined;
await cache.clear(namespace);
const status = await cache.status();
if (context.outputFormat === "pretty") {
context.logger.success(
namespace
? `Cleared namespace "${namespace}" from ${status.dbPath}`
: `Cleared all namespaces from ${status.dbPath}`,
);
}
return {
data: { cleared: true, dbPath: status.dbPath, namespace },
exitCode: 0,
summary: namespace
? `[kernel.cache.clear] Cleared namespace "${namespace}"`
: "[kernel.cache.clear] Cleared all cache namespaces",
};
} finally {
await cache.close();
}
}