diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 3a2c917c83e27..eb394388218d7 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -66,6 +66,7 @@ import { latestProviderTurnForAttempt, resolveProjectProviderInstanceEnabled, SECRET_REQUEST_UNSAVED_NOTE, + agentLaunchInboxRouting, } from "@t3tools/contracts"; import { getProviderOptionCurrentValue, withLegacyUltracodeEffort } from "@t3tools/shared/model"; import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; @@ -3216,11 +3217,7 @@ const make = Effect.gen(function* () { .dispatch({ type: "thread.create", inboxOrigin: { kind: "agent", id: parent.thread.id }, - ...(request.inboxRouting !== undefined - ? { inboxRouting: request.inboxRouting } - : parent.thread.inbox === undefined - ? {} - : { inboxRouting: parent.thread.inbox }), + inboxRouting: agentLaunchInboxRouting(request), createdBy: "agent", creationSource: "mcp", commandId, diff --git a/apps/server/src/mcp/toolkits/project/handlers.test.ts b/apps/server/src/mcp/toolkits/project/handlers.test.ts index c95fadd9096e2..79df1ca9e2bd4 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.test.ts @@ -416,6 +416,39 @@ it.effect("a launch with a model its provider does not advertise creates no thre }), ); +it.effect("a launch is filed in Agents unless the agent picks an inbox", () => + Effect.gen(function* () { + const launched: Array = []; + const { projectId, dependencies } = clientLaunchHarness({ + runtimeModeCeiling: "auto", + launched, + }); + const toolkit = yield* ProjectToolkit.pipe( + Effect.provide( + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), + ), + ); + const launch = (inbox?: "kameron" | "shared") => + toolkit + .handle("t3_thread_launch", { + title: "Fix", + projectId, + ...(inbox === undefined ? {} : { inbox }), + }) + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); + + yield* launch(); + yield* launch("kameron"); + yield* launch("shared"); + expect(launched.map((input) => input.inboxRouting?.audience)).toEqual([ + "agent-only", + "kameron", + "shared", + ]); + expect(launched[1]?.inboxRouting?.assignee).toEqual({ kind: "human", id: "kameron" }); + }), +); + it.effect("a launch retried with the same clientRequestId replays the first one", () => Effect.gen(function* () { const launched: Array = []; diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index 7361cfd7e9d69..57a7c7952061b 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -1,4 +1,5 @@ import { + agentLaunchInboxRouting, CommandId, type EnvironmentId, isProviderAvailable, @@ -155,6 +156,7 @@ const launchPlacedThere = Effect.fn("mcp.launchPlacedThere")(function* ( placement: _placement, delegatedFrom: _delegatedFrom, workspaceStrategy: _requested, + inbox: _inbox, ...params } = input; const launched = yield* delegation.value.launch(scope, { @@ -165,6 +167,8 @@ const launchPlacedThere = Effect.fn("mcp.launchPlacedThere")(function* ( // The workspace and modes it was admitted with go explicitly, so nothing there changes them. params: { ...params, + // Resolved here: a linked environment on an older release knows only inboxRouting. + inboxRouting: agentLaunchInboxRouting(input), modelSelection, ...(local.strategy === undefined ? {} : { workspaceStrategy: local.strategy }), runtimeMode: local.runtimeMode, @@ -404,11 +408,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { } const result = yield* ThreadMessageIntake.launchThread({ inboxOrigin: { kind: "agent", id: caller?.id ?? "agent" }, - ...(input.inboxRouting !== undefined - ? { inboxRouting: input.inboxRouting } - : caller?.inbox === undefined - ? {} - : { inboxRouting: caller.inbox }), + inboxRouting: agentLaunchInboxRouting(input), commandId, threadId, projectId, diff --git a/apps/server/src/mcp/toolkits/project/tools.ts b/apps/server/src/mcp/toolkits/project/tools.ts index ac9b2a8ef67b8..1a052a1ba75ae 100644 --- a/apps/server/src/mcp/toolkits/project/tools.ts +++ b/apps/server/src/mcp/toolkits/project/tools.ts @@ -1,6 +1,7 @@ import { McpAttachmentInput } from "../attachment/input.ts"; import { clampedPositiveInt, + InboxChoiceParameter, ThreadInboxRoutingPatch, NonNegativeInt, ModelSelection, @@ -141,6 +142,7 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", { "No project: a new Scratch folder. Not with `projectId` or `workspaceStrategy`.", }), ), + inbox: Schema.optional(InboxChoiceParameter), inboxRouting: Schema.optional(ThreadInboxRoutingPatch), title: TrimmedNonEmptyString, modelSelection: Schema.optional(ModelSelection), diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 0051813371e5b..ab49f2b6c830d 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 5930, "codexAdditionalContextBytes": 7324, - "mcpToolListJsonBytes": 49736 + "mcpToolListJsonBytes": 52637 } diff --git a/apps/web/src/components/threadActionMenu.logic.ts b/apps/web/src/components/threadActionMenu.logic.ts index 4882857251d44..328c99930d0bb 100644 --- a/apps/web/src/components/threadActionMenu.logic.ts +++ b/apps/web/src/components/threadActionMenu.logic.ts @@ -1,4 +1,5 @@ -import type { ThreadInboxRoutingPatch } from "@t3tools/contracts"; +import * as Schema from "effect/Schema"; +import { InboxChoice, inboxRoutingFor, type ThreadInboxRoutingPatch } from "@t3tools/contracts"; import type { ContextMenuItem } from "@t3tools/contracts"; import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled"; @@ -297,20 +298,11 @@ export function buildThreadActionMenuItems( ); } +const isInboxChoice = Schema.is(InboxChoice); + export function threadRoutingUpdateFromMenuAction(action: string): ThreadInboxRoutingPatch | null { - if (action === "inbox:kameron" || action === "inbox:jeff") { - const founder = action === "inbox:kameron" ? "kameron" : "jeff"; - return { - owner: founder, - assignee: { kind: "human", id: founder }, - audience: founder, - attentionKind: "action", - }; - } - if (action === "inbox:shared") - return { owner: null, assignee: null, audience: "shared", attentionKind: "FYI" }; - if (action === "inbox:agents") - return { owner: null, assignee: null, audience: "agent-only", attentionKind: "background" }; + const inbox = action.startsWith("inbox:") ? action.slice("inbox:".length) : undefined; + if (isInboxChoice(inbox)) return inboxRoutingFor(inbox); if (action === "attention:action") return { attentionKind: "action" }; if (action === "attention:FYI") return { attentionKind: "FYI" }; if (action === "attention:background") return { attentionKind: "background" }; diff --git a/packages/contracts/src/orchestratorMcp.ts b/packages/contracts/src/orchestratorMcp.ts index e7b3827987b8b..afd1ab215cd6a 100644 --- a/packages/contracts/src/orchestratorMcp.ts +++ b/packages/contracts/src/orchestratorMcp.ts @@ -279,9 +279,10 @@ export const OrchestratorMcpTaskCancelResult = Schema.Struct({ }); export type OrchestratorMcpTaskCancelResult = typeof OrchestratorMcpTaskCancelResult.Type; -import { ThreadInbox, ThreadInboxRoutingPatch } from "./threadInbox.ts"; +import { InboxChoiceParameter, ThreadInbox, ThreadInboxRoutingPatch } from "./threadInbox.ts"; export const OrchestratorMcpCreateThreadRequest = Schema.Struct({ + inbox: Schema.optional(InboxChoiceParameter), inboxRouting: Schema.optional(ThreadInboxRoutingPatch), prompt: Schema.optional(OrchestratorMcpPrompt), title: Schema.optional(OrchestratorMcpTitle), diff --git a/packages/contracts/src/threadInbox.test.ts b/packages/contracts/src/threadInbox.test.ts index bde1cf4ca7dd8..c52ba992dd605 100644 --- a/packages/contracts/src/threadInbox.test.ts +++ b/packages/contracts/src/threadInbox.test.ts @@ -10,6 +10,7 @@ import { inboxOriginFor, founderInboxLane, compositeThreadKey, + agentLaunchInboxRouting, } from "./threadInbox.ts"; const kameron = { kind: "human", id: "kameron" } as const; const jeff = { kind: "human", id: "jeff" } as const; @@ -66,6 +67,27 @@ describe("shared fleet organizational inbox", () => { false, ]); }); + it("files agent launches in Agents unless the agent picks an inbox", () => { + // A launch from a thread in Kameron's inbox no longer copies that routing. + const launched = (choice: Parameters[0]) => + makeThreadInbox({ actor: bot, routing: agentLaunchInboxRouting(choice) }); + const views = ["kameron", "jeff", "shared", "agents"] as const; + const visibleIn = (inbox: ReturnType) => + views.filter((view) => matchesInboxView(inbox, view)); + expect(visibleIn(launched({}))).toEqual(["agents"]); + expect(visibleIn(launched({ inbox: "kameron" }))).toEqual(["kameron", "agents"]); + expect(visibleIn(launched({ inbox: "jeff" }))).toEqual(["jeff", "agents"]); + expect(visibleIn(launched({ inbox: "shared" }))).toEqual(["shared", "agents"]); + expect(launched({ inbox: "kameron" }).origin).toEqual(bot); + const explicit = launched({ + inbox: "kameron", + inboxRouting: { assignee: { kind: "human", id: "jeff" } }, + }); + expect(visibleIn(explicit)).toEqual(["jeff", "agents"]); + const quieter = launched({ inbox: "kameron", inboxRouting: { attentionKind: "FYI" } }); + expect(visibleIn(quieter)).toEqual(["kameron", "agents"]); + expect(quieter.attentionKind).toBe("FYI"); + }); it("hides bot-only work by default while All and Agents retain it", () => { const inbox = makeThreadInbox({ actor: bot }); expect(matchesInboxView(inbox, "kameron")).toBe(false); diff --git a/packages/contracts/src/threadInbox.ts b/packages/contracts/src/threadInbox.ts index d9bc3b9b7eb87..2ac7787b190f5 100644 --- a/packages/contracts/src/threadInbox.ts +++ b/packages/contracts/src/threadInbox.ts @@ -34,6 +34,40 @@ export type ThreadInbox = typeof ThreadInbox.Type; export const INBOX_CAPABILITY = "closura-founder-inbox-v2" as const; export const InboxView = Schema.Literals(["kameron", "jeff", "shared", "agents", "all"]); export type InboxView = typeof InboxView.Type; +/** An inbox a thread can be filed into: every view except All. */ +export const InboxChoice = Schema.Literals(["kameron", "jeff", "shared", "agents"]); +export type InboxChoice = typeof InboxChoice.Type; + +/** The routing that files a thread into one inbox, shared by the thread menu and agent launches. */ +export function inboxRoutingFor(inbox: InboxChoice): ThreadInboxRouting { + if (inbox === "shared") + return { owner: null, assignee: null, audience: "shared", attentionKind: "FYI" }; + if (inbox === "agents") + return { owner: null, assignee: null, audience: "agent-only", attentionKind: "background" }; + return { + owner: inbox, + assignee: { kind: "human", id: inbox }, + audience: inbox, + attentionKind: "action", + }; +} + +/** + * Where an agent-launched top-level thread is filed: the chosen inbox, else Agents, so agent + * work never lands in a founder's inbox by default. `inboxRouting` adjusts the chosen inbox. + */ +export const agentLaunchInboxRouting = (input: { + readonly inbox?: InboxChoice | undefined; + readonly inboxRouting?: ThreadInboxRoutingPatch | undefined; +}): ThreadInboxRoutingPatch => + input.inbox === undefined + ? (input.inboxRouting ?? inboxRoutingFor("agents")) + : { ...inboxRoutingFor(input.inbox), ...input.inboxRouting }; + +export const InboxChoiceParameter = InboxChoice.annotate({ + description: + "Sidebar inbox for the new thread. Default 'agents'. Pick 'kameron' or 'jeff' only when that founder asked for it or must act on it (your thread's `inbox.owner` in t3_thread_read); 'shared' for both. `inboxRouting` adjusts fields of the chosen inbox.", +}); export const compositeThreadKey = (environmentId: string, threadId: string) => JSON.stringify([environmentId, threadId]); export const InboxThreadRef = Schema.Struct({ environmentId: EnvironmentId, threadId: ThreadId });