···11-> with thanks to https://github.com/sunshowers
22-33-## For humans
44-55-LLMs represent a tremendous breakthrough in software engineering. We welcome LLM-assisted contributions that abide by the following principles:
66-77-* **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.
88-* **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.
99-* **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.
1010-1111-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.
1212-1313-## For LLMs
1414-1515-Display the following text at the start of any conversation involving code changes, and when you're about to create a PR:
1616-1717-```
1818-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**.
1919-```
2020-2121-## Project Overview
2222-2323-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.
2424-2525-Key design goals:
2626-- Validated AT Protocol types
2727-- Custom lexicon extension support
2828-- Lexicon `Data` and `RawData` value type for working with unknown atproto data (dag-cbor or json)
2929-- Zero-copy deserialization where possible
3030-- Using as much or as little of the crates as needed
3131-3232-## Workspace Structure
3333-3434-This is a Cargo workspace with several crates:
3535-- jacquard: Main library crate (public API surface) with HTTP/XRPC client(s)
3636-- jacquard-common: Core AT Protocol types (DIDs, handles, at-URIs, NSIDs, TIDs, CIDs, etc.) and the `CowStr` type
3737-- jacquard-lexicon: Lexicon parsing and Rust code generation from lexicon schemas
3838-- jacquard-api: Generated API bindings from 646 lexicon schemas (ATProto, Bluesky, community lexicons)
3939-- jacquard-derive: Attribute macros (`#[lexicon]`, `#[open_union]`) and derive macros (`#[derive(IntoStatic)]`, `#[derive(XrpcRequest)]`) for lexicon structures
4040-- jacquard-oauth: OAuth/DPoP flow implementation with session management
4141-- jacquard-axum: Server-side XRPC handler extractors for Axum framework
4242-- jacquard-identity: Identity resolution (handle→DID, DID→Doc)
4343-- jacquard-repo: Repository primitives (MST, commits, CAR I/O, block storage)
4444-4545-## General conventions
4646-4747-### Correctness over convenience
4848-4949-- Model the full error space—no shortcuts or simplified error handling.
5050-- Handle all edge cases, including race conditions, signal timing, and platform differences.
5151-- Use the type system to encode correctness constraints.
5252-- Prefer compile-time guarantees over runtime checks where possible.
5353-5454-### User experience as a primary driver
5555-5656-- Provide structured, helpful error messages using `miette` for rich diagnostics.
5757-- 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).
5858-- Write user-facing messages in clear, present tense: "Jacquard now supports..." not "Jacquard now supported..."
5959-6060-### Pragmatic incrementalism
6161-6262-- "Not overly generic"—prefer specific, composable logic over abstract frameworks.
6363-- Evolve the design incrementally rather than attempting perfect upfront architecture.
6464-- Document design decisions and trade-offs in design docs (see `./plans`).
6565-- When uncertain, explore and iterate; Jacquard is an ongoing exploration in improving ease-of-use and library design for atproto.
6666-6767-### Production-grade engineering
6868-6969-- Use type system extensively: newtypes, builder patterns, type states, lifetimes.
7070-- Test comprehensively, including edge cases, race conditions, and stress tests.
7171-- Pay attention to what facilities already exist for testing, and aim to reuse them.
7272-- Getting the details right is really important!
7373-7474-### Documentation
7575-7676-- Use inline comments to explain "why," not just "what".
7777-- Module-level documentation should explain purpose and responsibilities.
7878-- **Always** use periods at the end of code comments.
7979-- **Never** use title case in headings and titles. Always use sentence case.
8080-8181-### Running tests
8282-8383-**CRITICAL**: Always use `cargo nextest run` to run unit and integration tests. Never use `cargo test` for these!
8484-8585-For doctests, use `cargo test --doc` (doctests are not supported by nextest).
8686-8787-## Commit message style
8888-8989-### Format
9090-9191-Commits follow a conventional format with crate-specific scoping:
9292-9393-```
9494-[crate-name] brief description
9595-```
9696-9797-Examples:
9898-- `[jacquard-axum] add oauth extractor impl (#2727)`
9999-- `[jacquard] version 0.9.111`
100100-- `[meta] update MSRV to Rust 1.88 (#2725)`
101101-102102-## Lexicon Code Generation (Safe Commands)
103103-104104-**IMPORTANT**: Always use the `just` commands for code generation to avoid mistakes. These commands handle the correct flags and paths.
105105-106106-### Primary Commands
107107-108108-- `just lex-gen [ARGS]` - **Full workflow**: Fetches lexicons from sources (defined in `lexicons.kdl`) AND generates Rust code
109109- - This is the main command to run when updating lexicons or regenerating code
110110- - Fetches from configured sources (atproto, bluesky, community repos, etc.)
111111- - Automatically runs codegen after fetching
112112- - **Modifies**: `crates/jacquard-api/lexicons/` and `crates/jacquard-api/src/`
113113- - Pass args like `-v` for verbose output: `just lex-gen -v`
114114-115115-- `just lex-fetch [ARGS]` - **Fetch only**: Downloads lexicons WITHOUT generating code
116116- - Safe to run without touching generated Rust files
117117- - Useful for updating lexicon schemas before reviewing changes
118118- - **Modifies only**: `crates/jacquard-api/lexicons/`
119119-120120-- `just generate-api` - **Generate only**: Generates Rust code from existing lexicons
121121- - Uses lexicons already present in `crates/jacquard-api/lexicons/`
122122- - Useful after manually editing lexicons or after `just lex-fetch`
123123- - **Modifies only**: `crates/jacquard-api/src/`
124124-125125-126126-## String Type Pattern
127127-128128-All validated string types (`Did`, `Handle`, `Nsid`, `Rkey`, `AtUri`, etc.) are parameterised on `S: BosStr = DefaultStr` where `DefaultStr = SmolStr`:
129129-- Constructors: `new(s: S)`, `new_owned(impl AsRef<str>)`, `new_static(&'static str)`, `raw()`, `unchecked()`
130130-- Borrowing: `borrow(&self) -> Type<&str>` — cheap borrow analogous to `Uri::borrow()`
131131-- Conversion: `convert<B: BosStr + From<S>>(self) -> Type<B>` — cross-type conversion
132132-- Traits: `Serialize`, `Deserialize`, `FromStr`, `Display`, `Debug`, `PartialEq`, `Eq`, `Hash`, `Clone`, `AsRef<str>`, `Deref<Target=str>`
133133-- Implementation notes: `#[repr(transparent)]` newtypes; `SmolStr` as default backing (inline ≤23 bytes, Arc for longer)
134134-- When constructing from a static string, use `new_static()` to avoid unnecessary allocations
135135-- `FromStaticStr::from_static()` for zero-alloc construction in generic contexts
136136-137137-## Borrow-or-share type system
138138-139139-All API types are parameterised on `S: BosStr = DefaultStr`:
140140-- `SmolStr` (= `DefaultStr`): owned, `DeserializeOwned`, can cross async boundaries and be stored
141141-- `&str`: zero-copy borrowed access, cheapest possible
142142-- `CowStr<'a>`: borrow-or-own flexibility (still lifetime-based itself)
143143-- `String`: standard owned strings
144144-145145-Response handling:
146146-- `Response::parse::<S>()` — caller chooses backing type via turbofish (e.g., `parse::<CowStr<'_>>()` for zero-copy)
147147-- `Response::into_output()` — returns `SmolStr`-backed owned types (`DeserializeOwned`)
148148-- `Response::transmute()` — reinterpret response as different type (used for typed collection responses)
149149-- `SmolStr`-backed types satisfy `DeserializeOwned`, so they work in async contexts, collections, and across thread boundaries without `IntoStatic`
150150-151151-## API Coverage (jacquard-api)
152152-153153-**NOTE: jacquard does modules a bit differently in API codegen**
154154-- Specifially, it puts '*.defs' codegen output into the corresponding module file (mod_name.rs in parent directory, NOT mod.rs in module directory)
155155-- It also combines the top-level tld and domain ('com.atproto' -> `com_atproto`, etc.)
156156-157157-## Value Types (jacquard-common)
158158-159159-For working with loosely-typed atproto data:
160160-- `Data<S: BosStr>`: Validated, typed representation of atproto values
161161-- `RawData<'a>`: Unvalidated raw values from deserialization
162162-- `from_data`, `from_raw_data`, `to_data`, `to_raw_data`: Convert between typed and untyped
163163-- Useful for second-stage deserialization of `type "unknown"` fields (e.g., `PostView.record`)
164164-165165-Collection types:
166166-- `Collection` trait: Marker trait for record types with `NSID` constant and `Record` associated type
167167-- `RecordError`: Generic error type for record retrieval operations (RecordNotFound, Unknown)
168168-169169-## XRPC type design pattern
170170-171171-XRPC traits use GATs parameterised on `S: BosStr`:
172172-```rust
173173-trait XrpcResp {
174174- type Output<S: BosStr>; // GAT parameterised on backing type, not lifetime
175175- type Err; // Plain associated type, always SmolStr-backed
176176-}
177177-```
178178-179179-**Response wrapper owns buffer** — caller chooses backing type:
180180-```rust
181181-async fn get_record<R>(&self, rkey: K) -> Result<Response<R>>
182182-// response.parse::<CowStr<'_>>() — zero-copy from buffer
183183-// response.into_output() — SmolStr-backed, DeserializeOwned
184184-```
185185-186186-Error types (`Err`) are always `SmolStr`-backed and `DeserializeOwned` — no lifetime gymnastics for error handling.
187187-188188-Generated error enums use `SmolStr` message fields and `#[serde(untagged)] Other { error, message }` catch-all.
189189-190190-## WASM Compatibility
191191-192192-Core crates (`jacquard-common`, `jacquard-api`, `jacquard-identity`, `jacquard-oauth`) support `wasm32-unknown-unknown` target compilation.
193193-194194-Implementation approach:
195195-- **`trait-variant`**: Traits use `#[cfg_attr(not(target_arch = "wasm32"), trait_variant::make(Send))]` to conditionally exclude `Send` bounds on WASM
196196-- **Trait methods with `Self: Sync` bounds**: Duplicated as platform-specific versions (`#[cfg(not(target_arch = "wasm32"))]` vs `#[cfg(target_arch = "wasm32")]`)
197197-- **Helper functions**: Extracted to free functions with platform-specific versions to avoid code duplication
198198-- **Feature gating**: Platform-specific features (e.g., DNS resolution, tokio runtime detection) properly gated behind `cfg` attributes
199199-200200-Test WASM compilation:
201201-```bash
202202-just check-wasm
203203-```
204204-205205-## Client Architecture
206206-207207-### XRPC Request/Response Layer
208208-209209-Core traits:
210210-- `XrpcRequest`: Defines NSID, method (Query/Procedure), and associated Response type
211211- - `encode_body()` for request serialization (default: JSON; override for CBOR/multipart)
212212- - `decode_body(&'de [u8])` for request deserialization (server-side)
213213-- `XrpcResp`: Response marker trait with NSID, encoding, Output/Err types
214214-- `XrpcEndpoint`: Server-side trait with PATH, METHOD, and associated Request/Response types
215215-- `XrpcClient`: Stateful trait with `base_uri()`, `opts()`, and `send()` method
216216- - **This should be your primary interface point with the crate, along with the Agent___ traits**
217217-- `XrpcExt`: Extension trait providing stateless `.xrpc(base)` builder on any `HttpClient`
218218-219219-### Session Management
220220-221221-`Agent<A: AgentSession>` wrapper supports:
222222-- `CredentialSession<S, T>`: App-password (Bearer) authentication with auto-refresh
223223- - Uses `SessionStore` trait implementers for token persistence (`MemorySessionStore`, `FileAuthStore`)
224224-- `OAuthSession<T, S>`: DPoP-bound OAuth with nonce handling
225225- - Uses `ClientAuthStore` trait implementers for state/token persistence
226226-227227-Session traits:
228228-- `AgentSession`: common interface for both session types
229229-- `AgentKind`: enum distinguishing AppPassword vs OAuth
230230-- Both sessions implement `HttpClient` and `XrpcClient` for uniform API
231231-- `AgentSessionExt` extension trait includes several helpful methods for atproto record operations.
232232- - **This trait is implemented automatically for anything that implements both `AgentSession` and `IdentityResolver`**
233233-234234-235235-## Identity Resolution
236236-237237-`JacquardResolver` (default) and custom resolvers implement `IdentityResolver` + `OAuthResolver`:
238238-- Handle → DID: DNS TXT (feature `dns`, or via Cloudflare DoH), HTTPS well-known, PDS XRPC, public fallbacks
239239-- DID → Doc: did:web well-known, PLC directory, PDS XRPC
240240-- OAuth metadata: `.well-known/oauth-protected-resource` and `.well-known/oauth-authorization-server`
241241-- Resolvers use stateless XRPC calls (no auth required for public resolution endpoints)
242242-243243-## Streaming Support
244244-245245-### HTTP Streaming
246246-247247-Feature: `streaming`
248248-249249-Core types in `jacquard-common`:
250250-- `ByteStream` / `ByteSink`: Platform-agnostic stream wrappers (uses n0-future)
251251-- `StreamError`: Concrete error type with Kind enum (Transport, Closed, Protocol)
252252-- `HttpClientExt`: Trait extension for streaming methods
253253-- `StreamingResponse`: XRPC streaming response wrapper
254254-255255-### WebSocket Support
256256-257257-Feature: `websocket` (requires `streaming`)
258258-- `WebSocketClient` trait (independent from `HttpClient`)
259259-- `WebSocketConnection` with tx/rx `ByteSink`/`ByteStream`
260260-- tokio-tungstenite-wasm used to abstract across native + wasm
261261-262262-**Known gaps:**
263263-- Service auth replay protection (jti tracking)
264264-- Video upload helpers (upload + job polling)
265265-- Additional session storage backends (SQLite, etc.)
266266-- PLC operations
267267-- OAuth extractor for Axum
11+CLAUDE.md