Cloudflare Computer 深度拆解:当 AI Agent 决定不再「租容器」,而是「拥有一台电脑」——SQLite VFS、Isolate 调度与智能执行路径的架构手术
引言:从「租容器」到「有电脑」的范式转变
2026年8月,Cloudflare 在 Agents Week 上抛出了一个颠覆性的项目:@cloudflare/computer。不是给每个 Agent 一个容器,而是给一台"电脑"——一个 SQLite 虚拟文件系统 + 可插拔执行后端,让 Agent 在轻量隔离环境和完整 Linux 容器之间自动选择最优执行路径。
这个项目的核心理念可以用一句话概括:让 AI Agent 拥有持久化、可观测、可审计的执行环境,而不是无状态的函数调用。
为什么这很重要?因为 2026 年的 AI Agent 已经不是 2022 年的玩具了。它们需要:
- 持久化文件系统:跨会话保持状态,而不是每次都从零开始
- 可审计的操作日志:每一步文件读写都可追溯,满足企业合规要求
- 弹性执行环境:简单任务走轻量 Isolate,复杂任务自动切换到完整容器
Cloudflare Computer 的创新之处在于:它把"容器"这个运维概念,改造成了"电脑"这个开发者概念。Agent 不需要知道 Isolate 和容器的区别,它只需要知道"我有文件系统、我可以执行命令"。
本文将从架构设计、核心组件、代码实战、性能优化、生产踩坑五个维度,深度拆解这个项目的技术内核。
一、架构设计:从容器抽象到电脑抽象
1.1 传统容器方案的三大痛点
2026年之前,AI Agent 的执行环境主要有两种方案:
方案一:无状态函数(Lambda/FaaS)
- 优点:启动快(毫秒级)、成本低(按调用计费)
- 缺点:无持久化存储、每次启动环境丢失、无法执行复杂任务
方案二:完整容器(Docker/Kubernetes)
- 优点:完整 Linux 环境、持久化存储、可运行任意代码
- 缺点:启动慢(秒级到分钟级)、成本高(按时间计费)、资源浪费
这两种方案存在一个共同的架构缺陷:它们都是从"运维视角"设计的,而不是从"Agent视角"设计的。
从 Agent 视角看,它需要的不是"容器"或"函数",而是:
- 文件系统:可以读、写、编辑文件
- 命令执行:可以运行 shell 命令
- 持久化:下次会话还能看到上次的文件
- 隔离性:不同 Agent 之间互不干扰
- 可观测性:人类可以审计 Agent 做了什么
传统方案要么满足 1-4 但成本高(容器),要么成本低但啥都没有(函数)。Cloudflare Computer 的答案是:把"电脑"这个概念抽象出来,让 Agent 感觉自己在用一台真实的机器,而底层调度由运行时自动完成。
1.2 Cloudflare Computer 的三层架构
┌─────────────────────────────────────────────────────────────┐
│ Agent SDK 兼容层 │
│ read / write / edit / ls / exec / shell │
├─────────────────────────────────────────────────────────────┤
│ Workspace 抽象层 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ VFS(虚拟 │ │ 权限控制 │ │ 审计日志 │ │
│ │ 文件系统) │ │ (读写执行)│ │ (全操作) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ 执行后端层 │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Isolate 轻量级 │◄──►│ Container 完整 │ │
│ │ (Cloudflare │ │ (Cloudflare │ │
│ │ Workers) │ │ Containers) │ │
│ └─────────────────┘ └─────────────────┘ │
│ ▲ ▲ │
│ │ 智能调度器 │ │
│ └───────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Layer 1:Agent SDK 兼容层
暴露给 Agent 的 API 极简:read、write、edit、ls、exec、shell。这些 API 与主流 Agent SDK(LangChain、AutoGPT、Claude Code)完全兼容,Agent 代码无需修改即可迁移。
// Agent 调用示例(伪代码)
const computer = new Computer({ workspaceId: 'my-agent-001' });
// 读文件
const content = await computer.read('config.json');
// 写文件
await computer.write('output.txt', 'Hello from Agent!');
// 执行命令
const result = await computer.exec('npm install lodash');
// 列出文件
const files = await computer.ls('/');
Layer 2:Workspace 抽象层
这是 Cloudflare Computer 的核心创新:
- VFS(虚拟文件系统):基于 SQLite 的虚拟文件系统,所有文件操作都在 SQLite 中完成
- 权限控制:每个 workspace 可以设置读写执行权限,精细控制 Agent 能做什么
- 审计日志:所有文件操作、命令执行都被记录到 SQLite,支持回放和审计
Layer 3:执行后端层
两个执行后端:
- Isolate(轻量级):基于 Cloudflare Workers 的 V8 Isolate,启动快(毫秒级)、成本低,但功能受限(无完整 Linux 环境)
- Container(完整级):基于 Cloudflare Containers 的完整 Linux 容器,启动慢(秒级)、成本高,但功能完整
智能调度器:根据任务特征自动选择后端:
- 简单文件操作(read/write/ls)→ Isolate
- 需要运行外部命令(npm/pip/git)→ Container
- 需要访问网络资源 → Container(可配置)
- 需要特定 Linux 工具 → Container
1.3 SQLite 虚拟文件系统的设计哲学
为什么选择 SQLite 作为虚拟文件系统的存储引擎?
理由一:嵌入式、零依赖、跨平台
SQLite 是嵌入式数据库,不需要独立服务器进程,直接编译到程序中。这意味着:
- 无需启动数据库服务
- 无需配置连接字符串
- 文件系统就是一个
.db文件,可以轻松备份、迁移
理由二:ACID 事务支持
所有文件操作都在事务中完成:
write操作失败会自动回滚- 并发写入不会导致文件损坏
- 支持快照和回滚
理由三:BLOB 存储效率高
文件内容以 BLOB 形式存储在 SQLite 中:
- 自动去重(相同内容的文件只存一份)
- 自动压缩(SQLite 内置压缩支持)
- 随机读写效率高(B-tree 索引)
理由四:审计日志天然支持
所有操作都可以记录在同一数据库中:
CREATE TABLE file_operations (
id INTEGER PRIMARY KEY,
workspace_id TEXT,
operation TEXT, -- read/write/exec
path TEXT,
timestamp INTEGER,
agent_id TEXT,
result TEXT
);
一个 SQLite 文件 = 文件系统 + 审计日志 + 元数据,完美契合"电脑"这个抽象概念。
二、核心组件深度拆解
2.1 Workspace:Agent 的「电脑」
Workspace 是 Cloudflare Computer 的核心抽象。每个 Agent 拥有一个独立的 workspace,就像每台电脑有自己的硬盘。
interface Workspace {
id: string; // workspace 唯一标识
vfs: VirtualFileSystem; // 虚拟文件系统
permissions: Permissions; // 权限配置
auditLog: AuditLog; // 审计日志
backend: 'isolate' | 'container'; // 执行后端(动态切换)
}
Workspace 的生命周期:
// 创建 workspace
const workspace = await computer.createWorkspace({
id: 'my-agent-001',
initialContent: {
'package.json': '{ "name": "my-project" }',
'src/index.ts': 'console.log("hello")'
},
permissions: {
allowRead: true,
allowWrite: true,
allowExec: true,
allowedCommands: ['npm', 'node', 'git'] // 白名单
}
});
// Agent 使用 workspace
await workspace.write('src/utils.ts', 'export const add = (a, b) => a + b;');
await workspace.exec('npm run build');
// 销毁 workspace(可选)
await workspace.destroy();
Workspace 可以从多个来源初始化:
- 从云存储导入:S3/R2 存储桶
- 从 Git 仓库克隆:自动 clone 指定仓库
- 从本地文件上传:用户上传 zip 文件
- 从模板创建:预设的项目模板(React/Vue/Go/Python)
// 从 Git 仓库创建
const workspace = await computer.createWorkspace({
id: 'clone-demo',
fromGit: 'https://github.com/cloudflare/workers-sdk.git',
branch: 'main'
});
// 从 R2 存储桶创建
const workspace = await computer.createWorkspace({
id: 'from-s3',
fromBucket: {
bucket: 'my-bucket',
prefix: 'projects/web/'
}
});
2.2 VFS(虚拟文件系统):SQLite 内核
VFS 是 workspace 的文件系统实现,核心是一个 SQLite 数据库:
-- 文件元数据表
CREATE TABLE files (
id INTEGER PRIMARY KEY,
path TEXT UNIQUE NOT NULL, -- 文件路径(绝对路径)
is_directory INTEGER DEFAULT 0, -- 是否是目录
content_id TEXT, -- 内容 BLOB 的 ID(外键)
size INTEGER, -- 文件大小
created_at INTEGER, -- 创建时间(Unix 时间戳)
modified_at INTEGER, -- 修改时间
mode INTEGER, -- 文件权限(Unix mode)
checksum TEXT -- SHA-256 校验和
);
-- 文件内容表(BLOB 存储)
CREATE TABLE file_contents (
id TEXT PRIMARY KEY, -- UUID
data BLOB, -- 文件二进制内容
compressed INTEGER DEFAULT 0 -- 是否压缩
);
-- 目录结构索引
CREATE INDEX idx_files_parent ON files(path);
VFS 的核心操作实现:
class VirtualFileSystem {
private db: Database; // SQLite 连接
// 读取文件
async read(path: string): Promise<Buffer> {
const stmt = this.db.prepare(`
SELECT fc.data
FROM files f
JOIN file_contents fc ON f.content_id = fc.id
WHERE f.path = ?
`);
const row = stmt.get(path);
if (!row) throw new Error(`ENOENT: ${path}`);
// 记录审计日志
await this.logOperation('read', path);
return row.data;
}
// 写入文件
async write(path: string, content: Buffer): Promise<void> {
const checksum = sha256(content);
const contentId = uuid();
this.db.transaction(() => {
// 插入内容(去重)
this.db.prepare(`
INSERT OR IGNORE INTO file_contents (id, data, compressed)
VALUES (?, ?, 0)
`).run(contentId, content);
// 插入或更新文件元数据
this.db.prepare(`
INSERT INTO files (path, content_id, size, modified_at, checksum)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(path) DO UPDATE SET
content_id = excluded.content_id,
size = excluded.size,
modified_at = excluded.modified_at,
checksum = excluded.checksum
`).run(path, contentId, content.length, Date.now(), checksum);
// 记录审计日志
this.logOperationSync('write', path);
})();
}
// 列出目录
async ls(dirPath: string): Promise<FileInfo[]> {
const stmt = this.db.prepare(`
SELECT path, is_directory, size, modified_at
FROM files
WHERE path LIKE ? AND path NOT LIKE ?
`);
const prefix = dirPath === '/' ? '/' : dirPath + '/';
const rows = stmt.all(prefix + '%', prefix + '%/%');
return rows.map(row => ({
path: row.path,
isDirectory: row.is_directory === 1,
size: row.size,
modifiedAt: row.modified_at
}));
}
// 执行命令(伪代码,实际需要调用后端)
async exec(command: string, args: string[]): Promise<ExecResult> {
// 检查命令是否在白名单
if (!this.isCommandAllowed(command)) {
throw new Error(`Command not allowed: ${command}`);
}
// 根据调度策略选择后端
const backend = this.selectBackend(command);
// 记录审计日志
await this.logOperation('exec', `${command} ${args.join(' ')}`);
// 调用后端执行
return await backend.execute(command, args, this.db);
}
}
VFS 的关键特性:
- 去重存储:相同内容的文件共享同一个 BLOB
- 事务保护:所有操作在事务中完成
- 权限检查:每次操作都检查权限配置
- 审计日志:所有操作自动记录
2.3 智能调度器:Isolate vs Container
智能调度器是 Cloudflare Computer 的性能关键。它根据任务特征自动选择执行后端:
class SmartScheduler {
// 调度规则表
private rules: ScheduleRule[] = [
{
pattern: /^(read|write|ls|edit)$/,
backend: 'isolate',
reason: 'Simple file operations, no external dependencies'
},
{
pattern: /^(npm|yarn|pnpm|pip|cargo)$/,
backend: 'container',
reason: 'Package managers need full Linux environment'
},
{
pattern: /^(git|gh)$/,
backend: 'container',
reason: 'Git operations need network and filesystem access'
},
{
pattern: /^(docker|kubectl)$/,
backend: 'container',
reason: 'Container tools need privileged access'
}
];
// 选择后端
selectBackend(command: string): 'isolate' | 'container' {
// 优先匹配显式规则
for (const rule of this.rules) {
if (rule.pattern.test(command)) {
return rule.backend;
}
}
// 默认走 Container(安全优先)
return 'container';
}
// 动态切换后端
async switchBackend(
workspace: Workspace,
target: 'isolate' | 'container'
): Promise<void> {
// 持久化 VFS(SQLite 文件)
const vfsPath = await workspace.vfs.exportToFile();
// 创建新后端实例
const newBackend = target === 'isolate'
? new IsolateBackend()
: new ContainerBackend();
// 在新后端中恢复 VFS
await newBackend.importVFS(vfsPath);
// 切换后端引用
workspace.backend = target;
}
}
Isolate 后端的特点:
- 启动速度:毫秒级(V8 Isolate 预热)
- 成本:按调用次数计费,极低
- 限制:
- 无法执行外部命令(只能运行 JS/TS)
- 无法访问原生库
- CPU 时间限制(50ms ~ 30s,取决于套餐)
Container 后端的特点:
- 启动速度:秒级(冷启动)到毫秒级(热池)
- 成本:按运行时间计费,较高
- 能力:
- 完整 Linux 环境
- 可以安装任意软件
- 可以运行任意命令
- 无 CPU 时间限制(可配置)
调度策略的数学模型:
设任务的特征向量为 $T = [t_1, t_2, ..., t_n]$,其中:
- $t_1$ = 是否需要外部命令
- $t_2$ = 预估执行时间
- $t_3$ = 是否需要网络访问
- $t_4$ = 是否需要特定工具
后端选择函数:
$$
\text{backend}(T) = \begin{cases}
\text{isolate} & \text{if } t_1 = 0 \land t_2 < t_{\text{threshold}} \land t_3 = 0 \
\text{container} & \text{otherwise}
\end{cases}
$$
其中 $t_{\text{threshold}}$ 是时间阈值,可动态调整。
2.4 权限系统:细粒度访问控制
权限系统采用白名单 + RBAC(基于角色的访问控制)模型:
interface Permissions {
// 文件系统权限
allowRead: boolean;
allowWrite: boolean;
allowDelete: boolean;
// 命令执行权限
allowExec: boolean;
allowedCommands: string[]; // 命令白名单
deniedCommands: string[]; // 命令黑名单
// 网络权限
allowNetwork: boolean;
allowedHosts: string[]; // 允许访问的域名
// 资源限制
maxFileSize: number; // 单文件最大大小
maxTotalSize: number; // 总存储大小
maxExecTime: number; // 最大执行时间
}
// 权限检查示例
function checkPermission(
workspace: Workspace,
operation: 'read' | 'write' | 'exec',
resource: string
): boolean {
const perms = workspace.permissions;
switch (operation) {
case 'read':
return perms.allowRead;
case 'write':
return perms.allowWrite &&
!isExceededSize(workspace, perms.maxTotalSize);
case 'exec':
const [cmd] = resource.split(' ');
return perms.allowExec &&
perms.allowedCommands.includes(cmd) &&
!perms.deniedCommands.includes(cmd);
default:
return false;
}
}
权限配置示例:
// 只读 Agent(数据分析)
const readOnlyWorkspace = await computer.createWorkspace({
id: 'analyzer',
permissions: {
allowRead: true,
allowWrite: false,
allowExec: false,
allowNetwork: false
}
});
// 构建 Agent(可以执行构建命令)
const builderWorkspace = await computer.createWorkspace({
id: 'builder',
permissions: {
allowRead: true,
allowWrite: true,
allowExec: true,
allowedCommands: ['npm', 'node', 'tsc', 'webpack', 'vite'],
allowNetwork: true,
allowedHosts: ['registry.npmjs.org', 'github.com']
}
});
// 安全敏感 Agent(受限命令)
const secureWorkspace = await computer.createWorkspace({
id: 'secure',
permissions: {
allowRead: true,
allowWrite: true,
allowExec: true,
allowedCommands: ['git', 'gh'],
deniedCommands: ['git push --force', 'git reset --hard'],
allowNetwork: true,
allowedHosts: ['github.com']
}
});
2.5 审计系统:全链路可追溯
审计系统记录所有文件操作和命令执行:
-- 审计日志表
CREATE TABLE audit_log (
id INTEGER PRIMARY KEY,
timestamp INTEGER NOT NULL,
workspace_id TEXT NOT NULL,
agent_id TEXT,
operation TEXT NOT NULL, -- read/write/exec/delete
resource TEXT, -- 文件路径或命令
result TEXT, -- success/failure
error TEXT, -- 错误信息
metadata TEXT -- JSON 格式的额外信息
);
CREATE INDEX idx_audit_workspace ON audit_log(workspace_id, timestamp);
CREATE INDEX idx_audit_operation ON audit_log(operation, timestamp);
审计日志查询示例:
// 查询某 workspace 的所有写操作
const writeOps = await db.prepare(`
SELECT * FROM audit_log
WHERE workspace_id = ? AND operation = 'write'
ORDER BY timestamp DESC
LIMIT 100
`).all('my-agent-001');
// 查询某时间段内的所有 exec 操作
const execOps = await db.prepare(`
SELECT * FROM audit_log
WHERE operation = 'exec'
AND timestamp BETWEEN ? AND ?
ORDER BY timestamp
`).all(startTime, endTime);
审计日志导出:
// 导出为 JSON Lines 格式
const exportAuditLog = async (workspaceId: string): Promise<string> => {
const logs = await db.prepare(`
SELECT * FROM audit_log
WHERE workspace_id = ?
ORDER BY timestamp
`).all(workspaceId);
return logs.map(log => JSON.stringify(log)).join('\n');
};
// 导出为人类可读的 Markdown 格式
const exportAuditMarkdown = async (workspaceId: string): Promise<string> => {
const logs = await db.prepare(`
SELECT * FROM audit_log
WHERE workspace_id = ?
ORDER BY timestamp
`).all(workspaceId);
let md = `# Audit Log for Workspace ${workspaceId}\n\n`;
for (const log of logs) {
const time = new Date(log.timestamp).toISOString();
md += `## ${time} - ${log.operation}\n`;
md += `- **Resource**: ${log.resource}\n`;
md += `- **Result**: ${log.result}\n`;
if (log.error) md += `- **Error**: ${log.error}\n`;
md += '\n';
}
return md;
};
三、代码实战:构建一个完整的 Agent 执行环境
3.1 初始化 Cloudflare Computer
# 安装依赖
npm install @cloudflare/computer
# 或使用 pnpm
pnpm add @cloudflare/computer
// computer-client.ts
import { Computer } from '@cloudflare/computer';
// 初始化客户端
const computer = new Computer({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!,
// 可选:配置后端偏好
backendPreference: {
// 优先使用 Isolate,仅在必要时切换到 Container
default: 'isolate',
fallback: 'container'
}
});
console.log('Computer client initialized');
3.2 创建 Agent Workspace
// create-workspace.ts
import { Computer } from '@cloudflare/computer';
const computer = new Computer({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!
});
async function createAgentWorkspace() {
// 创建 workspace
const workspace = await computer.createWorkspace({
id: 'my-first-agent',
// 从模板初始化
template: 'node-typescript',
// 或从 Git 仓库克隆
// fromGit: 'https://github.com/vercel/next.js.git',
// 权限配置
permissions: {
allowRead: true,
allowWrite: true,
allowExec: true,
allowedCommands: ['npm', 'node', 'npx', 'tsc', 'git'],
allowNetwork: true,
allowedHosts: ['*'], // 允许所有域名
maxFileSize: 10 * 1024 * 1024, // 10MB
maxTotalSize: 100 * 1024 * 1024, // 100MB
maxExecTime: 300 * 1000 // 5分钟
}
});
console.log(`Workspace created: ${workspace.id}`);
return workspace;
}
createAgentWorkspace();
3.3 Agent 执行文件操作
// agent-operations.ts
import { Computer } from '@cloudflare/computer';
const computer = new Computer({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!
});
async function agentFileOperations(workspaceId: string) {
const workspace = await computer.getWorkspace(workspaceId);
// 1. 读取 package.json
console.log('Reading package.json...');
const packageJson = await workspace.read('package.json');
const pkg = JSON.parse(packageJson.toString());
console.log(`Project name: ${pkg.name}`);
// 2. 创建新文件
console.log('Creating new file...');
await workspace.write('src/utils/math.ts', `
export const add = (a: number, b: number): number => {
return a + b;
};
export const multiply = (a: number, b: number): number => {
return a * b;
};
export const factorial = (n: number): number => {
if (n <= 1) return 1;
return n * factorial(n - 1);
};
`);
// 3. 编辑已有文件
console.log('Editing index.ts...');
await workspace.edit('src/index.ts', [
{
type: 'insert',
line: 1,
content: `import { add, multiply } from './utils/math';\n`
},
{
type: 'replace',
line: 3,
content: `console.log('2 + 3 =', add(2, 3));`
}
]);
// 4. 列出目录结构
console.log('Listing files...');
const files = await workspace.ls('src');
for (const file of files) {
console.log(` ${file.isDirectory ? '📁' : '📄'} ${file.path}`);
}
// 5. 删除文件
console.log('Deleting temp file...');
await workspace.delete('temp.txt');
// 6. 查看审计日志
console.log('Audit log:');
const logs = await workspace.getAuditLog({
limit: 10,
operation: 'write'
});
for (const log of logs) {
console.log(` [${new Date(log.timestamp).toISOString()}] ${log.operation} ${log.resource}`);
}
}
agentFileOperations('my-first-agent');
3.4 Agent 执行命令
// agent-exec-commands.ts
import { Computer } from '@cloudflare/computer';
const computer = new Computer({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!
});
async function agentExecCommands(workspaceId: string) {
const workspace = await computer.getWorkspace(workspaceId);
// 1. 安装依赖
console.log('Installing dependencies...');
const installResult = await workspace.exec('npm install');
console.log(`Exit code: ${installResult.exitCode}`);
console.log(`Output:\n${installResult.stdout}`);
if (installResult.exitCode !== 0) {
console.error(`Error:\n${installResult.stderr}`);
return;
}
// 2. 运行 TypeScript 编译
console.log('Compiling TypeScript...');
const buildResult = await workspace.exec('npx tsc');
console.log(`Build ${buildResult.exitCode === 0 ? 'succeeded' : 'failed'}`);
// 3. 运行测试
console.log('Running tests...');
const testResult = await workspace.exec('npm test');
console.log(`Tests ${testResult.exitCode === 0 ? 'passed' : 'failed'}`);
// 4. Git 操作
console.log('Git operations...');
const gitStatus = await workspace.exec('git status');
console.log(`Git status:\n${gitStatus.stdout}`);
// 5. 环境变量注入
console.log('Running with environment variables...');
const envResult = await workspace.exec('node -e "console.log(process.env.API_KEY)"', {
env: {
API_KEY: 'secret-key-12345'
}
});
console.log(`Result: ${envResult.stdout.trim()}`);
// 6. 超时控制
console.log('Running with timeout...');
try {
const timeoutResult = await workspace.exec('sleep 10', {
timeout: 5000 // 5秒超时
});
} catch (error) {
console.log('Command timed out as expected');
}
}
agentExecCommands('my-first-agent');
3.5 与 Agent SDK 集成
// agent-sdk-integration.ts
import { Computer } from '@cloudflare/computer';
import { Anthropic } from '@anthropic-ai/sdk';
const computer = new Computer({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!
});
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY!
});
// 定义 Computer 工具
const computerTools = [
{
name: 'computer_read',
description: 'Read a file from the workspace',
input_schema: {
type: 'object',
properties: {
path: { type: 'string', description: 'File path to read' }
},
required: ['path']
}
},
{
name: 'computer_write',
description: 'Write content to a file in the workspace',
input_schema: {
type: 'object',
properties: {
path: { type: 'string', description: 'File path to write' },
content: { type: 'string', description: 'Content to write' }
},
required: ['path', 'content']
}
},
{
name: 'computer_exec',
description: 'Execute a command in the workspace',
input_schema: {
type: 'object',
properties: {
command: { type: 'string', description: 'Command to execute' }
},
required: ['command']
}
}
];
async function runAgent(workspaceId: string, task: string) {
const workspace = await computer.getWorkspace(workspaceId);
// 初始消息
const messages: any[] = [
{
role: 'user',
content: task
}
];
// Agent 循环
while (true) {
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 4096,
tools: computerTools,
messages: messages
});
// 检查是否完成
if (response.stop_reason === 'end_turn') {
console.log('Agent completed task');
break;
}
// 处理工具调用
const toolResults: any[] = [];
for (const block of response.content) {
if (block.type === 'tool_use') {
console.log(`Tool call: ${block.name}`);
let result: any;
switch (block.name) {
case 'computer_read':
const content = await workspace.read(block.input.path);
result = { content: content.toString() };
break;
case 'computer_write':
await workspace.write(block.input.path, block.input.content);
result = { success: true };
break;
case 'computer_exec':
const execResult = await workspace.exec(block.input.command);
result = {
exitCode: execResult.exitCode,
stdout: execResult.stdout,
stderr: execResult.stderr
};
break;
}
toolResults.push({
type: 'tool_result',
tool_use_id: block.id,
content: JSON.stringify(result)
});
}
}
// 添加助手响应和工具结果到消息列表
messages.push({ role: 'assistant', content: response.content });
messages.push({ role: 'user', content: toolResults });
}
}
// 运行示例
runAgent(
'my-first-agent',
'Please create a simple Node.js HTTP server that responds with "Hello from Agent!" on port 3000'
);
3.6 批量操作与并发控制
// batch-operations.ts
import { Computer } from '@cloudflare/computer';
const computer = new Computer({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!
});
async function batchOperations(workspaceId: string) {
const workspace = await computer.getWorkspace(workspaceId);
// 批量创建文件
const files = [
{ path: 'src/api/users.ts', content: 'export const getUsers = () => [];' },
{ path: 'src/api/posts.ts', content: 'export const getPosts = () => [];' },
{ path: 'src/api/comments.ts', content: 'export const getComments = () => [];' },
{ path: 'src/models/user.ts', content: 'export interface User { id: string; }' },
{ path: 'src/models/post.ts', content: 'export interface Post { id: string; }' }
];
console.log('Batch creating files...');
await workspace.batchWrite(files);
// 批量读取文件
console.log('Batch reading files...');
const contents = await workspace.batchRead(
files.map(f => f.path)
);
for (const [path, content] of Object.entries(contents)) {
console.log(` ${path}: ${content.length} bytes`);
}
// 并发执行命令(有并发限制)
console.log('Concurrent exec...');
const commands = [
'npm run lint',
'npm run typecheck',
'npm run test:unit'
];
const results = await workspace.concurrentExec(commands, {
maxConcurrency: 2, // 最多2个并发
timeout: 60000 // 每个命令60秒超时
});
for (const [cmd, result] of Object.entries(results)) {
console.log(` ${cmd}: exit=${result.exitCode}`);
}
}
四、性能优化:从理论到实践
4.1 VFS 性能优化
优化一:内容去重
// 写入时自动去重
async write(path: string, content: Buffer): Promise<void> {
const checksum = sha256(content);
// 检查是否已存在相同内容
const existing = this.db.prepare(`
SELECT id FROM file_contents WHERE checksum = ?
`).get(checksum);
if (existing) {
// 复用已有内容
this.db.prepare(`
INSERT INTO files (path, content_id, ...)
VALUES (?, ?, ...)
`).run(path, existing.id);
} else {
// 插入新内容
// ...
}
}
优化二:预读取缓存
class VFSWithCache extends VirtualFileSystem {
private cache = new LRUCache<string, Buffer>({ max: 100 * 1024 * 1024 }); // 100MB
async read(path: string): Promise<Buffer> {
// 检查缓存
const cached = this.cache.get(path);
if (cached) return cached;
// 从数据库读取
const content = await super.read(path);
// 写入缓存
this.cache.set(path, content);
return content;
}
}
优化三:批量写入优化
// 批量写入时使用单个事务
async batchWrite(files: Array<{ path: string; content: Buffer }>): Promise<void> {
this.db.transaction(() => {
const insertContent = this.db.prepare(`
INSERT INTO file_contents (id, data, checksum) VALUES (?, ?, ?)
`);
const insertFile = this.db.prepare(`
INSERT INTO files (path, content_id, ...) VALUES (?, ?, ...)
`);
for (const { path, content } of files) {
const contentId = uuid();
const checksum = sha256(content);
insertContent.run(contentId, content, checksum);
insertFile.run(path, contentId, ...);
}
})();
}
4.2 调度器优化
优化一:预热 Isolate 池
class IsolatePool {
private pool: Isolate[] = [];
private maxPoolSize = 10;
constructor() {
// 启动时预热
for (let i = 0; i < this.maxPoolSize; i++) {
this.pool.push(this.createIsolate());
}
}
acquire(): Isolate {
if (this.pool.length > 0) {
return this.pool.pop()!;
}
return this.createIsolate();
}
release(isolate: Isolate): void {
if (this.pool.length < this.maxPoolSize) {
this.pool.push(isolate);
} else {
isolate.dispose();
}
}
}
优化二:Container 热池
class ContainerPool {
private hotContainers: Map<string, Container> = new Map();
async acquire(workspaceId: string): Promise<Container> {
// 检查热池
const hot = this.hotContainers.get(workspaceId);
if (hot && hot.isHealthy()) {
return hot;
}
// 创建新容器
const container = await this.createContainer(workspaceId);
// 加入热池
this.hotContainers.set(workspaceId, container);
return container;
}
}
4.3 审计日志优化
优化一:异步写入
class AsyncAuditLogger {
private queue: AuditEntry[] = [];
private flushInterval = 1000; // 1秒刷新一次
log(entry: AuditEntry): void {
this.queue.push(entry);
if (this.queue.length > 100) {
this.flush();
}
}
private flush(): void {
const entries = this.queue;
this.queue = [];
// 异步写入数据库
setImmediate(() => {
this.db.transaction(() => {
const stmt = this.db.prepare(`
INSERT INTO audit_log (...) VALUES (...)
`);
for (const entry of entries) {
stmt.run(...);
}
})();
});
}
}
优化二:分区表
-- 按时间分区
CREATE TABLE audit_log_2026_08 (
-- 同 audit_log 表结构
) WITHOUT ROWID;
CREATE TABLE audit_log_2026_09 (
-- 同 audit_log 表结构
) WITHOUT ROWID;
-- 视图统一查询
CREATE VIEW audit_log AS
SELECT * FROM audit_log_2026_08
UNION ALL
SELECT * FROM audit_log_2026_09;
4.4 性能基准测试
// benchmark.ts
import { Computer } from '@cloudflare/computer';
async function runBenchmarks() {
const computer = new Computer({...});
const workspace = await computer.createWorkspace({
id: 'benchmark',
template: 'empty'
});
// 测试1:写入性能
console.log('=== Write Performance ===');
const writeStart = Date.now();
for (let i = 0; i < 1000; i++) {
await workspace.write(`file_${i}.txt`, `Content ${i}`);
}
const writeEnd = Date.now();
console.log(`Write 1000 files: ${writeEnd - writeStart}ms`);
console.log(`Throughput: ${1000 / (writeEnd - writeStart) * 1000} ops/sec`);
// 测试2:读取性能
console.log('=== Read Performance ===');
const readStart = Date.now();
for (let i = 0; i < 1000; i++) {
await workspace.read(`file_${i}.txt`);
}
const readEnd = Date.now();
console.log(`Read 1000 files: ${readEnd - readStart}ms`);
console.log(`Throughput: ${1000 / (readEnd - readStart) * 1000} ops/sec`);
// 测试3:Isolate vs Container
console.log('=== Isolate vs Container ===');
// Isolate 执行
const isolateStart = Date.now();
for (let i = 0; i < 100; i++) {
await workspace.exec('node -e "console.log(1+1)"', { backend: 'isolate' });
}
const isolateEnd = Date.now();
console.log(`Isolate 100 executions: ${isolateEnd - isolateStart}ms`);
// Container 执行
const containerStart = Date.now();
for (let i = 0; i < 100; i++) {
await workspace.exec('node -e "console.log(1+1)"', { backend: 'container' });
}
const containerEnd = Date.now();
console.log(`Container 100 executions: ${containerEnd - containerStart}ms`);
// 清理
await workspace.destroy();
}
runBenchmarks();
五、生产环境踩坑清单
5.1 权限配置错误
踩坑:命令白名单不够严格
// 错误示例:允许 'npm' 会连带允许 'npm run <arbitrary-script>'
permissions: {
allowedCommands: ['npm', 'node']
}
// 正确示例:使用完整命令
permissions: {
allowedCommands: ['npm install', 'npm run build', 'npm test']
}
// 更好的做法:正则匹配
permissions: {
commandPattern: /^(npm (install|run|test)|node)($|\s)/
}
踩坑:网络白名单过于宽松
// 错误示例:允许所有域名
permissions: {
allowedHosts: ['*']
}
// 正确示例:明确列出需要的域名
permissions: {
allowedHosts: [
'registry.npmjs.org',
'github.com',
'api.github.com'
]
}
5.2 资源耗尽
踩坑:无限循环导致执行时间超限
// 错误示例:没有设置超时
await workspace.exec('npm run dev'); // 可能永远不退出
// 正确示例:设置超时
await workspace.exec('npm run dev', {
timeout: 60000 // 1分钟超时
});
踩坑:大文件写入导致存储耗尽
// 错误示例:没有检查文件大小
await workspace.write('large-file.bin', hugeBuffer);
// 正确示例:先检查
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
if (hugeBuffer.length > MAX_FILE_SIZE) {
throw new Error('File too large');
}
await workspace.write('large-file.bin', hugeBuffer);
5.3 审计日志丢失
踩坑:异步写入在崩溃时丢失
// 错误示例:纯异步写入
this.auditQueue.push(entry);
// 正确示例:关键操作同步写入
await this.db.prepare(`
INSERT INTO audit_log (...) VALUES (...)
`).run(...);
5.4 并发问题
踩坑:同一 workspace 被多个 Agent 同时操作
// 错误示例:无锁访问
const workspace = await computer.getWorkspace('shared');
// 正确示例:使用锁
const workspace = await computer.getWorkspace('shared', {
lock: 'exclusive', // 独占锁
timeout: 30000 // 等待30秒
});
5.5 安全漏洞
踩坑:命令注入
// 错误示例:直接拼接用户输入
await workspace.exec(`git clone ${userInput}`);
// 正确示例:参数化
await workspace.exec('git', ['clone', userInput]);
踩坑:路径遍历
// 错误示例:没有校验路径
await workspace.read(userPath); // userPath 可能是 '../secret.txt'
// 正确示例:规范化路径
const normalized = path.normalize(userPath);
if (normalized.startsWith('..') || path.isAbsolute(normalized)) {
throw new Error('Invalid path');
}
await workspace.read(normalized);
六、与其他方案对比
6.1 vs Docker/Kubernetes
| 维度 | Cloudflare Computer | Docker/K8s |
|---|---|---|
| 启动速度 | 毫秒级(Isolate)/ 秒级(Container) | 秒级到分钟级 |
| 成本 | 按调用/时间计费 | 按节点时间计费 |
| 隔离性 | V8 Isolate / Linux Container | Linux Container |
| 持久化 | SQLite VFS(内置) | 需配置 PV/PVC |
| 审计 | 内置 | 需额外配置 |
| 适用场景 | Serverless Agent | 长运行服务 |
6.2 vs AWS Lambda/FaaS
| 维度 | Cloudflare Computer | AWS Lambda |
|---|---|---|
| 执行时间 | 分钟级(Container)/ 毫秒级(Isolate) | 秒级到分钟级 |
| 文件系统 | 持久化 SQLite VFS | 临时 /tmp |
| 状态保持 | 有状态 | 无状态 |
| 审计 | 内置 | 需 CloudTrail |
| 适用场景 | 有状态 Agent | 无状态函数 |
6.3 vs Modal/Vercel
| 维度 | Cloudflare Computer | Modal | Vercel |
|---|---|---|---|
| 执行环境 | Isolate + Container | Container | Serverless |
| 持久化 | SQLite VFS | Volume | KV/D1 |
| Agent 友好度 | 专为 Agent 设计 | 通用计算 | Web 优先 |
| 成本模型 | 按调用/时间 | 按时间 | 按调用 |
七、未来展望
7.1 技术演进方向
方向一:更智能的调度
基于机器学习的调度器,根据历史执行数据预测最优后端:
- 记录每个命令的执行时间、资源消耗
- 训练预测模型
- 自动调整调度策略
方向二:跨区域同步
支持多区域部署,workspace 可以在不同区域之间同步:
- 低延迟访问(就近区域)
- 数据主权(数据留在特定区域)
- 灾备能力
方向三:更丰富的执行后端
除了 Isolate 和 Container,支持更多后端:
- Firecracker microVM:轻量级虚拟机
- WebAssembly:接近原生性能的沙箱
- 专用硬件:GPU/NPU 加速
7.2 生态整合
与 Agent 框架深度整合:
// LangChain 集成
import { ComputerTool } from '@cloudflare/computer-langchain';
const agent = new Agent({
tools: [
new ComputerTool({ workspaceId: 'my-agent' })
]
});
// AutoGPT 集成
from computer_autogpt import ComputerWorkspace
workspace = ComputerWorkspace(id='my-agent')
agent = AutoGPT(workspace=workspace)
与 CI/CD 整合:
# GitHub Actions 集成
jobs:
agent-test:
runs-on: ubuntu-latest
steps:
- name: Run Agent Tests
uses: cloudflare/computer-action@v1
with:
workspace-id: test-workspace
command: npm test
八、总结
Cloudflare Computer 代表了 2026 年 AI Agent 执行环境的最佳实践:
- 从「容器」到「电脑」:让 Agent 拥有持久化、可观测的执行环境
- SQLite VFS 创新:嵌入式数据库作为文件系统,兼具性能与审计能力
- 智能调度:Isolate 与 Container 自动切换,平衡成本与能力
- 权限与审计:企业级的安全与合规支持
- Agent SDK 友好:与主流框架无缝集成
这不是一个简单的技术产品,而是一次架构范式的转变:从运维视角转向 Agent 视角,从容器抽象转向电脑抽象。
对于开发者而言,Cloudflare Computer 提供了一个开箱即用的 Agent 执行环境,无需关心底层调度、权限、审计等复杂问题。对于企业而言,它提供了完整的合规能力,满足审计要求。
一句话总结:Cloudflare Computer 让 AI Agent 拥有了属于自己的「电脑」,而不是每次都租一个临时容器。
附录:完整示例代码
A. 完整的 Agent 项目示例
// complete-agent-example.ts
import { Computer, Workspace } from '@cloudflare/computer';
// 初始化
const computer = new Computer({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!
});
// Agent 类
class CodeAgent {
private workspace: Workspace;
constructor(private workspaceId: string) {}
async initialize(): Promise<void> {
this.workspace = await computer.createWorkspace({
id: this.workspaceId,
template: 'node-typescript',
permissions: {
allowRead: true,
allowWrite: true,
allowExec: true,
allowedCommands: ['npm', 'node', 'npx', 'tsc', 'git'],
allowNetwork: true,
allowedHosts: ['registry.npmjs.org', 'github.com'],
maxExecTime: 300000
}
});
}
async setupProject(): Promise<void> {
// 初始化项目
await this.workspace.exec('npm init -y');
await this.workspace.exec('npm install typescript @types/node --save-dev');
// 创建 tsconfig
await this.workspace.write('tsconfig.json', JSON.stringify({
compilerOptions: {
target: 'ES2022',
module: 'commonjs',
strict: true,
outDir: './dist'
}
}, null, 2));
// 创建源文件
await this.workspace.write('src/index.ts', `
import { greet } from './utils';
console.log(greet('World'));
`);
await this.workspace.write('src/utils.ts', `
export const greet = (name: string): string => {
return \`Hello, \${name}!\`;
};
`);
}
async build(): Promise<boolean> {
const result = await this.workspace.exec('npx tsc');
return result.exitCode === 0;
}
async run(): Promise<string> {
const result = await this.workspace.exec('node dist/index.js');
return result.stdout.trim();
}
async getAuditReport(): Promise<string> {
const logs = await this.workspace.getAuditLog({ limit: 100 });
let report = `# Audit Report for ${this.workspaceId}\n\n`;
report += `Total operations: ${logs.length}\n\n`;
for (const log of logs) {
report += `- [${new Date(log.timestamp).toISOString()}] ${log.operation}: ${log.resource}\n`;
}
return report;
}
async cleanup(): Promise<void> {
await this.workspace.destroy();
}
}
// 使用示例
async function main() {
const agent = new CodeAgent('demo-agent');
try {
console.log('Initializing agent...');
await agent.initialize();
console.log('Setting up project...');
await agent.setupProject();
console.log('Building project...');
const success = await agent.build();
console.log(`Build ${success ? 'succeeded' : 'failed'}`);
if (success) {
console.log('Running project...');
const output = await agent.run();
console.log(`Output: ${output}`);
}
console.log('Generating audit report...');
const report = await agent.getAuditReport();
console.log(report);
} finally {
await agent.cleanup();
}
}
main().catch(console.error);
B. 审计日志查询工具
// audit-query-tool.ts
import { Computer } from '@cloudflare/computer';
async function queryAuditLog(
workspaceId: string,
options: {
operation?: string;
startTime?: Date;
endTime?: Date;
limit?: number;
}
) {
const computer = new Computer({...});
const workspace = await computer.getWorkspace(workspaceId);
const logs = await workspace.getAuditLog({
operation: options.operation,
startTime: options.startTime?.getTime(),
endTime: options.endTime?.getTime(),
limit: options.limit || 100
});
// 统计
const stats = {
total: logs.length,
byOperation: {} as Record<string, number>,
byResult: {} as Record<string, number>,
errors: [] as string[]
};
for (const log of logs) {
stats.byOperation[log.operation] = (stats.byOperation[log.operation] || 0) + 1;
stats.byResult[log.result] = (stats.byResult[log.result] || 0) + 1;
if (log.result === 'failure') {
stats.errors.push(`${log.operation} ${log.resource}: ${log.error}`);
}
}
return { logs, stats };
}
// 使用示例
const { logs, stats } = await queryAuditLog('my-agent', {
operation: 'exec',
startTime: new Date(Date.now() - 24 * 60 * 60 * 1000), // 最近24小时
endTime: new Date()
});
console.log('Stats:', stats);
字数统计:约 12,500 字
关键词:Cloudflare Computer, AI Agent, SQLite VFS, Isolate, Container, 智能调度, 权限系统, 审计日志
标签:Cloudflare|Agent|SQLite|Isolate|Container|VFS|权限|审计|执行环境|Serverless