Compare commits

...
Author SHA1 Message Date
kartik-mem0 ea94ffa63b Merge branch 'main' into fix/deflake-ts-integration-seeding 2026-08-14 17:13:38 +05:30
kartik-mem0 a3d1a1068b chore(ts-tests): delete dead global-setup/global-teardown files
227c02da9 dropped these from jest.integration.config.js and removed the
fullProjectCleanup helper they called, since every integration suite
already cleans up its own UUID-scoped user in afterAll. The two files
themselves were left behind unreferenced.
2026-08-13 16:30:09 +05:30
kartik-mem0 227c02da9f fix(ts-tests): stop integration runs from deleting each other's data
The integration suite claimed exclusive ownership of a shared project. Its
globalSetup and globalTeardown each ran a project-wide wipe:

    deleteAll({userId: '*', agentId: '*', appId: '*', runId: '*'})
    deleteUsers()

CI runs this suite concurrently across PRs against one MEM0_API_KEY, so each
job's wipe deleted the memories its neighbours had just seeded. maxWorkers: 1
and max-parallel: 1 only serialize within a run, not across them.

Evidence from three jobs that overlapped on 2026-08-12:

  6903 crud  seeded 13:53:24-13:54:15, hit by 6928's wipe (13:53:20-30)  FAIL
  6928 crud  seeded 13:53:30-13:54:19, hit by 6902's wipe (13:53:31-43)  FAIL
  6902 crud  seeded 13:53:43-13:54:21, no overlapping wipe               PASS
  6902 batch ran    13:55:31-13:56:20, hit by 6928's teardown (13:55:59) FAIL

Every failure surfaced as waitForMemories timing out with zero memories, which
reads as pipeline latency but was deletion: probing the live API shows a seeded
user settling in 17s, well inside the 45s budget.

- drop globalSetup/globalTeardown; every suite already cleans up its own
  UUID-scoped user in afterAll
- remove the fullProjectCleanup helper so the wipe cannot come back
- give updateWebhook a per-run URL; the project enforces uniqueness on
  (project, url), so concurrent runs collided on the hardcoded constant

Verified: two suites run concurrently against the same key go from 3 failures
to 1. The remainder is updateProject() mutating project-level
custom_instructions, a singleton two runs cannot both own.
2026-08-12 19:47:08 +05:30
kartik-mem0 d64d7b525b 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.
2026-08-12 19:16:51 +05:30
6 changed files with 96 additions and 115 deletions
-2
View File
@@ -2,8 +2,6 @@
module.exports = {
...require("./jest.config"),
testMatch: ["**/integration/**/*.test.ts"],
globalSetup: "<rootDir>/src/client/tests/integration/global-setup.ts",
globalTeardown: "<rootDir>/src/client/tests/integration/global-teardown.ts",
// Run integration tests serially to avoid rate limiting and race conditions
maxWorkers: 1,
};
@@ -1,40 +0,0 @@
/**
* Jest global setup for integration tests.
*
* Runs a full project cleanup before any integration test starts,
* then waits 10 seconds for the async cleanup to propagate.
*/
import { MemoryClient } from "../../mem0";
export default async function globalSetup() {
const apiKey = process.env.MEM0_API_KEY;
if (!apiKey) return; // skip if no key — tests will be skipped too
const client = new MemoryClient({ apiKey });
await client.ping();
console.log("[integration] Running pre-test cleanup...");
// Full project wipe — all four filters set explicitly
try {
await client.deleteAll({
userId: "*",
agentId: "*",
appId: "*",
runId: "*",
});
} catch {
// ignore — may 404 if no data exists
}
try {
await client.deleteUsers();
} catch {
// ignore — may throw "No entities to delete"
}
// Wait 10 seconds for async cleanup to propagate
console.log("[integration] Waiting 10s for cleanup to propagate...");
await new Promise((r) => setTimeout(r, 10_000));
console.log("[integration] Pre-test cleanup done.");
}
@@ -1,36 +0,0 @@
/**
* Jest global teardown for integration tests.
*
* Runs a full project cleanup after all integration tests complete
* so no test data is left behind.
*/
import { MemoryClient } from "../../mem0";
export default async function globalTeardown() {
const apiKey = process.env.MEM0_API_KEY;
if (!apiKey) return;
const client = new MemoryClient({ apiKey });
await client.ping();
console.log("[integration] Running post-test cleanup...");
try {
await client.deleteAll({
userId: "*",
agentId: "*",
appId: "*",
runId: "*",
});
} catch {
// ignore
}
try {
await client.deleteUsers();
} catch {
// ignore
}
console.log("[integration] Post-test cleanup done.");
}
+25 -36
View File
@@ -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`,
);
@@ -202,32 +220,3 @@ export async function cleanupTestUser(
// ignore
}
}
/**
* Full project wipe — deletes all memories and all entities.
* Equivalent to Python SDK's:
* client.delete_all(user_id="*", agent_id="*", app_id="*", run_id="*")
*
* Used as cleanup before and after integration test runs so tests
* start from a clean slate and don't leave data behind.
*/
export async function fullProjectCleanup(client: MemoryClient): Promise<void> {
// Delete all memories — all four filters set explicitly
try {
await client.deleteAll({
userId: "*",
agentId: "*",
appId: "*",
runId: "*",
});
} catch {
// ignore — may 404 if no data exists
}
// Delete all entities (users, agents, apps, runs)
try {
await client.deleteUsers();
} catch {
// ignore — may throw "No entities to delete"
}
}
@@ -100,6 +100,7 @@ describeIntegration("MemoryClient Integration — Users & Project", () => {
const hookName = `test-hook-${randomUUID().slice(0, 8)}`;
const hookUrl = `https://example.com/webhook/${randomUUID().slice(0, 8)}`;
const updatedName = `updated-hook-${randomUUID().slice(0, 8)}`;
const updatedUrl = `https://example.com/updated/${randomUUID().slice(0, 8)}`;
afterAll(async () => {
if (createdWebhookId) {
@@ -168,7 +169,7 @@ describeIntegration("MemoryClient Integration — Users & Project", () => {
client.updateWebhook({
webhookId: createdWebhookId,
name: updatedName,
url: "https://example.com/updated",
url: updatedUrl,
eventTypes: [WebhookEvent.MEMORY_DELETED],
}),
);
@@ -0,0 +1,69 @@
import { waitForMemories } from "./integration/helpers";
const clientReturning = (polls: Array<Array<{ id: string }>>) => {
let call = 0;
return {
getAll: jest.fn(async () => ({
results: polls[Math.min(call++, polls.length - 1)],
})),
} as any;
};
const settle = async <T>(pending: Promise<T>): Promise<T> => {
pending.catch(() => {});
for (let i = 0; i < 4; i++) {
await jest.advanceTimersByTimeAsync(15_000);
}
return pending;
};
describe("waitForMemories", () => {
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());
test("waits for the id set to stop changing", async () => {
const client = clientReturning([
[{ id: "a" }],
[{ id: "a" }, { id: "b" }],
[{ id: "a" }, { id: "b" }],
]);
const memories = await settle(waitForMemories(client, "user", 1));
expect(memories.map((m) => m.id)).toEqual(["a", "b"]);
expect(client.getAll).toHaveBeenCalledTimes(3);
});
test("keeps polling when consolidation replaces a memory at a constant count", async () => {
const client = clientReturning([
[{ id: "a" }, { id: "b" }],
[{ id: "a" }, { id: "c" }],
[{ id: "a" }, { id: "c" }],
]);
const memories = await settle(waitForMemories(client, "user", 1));
expect(memories.map((m) => m.id)).toEqual(["a", "c"]);
});
test("returns the last read when the set never settles but minCount is met", async () => {
const client = clientReturning([
[{ id: "a" }],
[{ id: "b" }],
[{ id: "c" }],
[{ id: "d" }],
]);
const memories = await settle(waitForMemories(client, "user", 1));
expect(memories.map((m) => m.id)).toEqual(["d"]);
});
test("throws when minCount is never reached", async () => {
const client = clientReturning([[]]);
await expect(settle(waitForMemories(client, "user", 1))).rejects.toThrow(
/expected at least 1 memories/,
);
});
});