| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/agent/testutil" |
| 11 | "reasonix/internal/control" |
| 12 | "reasonix/internal/event" |
| 13 | "reasonix/internal/provider" |
| 14 | ) |
| 15 | |
| 16 | func TestUnifiedSurfaceExposesUseCapabilityForAllRoleSettings(t *testing.T) { |
| 17 | isolateConfigHome(t) |
| 18 | dir := robustTempDir(t) |
| 19 | t.Chdir(dir) |
| 20 | writeFile(t, dir, "reasonix.toml", ` |
| 21 | default_model = "test-model" |
| 22 | |
| 23 | [agent] |
| 24 | system_prompt = "BASE" |
| 25 | |
| 26 | [[providers]] |
| 27 | name = "test-model" |
| 28 | kind = "boot-token-profile-test" |
| 29 | model = "x" |
| 30 | `) |
| 31 | registerBootTokenProfileTestProvider() |
| 32 | |
| 33 | var surfaces [][]string |
| 34 | for _, mode := range []string{"", "economy", TokenModeFull, TokenModeDelivery, "light", "balanced"} { |
| 35 | prov := testutil.NewMock("ucap-"+mode, testutil.Turn{Text: "done"}) |
| 36 | setBootTokenProfileTestProvider(t, prov) |
| 37 | ctrl, err := Build(context.Background(), Options{Sink: event.Discard, TokenMode: mode, AgentPreset: mode}) |
| 38 | if err != nil { |
| 39 | t.Fatalf("Build(%q): %v", mode, err) |
| 40 | } |
| 41 | if err := ctrl.Run(context.Background(), "hello"); err != nil { |
| 42 | ctrl.Close() |
| 43 | t.Fatalf("Run(%q): %v", mode, err) |
| 44 | } |
| 45 | reqs := mainConversationRequests(prov.Requests()) |
| 46 | if len(reqs) != 1 { |
| 47 | ctrl.Close() |
| 48 | t.Fatalf("requests(%q)=%d", mode, len(reqs)) |
| 49 | } |
| 50 | surfaces = append(surfaces, toolSchemaNames(reqs[0].Tools)) |
| 51 | if !requestHasTool(reqs[0], "use_capability") { |
| 52 | ctrl.Close() |
| 53 | t.Fatalf("%q missing use_capability: %v", mode, toolSchemaNames(reqs[0].Tools)) |
| 54 | } |
| 55 | if requestHasTool(reqs[0], "connect_tool_source") { |
| 56 | ctrl.Close() |
| 57 | t.Fatalf("%q still exposes connect_tool_source", mode) |
| 58 | } |
| 59 | // grep stays registered for dispatch |
| 60 | reg := map[string]bool{} |
| 61 | for _, e := range ctrl.AllToolContractEntries() { |
| 62 | reg[e.Name] = true |
| 63 | } |
| 64 | if !reg["grep"] { |
| 65 | ctrl.Close() |
| 66 | t.Fatalf("%q registry missing grep for use_capability dispatch", mode) |
| 67 | } |
| 68 | if !reg["set_session_title"] { |
| 69 | ctrl.Close() |
| 70 | t.Fatalf("%q registry missing set_session_title for use_capability dispatch", mode) |
| 71 | } |
| 72 | ctrl.Close() |
| 73 | } |
| 74 | for i := 1; i < len(surfaces); i++ { |
| 75 | if !reflect.DeepEqual(surfaces[0], surfaces[i]) { |
| 76 | t.Fatalf("provider-visible surface diverged\nbase=%v\nother=%v", surfaces[0], surfaces[i]) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestUseCapabilityCallDispatchesHiddenGrep(t *testing.T) { |
| 82 | isolateConfigHome(t) |
| 83 | dir := robustTempDir(t) |
| 84 | t.Chdir(dir) |
| 85 | writeFile(t, dir, "a.go", "package a\n// needle_token\n") |
| 86 | writeFile(t, dir, "reasonix.toml", ` |
| 87 | default_model = "test-model" |
| 88 | |
| 89 | [agent] |
| 90 | system_prompt = "BASE" |
| 91 | |
| 92 | [[providers]] |
| 93 | name = "test-model" |
| 94 | kind = "boot-token-profile-test" |
| 95 | model = "x" |
| 96 | `) |
| 97 | registerBootTokenProfileTestProvider() |
| 98 | args, _ := json.Marshal(map[string]any{ |
| 99 | "action": "call", |
| 100 | "capability_id": "tool:grep", |
| 101 | "arguments": map[string]any{"pattern": "needle_token", "path": "."}, |
| 102 | }) |
| 103 | prov := testutil.NewMock("ucap-grep", |
| 104 | testutil.Turn{ToolCalls: []provider.ToolCall{{ID: "g1", Name: "use_capability", Arguments: string(args)}}}, |
| 105 | testutil.Turn{Text: "found"}, |
| 106 | ) |
| 107 | setBootTokenProfileTestProvider(t, prov) |
| 108 | ctrl, err := Build(context.Background(), Options{Sink: event.Discard}) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | defer ctrl.Close() |
| 113 | if err := ctrl.Run(context.Background(), "find needle"); err != nil { |
| 114 | t.Fatalf("Run: %v", err) |
| 115 | } |
| 116 | // Schema must stay lean after the call |
| 117 | for _, req := range prov.Requests() { |
| 118 | if requestHasTool(req, "grep") { |
| 119 | t.Fatalf("grep must not appear top-level after use_capability: %v", toolSchemaNames(req.Tools)) |
| 120 | } |
| 121 | } |
| 122 | var toolOut strings.Builder |
| 123 | for _, msg := range ctrl.History() { |
| 124 | if msg.Role == provider.RoleTool { |
| 125 | toolOut.WriteString(msg.Content) |
| 126 | } |
| 127 | } |
| 128 | if toolOut.Len() == 0 { |
| 129 | t.Fatal("expected use_capability/grep tool output") |
| 130 | } |
| 131 | if strings.Contains(toolOut.String(), "unavailable") || strings.Contains(toolOut.String(), "not registered") { |
| 132 | t.Fatalf("use_capability failed to dispatch grep: %s", toolOut.String()) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestUseCapabilitySetsOnlyTheCurrentSessionTitle(t *testing.T) { |
| 137 | isolateConfigHome(t) |
| 138 | dir := robustTempDir(t) |
| 139 | t.Chdir(dir) |
| 140 | sessionDir := robustTempDir(t) |
| 141 | writeFile(t, dir, "reasonix.toml", ` |
| 142 | default_model = "test-model" |
| 143 | |
| 144 | [agent] |
| 145 | system_prompt = "BASE" |
| 146 | |
| 147 | [[providers]] |
| 148 | name = "test-model" |
| 149 | kind = "boot-token-profile-test" |
| 150 | model = "x" |
| 151 | `) |
| 152 | registerBootTokenProfileTestProvider() |
| 153 | args, _ := json.Marshal(map[string]any{ |
| 154 | "action": "call", |
| 155 | "capability_id": "tool:set_session_title", |
| 156 | "arguments": map[string]any{"title": "Current integration task"}, |
| 157 | }) |
| 158 | prov := testutil.NewMock("ucap-session-title", |
| 159 | testutil.Turn{ToolCalls: []provider.ToolCall{{ID: "t1", Name: "use_capability", Arguments: string(args)}}}, |
| 160 | testutil.Turn{Text: "renamed"}, |
| 161 | ) |
| 162 | setBootTokenProfileTestProvider(t, prov) |
| 163 | var projectedPath string |
| 164 | ctrl, err := Build(context.Background(), withTestSession(t, Options{ |
| 165 | SessionDir: sessionDir, |
| 166 | Sink: event.Discard, |
| 167 | HeadlessApprovalMode: control.ToolApprovalYolo, |
| 168 | OnSessionTitleChanged: func(gotDir, gotPath, gotTitle string) error { |
| 169 | if gotDir != sessionDir || gotTitle != "Current integration task" { |
| 170 | t.Fatalf("projection = %q %q %q", gotDir, gotPath, gotTitle) |
| 171 | } |
| 172 | projectedPath = gotPath |
| 173 | return nil |
| 174 | }, |
| 175 | })) |
| 176 | if err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | defer ctrl.Close() |
| 180 | ctrl.EnsureSessionPath() |
| 181 | if err := ctrl.Run(context.Background(), "name this session"); err != nil { |
| 182 | t.Fatalf("Run: %v", err) |
| 183 | } |
| 184 | ref, ok := ctrl.SessionRef() |
| 185 | if !ok || projectedPath != ref.SessionID { |
| 186 | t.Fatalf("projected identity = %q, current = %+v", projectedPath, ref) |
| 187 | } |
| 188 | _, runtime, bound := ctrl.SessionBinding() |
| 189 | if !bound || runtime.Session().Snapshot().Projection.Title != "Current integration task" { |
| 190 | t.Fatalf("title projection = %q, want current integration task", runtime.Session().Snapshot().Projection.Title) |
| 191 | } |
| 192 | for _, req := range prov.Requests() { |
| 193 | if requestHasTool(req, "set_session_title") { |
| 194 | t.Fatalf("set_session_title leaked into provider-visible schemas: %v", toolSchemaNames(req.Tools)) |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 |