fix(mem0-ts): wait for memory set to settle before returning from waitForMemories
The live-API integration suites failed intermittently on a different test each run: CRUD delete 404ing on a known ID, batch update/delete failing, search returning nothing. All three share one cause. waitForMemories returned as soon as one memory existed, but the v3 pipeline was still consolidating, and consolidation replaces a memory (delete + add) rather than editing it in place. Tests captured memoryIds at that moment and later acted on IDs the platform had already invalidated. Wait for quiescence instead: two consecutive polls returning the same sorted ID set. Compare IDs rather than length, since a replacement leaves the count unchanged. Fixed in the one shared helper every integration suite seeds through.
This commit is contained in:
@@ -52,13 +52,17 @@ export async function withRetry<T>(
|
||||
}
|
||||
|
||||
/**
|
||||
* Poll getAll until memories appear for a user.
|
||||
* The Mem0 API processes memories asynchronously — after add()
|
||||
* we need to wait for them to be available.
|
||||
* Poll getAll until a user's memories reach minCount and the ID set
|
||||
* stops changing between polls.
|
||||
*
|
||||
* The Mem0 API processes memories asynchronously, and consolidating a
|
||||
* new fact replaces a memory (delete + add) instead of editing it in
|
||||
* place. Returning on the first non-empty read hands callers IDs the
|
||||
* pipeline then invalidates, so compare IDs rather than length: a
|
||||
* replacement leaves the count unchanged.
|
||||
*
|
||||
* Polls every 15 seconds with a maximum of 4 retries to avoid
|
||||
* hitting rate limits. Throws if results aren't available after
|
||||
* all retries.
|
||||
* hitting rate limits. Throws if minCount is never reached.
|
||||
*/
|
||||
export async function waitForMemories(
|
||||
client: MemoryClient,
|
||||
@@ -66,18 +70,32 @@ export async function waitForMemories(
|
||||
minCount: number,
|
||||
maxRetries = 4,
|
||||
): Promise<Memory[]> {
|
||||
let memories: Memory[] = [];
|
||||
let previousIds = "";
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
const response = await withRetry(() =>
|
||||
client.getAll({ filters: { user_id: userId } }),
|
||||
);
|
||||
const memories = response.results ?? [];
|
||||
if (memories.length >= minCount) {
|
||||
memories = response.results ?? [];
|
||||
|
||||
const ids = memories
|
||||
.map((m) => m.id)
|
||||
.sort()
|
||||
.join(",");
|
||||
if (memories.length >= minCount && ids === previousIds) {
|
||||
return memories;
|
||||
}
|
||||
previousIds = ids;
|
||||
|
||||
if (attempt < maxRetries) {
|
||||
await new Promise((r) => setTimeout(r, 15_000));
|
||||
}
|
||||
}
|
||||
|
||||
if (memories.length >= minCount) {
|
||||
return memories;
|
||||
}
|
||||
throw new Error(
|
||||
`waitForMemories: expected at least ${minCount} memories for user "${userId}" but did not get them after ${maxRetries} attempts`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user