返回 DeepSeek-Reasonix
rebuild_config_test.go
根目录 / internal / boot / rebuild_config_test.go
1 package boot
2
3 import (
4 "errors"
5 "path/filepath"
6 "reflect"
7 "testing"
8
9 "reasonix/internal/config"
10 "reasonix/internal/control"
11 "reasonix/internal/provider"
12 "reasonix/internal/sessioninbox"
13 )
14
15 func TestRebuildFromCompactRatioRefreshesActiveSession(t *testing.T) {
16 isolateConfigHome(t)
17 root := robustTempDir(t)
18 t.Chdir(root)
19 writeRuntimeFixture(t, root)
20 writeCompactRatio(t, config.UserConfigPath(), .85)
21 old, err := BuildRuntime(t.Context(), withTestSession(t, Options{WorkspaceRoot: root}))
22 if err != nil {
23 t.Fatal(err)
24 }
25 t.Cleanup(old.Controller.Close)
26 ctrl := old.Controller
27 ctrl.EnsureSessionPath()
28 service, runtime, bound := ctrl.SessionBinding()
29 ref, ok := ctrl.SessionRef()
30 if !ok || !bound {
31 t.Fatal("missing active session identity")
32 }
33 ctrl.AdoptHistory([]provider.Message{
34 {Role: provider.RoleSystem, Content: systemMessage(ctrl.History())},
35 {Role: provider.RoleUser, Content: "keep my existing conversation"},
36 {Role: provider.RoleAssistant, Content: "existing response"},
37 }, "")
38 ctrl.SetToolApprovalMode(control.ToolApprovalYolo)
39 ctrl.SetPlanMode(true)
40 ctrl.SetGoal("finish this conversation")
41 ctrl.RestoreSessionAuthorizations(control.SessionAuthorizations{
42 Grants: []string{"bash(go test ./...)"}, PlanModeReadOnlyCommands: []string{"git status"},
43 })
44 inboxID := queueRebuildInbox(t, ctrl)
45 history, authorizations := ctrl.History(), ctrl.SessionAuthorizations()
46 if got := ctrl.CompactRatio(); got != .85 {
47 t.Fatalf("initial compact ratio = %v, want .85", got)
48 }
49
50 writeCompactRatio(t, config.UserConfigPath(), .80)
51 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root})
52 if err != nil {
53 t.Fatal(err)
54 }
55 t.Cleanup(next.Controller.Close)
56 if got := next.Controller.CompactRatio(); got != .80 {
57 t.Fatalf("live compact ratio = %v, want saved .80", got)
58 }
59 if !next.ReusedController || next.Controller != ctrl {
60 t.Fatal("compact ratio replaced the active controller")
61 }
62 if got, ok := next.Controller.SessionRef(); !ok || got != ref {
63 t.Fatalf("session identity = %+v, want %+v", got, ref)
64 }
65 if gotService, gotRuntime, ok := next.Controller.SessionBinding(); !ok || gotService != service || gotRuntime != runtime {
66 t.Fatal("compact ratio rebuild replaced the session runtime or its service")
67 }
68 if !reflect.DeepEqual(next.Controller.History(), history) {
69 t.Fatal("compact ratio rebuild changed conversation history")
70 }
71 if !reflect.DeepEqual(next.Controller.SessionAuthorizations(), authorizations) ||
72 next.Controller.ToolApprovalMode() != control.ToolApprovalYolo || !next.Controller.PlanMode() ||
73 next.Controller.Goal() != ctrl.Goal() || next.Controller.SessionTemp() != ctrl.SessionTemp() {
74 t.Fatal("compact ratio rebuild lost session-owned state")
75 }
76 if next.Snapshot.CacheHash() != old.Snapshot.CacheHash() || next.Plan.PrefixChanged ||
77 !reflect.DeepEqual(next.Controller.ToolContractEntries(), ctrl.ToolContractEntries()) {
78 t.Fatal("compact ratio rebuild changed the provider-visible cache prefix")
79 }
80 assertRebuildInbox(t, next.Controller, inboxID)
81 if err := next.Controller.Snapshot(); err != nil {
82 t.Fatalf("live update closed the session writer: %v", err)
83 }
84 unchanged, err := RebuildFrom(t.Context(), next, Options{WorkspaceRoot: root})
85 if err != nil {
86 t.Fatal(err)
87 }
88 if !unchanged.ReusedController || unchanged.Controller != next.Controller {
89 t.Fatal("unchanged effective configuration no longer reuses the controller")
90 }
91 assertRebuildInbox(t, unchanged.Controller, inboxID)
92 }
93
94 func TestRebuildFromCompactRatioHonorsConfigSnapshot(t *testing.T) {
95 isolateConfigHome(t)
96 root := robustTempDir(t)
97 t.Chdir(root)
98 writeRuntimeFixture(t, root)
99 writeCompactRatio(t, config.UserConfigPath(), .85)
100 cfg, err := config.LoadModelRuntimeSnapshot(root)
101 if err != nil {
102 t.Fatal(err)
103 }
104 old, err := BuildRuntime(t.Context(), Options{WorkspaceRoot: root, ConfigSnapshot: cfg})
105 if err != nil {
106 t.Fatal(err)
107 }
108 t.Cleanup(old.Controller.Close)
109 snapshot := *cfg
110 snapshot.Agent.CompactRatio = .80
111 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root, ConfigSnapshot: &snapshot})
112 if err != nil {
113 t.Fatal(err)
114 }
115 t.Cleanup(next.Controller.Close)
116 if got := next.Controller.CompactRatio(); got != .80 {
117 t.Fatalf("snapshot compact ratio = %v, want .80", got)
118 }
119 if got := config.LoadForEdit(config.UserConfigPath()).Agent.CompactRatio; got != .85 {
120 t.Fatalf("snapshot rebuild changed persisted ratio to %v", got)
121 }
122 }
123
124 func TestRebuildFromCompactRatioPreservesProjectOverride(t *testing.T) {
125 isolateConfigHome(t)
126 root := robustTempDir(t)
127 t.Chdir(root)
128 writeRuntimeFixture(t, root)
129 writeCompactRatio(t, config.UserConfigPath(), .85)
130 writeCompactRatio(t, filepath.Join(root, "reasonix.toml"), .70)
131 old, err := BuildRuntime(t.Context(), Options{WorkspaceRoot: root})
132 if err != nil {
133 t.Fatal(err)
134 }
135 t.Cleanup(old.Controller.Close)
136 if got := old.Controller.CompactRatio(); got != .70 {
137 t.Fatalf("initial effective compact ratio = %v, want .70", got)
138 }
139 writeCompactRatio(t, config.UserConfigPath(), .80)
140 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root})
141 if err != nil {
142 t.Fatal(err)
143 }
144 t.Cleanup(next.Controller.Close)
145 if got := next.Controller.CompactRatio(); got != .70 {
146 t.Fatalf("effective compact ratio = %v, want project override .70", got)
147 }
148 if !next.ReusedController || next.Controller != old.Controller {
149 t.Fatal("unchanged project-effective configuration rebuilt the controller")
150 }
151 }
152
153 func TestRebuildFromCompactRatioFailureKeepsController(t *testing.T) {
154 isolateConfigHome(t)
155 root := robustTempDir(t)
156 t.Chdir(root)
157 writeRuntimeFixture(t, root)
158 writeCompactRatio(t, config.UserConfigPath(), .85)
159 old, err := BuildRuntime(t.Context(), withTestSession(t, Options{WorkspaceRoot: root}))
160 if err != nil {
161 t.Fatal(err)
162 }
163 t.Cleanup(old.Controller.Close)
164 old.Controller.EnsureSessionPath()
165 ref, _ := old.Controller.SessionRef()
166 history := old.Controller.History()
167 generation := old.Snapshot.Generation()
168 writeCompactRatio(t, config.UserConfigPath(), .80)
169 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root, Model: "missing-model"})
170 if !errors.Is(err, ErrUnknownModel) || next != nil {
171 t.Fatalf("failed rebuild = %v, %v; want no replacement and unknown model", next, err)
172 }
173 if got, ok := old.Controller.SessionRef(); !ok || got != ref ||
174 !reflect.DeepEqual(old.Controller.History(), history) || old.Controller.CompactRatio() != .85 ||
175 old.Runtime.Closed() || !old.Owner.Gate.AdmitNewWork(generation) {
176 t.Fatal("failed configuration rebuild changed or retired the live session")
177 }
178 writeCompactRatio(t, config.UserConfigPath(), .85)
179 restored, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root})
180 if err != nil {
181 t.Fatal(err)
182 }
183 if !restored.ReusedController || restored.Controller != old.Controller {
184 t.Fatal("failed rebuild replaced the previous configuration snapshot")
185 }
186 }
187
188 func writeCompactRatio(t *testing.T, path string, ratio float64) {
189 t.Helper()
190 cfg := config.LoadForEdit(path)
191 if err := cfg.SetCompactRatio(ratio); err != nil {
192 t.Fatal(err)
193 }
194 if err := cfg.SaveTo(path); err != nil {
195 t.Fatal(err)
196 }
197 }
198
199 func queueRebuildInbox(t *testing.T, ctrl *control.Controller) string {
200 t.Helper()
201 if err := ctrl.SetInboxPaused(true); err != nil {
202 t.Fatal(err)
203 }
204 receipt, err := ctrl.EnqueueInbox(control.InboxRequest{
205 Submit: "continue after rebuilding", Source: "test", Idempotency: "compact-ratio-rebuild",
206 })
207 if err != nil {
208 t.Fatal(err)
209 }
210 return receipt.ItemID
211 }
212
213 func assertRebuildInbox(t *testing.T, ctrl *control.Controller, id string) {
214 t.Helper()
215 snapshot := ctrl.InboxSnapshot()
216 if !snapshot.Paused || len(snapshot.Items) != 1 || snapshot.Items[0].ID != id || snapshot.Items[0].State != sessioninbox.StateQueued {
217 t.Fatalf("rebuild lost queued inbox state: %+v", snapshot)
218 }
219 _, envelope, err := ctrl.ReadInboxItem(id)
220 if err != nil || envelope.SubmitText != "continue after rebuilding" {
221 t.Fatalf("rebuild lost durable inbox body: %+v, %v", envelope, err)
222 }
223 }
224
224 lines GO