返回 DeepSeek-Reasonix
branch_ops_dag_test.go
根目录 / internal / control / branch_ops_dag_test.go
1 package control
2
3 import (
4 "path/filepath"
5 "strings"
6 "testing"
7
8 "reasonix/internal/agent"
9 "reasonix/internal/event"
10 "reasonix/internal/provider"
11 "reasonix/internal/store"
12 )
13
14 func newSchemaTwoBranchController(t *testing.T) (*Controller, *agent.Session, string) {
15 t.Helper()
16 dir := t.TempDir()
17 exec := agent.New(nil, nil, agent.NewSession("sys"), agent.Options{}, event.Discard)
18 sess := exec.Session()
19 sess.Add(provider.Message{Role: provider.RoleUser, Content: "root prompt"})
20 sess.Add(provider.Message{Role: provider.RoleAssistant, Content: "root answer"})
21 c := newOwnedTestController(t, Options{Executor: exec, SessionDir: dir, Label: "test", Sink: event.Discard})
22 path := filepath.Join(dir, "root.jsonl")
23 c.SetSessionPath(path)
24 if err := c.Snapshot(); err != nil {
25 t.Fatal(err)
26 }
27 if _, ok := sess.Head(); !ok {
28 t.Fatal("session must be schema 2 after its first save")
29 }
30 return c, sess, path
31 }
32
33 func TestBranchAndSwitchUseIndependentSessionsForSchemaTwo(t *testing.T) {
34 c, _, path := newSchemaTwoBranchController(t)
35 rootID := agent.BranchID(path)
36 branchPath, err := c.Branch("experiment")
37 if err != nil {
38 t.Fatalf("Branch: %v", err)
39 }
40 if branchPath == "" || branchPath == path || !strings.HasSuffix(branchPath, ".jsonl") {
41 t.Fatalf("Branch must return an independent session path, got %q", branchPath)
42 }
43 if c.SessionPath() != branchPath {
44 t.Fatalf("branch must switch to the child session: %q", c.SessionPath())
45 }
46 entries, _ := filepath.Glob(filepath.Join(filepath.Dir(path), "*.jsonl"))
47 transcripts := 0
48 for _, entry := range entries {
49 if store.IsSessionTranscriptName(filepath.Base(entry)) {
50 transcripts++
51 }
52 }
53 if transcripts != 2 {
54 t.Fatalf("branch transcripts = %v", entries)
55 }
56 c.executor.Session().Add(provider.Message{Role: provider.RoleUser, Content: "on the branch"})
57 if err := c.Snapshot(); err != nil {
58 t.Fatal(err)
59 }
60 branches, err := c.Branches()
61 if err != nil {
62 t.Fatal(err)
63 }
64 var main, branch *agent.BranchInfo
65 for i := range branches {
66 switch branches[i].ID {
67 case rootID:
68 main = &branches[i]
69 case agent.BranchID(branchPath):
70 branch = &branches[i]
71 }
72 }
73 if main == nil || branch == nil {
74 t.Fatalf("branches = %+v, want parent and independent child", branches)
75 }
76 if branch.ParentID != rootID || branch.Name != "experiment" || branch.Path != branchPath || branch.HeadID != "" {
77 t.Fatalf("file branch infos = main %+v branch %+v", main, branch)
78 }
79 tree := c.BranchTreeText()
80 if !strings.Contains(tree, "experiment") {
81 t.Fatalf("tree must list the head:\n%s", tree)
82 }
83 if _, err := c.SwitchBranch(rootID); err != nil {
84 t.Fatalf("SwitchBranch main: %v", err)
85 }
86 if got := len(c.executor.Session().Snapshot()); got != 3 || c.SessionPath() != path {
87 t.Fatalf("after switching back: %d messages path %q", got, c.SessionPath())
88 }
89 if _, err := c.SwitchBranch(agent.BranchID(branchPath)); err != nil {
90 t.Fatalf("SwitchBranch child: %v", err)
91 }
92 if got := c.executor.Session().Snapshot(); len(got) != 4 || got[3].Content != "on the branch" {
93 t.Fatalf("after switching to the child: %+v", got)
94 }
95 reloaded, err := agent.LoadSession(branchPath)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if got := reloaded.Snapshot(); len(got) != 4 || got[3].Content != "on the branch" {
100 t.Fatalf("reloaded child = %+v", got)
101 }
102 }
103
104 func TestForkAtTurnCreatesIndependentSession(t *testing.T) {
105 c, sess, path := newSchemaTwoBranchController(t)
106 // A guarded turn opens a checkpoint boundary the fork can target.
107 c.beginCheckpoint(t.Context(), "second prompt")
108 sess.Add(provider.Message{Role: provider.RoleUser, Content: "second prompt"})
109 sess.Add(provider.Message{Role: provider.RoleAssistant, Content: "second answer"})
110 if err := c.Snapshot(); err != nil {
111 t.Fatal(err)
112 }
113 turn := -1
114 for candidate := range 8 {
115 if c.CheckpointHasBoundary(candidate) {
116 turn = candidate
117 }
118 }
119 if turn < 0 {
120 t.Fatal("no checkpoint boundary recorded")
121 }
122 childPath, err := c.ForkNamed(turn, "")
123 if err != nil {
124 t.Fatalf("ForkNamed: %v", err)
125 }
126 if c.SessionPath() != childPath || childPath == path || !strings.HasSuffix(childPath, ".jsonl") {
127 t.Fatalf("fork must switch to independent child: path %q child %q", c.SessionPath(), childPath)
128 }
129 if got := len(c.executor.Session().Snapshot()); got != 3 {
130 t.Fatalf("forked transcript has %d messages, want the prefix before the turn", got)
131 }
132 parent, err := agent.LoadSession(path)
133 if err != nil {
134 t.Fatalf("load parent after fork: %v", err)
135 }
136 if len(parent.Snapshot()) != 5 {
137 t.Fatalf("parent changed after fork: messages=%d", len(parent.Snapshot()))
138 }
139 if heads, err := agent.ListSessionHeads(path); err != nil || len(heads) != 1 {
140 t.Fatalf("new fork added a writable legacy head: %+v err=%v", heads, err)
141 }
142 }
143
144 func TestFileBranchesOnlyKeepsFileBranchesForSchemaTwo(t *testing.T) {
145 dir := t.TempDir()
146 exec := agent.New(nil, nil, agent.NewSession("sys"), agent.Options{}, event.Discard)
147 exec.Session().Add(provider.Message{Role: provider.RoleUser, Content: "root prompt"})
148 c := newOwnedTestController(t, Options{Executor: exec, SessionDir: dir, Label: "test", Sink: event.Discard, FileBranchesOnly: true})
149 path := filepath.Join(dir, "root.jsonl")
150 c.SetSessionPath(path)
151 if err := c.Snapshot(); err != nil {
152 t.Fatal(err)
153 }
154 branchPath, err := c.Branch("child")
155 if err != nil {
156 t.Fatalf("Branch: %v", err)
157 }
158 if branchPath == path || !strings.HasSuffix(branchPath, ".jsonl") || c.SessionPath() != branchPath {
159 t.Fatalf("FileBranchesOnly must keep file branches: returned %q, session path %q", branchPath, c.SessionPath())
160 }
161 if heads, _ := agent.ListSessionHeads(path); len(heads) != 1 {
162 t.Fatalf("file branch must not add heads to the source log: %+v", heads)
163 }
164 }
165
166 func TestCommitRewindInPlaceForksRewindHeadAndKeepsController(t *testing.T) {
167 c, sess, path := newSchemaTwoBranchController(t)
168 c.beginCheckpoint(t.Context(), "second prompt")
169 sess.Add(provider.Message{Role: provider.RoleUser, Content: "second prompt"})
170 sess.Add(provider.Message{Role: provider.RoleAssistant, Content: "second answer"})
171 if err := c.Snapshot(); err != nil {
172 t.Fatal(err)
173 }
174 turn := -1
175 for candidate := range 8 {
176 if c.CheckpointHasBoundary(candidate) {
177 turn = candidate
178 }
179 }
180 plan, err := c.PrepareRewind(turn, RewindConversation)
181 if err != nil || !plan.CanConversation {
182 t.Fatalf("PrepareRewind = %+v err=%v", plan, err)
183 }
184 result, err := c.CommitRewindInPlace(plan.PlanID)
185 if err != nil || !result.OK || !result.ConversationForked || result.Branch == "" || !strings.HasSuffix(result.Branch, ".jsonl") {
186 t.Fatalf("CommitRewindInPlace = %+v err=%v", result, err)
187 }
188 if c.SessionPath() != result.Branch || len(c.executor.Session().Snapshot()) != 3 {
189 t.Fatalf("controller after in-place rewind: path %q messages %d", c.SessionPath(), len(c.executor.Session().Snapshot()))
190 }
191 if heads, err := agent.ListSessionHeads(path); err != nil || len(heads) != 1 {
192 t.Fatalf("rewind added a writable legacy head: %+v err=%v", heads, err)
193 }
194 }
195
196 func TestBranchTreeMarksTheCurrentHead(t *testing.T) {
197 c, _, path := newSchemaTwoBranchController(t)
198 if got := c.CurrentBranchID(); got != agent.BranchID(path) {
199 t.Fatalf("CurrentBranchID on main = %q, want the file id %q", got, agent.BranchID(path))
200 }
201 branchPath, err := c.Branch("experiment")
202 if err != nil {
203 t.Fatal(err)
204 }
205 if got := c.CurrentBranchID(); got != agent.BranchID(branchPath) {
206 t.Fatalf("CurrentBranchID after Branch = %q, want child %q", got, agent.BranchID(branchPath))
207 }
208 tree := c.BranchTreeText()
209 for line := range strings.SplitSeq(tree, "\n") {
210 if strings.Contains(line, "experiment") != strings.HasSuffix(line, "current") {
211 t.Fatalf("tree marks the wrong branch current:\n%s", tree)
212 }
213 }
214 if _, err := c.SwitchBranch(agent.BranchID(path)); err != nil {
215 t.Fatal(err)
216 }
217 if got := c.CurrentBranchID(); got != agent.BranchID(path) {
218 t.Fatalf("CurrentBranchID back on main = %q", got)
219 }
220 }
221
221 lines GO