| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/config" |
| 10 | ) |
| 11 | |
| 12 | func projectionConfig(t *testing.T) *config.Config { |
| 13 | t.Helper() |
| 14 | cfg := config.Default() |
| 15 | cfg.Providers = []config.ProviderEntry{{ |
| 16 | Name: "fixture", Kind: "openai", Model: "chat", BaseURL: "https://example.invalid", |
| 17 | APIKeyEnv: "REASONIX_PROJECTION_TEST_KEY", |
| 18 | }} |
| 19 | cfg.DefaultModel = "fixture/chat" |
| 20 | cfg.Agent.CompactRatio = .85 |
| 21 | cfg.FreezeProviderCredentials() |
| 22 | return cfg |
| 23 | } |
| 24 | |
| 25 | func TestRuntimeConfigFingerprintIgnoresPresentation(t *testing.T) { |
| 26 | isolateConfigHome(t) |
| 27 | t.Setenv("REASONIX_PROJECTION_TEST_KEY", "fixture-key") |
| 28 | cases := []struct { |
| 29 | name string |
| 30 | edit func(*config.Config) |
| 31 | }{ |
| 32 | {"compact ratio", func(c *config.Config) { c.Agent.CompactRatio = .8 }}, |
| 33 | {"CLI appearance", func(c *config.Config) { c.UI.Theme = "light" }}, |
| 34 | {"desktop appearance", func(c *config.Config) { c.Desktop.Theme = "light" }}, |
| 35 | {"desktop language", func(c *config.Config) { c.Desktop.Language = "zh" }}, |
| 36 | {"desktop defaults", func(c *config.Config) { c.Desktop.DefaultToolApprovalMode = "read-only" }}, |
| 37 | {"notifications", func(c *config.Config) { c.Notifications.Enabled = !c.Notifications.Enabled }}, |
| 38 | {"telemetry", func(c *config.Config) { c.Telemetry.CLIMetrics = "off" }}, |
| 39 | {"status line", func(c *config.Config) { c.Statusline.Command = "printf test" }}, |
| 40 | {"CLI updates", func(c *config.Config) { c.CLI.UpdateChannel = "test" }}, |
| 41 | {"bot transport", func(c *config.Config) { c.Bot.QueueCap++ }}, |
| 42 | {"serve transport", func(c *config.Config) { c.Serve.BehindProxy = !c.Serve.BehindProxy }}, |
| 43 | {"provider label", func(c *config.Config) { c.Providers[0].DisplayName = "Renamed" }}, |
| 44 | {"provider preset", func(c *config.Config) { c.Providers[0].PresetID = "preset"; c.Providers[0].PresetVersion++ }}, |
| 45 | {"model discovery URL", func(c *config.Config) { c.Providers[0].ModelsURL = "https://example.invalid/models" }}, |
| 46 | } |
| 47 | for _, tc := range cases { |
| 48 | t.Run(tc.name, func(t *testing.T) { |
| 49 | cfg := projectionConfig(t) |
| 50 | before := runtimeConfigFingerprint(cfg) |
| 51 | tc.edit(cfg) |
| 52 | if after := runtimeConfigFingerprint(cfg); before == "" || after != before { |
| 53 | t.Fatal("presentation or live-only change invalidated cached runtime dependencies") |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestRuntimeConfigFingerprintRetainsRuntimeDependencies(t *testing.T) { |
| 60 | isolateConfigHome(t) |
| 61 | t.Setenv("REASONIX_PROJECTION_TEST_KEY", "fixture-key") |
| 62 | cases := []struct { |
| 63 | name string |
| 64 | edit func(*config.Config) |
| 65 | }{ |
| 66 | {"reply language", func(c *config.Config) { c.Language = "zh" }}, |
| 67 | {"prompt", func(c *config.Config) { c.Agent.SystemPrompt += " extra" }}, |
| 68 | {"temperature", func(c *config.Config) { c.Agent.Temperature++ }}, |
| 69 | {"task budget", func(c *config.Config) { c.Agent.TaskCostBudget++ }}, |
| 70 | {"goal budget", func(c *config.Config) { c.Agent.GoalTokenBudget++ }}, |
| 71 | {"keep policy", func(c *config.Config) { c.Agent.Keep = []string{"all"} }}, |
| 72 | {"recent keep", func(c *config.Config) { c.Agent.RecentKeep++ }}, |
| 73 | {"plan command policy", func(c *config.Config) { c.Agent.PlanModeReadOnlyCommands = []string{"git status"} }}, |
| 74 | {"planner model", func(c *config.Config) { c.Agent.PlannerModel = "fixture/planner" }}, |
| 75 | {"child model", func(c *config.Config) { c.Agent.SubagentModel = "fixture/worker" }}, |
| 76 | {"provider access", func(c *config.Config) { c.Desktop.ProviderAccess = []string{} }}, |
| 77 | {"provider endpoint", func(c *config.Config) { c.Providers[0].BaseURL += "/v2" }}, |
| 78 | {"provider limits", func(c *config.Config) { c.Providers[0].ContextWindow++ }}, |
| 79 | {"provider header", func(c *config.Config) { c.Providers[0].Headers = map[string]string{"X-Test": "fixture"} }}, |
| 80 | {"balance endpoint", func(c *config.Config) { c.Providers[0].BalanceURL = "https://example.invalid/balance" }}, |
| 81 | {"credential name", func(c *config.Config) { c.Providers[0].APIKeyEnv = "OTHER_KEY" }}, |
| 82 | {"display currency", func(c *config.Config) { c.Billing.DisplayCurrency = "CNY" }}, |
| 83 | {"legacy currency", func(c *config.Config) { c.Billing.DisplayCurrency = ""; c.Desktop.Currency = "CNY" }}, |
| 84 | {"enabled tools", func(c *config.Config) { c.Tools.Enabled = []string{"read_file"} }}, |
| 85 | {"shell", func(c *config.Config) { c.Tools.Shell.Path = "/fixture/shell" }}, |
| 86 | {"permission rules", func(c *config.Config) { c.Permissions.Deny = []string{"bash"} }}, |
| 87 | {"sandbox roots", func(c *config.Config) { c.Sandbox.AllowWrite = []string{"/fixture/write"} }}, |
| 88 | {"sandbox reads", func(c *config.Config) { c.Sandbox.ForbidRead = []string{"/fixture/secret"} }}, |
| 89 | {"sandbox network", func(c *config.Config) { c.Sandbox.Network = !c.Sandbox.Network }}, |
| 90 | {"subprocess filtering", func(c *config.Config) { c.Secrets.FilterSubprocessEnv = !c.Secrets.FilterSubprocessEnv }}, |
| 91 | {"sensitive files", func(c *config.Config) { c.Secrets.ProtectSensitiveFiles = !c.Secrets.ProtectSensitiveFiles }}, |
| 92 | {"bot credential protection", func(c *config.Config) { c.Bot.QQ.AppSecretEnv = "BOT_FIXTURE_SECRET" }}, |
| 93 | {"proxy", func(c *config.Config) { c.Network.ProxyMode = "off" }}, |
| 94 | {"offline tools", func(c *config.Config) { c.Environment.Offline = !c.Environment.Offline }}, |
| 95 | {"skills", func(c *config.Config) { c.Skills.DisabledSkills = []string{"fixture"} }}, |
| 96 | {"plugins", func(c *config.Config) { c.Plugins = []config.PluginEntry{{Name: "fixture", Command: "fixture"}} }}, |
| 97 | {"language servers", func(c *config.Config) { c.LSP.Enabled = !c.LSP.Enabled }}, |
| 98 | {"browser", func(c *config.Config) { c.Browser.Enabled = !c.Browser.Enabled }}, |
| 99 | {"checkpoint retention", func(c *config.Config) { c.Checkpoints.RetainTurns++ }}, |
| 100 | } |
| 101 | for _, tc := range cases { |
| 102 | t.Run(tc.name, func(t *testing.T) { |
| 103 | cfg := projectionConfig(t) |
| 104 | before := runtimeConfigFingerprint(cfg) |
| 105 | tc.edit(cfg) |
| 106 | if after := runtimeConfigFingerprint(cfg); before == "" || after == "" || after == before { |
| 107 | t.Fatal("changed cached runtime dependency did not invalidate reuse") |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestRuntimeConfigFingerprintRetainsFrozenCredentials(t *testing.T) { |
| 114 | isolateConfigHome(t) |
| 115 | t.Setenv("REASONIX_HOME", t.TempDir()) |
| 116 | writeKey := func(value string) { |
| 117 | t.Helper() |
| 118 | if err := os.WriteFile(config.UserCredentialsPath(), []byte("REASONIX_PROJECTION_TEST_KEY="+value+"\n"), 0o600); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | } |
| 122 | writeKey("fixture-key") |
| 123 | old := projectionConfig(t) |
| 124 | before := runtimeConfigFingerprint(old) |
| 125 | writeKey("rotated-fixture-key") |
| 126 | if runtimeConfigFingerprint(old) != before { |
| 127 | t.Fatal("frozen runtime credential changed with the credential store") |
| 128 | } |
| 129 | if runtimeConfigFingerprint(projectionConfig(t)) == before { |
| 130 | t.Fatal("credential rotation did not invalidate runtime reuse") |
| 131 | } |
| 132 | writeKey("") |
| 133 | if runtimeConfigFingerprint(projectionConfig(t)) == before { |
| 134 | t.Fatal("credential removal did not invalidate runtime reuse") |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestRuntimeConfigFingerprintFailsClosed(t *testing.T) { |
| 139 | isolateConfigHome(t) |
| 140 | cfg := projectionConfig(t) |
| 141 | cfg.Providers[0].ExtraBody = map[string]any{"unsupported": math.NaN()} |
| 142 | if got := runtimeConfigFingerprint(cfg); got != "" { |
| 143 | t.Fatal("unrepresentable provider settings authorized a reusable fingerprint") |
| 144 | } |
| 145 | if got := runtimeConfigFingerprint(nil); got != "" { |
| 146 | t.Fatal("nil configuration authorized a reusable fingerprint") |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestRuntimeConfigFingerprintIncludesExpandedDependencies(t *testing.T) { |
| 151 | isolateConfigHome(t) |
| 152 | cases := []struct { |
| 153 | name string |
| 154 | edit func(*config.Config) |
| 155 | }{ |
| 156 | {"proxy", func(c *config.Config) { c.Network.Proxy.Password = "${REASONIX_PROJECTION_VALUE}" }}, |
| 157 | {"write roots", func(c *config.Config) { c.Sandbox.AllowWrite = []string{"${REASONIX_PROJECTION_VALUE}"} }}, |
| 158 | {"read roots", func(c *config.Config) { c.Sandbox.ForbidRead = []string{"${REASONIX_PROJECTION_VALUE}"} }}, |
| 159 | {"skill roots", func(c *config.Config) { c.Skills.Paths = []string{"${REASONIX_PROJECTION_VALUE}"} }}, |
| 160 | } |
| 161 | for _, tc := range cases { |
| 162 | t.Run(tc.name, func(t *testing.T) { |
| 163 | cfg := projectionConfig(t) |
| 164 | tc.edit(cfg) |
| 165 | t.Setenv("REASONIX_PROJECTION_VALUE", "/fixture/first") |
| 166 | before := runtimeConfigFingerprint(cfg) |
| 167 | t.Setenv("REASONIX_PROJECTION_VALUE", "/fixture/second") |
| 168 | if after := runtimeConfigFingerprint(cfg); before == "" || after == "" || after == before { |
| 169 | t.Fatal("changed expanded dependency did not invalidate runtime reuse") |
| 170 | } |
| 171 | }) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func TestRuntimeConfigFingerprintUsesWorkspacePrompt(t *testing.T) { |
| 176 | isolateConfigHome(t) |
| 177 | root := t.TempDir() |
| 178 | cfg := projectionConfig(t) |
| 179 | cfg.Agent.SystemPromptFile = "prompt.txt" |
| 180 | path := filepath.Join(root, cfg.Agent.SystemPromptFile) |
| 181 | if got := runtimeConfigFingerprint(cfg, root); got != "" { |
| 182 | t.Fatal("missing prompt file authorized runtime reuse") |
| 183 | } |
| 184 | if err := os.WriteFile(path, []byte("first prompt"), 0o600); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | before := runtimeConfigFingerprint(cfg, root) |
| 188 | if err := os.WriteFile(path, []byte("second prompt"), 0o600); err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | if after := runtimeConfigFingerprint(cfg, root); before == "" || after == "" || after == before { |
| 192 | t.Fatal("changed workspace prompt did not invalidate runtime reuse") |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func TestRuntimeConfigFingerprintIncludesCredentialProxy(t *testing.T) { |
| 197 | isolateConfigHome(t) |
| 198 | root := t.TempDir() |
| 199 | cfg := projectionConfig(t) |
| 200 | settings := config.ModelRuntimeSettings{ |
| 201 | ProxyURL: "http://127.0.0.1:1234", Providers: cfg.Providers, |
| 202 | Credentials: map[string]string{"fixture": "fixture-tunnel-token"}, |
| 203 | } |
| 204 | if err := settings.Apply(cfg, root); err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | before := runtimeConfigFingerprint(cfg, root) |
| 208 | settings.ProxyURL = "http://127.0.0.1:1235" |
| 209 | if err := settings.Apply(cfg, root); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | if after := runtimeConfigFingerprint(cfg, root); before == "" || after == "" || after == before { |
| 213 | t.Fatal("changed private credential proxy route did not invalidate runtime reuse") |
| 214 | } |
| 215 | } |
| 216 |