# 长任务测试提效：Codex 技术参考规范

> 面向 Codex / 自动化代理的长任务执行参考。  
> 目标：在**不减少测试、不降低质量门槛、不伪造 GREEN** 的前提下，减少数小时级任务中的无效关键路径、盲目全量重跑、重复构建和环境冲突。

---

## 0. 文档用途

这不是一篇面向人类阅读的实践博客，而是一份供 Codex 在执行大型测试、跨仓验证、候选门禁和发布前验证时参考的**操作规范**。

Codex 使用本规范时，应优先做三件事：

1. **先判断当前变化真正使哪些证据失效。**
2. **先运行便宜、上游、能尽早失败的门禁。**
3. **只重跑真实失效的长阶段；未失效的 sealed artifact 只读复验。**

本规范适用于：

- 多仓库、多语言、多平台工程；
- 单次完整回归达到几十分钟或数小时；
- 存在代码生成、契约同步、模拟器/设备、数据库 reset、覆盖率门禁等共享或昂贵资源；
- 需要保存可审计测试证据；
- Candidate 与 Release 有明确边界。

本规范不规定具体项目名、仓库名、脚本名、测试数量、覆盖率阈值或内部基础设施地址。所有项目特定值应从当前仓库中的正式配置、policy、runner 或 task description 读取。

---

# 1. 规范关键词

本文使用以下关键词：

- **MUST**：必须满足，否则不能把该阶段判为可信 PASS。
- **MUST NOT**：严格禁止。
- **SHOULD**：默认应这样做；只有明确证据证明不适用时才可偏离。
- **SHOULD NOT**：默认不应这样做。
- **MAY**：可选优化。
- **BLOCKED**：由于缺少必要输入、环境、权限、可信 baseline 或外部条件，当前阶段不能被证明。
- **UNVERIFIED**：存在结果或局部事实，但尚未经过要求的正式验证。
- **PASS**：该 stage 的完整合同已经满足。
- **FAIL**：正式执行或 postflight 未满足合同。

Codex 不得把：

- “看起来没问题”
- “定向测试过了”
- “之前跑过”
- “日志里大部分是绿的”
- “dry-run 成功”
- “自动重试之后成功”

等价为正式 PASS。

---

# 2. 顶层不变量

无论做何种提效，以下约束保持不变。

## 2.1 测试集合不因提效而缩水

Codex MUST 保留与真实生产回归直接相关的：

- 正向路径；
- 负向路径；
- race / concurrency；
- failure / restart；
- absence guard；
- migration / compatibility；
- 必需的 integration / e2e；
- 正式 coverage evaluator；
- 正式 expected-skip policy。

提效的目标是**减少无意义重复执行**，而不是减少正式需要执行的测试。

## 2.2 不得通过弱化断言取得 GREEN

Codex MUST NOT 为了使门禁通过而：

- 删除断言；
- 放宽 mock，使其偏离真实生产条件；
- 清除 failure marker；
- 覆盖旧失败日志；
- 修改测试使失败路径不再可达；
- 用过滤后结果替代完整 Candidate；
- 用自动重试“洗掉”第一次失败；
- 把 0-test 当作成功。

## 2.3 Evidence 必须绑定真实输入

任何 receipt / seal / PASS 结论 SHOULD 绑定：

- source identity；
- generated bytes；
- canonical contract；
- runner/verifier bytes；
- toolchain；
- build key；
- test selection policy；
- discovered / selected / observed count；
- freshness 条件；
- artifact identity；
- 终态。

如果这些输入发生变化，Codex MUST 重新计算失效范围。

## 2.4 Candidate 不等于 Release

本地 Candidate PASS MUST NOT 自动升级为 Release PASS。

如果 Release 所需的 provider、签名、真实设备、部署权限、control-plane readback、备份/回滚或其他正式外部条件缺失，状态必须保持：

`BLOCKED` 或 `UNVERIFIED`

不得使用 fake provider、静态配置检查、dry-run 或本地模拟替代真实 Release 证明。

---

# 3. 核心术语

## 3.1 Stage

一个具有独立输入、命令、环境、输出和完成合同的测试/验证单元。

示例类别：

- Fast；
- integration；
- race；
- contract audit；
- Host/Static；
- Candidate；
- verifier；
- Release readback。

## 3.2 Attempt

某个 stage 的一次完整执行实例。

每个 attempt MUST：

- 有唯一 ID；
- 使用唯一 artifact namespace；
- 不覆盖旧 attempt；
- 记录输入闭集；
- 记录实际命令和环境；
- 保留 PASS 或 FAIL 终态。

## 3.3 输入闭集（Input Closure）

决定某份 evidence 是否仍然有效的全部输入集合。

典型输入包括：

```text
source identity
generated bytes
canonical contract
peer lock
fixtures
test / selection / coverage policy
runner bytes
verifier bytes
toolchain
dependency resolution
destination
build key
runtime identity
freshness contract
artifact root
external trust inputs
```

Codex SHOULD 把“是否需要重跑”转化为：

> 新旧 attempt 的输入闭集是否在当前 stage 的依赖边上发生变化？

而不是：

> “这个仓看起来跟它没关系。”

## 3.4 Artifact

正式执行产生的测试证据。

成功封存后：

- MUST 只读；
- MUST NOT 被 cache 覆盖；
- MUST NOT 被后续 attempt 改写；
- SHOULD 可由独立 verifier 复算。

## 3.5 Cache

仅用于加速。

Cache：

- MAY 复用；
- MUST NOT 冒充 artifact；
- MUST NOT 被当作 PASS 证明；
- SHOULD 记录 provenance 和 integrity。

## 3.6 Seal

对 artifact 内容和必要 metadata 的不可变证明。

必须明确区分：

- content seal；
- identity seal；
- owner/mode 等 metadata；
- terminal state；
- external receipt。

不要把某一种 seal 的证明边界扩大成另一种。

## 3.7 Receipt

对某次 stage 或 join 结果的外置记录。

Receipt SHOULD 至少包含：

```yaml
attempt_id: ...
stage: ...
status: PASS | FAIL | BLOCKED | UNVERIFIED

source_identity: ...
input_manifest_hash: ...
runner_hash: ...
verifier_hash: ...
toolchain: ...
command: ...
environment_hash: ...

discovered_tests: ...
selected_tests: ...
observed_tests: ...

artifact_identity: ...
terminal_state: ...
created_at: ...
```

字段名可按项目调整，但语义不能缺失。

---

# 4. 四层执行模型

Codex SHOULD 将测试执行分为：

```text
Fast -> Impacted -> Candidate -> Release
```

四层回答不同问题，不代表越往前测试标准越低。

---

## 4.1 Fast

目的：开发过程中的低成本快速反馈。

### Codex SHOULD

- 运行变更 package / target / test class；
- 并发相关变更同步运行对应 race；
- contract 变化立即运行 codegen / compile / no-diff；
- schema / migration 变化触发严格迁移链；
- 失败后先保存 RED 证据，再做最小复现；
- 默认不启动昂贵模拟器、真实设备或重型 runtime。

### Codex MUST NOT

- 把 Fast PASS 当成 Candidate PASS；
- 用 targeted test 替代之后正式要求的 full execution。

---

## 4.2 Impacted

目的：沿真实消费者图扩展影响面。

影响关系 SHOULD 来自：

```text
canonical source
  -> generated output
  -> production caller
  -> runtime owner
  -> peer / lock
  -> dependent stage
```

### Codex SHOULD

- 依据依赖边扩展，而不是依据仓库名粗放扩展；
- 覆盖所有真实镜像、变体和消费者；
- 使用本次 discovery 的实际测试集合；
- 只并行完全只读、无共享资源的任务。

### Codex MUST NOT

- 因“某个仓变了”直接假设所有独立 stage 都失效；
- 用历史硬编码测试数量替代本次 discovery。

---

## 4.3 Candidate

目的：对**冻结后的最终树**产生完整候选证据。

Candidate 启动前，Codex MUST 先冻结：

- 产品源码；
- generated bytes；
- canonical contract；
- test policy；
- runner；
- build config；
- 关键 toolchain identity。

Candidate 运行期间，如果上述字节或正式 identity 发生变化，Codex SHOULD：

1. 停止继续扩大长任务；
2. 保存当前 attempt；
3. 重新计算失效范围；
4. 对真实失效 stage 创建新 attempt。

不要边跑长任务边继续改同一证据闭集中的输入。

---

## 4.4 Release

Release 是独立层。

Release MAY 消费 Candidate 的有效 evidence，但必须额外验证所有 Release-only 外部条件。

Codex MUST 把：

```text
Candidate PASS
```

和：

```text
Release PASS
```

作为两个不同状态。

---

# 5. 推荐关键路径

冻结最终树后，默认推荐顺序：

```text
0. zero-cost preflight
1. backend / upstream full gates
2. lightweight isolated prepare
3. final contract audit
4. Host / Static
5. expensive full Candidate
6. independent verifier
7. review / manifest join
8. Release-only checks
```

选择顺序的原则：

1. **越上游越先。**
2. **越便宜越先。**
3. **越可能要求改代码越先。**
4. **占用共享重资源的阶段越后。**
5. **小时级任务之前必须尽量清空分钟级失败风险。**

Codex 的优化目标不是“同时跑最多任务”，而是：

```text
minimize expected wall-clock time
while preserving evidence correctness
```

---

# 6. Zero-cost / Low-cost Preflight

在任何长阶段启动前，Codex SHOULD 一次性检查：

- source identity 是否冻结；
- checkout 是否 clean；
- input manifest 是否可生成；
- 必需 external receipt 是否存在且未过期；
- strict baseline 是否可信；
- toolchain 是否满足 policy；
- runtime / destination 是否可用；
- artifact root 是否 fresh；
- cache root 是否符合该 stage 合同；
- disk space 是否满足门槛；
- mutex / owner 是否唯一；
- 是否存在残留进程或句柄；
- symlink / special node 是否违反路径策略；
- 签名或 Release-only 凭据是否满足当前层要求。

如果这些条件已知会阻断后续 stage，Codex MUST 在启动数小时任务之前报告 BLOCKED/FAIL。

---

# 7. 输入闭集构建规则

每个 stage SHOULD 有一个显式 manifest。

建议结构：

```yaml
stage: candidate-mobile
attempt_id: <unique>

source:
  repositories:
    - identity: <commit-or-tree>
      clean: true

contracts:
  canonical_hash: <sha256>
  generated_hashes:
    - <sha256>
  peer_lock_hash: <sha256>

policy:
  test_selection_hash: <sha256>
  coverage_policy_hash: <sha256>
  expected_skip_hash: <sha256>

runner:
  hash: <sha256>

toolchain:
  compiler: <identity>
  sdk: <identity>
  runtime: <identity>
  package_resolution: <identity>

execution:
  destination: <identity>
  worker_count: <value>
  build_key: <hash>
  freshness: <contract>

paths:
  artifact_root: <absolute-or-stable-identity>
  cache_root: <absolute-or-stable-identity>
```

项目可以增加字段，但不要删除真正影响结果的输入。

---

# 8. Build Key

Build 只有在完整 build key 不变时才可复用。

Build key SHOULD 至少覆盖：

```text
exact source bytes
generated bytes
compiler / SDK
runtime
device type
test plan
build configuration
dependency resolution
relevant feature flags
```

Codex MUST NOT 因“源码看起来没变”就复用 build。

并且：

> build 可复用 != runtime 状态可复用

即使 build key 完全相同，若 Candidate 合同要求 fresh runtime / fresh simulator / fresh artifact root，则这些 freshness 条件仍必须执行。

---

# 9. 失效计算

## 9.1 基本算法

Codex SHOULD 使用以下逻辑：

```text
for each changed_input:
    affected_stages += dependency_graph.consumers(changed_input)

for each stage:
    if stage in affected_stages:
        rerun(stage)
    else if existing_artifact_is_sealed(stage):
        readonly_reverify(stage)
    else:
        run(stage)
```

关键点：

- “有变化”不等于“全部失效”；
- “未变化”也不等于“可直接相信旧日志”；
- 未失效的 evidence 应走 **readonly reverify**；
- 失效范围必须有可解释理由。

---

## 9.2 常见失效矩阵

| 变化 | 必须执行 | 可保留 evidence |
|---|---|---|
| 普通说明文档变化，且不是正式 source identity | 文档/diff/hash 检查 | 不受影响产品测试和 sealed artifact |
| review manifest source 变化 | manifest 重建、生成器测试、状态断言 | 输入闭集未变的产品测试 |
| verifier 变化 | verifier 自测 + 用新 verifier 只读复算 | sealed artifact 内原始测试结果 |
| stage runner / harness 变化 | runner 自测 + 该 stage 完整重跑 + 下游 join | 不消费该 runner 的独立 stage |
| 无关仓 identity 变化 | identity / peer 检查 + 新 join receipt | 输入闭集未变的目标 artifact |
| 产品源码变化 | 对应 Fast / Impacted / Candidate | 无依赖边的独立 artifact |
| generated bytes 变化 | 所有消费者 + contract audit | 不消费该 generated output 的独立 artifact |
| test plan / selection policy 变化 | 相关完整 Candidate | 与该 policy 无关的独立 evidence |
| coverage policy 变化 | 受影响 evaluator/stage 与下游 join | 不消费该 policy 的独立 evidence |
| schema / migration 变化 | strict migration、integration/e2e/drills、消费者 | 不消费 schema 的独立 stage |
| toolchain / SDK / runtime 变化 | 所有消费该工具链的 stage | 不消费该工具链的 stage |
| provider / deployed version 变化 | 当前 Release readback / promotion / rollback | 本地 Candidate |

---

# 10. Read-only Reverification

只读复验适用于：

- 原 stage 已 PASS；
- artifact 已 sealed；
- artifact 输入闭集未变化；
- 变化发生在外层 identity、join、verifier 或其他不属于该 stage 输入闭集的位置。

只读复验 SHOULD：

1. 不修改原 artifact；
2. 重新计算 artifact manifest；
3. 校验 seal；
4. 校验 terminal state；
5. 校验当前 source / peer / join identity；
6. 生成新的外置 receipt；
7. 保留旧 receipt 作为历史事实。

Codex MUST NOT 用 readonly verifier 把：

- 失败 child；
- 未执行 child；
- 输入已经变化的 child；

转换成 PASS。

---

# 11. Artifact、Cache、Build 的边界

| 对象 | 目的 | 是否可修改 | 是否可作为 PASS 证据 |
|---|---|---:|---:|
| Artifact | 测试证据 | sealed 后否 | 是 |
| Cache | 性能加速 | 是 | 否 |
| Build | 可执行测试产物 | 条件式复用 | 单独不能 |
| Receipt | 对 evidence 的结论记录 | no-clobber | 是，但必须绑定 evidence |

Codex SHOULD 始终把 artifact 和 cache 放在不同 namespace。

要求 fresh 的 stage：

- MUST 先证明目标路径不存在；
- MUST NOT 偷用旧 artifact root；
- MAY 按合同使用允许的 dependency cache；
- MUST 记录 cache provenance。

---

# 12. 并发策略

并发只能用于真正独立的工作。

## 12.1 默认允许并发

- sealed artifact 只读盘点；
- manifest hash 复算；
- documentation / checklist audit；
- 完全独立的静态分析；
- 不共享 runtime、数据库、codegen owner、mutex 的任务。

## 12.2 默认必须串行

- shared codegen；
- database reset；
- 单 simulator / device；
- Xcode / device test owner；
- 同一 mutex；
- 会修改同一 runtime 的阶段；
- 任何存在确定性冲突的任务。

## 12.3 并发退化规则

如果观察到并发导致：

- CPU contention；
- disk contention；
- memory pressure；
- wall-clock 增加；
- flaky evidence；
- shared-state 不确定；

Codex SHOULD 降低并发甚至改为串行。

衡量指标是：

```text
wall-clock + evidence determinism
```

而不是 worker 数量。

---

# 13. 长阶段启动合同

每个长阶段启动前，Codex MUST 检查：

```text
[ ] source identity frozen
[ ] repository clean
[ ] input closure materialized
[ ] runner hash frozen
[ ] toolchain identity frozen
[ ] artifact root fresh
[ ] required cache policy satisfied
[ ] disk threshold satisfied
[ ] required lock uniquely held
[ ] conflicting process absent
[ ] conflicting handle absent
[ ] runtime destination correct
[ ] external prerequisite present or stage marked BLOCKED
```

任何失败都应在启动昂贵测试前暴露。

---

# 14. 失败处理

## 14.1 第一原则：先保存 RED

失败发生后，Codex MUST 先保存：

- attempt ID；
- source identity；
- runner hash；
- exact command；
- relevant environment；
- seed；
- terminal output；
- step/meta；
- failure marker；
- artifact root。

然后才能进行诊断。

## 14.2 失败分类

SHOULD 分类为：

```text
production
test-harness
tooling
environment
disk-resource
flaky-evidence
external-blocker
```

分类必须有事实支撑，不要仅凭猜测。

## 14.3 恢复顺序

推荐：

```text
1. minimal reproduce
2. minimal real fix
3. Fast
4. nearest Impacted
5. invalidated Candidate stage
6. readonly reverify unaffected sealed stages
7. rebuild join / receipt
```

不要默认：

```text
failure -> rerun everything
```

---

# 15. 正式 Release 的特殊恢复规则

诊断阶段 MAY 定向补跑。

但如果正式 Release orchestrator 的合同要求：

- fresh gate ID；
- fresh root；
- 完整从头执行；
- 不导入旧 child receipt；

Codex MUST 遵守该正式合同。

局部修复结果只能帮助：

- 定位；
- 恢复 Fast / Impacted；
- 判断是否值得重新开始 Release。

不能把局部 child PASS 手工拼成正式 Release PASS。

---

# 16. 清理规则

Codex MUST 优先保存 evidence，而不是优先清理。

只允许清理：

- checkout 外；
- 明确 owned by current task；
- 可以精确重建；
- 未被 seal / receipt 引用；
- realpath/device/inode/owner 已验证；
- 无相关进程和句柄。

Codex SHOULD NOT 使用宽泛 glob。

Codex MUST NOT 为了“让环境重新变绿”而删除失败证据。

---

# 17. Stage 完成合同

一个 stage 只有满足全部合同才可标记 PASS。

## 17.1 Start

- fresh path 满足；
- source/runner/toolchain/hash 冻结；
- repo clean；
- 资源门槛满足；
- lock 唯一。

## 17.2 Run

- exact command 可回放；
- closed environment 可解释；
- discovered / selected / observed count 有记录；
- 正式 selection policy 被执行。

## 17.3 End

- exit code 满足合同；
- 无非预期 failed；
- 无非预期 skipped；
- 无 0-test 异常；
- postflight 成为最终状态。

## 17.4 Coverage

如果项目要求 coverage gate：

- MUST 使用正式 threshold evaluator；
- raw coverage 不能冒充 threshold PASS；
- expected skip 必须符合正式 allowlist / exclusion policy；
- 过期 skip 不能自动接受。

## 17.5 Seal

- manifest 可复算；
- sealed artifact 不可改写；
- receipt no-clobber；
- content 与 metadata 的证明边界明确。

## 17.6 Postflight

- 无残留相关进程/句柄；
- runtime 回到要求状态；
- repo 仍是 exact identity 且 clean。

## 17.7 Closure

Codex 只能关闭当前 stage 明确证明的事项。

以下状态不得自动传递：

```text
test PASS -> deploy PASS
candidate PASS -> release PASS
neighbor finding PASS -> current task PASS
```

---

# 18. Codex 默认决策流程

当接到一个“跑完整测试 / 验证候选 / 修复后重新跑 / 继续长任务”的请求时，Codex SHOULD 按以下顺序推理和执行。

```text
A. Identify requested final state
   |
   v
B. Read current source identity and formal policies
   |
   v
C. Build changed-input set
   |
   v
D. Build / load stage dependency graph
   |
   v
E. Compute invalidated stages
   |
   +--> existing sealed + not invalidated -> readonly reverify
   |
   +--> invalidated -> rerun
   |
   v
F. Run zero-cost preflight
   |
   +--> blocker -> BLOCKED / FAIL early
   |
   v
G. Execute cheap upstream stages first
   |
   v
H. Execute expensive shared-resource stages last
   |
   v
I. Seal artifacts
   |
   v
J. Independent verification
   |
   v
K. Join evidence for current identity
   |
   v
L. Stop at Candidate unless Release prerequisites are actually available
```

---

# 19. Codex 输出要求

执行过程中，Codex SHOULD 明确报告：

## 19.1 Before execution

```text
Goal:
Frozen inputs:
Changed inputs:
Invalidated stages:
Reusable sealed stages:
Planned order:
Known blockers:
```

## 19.2 After each stage

```text
Stage:
Attempt:
Status:
Input manifest:
Command:
Observed test count:
Artifact:
Seal:
Reason for rerun/reuse:
Next stage:
```

## 19.3 Final summary

最终总结至少包含：

```text
PASS:
FAIL:
BLOCKED:
UNVERIFIED:
Reused sealed evidence:
Rerun stages:
Release status:
Remaining external prerequisites:
```

避免只输出“全部测试通过”。

---

# 20. 禁止模式

Codex MUST NOT 采用以下模式。

## 20.1 Blind full rerun

```text
任何变化 -> 所有长任务全部重跑
```

除非正式 orchestrator 明确要求完整从头执行。

## 20.2 Blind reuse

```text
之前绿过 -> 直接复用
```

必须先比较输入闭集。

## 20.3 Repository-name reasoning

```text
另一个仓变了 -> 跟当前 stage 无关
```

或：

```text
同一个仓变了 -> 当前 stage 一定失效
```

两者都不可靠。必须看依赖边。

## 20.4 Log-based PASS

```text
console 看起来都是 green -> PASS
```

正式状态必须来自 stage completion contract。

## 20.5 Retry laundering

第一次正式执行失败后，不得自动重试直到出现 GREEN，再只保留最后一次。

## 20.6 Evidence clobber

不得覆盖：

- 失败 artifact；
- terminal；
- marker；
- receipt；
- 原 attempt namespace。

## 20.7 Release substitution

不得用：

- dry-run；
- fake provider；
- static config；
- local-only probe；

代替正式要求的真实 Release 证明。

---

# 21. 推荐机器化能力

长期维护时，优先建设以下三个组件。

## 21.1 Input Closure Generator

输入：

```text
stage + current repo state + policies + toolchain
```

输出：

```text
stable machine-comparable manifest
```

目标：让“这份 evidence 证明了什么”可机器回答。

## 21.2 Invalidation Calculator

输入：

```text
old manifest
new manifest
dependency graph
```

输出：

```yaml
rerun:
  - stage-a
readonly_reverify:
  - stage-b
unchanged:
  - stage-c
reasons:
  stage-a:
    - test_selection_hash changed
```

目标：让“要不要重跑”摆脱人工经验。

## 21.3 Independent Verifier

必须与 runner 解耦。

负责：

- 重算 manifest；
- 校验 artifact；
- 校验 seal；
- 校验 identity；
- 校验 terminal；
- 生成 receipt。

目标：避免“执行成功”和“证明执行成功”耦合。

---

# 22. 建议的 Stage 配置接口

项目可以为每个 stage 定义类似配置：

```yaml
name: mobile-candidate

depends_on:
  - contract-audit
  - host-static

consumes:
  - source.mobile
  - generated.shared
  - contract.canonical
  - policy.test-selection
  - policy.coverage
  - toolchain.mobile
  - runtime.simulator

freshness:
  artifact_root: required
  runtime_reset: required
  cache: policy-controlled

resources:
  mutex:
    - simulator-owner
  exclusive:
    - device-runtime

execution:
  filtered: false
  worker_policy: fixed
  discovery: required

evidence:
  seal: required
  readonly_verifier: required
```

这样 Codex 可以根据配置而不是自然语言猜测行为。

---

# 23. 建议的 Attempt 目录结构

示意：

```text
attempts/
  <attempt-id>/
    input/
      manifest.json
      command.txt
      environment.json
    logs/
      stdout.log
      stderr.log
    artifact/
      ...
    state/
      terminal.json
      failure.marker
    seal/
      artifact.manifest
      artifact.seal

receipts/
  <attempt-id>.json
```

要求：

- 新 attempt 新目录；
- sealed 后只读；
- cache 放在独立路径；
- receipt 外置；
- 失败 attempt 不覆盖。

---

# 24. 最小执行伪代码

```python
def execute_long_validation(change_set, target_state):
    policies = load_formal_policies()
    current = freeze_current_inputs(policies)

    stages = load_stage_graph()
    invalidated = compute_invalidation(
        previous_evidence=find_previous_evidence(),
        current_inputs=current,
        change_set=change_set,
        graph=stages,
    )

    preflight = run_preflight(current, target_state)
    if not preflight.ok:
        return blocked_or_failed(preflight)

    for stage in order_by_cost_and_upstream(stages, target_state):
        if stage not in invalidated:
            old = find_sealed_evidence(stage)
            if old and input_closure_matches(old, current, stage):
                readonly_reverify(old, current)
                continue

        attempt = create_fresh_attempt(stage, current)

        result = run_stage(attempt)

        if not result.pass_contract:
            preserve_red(attempt)
            return fail_with_recovery_scope(
                stage=stage,
                invalidated=compute_followup_scope(result, current),
            )

        seal(attempt)
        verify_independently(attempt)

    return join_current_identity_and_report()
```

此伪代码表达的是控制逻辑，不替代项目正式 runner。

---

# 25. 任务恢复模板

当 Codex 接手一个已经失败或被中断的长任务时，先执行：

```text
1. 找到最后一个完整 attempt。
2. 不修改旧 attempt。
3. 读取其 input manifest / terminal / artifact / seal。
4. 获取当前 source / policy / runner / toolchain identity。
5. 比较输入闭集。
6. 标记：
   - still-valid sealed stages
   - invalidated stages
   - missing stages
   - failed stages
7. 对 still-valid 执行 readonly reverify。
8. 对 invalidated / missing / failed 建新 attempt。
9. 按便宜上游优先重新执行。
10. 重新 join 当前 identity。
```

不要把“继续任务”理解为“从日志最后一行继续跑”。

恢复的对象是**证据图**，不是 shell session。

---

# 26. 最终检查清单

Codex 在宣布工作完成前，应检查：

```text
[ ] 没有因为提效减少正式测试集合
[ ] 没有放宽断言或 mock
[ ] 没有隐藏第一次失败
[ ] 每个 PASS 都绑定明确输入
[ ] 每个 reuse 都经过输入闭集比较
[ ] 未失效 artifact 只读复验
[ ] 失效 stage 使用新 attempt
[ ] artifact 与 cache 已分离
[ ] fresh stage 真的满足 freshness
[ ] shared resource 没有不安全并发
[ ] coverage 使用正式 evaluator
[ ] expected skip 符合正式 policy
[ ] 0-test 未被当成成功
[ ] failed artifact 已保留
[ ] Candidate 和 Release 状态未混淆
[ ] 缺少外部 Release 输入时状态为 BLOCKED/UNVERIFIED
[ ] 最终 receipt 对应当前 source identity
```

---

# 27. 一句话执行原则

> **先证明什么真的失效，再决定跑什么；先跑便宜上游，再跑昂贵下游；已经 sealed 且输入未变的结果只读复验，不盲目重跑；任何提效都不能突破正式测试、freshness、coverage、evidence 和 Release 边界。**

---

## 附录 A：给 Codex 的最短指令版

如果上下文很长，可将下面一段作为运行时摘要：

```text
执行长任务时不要默认全量重跑。

先冻结当前 source / generated / contract / policy / runner / toolchain / build / freshness 输入，生成输入闭集；与已有 sealed evidence 做机器比较，计算真正失效的 stage。

未失效且已 sealed 的 stage 只读复验并生成当前 identity 的新 receipt；失效或未执行的 stage 使用新 attempt / fresh artifact root 重跑。

执行顺序遵守“零成本预检 -> 便宜上游 -> contract -> host/static -> 最昂贵 Candidate -> independent verifier -> join”。共享 codegen、database reset、simulator/device 等保持串行。

失败先保存 RED artifact、terminal、source/runner hash 和 marker，再做最小复现；不得覆盖失败证据、放宽断言、过滤完整 Candidate、用自动重试洗绿或把 0-test 当成功。

Artifact 是证据，Cache 只是加速；build 可复用不代表 runtime freshness 可复用。

Candidate PASS 不等于 Release PASS；缺 provider / signing / device / deployment permission / control-plane readback 等正式外部条件时保持 BLOCKED/UNVERIFIED。

最终报告必须说明：哪些 stage 重跑、哪些 sealed evidence 被复验、为什么、当前 PASS/FAIL/BLOCKED/UNVERIFIED 状态，以及 Release 是否真正被证明。
```
