refactor: centralize cli manager cleanup

Co-authored-by: Nicholas Spisak <jsnsdirect@gmail.com>
This commit is contained in:
Peter Steinberger
2026-01-18 00:11:45 +00:00
parent 4d590f9254
commit b44d740720
4 changed files with 293 additions and 193 deletions

View File

@@ -47,6 +47,7 @@ Docs: https://docs.clawd.bot
### Changes ### Changes
- CLI: stamp build commit into dist metadata so banners show the commit in npm installs. - CLI: stamp build commit into dist metadata so banners show the commit in npm installs.
- CLI: close memory manager after memory commands to avoid hanging processes. (#1127) — thanks @NicholasSpisak.
## 2026.1.16-1 ## 2026.1.16-1

31
src/cli/cli-utils.ts Normal file
View File

@@ -0,0 +1,31 @@
export type ManagerLookupResult<T> = {
manager: T | null;
error?: string;
};
export function formatErrorMessage(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
export async function withManager<T>(params: {
getManager: () => Promise<ManagerLookupResult<T>>;
onMissing: (error?: string) => void;
run: (manager: T) => Promise<void>;
close: (manager: T) => Promise<void>;
onCloseError?: (err: unknown) => void;
}): Promise<void> {
const { manager, error } = await params.getManager();
if (!manager) {
params.onMissing(error);
return;
}
try {
await params.run(manager);
} finally {
try {
await params.close(manager);
} catch (err) {
params.onCloseError?.(err);
}
}
}

View File

@@ -135,6 +135,42 @@ describe("memory cli", () => {
expect(close).toHaveBeenCalled(); expect(close).toHaveBeenCalled();
}); });
it("logs close failure after status", async () => {
const { registerMemoryCli } = await import("./memory-cli.js");
const { defaultRuntime } = await import("../runtime.js");
const close = vi.fn(async () => {
throw new Error("close boom");
});
getMemorySearchManager.mockResolvedValueOnce({
manager: {
probeVectorAvailability: vi.fn(async () => true),
status: () => ({
files: 1,
chunks: 1,
dirty: false,
workspaceDir: "/tmp/clawd",
dbPath: "/tmp/memory.sqlite",
provider: "openai",
model: "text-embedding-3-small",
requestedProvider: "openai",
}),
close,
},
});
const error = vi.spyOn(defaultRuntime, "error").mockImplementation(() => {});
const program = new Command();
program.name("test");
registerMemoryCli(program);
await program.parseAsync(["memory", "status"], { from: "user" });
expect(close).toHaveBeenCalled();
expect(error).toHaveBeenCalledWith(
expect.stringContaining("Memory manager close failed: close boom"),
);
expect(process.exitCode).toBeUndefined();
});
it("reindexes on status --index", async () => { it("reindexes on status --index", async () => {
const { registerMemoryCli } = await import("./memory-cli.js"); const { registerMemoryCli } = await import("./memory-cli.js");
const { defaultRuntime } = await import("../runtime.js"); const { defaultRuntime } = await import("../runtime.js");
@@ -225,6 +261,42 @@ describe("memory cli", () => {
expect(process.exitCode).toBeUndefined(); expect(process.exitCode).toBeUndefined();
}); });
it("logs close failure after search", async () => {
const { registerMemoryCli } = await import("./memory-cli.js");
const { defaultRuntime } = await import("../runtime.js");
const close = vi.fn(async () => {
throw new Error("close boom");
});
const search = vi.fn(async () => [
{
path: "memory/2026-01-12.md",
startLine: 1,
endLine: 2,
score: 0.5,
snippet: "Hello",
},
]);
getMemorySearchManager.mockResolvedValueOnce({
manager: {
search,
close,
},
});
const error = vi.spyOn(defaultRuntime, "error").mockImplementation(() => {});
const program = new Command();
program.name("test");
registerMemoryCli(program);
await program.parseAsync(["memory", "search", "hello"], { from: "user" });
expect(search).toHaveBeenCalled();
expect(close).toHaveBeenCalled();
expect(error).toHaveBeenCalledWith(
expect.stringContaining("Memory manager close failed: close boom"),
);
expect(process.exitCode).toBeUndefined();
});
it("closes manager after search error", async () => { it("closes manager after search error", async () => {
const { registerMemoryCli } = await import("./memory-cli.js"); const { registerMemoryCli } = await import("./memory-cli.js");
const { defaultRuntime } = await import("../runtime.js"); const { defaultRuntime } = await import("../runtime.js");

View File

@@ -3,6 +3,7 @@ import type { Command } from "commander";
import { resolveDefaultAgentId } from "../agents/agent-scope.js"; import { resolveDefaultAgentId } from "../agents/agent-scope.js";
import { loadConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js";
import { withProgress, withProgressTotals } from "./progress.js"; import { withProgress, withProgressTotals } from "./progress.js";
import { formatErrorMessage, withManager } from "./cli-utils.js";
import { getMemorySearchManager, type MemorySearchManagerResult } from "../memory/index.js"; import { getMemorySearchManager, type MemorySearchManagerResult } from "../memory/index.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
@@ -23,34 +24,6 @@ function resolveAgent(cfg: ReturnType<typeof loadConfig>, agent?: string) {
return resolveDefaultAgentId(cfg); return resolveDefaultAgentId(cfg);
} }
function formatErrorMessage(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
async function closeManager(manager: MemoryManager): Promise<void> {
try {
await manager.close();
} catch (err) {
defaultRuntime.error(`Memory manager close failed: ${formatErrorMessage(err)}`);
}
}
async function withMemoryManager(
params: { cfg: ReturnType<typeof loadConfig>; agentId: string },
run: (manager: MemoryManager) => Promise<void>,
): Promise<void> {
const { manager, error } = await getMemorySearchManager(params);
if (!manager) {
defaultRuntime.log(error ?? "Memory search disabled.");
return;
}
try {
await run(manager);
} finally {
await closeManager(manager);
}
}
export function registerMemoryCli(program: Command) { export function registerMemoryCli(program: Command) {
const memory = program const memory = program
.command("memory") .command("memory")
@@ -71,135 +44,144 @@ export function registerMemoryCli(program: Command) {
.action(async (opts: MemoryCommandOptions) => { .action(async (opts: MemoryCommandOptions) => {
const cfg = loadConfig(); const cfg = loadConfig();
const agentId = resolveAgent(cfg, opts.agent); const agentId = resolveAgent(cfg, opts.agent);
await withMemoryManager({ cfg, agentId }, async (manager) => { await withManager<MemoryManager>({
const deep = Boolean(opts.deep || opts.index); getManager: () => getMemorySearchManager({ cfg, agentId }),
let embeddingProbe: Awaited<ReturnType<typeof manager.probeEmbeddingAvailability>> | undefined; onMissing: (error) => defaultRuntime.log(error ?? "Memory search disabled."),
let indexError: string | undefined; onCloseError: (err) =>
if (deep) { defaultRuntime.error(`Memory manager close failed: ${formatErrorMessage(err)}`),
await withProgress({ label: "Checking memory…", total: 2 }, async (progress) => { close: (manager) => manager.close(),
progress.setLabel("Probing vector…"); run: async (manager) => {
const deep = Boolean(opts.deep || opts.index);
let embeddingProbe:
| Awaited<ReturnType<typeof manager.probeEmbeddingAvailability>>
| undefined;
let indexError: string | undefined;
if (deep) {
await withProgress({ label: "Checking memory…", total: 2 }, async (progress) => {
progress.setLabel("Probing vector…");
await manager.probeVectorAvailability();
progress.tick();
progress.setLabel("Probing embeddings…");
embeddingProbe = await manager.probeEmbeddingAvailability();
progress.tick();
});
if (opts.index) {
await withProgressTotals(
{ label: "Indexing memory…", total: 0 },
async (update, progress) => {
try {
await manager.sync({
reason: "cli",
progress: (syncUpdate) => {
update({
completed: syncUpdate.completed,
total: syncUpdate.total,
label: syncUpdate.label,
});
if (syncUpdate.label) progress.setLabel(syncUpdate.label);
},
});
} catch (err) {
indexError = formatErrorMessage(err);
defaultRuntime.error(`Memory index failed: ${indexError}`);
process.exitCode = 1;
}
},
);
}
} else {
await manager.probeVectorAvailability(); await manager.probeVectorAvailability();
progress.tick(); }
progress.setLabel("Probing embeddings…"); const status = manager.status();
embeddingProbe = await manager.probeEmbeddingAvailability(); if (opts.json) {
progress.tick(); defaultRuntime.log(
}); JSON.stringify(
if (opts.index) { {
await withProgressTotals( ...status,
{ label: "Indexing memory…", total: 0 }, embeddings: embeddingProbe
async (update, progress) => { ? { ok: embeddingProbe.ok, error: embeddingProbe.error }
try { : undefined,
await manager.sync({ indexError,
reason: "cli", },
progress: (syncUpdate) => { null,
update({ 2,
completed: syncUpdate.completed, ),
total: syncUpdate.total,
label: syncUpdate.label,
});
if (syncUpdate.label) progress.setLabel(syncUpdate.label);
},
});
} catch (err) {
indexError = formatErrorMessage(err);
defaultRuntime.error(`Memory index failed: ${indexError}`);
process.exitCode = 1;
}
},
); );
return;
} }
} else { if (opts.index) {
await manager.probeVectorAvailability(); const line = indexError ? `Memory index failed: ${indexError}` : "Memory index complete.";
} defaultRuntime.log(line);
const status = manager.status();
if (opts.json) {
defaultRuntime.log(
JSON.stringify(
{
...status,
embeddings: embeddingProbe
? { ok: embeddingProbe.ok, error: embeddingProbe.error }
: undefined,
indexError,
},
null,
2,
),
);
return;
}
if (opts.index) {
const line = indexError ? `Memory index failed: ${indexError}` : "Memory index complete.";
defaultRuntime.log(line);
}
const rich = isRich();
const heading = (text: string) => colorize(rich, theme.heading, text);
const muted = (text: string) => colorize(rich, theme.muted, text);
const info = (text: string) => colorize(rich, theme.info, text);
const success = (text: string) => colorize(rich, theme.success, text);
const warn = (text: string) => colorize(rich, theme.warn, text);
const accent = (text: string) => colorize(rich, theme.accent, text);
const label = (text: string) => muted(`${text}:`);
const lines = [
`${heading("Memory Search")} ${muted(`(${agentId})`)}`,
`${label("Provider")} ${info(status.provider)} ${muted(
`(requested: ${status.requestedProvider})`,
)}`,
`${label("Model")} ${info(status.model)}`,
status.sources?.length ? `${label("Sources")} ${info(status.sources.join(", "))}` : null,
`${label("Indexed")} ${success(`${status.files} files · ${status.chunks} chunks`)}`,
`${label("Dirty")} ${status.dirty ? warn("yes") : muted("no")}`,
`${label("Store")} ${info(status.dbPath)}`,
`${label("Workspace")} ${info(status.workspaceDir)}`,
].filter(Boolean) as string[];
if (embeddingProbe) {
const state = embeddingProbe.ok ? "ready" : "unavailable";
const stateColor = embeddingProbe.ok ? theme.success : theme.warn;
lines.push(`${label("Embeddings")} ${colorize(rich, stateColor, state)}`);
if (embeddingProbe.error) {
lines.push(`${label("Embeddings error")} ${warn(embeddingProbe.error)}`);
} }
} const rich = isRich();
if (status.sourceCounts?.length) { const heading = (text: string) => colorize(rich, theme.heading, text);
lines.push(label("By source")); const muted = (text: string) => colorize(rich, theme.muted, text);
for (const entry of status.sourceCounts) { const info = (text: string) => colorize(rich, theme.info, text);
const counts = `${entry.files} files · ${entry.chunks} chunks`; const success = (text: string) => colorize(rich, theme.success, text);
lines.push(` ${accent(entry.source)} ${muted("·")} ${muted(counts)}`); const warn = (text: string) => colorize(rich, theme.warn, text);
const accent = (text: string) => colorize(rich, theme.accent, text);
const label = (text: string) => muted(`${text}:`);
const lines = [
`${heading("Memory Search")} ${muted(`(${agentId})`)}`,
`${label("Provider")} ${info(status.provider)} ${muted(
`(requested: ${status.requestedProvider})`,
)}`,
`${label("Model")} ${info(status.model)}`,
status.sources?.length ? `${label("Sources")} ${info(status.sources.join(", "))}` : null,
`${label("Indexed")} ${success(`${status.files} files · ${status.chunks} chunks`)}`,
`${label("Dirty")} ${status.dirty ? warn("yes") : muted("no")}`,
`${label("Store")} ${info(status.dbPath)}`,
`${label("Workspace")} ${info(status.workspaceDir)}`,
].filter(Boolean) as string[];
if (embeddingProbe) {
const state = embeddingProbe.ok ? "ready" : "unavailable";
const stateColor = embeddingProbe.ok ? theme.success : theme.warn;
lines.push(`${label("Embeddings")} ${colorize(rich, stateColor, state)}`);
if (embeddingProbe.error) {
lines.push(`${label("Embeddings error")} ${warn(embeddingProbe.error)}`);
}
} }
} if (status.sourceCounts?.length) {
if (status.fallback) { lines.push(label("By source"));
lines.push(`${label("Fallback")} ${warn(status.fallback.from)}`); for (const entry of status.sourceCounts) {
} const counts = `${entry.files} files · ${entry.chunks} chunks`;
if (status.vector) { lines.push(` ${accent(entry.source)} ${muted("·")} ${muted(counts)}`);
const vectorState = status.vector.enabled }
? status.vector.available
? "ready"
: "unavailable"
: "disabled";
const vectorColor =
vectorState === "ready"
? theme.success
: vectorState === "unavailable"
? theme.warn
: theme.muted;
lines.push(`${label("Vector")} ${colorize(rich, vectorColor, vectorState)}`);
if (status.vector.dims) {
lines.push(`${label("Vector dims")} ${info(String(status.vector.dims))}`);
} }
if (status.vector.extensionPath) { if (status.fallback) {
lines.push(`${label("Vector path")} ${info(status.vector.extensionPath)}`); lines.push(`${label("Fallback")} ${warn(status.fallback.from)}`);
} }
if (status.vector.loadError) { if (status.vector) {
lines.push(`${label("Vector error")} ${warn(status.vector.loadError)}`); const vectorState = status.vector.enabled
? status.vector.available
? "ready"
: "unavailable"
: "disabled";
const vectorColor =
vectorState === "ready"
? theme.success
: vectorState === "unavailable"
? theme.warn
: theme.muted;
lines.push(`${label("Vector")} ${colorize(rich, vectorColor, vectorState)}`);
if (status.vector.dims) {
lines.push(`${label("Vector dims")} ${info(String(status.vector.dims))}`);
}
if (status.vector.extensionPath) {
lines.push(`${label("Vector path")} ${info(status.vector.extensionPath)}`);
}
if (status.vector.loadError) {
lines.push(`${label("Vector error")} ${warn(status.vector.loadError)}`);
}
} }
} if (status.fallback?.reason) {
if (status.fallback?.reason) { lines.push(muted(status.fallback.reason));
lines.push(muted(status.fallback.reason)); }
} if (indexError) {
if (indexError) { lines.push(`${label("Index error")} ${warn(indexError)}`);
lines.push(`${label("Index error")} ${warn(indexError)}`); }
} defaultRuntime.log(lines.join("\n"));
defaultRuntime.log(lines.join("\n")); },
}); });
}); });
@@ -211,15 +193,22 @@ export function registerMemoryCli(program: Command) {
.action(async (opts: MemoryCommandOptions & { force?: boolean }) => { .action(async (opts: MemoryCommandOptions & { force?: boolean }) => {
const cfg = loadConfig(); const cfg = loadConfig();
const agentId = resolveAgent(cfg, opts.agent); const agentId = resolveAgent(cfg, opts.agent);
await withMemoryManager({ cfg, agentId }, async (manager) => { await withManager<MemoryManager>({
try { getManager: () => getMemorySearchManager({ cfg, agentId }),
await manager.sync({ reason: "cli", force: opts.force }); onMissing: (error) => defaultRuntime.log(error ?? "Memory search disabled."),
defaultRuntime.log("Memory index updated."); onCloseError: (err) =>
} catch (err) { defaultRuntime.error(`Memory manager close failed: ${formatErrorMessage(err)}`),
const message = formatErrorMessage(err); close: (manager) => manager.close(),
defaultRuntime.error(`Memory index failed: ${message}`); run: async (manager) => {
process.exitCode = 1; try {
} await manager.sync({ reason: "cli", force: opts.force });
defaultRuntime.log("Memory index updated.");
} catch (err) {
const message = formatErrorMessage(err);
defaultRuntime.error(`Memory index failed: ${message}`);
process.exitCode = 1;
}
},
}); });
}); });
@@ -241,41 +230,48 @@ export function registerMemoryCli(program: Command) {
) => { ) => {
const cfg = loadConfig(); const cfg = loadConfig();
const agentId = resolveAgent(cfg, opts.agent); const agentId = resolveAgent(cfg, opts.agent);
await withMemoryManager({ cfg, agentId }, async (manager) => { await withManager<MemoryManager>({
let results: Awaited<ReturnType<typeof manager.search>>; getManager: () => getMemorySearchManager({ cfg, agentId }),
try { onMissing: (error) => defaultRuntime.log(error ?? "Memory search disabled."),
results = await manager.search(query, { onCloseError: (err) =>
maxResults: opts.maxResults, defaultRuntime.error(`Memory manager close failed: ${formatErrorMessage(err)}`),
minScore: opts.minScore, close: (manager) => manager.close(),
}); run: async (manager) => {
} catch (err) { let results: Awaited<ReturnType<typeof manager.search>>;
const message = formatErrorMessage(err); try {
defaultRuntime.error(`Memory search failed: ${message}`); results = await manager.search(query, {
process.exitCode = 1; maxResults: opts.maxResults,
return; minScore: opts.minScore,
} });
if (opts.json) { } catch (err) {
defaultRuntime.log(JSON.stringify({ results }, null, 2)); const message = formatErrorMessage(err);
return; defaultRuntime.error(`Memory search failed: ${message}`);
} process.exitCode = 1;
if (results.length === 0) { return;
defaultRuntime.log("No matches."); }
return; if (opts.json) {
} defaultRuntime.log(JSON.stringify({ results }, null, 2));
const rich = isRich(); return;
const lines: string[] = []; }
for (const result of results) { if (results.length === 0) {
lines.push( defaultRuntime.log("No matches.");
`${colorize(rich, theme.success, result.score.toFixed(3))} ${colorize( return;
rich, }
theme.accent, const rich = isRich();
`${result.path}:${result.startLine}-${result.endLine}`, const lines: string[] = [];
)}`, for (const result of results) {
); lines.push(
lines.push(colorize(rich, theme.muted, result.snippet)); `${colorize(rich, theme.success, result.score.toFixed(3))} ${colorize(
lines.push(""); rich,
} theme.accent,
defaultRuntime.log(lines.join("\n").trim()); `${result.path}:${result.startLine}-${result.endLine}`,
)}`,
);
lines.push(colorize(rich, theme.muted, result.snippet));
lines.push("");
}
defaultRuntime.log(lines.join("\n").trim());
},
}); });
}, },
); );