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