进阶 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 常见的两个理由:

前端和后端分属不同仓库:一个功能同时涉及两边,方案需要一个统一的家,而不是割裂成两半。

``` 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
然后它会报告创建结果: ``` Store ready: team-plans Location: ~/openspec/team-plans OpenSpec root: ready Registry: registered Next: run normal OpenSpec commands against this store, for example: openspec new change --store team-plans ``` 像普通 Git 仓库一样,通过 commit 和 push 来共享这个 store。

推送到你的 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 --store team-plans ``` 几种配置形态 OpenSpec 有三种配置形态,本页后面都会用到这些名称:
  • repo-local:OpenSpec 在你的仓库内,不使用 store。这是默认形态。
  • store-only:仓库自身不保留任何 spec 或 change,全部存在 store 里。
  • store-optional:项目有自己的 openspec/ 文件夹,需要时也能访问 store。
默认形态:OpenSpec 在你的仓库内 openspec init 会在代码旁边创建 openspec/ 文件夹,该仓库的 spec 和 change 都放在里面,不涉及 store。这是 Set up your project 介绍的形态,大多数项目用这一种就够。 ``` web-app (code repo) └── openspec/ ├── specs/ └── changes/ ``` OpenSpec 在仓库外、存放在 store 中 仓库自身不保留 spec 和 change,所有规划内容都存在 store 里,仓库配置里的一行代码把两者关联起来。 常见于一个团队负责所有仓库、在同一个地方做规划的场景。前面的示例都是这种形态。 ``` team-plans (store) └── openspec/ ├── specs/ the repo's specs live here └── changes/ its changes too ▲ │ store: team-plans (the connecting line) web-app (code repo) └── openspec/ └── config.yaml nothing else ``` OpenSpec 既在仓库内、也在 store 中 仓库对自己内部的工作保持 repo-local,store 存放共享的 spec 和 change。在仓库内,OpenSpec 使用项目的 openspec/ 文件夹,只有传入 --store 时才会访问 store。 常见于仓库在 store 出现之前就已经在用 OpenSpec,或者一个相对独立的仓库只是偶尔涉及共享工作的场景。 ``` team-plans (store) └── openspec/ the shared specs and changes ▲ │ only when you pass --store team-plans web-app (code repo) └── openspec/ this repo's own ├── config.yaml ├── specs/ └── changes/ ``` 仓库可以先保持 repo-local,之后再把自己的 spec 和 change 迁入 store。Move a repo's specs and changes into the store 一节介绍了具体做法。 使用 store 时产物的创建位置 使用 store 时,OpenSpec 还需要决定产物创建在哪里。这取决于你的配置形态:
  • store-only(项目只写入 store):所有产物都创建在 store 里。下文的 store: 行记录的就是这一点。
  • store-optional(项目有自己的 openspec/ 文件夹,同时也使用 store):产物默认创建在你的项目里,除非你在请求中指明 store,或为该 change 传入 --store。你的 agent 会在后续工作流中沿用这个 flag。
OpenSpec 把产物写入两个位置之一:你的项目的 openspec/ 文件夹,或 store 的。它按以下顺序判断,第一个命中的生效:
  1. 命令上的 --store <id>。永远优先,在任意目录下都生效。
  2. 你项目的 openspec/ 文件夹。如果项目自带 specs/ 或 changes/ 文件夹,OpenSpec 会使用它们。
  3. 你项目中的 store: 行。这是 store-only 项目记录 store 的方式。
  4. 你机器上的 defaultStore。以上都不适用时的兜底。
如果以上都不适用而你已注册了 store,命令会失败并列出这些 store。当某个 store 的 id 与你仓库 origin remote 中的仓库名一致时,报错会指明该 store 以及选择它的 store: 行。OpenSpec 从不自动按名字猜测 store,因为不同组织的仓库可能重名。 OpenSpec 选中某个 store 时,会在命令输出前打印 Using OpenSpec root: ...。 store: 行(适用于 store-only 项目) 在项目的 openspec/config.yaml 中加一行: ```yaml # web-app/openspec/config.yaml store: team-plans ``` 之后你和 agent 在项目里运行的所有命令都会使用这个 store,无需敲任何 flag: ```bash # inside web-app, connected openspec status ``` ``` Using OpenSpec root: team-plans (/Users/you/openspec/team-plans) No active changes. Create one with: openspec new change --store team-plans ```
  • 不加这一行:在 store-only 项目里直接运行命令,OpenSpec 会报错并列出你已注册的 store。
  • 记得提交:克隆你项目的队友也会拿到这一行。他们仍需在自己的机器上注册这个 store(见 Set up a store 第 3 步),否则 OpenSpec 会报错并提示注册。
  • 与真实文件夹并存:如果项目同时还有 specs/ 或 changes/ 文件夹,OpenSpec 会优先使用它们,忽略这一行并给出警告。
在 store-only 仓库中安装集成 在代码仓库根目录运行 init,可以安装 AI 工具集成文件,同时不会把规划内容搬回该仓库: ```bash # inside web-app, at the repository root openspec init --scope project --tools claude ```
  • 集成文件:写入代码仓库。不带 --scope project 时,新的 setup 会把 skills 装在你的机器上,仓库里什么都不会有。
  • openspec/config.yaml:逐字节保留,包括 store: 行。
  • openspec/specs/ 和 openspec/changes/:不会在代码仓库中创建。
在代码仓库的子目录里运行这个命令会被 OpenSpec 拒绝,必须在仓库根目录执行。 OpenSpec 也会拒绝这里的 --language,因为语言配置属于外部 store。请在 store 根目录运行 init,或直接编辑那份配置。 机器上的 defaultStore 如果你参与的所有项目都用同一个 store,设置一次即可。当 OpenSpec 找不到 flag、本地 openspec/ 文件夹和 store: 行时,就会回退到它: ```bash # use team-plans whenever nothing else names a store openspec config set defaultStore team-plans # undo it openspec config unset defaultStore ``` 保持本地操作的命令。init、update、templates、schemas 以及 openspec schema 子命令只作用于当前目录,不接受 --store。 把仓库的 spec 和 change 迁入 store 把一个仓库从 repo-local 改为 store-only 的步骤:
  1. 把仓库 openspec/specs/ 和 openspec/changes/ 里的全部内容,移到 store 中对应的文件夹。
  2. 删除已清空的文件夹,让仓库的 openspec/ 里只剩 config.yaml。
  3. 在那个 config.yaml 中加上 store: 行。
之后在仓库内运行 openspec status,开头会显示 Using OpenSpec root: team-plans。 在 store 中工作 对你和你的 agent 来说,工作流没有任何变化。Propose、apply、archive 照常执行。唯一的区别是产物的创建位置,上文已经讲过。 在 store-only 仓库内创建 change,它会落到 store 里: ```bash # inside web-app; the store: line routes this to team-plans openspec new change add-login ``` ``` Using OpenSpec root: team-plans (/Users/you/openspec/team-plans) Created change 'add-login' at /Users/you/openspec/team-plans/openspec/changes/add-login/ Schema: spec-driven Next: openspec status --change add-login --store team-plans ```
  • 它去了哪里:进入了 store 仓库,而不是你的代码旁边。
  • 如何共享:在 commit 并 push store 仓库之前,这个 change 只存在于你的本地检出中。队友 pull 之后才能看到。工作流写入的所有产物都是如此。
  • 文档中的路径:无论文档哪里出现 openspec/ 路径,在 store 配置下指的都是 store 里的那个文件夹。
当产物出现在意料之外的位置时,openspec doctor 可以检查你的配置且不做任何改动,并对每个问题给出修复建议: ```bash # check the current root and its stores openspec doctor ``` ``` Doctor Root Location: /Users/you/openspec/team-plans OpenSpec root: ok Store: team-plans (metadata ok) References (none declared) ``` 如果想在不做检查的情况下查看当前目录关联的 root 和 store,可以用 openspec context。 要在同一个编辑器窗口中同时打开 store 和仓库,让 agent 两边都能读到,参见 Worksets (beta)。 从其他 store 读取 spec 你的仓库可以保留自己的 openspec/ 文件夹,同时让 agent 读取其他 store 的 spec。在仓库的 openspec/config.yaml 的 references: 下声明那个 store: ```yaml # api-server/openspec/config.yaml references: - team-plans ``` 引用是只读的。你的工作仍在自己的仓库里,引用只影响 agent 被告知的内容。 当工作流创建产物时,其指令中会附上被引用 store 的 spec 索引,每条带一行摘要和获取它的确切命令: ``` 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 阶段的限制

形态可能变化:命令名、flag 和文件格式在版本之间都可能改变。升级后请重新阅读本页。

设计上不做同步:OpenSpec 从不自动 clone、pull 或 push。本地检出过时,看到的 spec 就是过时的,直到你手动 pull;references 读取的也只是磁盘上现有的内容。

每个 store 名只能有一个检出:在已被注册的名字下注册第二个文件夹会失败,并提示先运行 openspec store unregister。

Schemas 改变 OpenSpec 产出的内容:产物、它们的顺序以及模板。Worksets (beta) 在同一个编辑器窗口打开 store 和使用它的仓库,让你的 agent 两者都能看到。

评论 (0)