d052fec0egave a dsh fork without a parentSession its own row, named by the reply ids it gets. A call whose reply dsh hasn't written down yet still went by the opening, which the fork copied and only the source owns, so the source's context line climbed with the fork's turns (Flandern1211: the source's card showed the fork's 30,778 while its own last call was 21,944). routeOwners keeps each opening a fork's seed carried a second time (ownerCopied). joinConvs leaves a call that names no session out of both cards (ctxRec.Unmatched) only when its opening is such a shared one, it has a reply id, and it ended within convsKept: the window Routing's looseKeys waits in. A call with no reply id (routes from beforee8df78f33, a dsh that writes none) and one past that window go by their opening as before, so the source keeps its history. An answer that left a call out stands ctxWhileUnmatched (5s) instead of ctxAgain's minute, and only while such a call is inside that window. This answers both review points on 1bce689ed. Ported from Flandern1211's ad461f7d4, with these changes: - ctxRec.End time.Time is now Millis int64. The tab keeps one ctxRec per route in the range (up to 30 days); End took it from 240 to 264 bytes, Millis to 248. - The conflict with #2398's Canceled is resolved by keeping both. - TestDshForkRouteAwaitingItsReplyIsNeithers also has a long call (begun 2m ago, ended 20s ago), and clears Session as well as Unmatched before its second join. - New TestContextHeldAnswerStandsOnlyWhileAReplyIsAwaited covers contextFor's cache: an answer without held stands past 6s, one with held does not, one with held stands for 1s. Run (mote, HOME=$(mktemp -d), -tags nogui): - go test -run 'Dsh|Fork|Routing|Context|Conv' ./internal/gui/, and -run 'Dsh|ResponseID' ./internal/gateway/ ./internal/sessions/: ok. - go test -count=1 ./internal/gui/ ./internal/sessions/: ok (117s, 60s). - go vet ./internal/gui/ for darwin, linux and windows; gofmt -l: clean. - node --test internal/gui/tests/context-*.test.cjs and compact-one-value.test.cjs (11 files): 474 of 474 pass. No GUI file changed; they guard the Context page it draws. - Benchmark (temporary, not committed), 300k dsh routes over 2000 sources and 2000 forks: joinConvs 125-135ms per pass with the change, 140-270ms without; contextOfRecs 130-145ms with, 140-150ms without; allocations the same (108MB per pass). mote was at load ~120. Broken by hand, each red: the age check, the reply-id check, the Unmatched skip (phantom magpie-… row), held never set, held not shortening the cache, Millis ignored, sharedOpening always false. Not tried: a real dsh fork on this side. Flandern1211 measured the pair on their computer: the source kept its 8 pre-#5491 calls, and a second /api/context 6s later took 9ms instead of 575ms. Not changed: Routing still goes by the opening for such a call (its looseKeys retries every 2s), and a fork call whose reply never comes goes back to the source after a minute. Reported-by: Flandern1211 Co-authored-by: Flandern1211 <163982805+Flandern1211@users.noreply.github.com>
78 lines
3.9 KiB
Go
78 lines
3.9 KiB
Go
package gui
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// dsh writes no parentSession for a fork it makes today, so sessions' ForkOf
|
|
// is empty for it: the opening its seed carried (SeedConvs) is what says the
|
|
// fork came from the session it copied, and its own replies are named for it
|
|
// rather than for that session. A reply id of another conversation still goes
|
|
// by that conversation's opening, as the guard for vendors' repeated ids asks.
|
|
func TestDshForkWithoutAParentIsNamedByItsReply(t *testing.T) {
|
|
list := []sessions.Session{
|
|
{Agent: "dsh", ID: "source", Convs: []string{"magpie-open"}},
|
|
{Agent: "dsh", ID: "fork", SeedConvs: []string{"magpie-open"}, ResponseIDs: []string{"chatcmpl-fork"}},
|
|
{Agent: "dsh", ID: "other", Convs: []string{"magpie-other"}, ResponseIDs: []string{"chatcmpl-other"}},
|
|
}
|
|
of := routeOwners(list)
|
|
for _, c := range []struct {
|
|
response, conv, want string
|
|
}{
|
|
{"chatcmpl-fork", "magpie-open", "fork"}, // the fork's own turn
|
|
{"", "magpie-open", "source"}, // the source's, by its opening
|
|
{"chatcmpl-other", "magpie-open", "source"}, // another conversation's reply id
|
|
{"chatcmpl-fork", "", "fork"}, // the reply alone
|
|
} {
|
|
if got := ownerOf("dsh", c.response, c.conv, of); got != c.want {
|
|
t.Errorf("reply %q, opening %q: %q, want %q", c.response, c.conv, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A fork's own request opens as its source's conversation does, and the reply
|
|
// id that names it is read from the fork's file a moment after the gateway
|
|
// ended the request: a call whose reply names no session yet is neither the
|
|
// source's nor the fork's, and the tab leaves it out of both rather than
|
|
// counting it under the source. A call without a reply id — dsh's history
|
|
// before #5491 recorded them — goes by its opening, the source's, as it always
|
|
// did, and so does one old enough that no reply is coming: the source's card
|
|
// keeps its history. The read after the reply is written names the fork.
|
|
func TestDshForkRouteAwaitingItsReplyIsNeithers(t *testing.T) {
|
|
source := sessions.Session{Agent: "dsh", ID: "source", Convs: []string{"magpie-open"}}
|
|
fork := sessions.Session{Agent: "dsh", ID: "fork", SeedConvs: []string{"magpie-open"}}
|
|
list := []sessions.Session{source, fork}
|
|
now := time.Now()
|
|
routes := []ctxRec{
|
|
{Agent: "dsh", Conv: "magpie-open", ResponseID: "chatcmpl-fork", Done: true, Time: now}, // the fork's turn, its reply unread
|
|
{Agent: "dsh", Conv: "magpie-open", Done: true, Time: now}, // no reply id: its opening's
|
|
{Agent: "dsh", Conv: "magpie-open", ResponseID: "chatcmpl-gone", Done: true,
|
|
Time: now.Add(-2 * time.Minute)}, // older than convsKept: no reply is coming
|
|
{Agent: "dsh", Conv: "magpie-open", ResponseID: "chatcmpl-long", Done: true,
|
|
Time: now.Add(-2 * time.Minute), Millis: 100_000}, // begun before the window, ended within it
|
|
}
|
|
joinConvs(routes, func() []sessions.Session { return list })
|
|
if routes[0].Session != "" || !routes[0].Unmatched {
|
|
t.Errorf("the fork's turn, its reply unread: session %q, unmatched %v; want neither's", routes[0].Session, routes[0].Unmatched)
|
|
}
|
|
if routes[3].Session != "" || !routes[3].Unmatched {
|
|
t.Errorf("a long call that ended 20s ago: session %q, unmatched %v; want neither's", routes[3].Session, routes[3].Unmatched)
|
|
}
|
|
for _, i := range []int{1, 2} {
|
|
if routes[i].Session != "source" || routes[i].Unmatched {
|
|
t.Errorf("route %d: session %q, unmatched %v; want the opening's, the source's", i, routes[i].Session, routes[i].Unmatched)
|
|
}
|
|
}
|
|
list[1].ResponseIDs = []string{"chatcmpl-fork"}
|
|
for i := range routes {
|
|
routes[i].Session, routes[i].Unmatched = "", false
|
|
}
|
|
joinConvs(routes, func() []sessions.Session { return list })
|
|
if routes[0].Session != "fork" || routes[0].Unmatched {
|
|
t.Errorf("the fork's own turn: session %q, unmatched %v; want the fork", routes[0].Session, routes[0].Unmatched)
|
|
}
|
|
}
|