Rule Override Pattern

March 17, 2026 · View on GitHub

1. Requirement Summary

  • Problem: Plugin 更新 auto-loop.md 時,若使用者已客製化同一 ## section(如 Auto-Trigger table),smart merge 會觸發 CONFLICT 狀態,需手動解決。使用者無法在不觸碰 plugin-managed 檔案的情況下客製化 auto-loop 行為。
  • Goals:
    1. 使用者可在獨立檔案中定義 project-specific auto-loop 行為
    2. Plugin 更新 base rules 時不會與使用者客製化衝突
    3. /install-rules 安裝時自動建立客製化 template,不需手動操作
    4. /claude-health 提供 4 項 safeguard 檢查
  • Scope:
    • v1: 僅 auto-loop-project.md(最常客製化的規則)
    • 命名慣例 *-project.md 保留給未來擴展
  • Non-goals:
    • 不修改 smart merge 演算法本身
    • 不重新命名現有 auto-loop.md(避免大量 blast radius)
    • 不通用化到所有 rules(v1 scope)

2. Existing Code Analysis

FilePurposeImpact
rules/auto-loop.mdPlugin source auto-loop ruleAdd redirect comment at bottom
.claude/rules/auto-loop.mdLocal installed copySame redirect comment
commands/install-rules.mdRule installation flowAdd project override file creation
skills/claude-health/SKILL.mdHealth check + syncAdd 4 safeguards
skills/project-setup/SKILL.mdProject onboardingAdd override file to setup flow
CLAUDE.md / .claude/CLAUDE.mdRule reference listAdd @rules/auto-loop-project.md
CLAUDE.template.mdTemplate for new projectsAdd project override reference

Reusable Components

  • Smart merge 8-state classification(reuse FRESH_INSTALL for project file creation)
  • Manifest .claude/.sd0x-install-state.json(project file explicitly NOT tracked)
  • /claude-health S2 component classification(extend with override-aware checks)

3. Technical Solution

3.1 Architecture Design

sequenceDiagram
    participant U as User
    participant IR as /install-rules
    participant CH as /claude-health
    participant CM as CLAUDE.md

    IR->>IR: Install auto-loop.md (managed, smart merge)
    IR->>IR: Create auto-loop-project.md (unmanaged template)
    IR->>CM: Add @rules/auto-loop-project.md reference

    Note over U: User edits auto-loop-project.md

    U->>CH: /claude-health
    CH->>CH: Check 4 safeguards
    alt Override drift
        CH->>U: Warn: base updated since override authored
    end
    alt Wrong-layer edit
        CH->>U: Warn: base auto-loop.md locally modified
    end

3.2 File Ownership Model

.claude/rules/
├── auto-loop.md              ← plugin-managed (smart merge tracked)
├── auto-loop-project.md      ← user-owned (NOT manifest-tracked)
├── codex-invocation.md       ← plugin-managed
└── ...
FileOwnerManifest TrackedSmart Merge
auto-loop.mdPluginYesYes (8-state)
auto-loop-project.mdUserNoNo (unmanaged)

Note: Template source exists in rules/ for /install-rules to copy from, but the installed copy is explicitly excluded from manifest hash tracking.

3.3 Project Override File Contract

# Auto-Loop Project Overrides

<!-- Precedence: When this file conflicts with auto-loop.md, this file takes precedence. -->
<!-- Based on: auto-loop.md @ <sha7> (<date>) -->

## Auto-Trigger

<!-- To override the Auto-Trigger table from auto-loop.md,
     restate the FULL section here (section-level replacement).
     Delete this section if you don't need to override. -->

Override semantics: Section-level full replacement(使用者重寫完整 ## section 來覆蓋 base)。

Note: Override section headings must exactly match base section headings for clear section-level replacement semantics.

Design DecisionChoiceRationale
Override granularitySection-level (##)LLM 更容易理解完整 section;避免 row-level delta 的歧義
Precedence mechanismSelf-contained header text不依賴 CLAUDE.md load order(@ 引用無保證順序)
Manifest trackingNot tracked避免 plugin 更新觸碰 user 檔案

3.4 Core Logic Changes

3.4.1 /install-rules Changes

Phase 3.5 extension — after managed rule classification, add:

# Exclusion: *-project.md files are NOT part of the managed install set.
# They are copied as templates only, with no manifest hash entry written.
managed_rules = rules/*.md EXCLUDING *-project.md

# Explicit override template mapping (not suffix-derived from managed_rules)
override_templates = { "auto-loop.md": "auto-loop-project.md" }

For each (base_rule, project_file) in override_templates:
  if project_file NOT exists in .claude/rules/:
    Copy from rules/{project_file} as template
    Do NOT write manifest entry for project_file
    Log: "Created project override template: {project_file}"
  else:
    Skip (user already has it)

Important: /install-rules must explicitly exclude *-project.md from the managed rule enumeration (rules/*.md) to prevent accidental manifest tracking. The template source rules/auto-loop-project.md is only a copy source, never a managed rule.

New flag: --customize <rule-name> — creates fuller template with examples.

3.4.2 /claude-health Safeguard Checks

4 new checks in S2 component classification:

#CheckSeverityDetectionRecommendation
1Override driftP2based_on hash in project file vs current base hash"Base auto-loop updated; review your overrides"
2Policy contradictionP1Override disables required check that hook enforces"Override conflicts with stop-guard enforcement"
3Missing referenceP1CLAUDE.md references @rules/auto-loop-project.md but file missing, OR file exists but not referenced in CLAUDE.md/install-rules to recreate or add reference
4Wrong-layer editP2Base auto-loop.md has LOCAL_MODIFIED, CONFLICT, or LEGACY doctor state (user modified base)"Move customization to auto-loop-project.md"

Policy contradiction detection contract: Parse the project override's Auto-Trigger table for required check commands. Cross-reference against hook-enforced sentinels (from hooks/stop-guard.sh): if override omits a command that stop-guard requires (e.g., removing /codex-review-fast for code changes), flag as P1 contradiction.

3.4.3 Base auto-loop.md Redirect

Add at bottom of rules/auto-loop.md:

## Project Customization

Project-specific overrides belong in `auto-loop-project.md` (not this file).
See `@rules/auto-loop-project.md` for your project's custom auto-loop behavior.

3.4.4 CLAUDE.md / Template Updates

## Rules

- @rules/auto-loop.md -- Auto review loop (highest priority)
- @rules/auto-loop-project.md -- Project-specific auto-loop overrides (user-owned)

Backfill for existing projects: /install-rules must perform idempotent missing-reference repair: if @rules/auto-loop.md reference exists in ## Rules but @rules/auto-loop-project.md is absent, insert only the missing line. This ensures existing projects receive the reference on next /install-rules run without requiring manual CLAUDE.md edits.

3.5 Migration Strategy (Backward Compatibility)

User StateMigration Path
No customization (base == manifest)/install-rules creates empty project file → no behavioral change
Has local modifications (LOCAL_MODIFIED)/claude-health detects wrong-layer → suggests moving to project file
Has CONFLICT stateResolve conflict first, then move customizations to project file

4. Risks and Dependencies

RiskProbabilityImpactMitigation
LLM ignores precedence instructionLowMediumSection-level replacement (no overlapping content) + explicit header
User edits base instead of project fileMediumLow/claude-health wrong-layer detection + base redirect comment
Override drift (base updated, project stale)MediumMediumbased_on hash + health check warning
Scope creep to all rulesLowMediumv1 scoped to auto-loop; convention reserved

Dependencies

DependencyStatusRisk
@rules/ flat include semantics in Claude CodeVerifiedLow — well-tested
Smart merge 8-state classificationExistingNone
/claude-health S2 classificationExistingLow — extend, not rewrite

5. Work Breakdown

#TaskFilesEffortDepends On
1Create template source rules/auto-loop-project.md (installed as unmanaged — no manifest entry)rules/auto-loop-project.md (new)S
2Add redirect section to auto-loop.mdrules/auto-loop.mdS
3Update /install-rules to create project filecommands/install-rules.mdM1
4Update /claude-health with 4 safeguardsskills/claude-health/SKILL.mdM1
5Update CLAUDE.md + template referencesCLAUDE.md, CLAUDE.template.md, .claude/CLAUDE.mdS1
6Update /project-setup flowskills/project-setup/SKILL.mdS1, 3
7Tests for override detectiontest/M3, 4

6. Testing Strategy

TypeScopeFile
UnitOverride file detection (exists/missing/drift)test/scripts/install-rules-override.test.js
UnitWrong-layer detection (base modified + project exists)test/scripts/claude-health-override.test.js
UnitPolicy contradiction detection (override removes required review step)test/scripts/claude-health-override.test.js
UnitManifest exclusion (no auto-loop-project.md entry in .claude/.sd0x-install-state.json)test/commands/install-rules-override.test.js
Integration/install-rules creates project file on fresh installManual
IntegrationPlugin update doesn't touch project fileManual

7. Open Questions

#QuestionDecision Owner
1Should --customize flag be on /install-rules or a separate command?Plugin maintainer
2Should the project file template include all sections as commented-out stubs?UX decision
3v2: Extend to codex-invocation-project.md, testing-project.md?Based on user demand