2121import dev .cel .common .types .SimpleType ;
2222import dev .cel .common .types .StructTypeReference ;
2323import dev .cel .expr .ai .Agent ;
24- import dev .cel .expr .ai .AgentContext ; // New Import
24+ import dev .cel .expr .ai .AgentContext ;
2525import dev .cel .expr .ai .AgentMessage ;
2626import dev .cel .expr .ai .Finding ;
2727import dev .cel .expr .ai .Tool ;
2828import dev .cel .expr .ai .ToolAnnotations ;
2929import dev .cel .expr .ai .ToolCall ;
30- import dev .cel .expr .ai .TrustLevel ; // New Import
30+ import dev .cel .expr .ai .TrustLevel ;
3131import dev .cel .parser .CelStandardMacro ;
3232import dev .cel .policy .testing .PolicyTestSuiteHelper ;
3333import dev .cel .policy .testing .PolicyTestSuiteHelper .PolicyTestSuite ;
3434import dev .cel .policy .testing .PolicyTestSuiteHelper .PolicyTestSuite .PolicyTestSection ;
3535import dev .cel .policy .testing .PolicyTestSuiteHelper .PolicyTestSuite .PolicyTestSection .PolicyTestCase ;
3636import dev .cel .runtime .CelEvaluationException ;
3737import dev .cel .runtime .CelFunctionBinding ;
38+ import dev .cel .runtime .CelLateFunctionBindings ;
3839import java .io .IOException ;
3940import java .net .URL ;
41+ import java .time .Instant ;
4042import java .util .List ;
43+ import java .util .stream .Collectors ;
4144import org .junit .Rule ;
4245import org .junit .Test ;
4346import org .junit .runner .RunWith ;
@@ -61,10 +64,12 @@ public class AgenticPolicyCompilerTest {
6164
6265 .addVar ("agent.input" , StructTypeReference .create ("cel.expr.ai.AgentMessage" ))
6366 .addVar ("agent.context" , StructTypeReference .create ("cel.expr.ai.AgentContext" ))
67+ .addVar ("_test_history" , ListType .create (StructTypeReference .create ("cel.expr.ai.AgentMessage" )))
68+ .addVar ("now" , SimpleType .TIMESTAMP )
69+
6470 .addVar ("tool.name" , SimpleType .STRING )
6571 .addVar ("tool.annotations" , StructTypeReference .create ("cel.expr.ai.ToolAnnotations" ))
6672 .addVar ("tool.call" , StructTypeReference .create ("cel.expr.ai.ToolCall" ))
67-
6873 .addFunctionDeclarations (
6974 newFunctionDeclaration (
7075 "ai.finding" ,
@@ -100,6 +105,31 @@ public class AgenticPolicyCompilerTest {
100105 ListType .create (StructTypeReference .create ("cel.expr.ai.Finding" )),
101106 ListType .create (StructTypeReference .create ("cel.expr.ai.Finding" ))
102107 )
108+ ),
109+ newFunctionDeclaration (
110+ "agent.history" ,
111+ newGlobalOverload (
112+ "agent_history" ,
113+ ListType .create (StructTypeReference .create ("cel.expr.ai.AgentMessage" ))
114+ )
115+ ),
116+ newFunctionDeclaration (
117+ "role" ,
118+ newMemberOverload (
119+ "list_agent_message_role_string" ,
120+ ListType .create (StructTypeReference .create ("cel.expr.ai.AgentMessage" )),
121+ ListType .create (StructTypeReference .create ("cel.expr.ai.AgentMessage" )),
122+ SimpleType .STRING
123+ )
124+ ),
125+ newFunctionDeclaration (
126+ "after" ,
127+ newMemberOverload (
128+ "list_agent_message_after_timestamp" ,
129+ ListType .create (StructTypeReference .create ("cel.expr.ai.AgentMessage" )),
130+ ListType .create (StructTypeReference .create ("cel.expr.ai.AgentMessage" )),
131+ SimpleType .TIMESTAMP
132+ )
103133 )
104134 )
105135 .addFunctionBindings (
@@ -151,14 +181,40 @@ public class AgenticPolicyCompilerTest {
151181 (args ) -> {
152182 List <Finding > actualFindings = (List <Finding >) args [0 ];
153183 List <Finding > expectedFindings = (List <Finding >) args [1 ];
154-
155184 return expectedFindings .stream ().anyMatch (expected ->
156185 actualFindings .stream ().anyMatch (actual ->
157186 actual .getValue ().equals (expected .getValue ()) &&
158187 actual .getConfidence () >= expected .getConfidence ()
159188 )
160189 );
161190 }
191+ ),
192+ CelFunctionBinding .from (
193+ "list_agent_message_role_string" ,
194+ ImmutableList .of (List .class , String .class ),
195+ (args ) -> {
196+ List <AgentMessage > history = (List <AgentMessage >) args [0 ];
197+ String role = (String ) args [1 ];
198+ return history .stream ()
199+ .filter (m -> m .getRole ().equals (role ))
200+ .collect (Collectors .toList ());
201+ }
202+ ),
203+ CelFunctionBinding .from (
204+ "list_agent_message_after_timestamp" ,
205+ ImmutableList .of (List .class , Instant .class ),
206+ (args ) -> {
207+ List <AgentMessage > history = (List <AgentMessage >) args [0 ];
208+ Instant cutoff = (Instant ) args [1 ];
209+
210+ return history .stream ()
211+ .filter (m -> {
212+ com .google .protobuf .Timestamp protoTs = m .getTime ();
213+ Instant msgTime = Instant .ofEpochSecond (protoTs .getSeconds (), protoTs .getNanos ());
214+ return msgTime .compareTo (cutoff ) >= 0 ;
215+ })
216+ .collect (Collectors .toList ());
217+ }
162218 )
163219 )
164220 .build ();
@@ -188,6 +244,10 @@ private enum AgenticPolicyTestCase {
188244 TRUST_CASCADING (
189245 "trust_cascading.celpolicy" ,
190246 "trust_cascading_tests.yaml"
247+ ),
248+ TIME_BOUND_APPROVAL (
249+ "time_bound_approval.celpolicy" ,
250+ "time_bound_approval_tests.yaml"
191251 );
192252
193253 private final String policyFilePath ;
@@ -217,7 +277,21 @@ private void runTests(Cel cel, CelAbstractSyntaxTree ast, PolicyTestSuite testSu
217277 "%s: %s" , testSection .getName (), testCase .getName ());
218278 try {
219279 ImmutableMap <String , Object > inputMap = testCase .toInputMap (cel );
220- Object evalResult = cel .createProgram (ast ).eval (inputMap );
280+
281+ List <AgentMessage > history =
282+ inputMap .containsKey ("_test_history" )
283+ ? (List <AgentMessage >) inputMap .get ("_test_history" )
284+ : ImmutableList .of ();
285+
286+ CelLateFunctionBindings bindings = CelLateFunctionBindings .from (
287+ CelFunctionBinding .from (
288+ "agent_history" ,
289+ ImmutableList .of (), // No args
290+ (args ) -> history
291+ )
292+ );
293+
294+ Object evalResult = cel .createProgram (ast ).eval (inputMap , bindings );
221295 Object expectedOutput = cel .createProgram (cel .compile (testCase .getOutput ()).getAst ()).eval ();
222296 expect .withMessage (testName ).that (evalResult ).isEqualTo (expectedOutput );
223297 } catch (CelValidationException e ) {
@@ -228,4 +302,4 @@ private void runTests(Cel cel, CelAbstractSyntaxTree ast, PolicyTestSuite testSu
228302 }
229303 }
230304 }
231- }
305+ }
0 commit comments