Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): show what is being approved before offering the decision

M3gA-Mind68a3d6de3558
diff --git a/app/src/features/conversations/components/AssistantUiToolCall.tsx b/app/src/features/conversations/components/AssistantUiToolCall.tsx
index 7611d98c12..650780af46 100644
--- a/app/src/features/conversations/components/AssistantUiToolCall.tsx
+++ b/app/src/features/conversations/components/AssistantUiToolCall.tsx
@@ -1,10 +1,6 @@
-import type {
- ToolApprovalOption,
- ToolCallMessagePart,
- ToolCallMessagePartComponent,
-} from '@assistant-ui/react';
+import type { ToolCallMessagePart, ToolCallMessagePartProps } from '@assistant-ui/react';
import { CheckIcon, ChevronDownIcon, CircleXIcon, Loader2Icon, WrenchIcon } from 'lucide-react';
-import { type ReactNode, useEffect, useState } from 'react';
+import type { FC, ReactNode } from 'react';
import { cn } from '../../../components/assistant-ui/lib/utils';
import {
@@ -12,8 +8,6 @@ import {
CollapsibleContent,
CollapsibleTrigger,
} from '../../../components/assistant-ui/ui/collapsible';
-import { Button } from '../../../components/ui';
-import { useT } from '../../../lib/i18n/I18nContext';
import type {
ToolFailureExplanation,
ToolTimelineEntryStatus,
@@ -116,87 +110,6 @@ export function isApprovalPending(approval: ToolCallMessagePart['approval']): bo
return approval != null && approval.approved === undefined && approval.resolution === undefined;
}
-/**
- * How long a pressed decision stays disabled before the buttons come back.
- * Long enough that a slow-but-succeeding decide never flickers; short enough
- * that a failed one does not strand the only surface that can unblock the turn.
- */
-const DECISION_RETRY_MS = 10_000;
-
-/** i18n key per assistant-ui approval `kind`; `undefined` for kinds we skip. */
-const APPROVAL_LABEL_KEYS: Record<string, string> = {
- 'allow-once': 'chat.approval.approve',
- 'allow-always': 'chat.approval.alwaysAllow',
- 'reject-once': 'chat.approval.deny',
- 'reject-always': 'chat.approval.deny',
-};
-
-/**
- * The decision row for a parked tool call, rendered inline under the call it
- * gates rather than as a banner elsewhere on the page.
- *
- * This is deliberately not `ToolFallbackApproval` from the assistant-ui kit:
- * that component hardcodes English labels and the kit's own button, while every
- * other approval surface in OpenHuman is localized and wears the app's buttons.
- * The wire contract is identical — `respondToApproval({ optionId })` resolves
- * through the runtime to the adapter's `onRespondToToolApproval`, which is what
- * calls `openhuman.approval_decide`.
- */
-function ToolApprovalBar({
- approval,
- respondToApproval,
-}: {
- approval: NonNullable<ToolCallMessagePart['approval']>;
- respondToApproval: (response: { optionId: string }) => void;
-}) {
- const { t } = useT();
- // Local, not derived from the store: the decision RPC is in flight before the
- // socket clears the gate, and a second click in that window would throw
- // ("Tool call has no pending approval") inside the runtime.
- // Which decision is in flight, not merely that one is: only the pressed
- // button says "Working…", the rest stay readable while disabled — the same
- // shape `ApprovalRequestCard` uses.
- const [deciding, setDeciding] = useState<string | null>(null);
- // A successful decide clears the gate and unmounts this bar within a beat. A
- // failed one cannot say so — `respondToApproval` returns void and the runtime
- // swallows the rejection — so re-offer the decision rather than leaving the
- // user staring at a disabled button on a turn that is still parked.
- useEffect(() => {
- if (!deciding) return;
- const timer = window.setTimeout(() => setDeciding(null), DECISION_RETRY_MS);
- return () => window.clearTimeout(timer);
- }, [deciding]);
- const options: readonly ToolApprovalOption[] = approval.options ?? [];
- if (options.length === 0) return null;
-
- return (
- <div
- data-testid="assistant-ui-tool-approval"
- className="flex flex-wrap items-center gap-2 px-3 pb-3">
- {options.map((option, index) => (
- <Button
- key={option.id}
- type="button"
- size="sm"
- variant={index === 0 ? 'primary' : 'secondary'}
- data-analytics-id={`chat-approval-${option.id}`}
- title={option.kind === 'allow-always' ? t('chat.approval.alwaysAllowHint') : undefined}
- disabled={deciding !== null}
- onClick={() => {
- if (deciding !== null) return;
- setDeciding(option.id);
- respondToApproval({ optionId: option.id });
- }}>
- {deciding === option.id
- ? t('chat.approval.deciding')
- : (option.label ??
- (APPROVAL_LABEL_KEYS[option.kind] ? t(APPROVAL_LABEL_KEYS[option.kind]) : option.id))}
- </Button>
- ))}
- </div>
- );
-}
-
export interface AssistantUiToolCallCardProps {
toolName: string;
args?: unknown;
@@ -352,13 +265,25 @@ function toolStatusEnvelope(
: undefined;
}
-export const OpenHumanToolCall: ToolCallMessagePartComponent = props => {
+/**
+ * One tool call in the assistant-ui transcript.
+ *
+ * `approval` is supplied by assistant-ui on every tool part; this component used
+ * to destructure four fields and drop the rest, which is why a parked call
+ * rendered as an ordinary running one with no way to answer it.
+ *
+ * The decision surface itself is passed in rather than built here. It is
+ * `ApprovalRequestCard`, which needs the thread id and the store's
+ * `PendingApproval` — neither of which belongs in this file, and both of which
+ * `ChatToolParts` already resolves for the `composio_connect` route.
+ */
+export const OpenHumanToolCall: FC<
+ ToolCallMessagePartProps & {
+ /** Decision surface for a parked call; rendered under the call's header. */
+ approvalCard?: ReactNode;
+ }
+> = props => {
const envelope = toolStatusEnvelope(props.result);
- // `approval`, `status` and `respondToApproval` are supplied by assistant-ui on
- // every tool part; this component used to destructure four fields and drop the
- // rest, which is why a parked call rendered as an ordinary running one with no
- // way to answer it.
- const pending = isApprovalPending(props.approval);
return (
<AssistantUiToolCallCard
toolName={props.toolName}
@@ -367,12 +292,8 @@ export const OpenHumanToolCall: ToolCallMessagePartComponent = props => {
result={envelope ? envelope.value : props.result}
status={envelope?.status}
failure={envelope?.failure}
- awaitingUser={pending}
- footer={
- pending && props.approval && props.respondToApproval ? (
- <ToolApprovalBar approval={props.approval} respondToApproval={props.respondToApproval} />
- ) : undefined
- }
+ awaitingUser={isApprovalPending(props.approval)}
+ footer={props.approvalCard}
/>
);
};
diff --git a/app/src/features/conversations/components/ChatToolParts.approval.test.tsx b/app/src/features/conversations/components/ChatToolParts.approval.test.tsx
index 679cf7b077..9cfb77de53 100644
--- a/app/src/features/conversations/components/ChatToolParts.approval.test.tsx
+++ b/app/src/features/conversations/components/ChatToolParts.approval.test.tsx
@@ -3,19 +3,26 @@
*
* `AssistantUiChat` overrides assistant-ui's `ToolFallback` with
* `ChatToolFallback`, and the component behind it destructured four fields and
- * dropped `status`, `approval` and `respondToApproval` — so even once the
- * runtime carried a decision, nothing on screen offered it. The kit's own
- * approval-capable fallback renders on no user-facing surface (only the dev
- * demo), which is why a grep for approval support in `components/assistant-ui`
- * looked healthy while the chat had none.
+ * dropped `status` and `approval` — so even once the runtime carried a decision,
+ * nothing on screen offered it. The kit's own approval-capable fallback renders
+ * on no user-facing surface (only the dev demo), which is why a grep for
+ * approval support in `components/assistant-ui` looked healthy while the chat
+ * had none.
+ *
+ * The controls are `ApprovalRequestCard`, the surface AGENTS.md designates for
+ * this gate, so the core's action summary and the decision are one component.
+ * A bespoke bar shipped first and had already lost the summary: it offered
+ * "Always allow" for a `shell` call whose command the user could not read, and
+ * `approve_always_for_tool` persists to the auto-approve allowlist.
*/
import { combineReducers, configureStore } from '@reduxjs/toolkit';
-import { act, render, screen } from '@testing-library/react';
+import { render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Provider } from 'react-redux';
-import { describe, expect, it, vi } from 'vitest';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AssistantUiRuntimeProvider } from '../../../providers/AssistantUiRuntimeProvider';
+import { callCoreRpc } from '../../../services/coreRpcClient';
import chatRuntimeReducer, {
type PendingApproval,
setPendingApprovalForThread,
@@ -37,9 +44,15 @@ vi.mock('../../../services/api/threadApi', () => ({
},
}));
+vi.mock('../../../services/coreRpcClient', () => ({ callCoreRpc: vi.fn() }));
+
const THREAD_ID = 't-1';
const REQUEST_ID = 'appr-1';
+/** The core's own explanation of what it is asking to do. */
+const SUMMARY = 'Run shell — list the repository root';
+const COMMAND = 'ls -la /Users/dev/secret-project';
+
/** The option set the projection puts on a parked call. */
const OPTIONS = [
{ id: 'approve_once', kind: 'allow-once' as const },
@@ -47,13 +60,22 @@ const OPTIONS = [
{ id: 'deny', kind: 'reject-once' as const },
];
+const SHELL_REQUEST: PendingApproval = {
+ requestId: REQUEST_ID,
+ toolName: 'shell',
+ message: SUMMARY,
+ command: COMMAND,
+};
+
function gatedPart(over: Record<string, unknown> = {}) {
return {
type: 'tool-call' as const,
toolName: 'shell',
toolCallId: 'call-1',
- args: { command: 'ls -la' } as never,
- argsText: '{"command":"ls -la"}',
+ // Deliberately argument-less: the gate can park before the `tool_call`
+ // frame lands, and redacted args are the case the summary exists for.
+ args: {} as never,
+ argsText: '{}',
result: undefined,
status: { type: 'requires-action' as const, reason: 'interrupt' as const },
approval: { id: REQUEST_ID, options: OPTIONS },
@@ -94,80 +116,93 @@ function renderInThread(node: React.ReactNode, approval?: PendingApproval) {
);
}
+beforeEach(() => {
+ vi.mocked(callCoreRpc).mockReset();
+ vi.mocked(callCoreRpc).mockResolvedValue(undefined as never);
+});
+
describe('ChatToolFallback — parked approval', () => {
- it('offers the decision on the gated call', () => {
- render(<ChatToolFallback {...gatedPart()} />);
-
- const bar = screen.getByTestId('assistant-ui-tool-approval');
- expect(bar).toBeInTheDocument();
- expect(screen.getByRole('button', { name: 'Approve' })).toBeInTheDocument();
- expect(screen.getByRole('button', { name: 'Always allow' })).toBeInTheDocument();
- expect(screen.getByRole('button', { name: 'Deny' })).toBeInTheDocument();
+ it('shows the core action summary and the exact command', () => {
+ renderInThread(<ChatToolFallback {...gatedPart()} />, SHELL_REQUEST);
+
+ expect(screen.getByText(SUMMARY)).toBeInTheDocument();
+ expect(screen.getByText(COMMAND)).toBeInTheDocument();
+ });
+
+ it('keeps every decision control inside the card that carries the summary', () => {
+ // The consent surface must show what is being consented to. A control that
+ // can render without the summary beside it is the defect this pins:
+ // `approve_always_for_tool` writes the auto-approve allowlist, so deciding
+ // blind is a durable mistake, not a recoverable one.
+ renderInThread(<ChatToolFallback {...gatedPart()} />, SHELL_REQUEST);
+
+ const card = screen.getByRole('alertdialog');
+ expect(within(card).getByText(SUMMARY)).toBeInTheDocument();
+ expect(within(card).getByText(COMMAND)).toBeInTheDocument();
+ for (const name of ['Approve', 'Always allow', 'Deny']) {
+ expect(within(card).getByRole('button', { name })).toBeInTheDocument();
+ }
+ // And nowhere else on the surface.
+ expect(screen.getAllByRole('button', { name: 'Approve' })).toHaveLength(1);
+ expect(screen.getAllByRole('button', { name: 'Always allow' })).toHaveLength(1);
});
it('says the call is awaiting input, not merely running', () => {
// The `awaiting input` label existed but was unreachable: its only trigger
// was a `status` the adapter forwards for `error` / `cancelled` alone.
- render(<ChatToolFallback {...gatedPart()} />);
+ renderInThread(<ChatToolFallback {...gatedPart()} />, SHELL_REQUEST);
expect(screen.getByText('awaiting input')).toBeInTheDocument();
expect(screen.queryByText('running')).not.toBeInTheDocument();
});
- it('sends the chosen option id straight to the runtime', async () => {
- const respondToApproval = vi.fn();
- render(<ChatToolFallback {...gatedPart({ respondToApproval })} />);
+ it('routes a decision to openhuman.approval_decide', async () => {
+ const store = buildStore(SHELL_REQUEST);
+ render(
+ <Provider store={store}>
+ <AssistantUiRuntimeProvider>
+ <ChatToolFallback {...gatedPart()} />
+ </AssistantUiRuntimeProvider>
+ </Provider>
+ );
await userEvent.click(screen.getByRole('button', { name: 'Always allow' }));
- // The id is the core's own `approval_decide` decision literal, so the
- // adapter forwards it without a translation table.
- expect(respondToApproval).toHaveBeenCalledWith({ optionId: 'approve_always_for_tool' });
+ await waitFor(() => expect(callCoreRpc).toHaveBeenCalledTimes(1));
+ expect(callCoreRpc).toHaveBeenCalledWith({
+ method: 'openhuman.approval_decide',
+ params: { request_id: REQUEST_ID, decision: 'approve_always_for_tool' },
+ });
+ await waitFor(() =>
+ expect(store.getState().chatRuntime.pendingApprovalByThread[THREAD_ID]).toBeUndefined()
+ );
});
- it('does not offer a second decision while the first is in flight', async () => {
- const respondToApproval = vi.fn();
- render(<ChatToolFallback {...gatedPart({ respondToApproval })} />);
-
- const approve = screen.getByRole('button', { name: 'Approve' });
- await userEvent.click(approve);
- await userEvent.click(approve);
+ it('re-offers the decision when the decide fails', async () => {
+ // Reusing the card buys this for free: a bespoke bar could not see the
+ // rejection, because the runtime swallows it.
+ vi.mocked(callCoreRpc).mockRejectedValue(new Error('core unreachable'));
+ renderInThread(<ChatToolFallback {...gatedPart()} />, SHELL_REQUEST);
- // A second decision throws "Tool call has no pending approval" inside the
- // runtime, in the window before the socket clears the gate.
- expect(respondToApproval).toHaveBeenCalledTimes(1);
- });
+ await userEvent.click(screen.getByRole('button', { name: 'Approve' }));
- it('re-offers the decision when the decide never lands', async () => {
- // `respondToApproval` returns void and the runtime swallows a rejection, so
- // a failed decide is indistinguishable from a slow one here. Coming back is
- // what keeps a still-parked turn answerable.
- vi.useFakeTimers();
- try {
- const respondToApproval = vi.fn();
- render(<ChatToolFallback {...gatedPart({ respondToApproval })} />);
- const approve = screen.getByRole('button', { name: 'Approve' });
- act(() => approve.click());
- expect(screen.getByRole('button', { name: 'Working…' })).toBeDisabled();
-
- act(() => vi.advanceTimersByTime(10_000));
-
- expect(screen.getByRole('button', { name: 'Approve' })).toBeEnabled();
- } finally {
- vi.useRealTimers();
- }
+ await waitFor(() =>
+ expect(screen.getByText(/Could not record your decision/)).toBeInTheDocument()
+ );
+ expect(screen.getByRole('button', { name: 'Approve' })).toBeEnabled();
});
it('leaves an ordinary running tool alone', () => {
// Every unsettled tool part in a `requires-action` message inherits that
- // status, so the prompt must key off the approval, not off the status.
- render(
+ // status, so the prompt must key off the store's request, not the status.
+ renderInThread(
<ChatToolFallback
{...gatedPart({ toolName: 'web_search', approval: undefined, toolCallId: 'call-2' })}
- />
+ />,
+ SHELL_REQUEST
);
- expect(screen.queryByTestId('assistant-ui-tool-approval')).not.toBeInTheDocument();
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
expect(screen.queryByText('awaiting input')).not.toBeInTheDocument();
});
@@ -188,27 +223,18 @@ describe('ChatToolFallback — parked approval', () => {
expect(screen.getByTestId('assistant-ui-integration-connect')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /connect/i })).toBeInTheDocument();
- expect(screen.queryByTestId('assistant-ui-tool-approval')).not.toBeInTheDocument();
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
});
- it('falls back to the ordinary card when the connect request is not the parked one', () => {
- // A stale part from an earlier turn must not adopt the live request.
+ it('falls back to the ordinary card when the part is not the parked request', () => {
+ // A stale part from an earlier turn must not adopt the live request — it
+ // would render someone else's summary above a live decision.
renderInThread(
- <ChatToolFallback
- {...gatedPart({
- toolName: 'composio_connect',
- approval: { id: 'appr-stale', options: OPTIONS },
- })}
- />,
- {
- requestId: REQUEST_ID,
- toolName: 'composio_connect',
- message: 'Connect Google Drive?',
- toolkit: 'googledrive',
- }
+ <ChatToolFallback {...gatedPart({ approval: { id: 'appr-stale', options: OPTIONS } })} />,
+ SHELL_REQUEST
);
- expect(screen.queryByTestId('assistant-ui-integration-connect')).not.toBeInTheDocument();
- expect(screen.getByTestId('assistant-ui-tool-approval')).toBeInTheDocument();
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
+ expect(screen.queryByText(SUMMARY)).not.toBeInTheDocument();
});
});