Drawdown Typed IR · V1 规范
- 版本:0.1
- 状态:Accepted(与
packages/core/src/ir.ts一致) - 对应 RFC:0002 核心语义模型
Typed IR 是 Drawdown 的语义执行核心。文本是入口,图形是投影;IR 不包含坐标、颜色、形状或布局参数。
1. 数据结构
ts
type Id = string;
type TypeRef = string;
type Value =
| string
| number
| boolean
| null
| readonly Value[]
| { readonly [key: string]: Value };
interface ElementRef {
modelId: Id;
elementId: Id;
}
interface Identified {
id: Id;
type: TypeRef;
name?: string;
description?: string;
traits: readonly TypeRef[];
properties: Readonly<Record<string, Value>>;
}
interface Element extends Identified {
kind: "element";
parent?: ElementRef;
}
interface RelationshipEnd {
role: string;
ref: ElementRef;
}
interface Relationship extends Identified {
kind: "relationship";
ends: readonly RelationshipEnd[];
}
interface GraphModel {
kind: "graph";
id: Id;
elements: Readonly<Record<Id, Element>>;
relationships: Readonly<Record<Id, Relationship>>;
}
interface Annotation {
id: Id;
type: TypeRef;
targets: readonly ElementRef[];
body?: { type: "text" | "markdown"; value: string };
motivation?: string;
}
interface SemanticDocument {
formatVersion: string;
documentId: Id;
models: Readonly<Record<Id, GraphModel>>;
annotations?: readonly Annotation[];
}V1 常量:
FORMAT_VERSION = "0.1"- 唯一模型 ID:
main - 默认文档 ID:
document
2. 身份与引用
2.1 ID-001(V1 身份策略)
- 显式
id: "…"属性优先,且在整个文档内唯一。 - 否则使用作用域限定 alias:
- 根作用域:
customer - 嵌套作用域:
backend.api
- 根作用域:
- 命名关系同样使用作用域限定 alias。
- 匿名关系:
~e:<fromId>-><toId>;同一有序端点对出现多次时追加#n(从第二次开始)。 - 编译器生成的临时对象使用
~el:<n>/~rel:<n>/~ann:<n>前缀。
~ 不可能出现在标识符中,因此生成 ID 不会与源码 alias 冲突。
2.2 与目标设计的差距
目标设计要求 Alias 可重命名而不影响 ID。V1 以 alias 作为 ID,因此重命名 alias 会改变身份。需要跨会话稳定身份时必须显式写 id::
draw
api: service "Order API" {
id: "01JABC123"
}这个差距是已知取舍:它让 V1 零配置即可工作,同时保留显式逃生口。完整 ID 映射(编辑器会话映射 / sidecar 持久化)属于 V1.1。
2.3 引用形式
ts
interface ElementRef {
modelId: Id; // V1 固定为 "main"
elementId: Id;
}跨模型与跨文档引用在结构上保留,但 V1 只解析同一模型内引用。
3. 类型引用
3.1 TYPE-001
类型与 Trait 的规范形式是 prefix:name。
归一化规则:
| 写法 | 归一化 | 说明 |
|---|---|---|
service | graph:service | 无前缀 → graph: |
graph:service | graph:service | 已带前缀 |
cloud.aws.lambda | cloud:aws.lambda | 第一个点作为命名空间分隔 |
with critical | graph:critical | Trait 同规则 |
- 已知类型由 CapabilityRegistry 解析;未知类型在 Draft 中警告,在 Strict 中错误。
- 能力(container / connectable / labelable / …)由类型及其父类型合并得到。
3.2 内置类型
core::element、relationship、annotation
graph::node、group、person、application、service、database、queue、external、note、directed-edge、undirected-edge、bidirectional-edge
graph: Traits:critical、deprecated、externally-visible
完整清单以 drawdown schema --json 为准。
4. 关系
4.1 ends 模型
关系不硬编码 source/target:
ts
interface Relationship {
kind: "relationship";
id: string;
type: string; // graph:directed-edge / graph:undirected-edge / ...
name?: string; // 关系标签
ends: RelationshipEnd[];
}- 方向由
type决定,不由 role 名字决定。 - 内置二元关系固定两个 ends:
- directed / bidirectional:
source、target - undirected:
end、end
- directed / bidirectional:
- 端点必须指向 Element(不能指向 Relationship)。
- 端点元素应具备
connectable能力;否则警告W_NOT_CONNECTABLE。
4.2 关系身份
- 命名关系:
read = api -> db: "read"→id = "read"(或作用域路径)。 - 匿名关系:结构 ID,见 ID-001。
- 关系与元素共享 ID 命名空间,全局唯一。
5. 包含
- 通过
Element.parent存储。 - 子元素最多一个父元素。
- 不允许包含环。
- 父元素必须声明
container能力,否则警告。 - 视觉分组不属于 IR;它在 View 层表达。
6. 注解
@name(value) 元数据生成 Annotation:
ts
{
id: "~ann:api#0",
type: "core:annotation",
targets: [{ modelId: "main", elementId: "api" }],
body: { type: "text", value: "use v2" },
motivation: "deprecated"
}- Annotation 不渲染为节点。
- 需要出现在图上的 Note 是
graph:note元素。
7. 编译状态与 Draft
ts
type DraftReference =
| { status: "resolved"; target: ElementRef }
| { status: "unresolved"; symbol: string; scope?: string };
type CompilationState =
| { status: "valid"; document: SemanticDocument; diagnostics }
| { status: "incomplete"; draft: DraftDocument; diagnostics }
| { status: "invalid"; draft: DraftDocument; diagnostics };valid:全部引用可解析,无 Error。incomplete:无 Error,但存在未解析引用(Draft 警告)。invalid:存在 Error;仍返回 Draft,供编辑器和 Agent 继续工作。
Draft 与 Valid 的结构差异只有一处:引用是 DraftReference 而不是 ElementRef。
8. 不变量
一个 valid 文档必须满足:
- 文档内 ID 唯一。
- 每个
Elements的parent指向存在的 Element。 - 包含关系无环。
- 每个 Relationship 恰好两个 ends。
- 每个 end 指向存在的 Element。
- 所有类型与 Trait 已在注册表中(Strict)。
formatVersion可识别。- 不存在未解析引用。
Draft 文档可以违反 2、5、8,但必须携带对应诊断。
9. 示例
graph { A -> B } 的 IR:
json
{
"formatVersion": "0.1",
"documentId": "document",
"models": {
"main": {
"kind": "graph",
"id": "main",
"elements": {
"A": {
"kind": "element",
"id": "A",
"type": "graph:node",
"name": "A",
"traits": [],
"properties": {}
},
"B": {
"kind": "element",
"id": "B",
"type": "graph:node",
"name": "B",
"traits": [],
"properties": {}
}
},
"relationships": {
"~e:A->B": {
"kind": "relationship",
"id": "~e:A->B",
"type": "graph:directed-edge",
"traits": [],
"properties": {},
"ends": [
{
"role": "source",
"ref": { "modelId": "main", "elementId": "A" }
},
{
"role": "target",
"ref": { "modelId": "main", "elementId": "B" }
}
]
}
}
}
}
}10. Revision
- Revision = 规范化 JSON 的 SHA-256。
- 规范化:对象 key 递归排序;数组保序;忽略
undefined。 applyOperations接收baseRevision,不匹配返回E_REVISION_CONFLICT。- 相同 IR 必然得到相同 revision;属性插入顺序不影响结果。
11. V1 已实现 / 未实现
| 项目 | 状态 |
|---|---|
| Element / Relationship / ends / parent | 已实现 |
| ID-001、TYPE-001 | 已实现 |
| Draft / Strict / 三态 | 已实现 |
| Annotation | 已实现 |
| 不变量校验 | 已实现(除 JSON Schema 属性校验) |
| JSON Schema 属性校验 | Deferred |
| Port / n 元关系 | Deferred |
| 稳定 ID 映射 / 跨文档引用 | Deferred |