-
Notifications
You must be signed in to change notification settings - Fork 276
fix: align codebase search readiness across mode filters #1630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WebMad
wants to merge
4
commits into
Zoo-Code-Org:main
Choose a base branch
from
WebMad:fix/codebase-search-readiness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2afa9f1
refactor(code-index): extract manager registry
WebMad 2dc6f28
test(task): mock code index registry in task suite
WebMad 8637e48
test(code-index): remove redundant context casts
WebMad c4f0f8a
fix: align codebase search readiness across mode filters
WebMad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/core/prompts/tools/__tests__/codebase-search-readiness.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import type OpenAI from "openai" | ||
| import type { ModeConfig } from "@roo-code/types" | ||
| import type { CodeIndexManager } from "../../../../services/code-index/manager" | ||
| import { filterNativeToolsForMode, getAvailableToolsInGroup, isToolAllowedInMode } from "../filter-tools-for-mode" | ||
|
|
||
| type Readiness = Pick<CodeIndexManager, "isFeatureEnabled" | "isFeatureConfigured" | "isInitialized"> | ||
|
|
||
| function makeManager(flags: Readiness): CodeIndexManager { | ||
| // These filters only read the three public readiness getters; no manager services are needed. | ||
| return flags as CodeIndexManager | ||
| } | ||
|
|
||
| const ready = { isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true } | ||
| const nativeTools: OpenAI.Chat.ChatCompletionTool[] = [ | ||
| "codebase_search", | ||
| "read_file", | ||
| "list_files", | ||
| "search_files", | ||
| ].map((name) => ({ type: "function", function: { name, parameters: { type: "object", properties: {} } } })) | ||
| const noReadMode: ModeConfig = { slug: "no-read", name: "No read", roleDefinition: "No reading", groups: ["command"] } | ||
|
|
||
| function checkAvailability( | ||
| manager: CodeIndexManager | undefined, | ||
| expected: boolean, | ||
| mode = "code", | ||
| settings: { disabledTools?: string[] } = {}, | ||
| ) { | ||
| const names = filterNativeToolsForMode(nativeTools, mode, [noReadMode], {}, manager, settings).flatMap((tool) => | ||
| "function" in tool ? [tool.function.name] : [], | ||
| ) | ||
| const group = getAvailableToolsInGroup("read", mode, [noReadMode], {}, manager, settings) | ||
| expect(names.includes("codebase_search")).toBe(expected) | ||
| expect(isToolAllowedInMode("codebase_search", mode, [noReadMode], {}, manager, settings)).toBe(expected) | ||
| expect(group.includes("codebase_search")).toBe(expected) | ||
| for (const tool of ["read_file", "list_files", "search_files"] as const) { | ||
| expect(names.includes(tool)).toBe(mode === "code") | ||
| expect(isToolAllowedInMode(tool, mode, [noReadMode], {}, manager, settings)).toBe(mode === "code") | ||
| expect(group.includes(tool)).toBe(mode === "code") | ||
| } | ||
| } | ||
|
|
||
| describe("codebase_search readiness across mode filtering APIs", () => { | ||
| it("excludes search without a manager while retaining ordinary read tools", () => { | ||
| checkAvailability(undefined, false) | ||
| }) | ||
|
|
||
| for (const isFeatureEnabled of [false, true]) { | ||
| for (const isFeatureConfigured of [false, true]) { | ||
| for (const isInitialized of [false, true]) { | ||
| it(`agrees for enabled=${isFeatureEnabled}, configured=${isFeatureConfigured}, initialized=${isInitialized}`, () => { | ||
| checkAvailability( | ||
| makeManager({ isFeatureEnabled, isFeatureConfigured, isInitialized }), | ||
| isFeatureEnabled && isFeatureConfigured && isInitialized, | ||
| ) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| it.each(["isFeatureEnabled", "isFeatureConfigured", "isInitialized"] as const)( | ||
| "rereads live %s changes", | ||
| (flag) => { | ||
| const flags = { ...ready } | ||
| const manager = makeManager(flags) | ||
| checkAvailability(manager, true) | ||
| flags[flag] = false | ||
| checkAvailability(manager, false) | ||
| flags[flag] = true | ||
| checkAvailability(manager, true) | ||
| }, | ||
| ) | ||
|
|
||
| it("keeps alternating managers isolated", () => { | ||
| const enabled = makeManager({ ...ready }) | ||
| const disabled = makeManager({ ...ready, isFeatureEnabled: false }) | ||
| checkAvailability(enabled, true) | ||
| checkAvailability(disabled, false) | ||
| checkAvailability(undefined, false) | ||
| checkAvailability(enabled, true) | ||
| }) | ||
|
|
||
| it("does not bypass a mode without the read group", () => { | ||
| checkAvailability(makeManager({ ...ready }), false, "no-read") | ||
| }) | ||
|
|
||
| it("does not bypass disabledTools with a ready manager", () => { | ||
| checkAvailability(makeManager({ ...ready }), false, "code", { disabledTools: ["codebase_search"] }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/core/task/__tests__/build-tools-readiness.integration.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import type OpenAI from "openai" | ||
| import type { CodeIndexManager } from "../../../services/code-index/manager" | ||
| import { CodeIndexManagerRegistry } from "../../../services/code-index/code-index-manager-registry" | ||
| import { makeExtensionContext } from "../../../test-utils/vscode" | ||
| import type { ClineProvider } from "../../webview/ClineProvider" | ||
| import { buildNativeToolsArrayWithRestrictions } from "../build-tools" | ||
|
|
||
| vi.mock("../../../services/code-index/code-index-manager-registry", () => ({ | ||
| CodeIndexManagerRegistry: { getInstance: vi.fn() }, | ||
| })) | ||
|
|
||
| function toolNames(tools: OpenAI.Chat.ChatCompletionTool[]) { | ||
| return tools.flatMap((tool) => ("function" in tool ? [tool.function.name] : [])) | ||
| } | ||
|
|
||
| describe("task tool building with real readiness filtering", () => { | ||
| beforeEach(() => vi.clearAllMocks()) | ||
|
|
||
| it.each([false, true])("uses task cwd/context and live manager readiness (restrictions=%s)", async (restricted) => { | ||
| const context = makeExtensionContext() | ||
| // The builder only consumes context and getMcpHub; avoid constructing the webview provider. | ||
| const provider = { context, getMcpHub: () => undefined } as ClineProvider | ||
| const flags = { isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true } | ||
| // Only the public readiness getters are consumed by the real filter. | ||
| const readyManager = flags as CodeIndexManager | ||
| const unreadyManager = { ...flags, isInitialized: false } as CodeIndexManager | ||
| const managers = new Map([ | ||
| ["/tasks/ready", readyManager], | ||
| ["/tasks/unready", unreadyManager], | ||
| ]) | ||
| vi.mocked(CodeIndexManagerRegistry.getInstance).mockImplementation((receivedContext, cwd) => { | ||
| expect(receivedContext).toBe(context) | ||
| return managers.get(cwd ?? "") | ||
| }) | ||
|
|
||
| async function check(cwd: string, expected: boolean, mode = "code", disabledTools: string[] = []) { | ||
| const result = await buildNativeToolsArrayWithRestrictions({ | ||
| provider, | ||
| cwd, | ||
| mode, | ||
| customModes: [{ slug: "no-read", name: "No read", roleDefinition: "No reading", groups: ["command"] }], | ||
| experiments: {}, | ||
| apiConfiguration: {}, | ||
| disabledTools, | ||
| includeAllToolsWithRestrictions: restricted, | ||
| }) | ||
| expect(CodeIndexManagerRegistry.getInstance).toHaveBeenLastCalledWith(context, cwd) | ||
| const definitions = toolNames(result.tools) | ||
| const callable = restricted ? result.allowedFunctionNames : definitions | ||
| expect(callable).toBeDefined() | ||
| expect(callable?.includes("codebase_search")).toBe(expected) | ||
| expect(callable?.includes("read_file")).toBe(mode === "code") | ||
| if (restricted) { | ||
| // Historical definitions remain present; only allowedFunctionNames controls calls. | ||
| expect(definitions).toContain("codebase_search") | ||
| } else { | ||
| expect(result.allowedFunctionNames).toBeUndefined() | ||
| } | ||
| } | ||
|
|
||
| await check("/tasks/ready", true) | ||
| await check("/tasks/unready", false) | ||
| await check("/tasks/missing", false) | ||
| await check("/tasks/ready", true) | ||
| for (const flag of ["isFeatureEnabled", "isFeatureConfigured", "isInitialized"] as const) { | ||
| flags[flag] = false | ||
| await check("/tasks/ready", false) | ||
| flags[flag] = true | ||
| await check("/tasks/ready", true) | ||
| } | ||
| await check("/tasks/ready", false, "no-read") | ||
| await check("/tasks/ready", false, "code", ["codebase_search"]) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Cover omitted
settings.The three filtering APIs accept optional settings. The helper default converts omitted settings to
{}, so the readiness tests do not exercisesettings === undefined. Remove the default so the existing ready-manager cases fail ifsettings?.disabledToolsbecomessettings.disabledTools.📝 Committable suggestion
🤖 Prompt for AI Agents