mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(sandbox): guard enable path and harden UI rendering performance (#341)
* fix(sandbox): guard enable path and harden UI rendering performance * chore(deps): allow esbuild postinstall in pnpm onlyBuiltDependencies * fix(sandbox): align availability reporting and address PR review feedback * fix(sandbox): block planning without process attestation
This commit is contained in:
1 parent
ba8e8f4b29
commit
d63198ec8a
19 files changed
+501
-82
No files matched your search
@@ -321,6 +321,82 @@ describe('MessagePart', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('tool output clamping', () => {
|
||||
const createBashPartWithOutput = (output: string): MessagePartType => ({
|
||||
type: 'tool',
|
||||
tool: 'bash',
|
||||
sessionID: 'test-session',
|
||||
state: {
|
||||
status: 'completed',
|
||||
input: { command: 'echo big' },
|
||||
output,
|
||||
time: { start: Date.now(), end: Date.now() + 100 },
|
||||
},
|
||||
})
|
||||
|
||||
const expandTool = () => {
|
||||
fireEvent.click(screen.getByRole('button'))
|
||||
}
|
||||
|
||||
it('renders small output in full without omission marker', () => {
|
||||
setup()
|
||||
const output = Array.from({ length: 200 }, (_, i) => `line ${i}`).join('\n')
|
||||
render(<MessagePart part={createBashPartWithOutput(output)} />)
|
||||
|
||||
expandTool()
|
||||
|
||||
expect(output).not.toContain('omitted')
|
||||
const pre = document.querySelector('pre')!
|
||||
expect(pre.textContent).toBe(output)
|
||||
expect(screen.queryByText(/omitted/)).toBeNull()
|
||||
})
|
||||
|
||||
it('clamps very large output with a marker and keeps head and tail', () => {
|
||||
setup()
|
||||
const lines: string[] = []
|
||||
for (let i = 0; lines.join('\n').length < 200_000; i++) {
|
||||
lines.push(`line-${i} ${'x'.repeat(80)} marker-start-${i === 0 ? 'FIRST' : ''}${i === 0 ? 'FIRST-marker-end' : ''}`)
|
||||
}
|
||||
const output = lines.join('\n')
|
||||
const startMarker = 'marker-start-FIRST'
|
||||
const endContent = `line-${lines.length - 1}`
|
||||
|
||||
render(<MessagePart part={createBashPartWithOutput(output)} />)
|
||||
|
||||
expandTool()
|
||||
|
||||
expect(screen.queryByText(/omitted/)).not.toBeNull()
|
||||
const pre = document.querySelector('pre')!
|
||||
const rendered = pre.textContent ?? ''
|
||||
expect(rendered).toContain('omitted — use the copy button for the full output')
|
||||
expect(rendered.length).toBeLessThan(35_000)
|
||||
expect(output.startsWith(rendered.split('\n')[0])).toBe(true)
|
||||
expect(rendered).toContain(endContent)
|
||||
expect(rendered).toContain(startMarker)
|
||||
})
|
||||
|
||||
it('copies the full unclamped output via the copy button', async () => {
|
||||
setup()
|
||||
const writeText = vi.fn().mockResolvedValue(undefined)
|
||||
Object.assign(navigator, { clipboard: { writeText } })
|
||||
|
||||
const lines: string[] = []
|
||||
for (let i = 0; lines.join('\n').length < 200_000; i++) {
|
||||
lines.push(`line-${i} ${'y'.repeat(80)}`)
|
||||
}
|
||||
const output = lines.join('\n')
|
||||
|
||||
render(<MessagePart part={createBashPartWithOutput(output)} />)
|
||||
|
||||
expandTool()
|
||||
|
||||
const copyButton = screen.getByTitle('Copy output')
|
||||
fireEvent.click(copyButton)
|
||||
|
||||
await expect(vi.waitFor(() => writeText.mock.calls[0]?.[0])).resolves.toBe(output)
|
||||
})
|
||||
})
|
||||
|
||||
describe('simpleChatMode', () => {
|
||||
const createToolPart = (): MessagePartType => ({
|
||||
type: 'tool',
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useRef, useEffect } from 'react'
|
||||
import { useState, useRef, useEffect, useMemo, memo } from 'react'
|
||||
import { unwrapSandboxExecCommand } from '@opencode-manager/shared/utils'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
@@ -13,6 +13,33 @@ import { getToolSpecificRender } from './FileToolRender'
|
||||
|
||||
type ToolPart = components['schemas']['ToolPart']
|
||||
|
||||
const DISPLAY_LIMIT = 30_000
|
||||
const DISPLAY_HEAD_LENGTH = 20_000
|
||||
const DISPLAY_TAIL_LENGTH = 10_000
|
||||
|
||||
function formatOmittedSize(size: number): string {
|
||||
return size < 1024 * 1024
|
||||
? `${(size / 1024).toFixed(1)} KB`
|
||||
: `${(size / (1024 * 1024)).toFixed(1)} MB`
|
||||
}
|
||||
|
||||
function clampDisplayText(text: string): string {
|
||||
if (text.length <= DISPLAY_LIMIT) return text
|
||||
const headCut = text.lastIndexOf('\n', DISPLAY_HEAD_LENGTH)
|
||||
const head = headCut === -1 ? text.slice(0, DISPLAY_HEAD_LENGTH) : text.slice(0, headCut)
|
||||
const tailFrom = text.length - DISPLAY_TAIL_LENGTH
|
||||
const tailCut = text.indexOf('\n', tailFrom)
|
||||
const tail = tailCut === -1 ? text.slice(tailFrom) : text.slice(tailCut + 1)
|
||||
const omitted = formatOmittedSize(text.length - head.length - tail.length)
|
||||
const marker = `\n[… ${omitted} omitted — use the copy button for the full output …]\n`
|
||||
return head + marker + tail
|
||||
}
|
||||
|
||||
function BoundedPre({ content, className }: { content: string; className: string }) {
|
||||
const clamped = useMemo(() => clampDisplayText(content), [content])
|
||||
return <pre className={className}>{clamped}</pre>
|
||||
}
|
||||
|
||||
interface ToolCallPartProps {
|
||||
part: ToolPart
|
||||
onFileClick?: (filePath: string, lineNumber?: number) => void
|
||||
@@ -29,8 +56,8 @@ function getTaskSessionId(part: ToolPart): string | undefined {
|
||||
}
|
||||
|
||||
function ClickableJson({ json, onFileClick }: { json: unknown; onFileClick?: (filePath: string) => void }) {
|
||||
const jsonString = JSON.stringify(json, null, 2)
|
||||
const references = detectFileReferences(jsonString)
|
||||
const jsonString = useMemo(() => JSON.stringify(json, null, 2), [json])
|
||||
const references = useMemo(() => detectFileReferences(jsonString), [jsonString])
|
||||
|
||||
if (references.length === 0) {
|
||||
return <pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-words">{jsonString}</pre>
|
||||
@@ -68,7 +95,7 @@ function ClickableJson({ json, onFileClick }: { json: unknown; onFileClick?: (fi
|
||||
return <pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-words">{parts}</pre>
|
||||
}
|
||||
|
||||
export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCallPartProps) {
|
||||
export const ToolCallPart = memo(function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCallPartProps) {
|
||||
const { preferences } = useSettings()
|
||||
const { userBashCommands } = useUserBash()
|
||||
const taskSessionId = part.tool === 'task' ? getTaskSessionId(part) : undefined
|
||||
@@ -79,7 +106,10 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
const rawCommand = part.tool === 'bash' && typeof part.state.input?.command === 'string'
|
||||
? part.state.input.command
|
||||
: undefined
|
||||
const displayCommand = rawCommand === undefined ? undefined : unwrapSandboxExecCommand(rawCommand)
|
||||
const displayCommand = useMemo(
|
||||
() => (rawCommand === undefined ? undefined : unwrapSandboxExecCommand(rawCommand)),
|
||||
[rawCommand]
|
||||
)
|
||||
const isSandboxedCommand = rawCommand !== undefined && (
|
||||
displayCommand !== rawCommand ||
|
||||
(part.state.status === 'completed' && (part.state.metadata as Record<string, unknown> | undefined)?.sandbox === true)
|
||||
@@ -260,10 +290,8 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
)}
|
||||
</div>
|
||||
<div className="relative">
|
||||
<pre className="bg-accent p-3 rounded text-xs overflow-x-auto whitespace-pre-wrap">
|
||||
{output}
|
||||
</pre>
|
||||
<CopyButton content={output} title="Copy output" className="absolute top-2 right-2" />
|
||||
<BoundedPre content={output ?? ''} className="bg-accent p-3 rounded text-xs overflow-x-auto whitespace-pre-wrap" />
|
||||
<CopyButton content={output ?? ''} title="Copy output" className="absolute top-2 right-2" />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
@@ -401,9 +429,10 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
<div className="text-sm">
|
||||
<div className="text-muted-foreground mb-1">Output:</div>
|
||||
<div className="relative">
|
||||
<pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-all">
|
||||
{part.state.status === 'completed' ? part.state.output : ''}
|
||||
</pre>
|
||||
<BoundedPre
|
||||
content={part.state.status === 'completed' ? part.state.output ?? '' : ''}
|
||||
className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-all"
|
||||
/>
|
||||
{part.state.status === 'completed' && part.state.output && (
|
||||
<CopyButton content={part.state.output} title="Copy output" className="absolute top-1 right-1" iconSize="sm" />
|
||||
)}
|
||||
@@ -420,13 +449,14 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
{part.state.status === 'error' && (
|
||||
<div className="text-sm">
|
||||
<div className="text-red-600 dark:text-red-400 mb-1">Error:</div>
|
||||
<pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-words text-red-600 dark:text-red-300">
|
||||
{part.state.error}
|
||||
</pre>
|
||||
<BoundedPre
|
||||
content={part.state.error ?? ''}
|
||||
className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-words text-red-600 dark:text-red-300"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user