codebase-teach
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesecodebase-teach
codebase-teach
Analyzes bounded contexts from the project's source tree, extracts domain knowledge per context, writesfiles, and producesai-context/features/<context>.mdwith coverage metrics.teach-report.md
Triggers: , teach codebase, extract domain knowledge, update feature docs, analyze bounded contexts
/codebase-teach从项目源码树中分析Bounded Contexts,提取每个上下文的领域知识,生成文件,并输出包含覆盖度指标的ai-context/features/<context>.md文件。teach-report.md
触发指令:、teach codebase、extract domain knowledge、update feature docs、analyze bounded contexts
/codebase-teachPurpose
用途
codebase-teachai-context/features/memory-updatememory-initcodebase-teachai-context/features/memory-updatememory-initProcess
执行流程
Step 0 — Load project context (non-blocking)
步骤0 — 加载项目上下文(非阻塞)
This step is non-blocking: any failure (missing file, unreadable file) MUST produce at most an INFO-level note. This step MUST NOT produce or .
status: blockedstatus: failed- Read — tech stack, versions, key tools.
ai-context/stack.md - Read — architectural decisions and rationale.
ai-context/architecture.md - Read — naming patterns, code conventions.
ai-context/conventions.md - Read the project's (at project root) and extract the
CLAUDE.mdsection.## Skills Registry
For each file:
- If absent: log
INFO: [filename] not found — proceeding without it. - If present: extract or
Last updated:date. If date is older than 30 days: logLast analyzed:NOTE: [filename] last updated [date] — context may be stale. Consider running /memory-update or /project-analyze.
Also read project config file ( at project root) if it exists, and extract if present:
config.yamlteach_max_files_per_context- If present:
max_files = teach_max_files_per_context - If absent: (default)
max_files = 10
Log: or
"File cap per context: [max_files] (source: config.yaml)""File cap per context: 10 (default)"此步骤为非阻塞:任何失败(文件缺失、无法读取)最多只能生成INFO级别的日志。此步骤不得返回或状态。
status: blockedstatus: failed- 读取文件——包含技术栈、版本信息及核心工具。
ai-context/stack.md - 读取文件——包含架构决策及理由。
ai-context/architecture.md - 读取文件——包含命名规范、代码约定。
ai-context/conventions.md - 读取项目根目录下的文件,并提取
CLAUDE.md章节内容。## Skills Registry
对于每个文件:
- 若文件不存在:记录日志
INFO: [filename] not found — proceeding without it. - 若文件存在:提取或
Last updated:对应的日期。若日期早于30天:记录日志Last analyzed:NOTE: [filename] last updated [date] — context may be stale. Consider running /memory-update or /project-analyze.
同时读取项目根目录下的配置文件(若存在),并提取其中的配置项:
config.yamlteach_max_files_per_context- 若存在该配置项:
max_files = teach_max_files_per_context - 若不存在:(默认值)
max_files = 10
记录日志: 或
"File cap per context: [max_files] (source: config.yaml)""File cap per context: 10 (default)"Step 1 — Scan bounded contexts
步骤1 — 扫描Bounded Contexts
Identify bounded context candidates by scanning the project directory tree at depth ≤ 2 under these root directories (in order):
- — subdirectories at depth 1 (e.g.,
src/,src/auth/)src/payments/ - — subdirectories at depth 1
app/ - — subdirectories at depth 1
features/ - — subdirectories at depth 1
domain/ - — existing feature file names (each file stem is treated as a context name)
ai-context/features/
Exclusion rules — skip directories named: , , , , , , , , , , ,
sharedutilscommonlibtypeshookscomponents__tests__testtestsnode_modules.gitCross-reference with existing feature files:
Read the directory (if it exists) and list all files, excluding files whose names begin with (e.g., ).
ai-context/features/.md__template.mdFor each detected context candidate, build a record:
{ slug: kebab-case of directory name, dir_path, existing_feature_file: bool }Slug convention: lowercase the directory name; replace spaces and underscores with hyphens. Example: → , → .
UserProfileuser-profileauth_serviceauth-serviceIf no context candidates are found:
- Log:
"No bounded context directories detected." - Write with a Summary noting "No bounded context directories detected" and a recommendation to run
teach-report.mdfirst./memory-init - Stop here.
If does not exist:
ai-context/features/- Log:
"INFO: ai-context/features/ not found — feature files will be created if possible." - Note in : "ai-context/features/ was absent at run time. Recommend running /memory-init to scaffold the directory before re-running /codebase-teach."
teach-report.md - Continue processing (the skill creates the directory and files as needed).
Output:
context_list = [{ slug, dir_path, existing_feature_file: bool }, ...]Log each context found:
"Detected context: [slug] — [dir_path] (feature file: [exists|absent])"通过扫描项目目录树(深度≤2)来识别Bounded Context候选对象,扫描优先级如下:
- 目录——深度为1的子目录(例如:
src/、src/auth/)src/payments/ - 目录——深度为1的子目录
app/ - 目录——深度为1的子目录
features/ - 目录——深度为1的子目录
domain/ - 目录——已存在的feature文件名(每个文件的主干名称作为上下文名称)
ai-context/features/
排除规则——跳过以下名称的目录:、、、、、、、、、、、
sharedutilscommonlibtypeshookscomponents__tests__testtestsnode_modules.git与现有feature文件交叉验证:
读取目录(若存在),列出所有文件,排除名称以下划线开头的文件(例如:)。
ai-context/features/.md__template.md针对每个检测到的上下文候选对象,构建如下记录:
{ slug: 目录名称的短横线分隔格式, dir_path, existing_feature_file: bool }Slug命名规范: 将目录名称转为小写;将空格和下划线替换为短横线。示例: → , → 。
UserProfileuser-profileauth_serviceauth-service未找到上下文候选对象的情况:
- 记录日志:
"No bounded context directories detected." - 生成文件,在摘要部分注明"未检测到Bounded Context目录",并建议先运行
teach-report.md指令。/memory-init - 终止流程。
ai-context/features/- 记录日志:
"INFO: ai-context/features/ not found — feature files will be created if possible." - 在中注明:"运行时未找到ai-context/features/目录。建议先运行/memory-init指令生成目录结构,再重新运行/codebase-teach。"
teach-report.md - 继续执行流程(工具会根据需要创建目录和文件)。
输出:
context_list = [{ slug, dir_path, existing_feature_file: bool }, ...]记录每个找到的上下文:
"Detected context: [slug] — [dir_path] (feature file: [exists|absent])"Step 2 — Read key files per context (sequential)
步骤2 — 按上下文读取核心文件(顺序执行)
Process contexts one at a time. For each context in :
context_list-
Enumerate implementation files in(recursive), filtering for file extensions:
dir_path,.ts,.tsx,.js,.jsx,.py,.java,.kt,.rb,.go,.ex,.exs,.cs,.rs,.php.swift- Prioritize files by recency (most recently modified first)
- Exclude files matching: ,
*.test.*,*.spec.*,test_*,*_test.*,*.d.ts*.min.*
-
Apply file cap: read at mostfiles. If total enumerated >
max_files, log:max_files"[slug]: [total] files found — sampling [max_files] (cap applied)" -
For each file to read:
- Attempt to read the file
- If the file is binary or unreadable: skip it; record in with reason
skipped_files - If readable: extract the following signals:
- Business rules: explicit conditional constraints (if/when/unless logic that enforces domain rules)
- Invariants: assertions or validation guards that are always enforced
- Data model entities: class/struct/interface/type names with their key fields
- Integration points: imports of external services, APIs, or infrastructure adapters
-
Accumulate:
context_knowledge = { slug, rules[], invariants[], entities[], integrations[], files_read[], skipped[] }
逐个处理上下文列表中的每个上下文:
-
枚举实现文件:在目录下递归枚举文件,筛选以下扩展名的文件:
dir_path、.ts、.tsx、.js、.jsx、.py、.java、.kt、.rb、.go、.ex、.exs、.cs、.rs、.php.swift- 按文件修改时间排序(最新修改的优先)
- 排除匹配以下模式的文件:、
*.test.*、*.spec.*、test_*、*_test.*、*.d.ts*.min.*
-
应用文件数量上限:最多读取个文件。若枚举到的文件总数大于
max_files,记录日志:max_files"[slug]: [total] files found — sampling [max_files] (cap applied)" -
读取每个目标文件:
- 尝试读取文件内容
- 若文件为二进制文件或无法读取:跳过该文件,并在中记录原因
skipped_files - 若文件可读:提取以下信息:
- 业务规则:明确的条件约束(如if/when/unless逻辑,用于执行领域规则)
- 不变量:始终生效的断言或验证规则
- 数据模型实体:类/结构体/接口/类型的名称及其核心字段
- 集成点:外部服务、API或基础设施适配器的导入信息
-
收集信息:
context_knowledge = { slug, rules[], invariants[], entities[], integrations[], files_read[], skipped[] }
Step 3 — Write ai-context/features/<slug>.md
ai-context/features/<slug>.md步骤3 — 生成ai-context/features/<slug>.md
文件
ai-context/features/<slug>.mdFor each context processed in Step 2:
If does not exist (or is absent):
ai-context/features/<slug>.mdai-context/features/- Create the directory if needed
- Write a new file using the six-section structure below
- All AI-generated sections receive markers
[auto-updated]
If already exists:
ai-context/features/<slug>.md- Read the entire file
- Identify ...
<!-- [auto-updated]: codebase-teach ... -->blocks<!-- [/auto-updated] --> - Overwrite content only inside blocks
[auto-updated] - Preserve byte-for-byte all content outside any block
[auto-updated] - If no block exists for a section, append the section at the end of the file inside a new
[auto-updated]block[auto-updated]
Six-section feature file structure:
markdown
undefined针对步骤2中处理的每个上下文:
若文件不存在(或目录不存在):
ai-context/features/<slug>.mdai-context/features/- 若需要则创建目录
- 使用以下六段式结构生成新文件
- 所有AI生成的内容需添加标记
[auto-updated]
若文件已存在:
ai-context/features/<slug>.md- 读取整个文件内容
- 识别...
<!-- [auto-updated]: codebase-teach ... -->代码块<!-- [/auto-updated] --> - 仅覆盖代码块内的内容
[auto-updated] - 严格保留代码块外的所有人工编写内容
[auto-updated] - 若某章节不存在代码块,则在文件末尾添加该章节,并包裹在新的
[auto-updated]代码块内[auto-updated]
六段式feature文件结构:
markdown
undefined[Context Name] — Domain Knowledge
[上下文名称] — 领域知识
Last updated by: codebase-teach
Last run: YYYY-MM-DD
Last updated by: codebase-teach
Last run: YYYY-MM-DD
Domain Overview
领域概述
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
[AI-extracted 2–4 sentence summary of what this bounded context does and its primary responsibilities]
<!-- [/auto-updated] -->
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
[AI提取的2-4句话摘要,说明该Bounded Context的功能及主要职责]
<!-- [/auto-updated] -->
Business Rules and Invariants
业务规则与不变量
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
[AI-extracted explicit conditional constraints and always-true invariants from the source]
- Rule: [description]
- Invariant: [description]
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
[从源码中提取的明确条件约束及始终生效的不变量]
- Rule: [描述]
- Invariant: [描述]
Data Model Summary
数据模型摘要
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
<!-- [/auto-updated] -->
Key entities detected:
| Entity | Key Fields |
|---|---|
| [Name] | [field1, field2, ...] |
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
<!-- [/auto-updated] -->
检测到的核心实体:
| 实体 | 核心字段 |
|---|---|
| [名称] | [field1, field2, ...] |
Integration Points
集成点
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
External dependencies and integration touchpoints:
- [service/API name]: [what it is used for]
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
外部依赖与集成触点:
Decision Log
决策日志
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
<!-- Append new AI-detected decisions below. Human entries above this marker are preserved. -->
<!-- [/auto-updated] -->
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
<!-- 在下方追加AI检测到的新决策。此标记上方的人工编写内容将被保留。 -->
<!-- [/auto-updated] -->
Known Gotchas
已知注意事项
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
<!-- Append new AI-detected gotchas below. Human entries above this marker are preserved. -->
<!-- [/auto-updated] -->
**`_template.md` guard:** never read, write, or treat any file whose name begins with `_` as a feature context. Skip entirely.
---<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
<!-- 在下方追加AI检测到的新注意事项。此标记上方的人工编写内容将被保留。 -->
<!-- [/auto-updated] -->
**`_template.md`防护规则:** 永远不要读取、写入或处理任何以下划线`_`开头的feature上下文文件,直接跳过。
---Step 4 — Evaluate coverage and write teach-report.md
teach-report.md步骤4 — 评估覆盖度并生成teach-report.md
文件
teach-report.mdCoverage calculation:
documented_contexts = count of contexts that have an ai-context/features/<slug>.md file after Step 3
total_contexts = count of contexts in context_list
coverage_pct = (documented_contexts / total_contexts) * 100 (0 if total_contexts == 0)
gap_list = contexts in context_list where existing_feature_file was false before Step 3
AND no file was created in Step 3Write in the project working directory root (same level as ). Overwrite if it exists.
teach-report.mdanalysis-report.mdmarkdown
undefined覆盖度计算方式:
documented_contexts = 步骤3完成后拥有ai-context/features/<slug>.md文件的上下文数量
total_contexts = 上下文列表中的总数量
coverage_pct = (documented_contexts / total_contexts) * 100 (若total_contexts为0则为0)
gap_list = 步骤3之前existing_feature_file为false且步骤3未生成文件的上下文生成文件:在项目工作目录根目录(与同级)生成,若已存在则覆盖。
teach-report.mdanalysis-report.mdmarkdown
undefinedTeach Report — [Project Name]
Teach Report — [项目名称]
Last run: YYYY-MM-DD
Skill: codebase-teach
Last run: YYYY-MM-DD
Skill: codebase-teach
Summary
摘要
Contexts detected: [total_contexts]
Contexts documented: [documented_contexts]
Coverage: [coverage_pct]%
检测到的上下文数量:[total_contexts]
已文档化的上下文数量:[documented_contexts]
覆盖度:[coverage_pct]%
Coverage
覆盖度详情
[coverage_pct]% — [documented_contexts] of [total_contexts] contexts documented.
[coverage_pct]% — 共[total_contexts]个上下文,已文档化[documented_contexts]个。
Gaps
覆盖缺口
Contexts detected in code but not documented in ai-context/features/:
- [context-slug] — [dir_path]
[If no gaps: "None — all detected contexts are documented."]
代码中检测到但未在ai-context/features/目录中文档化的上下文:
- [context-slug] — [dir_path]
[若无缺口:"无缺口——所有检测到的上下文均已文档化。"]
Files Read
读取的文件
[context-slug]
[context-slug]
- [file path] — sampled
- [file path] — sampled
- [file path] — SKIPPED: [reason]
[Repeat for each context]
- [文件路径] — 已采样
- [文件路径] — 已采样
- [文件路径] — 已跳过:[原因]
[为每个上下文重复上述内容]
Sections Written / Updated
已写入/更新的章节
- ai-context/features/[context].md — [created|updated] — sections: [list of sections written]
If `ai-context/features/` was absent at run time, append to the Summary section:
> Note: ai-context/features/ was absent at run time. Recommend running /memory-init to scaffold the directory before re-running /codebase-teach.
---- ai-context/features/[context].md — [已创建|已更新] — 已写入章节:[章节列表]
若运行时`ai-context/features/`目录不存在,在摘要部分追加:
> 注意:运行时未找到ai-context/features/目录。建议先运行/memory-init指令生成目录结构,再重新运行/codebase-teach。
---Rules
规则
- MUST NOT modify ,
ai-context/stack.md,ai-context/architecture.md,ai-context/conventions.md, orai-context/known-issues.mdai-context/changelog-ai.md - MUST NOT modify any file under
docs/ - MUST NOT be invoked automatically by any other skill — user-initiated only
- MUST skip any file or directory whose name begins with in
_ai-context/features/ - MUST preserve all human-authored content outside markers when updating existing feature files
[auto-updated] - MUST process contexts sequentially — never in parallel
- MUST apply the cap (default 10) to every context
teach_max_files_per_context - MUST list skipped files in under the "Files Read" section for the relevant context
teach-report.md - MUST complete without error even when no bounded context directories are detected
- MUST write on every successful run
teach-report.md - marker format:
[auto-updated]...<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->— consistent with<!-- [/auto-updated] -->conventionproject-analyze
- 禁止修改、
ai-context/stack.md、ai-context/architecture.md、ai-context/conventions.md或ai-context/known-issues.md文件ai-context/changelog-ai.md - 禁止修改目录下的任何文件
docs/ - 禁止被其他工具自动触发——仅允许用户手动触发
- 必须跳过目录下所有以下划线
ai-context/features/开头的文件或目录_ - 必须在更新现有feature文件时,保留标记外的所有人工编写内容
[auto-updated] - 必须按顺序处理上下文——禁止并行处理
- 必须为每个上下文应用数量上限(默认值为10)
teach_max_files_per_context - 必须在的"读取的文件"章节中列出每个上下文的跳过文件
teach-report.md - 必须在未检测到Bounded Context目录时仍能正常完成流程
- 必须在每次成功运行时生成文件
teach-report.md - 标记格式:
[auto-updated]...<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->— 与<!-- [/auto-updated] -->的标记格式保持一致project-analyze
Output
输出结果
teach-report.md
(mandatory — written to project root on every run)
teach-report.mdteach-report.md
(必填——每次运行均会在项目根目录生成)
teach-report.mdRequired sections:
| Section | Content |
|---|---|
| Summary | Contexts detected, documented, coverage % |
| Coverage | Percentage and ratio |
| Gaps | Contexts found in code but undocumented (or "None") |
| Files Read | Per-context list of files sampled and skipped |
| Sections Written / Updated | Per-feature-file: created or updated, which sections |
必填章节:
| 章节 | 内容 |
|---|---|
| 摘要 | 检测到的上下文数量、已文档化数量、覆盖度百分比 |
| 覆盖度详情 | 覆盖度百分比及比例 |
| 覆盖缺口 | 代码中存在但未文档化的上下文(或"无缺口") |
| 读取的文件 | 按上下文列出已采样和已跳过的文件 |
| 已写入/更新的章节 | 按feature文件列出:已创建或已更新、已处理的章节 |
ai-context/features/<context>.md
(one per bounded context)
ai-context/features/<context>.mdai-context/features/<context>.md
(每个Bounded Context对应一个文件)
ai-context/features/<context>.mdWritten or updated during Step 3. Six sections with markers on all AI-generated content.
[auto-updated]在步骤3中生成或更新。包含六段式结构,所有AI生成的内容均带有标记。",
[auto-updated]