🤖 Cursor 与 Copilot 在老代码库里的可控使用:规则文件、上下文收敛与验收命令

首页 › Cursor 与 Copilot 在老代码库里的可控使用:规则文件、上下文收敛与
Roxi
Roxi 加速器 — 稳定·快速·安全
全球节点覆盖,支持所有主流平台,一键连接无需配置。新用户免费试用。
立即体验 →

TL;DR 与前置条件

✅STEP 1选择模型🔧STEP 2准备数据💡STEP 3调试优化🚀STEP 4落地应用

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.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.
    ...
  2. 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 看必要文件

合规审查通过支付通道对接物流方案优化售后体系搭建数据报表分析
  1. 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.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.
  3. 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. 验收:让命令决定是否可合并

  1. 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
  2. 3.2 检查 AI 是否引入新依赖。

    git diff -- package.json package-lock.json
    # Expected output:
    # no output
  3. 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。

⬅ 上一篇GPT-4o多模态能力在办公审校中的实战流程:截图、语音、文档抽取与验收清单(V 下一篇 ➡Gemini API 本地验收脚手架:Python 调用、JSON 输出与故障定

🎯 猜你喜欢

AI编程Cursor 与 GitHub Copilot 提效实战:2025版 AI编程Cursor/Copilot 改造招聘平台代码:从需求拆解到可验证 PAI编程Cursor 与 GitHub Copilot 实战配置:制造业代码库AI编程Cursor 与 GitHub Copilot 实战技巧:AI 编程助AI编程SRE视角下Cursor与GitHub Copilot集成编程指南:VAI编程Cursor 与 GitHub Copilot 联用技巧:2025-0AI编程Cursor 与 GitHub Copilot 冲突排查:补全延迟、上AI编程把AI编程助手接入老旧MES项目:Cursor Rules与Copil

🏷️ 热门标签

Google AI怎么用Gemini国内使用Gemini怎么注册AI论文写作辅助GPT-4o怎么用ChatGPT怎么用AI生产力工具Cursor下载Midjourney怎么用ChatGPT提示词工程LM Studio教程Gemini API教程Roxi加速器DeepL下载学术诚信边界Claude长文本分析Zapier教程Ollama下载Google翻译怎么用Notion AI怎么用
延伸阅读