From 77505aab8da4be96b4e66bb9b54086dea5d17ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Thu, 8 Oct 2026 02:19:48 -0400 Subject: [PATCH] Q1: keep other work's chat mentions out of task prompts Every mention already wakes the agent through its own chat-wake task, so ordinary tasks no longer inject the org-wide mention inbox. Co-Authored-By: Claude Opus 5.5 --- src/main/services/task-execution-service.ts | 23 ------- src/main/services/task-prompts.ts | 14 ---- tests/integration/task-context-scope.test.ts | 72 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 tests/integration/task-context-scope.test.ts diff --git a/src/main/services/task-execution-service.ts b/src/main/services/task-execution-service.ts index 69e577bf..b2d976f3 100644 --- a/src/main/services/task-execution-service.ts +++ b/src/main/services/task-execution-service.ts @@ -15,7 +15,6 @@ import { buildAgentInstructions, buildTaskPrompt, type ChatContextBlock, - type InboxContextBlock, type GoalRow, type ProjectRow, type SkillRuntimeSnapshotRow, @@ -142,7 +141,6 @@ export class TaskExecutionService { workspace: workspace ? { id: workspace.id, localPath: workspace.local_path, writeAccess: task.requires_filesystem === 1 } : null, }; const chatContext = this.loadChatContext(orgId, isolation); - const inboxContext = chatContext ? null : this.loadInboxContext(orgId, agent.id); // The CEO is the c_suite role — the same predicate the grant system // enforces — not "has no manager", which any board hire can match. const isCeo = agent.role_level === "c_suite"; @@ -179,7 +177,6 @@ export class TaskExecutionService { hasReports, managerName: this.agentName(orgId, agent.manager_agent_id), chatContext, - inboxContext, priorCompletedRuns: this.completedRunCount(task.id, runId), }), toolContext, @@ -555,26 +552,6 @@ export class TaskExecutionService { }; } - private loadInboxContext(orgId: string, agentId: string): InboxContextBlock | null { - const messages = this.db.prepare(` - select m.body_md, - coalesce(a.name, e.display_name, m.sender_type) as sender_name, - s.name as channel_name - from chat_mentions cm - join chat_messages m on m.id = cm.message_id - join chat_spaces s on s.id = m.space_id - left join agents a on a.id = m.sender_id and m.sender_type = 'agent' - left join employees e on e.id = m.sender_id and m.sender_type = 'employee' - where cm.target_type = 'agent' and cm.target_id = ? and m.org_id = ? and m.deleted_at is null - order by m.created_at desc - limit 10 - `).all(agentId, orgId) as Array<{ body_md: string; sender_name: string; channel_name: string }>; - if (messages.length === 0) return null; - return { - transcript: messages.reverse().map((m) => `[${m.sender_name} in #${m.channel_name}]: ${m.body_md}`).join("\n"), - }; - } - private completedRunCount(taskId: string, excludingRunId: string): number { const row = this.db.prepare("select count(*) as n from task_runs where task_id = ? and state = 'completed' and id <> ?") .get(taskId, excludingRunId) as { n: number }; diff --git a/src/main/services/task-prompts.ts b/src/main/services/task-prompts.ts index e00329ac..570c46d8 100644 --- a/src/main/services/task-prompts.ts +++ b/src/main/services/task-prompts.ts @@ -150,10 +150,6 @@ function roleVoice(input: AgentInstructionContext): string { return "You own your craft. Ask the question early instead of guessing, do the work properly, and when you are stuck for more than a few minutes say so in the thread instead of spinning in place."; } -export interface InboxContextBlock { - transcript: string; -} - export function buildTaskPrompt(input: { task: TaskExecutionTaskRow; agent: TaskExecutionAgentRow; @@ -167,7 +163,6 @@ export function buildTaskPrompt(input: { hasReports: boolean; managerName: string | null; chatContext: ChatContextBlock | null; - inboxContext: InboxContextBlock | null; /** Completed runs this task already spent without reaching done. */ priorCompletedRuns: number; }): string { @@ -220,15 +215,6 @@ export function buildTaskPrompt(input: { ); return lines.join("\n"); } - if (input.inboxContext) { - lines.push( - "", - "Recent messages that mention you:", - input.inboxContext.transcript, - "", - "Respond to anyone above who needs your input before starting the task.", - ); - } if (input.task.requires_filesystem === 1 && input.workspace) { lines.push( "", diff --git a/tests/integration/task-context-scope.test.ts b/tests/integration/task-context-scope.test.ts new file mode 100644 index 00000000..e81006ae --- /dev/null +++ b/tests/integration/task-context-scope.test.ts @@ -0,0 +1,72 @@ +import { randomUUID } from "node:crypto"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, expect, test } from "vitest"; +import { DatabaseConnection } from "../../src/main/database/connection"; +import { OnboardingService } from "../../src/main/services/onboarding-service"; +import { ProviderService } from "../../src/main/services/provider-service"; +import { providerAdapters } from "../../src/main/services/provider-adapters"; +import { TaskExecutionService } from "../../src/main/services/task-execution-service"; +import { claimNextQueuedRun } from "../../src/main/app/task-queue-worker"; +import { canonicalMentionHandle } from "../../src/shared/mention-handles"; +import type { AgentTurnResult, ProviderToolTurnInput, ToolCall } from "../../src/main/services/provider-tool-turn"; + +const testVault = { + encryptSecret: (secret: string) => Buffer.from(secret, "utf8"), + decryptSecret: (ciphertext: Buffer) => ciphertext.toString("utf8"), +}; + +const CHANNEL_NOTE = "unrelated: rotate the staging certificates tonight"; + +function call(name: string, args: Record): ToolCall { + return { id: `call_${randomUUID()}`, name, arguments: args }; +} + +describe("task prompt scope", () => { + test("an ordinary task prompt carries no chat mentions from other work", async () => { + const handle = canonicalMentionHandle("Dana Lee"); + const danaPrompts: string[] = []; + let ceo = 0; + const ceoScript = [ + { text: "Staffing.", toolCalls: [call("hire_agent", { name: "Dana Lee", role: "Engineer", department: "Engineering" })] }, + { text: "Delegating.", toolCalls: [ + call("create_task", { title: "Build the booking API", description: "Ship the booking API.", assignee_agent_name: "Dana Lee" }), + call("send_message", { channel: "engineering", body: `@${handle} ${CHANNEL_NOTE}` }), + ] }, + { text: "Done.", toolCalls: [] }, + ]; + const turn = async (input: ProviderToolTurnInput): Promise => { + if ((input.instructions ?? "").includes("Dana Lee")) { + const first = input.messages.find((message) => message.role === "user"); + if (first && first.role === "user") danaPrompts.push(first.text); + return { text: "done", toolCalls: [], rawResponseJson: "{}" }; + } + const step = ceoScript[ceo] ?? { text: "done", toolCalls: [] }; + ceo += 1; + return { text: step.text, toolCalls: step.toolCalls, rawResponseJson: "{}" }; + }; + const connection = new DatabaseConnection(join(tmpdir(), `ac-scope-${randomUUID()}.db`)); + const providers = new ProviderService(connection.db, providerAdapters, testVault, undefined, turn); + const onboarding = new OnboardingService({ db: connection.db, saveProviderCredential: (input) => providers.saveCredential(input) }); + const execution = new TaskExecutionService(connection.db, providers); + let orgId = ""; + connection.transaction(() => { + orgId = onboarding.bootstrap({ + companyName: "OrbitOps AI", companyDescription: "Ops.", employeeName: "Yang", workspacePath: "", + executionMode: "ai_ceo", autoApproveHires: true, reviewDeliverables: false, + goalTitle: "Stand up a service business.", goalDescription: "Win customers.", + providerKey: "openai", providerSecret: "sk-test-key", providerModel: "gpt-5.4", + }).orgId; + }); + const kickoff = claimNextQueuedRun(connection); + await execution.executeRun(kickoff!.org_id, kickoff!.id); + + const task = connection.db.prepare("select id from tasks where org_id = ? and title = 'Build the booking API'").get(orgId) as { id: string }; + await execution.runTask({ orgId, taskId: task.id }); + + expect(danaPrompts).toHaveLength(1); + expect(danaPrompts[0]).toContain("Build the booking API"); + expect(danaPrompts[0]).not.toContain(CHANNEL_NOTE); + connection.close(); + }); +});