这份文档说明当前改造后的 Nucleus 应该如何使用。
Nucleus 现在不是脚手架、平台、provider SDK 集合,也不是项目模板生成器。它是给 AI agent、人类 reviewer、CI 共用的一层本地协议工具:把项目事实、可编辑边界、调用链、影响面、技术决策和验证证据变成结构化数据。
- 用
adopt接入已有 Go 项目,不用init创建项目。 - 用
mark声明契约、能力锚点和验证命令,不生成业务代码。 - 用
.nucleus/decisions/*.yaml记录 provider/library/driver/ORM 等技术选择。 - 用
describe/trace/impact/plan给 AI 提供定位链、影响链和编辑边界。 - 用
verify产出最终机器可读证据。 - 不用 Nucleus 选择 MySQL ORM、Redis client、Kafka SDK、目录结构或应用 wiring。
如果已经安装 CLI:
nucleus --help在本仓库源码内临时运行:
go run ./cmd/nucleus --help后文统一写成 nucleus。如果还没安装,可以把命令替换为:
go run ./cmd/nucleusadopt -> mark -> describe -> trace/impact -> plan -> decision -> change code -> verify -> report
对应命令:
nucleus adopt --dir . --agent codex --json --pretty
nucleus mark contract http --kind openapi --path api/openapi.yaml --json
nucleus mark capability order_store --kind relational_store --symbol OrderStore --json
nucleus mark verify "go test ./..." --json
nucleus describe --dir . --json --pretty --flow
nucleus trace symbol OrderStore --json --pretty
nucleus impact symbol OrderStore --json --pretty
nucleus plan --dir . --task "add mysql capability" --json --pretty --executable
nucleus decision validate .nucleus/decisions/order-store-mysql.yaml --json --pretty
nucleus verify --dir . --json --pretty
nucleus report --dir . --json --pretty在已有 Go 项目根目录执行:
nucleus adopt --dir . --agent codex --json --prettyadopt 只写:
nucleus.yaml.nucleus/*
它不会:
- 创建 service/worker/library 模板
- 生成业务目录
- 修改
go.mod或go.sum - 选择 provider、ORM、driver 或 SDK
如果已有 nucleus.yaml,需要显式覆盖:
nucleus adopt --dir . --force --json --prettyHTTP OpenAPI:
nucleus mark contract http --kind openapi --path api/openapi.yaml --json --prettygRPC proto:
nucleus mark contract grpc --kind proto --path api/proto/order.proto --json --pretty错误码契约:
nucleus mark contract errors --kind errors --path api/errors.yaml --json --pretty契约只是声明项目已有或将要维护的协议文件。外部行为变更应先改契约,再改实现。
能力是语义锚点,不是 provider 实现。
例如项目里已经有 OrderStore 接口:
nucleus mark capability order_store \
--kind relational_store \
--symbol OrderStore \
--intent "order persistence boundary" \
--json --pretty如果短 symbol 名不唯一,命令会返回候选项;应改用稳定 symbol ID。
能力声明可以表达:
- 这个项目有一个
order_store能力 - 它是
relational_store这类语义能力 - 它锚定到项目自己的接口或类型
能力声明不能表达:
- 使用 GORM 还是 Xorm
- 使用 MySQL 还是 PostgreSQL
- DSN 怎么配置
- 事务实现怎么写
- 要不要引入某个 SDK
这些属于技术决策,放到 .nucleus/decisions。
verify 只运行 nucleus.yaml 里声明的项目命令,不隐式运行 go test ./...。
添加验证命令:
nucleus mark verify "go test ./..." --json --pretty
nucleus mark verify "go test ./internal/order/..." --json --pretty这会写入 nucleus.yaml 的 verify.commands。
修改前先跑:
nucleus describe --dir . --json --pretty --flow重点读取:
result_kind/schema_version/schema_ref/ok/diagnosticsserviceendpointsgrpc_serviceserror_codescapabilitiesedit_surfacessymbol_graphflow_graphverificationgenerated_freshness
AI 修改代码前必须看 edit_surfaces:
allowed: 可以改readonly: 只能读forbidden: 不能改
如果目标文件不在 allowed 范围内,不要绕过去写业务代码,应先调整协议边界或让用户确认。
查 symbol 调用链:
nucleus trace symbol OrderStore --json --pretty查能力锚点:
nucleus trace capability order_store --json --pretty查路由链路:
nucleus trace route "POST /orders" --json --pretty查 symbol 影响面:
nucleus impact symbol OrderStore --json --pretty查文件影响面:
nucleus impact file internal/order/store.go --json --pretty查契约影响面:
nucleus impact contract api/openapi.yaml --json --pretty这些命令用于帮助 AI 快速定位:
- 谁调用了这个 symbol
- 这个改动影响哪些文件
- 哪些测试可能相关
- 哪些路由或契约可能受影响
- 哪些边是确定事实,哪些只是启发式推断
短 symbol 名有歧义时,不要猜,使用输出里的稳定 ID。
nucleus plan --dir . --task "add mysql capability" --json --pretty --executable重点读取:
okdiagnosticssuggested_editsblocked_editsblocked_decisionsimpact_summarycommandsriskscontext.recipe_candidates
规则:
blocked_edits不为空时,停止修改。blocked_decisions不为空时,先补或 supersede decision。recipe_candidates只是参考知识,不是默认技术选型。commands是建议链路,不代表 Nucleus 会自动执行所有命令。
当引入或替换 provider/library/driver/ORM/SDK 时,写 decision 文件。
示例:.nucleus/decisions/order-store-mysql.yaml
schema_version: "decision.v1"
id: order-store-mysql
capability: order_store
decision:
provider: mysql
library: gorm.io/gorm
driver: github.com/go-sql-driver/mysql
status: proposed
locked: false
reason:
- project team selected MySQL for order persistence
- existing service conventions use GORM repositories
impact:
symbols:
- go://example.com/order/internal/order#OrderStore
files:
- internal/order/store.go
- internal/order/mysql_store.go
verification:
commands:
- go test ./internal/order/...
alternatives:
- provider: mysql
library: xorm.io/xorm
driver: github.com/go-sql-driver/mysql
reason: rejected because project already uses GORM transaction helpers校验:
nucleus decision validate .nucleus/decisions/order-store-mysql.yaml --json --pretty接受并锁定:
nucleus decision accept .nucleus/decisions/order-store-mysql.yaml --by human --json --pretty锁定后,后续 AI 不能静默替换 provider/library/driver。
如果确实要替换,创建新的 supersede decision,然后执行:
nucleus decision supersede .nucleus/decisions/order-store-mysql-v2.yaml --json --pretty
nucleus decision validate .nucleus/decisions/order-store-mysql-v2.yaml --json --pretty契约变更后运行:
nucleus gen --dir . --json --prettygen 只生成契约派生产物。它不生成 provider glue、业务 wiring、目录模板或依赖变更。
快速检查:
nucleus validate --dir . --json --pretty
nucleus lint --dir . --strict --json --pretty最终证据:
nucleus verify --dir . --json --prettyverify 会输出 evidence.v1,重点看:
oksummarystepsdiagnostics- 每个 step 的
kind/status/ok/exit_code
如果根目录没有 nucleus.yaml,verify 会失败并给出 manifest.read_failed。这是正确行为;先运行 adopt。
启动本地 stdio MCP:
nucleus --dir . mcp --stdioMCP 是只读结构化工具层。它不会写文件、不会执行验证命令、不会访问网络、不会选择 provider。
常用工具:
get_service_descriptionget_edit_surfacesget_contractsget_capabilitiestrace_symboltrace_routetrace_capabilityimpact_symbolimpact_fileimpact_contractfind_symbollist_callerslist_calleesvalidate_decisionlist_decisionsget_reportbuild_planlist_recipesget_recipe
所有 MCP structuredContent 都应带:
result_kindschema_versionschema_refokdiagnostics
agent 不要解析自然语言文本来判断成功失败,应以这些字段为准。
nucleus adopt --dir . --agent codex --json --pretty
nucleus mark verify "go test ./..." --json --pretty
nucleus describe --dir . --json --prettynucleus mark capability order_store --kind relational_store --symbol OrderStore --json --pretty
nucleus plan --dir . --task "add mysql capability for order_store" --json --pretty --executable然后由用户或 AI 写 .nucleus/decisions/order-store-mysql.yaml,明确 MySQL、ORM、driver、理由、影响面和验证命令。
Nucleus 不会替你决定用 GORM 还是 Xorm。
nucleus describe --dir . --json --pretty --flow
nucleus impact contract api/openapi.yaml --json --pretty
nucleus plan --dir . --task "change GET /orders response" --json --pretty --executable先改 api/openapi.yaml,再运行:
nucleus gen --dir . --json --pretty
nucleus verify --dir . --json --prettynucleus plan --dir . --task "switch order_store provider to xorm" --json --pretty --executable如果出现 blocked_decisions,创建 supersede decision:
nucleus decision supersede .nucleus/decisions/order-store-xorm.yaml --json --pretty
nucleus decision validate .nucleus/decisions/order-store-xorm.yaml --json --pretty再重新 plan。
这些已经不是当前方向:
nucleus initnucleus capability addnucleus capability add --provider ...nucleus migratereport --platformbridgeprovider adapter 集合- 自动修改
go.mod/go.sum的能力接入 - 把 provider/library/driver 写进
nucleus.yaml
如果一个需求必须选择技术栈,把选择写成 decision evidence,而不是写成框架默认值。
- 先跑
describe,读取 edit surfaces、symbol graph、verification。 - 对目标 symbol/route/file 跑
trace或impact。 - 跑
plan --executable,遇到 blocked 就停止。 - provider/library/driver 变化必须写 decision。
- 只改 allowed edit surfaces 内的文件。
- 变更后跑
verify --json。 - 最终交付时引用 evidence,而不是说“我觉得可以”。