datasphere 0.1.1

Background daemon that distills knowledge from Claude Code sessions into a searchable graph
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# Datasphere P2P Sharing Spec

**Status:** Draft v3
**Author:** Planning session 2026-01-07

## Overview

Enable Datasphere instances to share knowledge graphs peer-to-peer over the internet, creating a "superlocal mesh" of shared context without central servers.

### Goals

1. **Decentralized** - No central server, direct peer connections with relay fallback
2. **Internet-native** - Works across NAT, firewalls, different networks
3. **Privacy-first** - Project-level sharing control, private by default
4. **Simple** - Publisher/subscriber model, one-way sync, no conflicts
5. **Isolated** - Each peer's data in separate DB, source always clear

### Non-Goals (v1)

- Real-time streaming
- Fine-grained (node-level) sharing control
- Bi-directional conflict resolution
- Update/deletion propagation (subscribers get snapshots)
- Access control beyond peer lists

## Architecture

```
┌─────────────────────────────────────────────────────────────────┐
│                     Datasphere Instance                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌───────────────────────────────────────────────────────────┐  │
│  │                    Local Store (all nodes)                │  │
│  │  ┌─────────────────────┐  ┌─────────────────────────────┐ │  │
│  │  │   Private projects  │  │   Shared projects           │ │  │
│  │  │   (never synced)    │  │   (filtered for publish)    │─┼──┼──▶ Peers
│  │  └─────────────────────┘  └─────────────────────────────┘ │  │
│  └───────────────────────────────────────────────────────────┘  │
│                                                                 │
│  ┌───────────────────────────────────────────────────────────┐  │
│  │              Peer Stores (read-only snapshots)            │  │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐       │  │
│  │  │  Peer A     │  │  Peer B     │  │  Peer C     │       │  │
│  │  │  nodes.lance│  │  nodes.lance│  │  nodes.lance│       │  │
│  │  └─────────────┘  └─────────────┘  └─────────────┘       │  │
│  └───────────────────────────────────────────────────────────┘  │
│                              │                                  │
│                       ┌──────▼──────┐                          │
│                       │ Query Layer │                          │
│                       │  (fan-out)  │                          │
│                       └─────────────┘                          │
│                                                                 │
├─────────────────────────────────────────────────────────────────┤
│                        Transport (iroh-net)                     │
│           QUIC + NAT traversal + relay fallback                 │
└─────────────────────────────────────────────────────────────────┘
```

## Storage Layout

```
~/.datasphere/
├── db/                              # Local database (all nodes)
│   ├── nodes.lance/
│   └── processed.lance/
├── peers/                           # Peer replicas (read-only)
│   ├── <node_id_1>/
│   │   ├── nodes.lance/
│   │   └── meta.json
│   └── <node_id_2>/
│       ├── nodes.lance/
│       └── meta.json
├── identity.key                     # Ed25519 keypair (iroh NodeId)
├── share.json                       # Which projects to publish
├── peers.json                       # Peer configuration
└── queue.jsonl                      # Existing job queue
```

## Configuration Files

### identity.key

Generated by iroh-net. Contains the node's cryptographic identity. The **NodeId** (public key) is the stable address used to connect to this instance from anywhere on the internet.

### share.json

Controls which projects are published to peers. Everything else is private.

```json
{
  "shared_projects": [
    "/Users/drazen/playground/ai-omnibus",
    "/Users/drazen/oss/my-library"
  ]
}
```

**Behavior:**
- Paths are canonicalized on add (symlinks resolved, absolute paths)
- Nodes are matched by `source` field prefix
- Only nodes from shared projects are sent to peers
- Private project nodes never leave the machine
- Default: empty list (share nothing)

**Privacy note:** The `source` field (containing project paths) is visible to peers. This reveals your filesystem structure for shared projects. If this is a concern, use a dedicated directory structure for shareable projects.

### peers.json

```json
{
  "node_id": "abc123...",
  "peers": {
    "def456...": {
      "name": "alice",
      "subscribe": true,
      "publish": true,
      "last_sync": "2026-01-07T10:30:00Z"
    },
    "ghi789...": {
      "name": "bob",
      "subscribe": true,
      "publish": false,
      "last_sync": null
    }
  }
}
```

**Fields:**
- `node_id`: This instance's iroh NodeId
- `subscribe`: Pull their shared nodes into `peers/<id>/`
- `publish`: Allow them to pull our shared nodes

### peers/<node_id>/meta.json

```json
{
  "node_id": "def456...",
  "name": "alice",
  "last_sync": "2026-01-07T10:30:00Z",
  "node_count": 1523,
  "sync_cursor": "2026-01-07T10:25:00Z"
}
```

## Transport: iroh-net

[iroh-net](https://iroh.computer/) provides:

- **QUIC connections** - Fast, multiplexed, encrypted
- **NAT traversal** - Hole punching for direct connections
- **Relay fallback** - When direct connection fails, traffic routes through relay
- **Stable identity** - NodeId works regardless of IP changes

### Why iroh-net

| Requirement | iroh-net solution |
|-------------|-------------------|
| Internet-wide connectivity | Built-in NAT traversal + relay |
| No central server | Relay is optional fallback, not required |
| Stable addressing | NodeId (public key) is the address |
| Rust-native | First-class Rust support |
| Encrypted | QUIC provides TLS 1.3 |

### Connection Flow

```
Subscriber                    iroh relay                    Publisher
    │                            │                              │
    │── connect(NodeId) ────────▶│                              │
    │                            │◀── register ─────────────────│
    │                            │                              │
    │◀─────────── hole punch / relay ─────────────────────────▶│
    │                            │                              │
    │◀═══════════ QUIC connection (direct or relayed) ════════▶│
```

### Relay Considerations

The iroh project provides a public relay network. For production/team use, consider:
- Self-hosted relay for reliability and privacy
- See [iroh relay docs]https://docs.rs/iroh-net for setup
- Graceful degradation: warn users when relay-only (no direct connection)

## Sync Protocol

### Overview

Pull-based sync over QUIC streams. Subscriber requests nodes newer than their cursor. Publisher filters by shared projects and streams matching nodes.

### Sync Semantics

**Snapshot model:** Subscribers receive a point-in-time snapshot of the publisher's shared nodes.

- **New nodes**: Synced via incremental cursor
- **Updated nodes**: NOT propagated (subscriber keeps original)
- **Deleted nodes**: NOT propagated (subscriber keeps copy)
- **Full re-sync**: Reset cursor to `null` to re-fetch everything

This is intentional simplicity for v1. Subscribers get what was shared at sync time. For fresh data, re-sync.

### Message Format

Length-prefixed MessagePack over QUIC stream. Each message:

```
┌──────────────┬─────────────────────────────┐
│ Length (4B)  │ MessagePack payload         │
│ big-endian   │                             │
└──────────────┴─────────────────────────────┘
```

### Messages

**SYNC_REQUEST**
```rust
SyncRequest {
    cursor: Option<DateTime<Utc>>,  // None = full sync
}
```

**SYNC_RESPONSE**
```rust
SyncResponse {
    nodes: Vec<Node>,
    new_cursor: DateTime<Utc>,
    has_more: bool,
}
```

**ERROR**
```rust
Error {
    code: ErrorCode,  // NotAuthorized, RateLimited, etc.
    message: String,
}
```

### Sync Flow

1. Subscriber opens QUIC stream to publisher's NodeId
2. iroh-net handles NAT traversal / relay
3. Subscriber sends SYNC_REQUEST with cursor
4. Publisher queries: `SELECT * FROM nodes WHERE source LIKE '<shared_project>%' AND timestamp > cursor ORDER BY timestamp`
5. Publisher streams SYNC_RESPONSE (paginated, batch size ~100 nodes)
6. Subscriber writes nodes to `peers/<node_id>/nodes.lance` (upsert by `id`)
7. Subscriber updates cursor in `meta.json`

### Node Deduplication

Subscriber performs upsert by `Node.id`:
- If node with same `id` exists, skip (keep existing - snapshot semantics)
- If new `id`, insert

This handles re-syncs and cursor resets gracefully.

### Authorization

Publisher checks if requester's NodeId is in `peers.json` with `publish: true`. Otherwise returns error:
```
Error {
    code: NotAuthorized,
    message: "Peer {node_id} not authorized. Use: ds peer add {node_id} --publish"
}
```

## Query Layer

### Multi-DB Fan-out

```rust
pub async fn query_all(query: &str, limit: usize) -> Vec<QueryResult> {
    let mut results = Vec::new();

    // Query local
    let local = local_store.query(query, limit).await?;
    for (node, score) in local {
        results.push(QueryResult {
            node,
            score,
            source: Source::Local,
        });
    }

    // Query each peer
    for (peer_id, peer_store) in peer_stores.iter() {
        let peer_results = peer_store.query(query, limit).await?;
        for (node, score) in peer_results {
            results.push(QueryResult {
                node,
                score,
                source: Source::Peer(peer_id.clone()),
            });
        }
    }

    // Sort by score, take top N
    results.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
    results.truncate(limit);
    results
}
```

### CLI Output

```bash
$ ds query "rust error handling"

[local]  (0.89) How to use thiserror for custom errors...
[alice]  (0.85) Error handling patterns in async Rust...
[bob]    (0.82) Result vs Option - when to use which...
```

### MCP Output

```json
{
  "results": [
    {
      "id": "uuid...",
      "content": "How to use thiserror...",
      "score": 0.89,
      "source": "local",
      "project": "/Users/drazen/playground/ai-omnibus"
    },
    {
      "id": "uuid...",
      "content": "Error handling patterns...",
      "score": 0.85,
      "source": "alice",
      "project": "/home/alice/rust-patterns"
    }
  ]
}
```

## CLI Commands

### Identity

```bash
ds peer init                  # Generate identity, initialize config
ds peer id                    # Print this node's ID (for sharing)
```

### Share Configuration

```bash
ds peer share <project_path>      # Add project to shared list (canonicalizes path)
ds peer unshare <project_path>    # Remove project from shared list
ds peer shared                    # List shared projects
```

### Peer Management

```bash
ds peer add <node_id> [--name <name>]
                              # Add peer, default subscribe=true, publish=false
ds peer add <node_id> --publish
                              # Add peer and allow them to pull from us
ds peer remove <node_id>      # Remove peer, delete their local replica
ds peer list                  # Show all peers with sync status
```

### Sync

```bash
ds peer sync                  # Sync with all subscribed peers
ds peer sync <node_id>        # Sync with specific peer
ds peer sync --full           # Full re-sync (reset all cursors)
```

### Integration with Daemon

The existing `ds start` daemon is extended:
- Accepts incoming sync connections (if any peers have `publish: true`)
- Optionally syncs on startup: `ds start --sync`
- Optionally syncs periodically: `ds start --sync-interval 30m`
- Uses file locking to prevent concurrent writes (daemon holds lock, CLI waits)

## Concurrency

To prevent conflicts between daemon and CLI:

1. **Write lock on peer stores**: Only one process writes to `peers/` at a time
2. **Lock file**: `~/.datasphere/peers/.lock`
3. **Behavior**:
   - `ds start` acquires lock on startup, releases on shutdown
   - `ds peer sync` acquires lock, syncs, releases
   - If lock held, wait with timeout (30s) then fail with message

## Build Configuration

P2P support adds significant dependencies (iroh-net, quinn, etc.). Available as optional feature:

```bash
# Default build (no P2P)
cargo build --release

# With P2P support
cargo build --release --features p2p
```

Commands `ds peer *` require the `p2p` feature. Without it, they print:
```
P2P features not enabled. Rebuild with: cargo build --features p2p
```

## Example Session

```bash
# Alice's machine
$ ds peer init
Generated NodeId: alice123...

$ ds peer share ~/oss/rust-patterns
Added to shared projects: /home/alice/oss/rust-patterns

$ ds start
Daemon started. Accepting peer connections.

# Bob's machine
$ ds peer init
Generated NodeId: bob456...

$ ds peer add alice123... --name alice
Added peer 'alice'

$ ds peer add alice123... --publish
Updated: alice can now pull from us

# Bob shares a project back
$ ds peer share ~/code/error-handling-guide
Added to shared projects: /home/bob/code/error-handling-guide

$ ds peer sync
Syncing with alice... 847 nodes received.

$ ds query "thiserror derive"
[alice]  (0.92) Using #[derive(Error)] with thiserror...
[local]  (0.78) My notes on error handling...

# Alice syncs back (after Bob enabled publish)
$ ds peer add bob456... --name bob
$ ds peer sync
Syncing with bob... 234 nodes received.
```

## Implementation Phases

### Phase 1: Foundation

- [ ] Add iroh-net as optional dependency (`features = ["p2p"]`)
- [ ] Implement identity generation (`ds peer init`)
- [ ] Create config files (share.json, peers.json)
- [ ] CLI: `ds peer id`, `ds peer share/unshare/shared`
- [ ] Path canonicalization for share.json

### Phase 2: Multi-DB Query

- [ ] Refactor Store to support peer stores in `peers/<id>/`
- [ ] Implement fan-out query across local + peers
- [ ] Update CLI output to show source
- [ ] Update MCP response to include source
- [ ] `ds peer import <path>` for manual sneakernet testing
- [ ] Per-peer stats in `ds stats`

### Phase 3: Sync Protocol

- [ ] Implement sync server (accept connections in daemon)
- [ ] Implement sync client (`ds peer sync`)
- [ ] Project filtering at publish time (prefix match on `source`)
- [ ] Cursor-based incremental sync with upsert
- [ ] Error handling for offline/unreachable peers
- [ ] `--full` flag for cursor reset

### Phase 4: Peer Management

- [ ] `ds peer add/remove/list` commands
- [ ] Authorization (check peers.json on connect)
- [ ] Helpful error messages for auth failures
- [ ] File locking for concurrent access

### Phase 5: Polish

- [ ] `--sync` and `--sync-interval` flags for daemon
- [ ] Progress indicators for long syncs
- [ ] Retry logic for failed syncs
- [ ] Relay status warnings (direct vs relayed)
- [ ] Documentation

## Decisions Log

| Decision | Rationale |
|----------|-----------|
| Snapshot semantics (no update/delete propagation) | Simplicity for v1. Full re-sync available if needed. |
| Project-level sharing (not node-level) | Right granularity. What would node-level even mean? |
| iroh-net transport | NAT traversal + relay, Rust-native, maintained |
| Feature flag for P2P | Keeps base binary small, opt-in complexity |
| Upsert by node ID | Handles re-syncs gracefully, keeps first version |
| Paths visible to peers | Accepted tradeoff. Use dedicated share dirs if sensitive. |

## Open Questions

1. **Stale peer data** - If you unsubscribe from a peer, delete their `peers/<id>/` immediately or keep as archive? *Lean: delete immediately, clean separation.*

2. **NodeId exchange** - How do users share NodeIds in practice? Copy-paste is fine for v1.

3. **iroh-net version** - Target latest stable (check API compatibility before implementation).

## Security Considerations

- **Encrypted in transit** - QUIC provides TLS 1.3
- **Identity-based auth** - Only configured peers can connect
- **Private by default** - Must explicitly share projects
- **Path exposure** - Shared project paths visible to peers
- **No data at rest encryption** - LanceDB files are unencrypted (same as current)

## Dependencies

| Crate | Purpose | Feature |
|-------|---------|---------|
| `iroh-net` | QUIC transport, NAT traversal, relay | `p2p` |
| `rmp-serde` | MessagePack serialization | `p2p` |
| `fs2` | File locking | `p2p` |
| (existing) | LanceDB, chrono, uuid, serde | core |

## References

- [iroh-net docs]https://docs.rs/iroh-net
- [iroh examples]https://github.com/n0-computer/iroh/tree/main/iroh-net/examples
- [Syncthing BEP]https://docs.syncthing.net/specs/bep-v1.html - inspiration for sync model