Skip to content

feat: add rule to limit stored procedures to single-partition atomic …#180

Open
ramnnn2006 wants to merge 3 commits into
AzureCosmosDB:mainfrom
ramnnn2006:feat/pattern-stored-procedure-scope
Open

feat: add rule to limit stored procedures to single-partition atomic …#180
ramnnn2006 wants to merge 3 commits into
AzureCosmosDB:mainfrom
ramnnn2006:feat/pattern-stored-procedure-scope

Conversation

@ramnnn2006

Copy link
Copy Markdown

Description

This adds a new rule under Design Patterns about when to actually use stored procedures in Cosmos DB. The short version is sprocs only make sense when you need multiple writes to commit atomically in the same partition and transactional batch can't do it. For everything else (single item CRUD, cross partition stuff, heavy computation) the rule says use plain SDK calls or transactional batch instead. Also covers the limitations like the 5 second execution cap and how painful sprocs are to debug. Includes the regenerated AGENTS.md and an eval task.

Type of Change

  • 📝 New rule - Adding a new best practice rule
  • ✏️ Rule improvement - Updating an existing rule
  • 🆕 New skill - Adding an entirely new skill
  • 🐛 Bug fix - Fixing an issue with existing content
  • 📚 Documentation - Updating README, CONTRIBUTING, or other docs
  • 🔧 Build/Scripts - Changes to build process or scri

Checklist

  • I have read the Contributing Guide
  • I ran npm run validate and it passed
  • I ran npm run build to regenerate AGENTS.md (if a
  • My rule file follows the naming convention: {prefix}-{description}.md
  • My rule includes valid frontmatter (title, `impac

Tests (Required)

  • I added/updated an eval task in `evals/cosmosdb-bes
  • I ran waza run evals/cosmosdb-best-practices/eval.yaml and all tasks pass
  • My task file includes id, name, description, d.outcomes`

Eval task file: `evals/cosmosdb-best-practices/tasks/

For New Rules

Rule file: skills/cosmosdb-best-practices/rules/patt

Category: Design Patterns

Impact level: Medium

Why is this rule important?
Sprocs get treated like a free server side compute layer, but they only see one partition, get killed around 5 seconds, and debugging them is rough since it's JavaScript living inside the database. Went will happily suggest a sproc for a basic insert orsome aggregation, when a normal SDK call or transactional batch does the same job and is way easier to test and maintain. The rule keeps sprocs for the one case where they actually earn their keep, whis to read data before deciding what to write, all withinone partition.

Agent Testing

  • Tested with GitHub Copilot
  • Tested with Claude Code
  • Tested with Cursor
  • Tested with other agent: _____
  • N/A (documentation only)

Related Issues

Closes #174

@ramnnn2006 ramnnn2006 force-pushed the feat/pattern-stored-procedure-scope branch from c6cdb90 to 77d0c22 Compare June 10, 2026 18:38

@avinashkamat48 avinashkamat48 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.

The generated AGENTS.md section now has ### 9.15 ... followed immediately by another ## Use Stored Procedures Only... heading and a second Impact line. That duplicates the rule heading inside the numbered hierarchy and makes the AGENTS doc structure inconsistent with the TOC entry. Could you drop the standalone rule ## heading / duplicate impact when embedding this into AGENTS.md, similar to the surrounding numbered sections?

@ramnnn2006

ramnnn2006 commented Jun 14, 2026

Copy link
Copy Markdown
Author

@avinashkamat48-design
hey ,, good catch, that duplicate impact line shouldn't have been in the rule body. dropped it and regenerated AGENTS.md so 9.15 now matches the surrounding numbered sections (heading + single impact line). could you take another look when you get a sec?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new Cosmos DB best-practice rule to the cosmosdb-best-practices skill clarifying that stored procedures should be used only for multi-document atomic operations within a single partition, and otherwise prefer normal SDK calls or transactional batch. It also updates the compiled AGENTS.md output and adds an eval task to validate the behavior.

Changes:

  • Added a new Design Patterns rule: pattern-stored-procedure-scope.md.
  • Regenerated skills/cosmosdb-best-practices/AGENTS.md to include the new rule in the TOC and content.
  • Added a new eval task YAML to exercise the rule’s expected guidance.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
skills/cosmosdb-best-practices/rules/pattern-stored-procedure-scope.md New rule defining correct/incorrect stored procedure usage with examples and references.
skills/cosmosdb-best-practices/AGENTS.md Compiled skill output updated to include the new rule.
evals/cosmosdb-best-practices/tasks/pattern-sproc-scope.yaml New evaluation task to ensure the guidance is triggered appropriately.

Comment on lines +82 to +93
function transfer(fromId, toId, amount) {
var collection = getContext().getCollection();
collection.readDocument(documentLink(fromId), function (err, from) {
if (err) throw err;
if (from.balance < amount) throw new Error("Insufficient funds");
from.balance -= amount;
collection.replaceDocument(from._self, from, function (err) {
if (err) throw err; // any failure rolls back the whole transfer
// ...read and credit the destination account next
});
});
}
Comment on lines +11532 to +11535
function transfer(fromId, toId, amount) {
var collection = getContext().getCollection();
collection.readDocument(documentLink(fromId), function (err, from) {
if (err) throw err;

@TheovanKraay TheovanKraay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good rule, stored procedures are definitely over-recommended by agents. A few issues:

The "When a stored procedure is still the right tool" example calls documentLink(fromId) which isn't defined anywhere in the snippet. Copilot flagged this too. Please make it self-contained (e.g., query for the document or construct the link explicitly).

There's still a duplicate heading in the rule body. ## Use Stored Procedures Only... appears right after the compile.js-generated ### 9.15 heading. Please remove the ## heading from the source rule file and rebuild.

The transfer sproc example is incomplete (stops at // ...read and credit the destination account next). Since this is the example showing when a sproc is warranted, it should demonstrate the full pattern.

Also, we recently merged a skill split (#204) that added topic-specific skills alongside the monolith. We're in a transitional phase where both coexist while we evaluate whether agent routing can handle the split on its own. Until that's resolved, new rules need to live in both places. Since this is a pattern- prefixed rule, please copy skills/cosmosdb-best-practices/rules/pattern-stored-procedure-scope.md to skills/cosmosdb-design-patterns/rules/pattern-stored-procedure-scope.md and then run npm run build to regenerate AGENTS.md for both skills.

@jaydestro

Copy link
Copy Markdown
Contributor

@ramnnn2006 there are some ongoing changes being evaluated to the structure that could require this to be modified. you'll definetely get notice when it's time to make any changes to avoid merge conflicts.

@ramnnn2006

Copy link
Copy Markdown
Author

okay , lmk when I could start working on it then @jaydestro

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.

[Rule] Use stored procedures only for multi-document atomic operations within a single partition

5 participants