Studio grew to many generators, each with its own models, prompts and renderers. The architecture exists to answer one question as that number grew: when I change the card-news pipeline, what else can break? The answer it aims for is "nothing outside that package", and this page is about how far the code gets towards that, including where it falls short.
Layers

| Layer | What lives there | Rule |
|---|---|---|
apps/ | The web app and the API. Wiring: router registration, start-up, the scheduler | Apps import packages. Packages do not import apps |
packages/domain-* | One bounded context each: articles, card news, detail pages, slides, banners, video, keywords, brand, billing, workspace | A domain reaches another domain only through its public index |
packages/shared-* | Utilities and integrations: search, storage, provider wrappers, heartbeat | Shared packages never import a domain |
Inside every domain the layering is the same: api → usecases → {adapters, model}. A route handler may call a use case. A use case may call an adapter. Nothing in the model imports upward.
What checks it
The rules are checked by tools, not by review.
- Python: import-linter contracts. One kind says a set of modules is forbidden to another. A second kind declares the layers of a package in order. This is the shape of the two, shortened from the repository’s contract file.
[importlinter:contract:shared-is-leaf]
name = shared-* packages must not depend on any trail_<domain>
type = forbidden
source_modules =
trail_utils
trail_integrations
forbidden_modules =
trail_banner
trail_article
...
[importlinter:contract:hexagonal-article]
name = trail_article Hexagonal layering
type = layers
layers =
trail_article.api
trail_article.usecases
trail_article.adapters
trail_article.model
- TypeScript: Nx’s module-boundary rule with scope tags: an app may import a domain or a shared package, a domain may import shared, shared may import only shared.
Known violations are not hidden. Each is listed by name in the contract’s ignore list, so the debt is a countable list and a new violation fails the check.
A request becomes a run
Generation takes minutes, so a request cannot wait for it.

- The browser calls the API through one client wrapper that attaches the session token and the active workspace id and refreshes the token on a 401.
- The route resolves the user and the workspace, creates the document for the thing being generated, and charges credits.
- The route starts the pipeline as an asyncio task in the API process and returns the document id.
- The pipeline writes its progress onto the document as it goes. The browser reads the document to show which step is running.
There is no separate worker process and no queue. The task registry keeps one task per document id, and starting a new run for the same document cancels the old one.
Running long work inside the API process has an obvious hazard: the process restarts, and a document is left saying “generating” for ever. Three mechanisms cover that.
| Mechanism | What it does |
|---|---|
| Heartbeat | A running pipeline stamps its document every 30 seconds |
| Sweeper | Every five minutes, documents with no heartbeat for 30 minutes are moved to an error state |
| Start-up recovery | On boot, articles and videos still marked as generating are marked failed, and stuck storyboards are returned to their ready state |
A pipeline that raises refunds its credits in its own error path, as described in Credits and tenancy. A run that dies with the process is handled differently: the sweeper and the start-up recovery change its status and leave the credits as they are.
Scheduled work
One APScheduler instance runs inside the API. It holds the fixed jobs: the sweeper, cleanups and periodic content jobs, some behind settings flags. One of those jobs is a tick every 60 seconds that looks for user-defined schedules stored in MongoDB that have come due.
State
Everything is in MongoDB through Beanie. Each domain exports the list of its document classes, and the API registers them all at start-up, so adding a domain does not mean editing a central schema. Generated files are served by the API from a local assets directory. An object-storage wrapper uploads to a bucket when credentials are configured and skips the upload when they are not.
The first versions kept operational data in SQLite. That was migrated: users, sessions, cost records and schedules are MongoDB collections now, and SQLite survives only in the one-off migration scripts.
Process and deployment
Two processes run under pm2: the API and the web app. A deploy script builds the web app locally, refuses to continue if a local dev server is running or if the build id is missing, syncs to the server, checks that the build id file arrived there, installs dependencies, reloads and runs health checks.
Decision records, and where the code stands
Eight decisions were written down as records in April 2026. The table gives the state of each in the code.
| Decision | Chosen over | State in the code |
|---|---|---|
| Nx as the monorepo tool | Turborepo, which is weak for Python and has no boundary enforcement | In use. The remote cache was never switched on |
| A uv workspace with one lock file | Poetry, with its path workarounds and slower resolver | In use |
| No shared-types package | A general hub of shared types | Holds. There is no such package |
| Domains call each other’s public functions, with an event bus for side effects | An event bus as the default, or HTTP between domains | Direct calls are used. The event bus was not built |
| Workspace and billing as injected dependencies | Middleware that sets request state | Workspace injection exists. Charging is an explicit call in each handler |
| Pipeline versions move through active, frozen, deprecated, removed | Keeping versions side by side indefinitely | Old versions were removed. The bookkeeping the record asks for was not kept |
| A self-built feature-flag store | A hosted flag service | Not built. Flags are environment variables |
| A shared package for the video renderer | A separate app, or inside the video domain | Not done. The renderer stayed where it was |
Three are followed as written, two in part, and three were not built. The ones that held are structural and are checked by tools. The ones that did not were infrastructure to be built ahead of a need.
Decisions
1. Domains are packages. A package has a public index and a contract. A folder has neither, and nothing stops a deep import.
2. Long runs in the API process. A worker tier and a queue are the conventional answer. Here an in-process task with a heartbeat and a sweeper covers detection: a run that dies is found and marked failed, with nothing extra to deploy. Its credits are not returned automatically.
3. Progress on the document. The pipeline writes its step onto the same document the browser reads. There is no second channel to keep consistent.
4. No checkpointer in the graph. The article graph is compiled without one. The article document is the checkpoint, and a retry rebuilds the graph from the step that failed.
5. Explicit router registration. Every router is listed. Nothing is discovered by scanning, so removing a line removes a feature. The task checklist includes a manual check of the route count.
6. Unchanged when a flag is off. New behaviour arrives behind a flag, with tests on the flag-off path. The brand loader returns exactly an empty block for a request without a brand, and the team graph keeps its original topology with the strict mode off.
Limits
- A run that dies with the process keeps its charge. The sweeper and the start-up recovery mark it failed and do not call the refund.
- There is no CI. The contracts, the tests and the linters are run by hand and by the coding agents’ rules. The deploy script checks the build and health, not tests.
- The app layer holds some logic. The article’s main graph lives under the API app, and a domain route calls back into it. That edge is on the contracts’ ignore list.
- Some imports go deep. Callers import the billing use cases directly instead of through the package’s public index.
- Tenant scoping is per query. Each query filters by workspace. There is no base class that applies the filter for you.
- Tests run one package at a time. Test configuration names collide across packages, so there is no single command for the whole suite.
