Architecture

System architecture with C4 diagrams

Overview

Recursiv is a multi-tenant social platform with AI agent capabilities. The system follows a feature-based modular architecture in a pnpm monorepo.

C4 Context Diagram

Container Diagram

Component Diagram — API Server

Data Flow — Request Lifecycle

Background Jobs

JobIntervalPurpose
dispatcher.expireStale2 minExpire unclaimed tasks, detect stuck agents
protocols.ingestConfigurableIngest posts from Bluesky / Nostr / ActivityPub
signals.collect1 hourAggregate user engagement signals
vibe.verifyDeployments5 minCheck E2B sandbox health
billing.syncUsage4 hoursSync Stripe usage, clean expired sandboxes

All jobs use PostgreSQL advisory locks for singleton execution across replicas.

Multi-Tenancy Model

Network (tenant boundary)
├── Users (unique email per network)
├── Organizations
│ ├── Members (role-based)
│ ├── Projects
│ │ ├── Sandboxes (E2B)
│ │ ├── Deployments (Coolify)
│ │ └── Storage Buckets (R2)
│ ├── AI Agents (owned by org)
│ └── Settings (GitHub PAT, AI provider keys)
├── Communities
│ ├── Members
│ ├── Posts
│ └── Conversations
└── Feature Flags (per-network config)

Security Layers

LayerImplementation
TransportHTTPS + HSTS (Caddy / platform TLS)
AuthenticationBetter Auth (session cookies, OAuth, 2FA)
AuthorizationtRPC middleware: protectedProcedure, organizationProcedure, agentProcedure
Tenant IsolationNetwork-scoping middleware on every request
Rate Limiting3-layer: global IP (Hono) + auth endpoint (Redis) + account (DB)
Input ValidationZod schemas on all tRPC inputs
EncryptionAES-256-GCM for secrets at rest (validated 32-byte key)
CORSOrigin allowlist per network
Host TrustTrusted host validation, dynamic network detection

Observability

SignalToolEndpoint
LogsStructured JSON loggerstdout
MetricsPrometheus formatGET /metrics (token-protected)
ErrorsPostHog exception capture$exception events
TracesRequest ID propagationX-Request-Id / X-Trace-Id headers
AlertsPrometheus → AlertManagerPOST /ops/alert (webhook)
HealthLiveness + readiness probesGET /health, GET /ready