Scope agent context: extract conventions, enforce git restrictions via hooks#258
Scope agent context: extract conventions, enforce git restrictions via hooks#258KRRT7 wants to merge 5 commits intomicrosoft:mainfrom
Conversation
gvanrossum
left a comment
There was a problem hiding this comment.
Not bad for a first draft, but I have some issues about what goes where.
| @@ -0,0 +1,11 @@ | |||
| { | |||
| "github.copilot.chat.agent.hooks": { | |||
There was a problem hiding this comment.
This file varies per user. We can't replace it with this. At best we could move this to README.md for humans to manually add there. (Or possibly we can add it to AGENTS.md. :-)
| @@ -1,5 +1,6 @@ | |||
| # Editor settings | |||
| .vscode | |||
| # Editor settings (but .vscode/settings.json is tracked for Copilot hooks) | |||
There was a problem hiding this comment.
So I would be against this.
| @@ -0,0 +1,11 @@ | |||
| { | |||
| "hooks": { | |||
There was a problem hiding this comment.
Same objection as for .vscode
| @@ -0,0 +1,62 @@ | |||
| When generating Python code (e.g. when translating TypeScript to Python), | |||
There was a problem hiding this comment.
I don't like hiding things under .github either. If we want to go through with this (I'm still skeptical) we should collect all the agent instructions in a single top-level directory (not hidden) and explain in AGENTS.md how to use them.
| @@ -1 +1,3 @@ | |||
| Get your instructions from AGENTS.md in the repo root. | |||
|
|
|||
| For code generation and style conventions, see [code-conventions.md](code-conventions.md). | |||
There was a problem hiding this comment.
Move this into AGENTS.md
| Never run git commands that make any changes. (`git status` and `git diff` are fine) | ||
|
|
||
| **NEVER COMMIT CODE. Do not run `git commit` or any other git commands | ||
| that make changes to the repository. Not even `git add`** | ||
|
|
There was a problem hiding this comment.
I would keep this and use the hook script as a second defense.
| When I ask to update AGENTS.md (even if maybe) extract a general rule from what I said | ||
| before and update AGENTS.md (unless it's already in there -- maybe reformulate since | ||
| it apparently didn't work). Also, when it looks like I state a general rule, add it to | ||
| AGENTS.md. In all cases show what you added to AGENTS.md. |
There was a problem hiding this comment.
Why delete this? I want it to learn from its mistakes. (Though it may have to point at a different file or files.)
|
Pushed updates addressing your feedback. Here's what changed and where I disagree: Tracked settings files ( These aren't user-specific editor preferences — they're project-level hook registrations. Both tools explicitly distinguish shared vs personal config:
The
The point of splitting is lazy loading. Three tools' own docs recommend this:
Hook rewritten in Python (fixes Windows, bypass, Rewrote
92 tests, pure Python, no subprocess dependency. AGENTS.md — trimmed, not restored You said "use the hook script as a second defense" — I took that further. The hook is the first line of defense: deterministic, zero context cost, can't be talked out of it. That means AGENTS.md doesn't need to repeat the git rules in prose. It went from 106 → 35 lines. Everything that can be enforced by a hook should be, so the agent context budget is spent on things that actually need natural language (workflow, conventions, preferences). |
…git restrictions - Extract code generation conventions from AGENTS.md into .github/code-conventions.md - Replace text-based "NEVER COMMIT" instruction with a PreToolUse hook that blocks git mutations - Register hook for Claude Code (.claude/settings.json) and VS Code Copilot (see PR notes) - Add 39 pytest tests verifying the hook blocks mutations and allows read-only commands - AGENTS.md reduced from 106 to 32 lines — only core workflow rules remain
Makes git-mutation enforcement work out-of-the-box for VS Code Copilot users instead of requiring manual setup.
- Replace bash hook with cross-platform Python script using allowlist approach, shlex parsing, git alias resolution, and chained command handling - Restore removed AGENTS.md sections (worktrees, debugging, self-learning) - Hook is first line of defense; AGENTS.md just references it - Update .claude/settings.json and .vscode/settings.json with Python invocation and Windows-specific command - Rewrite tests as pure Python unit tests (92 cases, no subprocess)
Remove restored worktrees, debugging, and self-learning sections. The hook handles git enforcement deterministically; AGENTS.md stays lean to minimize context consumption.
043c3f8 to
87b3261
Compare
Summary
AGENTS.mdinto.github/code-conventions.md— only loads when writing/generating code, not every interactionPreToolUsehook (scripts/block-git-mutations.sh) that actually blocks git mutations.claude/settings.json) and VS Code Copilot (.vscode/settings.json)AGENTS.mdgoes from 106 → 32 lines — only core workflow rules remainRelated discussion: #257
How it works
The hook script receives tool invocations as JSON on stdin, checks if it's a terminal command containing a git mutation (
commit,add,push,reset,rebase,merge,cherry-pick,revert,stash,tag), and returns adenydecision. Read-only commands (status,diff,log,show,branch) pass through.Verifying hooks
Tests: 39 pytest tests cover blocking mutations, allowing read-only commands, allowing non-git commands, and ignoring non-terminal tools:
Live verification:
/hooksto confirm the PreToolUse hook is registeredDocs
Test plan
uv run pytest tests/test_hooks.py -v— 39/39 passinggit commitin Claude Code session, verify it's blockedgit commitin VS Code Copilot session, verify it's blocked