---
title: System architecture
description: Understand Workbench's workspaces, HTTP API boundaries, and runtime composition.
owner: engineering
sourceFiles:
  - infra/alchemy.run.ts
  - infra/server.ts
  - infra/server-build.ts
  - infra/worker-upload.ts
  - infra/scripts/worker-analysis.ts
  - infra/storage/deleted-spaces.ts
  - infra/web.ts
  - apps/web/src/routes/api.$.ts
  - apps/web/src/routes/__root.tsx
  - infra/docs.ts
  - apps/server/src/worker.ts
  - apps/server/src/http/Api.ts
  - apps/server/src/domain/actor.ts
  - packages/contracts/src/server.ts
  - apps/server/src/http/middleware/error-handling.ts
  - apps/server/src/http/middleware/schema-error.ts
  - apps/web/vite.config.ts
  - AGENTS.md
---

Workbench uses Bun workspaces, a TanStack Start frontend, and a separate Effect API on Cloudflare Workers. Alchemy provisions the deployed resources. Local development runs the frontend and API against an existing personal database stage.

The [pinned Finance Core resources](/architecture/finance-core-runtime) supply a fixed binary diagnostic profile for later trusted Computer integration.

## Workspace layout

| Location | Responsibility |
| --- | --- |
| `apps/web` | TanStack Start, React, Tailwind 4, and Base UI shadcn components |
| `packages/contracts/src/server.ts` and `server/*` | Shared HTTP contract assembly, group endpoints, schemas, and public errors |
| `apps/server/src/http/Api.ts` | Assemble handler groups, HTTP middleware, cache policy, and fallback routes |
| `apps/server/src/http/middleware/` | Authentication, error handling, and schema validation |
| `apps/server/src/domain/organizations/` and `domain/tasks/` | Feature operations enforce permissions; each feature's `data/` folder owns database access |
| `apps/server/src/worker.ts` | Connect Hyperdrive, construct Drizzle, provide the `Db` and `AuthService` services, and assemble the runtime |
| `infra/server.ts` | Provision the Worker and declare deployment properties |
| `infra/docs.ts` | Build and publish the production-only Blume static site at docs.workbench.wurthy.co |
| `infra/database/` | PlanetScale resources, Hyperdrive, and stage policy |
| `infra/alchemy.run.ts` | Compose the infrastructure stack |
| `packages/db` | `Db` service, auth schema, and migrations |
| `packages/config` | Native Effect configuration, Proton provider, and the server-only `Env` service |
| `packages/auth` | Typed Effect Auth service and shared Better Auth options |

Workspace packages export TypeScript source for the app bundler; they need no separate package build.

The server's infrastructure entrypoint dynamically imports `@workbench/server` when Alchemy initializes the runtime on first use. Application module evaluation runs within that event instead of Cloudflare's isolate startup CPU budget. Alchemy shares the initialized runtime across subsequent events; HTTP assembly stays in `apps/server/src/worker.ts`.

Deployment-only storage tombstone checks use Alchemy's build-time runtime flag so the state client and its build tooling are omitted from Worker uploads. The API build caps uploaded JavaScript at 4.65 MB, counting dynamic-import chunks as well as the entrypoint. Exceeding that budget or emitting known compiler-tooling packages fails the build. [Worker measurements](/development/workflow#worker-measurements) report package growth and local startup CPU separately from hosted HTTP timings; inspect them before changing the budget.

The browser calls relative `/api/...` URLs in every environment. Vite proxies them locally; the hosted frontend forwards them through a Cloudflare service binding to the API Worker. The forwarding route preserves `/api/auth` for authentication and strips `/api` from other API paths without moving endpoint logic into the frontend.

The API Worker uses targeted placement in `aws:us-east-1`, next to the PlanetScale database in `us-east`. A request crosses regions once, between the caller and the API Worker; its database queries stay in one region. Keep the placement region matched to the database region.

The frontend's [design system guide](/development/design-system) documents shared tokens, component composition, and brand assets. [Frontend data fetching](/architecture/data-fetching) covers how pages load server state through TanStack Query. [Conversation files](/architecture/files) explains immutable uploads, private storage and authorized downloads.

## HTTP boundaries

Contracts declare paths and schemas. Focused HTTP groups implement them with `HttpApiBuilder.group`; `Api.ts` combines their layers. Declare public error schemas and statuses in their owning API group. HTTP handlers receive the authenticated actor, call application operations, and map domain failures to public errors using a mapping per group; Effect serializes the responses. Organization and task operations take a session-derived `Actor` (`userId`, `organizationId`) plus request input, enforce permissions within the selected organization, and then call database functions. Database functions handle data access; HTTP mappings stay in the handlers. Handlers and their operations receive the `Db` service from `packages/db` through `worker.ts` request services rather than importing provisioning code.

The API exposes readiness and stateless `/demo` groups. Invalid requests return 400, after authentication on the organization and task groups; readiness query failures/timeouts return 503; unmatched paths or methods return 404. Readiness has a five-second timeout. Expected query failures and timeouts do not produce application error logs. API responses prevent caching. [Authentication](/architecture/authentication) mounts Better Auth at `/api/auth/*` and uses Better Auth’s native `/api/auth/get-session` endpoint for session checks.

Native Effect global middleware applies the shared cache policy and Alchemy's safe HTTP failure handling. Unexpected failures are reported server-side and return an empty 500 response without exposing internal error details. Cancellation remains an interruption.

[AGENTS.md](https://github.com/CreditWurthy/workbench/blob/main/AGENTS.md) defines the exact layout and ownership rules. `bun run api:check` enforces filenames, exports, and entrypoints; lint protects contract/runtime import boundaries. Both run in `bun run check`.

The [Finance Core execution boundary](/architecture/finance-core) runs a pinned captured-source diagnostic as a draft preparation job.
