inherit-legacy-style

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Inherit Legacy Style

遗留项目风格继承

Prevents AI code style drift in legacy projects by scanning the codebase for implicit conventions across 4 meta-architecture dimensions, resolving conflicts with the user one at a time, and crystallizing the consensus into an enforceable
.ai-style-rules.md
. Fully language- and framework-agnostic.
通过扫描代码库,从4个元架构维度识别隐含约定,逐一与用户解决冲突,并将共识固化为可强制执行的
.ai-style-rules.md
文件,防止AI在遗留项目中出现代码风格漂移。完全与语言和框架无关。

When to Activate

激活时机

  • User types
    /inherit-legacy-style
  • User mentions onboarding AI onto a hand-written legacy project
  • User is worried about AI-generated code "drifting" from existing project conventions
  • User wants to extract and codify their project's implicit coding rules
  • 用户输入
    /inherit-legacy-style
  • 用户提及将AI接入手写遗留项目
  • 用户担心AI生成的代码与现有项目约定「漂移」
  • 用户想要提取并编纂项目的隐含编码规则

When to Use

使用场景

Use this skill when you need to preserve legacy project style and prevent AI-generated style drift. See When to Activate above for trigger conditions.
当你需要保留遗留项目风格、防止AI生成代码出现风格漂移时,使用该Skill。触发条件见上方激活时机

Prerequisites

前置条件

  • Git (recommended; non-Git projects fall back to file timestamps for incremental mode)
  • Read/Write access to the project root (generates
    .ai-style-rules.md
    and optionally
    CLAUDE.md
    )
  • Git(推荐;非Git项目将回退到文件时间戳以启用增量模式)
  • 项目根目录的读写权限(用于生成
    .ai-style-rules.md
    及可选的
    CLAUDE.md

Workflow

工作流程

Step 0 — Auto-Detect Mode

步骤0 — 自动检测模式

Silently check for
.ai-style-rules.md
at the project root:
File exists?Mode
NoBranch A — First-time Full-Scan
YesBranch B — Incremental Sniff
Announce the mode in one line and proceed — never ask the user to pick.
静默检查项目根目录下是否存在
.ai-style-rules.md
文件是否存在?模式
分支A — 首次全量扫描
分支B — 增量探测
用一句话告知当前模式后继续执行——绝不要求用户选择模式。

Branch A — First-time Full-Scan

分支A — 首次全量扫描

1. Measure scale, pick a scanning tier
bash
git ls-files | grep -cE '\.(js|ts|jsx|tsx|vue|py|go|rs|java|kt|rb|php|cs|swift|c|cpp|h)$'
TierSource filesStrategy
Small≲ 50Full close-read every source
Medium50–500Infra layer = full read; business layer = sample 2–3 per dimension
Large≳ 500Strict sampling + budget cap;
--stat
summary first, then targeted reads
2. Scan along 4 dimensions
  1. File Anatomy — in-file declaration order (imports → types → main logic → helpers → export)
  2. State & Control Flow — naming conventions for async state, pagination, flags
  3. Infrastructure — where cross-cutting utils live (interceptors, formatters, middleware)
  4. Error Handling — try/catch vs global interceptor vs Result return; null-check habits
3. Apply signal-threshold noise reduction
Before interrupting the user, evaluate signal strength:
  • Weak signal → auto-suppress: minority <5% AND count <10 → majority wins, minority goes to DONTs
  • Strong signal → grill: near-even split, or semantic fork on a core dimension
  • Small-project exception: sources ≲50, "3 vs 2" is NOT a majority → grill it
4. Resolve conflicts one at a time (Grilling Protocol)
For each strong-signal conflict, present exactly ONE question with 4 options:
Evidence:
pathA
uses style X,
pathB
uses style Y WARNING: Risk: mixing both fractures the project style Choose:
1
follow X
2
follow Y
3
this is evolution, update rules
4
I have a new rule
Suspend until the user answers, then proceed to the next conflict. Never stack questions.
5. Generate
.ai-style-rules.md
with three mandatory sections:
  • [Golden Files] — real exemplar paths annotated with what they demonstrate
  • [Naming & State-Control Rules] — concrete, checkable conventions
  • [DONTs] — anti-patterns that must not propagate
6. Install the persistent hook
Ask the user for enforcement strength (use
AskUserQuestion
):
OptionMechanism
1 Soft hook (recommended)Write
@.ai-style-rules.md
reference into project
CLAUDE.md
2 Hard hookSoft hook +
PreToolUse[Write|Edit|MultiEdit]
Hook in
settings.json
3 No hookKeep the rules file; user references manually
1. 评估规模,选择扫描层级
bash
git ls-files | grep -cE '\.(js|ts|jsx|tsx|vue|py|go|rs|java|kt|rb|php|cs|swift|c|cpp|h)$'
层级源文件数量策略
小型≲ 50逐行精读所有源文件
中型50–500基础设施层 = 全量读取;业务层 = 每个维度抽样2–3个文件
大型≳ 500严格抽样 + 预算限制;先读取
--stat
摘要,再定向读取目标文件
2. 从4个维度扫描
  1. 文件结构 — 文件内声明顺序(导入 → 类型 → 主逻辑 → 辅助函数 → 导出)
  2. 状态与控制流 — 异步状态、分页、标记的命名约定
  3. 基础设施 — 横切工具的存放位置(拦截器、格式化器、中间件)
  4. 错误处理 — try/catch 与全局拦截器、Result返回的选择;空值检查习惯
3. 应用信号阈值降噪
在打扰用户之前,评估信号强度:
  • 弱信号 → 自动忽略:少数派占比<5% 且 文件数<10 → 遵循多数派,少数派归入「禁止项」
  • 强信号 → 询问用户:近乎平分,或核心维度存在语义分歧
  • 小型项目例外:源文件数≲50时,「3比2」不算多数派 → 需要询问用户
4. 逐一解决冲突(询问流程)
对于每个强信号冲突,仅提出一个包含4个选项的问题:
证据:
pathA
使用风格X,
pathB
使用风格Y 警告:风险:混合两种风格会破坏项目一致性 选择:
1
遵循X
2
遵循Y
3
这是演进,更新规则
4
我有新规则
等待用户答复后,再处理下一个冲突。绝不堆叠多个问题。
5. 生成
.ai-style-rules.md
,包含三个必填章节:
  • [黄金文件] — 标注了示范内容的真实示例文件路径
  • [命名与状态控制规则] — 具体、可检查的约定
  • [禁止项] — 不得传播的反模式
6. 安装持久化钩子
向用户询问强制执行强度(使用
AskUserQuestion
):
选项机制
1 软钩子(推荐)
@.ai-style-rules.md
引用写入项目
CLAUDE.md
2 硬钩子软钩子 + 在
settings.json
中添加
PreToolUse[Write|Edit|MultiEdit]
钩子
3 不安装钩子保留规则文件;由用户手动引用

Branch B — Incremental Sniff

分支B — 增量探测

  1. Read existing
    .ai-style-rules.md
    ; if it has a commit fingerprint,
    git diff <last_hash> HEAD --stat
    to pinpoint delta
  2. Read recent Git changes (
    git log -3 --stat
    → inspect suspect files on demand)
  3. For oversized diffs (>hundreds of files):
    --stat
    summary only + sample the largest changes
  4. Compare new code against recorded rules → conflicts go through Grilling Protocol
  5. Append evolution log at the end of
    .ai-style-rules.md
    (never overwrite old rules)
  1. 读取现有
    .ai-style-rules.md
    ;若包含提交指纹,执行
    git diff <last_hash> HEAD --stat
    定位差异
  2. 读取近期Git变更(
    git log -3 --stat
    → 按需检查可疑文件)
  3. 对于超大差异(超过数百个文件):仅读取
    --stat
    摘要 + 抽样最大的变更
  4. 将新代码与已记录规则对比 → 冲突进入询问流程
  5. .ai-style-rules.md
    末尾追加演进日志(绝不覆盖旧规则)

Per-Turn Enforcement

逐轮强制执行

When
.ai-style-rules.md
is in context (loaded via CLAUDE.md), every code-writing task must open with a compliance declaration in the reasoning chain, naming the exemplar being followed and the DONTs being avoided.
.ai-style-rules.md
处于上下文环境(通过CLAUDE.md加载)时,每个代码编写任务必须在推理链开头添加合规声明,说明遵循的示例文件以及避免的禁止项。

How It Works

工作原理

This skill auto-detects whether it's a first-time or incremental run via
.ai-style-rules.md
presence:
  • First-time (Branch A) — Measures project scale, scans codebase across 4 meta-architecture dimensions (File Anatomy, State & Control Flow, Infrastructure, Error Handling), applies signal-threshold noise reduction to suppress weak conflicts, resolves strong-signal conflicts one-at-a-time with the user, generates
    .ai-style-rules.md
    with Golden Files / Naming Rules / DONTs, and offers optional enforcement hooks.
  • Incremental (Branch B) — Reads existing rules, checks recent Git diffs for new or conflicting patterns, runs the same one-at-a-time grilling protocol for any conflicts found, and appends evolution logs without overwriting existing rules.
  • Per-Turn Enforcement — When hooked via
    CLAUDE.md
    , every code-writing task opens with a compliance declaration naming the exemplar followed and the DONTs avoided.
该Skill通过
.ai-style-rules.md
的存在自动检测是首次运行还是增量运行:
  • 首次运行(分支A) — 评估项目规模,从4个元架构维度(文件结构、状态与控制流、基础设施、错误处理)扫描代码库,应用信号阈值降噪以忽略弱冲突,逐一与用户解决强信号冲突,生成包含黄金文件/命名规则/禁止项的
    .ai-style-rules.md
    ,并提供可选的强制执行钩子。
  • 增量运行(分支B) — 读取现有规则,检查近期Git差异中的新模式或冲突模式,对发现的冲突执行相同的逐一询问流程,并追加演进日志而不覆盖现有规则。
  • 逐轮强制执行 — 当通过
    CLAUDE.md
    挂载钩子后,每个代码编写任务开头都会添加合规声明,说明遵循的示例文件以及避免的禁止项。

Output Specification

输出规范

  • .ai-style-rules.md
    at project root (with commit fingerprint + scale tier in header)
  • Optionally
    CLAUDE.md
    with
    @.ai-style-rules.md
    reference
  • Evolution logs appended as
    ### [YYYY-MM-DD] Style Evolution Log
    entries
  • 项目根目录下的
    .ai-style-rules.md
    (头部包含提交指纹 + 规模层级)
  • 可选的
    CLAUDE.md
    ,包含
    @.ai-style-rules.md
    引用
  • 演进日志以
    ### [YYYY-MM-DD] 风格演进日志
    条目追加

Anti-Patterns

反模式

  • FAIL: Do NOT skip the scale measurement step — sampling a 30-file project "starves" it; full-scanning a 5,000-file repo blows up
  • FAIL: Do NOT stack multiple conflict questions at once — grilling is strictly one-at-a-time
  • FAIL: Do NOT overwrite old rules in incremental mode — always append evolution logs
  • FAIL: Do NOT default to "hard hook" without asking — enforcement strength is the user's call
  • FAIL: Do NOT judge syntax or tech-stack quality — this skill aligns meta-architecture only
  • FAIL: Do NOT copy bugs from exemplar files — reuse structure, flag defects
  • 错误:不得跳过规模评估步骤——对30个文件的项目抽样会导致信息不足;对5000个文件的仓库全量扫描会导致资源耗尽
  • 错误:不得同时堆叠多个冲突问题——询问必须严格逐一进行
  • 错误:增量模式下不得覆盖旧规则——始终追加演进日志
  • 错误:不得默认选择「硬钩子」而不询问用户——强制执行强度由用户决定
  • 错误:不得评判语法或技术栈质量——该Skill仅对齐元架构
  • 错误:不得复制示例文件中的bug——复用结构,标记缺陷

Best Practices

最佳实践

  • Announce the detected mode (first-time vs incremental) and scale tier in one line before scanning
  • For large projects, read
    --stat
    summaries first, then targeted
    Read
    on suspect files
  • Let the signal threshold handle noise — a 843-vs-8 naming split should auto-resolve without user interruption
  • When in doubt about signal strength, lean toward asking
  • The CLAUDE.md soft hook (
    @.ai-style-rules.md
    ) is usually sufficient; hard hook only if the user wants mechanical enforcement
  • 扫描前用一句话告知检测到的模式(首次/增量)和规模层级
  • 对于大型项目,先读取
    --stat
    摘要,再定向读取可疑文件
  • 让信号阈值处理噪声——843比8的命名差异应自动解决,无需打扰用户
  • 若对信号强度存疑,倾向于询问用户
  • CLAUDE.md
    软钩子(
    @.ai-style-rules.md
    )通常足够;仅当用户需要机械强制执行时才使用硬钩子

Related Skills

相关Skill

  • init
    — initialize a new CLAUDE.md with codebase documentation
  • code-review
    — review diffs for correctness and style issues
  • simplify
    — review code for reuse and simplification opportunities
  • init
    — 初始化包含代码库文档的新
    CLAUDE.md
  • code-review
    — 审查差异的正确性和风格问题
  • simplify
    — 审查代码的复用和简化机会

Examples

示例

  1. First-time onboarding
    • User: "Help me onboard AI to this older codebase without changing its style."
    • Action: Run Branch A full-scan → measure scale → scan 4 dimensions → grill conflicts → generate
      .ai-style-rules.md
      → offer hook strength (soft/hard/none).
  2. Incremental update after team changes
    • User: "We added a new module; keep existing style rules intact."
    • Action: Run Branch B incremental sniff → compare Git deltas to recorded rules → grill any new conflicts → append evolution log without overwriting.
  3. Enforcing DONTs via CLAUDE.md
    • User: "Make sure all new code stays consistent with the project's rules."
    • Action: Soft hook installed →
      .ai-style-rules.md
      auto-loaded every session → every code-writing task opens with compliance declaration, reusing exemplar patterns and avoiding DONTs.
  1. 首次接入
    • 用户:「帮我把AI接入这个旧代码库,不要改变它的风格。」
    • 操作:运行分支A全量扫描 → 评估规模 → 扫描4个维度 → 询问冲突 → 生成
      .ai-style-rules.md
      → 提供钩子强度选项(软/硬/无)。
  2. 团队变更后的增量更新
    • 用户:「我们新增了一个模块;保持现有风格规则不变。」
    • 操作:运行分支B增量探测 → 对比Git差异与已记录规则 → 询问新冲突 → 追加演进日志,不覆盖原有规则。
  3. 通过CLAUDE.md强制执行禁止项
    • 用户:「确保所有新代码都符合项目规则。」
    • 操作:安装软钩子 → 每次会话自动加载
      .ai-style-rules.md
      → 每个代码编写任务开头添加合规声明,复用示例模式并避免禁止项。