Add support for policy engine in extensions (#20049)

Co-authored-by: Jerop Kipruto <jerop@google.com>
This commit is contained in:
christine betts
2026-02-26 22:29:33 -05:00
committed by GitHub
parent b1befee8fb
commit e17f927a69
18 changed files with 657 additions and 89 deletions

View File

@@ -75,6 +75,21 @@ export abstract class ExtensionLoader {
await this.config.getMcpClientManager()!.startExtension(extension);
await this.maybeRefreshGeminiTools(extension);
// Register policy rules and checkers
if (extension.rules || extension.checkers) {
const policyEngine = this.config.getPolicyEngine();
if (extension.rules) {
for (const rule of extension.rules) {
policyEngine.addRule(rule);
}
}
if (extension.checkers) {
for (const checker of extension.checkers) {
policyEngine.addChecker(checker);
}
}
}
// Note: Context files are loaded only once all extensions are done
// loading/unloading to reduce churn, see the `maybeRefreshMemories` call
// below.
@@ -168,6 +183,27 @@ export abstract class ExtensionLoader {
await this.config.getMcpClientManager()!.stopExtension(extension);
await this.maybeRefreshGeminiTools(extension);
// Unregister policy rules and checkers
if (extension.rules || extension.checkers) {
const policyEngine = this.config.getPolicyEngine();
const sources = new Set<string>();
if (extension.rules) {
for (const rule of extension.rules) {
if (rule.source) sources.add(rule.source);
}
}
if (extension.checkers) {
for (const checker of extension.checkers) {
if (checker.source) sources.add(checker.source);
}
}
for (const source of sources) {
policyEngine.removeRulesBySource(source);
policyEngine.removeCheckersBySource(source);
}
}
// Note: Context files are loaded only once all extensions are done
// loading/unloading to reduce churn, see the `maybeRefreshMemories` call
// below.