我开发了一个Claude代码插件,可以在变更发生之前检测并阻止它。
我们都经历过这样的情况:你给Claude Code一个简单的提示,比如“修复utils.js中的拼写错误”,结果它却开始重构你的整个配置文件或添加无关的导入。这不仅令人烦恼——这就是范围蔓延,AI超出了任务的边界,进行了未经授权的修改,这可能会削弱信任、使代码库膨胀,并带来安全风险。但如果我们将范围蔓延视为一种可以主动防范的问题,而不是大型语言模型(LLMs)不可避免的特性呢?
受到这里和X平台上关于AI代理失控、出现幻觉或过度编辑的讨论的启发,我决定构建Scope Guard:一个轻量级的Claude Code插件,它在代理完成工作之前进行干预,确保编辑严格遵循你指定的意图。
### 为什么范围蔓延很重要
范围蔓延的发生是因为像Claude这样的LLMs被训练得非常乐于助人,但没有明确的边界时,它们会推断过多——导致对你没有提到的文件进行更改,或者添加看似“显然”相关但实际上并不相关的内容。在生产工作流程中,这意味着需要更多的时间来调试、审查或回滚。最近的讨论(例如关于Claude Code中的幻觉)显示,随着我们越来越依赖AI进行编码,这已成为一个日益严重的问题。
### 介绍Scope Guard
Scope Guard是一个零依赖的JavaScript插件,它接入Claude Code的生命周期,以强制执行提示遵循。它的设计极简:无需API密钥,无需配置文件,只需即插即用。
#### 主要功能:
- **提示捕获**:保存你的原始指令以供参考。
- **实时变更跟踪**:异步记录编辑,而不会减慢代理的速度。
- **严格评估**:使用Git差异分析工具使用后的修改。仅允许对明确提到的文件或逻辑上必需的文件(例如,函数更新的测试文件)进行更改。其他所有内容?都被视为范围蔓延而阻止。
- **用户友好的干预**:如果检测到范围蔓延,它会停止完成并解释原因,并提供批准、撤销或细化提示的选项。
- **清理脚本**:通过删除旧会话数据来保持项目整洁。
### 工作原理
通过在Claude Code中安装`/plugins add https://github.com/andreahlert/scope-guard`来进行安装。
- 提交提示时,它会捕获意图。
- 当代理进行编辑时,变更会被跟踪。
- 在停止之前,它会评估:这个文件被提到了吗?这个更改是必要的吗?如果不是——干预就会发生。
#### 示例:
- **允许**:提示“将电子邮件验证添加到auth.js”→编辑auth.js和auth.test.js。
- **阻止**:相同提示→尝试修改无关的db.js?不可以。
它遵循AGPL-3.0许可证,代码库开放以供贡献。你可以在这里查看:https://github.com/andreahlert/scope-guard。如果你觉得这个项目有用,请给它加星;如果你想进行修改,可以分叉它;或者在问题区留下反馈——我很想听听它在你的工作流程中如何适用(或不适用)。
你怎么看?你在使用Claude Code时遇到过范围蔓延的问题吗?欢迎在下方讨论!
祝好,
André
查看原文
We've all been there: You give Claude Code a simple prompt like "Fix the typo in utils.js," and suddenly it's refactoring your entire config file or adding unrelated imports. This isn't just annoying - it's scope creep, where the AI oversteps the task boundaries, introducing unauthorized modifications that can erode trust, bloat your codebase, and create security risks. But what if we treated scope creep not as an inevitable quirk of LLMs, but as something we can proactively guard against?<p>Inspired by discussions here and on X about AI agents going rogue with hallucinations or excessive edits, I decided to build Scope Guard: a lightweight plugin for Claude Code that intervenes before the agent completes its work, ensuring edits stay strictly within your specified intent.<p>Why Scope Creep Matters
Scope creep happens because LLMs like Claude are trained to be helpful, but without explicit boundaries, they infer too much—leading to changes in files you didn't mention, or additions that seem "obviously" related but aren't. In production workflows, this means more time debugging, reviewing, or rolling back. Recent threads (like those on hallucinations in Claude Code) show this is a growing pain point as we rely more on AI for coding.<p>Introducing Scope Guard
Scope Guard is a zero-dependency JavaScript plugin that hooks into Claude Code's lifecycle to enforce prompt adherence. It's minimalist by design: no API keys, no config files, just plug-and-play.<p>Key Features:
Prompt Capture: Saves your original instruction for reference.<p>Real-Time Change Tracking: Logs edits asynchronously without slowing down the agent.<p>Strict Evaluation: Uses Git diffs to analyze modifications post-tool use. Only allows changes to explicitly mentioned files or those logically essential (e.g., a test file for a function update). Everything else? Blocked as scope creep.<p>User-Friendly Intervention: If creep is detected, it halts completion and explains why, with options to approve, undo, or refine your prompt.<p>Cleanup Script: Keeps your project tidy by removing old session data.<p>How It Works
Install via /plugins add https://github.com/andreahlert/scope-guard in Claude Code.<p>On prompt submit, it captures the intent.<p>As the agent edits, changes are tracked.<p>Before stopping, it evaluates: Is this file mentioned? Is the change required? If not—bam, intervention.<p>Examples:<p>Allowed: Prompt "Add email validation to auth.js" → Edits auth.js and auth.test.js.<p>Blocked: Same prompt → Tries to tweak unrelated db.js? Nope.<p>It's licensed under AGPL-3.0, and the repo is open for contributions. Check it out here: https://github.com/andreahlert/scope-guard. Star it if this resonates, fork it if you want to hack on it, or drop feedback in the issues—I'd love to hear how it fits (or doesn't) in your workflow.<p>What do you think? Have you run into scope creep with Claude Code? Let's discuss below!<p>Cheers,
André