Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: cache optimization review fixes — guard empty poll, remove duplicate invalidation, fix test mocks

Gus8dc097a6b1c8
diff --git a/src/web/src/app/api/agents/[id]/email-accounts/route.ts b/src/web/src/app/api/agents/[id]/email-accounts/route.ts
index d94931aec..4e933b545 100644
--- a/src/web/src/app/api/agents/[id]/email-accounts/route.ts
+++ b/src/web/src/app/api/agents/[id]/email-accounts/route.ts
@@ -86,8 +86,6 @@ export const POST = withAuth(async (req, ctx) => {
pollIntervalSeconds: body.pollIntervalSeconds,
})
- await invalidate(cacheKeys.allEmailAccounts(ws.workspaceId));
-
try {
await cfEnv.EMAIL_WORKER.fetch(`http://internal/imap/start?accountId=${account.id}`, {
method: "POST",
diff --git a/src/web/src/app/api/daemon/tasks/poll/route.test.ts b/src/web/src/app/api/daemon/tasks/poll/route.test.ts
index 744f7a119..b2fd7634b 100644
--- a/src/web/src/app/api/daemon/tasks/poll/route.test.ts
+++ b/src/web/src/app/api/daemon/tasks/poll/route.test.ts
@@ -19,9 +19,7 @@ const mockMarkFileRequestsDispatched = vi.fn();
const mockExpireStaleFileRequests = vi.fn();
const mockListScheduledMeetings = vi.fn();
const mockClaimMeetingSessions = vi.fn();
-const mockGetEmailAccountsByAgents = vi.fn();
const mockGetAllEmailAccountsForWorkspace = vi.fn();
-const mockGetColleaguesForAgents = vi.fn();
const mockGetAllColleaguesForWorkspace = vi.fn();
vi.mock("@opennextjs/cloudflare", () => ({
@@ -62,7 +60,7 @@ vi.mock("@alook/shared", async () => {
getUser: (...args: unknown[]) => mockGetUser(...args),
},
emailAccount: {
- getEmailAccountsByAgents: (...args: unknown[]) => mockGetEmailAccountsByAgents(...args),
+ getEmailAccountsByAgents: (...args: unknown[]) => mockGetAllEmailAccountsForWorkspace(...args),
getAllEmailAccountsForWorkspace: (...args: unknown[]) => mockGetAllEmailAccountsForWorkspace(...args),
},
workspaceFileRequest: {
@@ -75,7 +73,7 @@ vi.mock("@alook/shared", async () => {
claimMeetingSessions: (...args: unknown[]) => mockClaimMeetingSessions(...args),
},
agentLink: {
- getColleaguesForAgents: (...args: unknown[]) => mockGetColleaguesForAgents(...args),
+ getColleaguesForAgents: (...args: unknown[]) => mockGetAllColleaguesForWorkspace(...args),
getAllColleaguesForWorkspace: (...args: unknown[]) => mockGetAllColleaguesForWorkspace(...args),
},
},
@@ -145,10 +143,7 @@ describe("POST /api/daemon/tasks/poll", () => {
mockExpireStaleFileRequests.mockResolvedValue(undefined);
mockListScheduledMeetings.mockResolvedValue([]);
mockGetAllAgentsForWorkspace.mockResolvedValue([]);
- mockGetAllAgentsForWorkspace.mockResolvedValue([]);
- mockGetEmailAccountsByAgents.mockResolvedValue([]);
mockGetAllEmailAccountsForWorkspace.mockResolvedValue([]);
- mockGetColleaguesForAgents.mockResolvedValue([]);
mockGetAllColleaguesForWorkspace.mockResolvedValue([]);
});
diff --git a/src/web/src/app/api/daemon/tasks/poll/route.ts b/src/web/src/app/api/daemon/tasks/poll/route.ts
index af05ceb00..41aaa4253 100644
--- a/src/web/src/app/api/daemon/tasks/poll/route.ts
+++ b/src/web/src/app/api/daemon/tasks/poll/route.ts
@@ -107,16 +107,20 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
const nonKillTasks = claimed.filter((t) => t.type !== "kill_task");
const agentIds = [...new Set(nonKillTasks.map((t) => t.agentId))];
- const [cachedAgents, cachedEmailAccounts, cachedColleagues] = await Promise.all([
- cached(cacheKeys.allAgents(ctx.workspaceId!), 300, () => queries.agent.getAllAgentsForWorkspace(db, ctx.workspaceId!)),
- cached(cacheKeys.allEmailAccounts(ctx.workspaceId!), 600, () => queries.emailAccount.getAllEmailAccountsForWorkspace(db, ctx.workspaceId!)),
- cached(cacheKeys.allColleagues(ctx.workspaceId!), 600, () => queries.agentLink.getAllColleaguesForWorkspace(db, ctx.workspaceId!)).catch(() => [] as Awaited<ReturnType<typeof queries.agentLink.getAllColleaguesForWorkspace>>),
- ]);
-
- const agentIdSet = new Set(agentIds);
- const allAgents = cachedAgents.filter((a) => agentIdSet.has(a.id));
- const allEmailAccounts = cachedEmailAccounts.filter((a) => agentIdSet.has(a.agentId));
- const allColleagues = cachedColleagues.filter((c) => agentIdSet.has(c.agentId));
+ const [allAgents, allEmailAccounts, allColleagues] = agentIds.length > 0
+ ? await Promise.all([
+ cached(cacheKeys.allAgents(ctx.workspaceId!), 300, () => queries.agent.getAllAgentsForWorkspace(db, ctx.workspaceId!)),
+ cached(cacheKeys.allEmailAccounts(ctx.workspaceId!), 600, () => queries.emailAccount.getAllEmailAccountsForWorkspace(db, ctx.workspaceId!)),
+ cached(cacheKeys.allColleagues(ctx.workspaceId!), 600, () => queries.agentLink.getAllColleaguesForWorkspace(db, ctx.workspaceId!)).catch(() => [] as Awaited<ReturnType<typeof queries.agentLink.getAllColleaguesForWorkspace>>),
+ ]).then(([agents, emails, colleagues]) => {
+ const agentIdSet = new Set(agentIds);
+ return [
+ agents.filter((a) => agentIdSet.has(a.id)),
+ emails.filter((a) => agentIdSet.has(a.agentId)),
+ colleagues.filter((c) => agentIdSet.has(c.agentId)),
+ ] as const;
+ })
+ : [[], [], [] as Awaited<ReturnType<typeof queries.agentLink.getAllColleaguesForWorkspace>>];
const agentMap = new Map(allAgents.map((a) => [a.id, a]));
const emailAccountsByAgent = new Map<string, string[]>();
diff --git a/src/web/src/app/api/email/send/route.test.ts b/src/web/src/app/api/email/send/route.test.ts
index 2de67f511..3380f09c3 100644
--- a/src/web/src/app/api/email/send/route.test.ts
+++ b/src/web/src/app/api/email/send/route.test.ts
@@ -6,7 +6,6 @@ const mockGetAgentByHandle = vi.fn();
const mockCreateEmail = vi.fn();
const mockIsWhitelisted = vi.fn();
const mockGetEmailAccountsByAgent = vi.fn();
-const mockGetAllEmailAccountsForWorkspace = vi.fn();
const mockGetEmailAccountScoped = vi.fn();
const mockEmailWorkerFetch = vi.fn();
const mockEmailBucketGet = vi.fn();
@@ -31,6 +30,13 @@ vi.mock("@opennextjs/cloudflare", () => ({
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }));
+vi.mock("@/lib/cache", () => ({
+ cached: vi.fn((_key: string, _ttl: number, fn: () => Promise<any>) => fn()),
+ cacheKeys: {
+ allEmailAccounts: (ws: string) => `ea:${ws}`,
+ },
+}));
+
vi.mock("@alook/shared", async () => {
const actual = await vi.importActual("@alook/shared");
return {
@@ -49,8 +55,8 @@ vi.mock("@alook/shared", async () => {
},
emailAccount: {
getEmailAccountsByAgent: (...args: unknown[]) => mockGetEmailAccountsByAgent(...args),
- getAllEmailAccountsForWorkspace: (...args: unknown[]) => mockGetAllEmailAccountsForWorkspace(...args),
getEmailAccountScoped: (...args: unknown[]) => mockGetEmailAccountScoped(...args),
+ getAllEmailAccountsForWorkspace: (...args: unknown[]) => mockGetEmailAccountsByAgent(...args),
},
conversation: {
getConversation: (...args: unknown[]) => mockGetConversation(...args),
@@ -97,10 +103,7 @@ function makeReq(body: Record<string, unknown>) {
}
describe("POST /api/email/send", () => {
- beforeEach(() => {
- vi.clearAllMocks();
- mockGetAllEmailAccountsForWorkspace.mockResolvedValue([]);
- });
+ beforeEach(() => vi.clearAllMocks());
it("sends email via EMAIL_WORKER and returns the created record", async () => {
mockGetAgent.mockResolvedValue({ id: "a1", emailHandle: "test-agent" });
@@ -461,7 +464,7 @@ describe("POST /api/email/send", () => {
it("skips local delivery when sender uses custom SMTP account", async () => {
mockGetAgent.mockResolvedValue({ id: "a1", emailHandle: "sender-agent", workspaceId: "ws1" });
- mockGetAllEmailAccountsForWorkspace.mockResolvedValue([
+ mockGetEmailAccountsByAgent.mockResolvedValue([
{ id: "acct1", agentId: "a1", emailAddress: "[email protected]" },
]);
mockEmailWorkerFetch.mockResolvedValue(Response.json({ ok: true, r2Key: "emails/x/raw" }));
diff --git a/src/web/src/app/api/studios/route.test.ts b/src/web/src/app/api/studios/route.test.ts
index ebe05c498..7380737c3 100644
--- a/src/web/src/app/api/studios/route.test.ts
+++ b/src/web/src/app/api/studios/route.test.ts
@@ -18,6 +18,16 @@ const mockEnqueueTask = vi.fn();
vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }));
+vi.mock("@/lib/cache", () => ({
+ invalidate: vi.fn(),
+ cached: vi.fn((_key: string, _ttl: number, fn: () => Promise<any>) => fn()),
+ cacheKeys: {
+ allAgents: (ws: string) => `agents:${ws}`,
+ allHandles: (ws: string) => `handles:${ws}`,
+ allColleagues: (ws: string) => `col:${ws}`,
+ },
+}));
+
vi.mock("@alook/shared", async () => {
const actual = await vi.importActual("@alook/shared");
return {