mirror of
https://github.com/mudler/cogito.git
synced 2026-07-23 10:25:44 -04:00
dd07cc7f09
Callers (nib) re-append the same system prompt to a persistent fragment every turn; merging N identical copies into the position-0 block grew the prompt prefix each turn and defeated the server's KV prefix cache — full re-prefill of the whole conversation every message (measured 24s@1808tok, 82s@2451tok on CPU). Dedupe identical system contents so the prefix stays stable; distinct system parts (e.g. the force-text-reply directive) are still preserved. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
package cogito
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
openai "github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
// N identical system messages (nib re-appends the same prompt every turn) must
|
|
// collapse to ONE system block, so the prompt prefix stays stable across turns
|
|
// and the server's KV prefix cache is not defeated.
|
|
func TestNormalizeSystemMessagesDedupesIdentical(t *testing.T) {
|
|
sys := "You are Dante. Big system prompt with memory."
|
|
msgs := []openai.ChatCompletionMessage{
|
|
{Role: "system", Content: sys},
|
|
{Role: "user", Content: "hi"},
|
|
{Role: "assistant", Content: "hello"},
|
|
{Role: "system", Content: sys}, // turn 2's duplicate
|
|
{Role: "user", Content: "again"},
|
|
{Role: "system", Content: sys}, // turn 3's duplicate
|
|
{Role: "user", Content: "third"},
|
|
}
|
|
out := normalizeSystemMessages(msgs)
|
|
sysCount := 0
|
|
for _, m := range out {
|
|
if m.Role == "system" {
|
|
sysCount++
|
|
}
|
|
}
|
|
if sysCount != 1 {
|
|
t.Fatalf("expected exactly 1 system message after dedupe, got %d", sysCount)
|
|
}
|
|
if out[0].Role != "system" || out[0].Content != sys {
|
|
t.Fatalf("system block should be hoisted to position 0 with the single prompt, got %q / %q", out[0].Role, out[0].Content)
|
|
}
|
|
// The 3 user + 1 assistant messages must all survive.
|
|
nonSys := 0
|
|
for _, m := range out {
|
|
if m.Role != "system" {
|
|
nonSys++
|
|
}
|
|
}
|
|
if nonSys != 4 {
|
|
t.Fatalf("expected 4 non-system messages preserved, got %d", nonSys)
|
|
}
|
|
}
|
|
|
|
// Distinct system messages (e.g. the force-text-reply directive + the real
|
|
// prompt) must both be kept, joined — dedupe must not drop different content.
|
|
func TestNormalizeSystemMessagesKeepsDistinct(t *testing.T) {
|
|
out := normalizeSystemMessages([]openai.ChatCompletionMessage{
|
|
{Role: "system", Content: "force text reply"},
|
|
{Role: "user", Content: "hi"},
|
|
{Role: "system", Content: "real system prompt"},
|
|
})
|
|
if out[0].Role != "system" {
|
|
t.Fatalf("position 0 must be the merged system block")
|
|
}
|
|
if !strings.Contains(out[0].Content, "force text reply") || !strings.Contains(out[0].Content, "real system prompt") {
|
|
t.Fatalf("both distinct system parts must survive, got %q", out[0].Content)
|
|
}
|
|
}
|