Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: filter active tasks by visible agents

Gus390ebe9df805
diff --git a/src/shared/src/db/queries/task.ts b/src/shared/src/db/queries/task.ts
index 86aa841b1..c6f3ab60a 100644
--- a/src/shared/src/db/queries/task.ts
+++ b/src/shared/src/db/queries/task.ts
@@ -506,8 +506,17 @@ export async function listActiveTaskCountsByWorkspace(
export async function listActiveTasksByWorkspace(
db: Database,
- workspaceId: string
+ workspaceId: string,
+ agentIds?: string[]
) {
+ const conditions = [
+ eq(agentTaskQueue.workspaceId, workspaceId),
+ inArray(agentTaskQueue.status, ["queued", "dispatched", "running"]),
+ ne(agentTaskQueue.type, TASK_TYPES.KILL_TASK),
+ ];
+ if (agentIds && agentIds.length > 0) {
+ conditions.push(inArray(agentTaskQueue.agentId, agentIds));
+ }
return db
.select({
id: agentTaskQueue.id,
@@ -521,13 +530,7 @@ export async function listActiveTasksByWorkspace(
})
.from(agentTaskQueue)
.leftJoin(conversation, eq(agentTaskQueue.conversationId, conversation.id))
- .where(
- and(
- eq(agentTaskQueue.workspaceId, workspaceId),
- inArray(agentTaskQueue.status, ["queued", "dispatched", "running"]),
- ne(agentTaskQueue.type, TASK_TYPES.KILL_TASK)
- )
- )
+ .where(and(...conditions))
.orderBy(desc(agentTaskQueue.createdAt))
.limit(50);
}
diff --git a/src/web/src/app/api/agents/active-tasks/route.test.ts b/src/web/src/app/api/agents/active-tasks/route.test.ts
index fb9e93b3d..e8c8edaaa 100644
--- a/src/web/src/app/api/agents/active-tasks/route.test.ts
+++ b/src/web/src/app/api/agents/active-tasks/route.test.ts
@@ -33,6 +33,7 @@ vi.mock("@/lib/cache", () => ({
cacheKeys: { allAgents: (w: string) => `ag:${w}`, allAgentAccess: (w: string) => `aa:${w}` },
}));
vi.mock("@/lib/agent-visibility", () => ({ filterVisibleAgents: vi.fn((a: any[]) => a) }));
+import { filterVisibleAgents } from "@/lib/agent-visibility";
import { GET } from "./route";
@@ -51,17 +52,33 @@ describe("GET /api/agents/active-tasks", () => {
expect(body.tasks[0]).toMatchObject({
id: "t1", agent_id: "a1", agent: { name: "Agent 1", avatarUrl: "u" }, channel: "default",
});
- expect(mockListActive).toHaveBeenCalledWith({}, "w1");
+ expect(mockListActive).toHaveBeenCalledWith({}, "w1", ["a1"]);
});
- it("yields null agent when the task's agent isn't visible", async () => {
+ it("passes visible agent IDs to the query for filtering", async () => {
+ mockGetAllAgents.mockResolvedValue([
+ { id: "a1", name: "Visible", avatarUrl: "u", visibility: "public", ownerId: "u1" },
+ { id: "a2", name: "Hidden", avatarUrl: "h", visibility: "private", ownerId: "u2" },
+ ]);
+ vi.mocked(filterVisibleAgents).mockReturnValue([
+ { id: "a1", name: "Visible", avatarUrl: "u", visibility: "public", ownerId: "u1" },
+ ]);
mockListActive.mockResolvedValue([
- { id: "t1", agentId: "hidden", prompt: "x", status: "running", type: "dm", conversationId: "c1", channel: "ops", createdAt: "d" },
+ { id: "t1", agentId: "a1", prompt: "x", status: "running", type: "dm", conversationId: "c1", channel: "ops", createdAt: "d" },
]);
+ const res = await GET(new NextRequest("http://localhost/x"), {});
+ const body = await res.json();
+ expect(mockListActive).toHaveBeenCalledWith({}, "w1", ["a1"]);
+ expect(body.tasks).toHaveLength(1);
+ expect(body.tasks[0].agent_id).toBe("a1");
+ });
+
+ it("returns empty tasks when user has no visible agents", async () => {
mockGetAllAgents.mockResolvedValue([]);
+ vi.mocked(filterVisibleAgents).mockReturnValue([]);
const res = await GET(new NextRequest("http://localhost/x"), {});
const body = await res.json();
- expect(body.tasks[0].agent).toBeNull();
- expect(body.tasks[0].channel).toBe("ops");
+ expect(body.tasks).toEqual([]);
+ expect(mockListActive).not.toHaveBeenCalled();
});
});
diff --git a/src/web/src/app/api/agents/active-tasks/route.ts b/src/web/src/app/api/agents/active-tasks/route.ts
index f6410094f..b59793928 100644
--- a/src/web/src/app/api/agents/active-tasks/route.ts
+++ b/src/web/src/app/api/agents/active-tasks/route.ts
@@ -14,14 +14,17 @@ export const GET = withAuth(async (req, ctx) => {
const { env } = getCloudflareContext();
const db = getDb((env as Env).DB);
- const tasks = await queries.task.listActiveTasksByWorkspace(db, ws.workspaceId);
const [allAgents, allAccess] = await Promise.all([
cached(cacheKeys.allAgents(ws.workspaceId), 300, () => queries.agent.getAllAgentsForWorkspace(db, ws.workspaceId)),
cached(cacheKeys.allAgentAccess(ws.workspaceId), 300, () => queries.agentAccess.getAllAgentAccessForWorkspace(db, ws.workspaceId)),
]);
const agents = filterVisibleAgents(allAgents, ctx.userId, allAccess);
+ const visibleAgentIds = agents.map((a) => a.id);
const agentMap = new Map(agents.map((a) => [a.id, { name: a.name, avatarUrl: a.avatarUrl }]));
+ if (visibleAgentIds.length === 0) return writeJSON({ tasks: [] });
+ const tasks = await queries.task.listActiveTasksByWorkspace(db, ws.workspaceId, visibleAgentIds);
+
return writeJSON({
tasks: tasks.map((t) => ({
id: t.id,