# Creating, publishing and installing modules

A Fellow module is developed inside a host application and distributed as a signed package through a
registry. This guide covers the whole path for an external developer and for the teams that consume modules.

## 1. Start

```bash
fellow new billing --template module      # host app + modules/billing + fellow.json with a local registry
cd billing && bun install && cp .env.example .env
```

Or add a module to an existing application with `fellow make:module billing`. Build it with the generators
(`make:resource`, `make:route`, `make:page`, `make:event`, `make:job`, `make:workflow`, ...) and keep it inside its
boundaries: own tables (`billing_` prefix), imports from other modules only through their `public.ts`, every
dependency declared in the manifest (see [`fellow docs modules`](/docs/modules)).

## 2. Verify

```bash
fellow check --tests          # architecture rules, types, tests
fellow make:isolation billing # cross-tenant isolation test for every tenant-scoped route
fellow module:validate billing
```

A module that fails validation cannot be packed.

## 3. Version

Modules follow semantic versioning measured on their contracts (routes, pages, events, permissions, schemas):
removing or breaking a contract is a major change (minor before 1.0), adding one is minor, anything else is a patch.
`module:pack` compares the contract surface with the previous package and refuses a smaller bump (FW-MOD-015).
Set the `fellow` range to the framework versions you test against (`^1.0.0-alpha.0`). Read [`fellow docs versioning`](/docs/versioning).

## 4. Sign and pack

```bash
fellow module:keygen                                     # once: .fellow/keys/publisher.{key,pub}.pem (never commit the private key)
fellow module:pack billing --sign .fellow/keys/publisher.key.pem
fellow module:verify dist/modules/billing-1.0.0.tgz --key .fellow/keys/publisher.pub.pem
```

The package holds the module files, `fellow-package.json` (SHA-256 of every file, the contract surface and the
provenance: repository, commit and whether the tree was dirty) and an Ed25519 signature over that description.

## 5. Publish

A registry is a static layout (`index.json`, `packages/`, `keys/`), so it can live in a directory, a file share,
object storage or any HTTPS host:

```bash
fellow registry:init ../registry --name company
```

```json
// fellow.json
{ "registries": { "company": "../registry" } }
```

```bash
fellow approve module.publish dist/modules/billing-1.0.0.tgz   # a person approves this exact package
fellow module:publish dist/modules/billing-1.0.0.tgz --registry company
```

Publishing requires a signed package and a human approval bound to the archive's content. Published versions are
immutable; dependencies must already be published in the same registry. To withdraw a broken version:

```bash
fellow module:yank billing@1.0.1 --reason "migration fails on existing data"
```

A yanked version is never chosen for new installs; existing lockfiles still resolve it and `module:outdated` warns.

### Private and public registries

- **Private:** serve the directory on the internal network, behind a token and a TLS proxy:
  `FELLOW_REGISTRY_TOKEN=... fellow registry:serve ../registry --host 0.0.0.0`. Consumers configure
  `{"registries": {"company": {"url": "https://registry.example.internal", "tokenEnv": "COMPANY_REGISTRY_TOKEN"}}}`.
- **Public:** upload the directory to any static HTTPS host (object storage, CDN). Package files are immutable and
  can be cached forever; `index.json` must not be cached.

HTTP registries are read-only for the CLI: publish to the directory, then sync it to the host.

## 6. Install

```bash
fellow catalog                        # modules, latest compatible version, publisher key, installed version
fellow module:install billing@^1      # picks the highest compatible, non-yanked version
fellow migrate
fellow module:activate billing --tenant city-x
```

`module:install` checks that the module supports the installed Fellow version, that its dependencies are present,
that the archive matches the integrity recorded in the index, that the signature comes from a key the project
trusts and that it matches the index entry. Then it writes `modules/billing` and records the exact version,
integrity, signer and registry in `fellow.lock.json` (commit it).

Trust is local and explicit. Keys published by a registry are never trusted implicitly; a person trusts a
publisher once:

```bash
fellow registry:trust 3f2a9c0d1e4b5a67 --registry company   # interactive; copies the key to .fellow/trust/
```

Keep `.fellow/trust/` in version control so the team and CI trust the same publishers.

## 7. Update

```bash
fellow module:outdated
fellow module:install billing --upgrade
fellow migrate                         # refuses downgrades; manual-update modules need `fellow module:upgrade`
```

Installed modules are vendored code that belongs to its publisher: do not edit it. `fellow upgrade:check` reports
an installed module that does not support a new Fellow version instead of rewriting it.

## Security model

- Signatures prove origin and integrity, not safety. Installed modules run in the application process with its
  database role. Review what you trust; prefer external services (API and events) for code you cannot trust.
- Archives are checked for links, absolute paths, `..` segments, unlisted, missing and altered files before
  anything is written.
- Approval for publishing is bound to the archive's hash: repacking needs a new approval.
