public inbox for kaagum@systemreboot.net
 help / color / mirror / Atom feed
From: Arun Isaac <arunisaac@systemreboot.net>
To: Arun Isaac <arunisaac@systemreboot.net>,
	Ricardo Wurmus <rekado@elephly.net>
Cc: kaagum@systemreboot.net
Subject: [PATCH] Support context compaction.
Date: Sun,  8 Nov 2026 01:36:26 +0000	[thread overview]
Message-ID: <20261108013629.2076-1-arunisaac@systemreboot.net> (raw)
In-Reply-To: <87zevkqf4d.fsf@systemreboot.net>

---
 bin/kaagum     |  9 ++++++++-
 kaagum/tea.scm | 34 +++++++++++++++++++---------------
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/bin/kaagum b/bin/kaagum
index 2c8c9ee..14a65b1 100755
--- a/bin/kaagum
+++ b/bin/kaagum
@@ -53,6 +53,9 @@ exec guile --no-auto-compile -e main -s "$0" "$@"
         (option (list #\t "more-tools") #t #f
                 (lambda (opt name arg result)
                   (acons 'more-tools arg result)))
+        (option (list #c "compacter") #t #f
+                (lambda (opt name arg result)
+                  (acons 'compacter arg result)))
         (option (list "kagi-api-key-command") #t #f
                 (lambda (opt name arg result)
                   (acons 'kagi-api-key-command arg result)))
@@ -79,6 +82,7 @@ Run kaagum AI agent.
   -m, --model=MODEL              LLM model name to start new sessions with
                                  (default: \"anthropic/claude-opus-4.8\")
   -t, --more-tools=FILE          scheme file providing more tools to the agent
+  -c, --compacter=FILE           scheme file providing context compacter
 
   --kagi-api-key-command=COMMAND command to run to get Kagi API key
 
@@ -135,7 +139,10 @@ Run kaagum AI agent.
                                       '())
                                   (if (assq-ref args 'more-tools)
                                       (load (assq-ref args 'more-tools))
-                                      '())))))
+                                      '()))
+                          (if (assq-ref args 'compacter)
+                              (load (assq-ref args 'compacter))
+                              identity))))
          (parameterize ((%kagi-api-key (and kagi-api-key-command
                                             (get-api-key kagi-api-key-command))))
            ;; (trace 'kagi "Key ~a" (%kagi-api-key))
diff --git a/kaagum/tea.scm b/kaagum/tea.scm
index f3b2397..8d28213 100644
--- a/kaagum/tea.scm
+++ b/kaagum/tea.scm
@@ -962,7 +962,7 @@ in @code{tea-loop}."
    ((tool-call-result? message)
     (tool-call-result-effects message))))
 
-(define* (tea-loop state llm-base-uri llm-api-key models tools
+(define* (tea-loop state llm-base-uri llm-api-key models tools compacter
                    #:optional (events (let ((line (get-line (current-input-port))))
                                         (and (not (eof-object? line))
                                              (begin
@@ -970,10 +970,10 @@ in @code{tea-loop}."
                                                (list (acp-message (json-string->scm line))))))))
   "Run a @acronym{TEA, The Elm Architecture} loop starting with @var{state}.
 
-@var{llm-base-uri}, @var{llm-api-key} and @var{tools} are the same as in
-@code{run-tea-loop}. @var{models} is an association list mapping model IDs to
-@code{<model>} objects, the first element of which is the default model.
-@var{events} is an internal recursion variable."
+@var{llm-base-uri}, @var{llm-api-key}, @var{tools} and @var{compacter} are the
+same as in @code{run-tea-loop}. @var{models} is an association list mapping
+model IDs to @code{<model>} objects, the first element of which is the default
+model. @var{events} is an internal recursion variable."
   ;; Read a JSON-RPC message, handle it, and loop.
   (when events
     (let-values (((effects next-state)
@@ -981,18 +981,18 @@ in @code{tea-loop}."
                     (state-append-map (cut acp-effects <> models tools)
                                       events)
                     state)))
-      (match (append-map (cut do-effect <> llm-base-uri llm-api-key tools)
+      (match (append-map (cut do-effect <> llm-base-uri llm-api-key tools compacter)
                          effects)
         (()
-         (tea-loop next-state llm-base-uri llm-api-key models tools))
+         (tea-loop next-state llm-base-uri llm-api-key models tools compacter))
         ((events ...)
-         (tea-loop next-state llm-base-uri llm-api-key models tools events))))))
+         (tea-loop next-state llm-base-uri llm-api-key models tools compacter events))))))
 
-(define (do-effect effect llm-base-uri llm-api-key tools)
+(define (do-effect effect llm-base-uri llm-api-key tools compacter)
   "Do @var{effect}. Return list of events produced by the effect.
 
-@var{llm-base-uri}, @var{llm-api-key} and @var{tools} are the same as in
-@code{run-tea-loop}."
+@var{llm-base-uri}, @var{llm-api-key}, @var{tools} and @var{compacter} are the
+same as in @code{run-tea-loop}."
   (cond
    ;; Send message to client, and return the state unchanged.
    ((acp-message? effect)
@@ -1009,7 +1009,7 @@ in @code{tea-loop}."
                         (openai-query llm-base-uri
                                       llm-api-key
                                       (llm-request-model effect)
-                                      (llm-request-messages effect)
+                                      (compacter (llm-request-messages effect))
                                       (map (match-lambda
                                              ((name . tool)
                                               (tool->spec name tool)))
@@ -1018,16 +1018,20 @@ in @code{tea-loop}."
    ((tool-call? effect)
     (list (eval-tool-call effect tools)))))
 
-(define (run-tea-loop llm-base-uri llm-api-key model tools)
+(define (run-tea-loop llm-base-uri llm-api-key model tools compacter)
   "Run a @acronym{TEA, The Elm Architecture} loop. @var{llm-base-uri} is the base
 URI of the LLM provider. @var{llm-api-key} is the API key to authenticate with
 the LLM provider. @var{model} is the name of the model to initialize sessions
 with. @var{tools} is the list of tools made available to the LLM. It is an
-association list matching tool names to @code{<tool>} objects."
+association list matching tool names to @code{<tool>} objects. @var{compacter}
+is a function that takes a list of messages and returns a compacted list of
+messages to query the LLM with. The compacted messages hopefully use fewer
+tokens than the original messages."
   (let ((models (openai-models llm-base-uri llm-api-key)))
     (tea-loop (state #f 0 0 '() '())
               llm-base-uri
               llm-api-key
               (cons (assoc model models)
                     (alist-delete model models))
-              tools)))
+              tools
+              compacter)))
-- 
2.54.0


  reply	other threads:[~2026-11-08  1:36 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06  8:11 strategies for context management Ricardo Wurmus
2026-11-06 14:21 ` Arun Isaac
2026-11-06 21:28   ` Ricardo Wurmus
2026-11-07  8:15     ` Ricardo Wurmus
2026-11-08  1:35       ` Arun Isaac
2026-11-08  1:36         ` Arun Isaac [this message]
2026-11-09 13:25         ` Ricardo Wurmus
2026-11-10 23:42           ` Arun Isaac
2026-11-13 22:41             ` [PATCH 1/4] Add message record equality comparators Arun Isaac
2026-11-13 22:41             ` [PATCH 2/4] Support context compaction Arun Isaac
2026-11-13 22:41             ` [PATCH 3/4] Add sliding window compacter Arun Isaac
2026-11-13 22:41             ` [PATCH 4/4] Allow loading compacter from file Arun Isaac
2026-11-13 22:46             ` strategies for context management Arun Isaac
2026-11-15  4:47               ` [PATCH v2 0/5] Context compaction Arun Isaac
2026-11-19 14:28                 ` Ricardo Wurmus
2026-11-19 14:29                   ` Ricardo Wurmus
2026-11-19 18:43                   ` Arun Isaac
2026-11-20  5:39                     ` Ricardo Wurmus
2026-11-20  5:56                       ` Ricardo Wurmus
2026-11-20 13:42                         ` Arun Isaac
2026-11-20 13:45                           ` Arun Isaac
2026-11-20 14:01                           ` Ricardo Wurmus
2026-11-22 16:54                             ` Arun Isaac
2026-11-15  4:47               ` [PATCH v2 1/5] Support context compaction Arun Isaac
2026-11-15  4:47               ` [PATCH v2 2/5] Add sliding window compacter Arun Isaac
2026-11-15  4:47               ` [PATCH v2 3/5] Add summarizing compacter Arun Isaac
2026-11-15  4:47               ` [PATCH v2 4/5] Document context compacters Arun Isaac
2026-11-15  4:47               ` [PATCH v2 5/5] Allow user-defined " Arun Isaac
2026-11-08  1:43     ` strategies for context management Arun Isaac

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261108013629.2076-1-arunisaac@systemreboot.net \
    --to=arunisaac@systemreboot.net \
    --cc=kaagum@systemreboot.net \
    --cc=rekado@elephly.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox