Files
zhuiguang-ai/.trae/ai_coding_knowledge.json
T

447 lines
24 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"schema_version": "1.0",
"type": "ai_coding_knowledge_graph",
"name": "AI 辅助编程效率知识库",
"description": "整合当前最有效的 AI 辅助编程方法、工具、工作流与反模式。机器可解析:AI 代理在每次任务开始前加载本文件(优先按 entity.type / applies_to / id 过滤),作为协作方法论;本文件不描述业务,业务知识见 .trae/knowledge_graph.jsonl。",
"version": "1.0.0",
"updated_at": "2026-10-01T16:58:00",
"project": "追光AI",
"machine_contract": {
"entity_required_fields": ["id", "type", "name", "summary"],
"entity_id_format": "{type}:{kebab-case-slug}",
"entity_types": ["tool", "method", "workflow", "principle", "anti_pattern"],
"relation_fields": ["from", "to", "type"],
"load_priority": ["principle", "method", "workflow", "anti_pattern", "tool"],
"note": "所有字段值为稳定英文 key 或中文短句;AI 可仅依赖 id/type/category/relations 做推理,summary 用于人类复核。"
},
"bound_to_project": {
"toolchain": ["Trae IDE", "Next.js 14 App Router", "TypeScript", "Prisma 7.8", "Vitest", "Docker"],
"deterministic_gates": ["npx tsc --noEmit", "npx vitest run"],
"persistent_context": [".trae/rules/*.md", ".trae/ai_coding_knowledge.json", ".trae/knowledge_graph.jsonl"],
"auto_record_entry": "node scripts/update-knowledge.mjs",
"note": "以上为本项目把通用方法落地的具体载体,替换其他项目时只需替换本块。"
},
"sources": [
"AGENTS.md 开放标准(Agentic AI Foundation / Linux Foundation)",
"OpenAI Codex 工程团队指南(2026)",
"Anthropic Claude Code 最佳实践(2026)",
"Cursor Rules / .mdc 规范(2026)",
"Trae IDE 官方规则与 MCP 文档(2026)"
],
"entities": [
{
"id": "principle:stateless-agent",
"type": "principle",
"name": "Agent 无状态",
"summary": "AI 代理每次会话从零开始,不记得上次讨论。规则文件与知识库的唯一作用是注入跨会话持久上下文。",
"applies_to": ["理解为什么必须维护 rules + 知识库"],
"consequence": "上下文不落盘 = 每次都重新犯错;落盘 = 迭代效率复利"
},
{
"id": "principle:enforcement-pyramid",
"type": "principle",
"name": "执行金字塔(Advisory → Gate → Hook)",
"summary": "规则文件只是建议层。关键标准必须逐级下沉:rules(建议)→ CI/测试门禁(强制)→ hooks(确定性)。",
"applies_to": ["关键规范落地", "防止规则被忽略"],
"levels": ["advisory: .trae/rules/*.md", "gate: npm test / tsc --noEmit / code review", "hook: git pre-commit / post-commit / CI"]
},
{
"id": "principle:context-budget",
"type": "principle",
"name": "上下文是预算",
"summary": "只注入与本任务相关的文件与最近变更;大仓库用分层索引,避免一次性灌入冗余历史。",
"applies_to": ["大代码库协作", "降低 token 成本与跑偏率"],
"levels": ["L0 索引: rules/README.md + 本文件 id 列表", "L1 规范: ai_collaboration.md / ai_agent_rules.md", "L2 项目知识: knowledge_graph.jsonl", "L3 细节: 具体源码文件"]
},
{
"id": "principle:single-source-of-truth",
"type": "principle",
"name": "单一事实源",
"summary": "同一份知识只允许一个权威载体,其他位置派生或引用;否则必然漂移。",
"applies_to": ["规则与知识库治理"],
"antipattern_ref": "antipattern:divergent-copies"
},
{
"id": "principle:observability-first",
"type": "principle",
"name": "可观测优先",
"summary": "AI 生成的代码默认只优化「能跑」而非「可排查」。必须显式要求日志/指标/错误上下文,否则凌晨排障无窗口。",
"applies_to": ["服务端代码", "定时任务脚本"],
"project_binding": "scripts/lib/logger.mjs + TaskLog 表"
},
{
"id": "method:task-decomposition",
"type": "method",
"name": "任务拆解",
"summary": "把大任务拆成 Epic→Story→Task→Step,每步有明确输入/输出契约,避免黑盒式长推理。",
"how": [
"窄范围 > 大而模糊:『修 /api/tools 分页 total 字段』优于『改进工具模块』",
"每步定义输入(依赖文件/数据)与输出(验收标准)",
"状态机驱动:Pending → Running → Blocked → Done / Failed"
],
"evidence": "Agent 在大而模糊的任务上跑偏率显著更高;窄范围单目标任务成功率最高",
"project_binding": "TodoWrite 工具 + 本文件 workflow:plan-first"
},
{
"id": "method:context-engineering",
"type": "method",
"name": "上下文工程",
"summary": "为 AI 提供强结构化的项目上下文(架构/规范/示例/反例),防止它用通用默认猜测。",
"how": [
"持久上下文写入 rules 文件,而非每次对话重复交代",
"文档化架构决策、命名约定、错误处理规则、禁止事项",
"措辞精确可验证,给出正例与反例",
"项目专属事实写进知识图谱,方法层写进本文件"
],
"evidence": "缺乏上下文时 AI 易混用 API、忽略错误处理、用过时库",
"project_binding": [
".trae/rules/project_rules.md(工程标准)",
".trae/rules/ai_collaboration.md(协作流程)",
".trae/ai_coding_knowledge.json(本文件,方法层)",
".trae/knowledge_graph.jsonl(事实层)"
]
},
{
"id": "method:acceptance-criteria",
"type": "method",
"name": "验收标准",
"summary": "每个任务开始前必须定义可验证的验收标准,否则 AI 无法可靠收敛。",
"how": [
"例:『npx tsc --noEmit 零错误 + npx vitest run 全绿 + 无 N+1』",
"verification 与 implementation 一样是交付物的一部分"
],
"applies_to": ["所有 AI 代理任务"],
"antipattern_ref": "antipattern:no-acceptance"
},
{
"id": "method:plan-first",
"type": "method",
"name": "Plan-first 工作流",
"summary": "先规划后执行:Explore → Plan → Implement → Verify 四阶段,提升一致性与可预测性。",
"how": [
"Explore:先读代码/规格/知识图谱,理解现状",
"Plan:输出结构化计划(步骤 + 依赖 + 验收标准),复杂任务先给计划再动手",
"Implement:按计划分步实现,一步一验证",
"Verify:跑门禁;失败回到 Implement"
],
"applies_to": ["复杂功能开发", "重构", "跨模块改动"],
"workflow_ref": "workflow:plan-first-loop"
},
{
"id": "method:review-diff",
"type": "method",
"name": "看 diff 不看对话",
"summary": "审查 AI 产出必须看实际 git diff,而不是它自己的解释。",
"how": ["用 git diff / IDE diff 视图逐行审查", "AI 的自我描述可信度低于实际改动", "对照验收标准核验,而非对照 AI 的说法"],
"evidence": "AI 可能误报自己做了什么;diff 才是真相",
"antipattern_ref": "antipattern:skip-review"
},
{
"id": "method:model-tiering",
"type": "method",
"name": "模型成本分级",
"summary": "高难步骤用强模型,常规步骤用轻量模型,控制时延与成本。",
"how": [
"补全/格式/单文件改动 → 轻量模型",
"架构决策/疑难根因/跨模块重构 → 强模型",
"任务开始先用轻量,按需升级"
],
"evidence": "分级可省 40-70% 成本而不明显降低质量"
},
{
"id": "method:logging-standard",
"type": "method",
"name": "日志标准(最高 ROI 规则)",
"summary": "强制 AI 产出可观测日志,因为 LLM 天然少打日志。",
"how": ["规则层强制日志格式,禁止 print('done') 式输出", "记录关键状态、失败原因、输入输出摘要", "脚本统一走 scripts/lib/logger.mjs,结果写 TaskLog"],
"applies_to": ["服务端代码", "定时任务脚本"],
"project_binding": "workspace 规则 §8.7 日志规范"
},
{
"id": "method:failure-classification",
"type": "method",
"name": "失败分型",
"summary": "把 AI 失败分为规划失败/执行失败/质量失败,分型后再优化,避免盲目调 prompt。",
"how": [
"规划失败 → 重新拆解任务",
"执行失败 → 修工具/环境/依赖",
"质量失败 → 收紧验收标准或门禁"
],
"applies_to": ["AI 任务复盘"],
"evidence": "分型后针对优化,比反复调 prompt 有效得多"
},
{
"id": "method:spec-driven",
"type": "method",
"name": "规格驱动开发",
"summary": "先写规格(输入/输出/边界/验收),再让 AI 实现;规格是 AI 与人的共同契约。",
"how": ["复杂需求先落到 .trae/specs/ 或设计文档", "规格含验收标准与不变量", "实现阶段只对照规格,不临场改需求"],
"applies_to": ["新模块", "接口变更", "结构重构"]
},
{
"id": "method:test-first-gate",
"type": "method",
"name": "测试先行 + 门禁",
"summary": "先写失败测试再实现,让 AI 有确定的收敛目标;门禁命令必须可一键复现。",
"how": ["先补一条会失败的用例", "实现到用例转绿", "提交前跑 tsc + vitest 双门禁"],
"project_binding": "Vitest;门禁:npx tsc --noEmit && npx vitest run"
},
{
"id": "method:incremental-refactor",
"type": "method",
"name": "小步重构",
"summary": "行为不变前提下小步改动,每步可回滚,禁止一次性大重构。",
"how": ["一次只改一个关注点", "每步跑门禁保持全绿", "大重构拆成可独立验证的若干步"]
},
{
"id": "workflow:plan-first-loop",
"type": "workflow",
"name": "Plan-first 主循环",
"summary": "本项目标准工作流:加载上下文 → 探索 → 规划 → 分步实现 → 验证 → 自动记录。",
"stages": ["load_context", "explore", "plan", "implement", "verify", "auto_record"],
"how": [
"load_context:读 rules/README.md → 本文件 → knowledge_graph.jsonl(按需)",
"verify:npx tsc --noEmit + npx vitest run,失败回 implement",
"auto_record:node scripts/update-knowledge.mjs(见 workflow:auto-record)"
],
"evidence": "可复用的 plan-first 流程让产出可预测、可验证"
},
{
"id": "workflow:edit-test-loop",
"type": "workflow",
"name": "Edit-Test Loop(编辑-测试循环)",
"summary": "让 AI 自主形成『写→测→读报错→修→重跑』闭环,是最核心的提效模式。",
"stages": ["写代码", "跑测试", "读报错", "修复", "重跑直到通过"],
"how": [
"给一条完整指令:实现 + 补测试 + 测试通过再报告",
"允许 AI 自动运行安全测试命令",
"通常 2-5 轮收敛到全绿"
],
"evidence": "让 AI 自闭环比人工逐步确认 diff 效率高数倍",
"tool_fit": ["tool:trae", "tool:claude-code"]
},
{
"id": "workflow:subagent-fanout",
"type": "workflow",
"name": "子代理并行扇出",
"summary": "把互相独立的任务拆成多个子代理并行执行,主代理只做编排与合并,保护主上下文。",
"stages": ["识别独立任务", "并行派发子代理", "汇总结果", "主代理复核与合并"],
"how": [
"仅对无共享状态的独立任务并行(如同时核查 3 个仓库/3 个目录)",
"探索型任务交给搜索代理,避免污染主上下文",
"子代理返回结论而非原始日志"
],
"evidence": "并行 + 上下文隔离,是大仓库提效的关键手段"
},
{
"id": "workflow:auto-record",
"type": "workflow",
"name": "开发完成自动记录",
"summary": "每次开发完成后自动捕获三类信息并沉淀,形成持续更新的知识库。",
"stages": ["scan_capabilities", "collect_dev_process", "capture_artifacts", "upsert_knowledge_graph"],
"how": [
"命令:node scripts/update-knowledge.mjs(可加 --dry-run 预览)",
"能力特征 → .trae/knowledge/capabilities.json(API/页面/组件/模型/脚本/任务/技术栈快照)",
"开发过程 → .trae/knowledge/dev_process.jsonl(按 commit 追加)",
"模型成果 → .trae/knowledge/artifacts.jsonl(迁移/脚本/提交统计等交付物)",
"写回 .trae/knowledge_graph.json(权威)+ .trae/knowledge_graph.jsonl(MCP 读取)"
],
"trigger": ["git post-commit hook(本机自动)", "AI 任务收尾手动执行", "npm run knowledge:update"],
"evidence": "不落盘的迭代 = 每次重新开始;落盘后 AI 可直接检索历史决策与踩坑",
"project_binding": "scripts/update-knowledge.mjs"
},
{
"id": "workflow:persistent-context-load",
"type": "workflow",
"name": "持久上下文加载顺序",
"summary": "AI 每次任务开始按固定顺序加载上下文,避免重复交代与遗漏。",
"stages": ["rules/README.md(索引)", "ai_agent_rules.md(启动规则)", "ai_coding_knowledge.json(方法层)", "knowledge_graph.jsonl(事实层,按需)", "具体源码"],
"how": ["先索引后详情,按 context-budget 原则只加载相关部分"],
"evidence": "固定加载顺序让 AI 行为可预测,减少『忘记项目约定』类返工"
},
{
"id": "tool:trae",
"type": "tool",
"category": "ai_ide",
"name": "Trae",
"summary": "本项目实际使用的 AI IDE,规则体系(.trae/rules)+ MCP + 技能 + 子代理。",
"observations": [
".trae/rules/*.md 规则体系(工作区级 + 项目级)",
".trae/mcp.json 注册 MCP 服务(含 Knowledge Graph Memory)",
"内置子代理(搜索/前端/后端/测试等)与 TodoWrite 任务管理",
"支持的 Skills 可封装可复用流程"
],
"use_cases": ["本项目日常开发", "规则驱动的 AI 协作", "知识库维护"],
"strengths": ["规则+知识图谱+子代理一体", "中文语境好"],
"weaknesses": ["生态较新", "部分能力依赖版本"]
},
{
"id": "tool:claude-code",
"type": "tool",
"category": "terminal_agent",
"name": "Claude Code",
"summary": "终端 Agent-first 工具,大上下文,可读代码库、改文件、跑命令、自修复,端到端完成任务。",
"observations": ["MCP 原生支持", ".claude/skills 跨会话复用约定", "hooks 做确定性约束", "自动读报错→定位→修复→重跑测试闭环"],
"use_cases": ["大型重构", "跨文件修改", "疑难 Bug 根因定位", "代码审查"],
"strengths": ["自主性最高", "终端串联顺手", "MCP 生态完整"],
"weaknesses": ["无 IDE 补全体验", "长任务易跑偏需纠偏"]
},
{
"id": "tool:cursor",
"type": "tool",
"category": "ai_ide",
"name": "Cursor",
"summary": "AI 原生 IDE,Tab 补全 + 内联编辑 + 多文件 Agent,零学习曲线。",
"observations": ["Tab 补全业界领先", ".cursor/rules/*.mdc 按 glob 自动激活规则", "多模型自由切换", "2025 年底起原生读 AGENTS.md"],
"use_cases": ["日常编码", "小粒度修改", "保持心流"],
"strengths": ["补全最顺滑", "可视化 Diff", "有免费层"],
"weaknesses": ["超大重构易跑偏", "CI/脚本集成弱"]
},
{
"id": "tool:codex",
"type": "tool",
"category": "cloud_agent",
"name": "OpenAI Codex",
"summary": "云端异步编码 Agent,沙箱执行,超大上下文,适合批量异步任务。",
"observations": ["异步任务模式:提交后云端执行,完成后通知", "AGENTS.md 标准发起者", "可并行批量处理", "支持只读/全自动权限分级"],
"use_cases": ["批量修改", "自动提 PR", "异步后台任务", "CI/CD 集成"],
"strengths": ["异步并行", "沙箱安全", "上下文最大"],
"weaknesses": ["非实时", "沙箱无本地文件直访"]
},
{
"id": "tool:copilot",
"type": "tool",
"category": "ide_extension",
"name": "GitHub Copilot",
"summary": "IDE 插件,补全 + agent 模式,通过 copilot-instructions.md 注入项目上下文。",
"observations": ["补全速度快", "GitHub 深度集成", "agent 模式支持多文件编辑与建 PR"],
"use_cases": ["日常补全", "快速原型", "GitHub 工作流"],
"strengths": ["补全快", "集成深"],
"weaknesses": ["自主性弱于终端 Agent"]
},
{
"id": "tool:mcp",
"type": "tool",
"category": "protocol",
"name": "MCP(Model Context Protocol)",
"summary": "AI 与外部工具/数据源的标准接口层,让代理具备确定性能力(读库、查图、调 API)。",
"observations": [
"本项目 .trae/mcp.json 注册服务",
"Knowledge Graph Memory:结构化长期记忆(读写 .trae/knowledge_graph.jsonl)",
"可接数据库/仓库/浏览器等外部能力",
"工具描述文件需先读 schema 再调用"
],
"use_cases": ["长期记忆", "确定性数据访问", "跨会话知识复用"],
"strengths": ["标准化", "可组合", "确定性优于纯 prompt"],
"weaknesses": ["需按 schema 调用", "服务未挂载则退化为文件维护"]
},
{
"id": "tool:agents-md",
"type": "tool",
"category": "standard",
"name": "AGENTS.md 开放标准",
"summary": "跨工具的项目上下文单一事实源,symlink 到各工具专属文件,解决配置碎片化。",
"observations": ["ln -s AGENTS.md CLAUDE.md / .cursorrules", "已被数万开源项目采用", "Cursor/Codex 原生支持"],
"use_cases": ["多工具并存的项目", "上下文统一治理"],
"strengths": ["一次编写多工具复用"],
"weaknesses": ["各工具支持度仍有差异"]
},
{
"id": "anti_pattern:one-shot-big",
"type": "anti_pattern",
"name": "一次性生成大量代码",
"why_bad": "Agent 在大任务里易跑偏,产出难审查、难验证",
"fix": "任务拆解,一次只给一个窄范围目标,分步实现 + 验证",
"detect": "单个任务改动文件数 > 15 或 diff > 800 行时需重新拆解"
},
{
"id": "anti_pattern:no-acceptance",
"type": "anti_pattern",
"name": "没有验收标准",
"why_bad": "AI 无法判断任务是否完成,会无限发散或过早收工",
"fix": "每个任务先定义可验证标准(tsc 零错误 / vitest 全绿 / 性能不回归)",
"detect": "任务开始前说不出『怎么算完成』即触发"
},
{
"id": "anti_pattern:skip-review",
"type": "anti_pattern",
"name": "不审查 AI 生成代码",
"why_bad": "可能含 bug、安全风险、过时实践、臆造 API",
"fix": "看 diff 逐行审查 + 类型检查 + 测试三重门禁",
"detect": "直接 git commit 且未跑门禁即触发"
},
{
"id": "anti_pattern:vague-task",
"type": "anti_pattern",
"name": "模糊的大任务",
"why_bad": "『优化一下社区模块』这类任务 Agent 必然迷失",
"fix": "改成窄范围明确目标,并给出验收标准",
"detect": "任务描述无具体文件/接口/行为即触发"
},
{
"id": "anti_pattern:context-overload",
"type": "anti_pattern",
"name": "一次性灌入冗余历史",
"why_bad": "上下文超载导致抓不住重点、成本飙升、准确率下降",
"fix": "只注入必要文件与最近变更,分层索引按需加载",
"detect": "单次注入 > 10 个无关文件即触发"
},
{
"id": "anti_pattern:model-is-everything",
"type": "anti_pattern",
"name": "把模型更强当系统更稳",
"why_bad": "没有编排、门禁与观测,强模型也会产生不可控波动",
"fix": "模型层 + 编排层 + 门禁/观测层三层闭环,而非只换更强模型",
"detect": "复盘只归因于『换个模型就好』且无门禁改进即触发"
},
{
"id": "anti_pattern:divergent-copies",
"type": "anti_pattern",
"name": "同一知识多份副本各自漂移",
"why_bad": "规则/图谱/文档多份并存且内容冲突,AI 加载到哪份全靠运气",
"fix": "确立单一事实源(其余派生)+ 增量化同步脚本 + 定期一致性巡检",
"detect": "同一事实在两处以上出现且可被独立编辑即触发",
"project_evidence": "2026-10-01 巡检发现:两份 project_rules.md 并存、根 modules.md 与实际规格文件不一致"
},
{
"id": "anti_pattern:memory-rot",
"type": "anti_pattern",
"name": "知识库只写不更新",
"why_bad": "知识图谱/规则过期后,AI 会依据错误事实决策,比没有知识更危险",
"fix": "开发完成强制运行 auto_record;关键节点做『图谱 vs 代码』一致性巡检",
"detect": "knowledge_graph.updated_at 落后于最近 commit 超过 7 天即触发",
"project_binding": "workflow:auto-record + node scripts/update-knowledge.mjs"
}
],
"relations": [
{"from": "method:context-engineering", "to": "principle:stateless-agent", "type": "SOLVES"},
{"from": "method:context-engineering", "to": "anti_pattern:context-overload", "type": "AVOIDS"},
{"from": "method:task-decomposition", "to": "anti_pattern:one-shot-big", "type": "AVOIDS"},
{"from": "method:task-decomposition", "to": "anti_pattern:vague-task", "type": "AVOIDS"},
{"from": "method:acceptance-criteria", "to": "anti_pattern:no-acceptance", "type": "AVOIDS"},
{"from": "method:review-diff", "to": "anti_pattern:skip-review", "type": "AVOIDS"},
{"from": "method:failure-classification", "to": "anti_pattern:model-is-everything", "type": "AVOIDS"},
{"from": "principle:single-source-of-truth", "to": "anti_pattern:divergent-copies", "type": "AVOIDS"},
{"from": "workflow:auto-record", "to": "anti_pattern:memory-rot", "type": "AVOIDS"},
{"from": "workflow:auto-record", "to": "workflow:persistent-context-load", "type": "FEEDS"},
{"from": "workflow:plan-first-loop", "to": "method:task-decomposition", "type": "USES"},
{"from": "workflow:plan-first-loop", "to": "method:plan-first", "type": "IMPLEMENTS"},
{"from": "workflow:plan-first-loop", "to": "workflow:edit-test-loop", "type": "CONTAINS"},
{"from": "workflow:plan-first-loop", "to": "workflow:auto-record", "type": "ENDS_WITH"},
{"from": "workflow:edit-test-loop", "to": "method:test-first-gate", "type": "USES"},
{"from": "method:test-first-gate", "to": "principle:enforcement-pyramid", "type": "IMPLEMENTS"},
{"from": "method:logging-standard", "to": "principle:observability-first", "type": "IMPLEMENTS"},
{"from": "method:spec-driven", "to": "method:acceptance-criteria", "type": "REQUIRES"},
{"from": "method:plan-first", "to": "method:spec-driven", "type": "RELATES_TO"},
{"from": "method:incremental-refactor", "to": "anti_pattern:one-shot-big", "type": "AVOIDS"},
{"from": "workflow:subagent-fanout", "to": "principle:context-budget", "type": "IMPLEMENTS"},
{"from": "tool:trae", "to": "workflow:plan-first-loop", "type": "BEST_AT"},
{"from": "tool:claude-code", "to": "workflow:edit-test-loop", "type": "BEST_AT"},
{"from": "tool:cursor", "to": "method:task-decomposition", "type": "NEEDS"},
{"from": "tool:codex", "to": "tool:agents-md", "type": "ORIGINATES"},
{"from": "tool:trae", "to": "tool:mcp", "type": "SUPPORTS"},
{"from": "tool:trae", "to": "workflow:auto-record", "type": "HOSTS"},
{"from": "tool:mcp", "to": "workflow:auto-record", "type": "STORES"}
]
}