🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
refactor(in-app-agent): Post background execution refactoring - deepen background interfaces (#15980)
Lysander Kieselfb4a4f12bf2d
diff --git a/packages/shared/AGENTS.md b/packages/shared/AGENTS.mdindex b08760d0d1..e508b3eb42 100644--- a/packages/shared/AGENTS.md+++ b/packages/shared/AGENTS.md@@ -55,7 +55,8 @@signature helpers for secrets and signed payloads.- `@langfuse/shared/query` via `src/features/query/index.ts`: dashboard query feature.- `@langfuse/shared/in-app-agent` via `src/in-app-agent/index.ts`:- client-safe in-app-agent contracts (AG-UI schemas, constants, id helpers).+ client-safe in-app-agent contracts (AG-UI schemas, constants, id helpers,+ and durable interrupt parsing shared by browser and server runtimes).`AgUiRunAgentInput` is a compile-time-only execution contract; there is noruntime input or browser runtime-state schema. Never re-export server codehere.diff --git a/packages/shared/src/in-app-agent/index.ts b/packages/shared/src/in-app-agent/index.tsindex b48f743f6a..7231aa70f8 100644--- a/packages/shared/src/in-app-agent/index.ts+++ b/packages/shared/src/in-app-agent/index.ts@@ -5,4 +5,5 @@ export * from "./schema";export * from "./backgroundWatch";export * from "./constants";export * from "./ids";+export * from "./interrupts";export * from "./messages";diff --git a/packages/shared/src/in-app-agent/interrupts.ts b/packages/shared/src/in-app-agent/interrupts.tsnew file mode 100644index 0000000000..69d986a280--- /dev/null+++ b/packages/shared/src/in-app-agent/interrupts.ts@@ -0,0 +1,37 @@+import { EventType } from "@ag-ui/core";+import { z } from "zod";++import { safeJsonParse } from "../utils/json";+import type { InAppAgentToolApprovalRequest } from "./schema";++const MastraSuspendEventSchema = z.object({+ type: z.literal("mastra_suspend"),+ toolCallId: z.string().min(1),+ toolName: z.string().min(1),+ args: z.unknown().optional(),+ runId: z.string().min(1),+});++/** Parses the durable interrupt event shape in browser and server runtimes. */+export function parseInAppAgentInterruptEvent(+ event: unknown,+): InAppAgentToolApprovalRequest | undefined {+ if (+ !event ||+ typeof event !== "object" ||+ !("type" in event) ||+ event.type !== EventType.CUSTOM ||+ !("name" in event) ||+ event.name !== "on_interrupt"+ ) {+ return undefined;+ }++ const value = "value" in event ? event.value : undefined;+ const parsedValue = typeof value === "string" ? safeJsonParse(value) : value;+ const interrupt = MastraSuspendEventSchema.safeParse(parsedValue);++ return interrupt.success+ ? { ...interrupt.data, type: "tool_approval_request" }+ : undefined;+}diff --git a/packages/shared/src/in-app-agent/server/agent.ts b/packages/shared/src/in-app-agent/server/agent.tsindex b447c48f98..9227e499a7 100644--- a/packages/shared/src/in-app-agent/server/agent.ts+++ b/packages/shared/src/in-app-agent/server/agent.ts@@ -30,16 +30,18 @@ import { getToolFailureMessage } from "./toolErrors";import {createSandboxTools,createRedirectActionTool,+ getToolCallId,+ hasCallableExecute,+ withOptionalSilentMcpOutput,+} from "./tools";+import type { CompletedInAppAgentMcpToolCall } from "./toolResults";+import {filterInAppAgentAvailableLangfuseMcpTools,getInAppAgentMcpAllowedToolNames,getInAppAgentRegistryToolName,- getToolCallId,- hasCallableExecute,- type CompletedInAppAgentMcpToolCall,type InAppAgentToolPolicy,- withOptionalSilentMcpOutput,withInAppAgentToolApproval,-} from "./tools";+} from "./mcpPolicy";import { toPublicEvent } from "./watch";import { LANGFUSE_IN_APP_AGENT_SKILLS } from "./skills";import type { InAppAgentSandbox } from "./sandbox";diff --git a/packages/shared/src/in-app-agent/server/human-in-the-loop.ts b/packages/shared/src/in-app-agent/server/human-in-the-loop.tsindex 43f7d23f15..9825e84fcd 100644--- a/packages/shared/src/in-app-agent/server/human-in-the-loop.ts+++ b/packages/shared/src/in-app-agent/server/human-in-the-loop.ts@@ -5,8 +5,7 @@ import { IN_APP_AGENT_TOOL_REJECTION_ERROR_CODE } from "../constants";import {IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES,type InAppAgentLangfuseMcpToolName,-} from "./tools";-import { safeJsonParse } from "../../utils/json";+} from "./mcpPolicy";import {type AgUiEvent,type AgUiMessage,@@ -45,14 +44,6 @@ export const InAppAgentMcpRunOverrideSchema = z"toolNames" in override ? override.toolNames : [override.toolName],}));-const MastraSuspendEventSchema = z.object({- type: z.literal("mastra_suspend"),- toolCallId: z.string().min(1),- toolName: z.string().min(1),- args: z.unknown().optional(),- runId: z.string().min(1),-});-export async function createInAppAgentMcpRunOverride(params: {toolNames: InAppAgentLangfuseMcpToolName[];}) {@@ -63,23 +54,6 @@ export async function createInAppAgentMcpRunOverride(params: {});}-export function parseInAppAgentInterruptEvent(- event: AgUiEvent,-): InAppAgentToolApprovalRequest | undefined {- if (event.type !== EventType.CUSTOM || event.name !== "on_interrupt") {- return undefined;- }-- const value = event.value;- const interrupt =- typeof value === "string" ? safeJsonParse(value) : (value as unknown);- const parsedInterrupt = MastraSuspendEventSchema.safeParse(interrupt);-- return parsedInterrupt.success- ? { ...parsedInterrupt.data, type: "tool_approval_request" }- : undefined;-}-export type ManualToolApprovalRunInput = {input: AgUiRunAgentInput;syntheticEvents: AgUiEvent[];diff --git a/packages/shared/src/in-app-agent/server/index.ts b/packages/shared/src/in-app-agent/server/index.tsindex f1cd5b645f..755abe5ee8 100644--- a/packages/shared/src/in-app-agent/server/index.ts+++ b/packages/shared/src/in-app-agent/server/index.ts@@ -3,6 +3,8 @@export * from "./access";export * from "./agent";export * from "./tools";+export * from "./mcpPolicy";+export * from "./toolResults";export * from "./human-in-the-loop";export * from "./instrumentation";export * from "./eventCompaction";diff --git a/packages/shared/src/in-app-agent/server/instrumentation.ts b/packages/shared/src/in-app-agent/server/instrumentation.tsindex 0224f977d7..b3914a9d01 100644--- a/packages/shared/src/in-app-agent/server/instrumentation.ts+++ b/packages/shared/src/in-app-agent/server/instrumentation.ts@@ -20,7 +20,7 @@ import {normalizeToolOutput,parseJsonOrString,} from "./toolErrors";-import type { InAppAgentUserAccess } from "./tools";+import type { InAppAgentUserAccess } from "./mcpPolicy";import { assertUnreachable } from "../../utils/typeChecks";export type InAppAgentTracingConfig = {diff --git a/packages/shared/src/in-app-agent/server/mcpPolicy.ts b/packages/shared/src/in-app-agent/server/mcpPolicy.tsnew file mode 100644index 0000000000..23402e96d7--- /dev/null+++ b/packages/shared/src/in-app-agent/server/mcpPolicy.ts@@ -0,0 +1,534 @@+import type { ProjectScope } from "../../features/rbac/projectAccessRights";+import { hasProjectAccessByRole } from "../../features/rbac/projectAccessRights";+import { Role } from "../../db";+import { IN_APP_AGENT_REDIRECT_TOOL_NAME } from "../constants";++type InAppAgentMcpToolApproval = "auto" | "approval";++export type InAppAgentUserAccess = {+ projectRole?: Role;+ // Global Langfuse admin flag. This bypasses project membership checks.+ isAdmin: boolean;+};++type InAppAgentMcpToolPolicy = {+ approval: InAppAgentMcpToolApproval;+ availability: {+ scope: ProjectScope;+ };+};++// Exhaustive approval policy for Langfuse MCP tools. Keys use the unprefixed+// MCP registry names and are the source of truth for the tool-name type below.+// Exhaustiveness against web's MCP toolRegistry is enforced by type and runtime+// assertions in web's in-app-agent stream servertest, so new MCP tools must be+// classified before the in-app agent can auto/approval-gate them.+export const IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES = {+ listAnnotationQueues: {+ approval: "auto",+ availability: { scope: "annotationQueues:read" },+ },+ createAnnotationQueue: {+ approval: "approval",+ availability: { scope: "annotationQueues:CUD" },+ },+ getAnnotationQueue: {+ approval: "auto",+ availability: { scope: "annotationQueues:read" },+ },+ listAnnotationQueueItems: {+ approval: "auto",+ availability: { scope: "annotationQueues:read" },+ },+ getAnnotationQueueItem: {+ approval: "auto",+ availability: { scope: "annotationQueues:read" },+ },+ createAnnotationQueueItem: {+ approval: "approval",+ availability: { scope: "annotationQueues:CUD" },+ },+ updateAnnotationQueueItem: {+ approval: "approval",+ availability: { scope: "annotationQueues:CUD" },+ },+ deleteAnnotationQueueItem: {+ approval: "approval",+ availability: { scope: "annotationQueues:CUD" },+ },+ createAnnotationQueueAssignment: {+ approval: "approval",+ availability: { scope: "annotationQueueAssignments:CUD" },+ },+ deleteAnnotationQueueAssignment: {+ approval: "approval",+ availability: { scope: "annotationQueueAssignments:CUD" },+ },+ createComment: {+ approval: "approval",+ availability: { scope: "comments:CUD" },+ },+ listComments: {+ approval: "auto",+ availability: { scope: "comments:read" },+ },+ getComment: {+ approval: "auto",+ availability: { scope: "comments:read" },+ },+ upsertDataset: {+ approval: "approval",+ availability: { scope: "datasets:CUD" },+ },+ listDatasets: {+ approval: "auto",+ availability: { scope: "datasets:read" },+ },+ getDataset: {+ approval: "auto",+ availability: { scope: "datasets:read" },+ },+ upsertDatasetItem: {+ approval: "approval",+ availability: { scope: "datasets:CUD" },+ },+ listDatasetItems: {+ approval: "auto",+ availability: { scope: "datasets:read" },+ },+ getDatasetItem: {+ approval: "auto",+ availability: { scope: "datasets:read" },+ },+ deleteDatasetItem: {+ approval: "approval",+ availability: { scope: "datasets:CUD" },+ },+ createDatasetRunItem: {+ approval: "approval",+ availability: { scope: "datasets:CUD" },+ },+ listDatasetRunItems: {+ approval: "auto",+ availability: { scope: "datasets:read" },+ },+ listDatasetRuns: {+ approval: "auto",+ availability: { scope: "datasets:read" },+ },+ getDatasetRun: {+ approval: "auto",+ availability: { scope: "datasets:read" },+ },+ deleteDatasetRun: {+ approval: "approval",+ availability: { scope: "datasets:CUD" },+ },+ listEvaluators: {+ approval: "auto",+ availability: { scope: "evalTemplate:read" },+ },+ getEvaluator: {+ approval: "auto",+ availability: { scope: "evalTemplate:read" },+ },+ upsertEvaluator: {+ approval: "approval",+ availability: { scope: "evalTemplate:CUD" },+ },+ deleteEvaluator: {+ approval: "approval",+ availability: { scope: "evalTemplate:CUD" },+ },+ listEvaluationRules: {+ approval: "auto",+ availability: { scope: "evalJob:read" },+ },+ getEvaluationRule: {+ approval: "auto",+ availability: { scope: "evalJob:read" },+ },+ createEvaluationRule: {+ approval: "approval",+ availability: { scope: "evalJob:CUD" },+ },+ updateEvaluationRule: {+ approval: "approval",+ availability: { scope: "evalJob:CUD" },+ },+ deleteEvaluationRule: {+ approval: "approval",+ availability: { scope: "evalJob:CUD" },+ },+ listExperiments: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ listExperimentItems: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ submitFeedback: {+ approval: "approval",+ availability: { scope: "project:read" },+ },+ getHealth: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getMedia: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ queryMetrics: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getMetricsSchema: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ listModels: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ createModel: {+ approval: "approval",+ availability: { scope: "models:CUD" },+ },+ getModel: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ deleteModel: {+ approval: "approval",+ availability: { scope: "models:CUD" },+ },+ listObservations: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getObservation: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getObservationFieldSchema: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getObservationFilterSchema: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getObservationFilterValues: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getPrompt: {+ approval: "auto",+ availability: { scope: "prompts:read" },+ },+ getPromptUnresolved: {+ approval: "auto",+ availability: { scope: "prompts:read" },+ },+ listAlerts: {+ approval: "auto",+ availability: { scope: "alerts:read" },+ },+ getAlert: {+ approval: "auto",+ availability: { scope: "alerts:read" },+ },+ listPrompts: {+ approval: "auto",+ availability: { scope: "prompts:read" },+ },+ createTextPrompt: {+ approval: "approval",+ availability: { scope: "prompts:CUD" },+ },+ createChatPrompt: {+ approval: "approval",+ availability: { scope: "prompts:CUD" },+ },+ updatePromptLabels: {+ approval: "approval",+ availability: { scope: "prompts:CUD" },+ },+ listScores: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ getScore: {+ approval: "auto",+ availability: { scope: "project:read" },+ },+ createScore: {+ approval: "approval",+ availability: { scope: "scores:CUD" },+ },+ listScoreConfigs: {+ approval: "auto",+ availability: { scope: "scoreConfigs:read" },+ },+ getScoreConfig: {+ approval: "auto",+ availability: { scope: "scoreConfigs:read" },+ },+ createScoreConfig: {+ approval: "approval",+ availability: { scope: "scoreConfigs:CUD" },+ },+ updateScoreConfig: {+ approval: "approval",+ availability: { scope: "scoreConfigs:CUD" },+ },+ deleteScoreConfig: {+ approval: "approval",+ availability: { scope: "scoreConfigs:CUD" },+ },+ createDashboardWidget: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ listDashboardWidgets: {+ approval: "auto",+ availability: { scope: "dashboards:read" },+ },+ getDashboardWidget: {+ approval: "auto",+ availability: { scope: "dashboards:read" },+ },+ updateDashboardWidget: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ deleteDashboardWidget: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ listDashboards: {+ approval: "auto",+ availability: { scope: "dashboards:read" },+ },+ getDashboard: {+ approval: "auto",+ availability: { scope: "dashboards:read" },+ },+ createDashboard: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ updateDashboard: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ deleteDashboard: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ addDashboardPlacement: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ updateDashboardPlacement: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+ deleteDashboardPlacement: {+ approval: "approval",+ availability: { scope: "dashboards:CUD" },+ },+} satisfies Record<string, InAppAgentMcpToolPolicy>;++export type InAppAgentLangfuseMcpToolName =+ keyof typeof IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES;++/** Durable grants retain the MCP surface prefix to avoid cross-surface collisions. */+export type InAppAgentPrefixedLangfuseMcpToolName =+ `langfuse_${InAppAgentLangfuseMcpToolName}`;++export const IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES =+ new Set<InAppAgentLangfuseMcpToolName>(+ Object.keys(+ IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES,+ ) as InAppAgentLangfuseMcpToolName[],+ );++export const IN_APP_AGENT_AUTO_APPROVED_EXTERNAL_TOOL_NAMES = new Set([+ IN_APP_AGENT_REDIRECT_TOOL_NAME,+]);++export const IN_APP_AGENT_SANDBOX_TOOL_NAMES = new Set([+ "read",+ "write",+ "edit",+ "bash",+]);++// Runtime-owned tools never suspend; documentation tools are approved by prefix.+const IN_APP_AGENT_LOCAL_AUTO_APPROVED_TOOL_NAMES = new Set<string>([+ ...IN_APP_AGENT_AUTO_APPROVED_EXTERNAL_TOOL_NAMES,+ ...IN_APP_AGENT_SANDBOX_TOOL_NAMES,+]);++export function isMcpToolName(+ input: string,+): input is InAppAgentLangfuseMcpToolName {+ return IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES.has(+ input as InAppAgentLangfuseMcpToolName,+ );+}++export function getInAppAgentRegistryToolName(+ toolName: string | undefined,+): InAppAgentLangfuseMcpToolName | undefined {+ if (!toolName?.startsWith("langfuse_")) {+ return undefined;+ }++ const registryToolName = toolName.slice("langfuse_".length);++ return isMcpToolName(registryToolName) ? registryToolName : undefined;+}++export function getInAppAgentPrefixedToolName(+ toolName: string | undefined,+): InAppAgentPrefixedLangfuseMcpToolName | undefined {+ const registryToolName = getInAppAgentRegistryToolName(toolName);++ return registryToolName ? `langfuse_${registryToolName}` : undefined;+}++function isInAppAgentLangfuseMcpToolAvailable(params: {+ toolName: InAppAgentLangfuseMcpToolName;+ userAccess?: InAppAgentUserAccess;+}): boolean {+ if (!params.userAccess) {+ return false;+ }++ const policy = IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[params.toolName];++ if (!policy) {+ return false;+ }++ return hasProjectAccessByRole({+ role: params.userAccess.projectRole ?? Role.MEMBER,+ admin: params.userAccess.isAdmin,+ scope: policy.availability.scope,+ });+}++export type InAppAgentToolPolicy = {+ readonly available: ReadonlySet<InAppAgentLangfuseMcpToolName>;+ readonly autoApproved: ReadonlySet<InAppAgentLangfuseMcpToolName>;+};++export function createInAppAgentToolPolicy(params: {+ userAccess?: InAppAgentUserAccess;+ alwaysAllowedTools?: Iterable<string>;+}): InAppAgentToolPolicy {+ const available = new Set<InAppAgentLangfuseMcpToolName>();+ const autoApproved = new Set<InAppAgentLangfuseMcpToolName>();++ for (const toolName of IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES) {+ if (+ !isInAppAgentLangfuseMcpToolAvailable({+ toolName,+ userAccess: params.userAccess,+ })+ ) {+ continue;+ }++ available.add(toolName);++ if (IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[toolName].approval === "auto") {+ autoApproved.add(toolName);+ }+ }++ for (const prefixedToolName of params.alwaysAllowedTools ?? []) {+ const toolName = getInAppAgentRegistryToolName(prefixedToolName);++ if (toolName && available.has(toolName)) {+ autoApproved.add(toolName);+ }+ }++ return { available, autoApproved };+}++export function getInAppAgentMcpAllowedToolNames(+ policy: InAppAgentToolPolicy,+ oneOffToolName?: InAppAgentLangfuseMcpToolName,+): InAppAgentLangfuseMcpToolName[] {+ const allowed = new Set<InAppAgentLangfuseMcpToolName>();++ // Keep the current approval first for legacy web pods that accept one tool.+ if (oneOffToolName && policy.available.has(oneOffToolName)) {+ allowed.add(oneOffToolName);+ }++ for (const toolName of policy.autoApproved) {+ if (+ IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[toolName].approval === "approval"+ ) {+ allowed.add(toolName);+ }+ }++ return [...allowed];+}++export function filterInAppAgentAvailableLangfuseMcpTools<TTool>(params: {+ tools: Partial<Record<InAppAgentLangfuseMcpToolName, TTool>> | undefined;+ policy: InAppAgentToolPolicy;+}): Partial<Record<InAppAgentLangfuseMcpToolName, TTool>> {+ return Object.fromEntries(+ Object.entries(params.tools ?? {}).flatMap(([toolName, tool]) => {+ if (!isMcpToolName(toolName) || !params.policy.available.has(toolName)) {+ return [];+ }++ return [[toolName, tool] as const];+ }),+ );+}++type InAppAgentTool = object;++export function withInAppAgentToolApproval<TTool extends InAppAgentTool>(+ tools: Record<string, TTool>,+ policy: InAppAgentToolPolicy,+): Record<string, TTool | (TTool & { requireApproval: true })> {+ return Object.fromEntries(+ Object.entries(tools).map(([toolName, tool]) => [+ toolName,+ isInAppAgentAutoApprovedToolName(toolName, policy)+ ? tool+ : { ...tool, requireApproval: true },+ ]),+ ) as Record<string, TTool | (TTool & { requireApproval: true })>;+}++function isInAppAgentAutoApprovedToolName(+ toolName: string,+ policy: InAppAgentToolPolicy,+): boolean {+ if (+ toolName.startsWith("langfuseDocs_") ||+ IN_APP_AGENT_LOCAL_AUTO_APPROVED_TOOL_NAMES.has(toolName)+ ) {+ return true;+ }++ const registryToolName = getInAppAgentRegistryToolName(toolName);++ return (+ registryToolName !== undefined && policy.autoApproved.has(registryToolName)+ );+}diff --git a/packages/shared/src/in-app-agent/server/persistence.ts b/packages/shared/src/in-app-agent/server/persistence.tsindex 5c327f1708..9192abab4c 100644--- a/packages/shared/src/in-app-agent/server/persistence.ts+++ b/packages/shared/src/in-app-agent/server/persistence.ts@@ -41,8 +41,8 @@ import {type CompletedInAppAgentMcpToolCall,getPublicInAppAgentMcpToolResultContent,getSandboxInAppAgentMcpToolResultContent,- IN_APP_AGENT_SANDBOX_TOOL_NAMES,-} from "./tools";+} from "./toolResults";+import { IN_APP_AGENT_SANDBOX_TOOL_NAMES } from "./mcpPolicy";import { getToolFailureMessage } from "./toolErrors";export const ACTIVE_RUN_CONFLICT_MESSAGE =@@ -394,12 +394,6 @@ export function createSandboxToolCallFileAccumulator(};}-export function getSandboxToolCallFiles(- events: readonly Omit<PersistedConversationEvent, "sequenceNumber">[],-) {- return createSandboxToolCallFileAccumulator(events).getFiles();-}-export async function getConversationMessages(params: {prisma: PrismaClient;projectId: string;@@ -408,25 +402,6 @@ export async function getConversationMessages(params: {return getMessagesFromPersistedEvents(await getConversationEvents(params));}-export async function getConversationMessagesForDisplay(params: {- prisma: PrismaClient;- projectId: string;- conversationId: string;-}) {- return getConversationMessagesForDisplayFromEvents(- await getConversationEvents(params),- );-}--export function getConversationMessagesForDisplayFromEvents(- events: readonly PersistedConversationEvent[],-) {- const messages = getMessagesFromPersistedEvents(events);- return redactSilentToolMessages(- dropEmptyAssistantMessages(dropUnpairedAssistantToolCalls(messages)),- );-}-export async function getConversationMessagesForReplay(params: {prisma: PrismaClient;projectId: string;@@ -585,16 +560,6 @@ ${JSON.stringify(transcript, null, 2)}}}-export function getMessagesFromEvents(events: readonly AgUiEvent[]) {- const accumulator = createConversationMessageAccumulator([]);-- for (const event of events) {- accumulator.processEvent(event);- }-- return accumulator.getMessages();-}-function getMessagesFromPersistedEvents(events: readonly PersistedConversationEvent[],) {diff --git a/packages/shared/src/in-app-agent/server/runLifecycle.ts b/packages/shared/src/in-app-agent/server/runLifecycle.tsindex 2f1c87c359..85dc58afaa 100644--- a/packages/shared/src/in-app-agent/server/runLifecycle.ts+++ b/packages/shared/src/in-app-agent/server/runLifecycle.ts@@ -12,7 +12,7 @@ import {IN_APP_AGENT_UNSETTLED_RUN_STATUSES,} from "../backgroundWatch";import type { AgUiEvent } from "../schema";-import type { InAppAgentPrefixedLangfuseMcpToolName } from "./tools";+import type { InAppAgentPrefixedLangfuseMcpToolName } from "./mcpPolicy";import {InAppAgentRunRequestSchema,resolveInAppAgentRootRunId,diff --git a/packages/shared/src/in-app-agent/server/toolResults.ts b/packages/shared/src/in-app-agent/server/toolResults.tsnew file mode 100644index 0000000000..a919efbd88--- /dev/null+++ b/packages/shared/src/in-app-agent/server/toolResults.ts@@ -0,0 +1,46 @@+import {+ IN_APP_AGENT_SILENT_MCP_OUTPUT_MESSAGE,+ IN_APP_AGENT_SILENT_MCP_OUTPUT_TYPE,+} from "../constants";++export type SilentInAppAgentMcpToolOutput = {+ type: typeof IN_APP_AGENT_SILENT_MCP_OUTPUT_TYPE;+ output: unknown;+};++export type CompletedInAppAgentMcpToolCall = {+ toolCallId: string;+ toolName: string;+ request: unknown;+ response: unknown;+ error: string | null;+ createdAt: Date;+};++export function getPublicInAppAgentMcpToolResultContent(content: string) {+ try {+ return isSilentInAppAgentMcpToolOutput(JSON.parse(content) as unknown)+ ? IN_APP_AGENT_SILENT_MCP_OUTPUT_MESSAGE+ : content;+ } catch {+ return content;+ }+}++export function getSandboxInAppAgentMcpToolResultContent(content: string) {+ const parsed = JSON.parse(content) as unknown;++ return isSilentInAppAgentMcpToolOutput(parsed) ? parsed.output : parsed;+}++export function isSilentInAppAgentMcpToolOutput(+ value: unknown,+): value is SilentInAppAgentMcpToolOutput {+ return (+ typeof value === "object" &&+ value !== null &&+ "type" in value &&+ value.type === IN_APP_AGENT_SILENT_MCP_OUTPUT_TYPE &&+ "output" in value+ );+}diff --git a/packages/shared/src/in-app-agent/server/tools.test.ts b/packages/shared/src/in-app-agent/server/tools.test.tsindex 652d06133a..a2cad946d4 100644--- a/packages/shared/src/in-app-agent/server/tools.test.ts+++ b/packages/shared/src/in-app-agent/server/tools.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { createInAppAgentToolPolicy } from "./tools";+import { createInAppAgentToolPolicy } from "./mcpPolicy";describe("createInAppAgentToolPolicy", () => {it("drops a stored grant the user's role no longer covers", () => {diff --git a/packages/shared/src/in-app-agent/server/tools.ts b/packages/shared/src/in-app-agent/server/tools.tsindex 6dfc293622..70e02a24dd 100644--- a/packages/shared/src/in-app-agent/server/tools.ts+++ b/packages/shared/src/in-app-agent/server/tools.ts@@ -4,10 +4,6 @@ import {} from "@mastra/core/schema";import { createTool, Tool } from "@mastra/core/tools";import type { InAppAgentSandbox } from "./sandbox";-import {- hasProjectAccessByRole,- type ProjectScope,-} from "../../features/rbac/projectAccessRights";import { assertUnreachable } from "../../utils/typeChecks";import {buildDashboardsPath,@@ -30,544 +26,17 @@ import {import z from "zod";import { TABLE_AGGREGATION_OPTIONS } from "../../utils/dateRanges";import { ObservationLevelDomain, TracingSearchType } from "../../index";-import { Role } from "../../db";import {IN_APP_AGENT_REDIRECT_TOOL_NAME,IN_APP_AGENT_SILENT_MCP_OUTPUT_MESSAGE,IN_APP_AGENT_SILENT_MCP_OUTPUT_TYPE,} from "../constants";import { getToolFailureMessage } from "./toolErrors";--type InAppAgentMcpToolApproval = "auto" | "approval";--export type InAppAgentUserAccess = {- projectRole?: Role;- // Global Langfuse admin flag. This bypasses project membership checks.- isAdmin: boolean;-};--type InAppAgentMcpToolPolicy = {- approval: InAppAgentMcpToolApproval;- availability: {- scope: ProjectScope;- };-};--// Exhaustive approval policy for Langfuse MCP tools. Keys use the unprefixed-// MCP registry names and are the source of truth for the tool-name type below.-// Exhaustiveness against web's MCP toolRegistry is enforced by type and runtime-// assertions in web's in-app-agent stream servertest, so new MCP tools must be-// classified before the in-app agent can auto/approval-gate them.-export const IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES = {- listAnnotationQueues: {- approval: "auto",- availability: { scope: "annotationQueues:read" },- },- createAnnotationQueue: {- approval: "approval",- availability: { scope: "annotationQueues:CUD" },- },- getAnnotationQueue: {- approval: "auto",- availability: { scope: "annotationQueues:read" },- },- listAnnotationQueueItems: {- approval: "auto",- availability: { scope: "annotationQueues:read" },- },- getAnnotationQueueItem: {- approval: "auto",- availability: { scope: "annotationQueues:read" },- },- createAnnotationQueueItem: {- approval: "approval",- availability: { scope: "annotationQueues:CUD" },- },- updateAnnotationQueueItem: {- approval: "approval",- availability: { scope: "annotationQueues:CUD" },- },- deleteAnnotationQueueItem: {- approval: "approval",- availability: { scope: "annotationQueues:CUD" },- },- createAnnotationQueueAssignment: {- approval: "approval",- availability: { scope: "annotationQueueAssignments:CUD" },- },- deleteAnnotationQueueAssignment: {- approval: "approval",- availability: { scope: "annotationQueueAssignments:CUD" },- },- createComment: {- approval: "approval",- availability: { scope: "comments:CUD" },- },- listComments: {- approval: "auto",- availability: { scope: "comments:read" },- },- getComment: {- approval: "auto",- availability: { scope: "comments:read" },- },- upsertDataset: {- approval: "approval",- availability: { scope: "datasets:CUD" },- },- listDatasets: {- approval: "auto",- availability: { scope: "datasets:read" },- },- getDataset: {- approval: "auto",- availability: { scope: "datasets:read" },- },- upsertDatasetItem: {- approval: "approval",- availability: { scope: "datasets:CUD" },- },- listDatasetItems: {- approval: "auto",- availability: { scope: "datasets:read" },- },- getDatasetItem: {- approval: "auto",- availability: { scope: "datasets:read" },- },- deleteDatasetItem: {- approval: "approval",- availability: { scope: "datasets:CUD" },- },- createDatasetRunItem: {- approval: "approval",- availability: { scope: "datasets:CUD" },- },- listDatasetRunItems: {- approval: "auto",- availability: { scope: "datasets:read" },- },- listDatasetRuns: {- approval: "auto",- availability: { scope: "datasets:read" },- },- getDatasetRun: {- approval: "auto",- availability: { scope: "datasets:read" },- },- deleteDatasetRun: {- approval: "approval",- availability: { scope: "datasets:CUD" },- },- listEvaluators: {- approval: "auto",- availability: { scope: "evalTemplate:read" },- },- getEvaluator: {- approval: "auto",- availability: { scope: "evalTemplate:read" },- },- upsertEvaluator: {- approval: "approval",- availability: { scope: "evalTemplate:CUD" },- },- deleteEvaluator: {- approval: "approval",- availability: { scope: "evalTemplate:CUD" },- },- listEvaluationRules: {- approval: "auto",- availability: { scope: "evalJob:read" },- },- getEvaluationRule: {- approval: "auto",- availability: { scope: "evalJob:read" },- },- createEvaluationRule: {- approval: "approval",- availability: { scope: "evalJob:CUD" },- },- updateEvaluationRule: {- approval: "approval",- availability: { scope: "evalJob:CUD" },- },- deleteEvaluationRule: {- approval: "approval",- availability: { scope: "evalJob:CUD" },- },- listExperiments: {- approval: "auto",- availability: { scope: "project:read" },- },- listExperimentItems: {- approval: "auto",- availability: { scope: "project:read" },- },- submitFeedback: {- approval: "approval",- availability: { scope: "project:read" },- },- getHealth: {- approval: "auto",- availability: { scope: "project:read" },- },- getMedia: {- approval: "auto",- availability: { scope: "project:read" },- },- queryMetrics: {- approval: "auto",- availability: { scope: "project:read" },- },- getMetricsSchema: {- approval: "auto",- availability: { scope: "project:read" },- },- listModels: {- approval: "auto",- availability: { scope: "project:read" },- },- createModel: {- approval: "approval",- availability: { scope: "models:CUD" },- },- getModel: {- approval: "auto",- availability: { scope: "project:read" },- },- deleteModel: {- approval: "approval",- availability: { scope: "models:CUD" },- },- listObservations: {- approval: "auto",- availability: { scope: "project:read" },- },- getObservation: {- approval: "auto",- availability: { scope: "project:read" },- },- getObservationFieldSchema: {- approval: "auto",- availability: { scope: "project:read" },- },- getObservationFilterSchema: {- approval: "auto",- availability: { scope: "project:read" },- },- getObservationFilterValues: {- approval: "auto",- availability: { scope: "project:read" },- },- getPrompt: {- approval: "auto",- availability: { scope: "prompts:read" },- },- getPromptUnresolved: {- approval: "auto",- availability: { scope: "prompts:read" },- },- listAlerts: {- approval: "auto",- availability: { scope: "alerts:read" },- },- getAlert: {- approval: "auto",- availability: { scope: "alerts:read" },- },- listPrompts: {- approval: "auto",- availability: { scope: "prompts:read" },- },- createTextPrompt: {- approval: "approval",- availability: { scope: "prompts:CUD" },- },- createChatPrompt: {- approval: "approval",- availability: { scope: "prompts:CUD" },- },- updatePromptLabels: {- approval: "approval",- availability: { scope: "prompts:CUD" },- },- listScores: {- approval: "auto",- availability: { scope: "project:read" },- },- getScore: {- approval: "auto",- availability: { scope: "project:read" },- },- createScore: {- approval: "approval",- availability: { scope: "scores:CUD" },- },- listScoreConfigs: {- approval: "auto",- availability: { scope: "scoreConfigs:read" },- },- getScoreConfig: {- approval: "auto",- availability: { scope: "scoreConfigs:read" },- },- createScoreConfig: {- approval: "approval",- availability: { scope: "scoreConfigs:CUD" },- },- updateScoreConfig: {- approval: "approval",- availability: { scope: "scoreConfigs:CUD" },- },- deleteScoreConfig: {- approval: "approval",- availability: { scope: "scoreConfigs:CUD" },- },- createDashboardWidget: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- listDashboardWidgets: {- approval: "auto",- availability: { scope: "dashboards:read" },- },- getDashboardWidget: {- approval: "auto",- availability: { scope: "dashboards:read" },- },- updateDashboardWidget: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- deleteDashboardWidget: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- listDashboards: {- approval: "auto",- availability: { scope: "dashboards:read" },- },- getDashboard: {- approval: "auto",- availability: { scope: "dashboards:read" },- },- createDashboard: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- updateDashboard: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- deleteDashboard: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- addDashboardPlacement: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- updateDashboardPlacement: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },- deleteDashboardPlacement: {- approval: "approval",- availability: { scope: "dashboards:CUD" },- },-} satisfies Record<string, InAppAgentMcpToolPolicy>;--export type InAppAgentLangfuseMcpToolName =- keyof typeof IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES;--export const IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES =- new Set<InAppAgentLangfuseMcpToolName>(- Object.keys(- IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES,- ) as InAppAgentLangfuseMcpToolName[],- );--export const IN_APP_AGENT_AUTO_APPROVED_EXTERNAL_TOOL_NAMES = new Set([- IN_APP_AGENT_REDIRECT_TOOL_NAME,-]);--export const IN_APP_AGENT_SANDBOX_TOOL_NAMES = new Set([- "read",- "write",- "edit",- "bash",-]);--// Runtime-owned tools never suspend; documentation tools are approved by prefix.-const IN_APP_AGENT_LOCAL_AUTO_APPROVED_TOOL_NAMES = new Set<string>([- ...IN_APP_AGENT_AUTO_APPROVED_EXTERNAL_TOOL_NAMES,- ...IN_APP_AGENT_SANDBOX_TOOL_NAMES,-]);--export function isMcpToolName(- input: string,-): input is InAppAgentLangfuseMcpToolName {- return IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES.has(- input as InAppAgentLangfuseMcpToolName,- );-}--/** Durable grants retain the MCP surface prefix to avoid cross-surface collisions. */-export type InAppAgentPrefixedLangfuseMcpToolName =- `langfuse_${InAppAgentLangfuseMcpToolName}`;--export function getInAppAgentRegistryToolName(- toolName: string | undefined,-): InAppAgentLangfuseMcpToolName | undefined {- if (!toolName?.startsWith("langfuse_")) {- return undefined;- }-- const registryToolName = toolName.slice("langfuse_".length);-- return isMcpToolName(registryToolName) ? registryToolName : undefined;-}--export function getInAppAgentPrefixedToolName(- toolName: string | undefined,-): InAppAgentPrefixedLangfuseMcpToolName | undefined {- const registryToolName = getInAppAgentRegistryToolName(toolName);-- return registryToolName ? `langfuse_${registryToolName}` : undefined;-}--export function isInAppAgentLangfuseMcpToolAvailable(params: {- toolName: InAppAgentLangfuseMcpToolName;- userAccess?: InAppAgentUserAccess;-}): boolean {- if (!params.userAccess) {- return false;- }-- const policy = IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[params.toolName];-- if (!policy) {- return false;- }-- return hasProjectAccessByRole({- role: params.userAccess.projectRole ?? Role.MEMBER,- admin: params.userAccess.isAdmin,- scope: policy.availability.scope,- });-}--export type InAppAgentToolPolicy = {- readonly available: ReadonlySet<InAppAgentLangfuseMcpToolName>;- readonly autoApproved: ReadonlySet<InAppAgentLangfuseMcpToolName>;-};--export function createInAppAgentToolPolicy(params: {- userAccess?: InAppAgentUserAccess;- alwaysAllowedTools?: Iterable<string>;-}): InAppAgentToolPolicy {- const available = new Set<InAppAgentLangfuseMcpToolName>();- const autoApproved = new Set<InAppAgentLangfuseMcpToolName>();-- for (const toolName of IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES) {- if (- !isInAppAgentLangfuseMcpToolAvailable({- toolName,- userAccess: params.userAccess,- })- ) {- continue;- }-- available.add(toolName);-- if (IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[toolName].approval === "auto") {- autoApproved.add(toolName);- }- }-- for (const prefixedToolName of params.alwaysAllowedTools ?? []) {- const toolName = getInAppAgentRegistryToolName(prefixedToolName);-- if (toolName && available.has(toolName)) {- autoApproved.add(toolName);- }- }-- return { available, autoApproved };-}--export function getInAppAgentMcpAllowedToolNames(- policy: InAppAgentToolPolicy,- oneOffToolName?: InAppAgentLangfuseMcpToolName,-): InAppAgentLangfuseMcpToolName[] {- const allowed = new Set<InAppAgentLangfuseMcpToolName>();-- // Keep the current approval first for legacy web pods that accept one tool.- if (oneOffToolName && policy.available.has(oneOffToolName)) {- allowed.add(oneOffToolName);- }-- for (const toolName of policy.autoApproved) {- if (- IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[toolName].approval === "approval"- ) {- allowed.add(toolName);- }- }-- return [...allowed];-}--export function filterInAppAgentAvailableLangfuseMcpTools<TTool>(params: {- tools: Partial<Record<InAppAgentLangfuseMcpToolName, TTool>> | undefined;- policy: InAppAgentToolPolicy;-}): Partial<Record<InAppAgentLangfuseMcpToolName, TTool>> {- return Object.fromEntries(- Object.entries(params.tools ?? {}).flatMap(([toolName, tool]) => {- if (!isMcpToolName(toolName) || !params.policy.available.has(toolName)) {- return [];- }-- return [[toolName, tool] as const];- }),- );-}--type InAppAgentTool = object;--export function withInAppAgentToolApproval<TTool extends InAppAgentTool>(- tools: Record<string, TTool>,- policy: InAppAgentToolPolicy,-): Record<string, TTool | (TTool & { requireApproval: true })> {- return Object.fromEntries(- Object.entries(tools).map(([toolName, tool]) => [- toolName,- isInAppAgentAutoApprovedToolName(toolName, policy)- ? tool- : { ...tool, requireApproval: true },- ]),- ) as Record<string, TTool | (TTool & { requireApproval: true })>;-}--function isInAppAgentAutoApprovedToolName(- toolName: string,- policy: InAppAgentToolPolicy,-): boolean {- if (- toolName.startsWith("langfuseDocs_") ||- IN_APP_AGENT_LOCAL_AUTO_APPROVED_TOOL_NAMES.has(toolName)- ) {- return true;- }-- const registryToolName = getInAppAgentRegistryToolName(toolName);-- return (- registryToolName !== undefined && policy.autoApproved.has(registryToolName)- );-}-+import {+ isSilentInAppAgentMcpToolOutput,+ type CompletedInAppAgentMcpToolCall,+ type SilentInAppAgentMcpToolOutput,+} from "./toolResults";export function createSandboxTools(sandbox: InAppAgentSandbox) {return {read: createTool({@@ -619,30 +88,16 @@ const SilentMcpToolInputSchema = z.looseObject({.describe(SILENT_MCP_TOOL_PARAMETER_DESCRIPTION),});-type SilentMcpToolOutput = {- type: typeof IN_APP_AGENT_SILENT_MCP_OUTPUT_TYPE;- output: unknown;-};--export type CompletedInAppAgentMcpToolCall = {- toolCallId: string;- toolName: string;- request: unknown;- response: unknown;- error: string | null;- createdAt: Date;-};--type ToolWithCallableExecute = {- execute: (...args: never[]) => unknown;-};-type WrappableMcpTool = Pick<Tool,"execute" | "inputSchema" | "toModelOutput"> &Required<Pick<Tool, "execute" | "inputSchema">>;+type ToolWithCallableExecute = {+ execute: (...args: never[]) => unknown;+};+export function hasCallableExecute(tool: unknown,): tool is ToolWithCallableExecute {@@ -761,10 +216,10 @@ export function withOptionalSilentMcpOutput(params: {return {type: IN_APP_AGENT_SILENT_MCP_OUTPUT_TYPE,output: result,- } satisfies SilentMcpToolOutput;+ } satisfies SilentInAppAgentMcpToolOutput;};tool.toModelOutput = (output) => {- if (isSilentMcpToolOutput(output)) {+ if (isSilentInAppAgentMcpToolOutput(output)) {return IN_APP_AGENT_SILENT_MCP_OUTPUT_MESSAGE;}@@ -792,32 +247,6 @@ export function getToolCallId(context: unknown) {return context.agent.toolCallId;}-export function getPublicInAppAgentMcpToolResultContent(content: string) {- try {- return isSilentMcpToolOutput(JSON.parse(content) as unknown)- ? IN_APP_AGENT_SILENT_MCP_OUTPUT_MESSAGE- : content;- } catch {- return content;- }-}--export function getSandboxInAppAgentMcpToolResultContent(content: string) {- const parsed = JSON.parse(content) as unknown;-- return isSilentMcpToolOutput(parsed) ? parsed.output : parsed;-}--function isSilentMcpToolOutput(value: unknown): value is SilentMcpToolOutput {- return (- typeof value === "object" &&- value !== null &&- "type" in value &&- value.type === IN_APP_AGENT_SILENT_MCP_OUTPUT_TYPE &&- "output" in value- );-}-const InAppAgentRedirectDestinationSchema = z.enum(["alerts","dashboardWidget",diff --git a/packages/shared/src/in-app-agent/server/watch.ts b/packages/shared/src/in-app-agent/server/watch.tsindex 627edf9bff..6787c91b23 100644--- a/packages/shared/src/in-app-agent/server/watch.ts+++ b/packages/shared/src/in-app-agent/server/watch.ts@@ -8,7 +8,7 @@ import {} from "../backgroundWatch";import type { AgUiEvent } from "../schema";import { reconcileConversationRuns } from "./runLifecycle";-import { getPublicInAppAgentMcpToolResultContent } from "./tools";+import { getPublicInAppAgentMcpToolResultContent } from "./toolResults";import {IN_APP_AGENT_HEARTBEAT_STALE_MS,IN_APP_AGENT_WATCH_KEEPALIVE_MS,diff --git a/web/AGENTS.md b/web/AGENTS.mdindex b49cb977a8..958279a014 100644--- a/web/AGENTS.md+++ b/web/AGENTS.md@@ -43,7 +43,8 @@- The in-app-agent runtime lives in shared:`@langfuse/shared/in-app-agent` is the client-safe contracts entry;its `AgUiRunAgentInput` is a compile-time-only execution contract;- `@langfuse/shared/in-app-agent/server` (and per-module subpaths) is+ `@langfuse/shared/in-app-agent` also owns the browser-safe durable interrupt+ parser. `@langfuse/shared/in-app-agent/server` (and per-module subpaths) isserver-only. Web keeps only the thin adapters in`src/features/in-app-agent/` (router and UI), plus the authenticated watchroute in `src/app/api/in-app-agent/watch/`.diff --git a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.tsindex 250ba78eb3..531f64c521 100644--- a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts+++ b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts@@ -9,7 +9,7 @@ import { createAuthedProjectAPIRoute } from "@/src/features/public-api/server/crimport {createInAppAgentToolPolicy,filterInAppAgentAvailableLangfuseMcpTools,-} from "@langfuse/shared/in-app-agent/server/tools";+} from "@langfuse/shared/in-app-agent/server/mcpPolicy";import { prisma } from "@langfuse/shared/src/db";import {createAndAddApiKeysToDb,diff --git a/web/src/__tests__/server/in-app-agent-stream.servertest.ts b/web/src/__tests__/server/in-app-agent-stream.servertest.tsindex 696a9547e4..6c138bd3df 100644--- a/web/src/__tests__/server/in-app-agent-stream.servertest.ts+++ b/web/src/__tests__/server/in-app-agent-stream.servertest.ts@@ -23,7 +23,7 @@ import {createInAppAgentToolPolicy,IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES,type InAppAgentLangfuseMcpToolName,-} from "@langfuse/shared/in-app-agent/server/tools";+} from "@langfuse/shared/in-app-agent/server/mcpPolicy";import {DEFAULT_SIDEBAR_HIDDEN_ENVIRONMENTS,decodeFiltersGeneric,diff --git a/web/src/__tests__/server/unit/in-app-agent-quick-action-context.servertest.ts b/web/src/__tests__/server/unit/in-app-agent-quick-action-context.servertest.tsdeleted file mode 100644index b33d37a198..0000000000--- a/web/src/__tests__/server/unit/in-app-agent-quick-action-context.servertest.ts+++ /dev/null@@ -1,74 +0,0 @@-import {- getInAppAgentMessageEntryPointTraceMetadata,- getInAppAgentQuickActionTraceMetadata,- sanitizeInAppAgentContext,-} from "@/src/features/in-app-agent/context";--describe("in-app agent quick-action attribution", () => {- it("exposes trace metadata for well-formed attribution and keeps it out of the model-visible context", () => {- const context = [- {- description: "quick_action_key",- value: "analyze-failure-patterns",- },- {- description: "quick_action_category",- value: "observability",- },- ];-- expect(getInAppAgentQuickActionTraceMetadata(context)).toEqual({- quick_action_key: "analyze-failure-patterns",- quick_action_category: "observability",- });- expect(sanitizeInAppAgentContext(context, "project-1")).toEqual([]);- });-- it("rejects malformed attribution", () => {- const invalidPairs = [- ["INVALID!", "observability"],- ["a".repeat(81), "observability"],- ["analyze-failure-patterns", "unknown-context"],- ] as const;-- for (const [quickActionKey, quickActionCategory] of invalidPairs) {- expect(- getInAppAgentQuickActionTraceMetadata([- { description: "quick_action_key", value: quickActionKey },- {- description: "quick_action_category",- value: quickActionCategory,- },- ]),- ).toEqual({});- }-- expect(- getInAppAgentQuickActionTraceMetadata([- {- description: "quick_action_key",- value: "analyze-failure-patterns",- },- ]),- ).toEqual({});- });-});--describe("in-app agent message entry point", () => {- it("exposes allowlisted entry points as trace metadata and keeps them out of the model-visible context", () => {- const context = [- { description: "message_entry_point", value: "add-widget-modal" },- ];-- expect(getInAppAgentMessageEntryPointTraceMetadata(context)).toEqual({- message_entry_point: "add-widget-modal",- });- expect(sanitizeInAppAgentContext(context, "project-1")).toEqual([]);-- expect(- getInAppAgentMessageEntryPointTraceMetadata([- { description: "message_entry_point", value: "not-a-real-source" },- ]),- ).toEqual({});- });-});diff --git a/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.ts b/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.tsindex 94f69d38c4..c3dc967add 100644--- a/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.ts+++ b/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.ts@@ -4,11 +4,7 @@ import { Tool } from "@mastra/core/tools";import { describe, expect, it } from "vitest";import { z } from "zod";-import {- createSandboxToolCallFileAccumulator,- getConversationMessagesForDisplay,- getSandboxToolCallFiles,-} from "@langfuse/shared/in-app-agent/server/persistence";+import { createSandboxToolCallFileAccumulator } from "@langfuse/shared/in-app-agent/server/persistence";import { createInAppAgentSandbox } from "@langfuse/shared/in-app-agent/server/sandbox";import { withOptionalSilentMcpOutput } from "@langfuse/shared/in-app-agent/server/tools";import { listObservationsTool } from "@/src/features/mcp/features/observations/tools/listObservations";@@ -31,59 +27,6 @@ const dummySandbox = {};describe("in-app agent sandbox", () => {- it("redacts silent MCP output from persisted conversation display", async () => {- const silentResult = JSON.stringify({- type: "silent-mcp-output",- output: { data: [{ id: "observation-1" }] },- });- const events = [- {- type: EventType.TOOL_CALL_START,- toolCallId: "tool-call-1",- toolCallName: "listObservations",- parentMessageId: "assistant-1",- },- {- type: EventType.TOOL_CALL_ARGS,- toolCallId: "tool-call-1",- delta: "{}",- },- {- type: EventType.TOOL_CALL_END,- toolCallId: "tool-call-1",- },- {- type: EventType.TOOL_CALL_RESULT,- messageId: "result-1",- toolCallId: "tool-call-1",- content: silentResult,- role: "tool",- },- ];- const messages = await getConversationMessagesForDisplay({- prisma: {- inAppAgentEvent: {- findMany: async () =>- events.map((event) => ({- event,- runId: "run-1",- createdAt: new Date("2026-07-27T10:00:00.000Z"),- })),- },- } as never,- projectId: "project-1",- conversationId: "conversation-1",- });-- expect(messages).toContainEqual(- expect.objectContaining({- role: "tool",- content: "Output saved to /workspace/tool_calls",- }),- );- expect(events[3]?.content).toBe(silentResult);- });-it("persists sandbox session state when a turn ends", async () => {const sandboxSession = {async syncReadonlyFiles() {},@@ -201,127 +144,6 @@ describe("in-app agent sandbox", () => {});});- it("exports prior non-sandbox tool calls into tool_calls files", () => {- const files = getSandboxToolCallFiles([- {- createdAt: new Date("2026-07-02T12:00:00.000Z"),- runId: "run-1",- event: {- type: EventType.TOOL_CALL_START,- toolCallId: "tool-call-1",- toolCallName: "langfuse_getHealth",- },- },- {- createdAt: new Date("2026-07-02T12:00:00.100Z"),- runId: "run-1",- event: {- type: EventType.TOOL_CALL_ARGS,- toolCallId: "tool-call-1",- delta: '{"projectId":"project-1"}',- },- },- {- createdAt: new Date("2026-07-02T12:00:00.200Z"),- runId: "run-1",- event: {- type: EventType.TOOL_CALL_RESULT,- toolCallId: "tool-call-1",- content: '{"status":"ok"}',- },- },- {- createdAt: new Date("2026-07-02T12:00:01.000Z"),- runId: "run-1",- event: {- type: EventType.TOOL_CALL_START,- toolCallId: "tool-call-2",- toolCallName: "read",- },- },- {- createdAt: new Date("2026-07-02T12:00:01.100Z"),- runId: "run-1",- event: {- type: EventType.TOOL_CALL_ARGS,- toolCallId: "tool-call-2",- delta: '{"path":"tool_calls/file.json"}',- },- },- {- createdAt: new Date("2026-07-02T12:00:01.200Z"),- runId: "run-1",- event: {- type: EventType.TOOL_CALL_RESULT,- toolCallId: "tool-call-2",- content: '{"content":"ignored"}',- },- },- ]);-- expect(files).toEqual([- {- path: "tool_calls/2026-07-02T12-00-00.000Z_langfuse_getHealth_tool-call-1.json",- content: JSON.stringify(- {- request: { projectId: "project-1" },- response: { status: "ok" },- error: null,- },- null,- 2,- ),- },- ]);- });-- it("keeps repeated same-name tool calls with identical timestamps", () => {- const createdAt = new Date("2026-07-02T12:00:00.000Z");- const files = getSandboxToolCallFiles([- {- createdAt,- runId: "run-1",- event: {- type: EventType.TOOL_CALL_START,- toolCallId: "tool-call-1",- toolCallName: "langfuse_getHealth",- },- },- {- createdAt,- runId: "run-1",- event: {- type: EventType.TOOL_CALL_RESULT,- toolCallId: "tool-call-1",- content: '{"status":"ok"}',- },- },- {- createdAt,- runId: "run-1",- event: {- type: EventType.TOOL_CALL_START,- toolCallId: "tool-call-2",- toolCallName: "langfuse_getHealth",- },- },- {- createdAt,- runId: "run-1",- event: {- type: EventType.TOOL_CALL_RESULT,- toolCallId: "tool-call-2",- content: '{"status":"ok"}',- },- },- ]);-- expect(files.map((file) => file.path)).toEqual([- "tool_calls/2026-07-02T12-00-00.000Z_langfuse_getHealth_tool-call-1.json",- "tool_calls/2026-07-02T12-00-00.000Z_langfuse_getHealth_tool-call-2.json",- ]);- });-it("returns the sandbox tool-call directory for silent MCP output", async () => {const execute = async (input: { query: string }) => ({result: input.query,@@ -516,7 +338,7 @@ describe("in-app agent sandbox", () => {});it("does not export failed tool calls into sandbox tool_calls files", () => {- const files = getSandboxToolCallFiles([+ const files = createSandboxToolCallFileAccumulator([{createdAt: new Date("2026-07-02T12:00:00.000Z"),runId: "run-1",@@ -548,7 +370,7 @@ describe("in-app agent sandbox", () => {}),},},- ]);+ ]).getFiles();expect(files).toEqual([]);});diff --git a/web/src/features/in-app-agent/README.md b/web/src/features/in-app-agent/README.mdindex 7b1e376b10..8951221270 100644--- a/web/src/features/in-app-agent/README.md+++ b/web/src/features/in-app-agent/README.md@@ -25,8 +25,11 @@ Closing the drawer detaches observation without cancelling the worker run.- `schema.ts`: runtime-neutral AG-UI schemas and types shared by browser, server, persistence, replay, and rendering, including Langfuse-owned human-in-the-loop wire contracts.- `server/agent.ts`: Mastra/Bedrock/MCP runtime setup, custom tool wiring, human-in-the-loop approval gates, AG-UI event normalization, and cleanup.-- `server/human-in-the-loop.ts`: interrupt parsing and worker continuation+- `interrupts.ts`: browser-safe parsing of the durable approval interrupt+- `server/human-in-the-loop.ts`: worker continuation handlingcompatibility.+- `server/mcpPolicy.ts`: MCP names, authorization, and approval policy+- `server/toolResults.ts`: persisted MCP result redaction and sandbox-file contracts- `server/tools.ts`: custom agent tools with strict schemas and scoped, user-visible behavior.- `server/persistence.ts`: conversations, events, canonical accumulation, and replay. Knows nothing about rendering.- `server/runLifecycle.ts`: durable run creation, claiming, cancellation, terminal transitions, active-run locking, and reconciliation.@@ -99,7 +102,10 @@ flowchart TB## Run Lifecycles-1. The provider starts the durable run through `startRun`.+1. The provider submits one user message plus browser context to the session.+ The session owns optimistic insertion and constructs the internal AG-UI run+ input before starting the durable run through `startRun`; the server remains+ the authoritative sanitizer before persisting the request.2. `BackgroundExecutionSessionController` installs the persisted canonicalmessages, the display state, and the cursor before attaching the watchstream. The seed is never projected or pruned; see `ARCHITECTURE.md`.@@ -210,13 +216,14 @@ MCP registry behavior:- In-app-agent keys can call read-only tools directly when the tool has `readOnlyHint: true`.- In-app-agent keys need a valid tool override to call named non-read-only Langfuse MCP tools.-RBAC is the first gate for Langfuse MCP tools. Before a tool is exposed to the model, `server/tools.ts` checks the signed-in user's `projectRole` and `isAdmin` against the tool's required `ProjectScope` with `hasProjectAccess()`. That means the assistant never sees tools the user could not use manually in the product UI or APIs. Human approval is a second gate on top of RBAC for tools classified as `"approval"`: approval can allow one execution of a tool the user already has access to, but it does not widen the user's project permissions.+RBAC is the first gate for Langfuse MCP tools. Before a tool is exposed to the model, `server/mcpPolicy.ts` checks the signed-in user's `projectRole` and `isAdmin` against the tool's required `ProjectScope` with `hasProjectAccess()`. That means the assistant never sees tools the user could not use manually in the product UI or APIs. Human approval is a second gate on top of RBAC for tools classified as `"approval"`: approval can allow one execution of a tool the user already has access to, but it does not widen the user's project permissions.-Human approval is separate from the MCP tool override. Shared `server/tools.ts` classifies every Langfuse MCP tool in `IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES`, using unprefixed MCP registry names and either `"auto"` or `"approval"`. The web in-app-agent server test imports the MCP registry's `McpToolName` contract and verifies both type equality and runtime registry equality, so adding a Langfuse MCP tool requires an explicit in-app agent approval classification without making MCP bootstrap depend on the in-app agent.+Human approval is separate from the MCP tool override. Shared `server/mcpPolicy.ts` classifies every Langfuse MCP tool in `IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES`, using unprefixed MCP registry names and either `"auto"` or `"approval"`. The web in-app-agent server test imports the MCP registry's `McpToolName` contract and verifies both type equality and runtime registry equality, so adding a Langfuse MCP tool requires an explicit in-app agent approval classification without making MCP bootstrap depend on the in-app agent.-`server/tools.ts` turns that map into an `InAppAgentToolPolicy` with an `available` set for tools the model may see and an `autoApproved` set for tools that run without a prompt. `createInAppAgentToolPolicy()` intersects stored grants with current availability, so an unknown, deleted, reclassified, or now-out-of-role tool silently drops out. Local tools such as `IN_APP_AGENT_REDIRECT_TOOL_NAME` and the sandbox tools are always auto-approved; docs MCP tools are auto-approved by the `langfuseDocs_` prefix. `server/agent.ts` marks every other tool with Mastra `requireApproval: true`.+`server/mcpPolicy.ts` turns that map into an `InAppAgentToolPolicy` with an `available` set for tools the model may see and an `autoApproved` set for tools that run without a prompt. `createInAppAgentToolPolicy()` intersects stored grants with current availability, so an unknown, deleted, reclassified, or now-out-of-role tool silently drops out. Local tools such as `IN_APP_AGENT_REDIRECT_TOOL_NAME` and the sandbox tools are always auto-approved; docs MCP tools are auto-approved by the `langfuseDocs_` prefix. `server/agent.ts` marks every other tool with Mastra `requireApproval: true`.Conversation-scoped grants live in `InAppAgentConversation.alwaysAllowedTools`, holding server-prefixed names such as `langfuse_createDashboardWidget`. The prefix is part of the identity, so a stored grant cannot be mistaken for a same-named tool on another MCP surface; unprefixed entries are rejected when the policy is rebuilt. Grants are written inside the same locked transaction as the approval decision in `server/runLifecycle.ts`. A one-off `Approve` writes nothing there; only `Always approve` does. The worker rebuilds the policy from the column on every run rather than trusting enqueue-time authorization. Mastra emits an interrupt, the browser asks the user, and the router records the decision for a durable worker continuation. `server/human-in-the-loop.ts` adapts Mastra's runtime interrupt payload into the Langfuse-owned `tool_approval_request` contract from `schema.ts`; the browser stores and forwards only that runtime-neutral shape.+The browser-safe `interrupts.ts` parser adapts Mastra's runtime payload into the Langfuse-owned `tool_approval_request` contract from `schema.ts`; browser, web server, and worker all consume that same parser.The `InAppAgentPendingToolApproval` table is not used by background execution.It remains temporarily so existing rows and Prisma relations stay valid.diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsxindex f068eab1fd..f062a4272a 100644--- a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx@@ -130,7 +130,7 @@ function selectTab(name: string) {}describe("InAppAgentWindow quick actions", () => {- it("switches tabs, resets on route change, and submits the action prompt with attribution", async () => {+ it("switches tabs, resets on route change, and submits the action prompt with analytics", async () => {const onSubmit = vi.fn().mockResolvedValue(true);const { rerender } = render(windowElement({ onSubmit }));@@ -172,12 +172,7 @@ describe("InAppAgentWindow quick actions", () => {await waitFor(() => {expect(onSubmit).toHaveBeenCalledWith("Help me create a new prompt in Langfuse prompt management, including choosing between a text and chat prompt, defining its variables, and setting a label.",- {- quickAction: {- key: "create-prompt",- category: "prompts",- },- },+ undefined,);});expect(capture).toHaveBeenCalledWith("in_app_agent:quick_action_started", {diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsxindex 4ce8118b70..22592d6a9d 100644--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx@@ -1366,12 +1366,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {quickActionCategory: context,position,});- submitInput(action.prompt, {- quickAction: {- key: action.id,- category: context,- },- });+ submitInput(action.prompt);}}/></div>diff --git a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsxindex c7250e9d94..8caa0f5dc1 100644--- a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx+++ b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx@@ -18,7 +18,6 @@ import { useRouter } from "next/router";import useSessionStorage from "@/src/components/useSessionStorage";import {createInAppAgentConversationId,- createInAppAgentMessageId,IN_APP_AGENT_REDIRECT_TOOL_NAME,} from "@langfuse/shared/in-app-agent";import {@@ -47,7 +46,6 @@ import { InAppAgentBackgroundClient } from "@/src/features/in-app-agent/lib/backimport {BackgroundExecutionSessionController,isCancellableBackgroundRun,- type AgentInput,type BackgroundExecutionRunView,type BackgroundExecutionSession,type BackgroundExecutionView,@@ -59,16 +57,10 @@ import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";import { api } from "@/src/utils/api";import {- createInAppAgentMessageEntryPointContext,- createInAppAgentQuickActionAttributionContext,createInAppAgentScreenContext,createInAppAgentUserContext,- type InAppAgentMessageEntryPoint,} from "@/src/features/in-app-agent/context";-import type {- InAppAgentQuickActionAttribution,- InAppAgentSubmitOptions,-} from "@/src/features/in-app-agent/quickActions";+import type { InAppAgentSubmitOptions } from "@/src/features/in-app-agent/quickActions";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";import {getInAppAgentError,@@ -325,7 +317,6 @@ function InAppAiAgentProviderInner({() => new Set(),);const [error, setError] = useState<InAppAgentError | null>(null);- const agentRef = useRef<InAppAgentBackgroundClient | null>(null);const backgroundSessionRef = useRef<BackgroundExecutionSession | null>(null);const [backgroundSession, setBackgroundSession] =useState<BackgroundExecutionSession | null>(null);@@ -659,7 +650,6 @@ function InAppAiAgentProviderInner({backgroundSessionRef.current?.dispose();backgroundSessionRef.current = null;setBackgroundSession(null);- agentRef.current = null;toolCallNamesRef.current.clear();handledToolCallIdsRef.current.clear();clearLoadingEvents();@@ -776,10 +766,10 @@ function InAppAiAgentProviderInner({setSubmittingConversationId(null);}, []);- const getOrCreateAgent = useCallback(+ const getOrCreateBackgroundSession = useCallback((conversationId: string, initialMessages: AgUiMessage[]) => {- if (agentRef.current?.threadId === conversationId) {- return agentRef.current;+ if (backgroundSessionRef.current?.conversationId === conversationId) {+ return backgroundSessionRef.current;}resetAgent();@@ -826,8 +816,6 @@ function InAppAiAgentProviderInner({},});- agentRef.current = agent;-const nextBackgroundSession = new BackgroundExecutionSessionController({agent,subscriber: sharedAgentSubscriber,@@ -914,7 +902,7 @@ function InAppAiAgentProviderInner({backgroundSessionRef.current = nextBackgroundSession;setBackgroundSession(nextBackgroundSession);- return agent;+ return nextBackgroundSession;},[cancelRunMutation,@@ -931,14 +919,9 @@ function InAppAiAgentProviderInner({],);- const createRunInput = useCallback(- (- runParameters?: AgentInput,- quickActionAttribution?: InAppAgentQuickActionAttribution,- messageEntryPoint?: InAppAgentMessageEntryPoint,- ): AgentInput => ({- ...runParameters,- context: createInAppAgentScreenContext({+ const createRunContext = useCallback(+ () =>+ createInAppAgentScreenContext({currentUrl: window.location.href,}).concat(createInAppAgentUserContext({@@ -949,16 +932,7 @@ function InAppAiAgentProviderInner({? Array.from(navigator.languages): [navigator.language],}),- quickActionAttribution- ? createInAppAgentQuickActionAttributionContext(- quickActionAttribution,- )- : [],- messageEntryPoint- ? createInAppAgentMessageEntryPointContext(messageEntryPoint)- : [],),- }),[session.data?.user?.name],);@@ -1092,24 +1066,17 @@ function InAppAiAgentProviderInner({: [];// TODO: Avoid hydrating the full history once the agent client can send// only the latest user turn; the server rebuilds history from persistence.- const agent = getOrCreateAgent(conversationId, initialMessages);-- if (agent.isRunning) {+ const backgroundSession = getOrCreateBackgroundSession(+ conversationId,+ initialMessages,+ );+ const execution = backgroundSession.run({+ message: content,+ context: createRunContext(),+ });+ if (!execution) {return false;}-- // Start background turns from the persisted transcript and cursor.- if (shouldRestorePersistedMessages) {- agent.setMessages(initialMessages);- }-- const userMessage = {- id: createInAppAgentMessageId(),- role: "user",- content,- } satisfies AgUiMessage;-- agent.addMessage(userMessage);const entryPoint = options?.entryPoint ?? "chat";if (startsNewConversation) {capture("in_app_agent:new_chat_started", {@@ -1124,20 +1091,12 @@ function InAppAiAgentProviderInner({}capture("in_app_agent:new_chat_turn", { entryPoint });startedRun = true;- const backgroundSession = backgroundSessionRef.current;-- if (!backgroundSession) {- throw new Error("Background execution session is unavailable");- }-clearLoadingEvents();- backgroundSession- .run(createRunInput(undefined, options?.quickAction, entryPoint))- .catch((error: unknown) => {- if (backgroundSession.getSnapshot().attachment.status !== "error") {- setError(getInAppAgentError(error));- }- });+ execution.catch((error: unknown) => {+ if (backgroundSession.getSnapshot().attachment.status !== "error") {+ setError(getInAppAgentError(error));+ }+ });return true;} catch (error) {setError(getInAppAgentError(error));@@ -1154,8 +1113,8 @@ function InAppAiAgentProviderInner({capture,clearLoadingEvents,error,- createRunInput,- getOrCreateAgent,+ createRunContext,+ getOrCreateBackgroundSession,isSelectedConversationHydrating,isRunning,releaseSubmitLock,@@ -1228,10 +1187,12 @@ function InAppAiAgentProviderInner({conversationQuery.data?.conversation.id === conversationId? conversationQuery.data.messages.filter(isAgentConversationMessage): [];- getOrCreateAgent(conversationId, initialMessages);- await backgroundSessionRef.current?.hydrateAndAttach();+ await getOrCreateBackgroundSession(+ conversationId,+ initialMessages,+ ).hydrateAndAttach();},- [conversationQuery.data, getOrCreateAgent],+ [conversationQuery.data, getOrCreateBackgroundSession],);const attachToConversationRef = useRef(attachToConversation);attachToConversationRef.current = attachToConversation;@@ -1324,12 +1285,10 @@ function InAppAiAgentProviderInner({conversationQuery.data?.conversation.id === selectedConversationId? conversationQuery.data.messages.filter(isAgentConversationMessage): [];- getOrCreateAgent(selectedConversationId, initialMessages);- const backgroundSession = backgroundSessionRef.current;-- if (!backgroundSession) {- return;- }+ const backgroundSession = getOrCreateBackgroundSession(+ selectedConversationId,+ initialMessages,+ );backgroundSession.cancel().catch((error: unknown) => {showErrorToast("Failed to stop the run", getAgentErrorMessage(error));@@ -1337,7 +1296,7 @@ function InAppAiAgentProviderInner({}, [conversationQuery.data,currentBackgroundRun,- getOrCreateAgent,+ getOrCreateBackgroundSession,selectedConversationId,]);@@ -1358,12 +1317,10 @@ function InAppAiAgentProviderInner({conversationQuery.data?.conversation.id === params.conversationId? conversationQuery.data.messages.filter(isAgentConversationMessage): [];- getOrCreateAgent(params.conversationId, initialMessages);- const backgroundSession = backgroundSessionRef.current;-- if (!backgroundSession) {- throw new Error("Background execution session is unavailable");- }+ const backgroundSession = getOrCreateBackgroundSession(+ params.conversationId,+ initialMessages,+ );await backgroundSession.decide({runId,@@ -1377,7 +1334,7 @@ function InAppAiAgentProviderInner({return false;}},- [conversationQuery.data, getOrCreateAgent],+ [conversationQuery.data, getOrCreateBackgroundSession],);const resumeToolApproval = useCallback(diff --git a/web/src/features/in-app-agent/components/index.ts b/web/src/features/in-app-agent/components/index.tsdeleted file mode 100644index 529abf1d06..0000000000--- a/web/src/features/in-app-agent/components/index.ts+++ /dev/null@@ -1,2 +0,0 @@-export { ControlledInAppAgentWindow } from "./ControlledInAppAgentWindow";-export { InAppAiAgentProvider, useInAppAiAgent } from "./InAppAiAgentProvider";diff --git a/web/src/features/in-app-agent/components/utils/side-effects.ts b/web/src/features/in-app-agent/components/utils/side-effects.tsindex 02e017b470..0eedc432b6 100644--- a/web/src/features/in-app-agent/components/utils/side-effects.ts+++ b/web/src/features/in-app-agent/components/utils/side-effects.ts@@ -1,4 +1,4 @@-import type { InAppAgentLangfuseMcpToolName } from "@langfuse/shared/in-app-agent/server/tools";+import type { InAppAgentLangfuseMcpToolName } from "@langfuse/shared/in-app-agent/server/mcpPolicy";import { safeJsonParse } from "@langfuse/shared";import { IN_APP_AGENT_TOOL_REJECTION_ERROR_CODE } from "@langfuse/shared/in-app-agent";import type { AgUiMessage } from "@langfuse/shared/in-app-agent";@@ -124,7 +124,7 @@ const IN_APP_AGENT_TOOL_TRPC_INVALIDATION_TARGETS = {readonly InAppAgentTrpcInvalidationTarget[]>;-export function getInAppAgentTrpcInvalidationTargets(toolName: string) {+function getInAppAgentTrpcInvalidationTargets(toolName: string) {return (IN_APP_AGENT_TOOL_TRPC_INVALIDATION_TARGETS[toolName as keyof typeof IN_APP_AGENT_TOOL_TRPC_INVALIDATION_TARGETSdiff --git a/web/src/features/in-app-agent/components/utils/utils.clienttest.ts b/web/src/features/in-app-agent/components/utils/utils.clienttest.tsindex a012ca0d1e..b313ff8c47 100644--- a/web/src/features/in-app-agent/components/utils/utils.clienttest.ts+++ b/web/src/features/in-app-agent/components/utils/utils.clienttest.ts@@ -5,9 +5,8 @@ import {import {IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES,IN_APP_AGENT_SANDBOX_TOOL_NAMES,-} from "@langfuse/shared/in-app-agent/server/tools";+} from "@langfuse/shared/in-app-agent/server/mcpPolicy";import {- extractLangfuseDocsSources,getInAppAgentToolDisplayName,getInAppAgentActivityProgressLabel,getInAppAgentToolProgressLabel,@@ -254,135 +253,6 @@ describe("getInAppAgentError", () => {});});-describe("extractLangfuseDocsSources", () => {- it("extracts and deduplicates document sources from docs tool results", () => {- const result = JSON.stringify({- _meta: {- choices: [- {- message: {- content: JSON.stringify({- content: [- { type: "text", text: "Search result" },- {- type: "document",- title: "Core Concepts",- url: "https://langfuse.com/docs/evaluation/core-concepts",- },- {- type: "document",- title: "Scores",- url: "https://langfuse.com/docs/evaluation/scores/overview",- },- ],- }),- },- },- {- message: {- content: JSON.stringify({- content: [- {- type: "document",- title: "Datasets",- url: "https://langfuse.com/docs/datasets/overview",- },- ],- }),- },- },- {- message: {- content: JSON.stringify({- content: [- {- type: "document",- title: "Scores duplicate",- url: "https://langfuse.com/docs/evaluation/scores/overview",- },- ],- }),- },- },- ],- },- });-- expect(- extractLangfuseDocsSources([- {- type: "tool",- name: "langfuseDocs_search",- args: "{}",- status: "succeeded",- result,- },- {- type: "tool",- name: "langfuse_queryMetrics",- args: "{}",- status: "succeeded",- result,- },- ]),- ).toEqual([- {- title: "Core Concepts",- url: "https://langfuse.com/docs/evaluation/core-concepts",- faviconUrl: "https://langfuse.com/favicon.ico",- },- {- title: "Scores",- url: "https://langfuse.com/docs/evaluation/scores/overview",- faviconUrl: "https://langfuse.com/favicon.ico",- },- {- title: "Datasets",- url: "https://langfuse.com/docs/datasets/overview",- faviconUrl: "https://langfuse.com/favicon.ico",- },- ]);- });-- it("ignores malformed structured sources", () => {- const result = JSON.stringify({- _meta: {- choices: [- { message: { content: "not json" } },- { message: { content: JSON.stringify({ content: "not array" }) } },- {- message: {- content: JSON.stringify({- content: [- { type: "document", title: "Missing URL" },- { type: "document", title: "Blank URL", url: " " },- {- type: "document",- title: "Unsafe protocol",- url: "javascript:alert(1)",- },- ],- }),- },- },- ],- },- });-- expect(- extractLangfuseDocsSources([- {- type: "tool",- name: "langfuseDocs_search",- args: "{}",- status: "succeeded",- result,- },- ]),- ).toEqual([]);- });-});-describe("getDrawerMessages", () => {it("maps tool results to explicit display statuses", () => {const rejectionMessage = "Tool call was not approved by the user.";diff --git a/web/src/features/in-app-agent/components/utils/utils.ts b/web/src/features/in-app-agent/components/utils/utils.tsindex c85625d699..8ec181c9cd 100644--- a/web/src/features/in-app-agent/components/utils/utils.ts+++ b/web/src/features/in-app-agent/components/utils/utils.ts@@ -788,7 +788,7 @@ function getRedirectActionFromToolResult(}}-export function extractLangfuseDocsSources(+function extractLangfuseDocsSources(tools: readonly InAppAgentToolCallContent[],): InAppAgentMessageSource[] {return mergeSources(diff --git a/web/src/features/in-app-agent/context.ts b/web/src/features/in-app-agent/context.tsindex 98ebd6a49d..a2b80023b6 100644--- a/web/src/features/in-app-agent/context.ts+++ b/web/src/features/in-app-agent/context.ts@@ -1,8 +1,4 @@import type { AgUiRunAgentInput } from "@langfuse/shared/in-app-agent";-import {- isInAppAgentQuickActionContext,- type InAppAgentQuickActionAttribution,-} from "@/src/features/in-app-agent/quickActions";import { getInAppAgentProjectRoute } from "@/src/features/in-app-agent/routeContext";import type { FilterState } from "@langfuse/shared";@@ -30,17 +26,12 @@ export type InAppAgentScreenContextDescription =| { type: "datasets-list" };const CURRENT_URL_CONTEXT_DESCRIPTION = "current_url";-const QUICK_ACTION_KEY_CONTEXT_DESCRIPTION = "quick_action_key";-const QUICK_ACTION_CATEGORY_CONTEXT_DESCRIPTION = "quick_action_category";-const MESSAGE_ENTRY_POINT_CONTEXT_DESCRIPTION = "message_entry_point";const MAX_SCREEN_CONTEXT_SEARCH_PARAMS = 30;const MAX_CONTEXT_KEY_LENGTH = 80;const MAX_CONTEXT_VALUE_LENGTH = 500;const MAX_SCREEN_CONTEXT_PATH_LENGTH = 500;const MAX_SCREEN_CONTEXT_HASH_LENGTH = 200;const MAX_SCREEN_CONTEXT_JSON_LENGTH = 4_000;-const MAX_QUICK_ACTION_KEY_LENGTH = 80;-const QUICK_ACTION_KEY_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;const USER_CONTEXT_DESCRIPTIONS = new Set(["user_name","current_timezone",@@ -209,99 +200,6 @@ export function sanitizeInAppAgentContext(return sanitizedContext;}-export function createInAppAgentQuickActionAttributionContext(- attribution: InAppAgentQuickActionAttribution,-): InAppAgentContext {- return [- {- description: QUICK_ACTION_KEY_CONTEXT_DESCRIPTION,- value: attribution.key,- },- {- description: QUICK_ACTION_CATEGORY_CONTEXT_DESCRIPTION,- value: attribution.category,- },- ];-}--// Attribution is telemetry only: it is validated by shape here and read for-// trace metadata, but never forwarded into the model-visible sanitized context.-export function getInAppAgentQuickActionAttribution(- context: InAppAgentContext,-): InAppAgentQuickActionAttribution | undefined {- const quickActionKey = context- .find((item) => item.description === QUICK_ACTION_KEY_CONTEXT_DESCRIPTION)- ?.value.trim();- const quickActionCategory = context- .find(- (item) => item.description === QUICK_ACTION_CATEGORY_CONTEXT_DESCRIPTION,- )- ?.value.trim();-- if (- !quickActionKey ||- quickActionKey.length > MAX_QUICK_ACTION_KEY_LENGTH ||- !QUICK_ACTION_KEY_PATTERN.test(quickActionKey) ||- !quickActionCategory ||- !isInAppAgentQuickActionContext(quickActionCategory)- ) {- return undefined;- }-- return { key: quickActionKey, category: quickActionCategory };-}--export function getInAppAgentQuickActionTraceMetadata(- context: InAppAgentContext,-): Record<string, string> {- const attribution = getInAppAgentQuickActionAttribution(context);-- return attribution- ? {- quick_action_key: attribution.key,- quick_action_category: attribution.category,- }- : {};-}--export const IN_APP_AGENT_MESSAGE_ENTRY_POINTS = [- "chat",- "add-widget-modal",-] as const;--export type InAppAgentMessageEntryPoint =- (typeof IN_APP_AGENT_MESSAGE_ENTRY_POINTS)[number];--export function createInAppAgentMessageEntryPointContext(- entryPoint: InAppAgentMessageEntryPoint,-): InAppAgentContext {- return [- {- description: MESSAGE_ENTRY_POINT_CONTEXT_DESCRIPTION,- value: entryPoint,- },- ];-}--// Telemetry only, like quick-action attribution: read for trace metadata but-// never forwarded into the model-visible sanitized context.-export function getInAppAgentMessageEntryPointTraceMetadata(- context: InAppAgentContext,-): Record<string, string> {- const entryPoint = context- .find(- (item) => item.description === MESSAGE_ENTRY_POINT_CONTEXT_DESCRIPTION,- )- ?.value.trim();-- return entryPoint &&- (IN_APP_AGENT_MESSAGE_ENTRY_POINTS as readonly string[]).includes(- entryPoint,- )- ? { message_entry_point: entryPoint }- : {};-}-function sanitizeUserContext(context: InAppAgentContext): InAppAgentContext {return context.flatMap((item) => {if (!USER_CONTEXT_DESCRIPTIONS.has(item.description)) {diff --git a/web/src/features/in-app-agent/lib/backgroundExecutionSession.clienttest.ts b/web/src/features/in-app-agent/lib/backgroundExecutionSession.clienttest.tsindex 9eca29a2d4..aa91ec3324 100644--- a/web/src/features/in-app-agent/lib/backgroundExecutionSession.clienttest.ts+++ b/web/src/features/in-app-agent/lib/backgroundExecutionSession.clienttest.ts@@ -105,7 +105,9 @@ const finalMessage = {function createAgent() {return {+ threadId: "conversation-1",messages: [],+ addMessage: vi.fn(),setMessages: vi.fn(),setCursor: vi.fn(),runAgent: vi.fn().mockResolvedValue(undefined),@@ -152,6 +154,8 @@ describe("BackgroundExecutionSessionController", () => {expect(agent.setCursor).toHaveBeenCalledWith(7);expect(agent.connectAgent).toHaveBeenCalledOnce();expect(hydrate).toHaveBeenCalledOnce();+ expect(session.run({ message: "ignored", context: [] })).toBeNull();+ expect(agent.addMessage).not.toHaveBeenCalled();expect(session.getSnapshot()).toMatchObject({messages: [message],eventCursor: 7,@@ -516,19 +520,24 @@ describe("BackgroundExecutionSessionController", () => {it("keeps run-start failures outside attachment state", async () => {const startError = new Error("start failed");+ const agent = {+ ...createAgent(),+ runAgent: vi.fn().mockRejectedValue(startError),+ };const session = new BackgroundExecutionSessionController({- agent: {- ...createAgent(),- runAgent: vi.fn().mockRejectedValue(startError),- },+ agent,hydrate: vi.fn().mockResolvedValue(runningView),cancelRun: vi.fn(),decideApproval: vi.fn(),});- await expect(session.run({ context: [] } as never)).rejects.toBe(+ await expect(session.run({ message: "hello", context: [] })).rejects.toBe(startError,);+ expect(agent.addMessage).toHaveBeenCalledWith(+ expect.objectContaining({ role: "user", content: "hello" }),+ );+ expect(agent.runAgent).toHaveBeenCalledWith({ context: [] });expect(session.getSnapshot().attachment).toEqual({status: "detached",});@@ -548,7 +557,7 @@ describe("BackgroundExecutionSessionController", () => {decideApproval: vi.fn(),});- await expect(session.run({ context: [] } as never)).rejects.toBe(+ await expect(session.run({ message: "hello", context: [] })).rejects.toBe(watchError,);expect(session.getSnapshot().attachment).toMatchObject({diff --git a/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts b/web/src/features/in-app-agent/lib/backgroundExecutionSession.tsindex 954f21fe54..62d12d1b27 100644--- a/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts+++ b/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts@@ -1,12 +1,16 @@-import type { AbstractAgent, AgentSubscriber } from "@ag-ui/client";-import { z } from "zod";+import type { AgentSubscriber } from "@ag-ui/client";import { InAppAgentRunErrorCode, InAppAgentRunStatus } from "@langfuse/shared";import type {+ AgUiRunAgentInput,AgUiMessage,InAppAgentToolApprovalRequest,} from "@langfuse/shared/in-app-agent";-import { AgUiMessageSchema } from "@langfuse/shared/in-app-agent";+import {+ AgUiMessageSchema,+ createInAppAgentMessageId,+ parseInAppAgentInterruptEvent,+} from "@langfuse/shared/in-app-agent";import { BackgroundExecutionConnectionError } from "./backgroundExecutionErrors";import {createInAppAgentDisplayState,@@ -15,7 +19,10 @@ import {type InAppAgentDisplayState,} from "./display";-export type AgentInput = Parameters<AbstractAgent["runAgent"]>[0];+export type BackgroundExecutionRunCommand = {+ message: string;+ context: AgUiRunAgentInput["context"];+};export type ApprovalDecision = {runId: string;@@ -60,8 +67,9 @@ export type BackgroundExecutionView = {};export type BackgroundExecutionSession = {+ readonly conversationId: string;hydrateAndAttach(): Promise<void>;- run(input: AgentInput): Promise<void>;+ run(command: BackgroundExecutionRunCommand): Promise<void> | null;cancel(): Promise<void>;decide(input: ApprovalDecision): Promise<void>;detach(): void;@@ -71,10 +79,12 @@ export type BackgroundExecutionSession = {};type BackgroundExecutionAgent = {+ threadId: string;messages: readonly unknown[];+ addMessage(message: AgUiMessage): void;setMessages(messages: AgUiMessage[]): void;subscribe(subscriber: AgentSubscriber): { unsubscribe(): void };- runAgent(input: AgentInput): Promise<unknown>;+ runAgent(input: { context: AgUiRunAgentInput["context"] }): Promise<unknown>;connectAgent(): Promise<unknown>;abortRun(): void;setCursor(cursor: number): void;@@ -105,6 +115,7 @@ type BackgroundExecutionAgentSubscriber = Pick<>;export class BackgroundExecutionSessionController implements BackgroundExecutionSession {+ readonly conversationId: string;private readonly agent: BackgroundExecutionAgent;private readonly hydrate: () => Promise<BackgroundExecutionHydration>;private readonly cancelRun: (runId: string) => Promise<unknown>;@@ -138,6 +149,7 @@ export class BackgroundExecutionSessionController implements BackgroundExecutiononError?: (error: unknown) => void;initialView?: Partial<BackgroundExecutionView>;}) {+ this.conversationId = config.agent.threadId;this.agent = config.agent;this.hydrate = config.hydrate;this.cancelRun = config.cancelRun;@@ -244,15 +256,32 @@ export class BackgroundExecutionSessionController implements BackgroundExecution}}- async run(input: AgentInput): Promise<void> {+ run(command: BackgroundExecutionRunCommand): Promise<void> | null {if (this.view.attachment.status === "attaching" ||- this.view.attachment.status === "attached"+ this.view.attachment.status === "attached" ||+ (this.view.currentRun &&+ isCancellableBackgroundRun(this.view.currentRun.status))) {- return;+ return null;}+ return this.executeRun(command);+ }++ private async executeRun(+ command: BackgroundExecutionRunCommand,+ ): Promise<void> {const generation = ++this.attachGeneration;+ const userMessage = {+ id: createInAppAgentMessageId(),+ role: "user",+ content: command.message,+ } satisfies AgUiMessage;+ this.agent.addMessage(userMessage);+ if (!this.view.messages.some((message) => message.id === userMessage.id)) {+ this.observeMessages([...this.view.messages, userMessage]);+ }this.setView({...this.view,currentRun: null,@@ -260,7 +289,7 @@ export class BackgroundExecutionSessionController implements BackgroundExecution});try {- await this.agent.runAgent(input);+ await this.agent.runAgent({ context: command.context });} catch (error) {if (generation !== this.attachGeneration) {return;@@ -527,49 +556,6 @@ export class BackgroundExecutionSessionController implements BackgroundExecution}}-const MastraSuspendEventSchema = z.object({- type: z.literal("mastra_suspend"),- toolCallId: z.string().min(1),- toolName: z.string().min(1),- args: z.unknown().optional(),- runId: z.string().min(1),-});--export function parseInAppAgentInterruptEvent(- event: unknown,-): InAppAgentToolApprovalRequest | null {- if (- !event ||- typeof event !== "object" ||- !("name" in event) ||- event.name !== "on_interrupt"- ) {- return null;- }-- const value = "value" in event ? event.value : undefined;- const parsedValue = typeof value === "string" ? parseJson(value) : value;- const interrupt = MastraSuspendEventSchema.safeParse(parsedValue);-- return interrupt.success- ? {- type: "tool_approval_request",- toolCallId: interrupt.data.toolCallId,- toolName: interrupt.data.toolName,- args: interrupt.data.args,- runId: interrupt.data.runId,- }- : null;-}--function parseJson(value: string): unknown {- try {- return JSON.parse(value) as unknown;- } catch {- return null;- }-}-export function isCancellableBackgroundRun(status: InAppAgentRunStatus,): boolean {diff --git a/web/src/features/in-app-agent/quickActions.ts b/web/src/features/in-app-agent/quickActions.tsindex 2fa11ffc99..0918e74cb1 100644--- a/web/src/features/in-app-agent/quickActions.ts+++ b/web/src/features/in-app-agent/quickActions.ts@@ -1,4 +1,3 @@-import type { InAppAgentMessageEntryPoint } from "@/src/features/in-app-agent/context";import { getInAppAgentProjectRoute } from "@/src/features/in-app-agent/routeContext";import {Activity,@@ -65,24 +64,17 @@ export const IN_APP_AGENT_QUICK_ACTION_CONTEXT_ICONS: Record<evaluation: Lightbulb,};-export type InAppAgentQuickActionAttribution = {- key: string;- category: InAppAgentQuickActionContext;-};-export type InAppAgentSubmitOptions = {- quickAction?: InAppAgentQuickActionAttribution;/** Force a fresh conversation instead of appending to the selected one. */newConversation?: boolean;- /** Which surface sent the message; telemetry only (PostHog + trace- * metadata), never shown to the agent. Defaults to "chat". */- entryPoint?: InAppAgentMessageEntryPoint;+ /** Which surface sent the message; PostHog only. Defaults to "chat". */+ entryPoint?: "chat" | "add-widget-modal";};// Version 1 starter sets. Idea is that periodic curation replaces sets when usage// supports a stronger ranking. Prompts are somewhat product-generic and act on the// current page context without copying customer data.-export const IN_APP_AGENT_QUICK_ACTIONS_BY_CONTEXT = {+const IN_APP_AGENT_QUICK_ACTIONS_BY_CONTEXT = {observability: [{id: "analyze-failure-patterns",@@ -192,7 +184,7 @@ export const IN_APP_AGENT_QUICK_ACTIONS_BY_CONTEXT = {readonly InAppAgentQuickAction[]>;-export const IN_APP_AGENT_FOCUSED_QUICK_ACTIONS = {+const IN_APP_AGENT_FOCUSED_QUICK_ACTIONS = {trace: [{id: "analyze-this-trace",diff --git a/web/src/features/in-app-agent/server/backgroundRunService.ts b/web/src/features/in-app-agent/server/backgroundRunService.tsindex 2d1d3d00b6..687ab39200 100644--- a/web/src/features/in-app-agent/server/backgroundRunService.ts+++ b/web/src/features/in-app-agent/server/backgroundRunService.ts@@ -21,10 +21,10 @@ import {createInAppAgentMessageId,createInAppAgentRunId,parseInAppAgentApprovalDecisionEvent,+ parseInAppAgentInterruptEvent,type AgUiRunAgentInput,} from "@langfuse/shared/in-app-agent";-import { parseInAppAgentInterruptEvent } from "@langfuse/shared/in-app-agent/server/human-in-the-loop";-import { getInAppAgentPrefixedToolName } from "@langfuse/shared/in-app-agent/server/tools";+import { getInAppAgentPrefixedToolName } from "@langfuse/shared/in-app-agent/server/mcpPolicy";import {ensureOwnedConversation,getConversationEvents,diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.tsindex b03fc7ebf0..39ef2976cf 100644--- a/worker/src/features/in-app-agent/executeInAppAgentRun.ts+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.ts@@ -18,6 +18,7 @@ import {} from "@langfuse/shared/src/server/auth/apiKeys";import {getInAppAgentInstrumentationTraceId,+ parseInAppAgentInterruptEvent,type AgUiEvent,type AgUiRunAgentInput,type InAppAgentToolApprovalRequest,@@ -39,7 +40,6 @@ import {createInAppAgentToolPolicy,getInAppAgentMcpAllowedToolNames,getInAppAgentRegistryToolName,- parseInAppAgentInterruptEvent,shouldFlushPersistedEvent,toPersistableAgentEvent,type InAppAgentUserAccess,