Skip to content

Composition Root & Configuration

James Dunkerley edited this page Dec 3, 2026 · 4 revisions

Home · Architecture · Backend core

In short

  • backend/src/tz_ps_conductor/dependencies.py is the composition root: it builds everything a process shares, once per process.
  • Only a root may ask it for a dependency. Everything deeper takes what it needs as arguments.
  • One process owns a storage root, through the root lock.
  • A process reads one JSON config document, once. Each top-level key belongs to one package, and nothing below the root reads config.

The composition root

backend/src/tz_ps_conductor/dependencies.py builds everything a process shares.

  • Nothing is held as a ready-made module-level instance.
  • The module keeps empty slots behind one re-entrant lock and fills them on first use.

build_x, get_x, stop_x

  • build_x() reads config and assembles the object.
  • get_x() hands it out.
    • Built once per process: get_runner, get_persistence, get_object_store, get_network_file_formats and the config itself.
    • Built on each call: the staging spaces, the export space and the three executors, which are light objects.
    • The executors first call get_runner(), because only that starts the durable runtime.
  • stop_x() exists for whatever starts something process-wide: stop_runner, stop_persistence, stop_telemetry, stop_root_lock.
  • clear_dependencies() resets everything and exists for tests.

The rule for callers is "pull at a root, push below it".

  • Only a root may call a get_x(): dependencies.py itself, a __main__, a FastAPI dependency, the HTTP lifespan, or a public entry point such as Project.get and Network.solve.
  • Everything deeper takes its collaborators as arguments.
  • A caller with its own warehouse uses Project.build_factory(warehouse, staging_space).

What it builds

  • Persistence, from tz_ps_datawarehouse.build_persistence: the Warehouse (networks and projects) and the run, import, export, workflow-run and heartbeat stores, with one stop. get_warehouse() and the store getters read their part of it.
  • The warehouse kind. storage.warehouse_kind defaults to GIT. Only the root falls back to NON_VERSIONED, and only when the document names no kind, sets no warehouse settings, and git is not installed.
  • The object store: a LocalObjectStore. This is the one place an object store adapter is named.
  • The solver registry. Each discovered solver is wrapped in LocalProcessSolver, so the instance in the API process only answers whether it is provisioned and which options it offers. The solve runs in a child process, which tz_ps_conductor/solve_process.py prepares.
  • The runner. start_durable_runner in tz_ps_conductor/durability.py builds the Runner with a DbosExecutor, starts the durable runtime with the import, export, workflow and measures steps, runs the start-up sweeps, and queues the measures backfill.
  • Capabilities: what this server can do, read from the warehouse it opened. See Backend core.

The root lock

One process owns a storage root.

  • start_root_lock() takes an operating-system lock under the root's state folder.
  • A held root raises StorageRootInUseError.
  • serve, the HTTP lifespan, seed and backfill-measures take the lock. A notebook that uses the Python API does not.
  • Storage describes the lock itself.

Start-up and shutdown

tz-ps-conductor serve (backend/libs/tz_ps_app/src/tz_ps_app/__main__.py) does three things, in this order:

  1. It lays --root and --port over the default config file.
  2. It takes the root lock, before it binds the port.
  3. It builds the app.

The rest happens in the FastAPI lifespan in tz_ps_http/app.py, which the tz-ps-http command runs as well. The command's standard-output lines and exit codes are a contract with the desktop app, which The HTTP API and the contracts owns.

The diagram shows the order in which serve and the lifespan start the shared dependencies, and the order in which they stop.

sequenceDiagram
    participant CLI as tz-ps-conductor serve
    participant Root as dependencies.py
    participant Life as HTTP lifespan
    participant Store as Persistence
    participant Run as Runner and durable runtime
    CLI->>Root: lay flags over the config document
    CLI->>Root: start_root_lock
    CLI->>Life: build the app and start uvicorn
    Life->>Root: start_telemetry
    Life->>Root: start_root_lock again, a no-op
    Life->>Root: get_warehouse
    Root->>Root: configure logging and discover frameworks
    Root->>Store: build_persistence
    Life->>Root: get_import_store
    Life->>Root: write the demo project at a first start
    Life->>Root: get_runner
    Root->>Run: discover solvers and start the durable runtime
    Run->>Run: recover workflows, run sweeps, queue the measures backfill
    Life-->>CLI: listening
    Note over Life,Run: shutdown
    Life->>Run: stop_runner
    Life->>Store: stop_persistence
    Life->>Root: stop_telemetry
    Life->>Root: stop_root_lock
Loading

What the order guarantees:

  • Telemetry starts first, so a bad telemetry config stops the start before storage opens.
  • The runner stops first, because it holds the warehouse and the run store.
  • A failed demo-project write is logged and never stops the server.

In a notebook there is no lifespan. Importing tz_ps_conductor installs framework discovery on Framework, so the first read of the framework registry configures logging from the config and discovers the frameworks.

Configuration

A process reads one JSON document, once.

  • TZ_PS_CONDUCTOR_CONFIG holds either the JSON itself or a path to a file.
  • Unset, ~/tz-ps-conductor/config.json is read when it exists.
  • Otherwise every package uses its defaults.

Each top-level key belongs to one package.

  • The package subclasses Config (tz_ps_models) and names its key.
  • The root reads it with get_config(MyConfig). A validation failure becomes InvalidConfigError.
  • A key no package claims is ignored.
  • The retired key warehouse is refused by name, because ignoring it would quietly move storage to the default root.
Key Class Package What it sets
storage StorageConfig tz_ps_datawarehouse The root path, the warehouse kind and its settings, the inline series length, DuckDB's memory limit
durable DurableConfig tz_ps_durable_runner Worker and import concurrency, retry and backoff, the solver log cap and polls, heartbeat and takeover times
compute ComputeConfig tz_ps_conductor The memory limits of the solve process
logging LoggingConfig tz_ps_conductor The console level, and the log file's path and level
telemetry TelemetryConfig tz_ps_conductor Whether to export, and the OTLP endpoint
seed SeedConfig tz_ps_conductor Whether a first start writes the demo project
updates UpdatesConfig tz_ps_conductor Whether to look for a newer release
http_server HttpServerConfig tz_ps_http Host, port, allowed origins, the import size limit, the live evaluation limits

No environment variable overrides a key, with two exceptions, each applied by a validator on its own config class:

  • TZ_PS_CONDUCTOR_SEED over seed.demo_data.
  • TZ_PS_CONDUCTOR_CHECK_RELEASES over updates.check_releases.

Nothing below the root reads config. The root hands values down inside objects:

  • DuckdbLimits to the warehouse.
  • SolveLimits inside SolverDependencies.
  • A LogCap to the run store.

The solve process is the exception in mechanism only: it reads the same document through the environment variable it inherits.

Clone this wiki locally