Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
803eb27
Add unit test harness: Glo* stubs and component init helpers
renecannao Mar 21, 2026
43c04fb
Add unit test Makefile and smoke test
renecannao Mar 21, 2026
77ae235
Integrate unit tests into TAP build system
renecannao Mar 21, 2026
a538532
Fix tap.cpp compilation on macOS: add ulong typedef
renecannao Mar 21, 2026
b187f9a
Address review feedback on test harness (PR #5482)
renecannao Mar 21, 2026
96387be
Add Query Cache unit tests (Phase 2.3, #5475)
renecannao Mar 21, 2026
2ae866b
Add query_cache_unit-t to build and init Prometheus registry
renecannao Mar 21, 2026
bcba803
Address review feedback on query cache unit tests (PR #5486)
renecannao Mar 21, 2026
17f9a0f
Merge pull request #5486 from sysown/v3.0-5475
renecannao Mar 21, 2026
76f1f2f
Add Query Processor rule management unit tests (Phase 2.4, #5476)
renecannao Mar 21, 2026
c57bd36
Add query_processor_unit-t to build and fix QP initialization
renecannao Mar 21, 2026
b25cf6d
Address review feedback on query processor unit tests (PR #5487)
renecannao Mar 21, 2026
8901604
Merge pull request #5487 from sysown/v3.0-5476
renecannao Mar 21, 2026
a7424f0
Add protocol encoding/decoding and utility unit tests (Phase 2.5, #5477)
renecannao Mar 21, 2026
e6e8590
Add protocol_unit-t to unit test Makefile
renecannao Mar 21, 2026
045ff26
Address review feedback on protocol unit tests (PR #5488)
renecannao Mar 21, 2026
87e3280
Merge pull request #5488 from sysown/v3.0-5477
renecannao Mar 22, 2026
2e52def
Add MySQL/PgSQL Authentication unit tests (Phase 2.2, #5474)
renecannao Mar 21, 2026
a968b04
Add auth_unit-t to unit test Makefile
renecannao Mar 21, 2026
051435f
Address review feedback on auth unit tests (PR #5485)
renecannao Mar 21, 2026
f3b9eda
Merge pull request #5485 from sysown/v3.0-5474
renecannao Mar 22, 2026
5e8b1e3
Extract connection pool decision logic into pure functions (MySQL + P…
Copilot Mar 21, 2026
37ce052
Fix connection pool extraction: separate header, proper unit tests
renecannao Mar 22, 2026
7a0026f
Address review feedback on connection pool unit tests (PR #5502)
renecannao Mar 22, 2026
60a7718
Merge pull request #5502 from sysown/v3.0-5489
renecannao Mar 22, 2026
54a2af3
refactor: extract query rule matching helper
Copilot Mar 21, 2026
0477a5b
test: fix wildcard prefix setup
Copilot Mar 21, 2026
aae1ad4
Fix rule matching extraction: proper unit tests with harness
renecannao Mar 22, 2026
666eb4a
Address review feedback on rule matching unit tests (PR #5503)
renecannao Mar 22, 2026
c0ab3bd
Merge pull request #5503 from sysown/v3.0-5490
renecannao Mar 22, 2026
bf250c6
Add agent guidelines documentation
renecannao Mar 22, 2026
c54e6e1
Expand task assignment template with learnings from CodeRabbit plans
renecannao Mar 22, 2026
f772c10
Add HostGroups Manager unit tests (Phase 2.6, #5478)
renecannao Mar 22, 2026
e4972ac
Fix hostgroups_unit-t hang: do not call MyHGM->init()
renecannao Mar 22, 2026
1549370
Address review feedback on hostgroups unit tests (PR #5506)
renecannao Mar 22, 2026
b0889e1
Merge pull request #5506 from sysown/v3.0-5478
renecannao Mar 22, 2026
59654e4
Extract monitor health decision logic into pure functions (Phase 3.3,…
renecannao Mar 22, 2026
24db561
Add monitor health unit tests (Phase 3.3, #5491)
renecannao Mar 22, 2026
99980ca
Address review feedback on monitor health (PR #5507)
renecannao Mar 22, 2026
f7f93e3
Merge pull request #5507 from sysown/v3.0-5491
renecannao Mar 22, 2026
dba1294
Extract server selection algorithm into pure functions (Phase 3.4, #5…
renecannao Mar 22, 2026
7697d4f
Add server selection unit tests (Phase 3.4, #5492)
renecannao Mar 22, 2026
7e49c41
Address review feedback on server selection (PR #5508)
renecannao Mar 22, 2026
586fb73
Move git workflow to top of task template — agents skip buried instru…
renecannao Mar 22, 2026
aaa02bb
Extract hostgroup routing decision logic (Phase 3.5, #5493)
renecannao Mar 22, 2026
98249bc
Add hostgroup routing unit tests + Makefile pattern rule (Phase 3.5, …
renecannao Mar 22, 2026
3f02816
Extract transaction state tracking logic (Phase 3.8, #5496)
renecannao Mar 22, 2026
8d12bef
Add transaction state unit tests + pattern rule (Phase 3.8, #5496)
renecannao Mar 22, 2026
3ed5e88
Address review feedback on hostgroup routing tests (PR #5509)
renecannao Mar 22, 2026
667f0f0
Address review feedback on transaction state tests (PR #5510)
renecannao Mar 22, 2026
139051d
Merge pull request #5509 from sysown/v3.0-5493
renecannao Mar 22, 2026
7b06515
Merge branch 'v3.0-5473' into v3.0-5496
renecannao Mar 22, 2026
1354b79
Merge pull request #5510 from sysown/v3.0-5496
renecannao Mar 22, 2026
5148989
Extract PgSQL CommandComplete tag parser for unit testing (#5499)
renecannao Mar 22, 2026
f872beb
Add PgSQL CommandComplete parser unit tests (#5499)
renecannao Mar 22, 2026
5c227a3
Extract MySQL error classification logic (Phase 3.7, #5495)
renecannao Mar 22, 2026
56eb121
Add MySQL error classifier unit tests (Phase 3.7, #5495)
renecannao Mar 22, 2026
ead2331
Extract backend variable sync decisions (Phase 3.6, #5494)
renecannao Mar 22, 2026
bc4251d
Add backend sync decision unit tests (Phase 3.6, #5494)
renecannao Mar 22, 2026
7840074
Extract PgSQL monitor health decisions (Phase 3.9, #5497)
renecannao Mar 22, 2026
fe49380
Add PgSQL monitor unit tests (Phase 3.9, #5497)
renecannao Mar 22, 2026
e82bcb3
Extract PgSQL error classification logic (Phase 3.10, #5498)
renecannao Mar 22, 2026
a066763
Add PgSQL error classifier unit tests (Phase 3.10, #5498)
renecannao Mar 22, 2026
f07cd83
Extract MySQL protocol utilities for FFTO unit testing (#5499)
renecannao Mar 22, 2026
1274fb7
Add comprehensive FFTO protocol unit tests — MySQL + PgSQL (#5499)
renecannao Mar 22, 2026
c40eed9
Merge branch 'v3.0-5473' into v3.0-5492
renecannao Mar 22, 2026
b51f50b
Address review: fix NULL asymmetry in backend sync decisions (PR #5511)
renecannao Mar 22, 2026
81f77d9
Merge pull request #5508 from sysown/v3.0-5492
renecannao Mar 22, 2026
468adb4
Address review on MySQL error classifier (PR #5512)
renecannao Mar 22, 2026
f5ace60
Address review on PgSQL error classifier (PR #5514)
renecannao Mar 22, 2026
cffd85f
Merge pull request #5511 from sysown/v3.0-5494
renecannao Mar 22, 2026
9bccd2d
Merge branch 'v3.0-5473' into v3.0-5495
renecannao Mar 22, 2026
d232834
Merge pull request #5512 from sysown/v3.0-5495
renecannao Mar 22, 2026
8d7459e
Merge branch 'v3.0-5473' into v3.0-5497
renecannao Mar 22, 2026
ec5a9f9
Merge pull request #5513 from sysown/v3.0-5497
renecannao Mar 22, 2026
d872a39
Merge branch 'v3.0-5473' into v3.0-5498
renecannao Mar 22, 2026
f1f2eaa
Merge pull request #5514 from sysown/v3.0-5498
renecannao Mar 22, 2026
ea16ebb
Merge branch 'v3.0-5473' into v3.0-5499
renecannao Mar 22, 2026
660ed5a
Merge pull request #5515 from sysown/v3.0-5499
renecannao Mar 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

ProxySQL is a high-performance, protocol-aware proxy for MySQL (and forks like MariaDB, Percona Server) and PostgreSQL. Written in C++17, it provides connection pooling, query routing, caching, and monitoring. Licensed under GPL.

## Build Commands

The build system is GNU Make-based with a three-stage pipeline: `deps` → `lib` → `src`.

```bash
# Full release build (auto-detects -j based on nproc/hw.ncpu)
make

# Debug build (-O0, -ggdb, -DDEBUG)
make debug

# Build with ASAN (requires no jemalloc)
NOJEMALLOC=1 WITHASAN=1 make build_deps_debug && make debug && make build_tap_test_debug

# Build TAP tests (requires proxysql binary built first)
make build_tap_tests # release
make build_tap_test_debug # debug

# Clean
make clean # clean src/lib
make cleanall # clean everything including deps

# Build packages
make packages
```

### Feature Tiers

The same codebase produces three product tiers via feature flags:

| Tier | Flag | Version | Adds |
|------|------|---------|------|
| Stable | (default) | v3.0.x | Core proxy |
| Innovative | `PROXYSQL31=1` | v3.1.x | FFTO, TSDB |
| AI/MCP | `PROXYSQLGENAI=1` | v4.0.x | GenAI, MCP, Anomaly Detection (requires Rust toolchain) |

`PROXYSQLGENAI=1` implies `PROXYSQL31=1`, which implies `PROXYSQLFFTO=1` and `PROXYSQLTSDB=1`.

### Build Flags

- `NOJEMALLOC=1` — disable jemalloc
- `WITHASAN=1` — enable AddressSanitizer (requires `NOJEMALLOC=1`)
- `WITHGCOV=1` — enable code coverage
- `PROXYSQLCLICKHOUSE=1` — enabled by default in current builds

## Testing

Tests use TAP (Test Anything Protocol) with Docker-based backend infrastructure.

```bash
# Build and run all TAP tests
make build_tap_tests
cd test/tap && make

# Run specific test groups
cd test/tap/tests && make
cd test/tap/tests_with_deps && make

# Test infrastructure (Docker environments)
# Located in test/infra/ with docker-compose configs for:
# mysql57, mysql84, mariadb10, pgsql16, pgsql17, clickhouse23, etc.
```

Test files follow the naming pattern `test_*.cpp` or `*-t.cpp` in `test/tap/tests/`.

## Architecture

### Build Pipeline

```
deps/ → builds 25+ vendored dependencies as static libraries
lib/ → compiles ~121 .cpp files into libproxysql.a
src/main.cpp → links against libproxysql.a to produce the proxysql binary
```

### Dual-Protocol Design

MySQL and PostgreSQL share parallel class hierarchies with the same architecture but protocol-specific implementations:

| Layer | MySQL | PostgreSQL |
|-------|-------|------------|
| Protocol | `MySQL_Protocol` | `PgSQL_Protocol` |
| Session | `MySQL_Session` | `PgSQL_Session` |
| Thread | `MySQL_Thread` | `PgSQL_Thread` |
| HostGroups | `MySQL_HostGroups_Manager` | `PgSQL_HostGroups_Manager` |
| Monitor | `MySQL_Monitor` | `PgSQL_Monitor` |
| Query Processor | `MySQL_Query_Processor` | `PgSQL_Query_Processor` |
| Logger | `MySQL_Logger` | `PgSQL_Logger` |

### Core Components

- **Admin Interface** (`ProxySQL_Admin.cpp`, `Admin_Handler.cpp`) — SQL-based configuration via SQLite3 backend. Supports runtime config changes without restart. Schema versions tracked in `ProxySQL_Admin_Tables_Definitions.h`.
- **HostGroups Manager** — Routes connections based on hostgroup assignments. Supports master-slave, Galera, Group Replication, and Aurora topologies.
- **Query Processor** — Parses queries, matches against routing rules, handles query caching via `Query_Cache`.
- **Monitor** — Health-checks backends for replication lag, read-only status, and connectivity.
- **Threading** — Event-based I/O using libev. `Base_Thread` base class with protocol-specific thread managers.
- **HTTP/REST** (`ProxySQL_HTTP_Server`, `ProxySQL_RESTAPI_Server`) — Metrics and management endpoints.

### Key Dependencies (in deps/)

- `jemalloc` — memory allocator
- `sqlite3` — admin config storage
- `mariadb-client-library` — MySQL protocol
- `postgresql` — PostgreSQL protocol
- `re2`, `pcre` — regex engines
- `libev` — event loop
- `libinjection` — SQL injection detection
- `lz4`, `zstd` — compression
- `curl`, `libmicrohttpd`, `libhttpserver` — HTTP
- `prometheus-cpp` — metrics
- `libscram` — SCRAM authentication

### Conditional Components

- **FFTO** (Fast Forward Traffic Observer) — `MySQLFFTO.cpp`, `PgSQLFFTO.cpp`
- **TSDB** — Time-series metrics with embedded dashboard
- **GenAI/MCP** — `GenAI_Thread`, `MCP_Thread`, `LLM_Bridge`, `Anomaly_Detector`, tool handlers
- **ClickHouse** — Native ClickHouse protocol support

## Code Layout

- `include/` — All headers (.h/.hpp). Include guards use `#ifndef __CLASS_*_H`.
- `lib/` — Core library sources (~121 files). One class per file typically.
- `src/main.cpp` — Entry point, daemon init, thread spawning (~95K lines).
- `test/tap/` — TAP test framework and tests.
- `test/infra/` — Docker-based test environments.
- `.github/workflows/` — CI/CD pipelines (selftests, TAP tests, package builds, CodeQL).

## Agent Guidelines

See `doc/agents/` for detailed guidance on working with AI coding agents:
- `doc/agents/project-conventions.md` — ProxySQL-specific rules (directories, build, test harness, git workflow)
- `doc/agents/task-assignment-template.md` — Template for writing issues assignable to AI agents
- `doc/agents/common-mistakes.md` — Known agent failure patterns with prevention and detection

### Unit Test Harness

Unit tests live in `test/tap/tests/unit/` and link against `libproxysql.a` via a custom test harness. Tests must use `test_globals.h` and `test_init.h` — see `doc/agents/project-conventions.md` for the full pattern.

## Coding Conventions

- Class names: `PascalCase` with protocol prefixes (`MySQL_`, `PgSQL_`, `ProxySQL_`)
- Member variables: `snake_case`
- Constants/macros: `UPPER_SNAKE_CASE`
- C++17 required; conditional compilation via `#ifdef PROXYSQLGENAI`, `#ifdef PROXYSQL31`, etc.
- Performance-critical code — consider implications of changes to hot paths
- RAII for resource management; jemalloc for allocation
- Pthread mutexes for synchronization; `std::atomic<>` for counters
9 changes: 9 additions & 0 deletions doc/agents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Agent Guidelines for ProxySQL

This directory contains guidance for AI coding agents (Claude Code, GitHub Copilot, etc.) working on the ProxySQL codebase.

| Document | Purpose |
|----------|---------|
| [project-conventions.md](project-conventions.md) | ProxySQL-specific rules: where files go, how to build, branch model |
| [task-assignment-template.md](task-assignment-template.md) | Template for writing issues that agents can execute correctly |
| [common-mistakes.md](common-mistakes.md) | Known failure patterns and how to prevent them |
148 changes: 148 additions & 0 deletions doc/agents/common-mistakes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Common Mistakes by AI Coding Agents

Patterns observed across multiple AI agent interactions on the ProxySQL codebase, with root cause analysis and prevention strategies.

## 1. Wrong Branch Target

**Symptom:** PR targets `v3.0` (main) instead of the feature branch.

**Root cause:** Agents prioritize technical content over administrative instructions. Even when branch info is present in the issue, agents often skim past it while focusing on code requirements, then use heuristics (e.g., most recent branch, default branch) to fill the gap they don't realize they have.

**Prevention:** Place git workflow instructions **at the very top** of the issue, before the technical description. Agents read top-down with decreasing attention — administrative details buried after exciting code specs will be skipped.

```
### FIRST: Git workflow (do this before reading anything else)
- Create branch `v3.0-XXXX` from `v3.0-5473`
- PR target: `v3.0-5473`
```

**Detection:** Check `gh pr view <number> --json baseRefName` after PR creation.

---

## 2. Reimplemented Functions in Test Files

**Symptom:** Test file contains copy-pasted reimplementations of the functions under test. Tests validate the copy, not the real production code.

**Root cause:** Agent doesn't know the build system links tests against `libproxysql.a`, so it creates standalone tests that don't depend on the library.

**Prevention:**
- Explain that tests link against `libproxysql.a` (the real functions are available at link time)
- Add to DO NOT list: "Do NOT reimplement extracted functions in the test file"
- Provide the Makefile rule that shows the linking

**Detection:** `grep -c "static.*calculate_eviction\|static.*evaluate_pool" test_file.cpp` — if > 0, functions were reimplemented.

---

## 3. Test Files in Wrong Directory

**Symptom:** Test placed in `test/tap/tests/` (E2E test directory) instead of `test/tap/tests/unit/` (unit test directory).

**Root cause:** Agent sees existing test files in `test/tap/tests/` and follows that pattern. Doesn't know about the `unit/` subdirectory.

**Prevention:** Specify the exact file path including directory in the issue deliverables.

**Detection:** `ls test/tap/tests/*unit*` should return nothing — unit tests belong in `test/tap/tests/unit/`.

---

## 4. Manual TAP Symbol Stubs

**Symptom:** Test file manually defines `noise_failures`, `noise_failure_mutex`, `stop_noise_tools()`, `get_noise_tools_count()`.

**Root cause:** Agent compiles `tap.cpp` which references these symbols. Without the harness, the agent must define them. This is a signal the agent isn't using the harness.

**Prevention:**
- Explain that `test_globals.cpp` already provides all TAP stubs
- Add to DO NOT list: "Do NOT define noise_failures or stop_noise_tools"

**Detection:** `grep -c "noise_failures\|stop_noise_tools" test_file.cpp` — if > 0, harness not used.

---

## 5. Merged Instead of Rebased

**Symptom:** PR diff includes dozens of unrelated files because the agent ran `git merge <upstream>` into its branch.

**Root cause:** Agent's default strategy for incorporating upstream changes is merge. This creates a merge commit that brings all upstream changes into the PR diff.

**Prevention:** Explicit instruction: "Use `git rebase`, NOT `git merge`."

**Detection:** `git log --merges <branch> --not <base>` — any merge commits indicate merging.

---

## 6. Circular Include Dependencies

**Symptom:** Production code compiles on the agent's machine (or doesn't get tested) but fails in CI or on other platforms with "unknown type name" errors.

**Root cause:** ProxySQL has circular include chains (`proxysql.h` → `cpp.h` → `MySQL_HostGroups_Manager.h` → `Base_HostGroups_Manager.h` → `proxysql.h`). Placing new declarations in these headers can result in the declarations being invisible depending on include order.

**Prevention:**
- Create standalone headers with their own include guards (e.g., `ConnectionPoolDecision.h`)
- Explicitly warn about the circular chain in the issue
- Require `make build_lib -j4` as a verification step

**Detection:** Compilation failure with "unknown type name" for a type that clearly exists in a header.

---

## 7. Modified Existing Test Files Instead of Creating New Ones

**Symptom:** Agent adds tests to an existing test file instead of creating a new one for the new feature.

**Root cause:** Agent sees a test file for a related component and assumes new tests belong there.

**Prevention:** Specify the exact test file name in the issue: "Create `test/tap/tests/unit/my_feature_unit-t.cpp`."

---

## 8. Didn't Verify Compilation

**Symptom:** PR contains code that doesn't compile. Agent submitted without building.

**Root cause:** Some agents don't have access to the build environment, or don't run the build as part of their workflow.

**Prevention:**
- Add explicit verification step: "`make build_lib -j4` must exit with code 0"
- Add to acceptance criteria as a hard requirement

---

## 9. Overly Broad Changes

**Symptom:** Agent refactors callers, updates documentation, fixes unrelated bugs, or "improves" code outside the task scope.

**Root cause:** Agent optimizes for perceived quality/completeness and makes changes it considers beneficial.

**Prevention:**
- Explicitly scope: "Only modify `<list of files>`"
- Add: "Do not refactor code outside the scope of this task"
- Add: "Do not fix pre-existing issues you notice — file separate issues for those"

---

## Summary: Red Flags in Agent PRs

Quick checks to run on any agent-generated PR:

```bash
# Wrong base branch?
gh pr view <PR> --json baseRefName -q '.baseRefName'

# Test in wrong directory?
gh pr diff <PR> | grep "^+++ b/test/tap/tests/[^u]"

# Reimplemented functions?
gh pr diff <PR> | grep "^+static.*calculate_\|^+static.*evaluate_\|^+static.*should_"

# Manual TAP stubs?
gh pr diff <PR> | grep "^+.*noise_failures\|^+.*stop_noise_tools"

# Merge commits?
gh pr view <PR> --json commits --jq '.commits[].messageHeadline' | grep -i merge

# Unrelated files changed?
gh pr diff <PR> | grep "^+++ b/" | grep -v "<expected_files_pattern>"
```
Loading
Loading