Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: include types in inbox count cache key to prevent stale results

Gus9d39ca77d611
diff --git a/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.ts b/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.ts
index 91ab4a90d..199b485e9 100644
--- a/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.ts
+++ b/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.ts
@@ -7,7 +7,7 @@ import { taskToResponse } from "@/lib/api/responses";
import { TaskService } from "@/lib/services/task";
import { CompleteTaskRequestSchema } from "@alook/shared";
import { broadcastToUser } from "@/lib/broadcast";
-import { invalidate, cacheKeys } from "@/lib/cache";
+import { invalidate, invalidateByPrefix, cacheKeys } from "@/lib/cache";
export const POST = withAuth(async (req: NextRequest, ctx) => {
if (!ctx.workspaceId) {
@@ -38,7 +38,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
);
const dateStr = new Date().toISOString().slice(0, 10);
invalidate(cacheKeys.overviewTaskStats(ctx.workspaceId, dateStr)).catch(() => {});
- invalidate(cacheKeys.inboxCount(ctx.userId, ctx.workspaceId)).catch(() => {});
+ invalidateByPrefix(cacheKeys.inboxCountPrefix(ctx.userId, ctx.workspaceId)).catch(() => {});
broadcastToUser(ctx.userId, { type: "task.updated", taskId, agentId: task.agentId, status: "completed" }).catch(() => {});
return writeJSON(taskToResponse(task));
} catch (e: unknown) {
diff --git a/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.ts b/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.ts
index b1249d5fd..286efd8b1 100644
--- a/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.ts
+++ b/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.ts
@@ -7,7 +7,7 @@ import { taskToResponse } from "@/lib/api/responses";
import { TaskService } from "@/lib/services/task";
import { FailTaskRequestSchema } from "@alook/shared";
import { broadcastToUser } from "@/lib/broadcast";
-import { invalidate, cacheKeys } from "@/lib/cache";
+import { invalidate, invalidateByPrefix, cacheKeys } from "@/lib/cache";
export const POST = withAuth(async (req: NextRequest, ctx) => {
if (!ctx.workspaceId) {
@@ -30,7 +30,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
const task = await taskService.failTask(taskId, ctx.workspaceId, body.error);
const dateStr = new Date().toISOString().slice(0, 10);
invalidate(cacheKeys.overviewTaskStats(ctx.workspaceId, dateStr)).catch(() => {});
- invalidate(cacheKeys.inboxCount(ctx.userId, ctx.workspaceId)).catch(() => {});
+ invalidateByPrefix(cacheKeys.inboxCountPrefix(ctx.userId, ctx.workspaceId)).catch(() => {});
broadcastToUser(ctx.userId, { type: "task.updated", taskId, agentId: task.agentId, status: "failed" }).catch(() => {});
return writeJSON(taskToResponse(task));
} catch (e: unknown) {
diff --git a/src/web/src/app/api/inbox/count/route.ts b/src/web/src/app/api/inbox/count/route.ts
index 08705d720..95e9ff0b5 100644
--- a/src/web/src/app/api/inbox/count/route.ts
+++ b/src/web/src/app/api/inbox/count/route.ts
@@ -21,7 +21,7 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {
: [];
const validTypes = types.length > 0 ? types : ["user_dm_message"];
- const count = await cached(cacheKeys.inboxCount(ctx.userId, ws.workspaceId), 60, () =>
+ const count = await cached(cacheKeys.inboxCount(ctx.userId, ws.workspaceId, validTypes), 60, () =>
queries.inbox.getUnreadCount(db, ctx.userId, ws.workspaceId, validTypes)
);
diff --git a/src/web/src/app/api/inbox/read-all/route.ts b/src/web/src/app/api/inbox/read-all/route.ts
index 62bb2b5a8..74ad9dd81 100644
--- a/src/web/src/app/api/inbox/read-all/route.ts
+++ b/src/web/src/app/api/inbox/read-all/route.ts
@@ -4,7 +4,7 @@ import { queries } from "@alook/shared";
import { getDb } from "@/lib/db";
import { withAuth } from "@/lib/middleware/auth";
import { withWorkspaceMember } from "@/lib/middleware/workspace";
-import { invalidate, cacheKeys } from "@/lib/cache";
+import { invalidateByPrefix, cacheKeys } from "@/lib/cache";
export const POST = withAuth(async (req: NextRequest, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
@@ -14,7 +14,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
const db = getDb((env as Env).DB);
await queries.inbox.markAllConversationsRead(db, ctx.userId, ws.workspaceId);
- invalidate(cacheKeys.inboxCount(ctx.userId, ws.workspaceId)).catch(() => {});
+ invalidateByPrefix(cacheKeys.inboxCountPrefix(ctx.userId, ws.workspaceId)).catch(() => {});
return new NextResponse(null, { status: 204 });
});
diff --git a/src/web/src/app/api/inbox/read/route.ts b/src/web/src/app/api/inbox/read/route.ts
index c5879c035..6f662036c 100644
--- a/src/web/src/app/api/inbox/read/route.ts
+++ b/src/web/src/app/api/inbox/read/route.ts
@@ -4,7 +4,7 @@ import { queries } from "@alook/shared";
import { getDb } from "@/lib/db";
import { withAuth } from "@/lib/middleware/auth";
import { withWorkspaceMember } from "@/lib/middleware/workspace";
-import { invalidate, cacheKeys } from "@/lib/cache";
+import { invalidateByPrefix, cacheKeys } from "@/lib/cache";
export const POST = withAuth(async (req: NextRequest, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
@@ -29,7 +29,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
}
await queries.inbox.markConversationRead(db, ctx.userId, body.conversationId);
- invalidate(cacheKeys.inboxCount(ctx.userId, ws.workspaceId)).catch(() => {});
+ invalidateByPrefix(cacheKeys.inboxCountPrefix(ctx.userId, ws.workspaceId)).catch(() => {});
return new NextResponse(null, { status: 204 });
});
diff --git a/src/web/src/lib/cache.ts b/src/web/src/lib/cache.ts
index edbfa5447..77654466e 100644
--- a/src/web/src/lib/cache.ts
+++ b/src/web/src/lib/cache.ts
@@ -138,6 +138,14 @@ export async function invalidateMany(keys: string[]): Promise<void> {
})));
}
+export async function invalidateByPrefix(prefix: string): Promise<void> {
+ const kv = getKV();
+ if (!kv) return;
+ const listed = await kv.list({ prefix }).catch(() => null);
+ if (!listed || listed.keys.length === 0) return;
+ await Promise.all(listed.keys.map((k) => kv.delete(k.name).catch(() => {})));
+}
+
export const cacheKeys = {
machineToken: (token: string) => `mt:${token.slice(0, 20)}`,
machineTokenLastUsed: (token: string) => `mt_lu:${token.slice(0, 20)}`,
@@ -157,5 +165,7 @@ export const cacheKeys = {
allRuntimes: (workspaceId: string) => `runtimes:${workspaceId}`,
allMembers: (workspaceId: string) => `members:${workspaceId}`,
activeTaskCounts: (workspaceId: string) => `atc:${workspaceId}`,
- inboxCount: (userId: string, workspaceId: string) => `inbox:${userId}:${workspaceId}`,
+ inboxCount: (userId: string, workspaceId: string, types?: string[]) =>
+ `inbox:${userId}:${workspaceId}:${types ? [...types].sort().join(",") : "*"}`,
+ inboxCountPrefix: (userId: string, workspaceId: string) => `inbox:${userId}:${workspaceId}:`,
};