diff --git a/.env.example b/.env.example index 1a8f555..a4fe63f 100644 --- a/.env.example +++ b/.env.example @@ -12,6 +12,12 @@ DEFAULT_EVENT_SLUG=demo # Encrypts the session cookie. REQUIRED, min 32 chars. # openssl rand -base64 48 SESSION_SECRET= +# While rotating: the old secret(s), comma-separated, still accepted for +# reading cookies for one session lifetime so nobody is signed out. +# SESSION_SECRET_PREVIOUS= +# Hashes login codes. Defaults to SESSION_SECRET; set it separately in +# production so rotating the session secret leaves codes in flight working. +# OTP_PEPPER= # ── Postgres ────────────────────────────────────────────────── POSTGRES_USER=matchmaker @@ -22,7 +28,18 @@ POSTGRES_PORT=5432 # Used by scripts run on the HOST (migrate/seed/import). # Inside docker-compose the host is `postgres`, not `localhost`. DATABASE_URL=postgres://matchmaker:matchmaker@localhost:5432/matchmaker +# disable (local, no TLS) | require (encrypted, unverified) | verify (encrypted, +# certificate checked against DATABASE_CA_CERT or DATABASE_CA_CERT_FILE). +# Production refuses anything but verify for a remote database. DATABASE_SSL=disable +# DATABASE_CA_CERT_FILE=/etc/ssl/certs/rds-global-bundle.pem + +# Deployed only: the migration task creates these least-privilege logins +# (packages/db/src/ensure-roles.ts); web and worker then use the runtime one. +# DATABASE_RUNTIME_USER=app_runtime +# DATABASE_RUNTIME_PASSWORD= +# KEYCLOAK_DB_USER=keycloak +# KEYCLOAK_DB_PASSWORD= # ── Keycloak ────────────────────────────────────────────────── KEYCLOAK_PORT=8080 @@ -43,11 +60,15 @@ KEYCLOAK_GOOGLE_IDP_ALIAS=google # From Google Cloud Console > APIs & Services > Credentials. # Authorised redirect URI must be: # http://localhost:8080/realms/match-maker/broker/google/endpoint -# Leave blank to develop with the e-mail-code path only. +# Leave blank to develop with the e-mail-code path only; the login page shows +# "Sign in with Google" only when GOOGLE_CLIENT_ID is set. After changing these +# (or KEYCLOAK_CLIENT_SECRET) on an existing realm, run: npm run keycloak:sync GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET= # ── Resend (login e-mails) ──────────────────────────────────── +# The web app refuses to start without a key unless EMAIL_DEV_FALLBACK=true. +# EMAIL_FROM must use a domain verified in Resend. RESEND_API_KEY= EMAIL_FROM=Mr. Bridge @@ -61,6 +82,10 @@ OTP_TTL_SECONDS=600 OTP_MAX_PER_WINDOW=5 OTP_RATE_WINDOW_SECONDS=900 +# Seconds the worker waits before refreshing other people's matches after a +# profile change, so bursts of changes coalesce (default 120). +# MATCH_REFRESH_DELAY_SECONDS=120 + # Proxies in front of the web app that append to X-Forwarded-For (1 behind # the AWS ALB). 0 trusts no header, so per-IP login limits are skipped locally. TRUSTED_PROXY_HOPS=0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 824252a..b1d8b60 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + # The migration gate compares with the base commit. + fetch-depth: 0 - uses: actions/setup-node@v4 with: @@ -47,29 +50,31 @@ jobs: - name: Unit tests run: npx vitest run tests/unit + # Migrations run before the new code starts, and a rollback runs the + # previous image on the migrated schema: released migrations never + # change and new ones only expand. + - name: Migrations are safe to roll back from + run: npx tsx tools/check-migrations.ts "${{ github.event.pull_request.base.sha || github.event.before }}" + integration: name: Integration tests runs-on: ubuntu-latest - services: - postgres: - image: pgvector/pgvector:pg16 - env: - POSTGRES_USER: matchmaker - POSTGRES_PASSWORD: matchmaker - POSTGRES_DB: matchmaker - ports: - - 5432:5432 - options: >- - --health-cmd "pg_isready -U matchmaker" - --health-interval 5s - --health-timeout 5s - --health-retries 20 - + # The same Postgres (with the Keycloak database) and Keycloak (with the + # realm import and theme) that docker compose runs locally. env: DATABASE_URL: postgres://matchmaker:matchmaker@localhost:5432/matchmaker DATABASE_SSL: disable SESSION_SECRET: ci-session-secret-long-enough-for-validation-x + KEYCLOAK_ISSUER: http://localhost:8080/realms/match-maker + KEYCLOAK_PUBLIC_URL: http://localhost:8080 + KEYCLOAK_ADMIN: admin + KEYCLOAK_ADMIN_PASSWORD: admin + KEYCLOAK_CLIENT_ID: match-maker-web + KEYCLOAK_CLIENT_SECRET: ci-client-secret + APP_BASE_URL: http://localhost:3000 + # Suites skip themselves without these services; here that is a failure. + REQUIRE_SERVICES: db,keycloak steps: - uses: actions/checkout@v4 @@ -81,13 +86,104 @@ jobs: - run: npm ci + - name: Start Postgres and Keycloak + run: docker compose up -d --wait postgres keycloak + # The suite calls migrate() itself, so this only proves the runner works # standalone — the way the deploy pipeline invokes it. - name: Apply migrations run: npm run db:migrate + - name: Configure Keycloak + run: | + npm run keycloak:wait + npm run keycloak:sync + - name: Integration tests - run: npx vitest run tests/integration + run: npx vitest run tests/integration --reporter=default --reporter=json --outputFile=vitest-report.json + + # A suite that skipped itself would otherwise count as a pass. + - name: Every integration test ran + run: npx tsx tools/check-test-report.ts vitest-report.json + + - name: Service logs + if: failure() + run: docker compose logs --no-color postgres keycloak | tail -n 300 + + browser: + name: Browser acceptance + runs-on: ubuntu-latest + needs: [quality] + + # The built app and the worker against the compose Postgres and Keycloak. + # Login codes are logged instead of e-mailed and read from the app's log. + env: + DATABASE_URL: postgres://matchmaker:matchmaker@localhost:5432/matchmaker + DATABASE_SSL: disable + SESSION_SECRET: ci-session-secret-long-enough-for-validation-x + KEYCLOAK_ISSUER: http://localhost:8080/realms/match-maker + KEYCLOAK_PUBLIC_URL: http://localhost:8080 + KEYCLOAK_ADMIN: admin + KEYCLOAK_ADMIN_PASSWORD: admin + KEYCLOAK_CLIENT_ID: match-maker-web + KEYCLOAK_CLIENT_SECRET: ci-client-secret + APP_BASE_URL: http://localhost:3000 + EMAIL_DEV_FALLBACK: "true" + MATCH_REFRESH_DELAY_SECONDS: "3" + E2E_BASE_URL: http://localhost:3000 + E2E_WEB_LOG: ${{ github.workspace }}/e2e-web.log + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + cache: npm + + - run: npm ci + + - name: Start Postgres and Keycloak + run: docker compose up -d --wait postgres keycloak + + - name: Migrate, seed and configure Keycloak + run: | + npm run db:migrate + npm run db:seed + npm run keycloak:wait + npm run keycloak:sync + + - name: Build + run: npm run build + + - name: Install Chromium + run: npx playwright install --with-deps chromium + + - name: Start app and worker + run: | + (cd apps/web && nohup npx next start -p 3000 > "$E2E_WEB_LOG" 2>&1 &) + nohup npm run worker > e2e-worker.log 2>&1 & + for i in $(seq 1 60); do + curl -fsS http://localhost:3000/api/health/ready && exit 0 + sleep 2 + done + cat "$E2E_WEB_LOG" + exit 1 + + - name: Browser tests + run: npm run test:e2e + + - name: Keep report and logs + if: failure() + uses: actions/upload-artifact@v4 + with: + name: browser-acceptance + path: | + playwright-report/ + test-results/ + e2e-web.log + e2e-worker.log + retention-days: 7 build: name: Build images diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index bf9fb36..6b8dffa 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -5,6 +5,15 @@ on: branches: [main] workflow_dispatch: inputs: + environment: + description: "Where to deploy. Staging is a separate Terraform stack (environment = staging)." + type: choice + options: [production, staging] + default: production + promote_tag: + description: "Promote: copy this tag's staging images to production unchanged, then migrate and roll out (production only)" + type: string + default: "" deploy_keycloak: description: "Also rebuild and roll out Keycloak" type: boolean @@ -16,7 +25,7 @@ on: # Never let two deploys race for the same service. concurrency: - group: deploy-production + group: deploy-${{ inputs.environment || 'production' }} cancel-in-progress: false permissions: @@ -24,29 +33,59 @@ permissions: id-token: write contents: read +# Staging names follow the Terraform stack (match-maker-staging-*) and never +# fall back to production's repository variables. Each job that touches AWS +# runs in the GitHub environment of the target, so its role, subnets and URL +# come from that environment's secrets and variables. env: + TARGET: ${{ inputs.environment || 'production' }} + STACK: ${{ inputs.environment == 'staging' && 'match-maker-staging' || 'match-maker-prod' }} AWS_REGION: ${{ vars.AWS_REGION || 'ap-south-1' }} - ECS_CLUSTER: ${{ vars.ECS_CLUSTER || 'match-maker-prod-cluster' }} - ECS_WEB_SERVICE: ${{ vars.ECS_WEB_SERVICE || 'match-maker-prod-web' }} - ECS_WORKER_SERVICE: ${{ vars.ECS_WORKER_SERVICE || 'match-maker-prod-worker' }} - ECS_KEYCLOAK_SERVICE: ${{ vars.ECS_KEYCLOAK_SERVICE || 'match-maker-prod-keycloak' }} - MIGRATE_TASK_FAMILY: ${{ vars.MIGRATE_TASK_FAMILY || 'match-maker-prod-migrate' }} + ECS_CLUSTER: ${{ inputs.environment == 'staging' && 'match-maker-staging-cluster' || vars.ECS_CLUSTER || 'match-maker-prod-cluster' }} + ECS_WEB_SERVICE: ${{ inputs.environment == 'staging' && 'match-maker-staging-web' || vars.ECS_WEB_SERVICE || 'match-maker-prod-web' }} + ECS_WORKER_SERVICE: ${{ inputs.environment == 'staging' && 'match-maker-staging-worker' || vars.ECS_WORKER_SERVICE || 'match-maker-prod-worker' }} + ECS_KEYCLOAK_SERVICE: ${{ inputs.environment == 'staging' && 'match-maker-staging-keycloak' || vars.ECS_KEYCLOAK_SERVICE || 'match-maker-prod-keycloak' }} + MIGRATE_TASK_FAMILY: ${{ inputs.environment == 'staging' && 'match-maker-staging-migrate' || vars.MIGRATE_TASK_FAMILY || 'match-maker-prod-migrate' }} jobs: # Decides what ships: the pushed commit, or an existing tag on rollback. plan: name: Plan release runs-on: ubuntu-latest + environment: ${{ inputs.environment || 'production' }} outputs: tag: ${{ steps.plan.outputs.tag }} rollback: ${{ steps.plan.outputs.rollback }} + promote: ${{ steps.plan.outputs.promote }} steps: + # A staging run must find staging's own settings, never production's. + - name: Check the target environment + env: + STACK_NAME: ${{ vars.STACK_NAME }} + run: | + set -euo pipefail + if [ "$TARGET" = "staging" ] && [ "${STACK_NAME:-}" != "match-maker-staging" ]; then + echo "::error::Set STACK_NAME=match-maker-staging (and staging's AWS secrets) in the GitHub 'staging' environment first." + exit 1 + fi + - id: plan env: ROLLBACK_TAG: ${{ inputs.rollback_tag }} + PROMOTE_TAG: ${{ inputs.promote_tag }} run: | set -euo pipefail - if [ -n "${ROLLBACK_TAG:-}" ]; then + echo "promote=false" >> "$GITHUB_OUTPUT" + if [ -n "${PROMOTE_TAG:-}" ]; then + if [ "$TARGET" != "production" ] || [ -n "${ROLLBACK_TAG:-}" ] || ! [[ "$PROMOTE_TAG" =~ ^[0-9a-f]{7,40}$ ]]; then + echo "::error::promote_tag takes an image tag, targets production, and cannot be combined with rollback_tag." + exit 1 + fi + echo "tag=$PROMOTE_TAG" >> "$GITHUB_OUTPUT" + echo "rollback=false" >> "$GITHUB_OUTPUT" + echo "promote=true" >> "$GITHUB_OUTPUT" + echo "Promoting the staging images tagged $PROMOTE_TAG" + elif [ -n "${ROLLBACK_TAG:-}" ]; then if ! [[ "$ROLLBACK_TAG" =~ ^[0-9a-f]{7,40}$ ]]; then echo "::error::rollback_tag must be an image tag (a commit SHA prefix), got '$ROLLBACK_TAG'." exit 1 @@ -67,7 +106,8 @@ jobs: ci: name: Required checks needs: [plan] - if: needs.plan.outputs.rollback != 'true' + # A promoted tag passed these checks when it was built for staging. + if: needs.plan.outputs.rollback != 'true' && needs.plan.outputs.promote != 'true' uses: ./.github/workflows/ci.yml with: build_images: false @@ -76,6 +116,10 @@ jobs: name: Build & push images runs-on: ubuntu-latest needs: [plan, ci] + if: >- + !cancelled() && needs.plan.result == 'success' && needs.plan.outputs.rollback != 'true' && + (needs.plan.outputs.promote == 'true' || needs.ci.result == 'success') + environment: ${{ inputs.environment || 'production' }} env: TAG: ${{ needs.plan.outputs.tag }} steps: @@ -91,7 +135,24 @@ jobs: - uses: docker/setup-buildx-action@v3 + # The same image digests that passed staging; nothing is rebuilt. Needs + # both stacks' repositories in one AWS account and registry. + - name: Copy the staging images + if: needs.plan.outputs.promote == 'true' + env: + REGISTRY: ${{ steps.ecr.outputs.registry }} + run: | + set -euo pipefail + repos="web migrate worker" + if [ "${{ inputs.deploy_keycloak }}" = "true" ]; then repos="$repos keycloak"; fi + for repo in $repos; do + docker buildx imagetools create \ + --tag "$REGISTRY/$STACK/$repo:$TAG" --tag "$REGISTRY/$STACK/$repo:latest" \ + "$REGISTRY/match-maker-staging/$repo:$TAG" + done + - name: Build & push web + if: needs.plan.outputs.promote != 'true' uses: docker/build-push-action@v6 with: context: . @@ -99,24 +160,26 @@ jobs: push: true build-args: APP_VERSION=${{ env.TAG }} tags: | - ${{ steps.ecr.outputs.registry }}/match-maker-prod/web:${{ env.TAG }} - ${{ steps.ecr.outputs.registry }}/match-maker-prod/web:latest + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/web:${{ env.TAG }} + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/web:latest cache-from: type=gha cache-to: type=gha,mode=max - name: Build & push migrate + if: needs.plan.outputs.promote != 'true' uses: docker/build-push-action@v6 with: context: . file: infra/docker/migrate.Dockerfile push: true tags: | - ${{ steps.ecr.outputs.registry }}/match-maker-prod/migrate:${{ env.TAG }} - ${{ steps.ecr.outputs.registry }}/match-maker-prod/migrate:latest + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/migrate:${{ env.TAG }} + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/migrate:latest cache-from: type=gha cache-to: type=gha,mode=max - name: Build & push worker + if: needs.plan.outputs.promote != 'true' uses: docker/build-push-action@v6 with: context: . @@ -124,21 +187,21 @@ jobs: push: true build-args: APP_VERSION=${{ env.TAG }} tags: | - ${{ steps.ecr.outputs.registry }}/match-maker-prod/worker:${{ env.TAG }} - ${{ steps.ecr.outputs.registry }}/match-maker-prod/worker:latest + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/worker:${{ env.TAG }} + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/worker:latest cache-from: type=gha cache-to: type=gha,mode=max - name: Build & push Keycloak - if: inputs.deploy_keycloak + if: inputs.deploy_keycloak && needs.plan.outputs.promote != 'true' uses: docker/build-push-action@v6 with: context: . file: infra/docker/keycloak.Dockerfile push: true tags: | - ${{ steps.ecr.outputs.registry }}/match-maker-prod/keycloak:${{ env.TAG }} - ${{ steps.ecr.outputs.registry }}/match-maker-prod/keycloak:latest + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/keycloak:${{ env.TAG }} + ${{ steps.ecr.outputs.registry }}/${{ env.STACK }}/keycloak:latest cache-from: type=gha cache-to: type=gha,mode=max @@ -146,6 +209,9 @@ jobs: name: Run migrations runs-on: ubuntu-latest needs: [plan, build-and-push] + if: >- + !cancelled() && needs.plan.outputs.rollback != 'true' && needs.build-and-push.result == 'success' + environment: ${{ inputs.environment || 'production' }} env: TAG: ${{ needs.plan.outputs.tag }} steps: @@ -164,7 +230,7 @@ jobs: REGISTRY="$(aws sts get-caller-identity --query Account --output text).dkr.ecr.${AWS_REGION}.amazonaws.com" aws ecs describe-task-definition --task-definition "$MIGRATE_TASK_FAMILY" \ --query 'taskDefinition' > migrate-taskdef.json - jq --arg IMAGE "$REGISTRY/match-maker-prod/migrate:$TAG" ' + jq --arg IMAGE "$REGISTRY/$STACK/migrate:$TAG" ' .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy, .deregisteredAt) @@ -198,7 +264,7 @@ jobs: echo "Exit code: $EXIT_CODE ($REASON)" if [ "$EXIT_CODE" != "0" ]; then - echo "::error::Migration failed — aborting deploy. Check /ecs/match-maker-prod/migrate." + echo "::error::Migration failed — aborting deploy. Check /ecs/$STACK/migrate." exit 1 fi @@ -206,6 +272,7 @@ jobs: name: Roll out runs-on: ubuntu-latest needs: [plan, build-and-push, migrate] + environment: ${{ inputs.environment || 'production' }} # A rollback skips checks, build and migrations (the schema only moves # forward); a normal release needs all of them to have succeeded. if: >- @@ -228,7 +295,7 @@ jobs: repos="web worker" if [ "${{ inputs.deploy_keycloak }}" = "true" ]; then repos="$repos keycloak"; fi for repo in $repos; do - aws ecr describe-images --repository-name "match-maker-prod/$repo" \ + aws ecr describe-images --repository-name "$STACK/$repo" \ --image-ids imageTag="$TAG" >/dev/null \ || { echo "::error::No $repo image tagged $TAG in ECR."; exit 1; } done @@ -252,13 +319,27 @@ jobs: set -euo pipefail REGISTRY="$(aws sts get-caller-identity --query Account --output text).dkr.ecr.${AWS_REGION}.amazonaws.com" - aws ecs describe-task-definition \ - --task-definition "$(aws ecs describe-services --cluster "$ECS_CLUSTER" --services "$ECS_WEB_SERVICE" --query 'services[0].taskDefinition' --output text)" \ + # Start from the family's newest revision, not the one the service is + # running, so settings and secrets changed by Terraform ship too. + FAMILY=$(aws ecs describe-services --cluster "$ECS_CLUSTER" --services "$ECS_WEB_SERVICE" \ + --query 'services[0].taskDefinition' --output text | sed -E 's#^.*task-definition/([^:]+):[0-9]+$#\1#') + aws ecs describe-task-definition --task-definition "$FAMILY" \ --query 'taskDefinition' > taskdef.json + # Production login must send real e-mail: never ship the dev fallback, + # and require the Resend key (an empty value fails the app's startup check). + if jq -e '[.containerDefinitions[].environment[]? | select(.name == "EMAIL_DEV_FALLBACK" and .value == "true")] | length > 0' taskdef.json >/dev/null; then + echo "::error::EMAIL_DEV_FALLBACK=true is set on the web task; login codes would be logged instead of sent." + exit 1 + fi + if ! jq -e '[.containerDefinitions[].secrets[]? | select(.name == "RESEND_API_KEY")] | length > 0' taskdef.json >/dev/null; then + echo "::error::The web task has no RESEND_API_KEY secret; login e-mail cannot be delivered." + exit 1 + fi + # Strip the server-managed fields that RegisterTaskDefinition rejects, # then point the container at the immutable tag. - jq --arg IMAGE "$REGISTRY/match-maker-prod/web:$TAG" ' + jq --arg IMAGE "$REGISTRY/$STACK/web:$TAG" ' .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy, .deregisteredAt) @@ -285,11 +366,12 @@ jobs: set -euo pipefail REGISTRY="$(aws sts get-caller-identity --query Account --output text).dkr.ecr.${AWS_REGION}.amazonaws.com" - aws ecs describe-task-definition \ - --task-definition "$(aws ecs describe-services --cluster "$ECS_CLUSTER" --services "$ECS_WORKER_SERVICE" --query 'services[0].taskDefinition' --output text)" \ + FAMILY=$(aws ecs describe-services --cluster "$ECS_CLUSTER" --services "$ECS_WORKER_SERVICE" \ + --query 'services[0].taskDefinition' --output text | sed -E 's#^.*task-definition/([^:]+):[0-9]+$#\1#') + aws ecs describe-task-definition --task-definition "$FAMILY" \ --query 'taskDefinition' > worker-taskdef.json - jq --arg IMAGE "$REGISTRY/match-maker-prod/worker:$TAG" ' + jq --arg IMAGE "$REGISTRY/$STACK/worker:$TAG" ' .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy, .deregisteredAt) @@ -316,10 +398,11 @@ jobs: run: | set -euo pipefail REGISTRY="$(aws sts get-caller-identity --query Account --output text).dkr.ecr.${AWS_REGION}.amazonaws.com" - aws ecs describe-task-definition \ - --task-definition "$(aws ecs describe-services --cluster "$ECS_CLUSTER" --services "$ECS_KEYCLOAK_SERVICE" --query 'services[0].taskDefinition' --output text)" \ + FAMILY=$(aws ecs describe-services --cluster "$ECS_CLUSTER" --services "$ECS_KEYCLOAK_SERVICE" \ + --query 'services[0].taskDefinition' --output text | sed -E 's#^.*task-definition/([^:]+):[0-9]+$#\1#') + aws ecs describe-task-definition --task-definition "$FAMILY" \ --query 'taskDefinition' > keycloak-taskdef.json - jq --arg IMAGE "$REGISTRY/match-maker-prod/keycloak:$TAG" ' + jq --arg IMAGE "$REGISTRY/$STACK/keycloak:$TAG" ' .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy, .deregisteredAt) diff --git a/.gitignore b/.gitignore index 22f52aa..88b2c56 100644 --- a/.gitignore +++ b/.gitignore @@ -68,6 +68,8 @@ infra/terraform/*.tfstate infra/terraform/*.tfstate.* infra/terraform/*.tfvars !infra/terraform/*.tfvars.example +infra/terraform/environments/*.tfvars +!infra/terraform/environments/*.tfvars.example infra/terraform/*.tfplan infra/terraform/.terraform.lock.hcl infra/terraform/override.tf @@ -116,7 +118,8 @@ temp/ *.swp *.swo *~ -.claude/settings.local.json +# Local Claude Code settings and agents; never pushed. +.claude/ # OS files .DS_Store diff --git a/ReadMe.md b/ReadMe.md index f003029..6a6b387 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -4,7 +4,7 @@ Mr. Bridge is a multi event networking and matchmaking app. Organizers create ev The implementation follows the architecture described in [docs/PROJECT_STRUCTURE.md](docs/PROJECT_STRUCTURE.md). The [end-to-end flow](docs/END_TO_END_FLOW.md) and [matching pipeline](docs/BRIDGE_ENGINE_PIPELINE.md) docs show the product journeys. -See the [production roadmap](docs/PRODUCTION_ROADMAP.md) for authentication findings, the invited-account signup policy, Google setup, and remaining work ordered by implementation effort. +See the [production roadmap](docs/PRODUCTION_ROADMAP.md) for authentication findings, the invited-account signup policy, Google setup, and remaining work ordered by implementation effort. The [operations runbook](docs/OPERATIONS.md) covers releases, staging, rollback, alarms, database restore, secret rotation and the launch checklist. ## Architecture @@ -102,11 +102,20 @@ The first command imports the global attendee directory so the organizer account | `npm run build` | Build workspace packages and the app | | `npm run typecheck` | Type-check all workspaces | | `npm run lint` | Run ESLint | -| `npm test` | Run unit and integration suites | +| `npm test` | Run unit and integration suites. Integration suites skip without Postgres/Keycloak; set `REQUIRE_SERVICES=db,keycloak` to fail instead (CI does, and `tools/check-test-report.ts` fails on any skipped test). Stop a locally running worker first, or it claims the pipeline test's jobs | +| `npm run test:e2e` | Browser acceptance suite (Playwright) against a running app and worker started with `EMAIL_DEV_FALLBACK=true`; set `E2E_BASE_URL` and `E2E_WEB_LOG` (the app's output, where login codes are read) | | `npm run db:migrate` | Apply pending PostgreSQL migrations | +| `npm run capacity:check` | Measure matching throughput, search latency and model calls for an event size against the budgets in the runbook | +| `npm run db:verify` / `db:restore-rehearsal` | Check a database (or a restored copy) is usable; rehearse dump and restore locally | | `npm run db:seed` | Upsert demo roster, organization, event, and organizer | | `npm run db:import -- [event-slug]` | Import attendee rows and optionally attach them to an event | | `npm run keycloak:wait` | Wait for the configured Keycloak realm to respond | +| `npm run keycloak:sync` | Bring an existing realm up to date: client secret, Google credentials, the account-linking flow (Google joins an account an e-mail code created), and the site login theme. Run after the first Keycloak start and after upgrades | +| `npm run account:disable -- ` | Block sign-in and end every session (database and Keycloak); `account:enable` undoes it | +| `npm run account:revoke-sessions -- ` | Sign the person out everywhere without disabling the account | +| `npm run jobs:failed` | List background matching jobs that failed after every retry, with their last error | +| `npm run jobs:retry -- ` | Queue failed jobs again with fresh attempts | +| `npm run account:erase -- --yes` | Deletion request: delete the person, their event profiles, matches, introductions, cached explanations and Keycloak user (recorded in the audit log) | | `npm run docker:up` | Build images and start the local Compose stack | `DATABASE_URL` should point at `localhost` for commands run on the host. Compose sets the service hostname `postgres` inside containers. @@ -125,6 +134,22 @@ See `.env.example` for the full list. The main settings are: | `DEFAULT_EVENT_SLUG` | Event used by the `/` redirect | | `SEED_ON_MIGRATE` | Seeds demo data in the local stack; leave false in production | +## Activating real e-mail and Google sign-in + +Locally, login codes are printed to the web log (`EMAIL_DEV_FALLBACK=true`) and the Google option is hidden. To switch on the real services: + +1. **E-mail:** verify a sending domain in [Resend](https://resend.com), then set `RESEND_API_KEY` and `EMAIL_FROM` (for example `Mr. Bridge `). Set `EMAIL_DEV_FALLBACK=false` to prove delivery works. +2. **Google:** create an OAuth web client in Google Cloud Console with the authorised redirect URI `http://localhost:8080/realms/match-maker/broker/google/endpoint` (use your Keycloak host in other environments). Set `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET`. +3. Apply the credentials to the running realm and restart the web app: + + ```powershell + npm run keycloak:sync + ``` + + Keycloak imports `realm-export.json` only on first boot, so editing `.env` alone does not update an existing realm. + +In AWS, put the same values in the app's Secrets Manager secret (see `terraform output`). The web app refuses to start without `RESEND_API_KEY`, and the deploy workflow rejects a web task that enables `EMAIL_DEV_FALLBACK` or lacks the Resend secret. + ## Docker and deployment Build from the repository root: diff --git a/apps/web/Dockerfile b/apps/web/Dockerfile index 4b1eae2..e5fffc4 100644 --- a/apps/web/Dockerfile +++ b/apps/web/Dockerfile @@ -56,6 +56,10 @@ COPY --from=builder --chown=node:node /repo/apps/web/.next/standalone ./ COPY --from=builder --chown=node:node /repo/apps/web/.next/static ./apps/web/.next/static COPY --from=builder --chown=node:node /repo/apps/web/public ./apps/web/public +# Amazon's RDS CA bundle, so DATABASE_SSL=verify can check the database +# certificate (DATABASE_CA_CERT_FILE points here in the task definitions). +ADD --chmod=644 https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /etc/ssl/certs/rds-global-bundle.pem + # The release tag (commit SHA) the deploy built, reported by /api/health so # smoke tests and rollbacks can tell which version is serving. Declared late # so a new tag does not invalidate the cached layers above. diff --git a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/analytics/page.tsx b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/analytics/page.tsx index ea60a5e..f246a3f 100644 --- a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/analytics/page.tsx +++ b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/analytics/page.tsx @@ -1,25 +1,51 @@ import Link from "next/link"; +import SignOutButton from "@/components/ui/SignOutButton"; import { requireOrganizerEvent } from "@/src/lib/auth"; export default async function AnalyticsPage({ params }: { params: Promise<{ orgSlug: string; eventSlug: string }> }) { const { orgSlug, eventSlug } = await params; const { event, container } = await requireOrganizerEvent(orgSlug, eventSlug); - const analytics = await container.events.analytics(event.id); + const [analytics, activity] = await Promise.all([container.events.analytics(event.id), container.audit.recent(event.id)]); const completion = analytics.roster ? Math.round(analytics.profiles / analytics.roster * 100) : 0; return
- +

Event analytics

{event.name} · {event.status}

- + + +
+

Recent activity

+ {!activity.length ?

No recorded activity yet.

:
    + {activity.map((item) =>
  • + {ACTIVITY_LABELS[item.action] ?? item.action} + {item.subjectName && <> · {item.subjectName}} +
    {item.actorName ?? "System"} · {item.createdAt.toLocaleString("en-IN", { dateStyle: "medium", timeStyle: "short" })}{summary(item.details)} +
  • )} +
}
; } +const ACTIVITY_LABELS: Record = { + "roster.imported": "Roster imported", + "invitations.sent": "Invitations sent", + "access.removed": "Access removed", + "access.restored": "Access restored", + "event.updated": "Settings changed", + "profile_form.updated": "Profile form changed", + "contacts.revealed": "Introduction accepted, contacts shared", +}; + +function summary(details: Record): string { + const parts = ["attached", "sent", "failed", "version"].filter((key) => typeof details[key] === "number").map((key) => `${key} ${details[key]}`); + return parts.length ? ` · ${parts.join(", ")}` : ""; +} + function Card({ label, value, note }: { label: string; value: number | string; note?: string }) { return
{label}{value}{note && {note}}
; } diff --git a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/matching/page.tsx b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/matching/page.tsx index c12ac21..967ca4e 100644 --- a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/matching/page.tsx +++ b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/matching/page.tsx @@ -1,4 +1,5 @@ import Link from "next/link"; +import SignOutButton from "@/components/ui/SignOutButton"; import { computeEventMatchesAction } from "../../../../../../actions/organizer.actions"; import { requireOrganizerEvent } from "@/src/lib/auth"; import { ActionForm } from "@/components/ui/form"; @@ -22,7 +23,7 @@ export default async function MatchingPage({ params, searchParams }: { ]); const action = computeEventMatchesAction.bind(null, orgSlug, eventSlug); return
- +

Matching pipeline

Embeddings and ranked matches run in the background for profiles already completed.

{query.queued &&

Queued profile matching for {query.queued} attendee(s).

} diff --git a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/page.tsx b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/page.tsx index 2b38d84..6c5e849 100644 --- a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/page.tsx +++ b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/page.tsx @@ -1,15 +1,18 @@ import Link from "next/link"; -import { updateEventAction } from "../../../../../actions/organizer.actions"; +import SignOutButton from "@/components/ui/SignOutButton"; +import { updateEventAction, updateProfileFormAction } from "../../../../../actions/organizer.actions"; +import ProfileFormSettings from "@/components/organizer/ProfileFormSettings"; +import { readProfileForm } from "@/src/domain/participant/profile-form"; import { requireOrganizerEvent } from "@/src/lib/auth"; import { EventSetupForm } from "@/components/organizer/EventForms"; export default async function EventDashboardPage({ params, searchParams }: { params: Promise<{ orgSlug: string; eventSlug: string }>; - searchParams: Promise<{ saved?: string }>; + searchParams: Promise<{ saved?: string; form?: string }>; }) { const { orgSlug, eventSlug } = await params; const notice = await searchParams; - const { event, container } = await requireOrganizerEvent(orgSlug, eventSlug); + const { event, container, can } = await requireOrganizerEvent(orgSlug, eventSlug); const analytics = await container.events.analytics(event.id); const action = updateEventAction.bind(null, orgSlug, eventSlug); return
@@ -21,7 +24,7 @@ export default async function EventDashboardPage({ params, searchParams }: { Matching Analytics Create event - +
@@ -30,9 +33,17 @@ export default async function EventDashboardPage({ params, searchParams }: {

Event setup

{notice.saved &&

Event configuration saved.

} - + }} /> :

+ Registration is {event.status}. Only event owners and admins can change this event's settings. +

} + {can("configure") && <> +

Profile form

+

Choose which questions attendees answer, what they are called, and which are required.

+ {notice.form &&

Profile form saved. Attendees missing a newly required answer are asked for it next time they open their profile.

} + + }
; } diff --git a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/roster/page.tsx b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/roster/page.tsx index fc89463..4ef91fb 100644 --- a/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/roster/page.tsx +++ b/apps/web/app/(organizer)/orgs/[orgSlug]/events/[eventSlug]/roster/page.tsx @@ -1,37 +1,95 @@ import Link from "next/link"; -import { importEventRosterAction } from "../../../../../../actions/organizer.actions"; +import SignOutButton from "@/components/ui/SignOutButton"; +import { importEventRosterAction, sendInvitationsAction, setAttendeeAccessAction } from "../../../../../../actions/organizer.actions"; import { requireOrganizerEvent } from "@/src/lib/auth"; import { RosterUploadForm } from "@/components/organizer/EventForms"; +import { ActionForm } from "@/components/ui/form"; +import { canInvite, invitationState, type InvitationState } from "@/src/domain/participant/invitation"; + +const STATE_LABELS: Record = { + not_invited: "Not invited", + invited: "Invited", + failed: "Invitation failed", + claimed: "Signed in", + complete: "Profile complete", + removed: "Access removed", +}; + +const date = (value: unknown) => value instanceof Date ? value : value ? new Date(String(value)) : null; +const when = (value: Date | null) => value ? value.toLocaleString("en-IN", { dateStyle: "medium", timeStyle: "short" }) : ""; export default async function RosterPage({ params, searchParams }: { params: Promise<{ orgSlug: string; eventSlug: string }>; - searchParams: Promise<{ imported?: string; skipped?: string }>; + searchParams: Promise<{ + imported?: string; skipped?: string; already?: string; kept?: string; phones?: string; + invited?: string; failed?: string; access?: string; + }>; }) { const { orgSlug, eventSlug } = await params; const query = await searchParams; - const { event, container } = await requireOrganizerEvent(orgSlug, eventSlug); - const roster = await container.events.roster(event.id); + const { event, container, can } = await requireOrganizerEvent(orgSlug, eventSlug); + const roster = (await container.events.roster(event.id)).map((person) => { + const facts = { + revokedAt: date(person.revoked_at), profileUpdatedAt: date(person.profile_updated_at), claimedAt: date(person.claimed_at), + invitedAt: date(person.invited_at), inviteError: person.invite_error ? String(person.invite_error) : null, + }; + return { person, facts, state: invitationState(facts) }; + }); + const counts = roster.reduce>((all, row) => ({ ...all, [row.state]: (all[row.state] ?? 0) + 1 }), {}); + const pending = (counts.not_invited ?? 0); const action = importEventRosterAction.bind(null, orgSlug, eventSlug); return
- +

Attendee roster

-

{roster.length} registered attendee{roster.length === 1 ? "" : "s"} for {event.name}.

+

{roster.length} attendee{roster.length === 1 ? "" : "s"} on the roster for {event.name}.

{query.imported &&

Added {query.imported} attendee(s) to this event. + {Number(query.already) > 0 && ` ${query.already} were already on the roster.`} {Number(query.skipped) > 0 && ` Skipped ${query.skipped} row(s) with a missing or invalid e-mail, or an e-mail/phone repeated in the file.`} + {Number(query.kept) > 0 && ` Kept the existing name, phone or details of ${query.kept} people already registered (they can update their own profile).`} + {Number(query.phones) > 0 && ` ${query.phones} new people were added without their phone, because it already belongs to someone else.`} +

} + {query.invited &&

0 ? "form-alert" : "notice"}> + Sent {query.invited} invitation(s).{Number(query.failed) > 0 && ` ${query.failed} could not be delivered; see the roster for the reason.`} +

} + {query.access &&

+ {query.access === "removed" ? "Access removed. They are signed out of this event and no longer appear in anyone's results." : "Access restored."}

} - + {can("importRoster") ? + :

Only event owners and admins can import attendees or remove access.

} + {roster.length > 0 &&
+ {(Object.keys(STATE_LABELS) as InvitationState[]).filter((state) => counts[state]).map((state) => + {counts[state]} {STATE_LABELS[state].toLowerCase()})} + {pending > 0 && can("invite") && } +
} {!roster.length ?

No attendees yet. Import an Excel roster to invite people to this event.

: -
- - {roster.map((person) => - - - - )} +
NameE-mailPhoneRoleDomainProfile
{String(person.name ?? "—")}{String(person.email ?? "—")}{String(person.phone ?? "—")}{String(person.designation ?? "—")}{String(person.domain ?? "—")}{person.profile_updated_at ? "Complete" : "Invited"}
+ + {roster.map(({ person, facts, state }) => { + const id = String(person.id); + const detail = state === "failed" ? facts.inviteError + : state === "removed" ? `since ${when(facts.revokedAt)}` + : state === "claimed" || state === "complete" ? `signed in ${when(date(person.claimed_at))}` + : state === "invited" ? `${when(facts.invitedAt)}${Number(person.invite_count) > 1 ? ` · sent ${person.invite_count} times` : ""}` + : ""; + return + + + + + ; + })}
NameE-mailPhoneRoleStatusActions
{String(person.name ?? "—")}{String(person.email ?? "—")}{String(person.phone ?? "—")}{String(person.designation ?? "—")}{STATE_LABELS[state]}{detail && <>
{detail}}
+ {can("invite") && canInvite(facts) && state !== "not_invited" && } + {!can("manageAccess") ? null : state === "removed" + ? + : } +
}
; } const shell = { minHeight: "100vh", maxWidth: 1100, margin: "0 auto", padding: "36px 24px", color: "#f4f4f5" } as const; const table = { width: "100%", borderCollapse: "collapse" as const, textAlign: "left" as const }; +const summary = { display: "flex", flexWrap: "wrap", alignItems: "center", gap: "8px 20px", marginTop: 24 } as const; diff --git a/apps/web/app/(organizer)/orgs/[orgSlug]/events/new/page.tsx b/apps/web/app/(organizer)/orgs/[orgSlug]/events/new/page.tsx index bfaba3d..dafbb6f 100644 --- a/apps/web/app/(organizer)/orgs/[orgSlug]/events/new/page.tsx +++ b/apps/web/app/(organizer)/orgs/[orgSlug]/events/new/page.tsx @@ -1,15 +1,17 @@ import Link from "next/link"; +import SignOutButton from "@/components/ui/SignOutButton"; import { createEventAction } from "../../../../../actions/organizer.actions"; import { requireOrganizationMember } from "@/src/lib/auth"; import { NewEventForm } from "@/components/organizer/EventForms"; export default async function NewEventPage({ params }: { params: Promise<{ orgSlug: string }> }) { const { orgSlug } = await params; - const { organization, container } = await requireOrganizationMember(orgSlug, `/orgs/${orgSlug}/events/new`); - const existing = await container.events.listForOrganization(orgSlug); + const { session, organization, container } = await requireOrganizationMember(orgSlug, `/orgs/${orgSlug}/events/new`); + const existing = await container.events.listForOrganization(orgSlug, session.attendeeId); const create = createEventAction.bind(null, orgSlug); return
← Organizer +

Create an event

Set up a roster, participant profile, and matching flow for {organization.name}.

diff --git a/apps/web/app/(organizer)/orgs/[orgSlug]/events/page.tsx b/apps/web/app/(organizer)/orgs/[orgSlug]/events/page.tsx index 8cbd130..bad065f 100644 --- a/apps/web/app/(organizer)/orgs/[orgSlug]/events/page.tsx +++ b/apps/web/app/(organizer)/orgs/[orgSlug]/events/page.tsx @@ -1,14 +1,16 @@ import Link from "next/link"; +import SignOutButton from "@/components/ui/SignOutButton"; import { requireOrganizationMember } from "@/src/lib/auth"; export default async function OrganizationEventsPage({ params }: { params: Promise<{ orgSlug: string }> }) { const { orgSlug } = await params; - const { organization, container } = await requireOrganizationMember(orgSlug); - const events = await container.events.listForOrganization(orgSlug); + const { session, organization, container, isMember } = await requireOrganizationMember(orgSlug, `/orgs/${orgSlug}/events`, { allowEventOrganizers: true }); + const events = await container.events.listForOrganization(orgSlug, session.attendeeId); return
ORGANIZER +

{organization.name} events

- Create event + {isMember && Create event}
{events.map((event) => {event.name}{event.status}{event.description} diff --git a/apps/web/app/(participant)/e/[eventSlug]/connections/page.tsx b/apps/web/app/(participant)/e/[eventSlug]/connections/page.tsx index 7e65b69..e961833 100644 --- a/apps/web/app/(participant)/e/[eventSlug]/connections/page.tsx +++ b/apps/web/app/(participant)/e/[eventSlug]/connections/page.tsx @@ -1,25 +1,29 @@ import Link from "next/link"; -import { getParticipantConnections, respondToParticipantIntro, shareVenueZoneAction } from "../../../../actions/intro.actions"; +import SignOutButton from "@/components/ui/SignOutButton"; +import { answerLocationAction, getParticipantConnections, requestLocationAction, respondToParticipantIntro, shareVenueZoneAction } from "../../../../actions/intro.actions"; import { requireParticipantEvent } from "@/src/lib/auth"; import { ActionForm } from "@/components/ui/form"; import { eventAllows } from "@/src/domain/shared/event-lifecycle"; export default async function ConnectionsPage({ params, searchParams }: { params: Promise<{ eventSlug: string }>; - searchParams: Promise<{ requested?: string; location?: string }>; + searchParams: Promise<{ requested?: string; location?: string; asked?: string; replied?: string; declined?: string }>; }) { const { eventSlug } = await params; const notice = await searchParams; const { event } = await requireParticipantEvent(eventSlug, `/e/${eventSlug}/connections`, "viewMatches"); const connections = await getParticipantConnections(eventSlug); return
- + INTRODUCTIONS · {event.name}

Connections

{notice.requested &&

Intro request sent. You will see their contact details once they accept.

} - {notice.location &&

Your venue zone is shared with your accepted connections.

} + {notice.location &&

Your venue zone is shared with all your accepted connections for 15 minutes.

} + {notice.asked &&

Request sent. They decide whether to share where they are.

} + {notice.replied &&

Your zone is shared with them for 15 minutes.

} + {notice.declined &&

Request declined. They have been told you would rather not share right now.

} {!connections.length ?

Your intro requests will show up here. Find someone you would like to meet from your matches or search.

:
- {connections.map((connection) =>
+ {connections.map((connection) =>

{connection.participant.name}

{[connection.participant.designation, connection.participant.domain].filter(Boolean).join(" · ")}

@@ -30,7 +34,25 @@ export default async function ConnectionsPage({ params, searchParams }: { {connection.contact.email && } {connection.contact.phone && } {connection.contact.venueZone &&
At the venue: {connection.contact.venueZone}
} - {event.zones.length > 0 && eventAllows(event.status, "shareZone") && + {connection.locationAsked?.status === "answered" &&
Replied: at {connection.locationAsked.zone}
} + {connection.locationAsked?.status === "pending" &&
Asked where they are. Waiting for a reply.
} + {connection.locationAsked?.status === "declined" &&
They would rather not share where they are right now.
} + {event.zones.length > 0 && eventAllows(event.status, "shareZone") && <> + {connection.locationIncoming &&
+ {connection.participant.name} is asking where you are. Sharing is up to you; only they will see it, for 15 minutes. +
+ + + + + +
+
} + {connection.locationAsked?.status !== "pending" && } + } + {event.zones.length > 0 && eventAllows(event.status, "shareZone") && setQuery(event.target.value)} required maxLength={LIMITS.searchQuery} placeholder="e.g. a product designer interested in climate AI" style={input} /> +
+
+ + +
+ {domains.length > 0 &&
+ + +
} +
{error &&

{error}

}

{busy ? "Searching" : searched ? `${results.length} results for ${searched}` : ""}

@@ -58,7 +78,7 @@ export default function SearchForm({ eventSlug }: { eventSlug: string }) {

{[person.designation, person.domain].filter(Boolean).join(" · ")}

{person.aboutMe &&

{person.aboutMe}

}

{person.reason}

- {person.mutualInterest && Your event goals align} + {person.mutualInterest && Likely a strong fit for each other}
)} @@ -69,3 +89,5 @@ export default function SearchForm({ eventSlug }: { eventSlug: string }) { const input = { flex: "1 1 240px", minWidth: 0, padding: 13, borderRadius: 8, border: "1px solid #3f3f46", background: "#151519", color: "#f4f4f5" } as const; const card = { display: "flex", gap: 18, flexWrap: "wrap", alignItems: "center", padding: 20, borderRadius: 12, background: "#151519", border: "1px solid #333" } as const; const muted = { color: "#a1a1aa" } as const; +const filterLabel = { display: "grid", gap: 4, fontSize: 14, color: "#a1a1aa" } as const; +const select = { padding: 10, borderRadius: 8, border: "1px solid #3f3f46", background: "#151519", color: "#f4f4f5", font: "inherit" } as const; diff --git a/apps/web/app/(participant)/e/[eventSlug]/search/page.tsx b/apps/web/app/(participant)/e/[eventSlug]/search/page.tsx index e4eac77..a46c084 100644 --- a/apps/web/app/(participant)/e/[eventSlug]/search/page.tsx +++ b/apps/web/app/(participant)/e/[eventSlug]/search/page.tsx @@ -1,14 +1,16 @@ import Link from "next/link"; +import SignOutButton from "@/components/ui/SignOutButton"; import { requireParticipantEvent } from "@/src/lib/auth"; import SearchForm from "./SearchForm"; export default async function SearchPage({ params }: { params: Promise<{ eventSlug: string }> }) { const { eventSlug } = await params; - await requireParticipantEvent(eventSlug, `/e/${eventSlug}/search`, "search"); + const { event, container } = await requireParticipantEvent(eventSlug, `/e/${eventSlug}/search`, "search"); + const domains = await container.participants.listDomains(event.id); return
- +

Search your event

Describe the people, skills, or collaboration you are looking for.

- +
; } diff --git a/apps/web/app/actions/intro.actions.ts b/apps/web/app/actions/intro.actions.ts index 7876609..0a4484e 100644 --- a/apps/web/app/actions/intro.actions.ts +++ b/apps/web/app/actions/intro.actions.ts @@ -6,6 +6,7 @@ import { respondToIntro } from "../../src/application/use-cases/respond-to-intro import { shareVenueZone } from "../../src/application/use-cases/share-venue-zone"; import { getContainer } from "../../src/composition/container"; import { assertEventAllows } from "../../src/domain/shared/event-lifecycle"; +import { answerLocation, requestLocation } from "../../src/application/use-cases/location-requests"; import { getSession } from "../../lib/session"; import { runFormAction, type FormState } from "../../lib/form-state"; @@ -75,8 +76,11 @@ export async function getParticipantConnections(eventSlug: string) { const event = await container.events.findByEventSlug(eventSlug); if (!event || !(await container.participants.hasEventAccess(event.id, session.attendeeId))) throw new Error("You are not registered for this event"); assertEventAllows(event.status, "viewMatches"); - const requests = await container.intros.listForParticipant(event.id, session.attendeeId); - const revealed = await container.intros.getRevealedContacts(event.id, session.attendeeId); + const [requests, revealed, locationRequests] = await Promise.all([ + container.intros.listForParticipant(event.id, session.attendeeId), + container.intros.getRevealedContacts(event.id, session.attendeeId), + container.locations.forParticipant(event.id, session.attendeeId), + ]); const contacts = new Map(revealed.map(({ intro, contact }) => [intro.id, contact])); return Promise.all(requests.map(async (intro) => { const otherId = intro.requesterId === session.attendeeId ? intro.recipientId : intro.requesterId; @@ -89,6 +93,35 @@ export async function getParticipantConnections(eventSlug: string) { incoming: intro.recipientId === session.attendeeId, participant: { id: otherId, name: other?.name ?? "Participant", designation: other?.designation, domain: other?.domain }, contact: contacts.get(intro.id) ?? null, + locationAsked: locationRequests.find((request) => request.introId === intro.id && request.direction === "outgoing") ?? null, + locationIncoming: locationRequests.find((request) => request.introId === intro.id && request.direction === "incoming" && request.status === "pending") ?? null, }; })); } + +async function participantContext(eventSlug: string, action: "shareZone") { + const session = await getSession(); + if (!session) throw new Error("Sign in first"); + const container = getContainer(); + const event = await container.events.findByEventSlug(eventSlug); + if (!event || !(await container.participants.hasEventAccess(event.id, session.attendeeId))) throw new Error("You are not registered for this event"); + assertEventAllows(event.status, action); + return { session, container, event }; +} + +export async function requestLocationAction(eventSlug: string, introId: string, _state: FormState, formData: FormData): Promise { + return runFormAction(formData, async () => { + const { session, container, event } = await participantContext(eventSlug, "shareZone"); + await requestLocation({ eventId: event.id, introId, requesterId: session.attendeeId }, container); + redirect(`/e/${eventSlug}/connections?asked=1#intro-${introId}`); + }); +} + +export async function answerLocationAction(eventSlug: string, introId: string, requestId: string, share: boolean, _state: FormState, formData: FormData): Promise { + return runFormAction(formData, async () => { + const { session, container, event } = await participantContext(eventSlug, "shareZone"); + const zone = share ? String(formData.get("venueZone") ?? "") : null; + await answerLocation({ eventId: event.id, requestId, recipientId: session.attendeeId, zone, allowedZones: event.zones }, container); + redirect(`/e/${eventSlug}/connections?${share ? "replied=1" : "declined=1"}#intro-${introId}`); + }); +} diff --git a/apps/web/app/actions/match.actions.ts b/apps/web/app/actions/match.actions.ts index 23099e1..2de5008 100644 --- a/apps/web/app/actions/match.actions.ts +++ b/apps/web/app/actions/match.actions.ts @@ -8,22 +8,22 @@ import { assertEventAllows } from "../../src/domain/shared/event-lifecycle"; import { getSession } from "../../lib/session"; import { runFormAction, userMessage, type FormState } from "../../lib/form-state"; -export async function searchEventParticipants(eventSlug: string, searchText: string) { +export async function searchEventParticipants(eventSlug: string, searchText: string, filters: { intent?: string | null; domain?: string | null } = {}) { try { - return { results: await searchEvent(eventSlug, searchText), error: null }; + return { results: await searchEvent(eventSlug, searchText, filters), error: null }; } catch (error) { return { results: [], error: userMessage(error, "Search failed. Please try again.") }; } } -async function searchEvent(eventSlug: string, searchText: string) { +async function searchEvent(eventSlug: string, searchText: string, filters: { intent?: string | null; domain?: string | null }) { const session = await getSession(); if (!session) throw new Error("Sign in to search this event"); const container = getContainer(); const event = await container.events.findByEventSlug(eventSlug); if (!event || !(await container.participants.hasEventAccess(event.id, session.attendeeId))) throw new Error("You are not registered for this event"); assertEventAllows(event.status, "search"); - const results = await searchParticipants({ eventId: event.id, participantId: session.attendeeId, query: searchText }, container); + const results = await searchParticipants({ eventId: event.id, participantId: session.attendeeId, query: searchText, eventIntents: event.intents, filters }, container); return results.map((match) => ({ id: match.participant.attendeeId, name: match.participant.name, diff --git a/apps/web/app/actions/organizer.actions.ts b/apps/web/app/actions/organizer.actions.ts index 2d9ac92..fa5c761 100644 --- a/apps/web/app/actions/organizer.actions.ts +++ b/apps/web/app/actions/organizer.actions.ts @@ -1,13 +1,15 @@ "use server"; import { redirect } from "next/navigation"; -import { createEvent, importEventRoster, queueEventMatches, updateEvent } from "../../src/application/use-cases/manage-event"; +import { configureProfileForm, createEvent, importEventRoster, queueEventMatches, updateEvent } from "../../src/application/use-cases/manage-event"; +import { PROFILE_FIELD_KEYS, type ProfileFieldKey, type ProfileFieldSetting } from "../../src/domain/participant/profile-form"; import { getContainer } from "../../src/composition/container"; import { assertEventAllows } from "../../src/domain/shared/event-lifecycle"; import { LIMITS, ValidationError } from "../../src/domain/shared/validation"; import { runFormAction, type FormState } from "../../lib/form-state"; import { parseRosterWorkbook } from "../../src/lib/excel-importer"; import { getSession } from "../../lib/session"; +import { sendInvitations, setAttendeeAccess } from "../../src/application/use-cases/manage-invitations"; export async function createEventAction(organizationSlug: string, _state: FormState, formData: FormData): Promise { return runFormAction(formData, async () => { @@ -41,8 +43,12 @@ async function importRoster(organizationSlug: string, eventSlug: string, formDat const event = await container.events.findBySlug(organizationSlug, eventSlug); if (!event) throw new Error("Event not found"); const { records, skipped } = parseRosterWorkbook(await file.arrayBuffer()); - const result = await importEventRoster({ eventId: event.id, organizerId: session.attendeeId, records }, container.events); - redirect(`/orgs/${organizationSlug}/events/${eventSlug}/roster?imported=${result.attached}&skipped=${skipped}`); + const result = await importEventRoster({ eventId: event.id, organizerId: session.attendeeId, records }, container.events, container.audit); + const report = new URLSearchParams({ + imported: String(result.attached), skipped: String(skipped), already: String(result.alreadyOnRoster), + kept: String(result.keptExisting), phones: String(result.phoneConflicts), + }); + redirect(`/orgs/${organizationSlug}/events/${eventSlug}/roster?${report}`); } export async function updateEventAction(organizationSlug: string, eventSlug: string, _state: FormState, formData: FormData): Promise { @@ -67,7 +73,7 @@ async function saveEvent(organizationSlug: string, eventSlug: string, formData: status: status as "draft" | "open" | "closed", intents, zones, - }, container.events); + }, container.events, container.audit); redirect(`/orgs/${organizationSlug}/events/${eventSlug}?saved=1`); } @@ -82,7 +88,57 @@ async function queueMatches(organizationSlug: string, eventSlug: string) { const event = await container.events.findBySlug(organizationSlug, eventSlug); if (!event) throw new Error("Event not found"); assertEventAllows(event.status, "computeMatches"); - if (event.status === "draft") await updateEvent({ eventId: event.id, organizerId: session.attendeeId, status: "open" }, container.events); + if (event.status === "draft") await updateEvent({ eventId: event.id, organizerId: session.attendeeId, status: "open" }, container.events, container.audit); const queued = await queueEventMatches({ eventId: event.id, organizerId: session.attendeeId }, container.events, container.enqueueEmbedding); redirect(`/orgs/${organizationSlug}/events/${eventSlug}/matching?queued=${queued}`); } + +/** Invites everyone never invited, or (with attendeeId) resends to one person. */ +export async function sendInvitationsAction(organizationSlug: string, eventSlug: string, attendeeId: string | null, _state: FormState, formData: FormData): Promise { + return runFormAction(formData, async () => { + const session = await getSession(); + if (!session) throw new Error("Sign in as an organizer first"); + const container = getContainer(); + const event = await container.events.findBySlug(organizationSlug, eventSlug); + if (!event) throw new Error("Event not found"); + const base = (process.env.APP_BASE_URL || "").replace(/\/$/, ""); + const result = await sendInvitations({ + eventId: event.id, + organizerId: session.attendeeId, + eventName: event.name, + organizationName: event.organization_name, + eventUrl: `${base}/e/${event.slug}`, + attendeeId: attendeeId ?? undefined, + }, container); + redirect(`/orgs/${organizationSlug}/events/${eventSlug}/roster?invited=${result.sent}&failed=${result.failed}`); + }); +} + +export async function setAttendeeAccessAction(organizationSlug: string, eventSlug: string, attendeeId: string, revoked: boolean, _state: FormState, formData: FormData): Promise { + return runFormAction(formData, async () => { + const session = await getSession(); + if (!session) throw new Error("Sign in as an organizer first"); + const container = getContainer(); + const event = await container.events.findBySlug(organizationSlug, eventSlug); + if (!event) throw new Error("Event not found"); + await setAttendeeAccess({ eventId: event.id, organizerId: session.attendeeId, attendeeId, revoked }, container); + redirect(`/orgs/${organizationSlug}/events/${eventSlug}/roster?access=${revoked ? "removed" : "restored"}`); + }); +} + +export async function updateProfileFormAction(organizationSlug: string, eventSlug: string, _state: FormState, formData: FormData): Promise { + return runFormAction(formData, async () => { + const session = await getSession(); + if (!session) throw new Error("Sign in as an organizer first"); + const container = getContainer(); + const event = await container.events.findBySlug(organizationSlug, eventSlug); + if (!event) throw new Error("Event not found"); + const fields = Object.fromEntries(PROFILE_FIELD_KEYS.map((key) => [key, { + label: String(formData.get(`${key}.label`) ?? ""), + shown: formData.get(`${key}.shown`) === "on", + required: formData.get(`${key}.required`) === "on", + }])) as Record; + await configureProfileForm({ eventId: event.id, organizerId: session.attendeeId, fields, currentConfig: event.form_config }, container.events, container.audit); + redirect(`/orgs/${organizationSlug}/events/${eventSlug}?form=saved`); + }); +} diff --git a/apps/web/app/actions/participant.actions.ts b/apps/web/app/actions/participant.actions.ts index c98e189..b55335b 100644 --- a/apps/web/app/actions/participant.actions.ts +++ b/apps/web/app/actions/participant.actions.ts @@ -5,7 +5,8 @@ import { getContainer } from "../../src/composition/container"; import { assertEventAllows } from "../../src/domain/shared/event-lifecycle"; import { Intent } from "../../src/domain/participant/intent"; import { ValidationError } from "../../src/domain/shared/validation"; -import { clearSession, getSession } from "../../lib/session"; +import { readProfileForm } from "../../src/domain/participant/profile-form"; +import { clearSession, endIdentityProviderSession, getSession } from "../../lib/session"; import { runFormAction, type FormState } from "../../lib/form-state"; import { redirect } from "next/navigation"; @@ -44,11 +45,14 @@ async function saveProfile(eventSlug: string, formData: FormData) { selfDescription: nullable(formData.get("selfDescription")), yearsOfExperience: parseYears(formData.get("yearsOfExperience")), mostImpressiveBuild: nullable(formData.get("mostImpressiveBuild")), - }, { participants: container.participants, enqueueMatching: container.enqueueEmbedding }); + visible: formData.get("visible") === "on", + }, { participants: container.participants }, readProfileForm(event.form_config)); } export async function signOutCurrentUser() { + const session = await getSession(); await clearSession(); + await endIdentityProviderSession(session); redirect("/login"); } diff --git a/apps/web/app/api/auth/google/start/route.js b/apps/web/app/api/auth/google/start/route.js index c2df7ce..026be95 100644 --- a/apps/web/app/api/auth/google/start/route.js +++ b/apps/web/app/api/auth/google/start/route.js @@ -1,13 +1,14 @@ import { NextResponse } from "next/server"; import { attendees } from "@match-maker/db"; import { canReturnToPath } from "@/lib/session"; -import { safeReturnTo } from "@/lib/safe-return-to"; +import { loginErrorPath, safeReturnTo } from "@/lib/safe-return-to"; import { buildAuthorizationUrl, createPkce, randomUrlSafe, sealOAuthState, sessionCookieOptions, + isGoogleSignInEnabled, OAUTH_COOKIE_NAME, OAUTH_COOKIE_MAX_AGE, } from "@match-maker/auth"; @@ -26,6 +27,9 @@ export async function GET(request) { const url = new URL(request.url); const email = url.searchParams.get("email")?.toLowerCase().trim() || ""; const returnTo = safeReturnTo(url.searchParams.get("returnTo")); + if (!isGoogleSignInEnabled()) { + return NextResponse.redirect(new URL(loginErrorPath("google_unavailable", returnTo), url.origin)); + } try { // The browser already checked the roster, but that check is advisory — @@ -33,7 +37,7 @@ export async function GET(request) { if (email) { const attendee = await attendees.findByEmail(email); if (!attendee || !(await canReturnToPath(returnTo, attendee.id))) { - return NextResponse.redirect(new URL("/login?error=unregistered", url.origin)); + return NextResponse.redirect(new URL(loginErrorPath("unregistered", returnTo), url.origin)); } } @@ -58,7 +62,7 @@ export async function GET(request) { return response; } catch (err) { console.error("[auth/google/start]", err); - return NextResponse.redirect(new URL("/login?error=provider", url.origin)); + return NextResponse.redirect(new URL(loginErrorPath("provider", returnTo), url.origin)); } } diff --git a/apps/web/app/api/auth/logout/route.js b/apps/web/app/api/auth/logout/route.js index cea567e..943469c 100644 --- a/apps/web/app/api/auth/logout/route.js +++ b/apps/web/app/api/auth/logout/route.js @@ -1,33 +1,18 @@ import { NextResponse } from "next/server"; -import { buildLogoutUrl } from "@match-maker/auth"; -import { clearSession, getSession } from "@/lib/session"; +import { clearSession, endIdentityProviderSession, getSession } from "@/lib/session"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; /** * Sign out. Clears the local session, and for Google logins also ends the - * Keycloak SSO session — otherwise the next sign-in would silently re-use it - * and appear to ignore the logout. + * Keycloak SSO session server-side — otherwise the next sign-in would silently + * re-use it and appear to ignore the logout. Same behaviour as the Sign out + * button (signOutCurrentUser). */ -export async function POST(request) { +export async function POST() { const session = await getSession(); await clearSession(); - - const origin = process.env.APP_BASE_URL || new URL(request.url).origin; - - if (session?.method === "google") { - try { - return NextResponse.json({ - ok: true, - redirectTo: await buildLogoutUrl(`${origin}/login`), - }); - } catch (err) { - // Keycloak being unreachable must not trap the user in a signed-in state; - // the local cookie is already gone. - console.error("[auth/logout] could not build Keycloak logout URL:", err); - } - } - + await endIdentityProviderSession(session); return NextResponse.json({ ok: true, redirectTo: "/login" }); } diff --git a/apps/web/app/auth/callback/route.js b/apps/web/app/auth/callback/route.js index 91f8b05..90d752e 100644 --- a/apps/web/app/auth/callback/route.js +++ b/apps/web/app/auth/callback/route.js @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { attendees } from "@match-maker/db"; import { canReturnToPath } from "@/lib/session"; -import { safeReturnTo } from "@/lib/safe-return-to"; +import { loginErrorPath, safeReturnTo } from "@/lib/safe-return-to"; import { exchangeCodeForTokens, verifyIdToken, @@ -33,8 +33,11 @@ export async function GET(request) { const providerError = url.searchParams.get("error"); if (providerError) { + // A cancelled Google consent lands here; the state cookie still knows + // which event the user was joining. console.warn("[auth/callback] provider error:", providerError); - return redirectTo(origin, "/login?error=provider"); + const stored = await unsealOAuthState(request.cookies.get(OAUTH_COOKIE_NAME)?.value ?? ""); + return redirectTo(origin, loginErrorPath("provider", stored?.returnTo)); } const token = url.searchParams.get("token"); @@ -44,22 +47,22 @@ export async function GET(request) { const state = url.searchParams.get("state"); if (code && state) return handleAuthorizationCode(request, code, state, origin); - return redirectTo(origin, "/login?error=invalid_callback"); + return redirectTo(origin, loginErrorPath("invalid_callback", url.searchParams.get("returnTo"))); } async function handleMagicLink(token, origin, returnTo) { try { const result = await verifyToken(token); if (!result.ok) { - return redirectTo(origin, `/login?error=${result.reason}`); + return redirectTo(origin, loginErrorPath(result.reason, returnTo)); } const attendee = result.attendeeId ? await attendees.findById(result.attendeeId) : await attendees.findByEmail(result.email); - if (!attendee) return redirectTo(origin, "/login?error=unregistered"); - if (!(await canReturnToPath(returnTo, attendee.id))) return redirectTo(origin, "/login?error=unregistered"); + if (!attendee) return redirectTo(origin, loginErrorPath("unregistered", returnTo)); + if (!(await canReturnToPath(returnTo, attendee.id))) return redirectTo(origin, loginErrorPath("unregistered", returnTo)); const subject = await ensureUser(attendee.email, attendee.name); return await redirectWithSession(origin, returnTo, { @@ -71,36 +74,37 @@ async function handleMagicLink(token, origin, returnTo) { }); } catch (err) { console.error("[auth/callback] magic link:", err); - return redirectTo(origin, "/login?error=verification_failed"); + return redirectTo(origin, loginErrorPath("verification_failed", returnTo)); } } async function handleAuthorizationCode(request, code, state, origin) { + let stored = null; try { - const stored = await unsealOAuthState( + stored = await unsealOAuthState( request.cookies.get(OAUTH_COOKIE_NAME)?.value ?? "" ); // No cookie means the flow was started in another browser or expired; // a mismatch means someone tried to inject their own authorization code. if (!stored || stored.state !== state) { - return redirectTo(origin, "/login?error=state_mismatch"); + return redirectTo(origin, loginErrorPath("state_mismatch", stored?.returnTo)); } const tokens = await exchangeCodeForTokens(code, stored.codeVerifier); const claims = await verifyIdToken(tokens.id_token, stored.nonce); const email = claims.email?.toLowerCase().trim(); - if (!email) return redirectTo(origin, "/login?error=no_email"); - if (claims.email_verified !== true) return redirectTo(origin, "/login?error=email_unverified"); + const returnTo = safeReturnTo(stored.returnTo); + if (!email) return redirectTo(origin, loginErrorPath("no_email", returnTo)); + if (claims.email_verified !== true) return redirectTo(origin, loginErrorPath("email_unverified", returnTo)); // The roster is still the gate: a valid Google account that is not an // attendee gets no session, matching the previous sign-out-on-callback // behaviour. const attendee = await attendees.findByEmail(email); - if (!attendee) return redirectTo(origin, "/login?error=unregistered"); - const returnTo = safeReturnTo(stored.returnTo); - if (!(await canReturnToPath(returnTo, attendee.id))) return redirectTo(origin, "/login?error=unregistered"); + if (!attendee) return redirectTo(origin, loginErrorPath("unregistered", returnTo)); + if (!(await canReturnToPath(returnTo, attendee.id))) return redirectTo(origin, loginErrorPath("unregistered", returnTo)); const response = await redirectWithSession(origin, returnTo, { sub: claims.sub, @@ -113,7 +117,7 @@ async function handleAuthorizationCode(request, code, state, origin) { return response; } catch (err) { console.error("[auth/callback] authorization code:", err); - return redirectTo(origin, "/login?error=verification_failed"); + return redirectTo(origin, loginErrorPath("verification_failed", stored?.returnTo)); } } diff --git a/apps/web/app/globals.css b/apps/web/app/globals.css index a130fed..1454048 100644 --- a/apps/web/app/globals.css +++ b/apps/web/app/globals.css @@ -22,6 +22,8 @@ h1 { font-size: clamp(1.75rem, 5vw, 2.5rem); line-height: 1.2; } } nav { display: flex; flex-wrap: wrap; gap: 8px 20px; margin-bottom: 24px; } +.nav-signout { margin-left: auto; } +.link-button { border: 0; padding: 0; background: none; color: var(--accent); font: inherit; text-decoration: underline; cursor: pointer; } /* Forms */ .form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; margin-top: 28px; } diff --git a/apps/web/app/login.jsx b/apps/web/app/login.jsx index c426028..4fda5fd 100644 --- a/apps/web/app/login.jsx +++ b/apps/web/app/login.jsx @@ -24,6 +24,7 @@ const CODE_LENGTH = 6; const ERROR_MESSAGES = { unregistered: "That account is not on the attendee list.", provider: "Google sign-in failed. Please try again.", + google_unavailable: "Google sign-in isn't available yet. Please use an email code.", state_mismatch: "Your sign-in session expired. Please start again.", verification_failed: "We couldn't verify that sign-in. Please try again.", invalid_callback: "That sign-in link is incomplete. Please try again.", @@ -99,7 +100,7 @@ function StatusPill({ type, text }) { ); } -export default function Login({ onAuthSuccess }) { +export default function Login({ onAuthSuccess, googleEnabled = false }) { const searchParams = useSearchParams(); const returnTo = safeReturnTo(searchParams.get("returnTo")); const [step, setStep] = useState("choose"); @@ -264,7 +265,7 @@ export default function Login({ onAuthSuccess }) {
›
- + {googleEnabled &&
Choose the Google account on your invitation
-
+ }
{status && } diff --git a/apps/web/app/login/login-route.jsx b/apps/web/app/login/login-route.jsx new file mode 100644 index 0000000..b845c53 --- /dev/null +++ b/apps/web/app/login/login-route.jsx @@ -0,0 +1,19 @@ +"use client"; + +import { useRouter } from "next/navigation"; +import Login from "../login.jsx"; + +export default function LoginRoute({ googleEnabled }) { + const router = useRouter(); + return ( + { + // The session cookie is set by the API response, so a refresh is needed + // for the server to see it on the next request. + router.replace(returnTo || "/"); + router.refresh(); + }} + /> + ); +} diff --git a/apps/web/app/login/page.jsx b/apps/web/app/login/page.jsx index 7d1fe1e..396275c 100644 --- a/apps/web/app/login/page.jsx +++ b/apps/web/app/login/page.jsx @@ -1,28 +1,15 @@ -"use client"; - import { Suspense } from "react"; -import { useRouter } from "next/navigation"; -import Login from "../login.jsx"; +import { isGoogleSignInEnabled } from "@match-maker/auth"; +import LoginRoute from "./login-route.jsx"; -function LoginRoute() { - const router = useRouter(); - return ( - { - // The session cookie is set by the API response, so a refresh is needed - // for the server to see it on the next request. - router.replace(returnTo || "/"); - router.refresh(); - }} - /> - ); -} +// Read Google availability per request, not once at build time. +export const dynamic = "force-dynamic"; export default function LoginPage() { // `useSearchParams` (used for the ?error= handling) needs a Suspense boundary. return ( }> - + ); } diff --git a/apps/web/components/organizer/ProfileFormSettings.tsx b/apps/web/components/organizer/ProfileFormSettings.tsx new file mode 100644 index 0000000..e505d30 --- /dev/null +++ b/apps/web/components/organizer/ProfileFormSettings.tsx @@ -0,0 +1,40 @@ +"use client"; + +import { useActionState } from "react"; +import { initialFormState } from "@/lib/form-state"; +import { LABEL_LIMIT, PROFILE_FIELD_KEYS, PROFILE_FIELDS, type ProfileFormConfig } from "@/src/domain/participant/profile-form"; +import { FormAlert, SubmitButton, type FormAction } from "../ui/form"; + +/** Organizer settings for the participant profile form: label, shown, required per field. */ +export default function ProfileFormSettings({ action, form }: { action: FormAction; form: ProfileFormConfig }) { + const [state, formAction] = useActionState(action, initialFormState); + const value = (name: string, fallback: string) => state.values[name] ?? fallback; + const checked = (name: string, fallback: boolean) => state.values[`${name}.submitted`] ? state.values[name] === "on" : fallback; + return
+ +
+ + {PROFILE_FIELD_KEYS.map((key) => { + const setting = form.fields[key]; + const error = state.fieldErrors[`${key}.label`] ?? state.fieldErrors[`${key}.required`]; + return + + + + + ; + })} +
FieldLabel shown to attendeesShowRequired
{PROFILE_FIELDS[key].label} + + + {error && {error}} + + +
+

Hidden fields keep what attendees already saved, and everything saved still feeds matching. Name, intent and venue zone are always asked.

+
Save profile form
+ ; +} + +const input = { width: "100%", minWidth: 180, boxSizing: "border-box", padding: "9px 10px", border: "1px solid #3f3f46", borderRadius: 8, background: "#151519", color: "#f4f4f5", font: "inherit" } as const; diff --git a/apps/web/components/participant/ProfileForm.tsx b/apps/web/components/participant/ProfileForm.tsx index 1124adc..e05f743 100644 --- a/apps/web/components/participant/ProfileForm.tsx +++ b/apps/web/components/participant/ProfileForm.tsx @@ -3,6 +3,7 @@ import { useActionState } from "react"; import { initialFormState } from "@/lib/form-state"; import { LIMITS } from "@/src/domain/shared/validation"; +import type { ProfileFormConfig } from "@/src/domain/participant/profile-form"; import { FormAlert, SelectField, SubmitButton, TextField, type FormAction } from "../ui/form"; export interface ProfileFormValues { @@ -16,36 +17,50 @@ export interface ProfileFormValues { interests: string[]; intent: string; venueZone: string | null; + visible: boolean; } const label = (intent: string) => intent.replaceAll("_", " ").toLowerCase(); -export default function ProfileForm({ action, profile, intents, zones }: { +/** The participant profile, shaped by the event's profile form (labels, hidden and required fields). */ +export default function ProfileForm({ action, profile, intents, zones, form }: { action: FormAction; profile: ProfileFormValues; intents: string[]; zones: string[]; + form: ProfileFormConfig; }) { const [state, formAction] = useActionState(action, initialFormState); + const field = form.fields; return
- - - - - - - + {field.designation.shown && } + {field.domain.shown && } + {field.aboutMe.shown && } + {field.selfDescription.shown && } + {field.mostImpressiveBuild.shown && } + {field.yearsOfExperience.shown && } + {field.interests.shown && } ({ value: intent, label: label(intent) }))} /> {zones.length > 0 && ({ value: zone, label: zone }))} />} +
+ + Hidden, you can still browse and keep your accepted connections; nobody new can find you or request an introduction. +
Save profile and find matches
; } diff --git a/apps/web/components/ui/SignOutButton.tsx b/apps/web/components/ui/SignOutButton.tsx new file mode 100644 index 0000000..ddf9d9b --- /dev/null +++ b/apps/web/components/ui/SignOutButton.tsx @@ -0,0 +1,8 @@ +import { signOutCurrentUser } from "@/app/actions/participant.actions"; + +/** Ends the session from any signed-in page; sits at the end of the page nav. */ +export default function SignOutButton() { + return
+ +
; +} diff --git a/apps/web/lib/safe-return-to.ts b/apps/web/lib/safe-return-to.ts index 881a867..cfb0179 100644 --- a/apps/web/lib/safe-return-to.ts +++ b/apps/web/lib/safe-return-to.ts @@ -4,7 +4,7 @@ export function safeReturnTo(value: unknown): string { typeof value !== "string" || !value.startsWith("/") || value.startsWith("//") || - /[\\\u0000-\u0020\u007f]/.test(value) + /[\\\u0000- \u007f]/.test(value) ) return "/"; try { @@ -16,3 +16,17 @@ export function safeReturnTo(value: unknown): string { return "/"; } } + +/** Where a signed-in user goes; never back to /login, which would loop. */ +export function postLoginPath(value: unknown): string { + const destination = safeReturnTo(value); + return /^\/login(?:[/?#]|$)/.test(destination) ? "/" : destination; +} + +/** Login URL for a failed attempt that keeps the user's intended destination. */ +export function loginErrorPath(error: string, returnTo?: unknown): string { + const params = new URLSearchParams({ error }); + const destination = postLoginPath(returnTo); + if (destination !== "/") params.set("returnTo", destination); + return `/login?${params}`; +} diff --git a/apps/web/lib/session.ts b/apps/web/lib/session.ts index 3e25a75..e18c544 100644 --- a/apps/web/lib/session.ts +++ b/apps/web/lib/session.ts @@ -4,9 +4,11 @@ import { sealSession, unsealSession, sessionCookieOptions, + isSessionActive, + logoutUser, type Session, } from "@match-maker/auth"; -import { events } from "@match-maker/db"; +import { attendees, events } from "@match-maker/db"; import { safeReturnTo } from "./safe-return-to"; export type { Session }; @@ -15,7 +17,9 @@ export type { Session }; export async function getSession(): Promise { const store = await cookies(); const raw = store.get(getSessionConfig().cookieName)?.value; - return raw ? unsealSession(raw) : null; + const session = raw ? await unsealSession(raw) : null; + // A disabled account, revoked sessions, or a disabled Keycloak user end it now. + return session && (await isSessionActive(session)) ? session : null; } /** Issue a session cookie for a freshly authenticated user. */ @@ -31,6 +35,20 @@ export async function setSession( ); } +/** + * Google sign-ins also hold a Keycloak SSO session. Ending it server-side means + * the next Google sign-in starts fresh, without sending the browser through + * Keycloak's "Do you want to log out?" page. Never blocks signing out. + */ +export async function endIdentityProviderSession(session: Session | null): Promise { + if (session?.method !== "google") return; + try { + await logoutUser(session.sub); + } catch (error) { + console.error("[auth] could not end the Keycloak session:", error); + } +} + export async function clearSession(): Promise { const store = await cookies(); store.set(getSessionConfig().cookieName, "", sessionCookieOptions(0)); @@ -53,12 +71,22 @@ export function clientIp(request: Request, hops = Number(process.env.TRUSTED_PRO /** Apply the event roster gate before issuing an event-specific login session. */ export async function canReturnToPath(returnTo: string | null | undefined, attendeeId: string): Promise { + // Every sign-in path checks this, so a disabled account cannot sign in at all. + const account = await attendees.accountState(attendeeId); + if (!account || account.disabled_at) return false; returnTo = safeReturnTo(returnTo); const participantRoute = returnTo.match(/^\/e\/([^/]+)(?:\/|$)/); if (participantRoute) { const event = await events.findByEventSlug(participantRoute[1]); return Boolean(event && await events.isParticipant(event.id, attendeeId)); } + // The event list and "new event" pages belong to the organization, not to an event. + const organizationRoute = returnTo.match(/^\/orgs\/([^/]+)\/events(\/new)?\/?$/); + if (organizationRoute) { + if (await events.hasOrganizationAccess(organizationRoute[1], attendeeId)) return true; + // Organizers of one event may open the list (not the new-event page). + return !organizationRoute[2] && (await events.listForOrganization(organizationRoute[1], attendeeId)).length > 0; + } const organizerRoute = returnTo.match(/^\/orgs\/([^/]+)\/events\/([^/]+)(?:\/|$)/); if (organizerRoute) { const event = await events.findBySlug(organizerRoute[1], organizerRoute[2]); diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index 85ba812..f8be377 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -1,23 +1,40 @@ import { NextResponse, type NextRequest } from "next/server"; +import { getSessionConfig, isSessionActive, sessionCookieOptions, unsealSession } from "@match-maker/auth"; +import { postLoginPath } from "./lib/safe-return-to"; -const SESSION_COOKIE = process.env.SESSION_COOKIE_NAME || "mb_session"; const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS"]); -export function middleware(request: NextRequest) { +export async function middleware(request: NextRequest) { if (request.nextUrl.pathname.startsWith("/api/")) { return SAFE_METHODS.has(request.method) || isSameOrigin(request) ? NextResponse.next() : NextResponse.json({ error: "Cross-site requests are not allowed" }, { status: 403 }); } + return loginRedirect(request); +} - // The login route can skip its form when a session cookie is present. - if (request.cookies.has(SESSION_COOKIE)) { - const url = request.nextUrl.clone(); - url.pathname = `/e/${process.env.DEFAULT_EVENT_SLUG || "demo"}`; - url.search = ""; - return NextResponse.redirect(url); +/** + * A valid session skips the login form and continues to `returnTo`. An expired + * or tampered cookie is cleared so the form shows instead of bouncing the user + * away. An `?error=` stays on the form so the user can switch accounts. + */ +async function loginRedirect(request: NextRequest) { + const { cookieName } = getSessionConfig(); + const raw = request.cookies.get(cookieName)?.value; + if (!raw) return NextResponse.next(); + + const { searchParams } = request.nextUrl; + const unsealed = await unsealSession(raw); + // Same rule as getSession, or a revoked session would loop between /login + // and the page that sent it there. + const session = unsealed && (await isSessionActive(unsealed)) ? unsealed : null; + if (session && !searchParams.has("error")) { + return NextResponse.redirect(new URL(postLoginPath(searchParams.get("returnTo")), request.url)); } - return NextResponse.next(); + + const response = NextResponse.next(); + if (!session) response.cookies.set(cookieName, "", sessionCookieOptions(0)); + return response; } /** @@ -44,4 +61,5 @@ function originOf(url: string | null): string | null { } } -export const config = { matcher: ["/login", "/api/:path*"] }; +// Session cookies are decrypted with node:crypto, so this cannot run on Edge. +export const config = { runtime: "nodejs", matcher: ["/login", "/api/:path*"] }; diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs index e6f283f..e50b24c 100644 --- a/apps/web/next.config.mjs +++ b/apps/web/next.config.mjs @@ -42,6 +42,10 @@ const nextConfig = { { key: "X-Content-Type-Options", value: "nosniff" }, { key: "X-Frame-Options", value: "DENY" }, { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" }, + // Browsers that have seen the HTTPS site never fall back to HTTP. + ...(process.env.APP_BASE_URL?.startsWith("https://") + ? [{ key: "Strict-Transport-Security", value: "max-age=31536000; includeSubDomains" }] + : []), ], }, ]; diff --git a/apps/web/src/application/ports/audit-log.ts b/apps/web/src/application/ports/audit-log.ts new file mode 100644 index 0000000..228950f --- /dev/null +++ b/apps/web/src/application/ports/audit-log.ts @@ -0,0 +1,14 @@ +/** Records sensitive actions. Recording must never block or fail the action itself. */ +export interface AuditLog { + record(entry: { + eventId?: string | null; + actorId?: string | null; + action: string; + subjectId?: string | null; + details?: Record; + }): Promise; + /** The event's most recent records, newest first, with people's names. */ + recent(eventId: string, limit?: number): Promise; createdAt: Date; + }>>; +} diff --git a/apps/web/src/application/ports/candidate-cache.ts b/apps/web/src/application/ports/candidate-cache.ts index 5c5a992..1dc1a0f 100644 --- a/apps/web/src/application/ports/candidate-cache.ts +++ b/apps/web/src/application/ports/candidate-cache.ts @@ -1,4 +1,15 @@ +/** + * Shared cache for search retrieval, visible to every web replica: candidate + * id lists, query embeddings, and short ownership locks that let concurrent + * identical searches compute once. + */ export interface CandidateCache { get(key: string): Promise; set(key: string, candidateIds: string[], ttlSeconds: number): Promise; + getVector(key: string): Promise; + setVector(key: string, vector: number[], ttlSeconds: number): Promise; + /** Takes the lock if free or expired; returns an owner token, or null when someone else holds it. */ + acquire(key: string, ttlMs: number): Promise; + /** Releases the lock only if `token` still owns it. */ + release(key: string, token: string): Promise; } diff --git a/apps/web/src/application/ports/embedding-provider.ts b/apps/web/src/application/ports/embedding-provider.ts index 3054a86..4c5def3 100644 --- a/apps/web/src/application/ports/embedding-provider.ts +++ b/apps/web/src/application/ports/embedding-provider.ts @@ -1,3 +1,13 @@ +/** Limits a caller can put on one provider call. */ +export interface ProviderCallOptions { + /** The most time this call may take, including retries. */ + timeoutMs?: number; + /** Aborts the underlying request when the caller gives up. */ + signal?: AbortSignal; +} + export interface EmbeddingProvider { - embed(text: string, task: "profile" | "query"): Promise; + /** Provider and model; vectors from different ids are different spaces and never mixed. */ + readonly id: string; + embed(text: string, task: "profile" | "query", options?: ProviderCallOptions): Promise; } diff --git a/apps/web/src/application/ports/event-repository.ts b/apps/web/src/application/ports/event-repository.ts index c902136..f3e562b 100644 --- a/apps/web/src/application/ports/event-repository.ts +++ b/apps/web/src/application/ports/event-repository.ts @@ -1,3 +1,5 @@ +import type { OrganizerRoles } from "../../domain/shared/organizer-permissions"; + export interface EventRecord { id: string; organization_id: string; @@ -25,11 +27,21 @@ export interface RosterAttendeeInput { most_impressive_build: string | null; } +export interface RosterImportResult { + created: number; + attached: number; + alreadyOnRoster: number; + keptExisting: number; + phoneConflicts: number; +} + export interface EventRepository { findBySlug(organizationSlug: string, eventSlug: string): Promise; findByEventSlug(eventSlug: string): Promise; findOrganization(slug: string): Promise<{ id: string; name: string; slug: string } | null>; - listForOrganization(organizationSlug: string): Promise; + /** With viewerId, only the events that person may open. */ + listForOrganization(organizationSlug: string, viewerId?: string): Promise; + organizerRoles(eventId: string, attendeeId: string): Promise; hasOrganizationAccess(organizationSlug: string, attendeeId: string): Promise; isOrganizer(eventId: string, attendeeId: string): Promise; create(input: { organizationId: string; organizerId: string; name: string; slug: string; description?: string | null }): Promise; @@ -37,6 +49,14 @@ export interface EventRepository { roster(eventId: string): Promise>>; attachRoster(eventId: string, attendeeIds: string[]): Promise; participantIds(eventId: string): Promise; - importRoster(eventId: string, records: RosterAttendeeInput[]): Promise<{ upserted: number; attached: number }>; - analytics(eventId: string): Promise<{ roster: number; profiles: number; matches: number; pendingIntros: number; acceptedIntros: number }>; + importRoster(eventId: string, records: RosterAttendeeInput[]): Promise; + analytics(eventId: string): Promise<{ + roster: number; invited: number; claimed: number; profiles: number; removed: number; + matches: number; pendingIntros: number; acceptedIntros: number; + }>; + invitationTargets(eventId: string, attendeeId: string | null, resendAfterMinutes: number): Promise>; + recordInvitation(eventId: string, attendeeId: string, error: string | null): Promise; + markClaimed(eventId: string, attendeeId: string): Promise; + setRevoked(eventId: string, attendeeId: string, revoked: boolean): Promise; + rosterState(eventId: string, attendeeId: string): Promise<"active" | "revoked" | null>; } diff --git a/apps/web/src/application/ports/explanation-cache.ts b/apps/web/src/application/ports/explanation-cache.ts new file mode 100644 index 0000000..58cda61 --- /dev/null +++ b/apps/web/src/application/ports/explanation-cache.ts @@ -0,0 +1,11 @@ +/** + * Cached match reasons for single viewer -> candidate pairs, plus the model's + * order for an exact shortlist (a pair's reason never decides an order). + */ +export interface ExplanationCache { + getMany(keys: string[]): Promise>; + setMany(entries: Array<{ key: string; eventId: string; viewerId: string; candidateId: string; reason: string }>, ttlSeconds: number): Promise; + /** Candidate ids in the model's order, or null. */ + getOrder(key: string): Promise; + setOrder(entry: { key: string; eventId: string; viewerId: string; candidateIds: string[] }, ttlSeconds: number): Promise; +} diff --git a/apps/web/src/application/ports/intro-repository.ts b/apps/web/src/application/ports/intro-repository.ts index 2b1167c..bc9e54b 100644 --- a/apps/web/src/application/ports/intro-repository.ts +++ b/apps/web/src/application/ports/intro-repository.ts @@ -4,6 +4,10 @@ export interface IntroRepository { create(input: { eventId: string; requesterId: string; recipientId: string; message?: string | null }): Promise; findById(eventId: string, introId: string): Promise; respond(eventId: string, introId: string, actorId: string, status: Extract): Promise; + /** Requests in either direction between two people. */ + findBetween(eventId: string, a: string, b: string): Promise; + countPendingOutgoing(eventId: string, requesterId: string): Promise; + /** The participant's requests, excluding those with someone removed from the event. */ listForParticipant(eventId: string, participantId: string): Promise; getRevealedContacts(eventId: string, participantId: string): Promise>; } diff --git a/apps/web/src/application/ports/llm-provider.ts b/apps/web/src/application/ports/llm-provider.ts index fb5bbb1..12fef3f 100644 --- a/apps/web/src/application/ports/llm-provider.ts +++ b/apps/web/src/application/ports/llm-provider.ts @@ -1,8 +1,11 @@ import type { MatchCandidate } from "../../domain/matching/match-strategy"; import type { ParticipantProfile } from "../../domain/participant/participant.entity"; +import type { ProviderCallOptions } from "./embedding-provider"; export interface LlmProvider { /** Identifies the provider and model, recorded with every reranked result. */ readonly id: string; - rerank(viewer: ParticipantProfile, candidates: MatchCandidate[]): Promise; + /** Bump when the prompt changes, so cached explanations are not reused across prompts. */ + readonly promptVersion: string; + rerank(viewer: ParticipantProfile, candidates: MatchCandidate[], options?: ProviderCallOptions): Promise; } diff --git a/apps/web/src/application/ports/location-repository.ts b/apps/web/src/application/ports/location-repository.ts new file mode 100644 index 0000000..2d84685 --- /dev/null +++ b/apps/web/src/application/ports/location-repository.ts @@ -0,0 +1,14 @@ +import type { LocationRequestView } from "../../domain/connection/location.rules"; + +export interface LocationRepository { + /** Creates a pending request and queues its notification in one transaction; null when one is already open. */ + create(input: { eventId: string; introId: string; requesterId: string; recipientId: string }): Promise<{ id: string } | null>; + /** + * Answers an open request addressed to `recipientId` with a zone, or + * declines it (zone null), and queues the notice to the asker. False when + * there is no such open request. + */ + answer(input: { eventId: string; requestId: string; recipientId: string; zone: string | null }): Promise; + /** The latest request per connection and direction involving the participant. */ + forParticipant(eventId: string, participantId: string): Promise; +} diff --git a/apps/web/src/application/ports/match-repository.ts b/apps/web/src/application/ports/match-repository.ts index 26d6ed6..8beaed2 100644 --- a/apps/web/src/application/ports/match-repository.ts +++ b/apps/web/src/application/ports/match-repository.ts @@ -2,6 +2,10 @@ import type { MatchCandidate } from "../../domain/matching/match-strategy"; export interface MatchRepository { /** Replaces the participant's matches; array order is the order shown to them. */ - save(eventId: string, participantId: string, candidates: MatchCandidate[], computationVersion: string): Promise; + /** + * With `expectedRevision`, writes only while the participant's profile is + * still at that revision and returns false otherwise (a newer job will). + */ + save(eventId: string, participantId: string, candidates: MatchCandidate[], computationVersion: string, expectedRevision?: number): Promise; listForParticipant(eventId: string, participantId: string, limit?: number): Promise; } diff --git a/apps/web/src/application/ports/metrics.ts b/apps/web/src/application/ports/metrics.ts new file mode 100644 index 0000000..34f8f9d --- /dev/null +++ b/apps/web/src/application/ports/metrics.ts @@ -0,0 +1,6 @@ +export type MetricUnit = "Count" | "Milliseconds" | "Seconds"; + +/** Operational measurements for dashboards and alarms. Never blocks or fails the caller. */ +export interface Metrics { + record(values: Array<{ name: string; value: number; unit: MetricUnit }>, dimensions?: Record): void; +} diff --git a/apps/web/src/application/ports/model-usage.ts b/apps/web/src/application/ports/model-usage.ts new file mode 100644 index 0000000..cf123a8 --- /dev/null +++ b/apps/web/src/application/ports/model-usage.ts @@ -0,0 +1,10 @@ +/** Records each model provider call for usage, cost and reliability reports. Never blocks the call. */ +export interface ModelUsageLog { + record(entry: { + provider: string; + operation: string; + outcome: "ok" | "error" | "timeout" | "rejected"; + latencyMs: number; + inputChars: number; + }): void; +} diff --git a/apps/web/src/application/ports/notification.ts b/apps/web/src/application/ports/notification.ts index bd84477..8bdd3bf 100644 --- a/apps/web/src/application/ports/notification.ts +++ b/apps/web/src/application/ports/notification.ts @@ -1,4 +1,17 @@ +/** One delivered message: who it goes to and the page it links to. */ +export interface NotificationTarget { + recipientEmail: string; + recipientName: string; + otherName: string; + eventName: string; + /** Deep link to the interaction, e.g. the connection on the Connections page. */ + link: string; +} + export interface NotificationPort { - introRequested(input: { recipientEmail: string; recipientName: string; requesterName: string; eventName: string; introId: string }): Promise; - introResponded(input: { recipientEmail: string; recipientName: string; requesterName: string; eventName: string; accepted: boolean }): Promise; + invitation(input: { email: string; name: string | null; eventName: string; organizationName: string; eventUrl: string }): Promise; + introRequested(input: NotificationTarget): Promise; + introAccepted(input: NotificationTarget): Promise; + locationRequested(input: NotificationTarget): Promise; + locationAnswered(input: NotificationTarget & { zone: string | null }): Promise; } diff --git a/apps/web/src/application/ports/participant-repository.ts b/apps/web/src/application/ports/participant-repository.ts index b54e77b..87b5dcd 100644 --- a/apps/web/src/application/ports/participant-repository.ts +++ b/apps/web/src/application/ports/participant-repository.ts @@ -1,16 +1,31 @@ import type { ParticipantProfile, ParticipantProfileInput } from "../../domain/participant/participant.entity"; +import type { SearchFilters } from "../../domain/matching/search-query"; export interface ParticipantRepository { findProfile(eventId: string, attendeeId: string): Promise; + /** Saves the profile and durably queues the work that reflects it. */ saveProfile(input: ParticipantProfileInput): Promise; listCandidates(input: { eventId: string; excludeAttendeeId: string | null; embedding?: number[] | null; + /** Without an embedding, only candidates matching one of these words, best first. */ + terms?: string[]; + filters?: SearchFilters; + /** Only people with one of these intents (the viewer's welcomed intents); null for all. */ + welcomedIntents?: string[] | null; + /** The model behind `embedding`; vectors from other models are not ranked against it. */ + embeddingModel?: string | null; limit?: number; }): Promise; + /** Changes whenever the event's participants change; part of retrieval cache keys. */ + corpusRevision(eventId: string): Promise; + /** Domains present among eligible participants, for the search filter. */ + listDomains(eventId: string): Promise; findCandidatesByIds(eventId: string, ids: string[]): Promise; - setEmbedding(eventId: string, attendeeId: string, embedding: number[]): Promise; + setEmbedding(eventId: string, attendeeId: string, embedding: number[], model: string): Promise; + /** Writes the embedding only while the profile is still at `revision`; false when it moved on. */ + setEmbeddingAt(eventId: string, attendeeId: string, embedding: number[], revision: number, model: string): Promise; shareVenueZone(eventId: string, attendeeId: string, zone: string): Promise; hasEventAccess(eventId: string, attendeeId: string): Promise; } diff --git a/apps/web/src/application/shared-computation.ts b/apps/web/src/application/shared-computation.ts new file mode 100644 index 0000000..20b782d --- /dev/null +++ b/apps/web/src/application/shared-computation.ts @@ -0,0 +1,64 @@ +import type { CandidateCache } from "./ports/candidate-cache"; + +/** How long a computation may hold its lock, and how long others wait for its result. */ +export const SHARED_LOCK_MS = 10_000; +export const SHARED_WAIT_MS = 2_000; +const POLL_MS = 100; + +/** + * Returns a cached value, or computes it once across replicas: the first + * caller takes the lock and computes; the others wait briefly for its result + * and then compute themselves. Any cache or lock failure falls back to + * computing directly, so a cache outage costs work but never fails a search. + */ +export async function computeOnce(input: { + cache: CandidateCache; + key: string; + read: () => Promise; + write: (value: T) => Promise; + compute: () => Promise; + /** Values not worth sharing (e.g. a missing embedding) are returned but not written. */ + shouldStore?: (value: T) => boolean; + waitMs?: number; +}): Promise { + const read = () => input.read().catch((error) => { + console.warn("[cache] read failed; computing directly", error); + return null; + }); + const store = async (value: T) => { + if (input.shouldStore && !input.shouldStore(value)) return; + await input.write(value).catch((error) => console.warn("[cache] write failed", error)); + }; + + const cached = await read(); + if (cached !== null) return cached; + + const token = await input.cache.acquire(`compute:${input.key}`, SHARED_LOCK_MS).catch((error) => { + console.warn("[cache] lock unavailable; computing directly", error); + return undefined; + }); + if (token === undefined) return input.compute(); + + if (token) { + try { + const raced = await read(); + if (raced !== null) return raced; + const value = await input.compute(); + await store(value); + return value; + } finally { + await input.cache.release(`compute:${input.key}`, token).catch(() => {}); + } + } + + // Someone else is computing: wait for their result, then stop waiting. + const deadline = Date.now() + (input.waitMs ?? SHARED_WAIT_MS); + while (Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, POLL_MS)); + const shared = await read(); + if (shared !== null) return shared; + } + const value = await input.compute(); + await store(value); + return value; +} diff --git a/apps/web/src/application/use-cases/compute-matches.ts b/apps/web/src/application/use-cases/compute-matches.ts index a15c0ca..a470fa3 100644 --- a/apps/web/src/application/use-cases/compute-matches.ts +++ b/apps/web/src/application/use-cases/compute-matches.ts @@ -1,5 +1,5 @@ import { MatchingPipeline } from "../../domain/matching/pipeline"; -export function computeMatches(input: { eventId: string; participantId: string; limit?: number }, pipeline: MatchingPipeline) { +export function computeMatches(input: { eventId: string; participantId: string; limit?: number; expectedRevision?: number }, pipeline: MatchingPipeline) { return pipeline.run({ ...input, mode: "profile" }); } diff --git a/apps/web/src/application/use-cases/location-requests.ts b/apps/web/src/application/use-cases/location-requests.ts new file mode 100644 index 0000000..f3d5acf --- /dev/null +++ b/apps/web/src/application/use-cases/location-requests.ts @@ -0,0 +1,34 @@ +import { LOCATION_RULES } from "../../domain/connection/location.rules"; +import type { IntroRepository } from "../ports/intro-repository"; +import type { LocationRepository } from "../ports/location-repository"; +import type { ParticipantRepository } from "../ports/participant-repository"; +import type { RateLimiter } from "../ports/rate-limiter"; + +/** Asks an accepted connection where they are. */ +export async function requestLocation( + input: { eventId: string; introId: string; requesterId: string }, + dependencies: { intros: IntroRepository; participants: ParticipantRepository; locations: LocationRepository; limiter?: RateLimiter } +) { + const intro = await dependencies.intros.findById(input.eventId, input.introId); + if (!intro || intro.status !== "accepted") throw new Error("You can ask once the introduction is accepted"); + if (intro.requesterId !== input.requesterId && intro.recipientId !== input.requesterId) { + throw new Error("This introduction does not belong to your account"); + } + const recipientId = intro.requesterId === input.requesterId ? intro.recipientId : intro.requesterId; + if (!(await dependencies.participants.hasEventAccess(input.eventId, recipientId))) throw new Error("This person is no longer part of the event"); + if (dependencies.limiter && !(await dependencies.limiter.consume(`location:${input.eventId}:${input.requesterId}`, LOCATION_RULES.perHour, 3600))) { + throw new Error("You have asked several people where they are this hour. Please try again later."); + } + const created = await dependencies.locations.create({ eventId: input.eventId, introId: input.introId, requesterId: input.requesterId, recipientId }); + if (!created) throw new Error("You already asked; they have not replied yet"); + return created; +} + +/** Replies to a request with a configured venue zone, or declines it (zone null). */ +export async function answerLocation( + input: { eventId: string; requestId: string; recipientId: string; zone: string | null; allowedZones: string[] }, + dependencies: { locations: LocationRepository } +) { + if (input.zone !== null && !input.allowedZones.includes(input.zone)) throw new Error("Choose a venue zone configured for this event"); + if (!(await dependencies.locations.answer(input))) throw new Error("This request has expired or was already answered"); +} diff --git a/apps/web/src/application/use-cases/manage-event.ts b/apps/web/src/application/use-cases/manage-event.ts index 81e1f13..d800e8f 100644 --- a/apps/web/src/application/use-cases/manage-event.ts +++ b/apps/web/src/application/use-cases/manage-event.ts @@ -1,7 +1,15 @@ import type { EventRepository, RosterAttendeeInput } from "../ports/event-repository"; +import type { AuditLog } from "../ports/audit-log"; import { slugify } from "../../domain/shared/value-objects"; import { Intent } from "../../domain/participant/intent"; import { boundedItems, boundedText, LIMITS, ValidationError } from "../../domain/shared/validation"; +import { assertOrganizerCan, type OrganizerAction } from "../../domain/shared/organizer-permissions"; +import { buildProfileForm, readProfileForm, type ProfileFieldKey, type ProfileFieldSetting } from "../../domain/participant/profile-form"; + +/** Throws unless the attendee's role on the event allows the action. */ +export async function authorizeOrganizer(events: EventRepository, eventId: string, attendeeId: string, action: OrganizerAction): Promise { + assertOrganizerCan(await events.organizerRoles(eventId, attendeeId), action); +} export async function createEvent(input: { organizationSlug: string; organizerId: string; name: string; slug?: string; description?: string }, events: EventRepository) { const organization = await events.findOrganization(input.organizationSlug); @@ -22,8 +30,8 @@ export async function updateEvent(input: { status?: "draft" | "open" | "closed"; intents?: string[]; zones?: string[]; -}, events: EventRepository) { - if (!(await events.isOrganizer(input.eventId, input.organizerId))) throw new Error("You are not an organizer for this event"); +}, events: EventRepository, audit?: AuditLog) { + await authorizeOrganizer(events, input.eventId, input.organizerId, "configure"); const updated = await events.update({ ...input, name: input.name === undefined ? undefined : eventName(input.name)!, @@ -34,17 +42,21 @@ export async function updateEvent(input: { : boundedItems(input.zones, "zones", "venue zones", { count: LIMITS.zoneCount, length: LIMITS.zoneLength }), }); if (!updated) throw new Error("Event not found"); + const fields = Object.keys(input).filter((key) => !["eventId", "organizerId"].includes(key) && input[key as keyof typeof input] !== undefined); + await audit?.record({ eventId: input.eventId, actorId: input.organizerId, action: "event.updated", details: { fields, status: updated.status } }); return updated; } -export async function importEventRoster(input: { eventId: string; organizerId: string; records: RosterAttendeeInput[] }, events: EventRepository) { - if (!(await events.isOrganizer(input.eventId, input.organizerId))) throw new Error("You are not an organizer for this event"); - if (!input.records.length) return { upserted: 0, attached: 0 }; - return events.importRoster(input.eventId, input.records); +export async function importEventRoster(input: { eventId: string; organizerId: string; records: RosterAttendeeInput[] }, events: EventRepository, audit?: AuditLog) { + await authorizeOrganizer(events, input.eventId, input.organizerId, "importRoster"); + if (!input.records.length) return { created: 0, attached: 0, alreadyOnRoster: 0, keptExisting: 0, phoneConflicts: 0 }; + const result = await events.importRoster(input.eventId, input.records); + await audit?.record({ eventId: input.eventId, actorId: input.organizerId, action: "roster.imported", details: { rows: input.records.length, ...result } }); + return result; } export async function queueEventMatches(input: { eventId: string; organizerId: string }, events: EventRepository, enqueue: (eventId: string, participantId: string) => Promise) { - if (!(await events.isOrganizer(input.eventId, input.organizerId))) throw new Error("You are not an organizer for this event"); + await authorizeOrganizer(events, input.eventId, input.organizerId, "computeMatches"); const participants = await events.participantIds(input.eventId); for (const participantId of participants) await enqueue(input.eventId, participantId); return participants.length; @@ -62,3 +74,17 @@ function eventIntents(values: string[]) { } return intents; } + +/** Saves the organizer's profile form settings as a new form version. */ +export async function configureProfileForm( + input: { eventId: string; organizerId: string; fields: Record; currentConfig: unknown }, + events: EventRepository, + audit?: AuditLog +) { + await authorizeOrganizer(events, input.eventId, input.organizerId, "configure"); + const form = buildProfileForm(input.fields, readProfileForm(input.currentConfig)); + const updated = await events.update({ eventId: input.eventId, organizerId: input.organizerId, formConfig: { ...form } }); + if (!updated) throw new Error("Event not found"); + await audit?.record({ eventId: input.eventId, actorId: input.organizerId, action: "profile_form.updated", details: { version: form.version } }); + return form; +} diff --git a/apps/web/src/application/use-cases/manage-invitations.ts b/apps/web/src/application/use-cases/manage-invitations.ts new file mode 100644 index 0000000..f6bb05d --- /dev/null +++ b/apps/web/src/application/use-cases/manage-invitations.ts @@ -0,0 +1,67 @@ +import { RESEND_AFTER_MINUTES } from "../../domain/participant/invitation"; +import type { EventRepository } from "../ports/event-repository"; +import type { NotificationPort } from "../ports/notification"; +import { authorizeOrganizer } from "./manage-event"; +import type { AuditLog } from "../ports/audit-log"; + +export interface InvitationResult { + sent: number; + failed: number; +} + +/** + * E-mails invitations: to one attendee (a resend), or to everyone on the + * roster never invited. Each attempt is recorded, so a failed delivery shows + * up on the roster instead of disappearing. + */ +export async function sendInvitations( + input: { eventId: string; organizerId: string; eventName: string; organizationName: string; eventUrl: string; attendeeId?: string }, + dependencies: { events: EventRepository; notifications: NotificationPort; audit?: AuditLog } +): Promise { + await authorizeOrganizer(dependencies.events, input.eventId, input.organizerId, "invite"); + const targets = await dependencies.events.invitationTargets(input.eventId, input.attendeeId ?? null, RESEND_AFTER_MINUTES); + if (input.attendeeId && !targets.length) { + throw new Error(`This attendee cannot be invited right now: they may have signed in already, been removed, or been invited in the last ${RESEND_AFTER_MINUTES} minutes.`); + } + const result: InvitationResult = { sent: 0, failed: 0 }; + for (const target of targets) { + try { + await dependencies.notifications.invitation({ + email: target.email, + name: target.name, + eventName: input.eventName, + organizationName: input.organizationName, + eventUrl: input.eventUrl, + }); + await dependencies.events.recordInvitation(input.eventId, target.attendee_id, null); + result.sent += 1; + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + await dependencies.events.recordInvitation(input.eventId, target.attendee_id, message.slice(0, 300)); + result.failed += 1; + } + } + await dependencies.audit?.record({ + eventId: input.eventId, actorId: input.organizerId, action: "invitations.sent", + subjectId: input.attendeeId ?? null, details: { ...result, resend: Boolean(input.attendeeId) }, + }); + return result; +} + +/** + * Removes or restores one attendee's access. Removal takes effect on their + * next request (existing sessions included) and drops them from matching and + * everyone's results; their history stays for the organizer. + */ +export async function setAttendeeAccess( + input: { eventId: string; organizerId: string; attendeeId: string; revoked: boolean }, + dependencies: { events: EventRepository; audit?: AuditLog } +): Promise { + await authorizeOrganizer(dependencies.events, input.eventId, input.organizerId, "manageAccess"); + if (!(await dependencies.events.setRevoked(input.eventId, input.attendeeId, input.revoked))) { + throw new Error("This attendee is not on the event roster"); + } + await dependencies.audit?.record({ + eventId: input.eventId, actorId: input.organizerId, action: input.revoked ? "access.removed" : "access.restored", subjectId: input.attendeeId, + }); +} diff --git a/apps/web/src/application/use-cases/request-intro.ts b/apps/web/src/application/use-cases/request-intro.ts index 1dc00b1..7be99a7 100644 --- a/apps/web/src/application/use-cases/request-intro.ts +++ b/apps/web/src/application/use-cases/request-intro.ts @@ -1,12 +1,14 @@ import type { IntroRepository } from "../ports/intro-repository"; import type { ParticipantRepository } from "../ports/participant-repository"; -import type { NotificationPort } from "../ports/notification"; import { boundedText, LIMITS } from "../../domain/shared/validation"; +import { INTRO_LIMITS, introRequestRefusal } from "../../domain/connection/connection.rules"; +import type { RateLimiter } from "../ports/rate-limiter"; export async function requestIntro( input: { eventId: string; requesterId: string; recipientId: string; message?: string | null; eventName?: string }, - dependencies: { participants: ParticipantRepository; intros: IntroRepository; notifications?: NotificationPort } + dependencies: { participants: ParticipantRepository; intros: IntroRepository; limiter?: RateLimiter } ) { + if (input.requesterId === input.recipientId) throw new Error("You cannot request an intro to yourself"); if (!(await dependencies.participants.hasEventAccess(input.eventId, input.requesterId))) throw new Error("You are not registered for this event"); if (!(await dependencies.participants.hasEventAccess(input.eventId, input.recipientId))) throw new Error("This participant is not in the event"); const [requester, recipient] = await Promise.all([ @@ -15,15 +17,15 @@ export async function requestIntro( ]); if (!requester || !recipient || !recipient.visible || !recipient.profileComplete) throw new Error("This participant is unavailable"); const message = boundedText(input.message, "message", "Message", LIMITS.introMessage); - const intro = await dependencies.intros.create({ ...input, message }); - if (recipient.email && dependencies.notifications) { - await dependencies.notifications.introRequested({ - recipientEmail: recipient.email, - recipientName: recipient.name, - requesterName: requester.name, - eventName: input.eventName ?? "your event", - introId: intro.id, - }).catch((error) => console.warn("[intro] notification failed", error)); + const refusal = introRequestRefusal(input.requesterId, await dependencies.intros.findBetween(input.eventId, input.requesterId, input.recipientId)); + if (refusal) throw new Error(refusal); + if (await dependencies.intros.countPendingOutgoing(input.eventId, input.requesterId) >= INTRO_LIMITS.pendingOutgoing) { + throw new Error(`You have ${INTRO_LIMITS.pendingOutgoing} requests waiting for an answer. Wait for replies before asking more people.`); + } + if (dependencies.limiter && !(await dependencies.limiter.consume(`intro:${input.eventId}:${input.requesterId}`, INTRO_LIMITS.perHour, 3600))) { + throw new Error("You have sent many introduction requests this hour. Please try again later."); } + // The notification is queued with the request (outbox) and delivered by the worker. + const intro = await dependencies.intros.create({ ...input, message }); return intro; } diff --git a/apps/web/src/application/use-cases/respond-to-intro.ts b/apps/web/src/application/use-cases/respond-to-intro.ts index c7f880c..5f67aea 100644 --- a/apps/web/src/application/use-cases/respond-to-intro.ts +++ b/apps/web/src/application/use-cases/respond-to-intro.ts @@ -1,26 +1,19 @@ import type { IntroRepository } from "../ports/intro-repository"; -import type { NotificationPort } from "../ports/notification"; import type { ParticipantRepository } from "../ports/participant-repository"; +import type { AuditLog } from "../ports/audit-log"; export async function respondToIntro( input: { eventId: string; introId: string; actorId: string; accept: boolean; eventName?: string }, - dependencies: { intros: IntroRepository; participants: ParticipantRepository; notifications?: NotificationPort } + dependencies: { intros: IntroRepository; participants: ParticipantRepository; audit?: AuditLog } ) { const existing = await dependencies.intros.findById(input.eventId, input.introId); if (!existing) throw new Error("Intro request not found"); + if (input.accept && !(await dependencies.participants.hasEventAccess(input.eventId, existing.requesterId))) { + throw new Error("This person is no longer part of the event"); + } const updated = await dependencies.intros.respond(input.eventId, input.introId, input.actorId, input.accept ? "accepted" : "declined"); - if (updated.status === "accepted" && dependencies.notifications) { - const requester = await dependencies.participants.findProfile(input.eventId, updated.requesterId); - const recipient = await dependencies.participants.findProfile(input.eventId, updated.recipientId); - if (requester?.email) { - await dependencies.notifications.introResponded({ - recipientEmail: requester.email, - recipientName: requester.name, - requesterName: recipient?.name ?? "Your connection", - eventName: input.eventName ?? "your event", - accepted: true, - }).catch((error) => console.warn("[intro] notification failed", error)); - } + if (updated.status === "accepted") { + await dependencies.audit?.record({ eventId: input.eventId, actorId: input.actorId, action: "contacts.revealed", subjectId: updated.requesterId }); } return updated; } diff --git a/apps/web/src/application/use-cases/search-participants.ts b/apps/web/src/application/use-cases/search-participants.ts index bb6f1e2..504fdc0 100644 --- a/apps/web/src/application/use-cases/search-participants.ts +++ b/apps/web/src/application/use-cases/search-participants.ts @@ -1,14 +1,33 @@ import { MatchingPipeline } from "../../domain/matching/pipeline"; +import { normalizeFilters } from "../../domain/matching/search-query"; +import type { ParticipantRepository } from "../ports/participant-repository"; import type { RateLimiter } from "../ports/rate-limiter"; +import type { Metrics } from "../ports/metrics"; import { boundedText, LIMITS } from "../../domain/shared/validation"; export async function searchParticipants( - input: { eventId: string; participantId: string; query: string; limit?: number }, - dependencies: { pipeline: MatchingPipeline; limiter?: RateLimiter } + input: { eventId: string; participantId: string; query: string; eventIntents: string[]; filters?: { intent?: string | null; domain?: string | null }; limit?: number }, + dependencies: { pipeline: MatchingPipeline; participants: ParticipantRepository; limiter?: RateLimiter; metrics?: Metrics } ) { if (dependencies.limiter && !(await dependencies.limiter.consume(`search:${input.participantId}`, 30, 60))) { throw new Error("Too many searches. Wait a moment and try again."); } const query = boundedText(input.query, "query", "Search", LIMITS.searchQuery, { required: true })!; - return dependencies.pipeline.run({ ...input, query, mode: "search" }); + const filters = normalizeFilters(input.filters ?? {}, { + intents: input.eventIntents, + domains: input.filters?.domain ? await dependencies.participants.listDomains(input.eventId) : [], + }); + const started = Date.now(); + let failed = 0; + try { + return await dependencies.pipeline.run({ eventId: input.eventId, participantId: input.participantId, query, filters, limit: input.limit, mode: "search" }); + } catch (error) { + failed = 1; + throw error; + } finally { + dependencies.metrics?.record([ + { name: "SearchLatencyMs", value: Date.now() - started, unit: "Milliseconds" }, + { name: "SearchFailures", value: failed, unit: "Count" }, + ]); + } } diff --git a/apps/web/src/application/use-cases/upsert-participant.ts b/apps/web/src/application/use-cases/upsert-participant.ts index 0e8386a..7539664 100644 --- a/apps/web/src/application/use-cases/upsert-participant.ts +++ b/apps/web/src/application/use-cases/upsert-participant.ts @@ -1,11 +1,21 @@ import { validateProfile, type ParticipantProfileInput } from "../../domain/participant/participant.entity"; +import { PROFILE_FIELD_KEYS, type ProfileFormConfig } from "../../domain/participant/profile-form"; import type { ParticipantRepository } from "../ports/participant-repository"; export async function upsertParticipant( input: ParticipantProfileInput, - dependencies: { participants: ParticipantRepository; enqueueMatching?: (eventId: string, participantId: string) => Promise } + dependencies: { participants: ParticipantRepository }, + form?: ProfileFormConfig ) { - const profile = await dependencies.participants.saveProfile(validateProfile(input)); - await dependencies.enqueueMatching?.(profile.eventId, profile.attendeeId); - return profile; + let values = input; + if (form && PROFILE_FIELD_KEYS.some((key) => !form.fields[key].shown)) { + // A hidden field keeps what was saved before, so changing the form never wipes profiles. + const existing = await dependencies.participants.findProfile(input.eventId, input.attendeeId); + values = { ...input }; + for (const key of PROFILE_FIELD_KEYS) { + if (!form.fields[key].shown) Object.assign(values, { [key]: existing?.[key] ?? (key === "interests" ? [] : null) }); + } + } + // saveProfile queues the embedding and matching in the same transaction. + return dependencies.participants.saveProfile(validateProfile(values, form)); } diff --git a/apps/web/src/composition/container.ts b/apps/web/src/composition/container.ts index d0f73be..5319bc2 100644 --- a/apps/web/src/composition/container.ts +++ b/apps/web/src/composition/container.ts @@ -4,11 +4,17 @@ import { PostgresMatchRepository } from "../infrastructure/postgres/match.repo"; import { PostgresParticipantRepository } from "../infrastructure/postgres/participant.repo"; import { PostgresRateLimiter } from "../infrastructure/cache/postgres-rate-limiter"; import { PostgresEventRepository } from "../infrastructure/postgres/event.repo"; +import { PostgresAuditLog } from "../infrastructure/postgres/audit.repo"; +import { PostgresLocationRepository } from "../infrastructure/postgres/location.repo"; import { PostgresMatchQueue } from "../infrastructure/postgres/match-queue.repo"; import { PostgresCandidateCache } from "../infrastructure/cache/postgres-candidate-cache"; +import { PostgresExplanationCache } from "../infrastructure/cache/postgres-explanation-cache"; import { UpstashCandidateCache } from "../infrastructure/cache/upstash-candidate-cache"; import { RealtimeNotifier } from "../infrastructure/notifications/realtime.notifier"; import { ProviderRegistry } from "../infrastructure/providers/provider-registry"; +import { GatewayEmbeddingProvider, GatewayLlmProvider } from "../infrastructure/providers/model-gateway"; +import { PostgresModelUsageLog } from "../infrastructure/postgres/model-usage.repo"; +import { EmfMetrics, MeteredModelUsageLog } from "../infrastructure/observability/emf-metrics"; import { MatchingPipeline } from "../domain/matching/pipeline"; const globalContainer = globalThis as typeof globalThis & { __matchMakerContainer?: ReturnType }; @@ -18,8 +24,11 @@ function buildContainer() { const matches = new PostgresMatchRepository(); const intros = new PostgresIntroRepository(); const events = new PostgresEventRepository(); - const embeddings = ProviderRegistry.embedding(); - const llm = ProviderRegistry.llm(); + const metrics = new EmfMetrics(); + const usage = new MeteredModelUsageLog(new PostgresModelUsageLog(), metrics); + const embeddings = new GatewayEmbeddingProvider(ProviderRegistry.embedding(), usage); + const rawLlm = ProviderRegistry.llm(); + const llm = rawLlm ? new GatewayLlmProvider(rawLlm, usage) : null; const candidateCache = new UpstashCandidateCache(new PostgresCandidateCache()); return { participants, @@ -30,9 +39,12 @@ function buildContainer() { llm, candidateCache, matchQueue: new PostgresMatchQueue(), + audit: new PostgresAuditLog(), + locations: new PostgresLocationRepository(), notifications: new RealtimeNotifier(), limiter: new PostgresRateLimiter(), - pipeline: new MatchingPipeline(participants, matches, embeddings, llm, candidateCache), + metrics, + pipeline: new MatchingPipeline(participants, matches, embeddings, llm, candidateCache, new PostgresExplanationCache()), enqueueMatching: matchingJobs.enqueue, enqueueEmbedding: (eventId: string, participantId: string) => matchingJobs.enqueue(eventId, participantId, "embed"), }; diff --git a/apps/web/src/domain/connection/connection.rules.ts b/apps/web/src/domain/connection/connection.rules.ts index e18d7f7..684899d 100644 --- a/apps/web/src/domain/connection/connection.rules.ts +++ b/apps/web/src/domain/connection/connection.rules.ts @@ -4,3 +4,26 @@ import type { IntroRequest } from "./intro-request.entity"; export function mayRevealContact(request: IntroRequest): boolean { return request.status === "accepted"; } + +/** Limits on introduction requests, per requester and event. */ +export const INTRO_LIMITS = { + perHour: 20, + pendingOutgoing: 25, +} as const; + +/** + * Whether a new request from `requesterId` may be created, given the requests + * already existing between the two people (either direction). A decline is + * final for the event, and a pending request the other way should be + * answered rather than duplicated. + */ +export function introRequestRefusal(requesterId: string, existing: IntroRequest[]): string | null { + for (const intro of existing) { + const mine = intro.requesterId === requesterId; + if (intro.status === "accepted") return "You are already connected"; + if (mine && intro.status === "pending") return "You already asked to meet them"; + if (mine && intro.status === "declined") return "They declined an earlier request, so you cannot ask again at this event"; + if (!mine && intro.status === "pending") return "They already asked to meet you. Accept their request on your Connections page"; + } + return null; +} diff --git a/apps/web/src/domain/connection/location.rules.ts b/apps/web/src/domain/connection/location.rules.ts new file mode 100644 index 0000000..9fdd534 --- /dev/null +++ b/apps/web/src/domain/connection/location.rules.ts @@ -0,0 +1,33 @@ +/** + * "Where are you?" between two accepted connections. Asking is a request, + * not tracking: the other person decides whether to reply with a venue zone. + * The reply is visible only to the person who asked. + */ +export const LOCATION_RULES = { + /** An unanswered request stops showing after this long. */ + requestExpiresMinutes: 30, + /** A shared zone is shown to the asker for this long. */ + replyVisibleMinutes: 15, + /** Requests one person may send per event and hour. */ + perHour: 10, +} as const; + +export type LocationRequestStatus = "pending" | "answered" | "declined" | "expired"; + +export interface LocationRequestView { + id: string; + introId: string; + direction: "incoming" | "outgoing"; + status: LocationRequestStatus; + /** Only for the asker, while the reply is fresh. */ + zone: string | null; + createdAt: Date; + zoneExpiresAt: Date | null; +} + +/** The state a request is in now: pending requests and shared zones expire. */ +export function currentStatus(request: { status: "pending" | "answered" | "declined"; expiresAt: Date; zoneExpiresAt: Date | null }, now = Date.now()): LocationRequestStatus { + if (request.status === "pending") return request.expiresAt.getTime() > now ? "pending" : "expired"; + if (request.status === "answered") return request.zoneExpiresAt && request.zoneExpiresAt.getTime() > now ? "answered" : "expired"; + return "declined"; +} diff --git a/apps/web/src/domain/matching/explanation-key.ts b/apps/web/src/domain/matching/explanation-key.ts new file mode 100644 index 0000000..00076f7 --- /dev/null +++ b/apps/web/src/domain/matching/explanation-key.ts @@ -0,0 +1,37 @@ +import { createHash } from "node:crypto"; +import type { ParticipantProfile } from "../participant/participant.entity"; +import { SCORING_CONFIG } from "./scoring-config"; + +/** Reasons are reused for a week at most, and earlier when anything in the key changes. */ +export const EXPLANATION_TTL_SECONDS = 7 * 24 * 60 * 60; + +/** + * Identifies one directional explanation: why `candidate` suits `viewer`, at + * these profile revisions, from this model and prompt, under this scoring + * configuration, for this context ("profile" or a normalized search query). + */ +export function explanationKey(input: { + eventId: string; + viewer: ParticipantProfile; + candidate: ParticipantProfile; + model: string; + promptVersion: string; + context: string; +}): string { + return createHash("sha256").update(JSON.stringify([ + input.eventId, + input.viewer.attendeeId, input.viewer.profileRevision ?? null, + input.candidate.attendeeId, input.candidate.profileRevision ?? null, + input.model, input.promptVersion, SCORING_CONFIG.version, input.context, + ])).digest("hex"); +} + +/** Identifies one exact shortlist: the same pairs, in any order, give the same key. */ +export function shortlistKey(pairKeys: string[]): string { + return createHash("sha256").update(`order:${[...pairKeys].sort().join(",")}`).digest("hex"); +} + +/** The context for search explanations: the normalized query. */ +export function queryContext(query: string): string { + return `query:${query.trim().toLowerCase().replace(/\s+/g, " ")}`; +} diff --git a/apps/web/src/domain/matching/pipeline.ts b/apps/web/src/domain/matching/pipeline.ts index 0a4351a..8a2c764 100644 --- a/apps/web/src/domain/matching/pipeline.ts +++ b/apps/web/src/domain/matching/pipeline.ts @@ -1,13 +1,20 @@ import type { EmbeddingProvider } from "../../application/ports/embedding-provider"; +import { welcomedIntents } from "../participant/intent"; import type { CandidateCache } from "../../application/ports/candidate-cache"; +import type { ExplanationCache } from "../../application/ports/explanation-cache"; +import { queryContext } from "./explanation-key"; +import { computeOnce } from "../../application/shared-computation"; +import { createHash } from "node:crypto"; import type { LlmProvider } from "../../application/ports/llm-provider"; import type { MatchRepository } from "../../application/ports/match-repository"; import type { ParticipantRepository } from "../../application/ports/participant-repository"; -import { profileText } from "../participant/participant.entity"; +import { profileText, type ParticipantProfile } from "../participant/participant.entity"; +import { NO_FILTERS, queryRelevance, queryTerms, searchScore, type SearchFilters } from "./search-query"; import { ComplementarityScorer } from "./scorers/complementarity.scorer"; import { DomainScorer } from "./scorers/domain.scorer"; import { IntentScorer } from "./scorers/intent.scorer"; -import { JaccardScorer } from "./scorers/jaccard.scorer"; +import { InterestCoverageScorer } from "./scorers/jaccard.scorer"; +import { SCORING_CONFIG } from "./scoring-config"; import { SemanticScorer } from "./scorers/semantic.scorer"; import { SeniorityScorer } from "./scorers/seniority.scorer"; import { IntentFilterStage } from "./stages/intent-filter.stage"; @@ -22,27 +29,47 @@ export interface MatchingInput { participantId: string; mode: "profile" | "search"; query?: string; + filters?: SearchFilters; limit?: number; + /** Profile runs from a background job: save only if the profile is still at this revision. */ + expectedRevision?: number; } /** * Bump when scorers, weights, or stage behavior change, so persisted matches * from an older computation can be recognised and recomputed. */ -export const MATCHING_PIPELINE_VERSION = "v1"; +export const MATCHING_PIPELINE_VERSION = `v2.${SCORING_CONFIG.version}`; -/** Same retrieve/filter/score/top-K/rerank pipeline for both entry points. */ -export class MatchingPipeline { - private readonly retrieve: RetrieveStage; - private readonly filter = new IntentFilterStage(); - private readonly score = new ScoreStage([ +/** Total time a search may spend on model calls; rerank is skipped if too little is left. */ +export const SEARCH_BUDGET_MS = 6_000; +const QUERY_EMBED_BUDGET_MS = 2_500; + +/** The profile changed while matching ran; the newer job will publish instead. */ +export class StaleRevisionError extends Error { + constructor() { + super("Profile changed during matching; the newer job will publish results"); + this.name = "StaleRevisionError"; + } +} + +/** The scorers the pipeline combines, weighted by SCORING_CONFIG. */ +export function defaultScorers() { + return [ new SemanticScorer(), new IntentScorer(), new DomainScorer(), - new JaccardScorer(), + new InterestCoverageScorer(), new ComplementarityScorer(), new SeniorityScorer(), - ]); + ]; +} + +/** Same retrieve/filter/score/top-K/rerank pipeline for both entry points. */ +export class MatchingPipeline { + private readonly retrieve: RetrieveStage; + private readonly filter = new IntentFilterStage(); + private readonly score = new ScoreStage(defaultScorers()); private readonly topK = new TopKStage(); private readonly rerank: LlmRerankStage; private readonly persist: PersistStage; @@ -52,10 +79,11 @@ export class MatchingPipeline { matches: MatchRepository, private readonly embeddings: EmbeddingProvider, llm: LlmProvider | null, - cache: CandidateCache + private readonly cache: CandidateCache, + explanations: ExplanationCache | null = null ) { this.retrieve = new RetrieveStage(participants, cache); - this.rerank = new LlmRerankStage(llm); + this.rerank = new LlmRerankStage(llm, explanations); this.persist = new PersistStage(matches); } @@ -65,20 +93,22 @@ export class MatchingPipeline { // Hidden viewers may still browse; only a saved profile is required. if (!storedProfile?.profileComplete) throw new Error("Complete your event profile before continuing"); - const embeddingText = input.mode === "search" ? input.query!.trim() : profileText(storedProfile); - const embedding = input.mode === "profile" && storedProfile.embedding - ? storedProfile.embedding - : await this.embeddings.embed(embeddingText, input.mode === "search" ? "query" : "profile"); - if (input.mode === "profile" && embedding) { - await this.participants.setEmbedding(input.eventId, input.participantId, embedding); + if (input.mode === "search") return this.search(input, storedProfile); + + // A vector from another model is a different space: embed again with the current one. + const current = storedProfile.embeddingModel === this.embeddings.id ? storedProfile.embedding ?? null : null; + const embedding = current ?? await this.embeddings.embed(profileText(storedProfile), "profile"); + if (embedding && !current) { + await this.participants.setEmbedding(input.eventId, input.participantId, embedding, this.embeddings.id); } - const viewer = { ...storedProfile, embedding: embedding ?? storedProfile.embedding }; + const viewer = { ...storedProfile, embedding, embeddingModel: embedding ? this.embeddings.id : null }; const retrieved = await this.retrieve.execute({ eventId: input.eventId, viewerId: input.participantId, embedding, - query: input.mode === "search" ? input.query : undefined, + embeddingSpace: this.embeddings.id, + welcomedIntents: welcomedIntents(viewer.intent), limit: 50, }); const compatible = this.filter.execute(viewer, retrieved); @@ -86,9 +116,57 @@ export class MatchingPipeline { const top = this.topK.execute(scored, input.limit ?? 12); const { candidates: ranked, rankedBy } = await this.rerank.execute(viewer, top); - if (input.mode === "profile") { - await this.persist.execute(input.eventId, input.participantId, ranked, `${MATCHING_PIPELINE_VERSION}+${rankedBy}`); - } + const saved = await this.persist.execute(input.eventId, input.participantId, ranked, `${MATCHING_PIPELINE_VERSION}+${rankedBy}`, input.expectedRevision); + if (saved === false) throw new StaleRevisionError(); + return ranked; + } + + /** + * Search: the query embedding (or its words, without embeddings) retrieves + * and measures relevance; the searcher's own profile measures compatibility. + * Both feed the score, so the same query ranks differently per person. + */ + /** Query embeddings are shared across replicas and searches for a day. */ + private embedQuery(query: string, timeoutMs: number): Promise { + const normalized = query.trim().toLowerCase().replace(/\s+/g, " "); + const key = createHash("sha256").update(JSON.stringify(["query-embedding", this.embeddings.id, normalized])).digest("hex"); + return computeOnce({ + cache: this.cache, + key, + read: () => this.cache.getVector(key), + write: (vector) => vector ? this.cache.setVector(key, vector, 24 * 60 * 60) : Promise.resolve(), + compute: () => this.embeddings.embed(normalized, "query", { timeoutMs }), + shouldStore: (vector) => vector !== null, + }); + } + + private async search(input: MatchingInput, viewer: ParticipantProfile) { + const query = input.query!.trim(); + const deadline = Date.now() + SEARCH_BUDGET_MS; + const terms = queryTerms(query); + const queryEmbedding = await this.embedQuery(query, Math.min(QUERY_EMBED_BUDGET_MS, deadline - Date.now())); + const retrieved = await this.retrieve.execute({ + eventId: input.eventId, + viewerId: input.participantId, + embedding: queryEmbedding, + query, + terms, + filters: input.filters ?? NO_FILTERS, + embeddingSpace: this.embeddings.id, + welcomedIntents: welcomedIntents(viewer.intent), + limit: 50, + }); + const compatible = this.filter.execute(viewer, retrieved); + const scored = this.score.execute(viewer, compatible).map((candidate) => { + const relevance = queryRelevance(terms, queryEmbedding, candidate.participant, this.embeddings.id); + return { + ...candidate, + score: searchScore(relevance, candidate.score), + scoreBreakdown: { ...candidate.scoreBreakdown, query: relevance, profile: candidate.score / 100 }, + }; + }).sort((a, b) => b.score - a.score); + const top = this.topK.execute(scored, input.limit ?? 12); + const { candidates: ranked } = await this.rerank.execute(viewer, top, queryContext(query), { timeoutMs: deadline - Date.now() }); return ranked; } } diff --git a/apps/web/src/domain/matching/scorers/complementarity.scorer.ts b/apps/web/src/domain/matching/scorers/complementarity.scorer.ts index 229606b..0cf04cc 100644 --- a/apps/web/src/domain/matching/scorers/complementarity.scorer.ts +++ b/apps/web/src/domain/matching/scorers/complementarity.scorer.ts @@ -1,10 +1,11 @@ import { areIntentsMutuallyCompatible } from "../../participant/intent"; +import { SCORING_CONFIG } from "../scoring-config"; import type { ParticipantProfile } from "../../participant/participant.entity"; import type { Scorer } from "./scorer"; export class ComplementarityScorer implements Scorer { readonly name = "complementarity"; - readonly weight = 0.05; + readonly weight = SCORING_CONFIG.weights.complementarity; score(viewer: ParticipantProfile, candidate: ParticipantProfile): number { if (areIntentsMutuallyCompatible(viewer.intent, candidate.intent)) return 1; return viewer.domain && candidate.domain && viewer.domain !== candidate.domain ? 0.7 : 0.45; diff --git a/apps/web/src/domain/matching/scorers/domain.scorer.ts b/apps/web/src/domain/matching/scorers/domain.scorer.ts index 4ae9113..6492874 100644 --- a/apps/web/src/domain/matching/scorers/domain.scorer.ts +++ b/apps/web/src/domain/matching/scorers/domain.scorer.ts @@ -1,9 +1,10 @@ +import { SCORING_CONFIG } from "../scoring-config"; import type { ParticipantProfile } from "../../participant/participant.entity"; import type { Scorer } from "./scorer"; export class DomainScorer implements Scorer { readonly name = "domain"; - readonly weight = 0.15; + readonly weight = SCORING_CONFIG.weights.domain; score(viewer: ParticipantProfile, candidate: ParticipantProfile): number { const a = viewer.domain?.trim().toLowerCase(); const b = candidate.domain?.trim().toLowerCase(); diff --git a/apps/web/src/domain/matching/scorers/intent.scorer.ts b/apps/web/src/domain/matching/scorers/intent.scorer.ts index b6fcdbf..60c8105 100644 --- a/apps/web/src/domain/matching/scorers/intent.scorer.ts +++ b/apps/web/src/domain/matching/scorers/intent.scorer.ts @@ -1,10 +1,11 @@ import { isIntentCompatible } from "../../participant/intent"; +import { SCORING_CONFIG } from "../scoring-config"; import type { ParticipantProfile } from "../../participant/participant.entity"; import type { Scorer } from "./scorer"; export class IntentScorer implements Scorer { readonly name = "intent"; - readonly weight = 0.25; + readonly weight = SCORING_CONFIG.weights.intent; score(viewer: ParticipantProfile, candidate: ParticipantProfile): number { return isIntentCompatible(viewer.intent, candidate.intent) ? 1 : 0; } diff --git a/apps/web/src/domain/matching/scorers/jaccard.scorer.ts b/apps/web/src/domain/matching/scorers/jaccard.scorer.ts index b690d0b..1970bda 100644 --- a/apps/web/src/domain/matching/scorers/jaccard.scorer.ts +++ b/apps/web/src/domain/matching/scorers/jaccard.scorer.ts @@ -1,14 +1,20 @@ import type { ParticipantProfile } from "../../participant/participant.entity"; +import { SCORING_CONFIG } from "../scoring-config"; import { tokenize, type Scorer } from "./scorer"; -export class JaccardScorer implements Scorer { +/** + * Directional interest fit: how much of what the viewer cares about (their + * interests and domain) the candidate shares. Unlike a symmetric overlap, a + * narrow profile can fit a broad one well while the reverse fits poorly, + * which is what lets score(A→B) and score(B→A) differ. + */ +export class InterestCoverageScorer implements Scorer { readonly name = "interests"; - readonly weight = 0.2; + readonly weight = SCORING_CONFIG.weights.interests; score(viewer: ParticipantProfile, candidate: ParticipantProfile): number { - const left = new Set([...viewer.interests, viewer.domain ?? ""].flatMap((v) => [...tokenize(v)])); - const right = new Set([...candidate.interests, candidate.domain ?? ""].flatMap((v) => [...tokenize(v)])); - if (!left.size || !right.size) return 0.4; - const intersection = [...left].filter((word) => right.has(word)).length; - return intersection / new Set([...left, ...right]).size; + const wanted = new Set([...viewer.interests, viewer.domain ?? ""].flatMap((v) => [...tokenize(v)])); + const offered = new Set([...candidate.interests, candidate.domain ?? ""].flatMap((v) => [...tokenize(v)])); + if (!wanted.size || !offered.size) return 0.4; + return [...wanted].filter((word) => offered.has(word)).length / wanted.size; } } diff --git a/apps/web/src/domain/matching/scorers/semantic.scorer.ts b/apps/web/src/domain/matching/scorers/semantic.scorer.ts index 7f092b3..3b9dd49 100644 --- a/apps/web/src/domain/matching/scorers/semantic.scorer.ts +++ b/apps/web/src/domain/matching/scorers/semantic.scorer.ts @@ -1,12 +1,15 @@ +import { SCORING_CONFIG } from "../scoring-config"; import type { ParticipantProfile } from "../../participant/participant.entity"; import { cosineSimilarity, tokenize, type Scorer } from "./scorer"; export class SemanticScorer implements Scorer { readonly name = "semantic"; - readonly weight = 0.3; + readonly weight = SCORING_CONFIG.weights.semantic; score(viewer: ParticipantProfile, candidate: ParticipantProfile): number { - const vectorScore = cosineSimilarity(viewer.embedding, candidate.embedding); + // Vectors from different models are not comparable; use the text fallback instead. + const sameSpace = viewer.embeddingModel === undefined || candidate.embeddingModel === undefined || viewer.embeddingModel === candidate.embeddingModel; + const vectorScore = sameSpace ? cosineSimilarity(viewer.embedding, candidate.embedding) : 0; if (vectorScore > 0) return vectorScore; const left = tokenize([viewer.aboutMe, viewer.selfDescription, viewer.mostImpressiveBuild].join(" ")); const right = tokenize([candidate.aboutMe, candidate.selfDescription, candidate.mostImpressiveBuild].join(" ")); diff --git a/apps/web/src/domain/matching/scorers/seniority.scorer.ts b/apps/web/src/domain/matching/scorers/seniority.scorer.ts index 55f6a15..8b3750a 100644 --- a/apps/web/src/domain/matching/scorers/seniority.scorer.ts +++ b/apps/web/src/domain/matching/scorers/seniority.scorer.ts @@ -1,9 +1,10 @@ +import { SCORING_CONFIG } from "../scoring-config"; import type { ParticipantProfile } from "../../participant/participant.entity"; import type { Scorer } from "./scorer"; export class SeniorityScorer implements Scorer { readonly name = "seniority"; - readonly weight = 0.05; + readonly weight = SCORING_CONFIG.weights.seniority; score(viewer: ParticipantProfile, candidate: ParticipantProfile): number { if (viewer.yearsOfExperience == null || candidate.yearsOfExperience == null) return 0.5; return Math.max(0.25, 1 - Math.abs(viewer.yearsOfExperience - candidate.yearsOfExperience) / 20); diff --git a/apps/web/src/domain/matching/scoring-config.ts b/apps/web/src/domain/matching/scoring-config.ts new file mode 100644 index 0000000..02ea30a --- /dev/null +++ b/apps/web/src/domain/matching/scoring-config.ts @@ -0,0 +1,33 @@ +/** + * The scoring configuration: scorer weights and the mutual-interest rule. + * Bump `version` whenever a weight, scorer or the threshold changes; it is + * part of the persisted computation version, so matches computed under an + * older configuration can be found and recomputed. + */ +export const SCORING_CONFIG = { + version: "s2", + weights: { + semantic: 0.3, + intent: 0.25, + interests: 0.2, + domain: 0.15, + complementarity: 0.05, + seniority: 0.05, + }, + /** + * Mutual interest means each person is a strong fit for the other: both + * directional scores, score(A→B) and score(B→A), reach this value (0–1). + * Calibrated on the examples in tests/unit/mutual-interest.test.ts. With + * these weights, compatible goals plus a shared domain already reach about + * 0.55, so 0.65 requires real interest overlap: a pair sharing domain and + * interests scores about 0.75 both ways, a one-way fit about 0.75 and 0.63, + * and compatible goals alone about 0.48. + */ + mutualInterestThreshold: 0.65, +} as const; + +export type ScorerName = keyof typeof SCORING_CONFIG.weights; + +export function isMutualInterest(forward: number, reverse: number, threshold: number = SCORING_CONFIG.mutualInterestThreshold): boolean { + return forward >= threshold && reverse >= threshold; +} diff --git a/apps/web/src/domain/matching/search-query.ts b/apps/web/src/domain/matching/search-query.ts new file mode 100644 index 0000000..216c7ac --- /dev/null +++ b/apps/web/src/domain/matching/search-query.ts @@ -0,0 +1,80 @@ +import { Intent } from "../participant/intent"; +import { profileText, type ParticipantProfile } from "../participant/participant.entity"; +import { ValidationError } from "../shared/validation"; +import { cosineSimilarity } from "./scorers/scorer"; + +/** + * What a search means. The query decides who is relevant; the searcher's own + * profile decides who is compatible. The two are scored separately and then + * combined, so the same query ranks differently for different people. + */ +export interface SearchFilters { + intent: Intent | null; + /** One of the domains present among the event's participants. */ + domain: string | null; +} + +export const NO_FILTERS: SearchFilters = { intent: null, domain: null }; + +/** Share of the final search score that comes from the query; the rest is personal compatibility. */ +export const QUERY_WEIGHT = 0.6; + +const MAX_TERMS = 12; +const STOPWORDS = new Set([ + "a", "an", "and", "are", "as", "at", "be", "but", "by", "for", "from", "has", "have", "i", "in", "into", "is", "it", + "looking", "me", "my", "of", "on", "or", "people", "person", "someone", "somebody", "the", "their", "they", "to", + "want", "who", "with", "working", "works", "someone's", "interested", "like", "find", "need", "about", +]); + +/** Meaningful lowercase words from a query, used for lexical retrieval and relevance. */ +export function queryTerms(query: string): string[] { + const words = query.toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? []; + return [...new Set(words.filter((word) => word.length > 1 && !STOPWORDS.has(word)))].slice(0, MAX_TERMS); +} + +/** Validates filters against what the event offers; unknown values are rejected, not ignored. */ +export function normalizeFilters( + raw: { intent?: string | null; domain?: string | null }, + allowed: { intents: string[]; domains: string[] } +): SearchFilters { + const intent = raw.intent?.trim() || null; + if (intent && (!allowed.intents.includes(intent) || !Object.values(Intent).includes(intent as Intent))) { + throw new ValidationError("intent", "Choose an intent offered by this event"); + } + const wanted = raw.domain?.trim().toLowerCase() || null; + const domain = wanted ? allowed.domains.find((value) => value.toLowerCase() === wanted) ?? null : null; + if (wanted && !domain) throw new ValidationError("domain", "Choose a domain from the list"); + return { intent: intent as Intent | null, domain }; +} + +export function matchesFilters(profile: ParticipantProfile, filters: SearchFilters): boolean { + if (filters.intent && profile.intent !== filters.intent) return false; + if (filters.domain && profile.domain?.toLowerCase() !== filters.domain.toLowerCase()) return false; + return true; +} + +/** Fraction of query terms that start a word of the candidate's profile (0–1). */ +export function lexicalRelevance(terms: string[], profile: ParticipantProfile): number { + if (!terms.length) return 0; + const words = profileText(profile).toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? []; + const found = terms.filter((term) => words.some((word) => word.startsWith(term))); + return found.length / terms.length; +} + +/** How well a candidate answers the query: semantic when both vectors exist, otherwise lexical. */ +export function queryRelevance(terms: string[], queryEmbedding: number[] | null, profile: ParticipantProfile, queryModel?: string): number { + const comparable = queryModel === undefined || profile.embeddingModel === undefined || profile.embeddingModel === queryModel; + const semantic = comparable ? cosineSimilarity(queryEmbedding, profile.embedding) : 0; + return semantic > 0 ? semantic : lexicalRelevance(terms, profile); +} + +/** Final search score (0–100) from query relevance (0–1) and personal compatibility (0–100). */ +export function searchScore(relevance: number, personal: number): number { + return Math.round(QUERY_WEIGHT * relevance * 100 + (1 - QUERY_WEIGHT) * personal); +} + +/** Postgres to_tsquery text matching any term as a prefix; terms are already letters and digits only. */ +export function lexicalTsQuery(terms: string[]): string | null { + const safe = terms.map((term) => term.replace(/[^\p{L}\p{N}]/gu, "")).filter(Boolean); + return safe.length ? safe.map((term) => `${term}:*`).join(" | ") : null; +} diff --git a/apps/web/src/domain/matching/stages/llm-rerank.stage.ts b/apps/web/src/domain/matching/stages/llm-rerank.stage.ts index 6002c0a..6827558 100644 --- a/apps/web/src/domain/matching/stages/llm-rerank.stage.ts +++ b/apps/web/src/domain/matching/stages/llm-rerank.stage.ts @@ -1,6 +1,8 @@ import type { LlmProvider } from "../../../application/ports/llm-provider"; import type { ParticipantProfile } from "../../participant/participant.entity"; import type { MatchCandidate } from "../match-strategy"; +import type { ExplanationCache } from "../../../application/ports/explanation-cache"; +import { EXPLANATION_TTL_SECONDS, explanationKey, shortlistKey } from "../explanation-key"; export interface RerankResult { candidates: MatchCandidate[]; @@ -8,18 +10,83 @@ export interface RerankResult { rankedBy: string; } +/** + * Orders the shortlist with the model and explains each match. Explanations + * are cached per viewer -> candidate pair: when every pair already has one, + * the model is not called, and its earlier order for this exact shortlist is + * reused (the scored order stands if there is none). A pair's cached reason + * never decides an order. + */ +/** Below this much remaining time a rerank call is not worth starting. */ +const MIN_RERANK_MS = 750; + export class LlmRerankStage { - constructor(private readonly provider: LlmProvider | null) {} + constructor( + private readonly provider: LlmProvider | null, + private readonly cache: ExplanationCache | null = null + ) {} - async execute(viewer: ParticipantProfile, candidates: MatchCandidate[]): Promise { + async execute(viewer: ParticipantProfile, candidates: MatchCandidate[], context = "profile", options: { timeoutMs?: number } = {}): Promise { const scored = { candidates: candidates.map(addFallbackReason), rankedBy: "score" }; if (!this.provider || candidates.length === 0) return scored; + const provider = this.provider; + const keyFor = (candidate: MatchCandidate) => explanationKey({ + eventId: viewer.eventId, viewer, candidate: candidate.participant, + model: provider.id, promptVersion: provider.promptVersion, context, + }); + const keys = candidates.map(keyFor); + const cached = await this.readCache(keys); + const withCached = candidates.map((candidate, index) => ({ ...candidate, reason: cached.get(keys[index]) ?? candidate.reason })); + if (cached.size === candidates.length) return this.withStoredOrder(keys, withCached, provider.id); + // Too little of the request's budget left for a model call: keep the scored order. + if (options.timeoutMs !== undefined && options.timeoutMs < MIN_RERANK_MS) { + return { candidates: withCached.map(addFallbackReason), rankedBy: "score" }; + } + try { - const reranked = await this.provider.rerank(viewer, candidates); - return { candidates: reranked.map(addFallbackReason), rankedBy: `llm:${this.provider.id}` }; + const reranked = await provider.rerank(viewer, candidates, { timeoutMs: options.timeoutMs }); + await this.writeCache(viewer, reranked.filter((candidate) => candidate.reason).map((candidate) => ({ + key: keyFor(candidate), candidateId: candidate.participant.attendeeId, reason: candidate.reason!, + })), { key: shortlistKey(keys), candidateIds: reranked.map((candidate) => candidate.participant.attendeeId) }); + return { candidates: reranked.map(addFallbackReason), rankedBy: `llm:${provider.id}` }; } catch (error) { console.warn("[matching] LLM rerank unavailable; using scored order", error); - return scored; + return { candidates: withCached.map(addFallbackReason), rankedBy: "score" }; + } + } + + private async withStoredOrder(keys: string[], candidates: MatchCandidate[], providerId: string): Promise { + let order: string[] | null = null; + try { + order = await this.cache!.getOrder(shortlistKey(keys)); + } catch (error) { + console.warn("[matching] explanation cache unavailable", error); + } + const position = new Map((order ?? []).map((id, index) => [id, index])); + if (!order || !candidates.every((candidate) => position.has(candidate.participant.attendeeId))) { + return { candidates, rankedBy: "score" }; + } + const sorted = [...candidates].sort((a, b) => position.get(a.participant.attendeeId)! - position.get(b.participant.attendeeId)!); + return { candidates: sorted, rankedBy: `llm:${providerId}` }; + } + + private async readCache(keys: string[]): Promise> { + if (!this.cache) return new Map(); + try { + return await this.cache.getMany(keys); + } catch (error) { + console.warn("[matching] explanation cache unavailable", error); + return new Map(); + } + } + + private async writeCache(viewer: ParticipantProfile, entries: Array<{ key: string; candidateId: string; reason: string }>, order: { key: string; candidateIds: string[] }) { + if (!this.cache || !entries.length) return; + try { + await this.cache.setMany(entries.map((entry) => ({ ...entry, eventId: viewer.eventId, viewerId: viewer.attendeeId })), EXPLANATION_TTL_SECONDS); + await this.cache.setOrder({ ...order, eventId: viewer.eventId, viewerId: viewer.attendeeId }, EXPLANATION_TTL_SECONDS); + } catch (error) { + console.warn("[matching] could not cache explanations", error); } } } @@ -27,7 +94,9 @@ export class LlmRerankStage { function addFallbackReason(candidate: MatchCandidate): MatchCandidate { if (candidate.reason) return candidate; const person = candidate.participant; - const sharedIntent = candidate.mutualInterest ? "Your networking goals align" : "Their background adds a complementary perspective"; + const sharedIntent = candidate.mutualInterest + ? "You look like a strong fit for each other" + : "Their background adds a complementary perspective"; const detail = person.domain ? ` in ${person.domain}` : ""; return { ...candidate, reason: `${sharedIntent}${detail}.` }; } diff --git a/apps/web/src/domain/matching/stages/persist.stage.ts b/apps/web/src/domain/matching/stages/persist.stage.ts index 1c1c07e..60f94e5 100644 --- a/apps/web/src/domain/matching/stages/persist.stage.ts +++ b/apps/web/src/domain/matching/stages/persist.stage.ts @@ -3,7 +3,7 @@ import type { MatchCandidate } from "../match-strategy"; export class PersistStage { constructor(private readonly matches: MatchRepository) {} - execute(eventId: string, participantId: string, candidates: MatchCandidate[], computationVersion: string): Promise { - return this.matches.save(eventId, participantId, candidates, computationVersion); + execute(eventId: string, participantId: string, candidates: MatchCandidate[], computationVersion: string, expectedRevision?: number): Promise { + return this.matches.save(eventId, participantId, candidates, computationVersion, expectedRevision); } } diff --git a/apps/web/src/domain/matching/stages/retrieve.stage.ts b/apps/web/src/domain/matching/stages/retrieve.stage.ts index 9bc9b66..72d913a 100644 --- a/apps/web/src/domain/matching/stages/retrieve.stage.ts +++ b/apps/web/src/domain/matching/stages/retrieve.stage.ts @@ -3,6 +3,11 @@ import type { ParticipantRepository } from "../../../application/ports/participa import type { MatchRepository } from "../../../application/ports/match-repository"; import type { CandidateCache } from "../../../application/ports/candidate-cache"; import type { ParticipantProfile } from "../../participant/participant.entity"; +import { matchesFilters, NO_FILTERS, type SearchFilters } from "../search-query"; +import { computeOnce } from "../../../application/shared-computation"; + +/** Bump when retrieval semantics change, so cached candidate lists are not reused. */ +const RETRIEVAL_VERSION = "r3"; export class RetrieveStage { constructor( @@ -15,35 +20,58 @@ export class RetrieveStage { viewerId: string; embedding: number[] | null; query?: string; + terms?: string[]; + filters?: SearchFilters; + /** Embedding provider/model; vectors from different spaces must not share entries. */ + embeddingSpace?: string; + /** + * The viewer's welcomed intents, applied before the limit: filtering + * afterwards left people with no matches when their nearest neighbours + * all had intents they do not welcome. + */ + welcomedIntents?: string[] | null; limit?: number; }): Promise { const limit = input.limit ?? 50; - const normalizedQuery = input.query?.trim().toLowerCase(); + const filters = input.filters ?? NO_FILTERS; + const normalizedQuery = input.query?.trim().toLowerCase().replace(/\s+/g, " "); if (normalizedQuery) { - const key = createHash("sha256") - .update(`${input.eventId}:${normalizedQuery}`) - .digest("hex"); - const cachedIds = await this.cache.get(key); - if (cachedIds) { - const cached = await this.participants.findCandidatesByIds(input.eventId, cachedIds); - const byId = new Map(cached.map((profile) => [profile.attendeeId, profile])); - return cachedIds.map((id) => byId.get(id)).filter((row): row is ParticipantProfile => row !== undefined && row.attendeeId !== input.viewerId); - } - - const candidates = await this.participants.listCandidates({ - eventId: input.eventId, - excludeAttendeeId: null, - embedding: input.embedding, - limit, + // Shared across viewers for the same corpus, query, filters and + // retrieval kind; the ranking that follows is still personal. + const corpus = await this.participants.corpusRevision(input.eventId); + const key = createHash("sha256").update(JSON.stringify([ + RETRIEVAL_VERSION, input.eventId, corpus, normalizedQuery, filters.intent, filters.domain?.toLowerCase() ?? null, input.welcomedIntents ?? null, + input.embedding ? input.embeddingSpace ?? "semantic" : "lexical", limit, + ])).digest("hex"); + const ids = await computeOnce({ + cache: this.cache, + key, + read: () => this.cache.get(key), + write: (value) => this.cache.set(key, value, 300), + compute: async () => (await this.participants.listCandidates({ + eventId: input.eventId, + excludeAttendeeId: null, + embedding: input.embedding, + embeddingModel: input.embeddingSpace, + terms: input.terms, + filters, + welcomedIntents: input.welcomedIntents, + limit, + })).map((candidate) => candidate.attendeeId), }); - await this.cache.set(key, candidates.map((c) => c.attendeeId), 300); - return candidates.filter((candidate) => candidate.attendeeId !== input.viewerId); + // Re-check eligibility and filters: the ids may come from another request. + const profiles = await this.participants.findCandidatesByIds(input.eventId, ids); + const byId = new Map(profiles.map((profile) => [profile.attendeeId, profile])); + return ids.map((id) => byId.get(id)) + .filter((row): row is ParticipantProfile => row !== undefined && row.attendeeId !== input.viewerId && matchesFilters(row, filters)); } return this.participants.listCandidates({ eventId: input.eventId, excludeAttendeeId: input.viewerId, embedding: input.embedding, + embeddingModel: input.embeddingSpace, + welcomedIntents: input.welcomedIntents, limit, }); } diff --git a/apps/web/src/domain/matching/stages/score.stage.ts b/apps/web/src/domain/matching/stages/score.stage.ts index 3c4abff..29bbc35 100644 --- a/apps/web/src/domain/matching/stages/score.stage.ts +++ b/apps/web/src/domain/matching/stages/score.stage.ts @@ -1,31 +1,36 @@ -import { areIntentsMutuallyCompatible } from "../../participant/intent"; import type { ParticipantProfile } from "../../participant/participant.entity"; import type { MatchCandidate } from "../match-strategy"; +import { isMutualInterest } from "../scoring-config"; import type { Scorer } from "../scorers/scorer"; +/** + * Scores each candidate for the viewer, and the viewer for each candidate. + * The forward score ranks; both directions decide mutual interest. + */ export class ScoreStage { constructor(private readonly scorers: Scorer[]) {} execute(viewer: ParticipantProfile, candidates: ParticipantProfile[]): MatchCandidate[] { return candidates.map((participant) => { - const scoreBreakdown = Object.fromEntries( - this.scorers.map((scorer) => [scorer.name, clamp(scorer.score(viewer, participant))]) - ); - const totalWeight = this.scorers.reduce((sum, scorer) => sum + scorer.weight, 0) || 1; - const score = this.scorers.reduce( - (sum, scorer) => sum + scoreBreakdown[scorer.name] * scorer.weight, - 0 - ) / totalWeight; - + const { total: forward, breakdown } = this.pairScore(viewer, participant); + const { total: reverse } = this.pairScore(participant, viewer); return { participant, - score: Math.round(score * 100), - scoreBreakdown, - mutualInterest: areIntentsMutuallyCompatible(viewer.intent, participant.intent), + score: Math.round(forward * 100), + scoreBreakdown: { ...breakdown, reverse }, + mutualInterest: isMutualInterest(forward, reverse), reason: null, }; }).sort((a, b) => b.score - a.score); } + + /** How well `candidate` fits `viewer`, 0–1, with each scorer's part. */ + pairScore(viewer: ParticipantProfile, candidate: ParticipantProfile): { total: number; breakdown: Record } { + const breakdown = Object.fromEntries(this.scorers.map((scorer) => [scorer.name, clamp(scorer.score(viewer, candidate))])); + const totalWeight = this.scorers.reduce((sum, scorer) => sum + scorer.weight, 0) || 1; + const total = this.scorers.reduce((sum, scorer) => sum + breakdown[scorer.name] * scorer.weight, 0) / totalWeight; + return { total, breakdown }; + } } function clamp(value: number): number { diff --git a/apps/web/src/domain/participant/intent.ts b/apps/web/src/domain/participant/intent.ts index ec07b33..a64f82e 100644 --- a/apps/web/src/domain/participant/intent.ts +++ b/apps/web/src/domain/participant/intent.ts @@ -23,6 +23,15 @@ export const INTENT_COMPATIBILITY: Record = { [Intent.INVESTING]: [Intent.RAISING_FUNDING, Intent.NETWORKING], }; +/** + * The intents a person welcomes, for narrowing retrieval before any top-K + * cut; null when they welcome everyone. + */ +export function welcomedIntents(intent: Intent): Intent[] | null { + const welcomed = INTENT_COMPATIBILITY[intent] ?? []; + return welcomed.length === Object.values(Intent).length ? null : [...welcomed].sort(); +} + export function isIntentCompatible(from: Intent, to: Intent): boolean { return INTENT_COMPATIBILITY[from]?.includes(to) ?? false; } diff --git a/apps/web/src/domain/participant/invitation.ts b/apps/web/src/domain/participant/invitation.ts new file mode 100644 index 0000000..93509cc --- /dev/null +++ b/apps/web/src/domain/participant/invitation.ts @@ -0,0 +1,32 @@ +/** + * Where an invited attendee stands for one event, as the organizer sees it. + * Removed overrides everything; otherwise the furthest step reached wins. + */ +export type InvitationState = "removed" | "complete" | "claimed" | "invited" | "failed" | "not_invited"; + +/** A resend for the same attendee is refused until this much time has passed. */ +export const RESEND_AFTER_MINUTES = 10; + +export interface InvitationFacts { + revokedAt: Date | null; + profileUpdatedAt: Date | null; + claimedAt: Date | null; + invitedAt: Date | null; + inviteError: string | null; +} + +export function invitationState(facts: InvitationFacts): InvitationState { + if (facts.revokedAt) return "removed"; + if (facts.profileUpdatedAt) return "complete"; + if (facts.claimedAt) return "claimed"; + if (facts.inviteError) return "failed"; + if (facts.invitedAt) return "invited"; + return "not_invited"; +} + +/** Whether the organizer can send this attendee an invitation now. */ +export function canInvite(facts: InvitationFacts, now = Date.now()): boolean { + const state = invitationState(facts); + if (state === "removed" || state === "claimed" || state === "complete") return false; + return !facts.invitedAt || now - facts.invitedAt.getTime() >= RESEND_AFTER_MINUTES * 60_000; +} diff --git a/apps/web/src/domain/participant/participant.entity.ts b/apps/web/src/domain/participant/participant.entity.ts index 28a8596..fed1537 100644 --- a/apps/web/src/domain/participant/participant.entity.ts +++ b/apps/web/src/domain/participant/participant.entity.ts @@ -1,5 +1,6 @@ import { Intent } from "./intent"; import { boundedItems, boundedText, LIMITS, ValidationError } from "../shared/validation"; +import { readProfileForm, type ProfileFormConfig } from "./profile-form"; export interface ParticipantProfile { eventId: string; @@ -18,7 +19,15 @@ export interface ParticipantProfile { venueZone: string | null; visible: boolean; profileComplete?: boolean; + /** Goes up on every save; background jobs carry the revision they reflect. */ + revision?: number; + /** When the profile was last saved; part of cached explanation keys. */ + profileRevision?: string | null; + /** Version of the event's profile form this profile was saved under. */ + formVersion?: number; embedding?: number[] | null; + /** The provider/model that produced `embedding`; vectors are only compared within one model. */ + embeddingModel?: string | null; } export type ParticipantProfileInput = Pick< @@ -34,7 +43,8 @@ export type ParticipantProfileInput = Pick< | "intent" | "interests" | "venueZone" ->; + | "formVersion" +> & { visible?: boolean }; export function profileText(profile: ParticipantProfile): string { return [ @@ -51,22 +61,31 @@ export function profileText(profile: ParticipantProfile): string { .join("\n"); } -export function validateProfile(input: ParticipantProfileInput): ParticipantProfileInput { +export function validateProfile(input: ParticipantProfileInput, form: ProfileFormConfig = readProfileForm({})): ParticipantProfileInput { + // Required fields and their labels come from the event's profile form. + const text = (key: "designation" | "domain" | "aboutMe" | "selfDescription" | "mostImpressiveBuild", value: string | null, limit: number) => + boundedText(value, key, form.fields[key].label, limit, { required: form.fields[key].required }); if (!input.eventId || !input.attendeeId) throw new ValidationError(null, "Participant and event are required"); if (!Object.values(Intent).includes(input.intent)) throw new ValidationError("intent", "Choose a valid networking intent"); if (!Array.isArray(input.interests)) throw new ValidationError("interests", "Interests must be a list"); const years = input.yearsOfExperience; + if (years == null && form.fields.yearsOfExperience.required) { + throw new ValidationError("yearsOfExperience", `${form.fields.yearsOfExperience.label} is required`); + } if (years != null && (!Number.isInteger(years) || years < 0 || years > LIMITS.yearsOfExperience)) { throw new ValidationError("yearsOfExperience", `Years of experience must be a whole number from 0 to ${LIMITS.yearsOfExperience}`); } + const interests = boundedItems(input.interests, "interests", "interests", { count: LIMITS.interestCount, length: LIMITS.interestLength }); + if (!interests.length && form.fields.interests.required) throw new ValidationError("interests", `${form.fields.interests.label} is required`); return { ...input, - designation: boundedText(input.designation, "designation", "Role or designation", LIMITS.designation, { required: true }), - domain: boundedText(input.domain, "domain", "Professional domain", LIMITS.domain, { required: true }), - aboutMe: boundedText(input.aboutMe, "aboutMe", "About you", LIMITS.aboutMe, { required: true }), - selfDescription: boundedText(input.selfDescription, "selfDescription", "Current focus", LIMITS.selfDescription), - mostImpressiveBuild: boundedText(input.mostImpressiveBuild, "mostImpressiveBuild", "Most impressive build", LIMITS.mostImpressiveBuild), + designation: text("designation", input.designation, LIMITS.designation), + domain: text("domain", input.domain, LIMITS.domain), + aboutMe: text("aboutMe", input.aboutMe, LIMITS.aboutMe), + selfDescription: text("selfDescription", input.selfDescription, LIMITS.selfDescription), + mostImpressiveBuild: text("mostImpressiveBuild", input.mostImpressiveBuild, LIMITS.mostImpressiveBuild), venueZone: boundedText(input.venueZone, "venueZone", "Venue zone", LIMITS.zoneLength), - interests: boundedItems(input.interests, "interests", "interests", { count: LIMITS.interestCount, length: LIMITS.interestLength }), + interests, + formVersion: form.version, }; } diff --git a/apps/web/src/domain/participant/profile-form.ts b/apps/web/src/domain/participant/profile-form.ts new file mode 100644 index 0000000..edff91a --- /dev/null +++ b/apps/web/src/domain/participant/profile-form.ts @@ -0,0 +1,76 @@ +import { boundedText, LIMITS, ValidationError } from "../shared/validation"; + +/** + * The profile fields an organizer can configure for an event. They are the + * fields matching already reads, so whatever the form shows, every stored + * value keeps feeding matching the same way. Intent and venue zone are + * configured separately (event intents and zones). + */ +export const PROFILE_FIELDS = { + designation: { label: "Role or designation", kind: "text", limit: LIMITS.designation, required: true, descriptive: true }, + domain: { label: "Professional domain", kind: "text", limit: LIMITS.domain, required: true, descriptive: true }, + aboutMe: { label: "About you", kind: "multiline", limit: LIMITS.aboutMe, required: true, descriptive: true }, + selfDescription: { label: "Current focus", kind: "multiline", limit: LIMITS.selfDescription, required: false, descriptive: true }, + mostImpressiveBuild: { label: "Most impressive thing you have built", kind: "multiline", limit: LIMITS.mostImpressiveBuild, required: false, descriptive: true }, + yearsOfExperience: { label: "Years of experience", kind: "number", limit: LIMITS.yearsOfExperience, required: false, descriptive: false }, + interests: { label: "Interests", kind: "list", limit: LIMITS.interestCount, required: false, descriptive: false }, +} as const; + +export type ProfileFieldKey = keyof typeof PROFILE_FIELDS; +export const PROFILE_FIELD_KEYS = Object.keys(PROFILE_FIELDS) as ProfileFieldKey[]; + +export interface ProfileFieldSetting { + shown: boolean; + required: boolean; + label: string; +} + +export interface ProfileFormConfig { + /** Goes up on every change, so profiles saved under an older form can be found. */ + version: number; + fields: Record; +} + +export const LABEL_LIMIT = 60; + +/** The event's stored form_config, with defaults for anything missing or malformed. */ +export function readProfileForm(raw: unknown): ProfileFormConfig { + const stored = (raw && typeof raw === "object" ? raw : {}) as { version?: unknown; fields?: Record> }; + const fields = {} as Record; + for (const key of PROFILE_FIELD_KEYS) { + const field = stored.fields?.[key] ?? {}; + const shown = typeof field.shown === "boolean" ? field.shown : true; + fields[key] = { + shown, + required: shown && (typeof field.required === "boolean" ? field.required : PROFILE_FIELDS[key].required), + label: typeof field.label === "string" && field.label.trim() ? field.label.trim().slice(0, LABEL_LIMIT) : PROFILE_FIELDS[key].label, + }; + } + return { version: Number.isInteger(stored.version) ? stored.version as number : 0, fields }; +} + +/** Validates an organizer's form settings and stamps the next version. */ +export function buildProfileForm(settings: Record, previous: ProfileFormConfig): ProfileFormConfig { + const fields = {} as Record; + for (const key of PROFILE_FIELD_KEYS) { + const setting = settings[key]; + const label = boundedText(setting.label, `${key}.label`, `${PROFILE_FIELDS[key].label} label`, LABEL_LIMIT) ?? PROFILE_FIELDS[key].label; + if (setting.required && !setting.shown) { + throw new ValidationError(`${key}.required`, `"${label}" cannot be required while it is hidden`); + } + fields[key] = { shown: setting.shown, required: setting.required, label }; + } + if (!PROFILE_FIELD_KEYS.some((key) => PROFILE_FIELDS[key].descriptive && fields[key].required)) { + throw new ValidationError(null, "Keep at least one descriptive field (role, domain, about, focus or build) required, so matching has something to read."); + } + return { version: previous.version + 1, fields }; +} + +/** Required fields a saved profile leaves empty under the current form. */ +export function missingRequiredFields(form: ProfileFormConfig, values: Partial>): ProfileFieldKey[] { + return PROFILE_FIELD_KEYS.filter((key) => { + if (!form.fields[key].required) return false; + const value = values[key]; + return value == null || (Array.isArray(value) ? value.length === 0 : String(value).trim() === ""); + }); +} diff --git a/apps/web/src/domain/shared/organizer-permissions.ts b/apps/web/src/domain/shared/organizer-permissions.ts new file mode 100644 index 0000000..b21db4b --- /dev/null +++ b/apps/web/src/domain/shared/organizer-permissions.ts @@ -0,0 +1,45 @@ +/** + * What each organizer role may do with an event. Organization owners and + * admins manage every event in their organization; on a single event, owners + * and admins configure it and control its roster, while an "organizer" runs + * it day to day (view, invite, start matching). + */ +export type OrganizerRole = "owner" | "admin" | "organizer"; + +export type OrganizerAction = "view" | "invite" | "computeMatches" | "configure" | "importRoster" | "manageAccess"; + +export interface OrganizerRoles { + event: OrganizerRole | null; + organization: OrganizerRole | null; +} + +const DAY_TO_DAY: readonly OrganizerAction[] = ["view", "invite", "computeMatches"]; + +const isManager = (role: OrganizerRole | null) => role === "owner" || role === "admin"; + +export function organizerCan(roles: OrganizerRoles, action: OrganizerAction): boolean { + if (isManager(roles.organization)) return true; + if (!roles.event) return false; + return DAY_TO_DAY.includes(action) || isManager(roles.event); +} + +/** Any member of an organization may create an event, and becomes its owner. */ +export function canCreateEvent(organizationRole: OrganizerRole | null): boolean { + return organizationRole !== null; +} + +const LABELS: Record = { + view: "view this event", + invite: "send invitations", + computeMatches: "start matching", + configure: "change this event's settings", + importRoster: "import attendees", + manageAccess: "remove or restore attendees", +}; + +export function assertOrganizerCan(roles: OrganizerRoles, action: OrganizerAction): void { + if (organizerCan(roles, action)) return; + throw new Error(roles.event || roles.organization + ? `Only event owners and admins can ${LABELS[action]}.` + : "You are not an organizer for this event"); +} diff --git a/apps/web/src/infrastructure/cache/postgres-candidate-cache.ts b/apps/web/src/infrastructure/cache/postgres-candidate-cache.ts index 90971b8..3478911 100644 --- a/apps/web/src/infrastructure/cache/postgres-candidate-cache.ts +++ b/apps/web/src/infrastructure/cache/postgres-candidate-cache.ts @@ -1,20 +1,55 @@ +import { randomUUID } from "node:crypto"; import { query } from "@match-maker/db"; import type { CandidateCache } from "../../application/ports/candidate-cache"; export class PostgresCandidateCache implements CandidateCache { async get(key: string): Promise { - const { rows } = await query<{ candidate_ids: string[] }>( + const value = await this.read(key); + return Array.isArray(value) && value.every((id) => typeof id === "string") ? value : null; + } + + async set(key: string, candidateIds: string[], ttlSeconds: number): Promise { + await this.write(key, candidateIds, ttlSeconds); + } + + async getVector(key: string): Promise { + const value = await this.read(key); + return Array.isArray(value) && value.length > 0 && value.every((n) => typeof n === "number") ? value : null; + } + + async setVector(key: string, vector: number[], ttlSeconds: number): Promise { + await this.write(key, vector, ttlSeconds); + } + + async acquire(key: string, ttlMs: number): Promise { + const token = randomUUID(); + const { rows } = await query<{ owner: string }>( + `insert into cache_locks (lock_key, owner, expires_at) values ($1, $2, now() + make_interval(secs => $3)) + on conflict (lock_key) do update set owner = excluded.owner, expires_at = excluded.expires_at + where cache_locks.expires_at < now() + returning owner`, + [key, token, ttlMs / 1000] + ); + return rows[0]?.owner === token ? token : null; + } + + async release(key: string, token: string): Promise { + await query(`delete from cache_locks where lock_key = $1 and owner = $2`, [key, token]); + } + + private async read(key: string): Promise { + const { rows } = await query<{ candidate_ids: unknown }>( `select candidate_ids from retrieval_cache where cache_key = $1 and expires_at > now()`, [key] ); return rows[0]?.candidate_ids ?? null; } - async set(key: string, candidateIds: string[], ttlSeconds: number): Promise { + private async write(key: string, value: unknown, ttlSeconds: number): Promise { await query( `insert into retrieval_cache (cache_key, candidate_ids, expires_at) - values ($1, $2::jsonb, now() + ($3 || ' seconds')::interval) + values ($1, $2::jsonb, now() + make_interval(secs => $3)) on conflict (cache_key) do update set candidate_ids = excluded.candidate_ids, expires_at = excluded.expires_at`, - [key, JSON.stringify(candidateIds), String(ttlSeconds)] + [key, JSON.stringify(value), ttlSeconds] ); } } diff --git a/apps/web/src/infrastructure/cache/postgres-explanation-cache.ts b/apps/web/src/infrastructure/cache/postgres-explanation-cache.ts new file mode 100644 index 0000000..b2c77d5 --- /dev/null +++ b/apps/web/src/infrastructure/cache/postgres-explanation-cache.ts @@ -0,0 +1,41 @@ +import { query } from "@match-maker/db"; +import type { ExplanationCache } from "../../application/ports/explanation-cache"; + +export class PostgresExplanationCache implements ExplanationCache { + async getMany(keys: string[]): Promise> { + if (!keys.length) return new Map(); + const { rows } = await query<{ cache_key: string; reason: string }>( + `select cache_key, reason from pair_explanations where cache_key = any($1::text[]) and expires_at > now()`, + [keys] + ); + return new Map(rows.map((row) => [row.cache_key, row.reason])); + } + + async setMany(entries: Parameters[0], ttlSeconds: number): Promise { + if (!entries.length) return; + await query( + `insert into pair_explanations (cache_key, event_id, viewer_id, candidate_id, reason, expires_at) + select e.key, e.event_id, e.viewer_id, e.candidate_id, e.reason, now() + make_interval(secs => $2) + from jsonb_to_recordset($1::jsonb) as e(key text, event_id uuid, viewer_id uuid, candidate_id uuid, reason text) + on conflict (cache_key) do update set reason = excluded.reason, expires_at = excluded.expires_at`, + [JSON.stringify(entries.map((entry) => ({ + key: entry.key, event_id: entry.eventId, viewer_id: entry.viewerId, candidate_id: entry.candidateId, reason: entry.reason, + }))), ttlSeconds] + ); + } + + async getOrder(key: string): Promise { + const { rows } = await query<{ candidate_ids: string[] }>( + `select candidate_ids from rerank_orders where cache_key = $1 and expires_at > now()`, [key]); + return rows[0]?.candidate_ids ?? null; + } + + async setOrder(entry: Parameters[0], ttlSeconds: number): Promise { + await query( + `insert into rerank_orders (cache_key, event_id, viewer_id, candidate_ids, expires_at) + values ($1, $2, $3, $4::uuid[], now() + make_interval(secs => $5)) + on conflict (cache_key) do update set candidate_ids = excluded.candidate_ids, expires_at = excluded.expires_at`, + [entry.key, entry.eventId, entry.viewerId, entry.candidateIds, ttlSeconds] + ); + } +} diff --git a/apps/web/src/infrastructure/cache/postgres-rate-limiter.ts b/apps/web/src/infrastructure/cache/postgres-rate-limiter.ts index 5e2a591..528265d 100644 --- a/apps/web/src/infrastructure/cache/postgres-rate-limiter.ts +++ b/apps/web/src/infrastructure/cache/postgres-rate-limiter.ts @@ -9,13 +9,13 @@ export class PostgresRateLimiter implements RateLimiter { values ($1, now(), 1) on conflict (bucket_key) do update set window_started_at = case - when rate_limit_buckets.window_started_at <= now() - ($3 || ' seconds')::interval then now() + when rate_limit_buckets.window_started_at <= now() - make_interval(secs => $2) then now() else rate_limit_buckets.window_started_at end, count = case - when rate_limit_buckets.window_started_at <= now() - ($3 || ' seconds')::interval then 1 + when rate_limit_buckets.window_started_at <= now() - make_interval(secs => $2) then 1 else rate_limit_buckets.count + 1 end returning count`, - [key, limit, String(windowSeconds)] + [key, windowSeconds] ); return Number(rows[0]?.count ?? limit + 1) <= limit; } diff --git a/apps/web/src/infrastructure/cache/upstash-candidate-cache.ts b/apps/web/src/infrastructure/cache/upstash-candidate-cache.ts index 3e6ca87..a5ec2ee 100644 --- a/apps/web/src/infrastructure/cache/upstash-candidate-cache.ts +++ b/apps/web/src/infrastructure/cache/upstash-candidate-cache.ts @@ -1,29 +1,71 @@ +import { randomUUID } from "node:crypto"; import type { CandidateCache } from "../../application/ports/candidate-cache"; -/** Redis REST adapter. Falls back to the local PostgreSQL cache when unset. */ +/** Deletes the lock only when it still holds this owner's token. */ +const RELEASE_SCRIPT = `if redis.call("GET", KEYS[1]) == ARGV[1] then return redis.call("DEL", KEYS[1]) else return 0 end`; + +/** Redis REST adapter. Falls back to the local PostgreSQL cache when unset or unreachable. */ export class UpstashCandidateCache implements CandidateCache { constructor(private readonly fallback: CandidateCache) {} async get(key: string): Promise { - if (!this.configured()) return this.fallback.get(key); + const parsed = await this.readJson(key, () => this.fallback.get(key)); + return Array.isArray(parsed) && parsed.every((id) => typeof id === "string") ? parsed : null; + } + + async set(key: string, candidateIds: string[], ttlSeconds: number): Promise { + await this.writeJson(key, candidateIds, ttlSeconds, () => this.fallback.set(key, candidateIds, ttlSeconds)); + } + + async getVector(key: string): Promise { + const parsed = await this.readJson(key, () => this.fallback.getVector(key)); + return Array.isArray(parsed) && parsed.length > 0 && parsed.every((n) => typeof n === "number") ? parsed : null; + } + + async setVector(key: string, vector: number[], ttlSeconds: number): Promise { + await this.writeJson(key, vector, ttlSeconds, () => this.fallback.setVector(key, vector, ttlSeconds)); + } + + async acquire(key: string, ttlMs: number): Promise { + if (!this.configured()) return this.fallback.acquire(key, ttlMs); + try { + const token = randomUUID(); + return await this.command(["SET", `lock:${key}`, token, "NX", "PX", String(ttlMs)]) === "OK" ? token : null; + } catch (error) { + console.warn("[cache] Redis lock failed; using PostgreSQL lock", error); + return this.fallback.acquire(key, ttlMs); + } + } + + async release(key: string, token: string): Promise { + if (!this.configured()) return this.fallback.release(key, token); + try { + await this.command(["EVAL", RELEASE_SCRIPT, "1", `lock:${key}`, token]); + } catch (error) { + // The lock expires on its own; releasing early is only an optimisation. + console.warn("[cache] Redis unlock failed", error); + await this.fallback.release(key, token).catch(() => {}); + } + } + + private async readJson(key: string, fallback: () => Promise): Promise { + if (!this.configured()) return fallback(); try { const result = await this.command(["GET", key]); - if (typeof result !== "string") return null; - const parsed: unknown = JSON.parse(result); - return Array.isArray(parsed) && parsed.every((id) => typeof id === "string") ? parsed : null; + return typeof result === "string" ? JSON.parse(result) : null; } catch (error) { console.warn("[cache] Redis GET failed; using PostgreSQL cache", error); - return this.fallback.get(key); + return fallback(); } } - async set(key: string, candidateIds: string[], ttlSeconds: number): Promise { - if (!this.configured()) return this.fallback.set(key, candidateIds, ttlSeconds); + private async writeJson(key: string, value: unknown, ttlSeconds: number, fallback: () => Promise): Promise { + if (!this.configured()) return fallback(); try { - await this.command(["SET", key, JSON.stringify(candidateIds), "EX", String(ttlSeconds)]); + await this.command(["SET", key, JSON.stringify(value), "EX", String(ttlSeconds)]); } catch (error) { console.warn("[cache] Redis SET failed; using PostgreSQL cache", error); - await this.fallback.set(key, candidateIds, ttlSeconds); + await fallback(); } } diff --git a/apps/web/src/infrastructure/notifications/realtime.notifier.ts b/apps/web/src/infrastructure/notifications/realtime.notifier.ts index ab37a8f..4b61de9 100644 --- a/apps/web/src/infrastructure/notifications/realtime.notifier.ts +++ b/apps/web/src/infrastructure/notifications/realtime.notifier.ts @@ -1,23 +1,35 @@ -import { sendNotificationEmail } from "@match-maker/email"; -import type { NotificationPort } from "../../application/ports/notification"; +import { renderInvitationEmail, sendNotificationEmail } from "@match-maker/email"; +import type { NotificationPort, NotificationTarget } from "../../application/ports/notification"; export class RealtimeNotifier implements NotificationPort { - async introRequested(input: { recipientEmail: string; recipientName: string; requesterName: string; eventName: string; introId: string }): Promise { - const subject = `${input.requesterName} would like to meet at ${input.eventName}`; - const text = `Hi ${input.recipientName},\n\n${input.requesterName} requested an introduction at ${input.eventName}. Sign in to Mr. Bridge to respond.`; - await sendNotificationEmail({ to: input.recipientEmail, subject, text, html: emailHtml(input.recipientName, input.requesterName, input.eventName, "requested an introduction. Sign in to Mr. Bridge to respond.") }); + async invitation(input: { email: string; name: string | null; eventName: string; organizationName: string; eventUrl: string }): Promise { + await sendNotificationEmail({ to: input.email, ...renderInvitationEmail(input) }); } - async introResponded(input: { recipientEmail: string; recipientName: string; requesterName: string; eventName: string; accepted: boolean }): Promise { - const status = input.accepted ? "accepted" : "declined"; - const subject = `Your introduction request was ${status}`; - const text = `Hi ${input.recipientName},\n\n${input.requesterName} ${status} your introduction request for ${input.eventName}.`; - await sendNotificationEmail({ to: input.recipientEmail, subject, text, html: emailHtml(input.recipientName, input.requesterName, input.eventName, `${status} your introduction request.`) }); + introRequested(input: NotificationTarget): Promise { + return send(input, `${input.otherName} would like to meet at ${input.eventName}`, `${input.otherName} requested an introduction. Accept to share contact details.`, "Respond"); + } + + introAccepted(input: NotificationTarget): Promise { + return send(input, `${input.otherName} accepted your introduction`, `${input.otherName} accepted your introduction request. Their contact details are now on your Connections page.`, "See contact"); + } + + locationRequested(input: NotificationTarget): Promise { + return send(input, `${input.otherName} is asking where you are`, `${input.otherName} would like to find you at ${input.eventName}. Sharing your venue zone is up to you.`, "Reply"); + } + + locationAnswered(input: NotificationTarget & { zone: string | null }): Promise { + const text = input.zone ? `${input.otherName} is at ${input.zone}.` : `${input.otherName} would rather not share their location right now.`; + return send(input, input.zone ? `${input.otherName} is at ${input.zone}` : `${input.otherName} replied to your location request`, text, "Open connections"); } } -function emailHtml(name: string, otherPerson: string, event: string, action: string): string { - return `

Mr. Bridge

Hi ${escapeHtml(name)},

${escapeHtml(otherPerson)} ${escapeHtml(action)}

Event: ${escapeHtml(event)}

Open Mr. Bridge to view your connections.

`; +async function send(input: NotificationTarget, subject: string, line: string, action: string): Promise { + const text = `Hi ${input.recipientName},\n\n${line}\n\n${action}: ${input.link}`; + const html = `

Mr. Bridge

` + + `

Hi ${escapeHtml(input.recipientName)},

${escapeHtml(line)}

Event: ${escapeHtml(input.eventName)}

` + + `

${escapeHtml(action)}

`; + await sendNotificationEmail({ to: input.recipientEmail, subject, text, html }); } function escapeHtml(value: string): string { diff --git a/apps/web/src/infrastructure/observability/emf-metrics.ts b/apps/web/src/infrastructure/observability/emf-metrics.ts new file mode 100644 index 0000000..7f5c552 --- /dev/null +++ b/apps/web/src/infrastructure/observability/emf-metrics.ts @@ -0,0 +1,48 @@ +import type { Metrics } from "../../application/ports/metrics"; +import type { ModelUsageLog } from "../../application/ports/model-usage"; + +/** + * Writes metrics as CloudWatch Embedded Metric Format: one JSON log line that + * CloudWatch Logs turns into metrics, so no agent or extra service is needed. + * Without METRICS_NAMESPACE (local runs) nothing is written. + */ +export class EmfMetrics implements Metrics { + constructor( + private readonly namespace: string | null = process.env.METRICS_NAMESPACE || null, + private readonly write: (line: string) => void = (line) => process.stdout.write(`${line}\n`) + ) {} + + record(values: Parameters[0], dimensions: Record = {}): void { + if (!this.namespace || !values.length) return; + try { + this.write(JSON.stringify({ + _aws: { + Timestamp: Date.now(), + CloudWatchMetrics: [{ + Namespace: this.namespace, + Dimensions: [Object.keys(dimensions)], + Metrics: values.map((metric) => ({ Name: metric.name, Unit: metric.unit })), + }], + }, + ...dimensions, + ...Object.fromEntries(values.map((metric) => [metric.name, metric.value])), + })); + } catch { + // Metrics are best effort. + } + } +} + +/** Adds call counts, failures and latency per operation to an existing usage log. */ +export class MeteredModelUsageLog implements ModelUsageLog { + constructor(private readonly inner: ModelUsageLog, private readonly metrics: Metrics) {} + + record(entry: Parameters[0]): void { + this.inner.record(entry); + this.metrics.record([ + { name: "ModelCalls", value: 1, unit: "Count" }, + { name: "ModelCallFailures", value: entry.outcome === "ok" ? 0 : 1, unit: "Count" }, + { name: "ModelCallLatencyMs", value: entry.latencyMs, unit: "Milliseconds" }, + ], { Operation: entry.operation }); + } +} diff --git a/apps/web/src/infrastructure/postgres/audit.repo.ts b/apps/web/src/infrastructure/postgres/audit.repo.ts new file mode 100644 index 0000000..69603c3 --- /dev/null +++ b/apps/web/src/infrastructure/postgres/audit.repo.ts @@ -0,0 +1,19 @@ +import { audit } from "@match-maker/db"; +import type { AuditLog } from "../../application/ports/audit-log"; + +export class PostgresAuditLog implements AuditLog { + async record(entry: Parameters[0]): Promise { + try { + await audit.record(entry); + } catch (error) { + // An unavailable audit table must not undo or block the action it records. + console.error("[audit] could not record", entry.action, error); + } + } + + async recent(eventId: string, limit = 20) { + return (await audit.listForEvent(eventId, limit)).map((row) => ({ + id: row.id, action: row.action, actorName: row.actor_name, subjectName: row.subject_name, details: row.details, createdAt: row.created_at, + })); + } +} diff --git a/apps/web/src/infrastructure/postgres/event.repo.ts b/apps/web/src/infrastructure/postgres/event.repo.ts index 821ebbf..f48ce7a 100644 --- a/apps/web/src/infrastructure/postgres/event.repo.ts +++ b/apps/web/src/infrastructure/postgres/event.repo.ts @@ -1,4 +1,4 @@ -import { attendees, events, query } from "@match-maker/db"; +import { events, query } from "@match-maker/db"; import type { EventRepository, EventRecord } from "../../application/ports/event-repository"; export class PostgresEventRepository implements EventRepository { @@ -11,8 +11,11 @@ export class PostgresEventRepository implements EventRepository { findOrganization(slug: string) { return events.findOrganization(slug); } - listForOrganization(organizationSlug: string) { - return events.listForOrganization(organizationSlug); + listForOrganization(organizationSlug: string, viewerId?: string) { + return events.listForOrganization(organizationSlug, viewerId); + } + organizerRoles(eventId: string, attendeeId: string) { + return events.organizerRoles(eventId, attendeeId); } hasOrganizationAccess(organizationSlug: string, attendeeId: string) { return events.hasOrganizationAccess(organizationSlug, attendeeId); @@ -37,28 +40,43 @@ export class PostgresEventRepository implements EventRepository { participantIds(eventId: string) { return events.participantIds(eventId); } + invitationTargets(eventId: string, attendeeId: string | null, resendAfterMinutes: number) { + return events.invitationTargets(eventId, attendeeId, resendAfterMinutes); + } + recordInvitation(eventId: string, attendeeId: string, error: string | null) { + return events.recordInvitation(eventId, attendeeId, error); + } + markClaimed(eventId: string, attendeeId: string) { + return events.markClaimed(eventId, attendeeId); + } + setRevoked(eventId: string, attendeeId: string, revoked: boolean) { + return events.setRevoked(eventId, attendeeId, revoked); + } + rosterState(eventId: string, attendeeId: string) { + return events.rosterState(eventId, attendeeId); + } - async importRoster(eventId: string, records: Parameters[1]) { - let upserted = 0; - for (let offset = 0; offset < records.length; offset += 100) { - upserted += await attendees.upsertMany(records.slice(offset, offset + 100)); - } - const rows = await attendees.findIdsByEmails(records.map((record) => record.email)); - const attached = await events.attachRoster(eventId, rows.map((row) => row.id)); - return { upserted, attached }; + importRoster(eventId: string, records: Parameters[1]) { + return events.importRoster(eventId, records); } async analytics(eventId: string) { const { rows } = await query<{ roster: string; + invited: string; + claimed: string; + removed: string; profiles: string; matches: string; pending_intros: string; accepted_intros: string; }>( `select - (select count(*) from event_participants where event_id = $1) as roster, - (select count(*) from event_participants where event_id = $1 and profile_updated_at is not null) as profiles, + (select count(*) from event_participants where event_id = $1 and revoked_at is null) as roster, + (select count(*) from event_participants where event_id = $1 and revoked_at is null and invited_at is not null) as invited, + (select count(*) from event_participants where event_id = $1 and revoked_at is null and claimed_at is not null) as claimed, + (select count(*) from event_participants where event_id = $1 and revoked_at is not null) as removed, + (select count(*) from event_participants where event_id = $1 and revoked_at is null and profile_updated_at is not null) as profiles, (select count(*) from event_matches where event_id = $1) as matches, (select count(*) from intro_requests where event_id = $1 and status = 'pending') as pending_intros, (select count(*) from intro_requests where event_id = $1 and status = 'accepted') as accepted_intros`, @@ -66,6 +84,9 @@ export class PostgresEventRepository implements EventRepository { ); return { roster: Number(rows[0]?.roster ?? 0), + invited: Number(rows[0]?.invited ?? 0), + claimed: Number(rows[0]?.claimed ?? 0), + removed: Number(rows[0]?.removed ?? 0), profiles: Number(rows[0]?.profiles ?? 0), matches: Number(rows[0]?.matches ?? 0), pendingIntros: Number(rows[0]?.pending_intros ?? 0), diff --git a/apps/web/src/infrastructure/postgres/intro.repo.ts b/apps/web/src/infrastructure/postgres/intro.repo.ts index 4f58db0..d1344f4 100644 --- a/apps/web/src/infrastructure/postgres/intro.repo.ts +++ b/apps/web/src/infrastructure/postgres/intro.repo.ts @@ -1,6 +1,7 @@ -import { query } from "@match-maker/db"; +import { outbox, query, transaction } from "@match-maker/db"; import type { IntroRepository } from "../../application/ports/intro-repository"; import type { IntroRequest, IntroStatus } from "../../domain/connection/intro-request.entity"; +import { introRequestRefusal } from "../../domain/connection/connection.rules"; interface IntroRow { id: string; @@ -20,16 +21,31 @@ export class PostgresIntroRepository implements IntroRepository { async create(input: { eventId: string; requesterId: string; recipientId: string; message?: string | null }): Promise { if (input.requesterId === input.recipientId) throw new Error("You cannot request an intro to yourself"); const message = input.message?.trim().slice(0, 500) || null; - const { rows } = await query( - `insert into intro_requests (event_id, requester_id, recipient_id, message) - values ($1, $2, $3, $4) - on conflict (event_id, requester_id, recipient_id) do update - set status = 'pending', message = excluded.message, created_at = now(), responded_at = null - where intro_requests.status = 'declined' - returning ${INTRO_COLUMNS}`, - [input.eventId, input.requesterId, input.recipientId, message] - ); - if (rows[0]) return mapIntro(rows[0]); + // The request and its notification commit together, so neither is lost. + const created = await transaction(async (client) => { + // One lock per pair of people, in either direction, so two requests + // crossing at the same moment cannot both pass the check below. + const [first, second] = [input.requesterId, input.recipientId].sort(); + await client.query(`select pg_advisory_xact_lock(hashtext('intro:' || $1 || ':' || $2 || ':' || $3))`, [input.eventId, first, second]); + const { rows: existing } = await client.query( + `select ${INTRO_COLUMNS} from intro_requests + where event_id = $1 and ((requester_id = $2 and recipient_id = $3) or (requester_id = $3 and recipient_id = $2))`, + [input.eventId, input.requesterId, input.recipientId] + ); + const refusal = introRequestRefusal(input.requesterId, existing.map(mapIntro)); + if (refusal) throw new Error(refusal); + const { rows } = await client.query( + `insert into intro_requests (event_id, requester_id, recipient_id, message) + values ($1, $2, $3, $4) + on conflict (event_id, requester_id, recipient_id) do nothing + returning ${INTRO_COLUMNS}`, + [input.eventId, input.requesterId, input.recipientId, message] + ); + if (!rows[0]) return null; + await outbox.enqueue({ eventId: input.eventId, recipientId: input.recipientId, kind: "intro.requested", subjectId: rows[0].id, dedupeKey: `intro.requested:${rows[0].id}` }, client); + return rows[0]; + }); + if (created) return mapIntro(created); throw new Error("An intro request already exists between these participants"); } @@ -42,21 +58,46 @@ export class PostgresIntroRepository implements IntroRepository { } async respond(eventId: string, introId: string, actorId: string, status: Extract): Promise { + const updated = await transaction(async (client) => { + const { rows } = await client.query( + `update intro_requests set status = $4, responded_at = now() + where event_id = $1 and id = $2 and recipient_id = $3 and status = 'pending' + returning ${INTRO_COLUMNS}`, + [eventId, introId, actorId, status] + ); + if (rows[0]?.status === "accepted") { + await outbox.enqueue({ eventId, recipientId: rows[0].requester_id, kind: "intro.accepted", subjectId: rows[0].id, dedupeKey: `intro.accepted:${rows[0].id}` }, client); + } + return rows[0] ?? null; + }); + if (!updated) throw new Error("Only the recipient can respond to a pending intro request"); + return mapIntro(updated); + } + + async findBetween(eventId: string, a: string, b: string): Promise { const { rows } = await query( - `update intro_requests set status = $4, responded_at = now() - where event_id = $1 and id = $2 and recipient_id = $3 and status = 'pending' - returning ${INTRO_COLUMNS}`, - [eventId, introId, actorId, status] + `select ${INTRO_COLUMNS} from intro_requests + where event_id = $1 and ((requester_id = $2 and recipient_id = $3) or (requester_id = $3 and recipient_id = $2))`, + [eventId, a, b] ); - if (!rows[0]) throw new Error("Only the recipient can respond to a pending intro request"); - return mapIntro(rows[0]); + return rows.map(mapIntro); + } + + async countPendingOutgoing(eventId: string, requesterId: string): Promise { + const { rows } = await query<{ count: number }>( + `select count(*)::int as count from intro_requests where event_id = $1 and requester_id = $2 and status = 'pending'`, + [eventId, requesterId] + ); + return rows[0]?.count ?? 0; } async listForParticipant(eventId: string, participantId: string): Promise { const { rows } = await query( - `select ${INTRO_COLUMNS} from intro_requests - where event_id = $1 and (requester_id = $2 or recipient_id = $2) - order by created_at desc`, + `select ${INTRO_COLUMNS_R} from intro_requests r + join event_participants other on other.event_id = r.event_id + and other.attendee_id = case when r.requester_id = $2 then r.recipient_id else r.requester_id end + where r.event_id = $1 and (r.requester_id = $2 or r.recipient_id = $2) and other.revoked_at is null + order by r.created_at desc`, [eventId, participantId] ); return rows.map(mapIntro); @@ -71,6 +112,8 @@ export class PostgresIntroRepository implements IntroRepository { left join event_participants cp on cp.event_id = r.event_id and cp.attendee_id = c.id where r.event_id = $1 and r.status = 'accepted' and (r.requester_id = $2 or r.recipient_id = $2) + -- Someone removed from the event no longer has their contact shown. + and cp.revoked_at is null order by r.responded_at desc`, [eventId, participantId] ); diff --git a/apps/web/src/infrastructure/postgres/location.repo.ts b/apps/web/src/infrastructure/postgres/location.repo.ts new file mode 100644 index 0000000..917edc9 --- /dev/null +++ b/apps/web/src/infrastructure/postgres/location.repo.ts @@ -0,0 +1,77 @@ +import { outbox, query, transaction } from "@match-maker/db"; +import type { LocationRepository } from "../../application/ports/location-repository"; +import { currentStatus, LOCATION_RULES, type LocationRequestView } from "../../domain/connection/location.rules"; + +interface RequestRow { + id: string; + intro_id: string; + requester_id: string; + status: "pending" | "answered" | "declined"; + zone: string | null; + created_at: Date; + expires_at: Date; + zone_expires_at: Date | null; +} + +export class PostgresLocationRepository implements LocationRepository { + async create(input: { eventId: string; introId: string; requesterId: string; recipientId: string }): Promise<{ id: string } | null> { + return transaction(async (client) => { + // A pending request that has run out of time no longer blocks a new one. + await client.query( + `update location_requests set status = 'declined' where intro_id = $1 and requester_id = $2 and status = 'pending' and expires_at < now()`, + [input.introId, input.requesterId] + ); + const { rows } = await client.query<{ id: string }>( + `insert into location_requests (event_id, intro_id, requester_id, recipient_id, expires_at) + values ($1, $2, $3, $4, now() + make_interval(mins => $5)) + on conflict (intro_id, requester_id) where status = 'pending' do nothing + returning id`, + [input.eventId, input.introId, input.requesterId, input.recipientId, LOCATION_RULES.requestExpiresMinutes] + ); + if (!rows[0]) return null; + await outbox.enqueue({ eventId: input.eventId, recipientId: input.recipientId, kind: "location.requested", subjectId: rows[0].id, dedupeKey: `location.requested:${rows[0].id}` }, client); + return rows[0]; + }); + } + + async answer(input: { eventId: string; requestId: string; recipientId: string; zone: string | null }): Promise { + return transaction(async (client) => { + const { rows } = await client.query<{ requester_id: string }>( + `update location_requests + set status = case when $4::text is null then 'declined' else 'answered' end, + zone = $4, answered_at = now(), + zone_expires_at = case when $4::text is null then null else now() + make_interval(mins => $5) end + where id = $1 and event_id = $2 and recipient_id = $3 and status = 'pending' and expires_at > now() + returning requester_id`, + [input.requestId, input.eventId, input.recipientId, input.zone, LOCATION_RULES.replyVisibleMinutes] + ); + if (!rows[0]) return false; + await outbox.enqueue({ eventId: input.eventId, recipientId: rows[0].requester_id, kind: "location.answered", subjectId: input.requestId, dedupeKey: `location.answered:${input.requestId}` }, client); + return true; + }); + } + + async forParticipant(eventId: string, participantId: string): Promise { + const { rows } = await query( + `select distinct on (intro_id, requester_id) id, intro_id, requester_id, status, zone, created_at, expires_at, zone_expires_at + from location_requests + where event_id = $1 and (requester_id = $2 or recipient_id = $2) + order by intro_id, requester_id, created_at desc`, + [eventId, participantId] + ); + return rows.map((row) => { + const outgoing = row.requester_id === participantId; + const status = currentStatus({ status: row.status, expiresAt: row.expires_at, zoneExpiresAt: row.zone_expires_at }); + return { + id: row.id, + introId: row.intro_id, + direction: outgoing ? "outgoing" : "incoming", + status, + // The zone is the asker's to see, and only while it is fresh. + zone: outgoing && status === "answered" ? row.zone : null, + createdAt: row.created_at, + zoneExpiresAt: row.zone_expires_at, + }; + }); + } +} diff --git a/apps/web/src/infrastructure/postgres/match.repo.ts b/apps/web/src/infrastructure/postgres/match.repo.ts index f491c75..8942530 100644 --- a/apps/web/src/infrastructure/postgres/match.repo.ts +++ b/apps/web/src/infrastructure/postgres/match.repo.ts @@ -26,10 +26,19 @@ interface MatchRow { }; export class PostgresMatchRepository implements MatchRepository { - async save(eventId: string, participantId: string, candidates: MatchCandidate[], computationVersion: string): Promise { - await transaction(async (client) => { + async save(eventId: string, participantId: string, candidates: MatchCandidate[], computationVersion: string, expectedRevision?: number): Promise { + return transaction(async (client) => { + if (expectedRevision !== undefined) { + // Lock the profile row so a save cannot slip in between check and write. + // NO KEY UPDATE still blocks a profile save, but not the key-share locks + // other people's match inserts take on this row through their foreign + // keys; FOR UPDATE made two lists naming each other deadlock. + const { rows } = await client.query<{ profile_revision: string }>( + `select profile_revision from event_participants where event_id = $1 and attendee_id = $2 for no key update`, [eventId, participantId]); + if (!rows[0] || Number(rows[0].profile_revision) !== expectedRevision) return false; + } await client.query(`delete from event_matches where event_id = $1 and participant_id = $2`, [eventId, participantId]); - if (candidates.length === 0) return; + if (candidates.length === 0) return true; // Array position is the final (possibly reranked) order, stored as rank 1..N. await client.query( `insert into event_matches @@ -45,6 +54,7 @@ export class PostgresMatchRepository implements MatchRepository { candidates.map((candidate) => candidate.reason), candidates.map((candidate) => candidate.mutualInterest)] ); + return true; }); } diff --git a/apps/web/src/infrastructure/postgres/model-usage.repo.ts b/apps/web/src/infrastructure/postgres/model-usage.repo.ts new file mode 100644 index 0000000..a3dc7eb --- /dev/null +++ b/apps/web/src/infrastructure/postgres/model-usage.repo.ts @@ -0,0 +1,9 @@ +import { modelUsage } from "@match-maker/db"; +import type { ModelUsageLog } from "../../application/ports/model-usage"; + +export class PostgresModelUsageLog implements ModelUsageLog { + record(entry: Parameters[0]): void { + // Fire and forget: accounting must never slow down or fail a model call. + modelUsage.record(entry).catch((error) => console.warn("[usage] could not record", error)); + } +} diff --git a/apps/web/src/infrastructure/postgres/participant.repo.ts b/apps/web/src/infrastructure/postgres/participant.repo.ts index c93ed7a..913e440 100644 --- a/apps/web/src/infrastructure/postgres/participant.repo.ts +++ b/apps/web/src/infrastructure/postgres/participant.repo.ts @@ -1,4 +1,5 @@ -import { query } from "@match-maker/db"; +import { matchingJobs, query, transaction } from "@match-maker/db"; +import { lexicalTsQuery, type SearchFilters } from "../../domain/matching/search-query"; import type { ParticipantRepository } from "../../application/ports/participant-repository"; import { Intent } from "../../domain/participant/intent"; import type { ParticipantProfile, ParticipantProfileInput } from "../../domain/participant/participant.entity"; @@ -21,6 +22,9 @@ interface ProfileRow { visible: boolean; embedding: string | number[] | null; profile_updated_at?: Date | null; + profile_form_version?: number; + profile_revision?: number; + embedding_model?: string | null; } const PROFILE_COLUMNS = `p.event_id, a.id as attendee_id, a.name, @@ -31,7 +35,7 @@ const PROFILE_COLUMNS = `p.event_id, a.id as attendee_id, a.name, coalesce(p.years_of_experience, a.years_of_experience) as years_of_experience, coalesce(p.most_impressive_build, a.most_impressive_build) as most_impressive_build, p.intent, p.interests, - p.venue_zone, p.visible, p.embedding::text as embedding, p.profile_updated_at`; + p.venue_zone, p.visible, p.embedding::text as embedding, p.profile_updated_at, p.profile_form_version, p.profile_revision::int as profile_revision, p.embedding_model`; const PROFILE_FROM = `from event_participants p join registered_attendees a on a.id = p.attendee_id`; /** @@ -39,7 +43,11 @@ const PROFILE_FROM = `from event_participants p * join), visible, named, and with a saved event profile. Every results path — * fresh retrieval, cached ids, saved matches — applies this same rule. */ -export const ELIGIBLE_CANDIDATE = `p.visible = true and a.name is not null and p.profile_updated_at is not null`; +/** Profile text searched when no embeddings are available. */ +const SEARCH_DOCUMENT = `to_tsvector('simple', concat_ws(' ', a.name, p.designation, p.domain, p.about_me, + p.self_description, p.most_impressive_build, array_to_string(p.interests, ' ')))`; + +export const ELIGIBLE_CANDIDATE = `p.visible = true and a.name is not null and p.profile_updated_at is not null and p.revoked_at is null`; export class PostgresParticipantRepository implements ParticipantRepository { async findProfile(eventId: string, attendeeId: string): Promise { @@ -51,19 +59,33 @@ export class PostgresParticipantRepository implements ParticipantRepository { return rows[0] ? mapProfile(rows[0]) : null; } + /** + * Saves the profile and queues its embedding in one transaction, so a crash + * cannot keep the change while losing the work that reflects it. + */ async saveProfile(input: ParticipantProfileInput): Promise { - const { rowCount } = await query( - `update event_participants set - intent = $3, interests = $4, venue_zone = $5, - about_me = $6, designation = $7, domain = $8, - self_description = $9, years_of_experience = $10, - most_impressive_build = $11, embedding = null, profile_updated_at = now(), updated_at = now() - where event_id = $1 and attendee_id = $2`, - [input.eventId, input.attendeeId, input.intent, input.interests, input.venueZone, - input.aboutMe, input.designation, input.domain, input.selfDescription, - input.yearsOfExperience, input.mostImpressiveBuild] - ); - if (!rowCount) throw new Error("This attendee is not on the event roster"); + await transaction(async (client) => { + const { rows: [updated] } = await client.query<{ profile_revision: string }>( + `update event_participants set + intent = $3, interests = $4, venue_zone = $5, + about_me = $6, designation = $7, domain = $8, + self_description = $9, years_of_experience = $10, + most_impressive_build = $11, profile_form_version = $12, visible = coalesce($13, visible), + embedding = null, profile_updated_at = now(), updated_at = now(), profile_revision = profile_revision + 1 + where event_id = $1 and attendee_id = $2 and revoked_at is null + returning profile_revision`, + [input.eventId, input.attendeeId, input.intent, input.interests, input.venueZone, + input.aboutMe, input.designation, input.domain, input.selfDescription, + input.yearsOfExperience, input.mostImpressiveBuild, input.formVersion ?? 0, input.visible ?? null] + ); + if (!updated) throw new Error("This attendee is not on the event roster"); + await matchingJobs.enqueue(input.eventId, input.attendeeId, "embed", { revision: Number(updated.profile_revision) }, client); + }); + if (input.visible === false) { + // Hidden profiles are not explained to others any more. + await query(`delete from pair_explanations where event_id = $1 and candidate_id = $2`, [input.eventId, input.attendeeId]); + await query(`delete from rerank_orders where event_id = $1 and $2::uuid = any(candidate_ids)`, [input.eventId, input.attendeeId]); + } const saved = await this.findProfile(input.eventId, input.attendeeId); if (!saved) throw new Error("Could not load the saved profile"); return saved; @@ -73,20 +95,50 @@ export class PostgresParticipantRepository implements ParticipantRepository { eventId: string; excludeAttendeeId: string | null; embedding?: number[] | null; + terms?: string[]; + filters?: SearchFilters; + welcomedIntents?: string[] | null; + /** The model that produced `embedding`; only candidates embedded by it are ranked by vector. */ + embeddingModel?: string | null; limit?: number; }): Promise { const vector = toVector(input.embedding ?? null); + // Lexical retrieval only when there is no query vector to rank by. + const tsQuery = vector ? null : lexicalTsQuery(input.terms ?? []); const { rows } = await query( `select ${PROFILE_COLUMNS} ${PROFILE_FROM} where p.event_id = $1 and ($2::uuid is null or a.id <> $2::uuid) and ${ELIGIBLE_CANDIDATE} - order by case when $3::vector is not null and p.embedding is not null then 0 else 1 end, - p.embedding <=> $3::vector nulls last, a.name asc + and ($5::text is null or p.intent = $5) + and ($6::text is null or lower(p.domain) = lower($6)) + and ($7::text is null or ${SEARCH_DOCUMENT} @@ to_tsquery('simple', $7)) + and ($9::text[] is null or p.intent = any($9::text[])) + order by case when $3::vector is not null and p.embedding is not null and p.embedding_model is not distinct from $8 then 0 else 1 end, + case when p.embedding_model is not distinct from $8 then p.embedding <=> $3::vector end nulls last, + case when $7::text is null then 0 else ts_rank(${SEARCH_DOCUMENT}, to_tsquery('simple', $7)) end desc, + a.name asc limit $4`, - [input.eventId, input.excludeAttendeeId, vector, Math.max(1, Math.min(100, input.limit ?? 50))] + [input.eventId, input.excludeAttendeeId, vector, Math.max(1, Math.min(100, input.limit ?? 50)), + input.filters?.intent ?? null, input.filters?.domain ?? null, tsQuery, input.embeddingModel ?? null, input.welcomedIntents ?? null] ); return rows.map(mapProfile); } + async corpusRevision(eventId: string): Promise { + const { rows } = await query<{ corpus_revision: string }>(`select corpus_revision from events where id = $1`, [eventId]); + return Number(rows[0]?.corpus_revision ?? 0); + } + + async listDomains(eventId: string): Promise { + const { rows } = await query<{ domain: string }>( + // One entry per domain ignoring case, preferring a capitalised spelling. + `select (array_agg(trim(p.domain) order by trim(p.domain) = lower(trim(p.domain)), trim(p.domain)))[1] as domain ${PROFILE_FROM} + where p.event_id = $1 and ${ELIGIBLE_CANDIDATE} and coalesce(trim(p.domain), '') <> '' + group by lower(trim(p.domain)) order by 1`, + [eventId] + ); + return rows.map((row) => row.domain.trim()); + } + async findCandidatesByIds(eventId: string, ids: string[]): Promise { if (!ids.length) return []; const { rows } = await query( @@ -98,12 +150,20 @@ export class PostgresParticipantRepository implements ParticipantRepository { return rows.map(mapProfile); } - async setEmbedding(eventId: string, attendeeId: string, embedding: number[]): Promise { - const vector = toVector(embedding); + async setEmbeddingAt(eventId: string, attendeeId: string, embedding: number[], revision: number, model: string): Promise { + const { rowCount } = await query( + `update event_participants set embedding = $3::vector, embedding_model = $5, updated_at = now() + where event_id = $1 and attendee_id = $2 and profile_revision = $4`, + [eventId, attendeeId, toVector(embedding), revision, model] + ); + return (rowCount ?? 0) > 0; + } + + async setEmbedding(eventId: string, attendeeId: string, embedding: number[], model: string): Promise { await query( - `update event_participants set embedding = $3::vector, updated_at = now() + `update event_participants set embedding = $3::vector, embedding_model = $4, updated_at = now() where event_id = $1 and attendee_id = $2`, - [eventId, attendeeId, vector] + [eventId, attendeeId, toVector(embedding), model] ); } @@ -118,7 +178,9 @@ export class PostgresParticipantRepository implements ParticipantRepository { async hasEventAccess(eventId: string, attendeeId: string): Promise { const { rows } = await query<{ allowed: boolean }>( - `select exists(select 1 from event_participants where event_id = $1 and attendee_id = $2) as allowed`, + `select exists( + select 1 from event_participants where event_id = $1 and attendee_id = $2 and revoked_at is null + ) as allowed`, [eventId, attendeeId] ); return rows[0]?.allowed ?? false; @@ -146,6 +208,10 @@ function mapProfile(row: ProfileRow): ParticipantProfile { venueZone: row.venue_zone, visible: row.visible, profileComplete: Boolean(row.profile_updated_at), + formVersion: row.profile_form_version ?? 0, + revision: row.profile_revision ?? 0, + embeddingModel: row.embedding_model ?? null, + profileRevision: row.profile_updated_at ? new Date(row.profile_updated_at).toISOString() : null, embedding: parseEmbedding(row.embedding), }; } diff --git a/apps/web/src/infrastructure/providers/gemini-embedding.provider.ts b/apps/web/src/infrastructure/providers/gemini-embedding.provider.ts index 1aa475f..0fc6024 100644 --- a/apps/web/src/infrastructure/providers/gemini-embedding.provider.ts +++ b/apps/web/src/infrastructure/providers/gemini-embedding.provider.ts @@ -1,9 +1,11 @@ -import type { EmbeddingProvider } from "../../application/ports/embedding-provider"; +import type { EmbeddingProvider, ProviderCallOptions } from "../../application/ports/embedding-provider"; const DIMENSIONS = 768; export class GeminiEmbeddingProvider implements EmbeddingProvider { - async embed(text: string, task: "profile" | "query"): Promise { + readonly id = `gemini:gemini-embedding-001:${DIMENSIONS}`; + + async embed(text: string, task: "profile" | "query", options: ProviderCallOptions = {}): Promise { const apiKey = process.env.GEMINI_API_KEY; if (!apiKey || !text.trim()) return null; const response = await fetch("https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent", { @@ -14,7 +16,7 @@ export class GeminiEmbeddingProvider implements EmbeddingProvider { taskType: task === "profile" ? "RETRIEVAL_DOCUMENT" : "RETRIEVAL_QUERY", outputDimensionality: DIMENSIONS, }), - signal: AbortSignal.timeout(15_000), + signal: options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(15_000)]) : AbortSignal.timeout(15_000), }); if (!response.ok) throw new Error(`Gemini embedding failed (${response.status}): ${(await response.text()).slice(0, 300)}`); const payload = await response.json() as { embedding?: { values?: number[] } }; diff --git a/apps/web/src/infrastructure/providers/gemini-llm.provider.ts b/apps/web/src/infrastructure/providers/gemini-llm.provider.ts index d3a3188..9a9ae80 100644 --- a/apps/web/src/infrastructure/providers/gemini-llm.provider.ts +++ b/apps/web/src/infrastructure/providers/gemini-llm.provider.ts @@ -1,15 +1,18 @@ import type { LlmProvider } from "../../application/ports/llm-provider"; +import type { ProviderCallOptions } from "../../application/ports/embedding-provider"; import type { MatchCandidate } from "../../domain/matching/match-strategy"; import type { ParticipantProfile } from "../../domain/participant/participant.entity"; const DEFAULT_MODEL = "gemini-3.8-flash"; export class GeminiLlmProvider implements LlmProvider { + readonly promptVersion = "p1"; + get id(): string { return `gemini:${process.env.GEMINI_LLM_MODEL || DEFAULT_MODEL}`; } - async rerank(viewer: ParticipantProfile, candidates: MatchCandidate[]): Promise { + async rerank(viewer: ParticipantProfile, candidates: MatchCandidate[], options: ProviderCallOptions = {}): Promise { const apiKey = process.env.GEMINI_API_KEY; if (!apiKey) return candidates; const model = process.env.GEMINI_LLM_MODEL || DEFAULT_MODEL; @@ -39,7 +42,7 @@ export class GeminiLlmProvider implements LlmProvider { contents: [{ parts: [{ text: prompt }] }], generationConfig: { temperature: 0.2, maxOutputTokens: 2048, responseMimeType: "application/json" }, }), - signal: AbortSignal.timeout(20_000), + signal: options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(20_000)]) : AbortSignal.timeout(20_000), }); if (!response.ok) throw new Error(`Gemini rerank failed (${response.status}): ${(await response.text()).slice(0, 300)}`); const payload = await response.json() as { candidates?: Array<{ content?: { parts?: Array<{ text?: string }> } }> }; diff --git a/apps/web/src/infrastructure/providers/hash-embedding.provider.ts b/apps/web/src/infrastructure/providers/hash-embedding.provider.ts new file mode 100644 index 0000000..902684a --- /dev/null +++ b/apps/web/src/infrastructure/providers/hash-embedding.provider.ts @@ -0,0 +1,23 @@ +import type { EmbeddingProvider } from "../../application/ports/embedding-provider"; + +const DIMENSIONS = 768; + +/** + * Words hashed into a unit vector: similar texts give similar vectors, with + * no network call and no cost. For capacity tests and staging rehearsals + * only (EMBEDDING_PROVIDER=hash); production refuses it. + */ +export class HashEmbeddingProvider implements EmbeddingProvider { + readonly id = "hash:768"; + + async embed(text: string): Promise { + const vector = new Array(DIMENSIONS).fill(0); + for (const word of text.toLowerCase().match(/[a-z0-9]+/g) ?? []) { + let hash = 0; + for (const char of word) hash = (hash * 31 + char.charCodeAt(0)) % DIMENSIONS; + vector[hash] += 1; + } + const norm = Math.hypot(...vector); + return norm ? vector.map((value) => value / norm) : null; + } +} diff --git a/apps/web/src/infrastructure/providers/local-bge.provider.ts b/apps/web/src/infrastructure/providers/local-bge.provider.ts index 55de0a1..e1d16c8 100644 --- a/apps/web/src/infrastructure/providers/local-bge.provider.ts +++ b/apps/web/src/infrastructure/providers/local-bge.provider.ts @@ -1,15 +1,17 @@ -import type { EmbeddingProvider } from "../../application/ports/embedding-provider"; +import type { EmbeddingProvider, ProviderCallOptions } from "../../application/ports/embedding-provider"; /** Adapter for an internal sentence-transformers inference endpoint. */ export class LocalBgeProvider implements EmbeddingProvider { - async embed(text: string): Promise { + readonly id = "local-bge:768"; + + async embed(text: string, _task: "profile" | "query" = "profile", options: ProviderCallOptions = {}): Promise { const endpoint = process.env.LOCAL_BGE_URL; if (!endpoint) return null; const response = await fetch(`${endpoint.replace(/\/$/, "")}/embed`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text, normalize: true }), - signal: AbortSignal.timeout(20_000), + signal: options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(20_000)]) : AbortSignal.timeout(20_000), }); if (!response.ok) throw new Error(`Local embedding service failed (${response.status})`); const result = await response.json() as { embedding?: number[] } | number[]; diff --git a/apps/web/src/infrastructure/providers/model-gateway.ts b/apps/web/src/infrastructure/providers/model-gateway.ts new file mode 100644 index 0000000..dec68dc --- /dev/null +++ b/apps/web/src/infrastructure/providers/model-gateway.ts @@ -0,0 +1,147 @@ +import type { EmbeddingProvider, ProviderCallOptions } from "../../application/ports/embedding-provider"; +import type { LlmProvider } from "../../application/ports/llm-provider"; +import type { ModelUsageLog } from "../../application/ports/model-usage"; +import type { MatchCandidate } from "../../domain/matching/match-strategy"; +import type { ParticipantProfile } from "../../domain/participant/participant.entity"; +import { Bulkhead, BulkheadFullError } from "../resilience/bulkhead"; +import { CircuitBreaker, CircuitOpenError } from "../resilience/circuit-breaker"; + +/** Per-operation limits: a time budget for the whole call and how many quick retries fit in it. */ +export interface GatewayPolicy { + timeoutMs: number; + retries: number; +} + +export const POLICIES = { + embedQuery: { timeoutMs: 2_500, retries: 1 }, + embedProfile: { timeoutMs: 15_000, retries: 2 }, + rerank: { timeoutMs: 20_000, retries: 1 }, +} satisfies Record; + +/** Rate limits, server errors, timeouts and network failures are worth one more try. */ +export function isTransient(error: unknown): boolean { + const message = error instanceof Error ? `${error.name} ${error.message}` : String(error); + return /\((429|5\d\d)\)|timeout|aborted|fetch failed|ECONNRESET|ETIMEDOUT|EAI_AGAIN/i.test(message); +} + +interface Guards { + breaker: CircuitBreaker; + bulkhead: Bulkhead; + usage: ModelUsageLog | null; +} + +/** + * Runs one provider call under the gateway's rules: a concurrency limit, a + * circuit breaker, a time budget for the whole call (including retries), and + * a couple of jittered retries for transient errors. Every attempt is + * recorded for usage and cost reports. Provider retries stay separate from + * background job retries, which take over when this gives up. + */ +export async function guardedCall(input: { + provider: string; + operation: string; + inputChars: number; + policy: GatewayPolicy; + budgetMs?: number; + guards: Guards; + call: (signal: AbortSignal) => Promise; +}): Promise { + const deadline = Date.now() + Math.min(input.policy.timeoutMs, input.budgetMs ?? Infinity); + for (let attempt = 0; ; attempt++) { + const remaining = deadline - Date.now(); + if (remaining <= 50) throw new Error(`${input.provider} ${input.operation} timeout: no time left in the budget`); + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(new Error(`${input.provider} ${input.operation} timeout`)), remaining); + const started = Date.now(); + try { + const result = await input.guards.bulkhead.run(() => input.guards.breaker.execute(() => input.call(controller.signal))); + input.guards.usage?.record({ provider: input.provider, operation: input.operation, outcome: "ok", latencyMs: Date.now() - started, inputChars: input.inputChars }); + return result; + } catch (error) { + const rejected = error instanceof CircuitOpenError || error instanceof BulkheadFullError; + const outcome = rejected ? "rejected" : controller.signal.aborted ? "timeout" : "error"; + input.guards.usage?.record({ provider: input.provider, operation: input.operation, outcome, latencyMs: Date.now() - started, inputChars: input.inputChars }); + const retryable = !rejected && (controller.signal.aborted ? false : isTransient(error)); + if (!retryable || attempt >= input.policy.retries) throw error; + const backoff = Math.min(100 * 2 ** attempt + Math.random() * 100, deadline - Date.now() - 100); + if (backoff <= 0) throw error; + await new Promise((resolve) => setTimeout(resolve, backoff)); + } finally { + clearTimeout(timer); + } + } +} + +/** + * Embedding calls through the gateway. A failed query embedding returns null, + * so search falls back to its word-matching path; a failed profile embedding + * throws, so the background job retries later. + */ +export class GatewayEmbeddingProvider implements EmbeddingProvider { + private readonly guards: Guards; + + constructor(private readonly inner: EmbeddingProvider, usage: ModelUsageLog | null) { + this.guards = { + breaker: new CircuitBreaker(`embeddings:${inner.id}`), + bulkhead: new Bulkhead(`embeddings:${inner.id}`, { maxConcurrent: 8, maxQueue: 32, queueTimeoutMs: 2_000 }), + usage, + }; + } + + get id(): string { + return this.inner.id; + } + + async embed(text: string, task: "profile" | "query", options: ProviderCallOptions = {}): Promise { + try { + return await guardedCall({ + provider: this.inner.id, + operation: `embed:${task}`, + inputChars: text.length, + policy: task === "query" ? POLICIES.embedQuery : POLICIES.embedProfile, + budgetMs: options.timeoutMs, + guards: this.guards, + call: (signal) => this.inner.embed(text, task, { ...options, signal }), + }); + } catch (error) { + if (task === "query") { + console.warn("[gateway] query embedding unavailable; searching by words", (error as Error).message); + return null; + } + throw error; + } + } +} + +/** Rerank calls through the gateway; failures surface so the stage keeps the scored order. */ +export class GatewayLlmProvider implements LlmProvider { + private readonly guards: Guards; + + constructor(private readonly inner: LlmProvider, usage: ModelUsageLog | null) { + this.guards = { + breaker: new CircuitBreaker(`llm:${inner.id}`), + bulkhead: new Bulkhead(`llm:${inner.id}`, { maxConcurrent: 4, maxQueue: 16, queueTimeoutMs: 2_000 }), + usage, + }; + } + + get id(): string { + return this.inner.id; + } + + get promptVersion(): string { + return this.inner.promptVersion; + } + + rerank(viewer: ParticipantProfile, candidates: MatchCandidate[], options: ProviderCallOptions = {}): Promise { + return guardedCall({ + provider: this.inner.id, + operation: "rerank", + inputChars: JSON.stringify(candidates.map((candidate) => candidate.participant.aboutMe ?? "")).length, + policy: POLICIES.rerank, + budgetMs: options.timeoutMs, + guards: this.guards, + call: (signal) => this.inner.rerank(viewer, candidates, { ...options, signal }), + }); + } +} diff --git a/apps/web/src/infrastructure/providers/provider-registry.ts b/apps/web/src/infrastructure/providers/provider-registry.ts index 28f1f5b..1cd4710 100644 --- a/apps/web/src/infrastructure/providers/provider-registry.ts +++ b/apps/web/src/infrastructure/providers/provider-registry.ts @@ -3,9 +3,11 @@ import type { LlmProvider } from "../../application/ports/llm-provider"; import { GeminiEmbeddingProvider } from "./gemini-embedding.provider"; import { GeminiLlmProvider } from "./gemini-llm.provider"; import { LocalBgeProvider } from "./local-bge.provider"; +import { HashEmbeddingProvider } from "./hash-embedding.provider"; export class ProviderRegistry { static embedding(): EmbeddingProvider { + if (process.env.EMBEDDING_PROVIDER === "hash") return new HashEmbeddingProvider(); if (process.env.EMBEDDING_PROVIDER === "local" && process.env.LOCAL_BGE_URL) { return new LocalBgeProvider(); } diff --git a/apps/web/src/infrastructure/resilience/bulkhead.ts b/apps/web/src/infrastructure/resilience/bulkhead.ts index 579f032..126bd5a 100644 --- a/apps/web/src/infrastructure/resilience/bulkhead.ts +++ b/apps/web/src/infrastructure/resilience/bulkhead.ts @@ -1 +1,49 @@ -export class Bulkhead {} +/** Thrown when a provider already has as many calls running and waiting as allowed. */ +export class BulkheadFullError extends Error { + constructor(name: string) { + super(`${name} is busy`); + this.name = "BulkheadFullError"; + } +} + +/** + * Limits concurrent calls to one provider in this process. Extra calls wait + * in a bounded queue for at most `queueTimeoutMs`, so a slow provider cannot + * tie up every request or exceed its rate limits. + */ +export class Bulkhead { + private running = 0; + private readonly waiting: Array<() => void> = []; + + constructor( + private readonly name: string, + private readonly options: { maxConcurrent: number; maxQueue: number; queueTimeoutMs: number } = { maxConcurrent: 8, maxQueue: 32, queueTimeoutMs: 2_000 } + ) {} + + async run(call: () => Promise): Promise { + if (this.running >= this.options.maxConcurrent) await this.wait(); + this.running += 1; + try { + return await call(); + } finally { + this.running -= 1; + this.waiting.shift()?.(); + } + } + + private wait(): Promise { + if (this.waiting.length >= this.options.maxQueue) return Promise.reject(new BulkheadFullError(this.name)); + return new Promise((resolve, reject) => { + const turn = () => { + clearTimeout(timer); + resolve(); + }; + const timer = setTimeout(() => { + const index = this.waiting.indexOf(turn); + if (index >= 0) this.waiting.splice(index, 1); + reject(new BulkheadFullError(this.name)); + }, this.options.queueTimeoutMs); + this.waiting.push(turn); + }); + } +} diff --git a/apps/web/src/infrastructure/resilience/circuit-breaker.ts b/apps/web/src/infrastructure/resilience/circuit-breaker.ts index 4068672..8b1a2ae 100644 --- a/apps/web/src/infrastructure/resilience/circuit-breaker.ts +++ b/apps/web/src/infrastructure/resilience/circuit-breaker.ts @@ -1 +1,48 @@ -export class CircuitBreaker {} +/** Thrown instead of calling a provider that keeps failing. */ +export class CircuitOpenError extends Error { + constructor(name: string) { + super(`${name} is temporarily unavailable`); + this.name = "CircuitOpenError"; + } +} + +/** + * Stops calling a provider after `failureThreshold` consecutive failures, for + * `resetAfterMs`. Then one trial call is let through: success closes the + * circuit, failure opens it again. Keeps an outage from adding a timeout to + * every request. + */ +export class CircuitBreaker { + private failures = 0; + private openedAt: number | null = null; + private trialRunning = false; + + constructor( + private readonly name: string, + private readonly options: { failureThreshold: number; resetAfterMs: number } = { failureThreshold: 5, resetAfterMs: 30_000 }, + private readonly now: () => number = Date.now + ) {} + + get state(): "closed" | "open" | "half-open" { + if (this.openedAt === null) return "closed"; + return this.now() - this.openedAt >= this.options.resetAfterMs ? "half-open" : "open"; + } + + async execute(call: () => Promise): Promise { + const state = this.state; + if (state === "open" || (state === "half-open" && this.trialRunning)) throw new CircuitOpenError(this.name); + if (state === "half-open") this.trialRunning = true; + try { + const result = await call(); + this.failures = 0; + this.openedAt = null; + return result; + } catch (error) { + this.failures += 1; + if (state === "half-open" || this.failures >= this.options.failureThreshold) this.openedAt = this.now(); + throw error; + } finally { + if (state === "half-open") this.trialRunning = false; + } + } +} diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts index e95b9fd..668e678 100644 --- a/apps/web/src/lib/auth.ts +++ b/apps/web/src/lib/auth.ts @@ -2,6 +2,7 @@ import { notFound, redirect } from "next/navigation"; import { getSession } from "../../lib/session"; import { getContainer } from "../composition/container"; import { eventAllows, type EventAction } from "../domain/shared/event-lifecycle"; +import { organizerCan, type OrganizerAction } from "../domain/shared/organizer-permissions"; /** With `action`, a page the event status does not allow sends the participant to the event landing page, which explains why. */ export async function requireParticipantEvent(eventSlug: string, returnPath?: string, action?: EventAction) { @@ -12,7 +13,9 @@ export async function requireParticipantEvent(eventSlug: string, returnPath?: st const event = await container.events.findByEventSlug(eventSlug); if (!event) notFound(); const registered = await container.participants.hasEventAccess(event.id, session.attendeeId); - if (!registered) notFound(); + // The landing page explains why: removed by the organizer, or a different account. + if (!registered) redirect(`/e/${eventSlug}`); + await container.events.markClaimed(event.id, session.attendeeId); if (action && !eventAllows(event.status, action)) redirect(`/e/${eventSlug}`); return { session, event, container }; } @@ -23,16 +26,26 @@ export async function requireOrganizerEvent(orgSlug: string, eventSlug: string, if (!session) redirect(`/login?returnTo=${encodeURIComponent(target)}`); const container = getContainer(); const event = await container.events.findBySlug(orgSlug, eventSlug); - if (!event || !(await container.events.isOrganizer(event.id, session.attendeeId))) notFound(); - return { session, event, container }; + if (!event) notFound(); + const roles = await container.events.organizerRoles(event.id, session.attendeeId); + if (!organizerCan(roles, "view")) notFound(); + const can = (action: OrganizerAction) => organizerCan(roles, action); + return { session, event, container, can }; } -export async function requireOrganizationMember(orgSlug: string, returnPath?: string) { +/** + * Organization pages. Members always get in; with `allowEventOrganizers`, so do + * people who organize one of its events without being members (they see only + * their events). `isMember` says whether they may create events. + */ +export async function requireOrganizationMember(orgSlug: string, returnPath?: string, options: { allowEventOrganizers?: boolean } = {}) { const target = returnPath || `/orgs/${orgSlug}/events/new`; const session = await getSession(); if (!session) redirect(`/login?returnTo=${encodeURIComponent(target)}`); const container = getContainer(); const organization = await container.events.findOrganization(orgSlug); - if (!organization || !(await container.events.hasOrganizationAccess(orgSlug, session.attendeeId))) notFound(); - return { session, organization, container }; + if (!organization) notFound(); + const isMember = await container.events.hasOrganizationAccess(orgSlug, session.attendeeId); + if (!isMember && !(options.allowEventOrganizers && (await container.events.listForOrganization(orgSlug, session.attendeeId)).length)) notFound(); + return { session, organization, container, isMember }; } diff --git a/apps/web/src/lib/env.ts b/apps/web/src/lib/env.ts index 07e909e..8308a4d 100644 --- a/apps/web/src/lib/env.ts +++ b/apps/web/src/lib/env.ts @@ -26,8 +26,8 @@ const POSITIVE_INTEGERS = [ ]; const ALLOWED: Record = { - DATABASE_SSL: ["disable", "require"], - EMBEDDING_PROVIDER: ["gemini", "local"], + DATABASE_SSL: ["disable", "require", "verify"], + EMBEDDING_PROVIDER: ["gemini", "local", "hash"], EMAIL_DEV_FALLBACK: ["true", "false"], TRUSTED_PROXY_HOPS: ["0", "1", "2", "3"], }; @@ -63,11 +63,43 @@ export function checkEnv(role: EnvRole, env: Env = process.env): string[] { problems.push("DATABASE_URL must start with postgres:// or postgresql://."); } + // Production must not quietly fall back to unverified or plain transport. + // Local hosts are exempt so the docker-compose stack still runs as production. + if (value("NODE_ENV") === "production") { + if (value("EMBEDDING_PROVIDER") === "hash") { + problems.push("EMBEDDING_PROVIDER=hash is for capacity tests only and cannot run in production."); + } + if (databaseUrl && !isLocalHost(databaseUrl)) { + if (value("DATABASE_SSL") !== "verify") { + problems.push("DATABASE_SSL must be verify in production, so the database certificate is checked."); + } else if (!value("DATABASE_CA_CERT") && !value("DATABASE_CA_CERT_FILE")) { + problems.push("DATABASE_SSL=verify needs DATABASE_CA_CERT or DATABASE_CA_CERT_FILE (the RDS CA bundle)."); + } + } + const base = value("APP_BASE_URL"); + if (role === "web" && !base) problems.push("APP_BASE_URL is required in production."); + if (role === "web" && base && isHttpUrl(base) && !isLocalHost(base) && new URL(base).protocol !== "https:") { + problems.push(`APP_BASE_URL must use https in production, got "${base}".`); + } + } + if (role === "web") { const secret = value("SESSION_SECRET"); if (secret && secret.length < 32) { problems.push("SESSION_SECRET must be at least 32 characters."); } + + // Login depends on e-mail, so a deploy that cannot send it must not boot. + if (!value("RESEND_API_KEY") && value("EMAIL_DEV_FALLBACK") !== "true") { + problems.push("RESEND_API_KEY is required to send login e-mail (EMAIL_DEV_FALLBACK=true logs codes instead, local only)."); + } + const from = value("EMAIL_FROM"); + if (value("RESEND_API_KEY") && !from) { + problems.push("EMAIL_FROM is required when RESEND_API_KEY is set, using a sender on a domain verified in Resend."); + } + if (from && !isSenderAddress(from)) { + problems.push(`EMAIL_FROM must be "address@domain" or "Name ", got "${from}".`); + } } if (value("EMBEDDING_PROVIDER") === "local" && !value("LOCAL_BGE_URL")) { @@ -92,6 +124,19 @@ export function assertEnv(role: EnvRole, env: Env = process.env): void { ); } +function isSenderAddress(raw: string): boolean { + const address = raw.match(/^[^<>]*<([^<>]+)>$/)?.[1] ?? raw; + return /^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]+$/.test(address.trim()); +} + +function isLocalHost(raw: string): boolean { + try { + return ["localhost", "127.0.0.1", "postgres"].includes(new URL(raw).hostname); + } catch { + return false; + } +} + function isHttpUrl(raw: string): boolean { try { const { protocol } = new URL(raw); diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json index a53f493..d49377e 100644 --- a/apps/web/tsconfig.json +++ b/apps/web/tsconfig.json @@ -1,7 +1,6 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "baseUrl": ".", "plugins": [{ "name": "next" }], "types": ["node"], "incremental": true, diff --git a/docker-compose.yml b/docker-compose.yml index d60efc2..10e572d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -55,6 +55,7 @@ services: - "${KEYCLOAK_PORT:-8080}:8080" volumes: - ./keycloak/realm-export.json:/opt/keycloak/data/import/realm-export.json:ro + - ./keycloak/themes/mr-bridge:/opt/keycloak/themes/mr-bridge:ro depends_on: postgres: condition: service_healthy @@ -101,6 +102,8 @@ services: KEYCLOAK_INTERNAL_ISSUER: http://keycloak:8080/realms/match-maker KEYCLOAK_CLIENT_ID: match-maker-web KEYCLOAK_CLIENT_SECRET: ${KEYCLOAK_CLIENT_SECRET:-dev-client-secret-change-me} + # Shows the Google option only when Keycloak's Google provider is configured. + GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID:-} SESSION_SECRET: ${SESSION_SECRET:?SESSION_SECRET is required - see .env.example} RESEND_API_KEY: ${RESEND_API_KEY:-} diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.md new file mode 100644 index 0000000..54a2552 --- /dev/null +++ b/docs/OPERATIONS.md @@ -0,0 +1,270 @@ +# Operations runbook + +How Mr. Bridge is released, watched and recovered in production. Every alarm +names a section of this file. Commands run from the repository root unless a +step says otherwise. AWS names follow the Terraform stack: +`match-maker-prod-*` for production, `match-maker-staging-*` for staging. + +## Service targets + +The launch target is one event of **300 attendees** with up to **15 +concurrent searches** and two worker tasks. + +| Measure | Budget | Checked by | +| --- | --- | --- | +| Search latency, p95 | ≤ 6 s (the search time budget; reranking is skipped when time runs short) | `SearchLatencyMs` alarm, capacity check | +| Every attendee has a match list after the profiles are saved | ≤ 10 min | capacity check | +| Queue quiet (all refreshes done) | ≤ 30 min | capacity check | +| A due matching job or notification waits | ≤ 10 min | `OldestDue*` alarms | +| Model calls per attendee (profile embedding plus reranks) | ≤ 6 | capacity check, `npm run models:usage` | +| Model calls per search (query embedding; cached for a day) | ≤ 2 | capacity check | +| Attendees left without matches | 0 | capacity check | + +Measured on a developer machine (local Postgres, free hash embedding, no +rerank), these are the system's own costs, without model latency: + +| Size | First match lists | Queue quiet | Search p50 / p95 | +| --- | --- | --- | --- | +| 300 attendees, 60 searches, 15 at a time | 6 s | 8 s | 42 ms / 83 ms | +| 1000 attendees, 200 searches, 40 at a time | 25 s | 36 s | 102 ms / 169 ms | + +Repeat the check on staging with the real providers before launch and after +any change to matching (see *Release*). It costs model calls. + +```sh +npm run capacity:check -- --attendees 300 --searches 60 --concurrency 15 --workers 0 --providers configured +``` + +`--workers 0` lets the deployed workers do the matching; the check only +watches. With `--workers N` it runs its own workers, so stop any other +worker on that database first. Without `--providers configured` it uses a +free deterministic embedding, which measures the system and not the models. +The check deletes its throwaway event and people when it finishes. + +## Release + +1. **CI** runs on every pull request and again inside every deploy: + typecheck, lint, unit tests, the migration gate, integration tests + against real Postgres and Keycloak (a skipped test fails the job), the + browser journey against the built app and worker, the image builds and + `terraform validate`. +2. **Staging.** Run the *Deploy to ECS* workflow with `environment: + staging` on the commit to release. It builds, migrates staging, rolls + out and smoke-tests. Then sign in on staging with a real e-mail and + Google account, walk the journey once (create an event, import, two + attendees, match, search, intro, accept, venue question), and run the + capacity check above. +3. **Production.** Merge to `main`, which builds and deploys that commit; + or, to ship exactly the images staging tested, run the workflow with + `environment: production` and `promote_tag: `. Promotion + copies the images unchanged, then migrates production and rolls out. + It needs `image_promotion_source = "match-maker-staging"` in production's + Terraform variables. +4. The deploy waits for the services to settle, checks the new version is + serving and its dependencies are ready, and puts the previous revisions + back by itself if that fails. + +### Migrations and rollback + +Migrations run before the new code and the previous image must keep +working on the migrated schema. CI enforces it (`tools/check-migrations.ts`): +released migrations never change, and a new migration may not drop or +rename a table or column, change a column type or truncate. To remove +something, stop using it in one release and drop it in a later one, with +the line marked `-- contract: `. + +### Roll back a release + +Run *Deploy to ECS* with `rollback_tag: ` (the 12-character +commit prefix shown in earlier runs and in `/api/health`). It skips checks, +build and migrations and redeploys those images. Because migrations only +expand, the old code runs on the current schema. Confirm `/api/health` +reports the old version and watch the alarms for ten minutes. + +Rehearse it on staging before launch: deploy a commit, roll back to the +previous tag, confirm sign-in and search still work, deploy forward again. + +## Alarms + +Alarms go to the SNS topic `match-maker-prod-alarms` (`alarm_email` in the +Terraform variables subscribes an address). The dashboard +`match-maker-prod` shows search latency, queue waits and sizes, failures, +model call failures and web response time. + +### Site down (`web-no-healthy-hosts`, `alb-5xx`, `alb-p95-latency`) + +1. ECS console → `match-maker-prod-web` → *Events* and stopped tasks: a + failing health check, an out-of-memory stop or a configuration error + (the app refuses to start on bad configuration and logs every problem). +2. If it began with a release, roll back (above). +3. If the database is the cause (`/api/health/ready` reports it), see the + database alarms. + +### Keycloak down (`keycloak-no-healthy-hosts`) + +Nobody can sign in (code sign-in also registers the person in Keycloak); +people already signed in carry on. Check the `match-maker-prod-keycloak` +service events and its logs in `/ecs/match-maker-prod/keycloak`; its +database login is in the `identity` secret. + +### Worker down (`worker-down`) + +No heartbeat means matching and notifications have stopped. Check the +`match-maker-prod-worker` service: a crash loop shows in its stopped tasks +and `/ecs/match-maker-prod/worker`. Queued work is durable; when the worker +returns it continues, and jobs held by a dead worker are reclaimed once +their lease ends (5 minutes). + +### Matching backlog (`matching-backlog`) + +Work is arriving faster than one worker handles it, or jobs are slow +because a model provider is slow. Check `ModelCallLatencyMs` and the model +failure alarm. To add capacity, raise `worker_desired_count` and apply, or +scale the service in the ECS console for the event day. Workers share the +queue safely. + +### Failed jobs (`jobs-failed`) + +A job failed five times. Run `npm run jobs:failed` against production (see +*Running tools against production*) to see each error. Fix the cause, then +`npm run jobs:retry -- all`. + +### Notifications stuck (`notification-backlog`, `notifications-failed`) + +Usually e-mail delivery. Check the worker log for `[worker:notify]` errors +and the Resend dashboard (key revoked, domain unverified, quota). Failed +notifications keep their last error in `notification_outbox`; once fixed, +set them back with +`update notification_outbox set status = 'pending', attempts = 0, available_at = now() where status = 'failed'`. +Each is sent at most once per subject, so retrying cannot duplicate. + +### Slow search (`search-slow`, `search-failing`) + +Search waits at most 2.5 s for the query embedding and skips reranking +when time runs short, so sustained slowness is usually the database. Check +RDS CPU and connections, then the model failure alarm, then errors in +`/ecs/match-maker-prod/web`. + +### Model provider failing (`model-failures`) + +Over 20% of Gemini calls fail. Matching keeps working on fallbacks: lexical +search, scored order with generated reasons. `npm run models:usage` shows +failures by operation. Common causes are an expired or revoked key (rotate +it, below) or an exhausted quota (raise it in Google AI Studio). The +gateway's circuit breaker stops calling a failing provider for 30 s at a +time. + +### Database (`rds-cpu`, `rds-free-storage`, `rds-connections`) + +CPU: look for a runaway query in Performance Insights; the worker can be +scaled down to shed load. Storage: raise `db_max_allocated_storage` (storage +grows by itself up to it) and apply. Connections: each task opens at most 10; a climb +without more tasks points at a leak in a recent release, so roll back. + +### Login e-mail failures (`login-email-failures`) + +People cannot sign in by code. Check the Resend dashboard and the +`RESEND_API_KEY` secret; rotate the key if needed (below). + +### Web at maximum capacity (`web-at-max-capacity`) + +Raise `web_max_capacity` and apply; check the database can take the extra +connections (10 per task). + +## Restore the database + +RDS keeps automated backups for `db_backup_retention_days` days and allows +point-in-time restore to any second in that window. + +1. Restore to a **new** instance; never overwrite the live one: + ```sh + aws rds restore-db-instance-to-point-in-time \ + --source-db-instance-identifier match-maker-prod-postgres \ + --target-db-instance-identifier match-maker-prod-restore-$(date +%Y%m%d%H%M) \ + --restore-time 2026-10-19T09:30:00Z \ + --db-subnet-group-name match-maker-prod-db-subnet-group --vpc-security-group-ids \ + --db-parameter-group-name match-maker-prod-pg16 \ + --no-publicly-accessible + aws rds wait db-instance-available --db-instance-identifier + ``` +2. Verify it from a task or host inside the VPC (the database is private): + ```sh + npm run db:verify -- --url "postgres://:@:5432/matchmaker" --compare "$DATABASE_URL" + ``` + It checks pgvector, that every migration of this release is applied + unchanged, the core tables, and lists row count differences against the + live database. +3. To switch over, stop the worker (desired count 0), point the + `DATABASE_URL` values in the `app` and `migrate` secrets at the restored + endpoint, run the migrate task (it also sets the runtime login's + password), force a new deployment of web and worker, and confirm + `/api/health/ready`. Keep the old instance until the restored one has + served a day. +4. For data recovery only (one deleted event, for example), copy the rows + from the restored instance instead of switching over. + +Rehearse before launch: restore staging to a time a few minutes ago and run +steps 1–2. Locally, `npm run db:restore-rehearsal` dumps the compose +database, restores it into a scratch database, verifies it and removes it, +timing each step. + +Targets: data loss at most 5 minutes (RDS uploads transaction logs every 5 +minutes); a restore and switch-over within 1 hour. + +## Rotate secrets + +All runtime secrets live in Secrets Manager (`match-maker-prod/app`, +`/identity`, `/migrate`). ECS reads them when a task starts, so every +rotation ends with a new deployment: + +```sh +aws ecs update-service --cluster match-maker-prod-cluster --service match-maker-prod-web --force-new-deployment +aws ecs update-service --cluster match-maker-prod-cluster --service match-maker-prod-worker --force-new-deployment +``` + +| Secret | How | Effect | +| --- | --- | --- | +| Session secret | `terraform apply -replace=random_password.session_secret_b -var session_secret_active=b`, then redeploy web. Next time replace `random_password.session_secret` and set `a`. | Nobody is signed out: cookies sealed with the old secret are still read (`SESSION_SECRET_PREVIOUS`) until they expire (7 days). Do not rotate again within those 7 days. | +| OTP pepper | `terraform apply -replace=random_password.otp_pepper`, redeploy web. | Codes e-mailed in the last 10 minutes stop working; people request a new one. | +| Database runtime password | `terraform apply -replace=random_password.db_runtime`, run the migrate task (sets the new password), redeploy web and worker. | Old tasks keep their open connections until replaced. | +| Keycloak client secret | `terraform apply -replace=random_password.keycloak_client_secret`, then `npm run keycloak:sync` against production Keycloak from an address in `keycloak_admin_cidrs`, then redeploy web. | Google sign-in fails between the apply and the sync; do it in a quiet hour. | +| Gemini, Resend, Upstash, Google keys | Create the new key at the provider, update the value in `match-maker-prod/app` (and `/identity` for Google), redeploy, then revoke the old key. | None if the old key is revoked last. | + +## Running tools against production + +The database is private, so tools run as one-off ECS tasks in the private +subnets with the command overridden: + +- `jobs:failed`, `jobs:retry`, `models:usage`, `db:verify`: the migrate task + definition (it has the database secrets), e.g. command + `npx tsx tools/manage-jobs.ts failed`. +- `account:*` and `keycloak:sync`: the worker task definition (it also has + the auth package), adding `KEYCLOAK_INTERNAL_ISSUER`, `KEYCLOAK_ADMIN` and + `KEYCLOAK_ADMIN_PASSWORD` (from the `identity` secret) to the overrides. + +## Accounts and data requests + +- Block a person everywhere: `npm run account:disable -- ` + (`account:enable` undoes it); sign out everywhere only: + `npm run account:revoke-sessions -- `. +- Erase a person: `npm run account:erase -- --yes`. This deletes + their profiles, matches, introductions, cached explanations and Keycloak + user, and records it in the audit log. + +## Launch checklist + +- [ ] Production Terraform applied with the domain, certificate, `alarm_email`, + `keycloak_admin_cidrs` and `image_promotion_source`; SNS e-mail confirmed. +- [ ] Real secrets filled in: Gemini, Resend (sending domain verified), Google + OAuth client (redirect URI on the Keycloak domain), Upstash if used. +- [ ] `npm run keycloak:sync` run once against production Keycloak. +- [ ] GitHub environments `production` and `staging` hold their AWS role, + subnets, security group and `APP_BASE_URL`; `staging` has + `STACK_NAME=match-maker-staging`. +- [ ] Staging journey walked with real e-mail and Google sign-in. +- [ ] Capacity check passed on staging with `--providers configured` at the + launch size. +- [ ] Rollback rehearsed on staging. +- [ ] Database restore rehearsed on staging and `db:verify` passed. +- [ ] Every alarm in this file seen once in the console, and the dashboard opened. +- [ ] Organizer accounts created (`npm run org:init`) and the first event set up. diff --git a/infra/docker/keycloak.Dockerfile b/infra/docker/keycloak.Dockerfile index e3b197c..608a199 100644 --- a/infra/docker/keycloak.Dockerfile +++ b/infra/docker/keycloak.Dockerfile @@ -23,9 +23,15 @@ FROM quay.io/keycloak/keycloak:26.0 COPY --from=builder /opt/keycloak/ /opt/keycloak/ +# Amazon's RDS CA bundle, referenced by KC_DB_URL (sslmode=verify-full). +ADD --chmod=644 https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /opt/keycloak/conf/rds-global-bundle.pem + # Imported on first boot. Placeholders inside are filled from the task's # environment (client secret, app URL, Google credentials). COPY keycloak/realm-export.json /opt/keycloak/data/import/realm-export.json +# Site-styled login theme; selected per realm by `npm run keycloak:sync`. +COPY keycloak/themes/mr-bridge /opt/keycloak/themes/mr-bridge + ENTRYPOINT ["/opt/keycloak/bin/kc.sh"] CMD ["start", "--optimized", "--import-realm"] diff --git a/infra/docker/migrate.Dockerfile b/infra/docker/migrate.Dockerfile index 5ff48e9..dc200b4 100644 --- a/infra/docker/migrate.Dockerfile +++ b/infra/docker/migrate.Dockerfile @@ -19,5 +19,9 @@ RUN --mount=type=cache,target=/root/.npm \ COPY packages/db packages/db COPY tools tools +# Amazon's RDS CA bundle, so DATABASE_SSL=verify can check the database +# certificate (DATABASE_CA_CERT_FILE points here in the task definitions). +ADD --chmod=644 https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /etc/ssl/certs/rds-global-bundle.pem + # Seeding is opt-in: it must run on a fresh local stack, never against prod data. CMD ["sh", "-c", "npx tsx packages/db/src/migrate.ts && if [ \"$SEED_ON_MIGRATE\" = \"true\" ]; then npx tsx packages/db/src/seed.ts; fi"] diff --git a/infra/docker/worker.Dockerfile b/infra/docker/worker.Dockerfile index ca7b4e8..98b6f14 100644 --- a/infra/docker/worker.Dockerfile +++ b/infra/docker/worker.Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1.7 # Runs the async profile embedding and matching queue processors. FROM node:22-alpine WORKDIR /repo @@ -14,6 +15,12 @@ COPY apps/web/src apps/web/src COPY packages/db packages/db COPY packages/auth packages/auth COPY packages/email packages/email +# Operator tools (account:*, keycloak:sync) run as one-off tasks from this image. +COPY tools tools + +# Amazon's RDS CA bundle, so DATABASE_SSL=verify can check the database +# certificate (DATABASE_CA_CERT_FILE points here in the task definitions). +ADD --chmod=644 https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /etc/ssl/certs/rds-global-bundle.pem ENV NODE_ENV=production # Release tag (commit SHA), logged when the worker starts. diff --git a/infra/terraform/alb.tf b/infra/terraform/alb.tf index 1e41e9c..c363314 100644 --- a/infra/terraform/alb.tf +++ b/infra/terraform/alb.tf @@ -104,6 +104,14 @@ resource "aws_lb_listener" "http" { port = 80 protocol = "HTTP" + lifecycle { + # Production must never serve logins and session cookies over plain HTTP. + precondition { + condition = var.environment != "prod" || (local.https_enabled && var.domain_name != "") + error_message = "environment = \"prod\" requires domain_name and acm_certificate_arn so the app is served over HTTPS." + } + } + dynamic "default_action" { for_each = local.https_enabled ? [1] : [] content { @@ -166,9 +174,56 @@ resource "aws_lb_listener_rule" "keycloak_host" { } } +# The admin console is not public: only var.keycloak_admin_cidrs reach it, and +# everyone else gets a 404 before the request is routed to Keycloak. The app's +# own admin API calls go over Cloud Map inside the VPC, not through the ALB. +resource "aws_lb_listener_rule" "keycloak_admin_allow" { + count = length(var.keycloak_admin_cidrs) > 0 ? 1 : 0 + + listener_arn = local.active_listener_arn + priority = 5 + + action { + type = "forward" + target_group_arn = aws_lb_target_group.keycloak.arn + } + + condition { + path_pattern { + values = ["/admin", "/admin/*"] + } + } + + condition { + source_ip { + values = var.keycloak_admin_cidrs + } + } +} + +resource "aws_lb_listener_rule" "keycloak_admin_deny" { + listener_arn = local.active_listener_arn + priority = 6 + + action { + type = "fixed-response" + fixed_response { + content_type = "text/plain" + message_body = "Not found" + status_code = "404" + } + } + + condition { + path_pattern { + values = ["/admin", "/admin/*"] + } + } +} + # Fallback: share the app hostname and split on the paths Keycloak owns. -# /auth/callback belongs to the APP, and is not listed here — Keycloak's own -# paths are /realms/*, /resources/* and /admin/*. +# /auth/callback belongs to the APP, and is not listed here. /admin/* is +# handled by the two rules above. resource "aws_lb_listener_rule" "keycloak_path" { count = local.keycloak_has_own_domain ? 0 : 1 @@ -182,7 +237,7 @@ resource "aws_lb_listener_rule" "keycloak_path" { condition { path_pattern { - values = ["/realms/*", "/resources/*", "/admin/*", "/js/*"] + values = ["/realms/*", "/resources/*", "/js/*"] } } } diff --git a/infra/terraform/database.tf b/infra/terraform/database.tf index a25b411..1d9f46c 100644 --- a/infra/terraform/database.tf +++ b/infra/terraform/database.tf @@ -19,6 +19,18 @@ resource "random_password" "db" { override_special = "!#$%&*()-_=+[]{}<>:?" } +# Least-privilege logins, created by the migration task (ensure-roles.ts). The +# master login above is used only to run migrations. +resource "random_password" "db_runtime" { + length = 40 + special = false +} + +resource "random_password" "db_keycloak" { + length = 40 + special = false +} + resource "aws_db_parameter_group" "main" { name = "${local.name}-pg16" family = "postgres16" @@ -86,7 +98,11 @@ resource "aws_db_instance" "main" { locals { db_password_encoded = urlencode(random_password.db.result) - app_database_url = format( + db_runtime_user = "app_runtime" + db_keycloak_user = "keycloak" + + # Migrations only. + admin_database_url = format( "postgres://%s:%s@%s:%s/%s", aws_db_instance.main.username, local.db_password_encoded, @@ -95,9 +111,23 @@ locals { aws_db_instance.main.db_name, ) + # Web and worker: row access only. + app_database_url = format( + "postgres://%s:%s@%s:%s/%s", + local.db_runtime_user, + random_password.db_runtime.result, + aws_db_instance.main.address, + aws_db_instance.main.port, + aws_db_instance.main.db_name, + ) + + # verify-full checks the RDS certificate with the bundle baked into the image. keycloak_jdbc_url = format( - "jdbc:postgresql://%s:%s/keycloak", + "jdbc:postgresql://%s:%s/keycloak?sslmode=verify-full&sslrootcert=/opt/keycloak/conf/rds-global-bundle.pem", aws_db_instance.main.address, aws_db_instance.main.port, ) + + # Path of the RDS CA bundle inside the web, worker and migrate images. + database_ca_file = "/etc/ssl/certs/rds-global-bundle.pem" } diff --git a/infra/terraform/ecs.tf b/infra/terraform/ecs.tf index 8d7ec90..70dbd22 100644 --- a/infra/terraform/ecs.tf +++ b/infra/terraform/ecs.tf @@ -211,6 +211,7 @@ resource "aws_ecs_task_definition" "web" { environment = [ { name = "NODE_ENV", value = "production" }, { name = "PORT", value = "3000" }, + { name = "METRICS_NAMESPACE", value = local.metrics_namespace }, { name = "APP_BASE_URL", value = local.app_base_url }, { name = "DEFAULT_EVENT_SLUG", value = var.default_event_slug }, # One ALB appends the client address to X-Forwarded-For. @@ -221,17 +222,21 @@ resource "aws_ecs_task_definition" "web" { { name = "KEYCLOAK_CLIENT_ID", value = var.keycloak_client_id }, { name = "KEYCLOAK_GOOGLE_IDP_ALIAS", value = "google" }, { name = "EMAIL_FROM", value = var.email_from }, - # RDS enforces TLS (rds.force_ssl), and the AWS CA is not in Node's - # default trust store, so verification is relaxed for this hop inside - # the VPC. Set DATABASE_CA_CERT to the RDS bundle to tighten it. - { name = "DATABASE_SSL", value = "require" }, + # RDS enforces TLS (rds.force_ssl); the certificate is checked against + # the RDS CA bundle baked into the image. + { name = "DATABASE_SSL", value = "verify" }, + { name = "DATABASE_CA_CERT_FILE", value = local.database_ca_file }, ] secrets = [ { name = "DATABASE_URL", valueFrom = local.secret_ref.DATABASE_URL }, { name = "SESSION_SECRET", valueFrom = local.secret_ref.SESSION_SECRET }, + { name = "SESSION_SECRET_PREVIOUS", valueFrom = local.secret_ref.SESSION_SECRET_PREVIOUS }, + { name = "OTP_PEPPER", valueFrom = local.secret_ref.OTP_PEPPER }, { name = "KEYCLOAK_CLIENT_SECRET", valueFrom = local.secret_ref.KEYCLOAK_CLIENT_SECRET }, { name = "RESEND_API_KEY", valueFrom = local.secret_ref.RESEND_API_KEY }, + # Only its presence is read: it switches the Google sign-in option on. + { name = "GOOGLE_CLIENT_ID", valueFrom = local.secret_ref.GOOGLE_CLIENT_ID }, { name = "GEMINI_API_KEY", valueFrom = local.secret_ref.GEMINI_API_KEY }, { name = "UPSTASH_REDIS_REST_URL", valueFrom = local.secret_ref.UPSTASH_REDIS_REST_URL }, { name = "UPSTASH_REDIS_REST_TOKEN", valueFrom = local.secret_ref.UPSTASH_REDIS_REST_TOKEN }, @@ -321,11 +326,13 @@ resource "aws_ecs_task_definition" "keycloak" { environment = [ { name = "KC_DB", value = "postgres" }, { name = "KC_DB_URL", value = local.keycloak_jdbc_url }, - { name = "KC_DB_USERNAME", value = aws_db_instance.main.username }, + { name = "KC_DB_USERNAME", value = local.db_keycloak_user }, # Tokens must be issued with the hostname the browser uses, or `iss` # will not match what the app verifies. { name = "KC_HOSTNAME", value = local.keycloak_public_origin }, - { name = "KC_HOSTNAME_STRICT", value = "false" }, + # Supported shared-domain setup: a fixed public hostname, while the app's + # back-channel calls over Cloud Map keep their internal URLs. + { name = "KC_HOSTNAME_BACKCHANNEL_DYNAMIC", value = "true" }, # TLS terminates at the ALB, so Keycloak speaks HTTP but must trust the # X-Forwarded-* headers to build correct absolute URLs. { name = "KC_PROXY_HEADERS", value = "xforwarded" }, @@ -339,12 +346,12 @@ resource "aws_ecs_task_definition" "keycloak" { ] secrets = [ - { name = "KC_DB_PASSWORD", valueFrom = local.secret_ref.KEYCLOAK_DB_PASSWORD }, - { name = "KC_BOOTSTRAP_ADMIN_PASSWORD", valueFrom = local.secret_ref.KEYCLOAK_ADMIN_PASSWORD }, + { name = "KC_DB_PASSWORD", valueFrom = local.identity_secret_ref.KEYCLOAK_DB_PASSWORD }, + { name = "KC_BOOTSTRAP_ADMIN_PASSWORD", valueFrom = local.identity_secret_ref.KEYCLOAK_ADMIN_PASSWORD }, # Substituted into realm-export.json during import. - { name = "KEYCLOAK_CLIENT_SECRET", valueFrom = local.secret_ref.KEYCLOAK_CLIENT_SECRET }, - { name = "GOOGLE_CLIENT_ID", valueFrom = local.secret_ref.GOOGLE_CLIENT_ID }, - { name = "GOOGLE_CLIENT_SECRET", valueFrom = local.secret_ref.GOOGLE_CLIENT_SECRET }, + { name = "KEYCLOAK_CLIENT_SECRET", valueFrom = local.identity_secret_ref.KEYCLOAK_CLIENT_SECRET }, + { name = "GOOGLE_CLIENT_ID", valueFrom = local.identity_secret_ref.GOOGLE_CLIENT_ID }, + { name = "GOOGLE_CLIENT_SECRET", valueFrom = local.identity_secret_ref.GOOGLE_CLIENT_SECRET }, ] logConfiguration = { @@ -418,23 +425,30 @@ resource "aws_ecs_task_definition" "migrate" { image = "${aws_ecr_repository.migrate.repository_url}:latest" essential = true - # Creates Keycloak's database on first run, then applies app migrations. - # Both steps are idempotent, so re-running a deploy is safe. + # Creates Keycloak's database on first run, applies app migrations, then + # creates/updates the runtime and Keycloak logins (after migrations, so + # grants cover every table). All steps are idempotent. command = [ "sh", "-c", - "npx tsx packages/db/src/ensure-database.ts keycloak && npx tsx packages/db/src/migrate.ts" + "npx tsx packages/db/src/ensure-database.ts keycloak && npx tsx packages/db/src/migrate.ts && npx tsx packages/db/src/ensure-roles.ts" ] environment = [ { name = "NODE_ENV", value = "production" }, - { name = "DATABASE_SSL", value = "require" }, + { name = "DATABASE_SSL", value = "verify" }, + { name = "DATABASE_CA_CERT_FILE", value = local.database_ca_file }, + { name = "DATABASE_RUNTIME_USER", value = local.db_runtime_user }, + { name = "KEYCLOAK_DB_USER", value = local.db_keycloak_user }, + { name = "KEYCLOAK_DB_NAME", value = "keycloak" }, # Seeding is for local development only; production data comes from the # Excel importer. { name = "SEED_ON_MIGRATE", value = "false" }, ] secrets = [ - { name = "DATABASE_URL", valueFrom = local.secret_ref.DATABASE_URL }, + { name = "DATABASE_URL", valueFrom = local.migrate_secret_ref.DATABASE_ADMIN_URL }, + { name = "DATABASE_RUNTIME_PASSWORD", valueFrom = local.migrate_secret_ref.DATABASE_RUNTIME_PASSWORD }, + { name = "KEYCLOAK_DB_PASSWORD", valueFrom = local.migrate_secret_ref.KEYCLOAK_DB_PASSWORD }, ] logConfiguration = { @@ -466,7 +480,9 @@ resource "aws_ecs_task_definition" "worker" { essential = true environment = [ { name = "NODE_ENV", value = "production" }, - { name = "DATABASE_SSL", value = "require" }, + { name = "METRICS_NAMESPACE", value = local.metrics_namespace }, + { name = "DATABASE_SSL", value = "verify" }, + { name = "DATABASE_CA_CERT_FILE", value = local.database_ca_file }, { name = "GEMINI_LLM_MODEL", value = "gemini-3.8-flash" }, { name = "EMBEDDING_PROVIDER", value = "gemini" }, ] diff --git a/infra/terraform/environments/staging.tfvars.example b/infra/terraform/environments/staging.tfvars.example new file mode 100644 index 0000000..cb6c6b4 --- /dev/null +++ b/infra/terraform/environments/staging.tfvars.example @@ -0,0 +1,54 @@ +# Staging: a separate stack (match-maker-staging-*) with its own state, in the +# same AWS account and region as production so tested images can be promoted +# unchanged. Copy to staging.tfvars (gitignored) and run, from infra/terraform: +# +# terraform init -reconfigure -backend-config="bucket=" \ +# -backend-config="key=match-maker/staging/terraform.tfstate" -backend-config="region=ap-south-1" +# terraform apply -var-file=environments/staging.tfvars +# +# Switch back to production with the same init and key=match-maker/terraform.tfstate. + +aws_region = "ap-south-1" +project_name = "match-maker" +environment = "staging" +default_event_slug = "demo" + +# Staging may be HTTP-only for a first evaluation; give it its own hostname +# and certificate to rehearse the production login flow (Google needs HTTPS). +domain_name = "" +keycloak_domain_name = "" +acm_certificate_arn = "" +route53_zone_id = "" +keycloak_admin_cidrs = [] + +# Production's stack creates the account's GitHub OIDC provider; reuse it. +github_repository = "LucknowAI/Bridge" +github_oidc_provider_arn = "arn:aws:iam:::oidc-provider/token.actions.githubusercontent.com" + +# Small but production-shaped: two web tasks so rolling deploys and the +# shared caches behave as in production. +web_desired_count = 2 +web_min_capacity = 2 +web_max_capacity = 4 +worker_desired_count = 1 +keycloak_min_capacity = 1 +keycloak_max_capacity = 2 + +db_instance_class = "db.t4g.micro" +db_multi_az = false +db_backup_retention_days = 3 +db_deletion_protection = false + +enable_nat_gateway = true + +# Staging gets its own provider keys, so its usage and quota never touch +# production's. Fill them into Secrets Manager after the first apply. +gemini_api_key = "" +upstash_redis_rest_url = "" +upstash_redis_rest_token = "" +resend_api_key = "" +google_client_id = "" +google_client_secret = "" + +email_from = "Mr. Bridge staging " +alarm_email = "" diff --git a/infra/terraform/iam.tf b/infra/terraform/iam.tf index 18a0ef1..a370fc4 100644 --- a/infra/terraform/iam.tf +++ b/infra/terraform/iam.tf @@ -37,8 +37,12 @@ resource "aws_iam_role_policy_attachment" "ecs_execution_managed" { # read access — not the task role. data "aws_iam_policy_document" "secrets_read" { statement { - actions = ["secretsmanager:GetSecretValue"] - resources = [aws_secretsmanager_secret.app.arn] + actions = ["secretsmanager:GetSecretValue"] + resources = [ + aws_secretsmanager_secret.app.arn, + aws_secretsmanager_secret.identity.arn, + aws_secretsmanager_secret.migrate.arn, + ] } } @@ -142,6 +146,17 @@ data "aws_iam_policy_document" "github_deploy" { resources = ["*"] } + # Promotion copies another stack's tested images (e.g. staging's) here + # unchanged, so this role may read that stack's repositories. + dynamic "statement" { + for_each = var.image_promotion_source != "" ? [var.image_promotion_source] : [] + content { + sid = "EcrPromoteFrom" + actions = ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer", "ecr:BatchCheckLayerAvailability", "ecr:DescribeImages"] + resources = ["arn:aws:ecr:${var.aws_region}:${data.aws_caller_identity.current.account_id}:repository/${statement.value}/*"] + } + } + statement { sid = "EcrPush" actions = [ @@ -158,6 +173,7 @@ data "aws_iam_policy_document" "github_deploy" { aws_ecr_repository.web.arn, aws_ecr_repository.keycloak.arn, aws_ecr_repository.migrate.arn, + aws_ecr_repository.worker.arn, ] } diff --git a/infra/terraform/monitoring.tf b/infra/terraform/monitoring.tf index 2e7c809..3720bab 100644 --- a/infra/terraform/monitoring.tf +++ b/infra/terraform/monitoring.tf @@ -17,6 +17,8 @@ resource "aws_sns_topic_subscription" "alarm_email" { locals { alarm_actions = [aws_sns_topic.alarms.arn] + # The web and worker write metrics here (CloudWatch embedded metric format). + metrics_namespace = "MatchMaker/${var.environment}" } # ── Availability ────────────────────────────────────────────── @@ -214,3 +216,179 @@ resource "aws_cloudwatch_metric_alarm" "login_email_failures" { alarm_actions = local.alarm_actions } + +# ── Pipeline ────────────────────────────────────────────────── +# Written by the worker once a minute and by every search. Each alarm names +# the runbook section in docs/OPERATIONS.md that says what to do. + +locals { + pipeline_alarms = { + worker-down = { + description = "No worker heartbeat for 5 minutes: matching and notifications have stopped. Runbook: Worker down." + metric = "WorkerHeartbeat" + statistic = "SampleCount" + period = 300 + periods = 1 + threshold = 1 + comparison = "LessThanThreshold" + missing = "breaching" + } + matching-backlog = { + description = "A due matching job has waited over 10 minutes. Runbook: Matching backlog." + metric = "OldestDueJobSeconds" + statistic = "Maximum" + period = 300 + periods = 2 + threshold = 600 + comparison = "GreaterThanThreshold" + missing = "notBreaching" + } + jobs-failed = { + description = "Matching jobs failed after every retry. Runbook: Failed jobs (npm run jobs:failed)." + metric = "JobsFailed" + statistic = "Maximum" + period = 300 + periods = 1 + threshold = 0 + comparison = "GreaterThanThreshold" + missing = "notBreaching" + } + notification-backlog = { + description = "A due notification has waited over 10 minutes. Runbook: Notifications stuck." + metric = "OldestDueNotificationSeconds" + statistic = "Maximum" + period = 300 + periods = 2 + threshold = 600 + comparison = "GreaterThanThreshold" + missing = "notBreaching" + } + notifications-failed = { + description = "Notifications failed after every retry. Runbook: Notifications stuck." + metric = "NotificationsFailed" + statistic = "Maximum" + period = 300 + periods = 1 + threshold = 0 + comparison = "GreaterThanThreshold" + missing = "notBreaching" + } + search-slow = { + description = "Search p95 above its 6 s budget for 15 minutes. Runbook: Slow search." + metric = "SearchLatencyMs" + statistic = "p95" + period = 300 + periods = 3 + threshold = 6000 + comparison = "GreaterThanThreshold" + missing = "notBreaching" + } + search-failing = { + description = "More than 5 searches failed in 5 minutes. Runbook: Slow search." + metric = "SearchFailures" + statistic = "Sum" + period = 300 + periods = 1 + threshold = 5 + comparison = "GreaterThanThreshold" + missing = "notBreaching" + } + } +} + +resource "aws_cloudwatch_metric_alarm" "pipeline" { + for_each = local.pipeline_alarms + + alarm_name = "${local.name}-${each.key}" + alarm_description = each.value.description + namespace = local.metrics_namespace + metric_name = each.value.metric + statistic = each.value.statistic == "p95" ? null : each.value.statistic + extended_statistic = each.value.statistic == "p95" ? "p95" : null + period = each.value.period + evaluation_periods = each.value.periods + threshold = each.value.threshold + comparison_operator = each.value.comparison + treat_missing_data = each.value.missing + + alarm_actions = local.alarm_actions + ok_actions = local.alarm_actions +} + +# Model failures are expected occasionally (the gateway falls back); only a +# sustained rate needs a person, e.g. an expired key or an exhausted quota. +resource "aws_cloudwatch_metric_alarm" "model_failures" { + alarm_name = "${local.name}-model-failures" + alarm_description = "Over 20% of model calls failing for 15 minutes; matching runs on fallbacks. Runbook: Model provider failing." + comparison_operator = "GreaterThanThreshold" + evaluation_periods = 3 + threshold = 20 + treat_missing_data = "notBreaching" + + metric_query { + id = "rate" + expression = "IF(calls > 0, 100 * failures / calls, 0)" + label = "Model call failure %" + return_data = true + } + + metric_query { + id = "failures" + expression = join(" + ", [for op in local.model_operations : "f_${replace(op, ":", "_")}"]) + } + + metric_query { + id = "calls" + expression = join(" + ", [for op in local.model_operations : "c_${replace(op, ":", "_")}"]) + } + + dynamic "metric_query" { + for_each = { for pair in setproduct(local.model_operations, ["f", "c"]) : "${pair[1]}_${replace(pair[0], ":", "_")}" => pair } + content { + id = metric_query.key + metric { + namespace = local.metrics_namespace + metric_name = metric_query.value[1] == "f" ? "ModelCallFailures" : "ModelCalls" + dimensions = { Operation = metric_query.value[0] } + period = 300 + stat = "Sum" + } + } + } + + alarm_actions = local.alarm_actions +} + +locals { + model_operations = ["embed:profile", "embed:query", "rerank"] +} + +resource "aws_cloudwatch_dashboard" "main" { + dashboard_name = local.name + dashboard_body = jsonencode({ + widgets = [ + for index, widget in [ + { title = "Search latency (ms)", metrics = [[local.metrics_namespace, "SearchLatencyMs", { stat = "p50" }], ["...", { stat = "p95" }]] }, + { title = "Queue wait (s)", metrics = [[local.metrics_namespace, "OldestDueJobSeconds"], [local.metrics_namespace, "OldestDueNotificationSeconds"]] }, + { title = "Queue size", metrics = [[local.metrics_namespace, "JobsPending"], [local.metrics_namespace, "NotificationsPending"]] }, + { title = "Failed for good", metrics = [[local.metrics_namespace, "JobsFailed"], [local.metrics_namespace, "NotificationsFailed"]] }, + { title = "Model call failures by operation", metrics = [for op in local.model_operations : [local.metrics_namespace, "ModelCallFailures", "Operation", op, { stat = "Sum" }]] }, + { title = "Web response time (s)", metrics = [["AWS/ApplicationELB", "TargetResponseTime", "LoadBalancer", aws_lb.main.arn_suffix, { stat = "p95" }]] }, + ] : { + type = "metric" + x = (index % 2) * 12 + y = floor(index / 2) * 6 + width = 12 + height = 6 + properties = { + title = widget.title + metrics = widget.metrics + region = var.aws_region + stat = "Maximum" + period = 300 + view = "timeSeries" + } + } + ] + }) +} diff --git a/infra/terraform/outputs.tf b/infra/terraform/outputs.tf index cb2f601..f3c3043 100644 --- a/infra/terraform/outputs.tf +++ b/infra/terraform/outputs.tf @@ -94,7 +94,7 @@ output "secrets_manager_secret_name" { } output "keycloak_admin_console_url" { - description = "Keycloak admin console. The bootstrap password is in Secrets Manager." + description = "Keycloak admin console, reachable only from var.keycloak_admin_cidrs. The bootstrap password is in the identity secret." value = "${local.keycloak_public_origin}/admin" } @@ -103,8 +103,9 @@ output "post_apply_checklist" { value = <<-EOT 1. Create a Google OAuth client and set its authorised redirect URI to: ${local.keycloak_public_origin}/realms/${var.keycloak_realm}/broker/google/endpoint - Then put the id/secret into the ${aws_secretsmanager_secret.app.name} secret - (GOOGLE_CLIENT_ID / GOOGLE_CLIENT_SECRET) and redeploy Keycloak. + Then put the id/secret into the ${aws_secretsmanager_secret.identity.name} secret + (GOOGLE_CLIENT_ID / GOOGLE_CLIENT_SECRET), and GOOGLE_CLIENT_ID into + ${aws_secretsmanager_secret.app.name} too, then redeploy web and Keycloak. 2. Verify your sending domain in Resend and set RESEND_API_KEY in the same secret. 3. Set GEMINI_API_KEY in the same secret. 4. Import the attendee roster: npm run db:import -- attendees.xlsx diff --git a/infra/terraform/secrets.tf b/infra/terraform/secrets.tf index ecb51fa..5261cd1 100644 --- a/infra/terraform/secrets.tf +++ b/infra/terraform/secrets.tf @@ -5,12 +5,28 @@ # ============================================================== # Encrypts the session cookie. Generated here so it never has to be handled by a -# human; rotating it simply signs everyone out. +# human. Two slots make rotation free of sign-outs: the active slot seals new +# cookies, the other is still accepted for reading (SESSION_SECRET_PREVIOUS). +# To rotate, regenerate the inactive slot and make it active in one apply: +# terraform apply -replace=random_password.session_secret_b -var session_secret_active=b +# (next time: -replace=random_password.session_secret -var session_secret_active=a) resource "random_password" "session_secret" { length = 64 special = false } +resource "random_password" "session_secret_b" { + length = 64 + special = false +} + +# Hashes login codes; separate from the session secret so rotating that one +# leaves codes already e-mailed valid. +resource "random_password" "otp_pepper" { + length = 64 + special = false +} + resource "random_password" "keycloak_client_secret" { length = 48 special = false @@ -22,25 +38,42 @@ resource "random_password" "keycloak_admin_password" { override_special = "!#$%&*()-_=+" } +# Three secrets, so each task reads only its own credentials: +# app web and worker (runtime database login, no master password) +# identity Keycloak (its own database login and admin bootstrap) +# migrate the migration task (master login plus the passwords it sets) locals { secret_values = { DATABASE_URL = local.app_database_url - SESSION_SECRET = random_password.session_secret.result + SESSION_SECRET = var.session_secret_active == "a" ? random_password.session_secret.result : random_password.session_secret_b.result + SESSION_SECRET_PREVIOUS = var.session_secret_active == "a" ? random_password.session_secret_b.result : random_password.session_secret.result + OTP_PEPPER = random_password.otp_pepper.result KEYCLOAK_CLIENT_SECRET = random_password.keycloak_client_secret.result - KEYCLOAK_ADMIN_PASSWORD = random_password.keycloak_admin_password.result - KEYCLOAK_DB_PASSWORD = random_password.db.result GEMINI_API_KEY = var.gemini_api_key UPSTASH_REDIS_REST_URL = var.upstash_redis_rest_url UPSTASH_REDIS_REST_TOKEN = var.upstash_redis_rest_token RESEND_API_KEY = var.resend_api_key GOOGLE_CLIENT_ID = var.google_client_id - GOOGLE_CLIENT_SECRET = var.google_client_secret + } + + identity_secret_values = { + KEYCLOAK_ADMIN_PASSWORD = random_password.keycloak_admin_password.result + KEYCLOAK_DB_PASSWORD = random_password.db_keycloak.result + KEYCLOAK_CLIENT_SECRET = random_password.keycloak_client_secret.result + GOOGLE_CLIENT_ID = var.google_client_id + GOOGLE_CLIENT_SECRET = var.google_client_secret + } + + migrate_secret_values = { + DATABASE_ADMIN_URL = local.admin_database_url + DATABASE_RUNTIME_PASSWORD = random_password.db_runtime.result + KEYCLOAK_DB_PASSWORD = random_password.db_keycloak.result } } resource "aws_secretsmanager_secret" "app" { name = "${local.name}/app" - description = "Runtime secrets for the Mr. Bridge web and Keycloak services." + description = "Runtime secrets for the Mr. Bridge web and worker services." # Zero means immediate deletion, which keeps `terraform destroy` from leaving # a name reserved for 30 days and blocking a re-apply. @@ -52,13 +85,32 @@ resource "aws_secretsmanager_secret_version" "app" { secret_string = jsonencode(local.secret_values) } -# Individual ARNs, referenced one-by-one from the task definitions so each +resource "aws_secretsmanager_secret" "identity" { + name = "${local.name}/identity" + description = "Keycloak's database login, admin bootstrap and identity-provider secrets." + recovery_window_in_days = 0 +} + +resource "aws_secretsmanager_secret_version" "identity" { + secret_id = aws_secretsmanager_secret.identity.id + secret_string = jsonencode(local.identity_secret_values) +} + +resource "aws_secretsmanager_secret" "migrate" { + name = "${local.name}/migrate" + description = "Database master login, used only by the migration task." + recovery_window_in_days = 0 +} + +resource "aws_secretsmanager_secret_version" "migrate" { + secret_id = aws_secretsmanager_secret.migrate.id + secret_string = jsonencode(local.migrate_secret_values) +} + +# Individual keys, referenced one by one from the task definitions so each # container receives only the values it actually needs. locals { - secret_arn = aws_secretsmanager_secret.app.arn - - secret_ref = { - for key in keys(local.secret_values) : - key => "${local.secret_arn}:${key}::" - } + secret_ref = { for key in keys(local.secret_values) : key => "${aws_secretsmanager_secret.app.arn}:${key}::" } + identity_secret_ref = { for key in keys(local.identity_secret_values) : key => "${aws_secretsmanager_secret.identity.arn}:${key}::" } + migrate_secret_ref = { for key in keys(local.migrate_secret_values) : key => "${aws_secretsmanager_secret.migrate.arn}:${key}::" } } diff --git a/infra/terraform/terraform.tfvars.example b/infra/terraform/terraform.tfvars.example index 5137123..c6385b3 100644 --- a/infra/terraform/terraform.tfvars.example +++ b/infra/terraform/terraform.tfvars.example @@ -6,13 +6,18 @@ environment = "prod" default_event_slug = "demo" # ── DNS / TLS ───────────────────────────────────────────────── -# Leave all three empty for a first apply; the stack comes up on the ALB's own -# DNS name over HTTP, and you can add the domain later. +# Required when environment = "prod": the plan refuses to serve production +# over plain HTTP. For an HTTP-only evaluation stack, set environment = "staging" +# and leave these empty. domain_name = "" keycloak_domain_name = "" acm_certificate_arn = "" route53_zone_id = "" +# CIDR blocks allowed to open the Keycloak admin console through the load +# balancer, e.g. ["203.0.113.10/32"]. Empty keeps it off the internet. +keycloak_admin_cidrs = [] + # ── CI/CD ───────────────────────────────────────────────────── # Enables the GitHub OIDC deploy role. Format: "owner/repo". github_repository = "LucknowAI/Bridge" diff --git a/infra/terraform/variables.tf b/infra/terraform/variables.tf index a58730a..2c9ff5c 100644 --- a/infra/terraform/variables.tf +++ b/infra/terraform/variables.tf @@ -54,7 +54,7 @@ variable "enable_nat_gateway" { # ── DNS / TLS ───────────────────────────────────────────────── variable "domain_name" { - description = "Public hostname for the app, e.g. bridge.example.com. Leave empty to serve on the ALB's own DNS name over HTTP only." + description = "Public hostname for the app, e.g. bridge.example.com. Required (with acm_certificate_arn) when environment is prod; leave empty only for a non-prod evaluation stack served over HTTP." type = string default = "" } @@ -71,6 +71,12 @@ variable "acm_certificate_arn" { default = "" } +variable "keycloak_admin_cidrs" { + description = "IPv4 CIDR blocks allowed to reach the Keycloak admin console (/admin) through the load balancer. Empty keeps it unreachable from the internet; use ECS Exec port forwarding instead." + type = list(string) + default = [] +} + variable "route53_zone_id" { description = "Route53 hosted zone to create records in. Leave empty to manage DNS elsewhere." type = string @@ -299,3 +305,20 @@ variable "enable_container_insights" { type = bool default = true } + +variable "session_secret_active" { + description = "Which session-secret slot seals new cookies (a or b); the other is still accepted. Flip it, with -replace of the newly active slot, to rotate without signing anyone out." + type = string + default = "a" + + validation { + condition = contains(["a", "b"], var.session_secret_active) + error_message = "session_secret_active must be \"a\" or \"b\"." + } +} + +variable "image_promotion_source" { + description = "Stack whose images production may promote unchanged, e.g. \"match-maker-staging\" (same account and region). Empty disables promotion." + type = string + default = "" +} diff --git a/keycloak/themes/mr-bridge/login/resources/css/mr-bridge.css b/keycloak/themes/mr-bridge/login/resources/css/mr-bridge.css new file mode 100644 index 0000000..999ee0f --- /dev/null +++ b/keycloak/themes/mr-bridge/login/resources/css/mr-bridge.css @@ -0,0 +1,57 @@ +/* Matches apps/web/app/globals.css: dark surfaces, amber accent. */ +:root { + --pf-v5-global--FontFamily--text: Inter, system-ui, sans-serif; + --pf-v5-global--FontFamily--heading: Inter, system-ui, sans-serif; + --pf-v5-global--primary-color--100: #f59e0b; + --pf-v5-global--primary-color--200: #d97706; + --pf-v5-global--link--Color: #f59e0b; + --pf-v5-global--link--Color--hover: #fbbf24; + --pf-v5-global--Color--100: #f4f4f5; + --pf-v5-global--Color--200: #a1a1aa; + --pf-v5-global--BackgroundColor--100: #151519; + --pf-v5-global--BorderColor--100: #3f3f46; +} + +html, body, .pf-v5-c-login { + background: #0a0a0e !important; + color: #f4f4f5; +} + +.pf-v5-c-login__main { + background: #151519; + border: 1px solid #333; + border-radius: 14px; +} + +.pf-v5-c-login__main-header, +.pf-v5-c-login__main-body, +.pf-v5-c-login__main-footer, +.pf-v5-c-title, +.pf-v5-c-form__label-text { + color: #f4f4f5; +} + +#kc-header-wrapper { + color: #f59e0b; + font-weight: 700; + letter-spacing: 2px; + text-transform: uppercase; +} + +.pf-v5-c-button.pf-m-primary { + --pf-v5-c-button--m-primary--BackgroundColor: #f59e0b; + --pf-v5-c-button--m-primary--hover--BackgroundColor: #fbbf24; + --pf-v5-c-button--m-primary--Color: #0a0a0e; + --pf-v5-c-button--m-primary--hover--Color: #0a0a0e; + font-weight: 700; +} + +.pf-v5-c-form-control { + --pf-v5-c-form-control--BackgroundColor: #0a0a0e; + --pf-v5-c-form-control--Color: #f4f4f5; +} + +:where(a, button, input):focus-visible { + outline: 2px solid #f59e0b; + outline-offset: 3px; +} diff --git a/keycloak/themes/mr-bridge/login/theme.properties b/keycloak/themes/mr-bridge/login/theme.properties new file mode 100644 index 0000000..2ef7c03 --- /dev/null +++ b/keycloak/themes/mr-bridge/login/theme.properties @@ -0,0 +1,6 @@ +# Mr. Bridge colours on the few Keycloak screens people can reach (account +# linking, errors, logout confirmation). Applied by `npm run keycloak:sync`. +parent=keycloak.v2 +import=common/keycloak +styles=css/styles.css css/mr-bridge.css +darkMode=false diff --git a/package-lock.json b/package-lock.json index 7a1db06..44a8a2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "packages/*" ], "devDependencies": { + "@playwright/test": "1.63.0", "@types/node": "^22.10.2", "@types/pg": "^8.11.10", "@types/react": "^19.0.2", @@ -1512,6 +1513,22 @@ "node": ">=12.4.0" } }, + "node_modules/@playwright/test": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.63.0.tgz", + "integrity": "sha512-oxMK4vllB9RK5NQ2l1pq1IfOf2AvnEuj/vYGDj0H2nMtmtZpKtCwt/l00GEO6xjGfpBNAvjovvYdCm50dRQkpQ==", + "devOptional": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.63.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/@react-email/render": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@react-email/render/-/render-1.1.2.tgz", @@ -6141,6 +6158,35 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/playwright": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.63.0.tgz", + "integrity": "sha512-+7ziBLidS4NaNCdt57SUDT+wYmmd5fmiQejUic/kb+YsYSCPyOOE9sebzMjNmQrsnNpDJqd4WHvV/8lfKfUDUg==", + "devOptional": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.63.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.63.0.tgz", + "integrity": "sha512-rYCsBF/M5HjUch52bbtVONEFjv6Xu8sm8h72dNlR5bzIE1fvC/bxgspzkjSfU+MweEMmPM8KJebG6nnyxo5mCg==", + "devOptional": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/possible-typed-array-names": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz", diff --git a/package.json b/package.json index 849b513..acb2922 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "typecheck": "turbo run typecheck", "test": "vitest run", "test:watch": "vitest", + "test:e2e": "playwright test", "db:migrate": "tsx packages/db/src/migrate.ts", "db:seed": "tsx packages/db/src/seed.ts", "db:import": "tsx tools/import-attendees.ts", @@ -27,9 +28,22 @@ "docker:up": "docker compose up -d --build", "docker:down": "docker compose down", "docker:logs": "docker compose logs -f web", - "keycloak:wait": "tsx tools/wait-for-keycloak.ts" + "keycloak:wait": "tsx tools/wait-for-keycloak.ts", + "keycloak:sync": "tsx tools/sync-keycloak.ts", + "account:disable": "tsx tools/manage-account.ts disable", + "account:enable": "tsx tools/manage-account.ts enable", + "account:revoke-sessions": "tsx tools/manage-account.ts revoke-sessions", + "account:erase": "tsx tools/manage-account.ts erase", + "jobs:failed": "tsx tools/manage-jobs.ts failed", + "jobs:retry": "tsx tools/manage-jobs.ts retry", + "models:usage": "tsx tools/model-usage.ts", + "capacity:check": "tsx tools/capacity-check.ts", + "db:verify": "tsx tools/verify-database.ts", + "db:restore-rehearsal": "tsx tools/restore-rehearsal.ts", + "db:check-migrations": "tsx tools/check-migrations.ts" }, "devDependencies": { + "@playwright/test": "1.63.0", "@types/node": "^22.10.2", "@types/pg": "^8.11.10", "@types/react": "^19.0.2", diff --git a/packages/auth/src/config.ts b/packages/auth/src/config.ts index 0099a84..4f6fb5e 100644 --- a/packages/auth/src/config.ts +++ b/packages/auth/src/config.ts @@ -55,11 +55,26 @@ export function getAuthConfig(): AuthConfig { }; } +/** + * Google sign-in is offered only when the Google client is configured; the + * same GOOGLE_CLIENT_ID feeds Keycloak's identity provider. + */ +export function isGoogleSignInEnabled(): boolean { + const clientId = process.env.GOOGLE_CLIENT_ID?.trim(); + return Boolean(clientId && clientId !== "not-configured"); +} + export interface SessionConfig { secret: string; cookieName: string; /** Session lifetime in seconds. */ maxAge: number; + /** + * Earlier secrets still accepted for reading cookies (never for writing), + * so rotating SESSION_SECRET does not sign anyone out. Keep the old value in + * SESSION_SECRET_PREVIOUS for at least one session lifetime. + */ + previousSecrets: string[]; secure: boolean; } @@ -71,8 +86,13 @@ export function getSessionConfig(): SessionConfig { "Generate one with: openssl rand -base64 48" ); } + const previousSecrets = (process.env.SESSION_SECRET_PREVIOUS ?? "").split(",").map((value) => value.trim()).filter(Boolean); + if (previousSecrets.some((value) => value.length < 32)) { + throw new Error("Every SESSION_SECRET_PREVIOUS value must be at least 32 characters."); + } return { secret, + previousSecrets, cookieName: optional("SESSION_COOKIE_NAME", "mb_session"), maxAge: Number(optional("SESSION_MAX_AGE", String(60 * 60 * 24 * 7))), secure: process.env.NODE_ENV === "production", diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts index 0f199df..6c82d83 100644 --- a/packages/auth/src/index.ts +++ b/packages/auth/src/index.ts @@ -2,6 +2,7 @@ export { getAuthConfig, getSessionConfig, getOtpConfig, + isGoogleSignInEnabled, type AuthConfig, type SessionConfig, type OtpConfig, @@ -29,6 +30,10 @@ export { buildLogoutUrl, ensureUser, findUserByEmail, + isUserEnabled, + setUserEnabled, + logoutUser, + deleteUser, resetCaches, type Pkce, type TokenSet, @@ -48,3 +53,4 @@ export { type VerifyResult, type VerifyFailure, } from "./otp"; +export { isSessionActive } from "./session-state"; diff --git a/packages/auth/src/keycloak.ts b/packages/auth/src/keycloak.ts index 58f7a91..aca8fe5 100644 --- a/packages/auth/src/keycloak.ts +++ b/packages/auth/src/keycloak.ts @@ -354,10 +354,57 @@ export async function ensureUser( return created.id; } +// ── User status ────────────────────────────────────────────────────────────── +// Sessions outlive a Keycloak disable or delete, so requests re-check the user. +// The answer is cached briefly per replica; when Keycloak cannot be reached the +// user is treated as enabled, and the database checks still apply. + +const USER_STATUS_TTL_MS = 5 * 60 * 1000; +const userStatus = new Map(); + +export async function isUserEnabled(sub: string, now = Date.now()): Promise { + const cached = userStatus.get(sub); + if (cached && cached.until > now) return cached.enabled; + let enabled: boolean; + try { + const res = await adminFetch(`/users/${encodeURIComponent(sub)}`); + if (res.status === 404) enabled = false; + else if (!res.ok) throw new Error(`Keycloak user lookup failed (${res.status})`); + else enabled = ((await res.json()) as { enabled?: boolean }).enabled !== false; + } catch (error) { + console.warn("[auth] could not check Keycloak user status; allowing:", (error as Error).message); + return true; + } + userStatus.set(sub, { enabled, until: now + USER_STATUS_TTL_MS }); + return enabled; +} + +/** Enables or disables a Keycloak user, ending their Keycloak sessions on disable. */ +export async function setUserEnabled(sub: string, enabled: boolean): Promise { + const res = await adminFetch(`/users/${encodeURIComponent(sub)}`, { method: "PUT", body: JSON.stringify({ enabled }) }); + if (!res.ok) throw new Error(`Keycloak user update failed (${res.status})`); + if (!enabled) await logoutUser(sub); + userStatus.delete(sub); +} + +/** Deletes the Keycloak user (account erasure). Missing users are fine. */ +export async function deleteUser(sub: string): Promise { + const res = await adminFetch(`/users/${encodeURIComponent(sub)}`, { method: "DELETE" }); + if (!res.ok && res.status !== 404) throw new Error(`Keycloak user deletion failed (${res.status})`); + userStatus.delete(sub); +} + +/** Ends the user's Keycloak (SSO) sessions, so Google sign-in asks again. */ +export async function logoutUser(sub: string): Promise { + const res = await adminFetch(`/users/${encodeURIComponent(sub)}/logout`, { method: "POST" }); + if (!res.ok && res.status !== 404) throw new Error(`Keycloak logout failed (${res.status})`); +} + /** Clear cached discovery/tokens. Exposed for tests. */ export function resetCaches(): void { discoveryCache = null; adminToken = null; + userStatus.clear(); jwks = null; jwksUri = null; } diff --git a/packages/auth/src/session-state.ts b/packages/auth/src/session-state.ts new file mode 100644 index 0000000..5357573 --- /dev/null +++ b/packages/auth/src/session-state.ts @@ -0,0 +1,16 @@ +import { attendees } from "@match-maker/db"; +import { isUserEnabled } from "./keycloak"; +import type { Session } from "./session"; + +/** + * Whether a cryptographically valid session may still be used. The cookie is + * self-contained, so this is what makes disabling an account, "sign out + * everywhere", and disabling or deleting the Keycloak user take effect on the + * next request instead of when the cookie expires. + */ +export async function isSessionActive(session: Session): Promise { + const account = await attendees.accountState(session.attendeeId); + if (!account || account.disabled_at) return false; + if (account.sessions_revoked_at && session.iat * 1000 < account.sessions_revoked_at.getTime()) return false; + return isUserEnabled(session.sub); +} diff --git a/packages/auth/src/session.ts b/packages/auth/src/session.ts index 34bb64b..7284099 100644 --- a/packages/auth/src/session.ts +++ b/packages/auth/src/session.ts @@ -34,6 +34,20 @@ function deriveKey(secret: string): Uint8Array { return new Uint8Array(createHash("sha256").update(secret).digest()); } +/** Decrypts with the current secret, then with each previous one (rotation). */ +async function decrypt(value: string, options: { issuer: string; audience: string }) { + const config = getSessionConfig(); + let failure: unknown; + for (const secret of [config.secret, ...config.previousSecrets]) { + try { + return await jwtDecrypt(value, deriveKey(secret), options); + } catch (error) { + failure = error; + } + } + throw failure; +} + /** Encrypt a session into a compact JWE suitable for a cookie value. */ export async function sealSession( session: Omit @@ -52,8 +66,7 @@ export async function sealSession( export async function unsealSession(value: string): Promise { if (!value) return null; try { - const config = getSessionConfig(); - const { payload } = await jwtDecrypt(value, deriveKey(config.secret), { + const { payload } = await decrypt(value, { issuer: ISSUER, audience: AUDIENCE, }); @@ -125,8 +138,7 @@ export async function unsealOAuthState( ): Promise { if (!value) return null; try { - const config = getSessionConfig(); - const { payload } = await jwtDecrypt(value, deriveKey(config.secret), { + const { payload } = await decrypt(value, { issuer: ISSUER, audience: "match-maker-oauth", }); diff --git a/packages/db/migrations/0006_invitations.sql b/packages/db/migrations/0006_invitations.sql new file mode 100644 index 0000000..b5daed9 --- /dev/null +++ b/packages/db/migrations/0006_invitations.sql @@ -0,0 +1,18 @@ +-- Invited account lifecycle per event. +-- +-- The roster row is the invitation. These columns record whether the +-- invitation e-mail went out (and why it failed), when the attendee first +-- signed in to the event (claimed), and whether the organizer removed their +-- access. A revoked row keeps its history but grants nothing: no sign-in to +-- the event, no matching, no appearance in anyone's results. + +alter table event_participants add column if not exists invited_at timestamptz; +alter table event_participants add column if not exists invite_count integer not null default 0; +alter table event_participants add column if not exists invite_error text; +alter table event_participants add column if not exists claimed_at timestamptz; +alter table event_participants add column if not exists revoked_at timestamptz; + +-- Anyone who already saved a profile has evidently claimed their invitation. +update event_participants + set claimed_at = profile_updated_at + where claimed_at is null and profile_updated_at is not null; diff --git a/packages/db/migrations/0007_account_state.sql b/packages/db/migrations/0007_account_state.sql new file mode 100644 index 0000000..92d0485 --- /dev/null +++ b/packages/db/migrations/0007_account_state.sql @@ -0,0 +1,10 @@ +-- Account-wide access, independent of any one event. +-- +-- disabled_at the account may not sign in or use any session +-- sessions_revoked_at sessions issued before this moment are no longer valid +-- (sign out everywhere), without disabling the account +-- +-- Session cookies are self-contained, so every request checks these columns. + +alter table registered_attendees add column if not exists disabled_at timestamptz; +alter table registered_attendees add column if not exists sessions_revoked_at timestamptz; diff --git a/packages/db/migrations/0008_profile_form_version.sql b/packages/db/migrations/0008_profile_form_version.sql new file mode 100644 index 0000000..c6da017 --- /dev/null +++ b/packages/db/migrations/0008_profile_form_version.sql @@ -0,0 +1,8 @@ +-- Which version of the event's profile form a profile was last saved under. +-- +-- Organizers can change field labels, visibility and required fields +-- (events.form_config). Existing profiles stay valid and keep matching; this +-- lets the profile page ask someone saved under an older form to fill in +-- fields that have since become required. + +alter table event_participants add column if not exists profile_form_version integer not null default 0; diff --git a/packages/db/migrations/0009_pair_explanations.sql b/packages/db/migrations/0009_pair_explanations.sql new file mode 100644 index 0000000..04b5467 --- /dev/null +++ b/packages/db/migrations/0009_pair_explanations.sql @@ -0,0 +1,19 @@ +-- Cached one-sentence reasons for a viewer -> candidate pair. +-- +-- The key covers the event, both people (in that direction), both profile +-- revisions, the model, prompt and scoring versions, and the query context, +-- so a changed profile or query never reuses stale reasoning. Only reasons +-- are cached here, never a shortlist order. + +create table if not exists pair_explanations ( + cache_key text primary key, + event_id uuid not null references events(id) on delete cascade, + viewer_id uuid not null, + candidate_id uuid not null, + reason text not null, + expires_at timestamptz not null, + created_at timestamptz not null default now() +); + +create index if not exists idx_pair_explanations_expires on pair_explanations (expires_at); +create index if not exists idx_pair_explanations_people on pair_explanations (event_id, candidate_id); diff --git a/packages/db/migrations/0010_audit_events.sql b/packages/db/migrations/0010_audit_events.sql new file mode 100644 index 0000000..d9c0983 --- /dev/null +++ b/packages/db/migrations/0010_audit_events.sql @@ -0,0 +1,17 @@ +-- Security audit records: who did what to whom, for sensitive actions +-- (roster imports, invitations, access changes, settings, contact reveals, +-- account administration). Details hold counts and field names, never +-- contact data. Kept for a year by the housekeeping sweep. + +create table if not exists audit_events ( + id bigserial primary key, + event_id uuid references events(id) on delete set null, + actor_id uuid, + action text not null, + subject_id uuid, + details jsonb not null default '{}'::jsonb, + created_at timestamptz not null default now() +); + +create index if not exists idx_audit_events_event on audit_events (event_id, created_at desc); +create index if not exists idx_audit_events_created on audit_events (created_at); diff --git a/packages/db/migrations/0011_shared_retrieval.sql b/packages/db/migrations/0011_shared_retrieval.sql new file mode 100644 index 0000000..82de180 --- /dev/null +++ b/packages/db/migrations/0011_shared_retrieval.sql @@ -0,0 +1,42 @@ +-- Shared search retrieval across web replicas. +-- +-- cache_locks: short-lived ownership of one expensive computation (a query +-- embedding or a retrieval), so concurrent identical searches on different +-- replicas compute it once. An expired lock can be taken over; only its +-- owner can release it. +-- +-- events.corpus_revision: bumped whenever an event's participants change +-- (added, removed, profile/visibility/access/embedding updated). It is part +-- of every retrieval cache key, so a change sends new searches to a fresh +-- entry instead of serving candidates retrieved from the old corpus. + +create table if not exists cache_locks ( + lock_key text primary key, + owner text not null, + expires_at timestamptz not null +); + +alter table events add column if not exists corpus_revision bigint not null default 0; + +create or replace function bump_corpus_revision() returns trigger language plpgsql as $$ +begin + if tg_op = 'DELETE' then + update events set corpus_revision = corpus_revision + 1 where id in (select distinct event_id from old_rows); + else + update events set corpus_revision = corpus_revision + 1 where id in (select distinct event_id from new_rows); + end if; + return null; +end $$; + +-- One bump per statement and event, so a 10,000-row import is a single update. +drop trigger if exists event_participants_corpus_insert on event_participants; +create trigger event_participants_corpus_insert after insert on event_participants + referencing new table as new_rows for each statement execute function bump_corpus_revision(); + +drop trigger if exists event_participants_corpus_update on event_participants; +create trigger event_participants_corpus_update after update on event_participants + referencing new table as new_rows for each statement execute function bump_corpus_revision(); + +drop trigger if exists event_participants_corpus_delete on event_participants; +create trigger event_participants_corpus_delete after delete on event_participants + referencing old table as old_rows for each statement execute function bump_corpus_revision(); diff --git a/packages/db/migrations/0012_durable_jobs.sql b/packages/db/migrations/0012_durable_jobs.sql new file mode 100644 index 0000000..7d3c5b6 --- /dev/null +++ b/packages/db/migrations/0012_durable_jobs.sql @@ -0,0 +1,34 @@ +-- Durable, revision-aware background matching. +-- +-- profile_revision goes up on every profile save. Jobs carry the revision +-- they must reflect, and embeddings or match lists are written only while the +-- profile is still at that revision, so a slow job never replaces newer work. +-- +-- One job per person and kind may be *pending*; a new edit updates it +-- instead of waiting behind a running one. Running jobs hold a lease; a +-- crashed worker's job is reclaimed when the lease expires. Retryable +-- failures go back to pending with a backoff; after the last attempt a job +-- is 'failed' for good, with its last error kept for inspection. +-- +-- trigger_kind tells a job caused by the person's own profile change +-- ('profile') from a refresh because someone else changed ('refresh'); only +-- the first kind asks for others to be refreshed, so refreshes never cascade. + +alter table event_participants add column if not exists profile_revision bigint not null default 0; + +alter table matching_jobs add column if not exists revision bigint not null default 0; +alter table matching_jobs add column if not exists lease_until timestamptz; +alter table matching_jobs add column if not exists last_error text; +alter table matching_jobs add column if not exists trigger_kind text not null default 'profile'; +alter table matching_jobs drop constraint if exists matching_jobs_trigger_kind_check; +alter table matching_jobs add constraint matching_jobs_trigger_kind_check check (trigger_kind in ('profile', 'refresh')); + +-- Before this migration 'failed' also meant "will retry"; those rows are pending now. +update matching_jobs set status = 'pending' where status = 'failed' and attempts < 5 + and not exists (select 1 from matching_jobs p where p.event_id = matching_jobs.event_id and p.participant_id = matching_jobs.participant_id + and p.job_type = matching_jobs.job_type and p.status = 'pending'); + +drop index if exists idx_matching_jobs_one_active; +create unique index if not exists idx_matching_jobs_one_pending + on matching_jobs (event_id, participant_id, job_type) where status = 'pending'; +create index if not exists idx_matching_jobs_leases on matching_jobs (lease_until) where status = 'running'; diff --git a/packages/db/migrations/0013_model_gateway.sql b/packages/db/migrations/0013_model_gateway.sql new file mode 100644 index 0000000..91fac31 --- /dev/null +++ b/packages/db/migrations/0013_model_gateway.sql @@ -0,0 +1,22 @@ +-- Model gateway accounting and versioned embeddings. +-- +-- model_usage: one row per provider call (provider and model, operation, +-- outcome, latency, approximate tokens) for cost and reliability reports. +-- Kept 90 days by the housekeeping sweep. +-- +-- event_participants.embedding_model: which provider/model produced the +-- stored vector. Vectors from different models live in different spaces and +-- are never compared; a profile embedded by another model is embedded again. + +create table if not exists model_usage ( + id bigserial primary key, + provider text not null, + operation text not null, + outcome text not null check (outcome in ('ok', 'error', 'timeout', 'rejected')), + latency_ms integer not null, + input_chars integer not null default 0, + created_at timestamptz not null default now() +); +create index if not exists idx_model_usage_created on model_usage (created_at); + +alter table event_participants add column if not exists embedding_model text; diff --git a/packages/db/migrations/0014_outbox_location_requests.sql b/packages/db/migrations/0014_outbox_location_requests.sql new file mode 100644 index 0000000..4cbc7e0 --- /dev/null +++ b/packages/db/migrations/0014_outbox_location_requests.sql @@ -0,0 +1,48 @@ +-- Reliable notifications and venue location requests. +-- +-- notification_outbox: written in the same transaction as the interaction it +-- announces (an intro request, an acceptance, a location request or reply), +-- then delivered by the worker with retries. dedupe_key makes each +-- notification unique, so retries and replays never send duplicates. +-- Sent rows are swept after 30 days. +-- +-- location_requests: "where are you?" between two accepted connections. The +-- recipient may reply with a configured venue zone or decline; the reply is +-- visible only to the person who asked, for 15 minutes. Pending requests +-- expire after 30 minutes. Rows are swept after a day. + +create table if not exists notification_outbox ( + id bigserial primary key, + event_id uuid references events(id) on delete cascade, + recipient_id uuid not null, + kind text not null, + subject_id uuid not null, + dedupe_key text not null unique, + status text not null default 'pending' check (status in ('pending', 'running', 'sent', 'failed')), + attempts integer not null default 0, + available_at timestamptz not null default now(), + lease_until timestamptz, + last_error text, + created_at timestamptz not null default now(), + sent_at timestamptz +); +create index if not exists idx_notification_outbox_due on notification_outbox (available_at) where status in ('pending', 'running'); + +create table if not exists location_requests ( + id uuid primary key default gen_random_uuid(), + event_id uuid not null references events(id) on delete cascade, + intro_id uuid not null references intro_requests(id) on delete cascade, + requester_id uuid not null, + recipient_id uuid not null, + status text not null default 'pending' check (status in ('pending', 'answered', 'declined')), + zone text, + created_at timestamptz not null default now(), + expires_at timestamptz not null, + answered_at timestamptz, + zone_expires_at timestamptz, + check (requester_id <> recipient_id) +); +-- One open request per direction of a connection. +create unique index if not exists idx_location_requests_one_pending + on location_requests (intro_id, requester_id) where status = 'pending'; +create index if not exists idx_location_requests_people on location_requests (event_id, recipient_id, created_at desc); diff --git a/packages/db/migrations/0015_rerank_orders.sql b/packages/db/migrations/0015_rerank_orders.sql new file mode 100644 index 0000000..f3e50eb --- /dev/null +++ b/packages/db/migrations/0015_rerank_orders.sql @@ -0,0 +1,20 @@ +-- The model's order for one exact shortlist. +-- +-- pair_explanations holds reasons per pair, never an order. When every pair of +-- a shortlist is cached the model is not asked again, and without its earlier +-- order the list fell back to score order, so a refresh that changed nothing +-- reshuffled a stored list. The key is built from every pair key in the set, +-- so an order is reused only for the same viewer, the same candidates at the +-- same revisions, and the same model, prompt, scoring and context. + +create table if not exists rerank_orders ( + cache_key text primary key, + event_id uuid not null references events(id) on delete cascade, + viewer_id uuid not null, + candidate_ids uuid[] not null, + expires_at timestamptz not null, + created_at timestamptz not null default now() +); + +create index if not exists idx_rerank_orders_expires on rerank_orders (expires_at); +create index if not exists idx_rerank_orders_candidates on rerank_orders using gin (candidate_ids); diff --git a/packages/db/src/client.ts b/packages/db/src/client.ts index 1a49db3..40c6aaa 100644 --- a/packages/db/src/client.ts +++ b/packages/db/src/client.ts @@ -1,4 +1,5 @@ import pg from "pg"; +import { readFileSync } from "node:fs"; import type { ConnectionOptions as TlsOptions } from "node:tls"; const { Pool } = pg; @@ -38,8 +39,6 @@ export function getPool(): pg.Pool { pool = new Pool({ connectionString, - // RDS terminates TLS with an Amazon-issued CA that is not in Node's default - // trust store. Verification is enabled only when a CA bundle is supplied. ssl: resolveSsl(connectionString), max: Number(process.env.DATABASE_POOL_MAX ?? 10), idleTimeoutMillis: 30_000, @@ -56,18 +55,36 @@ export function getPool(): pg.Pool { return pool; } -function resolveSsl(connectionString: string): TlsOptions | false { - if (process.env.DATABASE_SSL === "disable") return false; - - // Local docker-compose Postgres has no TLS at all. - const isLocal = - connectionString.includes("@localhost") || - connectionString.includes("@127.0.0.1") || - connectionString.includes("@postgres:"); - if (isLocal && process.env.DATABASE_SSL !== "require") return false; +/** + * TLS for a connection, from DATABASE_SSL: + * disable plain connection (local docker-compose Postgres has no TLS) + * require encrypted, certificate not checked + * verify encrypted and the server certificate is checked against + * DATABASE_CA_CERT (PEM text) or DATABASE_CA_CERT_FILE (a path); + * production images ship the RDS bundle for this + * Unset: off for local hosts, otherwise verify when a CA is configured and + * require when not. Production startup refuses `require` for remote hosts. + */ +export function resolveSsl(connectionString: string, env: NodeJS.ProcessEnv = process.env): TlsOptions | false { + const mode = env.DATABASE_SSL; + if (mode === "disable") return false; + if (!mode && isLocalDatabase(connectionString)) return false; + + const ca = env.DATABASE_CA_CERT || (env.DATABASE_CA_CERT_FILE ? readFileSync(env.DATABASE_CA_CERT_FILE, "utf8") : ""); + if (mode === "verify" && !ca) { + throw new Error("DATABASE_SSL=verify needs DATABASE_CA_CERT or DATABASE_CA_CERT_FILE"); + } + if (mode === "verify" || (!mode && ca)) return { ca, rejectUnauthorized: true }; + return { rejectUnauthorized: false }; +} - const ca = process.env.DATABASE_CA_CERT; - return ca ? { ca, rejectUnauthorized: true } : { rejectUnauthorized: false }; +/** Local docker-compose or developer databases, which have no TLS. */ +export function isLocalDatabase(connectionString: string): boolean { + try { + return ["localhost", "127.0.0.1", "postgres"].includes(new URL(connectionString).hostname); + } catch { + return false; + } } /** Run a parameterised query. Never interpolate user input into `text`. */ diff --git a/packages/db/src/ensure-database.ts b/packages/db/src/ensure-database.ts index 0ffeeea..a61ab19 100644 --- a/packages/db/src/ensure-database.ts +++ b/packages/db/src/ensure-database.ts @@ -10,6 +10,7 @@ import "dotenv/config"; import pg from "pg"; import { isMainModule } from "./is-main"; +import { resolveSsl } from "./client"; /** * Connect to the `postgres` maintenance database rather than the target, since @@ -32,10 +33,7 @@ export async function ensureDatabase(name: string): Promise { const client = new pg.Client({ connectionString: maintenanceUrl(databaseUrl), - ssl: - process.env.DATABASE_SSL === "disable" - ? false - : { rejectUnauthorized: false }, + ssl: resolveSsl(databaseUrl), }); await client.connect(); diff --git a/packages/db/src/ensure-roles.ts b/packages/db/src/ensure-roles.ts new file mode 100644 index 0000000..15388da --- /dev/null +++ b/packages/db/src/ensure-roles.ts @@ -0,0 +1,148 @@ +/** + * Create or update the least-privilege database logins. The migration task runs + * this as the master user, before migrations: + * + * runtime web and worker: read and write application rows; no DDL, and no + * access to other databases + * keycloak owns the Keycloak database and nothing in the application one + * + * The master login is then used only by migrations. Passwords come from the + * task's secrets, and re-running updates them, so rotation is a redeploy. + * + * Usage: tsx packages/db/src/ensure-roles.ts + * DATABASE_URL master connection to the application database + * DATABASE_RUNTIME_USER / _PASSWORD runtime login (skipped when unset) + * KEYCLOAK_DB_USER / _PASSWORD Keycloak login (skipped when unset) + * KEYCLOAK_DB_NAME Keycloak database (default "keycloak") + */ +import "dotenv/config"; +import pg from "pg"; +import { isMainModule } from "./is-main"; +import { resolveSsl } from "./client"; + +export interface RoleSpec { + runtime?: { user: string; password: string }; + keycloak?: { user: string; password: string; database: string }; +} + +const IDENTIFIER = /^[a-z_][a-z0-9_]{0,62}$/; + +export async function ensureRoles(databaseUrl: string, spec: RoleSpec): Promise { + for (const name of [spec.runtime?.user, spec.keycloak?.user, spec.keycloak?.database]) { + if (name !== undefined && !IDENTIFIER.test(name)) throw new Error(`Invalid role or database name: ${name}`); + } + + const app = await connect(databaseUrl); + try { + if (spec.runtime) await ensureRuntime(app, spec.runtime.user, spec.runtime.password, new URL(databaseUrl).pathname.slice(1)); + if (spec.keycloak) await ensureLogin(app, spec.keycloak.user, spec.keycloak.password); + } finally { + await app.end(); + } + + if (spec.keycloak) { + const url = new URL(databaseUrl); + url.pathname = `/${spec.keycloak.database}`; + const identity = await connect(url.toString()); + try { + await ensureKeycloakOwner(identity, spec.keycloak.user, spec.keycloak.database); + } finally { + await identity.end(); + } + } +} + +async function connect(connectionString: string): Promise { + const client = new pg.Client({ connectionString, ssl: resolveSsl(connectionString) }); + await client.connect(); + return client; +} + +async function ensureLogin(client: pg.Client, user: string, password: string): Promise { + const role = client.escapeIdentifier(user); + const { rowCount } = await client.query(`select 1 from pg_roles where rolname = $1`, [user]); + if (!rowCount) await client.query(`create role ${role} login`); + await client.query(`alter role ${role} with login nosuperuser nocreatedb nocreaterole password ${client.escapeLiteral(password)}`); + console.log(`[ensure-roles] login ${user} ready`); +} + +async function ensureRuntime(client: pg.Client, user: string, password: string, database: string): Promise { + await ensureLogin(client, user, password); + const role = client.escapeIdentifier(user); + const db = client.escapeIdentifier(database); + for (const statement of [ + // Only named logins may connect to the application database. + `revoke connect on database ${db} from public`, + `grant connect on database ${db} to ${role}`, + `grant usage on schema public to ${role}`, + `grant select, insert, update, delete on all tables in schema public to ${role}`, + `grant usage, select on all sequences in schema public to ${role}`, + // Tables and sequences that later migrations create (as this master login). + `alter default privileges in schema public grant select, insert, update, delete on tables to ${role}`, + `alter default privileges in schema public grant usage, select on sequences to ${role}`, + ]) await client.query(statement); + console.log(`[ensure-roles] ${user} can read and write ${database} rows, without DDL`); +} + +/** + * Makes the Keycloak login own its database and every object in it, including + * tables an earlier deploy created as the master user. Objects are handed over + * one by one: REASSIGN OWNED would also move databases the master owns. + */ +async function ensureKeycloakOwner(client: pg.Client, user: string, database: string): Promise { + const role = client.escapeIdentifier(user); + const db = client.escapeIdentifier(database); + // A non-superuser master (RDS) may only transfer ownership to roles it belongs to. + await client.query(`grant ${role} to current_user`).catch(() => {}); + await client.query(`alter database ${db} owner to ${role}`); + await client.query(`revoke connect on database ${db} from public`); + await client.query(`grant connect on database ${db} to ${role}`); + await client.query( + `do $$ + declare item record; + begin + for item in + select 'table' as kind, format('%I.%I', schemaname, tablename) as name from pg_tables + where schemaname = 'public' and tableowner = current_user + union all + select 'view', format('%I.%I', schemaname, viewname) from pg_views + where schemaname = 'public' and viewowner = current_user + union all + select 'sequence', format('%I.%I', schemaname, sequencename) from pg_sequences + where schemaname = 'public' and sequenceowner = current_user + loop + begin + execute format('alter %s %s owner to %s', item.kind, item.name, ${client.escapeLiteral(role)}); + exception when others then + -- Sequences owned by a column move with their table. + null; + end; + end loop; + end $$` + ); + console.log(`[ensure-roles] ${user} owns database ${database}`); +} + +function pair(user: string | undefined, password: string | undefined, label: string) { + if (!user && !password) return undefined; + if (!user || !password) throw new Error(`${label} user and password must be set together`); + return { user, password }; +} + +if (isMainModule(import.meta.url)) { + const databaseUrl = process.env.DATABASE_URL; + if (!databaseUrl) { + console.error("[ensure-roles] DATABASE_URL is not set"); + process.exit(1); + } + const runtime = pair(process.env.DATABASE_RUNTIME_USER, process.env.DATABASE_RUNTIME_PASSWORD, "DATABASE_RUNTIME"); + const keycloakLogin = pair(process.env.KEYCLOAK_DB_USER, process.env.KEYCLOAK_DB_PASSWORD, "KEYCLOAK_DB"); + const keycloak = keycloakLogin && { ...keycloakLogin, database: process.env.KEYCLOAK_DB_NAME || "keycloak" }; + if (!runtime && !keycloak) console.log("[ensure-roles] no logins configured; nothing to do"); + ensureRoles(databaseUrl, { runtime, keycloak }) + .then(() => process.exit(0)) + .catch((err) => { + console.error("[ensure-roles] failed:", err.message); + process.exit(1); + }); +} diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index 8d37dd8..ba2aebe 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -12,3 +12,7 @@ export * as matches from "./repositories/matches"; export * as events from "./repositories/events"; export * as matchingJobs from "./repositories/matching-jobs"; export * as housekeeping from "./repositories/housekeeping"; +export * as audit from "./repositories/audit"; +export * as modelUsage from "./repositories/model-usage"; +export * as outbox from "./repositories/outbox"; +export * as pipelineHealth from "./repositories/pipeline-health"; diff --git a/packages/db/src/repositories/attendees.ts b/packages/db/src/repositories/attendees.ts index 383a7df..fa51f0d 100644 --- a/packages/db/src/repositories/attendees.ts +++ b/packages/db/src/repositories/attendees.ts @@ -62,6 +62,55 @@ export async function findIdsByEmails(emails: string[]): Promise { + const { rows } = await query<{ disabled_at: Date | null; sessions_revoked_at: Date | null }>( + `select disabled_at, sessions_revoked_at from registered_attendees where id = $1`, + [id] + ); + return rows[0] ?? null; +} + +/** Disables or re-enables an account; disabling also ends its sessions. Returns false when unknown. */ +export async function setDisabled(email: string, disabled: boolean): Promise { + const { rowCount } = await query( + `update registered_attendees + set disabled_at = case when $2 then coalesce(disabled_at, now()) else null end, + sessions_revoked_at = case when $2 then now() else sessions_revoked_at end, + updated_at = now() + where email = $1`, + [normalizeEmail(email), disabled] + ); + return (rowCount ?? 0) > 0; +} + +/** Ends every session issued so far ("sign out everywhere"). Returns false when unknown. */ +export async function revokeSessions(email: string): Promise { + const { rowCount } = await query( + `update registered_attendees set sessions_revoked_at = now(), updated_at = now() where email = $1`, + [normalizeEmail(email)] + ); + return (rowCount ?? 0) > 0; +} + +/** + * Erases a person for a deletion request: their attendee row and everything + * that cascades from it (event participation, matches, introductions, login + * challenges), plus cached explanations and login challenges by e-mail. + * Returns the erased id, or null when the e-mail is unknown. + */ +export async function erase(email: string): Promise { + const normalized = normalizeEmail(email); + const { rows } = await query<{ id: string }>(`select id from registered_attendees where email = $1`, [normalized]); + const id = rows[0]?.id; + if (!id) return null; + await query(`delete from pair_explanations where viewer_id = $1 or candidate_id = $1`, [id]); + await query(`delete from rerank_orders where viewer_id = $1 or $1::uuid = any(candidate_ids)`, [id]); + await query(`delete from login_challenges where email = $1`, [normalized]); + await query(`delete from registered_attendees where id = $1`, [id]); + return id; +} + /** True when the e-mail is on the attendee roster — the gate for every login. */ export async function isRegisteredEmail(email: string): Promise { const { rows } = await query<{ exists: boolean }>( diff --git a/packages/db/src/repositories/audit.ts b/packages/db/src/repositories/audit.ts new file mode 100644 index 0000000..e433f61 --- /dev/null +++ b/packages/db/src/repositories/audit.ts @@ -0,0 +1,43 @@ +import { query } from "../client"; + +export interface AuditEntry { + eventId?: string | null; + actorId?: string | null; + action: string; + subjectId?: string | null; + /** Counts and field names only, never contact data. */ + details?: Record; +} + +export interface AuditRecord { + id: string; + action: string; + actor_name: string | null; + subject_name: string | null; + details: Record; + created_at: Date; +} + +/** Audit records are kept this long, then swept. */ +export const AUDIT_RETENTION_DAYS = 365; + +export async function record(entry: AuditEntry): Promise { + await query( + `insert into audit_events (event_id, actor_id, action, subject_id, details) values ($1, $2, $3, $4, $5::jsonb)`, + [entry.eventId ?? null, entry.actorId ?? null, entry.action, entry.subjectId ?? null, JSON.stringify(entry.details ?? {})] + ); +} + +export async function listForEvent(eventId: string, limit = 20): Promise { + const { rows } = await query( + `select e.id::text as id, e.action, actor.name as actor_name, subject.name as subject_name, e.details, e.created_at + from audit_events e + left join registered_attendees actor on actor.id = e.actor_id + left join registered_attendees subject on subject.id = e.subject_id + where e.event_id = $1 + order by e.created_at desc, e.id desc + limit $2`, + [eventId, limit] + ); + return rows; +} diff --git a/packages/db/src/repositories/events.ts b/packages/db/src/repositories/events.ts index 55e88db..db60177 100644 --- a/packages/db/src/repositories/events.ts +++ b/packages/db/src/repositories/events.ts @@ -35,11 +35,37 @@ export async function findOrganization(slug: string): Promise<{ id: string; name return rows[0] ?? null; } -export async function listForOrganization(orgSlug: string): Promise { - const { rows } = await query(`${EVENT_SELECT} where o.slug = $1 order by e.created_at desc`, [orgSlug]); +/** + * The organization's events. With a viewer, only those they may open: + * everything for organization owners and admins, otherwise their own events. + */ +export async function listForOrganization(orgSlug: string, viewerId?: string): Promise { + const { rows } = await query( + `${EVENT_SELECT} + where o.slug = $1 + and ($2::uuid is null + or exists (select 1 from organization_members m + where m.organization_id = o.id and m.attendee_id = $2 and m.role in ('owner', 'admin')) + or exists (select 1 from event_organizers eo where eo.event_id = e.id and eo.attendee_id = $2)) + order by e.created_at desc`, + [orgSlug, viewerId ?? null] + ); return rows; } +export type OrganizerRole = "owner" | "admin" | "organizer"; + +/** The attendee's role on the event and in its organization (null when none). */ +export async function organizerRoles(eventId: string, attendeeId: string): Promise<{ event: OrganizerRole | null; organization: OrganizerRole | null }> { + const { rows } = await query<{ event_role: OrganizerRole | null; organization_role: OrganizerRole | null }>( + `select (select role from event_organizers where event_id = $1 and attendee_id = $2) as event_role, + (select m.role from organization_members m join events e on e.organization_id = m.organization_id + where e.id = $1 and m.attendee_id = $2) as organization_role`, + [eventId, attendeeId] + ); + return { event: rows[0]?.event_role ?? null, organization: rows[0]?.organization_role ?? null }; +} + export async function create(input: { organizationId: string; organizerId: string; @@ -78,7 +104,10 @@ export async function update(input: { }): Promise { const { rows } = await query( `${EVENT_SELECT} - where e.id = $1 and exists (select 1 from event_organizers eo where eo.event_id = e.id and eo.attendee_id = $2) + where e.id = $1 + and (exists (select 1 from event_organizers eo where eo.event_id = e.id and eo.attendee_id = $2) + or exists (select 1 from organization_members m + where m.organization_id = e.organization_id and m.attendee_id = $2 and m.role in ('owner', 'admin'))) limit 1`, [input.eventId, input.organizerId] ); @@ -97,23 +126,29 @@ export async function update(input: { return findBySlug(old.organization_slug, old.slug); } +/** Name and public slug, for notification text and links. */ +export async function basics(eventId: string): Promise<{ name: string; slug: string } | null> { + const { rows } = await query<{ name: string; slug: string }>(`select name, slug from events where id = $1`, [eventId]); + return rows[0] ?? null; +} + /** The event's status, or null when the event no longer exists. */ export async function statusOf(eventId: string): Promise { const { rows } = await query<{ status: EventRecord["status"] }>(`select status from events where id = $1`, [eventId]); return rows[0]?.status ?? null; } +/** Whether the attendee may open the event's organizer pages (any event role, or organization owner/admin). */ export async function isOrganizer(eventId: string, attendeeId: string): Promise { - const { rows } = await query<{ allowed: boolean }>( - `select exists(select 1 from event_organizers where event_id = $1 and attendee_id = $2) as allowed`, - [eventId, attendeeId] - ); - return rows[0]?.allowed ?? false; + const roles = await organizerRoles(eventId, attendeeId); + return roles.event !== null || roles.organization === "owner" || roles.organization === "admin"; } export async function isParticipant(eventId: string, attendeeId: string): Promise { const { rows } = await query<{ allowed: boolean }>( - `select exists(select 1 from event_participants where event_id = $1 and attendee_id = $2) as allowed`, + `select exists( + select 1 from event_participants where event_id = $1 and attendee_id = $2 and revoked_at is null + ) as allowed`, [eventId, attendeeId] ); return rows[0]?.allowed ?? false; @@ -131,7 +166,8 @@ export async function hasOrganizationAccess(organizationSlug: string, attendeeId export async function participantIds(eventId: string): Promise { const { rows } = await query<{ attendee_id: string }>( - `select attendee_id from event_participants where event_id = $1 and profile_updated_at is not null`, [eventId] + `select attendee_id from event_participants + where event_id = $1 and profile_updated_at is not null and revoked_at is null`, [eventId] ); return rows.map((row) => row.attendee_id); } @@ -155,9 +191,174 @@ export async function roster(eventId: string): Promise { + const { rows } = await query( + `select a.id as attendee_id, a.email, a.name + from event_participants p join registered_attendees a on a.id = p.attendee_id + where p.event_id = $1 and p.revoked_at is null and p.claimed_at is null and a.email is not null + and ($2::uuid is null and p.invited_at is null + or a.id = $2::uuid and (p.invited_at is null or p.invited_at < now() - make_interval(mins => $3))) + order by a.name nulls last`, + [eventId, attendeeId, resendAfterMinutes] + ); + return rows; +} + +/** Records one delivery attempt; a failure keeps the last successful send time. */ +export async function recordInvitation(eventId: string, attendeeId: string, error: string | null): Promise { + await query( + `update event_participants + set invited_at = case when $3::text is null then now() else invited_at end, + invite_count = invite_count + case when $3::text is null then 1 else 0 end, + invite_error = $3, updated_at = now() + where event_id = $1 and attendee_id = $2`, + [eventId, attendeeId, error] + ); +} + +/** The first signed-in visit to the event claims the invitation. */ +export async function markClaimed(eventId: string, attendeeId: string): Promise { + await query( + `update event_participants set claimed_at = now() + where event_id = $1 and attendee_id = $2 and claimed_at is null and revoked_at is null`, + [eventId, attendeeId] + ); +} + +/** Removes or restores an attendee's access; returns false when they are not on the roster. */ +export async function setRevoked(eventId: string, attendeeId: string, revoked: boolean): Promise { + const { rowCount } = await query( + `update event_participants + set revoked_at = case when $3 then coalesce(revoked_at, now()) else null end, updated_at = now() + where event_id = $1 and attendee_id = $2`, + [eventId, attendeeId, revoked] + ); + // Explanations written about or for a removed attendee are not kept. + if (revoked && rowCount) { + await query(`delete from pair_explanations where event_id = $1 and (viewer_id = $2 or candidate_id = $2)`, [eventId, attendeeId]); + await query(`delete from rerank_orders where event_id = $1 and (viewer_id = $2 or $2::uuid = any(candidate_ids))`, [eventId, attendeeId]); + } + return (rowCount ?? 0) > 0; +} + +/** Whether an attendee is on the roster, and if so whether access was removed. */ +export async function rosterState(eventId: string, attendeeId: string): Promise<"active" | "revoked" | null> { + const { rows } = await query<{ revoked: boolean }>( + `select revoked_at is not null as revoked from event_participants where event_id = $1 and attendee_id = $2`, + [eventId, attendeeId] + ); + if (!rows[0]) return null; + return rows[0].revoked ? "revoked" : "active"; +} + +export interface RosterRow { + name: string | null; + email: string; + phone: string | null; + about_me: string | null; + designation: string | null; + domain: string | null; + self_description: string | null; + years_of_experience: number | null; + most_impressive_build: string | null; +} + +export interface RosterImportResult { + /** People not known before this import. */ + created: number; + /** People newly added to this event (created or already known). */ + attached: number; + /** People already on this event's roster. */ + alreadyOnRoster: number; + /** Known people whose existing details differ from the sheet and were kept. */ + keptExisting: number; + /** New people whose phone already belongs to someone else; imported without it. */ + phoneConflicts: number; +} + +const DETAIL_COLUMNS = ["name", "phone", "about_me", "designation", "domain", "self_description", "years_of_experience", "most_impressive_build"] as const; + +/** + * Imports a validated roster in one transaction, so a failure leaves nothing + * half-applied and retrying the same file is safe. + * + * People are identified by e-mail and shared across organizations, so an + * import never replaces anyone's existing name, phone or details: it only + * fills fields that are empty. A phone that already belongs to someone else + * is left off the new person instead of failing the whole import. + */ +export async function importRoster(eventId: string, rows: RosterRow[]): Promise { + return transaction(async (client) => { + await client.query(`select pg_advisory_xact_lock(hashtext('roster-import:' || $1))`, [eventId]); + const result: RosterImportResult = { created: 0, attached: 0, alreadyOnRoster: 0, keptExisting: 0, phoneConflicts: 0 }; + if (!rows.length) return result; + + const emails = rows.map((row) => row.email); + const phones = rows.map((row) => row.phone).filter((phone): phone is string => Boolean(phone)); + const { rows: known } = await client.query( + `select id, ${DETAIL_COLUMNS.join(", ")}, email from registered_attendees + where email = any($1::text[]) or phone = any($2::text[])`, + [emails, phones] + ); + const byEmail = new Map(known.filter((row) => row.email).map((row) => [row.email, row])); + const phoneOwner = new Map(known.filter((row) => row.phone).map((row) => [row.phone!, row.email])); + + for (const row of rows) { + const existing = byEmail.get(row.email); + if (existing) { + if (DETAIL_COLUMNS.some((column) => row[column] != null && existing[column] != null && String(row[column]) !== String(existing[column]))) { + result.keptExisting += 1; + } + const freePhone = row.phone && !existing.phone && !phoneOwner.has(row.phone) ? row.phone : null; + await client.query( + `update registered_attendees set + ${DETAIL_COLUMNS.map((column, index) => `${column} = coalesce(${column}, $${index + 2})`).join(", ")}, + updated_at = now() + where id = $1`, + [existing.id, ...DETAIL_COLUMNS.map((column) => column === "phone" ? freePhone : row[column])] + ); + if (freePhone) phoneOwner.set(freePhone, row.email); + continue; + } + const phoneTaken = Boolean(row.phone && phoneOwner.has(row.phone)); + if (phoneTaken) result.phoneConflicts += 1; + const columns = ["email", ...DETAIL_COLUMNS]; + await client.query( + `insert into registered_attendees (${columns.join(", ")}) values (${columns.map((_, index) => `$${index + 1}`).join(", ")})`, + [row.email, ...DETAIL_COLUMNS.map((column) => column === "phone" && phoneTaken ? null : row[column])] + ); + if (row.phone && !phoneTaken) phoneOwner.set(row.phone, row.email); + result.created += 1; + } + + const { rows: attached } = await client.query<{ attached: boolean }>( + `insert into event_participants (event_id, attendee_id) + select $1, a.id from registered_attendees a where a.email = any($2::text[]) + on conflict (event_id, attendee_id) do nothing + returning true as attached`, + [eventId, emails] + ); + result.attached = attached.length; + result.alreadyOnRoster = new Set(emails).size - attached.length; + return result; + }); +} diff --git a/packages/db/src/repositories/housekeeping.ts b/packages/db/src/repositories/housekeeping.ts index ec8e6fc..bfd1822 100644 --- a/packages/db/src/repositories/housekeeping.ts +++ b/packages/db/src/repositories/housekeeping.ts @@ -1,5 +1,8 @@ import { query } from "../client"; import { deleteExpired as deleteExpiredChallenges } from "./login-challenges"; +import { AUDIT_RETENTION_DAYS } from "./audit"; +import { USAGE_RETENTION_DAYS } from "./model-usage"; +import { SENT_RETENTION_DAYS } from "./outbox"; /** How long a dead row is kept before the sweep removes it. */ export const RETENTION = { @@ -13,6 +16,8 @@ export interface SweepResult { loginChallenges: number; rateLimitBuckets: number; retrievalCache: number; + pairExplanations: number; + auditEvents: number; } /** Deletes rows that can no longer be used. Safe to run on any schedule. */ @@ -23,5 +28,16 @@ export async function sweepExpired(): Promise { [RETENTION.rateLimitBucketHours] ); const cache = await query(`delete from retrieval_cache where expires_at < now()`); - return { loginChallenges, rateLimitBuckets: buckets.rowCount ?? 0, retrievalCache: cache.rowCount ?? 0 }; + const explanations = await query(`delete from pair_explanations where expires_at < now()`); + await query(`delete from rerank_orders where expires_at < now()`); + await query(`delete from cache_locks where expires_at < now()`); + await query(`delete from model_usage where created_at < now() - make_interval(days => $1)`, [USAGE_RETENTION_DAYS]); + await query(`delete from notification_outbox where status = 'sent' and sent_at < now() - make_interval(days => $1)`, [SENT_RETENTION_DAYS]); + // Venue locations are only meaningful during the event day. + await query(`delete from location_requests where created_at < now() - interval '1 day'`); + const audits = await query(`delete from audit_events where created_at < now() - make_interval(days => $1)`, [AUDIT_RETENTION_DAYS]); + return { + loginChallenges, rateLimitBuckets: buckets.rowCount ?? 0, retrievalCache: cache.rowCount ?? 0, + pairExplanations: explanations.rowCount ?? 0, auditEvents: audits.rowCount ?? 0, + }; } diff --git a/packages/db/src/repositories/matching-jobs.ts b/packages/db/src/repositories/matching-jobs.ts index 2f5a4c8..749dc83 100644 --- a/packages/db/src/repositories/matching-jobs.ts +++ b/packages/db/src/repositories/matching-jobs.ts @@ -6,39 +6,69 @@ export interface MatchingJob { participant_id: string; attempts: number; job_type: "embed" | "match"; + /** The profile revision this job must reflect. */ + revision: number; + trigger_kind: "profile" | "refresh"; } -/** Matches STUCK_AFTER_MS in apps/web/src/domain/matching/match-status.ts. */ -const STUCK_AFTER_MINUTES = 10; +/** Attempts before a job fails for good. */ +export const MAX_ATTEMPTS = 5; +/** How long a claimed job may run before another worker may take it over. */ +export const LEASE_SECONDS = 300; -export async function enqueue(eventId: string, participantId: string, jobType: "embed" | "match" = "match"): Promise { - // A job "running" past the stuck limit belongs to a crashed worker and would - // block the one-active-job index forever; release it so this request queues. - await query( - `update matching_jobs set status = 'failed' - where event_id = $1 and participant_id = $2 and job_type = $3 and status = 'running' - and started_at < now() - make_interval(mins => $4)`, - [eventId, participantId, jobType, STUCK_AFTER_MINUTES] - ); - await query( - `insert into matching_jobs (event_id, participant_id, job_type) values ($1, $2, $3) - on conflict (event_id, participant_id, job_type) where status in ('pending', 'running') do nothing`, - [eventId, participantId, jobType] +/** + * Queues work for a participant. At most one job per person and kind is + * pending: a newer request updates it (latest revision, earliest start, and + * "profile" wins over "refresh") instead of being dropped, and a running job + * no longer blocks a new one. Without a revision, the participant's current + * one is used. + */ +export async function enqueue( + eventId: string, + participantId: string, + jobType: "embed" | "match" = "match", + options: { revision?: number; trigger?: "profile" | "refresh"; delaySeconds?: number } = {}, + client: { query: typeof query } = { query } +): Promise { + await client.query( + `insert into matching_jobs (event_id, participant_id, job_type, revision, trigger_kind, available_at) + values ($1, $2, $3, + coalesce($4::bigint, (select profile_revision from event_participants where event_id = $1 and attendee_id = $2), 0), + $5, now() + make_interval(secs => $6)) + on conflict (event_id, participant_id, job_type) where status = 'pending' do update + set revision = greatest(matching_jobs.revision, excluded.revision), + available_at = least(matching_jobs.available_at, excluded.available_at), + trigger_kind = case when matching_jobs.trigger_kind = 'profile' then 'profile' else excluded.trigger_kind end`, + [eventId, participantId, jobType, options.revision ?? null, options.trigger ?? "profile", options.delaySeconds ?? 0] ); } -/** Claim one row safely when multiple worker replicas run at once. */ -export async function claim(jobType: "embed" | "match"): Promise { +/** + * Claims the oldest due job of a kind, safely across worker replicas: a + * pending job whose time has come, or a running one whose lease expired + * (its worker crashed). A crashed job already at its last attempt fails + * for good instead of being retried forever. + */ +export async function claim(jobType: "embed" | "match", onlyEventId: string | null = null): Promise { return transaction(async (client) => { + await client.query( + `update matching_jobs set status = 'failed', lease_until = null, last_error = coalesce(last_error, 'worker stopped responding') + where job_type = $1 and status = 'running' and lease_until < now() and attempts >= $2 + and ($3::uuid is null or event_id = $3)`, + [jobType, MAX_ATTEMPTS, onlyEventId] + ); const { rows } = await client.query( `with next_job as ( select id from matching_jobs - where job_type = $1 and status in ('pending', 'failed') and available_at <= now() and attempts < 5 - order by created_at asc for update skip locked limit 1 + where job_type = $1 and attempts < $2 and ($4::uuid is null or event_id = $4) + and ((status = 'pending' and available_at <= now()) or (status = 'running' and lease_until < now())) + order by available_at asc, created_at asc for update skip locked limit 1 ) - update matching_jobs j set status = 'running', attempts = attempts + 1, started_at = now() + update matching_jobs j set status = 'running', attempts = attempts + 1, started_at = now(), + lease_until = now() + make_interval(secs => $3) from next_job where j.id = next_job.id - returning j.id, j.event_id, j.participant_id, j.attempts, j.job_type`, [jobType] + returning j.id::int as id, j.event_id, j.participant_id, j.attempts, j.job_type, j.revision::int as revision, j.trigger_kind`, + [jobType, MAX_ATTEMPTS, LEASE_SECONDS, onlyEventId] ); return rows[0] ?? null; }); @@ -76,15 +106,87 @@ export async function latestForEvent(eventId: string): Promise { } export async function complete(id: number): Promise { - await query(`update matching_jobs set status = 'done' where id = $1`, [id]); + await query(`update matching_jobs set status = 'done', lease_until = null where id = $1`, [id]); } -export async function fail(id: number, retrySeconds = 30): Promise { +/** + * After someone's own profile change is processed, queues refreshes for the + * people it affects: those whose saved matches include them, and those whose + * list is not full (the person may now belong in it). Refreshes are delayed so + * a burst of changes coalesces into one run per person, and they never ask + * for further refreshes. Returns how many people were queued. + */ +export async function enqueueRefreshes(eventId: string, changedId: string, options: { delaySeconds: number; fullList: number }): Promise { + const { rowCount } = await query( + `insert into matching_jobs (event_id, participant_id, job_type, revision, trigger_kind, available_at) + select $1, p.attendee_id, 'match', p.profile_revision, 'refresh', now() + make_interval(secs => $3) + from event_participants p + where p.event_id = $1 and p.attendee_id <> $2 and p.profile_updated_at is not null and p.revoked_at is null + and (exists (select 1 from event_matches m where m.event_id = $1 and m.participant_id = p.attendee_id and m.candidate_id = $2) + or (select count(*) from event_matches m where m.event_id = $1 and m.participant_id = p.attendee_id) < $4) + on conflict (event_id, participant_id, job_type) where status = 'pending' do update + set revision = greatest(matching_jobs.revision, excluded.revision), + available_at = least(matching_jobs.available_at, excluded.available_at)`, + [eventId, changedId, options.delaySeconds, options.fullList] + ); + return rowCount ?? 0; +} + +/** Delay before retry `attempt` (1-based): 30s, 60s, 120s, ... capped at 15 minutes. */ +export function retryDelaySeconds(attempt: number): number { + return Math.min(30 * 2 ** Math.max(0, attempt - 1), 15 * 60); +} + +/** + * Records a failed attempt. Before the last attempt the job goes back to + * pending after a backoff; after it, the job is failed for good. If a newer + * job for the same person is already pending, this one is superseded. + */ +export async function fail(id: number, error: string): Promise { await query( - `update matching_jobs - set status = case when attempts >= 5 then 'failed' else 'pending' end, - available_at = now() + ($2 || ' seconds')::interval - where id = $1`, - [id, String(retrySeconds)] + `update matching_jobs j + set status = case + when exists (select 1 from matching_jobs p where p.event_id = j.event_id and p.participant_id = j.participant_id + and p.job_type = j.job_type and p.status = 'pending' and p.id <> j.id) then 'done' + when j.attempts >= $3 then 'failed' + else 'pending' end, + available_at = now() + make_interval(secs => least(30 * power(2, greatest(j.attempts - 1, 0)), 900)), + lease_until = null, + last_error = left($2, 1000) + where j.id = $1`, + [id, error, MAX_ATTEMPTS] + ); +} + +export interface FailedJob { + id: number; + event_slug: string; + email: string | null; + job_type: string; + attempts: number; + last_error: string | null; + started_at: Date | null; +} + +/** Jobs that failed for good, newest first (operator inspection). */ +export async function listFailed(limit = 50): Promise { + const { rows } = await query( + `select j.id::int as id, e.slug as event_slug, a.email, j.job_type, j.attempts, j.last_error, j.started_at + from matching_jobs j join events e on e.id = j.event_id left join registered_attendees a on a.id = j.participant_id + where j.status = 'failed' order by j.started_at desc nulls last limit $1`, + [limit] + ); + return rows; +} + +/** Queues failed jobs again with fresh attempts (one id, or all). Returns how many. */ +export async function retryFailed(id: number | "all"): Promise { + const { rows } = await query<{ event_id: string; participant_id: string; job_type: "embed" | "match" }>( + `update matching_jobs set status = 'done', last_error = coalesce(last_error, '') || ' (retried)' + where status = 'failed' and ($1::bigint is null or id = $1) + returning event_id, participant_id, job_type`, + [id === "all" ? null : id] ); + for (const row of rows) await enqueue(row.event_id, row.participant_id, row.job_type); + return rows.length; } diff --git a/packages/db/src/repositories/model-usage.ts b/packages/db/src/repositories/model-usage.ts new file mode 100644 index 0000000..fe003f8 --- /dev/null +++ b/packages/db/src/repositories/model-usage.ts @@ -0,0 +1,42 @@ +import { query } from "../client"; + +/** Usage rows are kept this long, then swept. */ +export const USAGE_RETENTION_DAYS = 90; + +export async function record(entry: { + provider: string; + operation: string; + outcome: "ok" | "error" | "timeout" | "rejected"; + latencyMs: number; + inputChars: number; +}): Promise { + await query( + `insert into model_usage (provider, operation, outcome, latency_ms, input_chars) values ($1, $2, $3, $4, $5)`, + [entry.provider, entry.operation, entry.outcome, Math.round(entry.latencyMs), entry.inputChars] + ); +} + +export interface UsageSummary { + provider: string; + operation: string; + calls: number; + failures: number; + avg_latency_ms: number; + p95_latency_ms: number; + /** Rough token estimate (characters / 4). */ + approx_tokens: number; +} + +export async function summary(hours = 24): Promise { + const { rows } = await query( + `select provider, operation, count(*)::int as calls, + count(*) filter (where outcome <> 'ok')::int as failures, + round(avg(latency_ms))::int as avg_latency_ms, + round(percentile_cont(0.95) within group (order by latency_ms))::int as p95_latency_ms, + (sum(input_chars) / 4)::int as approx_tokens + from model_usage where created_at > now() - make_interval(hours => $1) + group by provider, operation order by provider, operation`, + [hours] + ); + return rows; +} diff --git a/packages/db/src/repositories/outbox.ts b/packages/db/src/repositories/outbox.ts new file mode 100644 index 0000000..ec6fa35 --- /dev/null +++ b/packages/db/src/repositories/outbox.ts @@ -0,0 +1,76 @@ +import { query, transaction } from "../client"; + +export type NotificationKind = "intro.requested" | "intro.accepted" | "location.requested" | "location.answered"; + +export interface OutboxItem { + id: number; + event_id: string | null; + recipient_id: string; + kind: NotificationKind; + subject_id: string; + attempts: number; +} + +export const MAX_ATTEMPTS = 5; +const LEASE_SECONDS = 120; +/** Sent notifications are kept this long, then swept. */ +export const SENT_RETENTION_DAYS = 30; + +/** + * Queues a notification. Pass the transaction's client so it commits or + * rolls back with the interaction it announces. A repeated dedupe key is + * ignored, so the same notification is never queued twice. + */ +export async function enqueue( + item: { eventId: string; recipientId: string; kind: NotificationKind; subjectId: string; dedupeKey: string }, + client: { query: typeof query } = { query } +): Promise { + await client.query( + `insert into notification_outbox (event_id, recipient_id, kind, subject_id, dedupe_key) + values ($1, $2, $3, $4, $5) on conflict (dedupe_key) do nothing`, + [item.eventId, item.recipientId, item.kind, item.subjectId, item.dedupeKey] + ); +} + +/** Claims the oldest due notification (or one whose sender crashed) for delivery. */ +export async function claim(onlyEventId: string | null = null): Promise { + return transaction(async (client) => { + await client.query( + `update notification_outbox set status = 'failed', last_error = coalesce(last_error, 'sender stopped responding') + where status = 'running' and lease_until < now() and attempts >= $1`, [MAX_ATTEMPTS]); + const { rows } = await client.query( + `with next_item as ( + select id from notification_outbox + where attempts < $1 and ($3::uuid is null or event_id = $3) + and ((status = 'pending' and available_at <= now()) or (status = 'running' and lease_until < now())) + order by available_at, id for update skip locked limit 1 + ) + update notification_outbox o set status = 'running', attempts = attempts + 1, lease_until = now() + make_interval(secs => $2) + from next_item where o.id = next_item.id + returning o.id::int as id, o.event_id, o.recipient_id, o.kind, o.subject_id, o.attempts`, + [MAX_ATTEMPTS, LEASE_SECONDS, onlyEventId] + ); + return rows[0] ?? null; + }); +} + +export async function markSent(id: number): Promise { + await query(`update notification_outbox set status = 'sent', sent_at = now(), lease_until = null where id = $1`, [id]); +} + +/** Retries after 30s, 60s, 120s... (capped at 15 minutes); fails for good after the last attempt. */ +export async function fail(id: number, error: string): Promise { + await query( + `update notification_outbox + set status = case when attempts >= $3 then 'failed' else 'pending' end, + available_at = now() + make_interval(secs => least(30 * power(2, greatest(attempts - 1, 0)), 900)), + lease_until = null, last_error = left($2, 1000) + where id = $1`, + [id, error, MAX_ATTEMPTS] + ); +} + +/** Nothing left to send (e.g. the interaction was deleted): close it without delivery. */ +export async function skip(id: number, reason: string): Promise { + await query(`update notification_outbox set status = 'sent', sent_at = now(), lease_until = null, last_error = $2 where id = $1`, [id, reason]); +} diff --git a/packages/db/src/repositories/pipeline-health.ts b/packages/db/src/repositories/pipeline-health.ts new file mode 100644 index 0000000..d5a86d2 --- /dev/null +++ b/packages/db/src/repositories/pipeline-health.ts @@ -0,0 +1,30 @@ +import { query } from "../client"; + +/** Backlog and failure counts of the background queues, for metrics and alarms. */ +export interface PipelineHealth { + jobsPending: number; + /** Age of the oldest job that is due but not picked up; 0 when none. */ + oldestDueJobSeconds: number; + /** Jobs that failed after every retry, in the last day. */ + jobsFailed: number; + notificationsPending: number; + oldestDueNotificationSeconds: number; + notificationsFailed: number; +} + +export async function snapshot(): Promise { + const { rows: [row] } = await query>( + `select + (select count(*) from matching_jobs where status = 'pending') as "jobsPending", + (select coalesce(extract(epoch from now() - min(available_at)), 0) from matching_jobs + where status = 'pending' and available_at <= now()) as "oldestDueJobSeconds", + (select count(*) from matching_jobs where status = 'failed' + and coalesce(started_at, created_at) > now() - interval '1 day') as "jobsFailed", + (select count(*) from notification_outbox where status = 'pending') as "notificationsPending", + (select coalesce(extract(epoch from now() - min(available_at)), 0) from notification_outbox + where status = 'pending' and available_at <= now()) as "oldestDueNotificationSeconds", + (select count(*) from notification_outbox where status = 'failed' + and created_at > now() - interval '1 day') as "notificationsFailed"` + ); + return Object.fromEntries(Object.entries(row).map(([key, value]) => [key, Math.round(Number(value))])) as unknown as PipelineHealth; +} diff --git a/packages/db/src/types.ts b/packages/db/src/types.ts index 2a835da..3ea58f1 100644 --- a/packages/db/src/types.ts +++ b/packages/db/src/types.ts @@ -10,6 +10,8 @@ export interface Attendee { self_description: string | null; years_of_experience: number | null; most_impressive_build: string | null; + disabled_at: Date | null; + sessions_revoked_at: Date | null; created_at: Date; updated_at: Date; } diff --git a/packages/db/workers/embed.worker.ts b/packages/db/workers/embed.worker.ts index 14f0673..01f68cc 100644 --- a/packages/db/workers/embed.worker.ts +++ b/packages/db/workers/embed.worker.ts @@ -3,8 +3,9 @@ import { getContainer } from "../../../apps/web/src/composition/container"; import { profileText } from "../../../apps/web/src/domain/participant/participant.entity"; import { eventAllows } from "../../../apps/web/src/domain/shared/event-lifecycle"; -export async function processEmbedJob(): Promise { - const job = await matchingJobs.claim("embed"); +/** `onlyEventId` limits work to one event (tests). */ +export async function processEmbedJob(onlyEventId: string | null = null): Promise { + const job = await matchingJobs.claim("embed", onlyEventId); if (!job) return false; try { const status = await events.statusOf(job.event_id); @@ -15,15 +16,21 @@ export async function processEmbedJob(): Promise { } const container = getContainer(); const profile = await container.participants.findProfile(job.event_id, job.participant_id); - if (profile) { - const embedding = await container.embeddings.embed(profileText(profile), "profile"); - if (embedding) await container.participants.setEmbedding(job.event_id, job.participant_id, embedding); + // A newer save queued its own embed job; this one has nothing left to do. + if (!profile || (profile.revision ?? 0) !== job.revision) { + await matchingJobs.complete(job.id); + return true; + } + const embedding = await container.embeddings.embed(profileText(profile), "profile"); + if (embedding && !(await container.participants.setEmbeddingAt(job.event_id, job.participant_id, embedding, job.revision, container.embeddings.id))) { + await matchingJobs.complete(job.id); + return true; } await matchingJobs.complete(job.id); - await matchingJobs.enqueue(job.event_id, job.participant_id, "match"); + await matchingJobs.enqueue(job.event_id, job.participant_id, "match", { revision: job.revision, trigger: job.trigger_kind }); } catch (error) { console.error(`[worker:embed] job ${job.id} failed`, error); - await matchingJobs.fail(job.id); + await matchingJobs.fail(job.id, error instanceof Error ? error.message : String(error)); } return true; } diff --git a/packages/db/workers/match.worker.ts b/packages/db/workers/match.worker.ts index 7eeec83..2e91e52 100644 --- a/packages/db/workers/match.worker.ts +++ b/packages/db/workers/match.worker.ts @@ -1,10 +1,16 @@ import { events, matchingJobs } from "../src/index"; import { getContainer } from "../../../apps/web/src/composition/container"; import { computeMatches } from "../../../apps/web/src/application/use-cases/compute-matches"; +import { StaleRevisionError } from "../../../apps/web/src/domain/matching/pipeline"; import { eventAllows } from "../../../apps/web/src/domain/shared/event-lifecycle"; +import { DEFAULT_MATCH_LIMIT } from "../../../apps/web/src/domain/matching/match-strategy"; -export async function processMatchJob(): Promise { - const job = await matchingJobs.claim("match"); +/** Seconds to wait before refreshing affected people, so bursts coalesce. */ +const REFRESH_DELAY_SECONDS = Number(process.env.MATCH_REFRESH_DELAY_SECONDS ?? 120); + +/** `onlyEventId` limits work to one event (tests). */ +export async function processMatchJob(onlyEventId: string | null = null): Promise { + const job = await matchingJobs.claim("match", onlyEventId); if (!job) return false; try { const status = await events.statusOf(job.event_id); @@ -14,11 +20,23 @@ export async function processMatchJob(): Promise { return true; } const { pipeline } = getContainer(); - await computeMatches({ eventId: job.event_id, participantId: job.participant_id }, pipeline); + await computeMatches({ eventId: job.event_id, participantId: job.participant_id, expectedRevision: job.revision }, pipeline); await matchingJobs.complete(job.id); + if (job.trigger_kind === "profile") { + const queued = await matchingJobs.enqueueRefreshes(job.event_id, job.participant_id, { + delaySeconds: Number.isFinite(REFRESH_DELAY_SECONDS) ? REFRESH_DELAY_SECONDS : 120, + fullList: DEFAULT_MATCH_LIMIT, + }); + if (queued) console.info(`[worker:match] job ${job.id}: refreshing ${queued} affected participant(s)`); + } } catch (error) { + if (error instanceof StaleRevisionError) { + // The profile moved on during this run; the newer job publishes instead. + await matchingJobs.complete(job.id); + return true; + } console.error(`[worker:match] job ${job.id} failed`, error); - await matchingJobs.fail(job.id); + await matchingJobs.fail(job.id, error instanceof Error ? error.message : String(error)); } return true; } diff --git a/packages/db/workers/metrics.worker.ts b/packages/db/workers/metrics.worker.ts new file mode 100644 index 0000000..b015713 --- /dev/null +++ b/packages/db/workers/metrics.worker.ts @@ -0,0 +1,31 @@ +import { pipelineHealth } from "../src/index"; +import type { Metrics } from "../../../apps/web/src/application/ports/metrics"; + +export const REPORT_EVERY_MS = 60 * 1000; + +let lastReport = 0; + +/** + * Reports queue backlog and failures once a minute. WorkerHeartbeat doubles + * as liveness: when it stops arriving, the worker is down. + */ +export async function reportHealthIfDue(metrics: Metrics, now = Date.now()): Promise { + if (now - lastReport < REPORT_EVERY_MS) return false; + lastReport = now; + try { + const health = await pipelineHealth.snapshot(); + metrics.record([ + { name: "WorkerHeartbeat", value: 1, unit: "Count" }, + { name: "JobsPending", value: health.jobsPending, unit: "Count" }, + { name: "OldestDueJobSeconds", value: health.oldestDueJobSeconds, unit: "Seconds" }, + { name: "JobsFailed", value: health.jobsFailed, unit: "Count" }, + { name: "NotificationsPending", value: health.notificationsPending, unit: "Count" }, + { name: "OldestDueNotificationSeconds", value: health.oldestDueNotificationSeconds, unit: "Seconds" }, + { name: "NotificationsFailed", value: health.notificationsFailed, unit: "Count" }, + ]); + } catch (error) { + // A missing heartbeat raises the alarm; the queues keep running. + console.error("[worker:metrics] could not read queue health", error); + } + return true; +} diff --git a/packages/db/workers/notification.worker.ts b/packages/db/workers/notification.worker.ts new file mode 100644 index 0000000..061844b --- /dev/null +++ b/packages/db/workers/notification.worker.ts @@ -0,0 +1,55 @@ +import { events, outbox, query } from "../src/index"; +import { getContainer } from "../../../apps/web/src/composition/container"; + +/** + * Delivers one queued notification. Details are read at send time, so the + * message reflects the current state; an interaction that no longer exists + * is closed without sending. Failures retry with backoff (see outbox.fail). + */ +export async function processNotification(onlyEventId: string | null = null): Promise { + const item = await outbox.claim(onlyEventId); + if (!item) return false; + try { + const delivered = await deliver(item); + if (delivered) await outbox.markSent(item.id); + else await outbox.skip(item.id, "nothing to send any more"); + } catch (error) { + console.error(`[worker:notify] ${item.kind} #${item.id} failed`, error); + await outbox.fail(item.id, error instanceof Error ? error.message : String(error)); + } + return true; +} + +async function deliver(item: outbox.OutboxItem): Promise { + if (!item.event_id) return false; + const { participants, notifications } = getContainer(); + const event = await events.basics(item.event_id); + const recipient = await participants.findProfile(item.event_id, item.recipient_id); + if (!event || !recipient?.email) return false; + + const base = (process.env.APP_BASE_URL || "").replace(/\/$/, ""); + if (item.kind === "intro.requested" || item.kind === "intro.accepted") { + const { rows: [intro] } = await query<{ requester_id: string; recipient_id: string; status: string }>( + `select requester_id, recipient_id, status from intro_requests where id = $1`, [item.subject_id]); + if (!intro || (item.kind === "intro.accepted" && intro.status !== "accepted")) return false; + const otherId = intro.requester_id === item.recipient_id ? intro.recipient_id : intro.requester_id; + const other = await participants.findProfile(item.event_id, otherId); + const target = { recipientEmail: recipient.email, recipientName: recipient.name, otherName: other?.name ?? "A participant", eventName: event.name, link: `${base}/e/${event.slug}/connections#intro-${item.subject_id}` }; + await (item.kind === "intro.requested" ? notifications.introRequested(target) : notifications.introAccepted(target)); + return true; + } + + const { rows: [request] } = await query<{ intro_id: string; requester_id: string; recipient_id: string; status: string; zone: string | null }>( + `select intro_id, requester_id, recipient_id, status, zone from location_requests where id = $1`, [item.subject_id]); + if (!request) return false; + const otherId = request.requester_id === item.recipient_id ? request.recipient_id : request.requester_id; + const other = await participants.findProfile(item.event_id, otherId); + const target = { recipientEmail: recipient.email, recipientName: recipient.name, otherName: other?.name ?? "A participant", eventName: event.name, link: `${base}/e/${event.slug}/connections#intro-${request.intro_id}` }; + if (item.kind === "location.requested") { + if (request.status !== "pending") return false; + await notifications.locationRequested(target); + } else { + await notifications.locationAnswered({ ...target, zone: request.status === "answered" ? request.zone : null }); + } + return true; +} diff --git a/packages/db/workers/worker.ts b/packages/db/workers/worker.ts index 5650e2d..28dacc4 100644 --- a/packages/db/workers/worker.ts +++ b/packages/db/workers/worker.ts @@ -4,6 +4,9 @@ import { closePool } from "../src/client"; import { processEmbedJob } from "./embed.worker"; import { processMatchJob } from "./match.worker"; import { sweepIfDue } from "./housekeeping.worker"; +import { processNotification } from "./notification.worker"; +import { reportHealthIfDue } from "./metrics.worker"; +import { getContainer } from "../../../apps/web/src/composition/container"; let stopping = false; process.on("SIGINT", () => { stopping = true; }); @@ -14,9 +17,11 @@ async function main() { console.info(`[worker] embedding and matching queue started (version ${process.env.APP_VERSION || "dev"})`); while (!stopping) { await sweepIfDue(); + await reportHealthIfDue(getContainer().metrics); const embedded = await processEmbedJob(); const matched = await processMatchJob(); - if (!embedded && !matched) await new Promise((resolve) => setTimeout(resolve, 1200)); + const notified = await processNotification(); + if (!embedded && !matched && !notified) await new Promise((resolve) => setTimeout(resolve, 1200)); } await closePool(); console.info("[worker] stopped"); diff --git a/packages/email/src/index.ts b/packages/email/src/index.ts index c48847d..461b011 100644 --- a/packages/email/src/index.ts +++ b/packages/email/src/index.ts @@ -1,4 +1,5 @@ export { sendLoginEmail, sendNotificationEmail, type SendResult } from "./client"; +export { renderInvitationEmail, type InvitationEmailInput } from "./templates/invitation-email"; export { renderLoginEmail, type LoginEmailInput, diff --git a/packages/email/src/templates/invitation-email.ts b/packages/email/src/templates/invitation-email.ts new file mode 100644 index 0000000..e9f6384 --- /dev/null +++ b/packages/email/src/templates/invitation-email.ts @@ -0,0 +1,34 @@ +/** The invitation an organizer sends to everyone on an event roster. */ +export interface InvitationEmailInput { + name: string | null; + eventName: string; + organizationName: string; + /** The event page, where the attendee signs in and claims the invitation. */ + eventUrl: string; +} + +export function renderInvitationEmail(input: InvitationEmailInput): { subject: string; html: string; text: string } { + const greeting = input.name ? `Hi ${input.name},` : "Hi,"; + const subject = `You're invited to ${input.eventName} on Mr. Bridge`; + const text = [ + greeting, + "", + `${input.organizationName} added you to ${input.eventName}. Sign in with this e-mail address to claim your invitation, complete your profile, and see who you should meet.`, + "", + input.eventUrl, + "", + "If you did not expect this, you can ignore this e-mail.", + ].join("\n"); + const html = `
+

Mr. Bridge

+

${escapeHtml(greeting)}

+

${escapeHtml(input.organizationName)} added you to ${escapeHtml(input.eventName)}. Sign in with this e-mail address to claim your invitation, complete your profile, and see who you should meet.

+

Claim your invitation

+

If you did not expect this, you can ignore this e-mail.

+
`; + return { subject, html, text }; +} + +function escapeHtml(value: string): string { + return value.replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[char]!); +} diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..89802d1 --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,30 @@ +import { defineConfig } from "@playwright/test"; + +/** + * Browser acceptance suite (tests/e2e). It drives an already running app and + * worker, started with EMAIL_DEV_FALLBACK=true and their output written to + * E2E_WEB_LOG, which is where the tests read sign-in codes: + * + * E2E_BASE_URL=http://localhost:3000 npx playwright test + * + * Set E2E_BROWSER_CHANNEL=chrome to use an installed Chrome instead of the + * bundled Chromium (`npx playwright install chromium`). + */ +export default defineConfig({ + testDir: "tests/e2e", + // The journey is one story told in order; parallel runs would share an event. + workers: 1, + fullyParallel: false, + forbidOnly: !!process.env.CI, + retries: 0, + timeout: 60_000, + expect: { timeout: 15_000 }, + reporter: process.env.CI ? [["list"], ["html", { open: "never", outputFolder: "playwright-report" }]] : "list", + outputDir: "test-results/e2e", + use: { + baseURL: process.env.E2E_BASE_URL ?? "http://localhost:3000", + channel: process.env.E2E_BROWSER_CHANNEL || undefined, + screenshot: "only-on-failure", + trace: "retain-on-failure", + }, +}); diff --git a/tests/e2e/edges.spec.ts b/tests/e2e/edges.spec.ts new file mode 100644 index 0000000..908f5f7 --- /dev/null +++ b/tests/e2e/edges.spec.ts @@ -0,0 +1,89 @@ +/** + * Sign-in edges and layout: phone lookup still proves e-mail ownership, + * magic links work once, a tampered cookie leads to a usable login page, + * signing out ends access, and pages fit a phone screen. + */ +import { devices, expect, test, type Page } from "@playwright/test"; +import { ORG, ORGANIZER, RUN, createEvent, enterCode, logOffset, loginMail, phone, rosterFile, sidewaysOverflow, signIn } from "./support"; + +const SLUG = `e2e-edges-${RUN}`; +const dana = { email: `e2e-dana-${RUN}@example.com`, phone: phone() }; +const eli = { email: `e2e-eli-${RUN}@example.com`, phone: phone() }; +const PHONE = { ...devices["Pixel 7"] }; +delete (PHONE as { defaultBrowserType?: string }).defaultBrowserType; + +test.describe.configure({ mode: "serial" }); + +test.describe("sign-in edges and phone layout", () => { + let organizer: Page; + + test.beforeAll(async ({ browser }) => { + organizer = await signIn(browser, ORGANIZER, `/orgs/${ORG}/events/new`, PHONE); + await createEvent(organizer, SLUG); + await organizer.goto(`/orgs/${ORG}/events/${SLUG}/roster`); + await organizer.locator("#field-file").setInputFiles(rosterFile([ + { Name: "Dana Edge", Email: dana.email, Phone: dana.phone }, + { Name: "Eli Edge", Email: eli.email, Phone: eli.phone }, + ])); + await organizer.getByRole("button", { name: "Import roster" }).click(); + await expect(organizer.getByRole("status").first()).toContainText("Added 2 attendee"); + }); + + test("organizer pages fit a phone and offer sign out", async () => { + for (const path of ["", "/roster", "/matching", "/analytics"]) { + await organizer.goto(`/orgs/${ORG}/events/${SLUG}${path}`); + expect(await sidewaysOverflow(organizer), `organizer${path || " dashboard"}`).toBeLessThanOrEqual(1); + await expect(organizer.getByRole("button", { name: "Sign out" })).toHaveCount(1); + } + }); + + test("phone sign-in shows only a masked e-mail, then reaches the event", async ({ browser }) => { + const page = await (await browser.newContext(PHONE)).newPage(); + const returnTo = `/e/${SLUG}/connections`; + await page.goto(`/login?returnTo=${encodeURIComponent(returnTo)}`); + await page.getByRole("button", { name: /Phone Number/ }).click(); + await page.locator('input[type="tel"]').fill(dana.phone); + const since = logOffset(); + await page.getByRole("button", { name: "Send sign-in code" }).click(); + await expect(page.getByText("Check your inbox")).toBeVisible(); + await expect(page.locator("body")).not.toContainText(dana.email); + await enterCode(page, dana.email, since, returnTo); + + for (const path of ["/connections", "/matches"]) { + await page.goto(`/e/${SLUG}${path}`); + expect(await sidewaysOverflow(page), path).toBeLessThanOrEqual(1); + } + + await page.getByRole("button", { name: "Sign out" }).click(); + await page.waitForURL("**/login**"); + await page.goto(`/e/${SLUG}/connections`); + await expect(page).toHaveURL(/\/login/); + }); + + test("a magic link signs in once and cannot be reused", async ({ browser, baseURL }) => { + const page = await (await browser.newContext()).newPage(); + const since = logOffset(); + const response = await page.request.post("/api/auth/otp/request", { + headers: { "content-type": "application/json", origin: baseURL! }, + data: { email: eli.email, returnTo: `/e/${SLUG}/profile` }, + }); + expect(response.ok()).toBe(true); + const { link } = await loginMail(eli.email, since); + const local = link!.replace(/^https?:\/\/[^/]+/, ""); + await page.goto(local); + await expect(page).toHaveURL(new RegExp(`/e/${SLUG}/profile`)); + + const other = await (await browser.newContext()).newPage(); + await other.goto(local); + await expect(other).toHaveURL(/\/login/); + }); + + test("a tampered session cookie leads to a usable login page", async ({ browser, baseURL }) => { + const context = await browser.newContext(); + await context.addCookies([{ name: "mb_session", value: "tampered.value", url: baseURL! }]); + const page = await context.newPage(); + await page.goto(`/e/${SLUG}/matches`); + await expect(page).toHaveURL(/\/login/); + await expect(page.getByRole("button", { name: /Email Address/ })).toBeVisible(); + }); +}); diff --git a/tests/e2e/journey.spec.ts b/tests/e2e/journey.spec.ts new file mode 100644 index 0000000..02dea78 --- /dev/null +++ b/tests/e2e/journey.spec.ts @@ -0,0 +1,129 @@ +/** + * The documented journey, in order: an organizer sets up an event and + * imports the roster; invited attendees sign in and complete profiles; the + * worker matches them; search finds people; an intro is requested and + * accepted; contacts appear only then; a venue question is asked and + * answered; a third attendee sees none of it; closing the event freezes it. + */ +import { expect, test, type Page } from "@playwright/test"; +import { ORG, ORGANIZER, RUN, createEvent, phone, requestCode, rosterFile, signIn } from "./support"; + +const SLUG = `e2e-${RUN}`; +const people = { + alice: { name: "Alice E2E", designation: "ML Engineer", domain: "Artificial Intelligence", about: "I build retrieval systems and LLM evaluation tooling for startups.", interests: "AI, search, startups", intent: "NETWORKING" }, + bob: { name: "Bob E2E", designation: "Product Designer", domain: "Design", about: "Designing AI assistants for climate and health teams.", interests: "AI, design, climate", intent: "NETWORKING" }, + carol: { name: "Carol E2E", designation: "Founder", domain: "Artificial Intelligence", about: "Founder hiring engineers for an AI search startup.", interests: "AI, hiring, search", intent: "HIRING" }, +}; +type Person = keyof typeof people; +const email = (key: Person | "stranger") => `e2e-${key}-${RUN}@example.com`; + +test.describe.configure({ mode: "serial" }); + +test.describe("organizer to meeting", () => { + let organizer: Page; + const pages = {} as Record; + + test("organizer creates the event and imports the roster", async ({ browser }) => { + organizer = await signIn(browser, ORGANIZER, `/orgs/${ORG}/events/new`); + await createEvent(organizer, SLUG); + await organizer.goto(`/orgs/${ORG}/events/${SLUG}/roster`); + const rows = (Object.keys(people) as Person[]).map((key) => ({ Name: people[key].name, Email: email(key), Phone: phone() })); + await organizer.locator("#field-file").setInputFiles(rosterFile([...rows, { Name: "Broken Row", Email: "not-an-email" }])); + await organizer.getByRole("button", { name: "Import roster" }).click(); + const status = organizer.getByRole("status").first(); + await expect(status).toContainText("Added 3 attendee"); + await expect(status).toContainText("Skipped 1 row"); + }); + + test("an uninvited e-mail cannot sign in", async ({ browser }) => { + const page = await (await browser.newContext()).newPage(); + expect((await requestCode(page, email("stranger"), `/e/${SLUG}/profile`)).outcome).toBe("unregistered"); + }); + + test("invited attendees sign in and complete their profiles", async ({ browser }) => { + for (const key of Object.keys(people) as Person[]) { + const page = pages[key] = await signIn(browser, email(key), `/e/${SLUG}/profile`); + const person = people[key]; + await page.getByLabel("Role or designation").fill(person.designation); + await page.getByLabel("Professional domain").fill(person.domain); + await page.getByLabel("About you").fill(person.about); + await page.getByLabel("Years of experience").fill("5"); + await page.getByLabel("Interests").fill(person.interests); + await page.getByLabel("What are you looking for?").selectOption(person.intent); + await page.getByLabel("Where can people find you?").selectOption("Main hall"); + await page.getByRole("button", { name: /Save profile/ }).click(); + await page.waitForURL(/\/matches\?saved=1/); + } + }); + + test("the worker matches the first person to save without a manual refresh", async () => { + const alice = pages.alice; + await expect(async () => { + await alice.goto(`/e/${SLUG}/matches`); + await expect(alice.locator("article", { hasText: people.bob.name }).getByRole("button", { name: "Request intro" })).toBeVisible({ timeout: 1000 }); + }).toPass({ timeout: 45_000, intervals: [1500] }); + }); + + test("search finds others and never the searcher", async () => { + const alice = pages.alice; + await alice.goto(`/e/${SLUG}/search`); + await alice.getByLabel("Describe who you are looking for").fill("designer working on climate"); + await alice.getByRole("button", { name: "Search" }).click(); + const results = alice.locator("article"); + await expect(results.filter({ hasText: people.bob.name })).toHaveCount(1); + await expect(results.filter({ hasText: people.alice.name })).toHaveCount(0); + }); + + test("an intro reveals contacts only after the recipient accepts", async () => { + const { alice, bob } = pages; + await alice.goto(`/e/${SLUG}/matches`); + await alice.locator("article", { hasText: people.bob.name }).getByRole("button", { name: "Request intro" }).click(); + await alice.waitForURL(/connections\?requested=1/); + await expect(alice.locator("main")).not.toContainText(email("bob")); + + await bob.goto(`/e/${SLUG}/connections`); + await bob.getByRole("button", { name: "Accept" }).first().click(); + await bob.waitForLoadState("networkidle"); + await alice.goto(`/e/${SLUG}/connections`); + await expect(alice.locator("main")).toContainText(email("bob")); + }); + + test("a venue question is answered voluntarily and only the asker sees the zone", async () => { + const { alice, bob, carol } = pages; + await alice.getByRole("button", { name: "Ask where they are" }).click(); + await alice.waitForURL(/asked=1/); + await expect(alice.locator("main")).toContainText("Waiting for a reply"); + + await bob.goto(`/e/${SLUG}/connections`); + await expect(bob.locator("main")).toContainText(`${people.alice.name} is asking where you are`); + await bob.getByLabel("Your venue zone").first().selectOption("Cafe"); + await bob.getByRole("button", { name: `Share with ${people.alice.name}` }).click(); + await bob.waitForURL(/replied=1/); + + await alice.goto(`/e/${SLUG}/connections`); + await expect(alice.locator("main")).toContainText("Replied: at Cafe"); + await carol.goto(`/e/${SLUG}/connections`); + const carolView = carol.locator("main"); + for (const hidden of [email("alice"), email("bob"), "Cafe"]) await expect(carolView).not.toContainText(hidden); + }); + + test("analytics counts the accepted intro", async () => { + await organizer.goto(`/orgs/${ORG}/events/${SLUG}/analytics`); + await expect(organizer.locator("main")).toContainText(/Accepted introductions\s*1/); + }); + + test("closing the event keeps matches readable and stops changes", async () => { + await organizer.goto(`/orgs/${ORG}/events/${SLUG}`); + await organizer.getByLabel("Registration status").selectOption("closed"); + await organizer.getByRole("button", { name: "Save event configuration" }).click(); + await expect(organizer.getByText("Event configuration saved")).toBeVisible(); + + const { alice } = pages; + await alice.goto(`/e/${SLUG}/matches`); + await expect(alice.locator("main")).toContainText("has closed"); + await expect(alice.locator("main")).toContainText(people.bob.name); + await expect(alice.getByRole("button", { name: "Request intro" })).toHaveCount(0); + await alice.goto(`/e/${SLUG}/profile`); + await expect(alice).toHaveURL(new RegExp(`/e/${SLUG}$`)); + }); +}); diff --git a/tests/e2e/support.ts b/tests/e2e/support.ts new file mode 100644 index 0000000..e842cb7 --- /dev/null +++ b/tests/e2e/support.ts @@ -0,0 +1,101 @@ +/** + * Helpers for the browser suite: sign in through the real login page with + * the code the app logged (EMAIL_DEV_FALLBACK), build roster sheets, and + * create events. Every run uses its own event and attendees. + */ +import { readFileSync, statSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { tmpdir } from "node:os"; +import { expect, type Browser, type BrowserContextOptions, type Page } from "@playwright/test"; +import pg from "pg"; +import * as XLSX from "xlsx"; +import "dotenv/config"; + +export const ORG = process.env.E2E_ORGANIZATION ?? "ai-community-lucknow"; +export const ORGANIZER = process.env.E2E_ORGANIZER_EMAIL ?? "organizer@example.com"; +const WEB_LOG = process.env.E2E_WEB_LOG ?? "node_modules/.cache/e2e/web.log"; + +export const RUN = Date.now().toString(36); +export const phone = () => `9${String(Math.floor(Math.random() * 1e9)).padStart(9, "0")}`; + +export async function db(sql: string, params: unknown[] = []): Promise { + const client = new pg.Client({ connectionString: process.env.DATABASE_URL }); + await client.connect(); + try { + return (await client.query(sql, params)).rows; + } finally { + await client.end(); + } +} + +/** Byte offset of the end of the app log; mail logged after it is new. */ +export const logOffset = () => statSync(WEB_LOG).size; + +/** The newest login e-mail the app logged for `email` after byte offset `since`. */ +export async function loginMail(email: string, since: number): Promise<{ code: string; link: string | null }> { + for (let attempt = 0; attempt < 40; attempt++) { + const log = readFileSync(WEB_LOG).subarray(since).toString("utf8"); + const at = log.lastIndexOf(`To: ${email}`); + const code = at >= 0 ? log.slice(at).match(/Code:\s+(\d{6})/)?.[1] : undefined; + if (code) return { code, link: log.slice(at).match(/Link:\s+(\S+)/)?.[1] ?? null }; + await new Promise((resolve) => setTimeout(resolve, 250)); + } + throw new Error(`no login e-mail was logged for ${email}; is the app writing to ${WEB_LOG}?`); +} + +/** Sends a code from the login page; resolves with what the page answered. */ +export async function requestCode(page: Page, email: string, returnTo: string): Promise<{ outcome: "sent" | "unregistered"; since: number }> { + await db(`delete from login_challenges where email = $1`, [email]); + await page.goto(`/login?returnTo=${encodeURIComponent(returnTo)}`); + await page.getByRole("button", { name: /Email Address/ }).click(); + await page.locator('input[type="email"]').fill(email); + const since = logOffset(); + await page.getByRole("button", { name: "Send sign-in code" }).click(); + const outcome = await Promise.race([ + page.getByText("Check your inbox").waitFor().then(() => "sent" as const), + page.getByText(/not on the attendee list/i).waitFor().then(() => "unregistered" as const), + ]); + return { outcome, since }; +} + +export async function enterCode(page: Page, email: string, since: number, returnTo: string) { + const { code } = await loginMail(email, since); + await page.getByLabel("Six-digit email verification code").fill(code); + await page.getByRole("button", { name: "Verify & Sign In" }).click(); + await page.waitForURL((url) => url.pathname === returnTo.split("?")[0]); +} + +/** A new browser session signed in as `email`, on `returnTo`. */ +export async function signIn(browser: Browser, email: string, returnTo: string, options: BrowserContextOptions = {}): Promise { + const context = await browser.newContext(options); + const page = await context.newPage(); + const { outcome, since } = await requestCode(page, email, returnTo); + expect(outcome, `${email} should be invited`).toBe("sent"); + await enterCode(page, email, since, returnTo); + return page; +} + +export function rosterFile(rows: Array>): string { + const book = XLSX.utils.book_new(); + XLSX.utils.book_append_sheet(book, XLSX.utils.json_to_sheet(rows), "Roster"); + const file = join(tmpdir(), `mr-bridge-roster-${RUN}-${rows.length}.xlsx`); + writeFileSync(file, XLSX.write(book, { type: "buffer", bookType: "xlsx" })); + return file; +} + +export async function createEvent(page: Page, slug: string, zones = "Main hall, Cafe") { + await page.goto(`/orgs/${ORG}/events/new`); + await page.getByLabel("Event name").fill(`E2E ${slug}`); + await page.getByLabel("URL slug").fill(slug); + await page.getByRole("button", { name: "Create event" }).click(); + await page.waitForURL(`**/orgs/${ORG}/events/${slug}`); + await page.getByLabel("Supported intents").fill("NETWORKING, HIRING, MENTORING"); + await page.getByLabel("Venue zones").fill(zones); + await page.getByLabel("Registration status").selectOption("open"); + await page.getByRole("button", { name: "Save event configuration" }).click(); + await expect(page.getByText("Event configuration saved")).toBeVisible(); +} + +/** How far the page scrolls sideways; more than a pixel means a broken phone layout. */ +export const sidewaysOverflow = (page: Page) => + page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth); diff --git a/tests/global-setup.ts b/tests/global-setup.ts new file mode 100644 index 0000000..2463312 --- /dev/null +++ b/tests/global-setup.ts @@ -0,0 +1,45 @@ +/** + * Integration suites skip themselves when their services are missing, so a + * bare `npm test` works anywhere. In CI that would let them pass silently: + * set REQUIRE_SERVICES (e.g. "db,keycloak") and the run fails up front + * unless every listed service is reachable. + */ +export default async function requireServices(): Promise { + const required = (process.env.REQUIRE_SERVICES ?? "").split(",").map((name) => name.trim()).filter(Boolean); + if (!required.length) return; + await import("dotenv/config"); + const problems: string[] = []; + + if (required.includes("db")) { + if (!process.env.DATABASE_URL) { + problems.push("DATABASE_URL is not set"); + } else { + const { default: pg } = await import("pg"); + const client = new pg.Client({ connectionString: process.env.DATABASE_URL }); + try { + await client.connect(); + await client.query("select 1"); + } catch (error) { + problems.push(`Postgres is unreachable: ${(error as Error).message}`); + } finally { + await client.end().catch(() => {}); + } + } + } + + if (required.includes("keycloak")) { + const issuer = (process.env.KEYCLOAK_INTERNAL_ISSUER || process.env.KEYCLOAK_ISSUER || "").replace(/\/$/, ""); + if (!issuer || !process.env.KEYCLOAK_ADMIN_PASSWORD) { + problems.push("KEYCLOAK_ISSUER and KEYCLOAK_ADMIN_PASSWORD must be set"); + } else { + const ok = await fetch(`${issuer}/.well-known/openid-configuration`, { signal: AbortSignal.timeout(5000) }).then((res) => res.ok).catch(() => false); + if (!ok) problems.push(`Keycloak is unreachable at ${issuer}`); + } + } + + const unknown = required.filter((name) => !["db", "keycloak"].includes(name)); + if (unknown.length) problems.push(`Unknown REQUIRE_SERVICES entries: ${unknown.join(", ")}`); + if (problems.length) { + throw new Error(`Required test services are not available, so integration suites would skip:\n - ${problems.join("\n - ")}`); + } +} diff --git a/tests/integration/account-revocation.test.ts b/tests/integration/account-revocation.test.ts new file mode 100644 index 0000000..9c60ff8 --- /dev/null +++ b/tests/integration/account-revocation.test.ts @@ -0,0 +1,77 @@ +/** + * Account disable, enable and "sign out everywhere" against the real database + * and the real Keycloak realm. Creates its own attendee and Keycloak user and + * deletes both afterwards. Skipped unless DATABASE_URL and a reachable + * Keycloak (KEYCLOAK_ISSUER plus admin credentials) are configured. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, query } from "@match-maker/db"; +import { ensureUser, isSessionActive, isUserEnabled, resetCaches } from "@match-maker/auth"; +import { migrate } from "../../packages/db/src/migrate"; +import { manageAccount } from "../../tools/manage-account"; + +const issuer = (process.env.KEYCLOAK_INTERNAL_ISSUER || process.env.KEYCLOAK_ISSUER || "").replace(/\/$/, ""); +const reachable = await fetch(`${issuer}/.well-known/openid-configuration`, { signal: AbortSignal.timeout(3000) }) + .then((res) => res.ok).catch(() => false); +const describeLive = process.env.DATABASE_URL && process.env.KEYCLOAK_ADMIN_PASSWORD && reachable ? describe : describe.skip; + +const EMAIL = `revocation-${Date.now().toString(36)}@example.com`; + +async function deleteKeycloakUser(sub: string) { + const [, server, realm] = issuer.match(/^(.*)\/realms\/([^/]+)$/)!; + const token = await fetch(`${server}/realms/master/protocol/openid-connect/token`, { + method: "POST", + body: new URLSearchParams({ grant_type: "password", client_id: "admin-cli", username: process.env.KEYCLOAK_ADMIN!, password: process.env.KEYCLOAK_ADMIN_PASSWORD! }), + }).then((res) => res.json()); + await fetch(`${server}/admin/realms/${realm}/users/${sub}`, { method: "DELETE", headers: { Authorization: `Bearer ${token.access_token}` } }); +} + +describeLive("account disable and session revocation", () => { + let sub: string; + let attendeeId: string; + const session = () => ({ sub, email: EMAIL, name: "Revocation Test", attendeeId, method: "email" as const, iat: Math.floor(Date.now() / 1000) - 5 }); + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany([{ name: "Revocation Test", email: EMAIL }]); + attendeeId = (await attendees.findByEmail(EMAIL))!.id; + sub = await ensureUser(EMAIL, "Revocation Test"); + }); + + afterAll(async () => { + if (sub) await deleteKeycloakUser(sub); + await query(`delete from registered_attendees where email = $1`, [EMAIL]); + await closePool(); + }); + + it("keeps one Keycloak identity for repeated sign-ins", async () => { + expect(await ensureUser(EMAIL, "Revocation Test")).toBe(sub); + await expect(isSessionActive(session())).resolves.toBe(true); + }); + + it("disables the account in the database and in Keycloak", async () => { + const existing = session(); + const report = await manageAccount("disable", EMAIL); + expect(report.join("\n")).toMatch(/keycloak: user disabled/); + resetCaches(); + await expect(isUserEnabled(sub)).resolves.toBe(false); + await expect(isSessionActive(existing)).resolves.toBe(false); + }); + + it("enables it again, without reviving sessions from before the disable", async () => { + const beforeDisable = { ...session(), iat: Math.floor(Date.now() / 1000) - 3600 }; + await manageAccount("enable", EMAIL); + resetCaches(); + await expect(isUserEnabled(sub)).resolves.toBe(true); + await expect(isSessionActive(beforeDisable)).resolves.toBe(false); + await expect(isSessionActive({ ...session(), iat: Math.floor(Date.now() / 1000) + 1 })).resolves.toBe(true); + }); + + it("signs out everywhere without disabling", async () => { + const existing = session(); + await new Promise((resolve) => setTimeout(resolve, 1100)); + await manageAccount("revoke-sessions", EMAIL); + await expect(isSessionActive(existing)).resolves.toBe(false); + expect((await attendees.accountState(attendeeId))!.disabled_at).toBeNull(); + }); +}); diff --git a/tests/integration/audit-retention.test.ts b/tests/integration/audit-retention.test.ts new file mode 100644 index 0000000..d9b8b61 --- /dev/null +++ b/tests/integration/audit-retention.test.ts @@ -0,0 +1,97 @@ +/** + * Audit records and data retention against a real Postgres (and the local + * Keycloak realm for erasure). Uses its own organization, event and people. + * Skipped when DATABASE_URL is unset; the erase case also needs Keycloak. + */ +import { randomUUID } from "node:crypto"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, audit, closePool, events, housekeeping, query } from "@match-maker/db"; +import { ensureUser, findUserByEmail } from "@match-maker/auth"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { manageAccount } from "../../tools/manage-account"; +import { Intent } from "../../apps/web/src/domain/participant/intent"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const issuer = (process.env.KEYCLOAK_INTERNAL_ISSUER || process.env.KEYCLOAK_ISSUER || "").replace(/\/$/, ""); +const keycloakUp = await fetch(`${issuer}/.well-known/openid-configuration`, { signal: AbortSignal.timeout(3000) }).then((res) => res.ok).catch(() => false); +const RUN = Date.now().toString(36); +const ORG = `audit-${RUN}`; +const email = (key: string) => `audit-${key}-${RUN}@example.com`; + +describeDb("audit records and retention", () => { + const participants = new PostgresParticipantRepository(); + const ids: Record = {}; + let eventId: string; + + const explain = (viewer: string, candidate: string) => query( + `insert into pair_explanations (cache_key, event_id, viewer_id, candidate_id, reason, expires_at) + values ($1, $2, $3, $4, 'reason', now() + interval '1 day')`, [randomUUID(), eventId, viewer, candidate]); + const explanationsAbout = async (id: string) => + (await query(`select 1 from pair_explanations where viewer_id = $1 or candidate_id = $1`, [id])).rows.length; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(["org", "kim", "lou", "max"].map((key) => ({ name: `Audit ${key}`, email: email(key) }))); + for (const key of ["org", "kim", "lou", "max"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, [ids.kim, ids.lou, ids.max]); + }); + + afterAll(async () => { + await query(`delete from audit_events where subject_id = any($1::uuid[]) or actor_id = any($1::uuid[])`, [Object.values(ids)]); + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`audit-%-${RUN}@example.com`]); + await closePool(); + }); + + it("records actions with the people involved, newest first", async () => { + await audit.record({ eventId, actorId: ids.org, action: "roster.imported", details: { attached: 3 } }); + await audit.record({ eventId, actorId: ids.org, action: "access.removed", subjectId: ids.kim }); + const recent = await audit.listForEvent(eventId); + expect(recent.map((row) => row.action)).toEqual(["access.removed", "roster.imported"]); + expect(recent[0]).toMatchObject({ actor_name: "Audit org", subject_name: "Audit kim" }); + expect(recent[1].details).toEqual({ attached: 3 }); + }); + + it("sweeps records older than the retention period", async () => { + await audit.record({ eventId, action: "old.action", subjectId: ids.lou }); + await query(`update audit_events set created_at = now() - interval '400 days' where action = 'old.action' and subject_id = $1`, [ids.lou]); + await housekeeping.sweepExpired(); + expect((await query(`select 1 from audit_events where action = 'old.action' and subject_id = $1`, [ids.lou])).rows).toHaveLength(0); + }); + + it("drops cached explanations about someone removed from the event", async () => { + await explain(ids.kim, ids.lou); + await explain(ids.lou, ids.kim); + await events.setRevoked(eventId, ids.kim, true); + expect(await explanationsAbout(ids.kim)).toBe(0); + }); + + it("drops explanations of a profile that is hidden", async () => { + await explain(ids.max, ids.lou); + await participants.saveProfile({ + eventId, attendeeId: ids.lou, designation: "Engineer", domain: "AI", aboutMe: "x", selfDescription: null, + yearsOfExperience: null, mostImpressiveBuild: null, intent: Intent.NETWORKING, interests: [], venueZone: null, visible: false, + }); + expect(await explanationsAbout(ids.lou)).toBe(0); + }); + + (keycloakUp && process.env.KEYCLOAK_ADMIN_PASSWORD ? it : it.skip)("erases a person, their event data and their Keycloak user", async () => { + await ensureUser(email("max"), "Audit max"); + await explain(ids.lou, ids.max); + const report = await manageAccount("erase", email("max")); + expect(report.join("\n")).toMatch(/keycloak: user deleted/); + expect(await attendees.findByEmail(email("max"))).toBeNull(); + expect((await query(`select 1 from event_participants where attendee_id = $1`, [ids.max])).rows).toHaveLength(0); + expect(await explanationsAbout(ids.max)).toBe(0); + expect(await findUserByEmail(email("max"))).toBeNull(); + expect((await query(`select 1 from audit_events where action = 'account.erased' and subject_id = $1`, [ids.max])).rows).toHaveLength(1); + }); + + it("keeps records when their event is deleted", async () => { + await query(`delete from events where id = $1`, [eventId]); + expect((await query(`select event_id from audit_events where action = 'roster.imported' and actor_id = $1`, [ids.org])).rows[0].event_id).toBeNull(); + }); +}); diff --git a/tests/integration/capacity-check.test.ts b/tests/integration/capacity-check.test.ts new file mode 100644 index 0000000..6d1b24b --- /dev/null +++ b/tests/integration/capacity-check.test.ts @@ -0,0 +1,44 @@ +/** + * A small run of the capacity check against a real Postgres with the free + * hash embedding: every attendee gets matches, searches succeed, and the + * throwaway event and people are removed afterwards. Skipped without DATABASE_URL. + */ +import { afterAll, describe, expect, it, vi } from "vitest"; + +const saved = vi.hoisted(() => { + const saved = { EMBEDDING_PROVIDER: process.env.EMBEDDING_PROVIDER, GEMINI_API_KEY: process.env.GEMINI_API_KEY, MATCH_REFRESH_DELAY_SECONDS: process.env.MATCH_REFRESH_DELAY_SECONDS }; + process.env.EMBEDDING_PROVIDER = "hash"; + process.env.GEMINI_API_KEY = ""; + process.env.MATCH_REFRESH_DELAY_SECONDS = "0"; + delete (globalThis as { __matchMakerContainer?: unknown }).__matchMakerContainer; + return saved; +}); + +import { closePool, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { budgetFailures, runCapacityCheck } from "../../tools/capacity-check"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; + +describeDb("capacity check", () => { + afterAll(async () => { + await closePool(); + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + delete (globalThis as { __matchMakerContainer?: unknown }).__matchMakerContainer; + }); + + it("measures a small event and cleans up after itself", async () => { + await migrate(); + const throwaway = async () => (await query(`select 1 from organizations where slug like 'capacity-%'`)).rows.length; + const before = await throwaway(); + const report = await runCapacityCheck({ attendees: 12, searches: 6, concurrency: 3, workers: 1 }); + expect(report).toMatchObject({ attendees: 12, searches: 6, attendeesWithoutMatches: 0 }); + expect(report.search.failures).toBe(0); + expect(report.search.p95Ms).toBeGreaterThan(0); + expect(budgetFailures(report)).toEqual([]); + expect(await throwaway()).toBe(before); + }); +}); diff --git a/tests/integration/concurrency.test.ts b/tests/integration/concurrency.test.ts new file mode 100644 index 0000000..a473720 --- /dev/null +++ b/tests/integration/concurrency.test.ts @@ -0,0 +1,118 @@ +/** + * Races against a real Postgres: the same action sent twice at once, crossing + * requests, an accept racing a decline, and overlapping roster imports must + * each leave exactly one outcome. Uses its own organization, event and + * people. Skipped without DATABASE_URL. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresIntroRepository } from "../../apps/web/src/infrastructure/postgres/intro.repo"; +import { PostgresLocationRepository } from "../../apps/web/src/infrastructure/postgres/location.repo"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { PostgresMatchRepository } from "../../apps/web/src/infrastructure/postgres/match.repo"; +import { requestIntro } from "../../apps/web/src/application/use-cases/request-intro"; +import { Intent } from "../../apps/web/src/domain/participant/intent"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `race-${RUN}`; +const KEYS = Array.from({ length: 12 }, (_, i) => `p${i}`); +const email = (key: string) => `race-${key}-${RUN}@example.com`; + +describeDb("concurrent actions", () => { + const intros = new PostgresIntroRepository(); + const locations = new PostgresLocationRepository(); + const participants = new PostgresParticipantRepository(); + const matches = new PostgresMatchRepository(); + const ids: Record = {}; + let eventId: string; + + const settle = (promises: Array>) => Promise.allSettled(promises); + const fulfilled = (results: PromiseSettledResult[]) => results.filter((result) => result.status === "fulfilled").length; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(KEYS.map((key) => ({ name: `Race ${key}`, email: email(key) }))); + for (const key of KEYS) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + for (const key of KEYS) { + await participants.saveProfile({ + eventId, attendeeId: ids[key], designation: "Engineer", domain: "AI", aboutMe: "Builds things", selfDescription: null, + yearsOfExperience: null, mostImpressiveBuild: null, intent: Intent.NETWORKING, interests: [], venueZone: null, + }); + } + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`race-%-${RUN}@example.com`]); + await closePool(); + }); + + const ask = (from: string, to: string) => requestIntro({ eventId, requesterId: ids[from], recipientId: ids[to] }, { participants, intros }); + const between = async (a: string, b: string) => (await intros.findBetween(eventId, ids[a], ids[b])).length; + + it("creates one request when the same request is sent twice at once", async () => { + expect(fulfilled(await settle([ask("p0", "p1"), ask("p0", "p1"), ask("p0", "p1")]))).toBe(1); + expect(await between("p0", "p1")).toBe(1); + }); + + it("creates one request when two people ask each other at the same moment", async () => { + for (const [a, b] of [["p2", "p3"], ["p4", "p5"], ["p6", "p7"]]) { + expect(fulfilled(await settle([ask(a, b), ask(b, a)]))).toBe(1); + expect(await between(a, b)).toBe(1); + } + }); + + it("lets only one of an accept and a decline win, with one notification at most", async () => { + const intro = await ask("p8", "p9"); + const results = await settle([ + intros.respond(eventId, intro.id, ids.p9, "accepted"), + intros.respond(eventId, intro.id, ids.p9, "declined"), + intros.respond(eventId, intro.id, ids.p9, "accepted"), + ]); + expect(fulfilled(results)).toBe(1); + const final = (await intros.findById(eventId, intro.id))!.status; + const winner = results.find((result) => result.status === "fulfilled") as PromiseFulfilledResult<{ status: string }>; + expect(final).toBe(winner.value.status); + const accepted = await query(`select 1 from notification_outbox where kind = 'intro.accepted' and subject_id = $1`, [intro.id]); + expect(accepted.rows).toHaveLength(final === "accepted" ? 1 : 0); + }); + + it("keeps one open location request when the same question is asked twice at once", async () => { + const intro = await ask("p10", "p11"); + await intros.respond(eventId, intro.id, ids.p11, "accepted"); + const input = { eventId, introId: intro.id, requesterId: ids.p10, recipientId: ids.p11 }; + const created = await Promise.all([locations.create(input), locations.create(input)]); + expect(created.filter(Boolean)).toHaveLength(1); + }); + + it("saves match lists that name each other at the same time without deadlocking", async () => { + const group = ["p0", "p1", "p2", "p3", "p4", "p5"]; + const profiles = await Promise.all(group.map((key) => participants.findProfile(eventId, ids[key]))); + const listFor = (key: string) => profiles.filter((profile) => profile!.attendeeId !== ids[key]) + .map((profile) => ({ participant: profile!, score: 50, scoreBreakdown: {}, mutualInterest: false, reason: null })); + const revisionOf = (key: string) => profiles.find((profile) => profile!.attendeeId === ids[key])!.revision!; + for (let round = 0; round < 5; round++) { + const results = await settle(group.map((key) => matches.save(eventId, ids[key], listFor(key), "race", revisionOf(key)))); + expect(results.filter((result) => result.status === "rejected")).toEqual([]); + expect(results.map((result) => (result as PromiseFulfilledResult).value)).toEqual(group.map(() => true)); + } + }); + + it("imports overlapping roster sheets at once without duplicating anyone", async () => { + const sheet = (keys: string[]) => keys.map((key) => ({ + name: `Imported ${key}`, email: email(`imp-${key}`), phone: null, about_me: null, designation: null, domain: null, + self_description: null, years_of_experience: null, most_impressive_build: null, + })); + await Promise.all([events.importRoster(eventId, sheet(["a", "b", "c"])), events.importRoster(eventId, sheet(["b", "c", "d"]))]); + const { rows } = await query<{ email: string; on_roster: number }>( + `select a.email, count(p.attendee_id)::int as on_roster from registered_attendees a + left join event_participants p on p.attendee_id = a.id and p.event_id = $1 + where a.email like $2 group by a.email order by a.email`, [eventId, `race-imp-%-${RUN}@example.com`]); + expect(rows.map((row) => row.on_roster)).toEqual([1, 1, 1, 1]); + }); +}); diff --git a/tests/integration/connection-privacy.test.ts b/tests/integration/connection-privacy.test.ts new file mode 100644 index 0000000..7ca8a2a --- /dev/null +++ b/tests/integration/connection-privacy.test.ts @@ -0,0 +1,62 @@ +/** + * Connection privacy against a real Postgres: a declined request stays + * declined, and an attendee removed from the event disappears from other + * people's connections and contacts. Uses its own organization, event and + * attendees. Skipped when DATABASE_URL is unset. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresIntroRepository } from "../../apps/web/src/infrastructure/postgres/intro.repo"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `privacy-${RUN}`; +const email = (key: string) => `privacy-${key}-${RUN}@example.com`; + +describeDb("connection privacy", () => { + const intros = new PostgresIntroRepository(); + const ids: Record = {}; + let eventId: string; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(["ann", "bo", "cy"].map((key) => ({ name: key, email: email(key), phone: null }))); + for (const key of ["ann", "bo", "cy"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`privacy-%-${RUN}@example.com`]); + await closePool(); + }); + + it("keeps a declined request declined", async () => { + const declined = await intros.create({ eventId, requesterId: ids.ann, recipientId: ids.bo }); + await intros.respond(eventId, declined.id, ids.bo, "declined"); + await expect(intros.create({ eventId, requesterId: ids.ann, recipientId: ids.bo })).rejects.toThrow(/declined an earlier request/); + expect((await intros.findById(eventId, declined.id))?.status).toBe("declined"); + expect(await intros.findBetween(eventId, ids.bo, ids.ann)).toHaveLength(1); + }); + + it("counts pending outgoing requests", async () => { + await intros.create({ eventId, requesterId: ids.cy, recipientId: ids.bo }); + expect(await intros.countPendingOutgoing(eventId, ids.cy)).toBe(1); + }); + + it("hides a removed attendee from connections and contacts", async () => { + const accepted = await intros.create({ eventId, requesterId: ids.ann, recipientId: ids.cy }); + await intros.respond(eventId, accepted.id, ids.cy, "accepted"); + expect((await intros.getRevealedContacts(eventId, ids.ann)).map((row) => row.contact.id)).toEqual([ids.cy]); + + await events.setRevoked(eventId, ids.cy, true); + expect((await intros.listForParticipant(eventId, ids.ann)).some((intro) => intro.id === accepted.id)).toBe(false); + expect(await intros.getRevealedContacts(eventId, ids.ann)).toEqual([]); + + await events.setRevoked(eventId, ids.cy, false); + expect((await intros.getRevealedContacts(eventId, ids.ann)).map((row) => row.contact.id)).toEqual([ids.cy]); + }); +}); diff --git a/tests/integration/database-roles.test.ts b/tests/integration/database-roles.test.ts new file mode 100644 index 0000000..3afad1d --- /dev/null +++ b/tests/integration/database-roles.test.ts @@ -0,0 +1,100 @@ +/** + * The least-privilege logins against a real Postgres. Creates its own roles and + * Keycloak-style database, removes them afterwards, and restores the default + * CONNECT grant it revokes on the local application database. + * Skipped when DATABASE_URL is unset. + */ +import { randomBytes } from "node:crypto"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import pg from "pg"; +import { migrate } from "../../packages/db/src/migrate"; +import { ensureRoles } from "../../packages/db/src/ensure-roles"; + +const databaseUrl = process.env.DATABASE_URL; +const describeDb = databaseUrl ? describe : describe.skip; + +const suffix = randomBytes(3).toString("hex"); +const runtime = { user: `test_runtime_${suffix}`, password: randomBytes(12).toString("hex") }; +const keycloak = { user: `test_kc_${suffix}`, password: randomBytes(12).toString("hex"), database: `test_kc_db_${suffix}` }; + +function urlFor(user?: { user: string; password: string }, database?: string): string { + const url = new URL(databaseUrl!); + if (user) { + url.username = user.user; + url.password = user.password; + } + if (database) url.pathname = `/${database}`; + return url.toString(); +} + +async function as(connection: string, work: (client: pg.Client) => Promise): Promise { + const client = new pg.Client({ connectionString: connection }); + await client.connect(); + try { + return await work(client); + } finally { + await client.end(); + } +} + +describeDb("least-privilege database logins", () => { + const appDatabase = () => new URL(databaseUrl!).pathname.slice(1); + + beforeAll(async () => { + await migrate(); + // A Keycloak database that an earlier deploy filled as the master user. + await as(urlFor(undefined, "postgres"), (c) => c.query(`create database ${keycloak.database}`)); + await as(urlFor(undefined, keycloak.database), (c) => c.query(`create table realm (id serial primary key, name text)`)); + await ensureRoles(databaseUrl!, { runtime, keycloak }); + }); + + afterAll(async () => { + await as(urlFor(undefined, "postgres"), (c) => c.query(`drop database if exists ${keycloak.database}`)); + await as(databaseUrl!, async (c) => { + await c.query(`grant connect on database ${c.escapeIdentifier(appDatabase())} to public`); + await c.query(`drop owned by ${runtime.user}, ${keycloak.user}`); + await c.query(`drop role if exists ${runtime.user}`); + await c.query(`drop role if exists ${keycloak.user}`); + }); + }); + + it("lets the runtime login read and write application rows", async () => { + await as(urlFor(runtime), async (c) => { + const key = `test:roles:${suffix}`; + await c.query(`insert into rate_limit_buckets (bucket_key, window_started_at, count) values ($1, now(), 1)`, [key]); + expect((await c.query(`select count(*)::int as n from rate_limit_buckets where bucket_key = $1`, [key])).rows[0].n).toBe(1); + await c.query(`delete from rate_limit_buckets where bucket_key = $1`, [key]); + }); + }); + + it("refuses the runtime login any schema change", async () => { + await as(urlFor(runtime), async (c) => { + await expect(c.query(`create table test_forbidden (id int)`)).rejects.toThrow(/permission denied/); + await expect(c.query(`drop table rate_limit_buckets`)).rejects.toThrow(/must be owner|permission denied/); + }); + }); + + it("keeps the runtime login out of the Keycloak database", async () => { + await expect(as(urlFor(runtime, keycloak.database), (c) => c.query("select 1"))).rejects.toThrow(/permission denied/); + }); + + it("makes the Keycloak login own its database, including older tables", async () => { + await as(urlFor(keycloak, keycloak.database), async (c) => { + const owner = await c.query(`select tableowner from pg_tables where tablename = 'realm'`); + expect(owner.rows[0].tableowner).toBe(keycloak.user); + await c.query(`alter table realm add column if not exists display_name text`); + await c.query(`insert into realm (name) values ('match-maker')`); + }); + }); + + it("keeps the Keycloak login out of the application database", async () => { + await expect(as(urlFor(keycloak), (c) => c.query("select 1"))).rejects.toThrow(/permission denied/); + }); + + it("is safe to run again and updates passwords", async () => { + const rotated = { ...runtime, password: randomBytes(12).toString("hex") }; + await ensureRoles(databaseUrl!, { runtime: rotated }); + await expect(as(urlFor(rotated), (c) => c.query("select 1 from events limit 1"))).resolves.toBeDefined(); + await expect(as(urlFor(runtime), (c) => c.query("select 1"))).rejects.toThrow(/password/); + }); +}); diff --git a/tests/integration/durable-jobs.test.ts b/tests/integration/durable-jobs.test.ts new file mode 100644 index 0000000..6fe5fab --- /dev/null +++ b/tests/integration/durable-jobs.test.ts @@ -0,0 +1,140 @@ +/** + * Durable, revision-aware matching jobs against a real Postgres. Every claim + * is scoped to this test's own event, so jobs of other local data are never + * touched. Skipped when DATABASE_URL is unset. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, matchingJobs, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { PostgresMatchRepository } from "../../apps/web/src/infrastructure/postgres/match.repo"; +import { Intent } from "../../apps/web/src/domain/participant/intent"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `jobs-${RUN}`; +const email = (key: string) => `jobs-${key}-${RUN}@example.com`; + +describeDb("durable matching jobs", () => { + const participants = new PostgresParticipantRepository(); + const matches = new PostgresMatchRepository(); + const ids: Record = {}; + let eventId: string; + + const save = (key: string, aboutMe: string) => participants.saveProfile({ + eventId, attendeeId: ids[key], designation: "Engineer", domain: "AI", aboutMe, selfDescription: null, + yearsOfExperience: null, mostImpressiveBuild: null, intent: Intent.NETWORKING, interests: [], venueZone: null, + }); + const jobsOf = async (key: string) => (await query<{ id: number; job_type: string; status: string; revision: string; attempts: number; last_error: string | null }>( + `select id::int, job_type, status, revision, attempts, last_error from matching_jobs where event_id = $1 and participant_id = $2 order by id`, + [eventId, ids[key]])).rows; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(["ann", "bo", "cy"].map((key) => ({ name: `Jobs ${key}`, email: email(key) }))); + for (const key of ["ann", "bo", "cy"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`jobs-%-${RUN}@example.com`]); + await closePool(); + }); + + it("queues the embedding with the save, and keeps one pending job through rapid edits", async () => { + const first = await save("ann", "First version"); + expect(first.revision).toBe(1); + await save("ann", "Second version"); + const jobs = await jobsOf("ann"); + expect(jobs).toEqual([expect.objectContaining({ job_type: "embed", status: "pending", revision: "2" })]); + }); + + it("queues an edit made while a job runs instead of dropping it", async () => { + const running = await matchingJobs.claim("embed", eventId); + expect(running).toMatchObject({ participant_id: ids.ann, revision: 2 }); + await save("ann", "Third version"); + const jobs = await jobsOf("ann"); + expect(jobs.map((job) => `${job.status}:${job.revision}`)).toEqual(["running:2", "pending:3"]); + // The running job's embedding is now stale and refused. + expect(await participants.setEmbeddingAt(eventId, ids.ann, new Array(768).fill(0.01), 2)).toBe(false); + expect(await participants.setEmbeddingAt(eventId, ids.ann, new Array(768).fill(0.01), 3)).toBe(true); + await matchingJobs.complete(running!.id); + }); + + it("refuses to publish matches computed for an older revision", async () => { + await save("bo", "Bo's profile"); + const candidate = { participant: (await participants.findProfile(eventId, ids.ann))!, score: 70, scoreBreakdown: {}, mutualInterest: false, reason: "r" }; + expect(await matches.save(eventId, ids.bo, [candidate], "test", 0)).toBe(false); + expect(await matches.save(eventId, ids.bo, [candidate], "test", 1)).toBe(true); + expect(await matches.listForParticipant(eventId, ids.bo)).toHaveLength(1); + }); + + it("retries with a growing delay, then fails for good with the error kept", async () => { + await query(`update matching_jobs set status = 'done' where event_id = $1 and status = 'pending'`, [eventId]); + await save("cy", "Cy's profile"); + for (let attempt = 1; attempt <= 5; attempt++) { + await query(`update matching_jobs set available_at = now() where event_id = $1 and participant_id = $2 and status = 'pending'`, [eventId, ids.cy]); + const job = await matchingJobs.claim("embed", eventId); + expect(job).toMatchObject({ participant_id: ids.cy, attempts: attempt }); + await matchingJobs.fail(job!.id, `boom ${attempt}`); + const [row] = await jobsOf("cy"); + expect(row.status).toBe(attempt < 5 ? "pending" : "failed"); + expect(row.last_error).toBe(`boom ${attempt}`); + } + expect(matchingJobs.retryDelaySeconds(1)).toBe(30); + expect(matchingJobs.retryDelaySeconds(3)).toBe(120); + expect(matchingJobs.retryDelaySeconds(10)).toBe(900); + expect((await matchingJobs.listFailed()).some((job) => job.email === email("cy") && job.last_error === "boom 5")).toBe(true); + }); + + it("re-queues failed jobs on request", async () => { + expect(await matchingJobs.retryFailed("all")).toBeGreaterThanOrEqual(1); + expect((await jobsOf("cy")).some((job) => job.status === "pending" && job.attempts === 0)).toBe(true); + }); + + it("marks a failing job superseded when a newer one is already pending", async () => { + await query(`update matching_jobs set available_at = now() where event_id = $1 and participant_id = $2 and status = 'pending'`, [eventId, ids.cy]); + const job = await matchingJobs.claim("embed", eventId); + await save("cy", "Cy edited again"); + await matchingJobs.fail(job!.id, "late failure"); + expect((await jobsOf("cy")).find((row) => row.id === job!.id)?.status).toBe("done"); + }); + + it("refreshes the people a profile change affects, coalesced and without cascading", async () => { + // ann lists bo; cy has no matches yet; dee's (one-slot) list is full without bo; eve never finished a profile. + await attendees.upsertMany(["dee", "eve"].map((key) => ({ name: `Jobs ${key}`, email: email(key) }))); + for (const key of ["dee", "eve"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + await events.attachRoster(eventId, [ids.dee, ids.eve]); + await save("dee", "Dee's profile"); + await query(`update matching_jobs set status = 'done' where event_id = $1 and status = 'pending'`, [eventId]); + await query(`delete from event_matches where event_id = $1`, [eventId]); + await query(`insert into event_matches (event_id, participant_id, candidate_id, rank, score, computation_version) + values ($1, $2, $3, 1, 50, 't'), ($1, $4, $5, 1, 50, 't')`, [eventId, ids.ann, ids.bo, ids.dee, ids.ann]); + + expect(await matchingJobs.enqueueRefreshes(eventId, ids.bo, { delaySeconds: 60, fullList: 1 })).toBe(2); + await matchingJobs.enqueueRefreshes(eventId, ids.bo, { delaySeconds: 60, fullList: 1 }); + const pending = (await query<{ participant_id: string; trigger_kind: string }>( + `select participant_id, trigger_kind from matching_jobs where event_id = $1 and status = 'pending'`, [eventId])).rows; + expect(pending.map((row) => row.participant_id).sort()).toEqual([ids.ann, ids.cy].sort()); + expect(pending.every((row) => row.trigger_kind === "refresh")).toBe(true); + }); + + it("keeps a pending profile job a profile job when a refresh joins it", async () => { + await save("ann", "Ann edits again"); + await matchingJobs.enqueue(eventId, ids.ann, "embed", { trigger: "refresh" }); + expect((await query<{ trigger_kind: string }>(`select trigger_kind from matching_jobs where event_id = $1 and participant_id = $2 and job_type = 'embed' and status = 'pending'`, + [eventId, ids.ann])).rows[0].trigger_kind).toBe("profile"); + }); + + it("fails a crashed job that was already on its last attempt", async () => { + await query(`update matching_jobs set available_at = now() where event_id = $1 and status = 'pending'`, [eventId]); + const job = await matchingJobs.claim("embed", eventId); + await query(`update matching_jobs set attempts = 5, last_error = null, lease_until = now() - interval '1 minute' where id = $1`, [job!.id]); + await matchingJobs.claim("embed", eventId); + expect((await query<{ status: string; last_error: string }>(`select status, last_error from matching_jobs where id = $1`, [job!.id])).rows[0]) + .toEqual({ status: "failed", last_error: "worker stopped responding" }); + }); +}); diff --git a/tests/integration/explanation-cache.test.ts b/tests/integration/explanation-cache.test.ts new file mode 100644 index 0000000..28142eb --- /dev/null +++ b/tests/integration/explanation-cache.test.ts @@ -0,0 +1,52 @@ +/** + * The Postgres explanation cache: reads, overwrites, expiry, the housekeeping + * sweep and deletion with the event. Uses its own organization and event. + * Skipped when DATABASE_URL is unset. + */ +import { randomUUID } from "node:crypto"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { closePool, housekeeping, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresExplanationCache } from "../../apps/web/src/infrastructure/cache/postgres-explanation-cache"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const ORG = `explain-${Date.now().toString(36)}`; + +describeDb("Postgres explanation cache", () => { + const cache = new PostgresExplanationCache(); + let eventId: string; + const viewerId = randomUUID(); + const candidateId = randomUUID(); + const entry = (key: string, reason: string) => ({ key: `${ORG}:${key}`, eventId, viewerId, candidateId, reason }); + + beforeAll(async () => { + await migrate(); + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug) values ($1, $2, $2) returning id`, [org.id, ORG])).rows[0].id; + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await closePool(); + }); + + it("returns stored reasons and overwrites on a second write", async () => { + await cache.setMany([entry("one", "First reason"), entry("two", "Second reason")], 60); + await cache.setMany([entry("one", "Updated reason")], 60); + const found = await cache.getMany([`${ORG}:one`, `${ORG}:two`, `${ORG}:missing`]); + expect(Object.fromEntries(found)).toEqual({ [`${ORG}:one`]: "Updated reason", [`${ORG}:two`]: "Second reason" }); + }); + + it("ignores expired entries and the sweep removes them", async () => { + await cache.setMany([entry("old", "Old reason")], 60); + await query(`update pair_explanations set expires_at = now() - interval '1 minute' where cache_key = $1`, [`${ORG}:old`]); + expect((await cache.getMany([`${ORG}:old`])).size).toBe(0); + await housekeeping.sweepExpired(); + expect((await query(`select 1 from pair_explanations where cache_key = $1`, [`${ORG}:old`])).rows).toHaveLength(0); + }); + + it("is deleted with its event", async () => { + await query(`delete from events where id = $1`, [eventId]); + expect((await cache.getMany([`${ORG}:one`, `${ORG}:two`])).size).toBe(0); + }); +}); diff --git a/tests/integration/invitations.test.ts b/tests/integration/invitations.test.ts new file mode 100644 index 0000000..b48d3f5 --- /dev/null +++ b/tests/integration/invitations.test.ts @@ -0,0 +1,90 @@ +/** + * Invitation lifecycle against a real Postgres: who gets invited, claims, + * and what removing access takes away. Uses its own organization, event and + * attendees. Skipped when DATABASE_URL is unset. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { query, closePool, attendees, events } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const ORG = "integration-invitations"; +const KEYS = ["new", "invited", "claimed", "removed", "viewer"] as const; +const emailOf = (key: string) => `invite-${key}@example.com`; + +describeDb("invitation lifecycle", () => { + const participants = new PostgresParticipantRepository(); + let eventId: string; + let ids: Record; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(KEYS.map((key) => ({ name: `Invite ${key}`, email: emailOf(key) }))); + const found = await attendees.findIdsByEmails(KEYS.map(emailOf)); + ids = Object.fromEntries(KEYS.map((key) => [key, found.find((row) => row.email === emailOf(key))!.id])); + const { rows: [org] } = await query<{ id: string }>( + `insert into organizations (name, slug) values ('Invitations', $1) + on conflict (slug) do update set name = excluded.name returning id`, [ORG]); + const { rows: [event] } = await query<{ id: string }>( + `insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') + on conflict (organization_id, slug) do update set status = 'open' returning id`, [org.id, ORG]); + eventId = event.id; + await query(`delete from event_participants where event_id = $1`, [eventId]); + await events.attachRoster(eventId, Object.values(ids)); + await query( + `update event_participants set profile_updated_at = now(), designation = 'Engineer', domain = 'AI', about_me = 'Builds things' + where event_id = $1 and attendee_id = any($2::uuid[])`, [eventId, [ids.removed, ids.viewer]]); + await query(`update event_participants set invited_at = now() - interval '2 minutes', invite_count = 1 where event_id = $1 and attendee_id = $2`, [eventId, ids.invited]); + await events.markClaimed(eventId, ids.claimed); + await events.setRevoked(eventId, ids.removed, true); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email = any($1::text[])`, [KEYS.map(emailOf)]); + await closePool(); + }); + + it("invites only people never invited, never claimed and not removed", async () => { + const targets = await events.invitationTargets(eventId, null, 10); + expect(targets.map((target) => target.email).sort()).toEqual([emailOf("new"), emailOf("viewer")]); + }); + + it("throttles a resend to the same person", async () => { + expect(await events.invitationTargets(eventId, ids.invited, 10)).toEqual([]); + expect(await events.invitationTargets(eventId, ids.invited, 1)).toHaveLength(1); + expect(await events.invitationTargets(eventId, ids.claimed, 0)).toEqual([]); + }); + + it("records a failure without pretending the e-mail went out", async () => { + await events.recordInvitation(eventId, ids.new, "Resend rejected the address"); + await events.recordInvitation(eventId, ids.invited, null); + const rows = await events.roster(eventId); + const byId = new Map(rows.map((row) => [row.id, row])); + expect(byId.get(ids.new)).toMatchObject({ invited_at: null, invite_count: 0, invite_error: "Resend rejected the address" }); + expect(byId.get(ids.invited)).toMatchObject({ invite_count: 2, invite_error: null }); + }); + + it("keeps the first claim time", async () => { + const before = (await events.roster(eventId)).find((row) => row.id === ids.claimed)!.claimed_at; + await events.markClaimed(eventId, ids.claimed); + expect((await events.roster(eventId)).find((row) => row.id === ids.claimed)!.claimed_at).toEqual(before); + }); + + it("removes every kind of access from a revoked attendee", async () => { + expect(await events.isParticipant(eventId, ids.removed)).toBe(false); + expect(await participants.hasEventAccess(eventId, ids.removed)).toBe(false); + expect(await events.participantIds(eventId)).not.toContain(ids.removed); + expect(await events.rosterState(eventId, ids.removed)).toBe("revoked"); + const candidates = await participants.findCandidatesByIds(eventId, [ids.removed, ids.viewer]); + expect(candidates.map((candidate) => candidate.attendeeId)).toEqual([ids.viewer]); + }); + + it("restores access explicitly, and re-importing does not do it silently", async () => { + await events.attachRoster(eventId, [ids.removed]); + expect(await events.rosterState(eventId, ids.removed)).toBe("revoked"); + await events.setRevoked(eventId, ids.removed, false); + expect(await participants.hasEventAccess(eventId, ids.removed)).toBe(true); + }); +}); diff --git a/tests/integration/location-requests.test.ts b/tests/integration/location-requests.test.ts new file mode 100644 index 0000000..df58455 --- /dev/null +++ b/tests/integration/location-requests.test.ts @@ -0,0 +1,74 @@ +/** + * Location requests against a real Postgres: one open request per direction, + * replies visible only to the asker, expiry, and the notifications they + * queue. Uses its own organization, event and people. Skipped without DATABASE_URL. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresIntroRepository } from "../../apps/web/src/infrastructure/postgres/intro.repo"; +import { PostgresLocationRepository } from "../../apps/web/src/infrastructure/postgres/location.repo"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `location-${RUN}`; +const email = (key: string) => `location-${key}-${RUN}@example.com`; + +describeDb("location requests", () => { + const intros = new PostgresIntroRepository(); + const locations = new PostgresLocationRepository(); + const ids: Record = {}; + let eventId: string; + let introId: string; + let requestId: string; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(["amy", "ben", "cal"].map((key) => ({ name: `Location ${key}`, email: email(key) }))); + for (const key of ["amy", "ben", "cal"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + introId = (await intros.create({ eventId, requesterId: ids.amy, recipientId: ids.ben })).id; + await intros.respond(eventId, introId, ids.ben, "accepted"); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`location-%-${RUN}@example.com`]); + await closePool(); + }); + + it("opens one request per direction and queues a notification for it", async () => { + requestId = (await locations.create({ eventId, introId, requesterId: ids.amy, recipientId: ids.ben }))!.id; + expect(await locations.create({ eventId, introId, requesterId: ids.amy, recipientId: ids.ben })).toBeNull(); + const queued = await query(`select 1 from notification_outbox where kind = 'location.requested' and subject_id = $1 and recipient_id = $2`, [requestId, ids.ben]); + expect(queued.rows).toHaveLength(1); + expect((await locations.forParticipant(eventId, ids.ben))).toEqual([expect.objectContaining({ direction: "incoming", status: "pending", zone: null })]); + }); + + it("only the recipient can answer, and only the asker sees the zone", async () => { + expect(await locations.answer({ eventId, requestId, recipientId: ids.cal, zone: "Cafe" })).toBe(false); + expect(await locations.answer({ eventId, requestId, recipientId: ids.ben, zone: "Cafe" })).toBe(true); + expect(await locations.forParticipant(eventId, ids.amy)).toEqual([expect.objectContaining({ direction: "outgoing", status: "answered", zone: "Cafe" })]); + expect((await locations.forParticipant(eventId, ids.ben))[0]).toMatchObject({ direction: "incoming", zone: null }); + expect(await locations.forParticipant(eventId, ids.cal)).toEqual([]); + expect((await query(`select 1 from notification_outbox where kind = 'location.answered' and recipient_id = $1`, [ids.amy])).rows).toHaveLength(1); + }); + + it("expires shared zones and unanswered requests, and lets a new request replace an expired one", async () => { + await query(`update location_requests set zone_expires_at = now() - interval '1 minute' where id = $1`, [requestId]); + expect((await locations.forParticipant(eventId, ids.amy))[0]).toMatchObject({ status: "expired", zone: null }); + + const next = (await locations.create({ eventId, introId, requesterId: ids.amy, recipientId: ids.ben }))!; + await query(`update location_requests set expires_at = now() - interval '1 minute' where id = $1`, [next.id]); + expect(await locations.answer({ eventId, requestId: next.id, recipientId: ids.ben, zone: "Cafe" })).toBe(false); + expect(await locations.create({ eventId, introId, requesterId: ids.amy, recipientId: ids.ben })).not.toBeNull(); + }); + + it("records a decline without a zone", async () => { + const open = (await locations.forParticipant(eventId, ids.ben)).find((request) => request.status === "pending")!; + expect(await locations.answer({ eventId, requestId: open.id, recipientId: ids.ben, zone: null })).toBe(true); + expect((await locations.forParticipant(eventId, ids.amy))[0]).toMatchObject({ status: "declined", zone: null }); + }); +}); diff --git a/tests/integration/match-queue.test.ts b/tests/integration/match-queue.test.ts index cdd1e48..3ba4f55 100644 --- a/tests/integration/match-queue.test.ts +++ b/tests/integration/match-queue.test.ts @@ -73,20 +73,21 @@ describeDb("match queue status", () => { expect(await queue.latestJob(otherEventId, ids.c)).toMatchObject({ status: "pending" }); }); - it("re-queues a participant whose job is stuck, leaving fresh running jobs alone", async () => { + it("queues a new edit behind a running job instead of dropping it", async () => { await job(eventId, "c", "embed", "running", 2, 1); await matchingJobs.enqueue(eventId, ids.c, "embed"); - expect(await queue.latestJob(eventId, ids.c)).toMatchObject({ status: "running" }); + expect(await queue.latestJob(eventId, ids.c)).toMatchObject({ type: "embed", status: "pending" }); + }); - await matchingJobs.enqueue(eventId, ids.b, "embed"); - expect(await queue.latestJob(eventId, ids.b)).toMatchObject({ type: "embed", status: "pending" }); - const { rows } = await query<{ status: string }>( - `select status from matching_jobs where event_id = $1 and participant_id = $2 order by created_at`, [eventId, ids.b]); - expect(rows.map((row) => row.status)).toEqual(["failed", "pending"]); + it("reclaims a job whose worker stopped responding", async () => { + await query(`update matching_jobs set lease_until = now() - interval '1 minute' where event_id = $1 and participant_id = $2 and status = 'running'`, [eventId, ids.b]); + const reclaimed = await matchingJobs.claim("embed", eventId); + expect(reclaimed).toMatchObject({ participant_id: ids.b, attempts: 1 }); + await matchingJobs.complete(reclaimed!.id); }); it("lists one latest job per participant in the event", async () => { const jobs = await queue.latestJobsForEvent(eventId); - expect(jobs.map((row) => row.status).sort()).toEqual(["done", "pending", "running"]); + expect(jobs.map((row) => row.status).sort()).toEqual(["done", "done", "pending"]); }); }); diff --git a/tests/integration/matching-pipeline.test.ts b/tests/integration/matching-pipeline.test.ts new file mode 100644 index 0000000..577ba8e --- /dev/null +++ b/tests/integration/matching-pipeline.test.ts @@ -0,0 +1,159 @@ +/** + * The whole background journey against a real Postgres, driven by the real + * worker functions: save → embed → match → stored list, refreshes for people + * who saved earlier, the reranked order surviving storage, and recovery when + * a worker dies between stages. Only the model providers are replaced, by + * deterministic fakes. Work is scoped to this test's own event. Skipped + * without DATABASE_URL. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; + +vi.hoisted(() => { + process.env.MATCH_REFRESH_DELAY_SECONDS = "0"; +}); + +vi.mock("../../apps/web/src/infrastructure/providers/provider-registry", () => { + const DIMENSIONS = 768; + // Bag of words hashed into a unit vector: similar texts, similar vectors. + const embed = (text: string) => { + const vector = new Array(DIMENSIONS).fill(0); + for (const word of text.toLowerCase().match(/[a-z]+/g) ?? []) { + let hash = 0; + for (const char of word) hash = (hash * 31 + char.charCodeAt(0)) % DIMENSIONS; + vector[hash] += 1; + } + const norm = Math.hypot(...vector) || 1; + return vector.map((value) => value / norm); + }; + return { + ProviderRegistry: { + embedding: () => ({ id: "fake:bag-of-words", embed: async (text: string) => embed(text) }), + // Reverses the scored order, so a stored list shows whose order won. + llm: () => ({ + id: "fake:reverse", + promptVersion: "test-1", + rerank: async (_viewer: unknown, candidates: Array<{ participant: { name: string } }>) => + [...candidates].reverse().map((candidate) => ({ ...candidate, reason: `fake reason for ${candidate.participant.name}` })), + }), + }, + }; +}); + +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { processEmbedJob } from "../../packages/db/workers/embed.worker"; +import { processMatchJob } from "../../packages/db/workers/match.worker"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { PostgresMatchRepository } from "../../apps/web/src/infrastructure/postgres/match.repo"; +import { Intent } from "../../apps/web/src/domain/participant/intent"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `pipeline-${RUN}`; +const email = (key: string) => `pipeline-${key}-${RUN}@example.com`; +const PEOPLE = { + ann: "Builds graph databases in Rust and writes compilers", + bo: "Builds graph databases in Rust", + cy: "Writes compilers and graph tools", + di: "Paints watercolor landscapes", +}; +type Key = keyof typeof PEOPLE; + +describeDb("matching pipeline end to end", () => { + const participants = new PostgresParticipantRepository(); + const matches = new PostgresMatchRepository(); + const ids = {} as Record; + let eventId: string; + + const save = (key: Key, aboutMe = PEOPLE[key]) => participants.saveProfile({ + eventId, attendeeId: ids[key], designation: "Engineer", domain: "Software", aboutMe, selfDescription: null, + yearsOfExperience: null, mostImpressiveBuild: null, intent: Intent.NETWORKING, interests: [], venueZone: null, + }); + const drain = async () => { + for (let i = 0; i < 200; i++) { + if (!(await processEmbedJob(eventId)) && !(await processMatchJob(eventId))) { + const held = await query(`select 1 from matching_jobs where event_id = $1 and status = 'running' and lease_until > now()`, [eventId]); + // Another worker on this database took a job, with real providers. + if (held.rows.length) throw new Error("a separately running worker claimed this test's jobs; stop it and rerun"); + return; + } + } + throw new Error("jobs never drained"); + }; + const listOf = async (key: Key) => (await matches.listForParticipant(eventId, ids[key])).map((candidate) => ({ + name: candidate.participant.name, score: candidate.score, reason: candidate.reason, + })); + const openJobs = async () => (await query(`select 1 from matching_jobs where event_id = $1 and status <> 'done'`, [eventId])).rows.length; + const killWorkerHolding = async (type: "embed" | "match") => { + const { rows } = await query<{ id: number }>( + `update matching_jobs set status = 'running', attempts = attempts + 1, lease_until = now() - interval '1 second' + where id = (select id from matching_jobs where event_id = $1 and job_type = $2 and status = 'pending' order by id limit 1) + returning id::int`, [eventId, type]); + expect(rows).toHaveLength(1); + }; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany((Object.keys(PEOPLE) as Key[]).map((key) => ({ name: `Pipeline ${key}`, email: email(key) }))); + for (const key of Object.keys(PEOPLE) as Key[]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`pipeline-%-${RUN}@example.com`]); + await closePool(); + }); + + it("gives the first person to save matches once others arrive", async () => { + await save("ann"); + await drain(); + expect(await listOf("ann")).toEqual([]); + for (const key of ["bo", "cy", "di"] as Key[]) await save(key); + await drain(); + expect(await openJobs()).toBe(0); + expect((await listOf("ann")).map((match) => match.name).sort()).toEqual(["Pipeline bo", "Pipeline cy", "Pipeline di"]); + }); + + it("stores the reranked order, not the score order, and keeps it through a refresh that changes nothing", async () => { + const stored = await listOf("ann"); + const scores = stored.map((match) => match.score); + expect(scores).toEqual([...scores].sort((a, b) => a - b)); + expect(scores[0]).toBeLessThan(scores[scores.length - 1]); + expect(stored.every((match) => match.reason === `fake reason for ${match.name}`)).toBe(true); + + // A refresh with every explanation cached must not fall back to score order. + await query(`insert into matching_jobs (event_id, participant_id, job_type, status, revision, trigger_kind) + select $1, $2, 'match', 'pending', profile_revision, 'refresh' from event_participants where event_id = $1 and attendee_id = $2`, + [eventId, ids.ann]); + await drain(); + expect(await listOf("ann")).toEqual(stored); + }); + + it("recovers when a worker dies holding the embedding, without publishing a stale revision", async () => { + await save("ann", "Paints watercolor portraits"); + await killWorkerHolding("embed"); + const latest = await save("ann", "Paints watercolor landscapes and portraits"); + await drain(); + expect(await openJobs()).toBe(0); + const { rows: [row] } = await query<{ profile_revision: string; embedded: boolean }>( + `select profile_revision, embedding is not null as embedded from event_participants where event_id = $1 and attendee_id = $2`, [eventId, ids.ann]); + expect(Number(row.profile_revision)).toBe(latest.revision); + const { rows: done } = await query<{ revision: string }>( + `select revision from matching_jobs where event_id = $1 and participant_id = $2 and job_type = 'match' order by id desc limit 1`, [eventId, ids.ann]); + expect(Number(done[0].revision)).toBe(latest.revision); + // di's watercolor profile is now the closest; the fake rerank puts it last. + expect((await listOf("ann")).at(-1)?.name).toBe("Pipeline di"); + }); + + it("recovers when a worker dies holding the match job", async () => { + await save("bo", "Builds graph databases and query planners in Rust"); + await processEmbedJob(eventId); + await killWorkerHolding("match"); + await drain(); + expect(await openJobs()).toBe(0); + expect(await listOf("bo")).toHaveLength(3); + }); +}); diff --git a/tests/integration/notification-outbox.test.ts b/tests/integration/notification-outbox.test.ts new file mode 100644 index 0000000..d917c70 --- /dev/null +++ b/tests/integration/notification-outbox.test.ts @@ -0,0 +1,74 @@ +/** + * Notifications go through an outbox written with the interaction, and are + * delivered with retries. Real Postgres; delivery uses the e-mail dev + * fallback. Claims are scoped to this test's event. Skipped without DATABASE_URL. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { attendees, closePool, events, outbox, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresIntroRepository } from "../../apps/web/src/infrastructure/postgres/intro.repo"; +import { processNotification } from "../../packages/db/workers/notification.worker"; +import { getContainer } from "../../apps/web/src/composition/container"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `outbox-${RUN}`; +const email = (key: string) => `outbox-${key}-${RUN}@example.com`; + +describeDb("notification outbox", () => { + const intros = new PostgresIntroRepository(); + const ids: Record = {}; + let eventId: string; + let introId: string; + const queued = async () => (await query<{ kind: string; recipient_id: string; status: string; attempts: number; last_error: string | null }>( + `select kind, recipient_id, status, attempts, last_error from notification_outbox where event_id = $1 order by id`, [eventId])).rows; + + beforeAll(async () => { + process.env.EMAIL_DEV_FALLBACK = "true"; + await migrate(); + await attendees.upsertMany(["amy", "ben"].map((key) => ({ name: `Outbox ${key}`, email: email(key) }))); + for (const key of ["amy", "ben"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`outbox-%-${RUN}@example.com`]); + await closePool(); + }); + + it("queues the notification with the request, once", async () => { + introId = (await intros.create({ eventId, requesterId: ids.amy, recipientId: ids.ben })).id; + await outbox.enqueue({ eventId, recipientId: ids.ben, kind: "intro.requested", subjectId: introId, dedupeKey: `intro.requested:${introId}` }); + expect(await queued()).toEqual([expect.objectContaining({ kind: "intro.requested", recipient_id: ids.ben, status: "pending" })]); + }); + + it("delivers it with a link to the connection", async () => { + const info = vi.spyOn(console, "info").mockImplementation(() => {}); + expect(await processNotification(eventId)).toBe(true); + expect((await queued())[0].status).toBe("sent"); + const message = info.mock.calls.map((call) => String(call[0])).find((line) => line.includes(email("ben"))); + expect(message).toContain(`/e/${ORG}/connections#intro-${introId}`); + info.mockRestore(); + }); + + it("queues an acceptance notice for the requester", async () => { + await intros.respond(eventId, introId, ids.ben, "accepted"); + expect((await queued()).at(-1)).toMatchObject({ kind: "intro.accepted", recipient_id: ids.amy, status: "pending" }); + }); + + it("keeps a failed delivery for retry instead of losing it", async () => { + const container = getContainer(); + const failing = vi.spyOn(container.notifications, "introAccepted").mockRejectedValueOnce(new Error("Resend failed (503)")); + vi.spyOn(console, "error").mockImplementation(() => {}); + await processNotification(eventId); + expect((await queued()).at(-1)).toMatchObject({ status: "pending", attempts: 1, last_error: "Resend failed (503)" }); + failing.mockRestore(); + await query(`update notification_outbox set available_at = now() where event_id = $1 and status = 'pending'`, [eventId]); + vi.spyOn(console, "info").mockImplementation(() => {}); + await processNotification(eventId); + expect((await queued()).at(-1)).toMatchObject({ status: "sent", attempts: 2 }); + }); +}); diff --git a/tests/integration/pipeline-health.test.ts b/tests/integration/pipeline-health.test.ts new file mode 100644 index 0000000..ba3912b --- /dev/null +++ b/tests/integration/pipeline-health.test.ts @@ -0,0 +1,60 @@ +/** + * The queue health snapshot behind the backlog and failure alarms, against a + * real Postgres. Other local rows may exist, so it checks lower bounds from + * rows it creates in its own event. Skipped without DATABASE_URL. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, pipelineHealth, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { reportHealthIfDue } from "../../packages/db/workers/metrics.worker"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `health-${RUN}`; + +describeDb("pipeline health", () => { + let eventId: string; + let attendeeId: string; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany([{ name: "Health check", email: `health-${RUN}@example.com` }]); + attendeeId = (await attendees.findByEmail(`health-${RUN}@example.com`))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'closed') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, [attendeeId]); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email = $1`, [`health-${RUN}@example.com`]); + await closePool(); + }); + + it("reports how long due work has waited and what failed for good", async () => { + await query(`insert into matching_jobs (event_id, participant_id, job_type, status, available_at, created_at) + values ($1, $2, 'embed', 'pending', now() - interval '20 minutes', now() - interval '20 minutes'), + ($1, $2, 'match', 'failed', now(), now())`, [eventId, attendeeId]); + await query(`insert into notification_outbox (event_id, recipient_id, kind, subject_id, dedupe_key, status, available_at) + values ($1, $2, 'intro.requested', $2, $3, 'pending', now() - interval '15 minutes'), + ($1, $2, 'intro.accepted', $2, $4, 'failed', now())`, + [eventId, attendeeId, `health-a-${RUN}`, `health-b-${RUN}`]); + + const health = await pipelineHealth.snapshot(); + expect(health.jobsPending).toBeGreaterThanOrEqual(1); + expect(health.oldestDueJobSeconds).toBeGreaterThanOrEqual(20 * 60 - 5); + expect(health.jobsFailed).toBeGreaterThanOrEqual(1); + expect(health.oldestDueNotificationSeconds).toBeGreaterThanOrEqual(15 * 60 - 5); + expect(health.notificationsFailed).toBeGreaterThanOrEqual(1); + }); + + it("reports at most once a minute, with a heartbeat", async () => { + const metrics = { record: (values: Array<{ name: string }>) => recorded.push(values.map((value) => value.name)) }; + const recorded: string[][] = []; + const now = Date.now() + 10 * 60_000; + expect(await reportHealthIfDue(metrics, now)).toBe(true); + expect(await reportHealthIfDue(metrics, now + 1000)).toBe(false); + expect(recorded).toHaveLength(1); + expect(recorded[0]).toEqual(expect.arrayContaining(["WorkerHeartbeat", "OldestDueJobSeconds", "NotificationsFailed"])); + }); +}); diff --git a/tests/integration/rate-limiter.test.ts b/tests/integration/rate-limiter.test.ts new file mode 100644 index 0000000..8eb2246 --- /dev/null +++ b/tests/integration/rate-limiter.test.ts @@ -0,0 +1,47 @@ +/** + * The shared limiter backs search and the per-IP sign-in limits, so it runs + * against a real Postgres here (unit tests mock it). Uses its own bucket keys. + * Skipped when DATABASE_URL is unset. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { query, closePool } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresRateLimiter } from "../../apps/web/src/infrastructure/cache/postgres-rate-limiter"; + +const hasDatabase = Boolean(process.env.DATABASE_URL); +const describeDb = hasDatabase ? describe : describe.skip; +const PREFIX = "test:rate-limiter:"; + +describeDb("PostgresRateLimiter", () => { + const limiter = new PostgresRateLimiter(); + + beforeAll(async () => { + await migrate(); + await query(`delete from rate_limit_buckets where bucket_key like $1`, [`${PREFIX}%`]); + }); + + afterAll(async () => { + await query(`delete from rate_limit_buckets where bucket_key like $1`, [`${PREFIX}%`]); + await closePool(); + }); + + it("allows up to the limit, then refuses", async () => { + const key = `${PREFIX}sequential`; + const allowed = []; + for (let i = 0; i < 4; i++) allowed.push(await limiter.consume(key, 3, 60)); + expect(allowed).toEqual([true, true, true, false]); + }); + + it("holds the limit for simultaneous requests", async () => { + const results = await Promise.all(Array.from({ length: 10 }, () => limiter.consume(`${PREFIX}parallel`, 4, 60))); + expect(results.filter(Boolean)).toHaveLength(4); + }); + + it("starts a new window once the old one has passed", async () => { + const key = `${PREFIX}window`; + await limiter.consume(key, 1, 60); + expect(await limiter.consume(key, 1, 60)).toBe(false); + await query(`update rate_limit_buckets set window_started_at = now() - interval '2 minutes' where bucket_key = $1`, [key]); + expect(await limiter.consume(key, 1, 60)).toBe(true); + }); +}); diff --git a/tests/integration/retrieval-recall.test.ts b/tests/integration/retrieval-recall.test.ts new file mode 100644 index 0000000..e7db105 --- /dev/null +++ b/tests/integration/retrieval-recall.test.ts @@ -0,0 +1,65 @@ +/** + * Retrieval must not starve someone whose nearest neighbours all have + * intents they do not welcome: a mentor among many mentors still finds the + * one person seeking a mentor. Real Postgres, fake embeddings, own event. + * Skipped without DATABASE_URL. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { PostgresMatchRepository } from "../../apps/web/src/infrastructure/postgres/match.repo"; +import { PostgresCandidateCache } from "../../apps/web/src/infrastructure/cache/postgres-candidate-cache"; +import { MatchingPipeline } from "../../apps/web/src/domain/matching/pipeline"; +import { Intent } from "../../apps/web/src/domain/participant/intent"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `recall-${RUN}`; +const MENTORS = 55; +const email = (key: string) => `recall-${key}-${RUN}@example.com`; +const VECTOR = Array.from({ length: 768 }, (_, i) => (i === 0 ? 1 : 0)); +const embeddings = { id: "test:recall", embed: async () => VECTOR }; + +describeDb("retrieval recall", () => { + const participants = new PostgresParticipantRepository(); + const pipeline = new MatchingPipeline(participants, new PostgresMatchRepository(), embeddings, null, new PostgresCandidateCache(), null); + const ids: Record = {}; + let eventId: string; + + beforeAll(async () => { + await migrate(); + const keys = ["viewer", "seeker", ...Array.from({ length: MENTORS }, (_, i) => `mentor${i}`)]; + await attendees.upsertMany(keys.map((key) => ({ name: `Recall ${key}`, email: email(key) }))); + const { rows } = await query<{ id: string; email: string }>(`select id, email from registered_attendees where email like $1`, [`recall-%-${RUN}@example.com`]); + for (const row of rows) ids[row.email.split("-")[1]] = row.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + for (const key of keys) { + await participants.saveProfile({ + eventId, attendeeId: ids[key], designation: "Engineer", domain: "AI", aboutMe: key === "seeker" ? "New to the field" : "Mentors engineers", + selfDescription: null, yearsOfExperience: null, mostImpressiveBuild: null, + intent: key === "seeker" ? Intent.SEEKING_MENTOR : Intent.MENTORING, interests: [], venueZone: null, + }); + // Every mentor sits right next to the viewer; the seeker has no vector at all. + if (key.startsWith("mentor")) await participants.setEmbedding(eventId, ids[key], VECTOR, embeddings.id); + } + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`recall-%-${RUN}@example.com`]); + await closePool(); + }); + + it("finds the only compatible person beyond the nearest neighbours", async () => { + const ranked = await pipeline.run({ eventId, participantId: ids.viewer, mode: "profile" }); + expect(ranked.map((candidate) => candidate.participant.attendeeId)).toEqual([ids.seeker]); + }); + + it("does the same for search", async () => { + const ranked = await pipeline.run({ eventId, participantId: ids.viewer, mode: "search", query: "mentors engineers" }); + expect(ranked.map((candidate) => candidate.participant.attendeeId)).toContain(ids.seeker); + }); +}); diff --git a/tests/integration/roster-import-ownership.test.ts b/tests/integration/roster-import-ownership.test.ts new file mode 100644 index 0000000..e908785 --- /dev/null +++ b/tests/integration/roster-import-ownership.test.ts @@ -0,0 +1,107 @@ +/** + * Organizer roles and roster import ownership against a real Postgres. Uses + * two organizations of its own. Skipped when DATABASE_URL is unset. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG_A = `ownership-a-${RUN}`; +const ORG_B = `ownership-b-${RUN}`; +const email = (key: string) => `ownership-${key}-${RUN}@example.com`; +const PHONE_KEPT = `+9196${RUN.slice(-8).padStart(8, "0").replace(/\D/g, "7")}`; + +const row = (key: string, overrides: Record = {}) => ({ + name: `Sheet ${key}`, email: email(key), phone: null, about_me: null, designation: null, domain: null, + self_description: null, years_of_experience: null, most_impressive_build: null, ...overrides, +}); + +describeDb("organizer roles and roster import ownership", () => { + const ids: Record = {}; + let eventA: string; + let eventA2: string; + let eventB: string; + + async function org(slug: string) { + const { rows: [created] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [slug]); + return created.id; + } + + beforeAll(async () => { + await migrate(); + // An attendee organization A already registered, with a name and phone. + await attendees.upsertMany([ + { name: "Original Name", email: email("shared"), phone: PHONE_KEPT, designation: "Engineer" }, + { name: "Org Admin", email: email("admin") }, + { name: "Event Helper", email: email("helper") }, + { name: "Org Member", email: email("member") }, + ]); + for (const key of ["shared", "admin", "helper", "member"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const orgA = await org(ORG_A); + const orgB = await org(ORG_B); + await query(`insert into organization_members (organization_id, attendee_id, role) values ($1, $2, 'admin'), ($1, $3, 'organizer'), ($1, $4, 'organizer')`, + [orgA, ids.admin, ids.helper, ids.member]); + eventA = (await events.create({ organizationId: orgA, organizerId: ids.member, name: "A one", slug: `${ORG_A}-one` })).id; + eventA2 = (await events.create({ organizationId: orgA, organizerId: ids.admin, name: "A two", slug: `${ORG_A}-two` })).id; + await query(`insert into event_organizers (event_id, attendee_id, role) values ($1, $2, 'organizer')`, [eventA, ids.helper]); + eventB = (await query<{ id: string }>(`insert into events (organization_id, name, slug) values ($1, 'B', $2) returning id`, [orgB, `${ORG_B}-one`])).rows[0].id; + await events.attachRoster(eventA, [ids.shared]); + }); + + afterAll(async () => { + await query(`delete from organizations where slug = any($1::text[])`, [[ORG_A, ORG_B]]); + await query(`delete from registered_attendees where email like $1`, [`ownership-%-${RUN}@example.com`]); + await closePool(); + }); + + it("gives each role the right reach", async () => { + expect(await events.organizerRoles(eventA, ids.helper)).toEqual({ event: "organizer", organization: "organizer" }); + expect(await events.isOrganizer(eventA2, ids.admin)).toBe(true); + expect(await events.isOrganizer(eventA, ids.admin)).toBe(true); + expect(await events.isOrganizer(eventA2, ids.member)).toBe(false); + expect(await events.isOrganizer(eventB, ids.admin)).toBe(false); + }); + + it("lets an organization admin save settings of an event they did not create", async () => { + const updated = await events.update({ eventId: eventA, organizerId: ids.admin, zones: ["Hall"] }); + expect(updated?.zones).toEqual(["Hall"]); + expect(await events.update({ eventId: eventA2, organizerId: ids.member, zones: ["Nope"] })).toBeNull(); + }); + + it("lists only the events a viewer may open", async () => { + const slugs = async (viewer: string) => (await events.listForOrganization(ORG_A, viewer)).map((event) => event.slug).sort(); + expect(await slugs(ids.admin)).toEqual([`${ORG_A}-one`, `${ORG_A}-two`]); + expect(await slugs(ids.member)).toEqual([`${ORG_A}-one`]); + }); + + it("never replaces an existing person's details from another organization's sheet", async () => { + const result = await events.importRoster(eventB, [ + row("shared", { name: "Hijacked Name", phone: "+919000000001", designation: "Attacker" }), + row("fresh", { phone: PHONE_KEPT }), + ]); + expect(result).toEqual({ created: 1, attached: 2, alreadyOnRoster: 0, keptExisting: 1, phoneConflicts: 1 }); + const shared = (await attendees.findByEmail(email("shared")))!; + expect(shared).toMatchObject({ name: "Original Name", phone: PHONE_KEPT, designation: "Engineer" }); + const fresh = (await attendees.findByEmail(email("fresh")))!; + expect(fresh.phone).toBeNull(); + }); + + it("fills only empty fields of a known person", async () => { + await events.importRoster(eventB, [row("fresh", { domain: "Climate", name: "Different" })]); + expect(await attendees.findByEmail(email("fresh"))).toMatchObject({ name: "Sheet fresh", domain: "Climate" }); + }); + + it("is safe to retry", async () => { + const result = await events.importRoster(eventB, [row("shared"), row("fresh")]); + expect(result).toMatchObject({ created: 0, attached: 0, alreadyOnRoster: 2 }); + }); + + it("applies nothing when any row fails", async () => { + await expect(events.importRoster(eventA2, [row("rollback-ok"), row("rollback-bad", { years_of_experience: "not a number" })])).rejects.toThrow(); + expect(await attendees.findByEmail(email("rollback-ok"))).toBeNull(); + const { rows } = await query(`select 1 from event_participants where event_id = $1`, [eventA2]); + expect(rows).toHaveLength(0); + }); +}); diff --git a/tests/integration/search-retrieval.test.ts b/tests/integration/search-retrieval.test.ts new file mode 100644 index 0000000..66695d8 --- /dev/null +++ b/tests/integration/search-retrieval.test.ts @@ -0,0 +1,86 @@ +/** + * Query-sensitive retrieval without embeddings, and search filters, against a + * real Postgres. Uses its own organization, event and attendees. + * Skipped when DATABASE_URL is unset. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { Intent } from "../../apps/web/src/domain/participant/intent"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `search-${RUN}`; +const email = (key: string) => `search-${key}-${RUN}@example.com`; + +// Neutral names: names are part of the searchable text. +const NAMES: Record = { designer: "Ana Rao", engineer: "Ben Iyer", founder: "Cy Das" }; +const PEOPLE = { + designer: { designation: "Product Designer", domain: "Design", about_me: "Designing climate tools", intent: "NETWORKING", interests: ["climate"] }, + engineer: { designation: "ML Engineer", domain: "AI", about_me: "Builds retrieval and search systems", intent: "NETWORKING", interests: ["rag"] }, + founder: { designation: "Founder", domain: "ai", about_me: "Hiring engineers for search", intent: "HIRING", interests: ["search"] }, +} as const; + +describeDb("search retrieval", () => { + const repo = new PostgresParticipantRepository(); + let eventId: string; + const ids: Record = {}; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(Object.keys(PEOPLE).map((key) => ({ name: NAMES[key], email: email(key) }))); + for (const key of Object.keys(PEOPLE)) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + for (const [key, person] of Object.entries(PEOPLE)) { + await query( + `update event_participants set designation = $3, domain = $4, about_me = $5, intent = $6, interests = $7, profile_updated_at = now() + where event_id = $1 and attendee_id = $2`, + [eventId, ids[key], person.designation, person.domain, person.about_me, person.intent, person.interests] + ); + } + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`search-%-${RUN}@example.com`]); + await closePool(); + }); + + const names = async (input: Parameters[0]) => + (await repo.listCandidates(input)).map((profile) => profile.name); + + it("returns only people matching the query words, best match first", async () => { + expect(await names({ eventId, excludeAttendeeId: null, terms: ["climate"] })).toEqual(["Ana Rao"]); + const searchers = await names({ eventId, excludeAttendeeId: null, terms: ["search", "engineer"] }); + expect(searchers.sort()).toEqual(["Ben Iyer", "Cy Das"]); + expect(await names({ eventId, excludeAttendeeId: null, terms: ["quantum"] })).toEqual([]); + }); + + it("matches word prefixes", async () => { + expect(await names({ eventId, excludeAttendeeId: null, terms: ["design"] })).toEqual(["Ana Rao"]); + }); + + it("applies intent and domain filters, ignoring domain case", async () => { + expect(await names({ eventId, excludeAttendeeId: null, terms: ["search"], filters: { intent: Intent.HIRING, domain: null } })).toEqual(["Cy Das"]); + expect((await names({ eventId, excludeAttendeeId: null, filters: { intent: null, domain: "AI" } })).sort()).toEqual(["Ben Iyer", "Cy Das"]); + }); + + it("ranks by vector only among candidates embedded by the query's model", async () => { + const unit = (index: number) => Array.from({ length: 768 }, (_, i) => (i === index ? 1 : 0)); + // Ana's vector matches the query exactly but comes from another model; Ben's is close and from the same one. + await query(`update event_participants set embedding = $3::vector, embedding_model = 'other-model' where event_id = $1 and attendee_id = $2`, + [eventId, ids.designer, `[${unit(0).join(",")}]`]); + await query(`update event_participants set embedding = $3::vector, embedding_model = 'query-model' where event_id = $1 and attendee_id = $2`, + [eventId, ids.engineer, `[${unit(0).map((v, i) => (i === 1 ? 0.5 : v)).join(",")}]`]); + const ranked = await names({ eventId, excludeAttendeeId: null, embedding: unit(0), embeddingModel: "query-model" }); + expect(ranked[0]).toBe("Ben Iyer"); + expect(ranked).toContain("Ana Rao"); + }); + + it("lists each domain present once", async () => { + expect(await repo.listDomains(eventId)).toEqual(["AI", "Design"]); + }); +}); diff --git a/tests/integration/shared-retrieval.test.ts b/tests/integration/shared-retrieval.test.ts new file mode 100644 index 0000000..656b917 --- /dev/null +++ b/tests/integration/shared-retrieval.test.ts @@ -0,0 +1,107 @@ +/** + * Shared search retrieval against a real Postgres: lock ownership, query + * embedding reuse, and two pipeline instances (two "replicas") coalescing an + * identical search while still ranking personally. Uses its own organization, + * event and attendees. Skipped when DATABASE_URL is unset. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresCandidateCache } from "../../apps/web/src/infrastructure/cache/postgres-candidate-cache"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { MatchingPipeline } from "../../apps/web/src/domain/matching/pipeline"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const ORG = `shared-${RUN}`; +const email = (key: string) => `shared-${key}-${RUN}@example.com`; +const PEOPLE: Record = { + ana: { domain: "AI", interests: ["search"] }, + ben: { domain: "Design", interests: ["climate"] }, + cy: { domain: "AI", interests: ["search", "rag"] }, + dee: { domain: "Design", interests: ["climate", "ux"] }, +}; + +describeDb("shared search retrieval", () => { + const cache = new PostgresCandidateCache(); + const ids: Record = {}; + let eventId: string; + + beforeAll(async () => { + await migrate(); + await attendees.upsertMany(Object.keys(PEOPLE).map((key) => ({ name: `Shared ${key}`, email: email(key) }))); + for (const key of Object.keys(PEOPLE)) ids[key] = (await attendees.findByEmail(email(key)))!.id; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [ORG]); + eventId = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, ORG])).rows[0].id; + await events.attachRoster(eventId, Object.values(ids)); + for (const [key, person] of Object.entries(PEOPLE)) { + await query(`update event_participants set domain = $3, interests = $4, about_me = 'Builds products for people', designation = 'Builder', profile_updated_at = now() + where event_id = $1 and attendee_id = $2`, [eventId, ids[key], person.domain, person.interests]); + } + }); + + afterAll(async () => { + await query(`delete from organizations where slug = $1`, [ORG]); + await query(`delete from registered_attendees where email like $1`, [`shared-%-${RUN}@example.com`]); + await closePool(); + }); + + it("gives a lock to one owner at a time, and only the owner releases it", async () => { + const key = `test:${RUN}:lock`; + const first = await cache.acquire(key, 5000); + expect(first).toBeTruthy(); + expect(await cache.acquire(key, 5000)).toBeNull(); + await cache.release(key, "not-the-owner"); + expect(await cache.acquire(key, 5000)).toBeNull(); + await cache.release(key, first!); + const second = await cache.acquire(key, 5000); + expect(second).toBeTruthy(); + await cache.release(key, second!); + }); + + it("lets an expired lock be taken over", async () => { + const key = `test:${RUN}:expired`; + await cache.acquire(key, 5000); + await query(`update cache_locks set expires_at = now() - interval '1 second' where lock_key = $1`, [key]); + const token = await cache.acquire(key, 5000); + expect(token).toBeTruthy(); + await cache.release(key, token!); + }); + + it("stores and reads query vectors", async () => { + await cache.setVector(`test:${RUN}:vector`, [0.1, 0.2], 60); + expect(await cache.getVector(`test:${RUN}:vector`)).toEqual([0.1, 0.2]); + }); + + /** A pipeline as one web replica would build it, counting retrievals. */ + function replica() { + const participants = new PostgresParticipantRepository(); + const retrievals = vi.spyOn(participants, "listCandidates"); + const embeddings = { id: "none", embed: vi.fn(async () => null) }; + const matches = { save: vi.fn(), listForParticipant: vi.fn() }; + return { pipeline: new MatchingPipeline(participants, matches, embeddings, null, cache), retrievals }; + } + const search = (pipeline: MatchingPipeline, viewer: string, text: string, domain: string | null = null) => + pipeline.run({ eventId, participantId: ids[viewer], mode: "search", query: text, filters: { intent: null, domain } }) + .then((ranked) => ranked.map((match) => match.participant.name)); + + it("coalesces concurrent identical searches across replicas, but ranks personally", async () => { + const one = replica(); + const two = replica(); + const [forAna, forBen] = await Promise.all([search(one.pipeline, "ana", "builds products"), search(two.pipeline, "ben", "builds products")]); + expect(one.retrievals.mock.calls.length + two.retrievals.mock.calls.length).toBe(1); + expect(forAna[0]).toBe("Shared cy"); + expect(forBen[0]).toBe("Shared dee"); + }); + + it("retrieves again after the corpus changes or with a different filter", async () => { + const { pipeline, retrievals } = replica(); + await search(pipeline, "ana", "builds products"); + expect(retrievals).not.toHaveBeenCalled(); + await query(`update event_participants set interests = '{search,agents}' where event_id = $1 and attendee_id = $2`, [eventId, ids.cy]); + await search(pipeline, "ana", "builds products"); + expect(retrievals).toHaveBeenCalledTimes(1); + await search(pipeline, "ana", "builds products", "AI"); + expect(retrievals).toHaveBeenCalledTimes(2); + }); +}); diff --git a/tests/integration/tenant-isolation.test.ts b/tests/integration/tenant-isolation.test.ts new file mode 100644 index 0000000..5c47eb8 --- /dev/null +++ b/tests/integration/tenant-isolation.test.ts @@ -0,0 +1,103 @@ +/** + * Event isolation in one place, against a real Postgres: two organizations + * each run an event, and one person attends both. Nothing read or changed + * through one event may reach the other, even with ids taken from it. + * Skipped without DATABASE_URL. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { attendees, closePool, events, query } from "@match-maker/db"; +import { migrate } from "../../packages/db/src/migrate"; +import { PostgresIntroRepository } from "../../apps/web/src/infrastructure/postgres/intro.repo"; +import { PostgresLocationRepository } from "../../apps/web/src/infrastructure/postgres/location.repo"; +import { PostgresParticipantRepository } from "../../apps/web/src/infrastructure/postgres/participant.repo"; +import { PostgresMatchRepository } from "../../apps/web/src/infrastructure/postgres/match.repo"; +import { requestIntro } from "../../apps/web/src/application/use-cases/request-intro"; +import { Intent } from "../../apps/web/src/domain/participant/intent"; + +const describeDb = process.env.DATABASE_URL ? describe : describe.skip; +const RUN = Date.now().toString(36); +const email = (key: string) => `tenant-${key}-${RUN}@example.com`; + +describeDb("event isolation", () => { + const intros = new PostgresIntroRepository(); + const locations = new PostgresLocationRepository(); + const participants = new PostgresParticipantRepository(); + const matches = new PostgresMatchRepository(); + const ids: Record = {}; + const event: Record<"a" | "b", string> = { a: "", b: "" }; + let introA: string; + let locationA: string; + + const profile = (eventId: string, key: string, aboutMe: string) => participants.saveProfile({ + eventId, attendeeId: ids[key], designation: "Engineer", domain: "AI", aboutMe, selfDescription: null, + yearsOfExperience: null, mostImpressiveBuild: null, intent: Intent.NETWORKING, interests: [], venueZone: null, + }); + + beforeAll(async () => { + await migrate(); + // ann and bo attend A, cy attends B, sam attends both. + await attendees.upsertMany(["ann", "bo", "cy", "sam"].map((key) => ({ name: `Tenant ${key}`, email: email(key), phone: `+1555${RUN.slice(-4)}${key.length}${key.charCodeAt(0)}` }))); + for (const key of ["ann", "bo", "cy", "sam"]) ids[key] = (await attendees.findByEmail(email(key)))!.id; + for (const side of ["a", "b"] as const) { + const slug = `tenant-${side}-${RUN}`; + const { rows: [org] } = await query<{ id: string }>(`insert into organizations (name, slug) values ($1, $1) returning id`, [slug]); + event[side] = (await query<{ id: string }>(`insert into events (organization_id, name, slug, status) values ($1, $2, $2, 'open') returning id`, [org.id, slug])).rows[0].id; + } + await events.attachRoster(event.a, [ids.ann, ids.bo, ids.sam]); + await events.attachRoster(event.b, [ids.cy, ids.sam]); + for (const key of ["ann", "bo", "sam"]) await profile(event.a, key, `Event A profile of ${key}`); + for (const key of ["cy", "sam"]) await profile(event.b, key, `Event B profile of ${key}`); + + introA = (await intros.create({ eventId: event.a, requesterId: ids.ann, recipientId: ids.sam })).id; + await intros.respond(event.a, introA, ids.sam, "accepted"); + locationA = (await locations.create({ eventId: event.a, introId: introA, requesterId: ids.ann, recipientId: ids.sam }))!.id; + await matches.save(event.a, ids.ann, [{ participant: (await participants.findProfile(event.a, ids.sam))!, score: 0.9, scoreBreakdown: {}, mutualInterest: true, reason: "A only" }], "test"); + }); + + afterAll(async () => { + await query(`delete from organizations where slug like $1`, [`tenant-_-${RUN}`]); + await query(`delete from registered_attendees where email like $1`, [`tenant-%-${RUN}@example.com`]); + await closePool(); + }); + + it("knows who belongs to which event", async () => { + expect(await participants.hasEventAccess(event.b, ids.ann)).toBe(false); + expect(await participants.hasEventAccess(event.b, ids.sam)).toBe(true); + expect(await participants.findProfile(event.b, ids.ann)).toBeNull(); + expect((await participants.findProfile(event.b, ids.sam))!.aboutMe).toBe("Event B profile of sam"); + }); + + it("retrieves candidates and stored matches from one event only", async () => { + const inB = await participants.listCandidates({ eventId: event.b, excludeAttendeeId: ids.sam, terms: ["profile"] }); + expect(inB.map((candidate) => candidate.attendeeId)).toEqual([ids.cy]); + expect(await matches.listForParticipant(event.b, ids.ann)).toEqual([]); + expect((await matches.listForParticipant(event.a, ids.ann))[0].participant.aboutMe).toBe("Event A profile of sam"); + }); + + it("refuses intros to someone from another event", async () => { + await expect(requestIntro({ eventId: event.b, requesterId: ids.sam, recipientId: ids.ann }, { participants, intros })).rejects.toThrow(/not in the event/); + await expect(requestIntro({ eventId: event.a, requesterId: ids.ann, recipientId: ids.cy }, { participants, intros })).rejects.toThrow(/not in the event/); + }); + + it("never reads or answers one event's intro through another", async () => { + expect(await intros.findById(event.b, introA)).toBeNull(); + await expect(intros.respond(event.b, introA, ids.sam, "declined")).rejects.toThrow(); + expect(await intros.listForParticipant(event.b, ids.sam)).toEqual([]); + expect(await intros.getRevealedContacts(event.b, ids.sam)).toEqual([]); + expect((await intros.findById(event.a, introA))!.status).toBe("accepted"); + }); + + it("keeps location requests inside their event", async () => { + expect(await locations.forParticipant(event.b, ids.sam)).toEqual([]); + expect(await locations.answer({ eventId: event.b, requestId: locationA, recipientId: ids.sam, zone: "Cafe" })).toBe(false); + expect(await locations.create({ eventId: event.b, introId: introA, requesterId: ids.ann, recipientId: ids.sam })).toBeNull(); + expect((await locations.forParticipant(event.a, ids.sam))[0]).toMatchObject({ status: "pending" }); + }); + + it("removing someone from one event leaves their other event alone", async () => { + await events.setRevoked(event.b, ids.sam, true); + expect(await participants.hasEventAccess(event.b, ids.sam)).toBe(false); + expect(await participants.hasEventAccess(event.a, ids.sam)).toBe(true); + expect(await intros.getRevealedContacts(event.a, ids.ann)).toHaveLength(1); + }); +}); diff --git a/tests/unit/auth-config.test.ts b/tests/unit/auth-config.test.ts new file mode 100644 index 0000000..fcf1001 --- /dev/null +++ b/tests/unit/auth-config.test.ts @@ -0,0 +1,18 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { isGoogleSignInEnabled } from "@match-maker/auth"; + +afterEach(() => { + vi.unstubAllEnvs(); +}); + +describe("Google sign-in availability", () => { + it.each(["", " ", "not-configured"])("is off without a real Google client id: %j", (clientId) => { + vi.stubEnv("GOOGLE_CLIENT_ID", clientId); + expect(isGoogleSignInEnabled()).toBe(false); + }); + + it("is on once a Google client id is configured", () => { + vi.stubEnv("GOOGLE_CLIENT_ID", "1234.apps.googleusercontent.com"); + expect(isGoogleSignInEnabled()).toBe(true); + }); +}); diff --git a/tests/unit/auth-redirect.test.ts b/tests/unit/auth-redirect.test.ts index 06cfb8f..d6e00e1 100644 --- a/tests/unit/auth-redirect.test.ts +++ b/tests/unit/auth-redirect.test.ts @@ -1,5 +1,26 @@ import { describe, expect, it } from "vitest"; -import { safeReturnTo } from "../../apps/web/lib/safe-return-to"; +import { loginErrorPath, postLoginPath, safeReturnTo } from "../../apps/web/lib/safe-return-to"; + +describe("post-login destinations", () => { + it.each(["/login", "/login?returnTo=%2Fe%2Fdemo", "/login/", "/login#form"])("never returns to the login page: %s", (value) => { + expect(postLoginPath(value)).toBe("/"); + }); + + it("keeps ordinary destinations, including look-alike paths", () => { + expect(postLoginPath("/e/demo/matches")).toBe("/e/demo/matches"); + expect(postLoginPath("/loginhelp")).toBe("/loginhelp"); + }); + + it("builds an error URL that keeps the destination", () => { + expect(loginErrorPath("unregistered", "/e/demo/profile")).toBe("/login?error=unregistered&returnTo=%2Fe%2Fdemo%2Fprofile"); + }); + + it("omits an empty, external, or looping destination from the error URL", () => { + expect(loginErrorPath("provider")).toBe("/login?error=provider"); + expect(loginErrorPath("provider", "https://outside.example")).toBe("/login?error=provider"); + expect(loginErrorPath("provider", "/login")).toBe("/login?error=provider"); + }); +}); describe("login return destinations", () => { it.each([ diff --git a/tests/unit/auth-routes.test.ts b/tests/unit/auth-routes.test.ts index fb40a08..a7abde7 100644 --- a/tests/unit/auth-routes.test.ts +++ b/tests/unit/auth-routes.test.ts @@ -6,7 +6,8 @@ const mocks = vi.hoisted(() => ({ issueChallenge: vi.fn(), sendLoginEmail: vi.fn(), verifyCode: vi.fn(), ensureUser: vi.fn(), setSession: vi.fn(), exchangeCodeForTokens: vi.fn(), verifyIdToken: vi.fn(), unsealOAuthState: vi.fn(), sealSession: vi.fn(), - buildAuthorizationUrl: vi.fn(), sealOAuthState: vi.fn(), + buildAuthorizationUrl: vi.fn(), sealOAuthState: vi.fn(), verifyToken: vi.fn(), + isGoogleSignInEnabled: vi.fn(), })); vi.mock("@match-maker/db", () => ({ @@ -59,6 +60,7 @@ beforeEach(() => { mocks.sealSession.mockResolvedValue("sealed-session"); mocks.buildAuthorizationUrl.mockResolvedValue("http://localhost:8080/realms/match-maker/protocol/openid-connect/auth?kc_idp_hint=google"); mocks.sealOAuthState.mockResolvedValue("sealed-state"); + mocks.isGoogleSignInEnabled.mockReturnValue(true); }); describe("on-site email and phone login", () => { @@ -98,6 +100,13 @@ describe("Google sign-in", () => { expect(mocks.sealOAuthState).toHaveBeenCalledWith(expect.objectContaining({ returnTo: "/e/demo" })); }); + it("refuses to start Google when it is not configured", async () => { + mocks.isGoogleSignInEnabled.mockReturnValue(false); + const response = await startGoogle(new NextRequest("http://localhost:3000/api/auth/google/start?returnTo=%2Fe%2Fdemo")); + expect(response.headers.get("location")).toBe("http://localhost:3000/login?error=google_unavailable&returnTo=%2Fe%2Fdemo"); + expect(mocks.buildAuthorizationUrl).not.toHaveBeenCalled(); + }); + it("returns a verified invited account to its event with a session", async () => { const response = await callback(callbackRequest()); expect(response.headers.get("location")).toBe("http://localhost:3000/e/demo"); @@ -135,3 +144,38 @@ describe("Google sign-in", () => { expect(mocks.exchangeCodeForTokens).not.toHaveBeenCalled(); }); }); + +describe("failed sign-in keeps the event destination", () => { + it("returns a cancelled Google consent to login for the same event", async () => { + const response = await callback(new NextRequest("http://localhost:3000/auth/callback?error=access_denied", { + headers: { cookie: "mb_oauth=sealed-state" }, + })); + expect(response.headers.get("location")).toBe("http://localhost:3000/login?error=provider&returnTo=%2Fe%2Fdemo"); + expect(response.cookies.get("mb_oauth")?.value).toBe(""); + }); + + it("keeps the destination when the roster rejects a Google account", async () => { + mocks.findByEmail.mockResolvedValue(null); + const response = await callback(callbackRequest()); + expect(response.headers.get("location")).toBe("http://localhost:3000/login?error=unregistered&returnTo=%2Fe%2Fdemo"); + }); + + it("keeps the destination when token exchange fails", async () => { + mocks.exchangeCodeForTokens.mockRejectedValue(new Error("keycloak down")); + vi.spyOn(console, "error").mockImplementation(() => {}); + const response = await callback(callbackRequest()); + expect(response.headers.get("location")).toBe("http://localhost:3000/login?error=verification_failed&returnTo=%2Fe%2Fdemo"); + }); + + it("keeps the magic link destination when the link has expired", async () => { + mocks.verifyToken.mockResolvedValue({ ok: false, reason: "expired" }); + const response = await callback(new NextRequest("http://localhost:3000/auth/callback?token=old&returnTo=%2Fe%2Fdemo%2Fprofile")); + expect(response.headers.get("location")).toBe("http://localhost:3000/login?error=expired&returnTo=%2Fe%2Fdemo%2Fprofile"); + }); + + it("keeps the destination when Google start rejects an uninvited email", async () => { + mocks.findByEmail.mockResolvedValue(null); + const response = await startGoogle(new NextRequest("http://localhost:3000/api/auth/google/start?email=x%40example.com&returnTo=%2Fe%2Fdemo")); + expect(response.headers.get("location")).toBe("http://localhost:3000/login?error=unregistered&returnTo=%2Fe%2Fdemo"); + }); +}); diff --git a/tests/unit/candidate-eligibility.test.ts b/tests/unit/candidate-eligibility.test.ts index 5ac19bc..72a85e2 100644 --- a/tests/unit/candidate-eligibility.test.ts +++ b/tests/unit/candidate-eligibility.test.ts @@ -31,11 +31,16 @@ describe("matching pipeline viewer", () => { const participants = { findProfile: vi.fn(async () => viewer), listCandidates: vi.fn(async () => [eligible, hidden, incomplete]), + findCandidatesByIds: vi.fn(async (_event: string, ids: string[]) => [eligible, hidden, incomplete].filter((p) => ids.includes(p.attendeeId))), + corpusRevision: vi.fn(async () => 1), setEmbedding: vi.fn(), } as unknown as ParticipantRepository; const matches = { save: vi.fn(), listForParticipant: vi.fn() } satisfies MatchRepository; - const embeddings = { embed: vi.fn(async () => null) }; - const cache = { get: vi.fn(), set: vi.fn() }; + const embeddings = { id: "test-embeddings", embed: vi.fn(async () => null) }; + const cache = { + get: vi.fn(async () => null), set: vi.fn(async () => {}), getVector: vi.fn(async () => null), setVector: vi.fn(async () => {}), + acquire: vi.fn(async () => "token"), release: vi.fn(async () => {}), + }; return { pipeline: new MatchingPipeline(participants, matches, embeddings, null, cache), matches }; } const run = (pipeline: MatchingPipeline, mode: "profile" | "search") => @@ -64,7 +69,9 @@ describe("intro request recipient", () => { hasEventAccess: vi.fn(async () => true), findProfile: vi.fn(async (_eventId: string, id: string) => (id === "requester" ? profile("requester") : recipient)), } as unknown as ParticipantRepository; - const intros = { create: vi.fn(async () => ({ id: "intro" })) } as unknown as IntroRepository; + const intros = { + create: vi.fn(async () => ({ id: "intro" })), findBetween: vi.fn(async () => []), countPendingOutgoing: vi.fn(async () => 0), + } as unknown as IntroRepository; return { participants, intros }; } const send = (dependencies: ReturnType, recipientId: string) => diff --git a/tests/unit/capacity-check.test.ts b/tests/unit/capacity-check.test.ts new file mode 100644 index 0000000..bcaad9c --- /dev/null +++ b/tests/unit/capacity-check.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from "vitest"; +import { budgetFailures, percentile, type CapacityReport } from "../../tools/capacity-check"; +import { HashEmbeddingProvider } from "@/src/infrastructure/providers/hash-embedding.provider"; + +const report = (overrides: Partial = {}): CapacityReport => ({ + attendees: 300, searches: 60, firstListsSeconds: 30, quietSeconds: 60, attendeesWithoutMatches: 0, + search: { p50Ms: 200, p95Ms: 900, maxMs: 1200, failures: 0 }, + modelCalls: { perAttendee: 2, perSearch: 1, approxTokens: 1000, failures: 0 }, + ...overrides, +}); + +describe("capacity budgets", () => { + it("passes a report within every budget", () => { + expect(budgetFailures(report())).toEqual([]); + }); + + it("names each budget a report misses", () => { + const failures = budgetFailures(report({ + search: { p50Ms: 3000, p95Ms: 7000, maxMs: 9000, failures: 2 }, + attendeesWithoutMatches: 1, + modelCalls: { perAttendee: 9, perSearch: 1, approxTokens: 0, failures: 0 }, + })); + expect(failures).toEqual([ + "search p95 7000 ms is over the budget of 6000 ms", + "model calls per attendee 9 is over the budget of 6", + "2 search(es) failed", + "1 attendee(s) have no matches", + ]); + }); + + it("takes nearest-rank percentiles", () => { + const values = Array.from({ length: 100 }, (_, i) => i + 1); + expect(percentile(values, 50)).toBe(50); + expect(percentile(values, 95)).toBe(95); + expect(percentile([], 95)).toBe(0); + }); +}); + +describe("hash embedding", () => { + it("gives unit vectors that are closer for similar texts", async () => { + const provider = new HashEmbeddingProvider(); + const [a, b, c] = await Promise.all(["graph databases in rust", "rust graph databases", "watercolor painting"].map((text) => provider.embed(text))); + const dot = (x: number[], y: number[]) => x.reduce((sum, value, i) => sum + value * y[i], 0); + expect(a).toHaveLength(768); + expect(dot(a!, a!)).toBeCloseTo(1); + expect(dot(a!, b!)).toBeGreaterThan(dot(a!, c!)); + expect(await provider.embed(" ")).toBeNull(); + }); +}); diff --git a/tests/unit/embedding-versions.test.ts b/tests/unit/embedding-versions.test.ts new file mode 100644 index 0000000..e57200b --- /dev/null +++ b/tests/unit/embedding-versions.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, it, vi } from "vitest"; +import { SemanticScorer } from "@/src/domain/matching/scorers/semantic.scorer"; +import { queryRelevance } from "@/src/domain/matching/search-query"; +import { MatchingPipeline } from "@/src/domain/matching/pipeline"; +import type { ParticipantRepository } from "@/src/application/ports/participant-repository"; +import { Intent } from "@/src/domain/participant/intent"; +import type { ParticipantProfile } from "@/src/domain/participant/participant.entity"; + +const person = (attendeeId: string, overrides: Partial = {}): ParticipantProfile => ({ + eventId: "event", attendeeId, name: attendeeId, designation: null, domain: "AI", aboutMe: "builds search tools", selfDescription: null, + yearsOfExperience: null, mostImpressiveBuild: null, intent: Intent.NETWORKING, interests: [], venueZone: null, visible: true, + profileComplete: true, revision: 1, ...overrides, +}); + +describe("embeddings from different models", () => { + it("are never compared by the semantic scorer", () => { + const scorer = new SemanticScorer(); + const a = person("a", { embedding: [1, 0], embeddingModel: "model-a" }); + expect(scorer.score(a, person("b", { embedding: [1, 0], embeddingModel: "model-a" }))).toBeCloseTo(1); + // Same numbers, other model: falls back to the text overlap instead of a meaningless cosine. + expect(scorer.score(a, person("c", { embedding: [1, 0], embeddingModel: "model-b", aboutMe: "unrelated words" }))).toBeLessThan(0.5); + }); + + it("make query relevance fall back to words", () => { + const candidate = person("c", { embedding: [1, 0], embeddingModel: "old-model" }); + expect(queryRelevance(["nothing"], [1, 0], candidate, "new-model")).toBe(0); + expect(queryRelevance(["search"], [1, 0], candidate, "new-model")).toBe(1); + expect(queryRelevance(["nothing"], [1, 0], { ...candidate, embeddingModel: "new-model" }, "new-model")).toBeCloseTo(1); + }); + + it("are replaced: a profile embedded by another model is embedded again", async () => { + const viewer = person("viewer", { embedding: [0, 1], embeddingModel: "old-model" }); + const participants = { + findProfile: vi.fn(async () => viewer), + listCandidates: vi.fn(async () => []), + setEmbedding: vi.fn(), + } as unknown as ParticipantRepository; + const embeddings = { id: "new-model", embed: vi.fn(async () => [1, 0]) }; + const matches = { save: vi.fn(async () => true), listForParticipant: vi.fn() }; + const cache = { get: vi.fn(), set: vi.fn(), getVector: vi.fn(), setVector: vi.fn(), acquire: vi.fn(), release: vi.fn() }; + await new MatchingPipeline(participants, matches, embeddings, null, cache).run({ eventId: "event", participantId: "viewer", mode: "profile" }); + expect(embeddings.embed).toHaveBeenCalledWith(expect.any(String), "profile"); + expect(participants.setEmbedding).toHaveBeenCalledWith("event", "viewer", [1, 0], "new-model"); + expect(participants.listCandidates).toHaveBeenCalledWith(expect.objectContaining({ embedding: [1, 0], embeddingModel: "new-model" })); + }); +}); diff --git a/tests/unit/env.test.ts b/tests/unit/env.test.ts index c0a5807..29a12fb 100644 --- a/tests/unit/env.test.ts +++ b/tests/unit/env.test.ts @@ -7,6 +7,7 @@ const validWeb = { KEYCLOAK_ISSUER: "http://localhost:8080/realms/match-maker", KEYCLOAK_CLIENT_ID: "match-maker-web", KEYCLOAK_CLIENT_SECRET: "secret", + EMAIL_DEV_FALLBACK: "true", }; describe("checkEnv", () => { @@ -16,7 +17,7 @@ describe("checkEnv", () => { it("lists every missing web setting at once", () => { const problems = checkEnv("web", {}); - expect(problems).toHaveLength(5); + expect(problems).toHaveLength(6); expect(problems.join("\n")).toContain("SESSION_SECRET is required"); expect(problems.join("\n")).toContain("KEYCLOAK_CLIENT_SECRET is required"); }); @@ -46,7 +47,7 @@ describe("checkEnv", () => { expect(problems).toEqual([ 'APP_BASE_URL must be an http(s) URL, got "localhost:3000".', 'DATABASE_POOL_MAX must be a positive integer, got "ten".', - 'DATABASE_SSL must be one of disable, require, got "on".', + 'DATABASE_SSL must be one of disable, require, verify, got "on".', ]); }); @@ -69,6 +70,37 @@ describe("checkEnv", () => { }); }); +describe("login e-mail settings", () => { + const { EMAIL_DEV_FALLBACK: _fallback, ...production } = validWeb; + + it("refuses to boot the web app when login e-mail cannot be delivered", () => { + expect(checkEnv("web", production)).toEqual([ + "RESEND_API_KEY is required to send login e-mail (EMAIL_DEV_FALLBACK=true logs codes instead, local only).", + ]); + expect(checkEnv("web", { ...production, EMAIL_DEV_FALLBACK: "false" })).toHaveLength(1); + }); + + it("does not require e-mail settings for the worker", () => { + expect(checkEnv("worker", { DATABASE_URL: validWeb.DATABASE_URL })).toEqual([]); + }); + + it("requires a sender address once Resend is configured", () => { + expect(checkEnv("web", { ...production, RESEND_API_KEY: "re_key" })).toEqual([ + "EMAIL_FROM is required when RESEND_API_KEY is set, using a sender on a domain verified in Resend.", + ]); + }); + + it.each(["login@bridge.example", "Mr. Bridge "])("accepts the sender %s", (from) => { + expect(checkEnv("web", { ...production, RESEND_API_KEY: "re_key", EMAIL_FROM: from })).toEqual([]); + }); + + it.each(["Mr. Bridge", "Mr. Bridge ", "login@localhost"])("rejects the sender %s", (from) => { + expect(checkEnv("web", { ...production, RESEND_API_KEY: "re_key", EMAIL_FROM: from })).toEqual([ + `EMAIL_FROM must be "address@domain" or "Name ", got "${from}".`, + ]); + }); +}); + describe("assertEnv", () => { it("throws one error naming every problem", () => { expect(() => assertEnv("web", {})).toThrowError(/Invalid web configuration:[\s\S]*DATABASE_URL[\s\S]*SESSION_SECRET/); diff --git a/tests/unit/explanation-cache.test.ts b/tests/unit/explanation-cache.test.ts new file mode 100644 index 0000000..754bd7c --- /dev/null +++ b/tests/unit/explanation-cache.test.ts @@ -0,0 +1,124 @@ +import { describe, expect, it, vi } from "vitest"; +import { LlmRerankStage } from "@/src/domain/matching/stages/llm-rerank.stage"; +import { explanationKey, queryContext } from "@/src/domain/matching/explanation-key"; +import type { ExplanationCache } from "@/src/application/ports/explanation-cache"; +import type { LlmProvider } from "@/src/application/ports/llm-provider"; +import type { MatchCandidate } from "@/src/domain/matching/match-strategy"; +import { Intent } from "@/src/domain/participant/intent"; +import type { ParticipantProfile } from "@/src/domain/participant/participant.entity"; + +const person = (attendeeId: string, profileRevision = "r1"): ParticipantProfile => ({ + eventId: "event", attendeeId, name: attendeeId, designation: null, domain: "AI", aboutMe: null, selfDescription: null, + yearsOfExperience: null, mostImpressiveBuild: null, intent: Intent.NETWORKING, interests: [], venueZone: null, visible: true, + profileComplete: true, embedding: null, profileRevision, +}); +const candidate = (profile: ParticipantProfile, score: number): MatchCandidate => ({ participant: profile, score, scoreBreakdown: {}, mutualInterest: false, reason: null }); + +function memoryCache(): ExplanationCache & { store: Map; orders: Map } { + const store = new Map(); + const orders = new Map(); + return { + store, + orders, + getMany: vi.fn(async (keys: string[]) => new Map(keys.filter((key) => store.has(key)).map((key) => [key, store.get(key)!]))), + setMany: vi.fn(async (entries) => { for (const entry of entries) store.set(entry.key, entry.reason); }), + getOrder: vi.fn(async (key: string) => orders.get(key) ?? null), + setOrder: vi.fn(async (entry) => { orders.set(entry.key, entry.candidateIds); }), + }; +} + +function llm(): LlmProvider & { rerank: ReturnType } { + return { + id: "test:model", + promptVersion: "p1", + // Reverses the order and writes a reason naming the candidate. + rerank: vi.fn(async (_viewer: ParticipantProfile, list: MatchCandidate[]) => + [...list].reverse().map((item) => ({ ...item, reason: `LLM says ${item.participant.attendeeId}` }))), + }; +} + +const viewer = person("viewer"); +const shortlist = () => [candidate(person("a"), 90), candidate(person("b"), 80)]; +const order = (result: { candidates: MatchCandidate[] }) => result.candidates.map((item) => item.participant.attendeeId); + +describe("pair explanation cache", () => { + it("stores the model's reasons, then reuses them without calling the model", async () => { + const cache = memoryCache(); + const provider = llm(); + const stage = new LlmRerankStage(provider, cache); + + const first = await stage.execute(viewer, shortlist()); + expect(order(first)).toEqual(["b", "a"]); + expect(first.rankedBy).toBe("llm:test:model"); + expect(cache.store.size).toBe(2); + + const second = await stage.execute(viewer, shortlist()); + expect(provider.rerank).toHaveBeenCalledTimes(1); + expect(second.candidates.map((item) => item.reason)).toEqual(["LLM says b", "LLM says a"]); + // The model's order for this exact shortlist is kept, so a refresh does not reshuffle. + expect(order(second)).toEqual(["b", "a"]); + expect(second.rankedBy).toBe("llm:test:model"); + }); + + it("does not apply a stored order to a different shortlist", async () => { + const cache = memoryCache(); + const provider = llm(); + const stage = new LlmRerankStage(provider, cache); + await stage.execute(viewer, shortlist()); + await stage.execute(viewer, [candidate(person("b"), 80)]); + await stage.execute(viewer, [candidate(person("a"), 90)]); + // Every pair is cached, but this pair of people was never ordered as one list. + cache.orders.clear(); + const result = await stage.execute(viewer, shortlist()); + expect(provider.rerank).toHaveBeenCalledTimes(1); + expect(order(result)).toEqual(["a", "b"]); + expect(result.rankedBy).toBe("score"); + }); + + it("asks the model again when a profile changes", async () => { + const cache = memoryCache(); + const provider = llm(); + const stage = new LlmRerankStage(provider, cache); + await stage.execute(viewer, shortlist()); + await stage.execute(viewer, [candidate(person("a", "r2"), 90), candidate(person("b"), 80)]); + expect(provider.rerank).toHaveBeenCalledTimes(2); + }); + + it("never reuses one viewer's explanations for another", async () => { + const cache = memoryCache(); + const provider = llm(); + const stage = new LlmRerankStage(provider, cache); + await stage.execute(viewer, shortlist()); + await stage.execute(person("someone-else"), shortlist()); + expect(provider.rerank).toHaveBeenCalledTimes(2); + }); + + it("keeps query explanations apart from profile ones and from other queries", () => { + const base = { eventId: "event", viewer, candidate: person("a"), model: "m", promptVersion: "p1" }; + const profile = explanationKey({ ...base, context: "profile" }); + const climate = explanationKey({ ...base, context: queryContext(" Climate tools") }); + expect(climate).toBe(explanationKey({ ...base, context: queryContext("climate tools") })); + expect(new Set([profile, climate, explanationKey({ ...base, context: queryContext("finance") })]).size).toBe(3); + expect(explanationKey({ ...base, context: "profile", promptVersion: "p2" })).not.toBe(profile); + }); + + it("falls back to cached reasons when the model fails", async () => { + const cache = memoryCache(); + const provider = llm(); + const stage = new LlmRerankStage(provider, cache); + await stage.execute(viewer, [candidate(person("a"), 90)]); + provider.rerank.mockRejectedValueOnce(new Error("timeout")); + vi.spyOn(console, "warn").mockImplementation(() => {}); + const result = await stage.execute(viewer, shortlist()); + expect(result.candidates.map((item) => item.reason)).toEqual(["LLM says a", expect.stringMatching(/perspective|fit/)]); + }); + + it("keeps working when the cache itself fails", async () => { + const provider = llm(); + vi.spyOn(console, "warn").mockImplementation(() => {}); + const down = () => vi.fn().mockRejectedValue(new Error("down")); + const broken: ExplanationCache = { getMany: down(), setMany: down(), getOrder: down(), setOrder: down() }; + const result = await new LlmRerankStage(provider, broken).execute(viewer, shortlist()); + expect(result.rankedBy).toBe("llm:test:model"); + }); +}); diff --git a/tests/unit/intro-rules.test.ts b/tests/unit/intro-rules.test.ts new file mode 100644 index 0000000..e6ffc3e --- /dev/null +++ b/tests/unit/intro-rules.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it, vi } from "vitest"; +import { INTRO_LIMITS, introRequestRefusal } from "@/src/domain/connection/connection.rules"; +import { requestIntro } from "@/src/application/use-cases/request-intro"; +import type { IntroRequest } from "@/src/domain/connection/intro-request.entity"; + +const intro = (requesterId: string, recipientId: string, status: IntroRequest["status"]): IntroRequest => ({ + id: `${requesterId}-${recipientId}`, eventId: "event", requesterId, recipientId, status, message: null, createdAt: new Date(), respondedAt: null, +}); + +describe("intro request rules", () => { + it.each([ + [[], null], + [[intro("a", "b", "pending")], /already asked to meet them/], + [[intro("a", "b", "declined")], /declined an earlier request/], + [[intro("b", "a", "pending")], /already asked to meet you/], + [[intro("b", "a", "accepted")], /already connected/], + [[intro("b", "a", "declined")], null], + ] as const)("refuses or allows a request from a given %o", (existing, expected) => { + const refusal = introRequestRefusal("a", [...existing]); + if (expected === null) expect(refusal).toBeNull(); + else expect(refusal).toMatch(expected); + }); +}); + +function deps(overrides: { existing?: IntroRequest[]; pending?: number; allowed?: boolean } = {}) { + const profile = (id: string) => ({ attendeeId: id, name: id, visible: true, profileComplete: true, email: `${id}@example.com` }); + return { + participants: { hasEventAccess: vi.fn(async () => true), findProfile: vi.fn(async (_e: string, id: string) => profile(id)) }, + intros: { + findBetween: vi.fn(async () => overrides.existing ?? []), + countPendingOutgoing: vi.fn(async () => overrides.pending ?? 0), + create: vi.fn(async () => intro("a", "b", "pending")), + }, + limiter: { consume: vi.fn(async () => overrides.allowed ?? true) }, + }; +} +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const ask = (dependencies: ReturnType) => requestIntro({ eventId: "event", requesterId: "a", recipientId: "b" }, dependencies as any); + +describe("requestIntro abuse controls", () => { + it("creates a request when nothing blocks it", async () => { + const d = deps(); + await ask(d); + expect(d.intros.create).toHaveBeenCalled(); + expect(d.limiter.consume).toHaveBeenCalledWith("intro:event:a", INTRO_LIMITS.perHour, 3600); + }); + + it("refuses a request to yourself", async () => { + const d = deps(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await expect(requestIntro({ eventId: "event", requesterId: "a", recipientId: "a" }, d as any)).rejects.toThrow(/yourself/); + }); + + it("refuses after a decline, with too many pending, or over the hourly limit", async () => { + await expect(ask(deps({ existing: [intro("a", "b", "declined")] }))).rejects.toThrow(/declined/); + await expect(ask(deps({ pending: INTRO_LIMITS.pendingOutgoing }))).rejects.toThrow(/waiting for an answer/); + await expect(ask(deps({ allowed: false }))).rejects.toThrow(/this hour/); + }); +}); diff --git a/tests/unit/invitations.test.ts b/tests/unit/invitations.test.ts new file mode 100644 index 0000000..595497e --- /dev/null +++ b/tests/unit/invitations.test.ts @@ -0,0 +1,106 @@ +import { describe, expect, it, vi } from "vitest"; +import { canInvite, invitationState, RESEND_AFTER_MINUTES, type InvitationFacts } from "@/src/domain/participant/invitation"; +import { sendInvitations, setAttendeeAccess } from "@/src/application/use-cases/manage-invitations"; +import { renderInvitationEmail } from "@match-maker/email"; + +const NOW = Date.parse("2026-10-19T12:00:00Z"); +const facts = (overrides: Partial): InvitationFacts => ({ + revokedAt: null, profileUpdatedAt: null, claimedAt: null, invitedAt: null, inviteError: null, ...overrides, +}); +const at = (minutesAgo: number) => new Date(NOW - minutesAgo * 60_000); + +describe("invitationState", () => { + it.each([ + [facts({}), "not_invited"], + [facts({ invitedAt: at(1) }), "invited"], + [facts({ inviteError: "bounced" }), "failed"], + [facts({ invitedAt: at(5), claimedAt: at(1) }), "claimed"], + [facts({ claimedAt: at(5), profileUpdatedAt: at(1) }), "complete"], + [facts({ profileUpdatedAt: at(5), revokedAt: at(1) }), "removed"], + ] as const)("%o is %s", (input, state) => { + expect(invitationState(input)).toBe(state); + }); + + it("allows a resend only after the waiting period, and never after claiming or removal", () => { + expect(canInvite(facts({}), NOW)).toBe(true); + expect(canInvite(facts({ invitedAt: at(RESEND_AFTER_MINUTES - 1) }), NOW)).toBe(false); + expect(canInvite(facts({ invitedAt: at(RESEND_AFTER_MINUTES) }), NOW)).toBe(true); + expect(canInvite(facts({ claimedAt: at(1) }), NOW)).toBe(false); + expect(canInvite(facts({ revokedAt: at(1) }), NOW)).toBe(false); + }); +}); + +function ports(targets: Array<{ attendee_id: string; email: string; name: string | null }>, fail: string[] = []) { + const events = { + invitationTargets: vi.fn().mockResolvedValue(targets), + recordInvitation: vi.fn().mockResolvedValue(undefined), + setRevoked: vi.fn().mockResolvedValue(true), + organizerRoles: vi.fn().mockResolvedValue({ event: "owner", organization: null }), + }; + const notifications = { + invitation: vi.fn(async ({ email }: { email: string }) => { + if (fail.includes(email)) throw new Error("Resend rejected the address"); + }), + }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return { events: events as any, notifications: notifications as any, raw: { events, notifications } }; +} + +const event = { eventId: "event", organizerId: "organizer", eventName: "Summit", organizationName: "Acme", eventUrl: "https://bridge.example.com/e/summit" }; + +describe("sendInvitations", () => { + it("sends to every target and records successes and failures separately", async () => { + const { events, notifications, raw } = ports([ + { attendee_id: "a", email: "a@example.com", name: "A" }, + { attendee_id: "b", email: "bad@example.com", name: null }, + ], ["bad@example.com"]); + await expect(sendInvitations(event, { events, notifications })).resolves.toEqual({ sent: 1, failed: 1 }); + expect(raw.events.invitationTargets).toHaveBeenCalledWith("event", null, RESEND_AFTER_MINUTES); + expect(raw.events.recordInvitation).toHaveBeenCalledWith("event", "a", null); + expect(raw.events.recordInvitation).toHaveBeenCalledWith("event", "b", "Resend rejected the address"); + }); + + it("explains why a single resend is refused", async () => { + const { events, notifications } = ports([]); + await expect(sendInvitations({ ...event, attendeeId: "a" }, { events, notifications })).rejects.toThrow(/cannot be invited right now/); + }); +}); + +describe("setAttendeeAccess", () => { + it("refuses someone not on the roster", async () => { + const { events, raw } = ports([]); + raw.events.setRevoked.mockResolvedValue(false); + await expect(setAttendeeAccess({ eventId: "event", organizerId: "organizer", attendeeId: "x", revoked: true }, { events })) + .rejects.toThrow(/not on the event roster/); + }); + + it("refuses anyone who is not an organizer of the event", async () => { + const { events, notifications, raw } = ports([{ attendee_id: "a", email: "a@example.com", name: "A" }]); + raw.events.organizerRoles.mockResolvedValue({ event: null, organization: null }); + await expect(setAttendeeAccess({ eventId: "event", organizerId: "x", attendeeId: "a", revoked: true }, { events })).rejects.toThrow(/not an organizer/); + await expect(sendInvitations({ ...event, organizerId: "x" }, { events, notifications })).rejects.toThrow(/not an organizer/); + expect(raw.events.setRevoked).not.toHaveBeenCalled(); + expect(raw.notifications.invitation).not.toHaveBeenCalled(); + }); +}); + +describe("audit records", () => { + it("records invitations sent and access changes", async () => { + const { events, notifications } = ports([{ attendee_id: "a", email: "a@example.com", name: "A" }]); + const audit = { record: vi.fn(async () => {}), recent: vi.fn() }; + await sendInvitations(event, { events, notifications, audit }); + await setAttendeeAccess({ eventId: "event", organizerId: "organizer", attendeeId: "a", revoked: true }, { events, audit }); + expect(audit.record).toHaveBeenCalledWith(expect.objectContaining({ action: "invitations.sent", actorId: "organizer", details: expect.objectContaining({ sent: 1 }) })); + expect(audit.record).toHaveBeenCalledWith(expect.objectContaining({ action: "access.removed", subjectId: "a" })); + }); +}); + +describe("renderInvitationEmail", () => { + it("links to the event and escapes names in the HTML", () => { + const mail = renderInvitationEmail({ name: "