科研技能库/FigureSpec:确定性图表生成器
图表可视化
未发现用户侧风险

FigureSpec:确定性图表生成器

通过结构化JSON(FigureSpec)生成确定性的、出版质量的架构图、工作流和管道图,输出为可编辑的SVG矢量图。当需要精确、可编辑、出版就绪的矢量图时使用,适用于正式架构/工作流图。

文件预览

2 个文件
scripts
SKILL.md
10.5 KB · 可预览
---
name: figure-spec
description: "Generate deterministic publication-quality architecture, workflow, and pipeline diagrams from structured JSON (FigureSpec) into editable SVG. Use when user says \"架构图\", \"workflow 图\", \"pipeline 图\", \"确定性矢量图\", \"figure spec\", \"draw architecture\", or needs precise, editable, publication-ready vector diagrams. Preferred over AI illustration for formal architecture/workflow figures."
argument-hint: [description-of-diagram]
allowed-tools: Bash(*), Read, Write, Edit
---

# FigureSpec: Deterministic JSON → SVG Figure Generation

Generate publication-quality **architecture diagrams**, **workflow pipelines**, **audit cascades**, and **system topology** figures as editable SVG vector graphics using a deterministic JSON → SVG renderer.

## When to Use This Skill

**Use `figure-spec`** for:
- System architecture diagrams (layered, hub-and-spoke, multi-plane)
- Workflow / pipeline figures
- Audit cascade / flow-control diagrams
- Any structured diagram where node positions, connections, and groupings are semantically important
- Figures that need to be edited/tweaked later (SVG is plain text)
- Figures where determinism matters (same spec → same SVG)

**Do NOT use for:**
- Data plots (bar/line/scatter) — use `/paper-figure`
- Natural/qualitative illustrations — use `/paper-illustration`
- Quick state-machine / flowchart — use `/mermaid-diagram` (lighter syntax)

## Core Properties

- **Deterministic**: identical FigureSpec JSON always produces identical SVG output (for a fixed renderer version + fonts)
- **Editable**: SVG output is plain-text, can be post-edited by hand or programmatically
- **Validated**: renderer enforces schema, rejects malformed specs with clear error messages
- **Shape-aware**: edge clipping works correctly for rect/rounded/circle/ellipse/diamond
- **CJK support**: multi-line labels with proper Chinese character width estimation
- **No external API**: runs fully local, no network, no API keys

## Tool Location

Phase 3.1 (Arch C) move: the canonical implementation now lives at
`skills/figure-spec/scripts/figure_renderer.py` (this SKILL's own
`scripts/` subdirectory). A backwards-compatible shim at
`tools/figure_renderer.py` forwards to the canonical file via
`os.execv`, so existing users with `.aris/tools/figure_renderer.py`
or a manually copied `tools/figure_renderer.py` keep working
unchanged.

Resolve `$FIGURE_RENDERER` with the hybrid chain (layer 0 prefers the
self-contained location for the owning SKILL; layers 1-3 are the
shared-runtime chain documented in
[`shared-references/integration-contract.md`](../shared-references/integration-contract.md) §2,
Policy A — skill-local gate):

```bash
# Layer 0: self-contained (CC 1.0+ exposes $CLAUDE_SKILL_DIR).
FIGURE_RENDERER=""
if [ -n "${CLAUDE_SKILL_DIR:-}" ] && [ -f "$CLAUDE_SKILL_DIR/scripts/figure_renderer.py" ]; then
  FIGURE_RENDERER="$CLAUDE_SKILL_DIR/scripts/figure_renderer.py"
fi
# Layers 1-3: shared-runtime chain (legacy compatibility + non-CC hosts).
if [ -z "$FIGURE_RENDERER" ]; then
  cd "$(git rev-parse --show-toplevel 2>/dev/null || pwd)" || exit 1
  if [ -z "${ARIS_REPO:-}" ] && [ -f .aris/installed-skills.txt ]; then
      ARIS_REPO=$(awk -F'\t' '$1=="repo_root"{print $2; exit}' .aris/installed-skills.txt 2>/dev/null) || true
  fi
  FIGURE_RENDERER=".aris/tools/figure_renderer.py"
  [ -f "$FIGURE_RENDERER" ] || FIGURE_RENDERER="tools/figure_renderer.py"
  [ -f "$FIGURE_RENDERER" ] || { [ -n "${ARIS_REPO:-}" ] && FIGURE_RENDERER="$ARIS_REPO/tools/figure_renderer.py"; }
  [ -f "$FIGURE_RENDERER" ] || FIGURE_RENDERER=""
fi
[ -z "$FIGURE_RENDERER" ] && {
  echo "ERROR: figure_renderer.py not resolved (layer 0: \$CLAUDE_SKILL_DIR/scripts/; layers 1-3: .aris/tools/, tools/, \$ARIS_REPO/tools/)." >&2
  echo "       /figure-spec cannot produce SVG output. Fix: rerun bash tools/install_aris.sh, or copy the helper from \$ARIS_REPO/skills/figure-spec/scripts/." >&2
  exit 1
}
```

Invoke:

```bash
python3 "$FIGURE_RENDERER" render <spec.json> --output <out.svg>
python3 "$FIGURE_RENDERER" validate <spec.json>
python3 "$FIGURE_RENDERER" schema
```

## Workflow

### Step 1: Understand the Diagram Goal

From `$ARGUMENTS` (description or path to `PAPER_PLAN.md` / `NARRATIVE_REPORT.md`), identify:
- **Purpose**: architecture, workflow, pipeline, audit cascade, topology?
- **Main entities**: what are the boxes?
- **Relationships**: how do they connect? (uses, produces, calls, verifies, chains)
- **Grouping**: do entities cluster into named regions?
- **Hierarchy vs network**: stacked layers, left-to-right flow, or central hub?

### Step 2: Draft the FigureSpec JSON

Canvas sizing guide:
- Single-column figure: ~500×350 px
- Two-column (full-width): ~900×500 px
- Tall topology: ~700×700 px

Start from a template based on the diagram type:

**Architecture (stacked rows)**:
```json
{
  "canvas": {"width": 900, "height": 520},
  "nodes": [
    {"id": "layer1_label", "label": "Layer 1", "x": 450, "y": 60, ...},
    {"id": "node_a", "label": "A", "x": 180, "y": 120, ...},
    {"id": "node_b", "label": "B", "x": 350, "y": 120, ...}
  ],
  "edges": [...],
  "groups": [
    {"label": "Layer 1", "node_ids": ["node_a", "node_b"], "fill": "#F0F9FF", "stroke": "#BAE6FD"}
  ]
}
```

**Workflow (left-to-right chain)**:
```json
{
  "canvas": {"width": 900, "height": 300},
  "nodes": [
    {"id": "step1", "label": "Step 1", "x": 100, "y": 150, "shape": "rounded"},
    {"id": "step2", "label": "Step 2", "x": 280, "y": 150, "shape": "rounded"}
  ],
  "edges": [
    {"from": "step1", "to": "step2", "label": "produces"}
  ]
}
```

**Decision diamond**:
```json
{"id": "check", "label": "Passes?", "shape": "diamond", "x": 450, "y": 200}
```

### Step 3: Render and Validate

```bash
# Validate first ($FIGURE_RENDERER was resolved in "Tool Location" above)
python3 "$FIGURE_RENDERER" validate /tmp/spec.json

# Render to SVG
python3 "$FIGURE_RENDERER" render /tmp/spec.json --output figures/fig_arch.svg

# Convert to PDF for LaTeX inclusion
rsvg-convert -f pdf figures/fig_arch.svg -o figures/fig_arch.pdf
```

If validation fails, inspect the error (missing field, duplicate ID, overlap warning, invalid hex color) and fix the JSON.

### Step 4: Visual Review

Open the SVG/PDF and check:
- **No overlaps**: nodes don't collide with each other or group boundaries
- **Readability**: font sizes are consistent, labels aren't clipped
- **Edge clarity**: arrows hit nodes at clean angles, labels near edges are legible
- **Group alignment**: background rectangles frame their members cleanly
- **Color distinction**: categories are visually distinct in both color and grayscale

If issues found, edit the JSON spec (never the generated SVG) and re-render.

### Step 5: Iterate with Codex Review (Optional, for High-Stakes Figures)

For paper architecture figures, invoke cross-model review:

```
mcp__codex__codex:
  model: gpt-5.5
  config: {"model_reasoning_effort": "xhigh"}
  prompt: |
    Review this SVG figure for a technical paper (architecture / workflow diagram).

    Spec file: /path/to/spec.json
    Rendered: /path/to/fig.svg

    Evaluate:
    1. Clarity (C): can a reader understand the system from this figure alone?
    2. Readability (R): font sizes, label placement, visual hierarchy
    3. Semantic accuracy (S): do relationships match the described system?

    Score each axis 1-10 and list specific issues to fix.
```

Iterate until all three axes ≥ 7/10. The ARIS tech report figures went through 5 rounds of this loop to reach C:7/R:7/S:8.

## Schema Quick Reference

Run `python3 "$FIGURE_RENDERER" schema` (resolve $FIGURE_RENDERER per "Tool Location" above) for the authoritative schema.

### Nodes

| Field | Required | Default | Notes |
|-------|----------|---------|-------|
| `id` | ✓ | — | Unique |
| `label` | ✓ | — | `\n` for multi-line |
| `x`, `y` | ✓ | — | Center coordinates |
| `width`, `height` | | 120, 50 | |
| `shape` | | `rounded` | `rect` / `rounded` / `circle` / `ellipse` / `diamond` |
| `fill`, `stroke` | | auto from palette | `#RRGGBB` |
| `text_color` | | `#333333` | |
| `font_size` | | 14 | Override style default |

### Edges

| Field | Default | Notes |
|-------|---------|-------|
| `from`, `to` | required | Same = self-loop |
| `label` | — | Short edge label |
| `style` | `solid` | `solid` / `dashed` / `dotted` |
| `color` | `#555555` | |
| `curve` | `false` | Curved path |

### Groups

Rectangular background regions framing a set of nodes:
```json
{"label": "Layer Name", "node_ids": ["a", "b", "c"], "fill": "#EFF6FF", "stroke": "#BFDBFE"}
```

## Design Patterns

### Pattern 1: Layered Architecture
Stack rows of related nodes, each row is a group, add inter-layer arrows with semantic labels (`uses↓`, `produces↑`, `checks↓`).

### Pattern 2: Hub-and-Spoke
Central node (e.g., Executor), peripheral nodes (skills, tools), solid arrows for primary relations, dashed for feedback.

### Pattern 3: Pipeline with Feedback
Left-to-right main flow, feedback arrows curve below with `curve: true`.

### Pattern 4: Audit Cascade
Three-stage horizontal cascade with inputs feeding in from top, outputs exiting right, each stage in its own group.

## Anti-Patterns

- **Don't use groups as hierarchy**: groups frame peer nodes, not containment
- **Don't nest groups**: renderer draws them as background rectangles; nested groups look like Russian dolls
- **Don't cross-draw long diagonals**: if an arrow crosses 3+ rows, rethink the layout
- **Don't mix font sizes for same role**: keep one size per node category

## Output Contract

- SVG file in `figures/` (vector, editable, hand-tweakable)
- Source FigureSpec JSON saved in `figures/specs/` for reproducibility
- PDF version via `rsvg-convert` for LaTeX inclusion

## Integration with Other Skills

- **`/paper-writing`** (Workflow 3): when `illustration: figurespec` (default for architecture figures), this skill handles Phase 2b
- **`/paper-figure`**: handles data plots; they complement each other (data + architecture = complete figure set)
- **`/paper-illustration`**: fallback for figures that need natural/qualitative style (method illustrations with photos, qualitative result grids)
- **`/mermaid-diagram`**: lighter alternative for simple flowcharts

## Review Tracing

After each `mcp__codex__codex` or `mcp__codex__codex-reply` reviewer call, save the trace following `shared-references/review-tracing.md` (Policy C — forensic; never silently skip). Use `save_trace.sh` (resolved per the chain in `shared-references/integration-contract.md` §2) or write files directly to `.aris/traces/<skill>/<date>_run<NN>/`. Respect the `--- trace:` parameter (default: `full`).

SKILL.md

元数据
namefigure-spec
description通过结构化JSON(FigureSpec)生成确定性的、出版质量的架构图、工作流图和管道图,输出为可编辑的SVG矢量图。当用户输入“架构图”、“workflow 图”、“pipeline 图”、“确定性矢量图”、“figure spec”、“draw architecture”时,或需要精确、可编辑、出版就绪的矢量图时使用。对于正式的架构/工作流图,优先使用此工具而非AI绘图。
argument-hint["图表的描述"]
allowed-toolsBash(*), Read, Write, Edit

FigureSpec:确定性 JSON → SVG 图表生成

使用确定性 JSON → SVG 渲染器生成可出版质量的架构图、工作流管道、审计级联图和系统拓扑图,输出为可编辑的 SVG 矢量图形。

何时使用此技能

适用于 figure-spec 的场景:

  • 系统架构图(分层、星型、多平面)
  • 工作流 / 管道图
  • 审计级联 / 流程控制图
  • 节点位置、连接和分组在语义上重要的任何结构化图表
  • 需要后续编辑/调整的图表(SVG 为纯文本)
  • 需要确定性的图表(相同规格 → 相同 SVG)

不适用于:

  • 数据绘图(柱状/折线/散点)—— 使用 /paper-figure
  • 自然/定性插图 —— 使用 /paper-illustration
  • 快速状态机 / 流程图 —— 使用 /mermaid-diagram(更轻量的语法)

核心特性

  • 确定性:相同的 FigureSpec JSON 总是产生相同的 SVG 输出(在固定的渲染器版本和字体下)
  • 可编辑:SVG 输出为纯文本,可手动编辑或通过程序后处理
  • 已验证:渲染器强制执行模式,拒绝格式错误的规格并给出清晰的错误消息
  • 形状感知:针对 rect/rounded/circle/ellipse/diamond 的边缘裁剪正确处理
  • 中日韩文字支持:多行标签,并正确估算中文字符宽度
  • 无外部 API:完全本地运行,无需联网,无需 API 密钥

工具位置

第 3.1 阶段(Arch C)迁移:规范实现现在位于 skills/figure-spec/scripts/figure_renderer.py(此 SKILL 本身的 scripts/ 子目录)。位于 tools/figure_renderer.py 的向后兼容垫片通过 os.execv 转发到规范文件,因此使用 .aris/tools/figure_renderer.py 或手动复制的 tools/figure_renderer.py 的现有用户可保持不变。

使用混合链解析 $FIGURE_RENDERER(第 0 层优先选择 拥有该 SKILL 的自包含位置;第 1-3 层为共享运行时链,详见 shared-references/integration-contract.md §2, 策略 A — 技能本地门):

bash
# 第 0 层:自包含(CC 1.0+ 暴露 $CLAUDE_SKILL_DIR)。
FIGURE_RENDERER=""
if [ -n "${CLAUDE_SKILL_DIR:-}" ] && [ -f "$CLAUDE_SKILL_DIR/scripts/figure_renderer.py" ]; then
  FIGURE_RENDERER="$CLAUDE_SKILL_DIR/scripts/figure_renderer.py"
fi
# 第 1-3 层:共享运行时链(遗留兼容 + 非 CC 主机)。
if [ -z "$FIGURE_RENDERER" ]; then
  cd "$(git rev-parse --show-toplevel 2>/dev/null || pwd)" || exit 1
  if [ -z "${ARIS_REPO:-}" ] && [ -f .aris/installed-skills.txt ]; then
      ARIS_REPO=$(awk -F'\t' '$1=="repo_root"{print $2; exit}' .aris/installed-skills.txt 2>/dev/null) || true
  fi
  FIGURE_RENDERER=".aris/tools/figure_renderer.py"
  [ -f "$FIGURE_RENDERER" ] || FIGURE_RENDERER="tools/figure_renderer.py"
  [ -f "$FIGURE_RENDERER" ] || { [ -n "${ARIS_REPO:-}" ] && FIGURE_RENDERER="$ARIS_REPO/tools/figure_renderer.py"; }
  [ -f "$FIGURE_RENDERER" ] || FIGURE_RENDERER=""
fi
[ -z "$FIGURE_RENDERER" ] && {
  echo "ERROR: figure_renderer.py not resolved (第 0 层: \$CLAUDE_SKILL_DIR/scripts/; 第 1-3 层: .aris/tools/, tools/, \$ARIS_REPO/tools/)." >&2
  echo "       /figure-spec 无法生成 SVG 输出。修复方法:重新运行 bash tools/install_aris.sh,或从 \$ARIS_REPO/skills/figure-spec/scripts/ 复制辅助脚本。" >&2
  exit 1
}

调用:

bash
python3 "$FIGURE_RENDERER" render <spec.json> --output <out.svg>
python3 "$FIGURE_RENDERER" validate <spec.json>
python3 "$FIGURE_RENDERER" schema

工作流程

第 1 步:理解图表目标

从 $ARGUMENTS(描述或 PAPER_PLAN.md / NARRATIVE_REPORT.md 路径)中识别:

  • 目的:架构图、工作流、管道、审计级联还是拓扑?
  • 主要实体:有哪些方框?
  • 关系:它们如何连接?(使用、产生、调用、验证、链接)
  • 分组:实体是否聚合成命名的区域?
  • 层次结构与网络:堆叠的层、从左到右的流程还是中心枢纽?

第 2 步:起草 FigureSpec JSON

画布尺寸指南:

  • 单栏插图:~500×350 像素
  • 双栏(整版宽):~900×500 像素
  • 高拓扑图:~700×700 像素

根据图表类型选择模板:

架构图(分层行):

json
{
  "canvas": {"width": 900, "height": 520},
  "nodes": [
    {"id": "layer1_label", "label": "图层 1", "x": 450, "y": 60, ...},
    {"id": "node_a", "label": "A", "x": 180, "y": 120, ...},
    {"id": "node_b", "label": "B", "x": 350, "y": 120, ...}
  ],
  "edges": [...],
  "groups": [
    {"label": "图层 1", "node_ids": ["node_a", "node_b"], "fill": "#F0F9FF", "stroke": "#BAE6FD"}
  ]
}

工作流(从左到右的链条):

json
{
  "canvas": {"width": 900, "height": 300},
  "nodes": [
    {"id": "step1", "label": "步骤 1", "x": 100, "y": 150, "shape": "rounded"},
    {"id": "step2", "label": "步骤 2", "x": 280, "y": 150, "shape": "rounded"}
  ],
  "edges": [
    {"from": "step1", "to": "step2", "label": "产生"}
  ]
}

决策菱形:

json
{"id": "check", "label": "通过?", "shape": "diamond", "x": 450, "y": 200}

第 3 步:渲染与验证

bash
# 首先验证($FIGURE_RENDERER 已在“工具位置”中解析)
python3 "$FIGURE_RENDERER" validate /tmp/spec.json

# 渲染为 SVG
python3 "$FIGURE_RENDERER" render /tmp/spec.json --output figures/fig_arch.svg

# 转换为 PDF 以包含在 LaTeX 中
rsvg-convert -f pdf figures/fig_arch.svg -o figures/fig_arch.pdf

如果验证失败,检查错误原因(缺少字段、重复 ID、重叠警告、无效的十六进制颜色)并修正 JSON。

第 4 步:视觉审查

打开 SVG/PDF 并检查:

  • 无重叠:节点不与彼此或分组边界碰撞
  • 可读性:字体大小一致,标签未被裁剪
  • 边缘清晰:箭头以清晰的角度与节点相交,边缘附近的标签清晰可读
  • 分组对齐:背景矩形干净地框住其成员
  • 颜色区分:类别在彩色和灰度下视觉上都能明显区分

如果发现问题,编辑 JSON 规格(而不是生成的 SVG)并重新渲染。

第 5 步:使用 Codex 审查迭代(可选,针对重要图表)

对于论文架构图,调用跨模型审查:

text
mcp__codex__codex:
  model: gpt-5.5
  config: {"model_reasoning_effort": "xhigh"}
  prompt: |
    审查此技术论文(架构/工作流图)的 SVG 图。

    规格文件:/path/to/spec.json
    渲染结果:/path/to/fig.svg

    评估:
    1. 清晰度 (C):读者仅凭此图能否理解系统?
    2. 可读性 (R):字体大小、标签位置、视觉层次
    3. 语义准确性 (S):关系是否与描述的系统相符?

    为每个轴评分(1-10),并列出需要修复的具体问题。

迭代直到所有三个轴≥ 7/10。ARIS 技术报告中的图经过了 5 轮这样的循环才达到 C:7/R:7/S:8。

模式快速参考

运行 python3 "$FIGURE_RENDERER" schema(根据“工具位置”解析 $FIGURE_RENDERER)以获取权威模式。

节点

字段必填默认值说明
id✓—唯一
label✓—使用 \n 实现多行
x, y✓—中心坐标
width, height120, 50
shaperoundedrect / rounded / circle / ellipse / diamond
fill, stroke从调色板自动分配#RRGGBB
text_color#333333
font_size14覆盖样式默认值

边

字段默认值说明
from, to必填相同则为自环
label—短的边标签
stylesolidsolid / dashed / dotted
color#555555
curvefalse弯曲路径

分组

矩形背景区域,框住一组节点:

json
{"label": "层名称", "node_ids": ["a", "b", "c"], "fill": "#EFF6FF", "stroke": "#BFDBFE"}

设计模式

模式 1:分层架构

堆叠行的相关节点,每行是一个组,添加层间箭头并带有语义标签(使用↓、产生↑、检查↓)。

模式 2:星型结构

中心节点(例如执行器),外围节点(技能、工具),实线箭头表示主要关系,虚线表示反馈。

模式 3:带反馈的管道

从左到右的主要流程,反馈箭头在下方弯曲,curve: true。

模式 4:审计级联

三阶段水平级联,输入从顶部汇入,输出从右侧流出,每个阶段有自己的分组。

反模式

  • 不要将分组用作层次结构:分组框住同级节点,而不是包含关系
  • 不要嵌套分组:渲染器将它们绘制为背景矩形;嵌套的分组看起来像俄罗斯套娃
  • 不要使用长对角线:如果箭头穿过 3 行或更多,请重新考虑布局
  • 不要为相同角色混用字体大小:同一节点类别保持统一大小

输出约定

  • SVG 文件保存在 figures/(矢量,可编辑,可手动修改)
  • 源 FigureSpec JSON 保存在 figures/specs/ 以实现可复现性
  • PDF 版本通过 rsvg-convert 生成,用于 LaTeX 包含

与其他技能集成

  • /paper-writing(工作流 3):当 illustration: figurespec(架构图默认)时,此技能处理阶段 2b
  • /paper-figure:处理数据绘图;它们互补(数据 + 架构 = 完整的图表集)
  • /paper-illustration:需要自然/定性风格时的备选方案(带照片的方法插图、定性结果网格)
  • /mermaid-diagram:简单流程图更轻量的替代方案

审查追溯

每次调用 mcp__codex__codex 或 mcp__codex__codex-reply 审查器后,按照 shared-references/review-tracing.md(策略 C — 取证;绝不静默跳过)保存追踪。使用 save_trace.sh(根据 shared-references/integration-contract.md §2 中的链解析)或将文件直接写入 .aris/traces/<skill>/<date>_run<NN>/。遵守 --- trace: 参数(默认值:full)。