# Portal and hosted Worker deployment

The portal is a Cloudflare Pages static site with Pages Functions for GitHub OAuth and account/API-key management. The hosted Worker validates these hashed keys against the same D1 control store, enforces their scopes, separates DO storage by account and project, and caps accounts at 1,000 API requests plus an estimated 10,000 Workers AI neurons per UTC day. These quotas and estimates are not billing meters.

## One-time Cloudflare setup

1. Install Wrangler and authenticate to the Cloudflare account that will own the portal.
2. Create the Pages project:

   ```sh
   wrangler pages project create stubby
   ```

3. Create and migrate the D1 control store:

   ```sh
   wrangler d1 create stubby-control
   npx wrangler --cwd portal d1 migrations apply stubby-control --remote
   ```

4. Put the D1 database ID returned by `wrangler d1 create` in both Wrangler configs. They bind the same database as `CONTROL_STORE`; the Pages config also sets `SITE_URL` to the project’s production hostname. Cloudflare may suffix `stubby` if the plain `stubby.pages.dev` hostname is taken.
5. Add these Pages secrets using the dashboard or `wrangler pages secret put`:
   - `GITHUB_CLIENT_ID`
   - `GITHUB_CLIENT_SECRET`
   - `SESSION_SECRET` — generate a high-entropy secret of at least 32 random bytes.
6. Create a GitHub OAuth application with homepage URL matching `SITE_URL` in `portal/wrangler.toml` and authorization callback `<SITE_URL>/auth/callback`.
7. Add repository Actions secrets `CLOUDFLARE_API_TOKEN` (Pages and Workers deployment permissions) and `CLOUDFLARE_ACCOUNT_ID` to enable the deploy workflows.

The OAuth callback and site URLs must match the Pages environment. Use a separate GitHub OAuth app, D1 database, and secrets for preview/staging; do not reuse production credentials in previews.

## Deploy and verify

Run the portal helper tests, then deploy:

```sh
npm test --prefix portal
cd portal && wrangler pages deploy . --project-name stubby --branch main
```

Verify `/`, `/docs/`, `/auth/github`, `/api/me`, key creation/revocation, the callback state check, and logout. The account/API-key functions require the D1 binding and OAuth/session secrets. The Worker validates scoped account keys against the same D1 store; verify an authorized request, a revoked key, cross-account isolation, and quota rejection after deployment.

## Personal Durable Object deployment

The Worker uses the same D1 control store as the portal for key lookup and daily quota counters; project data remains in tenant-scoped Durable Objects. After setting the D1 ID and Actions secrets, the `Deploy hosted Worker` workflow runs on changes to `worker/` and can also be started manually. For a local deployment:

```sh
cd worker
wrangler deploy
```

In the personal environment, configure the GitHub OAuth portal and Worker on the same account. Create an account and API key in the portal, then provide that key to the remote TUI with `STUB_AGENT_API_KEY` or `--api-key`. Never commit Cloudflare credentials or API keys to this repository or issue/PR text. The Worker’s model can be overridden with the `AI_MODEL` variable in its Wrangler environment.
