merge-rules
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMerge Rules
规则合并
Merges from multiple projects into a unified portable rule set (.md + .examples.md). Promotes patterns that appear across a threshold of projects by converting them to Principles format. Merges files alongside rule files.
.claude/rules/.local.md.examples.md将多个项目的目录内容合并为统一的可移植规则集(.md + .examples.md)。将出现在超过阈值数量项目中的模式转换为Principles格式,同时合并规则文件对应的文件。
.claude/rules/.local.md.examples.mdUsage
使用方法
text
/merge-rules # Merge using config file
/merge-rules --config <path> # Merge using specified config file
/merge-rules --dry-run # Show what would be merged without writingtext
/merge-rules # 基于配置文件执行合并
/merge-rules --config <path> # 使用指定的配置文件执行合并
/merge-rules --dry-run # 预览合并结果,不写入文件Configuration
配置
Config file search order:
- argument
--config <path> - (project-level)
.claude/merge-rules.local.md - (user-level)
~/.claude/merge-rules.local.md
File format: YAML frontmatter only (no markdown body), same convention as .
extract-rules.local.mdyaml
---配置文件搜索优先级:
- 参数指定的文件
--config <path> - (项目级别)
.claude/merge-rules.local.md - (用户级别)
~/.claude/merge-rules.local.md
文件格式: 仅包含YAML frontmatter(无markdown正文),与的约定一致。
extract-rules.local.mdyaml
---Source projects (each must have extract-rules output)
源项目列表(每个项目都需要有extract-rules的输出)
projects:
- ~/projects/frontend-app
- ~/projects/backend-api
- ~/projects/shared-lib
projects:
- ~/projects/frontend-app
- ~/projects/backend-api
- ~/projects/shared-lib
Output directory (default: .claude/rules/)
输出目录(默认:.claude/rules/)
output_dir: .claude/rules/
output_dir: .claude/rules/
Rules directory within each project (default: .claude/rules/)
每个项目内的规则目录(默认:.claude/rules/)
Corresponds to extract-rules' output_dir setting
对应extract-rules的output_dir配置
rules_dir: .claude/rules/
rules_dir: .claude/rules/
Threshold for promoting .local.md patterns (default: 0.5 = majority)
.local.md模式的升级阈值(默认:0.5 = 过半数)
Examples: 3 projects → 2/3 needed, 4 projects → 3/4, 5 projects → 3/5
示例:3个项目 → 需要出现在2个项目中,4个项目 → 需要出现在3个项目中,5个项目 → 需要出现在3个项目中
promote_threshold: 0.5
promote_threshold: 0.5
Report language (default: ja)
报告语言(默认:ja)
language: ja
undefinedlanguage: ja
undefinedProcessing Flow
处理流程
Step 1: Load Configuration
第1步:加载配置
- Search for config file (see search order above)
- If not found: Error "No config file found. Create or specify with
.claude/merge-rules.local.md."--config
- If not found: Error "No config file found. Create
- Parse YAML frontmatter, apply defaults for omitted fields
- resolution order: Skill config → Claude Code settings (
language→~/.claude/settings.jsonfield) → defaultlanguageja
- Validate:
- must have at least 2 entries
projects - Each project path must exist and contain
rules_dir - Error with clear message if validation fails
- 按照上述优先级搜索配置文件
- 未找到则报错:"No config file found. Create or specify with
.claude/merge-rules.local.md."--config
- 未找到则报错:"No config file found. Create
- 解析YAML frontmatter,对未指定的字段应用默认值
- 解析优先级: Skill配置 → Claude Code设置(
language的~/.claude/settings.json字段)→ 默认值languageja
- 校验:
- 配置至少需要2个条目
projects - 每个项目路径必须存在且包含指定的目录
rules_dir - 校验失败则返回清晰的错误提示
Step 2: Collect Rule Files
第2步:收集规则文件
For each project:
- Find all ,
.md, and.local.mdfiles under.examples.md(recursive){path}/{rules_dir}/ - Categorize:
- → portable principles (always merge). If the file also contains
languages/*.md(hybrid format from## Project-specific patterns), treat patterns as promotion candidates (same assplit_output: false).local.md - → same as above
frameworks/*.md - → same as above
integrations/*.md - → promotion candidate
languages/*.local.md - → promotion candidate
frameworks/*.local.md - → promotion candidate
integrations/*.local.md - → example file (merge with rules)
languages/*.examples.md - → example file (merge with rules)
frameworks/*.examples.md - → example file (merge with rules)
integrations/*.examples.md - → skip (inherently project-specific)
project.md - → skip (inherently project-specific)
project.examples.md
- Parse each file: extract YAML frontmatter () and body sections (
paths:,## Principles,## Project-specific patterns,## Principles Examples)## Project-specific Examples
对每个项目执行以下操作:
- 递归查找下所有
{路径}/{rules_dir}/、.md和.local.md文件.examples.md - 分类处理:
- → 可移植原则(始终合并)。如果文件同时包含
languages/*.md(## Project-specific patterns生成的混合格式),则将其中的模式视为待升级候选(与split_output: false处理逻辑一致).local.md - → 同上
frameworks/*.md - → 同上
integrations/*.md - → 待升级候选
languages/*.local.md - → 待升级候选
frameworks/*.local.md - → 待升级候选
integrations/*.local.md - → 示例文件(随规则合并)
languages/*.examples.md - → 示例文件(随规则合并)
frameworks/*.examples.md - → 示例文件(随规则合并)
integrations/*.examples.md - → 跳过(本身属于项目专属内容)
project.md - → 跳过(本身属于项目专属内容)
project.examples.md
- 解析每个文件:提取YAML frontmatter(字段)和正文部分(
paths:、## Principles、## Project-specific patterns、## Principles Examples)## Project-specific Examples
Step 3: Normalize Similar File Names
第3步:相似文件名归一化
Before merging, group files that refer to the same concept but have different names. This applies to , , and files — a and its corresponding and share the same normalization (e.g., , , and are normalized together with their variants).
.md.local.md.examples.md.md.local.md.examples.mdrails-controller.mdrails-controller.local.mdrails-controller.examples.mdrails-controllers.*- Detect similar file names within the same directory (e.g., vs
rails-controller.md,rails-controllers.mdvsrails-model.md)rails-models.md- Singular/plural variants (e.g., /
controller)controllers - Minor naming differences for the same concept (use AI judgment based on file content and frontmatter overlap)
paths:
- Singular/plural variants (e.g.,
- For each group of similar files, select a canonical name:
- Prefer the name used by the majority of projects
- If tied, prefer the name matching extract-rules' layered framework convention (e.g., )
<framework>-<layer>
- Treat grouped files as the same file for subsequent merge steps (Step 4 and Step 5)
- Report normalized groups in the summary (e.g., "+
rails-controller.md→rails-controllers.md")rails-controllers.md
合并前,将指向同一概念但名称不同的文件归为一组,该规则适用于、和文件——文件与其对应的、文件共享相同的归一化逻辑(例如、和会与其变体一起归一化处理)。
.md.local.md.examples.md.md.local.md.examples.mdrails-controller.mdrails-controller.local.mdrails-controller.examples.mdrails-controllers.*- 识别同一目录下的相似文件名(例如与
rails-controller.md、rails-controllers.md与rails-model.md)rails-models.md- 单复数变体(例如/
controller)controllers - 同一概念的微小命名差异(基于文件内容和frontmatter的重叠度,通过AI判断)
paths:
- 单复数变体(例如
- 为每组相似文件选择一个规范名称:
- 优先选择多数项目使用的名称
- 平票时优先选择符合extract-rules分层框架约定的名称(例如格式)
<framework>-<layer>
- 在后续合并步骤(第4步和第5步)中,同组文件将被视为同一个文件处理
- 在汇总报告中展示归一化的分组(例如:"+
rails-controller.md→rails-controllers.md")rails-controllers.md
Step 4: Merge Portable Rules (.md)
第4步:合并可移植规则(.md)
Design note: Once a pattern is promoted to a Principle (via Step 5), it becomes a permanent org-level rule. Subsequent merge-rules runs will preserve it through Step 4's principle deduplication, regardless of whether the original pattern still meets the promotion threshold. To demote or remove a promoted Principle, manually edit the org rules output.
.local.mdFor each unique (normalized) file name across projects (e.g., , ):
languages/typescript.mdintegrations/rails-inertia.md- Collect all versions from projects that have this file (including normalized variants)
- Merge sections:
## Principles- Deduplicate by principle name (text before parenthetical hints)
- Union hints from all projects for the same principle
- If same principle name but clearly different meaning → keep both, flag in report (see Conflict Handling)
- Preserve unique principles from any project
- Merge frontmatter: union of all path patterns, deduplicate
paths: - If file exists in only 1 project, include as-is
设计说明: 模式通过第5步升级为Principle后,将成为永久的组织级规则。后续执行merge-rules时,会通过第4步的原则去重逻辑保留该规则,无论原模式是否仍满足升级阈值。如需降级或删除已升级的Principle,请手动编辑组织规则输出文件。
.local.md对所有项目中每个唯一(归一化后)的文件名(例如、):
languages/typescript.mdintegrations/rails-inertia.md- 收集所有包含该文件(包括归一化变体)的项目对应的版本
- 合并部分:
## Principles- 按原则名称(括号内提示前的文本)去重
- 合并同一原则下所有项目的提示信息并去重
- 如果原则名称相同但含义明显不同 → 同时保留,在报告中标记(见冲突处理)
- 保留所有项目的独有原则
- 合并frontmatter:合并所有路径模式并去重
paths: - 如果仅1个项目包含该文件,直接保留原内容
Step 5: Promote .local.md Patterns to Principles
第5步:将.local.md模式升级为Principles
For each normalized category (e.g., , , ):
languages/typescriptframeworks/rails-controllersintegrations/rails-inertia- Collect from all projects — from
## Project-specific patternsfiles and from hybrid.local.mdfiles that contain this section (see Step 2).md - Deduplicate against existing Principles: Exclude patterns whose description (text after ) semantically matches an existing principle name in the corresponding
-output (from Step 4). Use AI judgment for semantic equivalence (case-insensitive, synonyms). This prevents self-amplification when.mdcontains patterns previously promoted by older versions.local.md - Match remaining patterns by inline code signature (backtick portion before )
-- Use AI judgment to determine semantic equivalence (e.g., and
useAuth()refer to the same pattern)useAuth() → { user, login, logout }
- Use AI judgment to determine semantic equivalence (e.g.,
- Count occurrences per pattern across projects
- Calculate threshold: pattern must appear in more than projects (i.e., strict majority when threshold = 0.5)
len(projects) * promote_threshold - Convert to Principles format and append to in the corresponding normalized
## Principlesoutput:.md- Signature format: → Principles format:
`signature` - descriptionDescription (simplified signature) - The description becomes the principle name, the function/type name from the signature becomes the hint
- Examples:
- →
`useAuth() → { user, login, logout }` - auth hook interfaceAuth hook interface (useAuth) - →
`clean_bracket_params(:keyword)` - WAF付加のブラケット除去WAF付加のブラケット除去 (clean_bracket_params) - →
`RefOrNull<T extends { id: string }> = T | { id: null }` - nullable refsNullable refs (RefOrNull<T>)
- Apply Step 4's principle deduplication to the converted principles (skip if same principle name already exists)
- Signature format:
- Patterns below threshold → discard (listed in report for reference)
对每个归一化后的分类(例如、、):
languages/typescriptframeworks/rails-controllersintegrations/rails-inertia- 收集所有项目的——来自
## Project-specific patterns文件和包含该部分的混合格式.local.md文件(见第2步).md - 与现有Principles去重: 排除描述(后的文本)与对应
-输出文件(第4步生成)中现有原则名语义匹配的模式。使用AI判断语义等价性(不区分大小写、同义词),避免.md包含旧版本已升级的模式时出现自我重复。.local.md - 按行内代码标识(前的反引号部分)匹配剩余模式
-- 使用AI判断语义等价性(例如和
useAuth()指向同一模式)useAuth() → { user, login, logout }
- 使用AI判断语义等价性(例如
- 统计每个模式在所有项目中出现的次数
- 计算阈值:模式必须出现在超过的项目中(即阈值为0.5时需要严格过半数)
项目数量 * promote_threshold - 转换为Principles格式并追加到对应归一化输出文件的
.md部分:## Principles- 原标识格式:→ Principles格式:
`signature` - descriptionDescription (simplified signature) - 描述作为原则名称,标识中的函数/类型名作为提示
- 示例:
- →
`useAuth() → { user, login, logout }` - auth hook interfaceAuth hook interface (useAuth) - →
`clean_bracket_params(:keyword)` - WAF付加のブラケット除去WAF付加のブラケット除去 (clean_bracket_params) - →
`RefOrNull<T extends { id: string }> = T | { id: null }` - nullable refsNullable refs (RefOrNull<T>)
- 对转换后的原则应用第4步的原则去重逻辑(如果已存在同名原则则跳过)
- 原标识格式:
- 未达到阈值的模式 → 丢弃(在报告中列出供参考)
Step 5.5: Merge Examples (.examples.md)
第5.5步:合并示例(.examples.md)
For each normalized file group:
.examples.md- Collect all versions from projects that have this file (including normalized variants)
- Principles Examples: Merge by section heading (e.g., )
### FP only- Same principle heading across projects → adopt the most detailed example, or merge Good/Bad from different projects
- If Good/Bad contrast exists in one project but not another → adopt from the project that has it
- Deduplicate identical examples
- Promoted pattern examples: For patterns promoted in Step 5, include their examples under
## Principles Examples- Use the same semantic equivalence judgment as Step 5 (matching by inline code signature with AI judgment) to link example headings to promoted patterns — do not rely solely on exact heading match
### - title uses the converted Principle name (from Step 5), not the original signature
### - Include the full original signature as a Good example showing usage
- Discard examples for patterns below threshold (same as the pattern itself)
- Use the same semantic equivalence judgment as Step 5 (matching by inline code signature with AI judgment) to link
- Output file structure:
.examples.md
markdown
undefined对每个归一化后的文件组:
.examples.md- 收集所有包含该文件(包括归一化变体)的项目对应的版本
- Principles Examples:按章节标题合并(例如)
### FP only- 不同项目中同一原则的标题 → 采纳最详细的示例,或合并不同项目的正/反例
- 如果一个项目中有正/反例对比而另一个没有 → 采纳包含对比的项目的内容
- 完全相同的示例去重
- 已升级模式的示例:对于第5步中升级的模式,将其示例归入下
## Principles Examples- 使用与第5步相同的语义等价判断(通过AI按行内代码标识匹配)将示例标题与已升级模式关联,不要仅依赖精确标题匹配
### - 标题使用第5步转换后的Principle名称,而非原始标识
### - 保留完整的原始标识作为正例展示用法
- 丢弃未达到阈值的模式的示例(与模式本身处理逻辑一致)
- 使用与第5步相同的语义等价判断(通过AI按行内代码标识匹配)将
- 输出文件结构:
.examples.md
markdown
undefined<Category> Rules - Examples
<Category> Rules - Examples
Principles Examples
Principles Examples
<Principle name>
<Principle name>
Good:
<example>Bad:
<example>
- `###` titles must match the corresponding rule name in the merged output `.md` file. Do not rephrase
- No `paths:` frontmatter (prevents auto-loading)
- If no examples exist for any merged rule, skip generating the `.examples.md` fileGood:
<example>Bad:
<example>
- `###`标题必须与合并输出的`.md`文件中对应的规则名称完全一致,不得改写
- 不包含`paths:` frontmatter(避免自动加载)
- 如果所有合并后的规则都没有示例,跳过生成`.examples.md`文件Step 6: Write Output
第6步:写入输出文件
- Check output directory:
- If : skip writing, show planned file list with contents summary, then go to Step 7
--dry-run - If exists and has files: warn and ask for confirmation before overwriting
- If not exists: create with
mkdir -p
- If
- Write merged files preserving directory structure:
languages/<lang>.md- (if examples exist)
languages/<lang>.examples.md frameworks/<framework>.md- (if examples exist)
frameworks/<framework>.examples.md integrations/<framework>-<integration>.md- (if examples exist)
integrations/<framework>-<integration>.examples.md - Only and
.mdfiles (no.examples.mdin output).local.md
- Output file format:
markdown
---
paths:
- "**/*.ts"
- "**/*.tsx"
---- 检查输出目录:
- 如果指定了:跳过写入,展示计划生成的文件列表和内容摘要,然后进入第7步
--dry-run - 如果目录已存在且包含文件:覆盖前发出警告并请求确认
- 如果目录不存在:执行创建目录
mkdir -p
- 如果指定了
- 保留目录结构写入合并后的文件:
languages/<lang>.md- (存在示例时生成)
languages/<lang>.examples.md frameworks/<framework>.md- (存在示例时生成)
frameworks/<framework>.examples.md integrations/<framework>-<integration>.md- (存在示例时生成)
integrations/<framework>-<integration>.examples.md - 仅输出和
.md文件(输出中不包含.examples.md).local.md
- 输出文件格式:
markdown
---
paths:
- "**/*.ts"
- "**/*.tsx"
---TypeScript Rules
TypeScript Rules
Principles
Principles
- Immutability (spread, map/filter/reduce, const)
- Type safety (strict mode, explicit annotations, no any)
- Auth hook interface (useAuth)
- Output `.md` contains only `## Principles` (promoted patterns are converted and included here)
- Omit `## Principles` section if no principles exist for this category
- If a corresponding `.examples.md` was generated, append a reference section at the end:
```markdown
## Examples
When in doubt: ./<name>.examples.md- Immutability (spread, map/filter/reduce, const)
- Type safety (strict mode, explicit annotations, no any)
- Auth hook interface (useAuth)
- 输出的`.md`文件仅包含`## Principles`部分(已升级的模式会转换后归入此处)
- 如果该分类没有任何原则,省略`## Principles`章节
- 如果生成了对应的`.examples.md`文件,在末尾追加参考章节:
```markdown
## Examples
When in doubt: ./<name>.examples.mdStep 7: Report Summary
第7步:输出汇总报告
Display report using the project's directory name (last path component) as label. Report headers are always in English.
undefined使用项目的目录名(路径的最后一段)作为标签展示报告,报告标题始终使用英文。
undefinedMerge Rules Report
Merge Rules Report
Sources
Sources
- frontend-app (3 files)
- backend-api (2 files)
- shared-lib (4 files)
- frontend-app (3 files)
- backend-api (2 files)
- shared-lib (4 files)
File Name Normalization
File Name Normalization
- +
rails-controller.md→rails-controllers.mdrails-controllers.md - +
rails-model.md→rails-models.mdrails-models.md
- +
rails-controller.md→rails-controllers.mdrails-controllers.md - +
rails-model.md→rails-models.mdrails-models.md
Merge Results
Merge Results
| File | Sources | Principles | Promoted to Principles | Examples |
|---|---|---|---|---|
| languages/typescript.md | 3/3 | 5 | 2 | 7 |
| frameworks/react.md | 2/3 | 3 | 1 | 4 |
| integrations/rails-inertia.md | 2/3 | 2 | 0 | 2 |
Principles = total including promoted. Examples = total entries in the output .
###.examples.md| File | Sources | Principles | Promoted to Principles | Examples |
|---|---|---|---|---|
| languages/typescript.md | 3/3 | 5 | 2 | 7 |
| frameworks/react.md | 2/3 | 3 | 1 | 4 |
| integrations/rails-inertia.md | 2/3 | 2 | 0 | 2 |
Principles = 包含已升级模式的总原则数。Examples = 输出的文件中条目的总数。
.examples.md###Promoted to Principles
Promoted to Principles
- → Auth hook interface (useAuth) - 3/3 projects
useAuth() - → Path helpers (pathFor, url) - 2/3 projects
pathFor() + url()
- → Auth hook interface (useAuth) - 3/3 projects
useAuth() - → Path helpers (pathFor, url) - 2/3 projects
pathFor() + url()
Below Threshold (reference)
Below Threshold (reference)
- (typescript) - 1/3 (frontend-app only)
useCustomHook() - (typescript) - 1/3 (backend-api only)
ApiClient.create()
- (typescript) - 1/3 (frontend-app only)
useCustomHook() - (typescript) - 1/3 (backend-api only)
ApiClient.create()
Skipped
Skipped
- project.md x3 (project-specific, skipped)
undefined- project.md x3 (project-specific, skipped)
undefinedConflict Handling
冲突处理
- Same principle, different hints: Union all hints, deduplicate
- Same principle name, different meaning: Keep both, flag in report for human review
- Same category, different paths: Union all path patterns
- Contradicting principles: Keep both, report as conflict for human review
- 同一原则,不同提示:合并所有提示并去重
- 同一原则名称,不同含义:同时保留,在报告中标记供人工审核
- 同一分类,不同路径:合并所有路径模式
- 相互矛盾的原则:同时保留,在报告中标记为冲突供人工审核