Skip to content
opencrvsPublic

About

Mastra Factory server for OpenCRVS: turns labelled GitHub issues into plans and pull requests

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Mastra Factory (self-hosted)

Mastra Factory is an open source environment for building software with coding agents. Connect your repository to turn issues into plans, implementations, and reviewed pull requests.

This template runs the Factory Server entirely on infrastructure you operate, with no Mastra platform services. Keep it separate from the repository you want agents to change.

Read the documentation or watch the Mastra Factory overview.

What it uses

src/mastra/index.ts wires each service explicitly:

Service Provider Configured by
Storage PostgreSQL with pgvector DATABASE_URL
Auth WorkOS WORKOS_*
Source code Your own GitHub App GITHUB_APP_*
Sandboxes Local — agent commands run on this machine —
Secrets AES encryption of stored credentials FACTORY_CREDENTIAL_ENCRYPTION_KEY

To change a service, replace its instance in src/mastra/index.ts. See Configure your Factory.

Warning

Local sandboxes run agent commands as the server's user, with access to its files and network. Use them only on a machine dedicated to this Factory. For shared or multi-tenant hosts, replace LocalFactorySandbox with an isolated provider such as E2BFactorySandbox.

Start the Factory Server

  1. Install dependencies with npm install (npm create factory already did this).

  2. Copy .env.example to .env (npm create factory already did this).

  3. Generate a credential encryption key and save it as FACTORY_CREDENTIAL_ENCRYPTION_KEY. Preserve it across restarts and deployments, and keep a protected backup.

    openssl rand -base64 32
  4. Start the local PostgreSQL service and set DATABASE_URL=postgres://factory:factory@localhost:54330/factory:

    npm run db:up
  5. Create a WorkOS application. Set WORKOS_API_KEY, WORKOS_CLIENT_ID, and a 32+ character WORKOS_COOKIE_PASSWORD. Add http://localhost:4111/auth/callback as a redirect URI.

  6. Create a GitHub App and fill in the GITHUB_APP_* variables.

  7. Install Git and your repository's build tools on this machine, then start the server:

    npm run dev

Open the local URL printed by the server and sign in. The onboarding wizard asks you to select the repository agents should change and connect a model provider.

Run your first issue

  1. Open Settings → Work Intake → GitHub issues. Enable Sync GitHub issues and select your repository.
  2. Create a small GitHub issue, such as adding contribution guidance to the repository's README.
  3. Find the issue in Work → Intake, select Investigate, and open its session to follow the agent's work.

Continue with the issue-to-pull-request walkthrough.

Configure your Factory

Configuration What you can change
Models Provider access, personal or organization credentials, and default model.
GitHub Repository access and personal issue intake.
Linear Add a LinearIntegration to integrations.
Jira Add a JiraIntegration to integrations.
Slack Add a SlackIntegration to integrations.
Auth Replace MastraAuthWorkos with another auth provider.
Storage Replace PgFactoryStorage with another Factory storage adapter.
Sandboxes Replace LocalFactorySandbox, for example with E2BFactorySandbox.

Deploy

Run Factory as a persistent Node.js service on a virtual machine or in a container. Set MASTRACODE_PUBLIC_URL to the public HTTPS origin, update your WorkOS and GitHub App callback URLs to match, and supply the .env values through the deployment environment. Then build and start the server:

npm run build
npm run start

See self-hosting for runtime requirements.

OpenCRVS non-prod cluster

The OpenCRVS Factory runs at https://factory.opencrvs.dev in the non-prod Kubernetes cluster.

  • Dockerfile builds the server image. The image also has the tools that agents use in the local sandboxes: git, the GitHub CLI, Node.js from opencrvs-core/.nvmrc, pnpm and native build tools.
  • .github/workflows/publish-image.yml pushes the image to ghcr.io/opencrvs/factory on each push to main.
  • The Helm values, the network policy and the setup steps are in opencrvs-testland-infrastructure/environments/non-prod/factory.

When you change the Node.js version in opencrvs-core/.nvmrc, change the FROM line in Dockerfile too.

src/mastra/index.ts reads these settings:

Variable Default What it does
MASTRACODE_DISPATCH_MAX_IN_FLIGHT 2 Automated agent runs at the same time. Minimum 1.
MASTRACODE_DISPATCH_MAX_INTERACTIVE_IN_FLIGHT 1 Extra slots for replies from people in GitHub.
FACTORY_SESSION_MAX_IDLE_DAYS 7 At start, delete the session folders that had no change for this many days. 0 turns it off.

Scripts

Script What it does
npm run dev Start the local Factory Server with its UI and API.
npm run build Build the server and Factory UI in .mastra/output.
npm run start Run the production build.
npm run db:up / npm run db:down Start or stop the local PostgreSQL service.

Troubleshooting

  • Runtime: Use a Node.js version that matches engines.node in this project's package.json.
  • Port changes: Set PORT and update MASTRACODE_PUBLIC_URL to match. Update your WorkOS and GitHub App callback URLs.
  • Missing issues: Check repository access and your Work Intake selections. See intake troubleshooting.

See Troubleshooting or Environment variables.

License

Apache-2.0

About

Mastra Factory server for OpenCRVS: turns labelled GitHub issues into plans and pull requests

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages