codebase-teach

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

codebase-teach

codebase-teach

Analyzes bounded contexts from the project's source tree, extracts domain knowledge per context, writes
ai-context/features/<context>.md
files, and produces
teach-report.md
with coverage metrics.
Triggers:
/codebase-teach
, teach codebase, extract domain knowledge, update feature docs, analyze bounded contexts

从项目源码树中分析Bounded Contexts,提取每个上下文的领域知识,生成
ai-context/features/<context>.md
文件,并输出包含覆盖度指标的
teach-report.md
文件。
触发指令
/codebase-teach
、teach codebase、extract domain knowledge、update feature docs、analyze bounded contexts

Purpose

用途

codebase-teach
fills the
ai-context/features/
layer with structured domain knowledge derived from reading source code. It is the deep-read complement to
memory-update
(which records session decisions) and
memory-init
(which scaffolds empty stubs). It MUST only be invoked manually by the user.

codebase-teach
通过读取源码,为
ai-context/features/
目录填充结构化的领域知识。它是
memory-update
(记录会话决策)和
memory-init
(生成空的存根文件)的深度读取补充工具。该工具必须仅由用户手动触发。

Process

执行流程

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
status: blocked
or
status: failed
.
  1. Read
    ai-context/stack.md
    — tech stack, versions, key tools.
  2. Read
    ai-context/architecture.md
    — architectural decisions and rationale.
  3. Read
    ai-context/conventions.md
    — naming patterns, code conventions.
  4. Read the project's
    CLAUDE.md
    (at project root) and extract the
    ## Skills Registry
    section.
For each file:
  • If absent: log
    INFO: [filename] not found — proceeding without it.
  • If present: extract
    Last updated:
    or
    Last analyzed:
    date. If date is older than 30 days: log
    NOTE: [filename] last updated [date] — context may be stale. Consider running /memory-update or /project-analyze.
Also read project config file (
config.yaml
at project root) if it exists, and extract
teach_max_files_per_context
if present:
  • If present:
    max_files = teach_max_files_per_context
  • If absent:
    max_files = 10
    (default)
Log:
"File cap per context: [max_files] (source: config.yaml)"
or
"File cap per context: 10 (default)"

此步骤为非阻塞:任何失败(文件缺失、无法读取)最多只能生成INFO级别的日志。此步骤不得返回
status: blocked
status: failed
状态。
  1. 读取
    ai-context/stack.md
    文件——包含技术栈、版本信息及核心工具。
  2. 读取
    ai-context/architecture.md
    文件——包含架构决策及理由。
  3. 读取
    ai-context/conventions.md
    文件——包含命名规范、代码约定。
  4. 读取项目根目录下的
    CLAUDE.md
    文件,并提取
    ## Skills Registry
    章节内容。
对于每个文件:
  • 若文件不存在:记录日志
    INFO: [filename] not found — proceeding without it.
  • 若文件存在:提取
    Last updated:
    Last analyzed:
    对应的日期。若日期早于30天:记录日志
    NOTE: [filename] last updated [date] — context may be stale. Consider running /memory-update or /project-analyze.
同时读取项目根目录下的配置文件
config.yaml
(若存在),并提取其中的
teach_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):
  1. src/
    — subdirectories at depth 1 (e.g.,
    src/auth/
    ,
    src/payments/
    )
  2. app/
    — subdirectories at depth 1
  3. features/
    — subdirectories at depth 1
  4. domain/
    — subdirectories at depth 1
  5. ai-context/features/
    — existing feature file names (each file stem is treated as a context name)
Exclusion rules — skip directories named:
shared
,
utils
,
common
,
lib
,
types
,
hooks
,
components
,
__tests__
,
test
,
tests
,
node_modules
,
.git
Cross-reference with existing feature files: Read the
ai-context/features/
directory (if it exists) and list all
.md
files, excluding files whose names begin with
_
(e.g.,
_template.md
).
For 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:
UserProfile
user-profile
,
auth_service
auth-service
.
If no context candidates are found:
  • Log:
    "No bounded context directories detected."
  • Write
    teach-report.md
    with a Summary noting "No bounded context directories detected" and a recommendation to run
    /memory-init
    first.
  • Stop here.
If
ai-context/features/
does not exist:
  • Log:
    "INFO: ai-context/features/ not found — feature files will be created if possible."
  • Note in
    teach-report.md
    : "ai-context/features/ was absent at run time. Recommend running /memory-init to scaffold the directory before re-running /codebase-teach."
  • 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/
    目录——深度为1的子目录(例如:
    src/auth/
    src/payments/
  2. app/
    目录——深度为1的子目录
  3. features/
    目录——深度为1的子目录
  4. domain/
    目录——深度为1的子目录
  5. ai-context/features/
    目录——已存在的feature文件名(每个文件的主干名称作为上下文名称)
排除规则——跳过以下名称的目录:
shared
utils
common
lib
types
hooks
components
__tests__
test
tests
node_modules
.git
与现有feature文件交叉验证: 读取
ai-context/features/
目录(若存在),列出所有
.md
文件,排除名称以下划线
_
开头的文件(例如:
_template.md
)。
针对每个检测到的上下文候选对象,构建如下记录:
{ slug: 目录名称的短横线分隔格式, dir_path, existing_feature_file: bool }
Slug命名规范: 将目录名称转为小写;将空格和下划线替换为短横线。示例:
UserProfile
user-profile
auth_service
auth-service
未找到上下文候选对象的情况:
  • 记录日志:
    "No bounded context directories detected."
  • 生成
    teach-report.md
    文件,在摘要部分注明"未检测到Bounded Context目录",并建议先运行
    /memory-init
    指令。
  • 终止流程。
ai-context/features/
目录不存在的情况:
  • 记录日志:
    "INFO: ai-context/features/ not found — feature files will be created if possible."
  • teach-report.md
    中注明:"运行时未找到ai-context/features/目录。建议先运行/memory-init指令生成目录结构,再重新运行/codebase-teach。"
  • 继续执行流程(工具会根据需要创建目录和文件)。
输出:
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
:
  1. Enumerate implementation files in
    dir_path
    (recursive), filtering for file extensions:
    .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.*
  2. Apply file cap: read at most
    max_files
    files. If total enumerated >
    max_files
    , log:
    "[slug]: [total] files found — sampling [max_files] (cap applied)"
  3. For each file to read:
    • Attempt to read the file
    • If the file is binary or unreadable: skip it; record in
      skipped_files
      with reason
    • 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
  4. Accumulate:
    context_knowledge = { slug, rules[], invariants[], entities[], integrations[], files_read[], skipped[] }

逐个处理上下文列表中的每个上下文:
  1. 枚举实现文件:在
    dir_path
    目录下递归枚举文件,筛选以下扩展名的文件:
    .ts
    .tsx
    .js
    .jsx
    .py
    .java
    .kt
    .rb
    .go
    .ex
    .exs
    .cs
    .rs
    .php
    .swift
    • 按文件修改时间排序(最新修改的优先)
    • 排除匹配以下模式的文件:
      *.test.*
      *.spec.*
      test_*
      *_test.*
      *.d.ts
      *.min.*
  2. 应用文件数量上限:最多读取
    max_files
    个文件。若枚举到的文件总数大于
    max_files
    ,记录日志:
    "[slug]: [total] files found — sampling [max_files] (cap applied)"
  3. 读取每个目标文件
    • 尝试读取文件内容
    • 若文件为二进制文件或无法读取:跳过该文件,并在
      skipped_files
      中记录原因
    • 若文件可读:提取以下信息:
      • 业务规则:明确的条件约束(如if/when/unless逻辑,用于执行领域规则)
      • 不变量:始终生效的断言或验证规则
      • 数据模型实体:类/结构体/接口/类型的名称及其核心字段
      • 集成点:外部服务、API或基础设施适配器的导入信息
  4. 收集信息:
    context_knowledge = { slug, rules[], invariants[], entities[], integrations[], files_read[], skipped[] }

Step 3 — Write
ai-context/features/<slug>.md

步骤3 — 生成
ai-context/features/<slug>.md
文件

For each context processed in Step 2:
If
ai-context/features/<slug>.md
does not exist
(or
ai-context/features/
is absent):
  • Create the directory if needed
  • Write a new file using the six-section structure below
  • All AI-generated sections receive
    [auto-updated]
    markers
If
ai-context/features/<slug>.md
already exists:
  • Read the entire file
  • Identify
    <!-- [auto-updated]: codebase-teach ... -->
    ...
    <!-- [/auto-updated] -->
    blocks
  • Overwrite content only inside
    [auto-updated]
    blocks
  • Preserve byte-for-byte all content outside any
    [auto-updated]
    block
  • If no
    [auto-updated]
    block exists for a section, append the section at the end of the file inside a new
    [auto-updated]
    block
Six-section feature file structure:
markdown
undefined
针对步骤2中处理的每个上下文:
ai-context/features/<slug>.md
文件不存在
(或
ai-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] -->
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
[从源码中提取的明确条件约束及始终生效的不变量]
  • Rule: [描述]
  • Invariant: [描述]
<!-- [/auto-updated] -->

Data Model Summary

数据模型摘要

<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
Key entities detected:
EntityKey Fields
[Name][field1, field2, ...]
<!-- [/auto-updated] -->
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
检测到的核心实体:
实体核心字段
[名称][field1, field2, ...]
<!-- [/auto-updated] -->

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] -->
<!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
外部依赖与集成触点:
<!-- [/auto-updated] -->

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

步骤4 — 评估覆盖度并生成
teach-report.md
文件

Coverage 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 3
Write
teach-report.md
in the project working directory root (same level as
analysis-report.md
). Overwrite if it exists.
markdown
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.md
文件
:在项目工作目录根目录(与
analysis-report.md
同级)生成,若已存在则覆盖。
markdown
undefined

Teach 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
    ,
    ai-context/known-issues.md
    , or
    ai-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
    [auto-updated]
    markers when updating existing feature files
  • MUST process contexts sequentially — never in parallel
  • MUST apply the
    teach_max_files_per_context
    cap (default 10) to every context
  • MUST list skipped files in
    teach-report.md
    under the "Files Read" section for the relevant context
  • MUST complete without error even when no bounded context directories are detected
  • MUST write
    teach-report.md
    on every successful run
  • [auto-updated]
    marker format:
    <!-- [auto-updated]: codebase-teach — last run: YYYY-MM-DD -->
    ...
    <!-- [/auto-updated] -->
    — consistent with
    project-analyze
    convention

  • 禁止修改
    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]
    标记外的所有人工编写内容
  • 必须按顺序处理上下文——禁止并行处理
  • 必须为每个上下文应用
    teach_max_files_per_context
    数量上限(默认值为10)
  • 必须
    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.md
(必填——每次运行均会在项目根目录生成)

Required sections:
SectionContent
SummaryContexts detected, documented, coverage %
CoveragePercentage and ratio
GapsContexts found in code but undocumented (or "None")
Files ReadPer-context list of files sampled and skipped
Sections Written / UpdatedPer-feature-file: created or updated, which sections
必填章节:
章节内容
摘要检测到的上下文数量、已文档化数量、覆盖度百分比
覆盖度详情覆盖度百分比及比例
覆盖缺口代码中存在但未文档化的上下文(或"无缺口")
读取的文件按上下文列出已采样和已跳过的文件
已写入/更新的章节按feature文件列出:已创建或已更新、已处理的章节

ai-context/features/<context>.md
(one per bounded context)

ai-context/features/<context>.md
(每个Bounded Context对应一个文件)

Written or updated during Step 3. Six sections with
[auto-updated]
markers on all AI-generated content.
在步骤3中生成或更新。包含六段式结构,所有AI生成的内容均带有
[auto-updated]
标记。",