xonecas.io
6119
My

mysis / commit d13964e

feat: add smart history compression

Compress old tool results to prevent context explosion during long conversations.

Compression strategy:
- Keep last 10 turns full (configurable)
- Compress older messages by category:
  * State queries (get_status, get_map, etc.) -> "[compressed - old state data]"
  * Auth tools (login, register, logout) -> NEVER compressed
  * Action tools -> Truncate to 200 chars if over 500 chars

Tool categories:
- State: get_status, get_ship, get_system, get_sector, get_galaxy, get_map,
  get_players, get_leaderboard, get_market, get_cargo, captains_log_list
- Auth: login, register, logout (always preserved)
- Action: everything else (truncate if long)

Features:
- CompressHistory() function with configurable turn retention
- EstimateTokenCount() for debugging
- Debug logging shows compression stats (messages/tokens saved)
- Comprehensive test coverage

Prepares for autoplay feature by preventing unbounded context growth.

xonecas2026-02-10 d13964eb1797c3a95c974623daeaf25a691bd8fa parent 894507d

3 files changed

cmd/mysis/main.go+19−2
@@ -295,6 +295,23 @@ func (app *App) processTurn(ctx context.Context) error {
295295 maxToolRounds := 20 // Prevent infinite loops
296296
297297 for round := 0; round < maxToolRounds; round++ {
298+ // Compress history before sending to LLM
299+ // Keep last 10 turns full, compress older state queries
300+ compressedHistory := store.CompressHistory(app.history, 10)
301+
302+ // Log compression stats
303+ if len(compressedHistory) < len(app.history) {
304+ originalTokens := store.EstimateTokenCount(app.history)
305+ compressedTokens := store.EstimateTokenCount(compressedHistory)
306+ log.Debug().
307+ Int("original_msgs", len(app.history)).
308+ Int("compressed_msgs", len(compressedHistory)).
309+ Int("original_tokens", originalTokens).
310+ Int("compressed_tokens", compressedTokens).
311+ Int("saved_tokens", originalTokens-compressedTokens).
312+ Msg("History compressed")
313+ }
314+
298315 // Convert MCP tools to provider format
299316 providerTools := make([]provider.Tool, len(app.tools))
300317 for i, t := range app.tools {
@@ -305,8 +322,8 @@ func (app *App) processTurn(ctx context.Context) error {
305322 }
306323 }
307324
308- // Call LLM with tools
309- resp, err := app.provider.ChatWithTools(ctx, app.history, providerTools)
325+ // Call LLM with compressed history
326+ resp, err := app.provider.ChatWithTools(ctx, compressedHistory, providerTools)
310327 if err != nil {
311328 return fmt.Errorf("LLM call failed: %w", err)
312329 }
internal/store/compression.go+197−0
@@ -0,0 +1,197 @@
1+package store
2+
3+import (
4+ "encoding/json"
5+ "strings"
6+
7+ "github.com/xonecas/mysis/internal/provider"
8+)
9+
10+// ToolCategory represents the category of a tool for compression purposes.
11+type ToolCategory int
12+
13+const (
14+ // ToolCategoryAuth - Authentication tools (never compress)
15+ ToolCategoryAuth ToolCategory = iota
16+ // ToolCategoryState - State query tools (compress old results, keep latest)
17+ ToolCategoryState
18+ // ToolCategoryAction - Action tools (keep full history)
19+ ToolCategoryAction
20+)
21+
22+// compressedToolResult is a marker for compressed content
23+const compressedToolResult = "[compressed - old state data]"
24+
25+// isStateQueryTool returns true if the tool is a state query that can be compressed.
26+func isStateQueryTool(toolName string) bool {
27+ stateTools := []string{
28+ "get_status",
29+ "get_ship",
30+ "get_system",
31+ "get_sector",
32+ "get_galaxy",
33+ "get_map",
34+ "get_players",
35+ "get_leaderboard",
36+ "get_market",
37+ "get_cargo",
38+ "captains_log_list",
39+ }
40+
41+ toolName = strings.ToLower(toolName)
42+ for _, st := range stateTools {
43+ if toolName == st {
44+ return true
45+ }
46+ }
47+ return false
48+}
49+
50+// isAuthTool returns true if the tool is authentication-related (never compress).
51+func isAuthTool(toolName string) bool {
52+ authTools := []string{
53+ "login",
54+ "register",
55+ "logout",
56+ }
57+
58+ toolName = strings.ToLower(toolName)
59+ for _, at := range authTools {
60+ if toolName == at {
61+ return true
62+ }
63+ }
64+ return false
65+}
66+
67+// CompressHistory compresses old tool results while preserving recent context.
68+func CompressHistory(messages []provider.Message, keepFullTurns int) []provider.Message {
69+ if len(messages) == 0 {
70+ return messages
71+ }
72+
73+ // Count turns (user messages)
74+ turnCount := 0
75+ for i := len(messages) - 1; i >= 0; i-- {
76+ if messages[i].Role == "user" {
77+ turnCount++
78+ }
79+ }
80+
81+ // If we have fewer turns than the threshold, no compression needed
82+ if turnCount <= keepFullTurns {
83+ return messages
84+ }
85+
86+ // Find the cutoff point (first message of the turn that should be kept)
87+ // If keepFullTurns=2, we want to keep the last 2 turns and compress everything before
88+ currentTurn := 0
89+ cutoffIndex := -1
90+ for i := len(messages) - 1; i >= 0; i-- {
91+ if messages[i].Role == "user" {
92+ currentTurn++
93+ if currentTurn == keepFullTurns {
94+ // This is the first user message of the Nth-from-last turn
95+ // The cutoff is this message (we keep from here onwards)
96+ cutoffIndex = i
97+ break
98+ }
99+ }
100+ }
101+
102+ if cutoffIndex == -1 || cutoffIndex == 0 {
103+ return messages
104+ }
105+
106+ // Build compressed history
107+ compressed := make([]provider.Message, 0, len(messages))
108+
109+ // Second pass: compress old messages
110+ for i := 0; i < cutoffIndex; i++ {
111+ msg := messages[i]
112+
113+ // Keep user messages and assistant messages (they're small)
114+ if msg.Role == "user" || msg.Role == "assistant" {
115+ compressed = append(compressed, msg)
116+ continue
117+ }
118+
119+ // Handle tool results
120+ if msg.Role == "tool" {
121+ // Find the tool name from the assistant message
122+ toolName := findToolNameForResult(messages, i)
123+
124+ // Never compress auth tools
125+ if isAuthTool(toolName) {
126+ compressed = append(compressed, msg)
127+ continue
128+ }
129+
130+ // For state queries in old section, always compress
131+ if isStateQueryTool(toolName) {
132+ compressedMsg := msg
133+ compressedMsg.Content = compressedToolResult
134+ compressed = append(compressed, compressedMsg)
135+ continue
136+ }
137+
138+ // For action tools, compress if result is too long
139+ if len(msg.Content) > 500 {
140+ compressedMsg := msg
141+ compressedMsg.Content = msg.Content[:200] + "... [truncated]"
142+ compressed = append(compressed, compressedMsg)
143+ } else {
144+ compressed = append(compressed, msg)
145+ }
146+ }
147+ }
148+
149+ // Add all recent messages (after cutoff) unchanged
150+ compressed = append(compressed, messages[cutoffIndex:]...)
151+
152+ return compressed
153+}
154+
155+// findToolNameForResult finds the tool name for a tool result message
156+// by looking back for the assistant message with the matching tool call.
157+func findToolNameForResult(messages []provider.Message, resultIndex int) string {
158+ if resultIndex >= len(messages) || messages[resultIndex].Role != "tool" {
159+ return ""
160+ }
161+
162+ toolCallID := messages[resultIndex].ToolCallID
163+
164+ // Search backwards for the assistant message with this tool call
165+ for i := resultIndex - 1; i >= 0; i-- {
166+ msg := messages[i]
167+ if msg.Role == "assistant" && len(msg.ToolCalls) > 0 {
168+ for _, tc := range msg.ToolCalls {
169+ if tc.ID == toolCallID {
170+ return tc.Name
171+ }
172+ }
173+ }
174+ }
175+
176+ return ""
177+}
178+
179+// EstimateTokenCount provides a rough estimate of token count for history.
180+// This is used for logging and debugging, not exact.
181+func EstimateTokenCount(messages []provider.Message) int {
182+ total := 0
183+ for _, msg := range messages {
184+ // Rough estimate: ~4 characters per token
185+ total += len(msg.Content) / 4
186+
187+ // Add tool calls
188+ if len(msg.ToolCalls) > 0 {
189+ data, _ := json.Marshal(msg.ToolCalls)
190+ total += len(data) / 4
191+ }
192+
193+ // Add role overhead
194+ total += 4
195+ }
196+ return total
197+}
internal/store/compression_test.go+183−0
@@ -0,0 +1,183 @@
1+package store
2+
3+import (
4+ "testing"
5+
6+ "github.com/xonecas/mysis/internal/provider"
7+)
8+
9+func TestCompressHistory_NoCompression(t *testing.T) {
10+ messages := []provider.Message{
11+ {Role: "user", Content: "hello"},
12+ {Role: "assistant", Content: "hi there"},
13+ }
14+
15+ compressed := CompressHistory(messages, 10)
16+
17+ if len(compressed) != len(messages) {
18+ t.Errorf("expected no compression, got %d messages from %d", len(compressed), len(messages))
19+ }
20+}
21+
22+func TestCompressHistory_StateQueries(t *testing.T) {
23+ messages := []provider.Message{
24+ // Turn 1
25+ {Role: "user", Content: "what's my status?"},
26+ {Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
27+ {ID: "call1", Name: "get_status"},
28+ }},
29+ {Role: "tool", Content: "OLD STATUS DATA: very long result here...", ToolCallID: "call1"},
30+ {Role: "assistant", Content: "You're doing well"},
31+
32+ // Turn 2
33+ {Role: "user", Content: "status again?"},
34+ {Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
35+ {ID: "call2", Name: "get_status"},
36+ }},
37+ {Role: "tool", Content: "NEW STATUS DATA: latest result", ToolCallID: "call2"},
38+ {Role: "assistant", Content: "Here's the update"},
39+
40+ // Turn 3 (recent)
41+ {Role: "user", Content: "navigate somewhere"},
42+ {Role: "assistant", Content: "navigating..."},
43+ }
44+
45+ // Keep last 2 turns full, compress older
46+ compressed := CompressHistory(messages, 2)
47+
48+ // Find the old get_status result
49+ var oldStatusResult *provider.Message
50+ for i := range compressed {
51+ if compressed[i].Role == "tool" && compressed[i].ToolCallID == "call1" {
52+ oldStatusResult = &compressed[i]
53+ break
54+ }
55+ }
56+
57+ if oldStatusResult == nil {
58+ t.Fatal("old status result not found in compressed history")
59+ }
60+
61+ if oldStatusResult.Content != compressedToolResult {
62+ t.Errorf("old status result not compressed, got: %s", oldStatusResult.Content)
63+ }
64+
65+ // Find the new get_status result
66+ var newStatusResult *provider.Message
67+ for i := range compressed {
68+ if compressed[i].Role == "tool" && compressed[i].ToolCallID == "call2" {
69+ newStatusResult = &compressed[i]
70+ break
71+ }
72+ }
73+
74+ if newStatusResult == nil {
75+ t.Fatal("new status result not found in compressed history")
76+ }
77+
78+ if newStatusResult.Content != "NEW STATUS DATA: latest result" {
79+ t.Errorf("new status result was compressed when it shouldn't be, got: %s", newStatusResult.Content)
80+ }
81+}
82+
83+func TestCompressHistory_AuthToolsPreserved(t *testing.T) {
84+ messages := []provider.Message{
85+ // Turn 1 (old)
86+ {Role: "user", Content: "login"},
87+ {Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
88+ {ID: "call1", Name: "login"},
89+ }},
90+ {Role: "tool", Content: "Login successful with credentials", ToolCallID: "call1"},
91+ {Role: "assistant", Content: "Logged in"},
92+
93+ // Turn 2
94+ {Role: "user", Content: "status?"},
95+ {Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
96+ {ID: "call2", Name: "get_status"},
97+ }},
98+ {Role: "tool", Content: "Some status data", ToolCallID: "call2"},
99+ {Role: "assistant", Content: "Here's your status"},
100+
101+ // Turn 3 (recent)
102+ {Role: "user", Content: "navigate"},
103+ {Role: "assistant", Content: "navigating..."},
104+ }
105+
106+ // Keep last 1 turn full, compress older
107+ compressed := CompressHistory(messages, 1)
108+
109+ // Find the login result
110+ var loginResult *provider.Message
111+ for i := range compressed {
112+ if compressed[i].Role == "tool" && compressed[i].ToolCallID == "call1" {
113+ loginResult = &compressed[i]
114+ break
115+ }
116+ }
117+
118+ if loginResult == nil {
119+ t.Fatal("login result not found in compressed history")
120+ }
121+
122+ // Auth tools should NEVER be compressed
123+ if loginResult.Content != "Login successful with credentials" {
124+ t.Errorf("login result was compressed, got: %s", loginResult.Content)
125+ }
126+}
127+
128+func TestEstimateTokenCount(t *testing.T) {
129+ messages := []provider.Message{
130+ {Role: "user", Content: "hello world"}, // ~3 tokens + 4 overhead
131+ {Role: "assistant", Content: "hi there how are you"}, // ~5 tokens + 4 overhead
132+ }
133+
134+ tokens := EstimateTokenCount(messages)
135+
136+ // Should be roughly (12 + 20) / 4 + 8 = ~16
137+ if tokens < 10 || tokens > 20 {
138+ t.Errorf("token estimate seems off: %d", tokens)
139+ }
140+}
141+
142+func TestIsStateQueryTool(t *testing.T) {
143+ tests := []struct {
144+ name string
145+ want bool
146+ }{
147+ {"get_status", true},
148+ {"get_ship", true},
149+ {"get_map", true},
150+ {"GET_STATUS", true}, // case insensitive
151+ {"login", false},
152+ {"travel", false},
153+ {"mine", false},
154+ }
155+
156+ for _, tt := range tests {
157+ got := isStateQueryTool(tt.name)
158+ if got != tt.want {
159+ t.Errorf("isStateQueryTool(%q) = %v, want %v", tt.name, got, tt.want)
160+ }
161+ }
162+}
163+
164+func TestIsAuthTool(t *testing.T) {
165+ tests := []struct {
166+ name string
167+ want bool
168+ }{
169+ {"login", true},
170+ {"register", true},
171+ {"logout", true},
172+ {"LOGIN", true}, // case insensitive
173+ {"get_status", false},
174+ {"travel", false},
175+ }
176+
177+ for _, tt := range tests {
178+ got := isAuthTool(tt.name)
179+ if got != tt.want {
180+ t.Errorf("isAuthTool(%q) = %v, want %v", tt.name, got, tt.want)
181+ }
182+ }
183+}