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/bundle/plan.py
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
"""Plan what a run of a resolved bundle writes, in order, and check it before anything is written.
|
|
2
|
+
|
|
3
|
+
Planning selects the tasks and evals to run, collects the entities and built images they reach, and
|
|
4
|
+
orders them so each is written after what it references. Everything that can be known to fail
|
|
5
|
+
before a write is reported together: an id two writers claim, a reference loop, a store id that
|
|
6
|
+
doesn't exist or has the wrong type, and a task whose steps don't build. It reads stores but never
|
|
7
|
+
writes to one.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import heapq
|
|
13
|
+
import unicodedata
|
|
14
|
+
from collections.abc import Sequence
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from agent_env.a2a_agent import A2AAgent
|
|
19
|
+
from agent_env.artifact import Artifact, FileArtifact, FileArtifactUniverse
|
|
20
|
+
from agent_env.artifact.registry import canonical_type, get_artifact_registry
|
|
21
|
+
from agent_env.entity_refs import EntityKind
|
|
22
|
+
from agent_env.env.env import Env
|
|
23
|
+
from agent_env.store.base import NotFoundError
|
|
24
|
+
from agent_env.store.ids import LOCAL_PREFIX
|
|
25
|
+
from agent_env.task import Task
|
|
26
|
+
from agent_env.task_step.registry import get_task_step_registry
|
|
27
|
+
|
|
28
|
+
from ._fs import fold, relative, with_article
|
|
29
|
+
from .authoring import AuthoringContext, entry_file, entry_files
|
|
30
|
+
from .parse import BundleEntry, BundleError, BundleKind
|
|
31
|
+
from .resolve import BuiltImage, Reference, ResolvedBundle, ResolvedEntry, build_step
|
|
32
|
+
|
|
33
|
+
_Key = tuple[str, str] # (store, id)
|
|
34
|
+
|
|
35
|
+
_GETTERS = {EntityKind.ENV: Env.get, EntityKind.AGENT: A2AAgent.get, EntityKind.ARTIFACT: Artifact.get,
|
|
36
|
+
EntityKind.TASK: Task.get}
|
|
37
|
+
|
|
38
|
+
# The artifact types written from their folder's files, and how each lists them.
|
|
39
|
+
_FOLDER_WALKS = {FileArtifact: entry_file, FileArtifactUniverse: entry_files}
|
|
40
|
+
|
|
41
|
+
_NOTHING_TO_RUN = "this bundle has no tasks or evals to run"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class Write:
|
|
46
|
+
"""An entity or built image a run writes."""
|
|
47
|
+
|
|
48
|
+
kind: BundleKind # a built image is an ARTIFACT; kind.store keys uniqueness
|
|
49
|
+
id: str
|
|
50
|
+
source: ResolvedEntry | BuiltImage
|
|
51
|
+
needs: tuple[_Key, ...] # (kind.store, id) of the earlier writes it references
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True)
|
|
55
|
+
class Plan:
|
|
56
|
+
bundle: ResolvedBundle
|
|
57
|
+
writes: tuple[Write, ...] # everything the selection reaches, each after what it references
|
|
58
|
+
tasks: tuple[ResolvedEntry, ...] # the tasks selected to run on their own
|
|
59
|
+
evals: tuple[ResolvedEntry, ...]
|
|
60
|
+
store_refs: tuple[Reference, ...] # the store ids the writes reference, checked to exist
|
|
61
|
+
store_latest: dict[tuple[EntityKind, str], int] # the version each unpinned store ref named when planned
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def plan_bundle(resolved: ResolvedBundle, *, tasks: Sequence[str] = (), evals: Sequence[str] = ()) -> Plan:
|
|
65
|
+
"""Plan a run of ``tasks`` and ``evals``, each a name or id in this bundle. With neither, the
|
|
66
|
+
run is every eval or, in a bundle without evals, every task. Raises BundleError listing every
|
|
67
|
+
problem found."""
|
|
68
|
+
return _Planner(resolved).plan(tasks, evals)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def check_bundle(resolved: ResolvedBundle) -> None:
|
|
72
|
+
"""The checks ``plan_bundle`` makes without reading a store, over every task and eval in the bundle. Raises
|
|
73
|
+
BundleError listing every problem found."""
|
|
74
|
+
_Planner(resolved).check()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class _Planner:
|
|
78
|
+
def __init__(self, resolved: ResolvedBundle):
|
|
79
|
+
self.resolved = resolved
|
|
80
|
+
self.problems: list[str] = []
|
|
81
|
+
self.nodes: dict[_Key, ResolvedEntry | BuiltImage] = {}
|
|
82
|
+
self.rank: dict[_Key, int] = {}
|
|
83
|
+
self.edges: dict[_Key, list[_Key]] = {}
|
|
84
|
+
images: dict[BundleEntry, list[BuiltImage]] = {}
|
|
85
|
+
for image in resolved.built_images:
|
|
86
|
+
images.setdefault(image.entry, []).append(image)
|
|
87
|
+
for entry in resolved.entries:
|
|
88
|
+
for image in images.get(entry.entry, ()):
|
|
89
|
+
self._add(_key(image), image, [])
|
|
90
|
+
needs = dict.fromkeys(_key(ref.local) for ref in entry.references if ref.local is not None)
|
|
91
|
+
self._add(_key(entry.entry), entry, list(needs))
|
|
92
|
+
self.named = {fold(entry.entry.name): entry.entry for entry in resolved.entries}
|
|
93
|
+
self.identified = {entry.entry.id: entry.entry for entry in resolved.entries}
|
|
94
|
+
self.outputs = {(output.kind.value, output.id): (entry.entry, output)
|
|
95
|
+
for entry in resolved.entries for output in entry.outputs}
|
|
96
|
+
|
|
97
|
+
def _add(self, key: _Key, source: ResolvedEntry | BuiltImage, needs: list[_Key]) -> None:
|
|
98
|
+
"""Record a write in bundle order, a built image just before the entry that builds it."""
|
|
99
|
+
self.nodes[key], self.rank[key], self.edges[key] = source, len(self.rank), needs
|
|
100
|
+
|
|
101
|
+
def plan(self, tasks: Sequence[str], evals: Sequence[str]) -> Plan:
|
|
102
|
+
self._check_writers()
|
|
103
|
+
self._check_loops()
|
|
104
|
+
selected_tasks, selected_evals = self._selection(tasks, evals)
|
|
105
|
+
closure = self._closure(selected_tasks + selected_evals)
|
|
106
|
+
store_refs, store_latest = self._check_store_refs(closure)
|
|
107
|
+
self._build_tasks(closure)
|
|
108
|
+
self._check_files(closure)
|
|
109
|
+
if self.problems:
|
|
110
|
+
raise BundleError(self.problems)
|
|
111
|
+
return Plan(self.resolved, self._ordered(closure), tuple(selected_tasks), tuple(selected_evals), store_refs,
|
|
112
|
+
store_latest)
|
|
113
|
+
|
|
114
|
+
def check(self) -> None:
|
|
115
|
+
self._check_writers()
|
|
116
|
+
self._check_loops()
|
|
117
|
+
if not any(entry.entry.kind in (BundleKind.TASK, BundleKind.EVAL) for entry in self.resolved.entries):
|
|
118
|
+
self.problems.append(_NOTHING_TO_RUN)
|
|
119
|
+
everything = set(self.nodes)
|
|
120
|
+
self._check_store_refs(everything, stores=False)
|
|
121
|
+
self._build_tasks(everything)
|
|
122
|
+
self._check_files(everything)
|
|
123
|
+
if self.problems:
|
|
124
|
+
raise BundleError(self.problems)
|
|
125
|
+
|
|
126
|
+
# Checks over the whole bundle
|
|
127
|
+
|
|
128
|
+
def _check_writers(self) -> None:
|
|
129
|
+
"""Every id is written by one entity, built image or task output, per store."""
|
|
130
|
+
writers: dict[_Key, str] = {}
|
|
131
|
+
claims: list[tuple[_Key, str, str]] = []
|
|
132
|
+
for key, source in self.nodes.items():
|
|
133
|
+
if isinstance(source, BuiltImage):
|
|
134
|
+
claims.append((key, self._path(source.entry), f"the image built for {self._path(source.entry)}"))
|
|
135
|
+
else:
|
|
136
|
+
claims.append((key, self._path(source.entry), self._path(source.entry)))
|
|
137
|
+
for entry in self.resolved.entries:
|
|
138
|
+
for output in entry.outputs:
|
|
139
|
+
claims.append(((output.kind.value, output.id), f"{self._path(entry.entry)}: step {output.step_id!r}",
|
|
140
|
+
f"step {output.step_id!r} of {self._path(entry.entry)}"))
|
|
141
|
+
for key, where, writer in claims:
|
|
142
|
+
if key in writers:
|
|
143
|
+
self.problems.append(f"{where}: {key[1]!r} is also written by {writers[key]}; give each its own id")
|
|
144
|
+
else:
|
|
145
|
+
writers[key] = writer
|
|
146
|
+
|
|
147
|
+
def _check_loops(self) -> None:
|
|
148
|
+
for component in _components(self.nodes, self.edges, self.rank):
|
|
149
|
+
start = min(component, key=self.rank.__getitem__)
|
|
150
|
+
loop = " -> ".join(self._path(self.nodes[key].entry) for key in _loop(start, set(component), self.edges))
|
|
151
|
+
self.problems.append(f"{self._path(self.nodes[start].entry)}: references form a loop: {loop}")
|
|
152
|
+
|
|
153
|
+
# Selection
|
|
154
|
+
|
|
155
|
+
def _selection(self, tasks: Sequence[str], evals: Sequence[str]) -> tuple[list[ResolvedEntry], list[ResolvedEntry]]:
|
|
156
|
+
if tasks or evals:
|
|
157
|
+
return self._select(BundleKind.TASK, tasks), self._select(BundleKind.EVAL, evals)
|
|
158
|
+
every_task = [entry for entry in self.resolved.entries if entry.entry.kind is BundleKind.TASK]
|
|
159
|
+
every_eval = [entry for entry in self.resolved.entries if entry.entry.kind is BundleKind.EVAL]
|
|
160
|
+
if every_eval:
|
|
161
|
+
return [], every_eval
|
|
162
|
+
if not every_task:
|
|
163
|
+
self.problems.append(_NOTHING_TO_RUN)
|
|
164
|
+
return every_task, []
|
|
165
|
+
|
|
166
|
+
def _select(self, kind: BundleKind, wanted: Sequence[str]) -> list[ResolvedEntry]:
|
|
167
|
+
candidates = [entry for entry in self.resolved.entries if entry.entry.kind is kind]
|
|
168
|
+
exact = {_nfc(text): entry for entry in candidates for text in (entry.entry.name, entry.entry.id)}
|
|
169
|
+
near = {fold(text): entry for entry in candidates for text in (entry.entry.name, entry.entry.id)}
|
|
170
|
+
selected: dict[_Key, ResolvedEntry] = {}
|
|
171
|
+
for value in wanted:
|
|
172
|
+
flag = f"--{kind.store} {value!r}"
|
|
173
|
+
entry = exact.get(_nfc(value))
|
|
174
|
+
if entry is not None:
|
|
175
|
+
selected.setdefault(_key(entry.entry), entry)
|
|
176
|
+
elif fold(value) in near:
|
|
177
|
+
self.problems.append(f"{flag}: isn't in this bundle, but {near[fold(value)].entry.name!r} is; names "
|
|
178
|
+
"are case-sensitive")
|
|
179
|
+
else:
|
|
180
|
+
names = ", ".join(repr(entry.entry.name) for entry in candidates)
|
|
181
|
+
listed = f"; its {kind.value} are {names}" if names else f"; it has no {kind.value}"
|
|
182
|
+
self.problems.append(f"{flag}: this bundle has no {kind.store} with that name or id{listed}")
|
|
183
|
+
return list(selected.values())
|
|
184
|
+
|
|
185
|
+
def _closure(self, roots: list[ResolvedEntry]) -> set[_Key]:
|
|
186
|
+
closure: set[_Key] = set()
|
|
187
|
+
frontier = [_key(root.entry) for root in roots]
|
|
188
|
+
while frontier:
|
|
189
|
+
key = frontier.pop()
|
|
190
|
+
if key not in closure:
|
|
191
|
+
closure.add(key)
|
|
192
|
+
frontier.extend(self.edges[key])
|
|
193
|
+
return closure
|
|
194
|
+
|
|
195
|
+
# Checks over what the selection reaches
|
|
196
|
+
|
|
197
|
+
def _check_store_refs(
|
|
198
|
+
self, closure: set[_Key], *, stores: bool = True
|
|
199
|
+
) -> tuple[tuple[Reference, ...], dict[tuple[EntityKind, str], int]]:
|
|
200
|
+
"""Check each store id the closure references; without ``stores``, only the ``@local`` ones, which need no
|
|
201
|
+
store read."""
|
|
202
|
+
read: dict[tuple[EntityKind, str, int | None], Any] = {}
|
|
203
|
+
checked: dict[tuple[EntityKind, str, int | None], Reference] = {}
|
|
204
|
+
latest: dict[tuple[EntityKind, str], int] = {}
|
|
205
|
+
for key in sorted(closure, key=self.rank.__getitem__):
|
|
206
|
+
source = self.nodes[key]
|
|
207
|
+
if isinstance(source, BuiltImage):
|
|
208
|
+
continue
|
|
209
|
+
for ref in source.references:
|
|
210
|
+
if ref.local is not None:
|
|
211
|
+
continue
|
|
212
|
+
if not stores and not ref.id.startswith(LOCAL_PREFIX):
|
|
213
|
+
continue
|
|
214
|
+
problem = self._store_ref_problem(ref, read)
|
|
215
|
+
if problem:
|
|
216
|
+
self.problems.append(f"{self._path(source.entry)}: {ref.where}: {problem}")
|
|
217
|
+
else:
|
|
218
|
+
checked.setdefault((ref.kind, ref.id, ref.version), ref)
|
|
219
|
+
if ref.version is None:
|
|
220
|
+
latest[ref.kind, ref.id] = read[ref.kind, ref.id, None].version
|
|
221
|
+
return tuple(checked.values()), latest
|
|
222
|
+
|
|
223
|
+
def _store_ref_problem(self, ref: Reference, read: dict) -> str | None:
|
|
224
|
+
kind, key = ref.kind.value, _nfc(ref.id)
|
|
225
|
+
if ref.id.startswith(LOCAL_PREFIX):
|
|
226
|
+
if (kind, key) in self.outputs:
|
|
227
|
+
entry, output = self.outputs[kind, key]
|
|
228
|
+
return (f"{ref.id!r} is written by step {output.step_id!r} of {self._path(entry)}; refer to an output "
|
|
229
|
+
"by its name, in the task that writes it")
|
|
230
|
+
other = self.identified.get(key)
|
|
231
|
+
if other is not None:
|
|
232
|
+
return (f"{ref.id!r} is this bundle's {other.kind.store} {other.name!r}, but this field takes "
|
|
233
|
+
f"{with_article(kind)}")
|
|
234
|
+
if key.startswith(f"{self.resolved.bundle.id_root}/"):
|
|
235
|
+
return f"{ref.id!r} isn't in this bundle"
|
|
236
|
+
return f"{ref.id!r} isn't in this bundle, and references to another bundle's entities aren't supported yet"
|
|
237
|
+
lookup = (ref.kind, ref.id, ref.version)
|
|
238
|
+
if lookup not in read:
|
|
239
|
+
try:
|
|
240
|
+
read[lookup] = _GETTERS[ref.kind](ref.id, ref.version)
|
|
241
|
+
except NotFoundError:
|
|
242
|
+
read[lookup] = None
|
|
243
|
+
except (ValueError, KeyError, TypeError) as e:
|
|
244
|
+
read[lookup] = f"can't be read ({type(e).__name__}: {e})"
|
|
245
|
+
found = read[lookup]
|
|
246
|
+
if found is None:
|
|
247
|
+
at = f" version {ref.version}" if ref.version is not None else ""
|
|
248
|
+
other = self.named.get(fold(ref.id))
|
|
249
|
+
hint = f"; this bundle's {other.kind.store} {other.name!r} has that name" if other is not None else ""
|
|
250
|
+
return f"there is no {kind} {ref.id!r}{at} in the store{hint}"
|
|
251
|
+
if isinstance(found, str):
|
|
252
|
+
return f"{kind} {ref.id!r} {found}"
|
|
253
|
+
if ref.artifact_type and canonical_type(found.type) != canonical_type(ref.artifact_type):
|
|
254
|
+
return f"{ref.id!r} is {with_article(found.type)} in the store, but this field takes {ref.artifact_type}"
|
|
255
|
+
return None
|
|
256
|
+
|
|
257
|
+
def _check_files(self, closure: set[_Key]) -> None:
|
|
258
|
+
"""List the folder of each file artifact, and read its toml and each agent's the way their writes
|
|
259
|
+
will, so a link that leaves the bundle or a key the type doesn't take fails before anything is
|
|
260
|
+
written."""
|
|
261
|
+
registry = get_artifact_registry()
|
|
262
|
+
for key in sorted(closure, key=self.rank.__getitem__):
|
|
263
|
+
source = self.nodes[key]
|
|
264
|
+
if isinstance(source, BuiltImage):
|
|
265
|
+
continue
|
|
266
|
+
if source.entry.kind is BundleKind.AGENT:
|
|
267
|
+
try:
|
|
268
|
+
A2AAgent.accept_toml(source.config, AuthoringContext(self.resolved.bundle, source.entry))
|
|
269
|
+
except BundleError as e:
|
|
270
|
+
self.problems.extend(e.problems)
|
|
271
|
+
continue
|
|
272
|
+
if source.entry.kind is not BundleKind.ARTIFACT:
|
|
273
|
+
continue
|
|
274
|
+
cls = registry.get(canonical_type(source.entry.type))
|
|
275
|
+
walk = folder_walk(cls)
|
|
276
|
+
if walk is None:
|
|
277
|
+
continue
|
|
278
|
+
try:
|
|
279
|
+
walk(self.resolved.bundle, source.entry)
|
|
280
|
+
except BundleError as e:
|
|
281
|
+
self.problems.extend(e.problems)
|
|
282
|
+
try:
|
|
283
|
+
AuthoringContext(self.resolved.bundle, source.entry).accept(source.config, **cls.toml_keys)
|
|
284
|
+
except BundleError as e:
|
|
285
|
+
self.problems.extend(e.problems)
|
|
286
|
+
|
|
287
|
+
def _build_tasks(self, closure: set[_Key]) -> None:
|
|
288
|
+
"""Build each task's steps the way a write will, so a bad step fails before anything is written."""
|
|
289
|
+
registry = get_task_step_registry()
|
|
290
|
+
for key in sorted(closure, key=self.rank.__getitem__):
|
|
291
|
+
source = self.nodes[key]
|
|
292
|
+
if not (isinstance(source, ResolvedEntry) and source.entry.kind is BundleKind.TASK):
|
|
293
|
+
continue
|
|
294
|
+
steps, failed = [], False
|
|
295
|
+
for step in source.config:
|
|
296
|
+
try:
|
|
297
|
+
steps.append(build_step(step))
|
|
298
|
+
except Exception as e:
|
|
299
|
+
failed = True
|
|
300
|
+
self.problems.append(f"{self._path(source.entry)}: step {step.get('id')!r}: "
|
|
301
|
+
f"{registry[step['type']].type} can't read it ({type(e).__name__}: {e})")
|
|
302
|
+
if not failed:
|
|
303
|
+
try:
|
|
304
|
+
Task(id=source.entry.id, version=None, steps=steps)
|
|
305
|
+
except ValueError as e:
|
|
306
|
+
self.problems.append(f"{self._path(source.entry)}: {e}")
|
|
307
|
+
|
|
308
|
+
# Order
|
|
309
|
+
|
|
310
|
+
def _ordered(self, closure: set[_Key]) -> tuple[Write, ...]:
|
|
311
|
+
waiting = {key: len(self.edges[key]) for key in closure}
|
|
312
|
+
dependents: dict[_Key, list[_Key]] = {}
|
|
313
|
+
for key in closure:
|
|
314
|
+
for need in self.edges[key]:
|
|
315
|
+
dependents.setdefault(need, []).append(key)
|
|
316
|
+
ready = [(self.rank[key], key) for key, count in waiting.items() if count == 0]
|
|
317
|
+
heapq.heapify(ready)
|
|
318
|
+
writes = []
|
|
319
|
+
while ready:
|
|
320
|
+
_, key = heapq.heappop(ready)
|
|
321
|
+
source = self.nodes[key]
|
|
322
|
+
kind = BundleKind.ARTIFACT if isinstance(source, BuiltImage) else source.entry.kind
|
|
323
|
+
writes.append(Write(kind, key[1], source, tuple(self.edges[key])))
|
|
324
|
+
for dependent in dependents.get(key, ()):
|
|
325
|
+
waiting[dependent] -= 1
|
|
326
|
+
if waiting[dependent] == 0:
|
|
327
|
+
heapq.heappush(ready, (self.rank[dependent], dependent))
|
|
328
|
+
return tuple(writes)
|
|
329
|
+
|
|
330
|
+
def _path(self, entry: BundleEntry) -> str:
|
|
331
|
+
return relative(self.resolved.bundle.root, entry.path)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def folder_walk(cls: type | None) -> Any:
|
|
335
|
+
"""The listing a type's write runs: a subclass keeping its base's ``from_toml`` lists the same way,
|
|
336
|
+
one that overrides it may read its folder differently, so it gets none."""
|
|
337
|
+
for base, walk in _FOLDER_WALKS.items():
|
|
338
|
+
if cls is not None and issubclass(cls, base):
|
|
339
|
+
return walk if keeps_base_from_toml(cls, base) else None
|
|
340
|
+
return None
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def keeps_base_from_toml(cls: type, base: type) -> bool:
|
|
344
|
+
"""Whether ``cls`` is built from its TOML by ``base``'s ``from_toml`` rather than one of its own."""
|
|
345
|
+
return next(c for c in cls.__mro__ if "from_toml" in vars(c)) is base
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _key(entity: BundleEntry | BuiltImage) -> _Key:
|
|
349
|
+
kind = BundleKind.ARTIFACT if isinstance(entity, BuiltImage) else entity.kind
|
|
350
|
+
return (kind.store, entity.id)
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _nfc(text: str) -> str:
|
|
354
|
+
return unicodedata.normalize("NFC", text)
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _components(nodes: dict[_Key, Any], edges: dict[_Key, list[_Key]], rank: dict[_Key, int]) -> list[list[_Key]]:
|
|
358
|
+
"""The strongly connected components that hold a loop (Tarjan's algorithm, without recursion)."""
|
|
359
|
+
index: dict[_Key, int] = {}
|
|
360
|
+
low: dict[_Key, int] = {}
|
|
361
|
+
stack: list[_Key] = []
|
|
362
|
+
on_stack: set[_Key] = set()
|
|
363
|
+
loops = []
|
|
364
|
+
for root in sorted(nodes, key=rank.__getitem__):
|
|
365
|
+
if root in index:
|
|
366
|
+
continue
|
|
367
|
+
index[root] = low[root] = len(index)
|
|
368
|
+
stack.append(root)
|
|
369
|
+
on_stack.add(root)
|
|
370
|
+
work = [(root, iter(edges[root]))]
|
|
371
|
+
while work:
|
|
372
|
+
node, pending = work[-1]
|
|
373
|
+
for need in pending:
|
|
374
|
+
if need not in index:
|
|
375
|
+
index[need] = low[need] = len(index)
|
|
376
|
+
stack.append(need)
|
|
377
|
+
on_stack.add(need)
|
|
378
|
+
work.append((need, iter(edges[need])))
|
|
379
|
+
break
|
|
380
|
+
if need in on_stack:
|
|
381
|
+
low[node] = min(low[node], index[need])
|
|
382
|
+
else:
|
|
383
|
+
work.pop()
|
|
384
|
+
if work:
|
|
385
|
+
parent = work[-1][0]
|
|
386
|
+
low[parent] = min(low[parent], low[node])
|
|
387
|
+
if low[node] == index[node]:
|
|
388
|
+
component = []
|
|
389
|
+
while True:
|
|
390
|
+
member = stack.pop()
|
|
391
|
+
on_stack.discard(member)
|
|
392
|
+
component.append(member)
|
|
393
|
+
if member == node:
|
|
394
|
+
break
|
|
395
|
+
if len(component) > 1 or node in edges[node]:
|
|
396
|
+
loops.append(component)
|
|
397
|
+
return loops
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _loop(start: _Key, component: set[_Key], edges: dict[_Key, list[_Key]]) -> list[_Key]:
|
|
401
|
+
"""A shortest path from ``start`` back to itself inside its component."""
|
|
402
|
+
previous: dict[_Key, _Key | None] = {start: None}
|
|
403
|
+
queue = [start]
|
|
404
|
+
for node in queue:
|
|
405
|
+
if start in edges[node]:
|
|
406
|
+
break
|
|
407
|
+
for need in edges[node]:
|
|
408
|
+
if need in component and need not in previous:
|
|
409
|
+
previous[need] = node
|
|
410
|
+
queue.append(need)
|
|
411
|
+
path = [node]
|
|
412
|
+
while previous[path[-1]] is not None:
|
|
413
|
+
path.append(previous[path[-1]])
|
|
414
|
+
return [*reversed(path), start]
|