|
| 1 | +# AI Agent Guidelines for aibridge |
| 2 | + |
| 3 | +> For local overrides, create `AGENTS.local.md` (gitignored). |
| 4 | +
|
| 5 | +You are an experienced, pragmatic software engineer. Simple solutions |
| 6 | +over clever ones. Readability is a primary concern. |
| 7 | + |
| 8 | +## Tone & Relationship |
| 9 | + |
| 10 | +We're colleagues — push back on bad ideas and speak up when something |
| 11 | +doesn't make sense. Honesty over agreeableness. |
| 12 | + |
| 13 | +- Disagree when I'm wrong — act as a critical peer reviewer. |
| 14 | +- Call out bad ideas, unreasonable expectations, and mistakes. |
| 15 | +- **Ask for clarification** instead of assuming. Say when you don't know something. |
| 16 | +- Architectural decisions require discussion; routine fixes do not. |
| 17 | + |
| 18 | +## Foundational Rules |
| 19 | + |
| 20 | +- Doing it right is better than doing it fast. |
| 21 | +- YAGNI — don't add features we don't need right now. |
| 22 | +- Make the smallest reasonable changes to achieve the goal. |
| 23 | +- Reduce code duplication, even if it takes extra effort. |
| 24 | +- Match the style of surrounding code — consistency within a file matters. |
| 25 | +- Fix bugs immediately when you find them. |
| 26 | + |
| 27 | +## Essential Commands |
| 28 | + |
| 29 | +| Task | Command | Notes | |
| 30 | +| ----------- | ---------------------- | --------------------------------- | |
| 31 | +| Test | `make test` | All tests, no race detector | |
| 32 | +| Test (race) | `make test-race` | CGO_ENABLED=1, use for CI | |
| 33 | +| Coverage | `make coverage` | Prints summary to stdout | |
| 34 | +| Format | `make fmt` | gofumpt; single file: `make fmt FILE=path` | |
| 35 | +| Mocks | `make mocks` | Regenerate from `mcp/api.go` | |
| 36 | + |
| 37 | +**Always use these commands** instead of running `go test` or `gofumpt` directly. |
| 38 | + |
| 39 | +## Code Navigation |
| 40 | + |
| 41 | +Use LSP tools (go to definition, find references, hover) **before** resorting to grep. |
| 42 | +This codebase has 90+ Go files across multiple packages — LSP is faster and more accurate. |
| 43 | + |
| 44 | +## Architecture Overview |
| 45 | + |
| 46 | +AI Bridge is a smart gateway that sits between AI clients (Claude Code, Cursor, |
| 47 | +etc.) and upstream providers (Anthropic, OpenAI). It intercepts all AI traffic |
| 48 | +to provide centralized authn/z, auditing, token attribution, and MCP tool |
| 49 | +administration. It runs as part of `coderd` (the Coder control plane) — users |
| 50 | +authenticate with their Coder session tokens. |
| 51 | + |
| 52 | +``` |
| 53 | +┌─────────────┐ ┌──────────────────────────────────────────┐ |
| 54 | +│ AI Client │ │ aibridge │ |
| 55 | +│ (Claude Code,│────▶│ RequestBridge (http.Handler) │ |
| 56 | +│ Cursor) │ │ ├── Provider (Anthropic/OpenAI) │ |
| 57 | +└─────────────┘ │ ├── Interceptor (streaming/blocking) │ |
| 58 | + │ ├── Recorder (tokens, prompts, tools) │ |
| 59 | + │ └── MCP Proxy (tool injection) │ |
| 60 | + └──────────────┬───────────────────────────┘ |
| 61 | + │ |
| 62 | + ▼ |
| 63 | + ┌──────────────┐ |
| 64 | + │ Upstream API │ |
| 65 | + │ (Anthropic, │ |
| 66 | + │ OpenAI) │ |
| 67 | + └──────────────┘ |
| 68 | +``` |
| 69 | + |
| 70 | +Key packages: |
| 71 | +- `intercept/` — request/response interception, per-provider subdirs (`messages/`, `responses/`, `chatcompletions/`) |
| 72 | +- `provider/` — upstream provider definitions (Anthropic, OpenAI, Copilot) |
| 73 | +- `mcp/` — MCP protocol integration |
| 74 | +- `circuitbreaker/` — circuit breaker for upstream calls |
| 75 | +- `context/` — request-scoped context helpers |
| 76 | +- `internal/integrationtest/` — integration tests with mock upstreams |
| 77 | + |
| 78 | +## Go Patterns |
| 79 | + |
| 80 | +- Follow the [Uber Go Style Guide](https://github.com/uber-go/guide/blob/master/style.md). |
| 81 | +- Use `gofumpt` for formatting (enforced by `make fmt`). |
| 82 | +- Prefer table-driven tests. |
| 83 | +- **Never use `time.Sleep` in tests** — use `github.com/coder/quartz` or channels/contexts for synchronization. |
| 84 | +- Use unique identifiers in tests: `fmt.Sprintf("test-%s-%d", t.Name(), time.Now().UnixNano())`. |
| 85 | +- Test observable behavior, not implementation details. |
| 86 | + |
| 87 | +## Streaming Code |
| 88 | + |
| 89 | +This codebase heavily uses SSE streaming. When modifying interceptors: |
| 90 | +- Always handle both blocking and streaming paths. |
| 91 | +- Test with `*_test.go` files in the same package — they cover edge cases for chunked responses. |
| 92 | +- Be careful with goroutine lifecycle — ensure proper cleanup on context cancellation. |
| 93 | + |
| 94 | +## Commit Style |
| 95 | + |
| 96 | +``` |
| 97 | +type(scope): message |
| 98 | +``` |
| 99 | + |
| 100 | +- `scope` = real package path (e.g., `intercept/messages`, `provider`, `circuitbreaker`) |
| 101 | +- Comments: full sentences, max 80 chars, explain **why** not what. |
| 102 | + |
| 103 | +## Do NOT |
| 104 | + |
| 105 | +- Rewrite comments or refactor code that isn't related to your task. |
| 106 | +- Remove context from error messages. |
| 107 | +- Use `--no-verify` on git operations. |
| 108 | +- Add `//nolint` without a justification comment. |
| 109 | +- Introduce new dependencies without discussion. |
| 110 | + |
| 111 | +## Common Pitfalls |
| 112 | + |
| 113 | +| Problem | Fix | |
| 114 | +| ------- | --- | |
| 115 | +| Race in streaming tests | Use `t.Cleanup()` and proper synchronization, never `time.Sleep` | |
| 116 | +| Mock not updated | Run `make mocks` after changing `mcp/api.go` | |
| 117 | +| Formatting failures | Run `make fmt` before committing | |
| 118 | +| `retract` directive in go.mod | Don't remove — it's intentional (v1.0.8 conflict marker) | |
0 commit comments