floorplan architecture.json 模式详解:为代码库地图定义子系统、连线、流程与近期改动
floorplan architecture.json 模式详解:为代码库地图定义子系统、连线、流程与近期改动
floorplan 把代码库画成一张可交互的地图,而这张图的骨架由 floorplan/architecture.json 定义:哪些子系统存在、每个模块的职责、文件如何归属、扫描器看不见的连接、引导流程和"Start here"导览。本文以仓库中的 schema 参考文档为主线,逐字段拆解这个格式,并结合 构建脚本 与 glob/路径工具 的源码,说明每个字段在校验、文件分配和渲染时的真实行为,帮助你写出一次通过 build.mjs 校验、且读者能读懂的地图。
职责划分:architecture.json 只描述"结构"
按照 schema 文档 的约定,floorplan/architecture.json 负责描述结构:subsystems、modules、external services、flows。单个文件和路由的名称与描述不写在这里,而是写进 floorplan/descriptions/*.json。
两者分离的原因很直接:并行工作的 agent 可以各自写自己的 descriptions/<key>.json,互不冲突,不会在同一个大文件里争抢修改。build.mjs 会把两份内容都对着扫描结果做校验,并按字段报告问题。相关写法见 writing-descriptions.md。
这也决定了地图的生成链路:
node skills/floorplan/scripts/scan.mjs <root> # 产出 floorplan/.cache/scan.json
# 此时由 Claude 或 Codex 写 floorplan/architecture.json
node skills/floorplan/scripts/build.mjs <root> # 校验并生成 graph.json + index.html
build.mjs 的用法头注释写明了可用的命令行参数(build.mjs):
node build.mjs [root] [--arch <file>] [--scan <file>] [--out <dir>] [--check]
其中 --check 只做校验与统计、不写出产物(build.mjs),适合在批量修改后快速确认结构没有写坏;--arch 和 --scan 可以指向非默认位置的文件,--out 把地图输出到别的目录。
顶层字段
| 字段 | 类型 | 是否必需 | 含义 |
|---|---|---|---|
$schema |
"floorplan/architecture@1" |
否 | 标记字段 |
project |
object | 是 | name(字符串,必需)、summary(1–3 句)、entrypoints(文件路径) |
nodes |
array | 是 | 子系统与模块,见下文 |
edges |
array | 否 | 扫描器看不到的连接 |
annotations |
array | 否 | 钉在具体文件上的备注 |
flows |
array | 否 | 地图上的引导式走查 |
tour |
array | 否 | 首次打开时的"Start here"导览:3–8 个站点 { "node": "<node id 或文件路径>", "title": "…", "note": "…" },用平实语言、按新人应该看到的顺序排列。第一个 flow 会自动追加到末尾 |
audience |
"everyone" | "developers" |
否 | 名称和描述写给谁看。默认 "everyone":非技术负责人能听懂的业务语言;"developers":面向工程师的技术风格 |
include |
string[] | 否 | 要上图的文件类别。默认 ["code"],还可加 config、docs |
ignore |
string[] | 否 | 完全不上图的 glob(生成代码、vendored 库、fixtures) |
几个字段在源码中的实际约束值得提前知道:
project.name是硬性要求。build.mjs在project.name缺失时直接报project.name is required(build.mjs),地图页面的标题也用${project.name} map渲染(build.mjs)。include的取值来自文件分类。classify()会把文件归入code/config/docs/data/asset/lock/generated/log/other(common.mjs)。因此include: ["code", "config"]才是有意义组合;写一个data通常不会得到你想看的东西。ignore是"整个不上板",在文件被分给节点之前就过滤掉了(build.mjs)。它适合放生成客户端、vendored 副本、快照 fixtures;测试和脚本不该放这里,它们值得有自己的 chip。audience会被校验并给出纠错建议。值不在everyone/developers中时报错并附did you mean …,最终解析结果会写进graph.meta.audience(build.mjs、build.mjs)。entrypoints会影响"未使用文件"检查。health.mjs把project.entrypoints与 flow 步骤中出现的文件一起视为入口,避免把真正的入口误报成"没人用"(health.mjs);如果某个entrypoints路径不在被纳入的文件集合里,build.mjs会给出警告(build.mjs)。
nodes:地图上的每一块 chip
每个 node 都是板上的一个 chip,层级由 parent 决定:没有 parent 的节点位于顶层板面。
| 字段 | 类型 | 含义 |
|---|---|---|
id |
string | 唯一;字母、数字、_ - .。约定按父级用点号命名(api.orders) |
label |
string | 简短显示名("Orders API") |
kind |
enum | ui service worker library feature data config test script infra external |
parent |
string | 外层节点的 id |
summary |
string | 一句话:它负责什么 |
details |
string | 可选的长备注;空行分段 |
paths |
string[] | 认领文件的 glob(见下) |
exclude |
string[] | 从 paths 里再挖回去的 glob。会被所有后代继承 |
packages |
string[] | external 节点专用:这个节点代表的包名,例如 "stripe"、"npm:@stripe/*"、"pypi:sqlalchemy"。对这些包的 import 会变成指向该节点的边 |
hosts |
string[] | external 节点专用:代码里调用的主机名,例如 "api.stripe.com"、"*.squareup.com"。调用会变成 http 边 |
tags |
string[] | 自由标签,显示在面板里 |
kind 的语义(原文列表的浓缩):
ui:前端与应用service:可部署的服务端worker:后台任务与队列library:共享代码feature:服务或 UI 内部的一条纵向切片data:模型、迁移、数据库访问层configtestscript:一次性与运维脚本infra:部署、CI、IaCexternal:数据库和第三方 API,不拥有任何文件
这些枚举在源码里是 NODE_KINDS 常量,写错值会触发带纠错建议的报错(build.mjs、build.mjs)。同一份校验还会检查:
id必须匹配^[A-Za-z0-9_.\-]+$;unassigned是保留 id,不能占用(构建会自动生成一个同名 chip 承接无人认领的文件,见 build.mjs);id不能重复,label必填,paths/exclude/packages/hosts/tags必须是字符串数组;parent必须指向已存在的节点,父子链不能成环(成环会报node "x" is part of a parent cycle,build.mjs);kind: "external"却带了paths只会得到警告——external 节点按设计不拥有文件(build.mjs)。
details 是给读者更多上下文的地方,query.mjs show n:<id> 会把它一并返回(graph-query.js),所以写给"想深挖一层"的人看是值得的;summary 则应当在任何面板里单独成立。
Glob 语法
**匹配任意多层目录,*只在单个路径段内匹配,?匹配一个字符,{a,b}是二选一。- 方括号里包住的整个文件夹名(
[id]、[...slug]、[[...slug]],Next.js / SvelteKit 风格)按字面匹配该文件夹:app/orders/[id]/**。在其他位置,[abc]表示匹配其中一个字符。 - 不含通配符的模式同时按目录匹配:
backend/app等价于backend/app/**。 - 路径相对于代码库根目录;在多仓库根目录下,以仓库文件夹名开头。
这三条规则都有测试覆盖(helpers-lib.test.mjs),实现集中在 common.mjs:
// 无通配符:字面前缀即整个 glob,同时匹配其下所有文件
if (!GLOB_CHARS.test(g)) {
return { glob, specificity: g.length, test: (p) => p === g || p.startsWith(g + '/') };
}
// 有通配符:既匹配自身,也匹配其下所有内容
const re = globToRegExp(g);
const under = globToRegExp(g + '/**');
return { glob, specificity: literalPrefix.length, test: (p) => re.test(p) || under.test(p) };
注意 specificity 取的是字面前缀长度,这正是同层节点之间"谁更具体"的判定依据。
文件如何被分配
每个被纳入的文件只能属于一个节点:
- 收集
paths命中、且exclude未命中的节点。 - 最深的节点获胜,所以子节点能压过父节点。可以给父节点一个大 glob(
web/**),再给子节点更窄的 glob;没有子节点认领的文件留在父节点上。 - 同深度的节点之间,字面前缀更长的 glob 获胜。真正的平局会被警告。
- 没有任何节点认领的文件进入自动生成的 Unassigned chip。把它控制在约 10% 以下。
源码就是这三步的直译(build.mjs):
const matchers = nodes.filter((n) => n?.id && isStrArr(n.paths ?? [])).map((n, order) => ({
id: n.id, order, depth: depthOf.get(n.id) || 0,
paths: (n.paths || []).map(compileGlob),
exclude: inheritedExcludes(n).map(compileGlob),
hits: new Array((n.paths || []).length).fill(0),
}));
// 每个文件:跳过被 exclude 的节点 → 取命中的最大 specificity → 以 [depth, specificity] 字典序取最优
const key = [m.depth, spec];
exclude 的继承由 inheritedExcludes() 递归向上收集实现(build.mjs)。这就是文档里那个典型用法的原理:在前端节点上写 "exclude": ["**/*.test.tsx"],同目录的测试文件就会从它的所有子节点里一并排除,从而能被单独的 Tests 节点认领。
构建还会做两项体检:
- 某条
pathsglob 一个文件都没匹配到 → 警告node "x" path "…" matches no included file; - 两个同深度节点的 glob 精确度相同 → 警告
N file(s) matched two sibling nodes equally; the first declared won。
在阅读器里,一个节点自己拥有超过约 24 个文件时,会自动把这些文件归组为文件夹 chip,所以不必为每个目录都建节点;只有当某个目录确实有明确含义时才建 node。
edges:只写静态分析看不见的连接
只加静态分析找不到的东西。import(import)和能匹配到路由的 HTTP 调用(http)已经在扫描结果里了。
| 字段 | 类型 | 含义 |
|---|---|---|
from, to |
string | 节点 id 或 文件路径 |
kind |
enum | import call http rpc event queue data config |
label |
string | 简短标签,例如 "POST /orders"、"order.paid"、"reads/writes" |
description |
string | 可选的一句话,点击轨迹时显示 |
合适的候选:
- service → database(
data) - 发布者 → 队列 → 消费者(
queue/event) - 第三方进来的 webhook(
http,从 external 节点指向处理函数) - cron / 调度器 → 作业(
call) - Firebase 后台函数(定时、Firestore/Storage/Pub/Sub 触发器,来自扫描摘要的 FIREBASE FUNCTIONS 列表)→ 一个
worker节点;把触发它的集合或 topic 也连上(event) - 通过环境变量配置的 base URL 发起的调用,而匹配器漏掉了(
http)
from / to 的解析逻辑是:先看是不是节点 id,再看是不是被纳入的扫描文件;带 file: 前缀的文件路径也接受。指向一个存在但未被纳入的文件时,会明确提示"指向它的节点而不是文件";两者都不是则报 neither a node id nor a scanned file 并附 did you mean(build.mjs)。kind 不在 EDGE_KINDS 里同样会报错并纠错(build.mjs)。
external 节点的自动连接
packages 和 hosts 让外部依赖自动成边,规则在源码里很清晰(build.mjs):
// "stripe" 会被补成 "*:stripe",即匹配任意生态;"pypi:sqlalchemy" 则精确到生态
const pkgMatchers = nodes.filter((n) => n?.packages?.length)
.map((n) => ({ id: n.id, globs: n.packages.map((p) => compileGlob(p.includes(':') ? p : `*:${p}`)), hits: 0 }));
const hostMatchers = nodes.filter((n) => n?.hosts?.length)
.map((n) => ({ id: n.id, globs: n.hosts.map((h) => compileGlob(h.toLowerCase())), hits: 0 }));
由此可推出两个实用结论:
- 写
"stripe"会同时匹配npm:stripe和其他生态里的同名包;当你只想匹配某一个生态时,写成"npm:@stripe/*"、"pypi:sqlalchemy"这样的带前缀形式。 hosts的 glob 会被转成小写再匹配,写"*.squareup.com"就能一次覆盖多个子域。
如果配置的 packages 没匹配到任何 import、hosts 没匹配到任何调用,构建会分别警告 packages match nothing imported 和 hosts match no host called from code——这类警告通常意味着包名拼错,或者该依赖其实只在构建期出现、与运行时架构无关。
annotations:把备注钉在文件上
{ "path": "<file>", "note": "…", "tags": ["hotspot"] }
annotations 把一个备注钉在文件上,该文件的 chip 会获得强调描边。文档建议节制使用:留给 god-file、棘手的不变量,或者仍在使用的废弃代码。
校验相当严格:path 和 note 都是必填,且 path 必须是已纳入的文件,否则报错 annotations<a href="https://link.gitcode.com/i/d0e293330afcbaedb494f2aae2015aa7" target="_blank">i].path "…" is not an included file([build.mjs)。写入 graph.json 时只保留 note 与可选的 tags,最终落在文件对象上:
notes.set(a.path, { note: a.note, tags: isStrArr(a.tags) ? a.tags : undefined });
// …
...(notes.has(f.path) ? notes.get(f.path) : {}),
这也解释了为什么一笔备注不需要额外的 descriptions 条目:它直接挂在文件记录上,由阅读器渲染。
flows:可播放的引导走查
{ "id": "checkout", "title": "Checkout", "description": "optional",
"steps": [ { "node": "web.cart", "note": "User clicks Pay" }, { "node": "api.orders", "note": "Creates the order" } ] }
- 每一步的
node是节点 id 或文件路径,至少要有 2 步。 - 阅读器会移动到能同时看见相邻两步的板面,并在它们之间播放信号动画。
note要写成新工程师愿意读的旁白。
约束在源码中体现为:id 与 title 都必需,steps 少于 2 步直接报错;每一步的引用用同一个 resolveEndpoint() 解析(build.mjs)。这意味着 flow 的步骤和 edges 共用一套引用规则,写错的 id 会和边一样得到 did you mean 提示。
除了手写的 flow,地图上用户请求的 flow 会保存到 floorplan/custom-flows.json,并在每次构建时被折进地图:这些步骤使用稳定引用(route:METHOD /path、sym:<file>#<name>),指向已不存在代码的步骤会被丢弃并给出 N step(s) … point at code that no longer exists 的警告(build.mjs)。作为 architecture.json 的作者,你需要知道的是:手写 flow 与用户生成的 flow 会共存,所以 id 要取有辨识度的名字,不要和常见请求词冲突。
recent work:recent-work.json 的另一份契约
floorplan/recent-work.json 是独立文件,每次 make 与 update 都会重写。它记录"刚刚做了什么",并且是对照用户提出的需求来叙述的。阅读器会给这些卡片打上绿色的 "Changed"/"New" 标签,在包含它们的部件上显示 "N changed",把每条 note 加到卡片悬停里,并在 Recent work 按钮和总览面板中列出这些任务。
{
"updatedAt": "2026-10-27T10:30:00Z",
"tasks": [
{
"title": "Refunds from the order page",
"request": "Let staff refund an order straight from its order page",
"summary": "Staff can now refund all or part of an order from its page. The money goes back to the customer's card through Stripe, and they get an email. Refunds over $500 need a manager.",
"items": [
{ "ref": "shop-api/app/services/refunds.py", "change": "new", "note": "New: works out how much to refund, sends it back through Stripe and records it on the order. This is the heart of what was asked." },
{ "ref": "POST /api/orders/<int:order_id>/refund", "change": "new", "note": "The request the Refund button sends; it checks the manager limit before anything is refunded." }
]
}
]
}
| 字段 | 含义 |
|---|---|
tasks[].title |
2–5 个词,用于悬停和板面标题 |
tasks[].request |
用户当时怎么说的(可精简)。显示为 "You asked: …" |
tasks[].summary |
1–2 句:针对该需求做了什么、当前处于什么状态。删除也要在这里说明;被删除的文件不会出现在地图上 |
items[].ref |
文件路径、节点 id,或与地图上完全一致的 "METHOD /path"。不在图上的 ref 会被略过并警告 |
items[].change |
"new" 或 "changed"(默认) |
items[].note |
1–2 句平实的话:这张卡片上改了什么,以及它如何服务于该需求 |
{ "tasks": [] } 会清空所有高亮。
源码层面的行为值得注意(build.mjs):
tasks不是数组时,警告needs a "tasks" array (an empty one clears the highlights);- 缺少
title的任务被跳过; updatedAt缺失时回退为文件 mtime;change只区分"new",其余一律按"changed";- 额外的
status字符串会被透传(build.mjs); - 无法解析的 ref 会被收集成一条警告,最多列出 5 个示例,永远不会让构建失败。
一条 ref 的解析顺序是:n: / f: 前缀 → 节点 id → 已纳入的文件路径 → "METHOD /path"(方法大小写不敏感,会被转成大写)。所以写 get /api/orders 也能命中 GET /api/orders。
完整示例:前后端两个仓库并排
文档给出的完整示例如下,可以直接作为起点改写:
{
"$schema": "floorplan/architecture@1",
"project": {
"name": "Acme Shop",
"summary": "Next.js storefront talking to a Flask API over REST; Postgres for storage; Stripe for payments.",
"entrypoints": ["shop-api/app.py", "shop-web/src/app/page.tsx"]
},
"ignore": ["shop-api/migrations/versions/**"],
"nodes": [
{ "id": "web", "label": "Storefront", "kind": "ui", "summary": "Next.js app router UI.", "paths": ["shop-web/src"] },
{ "id": "web.pages", "parent": "web", "label": "Pages", "kind": "feature", "paths": ["shop-web/src/app"] },
{ "id": "web.api", "parent": "web", "label": "API client", "kind": "library", "summary": "Typed fetch wrapper for every backend call.", "paths": ["shop-web/src/lib/api*"] },
{ "id": "api", "label": "Shop API", "kind": "service", "summary": "Flask REST API.", "paths": ["shop-api/app.py", "shop-api/app"] },
{ "id": "api.routes", "parent": "api", "label": "Routes", "kind": "feature", "paths": ["shop-api/app/routes"] },
{ "id": "api.models", "parent": "api", "label": "Models", "kind": "data", "paths": ["shop-api/app/models"] },
{ "id": "tests", "label": "Tests", "kind": "test", "paths": ["shop-api/tests", "shop-web/**/*.test.{ts,tsx}"] },
{ "id": "pg", "label": "Postgres", "kind": "external", "packages": ["pypi:sqlalchemy", "pypi:psycopg2"] },
{ "id": "stripe", "label": "Stripe", "kind": "external", "packages": ["stripe"], "hosts": ["api.stripe.com"] }
],
"edges": [
{ "from": "stripe", "to": "api.routes", "kind": "http", "label": "webhooks", "description": "payment_intent.succeeded marks orders paid." }
],
"annotations": [
{ "path": "shop-api/app/routes/orders.py", "note": "All order state transitions live here.", "tags": ["hotspot"] }
],
"flows": [
{ "id": "checkout", "title": "Checkout", "steps": [
{ "node": "web.pages", "note": "Cart page submits the order" },
{ "node": "web.api", "note": "POST /api/orders" },
{ "node": "api.routes", "note": "Validates and creates the order" },
{ "node": "stripe", "note": "Creates a PaymentIntent" }
]}
]
}
把它逐条对照前文的规则,可以学到几处写法技巧:
web与api都用宽 glob(shop-web/src、shop-api/app),子节点再用更窄的 glob 切走app、routes、models。因为最深优先,父节点不需要exclude就能把文件让给子节点。shop-web/src/lib/api*用了段内通配,因此api.ts、apiClient.ts之类都会归到web.api。tests是独立的顶层节点,glob 同时覆盖 Python 测试目录和 TS 测试文件命名模式,避免测试文件被服务端节点吞掉、进而掩盖真实依赖结构。pg用生态前缀精确指定,stripe则同时用packages和hosts两条通道,import 和实际 HTTP 调用都能连上。- 唯一手写的边是 Stripe 的 webhook,因为静态分析看不到"外部服务主动调进来"这件事;其余 import 与 HTTP 调用都交给扫描器。
- flow 的每一步都落在能被看见的节点上,
web.pages → web.api → api.routes → stripe正好构成一条从界面到外部服务的完整链路。
校验、报错与修复路径
build.mjs 的报错设计目标是"按字段点名"。除了前文分散提到的检查,还有几类输出需要认识:
✗ 3 error(s) in floorplan/architecture.json:
- nodes[2] ("api.orders").kind "svc" must be one of ui, service, worker, … — did you mean "service"?
- nodes[5] ("api.web").parent "api2" does not exist — did you mean "api"?
- edges[0].to "api/order.ts" is neither a node id nor a scanned file — did you mean "api"?
ASSIGNMENT 812 files (code), 41 unassigned (5.0%)
LINKS 1204 (import 1101, http 78, data 21, event 4)
DESCRIPTIONS files 780/812 · routes 96/101 · modules 34/34
其中 did you mean 由编辑距离实现,阈值是 max(2, len/3),因此短单词也能得到合理建议(build.mjs)。ASSIGNMENT 行会列出每个顶层节点认领的文件数与 LOC,未分配文件还会按目录聚合,方便你决定是加 glob 还是加 ignore:
unassigned, by directory:
18 shop-api/migrations/versions/
DESCRIPTIONS 行统计已描述的文件数、路由数和模块数:任何一个模块缺 summary 都会出现在 modules without a summary 列表里。这也说明 architecture.json 与 descriptions/ 是同一条质量链路的两端——模块级信息在上面,文件级信息在下面,缺一边都能从构建输出里看出来。
三条最实用的排错顺序:
- 先看错误,再看警告。 有 error 时构建直接退出且不写产物;warning 不阻断,但
matches no included file、packages match nothing imported往往是 glob 或包名写错的信号。 - 未分配比例超过 10% 就回填 glob。 输出已经按目录给出数量,优先处理最大的目录。
- 同名平局优先加
exclude而不是堆叠更长的 glob。 平局时"声明在前的获胜",这很容易造成意外归属。
与 blueprint(plan.json)的关系
同一个 architecture.json 格式也用于"还没写的代码":blueprint 用它画一张从 PRD 推演出来的地图。此时结构定义仍在这里,而计划中的文件与端点写在 floorplan/plan.json,由 plan.mjs 转成 build.mjs 需要的扫描结果(blueprint.mjs)。细节见 blueprint.md。
两处交叉校验值得记住:
plan.json里文件的services/data必须写成architecture.json中的 external 节点 id;不在其中会报names "strpe" in its services, which is not a node id in architecture.json — did you mean "stripe"?(build.mjs)。- blueprint 的
Health面板换成了计划的设计评审:谁都能改数据的公开端点、没有决定访问权限的端点、未决问题、没有调用者的端点、没有连线的文件(blueprint.mjs)。测试里能直接看到这套输出,例如medium Anyone could change data through one planned endpoint、low One planned file isn’t connected to anything(blueprint.test.mjs)。
一个可直接参考的真实 blueprint 样例位于 tests/fixtures/blueprint/floorplan/architecture.json:它用 kind: "external" 表示 Postgres、Stripe、Postmark,用一条 stripe → api/src/routes/webhooks.ts 的 http 边表达回执,并给出三步 tour 与一条五步 booking flow。
成文质量清单
写 architecture.json 时,官方工作流给出的量化目标是:3–9 个顶层节点、每个大节点下 3–12 个子节点、glob 覆盖 90% 以上的代码文件、只为扫描器看不见的连接写 edges、1–3 条 flow、3–8 个 tour 站点,并且每个节点都要有 summary(SKILL.md)。边界怎么切见 choosing-boundaries.md:顶层回答"哪些是可部署或可拥有的东西,它们跟谁说话",第二层优先按职责而不是文件类型划分,层级不要超过三层。
最后两点提醒:
- 不要手改
graph.json和index.html,它们每次构建都会重新生成;你的编辑对象只有architecture.json、descriptions/*.json(以及 blueprint 的plan.json)。 tour少于 2 个有效站点时,阅读器会用节点摘要自动拼一条;所以即便你写了tour,也应保证summary本身足够像样,因为它是兜底素材(40-tour.js)。