# Architecture

Blossom keeps every business rule in a Rust core, and the macOS app renders what the core decides.

![Blossom clean architecture layers](/docs/architecture.jpg)

## Layer map

Dependencies point inward only. Each layer is its own Rust crate or Swift target, so an import in the wrong direction does not compile.

| Layer | Where | Role |
|---|---|---|
| `domain` | `crates/domain` | Agents, sessions, requests, priority, logos. Depends on nothing. |
| `application` | `crates/application` | `NotchState`, the reducer, the projections, the use cases and the ports. |
| `infrastructure` | `crates/infrastructure` | Implements the ports: adapters, packages, Extism plugins, the configuration file. |
| `protocol` | `crates/protocol` | Wire contracts shared with adapters and plugins. |
| `bridge` | `crates/bridge` | The UniFFI facade: start the core, subscribe, send an intent. |
| `Bridge` | `apps/macos/sources/bridge` | Generated bindings and their wrapper; imports the core xcframework. |
| `Presentation` | `apps/macos/sources/presentation` | SwiftUI views and the theme. Imports `Bridge`; renders, never decides. |
| `Platform` | `apps/macos/sources/platform` | AppKit and system APIs: the notch window, the shortcut, updates. |
| `Blossom` | `apps/macos/sources/application` | Entry point and composition root; imports the rest. |

Nothing in `crates/` depends on Apple frameworks, so another platform brings its own interface on top of the same core.

> [!TIP]
> Rule of thumb for any new line of code: if another platform would need the same rule, it belongs in the core.

## The core

`NotchState` is the single source of truth, and it changes in exactly one place: `reduce(state, event) -> state`, a pure function with no I/O and no clock. A user intent enters the reducer wrapped in an event, so every change takes one path.

Views never receive the whole state. The application projects it into shapes made for rendering: `Mode` says what the notch shows, `Ear` which tiles fit in idle, `PendingRequest` what a request asks. `Notch` owns the state behind a lock and calls each observer with the newest state after every event.

The application declares what it needs as ports in `crates/application/src/ports`: an adapter host, the packages of adapters, a plugin host, a configuration store, a catalog source and `Connections`. Infrastructure implements them; the application never names an implementation.

## Data flow

1. The app starts the core with the path of the configuration file, the folder of the app and the home folder.
2. The core launches each adapter that is on and sends `initialize`; the adapter answers with its manifest, then reports sessions.
3. Infrastructure translates wire messages into application events, and the reducer produces the next state.
4. The bridge projects the state into a `Snapshot` of FFI records, which the Swift wrapper exposes as an `AsyncStream`.
5. The composition root hands each snapshot to the models, and the views render them.
6. A view sends an intent through `Core.send(intent:)`, into the same reducer. When a request stops being pending, the adapter host writes its outcome back to the adapter that asked.

> [!NOTE]
> `Core` has five operations: `start`, `subscribe`, `send`, `report` for a signal, and `shut_down`. Beside it, `self_test` checks a built app for `Blossom --selftest`.

## Composition roots

| Side | File | Wires |
|---|---|---|
| Rust | `crates/bridge/src/core.rs` | The infrastructure implementation of every port. |
| Swift | `apps/macos/sources/application/composition.swift` | The core, the models, the views, the windows and the menu. |

## Around the core

- **Adapters** are separate processes speaking JSON-RPC 2.0 over stdio: see [protocol.md](protocol.md) and [adapters.md](adapters.md).
- **Plugins** run in process, inside an Extism sandbox: see [plugins.md](plugins.md).
- **Configuration** is one JSON file, `~/Library/Application Support/Blossom/configuration.json`, or the file `BLOSSOM_CONFIGURATION` names. The core writes it; the app never does.

> [!IMPORTANT]
> The core has no async runtime: the adapter host uses operating system threads for each launch. Add a runtime only when blocking threads stop being enough, and record that decision.
