Files
magpie/internal/gateway/compacting.go
T
yetoneandjueda12 743e330085 gateway: a system prompt or message that only quotes a compaction prompt is no longer taken for a compaction; the prompt must lead (#1635, jueda12)
What: compacting() looked for an agent's compaction prompt anywhere in the
system prompt (strings.Contains) and its ask anywhere in the last user
message. Minis puts the user's memory after its own prompt in every request,
so a memory that held Minis's compaction line made every turn of that
conversation a "compaction", routed to the compaction rule's model (28
requests in a row in the report, one with 4 tokens of chat). Now:
- a compactSystems prompt counts only when it begins within compactLead
  (2048 bytes) of the system prompt's start: alone, or behind a short lead
  (Claude Code's billing header and identity line, which its Anthropic
  system blocks join with no separator, so a line-start check would miss it);
- a compactAsks ask counts only when it begins within compactLead of a text
  part of the last user message (Claude Code 2.1.296 puts its ~420-byte
  "CRITICAL: Respond with TEXT ONLY" preamble first; Codex and Kimi start
  the part with it). Minis's END OF CONVERSATION TO COMPACT. follows the
  whole conversation in one part, so it counts only in a part that opens
  with Minis's own "Compact this conversation into a context summary:".
- Kimi Code's own compaction system prompt (kimi-cli 1.52.0,
  soul/compaction.py COMPACTION_SYSTEM_PROMPT) is added, so Kimi stays found
  without relying on the ask's part alone.

The approach (a leading window instead of the whole prompt) and the test
TestCompactingIgnoresQuotedPromptInSystem are jueda12's. Their sample put the
quote ~30 bytes into a short system prompt, which a leading window still
takes, and no rule can tell that apart from a short real lead; the test puts
the memory behind a long agent prompt, as Minis sends it.

Run (go1.26.8, sandbox HOME, private GOCACHE):
- the prompts were checked against the installed Claude Code 2.1.296
  binary (the hnn preamble, then "Your task is to create a detailed
  summary…") and kimi-cli 1.52.0's compaction.py (prompt in its own part).
- break checks: main's compacting.go -> TestCompactingIgnoresQuotedPromptInSystem
  FAIL (memory, claude code taken; kimi not taken) and
  TestCompactingAsksWhereTheAgentPutsThem FAIL (minis marker quoted, ask deep
  in a paste); leads() without the window -> the same, minus Minis; the
  Minis opens check dropped -> "minis marker quoted: taken for compacting".
  Restored: pass, with TestCompacting, TestCompactingMinis, TestRuleCompact,
  TestCompactionBrokeOffKeepsTheStick.
Not run: a real Minis client (the reporter offered to check with the
memory that still holds the line).

Co-authored-by: jueda12 <180685381+jueda12@users.noreply.github.com>
2026-10-13 05:48:39 +08:00

95 lines
3.8 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.
// compactLead is how far into the system prompt, or into a text part of the
// last user message, an agent's compaction prompt may begin. Each agent puts
// its prompt first or behind a short lead of its own (Claude Code's billing
// header and identity line; its "CRITICAL: Respond with TEXT ONLY" preamble,
// about 420 bytes, before the ask). Further in, it is something quoting the
// prompt: a memory or a note an agent puts in the system prompt behind its
// own, every turn (#1635, jueda12: a Minis memory holding Minis's compaction
// line sent every request of that conversation to the compaction rule's
// model), or a user writing about it.
const compactLead = 2048
// 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.",
// Kimi Code (kimi_cli/soul/compaction.py's COMPACTION_SYSTEM_PROMPT)
"You are a helpful assistant that compacts conversation context.",
}
// compactAsk is said in a text part of the last user message of a
// compaction request: within compactLead of the part's start, or, for an
// agent that sends the conversation ahead of it in the same part, anywhere
// in a part that opens with that agent's own first line (opens).
type compactAsk struct {
ask, opens string
}
// compactAsks are in the last user message of a compaction request.
var compactAsks = []compactAsk{
// Claude Code's /compact and auto-compact
{ask: "Your task is to create a detailed summary of the conversation so far"},
// Codex (and magpie's own for a Codex compaction_trigger)
{ask: "You are performing a CONTEXT CHECKPOINT COMPACTION."},
// Kimi Code (prompts/compact.md, a part of its own after the conversation)
{ask: "You are now given a task to compact this conversation context"},
// Minis, around the conversation it sends as one user message
{ask: "END OF CONVERSATION TO COMPACT.", opens: "Compact this conversation into a context summary:"},
}
// leads reports whether p begins within compactLead bytes of text's start.
func leads(text, p string) bool {
i := strings.Index(text, p)
return i >= 0 && i <= compactLead
}
// 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 leads(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 a.opens == "" && leads(p.Text, a.ask) ||
a.opens != "" && strings.HasPrefix(strings.TrimSpace(p.Text), a.opens) && strings.Contains(p.Text, a.ask) {
return true
}
}
}
break
}
return false
}