Commit Graph
6 Commits
Author SHA1 Message Date
yetone c9d253e4a2 gateway: an agent's compaction is recorded as Compaction, not a plain turn
shangyuan0818 (Discord, v0.1.3354): Claude Code's compactions don't show
in Usage → Requests. Through the gateway they were recorded and counted,
but as ordinary turns (Kind ""), so nothing read as a compaction: the
Compaction purpose listed none, the OTel export skipped them as turns
the transcript has (Claude Code's compaction is skipTranscript, so it
has none), and the reply ended the agent's turn.

The main handler now marks a call compacting() recognises (Claude
Code's summary ask, OpenCode's and the other agents' prompts) as
KindCompact, on every route it serves (messages, chat, responses,
gemini), unless a header already set its kind. The check reads the
facts parse the request already makes (bodyFacts.compact). Codex's
backend route already took the kind from its header; it now uses the
same constant.

Not fixable here: Claude Code used directly (not through magpie) never
writes a compaction's usage to its transcript (only compact_boundary
with token counts before and after), so magpie can't count those.

Run (mote, at this change on 3cabaab54):
- go test -timeout 45m -count=1 ./internal/gateway ./internal/usage ./internal/otlp: ok
- go vet ./internal/gateway, GOOS=linux and GOOS=windows: ok
- gofmt -l internal/: clean
Rebased onto 25eec8f6c (4 commits, 65 lines in internal/gateway, an
images-API change); at the pushed head: go vet ./internal/gateway and
go test ./internal/gateway -run 'Compact' (every compaction test): ok.
Broken by hand: TestCompactionRecordedAsCompaction on the old code is
red in 4 of 5 cases (claude code fork, claude code plain, chat,
responses: recorded as kind "", want "compact"); the plain turn stays "".
Not tried: a real Claude Code /compact through a sandbox gateway, and
Claude Desktop (assumed to be the same, since it runs Claude Code).
2026-11-09 11:37:06 +08:00
yetone 15ffd3b681 gateway: a compaction rule sees Alma's first compaction too (#2833)
Review of e72e97ef9: it knew Alma's compaction only by the ask of a
later one, "Merge the previous summary with the new conversation.".
Alma 0.4.335 (its app.asar, read on this Mac) has two: the first
compaction asks "Here is the full conversation to compact." under the
system prompt "You are compacting a long conversation so it can
continue WITHOUT losing context.", and a merge sends "You are UPDATING
an existing conversation summary ..." as its system prompt. The ask and
both system prompts are now known.

TestRuleCompactAlma also posts the first ask, and each system prompt
with an unrelated user message.

Run on mote: go test -run TestRuleCompactAlma ./internal/gateway, ok.
Broken by hand: the three new entries taken out, and the three new
cases go to the other rule (b/big).
Not tried: a real Alma compaction through magpie.
2026-11-01 10:22:57 +08:00
ggbdpq f0ff905167 gateway: a compaction rule sees Alma's compaction ask (#2833)
What changed: compactAsks in internal/gateway/compacting.go gains Alma's
compaction ask, "Merge the previous summary with the new conversation.",
which opens the last user message of Alma's compaction request. A group's
compact rule now matches it. No compactSystems entry was added: neither the
repository nor the report carries the opening of Alma's compaction system
prompt, so none is invented.

Why: compacting.go matched the compaction prompts of Claude Code, Codex,
OpenCode, Pi, Gemini CLI, Qwen Code, Minis and Kimi Code literally. Alma was
not in the table, so its compaction fell through to the intent classifier and
was routed by another rule's model, making every compaction cost more than
the group's compact rule intended. The ask sits at byte 0 of the user message,
well inside compactLead (2048), so one table entry recognises it.

Test: TestRuleCompactAlma drives a group whose rule 1 is compact and rule 2 is
tokens >= 1 through the gateway. With the fix the Alma request is answered by
rule 1's member and X-Magpie-Model says a/small; without it the request goes
to b/big. The same test asserts a long message that only quotes the ask past
compactLead is not a compaction; raising compactLead makes that assertion
fail, so the bound is what keeps notes from matching.

Run: go test -tags nogui -count=1 -run 'TestCompact|TestCompacting|TestRule'
./internal/gateway/ (ok, 2.839s); go test -tags nogui -count=1
./internal/gateway/ (ok, 344.144s); go test -race -tags nogui -count=1 -run
'TestCompact|TestCompacting' ./internal/gateway/ (ok, 2.172s); gofmt -l
internal/gateway/ (empty); go vet -tags nogui ./internal/gateway/ and with
GOOS=linux and GOOS=windows (all ok); go build -tags nogui ./..., GOOS=linux
go build -tags nogui ./..., and GOOS=windows go build -tags nogui -o /dev/null .
(all ok). Not tried against a real Alma client: the wording is quoted from
the report.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 10:22:56 +08:00
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