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 v2 2/5] Add sliding window compacter.
Date: Sun, 15 Nov 2026 04:47:47 +0000	[thread overview]
Message-ID: <20261115044752.5595-3-arunisaac@systemreboot.net> (raw)
In-Reply-To: <87ik20nycd.fsf@systemreboot.net>

---
 kaagum/compacters.scm | 97 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 95 insertions(+), 2 deletions(-)

diff --git a/kaagum/compacters.scm b/kaagum/compacters.scm
index 25a3281..4df4d19 100644
--- a/kaagum/compacters.scm
+++ b/kaagum/compacters.scm
@@ -17,10 +17,103 @@
 ;;; along with kaagum.  If not, see <https://www.gnu.org/licenses/>.
 
 (define-module (kaagum compacters)
+  #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-11)
+  #:use-module (srfi srfi-26)
+  #:use-module (ice-9 match)
+  #:use-module (json)
+  #:use-module (kaagum message)
   #:use-module (kaagum records)
-  #:export (noop-compacter))
+  #:use-module (kaagum trace)
+  #:export (trace-compacter-in
+            trace-compacter-out
+
+            noop-compacter
+            sliding-window-compacter))
 
 ;; The noop compacter is an identity compacter. It doesn't compact at all; it
 ;; returns the messages as they are.
 (define* (noop-compacter messages #:key tokens context-length #:allow-other-keys)
-  messages)
\ No newline at end of file
+  messages)
+
+(define (trace-compacter-in messages)
+  "Trace compacter input of @var{messages} and return it."
+  (trace 'compacter "<- ~a"
+         (scm->json-string (list->vector (map message->tree messages))))
+  messages)
+
+(define (trace-compacter-out messages)
+  "Trace compacter output of @var{messages} and return it."
+  (trace 'compacter "-> ~a"
+         (scm->json-string (list->vector (map message->tree messages))))
+  messages)
+
+(define (message-length message)
+  (or (and=> (message-content message)
+             string-length)
+      0))
+
+(define (nspan pred . lists)
+  "Like @code{span}, but where the @var{pred} takes many arguments instead of one."
+  (let-values (((keep-list drop-list)
+                (span (cut apply pred <>)
+                      (apply map list lists))))
+    (values (map first keep-list)
+            (map first drop-list))))
+
+(define (break-messages messages tokens keep-tokens)
+  "Break list of @var{messages} into two lists, the second list of which
+corresponds to @var{keep-tokens} number of tokens. The second list has at least
+one element, even when it corresponds to more than @var{keep-tokens} number of
+tokens. The @var{messages} list is only broken at a prompt turn boundary, that
+is, at @code{<user-message>} type messages.
+
+@var{tokens} is the number of tokens @var{messages} corresponds to."
+  (let ((total-message-length (fold (lambda (message result)
+                                      (+ (message-length message)
+                                         result))
+                                    0
+                                    messages)))
+    (let-values (((keep-messages drop-messages)
+                  (nspan (lambda (message previous-message tokens-so-far)
+                           ;; We break only at prompt turn boundaries, that is,
+                           ;; when the previous message is a <user-message>.
+                           ;; Since the first message has no previous message,
+                           ;; this also means that at least one message is
+                           ;; guaranteed to be kept.
+                           (or (not (user-message? previous-message))
+                               (< tokens-so-far keep-tokens)))
+                         (reverse messages)
+                         (cons #f (reverse messages))
+                         (fold (lambda (message result)
+                                 (cons (+ (* (/ (message-length message)
+                                                total-message-length)
+                                             tokens)
+                                          (match result
+                                            (() 0)
+                                            ((head tail ...) head)))
+                                       result))
+                               '()
+                               (reverse messages)))))
+      (values (reverse drop-messages)
+              (reverse keep-messages)))))
+
+;; Once CONTEXT-THRESHOLD fraction of the context window has been used, the
+;; sliding window compacter keeps only the most recent messages corresponding to
+;; KEEP-CONTEXT fraction of the context window.
+(define* (sliding-window-compacter #:key (context-threshold 0.7) (keep-context 0.2))
+  (lambda* (messages #:key tokens context-length #:allow-other-keys)
+    (if (> (/ tokens context-length)
+           context-threshold)
+        (let-values (((drop-messages keep-messages)
+                      (break-messages messages
+                                      tokens
+                                      (* keep-context context-length))))
+          (match drop-messages
+            ;; keep all messages
+            (() keep-messages)
+            ;; actually drop messages
+            (_
+             (trace-compacter-in messages)
+             (trace-compacter-out keep-messages))))
+        messages)))
-- 
2.54.0


  parent reply	other threads:[~2026-11-15  4:47 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         ` [PATCH] Support context compaction Arun Isaac
2026-11-09 13:25         ` strategies for context management 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               ` Arun Isaac [this message]
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=20261115044752.5595-3-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