diff --git a/internal/gui/context.go b/internal/gui/context.go index fd85fe013..15abf283a 100644 --- a/internal/gui/context.go +++ b/internal/gui/context.go @@ -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 } diff --git a/internal/gui/dsh_fork_seed_test.go b/internal/gui/dsh_fork_seed_test.go new file mode 100644 index 000000000..e96a0213d --- /dev/null +++ b/internal/gui/dsh_fork_seed_test.go @@ -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) + } + } +} diff --git a/internal/gui/trace_dsh_response_test.go b/internal/gui/trace_dsh_response_test.go index 708995a86..f72f3cc23 100644 --- a/internal/gui/trace_dsh_response_test.go +++ b/internal/gui/trace_dsh_response_test.go @@ -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 diff --git a/internal/sessions/dsh.go b/internal/sessions/dsh.go index 799ecafcd..9bbb2d042 100644 --- a/internal/sessions/dsh.go +++ b/internal/sessions/dsh.go @@ -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 diff --git a/internal/sessions/dsh_response_id_test.go b/internal/sessions/dsh_response_id_test.go index 4740de272..ddab92453 100644 --- a/internal/sessions/dsh_response_id_test.go +++ b/internal/sessions/dsh_response_id_test.go @@ -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) + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 9d4a16cac..c2faa4473 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -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 }