Files
superbrain/README.md
T
Andrew S Erwin 0848453732
release gate / check (push) Failing after 6m21s
Add the Superbrain assistant: ephemeral chat drawer with automation proposals
The A+B slice: one conversation surface for questions AND automations.

Backend: POST /api/ai/chat — ephemeral multi-turn chat through the same
per-user AI resolution as everything else (BYO key -> plan-gated server
key -> 428). The server is stateless per request: the browser owns
history, the handler caps it anyway (40 messages accepted, last 20 used,
8k chars each, last turn must be the user's). Per-user rate limit
(30/hour, keyed on the account not the IP) and a 50-second budget inside
the proxy's 55s window.

The chat system prompt embeds agent.AutomationSchema — the SAME contract
text the compiler uses — so chat proposals and compiled tasks can never
drift. The model answers in a {reply, automation|null} JSON envelope; a
proposal reaches the client only if its spec passes agent.ValidateSpec
(invalid ones are dropped silently, the reply survives), and model-
invented channelIds are scrubbed so the UI always picks from the user's
real channels. A model that ignores the envelope degrades gracefully: raw
text becomes the reply, no proposal.

Web: components/assistant.tsx — floating launcher on every signed-in
screen (mounted once in AppShellWrapper so conversations survive in-app
navigation), Sheet drawer with markdown rendering (house ReactMarkdown +
remarkGfm pattern), proposal cards (channel picker for notify, editable
recipient for email, Create automation -> the regular tasks endpoint),
and Save to page (Markdown transcript into Pages — the only persistence,
by design). Enter sends, Shift+Enter newline.

Tests: envelope parsing, valid proposal + channelId scrub, invalid-spec
drop with reply intact, non-JSON graceful fallback, request validation
(empty/assistant-last/system-role), multi-turn passthrough, 428 path.
Full suite green (-race); web lint/SSR-eslint/build clean.

Docs: API.md chat endpoint, RUNNING.md 'two doors' framing (drawer +
Settings), README feature bullet, plan/notes docs updated.
2026-10-28 19:38:45 -05:00

407 lines
21 KiB
Markdown

# Superbrain
A multi-user bookmark workspace: URLs with Markdown notes, tags, calendar
dates, editable kanban boards, project collections with typed properties and
saved views, pages, previews, sharing, contacts and email signals.
## Run the whole app
**Client setup guide:** [Running superbrain](RUNNING.md) covers cloning, setup,
first login, everyday commands, updates, and troubleshooting on Windows,
macOS, and Linux.
Superbrain installs three ways — pick the one that matches your target:
1. **Self-hosted with Docker Compose** (this section): one command on any
Docker host. Default shape: email/password signup and login, optional
Authelia, optional SMTP-backed password reset.
2. **Standalone remote server** — `scripts/deploy.sh` provisions a target
machine end to end (nginx + Let's Encrypt + database backups). See
[Deploying to a server](#deploying-to-a-server) below.
3. **Federated "core" suite host** — a one-shot suite installer for
operators running their own federated core (traefik, Authelia, shared
postgres, pdns, superpanel). See
[Installing into a federated core suite](#installing-into-a-federated-core-suite)
below.
The `TIER` environment variable selects the product shape and derives the
auth defaults (explicit `AUTH_MODE`/`SIGNUP_ENABLED` override): unset =
both password login and Authelia; `TIER=multi` = hosted self-service
(signup, password login, self-reset); `TIER=core` = Authelia SSO only.
Details: [web/docs/AUTHELIA.md](web/docs/AUTHELIA.md) §7.
Requires Docker with Compose v2 or newer. On a new installation:
```bash
git clone https://github.com/andrewthecodertx/superbrain.git
cd superbrain
cp .env.example .env
docker run --rm node:22-alpine node -e "console.log(require('node:crypto').randomBytes(32).toString('hex'))"
```
Put the generated value in `AUTH_SECRET` in `.env`, save the file, then run:
```bash
docker compose up -d --build # builds and starts PostgreSQL, Go and Next.js
```
On Windows PowerShell, use `Copy-Item .env.example .env` instead of `cp`.
- Web interface: **http://localhost:3000**. Sign up, then sign in.
- Go API: **http://localhost:8080** (local access; endpoints in [API.md](API.md)).
- PostgreSQL: `db:5432` inside Docker; not published on the host.
- With `ADMIN_EMAILS` empty, the first non-`@example.com` account becomes
administrator and completes a one-shot bootstrap: afterwards signup never
auto-promotes again, even if the last administrator is demoted or removed
— recovery is operator-driven (`ADMIN_EMAILS` + seed, or promotion by an
existing admin). Addresses in `ADMIN_EMAILS` are reserved for
operator-provisioned accounts; automatic
promotion requires a verified email. Existing accounts are preserved on restart.
- `docker compose down` stops the stack. It does not delete the database volume.
`make run` and `make stop` are optional shortcuts for the Compose commands.
`docker-compose.full.yml` is a compatibility alias for the same complete stack.
### Existing Docker database
Set `POSTGRES_VOLUME` to its existing Docker volume name and
`POSTGRES_VOLUME_EXTERNAL=true`. The volume must contain the `superbrain`
database and `superbrain` role. Stop the previous PostgreSQL container before
mounting that volume in this stack.
`db/schema.sql` initializes a **new, empty** database once. `db/migrations/`
holds the ordered, checksummed additive migrations that `db/migrate.sh` runs
(ledger table `_migration`) before Go starts. Each migration is applied and
recorded in a single transaction under an advisory lock, so a crash never
leaves an applied file unrecorded (or vice versa) and a second concurrent
runner aborts instead of double-applying. Neither path drops a database.
Do not run the initial schema against an already initialized database, and do
not edit a migration that has been applied — add a new numbered file.
Migrations are additive (new columns/tables/indexes with defaults), so the
previous application release keeps working against a migrated database;
rolling the data itself back means restoring the pre-deploy dump (see
RUNNING.md), not reverting SQL. Because schema.sql and the chain are two
paths to the same schema, every schema change needs both a schema.sql edit
and a new migration; `make db-converge` (included in `make check`) fails
when they drift apart. The page-search migration enables the `pg_trgm`
extension (bundled with PostgreSQL and trusted, so the database owner can
install it); the index-backed page search depends on it.
## Architecture
- `internal/`: Go API, persistence, authorization and integrations. SQL values
are parameterized; camelCase JSON matches the Next.js client contract.
- `web/`: Next.js UI and NextAuth browser sessions. Credentials are verified
by Go. A server-side API proxy signs 60-second identity assertions; Go
reloads roles and resolves admin impersonation on every data request.
Signing out in the browser also revokes backend sessions ("sign out
everywhere"): it bumps the user's token version, so prior assertions and
Go cookie sessions stop working.
- The frontend has no runtime Prisma client or direct database connection.
- Go also supports its own HttpOnly `superbrain_session` cookie for direct API
consumers. Administration uses the real user; data uses the effective user.
- Containers share the Compose backend network. The app runs as non-root
with a read-only filesystem; the web container has temporary `/tmp` storage.
## Features and integrations
- Bookmarks: create/edit/delete, tags, contacts, previews, calendar dates,
alerts, Markdown downloads and public read-only links.
- Bookmark collections use cursor pagination; the UI follows every page so
collections larger than 200 remain visible on all organization screens.
- Notion-style workspace shell: a Quick Find command palette (Ctrl/Cmd+K)
jumps to any page, bookmark, tag or view and runs commands (new page, add
bookmark, theme toggle); the slim top bar shows a breadcrumb path
(workspace / section / page); sidebar sections collapse and page rows
expose hover actions (pin, inline rename, delete).
- Pages: Markdown editing/autosave with a Notion-style slash-command block
menu (type `/` for headings, to-dos, lists, quotes, dividers and code) and
the `[]` + space to-do shortcut, link collections, pinning, reorder/export.
Saves are queued per page; unsaved drafts are kept in the current browser tab
for recovery after a failed save, together with the server version they were
based on. Page switching waits for pending saves. When another tab or device
saved first, autosave pauses in an explicit conflict state — the editor
offers overwrite, save-as-copy and discard, and typing alone never
overwrites the newer version.
- Kanban: custom columns, card moves, reorder; deleting a column moves cards.
- Collections: per-project workflow state over your existing bookmarks. The
same link joins many collections without duplication, each with its own
typed properties (text, number, select, multi-select, date, checkbox, URL),
saved table/board views, server-side filters with typed operator
allowlists, and manual ordering. Membership adds context — it never owns
or publishes the bookmark: removing an item or a whole collection keeps
the bookmark, and a bookmark whose share was revoked disappears from
collections everywhere. Every mutating PATCH carries an expected version,
so concurrent editors get a conflict instead of a silent overwrite;
reordering and property deletion advance the affected items' and views'
revisions too, and deletions are idempotent removals.
- Sharing: bookmarks, tags and calendar dates; recipients read shared content.
Owners can revoke shares. Shared tags and dates expose the associated URLs.
Tag sharing is group-wide: every recipient of a tag sees all bookmarks
carrying it, including ones other recipients add. Shared readers get a
narrowed response: the link and its reader-facing content only — never the
owner's contact record, the other recipients, alerts, or mailbox watches.
- Public bookmark links deliberately publish the title, description, notes,
due date, tags and preview; contacts, alerts, mailbox watches and the
owner identity stay private. The share panel states this before you toggle.
- Remote preview images and favicons are loaded by your browser directly
from the third-party host, with `referrerPolicy="no-referrer"` so the app
URL is not disclosed; opening a page can still let those hosts observe the
request. Server-side fetches (previews, CardDAV, IMAP) never touch private
network ranges, including 6to4/Teredo/NAT64-wrapped addresses. Previews are
pinned to HTTP/HTTPS ports 80/443 and mailbox connections to IMAP ports
143/993; CardDAV uses the explicitly configured HTTPS port.
- Text fields have per-field caps in addition to the request-body limit, so
an oversized bookmark title, tag name, note or page body is rejected
instead of bloating every list and search.
- Alerts: browser notifications and in-app dismissal, polled every five minutes.
- Nextcloud: CardDAV REPORT sync using each user's app password. Credentials
are used for that request only, not stored, and never travel plaintext:
the Nextcloud URL must use https (redirects are not followed).
- IMAP: encrypted per-user settings, real login tests, sender/subject searches,
email watchers, and Message-ID deduplication. Verified TLS is required —
plaintext mailbox logins are rejected. Watcher ingestion is incremental: a
durable per-source UID checkpoint processes bounded batches until caught up
instead of re-scanning the last 50 matches. Configure integrations in
Settings, then check watchers from a bookmark.
Enabled email watchers also run every five minutes in the Go process,
including watches enabled with the original mailbox-watch switch. The
scheduler runs users with bounded concurrency, per-user failure backoff,
and a database-backed lease so extra instances never double-poll a mailbox.
- Notifications: delivery channels (email via the server's SMTP relay,
Slack webhooks, Matrix rooms, Telegram bots) and rules that fire when a
new mailbox signal arrives or a bookmark comes due. Channel secrets are
encrypted at rest and never echoed back; availability follows your plan.
Configure in Settings → Notifications.
- **AI automations ("hey superbrain")**: describe an automation in plain
language — *"when the round trip on https://… drops below $400, email
david@example.com"* — and Superbrain compiles it into a task you review
and confirm before anything is saved. Two doors: the floating **Ask
Superbrain** assistant drawer (ephemeral chat on every screen; automation
requests come back as confirm-before-create proposal cards; *Save to
page* keeps a conversation) and Settings → Automations (form + task list
+ delivery history). Tasks watch a public web page on a schedule (one
extracted fact per check, e.g. a price threshold) or react to new mailbox
signals, then notify a channel, email a recipient, or save a bookmark.
The AI only ever *proposes* validated specs; actions are a fixed, bounded
set (no logins, no bookings, no deletes, no multi-step chains yet),
every fetch goes through the same private-range network guard as
previews, and email actions are hard-capped. AI is **bring-your-own-key**
per user (Settings → AI: provider, model, key — encrypted at rest) with
an optional server-wide key for plans that include it; without any key,
AI features report "not configured" and everything else keeps working.
Details: RUNNING.md → "AI automations".
### Connecting Authelia (OIDC)
Authelia is the primary sign-in path when configured. The trusted Next.js
server handles the OIDC authorization-code flow, validates the ID token, and
then exchanges the verified identity for a local Go user. Email/password
sign-in remains available as a fallback. The full
[Authelia connection guide](web/docs/AUTHELIA.md) includes the client
registration example and the identity-mapping details.
#### 1. Register the app in Authelia
Use an Authelia installation with OIDC enabled and a valid HTTPS issuer URL.
Register `superbrain` as a confidential client with:
- Scopes: `openid`, `profile`, `email`.
- Grant type: `authorization_code`; response type: `code`.
- Token endpoint authentication: `client_secret_basic`.
- Redirect URI: `<PUBLIC_APP_URL>/api/auth/callback/authelia`, for example
`https://superbrain.company.tld/api/auth/callback/authelia`.
Generate a client secret as described in the connection guide. Put its
**hashed digest** in Authelia and its **plain value** in the app's
`OIDC_CLIENT_SECRET`. The redirect URI must exactly match the public web URL
and callback path. It points to the Next.js server, not the Go API on port 8080.
#### 2. Configure superbrain
Edit the root `.env`, replacing the sample values with your deployment values:
```dotenv
NEXTAUTH_URL=https://superbrain.company.tld
COOKIE_SECURE=1
OIDC_ISSUER=https://auth.company.tld
OIDC_CLIENT_ID=superbrain
OIDC_CLIENT_SECRET=<plain-client-secret>
```
Serve the web app through an HTTPS reverse proxy; the default Docker ports
bind to localhost. Keep your existing `AUTH_SECRET` unchanged. Apply the
environment changes with `docker compose up -d --build`, then check
**Settings → OIDC**. When all three variables hold real values, the Authelia
button is enabled.
#### 3. How identity mapping works
The Next server owns OIDC verification and the callback. After Authelia returns
a verified identity, NextAuth's `jwt` callback calls the Go API's
`POST /api/auth/oidc/identity` endpoint with a short-lived service token
(`typ=oidc-provision`). Go resolves the OIDC subject to a local `User` through
the `Account` table:
- An existing provider link is authoritative.
- A matching local account with no password may be linked when Authelia asserts
a verified email.
- A matching local **password** account returns `409 Account link required`.
Email alone is not proof of identity; an administrator links it explicitly
with `POST /api/admin/oidc-link` (`{userId,subject}`).
- Otherwise Go creates a password-less local user. The first external user on a
fresh installation follows the same bootstrap-admin rules as signup.
The NextAuth session and every API assertion use the **local** `User.id`, never
Authelia's `sub` claim. A Go `tokenVersion` bump on sign-out revokes both the
NextAuth-backed assertions and Go cookie sessions.
The Go OIDC login/callback handlers in `internal/oidc/provider.go` remain 501
stubs. They are not the NextAuth callback implementation; the Next server drives
the flow. Config status (including an `enabled` flag) indicates that values are
present, not that Go itself runs an OIDC client.
## Developer commands
```bash
make run # build and start the full Compose stack
make stop # stop the stack, keep the PostgreSQL volume
make build # Go compile
make vet # Go vet
make test # Go unit tests with race detection
make integration # Docker PostgreSQL + CardDAV/IMAP fixture tests
make db-shell # psql inside the database container
make logs # Go and Next.js logs
make migrate # ordered additive database migrations
make db-converge # schema.sql vs migration-chain drift check
make check # release validation (fails without DB coverage)
make audit # dependency advisory scans (needs govulncheck)
```
The same release gate runs automatically on every push and pull request
(`.github/workflows/check.yml`), including the browser smoke tests against
the composed stack. Pass `YARN="corepack yarn"` to `make check` when yarn
comes through corepack.
Integration tests create/drop random test schemas; they never clear app data.
They exercise CRUD, sharing, ownership boundaries, impersonation, encryption,
CardDAV sync, IMAP login/search and signal deduplication.
Frontend checks (from `web/`): `yarn lint`, `yarn build`, and
`yarn eslint -c eslint.ssr.config.mjs .`. Builds enforce TypeScript checks.
Browser smoke tests (with the stack running):
```bash
npm --prefix scripts ci
CHROMIUM_PATH=/usr/bin/chromium make e2e
```
These verify login, UI bookmark creation, Go-backed persistence, public links,
Quick Find deep links, kanban, the collections workflow (membership without
duplication, typed properties, table+board over one membership), page
autosave/slash commands/exports and every application screen. Test accounts
have unique names and only those accounts are deleted afterward.
Targeted browser regressions (after `yarn build` in `web/`):
```bash
npm --prefix scripts run test:regressions
```
These run an isolated Next server with API fixtures and test login CSRF,
browser sign-out revocation, autosave switching/ordering/recovery (including
drafts whose base version is unknown, which pause for review), pagination,
and bookmark editing without creating or deleting application users.
Optional operator-provisioned account: set its address in `ADMIN_EMAILS` in
the root `.env`, run `docker compose up -d`, then:
```bash
docker compose exec -e SEED_EMAIL=admin@company.tld -e SEED_PASSWORD='<chosen-password>' app /superbrain --seed
```
This trusted command creates an admin for an allowlisted address. It never
changes an existing account's password or role, or deletes data. Additional
existing users can be promoted from the Admin screen.
## Deploying to a server
Deployment is target-agnostic — there are no built-in host or domain
defaults. Point DNS for your domain at the server, have an ssh alias (or
host) ready, then run from the repository root:
```bash
HOST=app-server DOMAIN=superbrain.example.tld CERTBOT_EMAIL=ops@example.tld ./scripts/deploy.sh
```
(`make deploy` runs the same script with the same variables.) It records the
exact git revision, refuses dirty trees unless `ALLOW_DIRTY=1`, rsyncs the
source, and on the server: provisions Docker/nginx/certbot, writes `.env` on
first deploy, takes a pre-deploy database dump plus `.env` snapshot into
`/var/backups/superbrain` (15 retained), builds revision-tagged images, and
installs the nginx + Let's Encrypt reverse proxy from
`deploy/nginx/superbrain.conf.template`. Rollback means re-deploying an older
revision or restoring a pre-deploy dump; migrations are additive. See
RUNNING.md → "Running on a remote server" for the operator checklist.
## Installing into a federated core suite
For operators running their own copy of the federated suite (traefik +
Authelia + shared postgres + pdns + superpanel), Superbrain is a
first-class suite app. On the **core host**, as root:
```bash
git clone https://gitea.federated.computer/Federated_Computer/brain.git /tmp/superbrain-repo
sudo bash /tmp/superbrain-repo/deploy/federated/install.sh
```
(Use whichever repository location you were given access to; the installer
re-clones it onto the host by default — override with `SUPERBRAIN_REPO`,
`SUPERBRAIN_REF`, or stage a local tree with `SUPERBRAIN_SRC`.)
The installer:
1. **Preflights the suite** — Authelia is *required* (the core tier is
SSO-only: password login, signup and reset are disabled at the API),
plus the house mail relay credential for notification emails, the
`core` docker network, and fixed-IP availability (override with
`SUPERBRAIN_{WEB,APP,MIGRATE}_IP` on collision).
2. **Installs the house integration** — `/federated/lib/superbrain.sh` and
`/federated/services/superbrain/service`, then runs the standard
`/federated/bin/installapp superbrain`: compose + `.env` generation
(`TIER=core`), image builds from the staged source (interim mode until
CI publishes registry images), database + user on the shared postgres,
pdns record, monitors, superpanel catalog entry.
3. **Registers the Authelia OIDC client** — including the `claims_policy`
that puts `email` in the ID token (without it every SSO login fails
with AccessDenied), validated before Authelia is restarted.
4. **Seeds and verifies** — a password-less `admin@$DOMAIN` account
(ADMIN via `ADMIN_EMAILS`; it links to your Authelia identity by
verified email on first SSO login), then container/DNS/SSO/HTTPS
checks and the operator's first-login instructions.
Prerequisites: Authelia with the OIDC provider configured, shared
`postgresql`, traefik, pdns, and the house `mail` relay (shared `fcore`
sender account). Full design record, IP plan, upgrade flow and incident
history: [deploy/federated/STRATEGY.md](deploy/federated/STRATEGY.md).
## Client test build
Use [RUNNING.md](RUNNING.md) for the client handoff. Test login, bookmark editing,
tag/contact organization, calendar dates, kanban, a collections workflow
(create a collection, add the same bookmark to two collections, give each a
status, move a card on a board view), Markdown pages/exports,
Quick Find (Ctrl/Cmd+K), slash commands in the page editor, and sharing with
separate accounts.
Current integration boundaries:
- Authelia is the primary sign-in path when configured; email/password remains
a fallback. Existing password accounts require explicit linking to an
Authelia identity.
- IMAP and Nextcloud require the client's own service credentials.
- Alerts are browser-only and checked every five minutes while the app is open.
- Page autosave needs an API connection. Check the save indicator; use **Save
now** after a failed save. Tab-local drafts are recovery aids, not backups.