最新资讯
dev.to 银行 77 意图分类最便宜 LLM:GPT-6.1 Terra 每次成功任务 $0.000727 theinformation.com Chesky的副业:Airbnb向CEO新设AI实验室出售IP theverge.com OpenAI 宣布数据中心项目不再与公职人员签署保密协议 dev.to NVIDIA 发布四个开放 Cosmos 3 仿真模型 dev.to 真实 MCP 客户端击穿我们服务器的五种方式 theverge.com Airbnb 正在开发 AI 旅行 agent,计划 2027 年上半年推出 wechat 这可能是美国AI最魔幻的一天。 nytimes.com 美众议院共和党领袖要求 Nvidia 黄仁勋闭门简报 pub.towardsai.net 忘掉我:为记忆而设计的系统中的删除难题 marktechpost.com Google 发布 Gemini Omni 1.2 Flash:15 秒视频,每秒 0.07 美元 unite.ai Airbnb 计划推出覆盖规划、预订与客服的 AI 旅行 agent techcrunch.com Airbnb 称将于 2027 年上半年推出 AI 旅行代理
多元动态
登录 注册
AI编码 dev.to · @Piekwerk

让 Cursor 规则真正生效:激活模式详解

原文 我的收藏

文章解释 Cursor 规则为何有时不生效,并拆解 .cursor/rules/ 下 .mdc 文件的 YAML frontmatter 三个字段:description、globs、alwaysApply,以及它们如何对应 Auto Agent、Agent Requested 等四种激活模式,帮助开发者写出真正会被加载的规则。

AI推荐理由 具体讲解 Cursor 规则文件 .mdc 的 frontmatter 字段(description、globs、alwaysApply)与四种激活模式,对使用 Cursor 的开发者有直接可操作的参考价值,内容技术性强、信息密度高,但属工具使用教程,非突破性信息。

62

正文

A Cursor rule that never fires is indistinguishable from a rule you never wrote. There is no warning, no log line, no UI flag. You find out when the agent, for the fifth time, generates API errors as raw strings despite your beautifully written rule saying not to. This article is about the activation mechanics that decide whether a rule loads at all. The frontmatter Every .mdc file in .cursor/rules/ has YAML frontmatter with three fields: --- description: "Error handling for payment API routes" globs: ["src/api/payments/**/*.ts"] alwaysApply: false --- Your rule content here. Enter fullscreen mode Exit fullscreen mode Field Controls Used by description What the model sees when deciding whether the rule is relevant Auto Agent and Agent Requested modes globs Which file paths trigger inclusion Auto Agent; matches against files in context alwaysApply Force-load the rule into every request Overrides everything when true The four activation modes Cursor's UI presents four "rule types". They map onto the frontmatter like this: UI mode Frontmatter When it loads Always alwaysApply: true Every request, everywhere. Costs tokens always Auto Agent globs set, alwaysApply: false When files matching the globs appear in the agent's context Agent Requested description set, no globs, alwaysApply: false The model reads all rule descriptions and decides to pull the rule in Manual @ruleName mention Only when explicitly invoked in chat Practical guidance: Always is for rules that are genuinely universal. Keep the count low; every Always rule spends your instruction budget on every request. Auto Agent (globs) is the workhorse. Scope by path and the rule loads exactly when relevant code is in play. This is how you afford specific knowledge without bloating always-on config. Agent Requested is worth using for cross-cutting rules that do not map to a path: "when writing database migrations.." applies to files whose names vary. The risk is that the model's relevance judgment is a judgment; a weak description means rare firing. Write descriptions as retrieval queries, not labels: "Applying when writing or modifying database migrations, schema files, or seed data" beats "Database stuff". Manual is for heavyweight references you explicitly want: style guides, long API conventions. The five silent failure modes 1. Broken glob. globs: "src/api/**/*.ts" when your API lives in app/api/ matches nothing. The rule never loads. Nothing tells you. This is the most common failure by a wide margin, and it survives code review because the glob looks plausible. 2. Single-string glob quirks. Cursor accepts a single string or an array of strings. globs: "src/a/**/*.ts,src/b/**/*.ts" (comma-joined in one string) does not do what you hope. Use an array. 3. Description-only rules with bad descriptions. In Agent Requested mode the description is the entire retrieval surface. "Coding standards" fires on vibes. Include the trigger vocabulary the model needs to match against the task at hand. 4. alwaysApply sprawl. Every rule someone marks "important" becomes alwaysApply: true, and suddenly you are back to a 900-line implicit CLAUDE.md, except worse because it is twelve files and nobody has seen the total. 5. Filewatcher drift. Globs are matched against your repo as it exists today. Move src/api to apps/web/src/api in a restructure and every rule scoped to the old path dies on the same day, quietly. Nobody re-audits globs during a restructure. Auditing: make firing observable Because Cursor will not warn you, test the globs yourself. The essence of an audit is: does each glob match at least one real file, and roughly how many? # rules_audit.py -- check every .mdc glob against the repo import pathlib, re, sys broken = 0 for mdc in pathlib.Path(".cursor/rules").glob("*.mdc"): text = mdc.read_text() m = re.search(r'globs:\s*\[?(.+?)\]? ', text) if not m: print(f"{mdc.name}: no globs found") continue # hand the glob list to pathspec/minimatch of your choice here .. Enter fullscreen mode Exit fullscreen mode If you do not want to maintain that script: our validation harness, which ships with AgentConfig Studio, parses every rule's frontmatter (catching malformed YAML and missing activation modes), verifies required files and line budgets, and scans for placeholder rot. Every kit passes it before release, so the rule you install is the rule that loads. Want to start from a working baseline instead? The free Next.js sample kit is MIT licensed: take it, break it, keep it.

来源报道 · 1 篇 · 以下链接均前往原始媒体

← 返回精选动态 原文

更多内容