CLAUDE.md ยท git:20251221.276b310 ยท 2025-12-21 ยท sha256 c9bea58e55ad20c5

CLAUDE.md git:20251221.276b310A

Immutable. This exact content never changes and is served at /api/v1/blob/c9bea58e55ad20c5.

# Claude Capsule Kit Integration

This project uses **Claude Capsule Kit** - a persistent context memory system for Claude Code that enables cross-message and cross-session memory.

## ๐ŸŽฏ System Overview

Claude Capsule Kit provides:
- **Persistent Context**: Remember files accessed, tasks worked on, and discoveries made
- **Smart Refresh**: Automatic context updates before each prompt
- **Cross-Session Memory**: Context persists across sessions (24-hour window)
- **Sub-Agent Tracking**: Remember findings from specialized agents
- **Discovery Logging**: Capture architectural insights and patterns

## ๐Ÿšจ CRITICAL: Directory Structure

**NEVER CREATE NEW TOOLS/HOOKS/SCRIPTS IN `.claude/` DIRECTORY!**

### Source vs. Installed Files

```
tools/                    โ†’ SOURCE code (packaged, committed to git)
  โ”œโ”€โ”€ progressive-reader/ โ†’ Tool source + install.sh
  โ””โ”€โ”€ progressive-reader-wrapper/ โ†’ Wrapper scripts + tool.json

.claude/                  โ†’ INSTALLED runtime files (generated, NOT committed)
  โ”œโ”€โ”€ tools/             โ†’ Installed copies from tools/
  โ”œโ”€โ”€ hooks/             โ†’ Installed copies from hooks/
  โ””โ”€โ”€ lib/               โ†’ Installed libraries
```

### Rule of Thumb

**When creating NEW tools, wrappers, hooks, or scripts:**
- โœ… CREATE in root directories: `tools/`, `hooks/`, `scripts/`
- โŒ DO NOT create in `.claude/` - this is for installed files only
- `.claude/` contents are GENERATED by install script
- Only modify `.claude/` when testing/debugging, then copy changes back to source

**Example:**
```bash
# CORRECT - Create wrapper in source location
tools/progressive-reader-wrapper/
  โ”œโ”€โ”€ progressive-reader.sh
  โ””โ”€โ”€ tool.json

# WRONG - Creating in installed location
.claude/tools/progressive-reader/  โŒ DON'T DO THIS!
```

## ๐Ÿ“– Usage Guide

**CRITICAL**: Read and follow `.claude/docs/CAPSULE_USAGE_GUIDE.md`

### Required Behavior

Claude (you!) MUST follow these patterns:

#### 1. Check Capsule Before Redundant Operations
```
BEFORE re-reading a file โ†’ Check if it's in capsule (Files in Context)
BEFORE running git status โ†’ Check capsule (Git State)
BEFORE asking about current task โ†’ Check capsule (Current Tasks)
```

#### 2. Log After Tool Usage

**After Read/Edit/Write:**
```bash
./.claude/hooks/log-file-access.sh "<path>" "read|edit|write"
```

**After Task Tool (Sub-Agents):**
```bash
./.claude/hooks/log-subagent.sh "<agent-type>" "<summary-of-findings>"
```

**After Discoveries:**
```bash
./.claude/hooks/log-discovery.sh "<category>" "<insight>"
# Categories: pattern, insight, decision, architecture, bug, optimization, achievement
```

**After TodoWrite:**
```bash
./.claude/hooks/log-task.sh "<status>" "<task-description>"
# Status: in_progress, pending, completed
```

#### 3. Workflow Pattern

```
1. Check capsule โ†’ See current context
2. Log task start โ†’ Mark as in_progress
3. Work on task โ†’ Read/edit files
4. Log operations โ†’ Track file access
5. Log discoveries โ†’ Capture insights
6. Complete task โ†’ Mark as completed
```

## โšก Tool Enforcement Rules

<tool-enforcement-rules priority="critical">
  <description>
    Claude Capsule Kit provides specialized tools that are FASTER and MORE ACCURATE than generic exploration.
    These rules are MANDATORY and enforced by PreToolUse hook.
  </description>

  <dependency-analysis category="always-use">
    <query type="what imports this file">
      <command>bash .claude/tools/query-deps/query-deps.sh &lt;file-path&gt;</command>
      <use-case>Finding files that import/depend on a specific file</use-case>
    </query>

    <query type="who uses this function">
      <command>bash .claude/tools/query-deps/query-deps.sh &lt;file-path&gt;</command>
      <use-case>Checking if a function/export is used before deleting</use-case>
    </query>

    <query type="what depends on X">
      <command>bash .claude/tools/query-deps/query-deps.sh &lt;file-path&gt;</command>
      <use-case>Understanding dependency relationships</use-case>
    </query>

    <query type="what would break if I change X">
      <command>bash .claude/tools/impact-analysis/impact-analysis.sh &lt;file-path&gt;</command>
      <use-case>Impact analysis before refactoring</use-case>
      <returns>Direct dependents, transitive dependents, risk assessment</returns>
    </query>

    <query type="circular dependencies">
      <command>bash .claude/tools/find-circular/find-circular.sh</command>
      <use-case>Finding import cycles</use-case>
      <returns>All circular dependency chains with fix suggestions</returns>
    </query>

    <query type="dead code">
      <command>bash .claude/tools/find-dead-code/find-dead-code.sh</command>
      <use-case>Finding unused/unreferenced files</use-case>
      <returns>List of potentially unused files</returns>
    </query>

    <never-use tool="Task" subagent="Explore" reason="inefficient-and-incomplete">
      <reason priority="high">Slower - must read and parse files sequentially</reason>
      <reason priority="high">Incomplete - may miss indirect dependencies</reason>
      <reason priority="high">Expensive - high token usage for simple queries</reason>
      <reason priority="critical">Cannot detect circular dependencies</reason>
    </never-use>
  </dependency-analysis>

  <file-search category="always-use">
    <tool name="Glob" reason="direct-file-matching">
      <query type="find file by name">
        <pattern>**/*auth*</pattern>
        <use-case>Where is the auth file?</use-case>
      </query>

      <query type="find files by extension">
        <pattern>**/*.ts</pattern>
        <use-case>Find all TypeScript files</use-case>
      </query>
    </tool>

    <never-use tool="Task" subagent="Explore" reason="inefficient">
      <alternative>Use Glob tool for direct file name/pattern matching</alternative>
    </never-use>
  </file-search>

  <code-search category="always-use">
    <tool name="Grep" reason="fast-pattern-matching">
      <query type="find by keyword">
        <pattern>TODO</pattern>
        <use-case>Find all TODO comments</use-case>
      </query>

      <query type="find definition">
        <pattern>function X</pattern>
        <use-case>Where is function X defined?</use-case>
      </query>
    </tool>

    <never-use tool="Task" subagent="Explore" reason="inefficient">
      <alternative>Use Grep tool for code pattern searches</alternative>
    </never-use>
  </code-search>

  <large-file-navigation category="use-for-structure" threshold="50KB">
    <tool name="progressive-reader" reason="file-navigation-and-structure-discovery">
      <description>
        Progressive-reader is a NAVIGATION TOOL for large files. Use it to understand
        file structure BEFORE reading, then read only what you need.
      </description>

      <primary-value feature="--list">
        The --list command shows file structure WITHOUT reading content:
        - Shows all functions/classes with their chunk numbers
        - Each chunk has a summary of what it contains
        - ~500 tokens to see entire file structure (vs ~48,000 for full read)
        - BETTER THAN GREP for understanding "what's in this file?"
      </primary-value>

      <workflow>
        <step1>Discover structure: .claude/bin/progressive-reader --path &lt;file&gt; --list</step1>
        <step2>Find relevant chunks from function/class names in the list</step2>
        <step3>Read specific chunk: .claude/bin/progressive-reader --path &lt;file&gt; --chunk N</step3>
        <step4>Continue if needed: .claude/bin/progressive-reader --continue-file /tmp/continue.toon</step4>
      </workflow>

      <when-to-use>
        <case>Understanding file structure - "What functions are in this file?"</case>
        <case>Finding specific functionality - "Which part handles authentication?"</case>
        <case>Adding new code - "Show me similar functions so I can follow the pattern"</case>
        <case>Targeted reading - "I need to understand just the login function"</case>
        <case>Context-limited sessions - nearing token limits, need efficient reading</case>
      </when-to-use>

      <when-grep-is-fine>
        <case>Finding specific keyword occurrences</case>
        <case>Searching for error messages or strings</case>
        <case>Quick lookups where you know what you're searching for</case>
      </when-grep-is-fine>

      <languages>TypeScript, JavaScript, Python, Go (full AST parsing)</languages>
      <fallback>Other languages use line-based chunking (still useful, less intelligent)</fallback>
      <token-savings>75-97% vs full file read</token-savings>
    </tool>

    <guidance for="Read-tool">
      <use-read-when>File is under 50KB OR you genuinely need the entire file</use-read-when>
      <use-progressive-when>File is large AND you need structure/specific sections</use-progressive-when>
      <note>PreToolUse hook will warn if you try to Read a file over 50KB</note>
    </guidance>

    <mandatory-file-size-check priority="CRITICAL">
      <rule>BEFORE using Read tool, ALWAYS check file size first:</rule>
      <check>Run: wc -c &lt;file&gt; | awk '{print int($1/1024)"KB"}'</check>

      <decision>
        <if-under-50KB>Use Read tool normally</if-under-50KB>
        <if-over-50KB>STOP. Use progressive-reader instead:</if-over-50KB>
      </decision>

      <progressive-reader-command>
        <list>.claude/bin/progressive-reader --path &lt;file&gt; --list</list>
        <read-chunk>.claude/bin/progressive-reader --path &lt;file&gt; --chunk N</read-chunk>
      </progressive-reader-command>

      <why-this-matters>
        Files over 50KB (~12,500+ tokens) cause MaxFileReadTokenExceededError.
        Each failed Read attempt wastes tokens. Check size FIRST.
      </why-this-matters>
    </mandatory-file-size-check>

    <error-recovery priority="CRITICAL">
      <trigger>MaxFileReadTokenExceededError</trigger>
      <action>IMMEDIATELY stop using Read tool on this file</action>
      <solution>Switch to: .claude/bin/progressive-reader --path &lt;file&gt; --list</solution>
      <do-not>Do NOT retry Read with offset/limit - use progressive-reader</do-not>
    </error-recovery>
  </large-file-navigation>

  <task-tool-allowed-uses>
    <allowed priority="high">
      <use-case>Complex architectural questions requiring analysis</use-case>
      <example>How does the authentication system work?</example>
    </allowed>

    <allowed priority="high">
      <use-case>Implementation understanding</use-case>
      <example>How does X work internally?</example>
    </allowed>

    <allowed priority="medium">
      <use-case>Multi-file refactoring planning</use-case>
      <example>Plan refactoring of auth module across files</example>
    </allowed>

    <allowed priority="medium">
      <use-case>Design pattern identification</use-case>
      <example>What patterns are used in this codebase?</example>
    </allowed>

    <not-allowed>
      <forbidden>Dependency lookups - use query-deps instead</forbidden>
      <forbidden>File searches - use Glob instead</forbidden>
      <forbidden>Code searches - use Grep instead</forbidden>
    </not-allowed>
  </task-tool-allowed-uses>

  <enforcement-mechanism>
    <hook name="PreToolUse" action="intercept-and-warn">
      PreToolUse hook intercepts Task tool calls for dependency queries and displays enforcement warnings.
      HEED THESE WARNINGS - they indicate you are using the wrong tool.
    </hook>

    <required>true</required>
    <bypass>not-allowed</bypass>
  </enforcement-mechanism>
</tool-enforcement-rules>

## Best Practices

<best-practices priority="critical">
  <required-behaviors>
    <behavior priority="high">Check capsule before redundant file reads</behavior>
    <behavior priority="medium">Capture sub-agent findings immediately</behavior>
    <behavior priority="medium">Note architectural discoveries as you learn</behavior>
    <behavior priority="high">Reference capsule context in responses</behavior>
  </required-behaviors>

  <forbidden-behaviors>
    <forbidden priority="critical">Ignore the capsule (defeats the purpose!)</forbidden>
    <forbidden priority="high">Re-read files shown in capsule (unless stale)</forbidden>
    <forbidden priority="medium">Launch duplicate sub-agents for same task</forbidden>
  </forbidden-behaviors>
</best-practices>

## Available Dependency Tools

<dependency-tools>
  <tool name="query-deps">
    <command>./.claude/tools/query-deps.sh &lt;file-path&gt;</command>
    <when-to-use>Before deleting files, understanding dependencies</when-to-use>
  </tool>

  <tool name="impact-analysis">
    <command>./.claude/tools/impact-analysis.sh &lt;file-path&gt;</command>
    <when-to-use>Before refactoring, assessing change risk</when-to-use>
  </tool>

  <tool name="find-circular">
    <command>./.claude/tools/find-circular.sh</command>
    <when-to-use>Debugging import failures, finding cycles</when-to-use>
  </tool>

  <tool name="find-dead-code">
    <command>./.claude/tools/find-dead-code.sh</command>
    <when-to-use>Code cleanup, finding unused files</when-to-use>
  </tool>
</dependency-tools>