academy.runtime¶
RuntimeConfig
dataclass
¶
RuntimeConfig(
cancel_actions_on_shutdown: bool = True,
max_action_concurrency: int | None = None,
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_action_concurrency
(int | None
) –Maximum size of the thread pool used to concurrently execute action requests.
-
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.
Note
This can only be run once. Calling
run()
multiple times will 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
async
¶
Run the agent.
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.
- Starts a
Task
to listen for messages in the agent's mailbox in the exchange. - Starts a
Task
for all control loops defined on the agent. - 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.