Skip to content

API Reference parrot.eval.sandbox.state

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

parrot.eval.sandbox.state

View source

State-based sandbox components for the Generic Agent Evaluation Harness.

FEAT-217 — Module 4.

TASK-1418: StateBackend + DictStateBackend TASK-1419: ToolkitBinder, InMemoryStateSandbox, InMemoryStateSandboxProvider, DatabaseToolkitBinder TASK-1420: JiraToolkitBinder (added later)

DictStateBackend is the resettable, in-memory world state owned by the sandbox. It is keyed as {collection: {entity_id: {field: value}}} and produces deterministic snapshots (sorted collections and entity keys) so diffs and baselines are stable.

Class DatabaseToolkitBinder

Binder for DatabaseToolkit (PostgresToolkit) subclasses.

Sets toolkit._connected = True to bypass start() and patches toolkit._acquire_asyncdb_connection to yield a FakeRawConnection backed by the DictStateBackend. Also patches toolkit._resolve_table to return a minimal TableMetadata stub so CRUD method internals work without a warm metadata cache.

The net effect: CRUD tool calls (insert_row, update_row, …) go through the full PostgresToolkit parameter-binding pipeline but the final SQL is routed to FakeRawConnection → DictStateBackend with NO real database connection.

Method DatabaseToolkitBinder.bind

Inject backend into toolkit.

bind(toolkit: Any, backend: 'DictStateBackend') -> None

Parameter Type Default Description
toolkit Any A DatabaseToolkit subclass instance.
backend 'DictStateBackend' The DictStateBackend to use as the world store.

Class DictStateBackend

In-memory {collection: {entity_id: {field: value}}} store.

Provides CRUD-ish helpers used by fake database / Jira drivers: create, get, update, delete, list, query.

Snapshots are:

  • deep copies — callers cannot mutate internal state.
  • deterministic — collections and entity keys are sorted.

Method DictStateBackend.reset

Reset the store.

reset(seed_state: dict[str, Any] | None) -> None

Parameter Type Default Description
seed_state dict[str, Any] | None New initial state. Deep-copied so the caller's dict is not aliased. Pass None to empty the store.

Method DictStateBackend.snapshot

Return a sorted, deep-copied snapshot of the current state.

snapshot() -> dict[str, Any]

Returns: dict[str, Any] — {collection: {entity_id: {field: value}}} with all collection names and entity ids sorted lexicographically.

Method DictStateBackend.create

Insert a new entity.

create(collection: str, entity_id: str, fields: dict[str, Any]) -> None

Parameter Type Default Description
collection str Collection (table) name.
entity_id str Unique identifier for the entity.
fields dict[str, Any] Initial field values.

Raises:

  • KeyError — If entity_id already exists in collection.

Method DictStateBackend.get

Fetch a single entity by id.

get(collection: str, entity_id: str) -> dict[str, Any] | None

Parameter Type Default Description
collection str Collection name.
entity_id str Entity identifier.

Returns: dict[str, Any] | None — A deep copy of the entity's fields, or None if not found.

Method DictStateBackend.update

Merge fields into an existing entity (partial update).

update(collection: str, entity_id: str, fields: dict[str, Any]) -> None

Parameter Type Default Description
collection str Collection name.
entity_id str Entity identifier.
fields dict[str, Any] Fields to merge (values set to None are removed).

Raises:

  • KeyError — If entity_id does not exist in collection.

Method DictStateBackend.delete

Remove an entity.

delete(collection: str, entity_id: str) -> bool

Parameter Type Default Description
collection str Collection name.
entity_id str Entity identifier.

Returns: bool — True if the entity was removed; False if it did not exist.

Method DictStateBackend.list

Return all entities in a collection as a list of dicts.

Each dict contains the entity's fields plus the key "_id" carrying the entity identifier.

list(collection: str) -> list[dict[str, Any]]

Parameter Type Default Description
collection str Collection name.

Returns: list[dict[str, Any]] — List of {_id: entity_id, **fields} dicts (sorted by id).

Method DictStateBackend.query

Return entities in collection where predicate returns True.

query(collection: str, predicate: Callable[[dict[str, Any]], bool]) -> list[dict[str, Any]]

Parameter Type Default Description
collection str Collection name.
predicate Callable[[dict[str, Any]], bool] A callable that receives a field dict (without the _id key) and returns True to include the entity.

Returns: list[dict[str, Any]] — Matching entities as {_id: entity_id, **fields} dicts, sorted by entity id.

Method DictStateBackend.upsert

Insert or update an entity.

upsert(collection: str, entity_id: str, fields: dict[str, Any]) -> None

Parameter Type Default Description
collection str Collection name.
entity_id str Entity identifier.
fields dict[str, Any] Field values to set (merged with existing on update).

Class InMemoryStateSandbox

State-based sandbox that owns a DictStateBackend.

Implements the Sandbox protocol without inheriting to avoid the abstract-method requirement — imported at runtime to avoid a circular dependency with base.py.

Parameter Type Default Description
backend 'DictStateBackend' The DictStateBackend holding world state.
binder ToolkitBinder The ToolkitBinder used to wire toolkits into the backend.

Method InMemoryStateSandbox.reset

Reset the backend to seed_state.

reset(seed_state: dict[str, Any] | None) -> None

Parameter Type Default Description
seed_state dict[str, Any] | None Initial state, or None to empty.

Method InMemoryStateSandbox.health_check

Always healthy (in-memory, no external dependency).

health_check() -> bool

Returns: bool — True.

Method InMemoryStateSandbox.snapshot

Return a sorted, deep-copied snapshot of the backend state.

snapshot() -> dict[str, Any]

Returns: dict[str, Any] — Snapshot dict from the backend.

Method InMemoryStateSandbox.exec

Not supported — raises NotImplementedError.

exec(cmd: list[str]) -> Any

Parameter Type Default Description
cmd list[str] Command list.

Returns: Any

Raises:

  • NotImplementedError — Always.

Method InMemoryStateSandbox.bind

Bind toolkit to the sandbox's backend.

This is called by AgentFactory before the agent starts its rollout so tool calls mutate the in-memory backend.

bind(toolkit: Any) -> None

Parameter Type Default Description
toolkit Any The toolkit instance to wire.

Class InMemoryStateSandboxProvider

Provider that provisions a fresh InMemoryStateSandbox per attempt.

No pooling — each acquire() returns a brand-new backend so attempts are fully independent.

Parameter Type Default Description
binder ToolkitBinder The ToolkitBinder shared across all sandboxes produced by this provider.

Method InMemoryStateSandboxProvider.acquire

Return a fresh InMemoryStateSandbox with an empty backend.

acquire(spec: Any = None) -> InMemoryStateSandbox

Parameter Type Default Description
spec Any None SandboxSpec (seed_state is applied during reset).

Returns: InMemoryStateSandbox — A new InMemoryStateSandbox.

Method InMemoryStateSandboxProvider.release

GC the sandbox (no pool).

release(sandbox: InMemoryStateSandbox) -> None

Parameter Type Default Description
sandbox InMemoryStateSandbox Ignored.

Class JiraToolkitBinder

Binder for JiraToolkit.

Pre-seeds toolkit.jira = FakeJiraClient(backend) so all tool calls that access self.jira go to the in-memory backend. No network calls and no credential_resolver resolution occur.

For non-oauth2_3lo auth modes (e.g. basic_auth, token_auth), JiraToolkit._pre_execute is a no-op and self.jira is used directly by every tool method — patching toolkit.jira is sufficient.

For oauth2_3lo mode the binder additionally pre-seeds toolkit._client_cache with the FakeJiraClient so the cache hit path in _pre_execute returns the fake without network I/O.

Method JiraToolkitBinder.bind

Inject backend into toolkit.

bind(toolkit: Any, backend: 'DictStateBackend') -> None

Parameter Type Default Description
toolkit Any A JiraToolkit instance.
backend 'DictStateBackend' The DictStateBackend to use as the Jira store.

Class StateBackend

Abstract resettable world-state store.

Sandboxes own one StateBackend and delegate reset/snapshot to it. The backend is also the injection point that ToolkitBinder implementations wire into toolkit internals.

Method StateBackend.reset

Reset the store to seed_state (or empty if None).

reset(seed_state: dict[str, Any] | None) -> None

Parameter Type Default Description
seed_state dict[str, Any] | None Initial state keyed as {collection: {entity_id: {field: value}}}. A deep copy is taken so the caller's dict is not aliased.

Method StateBackend.snapshot

Return a deterministic deep copy of the current state.

Collections and entity keys are sorted so that two snapshots taken after the same mutations are byte-equal (stable for diffs and baselines).

snapshot() -> dict[str, Any]

Returns: dict[str, Any] — {collection: {entity_id: {field: value}}} with all keys sorted.

Class ToolkitBinder

Abstract binder that wires a StateBackend into a concrete toolkit.

Each toolkit family (Database, Jira, …) has its own ToolkitBinder subclass that knows the toolkit's internal injection points. This keeps all toolkit-specific code out of the generic sandbox classes.

Method ToolkitBinder.bind

Inject backend into toolkit so tool calls mutate the backend.

bind(toolkit: Any, backend: 'DictStateBackend') -> None

Parameter Type Default Description
toolkit Any The toolkit instance to bind (e.g. PostgresToolkit).
backend 'DictStateBackend' The DictStateBackend that acts as the world store.

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