|
| 1 | +# OpenCRE Agent Instructions |
| 2 | + |
| 3 | +Cursor agents working in this repo must follow the rules in `.cursor/rules/`. |
| 4 | + |
| 5 | +## Quick start |
| 6 | + |
| 7 | +1. **Requirements gate** — If goal, success criteria, `@` file refs, or constraints are missing, stop and ask (`requirements-gate.mdc`). |
| 8 | +2. **Plan first** — Non-trivial or multi-file work requires a plan and user approval before coding (`plan-first-workflow.mdc`, `multi-agent-workflow.mdc`). |
| 9 | +3. **Verify** — After code changes, run checks and iterate until green (`verifiable-goals.mdc`): |
| 10 | + - `make lint` |
| 11 | + - `make mypy` |
| 12 | + - `make test` |
| 13 | + - `make frontend` (if frontend touched) |
| 14 | +4. **Review** — Substantive work needs independent judge/subagent review (`multi-agent-workflow.mdc`). |
| 15 | +5. **Production gap analysis** — On Heroku/opencreorg, gap analysis is cache-only: serve precomputed rows from Postgres; do not compute GA on production (cache miss → 404). |
| 16 | + |
| 17 | +## Operational scripts |
| 18 | + |
| 19 | +Prefer existing Makefile targets and `scripts/` helpers over ad-hoc Docker, GA, import, or prod-DB setup. Read script headers or `--help` for env vars and flags; do not reimplement their logic. |
| 20 | + |
| 21 | +### Local Docker services |
| 22 | + |
| 23 | +Use Makefile targets — do not hand-roll `docker run`: |
| 24 | + |
| 25 | +```bash |
| 26 | +make docker-neo4j # Neo4j (7474/7687) |
| 27 | +make docker-redis # Redis Stack (6379/8001) |
| 28 | +make docker-postgres # local Postgres (5432, cre/password) |
| 29 | +make start-containers # neo4j + redis only |
| 30 | +make migrate-upgrade # after postgres is up |
| 31 | +``` |
| 32 | + |
| 33 | +Reset volumes: `make docker-neo4j-rm` / `make docker-redis-rm`. |
| 34 | + |
| 35 | +### Imports and Neo4j populate |
| 36 | + |
| 37 | +```bash |
| 38 | +make import-all # full standards import via scripts/import-all.sh |
| 39 | +make import-projects # skip core CRE import (CRE_SKIP_IMPORT_CORE=1) |
| 40 | +make import-neo4j # populate Neo4j from cache |
| 41 | +``` |
| 42 | + |
| 43 | +`scripts/import-all.sh` handles parallel importers, SQLite export, and verification. For embeddings-only SQLite → Postgres push, use `scripts/sync_embeddings_table.py` (see its docstring). |
| 44 | + |
| 45 | +### Gap analysis (local compute, prod verify) |
| 46 | + |
| 47 | +**Local backfill** (starts containers, workers, migrations — see `scripts/backfill_gap_analysis.sh`): |
| 48 | + |
| 49 | +```bash |
| 50 | +make backfill-gap-analysis # parallel workers + --ga_backfill_missing |
| 51 | +make backfill-gap-analysis-sync # Neo4j populate + backfill without queue |
| 52 | +make sync-gap-analysis-table-local # upsert material rows sqlite → local Postgres |
| 53 | +``` |
| 54 | + |
| 55 | +**Prod / staging checks** (HTTP only — never compute GA on opencreorg): |
| 56 | + |
| 57 | +```bash |
| 58 | +make verify-ga-complete-prod # scripts/verify_ga_completeness.py |
| 59 | +make monitor-ga-health-prod # scripts/monitor_ga_health.py (503 / empty result alerts) |
| 60 | +make verify-ga-parity-local # scripts/verify_ga_postgres_neo_parity.py |
| 61 | +``` |
| 62 | + |
| 63 | +Table sync between databases: `scripts/sync_gap_analysis_table.py`, `scripts/sync_embeddings_table.py`. |
| 64 | + |
| 65 | +### Production DB (Heroku opencreorg) |
| 66 | + |
| 67 | +Use `scripts/db/*` — never raw destructive `psql` against prod without these wrappers. All flows capture and wait for a fresh Heroku backup first (`scripts/db/common.sh`). |
| 68 | + |
| 69 | +```bash |
| 70 | +scripts/db/backup-opencreorg.sh # backup only |
| 71 | +scripts/db/surgery-opencreorg.sh --sql-file path/to/change.sql # targeted SQL |
| 72 | +scripts/db/surgery-opencreorg.sh --sql-file … --destructive # DELETE/DROP/TRUNCATE |
| 73 | +scripts/db/sync-local-to-opencreorg.sh [--table node]… # local → prod sync |
| 74 | +``` |
| 75 | + |
| 76 | +Destructive surgery requires `CONFIRM_DESTRUCTIVE=I_UNDERSTAND_OPENCREORG_PROD_DB_DESTRUCTIVE_ACTION` (exact phrase). Override app: `APP_NAME=opencreorg`. See `production-db-ops-safety.mdc`. |
| 77 | + |
| 78 | +After prod GA cache changes, verify with `make verify-ga-complete-prod` / `make monitor-ga-health-prod`. |
| 79 | + |
| 80 | +### Weekly prod GA & data completeness (Cursor Automation) |
| 81 | + |
| 82 | +Schedule a **Cursor Automation** (not GitHub Actions) to run weekly — prod is cache-only; checks are HTTP + repo scripts only. |
| 83 | + |
| 84 | +| Setting | Value | |
| 85 | +|---------|--------| |
| 86 | +| Trigger | Cron: `0 9 * * 1` (Mondays 09:00) | |
| 87 | +| Repo | `OWASP/OpenCRE`, branch `main` | |
| 88 | +| Tools | None required (cloud agent uses repo checkout) | |
| 89 | + |
| 90 | +**Agent prompt (paste into Automations editor):** |
| 91 | + |
| 92 | +``` |
| 93 | +Weekly OpenCRE prod GA and data completeness for opencreorg. |
| 94 | +
|
| 95 | +1. python3 scripts/monitor_ga_health.py --base-url https://opencre.org --output-json tmp/prod-ga-health.json |
| 96 | +2. python3 scripts/verify_ga_completeness.py --base-url https://opencre.org --output-json tmp/prod-ga-completeness.json |
| 97 | +3. Confirm /rest/v1/standards and /rest/v1/ga_standards return non-empty lists. |
| 98 | +4. If incomplete_pairs > 0 or non-zero exit: list failing pairs/buckets; recommend AGENTS.md Operational scripts (local backfill + scripts/sync_gap_analysis_table.py). Do not compute GA on Heroku or run destructive prod DB ops without explicit approval. |
| 99 | +5. If all pass: report complete/total pairs and standards counts. |
| 100 | +``` |
| 101 | + |
| 102 | +Create via **Cursor → Automations → New** (Agents Window). Do not hand-roll docker/GA setup in the automation prompt. |
| 103 | + |
| 104 | +### Staging bootstrap |
| 105 | + |
| 106 | +`scripts/setup-heroku-staging.sh` — provisions staging from prod + local SQLite; supports `--embeddings`, `--gap_analysis`, or full sync. Requires `PROD_APP`, `STAGING_APP`, `LOCAL_SQLITE_DB`. |
| 107 | + |
| 108 | +### Deploy / migrations |
| 109 | + |
| 110 | +Before deploy or `flask db upgrade`: `make alembic-guardrail` (or `python scripts/check_alembic_revision_guardrail.py`). |
| 111 | + |
| 112 | +## Rule index |
| 113 | + |
| 114 | +| Rule | Purpose | |
| 115 | +|------|---------| |
| 116 | +| `requirements-gate.mdc` | Clarifying questions + requirements template | |
| 117 | +| `complete-ticket.mdc` | Ticket gate for `.md`/`.txt` files; uses `requirements-gate` template + coding standards | |
| 118 | +| `plan-first-workflow.mdc` | Plan Mode before non-trivial edits | |
| 119 | +| `multi-agent-workflow.mdc` | Big changes, approval gates, builder ≠ judge | |
| 120 | +| `verifiable-goals.mdc` | Lint, mypy, test, CI — show evidence | |
| 121 | +| `never-assume.mdc` | No guessing; complete code; minimal scope | |
| 122 | +| `tdd-workflow.mdc` | Test-first for new behavior and importers | |
| 123 | +| `autonomous-workflow.mdc` | Execute after approval; no unsolicited commits | |
| 124 | +| `context-management.mdc` | `/clear`, `@` refs, stale context recovery | |
| 125 | +| `production-db-ops-safety.mdc` | Destructive prod DB confirmation | |
| 126 | +| `alembic-deploy-guardrail.mdc` | Pre-deploy migration guardrail | |
| 127 | + |
| 128 | +## OpenCRE commands |
| 129 | + |
| 130 | +```bash |
| 131 | +make lint # black + frontend prettier |
| 132 | +make mypy # Python typecheck |
| 133 | +make test # Python unittest suite |
| 134 | +make frontend # yarn build (when TS/TSX changed) |
| 135 | +make alembic-guardrail # before deploy/migration ops |
| 136 | +``` |
0 commit comments