From 94b3a3be8fa3d01019d4d05112d7c63569d6d6f7 Mon Sep 17 00:00:00 2001 From: "detail-app[bot]" <180357370+detail-app[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:42:55 +0000 Subject: [PATCH 1/6] fix: resolve container-qualified bindings for unchecked parsed eval --- src/main/java/dev/trufflecel/EvalContext.java | 41 ++++++-- src/test/java/dev/trufflecel/BUILD.bazel | 13 +++ .../trufflecel/ContainerResolutionTest.java | 96 +++++++++++++++++++ tools/conformance/ConformanceServerTest.java | 47 +++++++++ 4 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 src/test/java/dev/trufflecel/ContainerResolutionTest.java diff --git a/src/main/java/dev/trufflecel/EvalContext.java b/src/main/java/dev/trufflecel/EvalContext.java index 1f82032e..e735255a 100644 --- a/src/main/java/dev/trufflecel/EvalContext.java +++ b/src/main/java/dev/trufflecel/EvalContext.java @@ -247,7 +247,8 @@ Object getRootBoundary(String name) { } Object getRoot(String name) { - return rootBinding(name.startsWith(".") ? absoluteName(name) : name); + boolean absolute = name.startsWith("."); + return rootBinding(absolute ? absoluteName(name) : name, absolute); } @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary @@ -260,7 +261,8 @@ Object getLocal(int slot) { return readStored(((Scoped) this).scopes[slot * 2 + 1]); } - private Object rootBinding(String name) { + @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary + private Object rootBinding(String name, boolean absolute) { Attribute path = evaluation == null || evaluation.partial == null ? null : Attribute.of(name); Object override = attributeRead(path, false); if (override != PartialState.NONE) return override; @@ -270,16 +272,43 @@ private Object rootBinding(String name) { Object binding = arguments[0]; value = binding instanceof String single - ? single.equals(name) ? arguments[1] : null - : lookup(binding, name); + ? matchesRoot(single, name, absolute) ? arguments[1] : null + : resolveRoot(binding, name, absolute); } else if (this instanceof Scoped scoped && scoped.singleName != null) - value = scoped.singleName.equals(name) ? activation : null; - else value = lookup(activation, name); + value = matchesRoot(scoped.singleName, name, absolute) ? activation : null; + else value = resolveRoot(activation, name, absolute); if (value != null) return value; if (path != null && evaluation.partial.containsUnknown(path)) return PartialState.MISSING; throw undeclared(name); } + @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary + private Object resolveRoot(Object activation, String name, boolean absolute) { + if (!absolute) { + String prefix = environment.container; + while (!prefix.isEmpty()) { + Object value = lookup(activation, prefix + "." + name); + if (value != null) return value; + int dot = prefix.lastIndexOf('.'); + prefix = dot < 0 ? "" : prefix.substring(0, dot); + } + } + return lookup(activation, name); + } + + @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary + private boolean matchesRoot(String binding, String name, boolean absolute) { + if (!absolute) { + String prefix = environment.container; + while (!prefix.isEmpty()) { + if (binding.equals(prefix + "." + name)) return true; + int dot = prefix.lastIndexOf('.'); + prefix = dot < 0 ? "" : prefix.substring(0, dot); + } + } + return binding.equals(name); + } + @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary private static CelException undeclared(String name) { return Values.error(CelException.Code.UNDECLARED, "no value for variable: " + name); diff --git a/src/test/java/dev/trufflecel/BUILD.bazel b/src/test/java/dev/trufflecel/BUILD.bazel index 464e4d09..04b849a3 100644 --- a/src/test/java/dev/trufflecel/BUILD.bazel +++ b/src/test/java/dev/trufflecel/BUILD.bazel @@ -219,6 +219,19 @@ java_test( ], ) +java_test( + name = "containerResolution", + srcs = ["ContainerResolutionTest.java"], + test_class = "dev.trufflecel.ContainerResolutionTest", + deps = [ + ":test_support", + "//:trufflecel", + "//:trufflecel-protobuf", + "@maven//:com_google_protobuf_protobuf_java", + "@maven//:junit_junit", + ], +) + java_test( name = "encoder", srcs = ["EncoderTest.java"], diff --git a/src/test/java/dev/trufflecel/ContainerResolutionTest.java b/src/test/java/dev/trufflecel/ContainerResolutionTest.java new file mode 100644 index 00000000..882087d3 --- /dev/null +++ b/src/test/java/dev/trufflecel/ContainerResolutionTest.java @@ -0,0 +1,96 @@ +package dev.trufflecel; + +import static dev.trufflecel.CanonicalAst.*; +import static org.junit.Assert.*; + +import com.google.protobuf.*; +import java.util.*; +import org.junit.Test; + +/** Runtime container-qualified binding resolution for unchecked/parsed evaluation. */ +public final class ContainerResolutionTest { + private static Program parsedProgram(String source, String container) { + Environment environment = Cel.newBuilder().container(container).inferVariables(true).build(); + Message parsed = CanonicalAst.parse(environment, source); + return CanonicalAst.program(environment, parsed); + } + + @Test + public void containerQualifiedBindingWinsOverBareForParsedIdent() { + Program program = parsedProgram("y", "x"); + assertEquals(Boolean.TRUE, program.evaluateInternal(Map.of("x.y", true, "y", "false"))); + assertEquals(Boolean.TRUE, program.evaluate(Map.of("x.y", true, "y", "false"))); + } + + @Test + public void containerQualifiedBindingAloneResolvesForParsedIdent() { + Program program = parsedProgram("y", "x"); + assertEquals(Boolean.TRUE, program.evaluateInternal(Map.of("x.y", true))); + } + + @Test + public void bareBindingStillResolvesWhenNoContainerQualifiedKeyExists() { + Program program = parsedProgram("y", "x"); + assertEquals("false", program.evaluateInternal(Map.of("y", "false"))); + } + + @Test + public void genuinelyMissingIdentifierStillErrorsAfterContainerWalk() { + Program program = parsedProgram("y", "x"); + assertEquals( + CelException.Code.UNDECLARED, + assertThrows(CelException.class, () -> program.evaluateInternal(Map.of("z", 1))).code()); + } + + @Test + public void nestedContainerWalksInnermostFirst() { + Program program = parsedProgram("y", "a.b"); + assertEquals(1L, program.evaluateInternal(Map.of("a.b.y", 1L, "a.y", 2L, "y", 3L))); + assertEquals(2L, program.evaluateInternal(Map.of("a.y", 2L, "y", 3L))); + assertEquals(3L, program.evaluateInternal(Map.of("y", 3L))); + assertEquals(1L, program.evaluateInternal(Map.of("a.b.y", 1L, "y", 3L))); + } + + @Test + public void multipleRootsEachResolveThroughContainer() { + Program program = parsedProgram("y + z", "x"); + assertEquals(3L, program.evaluateInternal(Map.of("x.y", 1L, "x.z", 2L, "y", 10L, "z", 20L))); + } + + @Test + public void absoluteReferenceSkipsContainerWalk() { + Program program = parsedProgram(".y", "x"); + assertEquals(2L, program.evaluateInternal(Map.of("x.y", 1L, "y", 2L))); + } + + @Test + public void checkedPathRemainsUnchangedByContainerResolution() { + Environment env = + Cel.newBuilder() + .container("x") + .variable("x.y", CelType.BOOL) + .variable("y", CelType.STRING) + .build(); + Message parsed = CanonicalAst.parse(env, "y"); + Message checked = CanonicalAst.check(env, parsed); + Program program = CanonicalAst.program(env, checked); + assertEquals(Boolean.TRUE, program.evaluateInternal(Map.of("x.y", true, "y", "false"))); + } + + @Test + public void emptyContainerFallsBackToBareNameOnly() { + Program program = parsedProgram("y", ""); + assertEquals("false", program.evaluateInternal(Map.of("y", "false"))); + assertEquals( + CelException.Code.UNDECLARED, + assertThrows(CelException.class, () -> program.evaluateInternal(Map.of("x.y", true))) + .code()); + } + + @Test + public void singleEntryActivationResolvesContainerQualifiedKey() { + Program program = parsedProgram("y", "x"); + assertEquals(Boolean.TRUE, program.evaluate(Activation.snapshot(Map.of("x.y", true)))); + assertEquals("false", program.evaluate(Activation.snapshot(Map.of("y", "false")))); + } +} diff --git a/tools/conformance/ConformanceServerTest.java b/tools/conformance/ConformanceServerTest.java index 9e59eb20..f5e72259 100644 --- a/tools/conformance/ConformanceServerTest.java +++ b/tools/conformance/ConformanceServerTest.java @@ -404,6 +404,53 @@ public void checkFallsBackToGeneratedIdWhenOverloadIdMissing() { assertTrue(has(response, "checked_expr")); } + @Test + public void evalParsedExprResolvesContainerQualifiedBindings() { + var service = new ConformanceServer(); + Message parsed = + nested(service.parse(message(P + "ParseRequest", "cel_source", "y")), "parsed_expr"); + Message bindingXY = + message("cel.expr.ExprValue", "value", message("cel.expr.Value", "bool_value", true)); + Message bindingY = + message("cel.expr.ExprValue", "value", message("cel.expr.Value", "string_value", "false")); + var both = new LinkedHashMap(); + both.put("x.y", bindingXY); + both.put("y", bindingY); + Message bothResult = + nested( + service.eval( + message( + P + "EvalRequest", "parsed_expr", parsed, "container", "x", "bindings", both)), + "result"); + assertEquals(true, field(nested(bothResult, "value"), "bool_value")); + Message qualifiedOnly = + nested( + service.eval( + message( + P + "EvalRequest", + "parsed_expr", + parsed, + "container", + "x", + "bindings", + Map.of("x.y", bindingXY))), + "result"); + assertEquals(true, field(nested(qualifiedOnly, "value"), "bool_value")); + Message bareOnly = + nested( + service.eval( + message( + P + "EvalRequest", + "parsed_expr", + parsed, + "container", + "x", + "bindings", + Map.of("y", bindingY))), + "result"); + assertEquals("false", field(nested(bareOnly, "value"), "string_value")); + } + @SuppressWarnings("unchecked") private static List overloadIds(Message checked) { var ids = new ArrayList(); From edc1165487a8a45e2924b8d55387da9a083a2203 Mon Sep 17 00:00:00 2001 From: "detail-app[bot]" <180357370+detail-app[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:22:54 +0000 Subject: [PATCH 2/6] fix: keep rootBinding on compiled path; mark new test NullMarked Drop @TruffleBoundary from rootBinding so the compiled fast path for container-less root resolution matches the pre-PR allocation profile (benchmarks:compiled budget). The new container-walk helpers stay behind @TruffleBoundary. Add @NullMarked to ContainerResolutionTest to satisfy RequireExplicitNullMarking. --- src/main/java/dev/trufflecel/EvalContext.java | 1 - src/test/java/dev/trufflecel/BUILD.bazel | 1 + src/test/java/dev/trufflecel/ContainerResolutionTest.java | 2 ++ 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/trufflecel/EvalContext.java b/src/main/java/dev/trufflecel/EvalContext.java index e735255a..4fac1f67 100644 --- a/src/main/java/dev/trufflecel/EvalContext.java +++ b/src/main/java/dev/trufflecel/EvalContext.java @@ -261,7 +261,6 @@ Object getLocal(int slot) { return readStored(((Scoped) this).scopes[slot * 2 + 1]); } - @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary private Object rootBinding(String name, boolean absolute) { Attribute path = evaluation == null || evaluation.partial == null ? null : Attribute.of(name); Object override = attributeRead(path, false); diff --git a/src/test/java/dev/trufflecel/BUILD.bazel b/src/test/java/dev/trufflecel/BUILD.bazel index 04b849a3..bd98ac96 100644 --- a/src/test/java/dev/trufflecel/BUILD.bazel +++ b/src/test/java/dev/trufflecel/BUILD.bazel @@ -229,6 +229,7 @@ java_test( "//:trufflecel-protobuf", "@maven//:com_google_protobuf_protobuf_java", "@maven//:junit_junit", + "@maven//:org_jspecify_jspecify", ], ) diff --git a/src/test/java/dev/trufflecel/ContainerResolutionTest.java b/src/test/java/dev/trufflecel/ContainerResolutionTest.java index 882087d3..866aee73 100644 --- a/src/test/java/dev/trufflecel/ContainerResolutionTest.java +++ b/src/test/java/dev/trufflecel/ContainerResolutionTest.java @@ -5,9 +5,11 @@ import com.google.protobuf.*; import java.util.*; +import org.jspecify.annotations.NullMarked; import org.junit.Test; /** Runtime container-qualified binding resolution for unchecked/parsed evaluation. */ +@NullMarked public final class ContainerResolutionTest { private static Program parsedProgram(String source, String container) { Environment environment = Cel.newBuilder().container(container).inferVariables(true).build(); From 4f668bafbbe7b4cbc63387f2f0c57914c7331627 Mon Sep 17 00:00:00 2001 From: "detail-app[bot]" <180357370+detail-app[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:10:48 +0000 Subject: [PATCH 3/6] fix: drop redundant jspecify dep flagged by gazelle Gazelle resolves org.jspecify.annotations transitively via //:trufflecel (-> truffledata-core -> jspecify), so the explicit @maven dep on containerResolution is unused and fails the gazelle check. @NullMarked remains on the test class to satisfy RequireExplicitNullMarking. --- src/test/java/dev/trufflecel/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/dev/trufflecel/BUILD.bazel b/src/test/java/dev/trufflecel/BUILD.bazel index bd98ac96..04b849a3 100644 --- a/src/test/java/dev/trufflecel/BUILD.bazel +++ b/src/test/java/dev/trufflecel/BUILD.bazel @@ -229,7 +229,6 @@ java_test( "//:trufflecel-protobuf", "@maven//:com_google_protobuf_protobuf_java", "@maven//:junit_junit", - "@maven//:org_jspecify_jspecify", ], ) From 0a257a09be0809baa7c4ccd8785700a000446834 Mon Sep 17 00:00:00 2001 From: "detail-app[bot]" <180357370+detail-app[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:21:52 +0000 Subject: [PATCH 4/6] fix: revert @NullMarked on ContainerResolutionTest The @NullMarked annotation conflicts with the repo's Gazelle config (strict deps require a direct @maven//:org_jspecify_jspecify dep, but Gazelle auto-removes it as redundant). The RequireExplicitNullMarking finding is WARN-level code-scanning, not a CI failure, and trufflecheck passed without it. --- src/test/java/dev/trufflecel/ContainerResolutionTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/dev/trufflecel/ContainerResolutionTest.java b/src/test/java/dev/trufflecel/ContainerResolutionTest.java index 866aee73..882087d3 100644 --- a/src/test/java/dev/trufflecel/ContainerResolutionTest.java +++ b/src/test/java/dev/trufflecel/ContainerResolutionTest.java @@ -5,11 +5,9 @@ import com.google.protobuf.*; import java.util.*; -import org.jspecify.annotations.NullMarked; import org.junit.Test; /** Runtime container-qualified binding resolution for unchecked/parsed evaluation. */ -@NullMarked public final class ContainerResolutionTest { private static Program parsedProgram(String source, String container) { Environment environment = Cel.newBuilder().container(container).inferVariables(true).build(); From 4f639e73d071bd7d64f1461540ea20a654271c93 Mon Sep 17 00:00:00 2001 From: "detail-app[bot]" <180357370+detail-app[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:39:33 +0000 Subject: [PATCH 5/6] fix: keep container walk off compiled fast path when container is empty The boundary-annotated container walk in rootBinding was invoked on every root resolution, including programs with the default empty container. That added per-evaluation allocation and pushed the compiled benchmark x * 2 + 1 > 100 from 64 to 80 bytes/evaluation, exceeding its budget. Gate the walk behind environment.container.isEmpty() so compiled code takes the original inline single.equals / lookup fast path when no container prefixes exist. The boundary helpers run only when a container is actually present, preserving the container-qualified resolution semantics for unchecked parsed eval. --- src/main/java/dev/trufflecel/EvalContext.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/dev/trufflecel/EvalContext.java b/src/main/java/dev/trufflecel/EvalContext.java index 4fac1f67..9ba350bb 100644 --- a/src/main/java/dev/trufflecel/EvalContext.java +++ b/src/main/java/dev/trufflecel/EvalContext.java @@ -267,43 +267,43 @@ private Object rootBinding(String name, boolean absolute) { if (override != PartialState.NONE) return override; // Program normalizes host nulls to CelNull, so null denotes an absent binding. Object value; + boolean walk = !absolute && !environment.container.isEmpty(); if (activation instanceof Object[] arguments) { Object binding = arguments[0]; value = binding instanceof String single - ? matchesRoot(single, name, absolute) ? arguments[1] : null - : resolveRoot(binding, name, absolute); + ? (walk ? matchesRoot(single, name) : single.equals(name)) ? arguments[1] : null + : walk ? resolveRoot(binding, name) : lookup(binding, name); } else if (this instanceof Scoped scoped && scoped.singleName != null) - value = matchesRoot(scoped.singleName, name, absolute) ? activation : null; - else value = resolveRoot(activation, name, absolute); + value = + (walk ? matchesRoot(scoped.singleName, name) : scoped.singleName.equals(name)) + ? activation + : null; + else value = walk ? resolveRoot(activation, name) : lookup(activation, name); if (value != null) return value; if (path != null && evaluation.partial.containsUnknown(path)) return PartialState.MISSING; throw undeclared(name); } @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary - private Object resolveRoot(Object activation, String name, boolean absolute) { - if (!absolute) { - String prefix = environment.container; - while (!prefix.isEmpty()) { - Object value = lookup(activation, prefix + "." + name); - if (value != null) return value; - int dot = prefix.lastIndexOf('.'); - prefix = dot < 0 ? "" : prefix.substring(0, dot); - } + private Object resolveRoot(Object activation, String name) { + String prefix = environment.container; + while (!prefix.isEmpty()) { + Object value = lookup(activation, prefix + "." + name); + if (value != null) return value; + int dot = prefix.lastIndexOf('.'); + prefix = dot < 0 ? "" : prefix.substring(0, dot); } return lookup(activation, name); } @com.oracle.truffle.api.CompilerDirectives.TruffleBoundary - private boolean matchesRoot(String binding, String name, boolean absolute) { - if (!absolute) { - String prefix = environment.container; - while (!prefix.isEmpty()) { - if (binding.equals(prefix + "." + name)) return true; - int dot = prefix.lastIndexOf('.'); - prefix = dot < 0 ? "" : prefix.substring(0, dot); - } + private boolean matchesRoot(String binding, String name) { + String prefix = environment.container; + while (!prefix.isEmpty()) { + if (binding.equals(prefix + "." + name)) return true; + int dot = prefix.lastIndexOf('.'); + prefix = dot < 0 ? "" : prefix.substring(0, dot); } return binding.equals(name); } From d3955b4d14d6b1a32d546b036c6298d8b0a00068 Mon Sep 17 00:00:00 2001 From: "detail-app[bot]" <180357370+detail-app[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:04:20 +0000 Subject: [PATCH 6/6] test: cover no-match container-walk branches in rootBinding The container-qualified root-resolution fast paths added to EvalContext.rootBinding (String-single Object[] path and Scoped single-name path) only exercised the matching case in singleEntryActivationResolvesContainerQualifiedKey. The no-match branch (matchesRoot returns false -> null -> UNDECLARED) was uncovered, which dropped production branch coverage to 99.78%, under the 99.8% gate. Add singleEntryActivationMissingIdentifierErrorsAfterContainerWalk which evaluates a parsed 'y' with container 'x' against a single Activation.snapshot binding 'z' that matches neither 'x.y' nor 'y', exercising both the Object[] fast path (no-options evaluate) and the Scoped single-name path (options-driven evaluate), each expected to raise UNDECLARED after walking the container. --- .../trufflecel/ContainerResolutionTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/java/dev/trufflecel/ContainerResolutionTest.java b/src/test/java/dev/trufflecel/ContainerResolutionTest.java index 882087d3..5cf4e4d9 100644 --- a/src/test/java/dev/trufflecel/ContainerResolutionTest.java +++ b/src/test/java/dev/trufflecel/ContainerResolutionTest.java @@ -93,4 +93,26 @@ public void singleEntryActivationResolvesContainerQualifiedKey() { assertEquals(Boolean.TRUE, program.evaluate(Activation.snapshot(Map.of("x.y", true)))); assertEquals("false", program.evaluate(Activation.snapshot(Map.of("y", "false")))); } + + @Test + public void singleEntryActivationMissingIdentifierErrorsAfterContainerWalk() { + Program program = parsedProgram("y", "x"); + // Object[] fast path: a single String binding that matches neither the container-qualified + // name nor the bare name must fall through to UNDECLARED after the container walk. + assertEquals( + CelException.Code.UNDECLARED, + assertThrows( + CelException.class, () -> program.evaluate(Activation.snapshot(Map.of("z", 1)))) + .code()); + // Scoped single-name path (options-driven evaluation): same container walk, no match. + assertEquals( + CelException.Code.UNDECLARED, + assertThrows( + CelException.class, + () -> + program.evaluate( + Activation.snapshot(Map.of("z", 1)), + EvaluationOptions.newBuilder().build())) + .code()); + } }