|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## General Technical Documentation |
| 6 | + |
| 7 | +For comprehensive technical information about the SQLite Sync architecture, build system, CRDT implementation, and design patterns, see [AGENTS.md](./AGENTS.md). |
| 8 | + |
| 9 | +This file contains: |
| 10 | +- Project overview and architecture |
| 11 | +- Build commands and testing |
| 12 | +- Core components and patterns |
| 13 | +- Performance considerations |
| 14 | +- Design principles and constraints |
| 15 | + |
| 16 | +## Development Workflow |
| 17 | + |
| 18 | +### Adding New SQL Functions |
| 19 | + |
| 20 | +1. Implement in `src/sqlite/cloudsync_sqlite.c` (e.g., `cloudsync_xyz_func`) |
| 21 | +2. Register in `cloudsync_register()` via `sqlite3_create_function()` |
| 22 | +3. Document in `API.md` |
| 23 | +4. Add tests in `test/unit.c` |
| 24 | + |
| 25 | +### Modifying CRDT Logic |
| 26 | + |
| 27 | +The merge algorithm lives in `cloudsync.c`: |
| 28 | +- `merge_insert()` - Handles row-level merge decisions |
| 29 | +- `merge_insert_col()` - Handles column-level merge decisions |
| 30 | +- Algorithm-specific logic controlled by `table->algo` enum |
| 31 | + |
| 32 | +**Performance requirement**: Merge code is hot-path (processes every incoming change during sync). Always use prepared statements stored in `cloudsync_table_context`. Never compile SQL at runtime in merge functions. See [AGENTS.md - Performance Considerations](./AGENTS.md#performance-considerations) for details. |
| 33 | + |
| 34 | +### Schema Migrations |
| 35 | + |
| 36 | +The extension tracks its schema version in `cloudsync_settings.schemaversion`. When the schema changes: |
| 37 | +1. Increment version in migration code |
| 38 | +2. Add migration logic in `dbutils_settings_init()` |
| 39 | +3. Handle both fresh installs and upgrades |
| 40 | + |
| 41 | +### Platform-Specific Code |
| 42 | + |
| 43 | +- Most code is platform-agnostic C |
| 44 | +- Platform detection via `CLOUDSYNC_DESKTOP_OS` macro (macOS, Linux desktop, Windows) |
| 45 | +- Network layer can use native APIs (macOS NSURLSession) with `NATIVE_NETWORK` flag |
| 46 | +- File I/O helpers (`cloudsync_file_*`) only available on desktop platforms |
| 47 | + |
| 48 | +## Specialized Subagents |
| 49 | + |
| 50 | +When working on specific areas of the codebase, you can launch specialized subagents with domain expertise. |
| 51 | + |
| 52 | +### PostgreSQL Extension Agent |
| 53 | + |
| 54 | +**Purpose**: Implement the PostgreSQL version of SQLite Sync extension |
| 55 | + |
| 56 | +**Context**: The codebase has a database abstraction layer (`database.h`) with database-specific implementations in subdirectories. PostgreSQL-specific code lives in `src/postgresql/`. The goal is to create a fully functional PostgreSQL extension that implements the same CRDT sync logic. |
| 57 | + |
| 58 | +**Launch command**: |
| 59 | +``` |
| 60 | +Use the Task tool with prompt: "Implement the PostgreSQL backend for SQLite Sync. Study the database.h abstraction layer and src/sqlite/database_sqlite.c implementation, then implement src/postgresql/database_postgresql.c with full PostgreSQL support including prepared statements, value binding, and transaction hooks." |
| 61 | +``` |
| 62 | + |
| 63 | +**Key files to study**: |
| 64 | +- `src/database.h` - Abstract database API |
| 65 | +- `src/sqlite/database_sqlite.c` - SQLite implementation (reference) |
| 66 | +- `src/postgresql/database_postgresql.c` - PostgreSQL implementation |
| 67 | +- `src/cloudsync.c` - Uses database abstraction (must work unchanged) |
| 68 | + |
| 69 | +**Requirements**: |
| 70 | +- Implement all functions in `database.h` using libpq (PostgreSQL C API) |
| 71 | +- Maintain same semantics as SQLite version |
| 72 | +- Handle PostgreSQL-specific data types mapping |
| 73 | +- Test with PostgreSQL backend |
| 74 | + |
| 75 | +**Testing approach**: |
| 76 | +- Modify Makefile to link against libpq |
| 77 | +- Create PostgreSQL-specific test suite |
| 78 | +- Verify CRDT operations work identically to SQLite |
| 79 | + |
| 80 | +### Other Potential Subagents |
| 81 | + |
| 82 | +Consider creating specialized agents for: |
| 83 | +- **WASM/Browser Agent**: Optimize for WebAssembly builds and OPFS storage |
| 84 | +- **Network Protocol Agent**: Enhance sync protocol or add new backends |
| 85 | +- **CRDT Algorithm Agent**: Implement new conflict resolution algorithms |
| 86 | +- **Performance Optimization Agent**: Profile and optimize hot-path code |
| 87 | + |
| 88 | +## Slash Commands |
| 89 | + |
| 90 | +Custom slash commands help automate common development tasks in this repository. |
| 91 | + |
| 92 | +### Available Commands |
| 93 | + |
| 94 | +Create slash commands in `.claude/commands/` directory. Each command is a markdown file executed when invoked. |
| 95 | + |
| 96 | +### Example: `/review-sync` - Review Sync Logic |
| 97 | + |
| 98 | +**File**: `.claude/commands/review-sync.md` |
| 99 | + |
| 100 | +```markdown |
| 101 | +Please review the CRDT synchronization logic for correctness and performance: |
| 102 | + |
| 103 | +1. Read and analyze the merge algorithm in `src/cloudsync.c`: |
| 104 | + - `merge_insert()` function |
| 105 | + - `merge_insert_col()` function |
| 106 | + - Vector clock comparison logic |
| 107 | + |
| 108 | +2. Check for potential issues: |
| 109 | + - Race conditions in concurrent merges |
| 110 | + - Memory leaks in error paths |
| 111 | + - Inefficient SQL queries (should use prepared statements) |
| 112 | + - Incorrect handling of tombstones |
| 113 | + |
| 114 | +3. Verify compliance with design principles from AGENTS.md: |
| 115 | + - Hot-path code uses prepared statements |
| 116 | + - No runtime SQL compilation in merge functions |
| 117 | + - Proper error handling |
| 118 | + |
| 119 | +4. Suggest improvements with specific code examples |
| 120 | + |
| 121 | +Please provide a summary of findings and recommendations. |
| 122 | +``` |
| 123 | + |
| 124 | +**Usage**: Type `/review-sync` in Claude Code to trigger this review workflow. |
| 125 | + |
| 126 | +### Example: `/test-crdt` - Test CRDT Algorithm |
| 127 | + |
| 128 | +**File**: `.claude/commands/test-crdt.md` |
| 129 | + |
| 130 | +```markdown |
| 131 | +Create a comprehensive test scenario for CRDT conflict resolution: |
| 132 | + |
| 133 | +1. Design a multi-device sync test with: |
| 134 | + - 3 devices making concurrent changes |
| 135 | + - Updates to same row, different columns |
| 136 | + - Updates to same column (conflict) |
| 137 | + - Deletions with concurrent updates |
| 138 | + |
| 139 | +2. Generate test code in `test/unit.c` format |
| 140 | + |
| 141 | +3. Show expected outcomes based on: |
| 142 | + - Vector clock values (db_version, seq, site_id) |
| 143 | + - CRDT algorithm (CLS/GOS/DWS/AWS) |
| 144 | + - Deterministic conflict resolution |
| 145 | + |
| 146 | +4. Run the test and verify results |
| 147 | + |
| 148 | +Focus on edge cases that could expose bugs in the merge algorithm. |
| 149 | +``` |
| 150 | + |
| 151 | +### Creating New Slash Commands |
| 152 | + |
| 153 | +To add a new command: |
| 154 | + |
| 155 | +1. Create `.claude/commands/<command-name>.md` |
| 156 | +2. Write the prompt describing the task |
| 157 | +3. Use `/command-name` to invoke it |
| 158 | + |
| 159 | +**Useful commands to create**: |
| 160 | +- `/add-function` - Scaffold a new SQL function with tests |
| 161 | +- `/optimize-query` - Analyze and optimize a SQL query |
| 162 | +- `/check-leaks` - Review code for memory leaks |
| 163 | +- `/cross-compile` - Build for all platforms and report issues |
| 164 | +- `/benchmark-merge` - Profile merge performance |
| 165 | + |
| 166 | +## Branch Information |
| 167 | + |
| 168 | +Main branch: `main` |
| 169 | +Current working branch: `database-api` - Database abstraction layer refactoring |
| 170 | + |
| 171 | +**macOS Testing Note:** If the default `/usr/bin/sqlite3` doesn't support loading extensions, set the SQLITE3 variable when running tests (Adjust the version path if using a specific version like /opt/homebrew/Cellar/sqlite/3.50.4/bin/sqlite3: |
| 172 | +``` |
| 173 | +make test SQLITE3=/opt/homebrew/bin/sqlite3 |
| 174 | +make unittest SQLITE3=/opt/homebrew/bin/sqlite3 |
| 175 | +``` |
0 commit comments