agentenv-framework 0.9.1254__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agent_env/__init__.py +0 -0
- agent_env/a2a_agent/__init__.py +23 -0
- agent_env/a2a_agent/a2a_agent.py +600 -0
- agent_env/a2a_agent/conversation_store.py +356 -0
- agent_env/a2a_agent/object_transfer.py +609 -0
- agent_env/a2a_agent/protocol.py +238 -0
- agent_env/a2a_agent/store.py +201 -0
- agent_env/a2a_agent/validator.py +901 -0
- agent_env/artifact/__init__.py +48 -0
- agent_env/artifact/artifact.py +74 -0
- agent_env/artifact/artifacts/__init__.py +0 -0
- agent_env/artifact/artifacts/cli.py +83 -0
- agent_env/artifact/artifacts/docker_image.py +457 -0
- agent_env/artifact/artifacts/environment.py +89 -0
- agent_env/artifact/artifacts/environment_universe.py +156 -0
- agent_env/artifact/artifacts/file.py +214 -0
- agent_env/artifact/artifacts/file_artifact_universe.py +206 -0
- agent_env/artifact/artifacts/skill.py +206 -0
- agent_env/artifact/artifacts/vm_image.py +95 -0
- agent_env/artifact/ref.py +15 -0
- agent_env/artifact/registry.py +251 -0
- agent_env/artifact/store.py +295 -0
- agent_env/artifact/universe.py +38 -0
- agent_env/attribution.py +41 -0
- agent_env/bundle/__init__.py +4 -0
- agent_env/bundle/_fs.py +128 -0
- agent_env/bundle/authoring.py +239 -0
- agent_env/bundle/installed.py +175 -0
- agent_env/bundle/ledger.py +269 -0
- agent_env/bundle/materialize.py +204 -0
- agent_env/bundle/parse.py +594 -0
- agent_env/bundle/plan.py +414 -0
- agent_env/bundle/resolve.py +404 -0
- agent_env/bundle/run.py +361 -0
- agent_env/cli/__init__.py +69 -0
- agent_env/cli/__main__.py +10 -0
- agent_env/cli/_installers.py +576 -0
- agent_env/cli/_plugin_changes.py +947 -0
- agent_env/cli/a2a_agent/__init__.py +22 -0
- agent_env/cli/a2a_agent/add_skill.py +97 -0
- agent_env/cli/a2a_agent/deploy.py +60 -0
- agent_env/cli/a2a_agent/get.py +15 -0
- agent_env/cli/a2a_agent/get_instance.py +26 -0
- agent_env/cli/a2a_agent/put.py +78 -0
- agent_env/cli/a2a_agent/validate.py +56 -0
- agent_env/cli/artifact/__init__.py +20 -0
- agent_env/cli/artifact/cli.py +83 -0
- agent_env/cli/artifact/environment.py +44 -0
- agent_env/cli/artifact/environment_universe.py +163 -0
- agent_env/cli/artifact/file_artifact_universe.py +281 -0
- agent_env/cli/artifact/skill.py +128 -0
- agent_env/cli/banner.py +25 -0
- agent_env/cli/config.py +263 -0
- agent_env/cli/env/__init__.py +31 -0
- agent_env/cli/env/deploy.py +112 -0
- agent_env/cli/env/gateway.py +57 -0
- agent_env/cli/env/get_instance.py +40 -0
- agent_env/cli/env/mcp_server.py +410 -0
- agent_env/cli/env/multi.py +282 -0
- agent_env/cli/env/service_db.py +99 -0
- agent_env/cli/env/snapshot.py +35 -0
- agent_env/cli/env/state/__init__.py +6 -0
- agent_env/cli/env/state/init.py +108 -0
- agent_env/cli/env/state/teardown.py +44 -0
- agent_env/cli/env/website.py +216 -0
- agent_env/cli/env/website_browser.py +67 -0
- agent_env/cli/eval/__init__.py +16 -0
- agent_env/cli/eval/add_tasks.py +63 -0
- agent_env/cli/eval/create.py +49 -0
- agent_env/cli/eval/run.py +194 -0
- agent_env/cli/identity/__init__.py +5 -0
- agent_env/cli/identity/client_identity.py +37 -0
- agent_env/cli/plugin.py +438 -0
- agent_env/cli/run.py +285 -0
- agent_env/cli/task/__init__.py +21 -0
- agent_env/cli/task/create.py +109 -0
- agent_env/cli/task/get.py +17 -0
- agent_env/cli/task/get_instance.py +34 -0
- agent_env/cli/task/run.py +615 -0
- agent_env/cli/task/validate.py +31 -0
- agent_env/cli/up.py +123 -0
- agent_env/cli/utils.py +183 -0
- agent_env/config/__init__.py +50 -0
- agent_env/config/describe.py +1067 -0
- agent_env/config/errors.py +13 -0
- agent_env/config/loader.py +245 -0
- agent_env/config/model.py +154 -0
- agent_env/config/paths.py +25 -0
- agent_env/config/plugin_tables.py +101 -0
- agent_env/config/provenance.py +134 -0
- agent_env/config/runtime.py +1064 -0
- agent_env/config/snapshot.py +81 -0
- agent_env/entity_refs.py +186 -0
- agent_env/env/__init__.py +49 -0
- agent_env/env/env.py +433 -0
- agent_env/env/env_artifact_store.py +87 -0
- agent_env/env/envs/__init__.py +7 -0
- agent_env/env/envs/_deployment.py +281 -0
- agent_env/env/envs/gateway_server.py +32 -0
- agent_env/env/envs/mcp_server.py +387 -0
- agent_env/env/envs/multi_env.py +780 -0
- agent_env/env/envs/service_db/Dockerfile +20 -0
- agent_env/env/envs/service_db/Dockerfile.db-mcp +42 -0
- agent_env/env/envs/service_db/Dockerfile.db-web +8 -0
- agent_env/env/envs/service_db.py +92 -0
- agent_env/env/envs/website.py +294 -0
- agent_env/env/envs/website_browser/Dockerfile +40 -0
- agent_env/env/envs/website_browser/__init__.py +17 -0
- agent_env/env/envs/website_browser/entrypoint.sh +13 -0
- agent_env/env/gateway/Dockerfile +14 -0
- agent_env/env/gateway/__init__.py +37 -0
- agent_env/env/gateway/clock.py +184 -0
- agent_env/env/gateway/constants.py +190 -0
- agent_env/env/gateway/entrypoint.py +156 -0
- agent_env/env/gateway/gateway.py +1095 -0
- agent_env/env/gateway/get_time.py +60 -0
- agent_env/env/gateway/requirements.txt +4 -0
- agent_env/env/gateway/triggers.py +1193 -0
- agent_env/env/legacy_protocol.py +146 -0
- agent_env/env/registry.py +93 -0
- agent_env/env/snapshot_store.py +372 -0
- agent_env/env/store.py +411 -0
- agent_env/eval/__init__.py +14 -0
- agent_env/eval/eval.py +68 -0
- agent_env/eval/store.py +116 -0
- agent_env/examples/hello/README.md +9 -0
- agent_env/examples/hello/artifacts/greeting/check.sh +1 -0
- agent_env/examples/hello/artifacts/greeting/hello.txt +1 -0
- agent_env/examples/hello/tasks/hello.json +10 -0
- agent_env/explorer/__init__.py +12 -0
- agent_env/explorer/app.py +277 -0
- agent_env/explorer/openapi_docs.py +242 -0
- agent_env/explorer/plugin.py +95 -0
- agent_env/explorer/routers/__init__.py +1 -0
- agent_env/explorer/routers/common.py +174 -0
- agent_env/explorer/routers/conversations.py +25 -0
- agent_env/explorer/routers/objects.py +112 -0
- agent_env/explorer/routers/runs.py +475 -0
- agent_env/plugins/__init__.py +59 -0
- agent_env/plugins/_cli.py +244 -0
- agent_env/plugins/_discovery.py +97 -0
- agent_env/plugins/_inventory.py +262 -0
- agent_env/plugins/_registration.py +268 -0
- agent_env/plugins/_report.py +47 -0
- agent_env/plugins/_requirements.py +55 -0
- agent_env/providers/__init__.py +37 -0
- agent_env/providers/env_providers/__init__.py +12 -0
- agent_env/providers/env_providers/constants.py +13 -0
- agent_env/providers/env_providers/env_gateway_provider.py +1354 -0
- agent_env/providers/env_providers/env_provider.py +237 -0
- agent_env/providers/env_providers/env_server_provider.py +133 -0
- agent_env/providers/env_state/__init__.py +49 -0
- agent_env/providers/env_state/env_state_provider.py +554 -0
- agent_env/providers/env_state/local_postgres.py +390 -0
- agent_env/providers/env_state/store.py +112 -0
- agent_env/providers/sandbox_providers/__init__.py +29 -0
- agent_env/providers/sandbox_providers/chained_sandbox_provider.py +93 -0
- agent_env/providers/sandbox_providers/e2b/__init__.py +6 -0
- agent_env/providers/sandbox_providers/e2b/provider.py +376 -0
- agent_env/providers/sandbox_providers/e2b/sandbox.py +342 -0
- agent_env/providers/sandbox_providers/e2b/template.py +158 -0
- agent_env/providers/sandbox_providers/local_sandbox.py +408 -0
- agent_env/providers/sandbox_providers/modal_sandbox.py +500 -0
- agent_env/providers/sandbox_providers/modal_vm_sandbox.py +466 -0
- agent_env/providers/sandbox_providers/sandbox.py +387 -0
- agent_env/providers/sandbox_providers/sandbox_provider.py +614 -0
- agent_env/py.typed +0 -0
- agent_env/runner/__init__.py +16 -0
- agent_env/runner/local_runner.py +190 -0
- agent_env/runner/runner.py +127 -0
- agent_env/runner/store.py +94 -0
- agent_env/store/__init__.py +126 -0
- agent_env/store/_google.py +68 -0
- agent_env/store/base.py +33 -0
- agent_env/store/document_store/__init__.py +60 -0
- agent_env/store/document_store/document_store.py +519 -0
- agent_env/store/document_store/dynamodb_document_store.py +306 -0
- agent_env/store/document_store/evaluation.py +199 -0
- agent_env/store/document_store/firestore_mongo_document_store.py +242 -0
- agent_env/store/document_store/mongo_document_store.py +259 -0
- agent_env/store/document_store/sqlite_document_store.py +314 -0
- agent_env/store/ids.py +123 -0
- agent_env/store/image_store/__init__.py +34 -0
- agent_env/store/image_store/ecr_image_store.py +131 -0
- agent_env/store/image_store/google_credentials.py +113 -0
- agent_env/store/image_store/image_store.py +113 -0
- agent_env/store/image_store/local_registry_image_store.py +106 -0
- agent_env/store/image_store/oci_registry_credentials.py +196 -0
- agent_env/store/local_state.py +27 -0
- agent_env/store/object_store/__init__.py +24 -0
- agent_env/store/object_store/gcs_object_store.py +415 -0
- agent_env/store/object_store/local_object_store.py +143 -0
- agent_env/store/object_store/object_store.py +198 -0
- agent_env/store/object_store/s3_object_store.py +354 -0
- agent_env/store/query.py +112 -0
- agent_env/store/routing.py +609 -0
- agent_env/store/secret_store/__init__.py +19 -0
- agent_env/store/secret_store/aws_secrets_manager_secret_store.py +293 -0
- agent_env/store/secret_store/gcp_secret_manager_secret_store.py +319 -0
- agent_env/store/secret_store/local_secret_store.py +47 -0
- agent_env/store/secret_store/secret_store.py +19 -0
- agent_env/task/__init__.py +27 -0
- agent_env/task/interrupts.py +109 -0
- agent_env/task/registry.py +27 -0
- agent_env/task/step_journal.py +99 -0
- agent_env/task/store.py +940 -0
- agent_env/task/task.py +815 -0
- agent_env/task/teardown.py +142 -0
- agent_env/task_step/__init__.py +47 -0
- agent_env/task_step/context.py +195 -0
- agent_env/task_step/context_ops.py +224 -0
- agent_env/task_step/registry.py +182 -0
- agent_env/task_step/review_store.py +118 -0
- agent_env/task_step/snapshot_utils/__init__.py +2 -0
- agent_env/task_step/snapshot_utils/agent_state_capture.py +261 -0
- agent_env/task_step/snapshot_utils/snapshot_series.py +631 -0
- agent_env/task_step/store.py +129 -0
- agent_env/task_step/task_step.py +173 -0
- agent_env/task_step/task_steps/__init__.py +27 -0
- agent_env/task_step/task_steps/a2a_agent_validator/__init__.py +31 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/README.md +158 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/clip.m4a +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/clip.mp3 +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/clip.mp4 +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/clip.ogg +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/clip.wav +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/document.pdf +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/red.gif +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/red.jpg +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/fixtures/red.png +0 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_agent_card.py +103 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_agent_config_identity.py +69 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_agent_mcp.py +178 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_core_protocol.py +67 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_install.py +115 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_litellm_attribution.py +124 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_litellm_attribution_runtime.py +173 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_modalities.py +270 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_peer_agents.py +102 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_role.py +151 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_skill_config.py +194 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_snapshot.py +111 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_system_prompt.py +215 -0
- agent_env/task_step/task_steps/a2a_agent_validator/verify_a2a_trajectory.py +164 -0
- agent_env/task_step/task_steps/add_skills.py +240 -0
- agent_env/task_step/task_steps/apply_server_config.py +288 -0
- agent_env/task_step/task_steps/collect_artifacts.py +940 -0
- agent_env/task_step/task_steps/deploy_agent.py +626 -0
- agent_env/task_step/task_steps/deploy_env.py +197 -0
- agent_env/task_step/task_steps/deploy_human_agent.py +100 -0
- agent_env/task_step/task_steps/deploy_sandbox.py +170 -0
- agent_env/task_step/task_steps/env_card_validator/__init__.py +4 -0
- agent_env/task_step/task_steps/env_card_validator/verify_env_card.py +109 -0
- agent_env/task_step/task_steps/env_card_validator/verify_env_core_protocol.py +108 -0
- agent_env/task_step/task_steps/install_agent.py +376 -0
- agent_env/task_step/task_steps/load_artifact.py +775 -0
- agent_env/task_step/task_steps/mcp_cli_builder/__init__.py +4 -0
- agent_env/task_step/task_steps/mcp_cli_builder/build_mcp_cli.py +201 -0
- agent_env/task_step/task_steps/mcp_cli_builder/codegen.py +627 -0
- agent_env/task_step/task_steps/mcp_env_validator/__init__.py +44 -0
- agent_env/task_step/task_steps/mcp_env_validator/validation_gate_aggregator.py +102 -0
- agent_env/task_step/task_steps/mcp_env_validator/verify_mcp_env_assessment.py +78 -0
- agent_env/task_step/task_steps/mcp_env_validator/verify_mcp_tool_schema.py +152 -0
- agent_env/task_step/task_steps/mcp_env_validator/verify_spec_conformance.py +486 -0
- agent_env/task_step/task_steps/modify_env_tool_access.py +80 -0
- agent_env/task_step/task_steps/multienv_validator/__init__.py +3 -0
- agent_env/task_step/task_steps/multienv_validator/combine_universe_verdicts.py +107 -0
- agent_env/task_step/task_steps/multienv_validator/universe_comparison.py +253 -0
- agent_env/task_step/task_steps/multienv_validator/verify_universe_agent_judge.py +180 -0
- agent_env/task_step/task_steps/multienv_validator/verify_universe_roundtrip.py +241 -0
- agent_env/task_step/task_steps/peer_agents.py +92 -0
- agent_env/task_step/task_steps/prompt_agent.py +900 -0
- agent_env/task_step/task_steps/register_agent_triggers.py +113 -0
- agent_env/task_step/task_steps/register_env_triggers.py +97 -0
- agent_env/task_step/task_steps/reset_env.py +71 -0
- agent_env/task_step/task_steps/review.py +152 -0
- agent_env/task_step/task_steps/run_code.py +461 -0
- agent_env/task_step/task_steps/run_code_runner.py +52 -0
- agent_env/task_step/task_steps/run_docker_container.py +380 -0
- agent_env/task_step/task_steps/sandbox_utils/__init__.py +0 -0
- agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py +79 -0
- agent_env/task_step/task_steps/snapshot_agent_state.py +205 -0
- agent_env/task_step/task_steps/snapshot_env.py +637 -0
- agent_env/task_step/task_steps/sync_env_clock.py +132 -0
- agent_env/task_step/task_steps/teardown_sandboxes.py +170 -0
- agent_env/task_step/task_steps/verifiers/__init__.py +23 -0
- agent_env/task_step/task_steps/verifiers/agent_prompt_response_verifier.py +166 -0
- agent_env/task_step/task_steps/verifiers/aggregate_verifiers.py +90 -0
- agent_env/task_step/task_steps/verifiers/env_outcome_verifier.py +127 -0
- agent_env/task_step/task_steps/verifiers/judge_utils/__init__.py +2 -0
- agent_env/task_step/task_steps/verifiers/judge_utils/frame_selection.py +377 -0
- agent_env/task_step/task_steps/verifiers/judge_utils/judge_output_format.py +1293 -0
- agent_env/task_step/task_steps/verifiers/judge_utils/trajectory_filter.py +514 -0
- agent_env/task_step/task_steps/verifiers/rubrics_verifier.py +1318 -0
- agent_env/task_step/task_steps/verifiers/run_container_unit_tests_verifier.py +434 -0
- agent_env/task_step/task_steps/verifiers/scoring.py +40 -0
- agent_env/task_step/task_steps/verifiers/verify_sandbox.py +308 -0
- agent_env/task_step/thread_work.py +61 -0
- agent_env/utils/__init__.py +0 -0
- agent_env/utils/card_naming.py +108 -0
- agent_env/utils/deprecation.py +109 -0
- agent_env/utils/docker_build.py +29 -0
- agent_env/utils/exec_retry.py +77 -0
- agent_env/utils/litellm_attribution.py +59 -0
- agent_env/utils/paths.py +20 -0
- agentenv_framework-0.9.1254.dist-info/METADATA +394 -0
- agentenv_framework-0.9.1254.dist-info/RECORD +312 -0
- agentenv_framework-0.9.1254.dist-info/WHEEL +4 -0
- agentenv_framework-0.9.1254.dist-info/entry_points.txt +5 -0
- agentenv_framework-0.9.1254.dist-info/licenses/LICENSE +202 -0
- agentenv_framework-0.9.1254.dist-info/licenses/NOTICE +4 -0
- agentenv_framework-0.9.1254.dist-info/licenses/THIRD_PARTY_NOTICES.md +7665 -0
agent_env/env/store.py
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
"""Env store for MongoDB persistence."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import dataclasses
|
|
6
|
+
import logging
|
|
7
|
+
import random
|
|
8
|
+
import string
|
|
9
|
+
from datetime import datetime, timedelta, timezone
|
|
10
|
+
from typing import TYPE_CHECKING, Any, Optional, Self, TypeVar
|
|
11
|
+
|
|
12
|
+
from agent_env.plugins import _registration
|
|
13
|
+
from agent_env.store.base import ConcurrentModificationError, NotFoundError
|
|
14
|
+
from agent_env.config import get_config
|
|
15
|
+
from agent_env.store.document_store import (
|
|
16
|
+
AbsentOrNull,
|
|
17
|
+
DocumentStore,
|
|
18
|
+
Filter,
|
|
19
|
+
LteOrAbsent,
|
|
20
|
+
Sort,
|
|
21
|
+
UpdateSpec,
|
|
22
|
+
VersionedEntityStore,
|
|
23
|
+
VersionedEntityStoreCache,
|
|
24
|
+
)
|
|
25
|
+
from agent_env.store.query import QueryBuilder, to_document_query
|
|
26
|
+
|
|
27
|
+
if TYPE_CHECKING:
|
|
28
|
+
from agent_env.env.env import DeployedEnv, Env
|
|
29
|
+
|
|
30
|
+
_E = TypeVar("_E", bound="Env")
|
|
31
|
+
|
|
32
|
+
logger = logging.getLogger(__name__)
|
|
33
|
+
|
|
34
|
+
ENVS_COLLECTION = "envs"
|
|
35
|
+
ENV_INSTANCES_COLLECTION = "env_instances"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class EnvQuery(QueryBuilder["Env"]):
|
|
39
|
+
def __init__(self, store: Optional["EnvStore"] = None) -> None:
|
|
40
|
+
super().__init__()
|
|
41
|
+
self._store = store
|
|
42
|
+
|
|
43
|
+
def _clone(self) -> Self:
|
|
44
|
+
clone = EnvQuery(self._store)
|
|
45
|
+
clone._filters = self._filters.copy()
|
|
46
|
+
clone._sort_field = self._sort_field
|
|
47
|
+
clone._sort_desc = self._sort_desc
|
|
48
|
+
clone._limit_value = self._limit_value
|
|
49
|
+
clone._offset_value = self._offset_value
|
|
50
|
+
return clone
|
|
51
|
+
|
|
52
|
+
def type(self, env_type: str) -> Self:
|
|
53
|
+
return self._add_filter("type", env_type)
|
|
54
|
+
|
|
55
|
+
def execute(self) -> list[Env]:
|
|
56
|
+
if self._store is None:
|
|
57
|
+
self._store = get_env_store()
|
|
58
|
+
return self._store.execute_query(self)
|
|
59
|
+
|
|
60
|
+
def _execute_count(self) -> int:
|
|
61
|
+
if self._store is None:
|
|
62
|
+
self._store = get_env_store()
|
|
63
|
+
return self._store.execute_count(self)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class EnvStore:
|
|
67
|
+
def __init__(self) -> None:
|
|
68
|
+
self._versioned_cache: VersionedEntityStoreCache[Env] = VersionedEntityStoreCache(
|
|
69
|
+
ENVS_COLLECTION, self._serialize, self._deserialize, secondary_indexes=[["type"]],
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def _doc_store(self):
|
|
74
|
+
return get_config().get_document_store()
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def _versioned(self) -> VersionedEntityStore[Env]:
|
|
78
|
+
return self._versioned_cache.for_store(self._doc_store)
|
|
79
|
+
|
|
80
|
+
def _serialize(self, env: Env) -> dict:
|
|
81
|
+
doc = env.to_dict()
|
|
82
|
+
doc["created_at_utc"] = datetime.now(timezone.utc)
|
|
83
|
+
return doc
|
|
84
|
+
|
|
85
|
+
def get(self, id: str, version: Optional[int] = None) -> Env:
|
|
86
|
+
env = self._versioned.get(id, version)
|
|
87
|
+
if env is None:
|
|
88
|
+
version_str = f" version={version}" if version is not None else ""
|
|
89
|
+
raise NotFoundError(f"Env {id}{version_str} not found")
|
|
90
|
+
return env
|
|
91
|
+
|
|
92
|
+
def next_version(self, id: str) -> int:
|
|
93
|
+
return self._versioned.next_version(id)
|
|
94
|
+
|
|
95
|
+
def put_document(self, env: _E) -> _E:
|
|
96
|
+
store = self._doc_store
|
|
97
|
+
existing = store.find_one(
|
|
98
|
+
ENVS_COLLECTION, Filter.of(id=env.id), sort=Sort.by("version", descending=True)
|
|
99
|
+
)
|
|
100
|
+
if existing and existing.get("type") != env.type:
|
|
101
|
+
raise ValueError(f"Env id '{env.id}' already exists as type '{existing.get('type')}'; cannot put as type '{env.type}'.")
|
|
102
|
+
env.version = self._versioned_cache.for_store(store).put(env)
|
|
103
|
+
return env
|
|
104
|
+
|
|
105
|
+
def update_metadata(
|
|
106
|
+
self,
|
|
107
|
+
id: str,
|
|
108
|
+
version: int,
|
|
109
|
+
old_metadata: dict[str, Any],
|
|
110
|
+
new_metadata: dict[str, Any],
|
|
111
|
+
) -> dict[str, Any]:
|
|
112
|
+
"""Replace an env's metadata using a timestamp-based compare-and-swap.
|
|
113
|
+
|
|
114
|
+
The update succeeds if the document's ``metadata.updated_at`` is older
|
|
115
|
+
than or equal to the caller's snapshot, or if the field does not yet
|
|
116
|
+
exist. On success both ``metadata`` and its ``updated_at`` field are
|
|
117
|
+
written atomically.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
id: Env ID.
|
|
121
|
+
version: Env version.
|
|
122
|
+
old_metadata: Previously loaded metadata (used to read the old timestamp).
|
|
123
|
+
new_metadata: The new metadata to set.
|
|
124
|
+
|
|
125
|
+
Raises:
|
|
126
|
+
NotFoundError: If (id, version) does not exist.
|
|
127
|
+
ConcurrentModificationError: If metadata was modified by a newer writer.
|
|
128
|
+
"""
|
|
129
|
+
now = datetime.now(timezone.utc).isoformat()
|
|
130
|
+
old_ts = old_metadata.get("updated_at")
|
|
131
|
+
new_metadata = {**new_metadata, "updated_at": now}
|
|
132
|
+
|
|
133
|
+
ts_pred = LteOrAbsent(old_ts) if old_ts is not None else AbsentOrNull()
|
|
134
|
+
store = self._doc_store
|
|
135
|
+
matched = store.update(
|
|
136
|
+
ENVS_COLLECTION,
|
|
137
|
+
Filter.of(id=id, version=version).where("metadata.updated_at", ts_pred),
|
|
138
|
+
UpdateSpec(set={"metadata": new_metadata}),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
if matched == 0:
|
|
142
|
+
if store.find_one(ENVS_COLLECTION, Filter.of(id=id, version=version)) is None:
|
|
143
|
+
raise NotFoundError(f"Env {id} version={version} not found")
|
|
144
|
+
raise ConcurrentModificationError(
|
|
145
|
+
f"Env {id} version={version} metadata was modified concurrently"
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
return new_metadata
|
|
149
|
+
|
|
150
|
+
def _deserialize(self, doc: dict) -> Env:
|
|
151
|
+
from agent_env.env.registry import get_env_registry
|
|
152
|
+
|
|
153
|
+
doc.pop("_id", None)
|
|
154
|
+
doc.pop("created_at_utc", None)
|
|
155
|
+
env_type = doc["type"]
|
|
156
|
+
registry = get_env_registry()
|
|
157
|
+
cls = registry.get(env_type)
|
|
158
|
+
if cls is None:
|
|
159
|
+
raise ValueError(f"Unknown env type: {env_type}{_registration.failure_note(_registration.ENVS, env_type)}")
|
|
160
|
+
return cls.from_dict(doc)
|
|
161
|
+
|
|
162
|
+
def execute_query(self, query: EnvQuery) -> list[Env]:
|
|
163
|
+
filt, sort = to_document_query(query)
|
|
164
|
+
return self._versioned.query(filt, sort, query._limit_value, query._offset_value)
|
|
165
|
+
|
|
166
|
+
def execute_count(self, query: EnvQuery) -> int:
|
|
167
|
+
filt, _ = to_document_query(query)
|
|
168
|
+
return self._versioned.count(filt)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
_env_store: Optional[EnvStore] = None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def get_env_store() -> EnvStore:
|
|
175
|
+
global _env_store
|
|
176
|
+
if _env_store is None:
|
|
177
|
+
_env_store = EnvStore()
|
|
178
|
+
return _env_store
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def set_env_store(store: EnvStore) -> None:
|
|
182
|
+
global _env_store
|
|
183
|
+
_env_store = store
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def reset_env_store() -> None:
|
|
187
|
+
global _env_store
|
|
188
|
+
_env_store = None
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
# --- EnvInstance store (non-versioned, tracks deployed env instances) ---
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class EnvInstanceStore:
|
|
195
|
+
def __init__(self) -> None:
|
|
196
|
+
self._indexed = None
|
|
197
|
+
|
|
198
|
+
@property
|
|
199
|
+
def _doc_store(self):
|
|
200
|
+
# Resolved per call: a cached store outlives reset_config(), so a process that
|
|
201
|
+
# re-pointed would read the new config and write the old backend.
|
|
202
|
+
store = get_config().get_document_store()
|
|
203
|
+
if self._indexed is not store:
|
|
204
|
+
store.ensure_index(ENV_INSTANCES_COLLECTION, ["instance_id"], unique=True)
|
|
205
|
+
self._indexed = store
|
|
206
|
+
return store
|
|
207
|
+
|
|
208
|
+
def create_instance(self, deployed_env: DeployedEnv, ttl_seconds: int) -> DeployedEnv:
|
|
209
|
+
"""Persist a deployed env as an instance record. Returns a new DeployedEnv with instance_id set."""
|
|
210
|
+
from agent_env.env.env import DeployedEnv as DeployedEnvCls
|
|
211
|
+
|
|
212
|
+
suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
|
|
213
|
+
instance_id = f"{deployed_env.env_id}-{suffix}"
|
|
214
|
+
now = datetime.now(timezone.utc)
|
|
215
|
+
created_at_utc = now.strftime("%Y-%m-%d %H:%M UTC")
|
|
216
|
+
expires_at_utc = (now + timedelta(seconds=ttl_seconds)).strftime("%Y-%m-%d %H:%M UTC")
|
|
217
|
+
doc = dataclasses.asdict(deployed_env) | {
|
|
218
|
+
"instance_id": instance_id,
|
|
219
|
+
"created_at_utc": created_at_utc,
|
|
220
|
+
"expires_at_utc": expires_at_utc,
|
|
221
|
+
}
|
|
222
|
+
self._doc_store.insert(ENV_INSTANCES_COLLECTION, doc)
|
|
223
|
+
return DeployedEnvCls.from_dict(doc)
|
|
224
|
+
|
|
225
|
+
def get(self, instance_id: str) -> DeployedEnv:
|
|
226
|
+
"""Look up a deployed env by instance ID."""
|
|
227
|
+
from agent_env.env.env import DeployedEnv as DeployedEnvCls
|
|
228
|
+
|
|
229
|
+
doc = self._doc_store.find_one(ENV_INSTANCES_COLLECTION, Filter.of(instance_id=instance_id))
|
|
230
|
+
if not doc:
|
|
231
|
+
raise NotFoundError(f"EnvInstance '{instance_id}' not found")
|
|
232
|
+
return DeployedEnvCls.from_dict(doc)
|
|
233
|
+
|
|
234
|
+
def set_environment_universe(self, instance_id: str, artifact_id: str, artifact_version: int) -> None:
|
|
235
|
+
"""Record which EnvironmentUniverseArtifact was loaded into this instance.
|
|
236
|
+
|
|
237
|
+
Writes both spellings (dual-write). Unlike every other
|
|
238
|
+
renamed surface — all of which are versioned immutable puts, where an
|
|
239
|
+
old-build writer merely omits the new key and a backfill catch-up repairs
|
|
240
|
+
it — this one mutates in place. An old-build writer here overwrites only
|
|
241
|
+
``service_universe``, leaving the pair *present but disagreeing*, which no
|
|
242
|
+
``{$exists: false}`` check can detect. Hence the dual-read below.
|
|
243
|
+
"""
|
|
244
|
+
subdoc = {"id": artifact_id, "version": artifact_version}
|
|
245
|
+
self._doc_store.update(
|
|
246
|
+
ENV_INSTANCES_COLLECTION,
|
|
247
|
+
Filter.of(instance_id=instance_id),
|
|
248
|
+
UpdateSpec(set={"service_universe": subdoc, "environment_universe": subdoc}),
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
def get_loaded_environments(self, instance_id: str, universe_id: str, universe_version: int) -> list[str]:
|
|
252
|
+
"""Which of a universe's services are already loaded into this instance.
|
|
253
|
+
|
|
254
|
+
Durable rather than context-carried on purpose. A runner may retry a step from the
|
|
255
|
+
context as it was before the step started, so progress recorded mid-step never
|
|
256
|
+
reaches the context a retry restores. The document store is the only place this
|
|
257
|
+
survives a step retry.
|
|
258
|
+
|
|
259
|
+
Keyed by universe id AND version, so loading a different universe -- or a new
|
|
260
|
+
version of the same one -- correctly resumes from nothing.
|
|
261
|
+
"""
|
|
262
|
+
return self._loaded_environments(self._doc_store, instance_id, universe_id, universe_version)
|
|
263
|
+
|
|
264
|
+
def _loaded_environments(
|
|
265
|
+
self, store: DocumentStore, instance_id: str, universe_id: str, universe_version: int,
|
|
266
|
+
) -> list[str]:
|
|
267
|
+
doc = store.find_one(ENV_INSTANCES_COLLECTION, Filter.of(instance_id=instance_id))
|
|
268
|
+
if not doc:
|
|
269
|
+
return []
|
|
270
|
+
progress = doc.get("loaded_environments") or {}
|
|
271
|
+
if not isinstance(progress, dict):
|
|
272
|
+
return []
|
|
273
|
+
names = progress.get(f"{universe_id}:{universe_version}") or []
|
|
274
|
+
return [n for n in names if isinstance(n, str)]
|
|
275
|
+
|
|
276
|
+
def record_loaded_environment(
|
|
277
|
+
self, instance_id: str, universe_id: str, universe_version: int, environment_name: str,
|
|
278
|
+
) -> None:
|
|
279
|
+
"""Mark one service as loaded. Called only after its data add has succeeded.
|
|
280
|
+
|
|
281
|
+
"After" is load-bearing: a service whose reset succeeded but whose re-add failed is
|
|
282
|
+
EMPTY, and recording it earlier would let a retry skip it and leave the env silently
|
|
283
|
+
missing data.
|
|
284
|
+
"""
|
|
285
|
+
key = f"loaded_environments.{universe_id}:{universe_version}"
|
|
286
|
+
store = self._doc_store
|
|
287
|
+
existing = set(self._loaded_environments(store, instance_id, universe_id, universe_version))
|
|
288
|
+
existing.add(environment_name)
|
|
289
|
+
store.update(
|
|
290
|
+
ENV_INSTANCES_COLLECTION,
|
|
291
|
+
Filter.of(instance_id=instance_id),
|
|
292
|
+
UpdateSpec(set={key: sorted(existing)}),
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
def clear_loaded_environments(self, instance_id: str) -> None:
|
|
296
|
+
"""Drop all resume state for an instance (a snapshot restore replaces everything)."""
|
|
297
|
+
self._doc_store.update(
|
|
298
|
+
ENV_INSTANCES_COLLECTION,
|
|
299
|
+
Filter.of(instance_id=instance_id),
|
|
300
|
+
UpdateSpec(set={"loaded_environments": {}}),
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
def merge_metadata(self, instance_id: str, values: dict) -> None:
|
|
304
|
+
"""Merge key/values into an instance record's ``metadata``.
|
|
305
|
+
|
|
306
|
+
``create_instance`` runs inside ``Env.deploy()``, so anything set on the
|
|
307
|
+
DeployedEnv *after* deploy() returns lands only on the in-memory object
|
|
308
|
+
and never reaches the record. Callers that need a value persisted use
|
|
309
|
+
this instead of assigning to ``deployed_env.metadata``.
|
|
310
|
+
|
|
311
|
+
Two writes, one of which applies. ``create_instance`` persists
|
|
312
|
+
``asdict(deployed_env)``, so an unset metadata field is stored as **null**
|
|
313
|
+
rather than being absent — and MongoDB refuses to create a child under a
|
|
314
|
+
scalar, failing ``$set {"metadata.x": ...}`` with PathNotViable. Callers
|
|
315
|
+
treat this helper as best-effort, so that error would be swallowed and the
|
|
316
|
+
annotation lost precisely on the records that need it. Backends disagree
|
|
317
|
+
here too (the local sqlite store rewrites the null, mongomock silently
|
|
318
|
+
no-ops), so the null shape is normalised explicitly instead of relying on
|
|
319
|
+
any of them.
|
|
320
|
+
|
|
321
|
+
The seed writes the whole subdocument, and only matches when metadata is
|
|
322
|
+
null or absent. Otherwise the dotted merge runs, which sets keys
|
|
323
|
+
individually so concurrent writers of different keys don't clobber each
|
|
324
|
+
other.
|
|
325
|
+
"""
|
|
326
|
+
if not values:
|
|
327
|
+
return
|
|
328
|
+
store = self._doc_store
|
|
329
|
+
seeded = store.update(
|
|
330
|
+
ENV_INSTANCES_COLLECTION,
|
|
331
|
+
Filter.of(instance_id=instance_id).where("metadata", AbsentOrNull()),
|
|
332
|
+
UpdateSpec(set={"metadata": dict(values)}),
|
|
333
|
+
)
|
|
334
|
+
if seeded:
|
|
335
|
+
return
|
|
336
|
+
store.update(
|
|
337
|
+
ENV_INSTANCES_COLLECTION,
|
|
338
|
+
Filter.of(instance_id=instance_id),
|
|
339
|
+
UpdateSpec(set={f"metadata.{key}": value for key, value in values.items()}),
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
def get_environment_universe(self, instance_id: str) -> dict | None:
|
|
343
|
+
"""Return the loaded-universe dict for an instance, or None if not set.
|
|
344
|
+
|
|
345
|
+
Reads the legacy key first, deliberately: every writer at every SDK
|
|
346
|
+
version sets it, so it is never the stale half of a divergent pair. The
|
|
347
|
+
new key is a fallback for docs written once the old one is dropped.
|
|
348
|
+
"""
|
|
349
|
+
doc = self._doc_store.find_one(ENV_INSTANCES_COLLECTION, Filter.of(instance_id=instance_id))
|
|
350
|
+
if not doc:
|
|
351
|
+
raise NotFoundError(f"EnvInstance '{instance_id}' not found")
|
|
352
|
+
return doc.get("service_universe") or doc.get("environment_universe")
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def register_env_instance(deployed_env: DeployedEnv, ttl_seconds: int) -> DeployedEnv:
|
|
356
|
+
_require_round_trip(deployed_env)
|
|
357
|
+
try:
|
|
358
|
+
return get_env_instance_store().create_instance(deployed_env, ttl_seconds=ttl_seconds)
|
|
359
|
+
except Exception:
|
|
360
|
+
logger.warning("Failed to create env instance record", exc_info=True)
|
|
361
|
+
return deployed_env
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def update_env_instance_metadata(instance_id: str, values: dict) -> None:
|
|
365
|
+
"""Persist metadata onto an existing env instance record (best-effort).
|
|
366
|
+
|
|
367
|
+
Non-fatal like ``register_env_instance``: instance records are an
|
|
368
|
+
observability/attribution surface, so failing to annotate one must not fail a
|
|
369
|
+
deploy that otherwise succeeded.
|
|
370
|
+
"""
|
|
371
|
+
try:
|
|
372
|
+
get_env_instance_store().merge_metadata(instance_id, values)
|
|
373
|
+
except Exception:
|
|
374
|
+
logger.warning("Failed to update env instance metadata", exc_info=True)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def update_env_instance_environment_universe(instance_id: str, artifact_id: str, artifact_version: int) -> None:
|
|
378
|
+
try:
|
|
379
|
+
get_env_instance_store().set_environment_universe(instance_id, artifact_id, artifact_version)
|
|
380
|
+
except Exception:
|
|
381
|
+
logger.warning("Failed to update env instance service_universe", exc_info=True)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
_env_instance_store: Optional[EnvInstanceStore] = None
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def get_env_instance_store() -> EnvInstanceStore:
|
|
388
|
+
global _env_instance_store
|
|
389
|
+
if _env_instance_store is None:
|
|
390
|
+
_env_instance_store = EnvInstanceStore()
|
|
391
|
+
return _env_instance_store
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def set_env_instance_store(store: EnvInstanceStore) -> None:
|
|
395
|
+
global _env_instance_store
|
|
396
|
+
_env_instance_store = store
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def reset_env_instance_store() -> None:
|
|
400
|
+
global _env_instance_store
|
|
401
|
+
_env_instance_store = None
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def _require_round_trip(deployed_env: DeployedEnv) -> None:
|
|
405
|
+
"""Refuse a record that would load back as another class, dropping the fields its own class adds."""
|
|
406
|
+
from agent_env.env.env import _record_class
|
|
407
|
+
|
|
408
|
+
loads_as = _record_class(dataclasses.asdict(deployed_env))
|
|
409
|
+
if type(deployed_env) is not loads_as:
|
|
410
|
+
raise TypeError(f"a {type(deployed_env).__name__} record loads back as {loads_as.__name__}, dropping its own "
|
|
411
|
+
f"fields; deploy() must return a {loads_as.__name__}")
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Eval module for AgentEnv."""
|
|
2
|
+
|
|
3
|
+
from .eval import Eval, EvalTask
|
|
4
|
+
from .store import EvalQuery, EvalStore, get_eval_store, reset_eval_store, set_eval_store
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"Eval",
|
|
8
|
+
"EvalTask",
|
|
9
|
+
"EvalStore",
|
|
10
|
+
"EvalQuery",
|
|
11
|
+
"get_eval_store",
|
|
12
|
+
"set_eval_store",
|
|
13
|
+
"reset_eval_store",
|
|
14
|
+
]
|
agent_env/eval/eval.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Eval model for AgentEnv."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import asdict, dataclass
|
|
6
|
+
from typing import TYPE_CHECKING, ClassVar, Optional
|
|
7
|
+
|
|
8
|
+
from agent_env.entity_refs import EntityRef
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from agent_env.eval.store import EvalQuery
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class EvalTask:
|
|
16
|
+
"""A reference to a task within an eval. Version is optional (None = latest)."""
|
|
17
|
+
|
|
18
|
+
task_id: str
|
|
19
|
+
task_version: Optional[int] = None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Eval:
|
|
23
|
+
"""An eval is a versioned collection of task references.
|
|
24
|
+
|
|
25
|
+
Each task reference pins a specific (task_id, task_version) pair.
|
|
26
|
+
Evals are immutable — modifications create new versions.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
type: ClassVar[str] = "eval"
|
|
30
|
+
toml_refs: ClassVar[tuple[EntityRef, ...]] = (EntityRef.task("tasks[]"),)
|
|
31
|
+
|
|
32
|
+
def __init__(self, id: str, version: Optional[int], tasks: list[EvalTask] | None = None):
|
|
33
|
+
self.id = id
|
|
34
|
+
self.version = version
|
|
35
|
+
self.tasks: list[EvalTask] = tasks or []
|
|
36
|
+
|
|
37
|
+
def to_dict(self) -> dict:
|
|
38
|
+
return {
|
|
39
|
+
"id": self.id,
|
|
40
|
+
"type": self.type,
|
|
41
|
+
"version": self.version,
|
|
42
|
+
"tasks": [asdict(t) for t in self.tasks],
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def from_dict(cls, data: dict) -> Eval:
|
|
47
|
+
tasks = [EvalTask(**t) for t in data.get("tasks", [])]
|
|
48
|
+
return cls(id=data["id"], version=data.get("version"), tasks=tasks)
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def get(cls, id: str, version: Optional[int] = None) -> Eval:
|
|
52
|
+
from .store import get_eval_store
|
|
53
|
+
|
|
54
|
+
return get_eval_store().get(id, version)
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def put(cls, **kwargs) -> Eval:
|
|
58
|
+
from .store import get_eval_store
|
|
59
|
+
|
|
60
|
+
kwargs.setdefault("version", None)
|
|
61
|
+
instance = cls(**kwargs)
|
|
62
|
+
return get_eval_store().put_document(instance)
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def query(cls) -> EvalQuery:
|
|
66
|
+
from .store import EvalQuery, get_eval_store
|
|
67
|
+
|
|
68
|
+
return EvalQuery(get_eval_store())
|
agent_env/eval/store.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Eval store for MongoDB persistence."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from typing import TYPE_CHECKING, Optional, Self
|
|
8
|
+
|
|
9
|
+
from agent_env.store.base import NotFoundError
|
|
10
|
+
from agent_env.config import get_config
|
|
11
|
+
from agent_env.store.document_store import VersionedEntityStore, VersionedEntityStoreCache
|
|
12
|
+
from agent_env.store.query import QueryBuilder, to_document_query
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from agent_env.eval.eval import Eval
|
|
16
|
+
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
EVALS_COLLECTION = "evals"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class EvalQuery(QueryBuilder["Eval"]):
|
|
23
|
+
def __init__(self, store: Optional["EvalStore"] = None) -> None:
|
|
24
|
+
super().__init__()
|
|
25
|
+
self._store = store
|
|
26
|
+
|
|
27
|
+
def _clone(self) -> Self:
|
|
28
|
+
clone = EvalQuery(self._store)
|
|
29
|
+
clone._filters = self._filters.copy()
|
|
30
|
+
clone._sort_field = self._sort_field
|
|
31
|
+
clone._sort_desc = self._sort_desc
|
|
32
|
+
clone._limit_value = self._limit_value
|
|
33
|
+
clone._offset_value = self._offset_value
|
|
34
|
+
return clone
|
|
35
|
+
|
|
36
|
+
def type(self, eval_type: str) -> Self:
|
|
37
|
+
return self._add_filter("type", eval_type)
|
|
38
|
+
|
|
39
|
+
def execute(self) -> list[Eval]:
|
|
40
|
+
if self._store is None:
|
|
41
|
+
self._store = get_eval_store()
|
|
42
|
+
return self._store.execute_query(self)
|
|
43
|
+
|
|
44
|
+
def _execute_count(self) -> int:
|
|
45
|
+
if self._store is None:
|
|
46
|
+
self._store = get_eval_store()
|
|
47
|
+
return self._store.execute_count(self)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class EvalStore:
|
|
51
|
+
def __init__(self) -> None:
|
|
52
|
+
self._versioned_cache: VersionedEntityStoreCache[Eval] = VersionedEntityStoreCache(
|
|
53
|
+
EVALS_COLLECTION, self._serialize, self._deserialize, secondary_indexes=[["type"]],
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def _doc_store(self):
|
|
58
|
+
return get_config().get_document_store()
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def _versioned(self) -> VersionedEntityStore[Eval]:
|
|
62
|
+
return self._versioned_cache.for_store(self._doc_store)
|
|
63
|
+
|
|
64
|
+
def _serialize(self, eval_obj: Eval) -> dict:
|
|
65
|
+
doc = eval_obj.to_dict()
|
|
66
|
+
doc["created_at_utc"] = datetime.now(timezone.utc)
|
|
67
|
+
return doc
|
|
68
|
+
|
|
69
|
+
def get(self, id: str, version: Optional[int] = None) -> Eval:
|
|
70
|
+
eval_obj = self._versioned.get(id, version)
|
|
71
|
+
if eval_obj is None:
|
|
72
|
+
version_str = f" version={version}" if version is not None else ""
|
|
73
|
+
raise NotFoundError(f"Eval {id}{version_str} not found")
|
|
74
|
+
return eval_obj
|
|
75
|
+
|
|
76
|
+
def next_version(self, id: str) -> int:
|
|
77
|
+
return self._versioned.next_version(id)
|
|
78
|
+
|
|
79
|
+
def put_document(self, eval_obj: Eval) -> Eval:
|
|
80
|
+
eval_obj.version = self._versioned.put(eval_obj)
|
|
81
|
+
return eval_obj
|
|
82
|
+
|
|
83
|
+
def _deserialize(self, doc: dict) -> Eval:
|
|
84
|
+
from agent_env.eval.eval import Eval
|
|
85
|
+
|
|
86
|
+
doc.pop("_id", None)
|
|
87
|
+
doc.pop("created_at_utc", None)
|
|
88
|
+
return Eval.from_dict(doc)
|
|
89
|
+
|
|
90
|
+
def execute_query(self, query: EvalQuery) -> list[Eval]:
|
|
91
|
+
filt, sort = to_document_query(query)
|
|
92
|
+
return self._versioned.query(filt, sort, query._limit_value, query._offset_value)
|
|
93
|
+
|
|
94
|
+
def execute_count(self, query: EvalQuery) -> int:
|
|
95
|
+
filt, _ = to_document_query(query)
|
|
96
|
+
return self._versioned.count(filt)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
_eval_store: Optional[EvalStore] = None
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def get_eval_store() -> EvalStore:
|
|
103
|
+
global _eval_store
|
|
104
|
+
if _eval_store is None:
|
|
105
|
+
_eval_store = EvalStore()
|
|
106
|
+
return _eval_store
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def set_eval_store(store: EvalStore) -> None:
|
|
110
|
+
global _eval_store
|
|
111
|
+
_eval_store = store
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def reset_eval_store() -> None:
|
|
115
|
+
global _eval_store
|
|
116
|
+
_eval_store = None
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The smallest bundle: load a greeting into a local sandbox and check it.
|
|
2
|
+
|
|
3
|
+
It needs no Docker, model or configuration. `deploy_sandbox` starts a local sandbox (a work
|
|
4
|
+
folder on this machine), `load_artifact` copies `artifacts/greeting/` into it, and
|
|
5
|
+
`verify_sandbox` checks the greeting with a file probe and a shell command. The run removes the
|
|
6
|
+
work folder when it ends; `agent-env run hello --keep` holds it up until Ctrl-C.
|
|
7
|
+
|
|
8
|
+
Run it with `agent-env run hello`, or copy this folder and run the copy with
|
|
9
|
+
`agent-env run ./my-hello`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[[ $(< hello.txt) == *hello* ]]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hello
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{"id": "box", "type": "deploy_sandbox", "sandbox_name": "box", "sandbox_mode": "vm", "sandbox_type": "local"},
|
|
3
|
+
{"id": "load", "type": "load_artifact", "sandbox_name": "box", "artifact_id": "greeting",
|
|
4
|
+
"destination_path": "/app/greeting"},
|
|
5
|
+
{"id": "hello", "type": "verify_sandbox", "sandbox_name": "box", "base_dir": "/app/greeting",
|
|
6
|
+
"verifier_id": "hello", "criteria": [
|
|
7
|
+
{"type": "probe_file_contains", "criterion": "greets", "paths": ["hello.txt"], "expected": "hello"},
|
|
8
|
+
{"type": "bash_cmd_succeeds", "criterion": "check passes", "bash_cmd": "bash check.sh"}
|
|
9
|
+
]}
|
|
10
|
+
]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""The local agent-env explorer (FastAPI). Requires the ``explorer`` extra."""
|
|
2
|
+
|
|
3
|
+
from agent_env.explorer.plugin import ExplorerPlugin, load_plugins
|
|
4
|
+
|
|
5
|
+
__all__ = ["ExplorerPlugin", "load_plugins", "create_app"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def __getattr__(name: str):
|
|
9
|
+
if name == "create_app": # lazy: keeps FastAPI off the import path
|
|
10
|
+
from agent_env.explorer.app import create_app
|
|
11
|
+
return create_app
|
|
12
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|