Implemented architecture

T3 Connect Control Plane and Managed Endpoint Flow

T3 Connect links a locally authorized T3 environment to a signed-in cloud user, provisions a managed HTTPS/WSS endpoint for that environment, and brokers proof-bound remote connections. The local environment remains the authority that issues environment access credentials. The hosted relay stores links, reconciles managed endpoint resources, validates signed proofs, and delivers agent-activity notifications.

Client -> Relay: Clerk bearer or relay DPoP Relay -> Environment: relay-signed health and mint proofs Environment -> Relay: environment bearer plus signed activity proof Client -> Environment: environment access token plus DPoP

Security Invariant

A cloud identity is not an environment login. Remote access requires all of the following:

  • a Clerk-authenticated T3 Connect user;
  • an active relay link for that user and environment;
  • a DPoP proof key held by the remote client;
  • a relay-signed mint request accepted by the linked local environment; and
  • an environment-issued credential bound to the remote client's DPoP thumbprint.
The relay cannot mint an environment access token by itself. It can only ask the local environment to mint a short-lived bootstrap credential for an authorized cloud user.

During an honest connection flow, the relay cannot act as the remote client:

  • Normal environment API traffic goes directly from the remote client through the managed endpoint. The relay does not receive the resulting environment access token.
  • The one-time bootstrap credential and resulting environment access token are bound to the remote client's DPoP public-key thumbprint. Redeeming or using them requires request proofs signed by the corresponding private key, which stays on the client.
  • Relay-signed health and mint proofs are short-lived, audience-bound, scoped, and replay-guarded. They are accepted only by the narrow cloud endpoints, not as normal environment API credentials.
  • Environment health and mint responses are signed by the linked environment and bound to the request nonce, so a different process behind the tunnel cannot impersonate the linked environment.
The hosted relay remains a trusted mint broker: it holds the cloud-mint signing key. DPoP prevents credential reuse by the relay or tunnel transport during an honest flow, but it does not make compromise of the relay signing authority harmless.

Product Boundaries

The generic local connector is called the relay client. The current implementation layer uses cloudflared, but that is an internal transport detail rather than the product contract.

  • The relay Worker owns cloud links, managed endpoint allocations, and notifications.
  • The environment server owns local sessions, environment keys, and access tokens.
  • The relay client exposes only the environment's loopback HTTP server.
  • Web and mobile clients connect directly to the managed environment endpoint after bootstrap.

Current Topology

flowchart LR
  subgraph Clients["User-facing clients"]
    Web["Desktop / hosted web UI"]
    Mobile["Mobile app"]
    CLI["Headless CLI"]
  end

  Clerk["Clerk
user identity and OAuth"] subgraph Relay["T3 Code Relay on Cloudflare"] Worker["Relay Worker
HTTP API"] Queue["APNs delivery queue
with dead-letter queue"] Hyperdrive["Hyperdrive"] CF["Cloudflare tunnel + DNS APIs"] end DB["PlanetScale Postgres
prod database + stage branches"] APNs["Apple Push Notification service"] Traces["Axiom OTLP traces"] subgraph Local["Linked T3 environment"] Env["Environment server
local auth authority"] RelayClient["Relay client
cloudflared implementation"] Secrets["Server secret store
keys + relay config"] end Endpoint["Managed HTTPS/WSS endpoint"] Web -->|"Clerk bearer / relay DPoP"| Worker Mobile -->|"Clerk bearer / relay DPoP"| Worker CLI -->|"Clerk OAuth bearer"| Worker Web --> Clerk Mobile --> Clerk CLI -->|"PKCE authorization code flow"| Clerk Worker --> Hyperdrive --> DB Worker --> Traces Worker --> CF Worker --> Queue --> APNs Worker --> Endpoint Endpoint --> RelayClient -->|"http://127.0.0.1:port"| Env Env --> Secrets Web -->|"environment token + DPoP
HTTPS / WSS"| Endpoint Mobile -->|"environment token + DPoP
HTTPS / WSS"| Endpoint Env -->|"environment bearer + signed activity proof"| Worker

Authentication and Transport Matrix

Boundary Transport Authentication Purpose
Web or mobile -> relay link management HTTPS Clerk session-template bearer token Create a link challenge, submit an environment proof, list links, or unlink.
CLI -> relay link management HTTPS Clerk OAuth access token obtained with PKCE Reconcile the desired headless cloud link when the server starts.
Web or mobile -> relay protected client endpoints HTTPS Relay-issued DPoP token plus per-request DPoP proof Check environment status, request a remote connection, and register mobile devices.
Relay -> managed environment endpoint HTTPS over the managed tunnel Short-lived relay-signed JWT request proof Request a signed health response or ask the local environment to mint a credential.
Environment -> relay activity publication HTTPS Relay-issued environment bearer credential plus environment-signed JWT proof Publish redacted agent-activity state for push notifications and Live Activities.
Remote client -> environment HTTPS and WSS over the managed tunnel Environment-issued access token bound to the client DPoP key Use the normal environment HTTP APIs and request a one-time WebSocket ticket.

Credential Ownership

Credential Created by Stored by Use
Clerk session-template JWT Clerk Web or mobile client Authenticate cloud-user relay requests and bootstrap relay DPoP token exchange.
CLI OAuth access and refresh token Clerk OAuth Local server secret store Authorize headless CLI link reconciliation.
Environment Ed25519 key pair Local environment server Local server secret store Sign link proofs, health responses, mint responses, and activity publication proofs.
Relay cloud-mint key pair Relay deployment Private key in relay Worker config; public key installed into the environment Sign relay-to-environment health and mint requests.
Relay environment bearer credential Relay Hashed in relay Postgres; plaintext in local server secret store Authenticate environment-to-relay agent-activity publication.
Relay DPoP access token Relay Remote client memory cache Authorize status, connect, and mobile registration endpoints with proof of possession.
Environment bootstrap credential Local environment server Passed relay -> remote client One-time exchange at the environment /oauth/token endpoint.
Environment access token Local environment server Remote client Authorize environment HTTP operations and WebSocket ticket issuance.
WebSocket ticket Local environment server Remote client until consumed Short-lived one-time credential for the WSS upgrade.
Relay client connector token Cloudflare tunnel API Local server secret store Start the local relay client. It is passed through TUNNEL_TOKEN, not a shell argument.

Flow 1: Link From the Desktop Web UI

sequenceDiagram
  participant U as Signed-in user
  participant C as Desktop web UI
  participant E as Local environment
  participant R as Relay Worker
  participant CF as Cloudflare APIs
  participant RC as Local relay client

  U->>C: Enable T3 Connect for a locally paired environment
  C->>E: Check relay client availability
  alt relay client is missing
    C->>U: Prompt before download and install
    U->>C: Confirm
    C->>E: Stream relay-client install RPC
    E-->>C: Progress stages
  end
  C->>R: POST /v1/client/environment-link-challenges (Clerk bearer)
  R-->>C: Short-lived challenge
  C->>E: POST /api/connect/link-proof (local relay:write bearer)
  E->>E: Authenticate, reject forwarded authority, get or create key pair, validate loopback origin
  E-->>C: Environment-signed link proof
  C->>R: POST /v1/client/environment-links (Clerk bearer + proof)
  R->>R: Verify challenge, proof, capabilities, nonce, and loopback origin
  R->>CF: Reconcile tunnel, ingress, and CNAME
  R->>R: Upsert user/environment link and issue environment bearer
  R-->>C: Managed endpoint, relay config, and connector token
  C->>E: POST /api/connect/relay-config (local relay:write bearer)
  E->>RC: Start relay client with TUNNEL_TOKEN
          
The local link-proof endpoint requires relay:write, rejects forwarded authority headers, and accepts only an exact loopback origin. Environment keypair persistence is atomic across concurrent link attempts.

A locally paired mobile client uses the same relay challenge, local proof, relay link, and local relay-config endpoints. The desktop web UI is the surface that checks relay client availability and offers the managed install dialog.

Flow 2: Headless CLI Link

sequenceDiagram
  participant U as Operator
  participant CLI as t3 connect CLI
  participant Clerk as Clerk OAuth
  participant S as Environment server on next start
  participant R as Relay Worker
  participant RC as Local relay client

  U->>CLI: t3 connect link
  CLI->>CLI: Resolve relay client
  alt relay client is missing
    CLI->>U: Confirm managed relay-client install
    U->>CLI: Confirm
    CLI->>CLI: Install with terminal progress updates
  end
  CLI->>Clerk: Browser PKCE authorization-code flow
  Clerk-->>CLI: OAuth access and refresh tokens
  CLI->>CLI: Persist desired cloud-link state
  U->>S: Start T3 environment server
  S->>R: Create challenge and submit local signed proof
  R-->>S: Managed endpoint config and connector token
  S->>RC: Start relay client
          

t3 connect login stores authorization without enabling exposure. t3 connect unlink disables exposure while retaining authorization. t3 connect logout also removes the stored CLI authorization.

Flow 3: Remote Connect Bootstrap

sequenceDiagram
  participant C as Web or mobile client
  participant Clerk as Clerk
  participant R as Relay Worker
  participant E as Managed environment endpoint

  C->>Clerk: Get Clerk session-template JWT
  C->>R: POST /v1/client/dpop-token (Clerk JWT + DPoP proof)
  R-->>C: Relay DPoP access token
  C->>R: POST /v1/environments/:id/connect (relay DPoP)
  R->>E: POST /api/t3-connect/mint-credential (relay-signed proof)
  E->>E: Verify relay signer, linked user, scope, lifetime, cnf, and replay guards
  E-->>R: Bootstrap credential + environment-signed proof
  R->>R: Verify environment signature, nonce, endpoint, and DPoP binding
  R-->>C: Managed endpoint + bootstrap credential
  C->>E: POST /oauth/token (bootstrap credential + DPoP proof)
  E-->>C: Environment DPoP-bound access token
  C->>E: POST /api/auth/websocket-ticket
  E-->>C: One-time WebSocket ticket
  C->>E: WSS /ws with ticket
          
Relay-to-environment control requests disable redirects, use only a reconciled managed endpoint allocation, and require a signed environment response before the relay returns data to the client.

Flow 4: Agent Activity Notifications

sequenceDiagram
  participant E as Local environment
  participant R as Relay Worker
  participant DB as Relay Postgres
  participant Q as APNs queue
  participant APNs as Apple Push Notification service
  participant M as Mobile app

  E->>E: Project publishable thread state and redact failure detail
  E->>R: POST /v1/environments/:environmentId/threads/:threadId/agent-activity
  Note over E,R: Environment bearer credential + environment-signed per-state proof
  R->>R: Verify credential, signature, scope, expiry, and nonce
  R->>DB: Store current activity row
  R->>Q: Enqueue push or Live Activity delivery jobs
  Q->>APNs: Deliver signed APNs request
  APNs-->>M: Push notification or Live Activity update
          

The local server publishes a deliberately narrow projection. Failed runs use a fixed redacted summary, and detail strings are capped before publication.

Managed Endpoint Reconciliation

Managed endpoint resources are keyed by (userId, environmentId). The relay reserves a stable hostname and tunnel name before provisioning, then checkpoints the tunnel ID, DNS record ID, and ready state in Postgres.

1. Reserve deterministic allocation Hostname and tunnel name derive from relay stage, cloud user, and environment ID.
2. Reuse or create tunnel Existing named tunnels are reused. Tunnel ingress is rewritten to the validated loopback origin.
3. Reconcile DNS Existing CNAMEs are updated and duplicate records are removed. Create conflicts are recovered by listing and reconciling the winning record.
4. Mark ready Connect and status requests use only ready allocations whose hostname still matches the relay-managed namespace.

Unlink Cleanup

Relay unlink removes the user's managed endpoint allocation before revoking the user/environment link:

  • delete the managed DNS record if present;
  • delete the Cloudflare tunnel if present;
  • remove the allocation row;
  • revoke the user's environment link; and
  • revoke the environment publication credential only after the final active link for that environment key disappears.

Local unlink stops the relay client and removes the locally persisted relay URL, issuer, linked user, environment publication credential, cloud-mint public key, connector config, and agent-activity preference.

Cloudflare Tunnel Operational Profile

Managed tunnels are private application backends, not general-purpose public hosting. The relay provisions one named Cloudflare tunnel and one DNS record per active (userId, environmentId) allocation.

SSRF and Tunnel Hardening

Risk Implemented control
A client asks the relay to tunnel an arbitrary host. The environment and relay both accept managed tunnel origins only for loopback hosts and valid ports.
A forwarded request tricks local origin validation. The local link-proof handler requires an exact loopback request URL and rejects forwarded host or protocol headers.
A stored endpoint is replaced with an arbitrary external URL. Connect and status resolve a ready allocation from Postgres and require a hostname under the configured managed endpoint zone.
A managed endpoint redirects relay egress elsewhere. Relay-to-environment status and mint requests set redirect handling to manual.
A process behind the tunnel returns attacker-controlled data. The relay verifies environment-signed health and mint responses against the linked environment public key, expected nonce, and request binding.
A stolen remote access token is replayed. Relay and environment access tokens are DPoP-bound; per-request proofs and replay guards are required.
The relay client token leaks through process listings or shell parsing. The relay client is spawned without a shell and receives its connector token in TUNNEL_TOKEN.
Managed tunnel hostnames live under the configured tunnel DNS zone. Serving them from a dedicated registrable domain with a Public Suffix List entry remains an operational isolation requirement before broad untrusted use.

Relay HTTP API

The relay publishes an OpenAPI document at /openapi.json, interactive API docs at /docs, and redirects / to the docs.

Endpoint Authentication Purpose
GET /health None Relay health check.
GET /.well-known/oauth-authorization-server None Relay OAuth token-exchange discovery metadata.
GET /.well-known/oauth-protected-resource None Relay DPoP protected-resource discovery metadata.
GET /v1/environments Clerk bearer List environments linked to the signed-in user.
POST /v1/client/environment-link-challenges Clerk bearer Create a short-lived user-bound environment link challenge.
POST /v1/client/environment-links Clerk bearer Verify a local environment proof, reconcile a managed endpoint, and upsert the user link.
DELETE /v1/client/environment-links/:environmentId Clerk bearer Remove managed endpoint resources and revoke the user's link.
POST /v1/client/dpop-token Clerk token in token-exchange payload plus DPoP proof Issue a relay DPoP access token for the requested supported scopes.
POST /v1/environments/:environmentId/status Relay DPoP Request and validate a signed environment health response.
POST /v1/environments/:environmentId/connect Relay DPoP Request and validate an environment bootstrap credential.
POST /v1/mobile/devices Relay DPoP Register or update a mobile device for notifications.
POST /v1/mobile/live-activities Relay DPoP Register a Live Activity push token.
DELETE /v1/mobile/devices/:deviceId Relay DPoP Unregister a mobile device.
POST /v1/environments/:environmentId/threads/:threadId/agent-activity Environment bearer plus signed publish proof Publish redacted current agent activity for notification delivery.

Environment Cloud HTTP API

Endpoint Authentication Purpose
POST /api/connect/link-proof Local environment token with relay:write Validate local origin and sign an environment link proof.
POST /api/connect/relay-config Local environment token with relay:write Persist linked relay configuration and start the relay client.
GET /api/connect/link-state Local environment token with relay:read Read the environment's current T3 Connect link state.
POST /api/connect/preferences Local environment token with relay:write Enable or disable agent-activity publication.
POST /api/connect/unlink Local environment token with relay:write Stop the relay client and clear local T3 Connect configuration.
POST /api/t3-connect/health Relay-signed proof Return a signed environment health response.
POST /api/t3-connect/mint-credential Relay-signed proof Mint a DPoP-bound one-time bootstrap credential for a remote client.
POST /oauth/token Bootstrap credential plus DPoP proof Exchange a bootstrap credential for an environment access token.
POST /api/auth/websocket-ticket Environment access token Issue a one-time ticket for the WSS upgrade.

Relay Client Installation

Desktop web UI availability checks and installs are local WebSocket RPC methods: cloud.getRelayClientStatus and streaming cloud.installRelayClient. The install stream emits the current stage: checking, waiting for lock, downloading, verifying, installing, validating, and activating.

The web UI asks for confirmation before installation and displays a custom progress dialog. The CLI invokes the same local relay-client service directly and reports the same progress stages in the terminal.

Relay Infrastructure

  • Cloudflare Worker for the hosted relay HTTP API.
  • PlanetScale Postgres database named t3coderelay.
  • Production uses the shared database; non-production stages use database branches.
  • Cloudflare Hyperdrive with caching disabled and a constrained origin connection limit.
  • Cloudflare tunnel and DNS bindings for managed endpoint reconciliation.
  • Cloudflare APNs delivery queue plus dead-letter queue.
  • A five-minute cron job that prunes expired DPoP replay rows.
  • Axiom OTLP trace dataset, scoped ingest token, and recent-spans view per stage.

Deployment Configuration

Boundary Configuration
Relay infrastructure RELAY_API_ZONE_NAME, RELAY_TUNNEL_ZONE_NAME, optional RELAY_DOMAIN, CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY, CLERK_JWT_AUDIENCE, and APNs credentials.
Source-built desktop/server cloud features Optional T3CODE_RELAY_URL, T3CODE_CLERK_PUBLISHABLE_KEY, and T3CODE_CLERK_CLI_OAUTH_CLIENT_ID. Release builds inject public values at build time.
Web and mobile cloud clients Public Clerk publishable key, Clerk JWT template name, and relay URL. Relay URLs must normalize to an absolute HTTPS origin without credentials, query, fragment, or non-root path.

Implementation Checklist

Standards References