# Archivr Mental Model This document explains the current project shape after the workspace refactor. ## Key Documents | Document | Role | |---|---| | `ARCHIVR-MENTAL-MODEL.md` | **This file.** Current architecture, data flows, and where to edit. | | `docs/README.md` | User-facing docs: how to run the tool, supported inputs, environment variables. | ## The Big Model Archivr is now a Rust workspace with three crates: ```mermaid flowchart LR CLI["archivr-cli"] --> Core["archivr-core"] Server["archivr-server"] --> Core UI["static web UI"] --> Server ServerConfig["server TOML registry"] --> Server Core --> DB["archive/.archivr/archivr.sqlite"] Core --> Store["archive store: raw, raw_tweets, structured, temp"] ``` The key rule: > `archivr-core` owns archive behavior. `archivr-cli` and `archivr-server` are adapters. ## Crates | Crate | Responsibility | |---|---| | `archivr-core` | Archive/domain logic, database schema, queries, download/store helpers | | `archivr-cli` | Command-line interface, argument parsing, terminal behavior | | `archivr-server` | Web server, API routes, mounted archive registry, static UI | ## Archive Model Each archive is still self-contained: ```text some-archive/ .archivr/ archivr.sqlite name store_path store/ raw/ raw_tweets/ structured/ temp/ ``` The web server can mount many independent archives through its own TOML registry. That registry is separate from the archives themselves. Example: ```toml [[archives]] id = "personal" label = "Personal" archive_path = "/path/to/archive/.archivr" ``` ## How To Run It There are two user-facing binaries: | Binary | Purpose | |---|---| | `archivr` | CLI for initializing archives and capturing material into one archive | | `archivr-server` | Web server for browsing one or more existing archives | The CLI writes archive data: ```sh nix run .#archivr -- init ./my-archive --name "My Archive" nix run .#archivr -- archive file:///absolute/path/to/file.pdf ``` The server reads archive data: ```sh nix run .#archivr-server -- ./archivr-server.toml ``` If no config path is passed, the server reads `./archivr-server.toml`. The config is a server registry, not archive data: ```toml [[archives]] id = "personal" label = "Personal" archive_path = "/absolute/path/to/my-archive/.archivr" ``` The packaged Nix server wrapper sets `ARCHIVR_STATIC_DIR` so the server can find the installed web UI assets. Source-tree runs do not need that variable because they fall back to `crates/archivr-server/static`. ## Write Data Flow When archiving something through the CLI: ```mermaid sequenceDiagram participant User participant CLI participant Core participant Store participant DB User->>CLI: archivr archive path-or-url CLI->>Core: classify source and call downloader/store helpers Core->>Store: save raw/structured artifacts Core->>DB: insert run, source identity, entry, artifacts CLI->>User: terminal result ``` ## Web Capture Pipeline Web pages (`Source::WebPage`) take a longer path than yt-dlp or tweets: 1. **Fetch URL selection.** If `via_freedium` is set and the locator isn't already a Freedium URL, the downloader fetches the page through `freedium-mirror.cfd` with no forwarded cookies. The canonical DB URL stays the original locator. 2. **Browser capture.** `downloader/singlefile.rs` shells out to `single-file-cli` driving headless Chromium. Extensions (uBlock, cookie-consent) and injected browser scripts (modal closer, reader mode) attach per `CaptureConfig`. 3. **Reader mode** (optional) concatenates Mozilla's `Readability.js` from `vendor/readability/` into the SingleFile browser script and stamps absolute URLs on lazy images so the serialised DOM points to fetchable sources. 4. **Freedium cleanup** strips mirror UI (nav, footer, toaster, author header, download control) using multi-signal selectors so article-authored controls aren't hit. 5. **Rust post-processing.** After SingleFile writes the HTML, a Rust pass fetches any images the browser couldn't inline (bounded reads, same-origin cookie forwarding) and embeds them as data URIs. Title extraction runs after embedded font blocks are stripped so large fonts don't push `