Commit Graph

115 Commits

Author SHA1 Message Date
Matthew Meszaros 1317ea67d6 fix: rename duplicate migration 000035 to 000037
PR #10 added 000035_api_key_suffix and PR #11 separately added
000035_api_key_smart_limits, both targeting the api_keys table.
golang-migrate refuses to load when two source files share a
version number, so the backend container failed health checks
in fresh stacks.

Bumping api_key_smart_limits to 000037 keeps PR #10 in its
original slot (it landed first) and matches the next free
version after 000036_contact_categories. The two migrations
touch different columns so apply order does not matter.
2026-05-25 11:43:14 +00:00
Matthew Meszaros c790812207 Merge pull request #11 from warmbly/feature/api-keys-dashboard
feat: ship api keys dashboard with smart limits and analytics
2026-05-25 12:26:54 +02:00
Matthew Meszaros e38d571610 Merge pull request #12 from warmbly/feature/campaign-creation-overhaul
feat: fix campaign creation 500 and ship full-config wizard
2026-05-25 12:26:44 +02:00
Matthew Meszaros 1435e0d618 style: gofmt api_key models 2026-05-25 10:23:32 +00:00
Matthew Meszaros 8d351d1f0f fix: satisfy golangci-lint on the contacts power-tools branch
- gofmt-align ContactEngagement fields and drop trailing blank line
  in contact/export.go
- remove no-op self-assignment ac.CustomFields = ac.CustomFields
  flagged by govet
2026-05-25 09:27:10 +00:00
Matthew Meszaros 512fea76ff feat: add contact 360 detail, sent-emails, and timeline endpoints 2026-05-25 08:30:19 +00:00
Matthew Meszaros 2170b86106 fix: stop import 500 by installing no-op audit service and drop redundant banner 2026-05-25 06:19:22 +00:00
Matthew Meszaros e523d220d8 fix: round-trip organizationpermission through smallint via scan/value 2026-05-25 05:03:04 +00:00
Matthew Meszaros 02c2f097e9 fix: resolve duplicate migration 000034 (api_key_suffix -> 000035, contact_categories -> 000036) 2026-05-24 16:25:42 +00:00
Matthew Meszaros 03dc3bce95 feat: overhaul campaign creation with atomic transactional create + multi-step wizard
- fix 500 on POST /campaigns: insert now sets NOT NULL created_at/updated_at
- atomic create writes campaign + sequences + ab variants + tag/folder links + advanced overrides in one tx
- create accepts full optional config (schedule, sender pool, tracking, sequences, variants)
- handler forwards orgID so future start/stop and org-scoped routes resolve
- fix Get/GetByID joining campaign_email_tags/campaign_folders on wrong column
- publish CAMPAIGN_CREATED realtime event after successful create
- replace name+description modal with 4-step wizard (basics, schedule, sender pool, first email + follow-ups)
2026-05-24 16:13:22 +00:00
Matthew Meszaros 9123c4bfcf feat: full api keys product (smart limits, analytics, dashboard) 2026-05-24 16:07:18 +00:00
Matthew Meszaros 07b0c8b5ff feat: backend contacts categories, smart export, csv/xlsx import 2026-05-24 16:04:48 +00:00
Matthew Meszaros 9aedd8cbb0 Merge remote-tracking branch 'origin/main' into feature/implement-templates
# Conflicts:
#	internal/api/routes.go
2026-05-24 15:25:02 +00:00
Matthew Meszaros 413108db49 Merge pull request #7 from warmbly/feature/full-seed-data
chore: stabilize CI and add repo hygiene for seed tooling
2026-05-24 17:18:31 +02:00
Matthew Meszaros 2c82106ca4 style: gofmt seed files 2026-05-24 12:01:53 +00:00
Matthew Meszaros 9bce2ad29e feat: full reply templates (search/reorder/duplicate/render + UI) 2026-05-24 12:01:37 +00:00
Matthew Meszaros 8002d56b86 feat: full api key support across data routes 2026-05-24 11:59:05 +00:00
Matthew Meszaros 6eb3a3bdfd merge: bring origin/main into branch, layer full seed on top of seedRich 2026-05-24 11:54:49 +00:00
Matthew Meszaros 0c1fa08a35 feat: full dev seed (users, orgs, workers, mailboxes, campaigns, crm, admin) 2026-05-24 11:41:51 +00:00
Matthew Meszaros 939f2160d9 Merge pull request #6 from warmbly/feature/redesign-email-onboarding
ci: fix CI permissions, add web pipeline, prune go linters
2026-05-24 13:26:25 +02:00
Matthew Meszaros 4eee3d0455 style: gofmt onboarding.go 2026-05-24 11:21:40 +00:00
Matthew Meszaros 72f9c5f51e style: gofmt danger_zone.go 2026-05-24 04:42:43 +00:00
Matthew Meszaros fceeddf5ea feat: app-wide pending-deletion banner + email every org member 2026-05-24 04:35:57 +00:00
Matthew Meszaros 779222bd4f feat(web): redesign add-email modal in the slate-900 theme 2026-05-24 04:16:23 +00:00
Matthew Meszaros c822e95e7f Merge remote-tracking branch 'origin/main' into feature/danger-zone-delayed-deletions
# Conflicts:
#	cmd/backend/main.go
#	internal/api/handler/handler.go
#	internal/api/routes.go
#	internal/models/audit.go
#	internal/models/organization.go
#	internal/models/user.go
#	internal/repository/pg_organization.go
#	internal/repository/pg_user.go
2026-05-24 04:12:10 +00:00
Matthew Meszaros 337d823703 merge: bring main into branch, reconcile email service constructor 2026-05-24 04:09:16 +00:00
Matthew Meszaros e42feac0e4 feat: danger zone with delayed deletion for orgs and accounts 2026-05-24 04:06:07 +00:00
Matthew Meszaros 45f48ba93b feat: redesign email account onboarding flow 2026-05-24 04:01:16 +00:00
Matthew Meszaros 0bbfc7114c ci: regenerate tasks.pb.go with protoc 33.6 to match CI installer 2026-05-23 17:10:40 +00:00
Matthew Meszaros d227038ca0 ci: drop unused/unconvert/gosimple + shadow/nilness, run gofmt
Disable the linters that fire on legacy code without flagging real
bugs: `unused` (orphan repos kept for future feature flags),
`unconvert` (defensive type conversions), `gosimple` (style
suggestions in code we don't want to touch).

govet: disable `shadow` (idiomatic `err :=` re-decls in transaction
patterns) and `nilness` (legitimate defensive nil checks that look
tautological to the analyzer).

Ran `gofmt -w internal/ cmd/` — every Go file now passes
gofmt -l with no output.

Kept: govet, staticcheck, ineffassign, typecheck, bodyclose, noctx,
sqlclosecheck, gofmt, goimports, misspell — the real-bug checks.
2026-05-23 16:54:12 +00:00
Matthew Meszaros 4eb8f7babe feat: settings overhaul, avatars, RBAC, plan alignment, perf
Backend:
- Fix contact-create 500 (nil custom_fields, doubled slice, bad RETURNING SQL)
- Avatar upload: migration 000033, S3 public-read, PNG/JPG only,
  client-resized to 512px + server dimension cap (1024px)
- Pull avatar_url through user + organization repo queries

Frontend:
- Settings restructured into nested routes with a rail layout
  (/app/settings/{profile,notifications,security,members,roles,
  workspace,billing,danger}); flat Section/Row primitives replace
  the per-card rectangles; Save buttons only render when dirty
- Standalone /app/billing and /app/team removed; legacy URLs
  redirect to the settings sections; UserNav trimmed accordingly
- CRM rebuilt: Pipelines CRUD + stage editor, Deals kanban with
  HTML5 drag/drop, Tasks bucketed by due-date with inline toggle.
  Frontend models realigned with backend (Deal.name, CRMTask.status
  enum, paginated list shapes)
- Avatars: AvatarUploader component, client-side canvas resize,
  wired into Profile + Workspace settings; UserNav + OrgSwitcher
  render the uploaded image with initials fallback
- RBAC: lib/permissions.ts mirrors organization_permission.go;
  inline role picker in Members; Roles & access section shows the
  permission matrix and per-role member counts
- Audit log page at /app/audit, gated to owner+admin via canManage
- Plans aligned with warmbly-web pricing: Starter/Grow/Business/
  Enterprise via lib/plans.ts; PlanPill, billing page, sidebar
  badges and LockedSurface all read from the same catalogue
- Header PlanPill shows current plan with status-aware coloring;
  sidebar locked rows show the required-plan badge instead of a
  generic lock icon

Perf:
- QueryClient defaults (staleTime: 30s, refetchOnWindowFocus: false,
  retry: 1) — kills 3-5 round-trip storm on every navigation
- useSubscription, usePlans → staleTime: Infinity (only invalidate
  on plan-change mutations); useUser/Timezones/Orgs get long stales
  with refetchOnMount: false
- vite.config: optimizeDeps for heavy libs + server.warmup for the
  most-mounted entry pages
2026-05-23 16:04:24 +00:00
Matthew Meszaros 5845fc3069 feat: inbox tag+multi-account filter + org gate + invite/join flow
Inbox filter:
- Backend: MailSearchParams gained EmailAccountIDs []uuid.UUID; the
  search SQL filters with `email_id = ANY($)`. /unibox handler now
  accepts both `email_id=<uuid>` (legacy) and `email_ids=<csv>`.
- Frontend: UniboxSearchParams gained accountIds[] and a UI-only
  tagId. searchIncoming sends email_ids=csv. UniboxFilterSheet:
  Accounts section is now (a) a row of tag chips backed by user.tags
  with per-tag account counts and (b) a multi-select list of every
  connected mailbox with an inline checkbox + avatar; accounts that
  belong to the active tag get a "via tag" affordance. Picking a tag
  resolves to the underlying account IDs at Apply time. "Select all"
  / "Clear" inline in the SectionBar header.

Org gate + onboarding:
- New /select-org page. Three sections: pending invitations (one-
  click Join), existing memberships (pick one to enter), and a
  Create New Workspace form (slate-900 primary). Routed at
  /select-org.
- OrgGate hook lives inside RealtimeManager. On load, if the user
  has zero orgs and no current org, it navigates to /select-org
  replace. Renders null so it doesn't displace AppLayout.

Invite + join:
- Team page rebuilt with real data: useMembers + usePendingInvitations,
  plus InviteDialog (email + role popover, slate-900 send button).
  Inline remove on member rows (skip "owner"), inline cancel on
  pending invitations.
- Pending invitations show up on /select-org too — a freshly
  invited user can accept without ever entering the dashboard first.

Response unwrapping:
- Org/member/invitation list clients now tolerate the backend's
  {data: T[] | null} envelope (it's the consistent shape across the
  Go handlers). Map nested membership rows into the flat
  Organization shape the rest of the app expects.

Seeder: re-run verified — dev@warmbly.com still gets "Dev's
Organization" so they don't bounce through /select-org.
2026-05-23 10:19:56 +00:00
Matthew Meszaros f04bd26b44 feat: comprehensive unibox + WS latency + dashboard-style transactional emails
User: "inbox is really really bad. So I want all possible ways to
search for an email that we can do... realtime for everything and the
dashboard to show our latency... show how much unread emails." Plus a
follow-up: "I don't like how the emails looks like because they have
that blue gradient, I want dashboard style good one."

Inbox:
- Wired the backend search endpoint (GET /unibox with from/subject/
  unseen/since/until/cursor/limit) — was implemented server-side but
  the frontend was never calling it. Inbox now actually reflects
  server data.
- New UniboxSearchParams model + searchIncoming client + infinite
  useUniboxSearch hook that drops null rows defensively.
- ConversationList: SearchInput (subject substring) + quick-filter
  strip (All / Unread / Today / This week). Unread count surfaces
  in the SectionBar header AND on the Unread chip. Skeleton +
  explicit error block with retry; "Load more · N shown" when more
  pages are available.
- UniboxFilterSheet (advanced filters in the right-side panel):
  free-text query, sender substring, account picker pulled from
  the user's connected mailboxes, status toggle (Any/Unread/Read),
  since/until date pickers with toggle, newest/oldest sort. Draft
  state mirrors parent until Apply.

LivePanel telemetry (sidebar):
- Real WS roundtrip latency. SocketProvider stamps performance.now()
  per heartbeat ref; phoenix phx_reply with that ref computes the
  delta and publishes via setWsLatencyMs. LivePanel colour-codes
  the latency text: <100ms emerald, <300ms amber, ≥300ms red, "—"
  when disconnected.
- Unread count row reads from useAppStore.unseenCount.
- Status label: OFFLINE / CONNECTING / LIVE (with pulse) / IDLE,
  tied to connectionStatus + active mailbox count.

Transactional emails (no more blue gradient):
- base.go rewritten as dashboard chrome: cream #f5f6f8 background,
  white card with hairline #e2e8f0 border, 8px radius, slate-900
  text. Logo monogram in slate, no decorative haze, no gradients.
- login_code / registration_code: tiny uppercase eyebrow + 18px
  bold heading + neutral body + monospace code pill in a hairline-
  bordered box. No serif type.
- reset_password / welcome: same chrome. Slate-900 primary button
  replaces the sky-gradient one. Plaintext link below for accessible
  fallback.
- Template tests updated against the new markup; all green.
2026-05-23 10:07:11 +00:00
Matthew Meszaros c8c4440b50 fix: confirm dialog z + theme, persist folders/tags across reload
User: "when I click on delete the confirm appears behind the form and
it looks really bad, doesn't fit in the theme; and also after I reload
the page, nothing appears after creation".

Two distinct bugs:

1) Confirm dialog stacking + styling
   FoldersModal/TagsModal render at z-[110]. ConfirmProvider rendered
   the confirm overlay at z-101 with bg-black/30 + scale animation +
   poppins styling — visually it landed BEHIND the folders modal and
   clicks went through to the backdrop instead.
   Rewrote ConfirmProvider in the brae chrome:
   - z-[200] so it stacks above page-level overlays AND nested
     dialogs.
   - Hairline-bordered card, 48px header (red alert tile + "Confirm"
     eyebrow), prose body, slate-900 footer (Cancel / red Confirm).
   - Escape closes; backdrop closes (both gated on !loading).
   - Spinner inside Confirm during the awaited action.

2) Created folders/tags disappeared after page reload
   POST /folders + /tags persisted to Postgres fine. The frontend
   optimistic-updated the cached user via setQueryData. But
   /auth/me did not return folders/tags/categories — the User payload
   omitted them entirely. On reload the cache refetched /auth/me,
   got missing fields, defaulted to [], and the items vanished from
   the UI.

   Backend fix:
   - models.User now carries Folders/Tags/Categories ([]Group),
     always serialized as arrays.
   - GroupRepository + GroupService gained a List(ctx, userID)
     method; ordered by position then created_at.
   - /auth/me handler now calls List on FolderService, TagService,
     CategoryService and attaches them to the user before responding.

Verified end-to-end:
  GET /auth/me → 200 with full folders/tags arrays populated.
  Create a folder, reload the page → folder still in the list.
2026-05-23 09:38:20 +00:00
Matthew Meszaros abf73d168b fix: dropdowns + folder/tag create (server-side + client-side)
Two real bugs surfaced from "All folders / Newest dropdowns don't open"
and "hex color must be a valid string":

1) Dropdowns silently no-op (broken across the whole dashboard)
   PopoverMenuTrigger asChild uses React.cloneElement to inject
   onClick / ref / aria-expanded onto the trigger child. SelectButton
   was a plain function component that destructured a fixed prop set
   and rendered its own <button> — so the injected props were
   dropped on the floor. Click did nothing.

   Fix: SelectButton is now React.forwardRef + spreads {...rest} onto
   the inner button. The injected click handler reaches the real
   element, the dropdown opens, the menu renders, and selection
   actually applies state.

   Every PopoverMenu trigger using SelectButton was affected — that's
   campaigns (folders + sort), emails (tag filter), contacts (sort +
   filters page rows). All now work.

2) Adding a folder/tag failed with "hex color must be a valid string"
   The /folders + /tags POST landed on groupRepository.Create with
   an empty color and the validator rejected. Even before the color
   check, the INSERT used tx.QueryRow + Scan against an INSERT with
   no RETURNING clause, which always errored with
   "sql: no rows in result set" once it got past validation.

   API improvements (kept the design but made it forgiving):
   - Color defaults: if the request omits color, the server picks one
     from an 8-swatch palette based on the new item's position. Two
     consecutive creates won't end up identical. Non-empty but
     invalid still 400s — that's a client bug worth surfacing.
   - Title min length 3 → 1. "Q1", "VIP", short names are common
     and shouldn't fail. Trimmed before validation so " " doesn't
     pass.
   - INSERT now uses tx.Exec instead of QueryRow.Scan — the broken
     code would never reach success even when validation passed.

   Verified end-to-end:
     POST /folders {"title":"Q1"} → 200, color=#94a3b8 (default).
     POST /folders {"title":"Q2","color":"#38bdf8"} → 200.
     POST /tags    {"title":"VIP","color":"#10b981"} → 200.

   Frontend:
   - createFolder / createTag clients accept an optional color param.
   - LabelListModal now picks a default palette color when entering
     add-row mode (rotating with item count) and offers a swatch
     popover to override before submitting. Selected color is sent to
     the backend.
2026-05-23 09:18:45 +00:00
Matthew Meszaros 72a6496086 fix(api): /campaigns 500 — SQL referenced cet.tag / cec.folder, columns are tag_id / folder_id
CAMPAIGN_SELECT_FULL had:
  array_agg(cet.tag_id)    FILTER (WHERE cet.tag    IS NOT NULL)
  array_agg(cec.folder_id) FILTER (WHERE cec.folder IS NOT NULL)

The columns referenced in the FILTER clauses don't exist:

  warmbly_dev=# \d campaign_email_tags
   Column    | Type
  -----------+------
   tag_id    | uuid
   campaign_id | uuid

  warmbly_dev=# \d campaign_folders
   Column    | Type
  -----------+------
   campaign_id | uuid
   folder_id   | uuid

Result: every GET /campaigns returned 500 with
  *pgconn.PgError: ERROR: column cet.tag does not exist (SQLSTATE 42703)
which is why the frontend page was perpetually blank — the request was
failing before any data could land. Fixed both FILTER predicates to
use the actual *_id columns.

Verified after rebuild:
  - dev@warmbly.com (no campaigns): 200 with empty data array.
  - beth@beta.test (owns seeded campaign): 200 with the Beta Cold
    Outreach Q1 record.
2026-05-23 05:50:31 +00:00
Matthew Meszaros 695e2b5a33 fix: contacts crash, campaigns panic, websocket — 4 distinct bugs found while triaging the page-blank symptom
1) Contacts crash "c is null":
   contactRepository.Search declared `var contacts []models.Contact` so
   an empty result set returned a nil slice, which Go marshals as JSON
   null. The frontend's flatMap((p) => p.data) over null yields [null],
   and the page then accesses c.subscribed → throws. Initialize as
   make([]models.Contact, 0, limit+1) so the wire format is always [].
   Also defensive on the client: useSearchContacts + useCampaigns now
   coerce p.data ?? [] and drop nulls before returning.

2) Campaigns panic on any non-empty result:
   campaignRepository.Search allocated `make([]models.Campaign, 0, limit+1)`
   (length 0) then did `campaigns[i] = campaign`. That's an
   index-out-of-range on the first iteration. Switched to `append`.
   Anyone with at least one campaign would see a 500 / blank screen.

3) Websocket "Token expired":
   SocketTTL was 60s. The frontend reconnect backoff caps at 30s, so
   after a rejected handshake the next attempt could fire 30-60s
   later. Combined with rare back-pressure on /getaway the token was
   already past exp by the time the realtime saw it. Bumped to 10 min
   — short enough to keep the token low-impact, long enough to outlast
   the backoff schedule.

4) Websocket "Connection limit exceeded":
   Realtime.Connections only untracked on channel terminate, never on
   socket disconnect. Sockets that connected and disconnected without
   joining a channel leaked. Each reconnect loop bumped the counter
   until the per-user limit (10) was hit, after which every legitimate
   connect was rejected even after fixing #3.
   Fix: GenServer Process.monitor's the socket pid on track, and
   `:DOWN` handler calls do_untrack with the right (user_id, ip).

5) Phoenix protocol mismatch:
   Frontend appended vsn=2.0.0 to the WS URL, but sendRaw + joinChannel
   send the V1 object format. Realtime's Phoenix.Socket.V2.JSONSerializer
   crashed with a badmatch on the first phx_join, killing the socket
   right after connect. Switched to vsn=1.0.0 to match what the client
   actually emits.
2026-05-23 05:32:28 +00:00
Matthew Meszaros 0d92f726f9 fix(api+web): root cause of blank campaigns + infinite-loading contacts; new filters sheet
Backend root cause:
The frontend client omits ?limit= when it would equal the default
(DEFAULT_PAGINATION_LIMIT = 50). validate.Limit("") treated empty as
invalid and returned errx.ErrLimit → 400 on /contacts/search and
/campaigns. ContactsTable derived isLoading from `!contacts`, which
stays true forever when the query errors, so the page hung in the
skeleton state instead of surfacing the error.

Fix:
- validate.Limit now accepts "" and returns LimitDefault = 50, in
  sync with the frontend constant. The frontend's omission semantics
  ("don't send the param when it's the default") was already correct;
  it was the validator that was wrong.

Frontend:
- ContactsTable: use isPending/isError/refetch directly from react-query
  instead of deriving from `contacts`. New explicit error block renders
  inside the body with: red alert tile, server error message, Try-again
  button (with spinner during refetch), and Reload-page fallback.
- Campaigns page: same error UI promoted from the old EmptyBlock CTA
  to a prominent block — alert tile + message + retry + reload.

New ContactFilters sheet (was the legacy 800px poppins drawer):
- 420px right-side panel matching the rest of the theme.
- Sticky 48px header with "Filters · N active" eyebrow + close.
- Sticky 48px footer with Reset / Cancel / Apply (slate-900 primary).
- Hairline-divided SectionBars between groups: Search, Custom field
  filters, Sort, Subscription, Campaign membership, Dates.
- Custom field rows pair TextInput + FILTER_TYPES popover + value
  input + remove button — all 28px tall.
- Sort: SelectButton popover + asc/desc toggle.
- Subscription: 3-state pill toggle (Any / Subscribed / Unsubscribed).
- Min/max campaign rows: checkbox toggle + number input + suffix.
- Date rows: checkbox toggle + native date input.
- Draft state mirrors parent until Apply, so editing filters doesn't
  trigger refetches mid-build.
2026-05-23 05:03:48 +00:00
Matthew Meszaros f9c02bba6e fix(db): plug 4 tx leaks + bump pool from 4 → 25 — root cause of 10-min logout
Root cause for the 10-min auto-logout (confirmed via pg_stat_activity):
the postgres pool MaxConns was 4, and four repository functions opened
a tx without committing or rolling back. After four calls each leaked
a connection in "idle in transaction" state. Once all four were gone
the pool was permanently exhausted — every new request that needed a
connection blocked until the client gave up. The 10-min trigger is
because that's when the first /auth/refresh fires; refresh tries to
acquire a connection, hangs, eventually the browser aborts the request,
the frontend treats the failure as session expiry, kicks the user.

The four leaking sites:
  - emailRepository.Search        (drove the leak — Accounts page)
  - campaignRepository.Search
  - sequenceRepository.Create
  - contactRepository.BulkUpdate

Each now has `defer tx.Rollback(ctx)` immediately after Begin, matching
the pattern used in the non-leaky sites in the same files. Rollback is
a no-op after Commit, so this is safe for both read-only tx (Search)
and read-write tx (Create / BulkUpdate).

Additional hardening so a future leak can't silently brick the backend:
  - MaxConns 4 → 25. 4 was reckless even without leaks; one bursty
    admin page would saturate. 25 is still well under postgres'
    default max_connections=100.
  - MinConns 0 → 2. Keep a couple of warm connections at idle so the
    first request after a quiet period doesn't pay the connect cost.
  - idle_in_transaction_session_timeout=300000 (5 min) as a session
    RuntimeParam. If a code path forgets the defer, postgres aborts
    the leaked tx after 5 min and reclaims the connection.
  - statement_timeout=60000 (60 s) as a session RuntimeParam.
    Statement runaway can't pin a connection forever.

Verified after backend restart:
  SELECT count(*) FROM pg_stat_activity
    WHERE datname='warmbly_dev' AND state='idle in transaction';
  → 0
2026-05-23 04:41:40 +00:00
Matthew Meszaros 8267458f4e fix(auth): invalidate cached session after refresh — was logging users out at 10 min
After /auth/refresh, Postgres got the new access + refresh nonces but
the Redis cached session still held the OLD ones. The next request:

  1. Frontend uses the new access token (new access_nonce in JWT)
  2. Backend ValidateAccessToken → GetSession → hits Redis cache
  3. Cached session has the OLD access_nonce
  4. session.AccessNonce != t.Nonce  → ErrToken (401)
  5. Frontend tries to refresh with the new refresh token
  6. RefreshToken → GetSession → again hits stale Redis
  7. sess.RefreshNonce (old) != t.Nonce (new)  → ErrToken
  8. Frontend clears tokens and bounces to /auth/login

The access token's 10-minute TTL was the trigger window because that's
when the first refresh fires. After the first refresh, the stale cache
poisoned every subsequent request.

Fix: delete the cached session after a successful repository update,
mirroring what SwitchOrganization already does for the same reason
(it updates current_organization_id in Postgres and then drops the
Redis copy). Next GetSession misses, re-reads from Postgres, caches
the fresh nonces.

The deleteSession failure path is intentionally swallowed — the
refresh already succeeded and we returned the new tokens, so worst
case is the next request triggers another refresh, not a logout.
2026-05-22 15:37:00 +00:00
Matthew Meszaros 7766b690b4 feat(workers): free-form tags for categorizing the fleet
Migration 000032 + repo + endpoints for arbitrary string tags on
workers. The fixed attributes (worker_type, free_tier, risk_pool)
cover the dimensions assignment logic uses. Tags cover everything
else admins want to group by: region (eu-west, fra), provider
(hetzner, ovh), role (warmup-only, burst-capacity), customer cohort —
whatever.

Schema:
  - worker_tags(worker_id, tag) composite PK
  - tag VARCHAR(64), lowercase + dashed via CHECK constraint
  - ON DELETE CASCADE so deleting a worker drops its tags

Endpoints:
  - GET  /admin/workers/tags             list distinct tags (autocomplete)
  - PUT  /admin/workers/:id/tags         replace tag set; normalizes input

Repo:
  - GetWorkerTags / SetWorkerTags / ListAllWorkerTags
  - HydrateWorkerTags batch-loads tags onto a slice of workers in one
    round-trip so the dashboard list doesn't do N+1 queries

PUT is transactional (delete + bulk insert) so the list view never
catches a worker mid-tag-swap. Auto-derived "smart" labels
(tier:free, pool:risky, state:error) are NOT stored — those are
computed client-side from the worker row so they stay in sync with
the source attributes automatically. Next commit wires the UI.
2026-05-20 14:15:52 +00:00
Matthew Meszaros 2685d6a06d feat(admin): preflight TCP reachability check before creating workers
POST /admin/workers/preflight {host, port} runs a 5s TCP dial against
host:port and returns ok + latency, or an error. Used by the worker
creation wizard to catch typos / firewall problems while the form is
still open — much better UX than discovering an unreachable VPS at the
SSH test step after the row already exists.

Doesn't attempt an SSH handshake (no credentials at this stage). A green
preflight just means "something is listening there." The actual SSH test
runs later, after the admin pastes the generated pubkey.
2026-05-20 14:05:28 +00:00
Matthew Meszaros d0ff189fcd feat(admin-ui): risk pool toggle + Pool column on workers list
Worker list grows a Pool column (clean=green, risky=amber,
quarantine=red badge). Dedicated workers render "n/a" — risk pools are
a shared-worker concept since dedicated workers don't share IPs across
customers.

Worker detail page (shared workers only) gets a "Risk pool" section
with three big buttons. Clicking a non-current pool confirms, then
calls PUT /admin/workers/:id/risk-pool. Action audited with the new
pool value.

Saving doesn't migrate accounts directly — the hourly rebalancer
notices the mismatch and moves mailboxes to a matching-pool worker
on its next tick. Documented in the section's helper text.

Endpoint accepts {risk_pool: "clean"|"risky"|"quarantine"} and is
gated by AdminPermManageWorkers. The worker detail row scan now
includes risk_pool so the column actually has data.
2026-05-19 05:41:41 +00:00
Matthew Meszaros 7e02bb2a5a feat(consumer): hourly risk rebalancer migrates mailboxes between risk pools
New background job in the consumer process:

  1. Pulls up to 1000 mailbox candidates joined with their worst warmup
     health state (across all pools they participate in) and their
     current worker's risk_pool. Dedicated workers are excluded — single
     tenant, segregation not applicable.
  2. Recomputes risk_band from health state via RiskBandFromHealth.
     If it changed, writes the new band.
  3. If the band's matching pool doesn't equal the worker's pool, picks
     a new worker via SelectSharedWorkerForBand and migrates the mailbox.
     Increments/decrements account counts.
  4. Logs each migration to admin_audit_log with action=
     "risk_rebalance_migrate" so operators see what moved and why.

Boot-time run + hourly ticker. Rebalancing is intentionally batch, not
event-driven: warmup health states change on a slow rolling-window basis
(warmup_health_sweep is also hourly), so reacting in real time gains
nothing and would cause thundering-herd migrations.

JobsService gets an AssignmentService dep. Nil disables the job (lets
self-hosters opt out by simply not wiring it).
2026-05-19 05:39:27 +00:00
Matthew Meszaros 3609b2b2cd feat(workers): SelectSharedWorkerForBand — risk-pool-aware assignment
New method on WorkerAssignmentService picks the least-loaded shared
worker whose risk_pool matches the mailbox's risk band. Three-step
fallback chain:

  1. Exact match: same pool, same tier
  2. Fall back to clean pool of the same tier when no matching-pool
     worker is available (better to land risky mailboxes on clean
     workers than refuse; the rebalancer will move them later)
  3. Last resort: any worker of the right tier (preserves legacy
     behavior for installations that haven't provisioned risky/
     quarantine pools)

Existing SelectSharedWorker is unchanged so call sites that don't
know about risk bands keep working. The next commit (background
rebalancer) is the first consumer of the new method.
2026-05-19 05:37:15 +00:00
Matthew Meszaros ba1c10fe19 feat(workers): risk-pool schema + per-mailbox risk band
Threat-level segregation, schema layer. Two new concepts:

  workers.risk_pool ∈ {clean, risky, quarantine}
    buckets shared workers by acceptable risk. Dedicated workers don't
    use it (single tenant = no cross-contamination risk).

  email_accounts.risk_band ∈ {clean, risky, quarantine}
    per-mailbox classification, derived from warmup_health_state by the
    rebalancer (next commit). Never set by user input.

The mapping is one-way and intentionally simple:

  healthy           → clean
  watch, throttled  → risky
  quarantined,      → quarantine
  blocked

Rebalancer code lands in the next commit. This commit just adds:

  - migration 000031 with enums + columns + filtered indexes
  - WorkerRiskPool / EmailRiskBand types + RiskBandFromHealth helper
  - WorkerRepository methods: SetWorkerRiskPool, SetEmailAccountRiskBand,
    GetSharedWorkersByTierAndPool, ListRiskCandidates
  - RiskCandidate result type joining email_accounts + warmup health
    (picks WORST state across pools via CASE ranking) + worker columns
    so the rebalancer can decide migrations in one scan
2026-05-19 05:36:25 +00:00
Matthew Meszaros bc612a76c6 feat(admin): convert a shared worker into a dedicated one for an org
POST /admin/workers/:id/convert-to-dedicated does three things in
sequence:

  1. Drain existing accounts to a supplied drain_to_worker_id (required
     if the source has any accounts; we don't auto-pick per-account
     targets because the right choice depends on each account's
     owning org).
  2. Flip workers.worker_type from "shared" to "dedicated".
  3. Atomic create of dedicated_worker_assignments binding the worker
     to a specific user/subscription (uses the existing
     CreateDedicatedAssignmentIfNotExists so re-running is safe).

Refusal cases:
  - already dedicated → 400
  - has accounts but no drain target → 400 (admin must pick where they go)
  - drain target equals source → 400

Worker detail UI gains a "Convert to dedicated" section, shown only
when the worker is currently shared. Inline form, no modal. The drain
dropdown excludes self, only lists shared+installed workers, sorts
least-loaded first.

Audit-logged with action="convert_to_dedicated" and the user_id,
subscription_id, drain target, and account count in details.
2026-05-19 05:33:21 +00:00
Matthew Meszaros baa12b1d2c test(worker): lock plan-aware assignment contract
The free-tier-vs-paid separation in AssignWorkerToEmail is one of those
rules that's silently load-bearing: if a free org ever slips onto a
premium worker, the IPs of paying customers absorb the deliverability
hit. The code is correct today (strict isPaidOrg check at line 67, free/
premium pool sync at line 116), but nothing was guarding against a
regression.

Five table-thin tests, hand-rolled stub repos (embed the interface as a
nil field so unused methods panic loudly):

  - free org → free shared worker → free warmup pool
  - paid org → premium shared worker → premium warmup pool
  - paid org with DedicatedWorkers > 0 + an assignment → dedicated worker
  - paid org with DedicatedWorkers > 0 but no assignment → falls back to
    premium shared (not free!)
  - SelectSharedWorker with no workers → ErrNoAvailableWorkers

No code changes — this commit is documentation.
2026-05-19 05:30:39 +00:00
Matthew Meszaros 2aefb7da02 feat(consumer): log auto-reassignment events to admin_audit_log
When the dead-worker job reassigns email accounts from a worker whose
heartbeat expired, write a row into admin_audit_log so the dashboard's
audit viewer surfaces these system actions alongside admin-driven ones.

admin_user_id is uuid.Nil (the platform identity), so admins searching
the log can distinguish "system did this" from "an admin did this" by
filtering on that ID. Details include the replacement worker, account
count, and reason.

JobsService gets an optional AdminRepo dep. Nil disables logging — keeps
the contract loose for any other call site that doesn't have one.
2026-05-19 05:21:26 +00:00
Matthew Meszaros 950965fe90 fix(audit): route new admin actions to admin_audit_log
The audit calls added in the last commit went to AuditService.LogAction,
which writes to the general user-facing audit log (Cassandra). The admin
audit-log viewer at /admin/audit-logs queries the admin_audit_log table
in Postgres, so worker / credentials / release actions never showed up.

Add a public AdminService.LogAdminAction that wraps the existing private
logAction (writes to admin_audit_log with the same shape as ban_user /
update_worker / etc.). Repoint h.audit() at it.

Actions now visible in the audit viewer:
  test, install, restart, upgrade, uninstall, rotate_keys, apply,
  assign, system_update, reboot, check_releases (plus the existing
  create/update/delete across workers, AWS creds, and profiles).
2026-05-19 05:21:18 +00:00