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
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""In-process runner: runs ``Task.run()`` in the hub's own event loop, bounded by a worker
|
|
2
|
+
semaphore. ``submit()`` fires the run in the background and returns immediately; there is no
|
|
3
|
+
persisted queue or lease, so the run's asyncio task lives only in this process.
|
|
4
|
+
|
|
5
|
+
Consequence: a run does not survive a hub restart. ``start()`` fails any run a previous
|
|
6
|
+
process left non-terminal (it can't still be executing), and ``stop()`` cancels the ones in
|
|
7
|
+
flight. A durable, lease-based runner that resurrects a run across a restart — and supports
|
|
8
|
+
multiple worker processes — is a follow-up; configure an external durable ``[runner]`` where that matters.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import asyncio
|
|
14
|
+
import contextlib
|
|
15
|
+
import logging
|
|
16
|
+
import uuid
|
|
17
|
+
from typing import Optional
|
|
18
|
+
|
|
19
|
+
from agent_env.runner import store as run_store
|
|
20
|
+
from agent_env.runner.runner import RunHandle, RunRecord, Runner, RunStatus
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class LocalRunner(Runner):
|
|
26
|
+
"""In-process runner. The DocumentStore holds run records for status/listing only."""
|
|
27
|
+
|
|
28
|
+
type = "local"
|
|
29
|
+
|
|
30
|
+
def __init__(self, workers: int = 2) -> None:
|
|
31
|
+
if workers < 1:
|
|
32
|
+
raise ValueError(f"workers must be >= 1, got {workers}")
|
|
33
|
+
self.workers = int(workers)
|
|
34
|
+
self._sem = asyncio.Semaphore(self.workers)
|
|
35
|
+
self._inflight: dict[str, asyncio.Task] = {}
|
|
36
|
+
self._stopping = False
|
|
37
|
+
|
|
38
|
+
# --- lifecycle ---------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
async def start(self) -> None:
|
|
41
|
+
"""Reconcile orphans from a previous process: a run left QUEUED/RUNNING has no live
|
|
42
|
+
task here, so fail it rather than leave it hanging. There is no queue to resume."""
|
|
43
|
+
run_store.ensure_indexes()
|
|
44
|
+
orphaned = 0
|
|
45
|
+
for record in run_store.active_runs(self.type):
|
|
46
|
+
run_store.mark_terminal(record.run_id, RunStatus.FAILED, error="interrupted by a hub restart")
|
|
47
|
+
orphaned += 1
|
|
48
|
+
if orphaned:
|
|
49
|
+
logger.info("Failed %d run(s) left non-terminal by a previous process", orphaned)
|
|
50
|
+
|
|
51
|
+
async def stop(self) -> None:
|
|
52
|
+
self._stopping = True
|
|
53
|
+
for task in list(self._inflight.values()):
|
|
54
|
+
task.cancel()
|
|
55
|
+
if self._inflight:
|
|
56
|
+
await asyncio.gather(*self._inflight.values(), return_exceptions=True)
|
|
57
|
+
self._inflight.clear()
|
|
58
|
+
|
|
59
|
+
# --- Runner API --------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
async def submit(
|
|
62
|
+
self,
|
|
63
|
+
task_id: str,
|
|
64
|
+
task_version: Optional[int] = None,
|
|
65
|
+
*,
|
|
66
|
+
agent_model: Optional[str] = None,
|
|
67
|
+
agent_artifact_id: Optional[str] = None,
|
|
68
|
+
metadata: Optional[dict] = None,
|
|
69
|
+
) -> RunHandle:
|
|
70
|
+
run_id = f"local-{uuid.uuid4().hex}"
|
|
71
|
+
instance_id = uuid.uuid4().hex # minted here so callers can link immediately
|
|
72
|
+
record = RunRecord(
|
|
73
|
+
run_id=run_id, runner=self.type, task_id=task_id, task_version=task_version,
|
|
74
|
+
instance_id=instance_id, status=RunStatus.QUEUED,
|
|
75
|
+
overrides={
|
|
76
|
+
"agent_model": agent_model,
|
|
77
|
+
"agent_artifact_id": agent_artifact_id,
|
|
78
|
+
"metadata": metadata or {},
|
|
79
|
+
},
|
|
80
|
+
)
|
|
81
|
+
run_store.insert_run(record)
|
|
82
|
+
self._inflight[run_id] = asyncio.create_task(self._run(record), name=f"agent-env-run-{run_id}")
|
|
83
|
+
logger.info("Submitted run %s for task %s v%s", run_id, task_id, task_version)
|
|
84
|
+
return RunHandle(run_id=run_id, instance_id=instance_id)
|
|
85
|
+
|
|
86
|
+
async def status(self, run_id: str) -> Optional[RunRecord]:
|
|
87
|
+
record = run_store.get_run(run_id)
|
|
88
|
+
if record is not None and record.runner != self.type:
|
|
89
|
+
# Submitted under a different [runner]; report rather than pretend to own it.
|
|
90
|
+
logger.warning("Run %s was submitted by runner %r, not %r", run_id, record.runner, self.type)
|
|
91
|
+
return record
|
|
92
|
+
|
|
93
|
+
async def cancel(self, run_id: str) -> bool:
|
|
94
|
+
record = run_store.get_run(run_id)
|
|
95
|
+
if record is None or record.status.is_terminal:
|
|
96
|
+
return False
|
|
97
|
+
task = self._inflight.get(run_id)
|
|
98
|
+
if task is not None:
|
|
99
|
+
# Signal only. `_run` is the sole writer of the terminal state, so a cancel that
|
|
100
|
+
# races the run's completion can't mislabel a finished run (see `_run`). This
|
|
101
|
+
# preempts Task.run() at its current await; it unwinds its in-flight step cleanly.
|
|
102
|
+
task.cancel()
|
|
103
|
+
else:
|
|
104
|
+
run_store.mark_terminal(run_id, RunStatus.CANCELED) # no live task here (defensive)
|
|
105
|
+
return True
|
|
106
|
+
|
|
107
|
+
# --- execution ---------------------------------------------------------
|
|
108
|
+
|
|
109
|
+
async def _run(self, record: RunRecord) -> None:
|
|
110
|
+
from agent_env.task import Task
|
|
111
|
+
|
|
112
|
+
run_task: Optional[asyncio.Task] = None
|
|
113
|
+
try:
|
|
114
|
+
async with self._sem: # bounded concurrency; the wait here IS the queue
|
|
115
|
+
if run_store.get_run(record.run_id).status == RunStatus.CANCELED:
|
|
116
|
+
return # canceled while it waited for a slot
|
|
117
|
+
run_store.set_running(record.run_id)
|
|
118
|
+
logger.info("Run %s starting (task=%s v%s)", record.run_id, record.task_id, record.task_version)
|
|
119
|
+
task = Task.get(record.task_id, record.task_version)
|
|
120
|
+
if task is None:
|
|
121
|
+
raise LookupError(f"Task {record.task_id} v{record.task_version} not found")
|
|
122
|
+
|
|
123
|
+
context = self._seed_context(record)
|
|
124
|
+
# start_step rides in metadata but Task.run takes it as a keyword;
|
|
125
|
+
# without lifting it out here every resume re-ran from zero.
|
|
126
|
+
run_task = asyncio.ensure_future(task.run(
|
|
127
|
+
agent_model=record.overrides.get("agent_model"),
|
|
128
|
+
agent_artifact_id=record.overrides.get("agent_artifact_id"),
|
|
129
|
+
context=context,
|
|
130
|
+
instance_id=record.instance_id,
|
|
131
|
+
start_step=int(context.metadata.get("start_step") or 0),
|
|
132
|
+
))
|
|
133
|
+
await run_task
|
|
134
|
+
self._finish(record.run_id, context)
|
|
135
|
+
except asyncio.CancelledError:
|
|
136
|
+
# cancel()/stop() cancelled us. `_run` is the single writer of the terminal state,
|
|
137
|
+
# decided here synchronously so no cancel can interpose: if Task.run() finished
|
|
138
|
+
# before the cancel landed, honor its outcome (its side effects are done, so
|
|
139
|
+
# CANCELED would misreport it); otherwise preempt the in-flight run and record
|
|
140
|
+
# CANCELED — or, on shutdown, leave it non-terminal for start() to reconcile.
|
|
141
|
+
if run_task is not None and run_task.done() and not run_task.cancelled():
|
|
142
|
+
self._finish(record.run_id, context, exc=run_task.exception())
|
|
143
|
+
else:
|
|
144
|
+
if run_task is not None:
|
|
145
|
+
run_task.cancel()
|
|
146
|
+
with contextlib.suppress(asyncio.CancelledError, Exception):
|
|
147
|
+
await run_task
|
|
148
|
+
if self._stopping:
|
|
149
|
+
logger.info("Run %s interrupted by shutdown", record.run_id)
|
|
150
|
+
else:
|
|
151
|
+
run_store.mark_terminal(record.run_id, RunStatus.CANCELED)
|
|
152
|
+
logger.info("Run %s canceled", record.run_id)
|
|
153
|
+
except Exception as e:
|
|
154
|
+
logger.exception("Run %s failed", record.run_id)
|
|
155
|
+
run_store.mark_terminal(record.run_id, RunStatus.FAILED, error=f"{type(e).__name__}: {e}")
|
|
156
|
+
finally:
|
|
157
|
+
self._inflight.pop(record.run_id, None)
|
|
158
|
+
|
|
159
|
+
def _finish(self, run_id: str, context, *, exc: Optional[BaseException] = None) -> None:
|
|
160
|
+
"""Persist the terminal state of a finished Task.run(): a raised step is FAILED,
|
|
161
|
+
otherwise an un-retried failed step on the context is authoritative (a step can fail
|
|
162
|
+
without raising when fail_task_on_error is False). Failures the scheduler recovered via
|
|
163
|
+
retry carry ``retried`` and don't count, so a run that recovers is COMPLETED."""
|
|
164
|
+
if exc is not None:
|
|
165
|
+
run_store.mark_terminal(run_id, RunStatus.FAILED, error=f"{type(exc).__name__}: {exc}")
|
|
166
|
+
logger.warning("Run %s failed: %s", run_id, exc)
|
|
167
|
+
elif failed := [
|
|
168
|
+
f for f in (context.metadata or {}).get("failed_steps") or []
|
|
169
|
+
if not (isinstance(f, dict) and f.get("retried")) # a malformed entry counts as a failure
|
|
170
|
+
]:
|
|
171
|
+
run_store.mark_terminal(run_id, RunStatus.FAILED, error=str(failed))
|
|
172
|
+
logger.warning("Run %s finished with failed steps: %s", run_id, failed)
|
|
173
|
+
else:
|
|
174
|
+
run_store.mark_terminal(run_id, RunStatus.COMPLETED)
|
|
175
|
+
logger.info("Run %s completed", run_id)
|
|
176
|
+
|
|
177
|
+
def _seed_context(self, record: RunRecord):
|
|
178
|
+
"""Build the context ``Task.run()`` mutates, carrying the run identity in metadata.
|
|
179
|
+
|
|
180
|
+
``workflow_id`` holds the run id — the hub, instance documents and UI all key off it —
|
|
181
|
+
so nothing downstream needs a special case for the local runner.
|
|
182
|
+
"""
|
|
183
|
+
from agent_env.task_step import TaskStepContext
|
|
184
|
+
|
|
185
|
+
context = TaskStepContext()
|
|
186
|
+
context.metadata.update(record.overrides.get("metadata") or {})
|
|
187
|
+
context.metadata["workflow_id"] = record.run_id
|
|
188
|
+
context.metadata["instance_id"] = record.instance_id
|
|
189
|
+
context.metadata["runner"] = self.type
|
|
190
|
+
return context
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""The ``Runner`` seam: dispatch a task run and return a handle to poll. Selected via
|
|
2
|
+
``[runner]`` in ``.agentenv/config.toml`` like a store.
|
|
3
|
+
|
|
4
|
+
``RunStatus`` and ``submit``'s ``(run_id, instance_id)`` return are the contract every
|
|
5
|
+
backend maps onto, so callers work against any configured runner unchanged.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from abc import ABC, abstractmethod
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from enum import StrEnum
|
|
13
|
+
from typing import Any, Optional
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class RunStatus(StrEnum):
|
|
17
|
+
"""Run lifecycle, engine-neutral: a backend maps its own states onto these. ``QUEUED``
|
|
18
|
+
is the pre-start state and is non-terminal."""
|
|
19
|
+
|
|
20
|
+
QUEUED = "QUEUED"
|
|
21
|
+
RUNNING = "RUNNING"
|
|
22
|
+
COMPLETED = "COMPLETED"
|
|
23
|
+
FAILED = "FAILED"
|
|
24
|
+
CANCELED = "CANCELED"
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def is_terminal(self) -> bool:
|
|
28
|
+
return self in (RunStatus.COMPLETED, RunStatus.FAILED, RunStatus.CANCELED)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class RunHandle:
|
|
33
|
+
"""Returned by ``submit`` before the run starts. ``run_id`` is the hub's
|
|
34
|
+
``workflow_id`` (a minted id under the local runner); ``instance_id`` is minted at
|
|
35
|
+
submit time so a caller can link to the run immediately."""
|
|
36
|
+
|
|
37
|
+
run_id: str
|
|
38
|
+
instance_id: str
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class RunRecord:
|
|
43
|
+
"""The persisted state of one run."""
|
|
44
|
+
|
|
45
|
+
run_id: str
|
|
46
|
+
runner: str
|
|
47
|
+
task_id: str
|
|
48
|
+
task_version: Optional[int]
|
|
49
|
+
instance_id: str
|
|
50
|
+
status: RunStatus = RunStatus.QUEUED
|
|
51
|
+
created_at_utc: Optional[str] = None
|
|
52
|
+
started_at_utc: Optional[str] = None
|
|
53
|
+
finished_at_utc: Optional[str] = None
|
|
54
|
+
error: Optional[str] = None
|
|
55
|
+
overrides: dict[str, Any] = field(default_factory=dict)
|
|
56
|
+
|
|
57
|
+
def to_dict(self) -> dict:
|
|
58
|
+
return {
|
|
59
|
+
"run_id": self.run_id,
|
|
60
|
+
"runner": self.runner,
|
|
61
|
+
"task_id": self.task_id,
|
|
62
|
+
"task_version": self.task_version,
|
|
63
|
+
"instance_id": self.instance_id,
|
|
64
|
+
"status": str(self.status),
|
|
65
|
+
"created_at_utc": self.created_at_utc,
|
|
66
|
+
"started_at_utc": self.started_at_utc,
|
|
67
|
+
"finished_at_utc": self.finished_at_utc,
|
|
68
|
+
"error": self.error,
|
|
69
|
+
"overrides": self.overrides,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def from_dict(cls, data: dict) -> "RunRecord":
|
|
74
|
+
return cls(
|
|
75
|
+
run_id=data["run_id"],
|
|
76
|
+
runner=data.get("runner", ""),
|
|
77
|
+
task_id=data["task_id"],
|
|
78
|
+
task_version=data.get("task_version"),
|
|
79
|
+
instance_id=data["instance_id"],
|
|
80
|
+
status=RunStatus(data.get("status", RunStatus.QUEUED)),
|
|
81
|
+
created_at_utc=data.get("created_at_utc"),
|
|
82
|
+
started_at_utc=data.get("started_at_utc"),
|
|
83
|
+
finished_at_utc=data.get("finished_at_utc"),
|
|
84
|
+
error=data.get("error"),
|
|
85
|
+
overrides=data.get("overrides") or {},
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class Runner(ABC):
|
|
90
|
+
"""Dispatches task runs and reports on them.
|
|
91
|
+
|
|
92
|
+
``type`` is persisted on every run record: a hub restarted under a different
|
|
93
|
+
``[runner]`` reports on a run it does not own rather than misreporting it.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
type: str = "runner"
|
|
97
|
+
|
|
98
|
+
@classmethod
|
|
99
|
+
def from_config(cls, **config) -> Runner:
|
|
100
|
+
"""Construct from the resolved ``[runner.config]`` table."""
|
|
101
|
+
return cls(**config)
|
|
102
|
+
|
|
103
|
+
@abstractmethod
|
|
104
|
+
async def submit(
|
|
105
|
+
self,
|
|
106
|
+
task_id: str,
|
|
107
|
+
task_version: Optional[int] = None,
|
|
108
|
+
*,
|
|
109
|
+
agent_model: Optional[str] = None,
|
|
110
|
+
agent_artifact_id: Optional[str] = None,
|
|
111
|
+
metadata: Optional[dict] = None,
|
|
112
|
+
) -> RunHandle:
|
|
113
|
+
"""Enqueue a run and return its handle immediately, without waiting for it."""
|
|
114
|
+
|
|
115
|
+
@abstractmethod
|
|
116
|
+
async def status(self, run_id: str) -> Optional[RunRecord]:
|
|
117
|
+
"""The current record for ``run_id``, or None if this runner has no such run."""
|
|
118
|
+
|
|
119
|
+
@abstractmethod
|
|
120
|
+
async def cancel(self, run_id: str) -> bool:
|
|
121
|
+
"""Request cancellation; True if the run was queued/running and is now canceled."""
|
|
122
|
+
|
|
123
|
+
async def start(self) -> None:
|
|
124
|
+
"""Begin processing work. No-op for runners backed by an external service."""
|
|
125
|
+
|
|
126
|
+
async def stop(self) -> None:
|
|
127
|
+
"""Stop processing and release resources."""
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Run-record persistence for the in-process runner.
|
|
2
|
+
|
|
3
|
+
Stored as a collection so the hub can list runs and report their status — not a durable
|
|
4
|
+
work queue: the runner executes ``Task.run()`` in-process and holds the run's asyncio task
|
|
5
|
+
in memory, so there is no lease or claim here. A run left non-terminal by a dead process
|
|
6
|
+
is failed at ``start()``, not resumed. A durable, lease-based runner is a follow-up.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from datetime import datetime, timezone
|
|
12
|
+
from typing import Optional
|
|
13
|
+
|
|
14
|
+
from agent_env.config import get_config
|
|
15
|
+
from agent_env.runner.runner import RunRecord, RunStatus
|
|
16
|
+
from agent_env.store import Eq, Filter, In, Sort, UpdateSpec
|
|
17
|
+
|
|
18
|
+
RUNS_COLLECTION = "runs"
|
|
19
|
+
|
|
20
|
+
# Terminal states are never overwritten: mark_terminal/set_running only act on a run that
|
|
21
|
+
# is still QUEUED or RUNNING, so a completion racing a cancel can't resurrect a CANCELED run.
|
|
22
|
+
_NON_TERMINAL = [In([str(RunStatus.QUEUED), str(RunStatus.RUNNING)])]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _now() -> str:
|
|
26
|
+
return datetime.now(timezone.utc).isoformat()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _docs():
|
|
30
|
+
return get_config().get_document_store()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def ensure_indexes() -> None:
|
|
34
|
+
"""Indexes backing the two reads: status-by-run-id and the per-task listing."""
|
|
35
|
+
docs = _docs()
|
|
36
|
+
docs.ensure_index(RUNS_COLLECTION, ["run_id"], unique=True)
|
|
37
|
+
docs.ensure_index(RUNS_COLLECTION, ["task_id", "created_at_utc"])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def insert_run(record: RunRecord) -> RunRecord:
|
|
41
|
+
record.created_at_utc = record.created_at_utc or _now()
|
|
42
|
+
_docs().insert(RUNS_COLLECTION, record.to_dict())
|
|
43
|
+
return record
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_run(run_id: str) -> Optional[RunRecord]:
|
|
47
|
+
doc = _docs().find_one(RUNS_COLLECTION, Filter.of(run_id=run_id))
|
|
48
|
+
return RunRecord.from_dict(doc) if doc else None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def list_runs(
|
|
52
|
+
task_id: Optional[str] = None, limit: int = 50, offset: int = 0
|
|
53
|
+
) -> list[RunRecord]:
|
|
54
|
+
filt = Filter.of(task_id=task_id) if task_id else Filter()
|
|
55
|
+
docs = _docs().query(
|
|
56
|
+
RUNS_COLLECTION, filt, sort=Sort.by("created_at_utc", descending=True),
|
|
57
|
+
limit=limit, offset=offset,
|
|
58
|
+
)
|
|
59
|
+
return [RunRecord.from_dict(d) for d in docs]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def active_runs(runner: str, limit: int = 100_000) -> list[RunRecord]:
|
|
63
|
+
"""Every non-terminal (QUEUED/RUNNING) run for ``runner`` — used at startup to reconcile
|
|
64
|
+
orphans a dead process left behind, no matter how many terminal runs precede them."""
|
|
65
|
+
docs = _docs().query(
|
|
66
|
+
RUNS_COLLECTION,
|
|
67
|
+
Filter(conditions={"runner": [Eq(runner)], "status": _NON_TERMINAL}),
|
|
68
|
+
limit=limit,
|
|
69
|
+
)
|
|
70
|
+
return [RunRecord.from_dict(d) for d in docs]
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def set_running(run_id: str) -> None:
|
|
74
|
+
"""QUEUED -> RUNNING once a worker slot frees. No-op if the run is no longer QUEUED
|
|
75
|
+
(e.g. it was canceled while waiting for the slot)."""
|
|
76
|
+
_docs().update(
|
|
77
|
+
RUNS_COLLECTION,
|
|
78
|
+
Filter(conditions={"run_id": [Eq(run_id)], "status": [Eq(str(RunStatus.QUEUED))]}),
|
|
79
|
+
UpdateSpec(set={"status": str(RunStatus.RUNNING), "started_at_utc": _now()}),
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def mark_terminal(run_id: str, status: RunStatus, error: Optional[str] = None) -> None:
|
|
84
|
+
"""Move a run to a terminal status, but only from QUEUED/RUNNING — so a run that a
|
|
85
|
+
concurrent cancel already terminated is left as-is (cancel wins, and it's idempotent)."""
|
|
86
|
+
_docs().update(
|
|
87
|
+
RUNS_COLLECTION,
|
|
88
|
+
Filter(conditions={"run_id": [Eq(run_id)], "status": _NON_TERMINAL}),
|
|
89
|
+
UpdateSpec(set={
|
|
90
|
+
"status": str(status),
|
|
91
|
+
"finished_at_utc": _now(),
|
|
92
|
+
"error": error,
|
|
93
|
+
}),
|
|
94
|
+
)
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""Store infrastructure for AgentEnv persistence layer"""
|
|
2
|
+
|
|
3
|
+
from agent_env.store.base import (
|
|
4
|
+
ConcurrentModificationError,
|
|
5
|
+
ConfigError,
|
|
6
|
+
GrantUnavailableError,
|
|
7
|
+
NotFoundError,
|
|
8
|
+
ObjectAlreadyExistsError,
|
|
9
|
+
ObjectNotFoundError,
|
|
10
|
+
)
|
|
11
|
+
from agent_env.config import (
|
|
12
|
+
Config,
|
|
13
|
+
configure,
|
|
14
|
+
get_config,
|
|
15
|
+
reset_config,
|
|
16
|
+
set_document_store,
|
|
17
|
+
set_image_store,
|
|
18
|
+
set_object_store,
|
|
19
|
+
set_secret_store,
|
|
20
|
+
)
|
|
21
|
+
from agent_env.store.document_store import (
|
|
22
|
+
AbsentOrNull,
|
|
23
|
+
DocumentStore,
|
|
24
|
+
DuplicateKeyError,
|
|
25
|
+
DynamoDbDocumentStore,
|
|
26
|
+
Eq,
|
|
27
|
+
Exists,
|
|
28
|
+
Filter,
|
|
29
|
+
Gte,
|
|
30
|
+
In,
|
|
31
|
+
LocalSqliteDocumentStore,
|
|
32
|
+
Lte,
|
|
33
|
+
LteOrAbsent,
|
|
34
|
+
MongoDocumentStore,
|
|
35
|
+
Ne,
|
|
36
|
+
Predicate,
|
|
37
|
+
Sort,
|
|
38
|
+
SortKey,
|
|
39
|
+
UpdateSpec,
|
|
40
|
+
VersionedEntityStore,
|
|
41
|
+
compare_and_swap,
|
|
42
|
+
rev_precondition,
|
|
43
|
+
)
|
|
44
|
+
from agent_env.store.image_store import (
|
|
45
|
+
EcrCredentials,
|
|
46
|
+
EcrImageStore,
|
|
47
|
+
ImageStore,
|
|
48
|
+
LocalRegistryImageStore,
|
|
49
|
+
OciRegistryCredentials,
|
|
50
|
+
OciRegistryImageStore,
|
|
51
|
+
RegistryAuth,
|
|
52
|
+
SecretStoreCredentials,
|
|
53
|
+
)
|
|
54
|
+
from agent_env.store.object_store import (
|
|
55
|
+
LocalFilesystemObjectStore,
|
|
56
|
+
ObjectMetadata,
|
|
57
|
+
ObjectStore,
|
|
58
|
+
S3ObjectStore,
|
|
59
|
+
)
|
|
60
|
+
from agent_env.store.query import QueryBuilder
|
|
61
|
+
from agent_env.store.secret_store import (
|
|
62
|
+
AwsSecretsManagerSecretStore,
|
|
63
|
+
LocalSecretStore,
|
|
64
|
+
SecretStore,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
__all__ = [
|
|
68
|
+
# Configuration
|
|
69
|
+
"Config",
|
|
70
|
+
"configure",
|
|
71
|
+
"get_config",
|
|
72
|
+
"reset_config",
|
|
73
|
+
"set_document_store",
|
|
74
|
+
"set_image_store",
|
|
75
|
+
"set_object_store",
|
|
76
|
+
"set_secret_store",
|
|
77
|
+
# Base classes
|
|
78
|
+
"QueryBuilder",
|
|
79
|
+
# Exceptions
|
|
80
|
+
"NotFoundError",
|
|
81
|
+
"ObjectAlreadyExistsError",
|
|
82
|
+
"ObjectNotFoundError",
|
|
83
|
+
"GrantUnavailableError",
|
|
84
|
+
"ConcurrentModificationError",
|
|
85
|
+
"ConfigError",
|
|
86
|
+
"DuplicateKeyError",
|
|
87
|
+
# Document store abstraction
|
|
88
|
+
"DocumentStore",
|
|
89
|
+
"MongoDocumentStore",
|
|
90
|
+
"LocalSqliteDocumentStore",
|
|
91
|
+
"DynamoDbDocumentStore",
|
|
92
|
+
"VersionedEntityStore",
|
|
93
|
+
"compare_and_swap",
|
|
94
|
+
"rev_precondition",
|
|
95
|
+
"Filter",
|
|
96
|
+
"Predicate",
|
|
97
|
+
"Eq",
|
|
98
|
+
"Ne",
|
|
99
|
+
"Gte",
|
|
100
|
+
"Lte",
|
|
101
|
+
"In",
|
|
102
|
+
"Exists",
|
|
103
|
+
"LteOrAbsent",
|
|
104
|
+
"AbsentOrNull",
|
|
105
|
+
"Sort",
|
|
106
|
+
"SortKey",
|
|
107
|
+
"UpdateSpec",
|
|
108
|
+
# Image store abstraction
|
|
109
|
+
"ImageStore",
|
|
110
|
+
"OciRegistryImageStore",
|
|
111
|
+
"RegistryAuth",
|
|
112
|
+
"OciRegistryCredentials",
|
|
113
|
+
"EcrCredentials",
|
|
114
|
+
"SecretStoreCredentials",
|
|
115
|
+
"EcrImageStore",
|
|
116
|
+
"LocalRegistryImageStore",
|
|
117
|
+
# Object store abstraction
|
|
118
|
+
"ObjectStore",
|
|
119
|
+
"ObjectMetadata",
|
|
120
|
+
"S3ObjectStore",
|
|
121
|
+
"LocalFilesystemObjectStore",
|
|
122
|
+
# Secret store abstraction
|
|
123
|
+
"SecretStore",
|
|
124
|
+
"AwsSecretsManagerSecretStore",
|
|
125
|
+
"LocalSecretStore",
|
|
126
|
+
]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""What the Google Cloud store backends share (the ``gcp`` extra): the scope they request, and how
|
|
2
|
+
they call Google's IAM API."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import ssl
|
|
7
|
+
from collections.abc import Iterator
|
|
8
|
+
|
|
9
|
+
import requests
|
|
10
|
+
from google.api_core.retry import Retry
|
|
11
|
+
from google.auth.credentials import AnonymousCredentials
|
|
12
|
+
from google.auth.exceptions import MutualTLSChannelError, TransportError
|
|
13
|
+
from google.auth.transport.requests import AuthorizedSession, Request
|
|
14
|
+
|
|
15
|
+
from agent_env.config.errors import ConfigError
|
|
16
|
+
|
|
17
|
+
SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
|
|
18
|
+
_TIMEOUT_SECONDS = 10
|
|
19
|
+
_LOST_IN_TRANSIT = (requests.ConnectionError, requests.Timeout, requests.exceptions.ChunkedEncodingError)
|
|
20
|
+
# The TLS failures that mean the connection was cut, not that the other side refused it.
|
|
21
|
+
_CUT_TLS = (ssl.SSLEOFError, ssl.SSLZeroReturnError)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def dropped_connection(error: Exception) -> bool:
|
|
25
|
+
"""A google-auth request lost in transit, before or during the response. A TLS failure counts
|
|
26
|
+
only when the handshake was cut short: a refused certificate would repeat."""
|
|
27
|
+
cause = error.__cause__
|
|
28
|
+
if not isinstance(error, TransportError) or not isinstance(cause, _LOST_IN_TRANSIT):
|
|
29
|
+
return False
|
|
30
|
+
tls = [e for e in _chain(cause) if isinstance(e, ssl.SSLError)]
|
|
31
|
+
return not tls or any(isinstance(e, _CUT_TLS) for e in tls)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _chain(error: BaseException) -> Iterator[BaseException]:
|
|
35
|
+
"""Every exception in the chain requests and urllib3 wrap a transport failure in."""
|
|
36
|
+
pending, seen = [error], set()
|
|
37
|
+
while pending:
|
|
38
|
+
current = pending.pop()
|
|
39
|
+
if id(current) in seen:
|
|
40
|
+
continue
|
|
41
|
+
seen.add(id(current))
|
|
42
|
+
yield current
|
|
43
|
+
links = [getattr(current, "reason", None), *current.args, current.__cause__, current.__context__]
|
|
44
|
+
pending += [link for link in links if isinstance(link, BaseException)]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
RETRY = Retry(predicate=dropped_connection, initial=0.5, maximum=4.0, timeout=30.0)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class _Request(Request):
|
|
51
|
+
"""google-auth's request with a short default timeout. It stays a Request, with its session in
|
|
52
|
+
view, so google-auth can mount its metadata-server mTLS adapter on it."""
|
|
53
|
+
|
|
54
|
+
def __call__(self, url, method="GET", body=None, headers=None, timeout=_TIMEOUT_SECONDS, **kwargs):
|
|
55
|
+
return super().__call__(url, method=method, body=body, headers=headers, timeout=timeout, **kwargs)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def pooled_request() -> Request:
|
|
59
|
+
"""A google-auth request over one pooled session. It adds no credentials of its own: google-auth
|
|
60
|
+
refreshes credentials over it, and those requests may go to non-Google token endpoints.
|
|
61
|
+
google.auth.iam calls the mTLS endpoint where client certificates are configured, so the session
|
|
62
|
+
presents them, as google-auth's own IAM calls do."""
|
|
63
|
+
session = AuthorizedSession(AnonymousCredentials(), refresh_status_codes=())
|
|
64
|
+
try:
|
|
65
|
+
session.configure_mtls_channel()
|
|
66
|
+
except MutualTLSChannelError as e:
|
|
67
|
+
raise ConfigError(f"Cannot set up mutual TLS for Google's IAM API: {e}") from e
|
|
68
|
+
return _Request(session)
|
agent_env/store/base.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Store error types shared across the persistence layer."""
|
|
2
|
+
|
|
3
|
+
# Re-exported for back-compat; ConfigError now lives with the config surface.
|
|
4
|
+
from agent_env.config.errors import ConfigError # noqa: F401
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class NotFoundError(Exception):
|
|
8
|
+
"""Raised when an entity is not found in the store."""
|
|
9
|
+
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ObjectAlreadyExistsError(Exception):
|
|
14
|
+
"""Raised when attempting to create an object that already exists."""
|
|
15
|
+
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ObjectNotFoundError(NotFoundError, FileNotFoundError):
|
|
20
|
+
"""Raised when an object store read addresses no object, on every backend. It is a
|
|
21
|
+
FileNotFoundError too, so filesystem-style handlers keep catching it."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class GrantUnavailableError(RuntimeError):
|
|
25
|
+
"""Raised when an object store cannot issue a transfer grant for the requested lifetime."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ConcurrentModificationError(Exception):
|
|
29
|
+
"""Raised when an update fails due to a concurrent modification."""
|
|
30
|
+
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|