Vercel Zero 深度解析:为 AI Agent 时代而生的系统编程语言
背景:2026年5月15日,Vercel Labs 开源了 Zero——一门明确标榜「为 AI Agent 打造」的系统编程语言。它不是 Python 脚本玩具,而是用 C 实现的、具备强类型、显式副作用与结构化编译输出的原生语言。本文从语言设计哲学、核心语法、能力系统、编译器架构多个维度深度拆解,试图回答一个根本问题:为什么 Agent 时代需要一门新语言?
一、引言:Agent 时代遇到了什么麻烦?
在 Zero 出现之前,AI Agent 写代码、用工具、调用 API、跑脚本,全靠 Python/TypeScript/Bash 粘合。表面上看这没问题,但深入 Agent 工作流后会发现几个根本性矛盾:
1.1 隐藏状态:Agent 无法可靠预测行为
Python 的 print()、Node.js 的 console.log、Bash 的 echo,全部依赖隐藏的全局进程状态。Agent 在执行 subprocess.run(["ls"]) 时,根本不知道这次调用会不会因为环境变量、当前工作目录或系统 locale 设置而产生意外结果。
换句话说:现有语言把副作用当默认值,Agent 无法静态分析工具调用的全部影响。
1.2 错误处理缺失:失败的调用被静默吞掉
Python 代码大量存在这样的模式:
result = subprocess.run(["ls", "-la"])
# 如果失败,result.returncode != 0,但代码通常不检查
process_output(result)
在 Agent 场景下,这会导致 Agent 把错误输出当作正常结果处理,或者在一条命令已经失败的情况下继续执行依赖其结果的下一条指令。
1.3 工具签名模糊:Agent 无法静态验证参数
大多数 CLI 工具的 --help 输出本质上是一段自然语言文本。Agent 要么靠 LLM「猜」参数语义,要么靠昂贵的运行时试探。而现实是:Agent 经常因为猜错 flag 类型导致整条流水线失败。
1.4 可移植性噩梦:脚本在本地跑,到 CI 就挂
Shell 脚本依赖 Bash 版本、GNU vs BSD 工具差异、系统路径。同一段脚本在开发者的 macOS 和生产服务器 CentOS 上可能表现完全不同。Agent 在不同环境下生成可复现的工具链,本身就是极其困难的工程问题。
Zero 的出现,瞄准的就是以上四个问题。它不是又一个「更好的 JavaScript」,而是一套从编译器层就考虑了 Agent 可理解性 的语言系统。
二、设计哲学:显式优于隐式,能力优于权限
Zero 的设计哲学浓缩成一句话:「效果(Effect)应该是可见的,而不是背景噪音」。
这句话出自 Zero 官方文档对语言设计目标的描述。理解这句话,是理解 Zero 全部语法特性的钥匙。
2.1 一切效果皆显式声明
在传统语言中,输出到 stdout 是隐式的:
print("hello") # 隐式 I/O
在 Zero 中,同等操作的语义是显式的:
pub fun main(world: World) -> Void raises {
check world.out.write("hello from zero\n")
}
world.out.write() 不是调用一个全局函数,而是通过一个 能力对象(Capability Object) world 来执行 I/O。这个对象由运行时传给 main,而不是从任何隐藏上下文读取。
2.2 能力系统(Capability System)
Zero 的核心抽象是 World——一个包含程序所有对外交互能力的能力容器。World 在程序启动时由运行时创建并注入,程序只能通过 World 访问外部世界:
| 能力字段 | 作用 |
|---|---|
world.out | 标准输出写入 |
world.err | 标准错误写入 |
world.fs | 文件系统操作 |
world.env | 环境变量读取 |
world.args | 命令行参数 |
程序在编译时可以看到它使用了哪些能力。Agent 可以通过 zero routes --json 或 zero skills get 这样的元命令,静态列出程序对外部世界的全部依赖——这对于代码审核和安全审计来说是革命性的。
2.3 raises 语义:错误是类型的一部分
pub fun main(world: World) -> Void raises {
check world.out.write("hello\n")
}
raises 关键字声明该函数可以「抛出」错误。这不是异常机制(Zero 没有 try/catch),而是一种代数错误类型的简写。
任何调用可能失败的操作,必须用 check 包裹。如果不处理错误,编译器会拒绝编译。
// 错误示例:编译器会报错
let result = world.fs.read("config.json")
process(result) // ❌ 没有 check,编译器报错
// 正确示例
let result = check world.fs.read("config.json")
process(result) // ✅
check 等价于「如果这里出错,立即从当前函数返回错误」。这与 Rust 的 ? 操作符和 Go 的多返回值错误处理有异曲同工之妙,但语法更简洁,且与 raises 声明形成完整的编译时错误追踪链。
2.4 可预测内存:无 GC,无隐藏分配
Zero 定位为「系统语言」,默认没有垃圾回收器。所有内存分配是显式的:
// 显式分配固定大小缓冲区
let buf = Buffer.alloc(1024)
check world.fs.read_to_buffer("data.bin", buf, 0, 1024)
对于 Agent 来说,这意味着:你可以静态分析一段 Zero 代码的内存使用量,而无需担心某个未知库在背后偷偷分配了几个 GB。
三、核心语法:快速入门
3.1 类型系统
Zero 采用强静态类型,所有类型在编译时确定:
// 基本整数类型
let count: i32 = 42
let big: i64 = 9_000_000_000 // 支持下划线分隔符
let byte: u8 = 255
let pointer: usize = 0x7fff_0000
// 浮点
let pi: f64 = 3.141592653589793
let small: f32 = 0.5
// 布尔
let flag: Bool = true
// 字符串(不可变)
let name: String = "Zero Agent"
let greeting = "Hello, " ++ name // 字符串连接
// 数组(固定长度)
let numbers: [i32; 5] = [1, 2, 3, 4, 5]
let first = numbers[0] // 编译时边界检查
3.2 函数
// 私有函数
fun add(a: i32, b: i32) -> i32 {
return a + b
}
// 公开(导出)函数
pub fun multiply(a: i32, b: i32) -> i32 {
return a * b
}
// 带错误处理的函数
pub fun read_config(path: String, world: World) -> Config raises {
let content = check world.fs.read_text(path)
return check parse_config(content)
}
// 函数重载:不支持同名函数,但支持泛型
fun identity[T](value: T) -> T {
return value
}
3.3 控制流
// if / else
let label = if count > 10 { "big" } else { "small" }
// match(模式匹配)
let description = match status {
Ok(val) => "success: " ++ val,
Err(msg) => "error: " ++ msg,
}
// 循环:仅 for-in(无 while 的无限循环场景通过递归实现)
for i in 0..10 {
check world.out.write(i as String ++ "\n")
}
3.4 结构体与包
// 定义结构体
struct Point {
x: f64,
y: f64,
}
impl Point {
pub fun distance(other: Point) -> f64 {
let dx = self.x - other.x
let dy = self.y - other.y
return (dx * dx + dy * dy).sqrt()
}
}
// 包引用
import fs from "std:fs"
import net from "std:net"
四、编译器架构:原生实现与多目标输出
4.1 双编译器策略
Zero 采用了极为罕见的双编译器架构:
Zero 源代码(.0 文件)
│
├── native/zero-c/ ──→ C 代码 ──→ 系统原生汇编 ──→ 可执行文件
│ (生产路径,性能优先)
│
└── compiler-zero/ ──→ Zero 自举编译器
(用于语言自身开发)
native/zero-c 是生产级 C 编译器,将 Zero 源码编译为高效的本地可执行文件。这是为 Agent 生成的生产级二进制工具提供的基础设施。
compiler-zero 是用 Zero 自己写的编译器前端。这是一个自举(bootstrapped)的过程,意味着 Zero 开发者用 Zero 编译 Zero 本身。
4.2 结构化输出:JSON 编译产物
Zero 编译器的输出不是黑盒二进制,而是一系列结构化的 JSON 文件:
# 代码检查(类型检查 + 语法验证)
zero check hello.0
# 生成中间表示 JSON
zero build --emit ir --target linux-musl-x64 hello.0 --out ./build/
# 生成依赖图 JSON(供 Agent 分析)
zero graph --json hello.0
# 生成大小分析报告
zero size --json hello.0
zero size --json 的输出示例:
{
"file": "examples/point.0",
"functions": [
{ "name": "main", "size_bytes": 256, "stack_frames": 2 }
],
"total_bytes": 2048
}
这是 Agent 时代最有价值的特性之一:编译器输出的元数据是机器可读的,Agent 可以自动分析、优化和审计 Zero 代码,而不需要发明额外的工具链。
4.3 多目标编译
# 编译为 Linux x64-musl(静态链接,适合容器化部署)
zero build --emit exe --target linux-musl-x64 main.0 --out ./dist/
# 编译为 macOS ARM64
zero build --emit exe --target darwin-arm64 main.0 --out ./dist/
# 输出 WebAssembly(供浏览器环境运行)
zero build --emit wasm --target wasm32-wasi main.0 --out ./dist/
这意味着:同一份 Zero 代码,可以同时生成原生可执行文件(用于服务器端工具)和 WebAssembly(用于浏览器端或边缘计算场景)。Agent 生成的工具可以灵活部署到任何环境。
五、Agent 工具链集成:Zero 的杀手级场景
5.1 场景一:Agent 生成的安全 CLI 工具
传统 Agent 生成 Bash 脚本时,总面临安全审查难题:
# Bash 脚本:Agent 无法在运行前知道它会执行哪些操作
#!/bin/bash
rm -rf /tmp/build # 可能删错目录
curl https://api.example.com/data > output.json # 发送哪些数据?
用 Zero 重写:
pub fun main(world: World) -> Void raises {
// 编译器静态分析:此程序只使用了 world.out 和 world.env
// Agent 可以通过 zero graph --json 确认没有 fs 删除操作
let api_key = check world.env.get("API_KEY")
let data = check fetch_from_api(api_key, world)
check world.out.write(data)
}
Agent 在发布工具前,可以用 zero routes --json 确认工具的外部依赖链——这是静态安全审计的基础。
5.2 场景二:多 Agent 协作的协议层
当多个 Agent 需要通过结构化协议通信时,Zero 提供了精确的消息格式:
struct AgentMessage {
sender_id: String,
intent: Intent,
payload: Payload,
timestamp: u64,
}
enum Intent {
Query,
Execute,
Respond,
Error,
}
pub fun main(world: World) -> Void raises {
let raw = check world.fs.read_text("/tmp/message.json")
let msg = check AgentMessage.parse(raw)
let response = match msg.intent {
Query => handle_query(msg),
Execute => handle_execute(msg),
Respond => handle_respond(msg),
Error => handle_error(msg),
}
check world.out.write(response.serialize())
}
结构化的消息类型消除了 JSON Schema 不一致、字段类型模糊的问题。不同 Agent 之间可以基于 Zero 类型系统进行协议对齐。
5.3 场景三:持续运行的 Agent 服务
Agent 需要在后台持续运行的场景(Web 服务器、长任务调度、监控脚本),Zero 提供了网络能力:
import net from "std:net"
import json from "std:json"
struct TaskRequest {
task_id: String,
command: String,
timeout_ms: u32,
}
pub fun main(world: World) -> Void raises {
let server = check net.listen("0.0.0.0:8080")
loop {
let conn = check server.accept()
let req_bytes = check conn.read_all()
let req = check TaskRequest.parse(req_bytes)
let result = check execute_task(req, world)
let resp = json.stringify(result)
check conn.write(resp)
check conn.close()
}
}
Zero 的 std:net 包提供了基础的 TCP 服务器能力,Agent 可以用它构建自定义的 API 网关或任务调度器。
六、与现有语言的对比
6.1 Rust vs Zero
Rust 是目前最受推崇的系统语言,但它的学习曲线极为陡峭。Zero 在设计上有意借鉴了 Rust 的理念(如 ? 错误传播、强类型系统),但在语法上大幅简化:
| 维度 | Rust | Zero |
|---|---|---|
| 内存管理 | 所有权 + 生命周期 | 手动显式分配(简化版) |
| 错误处理 | Result<T, E> + ? | raises + check |
| 泛型 | impl<T> | fun identity[T](value: T) |
| 编译输出 | ELF 二进制 | 可执行文件 + JSON 元数据 |
| 自举 | 需 Rust 编译器引导 | 需 C 编译器引导 |
| 目标场景 | 通用系统编程 | Agent 工具链专用 |
Rust 的目标是「让不可能的代码在编译时被杀死」,Zero 的目标是「让 Agent 能读懂每一条编译输出」。
6.2 Go vs Zero
Go 以简单著称,但它的隐式 I/O 和运行时 GC 使其不适合作为 Agent 生成工具的语言基础。
// Go:fmt.Println 依赖全局包状态
fmt.Println("hello") // 隐式,Agent 无法追踪
// Zero:world.out 是显式注入的能力
check world.out.write("hello\n") // 显式,Agent 可静态分析
6.3 TypeScript/JavaScript vs Zero
TS/JS 是 Agent 时代最流行的脚本语言,但它们面向的是 Node.js 运行时,而不是原生可执行文件。Agent 生成的工具如果依赖 Node.js 运行时,就引入了巨大的环境依赖。
Zero 生成的是无运行时依赖的原生二进制,部署到任何目标机器上直接运行:
# Zero 工具:单文件可执行,无依赖
./dist/my-agent-tool # 直接运行,不需要 Node.js / Python
# TypeScript 工具:需要运行时
node dist/my-agent-tool # 需要 npm install + node_modules
七、实战:从零构建一个 Agent 文件处理工具
下面我们用 Zero 实现一个完整的 Agent 工具:批量文件加密工具,展示 Zero 的完整开发流程。
7.1 安装编译器
curl -fsSL https://zerolang.ai/install.sh | bash
export PATH="$HOME/.zero/bin:$PATH"
zero --version
7.2 创建项目结构
zero new encrypt-tool
cd encrypt-tool
生成了以下文件结构:
encrypt-tool/
├── encrypt-tool.0 # 主程序
├── src/
│ ├── crypto.0 # 加密模块
│ └── scan.0 # 目录扫描
├── build.sh # 编译脚本
└── README.md
7.3 实现加密模块 src/crypto.0
import fs from "std:fs"
struct CryptoConfig {
algorithm: String, // "aes-256-gcm" | "chacha20-poly1305"
key_file: String,
output_suffix: String,
}
struct EncryptionResult {
input_path: String,
output_path: String,
bytes_encrypted: usize,
success: Bool,
error_message: String,
}
// 从密钥文件加载密钥(简化示例,实际应使用安全的密钥派生)
fun load_key(path: String, world: World) -> [u8; 32] raises {
let data = check fs.read_bytes(path)
// 取前 32 字节作为密钥
var key: [u8; 32] = [0; 32]
for i in 0..32 {
key[i] = data[i]
}
return key
}
// XOR 加密(演示用,生产环境请使用真实加密库)
fun xor_encrypt(data: []u8, key: []u8) -> []u8 {
let result_len = data.len()
var result: [u8; 1024] = [0; 1024]
for i in 0..result_len {
result[i] = data[i] ^ key[i % key.len()]
}
return result[0..result_len]
}
// 加密单个文件
pub fun encrypt_file(
input_path: String,
config: CryptoConfig,
world: World,
) -> EncryptionResult raises {
let data = check fs.read_bytes(input_path)
let key = check load_key(config.key_file, world)
let encrypted = xor_encrypt(data, key)
let output_path = input_path ++ config.output_suffix
check fs.write_bytes(output_path, encrypted)
return EncryptionResult {
input_path: input_path,
output_path: output_path,
bytes_encrypted: data.len(),
success: true,
error_message: "",
}
}
7.4 实现目录扫描 src/scan.0
import fs from "std:fs"
struct FileEntry {
path: String,
size_bytes: usize,
extension: String,
}
// 递归扫描目录,返回所有文件的绝对路径
pub fun scan_directory(dir: String, world: World) -> [String] raises {
var files: [String] = []
scan_recursive(dir, &files, world)
return files
}
fun scan_recursive(dir: String, out: &mut [String], world: World) raises {
let entries = check fs.read_dir(dir)
for entry in entries {
let full_path = dir ++ "/" ++ entry.name
if entry.is_dir {
scan_recursive(full_path, out, world)
} else {
out.push(full_path)
}
}
}
7.5 主程序 encrypt-tool.0
import fs from "std:fs"
import crypto from "src/crypto.0"
import scan from "src/scan.0"
struct AppConfig {
input_dir: String,
key_file: String,
output_suffix: String,
dry_run: Bool,
verbose: Bool,
}
// 解析命令行参数
fun parse_args(args: [String]) -> AppConfig {
var input_dir = ""
var key_file = "key.bin"
var output_suffix = ".enc"
var dry_run = false
var verbose = false
var i = 1
while i < args.len() {
let arg = args[i]
if arg == "--input" {
i = i + 1
input_dir = args[i]
} else if arg == "--key" {
i = i + 1
key_file = args[i]
} else if arg == "--suffix" {
i = i + 1
output_suffix = args[i]
} else if arg == "--dry-run" {
dry_run = true
} else if arg == "--verbose" {
verbose = true
}
i = i + 1
}
return AppConfig {
input_dir: input_dir,
key_file: key_file,
output_suffix: output_suffix,
dry_run: dry_run,
verbose: verbose,
}
}
pub fun main(world: World) -> Void raises {
let args = check world.args.get_all()
let config = parse_args(args)
if config.input_dir == "" {
check world.out.write("Usage: encrypt-tool --input <dir> [--key key.bin] [--suffix .enc] [--dry-run] [--verbose]\n")
return
}
check world.out.write("Scanning directory: " ++ config.input_dir ++ "\n")
let files = check scan.scan_directory(config.input_dir, world)
check world.out.write("Found " ++ files.len().as_string() ++ " files\n")
if config.dry_run {
check world.out.write("[DRY RUN] Would encrypt:\n")
for path in files {
check world.out.write(" " ++ path ++ "\n")
}
return
}
var success_count = 0
var fail_count = 0
for path in files {
let crypto_config = crypto.CryptoConfig {
algorithm: "xor-demo",
key_file: config.key_file,
output_suffix: config.output_suffix,
}
let result = check crypto.encrypt_file(path, crypto_config, world)
if result.success {
success_count = success_count + 1
if config.verbose {
check world.out.write("OK: " ++ result.output_path ++
" (" ++ result.bytes_encrypted.as_string() ++ " bytes)\n")
}
} else {
fail_count = fail_count + 1
check world.out.write("FAIL: " ++ result.input_path ++
" - " ++ result.error_message ++ "\n")
}
}
check world.out.write("\nDone: " ++ success_count.as_string() ++ " succeeded, " ++
fail_count.as_string() ++ " failed\n")
}
7.6 编译与运行
# 类型检查(不生成可执行文件)
zero check encrypt-tool.0
# 生成可执行文件
zero build --emit exe --target linux-musl-x64 encrypt-tool.0 --out ./dist/
# 或者在 macOS 上编译
zero build --emit exe --target darwin-arm64 encrypt-tool.0 --out ./dist/encrypt-tool
# 生成依赖图(供 Agent 分析)
zero graph --json encrypt-tool.0 > encrypt-tool-graph.json
# 运行
chmod +x ./dist/encrypt-tool
./dist/encrypt-tool --input ./data --key ./key.bin --verbose
输出示例:
Scanning directory: ./data
Found 42 files
OK: ./data/report.csv.enc (2048 bytes)
OK: ./data/secrets.json.enc (512 bytes)
FAIL: ./data/corrupt.bin - read error: unexpected EOF
Done: 40 succeeded, 2 failed
八、性能特性与基准测试
Zero 的官方仓库包含一套性能基准测试,通过 npm run bench 运行。测试覆盖以下维度:
8.1 编译速度
Zero 的 C 编译器前端在标准硬件上每秒可处理约 50,000 行代码(C 编译速度取决于系统 GCC/Clang)。作为对比,Go 编译速度约为 20,000 行/秒,Rust 约为 8,000 行/秒(增量编译)。
8.2 二进制大小
使用 zero size --json 可以精确测量每个函数的二进制大小:
{
"file": "examples/web/hello.0",
"functions": [
{"name": "main", "size_bytes": 384, "stack_frames": 1},
{"name": "handle_request", "size_bytes": 1024, "stack_frames": 4},
{"name": "parse_headers", "size_bytes": 512, "stack_frames": 2}
],
"total_bytes": 2048,
"target": "linux-musl-x64",
"stripped": true
}
8.3 运行时内存
由于 Zero 采用显式内存管理,内存使用可以通过静态分析精确预测,不存在 GC 暂停或内存抖动。对于长期运行的 Agent 服务,这一点至关重要。
九、限制与挑战
Zero 仍然处于实验阶段(官方文档明确声明「语言尚不稳定」),以下限制需要关注:
- 标准库不完整:
std:net只有基础的 TCP 监听/连接,HTTP/WebSocket 等高层协议尚未实现 - 错误处理链不完善:目前
check仅支持函数级别的错误返回,没有结构化的错误类型枚举(类似 Rust 的enum Error) - 泛型系统有限:泛型仅支持函数,不支持结构体级别的泛型
- 工具链生态空白:没有调试器、没有 profiler、没有 IDE 插件的完整支持
- 社区生态为零:目前只有官方 examples 和 conformance 测试,没有任何第三方包
十、总结:Agent 原生语言的意义
Zero 的出现不是一场语言革命,而是一种范式确认——它确认了 AI Agent 时代需要一套与人类程序员不同的编程工具:Agent 需要可静态分析的工具链、需要结构化的编译输出、需要显式而非隐式的能力声明。
Zero 的核心价值主张:
- 可预测性:Agent 生成的工具在部署前可以通过编译器的 JSON 输出进行完整审计
- 可移植性:同一份代码编译到任何目标平台,无运行时依赖
- 显式安全:程序对外部世界的所有访问都被编译器显式追踪
- 结构化元数据:编译输出不是黑盒,而是 Agent 可读的 JSON 文档
Zero 现在的状态,像极了 2015 年的 Rust(语言设计成熟,但生态尚在起步)。对于有技术追求的开发者,现在参与 Zero 的生态建设,可能是在 Agent 编程语言这个赛道最早的布局机会。
GitHub:vercel-labs/zero(570+ ⭐,持续增长中)
Tags: Vercel, Zero, 编程语言, Agent, AI编程, 系统语言, C语言, 编译器, 元编程
Keywords: Zero language, Vercel Labs, agent programming language, explicit effects, capability system, systems programming, compiler architecture, native compilation, zero programming language tutorial