context-management
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseContext Management Setup
上下文管理设置
Automates context management best practices for Claude Code projects.
Analyzes the current project and generates optimized configuration files.
为Claude Code项目自动化实现上下文管理最佳实践。
分析当前项目并生成优化后的配置文件。
When to Use
适用场景
- Starting a new project (no CLAUDE.md exists)
- Existing project needs context optimization
- User reports context loss after compaction
- User wants to set up rules, memory, or subagent guidelines
- Before a long coding session
- 启动新项目(尚无CLAUDE.md)
- 现有项目需要上下文优化
- 用户反馈压缩后出现上下文丢失
- 用户想要设置规则、内存或子代理指南
- 长时间编码会话开始前
Workflow
工作流程
Step 1: Analyze the Project
步骤1:分析项目
Before generating anything, read the project to understand its shape:
- Read ,
package.json,Cargo.toml, or equivalent to detect stackpyproject.toml - Read existing if present (will update, not overwrite)
CLAUDE.md - Check for directory
.claude/rules/ - Scan structure to identify major areas (api, components, lib, tests, etc.)
src/ - Check for existing ,
tsconfig.json,.eslintrcfor conventionsprettier.config - Read if present for project context
README.md
Collect this into a mental model of the project before proceeding.
在生成任何内容前,先读取项目了解其结构:
- 读取、
package.json、Cargo.toml或等效文件,识别技术栈pyproject.toml - 如果已有则读取该文件(将更新而非覆盖)
CLAUDE.md - 检查是否存在目录
.claude/rules/ - 扫描结构,识别核心模块(api、components、lib、tests等)
src/ - 检查已有、
tsconfig.json、.eslintrc等文件,获取编码规范prettier.config - 如果有则读取,获取项目上下文
README.md
在继续操作前将上述信息整合为项目的心智模型。
Step 2: Generate or Update CLAUDE.md
步骤2:生成或更新CLAUDE.md
The CLAUDE.md MUST stay under 200 lines. Use @references for details.
Structure to follow:
markdown
undefinedCLAUDE.md必须保持在200行以内,详情使用@引用。
遵循以下结构:
markdown
undefined[Project Name]
[Project Name]
[1-2 sentence description from README/package.json]
[1-2 sentence description from README/package.json]
Stack
Stack
[Detected stack — language, framework, DB, key libs]
[Detected stack — language, framework, DB, key libs]
Key Commands
Key Commands
[Detected from package.json scripts, Makefile, etc.]
- Dev:
[command] - Build:
[command] - Test:
[command] - Lint:
[command]
[Detected from package.json scripts, Makefile, etc.]
- Dev:
[command] - Build:
[command] - Test:
[command] - Lint:
[command]
Architecture
Architecture
[Brief description of src/ structure — max 5-8 lines]
- @[key-file-1] for [purpose]
- @[key-file-2] for [purpose]
[Brief description of src/ structure — max 5-8 lines]
- @[key-file-1] for [purpose]
- @[key-file-2] for [purpose]
Conventions
Conventions
[Detected from config files — indent, quotes, semicolons, naming]
[Detected from config files — indent, quotes, semicolons, naming]
Context Management
Context Management
Compact Instructions
Compact Instructions
When compacting, always preserve:
- List of modified files and their purpose
- Failing tests with error messages
- Architectural decisions made this session
- Current task status and next steps
When compacting, safe to discard:
- File contents that were only read for exploration
- Search results that didn't lead anywhere
- Verbose command output already acted upon
When compacting, always preserve:
- List of modified files and their purpose
- Failing tests with error messages
- Architectural decisions made this session
- Current task status and next steps
When compacting, safe to discard:
- File contents that were only read for exploration
- Search results that didn't lead anywhere
- Verbose command output already acted upon
Subagent Guidelines
Subagent Guidelines
- Use Explore agent for codebase searches requiring > 3 queries
- Use background agents for running test suites
- Use Plan agent before refactors touching > 5 files
- Delegate file-heavy research to subagents to protect main context
- Use Explore agent for codebase searches requiring > 3 queries
- Use background agents for running test suites
- Use Plan agent before refactors touching > 5 files
- Delegate file-heavy research to subagents to protect main context
Rules
Rules
See .claude/rules/ for scoped coding rules.
**Important:** If CLAUDE.md already exists, MERGE — don't replace. Add missing sections only.See .claude/rules/ for scoped coding rules.
**注意:**如果已存在CLAUDE.md,请合并而非替换,仅补充缺失的章节。Step 3: Generate Scoped Rules
步骤3:生成作用域规则
Create with rules scoped to relevant paths.
Only create rules for areas that actually exist in the project.
.claude/rules/Read for templates per area.
references/rule-templates.mdRules to consider creating (only if the area exists):
| File | Scope | Creates when |
|---|---|---|
| | Always — general conventions |
| | API routes exist |
| | UI components exist |
| | Test files exist |
| | ORM/DB config exists |
Each rule file should be 10-30 lines max. Use the templates from references.
创建目录,存放对应路径的规则。
仅为项目中实际存在的模块创建规则。
.claude/rules/读取获取各模块的规则模板。
references/rule-templates.md可考虑创建的规则(仅当对应模块存在时):
| 文件名 | 作用域 | 创建时机 |
|---|---|---|
| | 始终创建 — 通用规范 |
| | 存在API路由时 |
| | 存在UI组件时 |
| | 存在测试文件时 |
| | 存在ORM/DB配置时 |
每个规则文件最多10-30行,使用参考目录中的模板。
Step 4: Initialize Memory Structure
步骤4:初始化内存结构
If auto-memory is enabled, create initial memory notes:
~/.claude/projects/<project-hash>/memory/MEMORY.mdContent:
markdown
undefined如果启用了自动内存功能,创建初始内存记录:
~/.claude/projects/<project-hash>/memory/MEMORY.md内容:
markdown
undefined[Project Name] Memory
[Project Name] Memory
Project
Project
- Stack: [detected]
- Key paths: [detected]
- Stack: [detected]
- Key paths: [detected]
Patterns
Patterns
[Leave empty — Claude fills as it learns]
[Leave empty — Claude fills as it learns]
Debugging
Debugging
[Leave empty — Claude fills as issues arise]
undefined[Leave empty — Claude fills as issues arise]
undefinedStep 5: Summary Report
步骤5:汇总报告
After setup, show the user a summary:
Context Management Setup Complete
==================================
CLAUDE.md: [created/updated] (X lines)
Rules: [N] files in .claude/rules/
Memory: [initialized/already exists]
Files created:
- .claude/rules/code-style.md
- .claude/rules/[others...]
Recommendations:
- [Any project-specific suggestions]设置完成后,向用户展示汇总信息:
Context Management Setup Complete
==================================
CLAUDE.md: [created/updated] (X lines)
Rules: [N] files in .claude/rules/
Memory: [initialized/already exists]
Files created:
- .claude/rules/code-style.md
- .claude/rules/[others...]
Recommendations:
- [Any project-specific suggestions]Important Constraints
重要约束
- CLAUDE.md must stay under 200 lines
- Each rule file must stay under 30 lines
- Never overwrite existing files without showing diff first
- Detect conventions from config files, don't assume
- If stack is unknown, ask the user before generating
- Use @references in CLAUDE.md instead of inlining large content
- CLAUDE.md必须保持在200行以内
- 每个规则文件必须保持在30行以内
- 不得覆盖现有文件,需先展示差异
- 从配置文件中识别编码规范,不要主观假设
- 如果技术栈未知,生成前先询问用户
- 在CLAUDE.md中使用@引用,而非直接嵌入大量内容