inherit-legacy-style
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInherit 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 . Fully language- and framework-agnostic.
.ai-style-rules.md通过扫描代码库,从4个元架构维度识别隐含约定,逐一与用户解决冲突,并将共识固化为可强制执行的文件,防止AI在遗留项目中出现代码风格漂移。完全与语言和框架无关。
.ai-style-rules.mdWhen 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 and optionally
.ai-style-rules.md)CLAUDE.md
- Git(推荐;非Git项目将回退到文件时间戳以启用增量模式)
- 项目根目录的读写权限(用于生成及可选的
.ai-style-rules.md)CLAUDE.md
Workflow
工作流程
Step 0 — Auto-Detect Mode
步骤0 — 自动检测模式
Silently check for at the project root:
.ai-style-rules.md| File exists? | Mode |
|---|---|
| No | Branch A — First-time Full-Scan |
| Yes | Branch 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)$'| Tier | Source files | Strategy |
|---|---|---|
| Small | ≲ 50 | Full close-read every source |
| Medium | 50–500 | Infra layer = full read; business layer = sample 2–3 per dimension |
| Large | ≳ 500 | Strict sampling + budget cap; |
2. Scan along 4 dimensions
- File Anatomy — in-file declaration order (imports → types → main logic → helpers → export)
- State & Control Flow — naming conventions for async state, pagination, flags
- Infrastructure — where cross-cutting utils live (interceptors, formatters, middleware)
- 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:uses style X,pathAuses style Y WARNING: Risk: mixing both fractures the project style Choose:pathBfollow X1follow Y2this is evolution, update rules3I have a new rule4
Suspend until the user answers, then proceed to the next conflict. Never stack questions.
5. Generate with three mandatory sections:
.ai-style-rules.md- [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| Option | Mechanism |
|---|---|
| 1 Soft hook (recommended) | Write |
| 2 Hard hook | Soft hook + |
| 3 No hook | Keep 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 | 严格抽样 + 预算限制;先读取 |
2. 从4个维度扫描
- 文件结构 — 文件内声明顺序(导入 → 类型 → 主逻辑 → 辅助函数 → 导出)
- 状态与控制流 — 异步状态、分页、标记的命名约定
- 基础设施 — 横切工具的存放位置(拦截器、格式化器、中间件)
- 错误处理 — try/catch 与全局拦截器、Result返回的选择;空值检查习惯
3. 应用信号阈值降噪
在打扰用户之前,评估信号强度:
- 弱信号 → 自动忽略:少数派占比<5% 且 文件数<10 → 遵循多数派,少数派归入「禁止项」
- 强信号 → 询问用户:近乎平分,或核心维度存在语义分歧
- 小型项目例外:源文件数≲50时,「3比2」不算多数派 → 需要询问用户
4. 逐一解决冲突(询问流程)
对于每个强信号冲突,仅提出一个包含4个选项的问题:
证据:使用风格X,pathA使用风格Y 警告:风险:混合两种风格会破坏项目一致性 选择:pathB遵循X1遵循Y2这是演进,更新规则3我有新规则4
等待用户答复后,再处理下一个冲突。绝不堆叠多个问题。
5. 生成,包含三个必填章节:
.ai-style-rules.md- [黄金文件] — 标注了示范内容的真实示例文件路径
- [命名与状态控制规则] — 具体、可检查的约定
- [禁止项] — 不得传播的反模式
6. 安装持久化钩子
向用户询问强制执行强度(使用):
AskUserQuestion| 选项 | 机制 |
|---|---|
| 1 软钩子(推荐) | 将 |
| 2 硬钩子 | 软钩子 + 在 |
| 3 不安装钩子 | 保留规则文件;由用户手动引用 |
Branch B — Incremental Sniff
分支B — 增量探测
- Read existing ; if it has a commit fingerprint,
.ai-style-rules.mdto pinpoint deltagit diff <last_hash> HEAD --stat - Read recent Git changes (→ inspect suspect files on demand)
git log -3 --stat - For oversized diffs (>hundreds of files): summary only + sample the largest changes
--stat - Compare new code against recorded rules → conflicts go through Grilling Protocol
- Append evolution log at the end of (never overwrite old rules)
.ai-style-rules.md
- 读取现有;若包含提交指纹,执行
.ai-style-rules.md定位差异git diff <last_hash> HEAD --stat - 读取近期Git变更(→ 按需检查可疑文件)
git log -3 --stat - 对于超大差异(超过数百个文件):仅读取摘要 + 抽样最大的变更
--stat - 将新代码与已记录规则对比 → 冲突进入询问流程
- 在末尾追加演进日志(绝不覆盖旧规则)
.ai-style-rules.md
Per-Turn Enforcement
逐轮强制执行
When 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加载)时,每个代码编写任务必须在推理链开头添加合规声明,说明遵循的示例文件以及避免的禁止项。
.ai-style-rules.mdHow It Works
工作原理
This skill auto-detects whether it's a first-time or incremental run via presence:
.ai-style-rules.md- 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 with Golden Files / Naming Rules / DONTs, and offers optional enforcement hooks.
.ai-style-rules.md - 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 , every code-writing task opens with a compliance declaration naming the exemplar followed and the DONTs avoided.
CLAUDE.md
该Skill通过的存在自动检测是首次运行还是增量运行:
.ai-style-rules.md- 首次运行(分支A) — 评估项目规模,从4个元架构维度(文件结构、状态与控制流、基础设施、错误处理)扫描代码库,应用信号阈值降噪以忽略弱冲突,逐一与用户解决强信号冲突,生成包含黄金文件/命名规则/禁止项的,并提供可选的强制执行钩子。
.ai-style-rules.md - 增量运行(分支B) — 读取现有规则,检查近期Git差异中的新模式或冲突模式,对发现的冲突执行相同的逐一询问流程,并追加演进日志而不覆盖现有规则。
- 逐轮强制执行 — 当通过挂载钩子后,每个代码编写任务开头都会添加合规声明,说明遵循的示例文件以及避免的禁止项。
CLAUDE.md
Output Specification
输出规范
- at project root (with commit fingerprint + scale tier in header)
.ai-style-rules.md - Optionally with
CLAUDE.mdreference@.ai-style-rules.md - Evolution logs appended as entries
### [YYYY-MM-DD] Style Evolution Log
- 项目根目录下的(头部包含提交指纹 + 规模层级)
.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 summaries first, then targeted
--staton suspect filesRead - 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 () is usually sufficient; hard hook only if the user wants mechanical enforcement
@.ai-style-rules.md
- 扫描前用一句话告知检测到的模式(首次/增量)和规模层级
- 对于大型项目,先读取摘要,再定向读取可疑文件
--stat - 让信号阈值处理噪声——843比8的命名差异应自动解决,无需打扰用户
- 若对信号强度存疑,倾向于询问用户
- 软钩子(
CLAUDE.md)通常足够;仅当用户需要机械强制执行时才使用硬钩子@.ai-style-rules.md
Related Skills
相关Skill
- — initialize a new CLAUDE.md with codebase documentation
init - — review diffs for correctness and style issues
code-review - — review code for reuse and simplification opportunities
simplify
- — 初始化包含代码库文档的新
initCLAUDE.md - — 审查差异的正确性和风格问题
code-review - — 审查代码的复用和简化机会
simplify
Examples
示例
-
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 → offer hook strength (soft/hard/none).
.ai-style-rules.md
-
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.
-
Enforcing DONTs via CLAUDE.md
- User: "Make sure all new code stays consistent with the project's rules."
- Action: Soft hook installed → auto-loaded every session → every code-writing task opens with compliance declaration, reusing exemplar patterns and avoiding DONTs.
.ai-style-rules.md
-
首次接入
- 用户:「帮我把AI接入这个旧代码库,不要改变它的风格。」
- 操作:运行分支A全量扫描 → 评估规模 → 扫描4个维度 → 询问冲突 → 生成→ 提供钩子强度选项(软/硬/无)。
.ai-style-rules.md
-
团队变更后的增量更新
- 用户:「我们新增了一个模块;保持现有风格规则不变。」
- 操作:运行分支B增量探测 → 对比Git差异与已记录规则 → 询问新冲突 → 追加演进日志,不覆盖原有规则。
-
通过CLAUDE.md强制执行禁止项
- 用户:「确保所有新代码都符合项目规则。」
- 操作:安装软钩子 → 每次会话自动加载→ 每个代码编写任务开头添加合规声明,复用示例模式并避免禁止项。
.ai-style-rules.md