Commit Graph
3 Commits
Author SHA1 Message Date
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
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
yetone 3fe2ff9958 rules: a routing group's rule can be for the agent compacting its conversation, so a cheaper, faster model writes the summary while the conversation stays on its own (X, MR.Guo: 希望 Magpie 能像 OpenCode 一样支持单独指定 context compression model:当 Agent 需要做上下文压缩时…指定一个更便宜、更快的小模型专门负责总结和压缩历史上下文) — a compaction went to whatever model the conversation was on, the priciest in the group as often as not, and a long session paid for each summary at that price. A rule's new condition, compact (provider.Rule.Compact, "compacting"; magpie group rule add <group> use=<model> compact, compact=yes|no; the GUI editor's 压缩上下文时 / compacting toggle; the TUI hint), matches a request that is an agent compacting, told from the agent's own prompt (internal/gateway/compacting.go, read from each one's release): the system prompt of Claude Code 2.1's /compact and auto-compact, OpenCode's compaction agent, Pi, Gemini CLI's state_snapshot and Qwen Code, or the last user message of Claude Code's, Codex's (and magpie's own for a Codex compaction_trigger) and Kimi Code's; a prompt only quoted earlier in a conversation doesn't count. Only in a group with such a rule is a request looked at this way, so nothing else changes. A compaction is decided on its own, never held by the turn's rule, its classifier never asked; it isn't remembered for the turn, and neither the conversation's affinity nor the turn's size is taken from its answer, so the requests after it go on on the model the turn was on and its cache (without that, the next request of a Claude Code turn followed the summary to the cheap model). A member whose context is less than the compaction is passed over for the next rule that matches, or the group's order (RuleHit.Small). The trace tells it ("The agent is compacting the conversation, so rule 1 sends the summary to …", the models passed over named), and the editor notes a compact rule's model taking less than another member. Docs: start#rules (en, zh). Tested: internal/gateway/compacting_test.go — each agent's prompt taken, a plain and a quoted one not; in a group with rules compact→a/small and tokens≥2000→b/big, a long Claude Code turn on b, its /compact to a (Compact, N=1), the turn's next request back on b held, Codex's compaction to a too; a 70k-token compaction skipping a (64k) for rule 2 with Small [a/small]; a group with no compact rule routing a compaction as before; ParseRule/Line/Conditions/Matches for compact. TestRuleCompact failed with the affinity kept from the summary. internal/gui/tests/routing-compact.test.cjs, Chromium and WebKit, en and zh: the compaction in the trace, the rule's words, the toggle on and off with nothing moved, the room note, a compacting-only rule saved as compact:true and not refused. routing-kind/served/signin/steady, click-scroll and old-webkit pass. go vet, go build -tags nogui (and GOOS=linux, GOOS=windows) and go test -tags nogui ./... pass. Not tried against real agents' compactions through the gateway (the prompts are matched as read from their releases) nor in the real Wails window. 2026-09-30 10:24:18 +08:00