feat(agent): restrict sandbox filesystem access and update system prompt instructions

This commit is contained in:
yiling
2026-05-31 12:55:45 +08:00
parent 9c20938628
commit be7dc45051
+16 -2
View File
@@ -5,6 +5,7 @@ import { getModel } from '@earendil-works/pi-ai'
import { Type, type TSchema } from '@sinclair/typebox'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import { DatabaseSessionStorage } from './database-session-storage'
import { analyzeAssetMediaTool } from './tools/analyze-asset-media'
import { createCreateFileTool } from './tools/create-file'
@@ -109,7 +110,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
const piDir = path.join(process.cwd(), '.pi')
if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true })
const allowWrite = [piDir, '/tmp']
const allowWrite = [piDir]
// SandboxManager.initialize is a global operation that applies to the entire process.
await SandboxManager.initialize({
@@ -118,9 +119,10 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
deniedDomains: [],
},
filesystem: {
allowRead: [piDir],
denyRead: [os.homedir(), '.env', '.env.*', '*.pem', '*.key'],
allowWrite,
denyWrite: ['.env', '.env.*', '*.pem', '*.key'],
denyRead: ['~/.ssh', '~/.aws', '~/.gnupg'],
},
})
@@ -148,6 +150,18 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
model,
systemPrompt: async () => {
let prompt = systemPrompt
// Sandbox environment restrictions
prompt +=
'\n\n' +
[
'# Sandbox Environment Restrictions',
'Your shell environment (the `bash` tool) is highly sandboxed to protect the host system:',
'1. **Filesystem Isolation**: You only have read and write permissions to the `.pi` folder in the project root directory.',
'2. **Read/Write Restrictions**: All reading and writing to directories outside `.pi` (e.g. your home directory `~/`, `/tmp`, `/etc`, or the rest of the workspace) are strictly denied by the sandbox security policy.',
"3. **Avoid System Temp Directory Writes**: You must strictly avoid any commands or shell constructs that attempt to write to the system temporary directory `/tmp` or `/var/tmp`. For example, do not use Bash here documents (`<<EOF` or `<<'EOF'`) in your commands, as the bash shell internally implements here documents by writing temporary files to `/tmp`. If you need to create a file or write content, write it directly using file creation tools or write to files located inside the `.pi` directory without utilizing here documents.",
].join('\n')
if (teamSkills.length > 0) {
prompt += formatSkillsForPrompt(teamSkills)
}