返回 DeepSeek-Reasonix
rebuild_live_settings_test.go
根目录 / internal / boot / rebuild_live_settings_test.go
1 package boot
2
3 import (
4 "context"
5 "errors"
6 "math"
7 "reflect"
8 "testing"
9
10 "reasonix/internal/agent"
11 "reasonix/internal/config"
12 "reasonix/internal/provider"
13 )
14
15 func liveSettingsFixture(t *testing.T) (*BuildResult, *config.Config, string) {
16 t.Helper()
17 isolateConfigHome(t)
18 root := robustTempDir(t)
19 t.Chdir(root)
20 writeRuntimeFixture(t, root)
21 writeCompactRatio(t, config.UserConfigPath(), .85)
22 cfg, err := config.LoadModelRuntimeSnapshot(root)
23 if err != nil {
24 t.Fatal(err)
25 }
26 old, err := BuildRuntime(t.Context(), withTestSession(t, Options{WorkspaceRoot: root, ConfigSnapshot: cfg}))
27 if err != nil {
28 t.Fatal(err)
29 }
30 t.Cleanup(old.Controller.Close)
31 return old, cfg, root
32 }
33
34 func TestRebuildFromUnrelatedSettingsReusesController(t *testing.T) {
35 old, cfg, root := liveSettingsFixture(t)
36 cfg.UI.Theme = "light"
37 cfg.Desktop.Theme = "dark"
38 cfg.Desktop.LayoutStyle = "creation"
39 cfg.Notifications.Enabled = !cfg.Notifications.Enabled
40 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root, ConfigSnapshot: cfg})
41 if err != nil {
42 t.Fatal(err)
43 }
44 t.Cleanup(next.Controller.Close)
45 if !next.ReusedController || next.Controller != old.Controller || next.liveSettings != old.liveSettings {
46 t.Fatal("frontend preferences rebuilt the runtime")
47 }
48 if next.Snapshot.CacheHash() != old.Snapshot.CacheHash() || next.Plan.PrefixChanged {
49 t.Fatal("frontend preferences changed provider cache prefix")
50 }
51 }
52
53 func TestLiveCompactRatioFailedNarrowRebuildIsAtomic(t *testing.T) {
54 old, cfg, root := liveSettingsFixture(t)
55 cfg.Agent.CompactRatio = .8
56 generation := old.Controller.RuntimeGeneration()
57 history := old.Controller.History()
58 ctx, cancel := context.WithCancel(t.Context())
59 cancel()
60 next, err := RebuildFrom(ctx, old, Options{WorkspaceRoot: root, ConfigSnapshot: cfg})
61 if !errors.Is(err, context.Canceled) || next != nil {
62 t.Fatalf("cancelled narrow rebuild=%v,%v", next, err)
63 }
64 if old.Controller.CompactRatio() != .85 || old.Controller.RuntimeGeneration() != generation || !reflect.DeepEqual(old.Controller.History(), history) {
65 t.Fatal("failed stage changed live state")
66 }
67 }
68
69 func TestLiveCompactRatioRejectsInvalidSnapshotAndModel(t *testing.T) {
70 old, cfg, root := liveSettingsFixture(t)
71 for _, ratio := range []float64{math.NaN(), math.Inf(1), math.Inf(-1)} {
72 cfg.Agent.CompactRatio = ratio
73 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root, ConfigSnapshot: cfg})
74 if err == nil || next != nil || old.Controller.CompactRatio() != .85 {
75 t.Fatalf("invalid ratio committed: %v,%v", next, err)
76 }
77 }
78 cfg.Agent.CompactRatio = .8
79 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root, ConfigSnapshot: cfg, Model: "missing-model"})
80 if !errors.Is(err, ErrUnknownModel) || next != nil || old.Controller.CompactRatio() != .85 {
81 t.Fatalf("invalid model reused runtime: %v,%v", next, err)
82 }
83 }
84
85 func TestRuntimeConstructionChangeStillReplacesController(t *testing.T) {
86 old, cfg, root := liveSettingsFixture(t)
87 cfg.Agent.Temperature += .1
88 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root, ConfigSnapshot: cfg})
89 if err != nil {
90 t.Fatal(err)
91 }
92 t.Cleanup(next.Controller.Close)
93 if next.ReusedController || next.Controller == old.Controller {
94 t.Fatal("cached agent options silently reused")
95 }
96 }
97
98 func TestSkillOptionsSnapshotLiveCompactRatio(t *testing.T) {
99 live := newLiveRuntimeSettings(.85)
100 factory := live.skillOptions(func(context.Context, int, *provider.Pricing, int, int) agent.Options {
101 return agent.Options{CompactRatio: .85}
102 })
103 before := factory(t.Context(), 0, nil, 100_000, 1)
104 live.apply(.8)
105 after := factory(t.Context(), 0, nil, 100_000, 1)
106 if before.CompactRatio != .85 || after.CompactRatio != .8 || before.CompactRatioSource != nil || after.CompactRatioSource != nil {
107 t.Fatal("skill children did not snapshot threshold at creation")
108 }
109 }
110
111 func TestLiveCompactRatioRepeatedUpdatesKeepController(t *testing.T) {
112 current, cfg, root := liveSettingsFixture(t)
113 original := current.Controller
114 for _, ratio := range []float64{.8, .7, .85} {
115 cfg.Agent.CompactRatio = ratio
116 next, err := RebuildFrom(t.Context(), current, Options{WorkspaceRoot: root, ConfigSnapshot: cfg})
117 if err != nil {
118 t.Fatal(err)
119 }
120 t.Cleanup(next.Controller.Close)
121 if !next.ReusedController || next.Controller != original || original.CompactRatio() != ratio {
122 t.Fatalf("ratio %v did not update in place", ratio)
123 }
124 current = next
125 }
126 }
127
128 func TestRuntimeSelectionSnapshotIsDetached(t *testing.T) {
129 effort := "low"
130 selection := runtimeSelectionFrom(Options{Model: "fixture/model", EffortOverride: &effort})
131 effort = "high"
132 if selection == runtimeSelectionFrom(Options{Model: "fixture/model", EffortOverride: &effort}) {
133 t.Fatal("caller effort mutation aliased build snapshot")
134 }
135 if selection == runtimeSelectionFrom(Options{Model: "fixture/other", EffortOverride: &effort}) {
136 t.Fatal("caller model change did not invalidate selection")
137 }
138 if runtimeSelectionFrom(Options{}) == runtimeSelectionFrom(Options{EffortOverride: new(string)}) {
139 t.Fatal("explicit empty effort lost its distinction from provider default")
140 }
141 }
142
143 func TestLiveCompactRatioExplicitCurrentModelKeepsController(t *testing.T) {
144 old, cfg, root := liveSettingsFixture(t)
145 cfg.Agent.CompactRatio = .8
146 next, err := RebuildFrom(t.Context(), old, Options{WorkspaceRoot: root, ConfigSnapshot: cfg, Model: old.Controller.ModelRef()})
147 if err != nil {
148 t.Fatal(err)
149 }
150 t.Cleanup(next.Controller.Close)
151 if !next.ReusedController || next.Controller != old.Controller || next.Controller.CompactRatio() != .8 {
152 t.Fatal("making the current model explicit rebuilt the session")
153 }
154 }
155
155 lines GO