dsh: a fork without a parent gets its own row, named by its replies (#5542)
#5491 names a dsh fork's completed requests by the reply id dsh writes with them, over the conversation's opening only where the two agree: the reply's session owns the opening, or is a fork of its owner, which the fork header's parentSession says (Session.ForkOf). dsh 0.2.2's ordinary fork writes isSeeded and session/end-seed but no parentSession. ForkOf is empty, the guard refuses the reply id's own session, and the fork's requests stay under its source on Routing and Context; the fork's own row never appears (Flandern1211: 7 of 17 top-level seeded sessions on one computer are such forks). end-seed now keeps the conversation ids its copied events opened (SeedConvs) instead of dropping them with the seed's counts. That is the fork's own file saying it copied that opening, and forkedFrom accepts it when the fork names no parent, only to admit a reply id that is already one session's. A route with no reply id still goes by the opening, and a reply id two sessions share stays ambiguous. dsh's parse version rises so kept parses are read again; the Routing integration fixture leaves parentSession out, as current dsh does. Flandern1211's fix/dsh-fork-without-parent @ ead1abcce, as written; one conflict taken (parseVersions has antigravity 2 on main). ggbdpq's port on #5542 (0d7e18d1e) is the same change. Not ported: the follow-up fix/dsh-fork-unmatched @ 1bce689ed (Context page leaves a fork-shared opening's unread route out of both cards). Review findings are on #5542. Run at aca932bf7 (this change on5c5160c94), on mote; the pushed head adds onlye20517a88under it, a one-line Qoder mover min: - go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build ./..., GOOS=linux go build -tags nogui ./..., GOOS=windows go build ./...: all ok. - go test -tags nogui -count=1 ./internal/sessions/ ./internal/gui/ ./internal/gateway/: ok (18.8s, 82.8s, 783.9s). - gofmt -l on the six files: nothing. Broken by hand (mote, -run 'Dsh|Fork|Routing|Context'): - end-seed dropping SeedConvs: TestDshForkWithoutAParentKeepsItsSeedOpenings red ("seed openings [], want [magpie-e2f7b8e6… …]") and TestRoutingTellsAForkByItsReply red ("route 1: session \"session-source\", want \"session-fork\""). - forkedFrom ignoring the seed: TestDshForkWithoutAParentIsNamedByItsReply red ("reply \"chatcmpl-fork\", opening \"magpie-open\": \"source\", want \"fork\"") and TestRoutingTellsAForkByItsReply red. - assembleListed not carrying SeedConvs onto the Session: TestRoutingTellsAForkByItsReply red. - Review check: the source's routes from before reply ids were kept (no ResponseID) still make the source's Context card, 3 of 3 requests. Not tried: a replay of real dsh session files (the reporter's numbers are on #5542; ~/.dsh here is a live config and was not read); dsh's parse version bump is not pinned by a test of its own (the generic re-read on a raise is, in cache_decode_test.go). Reported-by: Flandern1211 Co-authored-by: Flandern1211 <163982805+Flandern1211@users.noreply.github.com>
This commit is contained in:
+19
-6
@@ -304,27 +304,33 @@ func joinConvs(routes []ctxRec, list func() []sessions.Session) {
|
||||
// which some give out again (Ollama's chatcmpl-<0-998>), so it names a
|
||||
// session over the opening only where the two agree: the opening is no
|
||||
// one's, or two sessions', or the reply's session's own, or one it was
|
||||
// forked from (sessions' ForkOf). Another session's opening goes by the
|
||||
// opening, as it did before reply ids were read.
|
||||
// forked from (sessions' ForkOf, or the opening its seed carried when the
|
||||
// fork names no parent). Another session's opening goes by the opening, as
|
||||
// it did before reply ids were read.
|
||||
func ownerOf(agent, responseID, conv string, of map[string]string) string {
|
||||
opened := ""
|
||||
if conv != "" {
|
||||
opened = of[ownerKey(agent, ownerConv, conv)]
|
||||
}
|
||||
if responseID != "" {
|
||||
if id := of[ownerKey(agent, ownerResponse, responseID)]; id != "" && (opened == "" || forkedFrom(agent, id, opened, of)) {
|
||||
if id := of[ownerKey(agent, ownerResponse, responseID)]; id != "" && (opened == "" || forkedFrom(agent, id, opened, conv, of)) {
|
||||
return id
|
||||
}
|
||||
}
|
||||
return opened
|
||||
}
|
||||
|
||||
// forkedFrom says session id is from, or a fork of it, or of a fork of it.
|
||||
func forkedFrom(agent, id, from string, of map[string]string) bool {
|
||||
// forkedFrom says session id is from, or a fork of it, or of a fork of it:
|
||||
// by the source its header names (ForkOf), or, for a fork dsh wrote with
|
||||
// none, by the opening conv its seed carried (sessions' SeedConvs).
|
||||
func forkedFrom(agent, id, from, conv string, of map[string]string) bool {
|
||||
for n := 0; id != "" && n < 64; n++ {
|
||||
if id == from {
|
||||
return true
|
||||
}
|
||||
if conv != "" && of[ownerKey(agent, ownerSeed, id+"\x00"+conv)] != "" {
|
||||
return true
|
||||
}
|
||||
id = of[ownerKey(agent, ownerFork, id)]
|
||||
}
|
||||
return false
|
||||
@@ -332,11 +338,13 @@ func forkedFrom(agent, id, from string, of map[string]string) bool {
|
||||
|
||||
// The kinds of id a route without a session id is named by: the reply it
|
||||
// got, and the conversation it opens; and a session's source, for the
|
||||
// session a fork's reply names (ForkOf).
|
||||
// session a fork's reply names (ForkOf), and the opening a fork's seed
|
||||
// carried, kept on the fork itself (SeedConvs).
|
||||
const (
|
||||
ownerResponse = "response"
|
||||
ownerConv = "conv"
|
||||
ownerFork = "fork"
|
||||
ownerSeed = "seed"
|
||||
)
|
||||
|
||||
func ownerKey(agent, kind, id string) string { return agent + "\x00" + kind + "\x00" + id }
|
||||
@@ -365,6 +373,11 @@ func routeOwners(list []sessions.Session) map[string]string {
|
||||
if s.ForkOf != "" {
|
||||
of[ownerKey(s.Agent, ownerFork, s.ID)] = s.ForkOf
|
||||
}
|
||||
for _, c := range s.SeedConvs {
|
||||
// the fork's own record that its seed opened c, the opening
|
||||
// it shares with the session it was copied from
|
||||
of[ownerKey(s.Agent, ownerSeed, s.ID+"\x00"+c)] = s.ID
|
||||
}
|
||||
}
|
||||
return of
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func TestRoutingTellsAForkByItsReply(t *testing.T) {
|
||||
}
|
||||
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}`,
|
||||
write("session-fork", `{"type":"session","version":4,"id":"session-fork","cwd":"/w","createdAt":1000,"delegationDepth":0,"isSeeded":true}`,
|
||||
opening, reply(2, "reply-source"), endSeed, reply(4, "reply-fork"))
|
||||
|
||||
var openingHash string
|
||||
|
||||
@@ -368,8 +368,12 @@ func dshCount(s *state, h *dshHead, b []byte, main bool) {
|
||||
case "session/end-seed":
|
||||
if h.Version >= 2 && h.IsSeeded && e.Data.Inherited {
|
||||
// the events a fork was seeded with end here: what was
|
||||
// counted of them is its source's
|
||||
n := &state{ID: s.ID, Cwd: s.Cwd, Title: s.Title, First: s.First, Named: s.Named, Size: s.Size, Mod: s.Mod}
|
||||
// counted of them is its source's. What they opened is
|
||||
// kept: dsh writes no parentSession for a fork, so the
|
||||
// openings its seed carried are all that says the fork
|
||||
// came from them (SeedConvs)
|
||||
n := &state{ID: s.ID, Cwd: s.Cwd, Title: s.Title, First: s.First, Named: s.Named, Size: s.Size, Mod: s.Mod,
|
||||
SeedConvs: append(append([]string(nil), s.SeedConvs...), s.Convs...)}
|
||||
n.saw(ms(h.CreatedAt), main)
|
||||
*s = *n
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
@@ -187,3 +188,29 @@ func TestDshForkOfARealSessionListsItsOwnReplies(t *testing.T) {
|
||||
t.Errorf("the source is forked from %q", byID[source].ForkOf)
|
||||
}
|
||||
}
|
||||
|
||||
// dsh writes no parentSession for a fork it makes today (0.2.2): the
|
||||
// openings its seed carried are the only record that it came from the
|
||||
// session it copied, and are kept when the seed's end takes them off Convs.
|
||||
func TestDshForkWithoutAParentKeepsItsSeedOpenings(t *testing.T) {
|
||||
head := `{"type":"session","version":4,"id":"session-child","cwd":"/w","createdAt":1000,"delegationDepth":0,"isSeeded":true}`
|
||||
s := dshSessionFile(t, []string{
|
||||
head,
|
||||
`{"type":"user/message","seq":1,"time":1000,"data":{"source":{"kind":"user"},"content":[{"type":"text","text":"same first prompt"}]}}`,
|
||||
`{"type":"session/end-seed","seq":2,"time":1000,"data":{"inherited":true}}`,
|
||||
dshReplyWithID(3, "own"),
|
||||
})
|
||||
want := dshConvs(json.RawMessage(`[{"type":"text","text":"same first prompt"}]`))
|
||||
if len(want) == 0 {
|
||||
t.Fatal("the fixture opens no conversation")
|
||||
}
|
||||
if !slices.Equal(s.SeedConvs, want) {
|
||||
t.Fatalf("seed openings %v, want %v", s.SeedConvs, want)
|
||||
}
|
||||
if len(s.Convs) != 0 {
|
||||
t.Fatalf("the fork keeps its source's opening as its own: %v", s.Convs)
|
||||
}
|
||||
if !slices.Equal(s.ResponseIDs, []string{"own"}) {
|
||||
t.Fatalf("replies %v, want [own]", s.ResponseIDs)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,6 +166,13 @@ type Session struct {
|
||||
// so a reply id that names the fork is taken over the opening only for
|
||||
// a fork of the opening's session (gui's ownerOf)
|
||||
ForkOf string `json:"-"`
|
||||
// SeedConvs are the conversation ids the fork's copied events opened
|
||||
// (dshConvs), which its seed's end takes off Convs: the source's
|
||||
// opening is what a fork and its source share, and dsh's current
|
||||
// forks write no parentSession to say so, so this is how the fork's
|
||||
// own replies are known to belong to a fork of that opening
|
||||
// (gui's ownerOf)
|
||||
SeedConvs []string `json:"-"`
|
||||
// Copies are the ids of the sessions that are copies of its
|
||||
// conversation, not listed apart from it (ccCopies)
|
||||
Copies []string `json:"-"`
|
||||
@@ -229,10 +236,15 @@ type state struct {
|
||||
// gateway wrote them on its routes and the agent kept them with the
|
||||
// reply (dsh's replay state): they name a request after the fact, where
|
||||
// a conversation id is only its opening (Route.ResponseID)
|
||||
ResponseIDs []string `json:"response_ids,omitempty"`
|
||||
Start time.Time `json:"start"`
|
||||
Last time.Time `json:"last"`
|
||||
Models named[Tokens] `json:"models,omitempty"`
|
||||
ResponseIDs []string `json:"response_ids,omitempty"`
|
||||
// SeedConvs are the conversation ids a fork's copied events opened
|
||||
// (dshConvs), kept when its seed's end takes them off Convs: the
|
||||
// opening a fork and its source share is the source's, and this is
|
||||
// the fork's own record that it came from that opening (ownerSeed)
|
||||
SeedConvs []string `json:"seed_convs,omitempty"`
|
||||
Start time.Time `json:"start"`
|
||||
Last time.Time `json:"last"`
|
||||
Models named[Tokens] `json:"models,omitempty"`
|
||||
// Days is Models again, split by the local date each message was
|
||||
// written on: a session that runs past midnight counts on both days.
|
||||
Days named[*day] `json:"days,omitempty"`
|
||||
@@ -394,6 +406,7 @@ func (s *state) clone() *state {
|
||||
}
|
||||
c.Convs = slices.Clone(s.Convs)
|
||||
c.ResponseIDs = slices.Clone(s.ResponseIDs)
|
||||
c.SeedConvs = slices.Clone(s.SeedConvs)
|
||||
c.CodexSpawnNames = maps.Clone(s.CodexSpawnNames)
|
||||
c.CodexSpawnConflicts = maps.Clone(s.CodexSpawnConflicts)
|
||||
c.CodexAgentIDs = maps.Clone(s.CodexAgentIDs)
|
||||
@@ -1163,11 +1176,15 @@ const cacheVersion = 29
|
||||
// dsh 1: the ids of the replies its own requests got (ResponseIDs), for
|
||||
// the routes that carry no session id and an opening a fork copies.
|
||||
//
|
||||
// dsh 2: the openings a fork's seed opened (SeedConvs), which its end
|
||||
// takes off Convs: a fork dsh writes today names no parentSession, so its
|
||||
// own replies are told from its source's by them.
|
||||
//
|
||||
// antigravity 1: a Gemini deployment's name with any one letter after it
|
||||
// (gemini-4-argon-d) is its model's (GeminiDeployment), not -n alone.
|
||||
// antigravity 2: a conversation's folder is its summary row's workspace
|
||||
// (a parse kept from before has none, under the same row revision).
|
||||
var parseVersions = map[string]int{"codex": 3, "zcode": 1, "dsh": 1, "antigravity": 2}
|
||||
var parseVersions = map[string]int{"codex": 3, "zcode": 1, "dsh": 2, "antigravity": 2}
|
||||
|
||||
type cacheFile struct {
|
||||
Version int `json:"version"`
|
||||
@@ -1878,6 +1895,7 @@ func assembleListed(fs []file, price func(string) *catalog.Price, codexTitle fun
|
||||
}
|
||||
s.Convs = append(s.Convs, st.Convs...)
|
||||
s.ResponseIDs = append(s.ResponseIDs, st.ResponseIDs...)
|
||||
s.SeedConvs = append(s.SeedConvs, st.SeedConvs...)
|
||||
if d := st.Dsh; d != nil && d.Head.IsSeeded && d.Head.DelegationDepth == 0 {
|
||||
s.ForkOf = d.Head.ParentSession
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user