Commit 9a281b1

mo khan <mo@mokhan.ca>
2026-01-21 17:00:21
feat: regenerate system prompt on each fetch
1 parent f5e76ea
Changed files (1)
lib
elelem
lib/elelem/agent.rb
@@ -31,10 +31,10 @@ module Elelem
       case input
       when "/exit" then exit(0)
       when "/clear"
-        @history = [{ role: "system", content: system_prompt }]
+        @history = []
         terminal.say "  → context cleared"
       when "/context"
-        terminal.say JSON.pretty_generate(history)
+        terminal.say JSON.pretty_generate(combined_history)
       else
         terminal.say "/clear /context /exit"
       end
@@ -42,7 +42,6 @@ module Elelem
 
     def turn(input)
       history << { role: "user", content: input }
-      compact_if_needed
       ctx = []
       content = nil
 
@@ -64,16 +63,6 @@ module Elelem
 
     private
 
-    def compact_if_needed
-      return if history.length < MAX_CONTEXT_MESSAGES
-      terminal.say "  → compacting context (#{history.length} messages)"
-
-      anchors = [history[0], history[1]]
-      recent = history.last(MAX_CONTEXT_MESSAGES / 2)
-
-      @history = anchors + recent
-    end
-
     def process(tool_call)
       name, args = tool_call[:name], tool_call[:arguments]
       terminal.say toolbox.header(name, args)
@@ -99,7 +88,7 @@ module Elelem
 
     def fetch_response(ctx)
       content = ""
-      tool_calls = client.fetch(history + ctx, toolbox.to_h) do |delta|
+      tool_calls = client.fetch(combined_history + ctx, toolbox.to_h) do |delta|
         content += delta[:content].to_s
         terminal.print(terminal.think(delta[:thinking])) if delta[:thinking]
       end
@@ -109,6 +98,11 @@ module Elelem
       [nil, []]
     end
 
+    def combined_history
+      system = [{ role: "system", content: system_prompt }]
+      system + history
+    end
+
     def system_prompt
       prompt = <<~PROMPT
         Terminal coding agent. Be concise. Verify your work.