| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/agent/testutil" |
| 11 | "reasonix/internal/memory" |
| 12 | "reasonix/internal/provider" |
| 13 | "reasonix/internal/sessioncontext" |
| 14 | ) |
| 15 | |
| 16 | func sessionContextMessage(msgs []provider.Message) string { |
| 17 | for _, message := range msgs { |
| 18 | if sessioncontext.IsContent(message.Content) { |
| 19 | return message.Content |
| 20 | } |
| 21 | } |
| 22 | return "" |
| 23 | } |
| 24 | |
| 25 | // TestBuildComposesByteStableSystemPrompt is the boot-level byte-stability |
| 26 | // guard: two Builds over the same workspace and config must compose the exact |
| 27 | // same system prompt. The system prompt is the provider-cached prefix of every |
| 28 | // request in every session — any byte of nondeterminism here (probe flaps, |
| 29 | // unsorted iteration, time-dependent content) cold-starts the provider cache |
| 30 | // for the whole machine, which is precisely the "desktop costs more" class |
| 31 | // (#2945). Environment probes are covered cross-process by the persisted |
| 32 | // snapshot tests in internal/environment; this test pins the rest of the |
| 33 | // composition (memory, skills index, output style, workspace line, policies). |
| 34 | func TestBuildComposesByteStableSystemPrompt(t *testing.T) { |
| 35 | isolateConfigHome(t) |
| 36 | dir := robustTempDir(t) |
| 37 | t.Chdir(dir) |
| 38 | |
| 39 | writeFile(t, dir, "reasonix.toml", ` |
| 40 | default_model = "test-model" |
| 41 | |
| 42 | [agent] |
| 43 | system_prompt = "BASE SYSTEM PROMPT" |
| 44 | |
| 45 | [[providers]] |
| 46 | name = "test-model" |
| 47 | kind = "openai" |
| 48 | base_url = "https://example.invalid" |
| 49 | model = "x" |
| 50 | api_key_env = "REASONIX_TEST_KEY_UNSET" |
| 51 | `) |
| 52 | writeFile(t, dir, "REASONIX.md", "Project rule: keep the prompt prefix stable.") |
| 53 | |
| 54 | first, err := Build(context.Background(), Options{}) |
| 55 | if err != nil { |
| 56 | t.Fatalf("first Build: %v", err) |
| 57 | } |
| 58 | firstPrompt := systemMessage(first.History()) |
| 59 | first.Close() |
| 60 | if strings.TrimSpace(firstPrompt) == "" { |
| 61 | t.Fatal("first Build composed an empty system prompt") |
| 62 | } |
| 63 | |
| 64 | second, err := Build(context.Background(), Options{}) |
| 65 | if err != nil { |
| 66 | t.Fatalf("second Build: %v", err) |
| 67 | } |
| 68 | secondPrompt := systemMessage(second.History()) |
| 69 | second.Close() |
| 70 | |
| 71 | if firstPrompt != secondPrompt { |
| 72 | t.Fatalf("system prompt is not byte-stable across identical Builds:\nfirst (%d bytes)\nsecond (%d bytes)\nfirst diff site: %q", |
| 73 | len(firstPrompt), len(secondPrompt), firstDivergence(firstPrompt, secondPrompt)) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestBackgroundMemoryAndSkillCatalogChangesDoNotChangeSystemPrompt(t *testing.T) { |
| 78 | isolateConfigHome(t) |
| 79 | dir := robustTempDir(t) |
| 80 | t.Chdir(dir) |
| 81 | writeFile(t, dir, "reasonix.toml", ` |
| 82 | default_model = "test-model" |
| 83 | |
| 84 | [agent] |
| 85 | system_prompt = "STABLE BASE" |
| 86 | |
| 87 | [environment] |
| 88 | enabled = false |
| 89 | |
| 90 | [[providers]] |
| 91 | name = "test-model" |
| 92 | kind = "openai" |
| 93 | base_url = "https://example.invalid" |
| 94 | model = "x" |
| 95 | api_key_env = "REASONIX_TEST_KEY_UNSET" |
| 96 | `) |
| 97 | |
| 98 | buildSystem := func() (*memory.Set, string) { |
| 99 | ctrl, err := Build(context.Background(), Options{}) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | defer ctrl.Close() |
| 104 | return ctrl.Memory(), systemMessage(ctrl.History()) |
| 105 | } |
| 106 | mem, baseline := buildSystem() |
| 107 | if _, err := mem.Store.Save(memory.Memory{ |
| 108 | Name: "dynamic-cache-fact", Description: "background-only fact", |
| 109 | Activation: memory.ActivationRelevant, Body: "secret body", |
| 110 | }); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | skillPath := filepath.Join(dir, ".reasonix", "skills", "dynamic-skill", "SKILL.md") |
| 114 | writeFile(t, dir, ".reasonix/skills/dynamic-skill/SKILL.md", "---\ndescription: dynamic catalog entry\n---\nbody") |
| 115 | |
| 116 | _, afterAdd := buildSystem() |
| 117 | if afterAdd != baseline { |
| 118 | t.Fatalf("background memory/skill addition changed system prompt: %q", firstDivergence(baseline, afterAdd)) |
| 119 | } |
| 120 | if strings.Contains(afterAdd, "dynamic-cache-fact") || strings.Contains(afterAdd, "dynamic-skill") || strings.Contains(afterAdd, "secret body") { |
| 121 | t.Fatalf("dynamic catalog data leaked into system:\n%s", afterAdd) |
| 122 | } |
| 123 | if err := os.Remove(skillPath); err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | if err := mem.Store.Delete("dynamic-cache-fact"); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | _, afterDelete := buildSystem() |
| 130 | if afterDelete != baseline { |
| 131 | t.Fatalf("background memory/skill deletion changed system prompt: %q", firstDivergence(baseline, afterDelete)) |
| 132 | } |
| 133 | |
| 134 | writeFile(t, dir, "AGENTS.md", "Standing rule: preserve the public API.") |
| 135 | _, withStanding := buildSystem() |
| 136 | if withStanding == baseline || !strings.Contains(withStanding, "Standing rule: preserve the public API.") { |
| 137 | t.Fatalf("standing instruction did not intentionally change system:\n%s", withStanding) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestDisableImplicitSkillInvocationOmitsPolicyAndCatalogButKeepsSlashSkill(t *testing.T) { |
| 142 | isolateConfigHome(t) |
| 143 | dir := robustTempDir(t) |
| 144 | t.Chdir(dir) |
| 145 | registerBootTokenProfileTestProvider() |
| 146 | prov := testutil.NewMock("implicit-off", testutil.Turn{Text: "done"}) |
| 147 | setBootTokenProfileTestProvider(t, prov) |
| 148 | writeFile(t, dir, "reasonix.toml", ` |
| 149 | default_model = "test-model" |
| 150 | |
| 151 | [agent] |
| 152 | system_prompt = "BASE" |
| 153 | |
| 154 | [environment] |
| 155 | enabled = false |
| 156 | |
| 157 | [skills] |
| 158 | disable_implicit_invocation = true |
| 159 | |
| 160 | [[providers]] |
| 161 | name = "test-model" |
| 162 | kind = "boot-token-profile-test" |
| 163 | model = "x" |
| 164 | `) |
| 165 | writeFile(t, dir, ".reasonix/skills/hot/SKILL.md", "---\ndescription: explicit hot skill\n---\nHOT BODY") |
| 166 | |
| 167 | ctrl, err := Build(context.Background(), Options{}) |
| 168 | if err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | defer ctrl.Close() |
| 172 | if sys := systemMessage(ctrl.History()); strings.Contains(sys, "# Skills") || strings.Contains(sys, "explicit hot skill") { |
| 173 | t.Fatalf("implicit-off system contains skill policy/catalog:\n%s", sys) |
| 174 | } |
| 175 | if rendered, ok := ctrl.RunSkill("/hot now"); !ok || !strings.Contains(rendered, "HOT BODY") { |
| 176 | t.Fatalf("explicit slash skill = %q, %v", rendered, ok) |
| 177 | } |
| 178 | _ = ctrl.Run(context.Background(), "capture request prefix") |
| 179 | if req := prov.LastRequest(); req == nil || strings.Contains(sessionContextMessage(req.Messages), "explicit hot skill") { |
| 180 | t.Fatalf("implicit-off request unexpectedly contains skills catalog: %+v", req) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // firstDivergence returns a small window around the first differing byte so a |
| 185 | // failure names the drifting prompt section instead of dumping both prompts. |
| 186 | func firstDivergence(a, b string) string { |
| 187 | limit := min(len(b), len(a)) |
| 188 | i := 0 |
| 189 | for i < limit && a[i] == b[i] { |
| 190 | i++ |
| 191 | } |
| 192 | start := max(i-40, 0) |
| 193 | endA := min(i+40, len(a)) |
| 194 | endB := min(i+40, len(b)) |
| 195 | return "..." + a[start:endA] + "... vs ..." + b[start:endB] + "..." |
| 196 | } |
| 197 |