meritocrab 0.1.4

A reputation system for open source repositories using LLM-based contribution evaluation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# Meritocrab

[![crates.io](https://img.shields.io/crates/v/meritocrab.svg)](https://crates.io/crates/meritocrab)
[![CI](https://github.com/hydai/meritocrab/actions/workflows/ci.yml/badge.svg)](https://github.com/hydai/meritocrab/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

A reputation/credit system for open source repositories that grades contributors based on contribution quality using LLM evaluation. The system gates PR submissions behind a credit threshold and provides tools for maintainers to manage contributor reputation.

## Features

- **Automated Credit Scoring**: LLM-powered evaluation of PRs and comments
- **PR Gating**: Contributors below credit threshold cannot open PRs
- **Shadow Blacklist**: Graceful handling of bad actors without alerting them
- **Maintainer Dashboard**: Web interface for reviewing evaluations and managing contributors
- **GitHub Integration**: Seamless integration via GitHub Apps and webhooks
- **Flexible Configuration**: Per-repository custom scoring via `.meritocrab.toml`
- **Role-Based Bypass**: Maintainers and collaborators exempt from checks
- **Audit Trail**: Complete history of all credit changes with maintainer overrides

## Installation

### As a library

```toml
# Use the facade crate for everything
[dependencies]
meritocrab = "0.1"

# Or depend on individual crates
[dependencies]
meritocrab-core = "0.1"
meritocrab-llm = "0.1"
```

### As a server

```bash
cargo install meritocrab-server
```

## Architecture

```
GitHub Webhooks (PR, Comment, Review)
        |
        v
+---------------------+
|  Axum HTTP Server    |  <- HMAC-SHA256 verification
|  /webhooks/github    |
+--------+------------+
         |
    +----+----+
    v         v
 GitHub    LLM Evaluator     <- trait-based (Claude, OpenAI, Mock)
  API       (async task)
(octocrab)    |
    |         v
    |    Credit Engine       <- pure functions, no I/O
    |         |
    +----+----+
         v
   Database (sqlx)           <- SQLite dev / PostgreSQL prod
         ^
         |
   Maintainer API            <- Admin endpoints + GitHub OAuth
```

## Quick Start

### Prerequisites

- Rust 1.85 or later
- Docker and Docker Compose (for containerized deployment)
- GitHub App with webhook and API access

### Local Development

1. **Clone the repository**:
   ```bash
   git clone https://github.com/hydai/meritocrab.git
   cd meritocrab
   ```

2. **Configure the application**:
   ```bash
   cp config.example.toml config.toml
   # Edit config.toml with your settings
   ```

3. **Set up GitHub App**:
   - Create a GitHub App at https://github.com/settings/apps
   - Set webhook URL to `https://yourdomain.com/webhooks/github`
   - Enable permissions: Repository contents (read), Pull requests (read/write), Issues (read/write)
   - Subscribe to events: Pull request, Issue comment, Pull request review
   - Download private key and save as `private-key.pem`

4. **Run migrations and start server**:
   ```bash
   cargo run --release
   ```

### Docker Deployment

1. **Configure environment**:
   ```bash
   cp .env.example .env
   # Edit .env with your GitHub App credentials
   ```

2. **Start services**:
   ```bash
   docker-compose up -d
   ```

3. **Verify health**:
   ```bash
   curl http://localhost:3000/health
   ```

## Configuration

### Server Configuration (`config.toml`)

```toml
[server]
host = "0.0.0.0"
port = 3000

[database]
url = "postgres://user:password@localhost/meritocrab"
max_connections = 10

[github]
app_id = 123456
installation_id = 7654321
webhook_secret = "your-webhook-secret"
private_key_path = "private-key.pem"
oauth_client_id = "your-oauth-client-id"
oauth_client_secret = "your-oauth-client-secret"
oauth_redirect_url = "http://localhost:3000/auth/callback"

[llm]
provider = "claude"  # claude, openai, or mock
api_key = "your-api-key"
model = "claude-3-5-sonnet-20241022"

[credit]
starting = 100
pr_threshold = 50
blacklist_threshold = 0

max_concurrent_llm_evals = 5
```

### Per-Repository Configuration (`.meritocrab.toml`)

Place this file in the root of your repository to customize scoring:

```toml
# Starting credit for new contributors
starting_credit = 100

# Minimum credit required to open PRs
pr_threshold = 50

# Credit level that triggers auto-blacklist
blacklist_threshold = 0

# PR opened scoring deltas
[pr_opened]
spam = -25
low = -5
acceptable = 5
high = 15

# Comment scoring deltas
[comment]
spam = -10
low = -2
acceptable = 1
high = 3

# PR merged bonus (no LLM evaluation)
[pr_merged]
bonus = 20

# Review submitted bonus (no LLM evaluation)
[review_submitted]
bonus = 5
```

## API Endpoints

### Public Endpoints

- `GET /health` - Health check with server status
- `POST /webhooks/github` - GitHub webhook receiver (HMAC verified)

### Authentication Endpoints

- `GET /auth/github` - Initiate GitHub OAuth flow
- `GET /auth/callback` - OAuth callback handler
- `POST /auth/logout` - Logout current session

### Admin API (Requires Maintainer Role)

- `GET /api/repos/:owner/:repo/evaluations?status=pending` - List pending evaluations
- `POST /api/repos/:owner/:repo/evaluations/:id/approve` - Approve evaluation
- `POST /api/repos/:owner/:repo/evaluations/:id/override` - Override evaluation with custom delta
- `GET /api/repos/:owner/:repo/contributors` - List all contributors
- `POST /api/repos/:owner/:repo/contributors/:user_id/adjust` - Manually adjust credit
- `POST /api/repos/:owner/:repo/contributors/:user_id/blacklist` - Toggle blacklist status
- `GET /api/repos/:owner/:repo/events` - View credit event history

## Maintainer Commands

Maintainers can use special comments in GitHub issues/PRs:

### Check Credit

```
/credit check @12345
```

Returns credit score, role, blacklist status, and last 5 credit events.

### Override Credit

```
/credit override @12345 +20 "Excellent contribution with thorough tests"
```

Adjusts credit by specified delta with reason. Auto-blacklists if credit drops to/below threshold.

### Manual Blacklist

```
/credit blacklist @12345
```

Immediately blacklists contributor. Future PRs will be shadow-closed.

**Note**: Commands currently require numeric GitHub user ID instead of username.

## Credit Scoring

| Event | Spam | Low Quality | Acceptable | High Quality |
|-------|------|-------------|------------|--------------|
| PR opened | -25 | -5 | +5 | +15 |
| Comment | -10 | -2 | +1 | +3 |
| PR merged |||| +20 |
| Review submitted |||| +5 |

### Workflow

1. **PR Opened**: Check credit >= threshold -> If insufficient, close PR with message
2. **LLM Evaluation**: Async evaluation of content quality
3. **Credit Adjustment**: Apply delta if confidence >= 0.85, else queue for maintainer review
4. **Auto-Blacklist**: If credit <= blacklist_threshold, auto-blacklist contributor
5. **Shadow Enforcement**: Blacklisted PRs closed after randomized delay (30-120s)

## Development

### Running Tests

```bash
# All tests
cargo test --all

# Specific crate
cargo test -p meritocrab-api

# With output
cargo test -- --nocapture
```

### Project Structure

```
meritocrab/
├── Cargo.toml                 # Workspace root + meritocrab facade crate
├── src/lib.rs                 # Facade: re-exports all sub-crates
├── LICENSE                    # MIT
├── Dockerfile                 # Multi-stage production build
├── docker-compose.yml         # Server + PostgreSQL
├── config.example.toml        # Server configuration template
├── .meritocrab.toml.example   # Per-repo config example
└── crates/
    ├── meritocrab-core/       # Credit scoring (pure functions, no I/O)
    ├── meritocrab-github/     # GitHub API + webhook verification
    ├── meritocrab-llm/        # LLM evaluator trait + implementations
    ├── meritocrab-db/         # Database layer + migrations
    ├── meritocrab-api/        # HTTP handlers + middleware
    └── meritocrab-server/     # Entry point, HTTP server setup
```

### Crate Dependency Graph

```
meritocrab-core       meritocrab-github     (leaf crates, no internal deps)
      |                     |
      +-------+-------+     |
              |       |     |
       meritocrab-db  meritocrab-llm
              |       |     |
              +---+---+-----+
                  |
           meritocrab-api
                  |
           meritocrab-server (binary)
```

### Adding a New LLM Provider

1. Implement `meritocrab_llm::LlmEvaluator` trait
2. Add configuration in `meritocrab_llm::create_evaluator()`
3. Update config example with new provider option

## Production Deployment

### Docker

The provided `Dockerfile` uses multi-stage builds for optimal image size:

```bash
# Build
docker build -t meritocrab:latest .

# Run
docker run -p 3000:3000 \
  -e DATABASE_URL=postgres://... \
  -e GITHUB_APP_ID=123456 \
  -v /path/to/private-key.pem:/app/private-key.pem:ro \
  meritocrab:latest
```

### Health Checks

The `/health` endpoint returns comprehensive status:

```json
{
  "status": "healthy",
  "version": "0.1.0",
  "uptime_seconds": 3600,
  "database": {
    "connected": true,
    "driver": "postgres"
  },
  "llm_provider": {
    "provider": "claude",
    "available": true
  }
}
```

### Graceful Shutdown

The server handles SIGTERM gracefully:

1. Stop accepting new requests
2. Complete in-flight webhook processing
3. Flush pending LLM evaluations to database
4. Close database connections

## Monitoring

### Logs

Structured logging with `tracing`:

- Request IDs for correlation
- Webhook events with type and contributor
- LLM evaluations with timing and classification
- Errors with full context

Configure log level via `RUST_LOG` environment variable:

```bash
RUST_LOG=info cargo run
RUST_LOG=debug,sqlx=warn cargo run  # Debug app, warn for sqlx
```

## Troubleshooting

### "No drivers installed" error

Ensure `sqlx::any::install_default_drivers()` is called before creating database pools.

### Webhook signature verification fails

Verify:
1. Webhook secret in GitHub App matches `GITHUB_WEBHOOK_SECRET`
2. Webhook URL is correctly configured
3. Content-Type is `application/json`

### LLM evaluation timeouts

Increase semaphore limit in config:

```toml
max_concurrent_llm_evals = 10  # Default: 5
```

## Security Considerations

- **Webhook Verification**: All webhooks verified with HMAC-SHA256
- **Authentication**: Admin endpoints protected by GitHub OAuth
- **Secrets**: Never commit `.env`, `config.toml`, or private keys to git
- **Database**: Use strong passwords and restrict network access
- **Shadow Blacklist**: Randomized delays prevent detection of blacklist status

## Releases

Releases are automated via [Knope](https://knope.tech/) and GitHub Actions:

1. Push conventional commits to `master` (e.g., `feat: add rate limiting`, `fix: handle timeout`)
2. The **prepare-release** workflow creates a release PR with version bumps and changelog
3. Merge the release PR to publish all 7 crates to crates.io and create a GitHub release

All crate versions stay in sync. Publishing uses [Trusted Publishing](https://doc.rust-lang.org/cargo/reference/registry-authentication.html#trusted-publishing) (OIDC) — no API tokens required.

## Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes with tests
4. Run `cargo fmt` and `cargo clippy`
5. Use [Conventional Commits]https://www.conventionalcommits.org/ for commit messages
6. Submit a pull request — CI will run fmt, clippy, and tests automatically

## License

MIT License - See [LICENSE](LICENSE) file for details.

## Support

- GitHub Issues: https://github.com/hydai/meritocrab/issues

## Acknowledgments

Built with:
- [Axum]https://github.com/tokio-rs/axum - Web framework
- [SQLx]https://github.com/launchbadge/sqlx - Async SQL toolkit
- [Octocrab]https://github.com/XAMPPRocky/octocrab - GitHub API client
- [Anthropic Claude]https://www.anthropic.com/ / [OpenAI]https://openai.com/ - LLM providers