# steipete/birdclaw architecture

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

- Interactive diagram: https://gitdiagram.com/steipete/birdclaw
- Repository: https://github.com/steipete/birdclaw

## Overview

#### Purpose and Entry Points
Birdclaw is a local-first Twitter/X workspace. It imports archives and optionally syncs live X data into a shared local SQLite model, then exposes that model through a CLI, local React web app, and restricted MCP read endpoint. CLI startup is `bin/birdclaw.mjs` and command registration is centered in `src/cli.ts`; the web router starts at `src/router.tsx` with route modules under `src/routes/`.

#### Canonical Local Data Layer
SQLite under `~/.birdclaw` is the system of record: canonical tweets/profiles plus account-scoped timeline, collection, follow, list, DM, and moderation edges. FTS-backed read models power local search and UI/API reads. Database lifecycle, migrations, and access are concentrated in `src/lib/db.ts`, `src/lib/database-migrations.ts`, and `src/lib/queries.ts`. Media and avatars are filesystem caches, deliberately outside SQLite.

#### Ingestion and Synchronization
Two inputs converge into the same normalized store:
- Archive discovery/import parses selective slices (tweets, profiles, follows, DMs, bookmarks), applies them transactionally, and extracts bundled media.
- Live sync adapters fetch through optional external `xurl` and `bird` CLIs, then persist canonical entities and account membership edges.
Core orchestration is in `src/lib/archive-import.ts`, `src/lib/live-sync-engine.ts`, and `src/lib/web-sync.ts`. This boundary isolates remote/auth/rate-limit variability from offline reads.

#### CLI, Jobs, and External Process Boundary
The CLI is the automation/control plane: core, sync, search, compose, moderation, graph, storage, analysis, and job commands are registered through modules such as `src/cli/register-sync.ts`, `src/cli/register-core.ts`, and `src/cli/register-jobs.ts`. Subprocess integration with `bird`/`xurl` is centralized behind adapters including `src/lib/bird.ts`, `src/lib/xurl.ts`, and `src/lib/subprocess.ts`. macOS LaunchAgent scheduling and durable sync audit behavior live in `src/lib/launchd.ts` and `src/lib/account-sync-job.ts`.

#### Web Application and Local APIs
The Vite/React application provides local timeline, mentions, DMs, inbox, links, blocks, profile, and analysis screens. Route modules compose DB-backed loaders/UI, while `/api` endpoints expose actions, sync triggers, queries, streams, and status. Key boundaries are `src/routes/__root.tsx`, `src/routes/api/query.tsx`, and `src/routes/api/sync.tsx`. UI components consume route/API data rather than directly accessing storage; timeline composition is represented by `src/components/TimelineFeedShell.tsx`.

#### Read Models, Triage, and Graph Analysis
Specialized local read models turn normalized data into operational views: DM conversations, inbox ranking, follow graph analysis, link indexing, profile identity evidence, and timeline/search results. Representative modules are `src/lib/dm-read-model.ts`, `src/lib/inbox.ts`, and `src/lib/follow-graph.ts`. These remain local-first; graph commands and ordinary search do not require live X access.

#### AI and Streaming Features
OpenAI is optional and layered over cached local context for inbox scoring, digest/discussion streams, and profile analysis; it is not a data source of truth. Typed Effect-based I/O programs are preferred internally and exposed as Promise boundaries to CLI/routes/components. Relevant runtime and streaming modules are `src/lib/openai-response-runtime.ts`, `src/lib/analysis-runtime.ts`, and `src/lib/ndjson-stream.ts`.

#### Backup, Serving, and Security Boundaries
Deterministic JSONL backup/export/import provides Git-friendly replication of durable SQLite data, managed by `src/lib/backup.ts` and `src/lib/backup-table-codecs.ts`. The production local server is implemented in `src/lib/production-server.ts`. Web access defaults to loopback; optional web token protection is separate from the read-only, tokenized MCP endpoint implemented by `src/lib/mcp-http.ts`. MCP excludes DMs, live sync, writes, and AI operations.

Main entry points: [`src/routes/api/query.tsx`](https://github.com/steipete/birdclaw/blob/main/src/routes/api/query.tsx) (Routes and local APIs), [`src/lib/db.ts`](https://github.com/steipete/birdclaw/blob/main/src/lib/db.ts) (Canonical SQLite store), [`src/lib/live-sync-engine.ts`](https://github.com/steipete/birdclaw/blob/main/src/lib/live-sync-engine.ts) (Live sync engine), [`src/cli.ts`](https://github.com/steipete/birdclaw/blob/main/src/cli.ts) (Birdclaw CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_cli["Birdclaw CLI<br/>command control plane<br/>[cli.ts]"]
  node_cli_bin["CLI launcher<br/>Node entry point<br/>[birdclaw.mjs]"]
  node_web_router["React router<br/>web application<br/>[router.tsx]"]
end

subgraph group_ingest["Ingestion and jobs"]
  node_archive_import["Archive import<br/>ingestion pipeline<br/>[archive-import.ts]"]
  node_live_sync["Live sync engine<br/>sync orchestration"]
  node_external_adapters{{"bird / xurl adapters<br/>subprocess boundary<br/>[subprocess.ts]"}}
  node_sync_jobs["Scheduled sync jobs<br/>durable job runner"]
end

subgraph group_data["Local data and read models"]
  node_sqlite[("Canonical SQLite store<br/>system of record<br/>[db.ts]")]
  node_migrations["Database migrations<br/>schema lifecycle"]
  node_query_layer["Query layer and FTS<br/>local read access<br/>[queries.ts]"]
  node_read_models["Operational read models<br/>local projections<br/>[inbox.ts]"]
  node_media_cache["Media and avatar cache<br/>filesystem cache"]
end

subgraph group_surfaces["Local surfaces"]
  node_routes_api["Routes and local APIs<br/>route modules<br/>[query.tsx]"]
  node_timeline_ui["Timeline UI<br/>React component"]
  node_ai_runtime["Optional AI runtime<br/>streaming analysis"]
  node_backup["JSONL backup replication<br/>durable export/import<br/>[backup.ts]"]
  node_production_server{{"Local production server<br/>server boundary"}}
  node_mcp{{"Read-only MCP endpoint<br/>tokenized local API<br/>[mcp-http.ts]"}}
end

node_cli_bin -->|"starts"| node_cli
node_cli -->|"imports archives"| node_archive_import
node_cli -->|"runs sync"| node_live_sync
node_cli -->|"manages jobs"| node_sync_jobs
node_sync_jobs -->|"executes"| node_live_sync
node_live_sync -->|"fetches through"| node_external_adapters
node_archive_import -->|"transactional writes"| node_sqlite
node_archive_import -->|"extracts media"| node_media_cache
node_live_sync -->|"persists normalized data"| node_sqlite
node_migrations -->|"evolves schema"| node_sqlite
node_sqlite -->|"canonical data"| node_query_layer
node_sqlite -->|"normalized records"| node_read_models
node_web_router -->|"mounts"| node_routes_api
node_routes_api -->|"local reads"| node_query_layer
node_routes_api -->|"operational views"| node_read_models
node_routes_api -->|"sync triggers"| node_live_sync
node_timeline_ui -->|"route/API data"| node_routes_api
node_routes_api -->|"analysis streams"| node_ai_runtime
node_ai_runtime -->|"cached context"| node_query_layer
node_backup -->|"exports and imports"| node_sqlite
node_production_server -->|"serves"| node_web_router
node_mcp -->|"restricted reads"| node_query_layer

click node_cli "https://github.com/steipete/birdclaw/blob/main/src/cli.ts"
click node_cli_bin "https://github.com/steipete/birdclaw/blob/main/bin/birdclaw.mjs"
click node_web_router "https://github.com/steipete/birdclaw/blob/main/src/router.tsx"
click node_archive_import "https://github.com/steipete/birdclaw/blob/main/src/lib/archive-import.ts"
click node_live_sync "https://github.com/steipete/birdclaw/blob/main/src/lib/live-sync-engine.ts"
click node_external_adapters "https://github.com/steipete/birdclaw/blob/main/src/lib/subprocess.ts"
click node_sync_jobs "https://github.com/steipete/birdclaw/blob/main/src/lib/account-sync-job.ts"
click node_sqlite "https://github.com/steipete/birdclaw/blob/main/src/lib/db.ts"
click node_migrations "https://github.com/steipete/birdclaw/blob/main/src/lib/database-migrations.ts"
click node_query_layer "https://github.com/steipete/birdclaw/blob/main/src/lib/queries.ts"
click node_read_models "https://github.com/steipete/birdclaw/blob/main/src/lib/inbox.ts"
click node_routes_api "https://github.com/steipete/birdclaw/blob/main/src/routes/api/query.tsx"
click node_timeline_ui "https://github.com/steipete/birdclaw/blob/main/src/components/TimelineFeedShell.tsx"
click node_ai_runtime "https://github.com/steipete/birdclaw/blob/main/src/lib/analysis-runtime.ts"
click node_backup "https://github.com/steipete/birdclaw/blob/main/src/lib/backup.ts"
click node_production_server "https://github.com/steipete/birdclaw/blob/main/src/lib/production-server.ts"
click node_mcp "https://github.com/steipete/birdclaw/blob/main/src/lib/mcp-http.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_cli,node_cli_bin,node_web_router toneBlue
class node_archive_import,node_live_sync,node_external_adapters,node_sync_jobs toneAmber
class node_sqlite,node_migrations,node_query_layer,node_read_models,node_media_cache toneMint
class node_routes_api,node_timeline_ui,node_ai_runtime,node_backup,node_production_server,node_mcp toneRose
```

## Components

### Entry points

- [Birdclaw CLI](https://github.com/steipete/birdclaw/blob/main/src/cli.ts): command control plane · Registers core, sync, search, job, and analysis commands. · `src/cli.ts`
- [CLI launcher](https://github.com/steipete/birdclaw/blob/main/bin/birdclaw.mjs): Node entry point · `bin/birdclaw.mjs`
- [React router](https://github.com/steipete/birdclaw/blob/main/src/router.tsx): web application · Composes local route modules and API endpoints. · `src/router.tsx`

### Ingestion and jobs

- [Archive import](https://github.com/steipete/birdclaw/blob/main/src/lib/archive-import.ts): ingestion pipeline · Selectively parses archives, applies transactions, and extracts media. · `src/lib/archive-import.ts`
- [Live sync engine](https://github.com/steipete/birdclaw/blob/main/src/lib/live-sync-engine.ts): sync orchestration · Normalizes remote data and account membership edges. · `src/lib/live-sync-engine.ts`
- [bird / xurl adapters](https://github.com/steipete/birdclaw/blob/main/src/lib/subprocess.ts): subprocess boundary · Isolates optional external CLI, authentication, and rate-limit behavior. · `src/lib/subprocess.ts`
- [Scheduled sync jobs](https://github.com/steipete/birdclaw/blob/main/src/lib/account-sync-job.ts): durable job runner · Records sync audits and integrates with macOS scheduling. · `src/lib/account-sync-job.ts`

### Local data and read models

- [Canonical SQLite store](https://github.com/steipete/birdclaw/blob/main/src/lib/db.ts): system of record · Stores canonical entities and account-scoped relationship edges under ~/.birdclaw. · `src/lib/db.ts`
- [Database migrations](https://github.com/steipete/birdclaw/blob/main/src/lib/database-migrations.ts): schema lifecycle · `src/lib/database-migrations.ts`
- [Query layer and FTS](https://github.com/steipete/birdclaw/blob/main/src/lib/queries.ts): local read access · Provides FTS-backed search and UI/API reads. · `src/lib/queries.ts`
- [Operational read models](https://github.com/steipete/birdclaw/blob/main/src/lib/inbox.ts): local projections · Builds DM, inbox, follow-graph, link, and identity views. · `src/lib/inbox.ts`
- **Media and avatar cache**: filesystem cache · Archive media is cached outside SQLite.

### Local surfaces

- [Routes and local APIs](https://github.com/steipete/birdclaw/blob/main/src/routes/api/query.tsx): route modules · DB-backed screens plus query, action, stream, status, and sync endpoints. · `src/routes/api/query.tsx`
- [Timeline UI](https://github.com/steipete/birdclaw/blob/main/src/components/TimelineFeedShell.tsx): React component · Consumes route/API data rather than storage directly. · `src/components/TimelineFeedShell.tsx`
- [Optional AI runtime](https://github.com/steipete/birdclaw/blob/main/src/lib/analysis-runtime.ts): streaming analysis · Uses cached local context for scoring, digests, and profile analysis. · `src/lib/analysis-runtime.ts`
- [JSONL backup replication](https://github.com/steipete/birdclaw/blob/main/src/lib/backup.ts): durable export/import · Deterministic Git-friendly replication of SQLite data. · `src/lib/backup.ts`
- [Local production server](https://github.com/steipete/birdclaw/blob/main/src/lib/production-server.ts): server boundary · Serves web access on loopback by default. · `src/lib/production-server.ts`
- [Read-only MCP endpoint](https://github.com/steipete/birdclaw/blob/main/src/lib/mcp-http.ts): tokenized local API · Excludes DMs, sync, writes, and AI operations. · `src/lib/mcp-http.ts`

## Connections

- CLI launcher → Birdclaw CLI: starts
- Birdclaw CLI → Archive import: imports archives
- Birdclaw CLI → Live sync engine: runs sync
- Birdclaw CLI → Scheduled sync jobs: manages jobs
- Scheduled sync jobs → Live sync engine: executes
- Live sync engine → bird / xurl adapters: fetches through
- Archive import → Canonical SQLite store: transactional writes
- Archive import → Media and avatar cache: extracts media
- Live sync engine → Canonical SQLite store: persists normalized data
- Database migrations → Canonical SQLite store: evolves schema
- Canonical SQLite store → Query layer and FTS: canonical data
- Canonical SQLite store → Operational read models: normalized records
- React router → Routes and local APIs: mounts
- Routes and local APIs → Query layer and FTS: local reads
- Routes and local APIs → Operational read models: operational views
- Routes and local APIs → Live sync engine: sync triggers
- Timeline UI → Routes and local APIs: route/API data
- Routes and local APIs → Optional AI runtime: analysis streams
- Optional AI runtime → Query layer and FTS: cached context
- JSONL backup replication → Canonical SQLite store: exports and imports
- Local production server → React router: serves
- Read-only MCP endpoint → Query layer and FTS: restricted reads
