Architecture and layer boundaries
When to use: Layering and boundaries, web vs public API, app layout (clients, routes, logging), ports/adapters, runtime-portable domain/shared/utils code, multi-tenancy, DDD layout, or anti-patterns.
App boundaries (`apps/*`)
Apps only handle:
- Input validation
- Authentication and authorization
- Organization access enforcement
- Routing to domain use-cases
No business logic in handlers, controllers, or jobs.
Application layout (`apps/*`)
- Clients: Initialize integrations in
apps/*/clients.tsand import from boundaries — avoid scattering raw clients. - Routes: Use
apps/*/routes/with aregisterRoutes()(or equivalent) pattern so the HTTP surface stays modular. - Logging: Use
createLogger()from@repo/observabilitywith a stable service name per app. - Tracing: Every
Effect.runPromisecall site must includewithTracingfrom@repo/observabilityin the pipe chain to connect Effect spans to the OTel pipeline. See effect-and-errors for the full tracing rules. - Configuration values: Read env through
parseEnv/parseEnvOptional— see env-configuration.
Web vs public API (`apps/web`, `apps/api`, `@repo/operations`)
- The public API's operation definitions (route config + transport-neutral
executelogic) live inpackages/operations(@repo/operations) — the boundary-contract layer between apps and domain. One definition fans out to the HTTP route, OpenAPI, MCP tool, SDK methods, CLI command, and in-process agent tools. apps/apiis the transport shell: middleware (auth, org context, rate limiting), the MCP HTTP transport, mountingoperationModules, and the manifest emit scripts. Treat the operation contracts as externally consumed and evolve them carefully.@repo/operationssits above domain: operations validate input, map to public schemas, and orchestrate@domain/*use-cases — the same boundary responsibilities apps own, factored into a package so non-HTTP consumers (worker-side agents) can runexecutein-process.apps/webmust not call or proxy throughapps/apifor internal product features.- For web product development, implement backend behavior in
apps/webserver functions by composing domain use-cases and platform adapters directly. - Keep iteration velocity in
apps/webby adding web-private server functions/stores while preservingapps/apistability. - Shared business rules still belong in domain packages;
apps/weband@repo/operationsshould both orchestrate domain use-cases rather than duplicating policy. - Latitude product capabilities should be equally accessible to humans through the web UI and to other LLM agents through MCP/API surfaces.
- Do not dead-end product behavior into UI-only flows. Preserve the boundary rules above, but design schemas, use-cases, and public capabilities so machine-facing access can exist without redesign.
- For the concrete recipe —
defineOperation,OperationModulemanifests,group/sdkMethod/access/rateLimitTier,pnpm openapi:emit/pnpm mcp:emit, schema-description rules that fan out to the TS + Python SDKs, MCP tools, and thelatitudeCLI, the required declarativeaccessfield, anddefineToolset(with its access ceiling) for internal agents — see api-endpoints.
Cross-cutting implementation constraints
- Public request/response schemas should remain boundary-specific; they may reuse shared domain schemas or narrower projections rather than forcing full domain entities onto every surface.
- When a capability is part of the product contract, preserve a machine-facing MCP/API surface instead of making it web-only.
Domain layer (`packages/domain/*`)
Business logic lives here. Domain packages expose:
- Use-cases
- Canonical entity schemas and inferred entity types
- Domain types and errors
- Dependency ports (interfaces/tags)
Domain package layout
Domain entities are Zod-first: entitySchema + z.infer<typeof entitySchema> in src/entities/<entity>.ts. See