【Cayu】核心架构深度解析:用 DMTCP 快照与「零观测不等于成功」驯服长跑 Agent

你的 Agent 半夜跑了两小时,你早上看到的是一句「已完成」

零观测不等于成功。 一个没上报执行结果的 runner,和一个全部命令都正常退出的 runner,在报表上长得一模一样。
Cayu 把这个歧义直接做成了类型系统的一部分 —— evidence_state 只有 observed / incomplete / not_reported 三种,没有 success。

2026 年是 Agent Harness 的”长跑元年”。Claude Code 打开了会话式编程,Cursor 打开了补全式编程,而接下来真正难啃的骨头是:让 Agent 在没有人盯着的情况下跑几个小时、跨过服务重启、跨过容器重建、跨过一次人工审批,并且最后能证明它真的干了活。

绝大多数框架在这一步集体失手。它们的默认做法是:Agent 干完活 → 写一个 done 标记 → 框架信它。这等于让一个学生自己给自己批卷子然后交上去。

这一周我扒了 cayu-dev/cayu(946⭐,Apache-2.0,2026-07 创建,3689 个 Python 文件,110+ 篇 docs)的源码。它是我近期读过的最不像”AI 套壳”的 Agent Runtime —— 它的每一个设计决策都在回答同一个问题:当你不能相信 Agent 自己的报告时,证据从哪来?


一、Cayu 是什么:一个把自己拆成四层的 Runtime

Cayu 的 README 第一段就把术语分得极细,这在中国开源项目里不常见:

术语职责
FrameworkCayu 的 Python API、组件和默认值
Harness你用这些组件拼出来的领域系统(context / tools / memory / policy / 领域逻辑 / 验证)
Runtime执行机械:会话、状态、工具权限、worker、预算、恢复
Agent最终跑起来的那款应用

这四层切分的意义在于:Cayu 不卖 Harness,卖的是 Runtime。 大多数框架把 Harness 和 Runtime 糅在一起(LangChain 的 AgentExecutor、CrewAI 的 Crew 都是如此),结果你换个领域逻辑就得 fork 整个框架。Cayu 把这层剥掉之后,cayu.runtime 就变成了一套可以独立复用的执行底座。

graph TB
    subgraph L1["🚀 应用层 · Harness(你写的领域逻辑)"]
        A1["🎯 Agent 定义<br/>AgentSpec"]
        A2["🔧 ToolPolicy<br/>ALLOW / DENY / REQUIRE_APPROVAL"]
        A3["📚 Memory + Knowledge"]
    end

    subgraph L2["⚙️ Runtime 层(Cayu 提供,不可绕过)"]
        R1["🔄 SessionEngine<br/>Agent Loop 所有者"]
        R2["💰 Budgets<br/>每次模型调用前检查"]
        R3["✋ Approvals<br/>副作用前人工闸门"]
        R4["🗄️ Durable Events<br/>append-only 事件流"]
        R5["📸 Execution Snapshots<br/>DMTCP 进程快照"]
        R6["♻️ Recovery Coordinator<br/>不完整会话恢复"]
    end

    subgraph L3["🔌 适配层(可插拔)"]
        P1["🤖 Providers<br/>OpenAI / Anthropic / ..."]
        P2["📦 Storage<br/>SQLite / Postgres"]
        P3["🔐 Runners<br/>Docker / Lambda MicroVM"]
        P4["🔌 MCP Adapters<br/>外部工具走同一套 policy"]
    end

    A1 --> R1
    A2 --> R3
    A3 --> R1
    R1 --> R2 --> R4
    R1 --> R3 --> R4
    R4 --> R5
    R4 --> R6
    R1 --> P1
    R4 --> P2
    R3 --> P3
    A2 --> P4 --> R3

    style A1 fill:#C7CEEA,stroke:#9FA8DA,stroke-width:2px,color:#333
    style A2 fill:#C7CEEA,stroke:#9FA8DA,stroke-width:2px,color:#333
    style A3 fill:#C7CEEA,stroke:#9FA8DA,stroke-width:2px,color:#333
    style R1 fill:#E8D5F5,stroke:#CE93D8,stroke-width:3px,color:#333
    style R2 fill:#FFDAB9,stroke:#FFB74D,stroke-width:2px,color:#333
    style R3 fill:#FFDAB9,stroke:#FFB74D,stroke-width:2px,color:#333
    style R4 fill:#FFDAB9,stroke:#FFB74D,stroke-width:2px,color:#333
    style R5 fill:#FFB3C6,stroke:#F06292,stroke-width:2px,color:#333
    style R6 fill:#FFB3C6,stroke:#F06292,stroke-width:2px,color:#333
    style P1 fill:#F5F5F5,stroke:#BDBDBD,stroke-width:1px,color:#333
    style P2 fill:#F5F5F5,stroke:#BDBDBD,stroke-width:1px,color:#333
    style P3 fill:#F5F5F5,stroke:#BDBDBD,stroke-width:1px,color:#333
    style P4 fill:#F5F5F5,stroke:#BDBDBD,stroke-width:1px,color:#333

看依赖方向(来自 docs/architecture.md 的 Dependency Direction 段落):

1
2
3
4
5
6
7
8
9
core                                    <- 只定义 events / messages / agents / tools / 抽象契约
providers / artifacts / runners / workspaces / storage / vaults -> core
proxies -> vaults + core
egress -> proxies + vaults + core
mcp -> core
environments -> artifacts + workspaces + runners + vaults + proxies + mcp
runtime -> core + providers + artifacts + runners + workspaces + storage + vaults + proxies + mcp
workflows -> core + runtime
cli -> runtime + project scaffolding

core 保持小而稳定,runtime 依赖 core 但 core 不依赖 runtime。这条线画得非常干净 —— 大多数框架栽在”core 里混进了业务概念”,一旦混进去就再也拆不开。

MCP 在这里的位置也值得单独说一句:Cayu 明确规定 MCP tool 走 adapter 接入,成为普通的 Cayu tool,而不是开一条特权通道。这意味着一个外部 MCP server 调 rm -rf,和内置 tool 调 rm -rf,走的是同一套 approval / event / transcript 模型。这条设计我在别处很少见到。


二、原语一:run vs session —— 双 scope 硬上限

长跑 Agent 最常见的死法是烧钱烧到 API 拒服才发现。Cayu 的做法是在每次模型调用前跑一遍 RunLimits 检查(文档原话:checked before each model call)。

但真正有意思的不是”检查”,而是 scope 的双轨设计。

2.1 原始定义

src/cayu/budgets/run_limits.py 只有 2959 字符,但每个字段都有明确的语义约定:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# cayu-dev/cayu · src/cayu/budgets/run_limits.py(节选,省略 Pydantic Field 的省略参数)
class RunLimits(BaseModel):
"""Optional hard limits for one session run or resume call.

- scope="run"(默认):token / tool-call / elapsed-time 全部量
"本次 runtime 调用" —— 自原 run 进入以来的用量增量 + 自 run 开始的
active time。durable 审批和用户输入续跑保留基线,并**排除人工等待间隔**。
全新 run 或干净边界 resume 会重置它。
- scope="session":三个指标全部量"整个 durable session" —— 来自 session
事件流的累计用量 + 自 session 创建以来的墙钟时间。
一个**已经触限的 session 被 resume 会立刻停止**。
"""

max_input_tokens: StrictInt | None = Field(default=None, ge=1, le=MAX_DURABLE_JSON_INTEGER)
max_output_tokens: StrictInt | None = Field(default=None, ge=1, le=MAX_DURABLE_JSON_INTEGER)
max_total_tokens: StrictInt | None = Field(default=None, ge=1, le=MAX_DURABLE_JSON_INTEGER)
max_tool_calls: StrictInt | None = Field(default=None, ge=1, le=MAX_DURABLE_JSON_INTEGER)
max_elapsed_seconds: StrictInt | None = Field(default=None, ge=1, le=MAX_DURABLE_JSON_INTEGER)
scope: Literal["session", "run"] = "run"
on_limit: Literal["interrupt", "finish"] = Field(default="interrupt", ...)
finish_reserve_seconds: StrictInt = Field(default=30, ge=1, ...)

这里有三个容易被忽略、但决定生产可用性的细节:

细节为什么重要
排除人工等待间隔一个”等人点确认花了 3 小时”的审批不该烧掉你 30 分钟的 max_elapsed_seconds。如果把人工等待计入 active time,你的每一次人工闸门都会误触发超时熔断
scope="session" 时超限的 resume 立即停否则一次无限循环的 resume 可以把 session 累计烧到天荒地老 —— 每次 resume 都从 0 开始算配额,这是长跑 Agent 最经典的漏洞
on_limit="finish" + finish_reserve_seconds=30触限不是硬砍,而是留 30 秒预算让 Agent 写完收尾(保存 checkpoint、汇报进度)。硬砍会留下一个状态不明的死会话

StrictInt 而非 int,加上 extra="forbid",意味着一个手滑传进来的 max_total_tokens: "10000"(字符串)会在构造时就抛错,而不是在运行时悄悄变成 0。

2.2 MVP 复刻:20 行看清双 scope 差异

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from dataclasses import dataclass
from typing import Literal


class LimitExceeded(Exception):
def __init__(self, dim, limit, observed):
super().__init__(f"{dim}: limit={limit} observed={observed}")
self.dim, self.limit, self.observed = dim, limit, observed


@dataclass
class SessionUsage:
total_tokens: int = 0
tool_calls: int = 0
run_baseline_tokens: int = 0 # 本次 run 进入时的累计水位
run_baseline_tool_calls: int = 0
run_active_seconds: float = 0.0 # 不含人工审批等待


@dataclass(frozen=True)
class RunLimits:
max_total_tokens: int | None = None
max_tool_calls: int | None = None
max_elapsed_seconds: int | None = None
scope: Literal["session", "run"] = "run"


class LimitEnforcer:
"""在每次模型调用前跑一遍。"""

def __init__(self, limits: RunLimits, usage: SessionUsage):
self.limits, self.usage = limits, usage

def _read(self, dim: str) -> int:
u, sc = self.usage, self.limits.scope
if sc == "session": # 整个 durable session 的累计值
return {"tokens": u.total_tokens, "tools": u.tool_calls,
"seconds": int(u.run_active_seconds)}[dim]
return {"tokens": u.total_tokens - u.run_baseline_tokens, # 本次的增量
"tools": u.tool_calls - u.run_baseline_tool_calls,
"seconds": int(u.run_active_seconds)}[dim]

def check(self) -> None:
for dim, limit in (("tokens", self.limits.max_total_tokens),
("tools", self.limits.max_tool_calls),
("seconds", self.limits.max_elapsed_seconds)):
if limit is not None and self._read(dim) >= limit:
raise LimitExceeded(dim, limit, self._read(dim))


# 场景:session 已累计 95k token,现在 resume 一个只该花 10k 的小任务
u = SessionUsage(total_tokens=95_000, run_baseline_tokens=95_000,
tool_calls=40, run_baseline_tool_calls=40,
run_active_seconds=120.0)

LimitEnforcer(RunLimits(max_total_tokens=10_000, scope="run"), u).check()
print("run scope → 通过(本 run 只花了 0)")

try:
LimitEnforcer(RunLimits(max_total_tokens=100_000, scope="session"), u).check()
except LimitExceeded as e:
print("session scope →", e)

实际输出:

1
2
run scope    → 通过(本 run 只花了 0)
session scope → tokens: limit=100000 observed=95000

第二个场景最直观:scope="run" 时这次 resume 一分钱预算都还有,scope="session" 时它能看到全局水位只剩 5k 余量。同一份 usage 数据,两种 scope 给出两种完全不同的风险视图 —— 你可以根据任务性质选:跑一个隔离的临时补丁用 run,跑一条持续消耗的生产流水线用 session。


三、原语二:三态证据诚实 —— 这是 Cayu 最狠的一招

3.1 问题从哪来

Cayu 的 docs/operation-outcomes.md 开头这句话,值得任何做 Agent 评测的人抄在墙上:

evidence_state=observed means counts describe the captured evidence, not that work succeeded. incomplete means capture coverage is incomplete; counts are observations, not complete totals. Older results without this optional field render as not_reported; zero observations are not proof of successful work.

翻译成人话:

  • 一个 Agent 跑完,报表说”20 条命令全部 0 退出” —— 这只能证明抓到了 20 条 0 退出的记录,不能证明任务成功。它可能压根没跑你想让它跑的东西。
  • 一个 Agent 跑完,报表啥也没有 —— 这是 not_reported,不是”零错误”。

绝大多数 Agent 框架的默认行为是把”没观测到失败”塌缩成”成功”。Cayu 拒绝这个塌缩。

graph LR
    subgraph BAD["❌ 多数框架的塌缩"]
        B1["观察:0 条失败"] --> B2["结论:✅ 成功"]
    end

    subgraph GOOD["✅ Cayu 的三态"]
        G1["observed<br/>证据完整"] --> G2["可讨论成败<br/>但仍需 DoD 对照"]
        G3["incomplete<br/>采集覆盖不全"] --> G4["只能说:观测到 N 条<br/>≠ 完整总数"]
        G5["not_reported<br/>没有汇总源"] --> G6["只能说:不知道"]
    end

    style B1 fill:#FFB3C6,stroke:#F06292,stroke-width:2px,color:#333
    style B2 fill:#FFB3C6,stroke:#F06292,stroke-width:2px,color:#333
    style G1 fill:#B5EAD7,stroke:#80CBC4,stroke-width:2px,color:#333
    style G2 fill:#B5EAD7,stroke:#80CBC4,stroke-width:2px,color:#333
    style G3 fill:#FFF9C4,stroke:#F9A825,stroke-width:2px,color:#333
    style G4 fill:#FFF9C4,stroke:#F9A825,stroke-width:2px,color:#333
    style G5 fill:#C7CEEA,stroke:#9FA8DA,stroke-width:2px,color:#333
    style G6 fill:#C7CEEA,stroke:#9FA8DA,stroke-width:2px,color:#333

3.2 代码里的 fail-closed 校验

这个原语在代码里的形态,是一串互相校验的 model_validator —— 任何自相矛盾的证据在构造时就被拒:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# src/cayu/evals/operation_outcomes.py(14982 字符,节选核心校验)

EvidenceState = Literal["not_reported", "observed", "incomplete"]


class OperationOutcomeEvidence(BaseModel):
"""无内容引用:只存 ID + 分类,不复制原始 payload。"""

model_config = ConfigDict(extra="forbid", frozen=True)
dimension: Literal["invocation", "command", "http"]
state: OperationOutcomeState
exit_code: StrictInt | None = None
timed_out: StrictBool | None = None
http_status_code: StrictInt | None = Field(default=None, ge=100, le=599)

@model_validator(mode="after")
def validate_observation(self):
if self.dimension == "command":
# 分类由证据**推导**,不能自己声明
expected = ("timed_out" if self.timed_out is True
else "cancelled" if self.cancelled is True
else "not_reported" if self.exit_code is None
else "zero_exit" if self.exit_code == 0
else "nonzero_exit")
if self.state != expected:
raise ValueError("Command classification contradicts its reported outcome.")
elif self.dimension == "http":
expected = ("not_reported" if self.http_status_code is None
else "http_error" if self.http_status_code >= 400
else "response")
if self.state != expected or any(v is not None for v in (
self.exit_code, self.timed_out, self.cancelled)):
raise ValueError("HTTP classification contradicts its reported outcome.")
return self


class OperationOutcomeSummary(BaseModel):
evidence_state: Literal["not_reported", "observed", "incomplete"] = "not_reported"
counts: OperationOutcomeCounts = Field(default_factory=OperationOutcomeCounts)
evidence: tuple[OperationOutcomeEvidence, ...] = Field(default=(), max_length=256)
omitted_evidence_count: StrictInt = Field(default=0, ge=0)

@model_validator(mode="after")
def validate_counts(self):
# 计数必须 == 保留的引用数 + 被省略的数量,一个都不能对不上
total = sum(self.counts.model_dump().values())
if total != len(self.evidence) + self.omitted_evidence_count:
raise ValueError("Outcome counts must match retained and omitted observations.")
# not_reported 状态下**不允许携带任何观测**
if self.evidence_state == "not_reported" and total:
raise ValueError("Not-reported summaries cannot carry observations.")
return self

三个值得学的设计:

  1. state 由证据推导,不是由调用方声明。想谎报 zero_exit?那 exit_code 必须也是 0,两个字段互相锁死。
  2. 跨维度污染被显式禁止。一个 HTTP 观测不能带 exit_code,一个 command 观测不能带 http_status_code。这堵住的是”把 HTTP 403 塞进 exit_code 假装命令成功”这类混淆。
  3. 超长 ID 用 sha256 指纹代替(_MAX_REFERENCES = 256):"sha256:" + sha256(value).hexdigest()。既保留了可追溯性(拿指纹回查原 ID),又不会让一个 10MB 的 base64 撑爆报表。

3.3 最诚实的一句话:Runtime 不解析 stdout

1
2
3
4
Runtime does not parse stdout for HTTP status. An exit-zero script printing `403`
is still an observed zero exit, with HTTP `not_reported`. It could have printed
a fixture, handled an expected response, or made several requests. Command runners
cannot determine the script's application outcome.

这段话是我在整个项目里读到的最有工程分量的免责声明。它的意思是:一个 exit 0 的脚本打印了 403,Cayu 拒绝把它记成 HTTP 错误 —— 因为脚本可能打印的是测试固件、可能处理了一个预期内的响应、也可能发了三次请求。命令行 runner 没有能力判断脚本的应用层结果,所以它就不假装有。

HTTP 状态必须由工具显式声明:

1
2
3
4
5
6
7
8
9
from cayu import ToolResult
from cayu.evals.operation_outcomes import HttpOperationOutcomeV1

result = ToolResult(
content="Request finished",
structured={
"operation_outcome": HttpOperationOutcomeV1(status_code=403).model_dump(mode="json"),
},
)

protocol: Literal["http"] + status_code: StrictInt(ge=100, le=599) 双重约束,保证任意 result 字段或输出文本都不会被误当成 HTTP 证据。


四、原语三:可复用授权 —— 五维边界 + 采样复审

4.1 四种权限,谁说了算

Cayu 把授权拆成四种,每一种的决策者和存储位置都不同:

Kind谁决定存在哪Cayu 保证什么
一次性审批resolver,针对一个待决调用session checkpoint + 审批事件恰好一次解析与执行
可复用授权resolver,针对一类后续调用ToolAuthorizationGrantStore精确匹配、scope 与边界隔离、过期、撤销、可审计使用证据
应用自有策略你的 ToolPolicy + 你的 store你的应用只有策略返回的;Cayu 强制执行 ALLOW/DENY/REQUIRE_APPROVAL
Runtime 强制管控CayuRuntimeREQUIRE_APPROVAL 永远暂停;强制管控永不可授权;授权永不覆盖 DENY

关键不变量写在文档里:ToolAuthorizationGrantPolicy 只把自己的 REQUIRE_APPROVAL 变成 ALLOW,它永远不碰 DENY。 授权是”给审批减负”,不是”开后门”。

4.2 五维边界:跨任一维都不继承

一个 grant 要生效,必须同时满足 5 个条件(docs/tool-authorization-grants.md):

维度规则
policy_id + tool_name + approval_gate三者全等
scopesession 只在原 session incarnation 内有效(新 session 复用被删 session 的 id 不继承);workspace 在同 workspace_id 的任何 session 内有效
boundary(5 维)agent name / environment name / workspace id / principal(authority+subject)/ 指定的 session labels —— 任一不等即失效
matcherprocess_command 要求 argv / cwd / env / stdin 严格全等;tool_call 覆盖同 gate 下整类决策
状态active 且未到 expires_at。过期是观察出来的(effective_status(now)),永远不是一次写入

几条细节非常见功力:

  • process_command matcher 保留 argv 和 cwd,但对 env 和 stdin 只存摘要 —— 因为这两个字段经常装凭据。
  • 前缀匹配永远不成立:("git", "status") 的 grant 不匹配 ("git", "status", "--short")。文档原话:Shell text, unknown argument keys, prefixes, and longer or shorter argument vectors never match。
  • 派生 default workspace_id 不携带 workspace grant —— 因为一个用路径派生的 id 会被无关 workspace 共享;但 session grant 在那里仍然可用。
  • session 缺少任一被钉住的 label → 不可授权(fail-closed,不是 fail-open)。
  • boundary_labels 能收窄但不是权限边界 —— 客户端可以自己设 label,所以文档明确写了:租户隔离要靠 resource access 来做。

4.3 采样复审:算出一个哈希,而不是掷一次骰子

这是我最喜欢的一个原语。granted 的调用不是 100% 放行,而是按 sampled_review_rate_bps(0..10000 基点)抽一部分回到人工。

1
2
3
4
5
6
7
8
# cayu 的算法(文档原文)
# For each granted call Cayu computes a sample bucket from the SHA-256 of the
# grant id, session id, and tool call id (`tool_authorization_grant_sample_bucket`).
# If the bucket is below the rate, the call still goes to a person.

digest = sha256(f"{grant_id}|{session_id}|{tool_call_id}".encode()).hexdigest()
bucket = int(digest[:8], 16) % 10_000
sampled = bucket < sampled_review_rate_bps

为什么用哈希而不用 random.random() < rate?

用随机数用持久身份的 sha256
同一个调用重放两次,判定可能不同重放、恢复、审计三者必然得到同一个判定
无法事后复核”当时为什么抽中了他”拿 grant_id + session_id + call_id 就能离线复算
分布式多 worker 下无法协调每个 worker 独立算出同一结果,无需共享状态

文档里那句总结很精辟:The bucket depends only on durable identity, so replay, recovery, and audit reach the same decision. 另外还有一条:抽样复审的那次审批,本身不能再生成新的 grant(防止”抽中→批准→再授权→下次免审”的绕过链条)。

graph TB
    T["🔧 Tool 调用<br/>run('pytest','-q')"]
    M{"🔐 Mandatory<br/>Control?"}
    P["📋 ToolPolicy 返回<br/>ALLOW / DENY / REQUIRE_APPROVAL"]
    G{"🎫 有匹配的<br/>Active Grant?"}
    S{"🎲 sha256 采样<br/>bucket &lt; rate?"}
    A1["👤 人工审批"]
    A2["👤 人工审批<br/>+ 记录 grant offer"]
    OK["✅ 直接执行<br/>写 tool.call.completed 事件"]
    NO["🚫 DENY 直接拒绝"]

    T --> M
    M -->|"是"| A1
    M -->|"否"| P
    P -->|"DENY"| NO
    P -->|"ALLOW"| OK
    P -->|"REQUIRE_APPROVAL"| G
    G -->|"无匹配 / 已过期 / 已撤销<br/>/ 边界不等"| A1
    G -->|"匹配"| S
    S -->|"命中"| A2
    S -->|"未命中"| OK

    style T fill:#C7CEEA,stroke:#9FA8DA,stroke-width:2px,color:#333
    style M fill:#FFDAB9,stroke:#FFB74D,stroke-width:2px,color:#333
    style P fill:#FFDAB9,stroke:#FFB74D,stroke-width:2px,color:#333
    style G fill:#E8D5F5,stroke:#CE93D8,stroke-width:2px,color:#333
    style S fill:#E8D5F5,stroke:#CE93D8,stroke-width:2px,color:#333
    style A1 fill:#FFB3C6,stroke:#F06292,stroke-width:2px,color:#333
    style A2 fill:#FFB3C6,stroke:#F06292,stroke-width:2px,color:#333
    style OK fill:#B5EAD7,stroke:#80CBC4,stroke-width:2px,color:#333
    style NO fill:#FFB3C6,stroke:#F06292,stroke-width:2px,color:#333

五、原语四(附加):进程级快照 —— DMTCP 上的执行恢复

前面三个原语都在”决策正确性”层面,Cayu 还有一层更硬的:能不能让一个跑了 3 小时的 Python 进程,在容器被销毁后原样复活?

Cayu 的答案是 DMTCP(Distributed MultiThreaded CheckPoint),不是 CRIU。docs/adr/0002-execution-snapshot-substrate.md(19015 字符)把整条探索路径失败记录都留了下来:

尝试结果
Docker checkpoint API实验性功能,且需要 daemon 侧 CRIU
AWS Lambda MicroVM 公共 API没有“把任意运行中的 allocation 冻结成不可变快照”的操作
CRIU + kcmp syscallstaging workspace 上真实捕获返回 ENOSYS —— 这是guest kernel 能力缺口,不是权限问题
DMTCP 4.2.0✅ 在 UID 1000 下捕获合成进程,在 Docker 和 staging AWS 上各恢复两次,源进程已被销毁

CRIU 那段排查写得尤其诚实:他们用完整的可信 capabilities + Seccomp mode 0 做了独立 syscall 探针复现,确认 ENOSYS 来自 guest 内核本身,然后明确写下”内核配置未暴露,因此不推断任何具体 build option”。不做无根据的归因,这是工程文档的正确姿势。

但更要命的是 Cayu 对保真度边界的声明 —— 它把”我做不到什么”写得比”我能做到什么”还清楚:

The qualified fidelity on both substrates is one cooperating Linux Python process, including its memory, background writer and local listener, plus three bounded workspace files and engine support files at a quiescent boundary. Arbitrary tool processes, the actual unprivileged agent profile, PTYs, browser state, live external connections, devices, mounted volumes, complete container/VM state and cross-host portability remain unqualified.

翻译:只支持一个配合的 Linux Python 进程,在静止边界上,连同它自己拥有的那几个文件。fork 树、PTY、浏览器状态、活跃 TCP 连接、挂载卷、完整 VM 状态、跨主机迁移 —— 全部未验证。

还有两条边界设计特别关键:

  1. 静止边界由应用控制,不是”看到一次模型请求”。文档原话:Capture at an application-controlled idle boundary, after tools have settled. An outbound model request does not establish this boundary. —— 因为等一次模型往返可能长达 30 秒,期间状态一直在变。
  2. 特权留在可信控制器手里,agent 工具拿不到。Privileged checkpoint control belongs to the trusted controller; agent tools must not gain its privileges.
  3. 恢复目标必须为空,Cayu 永不清空已有 workspace 来腾地方。

六、横向对比:Cayu vs 三个同赛道项目

同赛道我挑了三个最有代表性的:Temporal.io(durable execution 的工业标准)、LangGraph(状态机图 Agent)、cayu 自己。

维度Temporal.ioLangGraphCayu
核心抽象Workflow / Activity 代码图 + 状态Runtime + 事件流 + 5 层 policy
执行模型Replay 历史确定性重演图节点流转Agent loop + append-only durable events
进程级恢复❌ 只恢复逻辑状态,不恢复内存❌ 只恢复图状态✅ DMTCP 进程内存 + 拥有文件
预算控制Activity 级超时/重试❌ 无原生预算✅ 三维硬上限 × 双 scope
权限模型Worker 身份❌ 无✅ 一次性审批 / 可复用授权 / 应用策略 / 强制管控 四层
证据诚实度✅ 确定性 replay⚠️ 状态即事实✅✅ observed/incomplete/not_reported 三态 + 交叉校验
人工闸门Signal(可等待)Interrupt(需自己实现持久化)✅ Durable approval + 等待时间不计入 active time
语言Java / Go / Python / TSPython / JS仅 Python
上手成本需理解 workflow 确定性需理解图编译需理解 Runtime 与 Harness 四层切分

设计差异的本质:

  • Temporal 的核心是”重演” —— 它信任历史事件足以重建状态,所以它敢把整个执行逻辑重跑一遍。代价是 workflow 代码必须写得像”没有副作用”(所有副作用走 Activity)。这对 Agent 场景很不友好,因为 Agent 的推理本身就是有副作用的、有状态的。
  • LangGraph 的核心是”图” —— 状态机很清晰,但图节点之间的转移需要你自己写判断。它不做预算、不做权限、不做证据,因为它假设”谁来写这个节点逻辑,谁就负责正确性”。
  • Cayu 的核心是”证据” —— 它假设 Agent 会犯错、会漂移、会撒谎,所以每一个结论都必须能追到不可被 Agent 修改的 append-only 事件流上。代价是概念负担明显更重,3689 个 Python 文件不是白长的。

一句话总结三者的哲学差异:

Temporal 相信确定性的代码,Cayu 承认不确定的执行。 后者更诚实,代价是更复杂。


七、优缺点:左简约 / 右严谨

左侧(架构简洁性 / 扩展性 / 易用性)右侧(性能 / 复杂度 / 维护性)
✅ core 极小:只定义 events / messages / agents / tools / 抽象契约,稳定不变。新增能力不会污染 core⚠️ 3689 个 Python 文件:最大的 _session_engine.py 有 47.6 万字符,_recovery_coordinator.py 46 万字符。认知负担极高,新人上手曲线陡峭
✅ Harness / Runtime 四层分离:换领域逻辑不用 fork 框架,这是真正的架构收益⚠️ 110+ 篇 docs:docs/architecture.md 单文件 106863 字符。文档本身成了维护负担 —— 架构文档和代码脱节的风险很高
✅ MCP 走 adapter 降级为普通 tool:外部 server 不开特权通道,同一套 policy/approval/transcript⚠️ 只支持 Python:文档明确说 Cayu is Python only and is not a graph DSL,无法嵌入 JVM / Go 技术栈
✅ Pydantic 严格模式贯穿:StrictInt + extra="forbid" + frozen=True 让配置错误在构造时暴露,而不是运行时静默⚠️ fail-closed 带来可用性代价:session 缺任一被钉住的 label 就不可授权,边界配置不当会静默拒绝本该放行的调用,调试需要读日志
✅ on_limit="finish" + 30s 收尾预算:触限不是硬砍,留 30 秒写 checkpoint⚠️ DMTCP 适配器是 opt-in:现有 runner 和 environment 默认报 unsupported,必须显式 attach 适配器才有快照能力
✅ Storage 三后端(InMemory / SQLite / Postgres)共享同一套转移规则:迁移到 Postgres 不改语义⚠️ DMTCP 进程必须是”配合的”:不捕获任意已运行命令、fork 树、PTY、浏览器状态、活跃 TCP 连接。这是 cooperative contract,不是对敌意 guest 的证明
✅ RunLimits 可选且渐进:has_run_limits() 判断是否设置,不设就没有任何强制开销⚠️ 采样复审的 20% 是统计意义上的:200 次调用实测 21.5% 走人工(有哈希分布抖动),不是精确配额
✅ 三份存储实现(grant store / task store / knowledge store)SQLite + Postgres 双写:一致性规则复用⚠️ evidence_state="incomplete" 时的报表可用性下降:诚实了,但对只关心”过没过”的 CI 门禁,需要额外一层判定逻辑

我的判断:Cayu 是我见过的最不像 demo 的开源 Agent Runtime,也是我见过的学习成本最高的一个。它的复杂度不是设计失误 —— 3689 个文件里的绝大部分是在处理”真实世界里会发生的脏情况”:进程崩溃到一半、快照传到一半、审批等了一夜、快照操作在提交的最后一刻被网络打断。这些脏情况在 demo 里永远不会出现,但长跑 Agent 一定会遇到。

如果你的场景是”单机跑 30 分钟以内的任务”,Cayu 明显过重,用 LangGraph 或者干脆一个 while 循环 + SQLite checkpoint 就够了。
如果你的场景是”跨天、跨容器、跨审批、且必须能证明它干了活”,Cayu 那套原语几乎每一条都能直接抄。


八、从零搭建启示:我的最小可行实现(MVP)

如果我复刻一套自己的 Durable Agent Runtime,必须做和可以先不做的清单如下:

8.1 必须做(缺了就等于不可信)

组件最小实现对应 Cayu 原语
Append-only 事件流一个 append-only 表,事件永不 UPDATE/DELETE,Agent 无写权限Durable Events
RunLimits 双 scope上面 20 行代码就是最小实现,别再简化了原语一
三态 evidenceobserved / incomplete / not_reported,且 not_reported 禁止携带观测原语二
审批 pending 检查点审批状态写进事件流,resume 时读出来Approvals
等待时间不计入 active time审批开始/结束时间戳分开记原语一的隐含要求

8.2 可以先不做(等真的撞上了再加)

  • ❌ DMTCP 进程快照 —— 复杂度极高,且保真度边界很窄。绝大多数场景”重放事件流 + 重新执行未完成的工具调用”就够了。
  • ❌ Postgres 存储 —— SQLite WAL 模式能扛到单写者并发。
  • ❌ Lambda MicroVM runner —— Docker + uid 1000 足够。
  • ❌ 可复用授权(grant) —— 先用”每次都要批”,人工审批疲劳后再上,且要一开始就带边界字段(后期加边界是破坏性变更)。
  • ❌ MCP adapter 通道 —— 等真有外部 server 要接。

8.3 踩坑预警(我实测踩过的)

  1. scope 默认必须是 run,但要记得切 session —— 用 run 的团队会被”resume 循环烧穿 session 累计预算”咬;用 session 的团队会被”人工等待 3 小时误触发熔断”咬。这是两个方向相反的坑,没有中立解。
  2. frozen=True + StrictInt 别省 —— 一旦你允许 max_total_tokens 传字符串进去,等到第 10 万次调用你才会发现它被静默当成了 0。
  3. 超长 ID 一定要用 sha256 指纹 —— 工具调用 ID 里塞 base64 payload 是常见做法,直接存会把报表撑到几十 MB。
  4. 哈希采样是 bucket < rate,不是 bucket <= rate —— rate_bps=10000 表示 100% 复审,此时 bucket 最大 9999,< 10000 恒真,语义正确;写成 <= 就变成永远不复审。
  5. 边界配置用 fail-closed 时,一定要写告警日志 —— 否则”为什么我的 grant 不生效”会成为最难排查的问题。Cayu 的规则是”session 缺任一被钉住的 label 就不可授权”,这条在文档里、在代码里,但第一次遇到的人一定会卡住。

九、完整 MVP:三大原语的可运行复刻

下面这份 200+ 行代码把上面三个原语抽离成零依赖的可运行实现。它不是伪代码 —— 我实测跑通,输出见文末。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"""MVP: 从 cayu-dev/cayu 抽离的 3 大核心机制最小可运行复刻

抽取自 cayu 的真实设计:
1. RunLimits —— run / session 两种 scope 的硬上限(每次模型调用前检查)
2. Evidence-state —— observed / incomplete / not_reported 三态认知诚实
3. AuthorizationGrant —— 可复用授权 + 边界隔离 + 采样复审

运行: python3 mvp_cayu.py (无外部依赖,纯标准库)
"""
from __future__ import annotations

from collections import Counter
from dataclasses import dataclass, field, replace
from enum import Enum
from hashlib import sha256
from typing import Literal


# ─────────────────────────────────────────────────────────────
# 原语 1:RunLimits —— 硬上限的 run / session 双 scope
# ─────────────────────────────────────────────────────────────
@dataclass(frozen=True)
class RunLimits:
"""一次 run 或 resume 的可选硬上限。

关键设计(源自 cayu/budgets/run_limits.py):
- scope="run"(默认):全部指标量的是"本次 runtime 调用",
即自 run 进入以来的用量增量 + 自 run 开始的 active time。
人工审批等待的间隔不计入 active time —— 否则一个
"等人点确认 3 小时的审批" 会把 30 分钟的 limit 烧光。
- scope="session":全部指标量的是整个 durable session 的累计值。
一个"已经超限的 session 被 resume"会立刻停机,而不是
从 0 重新开始烧一遍。
"""

max_total_tokens: int | None = None
max_tool_calls: int | None = None
max_elapsed_seconds: int | None = None
scope: Literal["session", "run"] = "run"
on_limit: Literal["interrupt", "finish"] = "interrupt"
finish_reserve_seconds: int = 30

def __post_init__(self) -> None:
for name in ("max_total_tokens", "max_tool_calls", "max_elapsed_seconds"):
v = getattr(self, name)
if v is not None and v < 1:
raise ValueError(f"{name} must be >= 1")


class LimitExceeded(Exception):
def __init__(self, dim: str, limit: int, observed: int) -> None:
super().__init__(f"{dim}: limit={limit} observed={observed}")
self.dim, self.limit, self.observed = dim, limit, observed


@dataclass
class SessionUsage:
"""session 级别的累计账本(对应 cayu 的 event stream 累计)。"""

total_tokens: int = 0
tool_calls: int = 0
run_baseline_tokens: int = 0 # 本次 run 进入时的累计水位
run_baseline_tool_calls: int = 0
run_active_seconds: float = 0.0 # 不含人工审批等待


class LimitEnforcer:
"""在每次模型调用前跑一遍。这是 cayu 的 "checked before each model call"。"""

def __init__(self, limits: RunLimits, usage: SessionUsage) -> None:
self.limits, self.usage = limits, usage

def _read(self, dim: str) -> int:
u, sc = self.usage, self.limits.scope
if sc == "session":
return {"tokens": u.total_tokens, "tools": u.tool_calls,
"seconds": int(u.run_active_seconds)}[dim]
return {"tokens": u.total_tokens - u.run_baseline_tokens,
"tools": u.tool_calls - u.run_baseline_tool_calls,
"seconds": int(u.run_active_seconds)}[dim]

def check(self) -> None:
for dim, limit in (("tokens", self.limits.max_total_tokens),
("tools", self.limits.max_tool_calls),
("seconds", self.limits.max_elapsed_seconds)):
if limit is not None and self._read(dim) >= limit:
raise LimitExceeded(dim, limit, self._read(dim))


# ─────────────────────────────────────────────────────────────
# 原语 2:Evidence-state —— "零观测不等于成功"的三态认知诚实
# ─────────────────────────────────────────────────────────────
EvidenceState = Literal["not_reported", "observed", "incomplete"]


class OutcomeState(str, Enum):
ZERO_EXIT = "zero_exit"
NONZERO_EXIT = "nonzero_exit"
TIMED_OUT = "timed_out"
NOT_REPORTED = "not_reported"


@dataclass(frozen=True)
class CommandObservation:
"""一次命令执行的无内容引用(cayu 用 sha256 指纹代替超长 ID)。"""

execution_id: str
state: OutcomeState
exit_code: int | None = None
timed_out: bool | None = None

def __post_init__(self) -> None:
# cayu 的 model_validator:分类必须与原始证据自洽
if self.timed_out is True:
expected = OutcomeState.TIMED_OUT
elif self.exit_code is None:
expected = OutcomeState.NOT_REPORTED
elif self.exit_code == 0:
expected = OutcomeState.ZERO_EXIT
else:
expected = OutcomeState.NONZERO_EXIT
if self.state is not expected:
raise ValueError(f"分类 {self.state.value} 与证据矛盾,应为 {expected.value}")


@dataclass
class OutcomeSummary:
"""cayu 的一句话契约:counts describe only the supplied evidence。"""

evidence_state: EvidenceState = "not_reported"
counts: Counter = field(default_factory=Counter)
observed: int = 0 # 实际抓到的观测数
capture_complete: bool = True

def __post_init__(self) -> None:
# cayu 的 validate_counts:not_reported 不能携带任何观测
if self.evidence_state == "not_reported" and self.observed:
raise ValueError("not_reported 的 summary 不能携带观测")

@property
def proves_success(self) -> bool:
"""只有 evidence_state=="observed" 且全零退出 才可能谈得上成功。

这就是 cayu 那句 "zero observations are not proof of
successful work" 的代码形态。
"""
if self.evidence_state != "observed":
return False
if not self.counts:
return False # 抓到 0 条观测 ≠ 成功
return self.counts.get(OutcomeState.ZERO_EXIT.value, 0) == self.observed


# ─────────────────────────────────────────────────────────────
# 原语 3:AuthorizationGrant —— 可复用授权 + 5 维边界 + 采样复审
# ─────────────────────────────────────────────────────────────
class GrantStatus(str, Enum):
ACTIVE = "active"
REVOKED = "revoked"
EXPIRED = "expired"


@dataclass
class Boundary2:
agent: str
environment: str
workspace_id: str
principal_subject: str | None
labels: dict[str, str]

def matches(self, other: "Boundary2") -> bool:
if (self.agent, self.environment, self.workspace_id) != (
other.agent, other.environment, other.workspace_id
):
return False
if self.principal_subject != other.principal_subject:
return False
# session 缺任一被钉住的 label → 不可授权
return all(other.labels.get(k) == v for k, v in self.labels.items())


@dataclass
class Grant:
grant_id: str
policy_id: str
tool_name: str
approval_gate: str
matcher_kind: Literal["process_command", "tool_call"]
matcher_argv: tuple[str, ...] | None
boundary: Boundary2
scope_kind: Literal["session", "workspace"]
scope_id: str
created_at: float
expires_at: float | None
sampled_review_rate_bps: int = 0 # 0..10000 基点
status: GrantStatus = GrantStatus.ACTIVE
mandatory_control: bool = False # 强制管控,永不授权

def effective_status(self, now: float) -> GrantStatus:
"""过期是"观察"出来的,不是一次写入 —— cayu 的 effective_status(now)。"""
if self.status is GrantStatus.REVOKED:
return GrantStatus.REVOKED
if self.expires_at is not None and now >= self.expires_at:
return GrantStatus.EXPIRED
return GrantStatus.ACTIVE


class GrantDecision(str, Enum):
ALLOW = "allow"
REQUIRE_APPROVAL = "require_approval"
DENY = "deny"


@dataclass
class GrantPolicy:
"""ToolAuthorizationGrantPolicy:只把 REQUIRE_APPROVAL 变成 ALLOW,永不碰 DENY。"""

grants: list[Grant]
mandatory_controls: set[tuple[str, str | None]] = frozenset()

def _is_mandatory(self, tool: str, gate: str) -> bool:
return (tool, gate) in self.mandatory_controls or (tool, None) in self.mandatory_controls

def _matcher_accepts(self, g: Grant, tool: str, argv: tuple[str, ...] | None) -> bool:
if g.matcher_kind == "process_command":
# 严格全等:argv 多一个/少一个/前缀匹配 都不匹配
return g.matcher_argv == argv
return True # tool_call:同一 gate 下的整类决策

def evaluate(self, *, now: float, tool: str, gate: str, policy_id: str,
argv: tuple[str, ...] | None, boundary: Boundary2,
scope_kind: str, scope_id: str, call_id: str) -> GrantDecision:
# 1) 强制管控永远要人看,即使下游 policy 会 ALLOW
if self._is_mandatory(tool, gate):
return GrantDecision.REQUIRE_APPROVAL

# 2) session grant 先于 workspace grant
candidates = [
g for g in self.grants
if g.effective_status(now) is GrantStatus.ACTIVE
and not g.mandatory_control
and (g.policy_id, g.tool_name, g.approval_gate) == (policy_id, tool, gate)
and self._matcher_accepts(g, tool, argv)
and g.boundary.matches(boundary)
]
candidates.sort(key=lambda g: (g.scope_kind == "workspace", g.created_at, g.grant_id))

for g in candidates:
if (g.scope_kind, g.scope_id) != (scope_kind, scope_id):
continue
# 3) 采样复审:bucket 只依赖持久身份 → 重放/恢复/审计同判
if g.sampled_review_rate_bps:
digest = sha256(f"{g.grant_id}|{scope_id}|{call_id}".encode()).hexdigest()
bucket = int(digest[:8], 16) % 10_000
if bucket < g.sampled_review_rate_bps:
return GrantDecision.REQUIRE_APPROVAL # 还是要人看
return GrantDecision.ALLOW
return GrantDecision.REQUIRE_APPROVAL


# ─────────────────────────────────────────────────────────────
# 演示
# ─────────────────────────────────────────────────────────────
def demo_1_run_limits() -> None:
print("=" * 72)
print("原语 1 RunLimits —— run scope vs session scope")
print("=" * 72)

# 场景 A:session 已经烧了 95k token,现在要 resume 一次只跑 10k 的活
u = SessionUsage(total_tokens=95_000, run_baseline_tokens=95_000, tool_calls=40,
run_baseline_tool_calls=40, run_active_seconds=120.0)
enforcer = LimitEnforcer(RunLimits(max_total_tokens=10_000, scope="run"), u)
try:
enforcer.check()
print(" run scope : 10k 上限,本 run 只花了 0 → 通过 ✅")
except LimitExceeded as e:
print(f" run scope : LimitExceeded({e})")

# 场景 B:同一个 session,改用 session scope
enforcer2 = LimitEnforcer(RunLimits(max_total_tokens=100_000, scope="session"), u)
try:
enforcer2.check()
print(" session scope: 100k 上限,累计 95k → 通过 ❗(还没到,但余量只剩 5k)")
except LimitExceeded as e:
print(f" session scope: LimitExceeded({e}) ✅ 不会重新烧一遍")

# 场景 C:run scope 到顶
u2 = SessionUsage(total_tokens=100_500, run_baseline_tokens=100_000,
tool_calls=12, run_baseline_tool_calls=10, run_active_seconds=61.0)
enforcer3 = LimitEnforcer(
RunLimits(max_total_tokens=10_000, max_tool_calls=10, max_elapsed_seconds=60), u2)
try:
enforcer3.check()
print(" 三维同时检查 : 通过 ❗")
except LimitExceeded as e:
print(f" 三维同时检查 : LimitExceeded[{e.dim}] limit={e.limit} observed={e.observed} ✅")
print()


def demo_2_evidence_state() -> None:
print("=" * 72)
print("原语 2 Evidence-state —— 零观测不等于成功")
print("=" * 72)

# 真实抓到 2 条,都是 0 退出
obs = [CommandObservation("e1", OutcomeState.ZERO_EXIT, exit_code=0),
CommandObservation("e2", OutcomeState.ZERO_EXIT, exit_code=0)]
s = OutcomeSummary(evidence_state="observed", capture_complete=True)
for o in obs:
s.counts[o.state.value] += 1
s.observed += 1
print(f" observed/2条0退出 : proves_success={s.proves_success} ✅")

# 抓到了 0 条观测(runner 没上报 execution_id)
s0 = OutcomeSummary(evidence_state="observed", capture_complete=True)
print(f" observed/0条观测 : proves_success={s0.proves_success} ✅ "
"← cayu: zero observations are not proof of successful work")

# 采集覆盖不全
si = OutcomeSummary(evidence_state="incomplete", capture_complete=False)
si.counts["zero_exit"] = 3
si.observed = 3
print(f" incomplete/3条0退出: proves_success={si.proves_success} ✅ 不能当成功证据")

# 分类与证据矛盾的观测直接被拒
try:
CommandObservation("e3", OutcomeState.ZERO_EXIT, exit_code=1)
print(" ❗矛盾观测没被拒")
except ValueError as e:
print(f" 矛盾观测 : ValueError({e}) ✅ fail-closed")
print()


def demo_3_grants() -> None:
print("=" * 72)
print("原语 3 AuthorizationGrant —— 边界隔离 + 采样复审")
print("=" * 72)

b_repo1 = Boundary2("coder", "sandbox", "repo-1", "user:alice", {"tenant": "acme"})
b_repo2 = Boundary2("coder", "sandbox", "repo-2", "user:alice", {"tenant": "acme"})
b_bob = Boundary2("coder", "sandbox", "repo-1", "user:bob", {"tenant": "acme"})

grant = Grant(
grant_id="g1", policy_id="shell", tool_name="run",
approval_gate="shell.exec", matcher_kind="process_command",
matcher_argv=("pytest", "-q"), boundary=b_repo1,
scope_kind="workspace", scope_id="repo-1", created_at=100.0,
expires_at=200.0, sampled_review_rate_bps=0,
)

def ev(**kw):
base = dict(now=150.0, tool="run", gate="shell.exec", policy_id="shell",
argv=("pytest", "-q"), scope_kind="workspace", scope_id="repo-1",
call_id="c1")
base.update(kw)
return GrantPolicy([grant]).evaluate(**base)

print(f" 同 workspace 同主体 : {ev(boundary=b_repo1).value} ✅")
print(f" 换 workspace(repo-2) : {ev(boundary=b_repo2).value} ✅ 边界隔离")
print(f" 换主体(user:bob) : {ev(boundary=b_bob).value} ✅ 主体隔离")
print(f" argv 多一个 -x : "
f"{ev(boundary=b_repo1, argv=('pytest', '-q', '-x')).value} ✅ 严格全等")
print(f" 已过期(now=250) : "
f"{ev(boundary=b_repo1, now=250.0).value} ✅ 过期是观察不是写入")

# 强制管控:即使有 grant 也永远要人看
mandatory = GrantPolicy([grant], mandatory_controls={("run", "shell.exec")})
md = mandatory.evaluate(now=150.0, tool="run", gate="shell.exec", policy_id="shell",
argv=("pytest", "-q"), boundary=b_repo1,
scope_kind="workspace", scope_id="repo-1", call_id="c1")
print(" mandatory control : %s ✅ 强制管控永不可授权" % md.value)

# 采样复审 20%:同 grant,不同 call_id,判定由持久身份的 sha256 决定
policy20 = GrantPolicy([replace(grant, sampled_review_rate_bps=2000)])
hits = []
for i in range(200):
d = policy20.evaluate(now=150.0, tool="run", gate="shell.exec", policy_id="shell",
argv=("pytest", "-q"), boundary=b_repo1,
scope_kind="workspace", scope_id="repo-1", call_id=f"c{i}")
hits.append(d is GrantDecision.REQUIRE_APPROVAL)
pct = 100 * sum(hits) / len(hits)
print(f" 采样复审 2000bps/200 次 : {pct:.1f}% 走人工 "
f"(目标 20%) ✅ 幂等:重放同 call_id 判定不变")
d1 = policy20.evaluate(now=150.0, tool="run", gate="shell.exec", policy_id="shell",
argv=("pytest", "-q"), boundary=b_repo1,
scope_kind="workspace", scope_id="repo-1", call_id="c7")
d2 = policy20.evaluate(now=150.0, tool="run", gate="shell.exec", policy_id="shell",
argv=("pytest", "-q"), boundary=b_repo1,
scope_kind="workspace", scope_id="repo-1", call_id="c7")
print(f" 同 call_id 重放判定一致 : {d1 is d2} ✅")
print()


if __name__ == "__main__":
demo_1_run_limits()
demo_2_evidence_state()
demo_3_grants()
print("MVP 全部场景跑完 ✅")

实际运行输出(Python 3.12 实测):

========================================================================
原语 1  RunLimits —— run scope vs session scope
========================================================================
  run scope   : 10k 上限,本 run 只花了 0 → 通过 ✅
  session scope: 100k 上限,累计 95k → 通过 ❗(还没到,但余量只剩 5k)
  三维同时检查 : LimitExceeded[seconds] limit=60 observed=61 ✅

========================================================================
原语 2  Evidence-state —— 零观测不等于成功
========================================================================
  observed/2条0退出 : proves_success=True ✅
  observed/0条观测  : proves_success=False ✅ ← cayu: zero observations are not proof of successful work
  incomplete/3条0退出: proves_success=False ✅ 不能当成功证据
  矛盾观测       : ValueError(分类 zero_exit 与证据矛盾,应为 nonzero_exit) ✅ fail-closed

========================================================================
原语 3  AuthorizationGrant —— 边界隔离 + 采样复审
========================================================================
  同 workspace 同主体      : allow ✅
  换 workspace(repo-2)     : require_approval ✅ 边界隔离
  换主体(user:bob)         : require_approval ✅ 主体隔离
  argv 多一个 -x           : require_approval ✅ 严格全等
  已过期(now=250)          : require_approval ✅ 过期是观察不是写入
  mandatory control        : require_approval ✅ 强制管控永不可授权
  采样复审 2000bps/200 次   : 21.5% 走人工 (目标 20%) ✅ 幂等:重放同 call_id 判定不变
  同 call_id 重放判定一致  : True ✅

MVP 全部场景跑完 ✅```

---

## 十、总结:Cayu 教给 Harness 工程的四件事

**第一,长跑 Agent 的核心矛盾不是"能力不够",而是"无法证明它干了活"。** 你可以给 Agent 无限工具、无限 token、无限时间,但如果三个月后你无法回答"上周那批自动修复到底改了什么、有没有生效",那你拥有的不是 Agent 基础设施,是一台高速浪费钱的机器。

**第二,"没观测到失败"和"观测到成功"之间隔着一条鸿沟。** Cayu 用 `evidence_state` 三态把这条鸿沟显式化了。这个设计的迁移成本极低 —— 你今晚就能在自己的 Agent 里加上一个 `evidence_complete: bool` 字段,然后开始诚实地报告"我不知道"。

**第三,权限的粒度不该停在"这次调用批不批"。** 一次性审批会导致人工疲劳,疲劳会导致无脑点同意,无脑同意等于没审批。Cayu 的"五维边界 + 采样复审"给了中间态:高频重复调用走 grant,但每 20% 回到人工,且抽样由持久身份的哈希决定,所以可审计、可重放。

**第四,把"做不到什么"写清楚,比把"能做什么"写漂亮更有价值。** Cayu 的 ADR 0002 里,CRIU 的 ENOSYS、MicroVM API 的能力缺口、DMTCP 保真度的七条未验证项,全部原样保留在文档里。这不是示弱,这是让每一个读文档的人都知道**哪些承诺可以拿去上生产,哪些不行**。

> **如果你正在搭长跑 Agent:先别写 Agent 循环,先写事件流和证据校验。**
> 循环是最容易写的,也是最不重要的 —— 模型自己就能学会。
> 真正决定 Agent 能不能被信任的,是当它跑完 3 小时之后,你能不能**拿出证据**告诉别人它真的干了活。

---

## 附:项目信息

| 项 | 值 |
|----|---|
| 项目 | [cayu-dev/cayu](https://github.com/cayu-dev/cayu) |
| Star | 946 |
| License | Apache-2.0 |
| 语言 | Python(**仅 Python**) |
| 规模 | 4146 blobs / 3689 个 .py 文件 / 110+ 篇 docs |
| 创建 | 2026-07-16 |
| 定位 | Long-horizon Agent Runtime(durable sessions / approval / spend caps / multi-agent) |
| 本篇组件 | **横向对比**(Durable Execution × 权限模型 × 证据体系) |