Skip to content

Commit 8b9650d

Browse files
authored
fix(memory): fail corrupted index commands (#124770)
* fix(memory): fail corrupted index commands * fix(memory): preserve promotion output limit * fix(memory): keep ranked promotion output
1 parent 4d4ee7b commit 8b9650d

6 files changed

Lines changed: 399 additions & 46 deletions

File tree

‎extensions/memory-core/src/cli-index-search.runtime.ts‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,17 @@ export async function runMemoryPromote(
312312
}
313313
let candidates: Awaited<ReturnType<typeof rankShortTermPromotionCandidates>>;
314314
try {
315+
const gatherAllForApply = Boolean(opts.apply);
315316
candidates = await rankShortTermPromotionCandidates({
316317
workspaceDir,
317-
limit: opts.limit,
318-
minScore: opts.minScore ?? dreaming.minScore,
319-
minRecallCount: opts.minRecallCount ?? dreaming.minRecallCount,
320-
minUniqueQueries: opts.minUniqueQueries ?? dreaming.minUniqueQueries,
318+
limit: gatherAllForApply ? undefined : opts.limit,
319+
minScore: gatherAllForApply ? 0 : (opts.minScore ?? dreaming.minScore),
320+
minRecallCount: gatherAllForApply ? 0 : (opts.minRecallCount ?? dreaming.minRecallCount),
321+
minUniqueQueries: gatherAllForApply
322+
? 0
323+
: (opts.minUniqueQueries ?? dreaming.minUniqueQueries),
321324
recencyHalfLifeDays: dreaming.recencyHalfLifeDays,
322-
maxAgeDays: dreaming.maxAgeDays,
325+
maxAgeDays: gatherAllForApply ? undefined : dreaming.maxAgeDays,
323326
includePromoted: Boolean(opts.includePromoted),
324327
});
325328
} catch (err) {
@@ -347,6 +350,25 @@ export async function runMemoryPromote(
347350
return;
348351
}
349352
}
353+
const outputLimit =
354+
typeof opts.limit === "number" && Number.isFinite(opts.limit)
355+
? Math.max(0, Math.floor(opts.limit))
356+
: candidates.length;
357+
const rejectedCandidates = applyResult
358+
? applyResult.rejectedCandidates.slice(
359+
0,
360+
Math.max(0, outputLimit - applyResult.appliedCandidates.length),
361+
)
362+
: [];
363+
const outputCandidateKeys = applyResult
364+
? new Set([
365+
...applyResult.appliedCandidates.map((candidate) => candidate.key),
366+
...rejectedCandidates.map((rejection) => rejection.candidate.key),
367+
])
368+
: undefined;
369+
const outputCandidates = outputCandidateKeys
370+
? candidates.filter((candidate) => outputCandidateKeys.has(candidate.key))
371+
: candidates;
350372
const storePath = resolveShortTermRecallStorePath(workspaceDir);
351373
const lockPath = resolveShortTermRecallLockPath(workspaceDir);
352374
const audit = await auditShortTermPromotionArtifacts({ workspaceDir });
@@ -356,14 +378,15 @@ export async function runMemoryPromote(
356378
storePath,
357379
lockPath,
358380
audit,
359-
candidates,
381+
candidates: outputCandidates,
360382
apply: applyResult
361383
? {
362384
applied: applyResult.applied,
363385
appended: applyResult.appended,
364386
reconciledExisting: applyResult.reconciledExisting,
365387
memoryPath: applyResult.memoryPath,
366388
appliedCandidates: applyResult.appliedCandidates,
389+
rejectedCandidates,
367390
}
368391
: undefined,
369392
});
@@ -383,7 +406,7 @@ export async function runMemoryPromote(
383406
lines.push(`${heading("Short-Term Promotion Candidates")} ${muted(`(${agentId})`)}`);
384407
lines.push(`${muted("Recall store:")} ${shortenHomePath(storePath)}`);
385408
lines.push(muted(`Store health: ${formatAuditCounts(audit)}`));
386-
for (const candidate of candidates) {
409+
for (const candidate of outputCandidates) {
387410
lines.push(
388411
`${success(candidate.score.toFixed(3))} ${accent(`${shortenHomePath(candidate.path)}:${candidate.startLine}-${candidate.endLine}`)}`,
389412
);
@@ -408,6 +431,11 @@ export async function runMemoryPromote(
408431
lines.push("");
409432
}
410433
if (applyResult) {
434+
for (const rejection of rejectedCandidates) {
435+
const candidate = rejection.candidate;
436+
const source = `${shortenHomePath(candidate.path)}:${candidate.startLine}-${candidate.endLine}`;
437+
lines.push(warn(`Skipped ${source}: ${rejection.reason}.`));
438+
}
411439
if (applyResult.applied > 0) {
412440
lines.push(
413441
success(
@@ -419,7 +447,7 @@ export async function runMemoryPromote(
419447
`appended=${applyResult.appended} reconciledExisting=${applyResult.reconciledExisting}`,
420448
),
421449
);
422-
} else {
450+
} else if (rejectedCandidates.length === 0) {
423451
lines.push(warn("No candidates met apply criteria."));
424452
}
425453
}

‎extensions/memory-core/src/cli-runtime-common.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export function formatExtraPaths(workspaceDir: string, extraPaths: MemoryExtraPa
161161
});
162162
}
163163
async function withMemoryManagerForAgent(params: {
164+
commandName: string;
164165
cfg: OpenClawConfig;
165166
agentId: string;
166167
purpose?: MemoryManagerPurpose;
@@ -179,7 +180,14 @@ async function withMemoryManagerForAgent(params: {
179180
}
180181
await withManager<MemoryManager>({
181182
getManager: () => getMemorySearchManager(managerParams),
182-
onMissing: (error) => defaultRuntime.log(error ?? "Memory search disabled."),
183+
onMissing: (error) => {
184+
if (!error?.trim()) {
185+
defaultRuntime.log("Memory search disabled.");
186+
return;
187+
}
188+
defaultRuntime.error(`${params.commandName} failed (${params.agentId}): ${error}`);
189+
process.exitCode = 1;
190+
},
183191
onCloseError: (err) =>
184192
defaultRuntime.error(`Memory manager close failed: ${formatErrorMessage(err)}`),
185193
close: async (manager) => {
@@ -207,6 +215,7 @@ export async function withMemoryCommand(params: {
207215
: [resolveAgent(cfg, params.agent)];
208216
for (const agentId of agentIds) {
209217
await withMemoryManagerForAgent({
218+
commandName: params.commandName,
210219
cfg,
211220
agentId,
212221
purpose: params.purpose,

0 commit comments

Comments
 (0)