branch: externals/ellama
commit 5ec5694cee650e41befd6d908f61b9ebe9d3ccd8
Author: Sergey Kostyaev <[email protected]>
Commit: Sergey Kostyaev <[email protected]>

    Resume requests in the session buffer after preflight compaction
    
    Start deferred requests from preflight auto-compaction inside the target 
session buffer with a fresh request context. Strengthened regression tests to 
complete compaction from a different buffer and assert request state lands on 
the chat buffer.
---
 ellama.el                  |  6 ++++-
 tests/test-ellama-tools.el | 18 +++++++++++---
 tests/test-ellama.el       | 60 ++++++++++++++++++++++++++++++----------------
 3 files changed, 59 insertions(+), 25 deletions(-)

diff --git a/ellama.el b/ellama.el
index 14772d7177..c34b1eb00f 100644
--- a/ellama.el
+++ b/ellama.el
@@ -3638,7 +3638,11 @@ failure (with BUFFER current).
                (lambda ()
                  (when (ellama-session-p session)
                    (setq llm-prompt (ellama-session-prompt session)))
-                 (start-request)))
+                 (setq request-context
+                       (ellama--make-request-context
+                        (list buffer reasoning-buffer)))
+                 (with-current-buffer buffer
+                   (start-request))))
             (start-request)))))))
 
 (defun ellama-chain (initial-prompt forms &optional acc)
diff --git a/tests/test-ellama-tools.el b/tests/test-ellama-tools.el
index 8ef8adbbd1..b1f178d6d1 100644
--- a/tests/test-ellama-tools.el
+++ b/tests/test-ellama-tools.el
@@ -3267,7 +3267,8 @@ END_ELLAMA_AGENT_STATE"))
          (ellama-session-hide-org-quotes nil)
          (call-count 0)
          compact-callback
-         main-prompt)
+         main-prompt
+         callback-buffer)
     (llm-chat-prompt-append-response prompt "assistant 1" 'assistant)
     (llm-chat-prompt-append-response prompt "user 2")
     (llm-chat-prompt-append-response prompt "assistant 2" 'assistant)
@@ -3306,8 +3307,17 @@ END_ELLAMA_AGENT_STATE"))
             (should-not main-prompt)
             (should (ellama--session-extra-get
                      session :auto-compact-in-progress))
-            (funcall compact-callback '(:text "Summary"))
+            (setq callback-buffer
+                  (generate-new-buffer " *ellama-agent-compact-callback*"))
+            (with-current-buffer callback-buffer
+              (funcall compact-callback '(:text "Summary")))
             (should (= call-count 2))
+            (with-current-buffer buffer
+              (should (eq ellama--current-request 'main-request))
+              (should ellama--change-group))
+            (with-current-buffer callback-buffer
+              (should-not (eq ellama--current-request 'main-request))
+              (should-not ellama--change-group))
             (should (eq main-prompt (ellama-session-prompt session)))
             (should (string-match-p
                      "Current plan state"
@@ -3316,7 +3326,9 @@ END_ELLAMA_AGENT_STATE"))
                      "Next pending item: Keep going"
                      (llm-chat-prompt-to-text main-prompt)))))
       (when (buffer-live-p buffer)
-        (kill-buffer buffer)))))
+        (kill-buffer buffer))
+      (when (buffer-live-p callback-buffer)
+        (kill-buffer callback-buffer)))))
 
 (ert-deftest test-ellama-agent-report-result-restores-original-tools ()
   (ellama-test--ensure-local-ellama-tools)
diff --git a/tests/test-ellama.el b/tests/test-ellama.el
index 26852faf99..02daf841ff 100644
--- a/tests/test-ellama.el
+++ b/tests/test-ellama.el
@@ -1471,7 +1471,9 @@ detailed comparison to help you decide:
          (call-count 0)
          compact-callback
          main-prompt
-         done-text)
+         done-text
+         target-buffer
+         callback-buffer)
     (cl-letf (((symbol-function 'llm-count-tokens)
                (lambda (_provider _text) 120))
               ((symbol-function 'llm-chat-async)
@@ -1484,27 +1486,43 @@ detailed comparison to help you decide:
                        'compact-request)
                    (setq main-prompt prompt)
                    'main-request))))
-      (with-temp-buffer
-        (setq-local ellama--current-session session)
-        (ellama-stream "continue after compaction"
-                       :session session
-                       :on-done (lambda (text)
-                                  (setq done-text text)))
-        (should (= call-count 1))
-        (should compact-callback)
-        (should (ellama--session-extra-get
-                 session :auto-compact-in-progress))
-        (should-not main-prompt)
-        (should-not done-text)
-        (funcall compact-callback '(:text "Summary"))
-        (should (= call-count 2))
-        (should-not (ellama--session-extra-get
+      (unwind-protect
+          (progn
+            (setq target-buffer
+                  (generate-new-buffer " *ellama-preflight-target*"))
+            (setq callback-buffer
+                  (generate-new-buffer " *ellama-preflight-callback*"))
+            (with-current-buffer target-buffer
+              (setq-local ellama--current-session session)
+              (ellama-stream "continue after compaction"
+                             :session session
+                             :on-done (lambda (text)
+                                        (setq done-text text))))
+            (should (= call-count 1))
+            (should compact-callback)
+            (should (ellama--session-extra-get
                      session :auto-compact-in-progress))
-        (should (eq ellama--current-request 'main-request))
-        (should (eq main-prompt (ellama-session-prompt session)))
-        (should (string-match-p
-                 "continue after compaction"
-                 (llm-chat-prompt-to-text main-prompt)))))))
+            (should-not main-prompt)
+            (should-not done-text)
+            (with-current-buffer callback-buffer
+              (funcall compact-callback '(:text "Summary")))
+            (should (= call-count 2))
+            (should-not (ellama--session-extra-get
+                         session :auto-compact-in-progress))
+            (with-current-buffer target-buffer
+              (should (eq ellama--current-request 'main-request))
+              (should ellama--change-group))
+            (with-current-buffer callback-buffer
+              (should-not (eq ellama--current-request 'main-request))
+              (should-not ellama--change-group))
+            (should (eq main-prompt (ellama-session-prompt session)))
+            (should (string-match-p
+                     "continue after compaction"
+                     (llm-chat-prompt-to-text main-prompt))))
+        (when (buffer-live-p target-buffer)
+          (kill-buffer target-buffer))
+        (when (buffer-live-p callback-buffer)
+          (kill-buffer callback-buffer))))))
 
 (ert-deftest test-ellama-session-auto-compact-needed-above-threshold ()
   (let* ((provider (make-llm-fake))

Reply via email to