agent-spec-tool-first
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAgent Spec Tool-First Workflow
Agent Spec 工具优先工作流
Version: 3.1.0 | Last Updated: 2026-03-08
You are an expert at using as a CLI tool for contract-driven AI coding. Help users by:
agent-spec- Planning: Render task contracts before coding with
contract - Implementing: Follow contract Intent, Decisions, Boundaries
- Verifying: Run /
lifecycleto check code against specsguard - Reviewing: Use for human-readable summaries,
explainfor git trailersstamp - Debugging: Interpret verification failures and fix code accordingly
版本: 3.1.0 | 最后更新: 2026-03-08
你是使用这一CLI工具实现合约驱动AI编码的专家,请按照以下方式为用户提供帮助:
agent-spec- 规划阶段:编码前使用命令生成任务合约
contract - 实现阶段:严格遵循合约的意图、决策、边界要求
- 验证阶段:运行/
lifecycle命令对照规格检查代码guard - 评审阶段:使用生成人类可读的总结,使用
explain生成git尾注stamp - 调试阶段:解读验证失败原因并对应修复代码
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
快速参考
| Command | Purpose | When to Use |
|---|---|---|
| Scaffold new spec | Starting a new task |
| Render Task Contract | Before coding - read the execution plan |
| Spec quality check | After writing spec, before giving to Agent |
| Full lint + verify pipeline | After edits - main quality gate |
| Repo-wide check | Pre-commit / CI - all specs at once |
| PR-ready review summary | Contract Acceptance - paste into PR |
| Execution history | See how many retries the Agent needed |
| Preview git trailers | Before committing - traceability |
| Raw verification only | When you want verify without lint gate |
| VCS-aware status | Check uncommitted state |
| 命令 | 用途 | 使用时机 |
|---|---|---|
| 初始化新规格脚手架 | 启动新任务时 |
| 生成任务合约 | 编码前,查看执行计划时 |
| 规格质量检查 | 编写完规格后,交付给Agent前 |
| 完整检查+验证流水线 | 代码修改后,核心质量门禁 |
| 仓库级全局检查 | Pre-commit/CI场景,一次性检查所有规格 |
| 可直接用于PR的评审总结 | 合约验收阶段,粘贴到PR中时 |
| 执行历史 | 查看Agent重试次数时 |
| 预览git尾注 | 提交前,实现可追溯性时 |
| 仅执行原始验证 | 不需要lint门禁,仅需要验证时 |
| VCS感知的状态检查 | 检查未提交状态时 |
Documentation
文档说明
Refer to the local files for detailed command patterns:
- - Complete CLI command reference with all flags
./references/commands.md
详细命令模式请参考本地文件:
- - 包含所有参数的完整CLI命令参考
./references/commands.md
IMPORTANT: Documentation Completeness Check
重要提示:文档完整性检查
Before answering questions, Claude MUST:
- Read for exact command syntax
./references/commands.md - If file read fails: Inform user "references/commands.md is missing, answering from SKILL.md patterns"
- Still answer based on SKILL.md patterns + built-in knowledge
回答问题前,Claude必须:
- 读取获取准确的命令语法
./references/commands.md - 如果文件读取失败:告知用户“references/commands.md不存在,将基于SKILL.md的模式回答”
- 仍可基于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.7Catches: 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.specAgent 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.specAgent受到三重约束:
- 决策告诉它“怎么做”(不需要自行选型技术栈)
- 边界告诉它“可以修改什么”(不允许未经授权的文件修改)
- 完成标准告诉它“什么时候算完成”(所有关联测试必须通过)
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/runsFour verification layers run in sequence:
- lint — re-check Contract quality (prevent spec tampering)
- StructuralVerifier — pattern match Must NOT constraints against code
- BoundariesVerifier — check changed files are within Allowed Changes
- 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四层验证按顺序执行:
- lint——重新检查合约质量(防止规格被篡改)
- StructuralVerifier——对照代码匹配禁止性约束
- BoundariesVerifier——检查变更文件在允许修改范围内
- TestVerifier——执行绑定到每个场景的测试
Agent重试循环(无需人工介入):
编码 → lifecycle → 失败(2/5) → 读取失败总结 → 修复 → lifecycle → 失败(4/5) → 修复 → lifecycle → 通过(5/5) ✓运行日志会记录这个历史——“本合约共尝试3次才通过验证”。
Retry Protocol
重试协议
When lifecycle fails, follow this exact sequence:
- Run:
agent-spec lifecycle <spec> --code . --format json - Parse JSON output, find each scenario's and
verdictevidence - For : the bound test ran and failed — read evidence to understand why, fix code
fail - For : the bound test was not found — check
skipselector matches a real test nameTest: - For : AI verification pending — review manually or enable AI backend
uncertain - Fix code based on evidence. Do NOT modify the spec file — changing the Contract to make verification pass is sycophancy, not a fix
- Re-run lifecycle
- 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执行失败时,严格遵循以下流程:
- 运行:
agent-spec lifecycle <spec> --code . --format json - 解析JSON输出,找到每个场景的和
verdictevidence - 对于:绑定的测试运行失败——读取证据了解原因,修复代码
fail - 对于:未找到绑定的测试——检查
skip选择器是否匹配真实测试名称Test: - 对于:AI验证待处理——人工评审或启用AI后端
uncertain - 基于证据修复代码,不要修改spec文件——修改合约让验证通过属于奉承行为,不是真正的修复
- 重新运行lifecycle
- 同一场景连续失败3次后,停止重试并升级给人工处理
重要规则:规格定义了“什么是正确的”。如果代码不匹配,修复代码。如果规格本身有误,切换到编写模式显式更新合约——永远不要暗中降低验收标准。
Step 5: Guard gate (pre-commit / CI)
步骤5:Guard门禁(pre-commit/CI)
bash
undefinedbash
undefinedPre-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 markdownReviewer judges two questions:
- Is the Contract definition correct? (Intent, Decisions, Boundaries make sense?)
- 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评审者只需要判断两个问题:
- 合约定义是否正确?(意图、决策、边界是否合理?)
- 所有验证是否都通过?(包括错误路径在内4/4通过?)
如果两个答案都是“是”→ 审批。这比阅读代码diff快10倍。
如有需要可以查看重试历史:
bash
agent-spec explain specs/user-registration.spec --code . --historyAssisting Contract Acceptance
合约验收辅助
When helping a human review a completed task:
- Run and present the output
agent-spec explain <spec> --code . --format markdown - If human asks about retry history: run with flag
--history - If human asks about specific failures: run and extract the relevant scenario results
agent-spec lifecycle <spec> --code . --format json - If human approves: run and present the trailers
agent-spec stamp <spec> --code . --dry-run
协助人类评审已完成的任务时:
- 运行并展示输出
agent-spec explain <spec> --code . --format markdown - 如果用户询问重试历史:加上参数运行
--history - 如果用户询问特定失败原因:运行并提取对应场景的结果
agent-spec lifecycle <spec> --code . --format json - 如果用户审批通过:运行并展示尾注
agent-spec stamp <spec> --code . --dry-run
Step 7: Stamp and archive
步骤7:标记并归档
bash
agent-spec stamp specs/user-registration.spec --dry-runbash
agent-spec stamp specs/user-registration.spec --dry-runOutput: 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
验证结果解读
| Verdict | Meaning | Action |
|---|---|---|
| Scenario verified | No action needed |
| Scenario failed verification | Read evidence, fix code |
| Test not found or not run | Add missing test or fix selector |
| AI stub / manual review needed | Review manually or enable AI backend |
Key rule: != . All four verdicts are distinct.
skippass| 结果 | 含义 | 处理方式 |
|---|---|---|
| 场景验证通过 | 无需操作 |
| 场景验证失败 | 读取证据,修复代码 |
| 未找到测试或未运行测试 | 添加缺失的测试或修复选择器 |
| AI桩/需要人工评审 | 人工评审或启用AI后端 |
重要规则: != 。四种结果完全独立。
skippassVCS Awareness
VCS感知能力
agent-spec auto-detects the VCS from the project root. Behavior differs between git and jj:
| Condition | Behavior |
|---|---|
| Use |
| jj repo | Do NOT run |
| jj repo | |
| jj repo | |
Only | Use standard git commands ( |
| Neither | Change scope detection unavailable; use |
agent-spec会从项目根目录自动检测VCS,git和jj的行为不同:
| 条件 | 行为 |
|---|---|
存在 | 使用 |
| jj仓库 | 不要运行 |
| jj仓库 | |
| jj仓库 | |
仅存在 | 使用标准git命令( |
| 两者都不存在 | 无法自动检测变更范围;显式使用 |
Change Set Options
变更集选项
| Flag | Behavior | Default |
|---|---|---|
| Explicit file/dir for boundary checking | (none) |
| Git staged files | guard default |
| All git working tree changes | (none) |
| Jujutsu VCS changes | (none) |
| No change detection | lifecycle/verify default |
| 参数 | 行为 | 默认值 |
|---|---|---|
| 边界检查的显式文件/目录 | 无 |
| Git暂存文件 | guard命令默认 |
| Git工作树所有变更 | 无 |
| Jujutsu VCS变更 | 无 |
| 不做变更检测 | lifecycle/verify命令默认 |
Advanced Features
高级特性
Verification Layers
验证分层
bash
undefinedbash
undefinedRun 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
undefinedundefinedRun Logging
运行日志
bash
agent-spec lifecycle specs/task.spec --code . --run-log-dir .agent-spec/runs
agent-spec explain specs/task.spec --historybash
agent-spec lifecycle specs/task.spec --code . --run-log-dir .agent-spec/runs
agent-spec explain specs/task.spec --historyAI 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-verifierbash
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 is used, the calling Agent acts as the AI verifier. This is a two-step protocol:
--ai-mode callerStep 1: Emit AI requests
bash
agent-spec lifecycle specs/task.spec --code . --ai-mode caller --format jsonIf 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 objects with scenario context, code paths, contract intent, and constraints.
AiRequestStep 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.jsonThis 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
使用时,调用方Agent会作为AI验证者。这是一个两步协议:
--ai-mode caller步骤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
适用/不适用场景
| Scenario | Use agent-spec? | Why |
|---|---|---|
| Clear feature with defined inputs/outputs | Yes | Contract can express deterministic acceptance criteria |
| Bug fix with reproducible steps | Yes | Great for "given bug X, when fixed, then Y" |
| Exploratory prototyping | No | You don't know "what is done" yet - vibe code first |
| Large architecture refactor | No | Boundaries hard to define, "better architecture" isn't testable |
| Security/compliance rules | Yes (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.specCommon Errors
常见错误
| Error | Cause | Solution |
|---|---|---|
| Guard reports N specs failing | Specs have lint or verify issues | Run |
| Test selector doesn't match any test | Check |
| Quality score below threshold | Too many lint warnings | Fix vague verbs, add quantifiers, improve testability |
| Boundary violation detected | Changed file outside allowed paths | Either update Boundaries or revert the change |
| Using | Expected — review manually |
| Agent keeps failing lifecycle | Contract criteria too vague or too strict | Improve Completion Criteria specificity |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| Guard报告N个规格失败 | 规格存在lint或验证问题 | 单独对每个失败的规格运行 |
场景返回 | 测试选择器没有匹配到任何测试 | 检查spec中的 |
| 质量分数低于阈值 | lint警告过多 | 修复模糊动词,添加量化描述,提升可测试性 |
| 检测到边界违反 | 修改了允许路径外的文件 | 要么更新边界,要么回滚变更 |
所有AI场景返回 | 使用了 | 属于预期行为——人工评审即可 |
| Agent持续无法通过lifecycle | 合约标准过于模糊或过于严格 | 提升完成标准的具体性 |
Command Priority
命令优先级
| Preference | Use | Instead of |
|---|---|---|
| Render task contract | |
| Full pipeline | |
| Repo-wide | Multiple individual |
| Explicit paths known | |
| CLI commands | Tool-first approach | |
| 优先选择 | 使用 | 替代 |
|---|---|---|
| 生成任务合约 | |
| 全流程流水线 | 单独 |
| 仓库级检查 | 多次单独调用 |
| 已知明确路径时 | 路径明确时替代 |
| CLI命令 | 工具优先方案 | |
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 skill, update the Contract FIRST, re-run to validate the change, then resume implementation. Do NOT silently work outside the Contract's boundaries.
agent-spec-authoringagent-spec lint实现过程中,如果你发现:
- 完成标准中缺失了应该包含的异常路径
- 边界过于严格(需要修改超过允许范围的文件)
- 决策需要变更(技术选择错误)
切换到技能,首先更新合约,重新运行验证变更,然后恢复实现。永远不要暗中突破合约的边界要求。
agent-spec-authoringagent-spec lintEscalation
升级场景
Switch to library integration only when:
- Embedding into another Rust agent runtime
agent-spec - Testing internals
spec-gateway - Injecting a host via
AiBackendverify_with_backend(Arc<dyn AiBackend>)
仅在以下情况切换到库集成:
- 将嵌入到其他Rust agent运行时中
agent-spec - 测试内部逻辑
spec-gateway - 通过注入宿主
verify_with_backend(Arc<dyn AiBackend>)AiBackend