Spacedrive

repository·main·Indexed 12 days ago

https://github.com/spacedriveapp/spacedrive

A cross-device data platform and virtual distributed file system (VDFS) for indexing, searching, and syncing files, emails, notes, and cloud storage via P2P. It features a Rust-based core (sd-core 2.0.0-alpha.2) using CQRS and DDD patterns, a WASM-based extension system, a GPUI-powered photo grid, and a deployable server (sd-server) with a JSON-RPC 2.0 API.

Tokens
277.2K
Snippets
822
Records
1.2K
Agent score
98%

What's inside Spacedrive

  1. Overview of the Crypto crate

    main

    The crypto crate provides Spacedrive's cryptographic modules, primarily focused on encryption and decryption. It is designed to be lightweight, easy to maintain, and platform-agnostic, though it includes platform-specific code that is conditionally compiled based on the target architecture.

    Supported Algorithms

    • XChaCha20-Poly1305

    Security Warning

    This crate has NOT received a security audit. While some upstream libraries (from RustCrypto) have been audited (e.g., the XChaCha20-Poly1305 audit by NCC group), the crate itself is considered unstable. Breaking changes are likely, and no stability or security is guaranteed. Use at your own risk.

  2. Summary of Spacedrive React Hooks

    main

    Spacedrive's React hooks provide a type-safe interface for interacting with the Spacedrive core and libraries. Key features include:

    • Type Safety: All operations are type-checked against Rust definitions (auto-generated).
    • TanStack Query Integration: Full access to the TanStack Query API (loading states, error handling, caching).
    • Library Scoping: Automatic management of library IDs for scoped queries.
    • Real-time Updates: Event subscriptions via WebSockets.
    • Normalized Cache: Supports instant cross-device synchronization.

    Core Hook Categories:

    • Data Fetching: useCoreQuery, useLibraryQuery, useLibraries.
    • Mutations: useCoreMutation, useLibraryMutation.
    • Events: useEvent, useAllEvents for real-time subscriptions.
  3. Overview of the Spacedrive Extension System

    main

    Spacedrive provides an SDK for building extensions that run in sandboxed WASM environments. Extensions can access Spacedrive's capabilities to:

    • Define custom data models that sync across devices.
    • Build specialized interfaces for specific workflows.
    • Create AI-powered agents for data analysis.
    • Connect external services and platforms.

    Extensions can share models and build on each other's data (e.g., a photo management extension providing face data to a contacts extension).

  4. What is the Spacedrive Archive system?

    main

    The Archive system is Spacedrive's data archival engine designed to index external data sources that exist outside the traditional filesystem. While the VDFS (Virtual Data File System) manages files, the Archive system manages structured data like emails, notes, messages, bookmarks, calendar events, and contacts.

    Core Capabilities:

    • Universal Indexing: Uses adapters to ingest data via a script-based protocol (stdin/stdout JSONL) from sources like Gmail, Obsidian, Slack, and GitHub.
    • Hybrid Search: Combines full-text search (SQLite FTS5) with semantic vector search (LanceDB + FastEmbed) using Reciprocal Rank Fusion.
    • Safety Screening: Uses Prompt Guard 2 to classify indexed text for injection attacks, categorizing content into trust tiers (authored, collaborative, external).
    • Schema-driven Sources: Each source is self-contained with its own SQLite database, vector index, and TOML schema that auto-generates tables and indexes.
    • AI-Ready: Provides structured search APIs for AI agents (like Spacebot) with built-in safety metadata to prevent prompt injection.
  5. What is the KeyManager and how does it work?

    main

    The KeyManager is Spacedrive's unified cryptographic secret storage system. It provides encrypted storage for sensitive data such as device keys, library encryption keys, paired device session keys, and cloud credentials.

    Architecture

    • Storage Backend: Uses redb, an embedded key-value database located at <data_dir>/secrets.redb.
    • Encryption: Uses XChaCha20-Poly1305 AEAD cipher. All secrets are encrypted at rest using a Device Key.
    • Root Key: The Device Key (256-bit) is stored in the OS keychain (macOS Keychain, Linux Secret Service API, or Windows Credential Manager). A plaintext file fallback exists for development/testing only.

    Device Key Hierarchy

    1. Device Key (from OS keychain)
      • Library Keys: Per-library encryption used for cloud credentials and library-specific secrets.
      • Paired Device Data: Session keys and device info used for P2P networking.
      • Arbitrary Secrets: Application-level secrets used by extensions or custom storage.
  6. What is a Spacedrive Library?

    main

    A Spacedrive Library is a self-contained directory that acts as a single unit for all your data, metadata, and thumbnails. Libraries use the .sdlibrary extension. Because they are self-contained, they are highly portable: you can move, back up, or share them by simply copying the entire directory.

    When a library is opened, Spacedrive loads its database and configuration into memory and creates a .sdlibrary.lock file to prevent data corruption from concurrent access by multiple processes.

  7. What is a Location and how does it work?

    main

    A Location is any directory on your device that Spacedrive tracks and monitors. When you add a location, Spacedrive immediately indexes its contents, detects changes in real-time, and syncs metadata across devices (if enabled).

    Key Characteristics

    • Root Entry: Every location creates a 'root entry' in the database with parent_id = NULL. This root serves as the ancestor for all files and folders within that location.
    • Nested Locations: Spacedrive supports adding a sub-directory of an existing location as a new location. Because of the root entry design, this does not duplicate the file tree in the database; it simply creates a new pointer to an existing entry.
    • Ownership: Ownership is inherited from the location. A Location has a device_id, and all entries within its tree are considered owned by that device. This allows for efficient ownership transfers (e.g., moving an external drive between devices) by updating a single location record.
    // Location points to root entry
    Location {
        entry_id: Some(123),  // References entries table
        // ... other fields
    }
    
    // Root entry for "/Users/alice/Documents"
    Entry {
        id: 123,
        name: "Documents",
        kind: Directory,
        parent_id: None,  // Root has no parent
        // ... other fields
    }
  8. Overview of the Shared UI Strategy (`spaceui`)

    main

    Spacedrive utilizes a shared UI strategy through the spacedriveapp/spaceui repository to prevent UI divergence between different layers of the ecosystem (such as the Spacedrive application and the Spacebot Portal).

    Historically, different layers maintained separate UI stacks, leading to duplicated primitives, forms, and composite components. The spaceui strategy aims to unify these by centralizing components and design tokens to ensure consistency across the Spacedrive ecosystem.

  9. Understand the Spacedrive Networking Architecture

    main

    Spacedrive uses a peer-to-peer networking model powered by Iroh (built on QUIC) to enable secure, direct device-to-device communication without cloud servers. The architecture is centered around the NetworkingService, which coordinates connections, protocols, and device states.

    Core Components

    • NetworkingService: The central coordinator managing the Iroh endpoint, device registry, protocol routing, and cryptographic identity.
    • NetworkIdentity: Manages the device's persistent cryptographic identity using Ed25519 keys (node_id, signing_key, and verifying_key).
    • DeviceRegistry: The source of truth for all known devices and their current states (e.g., Discovered, Pairing, Paired, Connected, Disconnected).
    • Protocol System: Uses ALPN (Application-Layer Protocol Negotiation) to route connections to specific handlers like pairing/1.0, sync/1.0, or transfer/1.0.
    pub struct NetworkingService {
        endpoint: Endpoint,              // Iroh's QUIC endpoint
        device_registry: DeviceRegistry, // Tracks all known devices
        protocol_registry: ProtocolRegistry, // Routes messages
        identity: NetworkIdentity,       // Cryptographic identity
    }
  10. Use content hashing for cross-storage deduplication

    main

    Spacedrive uses consistent content hashing across all storage backends (local and cloud). This allows you to identify duplicate files, skip unnecessary cloud uploads, and find files across different storage locations using the same hash.

    Efficiency Note: For large files (>100KB), Spacedrive uses sample-based hashing. It only transfers approximately 58KB of data for content identification, making indexing efficient even on slow connections.

    // Same file gets same content hash regardless of location
    let local_hash = hash_file("/local/photo.jpg").await?;
    let cloud_hash = hash_file("s3://bucket/photo.jpg").await?;
    
    assert_eq!(local_hash, cloud_hash); // Same content = same hash
  11. Understand the Spacebot Data Flow and API Integration

    main

    Spacebot integration in the Spacedrive interface follows a specific data flow pattern to ensure consistency and live updates. Developers should follow this architecture:

    1. Context Provider: SpacebotContext provides the necessary state.
    2. Data Fetching: Use TanStack Query for managing conversations, history, and workers.
    3. Live Updates: Use SSE (Server-Sent Events) via EventSource for real-time updates.
    4. State Changes: Use Mutations for sending messages and creating conversations.
    5. Consumption: UI Routes consume the context via the useSpacebot() hook.

    New Data Sources: When adding new features (like Agent lists, Status, Tasks, Memory, or Cron jobs), follow the pattern of using TanStack Query for fetching, mutations for writes, and SSE for live updates.

    API Client: All interactions with the Spacebot HTTP API must go through the @spacebot/api-client package. Do not implement direct HTTP calls in the UI components; add new methods to the client instead.

    // Pattern for new data sources:
    // 1. TanStack Query for fetching
    // 2. Mutations for writes
    // 3. SSE for live updates
  12. How Inspector Variants work

    main

    The Spacedrive Inspector uses a composition-based architecture to display information about different resource types. Instead of a single monolithic component with complex conditionals, the Inspector acts as a generic container that renders specific variant components (like FileInspector or LocationInspector) based on the provided InspectorVariant state.

    Architecture Overview

    • Inspector.tsx: The generic container that manages the variant state and handles the high-level layout.
    • Variant Components: Specialized components (e.g., FileInspector.tsx, LocationInspector.tsx) that implement the specific UI for a resource type.
    • Shared Components: Reusable UI primitives located in shared/ used across all variants to maintain visual consistency.
    Inspector.tsx                  # Generic container
    ├── FileInspector.tsx         # File variant
    ├── LocationInspector.tsx         # Location variant
    ├── DeviceInspector.tsx       # Device variant (future)
    ├── VolumeInspector.tsx       # Volume variant (future)
    └── shared/                   # Shared tab components