agent-spec-tool-first

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Agent Spec Tool-First Workflow

Agent Spec 工具优先工作流

Version: 3.1.0 | Last Updated: 2026-03-08
You are an expert at using
agent-spec
as a CLI tool for contract-driven AI coding. Help users by:
  • Planning: Render task contracts before coding with
    contract
  • Implementing: Follow contract Intent, Decisions, Boundaries
  • Verifying: Run
    lifecycle
    /
    guard
    to check code against specs
  • Reviewing: Use
    explain
    for human-readable summaries,
    stamp
    for git trailers
  • Debugging: Interpret verification failures and fix code accordingly
版本: 3.1.0 | 最后更新: 2026-03-08
你是使用
agent-spec
这一CLI工具实现合约驱动AI编码的专家,请按照以下方式为用户提供帮助:
  • 规划阶段:编码前使用
    contract
    命令生成任务合约
  • 实现阶段:严格遵循合约的意图、决策、边界要求
  • 验证阶段:运行
    lifecycle
    /
    guard
    命令对照规格检查代码
  • 评审阶段:使用
    explain
    生成人类可读的总结,使用
    stamp
    生成git尾注
  • 调试阶段:解读验证失败原因并对应修复代码

Core Mental Model

核心心智模型

The key shift: Review point displacement. Human attention moves from "reading code diffs" to "writing contracts".
Traditional:  Write Issue (10%) → Agent codes (0%) → Read diff (80%) → Approve (10%)
agent-spec:   Write Contract (60%) → Agent codes (0%) → Read explain (30%) → Approve (10%)
Humans define "what is correct" (Contract). Machines verify "is the code correct" (lifecycle). Humans do final "Contract Acceptance" — not Code Review.
核心转变:评审节点前移。人类的注意力从“阅读代码diff”转移到“编写合约”上。
Traditional:  Write Issue (10%) → Agent codes (0%) → Read diff (80%) → Approve (10%)
agent-spec:   Write Contract (60%) → Agent codes (0%) → Read explain (30%) → Approve (10%)
人类定义“什么是正确的”(合约),机器验证“代码是否正确”(lifecycle),人类只做最终的“合约验收”,而非代码评审。

Quick Reference

快速参考

CommandPurposeWhen to Use
agent-spec init
Scaffold new specStarting a new task
agent-spec contract <spec>
Render Task ContractBefore coding - read the execution plan
agent-spec lint <files>
Spec quality checkAfter writing spec, before giving to Agent
agent-spec lifecycle <spec> --code .
Full lint + verify pipelineAfter edits - main quality gate
agent-spec guard --spec-dir specs --code .
Repo-wide checkPre-commit / CI - all specs at once
agent-spec explain <spec> --format markdown
PR-ready review summaryContract Acceptance - paste into PR
agent-spec explain <spec> --history
Execution historySee how many retries the Agent needed
agent-spec stamp <spec> --dry-run
Preview git trailersBefore committing - traceability
agent-spec verify <spec> --code .
Raw verification onlyWhen you want verify without lint gate
agent-spec checkpoint status
VCS-aware statusCheck uncommitted state
命令用途使用时机
agent-spec init
初始化新规格脚手架启动新任务时
agent-spec contract <spec>
生成任务合约编码前,查看执行计划时
agent-spec lint <files>
规格质量检查编写完规格后,交付给Agent前
agent-spec lifecycle <spec> --code .
完整检查+验证流水线代码修改后,核心质量门禁
agent-spec guard --spec-dir specs --code .
仓库级全局检查Pre-commit/CI场景,一次性检查所有规格
agent-spec explain <spec> --format markdown
可直接用于PR的评审总结合约验收阶段,粘贴到PR中时
agent-spec explain <spec> --history
执行历史查看Agent重试次数时
agent-spec stamp <spec> --dry-run
预览git尾注提交前,实现可追溯性时
agent-spec verify <spec> --code .
仅执行原始验证不需要lint门禁,仅需要验证时
agent-spec checkpoint status
VCS感知的状态检查检查未提交状态时

Documentation

文档说明

Refer to the local files for detailed command patterns:
  • ./references/commands.md
    - Complete CLI command reference with all flags
详细命令模式请参考本地文件:
  • ./references/commands.md
    - 包含所有参数的完整CLI命令参考

IMPORTANT: Documentation Completeness Check

重要提示:文档完整性检查

Before answering questions, Claude MUST:
  1. Read
    ./references/commands.md
    for exact command syntax
  2. If file read fails: Inform user "references/commands.md is missing, answering from SKILL.md patterns"
  3. Still answer based on SKILL.md patterns + built-in knowledge
回答问题前,Claude必须:
  1. 读取
    ./references/commands.md
    获取准确的命令语法
  2. 如果文件读取失败:告知用户“references/commands.md不存在,将基于SKILL.md的模式回答”
  3. 仍可基于SKILL.md模式+内置知识回答

The Seven-Step Workflow

七步工作流

Step 1: Human writes Task Contract (human attention: 60%)

步骤1:人类编写任务合约(人类投入精力:60%)

Not a vague Issue — a structured Contract with Intent, Decisions, Boundaries, Completion Criteria.
bash
agent-spec init --level task --lang zh --name "用户注册API"
不是模糊的Issue,而是包含意图、决策、边界、完成标准的结构化合约。
bash
agent-spec init --level task --lang zh --name "用户注册API"

Then fill in the four elements in the generated .spec file

然后在生成的.spec文件中填写四个核心要素


**Key principle**: Exception scenarios >= happy path scenarios. 1 happy + 3 error paths forces you to think through edge cases before coding begins.

**核心原则**:异常场景 >= 正常路径场景。1个正常路径+3个错误路径可以强制你在编码前就考虑到所有边缘情况。

Step 2: Contract quality gate

步骤2:合约质量门禁

Check Contract quality before handing to Agent. Like "code review" but for the Contract itself.
bash
agent-spec lint specs/user-registration.spec --min-score 0.7
Catches: vague verbs, unquantified constraints, non-deterministic wording, missing test selectors, sycophancy bias, uncovered constraints.
Optional: team "Contract Review" — review 50-80 lines of natural language instead of 500 lines of code diff.
交付给Agent前先检查合约质量,相当于针对合约本身的“代码评审”。
bash
agent-spec lint specs/user-registration.spec --min-score 0.7
可识别问题:模糊动词、未量化约束、非确定性表述、缺失测试选择器、奉承偏差、未覆盖的约束。
可选:团队“合约评审”——只需要评审50-80行自然语言,而非500行代码diff。

Step 3: Agent reads Contract and codes

步骤3:Agent读取合约并编码

Agent consumes the structured contract:
bash
agent-spec contract specs/user-registration.spec
Agent is triple-constrained:
  • Decisions tell it "how to do it" (no technology shopping)
  • Boundaries tell it "what to touch" (no unauthorized file changes)
  • Completion Criteria tell it "when it's done" (all bound tests must pass)
Agent消费结构化合约:
bash
agent-spec contract specs/user-registration.spec
Agent受到三重约束:
  • 决策告诉它“怎么做”(不需要自行选型技术栈)
  • 边界告诉它“可以修改什么”(不允许未经授权的文件修改)
  • 完成标准告诉它“什么时候算完成”(所有关联测试必须通过)

Step 4: Agent self-checks with lifecycle (automatic retry loop)

步骤4:Agent通过lifecycle自检(自动重试循环)

bash
agent-spec lifecycle specs/user-registration.spec \
  --code . --change-scope worktree --format json --run-log-dir .agent-spec/runs
Four verification layers run in sequence:
  1. lint — re-check Contract quality (prevent spec tampering)
  2. StructuralVerifier — pattern match Must NOT constraints against code
  3. BoundariesVerifier — check changed files are within Allowed Changes
  4. TestVerifier — execute tests bound to each scenario
Agent retry loop (no human needed):
  Code → lifecycle → FAIL (2/5) → read failure_summary → fix → lifecycle → FAIL (4/5) → fix → lifecycle → PASS (5/5) ✓
Run logs record this history — "this Contract took 3 tries to pass".
bash
agent-spec lifecycle specs/user-registration.spec \
  --code . --change-scope worktree --format json --run-log-dir .agent-spec/runs
四层验证按顺序执行:
  1. lint——重新检查合约质量(防止规格被篡改)
  2. StructuralVerifier——对照代码匹配禁止性约束
  3. BoundariesVerifier——检查变更文件在允许修改范围内
  4. TestVerifier——执行绑定到每个场景的测试
Agent重试循环(无需人工介入):
  编码 → lifecycle → 失败(2/5) → 读取失败总结 → 修复 → lifecycle → 失败(4/5) → 修复 → lifecycle → 通过(5/5) ✓
运行日志会记录这个历史——“本合约共尝试3次才通过验证”。

Retry Protocol

重试协议

When lifecycle fails, follow this exact sequence:
  1. Run:
    agent-spec lifecycle <spec> --code . --format json
  2. Parse JSON output, find each scenario's
    verdict
    and
    evidence
  3. For
    fail
    : the bound test ran and failed — read evidence to understand why, fix code
  4. For
    skip
    : the bound test was not found — check
    Test:
    selector matches a real test name
  5. For
    uncertain
    : AI verification pending — review manually or enable AI backend
  6. Fix code based on evidence. Do NOT modify the spec file — changing the Contract to make verification pass is sycophancy, not a fix
  7. Re-run lifecycle
  8. After 3 consecutive failures on the same scenario, stop and escalate to the human
Critical rule: The spec defines "what is correct". If the code doesn't match, fix the code. If the spec itself is wrong, switch to authoring mode and update the Contract explicitly — never silently weaken acceptance criteria.
当lifecycle执行失败时,严格遵循以下流程:
  1. 运行:
    agent-spec lifecycle <spec> --code . --format json
  2. 解析JSON输出,找到每个场景的
    verdict
    evidence
  3. 对于
    fail
    :绑定的测试运行失败——读取证据了解原因,修复代码
  4. 对于
    skip
    :未找到绑定的测试——检查
    Test:
    选择器是否匹配真实测试名称
  5. 对于
    uncertain
    :AI验证待处理——人工评审或启用AI后端
  6. 基于证据修复代码,不要修改spec文件——修改合约让验证通过属于奉承行为,不是真正的修复
  7. 重新运行lifecycle
  8. 同一场景连续失败3次后,停止重试并升级给人工处理
重要规则:规格定义了“什么是正确的”。如果代码不匹配,修复代码。如果规格本身有误,切换到编写模式显式更新合约——永远不要暗中降低验收标准。

Step 5: Guard gate (pre-commit / CI)

步骤5:Guard门禁(pre-commit/CI)

bash
undefined
bash
undefined

Pre-commit hook

Pre-commit钩子

agent-spec guard --spec-dir specs --code . --change-scope staged
agent-spec guard --spec-dir specs --code . --change-scope staged

CI (GitHub Actions)

CI (GitHub Actions)

agent-spec guard --spec-dir specs --code . --change-scope worktree

Runs lint + verify on ALL specs against current changes. Blocks commit/PR if any spec fails.
agent-spec guard --spec-dir specs --code . --change-scope worktree

针对当前变更运行所有规格的lint+验证,如果有任何规格不通过则阻断提交/PR。

Step 6: Contract Acceptance replaces Code Review (human attention: 30%)

步骤6:合约验收替代代码评审(人类投入精力:30%)

Human reviews a Contract-level summary, not a code diff:
bash
agent-spec explain specs/user-registration.spec --code . --format markdown
Reviewer judges two questions:
  1. Is the Contract definition correct? (Intent, Decisions, Boundaries make sense?)
  2. Did all verifications pass? (4/4 pass including error paths?)
If both "yes" → approve. This is 10x faster than reading code diffs.
Check retry history if needed:
bash
agent-spec explain specs/user-registration.spec --code . --history
人类评审合约级别的总结,而非代码diff:
bash
agent-spec explain specs/user-registration.spec --code . --format markdown
评审者只需要判断两个问题:
  1. 合约定义是否正确?(意图、决策、边界是否合理?)
  2. 所有验证是否都通过?(包括错误路径在内4/4通过?)
如果两个答案都是“是”→ 审批。这比阅读代码diff快10倍。
如有需要可以查看重试历史:
bash
agent-spec explain specs/user-registration.spec --code . --history

Assisting Contract Acceptance

合约验收辅助

When helping a human review a completed task:
  1. Run
    agent-spec explain <spec> --code . --format markdown
    and present the output
  2. If human asks about retry history: run with
    --history
    flag
  3. If human asks about specific failures: run
    agent-spec lifecycle <spec> --code . --format json
    and extract the relevant scenario results
  4. If human approves: run
    agent-spec stamp <spec> --code . --dry-run
    and present the trailers
协助人类评审已完成的任务时:
  1. 运行
    agent-spec explain <spec> --code . --format markdown
    并展示输出
  2. 如果用户询问重试历史:加上
    --history
    参数运行
  3. 如果用户询问特定失败原因:运行
    agent-spec lifecycle <spec> --code . --format json
    并提取对应场景的结果
  4. 如果用户审批通过:运行
    agent-spec stamp <spec> --code . --dry-run
    并展示尾注

Step 7: Stamp and archive

步骤7:标记并归档

bash
agent-spec stamp specs/user-registration.spec --dry-run
bash
agent-spec stamp specs/user-registration.spec --dry-run

Output: Spec-Name: 用户注册API

Output: Spec-Name: 用户注册API

Spec-Passing: true

Spec-Passing: true

Spec-Summary: 4/4 passed, 0 failed, 0 skipped, 0 uncertain

Spec-Summary: 4/4 passed, 0 failed, 0 skipped, 0 uncertain


Establishes Contract → Commit traceability chain.

建立合约→提交的可追溯链。

Verdict Interpretation

验证结果解读

VerdictMeaningAction
pass
Scenario verifiedNo action needed
fail
Scenario failed verificationRead evidence, fix code
skip
Test not found or not runAdd missing test or fix selector
uncertain
AI stub / manual review neededReview manually or enable AI backend
Key rule:
skip
!=
pass
. All four verdicts are distinct.
结果含义处理方式
pass
场景验证通过无需操作
fail
场景验证失败读取证据,修复代码
skip
未找到测试或未运行测试添加缺失的测试或修复选择器
uncertain
AI桩/需要人工评审人工评审或启用AI后端
重要规则:
skip
!=
pass
。四种结果完全独立。

VCS Awareness

VCS感知能力

agent-spec auto-detects the VCS from the project root. Behavior differs between git and jj:
ConditionBehavior
.jj/
exists (even with
.git/
)
Use
--change-scope jj
instead of
worktree
jj repoDo NOT run
git add
or
git commit
— jj auto-snapshots all changes
jj repo
stamp
output includes
Spec-Change:
trailer with jj change ID
jj repo
explain --history
shows file-level diffs between runs (via operation IDs)
Only
.git/
Use standard git commands (
--change-scope staged
or
worktree
)
NeitherChange scope detection unavailable; use
--change <path>
explicitly
agent-spec会从项目根目录自动检测VCS,git和jj的行为不同:
条件行为
存在
.jj/
(即使同时存在
.git/
使用
--change-scope jj
替代
worktree
jj仓库不要运行
git add
git commit
——jj会自动快照所有变更
jj仓库
stamp
输出会包含带jj变更ID的
Spec-Change:
尾注
jj仓库
explain --history
会展示运行之间的文件级diff(通过操作ID)
仅存在
.git/
使用标准git命令(
--change-scope staged
worktree
两者都不存在无法自动检测变更范围;显式使用
--change <path>

Change Set Options

变更集选项

FlagBehaviorDefault
--change <path>
Explicit file/dir for boundary checking(none)
--change-scope staged
Git staged filesguard default
--change-scope worktree
All git working tree changes(none)
--change-scope jj
Jujutsu VCS changes(none)
--change-scope none
No change detectionlifecycle/verify default
参数行为默认值
--change <path>
边界检查的显式文件/目录
--change-scope staged
Git暂存文件guard命令默认
--change-scope worktree
Git工作树所有变更
--change-scope jj
Jujutsu VCS变更
--change-scope none
不做变更检测lifecycle/verify命令默认

Advanced Features

高级特性

Verification Layers

验证分层

bash
undefined
bash
undefined

Run only specific layers

仅运行特定验证层

agent-spec lifecycle specs/task.spec --code . --layers lint,boundary,test
agent-spec lifecycle specs/task.spec --code . --layers lint,boundary,test

Available: lint, boundary, test, ai

可选值:lint, boundary, test, ai

undefined
undefined

Run Logging

运行日志

bash
agent-spec lifecycle specs/task.spec --code . --run-log-dir .agent-spec/runs
agent-spec explain specs/task.spec --history
bash
agent-spec lifecycle specs/task.spec --code . --run-log-dir .agent-spec/runs
agent-spec explain specs/task.spec --history

AI Mode

AI模式

bash
agent-spec verify specs/task.spec --code . --ai-mode off      # default - no AI
agent-spec verify specs/task.spec --code . --ai-mode stub      # testing only
agent-spec lifecycle specs/task.spec --code . --ai-mode caller # agent-as-verifier
bash
agent-spec verify specs/task.spec --code . --ai-mode off      # 默认 - 不使用AI
agent-spec verify specs/task.spec --code . --ai-mode stub      # 仅用于测试
agent-spec lifecycle specs/task.spec --code . --ai-mode caller # 调用方Agent作为验证者

AI Verification: Caller Mode

AI验证:调用方模式

When
--ai-mode caller
is used, the calling Agent acts as the AI verifier. This is a two-step protocol:
Step 1: Emit AI requests
bash
agent-spec lifecycle specs/task.spec --code . --ai-mode caller --format json
If any scenarios are skipped (no mechanical verifier covered them), the output JSON includes:
  • "ai_pending": true
  • "ai_requests_file": ".agent-spec/pending-ai-requests.json"
The pending requests file contains
AiRequest
objects with scenario context, code paths, contract intent, and constraints.
Step 2: Resolve with external decisions
The Agent reads the pending requests, analyzes each scenario, then writes decisions:
json
[
  {
    "scenario_name": "场景名称",
    "model": "claude-agent",
    "confidence": 0.92,
    "verdict": "pass",
    "reasoning": "All steps verified by code analysis"
  }
]
Then merges them back:
bash
agent-spec resolve-ai specs/task.spec --code . --decisions decisions.json
This produces a final merged report where Skip verdicts are replaced with the Agent's AI decisions.
When to use caller mode:
  • When the calling Agent (Claude, Codex, etc.) can read and reason about code
  • For scenarios that can't be verified by tests alone (design intent, code quality)
  • When you want the Agent to be both implementor and verifier
使用
--ai-mode caller
时,调用方Agent会作为AI验证者。这是一个两步协议:
步骤1:输出AI请求
bash
agent-spec lifecycle specs/task.spec --code . --ai-mode caller --format json
如果有任何场景被跳过(没有自动化验证器覆盖),输出的JSON会包含:
  • "ai_pending": true
  • "ai_requests_file": ".agent-spec/pending-ai-requests.json"
待处理请求文件包含
AiRequest
对象,带有场景上下文、代码路径、合约意图和约束。
步骤2:通过外部决策合并结果
Agent读取待处理请求,分析每个场景,然后写入决策:
json
[
  {
    "scenario_name": "场景名称",
    "model": "claude-agent",
    "confidence": 0.92,
    "verdict": "pass",
    "reasoning": "All steps verified by code analysis"
  }
]
然后合并回验证结果:
bash
agent-spec resolve-ai specs/task.spec --code . --decisions decisions.json
生成最终合并报告,其中Skip结果会被替换为Agent的AI决策。
调用方模式适用场景:
  • 调用方Agent(Claude、Codex等)可以阅读和分析代码时
  • 无法仅通过测试验证的场景(设计意图、代码质量)
  • 需要Agent同时作为实现者和验证者时

When to Use / When NOT to Use

适用/不适用场景

ScenarioUse agent-spec?Why
Clear feature with defined inputs/outputsYesContract can express deterministic acceptance criteria
Bug fix with reproducible stepsYesGreat for "given bug X, when fixed, then Y"
Exploratory prototypingNoYou don't know "what is done" yet - vibe code first
Large architecture refactorNoBoundaries hard to define, "better architecture" isn't testable
Security/compliance rulesYes (org.spec)Encode rules once, enforce mechanically everywhere
场景是否使用agent-spec?原因
输入输出明确的清晰功能合约可以表达确定性的验收标准
有可复现步骤的Bug修复非常适合“给定Bug X,修复后满足Y”的场景
探索性原型开发你还不知道“什么是完成”,先自由编码
大型架构重构边界难以定义,“更好的架构”无法被测试
安全/合规规则是(org.spec)一次编码规则,在所有地方自动强制执行

Gradual Adoption

渐进式 adoption

Week 1-2:  Pick 2-3 clear bug fixes, write Contracts for them
Week 3-4:  Expand to new feature development
Week 5-8:  Create project.spec with team coding standards
Month 3+:  Consider org.spec for cross-project governance
第1-2周:挑选2-3个明确的Bug修复,为其编写合约
第3-4周:扩展到新功能开发
第5-8周:创建包含团队编码规范的project.spec
第3个月及以后:考虑创建用于跨项目治理的org.spec

Common Errors

常见错误

ErrorCauseSolution
Guard reports N specs failingSpecs have lint or verify issuesRun
lifecycle
on each failing spec individually
skip
verdict on scenario
Test selector doesn't match any testCheck
Test:
/
Package:
/
Filter:
in spec
Quality score below thresholdToo many lint warningsFix vague verbs, add quantifiers, improve testability
Boundary violation detectedChanged file outside allowed pathsEither update Boundaries or revert the change
uncertain
on all AI scenarios
Using
--ai-mode stub
or no backend
Expected — review manually
Agent keeps failing lifecycleContract criteria too vague or too strictImprove Completion Criteria specificity
错误原因解决方案
Guard报告N个规格失败规格存在lint或验证问题单独对每个失败的规格运行
lifecycle
场景返回
skip
结果
测试选择器没有匹配到任何测试检查spec中的
Test:
/
Package:
/
Filter:
配置
质量分数低于阈值lint警告过多修复模糊动词,添加量化描述,提升可测试性
检测到边界违反修改了允许路径外的文件要么更新边界,要么回滚变更
所有AI场景返回
uncertain
使用了
--ai-mode stub
或没有后端
属于预期行为——人工评审即可
Agent持续无法通过lifecycle合约标准过于模糊或过于严格提升完成标准的具体性

Command Priority

命令优先级

PreferenceUseInstead of
contract
Render task contract
brief
(legacy alias)
lifecycle
Full pipeline
verify
alone (misses lint)
guard
Repo-wideMultiple individual
lifecycle
calls
--change
Explicit paths known
--change-scope
when paths are known
CLI commandsTool-first approach
spec-gateway
library API
优先选择使用替代
contract
生成任务合约
brief
(旧别名)
lifecycle
全流程流水线单独
verify
(会跳过lint)
guard
仓库级检查多次单独调用
lifecycle
--change
已知明确路径时路径明确时替代
--change-scope
CLI命令工具优先方案
spec-gateway
库API

When to Switch to Authoring Mode

何时切换到编写模式

During implementation, if you discover:
  • A missing exception path that should be in Completion Criteria
  • A Boundary that's too restrictive (need to modify more files than allowed)
  • A Decision that needs to change (technology choice was wrong)
Switch to
agent-spec-authoring
skill, update the Contract FIRST, re-run
agent-spec lint
to validate the change, then resume implementation. Do NOT silently work outside the Contract's boundaries.
实现过程中,如果你发现:
  • 完成标准中缺失了应该包含的异常路径
  • 边界过于严格(需要修改超过允许范围的文件)
  • 决策需要变更(技术选择错误)
切换到
agent-spec-authoring
技能,首先更新合约,重新运行
agent-spec lint
验证变更,然后恢复实现。永远不要暗中突破合约的边界要求。

Escalation

升级场景

Switch to library integration only when:
  • Embedding
    agent-spec
    into another Rust agent runtime
  • Testing
    spec-gateway
    internals
  • Injecting a host
    AiBackend
    via
    verify_with_backend(Arc<dyn AiBackend>)
仅在以下情况切换到库集成:
  • agent-spec
    嵌入到其他Rust agent运行时中
  • 测试
    spec-gateway
    内部逻辑
  • 通过
    verify_with_backend(Arc<dyn AiBackend>)
    注入宿主
    AiBackend