Skip to content

API Reference parrot.tools.executors.policy

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.tools.executors.policy

View source

Agent-level execution policy: declarative tool → executor routing.

Instead of wiring an executor= kwarg into every tool constructor, an :class:ExecutionPolicy lets an agent (or a bot config file) declare which tools/toolkits run remotely and through which executor::

agent = Agent(
    tools=[PythonREPLTool(), weather_toolkit],
    execution_policy={
        "rules": {
            "python_repl": {"name": "docker",
                            "options": {"network_mode": "none"}},
            "WeatherToolkit": "local",
            "shell_*": {"name": "docker",
                        "options": {"mode": "ephemeral"}},
        }
    },
)

Rule keys match, in precedence order:

  1. Exact tool name ("python_repl").
  2. Exact toolkit class name or tool_prefix ("WeatherToolkit") — applies to every tool the toolkit generates.
  3. fnmatch wildcard against the tool name ("shell_*"), in rule declaration order.
  4. Wildcard against the toolkit class name / prefix.
  5. The catch-all "*" rule, when present.

Rule values are :class:ExecutorSpecs — a registry name plus constructor options — or, for code-only use, a live :class:AbstractToolExecutor instance. Specs are instantiated at most once per rule, so every tool sharing a rule shares one executor (one warm container, not N).

The policy never overrides a tool that already carries an explicit executor= from its constructor, and never routes a tenant-bound tool: its tool scope lives in this process only, so the matched rule is skipped and the tool runs in-process.

A ToolManager resolves the route per dispatch from its own policy (:meth:ExecutionPolicy.route_for), so a tool instance shared by several managers runs where the dispatching manager's policy says when that policy has a rule for it. Registration (:meth:ExecutionPolicy.apply_to_tool) stores the route privately as the tool's fallback: a direct tool.execute() outside any dispatch uses it, and so does a dispatch through a manager with no policy or no matching rule, so a manager's lack of a rule never moves a sandboxed tool in-process. Nothing is written to the tool's public executor / remote_timeout_seconds / webhook_callback_url attributes (FEAT-933).

Alias AbstractToolExecutor

Re-exported from parrot.tools.executors.abstract.AbstractToolExecutor.

Attribute EXECUTOR_REGISTRY

Type: Dict[str, str]

Class ExecutionPolicy

Declarative mapping of tool/toolkit names to executors.

See the module docstring for rule syntax and precedence. ToolManager resolves a route per dispatch with :meth:route_for and stores the fallback route at registration with :meth:apply_to_tool; release pooled resources with :meth:close when the owning bot shuts down.

Fields:

Field Type Default Description
rules Dict[str, ExecutorSpec]

Method ExecutionPolicy.match

Return the (rule_key, spec) that governs tool, if any.

match(tool: Any) -> Optional[Tuple[str, ExecutorSpec]]

Parameter Type Default Description
tool Any

Returns: Optional[Tuple[str, ExecutorSpec]]

Method ExecutionPolicy.resolve

Return the executor for tool, instantiating (and caching) the matched spec on first use so all tools sharing a rule share one executor instance.

resolve(tool: Any) -> Optional[AbstractToolExecutor]

Parameter Type Default Description
tool Any

Returns: Optional[AbstractToolExecutor]

Method ExecutionPolicy.route_for

Return the route for tool, or None when no rule applies to it (FEAT-933).

Nothing is written onto tool. A matched rule is skipped for a tenant-bound tool — its tool scope lives in this process only, the predicate behind execution_gates.refuse_remote_executor — so the tool runs in-process. The rule's remote_timeout_seconds / webhook_callback_url override the tool's own values when set. The matched rule's executor is instantiated (and cached) on first use.

route_for(tool: Any) -> Optional[ToolExecutionRoute]

Parameter Type Default Description
tool Any The tool about to run, or being registered.

Returns: Optional[ToolExecutionRoute] — class:~parrot.tools.abstract.ToolExecutionRoute, or None when no rule matches or the tool is tenant-bound.

Method ExecutionPolicy.apply_to_tool

Store the policy's route for tool as its fallback route (FEAT-933).

ToolManager calls this at registration. The route lands in the private tool._policy_route, which a direct tool.execute() outside any ToolManager dispatch uses, and so does a dispatch through a manager with no rule for the tool; a manager whose policy matches resolves its own route per call (:meth:route_for). The tool's executor, remote_timeout_seconds and webhook_callback_url are never written. Explicit per-tool configuration always wins (a constructor executor= is left alone), and the first matching policy keeps the fallback.

apply_to_tool(tool: Any) -> bool

Parameter Type Default Description
tool Any The tool being registered.

Returns: bool — True when a route was stored; False for a constructor executor, an already-stored route, no matching rule, or a tenant-bound tool (the skipped rule is logged as a warning).

Method ExecutionPolicy.close

Close every executor this policy instantiated.

Idempotent. Executors passed in as live instance values are NOT closed — their lifecycle belongs to whoever created them.

close() -> None

Class ExecutorSpec

One executor target: a registry name + constructor options.

Fields:

Field Type Default Description
name str '' Key in :data:EXECUTOR_REGISTRY ("local", "docker", "k8s", "qworker").
options Dict[str, Any] Constructor kwargs for the executor class.
remote_timeout_seconds Optional[int] None When set, overrides the matched tool's remote_timeout_seconds.
webhook_callback_url Optional[str] None When set, overrides the matched tool's webhook_callback_url.
instance Optional[AbstractToolExecutor] None A pre-built executor (code-only shorthand). When set, name/options are ignored and the instance is used as-is; its lifecycle belongs to whoever created it.

Alias ToolExecutionRoute

Re-exported from parrot.tools.abstract.ToolExecutionRoute.

Function build_executor

Instantiate an executor from spec using :data:EXECUTOR_REGISTRY.

build_executor(spec: 'ExecutorSpec | str') -> AbstractToolExecutor

Parameter Type Default Description
spec 'ExecutorSpec | str'

Returns: AbstractToolExecutor

Raises:

  • KeyError — Unknown executor name.
  • NotImplementedError — Reserved-but-unimplemented name ("docker-sandbox").

Alias is_tenant_bound

Re-exported from parrot.tools.scope.is_tenant_bound.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally