Skip to content

Commit 0896a7a

Browse files
committed
chore(docs): add AGENTS.md for AI coding agent guidance
AI coding agents like Claude Code, Cursor, and Codex are increasingly used by contributors. We can maintain a comprehensive guidance for AI to mitigate issues which low quality PRs may waste reviewer's time.
1 parent a10b6e1 commit 0896a7a

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

AGENTS.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# AGENTS.md
2+
3+
This file provides guidance to AI coding agents (e.g., Claude Code, Cursor, ChatGPT Codex, Gemini) when working with code in this repository.
4+
5+
While working on Apache Kvrocks, please remember:
6+
7+
- Always use English in code and comments.
8+
- Only add meaningful comments when the code's behavior is difficult to understand.
9+
- Only add meaningful tests when they actually verify internal behaviors; otherwise, don't create them unless requested.
10+
- Always run the formatter before submitting changes.
11+
12+
## Build and Development Commands
13+
14+
### Building
15+
16+
```bash
17+
# Build kvrocks and utilities
18+
./x.py build # Build to ./build directory
19+
./x.py build -j N # Build with N parallel jobs
20+
./x.py build --unittest # Build with unit tests
21+
./x.py build -DENABLE_OPENSSL=ON # Build with TLS support
22+
./x.py build --ninja # Use Ninja build system
23+
./x.py build --skip-build # Only run CMake configure
24+
./x.py build -DCMAKE_BUILD_TYPE=Debug # Debug build
25+
26+
# Run a local server
27+
./build/kvrocks -c kvrocks.conf
28+
29+
# Fetch dependencies
30+
./x.py fetch-deps # Fetch dependency archives
31+
```
32+
33+
### Testing
34+
35+
```bash
36+
# Build and run C++ unit tests
37+
./x.py build --unittest
38+
./x.py test cpp
39+
40+
# Run Go integration tests
41+
./x.py test go
42+
43+
# Run specific Go test by path
44+
./x.py test go tests/gocase/unit/...
45+
```
46+
47+
### Lint
48+
49+
You must run the formatter and linters before submitting code changes. This ensures code quality and consistency across the project. It requires installing `clang-format`, `clang-tidy`, and `golangci-lint` locally. Please refer to the CONTRIBUTING.md for setup instructions.
50+
51+
```bash
52+
# Format code (must pass before submitting)
53+
./x.py format
54+
55+
# Check code format (fails if not formatted)
56+
./x.py check format
57+
58+
# Run clang-tidy
59+
./x.py check tidy
60+
61+
# Run golangci-lint for Go tests
62+
./x.py check golangci-lint
63+
```
64+
65+
## Architecture Overview
66+
67+
Apache Kvrocks is a distributed key-value NoSQL database compatible with the Redis protocol, using RocksDB as its storage engine.
68+
69+
### Core Structure
70+
71+
- **`src/server/`**: Main server orchestration, connection handling, and worker threads. The `Server` class manages the event loop, worker threads, and coordinates all components.
72+
- **`src/storage/`**: RocksDB integration layer. Key classes:
73+
- `Storage`: Manages RocksDB instance, column families, and write batches
74+
- `Context`: Passes snapshot and batch between APIs for transactional consistency
75+
- **`src/commands/`**: Redis protocol command implementations. Each command type has a corresponding `Commander` subclass.
76+
- **`src/types/`**: Redis data structure implementations (String, Hash, List, Set, ZSet, Stream, etc.)
77+
- **`src/cluster/`**: Cluster management, slot migration, and replication.
78+
- **`src/search/`**: Full-text search and vector search (HNSW) implementation.
79+
- **`src/config/`**: Server configuration parsing and management.
80+
- **`src/cli/`**: Command-line interface utilities.
81+
- **`src/common/`**: Shared utilities and helper functions.
82+
- **`src/stats/`**: Statistics and metrics collection.
83+
84+
### Key Patterns
85+
86+
- **Column Families**: 8 column families are used - PrimarySubkey, Metadata, SecondarySubkey, PubSub, Propagate, Stream, Search, Index.
87+
- **Command Registration**: Commands are registered via the `REDIS_REGISTER_COMMANDS` macro with flags like `kCmdWrite`, `kCmdReadOnly`, `kCmdBlocking`, etc.
88+
- **Write Batch with Index**: Used for transactional mode to group writes before commit.
89+
- **Worker Thread Model**: Libevent-based async I/O with dedicated worker threads.
90+
- **Namespace Isolation**: Token-based multi-tenancy using the `__namespace` column family.
91+
92+
### Data Encoding
93+
94+
- `METADATA_ENCODING_VERSION=1` (default): Encodes 64-bit size and expire time in milliseconds.
95+
- `METADATA_ENCODING_VERSION=0`: Legacy encoding.
96+
97+
## Coding Style and Naming Conventions
98+
99+
- C++ formatting follows `.clang-format` (Google-based, 2-space indent, 120-column limit, sorted includes).
100+
- Use `.cc`/`.h` file extensions with `snake_case` filenames.
101+
- Types use `PascalCase`; match existing patterns in nearby code.
102+
- Use existing utilities and helper functions when possible; avoid reinventing the wheel.
103+
- Go code should stay `gofmt`-clean and comply with `tests/gocase/.golangci.yml`.
104+
105+
## Testing Guidelines
106+
107+
You could provide Go tests for integration-level verification of command behaviors and C++ unit tests for internal logic. Focus on testing new features or bug fixes, and avoid adding tests that don't verify meaningful behavior changes.
108+
109+
- **Go Integration Tests** (`tests/gocase/`): Use `*_test.go` files, organized by feature (unit/, integration/, tls/).
110+
- **C++ Unit Tests** (`tests/cppunit/`): Use `*_test.cc` files with GoogleTest framework.
111+
- Add or update tests alongside behavior changes.
112+
- Prefer focused unit tests; add integration coverage when commands or replication/storage behaviors change.
113+
- Use `./x.py test ...` entry points for consistent setup.
114+
115+
## Commit Message Format
116+
117+
Use conventional commits with a scope indicating the affected component:
118+
119+
```
120+
feat(rdb): add DUMP support for SortedInt type
121+
fix(replication): prevent WAL exhaustion from slow consumers
122+
fix(string): add empty string value check for INCR to match Redis behavior
123+
perf(hash): use MultiGet to reduce RocksDB calls in HMSET
124+
chore(deps): Bump rocksdb to v10.10.1
125+
chore(ci): bump crate-ci/typos action to v1.43.1
126+
chore(tests): replace to slices.Reverse() in go test
127+
```
128+
129+
Common scopes: `server`, `storage`, `commands`, `cluster`, `search`, `types`, `replication`, `rdb`, `stream`, `hash`, `string`, `list`, `set`, `zset`, `deps`, `ci`, `tests`, `conf`.
130+
131+
## Common Tasks
132+
133+
### Adding a New Command
134+
135+
1. Create or update the command handler in `src/commands/`.
136+
2. Implement the `Commander` subclass with `Parse()` and `Execute()` methods.
137+
3. Register the command using `REDIS_REGISTER_COMMANDS` macro with appropriate flags.
138+
4. Add the underlying data operation in `src/types/` if needed.
139+
5. Add C++ unit tests in `tests/cppunit/`.
140+
6. Add Go integration tests in `tests/gocase/`.
141+
142+
### Adding a New Data Type
143+
144+
1. Implement the type in `src/types/` following existing patterns.
145+
2. Define the metadata encoding in `src/storage/`.
146+
3. Add command handlers in `src/commands/`.
147+
4. Register commands with the `REDIS_REGISTER_COMMANDS` macro.
148+
5. Add tests for both the type operations and command handlers.
149+
150+
### Debugging
151+
152+
1. Check server logs (configurable log level in kvrocks config).
153+
2. Use the `DEBUG` command for runtime inspection.
154+
3. Use sanitizers via build flags for memory and thread issues.
155+
4. Refer to `tests/lsan-suppressions` and `tests/tsan-suppressions` for known suppression rules.
156+
157+
## Important Notes
158+
159+
- Kvrocks aims for Redis protocol compatibility; always verify behavior against Redis when implementing or fixing commands.
160+
- All changes must pass `./x.py check format` and `./x.py check tidy`.
161+
- Don't change public command behavior unless requested.
162+
- RocksDB is the core storage dependency; be cautious with storage-layer changes.
163+
- Adding a new column family breaks forward compatibility; avoid this if possible and prefer using existing column families.

0 commit comments

Comments
 (0)