Files
magpie/internal/agent/dsh_compaction_budget_test.go
T
yetone ca4a1cde0d dsh: maxTokens leaves dsh half its window, or a quarter for output where that is more (#3298 item 3)
822f2b304 (#2823) held the maxTokens magpie lists for dsh to a quarter
of the window, because dsh reserves it, plus 65536 of headroom, before
it compacts (compaction-basic's resolveCompactSpec: threshold = min(0.8W,
W - maxTokens - 65536)). On a large window the quarter cut more than
compaction needs: DeepSeek's own deepseek-flash, 1000000/393216, was
listed at 250000, though all 393216 leaves dsh 541248 (54%).

Now the cap is max(W/4, W/2 - 65536): the output may take what leaves
dsh half the window to compact in, or a quarter of the window where that
is more (every window under 262144, where there is no half to spare
beside the headroom). A Max output the user set is still written as set
(outputOr), and contextWindow and /v1/models don't change.

dsh 0.2.0-rc.2's own resolveCompactSpec, run on the installed
compaction-basic over each figure (threshold tokens):

| window / output | vendor | quarter (main) | now |
| 524288 / 524288 | no message budget | 131072 -> 327680 | 196608 -> 262144 |
| 500000 / 393216 (#2823) | 41248 | 125000 -> 309464 | 184464 -> 250000 |
| 131072 / 65536 | no pressure budget | 32768 -> 32768 | 32768 -> 32768 |
| 1000000 / 393216 | 541248 | 250000 -> 684464 | 393216 -> 541248 |
| 400000 / 128000 | 206464 | 100000 -> 234464 | 128000 -> 206464 |
| 200000 / 393216 | no message budget | 50000 -> 84464 | 50000 -> 84464 |
| 128000 / 393216 | no message budget | 32000 -> 30464 | 32000 -> 30464 |

#2823's case stays fixed: 250000 is half its 500000 window (its
reporter's own plan A), against 41248 before 822f2b304; it is lower than
the quarter's 309464, which is the trade. The 128K and 200K windows
#3298 names don't change: any output above the quarter there takes it
straight out of what dsh compacts at, and a user who wants more sets Max
output.

Tests: TestDshOutputLeavesCompactionRoom's table takes the new figures
and adds DeepSeek's 1000000/393216 (393216), 200000 and 128000 windows;
its check now holds dsh's threshold at half the window where the window
has one, never under what a quarter leaves. TestDshModelLimits'
400000/128000 is now 128000; TestMaxTokensWithinContextWindow's dsh
figure follows the formula (its windows are all under 262144). Broken by
hand: the quarter alone (main) is red on 4 cases ("maxTokens 250000;
want ... 393216"), W/2 - 65536 alone on 3 (131072 gets 1), no cap
(pre-#2823) on #2823's own cases (524288/524288, 524288/1048576,
500000/393216, 131072/65536) and the 200K and 128K windows. docs/subsystems/agent-wiring.md says the new rule.

Run at 5adad1877 (this commit on top of item 4's): gofmt -l (nothing),
go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./... (all clean), go build for darwin, linux (nogui) and windows (ok),
and go test -tags nogui -timeout 45m ./...: all 69 packages ok, exit 0
(gateway 505s, agent 401s, provider 197s).

Not tried: a real DSH session against a 1M DeepSeek model through
magpie; the thresholds above come from dsh's resolveCompactSpec run
directly. Windows under ~87K (64000/384000 -> 16000) still leave dsh no
pressure budget, on main too; that needs a policy of its own and isn't
changed here.
2026-10-23 01:20:37 +08:00

76 lines
3.7 KiB
Go

package agent
import (
"fmt"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// dshCompactsAt is where dsh's compaction-basic starts compacting a model
// listed with window and maxTokens out, as its resolveCompactSpec works it
// out with the default policy (thresholdRatio 0.8, headroomTokens 65536):
// 0 when it has no budget and fails instead (#2823).
func dshCompactsAt(window, out int) int {
pressure := window - out - 65536
if window-out <= 0 || pressure <= 0 {
return 0
}
return min(window*8/10, pressure)
}
// TestDshOutputLeavesCompactionRoom: dsh reserves the maxTokens magpie lists
// a model with, and 65536 more, out of its window before it compacts. A
// vendor's reply limit near the window had dsh compact at 41248 of a 500000
// group (#2823) or fail outright on 524288/524288 (LoKi on Discord) and
// 131072/65536; it is held to what leaves dsh half the window
// (window/2 - 65536), or a quarter of the window where that is more, on every
// route and in the older llm-deepseek entry, while a Max output the user set
// stays. DeepSeek's own 1000000/393216 keeps all of its output (#3298).
func TestDshOutputLeavesCompactionRoom(t *testing.T) {
for _, tc := range []struct {
name string
model catalog.Model
want int
}{
{"full window", catalog.Model{ID: "space-bunny-free", Context: 524288, Output: 524288}, 196608},
{"above the window", catalog.Model{ID: "above", Context: 524288, Output: 1048576}, 196608},
{"group window under its members' output", catalog.Model{ID: "group/smart-split", Context: 500000, Output: 393216}, 184464},
{"half the window", catalog.Model{ID: "k131", Context: 131072, Output: 65536}, 32768},
{"within a quarter", catalog.Model{ID: "flash", Context: 1000000, Output: 128000}, 128000},
{"DeepSeek's window and output", catalog.Model{ID: "deepseek/deepseek-flash", Context: 1000000, Output: 393216}, 393216},
{"a 200K window", catalog.Model{ID: "ds200", Context: 200000, Output: 393216}, 50000},
{"a 128K window", catalog.Model{ID: "ds128", Context: 128000, Output: 393216}, 32000},
{"user output", catalog.Model{ID: "user", Context: 524288, Output: 393216, OutputSet: true}, 393216},
{"user full window", catalog.Model{ID: "user-full", Context: 524288, Output: 524288, OutputSet: true}, 524288},
{"unknown window", catalog.Model{ID: "unknown", Output: 128000}, 128000},
{"unknown output", catalog.Model{ID: "no-output", Context: 524288}, 0},
} {
t.Run(tc.name, func(t *testing.T) {
for _, k := range dshKinds {
got := dshKindConfig(k, []catalog.Model{tc.model}, "", "http://127.0.0.1:3425").Models[0]
if got.MaxTokens != tc.want || got.ContextWindow != tc.model.Context {
t.Fatalf("%s: contextWindow %d, maxTokens %d; want %d, %d", k.key, got.ContextWindow, got.MaxTokens, tc.model.Context, tc.want)
}
}
lines := strings.Join(dshProviderLines("high", []catalog.Model{tc.model}, "http://127.0.0.1:3425"), "\n")
if tc.want > 0 && !strings.Contains(lines+"\n", fmt.Sprintf("maxTokens: %d\n", tc.want)) {
t.Fatalf("llm-deepseek entry, want maxTokens: %d:\n%s", tc.want, lines)
}
if tc.want == 0 && strings.Contains(lines, "maxTokens") {
t.Fatalf("llm-deepseek entry lists a maxTokens for a model of no known output:\n%s", lines)
}
if tc.model.Context > 0 && !tc.model.OutputSet {
// half the window for messages where the window has a
// half to spare beside the headroom, and never under
// what a quarter for output leaves
floor := min(tc.model.Context/2, dshCompactsAt(tc.model.Context, min(tc.model.Output, tc.model.Context/4)))
if at := dshCompactsAt(tc.model.Context, tc.want); at == 0 || at < floor {
t.Fatalf("dsh compacts %d/%d at %d tokens, under %d", tc.model.Context, tc.want, at, floor)
}
}
})
}
}