Skip to content

feat: attach Cursor glob rules to chat turns by touched files - #235

Merged
ibetitsmike merged 11 commits into
mainfrom
mike/rules-glob-attach
Nov 12, 2026
Merged

ibetitsmike merged 11 commits into
mainfrom
mike/rules-glob-attach

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Stacked on #232. This attaches Cursor glob rules (.mdc rules with globs and no alwaysApply) to a chat turn when the chat has touched a file the globs match. #232 discovers these rules but holds them back, so that they are not injected on every turn.

Agent (agent/agentcontext)

  • InstructionFileBody gets repeated string globs = 100, and PushContextStateResponse gets bool fork_glob_rules = 102. Tags 100 and up are reserved for fork fields.
  • The agent pushes glob rules as instruction files that carry their globs, but only to a coderd that advertises fork_glob_rules. Without it, the feat(agent/agentcontext): load always-apply rules from rule directories #232 behavior stays the same: they are not pushed. A new push connection resets the capability, so a reconnect to an older coderd never starts with glob rules.
  • Glob rules now go through the same dedup and the same per-resource, count and byte caps as AGENTS.md. The cap order keeps always-on instruction files before glob rules, so a glob rule cannot push AGENTS.md out.

coderd (coderd/agentapi, coderd/x/chatd)

  • context.go advertises fork_glob_rules. Storage needs no change, because the stored body is protojson.
  • contextResourcesToPrompt keeps rows with globs out of the always-on instruction block.
  • fork_scoped_rules.go collects the paths the chat touched from the path argument of read_file, write_file and edit_files calls. It matches them with doublestar, relative to the rule's project root (the parent of .cursor/rules, or the home directory for user rules), and appends the matching rule bodies to the instruction block. A glob with no /, such as *.ts, matches at any depth. Paths outside the project root never match.
  • The touched paths are recomputed on every step, so a file read earlier in a turn attaches its rule on the next model call.
  • ARCHITECTURE.md gets a TODO for the author.

Known gaps:

  • coderd advertises the capability with no experiment gate. With mixed replicas, an older replica could inject glob rules on every turn.
  • Touched paths come from the prompt history only. Files touched before a compaction stop matching, and files read through other tools (for example cat in execute) are not counted.
  • When a rule starts matching, the system prompt changes, which costs one prompt-cache miss.
  • Matching assumes POSIX paths.
  • Only .cursor/rules and .coder/rules sources get a derived project root. Other rule folders fall back to the agent directory.
  • A glob without a slash, such as *.ts, matches at any depth, like a gitignore pattern. Cursor's docs say *.ts matches only files in the project root, so this attaches such rules more often than Cursor does.
  • A rule attaches on the model call after the chat touches a matching file. When the first touch is a write_file or edit_files call, that write runs without the rule.
  • A scalar globs: value with an inline YAML comment, or with commas inside braces (*.{ts,tsx}), is parsed wrongly by feat(agent/agentcontext): load always-apply rules from rule directories #232's frontmatter reader.
  • The chat context list shows glob rules as plain instruction files, without their globs.
  • The agent's byte cap counts the rule body but not the bytes its globs add.
  • Isolated worktree subagents get no scoped rules, because their tool paths resolve under the worktree, not the original checkout.
  • A symlinked .cursor/rules under an added context source falls back to the agent directory as its project root.
  • Touched paths are not trimmed, and failed or denied file-tool calls still count as touched.

Validation on 4008538 plus the regenerated agent.pb.go (d3c83cd; the head after the main merge, 0562033, has the same tree), run in a container:

  • make agent/proto/agent.pb.go with libprotoc 23.4 changed only agent/proto/agent.pb.go.
  • gofmt -l on the changed files passes.
  • go build ./agent/... ./coderd/... ./enterprise/... passes.
  • go vet ./agent/agentcontext/ ./coderd/agentapi/ ./coderd/x/chatd/ passes.
  • go test ./agent/agentcontext/ passes.
  • go test -run Context ./coderd/agentapi/ passes.
  • go test -run 'ContextResourcesToPrompt|ScopedRules|TouchedFilePaths|PinnedWorkspace|Plugin|Pack|Instruction' ./coderd/x/chatd/ passes.
  • Negative controls, each of which fails as intended:
    • never pushing glob rules: TestResolver_GlobRulesAdvertisedArePushed
    • dropping the cap order: TestResolver_GlobRulesShareCaps/Count
    • keeping glob rows in the always-on block: TestContextResourcesToPrompt_ScopedRules/UnrelatedFile

Agents push glob-scoped .mdc rules as instruction files carrying their
globs once coderd advertises fork_glob_rules, so the rules go through the
shared per-resource, count and byte caps (cut after always-on files).
Without the capability the D1 behavior stays: they are not pushed.

chatd keeps instruction rows with globs out of the always-on
workspace-context block and appends a rule's body when a read_file,
write_file or edit_files path in the prompt history matches one of its
globs, relative to the rule's project root.

Generated code (agent/proto/agent.pb.go) must be regenerated.
# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T08:07:30.394280Z cf146a8 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Base automatically changed from mike/rules-dirs-always-apply to main November 12, 2026 07:34

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d3c83cdaaa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_scoped_rules.go Resolved
Comment thread coderd/x/chatd/fork_scoped_rules.go Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/agentapi/context.go Resolved
Comment thread agent/agentcontext/drpc.go Resolved
# Conflicts:
#	agent/agentcontext/fork_rules.go
#	agent/agentcontext/fork_rules_test.go
#	agent/agentcontext/resolve.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0562033ef1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_scoped_rules.go Resolved
Comment thread coderd/x/chatd/fork_scoped_rules.go Resolved
Comment thread coderd/x/chatd/fork_scoped_rules.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cf146a8e1b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/turn_environment.go Resolved
Comment thread coderd/x/chatd/fork_scoped_rules.go Resolved
Comment thread coderd/x/chatd/fork_scoped_rules.go Resolved
@ibetitsmike
ibetitsmike merged commit 00e1260 into main Nov 12, 2026
10 checks passed
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum merged this PR on behalf of @ibetitsmike.

Merge evidence for head cf146a8e1b71f4e5f0104a984ef958e3776d9170:

  • fork-ci run 43179377324 passed on this head. All checks on the head pass.
  • Codex review 5871112559 is on this head. Its 3 P1 findings and all earlier findings were replied to and resolved. 11 threads, 0 unresolved.
    • Touched paths lost at compaction, and a first write before its rule attaches: accepted as known gaps. The rule attaches on the next read or edit of the file.
    • Slashless globs such as *.ts match at any depth, but Cursor's docs say they match only root files. This over-attaches context and never breaks a turn. It gets a small follow-up PR for root-only semantics.
  • UAT-235 passed 4/4 on :3100 on this exact head: always rule only, rule absent after a non-matching read, glob rule attached on the step after a matching read, restore and leak audit clean.
  • Old agents never set the new globs field (proto field 100), so their glob rules are skipped, not injected on every turn. Covered by TestScopedRulesFromInstructionFiles.
  • The ARCHITECTURE.md change adds a TODO for @ibetitsmike only.

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