routing: a dsh request names the dsh session its first prompt was read from
changqiu4890 on Discord: Routing -> Requests showed dsh's requests with no session id. dsh 0.2.1's pi-ai routes (openai-completions, anthropic), which magpie configures, send no session header (pi-ai's session affinity is withheld by dsh's compat gate), so a route had only its Conv, the hash of its first user message. The Context page already found dsh's session by that hash (dshConvs); the Routing page grouped by header session only. pricedRoutes now gives a dsh route with no session of its own the dsh session whose Convs hold its Conv (convOwners, shared with the Context page's joinConvs), from an index rebuilt at most every 2s when a conv is missing and every minute otherwise. Grouping and titles (dsh:<id>) follow. A route with its own session, another agent's route, and a conv two sessions share stay as they were. Run (mote-run, this tree): - go test -tags nogui ./internal/gui/ ./internal/sessions/: ok, ok - TestRoutingDshRoutesNameTheirSession and TestContext*: ok - GUI routing-sessions, routing-session-folder, routing-by-key, ctx-route, routing-day-window: Chromium 160/0, WebKit 80/0 - go vet darwin, linux (nogui), windows ./internal/gui; go build all three - gofmt -l clean Broken by hand: `r.Session = owners[k]` -> `_ = owners[k]`; the new test's dsh chat and messages routes went red. Not tried: a real dsh through magpie. A brand-new session's first request shows no session until dsh writes its session file and the index refreshes. The in-flight list (Active, concurrency endpoint) is unchanged.
This commit is contained in:
File diff suppressed because one or more lines are too long
+16
-8
@@ -283,8 +283,22 @@ func joinConvs(routes []ctxRec, list func() []sessions.Session) {
|
||||
if !loose {
|
||||
return
|
||||
}
|
||||
of := convOwners(list())
|
||||
for i, r := range routes {
|
||||
if r.Session == "" && r.Parent == "" && r.Conv != "" {
|
||||
if id := of[r.Agent+"\x00"+r.Conv]; id != "" {
|
||||
routes[i].Session = id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// convOwners is each agent's conversation id (agent, NUL, the gateway's
|
||||
// id) and the session of that agent it was read from (sessions' Convs);
|
||||
// "" for an id two sessions share, which stays as it is.
|
||||
func convOwners(list []sessions.Session) map[string]string {
|
||||
of := map[string]string{}
|
||||
for _, s := range list() {
|
||||
for _, s := range list {
|
||||
for _, c := range s.Convs {
|
||||
k := s.Agent + "\x00" + c
|
||||
if id, ok := of[k]; ok && id != s.ID {
|
||||
@@ -294,13 +308,7 @@ func joinConvs(routes []ctxRec, list func() []sessions.Session) {
|
||||
}
|
||||
}
|
||||
}
|
||||
for i, r := range routes {
|
||||
if r.Session == "" && r.Parent == "" && r.Conv != "" {
|
||||
if id := of[r.Agent+"\x00"+r.Conv]; id != "" {
|
||||
routes[i].Session = id
|
||||
}
|
||||
}
|
||||
}
|
||||
return of
|
||||
}
|
||||
|
||||
// compacted says a prompt is a compaction of the one before it: under 60%
|
||||
|
||||
@@ -96,9 +96,19 @@ func pricedRoutes(routes []gateway.Route) []routeJSON {
|
||||
}
|
||||
}
|
||||
titles := sessions.CodexTitles(ids)
|
||||
var loose []string
|
||||
for _, r := range routes {
|
||||
if k := looseConv(r); k != "" {
|
||||
loose = append(loose, k)
|
||||
}
|
||||
}
|
||||
owners := convSessions(loose)
|
||||
pub := publicRefs()
|
||||
for _, r := range routes {
|
||||
r = publicRoute(r, pub)
|
||||
if k := looseConv(r); k != "" && owners[k] != "" {
|
||||
r.Session = owners[k]
|
||||
}
|
||||
r.Usage = cacheCounted(r.Usage)
|
||||
recs := make([]usage.Record, len(r.Usage))
|
||||
for i, u := range r.Usage {
|
||||
@@ -120,6 +130,58 @@ func pricedRoutes(routes []gateway.Route) []routeJSON {
|
||||
return out
|
||||
}
|
||||
|
||||
// convAgents are the agents whose own session files say which of the
|
||||
// gateway's conversation ids are theirs (sessions' Convs): dsh's, whose
|
||||
// chat-completions and messages routes send no session id
|
||||
// (changqiu4890 on Discord: Routing put every dsh request under "No
|
||||
// session ID").
|
||||
var convAgents = map[string]bool{"dsh": true}
|
||||
|
||||
// looseConv is the agent and conversation id (convOwners' key) of a route
|
||||
// of such an agent that names no session, "" for any other.
|
||||
func looseConv(r gateway.Route) string {
|
||||
if r.Session != "" || r.ParentSession != "" || r.Conv == "" || !convAgents[r.Agent] {
|
||||
return ""
|
||||
}
|
||||
return r.Agent + "\x00" + r.Conv
|
||||
}
|
||||
|
||||
// routeConvs is convOwners of the sessions listed, for every trace the
|
||||
// Routing page polls: listing the sessions reads every agent's folder, so
|
||||
// it is made again only when it is convsKept old, or convsFresh old and a
|
||||
// route's conversation isn't in it (a session begun since).
|
||||
var routeConvs struct {
|
||||
sync.Mutex
|
||||
at time.Time
|
||||
of map[string]string // replaced, never changed: read outside the lock
|
||||
}
|
||||
|
||||
var convsFresh, convsKept = 2 * time.Second, time.Minute
|
||||
|
||||
// convSessions is convOwners for keys (looseConv's), nil when there are
|
||||
// none, so a page with no such route lists nothing.
|
||||
func convSessions(keys []string) map[string]string {
|
||||
if len(keys) == 0 {
|
||||
return nil
|
||||
}
|
||||
routeConvs.Lock()
|
||||
defer routeConvs.Unlock()
|
||||
age := time.Since(routeConvs.at)
|
||||
stale := routeConvs.of == nil || age >= convsKept
|
||||
if !stale && age >= convsFresh {
|
||||
for _, k := range keys {
|
||||
if _, ok := routeConvs.of[k]; !ok {
|
||||
stale = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if stale {
|
||||
routeConvs.of, routeConvs.at = convOwners(sessions.List(0)), time.Now()
|
||||
}
|
||||
return routeConvs.of
|
||||
}
|
||||
|
||||
// cacheCounted marks the usage of a vendor that counts no cache as such,
|
||||
// kept before its plugin said so (usage.CountsNoCache): its hit rate is
|
||||
// none rather than 0% (#3464). us is copied before it is changed.
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/gateway"
|
||||
"github.com/yetone/magpie/internal/sessions"
|
||||
)
|
||||
|
||||
// changqiu4890 on Discord: dsh sends no session id on its chat-completions
|
||||
// and messages routes, so the Routing page's requests list put every dsh
|
||||
// request under "No session ID". The routes it is served name the dsh
|
||||
// session their conversation id was read from, as the Context page does
|
||||
// (TestContextDshSessionTitles); another agent's route, and a conversation
|
||||
// no file holds, stay as they came.
|
||||
func TestRoutingDshRoutesNameTheirSession(t *testing.T) {
|
||||
sandboxHome(t)
|
||||
home := t.TempDir()
|
||||
t.Setenv("DSH_HOME", home)
|
||||
sessions.Reset()
|
||||
t.Cleanup(sessions.Reset)
|
||||
resetRouteConvs := func() {
|
||||
routeConvs.Lock()
|
||||
routeConvs.of, routeConvs.at = nil, time.Time{}
|
||||
routeConvs.Unlock()
|
||||
}
|
||||
resetRouteConvs()
|
||||
t.Cleanup(resetRouteConvs)
|
||||
defer func(f, k time.Duration) { convsFresh, convsKept = f, k }(convsFresh, convsKept)
|
||||
convsFresh = 0
|
||||
|
||||
const chat, msgs = "session-508e7b0a-37c0-41f7-ac62-e48ab3146111", "session-8d93476d-ca34-461c-9e8e-7031c1dedfff"
|
||||
route := func(id int64, agent, conv, session string) gateway.Route {
|
||||
return gateway.Route{ID: id, Agent: agent, Conv: conv, Session: session}
|
||||
}
|
||||
routes := []gateway.Route{
|
||||
route(1, "dsh", "magpie-950998433c3e70e50dc32994", ""), // chat-completions turn
|
||||
route(2, "dsh", "magpie-be31a0561e12cf603d3e79d9", ""), // messages turn
|
||||
route(3, "dsh", "magpie-000000000000000000000000", ""), // no file's
|
||||
route(4, "other", "magpie-950998433c3e70e50dc32994", ""), // not dsh
|
||||
route(5, "dsh", "magpie-950998433c3e70e50dc32994", "own-1"), // names its own
|
||||
}
|
||||
sessionsOf := func() map[int64]string {
|
||||
out := map[int64]string{}
|
||||
for _, r := range pricedRoutes(routes) {
|
||||
out[r.ID] = r.Session
|
||||
}
|
||||
return out
|
||||
}
|
||||
// listed before dsh wrote its files: nothing to name yet
|
||||
if got := sessionsOf(); got[1] != "" || got[2] != "" {
|
||||
t.Fatalf("before the files: %v", got)
|
||||
}
|
||||
fixture := filepath.Join("..", "sessions", "testdata", "dsh-gateway", "sessions")
|
||||
if err := os.CopyFS(filepath.Join(home, "sessions"), os.DirFS(fixture)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessions.Reset()
|
||||
// a session begun since the last listing is found on the next poll
|
||||
got := sessionsOf()
|
||||
want := map[int64]string{1: chat, 2: msgs, 3: "", 4: "", 5: "own-1"}
|
||||
for id, w := range want {
|
||||
if got[id] != w {
|
||||
t.Errorf("route %d: session %q, want %q", id, got[id], w)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user