drive-to-legacy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDrive to Legacy - Systematic Legacy Code Exploration
Drive to Legacy——系统化遗留代码探索
Overview
概述
A 6-phase systematic approach to fully understand an unfamiliar codebase, producing a comprehensive architecture document with mermaid diagrams that both humans and AI can use for porting, migration, or onboarding.
Core principle: Explore wide before deep. Map the full territory first, then drill into each layer. Every finding goes into a living ARCHITECTURE.md with diagrams.
这是一种分为6个阶段的系统化方法,用于全面理解不熟悉的代码库,生成包含Mermaid图表的综合性架构文档,供人类和AI用于移植、迁移或入职培训。
核心原则:先广度探索,再深度钻研。先绘制完整的代码版图,再深入每个层级。所有发现都要记录到动态更新的ARCHITECTURE.md文档中,并附带图表。
When to Use
适用场景
- Onboarding to an unfamiliar codebase
- Preparing to port or rewrite an application
- Documenting a legacy system that lacks documentation
- Studying code you need to maintain but did not write
- Any "how does this work?" question about a full project
When NOT to use:
- Small bug fixes in code you already understand
- Greenfield projects (no legacy to explore)
- Single-file scripts or utilities
- 入职新代码库时
- 准备移植或重写应用时
- 为缺乏文档的遗留系统编写文档时
- 研究需要维护但并非自己编写的代码时
- 针对整个项目提出的任何「这是如何工作的?」问题
不适用场景:
- 在已理解的代码中进行小bug修复
- 全新项目(无遗留代码需要探索)
- 单文件脚本或工具类代码
The 6 Phases
6个阶段
Follow these phases in order. Each phase builds on the previous. Use parallel Explore agents where possible to speed up discovery.
请按顺序遵循这些阶段,每个阶段都建立在前一阶段的基础上。尽可能使用并行Explore Agent来加快探索速度。
Phase 1: User Stories
阶段1:用户故事
Goal: Describe WHAT the app does from the user's perspective. This comes first because it answers "why does this app exist?" — the context that makes all technical details meaningful.
- Read /
package.jsonto understand the app's purposeREADME.md - Read entry points and main container components to identify the UI flow
- Walk through component render methods to find every user action
- Trace each action to its handler and side effects
- Note conditional paths (different behavior for different roles or data states)
Write to ARCHITECTURE.md:
- Section: "User Stories" with one subsection per story
- Each story: user story statement + mermaid showing the interaction
sequenceDiagram
Template per story:
markdown
undefined目标: 从用户视角描述应用的功能。这一步要优先完成,因为它回答了「这个应用存在的意义是什么?」——这是让所有技术细节变得有意义的上下文。
- 阅读/
package.json以理解应用的用途README.md - 阅读入口文件和主容器组件,识别UI流程
- 遍历组件渲染方法,找出所有用户操作
- 追踪每个操作对应的处理器和副作用
- 记录条件分支(不同角色或数据状态下的不同行为)
写入ARCHITECTURE.md:
- 章节:「用户故事」,每个故事对应一个子章节
- 每个故事:用户故事陈述 + 展示交互流程的mermaid
sequenceDiagram
故事模板:
markdown
undefinedUS-XX: Story Title
US-XX: 故事标题
AS A [role]
I WANT TO [action]
SO THAT [outcome]
(mermaid sequence diagram)
undefined作为[角色]
我想要[操作]
以便[结果]
(mermaid sequence diagram)
undefinedPhase 2: Project Identity and Component Map
阶段2:项目标识与组件映射
Goal: Know the tech stack, file layout, and component tree.
- Read build config (,
vite.config.ts,webpack.config.js, etc.)tsconfig.json - List the top-level directory structure
- Glob for all source files to see the shape: ,
**/*.tsx,**/*.ts, etc.**/*.vue - Read each main component/module entry point (not every file, just the key ones)
- Trace the component hierarchy from root down
- Identify reusable/shared components vs. page-specific ones
Write to ARCHITECTURE.md:
- Section: "What This App Does" (3 sentences max)
- Section: "Tech Stack and Key Dependencies" (bullet list)
- Section: "File-to-Responsibility Map" (tree with one-line descriptions)
- Section: "Component Hierarchy" (mermaid diagram)
graph TD - Section: "State Management Overview" (mermaid diagram showing where state lives)
目标: 了解技术栈、文件布局和组件树。
- 阅读构建配置文件(、
vite.config.ts、webpack.config.js等)tsconfig.json - 列出顶级目录结构
- 全局搜索所有源文件以了解代码形态:、
**/*.tsx、**/*.ts等**/*.vue - 阅读每个主要组件/模块的入口文件(无需阅读每个文件,只需关键文件)
- 从根组件向下追踪组件层级
- 区分可复用/共享组件与页面专属组件
写入ARCHITECTURE.md:
- 章节:「应用功能概述」(最多3句话)
- 章节:「技术栈与核心依赖」(项目符号列表)
- 章节:「文件职责映射」(带单行描述的树形结构)
- 章节:「组件层级」(mermaid 图表)
graph TD - 章节:「状态管理概述」(展示状态存储位置的mermaid图表)
Phase 3: Data Model
阶段3:数据模型
Goal: Understand every entity, type, and relationship.
- Read all type/interface/model files
- Read the service/API layer to find all entities referenced
- Map relationships between entities (foreign keys, lookups, joins)
- Identify choice/enum fields and their values
Capture:
- Every TypeScript interface / database model / API schema
- Entity relationships (one-to-many, many-to-many)
- Key constants and magic numbers with their meanings
Write to ARCHITECTURE.md:
- Section: "CRM/Database Entity Relationship Diagram" (mermaid with field lists)
erDiagram - Section: "Key Constants and Option Set Values" (table)
目标: 理解每个实体、类型及其关系。
- 阅读所有类型/接口/模型文件
- 阅读服务/API层,找出所有引用的实体
- 映射实体间的关系(外键、查找、关联)
- 识别选项/枚举字段及其值
记录内容:
- 所有TypeScript接口/数据库模型/API schema
- 实体关系(一对多、多对多)
- 关键常量和魔法数字及其含义
写入ARCHITECTURE.md:
- 章节:「CRM/数据库实体关系图」(带字段列表的mermaid )
erDiagram - 章节:「关键常量与选项集值」(表格)
Phase 4: Business Logic
阶段4:业务逻辑
Goal: Extract every rule, validation, cascade, and conditional behavior.
This is the deepest phase. Read line by line through:
- Form validation rules - what is required, when, why
- Field cascade logic - selecting A resets B, populates C
- Conditional rendering - what shows/hides and when
- Data transformation - how input maps to output on save
- Silent fallbacks - what happens when data is missing (nullability chains)
Write to ARCHITECTURE.md:
- Section: "Business Logic and Rules" with subsections for each rule domain
- Use mermaid for decision trees
flowchart TD - Use tables for validation rules and field mappings
- Include a "Nullability and Silent Fallbacks" table (what can be undefined and what happens)
- Include an "Auto-Population Rules" section (what triggers auto-fill of which fields)
目标: 提取所有规则、验证、级联逻辑和条件行为。
这是最深入的阶段,需要逐行阅读以下内容:
- 表单验证规则 - 哪些字段是必填项、何时需要验证、原因是什么
- 字段级联逻辑 - 选择A会重置B、填充C
- 条件渲染 - 哪些内容会显示/隐藏以及触发条件
- 数据转换 - 保存时输入如何映射到输出
- 静默降级逻辑 - 数据缺失时的处理方式(空值链)
写入ARCHITECTURE.md:
- 章节:「业务逻辑与规则」,每个规则领域对应一个子章节
- 使用mermaid 绘制决策树
flowchart TD - 使用表格展示验证规则和字段映射
- 包含「空值处理与静默降级」表格(哪些字段可为undefined以及对应的处理逻辑)
- 包含「自动填充规则」章节(哪些触发条件会自动填充哪些字段)
Phase 5: API Reference
阶段5:API参考
Goal: Document every data query with exact parameters, filters, joins, and purpose.
CRITICAL RULE: Every API endpoint MUST have a Why row. The Why explains the business reason the data is needed - not just "fetches X from Y" but why the application cannot function without this call. Without Why, a developer porting the code cannot tell which APIs are essential vs. nice-to-have, or what would break if the API were removed or changed.
For each service method / API call:
- Read the exact query (SQL, FetchXml, OData, GraphQL, REST URL)
- Document parameters, entity, joins, filters
- Note when it is called and by which component
- Explain WHY it exists (business purpose, not just technical description)
The Why row must answer: "What business goal does this query serve? What breaks or becomes impossible without it?"
Good Why examples:
- "Populates dropdown options so the form can render selectable choices. These are CRM choice-column labels, not hardcoded."
- "Finds the pre-defined stock item (inventory lane) for a customer + make between POL/POD ports. Links cargo to inventory forecasting."
- "Determines which other schedules can carry cargo onward. Same vessel for Cross, different vessel for Transshipment."
Bad Why examples:
- "Fetches data from the database" (too vague, says nothing about business purpose)
- "Called when user clicks save" (that's When, not Why)
- "Returns a list of records" (describes what, not why)
Write to ARCHITECTURE.md:
- Section: "API Reference" with one subsection per endpoint
- Each endpoint gets a properties table with Why always after When:
| Property | Value |
|---|---|
| Called by | Component or function name |
| API method | The actual call pattern |
| Entity | Table/endpoint queried |
| Parameters | Input values |
| When | Trigger condition |
| Why | Business reason this data is needed |
- Include the exact query (SQL/FetchXml/GraphQL) in a code block
- Include a mermaid join diagram for complex multi-table queries
- End with an "API Call Map by User Action" (mermaid flowchart showing which APIs fire on which user action)
Also document inline queries - queries defined inside components (not just in the service layer). These are easy to miss.
目标: 记录每个数据查询的精确参数、过滤器、关联和用途。
关键规则:每个API端点必须包含「原因(Why)」行。 「原因」要解释获取该数据的业务理由——不仅仅是「从Y获取X」,还要说明应用没有这个调用就无法正常运行的原因。如果没有「原因」,移植代码的开发者无法判断哪些API是必需的、哪些是锦上添花的,也无法知道如果API被移除或修改会导致什么问题。
针对每个服务方法/API调用:
- 阅读精确的查询语句(SQL、FetchXml、OData、GraphQL、REST URL)
- 记录参数、实体、关联、过滤器
- 记录调用时机和调用组件
- 解释存在的原因(业务目的,而非仅技术描述)
「原因」行必须回答:「这个查询服务于什么业务目标?没有它会导致什么功能失效或无法实现?」
优秀的「原因」示例:
- 「填充下拉选项,以便表单渲染可选择的选项。这些是CRM选择列的标签,而非硬编码值。」
- 「为客户+运输航线在POL/POD港口之间查找预定义的库存项(库存通道)。将货物与库存预测关联起来。」
- 「确定哪些其他班次可以转运货物。Cross航线使用同一艘船,Transshipment航线使用不同的船。」
糟糕的「原因」示例:
- 「从数据库获取数据」(过于模糊,未说明业务目的)
- 「用户点击保存时调用」(这是调用时机,不是原因)
- 「返回记录列表」(描述了内容,而非原因)
写入ARCHITECTURE.md:
- 章节:「API参考」,每个端点对应一个子章节
- 每个端点包含属性表格,「原因」始终紧跟「时机」:
| 属性 | 值 |
|---|---|
| 调用方 | 组件或函数名称 |
| API方法 | 实际调用模式 |
| 实体 | 查询的表/端点 |
| 参数 | 输入值 |
| 时机 | 触发条件 |
| 原因 | 获取该数据的业务理由 |
- 在代码块中包含精确的查询语句(SQL/FetchXml/GraphQL)
- 复杂的多表查询使用mermaid关联图展示
- 最后添加「按用户操作分类的API调用映射」(mermaid流程图展示哪些用户操作会触发哪些API调用)
还要记录内联查询——在组件内部定义的查询(不仅仅是服务层中的查询),这些很容易被遗漏。
Phase 6: Build and Deploy
阶段6:构建与部署
Goal: Know how to run, build, and ship.
- Read build scripts and CI/CD config
- Read deployment scripts or docs
- Note environment-specific configs
Write to ARCHITECTURE.md:
- Section: "Build and Deploy Pipeline" (mermaid from dev to production)
graph TD - Section: "LookupFieldInput / Reusable Components" (document non-obvious reusable patterns)
目标: 了解如何运行、构建和发布应用。
- 阅读构建脚本和CI/CD配置
- 阅读部署脚本或文档
- 记录环境特定的配置
写入ARCHITECTURE.md:
- 章节:「构建与部署流水线」(从开发到生产的mermaid 图表)
graph TD - 章节:「LookupFieldInput / 可复用组件」(记录非显而易见的可复用模式)
ARCHITECTURE.md Structure
ARCHITECTURE.md结构
The final document should follow this reading order (user stories first — they answer "why does this app exist?" and make everything else meaningful):
undefined最终文档应遵循以下阅读顺序(用户故事优先——它们回答了「这个应用存在的意义是什么?」,让其他所有内容都变得有意义):
undefinedProject Name - Architecture Reference
项目名称 - 架构参考
One-paragraph summary
一段摘要
Table of Contents (clickable anchor links)
目录(可点击锚点链接)
(overview Mermaid diagram here — the convention's mandatory document thumbnail)
(此处放置概述Mermaid图表——这是约定的必备文档缩略图)
Part 1: User Stories
第一部分:用户故事
- One subsection per story with sequence diagrams
- 每个故事对应一个子章节,附带序列图
Part 2: Overview and Project Structure
第二部分:概述与项目结构
- What This App Does
- Tech Stack and Key Dependencies
- File-to-Responsibility Map
- Key Constants and Option Set Values
- Component Hierarchy (diagram)
- Entity Relationship Diagram
- State Management Overview (diagram)
- 应用功能概述
- 技术栈与核心依赖
- 文件职责映射
- 关键常量与选项集值
- 组件层级(图表)
- 实体关系图
- 状态管理概述(图表)
Part 3: Application Flow
第三部分:应用流程
- Bootstrap / initialization sequence
- Main user flows as sequence diagrams
- Save / submit flow
- 启动/初始化序列
- 主要用户流程(序列图)
- 保存/提交流程
Part 4: Business Logic and Rules
第四部分:业务逻辑与规则
- Subsection per rule domain with flowcharts
- Validation rules
- Cascade logic
- Conditional rendering rules
- Auto-population rules
- Nullability and silent fallbacks
- 每个规则领域对应一个子章节,附带流程图
- 验证规则
- 级联逻辑
- 条件渲染规则
- 自动填充规则
- 空值处理与静默降级
Part 5: Data Model and Field Mapping
第五部分:数据模型与字段映射
- TypeScript-to-database field mapping (diagram)
- Conditional field mapping rules
- TypeScript到数据库的字段映射(图表)
- 条件字段映射规则
Part 6: API Reference
第六部分:API参考
- Overview of query patterns used
- One subsection per endpoint (properties table + exact query + why)
- Inline component queries
- API call map by user action (diagram)
- 使用的查询模式概述
- 每个端点对应一个子章节(属性表格 + 精确查询 + 原因)
- 组件内联查询
- 按用户操作分类的API调用映射(图表)
Part 7: Technical Components
第七部分:技术组件
- Reusable components worth understanding
- Build and deploy pipeline
undefined- 需要重点理解的可复用组件
- 构建与部署流水线
undefinedRules for the Architecture Document
架构文档规则
- Clickable TOC - every section linked with anchor. Avoid ,
&,--,/,*in headings (they break anchor generation).() - Mermaid diagrams everywhere - sequence diagrams for flows, flowcharts for logic, ER diagrams for data, graph TD for hierarchies. This implements the marketplace diagram convention () — including its mandatory overview diagram at the top of the document.
${CLAUDE_PLUGIN_ROOT}/references/diagram-convention.md - Every API table must have a Why row - business purpose, not just technical trigger.
- Tables for scannable data - validation rules, field mappings, constants, nullability.
- No speculation - only document what you read in the code. If behavior is unclear, note it as unclear.
- Horizontal rules () between Parts for visual separation.
---
- 可点击目录 - 每个章节都带有锚点链接。标题中避免使用、
&、--、/、*等字符(它们会破坏锚点生成)。() - 处处使用Mermaid图表 - 序列图展示流程、流程图展示逻辑、ER图展示数据、graph TD展示层级。这遵循了市场图表约定()——包括在文档顶部放置必备的概述图表。
${CLAUDE_PLUGIN_ROOT}/references/diagram-convention.md - 每个API表格必须包含「原因」行 - 说明业务目的,而非仅技术触发条件。
- 表格展示可扫描数据 - 验证规则、字段映射、常量、空值处理。
- 不做推测 - 仅记录从代码中读取到的内容。如果行为不明确,要标注为「不明确」。
- 各部分之间使用水平分隔线() - 用于视觉区分。
---
Parallel Exploration Strategy
并行探索策略
Use multiple Explore agents in parallel to speed up Phase 1-3:
Agent 1: Read entry points, config, types, UI flows → Phase 1 + Phase 2
Agent 2: Read all types, service layer, entity references → Phase 3
Agent 3: Read service layer, extract all queries → Phase 5Then do Phase 4 (business logic) sequentially — it requires understanding from all previous phases.
使用多个Explore Agent并行处理以加快阶段1-3的进度:
Agent 1: 读取入口文件、配置、类型、UI流程 → 阶段1 + 阶段2
Agent 2: 读取所有类型、服务层、实体引用 → 阶段3
Agent 3: 读取服务层、提取所有查询 → 阶段5然后按顺序处理阶段4(业务逻辑)——它需要基于前几个阶段的理解。
Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
| Starting with business logic before understanding structure | Follow phases in order. Phase 1-3 first. |
| Missing inline queries in components | Grep for query patterns in component files, not just service files |
| Writing "what" without "why" in API docs | Every API endpoint MUST have a Why row explaining the business reason, not just technical description. "Fetches data" is not a Why. |
| Why row says "fetches X" or "returns Y" | That describes What, not Why. Ask: "What breaks without this call? What business goal does it serve?" |
| Headings with special characters breaking TOC | Use only alphanumeric, spaces, and hyphens in headings |
| Enormous single-pass exploration | Use parallel agents. Split by concern. |
| Documenting only the happy path | Include error handling, fallbacks, and nullability chains |
| Skipping the user stories | User stories provide the "why" context that makes technical docs useful |
| 错误 | 修复方案 |
|---|---|
| 在理解结构之前就开始研究业务逻辑 | 按顺序遵循阶段,先完成阶段1-3。 |
| 遗漏组件中的内联查询 | 在组件文件中搜索查询模式,而不仅仅是服务文件 |
| API文档只写「是什么」不写「为什么」 | 每个API端点必须包含「原因」行,说明业务理由,而非仅技术描述。「获取数据」不是有效的原因。 |
| 「原因」行写「获取X」或「返回Y」 | 这描述的是「是什么」,不是「为什么」。要问:「没有这个调用会导致什么失效?它服务于什么业务目标?」 |
| 标题包含特殊字符导致目录失效 | 标题中仅使用字母数字、空格和连字符 |
| 单次探索范围过大 | 使用并行Agent,按关注点拆分任务。 |
| 仅记录正常流程 | 要包含错误处理、降级逻辑和空值链 |
| 跳过用户故事 | 用户故事提供了「为什么」的上下文,让技术文档更有价值 |