# Development guide

How code gets from a branch to the dev server. [AGENTS.md](https://github.com/YellowUmbrellaDev/sculk.network/blob/96e9a1b/AGENTS.md) holds the short rules; this guide holds the steps. Nightlies, stable releases, fixes and feature stages are in [the release guide](https://docs.sculk.network/nightly/sculk/releases.md).

## Branches

| Branch | Purpose | Receives |
| --- | --- | --- |
| `main` | The only long-lived branch and the default branch. See [Branches](https://docs.sculk.network/nightly/sculk/releases.md#branches) | Every PR, by squash merge |
| `release-<product>-X.Y` | Fixes for an older stable line | Cherry-picked fix PRs. See [Fix a stable release](https://docs.sculk.network/nightly/sculk/releases.md#fix-a-stable-release) |

Work on a short-lived branch cut from `main`, open a PR into `main`, and delete the branch after it merges.

```sh
git fetch --prune
git switch -c fix-restart-timeout origin/main
```

```text
branch 'fix-restart-timeout' set up to track 'origin/main'.
Switched to a new branch 'fix-restart-timeout'
```

A PR into a `release-*` branch fails the `branch-flow` job of `ci` unless every commit in it, other than a merge commit, is a cherry-pick (`git cherry-pick -x`) of a commit already in `main`.

## Commit messages

Every commit subject is a [Conventional Commit](https://www.conventionalcommits.org): `<type>(<scope>): <description>`. A `commit-msg` hook in `.githooks` checks it. `pnpm install` turns it on through the `prepare` script, which runs `git config core.hooksPath .githooks`.

```sh
git commit -m "Add the nightly channel"
```

```text
commit-msg: "Add the nightly channel" is not a Conventional Commit: expected <type>(<scope>): <description>, with type one of feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
```

The hook lets merge, revert, `fixup!`, `squash!` and `amend!` messages through, since git writes them. Because a squash merge turns the PR title into the commit subject on `main`, give the PR the same format.

## Checks

The `ci` workflow runs on every commit pushed to a PR into `main` or a `release-*` branch, and on every push to those branches. A new commit never cancels the run of the previous one. A `changes` job filters pull requests so build jobs only run for areas the PR touches; pushes to `main` and `release-*` run everything.

| Job | What it proves | Run it locally |
| --- | --- | --- |
| `lint` | Biome, `.env.example` and the env table match `env-keys.ts`, every relative doc link resolves, and every Mermaid diagram parses | `pnpm lint` |
| `typecheck` | `tsc` passes in every package | `pnpm typecheck` |
| `test` | Every test passes, and the run leaves the tree clean | `pnpm test` |
| `image` | The image builds, starts and `/healthz` reports the commit. Skipped on pushes to `main`, where `deploy-dev` builds it | `docker build -t sculk . && docker run --rm -p 8080:8080 sculk` |
| `oauthmc` | Rust fmt, clippy, unit tests, musl release build, Minecraft client matrix, lane `mc-join` e2e test, and Docker healthcheck smoke run | `cargo test --manifest-path crates/oauthmc/Cargo.toml` |
| `sculkworm` | Rust fmt, clippy, unit tests, musl release build, Minecraft client verification, and Docker smoke run | `cargo test --manifest-path crates/sculkworm/Cargo.toml` |
| `branch-flow` | A PR into a `release-*` branch carries only cherry-picks of commits in `main` (pull requests only) | |
| `ci-ok` | All of the above succeeded | |

`ci-ok` exists so that one name can be marked as required. GitHub offers branch protection and rulesets on private repositories only on a paid plan, and the `YellowUmbrellaDev` organisation is on Free, so nothing blocks a merge with a red check today. The rule is: merge a PR only when every check is green. When the plan changes, or the repository goes public, require `ci-ok` and a PR on `main` and `release-*`.

## Dev server

Every merge to `main` runs `deploy-dev`:

1. `publish` builds the image and pushes `ghcr.io/yellowumbrelladev/sculk.network:sha-<7 chars>` and `:dev`.
2. `deploy` joins the tailnet as `tag:ci` with Tailscale in userspace networking (the runner has no root), connects to the dev host over SSH on port 2222 and passes the new tag. Port 22 on the tailnet address is Tailscale SSH, which ignores the forced command of the key, so the host runs a second `sshd` listener for it.
3. The host pulls that tag and restarts the container. `deploy` then polls `https://dev.sculk.network/healthz` until `commit` equals the pushed commit, and fails after five minutes.

Deploys queue in order and never cancel each other, so the server ends on the last merge. Run the workflow again from the Actions tab (`workflow_dispatch`) to redeploy the dev server.

The dev server is `https://dev.sculk.network` on the host `0-0-0`. Its stack lives in the `homelab` repository (`hosts/0-0-0/services/sculk-qa`, which is being renamed `sculk-dev`), with its secrets in 1Password. It runs the same image as production with `SCULK_PUBLIC=1`. Treat its data as disposable.

### One-time setup

These steps need an owner of the GitHub organisation and of the tailnet. Do them once.

1. Tailnet policy: add `tag:ci` to `tagOwners`. No access rule is needed while the default grant allows every connection; once it is restricted, allow `tag:ci` to reach `0-0-0:2222`.
2. Tailscale admin, Settings, OAuth clients: create a client with the `auth_keys` scope and the tag `tag:ci`.
3. On `0-0-0`, create the deploy key and authorise it with a forced command, so the key can do nothing but deploy a tag.
4. In the repository settings, create the environment `dev`, then add:

| Kind | Name | Value |
| --- | --- | --- |
| Secret | `TS_OAUTH_SECRET` | Secret of the OAuth client |
| Secret | `DEV_SSH_KEY` | Private half of the deploy key |
| Variable | `DEV_SSH_HOST` | Tailnet name of the dev host, `0-0-0` |
| Variable | `DEV_SSH_USER` | User the forced command runs as |
| Variable | `DEV_SSH_KNOWN_HOSTS` | Output of `ssh-keyscan 0-0-0` taken from a trusted machine |
| Variable | `DEV_URL` | `https://dev.sculk.network` |

5. Sign-in redirect URIs of the Google, Discord and GitHub App registrations: add `https://dev.sculk.network/auth/<provider>/callback`. The GitHub App's first callback URL is the `github` one.

### Roll back dev

Run the workflow `deploy-dev` from the Actions tab, choosing the older commit's ref, or on the host run the deploy command with the old tag. Both pull that exact image.

If a migration merged after that commit, the older image refuses the database, which it does not know (see [Switch channels on an instance](https://docs.sculk.network/nightly/sculk/releases.md#switch-channels-on-an-instance)). The dev data is disposable: delete the data volume on the host and let the older image start from an empty database, or roll forward with a fix instead.

## Teaser

While the product is in development, `https://sculk.network` serves a one-page teaser from `apps/teaser`: a Cloudflare
Worker named `sculk-teaser` with static assets only, attached to the apex domain as a custom domain. The `deploy-teaser`
workflow builds and deploys it on every push to `main` that changes `apps/teaser`, `packages/brand`, the font license or
the lockfile, then checks that the live page matches the build. Run it from the Actions tab to redeploy.

Try a change locally with `node apps/teaser/build.mjs`, then open `apps/teaser/dist/index.html` through any static
server (the page loads its fonts and icons from `/`). Deploy by hand with:

```sh
CLOUDFLARE_API_TOKEN=<token> pnpm --filter @sculk/teaser run deploy
```

```
Uploaded sculk-teaser
Deployed sculk-teaser triggers
  sculk.network (custom domain)
```

The token lives in the GitHub environment `teaser` as `CLOUDFLARE_TEASER_TOKEN`. It is an account-owned token named
`sculk-teaser-deploy (GitHub Actions)` with Workers Scripts Write and Read on the account and Workers Routes Write and
Zone Read on the `sculk.network` zone, nothing else.

The logo, the favicon and the ink colour come from `packages/brand`, which the web app also draws from: change the mark
there and both the app and the teaser follow.

## Documentation site

The documentation for sculk.network, OAuthMC and Sculkworm is built by `apps/docs/build.mjs` into one Cloudflare Worker
(`sculk-docs`, `apps/docs/wrangler.jsonc`) at `https://docs.sculk.network` that serves both release channels under a
path prefix:

| Channel | Prefix | Source |
| --- | --- | --- |
| Stable | `/stable/` | Each product's latest stable tag (`vX.Y.Z`, `oauthmc-vX.Y.Z`, `sculkworm-vX.Y.Z`) |
| Nightly | `/nightly/` | `main` working tree |

`/` redirects (`302`) to `/stable/` once at least one product has a stable tag, and to `/nightly/` until then. On
`/stable/`, any product without a stable tag renders a "not released yet" page linking to its nightly documentation.

Every page also has a Markdown twin for AI agents at the path `apps/docs/src/urls.mjs` defines (such as
`/nightly/sculk/self-hosting.md`), with relative links rewritten to absolute URLs (internal docs to their `.md` twin,
assets to their absolute URL, other repo files to GitHub). Requesting a page route with `Accept: text/markdown` serves
that twin directly. Each channel publishes `/<channel>/llms.txt` and `/<channel>/llms-full.txt`, and `/llms.txt` at the
root mirrors the default channel.

Sidebar navigation is declared per product as titled `sections` in `apps/docs/src/config.mjs`. Any discovered
non-excluded Markdown file that is not listed in a section fails the build; internal files go in `EXCLUDED_DOCS`
(`docs/decisiones.md`, `docs/design-t*.md`, `docs/especificacion-aileen.md` and `docs/presentacion-aileen.html`). A
section entry may set `title` to override only the sidebar label and `agent` (`{ label, task }`) to render a
copy-a-prompt card that points an AI agent at that page's absolute Markdown URL (used on `docs/self-hosting.md`).

Preview a change locally by building the site and running Wrangler from `apps/docs`:

```sh
pnpm --filter @sculk/docs run build
pnpm --filter @sculk/docs exec wrangler dev
```

`deploy-docs` runs on every push to `main` touching documented files and when dispatched by `release` after a stable
release. Deploy by hand with:

```sh
CLOUDFLARE_API_TOKEN=<token> pnpm --filter @sculk/docs run deploy
```

The token lives in the GitHub environment `docs` as `CLOUDFLARE_DOCS_TOKEN`. Create an account-owned Cloudflare API
token named `sculk-docs-deploy (GitHub Actions)` with Workers Scripts Write and Read on the account and Workers Routes
Write and Zone Read on the `sculk.network` zone, matching the teaser token's least-privilege permissions.

## Everyday routines

| Routine | Command |
| --- | --- |
| Find branches that did not land | `git fetch --prune && git branch -r --no-merged origin/main` |
| Check a branch is already in `main` after a squash merge | `git cherry origin/main <branch>` (`-` means it is) |
| Add an env variable | Edit `apps/server/src/config/env-keys.ts`, then `node scripts/gen-env-docs.mjs` |
| Add a migration | New `apps/server/migrations/S<nn>-<name>.sql`; never edit a merged one |
| Prove a UI or API change | `pnpm lane up`, then the `verify-sculk` skill |
| Run one package's tests | `pnpm --filter @sculk/server test` |
| Build or change the sync jar and the installer | [clients/sync.md](https://docs.sculk.network/nightly/sculk/clients/sync.md) |
