Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

perf: reduce DB load with batched queries, caching, read replicas, and non-blocking housekeeping

Gusa69857e70805
diff --git a/scripts/cleanup-log-messages.sh b/scripts/cleanup-log-messages.sh
index 8adbd23b4..b8446daf2 100755
--- a/scripts/cleanup-log-messages.sh
+++ b/scripts/cleanup-log-messages.sh
@@ -2,7 +2,6 @@
# Bulk delete 'log' type rows from task_message table in batches of 50k
# Run from repo root: bash scripts/cleanup-log-messages.sh
-set -e
cd "$(dirname "$0")/../src/web"
BATCH=50000
@@ -12,13 +11,31 @@ while true; do
echo "Deleting batch of $BATCH rows..."
RESULT=$(npx wrangler d1 execute alook-app --remote --json --command "DELETE FROM task_message WHERE type = 'log' LIMIT $BATCH;" 2>&1)
+ if [ $? -ne 0 ]; then
+ echo "Wrangler command failed. Output:"
+ echo "$RESULT"
+ echo "Waiting 30s before retry..."
+ sleep 30
+ continue
+ fi
+
if echo "$RESULT" | grep -q '"error"'; then
- echo "D1 overloaded, waiting 30s before retry..."
+ echo "D1 error detected, waiting 30s before retry..."
+ echo "$RESULT"
sleep 30
continue
fi
- CHANGES=$(echo "$RESULT" | grep -o '"changes": [0-9]*' | head -1 | grep -o '[0-9]*')
+ CHANGES=$(echo "$RESULT" | grep -o '"changes":[[:space:]]*[0-9]*' | head -1 | grep -o '[0-9]*' || true)
+
+ if [ -z "$CHANGES" ]; then
+ echo "Could not parse changes from response:"
+ echo "$RESULT"
+ echo "Waiting 10s before retry..."
+ sleep 10
+ continue
+ fi
+
TOTAL_DELETED=$((TOTAL_DELETED + CHANGES))
echo "Deleted $CHANGES rows (total: $TOTAL_DELETED)"
diff --git a/src/email-worker/src/imap-poller-do.ts b/src/email-worker/src/imap-poller-do.ts
index bdfb30b84..90a9c284f 100644
--- a/src/email-worker/src/imap-poller-do.ts
+++ b/src/email-worker/src/imap-poller-do.ts
@@ -61,7 +61,12 @@ export class ImapPollerDO extends DurableObject<EmailEnv> {
}
async alarm(): Promise<void> {
- await this.pollImap()
+ await Promise.race([
+ this.pollImap(),
+ new Promise<void>((_, reject) => setTimeout(() => reject(new Error("poll timeout")), 25000)),
+ ]).catch((e) => {
+ log.warn("alarm: poll aborted", { err: String(e) })
+ })
}
private async pollImap(): Promise<void> {
diff --git a/src/shared/src/db/queries/task.ts b/src/shared/src/db/queries/task.ts
index bc7a241fb..9d7e2075a 100644
--- a/src/shared/src/db/queries/task.ts
+++ b/src/shared/src/db/queries/task.ts
@@ -102,24 +102,29 @@ export async function findSteerableReplacement(
)
);
- for (const active of activeTasks) {
- if (!active.contextKey) continue;
- const candidates = await db
- .select({ id: agentTaskQueue.id })
- .from(agentTaskQueue)
- .where(
- and(
- eq(agentTaskQueue.agentId, agentId),
- eq(agentTaskQueue.workspaceId, workspaceId),
- eq(agentTaskQueue.status, "queued"),
- eq(agentTaskQueue.conversationId, active.conversationId),
- eq(agentTaskQueue.contextKey, active.contextKey)
- )
+ const steerableActive = activeTasks.filter((t) => t.contextKey);
+ if (steerableActive.length === 0) return null;
+
+ const contextKeys = steerableActive.map((t) => t.contextKey!);
+ const queuedWithMatchingKeys = await db
+ .select({
+ conversationId: agentTaskQueue.conversationId,
+ contextKey: agentTaskQueue.contextKey,
+ })
+ .from(agentTaskQueue)
+ .where(
+ and(
+ eq(agentTaskQueue.agentId, agentId),
+ eq(agentTaskQueue.workspaceId, workspaceId),
+ eq(agentTaskQueue.status, "queued"),
+ inArray(agentTaskQueue.contextKey, contextKeys)
)
- .limit(1);
+ );
- if (candidates.length > 0) {
- return { predecessorId: active.id, contextKey: active.contextKey };
+ const queuedSet = new Set(queuedWithMatchingKeys.map((q) => `${q.conversationId}:${q.contextKey}`));
+ for (const active of steerableActive) {
+ if (queuedSet.has(`${active.conversationId}:${active.contextKey}`)) {
+ return { predecessorId: active.id, contextKey: active.contextKey! };
}
}
return null;
@@ -182,28 +187,30 @@ export async function claimTask(db: Database, agentId: string, workspaceId: stri
let candidates = await candidateQuery;
- // If no non-steerable candidate, try steerable conversations
+ // If no non-steerable candidate, try steerable conversations (batched, capped at 50)
if (candidates.length === 0 && steerableConvContextKeys.size > 0) {
- for (const [convId, activeContextKey] of steerableConvContextKeys) {
- const steerCandidates = await db
- .select({ id: agentTaskQueue.id })
- .from(agentTaskQueue)
- .where(
- and(
- eq(agentTaskQueue.agentId, agentId),
- eq(agentTaskQueue.workspaceId, workspaceId),
- eq(agentTaskQueue.status, "queued"),
- eq(agentTaskQueue.conversationId, convId),
- eq(agentTaskQueue.contextKey, activeContextKey)
- )
+ const steerConditions = [...steerableConvContextKeys.entries()].slice(0, 50).map(([convId, ctxKey]) =>
+ and(
+ eq(agentTaskQueue.conversationId, convId),
+ eq(agentTaskQueue.contextKey, ctxKey),
+ )
+ );
+ const steerCandidates = await db
+ .select({ id: agentTaskQueue.id })
+ .from(agentTaskQueue)
+ .where(
+ and(
+ eq(agentTaskQueue.agentId, agentId),
+ eq(agentTaskQueue.workspaceId, workspaceId),
+ eq(agentTaskQueue.status, "queued"),
+ or(...steerConditions),
)
- .orderBy(desc(agentTaskQueue.priority), asc(agentTaskQueue.createdAt))
- .limit(1);
+ )
+ .orderBy(desc(agentTaskQueue.priority), asc(agentTaskQueue.createdAt))
+ .limit(1);
- if (steerCandidates.length > 0) {
- candidates = steerCandidates;
- break;
- }
+ if (steerCandidates.length > 0) {
+ candidates = steerCandidates;
}
}
@@ -617,38 +624,20 @@ const DEFAULT_STALE_RUNNING_SECONDS = Number(process.env.ALOOK_STALE_RUNNING_TIM
export async function failStaleRunningTasks(db: Database, workspaceId: string, staleSeconds = DEFAULT_STALE_RUNNING_SECONDS) {
const threshold = new Date(Date.now() - staleSeconds * 1000).toISOString();
- const runningTasks = await db
- .select({ id: agentTaskQueue.id, startedAt: agentTaskQueue.startedAt })
+ const staleRows = await db
+ .select({ id: agentTaskQueue.id })
.from(agentTaskQueue)
.where(
and(
eq(agentTaskQueue.workspaceId, workspaceId),
eq(agentTaskQueue.status, "running"),
+ sql`coalesce((SELECT MAX(created_at) FROM task_message WHERE task_id = ${agentTaskQueue.id}), ${agentTaskQueue.startedAt}) < ${threshold}`
)
);
- if (runningTasks.length === 0) return [];
-
- const taskIds = runningTasks.map(t => t.id);
- const lastMessages = await db
- .select({
- taskId: taskMessage.taskId,
- lastMessageAt: sql<string>`max(${taskMessage.createdAt})`.as("last_message_at"),
- })
- .from(taskMessage)
- .where(inArray(taskMessage.taskId, taskIds))
- .groupBy(taskMessage.taskId);
-
- const lastMsgMap = new Map(lastMessages.map(m => [m.taskId, m.lastMessageAt]));
- const staleIds = runningTasks
- .filter(t => {
- const lastActivity = lastMsgMap.get(t.id) ?? t.startedAt;
- return lastActivity != null && lastActivity < threshold;
- })
- .map(t => t.id);
-
- if (staleIds.length === 0) return [];
+ if (staleRows.length === 0) return [];
+ const staleIds = staleRows.map((r) => r.id);
const rows = await db
.update(agentTaskQueue)
.set({
diff --git a/src/shared/src/db/schema.ts b/src/shared/src/db/schema.ts
index e5658ee16..60c780733 100644
--- a/src/shared/src/db/schema.ts
+++ b/src/shared/src/db/schema.ts
@@ -379,6 +379,7 @@ export const agentTaskQueue = sqliteTable(
index("idx_task_queue_trace").on(t.traceId),
index("idx_task_queue_parent").on(t.parentTaskId),
index("idx_task_queue_workspace_type_status").on(t.workspaceId, t.type, t.status),
+ index("idx_task_queue_workspace_status_dispatched").on(t.workspaceId, t.status, t.dispatchedAt),
foreignKey({
columns: [t.agentId, t.workspaceId],
foreignColumns: [agent.id, agent.workspaceId],
diff --git a/src/web/migrations/0030_task_queue_dispatch_index.sql b/src/web/migrations/0030_task_queue_dispatch_index.sql
new file mode 100644
index 000000000..efe8e5a19
--- /dev/null
+++ b/src/web/migrations/0030_task_queue_dispatch_index.sql
@@ -0,0 +1,5 @@
+-- Composite index for failStaleDispatchedTasks sweep query.
+-- The existing idx_task_queue_workspace_active covers (workspaceId, status, agentId)
+-- but lacks dispatched_at for the range scan used in the stale-dispatch check.
+CREATE INDEX IF NOT EXISTS idx_task_queue_workspace_status_dispatched
+ ON agent_task_queue(workspace_id, status, dispatched_at);
diff --git a/src/web/src/app/api/agents/active-task-counts/route.test.ts b/src/web/src/app/api/agents/active-task-counts/route.test.ts
index 6ae9c89f0..ac6cbe18e 100644
--- a/src/web/src/app/api/agents/active-task-counts/route.test.ts
+++ b/src/web/src/app/api/agents/active-task-counts/route.test.ts
@@ -18,8 +18,12 @@ vi.mock("@/lib/middleware/helpers", () => ({
vi.mock("@opennextjs/cloudflare", () => ({
getCloudflareContext: vi.fn(() => ({ env: { DB: {} } })),
}));
+vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})), getReadDb: vi.fn(() => ({})) }));
+vi.mock("@/lib/cache", () => ({
+ cached: vi.fn((_key: string, _ttl: number, fn: () => any) => fn()),
+ cacheKeys: { activeTaskCounts: (ws: string) => `atc:${ws}` },
+}));
vi.mock("@alook/shared", () => ({
- createDb: vi.fn(() => ({})),
queries: {
task: {
listActiveTaskCountsByWorkspace: (...args: any[]) =>
diff --git a/src/web/src/app/api/agents/active-task-counts/route.ts b/src/web/src/app/api/agents/active-task-counts/route.ts
index 8a1979f0a..0ba15aefe 100644
--- a/src/web/src/app/api/agents/active-task-counts/route.ts
+++ b/src/web/src/app/api/agents/active-task-counts/route.ts
@@ -1,21 +1,26 @@
import { getCloudflareContext } from "@opennextjs/cloudflare";
-import { createDb, queries } from "@alook/shared";
+import { queries } from "@alook/shared";
+import { getReadDb } from "@/lib/db";
import { withAuth } from "@/lib/middleware/auth";
import { withWorkspaceMember } from "@/lib/middleware/workspace";
import { writeJSON } from "@/lib/middleware/helpers";
+import { cached, cacheKeys } from "@/lib/cache";
export const GET = withAuth(async (req, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
if (ws instanceof Response) return ws;
const { env } = getCloudflareContext();
- const db = createDb((env as Env).DB);
+ const db = getReadDb((env as Env).DB);
- const rows = await queries.task.listActiveTaskCountsByWorkspace(db, ws.workspaceId);
- const counts: Record<string, number> = {};
- for (const row of rows) {
- counts[row.agentId] = Number(row.count);
- }
+ const counts = await cached(cacheKeys.activeTaskCounts(ws.workspaceId), 10, async () => {
+ const rows = await queries.task.listActiveTaskCountsByWorkspace(db, ws.workspaceId);
+ const result: Record<string, number> = {};
+ for (const row of rows) {
+ result[row.agentId] = Number(row.count);
+ }
+ return result;
+ });
return writeJSON({ counts });
});
diff --git a/src/web/src/app/api/agents/route.test.ts b/src/web/src/app/api/agents/route.test.ts
index 1813aaaa2..52f624415 100644
--- a/src/web/src/app/api/agents/route.test.ts
+++ b/src/web/src/app/api/agents/route.test.ts
@@ -9,7 +9,7 @@ const mockCreateAgent = vi.fn();
const mockGetAgent = vi.fn();
const mockGetAgentRuntimeForWorkspace = vi.fn();
-vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }));
+vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})), getReadDb: vi.fn(() => ({})) }));
vi.mock("@alook/shared", async () => {
const actual = await vi.importActual("@alook/shared");
@@ -57,10 +57,6 @@ vi.mock("@/lib/services/task", () => {
return { TaskService: Svc };
});
-vi.mock("@/lib/services/sweep", () => ({
- sweepStaleState: vi.fn().mockResolvedValue(undefined),
-}));
-
vi.mock("@/lib/cache", () => ({
invalidate: vi.fn(),
cached: vi.fn((_key: string, _ttl: number, fn: () => Promise<any>) => fn()),
diff --git a/src/web/src/app/api/agents/route.ts b/src/web/src/app/api/agents/route.ts
index 0398e2082..4100b17d0 100644
--- a/src/web/src/app/api/agents/route.ts
+++ b/src/web/src/app/api/agents/route.ts
@@ -1,13 +1,12 @@
import { NextRequest } from "next/server";
import { getCloudflareContext } from "@opennextjs/cloudflare"
import { queries, isValidHandle, isOnline, CreateAgentRequestSchema, TASK_TYPES } from "@alook/shared"
-import { getDb } from "@/lib/db"
+import { getDb, getReadDb } from "@/lib/db"
import { withAuth } from "@/lib/middleware/auth";
import { withWorkspaceMember } from "@/lib/middleware/workspace";
import { writeJSON, writeError, parseBody } from "@/lib/middleware/helpers";
import { agentToResponse } from "@/lib/api/responses";
import { TaskService } from "@/lib/services/task";
-import { sweepStaleState } from "@/lib/services/sweep";
import { invalidate, cached, cacheKeys } from "@/lib/cache";
import { filterVisibleAgents } from "@/lib/agent-visibility";
@@ -16,10 +15,7 @@ export const GET = withAuth(async (req, ctx) => {
if (ws instanceof Response) return ws;
const { env } = getCloudflareContext()
- const db = getDb((env as Env).DB)
-
- // Sweep stale state: catches stuck tasks even when all daemons are dead
- await sweepStaleState(db, ws.workspaceId);
+ const db = getReadDb((env as Env).DB)
const [allAgents, allAccess] = await Promise.all([
cached(cacheKeys.allAgents(ws.workspaceId), 300, () => queries.agent.getAllAgentsForWorkspace(db, ws.workspaceId)),
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 9ff5e9ef8..91ab4a90d 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
@@ -38,6 +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(() => {});
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 65ceb670b..b1249d5fd 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
@@ -30,6 +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(() => {});
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/daemon/tasks/poll/route.test.ts b/src/web/src/app/api/daemon/tasks/poll/route.test.ts
index 534fe8d62..af8e18ec2 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
@@ -25,7 +25,7 @@ const mockInvalidate = vi.fn().mockResolvedValue(undefined);
const mockKvPut = vi.fn().mockResolvedValue(undefined);
vi.mock("@opennextjs/cloudflare", () => ({
- getCloudflareContext: vi.fn(() => ({ env: { DB: {}, CACHE_KV: { put: (...args: unknown[]) => mockKvPut(...args), get: vi.fn().mockResolvedValue(null), delete: vi.fn().mockResolvedValue(undefined) } } })),
+ getCloudflareContext: vi.fn(() => ({ env: { DB: {}, CACHE_KV: { put: (...args: unknown[]) => mockKvPut(...args), get: vi.fn().mockResolvedValue(null), delete: vi.fn().mockResolvedValue(undefined) } }, ctx: { waitUntil: vi.fn() } })),
}));
vi.mock("@/lib/db", () => ({
@@ -839,7 +839,7 @@ describe("POST /api/daemon/tasks/poll", () => {
// --- Heartbeat cache invalidation ---
- it("invalidates allRuntimes cache on every poll heartbeat", async () => {
+ it("does NOT invalidate allRuntimes cache on poll heartbeat (prevents cache thrashing)", async () => {
mockUpsertMachine.mockResolvedValue({});
mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
mockSweepStaleState.mockResolvedValue(undefined);
@@ -848,7 +848,7 @@ describe("POST /api/daemon/tasks/poll", () => {
await POST(postReq({ daemon_id: "d1" }));
- expect(mockInvalidate).toHaveBeenCalledWith("runtimes:w1");
+ expect(mockInvalidate).not.toHaveBeenCalledWith("runtimes:w1");
});
it("writes KV heartbeat with 120s TTL", async () => {
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 eab59f592..180020d09 100644
--- a/src/web/src/app/api/daemon/tasks/poll/route.ts
+++ b/src/web/src/app/api/daemon/tasks/poll/route.ts
@@ -12,9 +12,9 @@ import { broadcastToUser } from "@/lib/broadcast";
import { log } from "@/lib/logger";
export const POST = withAuth(async (req: NextRequest, ctx) => {
- const { env } = getCloudflareContext();
+ const { env, ctx: cfCtx } = getCloudflareContext();
const db = getDb((env as Env).DB);
- const { cached, cacheKeys, throttled, invalidate } = await import("@/lib/cache");
+ const { cached, cacheKeys, throttled } = await import("@/lib/cache");
const [body, err] = await parseBody(req, PollRequestSchema);
if (err) return err;
@@ -45,7 +45,6 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
new Date().toISOString(),
{ expirationTtl: 120 },
).catch(() => {});
- invalidate(cacheKeys.allRuntimes(ctx.workspaceId)).catch(() => {});
}
try {
await throttled(
@@ -70,16 +69,16 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
status: "online",
}).catch(() => {});
- // 3. Housekeeping: sweep stale state — non-critical
- try {
- await sweepStaleState(db, ctx.workspaceId);
- } catch (e) {
- log.warn("sweep failed", { workspaceId: ctx.workspaceId, err: String(e) });
- }
+ // 3. Housekeeping: sweep stale state — non-blocking (runs after response is sent)
+ cfCtx.waitUntil(
+ sweepStaleState(db, ctx.workspaceId).catch((e) => {
+ log.warn("sweep failed", { workspaceId: ctx.workspaceId, err: String(e) });
+ })
+ );
- // 3b. Promote due calendar events — throttled to once per 30s per workspace
- try {
- await throttled(`cal:${ctx.workspaceId}`, 30, async () => {
+ // 3b. Promote due calendar events — non-blocking, throttled to once per 30s per workspace
+ cfCtx.waitUntil(
+ throttled(`cal:${ctx.workspaceId}`, 30, async () => {
const enqueued = await promoteDueCalendarEventsForWorkspace(
db,
ctx.workspaceId!,
@@ -87,13 +86,13 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
if (enqueued > 0) {
log.info("calendar: enqueued", { workspaceId: ctx.workspaceId, enqueued });
}
- });
- } catch (err) {
- log.warn("calendar: promote failed", {
- workspaceId: ctx.workspaceId,
- err: String(err),
- });
- }
+ }).catch((err) => {
+ log.warn("calendar: promote failed", {
+ workspaceId: ctx.workspaceId,
+ err: String(err),
+ });
+ })
+ );
// 4. Task claiming
const taskService = new TaskService(db);
diff --git a/src/web/src/app/api/inbox/count/route.ts b/src/web/src/app/api/inbox/count/route.ts
index 0a89b601f..08705d720 100644
--- a/src/web/src/app/api/inbox/count/route.ts
+++ b/src/web/src/app/api/inbox/count/route.ts
@@ -1,17 +1,18 @@
import { NextRequest } from "next/server";
import { getCloudflareContext } from "@opennextjs/cloudflare";
import { queries } from "@alook/shared";
-import { getDb } from "@/lib/db";
+import { getReadDb } from "@/lib/db";
import { withAuth } from "@/lib/middleware/auth";
import { withWorkspaceMember } from "@/lib/middleware/workspace";
import { writeJSON } from "@/lib/middleware/helpers";
+import { cached, cacheKeys } from "@/lib/cache";
export const GET = withAuth(async (req: NextRequest, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
if (ws instanceof Response) return ws;
const { env } = getCloudflareContext();
- const db = getDb((env as Env).DB);
+ const db = getReadDb((env as Env).DB);
const VALID_TYPES = ["user_dm_message", "calendar_event", "email_notification"];
const typesParam = req.nextUrl.searchParams.get("types");
@@ -20,7 +21,9 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {
: [];
const validTypes = types.length > 0 ? types : ["user_dm_message"];
- const count = await queries.inbox.getUnreadCount(db, ctx.userId, ws.workspaceId, validTypes);
+ const count = await cached(cacheKeys.inboxCount(ctx.userId, ws.workspaceId), 60, () =>
+ queries.inbox.getUnreadCount(db, ctx.userId, ws.workspaceId, validTypes)
+ );
return writeJSON({ count });
});
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 9b64e2014..62bb2b5a8 100644
--- a/src/web/src/app/api/inbox/read-all/route.ts
+++ b/src/web/src/app/api/inbox/read-all/route.ts
@@ -4,6 +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";
export const POST = withAuth(async (req: NextRequest, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
@@ -13,6 +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(() => {});
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 ce623534e..c5879c035 100644
--- a/src/web/src/app/api/inbox/read/route.ts
+++ b/src/web/src/app/api/inbox/read/route.ts
@@ -4,6 +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";
export const POST = withAuth(async (req: NextRequest, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
@@ -28,6 +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(() => {});
return new NextResponse(null, { status: 204 });
});
diff --git a/src/web/src/app/api/runtimes/route.test.ts b/src/web/src/app/api/runtimes/route.test.ts
index 101ea3b4d..f0e085b36 100644
--- a/src/web/src/app/api/runtimes/route.test.ts
+++ b/src/web/src/app/api/runtimes/route.test.ts
@@ -6,13 +6,19 @@ vi.mock("@opennextjs/cloudflare", () => ({
}));
const mockListAgentRuntimes = vi.fn();
-const mockSweepStaleState = vi.fn();
-vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }));
+vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})), getReadDb: vi.fn(() => ({})) }));
+
+vi.mock("@/lib/cache", () => ({
+ cached: vi.fn((_key: string, _ttl: number, fn: () => any) => fn()),
+ cacheKeys: {
+ allRuntimes: (ws: string) => `runtimes:${ws}`,
+ heartbeat: (ws: string, id: string) => `hb:${ws}:${id}`,
+ },
+}));
vi.mock("@alook/shared", () => ({
createDb: vi.fn(() => ({})),
- createLogger: vi.fn(() => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn() })),
queries: {
runtime: {
listAgentRuntimes: (...args: unknown[]) => mockListAgentRuntimes(...args),
@@ -20,10 +26,6 @@ vi.mock("@alook/shared", () => ({
},
}));
-vi.mock("@/lib/services/sweep", () => ({
- sweepStaleState: (...args: unknown[]) => mockSweepStaleState(...args),
-}));
-
vi.mock("@/lib/middleware/auth", () => ({
withAuth: vi.fn((handler: any) => async (req: any, ctx?: any) => {
const params = ctx?.params instanceof Promise ? await ctx.params : ctx?.params;
@@ -53,7 +55,6 @@ describe("GET /api/runtimes", () => {
beforeEach(() => vi.clearAllMocks());
it("lists runtimes in workspace", async () => {
- mockSweepStaleState.mockResolvedValue(undefined);
mockListAgentRuntimes.mockResolvedValue([
{ id: "r1", status: "online" },
{ id: "r2", status: "offline" },
@@ -69,15 +70,4 @@ describe("GET /api/runtimes", () => {
]);
expect(mockListAgentRuntimes).toHaveBeenCalledWith({}, "w1");
});
-
- it("calls sweepStaleState before listing", async () => {
- mockSweepStaleState.mockResolvedValue(undefined);
- mockListAgentRuntimes.mockResolvedValue([]);
-
- const req = new NextRequest("http://localhost/api/runtimes");
- await GET(req, {} as any);
-
- expect(mockSweepStaleState).toHaveBeenCalledWith({}, "w1");
- expect(mockSweepStaleState).toHaveBeenCalledBefore(mockListAgentRuntimes);
- });
});
diff --git a/src/web/src/app/api/runtimes/route.ts b/src/web/src/app/api/runtimes/route.ts
index 068a2e799..7611ce283 100644
--- a/src/web/src/app/api/runtimes/route.ts
+++ b/src/web/src/app/api/runtimes/route.ts
@@ -1,40 +1,33 @@
import { getCloudflareContext } from "@opennextjs/cloudflare"
-import { queries, createLogger } from "@alook/shared"
-import { getDb } from "@/lib/db"
+import { queries } from "@alook/shared"
+import { getReadDb } from "@/lib/db"
import { withAuth } from "@/lib/middleware/auth";
import { withWorkspaceMember } from "@/lib/middleware/workspace";
import { writeJSON } from "@/lib/middleware/helpers";
import { runtimeToResponse } from "@/lib/api/responses";
-import { sweepStaleState } from "@/lib/services/sweep";
import { cached, cacheKeys } from "@/lib/cache";
-const log = createLogger({ service: "api:runtimes" })
-
export const GET = withAuth(async (req, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
if (ws instanceof Response) return ws;
const { env } = getCloudflareContext()
- const db = getDb((env as Env).DB)
-
- // Sweep stale state: mark offline runtimes, fail stuck tasks, reconcile agents
- try {
- await sweepStaleState(db, ws.workspaceId);
- } catch (err) {
- log.warn("sweepStaleState failed, continuing", { workspaceId: ws.workspaceId, err: String(err) })
- }
+ const db = getReadDb((env as Env).DB)
const runtimes = await cached(cacheKeys.allRuntimes(ws.workspaceId), 120, () => queries.runtime.listAgentRuntimes(db, ws.workspaceId));
- // Overlay KV heartbeats for real-time online status
+ // Overlay KV heartbeats for real-time online status (deduplicated by daemonId)
const kv = (env as Env).CACHE_KV ?? null;
if (kv) {
- await Promise.all(runtimes.map(async (rt) => {
- if (rt.daemonId) {
- const hb = await kv.get(cacheKeys.heartbeat(ws.workspaceId, rt.daemonId)).catch(() => null);
- if (hb) rt.machineLastSeenAt = hb;
- }
- }));
+ const uniqueDaemonIds = [...new Set(runtimes.map((rt) => rt.daemonId).filter(Boolean))] as string[];
+ const heartbeats = await Promise.all(
+ uniqueDaemonIds.map((id) => kv.get(cacheKeys.heartbeat(ws.workspaceId, id)).catch(() => null))
+ );
+ const hbMap = new Map(uniqueDaemonIds.map((id, i) => [id, heartbeats[i]]));
+ for (const rt of runtimes) {
+ const hb = rt.daemonId ? hbMap.get(rt.daemonId) : null;
+ if (hb) rt.machineLastSeenAt = hb;
+ }
}
return writeJSON(runtimes.map(runtimeToResponse));
diff --git a/src/web/src/contexts/agent-context.tsx b/src/web/src/contexts/agent-context.tsx
index 19d6055d3..d49c35933 100644
--- a/src/web/src/contexts/agent-context.tsx
+++ b/src/web/src/contexts/agent-context.tsx
@@ -138,7 +138,7 @@ export function AgentProvider({
useEffect(() => {
taskCountsMountedRef.current = true;
fetchTaskCounts();
- const id = setInterval(fetchTaskCounts, 5000);
+ const id = setInterval(fetchTaskCounts, 15000);
return () => {
taskCountsMountedRef.current = false;
clearInterval(id);
diff --git a/src/web/src/contexts/inbox-count-context.tsx b/src/web/src/contexts/inbox-count-context.tsx
index 09da6c14f..120f1e632 100644
--- a/src/web/src/contexts/inbox-count-context.tsx
+++ b/src/web/src/contexts/inbox-count-context.tsx
@@ -76,7 +76,7 @@ export function InboxCountProvider({ children }: { children: ReactNode }) {
}, [subscribeWs, refresh]);
useEffect(() => {
- const id = setInterval(refresh, 30_000);
+ const id = setInterval(refresh, 60_000);
return () => clearInterval(id);
}, [refresh]);
diff --git a/src/web/src/lib/cache.ts b/src/web/src/lib/cache.ts
index 9ce417f49..edbfa5447 100644
--- a/src/web/src/lib/cache.ts
+++ b/src/web/src/lib/cache.ts
@@ -156,4 +156,6 @@ export const cacheKeys = {
allAgentAccess: (workspaceId: string) => `aa:${workspaceId}`,
allRuntimes: (workspaceId: string) => `runtimes:${workspaceId}`,
allMembers: (workspaceId: string) => `members:${workspaceId}`,
+ activeTaskCounts: (workspaceId: string) => `atc:${workspaceId}`,
+ inboxCount: (userId: string, workspaceId: string) => `inbox:${userId}:${workspaceId}`,
};
diff --git a/src/web/src/lib/db.ts b/src/web/src/lib/db.ts
index 2c04dc40f..4e11a35b2 100644
--- a/src/web/src/lib/db.ts
+++ b/src/web/src/lib/db.ts
@@ -7,12 +7,18 @@ export function getDb(d1: D1Database): Database {
return createDb(session as unknown as Parameters<typeof createDb>[0])
}
+export function getReadDb(d1: D1Database): Database {
+ const session = d1.withSession("first-unconstrained")
+ return createDb(session as unknown as Parameters<typeof createDb>[0])
+}
+
export async function withD1Retry<T>(fn: () => Promise<T>, retries = 3): Promise<T> {
for (let i = 0; i <= retries; i++) {
try {
return await fn();
} catch (err) {
if (i === retries) throw err;
+ await new Promise((r) => setTimeout(r, 100 * 2 ** i));
}
}
throw new Error("unreachable");
diff --git a/src/web/src/lib/services/task.ts b/src/web/src/lib/services/task.ts
index 99957e95c..d19e4a1d9 100644
--- a/src/web/src/lib/services/task.ts
+++ b/src/web/src/lib/services/task.ts
@@ -3,6 +3,7 @@ import { queries, TASK_TYPES, MAX_TASKS_PER_TRACE } from "@alook/shared";
import { log } from "@/lib/logger";
import { broadcastToUser } from "@/lib/broadcast";
import { messageToResponse, taskToResponse } from "@/lib/api/responses";
+import { invalidate, cacheKeys } from "@/lib/cache";
const taskQueries = queries.task;
const agentQueries = queries.agent;
@@ -36,7 +37,7 @@ export class TaskService {
}
}
- return taskQueries.createTask(this.db, {
+ const task = await taskQueries.createTask(this.db, {
agentId,
runtimeId: agent.runtimeId,
workspaceId,
@@ -49,6 +50,8 @@ export class TaskService {
traceId: opts?.traceId ?? null,
parentTaskId: opts?.parentTaskId ?? null,
});
+ invalidate(cacheKeys.activeTaskCounts(workspaceId)).catch(() => {});
+ return task;
}
async claimTask(agentId: string, workspaceId: string) {
@@ -78,7 +81,7 @@ export class TaskService {
async claimTasksForRuntimes(runtimeIds: string[], maxTasks: number, workspaceId: string) {
const killTasks = await taskQueries.claimKillTasks(this.db, runtimeIds, workspaceId, maxTasks);
- let remaining = maxTasks - killTasks.length;
+ const remaining = maxTasks - killTasks.length;
const tasks = remaining > 0
? await taskQueries.listPendingTasksByRuntimes(this.db, runtimeIds, workspaceId)
@@ -87,17 +90,22 @@ export class TaskService {
const triedAgents = new Set<string>();
const claimed: NonNullable<Awaited<ReturnType<typeof this.claimTask>>>[] = [...killTasks];
+ const uniqueCandidates: { agentId: string; workspaceId: string }[] = [];
for (const candidate of tasks) {
- if (remaining <= 0) break;
-
+ if (uniqueCandidates.length >= remaining) break;
const key = `${candidate.agentId}:${candidate.workspaceId}`;
if (triedAgents.has(key)) continue;
triedAgents.add(key);
+ uniqueCandidates.push(candidate);
+ }
+
+ const results = await Promise.all(
+ uniqueCandidates.map((c) => this.claimTask(c.agentId, c.workspaceId))
+ );
- const task = await this.claimTask(candidate.agentId, candidate.workspaceId);
+ for (const task of results) {
if (task && runtimeIdSet.has(task.runtimeId)) {
claimed.push(task);
- remaining--;
}
}
@@ -296,6 +304,7 @@ export class TaskService {
const running = await taskQueries.countRunningTasks(this.db, agentId, workspaceId);
const status = running > 0 ? "working" : "idle";
await agentQueries.updateAgentStatus(this.db, agentId, workspaceId, status);
+ invalidate(cacheKeys.activeTaskCounts(workspaceId)).catch(() => {});
}
async dispatchNextBufferedMessage(conversationId: string, workspaceId: string) {