Rules: an agent's session-title call rewrites the turn's rule state and reroutes the whole main turn
Rules: an agent's session-title call rewrites the turn's rule state and reroutes the whole main turn
Version: magpie 0.1.4582 (Windows). Qoder Desktop as the agent, its model set to a routing group (group/…, routing=order, stays=off).
What happens
A rule that matches an agent's side call reroutes every later request of the main turn to that rule's model. With Qoder Desktop, its session-title call is enough to poison the turn:
02:05:15 main #1 (turn 1, fresh) → glm-5.3 rule n=0
02:05:20 session-title call → MiniMax rule n=1 (system starts with "You are an expert conversation summarizer")
02:05:29 main #2 (tool results) → MiniMax rule n=1, held=true, within=true
… every later round of the turn → MiniMax, held=true
(from the routing log, ~/.config/magpie/routing/<day>.jsonl)
Why
- Qoder's title call is its own two-message conversation —
[system: "You are an expert conversation summarizer…", user: <the user's message, verbatim>]— so it lands on the same turn-state key as the main conversation. I verified this empirically: a title-style request carrying the same first user message interferes; the same request with different user text does not. turnIn()marks it not-within (own shape), soruleFor()takes the new-turn path and, at the end, writesturnRules.m[key] = {use: <rule 1's use>}.- Every subsequent within request holds that rule — the main turn now goes wherever the title rule pointed.
stays=offdoesn't help; the write is in the new-turn path, not the affinity.
Repro
Three requests to the gateway, same Session-Id, model group/<id> with one rule system starts with "You are an expert conversation summarizer" → <other model>:
- main-style, streaming:
[system A, user U] - while (1) is still streaming:
[system "You are an expert conversation summarizer…", user U]— the same text U,max_tokens: 64 - after (1) completes:
[system A, user U, assistant(tool_calls), tool result]
Expected: (3) → the group's first member. Actual: (3) → the rule's model, held, and so does the rest of the turn.
Suggested fix
Compaction calls are already special-cased (compacting() → the rule is evaluated on its own and returns before the turn-state write, "not the turn's decision"). Session-title calls deserve the same treatment — e.g. a prompt list like compactSystems (Qoder Desktop's opens with "You are an expert conversation summarizer. Your goal is to create a concise, 2-5 word title"), or more generally: a not-within request whose conversation doesn't extend the stored turn's conversation could skip overwriting the turn state.
Workaround
Don't keep any rule a title call can match; it then writes an empty use and within requests fall back to the group's order. (Title calls themselves then ride the main model — 64 output tokens once per session, negligible.)
Prepared a fix on fork branch eisen0419:fix/7455-side-call-rule-state (commit 5a44a71a, rebased onto main e5c2eb94) per docs/contributors.md.
Root cause
Session-title calls and other helper requests share the main turn's ruleKey (group + session + first user words). Because turnIn saw one user message with no tool result, ruleFor treated the side call as a new Turn 1 and overwrote turnRules (and on response updated sticks / ruleAnswered), so later tool rounds of the main turn held the side call's matched rule instead of the main turn's decision.
Summary of changes
internal/gateway/rules.go: Detect side calls (RuleHit.Aside) before the turn's token floor, classifier, orturnRuleswrite:- Known helpers via
req.systemTexts()/ request shape: session titles (Qoder, Claude Desktop, Claude Code, DeepSeek Harness, OpenCode, pi-rename, including after long preambles), Claude CodeAgentSummary,[SUGGESTION MODE:suggestions, auto-mode classifier, and compactions when no rule hasCompact. - Unrecognized tool-less one-message calls whose normalized system prompt (
systemID, stripping billingcch=and<system-reminder>outsideturnRules.Lock()) differs from the stored turn — plus a Turn-1 race re-check underturnRules.Lock()so a concurrent tool-less helper cannot overwrite a main turn that wrote first. - Propagate
Aside/CompactdownnestedRulesso unvisited nested ruled groups take the same side-call path without calling their classifier or writingturnRules, while keeping edits, regenerations,/rewind, retries, and tool-bearing turns after early tool-less calls working as real turns.
- Known helpers via
internal/gateway/gateway.go: PassAside/CompactintonestedRulesand guardansweredStanding,forgetOverflow, andruleAnsweredacross outer and nested keys withroutingAside(hit, nested).docs/subsystems/gateway-routing.md: Document the side-call routing behavior and test coverage.internal/gateway/rules_side_test.go: Add regression tests covering titles before/during/after turns, long preamble blocks, edits/rewinds/retries, transcript-appending helpers, token-floor/effort isolation, nested outer+inner groups (ruled and unruled), and concurrent Turn-1 races.
Verification
gofmt -lclean on all changed Go filesgo test -race ./internal/gateway -run 'TestRule(SideCall|TitleBefore|EditAndRewind|RetryOfAFailed|TranscriptSideCall|NestedSideCall|TitlesAround)' -count=3(and full routing/rules/compact/nested/effort-racesuite) passedgo test -race . ./internal/testenv -run 'TestNotices|TestDocs'&go test -race ./internal/middleware -run TestDocspassedgo vet ./...,GOOS=linux go vet -tags nogui ./..., andGOOS=windows go vet ./...passed