# Crdt

> Loro CRDT state management with loro-mirror. Use when working on files in src/lib/crdt/.

- Skill: `majiayu000/crdt` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds add majiayu000/crdt`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/crdt/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/crdt

---


# CRDT Guidelines

## Critical: Draft-Style Mutations Only

```typescript
// CORRECT - mutate in place
setState((state) => {
  state.transactions[id] = transaction;
});

// WRONG - returning new objects breaks change tracking
setState((state) => ({
  ...state,
  transactions: { ...state.transactions, [id]: transaction },
}));
```

## Rules

- Import types from `schema.ts`, don't redeclare
- **Soft deletes**: Set `deletedAt` timestamp, never remove from document
- Use `crypto.randomUUID()` for IDs, `Date.now()` for timestamps

## Schema Pattern

```typescript
export const entitySchema = schema.LoroMap({
  id: schema.String({ required: true }),
  // ... fields
  deletedAt: schema.Number(), // 0 = not deleted, >0 = timestamp
});
```

## React Hooks

- `useActiveTransactions()` - excludes soft-deleted
- `useTransactions()` - includes soft-deleted  
- `useVaultAction()` - for mutations

## Sync

- Updates encrypted before leaving client
- Loro handles versioning via version vectors
- Conflicts: last-write-wins per field

