Manage Cody Context
This documentation helps you control and manage what context from your codebase is used by Cody. You can do it via
- Cody Context Filters (Cody Enterprise Only)
- Cody Ignore File (Experimental) (All Cody users)
Cody Context Filters
Admins on the Sourcegraph Enterprise instance can use the Cody Context Filters to determine which repositories Cody can use as the context in its requests to third-party LLMs. Cody Context Filters can be used when the following conditions are met:
- A valid Cody Enterprise license running on Sourcegraph instance version
>=5.4.0
- Running the supported Cody client versions: VS Code
>=1.20.0
and JetBrains>=6.0.0
- Setting the
cody-context-filters-enabled
feature flag totrue
Administrators can configure the cody.contextFilters
field in the site configuration with the repos that they wish Cody to exclude or only include using the following structure:
JSON{ // Optional. If defined, it must have at least one // field set (either `include` or `exclude`). "cody.contextFilters": { // Optional. If defined, it must have at least one item. "include": [ { // Required. Should follow the RE2 syntax. "repoNamePattern": "^github\\.com\\/sourcegraph\\/.*" } ], // Optional. If defined, it must have at least one item. "exclude": [ { // Required. Should follow the RE2 syntax. "repoNamePattern": ".*analytics.*" } ] } }
How include and exclude rules work
The include
and exclude
rules define the repositories Cody can use as context. The rules can be defined in the following combination:
cody.contextFilters
field is not defined
By default, there are no restrictions on the repositories Cody can use for context in requests to third-party LLMs. All repositories are included, and none are excluded.
- Only
include
rules are specified
Cody is restricted from using content from repositories whose names match any specified patterns in the include
field. Since no exclude
rules are specified, no repositories are explicitly excluded. An include
field might contain a catch-all pattern (e.g., a regexp
matching any string). In this case, Cody can access content from any repository, as a single match in the include
rules is sufficient for inclusion. For example,
JSON{ "cody.contextFilters": { // Only repositories whose names either start with "github.com/sourcegraph/" // or contain "cody" are allowed. "include": [ { "repoNamePattern": "^github\\.com\\/sourcegraph\\/.+" }, { "repoNamePattern": ".*cody.*" } ] } }
The include
rules allow Cody to access the repository matching the regexp
. This means Cody can now fully access the repository and its content to fetch context.
- Only
exclude
rules are specified
All repositories are considered included by default without the include
rules. Cody is prohibited from using content from any repository whose name matches at least one pattern specified in the exclude field. An exclude
field may contain a catch-all condition (e.g., a regexp
matching any string). If such a match occurs, Cody is restricted from using content from any repository. For example,
JSON{ "cody.contextFilters": { // All repositories are allowed except for "github.com/sourcegraph/cody-analytics" // or the ones containing "secret" in their name. "exclude": [ { "repoNamePattern": "^github\\.com\\/sourcegraph\\/cody-analytics$" }, { "repoNamePattern": ".*secret.*" } ] } }
In this case, Cody's commands are disabled, and you cannot use them for context fetching. If you try running any of these, you'll be prompted with an error message. However, Cody chat will still work, and you can use it to ask questions.
The demo shows what happens when you try to run commands due to the exclude
rules.
- Both
include
andexclude
rules are specified
When making requests to third-party LLMs, Cody can use content from a repository if its name matches any of the include
patterns and does not match any of the exclude
patterns. Thus, the exclude
rules filter only the repositories allowed by the include
rules. For example,
JSON{ "cody.contextFilters": { // All repositories starting with "github.com/sourcegraph/" are allowed "include": [ { repoNamePattern: "^github\\.com\\/sourcegraph\\/.+" } ], // except for "github.com/sourcegraph/cody-analytics" // and the ones containing "cody" in their name. "exclude": [ { "repoNamePattern": "^github\\.com\\/sourcegraph\\/cody-analytics$" }, { "repoNamePattern": ".*cody.*" } ] } }
In this case, Cody can access content from repositories whose names start with github.com/sourcegraph/
. However, it excludes files from repositories with cody
in their name.
Cody Context Filters Compatibility Matrix
>=5.4.0
, VS Code >=1.20.0
and JetBrains >=6.0.0
for Enterprise users.Depending on the client type, here's a breakdown of versions supported and the behavior for unsupported versions:
Client Versions | Sourcegraph v<5.4.0 | Sourcegraph v=5.4.0 | Sourcegraph v=5.4.12303 | Sourcegraph v>=5.4.0 |
---|---|---|---|---|
JetBrains <= 5.5.9 | N/A | Cody is not compatible | Respects policy and prompts to upgrade | Not affected |
5.5.10 <= JetBrains < 6.0.0 | N/A | Cody is not compatible | Respects policy and prompts to upgrade | Not affected |
JetBrains >= 6.0.0 | N/A | Respects policy | Respects policy | Not affected |
VS Code < 1.16.X | N/A | Cody is not compatible | Respects policy and prompts to upgrade | Not affected |
1.18.0 < VS Code < 1.20.0 | N/A | Cody is not compatible | Respects policy and prompts to upgrade | Not affected |
VS Code >= 1.20.0 | N/A | Respects policy | Respects policy | Not affected |
Cody Ignore Files
Cody users can configure the .cody/ignore
file to specify files or folders from your codebase to be ignored as context by Cody through the following steps:
Enabling Unstable Features
To use the ignore context feature, you first need to enable unstable features in Cody. Here's how:
- Open your settings in Cody Extension
- Next, go to the
settings.json
file - Add a new line:
"cody.unstableFeatures": true
.
This will enable experimental features, including the ignore context feature.
Using the Ignore Context Feature
To ignore specific files or folders from the context Cody took, you need to create an ignore
file for your project. Here's how:
- Create a new folder in your project root named
.cody
. - Inside the
.cody
folder, create a file namedignore
. - In the
ignore
file, specify the files or folders you want to ignore.
The .cody/ignore
file works similarly to a .gitignore
file. If you want to ignore a file named secret.json
, you would add secret.json
to your .cody/ignore
file. If you're going to ignore a folder called lib/shared
, you would add lib/shared/
to your .cody/ignore
file.
Once a file or folder is added to the .cody/ignore
file, Cody will no longer provide autocomplete suggestions for that file or folder, and it will not appear in your chat results.