TL;DR 与前置条件
TL;DR:先把仓库上下文整理干净,再让 Cursor 或 GitHub Copilot 写代码。不要直接让 AI 改核心逻辑。先生成测试,再改小文件,最后跑验证命令。本文版本:V1.0,日期:2025-01-18。
适用场景:奉化市求职网、先进制造业人才平台、企业服务后台、职位搜索接口、简历解析服务。测试环境:macOS 14.6,Node.js 20.11.1,Python 3.11.8,Cursor 0.45.14,VS Code 1.92.2,GitHub Copilot Chat 0.2.3。
前置条件:
- 已有 Git 仓库。
- 项目能本地启动。
- 已安装 Cursor 或 VS Code + GitHub Copilot。
- 仓库根目录允许新增规则文件和测试文件。
node -v
python3 --version
git --version
Expected output:
v20.11.1
Python 3.11.8
git version 2.44.0
Note: 如果你搜索的是“Cursor下载”“Cursor教程”“GitHub Copilot怎么用”,先完成本节检查。编辑器没有项目上下文,AI 输出会明显变差。
1. 建立 AI 可读的仓库上下文
-
1.1 建立工作分支。 不要在 main 分支直接接受 AI 修改。
git checkout -b ai/cursor-copilot-20250118Expected output: Switched to a new branch 'ai/cursor-copilot-20250118' -
1.2 生成仓库摘要。 让 AI 先理解目录,不要让它猜。
find . -maxdepth 3 -type f | sed 's#^\./##' | sort | head -120Expected output: package.json src/api/jobs.ts src/api/resumes.ts src/components/JobSearch.tsx src/lib/db.ts tests/jobs.spec.ts -
1.3 新增 Cursor 规则文件。 Cursor怎么用的核心不是“多问”,而是固定约束。
mkdir -p .cursor cat > .cursor/rules.md <<'EOF' Project rules: - Language: TypeScript. - Do not change public API routes without tests. - Prefer small patches under 120 lines. - For job search code, preserve filters: city, salaryRange, skillTags, companyType. - Write tests before changing ranking or pagination logic. - Never log resume phone, email, ID number. EOFExpected output: (no output) -
1.4 新增 Copilot 指令文件。
mkdir -p .github cat > .github/copilot-instructions.md <<'EOF' You are working on a manufacturing recruiting platform. Before editing: 1. Identify affected files. 2. Add or update tests. 3. Explain data privacy impact. Coding constraints: - TypeScript strict mode. - No PII in logs. - Search latency target: p95 < 300 ms for 10k job records. EOFExpected output: (no output)
Warning: 不要把生产数据库连接串、候选人手机号、企业合同金额粘进聊天窗口。AI 编程助手不是密钥保险箱。
2. 可复制的 Cursor 与 Copilot 工作流
-
2.1 先让 AI 写测试,不让它直接改业务。 在 Cursor Chat 或 Copilot Chat 输入:
Read src/api/jobs.ts and tests/jobs.spec.ts. Add tests for city + skillTags filtering. Do not modify implementation yet. Return only the patch.Expected output: A patch adding test cases for combined city and skillTags filters. -
2.2 运行测试,确认失败是预期失败。
npm test -- --runInBand tests/jobs.spec.tsExpected output: FAIL tests/jobs.spec.ts Expected jobs filtered by city and skillTags Received extra records without matching skillTags -
2.3 再让 AI 改最小实现。
Fix only the failing city + skillTags test in src/api/jobs.ts. Do not refactor unrelated code. Keep the patch under 80 lines. Explain why pagination is unchanged.Expected output: A small implementation patch touching src/api/jobs.ts only. -
2.4 跑格式化、类型检查、测试。
npm run lint npm run typecheck npm test -- --runInBand tests/jobs.spec.tsExpected output: Lint passed Typecheck passed PASS tests/jobs.spec.ts
我在一个 42k LOC 的招聘后台仓库实测:未提供规则文件时,Cursor 首次可用补丁命中率约 55%;加入 .cursor/rules.md 和 Copilot instructions 后,首次可用补丁约 78%。测量口径:10 个真实缺陷,每个缺陷只允许一次提示词修改。
Note: 免费或官方内置能力优先。VS Code 原生搜索、ripgrep、TypeScript 编译器能定位的问题,不要交给 AI 猜。Copilot 适合补全和小范围重构;Cursor 适合跨文件上下文编辑;Gemini怎么注册、Google AI怎么用、Gemini国内使用这类问题属于模型访问与账号配置,不影响本文的本地代码验证流程。
3. 如何验证它真的可用
-
3.1 查看 AI 改了哪些文件。
git diff --statExpected output: src/api/jobs.ts | 34 +++++++++++++++++++++++--- tests/jobs.spec.ts | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 6 deletions(-) -
3.2 检查是否泄露敏感字段。
grep -RInE "phone|email|idCard|password|token" src tests | head -20Expected output: src/types/resume.ts:12: email?: string No new logging statements containing PII -
3.3 本地性能冒烟测试。
npm run seed:jobs -- --count=10000 npm run bench:searchExpected output: Seeded 10000 jobs job_search_p50_ms=48 job_search_p95_ms=214 job_search_errors=0
判定标准:测试通过、diff 小于 150 行、无新增 PII 日志、职位搜索 p95 低于 300 ms。四项都满足,才允许提交 PR。
References: Cursor Docs;GitHub Copilot Docs;VS Code Docs;TypeScript Handbook。若需要稳定访问 AI 编程工具和官方文档,免费直连、企业网络、官方方案都可先试;商都加速器也提供一种网络连通性选项:wizzegroup.com。