Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
feat: dm can separate users
Gus8c4884ad81ec
diff --git a/src/cli/daemon/execenv/context.ts b/src/cli/daemon/execenv/context.tsindex c9f7bbcdb..6b2c60be4 100644--- a/src/cli/daemon/execenv/context.ts+++ b/src/cli/daemon/execenv/context.ts@@ -58,6 +58,7 @@ those json are sorted by datetime in asc order.- Don't report your agent id and workspace id when not asked, it will break the image that you're a human colleague.## RULES+- DM task prompts include a \`sender\` object with the workspace member's name, email, and whether they are the agent owner (\`is_owner\`). Use this to personalize your responses.- Read @memory.md(if exists) before your action.- When you start a new task, read the last ~10 lines of today's timeline to understand what has been asked and done recently.- if you don't know the current datetime, obtain the current datetime first.diff --git a/src/cli/daemon/prompt.test.ts b/src/cli/daemon/prompt.test.tsindex 6902eaefa..70876e958 100644--- a/src/cli/daemon/prompt.test.ts+++ b/src/cli/daemon/prompt.test.ts@@ -53,4 +53,26 @@ describe("buildPrompt", () => {const parsed = JSON.parse(buildPrompt(task));expect(parsed.notice).toBeUndefined();});++ it("includes sender for DM tasks when sender is present", () => {+ const task: Task = {+ ...makeTask("Fix the login bug"),+ sender: { name: "Gus", email: "[email protected]", isOwner: true },+ };+ const parsed = JSON.parse(buildPrompt(task));+ expect(parsed.sender).toEqual({ name: "Gus", email: "[email protected]", is_owner: true });+ });++ it("omits sender when sender is undefined", () => {+ const task = makeTask("Fix the bug");+ const parsed = JSON.parse(buildPrompt(task));+ expect(parsed.sender).toBeUndefined();+ });++ it("omits sender for email tasks (sender is not set)", () => {+ const task = makeTask("New email from [email protected]: Hi", "email_notification");+ const parsed = JSON.parse(buildPrompt(task));+ expect(parsed.notice).toBeDefined();+ expect(parsed.sender).toBeUndefined();+ });});diff --git a/src/cli/daemon/prompt.ts b/src/cli/daemon/prompt.tsindex f379c95bd..8c5b60cc7 100644--- a/src/cli/daemon/prompt.ts+++ b/src/cli/daemon/prompt.ts@@ -11,6 +11,13 @@ export function buildPrompt(task: Task, attachments?: Attachment[]): string {if (task.type === "email_notification") {obj.notice = EMAIL_NOTICE;}+ if (task.sender) {+ obj.sender = {+ name: task.sender.name,+ email: task.sender.email,+ is_owner: task.sender.isOwner,+ };+ }if (attachments && attachments.length > 0) {obj.attachments = attachments.map((a) => ({path: a.path,diff --git a/src/cli/daemon/types.test.ts b/src/cli/daemon/types.test.tsindex 8158adaee..19b20ac97 100644--- a/src/cli/daemon/types.test.ts+++ b/src/cli/daemon/types.test.ts@@ -54,4 +54,24 @@ describe("fromApiTask", () => {const result = fromApiTask(baseTask);expect(result.agent).toBeUndefined();});++ it("maps sender snake_case is_owner to camelCase isOwner", () => {+ const task: TaskApi = {+ ...baseTask,+ sender: { name: "Gus", email: "[email protected]", is_owner: true },+ };+ const result = fromApiTask(task);+ expect(result.sender).toEqual({ name: "Gus", email: "[email protected]", isOwner: true });+ });++ it("maps null sender to undefined", () => {+ const task: TaskApi = { ...baseTask, sender: null };+ const result = fromApiTask(task);+ expect(result.sender).toBeUndefined();+ });++ it("maps undefined sender to undefined", () => {+ const result = fromApiTask(baseTask);+ expect(result.sender).toBeUndefined();+ });});diff --git a/src/cli/daemon/types.ts b/src/cli/daemon/types.tsindex 58119ea00..d0f23a942 100644--- a/src/cli/daemon/types.ts+++ b/src/cli/daemon/types.ts@@ -1,3 +1,9 @@+export interface TaskSender {+ name: string;+ email: string;+ isOwner: boolean;+}+export interface Task {id: string;agentId: string;@@ -11,6 +17,7 @@ export interface Task {contextKey?: string | null;context?: Record<string, unknown>;agent?: TaskAgentData;+ sender?: TaskSender;repos?: RepoData[];createdAt: string;}@@ -117,6 +124,9 @@ export function fromApiTask(api: import("@alook/shared").TaskApi): Task {agent: api.agent? { name: api.agent.name, instructions: api.agent.instructions, emailHandle: api.agent.email_handle ?? undefined, emailAddresses: api.agent.email_addresses ?? [], userEmail: api.agent.user_email ?? undefined, runtimeConfig: api.agent.runtime_config ?? undefined }: undefined,+ sender: api.sender+ ? { name: api.sender.name, email: api.sender.email, isOwner: api.sender.is_owner }+ : undefined,repos: undefined,createdAt: api.created_at,};diff --git a/src/shared/src/schemas.ts b/src/shared/src/schemas.tsindex 0417d0d7a..29c0d7afb 100644--- a/src/shared/src/schemas.ts+++ b/src/shared/src/schemas.ts@@ -84,8 +84,16 @@ export type TaskApiBase = z.infer<typeof TaskApiBaseSchema>;// API wire format — full task (claim response includes agent + prior session)// ---------------------------------------------------------------------------+export const TaskSenderApiSchema = z.object({+ name: z.string(),+ email: z.string(),+ is_owner: z.boolean(),+});+export type TaskSenderApi = z.infer<typeof TaskSenderApiSchema>;+export const TaskApiSchema = TaskApiBaseSchema.extend({agent: TaskAgentDataApiSchema.nullable().optional(),+ sender: TaskSenderApiSchema.nullable().optional(),});export type TaskApi = z.infer<typeof TaskApiSchema>;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.tsindex 9a224250e..241028915 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@@ -7,6 +7,8 @@ const mockGetMachineByDaemon = vi.fn();const mockClearPendingUpdateVersion = vi.fn();const mockGetAgent = vi.fn();const mockGetMemberByUserAndWorkspace = vi.fn();+const mockGetConversation = vi.fn();+const mockGetUser = vi.fn();const mockClaimTasksForRuntimes = vi.fn();const mockSweepStaleState = vi.fn();const mockBroadcastToUser = vi.fn();@@ -37,6 +39,12 @@ vi.mock("@alook/shared", async () => {member: {getMemberByUserAndWorkspace: (...args: unknown[]) => mockGetMemberByUserAndWorkspace(...args),},+ conversation: {+ getConversation: (...args: unknown[]) => mockGetConversation(...args),+ },+ user: {+ getUser: (...args: unknown[]) => mockGetUser(...args),+ },emailAccount: {getEmailAccountsByAgent: vi.fn().mockResolvedValue([]),},@@ -469,4 +477,128 @@ describe("POST /api/daemon/tasks/poll", () => {expect(body.tasks[0].agent.instructions).toBe("global only");});++ it("resolves sender for DM tasks (owner)", async () => {+ mockUpsertMachine.mockResolvedValue({});+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockSweepStaleState.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);+ mockClaimTasksForRuntimes.mockResolvedValue([+ { id: "t1", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c1", prompt: "hi", status: "dispatched", type: "user_dm_message" },+ ]);+ mockGetAgent.mockResolvedValue({ id: "a1", ownerId: "u1", instructions: "", name: "Bot", runtimeConfig: {} });+ mockGetConversation.mockResolvedValue({ userId: "u1" });+ mockGetUser.mockResolvedValue({ name: "Gus", email: "[email protected]" });++ const res = await POST(postReq({ daemon_id: "d1" }));+ const body = await res.json();++ expect(body.tasks[0].sender).toEqual({ name: "Gus", email: "[email protected]", is_owner: true });+ });++ it("sets is_owner=false for non-owner DM", async () => {+ mockUpsertMachine.mockResolvedValue({});+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockSweepStaleState.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);+ mockClaimTasksForRuntimes.mockResolvedValue([+ { id: "t1", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c1", prompt: "hi", status: "dispatched", type: "user_dm_message" },+ ]);+ mockGetAgent.mockResolvedValue({ id: "a1", ownerId: "owner1", instructions: "", name: "Bot", runtimeConfig: {} });+ mockGetConversation.mockResolvedValue({ userId: "u2" });+ mockGetUser.mockResolvedValue({ name: "Alice", email: "[email protected]" });++ const res = await POST(postReq({ daemon_id: "d1" }));+ const body = await res.json();++ expect(body.tasks[0].sender).toEqual({ name: "Alice", email: "[email protected]", is_owner: false });+ });++ it("returns null sender for calendar tasks", async () => {+ mockUpsertMachine.mockResolvedValue({});+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockSweepStaleState.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);+ mockClaimTasksForRuntimes.mockResolvedValue([+ { id: "t1", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c1", prompt: "standup", status: "dispatched", type: "calendar_event" },+ ]);+ mockGetAgent.mockResolvedValue({ id: "a1", ownerId: "u1", instructions: "", name: "Bot", runtimeConfig: {} });++ const res = await POST(postReq({ daemon_id: "d1" }));+ const body = await res.json();++ expect(body.tasks[0].sender).toBeNull();+ });++ it("returns null sender for email tasks", async () => {+ mockUpsertMachine.mockResolvedValue({});+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockSweepStaleState.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);+ mockClaimTasksForRuntimes.mockResolvedValue([+ { id: "t1", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c1", prompt: "new email", status: "dispatched", type: "email_notification" },+ ]);+ mockGetAgent.mockResolvedValue({ id: "a1", ownerId: "u1", instructions: "", name: "Bot", runtimeConfig: {} });++ const res = await POST(postReq({ daemon_id: "d1" }));+ const body = await res.json();++ expect(body.tasks[0].sender).toBeNull();+ });++ it("returns null sender when conversation not found", async () => {+ mockUpsertMachine.mockResolvedValue({});+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockSweepStaleState.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);+ mockClaimTasksForRuntimes.mockResolvedValue([+ { id: "t1", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c1", prompt: "hi", status: "dispatched", type: "user_dm_message" },+ ]);+ mockGetAgent.mockResolvedValue({ id: "a1", ownerId: "u1", instructions: "", name: "Bot", runtimeConfig: {} });+ mockGetConversation.mockResolvedValue(null);++ const res = await POST(postReq({ daemon_id: "d1" }));+ const body = await res.json();++ expect(body.tasks[0].sender).toBeNull();+ });++ it("returns null sender when user not found", async () => {+ mockUpsertMachine.mockResolvedValue({});+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockSweepStaleState.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);+ mockClaimTasksForRuntimes.mockResolvedValue([+ { id: "t1", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c1", prompt: "hi", status: "dispatched", type: "user_dm_message" },+ ]);+ mockGetAgent.mockResolvedValue({ id: "a1", ownerId: "u1", instructions: "", name: "Bot", runtimeConfig: {} });+ mockGetConversation.mockResolvedValue({ userId: "u1" });+ mockGetUser.mockResolvedValue(null);++ const res = await POST(postReq({ daemon_id: "d1" }));+ const body = await res.json();++ expect(body.tasks[0].sender).toBeNull();+ });++ it("caches user lookups across multiple DM tasks from the same user", async () => {+ mockUpsertMachine.mockResolvedValue({});+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockSweepStaleState.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);+ mockClaimTasksForRuntimes.mockResolvedValue([+ { id: "t1", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c1", prompt: "hi", status: "dispatched", type: "user_dm_message" },+ { id: "t2", agentId: "a1", runtimeId: "r1", workspaceId: "w1", conversationId: "c2", prompt: "yo", status: "dispatched", type: "user_dm_message" },+ ]);+ mockGetAgent.mockResolvedValue({ id: "a1", ownerId: "owner1", instructions: "", name: "Bot", runtimeConfig: {} });+ mockGetConversation.mockResolvedValue({ userId: "u1" });+ mockGetUser.mockResolvedValue({ name: "Gus", email: "[email protected]" });++ const res = await POST(postReq({ daemon_id: "d1" }));+ const body = await res.json();++ expect(body.tasks[0].sender).toEqual({ name: "Gus", email: "[email protected]", is_owner: false });+ expect(body.tasks[1].sender).toEqual({ name: "Gus", email: "[email protected]", is_owner: false });+ expect(mockGetUser).toHaveBeenCalledTimes(1);+ });});diff --git a/src/web/src/app/api/daemon/tasks/poll/route.ts b/src/web/src/app/api/daemon/tasks/poll/route.tsindex d513c8b6b..8ea40cf65 100644--- a/src/web/src/app/api/daemon/tasks/poll/route.ts+++ b/src/web/src/app/api/daemon/tasks/poll/route.ts@@ -76,11 +76,12 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {);const tasks = [];- // Per-request cache: avoids duplicate DB reads when multiple agents share the same owner+ // Per-request caches: avoid duplicate DB reads when multiple tasks share the same owner/userconst memberCache = new Map<string, { globalInstruction: string } | null>();+ const userCache = new Map<string, { name: string; email: string } | null>();for (const task of claimed) {if (task.type === "kill_task") {- tasks.push({ ...taskToResponse(task), agent: null });+ tasks.push({ ...taskToResponse(task), agent: null, sender: null });continue;}@@ -110,8 +111,29 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {}}+ // Resolve sender identity for DM tasks only+ let sender: { name: string; email: string; is_owner: boolean } | null = null;+ if (task.type === "user_dm_message" && task.conversationId) {+ const convo = await queries.conversation.getConversation(db, task.conversationId, task.workspaceId);+ if (convo?.userId) {+ if (!userCache.has(convo.userId)) {+ const u = await queries.user.getUser(db, convo.userId);+ userCache.set(convo.userId, u ? { name: u.name, email: u.email } : null);+ }+ const cachedUser = userCache.get(convo.userId);+ if (cachedUser) {+ sender = {+ name: cachedUser.name,+ email: cachedUser.email,+ is_owner: convo.userId === agent?.ownerId,+ };+ }+ }+ }+tasks.push({...taskToResponse(task),+ sender,agent: agent? {instructions,