Files
superbrain/web
Andrew S Erwin 2037a04817
release gate / check (push) Failing after 6m37s
Add agent automations: 'hey superbrain' NL-compiled watch-and-act tasks
The first real-agent slice (PLAN_PRICING_TIERS step 5, partial): describe
an automation in plain language — 'when the round trip on <url> drops
below $400, email david@…' — and Superbrain compiles it into a task the
user reviews and confirms before anything is stored.

Trust model (the point of the whole design): the LLM only ever PROPOSES.
Compile output and hand-built specs pass the same server-side
agent.ValidateSpec on every write AND on every engine load (a spec that
stops validating is paused, never executed). Actions are a closed set —
notify an owned channel, email one recipient, save one bookmark — with no
destructive or cross-user capability. Fetched page content is untrusted:
delimited in the extraction prompt, schema-checked reply ({found,value,
text,summary}), and no action can be chosen or retargeted by page text.
Multi-step requests (flights AND hotels bundled) are explicitly refused by
the compiler until chaining exists.

internal/ai: OpenAI-compatible gateway (OpenRouter default via AI_API_KEY/
AI_MODEL/AI_BASE_URL; any compatible endpoint works — house Core gateway or
Ollama later). Netguard-dialed, outbound-budgeted, balanced-brace JSON
extraction, provider bodies never echoed wholesale (they can reflect keys).

internal/agent: url_watch (scheduled fetch + one extracted fact + numeric/
text comparison) and signal (new mailbox signals, keyword filter)
conditions; poll floor 15min, once-semantics, per-task email cap 5,
per-owner daily email cap 20, global advisory tick lease, bounded run
history (pruned tail + age horizon). Engine loop joins signals/notify in
main's shutdown contract.

Schema: AgentTask + AgentRun (migration 014, converge-verified).
Entitlements: AITasks cap per plan (free 2 / standard 10 / premium 50 /
core+selfhosted unlimited) + ai_tasks override key — stored-task count is
plan data, never TIER checks.

API: /api/agent/compile (rate-limited, 422 surfaces the model's refusal,
503 when AI unconfigured), task CRUD with the version contract, manual
/run, bounded /runs history. Web: Settings -> Automations with the
composer, human-readable proposal + raw-spec review, channel picker for
notify actions, task list with Run now, and the runs strip (SafeTime for
hydration-safe timestamps).

Tests: loopback fake OpenAI-compatible server + watch target + the SMTP
fake — compile success/refusal/invalid-spec, create-time validation
(javascript: URLs rejected), version contract, url_watch fire with
once-completion, signal fire + no-refire without new signals, plan cap via
ai_tasks override, 503 without a key. Full suite green (-race); converge
green; web lint/SSR-eslint/build clean.

Docs: API.md agent section, RUNNING.md, .env.example (key stays in .env
only), compose passthrough.
2026-10-27 22:54:25 -05:00
..
2026-10-19 16:23:55 -05:00
2026-10-19 16:23:55 -05:00
2026-10-19 16:23:55 -05:00
2026-10-19 16:23:55 -05:00

superbrain — web UI

This app runs inside the root Compose stack. The root ../README.md is the single authoritative deployment/operations guide; ../API.md documents the Go API contract. This file covers only what a frontend developer needs.

Ownership boundary: the Go API owns persistence, previews, integrations and authentication semantics; this Next.js server owns browser sessions (NextAuth), the trusted API bridge (lib/api.ts signs short-lived assertions — the browser never supplies a trusted identity), and rendering. Data requests go through app/api/[...path]/route.ts, not local Prisma routes; the pre-port Prisma layer is gone. Schema and seeding live in the Go stack (../db/schema.sql, ../db/migrations/, make seed).

Requirements

  • Node.js 20.9 or newer
  • Yarn (Corepack: corepack enable)
  • A running Go API (API_BASE_URL — server-only Docker DNS, e.g. http://app:8080); the browser uses relative /api/* URLs

Setup

cp .env.example .env          # fill in AUTH_SECRET, NEXTAUTH_URL, API_BASE_URL
yarn install

Run

yarn dev                      # development: http://localhost:3000
yarn build && yarn start      # production

Production deployments run behind the Compose stack and the nginx template in ../deploy/nginx/. The Content-Security-Policy is emitted per response by middleware.ts (nonce-based, so Next's inline bootstrap scripts run); do not add a second script-src policy at the proxy — browsers intersect multiple CSPs and the nonce would be stripped.

Authentication

  • Email and password work out of the box. Sign up at /signup. Signup responses are uniform ({ok:true}) for fresh, duplicate and reserved addresses — account existence is never disclosed.
  • Authelia OIDC is fail-closed. Setting OIDC_ISSUER, OIDC_CLIENT_ID and OIDC_CLIENT_SECRET only makes the provider appear in configuration; the Go login/callback endpoints deliberately answer 501 until verified external identities are mapped to local users with explicit account-linking rules, and auth.ts rejects Authelia sign-ins until then. Configured variables are NOT working sign-in. See docs/AUTHELIA.md and the root AGENTS.md boundary.
  • Sign-out means sign out everywhere: the UI first calls /api/session/revoke (Go tokenVersion bump) and reports honestly when global revocation fails; the NextAuth signOut event is the backstop. Don't remove either path.

Encryption and key retention (operator-relevant)

IMAP mailbox passwords are encrypted with AES-256-GCM. The key derives from the dedicated ENCRYPTION_KEY when set, otherwise from the auth secret (NEXTAUTH_SECRET/AUTH_SECRET). Rows encrypted before ENCRYPTION_KEY existed decrypt via the auth-secret fallback and re-encrypt transparently (compare-and-swap — a concurrent password change always wins). Consequences:

  • Back up ENCRYPTION_KEY/AUTH_SECRET with the database; without the matching keys, stored mailbox credentials are unrecoverable.
  • Rotating an already-dedicated ENCRYPTION_KEY needs old-key-aware migration, not just a changed environment variable. Keep the old secret until every credential row has been re-encrypted and verified.

Project layout

Path Contents
app/(app)/ Signed-in screens: pages (primary workspace), bookmark filters (all, calendar, tags, contacts, kanban), collections, settings; /dashboard redirects to /bookmarks
app/api/ Next routes only: the Go proxy ([...path]), NextAuth (auth/*), session revoke; everything else is Go's
app/share/bookmark/[id] Public read-only bookmark page
components/ UI components
lib/ Server-side API bridge (api.ts), OIDC config, autosave/pagination helpers
middleware.ts Per-response nonce CSP (review CR-10)
auth.ts Auth.js configuration
docs/ Integration docs

Notes

  • URL previews are fetched server-side by the Go API (internal/preview, guarded by internal/netguard and a shared per-user/global budget) when a bookmark is saved or its URL changes — not by this app and not by open-graph-scraper.
  • Date alerts are polled by the browser every 5 minutes and shown as browser notifications; email signal ingestion is Go-side (IMAP).

Admins

Administrator provisioning is operator-driven: list addresses in ADMIN_EMAILS and run make seed (it never changes existing accounts). As a bootstrap convenience the FIRST signup on a fresh installation may be promoted once (AppSetting adminBootstrap, disable with ALLOW_BOOTSTRAP_SIGNUP=0); after bootstrap completes, signup never auto-promotes again — even with no admins left. Promotion of an existing allowlisted user requires emailVerified; DB roles remain authoritative. Admins can open any account via Admin → "View as" (mutations are blocked while impersonating).