diff --git a/backend/test/services/sandbox/command.test.ts b/backend/test/services/sandbox/command.test.ts index 96396fa4..2ae476bb 100644 --- a/backend/test/services/sandbox/command.test.ts +++ b/backend/test/services/sandbox/command.test.ts @@ -4,12 +4,14 @@ import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSyn import { tmpdir } from 'node:os' import path from 'node:path' import { ENV, getAssistantOpenCodeDir, getReposPath, getScheduleWorktreesPath } from '@opencode-manager/shared/config/env' +import { unwrapSandboxExecCommand } from '@opencode-manager/shared/utils' import { WORKSPACE_SANDBOX_NAME, SANDBOX_UNAVAILABLE_PREFIX, buildBlockedCommand, buildCanonicalSandboxSpec, buildSandboxCreateArgs, + buildSandboxExecCommandString, buildSandboxInspectArgs, buildSandboxListArgs, buildSandboxRemoveArgs, @@ -518,6 +520,30 @@ describe('sandbox command builders', () => { }) }) +describe('unwrapSandboxExecCommand round-trips against the real builder', () => { + it('recovers the original command from a real sandbox exec wrapper', () => { + const directory = '/workspace/repos/ai-test' + for (const command of [ + 'git status', + "echo 'hi there'", + "echo 'x' -- sh -c 'y'", + 'git status\ngit diff\necho done', + ]) { + expect(unwrapSandboxExecCommand(buildSandboxExecCommandString(directory, command))).toBe(command) + } + }) + + it('passes through plain, blocked, empty, and near-miss commands unchanged', () => { + const blocked = buildBlockedCommand('KVM is unavailable') + const nearMiss = buildSandboxExecCommandString('/workspace/repos/ai-test', 'git status').replace(' --no-tty -q ', ' --no-tty ') + + expect(unwrapSandboxExecCommand('git status')).toBe('git status') + expect(unwrapSandboxExecCommand(blocked)).toBe(blocked) + expect(unwrapSandboxExecCommand('')).toBe('') + expect(unwrapSandboxExecCommand(nearMiss)).toBe(nearMiss) + }) +}) + describe('resolveSandboxRuntimeTmpfsSizeMib', () => { it('clamps the quarter-memory floor to the 1-512 MiB range', () => { expect(resolveSandboxRuntimeTmpfsSizeMib(1)).toBe(1) diff --git a/frontend/src/components/message/MessagePart.test.tsx b/frontend/src/components/message/MessagePart.test.tsx index d544f1a0..f4d2b2d2 100644 --- a/frontend/src/components/message/MessagePart.test.tsx +++ b/frontend/src/components/message/MessagePart.test.tsx @@ -282,6 +282,37 @@ describe('MessagePart', () => { expect(screen.getByRole('button')).not.toHaveClass('bg-red-500/20') }) + describe('sandbox indicator', () => { + const createBashPart = (command: string): MessagePartType => ({ + type: 'tool', + tool: 'bash', + sessionID: 'test-session', + state: { + status: 'completed', + input: { command }, + output: 'ok', + time: { start: Date.now(), end: Date.now() + 100 }, + }, + }) + + const wrapped = "'/usr/local/bin/msb' exec ocm-workspace --no-tty -q -u '1001:1001' -w '/workspace/repos/ai-test' --timeout 600s -- sh -c 'git status'" + + it('shows the sandbox badge and the unwrapped command for a sandboxed bash call', () => { + render() + + expect(screen.getByText('sandbox')).toBeInTheDocument() + expect(screen.getByText('git status')).toBeInTheDocument() + expect(screen.queryByText(/msb/)).toBeNull() + }) + + it('omits the sandbox badge for a host bash call', () => { + render() + + expect(screen.queryByText('sandbox')).toBeNull() + expect(screen.getByText('git status')).toBeInTheDocument() + }) + }) + describe('simpleChatMode', () => { const createToolPart = (): MessagePartType => ({ type: 'tool', diff --git a/frontend/src/components/message/ToolCallPart.tsx b/frontend/src/components/message/ToolCallPart.tsx index 77ceae93..68127570 100644 --- a/frontend/src/components/message/ToolCallPart.tsx +++ b/frontend/src/components/message/ToolCallPart.tsx @@ -1,11 +1,13 @@ import { useState, useRef, useEffect } from 'react' +import { unwrapSandboxExecCommand } from '@opencode-manager/shared/utils' import type { components } from '@/api/opencode-types' import { useSettings } from '@/hooks/useSettings' import { useUserBash } from '@/stores/userBashStore' import { useSessionStatusForSession } from '@/stores/sessionStatusStore' import { usePermissions, useQuestions } from '@/contexts/EventContext' import { detectFileReferences } from '@/lib/fileReferences' -import { ExternalLink, Loader2 } from 'lucide-react' +import { ExternalLink, Loader2, Shield } from 'lucide-react' +import { Badge } from '@/components/ui/badge' import { CopyButton } from '@/components/ui/copy-button' import { getToolSpecificRender } from './FileToolRender' @@ -74,10 +76,14 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal const { getForCallID: getPermissionForCallID } = usePermissions() const { getForCallID: getQuestionForCallID } = useQuestions() const outputRef = useRef(null) - const isUserBashCommand = part.tool === 'bash' && - part.state.status === 'completed' && - typeof part.state.input?.command === 'string' && - userBashCommands.has(part.state.input.command) + const rawCommand = part.tool === 'bash' && typeof part.state.input?.command === 'string' + ? part.state.input.command + : undefined + const displayCommand = rawCommand === undefined ? undefined : unwrapSandboxExecCommand(rawCommand) + const isSandboxedCommand = rawCommand !== undefined && displayCommand !== rawCommand + const isUserBashCommand = part.state.status === 'completed' && + typeof displayCommand === 'string' && + userBashCommands.has(displayCommand) const isTodoTool = part.tool === 'todowrite' || part.tool === 'todoread' const [expanded, setExpanded] = useState(isUserBashCommand || isTodoTool || (preferences?.expandToolCalls ?? false)) @@ -134,7 +140,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal case 'edit': return (input.filePath as string) || null case 'bash': - return (input.command as string) || null + return displayCommand || null case 'glob': return (input.pattern as string) || null case 'grep': @@ -153,6 +159,12 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal const previewText = getPreviewText() const isFileTool = ['read', 'write', 'edit'].includes(part.tool) + const sandboxIndicator = isSandboxedCommand ? ( + + + sandbox + + ) : null if (part.tool === 'task') { const sessionId = taskSessionId @@ -225,7 +237,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal } if (isUserBashCommand) { - const command = part.state.input.command as string + const command = displayCommand ?? '' const output = part.state.status === 'completed' ? part.state.output : '' return (
@@ -233,6 +245,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal ✓ $ {command} + {sandboxIndicator} {part.state.status === 'completed' && part.state.time && ( {((part.state.time.end - part.state.time.start) / 1000).toFixed(2)}s @@ -274,6 +287,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal > {getStatusIcon()} {part.tool} + {sandboxIndicator} {previewText && isFileTool ? (
Command:
- $ {typeof part.state.input?.command === 'string' ? part.state.input.command : ''} + $ {displayCommand ?? ''}
@@ -363,12 +377,12 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
Command:
- $ {typeof part.state.input?.command === 'string' ? part.state.input.command : ''} + $ {displayCommand ?? ''}
) : ( diff --git a/shared/src/utils/index.ts b/shared/src/utils/index.ts index adcbed0e..6e4c1fd7 100644 --- a/shared/src/utils/index.ts +++ b/shared/src/utils/index.ts @@ -1,2 +1,3 @@ export * from './jsonc' export * from './repo' +export * from './sandbox-command' diff --git a/shared/src/utils/sandbox-command.ts b/shared/src/utils/sandbox-command.ts new file mode 100644 index 00000000..453d4bda --- /dev/null +++ b/shared/src/utils/sandbox-command.ts @@ -0,0 +1,8 @@ +const SANDBOX_EXEC_WRAPPER_PATTERN = /^'(?:[^']|'\\'')*' exec [A-Za-z0-9_-]+ --no-tty -q -u '(?:[^']|'\\'')*' -w '(?:[^']|'\\'')*' --timeout \d+s -- sh -c '((?:[^']|'\\'')*)'$/ +const SINGLE_QUOTE_ESCAPE_PATTERN = /'\\''/g + +export function unwrapSandboxExecCommand(command: string): string { + const match = SANDBOX_EXEC_WRAPPER_PATTERN.exec(command) + if (match === null) return command + return match[1]!.replace(SINGLE_QUOTE_ESCAPE_PATTERN, "'") +}