magpie icon indicating copy to clipboard operation
magpie copied to clipboard

routing: a free seat inside a nested group loses its free flag, and its row reads "100% used" again

Open MYCF711 opened this issue 1 month ago • 4 comments

What happens

On the Routing page a seat serving a model its vendor lists at no cost loses Weighed.Free when it sits inside a nested group, so its row goes back to "100% used · all but used up" and keeps the low colour (--fg-2, rgb(75,75,83) in light) — the exact reading b9ab1723 (#3763) set out to fix. The same seat in a flat group reads "free · costs nothing".

Why

Weighed.Routing is the seat's provider's routing (Routing: p.Routing in weighed()), and wg.free is built only where routes.Find(p.Routing).ByFree holds — weighRouted, if st.ByFree.

With a group set to free whose members are groups left on the default (no routing), each seat inside those members is planned by its own group's routing (planLevel recursing, #576). That routing is "", routes.Find("") is Smart, ByFree is false, so wg.free is never created and Weighed.Free stays false. The page then falls to the w.known branch, and low — !w.routing && w.known && w.used >= 90 — is added again because w.routing is empty too.

b9ab1723's message says "a real free seat always carries its routing". True for a flat group; not for a nested one.

Measured

Three groups, one free model (free: true in the vendor's list) and one priced, quoted from Weighed as the gateway put it in the trace:

group shape the free seat a paid seat beside it
flat, routing: free routing="free" free=true routing="free" free=false
routing: free, member group/inner on the default routing="" free=false routing="" free=false
routing: free, member group/inner also free routing="free" free=true routing="free" free=false

The same holds with affinity: session on the inner group, and with a member whose context window the request is longer than (left carries window, so #3749's fix does reach inside — only free is lost).

The group I run into this with:

deepseek-v4.1-flash1017   routing: free
  ├ group/model-free      routing unset (Smart)   ← OpenCode Zen's free DeepSeek is in here
  └ group/limited         routing unset (Smart)

Its rows read "已用 100% · 几乎用尽", not "免费 · 不花额度".

Reproducing it

internal/gateway, on 508911e4 — about 40 lines, no fixtures beyond the existing scripted/fresh helpers:

func liveFree(t *testing.T, id string, free bool) {
	t.Helper()
	body := `{"models":[{"id":"m","free":true}]}`
	if !free {
		body = `{"models":[{"id":"m","cost":{"input":3,"output":15}}]}`
	}
	if err := os.MkdirAll(filepath.Dir(catalog.LivePath(id)), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(catalog.LivePath(id), []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
	catalog.Reset()
}

then two provider.Saved providers (zenfree/m free, paid/m priced), provider.SaveGroup({Name:"inner", Members:["zenfree/m","paid/m"]}) — no routing — and provider.SaveGroup({Name:"outer", Members:["group/inner"], Routing:"free"}); postAs a request for group/outer and read lastRoute(s).Order: Provider == "zenfree" comes back with Free:false, Routing:"". Set the inner group's Routing:"free" and it comes back Free:true.

The shape of a fix

Yours to decide — I am not sending a patch for this one. The two readings I can see:

  1. weighRouted fills free for a routing that weighs by it, and a group whose members are groups is weighed by the outer group's routing for its heads only. A seat inside keeps its own group's — so the flag, and #3763's row with it, stop at the group boundary.
  2. Or Free first is meant to be set on the group that holds the providers, not on one that holds groups — in which case the Routing page should say so when it is set on a group of groups, rather than silently reading as In order for free purposes.

Either is fine by me; (2) at least would have saved me an hour of thinking the flag was broken. Thanks.

MYCF711 avatar Oct 27 '26 05:10 MYCF711

Overturning my own reading (2) above, after checking how the same question is already answered one level down.

ownRouting(p, g) is p.Routing != "" && p.Routing != g.Routing: a provider left on the default (empty) is not routed on its own — it is weighed with the group and takes the group's routing. That rule is applied wherever a provider is a member (fallback.go:516, affinity.go:598).

A group that is a member gets no such rule. planLevel's len(m.Path) > depth+1 branch recurses into planLevel(m.Via[depth], ...) unconditionally, so the inner group's routing is whatever it says — and "" is Smart, not the outer group's.

So "left on the default means follow the group" holds for providers and stops holding for groups. Reading (2) in my first message — that Free first belongs on the group that holds the providers — was me describing a workaround as if it were the design. It isn't: one user-visible setting means two different things depending on whether a member is a provider or a group, and nothing in the Routing page says which one you got.

What I expected, and what b9ab1723's "a real free seat always carries its routing" assumes: a group left on the default is weighed with the outer group, exactly as a provider left on the default already is. Then the outer free reaches the seats inside, Weighed.Free is filled where the vendor says the model is free, and #3763's row holds at any depth — and #3763's second sentence, "a real free seat always carries its routing", becomes true rather than true-only-for-flat-groups.

Still yours to decide: #576's "a group in a group is planned by its own routing" is deliberate, and this is its edge. But if it stays as it is, the page should say so where the setting is made — something like "Free first here orders the members' first seats; the seats inside a member are ordered by that member's own routing" — because I could not tell from the UI that the setting was only half-applied to my group. I set Free first, saw the free model still at the back, and went looking for a bug in the model list.

MYCF711 avatar Oct 27 '26 06:10 MYCF711

Update after reading v0.1.1767 (I run 0.1.1756; I pulled the tag's tree and diffed the paths below). I have to soften how I framed this report.

groupnest_test.go says it plainly — "A group in a group is routed by its own routing whatever the group's" — and group_own_routing_test.go repeats it as "as a group in the group does". So a group-as-member routing by its own routing, "" included, is a rule with a test behind it, not an oversight. ownRouting (internal/gateway/fallback.go:692) is unchanged:

func ownRouting(p provider.Provider, g provider.Group) bool {
	return p.Routing != "" && p.Routing != g.Routing
}

What still looks worth fixing is the asymmetry, not the routing. One empty field means two different things depending on what the member is:

member Routing = "" means decided by
a provider follows the group's routing ownRouting
a group Smart (routingOf("")) its own

That is what makes a free seat inside a nested group lose Weighed.Free and read "100% used" again: not that the numbers are wrong, but that its routing is Smart where the outer group carries free, and nothing on the page says which of the two readings you got.

I am not asking for the routing to change. What would settle this issue: does an empty routing on a group-as-member mean "follow the group" (and the page simply does not say so), or is Smart-on-a-member the intended reading? If it is the latter I will configure around it — but that only works for someone who already knows the answer.

MYCF711 avatar Oct 27 '26 17:10 MYCF711

8fff54dab (v0.1.1776): a group inside a group, with its routing left blank (Smart), now takes the outer group's routing when the outer one pools its members (Smart, Free first, Fastest and the like). Under an outer group set to In order, By weight or Manual it keeps its own, because those try each member whole. A nested group that sets its own routing always keeps it. The gateway, the trace and the Routing page all read the same walk, so they agree, and the help text on the Routing page says so. TestFreeFirstReachesAGroupInItLeftOnTheDefault is red without the change. Leaving this open until you confirm it on your groups.

yetone avatar Oct 27 '26 20:10 yetone

Upgraded to 0.1.1776 and restarted — magpie --version says 0.1.1776, the gateway answers on :3425, last-version is 0.1.1776.

My group is the shape this covers, though not quite the one I first wrote about — I had moved the outer group off Free first onto Weekly pace and configured around the symptom since reporting:

group/deepseek-v4.1-flash1017   routing: pace
  ├ group/model-free   routing unset (Smart)   ← opencode-zen-free/deepseek-v4.1-flash-free
  └ group/limited      routing unset (Smart)

So I am confirming on the pace shape: both members left on the default, and the outer group pools. Taking the routing itself on your word — that part is the code's, and I asked because I could not read it off the UI, not because I doubted the walk.

Closing. The help sentence on the Routing page is the piece that settles it for anyone who hits this next: it says which of the two readings a blank field gets, which is exactly what was missing when I set Free first and went looking for a bug in the model list.

MYCF711 avatar Oct 27 '26 20:10 MYCF711