Commit ef6bc6f36 for llama.cpp

commit ef6bc6f367082338006c8379bc7bcab3e1584766
Author: Xuan-Son Nguyen <son@huggingface.co>
Date:   Mon Nov 16 15:48:46 2026 +0100

    jinja: impl scope-chain key lookup (#32396)

    * jinja: impl scope-chain key lookup

    * add test for call_statement lambda

diff --git a/common/jinja/runtime.cpp b/common/jinja/runtime.cpp
index aae67cab0..1eae8d923 100644
--- a/common/jinja/runtime.cpp
+++ b/common/jinja/runtime.cpp
@@ -811,13 +811,14 @@ value call_statement::execute_impl(context & ctx) const {
     }
     auto * callee_func = cast_val<value_func>(callee_val);

-    context caller_ctx(ctx); // new scope for caller execution
+    // caller() can be saved and called after this scope ended, it owns a snapshot of the variables
+    auto caller_ctx = std::make_shared<context>(ctx.snapshot());

-    const func_handler func = [this, caller_ctx = std::move(caller_ctx)](const func_args & args) -> value {
+    const func_handler func = [this, caller_ctx](const func_args & args) -> value {
         if (args.ctx.macro_depth >= JINJA_RECURSIVE_LIMIT) {
             throw std::runtime_error("macro calls are nested more than " + std::to_string(JINJA_RECURSIVE_LIMIT) + " levels deep");
         }
-        context block_ctx(caller_ctx); // new scope for block execution
+        context block_ctx(*caller_ctx); // new scope for block execution
         block_ctx.macro_depth = args.ctx.macro_depth + 1;

         bind_parameters("caller", this->caller_args, args, block_ctx);
diff --git a/common/jinja/runtime.h b/common/jinja/runtime.h
index 97db5de33..066c4e0c0 100644
--- a/common/jinja/runtime.h
+++ b/common/jinja/runtime.h
@@ -81,12 +81,38 @@ struct context {
         current_time(parent.current_time),
         is_get_stats(parent.is_get_stats),
         macro_depth(parent.macro_depth),
-        // inherit variables (for example, when entering a new scope)
-        env(mk_val<value_object>(*parent.env)) {}
+        // child scope: own variables only, lookups fall back to the parent (which must outlive this scope)
+        parent(&parent),
+        env(mk_val<value_object>()) {
+        env->has_builtins = false;
+    }
+
+    context(context &&) = default;
+
+    // copy with all visible variables flattened in, for a scope that can outlive its parents (caller() of a call block)
+    context snapshot() const {
+        std::vector<const context *> chain;
+        for (const context * c = this; c; c = c->parent) {
+            chain.push_back(c);
+        }
+        context res(*this);
+        res.parent = nullptr;
+        for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
+            for (const auto & kv : (*it)->env->as_ordered_object()) {
+                res.env->insert(kv.first, kv.second);
+            }
+        }
+        return res;
+    }

     // returns nullptr if not found
     value find_val(const value & key) {
-        return env->find(key);
+        for (const context * c = this; c; c = c->parent) {
+            if (value v = c->env->find(key)) {
+                return v;
+            }
+        }
+        return nullptr;
     }

     value get_val(const std::string & name) {
@@ -107,6 +133,7 @@ struct context {
     }

 private:
+    const context * parent = nullptr;
     value_object env;
 };

diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp
index a2c14e9a7..2fd33beeb 100644
--- a/tests/test-jinja.cpp
+++ b/tests/test-jinja.cpp
@@ -1475,6 +1475,15 @@ static void test_macros(testing & t) {
         "a+b"
     );

+    test_template(t, "caller saved and called after its scope ended",
+        "{% set ns = namespace(f=none) %}"
+        "{% macro keep() %}{% set ns.f = caller %}{% endmacro %}"
+        "{% macro outer(p) %}{% call keep() %}[{{ p }}]{% endcall %}{% endmacro %}"
+        "{{ outer('a') }}{{ outer('b') }}{{ ns.f() }}{{ ns.f() }}",
+        json::object(),
+        "[b][b]"
+    );
+
     test_template(t, "macro with caller",
         "\
 {%- macro nest_dict(o, i, ff='') %}\n\