AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

How to Configure Sourcegraph Cody Context Filters to Exclude Sensitive Repositories from AI Context

365Finds Editorial
Indexed 11,430 Wiki articles

Put 365 Wiki to work with your AI

Copy the prompt below, paste it into the AI you already use, then ask your question.

Download full Skill

Try it with the prompt first. If you use 365 Wiki often, install the full 365Wiki Skill so you do not have to paste it again. No regular AI? Gemini or Microsoft Copilot on the web will work.

365Wiki Skill

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:

Client Compatibility and Enforcement

To prevent developers from bypassing enterprise boundaries using outdated IDE extensions, Sourcegraph enforces policy compliance across client versions:

ClientExtension VersionSourcegraph v5.4.0Sourcegraph v5.4.12303+
VS Code< 1.16.xCody not compatibleRespects policy; prompts user to upgrade
VS Code1.18.0 to < 1.20.0Cody not compatibleRespects policy; prompts user to upgrade
VS Code>= 1.20.0Fully respects policyFully respects policy
JetBrains<= 5.5.9Cody not compatibleRespects policy; prompts user to upgrade
JetBrains5.5.10 to < 6.0.0Cody not compatibleRespects policy; prompts user to upgrade
JetBrains>= 6.0.0Fully respects policyFully 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:


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.

{
  "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.

{
  "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:

  1. It must match at least one pattern in include.
  2. 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).

Step 2: Access the Site Configuration Editor

  1. Log in to Sourcegraph with Site Administrator credentials.
  2. Click your user avatar in the top navigation and select Site Admin.
  3. 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:

  1. Open a project located in an excluded repository within VS Code or JetBrains IDE.
  2. Trigger an autocomplete prompt or highlight a code block and initiate an inline edit command.
  3. 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)
    
  4. In the Cody Chat window, attempt to @-mention a 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:


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 LayerConfiguration MechanismScope & GranularityManaged ByBypassable by User?
Enterprise Context Filterscody.contextFilters in Site AdminEntire repositories via RE2 regular expressionsSite AdministratorsNo (Centrally enforced by server)
Repository Ignore Rules.cody/ignore file in repository rootSpecific files, subdirectories, or file extensionsRepository Owners / DevelopersCommitted to Git; applies to all repo collaborators
Client Workspace ExclusionsIDE extension settings (settings.json)Local file globs or temporary file typesIndividual DevelopersYes (Controlled locally per machine)
Sourcegraph Access PermissionsRBAC, GitHub/GitLab OAuth syncRepository-level read permissionsIdentity & Access Management (IAM)No (Cody never accesses repos a user cannot view)
Upstream Data PrivacyCody Gateway Zero-Retention AgreementsAll LLM API transmissionsCloud Provider ContractsNo (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.


References