Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cyclops-cs): keep generated keys out of model context (#7192)

r33drichards8f2d2fbc2194
diff --git a/libs/fleet/agent.test.ts b/libs/fleet/agent.test.ts
index 65f5edfb27..4ecd30dee6 100644
--- a/libs/fleet/agent.test.ts
+++ b/libs/fleet/agent.test.ts
@@ -21,6 +21,7 @@ import {
createBrowserSdkCommands,
type BrowserSdk,
} from "./src/browser-sdk-commands.ts"
+import { SensitiveOutputBuffer } from "./src/sensitive-output.ts"
import { gzipSync, gunzipSync } from "./src/node-zlib-browser.ts"
@@ -196,6 +197,68 @@ test("returns argument and SDK failures as shell errors", async () => {
assert.equal(rejected.stderr, "pool unavailable\n")
})
+test("keeps created user key credentials out of model context", async () => {
+ const credentials = {
+ clientId: "cua_client_123",
+ clientSecret: "super-secret-value",
+ tokenUrl: "https://auth.cua.ai/token",
+ name: "automation",
+ scope: ["sandboxes:read"],
+ }
+ const sensitiveOutputs = new SensitiveOutputBuffer()
+ const requests: Parameters<TurnClient>[1][] = []
+ const responses: AssistantMessage[] = [
+ {
+ role: "assistant",
+ content: "",
+ tool_calls: [
+ {
+ id: "call-create-key",
+ type: "function",
+ function: { name: "bash", arguments: JSON.stringify({ command: `createUserKey '["automation"]'` }) },
+ },
+ ],
+ },
+ { role: "assistant", content: "The key was created and shown above.", tool_calls: [] },
+ ]
+ const client: TurnClient = async (_id, messages) => {
+ requests.push(messages)
+ return responses.shift()!
+ }
+ const agent = new BrowserBashAgent(
+ client,
+ createBrowserSdkCommands(
+ { ...inertSdk, createUserKey: async () => credentials },
+ sensitiveOutputs,
+ ),
+ sensitiveOutputs,
+ )
+ const events: AgentEvent[] = []
+
+ await agent.run(
+ "conversation-secret",
+ [{ role: "user", content: "Create an API key" }],
+ event => events.push(event),
+ )
+
+ const toolResult = events.find(
+ (event): event is Extract<AgentEvent, { type: "tool_result" }> => event.type === "tool_result",
+ )
+ assert.deepEqual(toolResult?.sensitiveOutputs, [{ kind: "user_api_key", value: credentials }])
+ assert.equal(requests.length, 2)
+ const modelRequest = JSON.stringify(requests[1])
+ for (const privateValue of [
+ credentials.clientId,
+ credentials.clientSecret,
+ credentials.tokenUrl,
+ credentials.name,
+ ...credentials.scope,
+ ]) {
+ assert.equal(modelRequest.includes(privateValue), false)
+ }
+ assert.match(requests[1][0].content, /shown directly to the user/)
+})
+
test("exposes exact MCP tool names as composable bash commands", async () => {
const calls: Array<{ name: string; arguments: Record<string, unknown> }> = []
const shell = new Bash({
diff --git a/libs/fleet/src/browser-agent.ts b/libs/fleet/src/browser-agent.ts
index 5aab75d1a1..dcc0e3a428 100644
--- a/libs/fleet/src/browser-agent.ts
+++ b/libs/fleet/src/browser-agent.ts
@@ -1,4 +1,5 @@
import { Bash, type Command } from "just-bash/browser"
+import type { SensitiveOutputBuffer, SensitiveOutput } from "./sensitive-output.js"
export const BASH_TIMEOUT = { min: 250, default: 10_000, max: 60_000 } as const
export const BASH_OUTPUT = { min: 256, default: 20_000, max: 100_000 } as const
@@ -52,7 +53,7 @@ export type AgentEvent =
| { type: "generation_start" }
| { type: "assistant_delta"; delta: string }
| { type: "tool_start"; toolCall: ToolCall; arguments: NormalizedBashArguments }
- | { type: "tool_result"; toolCall: ToolCall; arguments: NormalizedBashArguments; result: BashToolResult }
+ | { type: "tool_result"; toolCall: ToolCall; arguments: NormalizedBashArguments; result: BashToolResult; sensitiveOutputs: SensitiveOutput[] }
| { type: "complete"; message: AssistantMessage }
export function normalizeBashArguments(input: BashToolArguments): NormalizedBashArguments {
@@ -94,10 +95,16 @@ export class BrowserBashAgent {
private readonly shells = new Map<string, Bash>()
private readonly client: TurnClient
private readonly customCommands: Command[]
+ private readonly sensitiveOutputs?: SensitiveOutputBuffer
- constructor(client: TurnClient, customCommands: Command[] = []) {
+ constructor(
+ client: TurnClient,
+ customCommands: Command[] = [],
+ sensitiveOutputs?: SensitiveOutputBuffer,
+ ) {
this.client = client
this.customCommands = customCommands
+ this.sensitiveOutputs = sensitiveOutputs
}
async executeBash(conversationID: string, input: BashToolArguments, signal?: AbortSignal): Promise<BashToolResult> {
@@ -180,14 +187,17 @@ export class BrowserBashAgent {
arguments: normalizeBashArguments(JSON.parse(toolCall.function.arguments) as BashToolArguments),
}
})
- const results = await Promise.all(
- calls.map(async ({ toolCall, arguments: arguments_ }) => {
- onEvent({ type: "tool_start", toolCall, arguments: arguments_ })
- const result = await this.executeBash(conversationID, arguments_, signal)
- onEvent({ type: "tool_result", toolCall, arguments: arguments_, result })
- return { toolCall, result }
- }),
- )
+ for (const { toolCall, arguments: arguments_ } of calls) {
+ onEvent({ type: "tool_start", toolCall, arguments: arguments_ })
+ }
+ const results: Array<{ toolCall: ToolCall; result: BashToolResult }> = []
+ for (const { toolCall, arguments: arguments_ } of calls) {
+ this.sensitiveOutputs?.drain()
+ const result = await this.executeBash(conversationID, arguments_, signal)
+ const sensitiveOutputs = this.sensitiveOutputs?.drain() ?? []
+ onEvent({ type: "tool_result", toolCall, arguments: arguments_, result, sensitiveOutputs })
+ results.push({ toolCall, result })
+ }
turnMessages = results.map(({ toolCall, result }) => ({
role: "tool",
tool_call_id: toolCall.id,
diff --git a/libs/fleet/src/browser-sdk-commands.ts b/libs/fleet/src/browser-sdk-commands.ts
index d08f5a8b3d..c84af64752 100644
--- a/libs/fleet/src/browser-sdk-commands.ts
+++ b/libs/fleet/src/browser-sdk-commands.ts
@@ -4,6 +4,7 @@ import {
renderCommandHelp,
type BrowserCommandHelp,
} from "./browser-command-help.js"
+import type { SensitiveOutputBuffer, UserApiKeyCredentials } from "./sensitive-output.js"
export type BrowserSdkMethod = (...args: never[]) => Promise<unknown>
@@ -183,13 +184,13 @@ const SDK_COMMAND_HELP: Record<BrowserSdkCommandName, BrowserCommandHelp> = {
summary: "Create a user API key whose secret is shown once.",
usage: "createUserKey '[\"name\",[\"scope\"]]'",
arguments: ["name: Human-readable key name.", "scopes: Optional array of scopes."],
- output: "A JSON object containing clientId, clientSecret, tokenUrl, name, and scopes.",
+ output: "A safe confirmation. The credential values are displayed directly to the user in the chat UI and are unavailable to the model.",
presentation: [
"Warn before execution that a new credential will be created.",
- "After success, clearly state that the client secret is shown once and render the credential fields in a fenced text block.",
- "Do not repeat the client secret later in the response or include it in a table.",
+ "After success, state that the credentials are shown directly in the chat and must be copied before the page is reloaded.",
+ "Do not ask the user to paste the client secret back into chat.",
],
- safety: "Sensitive mutation. Create only with the user's requested name and scopes; never expose the secret beyond the immediate result.",
+ safety: "Sensitive mutation. Create only with the user's requested name and scopes; credential values never enter model context or server-side chat history.",
examples: ["createUserKey '[\"automation\",[\"fleets:read\"]]'"],
},
deleteUserKey: {
@@ -230,7 +231,23 @@ function errorMessage(error: unknown): string {
return error instanceof Error && error.message ? error.message : "SDK command failed"
}
-export function createBrowserSdkCommands(sdk: BrowserSdk): Command[] {
+function isUserApiKeyCredentials(value: unknown): value is UserApiKeyCredentials {
+ if (!value || typeof value !== "object") return false
+ const credentials = value as Partial<UserApiKeyCredentials>
+ return (
+ typeof credentials.clientId === "string" &&
+ typeof credentials.clientSecret === "string" &&
+ typeof credentials.tokenUrl === "string" &&
+ typeof credentials.name === "string" &&
+ Array.isArray(credentials.scope) &&
+ credentials.scope.every(scope => typeof scope === "string")
+ )
+}
+
+export function createBrowserSdkCommands(
+ sdk: BrowserSdk,
+ sensitiveOutputs?: SensitiveOutputBuffer,
+): Command[] {
return BROWSER_SDK_COMMAND_NAMES.map(name =>
defineCommand(name, async (args, context) => {
if (isHelpRequest(args)) {
@@ -240,6 +257,17 @@ export function createBrowserSdkCommands(sdk: BrowserSdk): Command[] {
const methodArguments = parseArguments(args, decodeStdin(context.stdin))
const method = sdk[name] as (...args: unknown[]) => Promise<unknown>
const result = await method(...methodArguments)
+ if (name === "createUserKey") {
+ if (!isUserApiKeyCredentials(result)) {
+ throw new Error("createUserKey returned invalid credentials")
+ }
+ sensitiveOutputs?.push({ kind: "user_api_key", value: result })
+ return {
+ stdout: '{"status":"created","credentials":"shown directly to the user"}\n',
+ stderr: "",
+ exitCode: 0,
+ }
+ }
return {
stdout: result === undefined ? "" : `${JSON.stringify(result)}\n`,
stderr: "",
diff --git a/libs/fleet/src/pages/AgentChat.css b/libs/fleet/src/pages/AgentChat.css
index 8dd7001580..700f61afb4 100644
--- a/libs/fleet/src/pages/AgentChat.css
+++ b/libs/fleet/src/pages/AgentChat.css
@@ -281,3 +281,8 @@
.agent-chat-prompt textarea {
min-height: 48px;
}
+
+.agent-chat-credential {
+ margin-block-start: 12px;
+ max-width: 720px;
+}
diff --git a/libs/fleet/src/pages/AgentChat.tsx b/libs/fleet/src/pages/AgentChat.tsx
index 2fab5e6ad2..8d7a67b6da 100644
--- a/libs/fleet/src/pages/AgentChat.tsx
+++ b/libs/fleet/src/pages/AgentChat.tsx
@@ -5,7 +5,10 @@ import ChatBubble from "@cloudscape-design/chat-components/chat-bubble";
import Alert from "@cloudscape-design/components/alert";
import Box from "@cloudscape-design/components/box";
import Button from "@cloudscape-design/components/button";
+import Container from "@cloudscape-design/components/container";
+import CopyToClipboard from "@cloudscape-design/components/copy-to-clipboard";
import ExpandableSection from "@cloudscape-design/components/expandable-section";
+import FormField from "@cloudscape-design/components/form-field";
import Header from "@cloudscape-design/components/header";
import PromptInput from "@cloudscape-design/components/prompt-input";
import SpaceBetween from "@cloudscape-design/components/space-between";
@@ -39,6 +42,7 @@ import {
type ToolCall,
} from "../browser-agent";
import { createBrowserMcpCommands } from "../browser-mcp-commands";
+import { SensitiveOutputBuffer, type SensitiveOutput, type UserApiKeyCredentials } from "../sensitive-output";
import { useChatThreads } from "../chat/ChatThreadsContext";
import { localVisualPreviewPath } from "../local-visual-preview";
import {
@@ -56,6 +60,7 @@ interface CommandStep {
id: string;
arguments: NormalizedBashArguments;
result?: BashToolResult;
+ sensitiveOutputs?: SensitiveOutput[];
running?: boolean;
}
@@ -161,6 +166,7 @@ function parseStoredCommand(
toolCall: ChatToolCall,
messages: ChatMessage[],
index: number,
+ sensitiveOutputsByToolCall: Record<string, SensitiveOutput[]>,
): CommandStep | undefined {
if (toolCall.function.name !== "bash") return undefined;
try {
@@ -187,13 +193,17 @@ function parseStoredCommand(
: 20_000,
},
result: toolMessage ? parseStoredResult(toolMessage.content) : undefined,
+ sensitiveOutputs: sensitiveOutputsByToolCall[toolCall.id],
};
} catch {
return undefined;
}
}
-function reconstructTranscript(messages: ChatMessage[]): TranscriptItem[] {
+function reconstructTranscript(
+ messages: ChatMessage[],
+ sensitiveOutputsByToolCall: Record<string, SensitiveOutput[]>,
+): TranscriptItem[] {
return messages.flatMap((message, index) => {
if (message.role === "tool") return [];
const items: TranscriptItem[] = [];
@@ -206,7 +216,12 @@ function reconstructTranscript(messages: ChatMessage[]): TranscriptItem[] {
}
if (message.role === "assistant") {
for (const toolCall of message.tool_calls ?? []) {
- const command = parseStoredCommand(toolCall, messages, index);
+ const command = parseStoredCommand(
+ toolCall,
+ messages,
+ index,
+ sensitiveOutputsByToolCall,
+ );
if (command) items.push({ type: "command", command });
}
}
@@ -266,6 +281,59 @@ function MessageBubble({
);
}
+function UserApiKeyCredentialCard({
+ credentials,
+}: {
+ credentials: UserApiKeyCredentials;
+}) {
+ return (
+ <Container header={<Header variant="h3">API key created</Header>}>
+ <SpaceBetween size="m">
+ <Alert type="warning">
+ These credentials are held only in this page's memory. Copy them
+ before reloading or closing the page.
+ </Alert>
+ <FormField label="Client ID">
+ <CopyToClipboard
+ variant="inline"
+ textToCopy={credentials.clientId}
+ textToDisplay={<code>{credentials.clientId}</code>}
+ copyButtonAriaLabel="Copy client ID"
+ copySuccessText="Client ID copied"
+ copyErrorText="Failed to copy"
+ />
+ </FormField>
+ <FormField label="Client Secret">
+ <CopyToClipboard
+ variant="inline"
+ textToCopy={credentials.clientSecret}
+ textToDisplay={<code>{credentials.clientSecret}</code>}
+ copyButtonAriaLabel="Copy client secret"
+ copySuccessText="Client secret copied"
+ copyErrorText="Failed to copy"
+ />
+ </FormField>
+ <FormField label="Token URL">
+ <CopyToClipboard
+ variant="inline"
+ textToCopy={credentials.tokenUrl}
+ textToDisplay={<code>{credentials.tokenUrl}</code>}
+ copyButtonAriaLabel="Copy token URL"
+ copySuccessText="Token URL copied"
+ copyErrorText="Failed to copy"
+ />
+ </FormField>
+ <FormField label="Name">
+ <Box>{credentials.name}</Box>
+ </FormField>
+ <FormField label="Scopes">
+ <Box>{credentials.scope.join(", ") || "Unrestricted"}</Box>
+ </FormField>
+ </SpaceBetween>
+ </Container>
+ );
+}
+
function CommandStatus({ command }: { command: CommandStep }) {
const { result } = command;
const running = command.running ?? !result;
@@ -304,6 +372,13 @@ function CommandStatus({ command }: { command: CommandStep }) {
)}
</SpaceBetween>
</ExpandableSection>
+ {command.sensitiveOutputs?.map((output, index) =>
+ output.kind === "user_api_key" ? (
+ <div className="agent-chat-credential" key={index}>
+ <UserApiKeyCredentialCard credentials={output.value} />
+ </div>
+ ) : null,
+ )}
</div>
);
}
@@ -334,6 +409,7 @@ export function AgentChat() {
const [draft, setDraft] = useState("");
const [liveItems, setLiveItems] = useState<TranscriptItem[]>([]);
const [liveAssistant, setLiveAssistant] = useState<ChatMessage>();
+ const [sensitiveOutputsByToolCall, setSensitiveOutputsByToolCall] = useState<Record<string, SensitiveOutput[]>>({});
const [assistantLoading, setAssistantLoading] = useState(false);
const [running, setRunning] = useState(false);
const [abortable, setAbortable] = useState(false);
@@ -345,6 +421,7 @@ export function AgentChat() {
const pageRef = useRef<HTMLDivElement>(null);
const transcriptRef = useRef<HTMLElement>(null);
const agentRef = useRef<BrowserBashAgent>();
+ const sensitiveOutputBufferRef = useRef<SensitiveOutputBuffer>();
const abortControllerRef = useRef<AbortController>();
const loadSequenceRef = useRef(0);
const nextRunIdRef = useRef(0);
@@ -378,7 +455,12 @@ export function AgentChat() {
};
}, []);
+ if (!sensitiveOutputBufferRef.current) {
+ sensitiveOutputBufferRef.current = new SensitiveOutputBuffer();
+ }
+
if (!agentRef.current) {
+ const sensitiveOutputs = sensitiveOutputBufferRef.current;
agentRef.current = new BrowserBashAgent(
async (conversationID, messages, signal, onDelta) => {
const message = await streamTurn(
@@ -395,7 +477,11 @@ export function AgentChat() {
),
};
},
- [...createBrowserSdkCommands(browserSdk), ...createBrowserMcpCommands()],
+ [
+ ...createBrowserSdkCommands(browserSdk, sensitiveOutputs),
+ ...createBrowserMcpCommands(),
+ ],
+ sensitiveOutputs,
);
}
@@ -584,6 +670,12 @@ export function AgentChat() {
},
]);
} else if (event.type === "tool_result") {
+ if (event.sensitiveOutputs.length > 0) {
+ setSensitiveOutputsByToolCall((current) => ({
+ ...current,
+ [event.toolCall.id]: event.sensitiveOutputs,
+ }));
+ }
setLiveItems((current) =>
current.map((item) =>
item.type === "command" && item.command.id === event.toolCall.id
@@ -592,6 +684,7 @@ export function AgentChat() {
command: {
...item.command,
result: event.result,
+ sensitiveOutputs: event.sensitiveOutputs,
running: true,
},
}
@@ -803,7 +896,7 @@ export function AgentChat() {
const archived = Boolean(conversation?.archived_at);
const canRecoverLoad = conversationErrorStatus === 401 || conversationErrorStatus === 404;
const transcript = conversation
- ? reconstructTranscript(conversation.messages)
+ ? reconstructTranscript(conversation.messages, sensitiveOutputsByToolCall)
: [];
const visibleItems = [...transcript, ...liveItems];
const showPendingAssistant =
diff --git a/libs/fleet/src/sensitive-output.ts b/libs/fleet/src/sensitive-output.ts
new file mode 100644
index 0000000000..a14d9b58a4
--- /dev/null
+++ b/libs/fleet/src/sensitive-output.ts
@@ -0,0 +1,26 @@
+export interface UserApiKeyCredentials {
+ clientId: string
+ clientSecret: string
+ tokenUrl: string
+ name: string
+ scope: string[]
+}
+
+export type SensitiveOutput = {
+ kind: "user_api_key"
+ value: UserApiKeyCredentials
+}
+
+export class SensitiveOutputBuffer {
+ private outputs: SensitiveOutput[] = []
+
+ push(output: SensitiveOutput): void {
+ this.outputs.push(output)
+ }
+
+ drain(): SensitiveOutput[] {
+ const outputs = this.outputs
+ this.outputs = []
+ return outputs
+ }
+}