academy.runtime¶
RuntimeConfig
dataclass
¶
RuntimeConfig(
cancel_actions_on_shutdown: bool = True,
max_sync_concurrency: int | None = None,
raise_loop_errors_on_shutdown: bool = True,
shutdown_on_loop_error: bool = True,
terminate_on_error: bool = True,
terminate_on_success: bool = True,
)
Agent runtime configuration.
Attributes:
-
cancel_actions_on_shutdown
(bool
) –Cancel running actions when the agent is shutdown, otherwise wait for the actions to finish.
-
max_sync_concurrency
(int | None
) –Maximum number of concurrent sync tasks allowed via
Agent.agent_run_sync()
. This is used to set the number of threads in a defaultThreadPoolExecutor
. -
raise_loop_errors_on_shutdown
(bool
) –Raise any captured loop errors when the agent is shutdown.
-
shutdown_on_loop_error
(bool
) –Shutdown the agent if any loop raises an error.
-
terminate_on_error
(bool
) –Terminate the agent by closing its mailbox permanently if the agent shuts down due to an error.
-
terminate_on_success
(bool
) –Terminate the agent by closing its mailbox permanently if the agent shuts down without an error.
Runtime
¶
Runtime(
agent: AgentT,
*,
exchange_factory: ExchangeFactory[ExchangeTransportT],
registration: AgentRegistrationT,
config: RuntimeConfig | None = None
)
Bases: Generic[AgentT]
, NoPickleMixin
Agent runtime manager.
The runtime is used to execute an agent by managing stateful resources, startup/shutdown, lifecycle hooks, and concurrency.
An agent can be run in two ways:
runtime = Runtime(agent, ...)
# Option 1: Async context manager
async with runtime:
...
await runtime.wait_shutdown()
# Option 2: Run until complete
await runtime.run_until_complete()
Note
A runtime can only be used once, after which attempts to run an
agent using the same runtime with raise a
RuntimeError
.
Note
If any @loop
method raises an error, the agent will be signaled
to shutdown if shutdown_on_loop_error
is set in the config
.
Parameters:
-
agent
(AgentT
) –Agent that the agent will exhibit.
-
exchange_factory
(ExchangeFactory[ExchangeTransportT]
) –Message exchange factory.
-
registration
(AgentRegistrationT
) –Agent registration info returned by the exchange.
-
config
(RuntimeConfig | None
, default:None
) –Agent execution parameters.
Source code in academy/runtime.py
action
async
¶
Invoke an action of the agent's agent.
Parameters:
-
action
(str
) –Name of action to invoke.
-
source_id
(EntityId
) –ID of the source that requested the action.
-
args
(Any
) –Tuple of positional arguments.
-
kwargs
(Any
) –Dictionary of keyword arguments.
Returns:
-
Any
–Result of the action.
Raises:
-
AttributeError
–If an action with this name is not implemented by the agent's agent.
Source code in academy/runtime.py
run_until_complete
async
¶
Run the agent until shutdown.
Agent startup involves:
- Creates a new exchange client for the agent.
- Sets the runtime context on the agent.
- Binds all handles of the agent to this agent's exchange client.
- Schedules a
Task
to listen for messages in the agent's mailbox in the exchange. Agent requests will not start processing until the end of the startup sequence. - Schedules a
Task
for all control loops defined on the agent. Each task will block until the end of the startup sequence before starting the loop. - Calls
Agent.agent_on_startup()
.
After startup succeeds, this method waits for the agent to be shutdown, such as due to a failure in a control loop or receiving a shutdown message.
Agent shutdown involves:
- Calls
Agent.agent_on_shutdown()
. - Cancels running control loop tasks.
- Cancels the mailbox message listener task so no new requests are received.
- Waits for any currently executing actions to complete.
- Terminates the agent's mailbox in the exchange if configured.
- Closes the exchange client.
Raises:
-
RuntimeError
–If the agent has already been shutdown.
-
Exception
–Any exceptions raised during startup, shutdown, or inside of control loops.
Source code in academy/runtime.py
signal_shutdown
¶
Signal that the agent should exit.
If the agent has not started, this will cause the agent to immediately shutdown when next started. If the agent is shutdown, this has no effect.
Parameters:
-
expected
(bool
, default:True
) –If the reason for the shutdown was due to normal expected reasons or due to unexpected errors.
-
terminate
(bool | None
, default:None
) –Optionally override the mailbox termination settings in the run config.
Source code in academy/runtime.py
wait_shutdown
async
¶
wait_shutdown(timeout: float | None = None) -> None
Wait for agent shutdown to be signalled.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for a shutdown event.
Raises:
-
TimeoutError
–If
timeout
was exceeded while waiting for agents.