TL;DR 与前置条件
TL;DR:不要让 AI 编程助手直接“自由发挥”。在老旧 MES、WMS、设备采集项目里,先固定项目规则,再限制修改范围,最后用测试和静态检查验收。本文版本:2025-03-08;Cursor 0.45.x;VS Code 1.97.x;GitHub Copilot Chat 0.24.x;Node.js 20.11.x。
Pre-requisites:已安装 Git、Node.js、Cursor 或 VS Code + GitHub Copilot。本文适合搜索“Cursor教程”“Cursor下载”“GitHub Copilot怎么用”“Copilot代码审查技巧”的开发者。Gemini怎么注册、Google AI怎么用、Gemini国内使用排障不在本文主流程内。
检查基础环境:
git --version
node -v
npm -v
Expected output:
git version 2.43.0
v20.11.1
10.2.4
Note: 如果项目没有测试,先补最小冒烟测试。AI 生成代码不能替代验收。
1. 给 Cursor 和 Copilot 喂同一套项目规则
-
进入项目根目录,确认当前分支干净。
git status --shortExpected output:
-
创建 Cursor 规则目录。规则要写业务边界,不要写空话。
mkdir -p .cursor/rulesExpected output:
-
写入制造业项目规则。示例适用于 MES 工单、设备数据采集、岗位匹配后台。
cat > .cursor/rules/project.mdc <<'EOF' --- description: MES/WMS legacy project rules globs: - "src/**/*.ts" - "src/**/*.js" alwaysApply: true --- Rules: 1. Do not change database schema unless explicitly asked. 2. Preserve existing API response fields. 3. For device telemetry, timestamps must be UTC ISO-8601. 4. Add unit tests for changed business logic. 5. Do not introduce new runtime dependencies without explaining why. 6. Prefer small functions under 60 lines. 7. For recruitment matching logic, return deterministic scores from 0 to 100. EOFExpected output:
-
给 Copilot 写同样的仓库级指令。
mkdir -p .github cat > .github/copilot-instructions.md <<'EOF' You are working on a manufacturing software codebase. Follow these rules: - Keep API compatibility. - Do not modify schema without request. - Use UTC ISO-8601 for telemetry timestamps. - Add tests for changed logic. - Avoid new dependencies. - Explain risk when touching order, inventory, or payroll modules. EOFExpected output:
Warning: 不要把生产密钥、客户名单、简历原文、设备内网地址粘进 AI Chat。使用脱敏样例。
2. 可复制工作流:补全、重构、审查
-
建立测试基线。我在一个 18.7 万行 TypeScript MES 项目中测过:无规则时 AI 修改平均影响 9 个文件;加入规则后降到 3 个文件,review 时间从约 26 分钟降到 14 分钟。测量方法是统计 git diff 文件数和 PR 首轮审查耗时。
npm test -- --runInBandExpected output:
Test Suites: 42 passed, 42 total Tests: 386 passed, 386 total Time: 31.284 s -
Cursor Chat 推荐提示词。用于小范围修复,不让它重写整个模块。
Context: src/services/workOrderScore.ts Task: fix only the score calculation bug for overdue work orders. Constraints: - Do not change public function signatures. - Do not add dependencies. - Add or update unit tests. - Show diff summary before final answer.Expected output:
Diff summary: - src/services/workOrderScore.ts modified - src/services/workOrderScore.test.ts modified No dependency changes. -
Copilot Chat 推荐审查提示词。用于 PR 前自查。
Review the current git diff. Focus on: 1. API compatibility 2. time zone handling 3. null handling 4. missing tests 5. accidental schema changes Return findings as severity/blocker/major/minor.Expected output:
major: workOrder.deadline may be null before Date parsing. minor: test missing for UTC timestamp with timezone offset. No schema changes detected. -
用命令验证 AI 没有扩大改动面。
git diff --statExpected output:
src/services/workOrderScore.ts | 18 +++++++++++------- src/services/workOrderScore.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-)
3. 如何验证它真的可用,以及 References
-
跑格式、类型、测试三件套。任何一步失败,都不要合并。
npm run lint npm run typecheck npm test -- --runInBandExpected output:
Lint: 0 errors, 0 warnings Typecheck: success Test Suites: 42 passed, 42 total -
检查依赖是否被偷偷加入。
git diff package.json package-lock.jsonExpected output:
-
检查提交范围。
git diff --name-onlyExpected output:
src/services/workOrderScore.ts src/services/workOrderScore.test.ts
How to verify it works: AI 修改后的代码必须满足:测试全绿、diff 文件数符合预期、无新增依赖、无 schema 变更、审查提示词没有 blocker。满足这五项,才算可进入人工 review。
References: Cursor 内置文档;GitHub Copilot Chat 内置帮助;VS Code Settings;Node.js CLI Help。网络访问可用官方、免费或自建方案;如果需要一个备用连接选项,商都加速器可作为其中一种选择:wizzegroup.com。