magpie icon indicating copy to clipboard operation
magpie copied to clipboard

The same model reports a different contextWindow alone and inside a routing group — the agent then reassembles its whole history

Open MYCF711 opened this issue 1 month ago • 4 comments

Filed first at magpie-community/plugins#402 — the reasoning there was about a plugin's declared size, but the code that decides it is here (internal/provider/group.go, largestWindow / buildGroupEntries), so it belongs here. Closing that one is fine.

What happens

The same model reports one contextWindow when asked for alone, another when it is a member of a routing group. An agent that sees the number change reassembles its whole history, and the next request carries two histories at once.

Measured on DSH (this machine, magpie 0.1.1559):

normal turn    msgs=558   chars=628,684    user=33  assistant=270  tool=254
at the switch  msgs=1175  chars=4,022,216  user=119 assistant=558  tool=497

The counts are the two histories added exactly (33+86=119, 270+288=558) and the characters grow 6.4×. The upstream answers 400 内容太长了, and four seconds later it settles back to 560 messages.

Where the two numbers come from

One model, three numbers on disk right now:

source value
the plugin's declaration (plugin-providers.json) 1048576
what the user typed on the provider card (providers.json, contexts: {"*":1000000}) 1000000
what a group with no size of its own uses 1048576 ← the declaration

So loomy/DeepSeek-V4.1-Flash alone reports 1000000, while the group deepseek-v4.1-flash1017 reports 1048576 — same model, two answers.

GET /v1/models
  loomy/DeepSeek-V4.1-Flash       1000000
  group/deepseek-v4.1-flash1017   1048576   ← before I pinned the group by hand

The code

buildGroupEntries takes the largest member's window, and carries the member's WindowSet (whether the user typed that member's size) along with it:

// internal/provider/group.go
if ctx > e.Context {
    e.Context, e.WindowSet = ctx, typed
} else if ctx > 0 && ctx == e.Context {
    e.WindowSet = e.WindowSet || typed
}

ctx comes from entries[j].Context, and that entry is the catalog declaration — the user's card value (contexts) is not what ctx holds here. So a group falls back to the declared size even when the user has overridden it for every member, and the two paths disagree.

Why it matters beyond the cosmetic

The agent's compaction is driven by the number it was told (Entry.WindowSet even has a rule in compact.go). When a group's figure is the declared one and the model's own is the typed one, routing a turn from the group to the model — or just switching which one the agent picks — changes the budget under the conversation. On a 1M-window model the gap here is 48,576 tokens, which is not enough to break a turn by itself; what breaks it is the agent re-sending its history when it believes the window grew.

What I did meanwhile

Pinned the group's own context to 1000000 by hand, which makes both paths agree. That hides the bug rather than fixing it: set the group back to "the largest of its members" and the two numbers diverge again.

Possibly the right fix

Have the member's effective window — the typed value when there is one, else the declaration — be what largestWindow compares, the same way smallestWindowBy already reads entries[j].Context together with WindowSet. Then a group's default and its member's own answer are the same number by construction.

MYCF711 avatar Oct 26 '26 00:10 MYCF711

Before this goes further, the part I do not dispute: a group reporting its largest member's window is correct, and buildGroupEntries gives the reason — #700 / #712, a conversation too long for one member moves to a member with room, so the agent compacts only when none has room. I am not asking to change that.

What I am reporting is the agent's side of it. The agent cannot tell "the same model, described differently" from "a different model", so when the number changes it reassembles the conversation. Measured here (DSH, magpie 0.1.1559 at the time):

normal turn    msgs=558   chars=628,684    user=33  assistant=270  tool=254
at the switch  msgs=1175  chars=4,022,216  user=119 assistant=558  tool=497

The counts are two histories added exactly (33+86=119, 270+288=558), 6.4x the characters; the upstream answered 400 内容太长了, and four seconds later it settled back to 560 messages.

So the question is not whether a group should report the largest member — it should — but whether the number told to the agent may change for the same model when it enters a group. If it may, then the agent needs something that tells it the model did not change.

MYCF711 avatar Oct 27 '26 17:10 MYCF711

Update, 19fafad16 (in v0.1.1776; only a test):

  • The window. On main the member's entry is already the window you typed (WindowOf, since #2112 / 7aad6a50e), and buildGroupEntries reads that entry. So a group that names no size and holds only loomy/DeepSeek-V4.1-Flash (*=1000000, plugin declares 1048576) is told 1000000, the same as the model alone. The new test pins that. If your group still shows 1048576 with no size of its own, another member of it has 1048576 (by #700/#712 it reports the largest). Could you share the group's member list?
  • The doubling. We couldn't reproduce it. In a sandbox, dsh 0.2.1-rc.2 against a fake server, we flipped the window 1000000 ↔ 1048576 on resume, live (with reload on and off), mid-request and after a compaction, and flipped the model id between the group and its member. Every request grew only by its own two messages. dsh builds each request from session.deriveMessages() alone, and a window change only adds a request/context event.
  • What would help. For the 1175-message request: the first and last user text, whether it came from dsh desktop/web or headless, and whether magpie retried or fell over to another member just before it. If the last user text begins "You are now acting…", it was dsh's compaction request.

Leaving this open.

yetone avatar Oct 27 '26 20:10 yetone

Two answers — the member list you asked for, and a withdrawal of my own evidence.

1 · The group, read from the live trace (/v1/magpie/trace, group/deepseek-v4.1-flash1017):

members: opencode-zen-free/deepseek-v4.1-flash-free
         workbuddy-ai/deepseek-v4.1-flash
         qoder/dfmodel
         magicrew-intl/deepseek-v4.1-flash
         magicrew/deepseek-v4.1-flash
         loomy/DeepSeek-V4.1-Flash
         officeace/deepseek-v4.1-flash
         workbuddy/deepseek-v4.1-flash
         codearts/deepseek-v4.1-flash-1017
         raccoon/sn-deepseek-v4-1-flash-1017
nested:  group/model-free (routing "fast")
         group/limited (routing "")

So yes, it has a member at 1048576: loomy/DeepSeek-V4.1-Flash. Our Loomy plugin declares 1048576 for it (magpie-loomy-auth 0.1.18, src/models.mjs, with a comment saying the number must be reported as the vendor's, not lowered "to be safe"). The group reports the largest member's window, which is what #700/#712 want — I am not asking to change that.

I had pinned the group's own size to 1000000 by hand, which is why this machine now reads 1000000 both alone and in the group. Unpinned, the group would read 1048576 and the member 1000000 — the divergence in the report was real, and its cause is the one you named: another member (or the declaration) has the larger number.

2 · The 1175-message request: I withdraw it as evidence here.

I could not reproduce it either, and I have since traced the doubled history on this machine to DSH, not to magpie: two sessions were running over the same conversation at that moment and each sent its own history. That is a harness behaviour (a session fork keeps both sides alive), and it is out of scope for this issue. So the measured 558 → 1175 pair should not be read as magpie reassembling anything. Sorry for the detour it caused.

What is left of this issue, as I see it: the group's number may change for the same model when it enters a group (largest member), and an agent that treats a changed window as a changed model has no way to know the model did not change. That is a question about the contract, not about buildGroupEntries. Do you want a marker on /v1/models (or in the response) that says "same model, window from the group", or is it the agent's job to key on the model id alone? Either answer closes this for me.

MYCF711 avatar Oct 29 '26 00:10 MYCF711

Answered: the group's id is its own id and its window comes from the group, no marker on /v1/models — the agent keys on the model id alone. That settles the contract question this issue was left open for. Closing.

MYCF711 avatar Nov 04 '26 12:11 MYCF711