|
1 | 1 | import type OpenAI from "openai" |
2 | | -import type { ModeConfig } from "@roo-code/types" |
| 2 | +import { toolNamesSchema, type ModeConfig } from "@roo-code/types" |
3 | 3 | import type { CodeIndexManager } from "../../../../services/code-index/manager" |
4 | 4 | import { filterNativeToolsForMode } from "../filter-tools-for-mode" |
5 | 5 | import { resolveEffectiveToolPolicy } from "../effective-tool-policy" |
| 6 | +import { getNativeTools } from "../native-tools" |
6 | 7 |
|
| 8 | +const tools = toolNamesSchema.enum |
| 9 | +const ordinaryReadTools = [tools.read_file, tools.list_files, tools.search_files] |
7 | 10 | type Readiness = Pick<CodeIndexManager, "isFeatureEnabled" | "isFeatureConfigured" | "isInitialized"> |
8 | 11 |
|
9 | 12 | function makeManager(flags: Readiness): CodeIndexManager { |
10 | | - // These filters only read the three public readiness getters; no manager services are needed. |
| 13 | + // These consumers only read the public readiness getters, not manager services. |
11 | 14 | return flags as CodeIndexManager |
12 | 15 | } |
13 | 16 |
|
14 | | -const ready = { isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true } |
15 | | -const nativeTools: OpenAI.Chat.ChatCompletionTool[] = [ |
16 | | - "codebase_search", |
17 | | - "read_file", |
18 | | - "list_files", |
19 | | - "search_files", |
20 | | -].map((name) => ({ type: "function", function: { name, parameters: { type: "object", properties: {} } } })) |
21 | | -const noReadMode: ModeConfig = { slug: "no-read", name: "No read", roleDefinition: "No reading", groups: ["command"] } |
22 | | - |
23 | | -function checkAvailability( |
24 | | - manager: CodeIndexManager | undefined, |
25 | | - expected: boolean, |
26 | | - mode = "code", |
27 | | - settings: { disabledTools?: string[] } = {}, |
28 | | -) { |
29 | | - const names = filterNativeToolsForMode(nativeTools, mode, [noReadMode], {}, manager, settings).flatMap((tool) => |
30 | | - "function" in tool ? [tool.function.name] : [], |
31 | | - ) |
32 | | - const policy = resolveEffectiveToolPolicy({ |
33 | | - mode, |
34 | | - customModes: [noReadMode], |
35 | | - codeIndexManager: manager, |
36 | | - disabledTools: settings.disabledTools, |
37 | | - }) |
38 | | - expect(names.includes("codebase_search")).toBe(expected) |
39 | | - expect(policy.tools.has("codebase_search")).toBe(expected) |
40 | | - for (const tool of ["read_file", "list_files", "search_files"] as const) { |
41 | | - expect(names.includes(tool)).toBe(mode === "code") |
42 | | - expect(policy.tools.has(tool)).toBe(mode === "code") |
43 | | - } |
| 17 | +function toolNames(definitions: OpenAI.Chat.ChatCompletionTool[]) { |
| 18 | + return definitions.flatMap((tool) => ("function" in tool ? [tool.function.name] : [])) |
44 | 19 | } |
45 | 20 |
|
46 | | -describe("codebase_search readiness across mode filtering APIs", () => { |
47 | | - it("excludes search without a manager while retaining ordinary read tools", () => { |
48 | | - checkAvailability(undefined, false) |
| 21 | +describe("codebase_search readiness", () => { |
| 22 | + it("excludes search without a manager but retains ordinary read tools", () => { |
| 23 | + const policy = resolveEffectiveToolPolicy({ mode: "code" }) |
| 24 | + const filtered = filterNativeToolsForMode(getNativeTools(), "code", [], {}) |
| 25 | + |
| 26 | + expect(policy.tools).not.toContain(tools.codebase_search) |
| 27 | + expect(toolNames(filtered)).not.toContain(tools.codebase_search) |
| 28 | + for (const tool of ordinaryReadTools) { |
| 29 | + expect(policy.tools).toContain(tool) |
| 30 | + expect(toolNames(filtered)).toContain(tool) |
| 31 | + } |
49 | 32 | }) |
50 | 33 |
|
51 | | - for (const isFeatureEnabled of [false, true]) { |
52 | | - for (const isFeatureConfigured of [false, true]) { |
53 | | - for (const isInitialized of [false, true]) { |
54 | | - it(`agrees for enabled=${isFeatureEnabled}, configured=${isFeatureConfigured}, initialized=${isInitialized}`, () => { |
55 | | - checkAvailability( |
56 | | - makeManager({ isFeatureEnabled, isFeatureConfigured, isInitialized }), |
57 | | - isFeatureEnabled && isFeatureConfigured && isInitialized, |
58 | | - ) |
59 | | - }) |
| 34 | + it.each([ |
| 35 | + { isFeatureEnabled: false, isFeatureConfigured: false, isInitialized: false }, |
| 36 | + { isFeatureEnabled: false, isFeatureConfigured: false, isInitialized: true }, |
| 37 | + { isFeatureEnabled: false, isFeatureConfigured: true, isInitialized: false }, |
| 38 | + { isFeatureEnabled: false, isFeatureConfigured: true, isInitialized: true }, |
| 39 | + { isFeatureEnabled: true, isFeatureConfigured: false, isInitialized: false }, |
| 40 | + { isFeatureEnabled: true, isFeatureConfigured: false, isInitialized: true }, |
| 41 | + { isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: false }, |
| 42 | + ])( |
| 43 | + "excludes search for enabled=$isFeatureEnabled, configured=$isFeatureConfigured, initialized=$isInitialized", |
| 44 | + (flags) => { |
| 45 | + const manager = makeManager(flags) |
| 46 | + const policy = resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: manager }) |
| 47 | + const filtered = filterNativeToolsForMode(getNativeTools(), "code", [], {}, manager) |
| 48 | + |
| 49 | + expect(policy.tools).not.toContain(tools.codebase_search) |
| 50 | + expect(toolNames(filtered)).not.toContain(tools.codebase_search) |
| 51 | + for (const tool of ordinaryReadTools) { |
| 52 | + expect(policy.tools).toContain(tool) |
| 53 | + expect(toolNames(filtered)).toContain(tool) |
60 | 54 | } |
| 55 | + }, |
| 56 | + ) |
| 57 | + |
| 58 | + it("includes search when all three readiness conditions are met", () => { |
| 59 | + const manager = makeManager({ isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true }) |
| 60 | + const policy = resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: manager }) |
| 61 | + const filtered = filterNativeToolsForMode(getNativeTools(), "code", [], {}, manager) |
| 62 | + |
| 63 | + expect(policy.tools).toContain(tools.codebase_search) |
| 64 | + expect(toolNames(filtered)).toContain(tools.codebase_search) |
| 65 | + for (const tool of ordinaryReadTools) { |
| 66 | + expect(policy.tools).toContain(tool) |
| 67 | + expect(toolNames(filtered)).toContain(tool) |
61 | 68 | } |
62 | | - } |
| 69 | + }) |
63 | 70 |
|
64 | 71 | it.each(["isFeatureEnabled", "isFeatureConfigured", "isInitialized"] as const)( |
65 | | - "rereads live %s changes", |
| 72 | + "rereads %s when the same manager becomes unavailable and recovers", |
66 | 73 | (flag) => { |
67 | | - const flags = { ...ready } |
| 74 | + const flags = { isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true } |
68 | 75 | const manager = makeManager(flags) |
69 | | - checkAvailability(manager, true) |
| 76 | + |
| 77 | + expect(resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: manager }).tools).toContain( |
| 78 | + tools.codebase_search, |
| 79 | + ) |
| 80 | + expect(toolNames(filterNativeToolsForMode(getNativeTools(), "code", [], {}, manager))).toContain( |
| 81 | + tools.codebase_search, |
| 82 | + ) |
| 83 | + |
70 | 84 | flags[flag] = false |
71 | | - checkAvailability(manager, false) |
| 85 | + |
| 86 | + expect(resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: manager }).tools).not.toContain( |
| 87 | + tools.codebase_search, |
| 88 | + ) |
| 89 | + expect(toolNames(filterNativeToolsForMode(getNativeTools(), "code", [], {}, manager))).not.toContain( |
| 90 | + tools.codebase_search, |
| 91 | + ) |
| 92 | + |
72 | 93 | flags[flag] = true |
73 | | - checkAvailability(manager, true) |
| 94 | + |
| 95 | + expect(resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: manager }).tools).toContain( |
| 96 | + tools.codebase_search, |
| 97 | + ) |
| 98 | + expect(toolNames(filterNativeToolsForMode(getNativeTools(), "code", [], {}, manager))).toContain( |
| 99 | + tools.codebase_search, |
| 100 | + ) |
74 | 101 | }, |
75 | 102 | ) |
76 | 103 |
|
77 | | - it("keeps alternating managers isolated", () => { |
78 | | - const enabled = makeManager({ ...ready }) |
79 | | - const disabled = makeManager({ ...ready, isFeatureEnabled: false }) |
80 | | - checkAvailability(enabled, true) |
81 | | - checkAvailability(disabled, false) |
82 | | - checkAvailability(undefined, false) |
83 | | - checkAvailability(enabled, true) |
| 104 | + it("does not reuse readiness from another manager or a missing manager", () => { |
| 105 | + const ready = makeManager({ isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true }) |
| 106 | + const disabled = makeManager({ isFeatureEnabled: false, isFeatureConfigured: true, isInitialized: true }) |
| 107 | + |
| 108 | + expect(resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: ready }).tools).toContain( |
| 109 | + tools.codebase_search, |
| 110 | + ) |
| 111 | + expect(toolNames(filterNativeToolsForMode(getNativeTools(), "code", [], {}, ready))).toContain( |
| 112 | + tools.codebase_search, |
| 113 | + ) |
| 114 | + |
| 115 | + for (const manager of [disabled, undefined]) { |
| 116 | + expect(resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: manager }).tools).not.toContain( |
| 117 | + tools.codebase_search, |
| 118 | + ) |
| 119 | + expect(toolNames(filterNativeToolsForMode(getNativeTools(), "code", [], {}, manager))).not.toContain( |
| 120 | + tools.codebase_search, |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + expect(resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: ready }).tools).toContain( |
| 125 | + tools.codebase_search, |
| 126 | + ) |
| 127 | + expect(toolNames(filterNativeToolsForMode(getNativeTools(), "code", [], {}, ready))).toContain( |
| 128 | + tools.codebase_search, |
| 129 | + ) |
84 | 130 | }) |
85 | 131 |
|
86 | | - it("does not bypass a mode without the read group", () => { |
87 | | - checkAvailability(makeManager({ ...ready }), false, "no-read") |
| 132 | + it("does not grant read permissions merely because the manager is ready", () => { |
| 133 | + const manager = makeManager({ isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true }) |
| 134 | + const mode: ModeConfig = { slug: "no-read", name: "No read", roleDefinition: "No reading", groups: ["command"] } |
| 135 | + const policy = resolveEffectiveToolPolicy({ mode: mode.slug, customModes: [mode], codeIndexManager: manager }) |
| 136 | + const filtered = filterNativeToolsForMode(getNativeTools(), mode.slug, [mode], {}, manager) |
| 137 | + |
| 138 | + for (const tool of [tools.codebase_search, ...ordinaryReadTools]) { |
| 139 | + expect(policy.tools).not.toContain(tool) |
| 140 | + expect(toolNames(filtered)).not.toContain(tool) |
| 141 | + } |
| 142 | + // The mode remains usable; this is not an accidentally empty result. |
| 143 | + expect(policy.tools).toContain(tools.execute_command) |
| 144 | + expect(toolNames(filtered)).toContain(tools.execute_command) |
88 | 145 | }) |
89 | 146 |
|
90 | | - it("does not bypass disabledTools with a ready manager", () => { |
91 | | - checkAvailability(makeManager({ ...ready }), false, "code", { disabledTools: ["codebase_search"] }) |
| 147 | + it("honors explicit search disabling without disabling ordinary read tools", () => { |
| 148 | + const manager = makeManager({ isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true }) |
| 149 | + const disabledTools = [tools.codebase_search] |
| 150 | + const policy = resolveEffectiveToolPolicy({ mode: "code", codeIndexManager: manager, disabledTools }) |
| 151 | + const filtered = filterNativeToolsForMode(getNativeTools(), "code", [], {}, manager, { disabledTools }) |
| 152 | + |
| 153 | + expect(policy.tools).not.toContain(tools.codebase_search) |
| 154 | + expect(toolNames(filtered)).not.toContain(tools.codebase_search) |
| 155 | + for (const tool of ordinaryReadTools) { |
| 156 | + expect(policy.tools).toContain(tool) |
| 157 | + expect(toolNames(filtered)).toContain(tool) |
| 158 | + } |
92 | 159 | }) |
93 | 160 | }) |
0 commit comments