Skip to content

fix(rules): write Cursor rules as .mdc with derived frontmatter - #349

Merged
jeff-r2026 merged 2 commits into
Tencent:mainfrom
jeff-r2026:fix/cursor-mdc-rules
Aug 27, 2026
Merged

jeff-r2026 merged 2 commits into
Tencent:mainfrom
jeff-r2026:fix/cursor-mdc-rules

Conversation

@jeff-r2026

Copy link
Copy Markdown
Collaborator

Problem

teamai pull synced team rules into .cursor/rules/ as .md files, but Cursor project rules must be .mdc with YAML frontmatter (description / globs / alwaysApply). A plain .md there is silently ignored by Cursor, so team rules never entered a Cursor session — the sync reported success but had no effect. (Reported as git.woa.com/teamai/teamai-cli#3; confirmed against Cursor's current docs, which state "Project rules must use the .mdc extension" and "a plain .md file in .cursor/rules is ignored.")

RulesHandler.pullItem wrote ${item.name}.md for every tool with no per-tool branch. Push scanning and stale cleanup also only matched .md.

Fix

  • pull — for Cursor, write <name>.mdc and derive frontmatter from the team rule:
    • a rule scoped with a paths: list → globs: <comma-joined> + alwaysApply: false
    • a rule with no paths (a mandatory team rule) → alwaysApply: true
    • other tools still get a plain .md copy.
  • push — scanLocalForPush now also reads .cursor/rules/*.mdc, comparing markdown bodies only so a clean pull → push round-trip is not flagged as modified; pushItem strips the derived frontmatter when writing an .mdc back to the team repo (which stays tool-neutral .md).
  • cleanup — stale-file cleanup, removeItem, and the pull.ts tombstone cleanup all handle the .mdc extension for Cursor.

Frontmatter mapping and the tool-neutral team format are isolated in a new src/resources/cursor-mdc.ts. Body extraction is delimiter-based, not YAML-parsed: the Cursor globs value (a leading-* glob) is invalid YAML, so a strict parse would fail and swallow the frontmatter into the body.

Test Plan

  • npx tsc --noEmit — clean
  • npx vitest run — 2284 passed (18 new tests for .mdc conversion, round-trip stability, push read-back, stale cleanup, and remove; 1 stale assertion that encoded the old buggy behavior updated to expect .mdc)
  • Real CLI end-to-end (teamai init . → pull → push --dry-run):
    • Cursor dir gets .mdc, Claude dir stays .md
    • paths: rule → globs + alwaysApply:false; no-frontmatter rule → alwaysApply:true
    • push recognizes .cursor/rules/*.mdc (previously invisible)
    • second pull is idempotent, byte-identical output
    • deleting a team rule + pull removes the corresponding .mdc, keeps active ones

Docs

Added a Cursor section to docs/usage-guide.md and docs/usage-guide.zh-CN.md describing the .mdc behavior and frontmatter mapping.

Cursor project rules must live in .cursor/rules/ as .mdc files with YAML
frontmatter (description/globs/alwaysApply); a plain .md there is silently
ignored, so team rules synced via `teamai pull` never entered a Cursor
session. RulesHandler.pullItem wrote `${name}.md` for every tool.

- pull: for Cursor, write <name>.mdc and derive frontmatter from the team
  rule — a `paths:` list maps to `globs` + alwaysApply:false, and a rule
  with no paths (mandatory) maps to alwaysApply:true. Other tools still
  get a plain .md copy.
- push: scanLocalForPush now also reads .cursor/rules/*.mdc, comparing
  markdown bodies only so a pull→push round-trip is not seen as modified;
  pushItem strips the derived frontmatter when writing an .mdc back to the
  team repo.
- cleanup: stale-file cleanup, removeItem, and pull.ts tombstone cleanup
  handle the .mdc extension for Cursor.

Body extraction is delimiter-based, not YAML-parsed: the Cursor `globs`
value (a leading-star glob) is invalid YAML, so a strict parse would fail
and swallow the frontmatter into the body.

Fixes git.woa.com/teamai/teamai-cli#3
@jeff-r2026 jeff-r2026 self-assigned this Aug 27, 2026
…l site

Follow-up on the Cursor `.mdc` fix. The extension decision lived in a private
helper in rules.ts plus an inline special case in pull.ts, so the other five
writers of the same directories still assumed `.md`, and the push direction
dropped the team rule's frontmatter.

Correctness:
- pushItem now merges the Cursor body into the team file instead of replacing
  it, so a body edit in `.cursor/rules/*.mdc` no longer deletes the team rule's
  `paths:` scope for everyone on the next pull.
- pre-push-sync handles `.mdc`; it previously skipped Cursor entirely, so a
  stale local copy was reported as "modified" and pushed over a newer team rule.
- uninstall collects rules by stem, so Cursor's `.mdc` rules are actually
  removed instead of surviving uninstall.
- `globs` is emitted quoted: a glob starting with `*` is a YAML alias node, so
  the frontmatter we wrote could not be parsed (gray-matter throws on it) and
  Cursor could drop the rule — the exact failure this series fixes.
- Team frontmatter is parsed leniently, so a rule authored `globs: **/*.ts` is
  honoured instead of silently falling back to always-on.
- An unreadable source now fails loudly rather than writing an empty rule over
  the team repo (push) or a stub always-on rule (pull).
- The frontmatter regex accepts an empty `---/---` block instead of leaking the
  delimiters into the body.
- local-agent reports Cursor rules again.

Behavior:
- Built-in rules deploy to Cursor as `.mdc` too; `teamai-recall` was still being
  written as a `.md` Cursor ignores.
- `.cursor/rules/*.mdc` with no matching team rule is no longer offered as a new
  team resource — that is where Cursor's own "New Cursor Rule" writes personal
  rules.
- `.md` copies left by the pre-`.mdc` layout are swept by pull, remove,
  tombstone cleanup, and uninstall.

Altitude: the per-tool extension moves to src/resources/rule-format.ts
(mirroring agentFileExtensionForTool) so all call sites share one decision.

Docs updated in both languages; the old text claimed the body was preserved
verbatim and that a Cursor push sent "just that body change" upstream.
@jeff-r2026

Copy link
Copy Markdown
Collaborator Author

Follow-up commit addressing review findings on this branch.

Data loss / correctness

  • Push from Cursor deleted the team rule's paths: frontmatter. cursorMdcToTeamMd returned body-only, so editing a body in .cursor/rules/*.mdc and pushing wiped the team rule's scoping for everyone — the rule silently flipped to alwaysApply: true on the next pull. Replaced with mergeCursorBodyIntoTeamMd, which keeps the team file's own frontmatter and swaps only the body.
  • Pre-push sync skipped .mdc entirely (pre-push-sync.ts still matched .md), so a stale local Cursor copy was reported as modified and pushed over a teammate's newer rule.
  • uninstall left every Cursor rule on disk — it collected only .md, so team rules kept being injected into Cursor sessions after uninstall.
  • The emitted globs was not valid YAML. globs: **/*.ts is an alias node — this repo's own gray-matter throws unidentified alias "*/*.ts" on the frontmatter we wrote. If Cursor parses it with a real YAML parser, the rule is dropped, which is the exact failure this PR fixes. Now emitted quoted.
  • data.globs fallback was dead for the values it existed for (same YAML reason). Frontmatter is now parsed leniently, so globs: **/*.ts in a team rule is honoured instead of falling back to always-on.
  • An unreadable source silently wrote an empty file (raw ?? '') — over the team repo on push, or as a stub always-on rule on pull. Both now fail loudly.
  • The frontmatter regex rejected an empty ---/--- block, leaking the delimiters into the body and on into the team repo.
  • local-agent stopped reporting Cursor rules (scanRulesFromDisk matched .md only).

Behavior

  • Built-in rules deploy to Cursor as .mdc too — teamai-recall was still being written as a .md Cursor ignores, so the recall instruction never reached a Cursor session.
  • A .cursor/rules/*.mdc with no matching team rule is no longer offered as a new team resource. That directory is also where Cursor's own New Cursor Rule command writes personal rules, and push was proposing to publish them (frontmatter stripped) to the team repo.
  • .md copies left by the pre-.mdc layout are now swept by pull, remove, tombstone cleanup, and uninstall. Previously they were invisible to every path and survived teamai remove forever.

Altitude

The per-tool extension moved out of a private helper in rules.ts (plus an inline tool === 'cursor' special case in pull.ts) into src/resources/rule-format.ts, mirroring the existing agentFileExtensionForTool. That is what left five call sites out of sync in the first place.

Test Plan

  • npx tsc --noEmit — clean
  • npx vitest run — 2301 passed (+17: frontmatter preservation, unreadable-source guard, personal-rule exclusion, legacy .md sweep, built-in .mdc, uninstall, three pre-push-sync cases). The test that asserted the lossy push now asserts paths: survives.
  • Real CLI end-to-end against a local team repo (pull → edit .mdc → push → pull --force → uninstall):
    • scoped rule → globs: "**/*.ts, **/*.tsx" + alwaysApply: false, and the emitted frontmatter now parses as YAML; no-paths rule → alwaysApply: true
    • second pull byte-identical; clean tree reports nothing to push
    • a personal .cursor/rules/my-personal.mdc is not offered as a new team resource
    • body edit in the .mdc → pushed branch diff is only the body line; paths: intact upstream
    • team-side rule deletion + pull --force removes both .mdc and the legacy .md
    • teamai-recall lands as .mdc with alwaysApply: true; the legacy .md is swept; Claude still gets plain .md
    • uninstall removes the team and built-in .mdc rules, leaves a user-authored .mdc alone

@jeff-r2026
jeff-r2026 merged commit 0d01a78 into Tencent:main Aug 27, 2026
7 checks passed
@jeff-r2026
jeff-r2026 deleted the fix/cursor-mdc-rules branch August 27, 2026 14:07
jeff-r2026 added a commit that referenced this pull request Aug 27, 2026
…-up) (#355)

#349 moved Cursor rules from .md to .mdc, but the project-agent
cold-start E2E still asserted .cursor/rules/team-rule.md, so the
GitHub full-surface E2E job failed on that stale assertion.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…ent#349)

* fix(rules): write Cursor rules as .mdc with derived frontmatter

Cursor project rules must live in .cursor/rules/ as .mdc files with YAML
frontmatter (description/globs/alwaysApply); a plain .md there is silently
ignored, so team rules synced via `teamai pull` never entered a Cursor
session. RulesHandler.pullItem wrote `${name}.md` for every tool.

- pull: for Cursor, write <name>.mdc and derive frontmatter from the team
  rule — a `paths:` list maps to `globs` + alwaysApply:false, and a rule
  with no paths (mandatory) maps to alwaysApply:true. Other tools still
  get a plain .md copy.
- push: scanLocalForPush now also reads .cursor/rules/*.mdc, comparing
  markdown bodies only so a pull→push round-trip is not seen as modified;
  pushItem strips the derived frontmatter when writing an .mdc back to the
  team repo.
- cleanup: stale-file cleanup, removeItem, and pull.ts tombstone cleanup
  handle the .mdc extension for Cursor.

Body extraction is delimiter-based, not YAML-parsed: the Cursor `globs`
value (a leading-star glob) is invalid YAML, so a strict parse would fail
and swallow the frontmatter into the body.

Fixes git.woa.com/teamai/teamai-cli#3

* fix(rules): preserve team frontmatter and cover every Cursor .mdc call site

Follow-up on the Cursor `.mdc` fix. The extension decision lived in a private
helper in rules.ts plus an inline special case in pull.ts, so the other five
writers of the same directories still assumed `.md`, and the push direction
dropped the team rule's frontmatter.

Correctness:
- pushItem now merges the Cursor body into the team file instead of replacing
  it, so a body edit in `.cursor/rules/*.mdc` no longer deletes the team rule's
  `paths:` scope for everyone on the next pull.
- pre-push-sync handles `.mdc`; it previously skipped Cursor entirely, so a
  stale local copy was reported as "modified" and pushed over a newer team rule.
- uninstall collects rules by stem, so Cursor's `.mdc` rules are actually
  removed instead of surviving uninstall.
- `globs` is emitted quoted: a glob starting with `*` is a YAML alias node, so
  the frontmatter we wrote could not be parsed (gray-matter throws on it) and
  Cursor could drop the rule — the exact failure this series fixes.
- Team frontmatter is parsed leniently, so a rule authored `globs: **/*.ts` is
  honoured instead of silently falling back to always-on.
- An unreadable source now fails loudly rather than writing an empty rule over
  the team repo (push) or a stub always-on rule (pull).
- The frontmatter regex accepts an empty `---/---` block instead of leaking the
  delimiters into the body.
- local-agent reports Cursor rules again.

Behavior:
- Built-in rules deploy to Cursor as `.mdc` too; `teamai-recall` was still being
  written as a `.md` Cursor ignores.
- `.cursor/rules/*.mdc` with no matching team rule is no longer offered as a new
  team resource — that is where Cursor's own "New Cursor Rule" writes personal
  rules.
- `.md` copies left by the pre-`.mdc` layout are swept by pull, remove,
  tombstone cleanup, and uninstall.

Altitude: the per-tool extension moves to src/resources/rule-format.ts
(mirroring agentFileExtensionForTool) so all call sites share one decision.

Docs updated in both languages; the old text claimed the body was preserved
verbatim and that a Cursor push sent "just that body change" upstream.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…follow-up) (Tencent#355)

Tencent#349 moved Cursor rules from .md to .mdc, but the project-agent
cold-start E2E still asserted .cursor/rules/team-rule.md, so the
GitHub full-surface E2E job failed on that stale assertion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant