Files
goclaw/internal/agent/loop_compact_max_tokens_test.go
Clark Cant 5ca433b8ac fix: persist mid-loop compaction to stop re-compaction loop and revive episodic
The v3 pipeline compacts session history mid-loop (prune_stage +
final-request guard) but only mutates the run's message buffer, never the
session store. Each turn reloads full history and re-compacts from scratch:
message_tokens climb 129k->156k across turns while every turn compacts back
down to ~60k. The lossy compaction differs per run, degrading the agent.

The same missing persistence stalls episodic memory: the cumulative
compaction count never advances, so the episodic worker's idempotency key
(sessionKey:count) is pinned and every cycle after the first is skipped.
Observed on live traffic: 8 run.completed since deploy, 0 new episodic.

Fixes, all reusing existing machinery (no new store methods, no migrations):

- Bug A: emitSessionCompleted reads cumulative GetCompactionCount (matching
  the legacy v2 path) instead of the per-run counter that resets to 0.
- Bug B/anti-loop: finalize passes state.Prune.MidLoopCompacted into
  maybeSummarize; under pressure it lowers the trigger to a unit-aligned
  threshold (compactionInputCap - overhead, same MaxRequestShare the guard
  uses) so the compaction is PERSISTED via the existing TruncateHistory +
  IncrementCompaction path. Defensive floor prevents over-compaction on
  pathological config; tool-result-only bloat still skips (history-only).
- Bug C: SourceID embeds the count (sessionKey:count) so the eventbus dedup
  key advances per compaction cycle instead of swallowing rapid same-session
  turns within the 5m TTL.

Tests: episodic compaction, maybe_summarize pressure, request budget.
go build (PG + sqliteonly), go vet, go test -race all green.
2026-07-25 00:39:02 +07:00

79 lines
2.7 KiB
Go

package agent
import (
"context"
"testing"
"github.com/nextlevelbuilder/goclaw/internal/providers"
)
// capturingProvider records every ChatRequest passed to Chat.
// Distinct from stubProvider in intent_classify_test.go (that one ignores the request).
type capturingProvider struct {
captured []providers.ChatRequest
response string
}
func (c *capturingProvider) Chat(_ context.Context, req providers.ChatRequest) (*providers.ChatResponse, error) {
c.captured = append(c.captured, req)
return &providers.ChatResponse{Content: c.response}, nil
}
func (c *capturingProvider) ChatStream(_ context.Context, req providers.ChatRequest, _ func(providers.StreamChunk)) (*providers.ChatResponse, error) {
c.captured = append(c.captured, req)
return &providers.ChatResponse{Content: c.response}, nil
}
func (c *capturingProvider) DefaultModel() string { return "capturing-model" }
func (c *capturingProvider) Name() string { return "capturing" }
// TestCompactMessagesInPlace_MaxTokensDynamic verifies that compactMessagesInPlace
// passes max_tokens == dynamicSummaryMax(estimatedInputTokens) to the provider.
func TestCompactMessagesInPlace_MaxTokensDynamic(t *testing.T) {
cap := &capturingProvider{response: "Summary of conversation."}
loop := &Loop{
provider: cap,
model: "claude-3-5-sonnet",
contextWindow: 200_000,
// tokenCounter nil → estimateSummaryInputTokens uses rune/3 fallback
}
// Build 10 dummy messages (>= 6 required by compactMessagesInPlace).
msgs := make([]providers.Message, 10)
for i := range msgs {
if i%2 == 0 {
msgs[i] = providers.Message{Role: "user", Content: "user message"}
} else {
msgs[i] = providers.Message{Role: "assistant", Content: "assistant reply"}
}
}
result := loop.compactMessagesInPlace(context.Background(), msgs)
if result == nil {
t.Fatal("compactMessagesInPlace returned nil; expected compaction to succeed")
}
if len(cap.captured) != 1 {
t.Fatalf("provider.Chat called %d time(s), want 1", len(cap.captured))
}
req := cap.captured[0]
maxTokensRaw, ok := req.Options["max_tokens"]
if !ok {
t.Fatal("Options[\"max_tokens\"] not set in ChatRequest")
}
maxTokens, ok := maxTokensRaw.(int)
if !ok {
t.Fatalf("Options[\"max_tokens\"] type = %T, want int", maxTokensRaw)
}
// Compute expected using the same formula the implementation uses.
// With keepCount=4 and 10 messages, splitIdx=6 (first 6 messages summarised).
// tokenCounter nil → rune/3 fallback.
expectedIn := loop.estimateSummaryInputTokens(msgs[:6])
wantMax := dynamicSummaryMax(expectedIn)
if maxTokens != wantMax {
t.Errorf("max_tokens = %d, want %d (dynamicSummaryMax(%d))", maxTokens, wantMax, expectedIn)
}
}