# 0xdevansh/lapis architecture

> Architecture diagram of the GitHub repository 0xdevansh/lapis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0xdevansh/lapis
- Repository: https://github.com/0xdevansh/lapis

## Overview

#### Purpose and entry points
Lapis is a self-hosted, Cloudflare-native Obsidian-vault system: a browser workspace, an Obsidian plugin, and a REST/sync backend. Main execution boundaries are:
- Worker API: `worker/src/index.ts`
- React SPA bootstrap and application shell: `web/src/main.tsx`, `web/src/App.tsx`
- Obsidian plugin bootstrap: `plugin/src/main.ts`

#### Cloudflare backend and persistence
The Worker is the system of record and API gateway. It uses Cloudflare D1/SQLite for identities, vault metadata, search, device and remote records; R2 for vault file content; KV for session-oriented state; Durable Objects for serialized vault coordination; and Cloudflare Artifacts for sealed Git history. Schema evolution is in `worker/migrations/`; deployment bindings are declared in `worker/wrangler.jsonc`.
- `worker/src/index.ts`
- `worker/src/db/schema.sql`
- `worker/src/vault/coordinator.ts`

#### Vault file lifecycle
Vault routes implement authenticated file, manifest, export, membership, and lifecycle operations. File writes are normalized by vault path/identity rules, patched or stored as text/content, recorded in the manifest, then made available to clients. ZIP export is generated from vault state. Conflict handling preserves colliding edits as explicit conflict notes rather than overwriting.
- `worker/src/vault/routes.ts`
- `worker/src/vault/manifest.ts`
- `worker/src/vault/conflict.ts`

#### Synchronization and concurrency
The plugin and web clients use device-scoped identity and authenticated APIs. The plugin detects local changes, computes diffs/hashes, persists an offline journal, and replays/reconciles it on reconnect. Server-side sync routes and the per-vault coordinator form the concurrency boundary for mutations, notifications, and conflict resolution.
- `plugin/src/sync/engine.ts`
- `plugin/src/sync/journal.ts`
- `worker/src/sync/routes.ts`

#### Authentication, devices, and access control
User sessions are handled by the auth subsystem and Worker middleware. Obsidian devices connect through device-code authentication, then use device Bearer credentials for sync. Vault access, invitations, membership, and device ownership are enforced server-side before content or synchronization operations.
- `worker/src/auth/index.ts`
- `worker/src/middleware/auth.ts`
- `worker/src/device/routes.ts`

#### Browser workspace
The SPA provides authentication, vault selection, workspace editing, device administration, and MCP consent. Its central workspace state invokes the Worker API and responds to vault notifications. Markdown is rendered client-side with Obsidian-oriented constructs, while editing uses a dedicated Markdown editor.
- `web/src/pages/VaultWorkspace.tsx`
- `web/src/store/workspace.tsx`
- `web/src/api.ts`

#### Derived services and external remotes
After vault mutations, search indexing supports SQLite FTS-backed search, backlinks, and tags. Notification routes distribute live vault changes/presence to connected clients. Git remote support reconciles vault state with GitHub using configured remote/device credentials; Artifacts-based storage and LFS-pointer sealing support managed historical content. MCP routes expose controlled programmatic access for agents/integrations.
- `worker/src/search/indexer.ts`
- `worker/src/notify/routes.ts`
- `worker/src/git/routes.ts`

Main entry points: [`worker/src/index.ts`](https://github.com/0xdevansh/lapis/blob/main/worker/src/index.ts) (Worker API gateway), [`worker/src/vault/coordinator.ts`](https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/coordinator.ts) (Per-vault coordinator), [`worker/src/vault/routes.ts`](https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/routes.ts) (Vault lifecycle API), [`plugin/src/sync/engine.ts`](https://github.com/0xdevansh/lapis/blob/main/plugin/src/sync/engine.ts) (Offline sync engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client runtimes"]
  node_spa_bootstrap["React SPA shell<br/>browser app<br/>[main.tsx]"]
  node_workspace["Vault workspace state<br/>client state<br/>[workspace.tsx]"]
  node_obsidian_plugin["Obsidian plugin<br/>plugin runtime<br/>[main.ts]"]
  node_sync_engine["Offline sync engine<br/>sync client<br/>[engine.ts]"]
  node_sync_journal["Offline journal<br/>local journal<br/>[journal.ts]"]
end

subgraph group_worker["Cloudflare Worker"]
  node_worker_api{{"Worker API gateway<br/>Cloudflare Worker<br/>[index.ts]"}}
  node_auth["Auth &amp; access middleware<br/>auth subsystem<br/>[auth.ts]"]
  node_device_auth["Device authentication<br/>device routes<br/>[routes.ts]"]
  node_vault_routes["Vault lifecycle API<br/>vault routes<br/>[routes.ts]"]
  node_manifest["Vault manifest<br/>manifest service<br/>[manifest.ts]"]
  node_sync_routes["Server sync API<br/>sync routes<br/>[routes.ts]"]
  node_coordinator{{"Per-vault coordinator<br/>Durable Object<br/>[coordinator.ts]"}}
  node_conflicts["Conflict preservation<br/>conflict handler<br/>[conflict.ts]"]
end

subgraph group_storage["Cloudflare persistence"]
  node_d1[("D1 / SQLite<br/>metadata database<br/>[schema.sql]")]
  node_r2[("R2 vault content<br/>object storage")]
  node_kv[("KV session state<br/>key-value store")]
  node_artifacts[("Artifacts history<br/>sealed Git history")]
end

subgraph group_derived["Derived &amp; external services"]
  node_search["Search indexer<br/>derived index<br/>[indexer.ts]"]
  node_notifications["Vault notifications<br/>notification routes<br/>[routes.ts]"]
  node_git_remote["Git remote reconciliation<br/>Git integration<br/>[routes.ts]"]
  node_mcp["MCP access<br/>integration API"]
end

node_spa_bootstrap -->|"mounts"| node_workspace
node_workspace -->|"authenticated API"| node_worker_api
node_obsidian_plugin -->|"runs"| node_sync_engine
node_sync_engine -->|"persists offline changes"| node_sync_journal
node_sync_engine -->|"sync API"| node_worker_api
node_worker_api -->|"authenticates requests"| node_auth
node_auth -->|"identities and access"| node_d1
node_auth -->|"session state"| node_kv
node_worker_api -->|"device-code flow"| node_device_auth
node_device_auth -->|"device ownership"| node_d1
node_worker_api -->|"vault operations"| node_vault_routes
node_worker_api -->|"sync operations"| node_sync_routes
node_vault_routes -->|"serialized mutations"| node_coordinator
node_sync_routes -->|"serialized sync"| node_coordinator
node_vault_routes -->|"updates"| node_manifest
node_manifest -->|"file metadata"| node_d1
node_vault_routes -->|"file content"| node_r2
node_coordinator -->|"colliding edits"| node_conflicts
node_coordinator -->|"mutation events"| node_search
node_search -->|"FTS and graph data"| node_d1
node_coordinator -->|"vault events"| node_notifications
node_notifications -->|"live changes"| node_workspace
node_notifications -->|"sync signals"| node_sync_engine
node_coordinator -->|"reconcile state"| node_git_remote
node_git_remote -->|"sealed history"| node_artifacts
node_worker_api -->|"controlled programmatic access"| node_mcp
node_mcp -->|"vault actions"| node_vault_routes

click node_spa_bootstrap "https://github.com/0xdevansh/lapis/blob/main/web/src/main.tsx"
click node_workspace "https://github.com/0xdevansh/lapis/blob/main/web/src/store/workspace.tsx"
click node_obsidian_plugin "https://github.com/0xdevansh/lapis/blob/main/plugin/src/main.ts"
click node_sync_engine "https://github.com/0xdevansh/lapis/blob/main/plugin/src/sync/engine.ts"
click node_sync_journal "https://github.com/0xdevansh/lapis/blob/main/plugin/src/sync/journal.ts"
click node_worker_api "https://github.com/0xdevansh/lapis/blob/main/worker/src/index.ts"
click node_auth "https://github.com/0xdevansh/lapis/blob/main/worker/src/middleware/auth.ts"
click node_device_auth "https://github.com/0xdevansh/lapis/blob/main/worker/src/device/routes.ts"
click node_vault_routes "https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/routes.ts"
click node_manifest "https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/manifest.ts"
click node_sync_routes "https://github.com/0xdevansh/lapis/blob/main/worker/src/sync/routes.ts"
click node_coordinator "https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/coordinator.ts"
click node_conflicts "https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/conflict.ts"
click node_d1 "https://github.com/0xdevansh/lapis/blob/main/worker/src/db/schema.sql"
click node_search "https://github.com/0xdevansh/lapis/blob/main/worker/src/search/indexer.ts"
click node_notifications "https://github.com/0xdevansh/lapis/blob/main/worker/src/notify/routes.ts"
click node_git_remote "https://github.com/0xdevansh/lapis/blob/main/worker/src/git/routes.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spa_bootstrap,node_workspace,node_obsidian_plugin,node_sync_engine,node_sync_journal toneBlue
class node_worker_api,node_auth,node_device_auth,node_vault_routes,node_manifest,node_sync_routes,node_coordinator,node_conflicts toneAmber
class node_d1,node_r2,node_kv,node_artifacts toneMint
class node_search,node_notifications,node_git_remote,node_mcp toneRose
```

## Components

### Client runtimes

- [React SPA shell](https://github.com/0xdevansh/lapis/blob/main/web/src/main.tsx): browser app · `web/src/main.tsx`
- [Vault workspace state](https://github.com/0xdevansh/lapis/blob/main/web/src/store/workspace.tsx): client state · Edits vaults and responds to vault notifications. · `web/src/store/workspace.tsx`
- [Obsidian plugin](https://github.com/0xdevansh/lapis/blob/main/plugin/src/main.ts): plugin runtime · `plugin/src/main.ts`
- [Offline sync engine](https://github.com/0xdevansh/lapis/blob/main/plugin/src/sync/engine.ts): sync client · Diffs local changes and reconciles its journal after reconnect. · `plugin/src/sync/engine.ts`
- [Offline journal](https://github.com/0xdevansh/lapis/blob/main/plugin/src/sync/journal.ts): local journal · `plugin/src/sync/journal.ts`

### Cloudflare Worker

- [Worker API gateway](https://github.com/0xdevansh/lapis/blob/main/worker/src/index.ts): Cloudflare Worker · System of record and authenticated API entry point. · `worker/src/index.ts`
- [Auth & access middleware](https://github.com/0xdevansh/lapis/blob/main/worker/src/middleware/auth.ts): auth subsystem · Enforces sessions, device credentials, ownership, and vault access. · `worker/src/middleware/auth.ts`
- [Device authentication](https://github.com/0xdevansh/lapis/blob/main/worker/src/device/routes.ts): device routes · Issues device-scoped credentials through device-code authentication. · `worker/src/device/routes.ts`
- [Vault lifecycle API](https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/routes.ts): vault routes · Serves files, manifests, exports, membership, and lifecycle operations. · `worker/src/vault/routes.ts`
- [Vault manifest](https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/manifest.ts): manifest service · Records normalized vault file state. · `worker/src/vault/manifest.ts`
- [Server sync API](https://github.com/0xdevansh/lapis/blob/main/worker/src/sync/routes.ts): sync routes · `worker/src/sync/routes.ts`
- [Per-vault coordinator](https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/coordinator.ts): Durable Object · Serializes mutations, notifications, and conflict resolution. · `worker/src/vault/coordinator.ts`
- [Conflict preservation](https://github.com/0xdevansh/lapis/blob/main/worker/src/vault/conflict.ts): conflict handler · Stores colliding edits as explicit conflict notes. · `worker/src/vault/conflict.ts`

### Cloudflare persistence

- [D1 / SQLite](https://github.com/0xdevansh/lapis/blob/main/worker/src/db/schema.sql): metadata database · Stores identities, vault metadata, search, devices, and remotes. · `worker/src/db/schema.sql`
- **R2 vault content**: object storage
- **KV session state**: key-value store
- **Artifacts history**: sealed Git history · Stores managed historical content and LFS-pointer seals.

### Derived & external services

- [Search indexer](https://github.com/0xdevansh/lapis/blob/main/worker/src/search/indexer.ts): derived index · Builds FTS search, backlinks, and tags after mutations. · `worker/src/search/indexer.ts`
- [Vault notifications](https://github.com/0xdevansh/lapis/blob/main/worker/src/notify/routes.ts): notification routes · Distributes live vault changes and presence. · `worker/src/notify/routes.ts`
- [Git remote reconciliation](https://github.com/0xdevansh/lapis/blob/main/worker/src/git/routes.ts): Git integration · Reconciles vault state with GitHub remotes. · `worker/src/git/routes.ts`
- **MCP access**: integration API · Provides controlled agent and integration access.

## Connections

- React SPA shell → Vault workspace state: mounts
- Vault workspace state → Worker API gateway: authenticated API
- Obsidian plugin → Offline sync engine: runs
- Offline sync engine → Offline journal: persists offline changes
- Offline sync engine → Worker API gateway: sync API
- Worker API gateway → Auth & access middleware: authenticates requests
- Auth & access middleware → D1 / SQLite: identities and access
- Auth & access middleware → KV session state: session state
- Worker API gateway → Device authentication: device-code flow
- Device authentication → D1 / SQLite: device ownership
- Worker API gateway → Vault lifecycle API: vault operations
- Worker API gateway → Server sync API: sync operations
- Vault lifecycle API → Per-vault coordinator: serialized mutations
- Server sync API → Per-vault coordinator: serialized sync
- Vault lifecycle API → Vault manifest: updates
- Vault manifest → D1 / SQLite: file metadata
- Vault lifecycle API → R2 vault content: file content
- Per-vault coordinator → Conflict preservation: colliding edits
- Per-vault coordinator → Search indexer: mutation events
- Search indexer → D1 / SQLite: FTS and graph data
- Per-vault coordinator → Vault notifications: vault events
- Vault notifications → Vault workspace state: live changes
- Vault notifications → Offline sync engine: sync signals
- Per-vault coordinator → Git remote reconciliation: reconcile state
- Git remote reconciliation → Artifacts history: sealed history
- Worker API gateway → MCP access: controlled programmatic access
- MCP access → Vault lifecycle API: vault actions
