Files
magpie/internal/gateway/compacting.go
T
yetone 639826a89d gateway: a routing group's compaction rule takes Minis's compactions (#1635, jueda12)
Minis (OpenMinis, iOS and Android) compacts with one request and no
tools: its own system prompt ("You are a context compaction engine. Your
summary will REPLACE the original messages in the conversation context
window. ...") and the conversation wrapped in one user message ending
"END OF CONVERSATION TO COMPACT.". Neither was in compactSystems or
compactAsks, so a group's Compact rule never matched it and Minis's
compactions went to the group's ordinary model.

Both are added, keyed on Minis's own text, not its User-Agent (which
Android lets the user change). The prompts were read from OpenMinis at
b4c0661 (Android ChatViewModel.kt compactSummarySystemPrompt and its user
message; iOS AIChatViewModel+Compaction.swift, whose line continuations
render the first paragraph to the same bytes), and the test's fixture is
those literal bytes.

A general signal (no tools plus a summarize request) was considered and
not taken: an ordinary tool-less chat asking for a summary would be sent
to the compaction model too.

Run: go test -tags nogui ./internal/gateway, gofmt -l, GOOS=linux and
GOOS=windows go vet. With the two entries removed, TestCompactingMinis
fails for all four compaction cases.
2026-10-11 23:37:34 +08:00

67 lines
2.2 KiB
Go

package gateway
import "strings"
// An agent compacting its conversation asks a model to summarize it, with
// a prompt of its own in the system prompt or as the last user message; a
// rule may send that request to a cheaper, faster model (provider.Rule's
// Compact). The prompts are the agents' own, read from each one's release.
// compactSystems open (or are) the system prompt of a compaction request.
var compactSystems = []string{
// Claude Code 2.1, and OpenCode's before its own agent
"You are a helpful AI assistant tasked with summarizing conversations.",
// OpenCode's compaction agent (agent/prompt/compaction.txt)
"You are a context summarization agent.",
// Pi
"You are a context summarization assistant.",
// Gemini CLI
"You are a specialized system component responsible for distilling chat history into a structured XML <state_snapshot>",
// Qwen Code
"You are the component that summarizes a conversation when its context window is about to overflow.",
// Minis (OpenMinis), iOS and Android alike (compactSummarySystemPrompt)
"You are a context compaction engine. Your summary will REPLACE the original messages in the conversation context window.",
}
// compactAsks are in the last user message of a compaction request.
var compactAsks = []string{
// Claude Code's /compact and auto-compact
"Your task is to create a detailed summary of the conversation so far",
// Codex (and magpie's own for a Codex compaction_trigger)
"You are performing a CONTEXT CHECKPOINT COMPACTION.",
// Kimi Code (prompts/compact.md)
"You are now given a task to compact this conversation context",
// Minis, around the conversation it sends as one user message
"END OF CONVERSATION TO COMPACT.",
}
// compacting reports whether req is an agent compacting its conversation.
func compacting(req *Request) bool {
if req == nil {
return false
}
for _, p := range compactSystems {
if strings.Contains(req.System, p) {
return true
}
}
for i := len(req.Messages) - 1; i >= 0; i-- {
m := req.Messages[i]
if m.Role != "user" {
continue
}
for _, p := range m.Parts {
if p.Kind != Text {
continue
}
for _, a := range compactAsks {
if strings.Contains(p.Text, a) {
return true
}
}
}
break
}
return false
}