进阶 openspec.dev 2026-11-24 02:54:47 · 0 阅读
第8章 多仓库 Stores (beta) 详解
多仓库(beta) Stores(beta) 规划跨仓库的变更:一个 store,多个仓库。OpenSpec 通常存放在单个仓库内:一个 openspec/ 文件夹紧挨着它所规划的代码。Store 则把这个文件夹搬进一个独立的仓库,多个代码仓库可以共享它。
每台机器完成一次设置后,status、new change、archive 等命令就可以在任意目录下操作这个 store。
```
team-plans (a store: OpenSpec in its own repo)
├── .openspec-store/store.yaml the store's name
└── openspec/
├── specs/
└── changes/
▲
│ set up once on each machine,
│ shared by pushing and cloning like any repo
┌─────────────┼─────────────┐
│ │ │
web-app api-server mobile-app
(code repo) (code repo) (code repo)
```
Store 用 git 共享,和共享代码的方式一样:自己 commit、push、pull 并 review。Spec 和 change 也和代码一样走分支和 pull request。
什么时候需要 store
常见的两个理由:
--store team-plans
```
像普通 Git 仓库一样,通过 commit 和 push 来共享这个 store。
--store team-plans
```
几种配置形态
OpenSpec 有三种配置形态,本页后面都会用到这些名称:
--store team-plans
```
Store team-plans (/Users/you/openspec/team-plans):
- payments: Rules for charging and refunding customers.
Fetch: openspec show --type spec --store team-plans
```
引用还可以携带 store 的 clone URL,供尚未拥有该 store 的机器使用:
```yaml
references:
- team-plans
- { id: design-system, remote: "git@github.com:acme/design-system.git" }
```
声明了 URL 后,openspec doctor 会把缺失的 store 转化为一条可直接粘贴执行的修复命令:
```
# output wrapped to fit
References
- team-plans: ok (/Users/you/openspec/team-plans)
- design-system: Referenced store 'design-system' is not registered on this machine.
Fix: git clone -- git@github.com:acme/design-system.git '/Users/you/openspec/design-system' &&
openspec store register '/Users/you/openspec/design-system' --id design-system
```
Beta 阶段的限制
前端和后端分属不同仓库:一个功能同时涉及两边,方案需要一个统一的家,而不是割裂成两半。
``` shop-plans (store) └── openspec/changes/add-discounts/ one plan for the feature ▲ ┌─────────┴─────────┐ │ │ storefront api (frontend repo) (backend repo) ```一个产品,多个客户端仓库:Android、iOS 和 web 各自从自己的仓库发布,但共享同一套预期行为。Spec 描述的是行为而非实现,所以一份 spec 可以同时服务三者。
``` product-specs (store) └── openspec/specs/checkout/spec.md the expected behavior ▲ ┌─────────────┼─────────────┐ │ │ │ android-app ios-app web-app (code repo) (code repo) (code repo) ``` Store 可以有多个,但我们建议控制在较少的数量。 搭建 store 由一个人创建 store,其他人再加入。 创建 store(一个人操作,每个团队一次)。运行 openspec store setup 并按提示回答: ```bash # run from anywhere; it asks what to create and where openspec store setup ``` 它会问三个问题:- Store name:填 team-plans
- Where should this store live?:默认预填 ~/openspec/<name>,按回车接受或输入其他路径
- Create this store?:展示即将创建的内容,回答 Yes
推送到你的 git 托管平台。先在平台上创建一个空的 team-plans 仓库。Setup 不会添加 git remote,所以需要手动把 store 关联到该仓库,再推送:
```bash # connect the store to the empty repo on your git host cd ~/openspec/team-plans git remote add origin git@github.com:acme/team-plans.git # publish it git push -u origin main ``` 加入 store(每位队友操作,每台机器一次): ```bash # get the store onto your machine git clone git@github.com:acme/team-plans.git ~/openspec/team-plans # tell OpenSpec where it lives openspec store register ~/openspec/team-plans ``` ``` Store registered: team-plans Location: /Users/you/openspec/team-plans OpenSpec root: ready Registry: registered ``` 注册的作用是告诉你的机器这个 store 的位置。Store 的名字已经提交在它内部的 .openspec-store/store.yaml 里。Setup 时已注册了创建者的副本,所以只有 clone 下来的副本才需要这一步。 验证是否成功,在任意目录下运行: ```bash # any OpenSpec command reaches the store by name openspec status --store team-plans ``` ``` Using OpenSpec root: team-plans (/Users/you/openspec/team-plans) No active changes. Create one with: openspec new change- repo-local:OpenSpec 在你的仓库内,不使用 store。这是默认形态。
- store-only:仓库自身不保留任何 spec 或 change,全部存在 store 里。
- store-optional:项目有自己的 openspec/ 文件夹,需要时也能访问 store。
- store-only(项目只写入 store):所有产物都创建在 store 里。下文的 store: 行记录的就是这一点。
- store-optional(项目有自己的 openspec/ 文件夹,同时也使用 store):产物默认创建在你的项目里,除非你在请求中指明 store,或为该 change 传入 --store。你的 agent 会在后续工作流中沿用这个 flag。
- 命令上的 --store <id>。永远优先,在任意目录下都生效。
- 你项目的 openspec/ 文件夹。如果项目自带 specs/ 或 changes/ 文件夹,OpenSpec 会使用它们。
- 你项目中的 store: 行。这是 store-only 项目记录 store 的方式。
- 你机器上的 defaultStore。以上都不适用时的兜底。
- 不加这一行:在 store-only 项目里直接运行命令,OpenSpec 会报错并列出你已注册的 store。
- 记得提交:克隆你项目的队友也会拿到这一行。他们仍需在自己的机器上注册这个 store(见 Set up a store 第 3 步),否则 OpenSpec 会报错并提示注册。
- 与真实文件夹并存:如果项目同时还有 specs/ 或 changes/ 文件夹,OpenSpec 会优先使用它们,忽略这一行并给出警告。
- 集成文件:写入代码仓库。不带 --scope project 时,新的 setup 会把 skills 装在你的机器上,仓库里什么都不会有。
- openspec/config.yaml:逐字节保留,包括 store: 行。
- openspec/specs/ 和 openspec/changes/:不会在代码仓库中创建。
- 把仓库 openspec/specs/ 和 openspec/changes/ 里的全部内容,移到 store 中对应的文件夹。
- 删除已清空的文件夹,让仓库的 openspec/ 里只剩 config.yaml。
- 在那个 config.yaml 中加上 store: 行。
- 它去了哪里:进入了 store 仓库,而不是你的代码旁边。
- 如何共享:在 commit 并 push store 仓库之前,这个 change 只存在于你的本地检出中。队友 pull 之后才能看到。工作流写入的所有产物都是如此。
- 文档中的路径:无论文档哪里出现 openspec/ 路径,在 store 配置下指的都是 store 里的那个文件夹。
形态可能变化:命令名、flag 和文件格式在版本之间都可能改变。升级后请重新阅读本页。
设计上不做同步:OpenSpec 从不自动 clone、pull 或 push。本地检出过时,看到的 spec 就是过时的,直到你手动 pull;references 读取的也只是磁盘上现有的内容。
每个 store 名只能有一个检出:在已被注册的名字下注册第二个文件夹会失败,并提示先运行 openspec store unregister。
Schemas 改变 OpenSpec 产出的内容:产物、它们的顺序以及模板。Worksets (beta) 在同一个编辑器窗口打开 store 和使用它的仓库,让你的 agent 两者都能看到。