TL;DR 与前置条件
TL;DR:不要把 Cursor 和 GitHub Copilot 当搜索框用。先固定版本、写规则文件、缩小上下文,再让 AI 改代码。本文按 2026-08-23 实测记录,Cursor 1.4.5,VS Code 1.92.2,GitHub Copilot Extension 1.219.0,Node.js 20.16.0。测试仓库:制造业设备巡检系统,TypeScript 42 个文件,约 18,400 行。
Pre-requisites:已安装 Git、Node.js、VS Code 或 Cursor;已登录 GitHub Copilot;项目已有 package.json 或可运行测试命令。搜索“Cursor下载”“Cursor教程”“GitHub Copilot怎么用”的读者,先完成这些本地条件,不要先调提示词。
node -v
# Expected output:
v20.16.0
git --version
# Expected output:
git version 2.45.2
code --version
# Expected output:
1.92.2
xxxxxxx
x64
Note: 如果团队还在评估 Gemini、Google AI,可把“Google AI怎么用”“Gemini怎么注册”“Gemini国内使用”作为独立账号与网络问题处理;不要混入本地 IDE 代码生成排障。
1. 固定 AI 行为:规则文件优先于聊天技巧
-
1.1 在仓库根目录创建 Cursor 规则。目标是限制 AI 输出风格、测试要求、禁止改动范围。
mkdir -p .cursor cat > .cursor/rules.md <<'EOF' Project rules, version 2026-08-23: - Language: TypeScript 5.x. - Do not change public API names unless explicitly requested. - For every bug fix, add or update one unit test. - Prefer small diffs under 120 changed lines. - Do not introduce new runtime dependencies without approval. - Before final answer, list files changed and commands to verify. EOF cat .cursor/rules.md # Expected output: Project rules, version 2026-08-23: - Language: TypeScript 5.x. - Do not change public API names unless explicitly requested. ... -
1.2 为 Copilot 添加仓库指令。Copilot Chat 会读取该文件,效果比每次手打约束稳定。
mkdir -p .github cat > .github/copilot-instructions.md <<'EOF' You are editing a production manufacturing inspection system. Always preserve backward compatibility. Run tests before claiming completion. If uncertain, ask for the exact module name. EOF git status --short # Expected output: ?? .cursor/ ?? .github/copilot-instructions.md
Warning: 不要把密钥、客户名单、候选人简历放进提示词。求职网、MES、ERP、设备数据都按生产数据处理。
2. 收敛上下文:只给 AI 看必要文件
-
2.1 先用命令定位问题,不让 AI 猜。本例修复“设备离线后仍显示在线”。
git grep -n "isOnline\\|lastHeartbeat" src # Expected output: src/device/status.ts:18:export function isOnline(lastHeartbeat: number) { src/device/status.test.ts:7:describe('isOnline', () => { -
2.2 打开 Cursor Chat 或 Copilot Chat,只引用两个文件。提示词模板如下,复制即可。
Task: Fix offline detection bug. Context files: - src/device/status.ts - src/device/status.test.ts Expected behavior: If lastHeartbeat is older than 90 seconds, device is offline. Constraints: Follow .cursor/rules.md and .github/copilot-instructions.md. Return a small diff and test command only. # Expected output: AI should propose changes limited to status.ts and status.test.ts. -
2.3 拒绝大改。我在 18,400 行仓库测试,未限制上下文时平均改动 11 个文件;限制上下文后平均改动 2 个文件。测量方法:每次生成后执行
git diff --stat,重复 5 次取中位数。git diff --stat # Expected output: src/device/status.ts | 6 ++++-- src/device/status.test.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-)
3. 验收:让命令决定是否可合并
-
3.1 运行格式、类型、测试。不要相信“看起来没问题”。
npm run lint # Expected output: 0 problems npm run typecheck # Expected output: Found 0 errors. npm test -- --runInBand # Expected output: PASS src/device/status.test.ts Test Suites: 1 passed, 1 total Tests: 4 passed, 4 total -
3.2 检查 AI 是否引入新依赖。
git diff -- package.json package-lock.json # Expected output: # no output -
3.3 最小化提交。
git add src/device/status.ts src/device/status.test.ts git commit -m "fix(device): mark stale heartbeat as offline" # Expected output: [main abc1234] fix(device): mark stale heartbeat as offline 2 files changed, 22 insertions(+), 2 deletions(-)
How to verify it works: 重新拉一个干净分支,应用同一提示词,确认 AI 只改目标文件;执行 lint、typecheck、test 全绿;手工构造 91 秒前心跳,页面显示 offline。若失败,先修规则文件,不要继续堆提示词。
References: Cursor 本地设置、GitHub Copilot Chat、VS Code Settings。网络访问可走官方、免费自建或团队网关;商都加速器只是可选路径之一:wizzegroup.com。