feat(agent): finalize sandbox integration with strict isolation and tests

This commit is contained in:
yiling
2026-05-20 21:49:42 +08:00
parent 9b4f68dff2
commit 1d00de51b0
4 changed files with 27 additions and 61 deletions
+12 -28
View File
@@ -1,16 +1,18 @@
import { prisma } from '@/db'
import { logger } from '@/logger'
import { Usage } from '@/services/ai/provider/provider'
import { s3Service } from '@/services/s3/s3'
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
import { getModel, type ImageContent, type TextContent } from '@mariozechner/pi-ai'
import {
AuthStorage,
createAgentSession,
DefaultResourceLoader,
ModelRegistry,
SessionManager,
SettingsManager,
defineTool,
type ToolDefinition,
AuthStorage,
createAgentSession,
DefaultResourceLoader,
defineTool,
ModelRegistry,
SessionManager,
SettingsManager,
type ToolDefinition,
} from '@mariozechner/pi-coding-agent'
import { Type, type TSchema } from '@sinclair/typebox'
import * as fs from 'fs'
@@ -19,8 +21,6 @@ import { DatabaseSessionManager } from './database-session-manager'
import { analyzeAssetMediaTool } from './tools/analyze-asset-media'
import { createReadSkillTool } from './tools/read-skill'
import { createSandboxedBashTool } from './tools/sandboxed-bash'
import { Usage } from '@/services/ai/provider/provider'
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
export interface AutofillField {
id: string
@@ -176,21 +176,6 @@ export class AgentExecutor {
if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true })
const allowWrite = [piDir, '/tmp']
const allowRead = [
piDir,
'/tmp',
'/usr/bin', // Allow common binaries
'/bin',
'/usr/lib',
'/lib',
'/usr/share/zoneinfo', // Essential for time
]
// On macOS, /tmp is a symlink to /private/tmp, and other system paths might need expansion
if (process.platform === 'darwin') {
allowWrite.push('/private/tmp')
allowRead.push('/private/tmp', '/private/var/folders')
}
// NOTE: SandboxManager.initialize is a global operation that applies to the entire process.
// anthropic-experimental/sandbox-runtime currently does not support separate managers per session/team.
@@ -204,9 +189,8 @@ export class AgentExecutor {
},
filesystem: {
allowWrite,
denyRead: ['/', '~'], // Deny all roots
denyWrite: ['/', '~'],
allowRead,
denyWrite: ['.env', '.env.*', '*.pem', '*.key'],
denyRead: ['~/.ssh', '~/.aws', '~/.gnupg'],
},
})
+3 -1
View File
@@ -66,7 +66,9 @@ describe('createSandboxedBashTool', () => {
'bash',
['-c', 'sandboxed-echo hello'],
expect.objectContaining({
env: { TEST_ENV: 'test-value' },
env: expect.objectContaining({
TEST_ENV: 'test-value',
}),
}),
)
+1 -1
View File
@@ -1,5 +1,5 @@
import { createBashTool } from '@mariozechner/pi-coding-agent'
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
import { createBashTool } from '@mariozechner/pi-coding-agent'
import { spawn } from 'node:child_process'
import type { DatabaseSessionManager } from '../database-session-manager'