codecraftai-agent-kit 1.3.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.
- ccai/agentkit/__init__.py +304 -0
- ccai/agentkit/addressresolver.py +177 -0
- ccai/agentkit/agent.py +2945 -0
- ccai/agentkit/agentaddress.py +187 -0
- ccai/agentkit/agentconfiguration.py +581 -0
- ccai/agentkit/agentidentity.py +152 -0
- ccai/agentkit/agentregistry.py +221 -0
- ccai/agentkit/agentstatemachine.py +134 -0
- ccai/agentkit/alternateproviderstrategy.py +36 -0
- ccai/agentkit/approvalgate.py +238 -0
- ccai/agentkit/approvalrequest.py +250 -0
- ccai/agentkit/asyncagent.py +187 -0
- ccai/agentkit/breakpoint.py +150 -0
- ccai/agentkit/builtinagentmetrics.py +114 -0
- ccai/agentkit/builtins/__init__.py +11 -0
- ccai/agentkit/builtins/filereadtool.py +90 -0
- ccai/agentkit/builtins/filewritetool.py +93 -0
- ccai/agentkit/builtins/httprequesttool.py +169 -0
- ccai/agentkit/builtins/shellcommandtool.py +190 -0
- ccai/agentkit/cachedresultstrategy.py +43 -0
- ccai/agentkit/checkpointmanager.py +219 -0
- ccai/agentkit/circuitbreaker.py +230 -0
- ccai/agentkit/circuitbreakerconfiguration.py +158 -0
- ccai/agentkit/circuitbreakerevent.py +158 -0
- ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
- ccai/agentkit/circuitbreakerregistry.py +116 -0
- ccai/agentkit/circuitbreakerstate.py +44 -0
- ccai/agentkit/clearcacheaction.py +46 -0
- ccai/agentkit/concurrencybridge.py +143 -0
- ccai/agentkit/concurrencymodel.py +19 -0
- ccai/agentkit/conditionevaluator.py +594 -0
- ccai/agentkit/configurationchangeevent.py +179 -0
- ccai/agentkit/configurationerrors.py +152 -0
- ccai/agentkit/configurationlayer.py +160 -0
- ccai/agentkit/configurationlayersource.py +46 -0
- ccai/agentkit/configurationmanager.py +680 -0
- ccai/agentkit/configurationschema.py +224 -0
- ccai/agentkit/configurationschemafield.py +217 -0
- ccai/agentkit/configurationserializer.py +163 -0
- ccai/agentkit/configurationvalidationresult.py +118 -0
- ccai/agentkit/contextsummarizer.py +170 -0
- ccai/agentkit/contextwindow.py +264 -0
- ccai/agentkit/contextwindowstrategy.py +57 -0
- ccai/agentkit/correlationcontext.py +156 -0
- ccai/agentkit/costestimate.py +139 -0
- ccai/agentkit/costtracker.py +121 -0
- ccai/agentkit/deadletterentry.py +134 -0
- ccai/agentkit/deadletterquery.py +119 -0
- ccai/agentkit/deadletterqueue.py +123 -0
- ccai/agentkit/deadletterstore.py +86 -0
- ccai/agentkit/debuginspector.py +211 -0
- ccai/agentkit/debugsession.py +223 -0
- ccai/agentkit/defaultvaluestrategy.py +43 -0
- ccai/agentkit/degradationchangeevent.py +120 -0
- ccai/agentkit/degradationlevel.py +44 -0
- ccai/agentkit/degradationmanager.py +212 -0
- ccai/agentkit/degradationpolicy.py +130 -0
- ccai/agentkit/degradationstrategy.py +66 -0
- ccai/agentkit/embeddingprovider.py +106 -0
- ccai/agentkit/errors.py +95 -0
- ccai/agentkit/executionrecord.py +213 -0
- ccai/agentkit/executionrecorder.py +180 -0
- ccai/agentkit/executionrecording.py +230 -0
- ccai/agentkit/executionreplayer.py +187 -0
- ccai/agentkit/expirationpolicy.py +116 -0
- ccai/agentkit/fallbackchain.py +82 -0
- ccai/agentkit/featureflag.py +154 -0
- ccai/agentkit/featureflagevaluator.py +109 -0
- ccai/agentkit/featureflagregistry.py +258 -0
- ccai/agentkit/filedeadletterstore.py +112 -0
- ccai/agentkit/filememorystore.py +291 -0
- ccai/agentkit/healthcheck.py +46 -0
- ccai/agentkit/healthcheckregistry.py +51 -0
- ccai/agentkit/healthmonitor.py +258 -0
- ccai/agentkit/healthstatus.py +105 -0
- ccai/agentkit/hotreloadmonitor.py +214 -0
- ccai/agentkit/hotreloadpolicy.py +160 -0
- ccai/agentkit/inmemorydeadletterstore.py +61 -0
- ccai/agentkit/inmemorystore.py +173 -0
- ccai/agentkit/jsonmessageserializer.py +89 -0
- ccai/agentkit/jsonoutputparser.py +50 -0
- ccai/agentkit/lifecyclestate.py +22 -0
- ccai/agentkit/llmconversation.py +153 -0
- ccai/agentkit/llmerrors.py +193 -0
- ccai/agentkit/llmmessage.py +158 -0
- ccai/agentkit/llmprovider.py +158 -0
- ccai/agentkit/llmproviderregistry.py +200 -0
- ccai/agentkit/llmresponse.py +190 -0
- ccai/agentkit/llmstreamchunk.py +65 -0
- ccai/agentkit/llmstreamhandler.py +48 -0
- ccai/agentkit/llmtoolbridge.py +154 -0
- ccai/agentkit/logexporter.py +316 -0
- ccai/agentkit/logformatter.py +169 -0
- ccai/agentkit/memoryconfiguration.py +243 -0
- ccai/agentkit/memoryentry.py +254 -0
- ccai/agentkit/memoryerrors.py +135 -0
- ccai/agentkit/memoryindex.py +213 -0
- ccai/agentkit/memoryquery.py +248 -0
- ccai/agentkit/memoryqueryresult.py +86 -0
- ccai/agentkit/memoryscope.py +54 -0
- ccai/agentkit/memorystore.py +260 -0
- ccai/agentkit/memorystorehealthcheck.py +33 -0
- ccai/agentkit/memorystoreregistry.py +106 -0
- ccai/agentkit/message.py +310 -0
- ccai/agentkit/messageenvelope.py +144 -0
- ccai/agentkit/messagepattern.py +50 -0
- ccai/agentkit/messagerouter.py +290 -0
- ccai/agentkit/messageserializer.py +106 -0
- ccai/agentkit/messageserializerregistry.py +163 -0
- ccai/agentkit/messagingconfiguration.py +397 -0
- ccai/agentkit/messagingerrors.py +338 -0
- ccai/agentkit/metricdefinition.py +164 -0
- ccai/agentkit/metricmeasurement.py +178 -0
- ccai/agentkit/metricscollector.py +258 -0
- ccai/agentkit/metricsexporter.py +303 -0
- ccai/agentkit/metricsregistry.py +130 -0
- ccai/agentkit/metricssnapshot.py +155 -0
- ccai/agentkit/metrictype.py +51 -0
- ccai/agentkit/msgpackmessageserializer.py +97 -0
- ccai/agentkit/observabilityconfiguration.py +546 -0
- ccai/agentkit/observabilityerror.py +67 -0
- ccai/agentkit/observabilitypipeline.py +252 -0
- ccai/agentkit/outputparser.py +82 -0
- ccai/agentkit/outputschema.py +142 -0
- ccai/agentkit/parallelbranch.py +87 -0
- ccai/agentkit/platform/__init__.py +43 -0
- ccai/agentkit/platform/adapters/__init__.py +18 -0
- ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
- ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
- ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
- ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
- ccai/agentkit/platform/adapters/platformadapter.py +139 -0
- ccai/agentkit/platform/deployment/__init__.py +15 -0
- ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
- ccai/agentkit/platform/errors.py +224 -0
- ccai/agentkit/platform/platformconfiguration.py +412 -0
- ccai/agentkit/platform/platformdetector.py +125 -0
- ccai/agentkit/platform/platformruntime.py +331 -0
- ccai/agentkit/platform/platformserviceregistry.py +149 -0
- ccai/agentkit/platform/platformtype.py +60 -0
- ccai/agentkit/platform/services/__init__.py +19 -0
- ccai/agentkit/platform/services/computeservice.py +112 -0
- ccai/agentkit/platform/services/invocationresult.py +173 -0
- ccai/agentkit/platform/services/messagingservice.py +135 -0
- ccai/agentkit/platform/services/platformservice.py +111 -0
- ccai/agentkit/platform/services/queuemessage.py +193 -0
- ccai/agentkit/platform/services/storageobject.py +195 -0
- ccai/agentkit/platform/services/storageservice.py +159 -0
- ccai/agentkit/pluginconfiguration.py +241 -0
- ccai/agentkit/plugindependency.py +129 -0
- ccai/agentkit/plugindescriptor.py +225 -0
- ccai/agentkit/pluginlifecyclehook.py +41 -0
- ccai/agentkit/pluginmanager.py +360 -0
- ccai/agentkit/pluginstatus.py +47 -0
- ccai/agentkit/pooledagent.py +116 -0
- ccai/agentkit/prompttemplate.py +221 -0
- ccai/agentkit/prompttemplateregistry.py +145 -0
- ccai/agentkit/protobufmessageserializer.py +144 -0
- ccai/agentkit/pubsubbroker.py +311 -0
- ccai/agentkit/py.typed +0 -0
- ccai/agentkit/reconnecttransportaction.py +47 -0
- ccai/agentkit/regexoutputparser.py +68 -0
- ccai/agentkit/requestresponsehandler.py +274 -0
- ccai/agentkit/resetcircuitbreakeraction.py +48 -0
- ccai/agentkit/resilienceconfiguration.py +191 -0
- ccai/agentkit/resilienceerrors.py +103 -0
- ccai/agentkit/resiliencepipeline.py +201 -0
- ccai/agentkit/retryattempt.py +158 -0
- ccai/agentkit/retryexecutor.py +157 -0
- ccai/agentkit/retrypolicy.py +195 -0
- ccai/agentkit/retryresult.py +186 -0
- ccai/agentkit/selfhealingaction.py +42 -0
- ccai/agentkit/selfhealingpolicy.py +67 -0
- ccai/agentkit/selfhealingresult.py +130 -0
- ccai/agentkit/stepexecutionstatus.py +52 -0
- ccai/agentkit/stepresult.py +198 -0
- ccai/agentkit/steps/__init__.py +9 -0
- ccai/agentkit/steps/agentstep.py +247 -0
- ccai/agentkit/steps/approvalstep.py +276 -0
- ccai/agentkit/steps/llmstep.py +403 -0
- ccai/agentkit/steps/subworkflowstep.py +270 -0
- ccai/agentkit/steps/toolstep.py +265 -0
- ccai/agentkit/structuredlogentry.py +222 -0
- ccai/agentkit/structuredlogger.py +189 -0
- ccai/agentkit/subscription.py +141 -0
- ccai/agentkit/threadedagent.py +94 -0
- ccai/agentkit/tokenusage.py +136 -0
- ccai/agentkit/tool.py +203 -0
- ccai/agentkit/toolchain.py +268 -0
- ccai/agentkit/toolchainexecutor.py +134 -0
- ccai/agentkit/toolchainresult.py +166 -0
- ccai/agentkit/tooldescriptor.py +203 -0
- ccai/agentkit/toolerrors.py +160 -0
- ccai/agentkit/toolexecutor.py +168 -0
- ccai/agentkit/toolfactory.py +60 -0
- ccai/agentkit/toolregistry.py +184 -0
- ccai/agentkit/toolresult.py +142 -0
- ccai/agentkit/toolschema.py +197 -0
- ccai/agentkit/tracecontext.py +294 -0
- ccai/agentkit/traceevent.py +166 -0
- ccai/agentkit/traceexporter.py +305 -0
- ccai/agentkit/tracer.py +234 -0
- ccai/agentkit/tracespan.py +349 -0
- ccai/agentkit/transitionrecord.py +84 -0
- ccai/agentkit/transport.py +334 -0
- ccai/agentkit/transportconfiguration.py +436 -0
- ccai/agentkit/transporthealthcheck.py +36 -0
- ccai/agentkit/transportregistry.py +224 -0
- ccai/agentkit/transports/__init__.py +28 -0
- ccai/agentkit/transports/grpctransport.py +410 -0
- ccai/agentkit/transports/httptransport.py +423 -0
- ccai/agentkit/transports/messagequeuetransport.py +489 -0
- ccai/agentkit/transports/websockettransport.py +473 -0
- ccai/agentkit/workflowcheckpoint.py +208 -0
- ccai/agentkit/workflowconfiguration.py +295 -0
- ccai/agentkit/workflowcontext.py +249 -0
- ccai/agentkit/workflowdefinition.py +790 -0
- ccai/agentkit/workflowedge.py +128 -0
- ccai/agentkit/workflowerrors.py +295 -0
- ccai/agentkit/workflowexecutionstatus.py +48 -0
- ccai/agentkit/workflowexecutor.py +1121 -0
- ccai/agentkit/workflowregistry.py +185 -0
- ccai/agentkit/workflowresult.py +250 -0
- ccai/agentkit/workflowstep.py +195 -0
- codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
- codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
- codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
- codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ConcurrencyModel enum representing the supported
|
|
3
|
+
concurrency models for agents in the codecraftai-agent-kit framework.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import enum
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ConcurrencyModel(enum.Enum):
|
|
10
|
+
"""
|
|
11
|
+
Enum representing the concurrency models available for agents.
|
|
12
|
+
|
|
13
|
+
Agents can run using different concurrency strategies depending
|
|
14
|
+
on their workload and integration requirements.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
ASYNCIO = "ASYNCIO"
|
|
18
|
+
THREAD = "THREAD"
|
|
19
|
+
THREAD_POOL = "THREAD_POOL"
|
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ConditionEvaluator class for safe evaluation of
|
|
3
|
+
condition expression strings against a WorkflowContext.
|
|
4
|
+
|
|
5
|
+
The ConditionEvaluator parses expressions using the Python ast
|
|
6
|
+
module and validates that only allowed constructs are present
|
|
7
|
+
before evaluating. Supported constructs include step output
|
|
8
|
+
references (steps.<step_id>.<field>), workflow input references
|
|
9
|
+
(inputs.<field>), comparison operators, logical operators, and
|
|
10
|
+
the membership operator.
|
|
11
|
+
|
|
12
|
+
Any expression containing disallowed constructs such as function
|
|
13
|
+
calls, import statements, or arbitrary attribute access raises a
|
|
14
|
+
WorkflowDefinitionError. References to steps that have not been
|
|
15
|
+
executed raise a WorkflowExecutionError.
|
|
16
|
+
|
|
17
|
+
Requirements: 3.3-3.12
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import ast
|
|
21
|
+
import operator
|
|
22
|
+
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
from ccai.agentkit.workflowcontext import WorkflowContext
|
|
26
|
+
from ccai.agentkit.workflowerrors import WorkflowDefinitionError
|
|
27
|
+
from ccai.agentkit.workflowerrors import WorkflowExecutionError
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# Mapping of AST comparison operator types to Python operator functions.
|
|
31
|
+
_COMPARISON_OPS = {
|
|
32
|
+
ast.Eq: operator.eq,
|
|
33
|
+
ast.NotEq: operator.ne,
|
|
34
|
+
ast.Lt: operator.lt,
|
|
35
|
+
ast.Gt: operator.gt,
|
|
36
|
+
ast.LtE: operator.le,
|
|
37
|
+
ast.GtE: operator.ge,
|
|
38
|
+
ast.In: lambda left, right: left in right,
|
|
39
|
+
ast.NotIn: lambda left, right: left not in right,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
# Mapping of AST unary operator types to Python unary functions.
|
|
43
|
+
_UNARY_OPS = {
|
|
44
|
+
ast.Not: operator.not_,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
# The set of AST node types that are allowed in condition expressions.
|
|
48
|
+
_ALLOWED_NODE_TYPES = frozenset({
|
|
49
|
+
ast.Expression,
|
|
50
|
+
ast.BoolOp,
|
|
51
|
+
ast.And,
|
|
52
|
+
ast.Or,
|
|
53
|
+
ast.UnaryOp,
|
|
54
|
+
ast.Not,
|
|
55
|
+
ast.Compare,
|
|
56
|
+
ast.Eq,
|
|
57
|
+
ast.NotEq,
|
|
58
|
+
ast.Lt,
|
|
59
|
+
ast.Gt,
|
|
60
|
+
ast.LtE,
|
|
61
|
+
ast.GtE,
|
|
62
|
+
ast.In,
|
|
63
|
+
ast.NotIn,
|
|
64
|
+
ast.Attribute,
|
|
65
|
+
ast.Name,
|
|
66
|
+
ast.Constant,
|
|
67
|
+
ast.Load,
|
|
68
|
+
ast.List,
|
|
69
|
+
ast.Tuple,
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class ConditionEvaluator:
|
|
75
|
+
"""
|
|
76
|
+
Evaluates ConditionExpression strings against a WorkflowContext.
|
|
77
|
+
|
|
78
|
+
The evaluator restricts evaluation to a safe subset of Python
|
|
79
|
+
expressions. It parses the expression into an AST, validates
|
|
80
|
+
that only allowed node types are present, resolves step output
|
|
81
|
+
and workflow input references from the context, and evaluates
|
|
82
|
+
the expression to produce a boolean result.
|
|
83
|
+
|
|
84
|
+
Supported constructs:
|
|
85
|
+
|
|
86
|
+
- Step output references: ``steps.<step_id>.<output_field>``
|
|
87
|
+
- Workflow input references: ``inputs.<field_name>``
|
|
88
|
+
- Comparison operators: ``==``, ``!=``, ``<``, ``>``, ``<=``, ``>=``
|
|
89
|
+
- Logical operators: ``and``, ``or``, ``not``
|
|
90
|
+
- Membership operator: ``in``
|
|
91
|
+
- Literal values: strings, integers, floats, booleans, None
|
|
92
|
+
- List and tuple literals containing allowed values
|
|
93
|
+
|
|
94
|
+
Disallowed constructs include function calls, import statements,
|
|
95
|
+
subscript access, assignment, augmented assignment, and attribute
|
|
96
|
+
access beyond the allowed patterns.
|
|
97
|
+
|
|
98
|
+
:raises WorkflowDefinitionError: When the expression contains
|
|
99
|
+
invalid syntax or disallowed constructs.
|
|
100
|
+
:raises WorkflowExecutionError: When the expression references
|
|
101
|
+
a step that has not been executed.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
ALLOWED_OPERATORS = {
|
|
105
|
+
"==", "!=", "<", ">", "<=", ">=",
|
|
106
|
+
"and", "or", "not", "in"
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
def evaluate(self, expression: str, context: WorkflowContext) -> bool:
|
|
110
|
+
"""
|
|
111
|
+
Evaluates a condition expression against the workflow context.
|
|
112
|
+
|
|
113
|
+
Parses the expression string into an AST, validates that
|
|
114
|
+
only allowed constructs are present, resolves step output
|
|
115
|
+
and workflow input references from the context, and
|
|
116
|
+
evaluates the expression to produce a boolean result.
|
|
117
|
+
|
|
118
|
+
:param expression: The condition expression string to
|
|
119
|
+
evaluate.
|
|
120
|
+
:param context: The WorkflowContext containing inputs
|
|
121
|
+
and step outputs to resolve references against.
|
|
122
|
+
|
|
123
|
+
:returns: True when the condition is satisfied, False
|
|
124
|
+
otherwise.
|
|
125
|
+
|
|
126
|
+
:raises WorkflowDefinitionError: When the expression
|
|
127
|
+
contains invalid syntax or disallowed constructs.
|
|
128
|
+
:raises WorkflowExecutionError: When the expression
|
|
129
|
+
references a step identifier that has not been
|
|
130
|
+
executed.
|
|
131
|
+
"""
|
|
132
|
+
tree = self._parse_expression(expression)
|
|
133
|
+
self._validate_ast(tree, expression)
|
|
134
|
+
result = self._evaluate_node(tree.body, context, expression)
|
|
135
|
+
return bool(result)
|
|
136
|
+
|
|
137
|
+
def _parse_expression(self, expression: str) -> ast.Expression:
|
|
138
|
+
"""
|
|
139
|
+
Parses the expression string into an AST Expression node.
|
|
140
|
+
|
|
141
|
+
:param expression: The condition expression string.
|
|
142
|
+
|
|
143
|
+
:returns: An ast.Expression node representing the parsed
|
|
144
|
+
expression.
|
|
145
|
+
|
|
146
|
+
:raises WorkflowDefinitionError: When the expression
|
|
147
|
+
contains invalid Python syntax.
|
|
148
|
+
"""
|
|
149
|
+
try:
|
|
150
|
+
tree = ast.parse(expression, mode="eval")
|
|
151
|
+
except SyntaxError as syn_err:
|
|
152
|
+
detail = "Invalid expression syntax '{}': {}".format(
|
|
153
|
+
expression, syn_err.msg
|
|
154
|
+
)
|
|
155
|
+
raise WorkflowDefinitionError(
|
|
156
|
+
workflow_name="(condition)", detail=detail
|
|
157
|
+
) from syn_err
|
|
158
|
+
|
|
159
|
+
return tree
|
|
160
|
+
|
|
161
|
+
def _validate_ast(self, tree: ast.Expression,
|
|
162
|
+
expression: str) -> None:
|
|
163
|
+
"""
|
|
164
|
+
Walks the AST and validates that every node is an allowed
|
|
165
|
+
type. Raises WorkflowDefinitionError when a disallowed
|
|
166
|
+
construct is found.
|
|
167
|
+
|
|
168
|
+
:param tree: The parsed AST Expression node.
|
|
169
|
+
:param expression: The original expression string for
|
|
170
|
+
error reporting.
|
|
171
|
+
|
|
172
|
+
:raises WorkflowDefinitionError: When the AST contains
|
|
173
|
+
disallowed node types.
|
|
174
|
+
"""
|
|
175
|
+
for node in ast.walk(tree):
|
|
176
|
+
node_type = type(node)
|
|
177
|
+
|
|
178
|
+
if node_type not in _ALLOWED_NODE_TYPES:
|
|
179
|
+
detail = (
|
|
180
|
+
"Expression '{}' contains disallowed construct: "
|
|
181
|
+
"{}".format(expression, node_type.__name__)
|
|
182
|
+
)
|
|
183
|
+
raise WorkflowDefinitionError(
|
|
184
|
+
workflow_name="(condition)", detail=detail
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
# Validate that Attribute nodes only access allowed
|
|
188
|
+
# patterns: steps.<id>.<field> or inputs.<field>.
|
|
189
|
+
if node_type is ast.Attribute:
|
|
190
|
+
self._validate_attribute_access(node, expression)
|
|
191
|
+
|
|
192
|
+
return
|
|
193
|
+
|
|
194
|
+
def _validate_attribute_access(self, node: ast.Attribute,
|
|
195
|
+
expression: str) -> None:
|
|
196
|
+
"""
|
|
197
|
+
Validates that an Attribute node conforms to one of the
|
|
198
|
+
allowed access patterns:
|
|
199
|
+
|
|
200
|
+
- ``steps.<step_id>.<output_field>`` (three-level)
|
|
201
|
+
- ``inputs.<field_name>`` (two-level)
|
|
202
|
+
- ``steps.<step_id>`` (intermediate two-level for the
|
|
203
|
+
three-level pattern)
|
|
204
|
+
|
|
205
|
+
:param node: The ast.Attribute node to validate.
|
|
206
|
+
:param expression: The original expression string for
|
|
207
|
+
error reporting.
|
|
208
|
+
|
|
209
|
+
:raises WorkflowDefinitionError: When the attribute
|
|
210
|
+
access does not match an allowed pattern.
|
|
211
|
+
"""
|
|
212
|
+
chain = self._resolve_attribute_chain(node)
|
|
213
|
+
|
|
214
|
+
if chain is None:
|
|
215
|
+
detail = (
|
|
216
|
+
"Expression '{}' contains disallowed attribute "
|
|
217
|
+
"access".format(expression)
|
|
218
|
+
)
|
|
219
|
+
raise WorkflowDefinitionError(
|
|
220
|
+
workflow_name="(condition)", detail=detail
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
root_name = chain[0]
|
|
224
|
+
depth = len(chain)
|
|
225
|
+
|
|
226
|
+
if root_name == "inputs" and depth == 2:
|
|
227
|
+
# inputs.<field_name> — valid two-level access, but
|
|
228
|
+
# reject dunder attributes.
|
|
229
|
+
field_name = chain[1]
|
|
230
|
+
if field_name.startswith("__") and field_name.endswith("__"):
|
|
231
|
+
detail = (
|
|
232
|
+
"Expression '{}' contains disallowed dunder "
|
|
233
|
+
"attribute access: {}".format(
|
|
234
|
+
expression, ".".join(chain)
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
raise WorkflowDefinitionError(
|
|
238
|
+
workflow_name="(condition)", detail=detail
|
|
239
|
+
)
|
|
240
|
+
return
|
|
241
|
+
|
|
242
|
+
if root_name == "steps" and depth in (2, 3):
|
|
243
|
+
# steps.<step_id> or steps.<step_id>.<field> — valid,
|
|
244
|
+
# but reject dunder attributes which could access
|
|
245
|
+
# internal Python machinery.
|
|
246
|
+
for part in chain[1:]:
|
|
247
|
+
if part.startswith("__") and part.endswith("__"):
|
|
248
|
+
detail = (
|
|
249
|
+
"Expression '{}' contains disallowed dunder "
|
|
250
|
+
"attribute access: {}".format(
|
|
251
|
+
expression, ".".join(chain)
|
|
252
|
+
)
|
|
253
|
+
)
|
|
254
|
+
raise WorkflowDefinitionError(
|
|
255
|
+
workflow_name="(condition)", detail=detail
|
|
256
|
+
)
|
|
257
|
+
return
|
|
258
|
+
|
|
259
|
+
detail = (
|
|
260
|
+
"Expression '{}' contains disallowed attribute access "
|
|
261
|
+
"pattern: {}".format(expression, ".".join(chain))
|
|
262
|
+
)
|
|
263
|
+
raise WorkflowDefinitionError(
|
|
264
|
+
workflow_name="(condition)", detail=detail
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
def _resolve_attribute_chain(self, node: ast.Attribute):
|
|
268
|
+
"""
|
|
269
|
+
Resolves an Attribute node into a list of name strings
|
|
270
|
+
representing the dotted access chain.
|
|
271
|
+
|
|
272
|
+
For example, ``steps.step_a.result`` resolves to
|
|
273
|
+
``["steps", "step_a", "result"]``.
|
|
274
|
+
|
|
275
|
+
:param node: The ast.Attribute node to resolve.
|
|
276
|
+
|
|
277
|
+
:returns: A list of name strings, or None when the chain
|
|
278
|
+
contains non-Name/Attribute nodes.
|
|
279
|
+
"""
|
|
280
|
+
parts = []
|
|
281
|
+
current = node
|
|
282
|
+
|
|
283
|
+
while isinstance(current, ast.Attribute):
|
|
284
|
+
parts.append(current.attr)
|
|
285
|
+
current = current.value
|
|
286
|
+
|
|
287
|
+
if isinstance(current, ast.Name):
|
|
288
|
+
parts.append(current.id)
|
|
289
|
+
parts.reverse()
|
|
290
|
+
return parts
|
|
291
|
+
|
|
292
|
+
return None
|
|
293
|
+
|
|
294
|
+
def _evaluate_node(self, node: ast.AST,
|
|
295
|
+
context: WorkflowContext,
|
|
296
|
+
expression: str) -> Any:
|
|
297
|
+
"""
|
|
298
|
+
Recursively evaluates an AST node against the workflow
|
|
299
|
+
context.
|
|
300
|
+
|
|
301
|
+
:param node: The AST node to evaluate.
|
|
302
|
+
:param context: The WorkflowContext for resolving
|
|
303
|
+
references.
|
|
304
|
+
:param expression: The original expression string for
|
|
305
|
+
error reporting.
|
|
306
|
+
|
|
307
|
+
:returns: The evaluated value of the node.
|
|
308
|
+
|
|
309
|
+
:raises WorkflowExecutionError: When a step reference
|
|
310
|
+
cannot be resolved.
|
|
311
|
+
:raises WorkflowDefinitionError: When an unexpected
|
|
312
|
+
node type is encountered.
|
|
313
|
+
"""
|
|
314
|
+
node_type = type(node)
|
|
315
|
+
|
|
316
|
+
if node_type is ast.Constant:
|
|
317
|
+
return node.value
|
|
318
|
+
|
|
319
|
+
if node_type is ast.Name:
|
|
320
|
+
return self._resolve_name(node, context, expression)
|
|
321
|
+
|
|
322
|
+
if node_type is ast.Attribute:
|
|
323
|
+
return self._resolve_attribute(node, context, expression)
|
|
324
|
+
|
|
325
|
+
if node_type is ast.Compare:
|
|
326
|
+
return self._evaluate_compare(node, context, expression)
|
|
327
|
+
|
|
328
|
+
if node_type is ast.BoolOp:
|
|
329
|
+
return self._evaluate_boolop(node, context, expression)
|
|
330
|
+
|
|
331
|
+
if node_type is ast.UnaryOp:
|
|
332
|
+
return self._evaluate_unaryop(node, context, expression)
|
|
333
|
+
|
|
334
|
+
if node_type is ast.List:
|
|
335
|
+
return [
|
|
336
|
+
self._evaluate_node(elt, context, expression)
|
|
337
|
+
for elt in node.elts
|
|
338
|
+
]
|
|
339
|
+
|
|
340
|
+
if node_type is ast.Tuple:
|
|
341
|
+
return tuple(
|
|
342
|
+
self._evaluate_node(elt, context, expression)
|
|
343
|
+
for elt in node.elts
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
detail = (
|
|
347
|
+
"Expression '{}' contains unsupported construct: "
|
|
348
|
+
"{}".format(expression, node_type.__name__)
|
|
349
|
+
)
|
|
350
|
+
raise WorkflowDefinitionError(
|
|
351
|
+
workflow_name="(condition)", detail=detail
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
def _resolve_name(self, node: ast.Name,
|
|
355
|
+
context: WorkflowContext,
|
|
356
|
+
expression: str) -> Any:
|
|
357
|
+
"""
|
|
358
|
+
Resolves a bare Name node. Only ``True``, ``False``, and
|
|
359
|
+
``None`` are allowed as standalone names. The names
|
|
360
|
+
``steps`` and ``inputs`` are not valid as standalone
|
|
361
|
+
references — they must be followed by attribute access.
|
|
362
|
+
|
|
363
|
+
:param node: The ast.Name node to resolve.
|
|
364
|
+
:param context: The WorkflowContext (unused for literal
|
|
365
|
+
names).
|
|
366
|
+
:param expression: The original expression string for
|
|
367
|
+
error reporting.
|
|
368
|
+
|
|
369
|
+
:returns: The resolved value.
|
|
370
|
+
|
|
371
|
+
:raises WorkflowDefinitionError: When the name is not
|
|
372
|
+
an allowed literal.
|
|
373
|
+
"""
|
|
374
|
+
name = node.id
|
|
375
|
+
|
|
376
|
+
if name == "True":
|
|
377
|
+
return True
|
|
378
|
+
if name == "False":
|
|
379
|
+
return False
|
|
380
|
+
if name == "None":
|
|
381
|
+
return None
|
|
382
|
+
|
|
383
|
+
detail = (
|
|
384
|
+
"Expression '{}' contains unresolved name: "
|
|
385
|
+
"'{}'".format(expression, name)
|
|
386
|
+
)
|
|
387
|
+
raise WorkflowDefinitionError(
|
|
388
|
+
workflow_name="(condition)", detail=detail
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
def _resolve_attribute(self, node: ast.Attribute,
|
|
392
|
+
context: WorkflowContext,
|
|
393
|
+
expression: str) -> Any:
|
|
394
|
+
"""
|
|
395
|
+
Resolves an Attribute node against the workflow context.
|
|
396
|
+
|
|
397
|
+
Handles the patterns:
|
|
398
|
+
- ``inputs.<field_name>`` — returns the input value.
|
|
399
|
+
- ``steps.<step_id>.<output_field>`` — returns the step
|
|
400
|
+
output field value.
|
|
401
|
+
|
|
402
|
+
:param node: The ast.Attribute node to resolve.
|
|
403
|
+
:param context: The WorkflowContext for resolving
|
|
404
|
+
references.
|
|
405
|
+
:param expression: The original expression string for
|
|
406
|
+
error reporting.
|
|
407
|
+
|
|
408
|
+
:returns: The resolved value from the context.
|
|
409
|
+
|
|
410
|
+
:raises WorkflowExecutionError: When a step reference
|
|
411
|
+
cannot be resolved.
|
|
412
|
+
"""
|
|
413
|
+
chain = self._resolve_attribute_chain(node)
|
|
414
|
+
root_name = chain[0]
|
|
415
|
+
|
|
416
|
+
if root_name == "inputs":
|
|
417
|
+
field_name = chain[1]
|
|
418
|
+
value = context.get_input(field_name)
|
|
419
|
+
return value
|
|
420
|
+
|
|
421
|
+
if root_name == "steps":
|
|
422
|
+
step_id = chain[1]
|
|
423
|
+
step_output = context.get_step_output(step_id)
|
|
424
|
+
|
|
425
|
+
if step_output is None:
|
|
426
|
+
raise WorkflowExecutionError(
|
|
427
|
+
workflow_name="(condition)",
|
|
428
|
+
step_id=step_id,
|
|
429
|
+
cause=ValueError(
|
|
430
|
+
"Step '{}' has not been executed; cannot "
|
|
431
|
+
"resolve reference in expression "
|
|
432
|
+
"'{}'".format(step_id, expression)
|
|
433
|
+
)
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
if len(chain) == 3:
|
|
437
|
+
field_name = chain[2]
|
|
438
|
+
value = step_output.get(field_name)
|
|
439
|
+
return value
|
|
440
|
+
|
|
441
|
+
# Two-level steps.<step_id> returns the full output dict.
|
|
442
|
+
return step_output
|
|
443
|
+
|
|
444
|
+
detail = (
|
|
445
|
+
"Expression '{}' contains unresolved reference: "
|
|
446
|
+
"'{}'".format(expression, ".".join(chain))
|
|
447
|
+
)
|
|
448
|
+
raise WorkflowDefinitionError(
|
|
449
|
+
workflow_name="(condition)", detail=detail
|
|
450
|
+
)
|
|
451
|
+
|
|
452
|
+
def _evaluate_compare(self, node: ast.Compare,
|
|
453
|
+
context: WorkflowContext,
|
|
454
|
+
expression: str) -> bool:
|
|
455
|
+
"""
|
|
456
|
+
Evaluates a Compare node (e.g. ``a == b``, ``a < b < c``,
|
|
457
|
+
``x in [1, 2, 3]``).
|
|
458
|
+
|
|
459
|
+
Supports chained comparisons by evaluating each pair of
|
|
460
|
+
operands with the corresponding operator.
|
|
461
|
+
|
|
462
|
+
:param node: The ast.Compare node to evaluate.
|
|
463
|
+
:param context: The WorkflowContext for resolving
|
|
464
|
+
references.
|
|
465
|
+
:param expression: The original expression string for
|
|
466
|
+
error reporting.
|
|
467
|
+
|
|
468
|
+
:returns: True when all comparisons are satisfied.
|
|
469
|
+
"""
|
|
470
|
+
left = self._evaluate_node(node.left, context, expression)
|
|
471
|
+
|
|
472
|
+
for cmp_op, comparator in zip(node.ops, node.comparators):
|
|
473
|
+
right = self._evaluate_node(
|
|
474
|
+
comparator, context, expression
|
|
475
|
+
)
|
|
476
|
+
op_func = _COMPARISON_OPS.get(type(cmp_op))
|
|
477
|
+
|
|
478
|
+
if op_func is None:
|
|
479
|
+
detail = (
|
|
480
|
+
"Expression '{}' contains unsupported comparison "
|
|
481
|
+
"operator: {}".format(
|
|
482
|
+
expression, type(cmp_op).__name__
|
|
483
|
+
)
|
|
484
|
+
)
|
|
485
|
+
raise WorkflowDefinitionError(
|
|
486
|
+
workflow_name="(condition)", detail=detail
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
if not op_func(left, right):
|
|
490
|
+
return False
|
|
491
|
+
|
|
492
|
+
left = right
|
|
493
|
+
|
|
494
|
+
return True
|
|
495
|
+
|
|
496
|
+
def _evaluate_boolop(self, node: ast.BoolOp,
|
|
497
|
+
context: WorkflowContext,
|
|
498
|
+
expression: str) -> bool:
|
|
499
|
+
"""
|
|
500
|
+
Evaluates a BoolOp node (``and`` / ``or``).
|
|
501
|
+
|
|
502
|
+
Uses short-circuit evaluation: for ``and``, returns False
|
|
503
|
+
as soon as a falsy value is found; for ``or``, returns
|
|
504
|
+
True as soon as a truthy value is found.
|
|
505
|
+
|
|
506
|
+
:param node: The ast.BoolOp node to evaluate.
|
|
507
|
+
:param context: The WorkflowContext for resolving
|
|
508
|
+
references.
|
|
509
|
+
:param expression: The original expression string for
|
|
510
|
+
error reporting.
|
|
511
|
+
|
|
512
|
+
:returns: The boolean result of the logical operation.
|
|
513
|
+
"""
|
|
514
|
+
if isinstance(node.op, ast.And):
|
|
515
|
+
for value_node in node.values:
|
|
516
|
+
result = self._evaluate_node(
|
|
517
|
+
value_node, context, expression
|
|
518
|
+
)
|
|
519
|
+
if not result:
|
|
520
|
+
return False
|
|
521
|
+
return True
|
|
522
|
+
|
|
523
|
+
if isinstance(node.op, ast.Or):
|
|
524
|
+
for value_node in node.values:
|
|
525
|
+
result = self._evaluate_node(
|
|
526
|
+
value_node, context, expression
|
|
527
|
+
)
|
|
528
|
+
if result:
|
|
529
|
+
return True
|
|
530
|
+
return False
|
|
531
|
+
|
|
532
|
+
detail = (
|
|
533
|
+
"Expression '{}' contains unsupported boolean "
|
|
534
|
+
"operator: {}".format(
|
|
535
|
+
expression, type(node.op).__name__
|
|
536
|
+
)
|
|
537
|
+
)
|
|
538
|
+
raise WorkflowDefinitionError(
|
|
539
|
+
workflow_name="(condition)", detail=detail
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
def _evaluate_unaryop(self, node: ast.UnaryOp,
|
|
543
|
+
context: WorkflowContext,
|
|
544
|
+
expression: str) -> Any:
|
|
545
|
+
"""
|
|
546
|
+
Evaluates a UnaryOp node (``not``).
|
|
547
|
+
|
|
548
|
+
:param node: The ast.UnaryOp node to evaluate.
|
|
549
|
+
:param context: The WorkflowContext for resolving
|
|
550
|
+
references.
|
|
551
|
+
:param expression: The original expression string for
|
|
552
|
+
error reporting.
|
|
553
|
+
|
|
554
|
+
:returns: The result of applying the unary operator.
|
|
555
|
+
"""
|
|
556
|
+
operand = self._evaluate_node(
|
|
557
|
+
node.operand, context, expression
|
|
558
|
+
)
|
|
559
|
+
op_func = _UNARY_OPS.get(type(node.op))
|
|
560
|
+
|
|
561
|
+
if op_func is None:
|
|
562
|
+
detail = (
|
|
563
|
+
"Expression '{}' contains unsupported unary "
|
|
564
|
+
"operator: {}".format(
|
|
565
|
+
expression, type(node.op).__name__
|
|
566
|
+
)
|
|
567
|
+
)
|
|
568
|
+
raise WorkflowDefinitionError(
|
|
569
|
+
workflow_name="(condition)", detail=detail
|
|
570
|
+
)
|
|
571
|
+
|
|
572
|
+
result = op_func(operand)
|
|
573
|
+
return result
|
|
574
|
+
|
|
575
|
+
def __repr__(self) -> str:
|
|
576
|
+
"""
|
|
577
|
+
Returns a detailed string representation of the
|
|
578
|
+
ConditionEvaluator.
|
|
579
|
+
|
|
580
|
+
:returns: A repr string for debugging.
|
|
581
|
+
"""
|
|
582
|
+
repr_str = "{}()".format(self.__class__.__name__)
|
|
583
|
+
return repr_str
|
|
584
|
+
|
|
585
|
+
def __str__(self) -> str:
|
|
586
|
+
"""
|
|
587
|
+
Returns a human-readable summary of the
|
|
588
|
+
ConditionEvaluator.
|
|
589
|
+
|
|
590
|
+
:returns: A human-readable string describing the
|
|
591
|
+
evaluator.
|
|
592
|
+
"""
|
|
593
|
+
str_val = "ConditionEvaluator: safe expression evaluator"
|
|
594
|
+
return str_val
|