Skip to content

Commit

Permalink
[Backport M72] fix(agentic chat): terminal and openCtx are not regist…
Browse files Browse the repository at this point in the history
…ered as tools (#7133)

CLOSE https://linear.app/sourcegraph/issue/CODY-5063

Regression caused by #6909

The PR has moved the openCtx controller to be initialized before
CodyToolProvider is initialized, causing the current logic to set up
openctx provider listeners to fail.

This PR fixes the issue:

Move the OpenCtx provider listener setup from the controller
initialization to the CodyToolProvider initialization to ensure proper
setup timing and avoid circular dependencies.

## Test plan

- Verify OpenCtx provider listener is properly initialized when
CodyToolProvider is initialized
- Confirm no functionality changes in OpenCtx controller behavior
- Ensure existing OpenCtx features continue to work as expected

Ask Cody `summarize my staged changes using the CLI tool` in agentic
chat

Before:


![image](https://github.com/user-attachments/assets/f7da91f0-aad4-4f91-a8fc-d77b5472f451)

After:


![image](https://github.com/user-attachments/assets/95eae743-7cad-4f63-8d9f-0797770292c0)

 <br> Backport 3e79608 from #7130

Co-authored-by: Beatrix <[email protected]>
  • Loading branch information
sourcegraph-release-bot and abeatrix authored Feb 23, 2025
1 parent d6a8cfa commit 58372b0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
16 changes: 6 additions & 10 deletions vscode/src/chat/agentic/CodyToolProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type ContextItem, ContextItemSource, ps } from '@sourcegraph/cody-shared'
import { DeepCodyAgentID } from '@sourcegraph/cody-shared/src/models/client'
import { Observable } from 'observable-fns'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { URI } from 'vscode-uri'
Expand Down Expand Up @@ -63,24 +62,21 @@ describe('CodyToolProvider', () => {

beforeEach(() => {
vi.clearAllMocks()
CodyToolProvider.initialize(mockContextRetriever)
vi.spyOn(openctxAPI, 'openctxController', 'get').mockReturnValue(Observable.of(mockController))
})

it('should register default tools on initialization', () => {
it('should register default tools on initialization', async () => {
CodyToolProvider.initialize(mockContextRetriever)
await new Promise(resolve => setTimeout(resolve, 0))
expect(mockController.metaChanges).toHaveBeenCalled()
const tools = CodyToolProvider.getTools()
expect(tools.length).toBeGreaterThan(0)
expect(tools.some(tool => tool.config.title.includes('Code Search'))).toBe(true)
expect(tools.some(tool => tool.config.title.includes('Cody Memory'))).toBe(true)
})

it('should set up OpenCtx provider listener and build OpenCtx tools from provider metadata', async () => {
CodyToolProvider.setupOpenCtxProviderListener()
await new Promise(resolve => setTimeout(resolve, 0))
expect(mockController.metaChanges).toHaveBeenCalled()
// Wait for the observable to emit
await new Promise(resolve => setTimeout(resolve, 0))

const tools = CodyToolProvider.getTools()
expect(tools.some(tool => tool.config.title === 'Test Provider')).toBeTruthy()
expect(tools.some(tool => tool.config.title === 'Test Provider MCP')).toBeTruthy()
Expand All @@ -92,7 +88,7 @@ describe('CodyToolProvider', () => {

it('should not include CLI tool if shell is disabled', () => {
vi.spyOn(toolboxManager, 'getSettings').mockReturnValue({
agent: { name: DeepCodyAgentID },
agent: { name: 'deep-cody' },
shell: { enabled: false },
})
const tools = CodyToolProvider.getTools()
Expand All @@ -101,7 +97,7 @@ describe('CodyToolProvider', () => {

it('should include CLI tool if shell is enabled', () => {
vi.spyOn(toolboxManager, 'getSettings').mockReturnValue({
agent: { name: DeepCodyAgentID },
agent: { name: 'deep-cody' },
shell: { enabled: true },
})
const tools = CodyToolProvider.getTools()
Expand Down
3 changes: 2 additions & 1 deletion vscode/src/chat/agentic/CodyToolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ export class CodyToolProvider {

public static initialize(contextRetriever: Retriever): void {
CodyToolProvider.instance = new CodyToolProvider(contextRetriever)
CodyToolProvider.setupOpenCtxProviderListener()
}

public static getTools(): CodyTool[] {
return CodyToolProvider.instance?.factory.getInstances() ?? []
}

public static setupOpenCtxProviderListener(): void {
private static setupOpenCtxProviderListener(): void {
const provider = CodyToolProvider.instance
if (provider && !CodyToolProvider.configSubscription) {
CodyToolProvider.configSubscription = toolboxManager.observable.subscribe({})
Expand Down
2 changes: 0 additions & 2 deletions vscode/src/context/openctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import type {
} from '@openctx/client'
import type { createController } from '@openctx/vscode-lib'
import { Observable, map } from 'observable-fns'
import { CodyToolProvider } from '../chat/agentic/CodyToolProvider'
import { logDebug } from '../output-channel-logger'
import { createCodeSearchProvider } from './openctx/codeSearch'
import { gitMentionsProvider } from './openctx/git'
Expand Down Expand Up @@ -113,7 +112,6 @@ export function observeOpenCtxController(
),
mergeConfiguration,
})
CodyToolProvider.setupOpenCtxProviderListener()
return controller
} catch (error) {
logDebug('openctx', `Failed to load OpenCtx client: ${error}`)
Expand Down

0 comments on commit 58372b0

Please sign in to comment.