agentsml-impl 0.2.0__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.
- agentsml_impl/__init__.py +3 -0
- agentsml_impl/cli.py +472 -0
- agentsml_impl/compiler/__init__.py +1 -0
- agentsml_impl/compiler/capabilities.py +330 -0
- agentsml_impl/compiler/conditions.py +177 -0
- agentsml_impl/compiler/diagnostics.py +112 -0
- agentsml_impl/compiler/durations.py +31 -0
- agentsml_impl/compiler/expressions.py +123 -0
- agentsml_impl/compiler/identifiers.py +60 -0
- agentsml_impl/compiler/ir.py +391 -0
- agentsml_impl/compiler/limits.py +28 -0
- agentsml_impl/compiler/loader.py +83 -0
- agentsml_impl/compiler/naming.py +87 -0
- agentsml_impl/compiler/planner.py +79 -0
- agentsml_impl/compiler/render.py +90 -0
- agentsml_impl/compiler/schema_validation.py +121 -0
- agentsml_impl/compiler/semantic_validation.py +424 -0
- agentsml_impl/protocols/__init__.py +1 -0
- agentsml_impl/runtime/__init__.py +1 -0
- agentsml_impl/runtime/events.py +59 -0
- agentsml_impl/runtime/json_schema.py +138 -0
- agentsml_impl/runtime/tools.py +145 -0
- agentsml_impl/schemas/agentsml.schema.json +406 -0
- agentsml_impl/schemas/v0.1/agentsml.schema.json +628 -0
- agentsml_impl/schemas/v0.2/agentsml.schema.json +363 -0
- agentsml_impl/schemas/v0.3/agentsml.schema.json +406 -0
- agentsml_impl/targets/__init__.py +1 -0
- agentsml_impl/targets/a2a/__init__.py +0 -0
- agentsml_impl/targets/a2a/adapter.py +94 -0
- agentsml_impl/targets/a2a/capabilities.yaml +25 -0
- agentsml_impl/targets/ag2/__init__.py +0 -0
- agentsml_impl/targets/ag2/adapter.py +655 -0
- agentsml_impl/targets/ag2/capabilities.yaml +37 -0
- agentsml_impl/targets/ag_ui/__init__.py +0 -0
- agentsml_impl/targets/ag_ui/adapter.py +123 -0
- agentsml_impl/targets/ag_ui/capabilities.yaml +25 -0
- agentsml_impl/targets/agno/__init__.py +0 -0
- agentsml_impl/targets/agno/adapter.py +191 -0
- agentsml_impl/targets/agno/capabilities.yaml +26 -0
- agentsml_impl/targets/ap2/__init__.py +0 -0
- agentsml_impl/targets/ap2/adapter.py +84 -0
- agentsml_impl/targets/ap2/capabilities.yaml +25 -0
- agentsml_impl/targets/base.py +157 -0
- agentsml_impl/targets/bedrock_agentcore/__init__.py +0 -0
- agentsml_impl/targets/bedrock_agentcore/adapter.py +592 -0
- agentsml_impl/targets/bedrock_agentcore/capabilities.yaml +26 -0
- agentsml_impl/targets/claude_agent_sdk/__init__.py +0 -0
- agentsml_impl/targets/claude_agent_sdk/adapter.py +178 -0
- agentsml_impl/targets/claude_agent_sdk/capabilities.yaml +26 -0
- agentsml_impl/targets/crewai/__init__.py +0 -0
- agentsml_impl/targets/crewai/adapter.py +441 -0
- agentsml_impl/targets/crewai/capabilities.yaml +30 -0
- agentsml_impl/targets/databricks/__init__.py +0 -0
- agentsml_impl/targets/databricks/adapter.py +427 -0
- agentsml_impl/targets/databricks/capabilities.yaml +26 -0
- agentsml_impl/targets/flowise/__init__.py +0 -0
- agentsml_impl/targets/flowise/adapter.py +71 -0
- agentsml_impl/targets/flowise/capabilities.yaml +26 -0
- agentsml_impl/targets/google_adk/__init__.py +0 -0
- agentsml_impl/targets/google_adk/adapter.py +589 -0
- agentsml_impl/targets/google_adk/capabilities.yaml +36 -0
- agentsml_impl/targets/langchain/__init__.py +1 -0
- agentsml_impl/targets/langchain/adapter.py +351 -0
- agentsml_impl/targets/langchain/capabilities.yaml +30 -0
- agentsml_impl/targets/langflow/__init__.py +0 -0
- agentsml_impl/targets/langflow/adapter.py +147 -0
- agentsml_impl/targets/langflow/capabilities.yaml +26 -0
- agentsml_impl/targets/langgraph/__init__.py +0 -0
- agentsml_impl/targets/langgraph/adapter.py +911 -0
- agentsml_impl/targets/langgraph/capabilities.yaml +33 -0
- agentsml_impl/targets/llamaindex/__init__.py +1 -0
- agentsml_impl/targets/llamaindex/adapter.py +406 -0
- agentsml_impl/targets/llamaindex/capabilities.yaml +31 -0
- agentsml_impl/targets/lyzr/__init__.py +0 -0
- agentsml_impl/targets/lyzr/adapter.py +146 -0
- agentsml_impl/targets/lyzr/capabilities.yaml +26 -0
- agentsml_impl/targets/mastra/__init__.py +0 -0
- agentsml_impl/targets/mastra/adapter.py +635 -0
- agentsml_impl/targets/mastra/capabilities.yaml +32 -0
- agentsml_impl/targets/mcp_server/__init__.py +0 -0
- agentsml_impl/targets/mcp_server/adapter.py +141 -0
- agentsml_impl/targets/mcp_server/capabilities.yaml +25 -0
- agentsml_impl/targets/microsoft_agent_framework/__init__.py +0 -0
- agentsml_impl/targets/microsoft_agent_framework/adapter.py +563 -0
- agentsml_impl/targets/microsoft_agent_framework/capabilities.yaml +26 -0
- agentsml_impl/targets/n8n/__init__.py +0 -0
- agentsml_impl/targets/n8n/adapter.py +139 -0
- agentsml_impl/targets/n8n/capabilities.yaml +26 -0
- agentsml_impl/targets/openai_agents/__init__.py +0 -0
- agentsml_impl/targets/openai_agents/adapter.py +175 -0
- agentsml_impl/targets/openai_agents/capabilities.yaml +27 -0
- agentsml_impl/targets/pydantic_ai/__init__.py +0 -0
- agentsml_impl/targets/pydantic_ai/adapter.py +922 -0
- agentsml_impl/targets/pydantic_ai/capabilities.yaml +37 -0
- agentsml_impl/targets/registry.py +290 -0
- agentsml_impl/targets/relevance_ai/__init__.py +0 -0
- agentsml_impl/targets/relevance_ai/adapter.py +66 -0
- agentsml_impl/targets/relevance_ai/capabilities.yaml +26 -0
- agentsml_impl/targets/smolagents/__init__.py +0 -0
- agentsml_impl/targets/smolagents/adapter.py +210 -0
- agentsml_impl/targets/smolagents/capabilities.yaml +27 -0
- agentsml_impl/targets/snowflake_cortex/__init__.py +0 -0
- agentsml_impl/targets/snowflake_cortex/adapter.py +167 -0
- agentsml_impl/targets/snowflake_cortex/capabilities.yaml +26 -0
- agentsml_impl/targets/spring_ai/__init__.py +1 -0
- agentsml_impl/targets/spring_ai/adapter.py +626 -0
- agentsml_impl/targets/spring_ai/capabilities.yaml +31 -0
- agentsml_impl/targets/strands_agents/__init__.py +1 -0
- agentsml_impl/targets/strands_agents/adapter.py +309 -0
- agentsml_impl/targets/strands_agents/capabilities.yaml +29 -0
- agentsml_impl/targets/support_matrix.py +91 -0
- agentsml_impl/targets/ucp/__init__.py +0 -0
- agentsml_impl/targets/ucp/adapter.py +86 -0
- agentsml_impl/targets/ucp/capabilities.yaml +25 -0
- agentsml_impl/targets/vercel_ai/__init__.py +1 -0
- agentsml_impl/targets/vercel_ai/adapter.py +349 -0
- agentsml_impl/targets/vercel_ai/capabilities.yaml +30 -0
- agentsml_impl/targets/vertex_agent_engine/__init__.py +0 -0
- agentsml_impl/targets/vertex_agent_engine/adapter.py +407 -0
- agentsml_impl/targets/vertex_agent_engine/capabilities.yaml +26 -0
- agentsml_impl/targets/zapier_agents/__init__.py +0 -0
- agentsml_impl/targets/zapier_agents/adapter.py +106 -0
- agentsml_impl/targets/zapier_agents/capabilities.yaml +26 -0
- agentsml_impl-0.2.0.dist-info/METADATA +109 -0
- agentsml_impl-0.2.0.dist-info/RECORD +129 -0
- agentsml_impl-0.2.0.dist-info/WHEEL +5 -0
- agentsml_impl-0.2.0.dist-info/entry_points.txt +3 -0
- agentsml_impl-0.2.0.dist-info/licenses/LICENSE +28 -0
- agentsml_impl-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"""CORE-005: Semantic Validation — validate references and workflow semantics beyond JSON Schema."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
from agentsml_impl.compiler.ir import AgentSystemIR
|
|
5
|
+
from agentsml_impl.compiler.diagnostics import DiagCode, DiagnosticSet
|
|
6
|
+
from agentsml_impl.compiler.expressions import ExprKind, Expression, extract_expressions
|
|
7
|
+
from agentsml_impl.compiler.conditions import ConditionError, compile_condition_with_references
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
_GRAPH_PROFILES = {"agentsml-graph"}
|
|
11
|
+
_RAG_PROFILES = {"agentsml-rag"}
|
|
12
|
+
_MCP_PROFILES = {"agentsml-mcp"}
|
|
13
|
+
_A2A_PROFILES = {"agentsml-a2a"}
|
|
14
|
+
_UI_PROFILES = {"agentsml-ui"}
|
|
15
|
+
_COMMERCE_PROFILES = {"agentsml-commerce", "agentsml-payments"}
|
|
16
|
+
|
|
17
|
+
_KNOWN_SIDE_EFFECTS = {"read", "write", "delete", "browser", "external", "payment", "unknown"}
|
|
18
|
+
_DANGEROUS_SIDE_EFFECTS = {"payment", "delete"}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def validate_semantics(ir: AgentSystemIR, diagnostics: DiagnosticSet) -> None:
|
|
22
|
+
"""Run all semantic validation checks on the IR and emit diagnostics."""
|
|
23
|
+
_check_model_references(ir, diagnostics)
|
|
24
|
+
_check_agent_references(ir, diagnostics)
|
|
25
|
+
_check_workflow_steps(ir, diagnostics)
|
|
26
|
+
_check_workflow_edges(ir, diagnostics)
|
|
27
|
+
_check_policy_references(ir, diagnostics)
|
|
28
|
+
_check_tool_safety(ir, diagnostics)
|
|
29
|
+
_check_profile_usage(ir, diagnostics)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _check_model_references(ir: AgentSystemIR, ds: DiagnosticSet) -> None:
|
|
33
|
+
for agent_key, agent in ir.agents.items():
|
|
34
|
+
if agent.model not in ir.models:
|
|
35
|
+
ds.add_error(
|
|
36
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
37
|
+
f"Agent '{agent_key}' references undefined model '{agent.model}'.",
|
|
38
|
+
path=f"agents.{agent_key}.model",
|
|
39
|
+
)
|
|
40
|
+
# Check model fallbacks
|
|
41
|
+
for model_key, model in ir.models.items():
|
|
42
|
+
for fb in model.fallbacks:
|
|
43
|
+
if fb not in ir.models:
|
|
44
|
+
ds.add_warning(
|
|
45
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
46
|
+
f"Model '{model_key}' fallback '{fb}' is not defined in models.",
|
|
47
|
+
path=f"models.{model_key}.fallbacks",
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _check_agent_references(ir: AgentSystemIR, ds: DiagnosticSet) -> None:
|
|
52
|
+
for agent_key, agent in ir.agents.items():
|
|
53
|
+
for tool_ref in agent.tools:
|
|
54
|
+
if tool_ref not in ir.tools:
|
|
55
|
+
ds.add_error(
|
|
56
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
57
|
+
f"Agent '{agent_key}' references undefined tool '{tool_ref}'.",
|
|
58
|
+
path=f"agents.{agent_key}.tools",
|
|
59
|
+
)
|
|
60
|
+
for res_ref in agent.resources:
|
|
61
|
+
if res_ref not in ir.resources:
|
|
62
|
+
ds.add_warning(
|
|
63
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
64
|
+
f"Agent '{agent_key}' references undefined resource '{res_ref}'.",
|
|
65
|
+
path=f"agents.{agent_key}.resources",
|
|
66
|
+
)
|
|
67
|
+
for prompt_ref in agent.prompts:
|
|
68
|
+
if prompt_ref not in ir.prompts:
|
|
69
|
+
ds.add_warning(
|
|
70
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
71
|
+
f"Agent '{agent_key}' references undefined prompt '{prompt_ref}'.",
|
|
72
|
+
path=f"agents.{agent_key}.prompts",
|
|
73
|
+
)
|
|
74
|
+
for pol_ref in agent.policies:
|
|
75
|
+
if pol_ref not in ir.policies:
|
|
76
|
+
ds.add_warning(
|
|
77
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
78
|
+
f"Agent '{agent_key}' references undefined policy '{pol_ref}'.",
|
|
79
|
+
path=f"agents.{agent_key}.policies",
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _check_workflow_steps(ir: AgentSystemIR, ds: DiagnosticSet) -> None:
|
|
84
|
+
a2a_agents = ir.a2a_agents
|
|
85
|
+
|
|
86
|
+
for wf_key, wf in ir.workflows.items():
|
|
87
|
+
seen_ids: set[str] = set()
|
|
88
|
+
step_ids = wf.step_ids()
|
|
89
|
+
for step in wf.steps:
|
|
90
|
+
# Duplicate step IDs
|
|
91
|
+
if step.id in seen_ids:
|
|
92
|
+
ds.add_error(
|
|
93
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
94
|
+
f"Workflow '{wf_key}' has duplicate step id '{step.id}'.",
|
|
95
|
+
path=f"workflows.{wf_key}.steps",
|
|
96
|
+
)
|
|
97
|
+
seen_ids.add(step.id)
|
|
98
|
+
|
|
99
|
+
if step.fallback is not None and step.fallback.model \
|
|
100
|
+
and step.fallback.model not in ir.models:
|
|
101
|
+
ds.add_error(
|
|
102
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
103
|
+
f"Step '{step.id}' in workflow '{wf_key}' fallback.model references "
|
|
104
|
+
f"undefined model '{step.fallback.model}'.",
|
|
105
|
+
path=f"workflows.{wf_key}.steps.{step.id}.fallback.model",
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
targets = step.primary_targets()
|
|
109
|
+
|
|
110
|
+
# Composite step (more than one primary target)
|
|
111
|
+
if len(targets) > 1:
|
|
112
|
+
ds.add_error(
|
|
113
|
+
DiagCode.UNSUPPORTED_FEATURE,
|
|
114
|
+
f"Step '{step.id}' in workflow '{wf_key}' declares multiple primary targets: {targets}. "
|
|
115
|
+
"Only one of agent, tool, workflow, or a2a_agent is allowed per step.",
|
|
116
|
+
path=f"workflows.{wf_key}.steps.{step.id}",
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
if step.retry is not None and step.retry.extras:
|
|
120
|
+
ds.add_warning(
|
|
121
|
+
DiagCode.LOSSY_MAPPING,
|
|
122
|
+
f"Step '{step.id}' retry has unrecognised or non-typed fields "
|
|
123
|
+
f"{sorted(step.retry.extras)}; they are ignored by generated code.",
|
|
124
|
+
path=f"workflows.{wf_key}.steps.{step.id}.retry",
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
# Validate references
|
|
128
|
+
if step.agent and step.agent not in ir.agents:
|
|
129
|
+
ds.add_error(
|
|
130
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
131
|
+
f"Step '{step.id}' references undefined agent '{step.agent}'.",
|
|
132
|
+
path=f"workflows.{wf_key}.steps.{step.id}.agent",
|
|
133
|
+
)
|
|
134
|
+
if step.tool and step.tool not in ir.tools:
|
|
135
|
+
ds.add_error(
|
|
136
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
137
|
+
f"Step '{step.id}' references undefined tool '{step.tool}'.",
|
|
138
|
+
path=f"workflows.{wf_key}.steps.{step.id}.tool",
|
|
139
|
+
)
|
|
140
|
+
if step.workflow and step.workflow not in ir.workflows:
|
|
141
|
+
ds.add_error(
|
|
142
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
143
|
+
f"Step '{step.id}' references undefined workflow '{step.workflow}'.",
|
|
144
|
+
path=f"workflows.{wf_key}.steps.{step.id}.workflow",
|
|
145
|
+
)
|
|
146
|
+
if step.a2a_agent and step.a2a_agent not in a2a_agents:
|
|
147
|
+
ds.add_error(
|
|
148
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
149
|
+
f"Step '{step.id}' references undefined a2a_agent '{step.a2a_agent}'.",
|
|
150
|
+
path=f"workflows.{wf_key}.steps.{step.id}.a2a_agent",
|
|
151
|
+
recommendation="Declare the agent in the top-level a2a_agents block.",
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
_validate_workflow_expression_value(
|
|
155
|
+
step.input,
|
|
156
|
+
f"workflows.{wf_key}.steps.{step.id}.input",
|
|
157
|
+
step_ids,
|
|
158
|
+
ds,
|
|
159
|
+
)
|
|
160
|
+
_validate_workflow_expression_value(
|
|
161
|
+
step.output,
|
|
162
|
+
f"workflows.{wf_key}.steps.{step.id}.output",
|
|
163
|
+
step_ids,
|
|
164
|
+
ds,
|
|
165
|
+
)
|
|
166
|
+
_validate_workflow_expression_value(
|
|
167
|
+
step.condition,
|
|
168
|
+
f"workflows.{wf_key}.steps.{step.id}.condition",
|
|
169
|
+
step_ids,
|
|
170
|
+
ds,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# depends_on references
|
|
174
|
+
for dep in step.depends_on:
|
|
175
|
+
if dep not in seen_ids and dep not in step_ids:
|
|
176
|
+
ds.add_warning(
|
|
177
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
178
|
+
f"Step '{step.id}' depends_on '{dep}' which is not yet declared.",
|
|
179
|
+
path=f"workflows.{wf_key}.steps.{step.id}.depends_on",
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# Approval-only step without approval field
|
|
183
|
+
if not targets and not step.is_approval_only():
|
|
184
|
+
ds.add_warning(
|
|
185
|
+
DiagCode.UNSUPPORTED_FEATURE,
|
|
186
|
+
f"Step '{step.id}' in workflow '{wf_key}' has no primary target and no approval. "
|
|
187
|
+
"It will produce no output.",
|
|
188
|
+
path=f"workflows.{wf_key}.steps.{step.id}",
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _check_workflow_edges(ir: AgentSystemIR, ds: DiagnosticSet) -> None:
|
|
193
|
+
all_profiles = set(ir.profiles.all_profiles())
|
|
194
|
+
|
|
195
|
+
for wf_key, wf in ir.workflows.items():
|
|
196
|
+
step_ids = wf.step_ids()
|
|
197
|
+
for i, edge in enumerate(wf.edges):
|
|
198
|
+
if edge.from_step not in step_ids:
|
|
199
|
+
ds.add_error(
|
|
200
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
201
|
+
f"Edge in workflow '{wf_key}' references undefined step '{edge.from_step}' in 'from'.",
|
|
202
|
+
path=f"workflows.{wf_key}.edges[{i}].from",
|
|
203
|
+
)
|
|
204
|
+
if edge.to_step not in step_ids:
|
|
205
|
+
ds.add_error(
|
|
206
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
207
|
+
f"Edge in workflow '{wf_key}' references undefined step '{edge.to_step}' in 'to'.",
|
|
208
|
+
path=f"workflows.{wf_key}.edges[{i}].to",
|
|
209
|
+
)
|
|
210
|
+
_validate_workflow_expression_value(
|
|
211
|
+
edge.condition,
|
|
212
|
+
f"workflows.{wf_key}.edges[{i}].condition",
|
|
213
|
+
step_ids,
|
|
214
|
+
ds,
|
|
215
|
+
)
|
|
216
|
+
_validate_condition_references(
|
|
217
|
+
edge.condition,
|
|
218
|
+
f"workflows.{wf_key}.edges[{i}].condition",
|
|
219
|
+
step_ids,
|
|
220
|
+
ds,
|
|
221
|
+
)
|
|
222
|
+
_validate_workflow_expression_value(
|
|
223
|
+
edge.label,
|
|
224
|
+
f"workflows.{wf_key}.edges[{i}].label",
|
|
225
|
+
step_ids,
|
|
226
|
+
ds,
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
# Cycles require agentsml-graph
|
|
230
|
+
if wf.has_cycles() and not (all_profiles & _GRAPH_PROFILES):
|
|
231
|
+
ds.add_error(
|
|
232
|
+
DiagCode.UNSUPPORTED_PROFILE,
|
|
233
|
+
f"Workflow '{wf_key}' has cyclic edges but does not declare 'agentsml-graph' profile.",
|
|
234
|
+
path=f"workflows.{wf_key}.edges",
|
|
235
|
+
recommendation="Add 'agentsml-graph' to profiles.required.",
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
# Unconditional multi-path fan-out requires agentsml-graph
|
|
239
|
+
fan_out: dict[str, int] = {}
|
|
240
|
+
for edge in wf.edges:
|
|
241
|
+
if not edge.condition:
|
|
242
|
+
fan_out[edge.from_step] = fan_out.get(edge.from_step, 0) + 1
|
|
243
|
+
for source, count in fan_out.items():
|
|
244
|
+
if count > 1 and not (all_profiles & _GRAPH_PROFILES):
|
|
245
|
+
ds.add_error(
|
|
246
|
+
DiagCode.UNSUPPORTED_PROFILE,
|
|
247
|
+
f"Workflow '{wf_key}' fans out from step '{source}' to {count} unconditional successors "
|
|
248
|
+
"but does not declare 'agentsml-graph' profile.",
|
|
249
|
+
path=f"workflows.{wf_key}.edges",
|
|
250
|
+
recommendation="Add 'agentsml-graph' to profiles.required, or use conditional edges.",
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
# Joins (several unconditional predecessors) require agentsml-graph
|
|
254
|
+
fan_in: dict[str, int] = {}
|
|
255
|
+
for edge in wf.edges:
|
|
256
|
+
if not edge.condition:
|
|
257
|
+
fan_in[edge.to_step] = fan_in.get(edge.to_step, 0) + 1
|
|
258
|
+
for target, count in fan_in.items():
|
|
259
|
+
if count > 1 and not (all_profiles & _GRAPH_PROFILES):
|
|
260
|
+
ds.add_error(
|
|
261
|
+
DiagCode.UNSUPPORTED_PROFILE,
|
|
262
|
+
f"Workflow '{wf_key}' joins {count} unconditional predecessors into step '{target}' "
|
|
263
|
+
"but does not declare 'agentsml-graph' profile.",
|
|
264
|
+
path=f"workflows.{wf_key}.edges",
|
|
265
|
+
recommendation="Add 'agentsml-graph' to profiles.required.",
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _validate_workflow_expression_value(
|
|
270
|
+
value: object,
|
|
271
|
+
context_path: str,
|
|
272
|
+
step_ids: set[str],
|
|
273
|
+
ds: DiagnosticSet,
|
|
274
|
+
) -> None:
|
|
275
|
+
for expr in extract_expressions(value, context_path, ds):
|
|
276
|
+
_validate_workflow_expression(expr, context_path, step_ids, ds)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def _validate_condition_references(
|
|
280
|
+
value: object,
|
|
281
|
+
context_path: str,
|
|
282
|
+
step_ids: set[str],
|
|
283
|
+
ds: DiagnosticSet,
|
|
284
|
+
) -> None:
|
|
285
|
+
if not isinstance(value, str):
|
|
286
|
+
return
|
|
287
|
+
try:
|
|
288
|
+
_, references = compile_condition_with_references(value)
|
|
289
|
+
except ConditionError:
|
|
290
|
+
return
|
|
291
|
+
for ref in references:
|
|
292
|
+
parts = ref.split(".")
|
|
293
|
+
if parts[0] == "steps" and (len(parts) < 3 or parts[1] not in step_ids):
|
|
294
|
+
step_id = parts[1] if len(parts) > 1 else ""
|
|
295
|
+
ds.add_error(
|
|
296
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
297
|
+
f"Condition '{value.strip()}' references undefined step '{step_id}'.",
|
|
298
|
+
path=context_path,
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _validate_workflow_expression(
|
|
303
|
+
expr: Expression,
|
|
304
|
+
context_path: str,
|
|
305
|
+
step_ids: set[str],
|
|
306
|
+
ds: DiagnosticSet,
|
|
307
|
+
) -> None:
|
|
308
|
+
if expr.kind != ExprKind.STEP:
|
|
309
|
+
return
|
|
310
|
+
|
|
311
|
+
parts = expr.path.split(".")
|
|
312
|
+
if len(parts) < 3:
|
|
313
|
+
ds.add_warning(
|
|
314
|
+
DiagCode.NONPORTABLE_EXPRESSION,
|
|
315
|
+
f"Expression '{expr.raw.strip()}' should reference a step field like 'steps.<step_id>.output'.",
|
|
316
|
+
path=expr.source_path or context_path,
|
|
317
|
+
recommendation="Use a step field reference such as ${steps.review.output}.",
|
|
318
|
+
)
|
|
319
|
+
return
|
|
320
|
+
|
|
321
|
+
step_id = parts[1]
|
|
322
|
+
if step_id not in step_ids:
|
|
323
|
+
ds.add_error(
|
|
324
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
325
|
+
f"Expression '{expr.raw.strip()}' references undefined step '{step_id}'.",
|
|
326
|
+
path=expr.source_path or context_path,
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _check_policy_references(ir: AgentSystemIR, ds: DiagnosticSet) -> None:
|
|
331
|
+
for pol_key, pol in ir.policies.items():
|
|
332
|
+
for ref in pol.applies_to:
|
|
333
|
+
parts = ref.split(".")
|
|
334
|
+
if len(parts) >= 2:
|
|
335
|
+
collection_name, item_key = parts[0], parts[1]
|
|
336
|
+
if item_key == "*":
|
|
337
|
+
continue
|
|
338
|
+
if collection_name == "workflows" and len(parts) == 4 and parts[2] == "steps":
|
|
339
|
+
wf_name, step_name = parts[1], parts[3]
|
|
340
|
+
wf = ir.workflows.get(wf_name)
|
|
341
|
+
if wf and step_name != "*" and not any(s.id == step_name for s in wf.steps):
|
|
342
|
+
ds.add_warning(
|
|
343
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
344
|
+
f"Policy '{pol_key}' applies_to step '{step_name}' in workflow '{wf_name}' which does not exist.",
|
|
345
|
+
path=f"policies.{pol_key}.applies_to",
|
|
346
|
+
)
|
|
347
|
+
elif not wf and wf_name != "*":
|
|
348
|
+
ds.add_warning(
|
|
349
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
350
|
+
f"Policy '{pol_key}' applies_to workflow '{wf_name}' which does not exist.",
|
|
351
|
+
path=f"policies.{pol_key}.applies_to",
|
|
352
|
+
)
|
|
353
|
+
continue
|
|
354
|
+
collection = getattr(ir, collection_name, {})
|
|
355
|
+
if hasattr(collection, "__contains__") and item_key not in collection:
|
|
356
|
+
ds.add_warning(
|
|
357
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
358
|
+
f"Policy '{pol_key}' applies_to '{ref}' which does not exist.",
|
|
359
|
+
path=f"policies.{pol_key}.applies_to",
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def _check_tool_safety(ir: AgentSystemIR, ds: DiagnosticSet) -> None:
|
|
364
|
+
for tool_key, tool in ir.tools.items():
|
|
365
|
+
if tool.side_effect in _DANGEROUS_SIDE_EFFECTS:
|
|
366
|
+
# Check there is some approval gate
|
|
367
|
+
has_approval = bool(tool.needs_approval) or (
|
|
368
|
+
tool.permissions is not None
|
|
369
|
+
and "approval" in tool.permissions
|
|
370
|
+
and tool.permissions["approval"] != "not_required"
|
|
371
|
+
)
|
|
372
|
+
if not has_approval:
|
|
373
|
+
ds.add_warning(
|
|
374
|
+
DiagCode.UNSAFE_SIDE_EFFECT,
|
|
375
|
+
f"Tool '{tool_key}' has side_effect '{tool.side_effect}' but no approval gate declared.",
|
|
376
|
+
path=f"tools.{tool_key}.side_effect",
|
|
377
|
+
recommendation="Add permissions.approval: required to this tool.",
|
|
378
|
+
)
|
|
379
|
+
if tool.is_mcp() and not tool.mcp_server:
|
|
380
|
+
ds.add_error(
|
|
381
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
382
|
+
f"Tool '{tool_key}' has source 'mcp.tool' but no mcp_server declared.",
|
|
383
|
+
path=f"tools.{tool_key}.mcp_server",
|
|
384
|
+
recommendation="Declare the MCP server in the top-level mcp_servers block and reference it.",
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def _check_profile_usage(ir: AgentSystemIR, ds: DiagnosticSet) -> None:
|
|
389
|
+
all_profiles = set(ir.profiles.all_profiles())
|
|
390
|
+
|
|
391
|
+
# MCP tools declared without mcp profile
|
|
392
|
+
has_mcp_tools = any(t.is_mcp() for t in ir.tools.values())
|
|
393
|
+
has_mcp_servers = bool(ir.mcp_servers)
|
|
394
|
+
if (has_mcp_tools or has_mcp_servers) and not (all_profiles & _MCP_PROFILES):
|
|
395
|
+
ds.add_warning(
|
|
396
|
+
DiagCode.UNSUPPORTED_PROFILE,
|
|
397
|
+
"Document uses MCP tools or mcp_servers but does not declare 'agentsml-mcp' profile.",
|
|
398
|
+
recommendation="Add 'agentsml-mcp' to profiles.required.",
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
# A2A agents without a2a profile
|
|
402
|
+
if ir.a2a_agents and not (all_profiles & _A2A_PROFILES):
|
|
403
|
+
ds.add_warning(
|
|
404
|
+
DiagCode.UNSUPPORTED_PROFILE,
|
|
405
|
+
"Document declares a2a_agents but does not declare 'agentsml-a2a' profile.",
|
|
406
|
+
recommendation="Add 'agentsml-a2a' to profiles.required.",
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
# Knowledge (RAG) without rag profile
|
|
410
|
+
has_knowledge = any(a.knowledge for a in ir.agents.values())
|
|
411
|
+
if has_knowledge and not (all_profiles & _RAG_PROFILES):
|
|
412
|
+
ds.add_warning(
|
|
413
|
+
DiagCode.UNSUPPORTED_PROFILE,
|
|
414
|
+
"One or more agents declare knowledge but 'agentsml-rag' profile is not declared.",
|
|
415
|
+
recommendation="Add 'agentsml-rag' to profiles.required.",
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
# UI block without ui profile
|
|
419
|
+
if ir.ui and not (all_profiles & _UI_PROFILES):
|
|
420
|
+
ds.add_warning(
|
|
421
|
+
DiagCode.UNSUPPORTED_PROFILE,
|
|
422
|
+
"Document declares a ui block but 'agentsml-ui' profile is not declared.",
|
|
423
|
+
recommendation="Add 'agentsml-ui' to profiles.required.",
|
|
424
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Protocols package."""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Runtime helpers package."""
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""RUNTIME-003: Event normalization — AgentsML event constants and helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
from typing import Any
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AgentSMLEvent:
|
|
9
|
+
RUN_STARTED = "run.started"
|
|
10
|
+
RUN_FINISHED = "run.finished"
|
|
11
|
+
RUN_ERROR = "run.error"
|
|
12
|
+
STEP_STARTED = "step.started"
|
|
13
|
+
STEP_FINISHED = "step.finished"
|
|
14
|
+
MESSAGE_DELTA = "message.delta"
|
|
15
|
+
TOOL_CALL = "tool.call"
|
|
16
|
+
TOOL_RESULT = "tool.result"
|
|
17
|
+
APPROVAL_REQUESTED = "approval.requested"
|
|
18
|
+
APPROVAL_RESOLVED = "approval.resolved"
|
|
19
|
+
STATE_SNAPSHOT = "state.snapshot"
|
|
20
|
+
STATE_DELTA = "state.delta"
|
|
21
|
+
ARTIFACT_CREATED = "artifact.created"
|
|
22
|
+
HUMAN_INPUT_REQUIRED = "human.input_required"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# AG-UI event name mapping
|
|
26
|
+
AG_UI_EVENT_MAP: dict[str, str] = {
|
|
27
|
+
AgentSMLEvent.RUN_STARTED: "RUN_STARTED",
|
|
28
|
+
AgentSMLEvent.RUN_FINISHED: "RUN_FINISHED",
|
|
29
|
+
AgentSMLEvent.RUN_ERROR: "RUN_ERROR",
|
|
30
|
+
AgentSMLEvent.MESSAGE_DELTA: "TEXT_MESSAGE_CHUNK",
|
|
31
|
+
AgentSMLEvent.TOOL_CALL: "TOOL_CALL_START",
|
|
32
|
+
AgentSMLEvent.TOOL_RESULT: "TOOL_CALL_END",
|
|
33
|
+
AgentSMLEvent.STATE_SNAPSHOT: "STATE_SNAPSHOT",
|
|
34
|
+
AgentSMLEvent.STATE_DELTA: "STATE_DELTA",
|
|
35
|
+
AgentSMLEvent.HUMAN_INPUT_REQUIRED: "INTERRUPT",
|
|
36
|
+
AgentSMLEvent.ARTIFACT_CREATED: "CUSTOM",
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def make_event(event_type: str, data: Any = None, run_id: str = "", step_id: str = "") -> dict:
|
|
41
|
+
"""Construct a normalized AgentsML event dict."""
|
|
42
|
+
ev: dict = {"type": event_type}
|
|
43
|
+
if run_id:
|
|
44
|
+
ev["run_id"] = run_id
|
|
45
|
+
if step_id:
|
|
46
|
+
ev["step_id"] = step_id
|
|
47
|
+
if data is not None:
|
|
48
|
+
ev["data"] = data
|
|
49
|
+
return ev
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def serialize_event(event: dict) -> str:
|
|
53
|
+
"""Serialize event to JSON string."""
|
|
54
|
+
return json.dumps(event, default=str)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def to_sse(event: dict) -> str:
|
|
58
|
+
"""Format event as SSE (Server-Sent Events) line."""
|
|
59
|
+
return f"data: {serialize_event(event)}\n\n"
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""RUNTIME-001: JSON Schema to Pydantic model generator."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
from agentsml_impl.compiler.diagnostics import DiagCode, DiagnosticSet
|
|
6
|
+
from agentsml_impl.compiler.naming import pascal_identifier, snake_identifier, unique_identifiers
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
_TYPE_MAP = {
|
|
10
|
+
"string": "str",
|
|
11
|
+
"integer": "int",
|
|
12
|
+
"number": "float",
|
|
13
|
+
"boolean": "bool",
|
|
14
|
+
"null": "None",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_UNSUPPORTED_KEYWORDS = {"oneOf", "anyOf", "allOf", "not", "$ref", "if", "then", "else"}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def schema_to_python_type(
|
|
21
|
+
schema: dict,
|
|
22
|
+
class_name: str,
|
|
23
|
+
diagnostics: DiagnosticSet,
|
|
24
|
+
classes: Optional[list[str]] = None,
|
|
25
|
+
path: str = "",
|
|
26
|
+
) -> tuple[str, list[str]]:
|
|
27
|
+
"""Convert a JSON Schema to a Python type annotation and optional class definitions.
|
|
28
|
+
|
|
29
|
+
Returns (type_annotation, [class_source_lines]).
|
|
30
|
+
"""
|
|
31
|
+
if classes is None:
|
|
32
|
+
classes = []
|
|
33
|
+
|
|
34
|
+
if not schema:
|
|
35
|
+
return "dict[str, Any]", classes
|
|
36
|
+
|
|
37
|
+
for kw in _UNSUPPORTED_KEYWORDS:
|
|
38
|
+
if kw in schema:
|
|
39
|
+
diagnostics.add_warning(
|
|
40
|
+
DiagCode.REQUIRES_ADAPTER,
|
|
41
|
+
f"JSON Schema keyword '{kw}' at '{path}' is not supported for Pydantic codegen. "
|
|
42
|
+
"Falling back to dict[str, Any].",
|
|
43
|
+
path=path,
|
|
44
|
+
recommendation="Simplify the schema or implement a custom Pydantic validator.",
|
|
45
|
+
)
|
|
46
|
+
return "dict[str, Any]", classes
|
|
47
|
+
|
|
48
|
+
schema_type = schema.get("type")
|
|
49
|
+
|
|
50
|
+
if schema_type == "object":
|
|
51
|
+
return _object_to_class(schema, class_name, diagnostics, classes, path)
|
|
52
|
+
elif schema_type == "array":
|
|
53
|
+
items = schema.get("items", {})
|
|
54
|
+
if isinstance(items, dict) and items.get("type") == "object":
|
|
55
|
+
item_class = class_name + "Item"
|
|
56
|
+
item_type, classes = _object_to_class(items, item_class, diagnostics, classes, path + ".items")
|
|
57
|
+
return f"list[{item_class}]", classes
|
|
58
|
+
else:
|
|
59
|
+
item_py, classes = schema_to_python_type(items, class_name + "Item", diagnostics, classes, path + ".items")
|
|
60
|
+
return f"list[{item_py}]", classes
|
|
61
|
+
elif schema_type in _TYPE_MAP:
|
|
62
|
+
return _TYPE_MAP[schema_type], classes
|
|
63
|
+
else:
|
|
64
|
+
return "Any", classes
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _object_to_class(
|
|
68
|
+
schema: dict,
|
|
69
|
+
class_name: str,
|
|
70
|
+
diagnostics: DiagnosticSet,
|
|
71
|
+
classes: list[str],
|
|
72
|
+
path: str,
|
|
73
|
+
) -> tuple[str, list[str]]:
|
|
74
|
+
props = schema.get("properties", {})
|
|
75
|
+
required = set(schema.get("required", []))
|
|
76
|
+
|
|
77
|
+
lines = [f"class {class_name}(BaseModel):"]
|
|
78
|
+
if not props:
|
|
79
|
+
lines.append(" pass")
|
|
80
|
+
else:
|
|
81
|
+
field_names = unique_identifiers(
|
|
82
|
+
props,
|
|
83
|
+
lambda value: snake_identifier(value, prefix="field"),
|
|
84
|
+
)
|
|
85
|
+
for field_name, field_schema in props.items():
|
|
86
|
+
safe_field_name = field_names[field_name]
|
|
87
|
+
nested_class = class_name + pascal_identifier(field_name, prefix="Field")
|
|
88
|
+
field_type, classes = schema_to_python_type(
|
|
89
|
+
field_schema, nested_class, diagnostics, classes, f"{path}.{field_name}"
|
|
90
|
+
)
|
|
91
|
+
alias = f" = Field(alias={field_name!r})" if safe_field_name != field_name else ""
|
|
92
|
+
if field_name in required:
|
|
93
|
+
lines.append(f" {safe_field_name}: {field_type}{alias}")
|
|
94
|
+
else:
|
|
95
|
+
default = (
|
|
96
|
+
f"Field(default=None, alias={field_name!r})"
|
|
97
|
+
if safe_field_name != field_name else "None"
|
|
98
|
+
)
|
|
99
|
+
lines.append(f" {safe_field_name}: Optional[{field_type}] = {default}")
|
|
100
|
+
|
|
101
|
+
classes.append("\n".join(lines))
|
|
102
|
+
return class_name, classes
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def generate_models_source(
|
|
106
|
+
schemas: dict[str, dict],
|
|
107
|
+
diagnostics: DiagnosticSet,
|
|
108
|
+
) -> str:
|
|
109
|
+
"""Generate a complete models.py source from a dict of {class_name: json_schema}."""
|
|
110
|
+
all_classes: list[str] = []
|
|
111
|
+
for name, schema in schemas.items():
|
|
112
|
+
class_name = _to_class_name(name)
|
|
113
|
+
_, all_classes = schema_to_python_type(schema, class_name, diagnostics, all_classes, name)
|
|
114
|
+
|
|
115
|
+
class_names: list[str] = []
|
|
116
|
+
for class_src in all_classes:
|
|
117
|
+
first_line = class_src.splitlines()[0].strip()
|
|
118
|
+
if first_line.startswith("class ") and "(BaseModel):" in first_line:
|
|
119
|
+
class_names.append(first_line[len("class ") :].split("(", 1)[0])
|
|
120
|
+
|
|
121
|
+
lines = [
|
|
122
|
+
'"""Auto-generated Pydantic models from AgentsML schemas."""',
|
|
123
|
+
"from __future__ import annotations",
|
|
124
|
+
"from typing import Any, Optional",
|
|
125
|
+
"from pydantic import BaseModel, Field",
|
|
126
|
+
"",
|
|
127
|
+
]
|
|
128
|
+
lines.extend("\n\n".join(all_classes).splitlines())
|
|
129
|
+
if class_names:
|
|
130
|
+
lines.append("")
|
|
131
|
+
lines.append("# Rebuild models so nested forward references resolve under Pydantic v2.")
|
|
132
|
+
for class_name in class_names:
|
|
133
|
+
lines.append(f"{class_name}.model_rebuild()")
|
|
134
|
+
return "\n".join(lines) + "\n"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _to_class_name(name: str) -> str:
|
|
138
|
+
return pascal_identifier(name, prefix="Generated")
|