# Plugin architecture

A plugin is a WebAssembly module, or one file of JavaScript run by the engine Blossom embeds, that the host asks for an indicator, a command or a panel when the user does something.

## What exists

| Piece | Where |
|---|---|
| **Contract, version 1** — frozen | `crates/protocol/src/plugin` |
| **Contract, version 2** — timers, alarm, `wake`, kept text | `crates/contract` |
| **SDKs** | `sdks/plugin`, `sdks/second`, `sdks/assemblyscript`, `sdks/javascript` |
| **Rules** — the registry, and when the host is told to ask | `crates/application/src/plugin` |
| **Ports** | `PluginHost`, `CatalogSource` in `crates/application/src/ports` |
| **Host** — `ExtismPluginHost` | `crates/infrastructure/src/plugin` |
| **Bridge**, **harness**, **catalog** | `crates/bridge/src/plugin`, `sdks/harness`, `apps/catalog` |

## The contract

A plugin exports up to five functions with fixed names, each taking and returning JSON with `camelCase` keys, and the host decides when to call.

| Export | Returns | The host calls it |
|---|---|---|
| `describe` | the manifest | right after loading the module |
| `indicator` | an indicator, or `null` | only if the manifest declares one |
| `command` | `{}`, or `{ "panel": … }` | only for a declared command |
| `panel` | a panel | only if the manifest declares one |
| `wake` | `{}`, or `{ "alert": … }` | version 2, granted Alerts, when its alarm is due |

A plugin never acts by itself: it cannot call the host, and the host never asks on a timer. A manifest carries no identifier, so the host names a plugin after its file. Nothing is cut to fit, so an answer that breaks a limit is refused whole.

Version 2 adds what a plugin that keeps time needs: a `timer`, a block `timers`, one alarm, the export `wake` with its alert, `remember` and `recall`, and `permissions.links`. An older Blossom refuses it as `incompatible`.

## Execution model

A module is a `cdylib` built for `wasm32-unknown-unknown`; a script is one ES module of 4 MiB at most, run by QuickJS-NG built for WASI and pinned by `ENGINE_DIGEST`. Describing either has three steps:

1. **Inspecting** — the host reads the bytes with the parser of the runtime before the runtime sees them, and refuses what would cost time or memory: past 32 MiB, a start function, a table instruction, more than 256 pages of memory, or a cost to compile past its bound.
2. **Compiling** — in process, one function at a time, with no cache on disk.
3. **Calling `describe`** — inside the sandbox, under the limits of every call.

| Limit of one call | Value | Past it |
|---|---|---|
| Time | 2 seconds, shared by every request it sends | `exhausted` |
| Work | 2 000 000 000 units of fuel | `exhausted` |
| Memory | 256 pages, 16 MiB | `exhausted` |
| Size of an answer | 64 KiB | `invalid` |

A failure is one of eight kinds — `unreadable`, `too large`, `unsupported`, `exhausted`, `incompatible`, `invalid`, `not stored`, `mismatched` — and the app words it.

## Threads and timeouts

- **One thread per plugin** that has been asked, and it owns the module. Its questions wait in a line of eight and one more fails at once, so a plugin that never answers holds up no other plugin.
- **Generations.** The registry raises a number whenever a plugin is found, described, added, turned on or off; every question carries it and every answer brings it back, so an answer of another generation is dropped.
- **A failed call** throws the module away and loads it again: the plugin starts from nothing, and its next good answer makes it available again.
- **Fresh and stale.** An indicator is asked once per round, which the app holds for the interval of its theme, 60 seconds; a command, a grant or a revocation makes it stale too.

## Security boundaries

- **No WASI.** A plugin computes and reads the clock; it cannot start a process, write a file, read the clipboard or draw a random number.
- **Capabilities are granted one by one**, per plugin and per capability, and kept under `grants` in the configuration file. A grant or a revocation re-links the module before its next call and throws its thread away.
- **`fetch`** is `https` on port 443 to a granted host, with no redirect followed, no proxy and only public addresses; `localhost` and `127.0.0.1` are the exception, once granted.
- **Tokens** never enter a plugin: the core keeps them through the port `CredentialStore` over the Keychain, and the host adds the header after every check.
- **Links** open only a host the plugin may already reach.
- **The catalog** is a signed index: Blossom trusts only the keys of `crates/infrastructure/src/catalog/keys.rs`, keeps the digest of every module for a revocation, and refuses a module that is not what its entry says.

Plugins live in `~/Library/Application Support/Blossom/plugins`, one `<identifier>.wasm` or `<identifier>.js` each, 64 at most, off until the user turns one on. Writing one is [Plugins](../plugins.md).
