magpie icon indicating copy to clipboard operation
magpie copied to clipboard

Rules: an agent's session-title call rewrites the turn's rule state and reroutes the whole main turn

Open theLastWinner opened this issue 2 weeks ago • 1 comments

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

  1. 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.
  2. turnIn() marks it not-within (own shape), so ruleFor() takes the new-turn path and, at the end, writes turnRules.m[key] = {use: <rule 1's use>}.
  3. Every subsequent within request holds that rule — the main turn now goes wherever the title rule pointed. stays=off doesn'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>:

  1. main-style, streaming: [system A, user U]
  2. while (1) is still streaming: [system "You are an expert conversation summarizer…", user U] — the same text U, max_tokens: 64
  3. 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.)

theLastWinner avatar Nov 21 '26 18:11 theLastWinner

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, or turnRules write:
    • Known helpers via req.systemTexts() / request shape: session titles (Qoder, Claude Desktop, Claude Code, DeepSeek Harness, OpenCode, pi-rename, including after long preambles), Claude Code AgentSummary, [SUGGESTION MODE: suggestions, auto-mode classifier, and compactions when no rule has Compact.
    • Unrecognized tool-less one-message calls whose normalized system prompt (systemID, stripping billing cch= and <system-reminder> outside turnRules.Lock()) differs from the stored turn — plus a Turn-1 race re-check under turnRules.Lock() so a concurrent tool-less helper cannot overwrite a main turn that wrote first.
    • Propagate Aside / Compact down nestedRules so unvisited nested ruled groups take the same side-call path without calling their classifier or writing turnRules, while keeping edits, regenerations, /rewind, retries, and tool-bearing turns after early tool-less calls working as real turns.
  • internal/gateway/gateway.go: Pass Aside/Compact into nestedRules and guard answeredStanding, forgetOverflow, and ruleAnswered across outer and nested keys with routingAside(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 -l clean on all changed Go files
  • go test -race ./internal/gateway -run 'TestRule(SideCall|TitleBefore|EditAndRewind|RetryOfAFailed|TranscriptSideCall|NestedSideCall|TitlesAround)' -count=3 (and full routing/rules/compact/nested/effort -race suite) passed
  • go test -race . ./internal/testenv -run 'TestNotices|TestDocs' & go test -race ./internal/middleware -run TestDocs passed
  • go vet ./..., GOOS=linux go vet -tags nogui ./..., and GOOS=windows go vet ./... passed

eisen0419 avatar Nov 21 '26 22:11 eisen0419