Skip to content

Commit 0988a46

Browse files
ether-btcCharon
andauthored
fix: skip 75ms retry when store is empty (Bug 2) (#582)
Empty store: no write-ahead lag to wait for, so skip the retry sleep entirely. countRows() is O(1) metadata op — negligible cost. Logic: if results.length === 0 AND store.count() === 0 → return empty immediately, no sleep. Fixes Bug 2 from community code audit. Co-authored-by: Charon <charon@openclaw.ai>
1 parent 6bd4b57 commit 0988a46

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/store.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,12 @@ export class MemoryStore {
444444
return res.length > 0;
445445
}
446446

447+
/** Lightweight total row count via LanceDB countRows(). */
448+
async count(): Promise<number> {
449+
await this.ensureInitialized();
450+
return await this.table!.countRows();
451+
}
452+
447453
async getById(id: string, scopeFilter?: string[]): Promise<MemoryEntry | null> {
448454
await this.ensureInitialized();
449455

src/tools.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,15 @@ async function retrieveWithRetry(
175175
scopeFilter?: string[];
176176
category?: string;
177177
},
178+
countStore?: () => Promise<number>,
178179
): Promise<RetrievalResult[]> {
179180
let results = await retriever.retrieve(params);
180181
if (results.length === 0) {
182+
// Skip retry if store is empty — nothing to catch up via write-ahead lag.
183+
if (countStore) {
184+
const total = await countStore();
185+
if (total === 0) return results;
186+
}
181187
await sleep(75);
182188
results = await retriever.retrieve(params);
183189
}
@@ -210,7 +216,7 @@ async function resolveMemoryId(
210216
query: trimmed,
211217
limit: 5,
212218
scopeFilter,
213-
});
219+
}, () => context.store.count());
214220
if (results.length === 0) {
215221
return {
216222
ok: false,
@@ -575,7 +581,7 @@ export function registerMemoryRecallTool(
575581
scopeFilter,
576582
category,
577583
source: "manual",
578-
}), runtimeContext.workspaceBoundary);
584+
}, () => runtimeContext.store.count()), runtimeContext.workspaceBoundary);
579585

580586
if (results.length === 0) {
581587
return {
@@ -1079,7 +1085,7 @@ export function registerMemoryForgetTool(
10791085
query,
10801086
limit: 5,
10811087
scopeFilter,
1082-
});
1088+
}, () => context.store.count());
10831089

10841090
if (results.length === 0) {
10851091
return {
@@ -1221,7 +1227,7 @@ export function registerMemoryUpdateTool(
12211227
query: memoryId,
12221228
limit: 3,
12231229
scopeFilter,
1224-
});
1230+
}, () => context.store.count());
12251231
if (results.length === 0) {
12261232
return {
12271233
content: [
@@ -2142,7 +2148,7 @@ export function registerMemoryExplainRankTool(
21422148
limit: safeLimit,
21432149
scopeFilter,
21442150
source: "manual",
2145-
});
2151+
}, () => runtimeContext.store.count());
21462152
if (results.length === 0) {
21472153
return {
21482154
content: [{ type: "text", text: "No relevant memories found." }],

0 commit comments

Comments
 (0)