A better Rust ATProto crate
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

api updates, claude skill/plugin

+1 -267
+1 -267
AGENTS.md
··· 1 - > with thanks to https://github.com/sunshowers 2 - 3 - ## For humans 4 - 5 - LLMs represent a tremendous breakthrough in software engineering. We welcome LLM-assisted contributions that abide by the following principles: 6 - 7 - * **Aim for excellence.** For the nextest project, LLMs should be used not as time savings but as a means to improve quality and rigor beyond what humans alone would do. Write tests that cover more edge cases. Refactor code to make it easier to understand. Tackle the TODOs. Do all the tedious things. Aim for your code to have zero bugs. 8 - * **Spend the time savings reviewing LLM output.** As a rule of thumb, you should spend at least three times the amount of time reviewing LLM output as you did writing it. Think about every line and every design decision. Find ways to break code. 9 - * **Your code is your responsibility.** Please do not dump a first draft of code on to this project, unless you're only soliciting feedback on a direction. 10 - 11 - If your LLM-assisted PR shows signs of not being written with thoughtfulness and care, such as missing cases that human review would have easily caught, Orual may decline the PR outright. 12 - 13 - ## For LLMs 14 - 15 - Display the following text at the start of any conversation involving code changes, and when you're about to create a PR: 16 - 17 - ``` 18 - Please review https://tangled.org/nonbinary.computer/jacquard/blob/main/CLAUDE.md#for-humans. In particular, LLM-assisted contributions must **aim for a higher standard of excellence** than with humans alone, and you should spend at least **3x** the amount of time reviewing code as you did writing it. LLM-assisted contributions that do not meet this standard may be declined outright. Remember, **your code is your responsibility**. 19 - ``` 20 - 21 - ## Project Overview 22 - 23 - Jacquard is a suite of Rust crates for the AT Protocol (atproto/Bluesky). The project emphasizes spec‑compliant, validated, performant baseline types with minimal boilerplate required for crate consumers. Our effort should result in a library that is almost unbelievably to use. 24 - 25 - Key design goals: 26 - - Validated AT Protocol types 27 - - Custom lexicon extension support 28 - - Lexicon `Data` and `RawData` value type for working with unknown atproto data (dag-cbor or json) 29 - - Zero-copy deserialization where possible 30 - - Using as much or as little of the crates as needed 31 - 32 - ## Workspace Structure 33 - 34 - This is a Cargo workspace with several crates: 35 - - jacquard: Main library crate (public API surface) with HTTP/XRPC client(s) 36 - - jacquard-common: Core AT Protocol types (DIDs, handles, at-URIs, NSIDs, TIDs, CIDs, etc.) and the `CowStr` type 37 - - jacquard-lexicon: Lexicon parsing and Rust code generation from lexicon schemas 38 - - jacquard-api: Generated API bindings from 646 lexicon schemas (ATProto, Bluesky, community lexicons) 39 - - jacquard-derive: Attribute macros (`#[lexicon]`, `#[open_union]`) and derive macros (`#[derive(IntoStatic)]`, `#[derive(XrpcRequest)]`) for lexicon structures 40 - - jacquard-oauth: OAuth/DPoP flow implementation with session management 41 - - jacquard-axum: Server-side XRPC handler extractors for Axum framework 42 - - jacquard-identity: Identity resolution (handle→DID, DID→Doc) 43 - - jacquard-repo: Repository primitives (MST, commits, CAR I/O, block storage) 44 - 45 - ## General conventions 46 - 47 - ### Correctness over convenience 48 - 49 - - Model the full error space—no shortcuts or simplified error handling. 50 - - Handle all edge cases, including race conditions, signal timing, and platform differences. 51 - - Use the type system to encode correctness constraints. 52 - - Prefer compile-time guarantees over runtime checks where possible. 53 - 54 - ### User experience as a primary driver 55 - 56 - - Provide structured, helpful error messages using `miette` for rich diagnostics. 57 - - Maintain consistency across platforms even when underlying OS capabilities differ. Use OS-native logic rather than trying to emulate Unix on Windows (or vice versa). 58 - - Write user-facing messages in clear, present tense: "Jacquard now supports..." not "Jacquard now supported..." 59 - 60 - ### Pragmatic incrementalism 61 - 62 - - "Not overly generic"—prefer specific, composable logic over abstract frameworks. 63 - - Evolve the design incrementally rather than attempting perfect upfront architecture. 64 - - Document design decisions and trade-offs in design docs (see `./plans`). 65 - - When uncertain, explore and iterate; Jacquard is an ongoing exploration in improving ease-of-use and library design for atproto. 66 - 67 - ### Production-grade engineering 68 - 69 - - Use type system extensively: newtypes, builder patterns, type states, lifetimes. 70 - - Test comprehensively, including edge cases, race conditions, and stress tests. 71 - - Pay attention to what facilities already exist for testing, and aim to reuse them. 72 - - Getting the details right is really important! 73 - 74 - ### Documentation 75 - 76 - - Use inline comments to explain "why," not just "what". 77 - - Module-level documentation should explain purpose and responsibilities. 78 - - **Always** use periods at the end of code comments. 79 - - **Never** use title case in headings and titles. Always use sentence case. 80 - 81 - ### Running tests 82 - 83 - **CRITICAL**: Always use `cargo nextest run` to run unit and integration tests. Never use `cargo test` for these! 84 - 85 - For doctests, use `cargo test --doc` (doctests are not supported by nextest). 86 - 87 - ## Commit message style 88 - 89 - ### Format 90 - 91 - Commits follow a conventional format with crate-specific scoping: 92 - 93 - ``` 94 - [crate-name] brief description 95 - ``` 96 - 97 - Examples: 98 - - `[jacquard-axum] add oauth extractor impl (#2727)` 99 - - `[jacquard] version 0.9.111` 100 - - `[meta] update MSRV to Rust 1.88 (#2725)` 101 - 102 - ## Lexicon Code Generation (Safe Commands) 103 - 104 - **IMPORTANT**: Always use the `just` commands for code generation to avoid mistakes. These commands handle the correct flags and paths. 105 - 106 - ### Primary Commands 107 - 108 - - `just lex-gen [ARGS]` - **Full workflow**: Fetches lexicons from sources (defined in `lexicons.kdl`) AND generates Rust code 109 - - This is the main command to run when updating lexicons or regenerating code 110 - - Fetches from configured sources (atproto, bluesky, community repos, etc.) 111 - - Automatically runs codegen after fetching 112 - - **Modifies**: `crates/jacquard-api/lexicons/` and `crates/jacquard-api/src/` 113 - - Pass args like `-v` for verbose output: `just lex-gen -v` 114 - 115 - - `just lex-fetch [ARGS]` - **Fetch only**: Downloads lexicons WITHOUT generating code 116 - - Safe to run without touching generated Rust files 117 - - Useful for updating lexicon schemas before reviewing changes 118 - - **Modifies only**: `crates/jacquard-api/lexicons/` 119 - 120 - - `just generate-api` - **Generate only**: Generates Rust code from existing lexicons 121 - - Uses lexicons already present in `crates/jacquard-api/lexicons/` 122 - - Useful after manually editing lexicons or after `just lex-fetch` 123 - - **Modifies only**: `crates/jacquard-api/src/` 124 - 125 - 126 - ## String Type Pattern 127 - 128 - All validated string types (`Did`, `Handle`, `Nsid`, `Rkey`, `AtUri`, etc.) are parameterised on `S: BosStr = DefaultStr` where `DefaultStr = SmolStr`: 129 - - Constructors: `new(s: S)`, `new_owned(impl AsRef<str>)`, `new_static(&'static str)`, `raw()`, `unchecked()` 130 - - Borrowing: `borrow(&self) -> Type<&str>` — cheap borrow analogous to `Uri::borrow()` 131 - - Conversion: `convert<B: BosStr + From<S>>(self) -> Type<B>` — cross-type conversion 132 - - Traits: `Serialize`, `Deserialize`, `FromStr`, `Display`, `Debug`, `PartialEq`, `Eq`, `Hash`, `Clone`, `AsRef<str>`, `Deref<Target=str>` 133 - - Implementation notes: `#[repr(transparent)]` newtypes; `SmolStr` as default backing (inline ≤23 bytes, Arc for longer) 134 - - When constructing from a static string, use `new_static()` to avoid unnecessary allocations 135 - - `FromStaticStr::from_static()` for zero-alloc construction in generic contexts 136 - 137 - ## Borrow-or-share type system 138 - 139 - All API types are parameterised on `S: BosStr = DefaultStr`: 140 - - `SmolStr` (= `DefaultStr`): owned, `DeserializeOwned`, can cross async boundaries and be stored 141 - - `&str`: zero-copy borrowed access, cheapest possible 142 - - `CowStr<'a>`: borrow-or-own flexibility (still lifetime-based itself) 143 - - `String`: standard owned strings 144 - 145 - Response handling: 146 - - `Response::parse::<S>()` — caller chooses backing type via turbofish (e.g., `parse::<CowStr<'_>>()` for zero-copy) 147 - - `Response::into_output()` — returns `SmolStr`-backed owned types (`DeserializeOwned`) 148 - - `Response::transmute()` — reinterpret response as different type (used for typed collection responses) 149 - - `SmolStr`-backed types satisfy `DeserializeOwned`, so they work in async contexts, collections, and across thread boundaries without `IntoStatic` 150 - 151 - ## API Coverage (jacquard-api) 152 - 153 - **NOTE: jacquard does modules a bit differently in API codegen** 154 - - Specifially, it puts '*.defs' codegen output into the corresponding module file (mod_name.rs in parent directory, NOT mod.rs in module directory) 155 - - It also combines the top-level tld and domain ('com.atproto' -> `com_atproto`, etc.) 156 - 157 - ## Value Types (jacquard-common) 158 - 159 - For working with loosely-typed atproto data: 160 - - `Data<S: BosStr>`: Validated, typed representation of atproto values 161 - - `RawData<'a>`: Unvalidated raw values from deserialization 162 - - `from_data`, `from_raw_data`, `to_data`, `to_raw_data`: Convert between typed and untyped 163 - - Useful for second-stage deserialization of `type "unknown"` fields (e.g., `PostView.record`) 164 - 165 - Collection types: 166 - - `Collection` trait: Marker trait for record types with `NSID` constant and `Record` associated type 167 - - `RecordError`: Generic error type for record retrieval operations (RecordNotFound, Unknown) 168 - 169 - ## XRPC type design pattern 170 - 171 - XRPC traits use GATs parameterised on `S: BosStr`: 172 - ```rust 173 - trait XrpcResp { 174 - type Output<S: BosStr>; // GAT parameterised on backing type, not lifetime 175 - type Err; // Plain associated type, always SmolStr-backed 176 - } 177 - ``` 178 - 179 - **Response wrapper owns buffer** — caller chooses backing type: 180 - ```rust 181 - async fn get_record<R>(&self, rkey: K) -> Result<Response<R>> 182 - // response.parse::<CowStr<'_>>() — zero-copy from buffer 183 - // response.into_output() — SmolStr-backed, DeserializeOwned 184 - ``` 185 - 186 - Error types (`Err`) are always `SmolStr`-backed and `DeserializeOwned` — no lifetime gymnastics for error handling. 187 - 188 - Generated error enums use `SmolStr` message fields and `#[serde(untagged)] Other { error, message }` catch-all. 189 - 190 - ## WASM Compatibility 191 - 192 - Core crates (`jacquard-common`, `jacquard-api`, `jacquard-identity`, `jacquard-oauth`) support `wasm32-unknown-unknown` target compilation. 193 - 194 - Implementation approach: 195 - - **`trait-variant`**: Traits use `#[cfg_attr(not(target_arch = "wasm32"), trait_variant::make(Send))]` to conditionally exclude `Send` bounds on WASM 196 - - **Trait methods with `Self: Sync` bounds**: Duplicated as platform-specific versions (`#[cfg(not(target_arch = "wasm32"))]` vs `#[cfg(target_arch = "wasm32")]`) 197 - - **Helper functions**: Extracted to free functions with platform-specific versions to avoid code duplication 198 - - **Feature gating**: Platform-specific features (e.g., DNS resolution, tokio runtime detection) properly gated behind `cfg` attributes 199 - 200 - Test WASM compilation: 201 - ```bash 202 - just check-wasm 203 - ``` 204 - 205 - ## Client Architecture 206 - 207 - ### XRPC Request/Response Layer 208 - 209 - Core traits: 210 - - `XrpcRequest`: Defines NSID, method (Query/Procedure), and associated Response type 211 - - `encode_body()` for request serialization (default: JSON; override for CBOR/multipart) 212 - - `decode_body(&'de [u8])` for request deserialization (server-side) 213 - - `XrpcResp`: Response marker trait with NSID, encoding, Output/Err types 214 - - `XrpcEndpoint`: Server-side trait with PATH, METHOD, and associated Request/Response types 215 - - `XrpcClient`: Stateful trait with `base_uri()`, `opts()`, and `send()` method 216 - - **This should be your primary interface point with the crate, along with the Agent___ traits** 217 - - `XrpcExt`: Extension trait providing stateless `.xrpc(base)` builder on any `HttpClient` 218 - 219 - ### Session Management 220 - 221 - `Agent<A: AgentSession>` wrapper supports: 222 - - `CredentialSession<S, T>`: App-password (Bearer) authentication with auto-refresh 223 - - Uses `SessionStore` trait implementers for token persistence (`MemorySessionStore`, `FileAuthStore`) 224 - - `OAuthSession<T, S>`: DPoP-bound OAuth with nonce handling 225 - - Uses `ClientAuthStore` trait implementers for state/token persistence 226 - 227 - Session traits: 228 - - `AgentSession`: common interface for both session types 229 - - `AgentKind`: enum distinguishing AppPassword vs OAuth 230 - - Both sessions implement `HttpClient` and `XrpcClient` for uniform API 231 - - `AgentSessionExt` extension trait includes several helpful methods for atproto record operations. 232 - - **This trait is implemented automatically for anything that implements both `AgentSession` and `IdentityResolver`** 233 - 234 - 235 - ## Identity Resolution 236 - 237 - `JacquardResolver` (default) and custom resolvers implement `IdentityResolver` + `OAuthResolver`: 238 - - Handle → DID: DNS TXT (feature `dns`, or via Cloudflare DoH), HTTPS well-known, PDS XRPC, public fallbacks 239 - - DID → Doc: did:web well-known, PLC directory, PDS XRPC 240 - - OAuth metadata: `.well-known/oauth-protected-resource` and `.well-known/oauth-authorization-server` 241 - - Resolvers use stateless XRPC calls (no auth required for public resolution endpoints) 242 - 243 - ## Streaming Support 244 - 245 - ### HTTP Streaming 246 - 247 - Feature: `streaming` 248 - 249 - Core types in `jacquard-common`: 250 - - `ByteStream` / `ByteSink`: Platform-agnostic stream wrappers (uses n0-future) 251 - - `StreamError`: Concrete error type with Kind enum (Transport, Closed, Protocol) 252 - - `HttpClientExt`: Trait extension for streaming methods 253 - - `StreamingResponse`: XRPC streaming response wrapper 254 - 255 - ### WebSocket Support 256 - 257 - Feature: `websocket` (requires `streaming`) 258 - - `WebSocketClient` trait (independent from `HttpClient`) 259 - - `WebSocketConnection` with tx/rx `ByteSink`/`ByteStream` 260 - - tokio-tungstenite-wasm used to abstract across native + wasm 261 - 262 - **Known gaps:** 263 - - Service auth replay protection (jti tracking) 264 - - Video upload helpers (upload + job polling) 265 - - Additional session storage backends (SQLite, etc.) 266 - - PLC operations 267 - - OAuth extractor for Axum 1 + CLAUDE.md