Utilities

Utility functions for the communication protocol framework.

This module provides shared utility functions used across different environments and components of the multi-agent communication protocol system.

terrarium.utils.build_log_dir(environment_name, tag_model, seed, run_timestamp=None, *, log_root='logs')[source]

Build the filesystem path for log artifacts.

Parameters:
Return type:

Path

terrarium.utils.build_vllm_runtime(llm_config)[source]

Construct a VLLMProviderRuntime from normalized configuration.

Parameters:

llm_config (Dict[str, Any])

terrarium.utils.clear_seed_directories(environment_name, seed, full_config)[source]

Clear existing seed directories for both logs and plots to ensure clean state.

Parameters:
  • environment_name (str) – Name of the environment (e.g., “Hospital”, “PersonalAssistant”)

  • seed (int | str) – Seed value for the current run

  • full_config (Dict[str, Any]) – Full configuration dictionary containing all simulation settings

Return type:

None

terrarium.utils.configure_client_external_mcp(client, llm_config)[source]

Attach optional external MCP server configuration to a client instance.

Parameters:
Return type:

None

terrarium.utils.create_environment(protocol, environment_name, config, tool_logger)[source]

Create environment instance based on name.

Parameters:
  • protocol – Communication protocol instance

  • environment_name (str) – Environment class name (matches config environment.name)

  • config – Configuration dictionary

  • tool_logger – Tool call logger instance

Returns:

Instantiated environment object

Raises:

ValueError – If environment name is unknown

Return type:

AbstractEnvironment

terrarium.utils.extract_model_info(full_config)[source]

Extract model name from full config for logging.

Parameters:

full_config (Dict[str, Any]) – Full configuration dictionary containing all simulation settings

Returns:

Model name string, or “unknown” if not found

Return type:

str

terrarium.utils.get_client_instance(llm_config, *, agent_name=None, vllm_runtime=None)[source]

Create and return the appropriate LLM client based on provider configuration.

Parameters:
  • llm_config (Dict[str, Any]) – LLM configuration dictionary containing provider and provider-specific settings

  • agent_name (str | None)

  • vllm_runtime (Any)

Returns:

Instantiated client (OpenAI/Foundry/Grok/Anthropic/Gemini/Together/Fireworks/VLLM)

Raises:
terrarium.utils.get_implemented_environment_names()[source]

Return the list of supported environment.name values.

Return type:

List[str]

terrarium.utils.get_log_root_dir(full_config=None, default='logs')[source]

Resolve the base directory used for writing run log artifacts.

By default, logs are written under the repo-root logs/ folder. Experiment runners can override this to keep artifacts self-contained by setting:

logging:

root_dir: experiments/<project>/outputs/<tag>/<timestamp>/runs/<…>/logs

Parameters:
Return type:

Path

terrarium.utils.get_model_name(provider, llm_config)[source]

Extract model name based on provider from LLM configuration.

Parameters:
  • provider (str) – LLM provider name (openai, foundry, anthropic, gemini, together, fireworks, vllm)

  • llm_config (Dict[str, Any]) – LLM configuration dictionary

Returns:

Model name string

Raises:
Return type:

str

terrarium.utils.get_run_timestamp(full_config, default=None)[source]

Return the run timestamp stored in the simulation config, if any.

Parameters:
Return type:

str | None

terrarium.utils.get_tag_model_subdir(full_config)[source]

Generate tag_model subdirectory name from configuration.

Parameters:

full_config (Dict[str, Any]) – Full configuration dictionary containing all simulation settings

Returns:

Formatted string – {tag}_{model_name}

Return type:

str

terrarium.utils.load_config(config_file)[source]

Load and validate configuration from YAML file.

Parameters:

config_file – Path to YAML configuration file

Returns:

Validated configuration dictionary

Raises:
Return type:

Dict[str, Any]

terrarium.utils.load_seeds(seeds_file='seeds.txt')[source]

Load simulation seeds from text file for reproducibility.

Parameters:

seeds_file (str)

Return type:

List[int]

terrarium.utils.prepare_simulation_config(base_config, seed)[source]

Prepare a simulation config with the specified seed.

Creates a copy of the base config and injects the seed into the simulation section. This ensures each simulation run has the correct seed for reproducibility.

Parameters:
  • base_config (Dict[str, Any]) – Base configuration dictionary

  • seed (int) – Random seed for this simulation

Returns:

New config dictionary with seed injected into simulation.seed

Return type:

Dict[str, Any]

terrarium.utils.validate_config(config)[source]

Validate configuration structure and required fields.

Parameters:

config (Dict[str, Any]) – Configuration dictionary to validate

Raises:

ValueError – If config structure is invalid or missing required sections

Return type:

None