Files
magpie/internal/usage/purpose_test.go
T
congee949 b6831d83e1 gateway, gui: Codex's thread descriptions go to a model picked in Settings, apart from its titles (#1198, congee949)
Codex asks its own model, through its ChatGPT sign-in, for each thread's
description (thread_description), even while the conversation runs on
magpie's models. Settings › General gets "Codex thread descriptions"
beside "Codex thread titles". It defaults to Codex's own, which sends the
request as Codex sent it, or it can name one of magpie's models or a
routing group. The answer is wrapped as description-only JSON. The titles
setting is untouched. Usage and Routing show these requests under their
own Description purpose. The setting is kept across unrelated Settings
saves. There is no Off yet, because Codex's handling of an empty
description hasn't been checked. This is a first slice of #742.

Squashed from the PR and merged onto current main. Conflicts resolved:
- codex_backend.go: the description branch comes after the title branch,
  ahead of main's subagent pick.
- gateway.go: main's CC Switch #1301 comment above the compaction block is
  kept; the PR had shortened it.
- app.js: the gateway-mode list of hidden rows takes the new row.
- i18n.js and settings_keep_test.go: both sides kept.
- purposes.js: regenerated with go generate ./internal/usage.

Run on the merged tree:
- gofmt -l is clean.
- go vet passes on darwin, linux -tags nogui and windows; the builds on
  all three succeed.
- go test -tags nogui passes for ./internal/gateway, ./internal/gui,
  ./internal/settings and ./internal/usage.
- GUI, chromium and webkit: codex-titles, gateway-mode, purpose-filter,
  routing-purpose-state, gui-de, gui-ja, gui-zh-tw, i18n-keys,
  settings-general/-groups/-sections/-search and click-scroll: 118/118.

Broken by hand: with the description branch removed from codexBackend and
handle, TestCodexDescriptionsSetting, TestCodexDescriptionFallbackOnMagpie
and TestNoDescription fail.

Not tried: a real Codex thread_description request, and whether Codex's
sidebar takes the answer.
2026-10-13 10:13:15 +08:00

79 lines
3.3 KiB
Go

package usage
import (
"slices"
"testing"
"time"
)
func TestPurposeFilter(t *testing.T) {
for _, tc := range []struct {
purpose string
kinds []string
}{
{"kind:thread_title", []string{"thread_title", "thread_title_reconsideration", "title_generation", "title"}},
{"kind:guardian", []string{"guardian", "auto_review", "guardian_review"}},
{"kind:memory_consolidation", []string{"memory_consolidation", "memgen", "memory"}},
{"kind:collab_spawn", []string{"collab_spawn", "thread_spawn", "agent_job"}},
{"kind:ambient_suggestions", []string{"ambient_suggestions", "ambient_suggestion_safety"}},
{"kind:future_kind", []string{"future_kind"}},
{"kind:unmarked", []string{"unmarked"}},
{"unmarked", []string{""}},
} {
t.Run(tc.purpose, func(t *testing.T) {
f := Filter{Purpose: tc.purpose, Agent: "codex", Failed: true}
for _, kind := range tc.kinds {
r := Record{Kind: kind, Agent: "codex", Status: 429}
if !f.keeps(r) {
t.Fatalf("%q did not keep %q", tc.purpose, kind)
}
r.Status = 200
if f.keeps(r) {
t.Fatal("purpose bypassed the failure filter")
}
}
if f.keeps(Record{Kind: "different_unknown", Agent: "codex", Status: 429}) {
t.Fatal("unknown purposes were combined")
}
})
}
}
func TestDescriptionPurposeLabel(t *testing.T) {
p := PurposeKinds()["thread_description"]
if p.Purpose != "kind:thread_description" || p.Name != "Description" {
t.Fatalf("description purpose: %+v", p)
}
}
func TestPurposeRequestPages(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
start := Today.Since(now).Add(time.Second)
kinds := []string{"thread_title", "review", "thread_title_reconsideration", "future_kind", "", "title_generation", "unmarked"}
for i, kind := range kinds {
Append(Record{Time: start.Add(time.Duration(i) * time.Second), Agent: "codex", Provider: "openai", Model: "m",
Kind: kind, Input: 10, Output: 2, CacheRead: 3, CacheWrite: 4, Status: 200})
}
for _, purpose := range []string{"", "kind:thread_title", "kind:review", "kind:future_kind", "unmarked", "kind:unmarked", "kind:absent"} {
for _, f := range []Filter{{Purpose: purpose}, {Purpose: purpose, Provider: "absent"}, {Purpose: purpose, Day: start.Format(time.DateOnly)}} {
for _, offset := range []int{0, 1, 100} {
got := QueryPage(Today, f, offset, 1)
want := pageFromLedger(Today, f, offset, 1, LedgerOf(Today, Filter{}))
equalPage(t, got, want)
}
}
}
first := QueryPage(Today, Filter{Purpose: "kind:thread_title"}, 0, 1)
second := QueryPage(Today, Filter{Purpose: "kind:thread_title"}, 1, 1)
if first.Total != 3 || first.Sum.Calls != 3 || first.Sum.Input != 30 || first.Sum.Output != 6 || first.Sum.CacheRead != 9 || first.Sum.CacheWrite != 12 || first.Sum.Cost <= 0 {
t.Fatalf("title totals: %+v", first)
}
if second.Total != first.Total || second.Sum != first.Sum || len(first.Rows) != 1 || len(second.Rows) != 1 || first.Rows[0].Kind != "title_generation" || second.Rows[0].Kind != "thread_title_reconsideration" {
t.Fatalf("pagination changed totals or raw kinds: first %+v, second %+v", first, second)
}
if !slices.Equal(first.Purposes, []string{"kind:future_kind", "kind:review", "kind:thread_title", "kind:unmarked", "unmarked"}) {
t.Fatalf("filter options lost other purposes: %v", first.Purposes)
}
}