Skip to content

Commit 51c4b38

Browse files
committed
feat: token handling
1 parent 1f422b8 commit 51c4b38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1424
-1421
lines changed

.serena/memories/development_guidelines.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,20 @@ Target: 90%+ coverage
5454
2. **Green**: Minimal code to make tests pass
5555
3. **Refactor**: Clean up while keeping tests green
5656

57-
Run coverage: `cargo tarpaulin` or `cargo llvm-cov`
58-
5957
## Tooling
6058

61-
### Formatting
62-
Use nightly toolchain for formatting:
59+
### Build & Test
60+
Use `just` for common tasks (see `just` or `Justfile` for full list):
61+
```bash
62+
just check # pre-commit: fmt + clippy + test
63+
just ci # CI validation: fmt + clippy + doc-check + test
64+
```
65+
66+
Or cargo with pinned toolchain:
6367
```bash
68+
cargo +1.92 build --workspace
69+
cargo +1.92 test --workspace --lib
70+
cargo +1.92 clippy --workspace --all-targets -- -D warnings
6471
cargo +nightly fmt
6572
```
6673

.serena/memories/project_overview.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,33 @@ Ledger is InferaDB's storage layer — a blockchain database for cryptographical
66
## Tech Stack
77
- **Language**: Rust 1.92 (2024 edition)
88
- **Storage**: inferadb-ledger-store (custom embedded ACID B+ tree engine)
9-
- **Consensus**: openraft (Raft implementation)
9+
- **Consensus**: openraft 0.9 (Raft implementation)
1010
- **Networking**: gRPC via tonic/prost (HTTP/2 over TCP)
11-
- **Crypto**: SHA-256, seahash, rs_merkle
12-
- **Error Handling**: snafu with backtraces
11+
- **Crypto**: SHA-256, seahash, rs_merkle, Ed25519 (JWT signing), AES-256-GCM (envelope encryption)
12+
- **Error Handling**: snafu with implicit location tracking (server crates), thiserror (SDK crate)
13+
- **Builders**: bon crate for type-safe builders
1314

14-
## Crate Structure
15-
- `inferadb-ledger-types`: Core types, errors, crypto primitives
16-
- `inferadb-ledger-store`: Embedded B+ tree database engine
17-
- `inferadb-ledger-state`: Domain state management, indexes, snapshots
18-
- `inferadb-ledger-raft`: Raft consensus, gRPC services
19-
- `inferadb-ledger-server`: Server binary entry point
20-
- `inferadb-ledger-sdk`: Production-grade Rust SDK
21-
- `inferadb-ledger-test-utils`: Shared test utilities
15+
## Crate Structure (9 crates)
16+
- `inferadb-ledger-types`: Core types, errors, crypto primitives, config, token claims, newtype IDs
17+
- `inferadb-ledger-store`: Embedded B+ tree database engine, crypto key management
18+
- `inferadb-ledger-proto`: Protobuf code generation and From/TryFrom conversions
19+
- `inferadb-ledger-state`: Domain state, entity/relationship CRUD, system services (users, signing keys, tokens)
20+
- `inferadb-ledger-raft`: Raft consensus, transaction batching, rate limiting, saga orchestrator, background jobs
21+
- `inferadb-ledger-services`: gRPC service implementations (12 services), JwtEngine, LedgerServer assembly
22+
- `inferadb-ledger-server`: Server binary entry point, bootstrap, CLI configuration
23+
- `inferadb-ledger-sdk`: Production-grade Rust SDK, retry/circuit-breaker, cancellation, metrics
24+
- `inferadb-ledger-test-utils`: Shared test utilities, crash injection, proptest strategies
2225

2326
## Key Concepts
24-
- **Organization**: Top-level tenant isolation boundary
25-
- **Vault**: Relationship store with own cryptographic chain
27+
- **Organization**: Top-level tenant isolation boundary (dual-ID: OrganizationId/OrganizationSlug)
28+
- **Vault**: Relationship store with own cryptographic chain (dual-ID: VaultId/VaultSlug)
2629
- **Entity**: Key-value data with TTL/versioning
2730
- **Relationship**: Authorization tuple (resource, relation, subject)
31+
- **User**: Identity with email, role, status, token version (dual-ID: UserId/UserSlug)
32+
- **App**: Organization-scoped client application (dual-ID: AppId/AppSlug)
33+
- **SigningKey**: Ed25519 JWT signing key with scope and lifecycle
34+
- **RefreshToken**: Session token family with rotate-on-use and poison detection
2835
- **Shard**: Multiple organizations sharing a Raft group
36+
37+
## gRPC Services (12)
38+
Read, Write, Admin, Organization, Vault, User, App, Token, Events, Health, Discovery, Raft

.serena/memories/style_conventions.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,32 @@
1313
## Linting Rules
1414
**Denied:**
1515
- `unsafe_code` - No unsafe code allowed
16-
- `unwrap_used` - Use snafu error handling
16+
- `unwrap_used` - Use snafu error handling (server) or thiserror (SDK)
1717
- `panic` - No panics in production code
18+
- `todo` - No `todo!()` or `unimplemented!()` allowed
1819

1920
**Warned:**
2021
- `missing_docs` - Document public items
2122
- `expect_used` - Prefer proper error handling
22-
- `todo` - Allowed but flagged
2323

2424
## Error Handling
25-
- Use `snafu` with backtraces
26-
- No `.unwrap()` - use `.context()` or `.ok_or()`
27-
- Use `?` operator for propagation
25+
- **Server crates**: snafu with implicit location tracking and `.context()` propagation
26+
- **SDK crate**: thiserror for consumer-facing error types
27+
- No `.unwrap()` — use `.context()`, `.ok_or()`, or `?`
28+
29+
## Builders (bon)
30+
- `#[derive(bon::Builder)]` for simple structs
31+
- `#[bon]` impl block with `#[builder]` for fallible constructors
32+
- `#[builder(into)]` for String fields
33+
- Match `#[builder(default)]` with `#[serde(default)]` for config
2834

2935
## Documentation
3036
- Crate-level docs with `//!` comments
3137
- Document all public types and functions
38+
- Code examples use ` ```no_run ` (never `ignore` or `text`)
3239

3340
## Naming
3441
- Types: PascalCase (OrganizationId, VaultBlock)
3542
- Functions: snake_case (bucket_id, sha256_concat)
3643
- Constants: SCREAMING_SNAKE_CASE (EMPTY_HASH)
44+
- Newtype IDs: `define_id!` / `define_slug!` macros in types/src/types.rs
Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
11
# Development Commands
22

3-
## Build
3+
## Using `just` (preferred)
44
```bash
5-
cargo build # Build all crates
6-
cargo build --release # Release build
7-
cargo build -p inferadb-ledger-types # Build specific crate
5+
just check # pre-commit: fmt + clippy + test
6+
just check-quick # fast pre-commit: fmt + clippy only
7+
just ci # CI validation: fmt + clippy + doc-check + test
8+
just ready # pre-PR: proto + fmt + clippy + test
9+
just test # unit tests only (--workspace --lib)
10+
just test-ff # unit tests, stop on first failure
11+
just test-integration # integration tests (spawns clusters)
12+
just test-integration-ff # integration tests, stop on first failure
13+
just fmt # format code (nightly)
14+
just clippy # run linter
15+
just doc-check # build rustdoc with -D warnings
16+
just proto # regenerate protobuf code
17+
just run # run server (dev mode)
818
```
919

10-
## Testing
20+
## Using cargo directly
1121
```bash
12-
cargo test # Run all tests
13-
cargo test -p inferadb-ledger-state # Test specific crate
14-
cargo test -- --nocapture # With output
15-
```
16-
17-
## Linting & Formatting
18-
```bash
19-
cargo +nightly fmt # Format code (nightly required)
20-
cargo +nightly fmt --check # Check formatting
21-
cargo clippy --all-targets # Run clippy
22-
cargo clippy -- -D warnings # Clippy with warnings as errors
23-
```
24-
25-
## Documentation
26-
```bash
27-
cargo doc --open # Generate and open docs
28-
```
29-
30-
## Running
31-
```bash
32-
cargo run -p inferadb-ledger-server # Run server (dev)
33-
cargo run -p inferadb-ledger-server --release # Run server (release)
34-
```
35-
36-
## Full Check Before Commit
37-
```bash
38-
cargo +nightly fmt --check && cargo clippy --all-targets -- -D warnings && cargo test
22+
cargo +1.92 build --workspace # Build all crates
23+
cargo +1.92 build -p inferadb-ledger-types # Build specific crate
24+
cargo +1.92 test --workspace --lib # Unit tests
25+
cargo +1.92 test -p inferadb-ledger-state # Test specific crate
26+
cargo +1.92 test <name> -- --nocapture # Single test with output
27+
cargo +nightly fmt # Format (nightly required)
28+
cargo +1.92 clippy --workspace --all-targets -- -D warnings
29+
cargo +1.92 doc --workspace --no-deps # Generate docs
3930
```

.serena/memories/task_completion.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22

33
## Before Marking Complete
44

5-
1. **Format**: `cargo +nightly fmt`
6-
2. **Clippy**: `cargo +1.85 clippy --all-targets -- -D warnings`
7-
3. **Tests**: `cargo test`
8-
4. **Build**: `cargo build`
5+
1. **Format**: `cargo +nightly fmt --check`
6+
2. **Clippy**: `cargo +1.92 clippy --workspace --all-targets -- -D warnings`
7+
3. **Tests**: `cargo +1.92 test --workspace --lib`
8+
4. **Build**: `cargo +1.92 build --workspace`
99

1010
## Quick One-Liner
1111
```bash
12-
cargo +nightly fmt && cargo +1.85 clippy --all-targets -- -D warnings && cargo test
12+
just ci
13+
```
14+
15+
Or manually:
16+
```bash
17+
cargo +nightly fmt --check && cargo +1.92 clippy --workspace --all-targets -- -D warnings && cargo +1.92 test --workspace --lib
1318
```
1419

1520
## Critical Rules
16-
- Never use `.unwrap()` - always snafu error handling
17-
- Never use `panic!` in non-test code
21+
- Never use `.unwrap()` — use snafu `.context()` (server) or thiserror (SDK)
22+
- Never use `panic!`, `todo!()`, `unimplemented!()`
1823
- No unsafe code
24+
- No TODO/FIXME/HACK comments
1925
- Document all public items
2026
- Use `?` for error propagation

.serena/project.yml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,32 @@ initial_prompt: |
8787
It exposes a gRPC API (HTTP/2 over TCP) and uses TCP for inter-node Raft consensus.
8888
8989
## Core Terminology
90-
- **Namespace**: Storage unit per organization, containing entities and vaults. Isolated with separate Raft consensus per shard.
91-
- **Vault**: Relationship store within a namespace. Each vault maintains its own cryptographic chain (state_root, previous_hash, block height).
92-
- **Entity**: Key-value data stored in a namespace (users, teams, clients, sessions). Supports TTL, versioning, conditional writes.
93-
- **Relationship**: Authorization tuple in a vault: (resource, relation, subject). Used by Engine for permission checks.
94-
- **_system namespace**: Global data (user accounts, namespace routing), replicated to all nodes.
95-
- **Shard**: Multiple namespaces share a Raft group for efficiency. Vaults within a shard have independent cryptographic chains.
90+
- **Organization**: Top-level tenant isolation boundary. Sequential `OrganizationId(i64)` for storage, Snowflake `OrganizationSlug(u64)` for external APIs.
91+
- **Vault**: Relationship store within an organization. Each vault maintains its own cryptographic chain (state_root, previous_hash, block height).
92+
- **Entity**: Key-value data stored in an organization. Supports TTL, versioning, conditional writes.
93+
- **Relationship**: Authorization tuple in a vault: (resource, relation, subject).
94+
- **User**: Identity with email, role, status, and token version. Stored in `_system` organization.
95+
- **App**: Organization-scoped client application with vault connections.
96+
- **SigningKey**: Ed25519 JWT signing key with scope (Global/Organization) and lifecycle (Active/Rotated/Revoked).
97+
- **RefreshToken**: Session token family with rotate-on-use and poison detection.
98+
- **Team**: Organization-scoped user group.
99+
- **Shard**: Multiple organizations sharing a Raft group. Vaults within a shard have independent cryptographic chains.
96100
97101
## Architecture Highlights
98102
- Hybrid storage: State commitment (merkleized) is separate from state storage (fast K/V) to avoid write amplification.
99103
- Bucket-based state roots: 256 buckets with incremental hashing, O(k) complexity where k = dirty keys.
100104
- Per-vault failure isolation: Divergence in one vault doesn't cascade to other vaults in the same shard.
101-
- Leader-assigned sequential IDs: Deterministic for Raft replay (NamespaceId, VaultId, UserId are all int64).
105+
- Dual-ID architecture: Internal sequential IDs (`i64`) for storage, Snowflake slugs (`u64`) for APIs. `SlugResolver` translates at gRPC boundaries.
106+
- JWT token infrastructure: Ed25519 signing with envelope encryption (AES-256-GCM + AES-KWP), ArcSwap lock-free key cache.
102107
103108
## Key Design Decisions
104109
- All operations are idempotent, resolved by Raft total ordering.
105110
- SHA-256 for all cryptographic commitments; seahash for bucket assignment.
106111
- Embedded storage backend (single-file, ACID, COW B-trees).
107112
- Pagination tokens are opaque, HMAC-signed, and include height for consistent reads.
113+
- Server crates use snafu for error handling; SDK uses thiserror.
108114
109-
See DESIGN.md for full specifications.
115+
See DESIGN.md for full specifications. See AGENTS.md for development conventions.
110116
# the name by which the project can be referenced within Serena
111117
project_name: "InferaDB Ledger"
112118

@@ -146,3 +152,8 @@ language_backend:
146152
# list of regex patterns which, when matched, mark a memory entry as read‑only.
147153
# Extends the list from the global configuration, merging the two lists.
148154
read_only_memory_patterns: []
155+
156+
# line ending convention to use when writing source files.
157+
# Possible values: unset (use global setting), "lf", "crlf", or "native" (platform default)
158+
# This does not affect Serena's own files (e.g. memories and configuration files), which always use native line endings.
159+
line_ending:

0 commit comments

Comments
 (0)