Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

3.8 KB
Raw Blame
/**
 * Railway Infrastructure as Code for TrueForge hosted mode.
 *
 * One project: app + Postgres + Redis. Plan/apply with:
 *   pnpm install
 *   railway login
 *   railway init --name trueforge   # or: railway link
 *   railway config plan
 *   railway config apply
 *   # Shared TRUEFORGE_API_KEY once (Project Settings → Shared Variables), then
 *   # re-apply or redeploy so both services pick it up. Example value:
 *   #   openssl rand -hex 32
 *   railway domain                  # public URL for the trueforge service
 *
 * Docs: https://docs.railway.com/infrastructure-as-code
 *
 * Auth is off by default (anyone who can reach the URL is admin). Before
 * sharing a deployment, enable OIDC - see the commented block below and
 * https://trueforge.dev/authentication/overview
 */
import { defineRailway, github, group, postgres, project, redis, service } from 'railway/iac';

export default defineRailway(_ctx => {
  const db = postgres('Postgres');
  const cache = redis('Redis');

  const app = service('trueforge', {
    // Deploys from this repository's default branch. Forks: change owner/repo
    // (and optionally branch) to build your own copy.
    source: github('truefoundry/trueforge'),
    // Pin the root from-source Dockerfile (not Dockerfile.dev — that path was removed).
    build: { dockerfilePath: 'Dockerfile' },
    healthcheck: '/healthz',
    healthcheckTimeout: 300,
    deploy: {
      restartPolicyType: 'ON_FAILURE',
      restartPolicyMaxRetries: 5,
      // Above GRACEFUL_TIMEOUT_SECONDS (30) so SIGKILL does not cut off turn drain.
      drainingSeconds: 35,
    },
    env: {
      // Postgres + Redis (hosted topology)
      STANDALONE: 'false',
      DATABASE_URL: db.env.DATABASE_URL,
      REDIS_URL: cache.env.REDIS_URL,
      // Expanded by Railway at runtime once a public domain exists on this service.
      PUBLIC_BASE_URL: 'https://${{RAILWAY_PUBLIC_DOMAIN}}',
      // Set once in Railway (shared); IaC only references it - see header.
      TRUEFORGE_API_KEY: _ctx.shared.TRUEFORGE_API_KEY,

      // Optional OIDC (login off until these are set). Create matching *shared*
      // variables on the Railway environment, uncomment, then `railway config apply`.
      //
      // OIDC_ISSUER_URL: _ctx.shared.OIDC_ISSUER_URL,
      // OIDC_CLIENT_ID: _ctx.shared.OIDC_CLIENT_ID,
      // OIDC_CLIENT_SECRET: _ctx.shared.OIDC_CLIENT_SECRET,
      // OIDC_USER_REFERENCE_CLAIM: 'email',
      // OIDC_SCOPES: 'openid,profile,email',
      // OIDC_USER_ROLE_CLAIM: 'email',
      // OIDC_ADMIN_ROLE_VALUE: '[email protected]',
    },
  });

  // Schedule controller: a second service off the same repo/image that runs the
  // periodic control loops (schedule dispatch). Must run as exactly one replica.
  // It talks to Postgres and hands due runs to the app over Railway's private
  // network (SERVER_URL); it needs no public domain or healthcheck.
  const controller = service('trueforge-controller', {
    source: github('truefoundry/trueforge'),
    build: { dockerfilePath: 'Dockerfile' },
    startCommand: 'node dist/controller-main.js',
    replicas: 1,
    deploy: {
      restartPolicyType: 'ON_FAILURE',
      restartPolicyMaxRetries: 5,
      // Single-owner invariant: never run old + new controller at once on redeploy.
      overlapSeconds: 0,
      // Above GRACEFUL_TIMEOUT_SECONDS (30) so SIGKILL does not cut off loop drain.
      drainingSeconds: 35,
    },
    env: {
      STANDALONE: 'false',
      DATABASE_URL: db.env.DATABASE_URL,
      // Same shared key as the app (Bearer auth for schedule dispatch).
      TRUEFORGE_API_KEY: _ctx.shared.TRUEFORGE_API_KEY,
      // Reaches the app over Railway private networking (no public egress).
      SERVER_URL: 'http://${{trueforge.RAILWAY_PRIVATE_DOMAIN}}:${{trueforge.PORT}}',
    },
  });

  return project('trueforge', {
    resources: [group('TrueForge', [db, cache, app, controller])],
  });
});