Skip to content
reaatech

Files · Anthropic Code Sandbox for GitLab SMB Issue Auto-Resolution

79 (1 binary, 611.7 kB total)attempt 1

README.md·5142 B·markdown
markdown
# Anthropic Code Sandbox for GitLab SMB Issue Auto-Resolution
 
> An AI coding agent that automatically suggests fixes for reported GitLab issues, runs code in a sandbox, verifies the solution, and opens a merge request.
 
A tutorialized reference solution from [reaatech.com](https://reaatech.com), demonstrating how to build production-grade AI systems with the `@reaatech/*` package family.
 
## What it does
 
When a new GitLab issue is filed, a webhook routes it through a mesh of specialist agents:
 
1. **Context Fetcher** — retrieves the issue details and relevant source files from the GitLab repository
2. **Claude Code Generator** — uses Anthropic Claude to analyse the issue and generate a minimal code fix
3. **E2B Sandbox Verifier** — executes the proposed fix in a secure cloud sandbox and validates the output
4. **Confidence Router** — scores the fix quality; only high-confidence fixes proceed to MR creation
5. **Structured Repair** — parses the LLM-generated raw text into a valid Git patch using Zod schema validation
6. **Circuit Breakers** — prevents runaway agent loops and manages retries
 
If the fix passes all checks, the system automatically creates a GitLab merge request.
 
## Architecture
 
```
Webhook → IssueRouter → ContextFetcher → Claude Code Gen → E2B Sandbox → ConfidenceRouter → StructuredRepair → GitLab MR
```
 
## Prerequisites
 
- GitLab instance (CE/EE or gitlab.com) with API access
- Anthropic API key (from https://platform.claude.com/settings/keys)
- E2B API key (from https://e2b.dev/dashboard?tab=keys)
 
## Environment Variables
 
| Variable | Description | Default |
|---|---|---|
| `GITLAB_URL` | GitLab instance URL | `https://gitlab.com` |
| `GITLAB_ACCESS_TOKEN` | GitLab personal access token | — |
| `GITLAB_PROJECT_ID` | GitLab project ID | — |
| `ANTHROPIC_API_KEY` | Anthropic API key | — |
| `ANTHROPIC_MODEL` | Claude model identifier | `claude-sonnet-4-6` |
| `E2B_API_KEY` | E2B sandbox API key | — |
| `CONFIDENCE_ROUTE_THRESHOLD` | Minimum confidence to auto-create MR | `0.8` |
| `CONFIDENCE_FALLBACK_THRESHOLD` | Confidence below which fix is rejected | `0.3` |
| `CIRCUIT_BREAKER_FAILURE_THRESHOLD` | Failures before circuit opens | `5` |
| `CIRCUIT_BREAKER_RECOVERY_MS` | Milliseconds before circuit retries | `30000` |
| `PORT` | Express webhook server port | `3001` |
| `WEBHOOK_SECRET` | Secret for x-gitlab-token validation | — |
| `SANDBOX_TIMEOUT_MS` | Max E2B sandbox execution time | `120000` |
 
## Getting Started
 
```bash
pnpm install
cp .env.example .env
# Edit .env with your keys and project settings
 
# Start the Express webhook server
tsx src/server/index.ts
 
# Start the Next.js development server
pnpm dev
 
# Run tests with coverage
pnpm test
```
 
## Project Structure
 
```
app/
  api/
    projects/route.ts     Next.js API — project config
    mrs/route.ts          Next.js API — merge request review
  dashboard/page.tsx      Admin dashboard
src/
  agent/
    issue-router.ts       Main orchestrator (agent-mesh)
    code-generator.ts     Claude fix generation
    context-fetcher.ts    GitLab issue context
    verifier.ts           E2B sandbox verification
  lib/
    circuit-breaker-config.ts  Circuit breaker setup
    confidence-router.ts       Fix confidence scoring
    structured-repair.ts       LLM output → Git patch
  server/
    app.ts                Express app setup
    index.ts              Express entry point
    routes/gitlab-webhook.ts  Webhook handler
  services/
    gitlab-client.ts      GitLab REST API client
    patch-service.ts      Branch/MR creation
  types/
    gitlab.ts             GitLab domain types
    fix.ts                Fix proposal types
    index.ts              Type re-exports
tests/                    Vitest suite (mirrors src/)
  gitlab-client.test.ts
  circuit-breaker-config.test.ts
  confidence-router.test.ts
  structured-repair.test.ts
  patch-service.test.ts
  code-generator.test.ts
  context-fetcher.test.ts
  verifier.test.ts
  issue-router.test.ts
  gitlab-webhook.test.ts
  server-app.test.ts
  projects-api.test.ts
  mrs-api.test.ts
```
 
## API Reference
 
### POST /gitlab_webhook
 
Receives GitLab issue webhook events. Requires `x-gitlab-token` header matching `WEBHOOK_SECRET`.
 
- `202` — Accepted, processing in background
- `401` — Invalid webhook token
- `400` — Missing or invalid payload
 
### GET /health
 
Express health check endpoint.
 
### GET /api/projects
 
Returns configured GitLab project metadata.
 
### GET /api/mrs
 
Returns proposed merge requests.
 
### POST /api/mrs
 
Approve or reject a merge request.
 
Body: `{ mrIid: number, action: "approve" | "reject" }`
 
## Testing
 
```bash
pnpm test    # vitest run with 90% coverage threshold
```
 
## Packages Used
 
- `@reaatech/agent-mesh` — agent context schemas and types
- `@reaatech/confidence-router` — fix confidence scoring
- `@reaatech/circuit-breaker-agents` — circuit breaker agents
- `@reaatech/structured-repair-core` — LLM output repair
- `@anthropic-ai/sdk` — Claude API
- `@e2b/code-interpreter` — sandboxed code execution
 
## License
 
MIT — see [LICENSE](./LICENSE).