dsh's chat and messages routes send no session id, so Routing and Context found a dsh session by the hash of the conversation's opening. A fork repeats its source's opening, so every request the fork made showed under the source, and the fork's name never appeared. Flandern1211's patch, ported: - The gateway keeps the id of the reply it gave the client (Route.ResponseID). It is set from clientResponseID in the same update that marks the route done, so a poll that sees Done also sees the id, with no race against the usage ledger. When magpie translates, it is the id after translation, e.g. chatcmpl-…, not the upstream's msg_…. - sessions reads dsh's replayState.response.responseId into Session.ResponseIDs. Only main files are read, only replies served by the gateway, and only the fork's own replies after its session/end-seed (seedLength for version 1). - Routing (pricedRoutes) and Context (joinConvs) look up a finished request by its reply id first, then by its opening. A request logged before this keeps the opening's session. Changed from the patch: - A reply id names a session over the opening only when the opening is that session's own, or a session it was forked from, or the opening is unknown. Vendors' ids can repeat: an encoder keeps the upstream's id, and Ollama's is chatcmpl-<0..999>. Without the guard, two unrelated conversations could swap names. Session.ForkOf comes from the fork header's parentSession, isSeeded and delegationDepth 0, which is how dsh's buildForkSeed writes it. ownerOf walks that chain, so a fork of a fork works too. - Poll cost: dsh never writes down some reply ids (a title call, a refused turn). Under the patch, each such id made Routing list every session again every 2s, for as long as the route was on the page. Now an id is waited for only up to a minute (convsKept) after its request ended (looseKeys). After that the regular one-minute rebuild covers it. - A page whose loose routes carry only a reply id still builds the map (looseRoute). - The patch's synthetic fork headers gain parentSession, as dsh writes them. - docs/subsystems/gateway-routing.md describes the reply-id join. Tests: - TestDshForkNamedByTheReplyTheGatewayGave sends dsh 0.2.0-rc.2's real captured completions request (testdata/dsh-gateway) through gateway.New() twice, to an Anthropic-only upstream, so the reply is translated. It writes the source's real session file with the first client id, and a fork shaped like dsh's buildForkSeed output with the second. It then checks that pricedRoutes and contextOf put each request under its own session, and an old route without an id under the source. - TestDshForkOfARealSessionListsItsOwnReplies forks the real fixture session: the fork lists only its own reply and ForkOf, the source keeps [c1]. - TestDshReplyIDOfAnotherConversationNamesNoOne covers the guard. - TestRoutingWaitsForAReplyIDOnlyAWhile covers the poll bound. - The patch's own tests: TestDoneRouteKeepsTheClientResponseID (json and stream), TestDshResponseIDs*, TestRoutingTellsAForkByItsReply, TestContextTellsAForkByItsReply. Run on mote (mote-run), at this tree on origin/mainbe5e8f29f. The vet and go test lines below were run again after rebasing one6b9d1199, which changed only the README and library (ok): - gofmt -l internal/ printed nothing. - go vet ./internal/... . is clean. - GOOS=linux go vet -tags nogui and GOOS=windows go vet of internal/{sessions,gui,gateway} are ok. - go test ./internal/sessions ./internal/gateway ./internal/gui -run 'Dsh|ResponseID|Fork|Routing|Context|Conv' -count=1: ok. - GUI: routing-by-key, routing-ctx-sessions, routing-request-metrics, routing-sessions, routing-session-folder, sessions-empty-range, ctx-route, context-map and panel-routing pass in chromium (120/120) and webkit (120/120). Broken by hand, each red, then restored: - guard off: TestDshReplyIDOfAnotherConversationNamesNoOne; - poll bound off: TestRoutingWaitsForAReplyIDOnlyAWhile; - end-seed keeps the inherited reply ids: TestDshResponseIDsSkipTheSeed, TestDshResponseIDsEndSeedAcrossReads and TestDshForkOfARealSessionListsItsOwnReplies; - ForkOf not set: TestDshForkOfARealSessionListsItsOwnReplies, TestDshForkNamedByTheReplyTheGatewayGave, TestRoutingTellsAForkByItsReply and TestContextTellsAForkByItsReply; - gateway keeps no id: TestDoneRouteKeepsTheClientResponseID and TestDshForkNamedByTheReplyTheGatewayGave. Not tried: - a real dsh fork against a running magpie. The session shapes come from the real rc.2 fixture and dsh's fork source. - A request still running, or a failed one, still shows under the source until it has a reply id. The patch says the same. - Delegated subagent files (delegationDepth > 0) are not read for reply ids; they stay under their parent as before. - Not changed: the gateway's turn clock (sessionTurnKey) also falls back to the opening's hash, so a fork and its source running at once share one. Filed as #5496. The full suite runs after the push. Co-authored-by: Flandern1211 <163982805+Flandern1211@users.noreply.github.com> Reported-by: flandern1211_58540 (Discord)
81 lines
2.8 KiB
Go
81 lines
2.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// The Context page names a dsh fork's requests as Routing does: by the reply
|
|
// each got (Route.ResponseID), where the conversation's opening is the
|
|
// source's. A request of the source's own still opens under the source.
|
|
func TestContextTellsAForkByItsReply(t *testing.T) {
|
|
sandboxHome(t)
|
|
home := t.TempDir()
|
|
t.Setenv("DSH_HOME", home)
|
|
sessions.Reset()
|
|
t.Cleanup(sessions.Reset)
|
|
|
|
const opening = `{"type":"user/message","seq":1,"time":1000,"data":{"source":{"kind":"user"},"content":[{"type":"text","text":"same first prompt"}]}}`
|
|
const endSeed = `{"type":"session/end-seed","seq":2,"time":1000,"data":{"inherited":true}}`
|
|
reply := func(seq int, id string) string {
|
|
return `{"type":"assistant/message","seq":` + strconv.Itoa(seq) + `,"time":1000,"data":{"message":{"source":{"provider":"magpie","model":"group/flash","replayState":{"response":{"provider":"magpie","responseId":"` + id + `"}}}}}}`
|
|
}
|
|
write := func(name, header string, lines ...string) {
|
|
dir := filepath.Join(home, "sessions", "proj", name)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b := header + "\n"
|
|
for _, l := range lines {
|
|
b += l + "\n"
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "session.v4.jsonl"), []byte(b), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write("session-source", `{"type":"session","version":4,"id":"session-source","cwd":"/w","createdAt":1000,"delegationDepth":0,"isSeeded":false}`,
|
|
opening, reply(2, "reply-source"))
|
|
write("session-fork", `{"type":"session","version":4,"id":"session-fork","cwd":"/w","createdAt":1000,"delegationDepth":0,"parentSession":"session-source","isSeeded":true}`,
|
|
opening, reply(2, "reply-source"), endSeed, reply(4, "reply-fork"))
|
|
|
|
var openingHash string
|
|
for _, s := range sessions.List(0) {
|
|
if s.ID == "session-source" && len(s.Convs) > 0 {
|
|
openingHash = s.Convs[0]
|
|
}
|
|
}
|
|
if openingHash == "" {
|
|
t.Fatal("the fixture session names no conversation id")
|
|
}
|
|
conv := func(id int64, c string) gateway.Route {
|
|
r := ctxRoute(id, "dsh", "", 20000, int(1000*id), 128000, 0)
|
|
r.Conv = c
|
|
return r
|
|
}
|
|
fork := conv(1, openingHash)
|
|
fork.ResponseID = "reply-fork"
|
|
source := conv(2, openingHash)
|
|
source.ResponseID = "reply-source"
|
|
loose := conv(3, openingHash)
|
|
out := contextOf([]gateway.Route{fork, source, loose}, 7)
|
|
keys := map[string]string{}
|
|
for _, s := range out.Sessions {
|
|
keys[s.Key] = s.Title
|
|
}
|
|
// the fork's own turn is the fork; the source's is the source; the
|
|
// opening alone is the source's, whose it is
|
|
for _, k := range []string{"session-fork", "session-source"} {
|
|
if _, ok := keys[k]; !ok {
|
|
t.Fatalf("no %s among %v", k, keys)
|
|
}
|
|
}
|
|
if len(keys) != 2 {
|
|
t.Fatalf("sessions %v, want the fork and its source", keys)
|
|
}
|
|
}
|