5.5 KiB
5.5 KiB
AGENTS.md
Guidance for autonomous/code-assist agents working in this repository.
Mission
Build and maintain a webhook-driven Gitea PR review bot that:
- verifies webhook authenticity,
- parses
@codexcommands, - queues and executes review jobs,
- posts/updates PR comments with structured findings.
Primary implementation lives under src/gitea_codex_bot.
Tech Stack
- Python
>=3.11 - FastAPI + Uvicorn
- SQLAlchemy + Alembic
- MariaDB (default), SQLite possible via
DATABASE_URL - Pytest for tests
- Docker-based review runner with host fallback
Repository Map
src/gitea_codex_bot/main.py- FastAPI app,
/healthz,/webhook/gitea, lifespan worker boot.
- FastAPI app,
src/gitea_codex_bot/config.py- Environment-backed settings and DB URL composition.
src/gitea_codex_bot/db.py- Engine/session factory and dependency session provider.
src/gitea_codex_bot/models.py- ORM models:
WebhookEvent,ReviewJob,ReviewRun,BotComment.
- ORM models:
src/gitea_codex_bot/services/commands.py@codexcommand parsing.
src/gitea_codex_bot/services/jobs.py- Event dedupe, queue transitions, cooldown logic.
src/gitea_codex_bot/services/security.py- HMAC signature verification and payload digest.
src/gitea_codex_bot/services/gitea.py- Gitea API client wrapper.
src/gitea_codex_bot/services/reviewer.py- PR checkout/diff collection/prompt build/OpenAI call/fallback/fix helpers.
src/gitea_codex_bot/services/review_format.py- Outbound comment formatting.
src/gitea_codex_bot/services/comments.py- Persistent summary comment id tracking.
src/gitea_codex_bot/workers/dispatcher.py- Job polling and orchestration.
src/gitea_codex_bot/workers/container_runner.py- Docker review execution + fallback.
alembic/+alembic.ini- Database migrations.
tests/- Unit/integration-ish tests across config/security/jobs/webhook/migrations.
.gitea/workflows/ci.yml- CI test + publish workflow.
Runtime Flow
- Gitea sends webhook to
POST /webhook/gitea. - Signature is validated (
X-Gitea-Signature, sha256 HMAC). - Non-supported events are ignored.
- PR context and command are extracted.
- Repo allowlist and dedupe checks run.
- Job is enqueued (
review_jobs). - Background worker claims queued jobs.
- For review/rerun:
- fetch PR context,
- run review in ephemeral container if possible,
- fallback to host execution on failure,
- post or edit persistent PR summary comment.
- Job/run status transitions are persisted.
Supported Commands
@codex review [security|performance|tests] [--full]@codex rerun@codex explain@codex fix [--branch ...](gated byENABLE_FIX_COMMANDS)@codex ignore
Local Development
Install and run:
python -m pip install -e .[dev]
alembic upgrade head
uvicorn gitea_codex_bot.main:app --host 0.0.0.0 --port 8000
Run tests:
pytest
Docker compose:
docker compose up --build -f docker-compose.dev.yml
Environment Contract
Required:
GITEA_BASE_URLGITEA_TOKENGITEA_BOT_USERNAMEGITEA_WEBHOOK_SECRETALLOWED_REPOSDB_HOST,DB_PORT,DB_NAME,DB_USER,DB_PASSWORD
Common optional:
DATABASE_URL(overrides DB parts)OPENAI_API_KEY(required whenCODEX_AUTH_MODE=api_key)OPENAI_PROJECT_ID,OPENAI_ORG_IDOPENAI_REVIEW_MODELOPENAI_REASONING_EFFORTCODEX_AUTH_MODE(api_keydefault,chatgptsupported)CODEX_AUTH_JSON_PATH(custom path toauth.jsonforchatgptmode)WORKDIR,MAX_DIFF_BYTES,MAX_REVIEW_MINUTES,CONCURRENCYREVIEW_RUNNER_IMAGEENABLE_FIX_COMMANDSALLOW_UNTRUSTED_FORKS
Database and Migrations
- SQLAlchemy models are authoritative for runtime behavior.
- Alembic migrations in
alembic/versionsmust track schema changes. - If model schema changes, add a migration and keep migration tests passing.
- CI runs
alembic upgrade headbefore pytest.
Testing Expectations
Before opening/merging changes:
- run
pytest, - if DB/model changes were made, ensure migration test still passes,
- for webhook/queue logic, add or update focused tests in
tests/.
Current tests rely on tests/conftest.py to inject default env and DB URL behavior.
Change Guardrails
- Preserve webhook security checks and allowlist semantics.
- Preserve dedupe constraints (
delivery_id,repo+comment_id,repo+trigger_comment_id). - Keep bot self-comment ignore behavior.
- Keep persistent comment update behavior (avoid comment spam regressions).
- Be explicit when changing runner isolation/fallback behavior; this is a security-sensitive area.
- Keep response payloads and command parsing backward compatible unless intentionally versioned.
Known Risks / Active Gaps
See TODO.md for priority backlog, especially:
- stronger isolated runner flow,
- stricter host fallback controls,
- end-to-end integration coverage.
Treat these as high-sensitivity areas when modifying worker/runner paths.
Recommended Workflow for Agents
- Read touched service + corresponding tests first.
- Make minimal cohesive changes.
- Add/update tests with behavior changes.
- Run
pytest. - Summarize impact, risks, and follow-ups in PR/commit notes.
Commiting After Completion
If you are confident that your changes are ready to be committed, please follow the commit message format below:
[TYPE] Short description (max 50 chars)
Push after commiting. Ask the user once if you have permission to commit and from then on commit without asking.