When engineering organizations deploy Sourcegraph Cody across engineering teams, security and compliance leaders frequently raise a crucial governance concern: how can the enterprise ensure that proprietary algorithms, regulatory-restricted codebases (such as PCI-DSS, HIPAA, or ITAR data), or internal security tooling are never packaged as context and transmitted to third-party Large Language Models (LLMs)?
Sourcegraph Cody provides a centralized, server-enforced administrative mechanism called Cody Context Filters (cody.contextFilters). Managed directly within the Sourcegraph Site Configuration, context filters empower administrators to define strict inclusion and exclusion policies using regular expressions. These rules dictate exactly which repositories Cody can read to generate code completions, chat responses, and command outputs.
This guide details how engineering administrators can configure cody.contextFilters, unpacks the filtering logic and RE2 regular expression syntax, and outlines best practices for establishing multi-layered code privacy governance.
Prerequisites and Compatibility
Before configuring context filters, ensure that your infrastructure and developer workstations meet the following prerequisites:
- Sourcegraph Instance: Sourcegraph Enterprise version
5.4.0or higher with an active Cody Enterprise license. - Feature Flag: The
cody-context-filters-enabledfeature flag must be set totrue. - Supported IDE Clients:
- VS Code extension version
1.20.0or higher. - JetBrains extension version
6.0.0or higher. - Sourcegraph Web client.
- VS Code extension version
Client Compatibility and Enforcement
To prevent developers from bypassing enterprise boundaries using outdated IDE extensions, Sourcegraph enforces policy compliance across client versions:
| Client | Extension Version | Sourcegraph v5.4.0 | Sourcegraph v5.4.12303+ |
|---|---|---|---|
| VS Code | < 1.16.x | Cody not compatible | Respects policy; prompts user to upgrade |
| VS Code | 1.18.0 to < 1.20.0 | Cody not compatible | Respects policy; prompts user to upgrade |
| VS Code | >= 1.20.0 | Fully respects policy | Fully respects policy |
| JetBrains | <= 5.5.9 | Cody not compatible | Respects policy; prompts user to upgrade |
| JetBrains | 5.5.10 to < 6.0.0 | Cody not compatible | Respects policy; prompts user to upgrade |
| JetBrains | >= 6.0.0 | Fully respects policy | Fully respects policy |
On modern Sourcegraph instances, legacy clients that cannot interpret context filter directives will automatically block context access and instruct developers to upgrade their extensions, maintaining strict compliance posture.
Understanding the Configuration Schema
Context filters are declared in the Sourcegraph Site Configuration JSON under the top-level cody.contextFilters property. The schema accepts two primary arrays: include and exclude.
{
"cody.contextFilters": {
"include": [
{
"repoNamePattern": "^github\\.com\\/example-corp\\/.*"
}
],
"exclude": [
{
"repoNamePattern": ".*-sensitive$"
},
{
"repoNamePattern": "^github\\.com\\/example-corp\\/core-payments.*"
}
]
}
}
Key Schema Rules:
- If
cody.contextFiltersis defined, it must specify at least one block:include,exclude, or both. - Each block contains a list of objects containing the mandatory key
repoNamePattern. repoNamePatternmust adhere to RE2 regular expression syntax (the Google RE2 engine implemented in Go).- In JSON, all regular expression backslashes must be escaped with a double backslash (
\\). For instance, a literal dot must be written as\\., and forward slashes can be escaped as\\/or written directly as/.
Filter Evaluation Logic: Include vs. Exclude
The behavior of Sourcegraph Cody depends entirely on how the include and exclude arrays are combined:
1. Default Behavior (cody.contextFilters omitted)
When the setting is absent, Cody has unrestricted context access across every repository indexed by Sourcegraph that the requesting user has permission to read.
2. Only include Rules Defined (Strict Allowlist)
Cody is strictly restricted to pulling context from repositories whose canonical names match at least one pattern in the include list. All other repositories are excluded by default.
- Ideal for organizations adopting a Zero-Trust AI rollout, granting AI access solely to vetted, low-risk public or shared services.
- A single match in the
includearray authorizes the repository.
{
"cody.contextFilters": {
"include": [
{
"repoNamePattern": "^github\\.com\\/example-corp\\/frontend-.*"
},
{
"repoNamePattern": "^github\\.com\\/example-corp\\/shared-libraries$"
}
]
}
}
3. Only exclude Rules Defined (Denylist)
Cody can draw context from any repository except those matching at least one pattern in the exclude list.
- Ideal for organizations that want default developer productivity across general business code while blacklisting specific high-risk repositories (e.g., proprietary trading logic, biometric systems, payment vaults).
- Catch-all exclusions (e.g.,
.*) act as an immediate kill-switch, blocking Cody from fetching context across the entire instance.
{
"cody.contextFilters": {
"exclude": [
{
"repoNamePattern": "^github\\.com\\/example-corp\\/crypto-vault$"
},
{
"repoNamePattern": ".*payment-processing.*"
},
{
"repoNamePattern": ".*-restricted$"
}
]
}
}
4. Both include and exclude Rules Defined (Refined Scoping)
When both lists are provided, a repository must satisfy two conditions simultaneously:
- It must match at least one pattern in
include. - It must not match any pattern in
exclude.
Exclusion rules always override inclusion rules. In the example below, all repositories under example-corp are permitted, except for any repository containing compliance or ending in -secret:
{
"cody.contextFilters": {
"include": [
{
"repoNamePattern": "^github\\.com\\/example-corp\\/.+"
}
],
"exclude": [
{
"repoNamePattern": ".*compliance.*"
},
{
"repoNamePattern": ".*-secret$"
}
]
}
}
Step-by-Step Configuration Guide
Follow these steps to deploy and verify Cody context filters in your enterprise environment:
Step 1: Audit Repository Names in Sourcegraph
Cody matches repoNamePattern against the exact repository name as registered in Sourcegraph's database (typically formatted as <code-host>/<organization>/<repository>, such as github.com/acme-corp/billing-service or gitlab.internal.net/sec/auth-engine).
- Navigate to Site Admin > Repositories in the Sourcegraph web dashboard.
- Verify your naming conventions and catalog all repositories designated as sensitive, regulated, or restricted.
Step 2: Access the Site Configuration Editor
- Log in to Sourcegraph with Site Administrator credentials.
- Click your user avatar in the top navigation and select Site Admin.
- In the left-hand navigation sidebar, select Configuration (located at
/site-admin/configuration).
Step 3: Insert the cody.contextFilters Block
Locate a suitable position within the root JSON configuration object, insert your tailored filter block, and verify JSON syntax formatting.
{
// ... other site settings ...
"cody-context-filters-enabled": true,
"cody.contextFilters": {
"include": [
{
"repoNamePattern": "^github\\.com\\/acme-corp\\/.*"
}
],
"exclude": [
{
"repoNamePattern": "^github\\.com\\/acme-corp\\/hft-algorithm$"
},
{
"repoNamePattern": ".*proprietary.*"
},
{
"repoNamePattern": ".*vault.*"
}
]
}
}
Step 4: Save and Apply Changes
Click Save changes at the bottom of the configuration page. Sourcegraph updates its running configuration without requiring a full instance restart.
Step 5: Validate Enforcement in Developer Clients
To verify that the policy is functioning as intended:
- Open a project located in an excluded repository within VS Code or JetBrains IDE.
- Trigger an autocomplete prompt or highlight a code block and initiate an inline edit command.
- Observe the client response: Cody will decline code transmission, displaying an explanatory diagnostic message:
Edit failed to run: file is ignored (due to cody.contextFilters Enterprise configuration setting) - In the Cody Chat window, attempt to
@-mentiona file or symbol from an excluded repository. Notice that excluded repositories do not appear in context suggestion lists, and Cody does not retrieve embeddings or text snippets from them.
Developer Experience and Feature Impact
Understanding how context filters affect developers in their daily workflows helps administrators communicate changes smoothly:
- General Chat Availability: Even when a developer works inside an excluded repository, Cody Chat remains open for general coding assistance (e.g., explaining algorithms, generating boilerplate, or debugging generic snippets). However, Cody will not inject local repository context into the prompt payload.
- Feature Restrictions: Certain advanced chat features, such as custom reusable Prompts that expect repository-wide context binding, may display error messages or be disabled when executed on excluded repositories.
- Autocomplete Suppression: Cody Autocomplete respects context filters in real time. If the active document belongs to an excluded repository, no contextual chunks from that codebase are dispatched to LLM endpoints.
Multi-Layered Governance: Context Filters vs. Alternative Controls
Enterprise code governance requires a defense-in-depth architecture. The table below illustrates where cody.contextFilters fits alongside other security capabilities in the Sourcegraph ecosystem:
| Governance Layer | Configuration Mechanism | Scope & Granularity | Managed By | Bypassable by User? |
|---|---|---|---|---|
| Enterprise Context Filters | cody.contextFilters in Site Admin | Entire repositories via RE2 regular expressions | Site Administrators | No (Centrally enforced by server) |
| Repository Ignore Rules | .cody/ignore file in repository root | Specific files, subdirectories, or file extensions | Repository Owners / Developers | Committed to Git; applies to all repo collaborators |
| Client Workspace Exclusions | IDE extension settings (settings.json) | Local file globs or temporary file types | Individual Developers | Yes (Controlled locally per machine) |
| Sourcegraph Access Permissions | RBAC, GitHub/GitLab OAuth sync | Repository-level read permissions | Identity & Access Management (IAM) | No (Cody never accesses repos a user cannot view) |
| Upstream Data Privacy | Cody Gateway Zero-Retention Agreements | All LLM API transmissions | Cloud Provider Contracts | No (Code is not retained or trained on) |
By layering cody.contextFilters on top of native Sourcegraph repository access permissions and .cody/ignore files for local secrets, engineering organizations establish a watertight security boundary. Sensitive source code stays locked within your enterprise network, while your engineering teams benefit from generative AI acceleration across authorized projects.