🪢 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): make browser execution background-only (#15944)
Lysander Kieselc1bc62660015
diff --git a/.env.dev.example b/.env.dev.exampleindex 2cd6a20701..562fbca4e9 100644--- a/.env.dev.example+++ b/.env.dev.example@@ -212,8 +212,6 @@ LANGFUSE_IN_APP_AGENT_AWS_PROFILE="playground"# Turns run in background by default; without the consumer they die at queue_timeout.QUEUE_CONSUMER_IN_APP_AGENT_RUN_QUEUE_IS_ENABLED="true"# LANGFUSE_IN_APP_AGENT_RUN_QUEUE_PROCESSING_CONCURRENCY=5-# Background is the default; uncomment to force the foreground path locally.-# LANGFUSE_IN_APP_AGENT_EXECUTION_MODE="foreground"# V4 migration flags. Pinned to dual write mode to ensure we cover the tests;# bare deployments get the flipped v4 code defaults (events_only / direct).diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.tsindex f0f6efe2c7..7d79e9da22 100644--- a/packages/shared/src/env.ts+++ b/packages/shared/src/env.ts@@ -534,11 +534,6 @@ const EnvSchema = z.object({.int().positive().default(24 * 60 * 60_000),- // Rollback switch for the in-app agent: flip to "foreground" and ship.- // Nothing sets this per region, so this default is the only place to change.- LANGFUSE_IN_APP_AGENT_EXECUTION_MODE: z- .enum(["background", "foreground"])- .default("background"),// Flat safety ceilings on concurrent non-terminal runs. Per region, so a// small region (JP, staging) can be tightened below its execution capacity.LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_USER: z.coercediff --git a/web/src/features/in-app-agent/ARCHITECTURE.md b/web/src/features/in-app-agent/ARCHITECTURE.mdindex 4a4c6ee044..393ade09c5 100644--- a/web/src/features/in-app-agent/ARCHITECTURE.md+++ b/web/src/features/in-app-agent/ARCHITECTURE.md@@ -1,7 +1,8 @@# In-App Agent Architecture-Why the drawer is built the way it is, what the target shape is once background-execution replaces foreground, and which rules keep the two apart until then.+Why the drawer is built the way it is, how durable worker execution keeps the+browser coherent, and why the unused request-scoped adapter remains isolated+during the rollout observation gate.`README.md` is the operational guide: what each file owns, how a run flows, howthe sandbox and MCP authorization work. This document covers the parts you@@ -69,19 +70,18 @@ The two message-pruning helpers are the deliberate exception: they prunefrom the worker, so they sit in `packages/shared/src/in-app-agent/messages.ts`and are used by both replay sanitization and render-time settling.-## Current state: two execution paths+## Current state: one browser path, one retained adapter```mermaidflowchart LRsubgraph Browser- P["InAppAiAgentProvider"]- F["HttpAgent (foreground)"]S["BackgroundExecutionSessionController"]R["project → smooth → drawer"]endsubgraph WebServer["web server"]- H["handler.ts (streaming route)"]+ H["handler.ts (legacy POST adapter)"]RT["router.ts / backgroundRunService.ts"]+ WR["watch/route.ts"]endsubgraph Shared["packages/shared"]L[("in_app_agent_events")]@@ -89,32 +89,27 @@ flowchart LRendW["worker: executeInAppAgentRun"]- P --> F- F -->|"SSE, request-scoped"| HH --> AH --> L- P --> S- S -->|"tRPC startRun"| RT+ S -->|"tRPC start + snapshot"| RTRT -->|"enqueue"| WW --> AW --> L- S -->|"snapshot + SSE tail above cursor"| RT+ S -->|"SSE tail above cursor"| WRRT --> L+ WR --> L- P --> RS -.->|"canonical + displayState"| R- F -.->|"canonical + displayState"| R```-Both paths share the agent runtime, persistence, tools, approvals and the entire-render tree. What is genuinely forked is the run driver (an in-request stream-versus a queued worker run), approval resume (request continuation versus a-continuation run), and the client state machine.+The browser has no edge to `handler.ts`: it always uses the background session.+The authenticated POST adapter remains callable only while rollout observation+confirms that production sends it no traffic. It is not an execution mode or an+operational rollback lever.-Foreground's run cannot outlive the browser session. Background's can: closing-the drawer detaches observation without cancelling the run, and reopening-hydrates one snapshot and resumes the tail above the persisted cursor.+Closing the drawer detaches observation without cancelling the worker run, and+reopening hydrates one snapshot and resumes the tail above the persisted cursor.## Target state: background only@@ -137,25 +132,17 @@ flowchart LRS --> R```-Getting there is a deletion, not a redesign: remove `HttpAgent` wiring, the-foreground state in the provider (`foregroundMessages`, its display state, its-seeding), and the streaming route. The contract the remaining path uses is-already the final one.+The remaining execution-path change is deleting the isolated streaming route+and its request-scoped adapter after the observation gate. The browser and its+session contract are already in their final shape; separate dead-code cleanup+can follow without changing that contract.### The quarantine rule-Until foreground is deleted, do not introduce an abstraction whose purpose is-to normalize the foreground and background run drivers or client state-machines. Pure transcript contracts and presentation code may remain shared.--The temptation is to write an adapter that makes them interchangeable. That-adapter would be the most complex code in the feature and would have to be-untangled later rather than deleted.--Concretely: foreground-only members are marked delete-with-foreground, shared-code between the paths must be independent of how the server executes a run-(the projection, the drawer, the approval wire contract), and behavior tests-cover each path at its own seam.+Until the POST adapter is deleted, do not reconnect it to the browser or add an+abstraction that makes it interchangeable with the durable session. Shared+runtime contracts must remain independent of how a server run is driven so the+adapter can be removed without redesigning the worker path.## Change rulesdiff --git a/web/src/features/in-app-agent/README.md b/web/src/features/in-app-agent/README.mdindex b93374b316..252153677d 100644--- a/web/src/features/in-app-agent/README.md+++ b/web/src/features/in-app-agent/README.md@@ -1,13 +1,13 @@# In-App AgentThe in-app agent is Langfuse's project-scoped assistant inside the authenticated-product UI. Background execution through a worker-owned run is the default; the-request-scoped foreground path remains as the rollback target until it is-deleted.+product UI. Browser turns execute durably in a worker while the browser observes+their persisted event stream. The request-scoped POST adapter remains only for+the rollout observation gate; the browser no longer calls it.`ARCHITECTURE.md` covers why the feature is shaped this way: the one-log /-three-derivations contract, where code lives and why, the target state after-foreground is removed, and the rules that keep the two execution paths apart.+three-derivations contract, where code lives and why, and the temporary boundary+around the unused request-scoped adapter.Read it before changing how messages are represented or where logic lives.## Core Model@@ -18,24 +18,14 @@ projection around those events.The browser owns interaction state and submits intent. The server owns authorization, run/message IDs, request sanitization, MCP credentials, runtime configuration, tool access, persistence, and replay.-A conversation can have one active run. The browser uses one execution mode for-a turn:--- **Background (default):** the browser starts a durable run, hydrates one- persisted transcript/cursor snapshot, and observes its tail. Closing the- drawer detaches observation without cancelling the server run.-- **Foreground (rollback):** `HttpAgent` owns the live request. The run cannot- outlive the browser session and is capped by the route's 120s `maxDuration`.--`lib/executionMode.ts` resolves which one applies, from the deployment's-`LANGFUSE_IN_APP_AGENT_EXECUTION_MODE` read through-`inAppAgent.getExecutionMode`. There is no per-user override. See "Rollout And-Migration" below.+A conversation can have one active run. The browser starts a durable run,+hydrates one persisted transcript/cursor snapshot, and observes its tail.+Closing the drawer detaches observation without cancelling the worker run.## Major Files- `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/handler.ts`: streaming route and authority boundary for auth, request sanitization, run creation, MCP credentials, and terminal state.+- `server/handler.ts`: legacy request-scoped adapter retained during the rollout observation gate. No supported browser path calls it.- `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 helpers, pending tool approval persistence, and resume approval validation/consumption.- `server/tools.ts`: custom agent tools with strict schemas and scoped, user-visible behavior.@@ -52,15 +42,14 @@ Migration" below.- `server/sandbox/providers/*`: provider adapters for local Docker and Lambda MicroVM sandboxes.- `server/sandbox/types.ts`: runtime-neutral sandbox interface used by tools/agent.- `constants.ts`: stable names shared across prompts, tools, persistence, and rendering.-- `components/InAppAiAgentProvider.tsx`: mode selection, foreground transport,- query integration, and the React bridge to the background session.+- `components/InAppAiAgentProvider.tsx`: query integration and the React bridge+ to the background session.- `components/ControlledInAppAgentWindow.tsx` and`components/InAppAgentWindow.tsx`: prop-driven rendering and the explicit- foreground/background execution controls.+ execution controls.- `lib/backgroundAgentClient.ts`: durable run start/watch AG-UI adapter.- `lib/backgroundExecutionSession.ts`: background transcript, cursor, run,approval, attachment, cancel, and decision owner.-- `lib/executionMode.ts`: reads the deployment's execution mode.Outside this feature folder, `packages/in-app-agent-sandbox-runtime/src/*` provides the shared sandbox runtime and contract types used by both the local Docker provider and the Lambda MicroVM image.@@ -73,14 +62,16 @@ flowchart TBControlled --> Window["InAppAgentWindow.tsx\nmain UI entrypoint"]Window --> Message["InAppAgentMessage.tsx\nmessage rendering"]- Provider -->|AG-UI HttpAgent + SSE| Handler["server/handler.ts\nserver route for agent runs"]+ Handler["server/handler.ts\nlegacy POST adapter; no browser caller"]Provider -->|useSyncExternalStore| Session["backgroundExecutionSession.ts\nbackground state + actions"]Session --> BackgroundClient["backgroundAgentClient.ts\nstart + watch adapter"]Display["lib/display.ts\ndisplay state + projection"] -.-> ProviderDisplay -.-> SessionDisplay -.-> Snapshot["server/conversationSnapshot.ts\ncanonical + display state"]Router --> Snapshot- BackgroundClient -->|start/watch| Router+ BackgroundClient -->|start| Router+ Session -->|hydrate| Router+ BackgroundClient -->|SSE tail| Watch["app/api/in-app-agent/watch\nauthenticated watch route"]Provider -->|tRPC| Router["server/router.ts\ntRPC routes (non-streaming)"]Schema["schema.ts\nshared AG-UI contract"] -.-> Provider@@ -93,6 +84,12 @@ flowchart TBHandler --> Sandbox["server/sandbox/service.ts\nsandbox lifecycle"]Handler --> Agent["server/agent.ts\nagent runtime"]Handler --> HumanLoop+ Router -->|enqueue| Worker["worker executeInAppAgentRun\ndurable run driver"]+ Worker --> Persistence+ Worker --> Sandbox+ Worker --> Agent+ Worker --> HumanLoop+ Watch --> PersistenceSandbox --> PersistenceSandbox --> Providers["server/sandbox/providers/*\nDocker or Lambda MicroVM"]Agent --> HumanLoop@@ -104,28 +101,23 @@ flowchart TB## Run Lifecycles-### Foreground+### Retained POST adapter++The authenticated route still validates an AG-UI request, creates a+request-scoped run, invokes `server/agent.ts`, and streams its events to the+caller. Approval resumes use the pending-approval row managed by+`server/human-in-the-loop.ts`, and the run cannot outlive its request.-1. Browser sends the latest message, conversation state, and screen context through `HttpAgent`.-2. `server/handler.ts` validates the request and creates a server-owned run.-3. `server/handler.ts` loads conversation history and creates or resumes a conversation-scoped sandbox when a provider is enabled.-4. `server/persistence.ts` rebuilds readonly `tool_calls/*.json` files from prior non-sandbox tool calls, and `server/sandbox/service.ts` syncs them into the sandbox before each tool use.-5. `server/handler.ts` creates a temporary in-app-agent MCP API key and passes the signed-in user's project role/admin state plus optional sandbox access into the agent runtime.-6. `server/agent.ts` filters Langfuse MCP tools through RBAC, exposes sandbox tools (`read`, `write`, `edit`, `bash`) when available, and for approved Langfuse MCP resumes adds a tool-scoped override payload.-7. `server/agent.ts` connects Mastra to Langfuse MCP with the temporary API key and sends the override in `x-langfuse-in-app-agent-tool-override` when a single approved mutating MCP tool may run.-8. `server/agent.ts` streams normalized AG-UI events, calls telemetry hooks, and lets the request `onFinish` cleanup persist/suspend the sandbox at turn end.-9. `server/instrumentation.ts` records prompt metadata, stream events, completion, aborts, and errors.-10. `server/persistence.ts` stores compacted events and reconstructs replay messages.-11. `InAppAiAgentProvider.tsx` renders live AG-UI state and hydrates selected conversations through `server/router.ts`.+There is no browser execution mode that selects this adapter. It remains only+so rollout observation can confirm that it receives no traffic before deletion.-### Background+### Supported durable lifecycle-1. The provider selects the background client unless the resolved execution mode- is foreground, and starts the durable run through `startRun`.+1. The provider starts the durable run through `startRun`.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`.-3. The session snapshot is the sole owner of background messages, approvals,+3. The session snapshot is the sole owner of messages, approvals,current run, cancellation, and attachment state. React subscribes with`useSyncExternalStore`; it does not mirror those facts into component state.4. Approval and cancellation promises represent the durable mutation. A later@@ -137,14 +129,11 @@ flowchart TB## Client State Ownership- React Query owns conversation-list and persisted conversation query state.-- The foreground provider state owns the live `HttpAgent` transcript,- approvals, and request lifecycle.-- `BackgroundExecutionSessionController` owns the equivalent background facts;+- `BackgroundExecutionSessionController` owns the live execution facts;persisted query data only seeds the coherent bootstrap view consumed by thesame external-store hook before a controller exists. Rendering never unionsquery state with a live session snapshot.-- `InAppAgentWindow` receives one discriminated execution-UI value, so- foreground copy cannot be combined with background Stop state.+- `InAppAgentWindow` receives execution controls derived from that session.- Display pacing remains in `useSmoothStreamingMessages`; canonical AG-UImessages are never rewritten for animation.- Messages and their display state always come from the same source. The@@ -152,8 +141,7 @@ flowchart TBagainst persisted display state or the reverse.The provider is still a large integration controller. Do not add another-background state mirror or a generic long-lived foreground/background adapter;-the foreground branch is scheduled for deletion after background rollout.+background state mirror or reconnect the retained POST adapter to the browser.## Consumers And Stability Boundaries@@ -169,21 +157,10 @@ Stop intentionally emits no new analytics event during the internal rollout:the existing run lifecycle is sufficient to diagnose correctness, and there isno product decision that a separate click event would answer yet.-## Rollout And Migration+## Operations-Background execution is the default everywhere.--**Rollback is one line:** flip the `LANGFUSE_IN_APP_AGENT_EXECUTION_MODE`-default in `packages/shared/src/env.ts` to `"foreground"` and ship. Nothing sets-that variable per region, so that default is the single source of truth, and a-one-line PR puts every deployment back on the request-scoped path. It takes-effect on each client's next page load.--The variable exists on top of the default so an operator can override without a-code change if a redeploy is too slow. Anything that sets it per region would-become a second place to check during an incident, which is why nothing does.-It is server-owned rather than `NEXT_PUBLIC_*` because `web/Dockerfile` bakes-`NEXT_PUBLIC_*` at image build time.+Rollback is a code revert; there is no execution-mode environment override and+background execution does not require a schema rollback.`QUEUE_CONSUMER_IN_APP_AGENT_RUN_QUEUE_IS_ENABLED` must be on wherever turns runin background, otherwise runs commit as `QUEUED` and die at `queue_timeout`. It@@ -192,9 +169,9 @@ is already enabled in every Langfuse Cloud region. Checkits default of 20 equals full US capacity and exceeds JP and staging.Conversation switching, detached invalidations, retry, and conversation-list run-statuses are separate project issues. After background execution is stable, the-foreground transport and its state can be removed rather than preserved as a-permanent abstraction.+statuses are separate project issues. After the observation gate confirms that+the POST adapter receives no traffic, it can be deleted rather than preserved as+a permanent rollback path.## Sandbox Runtime@@ -232,12 +209,12 @@ Session reuse only relies on an existing live or suspended runtime instance iden## MCP Tool Authorization-The in-app agent uses two request-scoped inputs when calling Langfuse MCP:+The supported worker path uses two run-scoped inputs when calling Langfuse MCP:- A temporary project-scoped API key marked as an in-app-agent key.- An optional server-generated tool override sent with `x-langfuse-in-app-agent-tool-override`.-The API key authenticates the request and scopes it to the project. Without an override, in-app-agent keys are restricted to MCP tools annotated with `readOnlyHint: true`. When the user approves a single Langfuse MCP tool call, `server/handler.ts` creates a JSON override naming that one unprefixed MCP registry tool and passes it to the MCP route through the request header above.+The API key authenticates the request and scopes it to the project. Without an override, in-app-agent keys are restricted to MCP tools annotated with `readOnlyHint: true`. When the user approves a single Langfuse MCP tool call, the worker continuation creates a JSON override naming that one unprefixed MCP registry tool and passes it to the MCP route through the request header above. The retained POST adapter creates equivalent inputs inside `server/handler.ts`.MCP registry behavior:@@ -249,17 +226,21 @@ RBAC is the first gate for Langfuse MCP tools. Before a tool is exposed to the mHuman approval is separate from the MCP tool override. `server/agent.ts` classifies every Langfuse MCP tool in `IN_APP_AGENT_LANGFUSE_MCP_TOOL_APPROVALS`, using unprefixed MCP registry names and either `"auto"` or `"approval"`. The map is keyed by a type-only `McpToolName` union derived from the MCP feature modules, and tests compare this map against `toolRegistry`, so adding a Langfuse MCP tool requires an explicit in-app agent approval classification without exporting MCP feature modules into production in-app-agent code.-`IN_APP_AGENT_AUTO_APPROVED_TOOL_NAMES` is generated from that map by prefixing Langfuse MCP tools with `langfuse_` and adding local tools such as `IN_APP_AGENT_REDIRECT_TOOL_NAME`; docs MCP tools are auto-approved by the `langfuseDocs_` prefix. `server/agent.ts` marks every other tool with Mastra `requireApproval: true`. Mastra emits an interrupt, the browser asks the user, and resumed approvals are validated by `server/handler.ts` against the pending approval row persisted in Postgres. `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. `server/human-in-the-loop.ts` consumes the pending approval, executes approved tool calls at the adapter boundary, and injects synthetic AG-UI tool-call events/messages before the agent continues. The pending approval row stays server-local, stores the tool call identity and a stable argument fingerprint, and expires after a short TTL.+`IN_APP_AGENT_AUTO_APPROVED_TOOL_NAMES` is generated from that map by prefixing Langfuse MCP tools with `langfuse_` and adding local tools such as `IN_APP_AGENT_REDIRECT_TOOL_NAME`; docs MCP tools are auto-approved by the `langfuseDocs_` prefix. `server/agent.ts` marks every other tool with Mastra `requireApproval: true`. 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 retained POST adapter instead validates resumed approvals against its pending+approval row. The row stays server-local, stores the tool-call identity and a+stable argument fingerprint, and expires after a short TTL.Sandbox tools are separate from MCP authorization. When a sandbox provider is enabled, `server/tools.ts` adds local `read`, `write`, `edit`, and `bash` tools backed by the sandbox provider contract rather than the MCP registry.## Change Rules-- Check AG-UI docs at `https://docs.ag-ui.com/llms.txt` before changing event semantics, ordering, stream handling, compaction, tools, state, or `HttpAgent` integration.+- Check AG-UI docs at `https://docs.ag-ui.com/llms.txt` before changing event semantics, ordering, stream handling, compaction, tools, or state.- Keep persisted schemas backward-compatible unless there is an explicit migration.- Keep sandbox conversation state backward-compatible unless there is an explicit migration or cleanup plan.-- Keep presentational components prop-driven; connect tRPC, streaming, and persistence at provider/router/handler boundaries.+- Keep presentational components prop-driven; connect tRPC, watching, streaming, and persistence at provider/router/watch/handler/worker boundaries.- Before changing client state ownership or adding effects, read thefrontend-large-feature architecture and refactor-react-effects skills.-- Protect both the default foreground contract and the background session- contract with behavior tests at their public seams.+- Protect the background session and worker contracts at their public seams;+ retain adapter auth/stream coverage until the adapter is deleted.diff --git a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsxindex b348c8b522..8fa20a937a 100644--- a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx+++ b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx@@ -88,10 +88,8 @@ export function ControlledInAppAgentWindow(submit,submitFeedback,} = useInAppAiAgent();- const isCancellingRun =- execution.type === "background" && execution.isCancelling;- const shouldFlushCancelledRun =- execution.type === "background" && execution.run?.cancelRequested === true;+ const isCancellingRun = execution.isCancelling;+ const shouldFlushCancelledRun = execution.run?.cancelRequested === true;const {finishAnimation,isAnimating,@@ -107,23 +105,19 @@ export function ControlledInAppAgentWindow(// `isCancelling` has cleared, and must not restart the reveal after Stop.shouldFlush: error !== null || isCancellingRun || shouldFlushCancelledRun,});- const windowExecutionUi: InAppAgentWindowExecutionUi =- execution.type === "foreground"- ? execution- : {- type: "background",- notice: getBackgroundRunNotice(execution.run),- stop:- execution.run && isCancellableBackgroundRun(execution.run.status)- ? {- status: execution.isCancelling ? "stopping" : "available",- onStop: () => {- finishAnimation();- execution.cancel();- },- }- : null,- };+ const windowExecutionUi: InAppAgentWindowExecutionUi = {+ notice: getBackgroundRunNotice(execution.run),+ stop:+ execution.run && isCancellableBackgroundRun(execution.run.status)+ ? {+ status: execution.isCancelling ? "stopping" : "available",+ onStop: () => {+ finishAnimation();+ execution.cancel();+ },+ }+ : null,+ };// Only a read-only conversation disables the composer outright. An assistant// turn -- including one paused on an approval -- blocks submission but leaves// the draft editable.diff --git a/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsxindex c4544e8754..6538ccef26 100644--- a/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx+++ b/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx@@ -5,7 +5,6 @@ import {screen,waitFor,} from "@testing-library/react";-import { HttpAgent, type AgentSubscriber } from "@ag-ui/client";import { EventType } from "@ag-ui/core";import { InAppAgentRunErrorCode, InAppAgentRunStatus } from "@langfuse/shared";import type { AgUiMessage } from "@langfuse/shared/in-app-agent";@@ -21,7 +20,6 @@ const providerMocks = vi.hoisted(() => {const decideToolApproval = vi.fn();return {- backgroundExecutionEnabled: false,capture: vi.fn(),startRun,cancelRun,@@ -110,11 +108,6 @@ vi.mock("@/src/features/projects/hooks", () => ({}),}));-vi.mock("@/src/features/in-app-agent/lib/executionMode", () => ({- useInAppAgentBackgroundExecutionEnabled: () =>- providerMocks.backgroundExecutionEnabled,-}));-vi.mock("@/src/features/posthog-analytics/usePostHogClientCapture", () => ({usePostHogClientCapture: () => providerMocks.capture,}));@@ -211,71 +204,11 @@ describe("in-app agent execution", () => {vi.restoreAllMocks();vi.clearAllMocks();vi.unstubAllGlobals();- providerMocks.backgroundExecutionEnabled = false;providerMocks.conversationQuery.data = undefined;window.sessionStorage.clear();});- it("keeps foreground submission and approval working when background execution is disabled", async () => {- let subscriber: AgentSubscriber | undefined;- const runAgent = vi- .spyOn(HttpAgent.prototype, "runAgent")- .mockResolvedValue({ result: undefined, newMessages: [] });- const abortRun = vi.spyOn(HttpAgent.prototype, "abortRun");- vi.spyOn(HttpAgent.prototype, "subscribe").mockImplementation(- (nextSubscriber: AgentSubscriber) => {- subscriber = nextSubscriber;- return { unsubscribe: vi.fn() };- },- );-- const { unmount } = renderExecutionUi();-- expect(screen.queryByRole("status")).not.toBeInTheDocument();- expect(- screen.queryByRole("button", { name: /stop run/i }),- ).not.toBeInTheDocument();-- fireEvent.change(- screen.getByRole("textbox", { name: "Message the assistant" }),- { target: { value: "Investigate this project" } },- );- fireEvent.click(screen.getByRole("button", { name: "Send message" }));-- await waitFor(() => {- expect(runAgent).toHaveBeenCalledOnce();- });- expect(providerMocks.startRun).not.toHaveBeenCalled();-- await act(async () => {- await subscriber?.onCustomEvent?.({- event: {- name: "on_interrupt",- value: {- type: "mastra_suspend",- toolCallId: "tool-call-1",- toolName: "dangerousTool",- runId: "run-1",- },- },- } as never);- });-- fireEvent.click(- await screen.findByRole("button", {- name: "Confirm",- }),- );- await waitFor(() => {- expect(runAgent).toHaveBeenCalledTimes(2);- });-- unmount();- expect(abortRun).toHaveBeenCalled();- });-it("renders persisted background messages and approval actions in the drawer", async () => {- providerMocks.backgroundExecutionEnabled = true;providerMocks.conversationQuery.data = {conversation: {id: "conversation-1",@@ -334,7 +267,6 @@ describe("in-app agent execution", () => {});it("replays prompt and dashboard invalidations after a detached run completes", async () => {- providerMocks.backgroundExecutionEnabled = true;const completedSnapshot = {conversation: {id: "conversation-1",@@ -441,7 +373,6 @@ describe("in-app agent execution", () => {});it("keeps a hydrated in-flight tool call visibly running", async () => {- providerMocks.backgroundExecutionEnabled = true;providerMocks.conversationQuery.data = {conversation: {id: "conversation-1",@@ -493,7 +424,6 @@ describe("in-app agent execution", () => {});it("keeps approval cancellation visibly stopping until hydration settles", async () => {- providerMocks.backgroundExecutionEnabled = true;providerMocks.conversationQuery.data = {conversation: {id: "conversation-1",@@ -550,7 +480,6 @@ describe("in-app agent execution", () => {});it("does not observe a cached active run while the assistant is closed", async () => {- providerMocks.backgroundExecutionEnabled = true;const runningSnapshot = {conversation: { id: "conversation-1", isWriteLocked: false },messages: [],@@ -586,7 +515,6 @@ describe("in-app agent execution", () => {});it("settles the drawer without restarting its activity state after Stop", async () => {- providerMocks.backgroundExecutionEnabled = true;vi.stubGlobal("matchMedia",vi.fn(() => ({@@ -760,7 +688,6 @@ describe("in-app agent execution", () => {});it("settles a newly submitted run as soon as Stop receives a terminal status", async () => {- providerMocks.backgroundExecutionEnabled = true;vi.stubGlobal("matchMedia",vi.fn(() => ({diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsxindex 4b49460549..399c07c376 100644--- a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx@@ -18,19 +18,16 @@ const controlledAgent = vi.hoisted(() => ({isRunning: true,isSelectedConversationHydrating: false,isSubmitting: false,- execution: { type: "foreground" } as- | { type: "foreground" }- | {- type: "background";- run: {- id: string;- status: InAppAgentRunStatus;- errorCode: string | null;- cancelRequested: boolean;- } | null;- isCancelling: boolean;- cancel: () => void;- },+ execution: {+ run: null as {+ id: string;+ status: InAppAgentRunStatus;+ errorCode: string | null;+ cancelRequested: boolean;+ } | null,+ isCancelling: false,+ cancel: vi.fn(),+ },invalidateConversations: vi.fn(),liveMessageVersion: 0,loadMoreConversations: vi.fn(),@@ -80,7 +77,7 @@ function windowElement(const props: InAppAgentWindowProps = {conversations: [],error: null,- executionUi: { type: "foreground" },+ executionUi: { notice: null, stop: null },hasMoreConversations: false,isAssistantTurnInProgress: false,isExpanded: false,@@ -303,7 +300,6 @@ describe("ControlledInAppAgentWindow stop", () => {controlledAgent.value.isRunning = true;controlledAgent.value.pendingToolApprovals = [];controlledAgent.value.execution = {- type: "background",run: {id: "run-1",status: InAppAgentRunStatus.RUNNING,diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsxindex eca5746d0f..f530241f54 100644--- a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx@@ -588,7 +588,7 @@ const meta = preview.meta({],args: {error: null,- executionUi: { type: "foreground" as const },+ executionUi: { notice: null, stop: null },isExpanded: false,isConversationInteractionDisabled: false,conversations,@@ -998,7 +998,6 @@ export const BackgroundRun = meta.story({args: {isAssistantTurnInProgress: true,executionUi: {- type: "background",notice: null,stop: { status: "available", onStop: fn() },},@@ -1088,9 +1087,8 @@ export const BackgroundRunStops = meta.story({}executionUi={isSettled- ? { type: "background", notice: null, stop: null }+ ? { notice: null, stop: null }: {- type: "background",notice: phase === "stopping" ? "Stopping the run…" : null,stop: {status: phase === "stopping" ? "stopping" : "available",diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsxindex 0af04d2e19..4e4b3c6005 100644--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx@@ -256,16 +256,13 @@ type InAppAgentWindowCloseButtonProps =onClose: () => void;};-export type InAppAgentWindowExecutionUi =- | { type: "foreground" }- | {- type: "background";- notice: string | null;- stop: {- status: "available" | "stopping";- onStop: () => void;- } | null;- };+export type InAppAgentWindowExecutionUi = {+ notice: string | null;+ stop: {+ status: "available" | "stopping";+ onStop: () => void;+ } | null;+};export type InAppAgentWindowProps = {conversations: InAppAgentWindowConversation[];@@ -407,10 +404,8 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {const isComposerDisabled = isConversationInteractionDisabled;const isSubmitDisabled =isComposerDisabled || isRateLimited || isAssistantTurnInProgress;- const backgroundNotice =- executionUi.type === "background" ? executionUi.notice : null;- const executionStop =- executionUi.type === "background" ? executionUi.stop : null;+ const backgroundNotice = executionUi.notice;+ const executionStop = executionUi.stop;const canStopRun = executionStop !== null && isAssistantTurnInProgress;const isCancellingRun = executionStop?.status === "stopping";const viewportRef = useRef<HTMLDivElement>(null);diff --git a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsxindex 7886975554..8f2880bbbf 100644--- a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx+++ b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx@@ -11,21 +11,14 @@ import {type Dispatch,type SetStateAction,} from "react";-import {- EventType,- HttpAgent,- type AbstractAgent,- type AgentSubscriber,-} from "@ag-ui/client";+import { EventType, type AgentSubscriber } from "@ag-ui/client";import { useSession } from "next-auth/react";import { useRouter } from "next/router";import useSessionStorage from "@/src/components/useSessionStorage";-import { env } from "@/src/env.mjs";import {createInAppAgentConversationId,createInAppAgentMessageId,- createInAppAgentRunId,} from "@langfuse/shared/in-app-agent";import { IN_APP_AGENT_REDIRECT_TOOL_NAME } from "@langfuse/shared/in-app-agent";import {@@ -36,23 +29,18 @@ import {type AgUiMessage,type InAppAgentMessageFeedback,type InAppAgentMessageFeedbackValue,- type InAppAgentRuntimeState,type InAppAgentToolApprovalRequest,} from "@langfuse/shared/in-app-agent";import {createInAppAgentDisplayState,deserializeInAppAgentDisplayState,projectInAppAgentMessagesForDisplay,- recordInAppAgentMessagesForDisplay,- recordInAppAgentToolCallForDisplay,type InAppAgentDisplayState,} from "@/src/features/in-app-agent/lib/display";import { InAppAgentBackgroundClient } from "@/src/features/in-app-agent/lib/backgroundAgentClient";-import { useInAppAgentBackgroundExecutionEnabled } from "@/src/features/in-app-agent/lib/executionMode";import {BackgroundExecutionSessionController,isCancellableBackgroundRun,- parseInAppAgentInterruptEvent,type AgentInput,type BackgroundExecutionRunView,type BackgroundExecutionSession,@@ -112,15 +100,6 @@ export type InAppAgentEntryPoint =| "dashboard_widget"| "v4_migration";-const getConversationAgentState = (- projectId: string,- conversationId: string,- isNewConversation: boolean,-): InAppAgentRuntimeState =>- isNewConversation- ? { type: "newConversation", projectId }- : { type: "existingConversation", projectId, conversationId };-function useBackgroundExecutionView(session: BackgroundExecutionSession | null,bootstrapView: BackgroundExecutionView,@@ -149,7 +128,7 @@ const NOOP_CONTEXT: InAppAiAgentContextType = {isSubmitting: false,pendingToolApprovals: [],isSelectedConversationHydrating: false,- execution: { type: "foreground" },+ execution: { run: null, isCancelling: false, cancel: () => undefined },error: null,messages: [],liveMessageVersion: 0,@@ -188,14 +167,11 @@ export type InAppAiAgentConversation = {isWriteLocked: boolean;};-type InAppAiAgentExecution =- | { type: "foreground" }- | {- type: "background";- run: BackgroundExecutionRunView | null;- isCancelling: boolean;- cancel: () => void;- };+type InAppAiAgentExecution = {+ run: BackgroundExecutionRunView | null;+ isCancelling: boolean;+ cancel: () => void;+};type InAppAiAgentContextType = {isAvailable: boolean;@@ -317,44 +293,19 @@ function InAppAiAgentProviderInner({`${FEEDBACK_STORAGE_KEY_PREFIX}:${projectId}`,{},);- const [foregroundMessages, setForegroundMessages] = useState<AgUiMessage[]>(- [],- );- // Only live AG-UI publications increment this version. The display smoother- // uses it to distinguish stream updates from history hydration, including- // updates where the agent mutates message objects in place.- const [foregroundLiveMessageVersion, setForegroundLiveMessageVersion] =- useState(0);- const [displayState, setDisplayState] = useState(- createInAppAgentDisplayState,- );- const [foregroundPendingToolApprovals, setForegroundPendingToolApprovals] =- useState<InAppAgentPendingToolApproval[]>([]);- const foregroundPendingToolApprovalsRef = useRef<- InAppAgentPendingToolApproval[]- >([]);- const [isForegroundRunning, setIsForegroundRunning] = useState(false);const [isExpanded, setIsExpanded] = useState(false);const [isSubmitting, setIsSubmitting] = useState(false);const [loadingEventIds, setLoadingEventIds] = useState<ReadonlySet<string>>(() => new Set(),);const [error, setError] = useState<InAppAgentError | null>(null);- const agentRef = useRef<AbstractAgent | null>(null);+ const agentRef = useRef<InAppAgentBackgroundClient | null>(null);const backgroundSessionRef = useRef<BackgroundExecutionSession | null>(null);const [backgroundSession, setBackgroundSession] =useState<BackgroundExecutionSession | null>(null);- const activeRunIdRef = useRef<string | null>(null);const toolCallNamesRef = useRef(new Map<string, string>());const handledToolCallIdsRef = useRef(new Set<string>());- const backgroundExecutionEnabled =- useInAppAgentBackgroundExecutionEnabled(projectId);- const intentionalAbortRef = useRef(false);const submitInFlightRef = useRef(false);- const runInFlightRef = useRef(false);- const subscriptionRef = useRef<ReturnType<AbstractAgent["subscribe"]> | null>(- null,- );const conversationListQuery =api.inAppAgent.listConversations.useInfiniteQuery(@@ -434,34 +385,22 @@ function InAppAiAgentProviderInner({backgroundSession,bootstrapBackgroundView,);- const currentBackgroundRun = backgroundExecutionEnabled- ? backgroundExecutionView.currentRun- : null;+ const currentBackgroundRun = backgroundExecutionView.currentRun;const isBackgroundRunning =- backgroundExecutionEnabled &&- (backgroundExecutionView.attachment.status === "attaching" ||- backgroundExecutionView.attachment.status === "attached" ||- Boolean(- currentBackgroundRun &&- isActiveInAppAgentRunStatus(currentBackgroundRun.status),- ));- const isRunning = backgroundExecutionEnabled- ? isBackgroundRunning- : isForegroundRunning;+ backgroundExecutionView.attachment.status === "attaching" ||+ backgroundExecutionView.attachment.status === "attached" ||+ Boolean(+ currentBackgroundRun &&+ isActiveInAppAgentRunStatus(currentBackgroundRun.status),+ );+ const isRunning = isBackgroundRunning;const effectiveError =- backgroundExecutionEnabled &&backgroundExecutionView.attachment.status === "error"? getInAppAgentError(backgroundExecutionView.attachment.error): error;- const liveMessageVersion = backgroundExecutionEnabled- ? backgroundExecutionView.liveMessageRevision- : foregroundLiveMessageVersion;+ const liveMessageVersion = backgroundExecutionView.liveMessageRevision;const effectivePendingToolApprovals = useMemo(() => {- if (!backgroundExecutionEnabled) {- return foregroundPendingToolApprovals;- }-return backgroundExecutionView.pendingToolApprovals.map(({ runId, approvalRequest, status }): InAppAgentPendingToolApproval => ({id: approvalRequest.toolCallId,@@ -470,11 +409,7 @@ function InAppAiAgentProviderInner({runId,}),);- }, [- backgroundExecutionEnabled,- backgroundExecutionView.pendingToolApprovals,- foregroundPendingToolApprovals,- ]);+ }, [backgroundExecutionView.pendingToolApprovals]);/*** Messages and their display sidecar always come from the same source, so the* projection below can never fold live messages against persisted state (or@@ -499,49 +434,16 @@ function InAppAiAgentProviderInner({};}- if (backgroundExecutionEnabled) {- return {- messages: backgroundExecutionView.messages,- displayState: backgroundExecutionView.displayState,- isSettled: !isBackgroundRunning,- };- }-- const storedMessages =- conversationQuery.data?.conversation.id === selectedConversationId- ? conversationQuery.data.messages.filter(isAgentConversationMessage)- : undefined;-- if (- !isRunning &&- storedMessages &&- foregroundMessages.length <= storedMessages.length- ) {- return {- messages: storedMessages,- displayState: deserializeInAppAgentDisplayState(- conversationQuery.data?.displayState,- ),- isSettled: true,- };- }-return {- messages: foregroundMessages,- displayState,- isSettled: false,+ messages: backgroundExecutionView.messages,+ displayState: backgroundExecutionView.displayState,+ isSettled: !isBackgroundRunning,};}, [- backgroundExecutionEnabled,backgroundExecutionView.displayState,backgroundExecutionView.messages,- conversationQuery.data,- displayState,- foregroundMessages,isBackgroundRunning,- isRunning,isSelectedConversationNotFound,- selectedConversationId,]);const messagesWithUiState = useMemo(() => {// Unpaired tool calls and empty assistant messages are pruned only once the@@ -552,12 +454,10 @@ function InAppAiAgentProviderInner({dropUnpairedAssistantToolCalls(currentSource.messages),): currentSource.messages;- const messagesWithRunId = backgroundExecutionEnabled- ? attachActiveRunIdToAssistantMessages(- prunedMessages,- currentBackgroundRun?.id ?? null,- )- : prunedMessages;+ const messagesWithRunId = attachActiveRunIdToAssistantMessages(+ prunedMessages,+ currentBackgroundRun?.id ?? null,+ );const messagesWithFeedback = mergeMessagesWithFeedback(messagesWithRunId,selectedConversationId@@ -566,7 +466,6 @@ function InAppAiAgentProviderInner({);const unresolvedActiveRunToolCallIds = new Set<string>();if (- backgroundExecutionEnabled &¤tBackgroundRun &&isActiveInAppAgentRunStatus(currentBackgroundRun.status)) {@@ -617,7 +516,6 @@ function InAppAiAgentProviderInner({};});}, [- backgroundExecutionEnabled,currentBackgroundRun,feedbackByConversationId,currentSource,@@ -662,18 +560,6 @@ function InAppAiAgentProviderInner({Boolean(selectedConversationId) &&conversationQuery.isLoading &&!conversationQuery.data;- const updatePendingToolApprovals = useCallback(- (- updater: (- currentApprovals: InAppAgentPendingToolApproval[],- ) => InAppAgentPendingToolApproval[],- ) => {- const nextApprovals = updater(foregroundPendingToolApprovalsRef.current);- foregroundPendingToolApprovalsRef.current = nextApprovals;- setForegroundPendingToolApprovals(nextApprovals);- },- [],- );const updateLoadingEvent = useCallback((eventId: string, isLoading: boolean) => {setLoadingEventIds((currentIds) => {@@ -697,57 +583,15 @@ function InAppAiAgentProviderInner({currentIds.size > 0 ? new Set() : currentIds,);}, []);- const publishLiveMessages = useCallback(- (messages: readonly AgUiMessage[]) => {- setForegroundMessages([...messages]);- setForegroundLiveMessageVersion((currentVersion) => currentVersion + 1);- },- [],- );- const recordAgentMessagesForDisplay = useCallback(- (messages: AgUiMessage[]) => {- setDisplayState((currentState) =>- recordInAppAgentMessagesForDisplay(currentState, messages),- );- },- [],- );- const publishAgentMessages = useCallback(- (agentMessages: readonly unknown[]) => {- const nextMessages = agentMessages.filter(isAgentConversationMessage);- recordAgentMessagesForDisplay(nextMessages);-- publishLiveMessages(- attachActiveRunIdToAssistantMessages(- nextMessages,- activeRunIdRef.current,- ),- );- },- [publishLiveMessages, recordAgentMessagesForDisplay],- );const resetAgent = useCallback(() => {- if (agentRef.current?.isRunning) {- intentionalAbortRef.current = true;- }-- subscriptionRef.current?.unsubscribe();- subscriptionRef.current = null;backgroundSessionRef.current?.dispose();backgroundSessionRef.current = null;setBackgroundSession(null);- if (!backgroundExecutionEnabled) {- agentRef.current?.abortRun();- }agentRef.current = null;- activeRunIdRef.current = null;toolCallNamesRef.current.clear();handledToolCallIdsRef.current.clear();- setDisplayState(createInAppAgentDisplayState());- foregroundPendingToolApprovalsRef.current = [];- setForegroundPendingToolApprovals([]);clearLoadingEvents();- }, [backgroundExecutionEnabled, clearLoadingEvents]);+ }, [clearLoadingEvents]);useEffect(() => {return () => {@@ -783,43 +627,9 @@ function InAppAiAgentProviderInner({};}, [rateLimitRetryAt]);- const createSharedAgentSubscriber = useCallback(- (- options: {- reportRunError?: boolean;- /**- * Foreground only. The background session owns its own display state,- * so it must not also feed the provider's foreground copy.- */- recordDisplayState?: boolean;- } = {},- ) =>+ const sharedAgentSubscriber = useMemo(+ () =>({- onRunStartedEvent: ({- event,- messages: runMessages,- }: {- event: unknown;- messages: readonly unknown[];- }) => {- if (options.recordDisplayState !== false) {- setDisplayState((currentState) =>- recordInAppAgentMessagesForDisplay(- currentState,- runMessages.filter(isAgentConversationMessage),- ),- );- }-- if (- typeof event === "object" &&- event !== null &&- "runId" in event &&- typeof event.runId === "string"- ) {- activeRunIdRef.current = event.runId;- }- },onEvent: ({ event }) => {if (event.type === EventType.REASONING_MESSAGE_START ||@@ -839,16 +649,6 @@ function InAppAiAgentProviderInner({if (event.type === EventType.TOOL_CALL_START) {toolCallNamesRef.current.set(event.toolCallId, event.toolCallName);- if (options.recordDisplayState !== false) {- setDisplayState((currentState) =>- recordInAppAgentToolCallForDisplay(- currentState,- event.toolCallId,- event.parentMessageId,- ),- );- }-updateLoadingEvent(event.toolCallId, true);return;}@@ -885,80 +685,11 @@ function InAppAiAgentProviderInner({}},onRunErrorEvent: ({ event }) => {- if (intentionalAbortRef.current) {- return;- }-setError(getInAppAgentError(event));- if (options.reportRunError !== false) {- console.error("In-app agent drawer run error", event);- }},}) satisfies AgentSubscriber,[clearLoadingEvents, updateLoadingEvent, utils],);- const ensureSubscription = useCallback(- (agent: AbstractAgent) => {- if (- subscriptionRef.current ||- agent instanceof InAppAgentBackgroundClient- ) {- return;- }-- const sharedSubscriber = createSharedAgentSubscriber();- subscriptionRef.current = agent.subscribe({- ...sharedSubscriber,- onCustomEvent: ({ event }) => {- const approvalRequest = parseInAppAgentInterruptEvent(event);-- if (!approvalRequest) {- return;- }-- const approval: InAppAgentPendingToolApproval = {- id: approvalRequest.toolCallId,- approvalRequest,- status: "pending",- };-- updatePendingToolApprovals((currentApprovals) => {- const existingIndex = currentApprovals.findIndex(- (currentApproval) => currentApproval.id === approval.id,- );-- if (existingIndex === -1) {- return [...currentApprovals, approval];- }-- const nextApprovals = [...currentApprovals];- nextApprovals[existingIndex] = approval;- return nextApprovals;- });- },- onToolCallResultEvent: (params) => {- sharedSubscriber.onToolCallResultEvent(params);- updatePendingToolApprovals((currentApprovals) =>- currentApprovals.filter(- (approval) =>- approval.approvalRequest.toolCallId !== params.event.toolCallId,- ),- );- },- onMessagesChanged: ({ messages }) => {- publishAgentMessages(messages);- },- onStateChanged: ({ messages }) => {- publishAgentMessages(messages);- },- });- },- [- createSharedAgentSubscriber,- publishAgentMessages,- updatePendingToolApprovals,- ],- );const releaseSubmitLock = useCallback(() => {submitInFlightRef.current = false;@@ -966,23 +697,13 @@ function InAppAiAgentProviderInner({}, []);const getOrCreateAgent = useCallback(- (- conversationId: string,- initialMessages: AgUiMessage[],- isNewConversation: boolean,- ) => {+ (conversationId: string, initialMessages: AgUiMessage[]) => {if (agentRef.current?.threadId === conversationId) {return agentRef.current;}resetAgent();- const initialState = getConversationAgentState(- projectId,- conversationId,- isNewConversation,- );-const initialCursor =conversationQuery.data?.conversation.id === conversationId? conversationQuery.data.eventCursor@@ -998,132 +719,114 @@ function InAppAiAgentProviderInner({}: null;- const agent = backgroundExecutionEnabled- ? new InAppAgentBackgroundClient({+ const agent = new InAppAgentBackgroundClient({+ projectId,+ conversationId,+ threadId: conversationId,+ initialMessages,+ cursor: initialCursor,+ startRun: (params) =>+ startRunMutation.mutateAsync({projectId,conversationId,- threadId: conversationId,- initialMessages,- initialState,- cursor: initialCursor,- startRun: (params) =>- startRunMutation.mutateAsync({- projectId,- conversationId,- message: params.message,- context: [...params.context],- }),- })- : new HttpAgent({- url: getInAppAgentUrl(),- threadId: conversationId,- initialMessages,- initialState,- });+ message: params.message,+ context: [...params.context],+ }),+ });agentRef.current = agent;- if (agent instanceof InAppAgentBackgroundClient) {- const nextBackgroundSession = new BackgroundExecutionSessionController({- agent,- // The session owns background display state; the shared subscriber- // must not also record into the foreground-only provider state.- subscriber: createSharedAgentSubscriber({- reportRunError: false,- recordDisplayState: false,- }),- initialView: {- messages: initialMessages,+ const nextBackgroundSession = new BackgroundExecutionSessionController({+ agent,+ subscriber: sharedAgentSubscriber,+ initialView: {+ messages: initialMessages,+ displayState: deserializeInAppAgentDisplayState(+ conversationQuery.data?.conversation.id === conversationId+ ? conversationQuery.data.displayState+ : undefined,+ ),+ eventCursor: initialCursor,+ currentRun: initialRun,+ },+ hydrate: async () => {+ const snapshot = await utils.inAppAgent.getConversation.fetch({+ projectId,+ conversationId,+ });++ return {+ messages: snapshot.messages.filter(isAgentConversationMessage),displayState: deserializeInAppAgentDisplayState(- conversationQuery.data?.conversation.id === conversationId- ? conversationQuery.data.displayState- : undefined,+ snapshot.displayState,),- eventCursor: initialCursor,- currentRun: initialRun,- },- hydrate: async () => {- const snapshot = await utils.inAppAgent.getConversation.fetch({- projectId,- conversationId,- });-- return {- messages: snapshot.messages.filter(isAgentConversationMessage),- displayState: deserializeInAppAgentDisplayState(- snapshot.displayState,- ),- eventCursor: snapshot.eventCursor,- currentRun: snapshot.latestRun- ? {- id: snapshot.latestRun.id,- status: snapshot.latestRun.status,- errorCode: snapshot.latestRun.errorCode,- cancelRequested: snapshot.latestRun.cancelRequested,- }- : null,- pendingToolApprovals: snapshot.pendingToolApprovals.map(- (approval) => ({ ...approval, status: "pending" as const }),- ),- } satisfies Omit<- BackgroundExecutionView,- "attachment" | "cancelStatus" | "liveMessageRevision"- >;- },- cancelRun: (runId) =>- cancelRunMutation.mutateAsync({- projectId,- conversationId,- runId,- }),- decideApproval: (input) =>- decideToolApprovalMutation.mutateAsync({- projectId,- conversationId,- runId: input.runId,- toolCallId: input.toolCallId,- approved: input.approved,- }),- onHydratedSnapshot: ({ messages }) => {- performToolSideEffectsForCompletedToolCalls({- toolCalls: getCompletedToolCalls(messages),- handledToolCallIds: handledToolCallIdsRef.current,- utils,- }).catch((error: unknown) => {- console.error(- "Failed to replay tRPC invalidations after hydrated in-app agent tool calls",- error,- );- });- },- onSettled: () => {- clearLoadingEvents();- utils.inAppAgent.listConversations.invalidate({ projectId });- utils.inAppAgent.getConversation.invalidate({- projectId,- conversationId,- });- releaseSubmitLock();- activeRunIdRef.current = null;- intentionalAbortRef.current = false;- },- });- backgroundSessionRef.current = nextBackgroundSession;- setBackgroundSession(nextBackgroundSession);- }+ eventCursor: snapshot.eventCursor,+ currentRun: snapshot.latestRun+ ? {+ id: snapshot.latestRun.id,+ status: snapshot.latestRun.status,+ errorCode: snapshot.latestRun.errorCode,+ cancelRequested: snapshot.latestRun.cancelRequested,+ }+ : null,+ pendingToolApprovals: snapshot.pendingToolApprovals.map(+ (approval) => ({ ...approval, status: "pending" as const }),+ ),+ } satisfies Omit<+ BackgroundExecutionView,+ "attachment" | "cancelStatus" | "liveMessageRevision"+ >;+ },+ cancelRun: (runId) =>+ cancelRunMutation.mutateAsync({+ projectId,+ conversationId,+ runId,+ }),+ decideApproval: (input) =>+ decideToolApprovalMutation.mutateAsync({+ projectId,+ conversationId,+ runId: input.runId,+ toolCallId: input.toolCallId,+ approved: input.approved,+ }),+ onHydratedSnapshot: ({ messages }) => {+ performToolSideEffectsForCompletedToolCalls({+ toolCalls: getCompletedToolCalls(messages),+ handledToolCallIds: handledToolCallIdsRef.current,+ utils,+ }).catch((error: unknown) => {+ console.error(+ "Failed to replay tRPC invalidations after hydrated in-app agent tool calls",+ error,+ );+ });+ },+ onSettled: () => {+ clearLoadingEvents();+ utils.inAppAgent.listConversations.invalidate({ projectId });+ utils.inAppAgent.getConversation.invalidate({+ projectId,+ conversationId,+ });+ releaseSubmitLock();+ },+ });+ backgroundSessionRef.current = nextBackgroundSession;+ setBackgroundSession(nextBackgroundSession);return agent;},[- backgroundExecutionEnabled,cancelRunMutation,clearLoadingEvents,conversationQuery.data,- createSharedAgentSubscriber,decideToolApprovalMutation,projectId,releaseSubmitLock,resetAgent,+ sharedAgentSubscriber,startRunMutation,utils,],@@ -1160,76 +863,6 @@ function InAppAiAgentProviderInner({[session.data?.user?.name],);- const runAgent = useCallback(- (- agent: AbstractAgent,- conversationId: string,- runParameters?: Parameters<AbstractAgent["runAgent"]>[0],- quickActionAttribution?: InAppAgentQuickActionAttribution,- messageEntryPoint?: InAppAgentMessageEntryPoint,- ) => {- if (runInFlightRef.current) {- return Promise.resolve(false);- }-- runInFlightRef.current = true;- clearLoadingEvents();- setIsForegroundRunning(true);- return (async () => {- try {- await agent.runAgent(- createRunInput(- runParameters,- quickActionAttribution,- messageEntryPoint,- ),- );- return true;- } catch (error) {- if (intentionalAbortRef.current) {- return false;- }-- if (runParameters?.forwardedProps?.command?.resume) {- throw error;- }-- setError(getInAppAgentError(error));- console.error("In-app agent drawer error", error);- return false;- } finally {- const runId = activeRunIdRef.current;- clearLoadingEvents();- setIsForegroundRunning(false);- publishLiveMessages(- attachActiveRunIdToAssistantMessages(- agent.messages.filter(isAgentConversationMessage),- runId,- ),- );- utils.inAppAgent.listConversations.invalidate({ projectId });- utils.inAppAgent.getConversation.invalidate({- projectId,- conversationId,- });- releaseSubmitLock();- activeRunIdRef.current = null;- intentionalAbortRef.current = false;- runInFlightRef.current = false;- }- })();- },- [- projectId,- clearLoadingEvents,- createRunInput,- publishLiveMessages,- releaseSubmitLock,- utils.inAppAgent.getConversation,- utils.inAppAgent.listConversations,- ],- );-const selectConversation = useCallback((conversationId: string | null) => {if (isRunning || conversationId === _selectedConversationId) {@@ -1240,7 +873,6 @@ function InAppAiAgentProviderInner({isInAppAgentRateLimited(currentError) ? currentError : null,);resetAgent();- setForegroundMessages([]);setSelectedConversationId(conversationId);},[_selectedConversationId, isRunning, resetAgent, setSelectedConversationId],@@ -1260,7 +892,6 @@ function InAppAiAgentProviderInner({if (conversationId === selectedConversationId) {resetAgent();- setForegroundMessages([]);setSelectedConversationId(null);}@@ -1309,8 +940,7 @@ function InAppAiAgentProviderInner({isInAppAgentRateLimited(error) ||(options?.newConversation !== true &&isSelectedConversationHydrating) ||- submitInFlightRef.current ||- runInFlightRef.current+ submitInFlightRef.current) {return false;}@@ -1350,31 +980,20 @@ function InAppAiAgentProviderInner({: undefined;const initialMessages = isNewConversation? []- : backgroundExecutionEnabled- ? // Persisted state is the render source on the background path, so- // prefer it over the in-memory transcript.- (storedMessages?.filter(isAgentConversationMessage) ??- getHydratedMessages(foregroundMessages, storedMessages))- : getHydratedMessages(foregroundMessages, storedMessages);+ : (storedMessages?.filter(isAgentConversationMessage) ?? []);// 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,- isNewConversation,- );+ const agent = getOrCreateAgent(conversationId, initialMessages);if (agent.isRunning) {return false;}// Start background turns from the persisted transcript and cursor.- if (agent instanceof InAppAgentBackgroundClient && !isNewConversation) {+ if (!isNewConversation) {agent.setMessages(initialMessages);}- ensureSubscription(agent);-const userMessage = {id: createInAppAgentMessageId(),role: "user",@@ -1382,55 +1001,29 @@ function InAppAiAgentProviderInner({} satisfies AgUiMessage;agent.addMessage(userMessage);- if (!backgroundExecutionEnabled) {- setForegroundMessages(- agent.messages.filter(isAgentConversationMessage),- );- }const entryPoint = options?.entryPoint ?? "chat";if (isNewConversation) {capture("in_app_agent:new_chat_started", { entryPoint });}- capture("in_app_agent:new_chat_turn", {- entryPoint,- // Compares the two paths and confirms a rollback took effect.- executionMode: backgroundExecutionEnabled- ? "background"- : "foreground",- });+ capture("in_app_agent:new_chat_turn", { entryPoint });startedRun = true;- if (backgroundExecutionEnabled) {- const backgroundSession = backgroundSessionRef.current;-- if (!backgroundSession) {- throw new Error("Background execution session is unavailable");- }+ const backgroundSession = backgroundSessionRef.current;- clearLoadingEvents();- backgroundSession- .run(createRunInput(undefined, options?.quickAction, entryPoint))- .catch((error: unknown) => {- if (- backgroundSession.getSnapshot().attachment.status !== "error"- ) {- setError(getInAppAgentError(error));- }- });- } else {- runAgent(- agent,- conversationId,- undefined,- options?.quickAction,- entryPoint,- );+ 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));+ }+ });return true;} catch (error) {setError(getInAppAgentError(error));- if (!backgroundExecutionEnabled) {- console.error("Failed to start in-app agent conversation", error);- }return false;} finally {if (!startedRun) {@@ -1439,19 +1032,15 @@ function InAppAiAgentProviderInner({}},[- backgroundExecutionEnabled,conversationQuery.data,capture,clearLoadingEvents,- ensureSubscription,error,createRunInput,getOrCreateAgent,isSelectedConversationHydrating,isRunning,- foregroundMessages,releaseSubmitLock,- runAgent,selectedConversationId,selectedConversationIsWriteLocked,setSelectedConversationId,@@ -1517,36 +1106,20 @@ function InAppAiAgentProviderInner({// Hydrate transcript and cursor from one snapshot before observing its tail.const attachToConversation = useCallback(async (conversationId: string) => {- if (!backgroundExecutionEnabled) {- return;- }-const initialMessages =conversationQuery.data?.conversation.id === conversationId? conversationQuery.data.messages.filter(isAgentConversationMessage): [];- const agent = getOrCreateAgent(conversationId, initialMessages, false);-- if (!(agent instanceof InAppAgentBackgroundClient)) {- return;- }-- ensureSubscription(agent);+ getOrCreateAgent(conversationId, initialMessages);await backgroundSessionRef.current?.hydrateAndAttach();},- [- backgroundExecutionEnabled,- conversationQuery.data,- ensureSubscription,- getOrCreateAgent,- ],+ [conversationQuery.data, getOrCreateAgent],);const attachToConversationRef = useRef(attachToConversation);attachToConversationRef.current = attachToConversation;const hydratedActiveRunId =open &&- backgroundExecutionEnabled &&conversationQuery.data?.conversation.id === selectedConversationId &&conversationQuery.data.latestRun &&isActiveInAppAgentRunStatus(conversationQuery.data.latestRun.status)@@ -1576,13 +1149,11 @@ function InAppAiAgentProviderInner({// Collapse the drawer when closingsetIsExpanded(false);- if (backgroundExecutionEnabled) {- backgroundSessionRef.current?.detach();- releaseSubmitLock();- }+ backgroundSessionRef.current?.detach();+ releaseSubmitLock();}- if (nextOpen && backgroundExecutionEnabled && selectedConversationId) {+ if (nextOpen && selectedConversationId) {attachToConversation(selectedConversationId).catch(() => undefined);}@@ -1590,7 +1161,6 @@ function InAppAiAgentProviderInner({},[attachToConversation,- backgroundExecutionEnabled,open,releaseSubmitLock,selectedConversationId,@@ -1636,12 +1206,7 @@ function InAppAiAgentProviderInner({conversationQuery.data?.conversation.id === selectedConversationId? conversationQuery.data.messages.filter(isAgentConversationMessage): [];- const agent = getOrCreateAgent(- selectedConversationId,- initialMessages,- false,- );- ensureSubscription(agent);+ getOrCreateAgent(selectedConversationId, initialMessages);const backgroundSession = backgroundSessionRef.current;if (!backgroundSession) {@@ -1654,7 +1219,6 @@ function InAppAiAgentProviderInner({}, [conversationQuery.data,currentBackgroundRun,- ensureSubscription,getOrCreateAgent,selectedConversationId,]);@@ -1675,12 +1239,7 @@ function InAppAiAgentProviderInner({conversationQuery.data?.conversation.id === params.conversationId? conversationQuery.data.messages.filter(isAgentConversationMessage): [];- const agent = getOrCreateAgent(- params.conversationId,- initialMessages,- false,- );- ensureSubscription(agent);+ getOrCreateAgent(params.conversationId, initialMessages);const backgroundSession = backgroundSessionRef.current;if (!backgroundSession) {@@ -1698,7 +1257,7 @@ function InAppAiAgentProviderInner({return false;}},- [conversationQuery.data, ensureSubscription, getOrCreateAgent],+ [conversationQuery.data, getOrCreateAgent],);const resumeToolApproval = useCallback(@@ -1719,139 +1278,42 @@ function InAppAiAgentProviderInner({!approval ||!selectedConversationId ||isRunning ||- runInFlightRef.current ||isInAppAgentRateLimited(error)) {return;}- if (backgroundExecutionEnabled) {- const decisionAccepted = await decideBackgroundToolApproval({- approval,- approved,- conversationId: selectedConversationId,- });- if (decisionAccepted) {- capture("in_app_agent:tool_approval_decided", {- isApproved: approved,- toolName: approval.approvalRequest.toolName,- });- }- return;- }-- capture("in_app_agent:tool_approval_decided", {- isApproved: approved,- toolName: approval.approvalRequest.toolName,+ const decisionAccepted = await decideBackgroundToolApproval({+ approval,+ approved,+ conversationId: selectedConversationId,});-- const agent = agentRef.current;- if (!agent || agent.threadId !== selectedConversationId) {- showErrorToast(- "Failed to resume tool call",- "The interrupted assistant run is no longer available.",- );- return;- }-- updatePendingToolApprovals((currentApprovals) =>- currentApprovals.map((currentApproval) =>- currentApproval.id === approvalId- ? { ...currentApproval, status: "submitting" }- : currentApproval,- ),- );- setError(null);-- try {- ensureSubscription(agent);- const completed = await runAgent(agent, selectedConversationId, {- runId: createInAppAgentRunId(),- forwardedProps: {- command: {- resume: {- approved,- approvalRequest: approval.approvalRequest,- },- },- },+ if (decisionAccepted) {+ capture("in_app_agent:tool_approval_decided", {+ isApproved: approved,+ toolName: approval.approvalRequest.toolName,});-- if (!completed) {- updatePendingToolApprovals((currentApprovals) =>- currentApprovals.map((currentApproval) =>- currentApproval.id === approvalId- ? { ...currentApproval, status: "pending" }- : currentApproval,- ),- );- return;- }-- updatePendingToolApprovals((currentApprovals) =>- currentApprovals.filter(- (currentApproval) => currentApproval.id !== approvalId,- ),- );- } catch (error) {- const errorMessage = getAgentErrorMessage(error);- if (errorMessage === "Invalid forwarded props") {- updatePendingToolApprovals((currentApprovals) =>- currentApprovals.filter(- (currentApproval) => currentApproval.id !== approvalId,- ),- );- setError({- type: "generic",- message: "This tool approval is no longer valid. Please try again.",- });- console.error("Failed to resume in-app agent tool call", error);- return;- }-- updatePendingToolApprovals((currentApprovals) =>- currentApprovals.map((currentApproval) =>- currentApproval.id === approvalId- ? { ...currentApproval, status: "pending" }- : currentApproval,- ),- );- setError(getInAppAgentError(error));- console.error("Failed to resume in-app agent tool call", error);}},[- backgroundExecutionEnabled,capture,decideBackgroundToolApproval,effectivePendingToolApprovals,- ensureSubscription,error,isRunning,- runAgent,selectedConversationId,selectedConversationIsWriteLocked,- updatePendingToolApprovals,],);- const execution = useMemo<InAppAiAgentExecution>(() => {- if (!backgroundExecutionEnabled) {- return { type: "foreground" };- }-- return {- type: "background",+ const execution = useMemo<InAppAiAgentExecution>(+ () => ({run: currentBackgroundRun,isCancelling: isCancellingRun,cancel: cancelRun,- };- }, [- backgroundExecutionEnabled,- cancelRun,- currentBackgroundRun,- isCancellingRun,- ]);+ }),+ [cancelRun, currentBackgroundRun, isCancellingRun],+ );const approveToolCall = useCallback((approvalId: string) => resumeToolApproval(approvalId, true),@@ -1943,17 +1405,6 @@ function isAgentConversationMessage(message: unknown): message is AgUiMessage {return result.success;}-function getHydratedMessages(- localMessages: AgUiMessage[],- storedMessages: readonly unknown[] | undefined,-): AgUiMessage[] {- if (localMessages.length > 0) {- return localMessages;- }-- return storedMessages?.filter(isAgentConversationMessage) ?? [];-}-function mergeMessagesWithFeedback(messages: readonly AgUiMessage[],feedbackByMessageId: Record<string, InAppAgentMessageFeedback> | undefined,@@ -1993,10 +1444,6 @@ function attachActiveRunIdToAssistantMessages(});}-function getInAppAgentUrl() {- return `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/in-app-agent`;-}-function getAgentErrorMessage(error: unknown): string {if (error && typeof error === "object") {const payload = "payload" in error ? error.payload : undefined;diff --git a/web/src/features/in-app-agent/lib/backgroundAgentClient.ts b/web/src/features/in-app-agent/lib/backgroundAgentClient.tsindex e53e0f7b11..002f349312 100644--- a/web/src/features/in-app-agent/lib/backgroundAgentClient.ts+++ b/web/src/features/in-app-agent/lib/backgroundAgentClient.ts@@ -75,12 +75,10 @@ export class InAppAgentBackgroundClient extends AbstractAgent {onStatus?: (status: InAppAgentRunStatusUpdate) => void;threadId?: string;initialMessages?: AgUiMessage[];- initialState?: unknown;}) {super({threadId: config.threadId,initialMessages: config.initialMessages as never,- initialState: config.initialState as never,});this.projectId = config.projectId;diff --git a/web/src/features/in-app-agent/lib/executionMode.ts b/web/src/features/in-app-agent/lib/executionMode.tsdeleted file mode 100644index 947cf84509..0000000000--- a/web/src/features/in-app-agent/lib/executionMode.ts+++ /dev/null@@ -1,20 +0,0 @@-import { api } from "@/src/utils/api";--/**- * Unresolved reports background, the shipped default. Falling back to- * foreground would let a slow query defeat the rollout.- */-export function useInAppAgentBackgroundExecutionEnabled(- projectId: string,-): boolean {- const executionModeQuery = api.inAppAgent.getExecutionMode.useQuery(- { projectId },- {- enabled: Boolean(projectId),- staleTime: Infinity,- gcTime: Infinity,- },- );-- return executionModeQuery.data?.mode !== "foreground";-}diff --git a/web/src/features/in-app-agent/server/router.ts b/web/src/features/in-app-agent/server/router.tsindex cd420cda39..9a1f5da909 100644--- a/web/src/features/in-app-agent/server/router.ts+++ b/web/src/features/in-app-agent/server/router.ts@@ -11,7 +11,6 @@ import {upsertScore,} from "@langfuse/shared/src/server";import { env } from "@/src/env.mjs";-import { env as sharedEnv } from "@langfuse/shared/src/env";import {AgUiContextSchema,getInAppAgentInstrumentationObservationId,@@ -90,14 +89,6 @@ const IN_APP_AGENT_FEEDBACK_SCORE_NAME = "in_app_agent_feedback";const IN_APP_AGENT_FEEDBACK_ENVIRONMENT = "langfuse-in-app-agent";export const inAppAgentRouter = createTRPCRouter({- // Deployment config, not agent access, so no availability assert: an org- // without the entitlement should get an answer rather than an error.- getExecutionMode: protectedProjectProcedureWithoutTracing- .input(z.object({ projectId: z.string() }))- .query(() => ({- mode: sharedEnv.LANGFUSE_IN_APP_AGENT_EXECUTION_MODE,- })),-listConversations: protectedProjectProcedure.input(z.object({