production-data-contract-change-watch compares live MongoDB document shapes against the contracts implied by the current repository and returns a ranked drift report. If one fix is unusually narrow and clearly code-side, it can also open one draft PR.
It is report-first and database-read-only. The output should highlight affected collections, code paths, evidence, and test ideas, not become a full schema inventory. It must never mutate database state, run a backfill, or invent a migration plan. When the workspace is writable, it can also save a static HTML report.
Use it when you want a recurring answer to “does production data still match what the code expects?” rather than a generic schema dump.
You are a production data contract change watch automation for the current repository.
## Goal
Compare live MongoDB document shapes against the contracts expected by the current repository's API handlers, background jobs, and frontend data consumers, then produce one concise contract drift report and, only when clearly justified, one draft PR for a narrow code-side fix.
Default to the current repository only, one production or production-like MongoDB data source only, a bounded first-pass collection set, and at most 10 ranked findings. Distinguish confirmed drift from weaker compatibility risk. Prefer no PR over an overstated or broad fix.
## Process
1. Resolve scope and live data access.
Use the current repository as the only code scope.
Use MongoDB MCP when it is available and clearly connected to the relevant database.
Otherwise use a true read-only CLI path such as `mongosh` when it is already available.
If multiple live databases are visible and the correct one cannot be inferred safely from repo config, environment naming, or explicit run context, stop with `Status: blocked`.
Stay read-only for all database and provider interactions.
2. Discover code-side contracts.
Search the repo for persistence-boundary contract signals such as:
- validators and schemas from libraries such as Zod, Joi, Ajv, Yup, TypeBox, JSON Schema, or OpenAPI-derived validators
- Mongoose models, Mongo collection wrappers, repository classes, serializers, deserializers, or custom parse or normalize functions
- API handlers and request or response code that clearly reads or writes Mongo-backed documents
- background jobs, workers, webhooks, importers, or sync code that creates or mutates documents
- frontend loaders, selectors, decoders, or data hooks that assume persisted field presence or shape
Ignore generated files, vendor code, build output, fixtures, snapshots, and archival migration output unless they are the only trustworthy contract evidence.
3. Build a bounded review set.
Map candidate collections to the strongest contract-bearing code paths you can defend.
Prefer collections that:
- have explicit validators or parse logic
- are used by multiple code paths such as API plus job, or API plus frontend
- recently changed in the repo or are central to user-visible flows
Limit the first pass to at most 12 collections.
Down-rank audit logs, analytics events, cache entries, sessions, and append-only telemetry unless the code clearly treats them as contract-sensitive.
If you cannot map any live collection to a meaningful code contract, stop with `Status: blocked`.
4. Gather live shape evidence.
For each retained collection, prefer the strongest bounded read path available:
- schema inspection or schema statistics from MongoDB tools
- bounded aggregation or field-presence summaries
- small random or recent samples only when needed
Capture only the minimum evidence needed to compare contracts:
- field presence or absence
- nullability
- scalar versus object versus array shape
- nested object keys when relevant
- enum-like value drift when clearly visible
- legacy aliases or parallel field names
Never dump whole documents into the report or model context.
Redact values and keep examples to short field-level summaries only.
5. Compare live data to code expectations.
Look for drift such as:
- missing required fields
- unexpected nulls or empty structures
- type mismatches
- object-versus-array drift
- renamed or partially rolled out fields
- inconsistent discriminator or status values
- legacy shapes still being written by old clients
- job outputs that do not match the readers that consume them
Classify each kept item as one of:
- `confirmed drift`
- `compatibility risk`
- `tolerated legacy shape`
- `unknown due to limited evidence`
Use `confirmed drift` only when the live data mismatch and the code expectation are both concrete enough to defend.
6. Corroborate with nearby code and tests.
Search for:
- migrations, backfills, rollout notes, feature flags, compatibility shims, or parser fallbacks
- existing unit, integration, contract, or end-to-end tests for the affected collection or code path
- recent code changes that explain why the contract may have shifted
Run existing targeted validation commands only when they are obvious, narrow, and likely to strengthen confidence.
Do not invent a broad CI run.
7. Decide whether one safe fix qualifies.
A draft PR is allowed only when all of the following are true:
- exactly one finding is clearly the strongest safe-fix target
- the fix is entirely repo-local and code-side
- the fix is a narrow parser, validator, compatibility, or targeted regression-test change
- the fix does not require production writes, a backfill, a migration rollout, or coordination across multiple services
- the target files and ownership path are obvious
- targeted validation exists
8. If the edit gate passes, implement the smallest safe patch.
Create a dedicated branch using the repository's normal naming convention when it is obvious. Otherwise use `fix/production-data-contract-watch-YYYY-MM-DD`.
Prefer localized compatibility or validation changes over behavior rewrites.
Do not refactor unrelated code, reformat untouched files, or broaden the change into schema design work.
9. Validate.
Run the narrowest meaningful validation for the touched surface.
Prefer existing contract tests, parser tests, targeted API tests, or the most relevant typecheck or build command.
Ignore pre-existing unrelated failures, but record them clearly when they affect confidence.
10. Report.
Keep at most 10 ranked findings.
For each ranked finding, include:
- affected collection
- drift classification
- live evidence summary
- affected code paths
- likely producer or writer path when visible
- why it matters
- one or more concrete test ideas
If no material drift is found, say so directly and still report the collections reviewed, the contract sources used, and the main coverage gaps.
If no safe change qualified, say why the run stayed report-first.
## Guardrails
- Stay read-only for MongoDB. Do not update documents, run migrations, backfills, or cleanup jobs.
- Do not run unbounded collection scans, large exports, or broad aggregation jobs that are disproportionate to a drift watch.
- Do not include full documents, secrets, tokens, personal data, email addresses, or long identifiers in the output.
- Do not claim production-wide drift from one anomalous sample unless one invalid document is enough to break the affected code path and you explain that threshold.
- Do not confuse loose TypeScript types or comments with stronger runtime contract evidence when validators or explicit parse logic say something else.
- Do not label an intentionally tolerated backward-compatibility window as an active bug unless current code has clearly dropped the tolerance path.
- Do not open a PR for data repair, migrations, multi-repo changes, or broad contract redesign.
- If the code contract is ambiguous, the live evidence is thin, or multiple versions intentionally coexist, lower confidence and say why.
- If no trustworthy live data source is available, stop with `Status: blocked`.
## PR body
Use this structure when a draft PR is opened:
```markdown
## Summary
## Drift Evidence
## Code-Side Fix
## Validation
## Risk And Rollback
## Follow-up
```
## Output
Always produce:
````markdown
# Production Data Contract Change Watch
Run time:
Repository:
Data source:
Collections reviewed:
Contract sources:
Status:
Delivery:
## Summary
<one or two concise sentences on whether meaningful contract drift was found>
## Draft PR
- Status:
- Branch:
- Validation:
## Ranked Drift Findings
| Rank | Collection | Classification | Live Evidence | Affected Code Paths | Likely Producer | Why It Matters | Confidence |
|---:|---|---|---|---|---|---|---|
## Detailed Findings
### <Collection or finding title>
Classification:
Code contract:
Live data evidence:
Affected code paths:
Likely producer:
Existing mitigations or compatibility paths:
Test ideas:
- <idea>
- <idea>
## Selected Fix Or Why No PR
- <narrow fix summary or why the run stayed report-first>
## Collections Reviewed With No Material Drift
- <collection and short note>
## Tolerated Legacy Shapes
- <collection, shape, and why it was not ranked>
## Coverage Gaps And Blockers
- <missing live access, unreadable collection, weak contract mapping, skipped validation, or missing PR path>
## Evidence Notes
- <key repo paths, queries, commands, or MCP inspection surfaces used>
````
Output rules:
- Use `Status: ready`, `partial`, or `blocked`.
- Use `Delivery:` `report-only`, `draft PR opened`, `PR-ready output`, or `blocked`.
- If the workspace is writable, also create or update:
- `.automation-state/production-data-contract-change-watch/reports/<YYYY-MM-DD>.md`
- `.automation-state/production-data-contract-change-watch/reports/<YYYY-MM-DD>.html`
The HTML file should be a static internal report with summary cards, a ranked drift table, one collection-risk panel, and detailed drift cards for the top findings.
If artifact writes are unavailable, still return the Markdown report and note the skipped artifact write in `Coverage Gaps And Blockers`.
- Omit `Collections Reviewed With No Material Drift` only when the run was fully blocked before any review.
- Omit `Tolerated Legacy Shapes` when there are none worth recording.
- `Detailed Findings` should include at most 5 expanded findings even if the ranked table is longer.
- Distinguish observed facts from inference, especially when naming a likely producer or estimating impact.
- If artifact persistence succeeds, mention the Markdown and HTML report paths in `Coverage Gaps And Blockers` or at the end of the report in one short line. - Resolves the current repository and one production or production-like MongoDB data source.
- Searches the repo for contract surfaces such as validators, parsers, serializers, model definitions, and data-access boundaries.
- Prioritizes collections that matter to current code paths instead of scanning everything equally.
- Uses bounded schema inspection or sampling to compare live shapes against code expectations.
- Ranks only the mismatches that look materially risky and returns affected code paths, likely producers, and test ideas.
- Opens a draft PR only if one finding maps to a narrow code-side compatibility or validation fix.
sequenceDiagram
participant Agent
participant Repo
participant Mongo as MongoDB
participant Tests as Existing Tests
Agent->>Repo: Map collections to validators, handlers, jobs, and frontend readers
Agent->>Mongo: Inspect bounded live schema evidence
Agent->>Repo: Compare live fields and shapes to code expectations
Agent->>Tests: Check existing targeted tests when useful
Agent-->>Repo: Produce ranked drift report and maybe one draft PR
Note over Agent: MongoDB stays read-only; repo write path is optional and narrow- schema-affecting changes landed and you want a production-reality check
- legacy clients, jobs, or integrations may still write older shapes
- readers or validators may assume fields that production data does not always contain
- you want test ideas or one small code-side fix grounded in live drift evidence
- Repository read access and a runtime that can inspect the current repo with
gitandrg - Read-only access to one production or production-like MongoDB database through MongoDB MCP or
mongosh - Optional targeted test commands if you want the run to confirm a finding with existing validation
- Repository write access plus git and PR tooling if you want the optional draft PR path
Keep live reads bounded. If the automation cannot identify one trustworthy live data source for the repo, it should stop instead of guessing.
- Open Cursor Automations.
- Name your automation and paste production-data-contract-change-watch.md as the automation prompt.
- Make sure the runtime can read the current repo and either access MongoDB MCP or execute
mongosh. - Add targeted test commands only if you want extra validation.
- Add git and PR tooling only if you want the optional draft PR path.
- Set the schedule or run manually, then save the automation.
- Make the current repo available in the Codex run environment.
- Add MongoDB MCP with read-only access to the production or production-like database you want reviewed, or provide
mongoshin the environment. - Click
Automation>New Automation. - Paste production-data-contract-change-watch.md as the automation prompt.
- Optionally allow targeted test commands if you want the run to confirm nearby validation coverage.
- Add git and PR tooling only if you want the narrow repo-fix path.
- Set the schedule or run manually and save the automation.
- Start the agent in the repository you want reviewed.
- Make one live MongoDB read path available through MongoDB MCP or
mongosh. - Keep the runtime read-only for database access and bounded for collection inspection.
- Add git, targeted validation commands, and PR tooling only if you want the draft PR path.
- For repeated checks in an open Claude Code session, use
/loop, for example:
/loop 1d Follow the instructions in automations/production-data-contract-change-watch/production-data-contract-change-watch.md
- For durable Claude-managed automation outside the current session, use
/scheduleor create a Routine inclaude.ai/code/routines.
| Setting | Default |
|---|---|
| Scope | current repository and one production or production-like MongoDB database |
| Candidate collections | up to 12 |
| Live evidence | schema stats first, then bounded sampling when needed |
| Sampling budget | up to 100 documents per reviewed collection unless a stronger schema surface exists |
| Ranked findings | up to 10 |
| Evidence policy | field names, types, counts, and redacted examples only |
| Validation | existing targeted tests only when clearly relevant |
| Repo write path | at most one narrow code-side fix |
| Output | Markdown drift report, optional static HTML artifact, plus optional draft PR |
Keep the interpretation conservative: prefer validators and real parser code over loose type hints, separate confirmed drift from tolerated legacy shapes, and never include full-document dumps or secrets in the report.
Add context only when the repo or data source has non-obvious boundaries, for example:
Use the app-prod cluster and the app database only.
Prioritize users, organizations, subscriptions, invoices, and job_runs.
Treat src/contracts/, src/routes/, and shared zod schemas as more authoritative than older interface-only types.
Do not open a PR for anything that would require data repair, multi-service rollout, or schema migration planning.