|
| 1 | +# Code Agent System Design v0.1 |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +Code Agent is an automated code generation system developed in Go that receives `/code` commands through GitHub Webhooks, automatically generates code for Issues, and creates Pull Requests. |
| 6 | + |
| 7 | +## System Architecture |
| 8 | + |
| 9 | +``` |
| 10 | +GitHub Issue (/code) → Webhook → Code Agent → Temporary Repository → Claude Code Container → PR |
| 11 | +``` |
| 12 | + |
| 13 | +## Core Workflow |
| 14 | + |
| 15 | +### 1. Webhook Reception and Parsing |
| 16 | + |
| 17 | +- Listen for GitHub Issue comment events |
| 18 | +- Detect `/code` command triggers |
| 19 | +- Parse Issue content and context information |
| 20 | + |
| 21 | +### 2. Temporary Repository Preparation |
| 22 | + |
| 23 | +- Clone target repository to temporary directory |
| 24 | +- Create new branch (e.g., `Code-agent/issue-123`) |
| 25 | +- Generate initial commit: "Code-agent has received information, preparing to implement target issue" |
| 26 | +- Push branch and create PR |
| 27 | + |
| 28 | +### 3. Claude Code Execution |
| 29 | + |
| 30 | +- Mount temporary directory to Claude Code container |
| 31 | +- Pass Issue description and requirements as prompts |
| 32 | +- Execute automated code generation and modification |
| 33 | +- Implement functionality based on Issue requirements |
| 34 | + |
| 35 | +### 4. Result Processing |
| 36 | + |
| 37 | +- Parse Claude Code output and generated code |
| 38 | +- Commit changes with descriptive messages |
| 39 | +- Push to remote branch |
| 40 | +- Update PR description with implementation details |
| 41 | + |
| 42 | +## Technical Implementation |
| 43 | + |
| 44 | +### Directory Structure |
| 45 | + |
| 46 | +``` |
| 47 | +/tmp/codeagent/ |
| 48 | +├── repos/ |
| 49 | +│ └── owner-repo-issue-123/ |
| 50 | +│ ├── .git/ |
| 51 | +│ └── [repository files] |
| 52 | +└── sessions/ |
| 53 | + └── claude-session-123/ |
| 54 | + └── [Claude execution context] |
| 55 | +``` |
| 56 | + |
| 57 | +### Key Components |
| 58 | + |
| 59 | +#### 1. Webhook Handler |
| 60 | +- **File**: `internal/webhook/handler.go` |
| 61 | +- **Functionality**: Process GitHub webhook events |
| 62 | +- **Trigger Detection**: Parse comments for `/code` commands |
| 63 | + |
| 64 | +#### 2. Workspace Manager |
| 65 | +- **File**: `internal/workspace/manager.go` |
| 66 | +- **Functionality**: Manage temporary Git repositories |
| 67 | +- **Operations**: Clone, branch creation, cleanup |
| 68 | + |
| 69 | +#### 3. GitHub Client |
| 70 | +- **File**: `internal/github/client.go` |
| 71 | +- **Functionality**: GitHub API interactions |
| 72 | +- **Operations**: PR creation, commit pushing, status updates |
| 73 | + |
| 74 | +#### 4. Code Execution Engine |
| 75 | +- **File**: `internal/code/claude.go` |
| 76 | +- **Functionality**: Claude Code container management |
| 77 | +- **Operations**: Container creation, prompt execution, result parsing |
| 78 | + |
| 79 | +## Configuration |
| 80 | + |
| 81 | +### Environment Variables |
| 82 | + |
| 83 | +```bash |
| 84 | +# GitHub Configuration |
| 85 | +GITHUB_TOKEN=your_github_token |
| 86 | +WEBHOOK_SECRET=your_webhook_secret |
| 87 | + |
| 88 | +# Claude Configuration |
| 89 | +CLAUDE_API_KEY=your_claude_api_key |
| 90 | + |
| 91 | +# System Configuration |
| 92 | +WORKSPACE_PATH=/tmp/codeagent |
| 93 | +``` |
| 94 | + |
| 95 | +### Config File (config.yaml) |
| 96 | + |
| 97 | +```yaml |
| 98 | +github: |
| 99 | + token: "${GITHUB_TOKEN}" |
| 100 | + webhook_secret: "${WEBHOOK_SECRET}" |
| 101 | + |
| 102 | +code: |
| 103 | + provider: "claude" |
| 104 | + claude: |
| 105 | + api_key: "${CLAUDE_API_KEY}" |
| 106 | + model: "claude-3-sonnet-20240229" |
| 107 | + |
| 108 | +workspace: |
| 109 | + base_path: "/tmp/codeagent" |
| 110 | + cleanup_after_hours: 24 |
| 111 | + |
| 112 | +server: |
| 113 | + port: 8888 |
| 114 | + host: "0.0.0.0" |
| 115 | +``` |
| 116 | +
|
| 117 | +## Key Features |
| 118 | +
|
| 119 | +### 1. Automated Issue Processing |
| 120 | +- Parse Issue requirements automatically |
| 121 | +- Generate appropriate code solutions |
| 122 | +- Create PR with implementation |
| 123 | +
|
| 124 | +### 2. Git Repository Management |
| 125 | +- Temporary workspace isolation |
| 126 | +- Automatic branch management |
| 127 | +- Clean commit history |
| 128 | +
|
| 129 | +### 3. Claude Code Integration |
| 130 | +- Container-based execution |
| 131 | +- Context-aware code generation |
| 132 | +- Error handling and retry logic |
| 133 | +
|
| 134 | +### 4. GitHub Integration |
| 135 | +- Real-time webhook processing |
| 136 | +- PR status updates |
| 137 | +- Automatic cleanup |
| 138 | +
|
| 139 | +## Error Handling |
| 140 | +
|
| 141 | +### Common Scenarios |
| 142 | +
|
| 143 | +1. **GitHub API Rate Limits** |
| 144 | + - Implement exponential backoff |
| 145 | + - Queue requests when limit exceeded |
| 146 | + - Graceful degradation |
| 147 | +
|
| 148 | +2. **Claude Code Failures** |
| 149 | + - Retry mechanism with different prompts |
| 150 | + - Fallback to simpler implementations |
| 151 | + - Error reporting in PR comments |
| 152 | +
|
| 153 | +3. **Git Operation Failures** |
| 154 | + - Repository state validation |
| 155 | + - Conflict resolution strategies |
| 156 | + - Workspace cleanup on failure |
| 157 | +
|
| 158 | +4. **Resource Management** |
| 159 | + - Disk space monitoring |
| 160 | + - Process timeout handling |
| 161 | + - Memory usage optimization |
| 162 | +
|
| 163 | +## Security Considerations |
| 164 | +
|
| 165 | +### 1. Webhook Security |
| 166 | +- Signature verification for all incoming requests |
| 167 | +- IP allowlisting for GitHub webhooks |
| 168 | +- Rate limiting to prevent abuse |
| 169 | +
|
| 170 | +### 2. Code Execution Security |
| 171 | +- Containerized execution environment |
| 172 | +- Resource limits (CPU, memory, disk) |
| 173 | +- Network isolation |
| 174 | +
|
| 175 | +### 3. Repository Access |
| 176 | +- Token scope limitation |
| 177 | +- Read-only access where possible |
| 178 | +- Audit logging for all operations |
| 179 | +
|
| 180 | +## Deployment |
| 181 | +
|
| 182 | +### Docker Deployment |
| 183 | +
|
| 184 | +```dockerfile |
| 185 | +FROM golang:1.21-alpine AS builder |
| 186 | +WORKDIR /app |
| 187 | +COPY . . |
| 188 | +RUN go build -o codeagent ./cmd/codeagent |
| 189 | + |
| 190 | +FROM alpine:latest |
| 191 | +RUN apk --no-cache add ca-certificates git |
| 192 | +WORKDIR /root/ |
| 193 | +COPY --from=builder /app/codeagent . |
| 194 | +CMD ["./codeagent"] |
| 195 | +``` |
| 196 | + |
| 197 | +### Kubernetes Deployment |
| 198 | + |
| 199 | +```yaml |
| 200 | +apiVersion: apps/v1 |
| 201 | +kind: Deployment |
| 202 | +metadata: |
| 203 | + name: codeagent |
| 204 | +spec: |
| 205 | + replicas: 2 |
| 206 | + selector: |
| 207 | + matchLabels: |
| 208 | + app: codeagent |
| 209 | + template: |
| 210 | + metadata: |
| 211 | + labels: |
| 212 | + app: codeagent |
| 213 | + spec: |
| 214 | + containers: |
| 215 | + - name: codeagent |
| 216 | + image: codeagent:latest |
| 217 | + ports: |
| 218 | + - containerPort: 8888 |
| 219 | + env: |
| 220 | + - name: GITHUB_TOKEN |
| 221 | + valueFrom: |
| 222 | + secretKeyRef: |
| 223 | + name: github-secrets |
| 224 | + key: token |
| 225 | + volumeMounts: |
| 226 | + - name: workspace |
| 227 | + mountPath: /tmp/codeagent |
| 228 | + volumes: |
| 229 | + - name: workspace |
| 230 | + emptyDir: {} |
| 231 | +``` |
| 232 | +
|
| 233 | +## Future Enhancements |
| 234 | +
|
| 235 | +### v0.2 Planned Features |
| 236 | +- Multi-language support (Python, JavaScript, etc.) |
| 237 | +- Advanced prompt engineering |
| 238 | +- Code quality validation |
| 239 | +- Integration testing automation |
| 240 | +
|
| 241 | +### v0.3 Roadmap |
| 242 | +- Multiple AI provider support (OpenAI, Gemini) |
| 243 | +- Custom code templates |
| 244 | +- Advanced context understanding |
| 245 | +- Performance optimizations |
| 246 | +
|
| 247 | +## Monitoring and Logging |
| 248 | +
|
| 249 | +### Metrics Collection |
| 250 | +- Request processing time |
| 251 | +- Success/failure rates |
| 252 | +- Resource usage statistics |
| 253 | +- GitHub API usage tracking |
| 254 | +
|
| 255 | +### Log Levels |
| 256 | +- **DEBUG**: Detailed execution flow |
| 257 | +- **INFO**: Key operation milestones |
| 258 | +- **WARN**: Recoverable errors |
| 259 | +- **ERROR**: Critical failures requiring attention |
| 260 | +
|
| 261 | +### Health Checks |
| 262 | +- `/health` endpoint for load balancer |
| 263 | +- Database connectivity checks |
| 264 | +- External service availability |
| 265 | +- Resource utilization monitoring |
0 commit comments