How ankitects/anki works
Purpose and Entry Points
Anki is the desktop spaced-repetition application. Its primary runtime is a Python/Qt desktop client backed by Rust domain logic and SQLite storage. Desktop startup is packaged through qt/runanki.py and qt/installer/app/src/anki/__main__.py; the Qt application shell is centered in qt/aqt/main.py.
Desktop Presentation Layer
The aqt Python package owns native Qt windows, user interaction, profiles, background tasks, and add-on integration. It orchestrates domain calls rather than implementing most collection logic. Key surfaces include study/review (qt/aqt/reviewer.py), card browsing (qt/aqt/browser/browser.py), and note editing (qt/aqt/editor.py). Web content is embedded in Qt WebViews and locally served media (qt/aqt/webview.py, qt/aqt/mediasrv.py).
Rust Collection Core and Persistence
rslib is the authoritative domain layer: collections, notes, cards, decks, notetypes, undo, imports, search, scheduling, statistics, and sync. rslib/src/collection/mod.rs establishes collection lifecycle and transactional behavior; domain services expose operations around it. Storage is SQLite-oriented, with schema/migrations and SQL-backed repositories under rslib/src/storage/sqlite.rs, rslib/src/storage/schema11.sql, and rslib/src/storage/upgrades/.
Cross-Language API Boundary
Python calls Rust through generated protobuf-oriented backend APIs, not by directly owning core business rules. The public contract begins in proto/anki/backend.proto; Rust request handlers are grouped in rslib/src/backend/mod.rs; Python bridge/client code is in pylib/anki/_backend.py and pylib/anki/rsbackend.py. pylib/rsbridge/lib.rs provides the native Rust/Python bridge. This is the critical compatibility boundary for the desktop UI, Python library, and add-ons.
Study, Rendering, and Web UI Flow
A review action flows from Qt or TypeScript UI through the bridge to Rust scheduling, which selects/answers cards, writes card/review-log changes transactionally, and returns state/rendering data. Scheduling and FSRS behavior reside in rslib/src/scheduler/mod.rs, rslib/src/scheduler/answering/mod.rs, and rslib/src/scheduler/fsrs/mod.rs. Card-template rendering is in rslib/src/card_rendering/service.rs. Modern embedded UI is Svelte/TypeScript: reviewer code is ts/reviewer/index.ts; routed screens such as editor and deck options are in ts/routes/editor/+page.svelte and ts/routes/deck-options/[deckId]/+page.svelte.
Sync and External Integrations
Collection and media sync use HTTP protocol clients against AnkiWeb-compatible services, with optional server runtime. Client sync logic is in rslib/src/sync/collection/mod.rs and rslib/src/sync/media/syncer.rs; HTTP transport is rslib/src/sync/http_client/mod.rs. The standalone sync server binary starts at rslib/sync/main.rs, with server routes/handlers under rslib/src/sync/http_server/. Separate integrations include AnkiHub (rslib/src/ankihub/mod.rs) and AnkiDroid services (rslib/src/ankidroid/service.rs).
Build, Generated Contracts, and Distribution
This is a multi-runtime build: Rust, Python, Qt 6, Node/TypeScript/Svelte, protobuf, Sass, and platform packaging. Rust build configuration generates Ninja workflows from build/configure/src/main.rs and build/ninja_gen/src/lib.rs. Protobuf definitions in proto/anki/ generate language bindings; Fluent localization sources in ftl/core/ are compiled/consumed by Rust and frontend layers. Release packaging targets Windows, macOS, and Linux via qt/installer/, while CI/release automation runs in .github/workflows/ci.yml and .github/workflows/release.yml.
Main entry points: mod.rs Collection domain · _backend.py Python backend client · backend.proto Backend protobuf contract · reviewer.py Study, browser, editor
Components 22
Desktop client
- Desktop startup
qt/runanki.py - Qt application shell
qt/aqt/main.py - Study, browser, editor · Primary native interaction surfaces.
qt/aqt/reviewer.py - Embedded web UI · Hosts local web content and media.
qt/aqt/webview.py - Reviewer frontend
ts/reviewer/index.ts - Editor and deck routes
ts/routes/editor/+page.svelte - Python backend client
pylib/anki/_backend.py - Rust/Python bridge
pylib/rsbridge/lib.rs
Rust core
- Backend protobuf contract · Compatibility boundary for Python, UI, and add-ons.
proto/anki/backend.proto - Backend request handlers
rslib/src/backend/mod.rs - Collection domain · Owns collection lifecycle and transactions.
rslib/src/collection/mod.rs - Scheduler and FSRS · Selects and answers study cards.
rslib/src/scheduler/mod.rs - Card rendering
rslib/src/card_rendering/service.rs - SQLite storage · Schema, migrations, and SQL repositories.
rslib/src/storage/sqlite.rs
Sync services
- Collection and media sync
rslib/src/sync/collection/mod.rs - Sync HTTP transport
rslib/src/sync/http_client/mod.rs - Standalone sync server
rslib/sync/main.rs - AnkiWeb-compatible service
Build and release
- Ninja build generator
build/configure/src/main.rs - Generated language bindings · Generated from protobuf contracts.
- Platform packaging · Builds Windows, macOS, and Linux distributions.
- CI and release automation
.github/workflows/ci.yml
Connections 24
- Desktop startup → Qt application shell: launches
- Qt application shell → Study, browser, editor: hosts
- Study, browser, editor → Embedded web UI: embeds
- Embedded web UI → Reviewer frontend: serves
- Embedded web UI → Editor and deck routes: serves
- Study, browser, editor → Python backend client: domain requests
- Reviewer frontend → Python backend client: review actions
- Python backend client → Rust/Python bridge: invokes
- Rust/Python bridge → Backend request handlers: dispatches requests
- Backend protobuf contract → Python backend client: generates API
- Backend protobuf contract → Backend request handlers: defines requests
- Backend request handlers → Collection domain: orchestrates
- Collection domain → Scheduler and FSRS: study operations
- Scheduler and FSRS → Card rendering: returns card state
- Collection domain → SQLite storage: transactional reads/writes
- Collection domain → Collection and media sync: synchronizes
- Collection and media sync → Sync HTTP transport: uses
- Sync HTTP transport → AnkiWeb-compatible service: HTTP protocol
- Standalone sync server → AnkiWeb-compatible service: server implementation
- Ninja build generator → Generated language bindings: build workflow
- Backend protobuf contract → Generated language bindings: source definitions
- Generated language bindings → Rust/Python bridge: binds contract
- Ninja build generator → Platform packaging: produces release inputs
- CI and release automation → Platform packaging: publishes